]> git.saurik.com Git - apple/xnu.git/blame - osfmk/i386/pcb_native.c
xnu-7195.101.1.tar.gz
[apple/xnu.git] / osfmk / i386 / pcb_native.c
CommitLineData
6d2010ae 1/*
f427ee49 2 * Copyright (c) 2000-2020 Apple Inc. All rights reserved.
6d2010ae
A
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
0a7de745 5 *
6d2010ae
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 *
6d2010ae
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 *
6d2010ae
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
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
0a7de745 25 *
6d2010ae
A
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28/*
29 * @OSF_COPYRIGHT@
30 */
0a7de745 31/*
6d2010ae
A
32 * Mach Operating System
33 * Copyright (c) 1991,1990 Carnegie Mellon University
34 * All Rights Reserved.
0a7de745 35 *
6d2010ae
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 *
6d2010ae
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 *
6d2010ae 46 * Carnegie Mellon requests users of this software to return to
0a7de745 47 *
6d2010ae
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 *
6d2010ae
A
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
55 */
56
6d2010ae
A
57#include <mach_debug.h>
58#include <mach_ldebug.h>
59
60#include <sys/kdebug.h>
61
62#include <mach/kern_return.h>
63#include <mach/thread_status.h>
64#include <mach/vm_param.h>
65
6d2010ae
A
66#include <kern/mach_param.h>
67#include <kern/processor.h>
68#include <kern/cpu_data.h>
69#include <kern/cpu_number.h>
70#include <kern/task.h>
71#include <kern/thread.h>
72#include <kern/sched_prim.h>
73#include <kern/misc_protos.h>
74#include <kern/assert.h>
75#include <kern/spl.h>
76#include <kern/machine.h>
77#include <ipc/ipc_port.h>
78#include <vm/vm_kern.h>
79#include <vm/vm_map.h>
80#include <vm/pmap.h>
81#include <vm/vm_protos.h>
82
83#include <i386/commpage/commpage.h>
84#include <i386/cpu_data.h>
85#include <i386/cpu_number.h>
0a7de745 86#include <i386/cpuid.h>
6d2010ae
A
87#include <i386/eflags.h>
88#include <i386/proc_reg.h>
89#include <i386/tss.h>
90#include <i386/user_ldt.h>
91#include <i386/fpu.h>
92#include <i386/mp_desc.h>
93#include <i386/misc_protos.h>
94#include <i386/thread.h>
6d2010ae
A
95#include <i386/seg.h>
96#include <i386/machine_routines.h>
97
fe8ab488
A
98#if HYPERVISOR
99#include <kern/hv_support.h>
100#endif
101
0a7de745
A
102#define ASSERT_IS_16BYTE_MULTIPLE_SIZEOF(_type_) \
103extern char assert_is_16byte_multiple_sizeof_ ## _type_ \
104 [(sizeof(_type_) % 16) == 0 ? 1 : -1]
6d2010ae
A
105
106/* Compile-time checks for vital save area sizing: */
107ASSERT_IS_16BYTE_MULTIPLE_SIZEOF(x86_64_intr_stack_frame_t);
6d2010ae
A
108ASSERT_IS_16BYTE_MULTIPLE_SIZEOF(x86_saved_state_t);
109
110#define DIRECTION_FLAG_DEBUG (DEBUG | DEVELOPMENT)
111
0a7de745
A
112extern zone_t iss_zone; /* zone for saved_state area */
113extern zone_t ids_zone; /* zone for debug_state area */
114extern int tecs_mode_supported;
f427ee49
A
115extern boolean_t cpuid_tsx_supported;
116
117bool lbr_need_tsx_workaround = false;
0a7de745
A
118
119int force_thread_policy_tecs;
6d2010ae 120
f427ee49
A
121struct lbr_group {
122 uint32_t msr_from;
123 uint32_t msr_to;
124 uint32_t msr_info;
125};
126
127struct cpu_lbrs {
128 uint32_t lbr_count;
129 struct lbr_group msr_lbrs[X86_MAX_LBRS];
130};
131
132const struct cpu_lbrs *cpu_lbr_setp = NULL;
133int cpu_lbr_type;
134
135const struct cpu_lbrs nhm_cpu_lbrs = {
136 16 /* LBR count */,
137 {
138 { 0x680 /* FROM_0 */, 0x6c0 /* TO_0 */, 0 /* INFO_0 */ },
139 { 0x681 /* FROM_1 */, 0x6c1 /* TO_1 */, 0 /* INFO_1 */ },
140 { 0x682 /* FROM_2 */, 0x6c2 /* TO_2 */, 0 /* INFO_2 */ },
141 { 0x683 /* FROM_3 */, 0x6c3 /* TO_3 */, 0 /* INFO_3 */ },
142 { 0x684 /* FROM_4 */, 0x6c4 /* TO_4 */, 0 /* INFO_4 */ },
143 { 0x685 /* FROM_5 */, 0x6c5 /* TO_5 */, 0 /* INFO_5 */ },
144 { 0x686 /* FROM_6 */, 0x6c6 /* TO_6 */, 0 /* INFO_6 */ },
145 { 0x687 /* FROM_7 */, 0x6c7 /* TO_7 */, 0 /* INFO_7 */ },
146 { 0x688 /* FROM_8 */, 0x6c8 /* TO_8 */, 0 /* INFO_8 */ },
147 { 0x689 /* FROM_9 */, 0x6c9 /* TO_9 */, 0 /* INFO_9 */ },
148 { 0x68A /* FROM_10 */, 0x6ca /* TO_10 */, 0 /* INFO_10 */ },
149 { 0x68B /* FROM_11 */, 0x6cb /* TO_11 */, 0 /* INFO_11 */ },
150 { 0x68C /* FROM_12 */, 0x6cc /* TO_12 */, 0 /* INFO_12 */ },
151 { 0x68D /* FROM_13 */, 0x6cd /* TO_13 */, 0 /* INFO_13 */ },
152 { 0x68E /* FROM_14 */, 0x6ce /* TO_14 */, 0 /* INFO_14 */ },
153 { 0x68F /* FROM_15 */, 0x6cf /* TO_15 */, 0 /* INFO_15 */ }
154 }
155},
156 skl_cpu_lbrs = {
157 32 /* LBR count */,
158 {
159 { 0x680 /* FROM_0 */, 0x6c0 /* TO_0 */, 0xdc0 /* INFO_0 */ },
160 { 0x681 /* FROM_1 */, 0x6c1 /* TO_1 */, 0xdc1 /* INFO_1 */ },
161 { 0x682 /* FROM_2 */, 0x6c2 /* TO_2 */, 0xdc2 /* INFO_2 */ },
162 { 0x683 /* FROM_3 */, 0x6c3 /* TO_3 */, 0xdc3 /* INFO_3 */ },
163 { 0x684 /* FROM_4 */, 0x6c4 /* TO_4 */, 0xdc4 /* INFO_4 */ },
164 { 0x685 /* FROM_5 */, 0x6c5 /* TO_5 */, 0xdc5 /* INFO_5 */ },
165 { 0x686 /* FROM_6 */, 0x6c6 /* TO_6 */, 0xdc6 /* INFO_6 */ },
166 { 0x687 /* FROM_7 */, 0x6c7 /* TO_7 */, 0xdc7 /* INFO_7 */ },
167 { 0x688 /* FROM_8 */, 0x6c8 /* TO_8 */, 0xdc8 /* INFO_8 */ },
168 { 0x689 /* FROM_9 */, 0x6c9 /* TO_9 */, 0xdc9 /* INFO_9 */ },
169 { 0x68A /* FROM_10 */, 0x6ca /* TO_10 */, 0xdca /* INFO_10 */ },
170 { 0x68B /* FROM_11 */, 0x6cb /* TO_11 */, 0xdcb /* INFO_11 */ },
171 { 0x68C /* FROM_12 */, 0x6cc /* TO_12 */, 0xdcc /* INFO_12 */ },
172 { 0x68D /* FROM_13 */, 0x6cd /* TO_13 */, 0xdcd /* INFO_13 */ },
173 { 0x68E /* FROM_14 */, 0x6ce /* TO_14 */, 0xdce /* INFO_14 */ },
174 { 0x68F /* FROM_15 */, 0x6cf /* TO_15 */, 0xdcf /* INFO_15 */ },
175 { 0x690 /* FROM_16 */, 0x6d0 /* TO_16 */, 0xdd0 /* INFO_16 */ },
176 { 0x691 /* FROM_17 */, 0x6d1 /* TO_17 */, 0xdd1 /* INFO_17 */ },
177 { 0x692 /* FROM_18 */, 0x6d2 /* TO_18 */, 0xdd2 /* INFO_18 */ },
178 { 0x693 /* FROM_19 */, 0x6d3 /* TO_19 */, 0xdd3 /* INFO_19 */ },
179 { 0x694 /* FROM_20 */, 0x6d4 /* TO_20 */, 0xdd4 /* INFO_20 */ },
180 { 0x695 /* FROM_21 */, 0x6d5 /* TO_21 */, 0xdd5 /* INFO_21 */ },
181 { 0x696 /* FROM_22 */, 0x6d6 /* TO_22 */, 0xdd6 /* INFO_22 */ },
182 { 0x697 /* FROM_23 */, 0x6d7 /* TO_23 */, 0xdd7 /* INFO_23 */ },
183 { 0x698 /* FROM_24 */, 0x6d8 /* TO_24 */, 0xdd8 /* INFO_24 */ },
184 { 0x699 /* FROM_25 */, 0x6d9 /* TO_25 */, 0xdd9 /* INFO_25 */ },
185 { 0x69a /* FROM_26 */, 0x6da /* TO_26 */, 0xdda /* INFO_26 */ },
186 { 0x69b /* FROM_27 */, 0x6db /* TO_27 */, 0xddb /* INFO_27 */ },
187 { 0x69c /* FROM_28 */, 0x6dc /* TO_28 */, 0xddc /* INFO_28 */ },
188 { 0x69d /* FROM_29 */, 0x6dd /* TO_29 */, 0xddd /* INFO_29 */ },
189 { 0x69e /* FROM_30 */, 0x6de /* TO_30 */, 0xdde /* INFO_30 */ },
190 { 0x69f /* FROM_31 */, 0x6df /* TO_31 */, 0xddf /* INFO_31 */ }
191 }
192};
193
194void
195i386_lbr_disable(void)
196{
197 /* Enable LBRs */
198 wrmsr64(MSR_IA32_DEBUGCTLMSR, rdmsr64(MSR_IA32_DEBUGCTLMSR) & ~DEBUGCTL_LBR_ENA);
199}
200
201/*
202 * Disable ASAN for i386_lbr_enable and i386_lbr_init, otherwise we get a KASAN panic
203 * because the shadow map is not been initialized when these functions are called in
204 * early boot.
205 */
206void __attribute__((no_sanitize("address")))
207i386_lbr_enable(void)
208{
209 if (last_branch_support_enabled) {
210 /* Enable LBRs */
211 wrmsr64(MSR_IA32_DEBUGCTLMSR, rdmsr64(MSR_IA32_DEBUGCTLMSR) | DEBUGCTL_LBR_ENA);
212 }
213}
214
215void __attribute__((no_sanitize("address")))
216i386_lbr_init(i386_cpu_info_t *info_p, bool is_master)
217{
218 if (!last_branch_support_enabled) {
219 i386_lbr_disable();
220 return;
221 }
222
223 if (is_master) {
224 /* All NHM+ CPUs support PERF_CAPABILITIES, so no need to check cpuid for its presence */
225 cpu_lbr_type = PERFCAP_LBR_TYPE(rdmsr64(MSR_IA32_PERF_CAPABILITIES));
226
227 switch (info_p->cpuid_cpufamily) {
228 case CPUFAMILY_INTEL_NEHALEM:
229 case CPUFAMILY_INTEL_WESTMERE:
230 /* NHM family shares an LBR_SELECT MSR for both logical CPUs per core */
231 cpu_lbr_setp = &nhm_cpu_lbrs;
232 break;
233
234 case CPUFAMILY_INTEL_SANDYBRIDGE:
235 case CPUFAMILY_INTEL_IVYBRIDGE:
236 /* SNB+ has dedicated LBR_SELECT MSRs for each logical CPU per core */
237 cpu_lbr_setp = &nhm_cpu_lbrs;
238 break;
239
240 case CPUFAMILY_INTEL_HASWELL:
241 case CPUFAMILY_INTEL_BROADWELL:
242 lbr_need_tsx_workaround = cpuid_tsx_supported ? false : true;
243 cpu_lbr_setp = &nhm_cpu_lbrs;
244 break;
245
246 case CPUFAMILY_INTEL_SKYLAKE:
247 case CPUFAMILY_INTEL_KABYLAKE:
248 case CPUFAMILY_INTEL_ICELAKE:
249 cpu_lbr_setp = &skl_cpu_lbrs;
250 break;
251
252 default:
253 panic("Unknown CPU family");
254 }
255 }
256
257 /* Configure LBR_SELECT for CPL > 0 records only */
258 wrmsr64(MSR_IA32_LBR_SELECT, LBR_SELECT_CPL_EQ_0);
259
260 /* Enable LBRs */
261 wrmsr64(MSR_IA32_DEBUGCTLMSR, rdmsr64(MSR_IA32_DEBUGCTLMSR) | DEBUGCTL_LBR_ENA);
262}
263
264int
265i386_lbr_native_state_to_mach_thread_state(pcb_t pcb, last_branch_state_t *machlbrp)
266{
267 int last_entry;
268 int i, j, lbr_tos;
269 uint64_t from_rip, to_rip;
270#define LBR_SENTINEL_KERNEL_MODE (0x66726d6b65726e6cULL /* "frmkernl" */ )
271
272 machlbrp->lbr_count = cpu_lbr_setp->lbr_count;
273 lbr_tos = pcb->lbrs.lbr_tos & (X86_MAX_LBRS - 1);
274 last_entry = (lbr_tos == (cpu_lbr_setp->lbr_count - 1)) ? 0 : (lbr_tos + 1);
275
276 switch (cpu_lbr_type) {
277 case PERFCAP_LBR_TYPE_MISPRED: /* NHM */
278
279 machlbrp->lbr_supported_tsx = 0;
280 machlbrp->lbr_supported_cycle_count = 0;
281 for (j = 0, i = lbr_tos;; (i = (i == 0) ? (cpu_lbr_setp->lbr_count - 1) : (i - 1)), j++) {
282 to_rip = pcb->lbrs.lbrs[i].to_rip;
283 machlbrp->lbrs[j].to_ip = (to_rip > VM_MAX_USER_PAGE_ADDRESS) ? LBR_SENTINEL_KERNEL_MODE : to_rip;
284 from_rip = LBR_TYPE_MISPRED_FROMRIP(pcb->lbrs.lbrs[i].from_rip);
285 machlbrp->lbrs[j].from_ip = (from_rip > VM_MAX_USER_PAGE_ADDRESS) ? LBR_SENTINEL_KERNEL_MODE : from_rip;
286 machlbrp->lbrs[j].mispredict = LBR_TYPE_MISPRED_MISPREDICT(pcb->lbrs.lbrs[i].from_rip);
287 machlbrp->lbrs[j].tsx_abort = machlbrp->lbrs[j].in_tsx = 0; /* Not Supported */
288 if (i == last_entry) {
289 break;
290 }
291 }
292 break;
293
294 case PERFCAP_LBR_TYPE_TSXINFO: /* HSW/BDW */
295
296 machlbrp->lbr_supported_tsx = cpuid_tsx_supported ? 1 : 0;
297 machlbrp->lbr_supported_cycle_count = 0;
298 for (j = 0, i = lbr_tos;; (i = (i == 0) ? (cpu_lbr_setp->lbr_count - 1) : (i - 1)), j++) {
299 to_rip = pcb->lbrs.lbrs[i].to_rip;
300 machlbrp->lbrs[j].to_ip = (to_rip > VM_MAX_USER_PAGE_ADDRESS) ? LBR_SENTINEL_KERNEL_MODE : to_rip;
301
302 from_rip = LBR_TYPE_TSXINFO_FROMRIP(pcb->lbrs.lbrs[i].from_rip);
303 machlbrp->lbrs[j].from_ip = (from_rip > VM_MAX_USER_PAGE_ADDRESS) ? LBR_SENTINEL_KERNEL_MODE : from_rip;
304 machlbrp->lbrs[j].mispredict = LBR_TYPE_TSXINFO_MISPREDICT(pcb->lbrs.lbrs[i].from_rip);
305 if (cpuid_tsx_supported) {
306 machlbrp->lbrs[j].tsx_abort = LBR_TYPE_TSXINFO_TSX_ABORT(pcb->lbrs.lbrs[i].from_rip);
307 machlbrp->lbrs[j].in_tsx = LBR_TYPE_TSXINFO_IN_TSX(pcb->lbrs.lbrs[i].from_rip);
308 } else {
309 machlbrp->lbrs[j].tsx_abort = 0;
310 machlbrp->lbrs[j].in_tsx = 0;
311 }
312 if (i == last_entry) {
313 break;
314 }
315 }
316 break;
317
318 case PERFCAP_LBR_TYPE_EIP_WITH_LBRINFO: /* SKL+ */
319
320 machlbrp->lbr_supported_tsx = cpuid_tsx_supported ? 1 : 0;
321 machlbrp->lbr_supported_cycle_count = 1;
322 for (j = 0, i = lbr_tos;; (i = (i == 0) ? (cpu_lbr_setp->lbr_count - 1) : (i - 1)), j++) {
323 from_rip = pcb->lbrs.lbrs[i].from_rip;
324 machlbrp->lbrs[j].from_ip = (from_rip > VM_MAX_USER_PAGE_ADDRESS) ? LBR_SENTINEL_KERNEL_MODE : from_rip;
325 to_rip = pcb->lbrs.lbrs[i].to_rip;
326 machlbrp->lbrs[j].to_ip = (to_rip > VM_MAX_USER_PAGE_ADDRESS) ? LBR_SENTINEL_KERNEL_MODE : to_rip;
327 machlbrp->lbrs[j].mispredict = LBR_TYPE_EIP_WITH_LBRINFO_MISPREDICT(pcb->lbrs.lbrs[i].info);
328 machlbrp->lbrs[j].tsx_abort = LBR_TYPE_EIP_WITH_LBRINFO_TSX_ABORT(pcb->lbrs.lbrs[i].info);
329 machlbrp->lbrs[j].in_tsx = LBR_TYPE_EIP_WITH_LBRINFO_IN_TSX(pcb->lbrs.lbrs[i].info);
330 machlbrp->lbrs[j].cycle_count = LBR_TYPE_EIP_WITH_LBRINFO_CYC_COUNT(pcb->lbrs.lbrs[i].info);
331 if (i == last_entry) {
332 break;
333 }
334 }
335 break;
336
337 default:
338#if DEBUG || DEVELOPMENT
339 panic("Unknown LBR format: %d!", cpu_lbr_type);
340 /*NOTREACHED*/
341#else
342 return -1;
343#endif
344 }
345
346 return 0;
347}
348
349void
350i386_lbr_synch(thread_t thr)
351{
352 pcb_t old_pcb = THREAD_TO_PCB(thr);
353 int i;
354
355 /* First, save current LBRs to the old thread's PCB */
356 if (cpu_lbr_setp->msr_lbrs[0].msr_info != 0) {
357 for (i = 0; i < cpu_lbr_setp->lbr_count; i++) {
358 old_pcb->lbrs.lbrs[i].from_rip = rdmsr64(cpu_lbr_setp->msr_lbrs[i].msr_from);
359 old_pcb->lbrs.lbrs[i].to_rip = rdmsr64(cpu_lbr_setp->msr_lbrs[i].msr_to);
360 old_pcb->lbrs.lbrs[i].info = rdmsr64(cpu_lbr_setp->msr_lbrs[i].msr_info);
361 }
362 } else {
363 for (i = 0; i < cpu_lbr_setp->lbr_count; i++) {
364 old_pcb->lbrs.lbrs[i].from_rip = rdmsr64(cpu_lbr_setp->msr_lbrs[i].msr_from);
365 old_pcb->lbrs.lbrs[i].to_rip = rdmsr64(cpu_lbr_setp->msr_lbrs[i].msr_to);
366 }
367 }
368
369 /* Finally, save the TOS */
370 old_pcb->lbrs.lbr_tos = rdmsr64(MSR_IA32_LASTBRANCH_TOS);
371}
372
373void
374i386_switch_lbrs(thread_t old, thread_t new)
375{
376 pcb_t new_pcb;
377 int i;
378 bool save_old = (old != NULL && old->task != kernel_task);
379 bool restore_new = (new->task != kernel_task);
380
381 if (!save_old && !restore_new) {
382 return;
383 }
384
385 assert(cpu_lbr_setp != NULL);
386
387 new_pcb = THREAD_TO_PCB(new);
388
389 i386_lbr_disable();
390
391 if (save_old) {
392 i386_lbr_synch(old);
393 }
394
395 if (restore_new) {
396 /* Now restore the new threads's LBRs */
397 if (cpu_lbr_setp->msr_lbrs[0].msr_info != 0) {
398 for (i = 0; i < cpu_lbr_setp->lbr_count; i++) {
399 wrmsr64(cpu_lbr_setp->msr_lbrs[i].msr_from, new_pcb->lbrs.lbrs[i].from_rip);
400 wrmsr64(cpu_lbr_setp->msr_lbrs[i].msr_to, new_pcb->lbrs.lbrs[i].to_rip);
401 wrmsr64(cpu_lbr_setp->msr_lbrs[i].msr_info, new_pcb->lbrs.lbrs[i].info);
402 }
403 } else {
404 if (lbr_need_tsx_workaround) {
405 for (i = 0; i < cpu_lbr_setp->lbr_count; i++) {
406 /*
407 * If TSX has been disabled, the hardware expects those two bits to be sign
408 * extensions of bit 47 (even though it didn't return them that way via the rdmsr!)
409 */
410#define BIT_47 (1ULL << 47)
411 wrmsr64(cpu_lbr_setp->msr_lbrs[i].msr_from,
412 new_pcb->lbrs.lbrs[i].from_rip |
413 ((new_pcb->lbrs.lbrs[i].from_rip & BIT_47) ? 0x6000000000000000ULL : 0));
414 wrmsr64(cpu_lbr_setp->msr_lbrs[i].msr_to,
415 new_pcb->lbrs.lbrs[i].to_rip |
416 ((new_pcb->lbrs.lbrs[i].to_rip & BIT_47) ? 0x6000000000000000ULL : 0));
417 }
418 } else {
419 for (i = 0; i < cpu_lbr_setp->lbr_count; i++) {
420 wrmsr64(cpu_lbr_setp->msr_lbrs[i].msr_from, new_pcb->lbrs.lbrs[i].from_rip);
421 wrmsr64(cpu_lbr_setp->msr_lbrs[i].msr_to, new_pcb->lbrs.lbrs[i].to_rip);
422 }
423 }
424 }
425
426 /* Lastly, restore the new threads's TOS */
427 wrmsr64(MSR_IA32_LASTBRANCH_TOS, new_pcb->lbrs.lbr_tos);
428 }
429
430 i386_lbr_enable();
431}
432
6d2010ae 433void
f427ee49 434act_machine_switch_pcb(thread_t old, thread_t new)
6d2010ae 435{
0a7de745
A
436 pcb_t pcb = THREAD_TO_PCB(new);
437 cpu_data_t *cdp = current_cpu_datap();
438 struct real_descriptor *ldtp;
439 mach_vm_offset_t pcb_stack_top;
6d2010ae
A
440
441 assert(new->kernel_stack != 0);
442 assert(ml_get_interrupts_enabled() == FALSE);
0a7de745 443#ifdef DIRECTION_FLAG_DEBUG
6d2010ae
A
444 if (x86_get_flags() & EFL_DF) {
445 panic("Direction flag detected: 0x%lx", x86_get_flags());
446 }
447#endif
448
6d2010ae
A
449 /*
450 * Clear segment state
451 * unconditionally for DS/ES/FS but more carefully for GS whose
452 * cached state we track.
453 */
454 set_ds(NULL_SEG);
455 set_es(NULL_SEG);
456 set_fs(NULL_SEG);
5c9f4661 457
6d2010ae 458 if (get_gs() != NULL_SEG) {
0a7de745 459 swapgs(); /* switch to user's GS context */
6d2010ae 460 set_gs(NULL_SEG);
0a7de745 461 swapgs(); /* and back to kernel */
6d2010ae
A
462
463 /* record the active machine state lost */
464 cdp->cpu_uber.cu_user_gs_base = 0;
0a7de745 465 }
6d2010ae 466
0a7de745 467 vm_offset_t isf;
6d2010ae 468
39236c6e
A
469 /*
470 * Set pointer to PCB's interrupt stack frame in cpu data.
471 * Used by syscall and double-fault trap handlers.
472 */
473 isf = (vm_offset_t) &pcb->iss->ss_64.isf;
474 cdp->cpu_uber.cu_isf = isf;
475 pcb_stack_top = (vm_offset_t) (pcb->iss + 1);
476 /* require 16-byte alignment */
477 assert((pcb_stack_top & 0xF) == 0);
6d2010ae 478
5c9f4661 479 current_ktss64()->rsp0 = cdp->cpu_desc_index.cdi_sstku;
39236c6e
A
480 /*
481 * Top of temporary sysenter stack points to pcb stack.
482 * Although this is not normally used by 64-bit users,
483 * it needs to be set in case a sysenter is attempted.
484 */
485 *current_sstk64() = pcb_stack_top;
486
5c9f4661
A
487 cdp->cd_estack = cpu_shadowp(cdp->cpu_number)->cd_estack = cdp->cpu_desc_index.cdi_sstku;
488
39236c6e 489 if (is_saved_state64(pcb->iss)) {
0a7de745 490 cdp->cpu_task_map = new->map->pmap->pm_task_map;
6d2010ae
A
491
492 /*
493 * Enable the 64-bit user code segment, USER64_CS.
494 * Disable the 32-bit user code segment, USER_CS.
495 */
00867663
A
496 gdt_desc_p(USER64_CS)->access |= ACC_PL_U;
497 gdt_desc_p(USER_CS)->access &= ~ACC_PL_U;
6d2010ae
A
498
499 /*
500 * Switch user's GS base if necessary
501 * by setting the Kernel's GS base MSR
502 * - this will become the user's on the swapgs when
503 * returning to user-space. Avoid this for
504 * kernel threads (no user TLS support required)
505 * and verify the memory shadow of the segment base
506 * in the event it was altered in user space.
507 */
508 if ((pcb->cthread_self != 0) || (new->task != kernel_task)) {
00867663
A
509 if ((cdp->cpu_uber.cu_user_gs_base != pcb->cthread_self) ||
510 (pcb->cthread_self != rdmsr64(MSR_IA32_KERNEL_GS_BASE))) {
6d2010ae
A
511 cdp->cpu_uber.cu_user_gs_base = pcb->cthread_self;
512 wrmsr64(MSR_IA32_KERNEL_GS_BASE, pcb->cthread_self);
513 }
514 }
39236c6e 515 } else {
6d2010ae 516 cdp->cpu_task_map = TASK_MAP_32BIT;
6d2010ae
A
517
518 /*
519 * Disable USER64_CS
520 * Enable USER_CS
521 */
00867663
A
522
523 /* It's possible that writing to the GDT areas
524 * is expensive, if the processor intercepts those
525 * writes to invalidate its internal segment caches
526 * TODO: perhaps only do this if switching bitness
527 */
528 gdt_desc_p(USER64_CS)->access &= ~ACC_PL_U;
529 gdt_desc_p(USER_CS)->access |= ACC_PL_U;
6d2010ae
A
530
531 /*
532 * Set the thread`s cthread (a.k.a pthread)
533 * For 32-bit user this involves setting the USER_CTHREAD
534 * descriptor in the LDT to point to the cthread data.
535 * The involves copying in the pre-initialized descriptor.
0a7de745
A
536 */
537 ldtp = current_ldt();
6d2010ae 538 ldtp[sel_idx(USER_CTHREAD)] = pcb->cthread_desc;
0a7de745 539 if (pcb->uldt_selector != 0) {
6d2010ae 540 ldtp[sel_idx(pcb->uldt_selector)] = pcb->uldt_desc;
0a7de745 541 }
6d2010ae 542 cdp->cpu_uber.cu_user_gs_base = pcb->cthread_self;
0a7de745 543 }
6d2010ae 544
0a7de745
A
545 cdp->cpu_curthread_do_segchk = new->machine.mthr_do_segchk;
546
f427ee49
A
547 if (last_branch_support_enabled) {
548 i386_switch_lbrs(old, new);
549 }
550
0a7de745
A
551 /*
552 * Set the thread`s LDT or LDT entry.
553 */
554 if (__probable(new->task == TASK_NULL || new->task->i386_ldt == 0)) {
6d2010ae 555 /*
0a7de745 556 * Use system LDT.
6d2010ae 557 */
0a7de745
A
558 ml_cpu_set_ldt(KERNEL_LDT);
559 cdp->cpu_curtask_has_ldt = 0;
560 } else {
561 /*
562 * Task has its own LDT.
563 */
564 user_ldt_set(new);
565 cdp->cpu_curtask_has_ldt = 1;
6d2010ae 566 }
6d2010ae 567}
39236c6e
A
568
569kern_return_t
6d2010ae
A
570thread_set_wq_state32(thread_t thread, thread_state_t tstate)
571{
0a7de745
A
572 x86_thread_state32_t *state;
573 x86_saved_state32_t *saved_state;
6d2010ae 574 thread_t curth = current_thread();
0a7de745 575 spl_t s = 0;
6d2010ae
A
576
577 pal_register_cache_state(thread, DIRTY);
578
579 saved_state = USER_REGS32(thread);
580
581 state = (x86_thread_state32_t *)tstate;
0a7de745 582
6d2010ae
A
583 if (curth != thread) {
584 s = splsched();
0a7de745 585 thread_lock(thread);
6d2010ae
A
586 }
587
588 saved_state->ebp = 0;
589 saved_state->eip = state->eip;
590 saved_state->eax = state->eax;
591 saved_state->ebx = state->ebx;
592 saved_state->ecx = state->ecx;
593 saved_state->edx = state->edx;
594 saved_state->edi = state->edi;
595 saved_state->esi = state->esi;
596 saved_state->uesp = state->esp;
597 saved_state->efl = EFL_USER_SET;
598
599 saved_state->cs = USER_CS;
600 saved_state->ss = USER_DS;
601 saved_state->ds = USER_DS;
602 saved_state->es = USER_DS;
603
604 if (curth != thread) {
0a7de745 605 thread_unlock(thread);
6d2010ae
A
606 splx(s);
607 }
39236c6e
A
608
609 return KERN_SUCCESS;
6d2010ae
A
610}
611
612
39236c6e 613kern_return_t
6d2010ae
A
614thread_set_wq_state64(thread_t thread, thread_state_t tstate)
615{
0a7de745
A
616 x86_thread_state64_t *state;
617 x86_saved_state64_t *saved_state;
6d2010ae 618 thread_t curth = current_thread();
0a7de745 619 spl_t s = 0;
6d2010ae 620
6d2010ae
A
621 saved_state = USER_REGS64(thread);
622 state = (x86_thread_state64_t *)tstate;
0a7de745 623
39236c6e
A
624 /* Disallow setting non-canonical PC or stack */
625 if (!IS_USERADDR64_CANONICAL(state->rsp) ||
626 !IS_USERADDR64_CANONICAL(state->rip)) {
627 return KERN_FAILURE;
628 }
629
630 pal_register_cache_state(thread, DIRTY);
631
6d2010ae
A
632 if (curth != thread) {
633 s = splsched();
0a7de745 634 thread_lock(thread);
6d2010ae
A
635 }
636
637 saved_state->rbp = 0;
638 saved_state->rdi = state->rdi;
639 saved_state->rsi = state->rsi;
640 saved_state->rdx = state->rdx;
641 saved_state->rcx = state->rcx;
642 saved_state->r8 = state->r8;
643 saved_state->r9 = state->r9;
644
645 saved_state->isf.rip = state->rip;
646 saved_state->isf.rsp = state->rsp;
647 saved_state->isf.cs = USER64_CS;
648 saved_state->isf.rflags = EFL_USER_SET;
649
650 if (curth != thread) {
0a7de745 651 thread_unlock(thread);
6d2010ae
A
652 splx(s);
653 }
39236c6e
A
654
655 return KERN_SUCCESS;
6d2010ae
A
656}
657
658/*
659 * Initialize the machine-dependent state for a new thread.
660 */
661kern_return_t
662machine_thread_create(
0a7de745
A
663 thread_t thread,
664 task_t task)
6d2010ae 665{
0a7de745 666 pcb_t pcb = THREAD_TO_PCB(thread);
6d2010ae 667
f427ee49 668 if ((task->t_flags & TF_TECS) || __improbable(force_thread_policy_tecs)) {
0a7de745
A
669 thread->machine.mthr_do_segchk = 1;
670 } else {
671 thread->machine.mthr_do_segchk = 0;
672 }
673
6d2010ae
A
674 /*
675 * Allocate save frame only if required.
676 */
39236c6e 677 if (pcb->iss == NULL) {
6d2010ae 678 assert((get_preemption_level() == 0));
39236c6e 679 pcb->iss = (x86_saved_state_t *) zalloc(iss_zone);
0a7de745 680 if (pcb->iss == NULL) {
6d2010ae 681 panic("iss_zone");
0a7de745 682 }
6d2010ae
A
683 }
684
39236c6e 685 /*
00867663 686 * Ensure that the synthesized 32-bit state including
0a7de745 687 * the 64-bit interrupt state can be acommodated in the
39236c6e
A
688 * 64-bit state we allocate for both 32-bit and 64-bit threads.
689 */
690 assert(sizeof(pcb->iss->ss_32) + sizeof(pcb->iss->ss_64.isf) <=
0a7de745 691 sizeof(pcb->iss->ss_64));
6d2010ae 692
39236c6e 693 bzero((char *)pcb->iss, sizeof(x86_saved_state_t));
6d2010ae 694
f427ee49
A
695 bzero(&pcb->lbrs, sizeof(x86_lbrs_t));
696
0a7de745 697 if (task_has_64Bit_addr(task)) {
39236c6e 698 pcb->iss->flavor = x86_SAVED_STATE64;
6d2010ae 699
39236c6e
A
700 pcb->iss->ss_64.isf.cs = USER64_CS;
701 pcb->iss->ss_64.isf.ss = USER_DS;
702 pcb->iss->ss_64.fs = USER_DS;
703 pcb->iss->ss_64.gs = USER_DS;
704 pcb->iss->ss_64.isf.rflags = EFL_USER_SET;
6d2010ae 705 } else {
39236c6e
A
706 pcb->iss->flavor = x86_SAVED_STATE32;
707
708 pcb->iss->ss_32.cs = USER_CS;
709 pcb->iss->ss_32.ss = USER_DS;
710 pcb->iss->ss_32.ds = USER_DS;
711 pcb->iss->ss_32.es = USER_DS;
712 pcb->iss->ss_32.fs = USER_DS;
713 pcb->iss->ss_32.gs = USER_DS;
714 pcb->iss->ss_32.efl = EFL_USER_SET;
6d2010ae 715 }
6d2010ae
A
716
717 simple_lock_init(&pcb->lock, 0);
718
6d2010ae
A
719 pcb->cthread_self = 0;
720 pcb->uldt_selector = 0;
fe8ab488 721 pcb->thread_gpu_ns = 0;
6d2010ae
A
722 /* Ensure that the "cthread" descriptor describes a valid
723 * segment.
724 */
725 if ((pcb->cthread_desc.access & ACC_P) == 0) {
5c9f4661 726 pcb->cthread_desc = *gdt_desc_p(USER_DS);
6d2010ae
A
727 }
728
0a7de745 729
f427ee49
A
730 pcb->insn_state_copyin_failure_errorcode = 0;
731 if (pcb->insn_state != 0) { /* Reinit for new thread */
732 bzero(pcb->insn_state, sizeof(x86_instruction_state_t));
733 pcb->insn_state->insn_stream_valid_bytes = -1;
734 }
735
0a7de745 736 return KERN_SUCCESS;
6d2010ae
A
737}
738
739/*
740 * Machine-dependent cleanup prior to destroying a thread
741 */
742void
743machine_thread_destroy(
0a7de745 744 thread_t thread)
6d2010ae 745{
0a7de745 746 pcb_t pcb = THREAD_TO_PCB(thread);
6d2010ae 747
fe8ab488
A
748#if HYPERVISOR
749 if (thread->hv_thread_target) {
750 hv_callbacks.thread_destroy(thread->hv_thread_target);
751 thread->hv_thread_target = NULL;
752 }
753#endif
754
0a7de745 755 if (pcb->ifps != 0) {
5ba3f43e 756 fpu_free(thread, pcb->ifps);
0a7de745 757 }
39236c6e
A
758 if (pcb->iss != 0) {
759 zfree(iss_zone, pcb->iss);
760 pcb->iss = 0;
6d2010ae
A
761 }
762 if (pcb->ids) {
763 zfree(ids_zone, pcb->ids);
764 pcb->ids = NULL;
765 }
f427ee49
A
766
767 if (pcb->insn_state != 0) {
768 kfree(pcb->insn_state, sizeof(x86_instruction_state_t));
769 pcb->insn_state = 0;
770 }
771 pcb->insn_state_copyin_failure_errorcode = 0;
6d2010ae 772}
fe8ab488
A
773
774kern_return_t
775machine_thread_set_tsd_base(
0a7de745
A
776 thread_t thread,
777 mach_vm_offset_t tsd_base)
fe8ab488 778{
fe8ab488
A
779 if (thread->task == kernel_task) {
780 return KERN_INVALID_ARGUMENT;
781 }
782
d9a64523 783 if (thread_is_64bit_addr(thread)) {
fe8ab488 784 /* check for canonical address, set 0 otherwise */
0a7de745 785 if (!IS_USERADDR64_CANONICAL(tsd_base)) {
fe8ab488 786 tsd_base = 0ULL;
0a7de745 787 }
fe8ab488 788 } else {
0a7de745 789 if (tsd_base > UINT32_MAX) {
fe8ab488 790 tsd_base = 0ULL;
0a7de745 791 }
fe8ab488
A
792 }
793
794 pcb_t pcb = THREAD_TO_PCB(thread);
795 pcb->cthread_self = tsd_base;
796
d9a64523 797 if (!thread_is_64bit_addr(thread)) {
fe8ab488
A
798 /* Set up descriptor for later use */
799 struct real_descriptor desc = {
800 .limit_low = 1,
801 .limit_high = 0,
802 .base_low = tsd_base & 0xffff,
803 .base_med = (tsd_base >> 16) & 0xff,
804 .base_high = (tsd_base >> 24) & 0xff,
0a7de745
A
805 .access = ACC_P | ACC_PL_U | ACC_DATA_W,
806 .granularity = SZ_32 | SZ_G,
fe8ab488
A
807 };
808
809 pcb->cthread_desc = desc;
810 saved_state32(pcb->iss)->gs = USER_CTHREAD;
811 }
812
813 /* For current thread, make the TSD base active immediately */
814 if (thread == current_thread()) {
d9a64523 815 if (thread_is_64bit_addr(thread)) {
fe8ab488
A
816 cpu_data_t *cdp;
817
818 mp_disable_preemption();
819 cdp = current_cpu_datap();
820 if ((cdp->cpu_uber.cu_user_gs_base != pcb->cthread_self) ||
0a7de745 821 (pcb->cthread_self != rdmsr64(MSR_IA32_KERNEL_GS_BASE))) {
fe8ab488 822 wrmsr64(MSR_IA32_KERNEL_GS_BASE, tsd_base);
0a7de745 823 }
fe8ab488
A
824 cdp->cpu_uber.cu_user_gs_base = tsd_base;
825 mp_enable_preemption();
826 } else {
fe8ab488
A
827 /* assign descriptor */
828 mp_disable_preemption();
829 *ldt_desc_p(USER_CTHREAD) = pcb->cthread_desc;
830 mp_enable_preemption();
831 }
832 }
833
834 return KERN_SUCCESS;
835}
0a7de745
A
836
837void
838machine_tecs(thread_t thr)
839{
840 if (tecs_mode_supported) {
841 thr->machine.mthr_do_segchk = 1;
842 }
843}
844
845int
846machine_csv(cpuvn_e cve)
847{
848 switch (cve) {
849 case CPUVN_CI:
850 return (cpuid_wa_required(CPU_INTEL_SEGCHK) & CWA_ON) != 0;
851
852 default:
853 break;
854 }
855
856 return 0;
857}