desc.h 5.2 KB

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