segment.h 5.4 KB

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