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