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 {
36 unsigned ist : 3, zero0 : 5, type : 5, dpl : 2, p : 1;
40 } __attribute__((packed));
42 #define PTR_LOW(x) ((unsigned long)(x) & 0xFFFF)
43 #define PTR_MIDDLE(x) (((unsigned long)(x) >> 16) & 0xFFFF)
44 #define PTR_HIGH(x) ((unsigned long)(x) >> 32)
51 // LDT or TSS descriptor in the GDT. 16 bytes.
55 unsigned base1 : 8, type : 5, dpl : 2, p : 1;
56 unsigned limit1 : 4, zero0 : 3, g : 1, base2 : 8;
59 } __attribute__((packed));
63 unsigned long address;
64 } __attribute__((packed)) ;
66 extern struct desc_ptr idt_descr, cpu_gdt_descr[NR_CPUS];
68 extern struct desc_ptr cpu_gdt_table[NR_CPUS][GDT_ENTRIES];
70 #define get_cpu_gdt_table(_cpu) ((struct desc_ptr *)(cpu_gdt_descr[(_cpu)].address))
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 extern struct gate_struct idt_table[];
95 static inline void _set_gate(void *adr, unsigned type, unsigned long func, unsigned dpl, unsigned ist)
98 s.offset_low = PTR_LOW(func);
99 s.segment = __KERNEL_CS;
106 s.offset_middle = PTR_MIDDLE(func);
107 s.offset_high = PTR_HIGH(func);
108 /* does not need to be atomic because it is only done once at setup time */
112 static inline void set_intr_gate(int nr, void *func)
114 _set_gate(&idt_table[nr], GATE_INTERRUPT, (unsigned long) func, 0, 0);
117 static inline void set_intr_gate_ist(int nr, void *func, unsigned ist)
119 _set_gate(&idt_table[nr], GATE_INTERRUPT, (unsigned long) func, 0, ist);
122 static inline void set_system_gate(int nr, void *func)
124 _set_gate(&idt_table[nr], GATE_INTERRUPT, (unsigned long) func, 3, 0);
127 static inline void set_tssldt_descriptor(void *ptr, unsigned long tss, unsigned type,
130 struct ldttss_desc d;
131 memset(&d,0,sizeof(d));
132 d.limit0 = size & 0xFFFF;
133 d.base0 = PTR_LOW(tss);
134 d.base1 = PTR_MIDDLE(tss) & 0xFF;
137 d.limit1 = (size >> 16) & 0xF;
138 d.base2 = (PTR_MIDDLE(tss) >> 8) & 0xFF;
139 d.base3 = PTR_HIGH(tss);
143 static inline void set_tss_desc(unsigned cpu, void *addr)
145 set_tssldt_descriptor((struct ldttss_desc *)&get_cpu_gdt_table(cpu)[GDT_ENTRY_TSS],
148 sizeof(struct tss_struct) - 1);
151 static inline void set_ldt_desc(unsigned cpu, void *addr, int size)
153 set_tssldt_descriptor((struct ldttss_desc *)&get_cpu_gdt_table(cpu)[GDT_ENTRY_LDT],
155 DESC_LDT, size * 8 - 1);
158 static inline void set_seg_base(unsigned cpu, int entry, void *base)
160 struct desc_struct *d = (struct desc_struct *)&get_cpu_gdt_table(cpu)[entry];
161 u32 addr = (u32)(u64)base;
162 BUG_ON((u64)base >> 32);
163 d->base0 = addr & 0xffff;
164 d->base1 = (addr >> 16) & 0xff;
165 d->base2 = (addr >> 24) & 0xff;
168 #define LDT_entry_a(info) \
169 ((((info)->base_addr & 0x0000ffff) << 16) | ((info)->limit & 0x0ffff))
170 /* Don't allow setting of the lm bit. It is useless anyways because
171 64bit system calls require __USER_CS. */
172 #define LDT_entry_b(info) \
173 (((info)->base_addr & 0xff000000) | \
174 (((info)->base_addr & 0x00ff0000) >> 16) | \
175 ((info)->limit & 0xf0000) | \
176 (((info)->read_exec_only ^ 1) << 9) | \
177 ((info)->contents << 10) | \
178 (((info)->seg_not_present ^ 1) << 15) | \
179 ((info)->seg_32bit << 22) | \
180 ((info)->limit_in_pages << 23) | \
181 ((info)->useable << 20) | \
182 /* ((info)->lm << 21) | */ \
185 #define LDT_empty(info) (\
186 (info)->base_addr == 0 && \
187 (info)->limit == 0 && \
188 (info)->contents == 0 && \
189 (info)->read_exec_only == 1 && \
190 (info)->seg_32bit == 0 && \
191 (info)->limit_in_pages == 0 && \
192 (info)->seg_not_present == 1 && \
193 (info)->useable == 0 && \
197 # error update this code.
200 static inline void load_TLS(struct thread_struct *t, unsigned int cpu)
203 u64 *gdt = (u64 *)(cpu_gdt_table[cpu] + GDT_ENTRY_TLS_MIN);
204 gdt[0] = t->tls_array[0];
205 gdt[1] = t->tls_array[1];
206 gdt[2] = t->tls_array[2];
209 HYPERVISOR_update_descriptor(virt_to_machine(&get_cpu_gdt_table(cpu)[GDT_ENTRY_TLS_MIN + i]), t->tls_array[i])
216 * load one particular LDT into the current CPU
218 extern inline void load_LDT_nolock (mm_context_t *pc, int cpu)
220 void *segments = pc->ldt;
221 int count = pc->size;
226 xen_set_ldt((unsigned long)segments, count);
229 static inline void load_LDT(mm_context_t *pc)
232 load_LDT_nolock(pc, cpu);
236 extern struct desc_ptr idt_descr;
238 #endif /* !__ASSEMBLY__ */