1 /* Written 2000 by Andi Kleen */
5 #include <linux/threads.h>
10 #include <linux/string.h>
11 #include <asm/segment.h>
14 // 8 byte segment descriptor
18 unsigned base1 : 8, type : 4, s : 1, dpl : 2, p : 1;
19 unsigned limit : 4, avl : 1, l : 1, d : 1, g : 1, base2 : 8;
20 } __attribute__((packed));
22 struct n_desc_struct {
26 extern struct desc_struct cpu_gdt_table[NR_CPUS][GDT_ENTRIES];
38 unsigned ist : 3, zero0 : 5, type : 5, dpl : 2, p : 1;
42 } __attribute__((packed));
44 #define PTR_LOW(x) ((unsigned long)(x) & 0xFFFF)
45 #define PTR_MIDDLE(x) (((unsigned long)(x) >> 16) & 0xFFFF)
46 #define PTR_HIGH(x) ((unsigned long)(x) >> 32)
53 // LDT or TSS descriptor in the GDT. 16 bytes.
57 unsigned base1 : 8, type : 5, dpl : 2, p : 1;
58 unsigned limit1 : 4, zero0 : 3, g : 1, base2 : 8;
61 } __attribute__((packed));
65 unsigned long address;
66 } __attribute__((packed)) ;
68 #define load_TR_desc() asm volatile("ltr %w0"::"r" (GDT_ENTRY_TSS*8))
69 #define load_LDT_desc() asm volatile("lldt %w0"::"r" (GDT_ENTRY_LDT*8))
70 #define clear_LDT() asm volatile("lldt %w0"::"r" (0))
73 * This is the ldt that every process will get unless we need
74 * something other than this.
76 extern struct desc_struct default_ldt[];
77 extern struct gate_struct idt_table[];
79 static inline void _set_gate(void *adr, unsigned type, unsigned long func, unsigned dpl, unsigned ist)
82 s.offset_low = PTR_LOW(func);
83 s.segment = __KERNEL_CS;
90 s.offset_middle = PTR_MIDDLE(func);
91 s.offset_high = PTR_HIGH(func);
92 /* does not need to be atomic because it is only done once at setup time */
96 static inline void set_intr_gate(int nr, void *func)
98 _set_gate(&idt_table[nr], GATE_INTERRUPT, (unsigned long) func, 0, 0);
101 static inline void set_intr_gate_ist(int nr, void *func, unsigned ist)
103 _set_gate(&idt_table[nr], GATE_INTERRUPT, (unsigned long) func, 0, ist);
106 static inline void set_system_gate(int nr, void *func)
108 _set_gate(&idt_table[nr], GATE_INTERRUPT, (unsigned long) func, 3, 0);
111 static inline void set_tssldt_descriptor(void *ptr, unsigned long tss, unsigned type,
114 struct ldttss_desc d;
115 memset(&d,0,sizeof(d));
116 d.limit0 = size & 0xFFFF;
117 d.base0 = PTR_LOW(tss);
118 d.base1 = PTR_MIDDLE(tss) & 0xFF;
121 d.limit1 = (size >> 16) & 0xF;
122 d.base2 = (PTR_MIDDLE(tss) >> 8) & 0xFF;
123 d.base3 = PTR_HIGH(tss);
127 static inline void set_tss_desc(unsigned cpu, void *addr)
129 set_tssldt_descriptor(&cpu_gdt_table[cpu][GDT_ENTRY_TSS], (unsigned long)addr,
131 sizeof(struct tss_struct) - 1);
134 static inline void set_ldt_desc(unsigned cpu, void *addr, int size)
136 set_tssldt_descriptor(&cpu_gdt_table[cpu][GDT_ENTRY_LDT], (unsigned long)addr,
137 DESC_LDT, size * 8 - 1);
140 static inline void set_seg_base(unsigned cpu, int entry, void *base)
142 struct desc_struct *d = &cpu_gdt_table[cpu][entry];
143 u32 addr = (u32)(u64)base;
144 BUG_ON((u64)base >> 32);
145 d->base0 = addr & 0xffff;
146 d->base1 = (addr >> 16) & 0xff;
147 d->base2 = (addr >> 24) & 0xff;
150 #define LDT_entry_a(info) \
151 ((((info)->base_addr & 0x0000ffff) << 16) | ((info)->limit & 0x0ffff))
152 /* Don't allow setting of the lm bit. It is useless anyways because
153 64bit system calls require __USER_CS. */
154 #define LDT_entry_b(info) \
155 (((info)->base_addr & 0xff000000) | \
156 (((info)->base_addr & 0x00ff0000) >> 16) | \
157 ((info)->limit & 0xf0000) | \
158 (((info)->read_exec_only ^ 1) << 9) | \
159 ((info)->contents << 10) | \
160 (((info)->seg_not_present ^ 1) << 15) | \
161 ((info)->seg_32bit << 22) | \
162 ((info)->limit_in_pages << 23) | \
163 ((info)->useable << 20) | \
164 /* ((info)->lm << 21) | */ \
167 #define LDT_empty(info) (\
168 (info)->base_addr == 0 && \
169 (info)->limit == 0 && \
170 (info)->contents == 0 && \
171 (info)->read_exec_only == 1 && \
172 (info)->seg_32bit == 0 && \
173 (info)->limit_in_pages == 0 && \
174 (info)->seg_not_present == 1 && \
175 (info)->useable == 0 && \
179 # error update this code.
182 static inline void load_TLS(struct thread_struct *t, unsigned int cpu)
184 u64 *gdt = (u64 *)(cpu_gdt_table[cpu] + GDT_ENTRY_TLS_MIN);
185 gdt[0] = t->tls_array[0];
186 gdt[1] = t->tls_array[1];
187 gdt[2] = t->tls_array[2];
191 * load one particular LDT into the current CPU
193 extern inline void load_LDT_nolock (mm_context_t *pc, int cpu)
195 int count = pc->size;
197 if (likely(!count)) {
202 set_ldt_desc(cpu, pc->ldt, count);
206 static inline void load_LDT(mm_context_t *pc)
209 load_LDT_nolock(pc, cpu);
213 extern struct desc_ptr idt_descr;
215 #endif /* !__ASSEMBLY__ */