desc.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. #ifndef _ASM_X86_DESC_H
  2. #define _ASM_X86_DESC_H
  3. #include <asm/desc_defs.h>
  4. #include <asm/ldt.h>
  5. #include <asm/mmu.h>
  6. #include <linux/smp.h>
  7. #include <linux/percpu.h>
  8. static inline void fill_ldt(struct desc_struct *desc, const struct user_desc *info)
  9. {
  10. desc->limit0 = info->limit & 0x0ffff;
  11. desc->base0 = (info->base_addr & 0x0000ffff);
  12. desc->base1 = (info->base_addr & 0x00ff0000) >> 16;
  13. desc->type = (info->read_exec_only ^ 1) << 1;
  14. desc->type |= info->contents << 2;
  15. desc->s = 1;
  16. desc->dpl = 0x3;
  17. desc->p = info->seg_not_present ^ 1;
  18. desc->limit = (info->limit & 0xf0000) >> 16;
  19. desc->avl = info->useable;
  20. desc->d = info->seg_32bit;
  21. desc->g = info->limit_in_pages;
  22. desc->base2 = (info->base_addr & 0xff000000) >> 24;
  23. /*
  24. * Don't allow setting of the lm bit. It would confuse
  25. * user_64bit_mode and would get overridden by sysret anyway.
  26. */
  27. desc->l = 0;
  28. }
  29. extern struct desc_ptr idt_descr;
  30. extern gate_desc idt_table[];
  31. extern const struct desc_ptr debug_idt_descr;
  32. extern gate_desc debug_idt_table[];
  33. struct gdt_page {
  34. struct desc_struct gdt[GDT_ENTRIES];
  35. } __attribute__((aligned(PAGE_SIZE)));
  36. DECLARE_PER_CPU_PAGE_ALIGNED(struct gdt_page, gdt_page);
  37. static inline struct desc_struct *get_cpu_gdt_table(unsigned int cpu)
  38. {
  39. return per_cpu(gdt_page, cpu).gdt;
  40. }
  41. #ifdef CONFIG_X86_64
  42. static inline void pack_gate(gate_desc *gate, unsigned type, unsigned long func,
  43. unsigned dpl, unsigned ist, unsigned seg)
  44. {
  45. gate->offset_low = PTR_LOW(func);
  46. gate->segment = __KERNEL_CS;
  47. gate->ist = ist;
  48. gate->p = 1;
  49. gate->dpl = dpl;
  50. gate->zero0 = 0;
  51. gate->zero1 = 0;
  52. gate->type = type;
  53. gate->offset_middle = PTR_MIDDLE(func);
  54. gate->offset_high = PTR_HIGH(func);
  55. }
  56. #else
  57. static inline void pack_gate(gate_desc *gate, unsigned char type,
  58. unsigned long base, unsigned dpl, unsigned flags,
  59. unsigned short seg)
  60. {
  61. gate->a = (seg << 16) | (base & 0xffff);
  62. gate->b = (base & 0xffff0000) | (((0x80 | type | (dpl << 5)) & 0xff) << 8);
  63. }
  64. #endif
  65. static inline int desc_empty(const void *ptr)
  66. {
  67. const u32 *desc = ptr;
  68. return !(desc[0] | desc[1]);
  69. }
  70. #ifdef CONFIG_PARAVIRT
  71. #include <asm/paravirt.h>
  72. #else
  73. #define load_TR_desc() native_load_tr_desc()
  74. #define load_gdt(dtr) native_load_gdt(dtr)
  75. #define load_idt(dtr) native_load_idt(dtr)
  76. #define load_tr(tr) asm volatile("ltr %0"::"m" (tr))
  77. #define load_ldt(ldt) asm volatile("lldt %0"::"m" (ldt))
  78. #define store_gdt(dtr) native_store_gdt(dtr)
  79. #define store_idt(dtr) native_store_idt(dtr)
  80. #define store_tr(tr) (tr = native_store_tr())
  81. #define load_TLS(t, cpu) native_load_tls(t, cpu)
  82. #define set_ldt native_set_ldt
  83. #define write_ldt_entry(dt, entry, desc) native_write_ldt_entry(dt, entry, desc)
  84. #define write_gdt_entry(dt, entry, desc, type) native_write_gdt_entry(dt, entry, desc, type)
  85. #define write_idt_entry(dt, entry, g) native_write_idt_entry(dt, entry, g)
  86. static inline void paravirt_alloc_ldt(struct desc_struct *ldt, unsigned entries)
  87. {
  88. }
  89. static inline void paravirt_free_ldt(struct desc_struct *ldt, unsigned entries)
  90. {
  91. }
  92. #endif /* CONFIG_PARAVIRT */
  93. #define store_ldt(ldt) asm("sldt %0" : "=m"(ldt))
  94. static inline void native_write_idt_entry(gate_desc *idt, int entry, const gate_desc *gate)
  95. {
  96. memcpy(&idt[entry], gate, sizeof(*gate));
  97. }
  98. static inline void native_write_ldt_entry(struct desc_struct *ldt, int entry, const void *desc)
  99. {
  100. memcpy(&ldt[entry], desc, 8);
  101. }
  102. static inline void
  103. native_write_gdt_entry(struct desc_struct *gdt, int entry, const void *desc, int type)
  104. {
  105. unsigned int size;
  106. switch (type) {
  107. case DESC_TSS: size = sizeof(tss_desc); break;
  108. case DESC_LDT: size = sizeof(ldt_desc); break;
  109. default: size = sizeof(*gdt); break;
  110. }
  111. memcpy(&gdt[entry], desc, size);
  112. }
  113. static inline void pack_descriptor(struct desc_struct *desc, unsigned long base,
  114. unsigned long limit, unsigned char type,
  115. unsigned char flags)
  116. {
  117. desc->a = ((base & 0xffff) << 16) | (limit & 0xffff);
  118. desc->b = (base & 0xff000000) | ((base & 0xff0000) >> 16) |
  119. (limit & 0x000f0000) | ((type & 0xff) << 8) |
  120. ((flags & 0xf) << 20);
  121. desc->p = 1;
  122. }
  123. static inline void set_tssldt_descriptor(void *d, unsigned long addr, unsigned type, unsigned size)
  124. {
  125. #ifdef CONFIG_X86_64
  126. struct ldttss_desc64 *desc = d;
  127. memset(desc, 0, sizeof(*desc));
  128. desc->limit0 = size & 0xFFFF;
  129. desc->base0 = PTR_LOW(addr);
  130. desc->base1 = PTR_MIDDLE(addr) & 0xFF;
  131. desc->type = type;
  132. desc->p = 1;
  133. desc->limit1 = (size >> 16) & 0xF;
  134. desc->base2 = (PTR_MIDDLE(addr) >> 8) & 0xFF;
  135. desc->base3 = PTR_HIGH(addr);
  136. #else
  137. pack_descriptor((struct desc_struct *)d, addr, size, 0x80 | type, 0);
  138. #endif
  139. }
  140. static inline void __set_tss_desc(unsigned cpu, unsigned int entry, void *addr)
  141. {
  142. struct desc_struct *d = get_cpu_gdt_table(cpu);
  143. tss_desc tss;
  144. /*
  145. * sizeof(unsigned long) coming from an extra "long" at the end
  146. * of the iobitmap. See tss_struct definition in processor.h
  147. *
  148. * -1? seg base+limit should be pointing to the address of the
  149. * last valid byte
  150. */
  151. set_tssldt_descriptor(&tss, (unsigned long)addr, DESC_TSS,
  152. IO_BITMAP_OFFSET + IO_BITMAP_BYTES +
  153. sizeof(unsigned long) - 1);
  154. write_gdt_entry(d, entry, &tss, DESC_TSS);
  155. }
  156. #define set_tss_desc(cpu, addr) __set_tss_desc(cpu, GDT_ENTRY_TSS, addr)
  157. static inline void native_set_ldt(const void *addr, unsigned int entries)
  158. {
  159. if (likely(entries == 0))
  160. asm volatile("lldt %w0"::"q" (0));
  161. else {
  162. unsigned cpu = smp_processor_id();
  163. ldt_desc ldt;
  164. set_tssldt_descriptor(&ldt, (unsigned long)addr, DESC_LDT,
  165. entries * LDT_ENTRY_SIZE - 1);
  166. write_gdt_entry(get_cpu_gdt_table(cpu), GDT_ENTRY_LDT,
  167. &ldt, DESC_LDT);
  168. asm volatile("lldt %w0"::"q" (GDT_ENTRY_LDT*8));
  169. }
  170. }
  171. static inline void native_load_tr_desc(void)
  172. {
  173. asm volatile("ltr %w0"::"q" (GDT_ENTRY_TSS*8));
  174. }
  175. static inline void native_load_gdt(const struct desc_ptr *dtr)
  176. {
  177. asm volatile("lgdt %0"::"m" (*dtr));
  178. }
  179. static inline void native_load_idt(const struct desc_ptr *dtr)
  180. {
  181. asm volatile("lidt %0"::"m" (*dtr));
  182. }
  183. static inline void native_store_gdt(struct desc_ptr *dtr)
  184. {
  185. asm volatile("sgdt %0":"=m" (*dtr));
  186. }
  187. static inline void native_store_idt(struct desc_ptr *dtr)
  188. {
  189. asm volatile("sidt %0":"=m" (*dtr));
  190. }
  191. static inline unsigned long native_store_tr(void)
  192. {
  193. unsigned long tr;
  194. asm volatile("str %0":"=r" (tr));
  195. return tr;
  196. }
  197. static inline void native_load_tls(struct thread_struct *t, unsigned int cpu)
  198. {
  199. struct desc_struct *gdt = get_cpu_gdt_table(cpu);
  200. unsigned int i;
  201. for (i = 0; i < GDT_ENTRY_TLS_ENTRIES; i++)
  202. gdt[GDT_ENTRY_TLS_MIN + i] = t->tls_array[i];
  203. }
  204. /* This intentionally ignores lm, since 32-bit apps don't have that field. */
  205. #define LDT_empty(info) \
  206. ((info)->base_addr == 0 && \
  207. (info)->limit == 0 && \
  208. (info)->contents == 0 && \
  209. (info)->read_exec_only == 1 && \
  210. (info)->seg_32bit == 0 && \
  211. (info)->limit_in_pages == 0 && \
  212. (info)->seg_not_present == 1 && \
  213. (info)->useable == 0)
  214. /* Lots of programs expect an all-zero user_desc to mean "no segment at all". */
  215. static inline bool LDT_zero(const struct user_desc *info)
  216. {
  217. return (info->base_addr == 0 &&
  218. info->limit == 0 &&
  219. info->contents == 0 &&
  220. info->read_exec_only == 0 &&
  221. info->seg_32bit == 0 &&
  222. info->limit_in_pages == 0 &&
  223. info->seg_not_present == 0 &&
  224. info->useable == 0);
  225. }
  226. static inline void clear_LDT(void)
  227. {
  228. set_ldt(NULL, 0);
  229. }
  230. static inline unsigned long get_desc_base(const struct desc_struct *desc)
  231. {
  232. return (unsigned)(desc->base0 | ((desc->base1) << 16) | ((desc->base2) << 24));
  233. }
  234. static inline void set_desc_base(struct desc_struct *desc, unsigned long base)
  235. {
  236. desc->base0 = base & 0xffff;
  237. desc->base1 = (base >> 16) & 0xff;
  238. desc->base2 = (base >> 24) & 0xff;
  239. }
  240. static inline unsigned long get_desc_limit(const struct desc_struct *desc)
  241. {
  242. return desc->limit0 | (desc->limit << 16);
  243. }
  244. static inline void set_desc_limit(struct desc_struct *desc, unsigned long limit)
  245. {
  246. desc->limit0 = limit & 0xffff;
  247. desc->limit = (limit >> 16) & 0xf;
  248. }
  249. #ifdef CONFIG_X86_64
  250. static inline void set_nmi_gate(int gate, void *addr)
  251. {
  252. gate_desc s;
  253. pack_gate(&s, GATE_INTERRUPT, (unsigned long)addr, 0, 0, __KERNEL_CS);
  254. write_idt_entry(debug_idt_table, gate, &s);
  255. }
  256. #endif
  257. #ifdef CONFIG_TRACING
  258. extern struct desc_ptr trace_idt_descr;
  259. extern gate_desc trace_idt_table[];
  260. static inline void write_trace_idt_entry(int entry, const gate_desc *gate)
  261. {
  262. write_idt_entry(trace_idt_table, entry, gate);
  263. }
  264. static inline void _trace_set_gate(int gate, unsigned type, void *addr,
  265. unsigned dpl, unsigned ist, unsigned seg)
  266. {
  267. gate_desc s;
  268. pack_gate(&s, type, (unsigned long)addr, dpl, ist, seg);
  269. /*
  270. * does not need to be atomic because it is only done once at
  271. * setup time
  272. */
  273. write_trace_idt_entry(gate, &s);
  274. }
  275. #else
  276. static inline void write_trace_idt_entry(int entry, const gate_desc *gate)
  277. {
  278. }
  279. #define _trace_set_gate(gate, type, addr, dpl, ist, seg)
  280. #endif
  281. static inline void _set_gate(int gate, unsigned type, void *addr,
  282. unsigned dpl, unsigned ist, unsigned seg)
  283. {
  284. gate_desc s;
  285. pack_gate(&s, type, (unsigned long)addr, dpl, ist, seg);
  286. /*
  287. * does not need to be atomic because it is only done once at
  288. * setup time
  289. */
  290. write_idt_entry(idt_table, gate, &s);
  291. write_trace_idt_entry(gate, &s);
  292. }
  293. /*
  294. * This needs to use 'idt_table' rather than 'idt', and
  295. * thus use the _nonmapped_ version of the IDT, as the
  296. * Pentium F0 0F bugfix can have resulted in the mapped
  297. * IDT being write-protected.
  298. */
  299. #define set_intr_gate_notrace(n, addr) \
  300. do { \
  301. BUG_ON((unsigned)n > 0xFF); \
  302. _set_gate(n, GATE_INTERRUPT, (void *)addr, 0, 0, \
  303. __KERNEL_CS); \
  304. } while (0)
  305. #define set_intr_gate(n, addr) \
  306. do { \
  307. set_intr_gate_notrace(n, addr); \
  308. _trace_set_gate(n, GATE_INTERRUPT, (void *)trace_##addr,\
  309. 0, 0, __KERNEL_CS); \
  310. } while (0)
  311. extern int first_system_vector;
  312. /* used_vectors is BITMAP for irq is not managed by percpu vector_irq */
  313. extern unsigned long used_vectors[];
  314. static inline void alloc_system_vector(int vector)
  315. {
  316. if (!test_bit(vector, used_vectors)) {
  317. set_bit(vector, used_vectors);
  318. if (first_system_vector > vector)
  319. first_system_vector = vector;
  320. } else {
  321. BUG();
  322. }
  323. }
  324. #define alloc_intr_gate(n, addr) \
  325. do { \
  326. alloc_system_vector(n); \
  327. set_intr_gate(n, addr); \
  328. } while (0)
  329. /*
  330. * This routine sets up an interrupt gate at directory privilege level 3.
  331. */
  332. static inline void set_system_intr_gate(unsigned int n, void *addr)
  333. {
  334. BUG_ON((unsigned)n > 0xFF);
  335. _set_gate(n, GATE_INTERRUPT, addr, 0x3, 0, __KERNEL_CS);
  336. }
  337. static inline void set_system_trap_gate(unsigned int n, void *addr)
  338. {
  339. BUG_ON((unsigned)n > 0xFF);
  340. _set_gate(n, GATE_TRAP, addr, 0x3, 0, __KERNEL_CS);
  341. }
  342. static inline void set_trap_gate(unsigned int n, void *addr)
  343. {
  344. BUG_ON((unsigned)n > 0xFF);
  345. _set_gate(n, GATE_TRAP, addr, 0, 0, __KERNEL_CS);
  346. }
  347. static inline void set_task_gate(unsigned int n, unsigned int gdt_entry)
  348. {
  349. BUG_ON((unsigned)n > 0xFF);
  350. _set_gate(n, GATE_TASK, (void *)0, 0, 0, (gdt_entry<<3));
  351. }
  352. static inline void set_intr_gate_ist(int n, void *addr, unsigned ist)
  353. {
  354. BUG_ON((unsigned)n > 0xFF);
  355. _set_gate(n, GATE_INTERRUPT, addr, 0, ist, __KERNEL_CS);
  356. }
  357. static inline void set_system_intr_gate_ist(int n, void *addr, unsigned ist)
  358. {
  359. BUG_ON((unsigned)n > 0xFF);
  360. _set_gate(n, GATE_INTERRUPT, addr, 0x3, ist, __KERNEL_CS);
  361. }
  362. #ifdef CONFIG_X86_64
  363. DECLARE_PER_CPU(u32, debug_idt_ctr);
  364. static inline bool is_debug_idt_enabled(void)
  365. {
  366. if (this_cpu_read(debug_idt_ctr))
  367. return true;
  368. return false;
  369. }
  370. static inline void load_debug_idt(void)
  371. {
  372. load_idt((const struct desc_ptr *)&debug_idt_descr);
  373. }
  374. #else
  375. static inline bool is_debug_idt_enabled(void)
  376. {
  377. return false;
  378. }
  379. static inline void load_debug_idt(void)
  380. {
  381. }
  382. #endif
  383. #ifdef CONFIG_TRACING
  384. extern atomic_t trace_idt_ctr;
  385. static inline bool is_trace_idt_enabled(void)
  386. {
  387. if (atomic_read(&trace_idt_ctr))
  388. return true;
  389. return false;
  390. }
  391. static inline void load_trace_idt(void)
  392. {
  393. load_idt((const struct desc_ptr *)&trace_idt_descr);
  394. }
  395. #else
  396. static inline bool is_trace_idt_enabled(void)
  397. {
  398. return false;
  399. }
  400. static inline void load_trace_idt(void)
  401. {
  402. }
  403. #endif
  404. /*
  405. * The load_current_idt() must be called with interrupts disabled
  406. * to avoid races. That way the IDT will always be set back to the expected
  407. * descriptor. It's also called when a CPU is being initialized, and
  408. * that doesn't need to disable interrupts, as nothing should be
  409. * bothering the CPU then.
  410. */
  411. static inline void load_current_idt(void)
  412. {
  413. if (is_debug_idt_enabled())
  414. load_debug_idt();
  415. else if (is_trace_idt_enabled())
  416. load_trace_idt();
  417. else
  418. load_idt((const struct desc_ptr *)&idt_descr);
  419. }
  420. #endif /* _ASM_X86_DESC_H */