desc.h 9.9 KB

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