desc_64.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /* Written 2000 by Andi Kleen */
  2. #ifndef __ARCH_DESC_H
  3. #define __ARCH_DESC_H
  4. #include <linux/threads.h>
  5. #include <asm/ldt.h>
  6. #ifndef __ASSEMBLY__
  7. #include <linux/string.h>
  8. #include <linux/smp.h>
  9. #include <asm/desc_defs.h>
  10. #include <asm/segment.h>
  11. #include <asm/mmu.h>
  12. extern struct desc_struct cpu_gdt_table[GDT_ENTRIES];
  13. #define load_TR_desc() asm volatile("ltr %w0"::"r" (GDT_ENTRY_TSS*8))
  14. #define load_LDT_desc() asm volatile("lldt %w0"::"r" (GDT_ENTRY_LDT*8))
  15. #define clear_LDT() asm volatile("lldt %w0"::"r" (0))
  16. static inline unsigned long __store_tr(void)
  17. {
  18. unsigned long tr;
  19. asm volatile ("str %w0":"=r" (tr));
  20. return tr;
  21. }
  22. #define store_tr(tr) (tr) = __store_tr()
  23. extern gate_desc idt_table[];
  24. extern struct desc_ptr cpu_gdt_descr[];
  25. static inline void write_ldt_entry(struct desc_struct *ldt,
  26. int entry, u32 entry_low, u32 entry_high)
  27. {
  28. __u32 *lp = (__u32 *)((entry << 3) + (char *)ldt);
  29. lp[0] = entry_low;
  30. lp[1] = entry_high;
  31. }
  32. /* the cpu gdt accessor */
  33. #define get_cpu_gdt_table(x) ((struct desc_struct *)cpu_gdt_descr[x].address)
  34. static inline void load_gdt(const struct desc_ptr *ptr)
  35. {
  36. asm volatile("lgdt %w0"::"m" (*ptr));
  37. }
  38. static inline void store_gdt(struct desc_ptr *ptr)
  39. {
  40. asm("sgdt %w0":"=m" (*ptr));
  41. }
  42. static inline void _set_gate(void *adr, unsigned type, unsigned long func,
  43. unsigned dpl, unsigned ist)
  44. {
  45. gate_desc s;
  46. s.offset_low = PTR_LOW(func);
  47. s.segment = __KERNEL_CS;
  48. s.ist = ist;
  49. s.p = 1;
  50. s.dpl = dpl;
  51. s.zero0 = 0;
  52. s.zero1 = 0;
  53. s.type = type;
  54. s.offset_middle = PTR_MIDDLE(func);
  55. s.offset_high = PTR_HIGH(func);
  56. /*
  57. * does not need to be atomic because it is only done once at
  58. * setup time
  59. */
  60. memcpy(adr, &s, 16);
  61. }
  62. static inline void set_intr_gate(int nr, void *func)
  63. {
  64. BUG_ON((unsigned)nr > 0xFF);
  65. _set_gate(&idt_table[nr], GATE_INTERRUPT, (unsigned long) func, 0, 0);
  66. }
  67. static inline void set_intr_gate_ist(int nr, void *func, unsigned ist)
  68. {
  69. BUG_ON((unsigned)nr > 0xFF);
  70. _set_gate(&idt_table[nr], GATE_INTERRUPT, (unsigned long) func, 0, ist);
  71. }
  72. static inline void set_system_gate(int nr, void *func)
  73. {
  74. BUG_ON((unsigned)nr > 0xFF);
  75. _set_gate(&idt_table[nr], GATE_INTERRUPT, (unsigned long) func, 3, 0);
  76. }
  77. static inline void set_system_gate_ist(int nr, void *func, unsigned ist)
  78. {
  79. _set_gate(&idt_table[nr], GATE_INTERRUPT, (unsigned long) func, 3, ist);
  80. }
  81. static inline void load_idt(const struct desc_ptr *ptr)
  82. {
  83. asm volatile("lidt %w0"::"m" (*ptr));
  84. }
  85. static inline void store_idt(struct desc_ptr *dtr)
  86. {
  87. asm("sidt %w0":"=m" (*dtr));
  88. }
  89. static inline void set_tssldt_descriptor(void *ptr, unsigned long tss,
  90. unsigned type, unsigned size)
  91. {
  92. struct ldttss_desc d;
  93. memset(&d, 0, sizeof(d));
  94. d.limit0 = size & 0xFFFF;
  95. d.base0 = PTR_LOW(tss);
  96. d.base1 = PTR_MIDDLE(tss) & 0xFF;
  97. d.type = type;
  98. d.p = 1;
  99. d.limit1 = (size >> 16) & 0xF;
  100. d.base2 = (PTR_MIDDLE(tss) >> 8) & 0xFF;
  101. d.base3 = PTR_HIGH(tss);
  102. memcpy(ptr, &d, 16);
  103. }
  104. static inline void set_tss_desc(unsigned cpu, void *addr)
  105. {
  106. /*
  107. * sizeof(unsigned long) coming from an extra "long" at the end
  108. * of the iobitmap. See tss_struct definition in processor.h
  109. *
  110. * -1? seg base+limit should be pointing to the address of the
  111. * last valid byte
  112. */
  113. set_tssldt_descriptor(&get_cpu_gdt_table(cpu)[GDT_ENTRY_TSS],
  114. (unsigned long)addr, DESC_TSS,
  115. IO_BITMAP_OFFSET + IO_BITMAP_BYTES + sizeof(unsigned long) - 1);
  116. }
  117. static inline void set_ldt_desc(unsigned cpu, void *addr, int size)
  118. {
  119. set_tssldt_descriptor(&get_cpu_gdt_table(cpu)[GDT_ENTRY_LDT],
  120. (unsigned long)addr, DESC_LDT, size * 8 - 1);
  121. }
  122. #define LDT_entry_a(info) \
  123. ((((info)->base_addr & 0x0000ffff) << 16) | ((info)->limit & 0x0ffff))
  124. /* Don't allow setting of the lm bit. It is useless anyways because
  125. 64bit system calls require __USER_CS. */
  126. #define LDT_entry_b(info) \
  127. (((info)->base_addr & 0xff000000) | \
  128. (((info)->base_addr & 0x00ff0000) >> 16) | \
  129. ((info)->limit & 0xf0000) | \
  130. (((info)->read_exec_only ^ 1) << 9) | \
  131. ((info)->contents << 10) | \
  132. (((info)->seg_not_present ^ 1) << 15) | \
  133. ((info)->seg_32bit << 22) | \
  134. ((info)->limit_in_pages << 23) | \
  135. ((info)->useable << 20) | \
  136. /* ((info)->lm << 21) | */ \
  137. 0x7000)
  138. #define LDT_empty(info) (\
  139. (info)->base_addr == 0 && \
  140. (info)->limit == 0 && \
  141. (info)->contents == 0 && \
  142. (info)->read_exec_only == 1 && \
  143. (info)->seg_32bit == 0 && \
  144. (info)->limit_in_pages == 0 && \
  145. (info)->seg_not_present == 1 && \
  146. (info)->useable == 0 && \
  147. (info)->lm == 0)
  148. static inline void load_TLS(struct thread_struct *t, unsigned int cpu)
  149. {
  150. unsigned int i;
  151. u64 *gdt = (u64 *)(get_cpu_gdt_table(cpu) + GDT_ENTRY_TLS_MIN);
  152. for (i = 0; i < GDT_ENTRY_TLS_ENTRIES; i++)
  153. gdt[i] = t->tls_array[i];
  154. }
  155. /*
  156. * load one particular LDT into the current CPU
  157. */
  158. static inline void load_LDT_nolock(mm_context_t *pc, int cpu)
  159. {
  160. int count = pc->size;
  161. if (likely(!count)) {
  162. clear_LDT();
  163. return;
  164. }
  165. set_ldt_desc(cpu, pc->ldt, count);
  166. load_LDT_desc();
  167. }
  168. static inline void load_LDT(mm_context_t *pc)
  169. {
  170. int cpu = get_cpu();
  171. load_LDT_nolock(pc, cpu);
  172. put_cpu();
  173. }
  174. extern struct desc_ptr idt_descr;
  175. static inline unsigned long get_desc_base(const void *ptr)
  176. {
  177. const u32 *desc = ptr;
  178. unsigned long base;
  179. base = ((desc[0] >> 16) & 0x0000ffff) |
  180. ((desc[1] << 16) & 0x00ff0000) |
  181. (desc[1] & 0xff000000);
  182. return base;
  183. }
  184. #endif /* !__ASSEMBLY__ */
  185. #endif