segment.h 9.8 KB

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