xref: /xnu-8020.101.4/osfmk/i386/trap.c (revision e7776783b89a353188416a9a346c6cdb4928faad)
1 /*
2  * Copyright (c) 2000-2020 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 /*
29  * @OSF_COPYRIGHT@
30  */
31 /*
32  * Mach Operating System
33  * Copyright (c) 1991,1990,1989,1988 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  [email protected]
49  *  School of Computer Science
50  *  Carnegie Mellon University
51  *  Pittsburgh PA 15213-3890
52  *
53  * any improvements or extensions that they make and grant Carnegie Mellon
54  * the rights to redistribute these changes.
55  */
56 /*
57  */
58 
59 /*
60  * Hardware trap/fault handler.
61  */
62 
63 #include <mach_kdp.h>
64 #include <mach_ldebug.h>
65 
66 #include <types.h>
67 #include <i386/eflags.h>
68 #include <i386/trap.h>
69 #include <i386/pmap.h>
70 #include <i386/fpu.h>
71 #include <i386/panic_notify.h>
72 #include <i386/lapic.h>
73 
74 #include <mach/exception.h>
75 #include <mach/kern_return.h>
76 #include <mach/vm_param.h>
77 #include <mach/i386/thread_status.h>
78 
79 #include <vm/vm_kern.h>
80 #include <vm/vm_fault.h>
81 
82 #include <kern/kern_types.h>
83 #include <kern/processor.h>
84 #include <kern/thread.h>
85 #include <kern/task.h>
86 #include <kern/sched.h>
87 #include <kern/sched_prim.h>
88 #include <kern/exception.h>
89 #include <kern/spl.h>
90 #include <kern/misc_protos.h>
91 #include <kern/debug.h>
92 #if CONFIG_TELEMETRY
93 #include <kern/telemetry.h>
94 #endif
95 #include <sys/kdebug.h>
96 #include <kperf/kperf.h>
97 #include <prng/random.h>
98 #include <prng/entropy.h>
99 
100 #include <string.h>
101 
102 #include <i386/postcode.h>
103 #include <i386/mp_desc.h>
104 #include <i386/proc_reg.h>
105 #include <i386/machine_routines.h>
106 #if CONFIG_MCA
107 #include <i386/machine_check.h>
108 #endif
109 #include <mach/i386/syscall_sw.h>
110 
111 #include <libkern/OSDebug.h>
112 #include <i386/cpu_threads.h>
113 #include <machine/pal_routines.h>
114 #include <i386/lbr.h>
115 
116 extern void throttle_lowpri_io(int);
117 extern void kprint_state(x86_saved_state64_t *saved_state);
118 #if DEVELOPMENT || DEBUG
119 int insnstream_force_cacheline_mismatch = 0;
120 extern int panic_on_cacheline_mismatch;
121 extern char panic_on_trap_procname[];
122 extern uint32_t panic_on_trap_mask;
123 #endif
124 
125 extern int insn_copyin_count;
126 
127 /*
128  * Forward declarations
129  */
130 static void panic_trap(x86_saved_state64_t *saved_state, uint32_t pl, kern_return_t fault_result) __dead2;
131 static void set_recovery_ip(x86_saved_state64_t *saved_state, vm_offset_t ip);
132 #if DEVELOPMENT || DEBUG
133 static __attribute__((noinline)) void copy_instruction_stream(thread_t thread, uint64_t rip, int trap_code, bool inspect_cacheline);
134 #else
135 static __attribute__((noinline)) void copy_instruction_stream(thread_t thread, uint64_t rip, int trap_code);
136 #endif
137 
138 #if CONFIG_DTRACE
139 /* See <rdar://problem/4613924> */
140 perfCallback tempDTraceTrapHook = NULL; /* Pointer to DTrace fbt trap hook routine */
141 
142 extern boolean_t dtrace_tally_fault(user_addr_t);
143 extern boolean_t dtrace_handle_trap(int, x86_saved_state_t *);
144 #endif
145 
146 #ifdef MACH_BSD
147 extern char *   proc_name_address(void *p);
148 #endif /* MACH_BSD */
149 
150 extern boolean_t pmap_smep_enabled;
151 extern boolean_t pmap_smap_enabled;
152 
153 __attribute__((noreturn))
154 void
thread_syscall_return(kern_return_t ret)155 thread_syscall_return(
156 	kern_return_t ret)
157 {
158 	thread_t        thr_act = current_thread();
159 	boolean_t       is_mach;
160 	int             code;
161 
162 	pal_register_cache_state(thr_act, DIRTY);
163 
164 	if (thread_is_64bit_addr(thr_act)) {
165 		x86_saved_state64_t     *regs;
166 
167 		regs = USER_REGS64(thr_act);
168 
169 		code = (int) (regs->rax & SYSCALL_NUMBER_MASK);
170 		is_mach = (regs->rax & SYSCALL_CLASS_MASK)
171 		    == (SYSCALL_CLASS_MACH << SYSCALL_CLASS_SHIFT);
172 		if (kdebug_enable && is_mach) {
173 			/* Mach trap */
174 			KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
175 			    MACHDBG_CODE(DBG_MACH_EXCP_SC, code) | DBG_FUNC_END,
176 			    ret, 0, 0, 0, 0);
177 		}
178 		regs->rax = ret;
179 #if DEBUG
180 		if (is_mach) {
181 			DEBUG_KPRINT_SYSCALL_MACH(
182 				"thread_syscall_return: 64-bit mach ret=%u\n",
183 				ret);
184 		} else {
185 			DEBUG_KPRINT_SYSCALL_UNIX(
186 				"thread_syscall_return: 64-bit unix ret=%u\n",
187 				ret);
188 		}
189 #endif
190 	} else {
191 		x86_saved_state32_t     *regs;
192 
193 		regs = USER_REGS32(thr_act);
194 
195 		code = ((int) regs->eax);
196 		is_mach = (code < 0);
197 		if (kdebug_enable && is_mach) {
198 			/* Mach trap */
199 			KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
200 			    MACHDBG_CODE(DBG_MACH_EXCP_SC, -code) | DBG_FUNC_END,
201 			    ret, 0, 0, 0, 0);
202 		}
203 		regs->eax = ret;
204 #if DEBUG
205 		if (is_mach) {
206 			DEBUG_KPRINT_SYSCALL_MACH(
207 				"thread_syscall_return: 32-bit mach ret=%u\n",
208 				ret);
209 		} else {
210 			DEBUG_KPRINT_SYSCALL_UNIX(
211 				"thread_syscall_return: 32-bit unix ret=%u\n",
212 				ret);
213 		}
214 #endif
215 	}
216 
217 #if DEBUG || DEVELOPMENT
218 	kern_allocation_name_t
219 	prior __assert_only = thread_get_kernel_state(thr_act)->allocation_name;
220 	assertf(prior == NULL, "thread_set_allocation_name(\"%s\") not cleared", kern_allocation_get_name(prior));
221 #endif /* DEBUG || DEVELOPMENT */
222 
223 	throttle_lowpri_io(1);
224 
225 	thread_exception_return();
226 	/*NOTREACHED*/
227 }
228 
229 /*
230  * Fault recovery in copyin/copyout routines.
231  */
232 struct recovery {
233 	uintptr_t       fault_addr;
234 	uintptr_t       recover_addr;
235 };
236 
237 extern struct recovery  recover_table[];
238 extern struct recovery  recover_table_end[];
239 
240 const char *    trap_type[] = {TRAP_NAMES};
241 unsigned        TRAP_TYPES = sizeof(trap_type) / sizeof(trap_type[0]);
242 
243 extern void     PE_incoming_interrupt(int interrupt);
244 
245 #if defined(__x86_64__) && DEBUG
246 void
kprint_state(x86_saved_state64_t * saved_state)247 kprint_state(x86_saved_state64_t        *saved_state)
248 {
249 	kprintf("current_cpu_datap() 0x%lx\n", (uintptr_t)current_cpu_datap());
250 	kprintf("Current GS base MSR 0x%llx\n", rdmsr64(MSR_IA32_GS_BASE));
251 	kprintf("Kernel  GS base MSR 0x%llx\n", rdmsr64(MSR_IA32_KERNEL_GS_BASE));
252 	kprintf("state at 0x%lx:\n", (uintptr_t) saved_state);
253 
254 	kprintf("      rdi    0x%llx\n", saved_state->rdi);
255 	kprintf("      rsi    0x%llx\n", saved_state->rsi);
256 	kprintf("      rdx    0x%llx\n", saved_state->rdx);
257 	kprintf("      r10    0x%llx\n", saved_state->r10);
258 	kprintf("      r8     0x%llx\n", saved_state->r8);
259 	kprintf("      r9     0x%llx\n", saved_state->r9);
260 
261 	kprintf("      cr2    0x%llx\n", saved_state->cr2);
262 	kprintf("real  cr2    0x%lx\n", get_cr2());
263 	kprintf("      r15    0x%llx\n", saved_state->r15);
264 	kprintf("      r14    0x%llx\n", saved_state->r14);
265 	kprintf("      r13    0x%llx\n", saved_state->r13);
266 	kprintf("      r12    0x%llx\n", saved_state->r12);
267 	kprintf("      r11    0x%llx\n", saved_state->r11);
268 	kprintf("      rbp    0x%llx\n", saved_state->rbp);
269 	kprintf("      rbx    0x%llx\n", saved_state->rbx);
270 	kprintf("      rcx    0x%llx\n", saved_state->rcx);
271 	kprintf("      rax    0x%llx\n", saved_state->rax);
272 
273 	kprintf("      gs     0x%x\n", saved_state->gs);
274 	kprintf("      fs     0x%x\n", saved_state->fs);
275 
276 	kprintf("  isf.trapno 0x%x\n", saved_state->isf.trapno);
277 	kprintf("  isf._pad   0x%x\n", saved_state->isf._pad);
278 	kprintf("  isf.trapfn 0x%llx\n", saved_state->isf.trapfn);
279 	kprintf("  isf.err    0x%llx\n", saved_state->isf.err);
280 	kprintf("  isf.rip    0x%llx\n", saved_state->isf.rip);
281 	kprintf("  isf.cs     0x%llx\n", saved_state->isf.cs);
282 	kprintf("  isf.rflags 0x%llx\n", saved_state->isf.rflags);
283 	kprintf("  isf.rsp    0x%llx\n", saved_state->isf.rsp);
284 	kprintf("  isf.ss     0x%llx\n", saved_state->isf.ss);
285 }
286 #endif
287 
288 
289 /*
290  * Non-zero indicates latency assert is enabled and capped at valued
291  * absolute time units.
292  */
293 
294 uint64_t interrupt_latency_cap = 0;
295 boolean_t ilat_assert = FALSE;
296 
297 void
interrupt_latency_tracker_setup(void)298 interrupt_latency_tracker_setup(void)
299 {
300 	uint32_t ilat_cap_us;
301 	if (PE_parse_boot_argn("interrupt_latency_cap_us", &ilat_cap_us, sizeof(ilat_cap_us))) {
302 		interrupt_latency_cap = ilat_cap_us * NSEC_PER_USEC;
303 		nanoseconds_to_absolutetime(interrupt_latency_cap, &interrupt_latency_cap);
304 	} else {
305 		interrupt_latency_cap = LockTimeOut;
306 	}
307 	PE_parse_boot_argn("-interrupt_latency_assert_enable", &ilat_assert, sizeof(ilat_assert));
308 }
309 
310 void
interrupt_reset_latency_stats(void)311 interrupt_reset_latency_stats(void)
312 {
313 	uint32_t i;
314 	for (i = 0; i < real_ncpus; i++) {
315 		cpu_data_ptr[i]->cpu_max_observed_int_latency =
316 		    cpu_data_ptr[i]->cpu_max_observed_int_latency_vector = 0;
317 	}
318 }
319 
320 void
interrupt_populate_latency_stats(char * buf,unsigned bufsize)321 interrupt_populate_latency_stats(char *buf, unsigned bufsize)
322 {
323 	uint32_t i, tcpu = ~0;
324 	uint64_t cur_max = 0;
325 
326 	for (i = 0; i < real_ncpus; i++) {
327 		if (cur_max < cpu_data_ptr[i]->cpu_max_observed_int_latency) {
328 			cur_max = cpu_data_ptr[i]->cpu_max_observed_int_latency;
329 			tcpu = i;
330 		}
331 	}
332 
333 	if (tcpu < real_ncpus) {
334 		snprintf(buf, bufsize, "0x%x 0x%x 0x%llx", tcpu, cpu_data_ptr[tcpu]->cpu_max_observed_int_latency_vector, cpu_data_ptr[tcpu]->cpu_max_observed_int_latency);
335 	}
336 }
337 
338 uint32_t interrupt_timer_coalescing_enabled = 1;
339 uint64_t interrupt_coalesced_timers;
340 
341 /*
342  * Handle interrupts:
343  *  - local APIC interrupts (IPIs, timers, etc) are handled by the kernel,
344  *  - device interrupts go to the platform expert.
345  */
346 void
interrupt(x86_saved_state_t * state)347 interrupt(x86_saved_state_t *state)
348 {
349 	uint64_t        rip;
350 	uint64_t        rsp;
351 	int             interrupt_num;
352 	boolean_t       user_mode = FALSE;
353 	int             ipl;
354 	int             cnum = cpu_number();
355 	cpu_data_t      *cdp = cpu_data_ptr[cnum];
356 	int             itype = DBG_INTR_TYPE_UNKNOWN;
357 	int             handled;
358 
359 
360 	x86_saved_state64_t     *state64 = saved_state64(state);
361 	rip = state64->isf.rip;
362 	rsp = state64->isf.rsp;
363 	interrupt_num = state64->isf.trapno;
364 	if (state64->isf.cs & 0x03) {
365 		user_mode = TRUE;
366 	}
367 
368 #if DEVELOPMENT || DEBUG
369 	uint64_t frameptr = is_saved_state64(state) ? state64->rbp : saved_state32(state)->ebp;
370 	uint32_t traptrace_index = traptrace_start(interrupt_num, rip, mach_absolute_time(), frameptr);
371 #endif
372 
373 	if (cpu_data_ptr[cnum]->lcpu.package->num_idle == topoParms.nLThreadsPerPackage) {
374 		cpu_data_ptr[cnum]->cpu_hwIntpexits[interrupt_num]++;
375 	}
376 
377 	if (interrupt_num == (LAPIC_DEFAULT_INTERRUPT_BASE + LAPIC_INTERPROCESSOR_INTERRUPT)) {
378 		itype = DBG_INTR_TYPE_IPI;
379 	} else if (interrupt_num == (LAPIC_DEFAULT_INTERRUPT_BASE + LAPIC_TIMER_INTERRUPT)) {
380 		itype = DBG_INTR_TYPE_TIMER;
381 	} else {
382 		itype = DBG_INTR_TYPE_OTHER;
383 	}
384 
385 	KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
386 	    MACHDBG_CODE(DBG_MACH_EXCP_INTR, 0) | DBG_FUNC_START,
387 	    interrupt_num,
388 	    (user_mode ? rip : VM_KERNEL_UNSLIDE(rip)),
389 	    user_mode, itype, 0);
390 
391 	SCHED_STATS_INC(interrupt_count);
392 
393 #if CONFIG_TELEMETRY
394 	if (telemetry_needs_record) {
395 		telemetry_mark_curthread(user_mode, FALSE);
396 	}
397 #endif
398 
399 	ipl = get_preemption_level();
400 
401 	/*
402 	 * Handle local APIC interrupts
403 	 * else call platform expert for devices.
404 	 */
405 	handled = lapic_interrupt(interrupt_num, state);
406 
407 	if (!handled) {
408 		if (interrupt_num == (LAPIC_DEFAULT_INTERRUPT_BASE + LAPIC_CMCI_INTERRUPT)) {
409 			/*
410 			 * CMCI can be signalled on any logical processor, and the kexts
411 			 * that implement handling CMCI use IOKit to register handlers for
412 			 * the CMCI vector, so if we see a CMCI, do not encode a CPU
413 			 * number in bits 8:31 (since the vector is the same regardless of
414 			 * the handling CPU).
415 			 */
416 			PE_incoming_interrupt(interrupt_num);
417 		} else if (cnum <= lapic_max_interrupt_cpunum) {
418 			PE_incoming_interrupt((cnum << 8) | interrupt_num);
419 		}
420 	}
421 
422 	if (__improbable(get_preemption_level() != ipl)) {
423 		panic("Preemption level altered by interrupt vector 0x%x: initial 0x%x, final: 0x%x", interrupt_num, ipl, get_preemption_level());
424 	}
425 
426 
427 	if (__improbable(cdp->cpu_nested_istack)) {
428 		cdp->cpu_nested_istack_events++;
429 	} else {
430 		uint64_t ctime = mach_absolute_time();
431 		uint64_t int_latency = ctime - cdp->cpu_int_event_time;
432 		uint64_t esdeadline, ehdeadline;
433 		/* Attempt to process deferred timers in the context of
434 		 * this interrupt, unless interrupt time has already exceeded
435 		 * TCOAL_ILAT_THRESHOLD.
436 		 */
437 #define TCOAL_ILAT_THRESHOLD (30000ULL)
438 
439 		if ((int_latency < TCOAL_ILAT_THRESHOLD) &&
440 		    interrupt_timer_coalescing_enabled) {
441 			esdeadline = cdp->rtclock_timer.queue.earliest_soft_deadline;
442 			ehdeadline = cdp->rtclock_timer.deadline;
443 			if ((ctime >= esdeadline) && (ctime < ehdeadline)) {
444 				interrupt_coalesced_timers++;
445 				TCOAL_DEBUG(0x88880000 | DBG_FUNC_START, ctime, esdeadline, ehdeadline, interrupt_coalesced_timers, 0);
446 				rtclock_intr(state);
447 				TCOAL_DEBUG(0x88880000 | DBG_FUNC_END, ctime, esdeadline, interrupt_coalesced_timers, 0, 0);
448 			} else {
449 				TCOAL_DEBUG(0x77770000, ctime, cdp->rtclock_timer.queue.earliest_soft_deadline, cdp->rtclock_timer.deadline, interrupt_coalesced_timers, 0);
450 			}
451 		}
452 
453 		if (__improbable(ilat_assert && (int_latency > interrupt_latency_cap) && !machine_timeout_suspended())) {
454 			panic("Interrupt vector 0x%x exceeded interrupt latency threshold, 0x%llx absolute time delta, prior signals: 0x%x, current signals: 0x%x", interrupt_num, int_latency, cdp->cpu_prior_signals, cdp->cpu_signals);
455 		}
456 
457 		if (__improbable(int_latency > cdp->cpu_max_observed_int_latency)) {
458 			cdp->cpu_max_observed_int_latency = int_latency;
459 			cdp->cpu_max_observed_int_latency_vector = interrupt_num;
460 		}
461 	}
462 
463 	/*
464 	 * Having serviced the interrupt first, look at the interrupted stack depth.
465 	 */
466 	if (!user_mode) {
467 		uint64_t depth = cdp->cpu_kernel_stack
468 		    + sizeof(struct thread_kernel_state)
469 		    + sizeof(struct i386_exception_link *)
470 		    - rsp;
471 		if (__improbable(depth > kernel_stack_depth_max)) {
472 			kernel_stack_depth_max = (vm_offset_t)depth;
473 			KERNEL_DEBUG_CONSTANT(
474 				MACHDBG_CODE(DBG_MACH_SCHED, MACH_STACK_DEPTH),
475 				(long) depth, (long) VM_KERNEL_UNSLIDE(rip), 0, 0, 0);
476 		}
477 	}
478 
479 	if (cnum == master_cpu) {
480 		entropy_collect();
481 	}
482 
483 #if KPERF
484 	kperf_interrupt();
485 #endif /* KPERF */
486 
487 	KDBG_RELEASE(MACHDBG_CODE(DBG_MACH_EXCP_INTR, 0) | DBG_FUNC_END,
488 	    interrupt_num);
489 
490 	assert(ml_get_interrupts_enabled() == FALSE);
491 
492 #if DEVELOPMENT || DEBUG
493 	if (traptrace_index != TRAPTRACE_INVALID_INDEX) {
494 		traptrace_end(traptrace_index, mach_absolute_time());
495 	}
496 #endif
497 }
498 
499 static inline void
reset_dr7(void)500 reset_dr7(void)
501 {
502 	long dr7 = 0x400; /* magic dr7 reset value; 32 bit on i386, 64 bit on x86_64 */
503 	__asm__ volatile ("mov %0,%%dr7" : : "r" (dr7));
504 }
505 #if MACH_KDP
506 unsigned kdp_has_active_watchpoints = 0;
507 #define NO_WATCHPOINTS (!kdp_has_active_watchpoints)
508 #else
509 #define NO_WATCHPOINTS 1
510 #endif
511 /*
512  * Trap from kernel mode.  Only page-fault errors are recoverable,
513  * and then only in special circumstances.  All other errors are
514  * fatal.  Return value indicates if trap was handled.
515  */
516 
517 void
kernel_trap(x86_saved_state_t * state,uintptr_t * lo_spp)518 kernel_trap(
519 	x86_saved_state_t       *state,
520 	uintptr_t *lo_spp)
521 {
522 	x86_saved_state64_t     *saved_state;
523 	int                     code;
524 	user_addr_t             vaddr;
525 	int                     type;
526 	vm_map_t                map = 0;        /* protected by T_PAGE_FAULT */
527 	kern_return_t           result = KERN_FAILURE;
528 	kern_return_t           fault_result = KERN_SUCCESS;
529 	thread_t                thread;
530 	boolean_t               intr;
531 	vm_prot_t               prot;
532 	struct recovery         *rp;
533 	vm_offset_t             kern_ip;
534 	int                     is_user;
535 	int                     trap_pl = get_preemption_level();
536 
537 	thread = current_thread();
538 
539 	if (__improbable(is_saved_state32(state))) {
540 		panic("kernel_trap(%p) with 32-bit state", state);
541 	}
542 	saved_state = saved_state64(state);
543 
544 	/* Record cpu where state was captured */
545 	saved_state->isf.cpu = cpu_number();
546 
547 	vaddr = (user_addr_t)saved_state->cr2;
548 	type  = saved_state->isf.trapno;
549 	code  = (int)(saved_state->isf.err & 0xffff);
550 	intr  = (saved_state->isf.rflags & EFL_IF) != 0;        /* state of ints at trap */
551 	kern_ip = (vm_offset_t)saved_state->isf.rip;
552 
553 	is_user = (vaddr < VM_MAX_USER_PAGE_ADDRESS);
554 
555 #if DEVELOPMENT || DEBUG
556 	uint32_t traptrace_index = traptrace_start(type, kern_ip, mach_absolute_time(), saved_state->rbp);
557 #endif
558 
559 #if CONFIG_DTRACE
560 	/*
561 	 * Is there a DTrace hook?
562 	 */
563 	if (__improbable(tempDTraceTrapHook != NULL)) {
564 		if (tempDTraceTrapHook(type, state, lo_spp, 0) == KERN_SUCCESS) {
565 			/*
566 			 * If it succeeds, we are done...
567 			 */
568 			goto common_return;
569 		}
570 	}
571 
572 	/* Handle traps originated from probe context. */
573 	if (thread != THREAD_NULL && thread->t_dtrace_inprobe) {
574 		if (dtrace_handle_trap(type, state)) {
575 			goto common_return;
576 		}
577 	}
578 
579 #endif /* CONFIG_DTRACE */
580 
581 	/*
582 	 * we come here with interrupts off as we don't want to recurse
583 	 * on preemption below.  but we do want to re-enable interrupts
584 	 * as soon we possibly can to hold latency down
585 	 */
586 	if (__improbable(T_PREEMPT == type)) {
587 		ast_taken_kernel();
588 
589 		KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
590 		    (MACHDBG_CODE(DBG_MACH_EXCP_KTRAP_x86, type)) | DBG_FUNC_NONE,
591 		    0, 0, 0, VM_KERNEL_UNSLIDE(kern_ip), 0);
592 
593 		goto common_return;
594 	}
595 
596 	user_addr_t     kd_vaddr = is_user ? vaddr : VM_KERNEL_UNSLIDE(vaddr);
597 	KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
598 	    (MACHDBG_CODE(DBG_MACH_EXCP_KTRAP_x86, type)) | DBG_FUNC_NONE,
599 	    (unsigned)(kd_vaddr >> 32), (unsigned)kd_vaddr, is_user,
600 	    VM_KERNEL_UNSLIDE(kern_ip), 0);
601 
602 
603 	if (T_PAGE_FAULT == type) {
604 		/*
605 		 * assume we're faulting in the kernel map
606 		 */
607 		map = kernel_map;
608 
609 		if (__probable((thread != THREAD_NULL) && (thread->map != kernel_map) &&
610 		    (vaddr < VM_MAX_USER_PAGE_ADDRESS))) {
611 			/* fault occurred in userspace */
612 			map = thread->map;
613 
614 			/* Intercept a potential Supervisor Mode Execute
615 			 * Protection fault. These criteria identify
616 			 * both NX faults and SMEP faults, but both
617 			 * are fatal. We avoid checking PTEs (racy).
618 			 * (The VM could just redrive a SMEP fault, hence
619 			 * the intercept).
620 			 */
621 			if (__improbable((code == (T_PF_PROT | T_PF_EXECUTE)) &&
622 			    (pmap_smep_enabled) && (saved_state->isf.rip == vaddr))) {
623 				goto debugger_entry;
624 			}
625 
626 			/*
627 			 * Additionally check for SMAP faults...
628 			 * which are characterized by page-present and
629 			 * the AC bit unset (i.e. not from copyin/out path).
630 			 */
631 			if (__improbable(code & T_PF_PROT &&
632 			    pmap_smap_enabled &&
633 			    (saved_state->isf.rflags & EFL_AC) == 0)) {
634 				goto debugger_entry;
635 			}
636 
637 			/*
638 			 * If we're not sharing cr3 with the user
639 			 * and we faulted in copyio,
640 			 * then switch cr3 here and dismiss the fault.
641 			 */
642 			if (no_shared_cr3 &&
643 			    (thread->machine.specFlags & CopyIOActive) &&
644 			    map->pmap->pm_cr3 != get_cr3_base()) {
645 				pmap_assert(current_cpu_datap()->cpu_pmap_pcid_enabled == FALSE);
646 				set_cr3_raw(map->pmap->pm_cr3);
647 				return;
648 			}
649 			if (__improbable(vaddr < PAGE_SIZE) &&
650 			    ((thread->machine.specFlags & CopyIOActive) == 0)) {
651 				goto debugger_entry;
652 			}
653 		}
654 	}
655 
656 	(void) ml_set_interrupts_enabled(intr);
657 
658 	switch (type) {
659 	case T_NO_FPU:
660 		fpnoextflt();
661 		goto common_return;
662 
663 	case T_FPU_FAULT:
664 		fpextovrflt();
665 		goto common_return;
666 
667 	case T_FLOATING_POINT_ERROR:
668 		fpexterrflt();
669 		goto common_return;
670 
671 	case T_SSE_FLOAT_ERROR:
672 		fpSSEexterrflt();
673 		goto common_return;
674 
675 	case T_INVALID_OPCODE:
676 		fpUDflt(kern_ip);
677 		goto debugger_entry;
678 
679 	case T_DEBUG:
680 		/*
681 		 * Re-enable LBR tracing for core/panic files if necessary. i386_lbr_enable confirms LBR should be re-enabled.
682 		 */
683 		i386_lbr_enable();
684 		if ((saved_state->isf.rflags & EFL_TF) == 0 && NO_WATCHPOINTS) {
685 			/* We've somehow encountered a debug
686 			 * register match that does not belong
687 			 * to the kernel debugger.
688 			 * This isn't supposed to happen.
689 			 */
690 			reset_dr7();
691 			goto common_return;
692 		}
693 		goto debugger_entry;
694 	case T_INT3:
695 		goto debugger_entry;
696 	case T_PAGE_FAULT:
697 
698 #if CONFIG_DTRACE
699 		if (thread != THREAD_NULL && thread->t_dtrace_inprobe) { /* Executing under dtrace_probe? */
700 			if (dtrace_tally_fault(vaddr)) { /* Should a fault under dtrace be ignored? */
701 				/*
702 				 * DTrace has "anticipated" the possibility of this fault, and has
703 				 * established the suitable recovery state. Drop down now into the
704 				 * recovery handling code in "case T_GENERAL_PROTECTION:".
705 				 */
706 				goto FALL_THROUGH;
707 			}
708 		}
709 #endif /* CONFIG_DTRACE */
710 
711 		prot = VM_PROT_READ;
712 
713 		if (code & T_PF_WRITE) {
714 			prot |= VM_PROT_WRITE;
715 		}
716 		if (code & T_PF_EXECUTE) {
717 			prot |= VM_PROT_EXECUTE;
718 		}
719 
720 		fault_result = result = vm_fault(map,
721 		    vaddr,
722 		    prot,
723 		    FALSE, VM_KERN_MEMORY_NONE,
724 		    THREAD_UNINT, NULL, 0);
725 
726 		if (result == KERN_SUCCESS) {
727 			goto common_return;
728 		}
729 		/*
730 		 * fall through
731 		 */
732 #if CONFIG_DTRACE
733 FALL_THROUGH:
734 #endif /* CONFIG_DTRACE */
735 
736 	case T_GENERAL_PROTECTION:
737 		/*
738 		 * If there is a failure recovery address
739 		 * for this fault, go there.
740 		 */
741 		for (rp = recover_table; rp < recover_table_end; rp++) {
742 			if (kern_ip == rp->fault_addr) {
743 				set_recovery_ip(saved_state, rp->recover_addr);
744 				goto common_return;
745 			}
746 		}
747 
748 		/*
749 		 * Check thread recovery address also.
750 		 */
751 		if (thread != THREAD_NULL && thread->recover) {
752 			set_recovery_ip(saved_state, thread->recover);
753 			thread->recover = 0;
754 			goto common_return;
755 		}
756 		/*
757 		 * Unanticipated page-fault errors in kernel
758 		 * should not happen.
759 		 *
760 		 * fall through...
761 		 */
762 		OS_FALLTHROUGH;
763 	default:
764 		/*
765 		 * Exception 15 is reserved but some chips may generate it
766 		 * spuriously. Seen at startup on AMD Athlon-64.
767 		 */
768 		if (type == 15) {
769 			kprintf("kernel_trap() ignoring spurious trap 15\n");
770 			goto common_return;
771 		}
772 debugger_entry:
773 		/* Ensure that the i386_kernel_state at the base of the
774 		 * current thread's stack (if any) is synchronized with the
775 		 * context at the moment of the trap, to facilitate
776 		 * access through the debugger.
777 		 */
778 		sync_iss_to_iks(state);
779 #if  MACH_KDP
780 		if (kdp_i386_trap(type, saved_state, result, (vm_offset_t)vaddr)) {
781 			goto common_return;
782 		}
783 #endif
784 	}
785 	pal_cli();
786 	panic_trap(saved_state, trap_pl, fault_result);
787 	/*
788 	 * NO RETURN
789 	 */
790 
791 common_return:
792 #if DEVELOPMENT || DEBUG
793 	if (traptrace_index != TRAPTRACE_INVALID_INDEX) {
794 		traptrace_end(traptrace_index, mach_absolute_time());
795 	}
796 #endif
797 	return;
798 }
799 
800 static void
set_recovery_ip(x86_saved_state64_t * saved_state,vm_offset_t ip)801 set_recovery_ip(x86_saved_state64_t  *saved_state, vm_offset_t ip)
802 {
803 	saved_state->isf.rip = ip;
804 }
805 
806 static void
panic_trap(x86_saved_state64_t * regs,uint32_t pl,kern_return_t fault_result)807 panic_trap(x86_saved_state64_t *regs, uint32_t pl, kern_return_t fault_result)
808 {
809 	const char      *trapname = "Unknown";
810 	pal_cr_t        cr0, cr2, cr3, cr4;
811 	boolean_t       potential_smep_fault = FALSE, potential_kernel_NX_fault = FALSE;
812 	boolean_t       potential_smap_fault = FALSE;
813 
814 	pal_get_control_registers( &cr0, &cr2, &cr3, &cr4 );
815 	assert(ml_get_interrupts_enabled() == FALSE);
816 	current_cpu_datap()->cpu_fatal_trap_state = regs;
817 	/*
818 	 * Issue an I/O port read if one has been requested - this is an
819 	 * event logic analyzers can use as a trigger point.
820 	 */
821 	panic_notify();
822 
823 	kprintf("CPU %d panic trap number 0x%x, rip 0x%016llx\n",
824 	    cpu_number(), regs->isf.trapno, regs->isf.rip);
825 	kprintf("cr0 0x%016llx cr2 0x%016llx cr3 0x%016llx cr4 0x%016llx\n",
826 	    cr0, cr2, cr3, cr4);
827 
828 	if (regs->isf.trapno < TRAP_TYPES) {
829 		trapname = trap_type[regs->isf.trapno];
830 	}
831 
832 	if ((regs->isf.trapno == T_PAGE_FAULT) && (regs->isf.err == (T_PF_PROT | T_PF_EXECUTE)) && (regs->isf.rip == regs->cr2)) {
833 		if (pmap_smep_enabled && (regs->isf.rip < VM_MAX_USER_PAGE_ADDRESS)) {
834 			potential_smep_fault = TRUE;
835 		} else if (regs->isf.rip >= VM_MIN_KERNEL_AND_KEXT_ADDRESS) {
836 			potential_kernel_NX_fault = TRUE;
837 		}
838 	} else if (pmap_smap_enabled &&
839 	    regs->isf.trapno == T_PAGE_FAULT &&
840 	    regs->isf.err & T_PF_PROT &&
841 	    regs->cr2 < VM_MAX_USER_PAGE_ADDRESS &&
842 	    regs->isf.rip >= VM_MIN_KERNEL_AND_KEXT_ADDRESS) {
843 		potential_smap_fault = TRUE;
844 	}
845 
846 #undef panic
847 	panic("Kernel trap at 0x%016llx, type %d=%s, registers:\n"
848 	    "CR0: 0x%016llx, CR2: 0x%016llx, CR3: 0x%016llx, CR4: 0x%016llx\n"
849 	    "RAX: 0x%016llx, RBX: 0x%016llx, RCX: 0x%016llx, RDX: 0x%016llx\n"
850 	    "RSP: 0x%016llx, RBP: 0x%016llx, RSI: 0x%016llx, RDI: 0x%016llx\n"
851 	    "R8:  0x%016llx, R9:  0x%016llx, R10: 0x%016llx, R11: 0x%016llx\n"
852 	    "R12: 0x%016llx, R13: 0x%016llx, R14: 0x%016llx, R15: 0x%016llx\n"
853 	    "RFL: 0x%016llx, RIP: 0x%016llx, CS:  0x%016llx, SS:  0x%016llx\n"
854 	    "Fault CR2: 0x%016llx, Error code: 0x%016llx, Fault CPU: 0x%x%s%s%s%s, PL: %d, VF: %d\n",
855 	    regs->isf.rip, regs->isf.trapno, trapname,
856 	    cr0, cr2, cr3, cr4,
857 	    regs->rax, regs->rbx, regs->rcx, regs->rdx,
858 	    regs->isf.rsp, regs->rbp, regs->rsi, regs->rdi,
859 	    regs->r8, regs->r9, regs->r10, regs->r11,
860 	    regs->r12, regs->r13, regs->r14, regs->r15,
861 	    regs->isf.rflags, regs->isf.rip, regs->isf.cs & 0xFFFF,
862 	    regs->isf.ss & 0xFFFF, regs->cr2, regs->isf.err, regs->isf.cpu,
863 	    virtualized ? " VMM" : "",
864 	    potential_kernel_NX_fault ? " Kernel NX fault" : "",
865 	    potential_smep_fault ? " SMEP/User NX fault" : "",
866 	    potential_smap_fault ? " SMAP fault" : "",
867 	    pl,
868 	    fault_result);
869 }
870 
871 #if CONFIG_DTRACE
872 extern kern_return_t dtrace_user_probe(x86_saved_state_t *);
873 #endif
874 
875 #if DEBUG
876 uint32_t fsigs[2];
877 uint32_t fsigns, fsigcs;
878 #endif
879 
880 /*
881  *	Trap from user mode.
882  */
883 void
user_trap(x86_saved_state_t * saved_state)884 user_trap(
885 	x86_saved_state_t *saved_state)
886 {
887 	int                     exc;
888 	int                     err;
889 	mach_exception_code_t   code;
890 	mach_exception_subcode_t subcode;
891 	int                     type;
892 	user_addr_t             vaddr;
893 	vm_prot_t               prot;
894 	thread_t                thread = current_thread();
895 	kern_return_t           kret;
896 	user_addr_t             rip;
897 	unsigned long           dr6 = 0; /* 32 bit for i386, 64 bit for x86_64 */
898 	int                     current_cpu = cpu_number();
899 #if DEVELOPMENT || DEBUG
900 	bool                    inspect_cacheline = false;
901 	uint32_t                traptrace_index;
902 #endif
903 	assert((is_saved_state32(saved_state) && !thread_is_64bit_addr(thread)) ||
904 	    (is_saved_state64(saved_state) && thread_is_64bit_addr(thread)));
905 
906 	if (is_saved_state64(saved_state)) {
907 		x86_saved_state64_t     *regs;
908 
909 		regs = saved_state64(saved_state);
910 
911 		/* Record cpu where state was captured */
912 		regs->isf.cpu = current_cpu;
913 
914 		type = regs->isf.trapno;
915 		err  = (int)regs->isf.err & 0xffff;
916 		vaddr = (user_addr_t)regs->cr2;
917 		rip   = (user_addr_t)regs->isf.rip;
918 #if DEVELOPMENT || DEBUG
919 		traptrace_index = traptrace_start(type, rip, mach_absolute_time(), regs->rbp);
920 #endif
921 	} else {
922 		x86_saved_state32_t     *regs;
923 
924 		regs = saved_state32(saved_state);
925 
926 		/* Record cpu where state was captured */
927 		regs->cpu = current_cpu;
928 
929 		type  = regs->trapno;
930 		err   = regs->err & 0xffff;
931 		vaddr = (user_addr_t)regs->cr2;
932 		rip   = (user_addr_t)regs->eip;
933 #if DEVELOPMENT || DEBUG
934 		traptrace_index = traptrace_start(type, rip, mach_absolute_time(), regs->ebp);
935 #endif
936 	}
937 
938 #if DEVELOPMENT || DEBUG
939 	/*
940 	 * Copy the cacheline of code into the thread's instruction stream save area
941 	 * before enabling interrupts (the assumption is that we have not otherwise faulted or
942 	 * trapped since the original cache line stores).  If the saved code is not valid,
943 	 * we'll catch it below when we process the copyin() for unhandled faults.
944 	 */
945 	if (thread->machine.insn_copy_optout == false &&
946 	    (type == T_PAGE_FAULT || type == T_INVALID_OPCODE || type == T_GENERAL_PROTECTION)) {
947 #define CACHELINE_SIZE 64
948 		THREAD_TO_PCB(thread)->insn_cacheline[CACHELINE_SIZE] = (uint8_t)(rip & (CACHELINE_SIZE - 1));
949 		bcopy(&cpu_shadowp(current_cpu)->cpu_rtimes[0],
950 		    &THREAD_TO_PCB(thread)->insn_cacheline[0],
951 		    sizeof(THREAD_TO_PCB(thread)->insn_cacheline) - 1);
952 		inspect_cacheline = true;
953 	}
954 #endif
955 
956 	if (type == T_DEBUG) {
957 		if (thread->machine.ids) {
958 			unsigned long clear = 0;
959 			/* Stash and clear this processor's DR6 value, in the event
960 			 * this was a debug register match
961 			 */
962 			__asm__ volatile ("mov %%db6, %0" : "=r" (dr6));
963 			__asm__ volatile ("mov %0, %%db6" : : "r" (clear));
964 		}
965 		/* [Re]Enable LBRs *BEFORE* enabling interrupts to ensure we hit the right CPU */
966 		i386_lbr_enable();
967 	}
968 
969 	pal_sti();
970 
971 	KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
972 	    (MACHDBG_CODE(DBG_MACH_EXCP_UTRAP_x86, type)) | DBG_FUNC_NONE,
973 	    (unsigned)(vaddr >> 32), (unsigned)vaddr,
974 	    (unsigned)(rip >> 32), (unsigned)rip, 0);
975 
976 	code = 0;
977 	subcode = 0;
978 	exc = 0;
979 
980 #if CONFIG_DTRACE
981 	/*
982 	 * DTrace does not consume all user traps, only INT_3's for now.
983 	 * Avoid needlessly calling tempDTraceTrapHook here, and let the
984 	 * INT_3 case handle them.
985 	 */
986 #endif
987 
988 	DEBUG_KPRINT_SYSCALL_MASK(1,
989 	    "user_trap: type=0x%x(%s) err=0x%x cr2=%p rip=%p\n",
990 	    type, trap_type[type], err, (void *)(long) vaddr, (void *)(long) rip);
991 
992 	switch (type) {
993 	case T_DIVIDE_ERROR:
994 		exc = EXC_ARITHMETIC;
995 		code = EXC_I386_DIV;
996 		break;
997 
998 	case T_DEBUG:
999 	{
1000 		pcb_t   pcb;
1001 		/*
1002 		 * Update the PCB with this processor's DR6 value
1003 		 * in the event this was a debug register match.
1004 		 */
1005 		pcb = THREAD_TO_PCB(thread);
1006 		if (pcb->ids) {
1007 			/*
1008 			 * We can get and set the status register
1009 			 * in 32-bit mode even on a 64-bit thread
1010 			 * because the high order bits are not
1011 			 * used on x86_64
1012 			 */
1013 			if (thread_is_64bit_addr(thread)) {
1014 				x86_debug_state64_t *ids = pcb->ids;
1015 				ids->dr6 = dr6;
1016 			} else {         /* 32 bit thread */
1017 				x86_debug_state32_t *ids = pcb->ids;
1018 				ids->dr6 = (uint32_t) dr6;
1019 			}
1020 		}
1021 		exc = EXC_BREAKPOINT;
1022 		code = EXC_I386_SGL;
1023 		break;
1024 	}
1025 	case T_INT3:
1026 #if CONFIG_DTRACE
1027 		if (dtrace_user_probe(saved_state) == KERN_SUCCESS) {
1028 			return; /* If it succeeds, we are done... */
1029 		}
1030 #endif
1031 		exc = EXC_BREAKPOINT;
1032 		code = EXC_I386_BPT;
1033 		break;
1034 
1035 	case T_OVERFLOW:
1036 		exc = EXC_ARITHMETIC;
1037 		code = EXC_I386_INTO;
1038 		break;
1039 
1040 	case T_OUT_OF_BOUNDS:
1041 		exc = EXC_SOFTWARE;
1042 		code = EXC_I386_BOUND;
1043 		break;
1044 
1045 	case T_INVALID_OPCODE:
1046 		if (fpUDflt(rip) == 1) {
1047 			exc = EXC_BAD_INSTRUCTION;
1048 			code = EXC_I386_INVOP;
1049 		}
1050 		break;
1051 
1052 	case T_NO_FPU:
1053 		fpnoextflt();
1054 		break;
1055 
1056 	case T_FPU_FAULT:
1057 		fpextovrflt();
1058 		/*
1059 		 * Raise exception.
1060 		 */
1061 		exc = EXC_BAD_ACCESS;
1062 		code = VM_PROT_READ | VM_PROT_EXECUTE;
1063 		subcode = 0;
1064 		break;
1065 
1066 	case T_INVALID_TSS:     /* invalid TSS == iret with NT flag set */
1067 		exc = EXC_BAD_INSTRUCTION;
1068 		code = EXC_I386_INVTSSFLT;
1069 		subcode = err;
1070 		break;
1071 
1072 	case T_SEGMENT_NOT_PRESENT:
1073 		exc = EXC_BAD_INSTRUCTION;
1074 		code = EXC_I386_SEGNPFLT;
1075 		subcode = err;
1076 		break;
1077 
1078 	case T_STACK_FAULT:
1079 		exc = EXC_BAD_INSTRUCTION;
1080 		code = EXC_I386_STKFLT;
1081 		subcode = err;
1082 		break;
1083 
1084 	case T_GENERAL_PROTECTION:
1085 		/*
1086 		 * There's a wide range of circumstances which generate this
1087 		 * class of exception. From user-space, many involve bad
1088 		 * addresses (such as a non-canonical 64-bit address).
1089 		 * So we map this to EXC_BAD_ACCESS (and thereby SIGSEGV).
1090 		 * The trouble is cr2 doesn't contain the faulting address;
1091 		 * we'd need to decode the faulting instruction to really
1092 		 * determine this. We'll leave that to debuggers.
1093 		 * However, attempted execution of privileged instructions
1094 		 * (e.g. cli) also generate GP faults and so we map these to
1095 		 * to EXC_BAD_ACCESS (and thence SIGSEGV) also - rather than
1096 		 * EXC_BAD_INSTRUCTION which is more accurate. We just can't
1097 		 * win!
1098 		 */
1099 		exc = EXC_BAD_ACCESS;
1100 		code = EXC_I386_GPFLT;
1101 		subcode = err;
1102 		break;
1103 
1104 	case T_PAGE_FAULT:
1105 	{
1106 		prot = VM_PROT_READ;
1107 
1108 		if (err & T_PF_WRITE) {
1109 			prot |= VM_PROT_WRITE;
1110 		}
1111 		if (__improbable(err & T_PF_EXECUTE)) {
1112 			prot |= VM_PROT_EXECUTE;
1113 		}
1114 #if DEVELOPMENT || DEBUG
1115 		uint32_t fsig = 0;
1116 		fsig = thread_fpsimd_hash(thread);
1117 #if DEBUG
1118 		fsigs[0] = fsig;
1119 #endif
1120 #endif
1121 		kret = vm_fault(thread->map,
1122 		    vaddr,
1123 		    prot, FALSE, VM_KERN_MEMORY_NONE,
1124 		    THREAD_ABORTSAFE, NULL, 0);
1125 #if DEVELOPMENT || DEBUG
1126 		if (fsig) {
1127 			uint32_t fsig2 = thread_fpsimd_hash(thread);
1128 #if DEBUG
1129 			fsigcs++;
1130 			fsigs[1] = fsig2;
1131 #endif
1132 			if (fsig != fsig2) {
1133 				panic("FP/SIMD state hash mismatch across fault thread: %p 0x%x->0x%x", thread, fsig, fsig2);
1134 			}
1135 		} else {
1136 #if DEBUG
1137 			fsigns++;
1138 #endif
1139 		}
1140 #endif
1141 		if (__probable((kret == KERN_SUCCESS) || (kret == KERN_ABORTED))) {
1142 			break;
1143 		} else if (__improbable(kret == KERN_FAILURE)) {
1144 			/*
1145 			 * For a user trap, vm_fault() should never return KERN_FAILURE.
1146 			 * If it does, we're leaking preemption disables somewhere in the kernel.
1147 			 */
1148 			panic("vm_fault() KERN_FAILURE from user fault on thread %p", thread);
1149 		}
1150 
1151 		/* PAL debug hook (empty on x86) */
1152 		pal_dbg_page_fault(thread, vaddr, kret);
1153 		exc = EXC_BAD_ACCESS;
1154 		code = kret;
1155 		subcode = vaddr;
1156 	}
1157 	break;
1158 
1159 	case T_SSE_FLOAT_ERROR:
1160 		fpSSEexterrflt();
1161 		exc = EXC_ARITHMETIC;
1162 		code = EXC_I386_SSEEXTERR;
1163 		subcode = ((struct x86_fx_thread_state *)thread->machine.ifps)->fx_MXCSR;
1164 		break;
1165 
1166 
1167 	case T_FLOATING_POINT_ERROR:
1168 		fpexterrflt();
1169 		exc = EXC_ARITHMETIC;
1170 		code = EXC_I386_EXTERR;
1171 		subcode = ((struct x86_fx_thread_state *)thread->machine.ifps)->fx_status;
1172 		break;
1173 
1174 	case T_DTRACE_RET:
1175 #if CONFIG_DTRACE
1176 		if (dtrace_user_probe(saved_state) == KERN_SUCCESS) {
1177 			return; /* If it succeeds, we are done... */
1178 		}
1179 #endif
1180 		/*
1181 		 * If we get an INT 0x7f when we do not expect to,
1182 		 * treat it as an illegal instruction
1183 		 */
1184 		exc = EXC_BAD_INSTRUCTION;
1185 		code = EXC_I386_INVOP;
1186 		break;
1187 
1188 	default:
1189 		panic("Unexpected user trap, type %d", type);
1190 	}
1191 
1192 	if (exc != 0) {
1193 		uint16_t cs;
1194 		boolean_t intrs;
1195 
1196 		if (is_saved_state64(saved_state)) {
1197 			cs = saved_state64(saved_state)->isf.cs;
1198 		} else {
1199 			cs = saved_state32(saved_state)->cs;
1200 		}
1201 
1202 		if (last_branch_enabled_modes == LBR_ENABLED_USERMODE) {
1203 			intrs = ml_set_interrupts_enabled(FALSE);
1204 			/*
1205 			 * This is a bit racy (it's possible for this thread to migrate to another CPU, then
1206 			 * migrate back, but that seems rather rare in practice), but good enough to ensure
1207 			 * the LBRs are saved before proceeding with exception/signal dispatch.
1208 			 */
1209 			if (current_cpu == cpu_number()) {
1210 				i386_lbr_synch(thread);
1211 			}
1212 			ml_set_interrupts_enabled(intrs);
1213 		}
1214 
1215 		/*
1216 		 * Do not try to copyin from the instruction stream if the page fault was due
1217 		 * to an access to rip and was unhandled.
1218 		 * Do not deal with cases when %cs != USER[64]_CS
1219 		 * And of course there's no need to copy the instruction stream if the boot-arg
1220 		 * was set to 0.
1221 		 */
1222 		if (thread->machine.insn_copy_optout == false && insn_copyin_count > 0 &&
1223 		    (cs == USER64_CS || cs == USER_CS) && (type != T_PAGE_FAULT || vaddr != rip)) {
1224 #if DEVELOPMENT || DEBUG
1225 			copy_instruction_stream(thread, rip, type, inspect_cacheline);
1226 #else
1227 			copy_instruction_stream(thread, rip, type);
1228 #endif
1229 		}
1230 
1231 #if DEVELOPMENT || DEBUG
1232 		if (traptrace_index != TRAPTRACE_INVALID_INDEX) {
1233 			traptrace_end(traptrace_index, mach_absolute_time());
1234 		}
1235 #endif
1236 		/*
1237 		 * Note: Codepaths that directly return from user_trap() have pending
1238 		 * ASTs processed in locore
1239 		 */
1240 		i386_exception(exc, code, subcode);
1241 		/* NOTREACHED */
1242 	} else {
1243 #if DEVELOPMENT || DEBUG
1244 		if (traptrace_index != TRAPTRACE_INVALID_INDEX) {
1245 			traptrace_end(traptrace_index, mach_absolute_time());
1246 		}
1247 #endif
1248 	}
1249 }
1250 
1251 /*
1252  * Copyin up to x86_INSTRUCTION_STATE_MAX_INSN_BYTES bytes from the page that includes `rip`,
1253  * ensuring that we stay on the same page, clipping the start or end, as needed.
1254  * Add the clipped amount back at the start or end, depending on where it fits.
1255  * Consult the variable populated by the boot-arg `insn_capcnt'
1256  */
1257 static __attribute__((noinline)) void
copy_instruction_stream(thread_t thread,uint64_t rip,int __unused trap_code,bool inspect_cacheline)1258 copy_instruction_stream(thread_t thread, uint64_t rip, int __unused trap_code
1259 #if DEVELOPMENT || DEBUG
1260     , bool inspect_cacheline
1261 #endif
1262     )
1263 {
1264 #if x86_INSTRUCTION_STATE_MAX_INSN_BYTES > 4096
1265 #error x86_INSTRUCTION_STATE_MAX_INSN_BYTES cannot exceed a page in size.
1266 #endif
1267 	pcb_t pcb = THREAD_TO_PCB(thread);
1268 	vm_map_offset_t pagemask = ~vm_map_page_mask(current_map());
1269 	vm_map_offset_t rip_page = rip & pagemask;
1270 	vm_map_offset_t start_addr;
1271 	vm_map_offset_t insn_offset;
1272 	vm_map_offset_t end_addr = rip + (insn_copyin_count / 2);
1273 	void *stack_buffer;
1274 	int copyin_err = 0;
1275 #if defined(MACH_BSD) && (DEVELOPMENT || DEBUG)
1276 	void *procname;
1277 #endif
1278 
1279 #if DEVELOPMENT || DEBUG
1280 	assert(insn_copyin_count <= x86_INSTRUCTION_STATE_MAX_INSN_BYTES);
1281 #else
1282 	if (insn_copyin_count > x86_INSTRUCTION_STATE_MAX_INSN_BYTES ||
1283 	    insn_copyin_count < 64 /* CACHELINE_SIZE */) {
1284 		return;
1285 	}
1286 #endif
1287 
1288 #pragma clang diagnostic push
1289 #pragma clang diagnostic ignored "-Walloca"
1290 	stack_buffer = __builtin_alloca(insn_copyin_count);
1291 #pragma clang diagnostic pop
1292 
1293 	if (rip >= (insn_copyin_count / 2)) {
1294 		start_addr = rip - (insn_copyin_count / 2);
1295 	} else {
1296 		start_addr = 0;
1297 	}
1298 
1299 	if (start_addr < rip_page) {
1300 		insn_offset = (insn_copyin_count / 2) - (rip_page - start_addr);
1301 		end_addr += (rip_page - start_addr);
1302 		start_addr = rip_page;
1303 	} else if (end_addr >= (rip_page + (~pagemask + 1))) {
1304 		start_addr -= (end_addr - (rip_page + (~pagemask + 1))); /* Adjust start address backward */
1305 		/* Adjust instruction offset due to start address change */
1306 		insn_offset = (insn_copyin_count / 2) + (end_addr - (rip_page + (~pagemask + 1)));
1307 		end_addr = rip_page + (~pagemask + 1);  /* clip to the start of the next page (non-inclusive */
1308 	} else {
1309 		insn_offset = insn_copyin_count / 2;
1310 	}
1311 
1312 	disable_preemption();   /* Prevent copyin from faulting in the instruction stream */
1313 	if (
1314 #if DEVELOPMENT || DEBUG
1315 		(insnstream_force_cacheline_mismatch < 2) &&
1316 #endif
1317 		((end_addr > start_addr) && (copyin_err = copyin(start_addr, stack_buffer, end_addr - start_addr)) == 0)) {
1318 		enable_preemption();
1319 
1320 		if (pcb->insn_state == 0) {
1321 			pcb->insn_state = kalloc_data(sizeof(x86_instruction_state_t), Z_WAITOK);
1322 		}
1323 
1324 		if (pcb->insn_state != 0) {
1325 			bcopy(stack_buffer, pcb->insn_state->insn_bytes, end_addr - start_addr);
1326 			bzero(&pcb->insn_state->insn_bytes[end_addr - start_addr],
1327 			    insn_copyin_count - (end_addr - start_addr));
1328 
1329 			pcb->insn_state->insn_stream_valid_bytes = (int)(end_addr - start_addr);
1330 			pcb->insn_state->insn_offset = (int)insn_offset;
1331 
1332 #if DEVELOPMENT || DEBUG
1333 			/* Now try to validate the cacheline we read at early-fault time matches the code
1334 			 * copied in. Before we do that, we have to make sure the buffer contains a valid
1335 			 * cacheline by looking for the 2 sentinel values written in the event the cacheline
1336 			 * could not be copied.
1337 			 */
1338 #define CACHELINE_DATA_NOT_PRESENT 0xdeadc0debeefcafeULL
1339 #define CACHELINE_MASK (CACHELINE_SIZE - 1)
1340 
1341 			if (inspect_cacheline &&
1342 			    (*(uint64_t *)(uintptr_t)&pcb->insn_cacheline[0] != CACHELINE_DATA_NOT_PRESENT &&
1343 			    *(uint64_t *)(uintptr_t)&pcb->insn_cacheline[8] != CACHELINE_DATA_NOT_PRESENT)) {
1344 				/*
1345 				 * The position of the cacheline in the instruction buffer is at offset
1346 				 * insn_offset - (rip & CACHELINE_MASK)
1347 				 */
1348 				if (__improbable((rip & CACHELINE_MASK) > insn_offset)) {
1349 					printf("thread %p code cacheline @ %p clipped wrt copied-in code (offset %d)\n",
1350 					    thread, (void *)(rip & ~CACHELINE_MASK), (int)(rip & CACHELINE_MASK));
1351 				} else if (bcmp(&pcb->insn_state->insn_bytes[insn_offset - (rip & CACHELINE_MASK)],
1352 				    &pcb->insn_cacheline[0], CACHELINE_SIZE) != 0
1353 				    || insnstream_force_cacheline_mismatch
1354 				    ) {
1355 #if x86_INSTRUCTION_STATE_CACHELINE_SIZE != CACHELINE_SIZE
1356 #error cacheline size mismatch
1357 #endif
1358 					bcopy(&pcb->insn_cacheline[0], &pcb->insn_state->insn_cacheline[0],
1359 					    x86_INSTRUCTION_STATE_CACHELINE_SIZE);
1360 					/* Mark the instruction stream as being out-of-synch */
1361 					pcb->insn_state->out_of_synch = 1;
1362 
1363 					printf("thread %p code cacheline @ %p mismatches with copied-in code [trap 0x%x]\n",
1364 					    thread, (void *)(rip & ~CACHELINE_MASK), trap_code);
1365 					for (int i = 0; i < 8; i++) {
1366 						printf("\t[%d] cl=0x%08llx vs. ci=0x%08llx\n", i, *(uint64_t *)(uintptr_t)&pcb->insn_cacheline[i * 8],
1367 						    *(uint64_t *)(uintptr_t)&pcb->insn_state->insn_bytes[(i * 8) + insn_offset - (rip & CACHELINE_MASK)]);
1368 					}
1369 					if (panic_on_cacheline_mismatch) {
1370 						panic("Cacheline mismatch while processing unhandled exception.");
1371 					}
1372 				} else {
1373 					pcb->insn_state->out_of_synch = 0;
1374 				}
1375 			} else if (inspect_cacheline) {
1376 				printf("thread %p could not capture code cacheline at fault IP %p [offset %d]\n",
1377 				    (void *)thread, (void *)rip, (int)(insn_offset - (rip & CACHELINE_MASK)));
1378 				pcb->insn_state->out_of_synch = 0;
1379 			}
1380 #else
1381 			pcb->insn_state->out_of_synch = 0;
1382 #endif /* DEVELOPMENT || DEBUG */
1383 
1384 #if defined(MACH_BSD) && (DEVELOPMENT || DEBUG)
1385 			if (panic_on_trap_procname[0] != 0) {
1386 				task_t task = get_threadtask(thread);
1387 				char procnamebuf[65] = {0};
1388 
1389 				if (task->bsd_info != NULL) {
1390 					procname = proc_name_address(task->bsd_info);
1391 					strlcpy(procnamebuf, procname, sizeof(procnamebuf));
1392 
1393 					if (strcasecmp(panic_on_trap_procname, procnamebuf) == 0 &&
1394 					    ((1U << trap_code) & panic_on_trap_mask) != 0) {
1395 						panic("Panic requested on trap type 0x%x for process `%s'", trap_code,
1396 						    panic_on_trap_procname);
1397 						/*NORETURN*/
1398 					}
1399 				}
1400 			}
1401 #endif /* MACH_BSD && (DEVELOPMENT || DEBUG) */
1402 		}
1403 	} else {
1404 		enable_preemption();
1405 
1406 		pcb->insn_state_copyin_failure_errorcode = copyin_err;
1407 #if DEVELOPMENT || DEBUG
1408 		if (inspect_cacheline && pcb->insn_state == 0) {
1409 			pcb->insn_state = kalloc_data(sizeof(x86_instruction_state_t), Z_WAITOK);
1410 		}
1411 		if (pcb->insn_state != 0) {
1412 			pcb->insn_state->insn_stream_valid_bytes = 0;
1413 			pcb->insn_state->insn_offset = 0;
1414 
1415 			if (inspect_cacheline &&
1416 			    (*(uint64_t *)(uintptr_t)&pcb->insn_cacheline[0] != CACHELINE_DATA_NOT_PRESENT &&
1417 			    *(uint64_t *)(uintptr_t)&pcb->insn_cacheline[8] != CACHELINE_DATA_NOT_PRESENT)) {
1418 				/*
1419 				 * We can still copy the cacheline into the instruction state structure
1420 				 * if it contains valid data
1421 				 */
1422 				pcb->insn_state->out_of_synch = 1;
1423 				bcopy(&pcb->insn_cacheline[0], &pcb->insn_state->insn_cacheline[0],
1424 				    x86_INSTRUCTION_STATE_CACHELINE_SIZE);
1425 			}
1426 		}
1427 #endif /* DEVELOPMENT || DEBUG */
1428 	}
1429 }
1430 
1431 /*
1432  * Handle exceptions for i386.
1433  *
1434  * If we are an AT bus machine, we must turn off the AST for a
1435  * delayed floating-point exception.
1436  *
1437  * If we are providing floating-point emulation, we may have
1438  * to retrieve the real register values from the floating point
1439  * emulator.
1440  */
1441 void
i386_exception(int exc,mach_exception_code_t code,mach_exception_subcode_t subcode)1442 i386_exception(
1443 	int     exc,
1444 	mach_exception_code_t code,
1445 	mach_exception_subcode_t subcode)
1446 {
1447 	mach_exception_data_type_t   codes[EXCEPTION_CODE_MAX];
1448 
1449 	DEBUG_KPRINT_SYSCALL_MACH("i386_exception: exc=%d code=0x%llx subcode=0x%llx\n",
1450 	    exc, code, subcode);
1451 	codes[0] = code;                /* new exception interface */
1452 	codes[1] = subcode;
1453 	exception_triage(exc, codes, 2);
1454 	/*NOTREACHED*/
1455 }
1456 
1457 
1458 /* Synchronize a thread's x86_kernel_state (if any) with the given
1459  * x86_saved_state_t obtained from the trap/IPI handler; called in
1460  * kernel_trap() prior to entering the debugger, and when receiving
1461  * an "MP_KDP" IPI. Called with null saved_state if an incoming IPI
1462  * was detected from the kernel while spinning with interrupts masked.
1463  */
1464 
1465 void
sync_iss_to_iks(x86_saved_state_t * saved_state)1466 sync_iss_to_iks(x86_saved_state_t *saved_state)
1467 {
1468 	struct x86_kernel_state *iks = NULL;
1469 	vm_offset_t kstack;
1470 	boolean_t record_active_regs = FALSE;
1471 
1472 	/* The PAL may have a special way to sync registers */
1473 	if (saved_state && saved_state->flavor == THREAD_STATE_NONE) {
1474 		pal_get_kern_regs( saved_state );
1475 	}
1476 
1477 	if (current_thread() != NULL &&
1478 	    (kstack = current_thread()->kernel_stack) != 0) {
1479 		x86_saved_state64_t     *regs = saved_state64(saved_state);
1480 
1481 		iks = STACK_IKS(kstack);
1482 
1483 		/* Did we take the trap/interrupt in kernel mode? */
1484 		if (saved_state == NULL || /* NULL => polling in kernel */
1485 		    regs == USER_REGS64(current_thread())) {
1486 			record_active_regs = TRUE;
1487 		} else {
1488 			iks->k_rbx = regs->rbx;
1489 			iks->k_rsp = regs->isf.rsp;
1490 			iks->k_rbp = regs->rbp;
1491 			iks->k_r12 = regs->r12;
1492 			iks->k_r13 = regs->r13;
1493 			iks->k_r14 = regs->r14;
1494 			iks->k_r15 = regs->r15;
1495 			iks->k_rip = regs->isf.rip;
1496 		}
1497 	}
1498 
1499 	if (record_active_regs == TRUE) {
1500 		/* Show the trap handler path */
1501 		__asm__ volatile ("movq %%rbx, %0" : "=m" (iks->k_rbx));
1502 		__asm__ volatile ("movq %%rsp, %0" : "=m" (iks->k_rsp));
1503 		__asm__ volatile ("movq %%rbp, %0" : "=m" (iks->k_rbp));
1504 		__asm__ volatile ("movq %%r12, %0" : "=m" (iks->k_r12));
1505 		__asm__ volatile ("movq %%r13, %0" : "=m" (iks->k_r13));
1506 		__asm__ volatile ("movq %%r14, %0" : "=m" (iks->k_r14));
1507 		__asm__ volatile ("movq %%r15, %0" : "=m" (iks->k_r15));
1508 		/* "Current" instruction pointer */
1509 		__asm__ volatile ("leaq 1f(%%rip), %%rax; mov %%rax, %0\n1:"
1510                                   : "=m" (iks->k_rip)
1511                                   :
1512                                   : "rax");
1513 	}
1514 }
1515 
1516 /*
1517  * This is used by the NMI interrupt handler (from mp.c) to
1518  * uncondtionally sync the trap handler context to the IKS
1519  * irrespective of whether the NMI was fielded in kernel
1520  * or user space.
1521  */
1522 void
sync_iss_to_iks_unconditionally(__unused x86_saved_state_t * saved_state)1523 sync_iss_to_iks_unconditionally(__unused x86_saved_state_t *saved_state)
1524 {
1525 	struct x86_kernel_state *iks;
1526 	vm_offset_t kstack;
1527 
1528 	if ((kstack = current_thread()->kernel_stack) != 0) {
1529 		iks = STACK_IKS(kstack);
1530 		/* Display the trap handler path */
1531 		__asm__ volatile ("movq %%rbx, %0" : "=m" (iks->k_rbx));
1532 		__asm__ volatile ("movq %%rsp, %0" : "=m" (iks->k_rsp));
1533 		__asm__ volatile ("movq %%rbp, %0" : "=m" (iks->k_rbp));
1534 		__asm__ volatile ("movq %%r12, %0" : "=m" (iks->k_r12));
1535 		__asm__ volatile ("movq %%r13, %0" : "=m" (iks->k_r13));
1536 		__asm__ volatile ("movq %%r14, %0" : "=m" (iks->k_r14));
1537 		__asm__ volatile ("movq %%r15, %0" : "=m" (iks->k_r15));
1538 		/* "Current" instruction pointer */
1539 		__asm__ volatile ("leaq 1f(%%rip), %%rax; mov %%rax, %0\n1:" : "=m" (iks->k_rip)::"rax");
1540 	}
1541 }
1542 
1543 #if DEBUG
1544 #define TERI 1
1545 #endif
1546 
1547 #if TERI
1548 extern void     thread_exception_return_internal(void) __dead2;
1549 
1550 void
thread_exception_return(void)1551 thread_exception_return(void)
1552 {
1553 	thread_t thread = current_thread();
1554 	task_t   task   = current_task();
1555 
1556 	ml_set_interrupts_enabled(FALSE);
1557 	if (thread_is_64bit_addr(thread) != task_has_64Bit_addr(task)) {
1558 		panic("Task/thread bitness mismatch %p %p, task: %d, thread: %d",
1559 		    thread, task, thread_is_64bit_addr(thread), task_has_64Bit_addr(task));
1560 	}
1561 
1562 	if (thread_is_64bit_addr(thread)) {
1563 		if ((gdt_desc_p(USER64_CS)->access & ACC_PL_U) == 0) {
1564 			panic("64-GDT mismatch %p, descriptor: %p", thread, gdt_desc_p(USER64_CS));
1565 		}
1566 	} else {
1567 		if ((gdt_desc_p(USER_CS)->access & ACC_PL_U) == 0) {
1568 			panic("32-GDT mismatch %p, descriptor: %p", thread, gdt_desc_p(USER_CS));
1569 		}
1570 	}
1571 	assert(get_preemption_level() == 0);
1572 	thread_exception_return_internal();
1573 }
1574 #endif
1575