i386: NX emulation
[linux-flexiantxendom0-natty.git] / arch / x86 / include / asm / desc.h
1 #ifndef _ASM_X86_DESC_H
2 #define _ASM_X86_DESC_H
3
4 #include <asm/desc_defs.h>
5 #include <asm/ldt.h>
6 #include <asm/mmu.h>
7 #include <linux/smp.h>
8 #include <linux/mm_types.h>
9
10 static inline void fill_ldt(struct desc_struct *desc,
11                             const struct user_desc *info)
12 {
13         desc->limit0 = info->limit & 0x0ffff;
14         desc->base0 = info->base_addr & 0x0000ffff;
15
16         desc->base1 = (info->base_addr & 0x00ff0000) >> 16;
17         desc->type = (info->read_exec_only ^ 1) << 1;
18         desc->type |= info->contents << 2;
19         desc->s = 1;
20         desc->dpl = 0x3;
21         desc->p = info->seg_not_present ^ 1;
22         desc->limit = (info->limit & 0xf0000) >> 16;
23         desc->avl = info->useable;
24         desc->d = info->seg_32bit;
25         desc->g = info->limit_in_pages;
26         desc->base2 = (info->base_addr & 0xff000000) >> 24;
27         /*
28          * Don't allow setting of the lm bit. It is useless anyway
29          * because 64bit system calls require __USER_CS:
30          */
31         desc->l = 0;
32 }
33
34 extern struct desc_ptr idt_descr;
35 extern gate_desc idt_table[];
36
37 struct gdt_page {
38         struct desc_struct gdt[GDT_ENTRIES];
39 } __attribute__((aligned(PAGE_SIZE)));
40 DECLARE_PER_CPU_PAGE_ALIGNED(struct gdt_page, gdt_page);
41
42 static inline struct desc_struct *get_cpu_gdt_table(unsigned int cpu)
43 {
44         return per_cpu(gdt_page, cpu).gdt;
45 }
46
47 #ifdef CONFIG_X86_64
48
49 static inline void pack_gate(gate_desc *gate, unsigned type, unsigned long func,
50                              unsigned dpl, unsigned ist, unsigned seg)
51 {
52         gate->offset_low = PTR_LOW(func);
53         gate->segment = __KERNEL_CS;
54         gate->ist = ist;
55         gate->p = 1;
56         gate->dpl = dpl;
57         gate->zero0 = 0;
58         gate->zero1 = 0;
59         gate->type = type;
60         gate->offset_middle = PTR_MIDDLE(func);
61         gate->offset_high = PTR_HIGH(func);
62 }
63
64 #else
65 static inline void pack_gate(gate_desc *gate, unsigned char type,
66                              unsigned long base, unsigned dpl, unsigned flags,
67                              unsigned short seg)
68 {
69         gate->a = (seg << 16) | (base & 0xffff);
70         gate->b = (base & 0xffff0000) |
71                   (((0x80 | type | (dpl << 5)) & 0xff) << 8);
72 }
73
74 #endif
75
76 static inline int desc_empty(const void *ptr)
77 {
78         const u32 *desc = ptr;
79         return !(desc[0] | desc[1]);
80 }
81
82 #ifdef CONFIG_PARAVIRT
83 #include <asm/paravirt.h>
84 #else
85 #define load_TR_desc() native_load_tr_desc()
86 #define load_gdt(dtr) native_load_gdt(dtr)
87 #define load_idt(dtr) native_load_idt(dtr)
88 #define load_tr(tr) asm volatile("ltr %0"::"m" (tr))
89 #define load_ldt(ldt) asm volatile("lldt %0"::"m" (ldt))
90
91 #define store_gdt(dtr) native_store_gdt(dtr)
92 #define store_idt(dtr) native_store_idt(dtr)
93 #define store_tr(tr) (tr = native_store_tr())
94
95 #define load_TLS(t, cpu) native_load_tls(t, cpu)
96 #define set_ldt native_set_ldt
97 #ifdef CONFIG_X86_32
98 #define load_user_cs_desc native_load_user_cs_desc
99 #endif /*CONFIG_X86_32*/
100
101 #define write_ldt_entry(dt, entry, desc)        \
102         native_write_ldt_entry(dt, entry, desc)
103 #define write_gdt_entry(dt, entry, desc, type)          \
104         native_write_gdt_entry(dt, entry, desc, type)
105 #define write_idt_entry(dt, entry, g)           \
106         native_write_idt_entry(dt, entry, g)
107
108 static inline void paravirt_alloc_ldt(struct desc_struct *ldt, unsigned entries)
109 {
110 }
111
112 static inline void paravirt_free_ldt(struct desc_struct *ldt, unsigned entries)
113 {
114 }
115 #endif  /* CONFIG_PARAVIRT */
116
117 #define store_ldt(ldt) asm("sldt %0" : "=m"(ldt))
118
119 static inline void native_write_idt_entry(gate_desc *idt, int entry,
120                                           const gate_desc *gate)
121 {
122         memcpy(&idt[entry], gate, sizeof(*gate));
123 }
124
125 static inline void native_write_ldt_entry(struct desc_struct *ldt, int entry,
126                                           const void *desc)
127 {
128         memcpy(&ldt[entry], desc, 8);
129 }
130
131 static inline void native_write_gdt_entry(struct desc_struct *gdt, int entry,
132                                           const void *desc, int type)
133 {
134         unsigned int size;
135         switch (type) {
136         case DESC_TSS:
137                 size = sizeof(tss_desc);
138                 break;
139         case DESC_LDT:
140                 size = sizeof(ldt_desc);
141                 break;
142         default:
143                 size = sizeof(struct desc_struct);
144                 break;
145         }
146         memcpy(&gdt[entry], desc, size);
147 }
148
149 static inline void pack_descriptor(struct desc_struct *desc, unsigned long base,
150                                    unsigned long limit, unsigned char type,
151                                    unsigned char flags)
152 {
153         desc->a = ((base & 0xffff) << 16) | (limit & 0xffff);
154         desc->b = (base & 0xff000000) | ((base & 0xff0000) >> 16) |
155                 (limit & 0x000f0000) | ((type & 0xff) << 8) |
156                 ((flags & 0xf) << 20);
157         desc->p = 1;
158 }
159
160
161 static inline void set_tssldt_descriptor(void *d, unsigned long addr,
162                                          unsigned type, unsigned size)
163 {
164 #ifdef CONFIG_X86_64
165         struct ldttss_desc64 *desc = d;
166         memset(desc, 0, sizeof(*desc));
167         desc->limit0 = size & 0xFFFF;
168         desc->base0 = PTR_LOW(addr);
169         desc->base1 = PTR_MIDDLE(addr) & 0xFF;
170         desc->type = type;
171         desc->p = 1;
172         desc->limit1 = (size >> 16) & 0xF;
173         desc->base2 = (PTR_MIDDLE(addr) >> 8) & 0xFF;
174         desc->base3 = PTR_HIGH(addr);
175 #else
176         pack_descriptor((struct desc_struct *)d, addr, size, 0x80 | type, 0);
177 #endif
178 }
179
180 static inline void __set_tss_desc(unsigned cpu, unsigned int entry, void *addr)
181 {
182         struct desc_struct *d = get_cpu_gdt_table(cpu);
183         tss_desc tss;
184
185         /*
186          * sizeof(unsigned long) coming from an extra "long" at the end
187          * of the iobitmap. See tss_struct definition in processor.h
188          *
189          * -1? seg base+limit should be pointing to the address of the
190          * last valid byte
191          */
192         set_tssldt_descriptor(&tss, (unsigned long)addr, DESC_TSS,
193                               IO_BITMAP_OFFSET + IO_BITMAP_BYTES +
194                               sizeof(unsigned long) - 1);
195         write_gdt_entry(d, entry, &tss, DESC_TSS);
196 }
197
198 #define set_tss_desc(cpu, addr) __set_tss_desc(cpu, GDT_ENTRY_TSS, addr)
199
200 static inline void native_set_ldt(const void *addr, unsigned int entries)
201 {
202         if (likely(entries == 0))
203                 asm volatile("lldt %w0"::"q" (0));
204         else {
205                 unsigned cpu = smp_processor_id();
206                 ldt_desc ldt;
207
208                 set_tssldt_descriptor(&ldt, (unsigned long)addr, DESC_LDT,
209                                       entries * LDT_ENTRY_SIZE - 1);
210                 write_gdt_entry(get_cpu_gdt_table(cpu), GDT_ENTRY_LDT,
211                                 &ldt, DESC_LDT);
212                 asm volatile("lldt %w0"::"q" (GDT_ENTRY_LDT*8));
213         }
214 }
215
216 static inline void native_load_tr_desc(void)
217 {
218         asm volatile("ltr %w0"::"q" (GDT_ENTRY_TSS*8));
219 }
220
221 static inline void native_load_gdt(const struct desc_ptr *dtr)
222 {
223         asm volatile("lgdt %0"::"m" (*dtr));
224 }
225
226 static inline void native_load_idt(const struct desc_ptr *dtr)
227 {
228         asm volatile("lidt %0"::"m" (*dtr));
229 }
230
231 static inline void native_store_gdt(struct desc_ptr *dtr)
232 {
233         asm volatile("sgdt %0":"=m" (*dtr));
234 }
235
236 static inline void native_store_idt(struct desc_ptr *dtr)
237 {
238         asm volatile("sidt %0":"=m" (*dtr));
239 }
240
241 static inline unsigned long native_store_tr(void)
242 {
243         unsigned long tr;
244         asm volatile("str %0":"=r" (tr));
245         return tr;
246 }
247
248 static inline void native_load_tls(struct thread_struct *t, unsigned int cpu)
249 {
250         unsigned int i;
251         struct desc_struct *gdt = get_cpu_gdt_table(cpu);
252
253         for (i = 0; i < GDT_ENTRY_TLS_ENTRIES; i++)
254                 gdt[GDT_ENTRY_TLS_MIN + i] = t->tls_array[i];
255 }
256
257 #define _LDT_empty(info)                                \
258         ((info)->base_addr              == 0    &&      \
259          (info)->limit                  == 0    &&      \
260          (info)->contents               == 0    &&      \
261          (info)->read_exec_only         == 1    &&      \
262          (info)->seg_32bit              == 0    &&      \
263          (info)->limit_in_pages         == 0    &&      \
264          (info)->seg_not_present        == 1    &&      \
265          (info)->useable                == 0)
266
267 #ifdef CONFIG_X86_64
268 #define LDT_empty(info) (_LDT_empty(info) && ((info)->lm == 0))
269 #else
270 #define LDT_empty(info) (_LDT_empty(info))
271 #endif
272
273 static inline void clear_LDT(void)
274 {
275         set_ldt(NULL, 0);
276 }
277
278 /*
279  * load one particular LDT into the current CPU
280  */
281 static inline void load_LDT_nolock(mm_context_t *pc)
282 {
283         set_ldt(pc->ldt, pc->size);
284 }
285
286 static inline void load_LDT(mm_context_t *pc)
287 {
288         preempt_disable();
289         load_LDT_nolock(pc);
290         preempt_enable();
291 }
292
293 static inline unsigned long get_desc_base(const struct desc_struct *desc)
294 {
295         return (unsigned)(desc->base0 | ((desc->base1) << 16) | ((desc->base2) << 24));
296 }
297
298 static inline void set_desc_base(struct desc_struct *desc, unsigned long base)
299 {
300         desc->base0 = base & 0xffff;
301         desc->base1 = (base >> 16) & 0xff;
302         desc->base2 = (base >> 24) & 0xff;
303 }
304
305 static inline unsigned long get_desc_limit(const struct desc_struct *desc)
306 {
307         return desc->limit0 | (desc->limit << 16);
308 }
309
310 static inline void set_desc_limit(struct desc_struct *desc, unsigned long limit)
311 {
312         desc->limit0 = limit & 0xffff;
313         desc->limit = (limit >> 16) & 0xf;
314 }
315
316 static inline void _set_gate(int gate, unsigned type, void *addr,
317                              unsigned dpl, unsigned ist, unsigned seg)
318 {
319         gate_desc s;
320         pack_gate(&s, type, (unsigned long)addr, dpl, ist, seg);
321         /*
322          * does not need to be atomic because it is only done once at
323          * setup time
324          */
325         write_idt_entry(idt_table, gate, &s);
326 }
327
328 /*
329  * This needs to use 'idt_table' rather than 'idt', and
330  * thus use the _nonmapped_ version of the IDT, as the
331  * Pentium F0 0F bugfix can have resulted in the mapped
332  * IDT being write-protected.
333  */
334 static inline void set_intr_gate(unsigned int n, void *addr)
335 {
336         BUG_ON((unsigned)n > 0xFF);
337         _set_gate(n, GATE_INTERRUPT, addr, 0, 0, __KERNEL_CS);
338 }
339
340 extern int first_system_vector;
341 /* used_vectors is BITMAP for irq is not managed by percpu vector_irq */
342 extern unsigned long used_vectors[];
343
344 static inline void alloc_system_vector(int vector)
345 {
346         if (!test_bit(vector, used_vectors)) {
347                 set_bit(vector, used_vectors);
348                 if (first_system_vector > vector)
349                         first_system_vector = vector;
350         } else
351                 BUG();
352 }
353
354 static inline void alloc_intr_gate(unsigned int n, void *addr)
355 {
356         alloc_system_vector(n);
357         set_intr_gate(n, addr);
358 }
359
360 /*
361  * This routine sets up an interrupt gate at directory privilege level 3.
362  */
363 static inline void set_system_intr_gate(unsigned int n, void *addr)
364 {
365         BUG_ON((unsigned)n > 0xFF);
366         _set_gate(n, GATE_INTERRUPT, addr, 0x3, 0, __KERNEL_CS);
367 }
368
369 static inline void set_system_trap_gate(unsigned int n, void *addr)
370 {
371         BUG_ON((unsigned)n > 0xFF);
372         _set_gate(n, GATE_TRAP, addr, 0x3, 0, __KERNEL_CS);
373 }
374
375 static inline void set_trap_gate(unsigned int n, void *addr)
376 {
377         BUG_ON((unsigned)n > 0xFF);
378         _set_gate(n, GATE_TRAP, addr, 0, 0, __KERNEL_CS);
379 }
380
381 static inline void set_task_gate(unsigned int n, unsigned int gdt_entry)
382 {
383         BUG_ON((unsigned)n > 0xFF);
384         _set_gate(n, GATE_TASK, (void *)0, 0, 0, (gdt_entry<<3));
385 }
386
387 static inline void set_intr_gate_ist(int n, void *addr, unsigned ist)
388 {
389         BUG_ON((unsigned)n > 0xFF);
390         _set_gate(n, GATE_INTERRUPT, addr, 0, ist, __KERNEL_CS);
391 }
392
393 static inline void set_system_intr_gate_ist(int n, void *addr, unsigned ist)
394 {
395         BUG_ON((unsigned)n > 0xFF);
396         _set_gate(n, GATE_INTERRUPT, addr, 0x3, ist, __KERNEL_CS);
397 }
398
399 #ifdef CONFIG_X86_32
400 static inline void set_user_cs(struct desc_struct *desc, unsigned long limit)
401 {
402         limit = (limit - 1) / PAGE_SIZE;
403         desc->a = limit & 0xffff;
404         desc->b = (limit & 0xf0000) | 0x00c0fb00;
405 }
406
407 static inline void native_load_user_cs_desc(int cpu, struct mm_struct *mm)
408 {
409         get_cpu_gdt_table(cpu)[GDT_ENTRY_DEFAULT_USER_CS] = (mm)->context.user_cs;
410 }
411
412 #define arch_add_exec_range arch_add_exec_range
413 #define arch_remove_exec_range arch_remove_exec_range
414 #define arch_flush_exec_range arch_flush_exec_range
415 extern void arch_add_exec_range(struct mm_struct *mm, unsigned long limit);
416 extern void arch_remove_exec_range(struct mm_struct *mm, unsigned long limit);
417 extern void arch_flush_exec_range(struct mm_struct *mm);
418 #endif /* CONFIG_X86_32 */
419
420 #endif /* _ASM_X86_DESC_H */