]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/mp_desc.c
xnu-1504.9.37.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
85 #ifdef __x86_64__
86 #define K_INTR_GATE (ACC_P|ACC_PL_K|ACC_INTR_GATE)
87 #define U_INTR_GATE (ACC_P|ACC_PL_U|ACC_INTR_GATE)
88
89 // Declare macros that will declare the externs
90 #define TRAP(n, name) extern void *name ;
91 #define TRAP_ERR(n, name) extern void *name ;
92 #define TRAP_SPC(n, name) extern void *name ;
93 #define TRAP_IST(n, name) extern void *name ;
94 #define INTERRUPT(n) extern void *_intr_ ## n ;
95 #define USER_TRAP(n, name) extern void *name ;
96 #define USER_TRAP_SPC(n, name) extern void *name ;
97
98 // Include the table to declare the externs
99 #include "../x86_64/idt_table.h"
100
101 // Undef the macros, then redefine them so we can declare the table
102 #undef TRAP
103 #undef TRAP_ERR
104 #undef TRAP_SPC
105 #undef TRAP_IST
106 #undef INTERRUPT
107 #undef USER_TRAP
108 #undef USER_TRAP_SPC
109
110 #define TRAP(n, name) \
111 [n] { \
112 (uintptr_t)&name, \
113 KERNEL64_CS, \
114 0, \
115 K_INTR_GATE, \
116 0 \
117 },
118
119 #define TRAP_ERR TRAP
120 #define TRAP_SPC TRAP
121
122 #define TRAP_IST(n, name) \
123 [n] { \
124 (uintptr_t)&name, \
125 KERNEL64_CS, \
126 1, \
127 K_INTR_GATE, \
128 0 \
129 },
130
131 #define INTERRUPT(n) \
132 [n] { \
133 (uintptr_t)&_intr_ ## n,\
134 KERNEL64_CS, \
135 0, \
136 K_INTR_GATE, \
137 0 \
138 },
139
140 #define USER_TRAP(n, name) \
141 [n] { \
142 (uintptr_t)&name, \
143 KERNEL64_CS, \
144 0, \
145 U_INTR_GATE, \
146 0 \
147 },
148
149 #define USER_TRAP_SPC USER_TRAP
150
151
152 // Declare the table using the macros we just set up
153 struct fake_descriptor64 master_idt64[IDTSZ] __attribute__ ((aligned (4096))) = {
154 #include "../x86_64/idt_table.h"
155 };
156 #endif
157
158 /*
159 * The i386 needs an interrupt stack to keep the PCB stack from being
160 * overrun by interrupts. All interrupt stacks MUST lie at lower addresses
161 * than any thread`s kernel stack.
162 */
163
164 /*
165 * First cpu`s interrupt stack.
166 */
167 extern uint32_t low_intstack[]; /* bottom */
168 extern uint32_t low_eintstack[]; /* top */
169
170 /*
171 * Per-cpu data area pointers.
172 * The master cpu (cpu 0) has its data area statically allocated;
173 * others are allocated dynamically and this array is updated at runtime.
174 */
175 cpu_data_t cpu_data_master = {
176 .cpu_this = &cpu_data_master,
177 .cpu_nanotime = &rtc_nanotime_info,
178 .cpu_int_stack_top = (vm_offset_t) low_eintstack,
179 #ifdef __i386__
180 .cpu_is64bit = FALSE,
181 #else
182 .cpu_is64bit = TRUE
183 #endif
184 };
185 cpu_data_t *cpu_data_ptr[MAX_CPUS] = { [0] &cpu_data_master };
186
187 decl_simple_lock_data(,ncpus_lock); /* protects real_ncpus */
188 unsigned int real_ncpus = 1;
189 unsigned int max_ncpus = MAX_CPUS;
190
191 #ifdef __i386__
192 extern void *hi_remap_text;
193 #define HI_TEXT(lo_text) \
194 (((uint32_t)&lo_text - (uint32_t)&hi_remap_text) + HIGH_MEM_BASE)
195
196 extern void hi_sysenter(void);
197
198 typedef struct {
199 uint16_t length;
200 uint32_t offset[2];
201 } __attribute__((__packed__)) table_descriptor64_t;
202
203 extern table_descriptor64_t gdtptr64;
204 extern table_descriptor64_t idtptr64;
205 #endif
206 extern void hi64_sysenter(void);
207 extern void hi64_syscall(void);
208
209 #if defined(__x86_64__) && !defined(UBER64)
210 #define UBER64(x) ((uintptr_t)x)
211 #endif
212
213 /*
214 * Multiprocessor i386/i486 systems use a separate copy of the
215 * GDT, IDT, LDT, and kernel TSS per processor. The first three
216 * are separate to avoid lock contention: the i386 uses locked
217 * memory cycles to access the descriptor tables. The TSS is
218 * separate since each processor needs its own kernel stack,
219 * and since using a TSS marks it busy.
220 */
221
222 /*
223 * Allocate and initialize the per-processor descriptor tables.
224 */
225
226 struct fake_descriptor ldt_desc_pattern = {
227 (unsigned int) 0,
228 LDTSZ_MIN * sizeof(struct fake_descriptor) - 1,
229 0,
230 ACC_P|ACC_PL_K|ACC_LDT
231 };
232
233 struct fake_descriptor tss_desc_pattern = {
234 (unsigned int) 0,
235 sizeof(struct i386_tss) - 1,
236 0,
237 ACC_P|ACC_PL_K|ACC_TSS
238 };
239
240 struct fake_descriptor cpudata_desc_pattern = {
241 (unsigned int) 0,
242 sizeof(cpu_data_t)-1,
243 SZ_32,
244 ACC_P|ACC_PL_K|ACC_DATA_W
245 };
246
247 struct fake_descriptor userwindow_desc_pattern = {
248 (unsigned int) 0,
249 ((NBPDE * NCOPY_WINDOWS) / PAGE_SIZE) - 1,
250 SZ_32 | SZ_G,
251 ACC_P|ACC_PL_U|ACC_DATA_W
252 };
253
254 struct fake_descriptor physwindow_desc_pattern = {
255 (unsigned int) 0,
256 PAGE_SIZE - 1,
257 SZ_32,
258 ACC_P|ACC_PL_K|ACC_DATA_W
259 };
260
261 /*
262 * This is the expanded, 64-bit variant of the kernel LDT descriptor.
263 * When switching to 64-bit mode this replaces KERNEL_LDT entry
264 * and the following empty slot. This enables the LDT to be referenced
265 * in the uber-space remapping window on the kernel.
266 */
267 struct fake_descriptor64 kernel_ldt_desc64 = {
268 0,
269 LDTSZ_MIN*sizeof(struct fake_descriptor)-1,
270 0,
271 ACC_P|ACC_PL_K|ACC_LDT,
272 0
273 };
274
275 /*
276 * This is the expanded, 64-bit variant of the kernel TSS descriptor.
277 * It is follows pattern of the KERNEL_LDT.
278 */
279 struct fake_descriptor64 kernel_tss_desc64 = {
280 0,
281 sizeof(struct x86_64_tss)-1,
282 0,
283 ACC_P|ACC_PL_K|ACC_TSS,
284 0
285 };
286
287 /*
288 * Convert a descriptor from fake to real format.
289 *
290 * Fake descriptor format:
291 * bytes 0..3 base 31..0
292 * bytes 4..5 limit 15..0
293 * byte 6 access byte 2 | limit 19..16
294 * byte 7 access byte 1
295 *
296 * Real descriptor format:
297 * bytes 0..1 limit 15..0
298 * bytes 2..3 base 15..0
299 * byte 4 base 23..16
300 * byte 5 access byte 1
301 * byte 6 access byte 2 | limit 19..16
302 * byte 7 base 31..24
303 *
304 * Fake gate format:
305 * bytes 0..3 offset
306 * bytes 4..5 selector
307 * byte 6 word count << 4 (to match fake descriptor)
308 * byte 7 access byte 1
309 *
310 * Real gate format:
311 * bytes 0..1 offset 15..0
312 * bytes 2..3 selector
313 * byte 4 word count
314 * byte 5 access byte 1
315 * bytes 6..7 offset 31..16
316 */
317 void
318 fix_desc(void *d, int num_desc) {
319 //early_kprintf("fix_desc(%x, %x)\n", d, num_desc);
320 uint8_t *desc = (uint8_t*) d;
321
322 do {
323 if ((desc[7] & 0x14) == 0x04) { /* gate */
324 uint32_t offset;
325 uint16_t selector;
326 uint8_t wordcount;
327 uint8_t acc;
328
329 offset = *((uint32_t*)(desc));
330 selector = *((uint32_t*)(desc+4));
331 wordcount = desc[6] >> 4;
332 acc = desc[7];
333
334 *((uint16_t*)desc) = offset & 0xFFFF;
335 *((uint16_t*)(desc+2)) = selector;
336 desc[4] = wordcount;
337 desc[5] = acc;
338 *((uint16_t*)(desc+6)) = offset >> 16;
339
340 } else { /* descriptor */
341 uint32_t base;
342 uint16_t limit;
343 uint8_t acc1, acc2;
344
345 base = *((uint32_t*)(desc));
346 limit = *((uint16_t*)(desc+4));
347 acc2 = desc[6];
348 acc1 = desc[7];
349
350 *((uint16_t*)(desc)) = limit;
351 *((uint16_t*)(desc+2)) = base & 0xFFFF;
352 desc[4] = (base >> 16) & 0xFF;
353 desc[5] = acc1;
354 desc[6] = acc2;
355 desc[7] = base >> 24;
356 }
357 desc += 8;
358 } while (--num_desc);
359 }
360
361 void
362 fix_desc64(void *descp, int count)
363 {
364 struct fake_descriptor64 *fakep;
365 union {
366 struct real_gate64 gate;
367 struct real_descriptor64 desc;
368 } real;
369 int i;
370
371 fakep = (struct fake_descriptor64 *) descp;
372
373 for (i = 0; i < count; i++, fakep++) {
374 /*
375 * Construct the real decriptor locally.
376 */
377
378 bzero((void *) &real, sizeof(real));
379
380 switch (fakep->access & ACC_TYPE) {
381 case 0:
382 break;
383 case ACC_CALL_GATE:
384 case ACC_INTR_GATE:
385 case ACC_TRAP_GATE:
386 real.gate.offset_low16 = fakep->offset64 & 0xFFFF;
387 real.gate.selector16 = fakep->lim_or_seg & 0xFFFF;
388 real.gate.IST = fakep->size_or_IST & 0x7;
389 real.gate.access8 = fakep->access;
390 real.gate.offset_high16 = (fakep->offset64>>16)&0xFFFF;
391 real.gate.offset_top32 = (uint32_t)(fakep->offset64>>32);
392 break;
393 default: /* Otherwise */
394 real.desc.limit_low16 = fakep->lim_or_seg & 0xFFFF;
395 real.desc.base_low16 = fakep->offset64 & 0xFFFF;
396 real.desc.base_med8 = (fakep->offset64 >> 16) & 0xFF;
397 real.desc.access8 = fakep->access;
398 real.desc.limit_high4 = (fakep->lim_or_seg >> 16) & 0xFF;
399 real.desc.granularity4 = fakep->size_or_IST;
400 real.desc.base_high8 = (fakep->offset64 >> 24) & 0xFF;
401 real.desc.base_top32 = (uint32_t)(fakep->offset64>>32);
402 }
403
404 /*
405 * Now copy back over the fake structure.
406 */
407 bcopy((void *) &real, (void *) fakep, sizeof(real));
408 }
409 }
410
411 #ifdef __i386__
412 void
413 cpu_desc_init(cpu_data_t *cdp)
414 {
415 cpu_desc_index_t *cdi = &cdp->cpu_desc_index;
416
417 if (cdp == &cpu_data_master) {
418 /*
419 * Fix up the entries in the GDT to point to
420 * this LDT and this TSS.
421 */
422 struct fake_descriptor temp_fake_desc;
423 temp_fake_desc = ldt_desc_pattern;
424 temp_fake_desc.offset = (vm_offset_t) &master_ldt;
425 fix_desc(&temp_fake_desc, 1);
426 *(struct fake_descriptor *) &master_gdt[sel_idx(KERNEL_LDT)] =
427 temp_fake_desc;
428 *(struct fake_descriptor *) &master_gdt[sel_idx(USER_LDT)] =
429 temp_fake_desc;
430
431 temp_fake_desc = tss_desc_pattern;
432 temp_fake_desc.offset = (vm_offset_t) &master_ktss;
433 fix_desc(&temp_fake_desc, 1);
434 *(struct fake_descriptor *) &master_gdt[sel_idx(KERNEL_TSS)] =
435 temp_fake_desc;
436
437 #if MACH_KDB
438 temp_fake_desc = tss_desc_pattern;
439 temp_fake_desc.offset = (vm_offset_t) &master_dbtss;
440 fix_desc(&temp_fake_desc, 1);
441 *(struct fake_descriptor *) &master_gdt[sel_idx(DEBUG_TSS)] =
442 temp_fake_desc;
443 #endif
444
445 temp_fake_desc = cpudata_desc_pattern;
446 temp_fake_desc.offset = (vm_offset_t) &cpu_data_master;
447 fix_desc(&temp_fake_desc, 1);
448 *(struct fake_descriptor *) &master_gdt[sel_idx(CPU_DATA_GS)] =
449 temp_fake_desc;
450
451 fix_desc((void *)&master_idt, IDTSZ);
452
453 cdi->cdi_idt.ptr = master_idt;
454 cdi->cdi_gdt.ptr = (void *)master_gdt;
455
456
457 /*
458 * Master CPU uses the tables built at boot time.
459 * Just set the index pointers to the high shared-mapping space.
460 * Note that the sysenter stack uses empty space above the ktss
461 * in the HIGH_FIXED_KTSS page. In this case we don't map the
462 * the real master_sstk in low memory.
463 */
464 cdi->cdi_ktss = (struct i386_tss *)
465 pmap_index_to_virt(HIGH_FIXED_KTSS) ;
466 cdi->cdi_sstk = (vm_offset_t) (cdi->cdi_ktss + 1) +
467 (vm_offset_t) &master_sstk.top -
468 (vm_offset_t) &master_sstk;
469 } else {
470 cpu_desc_table_t *cdt = (cpu_desc_table_t *) cdp->cpu_desc_tablep;
471
472 vm_offset_t cpu_hi_desc;
473
474 cpu_hi_desc = pmap_cpu_high_shared_remap(
475 cdp->cpu_number,
476 HIGH_CPU_DESC,
477 (vm_offset_t) cdt, 1);
478
479 /*
480 * Per-cpu GDT, IDT, LDT, KTSS descriptors are allocated in one
481 * block (cpu_desc_table) and double-mapped into high shared space
482 * in one page window.
483 * Also, a transient stack for the fast sysenter path. The top of
484 * which is set at context switch time to point to the PCB using
485 * the high address.
486 */
487 cdi->cdi_gdt.ptr = (struct fake_descriptor *) (cpu_hi_desc +
488 offsetof(cpu_desc_table_t, gdt[0]));
489 cdi->cdi_idt.ptr = (struct fake_descriptor *) (cpu_hi_desc +
490 offsetof(cpu_desc_table_t, idt[0]));
491 cdi->cdi_ktss = (struct i386_tss *) (cpu_hi_desc +
492 offsetof(cpu_desc_table_t, ktss));
493 cdi->cdi_sstk = cpu_hi_desc + offsetof(cpu_desc_table_t, sstk.top);
494
495 /*
496 * LDT descriptors are mapped into a seperate area.
497 */
498 cdi->cdi_ldt = (struct fake_descriptor *)
499 pmap_cpu_high_shared_remap(
500 cdp->cpu_number,
501 HIGH_CPU_LDT_BEGIN,
502 (vm_offset_t) cdp->cpu_ldtp,
503 HIGH_CPU_LDT_END - HIGH_CPU_LDT_BEGIN + 1);
504
505 /*
506 * Copy the tables
507 */
508 bcopy((char *)master_idt, (char *)cdt->idt, sizeof(master_idt));
509 bcopy((char *)master_gdt, (char *)cdt->gdt, sizeof(master_gdt));
510 bcopy((char *)master_ldt, (char *)cdp->cpu_ldtp, sizeof(master_ldt));
511 bzero((char *)&cdt->ktss, sizeof(struct i386_tss));
512 #if MACH_KDB
513 cdi->cdi_dbtss = (struct i386_tss *) (cpu_hi_desc +
514 offsetof(cpu_desc_table_t, dbtss));
515 bcopy((char *)&master_dbtss,
516 (char *)&cdt->dbtss,
517 sizeof(struct i386_tss));
518 #endif /* MACH_KDB */
519
520 /*
521 * Fix up the entries in the GDT to point to
522 * this LDT and this TSS.
523 */
524 struct fake_descriptor temp_ldt = ldt_desc_pattern;
525 temp_ldt.offset = (vm_offset_t)cdi->cdi_ldt;
526 fix_desc(&temp_ldt, 1);
527
528 cdt->gdt[sel_idx(KERNEL_LDT)] = temp_ldt;
529 cdt->gdt[sel_idx(USER_LDT)] = temp_ldt;
530
531 cdt->gdt[sel_idx(KERNEL_TSS)] = tss_desc_pattern;
532 cdt->gdt[sel_idx(KERNEL_TSS)].offset = (vm_offset_t) cdi->cdi_ktss;
533 fix_desc(&cdt->gdt[sel_idx(KERNEL_TSS)], 1);
534
535 cdt->gdt[sel_idx(CPU_DATA_GS)] = cpudata_desc_pattern;
536 cdt->gdt[sel_idx(CPU_DATA_GS)].offset = (vm_offset_t) cdp;
537 fix_desc(&cdt->gdt[sel_idx(CPU_DATA_GS)], 1);
538
539 #if MACH_KDB
540 cdt->gdt[sel_idx(DEBUG_TSS)] = tss_desc_pattern;
541 cdt->gdt[sel_idx(DEBUG_TSS)].offset = (vm_offset_t) cdi->cdi_dbtss;
542 fix_desc(&cdt->gdt[sel_idx(DEBUG_TSS)], 1);
543
544 cdt->dbtss.esp0 = (int)(db_task_stack_store +
545 (INTSTACK_SIZE * (cdp->cpu_number)) - sizeof (natural_t));
546 cdt->dbtss.esp = cdt->dbtss.esp0;
547 cdt->dbtss.eip = (int)&db_task_start;
548 #endif /* MACH_KDB */
549
550 cdt->ktss.ss0 = KERNEL_DS;
551 cdt->ktss.io_bit_map_offset = 0x0FFF; /* no IO bitmap */
552
553 cpu_userwindow_init(cdp->cpu_number);
554 cpu_physwindow_init(cdp->cpu_number);
555
556 }
557 }
558 #endif /* __i386__ */
559
560 void
561 cpu_desc_init64(cpu_data_t *cdp)
562 {
563 cpu_desc_index_t *cdi = &cdp->cpu_desc_index;
564
565 if (cdp == &cpu_data_master) {
566 /*
567 * Master CPU uses the tables built at boot time.
568 * Just set the index pointers to the low memory space.
569 */
570 cdi->cdi_ktss = (void *)&master_ktss64;
571 cdi->cdi_sstk = (vm_offset_t) &master_sstk.top;
572 cdi->cdi_gdt.ptr = (void *)master_gdt;
573 cdi->cdi_idt.ptr = (void *)master_idt64;
574 cdi->cdi_ldt = (struct fake_descriptor *) master_ldt;
575
576
577 /* Replace the expanded LDTs and TSS slots in the GDT */
578 kernel_ldt_desc64.offset64 = UBER64(&master_ldt);
579 *(struct fake_descriptor64 *) &master_gdt[sel_idx(KERNEL_LDT)] =
580 kernel_ldt_desc64;
581 *(struct fake_descriptor64 *) &master_gdt[sel_idx(USER_LDT)] =
582 kernel_ldt_desc64;
583 kernel_tss_desc64.offset64 = UBER64(&master_ktss64);
584 *(struct fake_descriptor64 *) &master_gdt[sel_idx(KERNEL_TSS)] =
585 kernel_tss_desc64;
586
587 /* Fix up the expanded descriptors for 64-bit. */
588 fix_desc64((void *) &master_idt64, IDTSZ);
589 fix_desc64((void *) &master_gdt[sel_idx(KERNEL_LDT)], 1);
590 fix_desc64((void *) &master_gdt[sel_idx(USER_LDT)], 1);
591 fix_desc64((void *) &master_gdt[sel_idx(KERNEL_TSS)], 1);
592
593 /*
594 * Set the double-fault stack as IST1 in the 64-bit TSS
595 */
596 master_ktss64.ist1 = UBER64((uintptr_t) df_task_stack_end);
597
598 } else {
599 cpu_desc_table64_t *cdt = (cpu_desc_table64_t *) cdp->cpu_desc_tablep;
600 /*
601 * Per-cpu GDT, IDT, KTSS descriptors are allocated in kernel
602 * heap (cpu_desc_table).
603 * LDT descriptors are mapped into a separate area.
604 */
605 cdi->cdi_gdt.ptr = (struct fake_descriptor *)cdt->gdt;
606 cdi->cdi_idt.ptr = (void *)cdt->idt;
607 cdi->cdi_ktss = (void *)&cdt->ktss;
608 cdi->cdi_sstk = (vm_offset_t)&cdt->sstk.top;
609 cdi->cdi_ldt = cdp->cpu_ldtp;
610
611 /*
612 * Copy the tables
613 */
614 bcopy((char *)master_idt64, (char *)cdt->idt, sizeof(master_idt64));
615 bcopy((char *)master_gdt, (char *)cdt->gdt, sizeof(master_gdt));
616 bcopy((char *)master_ldt, (char *)cdp->cpu_ldtp, sizeof(master_ldt));
617 bcopy((char *)&master_ktss64, (char *)&cdt->ktss, sizeof(struct x86_64_tss));
618
619 /*
620 * Fix up the entries in the GDT to point to
621 * this LDT and this TSS.
622 */
623 kernel_ldt_desc64.offset64 = UBER64(cdi->cdi_ldt);
624 *(struct fake_descriptor64 *) &cdt->gdt[sel_idx(KERNEL_LDT)] =
625 kernel_ldt_desc64;
626 fix_desc64(&cdt->gdt[sel_idx(KERNEL_LDT)], 1);
627
628 kernel_ldt_desc64.offset64 = UBER64(cdi->cdi_ldt);
629 *(struct fake_descriptor64 *) &cdt->gdt[sel_idx(USER_LDT)] =
630 kernel_ldt_desc64;
631 fix_desc64(&cdt->gdt[sel_idx(USER_LDT)], 1);
632
633 kernel_tss_desc64.offset64 = UBER64(cdi->cdi_ktss);
634 *(struct fake_descriptor64 *) &cdt->gdt[sel_idx(KERNEL_TSS)] =
635 kernel_tss_desc64;
636 fix_desc64(&cdt->gdt[sel_idx(KERNEL_TSS)], 1);
637
638 /* Set double-fault stack as IST1 */
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 cpu_data_t *
779 cpu_data_alloc(boolean_t is_boot_cpu)
780 {
781 int ret;
782 cpu_data_t *cdp;
783
784 if (is_boot_cpu) {
785 assert(real_ncpus == 1);
786 cdp = &cpu_data_master;
787 if (cdp->cpu_processor == NULL) {
788 simple_lock_init(&ncpus_lock, 0);
789 cdp->cpu_processor = cpu_processor_alloc(TRUE);
790 #if NCOPY_WINDOWS > 0
791 cdp->cpu_pmap = pmap_cpu_alloc(TRUE);
792 #endif
793 queue_init(&cdp->rtclock_timer.queue);
794 cdp->rtclock_timer.deadline = EndOfAllTime;
795 }
796 return cdp;
797 }
798
799 /*
800 * Allocate per-cpu data:
801 */
802 ret = kmem_alloc(kernel_map, (vm_offset_t *) &cdp, sizeof(cpu_data_t));
803 if (ret != KERN_SUCCESS) {
804 printf("cpu_data_alloc() failed, ret=%d\n", ret);
805 goto abort;
806 }
807 bzero((void*) cdp, sizeof(cpu_data_t));
808 cdp->cpu_this = cdp;
809
810 /* Propagate mode */
811 cdp->cpu_is64bit = cpu_mode_is64bit();
812
813 /*
814 * Allocate interrupt stack:
815 */
816 ret = kmem_alloc(kernel_map,
817 (vm_offset_t *) &cdp->cpu_int_stack_top,
818 INTSTACK_SIZE);
819 if (ret != KERN_SUCCESS) {
820 printf("cpu_data_alloc() int stack failed, ret=%d\n", ret);
821 goto abort;
822 }
823 bzero((void*) cdp->cpu_int_stack_top, INTSTACK_SIZE);
824 cdp->cpu_int_stack_top += INTSTACK_SIZE;
825
826
827 /*
828 * Allocate descriptor table:
829 * Size depends on cpu mode.
830 */
831 ret = kmem_alloc(kernel_map,
832 (vm_offset_t *) &cdp->cpu_desc_tablep,
833 cdp->cpu_is64bit ? sizeof(cpu_desc_table64_t)
834 : sizeof(cpu_desc_table_t));
835 if (ret != KERN_SUCCESS) {
836 printf("cpu_data_alloc() desc_table failed, ret=%d\n", ret);
837 goto abort;
838 }
839
840 /*
841 * Allocate LDT
842 */
843 ret = kmem_alloc(kernel_map,
844 (vm_offset_t *) &cdp->cpu_ldtp,
845 sizeof(struct real_descriptor) * LDTSZ);
846 if (ret != KERN_SUCCESS) {
847 printf("cpu_data_alloc() ldt failed, ret=%d\n", ret);
848 goto abort;
849 }
850
851 #if CONFIG_MCA
852 /* Machine-check shadow register allocation. */
853 mca_cpu_alloc(cdp);
854 #endif
855
856 simple_lock(&ncpus_lock);
857
858 cpu_data_ptr[real_ncpus] = cdp;
859 cdp->cpu_number = real_ncpus;
860 real_ncpus++;
861 simple_unlock(&ncpus_lock);
862
863 cdp->cpu_nanotime = &rtc_nanotime_info;
864 queue_init(&cdp->rtclock_timer.queue);
865 cdp->rtclock_timer.deadline = EndOfAllTime;
866
867 kprintf("cpu_data_alloc(%d) %p desc_table: %p "
868 "ldt: %p "
869 "int_stack: 0x%lx-0x%lx\n",
870 cdp->cpu_number, cdp, cdp->cpu_desc_tablep, cdp->cpu_ldtp,
871 (long)(cdp->cpu_int_stack_top - INTSTACK_SIZE), (long)(cdp->cpu_int_stack_top));
872
873 return cdp;
874
875 abort:
876 if (cdp) {
877 if (cdp->cpu_desc_tablep)
878 kfree((void *) cdp->cpu_desc_tablep,
879 sizeof(*cdp->cpu_desc_tablep));
880 if (cdp->cpu_int_stack_top)
881 kfree((void *) (cdp->cpu_int_stack_top - INTSTACK_SIZE),
882 INTSTACK_SIZE);
883 kfree((void *) cdp, sizeof(*cdp));
884 }
885 return NULL;
886 }
887
888 boolean_t
889 valid_user_segment_selectors(uint16_t cs,
890 uint16_t ss,
891 uint16_t ds,
892 uint16_t es,
893 uint16_t fs,
894 uint16_t gs)
895 {
896 return valid_user_code_selector(cs) &&
897 valid_user_stack_selector(ss) &&
898 valid_user_data_selector(ds) &&
899 valid_user_data_selector(es) &&
900 valid_user_data_selector(fs) &&
901 valid_user_data_selector(gs);
902 }
903
904
905 #if NCOPY_WINDOWS > 0
906
907 static vm_offset_t user_window_base = 0;
908
909 void
910 cpu_userwindow_init(int cpu)
911 {
912 cpu_data_t *cdp = cpu_data_ptr[cpu];
913 vm_offset_t user_window;
914 vm_offset_t vaddr;
915 int num_cpus;
916
917 num_cpus = ml_get_max_cpus();
918
919 if (cpu >= num_cpus)
920 panic("cpu_userwindow_init: cpu > num_cpus");
921
922 if (user_window_base == 0) {
923
924 if (vm_allocate(kernel_map, &vaddr,
925 (NBPDE * NCOPY_WINDOWS * num_cpus) + NBPDE,
926 VM_FLAGS_ANYWHERE) != KERN_SUCCESS)
927 panic("cpu_userwindow_init: "
928 "couldn't allocate user map window");
929
930 /*
931 * window must start on a page table boundary
932 * in the virtual address space
933 */
934 user_window_base = (vaddr + (NBPDE - 1)) & ~(NBPDE - 1);
935
936 /*
937 * get rid of any allocation leading up to our
938 * starting boundary
939 */
940 vm_deallocate(kernel_map, vaddr, user_window_base - vaddr);
941
942 /*
943 * get rid of tail that we don't need
944 */
945 user_window = user_window_base +
946 (NBPDE * NCOPY_WINDOWS * num_cpus);
947
948 vm_deallocate(kernel_map, user_window,
949 (vaddr +
950 ((NBPDE * NCOPY_WINDOWS * num_cpus) + NBPDE)) -
951 user_window);
952 }
953
954 user_window = user_window_base + (cpu * NCOPY_WINDOWS * NBPDE);
955
956 cdp->cpu_copywindow_base = user_window;
957 cdp->cpu_copywindow_pdp = pmap_pde(kernel_pmap, user_window);
958
959 #ifdef __i386__
960 cpu_desc_index_t *cdi = &cdp->cpu_desc_index;
961 cdi->cdi_gdt.ptr[sel_idx(USER_WINDOW_SEL)] = userwindow_desc_pattern;
962 cdi->cdi_gdt.ptr[sel_idx(USER_WINDOW_SEL)].offset = user_window;
963
964 fix_desc(&cdi->cdi_gdt.ptr[sel_idx(USER_WINDOW_SEL)], 1);
965 #endif /* __i386__ */
966 }
967
968 void
969 cpu_physwindow_init(int cpu)
970 {
971 cpu_data_t *cdp = cpu_data_ptr[cpu];
972 vm_offset_t phys_window = cdp->cpu_physwindow_base;
973
974 if (phys_window == 0) {
975 if (vm_allocate(kernel_map, &phys_window,
976 PAGE_SIZE, VM_FLAGS_ANYWHERE)
977 != KERN_SUCCESS)
978 panic("cpu_physwindow_init: "
979 "couldn't allocate phys map window");
980
981 /*
982 * make sure the page that encompasses the
983 * pte pointer we're interested in actually
984 * exists in the page table
985 */
986 pmap_expand(kernel_pmap, phys_window);
987
988 cdp->cpu_physwindow_base = phys_window;
989 cdp->cpu_physwindow_ptep = vtopte(phys_window);
990 }
991 #ifdef __i386__
992 cpu_desc_index_t *cdi = &cdp->cpu_desc_index;
993 cdi->cdi_gdt.ptr[sel_idx(PHYS_WINDOW_SEL)] = physwindow_desc_pattern;
994 cdi->cdi_gdt.ptr[sel_idx(PHYS_WINDOW_SEL)].offset = phys_window;
995
996 fix_desc(&cdi->cdi_gdt.ptr[sel_idx(PHYS_WINDOW_SEL)], 1);
997 #endif /* __i386__ */
998 }
999 #endif /* NCOPY_WINDOWS > 0 */
1000
1001 /*
1002 * Load the segment descriptor tables for the current processor.
1003 */
1004 void
1005 cpu_mode_init(cpu_data_t *cdp)
1006 {
1007 #ifdef __i386__
1008 if (cpu_mode_is64bit()) {
1009 cpu_IA32e_enable(cdp);
1010 cpu_desc_load64(cdp);
1011 fast_syscall_init64(cdp);
1012 } else {
1013 fast_syscall_init(cdp);
1014 }
1015 #else
1016 fast_syscall_init64(cdp);
1017 #endif
1018
1019 /* Call for per-cpu pmap mode initialization */
1020 pmap_cpu_init();
1021 }
1022