]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
cb323159 | 2 | * Copyright (c) 2000-2019 Apple Inc. All rights reserved. |
1c79356b | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
0a7de745 | 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. | |
0a7de745 | 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. | |
0a7de745 | 17 | * |
2d21ac55 A |
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. | |
0a7de745 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b A |
27 | */ |
28 | /* | |
29 | * @OSF_COPYRIGHT@ | |
30 | */ | |
0a7de745 | 31 | /* |
1c79356b A |
32 | * Mach Operating System |
33 | * Copyright (c) 1991,1990 Carnegie Mellon University | |
34 | * All Rights Reserved. | |
0a7de745 | 35 | * |
1c79356b A |
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. | |
0a7de745 | 41 | * |
1c79356b A |
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. | |
0a7de745 | 45 | * |
1c79356b | 46 | * Carnegie Mellon requests users of this software to return to |
0a7de745 | 47 | * |
1c79356b A |
48 | * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU |
49 | * School of Computer Science | |
50 | * Carnegie Mellon University | |
51 | * Pittsburgh PA 15213-3890 | |
0a7de745 | 52 | * |
1c79356b A |
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 A |
60 | #include <kern/cpu_number.h> |
61 | #include <kern/cpu_data.h> | |
f427ee49 | 62 | #include <kern/percpu.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> |
5ba3f43e | 75 | #include <i386/postcode.h> |
316670eb | 76 | #include <i386/pmap_internal.h> |
b0d623f7 | 77 | #if CONFIG_MCA |
2d21ac55 | 78 | #include <i386/machine_check.h> |
b0d623f7 | 79 | #endif |
1c79356b A |
80 | |
81 | #include <kern/misc_protos.h> | |
82 | ||
5ba3f43e A |
83 | #if MONOTONIC |
84 | #include <kern/monotonic.h> | |
85 | #endif /* MONOTONIC */ | |
86 | #include <san/kasan.h> | |
87 | ||
b0d623f7 A |
88 | #define K_INTR_GATE (ACC_P|ACC_PL_K|ACC_INTR_GATE) |
89 | #define U_INTR_GATE (ACC_P|ACC_PL_U|ACC_INTR_GATE) | |
90 | ||
91 | // Declare macros that will declare the externs | |
0a7de745 A |
92 | #define TRAP(n, name) extern void *name ; |
93 | #define TRAP_ERR(n, name) extern void *name ; | |
94 | #define TRAP_SPC(n, name) extern void *name ; | |
95 | #define TRAP_IST1(n, name) extern void *name ; | |
96 | #define TRAP_IST2(n, name) extern void *name ; | |
97 | #define INTERRUPT(n) extern void *_intr_ ## n ; | |
98 | #define USER_TRAP(n, name) extern void *name ; | |
99 | #define USER_TRAP_SPC(n, name) extern void *name ; | |
b0d623f7 A |
100 | |
101 | // Include the table to declare the externs | |
102 | #include "../x86_64/idt_table.h" | |
103 | ||
104 | // Undef the macros, then redefine them so we can declare the table | |
105 | #undef TRAP | |
106 | #undef TRAP_ERR | |
107 | #undef TRAP_SPC | |
39236c6e A |
108 | #undef TRAP_IST1 |
109 | #undef TRAP_IST2 | |
b0d623f7 A |
110 | #undef INTERRUPT |
111 | #undef USER_TRAP | |
112 | #undef USER_TRAP_SPC | |
113 | ||
0a7de745 A |
114 | #define TRAP(n, name) \ |
115 | [n] = { \ | |
116 | (uintptr_t)&name, \ | |
117 | KERNEL64_CS, \ | |
118 | 0, \ | |
119 | K_INTR_GATE, \ | |
120 | 0 \ | |
b0d623f7 A |
121 | }, |
122 | ||
123 | #define TRAP_ERR TRAP | |
124 | #define TRAP_SPC TRAP | |
125 | ||
39236c6e | 126 | #define TRAP_IST1(n, name) \ |
0a7de745 A |
127 | [n] = { \ |
128 | (uintptr_t)&name, \ | |
129 | KERNEL64_CS, \ | |
130 | 1, \ | |
131 | K_INTR_GATE, \ | |
132 | 0 \ | |
b0d623f7 A |
133 | }, |
134 | ||
39236c6e | 135 | #define TRAP_IST2(n, name) \ |
0a7de745 A |
136 | [n] = { \ |
137 | (uintptr_t)&name, \ | |
138 | KERNEL64_CS, \ | |
139 | 2, \ | |
140 | K_INTR_GATE, \ | |
141 | 0 \ | |
39236c6e A |
142 | }, |
143 | ||
b0d623f7 | 144 | #define INTERRUPT(n) \ |
0a7de745 A |
145 | [n] = { \ |
146 | (uintptr_t)&_intr_ ## n,\ | |
147 | KERNEL64_CS, \ | |
148 | 0, \ | |
149 | K_INTR_GATE, \ | |
150 | 0 \ | |
b0d623f7 A |
151 | }, |
152 | ||
153 | #define USER_TRAP(n, name) \ | |
0a7de745 A |
154 | [n] = { \ |
155 | (uintptr_t)&name, \ | |
156 | KERNEL64_CS, \ | |
157 | 0, \ | |
158 | U_INTR_GATE, \ | |
159 | 0 \ | |
b0d623f7 A |
160 | }, |
161 | ||
162 | #define USER_TRAP_SPC USER_TRAP | |
b0d623f7 A |
163 | |
164 | // Declare the table using the macros we just set up | |
316670eb | 165 | struct fake_descriptor64 master_idt64[IDTSZ] |
0a7de745 A |
166 | __attribute__ ((section("__HIB,__desc"))) |
167 | __attribute__ ((aligned(PAGE_SIZE))) = { | |
b0d623f7 A |
168 | #include "../x86_64/idt_table.h" |
169 | }; | |
1c79356b | 170 | |
1c79356b A |
171 | /* |
172 | * First cpu`s interrupt stack. | |
173 | */ | |
0a7de745 A |
174 | extern uint32_t low_intstack[]; /* bottom */ |
175 | extern uint32_t low_eintstack[]; /* top */ | |
1c79356b A |
176 | |
177 | /* | |
91447636 | 178 | * Per-cpu data area pointers. |
1c79356b | 179 | */ |
5c9f4661 A |
180 | cpu_data_t cpshadows[MAX_CPUS] __attribute__((aligned(64))) __attribute__((section("__HIB, __desc"))); |
181 | cpu_data_t scdatas[MAX_CPUS] __attribute__((aligned(64))) = { | |
182 | [0].cpu_this = &scdatas[0], | |
183 | [0].cpu_nanotime = &pal_rtc_nanotime_info, | |
184 | [0].cpu_int_stack_top = (vm_offset_t) low_eintstack, | |
185 | [0].cd_shadow = &cpshadows[0] | |
b0d623f7 | 186 | }; |
5c9f4661 A |
187 | cpu_data_t *cpu_data_master = &scdatas[0]; |
188 | ||
0a7de745 | 189 | cpu_data_t *cpu_data_ptr[MAX_CPUS] = {[0] = &scdatas[0] }; |
91447636 | 190 | |
f427ee49 A |
191 | SECURITY_READ_ONLY_LATE(struct percpu_base) percpu_base; |
192 | ||
0a7de745 A |
193 | decl_simple_lock_data(, ncpus_lock); /* protects real_ncpus */ |
194 | unsigned int real_ncpus = 1; | |
195 | unsigned int max_ncpus = MAX_CPUS; | |
f427ee49 | 196 | unsigned int max_cpus_from_firmware = 0; |
1c79356b | 197 | |
2d21ac55 A |
198 | extern void hi64_sysenter(void); |
199 | extern void hi64_syscall(void); | |
0c530ab8 | 200 | |
5c9f4661 A |
201 | typedef struct { |
202 | struct real_descriptor pcldts[LDTSZ]; | |
203 | } cldt_t; | |
204 | ||
205 | cpu_desc_table64_t scdtables[MAX_CPUS] __attribute__((aligned(64))) __attribute__((section("__HIB, __desc"))); | |
206 | cpu_fault_stack_t scfstks[MAX_CPUS] __attribute__((aligned(64))) __attribute__((section("__HIB, __desc"))); | |
207 | ||
208 | cldt_t *dyn_ldts; | |
209 | ||
1c79356b A |
210 | /* |
211 | * Multiprocessor i386/i486 systems use a separate copy of the | |
212 | * GDT, IDT, LDT, and kernel TSS per processor. The first three | |
213 | * are separate to avoid lock contention: the i386 uses locked | |
214 | * memory cycles to access the descriptor tables. The TSS is | |
215 | * separate since each processor needs its own kernel stack, | |
216 | * and since using a TSS marks it busy. | |
217 | */ | |
218 | ||
1c79356b A |
219 | /* |
220 | * Allocate and initialize the per-processor descriptor tables. | |
221 | */ | |
222 | ||
0c530ab8 A |
223 | /* |
224 | * This is the expanded, 64-bit variant of the kernel LDT descriptor. | |
225 | * When switching to 64-bit mode this replaces KERNEL_LDT entry | |
226 | * and the following empty slot. This enables the LDT to be referenced | |
227 | * in the uber-space remapping window on the kernel. | |
228 | */ | |
229 | struct fake_descriptor64 kernel_ldt_desc64 = { | |
cb323159 A |
230 | .offset64 = 0, |
231 | .lim_or_seg = LDTSZ_MIN * sizeof(struct fake_descriptor) - 1, | |
232 | .size_or_IST = 0, | |
233 | .access = ACC_P | ACC_PL_K | ACC_LDT, | |
234 | .reserved = 0 | |
0c530ab8 A |
235 | }; |
236 | ||
237 | /* | |
238 | * This is the expanded, 64-bit variant of the kernel TSS descriptor. | |
239 | * It is follows pattern of the KERNEL_LDT. | |
240 | */ | |
241 | struct fake_descriptor64 kernel_tss_desc64 = { | |
cb323159 A |
242 | .offset64 = 0, |
243 | .lim_or_seg = sizeof(struct x86_64_tss) - 1, | |
244 | .size_or_IST = 0, | |
245 | .access = ACC_P | ACC_PL_K | ACC_TSS, | |
246 | .reserved = 0 | |
0c530ab8 A |
247 | }; |
248 | ||
b0d623f7 A |
249 | /* |
250 | * Convert a descriptor from fake to real format. | |
251 | * | |
252 | * Fake descriptor format: | |
253 | * bytes 0..3 base 31..0 | |
254 | * bytes 4..5 limit 15..0 | |
255 | * byte 6 access byte 2 | limit 19..16 | |
256 | * byte 7 access byte 1 | |
257 | * | |
258 | * Real descriptor format: | |
259 | * bytes 0..1 limit 15..0 | |
260 | * bytes 2..3 base 15..0 | |
261 | * byte 4 base 23..16 | |
262 | * byte 5 access byte 1 | |
263 | * byte 6 access byte 2 | limit 19..16 | |
264 | * byte 7 base 31..24 | |
265 | * | |
266 | * Fake gate format: | |
267 | * bytes 0..3 offset | |
268 | * bytes 4..5 selector | |
269 | * byte 6 word count << 4 (to match fake descriptor) | |
270 | * byte 7 access byte 1 | |
271 | * | |
272 | * Real gate format: | |
273 | * bytes 0..1 offset 15..0 | |
274 | * bytes 2..3 selector | |
275 | * byte 4 word count | |
276 | * byte 5 access byte 1 | |
277 | * bytes 6..7 offset 31..16 | |
278 | */ | |
279 | void | |
0a7de745 A |
280 | fix_desc(void *d, int num_desc) |
281 | { | |
b0d623f7 A |
282 | uint8_t *desc = (uint8_t*) d; |
283 | ||
284 | do { | |
285 | if ((desc[7] & 0x14) == 0x04) { /* gate */ | |
286 | uint32_t offset; | |
287 | uint16_t selector; | |
288 | uint8_t wordcount; | |
289 | uint8_t acc; | |
0a7de745 | 290 | |
b0d623f7 | 291 | offset = *((uint32_t*)(desc)); |
0a7de745 | 292 | selector = *((uint32_t*)(desc + 4)); |
b0d623f7 A |
293 | wordcount = desc[6] >> 4; |
294 | acc = desc[7]; | |
295 | ||
296 | *((uint16_t*)desc) = offset & 0xFFFF; | |
0a7de745 | 297 | *((uint16_t*)(desc + 2)) = selector; |
b0d623f7 A |
298 | desc[4] = wordcount; |
299 | desc[5] = acc; | |
0a7de745 | 300 | *((uint16_t*)(desc + 6)) = offset >> 16; |
b0d623f7 A |
301 | } else { /* descriptor */ |
302 | uint32_t base; | |
303 | uint16_t limit; | |
304 | uint8_t acc1, acc2; | |
305 | ||
306 | base = *((uint32_t*)(desc)); | |
0a7de745 | 307 | limit = *((uint16_t*)(desc + 4)); |
b0d623f7 A |
308 | acc2 = desc[6]; |
309 | acc1 = desc[7]; | |
310 | ||
311 | *((uint16_t*)(desc)) = limit; | |
0a7de745 | 312 | *((uint16_t*)(desc + 2)) = base & 0xFFFF; |
b0d623f7 A |
313 | desc[4] = (base >> 16) & 0xFF; |
314 | desc[5] = acc1; | |
315 | desc[6] = acc2; | |
316 | desc[7] = base >> 24; | |
317 | } | |
318 | desc += 8; | |
319 | } while (--num_desc); | |
320 | } | |
321 | ||
322 | void | |
323 | fix_desc64(void *descp, int count) | |
324 | { | |
0a7de745 | 325 | struct fake_descriptor64 *fakep; |
b0d623f7 | 326 | union { |
0a7de745 A |
327 | struct real_gate64 gate; |
328 | struct real_descriptor64 desc; | |
329 | } real; | |
330 | int i; | |
b0d623f7 A |
331 | |
332 | fakep = (struct fake_descriptor64 *) descp; | |
0a7de745 | 333 | |
b0d623f7 A |
334 | for (i = 0; i < count; i++, fakep++) { |
335 | /* | |
336 | * Construct the real decriptor locally. | |
337 | */ | |
338 | ||
339 | bzero((void *) &real, sizeof(real)); | |
340 | ||
341 | switch (fakep->access & ACC_TYPE) { | |
342 | case 0: | |
343 | break; | |
344 | case ACC_CALL_GATE: | |
345 | case ACC_INTR_GATE: | |
346 | case ACC_TRAP_GATE: | |
6d2010ae | 347 | real.gate.offset_low16 = (uint16_t)(fakep->offset64 & 0xFFFF); |
b0d623f7 A |
348 | real.gate.selector16 = fakep->lim_or_seg & 0xFFFF; |
349 | real.gate.IST = fakep->size_or_IST & 0x7; | |
350 | real.gate.access8 = fakep->access; | |
0a7de745 A |
351 | real.gate.offset_high16 = (uint16_t)((fakep->offset64 >> 16) & 0xFFFF); |
352 | real.gate.offset_top32 = (uint32_t)(fakep->offset64 >> 32); | |
b0d623f7 | 353 | break; |
0a7de745 | 354 | default: /* Otherwise */ |
b0d623f7 | 355 | real.desc.limit_low16 = fakep->lim_or_seg & 0xFFFF; |
6d2010ae A |
356 | real.desc.base_low16 = (uint16_t)(fakep->offset64 & 0xFFFF); |
357 | real.desc.base_med8 = (uint8_t)((fakep->offset64 >> 16) & 0xFF); | |
b0d623f7 A |
358 | real.desc.access8 = fakep->access; |
359 | real.desc.limit_high4 = (fakep->lim_or_seg >> 16) & 0xFF; | |
360 | real.desc.granularity4 = fakep->size_or_IST; | |
6d2010ae | 361 | real.desc.base_high8 = (uint8_t)((fakep->offset64 >> 24) & 0xFF); |
0a7de745 | 362 | real.desc.base_top32 = (uint32_t)(fakep->offset64 >> 32); |
b0d623f7 A |
363 | } |
364 | ||
365 | /* | |
366 | * Now copy back over the fake structure. | |
367 | */ | |
368 | bcopy((void *) &real, (void *) fakep, sizeof(real)); | |
369 | } | |
370 | } | |
371 | ||
5c9f4661 | 372 | extern unsigned mldtsz; |
0c530ab8 | 373 | void |
5ba3f43e | 374 | cpu_desc_init(cpu_data_t *cdp) |
0c530ab8 | 375 | { |
0a7de745 | 376 | cpu_desc_index_t *cdi = &cdp->cpu_desc_index; |
0c530ab8 | 377 | |
5c9f4661 | 378 | if (cdp == cpu_data_master) { |
2d21ac55 | 379 | /* |
5c9f4661 A |
380 | * Populate the double-mapped 'u' and base 'b' fields in the |
381 | * KTSS with I/G/LDT and sysenter stack data. | |
2d21ac55 | 382 | */ |
5c9f4661 A |
383 | cdi->cdi_ktssu = (void *)DBLMAP(&master_ktss64); |
384 | cdi->cdi_ktssb = (void *)&master_ktss64; | |
385 | cdi->cdi_sstku = (vm_offset_t) DBLMAP(&master_sstk.top); | |
386 | cdi->cdi_sstkb = (vm_offset_t) &master_sstk.top; | |
387 | ||
388 | cdi->cdi_gdtu.ptr = (void *)DBLMAP((uintptr_t) &master_gdt); | |
389 | cdi->cdi_gdtb.ptr = (void *)&master_gdt; | |
390 | cdi->cdi_idtu.ptr = (void *)DBLMAP((uintptr_t) &master_idt64); | |
391 | cdi->cdi_idtb.ptr = (void *)((uintptr_t) &master_idt64); | |
0a7de745 A |
392 | cdi->cdi_ldtu = (struct real_descriptor *)DBLMAP((uintptr_t)&master_ldt[0]); |
393 | cdi->cdi_ldtb = &master_ldt[0]; | |
b0d623f7 | 394 | |
b0d623f7 | 395 | /* Replace the expanded LDTs and TSS slots in the GDT */ |
5c9f4661 | 396 | kernel_ldt_desc64.offset64 = (uintptr_t) cdi->cdi_ldtu; |
2d21ac55 | 397 | *(struct fake_descriptor64 *) &master_gdt[sel_idx(KERNEL_LDT)] = |
0a7de745 | 398 | kernel_ldt_desc64; |
b0d623f7 | 399 | *(struct fake_descriptor64 *) &master_gdt[sel_idx(USER_LDT)] = |
0a7de745 | 400 | kernel_ldt_desc64; |
5c9f4661 | 401 | kernel_tss_desc64.offset64 = (uintptr_t) DBLMAP(&master_ktss64); |
2d21ac55 | 402 | *(struct fake_descriptor64 *) &master_gdt[sel_idx(KERNEL_TSS)] = |
0a7de745 | 403 | kernel_tss_desc64; |
0c530ab8 | 404 | |
b0d623f7 | 405 | /* Fix up the expanded descriptors for 64-bit. */ |
2d21ac55 A |
406 | fix_desc64((void *) &master_idt64, IDTSZ); |
407 | fix_desc64((void *) &master_gdt[sel_idx(KERNEL_LDT)], 1); | |
b0d623f7 | 408 | fix_desc64((void *) &master_gdt[sel_idx(USER_LDT)], 1); |
2d21ac55 | 409 | fix_desc64((void *) &master_gdt[sel_idx(KERNEL_TSS)], 1); |
0c530ab8 | 410 | |
2d21ac55 | 411 | /* |
39236c6e | 412 | * Set the NMI/fault stacks as IST2/IST1 in the 64-bit TSS |
2d21ac55 | 413 | */ |
39236c6e | 414 | master_ktss64.ist2 = (uintptr_t) low_eintstack; |
5c9f4661 | 415 | master_ktss64.ist1 = (uintptr_t) low_eintstack - sizeof(x86_64_intr_stack_frame_t); |
0a7de745 A |
416 | } else if (cdi->cdi_ktssu == NULL) { /* Skipping re-init on wake */ |
417 | cpu_desc_table64_t *cdt = (cpu_desc_table64_t *) cdp->cpu_desc_tablep; | |
143464d5 | 418 | |
5c9f4661 | 419 | cdi->cdi_idtu.ptr = (void *)DBLMAP((uintptr_t) &master_idt64); |
0c530ab8 | 420 | |
5c9f4661 A |
421 | cdi->cdi_ktssu = (void *)DBLMAP(&cdt->ktss); |
422 | cdi->cdi_ktssb = (void *)(&cdt->ktss); | |
423 | cdi->cdi_sstku = (vm_offset_t)DBLMAP(&cdt->sstk.top); | |
424 | cdi->cdi_sstkb = (vm_offset_t)(&cdt->sstk.top); | |
425 | cdi->cdi_ldtu = (void *)LDTALIAS(cdp->cpu_ldtp); | |
426 | cdi->cdi_ldtb = (void *)(cdp->cpu_ldtp); | |
143464d5 | 427 | |
2d21ac55 A |
428 | /* |
429 | * Copy the tables | |
430 | */ | |
b0d623f7 | 431 | bcopy((char *)master_gdt, (char *)cdt->gdt, sizeof(master_gdt)); |
5c9f4661 | 432 | bcopy((char *)master_ldt, (char *)cdp->cpu_ldtp, mldtsz); |
b0d623f7 | 433 | bcopy((char *)&master_ktss64, (char *)&cdt->ktss, sizeof(struct x86_64_tss)); |
5c9f4661 A |
434 | cdi->cdi_gdtu.ptr = (void *)DBLMAP(cdt->gdt); |
435 | cdi->cdi_gdtb.ptr = (void *)(cdt->gdt); | |
2d21ac55 A |
436 | /* |
437 | * Fix up the entries in the GDT to point to | |
438 | * this LDT and this TSS. | |
5c9f4661 A |
439 | * Note reuse of global 'kernel_ldt_desc64, which is not |
440 | * concurrency-safe. Higher level synchronization is expected | |
2d21ac55 | 441 | */ |
5c9f4661 | 442 | kernel_ldt_desc64.offset64 = (uintptr_t) cdi->cdi_ldtu; |
2d21ac55 | 443 | *(struct fake_descriptor64 *) &cdt->gdt[sel_idx(KERNEL_LDT)] = |
0a7de745 | 444 | kernel_ldt_desc64; |
2d21ac55 | 445 | fix_desc64(&cdt->gdt[sel_idx(KERNEL_LDT)], 1); |
0c530ab8 | 446 | |
5c9f4661 | 447 | kernel_ldt_desc64.offset64 = (uintptr_t) cdi->cdi_ldtu; |
2d21ac55 | 448 | *(struct fake_descriptor64 *) &cdt->gdt[sel_idx(USER_LDT)] = |
0a7de745 | 449 | kernel_ldt_desc64; |
2d21ac55 | 450 | fix_desc64(&cdt->gdt[sel_idx(USER_LDT)], 1); |
0c530ab8 | 451 | |
5c9f4661 | 452 | kernel_tss_desc64.offset64 = (uintptr_t) cdi->cdi_ktssu; |
2d21ac55 | 453 | *(struct fake_descriptor64 *) &cdt->gdt[sel_idx(KERNEL_TSS)] = |
0a7de745 | 454 | kernel_tss_desc64; |
2d21ac55 | 455 | fix_desc64(&cdt->gdt[sel_idx(KERNEL_TSS)], 1); |
0c530ab8 | 456 | |
39236c6e | 457 | /* Set (zeroed) fault stack as IST1, NMI intr stack IST2 */ |
5c9f4661 A |
458 | uint8_t *cfstk = &scfstks[cdp->cpu_number].fstk[0]; |
459 | cdt->fstkp = cfstk; | |
460 | bzero((void *) cfstk, FSTK_SZ); | |
461 | cdt->ktss.ist2 = DBLMAP((uint64_t)cdt->fstkp + FSTK_SZ); | |
462 | cdt->ktss.ist1 = cdt->ktss.ist2 - sizeof(x86_64_intr_stack_frame_t); | |
0c530ab8 A |
463 | } |
464 | ||
465 | /* Require that the top of the sysenter stack is 16-byte aligned */ | |
0a7de745 | 466 | if ((cdi->cdi_sstku % 16) != 0) { |
5ba3f43e | 467 | panic("cpu_desc_init() sysenter stack not 16-byte aligned"); |
0a7de745 | 468 | } |
0c530ab8 | 469 | } |
b0d623f7 | 470 | void |
5ba3f43e | 471 | cpu_desc_load(cpu_data_t *cdp) |
b0d623f7 | 472 | { |
0a7de745 | 473 | cpu_desc_index_t *cdi = &cdp->cpu_desc_index; |
b0d623f7 | 474 | |
5ba3f43e A |
475 | postcode(CPU_DESC_LOAD_ENTRY); |
476 | ||
143464d5 | 477 | /* Stuff the kernel per-cpu data area address into the MSRs */ |
5ba3f43e | 478 | postcode(CPU_DESC_LOAD_GS_BASE); |
143464d5 | 479 | wrmsr64(MSR_IA32_GS_BASE, (uintptr_t) cdp); |
5ba3f43e | 480 | postcode(CPU_DESC_LOAD_KERNEL_GS_BASE); |
143464d5 A |
481 | wrmsr64(MSR_IA32_KERNEL_GS_BASE, (uintptr_t) cdp); |
482 | ||
483 | /* | |
484 | * Ensure the TSS segment's busy bit is clear. This is required | |
485 | * for the case of reloading descriptors at wake to avoid | |
486 | * their complete re-initialization. | |
487 | */ | |
488 | gdt_desc_p(KERNEL_TSS)->access &= ~ACC_TSS_BUSY; | |
489 | ||
b0d623f7 | 490 | /* Load the GDT, LDT, IDT and TSS */ |
0a7de745 | 491 | cdi->cdi_gdtb.size = sizeof(struct real_descriptor) * GDTSZ - 1; |
5c9f4661 A |
492 | cdi->cdi_gdtu.size = cdi->cdi_gdtb.size; |
493 | cdi->cdi_idtb.size = 0x1000 + cdp->cpu_number; | |
494 | cdi->cdi_idtu.size = cdi->cdi_idtb.size; | |
495 | ||
5ba3f43e | 496 | postcode(CPU_DESC_LOAD_GDT); |
5c9f4661 | 497 | lgdt((uintptr_t *) &cdi->cdi_gdtu); |
5ba3f43e | 498 | postcode(CPU_DESC_LOAD_IDT); |
5c9f4661 | 499 | lidt((uintptr_t *) &cdi->cdi_idtu); |
5ba3f43e | 500 | postcode(CPU_DESC_LOAD_LDT); |
b0d623f7 | 501 | lldt(KERNEL_LDT); |
5ba3f43e | 502 | postcode(CPU_DESC_LOAD_TSS); |
b0d623f7 A |
503 | set_tr(KERNEL_TSS); |
504 | ||
5ba3f43e | 505 | postcode(CPU_DESC_LOAD_EXIT); |
b0d623f7 A |
506 | } |
507 | ||
0c530ab8 | 508 | /* |
b0d623f7 | 509 | * Set MSRs for sysenter/sysexit and syscall/sysret for 64-bit. |
0c530ab8 | 510 | */ |
5ba3f43e A |
511 | void |
512 | cpu_syscall_init(cpu_data_t *cdp) | |
0c530ab8 | 513 | { |
5ba3f43e | 514 | #pragma unused(cdp) |
cb323159 | 515 | |
0a7de745 | 516 | wrmsr64(MSR_IA32_SYSENTER_CS, SYSENTER_CS); |
5c9f4661 A |
517 | wrmsr64(MSR_IA32_SYSENTER_EIP, DBLMAP((uintptr_t) hi64_sysenter)); |
518 | wrmsr64(MSR_IA32_SYSENTER_ESP, current_cpu_datap()->cpu_desc_index.cdi_sstku); | |
0c530ab8 A |
519 | /* Enable syscall/sysret */ |
520 | wrmsr64(MSR_IA32_EFER, rdmsr64(MSR_IA32_EFER) | MSR_IA32_EFER_SCE); | |
521 | ||
522 | /* | |
523 | * MSRs for 64-bit syscall/sysret | |
524 | * Note USER_CS because sysret uses this + 16 when returning to | |
525 | * 64-bit code. | |
526 | */ | |
5c9f4661 A |
527 | wrmsr64(MSR_IA32_LSTAR, DBLMAP((uintptr_t) hi64_syscall)); |
528 | wrmsr64(MSR_IA32_STAR, (((uint64_t)USER_CS) << 48) | (((uint64_t)KERNEL64_CS) << 32)); | |
0c530ab8 A |
529 | /* |
530 | * Emulate eflags cleared by sysenter but note that | |
531 | * we also clear the trace trap to avoid the complications | |
2d21ac55 A |
532 | * of single-stepping into a syscall. The nested task bit |
533 | * is also cleared to avoid a spurious "task switch" | |
534 | * should we choose to return via an IRET. | |
0c530ab8 | 535 | */ |
0a7de745 | 536 | wrmsr64(MSR_IA32_FMASK, EFL_DF | EFL_IF | EFL_TF | EFL_NT); |
1c79356b | 537 | } |
5c9f4661 A |
538 | extern vm_offset_t dyn_dblmap(vm_offset_t, vm_offset_t); |
539 | uint64_t ldt_alias_offset; | |
6d2010ae | 540 | |
f427ee49 A |
541 | __startup_func |
542 | static void | |
543 | cpu_data_startup_init(void) | |
544 | { | |
545 | int flags = KMA_GUARD_FIRST | KMA_GUARD_LAST | KMA_PERMANENT | | |
546 | KMA_ZERO | KMA_KOBJECT; | |
547 | uint32_t cpus = max_cpus_from_firmware; | |
548 | vm_size_t size = percpu_section_size() * cpus; | |
549 | kern_return_t kr; | |
550 | ||
551 | percpu_base.size = percpu_section_size(); | |
552 | if (cpus == 0) { | |
553 | panic("percpu: max_cpus_from_firmware not yet initialized"); | |
554 | } | |
555 | if (cpus == 1) { | |
556 | percpu_base.start = VM_MAX_KERNEL_ADDRESS; | |
557 | return; | |
558 | } | |
559 | ||
560 | kr = kmem_alloc_flags(kernel_map, &percpu_base.start, | |
561 | round_page(size) + 2 * PAGE_SIZE, VM_KERN_MEMORY_CPU, flags); | |
562 | if (kr != KERN_SUCCESS) { | |
563 | panic("percpu: kmem_alloc failed (%d)", kr); | |
564 | } | |
565 | ||
566 | percpu_base.start += PAGE_SIZE - percpu_section_start(); | |
567 | percpu_base.end = percpu_base.start + size - 1; | |
568 | } | |
569 | STARTUP(PERCPU, STARTUP_RANK_FIRST, cpu_data_startup_init); | |
570 | ||
91447636 A |
571 | cpu_data_t * |
572 | cpu_data_alloc(boolean_t is_boot_cpu) | |
1c79356b | 573 | { |
0a7de745 A |
574 | int ret; |
575 | cpu_data_t *cdp; | |
91447636 A |
576 | |
577 | if (is_boot_cpu) { | |
578 | assert(real_ncpus == 1); | |
316670eb | 579 | cdp = cpu_datap(0); |
91447636 | 580 | if (cdp->cpu_processor == NULL) { |
b0d623f7 | 581 | simple_lock_init(&ncpus_lock, 0); |
f427ee49 | 582 | cdp->cpu_processor = PERCPU_GET_MASTER(processor); |
91447636 A |
583 | } |
584 | return cdp; | |
585 | } | |
1c79356b | 586 | |
5c9f4661 | 587 | boolean_t do_ldt_alloc = FALSE; |
0a7de745 | 588 | simple_lock(&ncpus_lock, LCK_GRP_NULL); |
5c9f4661 A |
589 | int cnum = real_ncpus; |
590 | real_ncpus++; | |
591 | if (dyn_ldts == NULL) { | |
592 | do_ldt_alloc = TRUE; | |
593 | } | |
594 | simple_unlock(&ncpus_lock); | |
595 | ||
1c79356b | 596 | /* |
91447636 | 597 | * Allocate per-cpu data: |
1c79356b | 598 | */ |
5c9f4661 A |
599 | |
600 | cdp = &scdatas[cnum]; | |
91447636 A |
601 | bzero((void*) cdp, sizeof(cpu_data_t)); |
602 | cdp->cpu_this = cdp; | |
5c9f4661 A |
603 | cdp->cpu_number = cnum; |
604 | cdp->cd_shadow = &cpshadows[cnum]; | |
f427ee49 A |
605 | cdp->cpu_pcpu_base = percpu_base.start + (cnum - 1) * percpu_section_size(); |
606 | cdp->cpu_processor = PERCPU_GET_WITH_BASE(cdp->cpu_pcpu_base, processor); | |
607 | ||
1c79356b | 608 | /* |
91447636 | 609 | * Allocate interrupt stack: |
1c79356b | 610 | */ |
0a7de745 A |
611 | ret = kmem_alloc(kernel_map, |
612 | (vm_offset_t *) &cdp->cpu_int_stack_top, | |
613 | INTSTACK_SIZE, VM_KERN_MEMORY_CPU); | |
91447636 | 614 | if (ret != KERN_SUCCESS) { |
5c9f4661 | 615 | panic("cpu_data_alloc() int stack failed, ret=%d\n", ret); |
1c79356b | 616 | } |
91447636 A |
617 | bzero((void*) cdp->cpu_int_stack_top, INTSTACK_SIZE); |
618 | cdp->cpu_int_stack_top += INTSTACK_SIZE; | |
1c79356b A |
619 | |
620 | /* | |
91447636 | 621 | * Allocate descriptor table: |
1c79356b A |
622 | */ |
623 | ||
5c9f4661 | 624 | cdp->cpu_desc_tablep = (struct cpu_desc_table *) &scdtables[cnum]; |
0c530ab8 A |
625 | /* |
626 | * Allocate LDT | |
627 | */ | |
5c9f4661 A |
628 | if (do_ldt_alloc) { |
629 | boolean_t do_ldt_free = FALSE; | |
630 | vm_offset_t sldtoffset = 0; | |
631 | /* | |
632 | * Allocate LDT | |
633 | */ | |
634 | vm_offset_t ldtalloc = 0, ldtallocsz = round_page_64(MAX_CPUS * sizeof(struct real_descriptor) * LDTSZ); | |
635 | ret = kmem_alloc(kernel_map, (vm_offset_t *) &ldtalloc, ldtallocsz, VM_KERN_MEMORY_CPU); | |
636 | if (ret != KERN_SUCCESS) { | |
637 | panic("cpu_data_alloc() ldt failed, kmem_alloc=%d\n", ret); | |
638 | } | |
639 | ||
0a7de745 | 640 | simple_lock(&ncpus_lock, LCK_GRP_NULL); |
5c9f4661 A |
641 | if (dyn_ldts == NULL) { |
642 | dyn_ldts = (cldt_t *)ldtalloc; | |
643 | } else { | |
644 | do_ldt_free = TRUE; | |
645 | } | |
646 | simple_unlock(&ncpus_lock); | |
647 | ||
648 | if (do_ldt_free) { | |
649 | kmem_free(kernel_map, ldtalloc, ldtallocsz); | |
650 | } else { | |
651 | /* CPU registration and startup are expected to execute | |
652 | * serially, as invoked by the platform driver. | |
653 | * Create trampoline alias of LDT region. | |
654 | */ | |
655 | sldtoffset = dyn_dblmap(ldtalloc, ldtallocsz); | |
656 | ldt_alias_offset = sldtoffset; | |
657 | } | |
0c530ab8 | 658 | } |
5c9f4661 | 659 | cdp->cpu_ldtp = &dyn_ldts[cnum].pcldts[0]; |
0c530ab8 | 660 | |
b0d623f7 | 661 | #if CONFIG_MCA |
2d21ac55 A |
662 | /* Machine-check shadow register allocation. */ |
663 | mca_cpu_alloc(cdp); | |
b0d623f7 A |
664 | #endif |
665 | ||
fe8ab488 A |
666 | /* |
667 | * Before this cpu has been assigned a real thread context, | |
668 | * we give it a fake, unique, non-zero thread id which the locking | |
669 | * primitives use as their lock value. | |
670 | * Note that this does not apply to the boot processor, cpu 0, which | |
671 | * transitions to a thread context well before other processors are | |
672 | * started. | |
673 | */ | |
674 | cdp->cpu_active_thread = (thread_t) (uintptr_t) cdp->cpu_number; | |
d9a64523 | 675 | cdp->cpu_NMI_acknowledged = TRUE; |
6d2010ae | 676 | cdp->cpu_nanotime = &pal_rtc_nanotime_info; |
593a1d5f | 677 | |
2d21ac55 | 678 | kprintf("cpu_data_alloc(%d) %p desc_table: %p " |
0a7de745 A |
679 | "ldt: %p " |
680 | "int_stack: 0x%lx-0x%lx\n", | |
681 | cdp->cpu_number, cdp, cdp->cpu_desc_tablep, cdp->cpu_ldtp, | |
682 | (long)(cdp->cpu_int_stack_top - INTSTACK_SIZE), (long)(cdp->cpu_int_stack_top)); | |
5c9f4661 | 683 | cpu_data_ptr[cnum] = cdp; |
91447636 A |
684 | |
685 | return cdp; | |
91447636 | 686 | } |
1c79356b | 687 | |
6d2010ae A |
688 | boolean_t |
689 | valid_user_data_selector(uint16_t selector) | |
690 | { | |
0a7de745 A |
691 | sel_t sel = selector_to_sel(selector); |
692 | ||
693 | if (selector == 0) { | |
694 | return TRUE; | |
695 | } | |
696 | ||
697 | if (sel.ti == SEL_LDT) { | |
698 | return TRUE; | |
699 | } else if (sel.index < GDTSZ) { | |
700 | if ((gdt_desc_p(selector)->access & ACC_PL_U) == ACC_PL_U) { | |
701 | return TRUE; | |
702 | } | |
703 | } | |
704 | return FALSE; | |
6d2010ae A |
705 | } |
706 | ||
707 | boolean_t | |
708 | valid_user_code_selector(uint16_t selector) | |
709 | { | |
0a7de745 A |
710 | sel_t sel = selector_to_sel(selector); |
711 | ||
712 | if (selector == 0) { | |
713 | return FALSE; | |
714 | } | |
6d2010ae | 715 | |
0a7de745 A |
716 | if (sel.ti == SEL_LDT) { |
717 | if (sel.rpl == USER_PRIV) { | |
718 | return TRUE; | |
719 | } | |
720 | } else if (sel.index < GDTSZ && sel.rpl == USER_PRIV) { | |
721 | if ((gdt_desc_p(selector)->access & ACC_PL_U) == ACC_PL_U) { | |
722 | return TRUE; | |
723 | } | |
724 | /* Explicitly validate the system code selectors | |
725 | * even if not instantaneously privileged, | |
726 | * since they are dynamically re-privileged | |
727 | * at context switch | |
728 | */ | |
729 | if ((selector == USER_CS) || (selector == USER64_CS)) { | |
730 | return TRUE; | |
731 | } | |
732 | } | |
733 | ||
734 | return FALSE; | |
6d2010ae A |
735 | } |
736 | ||
737 | boolean_t | |
738 | valid_user_stack_selector(uint16_t selector) | |
739 | { | |
0a7de745 A |
740 | sel_t sel = selector_to_sel(selector); |
741 | ||
742 | if (selector == 0) { | |
743 | return FALSE; | |
744 | } | |
745 | ||
746 | if (sel.ti == SEL_LDT) { | |
747 | if (sel.rpl == USER_PRIV) { | |
748 | return TRUE; | |
749 | } | |
750 | } else if (sel.index < GDTSZ && sel.rpl == USER_PRIV) { | |
751 | if ((gdt_desc_p(selector)->access & ACC_PL_U) == ACC_PL_U) { | |
752 | return TRUE; | |
753 | } | |
754 | } | |
755 | ||
756 | return FALSE; | |
6d2010ae A |
757 | } |
758 | ||
91447636 A |
759 | boolean_t |
760 | valid_user_segment_selectors(uint16_t cs, | |
0a7de745 A |
761 | uint16_t ss, |
762 | uint16_t ds, | |
763 | uint16_t es, | |
764 | uint16_t fs, | |
765 | uint16_t gs) | |
766 | { | |
767 | return valid_user_code_selector(cs) && | |
768 | valid_user_stack_selector(ss) && | |
769 | valid_user_data_selector(ds) && | |
770 | valid_user_data_selector(es) && | |
771 | valid_user_data_selector(fs) && | |
772 | valid_user_data_selector(gs); | |
1c79356b A |
773 | } |
774 | ||
316670eb A |
775 | /* |
776 | * Allocate a new interrupt stack for the boot processor from the | |
777 | * heap rather than continue to use the statically allocated space. | |
778 | * Also switch to a dynamically allocated cpu data area. | |
779 | */ | |
780 | void | |
781 | cpu_data_realloc(void) | |
782 | { | |
0a7de745 A |
783 | int ret; |
784 | vm_offset_t istk; | |
785 | cpu_data_t *cdp; | |
786 | boolean_t istate; | |
316670eb | 787 | |
3e170ce0 | 788 | ret = kmem_alloc(kernel_map, &istk, INTSTACK_SIZE, VM_KERN_MEMORY_CPU); |
316670eb A |
789 | if (ret != KERN_SUCCESS) { |
790 | panic("cpu_data_realloc() stack alloc, ret=%d\n", ret); | |
791 | } | |
39236c6e A |
792 | bzero((void*) istk, INTSTACK_SIZE); |
793 | istk += INTSTACK_SIZE; | |
316670eb | 794 | |
5c9f4661 | 795 | cdp = &scdatas[0]; |
316670eb A |
796 | |
797 | /* Copy old contents into new area and make fix-ups */ | |
39236c6e A |
798 | assert(cpu_number() == 0); |
799 | bcopy((void *) cpu_data_ptr[0], (void*) cdp, sizeof(cpu_data_t)); | |
316670eb | 800 | cdp->cpu_this = cdp; |
39236c6e A |
801 | cdp->cpu_int_stack_top = istk; |
802 | timer_call_queue_init(&cdp->rtclock_timer.queue); | |
5c9f4661 | 803 | cdp->cpu_desc_tablep = (struct cpu_desc_table *) &scdtables[0]; |
0a7de745 | 804 | cpu_desc_table64_t *cdt = (cpu_desc_table64_t *) cdp->cpu_desc_tablep; |
316670eb | 805 | |
5c9f4661 A |
806 | uint8_t *cfstk = &scfstks[cdp->cpu_number].fstk[0]; |
807 | cdt->fstkp = cfstk; | |
808 | cfstk += FSTK_SZ; | |
316670eb A |
809 | |
810 | /* | |
811 | * With interrupts disabled commmit the new areas. | |
812 | */ | |
813 | istate = ml_set_interrupts_enabled(FALSE); | |
814 | cpu_data_ptr[0] = cdp; | |
5c9f4661 A |
815 | master_ktss64.ist2 = DBLMAP((uintptr_t) cfstk); |
816 | master_ktss64.ist1 = DBLMAP((uintptr_t) cfstk - sizeof(x86_64_intr_stack_frame_t)); | |
316670eb A |
817 | wrmsr64(MSR_IA32_GS_BASE, (uintptr_t) cdp); |
818 | wrmsr64(MSR_IA32_KERNEL_GS_BASE, (uintptr_t) cdp); | |
819 | (void) ml_set_interrupts_enabled(istate); | |
39236c6e A |
820 | |
821 | kprintf("Reallocated master cpu data: %p," | |
0a7de745 A |
822 | " interrupt stack: %p, fault stack: %p\n", |
823 | (void *) cdp, (void *) istk, (void *) cfstk); | |
316670eb | 824 | } |