2 * Copyright (c) 2000-2006 Apple Computer, 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 <vm/vm_kern.h>
67 #include <vm/vm_map.h>
69 #include <i386/mp_desc.h>
70 #include <i386/lock.h>
71 #include <i386/misc_protos.h>
73 #include <i386/pmap.h>
74 #include <i386/machine_check.h>
76 #include <kern/misc_protos.h>
81 * The i386 needs an interrupt stack to keep the PCB stack from being
82 * overrun by interrupts. All interrupt stacks MUST lie at lower addresses
83 * than any thread`s kernel stack.
87 * First cpu`s interrupt stack.
89 extern uint32_t low_intstack
[]; /* bottom */
90 extern uint32_t low_eintstack
[]; /* top */
93 * Per-cpu data area pointers.
94 * The master cpu (cpu 0) has its data area statically allocated;
95 * others are allocated dynamically and this array is updated at runtime.
97 cpu_data_t cpu_data_master
;
98 cpu_data_t
*cpu_data_ptr
[MAX_CPUS
] = { [0] &cpu_data_master
};
100 decl_simple_lock_data(,cpu_lock
); /* protects real_ncpus */
101 unsigned int real_ncpus
= 1;
102 unsigned int max_ncpus
= MAX_CPUS
;
104 extern void *hi_remap_text
;
105 #define HI_TEXT(lo_text) \
106 (((uint32_t)&lo_text - (uint32_t)&hi_remap_text) + HIGH_MEM_BASE)
108 extern void hi_sysenter(void);
109 extern void hi64_sysenter(void);
110 extern void hi64_syscall(void);
113 * Multiprocessor i386/i486 systems use a separate copy of the
114 * GDT, IDT, LDT, and kernel TSS per processor. The first three
115 * are separate to avoid lock contention: the i386 uses locked
116 * memory cycles to access the descriptor tables. The TSS is
117 * separate since each processor needs its own kernel stack,
118 * and since using a TSS marks it busy.
122 * Allocate and initialize the per-processor descriptor tables.
125 struct fake_descriptor ldt_desc_pattern
= {
127 LDTSZ_MIN
* sizeof(struct fake_descriptor
) - 1,
129 ACC_P
|ACC_PL_K
|ACC_LDT
132 struct fake_descriptor tss_desc_pattern
= {
134 sizeof(struct i386_tss
) - 1,
136 ACC_P
|ACC_PL_K
|ACC_TSS
139 struct fake_descriptor cpudata_desc_pattern
= {
141 sizeof(cpu_data_t
)-1,
143 ACC_P
|ACC_PL_K
|ACC_DATA_W
146 struct fake_descriptor userwindow_desc_pattern
= {
148 ((NBPDE
* NCOPY_WINDOWS
) / PAGE_SIZE
) - 1,
150 ACC_P
|ACC_PL_U
|ACC_DATA_W
153 struct fake_descriptor physwindow_desc_pattern
= {
157 ACC_P
|ACC_PL_K
|ACC_DATA_W
161 * This is the expanded, 64-bit variant of the kernel LDT descriptor.
162 * When switching to 64-bit mode this replaces KERNEL_LDT entry
163 * and the following empty slot. This enables the LDT to be referenced
164 * in the uber-space remapping window on the kernel.
166 struct fake_descriptor64 kernel_ldt_desc64
= {
167 FAKE_UBER64(&master_ldt
),
168 LDTSZ_MIN
*sizeof(struct fake_descriptor
)-1,
170 ACC_P
|ACC_PL_K
|ACC_LDT
,
175 * This is the expanded, 64-bit variant of the kernel TSS descriptor.
176 * It is follows pattern of the KERNEL_LDT.
178 struct fake_descriptor64 kernel_tss_desc64
= {
179 FAKE_UBER64(&master_ktss64
),
180 sizeof(struct x86_64_tss
)-1,
182 ACC_P
|ACC_PL_K
|ACC_TSS
,
189 boolean_t is_boot_cpu
)
191 cpu_desc_table_t
*cdt
= cdp
->cpu_desc_tablep
;
192 cpu_desc_index_t
*cdi
= &cdp
->cpu_desc_index
;
196 * Master CPU uses the tables built at boot time.
197 * Just set the index pointers to the high shared-mapping space.
198 * Note that the sysenter stack uses empty space above the ktss
199 * in the HIGH_FIXED_KTSS page. In this case we don't map the
200 * the real master_sstk in low memory.
202 cdi
->cdi_ktss
= (struct i386_tss
*)
203 pmap_index_to_virt(HIGH_FIXED_KTSS
) ;
204 cdi
->cdi_sstk
= (vm_offset_t
) (cdi
->cdi_ktss
+ 1) +
205 (vm_offset_t
) &master_sstk
.top
-
206 (vm_offset_t
) &master_sstk
;
208 cdi
->cdi_dbtss
= (struct i386_tss
*)
209 pmap_index_to_virt(HIGH_FIXED_DBTSS
);
210 #endif /* MACH_KDB */
211 cdi
->cdi_gdt
= (struct fake_descriptor
*)
212 pmap_index_to_virt(HIGH_FIXED_GDT
);
213 cdi
->cdi_idt
= (struct fake_descriptor
*)
214 pmap_index_to_virt(HIGH_FIXED_IDT
);
215 cdi
->cdi_ldt
= (struct fake_descriptor
*)
216 pmap_index_to_virt(HIGH_FIXED_LDT_BEGIN
);
219 vm_offset_t cpu_hi_desc
;
221 cpu_hi_desc
= pmap_cpu_high_shared_remap(cdp
->cpu_number
,
223 (vm_offset_t
) cdt
, 1);
226 * Per-cpu GDT, IDT, LDT, KTSS descriptors are allocated in one
227 * block (cpu_desc_table) and double-mapped into high shared space
228 * in one page window.
229 * Also, a transient stack for the fast sysenter path. The top of
230 * which is set at context switch time to point to the PCB using
233 cdi
->cdi_gdt
= (struct fake_descriptor
*) (cpu_hi_desc
+
234 offsetof(cpu_desc_table_t
, gdt
[0]));
235 cdi
->cdi_idt
= (struct fake_descriptor
*) (cpu_hi_desc
+
236 offsetof(cpu_desc_table_t
, idt
[0]));
237 cdi
->cdi_ktss
= (struct i386_tss
*) (cpu_hi_desc
+
238 offsetof(cpu_desc_table_t
, ktss
));
239 cdi
->cdi_sstk
= cpu_hi_desc
+
240 offsetof(cpu_desc_table_t
, sstk
.top
);
243 * LDT descriptors are mapped into a seperate area.
245 cdi
->cdi_ldt
= (struct fake_descriptor
*)
246 pmap_cpu_high_shared_remap(
249 (vm_offset_t
) cdp
->cpu_ldtp
,
250 HIGH_CPU_LDT_END
- HIGH_CPU_LDT_BEGIN
+ 1);
255 bcopy((char *)master_idt
,
258 bcopy((char *)master_gdt
,
261 bcopy((char *)master_ldt
,
262 (char *)cdp
->cpu_ldtp
,
264 bzero((char *)&cdt
->ktss
,
265 sizeof(struct i386_tss
));
268 cdi
->cdi_dbtss
= (struct i386_tss
*) (cpu_hi_desc
+
269 offsetof(cpu_desc_table_t
, dbtss
));
270 bcopy((char *)&master_dbtss
,
272 sizeof(struct i386_tss
));
273 #endif /* MACH_KDB */
276 * Fix up the entries in the GDT to point to
277 * this LDT and this TSS.
279 cdt
->gdt
[sel_idx(KERNEL_LDT
)] = ldt_desc_pattern
;
280 cdt
->gdt
[sel_idx(KERNEL_LDT
)].offset
= (vm_offset_t
) cdi
->cdi_ldt
;
281 fix_desc(&cdt
->gdt
[sel_idx(KERNEL_LDT
)], 1);
283 cdt
->gdt
[sel_idx(USER_LDT
)] = ldt_desc_pattern
;
284 cdt
->gdt
[sel_idx(USER_LDT
)].offset
= (vm_offset_t
) cdi
->cdi_ldt
;
285 fix_desc(&cdt
->gdt
[sel_idx(USER_LDT
)], 1);
287 cdt
->gdt
[sel_idx(KERNEL_TSS
)] = tss_desc_pattern
;
288 cdt
->gdt
[sel_idx(KERNEL_TSS
)].offset
= (vm_offset_t
) cdi
->cdi_ktss
;
289 fix_desc(&cdt
->gdt
[sel_idx(KERNEL_TSS
)], 1);
291 cdt
->gdt
[sel_idx(CPU_DATA_GS
)] = cpudata_desc_pattern
;
292 cdt
->gdt
[sel_idx(CPU_DATA_GS
)].offset
= (vm_offset_t
) cdp
;
293 fix_desc(&cdt
->gdt
[sel_idx(CPU_DATA_GS
)], 1);
296 cdt
->gdt
[sel_idx(DEBUG_TSS
)] = tss_desc_pattern
;
297 cdt
->gdt
[sel_idx(DEBUG_TSS
)].offset
= (vm_offset_t
) cdi
->cdi_dbtss
;
298 fix_desc(&cdt
->gdt
[sel_idx(DEBUG_TSS
)], 1);
300 cdt
->dbtss
.esp0
= (int)(db_task_stack_store
+
301 (INTSTACK_SIZE
* (cdp
->cpu_number
)) - sizeof (natural_t
));
302 cdt
->dbtss
.esp
= cdt
->dbtss
.esp0
;
303 cdt
->dbtss
.eip
= (int)&db_task_start
;
304 #endif /* MACH_KDB */
306 cdt
->ktss
.ss0
= KERNEL_DS
;
307 cdt
->ktss
.io_bit_map_offset
= 0x0FFF; /* no IO bitmap */
309 cpu_userwindow_init(cdp
->cpu_number
);
310 cpu_physwindow_init(cdp
->cpu_number
);
319 boolean_t is_boot_cpu
)
321 cpu_desc_table64_t
*cdt
= (cpu_desc_table64_t
*)
322 cdp
->cpu_desc_tablep
;
323 cpu_desc_index_t
*cdi
= &cdp
->cpu_desc_index
;
327 * Master CPU uses the tables built at boot time.
328 * Just set the index pointers to the low memory space.
329 * Note that in 64-bit mode these are addressed in the
330 * double-mapped window (uber-space).
332 cdi
->cdi_ktss
= (struct i386_tss
*) &master_ktss64
;
333 cdi
->cdi_sstk
= (vm_offset_t
) &master_sstk
.top
;
334 cdi
->cdi_gdt
= master_gdt
;
335 cdi
->cdi_idt
= (struct fake_descriptor
*) &master_idt64
;
336 cdi
->cdi_ldt
= (struct fake_descriptor
*) &master_ldt
;
338 /* Replace the expanded LDT and TSS slots in the GDT: */
339 *(struct fake_descriptor64
*) &master_gdt
[sel_idx(KERNEL_LDT
)] =
341 *(struct fake_descriptor64
*) &master_gdt
[sel_idx(KERNEL_TSS
)] =
345 * Fix up the expanded descriptors for 64-bit.
347 fix_desc64((void *) &master_idt64
, IDTSZ
);
348 fix_desc64((void *) &master_gdt
[sel_idx(KERNEL_LDT
)], 1);
349 fix_desc64((void *) &master_gdt
[sel_idx(KERNEL_TSS
)], 1);
352 * Set the double-fault stack as IST1 in the 64-bit TSS
354 master_ktss64
.ist1
= UBER64(df_task_stack_end
);
358 * Per-cpu GDT, IDT, KTSS descriptors are allocated in kernel
359 * heap (cpu_desc_table) and double-mapped in uber-space
361 * LDT descriptors are mapped into a separate area.
363 cdi
->cdi_gdt
= (struct fake_descriptor
*)cdt
->gdt
;
364 cdi
->cdi_idt
= (struct fake_descriptor
*)cdt
->idt
;
365 cdi
->cdi_ktss
= (struct i386_tss
*)&cdt
->ktss
;
366 cdi
->cdi_sstk
= (vm_offset_t
)&cdt
->sstk
.top
;
367 cdi
->cdi_ldt
= cdp
->cpu_ldtp
;
372 bcopy((char *)master_idt64
,
374 sizeof(master_idt64
));
375 bcopy((char *)master_gdt
,
378 bcopy((char *)master_ldt
,
379 (char *)cdp
->cpu_ldtp
,
381 bcopy((char *)&master_ktss64
,
383 sizeof(struct x86_64_tss
));
386 * Fix up the entries in the GDT to point to
387 * this LDT and this TSS.
389 kernel_ldt_desc64
.offset
[0] = (vm_offset_t
) cdi
->cdi_ldt
;
390 *(struct fake_descriptor64
*) &cdt
->gdt
[sel_idx(KERNEL_LDT
)] =
392 fix_desc64(&cdt
->gdt
[sel_idx(KERNEL_LDT
)], 1);
394 kernel_ldt_desc64
.offset
[0] = (vm_offset_t
) cdi
->cdi_ldt
;
395 *(struct fake_descriptor64
*) &cdt
->gdt
[sel_idx(USER_LDT
)] =
397 fix_desc64(&cdt
->gdt
[sel_idx(USER_LDT
)], 1);
399 kernel_tss_desc64
.offset
[0] = (vm_offset_t
) cdi
->cdi_ktss
;
400 *(struct fake_descriptor64
*) &cdt
->gdt
[sel_idx(KERNEL_TSS
)] =
402 fix_desc64(&cdt
->gdt
[sel_idx(KERNEL_TSS
)], 1);
404 cdt
->gdt
[sel_idx(CPU_DATA_GS
)] = cpudata_desc_pattern
;
405 cdt
->gdt
[sel_idx(CPU_DATA_GS
)].offset
= (vm_offset_t
) cdp
;
406 fix_desc(&cdt
->gdt
[sel_idx(CPU_DATA_GS
)], 1);
408 /* Set double-fault stack as IST1 */
409 cdt
->ktss
.ist1
= UBER64((unsigned long)cdt
->dfstk
410 + sizeof(cdt
->dfstk
));
413 * Allocate copyio windows.
415 cpu_userwindow_init(cdp
->cpu_number
);
416 cpu_physwindow_init(cdp
->cpu_number
);
419 /* Require that the top of the sysenter stack is 16-byte aligned */
420 if ((cdi
->cdi_sstk
% 16) != 0)
421 panic("cpu_desc_init64() sysenter stack not 16-byte aligned");
425 * Set MSRs for sysenter/sysexit for 64-bit.
428 fast_syscall_init64(void)
430 wrmsr64(MSR_IA32_SYSENTER_CS
, SYSENTER_CS
);
431 wrmsr64(MSR_IA32_SYSENTER_EIP
, UBER64(hi64_sysenter
));
432 wrmsr64(MSR_IA32_SYSENTER_ESP
, UBER64(current_sstk()));
434 /* Enable syscall/sysret */
435 wrmsr64(MSR_IA32_EFER
, rdmsr64(MSR_IA32_EFER
) | MSR_IA32_EFER_SCE
);
438 * MSRs for 64-bit syscall/sysret
439 * Note USER_CS because sysret uses this + 16 when returning to
442 wrmsr64(MSR_IA32_LSTAR
, UBER64(hi64_syscall
));
443 wrmsr64(MSR_IA32_STAR
, (((uint64_t)USER_CS
) << 48) |
444 (((uint64_t)KERNEL64_CS
) << 32));
446 * Emulate eflags cleared by sysenter but note that
447 * we also clear the trace trap to avoid the complications
448 * of single-stepping into a syscall. The nested task bit
449 * is also cleared to avoid a spurious "task switch"
450 * should we choose to return via an IRET.
452 wrmsr64(MSR_IA32_FMASK
, EFL_DF
|EFL_IF
|EFL_TF
|EFL_NT
);
455 * Set the Kernel GS base MSR to point to per-cpu data in uber-space.
456 * The uber-space handler (hi64_syscall) uses the swapgs instruction.
458 wrmsr64(MSR_IA32_KERNEL_GS_BASE
,
459 UBER64((unsigned long)current_cpu_datap()));
460 kprintf("fast_syscall_init64() KERNEL_GS_BASE=0x%016llx\n",
461 rdmsr64(MSR_IA32_KERNEL_GS_BASE
));
465 * Set MSRs for sysenter/sysexit
468 fast_syscall_init(void)
470 wrmsr(MSR_IA32_SYSENTER_CS
, SYSENTER_CS
, 0);
471 wrmsr(MSR_IA32_SYSENTER_EIP
, HI_TEXT(hi_sysenter
), 0);
472 wrmsr(MSR_IA32_SYSENTER_ESP
, current_sstk(), 0);
476 cpu_data_alloc(boolean_t is_boot_cpu
)
482 assert(real_ncpus
== 1);
483 simple_lock_init(&cpu_lock
, 0);
484 cdp
= &cpu_data_master
;
485 if (cdp
->cpu_processor
== NULL
) {
486 cdp
->cpu_processor
= cpu_processor_alloc(TRUE
);
487 cdp
->cpu_pmap
= pmap_cpu_alloc(TRUE
);
489 cdp
->cpu_is64bit
= FALSE
;
490 cdp
->cpu_int_stack_top
= (vm_offset_t
) low_eintstack
;
491 cpu_desc_init(cdp
, TRUE
);
497 /* Check count before making allocations */
498 if (real_ncpus
>= max_ncpus
)
502 * Allocate per-cpu data:
504 ret
= kmem_alloc(kernel_map
,
505 (vm_offset_t
*) &cdp
, sizeof(cpu_data_t
));
506 if (ret
!= KERN_SUCCESS
) {
507 printf("cpu_data_alloc() failed, ret=%d\n", ret
);
510 bzero((void*) cdp
, sizeof(cpu_data_t
));
514 cdp
->cpu_is64bit
= cpu_mode_is64bit();
517 * Allocate interrupt stack:
519 ret
= kmem_alloc(kernel_map
,
520 (vm_offset_t
*) &cdp
->cpu_int_stack_top
,
522 if (ret
!= KERN_SUCCESS
) {
523 printf("cpu_data_alloc() int stack failed, ret=%d\n", ret
);
526 bzero((void*) cdp
->cpu_int_stack_top
, INTSTACK_SIZE
);
527 cdp
->cpu_int_stack_top
+= INTSTACK_SIZE
;
530 * Allocate descriptor table:
531 * Size depends on cpu mode.
533 ret
= kmem_alloc(kernel_map
,
534 (vm_offset_t
*) &cdp
->cpu_desc_tablep
,
535 cdp
->cpu_is64bit
? sizeof(cpu_desc_table64_t
)
536 : sizeof(cpu_desc_table_t
));
537 if (ret
!= KERN_SUCCESS
) {
538 printf("cpu_data_alloc() desc_table failed, ret=%d\n", ret
);
545 ret
= kmem_alloc(kernel_map
,
546 (vm_offset_t
*) &cdp
->cpu_ldtp
,
547 sizeof(struct real_descriptor
) * LDTSZ
);
548 if (ret
!= KERN_SUCCESS
) {
549 printf("cpu_data_alloc() ldt failed, ret=%d\n", ret
);
553 /* Machine-check shadow register allocation. */
556 simple_lock(&cpu_lock
);
557 if (real_ncpus
>= max_ncpus
) {
558 simple_unlock(&cpu_lock
);
561 cpu_data_ptr
[real_ncpus
] = cdp
;
562 cdp
->cpu_number
= real_ncpus
;
564 simple_unlock(&cpu_lock
);
566 kprintf("cpu_data_alloc(%d) %p desc_table: %p "
568 "int_stack: 0x%x-0x%x\n",
569 cdp
->cpu_number
, cdp
, cdp
->cpu_desc_tablep
, cdp
->cpu_ldtp
,
570 cdp
->cpu_int_stack_top
- INTSTACK_SIZE
, cdp
->cpu_int_stack_top
);
576 if (cdp
->cpu_desc_tablep
)
577 kfree((void *) cdp
->cpu_desc_tablep
,
578 sizeof(*cdp
->cpu_desc_tablep
));
579 if (cdp
->cpu_int_stack_top
)
580 kfree((void *) (cdp
->cpu_int_stack_top
- INTSTACK_SIZE
),
582 kfree((void *) cdp
, sizeof(*cdp
));
588 valid_user_segment_selectors(uint16_t cs
,
595 return valid_user_code_selector(cs
) &&
596 valid_user_stack_selector(ss
) &&
597 valid_user_data_selector(ds
) &&
598 valid_user_data_selector(es
) &&
599 valid_user_data_selector(fs
) &&
600 valid_user_data_selector(gs
);
604 static vm_offset_t user_window_base
= 0;
607 cpu_userwindow_init(int cpu
)
609 cpu_data_t
*cdp
= cpu_data_ptr
[cpu
];
610 cpu_desc_index_t
*cdi
= &cdp
->cpu_desc_index
;
611 vm_offset_t user_window
;
615 num_cpus
= ml_get_max_cpus();
618 panic("cpu_userwindow_init: cpu > num_cpus");
620 if (user_window_base
== 0) {
622 if (vm_allocate(kernel_map
, &vaddr
,
623 (NBPDE
* NCOPY_WINDOWS
* num_cpus
) + NBPDE
,
624 VM_FLAGS_ANYWHERE
) != KERN_SUCCESS
)
625 panic("cpu_userwindow_init: "
626 "couldn't allocate user map window");
629 * window must start on a page table boundary
630 * in the virtual address space
632 user_window_base
= (vaddr
+ (NBPDE
- 1)) & ~(NBPDE
- 1);
635 * get rid of any allocation leading up to our
638 vm_deallocate(kernel_map
, vaddr
, user_window_base
- vaddr
);
641 * get rid of tail that we don't need
643 user_window
= user_window_base
+
644 (NBPDE
* NCOPY_WINDOWS
* num_cpus
);
646 vm_deallocate(kernel_map
, user_window
,
648 ((NBPDE
* NCOPY_WINDOWS
* num_cpus
) + NBPDE
)) -
652 user_window
= user_window_base
+ (cpu
* NCOPY_WINDOWS
* NBPDE
);
654 cdp
->cpu_copywindow_base
= user_window
;
655 cdp
->cpu_copywindow_pdp
= pmap_pde(kernel_pmap
, user_window
);
657 cdi
->cdi_gdt
[sel_idx(USER_WINDOW_SEL
)] = userwindow_desc_pattern
;
658 cdi
->cdi_gdt
[sel_idx(USER_WINDOW_SEL
)].offset
= user_window
;
660 fix_desc(&cdi
->cdi_gdt
[sel_idx(USER_WINDOW_SEL
)], 1);
665 cpu_physwindow_init(int cpu
)
667 cpu_data_t
*cdp
= cpu_data_ptr
[cpu
];
668 cpu_desc_index_t
*cdi
= &cdp
->cpu_desc_index
;
669 vm_offset_t phys_window
;
671 if (vm_allocate(kernel_map
, &phys_window
,
672 PAGE_SIZE
, VM_FLAGS_ANYWHERE
)
674 panic("cpu_physwindow_init: couldn't allocate phys map window");
677 * make sure the page that encompasses the
678 * pte pointer we're interested in actually
679 * exists in the page table
681 pmap_expand(kernel_pmap
, phys_window
);
683 cdp
->cpu_physwindow_base
= phys_window
;
684 cdp
->cpu_physwindow_ptep
= vtopte(phys_window
);
686 cdi
->cdi_gdt
[sel_idx(PHYS_WINDOW_SEL
)] = physwindow_desc_pattern
;
687 cdi
->cdi_gdt
[sel_idx(PHYS_WINDOW_SEL
)].offset
= phys_window
;
689 fix_desc(&cdi
->cdi_gdt
[sel_idx(PHYS_WINDOW_SEL
)], 1);
696 } __attribute__((__packed__
)) table_descriptor64_t
;
698 extern table_descriptor64_t gdtptr64
;
699 extern table_descriptor64_t idtptr64
;
701 * Load the segment descriptor tables for the current processor.
704 cpu_desc_load64(cpu_data_t
*cdp
)
706 cpu_desc_index_t
*cdi
= &cdp
->cpu_desc_index
;
709 * Load up the new descriptors etc
710 * ml_load_desc64() expects these global pseudo-descriptors:
711 * gdtptr64 -> master_gdt
712 * idtptr64 -> master_idt64
713 * These are 10-byte descriptors with 64-bit addresses into
716 gdtptr64
.length
= sizeof(master_gdt
) - 1;
717 gdtptr64
.offset
[0] = (uint32_t) cdi
->cdi_gdt
;
718 gdtptr64
.offset
[1] = KERNEL_UBER_BASE_HI32
;
719 idtptr64
.length
= sizeof(master_idt64
) - 1;
720 idtptr64
.offset
[0] = (uint32_t) cdi
->cdi_idt
;
721 idtptr64
.offset
[1] = KERNEL_UBER_BASE_HI32
;
723 /* Make sure busy bit is cleared in the TSS */
724 gdt_desc_p(KERNEL_TSS
)->access
&= ~ACC_TSS_BUSY
;
728 kprintf("64-bit descriptor tables loaded\n");
732 cpu_mode_init(cpu_data_t
*cdp
)
734 if (cpu_mode_is64bit()) {
735 cpu_IA32e_enable(cdp
);
736 cpu_desc_load64(cdp
);
737 fast_syscall_init64();
742 /* Call for per-cpu pmap mode initialization */