segment.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #ifndef ASM_X86__SEGMENT_H
  2. #define ASM_X86__SEGMENT_H
  3. /* Constructor for a conventional segment GDT (or LDT) entry */
  4. /* This is a macro so it can be used in initializers */
  5. #define GDT_ENTRY(flags, base, limit) \
  6. ((((base) & 0xff000000ULL) << (56-24)) | \
  7. (((flags) & 0x0000f0ffULL) << 40) | \
  8. (((limit) & 0x000f0000ULL) << (48-16)) | \
  9. (((base) & 0x00ffffffULL) << 16) | \
  10. (((limit) & 0x0000ffffULL)))
  11. /* Simple and small GDT entries for booting only */
  12. #define GDT_ENTRY_BOOT_CS 2
  13. #define __BOOT_CS (GDT_ENTRY_BOOT_CS * 8)
  14. #define GDT_ENTRY_BOOT_DS (GDT_ENTRY_BOOT_CS + 1)
  15. #define __BOOT_DS (GDT_ENTRY_BOOT_DS * 8)
  16. #define GDT_ENTRY_BOOT_TSS (GDT_ENTRY_BOOT_CS + 2)
  17. #define __BOOT_TSS (GDT_ENTRY_BOOT_TSS * 8)
  18. #ifdef CONFIG_X86_32
  19. /*
  20. * The layout of the per-CPU GDT under Linux:
  21. *
  22. * 0 - null
  23. * 1 - reserved
  24. * 2 - reserved
  25. * 3 - reserved
  26. *
  27. * 4 - unused <==== new cacheline
  28. * 5 - unused
  29. *
  30. * ------- start of TLS (Thread-Local Storage) segments:
  31. *
  32. * 6 - TLS segment #1 [ glibc's TLS segment ]
  33. * 7 - TLS segment #2 [ Wine's %fs Win32 segment ]
  34. * 8 - TLS segment #3
  35. * 9 - reserved
  36. * 10 - reserved
  37. * 11 - reserved
  38. *
  39. * ------- start of kernel segments:
  40. *
  41. * 12 - kernel code segment <==== new cacheline
  42. * 13 - kernel data segment
  43. * 14 - default user CS
  44. * 15 - default user DS
  45. * 16 - TSS
  46. * 17 - LDT
  47. * 18 - PNPBIOS support (16->32 gate)
  48. * 19 - PNPBIOS support
  49. * 20 - PNPBIOS support
  50. * 21 - PNPBIOS support
  51. * 22 - PNPBIOS support
  52. * 23 - APM BIOS support
  53. * 24 - APM BIOS support
  54. * 25 - APM BIOS support
  55. *
  56. * 26 - ESPFIX small SS
  57. * 27 - per-cpu [ offset to per-cpu data area ]
  58. * 28 - unused
  59. * 29 - unused
  60. * 30 - unused
  61. * 31 - TSS for double fault handler
  62. */
  63. #define GDT_ENTRY_TLS_MIN 6
  64. #define GDT_ENTRY_TLS_MAX (GDT_ENTRY_TLS_MIN + GDT_ENTRY_TLS_ENTRIES - 1)
  65. #define GDT_ENTRY_DEFAULT_USER_CS 14
  66. #define GDT_ENTRY_DEFAULT_USER_DS 15
  67. #define GDT_ENTRY_KERNEL_BASE 12
  68. #define GDT_ENTRY_KERNEL_CS (GDT_ENTRY_KERNEL_BASE + 0)
  69. #define GDT_ENTRY_KERNEL_DS (GDT_ENTRY_KERNEL_BASE + 1)
  70. #define GDT_ENTRY_TSS (GDT_ENTRY_KERNEL_BASE + 4)
  71. #define GDT_ENTRY_LDT (GDT_ENTRY_KERNEL_BASE + 5)
  72. #define GDT_ENTRY_PNPBIOS_BASE (GDT_ENTRY_KERNEL_BASE + 6)
  73. #define GDT_ENTRY_APMBIOS_BASE (GDT_ENTRY_KERNEL_BASE + 11)
  74. #define GDT_ENTRY_ESPFIX_SS (GDT_ENTRY_KERNEL_BASE + 14)
  75. #define __ESPFIX_SS (GDT_ENTRY_ESPFIX_SS * 8)
  76. #define GDT_ENTRY_PERCPU (GDT_ENTRY_KERNEL_BASE + 15)
  77. #ifdef CONFIG_SMP
  78. #define __KERNEL_PERCPU (GDT_ENTRY_PERCPU * 8)
  79. #else
  80. #define __KERNEL_PERCPU 0
  81. #endif
  82. #define GDT_ENTRY_DOUBLEFAULT_TSS 31
  83. /*
  84. * The GDT has 32 entries
  85. */
  86. #define GDT_ENTRIES 32
  87. /* The PnP BIOS entries in the GDT */
  88. #define GDT_ENTRY_PNPBIOS_CS32 (GDT_ENTRY_PNPBIOS_BASE + 0)
  89. #define GDT_ENTRY_PNPBIOS_CS16 (GDT_ENTRY_PNPBIOS_BASE + 1)
  90. #define GDT_ENTRY_PNPBIOS_DS (GDT_ENTRY_PNPBIOS_BASE + 2)
  91. #define GDT_ENTRY_PNPBIOS_TS1 (GDT_ENTRY_PNPBIOS_BASE + 3)
  92. #define GDT_ENTRY_PNPBIOS_TS2 (GDT_ENTRY_PNPBIOS_BASE + 4)
  93. /* The PnP BIOS selectors */
  94. #define PNP_CS32 (GDT_ENTRY_PNPBIOS_CS32 * 8) /* segment for calling fn */
  95. #define PNP_CS16 (GDT_ENTRY_PNPBIOS_CS16 * 8) /* code segment for BIOS */
  96. #define PNP_DS (GDT_ENTRY_PNPBIOS_DS * 8) /* data segment for BIOS */
  97. #define PNP_TS1 (GDT_ENTRY_PNPBIOS_TS1 * 8) /* transfer data segment */
  98. #define PNP_TS2 (GDT_ENTRY_PNPBIOS_TS2 * 8) /* another data segment */
  99. /* Bottom two bits of selector give the ring privilege level */
  100. #define SEGMENT_RPL_MASK 0x3
  101. /* Bit 2 is table indicator (LDT/GDT) */
  102. #define SEGMENT_TI_MASK 0x4
  103. /* User mode is privilege level 3 */
  104. #define USER_RPL 0x3
  105. /* LDT segment has TI set, GDT has it cleared */
  106. #define SEGMENT_LDT 0x4
  107. #define SEGMENT_GDT 0x0
  108. /*
  109. * Matching rules for certain types of segments.
  110. */
  111. /* Matches PNP_CS32 and PNP_CS16 (they must be consecutive) */
  112. #define SEGMENT_IS_PNP_CODE(x) (((x) & 0xf4) == GDT_ENTRY_PNPBIOS_BASE * 8)
  113. #else
  114. #include <asm/cache.h>
  115. #define GDT_ENTRY_KERNEL32_CS 1
  116. #define GDT_ENTRY_KERNEL_CS 2
  117. #define GDT_ENTRY_KERNEL_DS 3
  118. #define __KERNEL32_CS (GDT_ENTRY_KERNEL32_CS * 8)
  119. /*
  120. * we cannot use the same code segment descriptor for user and kernel
  121. * -- not even in the long flat mode, because of different DPL /kkeil
  122. * The segment offset needs to contain a RPL. Grr. -AK
  123. * GDT layout to get 64bit syscall right (sysret hardcodes gdt offsets)
  124. */
  125. #define GDT_ENTRY_DEFAULT_USER32_CS 4
  126. #define GDT_ENTRY_DEFAULT_USER_DS 5
  127. #define GDT_ENTRY_DEFAULT_USER_CS 6
  128. #define __USER32_CS (GDT_ENTRY_DEFAULT_USER32_CS * 8 + 3)
  129. #define __USER32_DS __USER_DS
  130. #define GDT_ENTRY_TSS 8 /* needs two entries */
  131. #define GDT_ENTRY_LDT 10 /* needs two entries */
  132. #define GDT_ENTRY_TLS_MIN 12
  133. #define GDT_ENTRY_TLS_MAX 14
  134. #define GDT_ENTRY_PER_CPU 15 /* Abused to load per CPU data from limit */
  135. #define __PER_CPU_SEG (GDT_ENTRY_PER_CPU * 8 + 3)
  136. /* TLS indexes for 64bit - hardcoded in arch_prctl */
  137. #define FS_TLS 0
  138. #define GS_TLS 1
  139. #define GS_TLS_SEL ((GDT_ENTRY_TLS_MIN+GS_TLS)*8 + 3)
  140. #define FS_TLS_SEL ((GDT_ENTRY_TLS_MIN+FS_TLS)*8 + 3)
  141. #define GDT_ENTRIES 16
  142. #endif
  143. #define __KERNEL_CS (GDT_ENTRY_KERNEL_CS * 8)
  144. #define __KERNEL_DS (GDT_ENTRY_KERNEL_DS * 8)
  145. #define __USER_DS (GDT_ENTRY_DEFAULT_USER_DS* 8 + 3)
  146. #define __USER_CS (GDT_ENTRY_DEFAULT_USER_CS* 8 + 3)
  147. #ifndef CONFIG_PARAVIRT
  148. #define get_kernel_rpl() 0
  149. #endif
  150. /* User mode is privilege level 3 */
  151. #define USER_RPL 0x3
  152. /* LDT segment has TI set, GDT has it cleared */
  153. #define SEGMENT_LDT 0x4
  154. #define SEGMENT_GDT 0x0
  155. /* Bottom two bits of selector give the ring privilege level */
  156. #define SEGMENT_RPL_MASK 0x3
  157. /* Bit 2 is table indicator (LDT/GDT) */
  158. #define SEGMENT_TI_MASK 0x4
  159. #define IDT_ENTRIES 256
  160. #define NUM_EXCEPTION_VECTORS 32
  161. #define GDT_SIZE (GDT_ENTRIES * 8)
  162. #define GDT_ENTRY_TLS_ENTRIES 3
  163. #define TLS_SIZE (GDT_ENTRY_TLS_ENTRIES * 8)
  164. #ifdef __KERNEL__
  165. #ifndef __ASSEMBLY__
  166. extern const char early_idt_handlers[NUM_EXCEPTION_VECTORS][10];
  167. #endif
  168. #endif
  169. #endif /* ASM_X86__SEGMENT_H */