desc.h 6.0 KB

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