1 /* Written 2000 by Andi Kleen */
5 #include <linux/threads.h>
10 #include <linux/string.h>
11 #include <linux/smp.h>
13 #include <asm/segment.h>
16 // 8 byte segment descriptor
20 unsigned base1 : 8, type : 4, s : 1, dpl : 2, p : 1;
21 unsigned limit : 4, avl : 1, l : 1, d : 1, g : 1, base2 : 8;
22 } __attribute__((packed));
24 struct n_desc_struct {
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 extern struct desc_ptr idt_descr, cpu_gdt_descr[NR_CPUS];
70 extern struct desc_struct cpu_gdt_table[GDT_ENTRIES];
72 #define load_TR_desc() asm volatile("ltr %w0"::"r" (GDT_ENTRY_TSS*8))
73 #define load_LDT_desc() asm volatile("lldt %w0"::"r" (GDT_ENTRY_LDT*8))
75 static inline void clear_LDT(void)
80 * NB. We load the default_ldt for lcall7/27 handling on demand, as
81 * it slows down context switching. Noone uses it anyway.
83 cpu = cpu; /* XXX avoid compiler warning */
89 * This is the ldt that every process will get unless we need
90 * something other than this.
92 extern struct desc_struct default_ldt[];
93 #ifndef CONFIG_X86_NO_IDT
94 extern struct gate_struct idt_table[];
96 extern struct desc_ptr cpu_gdt_descr[];
98 /* the cpu gdt accessor */
99 #define cpu_gdt(_cpu) ((struct desc_struct *)cpu_gdt_descr[_cpu].address)
101 static inline void _set_gate(void *adr, unsigned type, unsigned long func, unsigned dpl, unsigned ist)
103 struct gate_struct s;
104 s.offset_low = PTR_LOW(func);
105 s.segment = __KERNEL_CS;
112 s.offset_middle = PTR_MIDDLE(func);
113 s.offset_high = PTR_HIGH(func);
114 /* does not need to be atomic because it is only done once at setup time */
118 #ifndef CONFIG_X86_NO_IDT
119 static inline void set_intr_gate(int nr, void *func)
121 BUG_ON((unsigned)nr > 0xFF);
122 _set_gate(&idt_table[nr], GATE_INTERRUPT, (unsigned long) func, 0, 0);
125 static inline void set_intr_gate_ist(int nr, void *func, unsigned ist)
127 BUG_ON((unsigned)nr > 0xFF);
128 _set_gate(&idt_table[nr], GATE_INTERRUPT, (unsigned long) func, 0, ist);
131 static inline void set_system_gate(int nr, void *func)
133 BUG_ON((unsigned)nr > 0xFF);
134 _set_gate(&idt_table[nr], GATE_INTERRUPT, (unsigned long) func, 3, 0);
137 static inline void set_system_gate_ist(int nr, void *func, unsigned ist)
139 _set_gate(&idt_table[nr], GATE_INTERRUPT, (unsigned long) func, 3, ist);
143 static inline void set_tssldt_descriptor(void *ptr, unsigned long tss, unsigned type,
146 struct ldttss_desc d;
147 memset(&d,0,sizeof(d));
148 d.limit0 = size & 0xFFFF;
149 d.base0 = PTR_LOW(tss);
150 d.base1 = PTR_MIDDLE(tss) & 0xFF;
153 d.limit1 = (size >> 16) & 0xF;
154 d.base2 = (PTR_MIDDLE(tss) >> 8) & 0xFF;
155 d.base3 = PTR_HIGH(tss);
159 #ifndef CONFIG_X86_NO_TSS
160 static inline void set_tss_desc(unsigned cpu, void *addr)
163 * sizeof(unsigned long) coming from an extra "long" at the end
164 * of the iobitmap. See tss_struct definition in processor.h
166 * -1? seg base+limit should be pointing to the address of the
169 set_tssldt_descriptor(&cpu_gdt(cpu)[GDT_ENTRY_TSS],
170 (unsigned long)addr, DESC_TSS,
171 IO_BITMAP_OFFSET + IO_BITMAP_BYTES + sizeof(unsigned long) - 1);
175 static inline void set_ldt_desc(unsigned cpu, void *addr, int size)
177 set_tssldt_descriptor(&cpu_gdt(cpu)[GDT_ENTRY_LDT], (unsigned long)addr,
178 DESC_LDT, size * 8 - 1);
181 static inline void set_seg_base(unsigned cpu, int entry, void *base)
183 struct desc_struct *d = &cpu_gdt(cpu)[entry];
184 u32 addr = (u32)(u64)base;
185 BUG_ON((u64)base >> 32);
186 d->base0 = addr & 0xffff;
187 d->base1 = (addr >> 16) & 0xff;
188 d->base2 = (addr >> 24) & 0xff;
191 #define LDT_entry_a(info) \
192 ((((info)->base_addr & 0x0000ffff) << 16) | ((info)->limit & 0x0ffff))
193 /* Don't allow setting of the lm bit. It is useless anyways because
194 64bit system calls require __USER_CS. */
195 #define LDT_entry_b(info) \
196 (((info)->base_addr & 0xff000000) | \
197 (((info)->base_addr & 0x00ff0000) >> 16) | \
198 ((info)->limit & 0xf0000) | \
199 (((info)->read_exec_only ^ 1) << 9) | \
200 ((info)->contents << 10) | \
201 (((info)->seg_not_present ^ 1) << 15) | \
202 ((info)->seg_32bit << 22) | \
203 ((info)->limit_in_pages << 23) | \
204 ((info)->useable << 20) | \
205 /* ((info)->lm << 21) | */ \
208 #define LDT_empty(info) (\
209 (info)->base_addr == 0 && \
210 (info)->limit == 0 && \
211 (info)->contents == 0 && \
212 (info)->read_exec_only == 1 && \
213 (info)->seg_32bit == 0 && \
214 (info)->limit_in_pages == 0 && \
215 (info)->seg_not_present == 1 && \
216 (info)->useable == 0 && \
220 # error update this code.
223 static inline void load_TLS(struct thread_struct *t, unsigned int cpu)
226 u64 *gdt = (u64 *)(cpu_gdt(cpu) + GDT_ENTRY_TLS_MIN);
227 gdt[0] = t->tls_array[0];
228 gdt[1] = t->tls_array[1];
229 gdt[2] = t->tls_array[2];
232 HYPERVISOR_update_descriptor(virt_to_machine(&cpu_gdt(cpu)[GDT_ENTRY_TLS_MIN + i]), t->tls_array[i])
239 * load one particular LDT into the current CPU
241 static inline void load_LDT_nolock (mm_context_t *pc, int cpu)
243 void *segments = pc->ldt;
244 int count = pc->size;
249 xen_set_ldt((unsigned long)segments, count);
252 static inline void load_LDT(mm_context_t *pc)
255 load_LDT_nolock(pc, cpu);
259 extern struct desc_ptr idt_descr;
261 #endif /* !__ASSEMBLY__ */