desc_32.h 6.4 KB

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