blob: e17581ad882414de354651f11f2db9d49ad79d53 [file] [log] [blame]
Glauber de Oliveira Costa80fbb692008-01-30 13:31:13 +01001#ifndef _ASM_DESC_H_
2#define _ASM_DESC_H_
3
4#ifndef __ASSEMBLY__
5#include <asm/desc_defs.h>
6#include <asm/ldt.h>
Glauber de Oliveira Costa881c2972008-01-30 13:31:14 +01007#include <asm/mmu.h>
Glauber de Oliveira Costa54cd0ea2008-01-30 13:31:14 +01008#include <linux/smp.h>
Glauber de Oliveira Costa80fbb692008-01-30 13:31:13 +01009
10static inline void fill_ldt(struct desc_struct *desc, struct user_desc *info)
11{
12 desc->limit0 = info->limit & 0x0ffff;
13 desc->base0 = info->base_addr & 0x0000ffff;
14
15 desc->base1 = (info->base_addr & 0x00ff0000) >> 16;
16 desc->type = (info->read_exec_only ^ 1) << 1;
17 desc->type |= info->contents << 2;
18 desc->s = 1;
19 desc->dpl = 0x3;
20 desc->p = info->seg_not_present ^ 1;
21 desc->limit = (info->limit & 0xf0000) >> 16;
22 desc->avl = info->useable;
23 desc->d = info->seg_32bit;
24 desc->g = info->limit_in_pages;
25 desc->base2 = (info->base_addr & 0xff000000) >> 24;
26}
27
Glauber de Oliveira Costa881c2972008-01-30 13:31:14 +010028extern struct desc_ptr idt_descr;
29extern gate_desc idt_table[];
Glauber de Oliveira Costa80fbb692008-01-30 13:31:13 +010030
Glauber de Oliveira Costa54cd0ea2008-01-30 13:31:14 +010031#ifdef CONFIG_X86_64
32extern struct desc_struct cpu_gdt_table[GDT_ENTRIES];
33extern struct desc_ptr cpu_gdt_descr[];
34/* the cpu gdt accessor */
35#define get_cpu_gdt_table(x) ((struct desc_struct *)cpu_gdt_descr[x].address)
Glauber de Oliveira Costa507f90c2008-01-30 13:31:14 +010036
37static inline void pack_gate(gate_desc *gate, unsigned type, unsigned long func,
38 unsigned dpl, unsigned ist, unsigned seg)
39{
40 gate->offset_low = PTR_LOW(func);
41 gate->segment = __KERNEL_CS;
42 gate->ist = ist;
43 gate->p = 1;
44 gate->dpl = dpl;
45 gate->zero0 = 0;
46 gate->zero1 = 0;
47 gate->type = type;
48 gate->offset_middle = PTR_MIDDLE(func);
49 gate->offset_high = PTR_HIGH(func);
50}
51
Glauber de Oliveira Costa54cd0ea2008-01-30 13:31:14 +010052#else
53struct gdt_page {
54 struct desc_struct gdt[GDT_ENTRIES];
55} __attribute__((aligned(PAGE_SIZE)));
56DECLARE_PER_CPU(struct gdt_page, gdt_page);
57
58static inline struct desc_struct *get_cpu_gdt_table(unsigned int cpu)
59{
60 return per_cpu(gdt_page, cpu).gdt;
61}
Glauber de Oliveira Costa507f90c2008-01-30 13:31:14 +010062
63static inline void pack_gate(gate_desc *gate, unsigned char type,
64 unsigned long base, unsigned dpl, unsigned flags, unsigned short seg)
65
66{
67 gate->a = (seg << 16) | (base & 0xffff);
68 gate->b = (base & 0xffff0000) |
69 (((0x80 | type | (dpl << 5)) & 0xff) << 8);
70}
71
Glauber de Oliveira Costa54cd0ea2008-01-30 13:31:14 +010072#endif
73
Glauber de Oliveira Costa746ff602008-01-30 13:31:27 +010074static inline int desc_empty(const void *ptr)
75{
76 const u32 *desc = ptr;
77 return !(desc[0] | desc[1]);
78}
79
Glauber de Oliveira Costa54cd0ea2008-01-30 13:31:14 +010080#ifdef CONFIG_PARAVIRT
81#include <asm/paravirt.h>
82#else
83#define load_TR_desc() native_load_tr_desc()
84#define load_gdt(dtr) native_load_gdt(dtr)
85#define load_idt(dtr) native_load_idt(dtr)
86#define load_tr(tr) __asm__ __volatile("ltr %0"::"m" (tr))
87#define load_ldt(ldt) __asm__ __volatile("lldt %0"::"m" (ldt))
88
89#define store_gdt(dtr) native_store_gdt(dtr)
90#define store_idt(dtr) native_store_idt(dtr)
91#define store_tr(tr) (tr = native_store_tr())
92#define store_ldt(ldt) __asm__ ("sldt %0":"=m" (ldt))
93
94#define load_TLS(t, cpu) native_load_tls(t, cpu)
95#define set_ldt native_set_ldt
96
97#define write_ldt_entry(dt, entry, desc) \
98 native_write_ldt_entry(dt, entry, desc)
99#define write_gdt_entry(dt, entry, desc, type) \
100 native_write_gdt_entry(dt, entry, desc, type)
101#define write_idt_entry(dt, entry, g) native_write_idt_entry(dt, entry, g)
102#endif
103
104static inline void native_write_idt_entry(gate_desc *idt, int entry,
105 const gate_desc *gate)
106{
107 memcpy(&idt[entry], gate, sizeof(*gate));
108}
109
110static inline void native_write_ldt_entry(struct desc_struct *ldt, int entry,
111 const void *desc)
112{
113 memcpy(&ldt[entry], desc, 8);
114}
115
116static inline void native_write_gdt_entry(struct desc_struct *gdt, int entry,
117 const void *desc, int type)
118{
119 unsigned int size;
120 switch (type) {
121 case DESC_TSS:
122 size = sizeof(tss_desc);
123 break;
124 case DESC_LDT:
125 size = sizeof(ldt_desc);
126 break;
127 default:
128 size = sizeof(struct desc_struct);
129 break;
130 }
131 memcpy(&gdt[entry], desc, size);
132}
133
Glauber de Oliveira Costa54cd0ea2008-01-30 13:31:14 +0100134static inline void pack_descriptor(struct desc_struct *desc, unsigned long base,
135 unsigned long limit, unsigned char type,
136 unsigned char flags)
137{
138 desc->a = ((base & 0xffff) << 16) | (limit & 0xffff);
139 desc->b = (base & 0xff000000) | ((base & 0xff0000) >> 16) |
140 (limit & 0x000f0000) | ((type & 0xff) << 8) |
141 ((flags & 0xf) << 20);
142 desc->p = 1;
143}
144
Glauber de Oliveira Costa54cd0ea2008-01-30 13:31:14 +0100145
Glauber de Oliveira Costaf6e0eba2008-01-30 13:31:20 +0100146static inline void set_tssldt_descriptor(void *d, unsigned long addr,
147 unsigned type, unsigned size)
Glauber de Oliveira Costac81c6ca2008-01-30 13:31:14 +0100148{
149#ifdef CONFIG_X86_64
Glauber de Oliveira Costaf6e0eba2008-01-30 13:31:20 +0100150 struct ldttss_desc64 *desc = d;
151 memset(desc, 0, sizeof(*desc));
152 desc->limit0 = size & 0xFFFF;
153 desc->base0 = PTR_LOW(addr);
154 desc->base1 = PTR_MIDDLE(addr) & 0xFF;
155 desc->type = type;
156 desc->p = 1;
157 desc->limit1 = (size >> 16) & 0xF;
158 desc->base2 = (PTR_MIDDLE(addr) >> 8) & 0xFF;
159 desc->base3 = PTR_HIGH(addr);
Glauber de Oliveira Costac81c6ca2008-01-30 13:31:14 +0100160#else
Glauber de Oliveira Costaf6e0eba2008-01-30 13:31:20 +0100161
162 pack_descriptor((struct desc_struct *)d, addr, size, 0x80 | type, 0);
Glauber de Oliveira Costac81c6ca2008-01-30 13:31:14 +0100163#endif
164}
165
166static inline void __set_tss_desc(unsigned cpu, unsigned int entry, void *addr)
167{
168 struct desc_struct *d = get_cpu_gdt_table(cpu);
169 tss_desc tss;
170
171 /*
172 * sizeof(unsigned long) coming from an extra "long" at the end
173 * of the iobitmap. See tss_struct definition in processor.h
174 *
175 * -1? seg base+limit should be pointing to the address of the
176 * last valid byte
177 */
Glauber de Oliveira Costaf6e0eba2008-01-30 13:31:20 +0100178 set_tssldt_descriptor(&tss, (unsigned long)addr, DESC_TSS,
179 IO_BITMAP_OFFSET + IO_BITMAP_BYTES + sizeof(unsigned long) - 1);
Glauber de Oliveira Costac81c6ca2008-01-30 13:31:14 +0100180 write_gdt_entry(d, entry, &tss, DESC_TSS);
181}
182
183#define set_tss_desc(cpu, addr) __set_tss_desc(cpu, GDT_ENTRY_TSS, addr)
184
Glauber de Oliveira Costa54cd0ea2008-01-30 13:31:14 +0100185static inline void native_set_ldt(const void *addr, unsigned int entries)
186{
187 if (likely(entries == 0))
188 __asm__ __volatile__("lldt %w0"::"q" (0));
189 else {
190 unsigned cpu = smp_processor_id();
191 ldt_desc ldt;
192
Glauber de Oliveira Costaf6e0eba2008-01-30 13:31:20 +0100193 set_tssldt_descriptor(&ldt, (unsigned long)addr,
194 DESC_LDT, entries * sizeof(ldt) - 1);
Glauber de Oliveira Costa54cd0ea2008-01-30 13:31:14 +0100195 write_gdt_entry(get_cpu_gdt_table(cpu), GDT_ENTRY_LDT,
196 &ldt, DESC_LDT);
197 __asm__ __volatile__("lldt %w0"::"q" (GDT_ENTRY_LDT*8));
198 }
199}
200
201static inline void native_load_tr_desc(void)
202{
203 asm volatile("ltr %w0"::"q" (GDT_ENTRY_TSS*8));
204}
205
206static inline void native_load_gdt(const struct desc_ptr *dtr)
207{
208 asm volatile("lgdt %0"::"m" (*dtr));
209}
210
211static inline void native_load_idt(const struct desc_ptr *dtr)
212{
213 asm volatile("lidt %0"::"m" (*dtr));
214}
215
216static inline void native_store_gdt(struct desc_ptr *dtr)
217{
218 asm volatile("sgdt %0":"=m" (*dtr));
219}
220
221static inline void native_store_idt(struct desc_ptr *dtr)
222{
223 asm volatile("sidt %0":"=m" (*dtr));
224}
225
226static inline unsigned long native_store_tr(void)
227{
228 unsigned long tr;
229 asm volatile("str %0":"=r" (tr));
230 return tr;
231}
232
233static inline void native_load_tls(struct thread_struct *t, unsigned int cpu)
234{
235 unsigned int i;
236 struct desc_struct *gdt = get_cpu_gdt_table(cpu);
237
238 for (i = 0; i < GDT_ENTRY_TLS_ENTRIES; i++)
239 gdt[GDT_ENTRY_TLS_MIN + i] = t->tls_array[i];
240}
241
Glauber de Oliveira Costa881c2972008-01-30 13:31:14 +0100242#define _LDT_empty(info) (\
243 (info)->base_addr == 0 && \
244 (info)->limit == 0 && \
245 (info)->contents == 0 && \
246 (info)->read_exec_only == 1 && \
247 (info)->seg_32bit == 0 && \
248 (info)->limit_in_pages == 0 && \
249 (info)->seg_not_present == 1 && \
250 (info)->useable == 0)
251
252#ifdef CONFIG_X86_64
253#define LDT_empty(info) (_LDT_empty(info) && ((info)->lm == 0))
254#else
255#define LDT_empty(info) (_LDT_empty(info))
256#endif
257
258static inline void clear_LDT(void)
259{
260 set_ldt(NULL, 0);
261}
262
263/*
264 * load one particular LDT into the current CPU
265 */
266static inline void load_LDT_nolock(mm_context_t *pc)
267{
268 set_ldt(pc->ldt, pc->size);
269}
270
271static inline void load_LDT(mm_context_t *pc)
272{
273 preempt_disable();
274 load_LDT_nolock(pc);
275 preempt_enable();
276}
277
Glauber de Oliveira Costacc697852008-01-30 13:31:14 +0100278static inline unsigned long get_desc_base(struct desc_struct *desc)
279{
280 return desc->base0 | ((desc->base1) << 16) | ((desc->base2) << 24);
281}
Glauber de Oliveira Costa507f90c2008-01-30 13:31:14 +0100282static inline void _set_gate(int gate, unsigned type, void *addr,
283 unsigned dpl, unsigned ist, unsigned seg)
284{
285 gate_desc s;
286 pack_gate(&s, type, (unsigned long)addr, dpl, ist, seg);
287 /*
288 * does not need to be atomic because it is only done once at
289 * setup time
290 */
291 write_idt_entry(idt_table, gate, &s);
292}
293
294/*
295 * This needs to use 'idt_table' rather than 'idt', and
296 * thus use the _nonmapped_ version of the IDT, as the
297 * Pentium F0 0F bugfix can have resulted in the mapped
298 * IDT being write-protected.
299 */
300static inline void set_intr_gate(unsigned int n, void *addr)
301{
302 BUG_ON((unsigned)n > 0xFF);
303 _set_gate(n, GATE_INTERRUPT, addr, 0, 0, __KERNEL_CS);
304}
305
306/*
307 * This routine sets up an interrupt gate at directory privilege level 3.
308 */
309static inline void set_system_intr_gate(unsigned int n, void *addr)
310{
311 BUG_ON((unsigned)n > 0xFF);
312 _set_gate(n, GATE_INTERRUPT, addr, 0x3, 0, __KERNEL_CS);
313}
314
315static inline void set_trap_gate(unsigned int n, void *addr)
316{
317 BUG_ON((unsigned)n > 0xFF);
318 _set_gate(n, GATE_TRAP, addr, 0, 0, __KERNEL_CS);
319}
320
321static inline void set_system_gate(unsigned int n, void *addr)
322{
323 BUG_ON((unsigned)n > 0xFF);
324#ifdef CONFIG_X86_32
325 _set_gate(n, GATE_TRAP, addr, 0x3, 0, __KERNEL_CS);
326#else
327 _set_gate(n, GATE_INTERRUPT, addr, 0x3, 0, __KERNEL_CS);
328#endif
329}
330
331static inline void set_task_gate(unsigned int n, unsigned int gdt_entry)
332{
333 BUG_ON((unsigned)n > 0xFF);
334 _set_gate(n, GATE_TASK, (void *)0, 0, 0, (gdt_entry<<3));
335}
336
337static inline void set_intr_gate_ist(int n, void *addr, unsigned ist)
338{
339 BUG_ON((unsigned)n > 0xFF);
340 _set_gate(n, GATE_INTERRUPT, addr, 0, ist, __KERNEL_CS);
341}
342
343static inline void set_system_gate_ist(int n, void *addr, unsigned ist)
344{
345 BUG_ON((unsigned)n > 0xFF);
346 _set_gate(n, GATE_INTERRUPT, addr, 0x3, ist, __KERNEL_CS);
347}
Glauber de Oliveira Costacc697852008-01-30 13:31:14 +0100348
Glauber de Oliveira Costa881c2972008-01-30 13:31:14 +0100349#else
350/*
351 * GET_DESC_BASE reads the descriptor base of the specified segment.
352 *
353 * Args:
354 * idx - descriptor index
355 * gdt - GDT pointer
356 * base - 32bit register to which the base will be written
357 * lo_w - lo word of the "base" register
358 * lo_b - lo byte of the "base" register
359 * hi_b - hi byte of the low word of the "base" register
360 *
361 * Example:
362 * GET_DESC_BASE(GDT_ENTRY_ESPFIX_SS, %ebx, %eax, %ax, %al, %ah)
363 * Will read the base address of GDT_ENTRY_ESPFIX_SS and put it into %eax.
364 */
365#define GET_DESC_BASE(idx, gdt, base, lo_w, lo_b, hi_b) \
366 movb idx*8+4(gdt), lo_b; \
367 movb idx*8+7(gdt), hi_b; \
368 shll $16, base; \
369 movw idx*8+2(gdt), lo_w;
370
371
372#endif /* __ASSEMBLY__ */
373
Glauber de Oliveira Costa80fbb692008-01-30 13:31:13 +0100374#endif