desc.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. extern struct desc_struct cpu_gdt_table[GDT_ENTRIES];
  11. struct Xgt_desc_struct {
  12. unsigned short size;
  13. unsigned long address __attribute__((packed));
  14. unsigned short pad;
  15. } __attribute__ ((packed));
  16. extern struct Xgt_desc_struct idt_descr;
  17. DECLARE_PER_CPU(struct Xgt_desc_struct, cpu_gdt_descr);
  18. static inline struct desc_struct *get_cpu_gdt_table(unsigned int cpu)
  19. {
  20. return (struct desc_struct *)per_cpu(cpu_gdt_descr, cpu).address;
  21. }
  22. extern struct desc_struct idt_table[];
  23. extern void set_intr_gate(unsigned int irq, void * addr);
  24. static inline void pack_descriptor(__u32 *a, __u32 *b,
  25. unsigned long base, unsigned long limit, unsigned char type, unsigned char flags)
  26. {
  27. *a = ((base & 0xffff) << 16) | (limit & 0xffff);
  28. *b = (base & 0xff000000) | ((base & 0xff0000) >> 16) |
  29. (limit & 0x000f0000) | ((type & 0xff) << 8) | ((flags & 0xf) << 20);
  30. }
  31. static inline void pack_gate(__u32 *a, __u32 *b,
  32. unsigned long base, unsigned short seg, unsigned char type, unsigned char flags)
  33. {
  34. *a = (seg << 16) | (base & 0xffff);
  35. *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() __asm__ __volatile__("ltr %w0"::"q" (GDT_ENTRY_TSS*8))
  48. #define load_gdt(dtr) __asm__ __volatile("lgdt %0"::"m" (*dtr))
  49. #define load_idt(dtr) __asm__ __volatile("lidt %0"::"m" (*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) __asm__ ("sgdt %0":"=m" (*dtr))
  53. #define store_idt(dtr) __asm__ ("sidt %0":"=m" (*dtr))
  54. #define store_tr(tr) __asm__ ("str %0":"=m" (tr))
  55. #define store_ldt(ldt) __asm__ ("sldt %0":"=m" (ldt))
  56. #if TLS_SIZE != 24
  57. # error update this code.
  58. #endif
  59. static inline void load_TLS(struct thread_struct *t, unsigned int cpu)
  60. {
  61. #define C(i) get_cpu_gdt_table(cpu)[GDT_ENTRY_TLS_MIN + i] = t->tls_array[i]
  62. C(0); C(1); C(2);
  63. #undef C
  64. }
  65. #define write_ldt_entry(dt, entry, a, b) write_dt_entry(dt, entry, a, b)
  66. #define write_gdt_entry(dt, entry, a, b) write_dt_entry(dt, entry, a, b)
  67. #define write_idt_entry(dt, entry, a, b) write_dt_entry(dt, entry, a, b)
  68. static inline void write_dt_entry(void *dt, int entry, __u32 entry_a, __u32 entry_b)
  69. {
  70. __u32 *lp = (__u32 *)((char *)dt + entry*8);
  71. *lp = entry_a;
  72. *(lp+1) = entry_b;
  73. }
  74. #define set_ldt native_set_ldt
  75. #endif /* CONFIG_PARAVIRT */
  76. static inline fastcall void native_set_ldt(const void *addr,
  77. unsigned int entries)
  78. {
  79. if (likely(entries == 0))
  80. __asm__ __volatile__("lldt %w0"::"q" (0));
  81. else {
  82. unsigned cpu = smp_processor_id();
  83. __u32 a, b;
  84. pack_descriptor(&a, &b, (unsigned long)addr,
  85. entries * sizeof(struct desc_struct) - 1,
  86. DESCTYPE_LDT, 0);
  87. write_gdt_entry(get_cpu_gdt_table(cpu), GDT_ENTRY_LDT, a, b);
  88. __asm__ __volatile__("lldt %w0"::"q" (GDT_ENTRY_LDT*8));
  89. }
  90. }
  91. static inline void _set_gate(int gate, unsigned int type, void *addr, unsigned short seg)
  92. {
  93. __u32 a, b;
  94. pack_gate(&a, &b, (unsigned long)addr, seg, type, 0);
  95. write_idt_entry(idt_table, gate, a, b);
  96. }
  97. static inline void __set_tss_desc(unsigned int cpu, unsigned int entry, const void *addr)
  98. {
  99. __u32 a, b;
  100. pack_descriptor(&a, &b, (unsigned long)addr,
  101. offsetof(struct tss_struct, __cacheline_filler) - 1,
  102. DESCTYPE_TSS, 0);
  103. write_gdt_entry(get_cpu_gdt_table(cpu), entry, a, b);
  104. }
  105. #define set_tss_desc(cpu,addr) __set_tss_desc(cpu, GDT_ENTRY_TSS, addr)
  106. #define LDT_entry_a(info) \
  107. ((((info)->base_addr & 0x0000ffff) << 16) | ((info)->limit & 0x0ffff))
  108. #define LDT_entry_b(info) \
  109. (((info)->base_addr & 0xff000000) | \
  110. (((info)->base_addr & 0x00ff0000) >> 16) | \
  111. ((info)->limit & 0xf0000) | \
  112. (((info)->read_exec_only ^ 1) << 9) | \
  113. ((info)->contents << 10) | \
  114. (((info)->seg_not_present ^ 1) << 15) | \
  115. ((info)->seg_32bit << 22) | \
  116. ((info)->limit_in_pages << 23) | \
  117. ((info)->useable << 20) | \
  118. 0x7000)
  119. #define LDT_empty(info) (\
  120. (info)->base_addr == 0 && \
  121. (info)->limit == 0 && \
  122. (info)->contents == 0 && \
  123. (info)->read_exec_only == 1 && \
  124. (info)->seg_32bit == 0 && \
  125. (info)->limit_in_pages == 0 && \
  126. (info)->seg_not_present == 1 && \
  127. (info)->useable == 0 )
  128. static inline void clear_LDT(void)
  129. {
  130. set_ldt(NULL, 0);
  131. }
  132. /*
  133. * load one particular LDT into the current CPU
  134. */
  135. static inline void load_LDT_nolock(mm_context_t *pc)
  136. {
  137. set_ldt(pc->ldt, pc->size);
  138. }
  139. static inline void load_LDT(mm_context_t *pc)
  140. {
  141. preempt_disable();
  142. load_LDT_nolock(pc);
  143. preempt_enable();
  144. }
  145. static inline unsigned long get_desc_base(unsigned long *desc)
  146. {
  147. unsigned long base;
  148. base = ((desc[0] >> 16) & 0x0000ffff) |
  149. ((desc[1] << 16) & 0x00ff0000) |
  150. (desc[1] & 0xff000000);
  151. return base;
  152. }
  153. #else /* __ASSEMBLY__ */
  154. /*
  155. * GET_DESC_BASE reads the descriptor base of the specified segment.
  156. *
  157. * Args:
  158. * idx - descriptor index
  159. * gdt - GDT pointer
  160. * base - 32bit register to which the base will be written
  161. * lo_w - lo word of the "base" register
  162. * lo_b - lo byte of the "base" register
  163. * hi_b - hi byte of the low word of the "base" register
  164. *
  165. * Example:
  166. * GET_DESC_BASE(GDT_ENTRY_ESPFIX_SS, %ebx, %eax, %ax, %al, %ah)
  167. * Will read the base address of GDT_ENTRY_ESPFIX_SS and put it into %eax.
  168. */
  169. #define GET_DESC_BASE(idx, gdt, base, lo_w, lo_b, hi_b) \
  170. movb idx*8+4(gdt), lo_b; \
  171. movb idx*8+7(gdt), hi_b; \
  172. shll $16, base; \
  173. movw idx*8+2(gdt), lo_w;
  174. #endif /* !__ASSEMBLY__ */
  175. #endif