2 * Copyright (c) 2000-2012 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * Mach Operating System
33 * Copyright (c) 1991,1990 Carnegie Mellon University
34 * All Rights Reserved.
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.
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.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
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>
70 #include <i386/lock.h>
71 #include <i386/mp_desc.h>
72 #include <i386/misc_protos.h>
74 #include <i386/pmap.h>
75 #if defined(__i386__) || defined(__x86_64__)
76 #include <i386/pmap_internal.h>
79 #include <i386/machine_check.h>
82 #include <kern/misc_protos.h>
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)
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 ;
97 // Include the table to declare the externs
98 #include "../x86_64/idt_table.h"
100 // Undef the macros, then redefine them so we can declare the table
110 #define TRAP(n, name) \
119 #define TRAP_ERR TRAP
120 #define TRAP_SPC TRAP
122 #define TRAP_IST1(n, name) \
131 #define TRAP_IST2(n, name) \
140 #define INTERRUPT(n) \
142 (uintptr_t)&_intr_ ## n,\
149 #define USER_TRAP(n, name) \
158 #define USER_TRAP_SPC USER_TRAP
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"
168 * First cpu`s interrupt stack.
170 extern uint32_t low_intstack
[]; /* bottom */
171 extern uint32_t low_eintstack
[]; /* top */
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.
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
,
183 cpu_data_t
*cpu_data_ptr
[MAX_CPUS
] = { [0] = &cpu_data_master
};
185 decl_simple_lock_data(,ncpus_lock
); /* protects real_ncpus */
186 unsigned int real_ncpus
= 1;
187 unsigned int max_ncpus
= MAX_CPUS
;
189 extern void hi64_sysenter(void);
190 extern void hi64_syscall(void);
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.
202 * Allocate and initialize the per-processor descriptor tables.
205 struct fake_descriptor ldt_desc_pattern
= {
207 LDTSZ_MIN
* sizeof(struct fake_descriptor
) - 1,
209 ACC_P
|ACC_PL_K
|ACC_LDT
212 struct fake_descriptor tss_desc_pattern
= {
214 sizeof(struct i386_tss
) - 1,
216 ACC_P
|ACC_PL_K
|ACC_TSS
219 struct fake_descriptor cpudata_desc_pattern
= {
221 sizeof(cpu_data_t
)-1,
223 ACC_P
|ACC_PL_K
|ACC_DATA_W
226 #if NCOPY_WINDOWS > 0
227 struct fake_descriptor userwindow_desc_pattern
= {
229 ((NBPDE
* NCOPY_WINDOWS
) / PAGE_SIZE
) - 1,
231 ACC_P
|ACC_PL_U
|ACC_DATA_W
235 struct fake_descriptor physwindow_desc_pattern
= {
239 ACC_P
|ACC_PL_K
|ACC_DATA_W
243 * This is the expanded, 64-bit variant of the kernel LDT descriptor.
244 * When switching to 64-bit mode this replaces KERNEL_LDT entry
245 * and the following empty slot. This enables the LDT to be referenced
246 * in the uber-space remapping window on the kernel.
248 struct fake_descriptor64 kernel_ldt_desc64
= {
250 LDTSZ_MIN
*sizeof(struct fake_descriptor
)-1,
252 ACC_P
|ACC_PL_K
|ACC_LDT
,
257 * This is the expanded, 64-bit variant of the kernel TSS descriptor.
258 * It is follows pattern of the KERNEL_LDT.
260 struct fake_descriptor64 kernel_tss_desc64
= {
262 sizeof(struct x86_64_tss
)-1,
264 ACC_P
|ACC_PL_K
|ACC_TSS
,
269 * Convert a descriptor from fake to real format.
271 * Fake descriptor format:
272 * bytes 0..3 base 31..0
273 * bytes 4..5 limit 15..0
274 * byte 6 access byte 2 | limit 19..16
275 * byte 7 access byte 1
277 * Real descriptor format:
278 * bytes 0..1 limit 15..0
279 * bytes 2..3 base 15..0
281 * byte 5 access byte 1
282 * byte 6 access byte 2 | limit 19..16
287 * bytes 4..5 selector
288 * byte 6 word count << 4 (to match fake descriptor)
289 * byte 7 access byte 1
292 * bytes 0..1 offset 15..0
293 * bytes 2..3 selector
295 * byte 5 access byte 1
296 * bytes 6..7 offset 31..16
299 fix_desc(void *d
, int num_desc
) {
300 //early_kprintf("fix_desc(%x, %x)\n", d, num_desc);
301 uint8_t *desc
= (uint8_t*) d
;
304 if ((desc
[7] & 0x14) == 0x04) { /* gate */
310 offset
= *((uint32_t*)(desc
));
311 selector
= *((uint32_t*)(desc
+4));
312 wordcount
= desc
[6] >> 4;
315 *((uint16_t*)desc
) = offset
& 0xFFFF;
316 *((uint16_t*)(desc
+2)) = selector
;
319 *((uint16_t*)(desc
+6)) = offset
>> 16;
321 } else { /* descriptor */
326 base
= *((uint32_t*)(desc
));
327 limit
= *((uint16_t*)(desc
+4));
331 *((uint16_t*)(desc
)) = limit
;
332 *((uint16_t*)(desc
+2)) = base
& 0xFFFF;
333 desc
[4] = (base
>> 16) & 0xFF;
336 desc
[7] = base
>> 24;
339 } while (--num_desc
);
343 fix_desc64(void *descp
, int count
)
345 struct fake_descriptor64
*fakep
;
347 struct real_gate64 gate
;
348 struct real_descriptor64 desc
;
352 fakep
= (struct fake_descriptor64
*) descp
;
354 for (i
= 0; i
< count
; i
++, fakep
++) {
356 * Construct the real decriptor locally.
359 bzero((void *) &real
, sizeof(real
));
361 switch (fakep
->access
& ACC_TYPE
) {
367 real
.gate
.offset_low16
= (uint16_t)(fakep
->offset64
& 0xFFFF);
368 real
.gate
.selector16
= fakep
->lim_or_seg
& 0xFFFF;
369 real
.gate
.IST
= fakep
->size_or_IST
& 0x7;
370 real
.gate
.access8
= fakep
->access
;
371 real
.gate
.offset_high16
= (uint16_t)((fakep
->offset64
>>16) & 0xFFFF);
372 real
.gate
.offset_top32
= (uint32_t)(fakep
->offset64
>>32);
374 default: /* Otherwise */
375 real
.desc
.limit_low16
= fakep
->lim_or_seg
& 0xFFFF;
376 real
.desc
.base_low16
= (uint16_t)(fakep
->offset64
& 0xFFFF);
377 real
.desc
.base_med8
= (uint8_t)((fakep
->offset64
>> 16) & 0xFF);
378 real
.desc
.access8
= fakep
->access
;
379 real
.desc
.limit_high4
= (fakep
->lim_or_seg
>> 16) & 0xFF;
380 real
.desc
.granularity4
= fakep
->size_or_IST
;
381 real
.desc
.base_high8
= (uint8_t)((fakep
->offset64
>> 24) & 0xFF);
382 real
.desc
.base_top32
= (uint32_t)(fakep
->offset64
>>32);
386 * Now copy back over the fake structure.
388 bcopy((void *) &real
, (void *) fakep
, sizeof(real
));
393 cpu_gdt_alias(vm_map_offset_t gdt
, vm_map_offset_t alias
)
395 pt_entry_t
*pte
= NULL
;
397 /* Require page alignment */
398 assert(page_aligned(gdt
));
399 assert(page_aligned(alias
));
401 pte
= pmap_pte(kernel_pmap
, alias
);
402 pmap_store_pte(pte
, kvtophys(gdt
) | INTEL_PTE_REF
409 /* TLB flush unneccessry because target processor isn't running yet */
414 cpu_desc_init64(cpu_data_t
*cdp
)
416 cpu_desc_index_t
*cdi
= &cdp
->cpu_desc_index
;
418 if (cdp
== &cpu_data_master
) {
420 * Master CPU uses the tables built at boot time.
421 * Just set the index pointers to the low memory space.
423 cdi
->cdi_ktss
= (void *)&master_ktss64
;
424 cdi
->cdi_sstk
= (vm_offset_t
) &master_sstk
.top
;
425 cdi
->cdi_gdt
.ptr
= (void *)MASTER_GDT_ALIAS
;
426 cdi
->cdi_idt
.ptr
= (void *)MASTER_IDT_ALIAS
;
427 cdi
->cdi_ldt
= (struct fake_descriptor
*) master_ldt
;
429 /* Replace the expanded LDTs and TSS slots in the GDT */
430 kernel_ldt_desc64
.offset64
= (uintptr_t) &master_ldt
;
431 *(struct fake_descriptor64
*) &master_gdt
[sel_idx(KERNEL_LDT
)] =
433 *(struct fake_descriptor64
*) &master_gdt
[sel_idx(USER_LDT
)] =
435 kernel_tss_desc64
.offset64
= (uintptr_t) &master_ktss64
;
436 *(struct fake_descriptor64
*) &master_gdt
[sel_idx(KERNEL_TSS
)] =
439 /* Fix up the expanded descriptors for 64-bit. */
440 fix_desc64((void *) &master_idt64
, IDTSZ
);
441 fix_desc64((void *) &master_gdt
[sel_idx(KERNEL_LDT
)], 1);
442 fix_desc64((void *) &master_gdt
[sel_idx(USER_LDT
)], 1);
443 fix_desc64((void *) &master_gdt
[sel_idx(KERNEL_TSS
)], 1);
446 * Set the NMI/fault stacks as IST2/IST1 in the 64-bit TSS
447 * Note: this will be dynamically re-allocated in VM later.
449 master_ktss64
.ist2
= (uintptr_t) low_eintstack
;
450 master_ktss64
.ist1
= (uintptr_t) low_eintstack
451 - sizeof(x86_64_intr_stack_frame_t
);
453 } else if (cdi
->cdi_ktss
== NULL
) { /* Skipping re-init on wake */
454 cpu_desc_table64_t
*cdt
= (cpu_desc_table64_t
*) cdp
->cpu_desc_tablep
;
457 * Per-cpu GDT, IDT, KTSS descriptors are allocated in kernel
458 * heap (cpu_desc_table).
459 * LDT descriptors are mapped into a separate area.
460 * GDT descriptors are addressed by alias to avoid sgdt leaks to user-space.
462 cdi
->cdi_idt
.ptr
= (void *)MASTER_IDT_ALIAS
;
463 cdi
->cdi_gdt
.ptr
= (void *)CPU_GDT_ALIAS(cdp
->cpu_number
);
464 cdi
->cdi_ktss
= (void *)&cdt
->ktss
;
465 cdi
->cdi_sstk
= (vm_offset_t
)&cdt
->sstk
.top
;
466 cdi
->cdi_ldt
= cdp
->cpu_ldtp
;
468 /* Make the virtual alias address for the GDT */
469 cpu_gdt_alias((vm_map_offset_t
) &cdt
->gdt
,
470 (vm_map_offset_t
) cdi
->cdi_gdt
.ptr
);
475 bcopy((char *)master_gdt
, (char *)cdt
->gdt
, sizeof(master_gdt
));
476 bcopy((char *)master_ldt
, (char *)cdp
->cpu_ldtp
, sizeof(master_ldt
));
477 bcopy((char *)&master_ktss64
, (char *)&cdt
->ktss
, sizeof(struct x86_64_tss
));
480 * Fix up the entries in the GDT to point to
481 * this LDT and this TSS.
483 kernel_ldt_desc64
.offset64
= (uintptr_t) cdi
->cdi_ldt
;
484 *(struct fake_descriptor64
*) &cdt
->gdt
[sel_idx(KERNEL_LDT
)] =
486 fix_desc64(&cdt
->gdt
[sel_idx(KERNEL_LDT
)], 1);
488 kernel_ldt_desc64
.offset64
= (uintptr_t) cdi
->cdi_ldt
;
489 *(struct fake_descriptor64
*) &cdt
->gdt
[sel_idx(USER_LDT
)] =
491 fix_desc64(&cdt
->gdt
[sel_idx(USER_LDT
)], 1);
493 kernel_tss_desc64
.offset64
= (uintptr_t) cdi
->cdi_ktss
;
494 *(struct fake_descriptor64
*) &cdt
->gdt
[sel_idx(KERNEL_TSS
)] =
496 fix_desc64(&cdt
->gdt
[sel_idx(KERNEL_TSS
)], 1);
498 /* Set (zeroed) fault stack as IST1, NMI intr stack IST2 */
499 bzero((void *) cdt
->fstk
, sizeof(cdt
->fstk
));
500 cdt
->ktss
.ist2
= (unsigned long)cdt
->fstk
+ sizeof(cdt
->fstk
);
501 cdt
->ktss
.ist1
= cdt
->ktss
.ist2
502 - sizeof(x86_64_intr_stack_frame_t
);
505 /* Require that the top of the sysenter stack is 16-byte aligned */
506 if ((cdi
->cdi_sstk
% 16) != 0)
507 panic("cpu_desc_init64() sysenter stack not 16-byte aligned");
512 cpu_desc_load64(cpu_data_t
*cdp
)
514 cpu_desc_index_t
*cdi
= &cdp
->cpu_desc_index
;
516 /* Stuff the kernel per-cpu data area address into the MSRs */
517 wrmsr64(MSR_IA32_GS_BASE
, (uintptr_t) cdp
);
518 wrmsr64(MSR_IA32_KERNEL_GS_BASE
, (uintptr_t) cdp
);
521 * Ensure the TSS segment's busy bit is clear. This is required
522 * for the case of reloading descriptors at wake to avoid
523 * their complete re-initialization.
525 gdt_desc_p(KERNEL_TSS
)->access
&= ~ACC_TSS_BUSY
;
527 /* Load the GDT, LDT, IDT and TSS */
528 cdi
->cdi_gdt
.size
= sizeof(struct real_descriptor
)*GDTSZ
- 1;
529 cdi
->cdi_idt
.size
= 0x1000 + cdp
->cpu_number
;
530 lgdt((uintptr_t *) &cdi
->cdi_gdt
);
531 lidt((uintptr_t *) &cdi
->cdi_idt
);
535 #if GPROF // Hack to enable mcount to work on K64
536 __asm__
volatile("mov %0, %%gs" : : "rm" ((unsigned short)(KERNEL_DS
)));
542 * Set MSRs for sysenter/sysexit and syscall/sysret for 64-bit.
545 fast_syscall_init64(__unused cpu_data_t
*cdp
)
547 wrmsr64(MSR_IA32_SYSENTER_CS
, SYSENTER_CS
);
548 wrmsr64(MSR_IA32_SYSENTER_EIP
, (uintptr_t) hi64_sysenter
);
549 wrmsr64(MSR_IA32_SYSENTER_ESP
, current_sstk());
550 /* Enable syscall/sysret */
551 wrmsr64(MSR_IA32_EFER
, rdmsr64(MSR_IA32_EFER
) | MSR_IA32_EFER_SCE
);
554 * MSRs for 64-bit syscall/sysret
555 * Note USER_CS because sysret uses this + 16 when returning to
558 wrmsr64(MSR_IA32_LSTAR
, (uintptr_t) hi64_syscall
);
559 wrmsr64(MSR_IA32_STAR
, (((uint64_t)USER_CS
) << 48) |
560 (((uint64_t)KERNEL64_CS
) << 32));
562 * Emulate eflags cleared by sysenter but note that
563 * we also clear the trace trap to avoid the complications
564 * of single-stepping into a syscall. The nested task bit
565 * is also cleared to avoid a spurious "task switch"
566 * should we choose to return via an IRET.
568 wrmsr64(MSR_IA32_FMASK
, EFL_DF
|EFL_IF
|EFL_TF
|EFL_NT
);
574 cpu_data_alloc(boolean_t is_boot_cpu
)
580 assert(real_ncpus
== 1);
582 if (cdp
->cpu_processor
== NULL
) {
583 simple_lock_init(&ncpus_lock
, 0);
584 cdp
->cpu_processor
= cpu_processor_alloc(TRUE
);
585 #if NCOPY_WINDOWS > 0
586 cdp
->cpu_pmap
= pmap_cpu_alloc(TRUE
);
593 * Allocate per-cpu data:
595 ret
= kmem_alloc(kernel_map
, (vm_offset_t
*) &cdp
, sizeof(cpu_data_t
));
596 if (ret
!= KERN_SUCCESS
) {
597 printf("cpu_data_alloc() failed, ret=%d\n", ret
);
600 bzero((void*) cdp
, sizeof(cpu_data_t
));
604 * Allocate interrupt stack:
606 ret
= kmem_alloc(kernel_map
,
607 (vm_offset_t
*) &cdp
->cpu_int_stack_top
,
609 if (ret
!= KERN_SUCCESS
) {
610 printf("cpu_data_alloc() int stack failed, ret=%d\n", ret
);
613 bzero((void*) cdp
->cpu_int_stack_top
, INTSTACK_SIZE
);
614 cdp
->cpu_int_stack_top
+= INTSTACK_SIZE
;
617 * Allocate descriptor table:
619 ret
= kmem_alloc(kernel_map
,
620 (vm_offset_t
*) &cdp
->cpu_desc_tablep
,
621 sizeof(cpu_desc_table64_t
));
622 if (ret
!= KERN_SUCCESS
) {
623 printf("cpu_data_alloc() desc_table failed, ret=%d\n", ret
);
630 ret
= kmem_alloc(kernel_map
,
631 (vm_offset_t
*) &cdp
->cpu_ldtp
,
632 sizeof(struct real_descriptor
) * LDTSZ
);
633 if (ret
!= KERN_SUCCESS
) {
634 printf("cpu_data_alloc() ldt failed, ret=%d\n", ret
);
639 /* Machine-check shadow register allocation. */
643 simple_lock(&ncpus_lock
);
645 cpu_data_ptr
[real_ncpus
] = cdp
;
646 cdp
->cpu_number
= real_ncpus
;
648 simple_unlock(&ncpus_lock
);
650 cdp
->cpu_nanotime
= &pal_rtc_nanotime_info
;
652 kprintf("cpu_data_alloc(%d) %p desc_table: %p "
654 "int_stack: 0x%lx-0x%lx\n",
655 cdp
->cpu_number
, cdp
, cdp
->cpu_desc_tablep
, cdp
->cpu_ldtp
,
656 (long)(cdp
->cpu_int_stack_top
- INTSTACK_SIZE
), (long)(cdp
->cpu_int_stack_top
));
662 if (cdp
->cpu_desc_tablep
)
663 kfree((void *) cdp
->cpu_desc_tablep
,
664 sizeof(cpu_desc_table64_t
));
665 if (cdp
->cpu_int_stack_top
)
666 kfree((void *) (cdp
->cpu_int_stack_top
- INTSTACK_SIZE
),
668 kfree((void *) cdp
, sizeof(*cdp
));
674 valid_user_data_selector(uint16_t selector
)
676 sel_t sel
= selector_to_sel(selector
);
681 if (sel
.ti
== SEL_LDT
)
683 else if (sel
.index
< GDTSZ
) {
684 if ((gdt_desc_p(selector
)->access
& ACC_PL_U
) == ACC_PL_U
)
692 valid_user_code_selector(uint16_t selector
)
694 sel_t sel
= selector_to_sel(selector
);
699 if (sel
.ti
== SEL_LDT
) {
700 if (sel
.rpl
== USER_PRIV
)
703 else if (sel
.index
< GDTSZ
&& sel
.rpl
== USER_PRIV
) {
704 if ((gdt_desc_p(selector
)->access
& ACC_PL_U
) == ACC_PL_U
)
712 valid_user_stack_selector(uint16_t selector
)
714 sel_t sel
= selector_to_sel(selector
);
719 if (sel
.ti
== SEL_LDT
) {
720 if (sel
.rpl
== USER_PRIV
)
723 else if (sel
.index
< GDTSZ
&& sel
.rpl
== USER_PRIV
) {
724 if ((gdt_desc_p(selector
)->access
& ACC_PL_U
) == ACC_PL_U
)
732 valid_user_segment_selectors(uint16_t cs
,
739 return valid_user_code_selector(cs
) &&
740 valid_user_stack_selector(ss
) &&
741 valid_user_data_selector(ds
) &&
742 valid_user_data_selector(es
) &&
743 valid_user_data_selector(fs
) &&
744 valid_user_data_selector(gs
);
747 #if NCOPY_WINDOWS > 0
749 static vm_offset_t user_window_base
= 0;
752 cpu_userwindow_init(int cpu
)
754 cpu_data_t
*cdp
= cpu_data_ptr
[cpu
];
755 vm_offset_t user_window
;
759 num_cpus
= ml_get_max_cpus();
762 panic("cpu_userwindow_init: cpu > num_cpus");
764 if (user_window_base
== 0) {
766 if (vm_allocate(kernel_map
, &vaddr
,
767 (NBPDE
* NCOPY_WINDOWS
* num_cpus
) + NBPDE
,
768 VM_FLAGS_ANYWHERE
) != KERN_SUCCESS
)
769 panic("cpu_userwindow_init: "
770 "couldn't allocate user map window");
773 * window must start on a page table boundary
774 * in the virtual address space
776 user_window_base
= (vaddr
+ (NBPDE
- 1)) & ~(NBPDE
- 1);
779 * get rid of any allocation leading up to our
782 vm_deallocate(kernel_map
, vaddr
, user_window_base
- vaddr
);
785 * get rid of tail that we don't need
787 user_window
= user_window_base
+
788 (NBPDE
* NCOPY_WINDOWS
* num_cpus
);
790 vm_deallocate(kernel_map
, user_window
,
792 ((NBPDE
* NCOPY_WINDOWS
* num_cpus
) + NBPDE
)) -
796 user_window
= user_window_base
+ (cpu
* NCOPY_WINDOWS
* NBPDE
);
798 cdp
->cpu_copywindow_base
= user_window
;
800 * Abuse this pdp entry, the pdp now actually points to
801 * an array of copy windows addresses.
803 cdp
->cpu_copywindow_pdp
= pmap_pde(kernel_pmap
, user_window
);
808 cpu_physwindow_init(int cpu
)
810 cpu_data_t
*cdp
= cpu_data_ptr
[cpu
];
811 vm_offset_t phys_window
= cdp
->cpu_physwindow_base
;
813 if (phys_window
== 0) {
814 if (vm_allocate(kernel_map
, &phys_window
,
815 PAGE_SIZE
, VM_FLAGS_ANYWHERE
)
817 panic("cpu_physwindow_init: "
818 "couldn't allocate phys map window");
821 * make sure the page that encompasses the
822 * pte pointer we're interested in actually
823 * exists in the page table
825 pmap_expand(kernel_pmap
, phys_window
, PMAP_EXPAND_OPTIONS_NONE
);
827 cdp
->cpu_physwindow_base
= phys_window
;
828 cdp
->cpu_physwindow_ptep
= vtopte(phys_window
);
831 #endif /* NCOPY_WINDOWS > 0 */
834 * Load the segment descriptor tables for the current processor.
837 cpu_mode_init(cpu_data_t
*cdp
)
839 fast_syscall_init64(cdp
);
843 * Allocate a new interrupt stack for the boot processor from the
844 * heap rather than continue to use the statically allocated space.
845 * Also switch to a dynamically allocated cpu data area.
848 cpu_data_realloc(void)
856 ret
= kmem_alloc(kernel_map
, &istk
, INTSTACK_SIZE
);
857 if (ret
!= KERN_SUCCESS
) {
858 panic("cpu_data_realloc() stack alloc, ret=%d\n", ret
);
860 bzero((void*) istk
, INTSTACK_SIZE
);
861 istk
+= INTSTACK_SIZE
;
863 ret
= kmem_alloc(kernel_map
, (vm_offset_t
*) &cdp
, sizeof(cpu_data_t
));
864 if (ret
!= KERN_SUCCESS
) {
865 panic("cpu_data_realloc() cpu data alloc, ret=%d\n", ret
);
868 /* Copy old contents into new area and make fix-ups */
869 assert(cpu_number() == 0);
870 bcopy((void *) cpu_data_ptr
[0], (void*) cdp
, sizeof(cpu_data_t
));
872 cdp
->cpu_int_stack_top
= istk
;
873 timer_call_queue_init(&cdp
->rtclock_timer
.queue
);
875 /* Allocate the separate fault stack */
876 ret
= kmem_alloc(kernel_map
, &fstk
, PAGE_SIZE
);
877 if (ret
!= KERN_SUCCESS
) {
878 panic("cpu_data_realloc() fault stack alloc, ret=%d\n", ret
);
880 bzero((void*) fstk
, PAGE_SIZE
);
884 * With interrupts disabled commmit the new areas.
886 istate
= ml_set_interrupts_enabled(FALSE
);
887 cpu_data_ptr
[0] = cdp
;
888 master_ktss64
.ist2
= (uintptr_t) fstk
;
889 master_ktss64
.ist1
= (uintptr_t) fstk
890 - sizeof(x86_64_intr_stack_frame_t
);
891 wrmsr64(MSR_IA32_GS_BASE
, (uintptr_t) cdp
);
892 wrmsr64(MSR_IA32_KERNEL_GS_BASE
, (uintptr_t) cdp
);
893 (void) ml_set_interrupts_enabled(istate
);
895 kprintf("Reallocated master cpu data: %p,"
896 " interrupt stack: %p, fault stack: %p\n",
897 (void *) cdp
, (void *) istk
, (void *) fstk
);