desc_32.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. #ifndef __ARCH_DESC_H
  2. #define __ARCH_DESC_H
  3. #include <asm/ldt.h>
  4. #include <asm/segment.h>
  5. #include <asm/desc_defs.h>
  6. #ifndef __ASSEMBLY__
  7. #include <linux/preempt.h>
  8. #include <linux/smp.h>
  9. #include <linux/percpu.h>
  10. #include <asm/mmu.h>
  11. struct gdt_page
  12. {
  13. struct desc_struct gdt[GDT_ENTRIES];
  14. } __attribute__((aligned(PAGE_SIZE)));
  15. DECLARE_PER_CPU(struct gdt_page, gdt_page);
  16. static inline struct desc_struct *get_cpu_gdt_table(unsigned int cpu)
  17. {
  18. return per_cpu(gdt_page, cpu).gdt;
  19. }
  20. extern struct desc_ptr idt_descr;
  21. extern gate_desc idt_table[];
  22. extern void set_intr_gate(unsigned int irq, void * addr);
  23. static inline void pack_descriptor(struct desc_struct *desc,
  24. unsigned long base, unsigned long limit, unsigned char type, unsigned char flags)
  25. {
  26. desc->a = ((base & 0xffff) << 16) | (limit & 0xffff);
  27. desc->b = (base & 0xff000000) | ((base & 0xff0000) >> 16) |
  28. (limit & 0x000f0000) | ((type & 0xff) << 8) | ((flags & 0xf) << 20);
  29. desc->p = 1;
  30. }
  31. static inline void pack_gate(gate_desc *gate,
  32. unsigned long base, unsigned short seg, unsigned char type, unsigned char flags)
  33. {
  34. gate->a = (seg << 16) | (base & 0xffff);
  35. gate->b = (base & 0xffff0000) | ((type & 0xff) << 8) | (flags & 0xff);
  36. }
  37. #define DESCTYPE_LDT 0x82 /* present, system, DPL-0, LDT */
  38. #define DESCTYPE_TSS 0x89 /* present, system, DPL-0, 32-bit TSS */
  39. #define DESCTYPE_TASK 0x85 /* present, system, DPL-0, task gate */
  40. #define DESCTYPE_INT 0x8e /* present, system, DPL-0, interrupt gate */
  41. #define DESCTYPE_TRAP 0x8f /* present, system, DPL-0, trap gate */
  42. #define DESCTYPE_DPL3 0x60 /* DPL-3 */
  43. #define DESCTYPE_S 0x10 /* !system */
  44. #ifdef CONFIG_PARAVIRT
  45. #include <asm/paravirt.h>
  46. #else
  47. #define load_TR_desc() native_load_tr_desc()
  48. #define load_gdt(dtr) native_load_gdt(dtr)
  49. #define load_idt(dtr) native_load_idt(dtr)
  50. #define load_tr(tr) __asm__ __volatile("ltr %0"::"m" (tr))
  51. #define load_ldt(ldt) __asm__ __volatile("lldt %0"::"m" (ldt))
  52. #define store_gdt(dtr) native_store_gdt(dtr)
  53. #define store_idt(dtr) native_store_idt(dtr)
  54. #define store_tr(tr) (tr = native_store_tr())
  55. #define store_ldt(ldt) __asm__ ("sldt %0":"=m" (ldt))
  56. #define load_TLS(t, cpu) native_load_tls(t, cpu)
  57. #define set_ldt native_set_ldt
  58. #define write_ldt_entry(dt, entry, desc) \
  59. native_write_ldt_entry(dt, entry, desc)
  60. #define write_gdt_entry(dt, entry, desc, type) \
  61. native_write_gdt_entry(dt, entry, desc, type)
  62. #define write_idt_entry(dt, entry, g) native_write_idt_entry(dt, entry, g)
  63. #endif
  64. static inline void native_write_ldt_entry(struct desc_struct *ldt, int entry,
  65. const void *desc)
  66. {
  67. memcpy(&ldt[entry], desc, sizeof(struct desc_struct));
  68. }
  69. static inline void native_write_idt_entry(gate_desc *idt, int entry,
  70. const gate_desc *gate)
  71. {
  72. memcpy(&idt[entry], gate, sizeof(*gate));
  73. }
  74. static inline void native_write_gdt_entry(struct desc_struct *gdt, int entry,
  75. const void *desc, int type)
  76. {
  77. memcpy(&gdt[entry], desc, sizeof(struct desc_struct));
  78. }
  79. static inline void write_dt_entry(struct desc_struct *dt,
  80. int entry, u32 entry_low, u32 entry_high)
  81. {
  82. dt[entry].a = entry_low;
  83. dt[entry].b = entry_high;
  84. }
  85. static inline void native_set_ldt(const void *addr, unsigned int entries)
  86. {
  87. if (likely(entries == 0))
  88. __asm__ __volatile__("lldt %w0"::"q" (0));
  89. else {
  90. unsigned cpu = smp_processor_id();
  91. ldt_desc ldt;
  92. pack_descriptor(&ldt, (unsigned long)addr,
  93. entries * sizeof(struct desc_struct) - 1,
  94. DESC_LDT, 0);
  95. write_gdt_entry(get_cpu_gdt_table(cpu), GDT_ENTRY_LDT,
  96. &ldt, DESC_LDT);
  97. __asm__ __volatile__("lldt %w0"::"q" (GDT_ENTRY_LDT*8));
  98. }
  99. }
  100. static inline void native_load_tr_desc(void)
  101. {
  102. asm volatile("ltr %w0"::"q" (GDT_ENTRY_TSS*8));
  103. }
  104. static inline void native_load_gdt(const struct desc_ptr *dtr)
  105. {
  106. asm volatile("lgdt %0"::"m" (*dtr));
  107. }
  108. static inline void native_load_idt(const struct desc_ptr *dtr)
  109. {
  110. asm volatile("lidt %0"::"m" (*dtr));
  111. }
  112. static inline void native_store_gdt(struct desc_ptr *dtr)
  113. {
  114. asm ("sgdt %0":"=m" (*dtr));
  115. }
  116. static inline void native_store_idt(struct desc_ptr *dtr)
  117. {
  118. asm ("sidt %0":"=m" (*dtr));
  119. }
  120. static inline unsigned long native_store_tr(void)
  121. {
  122. unsigned long tr;
  123. asm ("str %0":"=r" (tr));
  124. return tr;
  125. }
  126. static inline void native_load_tls(struct thread_struct *t, unsigned int cpu)
  127. {
  128. unsigned int i;
  129. struct desc_struct *gdt = get_cpu_gdt_table(cpu);
  130. for (i = 0; i < GDT_ENTRY_TLS_ENTRIES; i++)
  131. gdt[GDT_ENTRY_TLS_MIN + i] = t->tls_array[i];
  132. }
  133. static inline void _set_gate(int gate, unsigned int type, void *addr, unsigned short seg)
  134. {
  135. gate_desc g;
  136. pack_gate(&g, (unsigned long)addr, seg, type, 0);
  137. write_idt_entry(idt_table, gate, &g);
  138. }
  139. static inline void __set_tss_desc(unsigned int cpu, unsigned int entry, const void *addr)
  140. {
  141. tss_desc tss;
  142. pack_descriptor(&tss, (unsigned long)addr,
  143. offsetof(struct tss_struct, __cacheline_filler) - 1,
  144. DESC_TSS, 0);
  145. write_gdt_entry(get_cpu_gdt_table(cpu), entry, &tss, DESC_TSS);
  146. }
  147. #define set_tss_desc(cpu,addr) __set_tss_desc(cpu, GDT_ENTRY_TSS, addr)
  148. #define LDT_empty(info) (\
  149. (info)->base_addr == 0 && \
  150. (info)->limit == 0 && \
  151. (info)->contents == 0 && \
  152. (info)->read_exec_only == 1 && \
  153. (info)->seg_32bit == 0 && \
  154. (info)->limit_in_pages == 0 && \
  155. (info)->seg_not_present == 1 && \
  156. (info)->useable == 0 )
  157. static inline void clear_LDT(void)
  158. {
  159. set_ldt(NULL, 0);
  160. }
  161. /*
  162. * load one particular LDT into the current CPU
  163. */
  164. static inline void load_LDT_nolock(mm_context_t *pc)
  165. {
  166. set_ldt(pc->ldt, pc->size);
  167. }
  168. static inline void load_LDT(mm_context_t *pc)
  169. {
  170. preempt_disable();
  171. load_LDT_nolock(pc);
  172. preempt_enable();
  173. }
  174. static inline unsigned long get_desc_base(unsigned long *desc)
  175. {
  176. unsigned long base;
  177. base = ((desc[0] >> 16) & 0x0000ffff) |
  178. ((desc[1] << 16) & 0x00ff0000) |
  179. (desc[1] & 0xff000000);
  180. return base;
  181. }
  182. #else /* __ASSEMBLY__ */
  183. /*
  184. * GET_DESC_BASE reads the descriptor base of the specified segment.
  185. *
  186. * Args:
  187. * idx - descriptor index
  188. * gdt - GDT pointer
  189. * base - 32bit register to which the base will be written
  190. * lo_w - lo word of the "base" register
  191. * lo_b - lo byte of the "base" register
  192. * hi_b - hi byte of the low word of the "base" register
  193. *
  194. * Example:
  195. * GET_DESC_BASE(GDT_ENTRY_ESPFIX_SS, %ebx, %eax, %ax, %al, %ah)
  196. * Will read the base address of GDT_ENTRY_ESPFIX_SS and put it into %eax.
  197. */
  198. #define GET_DESC_BASE(idx, gdt, base, lo_w, lo_b, hi_b) \
  199. movb idx*8+4(gdt), lo_b; \
  200. movb idx*8+7(gdt), hi_b; \
  201. shll $16, base; \
  202. movw idx*8+2(gdt), lo_w;
  203. #endif /* !__ASSEMBLY__ */
  204. #endif