desc.h 9.0 KB

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