segment.h 9.4 KB

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