segment.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. #ifndef _ASM_X86_SEGMENT_H
  2. #define _ASM_X86_SEGMENT_H
  3. #include <linux/const.h>
  4. /*
  5. * Constructor for a conventional segment GDT (or LDT) entry.
  6. * This is a macro so it can be used in initializers.
  7. */
  8. #define GDT_ENTRY(flags, base, limit) \
  9. ((((base) & _AC(0xff000000,ULL)) << (56-24)) | \
  10. (((flags) & _AC(0x0000f0ff,ULL)) << 40) | \
  11. (((limit) & _AC(0x000f0000,ULL)) << (48-16)) | \
  12. (((base) & _AC(0x00ffffff,ULL)) << 16) | \
  13. (((limit) & _AC(0x0000ffff,ULL))))
  14. /* Simple and small GDT entries for booting only: */
  15. #define GDT_ENTRY_BOOT_CS 2
  16. #define GDT_ENTRY_BOOT_DS 3
  17. #define GDT_ENTRY_BOOT_TSS 4
  18. #define __BOOT_CS (GDT_ENTRY_BOOT_CS*8)
  19. #define __BOOT_DS (GDT_ENTRY_BOOT_DS*8)
  20. #define __BOOT_TSS (GDT_ENTRY_BOOT_TSS*8)
  21. /*
  22. * Bottom two bits of selector give the ring
  23. * privilege level
  24. */
  25. #define SEGMENT_RPL_MASK 0x3
  26. /* User mode is privilege level 3: */
  27. #define USER_RPL 0x3
  28. /* Bit 2 is Table Indicator (TI): selects between LDT or GDT */
  29. #define SEGMENT_TI_MASK 0x4
  30. /* LDT segment has TI set ... */
  31. #define SEGMENT_LDT 0x4
  32. /* ... GDT has it cleared */
  33. #define SEGMENT_GDT 0x0
  34. #define GDT_ENTRY_INVALID_SEG 0
  35. #ifdef CONFIG_X86_32
  36. /*
  37. * The layout of the per-CPU GDT under Linux:
  38. *
  39. * 0 - null <=== cacheline #1
  40. * 1 - reserved
  41. * 2 - reserved
  42. * 3 - reserved
  43. *
  44. * 4 - unused <=== cacheline #2
  45. * 5 - unused
  46. *
  47. * ------- start of TLS (Thread-Local Storage) segments:
  48. *
  49. * 6 - TLS segment #1 [ glibc's TLS segment ]
  50. * 7 - TLS segment #2 [ Wine's %fs Win32 segment ]
  51. * 8 - TLS segment #3 <=== cacheline #3
  52. * 9 - reserved
  53. * 10 - reserved
  54. * 11 - reserved
  55. *
  56. * ------- start of kernel segments:
  57. *
  58. * 12 - kernel code segment <=== cacheline #4
  59. * 13 - kernel data segment
  60. * 14 - default user CS
  61. * 15 - default user DS
  62. * 16 - TSS <=== cacheline #5
  63. * 17 - LDT
  64. * 18 - PNPBIOS support (16->32 gate)
  65. * 19 - PNPBIOS support
  66. * 20 - PNPBIOS support <=== cacheline #6
  67. * 21 - PNPBIOS support
  68. * 22 - PNPBIOS support
  69. * 23 - APM BIOS support
  70. * 24 - APM BIOS support <=== cacheline #7
  71. * 25 - APM BIOS support
  72. *
  73. * 26 - ESPFIX small SS
  74. * 27 - per-cpu [ offset to per-cpu data area ]
  75. * 28 - stack_canary-20 [ for stack protector ] <=== cacheline #8
  76. * 29 - unused
  77. * 30 - unused
  78. * 31 - TSS for double fault handler
  79. */
  80. #define GDT_ENTRY_TLS_MIN 6
  81. #define GDT_ENTRY_TLS_MAX (GDT_ENTRY_TLS_MIN + GDT_ENTRY_TLS_ENTRIES - 1)
  82. #define GDT_ENTRY_KERNEL_CS 12
  83. #define GDT_ENTRY_KERNEL_DS 13
  84. #define GDT_ENTRY_DEFAULT_USER_CS 14
  85. #define GDT_ENTRY_DEFAULT_USER_DS 15
  86. #define GDT_ENTRY_TSS 16
  87. #define GDT_ENTRY_LDT 17
  88. #define GDT_ENTRY_PNPBIOS_CS32 18
  89. #define GDT_ENTRY_PNPBIOS_CS16 19
  90. #define GDT_ENTRY_PNPBIOS_DS 20
  91. #define GDT_ENTRY_PNPBIOS_TS1 21
  92. #define GDT_ENTRY_PNPBIOS_TS2 22
  93. #define GDT_ENTRY_APMBIOS_BASE 23
  94. #define GDT_ENTRY_ESPFIX_SS 26
  95. #define GDT_ENTRY_PERCPU 27
  96. #define GDT_ENTRY_STACK_CANARY 28
  97. #define GDT_ENTRY_DOUBLEFAULT_TSS 31
  98. /*
  99. * Number of entries in the GDT table:
  100. */
  101. #define GDT_ENTRIES 32
  102. /*
  103. * Segment selector values corresponding to the above entries:
  104. */
  105. #define __KERNEL_CS (GDT_ENTRY_KERNEL_CS*8)
  106. #define __KERNEL_DS (GDT_ENTRY_KERNEL_DS*8)
  107. #define __USER_DS (GDT_ENTRY_DEFAULT_USER_DS*8 + 3)
  108. #define __USER_CS (GDT_ENTRY_DEFAULT_USER_CS*8 + 3)
  109. #define __ESPFIX_SS (GDT_ENTRY_ESPFIX_SS*8)
  110. /* segment for calling fn: */
  111. #define PNP_CS32 (GDT_ENTRY_PNPBIOS_CS32*8)
  112. /* code segment for BIOS: */
  113. #define PNP_CS16 (GDT_ENTRY_PNPBIOS_CS16*8)
  114. /* "Is this PNP code selector (PNP_CS32 or PNP_CS16)?" */
  115. #define SEGMENT_IS_PNP_CODE(x) (((x) & 0xf4) == PNP_CS32)
  116. /* data segment for BIOS: */
  117. #define PNP_DS (GDT_ENTRY_PNPBIOS_DS*8)
  118. /* transfer data segment: */
  119. #define PNP_TS1 (GDT_ENTRY_PNPBIOS_TS1*8)
  120. /* another data segment: */
  121. #define PNP_TS2 (GDT_ENTRY_PNPBIOS_TS2*8)
  122. #ifdef CONFIG_SMP
  123. # define __KERNEL_PERCPU (GDT_ENTRY_PERCPU*8)
  124. #else
  125. # define __KERNEL_PERCPU 0
  126. #endif
  127. #ifdef CONFIG_CC_STACKPROTECTOR
  128. # define __KERNEL_STACK_CANARY (GDT_ENTRY_STACK_CANARY*8)
  129. #else
  130. # define __KERNEL_STACK_CANARY 0
  131. #endif
  132. #else /* 64-bit: */
  133. #include <asm/cache.h>
  134. #define GDT_ENTRY_KERNEL32_CS 1
  135. #define GDT_ENTRY_KERNEL_CS 2
  136. #define GDT_ENTRY_KERNEL_DS 3
  137. /*
  138. * We cannot use the same code segment descriptor for user and kernel mode,
  139. * not even in long flat mode, because of different DPL.
  140. *
  141. * GDT layout to get 64-bit SYSCALL/SYSRET support right. SYSRET hardcodes
  142. * selectors:
  143. *
  144. * if returning to 32-bit userspace: cs = STAR.SYSRET_CS,
  145. * if returning to 64-bit userspace: cs = STAR.SYSRET_CS+16,
  146. *
  147. * ss = STAR.SYSRET_CS+8 (in either case)
  148. *
  149. * thus USER_DS should be between 32-bit and 64-bit code selectors:
  150. */
  151. #define GDT_ENTRY_DEFAULT_USER32_CS 4
  152. #define GDT_ENTRY_DEFAULT_USER_DS 5
  153. #define GDT_ENTRY_DEFAULT_USER_CS 6
  154. /* Needs two entries */
  155. #define GDT_ENTRY_TSS 8
  156. /* Needs two entries */
  157. #define GDT_ENTRY_LDT 10
  158. #define GDT_ENTRY_TLS_MIN 12
  159. #define GDT_ENTRY_TLS_MAX 14
  160. /* Abused to load per CPU data from limit */
  161. #define GDT_ENTRY_PER_CPU 15
  162. /*
  163. * Number of entries in the GDT table:
  164. */
  165. #define GDT_ENTRIES 16
  166. /*
  167. * Segment selector values corresponding to the above entries:
  168. *
  169. * Note, selectors also need to have a correct RPL,
  170. * expressed with the +3 value for user-space selectors:
  171. */
  172. #define __KERNEL32_CS (GDT_ENTRY_KERNEL32_CS*8)
  173. #define __KERNEL_CS (GDT_ENTRY_KERNEL_CS*8)
  174. #define __KERNEL_DS (GDT_ENTRY_KERNEL_DS*8)
  175. #define __USER32_CS (GDT_ENTRY_DEFAULT_USER32_CS*8 + 3)
  176. #define __USER_DS (GDT_ENTRY_DEFAULT_USER_DS*8 + 3)
  177. #define __USER32_DS __USER_DS
  178. #define __USER_CS (GDT_ENTRY_DEFAULT_USER_CS*8 + 3)
  179. #define __PER_CPU_SEG (GDT_ENTRY_PER_CPU*8 + 3)
  180. /* TLS indexes for 64-bit - hardcoded in arch_prctl(): */
  181. #define FS_TLS 0
  182. #define GS_TLS 1
  183. #define GS_TLS_SEL ((GDT_ENTRY_TLS_MIN+GS_TLS)*8 + 3)
  184. #define FS_TLS_SEL ((GDT_ENTRY_TLS_MIN+FS_TLS)*8 + 3)
  185. #endif
  186. #ifndef CONFIG_PARAVIRT
  187. # define get_kernel_rpl() 0
  188. #endif
  189. #define IDT_ENTRIES 256
  190. #define NUM_EXCEPTION_VECTORS 32
  191. /* Bitmask of exception vectors which push an error code on the stack: */
  192. #define EXCEPTION_ERRCODE_MASK 0x00027d00
  193. #define GDT_SIZE (GDT_ENTRIES*8)
  194. #define GDT_ENTRY_TLS_ENTRIES 3
  195. #define TLS_SIZE (GDT_ENTRY_TLS_ENTRIES* 8)
  196. #ifdef __KERNEL__
  197. /*
  198. * early_idt_handler_array is an array of entry points referenced in the
  199. * early IDT. For simplicity, it's a real array with one entry point
  200. * every nine bytes. That leaves room for an optional 'push $0' if the
  201. * vector has no error code (two bytes), a 'push $vector_number' (two
  202. * bytes), and a jump to the common entry code (up to five bytes).
  203. */
  204. #define EARLY_IDT_HANDLER_SIZE 9
  205. #ifndef __ASSEMBLY__
  206. extern const char early_idt_handler_array[NUM_EXCEPTION_VECTORS][EARLY_IDT_HANDLER_SIZE];
  207. #ifdef CONFIG_TRACING
  208. # define trace_early_idt_handler_array early_idt_handler_array
  209. #endif
  210. /*
  211. * Load a segment. Fall back on loading the zero
  212. * segment if something goes wrong..
  213. */
  214. #define loadsegment(seg, value) \
  215. do { \
  216. unsigned short __val = (value); \
  217. \
  218. asm volatile(" \n" \
  219. "1: movl %k0,%%" #seg " \n" \
  220. \
  221. ".section .fixup,\"ax\" \n" \
  222. "2: xorl %k0,%k0 \n" \
  223. " jmp 1b \n" \
  224. ".previous \n" \
  225. \
  226. _ASM_EXTABLE(1b, 2b) \
  227. \
  228. : "+r" (__val) : : "memory"); \
  229. } while (0)
  230. /*
  231. * Save a segment register away:
  232. */
  233. #define savesegment(seg, value) \
  234. asm("mov %%" #seg ",%0":"=r" (value) : : "memory")
  235. /*
  236. * x86-32 user GS accessors:
  237. */
  238. #ifdef CONFIG_X86_32
  239. # ifdef CONFIG_X86_32_LAZY_GS
  240. # define get_user_gs(regs) (u16)({ unsigned long v; savesegment(gs, v); v; })
  241. # define set_user_gs(regs, v) loadsegment(gs, (unsigned long)(v))
  242. # define task_user_gs(tsk) ((tsk)->thread.gs)
  243. # define lazy_save_gs(v) savesegment(gs, (v))
  244. # define lazy_load_gs(v) loadsegment(gs, (v))
  245. # else /* X86_32_LAZY_GS */
  246. # define get_user_gs(regs) (u16)((regs)->gs)
  247. # define set_user_gs(regs, v) do { (regs)->gs = (v); } while (0)
  248. # define task_user_gs(tsk) (task_pt_regs(tsk)->gs)
  249. # define lazy_save_gs(v) do { } while (0)
  250. # define lazy_load_gs(v) do { } while (0)
  251. # endif /* X86_32_LAZY_GS */
  252. #endif /* X86_32 */
  253. #endif /* !__ASSEMBLY__ */
  254. #endif /* __KERNEL__ */
  255. #endif /* _ASM_X86_SEGMENT_H */