2 * Copyright (c) 2000-2009 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>
76 #include <i386/machine_check.h>
79 #include <kern/misc_protos.h>
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)
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 ;
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
109 #define TRAP(n, name) \
118 #define TRAP_ERR TRAP
119 #define TRAP_SPC TRAP
121 #define TRAP_IST(n, name) \
130 #define INTERRUPT(n) \
132 (uintptr_t)&_intr_ ## n,\
139 #define USER_TRAP(n, name) \
148 #define USER_TRAP_SPC USER_TRAP
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"
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.
164 * First cpu`s interrupt stack.
166 extern uint32_t low_intstack
[]; /* bottom */
167 extern uint32_t low_eintstack
[]; /* top */
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.
174 cpu_data_t cpu_data_master
= {
175 .cpu_this
= &cpu_data_master
,
176 .cpu_nanotime
= &rtc_nanotime_info
,
177 .cpu_int_stack_top
= (vm_offset_t
) low_eintstack
,
179 .cpu_is64bit
= FALSE
,
184 cpu_data_t
*cpu_data_ptr
[MAX_CPUS
] = { [0] &cpu_data_master
};
186 decl_simple_lock_data(,ncpus_lock
); /* protects real_ncpus */
187 unsigned int real_ncpus
= 1;
188 unsigned int max_ncpus
= MAX_CPUS
;
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)
195 extern void hi_sysenter(void);
200 } __attribute__((__packed__
)) table_descriptor64_t
;
202 extern table_descriptor64_t gdtptr64
;
203 extern table_descriptor64_t idtptr64
;
205 extern void hi64_sysenter(void);
206 extern void hi64_syscall(void);
208 #if defined(__x86_64__) && !defined(UBER64)
209 #define UBER64(x) ((uintptr_t)x)
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.
222 * Allocate and initialize the per-processor descriptor tables.
225 struct fake_descriptor ldt_desc_pattern
= {
227 LDTSZ_MIN
* sizeof(struct fake_descriptor
) - 1,
229 ACC_P
|ACC_PL_K
|ACC_LDT
232 struct fake_descriptor tss_desc_pattern
= {
234 sizeof(struct i386_tss
) - 1,
236 ACC_P
|ACC_PL_K
|ACC_TSS
239 struct fake_descriptor cpudata_desc_pattern
= {
241 sizeof(cpu_data_t
)-1,
243 ACC_P
|ACC_PL_K
|ACC_DATA_W
246 struct fake_descriptor userwindow_desc_pattern
= {
248 ((NBPDE
* NCOPY_WINDOWS
) / PAGE_SIZE
) - 1,
250 ACC_P
|ACC_PL_U
|ACC_DATA_W
253 struct fake_descriptor physwindow_desc_pattern
= {
257 ACC_P
|ACC_PL_K
|ACC_DATA_W
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.
266 struct fake_descriptor64 kernel_ldt_desc64
= {
268 LDTSZ_MIN
*sizeof(struct fake_descriptor
)-1,
270 ACC_P
|ACC_PL_K
|ACC_LDT
,
275 * This is the expanded, 64-bit variant of the kernel TSS descriptor.
276 * It is follows pattern of the KERNEL_LDT.
278 struct fake_descriptor64 kernel_tss_desc64
= {
280 sizeof(struct x86_64_tss
)-1,
286 ACC_P
|ACC_PL_K
|ACC_TSS
,
291 * Convert a descriptor from fake to real format.
293 * Fake descriptor format:
294 * bytes 0..3 base 31..0
295 * bytes 4..5 limit 15..0
296 * byte 6 access byte 2 | limit 19..16
297 * byte 7 access byte 1
299 * Real descriptor format:
300 * bytes 0..1 limit 15..0
301 * bytes 2..3 base 15..0
303 * byte 5 access byte 1
304 * byte 6 access byte 2 | limit 19..16
309 * bytes 4..5 selector
310 * byte 6 word count << 4 (to match fake descriptor)
311 * byte 7 access byte 1
314 * bytes 0..1 offset 15..0
315 * bytes 2..3 selector
317 * byte 5 access byte 1
318 * bytes 6..7 offset 31..16
321 fix_desc(void *d
, int num_desc
) {
322 //early_kprintf("fix_desc(%x, %x)\n", d, num_desc);
323 uint8_t *desc
= (uint8_t*) d
;
326 if ((desc
[7] & 0x14) == 0x04) { /* gate */
332 offset
= *((uint32_t*)(desc
));
333 selector
= *((uint32_t*)(desc
+4));
334 wordcount
= desc
[6] >> 4;
337 *((uint16_t*)desc
) = offset
& 0xFFFF;
338 *((uint16_t*)(desc
+2)) = selector
;
341 *((uint16_t*)(desc
+6)) = offset
>> 16;
343 } else { /* descriptor */
348 base
= *((uint32_t*)(desc
));
349 limit
= *((uint16_t*)(desc
+4));
353 *((uint16_t*)(desc
)) = limit
;
354 *((uint16_t*)(desc
+2)) = base
& 0xFFFF;
355 desc
[4] = (base
>> 16) & 0xFF;
358 desc
[7] = base
>> 24;
361 } while (--num_desc
);
365 fix_desc64(void *descp
, int count
)
367 struct fake_descriptor64
*fakep
;
369 struct real_gate64 gate
;
370 struct real_descriptor64 desc
;
374 fakep
= (struct fake_descriptor64
*) descp
;
376 for (i
= 0; i
< count
; i
++, fakep
++) {
378 * Construct the real decriptor locally.
381 bzero((void *) &real
, sizeof(real
));
383 switch (fakep
->access
& ACC_TYPE
) {
389 real
.gate
.offset_low16
= fakep
->offset64
& 0xFFFF;
390 real
.gate
.selector16
= fakep
->lim_or_seg
& 0xFFFF;
391 real
.gate
.IST
= fakep
->size_or_IST
& 0x7;
392 real
.gate
.access8
= fakep
->access
;
393 real
.gate
.offset_high16
= (fakep
->offset64
>>16)&0xFFFF;
394 real
.gate
.offset_top32
= (uint32_t)(fakep
->offset64
>>32);
396 default: /* Otherwise */
397 real
.desc
.limit_low16
= fakep
->lim_or_seg
& 0xFFFF;
398 real
.desc
.base_low16
= fakep
->offset64
& 0xFFFF;
399 real
.desc
.base_med8
= (fakep
->offset64
>> 16) & 0xFF;
400 real
.desc
.access8
= fakep
->access
;
401 real
.desc
.limit_high4
= (fakep
->lim_or_seg
>> 16) & 0xFF;
402 real
.desc
.granularity4
= fakep
->size_or_IST
;
403 real
.desc
.base_high8
= (fakep
->offset64
>> 24) & 0xFF;
404 real
.desc
.base_top32
= (uint32_t)(fakep
->offset64
>>32);
408 * Now copy back over the fake structure.
410 bcopy((void *) &real
, (void *) fakep
, sizeof(real
));
416 cpu_desc_init(cpu_data_t
*cdp
)
418 cpu_desc_index_t
*cdi
= &cdp
->cpu_desc_index
;
420 if (cdp
== &cpu_data_master
) {
422 * Fix up the entries in the GDT to point to
423 * this LDT and this TSS.
425 struct fake_descriptor temp_fake_desc
;
426 temp_fake_desc
= ldt_desc_pattern
;
427 temp_fake_desc
.offset
= (vm_offset_t
) &master_ldt
;
428 fix_desc(&temp_fake_desc
, 1);
429 *(struct fake_descriptor
*) &master_gdt
[sel_idx(KERNEL_LDT
)] =
431 *(struct fake_descriptor
*) &master_gdt
[sel_idx(USER_LDT
)] =
434 temp_fake_desc
= tss_desc_pattern
;
435 temp_fake_desc
.offset
= (vm_offset_t
) &master_ktss
;
436 fix_desc(&temp_fake_desc
, 1);
437 *(struct fake_descriptor
*) &master_gdt
[sel_idx(KERNEL_TSS
)] =
441 temp_fake_desc
= tss_desc_pattern
;
442 temp_fake_desc
.offset
= (vm_offset_t
) &master_dbtss
;
443 fix_desc(&temp_fake_desc
, 1);
444 *(struct fake_descriptor
*) &master_gdt
[sel_idx(DEBUG_TSS
)] =
448 temp_fake_desc
= cpudata_desc_pattern
;
449 temp_fake_desc
.offset
= (vm_offset_t
) &cpu_data_master
;
450 fix_desc(&temp_fake_desc
, 1);
451 *(struct fake_descriptor
*) &master_gdt
[sel_idx(CPU_DATA_GS
)] =
454 fix_desc((void *)&master_idt
, IDTSZ
);
456 cdi
->cdi_idt
.ptr
= master_idt
;
457 cdi
->cdi_gdt
.ptr
= (void *)master_gdt
;
461 * Master CPU uses the tables built at boot time.
462 * Just set the index pointers to the high shared-mapping space.
463 * Note that the sysenter stack uses empty space above the ktss
464 * in the HIGH_FIXED_KTSS page. In this case we don't map the
465 * the real master_sstk in low memory.
467 cdi
->cdi_ktss
= (struct i386_tss
*)
468 pmap_index_to_virt(HIGH_FIXED_KTSS
) ;
469 cdi
->cdi_sstk
= (vm_offset_t
) (cdi
->cdi_ktss
+ 1) +
470 (vm_offset_t
) &master_sstk
.top
-
471 (vm_offset_t
) &master_sstk
;
473 cpu_desc_table_t
*cdt
= (cpu_desc_table_t
*) cdp
->cpu_desc_tablep
;
475 vm_offset_t cpu_hi_desc
;
477 cpu_hi_desc
= pmap_cpu_high_shared_remap(
480 (vm_offset_t
) cdt
, 1);
483 * Per-cpu GDT, IDT, LDT, KTSS descriptors are allocated in one
484 * block (cpu_desc_table) and double-mapped into high shared space
485 * in one page window.
486 * Also, a transient stack for the fast sysenter path. The top of
487 * which is set at context switch time to point to the PCB using
490 cdi
->cdi_gdt
.ptr
= (struct fake_descriptor
*) (cpu_hi_desc
+
491 offsetof(cpu_desc_table_t
, gdt
[0]));
492 cdi
->cdi_idt
.ptr
= (struct fake_descriptor
*) (cpu_hi_desc
+
493 offsetof(cpu_desc_table_t
, idt
[0]));
494 cdi
->cdi_ktss
= (struct i386_tss
*) (cpu_hi_desc
+
495 offsetof(cpu_desc_table_t
, ktss
));
496 cdi
->cdi_sstk
= cpu_hi_desc
+ offsetof(cpu_desc_table_t
, sstk
.top
);
499 * LDT descriptors are mapped into a seperate area.
501 cdi
->cdi_ldt
= (struct fake_descriptor
*)
502 pmap_cpu_high_shared_remap(
505 (vm_offset_t
) cdp
->cpu_ldtp
,
506 HIGH_CPU_LDT_END
- HIGH_CPU_LDT_BEGIN
+ 1);
511 bcopy((char *)master_idt
, (char *)cdt
->idt
, sizeof(master_idt
));
512 bcopy((char *)master_gdt
, (char *)cdt
->gdt
, sizeof(master_gdt
));
513 bcopy((char *)master_ldt
, (char *)cdp
->cpu_ldtp
, sizeof(master_ldt
));
514 bzero((char *)&cdt
->ktss
, sizeof(struct i386_tss
));
516 cdi
->cdi_dbtss
= (struct i386_tss
*) (cpu_hi_desc
+
517 offsetof(cpu_desc_table_t
, dbtss
));
518 bcopy((char *)&master_dbtss
,
520 sizeof(struct i386_tss
));
521 #endif /* MACH_KDB */
524 * Fix up the entries in the GDT to point to
525 * this LDT and this TSS.
527 struct fake_descriptor temp_ldt
= ldt_desc_pattern
;
528 temp_ldt
.offset
= (vm_offset_t
)cdi
->cdi_ldt
;
529 fix_desc(&temp_ldt
, 1);
531 cdt
->gdt
[sel_idx(KERNEL_LDT
)] = temp_ldt
;
532 cdt
->gdt
[sel_idx(USER_LDT
)] = temp_ldt
;
534 cdt
->gdt
[sel_idx(KERNEL_TSS
)] = tss_desc_pattern
;
535 cdt
->gdt
[sel_idx(KERNEL_TSS
)].offset
= (vm_offset_t
) cdi
->cdi_ktss
;
536 fix_desc(&cdt
->gdt
[sel_idx(KERNEL_TSS
)], 1);
538 cdt
->gdt
[sel_idx(CPU_DATA_GS
)] = cpudata_desc_pattern
;
539 cdt
->gdt
[sel_idx(CPU_DATA_GS
)].offset
= (vm_offset_t
) cdp
;
540 fix_desc(&cdt
->gdt
[sel_idx(CPU_DATA_GS
)], 1);
543 cdt
->gdt
[sel_idx(DEBUG_TSS
)] = tss_desc_pattern
;
544 cdt
->gdt
[sel_idx(DEBUG_TSS
)].offset
= (vm_offset_t
) cdi
->cdi_dbtss
;
545 fix_desc(&cdt
->gdt
[sel_idx(DEBUG_TSS
)], 1);
547 cdt
->dbtss
.esp0
= (int)(db_task_stack_store
+
548 (INTSTACK_SIZE
* (cdp
->cpu_number
)) - sizeof (natural_t
));
549 cdt
->dbtss
.esp
= cdt
->dbtss
.esp0
;
550 cdt
->dbtss
.eip
= (int)&db_task_start
;
551 #endif /* MACH_KDB */
553 cdt
->ktss
.ss0
= KERNEL_DS
;
554 cdt
->ktss
.io_bit_map_offset
= 0x0FFF; /* no IO bitmap */
556 cpu_userwindow_init(cdp
->cpu_number
);
557 cpu_physwindow_init(cdp
->cpu_number
);
561 #endif /* __i386__ */
564 cpu_desc_init64(cpu_data_t
*cdp
)
566 cpu_desc_index_t
*cdi
= &cdp
->cpu_desc_index
;
568 if (cdp
== &cpu_data_master
) {
570 * Master CPU uses the tables built at boot time.
571 * Just set the index pointers to the low memory space.
572 * Note that in 64-bit mode these are addressed in the
573 * double-mapped window (uber-space).
575 cdi
->cdi_ktss
= (void *)&master_ktss64
;
576 cdi
->cdi_sstk
= (vm_offset_t
) &master_sstk
.top
;
577 cdi
->cdi_gdt
.ptr
= (void *)master_gdt
;
578 cdi
->cdi_idt
.ptr
= (void *)master_idt64
;
579 cdi
->cdi_ldt
= (struct fake_descriptor
*) master_ldt
;
582 /* Replace the expanded LDTs and TSS slots in the GDT */
583 kernel_ldt_desc64
.offset64
= UBER64(&master_ldt
);
584 *(struct fake_descriptor64
*) &master_gdt
[sel_idx(KERNEL_LDT
)] =
586 *(struct fake_descriptor64
*) &master_gdt
[sel_idx(USER_LDT
)] =
588 kernel_tss_desc64
.offset64
= UBER64(&master_ktss64
);
589 *(struct fake_descriptor64
*) &master_gdt
[sel_idx(KERNEL_TSS
)] =
592 /* Fix up the expanded descriptors for 64-bit. */
593 fix_desc64((void *) &master_idt64
, IDTSZ
);
594 fix_desc64((void *) &master_gdt
[sel_idx(KERNEL_LDT
)], 1);
595 fix_desc64((void *) &master_gdt
[sel_idx(USER_LDT
)], 1);
596 fix_desc64((void *) &master_gdt
[sel_idx(KERNEL_TSS
)], 1);
599 * Set the double-fault stack as IST1 in the 64-bit TSS
601 master_ktss64
.ist1
= UBER64((uintptr_t) df_task_stack_end
);
604 cpu_desc_table64_t
*cdt
= (cpu_desc_table64_t
*) cdp
->cpu_desc_tablep
;
606 * Per-cpu GDT, IDT, KTSS descriptors are allocated in kernel
607 * heap (cpu_desc_table) .
608 * On K32 they're double-mapped in uber-space (over 4GB).
609 * LDT descriptors are mapped into a separate area.
611 cdi
->cdi_gdt
.ptr
= (struct fake_descriptor
*)cdt
->gdt
;
612 cdi
->cdi_idt
.ptr
= (void *)cdt
->idt
;
613 cdi
->cdi_ktss
= (void *)&cdt
->ktss
;
614 cdi
->cdi_sstk
= (vm_offset_t
)&cdt
->sstk
.top
;
615 cdi
->cdi_ldt
= cdp
->cpu_ldtp
;
620 bcopy((char *)master_idt64
, (char *)cdt
->idt
, sizeof(master_idt64
));
621 bcopy((char *)master_gdt
, (char *)cdt
->gdt
, sizeof(master_gdt
));
622 bcopy((char *)master_ldt
, (char *)cdp
->cpu_ldtp
, sizeof(master_ldt
));
623 bcopy((char *)&master_ktss64
, (char *)&cdt
->ktss
, sizeof(struct x86_64_tss
));
626 * Fix up the entries in the GDT to point to
627 * this LDT and this TSS.
629 kernel_ldt_desc64
.offset64
= UBER64(cdi
->cdi_ldt
);
630 *(struct fake_descriptor64
*) &cdt
->gdt
[sel_idx(KERNEL_LDT
)] =
632 fix_desc64(&cdt
->gdt
[sel_idx(KERNEL_LDT
)], 1);
634 kernel_ldt_desc64
.offset64
= UBER64(cdi
->cdi_ldt
);
635 *(struct fake_descriptor64
*) &cdt
->gdt
[sel_idx(USER_LDT
)] =
637 fix_desc64(&cdt
->gdt
[sel_idx(USER_LDT
)], 1);
639 kernel_tss_desc64
.offset64
= UBER64(cdi
->cdi_ktss
);
640 *(struct fake_descriptor64
*) &cdt
->gdt
[sel_idx(KERNEL_TSS
)] =
642 fix_desc64(&cdt
->gdt
[sel_idx(KERNEL_TSS
)], 1);
644 /* Set double-fault stack as IST1 */
645 cdt
->ktss
.ist1
= UBER64((unsigned long)cdt
->dfstk
+ sizeof(cdt
->dfstk
));
647 cdt
->gdt
[sel_idx(CPU_DATA_GS
)] = cpudata_desc_pattern
;
648 cdt
->gdt
[sel_idx(CPU_DATA_GS
)].offset
= (vm_offset_t
) cdp
;
649 fix_desc(&cdt
->gdt
[sel_idx(CPU_DATA_GS
)], 1);
651 /* Allocate copyio windows */
652 cpu_userwindow_init(cdp
->cpu_number
);
653 cpu_physwindow_init(cdp
->cpu_number
);
657 /* Require that the top of the sysenter stack is 16-byte aligned */
658 if ((cdi
->cdi_sstk
% 16) != 0)
659 panic("cpu_desc_init64() sysenter stack not 16-byte aligned");
664 cpu_desc_load(cpu_data_t
*cdp
)
666 cpu_desc_index_t
*cdi
= &cdp
->cpu_desc_index
;
668 cdi
->cdi_idt
.size
= 0x1000 + cdp
->cpu_number
;
669 cdi
->cdi_gdt
.size
= sizeof(struct real_descriptor
)*GDTSZ
- 1;
671 lgdt((unsigned long *) &cdi
->cdi_gdt
);
672 lidt((unsigned long *) &cdi
->cdi_idt
);
677 __asm__
volatile("mov %0, %%gs" : : "rm" ((unsigned short)(CPU_DATA_GS
)));
679 #endif /* __i386__ */
682 cpu_desc_load64(cpu_data_t
*cdp
)
684 cpu_desc_index_t
*cdi
= &cdp
->cpu_desc_index
;
688 * Load up the new descriptors etc
689 * ml_load_desc64() expects these global pseudo-descriptors:
690 * gdtptr64 -> per-cpu gdt
691 * idtptr64 -> per-cpu idt
692 * These are 10-byte descriptors with 64-bit addresses into
695 * Refer to commpage/cpu_number.s for the IDT limit trick.
697 gdtptr64
.length
= GDTSZ
* sizeof(struct real_descriptor64
) - 1;
698 gdtptr64
.offset
[0] = (uint32_t) cdi
->cdi_gdt
.ptr
;
699 gdtptr64
.offset
[1] = KERNEL_UBER_BASE_HI32
;
700 idtptr64
.length
= 0x1000 + cdp
->cpu_number
;
701 idtptr64
.offset
[0] = (uint32_t) cdi
->cdi_idt
.ptr
;
702 idtptr64
.offset
[1] = KERNEL_UBER_BASE_HI32
;
704 /* Make sure busy bit is cleared in the TSS */
705 gdt_desc_p(KERNEL_TSS
)->access
&= ~ACC_TSS_BUSY
;
709 /* Load the GDT, LDT, IDT and TSS */
710 cdi
->cdi_gdt
.size
= sizeof(struct real_descriptor64
)*GDTSZ
- 1;
711 cdi
->cdi_idt
.size
= 0x1000 + cdp
->cpu_number
;
712 lgdt((unsigned long *) &cdi
->cdi_gdt
);
713 lidt((unsigned long *) &cdi
->cdi_idt
);
717 /* Stuff the pre-cpu data area into the MSR and swapgs to activate */
718 wrmsr64(MSR_IA32_KERNEL_GS_BASE
, (unsigned long)cdp
);
719 #if GPROF // Hack to enable mcount to work on K64
720 __asm__
volatile("mov %0, %%gs" : : "rm" ((unsigned short)(KERNEL_DS
)));
730 * Set MSRs for sysenter/sysexit for 32-bit.
733 fast_syscall_init(__unused cpu_data_t
*cdp
)
735 wrmsr(MSR_IA32_SYSENTER_CS
, SYSENTER_CS
, 0);
736 wrmsr(MSR_IA32_SYSENTER_EIP
, HI_TEXT(hi_sysenter
), 0);
737 wrmsr(MSR_IA32_SYSENTER_ESP
, current_sstk(), 0);
742 * Set MSRs for sysenter/sysexit and syscall/sysret for 64-bit.
745 fast_syscall_init64(__unused cpu_data_t
*cdp
)
747 wrmsr64(MSR_IA32_SYSENTER_CS
, SYSENTER_CS
);
748 wrmsr64(MSR_IA32_SYSENTER_EIP
, UBER64((uintptr_t) hi64_sysenter
));
749 wrmsr64(MSR_IA32_SYSENTER_ESP
, UBER64(current_sstk()));
750 /* Enable syscall/sysret */
751 wrmsr64(MSR_IA32_EFER
, rdmsr64(MSR_IA32_EFER
) | MSR_IA32_EFER_SCE
);
754 * MSRs for 64-bit syscall/sysret
755 * Note USER_CS because sysret uses this + 16 when returning to
758 wrmsr64(MSR_IA32_LSTAR
, UBER64((uintptr_t) hi64_syscall
));
759 wrmsr64(MSR_IA32_STAR
, (((uint64_t)USER_CS
) << 48) |
760 (((uint64_t)KERNEL64_CS
) << 32));
762 * Emulate eflags cleared by sysenter but note that
763 * we also clear the trace trap to avoid the complications
764 * of single-stepping into a syscall. The nested task bit
765 * is also cleared to avoid a spurious "task switch"
766 * should we choose to return via an IRET.
768 wrmsr64(MSR_IA32_FMASK
, EFL_DF
|EFL_IF
|EFL_TF
|EFL_NT
);
772 * Set the Kernel GS base MSR to point to per-cpu data in uber-space.
773 * The uber-space handler (hi64_syscall) uses the swapgs instruction.
775 wrmsr64(MSR_IA32_KERNEL_GS_BASE
, UBER64(cdp
));
777 #if ONLY_SAFE_FOR_LINDA_SERIAL
778 kprintf("fast_syscall_init64() KERNEL_GS_BASE=0x%016llx\n",
779 rdmsr64(MSR_IA32_KERNEL_GS_BASE
));
785 cpu_data_alloc(boolean_t is_boot_cpu
)
791 assert(real_ncpus
== 1);
792 cdp
= &cpu_data_master
;
793 if (cdp
->cpu_processor
== NULL
) {
794 simple_lock_init(&ncpus_lock
, 0);
795 cdp
->cpu_processor
= cpu_processor_alloc(TRUE
);
796 #if NCOPY_WINDOWS > 0
797 cdp
->cpu_pmap
= pmap_cpu_alloc(TRUE
);
799 queue_init(&cdp
->rtclock_timer
.queue
);
800 cdp
->rtclock_timer
.deadline
= EndOfAllTime
;
806 * Allocate per-cpu data:
808 ret
= kmem_alloc(kernel_map
, (vm_offset_t
*) &cdp
, sizeof(cpu_data_t
));
809 if (ret
!= KERN_SUCCESS
) {
810 printf("cpu_data_alloc() failed, ret=%d\n", ret
);
813 bzero((void*) cdp
, sizeof(cpu_data_t
));
817 cdp
->cpu_is64bit
= cpu_mode_is64bit();
820 * Allocate interrupt stack:
822 ret
= kmem_alloc(kernel_map
,
823 (vm_offset_t
*) &cdp
->cpu_int_stack_top
,
825 if (ret
!= KERN_SUCCESS
) {
826 printf("cpu_data_alloc() int stack failed, ret=%d\n", ret
);
829 bzero((void*) cdp
->cpu_int_stack_top
, INTSTACK_SIZE
);
830 cdp
->cpu_int_stack_top
+= INTSTACK_SIZE
;
834 * Allocate descriptor table:
835 * Size depends on cpu mode.
837 ret
= kmem_alloc(kernel_map
,
838 (vm_offset_t
*) &cdp
->cpu_desc_tablep
,
839 cdp
->cpu_is64bit
? sizeof(cpu_desc_table64_t
)
840 : sizeof(cpu_desc_table_t
));
841 if (ret
!= KERN_SUCCESS
) {
842 printf("cpu_data_alloc() desc_table failed, ret=%d\n", ret
);
849 ret
= kmem_alloc(kernel_map
,
850 (vm_offset_t
*) &cdp
->cpu_ldtp
,
851 sizeof(struct real_descriptor
) * LDTSZ
);
852 if (ret
!= KERN_SUCCESS
) {
853 printf("cpu_data_alloc() ldt failed, ret=%d\n", ret
);
858 /* Machine-check shadow register allocation. */
862 simple_lock(&ncpus_lock
);
864 cpu_data_ptr
[real_ncpus
] = cdp
;
865 cdp
->cpu_number
= real_ncpus
;
867 simple_unlock(&ncpus_lock
);
869 cdp
->cpu_nanotime
= &rtc_nanotime_info
;
870 queue_init(&cdp
->rtclock_timer
.queue
);
871 cdp
->rtclock_timer
.deadline
= EndOfAllTime
;
873 kprintf("cpu_data_alloc(%d) %p desc_table: %p "
875 "int_stack: 0x%lx-0x%lx\n",
876 cdp
->cpu_number
, cdp
, cdp
->cpu_desc_tablep
, cdp
->cpu_ldtp
,
877 (long)(cdp
->cpu_int_stack_top
- INTSTACK_SIZE
), (long)(cdp
->cpu_int_stack_top
));
883 if (cdp
->cpu_desc_tablep
)
884 kfree((void *) cdp
->cpu_desc_tablep
,
885 sizeof(*cdp
->cpu_desc_tablep
));
886 if (cdp
->cpu_int_stack_top
)
887 kfree((void *) (cdp
->cpu_int_stack_top
- INTSTACK_SIZE
),
889 kfree((void *) cdp
, sizeof(*cdp
));
895 valid_user_segment_selectors(uint16_t cs
,
902 return valid_user_code_selector(cs
) &&
903 valid_user_stack_selector(ss
) &&
904 valid_user_data_selector(ds
) &&
905 valid_user_data_selector(es
) &&
906 valid_user_data_selector(fs
) &&
907 valid_user_data_selector(gs
);
911 #if NCOPY_WINDOWS > 0
913 static vm_offset_t user_window_base
= 0;
916 cpu_userwindow_init(int cpu
)
918 cpu_data_t
*cdp
= cpu_data_ptr
[cpu
];
919 vm_offset_t user_window
;
923 num_cpus
= ml_get_max_cpus();
926 panic("cpu_userwindow_init: cpu > num_cpus");
928 if (user_window_base
== 0) {
930 if (vm_allocate(kernel_map
, &vaddr
,
931 (NBPDE
* NCOPY_WINDOWS
* num_cpus
) + NBPDE
,
932 VM_FLAGS_ANYWHERE
) != KERN_SUCCESS
)
933 panic("cpu_userwindow_init: "
934 "couldn't allocate user map window");
937 * window must start on a page table boundary
938 * in the virtual address space
940 user_window_base
= (vaddr
+ (NBPDE
- 1)) & ~(NBPDE
- 1);
943 * get rid of any allocation leading up to our
946 vm_deallocate(kernel_map
, vaddr
, user_window_base
- vaddr
);
949 * get rid of tail that we don't need
951 user_window
= user_window_base
+
952 (NBPDE
* NCOPY_WINDOWS
* num_cpus
);
954 vm_deallocate(kernel_map
, user_window
,
956 ((NBPDE
* NCOPY_WINDOWS
* num_cpus
) + NBPDE
)) -
960 user_window
= user_window_base
+ (cpu
* NCOPY_WINDOWS
* NBPDE
);
962 cdp
->cpu_copywindow_base
= user_window
;
963 cdp
->cpu_copywindow_pdp
= pmap_pde(kernel_pmap
, user_window
);
966 cpu_desc_index_t
*cdi
= &cdp
->cpu_desc_index
;
967 cdi
->cdi_gdt
.ptr
[sel_idx(USER_WINDOW_SEL
)] = userwindow_desc_pattern
;
968 cdi
->cdi_gdt
.ptr
[sel_idx(USER_WINDOW_SEL
)].offset
= user_window
;
970 fix_desc(&cdi
->cdi_gdt
.ptr
[sel_idx(USER_WINDOW_SEL
)], 1);
971 #endif /* __i386__ */
975 cpu_physwindow_init(int cpu
)
977 cpu_data_t
*cdp
= cpu_data_ptr
[cpu
];
978 vm_offset_t phys_window
= cdp
->cpu_physwindow_base
;
980 if (phys_window
== 0) {
981 if (vm_allocate(kernel_map
, &phys_window
,
982 PAGE_SIZE
, VM_FLAGS_ANYWHERE
)
984 panic("cpu_physwindow_init: "
985 "couldn't allocate phys map window");
988 * make sure the page that encompasses the
989 * pte pointer we're interested in actually
990 * exists in the page table
992 pmap_expand(kernel_pmap
, phys_window
);
994 cdp
->cpu_physwindow_base
= phys_window
;
995 cdp
->cpu_physwindow_ptep
= vtopte(phys_window
);
998 cpu_desc_index_t
*cdi
= &cdp
->cpu_desc_index
;
999 cdi
->cdi_gdt
.ptr
[sel_idx(PHYS_WINDOW_SEL
)] = physwindow_desc_pattern
;
1000 cdi
->cdi_gdt
.ptr
[sel_idx(PHYS_WINDOW_SEL
)].offset
= phys_window
;
1002 fix_desc(&cdi
->cdi_gdt
.ptr
[sel_idx(PHYS_WINDOW_SEL
)], 1);
1003 #endif /* __i386__ */
1005 #endif /* NCOPY_WINDOWS > 0 */
1008 * Load the segment descriptor tables for the current processor.
1011 cpu_mode_init(cpu_data_t
*cdp
)
1014 if (cpu_mode_is64bit()) {
1015 cpu_IA32e_enable(cdp
);
1016 cpu_desc_load64(cdp
);
1017 fast_syscall_init64(cdp
);
1019 fast_syscall_init(cdp
);
1022 fast_syscall_init64(cdp
);
1025 /* Call for per-cpu pmap mode initialization */