]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/mp_desc.c
xnu-3789.70.16.tar.gz
[apple/xnu.git] / osfmk / i386 / mp_desc.c
1 /*
2 * Copyright (c) 2000-2012 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
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
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
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
60 #include <kern/cpu_number.h>
61 #include <kern/kalloc.h>
62 #include <kern/cpu_data.h>
63 #include <mach/mach_types.h>
64 #include <mach/machine.h>
65 #include <mach/vm_map.h>
66 #include <mach/machine/vm_param.h>
67 #include <vm/vm_kern.h>
68 #include <vm/vm_map.h>
69
70 #include <i386/bit_routines.h>
71 #include <i386/mp_desc.h>
72 #include <i386/misc_protos.h>
73 #include <i386/mp.h>
74 #include <i386/pmap.h>
75 #if defined(__i386__) || defined(__x86_64__)
76 #include <i386/pmap_internal.h>
77 #endif /* i386 */
78 #if CONFIG_MCA
79 #include <i386/machine_check.h>
80 #endif
81
82 #include <kern/misc_protos.h>
83
84 #define K_INTR_GATE (ACC_P|ACC_PL_K|ACC_INTR_GATE)
85 #define U_INTR_GATE (ACC_P|ACC_PL_U|ACC_INTR_GATE)
86
87 // Declare macros that will declare the externs
88 #define TRAP(n, name) extern void *name ;
89 #define TRAP_ERR(n, name) extern void *name ;
90 #define TRAP_SPC(n, name) extern void *name ;
91 #define TRAP_IST1(n, name) extern void *name ;
92 #define TRAP_IST2(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_IST1
105 #undef TRAP_IST2
106 #undef INTERRUPT
107 #undef USER_TRAP
108 #undef USER_TRAP_SPC
109
110 #define TRAP(n, name) \
111 [n] = { \
112 (uintptr_t)&name, \
113 KERNEL64_CS, \
114 0, \
115 K_INTR_GATE, \
116 0 \
117 },
118
119 #define TRAP_ERR TRAP
120 #define TRAP_SPC TRAP
121
122 #define TRAP_IST1(n, name) \
123 [n] = { \
124 (uintptr_t)&name, \
125 KERNEL64_CS, \
126 1, \
127 K_INTR_GATE, \
128 0 \
129 },
130
131 #define TRAP_IST2(n, name) \
132 [n] = { \
133 (uintptr_t)&name, \
134 KERNEL64_CS, \
135 2, \
136 K_INTR_GATE, \
137 0 \
138 },
139
140 #define INTERRUPT(n) \
141 [n] = { \
142 (uintptr_t)&_intr_ ## n,\
143 KERNEL64_CS, \
144 0, \
145 K_INTR_GATE, \
146 0 \
147 },
148
149 #define USER_TRAP(n, name) \
150 [n] = { \
151 (uintptr_t)&name, \
152 KERNEL64_CS, \
153 0, \
154 U_INTR_GATE, \
155 0 \
156 },
157
158 #define USER_TRAP_SPC USER_TRAP
159
160 // Declare the table using the macros we just set up
161 struct fake_descriptor64 master_idt64[IDTSZ]
162 __attribute__ ((section("__HIB,__desc")))
163 __attribute__ ((aligned(PAGE_SIZE))) = {
164 #include "../x86_64/idt_table.h"
165 };
166
167 /*
168 * First cpu`s interrupt stack.
169 */
170 extern uint32_t low_intstack[]; /* bottom */
171 extern uint32_t low_eintstack[]; /* top */
172
173 /*
174 * Per-cpu data area pointers.
175 * The master cpu (cpu 0) has its data area statically allocated;
176 * others are allocated dynamically and this array is updated at runtime.
177 */
178 static cpu_data_t cpu_data_master = {
179 .cpu_this = &cpu_data_master,
180 .cpu_nanotime = &pal_rtc_nanotime_info,
181 .cpu_int_stack_top = (vm_offset_t) low_eintstack,
182 };
183 cpu_data_t *cpu_data_ptr[MAX_CPUS] = { [0] = &cpu_data_master };
184
185 decl_simple_lock_data(,ncpus_lock); /* protects real_ncpus */
186 unsigned int real_ncpus = 1;
187 unsigned int max_ncpus = MAX_CPUS;
188
189 extern void hi64_sysenter(void);
190 extern void hi64_syscall(void);
191
192 /*
193 * Multiprocessor i386/i486 systems use a separate copy of the
194 * GDT, IDT, LDT, and kernel TSS per processor. The first three
195 * are separate to avoid lock contention: the i386 uses locked
196 * memory cycles to access the descriptor tables. The TSS is
197 * separate since each processor needs its own kernel stack,
198 * and since using a TSS marks it busy.
199 */
200
201 /*
202 * Allocate and initialize the per-processor descriptor tables.
203 */
204
205 /*
206 * This is the expanded, 64-bit variant of the kernel LDT descriptor.
207 * When switching to 64-bit mode this replaces KERNEL_LDT entry
208 * and the following empty slot. This enables the LDT to be referenced
209 * in the uber-space remapping window on the kernel.
210 */
211 struct fake_descriptor64 kernel_ldt_desc64 = {
212 0,
213 LDTSZ_MIN*sizeof(struct fake_descriptor)-1,
214 0,
215 ACC_P|ACC_PL_K|ACC_LDT,
216 0
217 };
218
219 /*
220 * This is the expanded, 64-bit variant of the kernel TSS descriptor.
221 * It is follows pattern of the KERNEL_LDT.
222 */
223 struct fake_descriptor64 kernel_tss_desc64 = {
224 0,
225 sizeof(struct x86_64_tss)-1,
226 0,
227 ACC_P|ACC_PL_K|ACC_TSS,
228 0
229 };
230
231 /*
232 * Convert a descriptor from fake to real format.
233 *
234 * Fake descriptor format:
235 * bytes 0..3 base 31..0
236 * bytes 4..5 limit 15..0
237 * byte 6 access byte 2 | limit 19..16
238 * byte 7 access byte 1
239 *
240 * Real descriptor format:
241 * bytes 0..1 limit 15..0
242 * bytes 2..3 base 15..0
243 * byte 4 base 23..16
244 * byte 5 access byte 1
245 * byte 6 access byte 2 | limit 19..16
246 * byte 7 base 31..24
247 *
248 * Fake gate format:
249 * bytes 0..3 offset
250 * bytes 4..5 selector
251 * byte 6 word count << 4 (to match fake descriptor)
252 * byte 7 access byte 1
253 *
254 * Real gate format:
255 * bytes 0..1 offset 15..0
256 * bytes 2..3 selector
257 * byte 4 word count
258 * byte 5 access byte 1
259 * bytes 6..7 offset 31..16
260 */
261 void
262 fix_desc(void *d, int num_desc) {
263 //early_kprintf("fix_desc(%x, %x)\n", d, num_desc);
264 uint8_t *desc = (uint8_t*) d;
265
266 do {
267 if ((desc[7] & 0x14) == 0x04) { /* gate */
268 uint32_t offset;
269 uint16_t selector;
270 uint8_t wordcount;
271 uint8_t acc;
272
273 offset = *((uint32_t*)(desc));
274 selector = *((uint32_t*)(desc+4));
275 wordcount = desc[6] >> 4;
276 acc = desc[7];
277
278 *((uint16_t*)desc) = offset & 0xFFFF;
279 *((uint16_t*)(desc+2)) = selector;
280 desc[4] = wordcount;
281 desc[5] = acc;
282 *((uint16_t*)(desc+6)) = offset >> 16;
283
284 } else { /* descriptor */
285 uint32_t base;
286 uint16_t limit;
287 uint8_t acc1, acc2;
288
289 base = *((uint32_t*)(desc));
290 limit = *((uint16_t*)(desc+4));
291 acc2 = desc[6];
292 acc1 = desc[7];
293
294 *((uint16_t*)(desc)) = limit;
295 *((uint16_t*)(desc+2)) = base & 0xFFFF;
296 desc[4] = (base >> 16) & 0xFF;
297 desc[5] = acc1;
298 desc[6] = acc2;
299 desc[7] = base >> 24;
300 }
301 desc += 8;
302 } while (--num_desc);
303 }
304
305 void
306 fix_desc64(void *descp, int count)
307 {
308 struct fake_descriptor64 *fakep;
309 union {
310 struct real_gate64 gate;
311 struct real_descriptor64 desc;
312 } real;
313 int i;
314
315 fakep = (struct fake_descriptor64 *) descp;
316
317 for (i = 0; i < count; i++, fakep++) {
318 /*
319 * Construct the real decriptor locally.
320 */
321
322 bzero((void *) &real, sizeof(real));
323
324 switch (fakep->access & ACC_TYPE) {
325 case 0:
326 break;
327 case ACC_CALL_GATE:
328 case ACC_INTR_GATE:
329 case ACC_TRAP_GATE:
330 real.gate.offset_low16 = (uint16_t)(fakep->offset64 & 0xFFFF);
331 real.gate.selector16 = fakep->lim_or_seg & 0xFFFF;
332 real.gate.IST = fakep->size_or_IST & 0x7;
333 real.gate.access8 = fakep->access;
334 real.gate.offset_high16 = (uint16_t)((fakep->offset64>>16) & 0xFFFF);
335 real.gate.offset_top32 = (uint32_t)(fakep->offset64>>32);
336 break;
337 default: /* Otherwise */
338 real.desc.limit_low16 = fakep->lim_or_seg & 0xFFFF;
339 real.desc.base_low16 = (uint16_t)(fakep->offset64 & 0xFFFF);
340 real.desc.base_med8 = (uint8_t)((fakep->offset64 >> 16) & 0xFF);
341 real.desc.access8 = fakep->access;
342 real.desc.limit_high4 = (fakep->lim_or_seg >> 16) & 0xFF;
343 real.desc.granularity4 = fakep->size_or_IST;
344 real.desc.base_high8 = (uint8_t)((fakep->offset64 >> 24) & 0xFF);
345 real.desc.base_top32 = (uint32_t)(fakep->offset64>>32);
346 }
347
348 /*
349 * Now copy back over the fake structure.
350 */
351 bcopy((void *) &real, (void *) fakep, sizeof(real));
352 }
353 }
354
355 static void
356 cpu_gdt_alias(vm_map_offset_t gdt, vm_map_offset_t alias)
357 {
358 pt_entry_t *pte = NULL;
359
360 /* Require page alignment */
361 assert(page_aligned(gdt));
362 assert(page_aligned(alias));
363
364 pte = pmap_pte(kernel_pmap, alias);
365 pmap_store_pte(pte, kvtophys(gdt) | INTEL_PTE_REF
366 | INTEL_PTE_MOD
367 | INTEL_PTE_WIRED
368 | INTEL_PTE_VALID
369 | INTEL_PTE_WRITE
370 | INTEL_PTE_NX);
371
372 /* TLB flush unneccessry because target processor isn't running yet */
373 }
374
375
376 void
377 cpu_desc_init64(cpu_data_t *cdp)
378 {
379 cpu_desc_index_t *cdi = &cdp->cpu_desc_index;
380
381 if (cdp == &cpu_data_master) {
382 /*
383 * Master CPU uses the tables built at boot time.
384 * Just set the index pointers to the low memory space.
385 */
386 cdi->cdi_ktss = (void *)&master_ktss64;
387 cdi->cdi_sstk = (vm_offset_t) &master_sstk.top;
388 cdi->cdi_gdt.ptr = (void *)MASTER_GDT_ALIAS;
389 cdi->cdi_idt.ptr = (void *)MASTER_IDT_ALIAS;
390 cdi->cdi_ldt = (struct fake_descriptor *) master_ldt;
391
392 /* Replace the expanded LDTs and TSS slots in the GDT */
393 kernel_ldt_desc64.offset64 = (uintptr_t) &master_ldt;
394 *(struct fake_descriptor64 *) &master_gdt[sel_idx(KERNEL_LDT)] =
395 kernel_ldt_desc64;
396 *(struct fake_descriptor64 *) &master_gdt[sel_idx(USER_LDT)] =
397 kernel_ldt_desc64;
398 kernel_tss_desc64.offset64 = (uintptr_t) &master_ktss64;
399 *(struct fake_descriptor64 *) &master_gdt[sel_idx(KERNEL_TSS)] =
400 kernel_tss_desc64;
401
402 /* Fix up the expanded descriptors for 64-bit. */
403 fix_desc64((void *) &master_idt64, IDTSZ);
404 fix_desc64((void *) &master_gdt[sel_idx(KERNEL_LDT)], 1);
405 fix_desc64((void *) &master_gdt[sel_idx(USER_LDT)], 1);
406 fix_desc64((void *) &master_gdt[sel_idx(KERNEL_TSS)], 1);
407
408 /*
409 * Set the NMI/fault stacks as IST2/IST1 in the 64-bit TSS
410 * Note: this will be dynamically re-allocated in VM later.
411 */
412 master_ktss64.ist2 = (uintptr_t) low_eintstack;
413 master_ktss64.ist1 = (uintptr_t) low_eintstack
414 - sizeof(x86_64_intr_stack_frame_t);
415
416 } else if (cdi->cdi_ktss == NULL) { /* Skipping re-init on wake */
417 cpu_desc_table64_t *cdt = (cpu_desc_table64_t *) cdp->cpu_desc_tablep;
418
419 /*
420 * Per-cpu GDT, IDT, KTSS descriptors are allocated in kernel
421 * heap (cpu_desc_table).
422 * LDT descriptors are mapped into a separate area.
423 * GDT descriptors are addressed by alias to avoid sgdt leaks to user-space.
424 */
425 cdi->cdi_idt.ptr = (void *)MASTER_IDT_ALIAS;
426 cdi->cdi_gdt.ptr = (void *)CPU_GDT_ALIAS(cdp->cpu_number);
427 cdi->cdi_ktss = (void *)&cdt->ktss;
428 cdi->cdi_sstk = (vm_offset_t)&cdt->sstk.top;
429 cdi->cdi_ldt = cdp->cpu_ldtp;
430
431 /* Make the virtual alias address for the GDT */
432 cpu_gdt_alias((vm_map_offset_t) &cdt->gdt,
433 (vm_map_offset_t) cdi->cdi_gdt.ptr);
434
435 /*
436 * Copy the tables
437 */
438 bcopy((char *)master_gdt, (char *)cdt->gdt, sizeof(master_gdt));
439 bcopy((char *)master_ldt, (char *)cdp->cpu_ldtp, sizeof(master_ldt));
440 bcopy((char *)&master_ktss64, (char *)&cdt->ktss, sizeof(struct x86_64_tss));
441
442 /*
443 * Fix up the entries in the GDT to point to
444 * this LDT and this TSS.
445 */
446 kernel_ldt_desc64.offset64 = (uintptr_t) cdi->cdi_ldt;
447 *(struct fake_descriptor64 *) &cdt->gdt[sel_idx(KERNEL_LDT)] =
448 kernel_ldt_desc64;
449 fix_desc64(&cdt->gdt[sel_idx(KERNEL_LDT)], 1);
450
451 kernel_ldt_desc64.offset64 = (uintptr_t) cdi->cdi_ldt;
452 *(struct fake_descriptor64 *) &cdt->gdt[sel_idx(USER_LDT)] =
453 kernel_ldt_desc64;
454 fix_desc64(&cdt->gdt[sel_idx(USER_LDT)], 1);
455
456 kernel_tss_desc64.offset64 = (uintptr_t) cdi->cdi_ktss;
457 *(struct fake_descriptor64 *) &cdt->gdt[sel_idx(KERNEL_TSS)] =
458 kernel_tss_desc64;
459 fix_desc64(&cdt->gdt[sel_idx(KERNEL_TSS)], 1);
460
461 /* Set (zeroed) fault stack as IST1, NMI intr stack IST2 */
462 bzero((void *) cdt->fstk, sizeof(cdt->fstk));
463 cdt->ktss.ist2 = (unsigned long)cdt->fstk + sizeof(cdt->fstk);
464 cdt->ktss.ist1 = cdt->ktss.ist2
465 - sizeof(x86_64_intr_stack_frame_t);
466 }
467
468 /* Require that the top of the sysenter stack is 16-byte aligned */
469 if ((cdi->cdi_sstk % 16) != 0)
470 panic("cpu_desc_init64() sysenter stack not 16-byte aligned");
471 }
472
473
474 void
475 cpu_desc_load64(cpu_data_t *cdp)
476 {
477 cpu_desc_index_t *cdi = &cdp->cpu_desc_index;
478
479 /* Stuff the kernel per-cpu data area address into the MSRs */
480 wrmsr64(MSR_IA32_GS_BASE, (uintptr_t) cdp);
481 wrmsr64(MSR_IA32_KERNEL_GS_BASE, (uintptr_t) cdp);
482
483 /*
484 * Ensure the TSS segment's busy bit is clear. This is required
485 * for the case of reloading descriptors at wake to avoid
486 * their complete re-initialization.
487 */
488 gdt_desc_p(KERNEL_TSS)->access &= ~ACC_TSS_BUSY;
489
490 /* Load the GDT, LDT, IDT and TSS */
491 cdi->cdi_gdt.size = sizeof(struct real_descriptor)*GDTSZ - 1;
492 cdi->cdi_idt.size = 0x1000 + cdp->cpu_number;
493 lgdt((uintptr_t *) &cdi->cdi_gdt);
494 lidt((uintptr_t *) &cdi->cdi_idt);
495 lldt(KERNEL_LDT);
496 set_tr(KERNEL_TSS);
497
498 #if GPROF // Hack to enable mcount to work on K64
499 __asm__ volatile("mov %0, %%gs" : : "rm" ((unsigned short)(KERNEL_DS)));
500 #endif
501 }
502
503
504 /*
505 * Set MSRs for sysenter/sysexit and syscall/sysret for 64-bit.
506 */
507 static void
508 fast_syscall_init64(__unused cpu_data_t *cdp)
509 {
510 wrmsr64(MSR_IA32_SYSENTER_CS, SYSENTER_CS);
511 wrmsr64(MSR_IA32_SYSENTER_EIP, (uintptr_t) hi64_sysenter);
512 wrmsr64(MSR_IA32_SYSENTER_ESP, current_sstk());
513 /* Enable syscall/sysret */
514 wrmsr64(MSR_IA32_EFER, rdmsr64(MSR_IA32_EFER) | MSR_IA32_EFER_SCE);
515
516 /*
517 * MSRs for 64-bit syscall/sysret
518 * Note USER_CS because sysret uses this + 16 when returning to
519 * 64-bit code.
520 */
521 wrmsr64(MSR_IA32_LSTAR, (uintptr_t) hi64_syscall);
522 wrmsr64(MSR_IA32_STAR, (((uint64_t)USER_CS) << 48) |
523 (((uint64_t)KERNEL64_CS) << 32));
524 /*
525 * Emulate eflags cleared by sysenter but note that
526 * we also clear the trace trap to avoid the complications
527 * of single-stepping into a syscall. The nested task bit
528 * is also cleared to avoid a spurious "task switch"
529 * should we choose to return via an IRET.
530 */
531 wrmsr64(MSR_IA32_FMASK, EFL_DF|EFL_IF|EFL_TF|EFL_NT);
532
533 }
534
535
536 cpu_data_t *
537 cpu_data_alloc(boolean_t is_boot_cpu)
538 {
539 int ret;
540 cpu_data_t *cdp;
541
542 if (is_boot_cpu) {
543 assert(real_ncpus == 1);
544 cdp = cpu_datap(0);
545 if (cdp->cpu_processor == NULL) {
546 simple_lock_init(&ncpus_lock, 0);
547 cdp->cpu_processor = cpu_processor_alloc(TRUE);
548 #if NCOPY_WINDOWS > 0
549 cdp->cpu_pmap = pmap_cpu_alloc(TRUE);
550 #endif
551 }
552 return cdp;
553 }
554
555 /*
556 * Allocate per-cpu data:
557 */
558 ret = kmem_alloc(kernel_map, (vm_offset_t *) &cdp, sizeof(cpu_data_t), VM_KERN_MEMORY_CPU);
559 if (ret != KERN_SUCCESS) {
560 printf("cpu_data_alloc() failed, ret=%d\n", ret);
561 goto abort;
562 }
563 bzero((void*) cdp, sizeof(cpu_data_t));
564 cdp->cpu_this = cdp;
565
566 /*
567 * Allocate interrupt stack:
568 */
569 ret = kmem_alloc(kernel_map,
570 (vm_offset_t *) &cdp->cpu_int_stack_top,
571 INTSTACK_SIZE, VM_KERN_MEMORY_CPU);
572 if (ret != KERN_SUCCESS) {
573 printf("cpu_data_alloc() int stack failed, ret=%d\n", ret);
574 goto abort;
575 }
576 bzero((void*) cdp->cpu_int_stack_top, INTSTACK_SIZE);
577 cdp->cpu_int_stack_top += INTSTACK_SIZE;
578
579 /*
580 * Allocate descriptor table:
581 */
582 ret = kmem_alloc(kernel_map,
583 (vm_offset_t *) &cdp->cpu_desc_tablep,
584 sizeof(cpu_desc_table64_t),
585 VM_KERN_MEMORY_CPU);
586 if (ret != KERN_SUCCESS) {
587 printf("cpu_data_alloc() desc_table failed, ret=%d\n", ret);
588 goto abort;
589 }
590
591 /*
592 * Allocate LDT
593 */
594 ret = kmem_alloc(kernel_map,
595 (vm_offset_t *) &cdp->cpu_ldtp,
596 sizeof(struct real_descriptor) * LDTSZ,
597 VM_KERN_MEMORY_CPU);
598 if (ret != KERN_SUCCESS) {
599 printf("cpu_data_alloc() ldt failed, ret=%d\n", ret);
600 goto abort;
601 }
602
603 #if CONFIG_MCA
604 /* Machine-check shadow register allocation. */
605 mca_cpu_alloc(cdp);
606 #endif
607
608 simple_lock(&ncpus_lock);
609
610 cpu_data_ptr[real_ncpus] = cdp;
611 cdp->cpu_number = real_ncpus;
612 real_ncpus++;
613 simple_unlock(&ncpus_lock);
614
615 /*
616 * Before this cpu has been assigned a real thread context,
617 * we give it a fake, unique, non-zero thread id which the locking
618 * primitives use as their lock value.
619 * Note that this does not apply to the boot processor, cpu 0, which
620 * transitions to a thread context well before other processors are
621 * started.
622 */
623 cdp->cpu_active_thread = (thread_t) (uintptr_t) cdp->cpu_number;
624
625 cdp->cpu_nanotime = &pal_rtc_nanotime_info;
626
627 kprintf("cpu_data_alloc(%d) %p desc_table: %p "
628 "ldt: %p "
629 "int_stack: 0x%lx-0x%lx\n",
630 cdp->cpu_number, cdp, cdp->cpu_desc_tablep, cdp->cpu_ldtp,
631 (long)(cdp->cpu_int_stack_top - INTSTACK_SIZE), (long)(cdp->cpu_int_stack_top));
632
633 return cdp;
634
635 abort:
636 if (cdp) {
637 if (cdp->cpu_desc_tablep)
638 kfree((void *) cdp->cpu_desc_tablep,
639 sizeof(cpu_desc_table64_t));
640 if (cdp->cpu_int_stack_top)
641 kfree((void *) (cdp->cpu_int_stack_top - INTSTACK_SIZE),
642 INTSTACK_SIZE);
643 kfree((void *) cdp, sizeof(*cdp));
644 }
645 return NULL;
646 }
647
648 boolean_t
649 valid_user_data_selector(uint16_t selector)
650 {
651 sel_t sel = selector_to_sel(selector);
652
653 if (selector == 0)
654 return (TRUE);
655
656 if (sel.ti == SEL_LDT)
657 return (TRUE);
658 else if (sel.index < GDTSZ) {
659 if ((gdt_desc_p(selector)->access & ACC_PL_U) == ACC_PL_U)
660 return (TRUE);
661 }
662
663 return (FALSE);
664 }
665
666 boolean_t
667 valid_user_code_selector(uint16_t selector)
668 {
669 sel_t sel = selector_to_sel(selector);
670
671 if (selector == 0)
672 return (FALSE);
673
674 if (sel.ti == SEL_LDT) {
675 if (sel.rpl == USER_PRIV)
676 return (TRUE);
677 }
678 else if (sel.index < GDTSZ && sel.rpl == USER_PRIV) {
679 if ((gdt_desc_p(selector)->access & ACC_PL_U) == ACC_PL_U)
680 return (TRUE);
681 /* Explicitly validate the system code selectors
682 * even if not instantaneously privileged,
683 * since they are dynamically re-privileged
684 * at context switch
685 */
686 if ((selector == USER_CS) || (selector == USER64_CS))
687 return (TRUE);
688 }
689
690 return (FALSE);
691 }
692
693 boolean_t
694 valid_user_stack_selector(uint16_t selector)
695 {
696 sel_t sel = selector_to_sel(selector);
697
698 if (selector == 0)
699 return (FALSE);
700
701 if (sel.ti == SEL_LDT) {
702 if (sel.rpl == USER_PRIV)
703 return (TRUE);
704 }
705 else if (sel.index < GDTSZ && sel.rpl == USER_PRIV) {
706 if ((gdt_desc_p(selector)->access & ACC_PL_U) == ACC_PL_U)
707 return (TRUE);
708 }
709
710 return (FALSE);
711 }
712
713 boolean_t
714 valid_user_segment_selectors(uint16_t cs,
715 uint16_t ss,
716 uint16_t ds,
717 uint16_t es,
718 uint16_t fs,
719 uint16_t gs)
720 {
721 return valid_user_code_selector(cs) &&
722 valid_user_stack_selector(ss) &&
723 valid_user_data_selector(ds) &&
724 valid_user_data_selector(es) &&
725 valid_user_data_selector(fs) &&
726 valid_user_data_selector(gs);
727 }
728
729 #if NCOPY_WINDOWS > 0
730
731 static vm_offset_t user_window_base = 0;
732
733 void
734 cpu_userwindow_init(int cpu)
735 {
736 cpu_data_t *cdp = cpu_data_ptr[cpu];
737 vm_offset_t user_window;
738 vm_offset_t vaddr;
739 int num_cpus;
740
741 num_cpus = ml_get_max_cpus();
742
743 if (cpu >= num_cpus)
744 panic("cpu_userwindow_init: cpu > num_cpus");
745
746 if (user_window_base == 0) {
747
748 if (vm_allocate(kernel_map, &vaddr,
749 (NBPDE * NCOPY_WINDOWS * num_cpus) + NBPDE,
750 VM_FLAGS_ANYWHERE | VM_MAKE_TAG(VM_KERN_MEMORY_CPU)) != KERN_SUCCESS)
751 panic("cpu_userwindow_init: "
752 "couldn't allocate user map window");
753
754 /*
755 * window must start on a page table boundary
756 * in the virtual address space
757 */
758 user_window_base = (vaddr + (NBPDE - 1)) & ~(NBPDE - 1);
759
760 /*
761 * get rid of any allocation leading up to our
762 * starting boundary
763 */
764 vm_deallocate(kernel_map, vaddr, user_window_base - vaddr);
765
766 /*
767 * get rid of tail that we don't need
768 */
769 user_window = user_window_base +
770 (NBPDE * NCOPY_WINDOWS * num_cpus);
771
772 vm_deallocate(kernel_map, user_window,
773 (vaddr +
774 ((NBPDE * NCOPY_WINDOWS * num_cpus) + NBPDE)) -
775 user_window);
776 }
777
778 user_window = user_window_base + (cpu * NCOPY_WINDOWS * NBPDE);
779
780 cdp->cpu_copywindow_base = user_window;
781 /*
782 * Abuse this pdp entry, the pdp now actually points to
783 * an array of copy windows addresses.
784 */
785 cdp->cpu_copywindow_pdp = pmap_pde(kernel_pmap, user_window);
786
787 }
788
789 void
790 cpu_physwindow_init(int cpu)
791 {
792 cpu_data_t *cdp = cpu_data_ptr[cpu];
793 vm_offset_t phys_window = cdp->cpu_physwindow_base;
794
795 if (phys_window == 0) {
796 if (vm_allocate(kernel_map, &phys_window,
797 PAGE_SIZE, VM_FLAGS_ANYWHERE | VM_MAKE_TAG(VM_KERN_MEMORY_CPU))
798 != KERN_SUCCESS)
799 panic("cpu_physwindow_init: "
800 "couldn't allocate phys map window");
801
802 /*
803 * make sure the page that encompasses the
804 * pte pointer we're interested in actually
805 * exists in the page table
806 */
807 pmap_expand(kernel_pmap, phys_window, PMAP_EXPAND_OPTIONS_NONE);
808
809 cdp->cpu_physwindow_base = phys_window;
810 cdp->cpu_physwindow_ptep = vtopte(phys_window);
811 }
812 }
813 #endif /* NCOPY_WINDOWS > 0 */
814
815 /*
816 * Load the segment descriptor tables for the current processor.
817 */
818 void
819 cpu_mode_init(cpu_data_t *cdp)
820 {
821 fast_syscall_init64(cdp);
822 }
823
824 /*
825 * Allocate a new interrupt stack for the boot processor from the
826 * heap rather than continue to use the statically allocated space.
827 * Also switch to a dynamically allocated cpu data area.
828 */
829 void
830 cpu_data_realloc(void)
831 {
832 int ret;
833 vm_offset_t istk;
834 vm_offset_t fstk;
835 cpu_data_t *cdp;
836 boolean_t istate;
837
838 ret = kmem_alloc(kernel_map, &istk, INTSTACK_SIZE, VM_KERN_MEMORY_CPU);
839 if (ret != KERN_SUCCESS) {
840 panic("cpu_data_realloc() stack alloc, ret=%d\n", ret);
841 }
842 bzero((void*) istk, INTSTACK_SIZE);
843 istk += INTSTACK_SIZE;
844
845 ret = kmem_alloc(kernel_map, (vm_offset_t *) &cdp, sizeof(cpu_data_t), VM_KERN_MEMORY_CPU);
846 if (ret != KERN_SUCCESS) {
847 panic("cpu_data_realloc() cpu data alloc, ret=%d\n", ret);
848 }
849
850 /* Copy old contents into new area and make fix-ups */
851 assert(cpu_number() == 0);
852 bcopy((void *) cpu_data_ptr[0], (void*) cdp, sizeof(cpu_data_t));
853 cdp->cpu_this = cdp;
854 cdp->cpu_int_stack_top = istk;
855 timer_call_queue_init(&cdp->rtclock_timer.queue);
856
857 /* Allocate the separate fault stack */
858 ret = kmem_alloc(kernel_map, &fstk, PAGE_SIZE, VM_KERN_MEMORY_CPU);
859 if (ret != KERN_SUCCESS) {
860 panic("cpu_data_realloc() fault stack alloc, ret=%d\n", ret);
861 }
862 bzero((void*) fstk, PAGE_SIZE);
863 fstk += PAGE_SIZE;
864
865 /*
866 * With interrupts disabled commmit the new areas.
867 */
868 istate = ml_set_interrupts_enabled(FALSE);
869 cpu_data_ptr[0] = cdp;
870 master_ktss64.ist2 = (uintptr_t) fstk;
871 master_ktss64.ist1 = (uintptr_t) fstk
872 - sizeof(x86_64_intr_stack_frame_t);
873 wrmsr64(MSR_IA32_GS_BASE, (uintptr_t) cdp);
874 wrmsr64(MSR_IA32_KERNEL_GS_BASE, (uintptr_t) cdp);
875 (void) ml_set_interrupts_enabled(istate);
876
877 kprintf("Reallocated master cpu data: %p,"
878 " interrupt stack: %p, fault stack: %p\n",
879 (void *) cdp, (void *) istk, (void *) fstk);
880 }