[PATCH] i386: rationalize paravirt wrappers
[linux-2.6] / include / asm-i386 / desc.h
1 #ifndef __ARCH_DESC_H
2 #define __ARCH_DESC_H
3
4 #include <asm/ldt.h>
5 #include <asm/segment.h>
6
7 #ifndef __ASSEMBLY__
8
9 #include <linux/preempt.h>
10 #include <linux/smp.h>
11 #include <linux/percpu.h>
12
13 #include <asm/mmu.h>
14
15 struct Xgt_desc_struct {
16         unsigned short size;
17         unsigned long address __attribute__((packed));
18         unsigned short pad;
19 } __attribute__ ((packed));
20
21 extern struct Xgt_desc_struct idt_descr;
22 DECLARE_PER_CPU(struct Xgt_desc_struct, cpu_gdt_descr);
23 DECLARE_PER_CPU(struct desc_struct, cpu_gdt[GDT_ENTRIES]);
24 extern struct Xgt_desc_struct early_gdt_descr;
25
26 static inline struct desc_struct *get_cpu_gdt_table(unsigned int cpu)
27 {
28         return (struct desc_struct *)per_cpu(cpu_gdt_descr, cpu).address;
29 }
30
31 extern struct desc_struct idt_table[];
32 extern void set_intr_gate(unsigned int irq, void * addr);
33
34 static inline void pack_descriptor(__u32 *a, __u32 *b,
35         unsigned long base, unsigned long limit, unsigned char type, unsigned char flags)
36 {
37         *a = ((base & 0xffff) << 16) | (limit & 0xffff);
38         *b = (base & 0xff000000) | ((base & 0xff0000) >> 16) |
39                 (limit & 0x000f0000) | ((type & 0xff) << 8) | ((flags & 0xf) << 20);
40 }
41
42 static inline void pack_gate(__u32 *a, __u32 *b,
43         unsigned long base, unsigned short seg, unsigned char type, unsigned char flags)
44 {
45         *a = (seg << 16) | (base & 0xffff);
46         *b = (base & 0xffff0000) | ((type & 0xff) << 8) | (flags & 0xff);
47 }
48
49 #define DESCTYPE_LDT    0x82    /* present, system, DPL-0, LDT */
50 #define DESCTYPE_TSS    0x89    /* present, system, DPL-0, 32-bit TSS */
51 #define DESCTYPE_TASK   0x85    /* present, system, DPL-0, task gate */
52 #define DESCTYPE_INT    0x8e    /* present, system, DPL-0, interrupt gate */
53 #define DESCTYPE_TRAP   0x8f    /* present, system, DPL-0, trap gate */
54 #define DESCTYPE_DPL3   0x60    /* DPL-3 */
55 #define DESCTYPE_S      0x10    /* !system */
56
57 #ifdef CONFIG_PARAVIRT
58 #include <asm/paravirt.h>
59 #else
60 #define load_TR_desc() native_load_tr_desc()
61 #define load_gdt(dtr) native_load_gdt(dtr)
62 #define load_idt(dtr) native_load_idt(dtr)
63 #define load_tr(tr) __asm__ __volatile("ltr %0"::"m" (tr))
64 #define load_ldt(ldt) __asm__ __volatile("lldt %0"::"m" (ldt))
65
66 #define store_gdt(dtr) native_store_gdt(dtr)
67 #define store_idt(dtr) native_store_idt(dtr)
68 #define store_tr(tr) (tr = native_store_tr())
69 #define store_ldt(ldt) __asm__ ("sldt %0":"=m" (ldt))
70
71 #define load_TLS(t, cpu) native_load_tls(t, cpu)
72 #define set_ldt native_set_ldt
73
74 #define write_ldt_entry(dt, entry, a, b) write_dt_entry(dt, entry, a, b)
75 #define write_gdt_entry(dt, entry, a, b) write_dt_entry(dt, entry, a, b)
76 #define write_idt_entry(dt, entry, a, b) write_dt_entry(dt, entry, a, b)
77 #endif
78
79 static inline void write_dt_entry(struct desc_struct *dt,
80                                   int entry, u32 entry_low, u32 entry_high)
81 {
82         dt[entry].a = entry_low;
83         dt[entry].b = entry_high;
84 }
85
86 static inline void native_set_ldt(const void *addr, unsigned int entries)
87 {
88         if (likely(entries == 0))
89                 __asm__ __volatile__("lldt %w0"::"q" (0));
90         else {
91                 unsigned cpu = smp_processor_id();
92                 __u32 a, b;
93
94                 pack_descriptor(&a, &b, (unsigned long)addr,
95                                 entries * sizeof(struct desc_struct) - 1,
96                                 DESCTYPE_LDT, 0);
97                 write_gdt_entry(get_cpu_gdt_table(cpu), GDT_ENTRY_LDT, a, b);
98                 __asm__ __volatile__("lldt %w0"::"q" (GDT_ENTRY_LDT*8));
99         }
100 }
101
102
103 static inline void native_load_tr_desc(void)
104 {
105         asm volatile("ltr %w0"::"q" (GDT_ENTRY_TSS*8));
106 }
107
108 static inline void native_load_gdt(const struct Xgt_desc_struct *dtr)
109 {
110         asm volatile("lgdt %0"::"m" (*dtr));
111 }
112
113 static inline void native_load_idt(const struct Xgt_desc_struct *dtr)
114 {
115         asm volatile("lidt %0"::"m" (*dtr));
116 }
117
118 static inline void native_store_gdt(struct Xgt_desc_struct *dtr)
119 {
120         asm ("sgdt %0":"=m" (*dtr));
121 }
122
123 static inline void native_store_idt(struct Xgt_desc_struct *dtr)
124 {
125         asm ("sidt %0":"=m" (*dtr));
126 }
127
128 static inline unsigned long native_store_tr(void)
129 {
130         unsigned long tr;
131         asm ("str %0":"=r" (tr));
132         return tr;
133 }
134
135 static inline void native_load_tls(struct thread_struct *t, unsigned int cpu)
136 {
137         unsigned int i;
138         struct desc_struct *gdt = get_cpu_gdt_table(cpu);
139
140         for (i = 0; i < GDT_ENTRY_TLS_ENTRIES; i++)
141                 gdt[GDT_ENTRY_TLS_MIN + i] = t->tls_array[i];
142 }
143
144 static inline void _set_gate(int gate, unsigned int type, void *addr, unsigned short seg)
145 {
146         __u32 a, b;
147         pack_gate(&a, &b, (unsigned long)addr, seg, type, 0);
148         write_idt_entry(idt_table, gate, a, b);
149 }
150
151 static inline void __set_tss_desc(unsigned int cpu, unsigned int entry, const void *addr)
152 {
153         __u32 a, b;
154         pack_descriptor(&a, &b, (unsigned long)addr,
155                         offsetof(struct tss_struct, __cacheline_filler) - 1,
156                         DESCTYPE_TSS, 0);
157         write_gdt_entry(get_cpu_gdt_table(cpu), entry, a, b);
158 }
159
160
161 #define set_tss_desc(cpu,addr) __set_tss_desc(cpu, GDT_ENTRY_TSS, addr)
162
163 #define LDT_entry_a(info) \
164         ((((info)->base_addr & 0x0000ffff) << 16) | ((info)->limit & 0x0ffff))
165
166 #define LDT_entry_b(info) \
167         (((info)->base_addr & 0xff000000) | \
168         (((info)->base_addr & 0x00ff0000) >> 16) | \
169         ((info)->limit & 0xf0000) | \
170         (((info)->read_exec_only ^ 1) << 9) | \
171         ((info)->contents << 10) | \
172         (((info)->seg_not_present ^ 1) << 15) | \
173         ((info)->seg_32bit << 22) | \
174         ((info)->limit_in_pages << 23) | \
175         ((info)->useable << 20) | \
176         0x7000)
177
178 #define LDT_empty(info) (\
179         (info)->base_addr       == 0    && \
180         (info)->limit           == 0    && \
181         (info)->contents        == 0    && \
182         (info)->read_exec_only  == 1    && \
183         (info)->seg_32bit       == 0    && \
184         (info)->limit_in_pages  == 0    && \
185         (info)->seg_not_present == 1    && \
186         (info)->useable         == 0    )
187
188 static inline void clear_LDT(void)
189 {
190         set_ldt(NULL, 0);
191 }
192
193 /*
194  * load one particular LDT into the current CPU
195  */
196 static inline void load_LDT_nolock(mm_context_t *pc)
197 {
198         set_ldt(pc->ldt, pc->size);
199 }
200
201 static inline void load_LDT(mm_context_t *pc)
202 {
203         preempt_disable();
204         load_LDT_nolock(pc);
205         preempt_enable();
206 }
207
208 static inline unsigned long get_desc_base(unsigned long *desc)
209 {
210         unsigned long base;
211         base = ((desc[0] >> 16)  & 0x0000ffff) |
212                 ((desc[1] << 16) & 0x00ff0000) |
213                 (desc[1] & 0xff000000);
214         return base;
215 }
216
217 #else /* __ASSEMBLY__ */
218
219 /*
220  * GET_DESC_BASE reads the descriptor base of the specified segment.
221  *
222  * Args:
223  *    idx - descriptor index
224  *    gdt - GDT pointer
225  *    base - 32bit register to which the base will be written
226  *    lo_w - lo word of the "base" register
227  *    lo_b - lo byte of the "base" register
228  *    hi_b - hi byte of the low word of the "base" register
229  *
230  * Example:
231  *    GET_DESC_BASE(GDT_ENTRY_ESPFIX_SS, %ebx, %eax, %ax, %al, %ah)
232  *    Will read the base address of GDT_ENTRY_ESPFIX_SS and put it into %eax.
233  */
234 #define GET_DESC_BASE(idx, gdt, base, lo_w, lo_b, hi_b) \
235         movb idx*8+4(gdt), lo_b; \
236         movb idx*8+7(gdt), hi_b; \
237         shll $16, base; \
238         movw idx*8+2(gdt), lo_w;
239
240 #endif /* !__ASSEMBLY__ */
241
242 #endif