]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/mp_desc.c
xnu-1228.tar.gz
[apple/xnu.git] / osfmk / i386 / mp_desc.c
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 */
89 extern uint32_t low_intstack[]; /* bottom */
90 extern 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 */
97 cpu_data_t cpu_data_master;
98 cpu_data_t *cpu_data_ptr[MAX_CPUS] = { [0] &cpu_data_master };
99
100 decl_simple_lock_data(,cpu_lock); /* protects real_ncpus */
101 unsigned int real_ncpus = 1;
102 unsigned int max_ncpus = MAX_CPUS;
103
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)
107
108 extern void hi_sysenter(void);
109 extern void hi64_sysenter(void);
110 extern 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
125 struct 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
132 struct 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
139 struct 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
146 struct 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
153 struct 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 */
166 struct 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 */
178 struct 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
186 void
187 cpu_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
316 void
317 cpu_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 */
427 static void
428 fast_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 kprintf("fast_syscall_init64() KERNEL_GS_BASE=0x%016llx\n",
461 rdmsr64(MSR_IA32_KERNEL_GS_BASE));
462 }
463
464 /*
465 * Set MSRs for sysenter/sysexit
466 */
467 static void
468 fast_syscall_init(void)
469 {
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);
473 }
474
475 cpu_data_t *
476 cpu_data_alloc(boolean_t is_boot_cpu)
477 {
478 int ret;
479 cpu_data_t *cdp;
480
481 if (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);
488 cdp->cpu_this = cdp;
489 cdp->cpu_is64bit = FALSE;
490 cdp->cpu_int_stack_top = (vm_offset_t) low_eintstack;
491 cpu_desc_init(cdp, TRUE);
492 fast_syscall_init();
493 }
494 return cdp;
495 }
496
497 /* Check count before making allocations */
498 if (real_ncpus >= max_ncpus)
499 return NULL;
500
501 /*
502 * Allocate per-cpu data:
503 */
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);
508 goto abort;
509 }
510 bzero((void*) cdp, sizeof(cpu_data_t));
511 cdp->cpu_this = cdp;
512
513 /* Propagate mode */
514 cdp->cpu_is64bit = cpu_mode_is64bit();
515
516 /*
517 * Allocate interrupt stack:
518 */
519 ret = kmem_alloc(kernel_map,
520 (vm_offset_t *) &cdp->cpu_int_stack_top,
521 INTSTACK_SIZE);
522 if (ret != KERN_SUCCESS) {
523 printf("cpu_data_alloc() int stack failed, ret=%d\n", ret);
524 goto abort;
525 }
526 bzero((void*) cdp->cpu_int_stack_top, INTSTACK_SIZE);
527 cdp->cpu_int_stack_top += INTSTACK_SIZE;
528
529 /*
530 * Allocate descriptor table:
531 * Size depends on cpu mode.
532 */
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);
539 goto abort;
540 }
541
542 /*
543 * Allocate LDT
544 */
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);
550 goto abort;
551 }
552
553 /* Machine-check shadow register allocation. */
554 mca_cpu_alloc(cdp);
555
556 simple_lock(&cpu_lock);
557 if (real_ncpus >= max_ncpus) {
558 simple_unlock(&cpu_lock);
559 goto abort;
560 }
561 cpu_data_ptr[real_ncpus] = cdp;
562 cdp->cpu_number = real_ncpus;
563 real_ncpus++;
564 simple_unlock(&cpu_lock);
565
566 kprintf("cpu_data_alloc(%d) %p desc_table: %p "
567 "ldt: %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);
571
572 return cdp;
573
574 abort:
575 if (cdp) {
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),
581 INTSTACK_SIZE);
582 kfree((void *) cdp, sizeof(*cdp));
583 }
584 return NULL;
585 }
586
587 boolean_t
588 valid_user_segment_selectors(uint16_t cs,
589 uint16_t ss,
590 uint16_t ds,
591 uint16_t es,
592 uint16_t fs,
593 uint16_t gs)
594 {
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);
601 }
602
603
604 static vm_offset_t user_window_base = 0;
605
606 void
607 cpu_userwindow_init(int cpu)
608 {
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;
612 vm_offset_t vaddr;
613 int num_cpus;
614
615 num_cpus = ml_get_max_cpus();
616
617 if (cpu >= num_cpus)
618 panic("cpu_userwindow_init: cpu > num_cpus");
619
620 if (user_window_base == 0) {
621
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");
627
628 /*
629 * window must start on a page table boundary
630 * in the virtual address space
631 */
632 user_window_base = (vaddr + (NBPDE - 1)) & ~(NBPDE - 1);
633
634 /*
635 * get rid of any allocation leading up to our
636 * starting boundary
637 */
638 vm_deallocate(kernel_map, vaddr, user_window_base - vaddr);
639
640 /*
641 * get rid of tail that we don't need
642 */
643 user_window = user_window_base +
644 (NBPDE * NCOPY_WINDOWS * num_cpus);
645
646 vm_deallocate(kernel_map, user_window,
647 (vaddr +
648 ((NBPDE * NCOPY_WINDOWS * num_cpus) + NBPDE)) -
649 user_window);
650 }
651
652 user_window = user_window_base + (cpu * NCOPY_WINDOWS * NBPDE);
653
654 cdp->cpu_copywindow_base = user_window;
655 cdp->cpu_copywindow_pdp = pmap_pde(kernel_pmap, user_window);
656
657 cdi->cdi_gdt[sel_idx(USER_WINDOW_SEL)] = userwindow_desc_pattern;
658 cdi->cdi_gdt[sel_idx(USER_WINDOW_SEL)].offset = user_window;
659
660 fix_desc(&cdi->cdi_gdt[sel_idx(USER_WINDOW_SEL)], 1);
661
662 }
663
664 void
665 cpu_physwindow_init(int cpu)
666 {
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;
670
671 if (vm_allocate(kernel_map, &phys_window,
672 PAGE_SIZE, VM_FLAGS_ANYWHERE)
673 != KERN_SUCCESS)
674 panic("cpu_physwindow_init: couldn't allocate phys map window");
675
676 /*
677 * make sure the page that encompasses the
678 * pte pointer we're interested in actually
679 * exists in the page table
680 */
681 pmap_expand(kernel_pmap, phys_window);
682
683 cdp->cpu_physwindow_base = phys_window;
684 cdp->cpu_physwindow_ptep = vtopte(phys_window);
685
686 cdi->cdi_gdt[sel_idx(PHYS_WINDOW_SEL)] = physwindow_desc_pattern;
687 cdi->cdi_gdt[sel_idx(PHYS_WINDOW_SEL)].offset = phys_window;
688
689 fix_desc(&cdi->cdi_gdt[sel_idx(PHYS_WINDOW_SEL)], 1);
690 }
691
692
693 typedef struct {
694 uint16_t length;
695 uint32_t offset[2];
696 } __attribute__((__packed__)) table_descriptor64_t;
697
698 extern table_descriptor64_t gdtptr64;
699 extern table_descriptor64_t idtptr64;
700 /*
701 * Load the segment descriptor tables for the current processor.
702 */
703 void
704 cpu_desc_load64(cpu_data_t *cdp)
705 {
706 cpu_desc_index_t *cdi = &cdp->cpu_desc_index;
707
708 /*
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
714 * uber-space.
715 */
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;
722
723 /* Make sure busy bit is cleared in the TSS */
724 gdt_desc_p(KERNEL_TSS)->access &= ~ACC_TSS_BUSY;
725
726 ml_load_desc64();
727
728 kprintf("64-bit descriptor tables loaded\n");
729 }
730
731 void
732 cpu_mode_init(cpu_data_t *cdp)
733 {
734 if (cpu_mode_is64bit()) {
735 cpu_IA32e_enable(cdp);
736 cpu_desc_load64(cdp);
737 fast_syscall_init64();
738 } else {
739 fast_syscall_init();
740 }
741
742 /* Call for per-cpu pmap mode initialization */
743 pmap_cpu_init();
744
745 }
746