desc.h 9.7 KB

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