segment.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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_CPUNODE 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 __CPUNODE_SEG (GDT_ENTRY_CPUNODE*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 CONFIG_X86_64
  193. /* Bit size and mask of CPU number stored in the per CPU data (and TSC_AUX) */
  194. #define VDSO_CPUNODE_BITS 12
  195. #define VDSO_CPUNODE_MASK 0xfff
  196. #ifndef __ASSEMBLY__
  197. /* Helper functions to store/load CPU and node numbers */
  198. static inline unsigned long vdso_encode_cpunode(int cpu, unsigned long node)
  199. {
  200. return (node << VDSO_CPUNODE_BITS) | cpu;
  201. }
  202. static inline void vdso_read_cpunode(unsigned *cpu, unsigned *node)
  203. {
  204. unsigned int p;
  205. /*
  206. * Load CPU and node number from the GDT. LSL is faster than RDTSCP
  207. * and works on all CPUs. This is volatile so that it orders
  208. * correctly with respect to barrier() and to keep GCC from cleverly
  209. * hoisting it out of the calling function.
  210. *
  211. * If RDPID is available, use it.
  212. */
  213. alternative_io ("lsl %[seg],%[p]",
  214. ".byte 0xf3,0x0f,0xc7,0xf8", /* RDPID %eax/rax */
  215. X86_FEATURE_RDPID,
  216. [p] "=a" (p), [seg] "r" (__CPUNODE_SEG));
  217. if (cpu)
  218. *cpu = (p & VDSO_CPUNODE_MASK);
  219. if (node)
  220. *node = (p >> VDSO_CPUNODE_BITS);
  221. }
  222. #endif /* !__ASSEMBLY__ */
  223. #endif /* CONFIG_X86_64 */
  224. #ifdef __KERNEL__
  225. /*
  226. * early_idt_handler_array is an array of entry points referenced in the
  227. * early IDT. For simplicity, it's a real array with one entry point
  228. * every nine bytes. That leaves room for an optional 'push $0' if the
  229. * vector has no error code (two bytes), a 'push $vector_number' (two
  230. * bytes), and a jump to the common entry code (up to five bytes).
  231. */
  232. #define EARLY_IDT_HANDLER_SIZE 9
  233. /*
  234. * xen_early_idt_handler_array is for Xen pv guests: for each entry in
  235. * early_idt_handler_array it contains a prequel in the form of
  236. * pop %rcx; pop %r11; jmp early_idt_handler_array[i]; summing up to
  237. * max 8 bytes.
  238. */
  239. #define XEN_EARLY_IDT_HANDLER_SIZE 8
  240. #ifndef __ASSEMBLY__
  241. extern const char early_idt_handler_array[NUM_EXCEPTION_VECTORS][EARLY_IDT_HANDLER_SIZE];
  242. extern void early_ignore_irq(void);
  243. #if defined(CONFIG_X86_64) && defined(CONFIG_XEN_PV)
  244. extern const char xen_early_idt_handler_array[NUM_EXCEPTION_VECTORS][XEN_EARLY_IDT_HANDLER_SIZE];
  245. #endif
  246. /*
  247. * Load a segment. Fall back on loading the zero segment if something goes
  248. * wrong. This variant assumes that loading zero fully clears the segment.
  249. * This is always the case on Intel CPUs and, even on 64-bit AMD CPUs, any
  250. * failure to fully clear the cached descriptor is only observable for
  251. * FS and GS.
  252. */
  253. #define __loadsegment_simple(seg, value) \
  254. do { \
  255. unsigned short __val = (value); \
  256. \
  257. asm volatile(" \n" \
  258. "1: movl %k0,%%" #seg " \n" \
  259. \
  260. ".section .fixup,\"ax\" \n" \
  261. "2: xorl %k0,%k0 \n" \
  262. " jmp 1b \n" \
  263. ".previous \n" \
  264. \
  265. _ASM_EXTABLE(1b, 2b) \
  266. \
  267. : "+r" (__val) : : "memory"); \
  268. } while (0)
  269. #define __loadsegment_ss(value) __loadsegment_simple(ss, (value))
  270. #define __loadsegment_ds(value) __loadsegment_simple(ds, (value))
  271. #define __loadsegment_es(value) __loadsegment_simple(es, (value))
  272. #ifdef CONFIG_X86_32
  273. /*
  274. * On 32-bit systems, the hidden parts of FS and GS are unobservable if
  275. * the selector is NULL, so there's no funny business here.
  276. */
  277. #define __loadsegment_fs(value) __loadsegment_simple(fs, (value))
  278. #define __loadsegment_gs(value) __loadsegment_simple(gs, (value))
  279. #else
  280. static inline void __loadsegment_fs(unsigned short value)
  281. {
  282. asm volatile(" \n"
  283. "1: movw %0, %%fs \n"
  284. "2: \n"
  285. _ASM_EXTABLE_HANDLE(1b, 2b, ex_handler_clear_fs)
  286. : : "rm" (value) : "memory");
  287. }
  288. /* __loadsegment_gs is intentionally undefined. Use load_gs_index instead. */
  289. #endif
  290. #define loadsegment(seg, value) __loadsegment_ ## seg (value)
  291. /*
  292. * Save a segment register away:
  293. */
  294. #define savesegment(seg, value) \
  295. asm("mov %%" #seg ",%0":"=r" (value) : : "memory")
  296. /*
  297. * x86-32 user GS accessors:
  298. */
  299. #ifdef CONFIG_X86_32
  300. # ifdef CONFIG_X86_32_LAZY_GS
  301. # define get_user_gs(regs) (u16)({ unsigned long v; savesegment(gs, v); v; })
  302. # define set_user_gs(regs, v) loadsegment(gs, (unsigned long)(v))
  303. # define task_user_gs(tsk) ((tsk)->thread.gs)
  304. # define lazy_save_gs(v) savesegment(gs, (v))
  305. # define lazy_load_gs(v) loadsegment(gs, (v))
  306. # else /* X86_32_LAZY_GS */
  307. # define get_user_gs(regs) (u16)((regs)->gs)
  308. # define set_user_gs(regs, v) do { (regs)->gs = (v); } while (0)
  309. # define task_user_gs(tsk) (task_pt_regs(tsk)->gs)
  310. # define lazy_save_gs(v) do { } while (0)
  311. # define lazy_load_gs(v) do { } while (0)
  312. # endif /* X86_32_LAZY_GS */
  313. #endif /* X86_32 */
  314. #endif /* !__ASSEMBLY__ */
  315. #endif /* __KERNEL__ */
  316. #endif /* _ASM_X86_SEGMENT_H */