desc.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. #else
  32. struct gdt_page {
  33. struct desc_struct gdt[GDT_ENTRIES];
  34. } __attribute__((aligned(PAGE_SIZE)));
  35. DECLARE_PER_CPU(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. #endif
  41. #ifdef CONFIG_PARAVIRT
  42. #include <asm/paravirt.h>
  43. #else
  44. #define load_TR_desc() native_load_tr_desc()
  45. #define load_gdt(dtr) native_load_gdt(dtr)
  46. #define load_idt(dtr) native_load_idt(dtr)
  47. #define load_tr(tr) __asm__ __volatile("ltr %0"::"m" (tr))
  48. #define load_ldt(ldt) __asm__ __volatile("lldt %0"::"m" (ldt))
  49. #define store_gdt(dtr) native_store_gdt(dtr)
  50. #define store_idt(dtr) native_store_idt(dtr)
  51. #define store_tr(tr) (tr = native_store_tr())
  52. #define store_ldt(ldt) __asm__ ("sldt %0":"=m" (ldt))
  53. #define load_TLS(t, cpu) native_load_tls(t, cpu)
  54. #define set_ldt native_set_ldt
  55. #define write_ldt_entry(dt, entry, desc) \
  56. native_write_ldt_entry(dt, entry, desc)
  57. #define write_gdt_entry(dt, entry, desc, type) \
  58. native_write_gdt_entry(dt, entry, desc, type)
  59. #define write_idt_entry(dt, entry, g) native_write_idt_entry(dt, entry, g)
  60. #endif
  61. static inline void native_write_idt_entry(gate_desc *idt, int entry,
  62. const gate_desc *gate)
  63. {
  64. memcpy(&idt[entry], gate, sizeof(*gate));
  65. }
  66. static inline void native_write_ldt_entry(struct desc_struct *ldt, int entry,
  67. const void *desc)
  68. {
  69. memcpy(&ldt[entry], desc, 8);
  70. }
  71. static inline void native_write_gdt_entry(struct desc_struct *gdt, int entry,
  72. const void *desc, int type)
  73. {
  74. unsigned int size;
  75. switch (type) {
  76. case DESC_TSS:
  77. size = sizeof(tss_desc);
  78. break;
  79. case DESC_LDT:
  80. size = sizeof(ldt_desc);
  81. break;
  82. default:
  83. size = sizeof(struct desc_struct);
  84. break;
  85. }
  86. memcpy(&gdt[entry], desc, size);
  87. }
  88. static inline void set_tssldt_descriptor(struct ldttss_desc64 *d,
  89. unsigned long tss, unsigned type,
  90. unsigned size)
  91. {
  92. memset(d, 0, sizeof(*d));
  93. d->limit0 = size & 0xFFFF;
  94. d->base0 = PTR_LOW(tss);
  95. d->base1 = PTR_MIDDLE(tss) & 0xFF;
  96. d->type = type;
  97. d->p = 1;
  98. d->limit1 = (size >> 16) & 0xF;
  99. d->base2 = (PTR_MIDDLE(tss) >> 8) & 0xFF;
  100. d->base3 = PTR_HIGH(tss);
  101. }
  102. static inline void pack_descriptor(struct desc_struct *desc, unsigned long base,
  103. unsigned long limit, unsigned char type,
  104. unsigned char flags)
  105. {
  106. desc->a = ((base & 0xffff) << 16) | (limit & 0xffff);
  107. desc->b = (base & 0xff000000) | ((base & 0xff0000) >> 16) |
  108. (limit & 0x000f0000) | ((type & 0xff) << 8) |
  109. ((flags & 0xf) << 20);
  110. desc->p = 1;
  111. }
  112. static inline void pack_ldt(ldt_desc *ldt, unsigned long addr,
  113. unsigned size)
  114. {
  115. #ifdef CONFIG_X86_64
  116. set_tssldt_descriptor(ldt,
  117. addr, DESC_LDT, size);
  118. #else
  119. pack_descriptor(ldt, (unsigned long)addr,
  120. size,
  121. 0x80 | DESC_LDT, 0);
  122. #endif
  123. }
  124. static inline void native_set_ldt(const void *addr, unsigned int entries)
  125. {
  126. if (likely(entries == 0))
  127. __asm__ __volatile__("lldt %w0"::"q" (0));
  128. else {
  129. unsigned cpu = smp_processor_id();
  130. ldt_desc ldt;
  131. pack_ldt(&ldt, (unsigned long)addr,
  132. entries * sizeof(ldt) - 1);
  133. write_gdt_entry(get_cpu_gdt_table(cpu), GDT_ENTRY_LDT,
  134. &ldt, DESC_LDT);
  135. __asm__ __volatile__("lldt %w0"::"q" (GDT_ENTRY_LDT*8));
  136. }
  137. }
  138. static inline void native_load_tr_desc(void)
  139. {
  140. asm volatile("ltr %w0"::"q" (GDT_ENTRY_TSS*8));
  141. }
  142. static inline void native_load_gdt(const struct desc_ptr *dtr)
  143. {
  144. asm volatile("lgdt %0"::"m" (*dtr));
  145. }
  146. static inline void native_load_idt(const struct desc_ptr *dtr)
  147. {
  148. asm volatile("lidt %0"::"m" (*dtr));
  149. }
  150. static inline void native_store_gdt(struct desc_ptr *dtr)
  151. {
  152. asm volatile("sgdt %0":"=m" (*dtr));
  153. }
  154. static inline void native_store_idt(struct desc_ptr *dtr)
  155. {
  156. asm volatile("sidt %0":"=m" (*dtr));
  157. }
  158. static inline unsigned long native_store_tr(void)
  159. {
  160. unsigned long tr;
  161. asm volatile("str %0":"=r" (tr));
  162. return tr;
  163. }
  164. static inline void native_load_tls(struct thread_struct *t, unsigned int cpu)
  165. {
  166. unsigned int i;
  167. struct desc_struct *gdt = get_cpu_gdt_table(cpu);
  168. for (i = 0; i < GDT_ENTRY_TLS_ENTRIES; i++)
  169. gdt[GDT_ENTRY_TLS_MIN + i] = t->tls_array[i];
  170. }
  171. #ifdef CONFIG_X86_32
  172. # include "desc_32.h"
  173. #else
  174. # include "desc_64.h"
  175. #endif
  176. #define _LDT_empty(info) (\
  177. (info)->base_addr == 0 && \
  178. (info)->limit == 0 && \
  179. (info)->contents == 0 && \
  180. (info)->read_exec_only == 1 && \
  181. (info)->seg_32bit == 0 && \
  182. (info)->limit_in_pages == 0 && \
  183. (info)->seg_not_present == 1 && \
  184. (info)->useable == 0)
  185. #ifdef CONFIG_X86_64
  186. #define LDT_empty(info) (_LDT_empty(info) && ((info)->lm == 0))
  187. #else
  188. #define LDT_empty(info) (_LDT_empty(info))
  189. #endif
  190. static inline void clear_LDT(void)
  191. {
  192. set_ldt(NULL, 0);
  193. }
  194. /*
  195. * load one particular LDT into the current CPU
  196. */
  197. static inline void load_LDT_nolock(mm_context_t *pc)
  198. {
  199. set_ldt(pc->ldt, pc->size);
  200. }
  201. static inline void load_LDT(mm_context_t *pc)
  202. {
  203. preempt_disable();
  204. load_LDT_nolock(pc);
  205. preempt_enable();
  206. }
  207. static inline unsigned long get_desc_base(struct desc_struct *desc)
  208. {
  209. return desc->base0 | ((desc->base1) << 16) | ((desc->base2) << 24);
  210. }
  211. #else
  212. /*
  213. * GET_DESC_BASE reads the descriptor base of the specified segment.
  214. *
  215. * Args:
  216. * idx - descriptor index
  217. * gdt - GDT pointer
  218. * base - 32bit register to which the base will be written
  219. * lo_w - lo word of the "base" register
  220. * lo_b - lo byte of the "base" register
  221. * hi_b - hi byte of the low word of the "base" register
  222. *
  223. * Example:
  224. * GET_DESC_BASE(GDT_ENTRY_ESPFIX_SS, %ebx, %eax, %ax, %al, %ah)
  225. * Will read the base address of GDT_ENTRY_ESPFIX_SS and put it into %eax.
  226. */
  227. #define GET_DESC_BASE(idx, gdt, base, lo_w, lo_b, hi_b) \
  228. movb idx*8+4(gdt), lo_b; \
  229. movb idx*8+7(gdt), hi_b; \
  230. shll $16, base; \
  231. movw idx*8+2(gdt), lo_w;
  232. #endif /* __ASSEMBLY__ */
  233. #endif