]> git.saurik.com Git - apple/xnu.git/blame_incremental - osfmk/i386/mp_desc.c
xnu-1228.5.18.tar.gz
[apple/xnu.git] / osfmk / i386 / mp_desc.c
... / ...
CommitLineData
1/*
2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28/*
29 * @OSF_COPYRIGHT@
30 */
31/*
32 * Mach Operating System
33 * Copyright (c) 1991,1990 Carnegie Mellon University
34 * All Rights Reserved.
35 *
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
41 *
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
45 *
46 * Carnegie Mellon requests users of this software to return to
47 *
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
52 *
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
55 */
56
57/*
58 */
59
60#include <kern/cpu_number.h>
61#include <kern/kalloc.h>
62#include <kern/cpu_data.h>
63#include <mach/mach_types.h>
64#include <mach/machine.h>
65#include <mach/vm_map.h>
66#include <vm/vm_kern.h>
67#include <vm/vm_map.h>
68
69#include <i386/mp_desc.h>
70#include <i386/lock.h>
71#include <i386/misc_protos.h>
72#include <i386/mp.h>
73#include <i386/pmap.h>
74#include <i386/machine_check.h>
75
76#include <kern/misc_protos.h>
77
78#include <mach_kdb.h>
79
80/*
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.
84 */
85
86/*
87 * First cpu`s interrupt stack.
88 */
89extern uint32_t low_intstack[]; /* bottom */
90extern uint32_t low_eintstack[]; /* top */
91
92/*
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.
96 */
97cpu_data_t cpu_data_master;
98cpu_data_t *cpu_data_ptr[MAX_CPUS] = { [0] &cpu_data_master };
99
100decl_simple_lock_data(,cpu_lock); /* protects real_ncpus */
101unsigned int real_ncpus = 1;
102unsigned int max_ncpus = MAX_CPUS;
103
104extern void *hi_remap_text;
105#define HI_TEXT(lo_text) \
106 (((uint32_t)&lo_text - (uint32_t)&hi_remap_text) + HIGH_MEM_BASE)
107
108extern void hi_sysenter(void);
109extern void hi64_sysenter(void);
110extern void hi64_syscall(void);
111
112/*
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.
119 */
120
121/*
122 * Allocate and initialize the per-processor descriptor tables.
123 */
124
125struct fake_descriptor ldt_desc_pattern = {
126 (unsigned int) 0,
127 LDTSZ_MIN * sizeof(struct fake_descriptor) - 1,
128 0,
129 ACC_P|ACC_PL_K|ACC_LDT
130};
131
132struct fake_descriptor tss_desc_pattern = {
133 (unsigned int) 0,
134 sizeof(struct i386_tss) - 1,
135 0,
136 ACC_P|ACC_PL_K|ACC_TSS
137};
138
139struct fake_descriptor cpudata_desc_pattern = {
140 (unsigned int) 0,
141 sizeof(cpu_data_t)-1,
142 SZ_32,
143 ACC_P|ACC_PL_K|ACC_DATA_W
144};
145
146struct fake_descriptor userwindow_desc_pattern = {
147 (unsigned int) 0,
148 ((NBPDE * NCOPY_WINDOWS) / PAGE_SIZE) - 1,
149 SZ_32 | SZ_G,
150 ACC_P|ACC_PL_U|ACC_DATA_W
151};
152
153struct fake_descriptor physwindow_desc_pattern = {
154 (unsigned int) 0,
155 PAGE_SIZE - 1,
156 SZ_32,
157 ACC_P|ACC_PL_K|ACC_DATA_W
158};
159
160/*
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.
165 */
166struct fake_descriptor64 kernel_ldt_desc64 = {
167 FAKE_UBER64(&master_ldt),
168 LDTSZ_MIN*sizeof(struct fake_descriptor)-1,
169 0,
170 ACC_P|ACC_PL_K|ACC_LDT,
171 0
172};
173
174/*
175 * This is the expanded, 64-bit variant of the kernel TSS descriptor.
176 * It is follows pattern of the KERNEL_LDT.
177 */
178struct fake_descriptor64 kernel_tss_desc64 = {
179 FAKE_UBER64(&master_ktss64),
180 sizeof(struct x86_64_tss)-1,
181 0,
182 ACC_P|ACC_PL_K|ACC_TSS,
183 0
184};
185
186void
187cpu_desc_init(
188 cpu_data_t *cdp,
189 boolean_t is_boot_cpu)
190{
191 cpu_desc_table_t *cdt = cdp->cpu_desc_tablep;
192 cpu_desc_index_t *cdi = &cdp->cpu_desc_index;
193
194 if (is_boot_cpu) {
195 /*
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.
201 */
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;
207#if MACH_KDB
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);
217 } else {
218
219 vm_offset_t cpu_hi_desc;
220
221 cpu_hi_desc = pmap_cpu_high_shared_remap(cdp->cpu_number,
222 HIGH_CPU_DESC,
223 (vm_offset_t) cdt, 1);
224
225 /*
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
231 * the high address.
232 */
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);
241
242 /*
243 * LDT descriptors are mapped into a seperate area.
244 */
245 cdi->cdi_ldt = (struct fake_descriptor *)
246 pmap_cpu_high_shared_remap(
247 cdp->cpu_number,
248 HIGH_CPU_LDT_BEGIN,
249 (vm_offset_t) cdp->cpu_ldtp,
250 HIGH_CPU_LDT_END - HIGH_CPU_LDT_BEGIN + 1);
251
252 /*
253 * Copy the tables
254 */
255 bcopy((char *)master_idt,
256 (char *)cdt->idt,
257 sizeof(master_idt));
258 bcopy((char *)master_gdt,
259 (char *)cdt->gdt,
260 sizeof(master_gdt));
261 bcopy((char *)master_ldt,
262 (char *)cdp->cpu_ldtp,
263 sizeof(master_ldt));
264 bzero((char *)&cdt->ktss,
265 sizeof(struct i386_tss));
266
267#if MACH_KDB
268 cdi->cdi_dbtss = (struct i386_tss *) (cpu_hi_desc +
269 offsetof(cpu_desc_table_t, dbtss));
270 bcopy((char *)&master_dbtss,
271 (char *)&cdt->dbtss,
272 sizeof(struct i386_tss));
273#endif /* MACH_KDB */
274
275 /*
276 * Fix up the entries in the GDT to point to
277 * this LDT and this TSS.
278 */
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);
282
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);
286
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);
290
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);
294
295#if MACH_KDB
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);
299
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 */
305
306 cdt->ktss.ss0 = KERNEL_DS;
307 cdt->ktss.io_bit_map_offset = 0x0FFF; /* no IO bitmap */
308
309 cpu_userwindow_init(cdp->cpu_number);
310 cpu_physwindow_init(cdp->cpu_number);
311
312 }
313
314}
315
316void
317cpu_desc_init64(
318 cpu_data_t *cdp,
319 boolean_t is_boot_cpu)
320{
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;
324
325 if (is_boot_cpu) {
326 /*
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).
331 */
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;
337
338 /* Replace the expanded LDT and TSS slots in the GDT: */
339 *(struct fake_descriptor64 *) &master_gdt[sel_idx(KERNEL_LDT)] =
340 kernel_ldt_desc64;
341 *(struct fake_descriptor64 *) &master_gdt[sel_idx(KERNEL_TSS)] =
342 kernel_tss_desc64;
343
344 /*
345 * Fix up the expanded descriptors for 64-bit.
346 */
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);
350
351 /*
352 * Set the double-fault stack as IST1 in the 64-bit TSS
353 */
354 master_ktss64.ist1 = UBER64(df_task_stack_end);
355
356 } else {
357 /*
358 * Per-cpu GDT, IDT, KTSS descriptors are allocated in kernel
359 * heap (cpu_desc_table) and double-mapped in uber-space
360 * (over 4GB).
361 * LDT descriptors are mapped into a separate area.
362 */
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;
368
369 /*
370 * Copy the tables
371 */
372 bcopy((char *)master_idt64,
373 (char *)cdt->idt,
374 sizeof(master_idt64));
375 bcopy((char *)master_gdt,
376 (char *)cdt->gdt,
377 sizeof(master_gdt));
378 bcopy((char *)master_ldt,
379 (char *)cdp->cpu_ldtp,
380 sizeof(master_ldt));
381 bcopy((char *)&master_ktss64,
382 (char *)&cdt->ktss,
383 sizeof(struct x86_64_tss));
384
385 /*
386 * Fix up the entries in the GDT to point to
387 * this LDT and this TSS.
388 */
389 kernel_ldt_desc64.offset[0] = (vm_offset_t) cdi->cdi_ldt;
390 *(struct fake_descriptor64 *) &cdt->gdt[sel_idx(KERNEL_LDT)] =
391 kernel_ldt_desc64;
392 fix_desc64(&cdt->gdt[sel_idx(KERNEL_LDT)], 1);
393
394 kernel_ldt_desc64.offset[0] = (vm_offset_t) cdi->cdi_ldt;
395 *(struct fake_descriptor64 *) &cdt->gdt[sel_idx(USER_LDT)] =
396 kernel_ldt_desc64;
397 fix_desc64(&cdt->gdt[sel_idx(USER_LDT)], 1);
398
399 kernel_tss_desc64.offset[0] = (vm_offset_t) cdi->cdi_ktss;
400 *(struct fake_descriptor64 *) &cdt->gdt[sel_idx(KERNEL_TSS)] =
401 kernel_tss_desc64;
402 fix_desc64(&cdt->gdt[sel_idx(KERNEL_TSS)], 1);
403
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);
407
408 /* Set double-fault stack as IST1 */
409 cdt->ktss.ist1 = UBER64((unsigned long)cdt->dfstk
410 + sizeof(cdt->dfstk));
411
412 /*
413 * Allocate copyio windows.
414 */
415 cpu_userwindow_init(cdp->cpu_number);
416 cpu_physwindow_init(cdp->cpu_number);
417 }
418
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");
422}
423
424/*
425 * Set MSRs for sysenter/sysexit for 64-bit.
426 */
427static void
428fast_syscall_init64(void)
429{
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()));
433
434 /* Enable syscall/sysret */
435 wrmsr64(MSR_IA32_EFER, rdmsr64(MSR_IA32_EFER) | MSR_IA32_EFER_SCE);
436
437 /*
438 * MSRs for 64-bit syscall/sysret
439 * Note USER_CS because sysret uses this + 16 when returning to
440 * 64-bit code.
441 */
442 wrmsr64(MSR_IA32_LSTAR, UBER64(hi64_syscall));
443 wrmsr64(MSR_IA32_STAR, (((uint64_t)USER_CS) << 48) |
444 (((uint64_t)KERNEL64_CS) << 32));
445 /*
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.
451 */
452 wrmsr64(MSR_IA32_FMASK, EFL_DF|EFL_IF|EFL_TF|EFL_NT);
453
454 /*
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.
457 */
458 wrmsr64(MSR_IA32_KERNEL_GS_BASE,
459 UBER64((unsigned long)current_cpu_datap()));
460
461#if ONLY_SAFE_FOR_LINDA_SERIAL
462 kprintf("fast_syscall_init64() KERNEL_GS_BASE=0x%016llx\n",
463 rdmsr64(MSR_IA32_KERNEL_GS_BASE));
464#endif
465}
466
467/*
468 * Set MSRs for sysenter/sysexit
469 */
470static void
471fast_syscall_init(void)
472{
473 wrmsr(MSR_IA32_SYSENTER_CS, SYSENTER_CS, 0);
474 wrmsr(MSR_IA32_SYSENTER_EIP, HI_TEXT(hi_sysenter), 0);
475 wrmsr(MSR_IA32_SYSENTER_ESP, current_sstk(), 0);
476}
477
478cpu_data_t *
479cpu_data_alloc(boolean_t is_boot_cpu)
480{
481 int ret;
482 cpu_data_t *cdp;
483
484 if (is_boot_cpu) {
485 assert(real_ncpus == 1);
486 simple_lock_init(&cpu_lock, 0);
487 cdp = &cpu_data_master;
488 if (cdp->cpu_processor == NULL) {
489 cdp->cpu_processor = cpu_processor_alloc(TRUE);
490 cdp->cpu_pmap = pmap_cpu_alloc(TRUE);
491 cdp->cpu_this = cdp;
492 cdp->cpu_is64bit = FALSE;
493 cdp->cpu_int_stack_top = (vm_offset_t) low_eintstack;
494 cpu_desc_init(cdp, TRUE);
495 fast_syscall_init();
496 }
497 return cdp;
498 }
499
500 /* Check count before making allocations */
501 if (real_ncpus >= max_ncpus)
502 return NULL;
503
504 /*
505 * Allocate per-cpu data:
506 */
507 ret = kmem_alloc(kernel_map,
508 (vm_offset_t *) &cdp, sizeof(cpu_data_t));
509 if (ret != KERN_SUCCESS) {
510 printf("cpu_data_alloc() failed, ret=%d\n", ret);
511 goto abort;
512 }
513 bzero((void*) cdp, sizeof(cpu_data_t));
514 cdp->cpu_this = cdp;
515
516 /* Propagate mode */
517 cdp->cpu_is64bit = cpu_mode_is64bit();
518
519 /*
520 * Allocate interrupt stack:
521 */
522 ret = kmem_alloc(kernel_map,
523 (vm_offset_t *) &cdp->cpu_int_stack_top,
524 INTSTACK_SIZE);
525 if (ret != KERN_SUCCESS) {
526 printf("cpu_data_alloc() int stack failed, ret=%d\n", ret);
527 goto abort;
528 }
529 bzero((void*) cdp->cpu_int_stack_top, INTSTACK_SIZE);
530 cdp->cpu_int_stack_top += INTSTACK_SIZE;
531
532 /*
533 * Allocate descriptor table:
534 * Size depends on cpu mode.
535 */
536 ret = kmem_alloc(kernel_map,
537 (vm_offset_t *) &cdp->cpu_desc_tablep,
538 cdp->cpu_is64bit ? sizeof(cpu_desc_table64_t)
539 : sizeof(cpu_desc_table_t));
540 if (ret != KERN_SUCCESS) {
541 printf("cpu_data_alloc() desc_table failed, ret=%d\n", ret);
542 goto abort;
543 }
544
545 /*
546 * Allocate LDT
547 */
548 ret = kmem_alloc(kernel_map,
549 (vm_offset_t *) &cdp->cpu_ldtp,
550 sizeof(struct real_descriptor) * LDTSZ);
551 if (ret != KERN_SUCCESS) {
552 printf("cpu_data_alloc() ldt failed, ret=%d\n", ret);
553 goto abort;
554 }
555
556 /* Machine-check shadow register allocation. */
557 mca_cpu_alloc(cdp);
558
559 simple_lock(&cpu_lock);
560 if (real_ncpus >= max_ncpus) {
561 simple_unlock(&cpu_lock);
562 goto abort;
563 }
564 cpu_data_ptr[real_ncpus] = cdp;
565 cdp->cpu_number = real_ncpus;
566 real_ncpus++;
567 simple_unlock(&cpu_lock);
568
569 kprintf("cpu_data_alloc(%d) %p desc_table: %p "
570 "ldt: %p "
571 "int_stack: 0x%x-0x%x\n",
572 cdp->cpu_number, cdp, cdp->cpu_desc_tablep, cdp->cpu_ldtp,
573 cdp->cpu_int_stack_top - INTSTACK_SIZE, cdp->cpu_int_stack_top);
574
575 return cdp;
576
577abort:
578 if (cdp) {
579 if (cdp->cpu_desc_tablep)
580 kfree((void *) cdp->cpu_desc_tablep,
581 sizeof(*cdp->cpu_desc_tablep));
582 if (cdp->cpu_int_stack_top)
583 kfree((void *) (cdp->cpu_int_stack_top - INTSTACK_SIZE),
584 INTSTACK_SIZE);
585 kfree((void *) cdp, sizeof(*cdp));
586 }
587 return NULL;
588}
589
590boolean_t
591valid_user_segment_selectors(uint16_t cs,
592 uint16_t ss,
593 uint16_t ds,
594 uint16_t es,
595 uint16_t fs,
596 uint16_t gs)
597{
598 return valid_user_code_selector(cs) &&
599 valid_user_stack_selector(ss) &&
600 valid_user_data_selector(ds) &&
601 valid_user_data_selector(es) &&
602 valid_user_data_selector(fs) &&
603 valid_user_data_selector(gs);
604}
605
606
607static vm_offset_t user_window_base = 0;
608
609void
610cpu_userwindow_init(int cpu)
611{
612 cpu_data_t *cdp = cpu_data_ptr[cpu];
613 cpu_desc_index_t *cdi = &cdp->cpu_desc_index;
614 vm_offset_t user_window;
615 vm_offset_t vaddr;
616 int num_cpus;
617
618 num_cpus = ml_get_max_cpus();
619
620 if (cpu >= num_cpus)
621 panic("cpu_userwindow_init: cpu > num_cpus");
622
623 if (user_window_base == 0) {
624
625 if (vm_allocate(kernel_map, &vaddr,
626 (NBPDE * NCOPY_WINDOWS * num_cpus) + NBPDE,
627 VM_FLAGS_ANYWHERE) != KERN_SUCCESS)
628 panic("cpu_userwindow_init: "
629 "couldn't allocate user map window");
630
631 /*
632 * window must start on a page table boundary
633 * in the virtual address space
634 */
635 user_window_base = (vaddr + (NBPDE - 1)) & ~(NBPDE - 1);
636
637 /*
638 * get rid of any allocation leading up to our
639 * starting boundary
640 */
641 vm_deallocate(kernel_map, vaddr, user_window_base - vaddr);
642
643 /*
644 * get rid of tail that we don't need
645 */
646 user_window = user_window_base +
647 (NBPDE * NCOPY_WINDOWS * num_cpus);
648
649 vm_deallocate(kernel_map, user_window,
650 (vaddr +
651 ((NBPDE * NCOPY_WINDOWS * num_cpus) + NBPDE)) -
652 user_window);
653 }
654
655 user_window = user_window_base + (cpu * NCOPY_WINDOWS * NBPDE);
656
657 cdp->cpu_copywindow_base = user_window;
658 cdp->cpu_copywindow_pdp = pmap_pde(kernel_pmap, user_window);
659
660 cdi->cdi_gdt[sel_idx(USER_WINDOW_SEL)] = userwindow_desc_pattern;
661 cdi->cdi_gdt[sel_idx(USER_WINDOW_SEL)].offset = user_window;
662
663 fix_desc(&cdi->cdi_gdt[sel_idx(USER_WINDOW_SEL)], 1);
664
665}
666
667void
668cpu_physwindow_init(int cpu)
669{
670 cpu_data_t *cdp = cpu_data_ptr[cpu];
671 cpu_desc_index_t *cdi = &cdp->cpu_desc_index;
672 vm_offset_t phys_window;
673
674 if (vm_allocate(kernel_map, &phys_window,
675 PAGE_SIZE, VM_FLAGS_ANYWHERE)
676 != KERN_SUCCESS)
677 panic("cpu_physwindow_init: couldn't allocate phys map window");
678
679 /*
680 * make sure the page that encompasses the
681 * pte pointer we're interested in actually
682 * exists in the page table
683 */
684 pmap_expand(kernel_pmap, phys_window);
685
686 cdp->cpu_physwindow_base = phys_window;
687 cdp->cpu_physwindow_ptep = vtopte(phys_window);
688
689 cdi->cdi_gdt[sel_idx(PHYS_WINDOW_SEL)] = physwindow_desc_pattern;
690 cdi->cdi_gdt[sel_idx(PHYS_WINDOW_SEL)].offset = phys_window;
691
692 fix_desc(&cdi->cdi_gdt[sel_idx(PHYS_WINDOW_SEL)], 1);
693}
694
695
696typedef struct {
697 uint16_t length;
698 uint32_t offset[2];
699} __attribute__((__packed__)) table_descriptor64_t;
700
701extern table_descriptor64_t gdtptr64;
702extern table_descriptor64_t idtptr64;
703/*
704 * Load the segment descriptor tables for the current processor.
705 */
706void
707cpu_desc_load64(cpu_data_t *cdp)
708{
709 cpu_desc_index_t *cdi = &cdp->cpu_desc_index;
710
711 /*
712 * Load up the new descriptors etc
713 * ml_load_desc64() expects these global pseudo-descriptors:
714 * gdtptr64 -> master_gdt
715 * idtptr64 -> master_idt64
716 * These are 10-byte descriptors with 64-bit addresses into
717 * uber-space.
718 */
719 gdtptr64.length = sizeof(master_gdt) - 1;
720 gdtptr64.offset[0] = (uint32_t) cdi->cdi_gdt;
721 gdtptr64.offset[1] = KERNEL_UBER_BASE_HI32;
722 idtptr64.length = sizeof(master_idt64) - 1;
723 idtptr64.offset[0] = (uint32_t) cdi->cdi_idt;
724 idtptr64.offset[1] = KERNEL_UBER_BASE_HI32;
725
726 /* Make sure busy bit is cleared in the TSS */
727 gdt_desc_p(KERNEL_TSS)->access &= ~ACC_TSS_BUSY;
728
729 ml_load_desc64();
730
731#if ONLY_SAFE_FOR_LINDA_SERIAL
732 kprintf("64-bit descriptor tables loaded\n");
733#endif
734}
735
736void
737cpu_mode_init(cpu_data_t *cdp)
738{
739 if (cpu_mode_is64bit()) {
740 cpu_IA32e_enable(cdp);
741 cpu_desc_load64(cdp);
742 fast_syscall_init64();
743 } else {
744 fast_syscall_init();
745 }
746
747 /* Call for per-cpu pmap mode initialization */
748 pmap_cpu_init();
749
750}
751