desc.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. static inline void fill_ldt(struct desc_struct *desc, const struct user_desc *info)
  8. {
  9. desc->limit0 = info->limit & 0x0ffff;
  10. desc->base0 = (info->base_addr & 0x0000ffff);
  11. desc->base1 = (info->base_addr & 0x00ff0000) >> 16;
  12. desc->type = (info->read_exec_only ^ 1) << 1;
  13. desc->type |= info->contents << 2;
  14. desc->s = 1;
  15. desc->dpl = 0x3;
  16. desc->p = info->seg_not_present ^ 1;
  17. desc->limit = (info->limit & 0xf0000) >> 16;
  18. desc->avl = info->useable;
  19. desc->d = info->seg_32bit;
  20. desc->g = info->limit_in_pages;
  21. desc->base2 = (info->base_addr & 0xff000000) >> 24;
  22. /*
  23. * Don't allow setting of the lm bit. It would confuse
  24. * user_64bit_mode and would get overridden by sysret anyway.
  25. */
  26. desc->l = 0;
  27. }
  28. extern struct desc_ptr idt_descr;
  29. extern gate_desc idt_table[];
  30. extern struct desc_ptr nmi_idt_descr;
  31. extern gate_desc nmi_idt_table[];
  32. struct gdt_page {
  33. struct desc_struct gdt[GDT_ENTRIES];
  34. } __attribute__((aligned(PAGE_SIZE)));
  35. DECLARE_PER_CPU_PAGE_ALIGNED(struct gdt_page, gdt_page);
  36. static inline struct desc_struct *get_cpu_gdt_table(unsigned int cpu)
  37. {
  38. return per_cpu(gdt_page, cpu).gdt;
  39. }
  40. #ifdef CONFIG_X86_64
  41. static inline void pack_gate(gate_desc *gate, unsigned type, unsigned long func,
  42. unsigned dpl, unsigned ist, unsigned seg)
  43. {
  44. gate->offset_low = PTR_LOW(func);
  45. gate->segment = __KERNEL_CS;
  46. gate->ist = ist;
  47. gate->p = 1;
  48. gate->dpl = dpl;
  49. gate->zero0 = 0;
  50. gate->zero1 = 0;
  51. gate->type = type;
  52. gate->offset_middle = PTR_MIDDLE(func);
  53. gate->offset_high = PTR_HIGH(func);
  54. }
  55. #else
  56. static inline void pack_gate(gate_desc *gate, unsigned char type,
  57. unsigned long base, unsigned dpl, unsigned flags,
  58. unsigned short seg)
  59. {
  60. gate->a = (seg << 16) | (base & 0xffff);
  61. gate->b = (base & 0xffff0000) | (((0x80 | type | (dpl << 5)) & 0xff) << 8);
  62. }
  63. #endif
  64. static inline int desc_empty(const void *ptr)
  65. {
  66. const u32 *desc = ptr;
  67. return !(desc[0] | desc[1]);
  68. }
  69. #ifdef CONFIG_PARAVIRT
  70. #include <asm/paravirt.h>
  71. #else
  72. #define load_TR_desc() native_load_tr_desc()
  73. #define load_gdt(dtr) native_load_gdt(dtr)
  74. #define load_idt(dtr) native_load_idt(dtr)
  75. #define load_tr(tr) asm volatile("ltr %0"::"m" (tr))
  76. #define load_ldt(ldt) asm volatile("lldt %0"::"m" (ldt))
  77. #define store_gdt(dtr) native_store_gdt(dtr)
  78. #define store_idt(dtr) native_store_idt(dtr)
  79. #define store_tr(tr) (tr = native_store_tr())
  80. #define load_TLS(t, cpu) native_load_tls(t, cpu)
  81. #define set_ldt native_set_ldt
  82. #define write_ldt_entry(dt, entry, desc) native_write_ldt_entry(dt, entry, desc)
  83. #define write_gdt_entry(dt, entry, desc, type) native_write_gdt_entry(dt, entry, desc, type)
  84. #define write_idt_entry(dt, entry, g) native_write_idt_entry(dt, entry, g)
  85. static inline void paravirt_alloc_ldt(struct desc_struct *ldt, unsigned entries)
  86. {
  87. }
  88. static inline void paravirt_free_ldt(struct desc_struct *ldt, unsigned entries)
  89. {
  90. }
  91. #endif /* CONFIG_PARAVIRT */
  92. #define store_ldt(ldt) asm("sldt %0" : "=m"(ldt))
  93. static inline void native_write_idt_entry(gate_desc *idt, int entry, const gate_desc *gate)
  94. {
  95. memcpy(&idt[entry], gate, sizeof(*gate));
  96. }
  97. static inline void native_write_ldt_entry(struct desc_struct *ldt, int entry, const void *desc)
  98. {
  99. memcpy(&ldt[entry], desc, 8);
  100. }
  101. static inline void
  102. native_write_gdt_entry(struct desc_struct *gdt, int entry, const void *desc, int type)
  103. {
  104. unsigned int size;
  105. switch (type) {
  106. case DESC_TSS: size = sizeof(tss_desc); break;
  107. case DESC_LDT: size = sizeof(ldt_desc); break;
  108. default: size = sizeof(*gdt); break;
  109. }
  110. memcpy(&gdt[entry], desc, size);
  111. }
  112. static inline void pack_descriptor(struct desc_struct *desc, unsigned long base,
  113. unsigned long limit, unsigned char type,
  114. unsigned char flags)
  115. {
  116. desc->a = ((base & 0xffff) << 16) | (limit & 0xffff);
  117. desc->b = (base & 0xff000000) | ((base & 0xff0000) >> 16) |
  118. (limit & 0x000f0000) | ((type & 0xff) << 8) |
  119. ((flags & 0xf) << 20);
  120. desc->p = 1;
  121. }
  122. static inline void set_tssldt_descriptor(void *d, unsigned long addr, unsigned type, unsigned size)
  123. {
  124. #ifdef CONFIG_X86_64
  125. struct ldttss_desc64 *desc = d;
  126. memset(desc, 0, sizeof(*desc));
  127. desc->limit0 = size & 0xFFFF;
  128. desc->base0 = PTR_LOW(addr);
  129. desc->base1 = PTR_MIDDLE(addr) & 0xFF;
  130. desc->type = type;
  131. desc->p = 1;
  132. desc->limit1 = (size >> 16) & 0xF;
  133. desc->base2 = (PTR_MIDDLE(addr) >> 8) & 0xFF;
  134. desc->base3 = PTR_HIGH(addr);
  135. #else
  136. pack_descriptor((struct desc_struct *)d, addr, size, 0x80 | type, 0);
  137. #endif
  138. }
  139. static inline void __set_tss_desc(unsigned cpu, unsigned int entry, void *addr)
  140. {
  141. struct desc_struct *d = get_cpu_gdt_table(cpu);
  142. tss_desc tss;
  143. /*
  144. * sizeof(unsigned long) coming from an extra "long" at the end
  145. * of the iobitmap. See tss_struct definition in processor.h
  146. *
  147. * -1? seg base+limit should be pointing to the address of the
  148. * last valid byte
  149. */
  150. set_tssldt_descriptor(&tss, (unsigned long)addr, DESC_TSS,
  151. IO_BITMAP_OFFSET + IO_BITMAP_BYTES +
  152. sizeof(unsigned long) - 1);
  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_table(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_tr_desc(void)
  171. {
  172. asm volatile("ltr %w0"::"q" (GDT_ENTRY_TSS*8));
  173. }
  174. static inline void native_load_gdt(const struct desc_ptr *dtr)
  175. {
  176. asm volatile("lgdt %0"::"m" (*dtr));
  177. }
  178. static inline void native_load_idt(const struct desc_ptr *dtr)
  179. {
  180. asm volatile("lidt %0"::"m" (*dtr));
  181. }
  182. static inline void native_store_gdt(struct desc_ptr *dtr)
  183. {
  184. asm volatile("sgdt %0":"=m" (*dtr));
  185. }
  186. static inline void native_store_idt(struct desc_ptr *dtr)
  187. {
  188. asm volatile("sidt %0":"=m" (*dtr));
  189. }
  190. static inline unsigned long native_store_tr(void)
  191. {
  192. unsigned long tr;
  193. asm volatile("str %0":"=r" (tr));
  194. return tr;
  195. }
  196. static inline void native_load_tls(struct thread_struct *t, unsigned int cpu)
  197. {
  198. struct desc_struct *gdt = get_cpu_gdt_table(cpu);
  199. unsigned int i;
  200. for (i = 0; i < GDT_ENTRY_TLS_ENTRIES; i++)
  201. gdt[GDT_ENTRY_TLS_MIN + i] = t->tls_array[i];
  202. }
  203. /* This intentionally ignores lm, since 32-bit apps don't have that field. */
  204. #define LDT_empty(info) \
  205. ((info)->base_addr == 0 && \
  206. (info)->limit == 0 && \
  207. (info)->contents == 0 && \
  208. (info)->read_exec_only == 1 && \
  209. (info)->seg_32bit == 0 && \
  210. (info)->limit_in_pages == 0 && \
  211. (info)->seg_not_present == 1 && \
  212. (info)->useable == 0)
  213. /* Lots of programs expect an all-zero user_desc to mean "no segment at all". */
  214. static inline bool LDT_zero(const struct user_desc *info)
  215. {
  216. return (info->base_addr == 0 &&
  217. info->limit == 0 &&
  218. info->contents == 0 &&
  219. info->read_exec_only == 0 &&
  220. info->seg_32bit == 0 &&
  221. info->limit_in_pages == 0 &&
  222. info->seg_not_present == 0 &&
  223. info->useable == 0);
  224. }
  225. static inline void clear_LDT(void)
  226. {
  227. set_ldt(NULL, 0);
  228. }
  229. static inline unsigned long get_desc_base(const struct desc_struct *desc)
  230. {
  231. return (unsigned)(desc->base0 | ((desc->base1) << 16) | ((desc->base2) << 24));
  232. }
  233. static inline void set_desc_base(struct desc_struct *desc, unsigned long base)
  234. {
  235. desc->base0 = base & 0xffff;
  236. desc->base1 = (base >> 16) & 0xff;
  237. desc->base2 = (base >> 24) & 0xff;
  238. }
  239. static inline unsigned long get_desc_limit(const struct desc_struct *desc)
  240. {
  241. return desc->limit0 | (desc->limit << 16);
  242. }
  243. static inline void set_desc_limit(struct desc_struct *desc, unsigned long limit)
  244. {
  245. desc->limit0 = limit & 0xffff;
  246. desc->limit = (limit >> 16) & 0xf;
  247. }
  248. #ifdef CONFIG_X86_64
  249. static inline void set_nmi_gate(int gate, void *addr)
  250. {
  251. gate_desc s;
  252. pack_gate(&s, GATE_INTERRUPT, (unsigned long)addr, 0, 0, __KERNEL_CS);
  253. write_idt_entry(nmi_idt_table, gate, &s);
  254. }
  255. #endif
  256. static inline void _set_gate(int gate, unsigned type, void *addr,
  257. unsigned dpl, unsigned ist, unsigned seg)
  258. {
  259. gate_desc s;
  260. pack_gate(&s, type, (unsigned long)addr, dpl, ist, seg);
  261. /*
  262. * does not need to be atomic because it is only done once at
  263. * setup time
  264. */
  265. write_idt_entry(idt_table, gate, &s);
  266. }
  267. /*
  268. * This needs to use 'idt_table' rather than 'idt', and
  269. * thus use the _nonmapped_ version of the IDT, as the
  270. * Pentium F0 0F bugfix can have resulted in the mapped
  271. * IDT being write-protected.
  272. */
  273. static inline void set_intr_gate(unsigned int n, void *addr)
  274. {
  275. BUG_ON((unsigned)n > 0xFF);
  276. _set_gate(n, GATE_INTERRUPT, addr, 0, 0, __KERNEL_CS);
  277. }
  278. extern int first_system_vector;
  279. /* used_vectors is BITMAP for irq is not managed by percpu vector_irq */
  280. extern unsigned long used_vectors[];
  281. static inline void alloc_system_vector(int vector)
  282. {
  283. if (!test_bit(vector, used_vectors)) {
  284. set_bit(vector, used_vectors);
  285. if (first_system_vector > vector)
  286. first_system_vector = vector;
  287. } else {
  288. BUG();
  289. }
  290. }
  291. static inline void alloc_intr_gate(unsigned int n, void *addr)
  292. {
  293. alloc_system_vector(n);
  294. set_intr_gate(n, addr);
  295. }
  296. /*
  297. * This routine sets up an interrupt gate at directory privilege level 3.
  298. */
  299. static inline void set_system_intr_gate(unsigned int n, void *addr)
  300. {
  301. BUG_ON((unsigned)n > 0xFF);
  302. _set_gate(n, GATE_INTERRUPT, addr, 0x3, 0, __KERNEL_CS);
  303. }
  304. static inline void set_system_trap_gate(unsigned int n, void *addr)
  305. {
  306. BUG_ON((unsigned)n > 0xFF);
  307. _set_gate(n, GATE_TRAP, addr, 0x3, 0, __KERNEL_CS);
  308. }
  309. static inline void set_trap_gate(unsigned int n, void *addr)
  310. {
  311. BUG_ON((unsigned)n > 0xFF);
  312. _set_gate(n, GATE_TRAP, addr, 0, 0, __KERNEL_CS);
  313. }
  314. static inline void set_task_gate(unsigned int n, unsigned int gdt_entry)
  315. {
  316. BUG_ON((unsigned)n > 0xFF);
  317. _set_gate(n, GATE_TASK, (void *)0, 0, 0, (gdt_entry<<3));
  318. }
  319. static inline void set_intr_gate_ist(int n, void *addr, unsigned ist)
  320. {
  321. BUG_ON((unsigned)n > 0xFF);
  322. _set_gate(n, GATE_INTERRUPT, addr, 0, ist, __KERNEL_CS);
  323. }
  324. static inline void set_system_intr_gate_ist(int n, void *addr, unsigned ist)
  325. {
  326. BUG_ON((unsigned)n > 0xFF);
  327. _set_gate(n, GATE_INTERRUPT, addr, 0x3, ist, __KERNEL_CS);
  328. }
  329. #endif /* _ASM_X86_DESC_H */