]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
39236c6e | 2 | * Copyright (c) 2000-2012 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> |
0c530ab8 | 65 | #include <mach/vm_map.h> |
b0d623f7 | 66 | #include <mach/machine/vm_param.h> |
1c79356b | 67 | #include <vm/vm_kern.h> |
0c530ab8 | 68 | #include <vm/vm_map.h> |
1c79356b | 69 | |
fe8ab488 | 70 | #include <i386/bit_routines.h> |
b0d623f7 | 71 | #include <i386/mp_desc.h> |
1c79356b | 72 | #include <i386/misc_protos.h> |
55e303ae | 73 | #include <i386/mp.h> |
91447636 | 74 | #include <i386/pmap.h> |
143464d5 | 75 | #if defined(__i386__) || defined(__x86_64__) |
316670eb A |
76 | #include <i386/pmap_internal.h> |
77 | #endif /* i386 */ | |
b0d623f7 | 78 | #if CONFIG_MCA |
2d21ac55 | 79 | #include <i386/machine_check.h> |
b0d623f7 | 80 | #endif |
1c79356b A |
81 | |
82 | #include <kern/misc_protos.h> | |
83 | ||
b0d623f7 A |
84 | #define K_INTR_GATE (ACC_P|ACC_PL_K|ACC_INTR_GATE) |
85 | #define U_INTR_GATE (ACC_P|ACC_PL_U|ACC_INTR_GATE) | |
86 | ||
87 | // Declare macros that will declare the externs | |
88 | #define TRAP(n, name) extern void *name ; | |
89 | #define TRAP_ERR(n, name) extern void *name ; | |
90 | #define TRAP_SPC(n, name) extern void *name ; | |
39236c6e A |
91 | #define TRAP_IST1(n, name) extern void *name ; |
92 | #define TRAP_IST2(n, name) extern void *name ; | |
b0d623f7 A |
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 | |
39236c6e A |
104 | #undef TRAP_IST1 |
105 | #undef TRAP_IST2 | |
b0d623f7 A |
106 | #undef INTERRUPT |
107 | #undef USER_TRAP | |
108 | #undef USER_TRAP_SPC | |
109 | ||
110 | #define TRAP(n, name) \ | |
6d2010ae | 111 | [n] = { \ |
b0d623f7 A |
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 | ||
39236c6e | 122 | #define TRAP_IST1(n, name) \ |
6d2010ae | 123 | [n] = { \ |
b0d623f7 A |
124 | (uintptr_t)&name, \ |
125 | KERNEL64_CS, \ | |
126 | 1, \ | |
127 | K_INTR_GATE, \ | |
128 | 0 \ | |
129 | }, | |
130 | ||
39236c6e A |
131 | #define TRAP_IST2(n, name) \ |
132 | [n] = { \ | |
133 | (uintptr_t)&name, \ | |
134 | KERNEL64_CS, \ | |
135 | 2, \ | |
136 | K_INTR_GATE, \ | |
137 | 0 \ | |
138 | }, | |
139 | ||
b0d623f7 | 140 | #define INTERRUPT(n) \ |
6d2010ae | 141 | [n] = { \ |
b0d623f7 A |
142 | (uintptr_t)&_intr_ ## n,\ |
143 | KERNEL64_CS, \ | |
144 | 0, \ | |
145 | K_INTR_GATE, \ | |
146 | 0 \ | |
147 | }, | |
148 | ||
149 | #define USER_TRAP(n, name) \ | |
6d2010ae | 150 | [n] = { \ |
b0d623f7 A |
151 | (uintptr_t)&name, \ |
152 | KERNEL64_CS, \ | |
153 | 0, \ | |
154 | U_INTR_GATE, \ | |
155 | 0 \ | |
156 | }, | |
157 | ||
158 | #define USER_TRAP_SPC USER_TRAP | |
b0d623f7 A |
159 | |
160 | // Declare the table using the macros we just set up | |
316670eb A |
161 | struct fake_descriptor64 master_idt64[IDTSZ] |
162 | __attribute__ ((section("__HIB,__desc"))) | |
163 | __attribute__ ((aligned(PAGE_SIZE))) = { | |
b0d623f7 A |
164 | #include "../x86_64/idt_table.h" |
165 | }; | |
1c79356b | 166 | |
1c79356b A |
167 | /* |
168 | * First cpu`s interrupt stack. | |
169 | */ | |
316670eb | 170 | extern uint32_t low_intstack[]; /* bottom */ |
0c530ab8 | 171 | extern uint32_t low_eintstack[]; /* top */ |
1c79356b A |
172 | |
173 | /* | |
91447636 A |
174 | * Per-cpu data area pointers. |
175 | * The master cpu (cpu 0) has its data area statically allocated; | |
176 | * others are allocated dynamically and this array is updated at runtime. | |
1c79356b | 177 | */ |
39236c6e | 178 | static cpu_data_t cpu_data_master = { |
b0d623f7 | 179 | .cpu_this = &cpu_data_master, |
6d2010ae | 180 | .cpu_nanotime = &pal_rtc_nanotime_info, |
b0d623f7 | 181 | .cpu_int_stack_top = (vm_offset_t) low_eintstack, |
b0d623f7 | 182 | }; |
6d2010ae | 183 | cpu_data_t *cpu_data_ptr[MAX_CPUS] = { [0] = &cpu_data_master }; |
91447636 | 184 | |
b0d623f7 | 185 | decl_simple_lock_data(,ncpus_lock); /* protects real_ncpus */ |
91447636 A |
186 | unsigned int real_ncpus = 1; |
187 | unsigned int max_ncpus = MAX_CPUS; | |
1c79356b | 188 | |
2d21ac55 A |
189 | extern void hi64_sysenter(void); |
190 | extern void hi64_syscall(void); | |
0c530ab8 | 191 | |
1c79356b A |
192 | /* |
193 | * Multiprocessor i386/i486 systems use a separate copy of the | |
194 | * GDT, IDT, LDT, and kernel TSS per processor. The first three | |
195 | * are separate to avoid lock contention: the i386 uses locked | |
196 | * memory cycles to access the descriptor tables. The TSS is | |
197 | * separate since each processor needs its own kernel stack, | |
198 | * and since using a TSS marks it busy. | |
199 | */ | |
200 | ||
1c79356b A |
201 | /* |
202 | * Allocate and initialize the per-processor descriptor tables. | |
203 | */ | |
204 | ||
205 | struct fake_descriptor ldt_desc_pattern = { | |
206 | (unsigned int) 0, | |
0c530ab8 | 207 | LDTSZ_MIN * sizeof(struct fake_descriptor) - 1, |
1c79356b A |
208 | 0, |
209 | ACC_P|ACC_PL_K|ACC_LDT | |
210 | }; | |
0c530ab8 | 211 | |
1c79356b A |
212 | struct fake_descriptor tss_desc_pattern = { |
213 | (unsigned int) 0, | |
0c530ab8 | 214 | sizeof(struct i386_tss) - 1, |
1c79356b A |
215 | 0, |
216 | ACC_P|ACC_PL_K|ACC_TSS | |
217 | }; | |
218 | ||
219 | struct fake_descriptor cpudata_desc_pattern = { | |
220 | (unsigned int) 0, | |
221 | sizeof(cpu_data_t)-1, | |
222 | SZ_32, | |
223 | ACC_P|ACC_PL_K|ACC_DATA_W | |
224 | }; | |
225 | ||
316670eb | 226 | #if NCOPY_WINDOWS > 0 |
0c530ab8 A |
227 | struct fake_descriptor userwindow_desc_pattern = { |
228 | (unsigned int) 0, | |
229 | ((NBPDE * NCOPY_WINDOWS) / PAGE_SIZE) - 1, | |
230 | SZ_32 | SZ_G, | |
231 | ACC_P|ACC_PL_U|ACC_DATA_W | |
232 | }; | |
316670eb | 233 | #endif |
0c530ab8 A |
234 | |
235 | struct fake_descriptor physwindow_desc_pattern = { | |
236 | (unsigned int) 0, | |
237 | PAGE_SIZE - 1, | |
238 | SZ_32, | |
239 | ACC_P|ACC_PL_K|ACC_DATA_W | |
240 | }; | |
241 | ||
242 | /* | |
243 | * This is the expanded, 64-bit variant of the kernel LDT descriptor. | |
244 | * When switching to 64-bit mode this replaces KERNEL_LDT entry | |
245 | * and the following empty slot. This enables the LDT to be referenced | |
246 | * in the uber-space remapping window on the kernel. | |
247 | */ | |
248 | struct fake_descriptor64 kernel_ldt_desc64 = { | |
b0d623f7 | 249 | 0, |
0c530ab8 A |
250 | LDTSZ_MIN*sizeof(struct fake_descriptor)-1, |
251 | 0, | |
252 | ACC_P|ACC_PL_K|ACC_LDT, | |
253 | 0 | |
254 | }; | |
255 | ||
256 | /* | |
257 | * This is the expanded, 64-bit variant of the kernel TSS descriptor. | |
258 | * It is follows pattern of the KERNEL_LDT. | |
259 | */ | |
260 | struct fake_descriptor64 kernel_tss_desc64 = { | |
b0d623f7 | 261 | 0, |
0c530ab8 A |
262 | sizeof(struct x86_64_tss)-1, |
263 | 0, | |
264 | ACC_P|ACC_PL_K|ACC_TSS, | |
265 | 0 | |
266 | }; | |
267 | ||
b0d623f7 A |
268 | /* |
269 | * Convert a descriptor from fake to real format. | |
270 | * | |
271 | * Fake descriptor format: | |
272 | * bytes 0..3 base 31..0 | |
273 | * bytes 4..5 limit 15..0 | |
274 | * byte 6 access byte 2 | limit 19..16 | |
275 | * byte 7 access byte 1 | |
276 | * | |
277 | * Real descriptor format: | |
278 | * bytes 0..1 limit 15..0 | |
279 | * bytes 2..3 base 15..0 | |
280 | * byte 4 base 23..16 | |
281 | * byte 5 access byte 1 | |
282 | * byte 6 access byte 2 | limit 19..16 | |
283 | * byte 7 base 31..24 | |
284 | * | |
285 | * Fake gate format: | |
286 | * bytes 0..3 offset | |
287 | * bytes 4..5 selector | |
288 | * byte 6 word count << 4 (to match fake descriptor) | |
289 | * byte 7 access byte 1 | |
290 | * | |
291 | * Real gate format: | |
292 | * bytes 0..1 offset 15..0 | |
293 | * bytes 2..3 selector | |
294 | * byte 4 word count | |
295 | * byte 5 access byte 1 | |
296 | * bytes 6..7 offset 31..16 | |
297 | */ | |
298 | void | |
299 | fix_desc(void *d, int num_desc) { | |
300 | //early_kprintf("fix_desc(%x, %x)\n", d, num_desc); | |
301 | uint8_t *desc = (uint8_t*) d; | |
302 | ||
303 | do { | |
304 | if ((desc[7] & 0x14) == 0x04) { /* gate */ | |
305 | uint32_t offset; | |
306 | uint16_t selector; | |
307 | uint8_t wordcount; | |
308 | uint8_t acc; | |
309 | ||
310 | offset = *((uint32_t*)(desc)); | |
311 | selector = *((uint32_t*)(desc+4)); | |
312 | wordcount = desc[6] >> 4; | |
313 | acc = desc[7]; | |
314 | ||
315 | *((uint16_t*)desc) = offset & 0xFFFF; | |
316 | *((uint16_t*)(desc+2)) = selector; | |
317 | desc[4] = wordcount; | |
318 | desc[5] = acc; | |
319 | *((uint16_t*)(desc+6)) = offset >> 16; | |
320 | ||
321 | } else { /* descriptor */ | |
322 | uint32_t base; | |
323 | uint16_t limit; | |
324 | uint8_t acc1, acc2; | |
325 | ||
326 | base = *((uint32_t*)(desc)); | |
327 | limit = *((uint16_t*)(desc+4)); | |
328 | acc2 = desc[6]; | |
329 | acc1 = desc[7]; | |
330 | ||
331 | *((uint16_t*)(desc)) = limit; | |
332 | *((uint16_t*)(desc+2)) = base & 0xFFFF; | |
333 | desc[4] = (base >> 16) & 0xFF; | |
334 | desc[5] = acc1; | |
335 | desc[6] = acc2; | |
336 | desc[7] = base >> 24; | |
337 | } | |
338 | desc += 8; | |
339 | } while (--num_desc); | |
340 | } | |
341 | ||
342 | void | |
343 | fix_desc64(void *descp, int count) | |
344 | { | |
345 | struct fake_descriptor64 *fakep; | |
346 | union { | |
347 | struct real_gate64 gate; | |
348 | struct real_descriptor64 desc; | |
349 | } real; | |
350 | int i; | |
351 | ||
352 | fakep = (struct fake_descriptor64 *) descp; | |
353 | ||
354 | for (i = 0; i < count; i++, fakep++) { | |
355 | /* | |
356 | * Construct the real decriptor locally. | |
357 | */ | |
358 | ||
359 | bzero((void *) &real, sizeof(real)); | |
360 | ||
361 | switch (fakep->access & ACC_TYPE) { | |
362 | case 0: | |
363 | break; | |
364 | case ACC_CALL_GATE: | |
365 | case ACC_INTR_GATE: | |
366 | case ACC_TRAP_GATE: | |
6d2010ae | 367 | real.gate.offset_low16 = (uint16_t)(fakep->offset64 & 0xFFFF); |
b0d623f7 A |
368 | real.gate.selector16 = fakep->lim_or_seg & 0xFFFF; |
369 | real.gate.IST = fakep->size_or_IST & 0x7; | |
370 | real.gate.access8 = fakep->access; | |
6d2010ae | 371 | real.gate.offset_high16 = (uint16_t)((fakep->offset64>>16) & 0xFFFF); |
b0d623f7 A |
372 | real.gate.offset_top32 = (uint32_t)(fakep->offset64>>32); |
373 | break; | |
374 | default: /* Otherwise */ | |
375 | real.desc.limit_low16 = fakep->lim_or_seg & 0xFFFF; | |
6d2010ae A |
376 | real.desc.base_low16 = (uint16_t)(fakep->offset64 & 0xFFFF); |
377 | real.desc.base_med8 = (uint8_t)((fakep->offset64 >> 16) & 0xFF); | |
b0d623f7 A |
378 | real.desc.access8 = fakep->access; |
379 | real.desc.limit_high4 = (fakep->lim_or_seg >> 16) & 0xFF; | |
380 | real.desc.granularity4 = fakep->size_or_IST; | |
6d2010ae | 381 | real.desc.base_high8 = (uint8_t)((fakep->offset64 >> 24) & 0xFF); |
b0d623f7 A |
382 | real.desc.base_top32 = (uint32_t)(fakep->offset64>>32); |
383 | } | |
384 | ||
385 | /* | |
386 | * Now copy back over the fake structure. | |
387 | */ | |
388 | bcopy((void *) &real, (void *) fakep, sizeof(real)); | |
389 | } | |
390 | } | |
391 | ||
143464d5 A |
392 | static void |
393 | cpu_gdt_alias(vm_map_offset_t gdt, vm_map_offset_t alias) | |
394 | { | |
395 | pt_entry_t *pte = NULL; | |
396 | ||
397 | /* Require page alignment */ | |
398 | assert(page_aligned(gdt)); | |
399 | assert(page_aligned(alias)); | |
400 | ||
401 | pte = pmap_pte(kernel_pmap, alias); | |
402 | pmap_store_pte(pte, kvtophys(gdt) | INTEL_PTE_REF | |
403 | | INTEL_PTE_MOD | |
404 | | INTEL_PTE_WIRED | |
405 | | INTEL_PTE_VALID | |
406 | | INTEL_PTE_WRITE | |
407 | | INTEL_PTE_NX); | |
408 | ||
409 | /* TLB flush unneccessry because target processor isn't running yet */ | |
410 | } | |
411 | ||
0c530ab8 A |
412 | |
413 | void | |
b0d623f7 | 414 | cpu_desc_init64(cpu_data_t *cdp) |
0c530ab8 | 415 | { |
0c530ab8 A |
416 | cpu_desc_index_t *cdi = &cdp->cpu_desc_index; |
417 | ||
b0d623f7 | 418 | if (cdp == &cpu_data_master) { |
2d21ac55 A |
419 | /* |
420 | * Master CPU uses the tables built at boot time. | |
421 | * Just set the index pointers to the low memory space. | |
2d21ac55 | 422 | */ |
b0d623f7 | 423 | cdi->cdi_ktss = (void *)&master_ktss64; |
2d21ac55 | 424 | cdi->cdi_sstk = (vm_offset_t) &master_sstk.top; |
316670eb A |
425 | cdi->cdi_gdt.ptr = (void *)MASTER_GDT_ALIAS; |
426 | cdi->cdi_idt.ptr = (void *)MASTER_IDT_ALIAS; | |
b0d623f7 A |
427 | cdi->cdi_ldt = (struct fake_descriptor *) master_ldt; |
428 | ||
b0d623f7 | 429 | /* Replace the expanded LDTs and TSS slots in the GDT */ |
39236c6e | 430 | kernel_ldt_desc64.offset64 = (uintptr_t) &master_ldt; |
2d21ac55 A |
431 | *(struct fake_descriptor64 *) &master_gdt[sel_idx(KERNEL_LDT)] = |
432 | kernel_ldt_desc64; | |
b0d623f7 A |
433 | *(struct fake_descriptor64 *) &master_gdt[sel_idx(USER_LDT)] = |
434 | kernel_ldt_desc64; | |
39236c6e | 435 | kernel_tss_desc64.offset64 = (uintptr_t) &master_ktss64; |
2d21ac55 A |
436 | *(struct fake_descriptor64 *) &master_gdt[sel_idx(KERNEL_TSS)] = |
437 | kernel_tss_desc64; | |
0c530ab8 | 438 | |
b0d623f7 | 439 | /* Fix up the expanded descriptors for 64-bit. */ |
2d21ac55 A |
440 | fix_desc64((void *) &master_idt64, IDTSZ); |
441 | fix_desc64((void *) &master_gdt[sel_idx(KERNEL_LDT)], 1); | |
b0d623f7 | 442 | fix_desc64((void *) &master_gdt[sel_idx(USER_LDT)], 1); |
2d21ac55 | 443 | fix_desc64((void *) &master_gdt[sel_idx(KERNEL_TSS)], 1); |
0c530ab8 | 444 | |
2d21ac55 | 445 | /* |
39236c6e A |
446 | * Set the NMI/fault stacks as IST2/IST1 in the 64-bit TSS |
447 | * Note: this will be dynamically re-allocated in VM later. | |
2d21ac55 | 448 | */ |
39236c6e A |
449 | master_ktss64.ist2 = (uintptr_t) low_eintstack; |
450 | master_ktss64.ist1 = (uintptr_t) low_eintstack | |
451 | - sizeof(x86_64_intr_stack_frame_t); | |
0c530ab8 | 452 | |
143464d5 | 453 | } else if (cdi->cdi_ktss == NULL) { /* Skipping re-init on wake */ |
b0d623f7 | 454 | cpu_desc_table64_t *cdt = (cpu_desc_table64_t *) cdp->cpu_desc_tablep; |
143464d5 | 455 | |
2d21ac55 A |
456 | /* |
457 | * Per-cpu GDT, IDT, KTSS descriptors are allocated in kernel | |
7e4a7d39 | 458 | * heap (cpu_desc_table). |
2d21ac55 | 459 | * LDT descriptors are mapped into a separate area. |
143464d5 | 460 | * GDT descriptors are addressed by alias to avoid sgdt leaks to user-space. |
2d21ac55 | 461 | */ |
316670eb | 462 | cdi->cdi_idt.ptr = (void *)MASTER_IDT_ALIAS; |
143464d5 | 463 | cdi->cdi_gdt.ptr = (void *)CPU_GDT_ALIAS(cdp->cpu_number); |
b0d623f7 | 464 | cdi->cdi_ktss = (void *)&cdt->ktss; |
2d21ac55 A |
465 | cdi->cdi_sstk = (vm_offset_t)&cdt->sstk.top; |
466 | cdi->cdi_ldt = cdp->cpu_ldtp; | |
0c530ab8 | 467 | |
143464d5 A |
468 | /* Make the virtual alias address for the GDT */ |
469 | cpu_gdt_alias((vm_map_offset_t) &cdt->gdt, | |
470 | (vm_map_offset_t) cdi->cdi_gdt.ptr); | |
471 | ||
2d21ac55 A |
472 | /* |
473 | * Copy the tables | |
474 | */ | |
b0d623f7 A |
475 | bcopy((char *)master_gdt, (char *)cdt->gdt, sizeof(master_gdt)); |
476 | bcopy((char *)master_ldt, (char *)cdp->cpu_ldtp, sizeof(master_ldt)); | |
477 | bcopy((char *)&master_ktss64, (char *)&cdt->ktss, sizeof(struct x86_64_tss)); | |
0c530ab8 | 478 | |
2d21ac55 A |
479 | /* |
480 | * Fix up the entries in the GDT to point to | |
481 | * this LDT and this TSS. | |
482 | */ | |
39236c6e | 483 | kernel_ldt_desc64.offset64 = (uintptr_t) cdi->cdi_ldt; |
2d21ac55 A |
484 | *(struct fake_descriptor64 *) &cdt->gdt[sel_idx(KERNEL_LDT)] = |
485 | kernel_ldt_desc64; | |
486 | fix_desc64(&cdt->gdt[sel_idx(KERNEL_LDT)], 1); | |
0c530ab8 | 487 | |
39236c6e | 488 | kernel_ldt_desc64.offset64 = (uintptr_t) cdi->cdi_ldt; |
2d21ac55 A |
489 | *(struct fake_descriptor64 *) &cdt->gdt[sel_idx(USER_LDT)] = |
490 | kernel_ldt_desc64; | |
491 | fix_desc64(&cdt->gdt[sel_idx(USER_LDT)], 1); | |
0c530ab8 | 492 | |
39236c6e | 493 | kernel_tss_desc64.offset64 = (uintptr_t) cdi->cdi_ktss; |
2d21ac55 A |
494 | *(struct fake_descriptor64 *) &cdt->gdt[sel_idx(KERNEL_TSS)] = |
495 | kernel_tss_desc64; | |
496 | fix_desc64(&cdt->gdt[sel_idx(KERNEL_TSS)], 1); | |
0c530ab8 | 497 | |
39236c6e A |
498 | /* Set (zeroed) fault stack as IST1, NMI intr stack IST2 */ |
499 | bzero((void *) cdt->fstk, sizeof(cdt->fstk)); | |
500 | cdt->ktss.ist2 = (unsigned long)cdt->fstk + sizeof(cdt->fstk); | |
501 | cdt->ktss.ist1 = cdt->ktss.ist2 | |
502 | - sizeof(x86_64_intr_stack_frame_t); | |
0c530ab8 A |
503 | } |
504 | ||
505 | /* Require that the top of the sysenter stack is 16-byte aligned */ | |
506 | if ((cdi->cdi_sstk % 16) != 0) | |
507 | panic("cpu_desc_init64() sysenter stack not 16-byte aligned"); | |
508 | } | |
509 | ||
b0d623f7 A |
510 | |
511 | void | |
512 | cpu_desc_load64(cpu_data_t *cdp) | |
513 | { | |
514 | cpu_desc_index_t *cdi = &cdp->cpu_desc_index; | |
515 | ||
143464d5 A |
516 | /* Stuff the kernel per-cpu data area address into the MSRs */ |
517 | wrmsr64(MSR_IA32_GS_BASE, (uintptr_t) cdp); | |
518 | wrmsr64(MSR_IA32_KERNEL_GS_BASE, (uintptr_t) cdp); | |
519 | ||
520 | /* | |
521 | * Ensure the TSS segment's busy bit is clear. This is required | |
522 | * for the case of reloading descriptors at wake to avoid | |
523 | * their complete re-initialization. | |
524 | */ | |
525 | gdt_desc_p(KERNEL_TSS)->access &= ~ACC_TSS_BUSY; | |
526 | ||
b0d623f7 | 527 | /* Load the GDT, LDT, IDT and TSS */ |
7e4a7d39 | 528 | cdi->cdi_gdt.size = sizeof(struct real_descriptor)*GDTSZ - 1; |
b0d623f7 | 529 | cdi->cdi_idt.size = 0x1000 + cdp->cpu_number; |
316670eb A |
530 | lgdt((uintptr_t *) &cdi->cdi_gdt); |
531 | lidt((uintptr_t *) &cdi->cdi_idt); | |
b0d623f7 A |
532 | lldt(KERNEL_LDT); |
533 | set_tr(KERNEL_TSS); | |
534 | ||
b0d623f7 A |
535 | #if GPROF // Hack to enable mcount to work on K64 |
536 | __asm__ volatile("mov %0, %%gs" : : "rm" ((unsigned short)(KERNEL_DS))); | |
537 | #endif | |
b0d623f7 A |
538 | } |
539 | ||
b0d623f7 | 540 | |
0c530ab8 | 541 | /* |
b0d623f7 | 542 | * Set MSRs for sysenter/sysexit and syscall/sysret for 64-bit. |
0c530ab8 | 543 | */ |
2d21ac55 | 544 | static void |
b0d623f7 | 545 | fast_syscall_init64(__unused cpu_data_t *cdp) |
0c530ab8 A |
546 | { |
547 | wrmsr64(MSR_IA32_SYSENTER_CS, SYSENTER_CS); | |
39236c6e A |
548 | wrmsr64(MSR_IA32_SYSENTER_EIP, (uintptr_t) hi64_sysenter); |
549 | wrmsr64(MSR_IA32_SYSENTER_ESP, current_sstk()); | |
0c530ab8 A |
550 | /* Enable syscall/sysret */ |
551 | wrmsr64(MSR_IA32_EFER, rdmsr64(MSR_IA32_EFER) | MSR_IA32_EFER_SCE); | |
552 | ||
553 | /* | |
554 | * MSRs for 64-bit syscall/sysret | |
555 | * Note USER_CS because sysret uses this + 16 when returning to | |
556 | * 64-bit code. | |
557 | */ | |
39236c6e | 558 | wrmsr64(MSR_IA32_LSTAR, (uintptr_t) hi64_syscall); |
b0d623f7 A |
559 | wrmsr64(MSR_IA32_STAR, (((uint64_t)USER_CS) << 48) | |
560 | (((uint64_t)KERNEL64_CS) << 32)); | |
0c530ab8 A |
561 | /* |
562 | * Emulate eflags cleared by sysenter but note that | |
563 | * we also clear the trace trap to avoid the complications | |
2d21ac55 A |
564 | * of single-stepping into a syscall. The nested task bit |
565 | * is also cleared to avoid a spurious "task switch" | |
566 | * should we choose to return via an IRET. | |
0c530ab8 A |
567 | */ |
568 | wrmsr64(MSR_IA32_FMASK, EFL_DF|EFL_IF|EFL_TF|EFL_NT); | |
569 | ||
1c79356b A |
570 | } |
571 | ||
6d2010ae | 572 | |
91447636 A |
573 | cpu_data_t * |
574 | cpu_data_alloc(boolean_t is_boot_cpu) | |
1c79356b | 575 | { |
91447636 A |
576 | int ret; |
577 | cpu_data_t *cdp; | |
578 | ||
579 | if (is_boot_cpu) { | |
580 | assert(real_ncpus == 1); | |
316670eb | 581 | cdp = cpu_datap(0); |
91447636 | 582 | if (cdp->cpu_processor == NULL) { |
b0d623f7 | 583 | simple_lock_init(&ncpus_lock, 0); |
91447636 | 584 | cdp->cpu_processor = cpu_processor_alloc(TRUE); |
b0d623f7 | 585 | #if NCOPY_WINDOWS > 0 |
91447636 | 586 | cdp->cpu_pmap = pmap_cpu_alloc(TRUE); |
b0d623f7 | 587 | #endif |
91447636 A |
588 | } |
589 | return cdp; | |
590 | } | |
1c79356b | 591 | |
1c79356b | 592 | /* |
91447636 | 593 | * Allocate per-cpu data: |
1c79356b | 594 | */ |
3e170ce0 | 595 | ret = kmem_alloc(kernel_map, (vm_offset_t *) &cdp, sizeof(cpu_data_t), VM_KERN_MEMORY_CPU); |
91447636 A |
596 | if (ret != KERN_SUCCESS) { |
597 | printf("cpu_data_alloc() failed, ret=%d\n", ret); | |
598 | goto abort; | |
599 | } | |
600 | bzero((void*) cdp, sizeof(cpu_data_t)); | |
601 | cdp->cpu_this = cdp; | |
1c79356b A |
602 | |
603 | /* | |
91447636 | 604 | * Allocate interrupt stack: |
1c79356b | 605 | */ |
91447636 A |
606 | ret = kmem_alloc(kernel_map, |
607 | (vm_offset_t *) &cdp->cpu_int_stack_top, | |
3e170ce0 | 608 | INTSTACK_SIZE, VM_KERN_MEMORY_CPU); |
91447636 A |
609 | if (ret != KERN_SUCCESS) { |
610 | printf("cpu_data_alloc() int stack failed, ret=%d\n", ret); | |
611 | goto abort; | |
1c79356b | 612 | } |
91447636 A |
613 | bzero((void*) cdp->cpu_int_stack_top, INTSTACK_SIZE); |
614 | cdp->cpu_int_stack_top += INTSTACK_SIZE; | |
1c79356b A |
615 | |
616 | /* | |
91447636 | 617 | * Allocate descriptor table: |
1c79356b | 618 | */ |
91447636 A |
619 | ret = kmem_alloc(kernel_map, |
620 | (vm_offset_t *) &cdp->cpu_desc_tablep, | |
3e170ce0 A |
621 | sizeof(cpu_desc_table64_t), |
622 | VM_KERN_MEMORY_CPU); | |
91447636 A |
623 | if (ret != KERN_SUCCESS) { |
624 | printf("cpu_data_alloc() desc_table failed, ret=%d\n", ret); | |
625 | goto abort; | |
626 | } | |
1c79356b | 627 | |
0c530ab8 A |
628 | /* |
629 | * Allocate LDT | |
630 | */ | |
631 | ret = kmem_alloc(kernel_map, | |
632 | (vm_offset_t *) &cdp->cpu_ldtp, | |
3e170ce0 A |
633 | sizeof(struct real_descriptor) * LDTSZ, |
634 | VM_KERN_MEMORY_CPU); | |
0c530ab8 A |
635 | if (ret != KERN_SUCCESS) { |
636 | printf("cpu_data_alloc() ldt failed, ret=%d\n", ret); | |
637 | goto abort; | |
638 | } | |
639 | ||
b0d623f7 | 640 | #if CONFIG_MCA |
2d21ac55 A |
641 | /* Machine-check shadow register allocation. */ |
642 | mca_cpu_alloc(cdp); | |
b0d623f7 A |
643 | #endif |
644 | ||
645 | simple_lock(&ncpus_lock); | |
2d21ac55 | 646 | |
91447636 A |
647 | cpu_data_ptr[real_ncpus] = cdp; |
648 | cdp->cpu_number = real_ncpus; | |
649 | real_ncpus++; | |
b0d623f7 | 650 | simple_unlock(&ncpus_lock); |
0c530ab8 | 651 | |
fe8ab488 A |
652 | /* |
653 | * Before this cpu has been assigned a real thread context, | |
654 | * we give it a fake, unique, non-zero thread id which the locking | |
655 | * primitives use as their lock value. | |
656 | * Note that this does not apply to the boot processor, cpu 0, which | |
657 | * transitions to a thread context well before other processors are | |
658 | * started. | |
659 | */ | |
660 | cdp->cpu_active_thread = (thread_t) (uintptr_t) cdp->cpu_number; | |
661 | ||
6d2010ae | 662 | cdp->cpu_nanotime = &pal_rtc_nanotime_info; |
593a1d5f | 663 | |
2d21ac55 A |
664 | kprintf("cpu_data_alloc(%d) %p desc_table: %p " |
665 | "ldt: %p " | |
b0d623f7 | 666 | "int_stack: 0x%lx-0x%lx\n", |
0c530ab8 | 667 | cdp->cpu_number, cdp, cdp->cpu_desc_tablep, cdp->cpu_ldtp, |
b0d623f7 | 668 | (long)(cdp->cpu_int_stack_top - INTSTACK_SIZE), (long)(cdp->cpu_int_stack_top)); |
91447636 A |
669 | |
670 | return cdp; | |
671 | ||
672 | abort: | |
673 | if (cdp) { | |
674 | if (cdp->cpu_desc_tablep) | |
675 | kfree((void *) cdp->cpu_desc_tablep, | |
39236c6e | 676 | sizeof(cpu_desc_table64_t)); |
91447636 A |
677 | if (cdp->cpu_int_stack_top) |
678 | kfree((void *) (cdp->cpu_int_stack_top - INTSTACK_SIZE), | |
679 | INTSTACK_SIZE); | |
680 | kfree((void *) cdp, sizeof(*cdp)); | |
681 | } | |
682 | return NULL; | |
683 | } | |
1c79356b | 684 | |
6d2010ae A |
685 | boolean_t |
686 | valid_user_data_selector(uint16_t selector) | |
687 | { | |
688 | sel_t sel = selector_to_sel(selector); | |
689 | ||
690 | if (selector == 0) | |
691 | return (TRUE); | |
692 | ||
693 | if (sel.ti == SEL_LDT) | |
694 | return (TRUE); | |
695 | else if (sel.index < GDTSZ) { | |
696 | if ((gdt_desc_p(selector)->access & ACC_PL_U) == ACC_PL_U) | |
697 | return (TRUE); | |
698 | } | |
699 | ||
700 | return (FALSE); | |
701 | } | |
702 | ||
703 | boolean_t | |
704 | valid_user_code_selector(uint16_t selector) | |
705 | { | |
706 | sel_t sel = selector_to_sel(selector); | |
707 | ||
708 | if (selector == 0) | |
709 | return (FALSE); | |
710 | ||
711 | if (sel.ti == SEL_LDT) { | |
712 | if (sel.rpl == USER_PRIV) | |
713 | return (TRUE); | |
714 | } | |
715 | else if (sel.index < GDTSZ && sel.rpl == USER_PRIV) { | |
716 | if ((gdt_desc_p(selector)->access & ACC_PL_U) == ACC_PL_U) | |
717 | return (TRUE); | |
718 | } | |
719 | ||
720 | return (FALSE); | |
721 | } | |
722 | ||
723 | boolean_t | |
724 | valid_user_stack_selector(uint16_t selector) | |
725 | { | |
726 | sel_t sel = selector_to_sel(selector); | |
727 | ||
728 | if (selector == 0) | |
729 | return (FALSE); | |
730 | ||
731 | if (sel.ti == SEL_LDT) { | |
732 | if (sel.rpl == USER_PRIV) | |
733 | return (TRUE); | |
734 | } | |
735 | else if (sel.index < GDTSZ && sel.rpl == USER_PRIV) { | |
736 | if ((gdt_desc_p(selector)->access & ACC_PL_U) == ACC_PL_U) | |
737 | return (TRUE); | |
738 | } | |
739 | ||
740 | return (FALSE); | |
741 | } | |
742 | ||
91447636 A |
743 | boolean_t |
744 | valid_user_segment_selectors(uint16_t cs, | |
b0d623f7 A |
745 | uint16_t ss, |
746 | uint16_t ds, | |
747 | uint16_t es, | |
748 | uint16_t fs, | |
749 | uint16_t gs) | |
91447636 A |
750 | { |
751 | return valid_user_code_selector(cs) && | |
b0d623f7 A |
752 | valid_user_stack_selector(ss) && |
753 | valid_user_data_selector(ds) && | |
754 | valid_user_data_selector(es) && | |
755 | valid_user_data_selector(fs) && | |
756 | valid_user_data_selector(gs); | |
1c79356b A |
757 | } |
758 | ||
b0d623f7 A |
759 | #if NCOPY_WINDOWS > 0 |
760 | ||
0c530ab8 | 761 | static vm_offset_t user_window_base = 0; |
0c530ab8 A |
762 | |
763 | void | |
2d21ac55 | 764 | cpu_userwindow_init(int cpu) |
0c530ab8 A |
765 | { |
766 | cpu_data_t *cdp = cpu_data_ptr[cpu]; | |
b0d623f7 A |
767 | vm_offset_t user_window; |
768 | vm_offset_t vaddr; | |
0c530ab8 A |
769 | int num_cpus; |
770 | ||
771 | num_cpus = ml_get_max_cpus(); | |
772 | ||
773 | if (cpu >= num_cpus) | |
b0d623f7 | 774 | panic("cpu_userwindow_init: cpu > num_cpus"); |
0c530ab8 A |
775 | |
776 | if (user_window_base == 0) { | |
777 | ||
b0d623f7 A |
778 | if (vm_allocate(kernel_map, &vaddr, |
779 | (NBPDE * NCOPY_WINDOWS * num_cpus) + NBPDE, | |
3e170ce0 | 780 | VM_FLAGS_ANYWHERE | VM_MAKE_TAG(VM_KERN_MEMORY_CPU)) != KERN_SUCCESS) |
b0d623f7 A |
781 | panic("cpu_userwindow_init: " |
782 | "couldn't allocate user map window"); | |
0c530ab8 A |
783 | |
784 | /* | |
785 | * window must start on a page table boundary | |
786 | * in the virtual address space | |
787 | */ | |
788 | user_window_base = (vaddr + (NBPDE - 1)) & ~(NBPDE - 1); | |
789 | ||
790 | /* | |
791 | * get rid of any allocation leading up to our | |
792 | * starting boundary | |
793 | */ | |
794 | vm_deallocate(kernel_map, vaddr, user_window_base - vaddr); | |
795 | ||
796 | /* | |
797 | * get rid of tail that we don't need | |
798 | */ | |
799 | user_window = user_window_base + | |
800 | (NBPDE * NCOPY_WINDOWS * num_cpus); | |
801 | ||
802 | vm_deallocate(kernel_map, user_window, | |
803 | (vaddr + | |
804 | ((NBPDE * NCOPY_WINDOWS * num_cpus) + NBPDE)) - | |
805 | user_window); | |
0c530ab8 A |
806 | } |
807 | ||
b0d623f7 | 808 | user_window = user_window_base + (cpu * NCOPY_WINDOWS * NBPDE); |
0c530ab8 | 809 | |
0c530ab8 | 810 | cdp->cpu_copywindow_base = user_window; |
6d2010ae A |
811 | /* |
812 | * Abuse this pdp entry, the pdp now actually points to | |
813 | * an array of copy windows addresses. | |
814 | */ | |
0c530ab8 A |
815 | cdp->cpu_copywindow_pdp = pmap_pde(kernel_pmap, user_window); |
816 | ||
2d21ac55 | 817 | } |
0c530ab8 | 818 | |
2d21ac55 A |
819 | void |
820 | cpu_physwindow_init(int cpu) | |
821 | { | |
822 | cpu_data_t *cdp = cpu_data_ptr[cpu]; | |
c910b4d9 | 823 | vm_offset_t phys_window = cdp->cpu_physwindow_base; |
2d21ac55 | 824 | |
c910b4d9 A |
825 | if (phys_window == 0) { |
826 | if (vm_allocate(kernel_map, &phys_window, | |
3e170ce0 | 827 | PAGE_SIZE, VM_FLAGS_ANYWHERE | VM_MAKE_TAG(VM_KERN_MEMORY_CPU)) |
2d21ac55 | 828 | != KERN_SUCCESS) |
c910b4d9 A |
829 | panic("cpu_physwindow_init: " |
830 | "couldn't allocate phys map window"); | |
2d21ac55 | 831 | |
c910b4d9 A |
832 | /* |
833 | * make sure the page that encompasses the | |
834 | * pte pointer we're interested in actually | |
835 | * exists in the page table | |
836 | */ | |
316670eb | 837 | pmap_expand(kernel_pmap, phys_window, PMAP_EXPAND_OPTIONS_NONE); |
0c530ab8 | 838 | |
c910b4d9 A |
839 | cdp->cpu_physwindow_base = phys_window; |
840 | cdp->cpu_physwindow_ptep = vtopte(phys_window); | |
841 | } | |
0c530ab8 | 842 | } |
b0d623f7 | 843 | #endif /* NCOPY_WINDOWS > 0 */ |
0c530ab8 | 844 | |
0c530ab8 A |
845 | /* |
846 | * Load the segment descriptor tables for the current processor. | |
847 | */ | |
2d21ac55 A |
848 | void |
849 | cpu_mode_init(cpu_data_t *cdp) | |
850 | { | |
b0d623f7 | 851 | fast_syscall_init64(cdp); |
2d21ac55 A |
852 | } |
853 | ||
316670eb A |
854 | /* |
855 | * Allocate a new interrupt stack for the boot processor from the | |
856 | * heap rather than continue to use the statically allocated space. | |
857 | * Also switch to a dynamically allocated cpu data area. | |
858 | */ | |
859 | void | |
860 | cpu_data_realloc(void) | |
861 | { | |
862 | int ret; | |
39236c6e A |
863 | vm_offset_t istk; |
864 | vm_offset_t fstk; | |
316670eb A |
865 | cpu_data_t *cdp; |
866 | boolean_t istate; | |
867 | ||
3e170ce0 | 868 | ret = kmem_alloc(kernel_map, &istk, INTSTACK_SIZE, VM_KERN_MEMORY_CPU); |
316670eb A |
869 | if (ret != KERN_SUCCESS) { |
870 | panic("cpu_data_realloc() stack alloc, ret=%d\n", ret); | |
871 | } | |
39236c6e A |
872 | bzero((void*) istk, INTSTACK_SIZE); |
873 | istk += INTSTACK_SIZE; | |
316670eb | 874 | |
3e170ce0 | 875 | ret = kmem_alloc(kernel_map, (vm_offset_t *) &cdp, sizeof(cpu_data_t), VM_KERN_MEMORY_CPU); |
316670eb A |
876 | if (ret != KERN_SUCCESS) { |
877 | panic("cpu_data_realloc() cpu data alloc, ret=%d\n", ret); | |
878 | } | |
879 | ||
880 | /* Copy old contents into new area and make fix-ups */ | |
39236c6e A |
881 | assert(cpu_number() == 0); |
882 | bcopy((void *) cpu_data_ptr[0], (void*) cdp, sizeof(cpu_data_t)); | |
316670eb | 883 | cdp->cpu_this = cdp; |
39236c6e A |
884 | cdp->cpu_int_stack_top = istk; |
885 | timer_call_queue_init(&cdp->rtclock_timer.queue); | |
316670eb | 886 | |
39236c6e | 887 | /* Allocate the separate fault stack */ |
3e170ce0 | 888 | ret = kmem_alloc(kernel_map, &fstk, PAGE_SIZE, VM_KERN_MEMORY_CPU); |
39236c6e A |
889 | if (ret != KERN_SUCCESS) { |
890 | panic("cpu_data_realloc() fault stack alloc, ret=%d\n", ret); | |
891 | } | |
892 | bzero((void*) fstk, PAGE_SIZE); | |
893 | fstk += PAGE_SIZE; | |
316670eb A |
894 | |
895 | /* | |
896 | * With interrupts disabled commmit the new areas. | |
897 | */ | |
898 | istate = ml_set_interrupts_enabled(FALSE); | |
899 | cpu_data_ptr[0] = cdp; | |
39236c6e A |
900 | master_ktss64.ist2 = (uintptr_t) fstk; |
901 | master_ktss64.ist1 = (uintptr_t) fstk | |
902 | - sizeof(x86_64_intr_stack_frame_t); | |
316670eb A |
903 | wrmsr64(MSR_IA32_GS_BASE, (uintptr_t) cdp); |
904 | wrmsr64(MSR_IA32_KERNEL_GS_BASE, (uintptr_t) cdp); | |
905 | (void) ml_set_interrupts_enabled(istate); | |
39236c6e A |
906 | |
907 | kprintf("Reallocated master cpu data: %p," | |
908 | " interrupt stack: %p, fault stack: %p\n", | |
909 | (void *) cdp, (void *) istk, (void *) fstk); | |
316670eb | 910 | } |