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 <kern/etimer.h>
66 #include <mach/vm_map.h>
67 #include <mach/machine/vm_param.h>
68 #include <vm/vm_kern.h>
69 #include <vm/vm_map.h>
71 #include <i386/lock.h>
72 #include <i386/mp_desc.h>
73 #include <i386/misc_protos.h>
75 #include <i386/pmap.h>
77 #include <i386/machine_check.h>
80 #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
= &pal_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,
282 ACC_P
|ACC_PL_K
|ACC_TSS
,
287 * Convert a descriptor from fake to real format.
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
295 * Real descriptor format:
296 * bytes 0..1 limit 15..0
297 * bytes 2..3 base 15..0
299 * byte 5 access byte 1
300 * byte 6 access byte 2 | limit 19..16
305 * bytes 4..5 selector
306 * byte 6 word count << 4 (to match fake descriptor)
307 * byte 7 access byte 1
310 * bytes 0..1 offset 15..0
311 * bytes 2..3 selector
313 * byte 5 access byte 1
314 * bytes 6..7 offset 31..16
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
;
322 if ((desc
[7] & 0x14) == 0x04) { /* gate */
328 offset
= *((uint32_t*)(desc
));
329 selector
= *((uint32_t*)(desc
+4));
330 wordcount
= desc
[6] >> 4;
333 *((uint16_t*)desc
) = offset
& 0xFFFF;
334 *((uint16_t*)(desc
+2)) = selector
;
337 *((uint16_t*)(desc
+6)) = offset
>> 16;
339 } else { /* descriptor */
344 base
= *((uint32_t*)(desc
));
345 limit
= *((uint16_t*)(desc
+4));
349 *((uint16_t*)(desc
)) = limit
;
350 *((uint16_t*)(desc
+2)) = base
& 0xFFFF;
351 desc
[4] = (base
>> 16) & 0xFF;
354 desc
[7] = base
>> 24;
357 } while (--num_desc
);
361 fix_desc64(void *descp
, int count
)
363 struct fake_descriptor64
*fakep
;
365 struct real_gate64 gate
;
366 struct real_descriptor64 desc
;
370 fakep
= (struct fake_descriptor64
*) descp
;
372 for (i
= 0; i
< count
; i
++, fakep
++) {
374 * Construct the real decriptor locally.
377 bzero((void *) &real
, sizeof(real
));
379 switch (fakep
->access
& ACC_TYPE
) {
385 real
.gate
.offset_low16
= (uint16_t)(fakep
->offset64
& 0xFFFF);
386 real
.gate
.selector16
= fakep
->lim_or_seg
& 0xFFFF;
387 real
.gate
.IST
= fakep
->size_or_IST
& 0x7;
388 real
.gate
.access8
= fakep
->access
;
389 real
.gate
.offset_high16
= (uint16_t)((fakep
->offset64
>>16) & 0xFFFF);
390 real
.gate
.offset_top32
= (uint32_t)(fakep
->offset64
>>32);
392 default: /* Otherwise */
393 real
.desc
.limit_low16
= fakep
->lim_or_seg
& 0xFFFF;
394 real
.desc
.base_low16
= (uint16_t)(fakep
->offset64
& 0xFFFF);
395 real
.desc
.base_med8
= (uint8_t)((fakep
->offset64
>> 16) & 0xFF);
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
;
399 real
.desc
.base_high8
= (uint8_t)((fakep
->offset64
>> 24) & 0xFF);
400 real
.desc
.base_top32
= (uint32_t)(fakep
->offset64
>>32);
404 * Now copy back over the fake structure.
406 bcopy((void *) &real
, (void *) fakep
, sizeof(real
));
412 cpu_desc_init(cpu_data_t
*cdp
)
414 cpu_desc_index_t
*cdi
= &cdp
->cpu_desc_index
;
416 if (cdp
== &cpu_data_master
) {
418 * Fix up the entries in the GDT to point to
419 * this LDT and this TSS.
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
)] =
427 *(struct fake_descriptor
*) &master_gdt
[sel_idx(USER_LDT
)] =
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
)] =
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
)] =
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
)] =
450 fix_desc((void *)&master_idt
, IDTSZ
);
452 cdi
->cdi_idt
.ptr
= master_idt
;
453 cdi
->cdi_gdt
.ptr
= (void *)master_gdt
;
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.
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) +
466 (vm_offset_t
) &master_sstk
.top
-
467 (vm_offset_t
) &master_sstk
;
469 cpu_desc_table_t
*cdt
= (cpu_desc_table_t
*) cdp
->cpu_desc_tablep
;
471 vm_offset_t cpu_hi_desc
;
473 cpu_hi_desc
= pmap_cpu_high_shared_remap(
476 (vm_offset_t
) cdt
, 1);
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
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
);
495 * LDT descriptors are mapped into a seperate area.
497 cdi
->cdi_ldt
= (struct fake_descriptor
*)
498 pmap_cpu_high_shared_remap(
501 (vm_offset_t
) cdp
->cpu_ldtp
,
502 HIGH_CPU_LDT_END
- HIGH_CPU_LDT_BEGIN
+ 1);
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
));
512 cdi
->cdi_dbtss
= (struct i386_tss
*) (cpu_hi_desc
+
513 offsetof(cpu_desc_table_t
, dbtss
));
514 bcopy((char *)&master_dbtss
,
516 sizeof(struct i386_tss
));
517 #endif /* MACH_KDB */
520 * Fix up the entries in the GDT to point to
521 * this LDT and this TSS.
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);
527 cdt
->gdt
[sel_idx(KERNEL_LDT
)] = temp_ldt
;
528 cdt
->gdt
[sel_idx(USER_LDT
)] = temp_ldt
;
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);
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);
538 #if MACH_KDB /* this only works for legacy 32-bit machines */
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);
543 cdt
->dbtss
.esp0
= (int)(db_task_stack_store
+
544 (INTSTACK_SIZE
* (cdp
->cpu_number
+ 1)) - sizeof (natural_t
));
545 cdt
->dbtss
.esp
= cdt
->dbtss
.esp0
;
546 cdt
->dbtss
.eip
= (int)&db_task_start
;
547 #endif /* MACH_KDB */
549 cdt
->ktss
.ss0
= KERNEL_DS
;
550 cdt
->ktss
.io_bit_map_offset
= 0x0FFF; /* no IO bitmap */
552 cpu_userwindow_init(cdp
->cpu_number
);
553 cpu_physwindow_init(cdp
->cpu_number
);
557 #endif /* __i386__ */
560 cpu_desc_init64(cpu_data_t
*cdp
)
562 cpu_desc_index_t
*cdi
= &cdp
->cpu_desc_index
;
564 if (cdp
== &cpu_data_master
) {
566 * Master CPU uses the tables built at boot time.
567 * Just set the index pointers to the low memory space.
569 cdi
->cdi_ktss
= (void *)&master_ktss64
;
570 cdi
->cdi_sstk
= (vm_offset_t
) &master_sstk
.top
;
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
;
576 /* Replace the expanded LDTs and TSS slots in the GDT */
577 kernel_ldt_desc64
.offset64
= UBER64(&master_ldt
);
578 *(struct fake_descriptor64
*) &master_gdt
[sel_idx(KERNEL_LDT
)] =
580 *(struct fake_descriptor64
*) &master_gdt
[sel_idx(USER_LDT
)] =
582 kernel_tss_desc64
.offset64
= UBER64(&master_ktss64
);
583 *(struct fake_descriptor64
*) &master_gdt
[sel_idx(KERNEL_TSS
)] =
586 /* Fix up the expanded descriptors for 64-bit. */
587 fix_desc64((void *) &master_idt64
, IDTSZ
);
588 fix_desc64((void *) &master_gdt
[sel_idx(KERNEL_LDT
)], 1);
589 fix_desc64((void *) &master_gdt
[sel_idx(USER_LDT
)], 1);
590 fix_desc64((void *) &master_gdt
[sel_idx(KERNEL_TSS
)], 1);
593 * Set the double-fault stack as IST1 in the 64-bit TSS
595 master_ktss64
.ist1
= UBER64((uintptr_t) df_task_stack_end
);
598 cpu_desc_table64_t
*cdt
= (cpu_desc_table64_t
*) cdp
->cpu_desc_tablep
;
600 * Per-cpu GDT, IDT, KTSS descriptors are allocated in kernel
601 * heap (cpu_desc_table).
602 * LDT descriptors are mapped into a separate area.
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
;
607 cdi
->cdi_sstk
= (vm_offset_t
)&cdt
->sstk
.top
;
608 cdi
->cdi_ldt
= cdp
->cpu_ldtp
;
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
));
619 * Fix up the entries in the GDT to point to
620 * this LDT and this TSS.
622 kernel_ldt_desc64
.offset64
= UBER64(cdi
->cdi_ldt
);
623 *(struct fake_descriptor64
*) &cdt
->gdt
[sel_idx(KERNEL_LDT
)] =
625 fix_desc64(&cdt
->gdt
[sel_idx(KERNEL_LDT
)], 1);
627 kernel_ldt_desc64
.offset64
= UBER64(cdi
->cdi_ldt
);
628 *(struct fake_descriptor64
*) &cdt
->gdt
[sel_idx(USER_LDT
)] =
630 fix_desc64(&cdt
->gdt
[sel_idx(USER_LDT
)], 1);
632 kernel_tss_desc64
.offset64
= UBER64(cdi
->cdi_ktss
);
633 *(struct fake_descriptor64
*) &cdt
->gdt
[sel_idx(KERNEL_TSS
)] =
635 fix_desc64(&cdt
->gdt
[sel_idx(KERNEL_TSS
)], 1);
637 /* Set (zeroed) double-fault stack as IST1 */
638 bzero((void *) cdt
->dfstk
, sizeof(cdt
->dfstk
));
639 cdt
->ktss
.ist1
= UBER64((unsigned long)cdt
->dfstk
+ sizeof(cdt
->dfstk
));
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);
645 /* Allocate copyio windows */
646 cpu_userwindow_init(cdp
->cpu_number
);
647 cpu_physwindow_init(cdp
->cpu_number
);
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");
658 cpu_desc_load(cpu_data_t
*cdp
)
660 cpu_desc_index_t
*cdi
= &cdp
->cpu_desc_index
;
662 cdi
->cdi_idt
.size
= 0x1000 + cdp
->cpu_number
;
663 cdi
->cdi_gdt
.size
= sizeof(struct real_descriptor
)*GDTSZ
- 1;
665 lgdt((unsigned long *) &cdi
->cdi_gdt
);
666 lidt((unsigned long *) &cdi
->cdi_idt
);
671 __asm__
volatile("mov %0, %%gs" : : "rm" ((unsigned short)(CPU_DATA_GS
)));
673 #endif /* __i386__ */
676 cpu_desc_load64(cpu_data_t
*cdp
)
678 cpu_desc_index_t
*cdi
= &cdp
->cpu_desc_index
;
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
689 * Refer to commpage/cpu_number.s for the IDT limit trick.
691 gdtptr64
.length
= GDTSZ
* sizeof(struct real_descriptor
) - 1;
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
;
698 /* Make sure busy bit is cleared in the TSS */
699 gdt_desc_p(KERNEL_TSS
)->access
&= ~ACC_TSS_BUSY
;
703 /* Load the GDT, LDT, IDT and TSS */
704 cdi
->cdi_gdt
.size
= sizeof(struct real_descriptor
)*GDTSZ
- 1;
705 cdi
->cdi_idt
.size
= 0x1000 + cdp
->cpu_number
;
706 lgdt((unsigned long *) &cdi
->cdi_gdt
);
707 lidt((unsigned long *) &cdi
->cdi_idt
);
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
)));
724 * Set MSRs for sysenter/sysexit for 32-bit.
727 fast_syscall_init(__unused cpu_data_t
*cdp
)
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);
736 * Set MSRs for sysenter/sysexit and syscall/sysret for 64-bit.
739 fast_syscall_init64(__unused cpu_data_t
*cdp
)
741 wrmsr64(MSR_IA32_SYSENTER_CS
, SYSENTER_CS
);
742 wrmsr64(MSR_IA32_SYSENTER_EIP
, UBER64((uintptr_t) hi64_sysenter
));
743 wrmsr64(MSR_IA32_SYSENTER_ESP
, UBER64(current_sstk()));
744 /* Enable syscall/sysret */
745 wrmsr64(MSR_IA32_EFER
, rdmsr64(MSR_IA32_EFER
) | MSR_IA32_EFER_SCE
);
748 * MSRs for 64-bit syscall/sysret
749 * Note USER_CS because sysret uses this + 16 when returning to
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));
756 * Emulate eflags cleared by sysenter but note that
757 * we also clear the trace trap to avoid the complications
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.
762 wrmsr64(MSR_IA32_FMASK
, EFL_DF
|EFL_IF
|EFL_TF
|EFL_NT
);
766 * Set the Kernel GS base MSR to point to per-cpu data in uber-space.
767 * The uber-space handler (hi64_syscall) uses the swapgs instruction.
769 wrmsr64(MSR_IA32_KERNEL_GS_BASE
, UBER64(cdp
));
771 #if ONLY_SAFE_FOR_LINDA_SERIAL
772 kprintf("fast_syscall_init64() KERNEL_GS_BASE=0x%016llx\n",
773 rdmsr64(MSR_IA32_KERNEL_GS_BASE
));
780 cpu_data_alloc(boolean_t is_boot_cpu
)
786 assert(real_ncpus
== 1);
787 cdp
= &cpu_data_master
;
788 if (cdp
->cpu_processor
== NULL
) {
789 simple_lock_init(&ncpus_lock
, 0);
790 cdp
->cpu_processor
= cpu_processor_alloc(TRUE
);
791 #if NCOPY_WINDOWS > 0
792 cdp
->cpu_pmap
= pmap_cpu_alloc(TRUE
);
799 * Allocate per-cpu data:
801 ret
= kmem_alloc(kernel_map
, (vm_offset_t
*) &cdp
, sizeof(cpu_data_t
));
802 if (ret
!= KERN_SUCCESS
) {
803 printf("cpu_data_alloc() failed, ret=%d\n", ret
);
806 bzero((void*) cdp
, sizeof(cpu_data_t
));
810 cdp
->cpu_is64bit
= cpu_mode_is64bit();
813 * Allocate interrupt stack:
815 ret
= kmem_alloc(kernel_map
,
816 (vm_offset_t
*) &cdp
->cpu_int_stack_top
,
818 if (ret
!= KERN_SUCCESS
) {
819 printf("cpu_data_alloc() int stack failed, ret=%d\n", ret
);
822 bzero((void*) cdp
->cpu_int_stack_top
, INTSTACK_SIZE
);
823 cdp
->cpu_int_stack_top
+= INTSTACK_SIZE
;
826 * Allocate descriptor table:
827 * Size depends on cpu mode.
829 ret
= kmem_alloc(kernel_map
,
830 (vm_offset_t
*) &cdp
->cpu_desc_tablep
,
831 cdp
->cpu_is64bit
? sizeof(cpu_desc_table64_t
)
832 : sizeof(cpu_desc_table_t
));
833 if (ret
!= KERN_SUCCESS
) {
834 printf("cpu_data_alloc() desc_table failed, ret=%d\n", ret
);
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
);
850 /* Machine-check shadow register allocation. */
854 simple_lock(&ncpus_lock
);
856 cpu_data_ptr
[real_ncpus
] = cdp
;
857 cdp
->cpu_number
= real_ncpus
;
859 simple_unlock(&ncpus_lock
);
861 cdp
->cpu_nanotime
= &pal_rtc_nanotime_info
;
863 kprintf("cpu_data_alloc(%d) %p desc_table: %p "
865 "int_stack: 0x%lx-0x%lx\n",
866 cdp
->cpu_number
, cdp
, cdp
->cpu_desc_tablep
, cdp
->cpu_ldtp
,
867 (long)(cdp
->cpu_int_stack_top
- INTSTACK_SIZE
), (long)(cdp
->cpu_int_stack_top
));
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
),
879 kfree((void *) cdp
, sizeof(*cdp
));
885 valid_user_data_selector(uint16_t selector
)
887 sel_t sel
= selector_to_sel(selector
);
892 if (sel
.ti
== SEL_LDT
)
894 else if (sel
.index
< GDTSZ
) {
895 if ((gdt_desc_p(selector
)->access
& ACC_PL_U
) == ACC_PL_U
)
903 valid_user_code_selector(uint16_t selector
)
905 sel_t sel
= selector_to_sel(selector
);
910 if (sel
.ti
== SEL_LDT
) {
911 if (sel
.rpl
== USER_PRIV
)
914 else if (sel
.index
< GDTSZ
&& sel
.rpl
== USER_PRIV
) {
915 if ((gdt_desc_p(selector
)->access
& ACC_PL_U
) == ACC_PL_U
)
923 valid_user_stack_selector(uint16_t selector
)
925 sel_t sel
= selector_to_sel(selector
);
930 if (sel
.ti
== SEL_LDT
) {
931 if (sel
.rpl
== USER_PRIV
)
934 else if (sel
.index
< GDTSZ
&& sel
.rpl
== USER_PRIV
) {
935 if ((gdt_desc_p(selector
)->access
& ACC_PL_U
) == ACC_PL_U
)
943 valid_user_segment_selectors(uint16_t cs
,
950 return valid_user_code_selector(cs
) &&
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
);
958 #if NCOPY_WINDOWS > 0
960 static vm_offset_t user_window_base
= 0;
963 cpu_userwindow_init(int cpu
)
965 cpu_data_t
*cdp
= cpu_data_ptr
[cpu
];
966 vm_offset_t user_window
;
970 num_cpus
= ml_get_max_cpus();
973 panic("cpu_userwindow_init: cpu > num_cpus");
975 if (user_window_base
== 0) {
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");
984 * window must start on a page table boundary
985 * in the virtual address space
987 user_window_base
= (vaddr
+ (NBPDE
- 1)) & ~(NBPDE
- 1);
990 * get rid of any allocation leading up to our
993 vm_deallocate(kernel_map
, vaddr
, user_window_base
- vaddr
);
996 * get rid of tail that we don't need
998 user_window
= user_window_base
+
999 (NBPDE
* NCOPY_WINDOWS
* num_cpus
);
1001 vm_deallocate(kernel_map
, user_window
,
1003 ((NBPDE
* NCOPY_WINDOWS
* num_cpus
) + NBPDE
)) -
1007 user_window
= user_window_base
+ (cpu
* NCOPY_WINDOWS
* NBPDE
);
1009 cdp
->cpu_copywindow_base
= user_window
;
1011 * Abuse this pdp entry, the pdp now actually points to
1012 * an array of copy windows addresses.
1014 cdp
->cpu_copywindow_pdp
= pmap_pde(kernel_pmap
, user_window
);
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
;
1021 fix_desc(&cdi
->cdi_gdt
.ptr
[sel_idx(USER_WINDOW_SEL
)], 1);
1022 #endif /* __i386__ */
1026 cpu_physwindow_init(int cpu
)
1028 cpu_data_t
*cdp
= cpu_data_ptr
[cpu
];
1029 vm_offset_t phys_window
= cdp
->cpu_physwindow_base
;
1031 if (phys_window
== 0) {
1032 if (vm_allocate(kernel_map
, &phys_window
,
1033 PAGE_SIZE
, VM_FLAGS_ANYWHERE
)
1035 panic("cpu_physwindow_init: "
1036 "couldn't allocate phys map window");
1039 * make sure the page that encompasses the
1040 * pte pointer we're interested in actually
1041 * exists in the page table
1043 pmap_expand(kernel_pmap
, phys_window
);
1045 cdp
->cpu_physwindow_base
= phys_window
;
1046 cdp
->cpu_physwindow_ptep
= vtopte(phys_window
);
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
;
1053 fix_desc(&cdi
->cdi_gdt
.ptr
[sel_idx(PHYS_WINDOW_SEL
)], 1);
1054 #endif /* __i386__ */
1056 #endif /* NCOPY_WINDOWS > 0 */
1059 * Load the segment descriptor tables for the current processor.
1062 cpu_mode_init(cpu_data_t
*cdp
)
1065 if (cdp
->cpu_is64bit
) {
1066 cpu_IA32e_enable(cdp
);
1067 cpu_desc_load64(cdp
);
1068 fast_syscall_init64(cdp
);
1070 fast_syscall_init(cdp
);
1073 fast_syscall_init64(cdp
);
1076 /* Call for per-cpu pmap mode initialization */