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