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 | |
---|
13 | #include <asm/segment.h> |
---|
14 | #include <asm/mmu.h> |
---|
15 | |
---|
16 | // 8 byte segment descriptor |
---|
17 | struct desc_struct { |
---|
18 | u16 limit0; |
---|
19 | u16 base0; |
---|
20 | unsigned base1 : 8, type : 4, s : 1, dpl : 2, p : 1; |
---|
21 | unsigned limit : 4, avl : 1, l : 1, d : 1, g : 1, base2 : 8; |
---|
22 | } __attribute__((packed)); |
---|
23 | |
---|
24 | struct n_desc_struct { |
---|
25 | unsigned int a,b; |
---|
26 | }; |
---|
27 | |
---|
28 | enum { |
---|
29 | GATE_INTERRUPT = 0xE, |
---|
30 | GATE_TRAP = 0xF, |
---|
31 | GATE_CALL = 0xC, |
---|
32 | }; |
---|
33 | |
---|
34 | // 16byte gate |
---|
35 | struct gate_struct { |
---|
36 | u16 offset_low; |
---|
37 | u16 segment; |
---|
38 | unsigned ist : 3, zero0 : 5, type : 5, dpl : 2, p : 1; |
---|
39 | u16 offset_middle; |
---|
40 | u32 offset_high; |
---|
41 | u32 zero1; |
---|
42 | } __attribute__((packed)); |
---|
43 | |
---|
44 | #define PTR_LOW(x) ((unsigned long)(x) & 0xFFFF) |
---|
45 | #define PTR_MIDDLE(x) (((unsigned long)(x) >> 16) & 0xFFFF) |
---|
46 | #define PTR_HIGH(x) ((unsigned long)(x) >> 32) |
---|
47 | |
---|
48 | enum { |
---|
49 | DESC_TSS = 0x9, |
---|
50 | DESC_LDT = 0x2, |
---|
51 | }; |
---|
52 | |
---|
53 | // LDT or TSS descriptor in the GDT. 16 bytes. |
---|
54 | struct ldttss_desc { |
---|
55 | u16 limit0; |
---|
56 | u16 base0; |
---|
57 | unsigned base1 : 8, type : 5, dpl : 2, p : 1; |
---|
58 | unsigned limit1 : 4, zero0 : 3, g : 1, base2 : 8; |
---|
59 | u32 base3; |
---|
60 | u32 zero1; |
---|
61 | } __attribute__((packed)); |
---|
62 | |
---|
63 | struct desc_ptr { |
---|
64 | unsigned short size; |
---|
65 | unsigned long address; |
---|
66 | } __attribute__((packed)) ; |
---|
67 | |
---|
68 | extern struct desc_ptr idt_descr, cpu_gdt_descr[NR_CPUS]; |
---|
69 | |
---|
70 | extern struct desc_struct cpu_gdt_table[GDT_ENTRIES]; |
---|
71 | |
---|
72 | #define load_TR_desc() asm volatile("ltr %w0"::"r" (GDT_ENTRY_TSS*8)) |
---|
73 | #define load_LDT_desc() asm volatile("lldt %w0"::"r" (GDT_ENTRY_LDT*8)) |
---|
74 | |
---|
75 | static inline void clear_LDT(void) |
---|
76 | { |
---|
77 | int cpu = get_cpu(); |
---|
78 | |
---|
79 | /* |
---|
80 | * NB. We load the default_ldt for lcall7/27 handling on demand, as |
---|
81 | * it slows down context switching. Noone uses it anyway. |
---|
82 | */ |
---|
83 | cpu = cpu; /* XXX avoid compiler warning */ |
---|
84 | xen_set_ldt(0UL, 0); |
---|
85 | put_cpu(); |
---|
86 | } |
---|
87 | |
---|
88 | /* |
---|
89 | * This is the ldt that every process will get unless we need |
---|
90 | * something other than this. |
---|
91 | */ |
---|
92 | extern struct desc_struct default_ldt[]; |
---|
93 | #ifndef CONFIG_X86_NO_IDT |
---|
94 | extern struct gate_struct idt_table[]; |
---|
95 | #endif |
---|
96 | extern struct desc_ptr cpu_gdt_descr[]; |
---|
97 | |
---|
98 | /* the cpu gdt accessor */ |
---|
99 | #define cpu_gdt(_cpu) ((struct desc_struct *)cpu_gdt_descr[_cpu].address) |
---|
100 | |
---|
101 | static inline void _set_gate(void *adr, unsigned type, unsigned long func, unsigned dpl, unsigned ist) |
---|
102 | { |
---|
103 | struct gate_struct s; |
---|
104 | s.offset_low = PTR_LOW(func); |
---|
105 | s.segment = __KERNEL_CS; |
---|
106 | s.ist = ist; |
---|
107 | s.p = 1; |
---|
108 | s.dpl = dpl; |
---|
109 | s.zero0 = 0; |
---|
110 | s.zero1 = 0; |
---|
111 | s.type = type; |
---|
112 | s.offset_middle = PTR_MIDDLE(func); |
---|
113 | s.offset_high = PTR_HIGH(func); |
---|
114 | /* does not need to be atomic because it is only done once at setup time */ |
---|
115 | memcpy(adr, &s, 16); |
---|
116 | } |
---|
117 | |
---|
118 | #ifndef CONFIG_X86_NO_IDT |
---|
119 | static inline void set_intr_gate(int nr, void *func) |
---|
120 | { |
---|
121 | BUG_ON((unsigned)nr > 0xFF); |
---|
122 | _set_gate(&idt_table[nr], GATE_INTERRUPT, (unsigned long) func, 0, 0); |
---|
123 | } |
---|
124 | |
---|
125 | static inline void set_intr_gate_ist(int nr, void *func, unsigned ist) |
---|
126 | { |
---|
127 | BUG_ON((unsigned)nr > 0xFF); |
---|
128 | _set_gate(&idt_table[nr], GATE_INTERRUPT, (unsigned long) func, 0, ist); |
---|
129 | } |
---|
130 | |
---|
131 | static inline void set_system_gate(int nr, void *func) |
---|
132 | { |
---|
133 | BUG_ON((unsigned)nr > 0xFF); |
---|
134 | _set_gate(&idt_table[nr], GATE_INTERRUPT, (unsigned long) func, 3, 0); |
---|
135 | } |
---|
136 | |
---|
137 | static inline void set_system_gate_ist(int nr, void *func, unsigned ist) |
---|
138 | { |
---|
139 | _set_gate(&idt_table[nr], GATE_INTERRUPT, (unsigned long) func, 3, ist); |
---|
140 | } |
---|
141 | #endif |
---|
142 | |
---|
143 | static inline void set_tssldt_descriptor(void *ptr, unsigned long tss, unsigned type, |
---|
144 | unsigned size) |
---|
145 | { |
---|
146 | struct ldttss_desc d; |
---|
147 | memset(&d,0,sizeof(d)); |
---|
148 | d.limit0 = size & 0xFFFF; |
---|
149 | d.base0 = PTR_LOW(tss); |
---|
150 | d.base1 = PTR_MIDDLE(tss) & 0xFF; |
---|
151 | d.type = type; |
---|
152 | d.p = 1; |
---|
153 | d.limit1 = (size >> 16) & 0xF; |
---|
154 | d.base2 = (PTR_MIDDLE(tss) >> 8) & 0xFF; |
---|
155 | d.base3 = PTR_HIGH(tss); |
---|
156 | memcpy(ptr, &d, 16); |
---|
157 | } |
---|
158 | |
---|
159 | #ifndef CONFIG_X86_NO_TSS |
---|
160 | static inline void set_tss_desc(unsigned cpu, void *addr) |
---|
161 | { |
---|
162 | /* |
---|
163 | * sizeof(unsigned long) coming from an extra "long" at the end |
---|
164 | * of the iobitmap. See tss_struct definition in processor.h |
---|
165 | * |
---|
166 | * -1? seg base+limit should be pointing to the address of the |
---|
167 | * last valid byte |
---|
168 | */ |
---|
169 | set_tssldt_descriptor(&cpu_gdt(cpu)[GDT_ENTRY_TSS], |
---|
170 | (unsigned long)addr, DESC_TSS, |
---|
171 | IO_BITMAP_OFFSET + IO_BITMAP_BYTES + sizeof(unsigned long) - 1); |
---|
172 | } |
---|
173 | #endif |
---|
174 | |
---|
175 | static inline void set_ldt_desc(unsigned cpu, void *addr, int size) |
---|
176 | { |
---|
177 | set_tssldt_descriptor(&cpu_gdt(cpu)[GDT_ENTRY_LDT], (unsigned long)addr, |
---|
178 | DESC_LDT, size * 8 - 1); |
---|
179 | } |
---|
180 | |
---|
181 | static inline void set_seg_base(unsigned cpu, int entry, void *base) |
---|
182 | { |
---|
183 | struct desc_struct *d = &cpu_gdt(cpu)[entry]; |
---|
184 | u32 addr = (u32)(u64)base; |
---|
185 | BUG_ON((u64)base >> 32); |
---|
186 | d->base0 = addr & 0xffff; |
---|
187 | d->base1 = (addr >> 16) & 0xff; |
---|
188 | d->base2 = (addr >> 24) & 0xff; |
---|
189 | } |
---|
190 | |
---|
191 | #define LDT_entry_a(info) \ |
---|
192 | ((((info)->base_addr & 0x0000ffff) << 16) | ((info)->limit & 0x0ffff)) |
---|
193 | /* Don't allow setting of the lm bit. It is useless anyways because |
---|
194 | 64bit system calls require __USER_CS. */ |
---|
195 | #define LDT_entry_b(info) \ |
---|
196 | (((info)->base_addr & 0xff000000) | \ |
---|
197 | (((info)->base_addr & 0x00ff0000) >> 16) | \ |
---|
198 | ((info)->limit & 0xf0000) | \ |
---|
199 | (((info)->read_exec_only ^ 1) << 9) | \ |
---|
200 | ((info)->contents << 10) | \ |
---|
201 | (((info)->seg_not_present ^ 1) << 15) | \ |
---|
202 | ((info)->seg_32bit << 22) | \ |
---|
203 | ((info)->limit_in_pages << 23) | \ |
---|
204 | ((info)->useable << 20) | \ |
---|
205 | /* ((info)->lm << 21) | */ \ |
---|
206 | 0x7000) |
---|
207 | |
---|
208 | #define LDT_empty(info) (\ |
---|
209 | (info)->base_addr == 0 && \ |
---|
210 | (info)->limit == 0 && \ |
---|
211 | (info)->contents == 0 && \ |
---|
212 | (info)->read_exec_only == 1 && \ |
---|
213 | (info)->seg_32bit == 0 && \ |
---|
214 | (info)->limit_in_pages == 0 && \ |
---|
215 | (info)->seg_not_present == 1 && \ |
---|
216 | (info)->useable == 0 && \ |
---|
217 | (info)->lm == 0) |
---|
218 | |
---|
219 | #if TLS_SIZE != 24 |
---|
220 | # error update this code. |
---|
221 | #endif |
---|
222 | |
---|
223 | static inline void load_TLS(struct thread_struct *t, unsigned int cpu) |
---|
224 | { |
---|
225 | #if 0 |
---|
226 | u64 *gdt = (u64 *)(cpu_gdt(cpu) + GDT_ENTRY_TLS_MIN); |
---|
227 | gdt[0] = t->tls_array[0]; |
---|
228 | gdt[1] = t->tls_array[1]; |
---|
229 | gdt[2] = t->tls_array[2]; |
---|
230 | #endif |
---|
231 | #define C(i) \ |
---|
232 | HYPERVISOR_update_descriptor(virt_to_machine(&cpu_gdt(cpu)[GDT_ENTRY_TLS_MIN + i]), t->tls_array[i]) |
---|
233 | |
---|
234 | C(0); C(1); C(2); |
---|
235 | #undef C |
---|
236 | } |
---|
237 | |
---|
238 | /* |
---|
239 | * load one particular LDT into the current CPU |
---|
240 | */ |
---|
241 | static inline void load_LDT_nolock (mm_context_t *pc, int cpu) |
---|
242 | { |
---|
243 | void *segments = pc->ldt; |
---|
244 | int count = pc->size; |
---|
245 | |
---|
246 | if (likely(!count)) |
---|
247 | segments = NULL; |
---|
248 | |
---|
249 | xen_set_ldt((unsigned long)segments, count); |
---|
250 | } |
---|
251 | |
---|
252 | static inline void load_LDT(mm_context_t *pc) |
---|
253 | { |
---|
254 | int cpu = get_cpu(); |
---|
255 | load_LDT_nolock(pc, cpu); |
---|
256 | put_cpu(); |
---|
257 | } |
---|
258 | |
---|
259 | extern struct desc_ptr idt_descr; |
---|
260 | |
---|
261 | #endif /* !__ASSEMBLY__ */ |
---|
262 | |
---|
263 | #endif |
---|