]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/mp_desc.c
709e2b4b34c9455b6823cff1f38abb67f6d6595b
[apple/xnu.git] / osfmk / i386 / mp_desc.c
1 /*
2 * Copyright (c) 2000-2009 Apple 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 <mach/machine/vm_param.h>
67 #include <vm/vm_kern.h>
68 #include <vm/vm_map.h>
69
70 #include <i386/lock.h>
71 #include <i386/mp_desc.h>
72 #include <i386/misc_protos.h>
73 #include <i386/mp.h>
74 #include <i386/pmap.h>
75 #if CONFIG_MCA
76 #include <i386/machine_check.h>
77 #endif
78
79 #include <kern/misc_protos.h>
80
81 #include <mach_kdb.h>
82
83
84 #ifdef __x86_64__
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)
87
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 ;
96
97 // Include the table to declare the externs
98 #include "../x86_64/idt_table.h"
99
100 // Undef the macros, then redefine them so we can declare the table
101 #undef TRAP
102 #undef TRAP_ERR
103 #undef TRAP_SPC
104 #undef TRAP_IST
105 #undef INTERRUPT
106 #undef USER_TRAP
107 #undef USER_TRAP_SPC
108
109 #define TRAP(n, name) \
110 [n] { \
111 (uintptr_t)&name, \
112 KERNEL64_CS, \
113 0, \
114 K_INTR_GATE, \
115 0 \
116 },
117
118 #define TRAP_ERR TRAP
119 #define TRAP_SPC TRAP
120
121 #define TRAP_IST(n, name) \
122 [n] { \
123 (uintptr_t)&name, \
124 KERNEL64_CS, \
125 1, \
126 K_INTR_GATE, \
127 0 \
128 },
129
130 #define INTERRUPT(n) \
131 [n] { \
132 (uintptr_t)&_intr_ ## n,\
133 KERNEL64_CS, \
134 0, \
135 K_INTR_GATE, \
136 0 \
137 },
138
139 #define USER_TRAP(n, name) \
140 [n] { \
141 (uintptr_t)&name, \
142 KERNEL64_CS, \
143 0, \
144 U_INTR_GATE, \
145 0 \
146 },
147
148 #define USER_TRAP_SPC USER_TRAP
149
150
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"
154 };
155 #endif
156
157 /*
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.
161 */
162
163 /*
164 * First cpu`s interrupt stack.
165 */
166 extern uint32_t low_intstack[]; /* bottom */
167 extern uint32_t low_eintstack[]; /* top */
168
169 /*
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.
173 */
174 cpu_data_t cpu_data_master = {
175 .cpu_this = &cpu_data_master,
176 .cpu_nanotime = &rtc_nanotime_info,
177 .cpu_int_stack_top = (vm_offset_t) low_eintstack,
178 #ifdef __i386__
179 .cpu_is64bit = FALSE,
180 #else
181 .cpu_is64bit = TRUE
182 #endif
183 };
184 cpu_data_t *cpu_data_ptr[MAX_CPUS] = { [0] &cpu_data_master };
185
186 decl_simple_lock_data(,ncpus_lock); /* protects real_ncpus */
187 unsigned int real_ncpus = 1;
188 unsigned int max_ncpus = MAX_CPUS;
189
190 #ifdef __i386__
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)
194
195 extern void hi_sysenter(void);
196
197 typedef struct {
198 uint16_t length;
199 uint32_t offset[2];
200 } __attribute__((__packed__)) table_descriptor64_t;
201
202 extern table_descriptor64_t gdtptr64;
203 extern table_descriptor64_t idtptr64;
204 #endif
205 extern void hi64_sysenter(void);
206 extern void hi64_syscall(void);
207
208 #if defined(__x86_64__) && !defined(UBER64)
209 #define UBER64(x) ((uintptr_t)x)
210 #endif
211
212 /*
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.
219 */
220
221 /*
222 * Allocate and initialize the per-processor descriptor tables.
223 */
224
225 struct fake_descriptor ldt_desc_pattern = {
226 (unsigned int) 0,
227 LDTSZ_MIN * sizeof(struct fake_descriptor) - 1,
228 0,
229 ACC_P|ACC_PL_K|ACC_LDT
230 };
231
232 struct fake_descriptor tss_desc_pattern = {
233 (unsigned int) 0,
234 sizeof(struct i386_tss) - 1,
235 0,
236 ACC_P|ACC_PL_K|ACC_TSS
237 };
238
239 struct fake_descriptor cpudata_desc_pattern = {
240 (unsigned int) 0,
241 sizeof(cpu_data_t)-1,
242 SZ_32,
243 ACC_P|ACC_PL_K|ACC_DATA_W
244 };
245
246 struct fake_descriptor userwindow_desc_pattern = {
247 (unsigned int) 0,
248 ((NBPDE * NCOPY_WINDOWS) / PAGE_SIZE) - 1,
249 SZ_32 | SZ_G,
250 ACC_P|ACC_PL_U|ACC_DATA_W
251 };
252
253 struct fake_descriptor physwindow_desc_pattern = {
254 (unsigned int) 0,
255 PAGE_SIZE - 1,
256 SZ_32,
257 ACC_P|ACC_PL_K|ACC_DATA_W
258 };
259
260 /*
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.
265 */
266 struct fake_descriptor64 kernel_ldt_desc64 = {
267 0,
268 LDTSZ_MIN*sizeof(struct fake_descriptor)-1,
269 0,
270 ACC_P|ACC_PL_K|ACC_LDT,
271 0
272 };
273
274 /*
275 * This is the expanded, 64-bit variant of the kernel TSS descriptor.
276 * It is follows pattern of the KERNEL_LDT.
277 */
278 struct fake_descriptor64 kernel_tss_desc64 = {
279 0,
280 sizeof(struct x86_64_tss)-1,
281 #ifdef __x86_64__
282 SZ_G,
283 #else
284 0,
285 #endif
286 ACC_P|ACC_PL_K|ACC_TSS,
287 0
288 };
289
290 /*
291 * Convert a descriptor from fake to real format.
292 *
293 * Fake descriptor format:
294 * bytes 0..3 base 31..0
295 * bytes 4..5 limit 15..0
296 * byte 6 access byte 2 | limit 19..16
297 * byte 7 access byte 1
298 *
299 * Real descriptor format:
300 * bytes 0..1 limit 15..0
301 * bytes 2..3 base 15..0
302 * byte 4 base 23..16
303 * byte 5 access byte 1
304 * byte 6 access byte 2 | limit 19..16
305 * byte 7 base 31..24
306 *
307 * Fake gate format:
308 * bytes 0..3 offset
309 * bytes 4..5 selector
310 * byte 6 word count << 4 (to match fake descriptor)
311 * byte 7 access byte 1
312 *
313 * Real gate format:
314 * bytes 0..1 offset 15..0
315 * bytes 2..3 selector
316 * byte 4 word count
317 * byte 5 access byte 1
318 * bytes 6..7 offset 31..16
319 */
320 void
321 fix_desc(void *d, int num_desc) {
322 //early_kprintf("fix_desc(%x, %x)\n", d, num_desc);
323 uint8_t *desc = (uint8_t*) d;
324
325 do {
326 if ((desc[7] & 0x14) == 0x04) { /* gate */
327 uint32_t offset;
328 uint16_t selector;
329 uint8_t wordcount;
330 uint8_t acc;
331
332 offset = *((uint32_t*)(desc));
333 selector = *((uint32_t*)(desc+4));
334 wordcount = desc[6] >> 4;
335 acc = desc[7];
336
337 *((uint16_t*)desc) = offset & 0xFFFF;
338 *((uint16_t*)(desc+2)) = selector;
339 desc[4] = wordcount;
340 desc[5] = acc;
341 *((uint16_t*)(desc+6)) = offset >> 16;
342
343 } else { /* descriptor */
344 uint32_t base;
345 uint16_t limit;
346 uint8_t acc1, acc2;
347
348 base = *((uint32_t*)(desc));
349 limit = *((uint16_t*)(desc+4));
350 acc2 = desc[6];
351 acc1 = desc[7];
352
353 *((uint16_t*)(desc)) = limit;
354 *((uint16_t*)(desc+2)) = base & 0xFFFF;
355 desc[4] = (base >> 16) & 0xFF;
356 desc[5] = acc1;
357 desc[6] = acc2;
358 desc[7] = base >> 24;
359 }
360 desc += 8;
361 } while (--num_desc);
362 }
363
364 void
365 fix_desc64(void *descp, int count)
366 {
367 struct fake_descriptor64 *fakep;
368 union {
369 struct real_gate64 gate;
370 struct real_descriptor64 desc;
371 } real;
372 int i;
373
374 fakep = (struct fake_descriptor64 *) descp;
375
376 for (i = 0; i < count; i++, fakep++) {
377 /*
378 * Construct the real decriptor locally.
379 */
380
381 bzero((void *) &real, sizeof(real));
382
383 switch (fakep->access & ACC_TYPE) {
384 case 0:
385 break;
386 case ACC_CALL_GATE:
387 case ACC_INTR_GATE:
388 case ACC_TRAP_GATE:
389 real.gate.offset_low16 = fakep->offset64 & 0xFFFF;
390 real.gate.selector16 = fakep->lim_or_seg & 0xFFFF;
391 real.gate.IST = fakep->size_or_IST & 0x7;
392 real.gate.access8 = fakep->access;
393 real.gate.offset_high16 = (fakep->offset64>>16)&0xFFFF;
394 real.gate.offset_top32 = (uint32_t)(fakep->offset64>>32);
395 break;
396 default: /* Otherwise */
397 real.desc.limit_low16 = fakep->lim_or_seg & 0xFFFF;
398 real.desc.base_low16 = fakep->offset64 & 0xFFFF;
399 real.desc.base_med8 = (fakep->offset64 >> 16) & 0xFF;
400 real.desc.access8 = fakep->access;
401 real.desc.limit_high4 = (fakep->lim_or_seg >> 16) & 0xFF;
402 real.desc.granularity4 = fakep->size_or_IST;
403 real.desc.base_high8 = (fakep->offset64 >> 24) & 0xFF;
404 real.desc.base_top32 = (uint32_t)(fakep->offset64>>32);
405 }
406
407 /*
408 * Now copy back over the fake structure.
409 */
410 bcopy((void *) &real, (void *) fakep, sizeof(real));
411 }
412 }
413
414 #ifdef __i386__
415 void
416 cpu_desc_init(cpu_data_t *cdp)
417 {
418 cpu_desc_index_t *cdi = &cdp->cpu_desc_index;
419
420 if (cdp == &cpu_data_master) {
421 /*
422 * Fix up the entries in the GDT to point to
423 * this LDT and this TSS.
424 */
425 struct fake_descriptor temp_fake_desc;
426 temp_fake_desc = ldt_desc_pattern;
427 temp_fake_desc.offset = (vm_offset_t) &master_ldt;
428 fix_desc(&temp_fake_desc, 1);
429 *(struct fake_descriptor *) &master_gdt[sel_idx(KERNEL_LDT)] =
430 temp_fake_desc;
431 *(struct fake_descriptor *) &master_gdt[sel_idx(USER_LDT)] =
432 temp_fake_desc;
433
434 temp_fake_desc = tss_desc_pattern;
435 temp_fake_desc.offset = (vm_offset_t) &master_ktss;
436 fix_desc(&temp_fake_desc, 1);
437 *(struct fake_descriptor *) &master_gdt[sel_idx(KERNEL_TSS)] =
438 temp_fake_desc;
439
440 #if MACH_KDB
441 temp_fake_desc = tss_desc_pattern;
442 temp_fake_desc.offset = (vm_offset_t) &master_dbtss;
443 fix_desc(&temp_fake_desc, 1);
444 *(struct fake_descriptor *) &master_gdt[sel_idx(DEBUG_TSS)] =
445 temp_fake_desc;
446 #endif
447
448 temp_fake_desc = cpudata_desc_pattern;
449 temp_fake_desc.offset = (vm_offset_t) &cpu_data_master;
450 fix_desc(&temp_fake_desc, 1);
451 *(struct fake_descriptor *) &master_gdt[sel_idx(CPU_DATA_GS)] =
452 temp_fake_desc;
453
454 fix_desc((void *)&master_idt, IDTSZ);
455
456 cdi->cdi_idt.ptr = master_idt;
457 cdi->cdi_gdt.ptr = (void *)master_gdt;
458
459
460 /*
461 * Master CPU uses the tables built at boot time.
462 * Just set the index pointers to the high shared-mapping space.
463 * Note that the sysenter stack uses empty space above the ktss
464 * in the HIGH_FIXED_KTSS page. In this case we don't map the
465 * the real master_sstk in low memory.
466 */
467 cdi->cdi_ktss = (struct i386_tss *)
468 pmap_index_to_virt(HIGH_FIXED_KTSS) ;
469 cdi->cdi_sstk = (vm_offset_t) (cdi->cdi_ktss + 1) +
470 (vm_offset_t) &master_sstk.top -
471 (vm_offset_t) &master_sstk;
472 } else {
473 cpu_desc_table_t *cdt = (cpu_desc_table_t *) cdp->cpu_desc_tablep;
474
475 vm_offset_t cpu_hi_desc;
476
477 cpu_hi_desc = pmap_cpu_high_shared_remap(
478 cdp->cpu_number,
479 HIGH_CPU_DESC,
480 (vm_offset_t) cdt, 1);
481
482 /*
483 * Per-cpu GDT, IDT, LDT, KTSS descriptors are allocated in one
484 * block (cpu_desc_table) and double-mapped into high shared space
485 * in one page window.
486 * Also, a transient stack for the fast sysenter path. The top of
487 * which is set at context switch time to point to the PCB using
488 * the high address.
489 */
490 cdi->cdi_gdt.ptr = (struct fake_descriptor *) (cpu_hi_desc +
491 offsetof(cpu_desc_table_t, gdt[0]));
492 cdi->cdi_idt.ptr = (struct fake_descriptor *) (cpu_hi_desc +
493 offsetof(cpu_desc_table_t, idt[0]));
494 cdi->cdi_ktss = (struct i386_tss *) (cpu_hi_desc +
495 offsetof(cpu_desc_table_t, ktss));
496 cdi->cdi_sstk = cpu_hi_desc + offsetof(cpu_desc_table_t, sstk.top);
497
498 /*
499 * LDT descriptors are mapped into a seperate area.
500 */
501 cdi->cdi_ldt = (struct fake_descriptor *)
502 pmap_cpu_high_shared_remap(
503 cdp->cpu_number,
504 HIGH_CPU_LDT_BEGIN,
505 (vm_offset_t) cdp->cpu_ldtp,
506 HIGH_CPU_LDT_END - HIGH_CPU_LDT_BEGIN + 1);
507
508 /*
509 * Copy the tables
510 */
511 bcopy((char *)master_idt, (char *)cdt->idt, sizeof(master_idt));
512 bcopy((char *)master_gdt, (char *)cdt->gdt, sizeof(master_gdt));
513 bcopy((char *)master_ldt, (char *)cdp->cpu_ldtp, sizeof(master_ldt));
514 bzero((char *)&cdt->ktss, sizeof(struct i386_tss));
515 #if MACH_KDB
516 cdi->cdi_dbtss = (struct i386_tss *) (cpu_hi_desc +
517 offsetof(cpu_desc_table_t, dbtss));
518 bcopy((char *)&master_dbtss,
519 (char *)&cdt->dbtss,
520 sizeof(struct i386_tss));
521 #endif /* MACH_KDB */
522
523 /*
524 * Fix up the entries in the GDT to point to
525 * this LDT and this TSS.
526 */
527 struct fake_descriptor temp_ldt = ldt_desc_pattern;
528 temp_ldt.offset = (vm_offset_t)cdi->cdi_ldt;
529 fix_desc(&temp_ldt, 1);
530
531 cdt->gdt[sel_idx(KERNEL_LDT)] = temp_ldt;
532 cdt->gdt[sel_idx(USER_LDT)] = temp_ldt;
533
534 cdt->gdt[sel_idx(KERNEL_TSS)] = tss_desc_pattern;
535 cdt->gdt[sel_idx(KERNEL_TSS)].offset = (vm_offset_t) cdi->cdi_ktss;
536 fix_desc(&cdt->gdt[sel_idx(KERNEL_TSS)], 1);
537
538 cdt->gdt[sel_idx(CPU_DATA_GS)] = cpudata_desc_pattern;
539 cdt->gdt[sel_idx(CPU_DATA_GS)].offset = (vm_offset_t) cdp;
540 fix_desc(&cdt->gdt[sel_idx(CPU_DATA_GS)], 1);
541
542 #if MACH_KDB
543 cdt->gdt[sel_idx(DEBUG_TSS)] = tss_desc_pattern;
544 cdt->gdt[sel_idx(DEBUG_TSS)].offset = (vm_offset_t) cdi->cdi_dbtss;
545 fix_desc(&cdt->gdt[sel_idx(DEBUG_TSS)], 1);
546
547 cdt->dbtss.esp0 = (int)(db_task_stack_store +
548 (INTSTACK_SIZE * (cdp->cpu_number)) - sizeof (natural_t));
549 cdt->dbtss.esp = cdt->dbtss.esp0;
550 cdt->dbtss.eip = (int)&db_task_start;
551 #endif /* MACH_KDB */
552
553 cdt->ktss.ss0 = KERNEL_DS;
554 cdt->ktss.io_bit_map_offset = 0x0FFF; /* no IO bitmap */
555
556 cpu_userwindow_init(cdp->cpu_number);
557 cpu_physwindow_init(cdp->cpu_number);
558
559 }
560 }
561 #endif /* __i386__ */
562
563 void
564 cpu_desc_init64(cpu_data_t *cdp)
565 {
566 cpu_desc_index_t *cdi = &cdp->cpu_desc_index;
567
568 if (cdp == &cpu_data_master) {
569 /*
570 * Master CPU uses the tables built at boot time.
571 * Just set the index pointers to the low memory space.
572 * Note that in 64-bit mode these are addressed in the
573 * double-mapped window (uber-space).
574 */
575 cdi->cdi_ktss = (void *)&master_ktss64;
576 cdi->cdi_sstk = (vm_offset_t) &master_sstk.top;
577 cdi->cdi_gdt.ptr = (void *)master_gdt;
578 cdi->cdi_idt.ptr = (void *)master_idt64;
579 cdi->cdi_ldt = (struct fake_descriptor *) master_ldt;
580
581
582 /* Replace the expanded LDTs and TSS slots in the GDT */
583 kernel_ldt_desc64.offset64 = UBER64(&master_ldt);
584 *(struct fake_descriptor64 *) &master_gdt[sel_idx(KERNEL_LDT)] =
585 kernel_ldt_desc64;
586 *(struct fake_descriptor64 *) &master_gdt[sel_idx(USER_LDT)] =
587 kernel_ldt_desc64;
588 kernel_tss_desc64.offset64 = UBER64(&master_ktss64);
589 *(struct fake_descriptor64 *) &master_gdt[sel_idx(KERNEL_TSS)] =
590 kernel_tss_desc64;
591
592 /* Fix up the expanded descriptors for 64-bit. */
593 fix_desc64((void *) &master_idt64, IDTSZ);
594 fix_desc64((void *) &master_gdt[sel_idx(KERNEL_LDT)], 1);
595 fix_desc64((void *) &master_gdt[sel_idx(USER_LDT)], 1);
596 fix_desc64((void *) &master_gdt[sel_idx(KERNEL_TSS)], 1);
597
598 /*
599 * Set the double-fault stack as IST1 in the 64-bit TSS
600 */
601 master_ktss64.ist1 = UBER64((uintptr_t) df_task_stack_end);
602
603 } else {
604 cpu_desc_table64_t *cdt = (cpu_desc_table64_t *) cdp->cpu_desc_tablep;
605 /*
606 * Per-cpu GDT, IDT, KTSS descriptors are allocated in kernel
607 * heap (cpu_desc_table) .
608 * On K32 they're double-mapped in uber-space (over 4GB).
609 * LDT descriptors are mapped into a separate area.
610 */
611 cdi->cdi_gdt.ptr = (struct fake_descriptor *)cdt->gdt;
612 cdi->cdi_idt.ptr = (void *)cdt->idt;
613 cdi->cdi_ktss = (void *)&cdt->ktss;
614 cdi->cdi_sstk = (vm_offset_t)&cdt->sstk.top;
615 cdi->cdi_ldt = cdp->cpu_ldtp;
616
617 /*
618 * Copy the tables
619 */
620 bcopy((char *)master_idt64, (char *)cdt->idt, sizeof(master_idt64));
621 bcopy((char *)master_gdt, (char *)cdt->gdt, sizeof(master_gdt));
622 bcopy((char *)master_ldt, (char *)cdp->cpu_ldtp, sizeof(master_ldt));
623 bcopy((char *)&master_ktss64, (char *)&cdt->ktss, sizeof(struct x86_64_tss));
624
625 /*
626 * Fix up the entries in the GDT to point to
627 * this LDT and this TSS.
628 */
629 kernel_ldt_desc64.offset64 = UBER64(cdi->cdi_ldt);
630 *(struct fake_descriptor64 *) &cdt->gdt[sel_idx(KERNEL_LDT)] =
631 kernel_ldt_desc64;
632 fix_desc64(&cdt->gdt[sel_idx(KERNEL_LDT)], 1);
633
634 kernel_ldt_desc64.offset64 = UBER64(cdi->cdi_ldt);
635 *(struct fake_descriptor64 *) &cdt->gdt[sel_idx(USER_LDT)] =
636 kernel_ldt_desc64;
637 fix_desc64(&cdt->gdt[sel_idx(USER_LDT)], 1);
638
639 kernel_tss_desc64.offset64 = UBER64(cdi->cdi_ktss);
640 *(struct fake_descriptor64 *) &cdt->gdt[sel_idx(KERNEL_TSS)] =
641 kernel_tss_desc64;
642 fix_desc64(&cdt->gdt[sel_idx(KERNEL_TSS)], 1);
643
644 /* Set double-fault stack as IST1 */
645 cdt->ktss.ist1 = UBER64((unsigned long)cdt->dfstk + sizeof(cdt->dfstk));
646 #ifdef __i386__
647 cdt->gdt[sel_idx(CPU_DATA_GS)] = cpudata_desc_pattern;
648 cdt->gdt[sel_idx(CPU_DATA_GS)].offset = (vm_offset_t) cdp;
649 fix_desc(&cdt->gdt[sel_idx(CPU_DATA_GS)], 1);
650
651 /* Allocate copyio windows */
652 cpu_userwindow_init(cdp->cpu_number);
653 cpu_physwindow_init(cdp->cpu_number);
654 #endif
655 }
656
657 /* Require that the top of the sysenter stack is 16-byte aligned */
658 if ((cdi->cdi_sstk % 16) != 0)
659 panic("cpu_desc_init64() sysenter stack not 16-byte aligned");
660 }
661
662 #ifdef __i386__
663 void
664 cpu_desc_load(cpu_data_t *cdp)
665 {
666 cpu_desc_index_t *cdi = &cdp->cpu_desc_index;
667
668 cdi->cdi_idt.size = 0x1000 + cdp->cpu_number;
669 cdi->cdi_gdt.size = sizeof(struct real_descriptor)*GDTSZ - 1;
670
671 lgdt((unsigned long *) &cdi->cdi_gdt);
672 lidt((unsigned long *) &cdi->cdi_idt);
673 lldt(KERNEL_LDT);
674
675 set_tr(KERNEL_TSS);
676
677 __asm__ volatile("mov %0, %%gs" : : "rm" ((unsigned short)(CPU_DATA_GS)));
678 }
679 #endif /* __i386__ */
680
681 void
682 cpu_desc_load64(cpu_data_t *cdp)
683 {
684 cpu_desc_index_t *cdi = &cdp->cpu_desc_index;
685
686 #ifdef __i386__
687 /*
688 * Load up the new descriptors etc
689 * ml_load_desc64() expects these global pseudo-descriptors:
690 * gdtptr64 -> per-cpu gdt
691 * idtptr64 -> per-cpu idt
692 * These are 10-byte descriptors with 64-bit addresses into
693 * uber-space.
694 *
695 * Refer to commpage/cpu_number.s for the IDT limit trick.
696 */
697 gdtptr64.length = GDTSZ * sizeof(struct real_descriptor64) - 1;
698 gdtptr64.offset[0] = (uint32_t) cdi->cdi_gdt.ptr;
699 gdtptr64.offset[1] = KERNEL_UBER_BASE_HI32;
700 idtptr64.length = 0x1000 + cdp->cpu_number;
701 idtptr64.offset[0] = (uint32_t) cdi->cdi_idt.ptr;
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 #else
709 /* Load the GDT, LDT, IDT and TSS */
710 cdi->cdi_gdt.size = sizeof(struct real_descriptor64)*GDTSZ - 1;
711 cdi->cdi_idt.size = 0x1000 + cdp->cpu_number;
712 lgdt((unsigned long *) &cdi->cdi_gdt);
713 lidt((unsigned long *) &cdi->cdi_idt);
714 lldt(KERNEL_LDT);
715 set_tr(KERNEL_TSS);
716
717 /* Stuff the pre-cpu data area into the MSR and swapgs to activate */
718 wrmsr64(MSR_IA32_KERNEL_GS_BASE, (unsigned long)cdp);
719 #if GPROF // Hack to enable mcount to work on K64
720 __asm__ volatile("mov %0, %%gs" : : "rm" ((unsigned short)(KERNEL_DS)));
721 #endif
722 swapgs();
723
724 cpu_mode_init(cdp);
725 #endif
726 }
727
728 #ifdef __i386__
729 /*
730 * Set MSRs for sysenter/sysexit for 32-bit.
731 */
732 static void
733 fast_syscall_init(__unused cpu_data_t *cdp)
734 {
735 wrmsr(MSR_IA32_SYSENTER_CS, SYSENTER_CS, 0);
736 wrmsr(MSR_IA32_SYSENTER_EIP, HI_TEXT(hi_sysenter), 0);
737 wrmsr(MSR_IA32_SYSENTER_ESP, current_sstk(), 0);
738 }
739 #endif
740
741 /*
742 * Set MSRs for sysenter/sysexit and syscall/sysret for 64-bit.
743 */
744 static void
745 fast_syscall_init64(__unused cpu_data_t *cdp)
746 {
747 wrmsr64(MSR_IA32_SYSENTER_CS, SYSENTER_CS);
748 wrmsr64(MSR_IA32_SYSENTER_EIP, UBER64((uintptr_t) hi64_sysenter));
749 wrmsr64(MSR_IA32_SYSENTER_ESP, UBER64(current_sstk()));
750 /* Enable syscall/sysret */
751 wrmsr64(MSR_IA32_EFER, rdmsr64(MSR_IA32_EFER) | MSR_IA32_EFER_SCE);
752
753 /*
754 * MSRs for 64-bit syscall/sysret
755 * Note USER_CS because sysret uses this + 16 when returning to
756 * 64-bit code.
757 */
758 wrmsr64(MSR_IA32_LSTAR, UBER64((uintptr_t) hi64_syscall));
759 wrmsr64(MSR_IA32_STAR, (((uint64_t)USER_CS) << 48) |
760 (((uint64_t)KERNEL64_CS) << 32));
761 /*
762 * Emulate eflags cleared by sysenter but note that
763 * we also clear the trace trap to avoid the complications
764 * of single-stepping into a syscall. The nested task bit
765 * is also cleared to avoid a spurious "task switch"
766 * should we choose to return via an IRET.
767 */
768 wrmsr64(MSR_IA32_FMASK, EFL_DF|EFL_IF|EFL_TF|EFL_NT);
769
770 #ifdef __i386__
771 /*
772 * Set the Kernel GS base MSR to point to per-cpu data in uber-space.
773 * The uber-space handler (hi64_syscall) uses the swapgs instruction.
774 */
775 wrmsr64(MSR_IA32_KERNEL_GS_BASE, UBER64(cdp));
776
777 #if ONLY_SAFE_FOR_LINDA_SERIAL
778 kprintf("fast_syscall_init64() KERNEL_GS_BASE=0x%016llx\n",
779 rdmsr64(MSR_IA32_KERNEL_GS_BASE));
780 #endif
781 #endif
782 }
783
784 cpu_data_t *
785 cpu_data_alloc(boolean_t is_boot_cpu)
786 {
787 int ret;
788 cpu_data_t *cdp;
789
790 if (is_boot_cpu) {
791 assert(real_ncpus == 1);
792 cdp = &cpu_data_master;
793 if (cdp->cpu_processor == NULL) {
794 simple_lock_init(&ncpus_lock, 0);
795 cdp->cpu_processor = cpu_processor_alloc(TRUE);
796 #if NCOPY_WINDOWS > 0
797 cdp->cpu_pmap = pmap_cpu_alloc(TRUE);
798 #endif
799 queue_init(&cdp->rtclock_timer.queue);
800 cdp->rtclock_timer.deadline = EndOfAllTime;
801 }
802 return cdp;
803 }
804
805 /*
806 * Allocate per-cpu data:
807 */
808 ret = kmem_alloc(kernel_map, (vm_offset_t *) &cdp, sizeof(cpu_data_t));
809 if (ret != KERN_SUCCESS) {
810 printf("cpu_data_alloc() failed, ret=%d\n", ret);
811 goto abort;
812 }
813 bzero((void*) cdp, sizeof(cpu_data_t));
814 cdp->cpu_this = cdp;
815
816 /* Propagate mode */
817 cdp->cpu_is64bit = cpu_mode_is64bit();
818
819 /*
820 * Allocate interrupt stack:
821 */
822 ret = kmem_alloc(kernel_map,
823 (vm_offset_t *) &cdp->cpu_int_stack_top,
824 INTSTACK_SIZE);
825 if (ret != KERN_SUCCESS) {
826 printf("cpu_data_alloc() int stack failed, ret=%d\n", ret);
827 goto abort;
828 }
829 bzero((void*) cdp->cpu_int_stack_top, INTSTACK_SIZE);
830 cdp->cpu_int_stack_top += INTSTACK_SIZE;
831
832
833 /*
834 * Allocate descriptor table:
835 * Size depends on cpu mode.
836 */
837 ret = kmem_alloc(kernel_map,
838 (vm_offset_t *) &cdp->cpu_desc_tablep,
839 cdp->cpu_is64bit ? sizeof(cpu_desc_table64_t)
840 : sizeof(cpu_desc_table_t));
841 if (ret != KERN_SUCCESS) {
842 printf("cpu_data_alloc() desc_table failed, ret=%d\n", ret);
843 goto abort;
844 }
845
846 /*
847 * Allocate LDT
848 */
849 ret = kmem_alloc(kernel_map,
850 (vm_offset_t *) &cdp->cpu_ldtp,
851 sizeof(struct real_descriptor) * LDTSZ);
852 if (ret != KERN_SUCCESS) {
853 printf("cpu_data_alloc() ldt failed, ret=%d\n", ret);
854 goto abort;
855 }
856
857 #if CONFIG_MCA
858 /* Machine-check shadow register allocation. */
859 mca_cpu_alloc(cdp);
860 #endif
861
862 simple_lock(&ncpus_lock);
863
864 cpu_data_ptr[real_ncpus] = cdp;
865 cdp->cpu_number = real_ncpus;
866 real_ncpus++;
867 simple_unlock(&ncpus_lock);
868
869 cdp->cpu_nanotime = &rtc_nanotime_info;
870 queue_init(&cdp->rtclock_timer.queue);
871 cdp->rtclock_timer.deadline = EndOfAllTime;
872
873 kprintf("cpu_data_alloc(%d) %p desc_table: %p "
874 "ldt: %p "
875 "int_stack: 0x%lx-0x%lx\n",
876 cdp->cpu_number, cdp, cdp->cpu_desc_tablep, cdp->cpu_ldtp,
877 (long)(cdp->cpu_int_stack_top - INTSTACK_SIZE), (long)(cdp->cpu_int_stack_top));
878
879 return cdp;
880
881 abort:
882 if (cdp) {
883 if (cdp->cpu_desc_tablep)
884 kfree((void *) cdp->cpu_desc_tablep,
885 sizeof(*cdp->cpu_desc_tablep));
886 if (cdp->cpu_int_stack_top)
887 kfree((void *) (cdp->cpu_int_stack_top - INTSTACK_SIZE),
888 INTSTACK_SIZE);
889 kfree((void *) cdp, sizeof(*cdp));
890 }
891 return NULL;
892 }
893
894 boolean_t
895 valid_user_segment_selectors(uint16_t cs,
896 uint16_t ss,
897 uint16_t ds,
898 uint16_t es,
899 uint16_t fs,
900 uint16_t gs)
901 {
902 return valid_user_code_selector(cs) &&
903 valid_user_stack_selector(ss) &&
904 valid_user_data_selector(ds) &&
905 valid_user_data_selector(es) &&
906 valid_user_data_selector(fs) &&
907 valid_user_data_selector(gs);
908 }
909
910
911 #if NCOPY_WINDOWS > 0
912
913 static vm_offset_t user_window_base = 0;
914
915 void
916 cpu_userwindow_init(int cpu)
917 {
918 cpu_data_t *cdp = cpu_data_ptr[cpu];
919 vm_offset_t user_window;
920 vm_offset_t vaddr;
921 int num_cpus;
922
923 num_cpus = ml_get_max_cpus();
924
925 if (cpu >= num_cpus)
926 panic("cpu_userwindow_init: cpu > num_cpus");
927
928 if (user_window_base == 0) {
929
930 if (vm_allocate(kernel_map, &vaddr,
931 (NBPDE * NCOPY_WINDOWS * num_cpus) + NBPDE,
932 VM_FLAGS_ANYWHERE) != KERN_SUCCESS)
933 panic("cpu_userwindow_init: "
934 "couldn't allocate user map window");
935
936 /*
937 * window must start on a page table boundary
938 * in the virtual address space
939 */
940 user_window_base = (vaddr + (NBPDE - 1)) & ~(NBPDE - 1);
941
942 /*
943 * get rid of any allocation leading up to our
944 * starting boundary
945 */
946 vm_deallocate(kernel_map, vaddr, user_window_base - vaddr);
947
948 /*
949 * get rid of tail that we don't need
950 */
951 user_window = user_window_base +
952 (NBPDE * NCOPY_WINDOWS * num_cpus);
953
954 vm_deallocate(kernel_map, user_window,
955 (vaddr +
956 ((NBPDE * NCOPY_WINDOWS * num_cpus) + NBPDE)) -
957 user_window);
958 }
959
960 user_window = user_window_base + (cpu * NCOPY_WINDOWS * NBPDE);
961
962 cdp->cpu_copywindow_base = user_window;
963 cdp->cpu_copywindow_pdp = pmap_pde(kernel_pmap, user_window);
964
965 #ifdef __i386__
966 cpu_desc_index_t *cdi = &cdp->cpu_desc_index;
967 cdi->cdi_gdt.ptr[sel_idx(USER_WINDOW_SEL)] = userwindow_desc_pattern;
968 cdi->cdi_gdt.ptr[sel_idx(USER_WINDOW_SEL)].offset = user_window;
969
970 fix_desc(&cdi->cdi_gdt.ptr[sel_idx(USER_WINDOW_SEL)], 1);
971 #endif /* __i386__ */
972 }
973
974 void
975 cpu_physwindow_init(int cpu)
976 {
977 cpu_data_t *cdp = cpu_data_ptr[cpu];
978 vm_offset_t phys_window = cdp->cpu_physwindow_base;
979
980 if (phys_window == 0) {
981 if (vm_allocate(kernel_map, &phys_window,
982 PAGE_SIZE, VM_FLAGS_ANYWHERE)
983 != KERN_SUCCESS)
984 panic("cpu_physwindow_init: "
985 "couldn't allocate phys map window");
986
987 /*
988 * make sure the page that encompasses the
989 * pte pointer we're interested in actually
990 * exists in the page table
991 */
992 pmap_expand(kernel_pmap, phys_window);
993
994 cdp->cpu_physwindow_base = phys_window;
995 cdp->cpu_physwindow_ptep = vtopte(phys_window);
996 }
997 #ifdef __i386__
998 cpu_desc_index_t *cdi = &cdp->cpu_desc_index;
999 cdi->cdi_gdt.ptr[sel_idx(PHYS_WINDOW_SEL)] = physwindow_desc_pattern;
1000 cdi->cdi_gdt.ptr[sel_idx(PHYS_WINDOW_SEL)].offset = phys_window;
1001
1002 fix_desc(&cdi->cdi_gdt.ptr[sel_idx(PHYS_WINDOW_SEL)], 1);
1003 #endif /* __i386__ */
1004 }
1005 #endif /* NCOPY_WINDOWS > 0 */
1006
1007 /*
1008 * Load the segment descriptor tables for the current processor.
1009 */
1010 void
1011 cpu_mode_init(cpu_data_t *cdp)
1012 {
1013 #ifdef __i386__
1014 if (cpu_mode_is64bit()) {
1015 cpu_IA32e_enable(cdp);
1016 cpu_desc_load64(cdp);
1017 fast_syscall_init64(cdp);
1018 } else {
1019 fast_syscall_init(cdp);
1020 }
1021 #else
1022 fast_syscall_init64(cdp);
1023 #endif
1024
1025 /* Call for per-cpu pmap mode initialization */
1026 pmap_cpu_init();
1027 }
1028