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