]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/mp_desc.c
xnu-792.10.96.tar.gz
[apple/xnu.git] / osfmk / i386 / mp_desc.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /*
23 * @OSF_COPYRIGHT@
24 */
25 /*
26 * Mach Operating System
27 * Copyright (c) 1991,1990 Carnegie Mellon University
28 * All Rights Reserved.
29 *
30 * Permission to use, copy, modify and distribute this software and its
31 * documentation is hereby granted, provided that both the copyright
32 * notice and this permission notice appear in all copies of the
33 * software, derivative works or modified versions, and any portions
34 * thereof, and that both notices appear in supporting documentation.
35 *
36 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
37 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
38 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
39 *
40 * Carnegie Mellon requests users of this software to return to
41 *
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
46 *
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
49 */
50
51 /*
52 */
53
54
55 #include <kern/cpu_number.h>
56 #include <kern/kalloc.h>
57 #include <kern/cpu_data.h>
58 #include <mach/mach_types.h>
59 #include <mach/machine.h>
60 #include <mach/vm_map.h>
61 #include <vm/vm_kern.h>
62 #include <vm/vm_map.h>
63
64 #include <i386/mp_desc.h>
65 #include <i386/lock.h>
66 #include <i386/misc_protos.h>
67 #include <i386/mp.h>
68 #include <i386/pmap.h>
69 #include <i386/cpu_threads.h>
70
71 #include <kern/misc_protos.h>
72
73 #include <mach_kdb.h>
74
75 /*
76 * The i386 needs an interrupt stack to keep the PCB stack from being
77 * overrun by interrupts. All interrupt stacks MUST lie at lower addresses
78 * than any thread`s kernel stack.
79 */
80
81 /*
82 * First cpu`s interrupt stack.
83 */
84 extern uint32_t low_intstack[]; /* bottom */
85 extern uint32_t low_eintstack[]; /* top */
86
87 /*
88 * Per-cpu data area pointers.
89 * The master cpu (cpu 0) has its data area statically allocated;
90 * others are allocated dynamically and this array is updated at runtime.
91 */
92 cpu_data_t cpu_data_master;
93 cpu_data_t *cpu_data_ptr[MAX_CPUS] = { [0] &cpu_data_master };
94
95 decl_simple_lock_data(,cpu_lock); /* protects real_ncpus */
96 unsigned int real_ncpus = 1;
97 unsigned int max_ncpus = MAX_CPUS;
98
99 extern void *hi_remap_text;
100 #define HI_TEXT(lo_text) \
101 (((uint32_t)&lo_text - (uint32_t)&hi_remap_text) + HIGH_MEM_BASE)
102
103 extern void hi_sysenter(void);
104 extern void hi64_sysenter(void);
105 extern void hi64_syscall(void);
106
107
108 /*
109 * Multiprocessor i386/i486 systems use a separate copy of the
110 * GDT, IDT, LDT, and kernel TSS per processor. The first three
111 * are separate to avoid lock contention: the i386 uses locked
112 * memory cycles to access the descriptor tables. The TSS is
113 * separate since each processor needs its own kernel stack,
114 * and since using a TSS marks it busy.
115 */
116
117 /*
118 * Allocate and initialize the per-processor descriptor tables.
119 */
120
121 struct fake_descriptor ldt_desc_pattern = {
122 (unsigned int) 0,
123 LDTSZ_MIN * sizeof(struct fake_descriptor) - 1,
124 0,
125 ACC_P|ACC_PL_K|ACC_LDT
126 };
127
128 struct fake_descriptor tss_desc_pattern = {
129 (unsigned int) 0,
130 sizeof(struct i386_tss) - 1,
131 0,
132 ACC_P|ACC_PL_K|ACC_TSS
133 };
134
135 struct fake_descriptor cpudata_desc_pattern = {
136 (unsigned int) 0,
137 sizeof(cpu_data_t)-1,
138 SZ_32,
139 ACC_P|ACC_PL_K|ACC_DATA_W
140 };
141
142 struct fake_descriptor userwindow_desc_pattern = {
143 (unsigned int) 0,
144 ((NBPDE * NCOPY_WINDOWS) / PAGE_SIZE) - 1,
145 SZ_32 | SZ_G,
146 ACC_P|ACC_PL_U|ACC_DATA_W
147 };
148
149 struct fake_descriptor physwindow_desc_pattern = {
150 (unsigned int) 0,
151 PAGE_SIZE - 1,
152 SZ_32,
153 ACC_P|ACC_PL_K|ACC_DATA_W
154 };
155
156 /*
157 * This is the expanded, 64-bit variant of the kernel LDT descriptor.
158 * When switching to 64-bit mode this replaces KERNEL_LDT entry
159 * and the following empty slot. This enables the LDT to be referenced
160 * in the uber-space remapping window on the kernel.
161 */
162 struct fake_descriptor64 kernel_ldt_desc64 = {
163 FAKE_UBER64(&master_ldt),
164 LDTSZ_MIN*sizeof(struct fake_descriptor)-1,
165 0,
166 ACC_P|ACC_PL_K|ACC_LDT,
167 0
168 };
169
170 /*
171 * This is the expanded, 64-bit variant of the kernel TSS descriptor.
172 * It is follows pattern of the KERNEL_LDT.
173 */
174 struct fake_descriptor64 kernel_tss_desc64 = {
175 FAKE_UBER64(&master_ktss64),
176 sizeof(struct x86_64_tss)-1,
177 0,
178 ACC_P|ACC_PL_K|ACC_TSS,
179 0
180 };
181
182 void
183 cpu_desc_init(
184 cpu_data_t *cdp,
185 boolean_t is_boot_cpu)
186 {
187 cpu_desc_table_t *cdt = cdp->cpu_desc_tablep;
188 cpu_desc_index_t *cdi = &cdp->cpu_desc_index;
189
190 if (is_boot_cpu) {
191 /*
192 * Master CPU uses the tables built at boot time.
193 * Just set the index pointers to the high shared-mapping space.
194 * Note that the sysenter stack uses empty space above the ktss
195 * in the HIGH_FIXED_KTSS page. In this case we don't map the
196 * the real master_sstk in low memory.
197 */
198 cdi->cdi_ktss = (struct i386_tss *)
199 pmap_index_to_virt(HIGH_FIXED_KTSS) ;
200 cdi->cdi_sstk = (vm_offset_t) (cdi->cdi_ktss + 1) +
201 (vm_offset_t) &master_sstk.top -
202 (vm_offset_t) &master_sstk;
203 #if MACH_KDB
204 cdi->cdi_dbtss = (struct i386_tss *)
205 pmap_index_to_virt(HIGH_FIXED_DBTSS);
206 #endif /* MACH_KDB */
207 cdi->cdi_gdt = (struct fake_descriptor *)
208 pmap_index_to_virt(HIGH_FIXED_GDT);
209 cdi->cdi_idt = (struct fake_descriptor *)
210 pmap_index_to_virt(HIGH_FIXED_IDT);
211 cdi->cdi_ldt = (struct fake_descriptor *)
212 pmap_index_to_virt(HIGH_FIXED_LDT_BEGIN);
213
214 } else {
215
216 vm_offset_t cpu_hi_desc;
217
218 cpu_hi_desc = pmap_cpu_high_shared_remap(cdp->cpu_number,
219 HIGH_CPU_DESC,
220 (vm_offset_t) cdt, 1);
221
222 /*
223 * Per-cpu GDT, IDT, LDT, KTSS descriptors are allocated in one
224 * block (cpu_desc_table) and double-mapped into high shared space
225 * in one page window.
226 * Also, a transient stack for the fast sysenter path. The top of
227 * which is set at context switch time to point to the PCB using
228 * the high address.
229 */
230 cdi->cdi_gdt = (struct fake_descriptor *) (cpu_hi_desc +
231 offsetof(cpu_desc_table_t, gdt[0]));
232 cdi->cdi_idt = (struct fake_descriptor *) (cpu_hi_desc +
233 offsetof(cpu_desc_table_t, idt[0]));
234 cdi->cdi_ktss = (struct i386_tss *) (cpu_hi_desc +
235 offsetof(cpu_desc_table_t, ktss));
236 cdi->cdi_sstk = cpu_hi_desc +
237 offsetof(cpu_desc_table_t, sstk.top);
238
239 /*
240 * LDT descriptors are mapped into a seperate area.
241 */
242 cdi->cdi_ldt = (struct fake_descriptor *)
243 pmap_cpu_high_shared_remap(
244 cdp->cpu_number,
245 HIGH_CPU_LDT_BEGIN,
246 (vm_offset_t) cdp->cpu_ldtp,
247 HIGH_CPU_LDT_END - HIGH_CPU_LDT_BEGIN + 1);
248
249 /*
250 * Copy the tables
251 */
252 bcopy((char *)master_idt,
253 (char *)cdt->idt,
254 sizeof(master_idt));
255 bcopy((char *)master_gdt,
256 (char *)cdt->gdt,
257 sizeof(master_gdt));
258 bcopy((char *)master_ldt,
259 (char *)cdp->cpu_ldtp,
260 sizeof(master_ldt));
261 bzero((char *)&cdt->ktss,
262 sizeof(struct i386_tss));
263
264 #if MACH_KDB
265 cdi->cdi_dbtss = (struct i386_tss *) (cpu_hi_desc +
266 offsetof(cpu_desc_table_t, dbtss));
267 bcopy((char *)&master_dbtss,
268 (char *)&cdt->dbtss,
269 sizeof(struct i386_tss));
270 #endif /* MACH_KDB */
271
272 /*
273 * Fix up the entries in the GDT to point to
274 * this LDT and this TSS.
275 */
276 cdt->gdt[sel_idx(KERNEL_LDT)] = ldt_desc_pattern;
277 cdt->gdt[sel_idx(KERNEL_LDT)].offset = (vm_offset_t) cdi->cdi_ldt;
278 fix_desc(&cdt->gdt[sel_idx(KERNEL_LDT)], 1);
279
280 cdt->gdt[sel_idx(USER_LDT)] = ldt_desc_pattern;
281 cdt->gdt[sel_idx(USER_LDT)].offset = (vm_offset_t) cdi->cdi_ldt;
282 fix_desc(&cdt->gdt[sel_idx(USER_LDT)], 1);
283
284 cdt->gdt[sel_idx(KERNEL_TSS)] = tss_desc_pattern;
285 cdt->gdt[sel_idx(KERNEL_TSS)].offset = (vm_offset_t) cdi->cdi_ktss;
286 fix_desc(&cdt->gdt[sel_idx(KERNEL_TSS)], 1);
287
288 cdt->gdt[sel_idx(CPU_DATA_GS)] = cpudata_desc_pattern;
289 cdt->gdt[sel_idx(CPU_DATA_GS)].offset = (vm_offset_t) cdp;
290 fix_desc(&cdt->gdt[sel_idx(CPU_DATA_GS)], 1);
291
292 #if MACH_KDB
293 cdt->gdt[sel_idx(DEBUG_TSS)] = tss_desc_pattern;
294 cdt->gdt[sel_idx(DEBUG_TSS)].offset = (vm_offset_t) cdi->cdi_dbtss;
295 fix_desc(&cdt->gdt[sel_idx(DEBUG_TSS)], 1);
296
297 cdt->dbtss.esp0 = (int)(db_task_stack_store +
298 (INTSTACK_SIZE * (cdp->cpu_number)) - sizeof (natural_t));
299 cdt->dbtss.esp = cdt->dbtss.esp0;
300 cdt->dbtss.eip = (int)&db_task_start;
301 #endif /* MACH_KDB */
302
303 cdt->ktss.ss0 = KERNEL_DS;
304 cdt->ktss.io_bit_map_offset = 0x0FFF; /* no IO bitmap */
305
306 cpu_window_init(cdp->cpu_number);
307
308 }
309
310 }
311
312 void
313 cpu_desc_init64(
314 cpu_data_t *cdp,
315 boolean_t is_boot_cpu)
316 {
317 cpu_desc_table64_t *cdt = (cpu_desc_table64_t *)
318 cdp->cpu_desc_tablep;
319 cpu_desc_index_t *cdi = &cdp->cpu_desc_index;
320
321 if (is_boot_cpu) {
322 /*
323 * Master CPU uses the tables built at boot time.
324 * Just set the index pointers to the low memory space.
325 * Note that in 64-bit mode these are addressed in the
326 * double-mapped window (uber-space).
327 */
328 cdi->cdi_ktss = (struct i386_tss *) &master_ktss64;
329 cdi->cdi_sstk = (vm_offset_t) &master_sstk.top;
330 cdi->cdi_gdt = master_gdt;
331 cdi->cdi_idt = (struct fake_descriptor *) &master_idt64;
332 cdi->cdi_ldt = (struct fake_descriptor *) &master_ldt;
333
334 /* Replace the expanded LDT and TSS slots in the GDT: */
335 *(struct fake_descriptor64 *) &master_gdt[sel_idx(KERNEL_LDT)] =
336 kernel_ldt_desc64;
337 *(struct fake_descriptor64 *) &master_gdt[sel_idx(KERNEL_TSS)] =
338 kernel_tss_desc64;
339
340 /*
341 * Fix up the expanded descriptors for 64-bit.
342 */
343 fix_desc64((void *) &master_idt64, IDTSZ);
344 fix_desc64((void *) &master_gdt[sel_idx(KERNEL_LDT)], 1);
345 fix_desc64((void *) &master_gdt[sel_idx(KERNEL_TSS)], 1);
346
347 /*
348 * Set the double-fault stack as IST1 in the 64-bit TSS
349 */
350 master_ktss64.ist1 = UBER64(df_task_stack_end);
351
352 } else {
353 /*
354 * Per-cpu GDT, IDT, KTSS descriptors are allocated in kernel
355 * heap (cpu_desc_table) and double-mapped in uber-space (over 4GB).
356 * LDT descriptors are mapped into a separate area.
357 */
358 cdi->cdi_gdt = cdt->gdt;
359 cdi->cdi_idt = (struct fake_descriptor *) cdt->idt;
360 cdi->cdi_ktss = (struct i386_tss *) &cdt->ktss;
361 cdi->cdi_sstk = (vm_offset_t) &cdt->sstk.top;
362 cdi->cdi_ldt = cdp->cpu_ldtp;
363
364 /*
365 * Copy the tables
366 */
367 bcopy((char *)master_idt64,
368 (char *)cdt->idt,
369 sizeof(master_idt64));
370 bcopy((char *)master_gdt,
371 (char *)cdt->gdt,
372 sizeof(master_gdt));
373 bcopy((char *)master_ldt,
374 (char *)cdp->cpu_ldtp,
375 sizeof(master_ldt));
376 bcopy((char *)&master_ktss64,
377 (char *)&cdt->ktss,
378 sizeof(struct x86_64_tss));
379
380 /*
381 * Fix up the entries in the GDT to point to
382 * this LDT and this TSS.
383 */
384 kernel_ldt_desc64.offset[0] = (vm_offset_t) cdi->cdi_ldt;
385 *(struct fake_descriptor64 *) &cdt->gdt[sel_idx(KERNEL_LDT)] =
386 kernel_ldt_desc64;
387 fix_desc64(&cdt->gdt[sel_idx(KERNEL_LDT)], 1);
388
389 kernel_ldt_desc64.offset[0] = (vm_offset_t) cdi->cdi_ldt;
390 *(struct fake_descriptor64 *) &cdt->gdt[sel_idx(USER_LDT)] =
391 kernel_ldt_desc64;
392 fix_desc64(&cdt->gdt[sel_idx(USER_LDT)], 1);
393
394 kernel_tss_desc64.offset[0] = (vm_offset_t) cdi->cdi_ktss;
395 *(struct fake_descriptor64 *) &cdt->gdt[sel_idx(KERNEL_TSS)] =
396 kernel_tss_desc64;
397 fix_desc64(&cdt->gdt[sel_idx(KERNEL_TSS)], 1);
398
399 cdt->gdt[sel_idx(CPU_DATA_GS)] = cpudata_desc_pattern;
400 cdt->gdt[sel_idx(CPU_DATA_GS)].offset = (vm_offset_t) cdp;
401 fix_desc(&cdt->gdt[sel_idx(CPU_DATA_GS)], 1);
402
403 /* Set double-fault stack as IST1 */
404 cdt->ktss.ist1 = UBER64(cdt->dfstk + sizeof(cdt->dfstk));
405
406 /*
407 * Allocate copyio windows.
408 */
409 cpu_window_init(cdp->cpu_number);
410
411 }
412
413 /* Require that the top of the sysenter stack is 16-byte aligned */
414 if ((cdi->cdi_sstk % 16) != 0)
415 panic("cpu_desc_init64() sysenter stack not 16-byte aligned");
416 }
417
418 /*
419 * Set MSRs for sysenter/sysexit for 64-bit.
420 */
421 void
422 fast_syscall_init64(void)
423 {
424 wrmsr64(MSR_IA32_SYSENTER_CS, SYSENTER_CS);
425 wrmsr64(MSR_IA32_SYSENTER_EIP, UBER64(hi64_sysenter));
426 wrmsr64(MSR_IA32_SYSENTER_ESP, UBER64(current_sstk()));
427
428 /* Enable syscall/sysret */
429 wrmsr64(MSR_IA32_EFER, rdmsr64(MSR_IA32_EFER) | MSR_IA32_EFER_SCE);
430
431 /*
432 * MSRs for 64-bit syscall/sysret
433 * Note USER_CS because sysret uses this + 16 when returning to
434 * 64-bit code.
435 */
436 wrmsr64(MSR_IA32_LSTAR, UBER64(hi64_syscall));
437 wrmsr64(MSR_IA32_STAR, (((uint64_t)USER_CS) << 48) |
438 (((uint64_t)KERNEL64_CS) << 32));
439 /*
440 * Emulate eflags cleared by sysenter but note that
441 * we also clear the trace trap to avoid the complications
442 * of single-stepping into a syscall. We also clear
443 * the nested task bit to avoid a spurious "task switch"
444 * on IRET.
445 */
446 wrmsr64(MSR_IA32_FMASK, EFL_DF|EFL_IF|EFL_TF|EFL_NT);
447
448 /*
449 * Set the Kermel GS base MSR to point to per-cpu data in uber-space.
450 * The uber-space handler (hi64_syscall) uses the swapgs instruction.
451 */
452 wrmsr64(MSR_IA32_KERNEL_GS_BASE, UBER64(current_cpu_datap()));
453 kprintf("fast_syscall_init64() KERNEL_GS_BASE=0x%016llx\n",
454 rdmsr64(MSR_IA32_KERNEL_GS_BASE));
455 }
456
457 /*
458 * Set MSRs for sysenter/sysexit
459 */
460 void
461 fast_syscall_init(void)
462 {
463 wrmsr(MSR_IA32_SYSENTER_CS, SYSENTER_CS, 0);
464 wrmsr(MSR_IA32_SYSENTER_EIP, HI_TEXT(hi_sysenter), 0);
465 wrmsr(MSR_IA32_SYSENTER_ESP, current_sstk(), 0);
466 }
467
468 cpu_data_t *
469 cpu_data_alloc(boolean_t is_boot_cpu)
470 {
471 int ret;
472 cpu_data_t *cdp;
473
474 if (is_boot_cpu) {
475 assert(real_ncpus == 1);
476 simple_lock_init(&cpu_lock, 0);
477 cdp = &cpu_data_master;
478 if (cdp->cpu_processor == NULL) {
479 cdp->cpu_processor = cpu_processor_alloc(TRUE);
480 cdp->cpu_pmap = pmap_cpu_alloc(TRUE);
481 cdp->cpu_this = cdp;
482 cdp->cpu_is64bit = FALSE;
483 cdp->cpu_int_stack_top = (vm_offset_t) low_eintstack;
484 cpu_desc_init(cdp, TRUE);
485 fast_syscall_init();
486 }
487 return cdp;
488 }
489
490 /* Check count before making allocations */
491 if (real_ncpus >= max_ncpus)
492 return NULL;
493
494 /*
495 * Allocate per-cpu data:
496 */
497 ret = kmem_alloc(kernel_map,
498 (vm_offset_t *) &cdp, sizeof(cpu_data_t));
499 if (ret != KERN_SUCCESS) {
500 printf("cpu_data_alloc() failed, ret=%d\n", ret);
501 goto abort;
502 }
503 bzero((void*) cdp, sizeof(cpu_data_t));
504 cdp->cpu_this = cdp;
505
506 /* Propagate mode */
507 cdp->cpu_is64bit = cpu_mode_is64bit();
508
509 /*
510 * Allocate interrupt stack:
511 */
512 ret = kmem_alloc(kernel_map,
513 (vm_offset_t *) &cdp->cpu_int_stack_top,
514 INTSTACK_SIZE);
515 if (ret != KERN_SUCCESS) {
516 printf("cpu_data_alloc() int stack failed, ret=%d\n", ret);
517 goto abort;
518 }
519 bzero((void*) cdp->cpu_int_stack_top, INTSTACK_SIZE);
520 cdp->cpu_int_stack_top += INTSTACK_SIZE;
521
522 /*
523 * Allocate descriptor table:
524 * Size depends on cpu mode.
525 */
526 ret = kmem_alloc(kernel_map,
527 (vm_offset_t *) &cdp->cpu_desc_tablep,
528 cdp->cpu_is64bit ? sizeof(cpu_desc_table64_t)
529 : sizeof(cpu_desc_table_t));
530 if (ret != KERN_SUCCESS) {
531 printf("cpu_data_alloc() desc_table failed, ret=%d\n", ret);
532 goto abort;
533 }
534
535 /*
536 * Allocate LDT
537 */
538 ret = kmem_alloc(kernel_map,
539 (vm_offset_t *) &cdp->cpu_ldtp,
540 sizeof(struct real_descriptor) * LDTSZ);
541 if (ret != KERN_SUCCESS) {
542 printf("cpu_data_alloc() ldt failed, ret=%d\n", ret);
543 goto abort;
544 }
545
546 simple_lock(&cpu_lock);
547 if (real_ncpus >= max_ncpus) {
548 simple_unlock(&cpu_lock);
549 goto abort;
550 }
551 cpu_data_ptr[real_ncpus] = cdp;
552 cdp->cpu_number = real_ncpus;
553 real_ncpus++;
554 simple_unlock(&cpu_lock);
555
556 kprintf("cpu_data_alloc(%d) 0x%x desc_table: 0x%x "
557 "ldt: 0x%x "
558 "int_stack: 0x%x-0x%x\n",
559 cdp->cpu_number, cdp, cdp->cpu_desc_tablep, cdp->cpu_ldtp,
560 cdp->cpu_int_stack_top - INTSTACK_SIZE, cdp->cpu_int_stack_top);
561
562 return cdp;
563
564 abort:
565 if (cdp) {
566 if (cdp->cpu_desc_tablep)
567 kfree((void *) cdp->cpu_desc_tablep,
568 sizeof(*cdp->cpu_desc_tablep));
569 if (cdp->cpu_int_stack_top)
570 kfree((void *) (cdp->cpu_int_stack_top - INTSTACK_SIZE),
571 INTSTACK_SIZE);
572 kfree((void *) cdp, sizeof(*cdp));
573 }
574 return NULL;
575 }
576
577 boolean_t
578 valid_user_segment_selectors(uint16_t cs,
579 uint16_t ss,
580 uint16_t ds,
581 uint16_t es,
582 uint16_t fs,
583 uint16_t gs)
584 {
585 return valid_user_code_selector(cs) &&
586 valid_user_stack_selector(ss) &&
587 valid_user_data_selector(ds) &&
588 valid_user_data_selector(es) &&
589 valid_user_data_selector(fs) &&
590 valid_user_data_selector(gs);
591 }
592
593
594 static vm_offset_t user_window_base = 0;
595 static vm_offset_t phys_window_base = 0;
596
597 void
598 cpu_window_init(int cpu)
599 {
600 cpu_data_t *cdp = cpu_data_ptr[cpu];
601 cpu_desc_index_t *cdi;
602 vm_offset_t user_window;
603 vm_offset_t phys_window;
604 vm_offset_t vaddr;
605 int num_cpus;
606
607 num_cpus = ml_get_max_cpus();
608
609 if (cpu >= num_cpus)
610 panic("copy_window_init: cpu > num_cpus");
611
612 if (user_window_base == 0) {
613
614 if (vm_allocate(kernel_map, &vaddr,
615 (NBPDE * NCOPY_WINDOWS * num_cpus) + NBPDE,
616 VM_FLAGS_ANYWHERE) != KERN_SUCCESS)
617 panic("copy_window_init: "
618 "couldn't allocate user map window");
619
620 /*
621 * window must start on a page table boundary
622 * in the virtual address space
623 */
624 user_window_base = (vaddr + (NBPDE - 1)) & ~(NBPDE - 1);
625
626 /*
627 * get rid of any allocation leading up to our
628 * starting boundary
629 */
630 vm_deallocate(kernel_map, vaddr, user_window_base - vaddr);
631
632 /*
633 * get rid of tail that we don't need
634 */
635 user_window = user_window_base +
636 (NBPDE * NCOPY_WINDOWS * num_cpus);
637
638 vm_deallocate(kernel_map, user_window,
639 (vaddr +
640 ((NBPDE * NCOPY_WINDOWS * num_cpus) + NBPDE)) -
641 user_window);
642
643 if (vm_allocate(kernel_map, &phys_window_base,
644 PAGE_SIZE * num_cpus, VM_FLAGS_ANYWHERE)
645 != KERN_SUCCESS)
646 panic("copy_window_init: "
647 "couldn't allocate phys map window");
648 }
649
650 user_window = user_window_base + (cpu * NCOPY_WINDOWS * NBPDE);
651 phys_window = phys_window_base + (cpu * PAGE_SIZE);
652
653 cdi = &cdp->cpu_desc_index;
654
655 cdp->cpu_copywindow_base = user_window;
656 cdp->cpu_copywindow_pdp = pmap_pde(kernel_pmap, user_window);
657
658 cdi->cdi_gdt[sel_idx(USER_WINDOW_SEL)] = userwindow_desc_pattern;
659 cdi->cdi_gdt[sel_idx(USER_WINDOW_SEL)].offset = user_window;
660
661 fix_desc(&cdi->cdi_gdt[sel_idx(USER_WINDOW_SEL)], 1);
662
663 cdp->cpu_physwindow_base = phys_window;
664 cdp->cpu_physwindow_ptep = vtopte(phys_window);
665
666 cdi->cdi_gdt[sel_idx(PHYS_WINDOW_SEL)] = physwindow_desc_pattern;
667 cdi->cdi_gdt[sel_idx(PHYS_WINDOW_SEL)].offset = phys_window;
668
669 fix_desc(&cdi->cdi_gdt[sel_idx(PHYS_WINDOW_SEL)], 1);
670
671 }
672
673
674 typedef struct {
675 uint16_t length;
676 uint32_t offset[2];
677 } __attribute__((__packed__)) table_descriptor64_t;
678
679 extern table_descriptor64_t gdtptr64;
680 extern table_descriptor64_t idtptr64;
681 /*
682 * Load the segment descriptor tables for the current processor.
683 */
684 void
685 cpu_desc_load64(cpu_data_t *cdp)
686 {
687 cpu_desc_index_t *cdi = &cdp->cpu_desc_index;
688
689 /*
690 * Load up the new descriptors etc
691 * ml_load_desc64() expects these global pseudo-descriptors:
692 * gdtptr64 -> master_gdt
693 * idtptr64 -> master_idt64
694 * These are 10-byte descriptors with 64-bit addresses into
695 * uber-space.
696 */
697 gdtptr64.length = sizeof(master_gdt) - 1;
698 gdtptr64.offset[0] = (uint32_t) cdi->cdi_gdt;
699 gdtptr64.offset[1] = KERNEL_UBER_BASE_HI32;
700 idtptr64.length = sizeof(master_idt64) - 1;
701 idtptr64.offset[0] = (uint32_t) cdi->cdi_idt;
702 idtptr64.offset[1] = KERNEL_UBER_BASE_HI32;
703
704 /* Make sure busy bit is cleared in the TSS */
705 gdt_desc_p(KERNEL_TSS)->access &= ~ACC_TSS_BUSY;
706
707 ml_load_desc64();
708
709 kprintf("64-bit descriptor tables loaded\n");
710 }