segment.h 5.7 KB

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