desc.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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/segment.h>
  10. #include <asm/mmu.h>
  11. // 8 byte segment descriptor
  12. struct desc_struct {
  13. u16 limit0;
  14. u16 base0;
  15. unsigned base1 : 8, type : 4, s : 1, dpl : 2, p : 1;
  16. unsigned limit : 4, avl : 1, l : 1, d : 1, g : 1, base2 : 8;
  17. } __attribute__((packed));
  18. struct n_desc_struct {
  19. unsigned int a,b;
  20. };
  21. extern struct desc_struct cpu_gdt_table[GDT_ENTRIES];
  22. enum {
  23. GATE_INTERRUPT = 0xE,
  24. GATE_TRAP = 0xF,
  25. GATE_CALL = 0xC,
  26. };
  27. // 16byte gate
  28. struct gate_struct {
  29. u16 offset_low;
  30. u16 segment;
  31. unsigned ist : 3, zero0 : 5, type : 5, dpl : 2, p : 1;
  32. u16 offset_middle;
  33. u32 offset_high;
  34. u32 zero1;
  35. } __attribute__((packed));
  36. #define PTR_LOW(x) ((unsigned long)(x) & 0xFFFF)
  37. #define PTR_MIDDLE(x) (((unsigned long)(x) >> 16) & 0xFFFF)
  38. #define PTR_HIGH(x) ((unsigned long)(x) >> 32)
  39. enum {
  40. DESC_TSS = 0x9,
  41. DESC_LDT = 0x2,
  42. };
  43. // LDT or TSS descriptor in the GDT. 16 bytes.
  44. struct ldttss_desc {
  45. u16 limit0;
  46. u16 base0;
  47. unsigned base1 : 8, type : 5, dpl : 2, p : 1;
  48. unsigned limit1 : 4, zero0 : 3, g : 1, base2 : 8;
  49. u32 base3;
  50. u32 zero1;
  51. } __attribute__((packed));
  52. struct desc_ptr {
  53. unsigned short size;
  54. unsigned long address;
  55. } __attribute__((packed)) ;
  56. #define load_TR_desc() asm volatile("ltr %w0"::"r" (GDT_ENTRY_TSS*8))
  57. #define load_LDT_desc() asm volatile("lldt %w0"::"r" (GDT_ENTRY_LDT*8))
  58. #define clear_LDT() asm volatile("lldt %w0"::"r" (0))
  59. /*
  60. * This is the ldt that every process will get unless we need
  61. * something other than this.
  62. */
  63. extern struct desc_struct default_ldt[];
  64. extern struct gate_struct idt_table[];
  65. extern struct desc_ptr cpu_gdt_descr[];
  66. /* the cpu gdt accessor */
  67. #define cpu_gdt(_cpu) ((struct desc_struct *)cpu_gdt_descr[_cpu].address)
  68. static inline void _set_gate(void *adr, unsigned type, unsigned long func, unsigned dpl, unsigned ist)
  69. {
  70. struct gate_struct s;
  71. s.offset_low = PTR_LOW(func);
  72. s.segment = __KERNEL_CS;
  73. s.ist = ist;
  74. s.p = 1;
  75. s.dpl = dpl;
  76. s.zero0 = 0;
  77. s.zero1 = 0;
  78. s.type = type;
  79. s.offset_middle = PTR_MIDDLE(func);
  80. s.offset_high = PTR_HIGH(func);
  81. /* does not need to be atomic because it is only done once at setup time */
  82. memcpy(adr, &s, 16);
  83. }
  84. static inline void set_intr_gate(int nr, void *func)
  85. {
  86. BUG_ON((unsigned)nr > 0xFF);
  87. _set_gate(&idt_table[nr], GATE_INTERRUPT, (unsigned long) func, 0, 0);
  88. }
  89. static inline void set_intr_gate_ist(int nr, void *func, unsigned ist)
  90. {
  91. BUG_ON((unsigned)nr > 0xFF);
  92. _set_gate(&idt_table[nr], GATE_INTERRUPT, (unsigned long) func, 0, ist);
  93. }
  94. static inline void set_system_gate(int nr, void *func)
  95. {
  96. BUG_ON((unsigned)nr > 0xFF);
  97. _set_gate(&idt_table[nr], GATE_INTERRUPT, (unsigned long) func, 3, 0);
  98. }
  99. static inline void set_system_gate_ist(int nr, void *func, unsigned ist)
  100. {
  101. _set_gate(&idt_table[nr], GATE_INTERRUPT, (unsigned long) func, 3, ist);
  102. }
  103. static inline void set_tssldt_descriptor(void *ptr, unsigned long tss, unsigned type,
  104. unsigned size)
  105. {
  106. struct ldttss_desc d;
  107. memset(&d,0,sizeof(d));
  108. d.limit0 = size & 0xFFFF;
  109. d.base0 = PTR_LOW(tss);
  110. d.base1 = PTR_MIDDLE(tss) & 0xFF;
  111. d.type = type;
  112. d.p = 1;
  113. d.limit1 = (size >> 16) & 0xF;
  114. d.base2 = (PTR_MIDDLE(tss) >> 8) & 0xFF;
  115. d.base3 = PTR_HIGH(tss);
  116. memcpy(ptr, &d, 16);
  117. }
  118. static inline void set_tss_desc(unsigned cpu, void *addr)
  119. {
  120. /*
  121. * sizeof(unsigned long) coming from an extra "long" at the end
  122. * of the iobitmap. See tss_struct definition in processor.h
  123. *
  124. * -1? seg base+limit should be pointing to the address of the
  125. * last valid byte
  126. */
  127. set_tssldt_descriptor(&cpu_gdt(cpu)[GDT_ENTRY_TSS],
  128. (unsigned long)addr, DESC_TSS,
  129. IO_BITMAP_OFFSET + IO_BITMAP_BYTES + sizeof(unsigned long) - 1);
  130. }
  131. static inline void set_ldt_desc(unsigned cpu, void *addr, int size)
  132. {
  133. set_tssldt_descriptor(&cpu_gdt(cpu)[GDT_ENTRY_LDT], (unsigned long)addr,
  134. DESC_LDT, size * 8 - 1);
  135. }
  136. static inline void set_seg_base(unsigned cpu, int entry, void *base)
  137. {
  138. struct desc_struct *d = &cpu_gdt(cpu)[entry];
  139. u32 addr = (u32)(u64)base;
  140. BUG_ON((u64)base >> 32);
  141. d->base0 = addr & 0xffff;
  142. d->base1 = (addr >> 16) & 0xff;
  143. d->base2 = (addr >> 24) & 0xff;
  144. }
  145. #define LDT_entry_a(info) \
  146. ((((info)->base_addr & 0x0000ffff) << 16) | ((info)->limit & 0x0ffff))
  147. /* Don't allow setting of the lm bit. It is useless anyways because
  148. 64bit system calls require __USER_CS. */
  149. #define LDT_entry_b(info) \
  150. (((info)->base_addr & 0xff000000) | \
  151. (((info)->base_addr & 0x00ff0000) >> 16) | \
  152. ((info)->limit & 0xf0000) | \
  153. (((info)->read_exec_only ^ 1) << 9) | \
  154. ((info)->contents << 10) | \
  155. (((info)->seg_not_present ^ 1) << 15) | \
  156. ((info)->seg_32bit << 22) | \
  157. ((info)->limit_in_pages << 23) | \
  158. ((info)->useable << 20) | \
  159. /* ((info)->lm << 21) | */ \
  160. 0x7000)
  161. #define LDT_empty(info) (\
  162. (info)->base_addr == 0 && \
  163. (info)->limit == 0 && \
  164. (info)->contents == 0 && \
  165. (info)->read_exec_only == 1 && \
  166. (info)->seg_32bit == 0 && \
  167. (info)->limit_in_pages == 0 && \
  168. (info)->seg_not_present == 1 && \
  169. (info)->useable == 0 && \
  170. (info)->lm == 0)
  171. #if TLS_SIZE != 24
  172. # error update this code.
  173. #endif
  174. static inline void load_TLS(struct thread_struct *t, unsigned int cpu)
  175. {
  176. u64 *gdt = (u64 *)(cpu_gdt(cpu) + GDT_ENTRY_TLS_MIN);
  177. gdt[0] = t->tls_array[0];
  178. gdt[1] = t->tls_array[1];
  179. gdt[2] = t->tls_array[2];
  180. }
  181. /*
  182. * load one particular LDT into the current CPU
  183. */
  184. static inline void load_LDT_nolock (mm_context_t *pc, int cpu)
  185. {
  186. int count = pc->size;
  187. if (likely(!count)) {
  188. clear_LDT();
  189. return;
  190. }
  191. set_ldt_desc(cpu, pc->ldt, count);
  192. load_LDT_desc();
  193. }
  194. static inline void load_LDT(mm_context_t *pc)
  195. {
  196. int cpu = get_cpu();
  197. load_LDT_nolock(pc, cpu);
  198. put_cpu();
  199. }
  200. extern struct desc_ptr idt_descr;
  201. #endif /* !__ASSEMBLY__ */
  202. #endif