desc.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ASM_X86_DESC_H
  3. #define _ASM_X86_DESC_H
  4. #include <asm/desc_defs.h>
  5. #include <asm/ldt.h>
  6. #include <asm/mmu.h>
  7. #include <asm/fixmap.h>
  8. #include <asm/irq_vectors.h>
  9. #include <asm/cpu_entry_area.h>
  10. #include <linux/debug_locks.h>
  11. #include <linux/smp.h>
  12. #include <linux/percpu.h>
  13. static inline void fill_ldt(struct desc_struct *desc, const struct user_desc *info)
  14. {
  15. desc->limit0 = info->limit & 0x0ffff;
  16. desc->base0 = (info->base_addr & 0x0000ffff);
  17. desc->base1 = (info->base_addr & 0x00ff0000) >> 16;
  18. desc->type = (info->read_exec_only ^ 1) << 1;
  19. desc->type |= info->contents << 2;
  20. /* Set the ACCESS bit so it can be mapped RO */
  21. desc->type |= 1;
  22. desc->s = 1;
  23. desc->dpl = 0x3;
  24. desc->p = info->seg_not_present ^ 1;
  25. desc->limit1 = (info->limit & 0xf0000) >> 16;
  26. desc->avl = info->useable;
  27. desc->d = info->seg_32bit;
  28. desc->g = info->limit_in_pages;
  29. desc->base2 = (info->base_addr & 0xff000000) >> 24;
  30. /*
  31. * Don't allow setting of the lm bit. It would confuse
  32. * user_64bit_mode and would get overridden by sysret anyway.
  33. */
  34. desc->l = 0;
  35. }
  36. struct gdt_page {
  37. struct desc_struct gdt[GDT_ENTRIES];
  38. } __attribute__((aligned(PAGE_SIZE)));
  39. DECLARE_PER_CPU_PAGE_ALIGNED(struct gdt_page, gdt_page);
  40. /* Provide the original GDT */
  41. static inline struct desc_struct *get_cpu_gdt_rw(unsigned int cpu)
  42. {
  43. return per_cpu(gdt_page, cpu).gdt;
  44. }
  45. /* Provide the current original GDT */
  46. static inline struct desc_struct *get_current_gdt_rw(void)
  47. {
  48. return this_cpu_ptr(&gdt_page)->gdt;
  49. }
  50. /* Provide the fixmap address of the remapped GDT */
  51. static inline struct desc_struct *get_cpu_gdt_ro(int cpu)
  52. {
  53. return (struct desc_struct *)&get_cpu_entry_area(cpu)->gdt;
  54. }
  55. /* Provide the current read-only GDT */
  56. static inline struct desc_struct *get_current_gdt_ro(void)
  57. {
  58. return get_cpu_gdt_ro(smp_processor_id());
  59. }
  60. /* Provide the physical address of the GDT page. */
  61. static inline phys_addr_t get_cpu_gdt_paddr(unsigned int cpu)
  62. {
  63. return per_cpu_ptr_to_phys(get_cpu_gdt_rw(cpu));
  64. }
  65. static inline void pack_gate(gate_desc *gate, unsigned type, unsigned long func,
  66. unsigned dpl, unsigned ist, unsigned seg)
  67. {
  68. gate->offset_low = (u16) func;
  69. gate->bits.p = 1;
  70. gate->bits.dpl = dpl;
  71. gate->bits.zero = 0;
  72. gate->bits.type = type;
  73. gate->offset_middle = (u16) (func >> 16);
  74. #ifdef CONFIG_X86_64
  75. gate->segment = __KERNEL_CS;
  76. gate->bits.ist = ist;
  77. gate->reserved = 0;
  78. gate->offset_high = (u32) (func >> 32);
  79. #else
  80. gate->segment = seg;
  81. gate->bits.ist = 0;
  82. #endif
  83. }
  84. static inline int desc_empty(const void *ptr)
  85. {
  86. const u32 *desc = ptr;
  87. return !(desc[0] | desc[1]);
  88. }
  89. #ifdef CONFIG_PARAVIRT_XXL
  90. #include <asm/paravirt.h>
  91. #else
  92. #define load_TR_desc() native_load_tr_desc()
  93. #define load_gdt(dtr) native_load_gdt(dtr)
  94. #define load_idt(dtr) native_load_idt(dtr)
  95. #define load_tr(tr) asm volatile("ltr %0"::"m" (tr))
  96. #define load_ldt(ldt) asm volatile("lldt %0"::"m" (ldt))
  97. #define store_gdt(dtr) native_store_gdt(dtr)
  98. #define store_tr(tr) (tr = native_store_tr())
  99. #define load_TLS(t, cpu) native_load_tls(t, cpu)
  100. #define set_ldt native_set_ldt
  101. #define write_ldt_entry(dt, entry, desc) native_write_ldt_entry(dt, entry, desc)
  102. #define write_gdt_entry(dt, entry, desc, type) native_write_gdt_entry(dt, entry, desc, type)
  103. #define write_idt_entry(dt, entry, g) native_write_idt_entry(dt, entry, g)
  104. static inline void paravirt_alloc_ldt(struct desc_struct *ldt, unsigned entries)
  105. {
  106. }
  107. static inline void paravirt_free_ldt(struct desc_struct *ldt, unsigned entries)
  108. {
  109. }
  110. #endif /* CONFIG_PARAVIRT_XXL */
  111. #define store_ldt(ldt) asm("sldt %0" : "=m"(ldt))
  112. static inline void native_write_idt_entry(gate_desc *idt, int entry, const gate_desc *gate)
  113. {
  114. memcpy(&idt[entry], gate, sizeof(*gate));
  115. }
  116. static inline void native_write_ldt_entry(struct desc_struct *ldt, int entry, const void *desc)
  117. {
  118. memcpy(&ldt[entry], desc, 8);
  119. }
  120. static inline void
  121. native_write_gdt_entry(struct desc_struct *gdt, int entry, const void *desc, int type)
  122. {
  123. unsigned int size;
  124. switch (type) {
  125. case DESC_TSS: size = sizeof(tss_desc); break;
  126. case DESC_LDT: size = sizeof(ldt_desc); break;
  127. default: size = sizeof(*gdt); break;
  128. }
  129. memcpy(&gdt[entry], desc, size);
  130. }
  131. static inline void set_tssldt_descriptor(void *d, unsigned long addr,
  132. unsigned type, unsigned size)
  133. {
  134. struct ldttss_desc *desc = d;
  135. memset(desc, 0, sizeof(*desc));
  136. desc->limit0 = (u16) size;
  137. desc->base0 = (u16) addr;
  138. desc->base1 = (addr >> 16) & 0xFF;
  139. desc->type = type;
  140. desc->p = 1;
  141. desc->limit1 = (size >> 16) & 0xF;
  142. desc->base2 = (addr >> 24) & 0xFF;
  143. #ifdef CONFIG_X86_64
  144. desc->base3 = (u32) (addr >> 32);
  145. #endif
  146. }
  147. static inline void __set_tss_desc(unsigned cpu, unsigned int entry, struct x86_hw_tss *addr)
  148. {
  149. struct desc_struct *d = get_cpu_gdt_rw(cpu);
  150. tss_desc tss;
  151. set_tssldt_descriptor(&tss, (unsigned long)addr, DESC_TSS,
  152. __KERNEL_TSS_LIMIT);
  153. write_gdt_entry(d, entry, &tss, DESC_TSS);
  154. }
  155. #define set_tss_desc(cpu, addr) __set_tss_desc(cpu, GDT_ENTRY_TSS, addr)
  156. static inline void native_set_ldt(const void *addr, unsigned int entries)
  157. {
  158. if (likely(entries == 0))
  159. asm volatile("lldt %w0"::"q" (0));
  160. else {
  161. unsigned cpu = smp_processor_id();
  162. ldt_desc ldt;
  163. set_tssldt_descriptor(&ldt, (unsigned long)addr, DESC_LDT,
  164. entries * LDT_ENTRY_SIZE - 1);
  165. write_gdt_entry(get_cpu_gdt_rw(cpu), GDT_ENTRY_LDT,
  166. &ldt, DESC_LDT);
  167. asm volatile("lldt %w0"::"q" (GDT_ENTRY_LDT*8));
  168. }
  169. }
  170. static inline void native_load_gdt(const struct desc_ptr *dtr)
  171. {
  172. asm volatile("lgdt %0"::"m" (*dtr));
  173. }
  174. static __always_inline void native_load_idt(const struct desc_ptr *dtr)
  175. {
  176. asm volatile("lidt %0"::"m" (*dtr));
  177. }
  178. static inline void native_store_gdt(struct desc_ptr *dtr)
  179. {
  180. asm volatile("sgdt %0":"=m" (*dtr));
  181. }
  182. static inline void store_idt(struct desc_ptr *dtr)
  183. {
  184. asm volatile("sidt %0":"=m" (*dtr));
  185. }
  186. static inline void native_gdt_invalidate(void)
  187. {
  188. const struct desc_ptr invalid_gdt = {
  189. .address = 0,
  190. .size = 0
  191. };
  192. native_load_gdt(&invalid_gdt);
  193. }
  194. static inline void native_idt_invalidate(void)
  195. {
  196. const struct desc_ptr invalid_idt = {
  197. .address = 0,
  198. .size = 0
  199. };
  200. native_load_idt(&invalid_idt);
  201. }
  202. /*
  203. * The LTR instruction marks the TSS GDT entry as busy. On 64-bit, the GDT is
  204. * a read-only remapping. To prevent a page fault, the GDT is switched to the
  205. * original writeable version when needed.
  206. */
  207. #ifdef CONFIG_X86_64
  208. static inline void native_load_tr_desc(void)
  209. {
  210. struct desc_ptr gdt;
  211. int cpu = raw_smp_processor_id();
  212. bool restore = 0;
  213. struct desc_struct *fixmap_gdt;
  214. native_store_gdt(&gdt);
  215. fixmap_gdt = get_cpu_gdt_ro(cpu);
  216. /*
  217. * If the current GDT is the read-only fixmap, swap to the original
  218. * writeable version. Swap back at the end.
  219. */
  220. if (gdt.address == (unsigned long)fixmap_gdt) {
  221. load_direct_gdt(cpu);
  222. restore = 1;
  223. }
  224. asm volatile("ltr %w0"::"q" (GDT_ENTRY_TSS*8));
  225. if (restore)
  226. load_fixmap_gdt(cpu);
  227. }
  228. #else
  229. static inline void native_load_tr_desc(void)
  230. {
  231. asm volatile("ltr %w0"::"q" (GDT_ENTRY_TSS*8));
  232. }
  233. #endif
  234. static inline unsigned long native_store_tr(void)
  235. {
  236. unsigned long tr;
  237. asm volatile("str %0":"=r" (tr));
  238. return tr;
  239. }
  240. static inline void native_load_tls(struct thread_struct *t, unsigned int cpu)
  241. {
  242. struct desc_struct *gdt = get_cpu_gdt_rw(cpu);
  243. unsigned int i;
  244. for (i = 0; i < GDT_ENTRY_TLS_ENTRIES; i++)
  245. gdt[GDT_ENTRY_TLS_MIN + i] = t->tls_array[i];
  246. }
  247. DECLARE_PER_CPU(bool, __tss_limit_invalid);
  248. static inline void force_reload_TR(void)
  249. {
  250. struct desc_struct *d = get_current_gdt_rw();
  251. tss_desc tss;
  252. memcpy(&tss, &d[GDT_ENTRY_TSS], sizeof(tss_desc));
  253. /*
  254. * LTR requires an available TSS, and the TSS is currently
  255. * busy. Make it be available so that LTR will work.
  256. */
  257. tss.type = DESC_TSS;
  258. write_gdt_entry(d, GDT_ENTRY_TSS, &tss, DESC_TSS);
  259. load_TR_desc();
  260. this_cpu_write(__tss_limit_invalid, false);
  261. }
  262. /*
  263. * Call this if you need the TSS limit to be correct, which should be the case
  264. * if and only if you have TIF_IO_BITMAP set or you're switching to a task
  265. * with TIF_IO_BITMAP set.
  266. */
  267. static inline void refresh_tss_limit(void)
  268. {
  269. DEBUG_LOCKS_WARN_ON(preemptible());
  270. if (unlikely(this_cpu_read(__tss_limit_invalid)))
  271. force_reload_TR();
  272. }
  273. /*
  274. * If you do something evil that corrupts the cached TSS limit (I'm looking
  275. * at you, VMX exits), call this function.
  276. *
  277. * The optimization here is that the TSS limit only matters for Linux if the
  278. * IO bitmap is in use. If the TSS limit gets forced to its minimum value,
  279. * everything works except that IO bitmap will be ignored and all CPL 3 IO
  280. * instructions will #GP, which is exactly what we want for normal tasks.
  281. */
  282. static inline void invalidate_tss_limit(void)
  283. {
  284. DEBUG_LOCKS_WARN_ON(preemptible());
  285. if (unlikely(test_thread_flag(TIF_IO_BITMAP)))
  286. force_reload_TR();
  287. else
  288. this_cpu_write(__tss_limit_invalid, true);
  289. }
  290. /* This intentionally ignores lm, since 32-bit apps don't have that field. */
  291. #define LDT_empty(info) \
  292. ((info)->base_addr == 0 && \
  293. (info)->limit == 0 && \
  294. (info)->contents == 0 && \
  295. (info)->read_exec_only == 1 && \
  296. (info)->seg_32bit == 0 && \
  297. (info)->limit_in_pages == 0 && \
  298. (info)->seg_not_present == 1 && \
  299. (info)->useable == 0)
  300. /* Lots of programs expect an all-zero user_desc to mean "no segment at all". */
  301. static inline bool LDT_zero(const struct user_desc *info)
  302. {
  303. return (info->base_addr == 0 &&
  304. info->limit == 0 &&
  305. info->contents == 0 &&
  306. info->read_exec_only == 0 &&
  307. info->seg_32bit == 0 &&
  308. info->limit_in_pages == 0 &&
  309. info->seg_not_present == 0 &&
  310. info->useable == 0);
  311. }
  312. static inline void clear_LDT(void)
  313. {
  314. set_ldt(NULL, 0);
  315. }
  316. static inline unsigned long get_desc_base(const struct desc_struct *desc)
  317. {
  318. return (unsigned)(desc->base0 | ((desc->base1) << 16) | ((desc->base2) << 24));
  319. }
  320. static inline void set_desc_base(struct desc_struct *desc, unsigned long base)
  321. {
  322. desc->base0 = base & 0xffff;
  323. desc->base1 = (base >> 16) & 0xff;
  324. desc->base2 = (base >> 24) & 0xff;
  325. }
  326. static inline unsigned long get_desc_limit(const struct desc_struct *desc)
  327. {
  328. return desc->limit0 | (desc->limit1 << 16);
  329. }
  330. static inline void set_desc_limit(struct desc_struct *desc, unsigned long limit)
  331. {
  332. desc->limit0 = limit & 0xffff;
  333. desc->limit1 = (limit >> 16) & 0xf;
  334. }
  335. void alloc_intr_gate(unsigned int n, const void *addr);
  336. static inline void init_idt_data(struct idt_data *data, unsigned int n,
  337. const void *addr)
  338. {
  339. BUG_ON(n > 0xFF);
  340. memset(data, 0, sizeof(*data));
  341. data->vector = n;
  342. data->addr = addr;
  343. data->segment = __KERNEL_CS;
  344. data->bits.type = GATE_INTERRUPT;
  345. data->bits.p = 1;
  346. }
  347. static inline void idt_init_desc(gate_desc *gate, const struct idt_data *d)
  348. {
  349. unsigned long addr = (unsigned long) d->addr;
  350. gate->offset_low = (u16) addr;
  351. gate->segment = (u16) d->segment;
  352. gate->bits = d->bits;
  353. gate->offset_middle = (u16) (addr >> 16);
  354. #ifdef CONFIG_X86_64
  355. gate->offset_high = (u32) (addr >> 32);
  356. gate->reserved = 0;
  357. #endif
  358. }
  359. extern unsigned long system_vectors[];
  360. extern void load_current_idt(void);
  361. extern void idt_setup_early_handler(void);
  362. extern void idt_setup_early_traps(void);
  363. extern void idt_setup_traps(void);
  364. extern void idt_setup_apic_and_irq_gates(void);
  365. extern bool idt_is_f00f_address(unsigned long address);
  366. #ifdef CONFIG_X86_64
  367. extern void idt_setup_early_pf(void);
  368. #else
  369. static inline void idt_setup_early_pf(void) { }
  370. #endif
  371. extern void idt_invalidate(void);
  372. #endif /* _ASM_X86_DESC_H */