desc.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. #ifndef ASM_X86__DESC_H
  2. #define ASM_X86__DESC_H
  3. #ifndef __ASSEMBLY__
  4. #include <asm/desc_defs.h>
  5. #include <asm/ldt.h>
  6. #include <asm/mmu.h>
  7. #include <linux/smp.h>
  8. static inline void fill_ldt(struct desc_struct *desc,
  9. const struct user_desc *info)
  10. {
  11. desc->limit0 = info->limit & 0x0ffff;
  12. desc->base0 = info->base_addr & 0x0000ffff;
  13. desc->base1 = (info->base_addr & 0x00ff0000) >> 16;
  14. desc->type = (info->read_exec_only ^ 1) << 1;
  15. desc->type |= info->contents << 2;
  16. desc->s = 1;
  17. desc->dpl = 0x3;
  18. desc->p = info->seg_not_present ^ 1;
  19. desc->limit = (info->limit & 0xf0000) >> 16;
  20. desc->avl = info->useable;
  21. desc->d = info->seg_32bit;
  22. desc->g = info->limit_in_pages;
  23. desc->base2 = (info->base_addr & 0xff000000) >> 24;
  24. }
  25. extern struct desc_ptr idt_descr;
  26. extern gate_desc idt_table[];
  27. struct gdt_page {
  28. struct desc_struct gdt[GDT_ENTRIES];
  29. } __attribute__((aligned(PAGE_SIZE)));
  30. DECLARE_PER_CPU(struct gdt_page, gdt_page);
  31. static inline struct desc_struct *get_cpu_gdt_table(unsigned int cpu)
  32. {
  33. return per_cpu(gdt_page, cpu).gdt;
  34. }
  35. #ifdef CONFIG_X86_64
  36. static inline void pack_gate(gate_desc *gate, unsigned type, unsigned long func,
  37. unsigned dpl, unsigned ist, unsigned seg)
  38. {
  39. gate->offset_low = PTR_LOW(func);
  40. gate->segment = __KERNEL_CS;
  41. gate->ist = ist;
  42. gate->p = 1;
  43. gate->dpl = dpl;
  44. gate->zero0 = 0;
  45. gate->zero1 = 0;
  46. gate->type = type;
  47. gate->offset_middle = PTR_MIDDLE(func);
  48. gate->offset_high = PTR_HIGH(func);
  49. }
  50. #else
  51. static inline void pack_gate(gate_desc *gate, unsigned char type,
  52. unsigned long base, unsigned dpl, unsigned flags,
  53. unsigned short seg)
  54. {
  55. gate->a = (seg << 16) | (base & 0xffff);
  56. gate->b = (base & 0xffff0000) |
  57. (((0x80 | type | (dpl << 5)) & 0xff) << 8);
  58. }
  59. #endif
  60. static inline int desc_empty(const void *ptr)
  61. {
  62. const u32 *desc = ptr;
  63. return !(desc[0] | desc[1]);
  64. }
  65. #ifdef CONFIG_PARAVIRT
  66. #include <asm/paravirt.h>
  67. #else
  68. #define load_TR_desc() native_load_tr_desc()
  69. #define load_gdt(dtr) native_load_gdt(dtr)
  70. #define load_idt(dtr) native_load_idt(dtr)
  71. #define load_tr(tr) asm volatile("ltr %0"::"m" (tr))
  72. #define load_ldt(ldt) asm volatile("lldt %0"::"m" (ldt))
  73. #define store_gdt(dtr) native_store_gdt(dtr)
  74. #define store_idt(dtr) native_store_idt(dtr)
  75. #define store_tr(tr) (tr = native_store_tr())
  76. #define store_ldt(ldt) asm("sldt %0":"=m" (ldt))
  77. #define load_TLS(t, cpu) native_load_tls(t, cpu)
  78. #define set_ldt native_set_ldt
  79. #define write_ldt_entry(dt, entry, desc) \
  80. native_write_ldt_entry(dt, entry, desc)
  81. #define write_gdt_entry(dt, entry, desc, type) \
  82. native_write_gdt_entry(dt, entry, desc, type)
  83. #define write_idt_entry(dt, entry, g) \
  84. native_write_idt_entry(dt, entry, g)
  85. #endif
  86. static inline void native_write_idt_entry(gate_desc *idt, int entry,
  87. const gate_desc *gate)
  88. {
  89. memcpy(&idt[entry], gate, sizeof(*gate));
  90. }
  91. static inline void native_write_ldt_entry(struct desc_struct *ldt, int entry,
  92. const void *desc)
  93. {
  94. memcpy(&ldt[entry], desc, 8);
  95. }
  96. static inline void native_write_gdt_entry(struct desc_struct *gdt, int entry,
  97. const void *desc, int type)
  98. {
  99. unsigned int size;
  100. switch (type) {
  101. case DESC_TSS:
  102. size = sizeof(tss_desc);
  103. break;
  104. case DESC_LDT:
  105. size = sizeof(ldt_desc);
  106. break;
  107. default:
  108. size = sizeof(struct desc_struct);
  109. 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,
  124. unsigned type, unsigned size)
  125. {
  126. #ifdef CONFIG_X86_64
  127. struct ldttss_desc64 *desc = d;
  128. memset(desc, 0, sizeof(*desc));
  129. desc->limit0 = size & 0xFFFF;
  130. desc->base0 = PTR_LOW(addr);
  131. desc->base1 = PTR_MIDDLE(addr) & 0xFF;
  132. desc->type = type;
  133. desc->p = 1;
  134. desc->limit1 = (size >> 16) & 0xF;
  135. desc->base2 = (PTR_MIDDLE(addr) >> 8) & 0xFF;
  136. desc->base3 = PTR_HIGH(addr);
  137. #else
  138. pack_descriptor((struct desc_struct *)d, addr, size, 0x80 | type, 0);
  139. #endif
  140. }
  141. static inline void __set_tss_desc(unsigned cpu, unsigned int entry, void *addr)
  142. {
  143. struct desc_struct *d = get_cpu_gdt_table(cpu);
  144. tss_desc tss;
  145. /*
  146. * sizeof(unsigned long) coming from an extra "long" at the end
  147. * of the iobitmap. See tss_struct definition in processor.h
  148. *
  149. * -1? seg base+limit should be pointing to the address of the
  150. * last valid byte
  151. */
  152. set_tssldt_descriptor(&tss, (unsigned long)addr, DESC_TSS,
  153. IO_BITMAP_OFFSET + IO_BITMAP_BYTES +
  154. sizeof(unsigned long) - 1);
  155. write_gdt_entry(d, entry, &tss, DESC_TSS);
  156. }
  157. #define set_tss_desc(cpu, addr) __set_tss_desc(cpu, GDT_ENTRY_TSS, addr)
  158. static inline void native_set_ldt(const void *addr, unsigned int entries)
  159. {
  160. if (likely(entries == 0))
  161. asm volatile("lldt %w0"::"q" (0));
  162. else {
  163. unsigned cpu = smp_processor_id();
  164. ldt_desc ldt;
  165. set_tssldt_descriptor(&ldt, (unsigned long)addr, DESC_LDT,
  166. entries * LDT_ENTRY_SIZE - 1);
  167. write_gdt_entry(get_cpu_gdt_table(cpu), GDT_ENTRY_LDT,
  168. &ldt, DESC_LDT);
  169. asm volatile("lldt %w0"::"q" (GDT_ENTRY_LDT*8));
  170. }
  171. }
  172. static inline void native_load_tr_desc(void)
  173. {
  174. asm volatile("ltr %w0"::"q" (GDT_ENTRY_TSS*8));
  175. }
  176. static inline void native_load_gdt(const struct desc_ptr *dtr)
  177. {
  178. asm volatile("lgdt %0"::"m" (*dtr));
  179. }
  180. static inline void native_load_idt(const struct desc_ptr *dtr)
  181. {
  182. asm volatile("lidt %0"::"m" (*dtr));
  183. }
  184. static inline void native_store_gdt(struct desc_ptr *dtr)
  185. {
  186. asm volatile("sgdt %0":"=m" (*dtr));
  187. }
  188. static inline void native_store_idt(struct desc_ptr *dtr)
  189. {
  190. asm volatile("sidt %0":"=m" (*dtr));
  191. }
  192. static inline unsigned long native_store_tr(void)
  193. {
  194. unsigned long tr;
  195. asm volatile("str %0":"=r" (tr));
  196. return tr;
  197. }
  198. static inline void native_load_tls(struct thread_struct *t, unsigned int cpu)
  199. {
  200. unsigned int i;
  201. struct desc_struct *gdt = get_cpu_gdt_table(cpu);
  202. for (i = 0; i < GDT_ENTRY_TLS_ENTRIES; i++)
  203. gdt[GDT_ENTRY_TLS_MIN + i] = t->tls_array[i];
  204. }
  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. #ifdef CONFIG_X86_64
  215. #define LDT_empty(info) (_LDT_empty(info) && ((info)->lm == 0))
  216. #else
  217. #define LDT_empty(info) (_LDT_empty(info))
  218. #endif
  219. static inline void clear_LDT(void)
  220. {
  221. set_ldt(NULL, 0);
  222. }
  223. /*
  224. * load one particular LDT into the current CPU
  225. */
  226. static inline void load_LDT_nolock(mm_context_t *pc)
  227. {
  228. set_ldt(pc->ldt, pc->size);
  229. }
  230. static inline void load_LDT(mm_context_t *pc)
  231. {
  232. preempt_disable();
  233. load_LDT_nolock(pc);
  234. preempt_enable();
  235. }
  236. static inline unsigned long get_desc_base(const struct desc_struct *desc)
  237. {
  238. return desc->base0 | ((desc->base1) << 16) | ((desc->base2) << 24);
  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_gate(int gate, unsigned type, void *addr,
  245. unsigned dpl, unsigned ist, unsigned seg)
  246. {
  247. gate_desc s;
  248. pack_gate(&s, type, (unsigned long)addr, dpl, ist, seg);
  249. /*
  250. * does not need to be atomic because it is only done once at
  251. * setup time
  252. */
  253. write_idt_entry(idt_table, gate, &s);
  254. }
  255. /*
  256. * This needs to use 'idt_table' rather than 'idt', and
  257. * thus use the _nonmapped_ version of the IDT, as the
  258. * Pentium F0 0F bugfix can have resulted in the mapped
  259. * IDT being write-protected.
  260. */
  261. static inline void set_intr_gate(unsigned int n, void *addr)
  262. {
  263. BUG_ON((unsigned)n > 0xFF);
  264. _set_gate(n, GATE_INTERRUPT, addr, 0, 0, __KERNEL_CS);
  265. }
  266. #define SYS_VECTOR_FREE 0
  267. #define SYS_VECTOR_ALLOCED 1
  268. extern int first_system_vector;
  269. extern char system_vectors[];
  270. static inline void alloc_system_vector(int vector)
  271. {
  272. if (system_vectors[vector] == SYS_VECTOR_FREE) {
  273. system_vectors[vector] = SYS_VECTOR_ALLOCED;
  274. if (first_system_vector > vector)
  275. first_system_vector = vector;
  276. } else
  277. BUG();
  278. }
  279. static inline void alloc_intr_gate(unsigned int n, void *addr)
  280. {
  281. alloc_system_vector(n);
  282. set_intr_gate(n, addr);
  283. }
  284. /*
  285. * This routine sets up an interrupt gate at directory privilege level 3.
  286. */
  287. static inline void set_system_intr_gate(unsigned int n, void *addr)
  288. {
  289. BUG_ON((unsigned)n > 0xFF);
  290. _set_gate(n, GATE_INTERRUPT, addr, 0x3, 0, __KERNEL_CS);
  291. }
  292. static inline void set_trap_gate(unsigned int n, void *addr)
  293. {
  294. BUG_ON((unsigned)n > 0xFF);
  295. _set_gate(n, GATE_TRAP, addr, 0, 0, __KERNEL_CS);
  296. }
  297. static inline void set_system_gate(unsigned int n, void *addr)
  298. {
  299. BUG_ON((unsigned)n > 0xFF);
  300. #ifdef CONFIG_X86_32
  301. _set_gate(n, GATE_TRAP, addr, 0x3, 0, __KERNEL_CS);
  302. #else
  303. _set_gate(n, GATE_INTERRUPT, addr, 0x3, 0, __KERNEL_CS);
  304. #endif
  305. }
  306. static inline void set_task_gate(unsigned int n, unsigned int gdt_entry)
  307. {
  308. BUG_ON((unsigned)n > 0xFF);
  309. _set_gate(n, GATE_TASK, (void *)0, 0, 0, (gdt_entry<<3));
  310. }
  311. static inline void set_intr_gate_ist(int n, void *addr, unsigned ist)
  312. {
  313. BUG_ON((unsigned)n > 0xFF);
  314. _set_gate(n, GATE_INTERRUPT, addr, 0, ist, __KERNEL_CS);
  315. }
  316. static inline void set_system_gate_ist(int n, void *addr, unsigned ist)
  317. {
  318. BUG_ON((unsigned)n > 0xFF);
  319. _set_gate(n, GATE_INTERRUPT, addr, 0x3, ist, __KERNEL_CS);
  320. }
  321. #else
  322. /*
  323. * GET_DESC_BASE reads the descriptor base of the specified segment.
  324. *
  325. * Args:
  326. * idx - descriptor index
  327. * gdt - GDT pointer
  328. * base - 32bit register to which the base will be written
  329. * lo_w - lo word of the "base" register
  330. * lo_b - lo byte of the "base" register
  331. * hi_b - hi byte of the low word of the "base" register
  332. *
  333. * Example:
  334. * GET_DESC_BASE(GDT_ENTRY_ESPFIX_SS, %ebx, %eax, %ax, %al, %ah)
  335. * Will read the base address of GDT_ENTRY_ESPFIX_SS and put it into %eax.
  336. */
  337. #define GET_DESC_BASE(idx, gdt, base, lo_w, lo_b, hi_b) \
  338. movb idx * 8 + 4(gdt), lo_b; \
  339. movb idx * 8 + 7(gdt), hi_b; \
  340. shll $16, base; \
  341. movw idx * 8 + 2(gdt), lo_w;
  342. #endif /* __ASSEMBLY__ */
  343. #endif /* ASM_X86__DESC_H */