]> err.no Git - linux-2.6/blob - include/asm-x86/desc_64.h
x86: introduce fill_ldt
[linux-2.6] / include / asm-x86 / desc_64.h
1 /* Written 2000 by Andi Kleen */
2 #ifndef __ARCH_DESC_H
3 #define __ARCH_DESC_H
4
5 #include <linux/threads.h>
6 #include <asm/ldt.h>
7
8 #ifndef __ASSEMBLY__
9
10 #include <linux/string.h>
11 #include <linux/smp.h>
12 #include <asm/desc_defs.h>
13
14 #include <asm/segment.h>
15 #include <asm/mmu.h>
16
17 extern struct desc_struct cpu_gdt_table[GDT_ENTRIES];
18
19 #define load_TR_desc() asm volatile("ltr %w0"::"r" (GDT_ENTRY_TSS*8))
20 #define load_LDT_desc() asm volatile("lldt %w0"::"r" (GDT_ENTRY_LDT*8))
21 #define clear_LDT()  asm volatile("lldt %w0"::"r" (0))
22
23 static inline unsigned long __store_tr(void)
24 {
25        unsigned long tr;
26
27        asm volatile ("str %w0":"=r" (tr));
28        return tr;
29 }
30
31 #define store_tr(tr) (tr) = __store_tr()
32
33 extern gate_desc idt_table[];
34 extern struct desc_ptr cpu_gdt_descr[];
35
36 static inline void write_ldt_entry(struct desc_struct *ldt,
37                                    int entry, u32 entry_low, u32 entry_high)
38 {
39         __u32 *lp = (__u32 *)((entry << 3) + (char *)ldt);
40
41         lp[0] = entry_low;
42         lp[1] = entry_high;
43 }
44
45 /* the cpu gdt accessor */
46 #define get_cpu_gdt_table(x) ((struct desc_struct *)cpu_gdt_descr[x].address)
47
48 static inline void load_gdt(const struct desc_ptr *ptr)
49 {
50         asm volatile("lgdt %w0"::"m" (*ptr));
51 }
52
53 static inline void store_gdt(struct desc_ptr *ptr)
54 {
55        asm("sgdt %w0":"=m" (*ptr));
56 }
57
58 static inline void _set_gate(void *adr, unsigned type, unsigned long func,
59                              unsigned dpl, unsigned ist)
60 {
61         gate_desc s;
62
63         s.offset_low = PTR_LOW(func);
64         s.segment = __KERNEL_CS;
65         s.ist = ist;
66         s.p = 1;
67         s.dpl = dpl;
68         s.zero0 = 0;
69         s.zero1 = 0;
70         s.type = type;
71         s.offset_middle = PTR_MIDDLE(func);
72         s.offset_high = PTR_HIGH(func);
73         /*
74          * does not need to be atomic because it is only done once at
75          * setup time
76          */
77         memcpy(adr, &s, 16);
78 }
79
80 static inline void set_intr_gate(int nr, void *func)
81 {
82         BUG_ON((unsigned)nr > 0xFF);
83         _set_gate(&idt_table[nr], GATE_INTERRUPT, (unsigned long) func, 0, 0);
84 }
85
86 static inline void set_intr_gate_ist(int nr, void *func, unsigned ist)
87 {
88         BUG_ON((unsigned)nr > 0xFF);
89         _set_gate(&idt_table[nr], GATE_INTERRUPT, (unsigned long) func, 0, ist);
90 }
91
92 static inline void set_system_gate(int nr, void *func)
93 {
94         BUG_ON((unsigned)nr > 0xFF);
95         _set_gate(&idt_table[nr], GATE_INTERRUPT, (unsigned long) func, 3, 0);
96 }
97
98 static inline void set_system_gate_ist(int nr, void *func, unsigned ist)
99 {
100         _set_gate(&idt_table[nr], GATE_INTERRUPT, (unsigned long) func, 3, ist);
101 }
102
103 static inline void load_idt(const struct desc_ptr *ptr)
104 {
105         asm volatile("lidt %w0"::"m" (*ptr));
106 }
107
108 static inline void store_idt(struct desc_ptr *dtr)
109 {
110        asm("sidt %w0":"=m" (*dtr));
111 }
112
113 static inline void set_tssldt_descriptor(void *ptr, unsigned long tss,
114                                          unsigned type, unsigned size)
115 {
116         struct ldttss_desc64 d;
117
118         memset(&d, 0, sizeof(d));
119         d.limit0 = size & 0xFFFF;
120         d.base0 = PTR_LOW(tss);
121         d.base1 = PTR_MIDDLE(tss) & 0xFF;
122         d.type = type;
123         d.p = 1;
124         d.limit1 = (size >> 16) & 0xF;
125         d.base2 = (PTR_MIDDLE(tss) >> 8) & 0xFF;
126         d.base3 = PTR_HIGH(tss);
127         memcpy(ptr, &d, 16);
128 }
129
130 static inline void set_tss_desc(unsigned cpu, void *addr)
131 {
132         /*
133          * sizeof(unsigned long) coming from an extra "long" at the end
134          * of the iobitmap. See tss_struct definition in processor.h
135          *
136          * -1? seg base+limit should be pointing to the address of the
137          * last valid byte
138          */
139         set_tssldt_descriptor(&get_cpu_gdt_table(cpu)[GDT_ENTRY_TSS],
140                 (unsigned long)addr, DESC_TSS,
141                 IO_BITMAP_OFFSET + IO_BITMAP_BYTES + sizeof(unsigned long) - 1);
142 }
143
144 static inline void set_ldt_desc(unsigned cpu, void *addr, int size)
145 {
146         set_tssldt_descriptor(&get_cpu_gdt_table(cpu)[GDT_ENTRY_LDT],
147                              (unsigned long)addr, DESC_LDT, size * 8 - 1);
148 }
149
150 #define LDT_empty(info) (\
151         (info)->base_addr       == 0    && \
152         (info)->limit           == 0    && \
153         (info)->contents        == 0    && \
154         (info)->read_exec_only  == 1    && \
155         (info)->seg_32bit       == 0    && \
156         (info)->limit_in_pages  == 0    && \
157         (info)->seg_not_present == 1    && \
158         (info)->useable         == 0    && \
159         (info)->lm              == 0)
160
161 static inline void load_TLS(struct thread_struct *t, unsigned int cpu)
162 {
163         unsigned int i;
164         u64 *gdt = (u64 *)(get_cpu_gdt_table(cpu) + GDT_ENTRY_TLS_MIN);
165
166         for (i = 0; i < GDT_ENTRY_TLS_ENTRIES; i++)
167                 gdt[i] = t->tls_array[i];
168 }
169
170 /*
171  * load one particular LDT into the current CPU
172  */
173 static inline void load_LDT_nolock(mm_context_t *pc, int cpu)
174 {
175         int count = pc->size;
176
177         if (likely(!count)) {
178                 clear_LDT();
179                 return;
180         }
181
182         set_ldt_desc(cpu, pc->ldt, count);
183         load_LDT_desc();
184 }
185
186 static inline void load_LDT(mm_context_t *pc)
187 {
188         int cpu = get_cpu();
189
190         load_LDT_nolock(pc, cpu);
191         put_cpu();
192 }
193
194 extern struct desc_ptr idt_descr;
195
196 static inline unsigned long get_desc_base(const void *ptr)
197 {
198         const u32 *desc = ptr;
199         unsigned long base;
200         base = ((desc[0] >> 16)  & 0x0000ffff) |
201                 ((desc[1] << 16) & 0x00ff0000) |
202                 (desc[1] & 0xff000000);
203         return base;
204 }
205
206 #endif /* !__ASSEMBLY__ */
207
208 #endif