]> git.saurik.com Git - apple/xnu.git/blame - osfmk/i386/mp_desc.c
xnu-1504.15.3.tar.gz
[apple/xnu.git] / osfmk / i386 / mp_desc.c
CommitLineData
1c79356b 1/*
b0d623f7 2 * Copyright (c) 2000-2009 Apple Inc. All rights reserved.
1c79356b 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
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.
8f6c56a5 14 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
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
1c79356b 60#include <kern/cpu_number.h>
91447636 61#include <kern/kalloc.h>
1c79356b 62#include <kern/cpu_data.h>
0c530ab8 63#include <mach/mach_types.h>
1c79356b 64#include <mach/machine.h>
7e4a7d39 65#include <kern/etimer.h>
0c530ab8 66#include <mach/vm_map.h>
b0d623f7 67#include <mach/machine/vm_param.h>
1c79356b 68#include <vm/vm_kern.h>
0c530ab8 69#include <vm/vm_map.h>
1c79356b 70
1c79356b 71#include <i386/lock.h>
b0d623f7 72#include <i386/mp_desc.h>
1c79356b 73#include <i386/misc_protos.h>
55e303ae 74#include <i386/mp.h>
91447636 75#include <i386/pmap.h>
b0d623f7 76#if CONFIG_MCA
2d21ac55 77#include <i386/machine_check.h>
b0d623f7 78#endif
1c79356b
A
79
80#include <kern/misc_protos.h>
81
82#include <mach_kdb.h>
83
b0d623f7
A
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
153struct fake_descriptor64 master_idt64[IDTSZ] __attribute__ ((aligned (4096))) = {
154#include "../x86_64/idt_table.h"
155};
156#endif
157
1c79356b
A
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
1c79356b
A
164/*
165 * First cpu`s interrupt stack.
166 */
0c530ab8
A
167extern uint32_t low_intstack[]; /* bottom */
168extern uint32_t low_eintstack[]; /* top */
1c79356b
A
169
170/*
91447636
A
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.
1c79356b 174 */
593a1d5f 175cpu_data_t cpu_data_master = {
b0d623f7
A
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};
91447636
A
185cpu_data_t *cpu_data_ptr[MAX_CPUS] = { [0] &cpu_data_master };
186
b0d623f7 187decl_simple_lock_data(,ncpus_lock); /* protects real_ncpus */
91447636
A
188unsigned int real_ncpus = 1;
189unsigned int max_ncpus = MAX_CPUS;
1c79356b 190
b0d623f7 191#ifdef __i386__
0c530ab8
A
192extern void *hi_remap_text;
193#define HI_TEXT(lo_text) \
194 (((uint32_t)&lo_text - (uint32_t)&hi_remap_text) + HIGH_MEM_BASE)
195
2d21ac55 196extern void hi_sysenter(void);
b0d623f7
A
197
198typedef struct {
199 uint16_t length;
200 uint32_t offset[2];
201} __attribute__((__packed__)) table_descriptor64_t;
202
203extern table_descriptor64_t gdtptr64;
204extern table_descriptor64_t idtptr64;
205#endif
2d21ac55
A
206extern void hi64_sysenter(void);
207extern void hi64_syscall(void);
0c530ab8 208
b0d623f7
A
209#if defined(__x86_64__) && !defined(UBER64)
210#define UBER64(x) ((uintptr_t)x)
211#endif
212
1c79356b
A
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
1c79356b
A
222/*
223 * Allocate and initialize the per-processor descriptor tables.
224 */
225
226struct fake_descriptor ldt_desc_pattern = {
227 (unsigned int) 0,
0c530ab8 228 LDTSZ_MIN * sizeof(struct fake_descriptor) - 1,
1c79356b
A
229 0,
230 ACC_P|ACC_PL_K|ACC_LDT
231};
0c530ab8 232
1c79356b
A
233struct fake_descriptor tss_desc_pattern = {
234 (unsigned int) 0,
0c530ab8 235 sizeof(struct i386_tss) - 1,
1c79356b
A
236 0,
237 ACC_P|ACC_PL_K|ACC_TSS
238};
239
240struct 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
0c530ab8
A
247struct 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
254struct 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 */
267struct fake_descriptor64 kernel_ldt_desc64 = {
b0d623f7 268 0,
0c530ab8
A
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 */
279struct fake_descriptor64 kernel_tss_desc64 = {
b0d623f7 280 0,
0c530ab8
A
281 sizeof(struct x86_64_tss)-1,
282 0,
283 ACC_P|ACC_PL_K|ACC_TSS,
284 0
285};
286
b0d623f7
A
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 */
317void
318fix_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
361void
362fix_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__
91447636 412void
b0d623f7 413cpu_desc_init(cpu_data_t *cdp)
1c79356b 414{
0c530ab8 415 cpu_desc_index_t *cdi = &cdp->cpu_desc_index;
1c79356b 416
b0d623f7
A
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) +
0c530ab8
A
467 (vm_offset_t) &master_sstk.top -
468 (vm_offset_t) &master_sstk;
91447636 469 } else {
b0d623f7
A
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);
91447636 494
b0d623f7
A
495 /*
496 * LDT descriptors are mapped into a seperate area.
497 */
498 cdi->cdi_ldt = (struct fake_descriptor *)
0c530ab8 499 pmap_cpu_high_shared_remap(
b0d623f7
A
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);
55e303ae 504
b0d623f7
A
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));
1c79356b 512#if MACH_KDB
b0d623f7 513 cdi->cdi_dbtss = (struct i386_tss *) (cpu_hi_desc +
0c530ab8 514 offsetof(cpu_desc_table_t, dbtss));
b0d623f7
A
515 bcopy((char *)&master_dbtss,
516 (char *)&cdt->dbtss,
517 sizeof(struct i386_tss));
1c79356b
A
518#endif /* MACH_KDB */
519
b0d623f7
A
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);
0c530ab8 527
b0d623f7
A
528 cdt->gdt[sel_idx(KERNEL_LDT)] = temp_ldt;
529 cdt->gdt[sel_idx(USER_LDT)] = temp_ldt;
4452a7af 530
b0d623f7
A
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);
1c79356b 534
b0d623f7
A
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);
1c79356b
A
538
539#if MACH_KDB
b0d623f7
A
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;
1c79356b
A
548#endif /* MACH_KDB */
549
b0d623f7
A
550 cdt->ktss.ss0 = KERNEL_DS;
551 cdt->ktss.io_bit_map_offset = 0x0FFF; /* no IO bitmap */
0c530ab8 552
b0d623f7
A
553 cpu_userwindow_init(cdp->cpu_number);
554 cpu_physwindow_init(cdp->cpu_number);
0c530ab8 555
4452a7af 556 }
0c530ab8 557}
b0d623f7 558#endif /* __i386__ */
0c530ab8
A
559
560void
b0d623f7 561cpu_desc_init64(cpu_data_t *cdp)
0c530ab8 562{
0c530ab8
A
563 cpu_desc_index_t *cdi = &cdp->cpu_desc_index;
564
b0d623f7 565 if (cdp == &cpu_data_master) {
2d21ac55
A
566 /*
567 * Master CPU uses the tables built at boot time.
568 * Just set the index pointers to the low memory space.
2d21ac55 569 */
b0d623f7 570 cdi->cdi_ktss = (void *)&master_ktss64;
2d21ac55 571 cdi->cdi_sstk = (vm_offset_t) &master_sstk.top;
b0d623f7
A
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
2d21ac55 576
b0d623f7
A
577 /* Replace the expanded LDTs and TSS slots in the GDT */
578 kernel_ldt_desc64.offset64 = UBER64(&master_ldt);
2d21ac55
A
579 *(struct fake_descriptor64 *) &master_gdt[sel_idx(KERNEL_LDT)] =
580 kernel_ldt_desc64;
b0d623f7
A
581 *(struct fake_descriptor64 *) &master_gdt[sel_idx(USER_LDT)] =
582 kernel_ldt_desc64;
583 kernel_tss_desc64.offset64 = UBER64(&master_ktss64);
2d21ac55
A
584 *(struct fake_descriptor64 *) &master_gdt[sel_idx(KERNEL_TSS)] =
585 kernel_tss_desc64;
0c530ab8 586
b0d623f7 587 /* Fix up the expanded descriptors for 64-bit. */
2d21ac55
A
588 fix_desc64((void *) &master_idt64, IDTSZ);
589 fix_desc64((void *) &master_gdt[sel_idx(KERNEL_LDT)], 1);
b0d623f7 590 fix_desc64((void *) &master_gdt[sel_idx(USER_LDT)], 1);
2d21ac55 591 fix_desc64((void *) &master_gdt[sel_idx(KERNEL_TSS)], 1);
0c530ab8 592
2d21ac55
A
593 /*
594 * Set the double-fault stack as IST1 in the 64-bit TSS
595 */
b0d623f7 596 master_ktss64.ist1 = UBER64((uintptr_t) df_task_stack_end);
0c530ab8
A
597
598 } else {
b0d623f7 599 cpu_desc_table64_t *cdt = (cpu_desc_table64_t *) cdp->cpu_desc_tablep;
2d21ac55
A
600 /*
601 * Per-cpu GDT, IDT, KTSS descriptors are allocated in kernel
7e4a7d39 602 * heap (cpu_desc_table).
2d21ac55
A
603 * LDT descriptors are mapped into a separate area.
604 */
b0d623f7
A
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;
2d21ac55
A
608 cdi->cdi_sstk = (vm_offset_t)&cdt->sstk.top;
609 cdi->cdi_ldt = cdp->cpu_ldtp;
0c530ab8 610
2d21ac55
A
611 /*
612 * Copy the tables
613 */
b0d623f7
A
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));
0c530ab8 618
2d21ac55
A
619 /*
620 * Fix up the entries in the GDT to point to
621 * this LDT and this TSS.
622 */
b0d623f7 623 kernel_ldt_desc64.offset64 = UBER64(cdi->cdi_ldt);
2d21ac55
A
624 *(struct fake_descriptor64 *) &cdt->gdt[sel_idx(KERNEL_LDT)] =
625 kernel_ldt_desc64;
626 fix_desc64(&cdt->gdt[sel_idx(KERNEL_LDT)], 1);
0c530ab8 627
b0d623f7 628 kernel_ldt_desc64.offset64 = UBER64(cdi->cdi_ldt);
2d21ac55
A
629 *(struct fake_descriptor64 *) &cdt->gdt[sel_idx(USER_LDT)] =
630 kernel_ldt_desc64;
631 fix_desc64(&cdt->gdt[sel_idx(USER_LDT)], 1);
0c530ab8 632
b0d623f7 633 kernel_tss_desc64.offset64 = UBER64(cdi->cdi_ktss);
2d21ac55
A
634 *(struct fake_descriptor64 *) &cdt->gdt[sel_idx(KERNEL_TSS)] =
635 kernel_tss_desc64;
636 fix_desc64(&cdt->gdt[sel_idx(KERNEL_TSS)], 1);
0c530ab8 637
b0d623f7
A
638 /* Set double-fault stack as IST1 */
639 cdt->ktss.ist1 = UBER64((unsigned long)cdt->dfstk + sizeof(cdt->dfstk));
640#ifdef __i386__
2d21ac55
A
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);
0c530ab8 644
b0d623f7 645 /* Allocate copyio windows */
2d21ac55
A
646 cpu_userwindow_init(cdp->cpu_number);
647 cpu_physwindow_init(cdp->cpu_number);
b0d623f7 648#endif
0c530ab8
A
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
b0d623f7
A
656#ifdef __i386__
657void
658cpu_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
675void
676cpu_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 */
7e4a7d39 691 gdtptr64.length = GDTSZ * sizeof(struct real_descriptor) - 1;
b0d623f7
A
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 */
7e4a7d39 704 cdi->cdi_gdt.size = sizeof(struct real_descriptor)*GDTSZ - 1;
b0d623f7
A
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 */
726static void
727fast_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
0c530ab8 735/*
b0d623f7 736 * Set MSRs for sysenter/sysexit and syscall/sysret for 64-bit.
0c530ab8 737 */
2d21ac55 738static void
b0d623f7 739fast_syscall_init64(__unused cpu_data_t *cdp)
0c530ab8
A
740{
741 wrmsr64(MSR_IA32_SYSENTER_CS, SYSENTER_CS);
b0d623f7 742 wrmsr64(MSR_IA32_SYSENTER_EIP, UBER64((uintptr_t) hi64_sysenter));
0c530ab8 743 wrmsr64(MSR_IA32_SYSENTER_ESP, UBER64(current_sstk()));
0c530ab8
A
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 */
b0d623f7
A
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));
0c530ab8
A
755 /*
756 * Emulate eflags cleared by sysenter but note that
757 * we also clear the trace trap to avoid the complications
2d21ac55
A
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.
0c530ab8
A
761 */
762 wrmsr64(MSR_IA32_FMASK, EFL_DF|EFL_IF|EFL_TF|EFL_NT);
763
b0d623f7 764#ifdef __i386__
0c530ab8 765 /*
2d21ac55 766 * Set the Kernel GS base MSR to point to per-cpu data in uber-space.
0c530ab8
A
767 * The uber-space handler (hi64_syscall) uses the swapgs instruction.
768 */
b0d623f7 769 wrmsr64(MSR_IA32_KERNEL_GS_BASE, UBER64(cdp));
4a3eedf9
A
770
771#if ONLY_SAFE_FOR_LINDA_SERIAL
0c530ab8 772 kprintf("fast_syscall_init64() KERNEL_GS_BASE=0x%016llx\n",
b0d623f7
A
773 rdmsr64(MSR_IA32_KERNEL_GS_BASE));
774#endif
4a3eedf9 775#endif
1c79356b
A
776}
777
91447636
A
778cpu_data_t *
779cpu_data_alloc(boolean_t is_boot_cpu)
1c79356b 780{
91447636
A
781 int ret;
782 cpu_data_t *cdp;
783
784 if (is_boot_cpu) {
785 assert(real_ncpus == 1);
91447636
A
786 cdp = &cpu_data_master;
787 if (cdp->cpu_processor == NULL) {
b0d623f7 788 simple_lock_init(&ncpus_lock, 0);
91447636 789 cdp->cpu_processor = cpu_processor_alloc(TRUE);
b0d623f7 790#if NCOPY_WINDOWS > 0
91447636 791 cdp->cpu_pmap = pmap_cpu_alloc(TRUE);
b0d623f7 792#endif
c910b4d9
A
793 queue_init(&cdp->rtclock_timer.queue);
794 cdp->rtclock_timer.deadline = EndOfAllTime;
91447636
A
795 }
796 return cdp;
797 }
1c79356b 798
1c79356b 799 /*
91447636 800 * Allocate per-cpu data:
1c79356b 801 */
b0d623f7 802 ret = kmem_alloc(kernel_map, (vm_offset_t *) &cdp, sizeof(cpu_data_t));
91447636
A
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;
1c79356b 809
0c530ab8
A
810 /* Propagate mode */
811 cdp->cpu_is64bit = cpu_mode_is64bit();
812
1c79356b 813 /*
91447636 814 * Allocate interrupt stack:
1c79356b 815 */
91447636
A
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;
1c79356b 822 }
91447636
A
823 bzero((void*) cdp->cpu_int_stack_top, INTSTACK_SIZE);
824 cdp->cpu_int_stack_top += INTSTACK_SIZE;
1c79356b 825
b0d623f7 826
1c79356b 827 /*
91447636 828 * Allocate descriptor table:
0c530ab8 829 * Size depends on cpu mode.
1c79356b 830 */
91447636
A
831 ret = kmem_alloc(kernel_map,
832 (vm_offset_t *) &cdp->cpu_desc_tablep,
0c530ab8
A
833 cdp->cpu_is64bit ? sizeof(cpu_desc_table64_t)
834 : sizeof(cpu_desc_table_t));
91447636
A
835 if (ret != KERN_SUCCESS) {
836 printf("cpu_data_alloc() desc_table failed, ret=%d\n", ret);
837 goto abort;
838 }
1c79356b 839
0c530ab8
A
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
b0d623f7 851#if CONFIG_MCA
2d21ac55
A
852 /* Machine-check shadow register allocation. */
853 mca_cpu_alloc(cdp);
b0d623f7
A
854#endif
855
856 simple_lock(&ncpus_lock);
2d21ac55 857
91447636
A
858 cpu_data_ptr[real_ncpus] = cdp;
859 cdp->cpu_number = real_ncpus;
860 real_ncpus++;
b0d623f7 861 simple_unlock(&ncpus_lock);
0c530ab8 862
593a1d5f 863 cdp->cpu_nanotime = &rtc_nanotime_info;
c910b4d9
A
864 queue_init(&cdp->rtclock_timer.queue);
865 cdp->rtclock_timer.deadline = EndOfAllTime;
593a1d5f 866
2d21ac55
A
867 kprintf("cpu_data_alloc(%d) %p desc_table: %p "
868 "ldt: %p "
b0d623f7 869 "int_stack: 0x%lx-0x%lx\n",
0c530ab8 870 cdp->cpu_number, cdp, cdp->cpu_desc_tablep, cdp->cpu_ldtp,
b0d623f7 871 (long)(cdp->cpu_int_stack_top - INTSTACK_SIZE), (long)(cdp->cpu_int_stack_top));
91447636
A
872
873 return cdp;
874
875abort:
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}
1c79356b 887
91447636
A
888boolean_t
889valid_user_segment_selectors(uint16_t cs,
b0d623f7
A
890 uint16_t ss,
891 uint16_t ds,
892 uint16_t es,
893 uint16_t fs,
894 uint16_t gs)
91447636
A
895{
896 return valid_user_code_selector(cs) &&
b0d623f7
A
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);
1c79356b
A
902}
903
0c530ab8 904
b0d623f7
A
905#if NCOPY_WINDOWS > 0
906
0c530ab8 907static vm_offset_t user_window_base = 0;
0c530ab8
A
908
909void
2d21ac55 910cpu_userwindow_init(int cpu)
0c530ab8
A
911{
912 cpu_data_t *cdp = cpu_data_ptr[cpu];
b0d623f7
A
913 vm_offset_t user_window;
914 vm_offset_t vaddr;
0c530ab8
A
915 int num_cpus;
916
917 num_cpus = ml_get_max_cpus();
918
919 if (cpu >= num_cpus)
b0d623f7 920 panic("cpu_userwindow_init: cpu > num_cpus");
0c530ab8
A
921
922 if (user_window_base == 0) {
923
b0d623f7
A
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");
0c530ab8
A
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);
0c530ab8
A
952 }
953
b0d623f7 954 user_window = user_window_base + (cpu * NCOPY_WINDOWS * NBPDE);
0c530ab8 955
0c530ab8
A
956 cdp->cpu_copywindow_base = user_window;
957 cdp->cpu_copywindow_pdp = pmap_pde(kernel_pmap, user_window);
958
b0d623f7
A
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;
0c530ab8 963
b0d623f7
A
964 fix_desc(&cdi->cdi_gdt.ptr[sel_idx(USER_WINDOW_SEL)], 1);
965#endif /* __i386__ */
2d21ac55 966}
0c530ab8 967
2d21ac55
A
968void
969cpu_physwindow_init(int cpu)
970{
971 cpu_data_t *cdp = cpu_data_ptr[cpu];
c910b4d9 972 vm_offset_t phys_window = cdp->cpu_physwindow_base;
2d21ac55 973
c910b4d9
A
974 if (phys_window == 0) {
975 if (vm_allocate(kernel_map, &phys_window,
976 PAGE_SIZE, VM_FLAGS_ANYWHERE)
2d21ac55 977 != KERN_SUCCESS)
c910b4d9
A
978 panic("cpu_physwindow_init: "
979 "couldn't allocate phys map window");
2d21ac55 980
c910b4d9
A
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);
0c530ab8 987
c910b4d9
A
988 cdp->cpu_physwindow_base = phys_window;
989 cdp->cpu_physwindow_ptep = vtopte(phys_window);
990 }
b0d623f7
A
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;
0c530ab8 995
b0d623f7
A
996 fix_desc(&cdi->cdi_gdt.ptr[sel_idx(PHYS_WINDOW_SEL)], 1);
997#endif /* __i386__ */
0c530ab8 998}
b0d623f7 999#endif /* NCOPY_WINDOWS > 0 */
0c530ab8 1000
0c530ab8
A
1001/*
1002 * Load the segment descriptor tables for the current processor.
1003 */
2d21ac55
A
1004void
1005cpu_mode_init(cpu_data_t *cdp)
1006{
b0d623f7 1007#ifdef __i386__
2d21ac55
A
1008 if (cpu_mode_is64bit()) {
1009 cpu_IA32e_enable(cdp);
1010 cpu_desc_load64(cdp);
b0d623f7 1011 fast_syscall_init64(cdp);
2d21ac55 1012 } else {
b0d623f7 1013 fast_syscall_init(cdp);
2d21ac55 1014 }
b0d623f7
A
1015#else
1016 fast_syscall_init64(cdp);
1017#endif
2d21ac55
A
1018
1019 /* Call for per-cpu pmap mode initialization */
1020 pmap_cpu_init();
2d21ac55
A
1021}
1022