desc_32.h 6.3 KB

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