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