x64.inc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*++
  2. Copyright (c) 2015 Minoca Corp.
  3. This file is licensed under the terms of the GNU General Public License
  4. version 3. Alternative licensing terms are available. Contact
  5. info@minocacorp.com for details. See the LICENSE file at the root of this
  6. project for complete licensing information.
  7. Module Name:
  8. x64.inc
  9. Abstract:
  10. This module contains common definitions for the 64-bit x86 architecture.
  11. Author:
  12. Evan Green 17-Jan-2015
  13. Environment:
  14. Any
  15. --*/
  16. //
  17. // ------------------------------------------------------------------ Includes
  18. //
  19. #include <minoca/kernel/x86defs.h>
  20. //
  21. // --------------------------------------------------------------- Definitions
  22. //
  23. //
  24. // Basic constants.
  25. //
  26. #define FALSE 0
  27. #define TRUE 1
  28. #define EXCEPTION_NMI 0x02
  29. #define EXCEPTION_BREAK 0x03
  30. #define EXCEPTION_SINGLE_STEP 0x04
  31. #define EXCEPTION_ACCESS_VIOLATION 0x05
  32. #define EXCEPTION_ASSERTION_FAILURE 0x07
  33. #define EXCEPTION_DOUBLE_FAULT 0x0C
  34. #define CONTEXT_SWAP_MAGIC 0x5A4A3A2A
  35. //
  36. // Relevant TSS structure definitions.
  37. //
  38. #define TSS_RSP0 0x04
  39. //
  40. // Processor block offsets.
  41. //
  42. #define PROCESSOR_BLOCK_TSS 0x10
  43. //
  44. // Definition for the TRAP_FRAME structure and the exception stack directly
  45. // above it.
  46. //
  47. #define TRAP_DS 0
  48. #define TRAP_ES 4
  49. #define TRAP_FS 8
  50. #define TRAP_GS 12
  51. #define TRAP_PADDING 16
  52. #define TRAP_RAX 24
  53. #define TRAP_RBX 32
  54. #define TRAP_RCX 40
  55. #define TRAP_RDX 48
  56. #define TRAP_RSI 56
  57. #define TRAP_RDI 64
  58. #define TRAP_RBP 72
  59. #define TRAP_R8 80
  60. #define TRAP_R9 88
  61. #define TRAP_R10 96
  62. #define TRAP_R11 104
  63. #define TRAP_R12 112
  64. #define TRAP_R13 120
  65. #define TRAP_R14 128
  66. #define TRAP_R15 136
  67. #define TRAP_ERRORCODE 144
  68. #define TRAP_RIP 152
  69. #define TRAP_CS 160
  70. #define TRAP_RFLAGS 168
  71. #define TRAP_RSP 176
  72. #define TRAP_SS 184
  73. #define TRAP_FRAME_SIZE 192
  74. //
  75. // Define the system call numbers used by assembly, and some relevant
  76. // structure sizes.
  77. //
  78. #define SystemCallRestoreContext 1
  79. #define SystemCallForkProcess 2
  80. #define PROCESSOR_CONTEXT_SIZE 284
  81. #define SIGNAL_CONTEXT_SIZE 48
  82. //
  83. // Define the minimum and maximum external interrupt vectors.
  84. //
  85. #define MINIMUM_VECTOR 0x30
  86. #define MAXIMUM_VECTOR 0xFF
  87. //
  88. // APIC End Of Interrupt Offset.
  89. //
  90. #define APIC_EOI_OFFSET 0x0B
  91. //
  92. // -------------------------------------------------------------------- Macros
  93. //
  94. //
  95. // This macro goes at the start of every assembly file.
  96. //
  97. #define ASSEMBLY_FILE_HEADER \
  98. .text ; \
  99. .code64
  100. //
  101. // This macro loads DS, ES, and GS with the correct values for the kernel.
  102. //
  103. #define LOAD_KERNEL_DATA_SEGMENTS \
  104. movw $KERNEL_DS, %ax ; \
  105. mov %ax, %ds ; \
  106. mov %ax, %ss ; \
  107. mov %ax, %es ; \
  108. xorl %eax, %eax ; \
  109. mov %ax, %fs ; \
  110. mov %ax, %gs ;
  111. #if defined(__WINNT__) || defined(__CYGWIN__)
  112. #define FUNCTION(_Name) \
  113. _Name: \
  114. .def _##_Name; .scl 2; .type 32; .endef ; \
  115. .global _##_Name ; \
  116. _##_Name:
  117. #define PROTECTED_FUNCTION(_Name) FUNCTION(_Name)
  118. #define EXPORTED_FUNCTION(_Name) FUNCTION(_Name)
  119. #define END_FUNCTION(_Name)
  120. #elif defined(__ELF__)
  121. //
  122. // This macro defines a function, callable from C code in any module and
  123. // capable of being overridden by other functions.
  124. //
  125. #define EXPORTED_FUNCTION(_Name) \
  126. .func _Name ; \
  127. .type _Name, %function ; \
  128. .cfi_startproc ; \
  129. .cfi_def_cfa %rsp, 8 ; \
  130. .cfi_offset %rip, -8 ; \
  131. .global _Name ; \
  132. _Name:
  133. //
  134. // This macro defines a function, callable from C code in the current module
  135. // only.
  136. //
  137. #define FUNCTION(_Name) \
  138. .hidden _Name ; \
  139. EXPORTED_FUNCTION(_Name)
  140. //
  141. // This macro defines a function, callable from C code in any module but always
  142. // called locally in the current module.
  143. //
  144. #define PROTECTED_FUNCTION(_Name) \
  145. .protected _Name ; \
  146. EXPORTED_FUNCTION(_Name)
  147. #define END_FUNCTION(_Name) \
  148. .size _Name, .-_Name ; \
  149. .endfunc ; \
  150. .cfi_endproc
  151. #elif defined(__APPLE__)
  152. #define FUNCTION(_Name) \
  153. .global _##_Name ; \
  154. _##_Name:
  155. #define PROTECTED_FUNCTION(_Name) FUNCTION(_Name)
  156. #define EXPORTED_FUNCTION(_Name) FUNCTION(_Name)
  157. #define END_FUNCTION(_Name)
  158. #else
  159. #define FUNCTION(_Name) \
  160. .global _Name ; \
  161. _Name:
  162. #define PROTECTED_FUNCTION(_Name) FUNCTION(_Name)
  163. #define EXPORTED_FUNCTION(_Name) FUNCTION(_Name)
  164. #define END_FUNCTION(_Name)
  165. #endif
  166. //
  167. // This macro sets the call frame information so that the debugger can unwind
  168. // a trap frame. It assumes the CFA register is esp, and sets the CFA to the
  169. // base of the trap frame just to make things easier.
  170. //
  171. #define CFI_TRAP_FRAME_PUSHED \
  172. .cfi_def_cfa_offset 0 ; \
  173. .cfi_offset %rax, TRAP_RAX ; \
  174. .cfi_offset %rbx, TRAP_RBX ; \
  175. .cfi_offset %rcx, TRAP_RCX ; \
  176. .cfi_offset %rdx, TRAP_RDX ; \
  177. .cfi_offset %rsi, TRAP_RSI ; \
  178. .cfi_offset %rdi, TRAP_RDI ; \
  179. .cfi_offset %rbp, TRAP_RBP ; \
  180. .cfi_offset %r8, TRAP_R8 ; \
  181. .cfi_offset %r9, TRAP_R9 ; \
  182. .cfi_offset %r10, TRAP_R10 ; \
  183. .cfi_offset %r11, TRAP_R11 ; \
  184. .cfi_offset %r12, TRAP_R12 ; \
  185. .cfi_offset %r13, TRAP_R13 ; \
  186. .cfi_offset %r14, TRAP_R14 ; \
  187. .cfi_offset %r15, TRAP_R15 ; \
  188. .cfi_offset %rip, TRAP_RIP ; \
  189. .cfi_offset %rsp, TRAP_RSP ; \
  190. .cfi_offset %rflags, TRAP_RFLAGS
  191. //
  192. // This macro sets the call frame information just after a trap frame was
  193. // restored. It indicates to the debugger that most registers are now in their
  194. // proper place. It assumes the CFA register is esp+0.
  195. //
  196. #define CFI_TRAP_FRAME_POPPED \
  197. .cfi_same_value %rax ; \
  198. .cfi_same_value %rbx ; \
  199. .cfi_same_value %rcx ; \
  200. .cfi_same_value %rdx ; \
  201. .cfi_same_value %rsi ; \
  202. .cfi_same_value %rdi ; \
  203. .cfi_same_value %rbp ; \
  204. .cfi_same_value %r8 ; \
  205. .cfi_same_value %r9 ; \
  206. .cfi_same_value %r10 ; \
  207. .cfi_same_value %r11 ; \
  208. .cfi_same_value %r12 ; \
  209. .cfi_same_value %r13 ; \
  210. .cfi_same_value %r14 ; \
  211. .cfi_same_value %r15 ; \
  212. .cfi_offset %rip, 0
  213. //
  214. // Define .cfi directives, macroed so they can be excised if unneeded.
  215. //
  216. #define CFI_DEF_CFA(_Register, _Offset) .cfi_def_cfa _Register, _Offset
  217. #define CFI_DEF_CFA_OFFSET(_Offset) .cfi_def_cfa_offset _Offset
  218. #define CFI_ADJUST_CFA_OFFSET(_Amount) .cfi_adjust_cfa_offset _Amount
  219. #define CFI_OFFSET(_Register, _Offset) .cfi_offset _Register, _Offset
  220. #define CFI_UNDEFINED(_Register) .cfi_undefined _Register
  221. #define CFI_SAME_VALUE(_Register) .cfi_same_value _Register