x86.inc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*++
  2. Copyright (c) 2012 Minoca Corp. All Rights Reserved
  3. Module Name:
  4. x86.inc
  5. Abstract:
  6. This module contains common definitions for the x86 architecture.
  7. Author:
  8. Evan Green 18-Jul-2012
  9. Environment:
  10. Kernel mode
  11. --*/
  12. ##
  13. ## --------------------------------------------------------------- Definitions
  14. ##
  15. ##
  16. ## Processor flags
  17. ##
  18. #define IA32_EFLAG_TF 0x00000100
  19. #define IA32_EFLAG_IF 0x00000200
  20. #define IA32_EFLAG_IOPL_MASK 0x00003000
  21. #define IA32_EFLAG_VM 0x00020000
  22. #define IA32_EFLAG_VIF 0x00080000
  23. #define SEGMENT_PRIVILEGE_MASK 0x0003
  24. ##
  25. ## CR0 flags
  26. ##
  27. #define CR0_PAGING_ENABLE 0x80000000
  28. #define CR0_CACHE_DISABLE 0x40000000
  29. #define CR0_NOT_WRITE_THROUGH 0x20000000
  30. #define CR0_WRITE_PROTECT_ENABLE 0x00010000
  31. #define CR0_X87_EXCEPTIONS 0x00000020
  32. #define CR0_TASK_SWITCHED 0x00000008
  33. #define CR0_EMULATE_FPU 0x00000004
  34. #define CR0_MONITOR_COPROCESSOR 0x00000002
  35. #define CR0_PROTECTED_MODE 0x00000001
  36. #define CR0_OR_MASK 0x8001002B
  37. #define CR0_AND_MASK ~(0x60000004)
  38. ##
  39. ## Basic constants.
  40. ##
  41. #define FALSE 0
  42. #define TRUE 1
  43. #define NULL 0
  44. ##
  45. ## Kernel constants.
  46. ##
  47. #define KERNEL_CS 0x08
  48. #define KERNEL_DS 0x10
  49. #define USER_CS (0x18 | 3)
  50. #define USER_DS (0x20 | 3)
  51. #define GDT_PROCESSOR 0x28
  52. #define GDT_THREAD (0x30 | 3)
  53. #define KERNEL_TSS 0x38
  54. #define DOUBLE_FAULT_TSS 0x40
  55. #define NMI_TSS 0x48
  56. #define GDT_ENTRIES 10
  57. #define EXCEPTION_NMI 0x02
  58. #define EXCEPTION_BREAK 0x03
  59. #define EXCEPTION_SINGLE_STEP 0x04
  60. #define EXCEPTION_ACCESS_VIOLATION 0x05
  61. #define EXCEPTION_ASSERTION_FAILURE 0x07
  62. #define EXCEPTION_DOUBLE_FAULT 0x0C
  63. #define CONTEXT_SWAP_MAGIC 0x5A4A3A2A
  64. ##
  65. ## Relevant TSS structure definitions.
  66. ##
  67. #define TSS_ESP0 0x04
  68. ##
  69. ## Processor block offsets.
  70. ##
  71. #define PROCESSOR_BLOCK_TSS 0x0C
  72. #define PROCESSOR_BLOCK_GDT 0x10
  73. ##
  74. ## Definition for the TRAP_FRAME structure and the exception stack directly
  75. ## above it.
  76. ##
  77. #define TRAP_DS 0
  78. #define TRAP_ES 4
  79. #define TRAP_FS 8
  80. #define TRAP_GS 12
  81. #define TRAP_SS 16
  82. #define TRAP_EAX 20
  83. #define TRAP_EBX 24
  84. #define TRAP_ECX 28
  85. #define TRAP_EDX 32
  86. #define TRAP_ESI 36
  87. #define TRAP_EDI 40
  88. #define TRAP_EBP 44
  89. #define TRAP_ERRORCODE 48
  90. #define TRAP_EIP 52
  91. #define TRAP_CS 56
  92. #define TRAP_EFLAGS 60
  93. #define TRAP_ESP 64
  94. #define TRAP_RET_ERRORCODE 68
  95. #define TRAP_RET_EIP 72
  96. #define TRAP_RET_CS 76
  97. #define TRAP_RET_EFLAGS 80
  98. #define TRAP_RET_ESP 84
  99. #define TRAP_RET_SS 88
  100. #define TRAP_RET_ES 92
  101. #define TRAP_RET_DS 96
  102. #define TRAP_RET_FS 100
  103. #define TRAP_RET_GS 104
  104. #define TRAP_FRAME_SIZE 68
  105. #define PROCESSOR_CONTEXT_SIZE 0x60
  106. #define SIGNAL_CONTEXT_SIZE 32
  107. ##
  108. ## Define the minimum and maximum external interrupt vectors.
  109. ##
  110. #define MINIMUM_VECTOR 0x30
  111. #define MAXIMUM_VECTOR 0xFF
  112. ##
  113. ## APIC End Of Interrupt Offset.
  114. ##
  115. #define APIC_EOI_OFFSET 0xB
  116. ##
  117. ## Define the system call number for resuming after a signal.
  118. ##
  119. #define SystemCallRestoreContext 1
  120. #define SIGNAL_PARAMETERS_SIZE 24
  121. ##
  122. ## -------------------------------------------------------------------- Macros
  123. ##
  124. ##
  125. ## This macro switches the DS and ES data segments to the kernel's data
  126. ## segments.
  127. ##
  128. .macro LoadKernelDataSegments
  129. movw $KERNEL_DS, %ax # Get the data segment selector,
  130. mov %ax, %ds # and save it into the data segments.
  131. mov %ax, %es #
  132. mov $GDT_PROCESSOR, %ax # Get the processor segment.
  133. mov %ax, %fs # Load it.
  134. .endm
  135. #if defined(__ELF__)
  136. ##
  137. ## This macro defines a function, callable from C code in any module and
  138. ## capable of being overridden by other functions.
  139. ##
  140. #define EXPORTED_FUNCTION(_Name) \
  141. .func _Name ; \
  142. .type _Name, %function ; \
  143. .cfi_startproc ; \
  144. .global _Name ; \
  145. _Name:
  146. ##
  147. ## This macro defines a function, callable from C code in the current module
  148. ## only.
  149. ##
  150. #define FUNCTION(_Name) \
  151. .hidden _Name ; \
  152. EXPORTED_FUNCTION(_Name)
  153. ##
  154. ## This macro defines a function, callable from C code in any module but always
  155. ## called locally in the current module.
  156. ##
  157. #define PROTECTED_FUNCTION(_Name) \
  158. .protected _Name ; \
  159. EXPORTED_FUNCTION(_Name)
  160. #define END_FUNCTION(_Name) \
  161. .size _Name, .-_Name ; \
  162. .endfunc ; \
  163. .cfi_endproc
  164. #else
  165. #define FUNCTION(_Name) \
  166. _Name: \
  167. .def _##_Name; .scl 2; .type 32; .endef ; \
  168. .global _##_Name ; \
  169. _##_Name:
  170. #define PROTECTED_FUNCTION(_Name) FUNCTION(_Name)
  171. #define EXPORTED_FUNCTION(_Name) FUNCTION(_Name)
  172. #define END_FUNCTION(_Name)
  173. #endif