]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
b0d623f7 | 2 | * Copyright (c) 2000-2009 Apple Inc. All rights reserved. |
1c79356b | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
1c79356b | 5 | * |
2d21ac55 A |
6 | * This file contains Original Code and/or Modifications of Original Code |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. The rights granted to you under the License | |
10 | * may not be used to create, or enable the creation or redistribution of, | |
11 | * unlawful or unlicensed copies of an Apple operating system, or to | |
12 | * circumvent, violate, or enable the circumvention or violation of, any | |
13 | * terms of an Apple operating system software license agreement. | |
8f6c56a5 | 14 | * |
2d21ac55 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
17 | * | |
18 | * The Original Code and all software distributed under the License are | |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
23 | * Please see the License for the specific language governing rights and | |
24 | * limitations under the License. | |
8f6c56a5 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b A |
27 | */ |
28 | /* | |
29 | * @OSF_COPYRIGHT@ | |
30 | */ | |
31 | /* | |
32 | * Mach Operating System | |
33 | * Copyright (c) 1991,1990 Carnegie Mellon University | |
34 | * All Rights Reserved. | |
35 | * | |
36 | * Permission to use, copy, modify and distribute this software and its | |
37 | * documentation is hereby granted, provided that both the copyright | |
38 | * notice and this permission notice appear in all copies of the | |
39 | * software, derivative works or modified versions, and any portions | |
40 | * thereof, and that both notices appear in supporting documentation. | |
41 | * | |
42 | * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" | |
43 | * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR | |
44 | * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. | |
45 | * | |
46 | * Carnegie Mellon requests users of this software to return to | |
47 | * | |
48 | * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU | |
49 | * School of Computer Science | |
50 | * Carnegie Mellon University | |
51 | * Pittsburgh PA 15213-3890 | |
52 | * | |
53 | * any improvements or extensions that they make and grant Carnegie Mellon | |
54 | * the rights to redistribute these changes. | |
55 | */ | |
56 | ||
57 | /* | |
58 | */ | |
59 | ||
1c79356b | 60 | #include <kern/cpu_number.h> |
91447636 | 61 | #include <kern/kalloc.h> |
1c79356b | 62 | #include <kern/cpu_data.h> |
0c530ab8 | 63 | #include <mach/mach_types.h> |
1c79356b | 64 | #include <mach/machine.h> |
7e4a7d39 | 65 | #include <kern/etimer.h> |
0c530ab8 | 66 | #include <mach/vm_map.h> |
b0d623f7 | 67 | #include <mach/machine/vm_param.h> |
1c79356b | 68 | #include <vm/vm_kern.h> |
0c530ab8 | 69 | #include <vm/vm_map.h> |
1c79356b | 70 | |
1c79356b | 71 | #include <i386/lock.h> |
b0d623f7 | 72 | #include <i386/mp_desc.h> |
1c79356b | 73 | #include <i386/misc_protos.h> |
55e303ae | 74 | #include <i386/mp.h> |
91447636 | 75 | #include <i386/pmap.h> |
b0d623f7 | 76 | #if CONFIG_MCA |
2d21ac55 | 77 | #include <i386/machine_check.h> |
b0d623f7 | 78 | #endif |
1c79356b A |
79 | |
80 | #include <kern/misc_protos.h> | |
81 | ||
82 | #include <mach_kdb.h> | |
83 | ||
b0d623f7 A |
84 | #ifdef __x86_64__ |
85 | #define K_INTR_GATE (ACC_P|ACC_PL_K|ACC_INTR_GATE) | |
86 | #define U_INTR_GATE (ACC_P|ACC_PL_U|ACC_INTR_GATE) | |
87 | ||
88 | // Declare macros that will declare the externs | |
89 | #define TRAP(n, name) extern void *name ; | |
90 | #define TRAP_ERR(n, name) extern void *name ; | |
91 | #define TRAP_SPC(n, name) extern void *name ; | |
92 | #define TRAP_IST(n, name) extern void *name ; | |
93 | #define INTERRUPT(n) extern void *_intr_ ## n ; | |
94 | #define USER_TRAP(n, name) extern void *name ; | |
95 | #define USER_TRAP_SPC(n, name) extern void *name ; | |
96 | ||
97 | // Include the table to declare the externs | |
98 | #include "../x86_64/idt_table.h" | |
99 | ||
100 | // Undef the macros, then redefine them so we can declare the table | |
101 | #undef TRAP | |
102 | #undef TRAP_ERR | |
103 | #undef TRAP_SPC | |
104 | #undef TRAP_IST | |
105 | #undef INTERRUPT | |
106 | #undef USER_TRAP | |
107 | #undef USER_TRAP_SPC | |
108 | ||
109 | #define TRAP(n, name) \ | |
6d2010ae | 110 | [n] = { \ |
b0d623f7 A |
111 | (uintptr_t)&name, \ |
112 | KERNEL64_CS, \ | |
113 | 0, \ | |
114 | K_INTR_GATE, \ | |
115 | 0 \ | |
116 | }, | |
117 | ||
118 | #define TRAP_ERR TRAP | |
119 | #define TRAP_SPC TRAP | |
120 | ||
121 | #define TRAP_IST(n, name) \ | |
6d2010ae | 122 | [n] = { \ |
b0d623f7 A |
123 | (uintptr_t)&name, \ |
124 | KERNEL64_CS, \ | |
125 | 1, \ | |
126 | K_INTR_GATE, \ | |
127 | 0 \ | |
128 | }, | |
129 | ||
130 | #define INTERRUPT(n) \ | |
6d2010ae | 131 | [n] = { \ |
b0d623f7 A |
132 | (uintptr_t)&_intr_ ## n,\ |
133 | KERNEL64_CS, \ | |
134 | 0, \ | |
135 | K_INTR_GATE, \ | |
136 | 0 \ | |
137 | }, | |
138 | ||
139 | #define USER_TRAP(n, name) \ | |
6d2010ae | 140 | [n] = { \ |
b0d623f7 A |
141 | (uintptr_t)&name, \ |
142 | KERNEL64_CS, \ | |
143 | 0, \ | |
144 | U_INTR_GATE, \ | |
145 | 0 \ | |
146 | }, | |
147 | ||
148 | #define USER_TRAP_SPC USER_TRAP | |
149 | ||
150 | ||
151 | // Declare the table using the macros we just set up | |
152 | struct fake_descriptor64 master_idt64[IDTSZ] __attribute__ ((aligned (4096))) = { | |
153 | #include "../x86_64/idt_table.h" | |
154 | }; | |
155 | #endif | |
156 | ||
1c79356b A |
157 | /* |
158 | * The i386 needs an interrupt stack to keep the PCB stack from being | |
159 | * overrun by interrupts. All interrupt stacks MUST lie at lower addresses | |
160 | * than any thread`s kernel stack. | |
161 | */ | |
162 | ||
1c79356b A |
163 | /* |
164 | * First cpu`s interrupt stack. | |
165 | */ | |
0c530ab8 A |
166 | extern uint32_t low_intstack[]; /* bottom */ |
167 | extern uint32_t low_eintstack[]; /* top */ | |
1c79356b A |
168 | |
169 | /* | |
91447636 A |
170 | * Per-cpu data area pointers. |
171 | * The master cpu (cpu 0) has its data area statically allocated; | |
172 | * others are allocated dynamically and this array is updated at runtime. | |
1c79356b | 173 | */ |
593a1d5f | 174 | cpu_data_t cpu_data_master = { |
b0d623f7 | 175 | .cpu_this = &cpu_data_master, |
6d2010ae | 176 | .cpu_nanotime = &pal_rtc_nanotime_info, |
b0d623f7 A |
177 | .cpu_int_stack_top = (vm_offset_t) low_eintstack, |
178 | #ifdef __i386__ | |
179 | .cpu_is64bit = FALSE, | |
180 | #else | |
181 | .cpu_is64bit = TRUE | |
182 | #endif | |
183 | }; | |
6d2010ae | 184 | cpu_data_t *cpu_data_ptr[MAX_CPUS] = { [0] = &cpu_data_master }; |
91447636 | 185 | |
b0d623f7 | 186 | decl_simple_lock_data(,ncpus_lock); /* protects real_ncpus */ |
91447636 A |
187 | unsigned int real_ncpus = 1; |
188 | unsigned int max_ncpus = MAX_CPUS; | |
1c79356b | 189 | |
b0d623f7 | 190 | #ifdef __i386__ |
0c530ab8 A |
191 | extern void *hi_remap_text; |
192 | #define HI_TEXT(lo_text) \ | |
193 | (((uint32_t)&lo_text - (uint32_t)&hi_remap_text) + HIGH_MEM_BASE) | |
194 | ||
2d21ac55 | 195 | extern void hi_sysenter(void); |
b0d623f7 A |
196 | |
197 | typedef struct { | |
198 | uint16_t length; | |
199 | uint32_t offset[2]; | |
200 | } __attribute__((__packed__)) table_descriptor64_t; | |
201 | ||
202 | extern table_descriptor64_t gdtptr64; | |
203 | extern table_descriptor64_t idtptr64; | |
204 | #endif | |
2d21ac55 A |
205 | extern void hi64_sysenter(void); |
206 | extern void hi64_syscall(void); | |
0c530ab8 | 207 | |
b0d623f7 A |
208 | #if defined(__x86_64__) && !defined(UBER64) |
209 | #define UBER64(x) ((uintptr_t)x) | |
210 | #endif | |
211 | ||
1c79356b A |
212 | /* |
213 | * Multiprocessor i386/i486 systems use a separate copy of the | |
214 | * GDT, IDT, LDT, and kernel TSS per processor. The first three | |
215 | * are separate to avoid lock contention: the i386 uses locked | |
216 | * memory cycles to access the descriptor tables. The TSS is | |
217 | * separate since each processor needs its own kernel stack, | |
218 | * and since using a TSS marks it busy. | |
219 | */ | |
220 | ||
1c79356b A |
221 | /* |
222 | * Allocate and initialize the per-processor descriptor tables. | |
223 | */ | |
224 | ||
225 | struct fake_descriptor ldt_desc_pattern = { | |
226 | (unsigned int) 0, | |
0c530ab8 | 227 | LDTSZ_MIN * sizeof(struct fake_descriptor) - 1, |
1c79356b A |
228 | 0, |
229 | ACC_P|ACC_PL_K|ACC_LDT | |
230 | }; | |
0c530ab8 | 231 | |
1c79356b A |
232 | struct fake_descriptor tss_desc_pattern = { |
233 | (unsigned int) 0, | |
0c530ab8 | 234 | sizeof(struct i386_tss) - 1, |
1c79356b A |
235 | 0, |
236 | ACC_P|ACC_PL_K|ACC_TSS | |
237 | }; | |
238 | ||
239 | struct fake_descriptor cpudata_desc_pattern = { | |
240 | (unsigned int) 0, | |
241 | sizeof(cpu_data_t)-1, | |
242 | SZ_32, | |
243 | ACC_P|ACC_PL_K|ACC_DATA_W | |
244 | }; | |
245 | ||
0c530ab8 A |
246 | struct fake_descriptor userwindow_desc_pattern = { |
247 | (unsigned int) 0, | |
248 | ((NBPDE * NCOPY_WINDOWS) / PAGE_SIZE) - 1, | |
249 | SZ_32 | SZ_G, | |
250 | ACC_P|ACC_PL_U|ACC_DATA_W | |
251 | }; | |
252 | ||
253 | struct fake_descriptor physwindow_desc_pattern = { | |
254 | (unsigned int) 0, | |
255 | PAGE_SIZE - 1, | |
256 | SZ_32, | |
257 | ACC_P|ACC_PL_K|ACC_DATA_W | |
258 | }; | |
259 | ||
260 | /* | |
261 | * This is the expanded, 64-bit variant of the kernel LDT descriptor. | |
262 | * When switching to 64-bit mode this replaces KERNEL_LDT entry | |
263 | * and the following empty slot. This enables the LDT to be referenced | |
264 | * in the uber-space remapping window on the kernel. | |
265 | */ | |
266 | struct fake_descriptor64 kernel_ldt_desc64 = { | |
b0d623f7 | 267 | 0, |
0c530ab8 A |
268 | LDTSZ_MIN*sizeof(struct fake_descriptor)-1, |
269 | 0, | |
270 | ACC_P|ACC_PL_K|ACC_LDT, | |
271 | 0 | |
272 | }; | |
273 | ||
274 | /* | |
275 | * This is the expanded, 64-bit variant of the kernel TSS descriptor. | |
276 | * It is follows pattern of the KERNEL_LDT. | |
277 | */ | |
278 | struct fake_descriptor64 kernel_tss_desc64 = { | |
b0d623f7 | 279 | 0, |
0c530ab8 A |
280 | sizeof(struct x86_64_tss)-1, |
281 | 0, | |
282 | ACC_P|ACC_PL_K|ACC_TSS, | |
283 | 0 | |
284 | }; | |
285 | ||
b0d623f7 A |
286 | /* |
287 | * Convert a descriptor from fake to real format. | |
288 | * | |
289 | * Fake descriptor format: | |
290 | * bytes 0..3 base 31..0 | |
291 | * bytes 4..5 limit 15..0 | |
292 | * byte 6 access byte 2 | limit 19..16 | |
293 | * byte 7 access byte 1 | |
294 | * | |
295 | * Real descriptor format: | |
296 | * bytes 0..1 limit 15..0 | |
297 | * bytes 2..3 base 15..0 | |
298 | * byte 4 base 23..16 | |
299 | * byte 5 access byte 1 | |
300 | * byte 6 access byte 2 | limit 19..16 | |
301 | * byte 7 base 31..24 | |
302 | * | |
303 | * Fake gate format: | |
304 | * bytes 0..3 offset | |
305 | * bytes 4..5 selector | |
306 | * byte 6 word count << 4 (to match fake descriptor) | |
307 | * byte 7 access byte 1 | |
308 | * | |
309 | * Real gate format: | |
310 | * bytes 0..1 offset 15..0 | |
311 | * bytes 2..3 selector | |
312 | * byte 4 word count | |
313 | * byte 5 access byte 1 | |
314 | * bytes 6..7 offset 31..16 | |
315 | */ | |
316 | void | |
317 | fix_desc(void *d, int num_desc) { | |
318 | //early_kprintf("fix_desc(%x, %x)\n", d, num_desc); | |
319 | uint8_t *desc = (uint8_t*) d; | |
320 | ||
321 | do { | |
322 | if ((desc[7] & 0x14) == 0x04) { /* gate */ | |
323 | uint32_t offset; | |
324 | uint16_t selector; | |
325 | uint8_t wordcount; | |
326 | uint8_t acc; | |
327 | ||
328 | offset = *((uint32_t*)(desc)); | |
329 | selector = *((uint32_t*)(desc+4)); | |
330 | wordcount = desc[6] >> 4; | |
331 | acc = desc[7]; | |
332 | ||
333 | *((uint16_t*)desc) = offset & 0xFFFF; | |
334 | *((uint16_t*)(desc+2)) = selector; | |
335 | desc[4] = wordcount; | |
336 | desc[5] = acc; | |
337 | *((uint16_t*)(desc+6)) = offset >> 16; | |
338 | ||
339 | } else { /* descriptor */ | |
340 | uint32_t base; | |
341 | uint16_t limit; | |
342 | uint8_t acc1, acc2; | |
343 | ||
344 | base = *((uint32_t*)(desc)); | |
345 | limit = *((uint16_t*)(desc+4)); | |
346 | acc2 = desc[6]; | |
347 | acc1 = desc[7]; | |
348 | ||
349 | *((uint16_t*)(desc)) = limit; | |
350 | *((uint16_t*)(desc+2)) = base & 0xFFFF; | |
351 | desc[4] = (base >> 16) & 0xFF; | |
352 | desc[5] = acc1; | |
353 | desc[6] = acc2; | |
354 | desc[7] = base >> 24; | |
355 | } | |
356 | desc += 8; | |
357 | } while (--num_desc); | |
358 | } | |
359 | ||
360 | void | |
361 | fix_desc64(void *descp, int count) | |
362 | { | |
363 | struct fake_descriptor64 *fakep; | |
364 | union { | |
365 | struct real_gate64 gate; | |
366 | struct real_descriptor64 desc; | |
367 | } real; | |
368 | int i; | |
369 | ||
370 | fakep = (struct fake_descriptor64 *) descp; | |
371 | ||
372 | for (i = 0; i < count; i++, fakep++) { | |
373 | /* | |
374 | * Construct the real decriptor locally. | |
375 | */ | |
376 | ||
377 | bzero((void *) &real, sizeof(real)); | |
378 | ||
379 | switch (fakep->access & ACC_TYPE) { | |
380 | case 0: | |
381 | break; | |
382 | case ACC_CALL_GATE: | |
383 | case ACC_INTR_GATE: | |
384 | case ACC_TRAP_GATE: | |
6d2010ae | 385 | real.gate.offset_low16 = (uint16_t)(fakep->offset64 & 0xFFFF); |
b0d623f7 A |
386 | real.gate.selector16 = fakep->lim_or_seg & 0xFFFF; |
387 | real.gate.IST = fakep->size_or_IST & 0x7; | |
388 | real.gate.access8 = fakep->access; | |
6d2010ae | 389 | real.gate.offset_high16 = (uint16_t)((fakep->offset64>>16) & 0xFFFF); |
b0d623f7 A |
390 | real.gate.offset_top32 = (uint32_t)(fakep->offset64>>32); |
391 | break; | |
392 | default: /* Otherwise */ | |
393 | real.desc.limit_low16 = fakep->lim_or_seg & 0xFFFF; | |
6d2010ae A |
394 | real.desc.base_low16 = (uint16_t)(fakep->offset64 & 0xFFFF); |
395 | real.desc.base_med8 = (uint8_t)((fakep->offset64 >> 16) & 0xFF); | |
b0d623f7 A |
396 | real.desc.access8 = fakep->access; |
397 | real.desc.limit_high4 = (fakep->lim_or_seg >> 16) & 0xFF; | |
398 | real.desc.granularity4 = fakep->size_or_IST; | |
6d2010ae | 399 | real.desc.base_high8 = (uint8_t)((fakep->offset64 >> 24) & 0xFF); |
b0d623f7 A |
400 | real.desc.base_top32 = (uint32_t)(fakep->offset64>>32); |
401 | } | |
402 | ||
403 | /* | |
404 | * Now copy back over the fake structure. | |
405 | */ | |
406 | bcopy((void *) &real, (void *) fakep, sizeof(real)); | |
407 | } | |
408 | } | |
409 | ||
410 | #ifdef __i386__ | |
91447636 | 411 | void |
b0d623f7 | 412 | cpu_desc_init(cpu_data_t *cdp) |
1c79356b | 413 | { |
0c530ab8 | 414 | cpu_desc_index_t *cdi = &cdp->cpu_desc_index; |
1c79356b | 415 | |
b0d623f7 A |
416 | if (cdp == &cpu_data_master) { |
417 | /* | |
418 | * Fix up the entries in the GDT to point to | |
419 | * this LDT and this TSS. | |
420 | */ | |
421 | struct fake_descriptor temp_fake_desc; | |
422 | temp_fake_desc = ldt_desc_pattern; | |
423 | temp_fake_desc.offset = (vm_offset_t) &master_ldt; | |
424 | fix_desc(&temp_fake_desc, 1); | |
425 | *(struct fake_descriptor *) &master_gdt[sel_idx(KERNEL_LDT)] = | |
426 | temp_fake_desc; | |
427 | *(struct fake_descriptor *) &master_gdt[sel_idx(USER_LDT)] = | |
428 | temp_fake_desc; | |
429 | ||
430 | temp_fake_desc = tss_desc_pattern; | |
431 | temp_fake_desc.offset = (vm_offset_t) &master_ktss; | |
432 | fix_desc(&temp_fake_desc, 1); | |
433 | *(struct fake_descriptor *) &master_gdt[sel_idx(KERNEL_TSS)] = | |
434 | temp_fake_desc; | |
435 | ||
436 | #if MACH_KDB | |
437 | temp_fake_desc = tss_desc_pattern; | |
438 | temp_fake_desc.offset = (vm_offset_t) &master_dbtss; | |
439 | fix_desc(&temp_fake_desc, 1); | |
440 | *(struct fake_descriptor *) &master_gdt[sel_idx(DEBUG_TSS)] = | |
441 | temp_fake_desc; | |
442 | #endif | |
443 | ||
444 | temp_fake_desc = cpudata_desc_pattern; | |
445 | temp_fake_desc.offset = (vm_offset_t) &cpu_data_master; | |
446 | fix_desc(&temp_fake_desc, 1); | |
447 | *(struct fake_descriptor *) &master_gdt[sel_idx(CPU_DATA_GS)] = | |
448 | temp_fake_desc; | |
449 | ||
450 | fix_desc((void *)&master_idt, IDTSZ); | |
451 | ||
452 | cdi->cdi_idt.ptr = master_idt; | |
453 | cdi->cdi_gdt.ptr = (void *)master_gdt; | |
454 | ||
455 | ||
456 | /* | |
457 | * Master CPU uses the tables built at boot time. | |
458 | * Just set the index pointers to the high shared-mapping space. | |
459 | * Note that the sysenter stack uses empty space above the ktss | |
460 | * in the HIGH_FIXED_KTSS page. In this case we don't map the | |
461 | * the real master_sstk in low memory. | |
462 | */ | |
463 | cdi->cdi_ktss = (struct i386_tss *) | |
464 | pmap_index_to_virt(HIGH_FIXED_KTSS) ; | |
465 | cdi->cdi_sstk = (vm_offset_t) (cdi->cdi_ktss + 1) + | |
0c530ab8 A |
466 | (vm_offset_t) &master_sstk.top - |
467 | (vm_offset_t) &master_sstk; | |
91447636 | 468 | } else { |
b0d623f7 A |
469 | cpu_desc_table_t *cdt = (cpu_desc_table_t *) cdp->cpu_desc_tablep; |
470 | ||
471 | vm_offset_t cpu_hi_desc; | |
472 | ||
473 | cpu_hi_desc = pmap_cpu_high_shared_remap( | |
474 | cdp->cpu_number, | |
475 | HIGH_CPU_DESC, | |
476 | (vm_offset_t) cdt, 1); | |
477 | ||
478 | /* | |
479 | * Per-cpu GDT, IDT, LDT, KTSS descriptors are allocated in one | |
480 | * block (cpu_desc_table) and double-mapped into high shared space | |
481 | * in one page window. | |
482 | * Also, a transient stack for the fast sysenter path. The top of | |
483 | * which is set at context switch time to point to the PCB using | |
484 | * the high address. | |
485 | */ | |
486 | cdi->cdi_gdt.ptr = (struct fake_descriptor *) (cpu_hi_desc + | |
487 | offsetof(cpu_desc_table_t, gdt[0])); | |
488 | cdi->cdi_idt.ptr = (struct fake_descriptor *) (cpu_hi_desc + | |
489 | offsetof(cpu_desc_table_t, idt[0])); | |
490 | cdi->cdi_ktss = (struct i386_tss *) (cpu_hi_desc + | |
491 | offsetof(cpu_desc_table_t, ktss)); | |
492 | cdi->cdi_sstk = cpu_hi_desc + offsetof(cpu_desc_table_t, sstk.top); | |
91447636 | 493 | |
b0d623f7 A |
494 | /* |
495 | * LDT descriptors are mapped into a seperate area. | |
496 | */ | |
497 | cdi->cdi_ldt = (struct fake_descriptor *) | |
0c530ab8 | 498 | pmap_cpu_high_shared_remap( |
b0d623f7 A |
499 | cdp->cpu_number, |
500 | HIGH_CPU_LDT_BEGIN, | |
501 | (vm_offset_t) cdp->cpu_ldtp, | |
502 | HIGH_CPU_LDT_END - HIGH_CPU_LDT_BEGIN + 1); | |
55e303ae | 503 | |
b0d623f7 A |
504 | /* |
505 | * Copy the tables | |
506 | */ | |
507 | bcopy((char *)master_idt, (char *)cdt->idt, sizeof(master_idt)); | |
508 | bcopy((char *)master_gdt, (char *)cdt->gdt, sizeof(master_gdt)); | |
509 | bcopy((char *)master_ldt, (char *)cdp->cpu_ldtp, sizeof(master_ldt)); | |
510 | bzero((char *)&cdt->ktss, sizeof(struct i386_tss)); | |
1c79356b | 511 | #if MACH_KDB |
b0d623f7 | 512 | cdi->cdi_dbtss = (struct i386_tss *) (cpu_hi_desc + |
0c530ab8 | 513 | offsetof(cpu_desc_table_t, dbtss)); |
b0d623f7 A |
514 | bcopy((char *)&master_dbtss, |
515 | (char *)&cdt->dbtss, | |
516 | sizeof(struct i386_tss)); | |
1c79356b A |
517 | #endif /* MACH_KDB */ |
518 | ||
b0d623f7 A |
519 | /* |
520 | * Fix up the entries in the GDT to point to | |
521 | * this LDT and this TSS. | |
522 | */ | |
523 | struct fake_descriptor temp_ldt = ldt_desc_pattern; | |
524 | temp_ldt.offset = (vm_offset_t)cdi->cdi_ldt; | |
525 | fix_desc(&temp_ldt, 1); | |
0c530ab8 | 526 | |
b0d623f7 A |
527 | cdt->gdt[sel_idx(KERNEL_LDT)] = temp_ldt; |
528 | cdt->gdt[sel_idx(USER_LDT)] = temp_ldt; | |
4452a7af | 529 | |
b0d623f7 A |
530 | cdt->gdt[sel_idx(KERNEL_TSS)] = tss_desc_pattern; |
531 | cdt->gdt[sel_idx(KERNEL_TSS)].offset = (vm_offset_t) cdi->cdi_ktss; | |
532 | fix_desc(&cdt->gdt[sel_idx(KERNEL_TSS)], 1); | |
1c79356b | 533 | |
b0d623f7 A |
534 | cdt->gdt[sel_idx(CPU_DATA_GS)] = cpudata_desc_pattern; |
535 | cdt->gdt[sel_idx(CPU_DATA_GS)].offset = (vm_offset_t) cdp; | |
536 | fix_desc(&cdt->gdt[sel_idx(CPU_DATA_GS)], 1); | |
1c79356b | 537 | |
6d2010ae | 538 | #if MACH_KDB /* this only works for legacy 32-bit machines */ |
b0d623f7 A |
539 | cdt->gdt[sel_idx(DEBUG_TSS)] = tss_desc_pattern; |
540 | cdt->gdt[sel_idx(DEBUG_TSS)].offset = (vm_offset_t) cdi->cdi_dbtss; | |
541 | fix_desc(&cdt->gdt[sel_idx(DEBUG_TSS)], 1); | |
542 | ||
543 | cdt->dbtss.esp0 = (int)(db_task_stack_store + | |
6d2010ae | 544 | (INTSTACK_SIZE * (cdp->cpu_number + 1)) - sizeof (natural_t)); |
b0d623f7 A |
545 | cdt->dbtss.esp = cdt->dbtss.esp0; |
546 | cdt->dbtss.eip = (int)&db_task_start; | |
1c79356b A |
547 | #endif /* MACH_KDB */ |
548 | ||
b0d623f7 A |
549 | cdt->ktss.ss0 = KERNEL_DS; |
550 | cdt->ktss.io_bit_map_offset = 0x0FFF; /* no IO bitmap */ | |
0c530ab8 | 551 | |
b0d623f7 A |
552 | cpu_userwindow_init(cdp->cpu_number); |
553 | cpu_physwindow_init(cdp->cpu_number); | |
0c530ab8 | 554 | |
4452a7af | 555 | } |
0c530ab8 | 556 | } |
b0d623f7 | 557 | #endif /* __i386__ */ |
0c530ab8 A |
558 | |
559 | void | |
b0d623f7 | 560 | cpu_desc_init64(cpu_data_t *cdp) |
0c530ab8 | 561 | { |
0c530ab8 A |
562 | cpu_desc_index_t *cdi = &cdp->cpu_desc_index; |
563 | ||
b0d623f7 | 564 | if (cdp == &cpu_data_master) { |
2d21ac55 A |
565 | /* |
566 | * Master CPU uses the tables built at boot time. | |
567 | * Just set the index pointers to the low memory space. | |
2d21ac55 | 568 | */ |
b0d623f7 | 569 | cdi->cdi_ktss = (void *)&master_ktss64; |
2d21ac55 | 570 | cdi->cdi_sstk = (vm_offset_t) &master_sstk.top; |
b0d623f7 A |
571 | cdi->cdi_gdt.ptr = (void *)master_gdt; |
572 | cdi->cdi_idt.ptr = (void *)master_idt64; | |
573 | cdi->cdi_ldt = (struct fake_descriptor *) master_ldt; | |
574 | ||
2d21ac55 | 575 | |
b0d623f7 A |
576 | /* Replace the expanded LDTs and TSS slots in the GDT */ |
577 | kernel_ldt_desc64.offset64 = UBER64(&master_ldt); | |
2d21ac55 A |
578 | *(struct fake_descriptor64 *) &master_gdt[sel_idx(KERNEL_LDT)] = |
579 | kernel_ldt_desc64; | |
b0d623f7 A |
580 | *(struct fake_descriptor64 *) &master_gdt[sel_idx(USER_LDT)] = |
581 | kernel_ldt_desc64; | |
582 | kernel_tss_desc64.offset64 = UBER64(&master_ktss64); | |
2d21ac55 A |
583 | *(struct fake_descriptor64 *) &master_gdt[sel_idx(KERNEL_TSS)] = |
584 | kernel_tss_desc64; | |
0c530ab8 | 585 | |
b0d623f7 | 586 | /* Fix up the expanded descriptors for 64-bit. */ |
2d21ac55 A |
587 | fix_desc64((void *) &master_idt64, IDTSZ); |
588 | fix_desc64((void *) &master_gdt[sel_idx(KERNEL_LDT)], 1); | |
b0d623f7 | 589 | fix_desc64((void *) &master_gdt[sel_idx(USER_LDT)], 1); |
2d21ac55 | 590 | fix_desc64((void *) &master_gdt[sel_idx(KERNEL_TSS)], 1); |
0c530ab8 | 591 | |
2d21ac55 A |
592 | /* |
593 | * Set the double-fault stack as IST1 in the 64-bit TSS | |
594 | */ | |
b0d623f7 | 595 | master_ktss64.ist1 = UBER64((uintptr_t) df_task_stack_end); |
0c530ab8 A |
596 | |
597 | } else { | |
b0d623f7 | 598 | cpu_desc_table64_t *cdt = (cpu_desc_table64_t *) cdp->cpu_desc_tablep; |
2d21ac55 A |
599 | /* |
600 | * Per-cpu GDT, IDT, KTSS descriptors are allocated in kernel | |
7e4a7d39 | 601 | * heap (cpu_desc_table). |
2d21ac55 A |
602 | * LDT descriptors are mapped into a separate area. |
603 | */ | |
b0d623f7 A |
604 | cdi->cdi_gdt.ptr = (struct fake_descriptor *)cdt->gdt; |
605 | cdi->cdi_idt.ptr = (void *)cdt->idt; | |
606 | cdi->cdi_ktss = (void *)&cdt->ktss; | |
2d21ac55 A |
607 | cdi->cdi_sstk = (vm_offset_t)&cdt->sstk.top; |
608 | cdi->cdi_ldt = cdp->cpu_ldtp; | |
0c530ab8 | 609 | |
2d21ac55 A |
610 | /* |
611 | * Copy the tables | |
612 | */ | |
b0d623f7 A |
613 | bcopy((char *)master_idt64, (char *)cdt->idt, sizeof(master_idt64)); |
614 | bcopy((char *)master_gdt, (char *)cdt->gdt, sizeof(master_gdt)); | |
615 | bcopy((char *)master_ldt, (char *)cdp->cpu_ldtp, sizeof(master_ldt)); | |
616 | bcopy((char *)&master_ktss64, (char *)&cdt->ktss, sizeof(struct x86_64_tss)); | |
0c530ab8 | 617 | |
2d21ac55 A |
618 | /* |
619 | * Fix up the entries in the GDT to point to | |
620 | * this LDT and this TSS. | |
621 | */ | |
b0d623f7 | 622 | kernel_ldt_desc64.offset64 = UBER64(cdi->cdi_ldt); |
2d21ac55 A |
623 | *(struct fake_descriptor64 *) &cdt->gdt[sel_idx(KERNEL_LDT)] = |
624 | kernel_ldt_desc64; | |
625 | fix_desc64(&cdt->gdt[sel_idx(KERNEL_LDT)], 1); | |
0c530ab8 | 626 | |
b0d623f7 | 627 | kernel_ldt_desc64.offset64 = UBER64(cdi->cdi_ldt); |
2d21ac55 A |
628 | *(struct fake_descriptor64 *) &cdt->gdt[sel_idx(USER_LDT)] = |
629 | kernel_ldt_desc64; | |
630 | fix_desc64(&cdt->gdt[sel_idx(USER_LDT)], 1); | |
0c530ab8 | 631 | |
b0d623f7 | 632 | kernel_tss_desc64.offset64 = UBER64(cdi->cdi_ktss); |
2d21ac55 A |
633 | *(struct fake_descriptor64 *) &cdt->gdt[sel_idx(KERNEL_TSS)] = |
634 | kernel_tss_desc64; | |
635 | fix_desc64(&cdt->gdt[sel_idx(KERNEL_TSS)], 1); | |
0c530ab8 | 636 | |
6d2010ae A |
637 | /* Set (zeroed) double-fault stack as IST1 */ |
638 | bzero((void *) cdt->dfstk, sizeof(cdt->dfstk)); | |
b0d623f7 A |
639 | cdt->ktss.ist1 = UBER64((unsigned long)cdt->dfstk + sizeof(cdt->dfstk)); |
640 | #ifdef __i386__ | |
2d21ac55 A |
641 | cdt->gdt[sel_idx(CPU_DATA_GS)] = cpudata_desc_pattern; |
642 | cdt->gdt[sel_idx(CPU_DATA_GS)].offset = (vm_offset_t) cdp; | |
643 | fix_desc(&cdt->gdt[sel_idx(CPU_DATA_GS)], 1); | |
0c530ab8 | 644 | |
b0d623f7 | 645 | /* Allocate copyio windows */ |
2d21ac55 A |
646 | cpu_userwindow_init(cdp->cpu_number); |
647 | cpu_physwindow_init(cdp->cpu_number); | |
b0d623f7 | 648 | #endif |
0c530ab8 A |
649 | } |
650 | ||
651 | /* Require that the top of the sysenter stack is 16-byte aligned */ | |
652 | if ((cdi->cdi_sstk % 16) != 0) | |
653 | panic("cpu_desc_init64() sysenter stack not 16-byte aligned"); | |
654 | } | |
655 | ||
b0d623f7 A |
656 | #ifdef __i386__ |
657 | void | |
658 | cpu_desc_load(cpu_data_t *cdp) | |
659 | { | |
660 | cpu_desc_index_t *cdi = &cdp->cpu_desc_index; | |
661 | ||
662 | cdi->cdi_idt.size = 0x1000 + cdp->cpu_number; | |
663 | cdi->cdi_gdt.size = sizeof(struct real_descriptor)*GDTSZ - 1; | |
664 | ||
665 | lgdt((unsigned long *) &cdi->cdi_gdt); | |
666 | lidt((unsigned long *) &cdi->cdi_idt); | |
667 | lldt(KERNEL_LDT); | |
668 | ||
669 | set_tr(KERNEL_TSS); | |
670 | ||
671 | __asm__ volatile("mov %0, %%gs" : : "rm" ((unsigned short)(CPU_DATA_GS))); | |
672 | } | |
673 | #endif /* __i386__ */ | |
674 | ||
675 | void | |
676 | cpu_desc_load64(cpu_data_t *cdp) | |
677 | { | |
678 | cpu_desc_index_t *cdi = &cdp->cpu_desc_index; | |
679 | ||
680 | #ifdef __i386__ | |
681 | /* | |
682 | * Load up the new descriptors etc | |
683 | * ml_load_desc64() expects these global pseudo-descriptors: | |
684 | * gdtptr64 -> per-cpu gdt | |
685 | * idtptr64 -> per-cpu idt | |
686 | * These are 10-byte descriptors with 64-bit addresses into | |
687 | * uber-space. | |
688 | * | |
689 | * Refer to commpage/cpu_number.s for the IDT limit trick. | |
690 | */ | |
7e4a7d39 | 691 | gdtptr64.length = GDTSZ * sizeof(struct real_descriptor) - 1; |
b0d623f7 A |
692 | gdtptr64.offset[0] = (uint32_t) cdi->cdi_gdt.ptr; |
693 | gdtptr64.offset[1] = KERNEL_UBER_BASE_HI32; | |
694 | idtptr64.length = 0x1000 + cdp->cpu_number; | |
695 | idtptr64.offset[0] = (uint32_t) cdi->cdi_idt.ptr; | |
696 | idtptr64.offset[1] = KERNEL_UBER_BASE_HI32; | |
697 | ||
698 | /* Make sure busy bit is cleared in the TSS */ | |
699 | gdt_desc_p(KERNEL_TSS)->access &= ~ACC_TSS_BUSY; | |
700 | ||
701 | ml_load_desc64(); | |
702 | #else | |
703 | /* Load the GDT, LDT, IDT and TSS */ | |
7e4a7d39 | 704 | cdi->cdi_gdt.size = sizeof(struct real_descriptor)*GDTSZ - 1; |
b0d623f7 A |
705 | cdi->cdi_idt.size = 0x1000 + cdp->cpu_number; |
706 | lgdt((unsigned long *) &cdi->cdi_gdt); | |
707 | lidt((unsigned long *) &cdi->cdi_idt); | |
708 | lldt(KERNEL_LDT); | |
709 | set_tr(KERNEL_TSS); | |
710 | ||
711 | /* Stuff the pre-cpu data area into the MSR and swapgs to activate */ | |
712 | wrmsr64(MSR_IA32_KERNEL_GS_BASE, (unsigned long)cdp); | |
713 | #if GPROF // Hack to enable mcount to work on K64 | |
714 | __asm__ volatile("mov %0, %%gs" : : "rm" ((unsigned short)(KERNEL_DS))); | |
715 | #endif | |
716 | swapgs(); | |
717 | ||
718 | cpu_mode_init(cdp); | |
719 | #endif | |
720 | } | |
721 | ||
722 | #ifdef __i386__ | |
723 | /* | |
724 | * Set MSRs for sysenter/sysexit for 32-bit. | |
725 | */ | |
726 | static void | |
727 | fast_syscall_init(__unused cpu_data_t *cdp) | |
728 | { | |
729 | wrmsr(MSR_IA32_SYSENTER_CS, SYSENTER_CS, 0); | |
730 | wrmsr(MSR_IA32_SYSENTER_EIP, HI_TEXT(hi_sysenter), 0); | |
731 | wrmsr(MSR_IA32_SYSENTER_ESP, current_sstk(), 0); | |
732 | } | |
733 | #endif | |
734 | ||
0c530ab8 | 735 | /* |
b0d623f7 | 736 | * Set MSRs for sysenter/sysexit and syscall/sysret for 64-bit. |
0c530ab8 | 737 | */ |
2d21ac55 | 738 | static void |
b0d623f7 | 739 | fast_syscall_init64(__unused cpu_data_t *cdp) |
0c530ab8 A |
740 | { |
741 | wrmsr64(MSR_IA32_SYSENTER_CS, SYSENTER_CS); | |
b0d623f7 | 742 | wrmsr64(MSR_IA32_SYSENTER_EIP, UBER64((uintptr_t) hi64_sysenter)); |
0c530ab8 | 743 | wrmsr64(MSR_IA32_SYSENTER_ESP, UBER64(current_sstk())); |
0c530ab8 A |
744 | /* Enable syscall/sysret */ |
745 | wrmsr64(MSR_IA32_EFER, rdmsr64(MSR_IA32_EFER) | MSR_IA32_EFER_SCE); | |
746 | ||
747 | /* | |
748 | * MSRs for 64-bit syscall/sysret | |
749 | * Note USER_CS because sysret uses this + 16 when returning to | |
750 | * 64-bit code. | |
751 | */ | |
b0d623f7 A |
752 | wrmsr64(MSR_IA32_LSTAR, UBER64((uintptr_t) hi64_syscall)); |
753 | wrmsr64(MSR_IA32_STAR, (((uint64_t)USER_CS) << 48) | | |
754 | (((uint64_t)KERNEL64_CS) << 32)); | |
0c530ab8 A |
755 | /* |
756 | * Emulate eflags cleared by sysenter but note that | |
757 | * we also clear the trace trap to avoid the complications | |
2d21ac55 A |
758 | * of single-stepping into a syscall. The nested task bit |
759 | * is also cleared to avoid a spurious "task switch" | |
760 | * should we choose to return via an IRET. | |
0c530ab8 A |
761 | */ |
762 | wrmsr64(MSR_IA32_FMASK, EFL_DF|EFL_IF|EFL_TF|EFL_NT); | |
763 | ||
b0d623f7 | 764 | #ifdef __i386__ |
0c530ab8 | 765 | /* |
2d21ac55 | 766 | * Set the Kernel GS base MSR to point to per-cpu data in uber-space. |
0c530ab8 A |
767 | * The uber-space handler (hi64_syscall) uses the swapgs instruction. |
768 | */ | |
b0d623f7 | 769 | wrmsr64(MSR_IA32_KERNEL_GS_BASE, UBER64(cdp)); |
4a3eedf9 A |
770 | |
771 | #if ONLY_SAFE_FOR_LINDA_SERIAL | |
0c530ab8 | 772 | kprintf("fast_syscall_init64() KERNEL_GS_BASE=0x%016llx\n", |
b0d623f7 A |
773 | rdmsr64(MSR_IA32_KERNEL_GS_BASE)); |
774 | #endif | |
4a3eedf9 | 775 | #endif |
1c79356b A |
776 | } |
777 | ||
6d2010ae | 778 | |
91447636 A |
779 | cpu_data_t * |
780 | cpu_data_alloc(boolean_t is_boot_cpu) | |
1c79356b | 781 | { |
91447636 A |
782 | int ret; |
783 | cpu_data_t *cdp; | |
784 | ||
785 | if (is_boot_cpu) { | |
786 | assert(real_ncpus == 1); | |
91447636 A |
787 | cdp = &cpu_data_master; |
788 | if (cdp->cpu_processor == NULL) { | |
b0d623f7 | 789 | simple_lock_init(&ncpus_lock, 0); |
91447636 | 790 | cdp->cpu_processor = cpu_processor_alloc(TRUE); |
b0d623f7 | 791 | #if NCOPY_WINDOWS > 0 |
91447636 | 792 | cdp->cpu_pmap = pmap_cpu_alloc(TRUE); |
b0d623f7 | 793 | #endif |
91447636 A |
794 | } |
795 | return cdp; | |
796 | } | |
1c79356b | 797 | |
1c79356b | 798 | /* |
91447636 | 799 | * Allocate per-cpu data: |
1c79356b | 800 | */ |
b0d623f7 | 801 | ret = kmem_alloc(kernel_map, (vm_offset_t *) &cdp, sizeof(cpu_data_t)); |
91447636 A |
802 | if (ret != KERN_SUCCESS) { |
803 | printf("cpu_data_alloc() failed, ret=%d\n", ret); | |
804 | goto abort; | |
805 | } | |
806 | bzero((void*) cdp, sizeof(cpu_data_t)); | |
807 | cdp->cpu_this = cdp; | |
1c79356b | 808 | |
0c530ab8 A |
809 | /* Propagate mode */ |
810 | cdp->cpu_is64bit = cpu_mode_is64bit(); | |
811 | ||
1c79356b | 812 | /* |
91447636 | 813 | * Allocate interrupt stack: |
1c79356b | 814 | */ |
91447636 A |
815 | ret = kmem_alloc(kernel_map, |
816 | (vm_offset_t *) &cdp->cpu_int_stack_top, | |
817 | INTSTACK_SIZE); | |
818 | if (ret != KERN_SUCCESS) { | |
819 | printf("cpu_data_alloc() int stack failed, ret=%d\n", ret); | |
820 | goto abort; | |
1c79356b | 821 | } |
91447636 A |
822 | bzero((void*) cdp->cpu_int_stack_top, INTSTACK_SIZE); |
823 | cdp->cpu_int_stack_top += INTSTACK_SIZE; | |
1c79356b A |
824 | |
825 | /* | |
91447636 | 826 | * Allocate descriptor table: |
0c530ab8 | 827 | * Size depends on cpu mode. |
1c79356b | 828 | */ |
91447636 A |
829 | ret = kmem_alloc(kernel_map, |
830 | (vm_offset_t *) &cdp->cpu_desc_tablep, | |
0c530ab8 A |
831 | cdp->cpu_is64bit ? sizeof(cpu_desc_table64_t) |
832 | : sizeof(cpu_desc_table_t)); | |
91447636 A |
833 | if (ret != KERN_SUCCESS) { |
834 | printf("cpu_data_alloc() desc_table failed, ret=%d\n", ret); | |
835 | goto abort; | |
836 | } | |
1c79356b | 837 | |
0c530ab8 A |
838 | /* |
839 | * Allocate LDT | |
840 | */ | |
841 | ret = kmem_alloc(kernel_map, | |
842 | (vm_offset_t *) &cdp->cpu_ldtp, | |
843 | sizeof(struct real_descriptor) * LDTSZ); | |
844 | if (ret != KERN_SUCCESS) { | |
845 | printf("cpu_data_alloc() ldt failed, ret=%d\n", ret); | |
846 | goto abort; | |
847 | } | |
848 | ||
b0d623f7 | 849 | #if CONFIG_MCA |
2d21ac55 A |
850 | /* Machine-check shadow register allocation. */ |
851 | mca_cpu_alloc(cdp); | |
b0d623f7 A |
852 | #endif |
853 | ||
854 | simple_lock(&ncpus_lock); | |
2d21ac55 | 855 | |
91447636 A |
856 | cpu_data_ptr[real_ncpus] = cdp; |
857 | cdp->cpu_number = real_ncpus; | |
858 | real_ncpus++; | |
b0d623f7 | 859 | simple_unlock(&ncpus_lock); |
0c530ab8 | 860 | |
6d2010ae | 861 | cdp->cpu_nanotime = &pal_rtc_nanotime_info; |
593a1d5f | 862 | |
2d21ac55 A |
863 | kprintf("cpu_data_alloc(%d) %p desc_table: %p " |
864 | "ldt: %p " | |
b0d623f7 | 865 | "int_stack: 0x%lx-0x%lx\n", |
0c530ab8 | 866 | cdp->cpu_number, cdp, cdp->cpu_desc_tablep, cdp->cpu_ldtp, |
b0d623f7 | 867 | (long)(cdp->cpu_int_stack_top - INTSTACK_SIZE), (long)(cdp->cpu_int_stack_top)); |
91447636 A |
868 | |
869 | return cdp; | |
870 | ||
871 | abort: | |
872 | if (cdp) { | |
873 | if (cdp->cpu_desc_tablep) | |
874 | kfree((void *) cdp->cpu_desc_tablep, | |
875 | sizeof(*cdp->cpu_desc_tablep)); | |
876 | if (cdp->cpu_int_stack_top) | |
877 | kfree((void *) (cdp->cpu_int_stack_top - INTSTACK_SIZE), | |
878 | INTSTACK_SIZE); | |
879 | kfree((void *) cdp, sizeof(*cdp)); | |
880 | } | |
881 | return NULL; | |
882 | } | |
1c79356b | 883 | |
6d2010ae A |
884 | boolean_t |
885 | valid_user_data_selector(uint16_t selector) | |
886 | { | |
887 | sel_t sel = selector_to_sel(selector); | |
888 | ||
889 | if (selector == 0) | |
890 | return (TRUE); | |
891 | ||
892 | if (sel.ti == SEL_LDT) | |
893 | return (TRUE); | |
894 | else if (sel.index < GDTSZ) { | |
895 | if ((gdt_desc_p(selector)->access & ACC_PL_U) == ACC_PL_U) | |
896 | return (TRUE); | |
897 | } | |
898 | ||
899 | return (FALSE); | |
900 | } | |
901 | ||
902 | boolean_t | |
903 | valid_user_code_selector(uint16_t selector) | |
904 | { | |
905 | sel_t sel = selector_to_sel(selector); | |
906 | ||
907 | if (selector == 0) | |
908 | return (FALSE); | |
909 | ||
910 | if (sel.ti == SEL_LDT) { | |
911 | if (sel.rpl == USER_PRIV) | |
912 | return (TRUE); | |
913 | } | |
914 | else if (sel.index < GDTSZ && sel.rpl == USER_PRIV) { | |
915 | if ((gdt_desc_p(selector)->access & ACC_PL_U) == ACC_PL_U) | |
916 | return (TRUE); | |
917 | } | |
918 | ||
919 | return (FALSE); | |
920 | } | |
921 | ||
922 | boolean_t | |
923 | valid_user_stack_selector(uint16_t selector) | |
924 | { | |
925 | sel_t sel = selector_to_sel(selector); | |
926 | ||
927 | if (selector == 0) | |
928 | return (FALSE); | |
929 | ||
930 | if (sel.ti == SEL_LDT) { | |
931 | if (sel.rpl == USER_PRIV) | |
932 | return (TRUE); | |
933 | } | |
934 | else if (sel.index < GDTSZ && sel.rpl == USER_PRIV) { | |
935 | if ((gdt_desc_p(selector)->access & ACC_PL_U) == ACC_PL_U) | |
936 | return (TRUE); | |
937 | } | |
938 | ||
939 | return (FALSE); | |
940 | } | |
941 | ||
91447636 A |
942 | boolean_t |
943 | valid_user_segment_selectors(uint16_t cs, | |
b0d623f7 A |
944 | uint16_t ss, |
945 | uint16_t ds, | |
946 | uint16_t es, | |
947 | uint16_t fs, | |
948 | uint16_t gs) | |
91447636 A |
949 | { |
950 | return valid_user_code_selector(cs) && | |
b0d623f7 A |
951 | valid_user_stack_selector(ss) && |
952 | valid_user_data_selector(ds) && | |
953 | valid_user_data_selector(es) && | |
954 | valid_user_data_selector(fs) && | |
955 | valid_user_data_selector(gs); | |
1c79356b A |
956 | } |
957 | ||
b0d623f7 A |
958 | #if NCOPY_WINDOWS > 0 |
959 | ||
0c530ab8 | 960 | static vm_offset_t user_window_base = 0; |
0c530ab8 A |
961 | |
962 | void | |
2d21ac55 | 963 | cpu_userwindow_init(int cpu) |
0c530ab8 A |
964 | { |
965 | cpu_data_t *cdp = cpu_data_ptr[cpu]; | |
b0d623f7 A |
966 | vm_offset_t user_window; |
967 | vm_offset_t vaddr; | |
0c530ab8 A |
968 | int num_cpus; |
969 | ||
970 | num_cpus = ml_get_max_cpus(); | |
971 | ||
972 | if (cpu >= num_cpus) | |
b0d623f7 | 973 | panic("cpu_userwindow_init: cpu > num_cpus"); |
0c530ab8 A |
974 | |
975 | if (user_window_base == 0) { | |
976 | ||
b0d623f7 A |
977 | if (vm_allocate(kernel_map, &vaddr, |
978 | (NBPDE * NCOPY_WINDOWS * num_cpus) + NBPDE, | |
979 | VM_FLAGS_ANYWHERE) != KERN_SUCCESS) | |
980 | panic("cpu_userwindow_init: " | |
981 | "couldn't allocate user map window"); | |
0c530ab8 A |
982 | |
983 | /* | |
984 | * window must start on a page table boundary | |
985 | * in the virtual address space | |
986 | */ | |
987 | user_window_base = (vaddr + (NBPDE - 1)) & ~(NBPDE - 1); | |
988 | ||
989 | /* | |
990 | * get rid of any allocation leading up to our | |
991 | * starting boundary | |
992 | */ | |
993 | vm_deallocate(kernel_map, vaddr, user_window_base - vaddr); | |
994 | ||
995 | /* | |
996 | * get rid of tail that we don't need | |
997 | */ | |
998 | user_window = user_window_base + | |
999 | (NBPDE * NCOPY_WINDOWS * num_cpus); | |
1000 | ||
1001 | vm_deallocate(kernel_map, user_window, | |
1002 | (vaddr + | |
1003 | ((NBPDE * NCOPY_WINDOWS * num_cpus) + NBPDE)) - | |
1004 | user_window); | |
0c530ab8 A |
1005 | } |
1006 | ||
b0d623f7 | 1007 | user_window = user_window_base + (cpu * NCOPY_WINDOWS * NBPDE); |
0c530ab8 | 1008 | |
0c530ab8 | 1009 | cdp->cpu_copywindow_base = user_window; |
6d2010ae A |
1010 | /* |
1011 | * Abuse this pdp entry, the pdp now actually points to | |
1012 | * an array of copy windows addresses. | |
1013 | */ | |
0c530ab8 A |
1014 | cdp->cpu_copywindow_pdp = pmap_pde(kernel_pmap, user_window); |
1015 | ||
b0d623f7 A |
1016 | #ifdef __i386__ |
1017 | cpu_desc_index_t *cdi = &cdp->cpu_desc_index; | |
1018 | cdi->cdi_gdt.ptr[sel_idx(USER_WINDOW_SEL)] = userwindow_desc_pattern; | |
1019 | cdi->cdi_gdt.ptr[sel_idx(USER_WINDOW_SEL)].offset = user_window; | |
0c530ab8 | 1020 | |
b0d623f7 A |
1021 | fix_desc(&cdi->cdi_gdt.ptr[sel_idx(USER_WINDOW_SEL)], 1); |
1022 | #endif /* __i386__ */ | |
2d21ac55 | 1023 | } |
0c530ab8 | 1024 | |
2d21ac55 A |
1025 | void |
1026 | cpu_physwindow_init(int cpu) | |
1027 | { | |
1028 | cpu_data_t *cdp = cpu_data_ptr[cpu]; | |
c910b4d9 | 1029 | vm_offset_t phys_window = cdp->cpu_physwindow_base; |
2d21ac55 | 1030 | |
c910b4d9 A |
1031 | if (phys_window == 0) { |
1032 | if (vm_allocate(kernel_map, &phys_window, | |
1033 | PAGE_SIZE, VM_FLAGS_ANYWHERE) | |
2d21ac55 | 1034 | != KERN_SUCCESS) |
c910b4d9 A |
1035 | panic("cpu_physwindow_init: " |
1036 | "couldn't allocate phys map window"); | |
2d21ac55 | 1037 | |
c910b4d9 A |
1038 | /* |
1039 | * make sure the page that encompasses the | |
1040 | * pte pointer we're interested in actually | |
1041 | * exists in the page table | |
1042 | */ | |
1043 | pmap_expand(kernel_pmap, phys_window); | |
0c530ab8 | 1044 | |
c910b4d9 A |
1045 | cdp->cpu_physwindow_base = phys_window; |
1046 | cdp->cpu_physwindow_ptep = vtopte(phys_window); | |
1047 | } | |
b0d623f7 A |
1048 | #ifdef __i386__ |
1049 | cpu_desc_index_t *cdi = &cdp->cpu_desc_index; | |
1050 | cdi->cdi_gdt.ptr[sel_idx(PHYS_WINDOW_SEL)] = physwindow_desc_pattern; | |
1051 | cdi->cdi_gdt.ptr[sel_idx(PHYS_WINDOW_SEL)].offset = phys_window; | |
0c530ab8 | 1052 | |
b0d623f7 A |
1053 | fix_desc(&cdi->cdi_gdt.ptr[sel_idx(PHYS_WINDOW_SEL)], 1); |
1054 | #endif /* __i386__ */ | |
0c530ab8 | 1055 | } |
b0d623f7 | 1056 | #endif /* NCOPY_WINDOWS > 0 */ |
0c530ab8 | 1057 | |
0c530ab8 A |
1058 | /* |
1059 | * Load the segment descriptor tables for the current processor. | |
1060 | */ | |
2d21ac55 A |
1061 | void |
1062 | cpu_mode_init(cpu_data_t *cdp) | |
1063 | { | |
b0d623f7 | 1064 | #ifdef __i386__ |
6d2010ae | 1065 | if (cdp->cpu_is64bit) { |
2d21ac55 A |
1066 | cpu_IA32e_enable(cdp); |
1067 | cpu_desc_load64(cdp); | |
b0d623f7 | 1068 | fast_syscall_init64(cdp); |
2d21ac55 | 1069 | } else { |
b0d623f7 | 1070 | fast_syscall_init(cdp); |
2d21ac55 | 1071 | } |
b0d623f7 A |
1072 | #else |
1073 | fast_syscall_init64(cdp); | |
1074 | #endif | |
2d21ac55 A |
1075 | |
1076 | /* Call for per-cpu pmap mode initialization */ | |
1077 | pmap_cpu_init(); | |
2d21ac55 A |
1078 | } |
1079 |