x64.inc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*++
  2. Copyright (c) 2015 Minoca Corp. All Rights Reserved
  3. Module Name:
  4. x64.inc
  5. Abstract:
  6. This module contains common definitions for the 64-bit x86 architecture.
  7. Author:
  8. Evan Green 17-Jan-2015
  9. Environment:
  10. Any
  11. --*/
  12. //
  13. // --------------------------------------------------------------- Definitions
  14. //
  15. //
  16. // Basic constants.
  17. //
  18. #define FALSE 0
  19. #define TRUE 1
  20. #define NULL 0
  21. //
  22. // Kernel constants.
  23. //
  24. #define KERNEL_CS 0x08
  25. #define KERNEL_DS 0x10
  26. #define USER_CS (0x18 | 3)
  27. #define USER_DS (0x20 | 3)
  28. #define GDT_PROCESSOR 0x28
  29. #define GDT_THREAD (0x30 | 3)
  30. #define KERNEL_TSS 0x38
  31. #define NMI_TSS 0x48
  32. #define GDT_ENTRIES 10
  33. #define EXCEPTION_NMI 0x02
  34. #define EXCEPTION_BREAK 0x03
  35. #define EXCEPTION_SINGLE_STEP 0x04
  36. #define EXCEPTION_ACCESS_VIOLATION 0x05
  37. #define EXCEPTION_ASSERTION_FAILURE 0x07
  38. #define EXCEPTION_DOUBLE_FAULT 0x0C
  39. #define CONTEXT_SWAP_MAGIC 0x9A8A7A6A5A4A3A2A
  40. //
  41. // Definition for the TRAP_FRAME structure and the exception stack directly
  42. // above it.
  43. //
  44. #define TRAP_DS 0
  45. #define TRAP_ES 4
  46. #define TRAP_FS 8
  47. #define TRAP_GS 12
  48. #define TRAP_SS 16
  49. #define TRAP_RAX 24
  50. #define TRAP_RBX 32
  51. #define TRAP_RCX 40
  52. #define TRAP_RDX 48
  53. #define TRAP_RSI 56
  54. #define TRAP_RDI 64
  55. #define TRAP_RBP 72
  56. #define TRAP_R8 80
  57. #define TRAP_R9 88
  58. #define TRAP_R10 96
  59. #define TRAP_R11 104
  60. #define TRAP_R12 112
  61. #define TRAP_R13 120
  62. #define TRAP_R14 128
  63. #define TRAP_R15 136
  64. #define TRAP_ERRORCODE 144
  65. #define TRAP_RIP 152
  66. #define TRAP_CS 160
  67. #define TRAP_EFLAGS 168
  68. #define TRAP_RSP 176
  69. #define TRAP_FRAME_SIZE 184
  70. #define PROCESSOR_CONTEXT_SIZE 0x60
  71. #define SIGNAL_CONTEXT_SIZE 28
  72. //
  73. // Define the minimum and maximum external interrupt vectors.
  74. //
  75. #define MINIMUM_VECTOR 0x30
  76. #define MAXIMUM_VECTOR 0xFF
  77. //
  78. // APIC End Of Interrupt Offset.
  79. //
  80. #define APIC_EOI_OFFSET 0x0B
  81. //
  82. // -------------------------------------------------------------------- Macros
  83. //
  84. //
  85. // This macro goes at the start of every assembly file.
  86. //
  87. #define ASSEMBLY_FILE_HEADER \
  88. .text
  89. //
  90. // This macro loads DS, ES, and GS with the correct values for the kernel.
  91. //
  92. #define LOAD_KERNEL_DATA_SEGMENTS \
  93. movw $KERNEL_DS, %ax ; \
  94. mov %ax, %ds ; \
  95. mov %ax, %es ; \
  96. mov $GDT_PROCESSOR, %ax ; \
  97. mov %ax, %fs ; \
  98. #if defined(__ELF__)
  99. ##
  100. ## This macro defines a function, callable from C code in any module and
  101. ## capable of being overridden by other functions.
  102. ##
  103. #define EXPORTED_FUNCTION(_Name) \
  104. .func _Name ; \
  105. .type _Name, %function ; \
  106. .cfi_startproc ; \
  107. .global _Name ; \
  108. _Name:
  109. ##
  110. ## This macro defines a function, callable from C code in the current module
  111. ## only.
  112. ##
  113. #define FUNCTION(_Name) \
  114. .hidden _Name ; \
  115. EXPORTED_FUNCTION(_Name)
  116. ##
  117. ## This macro defines a function, callable from C code in any module but always
  118. ## called locally in the current module.
  119. ##
  120. #define PROTECTED_FUNCTION(_Name) \
  121. .protected _Name ; \
  122. EXPORTED_FUNCTION(_Name)
  123. #define END_FUNCTION(_Name) \
  124. .size _Name, .-_Name ; \
  125. .endfunc ; \
  126. .cfi_endproc
  127. #else
  128. #define FUNCTION(_Name) \
  129. .def _Name; .scl 2; .type 32; .endef ; \
  130. .global _Name ; \
  131. _Name:
  132. #define PROTECTED_FUNCTION(_Name) FUNCTION(_Name)
  133. #define EXPORTED_FUNCTION(_Name) FUNCTION(_Name)
  134. #define END_FUNCTION(_Name)
  135. #endif