x86.inc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*++
  2. Copyright (c) 2012 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. x86.inc
  9. Abstract:
  10. This module contains common definitions for the x86 architecture.
  11. Author:
  12. Evan Green 18-Jul-2012
  13. Environment:
  14. Kernel mode
  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_ESP0 0x04
  39. //
  40. // Processor block offsets.
  41. //
  42. #define PROCESSOR_BLOCK_TSS 0x0C
  43. #define PROCESSOR_BLOCK_GDT 0x10
  44. //
  45. // Definition for the TRAP_FRAME structure and the exception stack directly
  46. // above it.
  47. //
  48. #define TRAP_DS 0
  49. #define TRAP_ES 4
  50. #define TRAP_FS 8
  51. #define TRAP_GS 12
  52. #define TRAP_SS 16
  53. #define TRAP_EAX 20
  54. #define TRAP_EBX 24
  55. #define TRAP_ECX 28
  56. #define TRAP_EDX 32
  57. #define TRAP_ESI 36
  58. #define TRAP_EDI 40
  59. #define TRAP_EBP 44
  60. #define TRAP_ERRORCODE 48
  61. #define TRAP_EIP 52
  62. #define TRAP_CS 56
  63. #define TRAP_EFLAGS 60
  64. #define TRAP_ESP 64
  65. #define TRAP_RET_ERRORCODE 68
  66. #define TRAP_RET_EIP 72
  67. #define TRAP_RET_CS 76
  68. #define TRAP_RET_EFLAGS 80
  69. #define TRAP_RET_ESP 84
  70. #define TRAP_RET_SS 88
  71. #define TRAP_RET_ES 92
  72. #define TRAP_RET_DS 96
  73. #define TRAP_RET_FS 100
  74. #define TRAP_RET_GS 104
  75. #define TRAP_FRAME_SIZE 68
  76. #define PROCESSOR_CONTEXT_SIZE 0x60
  77. #define SIGNAL_CONTEXT_SIZE 32
  78. //
  79. // Define the minimum and maximum external interrupt vectors.
  80. //
  81. #define MINIMUM_VECTOR 0x30
  82. #define MAXIMUM_VECTOR 0xFF
  83. //
  84. // APIC End Of Interrupt Offset.
  85. //
  86. #define APIC_EOI_OFFSET 0xB
  87. //
  88. // Define the system call number for resuming after a signal.
  89. //
  90. #define SystemCallRestoreContext 1
  91. #define SIGNAL_PARAMETERS_SIZE 24
  92. #define SIGNAL_CONTEXT_SIZE 32
  93. //
  94. // -------------------------------------------------------------------- Macros
  95. //
  96. //
  97. // This macro switches the DS and ES data segments to the kernel's data
  98. // segments.
  99. //
  100. .macro LoadKernelDataSegments
  101. movw $KERNEL_DS, %ax # Get the data segment selector,
  102. mov %ax, %ds # and save it into the data segments.
  103. mov %ax, %es #
  104. mov $GDT_PROCESSOR, %ax # Get the processor segment.
  105. mov %ax, %fs # Load it.
  106. .endm
  107. #if defined(__WINNT__) || defined(__CYGWIN__)
  108. #define FUNCTION(_Name) \
  109. _Name: \
  110. .def _##_Name; .scl 2; .type 32; .endef ; \
  111. .global _##_Name ; \
  112. _##_Name:
  113. #define PROTECTED_FUNCTION(_Name) FUNCTION(_Name)
  114. #define EXPORTED_FUNCTION(_Name) FUNCTION(_Name)
  115. #define END_FUNCTION(_Name)
  116. #elif defined(__ELF__)
  117. //
  118. // This macro defines a function, callable from C code in any module and
  119. // capable of being overridden by other functions.
  120. //
  121. #define EXPORTED_FUNCTION(_Name) \
  122. .func _Name ; \
  123. .type _Name, %function ; \
  124. .cfi_startproc ; \
  125. .cfi_def_cfa %esp, 4 ; \
  126. .cfi_offset %eip, -4 ; \
  127. .global _Name ; \
  128. _Name:
  129. //
  130. // This macro defines a function, callable from C code in the current module
  131. // only.
  132. //
  133. #define FUNCTION(_Name) \
  134. .hidden _Name ; \
  135. EXPORTED_FUNCTION(_Name)
  136. //
  137. // This macro defines a function, callable from C code in any module but always
  138. // called locally in the current module.
  139. //
  140. #define PROTECTED_FUNCTION(_Name) \
  141. .protected _Name ; \
  142. EXPORTED_FUNCTION(_Name)
  143. #define END_FUNCTION(_Name) \
  144. .size _Name, .-_Name ; \
  145. .endfunc ; \
  146. .cfi_endproc
  147. #else
  148. #define FUNCTION(_Name) \
  149. .global _Name ; \
  150. _Name:
  151. #define PROTECTED_FUNCTION(_Name) FUNCTION(_Name)
  152. #define EXPORTED_FUNCTION(_Name) FUNCTION(_Name)
  153. #define END_FUNCTION(_Name)
  154. #endif
  155. //
  156. // This macro sets the call frame information so that the debugger can unwind
  157. // a trap frame. It assumes the CFA register is esp, and sets the CFA to the
  158. // base of the trap frame just to make things easier.
  159. //
  160. #define CFI_TRAP_FRAME_PUSHED \
  161. .cfi_def_cfa_offset 0 ; \
  162. .cfi_offset %eax, TRAP_EAX ; \
  163. .cfi_offset %ebx, TRAP_EBX ; \
  164. .cfi_offset %ecx, TRAP_ECX ; \
  165. .cfi_offset %edx, TRAP_EDX ; \
  166. .cfi_offset %esi, TRAP_ESI ; \
  167. .cfi_offset %edi, TRAP_EDI ; \
  168. .cfi_offset %ebp, TRAP_EBP ; \
  169. .cfi_offset %eip, TRAP_EIP ; \
  170. .cfi_offset %esp, TRAP_ESP ; \
  171. .cfi_offset %eflags, TRAP_EFLAGS
  172. //
  173. // This macro sets the call frame information just after a trap frame was
  174. // restored. It indicates to the debugger that most registers are now in their
  175. // proper place. It assumes the CFA register is esp+0.
  176. //
  177. #define CFI_TRAP_FRAME_POPPED \
  178. .cfi_same_value %eax ; \
  179. .cfi_same_value %ebx ; \
  180. .cfi_same_value %ecx ; \
  181. .cfi_same_value %edx ; \
  182. .cfi_same_value %esi ; \
  183. .cfi_same_value %edi ; \
  184. .cfi_same_value %ebp ; \
  185. .cfi_offset %eip, 4
  186. //
  187. // Define .cfi directives, macroed so they can be excised if unneeded.
  188. //
  189. #define CFI_DEF_CFA(_Register, _Offset) .cfi_def_cfa _Register, _Offset
  190. #define CFI_DEF_CFA_OFFSET(_Offset) .cfi_def_cfa_offset _Offset
  191. #define CFI_ADJUST_CFA_OFFSET(_Amount) .cfi_adjust_cfa_offset _Amount
  192. #define CFI_OFFSET(_Register, _Offset) .cfi_offset _Register, _Offset
  193. #define CFI_UNDEFINED(_Register) .cfi_undefined _Register
  194. #define CFI_SAME_VALUE(_Register) .cfi_same_value _Register