desc_64.h 5.0 KB

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