xref: /xnu-11417.101.15/osfmk/arm64/sleh.c (revision e3723e1f17661b24996789d8afc084c0c3303b26)
1 /*
2  * Copyright (c) 2012-2023 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 #include <arm/caches_internal.h>
30 #include <arm/cpu_data.h>
31 #include <arm/cpu_data_internal.h>
32 #include <arm/misc_protos.h>
33 #include <arm/thread.h>
34 #include <arm/rtclock.h>
35 #include <arm/trap_internal.h> /* for IS_ARM_GDB_TRAP() et al */
36 #include <arm64/proc_reg.h>
37 #include <arm64/machine_machdep.h>
38 #include <arm64/monotonic.h>
39 #include <arm64/instructions.h>
40 
41 #include <kern/debug.h>
42 #include <kern/exc_guard.h>
43 #include <kern/restartable.h>
44 #include <kern/socd_client.h>
45 #include <kern/task.h>
46 #include <kern/thread.h>
47 #include <kern/zalloc_internal.h>
48 #include <mach/exception.h>
49 #include <mach/arm/traps.h>
50 #include <mach/vm_types.h>
51 #include <mach/machine/thread_status.h>
52 
53 #include <machine/atomic.h>
54 #include <machine/limits.h>
55 
56 #include <pexpert/arm/protos.h>
57 #include <pexpert/arm64/apple_arm64_cpu.h>
58 #include <pexpert/arm64/apple_arm64_regs.h>
59 #include <pexpert/arm64/board_config.h>
60 
61 #include <vm/vm_page.h>
62 #include <vm/pmap.h>
63 #include <vm/vm_fault.h>
64 #include <vm/vm_kern.h>
65 #include <vm/vm_map_xnu.h>
66 
67 #include <sys/errno.h>
68 #include <sys/kdebug.h>
69 #include <sys/code_signing.h>
70 #include <sys/reason.h>
71 #include <kperf/kperf.h>
72 
73 #include <kern/policy_internal.h>
74 #if CONFIG_TELEMETRY
75 #include <kern/telemetry.h>
76 #include <kern/trap_telemetry.h>
77 #endif
78 
79 #include <prng/entropy.h>
80 
81 
82 
83 
84 #include <arm64/platform_error_handler.h>
85 
86 #if KASAN_TBI
87 #include <san/kasan.h>
88 #endif /* KASAN_TBI */
89 
90 #if CONFIG_UBSAN_MINIMAL
91 #include <san/ubsan_minimal.h>
92 #endif
93 
94 
95 
96 #ifdef CONFIG_BTI_TELEMETRY
97 #include <arm64/bti_telemetry.h>
98 #endif /* CONFIG_BTI_TELEMETRY */
99 
100 #ifndef __arm64__
101 #error Should only be compiling for arm64.
102 #endif
103 
104 #if DEBUG || DEVELOPMENT
105 #define HAS_TELEMETRY_KERNEL_BRK 1
106 #endif
107 
108 
109 #define TEST_CONTEXT32_SANITY(context) \
110 	(context->ss.ash.flavor == ARM_SAVED_STATE32 && context->ss.ash.count == ARM_SAVED_STATE32_COUNT && \
111 	 context->ns.nsh.flavor == ARM_NEON_SAVED_STATE32 && context->ns.nsh.count == ARM_NEON_SAVED_STATE32_COUNT)
112 
113 #define TEST_CONTEXT64_SANITY(context) \
114 	(context->ss.ash.flavor == ARM_SAVED_STATE64 && context->ss.ash.count == ARM_SAVED_STATE64_COUNT && \
115 	 context->ns.nsh.flavor == ARM_NEON_SAVED_STATE64 && context->ns.nsh.count == ARM_NEON_SAVED_STATE64_COUNT)
116 
117 #define ASSERT_CONTEXT_SANITY(context) \
118 	assert(TEST_CONTEXT32_SANITY(context) || TEST_CONTEXT64_SANITY(context))
119 
120 
121 #define COPYIN(src, dst, size)                           \
122 	(PSR64_IS_KERNEL(get_saved_state_cpsr(state))) ? \
123 	copyin_kern(src, dst, size) :                    \
124 	copyin(src, dst, size)
125 
126 #define COPYOUT(src, dst, size)                          \
127 	(PSR64_IS_KERNEL(get_saved_state_cpsr(state))) ? \
128 	copyout_kern(src, dst, size)                   : \
129 	copyout(src, dst, size)
130 
131 // Below is for concatenating a string param to a string literal
132 #define STR1(x) #x
133 #define STR(x) STR1(x)
134 
135 #define ARM64_KDBG_CODE_KERNEL (0 << 8)
136 #define ARM64_KDBG_CODE_USER   (1 << 8)
137 #define ARM64_KDBG_CODE_GUEST  (2 << 8)
138 
139 _Static_assert(ARM64_KDBG_CODE_GUEST <= KDBG_CODE_MAX, "arm64 KDBG trace codes out of range");
140 _Static_assert(ARM64_KDBG_CODE_GUEST <= UINT16_MAX, "arm64 KDBG trace codes out of range");
141 
142 void panic_with_thread_kernel_state(const char *msg, arm_saved_state_t *ss) __abortlike;
143 
144 void sleh_synchronous_sp1(arm_context_t *, uint64_t, vm_offset_t) __abortlike;
145 void sleh_synchronous(arm_context_t *, uint64_t, vm_offset_t, bool);
146 
147 
148 
149 void sleh_irq(arm_saved_state_t *);
150 void sleh_fiq(arm_saved_state_t *);
151 void sleh_serror(arm_context_t *context, uint64_t esr, vm_offset_t far);
152 void sleh_invalid_stack(arm_context_t *context, uint64_t esr, vm_offset_t far) __dead2;
153 
154 static void sleh_interrupt_handler_prologue(arm_saved_state_t *, unsigned int type);
155 static void sleh_interrupt_handler_epilogue(void);
156 
157 static void handle_svc(arm_saved_state_t *);
158 static void handle_mach_absolute_time_trap(arm_saved_state_t *);
159 static void handle_mach_continuous_time_trap(arm_saved_state_t *);
160 
161 static void handle_msr_trap(arm_saved_state_t *state, uint64_t esr);
162 #if __has_feature(ptrauth_calls)
163 static void handle_pac_fail(arm_saved_state_t *state, uint64_t esr) __dead2;
164 static inline uint64_t fault_addr_bitmask(unsigned int bit_from, unsigned int bit_to);
165 #endif
166 static void handle_bti_fail(arm_saved_state_t *state, uint64_t esr);
167 extern kern_return_t arm_fast_fault(pmap_t, vm_map_address_t, vm_prot_t, bool, bool);
168 
169 static void handle_uncategorized(arm_saved_state_t *);
170 
171 static void handle_kernel_breakpoint(arm_saved_state_t *, uint64_t);
172 
173 static void handle_breakpoint(arm_saved_state_t *, uint64_t) __dead2;
174 
175 typedef void (*abort_inspector_t)(uint32_t, fault_status_t *, vm_prot_t *);
176 static void inspect_instruction_abort(uint32_t, fault_status_t *, vm_prot_t *);
177 static void inspect_data_abort(uint32_t, fault_status_t *, vm_prot_t *);
178 
179 static int is_vm_fault(fault_status_t);
180 static int is_translation_fault(fault_status_t);
181 static int is_alignment_fault(fault_status_t);
182 
183 typedef void (*abort_handler_t)(arm_saved_state_t *, uint64_t, vm_offset_t, fault_status_t, vm_prot_t, expected_fault_handler_t);
184 static void handle_user_abort(arm_saved_state_t *, uint64_t, vm_offset_t, fault_status_t, vm_prot_t, expected_fault_handler_t);
185 static void handle_kernel_abort(arm_saved_state_t *, uint64_t, vm_offset_t, fault_status_t, vm_prot_t, expected_fault_handler_t);
186 
187 static void handle_pc_align(arm_saved_state_t *ss) __dead2;
188 static void handle_sp_align(arm_saved_state_t *ss) __dead2;
189 static void handle_sw_step_debug(arm_saved_state_t *ss) __dead2;
190 static void handle_wf_trap(arm_saved_state_t *ss) __dead2;
191 static void handle_fp_trap(arm_saved_state_t *ss, uint64_t esr) __dead2;
192 #if HAS_ARM_FEAT_SME
193 static void handle_sme_trap(arm_saved_state_t *state, uint64_t esr);
194 #endif /* HAS_ARM_FEAT_SME */
195 
196 static void handle_watchpoint(vm_offset_t fault_addr) __dead2;
197 
198 static void handle_abort(arm_saved_state_t *, uint64_t, vm_offset_t, abort_inspector_t, abort_handler_t, expected_fault_handler_t);
199 
200 static void handle_user_trapped_instruction32(arm_saved_state_t *, uint64_t esr) __dead2;
201 
202 static void handle_simd_trap(arm_saved_state_t *, uint64_t esr) __dead2;
203 
204 extern void current_cached_proc_cred_update(void);
205 void   mach_syscall_trace_exit(unsigned int retval, unsigned int call_number);
206 
207 struct proc;
208 
209 typedef uint32_t arm64_instr_t;
210 
211 extern void
212 unix_syscall(struct arm_saved_state * regs, thread_t thread_act, struct proc * proc);
213 
214 extern void
215 mach_syscall(struct arm_saved_state*);
216 
217 #if CONFIG_SPTM
218 bool sleh_panic_lockdown_should_initiate_el1_sp0_sync(uint64_t esr, uint64_t elr, uint64_t far, uint64_t spsr);
219 #endif /* CONFIG_SPTM */
220 
221 #if CONFIG_DTRACE
222 extern kern_return_t dtrace_user_probe(arm_saved_state_t* regs);
223 extern boolean_t dtrace_tally_fault(user_addr_t);
224 
225 /*
226  * Traps for userland processing. Can't include bsd/sys/fasttrap_isa.h, so copy
227  * and paste the trap instructions
228  * over from that file. Need to keep these in sync!
229  */
230 #define FASTTRAP_ARM32_INSTR 0xe7ffdefc
231 #define FASTTRAP_THUMB32_INSTR 0xdefc
232 #define FASTTRAP_ARM64_INSTR 0xe7eeee7e
233 
234 #define FASTTRAP_ARM32_RET_INSTR 0xe7ffdefb
235 #define FASTTRAP_THUMB32_RET_INSTR 0xdefb
236 #define FASTTRAP_ARM64_RET_INSTR 0xe7eeee7d
237 
238 /* See <rdar://problem/4613924> */
239 perfCallback tempDTraceTrapHook = NULL; /* Pointer to DTrace fbt trap hook routine */
240 #endif
241 
242 
243 
244 extern void arm64_thread_exception_return(void) __dead2;
245 
246 #if defined(APPLETYPHOON)
247 #define CPU_NAME "Typhoon"
248 #elif defined(APPLETWISTER)
249 #define CPU_NAME "Twister"
250 #elif defined(APPLEHURRICANE)
251 #define CPU_NAME "Hurricane"
252 #elif defined(APPLELIGHTNING)
253 #define CPU_NAME "Lightning"
254 #elif defined(APPLEEVEREST)
255 #define CPU_NAME "Everest"
256 #elif defined(APPLEH16)
257 #define CPU_NAME "AppleH16"
258 #else
259 #define CPU_NAME "Unknown"
260 #endif
261 
262 #if (CONFIG_KERNEL_INTEGRITY && defined(KERNEL_INTEGRITY_WT))
263 #define ESR_WT_SERROR(esr) (((esr) & 0xffffff00) == 0xbf575400)
264 #define ESR_WT_REASON(esr) ((esr) & 0xff)
265 
266 #define WT_REASON_NONE           0
267 #define WT_REASON_INTEGRITY_FAIL 1
268 #define WT_REASON_BAD_SYSCALL    2
269 #define WT_REASON_NOT_LOCKED     3
270 #define WT_REASON_ALREADY_LOCKED 4
271 #define WT_REASON_SW_REQ         5
272 #define WT_REASON_PT_INVALID     6
273 #define WT_REASON_PT_VIOLATION   7
274 #define WT_REASON_REG_VIOLATION  8
275 #endif
276 
277 #if defined(HAS_IPI)
278 void cpu_signal_handler(void);
279 extern unsigned int gFastIPI;
280 #endif /* defined(HAS_IPI) */
281 
282 static arm_saved_state64_t *original_faulting_state = NULL;
283 
284 
285 TUNABLE(bool, fp_exceptions_enabled, "-fp_exceptions", false);
286 
287 extern const vm_map_address_t physmap_base;
288 extern const vm_map_address_t physmap_end;
289 extern vm_offset_t static_memory_end;
290 
291 /*
292  * Fault copyio_recovery_entry in copyin/copyout routines.
293  *
294  * Offets are expressed in bytes from &copy_recovery_table
295  */
296 struct copyio_recovery_entry {
297 	ptrdiff_t cre_start;
298 	ptrdiff_t cre_end;
299 	ptrdiff_t cre_recovery;
300 };
301 
302 extern struct copyio_recovery_entry copyio_recover_table[];
303 extern struct copyio_recovery_entry copyio_recover_table_end[];
304 
305 static inline ptrdiff_t
copyio_recovery_offset(uintptr_t addr)306 copyio_recovery_offset(uintptr_t addr)
307 {
308 	return (ptrdiff_t)(addr - (uintptr_t)copyio_recover_table);
309 }
310 
311 #if !HAS_APPLE_PAC
312 static inline uintptr_t
copyio_recovery_addr(ptrdiff_t offset)313 copyio_recovery_addr(ptrdiff_t offset)
314 {
315 	return (uintptr_t)copyio_recover_table + (uintptr_t)offset;
316 }
317 #endif
318 
319 static inline struct copyio_recovery_entry *
find_copyio_recovery_entry(uint64_t pc)320 find_copyio_recovery_entry(uint64_t pc)
321 {
322 	ptrdiff_t offset = copyio_recovery_offset(pc);
323 	struct copyio_recovery_entry *e;
324 
325 	for (e = copyio_recover_table; e < copyio_recover_table_end; e++) {
326 		if (offset >= e->cre_start && offset < e->cre_end) {
327 			return e;
328 		}
329 	}
330 
331 	return NULL;
332 }
333 
334 static inline int
is_vm_fault(fault_status_t status)335 is_vm_fault(fault_status_t status)
336 {
337 	switch (status) {
338 	case FSC_TRANSLATION_FAULT_L0:
339 	case FSC_TRANSLATION_FAULT_L1:
340 	case FSC_TRANSLATION_FAULT_L2:
341 	case FSC_TRANSLATION_FAULT_L3:
342 	case FSC_ACCESS_FLAG_FAULT_L1:
343 	case FSC_ACCESS_FLAG_FAULT_L2:
344 	case FSC_ACCESS_FLAG_FAULT_L3:
345 	case FSC_PERMISSION_FAULT_L1:
346 	case FSC_PERMISSION_FAULT_L2:
347 	case FSC_PERMISSION_FAULT_L3:
348 		return TRUE;
349 	default:
350 		return FALSE;
351 	}
352 }
353 
354 static inline int
is_translation_fault(fault_status_t status)355 is_translation_fault(fault_status_t status)
356 {
357 	switch (status) {
358 	case FSC_TRANSLATION_FAULT_L0:
359 	case FSC_TRANSLATION_FAULT_L1:
360 	case FSC_TRANSLATION_FAULT_L2:
361 	case FSC_TRANSLATION_FAULT_L3:
362 		return TRUE;
363 	default:
364 		return FALSE;
365 	}
366 }
367 
368 static inline int
is_permission_fault(fault_status_t status)369 is_permission_fault(fault_status_t status)
370 {
371 	switch (status) {
372 	case FSC_PERMISSION_FAULT_L1:
373 	case FSC_PERMISSION_FAULT_L2:
374 	case FSC_PERMISSION_FAULT_L3:
375 		return TRUE;
376 	default:
377 		return FALSE;
378 	}
379 }
380 
381 static inline int
is_alignment_fault(fault_status_t status)382 is_alignment_fault(fault_status_t status)
383 {
384 	return status == FSC_ALIGNMENT_FAULT;
385 }
386 
387 static inline int
is_parity_error(fault_status_t status)388 is_parity_error(fault_status_t status)
389 {
390 	switch (status) {
391 #if defined(ARM64_BOARD_CONFIG_T6020)
392 		/*
393 		 * H14 Erratum (rdar://61553243): Despite having FEAT_RAS implemented,
394 		 * FSC_SYNC_PARITY_X can be reported for data and instruction aborts
395 		 * and should be interpreted as FSC_SYNC_EXT_ABORT_x
396 		 */
397 #else
398 	/*
399 	 * TODO: According to ARM ARM, Async Parity (0b011001) is a DFSC that is
400 	 * only applicable to AArch32 HSR register. Can this be removed?
401 	 */
402 	case FSC_ASYNC_PARITY:
403 	case FSC_SYNC_PARITY:
404 	case FSC_SYNC_PARITY_TT_L1:
405 	case FSC_SYNC_PARITY_TT_L2:
406 	case FSC_SYNC_PARITY_TT_L3:
407 		return TRUE;
408 #endif
409 	default:
410 		return FALSE;
411 	}
412 }
413 
414 static inline int
is_sync_external_abort(fault_status_t status)415 is_sync_external_abort(fault_status_t status)
416 {
417 	switch (status) {
418 #if defined(ARM64_BOARD_CONFIG_T6020)
419 	/*
420 	 * H14 Erratum (rdar://61553243): Despite having FEAT_RAS implemented,
421 	 * FSC_SYNC_PARITY_x can be reported for data and instruction aborts
422 	 * and should be interpreted as FSC_SYNC_EXT_ABORT_x
423 	 */
424 	case FSC_SYNC_PARITY:
425 #endif /* defined(ARM64_BOARD_CONFIG_T6020) */
426 	case FSC_SYNC_EXT_ABORT:
427 		return TRUE;
428 	default:
429 		return FALSE;
430 	}
431 }
432 
433 static inline int
is_table_walk_error(fault_status_t status)434 is_table_walk_error(fault_status_t status)
435 {
436 	switch (status) {
437 	case FSC_SYNC_EXT_ABORT_TT_L1:
438 	case FSC_SYNC_EXT_ABORT_TT_L2:
439 	case FSC_SYNC_EXT_ABORT_TT_L3:
440 #if defined(ARM64_BOARD_CONFIG_T6020)
441 	/*
442 	 * H14 Erratum(rdar://61553243): Despite having FEAT_RAS implemented,
443 	 * FSC_SYNC_PARITY_x can be reported for data and instruction aborts
444 	 * and should be interpreted as FSC_SYNC_EXT_ABORT_x
445 	 */
446 	case FSC_SYNC_PARITY_TT_L1:
447 	case FSC_SYNC_PARITY_TT_L2:
448 	case FSC_SYNC_PARITY_TT_L3:
449 #endif /* defined(ARM64_BOARD_CONFIG_T6020) */
450 		return TRUE;
451 	default:
452 		return FALSE;
453 	}
454 }
455 
456 
457 
458 static inline int
is_servicible_fault(fault_status_t status,uint64_t esr)459 is_servicible_fault(fault_status_t status, uint64_t esr)
460 {
461 #pragma unused(esr)
462 	return is_vm_fault(status);
463 }
464 
465 __dead2 __unused
466 static void
arm64_implementation_specific_error(arm_saved_state_t * state,uint64_t esr,vm_offset_t far)467 arm64_implementation_specific_error(arm_saved_state_t *state, uint64_t esr, vm_offset_t far)
468 {
469 #pragma unused (state, esr, far)
470 	panic_plain("Unhandled implementation specific error\n");
471 }
472 
473 #if CONFIG_KERNEL_INTEGRITY
474 #pragma clang diagnostic push
475 #pragma clang diagnostic ignored "-Wunused-parameter"
476 static void
kernel_integrity_error_handler(uint64_t esr,vm_offset_t far)477 kernel_integrity_error_handler(uint64_t esr, vm_offset_t far)
478 {
479 #if defined(KERNEL_INTEGRITY_WT)
480 #if (DEVELOPMENT || DEBUG)
481 	if (ESR_WT_SERROR(esr)) {
482 		switch (ESR_WT_REASON(esr)) {
483 		case WT_REASON_INTEGRITY_FAIL:
484 			panic_plain("Kernel integrity, violation in frame 0x%016lx.", far);
485 		case WT_REASON_BAD_SYSCALL:
486 			panic_plain("Kernel integrity, bad syscall.");
487 		case WT_REASON_NOT_LOCKED:
488 			panic_plain("Kernel integrity, not locked.");
489 		case WT_REASON_ALREADY_LOCKED:
490 			panic_plain("Kernel integrity, already locked.");
491 		case WT_REASON_SW_REQ:
492 			panic_plain("Kernel integrity, software request.");
493 		case WT_REASON_PT_INVALID:
494 			panic_plain("Kernel integrity, encountered invalid TTE/PTE while "
495 			    "walking 0x%016lx.", far);
496 		case WT_REASON_PT_VIOLATION:
497 			panic_plain("Kernel integrity, violation in mapping 0x%016lx.",
498 			    far);
499 		case WT_REASON_REG_VIOLATION:
500 			panic_plain("Kernel integrity, violation in system register %d.",
501 			    (unsigned) far);
502 		default:
503 			panic_plain("Kernel integrity, unknown (esr=0x%08llx).", esr);
504 		}
505 	}
506 #else
507 	if (ESR_WT_SERROR(esr)) {
508 		panic_plain("SError esr: 0x%08llx far: 0x%016lx.", esr, far);
509 	}
510 #endif
511 #endif
512 }
513 #pragma clang diagnostic pop
514 #endif
515 
516 static void
arm64_platform_error(arm_saved_state_t * state,uint64_t esr,vm_offset_t far,platform_error_source_t source)517 arm64_platform_error(arm_saved_state_t *state, uint64_t esr, vm_offset_t far, platform_error_source_t source)
518 {
519 #if CONFIG_KERNEL_INTEGRITY
520 	kernel_integrity_error_handler(esr, far);
521 #endif
522 
523 	(void)source;
524 	cpu_data_t *cdp = getCpuDatap();
525 
526 	if (PE_handle_platform_error(far)) {
527 		return;
528 	} else if (cdp->platform_error_handler != NULL) {
529 		cdp->platform_error_handler(cdp->cpu_id, far);
530 	} else {
531 		arm64_implementation_specific_error(state, esr, far);
532 	}
533 }
534 
535 void
panic_with_thread_kernel_state(const char * msg,arm_saved_state_t * ss)536 panic_with_thread_kernel_state(const char *msg, arm_saved_state_t *ss)
537 {
538 	boolean_t ss_valid;
539 
540 	ss_valid = is_saved_state64(ss);
541 	arm_saved_state64_t *state = saved_state64(ss);
542 
543 	os_atomic_cmpxchg(&original_faulting_state, NULL, state, seq_cst);
544 
545 	// rdar://80659177
546 	// Read SoCD tracepoints up to twice — once the first time we call panic and
547 	// another time if we encounter a nested panic after that.
548 	static int twice = 2;
549 	if (twice > 0) {
550 		twice--;
551 		SOCD_TRACE_XNU(KERNEL_STATE_PANIC,
552 		    SOCD_TRACE_MODE_STICKY_TRACEPOINT,
553 		    ADDR(state->pc),
554 		    PACK_LSB(VALUE(state->lr), VALUE(ss_valid)),
555 		    PACK_2X32(VALUE(state->esr), VALUE(state->cpsr)),
556 		    VALUE(state->far));
557 	}
558 
559 
560 	panic_plain("%s at pc 0x%016llx, lr 0x%016llx (saved state: %p%s)\n"
561 	    "\t  x0:  0x%016llx x1:  0x%016llx  x2:  0x%016llx  x3:  0x%016llx\n"
562 	    "\t  x4:  0x%016llx x5:  0x%016llx  x6:  0x%016llx  x7:  0x%016llx\n"
563 	    "\t  x8:  0x%016llx x9:  0x%016llx  x10: 0x%016llx  x11: 0x%016llx\n"
564 	    "\t  x12: 0x%016llx x13: 0x%016llx  x14: 0x%016llx  x15: 0x%016llx\n"
565 	    "\t  x16: 0x%016llx x17: 0x%016llx  x18: 0x%016llx  x19: 0x%016llx\n"
566 	    "\t  x20: 0x%016llx x21: 0x%016llx  x22: 0x%016llx  x23: 0x%016llx\n"
567 	    "\t  x24: 0x%016llx x25: 0x%016llx  x26: 0x%016llx  x27: 0x%016llx\n"
568 	    "\t  x28: 0x%016llx fp:  0x%016llx  lr:  0x%016llx  sp:  0x%016llx\n"
569 	    "\t  pc:  0x%016llx cpsr: 0x%08x         esr: 0x%016llx  far: 0x%016llx\n",
570 	    msg, state->pc, state->lr, ss, (ss_valid ? "" : " INVALID"),
571 	    state->x[0], state->x[1], state->x[2], state->x[3],
572 	    state->x[4], state->x[5], state->x[6], state->x[7],
573 	    state->x[8], state->x[9], state->x[10], state->x[11],
574 	    state->x[12], state->x[13], state->x[14], state->x[15],
575 	    state->x[16], state->x[17], state->x[18], state->x[19],
576 	    state->x[20], state->x[21], state->x[22], state->x[23],
577 	    state->x[24], state->x[25], state->x[26], state->x[27],
578 	    state->x[28], state->fp, state->lr, state->sp,
579 	    state->pc, state->cpsr, state->esr, state->far);
580 }
581 
582 void
sleh_synchronous_sp1(arm_context_t * context,uint64_t esr,vm_offset_t far __unused)583 sleh_synchronous_sp1(arm_context_t *context, uint64_t esr, vm_offset_t far __unused)
584 {
585 	esr_exception_class_t  class = ESR_EC(esr);
586 	arm_saved_state_t    * state = &context->ss;
587 
588 	switch (class) {
589 	case ESR_EC_UNCATEGORIZED:
590 	{
591 #if (DEVELOPMENT || DEBUG)
592 		uint32_t instr = *((uint32_t*)get_saved_state_pc(state));
593 		if (IS_ARM_GDB_TRAP(instr)) {
594 			DebuggerCall(EXC_BREAKPOINT, state);
595 		}
596 		OS_FALLTHROUGH; // panic if we return from the debugger
597 #else
598 		panic_with_thread_kernel_state("Unexpected debugger trap while SP1 selected", state);
599 #endif /* (DEVELOPMENT || DEBUG) */
600 	}
601 	default:
602 		panic_with_thread_kernel_state("Synchronous exception taken while SP1 selected", state);
603 	}
604 }
605 
606 
607 __attribute__((noreturn))
608 void
thread_exception_return()609 thread_exception_return()
610 {
611 	thread_t thread = current_thread();
612 	if (thread->machine.exception_trace_code != 0) {
613 		KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
614 		    MACHDBG_CODE(DBG_MACH_EXCP_SYNC_ARM, thread->machine.exception_trace_code) | DBG_FUNC_END, 0, 0, 0, 0, 0);
615 		thread->machine.exception_trace_code = 0;
616 	}
617 
618 #if KASAN_TBI
619 	kasan_unpoison_curstack(true);
620 #endif /* KASAN_TBI */
621 	arm64_thread_exception_return();
622 	__builtin_unreachable();
623 }
624 
625 /*
626  * check whether task vtimers are running and set thread and CPU BSD AST
627  *
628  * must be called with interrupts masked so updates of fields are atomic
629  * must be emitted inline to avoid generating an FBT probe on the exception path
630  *
631  */
632 __attribute__((__always_inline__))
633 static inline void
task_vtimer_check(thread_t thread)634 task_vtimer_check(thread_t thread)
635 {
636 	task_t task = get_threadtask_early(thread);
637 
638 	if (__improbable(task != NULL && task->vtimers)) {
639 		thread_ast_set(thread, AST_BSD);
640 		thread->machine.CpuDatap->cpu_pending_ast |= AST_BSD;
641 	}
642 }
643 
644 #if MACH_ASSERT
645 /**
646  * A version of get_preemption_level() that works in early boot.
647  *
648  * If an exception is raised in early boot before the initial thread has been
649  * set up, then calling get_preemption_level() in the SLEH will trigger an
650  * infinitely-recursing exception. This function handles this edge case.
651  */
652 static inline int
sleh_get_preemption_level(void)653 sleh_get_preemption_level(void)
654 {
655 	if (__improbable(current_thread() == NULL)) {
656 		return 0;
657 	}
658 	return get_preemption_level();
659 }
660 #endif // MACH_ASSERT
661 
662 static inline bool
is_platform_error(uint64_t esr)663 is_platform_error(uint64_t esr)
664 {
665 	esr_exception_class_t class = ESR_EC(esr);
666 	uint32_t iss = ESR_ISS(esr);
667 	fault_status_t fault_code;
668 
669 	if (class == ESR_EC_DABORT_EL0 || class == ESR_EC_DABORT_EL1) {
670 		fault_code = ISS_DA_FSC(iss);
671 	} else if (class == ESR_EC_IABORT_EL0 || class == ESR_EC_IABORT_EL1) {
672 		fault_code = ISS_IA_FSC(iss);
673 	} else {
674 		return false;
675 	}
676 
677 	return is_parity_error(fault_code) || is_sync_external_abort(fault_code) ||
678 	       is_table_walk_error(fault_code);
679 }
680 
681 void
sleh_synchronous(arm_context_t * context,uint64_t esr,vm_offset_t far,__unused bool did_initiate_panic_lockdown)682 sleh_synchronous(arm_context_t *context, uint64_t esr, vm_offset_t far, __unused bool did_initiate_panic_lockdown)
683 {
684 	esr_exception_class_t  class   = ESR_EC(esr);
685 	arm_saved_state_t    * state   = &context->ss;
686 	thread_t               thread  = current_thread();
687 #if MACH_ASSERT
688 	int                    preemption_level = sleh_get_preemption_level();
689 #endif
690 	expected_fault_handler_t expected_fault_handler = NULL;
691 #ifdef CONFIG_XNUPOST
692 	expected_fault_handler_t saved_expected_fault_handler = NULL;
693 	uintptr_t saved_expected_fault_addr = 0;
694 	uintptr_t saved_expected_fault_pc = 0;
695 #endif /* CONFIG_XNUPOST */
696 
697 	ASSERT_CONTEXT_SANITY(context);
698 
699 	task_vtimer_check(thread);
700 
701 #if CONFIG_DTRACE
702 	/*
703 	 * Handle kernel DTrace probes as early as possible to minimize the likelihood
704 	 * that this path will itself trigger a DTrace probe, which would lead to infinite
705 	 * probe recursion.
706 	 */
707 	if (__improbable((class == ESR_EC_UNCATEGORIZED) && tempDTraceTrapHook &&
708 	    (tempDTraceTrapHook(EXC_BAD_INSTRUCTION, state, 0, 0) == KERN_SUCCESS))) {
709 #if CONFIG_SPTM
710 		if (__improbable(did_initiate_panic_lockdown)) {
711 			panic("Unexpectedly initiated lockdown for DTrace probe?");
712 		}
713 #endif
714 		return;
715 	}
716 #endif
717 	bool is_user = PSR64_IS_USER(get_saved_state_cpsr(state));
718 
719 #if CONFIG_SPTM
720 	// Lockdown should only be initiated for kernel exceptions
721 	assert(!(is_user && did_initiate_panic_lockdown));
722 #endif /* CONFIG_SPTM */
723 
724 	/*
725 	 * Use KERNEL_DEBUG_CONSTANT_IST here to avoid producing tracepoints
726 	 * that would disclose the behavior of PT_DENY_ATTACH processes.
727 	 */
728 	if (is_user) {
729 		/* Sanitize FAR (but only if the exception was taken from userspace) */
730 		switch (class) {
731 		case ESR_EC_IABORT_EL1:
732 		case ESR_EC_IABORT_EL0:
733 			/* If this is a SEA, since we can't trust FnV, just clear FAR from the save area. */
734 			if (ISS_IA_FSC(ESR_ISS(esr)) == FSC_SYNC_EXT_ABORT) {
735 				saved_state64(state)->far = 0;
736 			}
737 			break;
738 		case ESR_EC_DABORT_EL1:
739 		case ESR_EC_DABORT_EL0:
740 			/* If this is a SEA, since we can't trust FnV, just clear FAR from the save area. */
741 			if (ISS_DA_FSC(ESR_ISS(esr)) == FSC_SYNC_EXT_ABORT) {
742 				saved_state64(state)->far = 0;
743 			}
744 			break;
745 		case ESR_EC_WATCHPT_MATCH_EL1:
746 		case ESR_EC_WATCHPT_MATCH_EL0:
747 		case ESR_EC_PC_ALIGN:
748 			break;  /* FAR_ELx is valid */
749 		default:
750 			saved_state64(state)->far = 0;
751 			break;
752 		}
753 
754 		thread->machine.exception_trace_code = (uint16_t)(ARM64_KDBG_CODE_USER | class);
755 		KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
756 		    MACHDBG_CODE(DBG_MACH_EXCP_SYNC_ARM, thread->machine.exception_trace_code) | DBG_FUNC_START,
757 		    esr, far, get_saved_state_pc(state), 0, 0);
758 	} else {
759 		KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
760 		    MACHDBG_CODE(DBG_MACH_EXCP_SYNC_ARM, ARM64_KDBG_CODE_KERNEL | class) | DBG_FUNC_START,
761 		    esr, VM_KERNEL_ADDRHIDE(far), VM_KERNEL_UNSLIDE(get_saved_state_pc(state)), 0, 0);
762 	}
763 
764 	if (__improbable(ESR_INSTR_IS_2BYTES(esr))) {
765 		/*
766 		 * We no longer support 32-bit, which means no 2-byte
767 		 * instructions.
768 		 */
769 		if (is_user) {
770 			panic("Exception on 2-byte instruction, "
771 			    "context=%p, esr=%#llx, far=%p",
772 			    context, esr, (void *)far);
773 		} else {
774 			panic_with_thread_kernel_state("Exception on 2-byte instruction", state);
775 		}
776 	}
777 
778 #ifdef CONFIG_XNUPOST
779 	if (thread->machine.expected_fault_handler != NULL) {
780 		bool matching_fault_pc = false;
781 		saved_expected_fault_handler = thread->machine.expected_fault_handler;
782 		saved_expected_fault_addr = thread->machine.expected_fault_addr;
783 		saved_expected_fault_pc = thread->machine.expected_fault_pc;
784 
785 		thread->machine.expected_fault_handler = NULL;
786 		thread->machine.expected_fault_addr = 0;
787 		thread->machine.expected_fault_pc = 0;
788 
789 #if __has_feature(ptrauth_calls)
790 		/*
791 		 * Compare only the bits of PC which make up the virtual address.
792 		 * This ignores the upper bits, which may have been corrupted by HW in
793 		 * platform dependent ways to signal pointer authentication fault.
794 		 */
795 		uint64_t fault_addr_mask = fault_addr_bitmask(0, 64 - T1SZ_BOOT - 1);
796 		uint64_t masked_expected_pc = saved_expected_fault_pc & fault_addr_mask;
797 		uint64_t masked_saved_pc = get_saved_state_pc(state) & fault_addr_mask;
798 		matching_fault_pc = masked_expected_pc == masked_saved_pc;
799 #else
800 		matching_fault_pc =
801 		    (saved_expected_fault_pc == get_saved_state_pc(state));
802 #endif /* ptrauth_call */
803 		if (saved_expected_fault_addr == far ||
804 		    matching_fault_pc) {
805 			expected_fault_handler = saved_expected_fault_handler;
806 		}
807 	}
808 #endif /* CONFIG_XNUPOST */
809 
810 	if (__improbable(is_platform_error(esr))) {
811 		/*
812 		 * Must gather error info in platform error handler before
813 		 * thread is preempted to another core/cluster to guarantee
814 		 * accurate error details
815 		 */
816 
817 		arm64_platform_error(state, esr, far, PLAT_ERR_SRC_SYNC);
818 #if CONFIG_SPTM
819 		if (__improbable(did_initiate_panic_lockdown)) {
820 			panic("Panic lockdown initiated for platform error");
821 		}
822 #endif
823 		return;
824 	}
825 
826 	if (is_user && class == ESR_EC_DABORT_EL0) {
827 		thread_reset_pcs_will_fault(thread);
828 	}
829 
830 #if CONFIG_SPTM
831 	if (__improbable(did_initiate_panic_lockdown && current_thread() != NULL)) {
832 		/*
833 		 * If we initiated panic lockdown, we must disable preemption before
834 		 * enabling interrupts. While unlikely, preempting the panicked thread
835 		 * after lockdown has occurred may hang the system if all cores end up
836 		 * blocked while attempting to return to user space.
837 		 */
838 		disable_preemption();
839 	}
840 #endif /* CONFIG_SPTM */
841 
842 	/* Inherit the interrupt masks from previous context */
843 	if (SPSR_INTERRUPTS_ENABLED(get_saved_state_cpsr(state))) {
844 		ml_set_interrupts_enabled(TRUE);
845 	}
846 
847 	switch (class) {
848 	case ESR_EC_SVC_64:
849 		if (!is_saved_state64(state) || !is_user) {
850 			panic("Invalid SVC_64 context");
851 		}
852 
853 		handle_svc(state);
854 		break;
855 
856 	case ESR_EC_DABORT_EL0:
857 		handle_abort(state, esr, far, inspect_data_abort, handle_user_abort, expected_fault_handler);
858 		break;
859 
860 	case ESR_EC_MSR_TRAP:
861 		handle_msr_trap(state, esr);
862 		break;
863 /**
864  * Some APPLEVIRTUALPLATFORM targets do not specify armv8.6, but it's still possible for
865  * them to be hosted by a host that implements ARM_FPAC. There's no way for such a host
866  * to disable it or trap it without substantial performance penalty. Therefore, the FPAC
867  * handler here needs to be built into the guest kernels to prevent the exception to fall
868  * through.
869  */
870 #if __has_feature(ptrauth_calls)
871 	case ESR_EC_PAC_FAIL:
872 #ifdef CONFIG_XNUPOST
873 		if (expected_fault_handler != NULL && expected_fault_handler(state)) {
874 			break;
875 		}
876 #endif /* CONFIG_XNUPOST */
877 		handle_pac_fail(state, esr);
878 		__builtin_unreachable();
879 
880 #endif /* __has_feature(ptrauth_calls) */
881 
882 #if HAS_ARM_FEAT_SME
883 	case ESR_EC_SME:
884 		handle_sme_trap(state, esr);
885 		break;
886 #endif /* HAS_ARM_FEAT_SME */
887 
888 	case ESR_EC_IABORT_EL0:
889 		handle_abort(state, esr, far, inspect_instruction_abort, handle_user_abort, expected_fault_handler);
890 		break;
891 
892 	case ESR_EC_IABORT_EL1:
893 #ifdef CONFIG_XNUPOST
894 		if ((expected_fault_handler != NULL) && expected_fault_handler(state)) {
895 			break;
896 		}
897 #endif /* CONFIG_XNUPOST */
898 
899 		panic_with_thread_kernel_state("Kernel instruction fetch abort", state);
900 
901 	case ESR_EC_PC_ALIGN:
902 		handle_pc_align(state);
903 		__builtin_unreachable();
904 
905 	case ESR_EC_DABORT_EL1:
906 		handle_abort(state, esr, far, inspect_data_abort, handle_kernel_abort, expected_fault_handler);
907 		break;
908 
909 	case ESR_EC_UNCATEGORIZED:
910 		assert(!ESR_ISS(esr));
911 
912 #if CONFIG_XNUPOST
913 		if (!is_user && (expected_fault_handler != NULL) && expected_fault_handler(state)) {
914 			/*
915 			 * The fault handler accepted the exception and handled it on its
916 			 * own. Don't trap to the debugger/panic.
917 			 */
918 			break;
919 		}
920 #endif /* CONFIG_XNUPOST */
921 		handle_uncategorized(&context->ss);
922 		break;
923 
924 	case ESR_EC_SP_ALIGN:
925 		handle_sp_align(state);
926 		__builtin_unreachable();
927 
928 	case ESR_EC_BKPT_AARCH32:
929 		handle_breakpoint(state, esr);
930 		__builtin_unreachable();
931 
932 	case ESR_EC_BRK_AARCH64:
933 #ifdef CONFIG_XNUPOST
934 		if ((expected_fault_handler != NULL) && expected_fault_handler(state)) {
935 			break;
936 		}
937 #endif /* CONFIG_XNUPOST */
938 		if (PSR64_IS_KERNEL(get_saved_state_cpsr(state))) {
939 			handle_kernel_breakpoint(state, esr);
940 			break;
941 		} else {
942 			handle_breakpoint(state, esr);
943 			__builtin_unreachable();
944 		}
945 
946 	case ESR_EC_BKPT_REG_MATCH_EL0:
947 		if (FSC_DEBUG_FAULT == ISS_SSDE_FSC(esr)) {
948 			handle_breakpoint(state, esr);
949 		}
950 		panic("Unsupported Class %u event code. state=%p class=%u esr=%llu far=%p",
951 		    class, state, class, esr, (void *)far);
952 		__builtin_unreachable();
953 
954 	case ESR_EC_BKPT_REG_MATCH_EL1:
955 		panic_with_thread_kernel_state("Hardware Breakpoint Debug exception from kernel. Panic (by design)", state);
956 		__builtin_unreachable();
957 
958 	case ESR_EC_SW_STEP_DEBUG_EL0:
959 		if (FSC_DEBUG_FAULT == ISS_SSDE_FSC(esr)) {
960 			handle_sw_step_debug(state);
961 		}
962 		panic("Unsupported Class %u event code. state=%p class=%u esr=%llu far=%p",
963 		    class, state, class, esr, (void *)far);
964 		__builtin_unreachable();
965 
966 	case ESR_EC_SW_STEP_DEBUG_EL1:
967 		panic_with_thread_kernel_state("Software Step Debug exception from kernel. Panic (by design)", state);
968 		__builtin_unreachable();
969 
970 	case ESR_EC_WATCHPT_MATCH_EL0:
971 		if (FSC_DEBUG_FAULT == ISS_SSDE_FSC(esr)) {
972 			handle_watchpoint(far);
973 		}
974 		panic("Unsupported Class %u event code. state=%p class=%u esr=%llu far=%p",
975 		    class, state, class, esr, (void *)far);
976 		__builtin_unreachable();
977 
978 	case ESR_EC_WATCHPT_MATCH_EL1:
979 		/*
980 		 * If we hit a watchpoint in kernel mode, probably in a copyin/copyout which we don't want to
981 		 * abort.  Turn off watchpoints and keep going; we'll turn them back on in return_from_exception..
982 		 */
983 		if (FSC_DEBUG_FAULT == ISS_SSDE_FSC(esr)) {
984 			arm_debug_set(NULL);
985 			break; /* return to first level handler */
986 		}
987 		panic("Unsupported Class %u event code. state=%p class=%u esr=%llu far=%p",
988 		    class, state, class, esr, (void *)far);
989 		__builtin_unreachable();
990 
991 	case ESR_EC_TRAP_SIMD_FP:
992 		handle_simd_trap(state, esr);
993 		__builtin_unreachable();
994 
995 	case ESR_EC_ILLEGAL_INSTR_SET:
996 		panic("Illegal instruction set exception. state=%p class=%u esr=%llu far=%p spsr=0x%x",
997 		    state, class, esr, (void *)far, get_saved_state_cpsr(state));
998 		__builtin_unreachable();
999 
1000 	case ESR_EC_MCR_MRC_CP15_TRAP:
1001 	case ESR_EC_MCRR_MRRC_CP15_TRAP:
1002 	case ESR_EC_MCR_MRC_CP14_TRAP:
1003 	case ESR_EC_LDC_STC_CP14_TRAP:
1004 	case ESR_EC_MCRR_MRRC_CP14_TRAP:
1005 		handle_user_trapped_instruction32(state, esr);
1006 		__builtin_unreachable();
1007 
1008 	case ESR_EC_WFI_WFE:
1009 		// Use of WFI or WFE instruction when they have been disabled for EL0
1010 		handle_wf_trap(state);
1011 		__builtin_unreachable();
1012 
1013 	case ESR_EC_FLOATING_POINT_64:
1014 		handle_fp_trap(state, esr);
1015 		__builtin_unreachable();
1016 	case ESR_EC_BTI_FAIL:
1017 #ifdef CONFIG_XNUPOST
1018 		if ((expected_fault_handler != NULL) && expected_fault_handler(state)) {
1019 			break;
1020 		}
1021 #endif /* CONFIG_XNUPOST */
1022 #ifdef CONFIG_BTI_TELEMETRY
1023 		if (bti_telemetry_handle_exception(state)) {
1024 			/* Telemetry has accepted and corrected the exception, continue */
1025 			break;
1026 		}
1027 #endif /* CONFIG_BTI_TELEMETRY */
1028 		handle_bti_fail(state, esr);
1029 		__builtin_unreachable();
1030 
1031 	default:
1032 		handle_uncategorized(state);
1033 	}
1034 
1035 #ifdef CONFIG_XNUPOST
1036 	if (saved_expected_fault_handler != NULL) {
1037 		thread->machine.expected_fault_handler = saved_expected_fault_handler;
1038 		thread->machine.expected_fault_addr = saved_expected_fault_addr;
1039 		thread->machine.expected_fault_pc = saved_expected_fault_pc;
1040 	}
1041 #endif /* CONFIG_XNUPOST */
1042 
1043 	if (is_user) {
1044 		KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
1045 		    MACHDBG_CODE(DBG_MACH_EXCP_SYNC_ARM, thread->machine.exception_trace_code) | DBG_FUNC_END,
1046 		    esr, far, get_saved_state_pc(state), 0, 0);
1047 		thread->machine.exception_trace_code = 0;
1048 	} else {
1049 		KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
1050 		    MACHDBG_CODE(DBG_MACH_EXCP_SYNC_ARM, ARM64_KDBG_CODE_KERNEL | class) | DBG_FUNC_END,
1051 		    esr, VM_KERNEL_ADDRHIDE(far), VM_KERNEL_UNSLIDE(get_saved_state_pc(state)), 0, 0);
1052 	}
1053 
1054 #if CONFIG_SPTM
1055 	if (__improbable(did_initiate_panic_lockdown)) {
1056 #if CONFIG_XNUPOST
1057 		bool can_recover = !!(expected_fault_handler);
1058 #else
1059 		bool can_recover = false;
1060 #endif /* CONFIG_XNU_POST */
1061 
1062 		if (can_recover) {
1063 			/*
1064 			 * If we matched an exception handler, this was a simulated lockdown
1065 			 * and so we can recover. Re-enable preemption if we disabled it.
1066 			 */
1067 			if (current_thread() != NULL) {
1068 				enable_preemption();
1069 			}
1070 		} else {
1071 			/*
1072 			 * fleh already triggered a lockdown but we, for whatever reason,
1073 			 * didn't end up finding a reason to panic. Catch all panic in this
1074 			 * case.
1075 			 * Note that the panic here has no security benefit as the system is
1076 			 * already hosed, this is merely for telemetry.
1077 			 */
1078 			panic_with_thread_kernel_state("Panic lockdown initiated", state);
1079 		}
1080 	}
1081 #endif /* CONFIG_SPTM */
1082 
1083 #if MACH_ASSERT
1084 	if (preemption_level != sleh_get_preemption_level()) {
1085 		panic("synchronous exception changed preemption level from %d to %d", preemption_level, sleh_get_preemption_level());
1086 	}
1087 #endif
1088 }
1089 
1090 /*
1091  * Uncategorized exceptions are a catch-all for general execution errors.
1092  * ARM64_TODO: For now, we assume this is for undefined instruction exceptions.
1093  */
1094 static void
handle_uncategorized(arm_saved_state_t * state)1095 handle_uncategorized(arm_saved_state_t *state)
1096 {
1097 	exception_type_t           exception = EXC_BAD_INSTRUCTION;
1098 	mach_exception_data_type_t codes[2]  = {EXC_ARM_UNDEFINED};
1099 	mach_msg_type_number_t     numcodes  = 2;
1100 	uint32_t                   instr     = 0;
1101 
1102 	COPYIN(get_saved_state_pc(state), (char *)&instr, sizeof(instr));
1103 
1104 #if CONFIG_DTRACE
1105 
1106 	if (PSR64_IS_USER64(get_saved_state_cpsr(state))) {
1107 		/*
1108 		 * For a 64bit user process, we care about all 4 bytes of the
1109 		 * instr.
1110 		 */
1111 		if (instr == FASTTRAP_ARM64_INSTR || instr == FASTTRAP_ARM64_RET_INSTR) {
1112 			if (dtrace_user_probe(state) == KERN_SUCCESS) {
1113 				return;
1114 			}
1115 		}
1116 	} else if (PSR64_IS_USER32(get_saved_state_cpsr(state))) {
1117 		/*
1118 		 * For a 32bit user process, we check for thumb mode, in
1119 		 * which case we only care about a 2 byte instruction length.
1120 		 * For non-thumb mode, we care about all 4 bytes of the instructin.
1121 		 */
1122 		if (get_saved_state_cpsr(state) & PSR64_MODE_USER32_THUMB) {
1123 			if (((uint16_t)instr == FASTTRAP_THUMB32_INSTR) ||
1124 			    ((uint16_t)instr == FASTTRAP_THUMB32_RET_INSTR)) {
1125 				if (dtrace_user_probe(state) == KERN_SUCCESS) {
1126 					return;
1127 				}
1128 			}
1129 		} else {
1130 			if ((instr == FASTTRAP_ARM32_INSTR) ||
1131 			    (instr == FASTTRAP_ARM32_RET_INSTR)) {
1132 				if (dtrace_user_probe(state) == KERN_SUCCESS) {
1133 					return;
1134 				}
1135 			}
1136 		}
1137 	}
1138 
1139 #endif /* CONFIG_DTRACE */
1140 
1141 	if (PSR64_IS_KERNEL(get_saved_state_cpsr(state))) {
1142 		if (IS_ARM_GDB_TRAP(instr)) {
1143 			boolean_t interrupt_state;
1144 			exception = EXC_BREAKPOINT;
1145 
1146 			interrupt_state = ml_set_interrupts_enabled(FALSE);
1147 
1148 			/* Save off the context here (so that the debug logic
1149 			 * can see the original state of this thread).
1150 			 */
1151 			current_thread()->machine.kpcb = state;
1152 
1153 			/* Hop into the debugger (typically either due to a
1154 			 * fatal exception, an explicit panic, or a stackshot
1155 			 * request.
1156 			 */
1157 			DebuggerCall(exception, state);
1158 
1159 			current_thread()->machine.kpcb = NULL;
1160 			(void) ml_set_interrupts_enabled(interrupt_state);
1161 			return;
1162 		} else {
1163 			panic("Undefined kernel instruction: pc=%p instr=%x", (void*)get_saved_state_pc(state), instr);
1164 		}
1165 	}
1166 
1167 	/*
1168 	 * Check for GDB breakpoint via illegal opcode.
1169 	 */
1170 	if (IS_ARM_GDB_TRAP(instr)) {
1171 		exception = EXC_BREAKPOINT;
1172 		codes[0] = EXC_ARM_BREAKPOINT;
1173 		codes[1] = instr;
1174 	} else {
1175 		codes[1] = instr;
1176 	}
1177 
1178 	exception_triage(exception, codes, numcodes);
1179 	__builtin_unreachable();
1180 }
1181 
1182 #if __has_feature(ptrauth_calls)
1183 static const uint16_t PTRAUTH_TRAP_START = 0xC470;
1184 static inline bool
brk_comment_is_ptrauth(uint16_t comment)1185 brk_comment_is_ptrauth(uint16_t comment)
1186 {
1187 	return comment >= PTRAUTH_TRAP_START &&
1188 	       comment <= PTRAUTH_TRAP_START + ptrauth_key_asdb;
1189 }
1190 
1191 static inline const char *
ptrauth_key_to_string(ptrauth_key key)1192 ptrauth_key_to_string(ptrauth_key key)
1193 {
1194 	switch (key) {
1195 	case ptrauth_key_asia:
1196 		return "IA";
1197 	case ptrauth_key_asib:
1198 		return "IB";
1199 	case ptrauth_key_asda:
1200 		return "DA";
1201 	case ptrauth_key_asdb:
1202 		return "DB";
1203 	default:
1204 		__builtin_unreachable();
1205 	}
1206 }
1207 
1208 static const char *
ptrauth_handle_brk_trap(void * tstate,uint16_t comment)1209 ptrauth_handle_brk_trap(void *tstate, uint16_t comment)
1210 {
1211 	kernel_panic_reason_t pr = PERCPU_GET(panic_reason);
1212 	arm_saved_state_t *state = (arm_saved_state_t *)tstate;
1213 
1214 	ptrauth_key key = (ptrauth_key)(comment - PTRAUTH_TRAP_START);
1215 	const char *key_str = ptrauth_key_to_string(key);
1216 
1217 	snprintf(pr->buf, sizeof(pr->buf),
1218 	    "Break 0x%04X instruction exception from kernel. "
1219 	    "Ptrauth failure with %s key resulted in 0x%016llx",
1220 	    comment, key_str, saved_state64(state)->x[16]);
1221 
1222 	return pr->buf;
1223 }
1224 #endif /* __has_feature(ptrauth_calls) */
1225 
1226 #if HAS_TELEMETRY_KERNEL_BRK
1227 static uint32_t bound_chk_violations_event;
1228 
1229 static const char *
xnu_soft_trap_handle_breakpoint(void * tstate,uint16_t comment)1230 xnu_soft_trap_handle_breakpoint(
1231 	void              *tstate,
1232 	uint16_t          comment)
1233 {
1234 #if CONFIG_UBSAN_MINIMAL
1235 	if (comment == UBSAN_SOFT_TRAP_SIGNED_OF) {
1236 		ubsan_handle_brk_trap(tstate, comment);
1237 	}
1238 #else
1239 	(void)tstate;
1240 #endif
1241 
1242 	if (comment == CLANG_SOFT_TRAP_BOUND_CHK) {
1243 		os_atomic_inc(&bound_chk_violations_event, relaxed);
1244 	}
1245 	return NULL;
1246 }
1247 #endif /* HAS_TELEMETRY_KERNEL_BRK */
1248 
1249 static const char *
xnu_hard_trap_handle_breakpoint(void * tstate,uint16_t comment)1250 xnu_hard_trap_handle_breakpoint(void *tstate, uint16_t comment)
1251 {
1252 	kernel_panic_reason_t pr = PERCPU_GET(panic_reason);
1253 	arm_saved_state64_t *state = saved_state64(tstate);
1254 
1255 	switch (comment) {
1256 	case XNU_HARD_TRAP_SAFE_UNLINK:
1257 		snprintf(pr->buf, sizeof(pr->buf),
1258 		    "panic: corrupt list around element %p",
1259 		    (void *)state->x[8]);
1260 		return pr->buf;
1261 
1262 	case XNU_HARD_TRAP_STRING_CHK:
1263 		return "panic: string operation caused an overflow";
1264 
1265 	case XNU_HARD_TRAP_ASSERT_FAILURE:
1266 		/*
1267 		 * Read the implicit assert arguments, see:
1268 		 * ML_TRAP_REGISTER_1: x8
1269 		 * ML_TRAP_REGISTER_2: x16
1270 		 * ML_TRAP_REGISTER_3: x17
1271 		 */
1272 		panic_assert_format(pr->buf, sizeof(pr->buf),
1273 		    (struct mach_assert_hdr *)state->x[8],
1274 		    state->x[16], state->x[17]);
1275 		return pr->buf;
1276 
1277 	default:
1278 		return NULL;
1279 	}
1280 }
1281 
1282 #if __has_feature(ptrauth_calls)
1283 KERNEL_BRK_DESCRIPTOR_DEFINE(ptrauth_desc,
1284     .type                = TRAP_TELEMETRY_TYPE_KERNEL_BRK_PTRAUTH,
1285     .base                = PTRAUTH_TRAP_START,
1286     .max                 = PTRAUTH_TRAP_START + ptrauth_key_asdb,
1287     .options             = BRK_TELEMETRY_OPTIONS_FATAL_DEFAULT,
1288     .handle_breakpoint   = ptrauth_handle_brk_trap);
1289 #endif
1290 
1291 KERNEL_BRK_DESCRIPTOR_DEFINE(clang_desc,
1292     .type                = TRAP_TELEMETRY_TYPE_KERNEL_BRK_CLANG,
1293     .base                = CLANG_ARM_TRAP_START,
1294     .max                 = CLANG_ARM_TRAP_END,
1295     .options             = BRK_TELEMETRY_OPTIONS_FATAL_DEFAULT,
1296     .handle_breakpoint   = NULL);
1297 
1298 KERNEL_BRK_DESCRIPTOR_DEFINE(libcxx_desc,
1299     .type                = TRAP_TELEMETRY_TYPE_KERNEL_BRK_LIBCXX,
1300     .base                = LIBCXX_TRAP_START,
1301     .max                 = LIBCXX_TRAP_END,
1302     .options             = BRK_TELEMETRY_OPTIONS_FATAL_DEFAULT,
1303     .handle_breakpoint   = NULL);
1304 
1305 #if HAS_TELEMETRY_KERNEL_BRK
1306 KERNEL_BRK_DESCRIPTOR_DEFINE(xnu_soft_traps_desc,
1307     .type                = TRAP_TELEMETRY_TYPE_KERNEL_BRK_TELEMETRY,
1308     .base                = XNU_SOFT_TRAP_START,
1309     .max                 = XNU_SOFT_TRAP_END,
1310     .options             = BRK_TELEMETRY_OPTIONS_RECOVERABLE_DEFAULT(
1311 	    /* enable_telemetry */ true),
1312     .handle_breakpoint   = xnu_soft_trap_handle_breakpoint);
1313 #endif /* HAS_TELEMETRY_KERNEL_BRK */
1314 
1315 KERNEL_BRK_DESCRIPTOR_DEFINE(xnu_hard_traps_desc,
1316     .type                = TRAP_TELEMETRY_TYPE_KERNEL_BRK_XNU,
1317     .base                = XNU_HARD_TRAP_START,
1318     .max                 = XNU_HARD_TRAP_END,
1319     .options             = BRK_TELEMETRY_OPTIONS_FATAL_DEFAULT,
1320     .handle_breakpoint   = xnu_hard_trap_handle_breakpoint);
1321 
1322 static void
1323 #if !HAS_TELEMETRY_KERNEL_BRK
1324 __attribute__((noreturn))
1325 #endif
handle_kernel_breakpoint(arm_saved_state_t * state,uint64_t esr)1326 handle_kernel_breakpoint(arm_saved_state_t *state, uint64_t esr)
1327 {
1328 	uint16_t comment = ISS_BRK_COMMENT(esr);
1329 	const struct kernel_brk_descriptor *desc;
1330 	const char *msg = NULL;
1331 
1332 	desc = find_brk_descriptor_by_comment(comment);
1333 
1334 	if (!desc) {
1335 		goto brk_out;
1336 	}
1337 
1338 #if HAS_TELEMETRY_KERNEL_BRK
1339 	if (desc->options.enable_trap_telemetry) {
1340 		trap_telemetry_report_exception(
1341 			/* trap_type   */ desc->type,
1342 			/* trap_code   */ comment,
1343 			/* options     */ desc->options.telemetry_options,
1344 			/* saved_state */ (void *)state);
1345 	}
1346 #endif
1347 
1348 	if (desc->handle_breakpoint) {
1349 		msg = desc->handle_breakpoint(state, comment);
1350 	}
1351 
1352 #if HAS_TELEMETRY_KERNEL_BRK
1353 	/* Still alive? Check if we should recover. */
1354 	if (desc->options.recoverable) {
1355 		add_saved_state_pc(state, 4);
1356 		return;
1357 	}
1358 #endif
1359 
1360 brk_out:
1361 	if (msg == NULL) {
1362 		kernel_panic_reason_t pr = PERCPU_GET(panic_reason);
1363 
1364 		msg = tsnprintf(pr->buf, sizeof(pr->buf),
1365 		    "Break 0x%04X instruction exception from kernel. "
1366 		    "Panic (by design)",
1367 		    comment);
1368 	}
1369 
1370 	panic_with_thread_kernel_state(msg, state);
1371 	__builtin_unreachable();
1372 #undef MSG_FMT
1373 }
1374 
1375 static void
handle_breakpoint(arm_saved_state_t * state,uint64_t esr __unused)1376 handle_breakpoint(arm_saved_state_t *state, uint64_t esr __unused)
1377 {
1378 	exception_type_t           exception = EXC_BREAKPOINT;
1379 	mach_exception_data_type_t codes[2]  = {EXC_ARM_BREAKPOINT};
1380 	mach_msg_type_number_t     numcodes  = 2;
1381 
1382 #if __has_feature(ptrauth_calls)
1383 	if (ESR_EC(esr) == ESR_EC_BRK_AARCH64 &&
1384 	    brk_comment_is_ptrauth(ISS_BRK_COMMENT(esr))) {
1385 		exception |= EXC_PTRAUTH_BIT;
1386 	}
1387 #endif /* __has_feature(ptrauth_calls) */
1388 
1389 	codes[1] = get_saved_state_pc(state);
1390 	exception_triage(exception, codes, numcodes);
1391 	__builtin_unreachable();
1392 }
1393 
1394 static void
handle_watchpoint(vm_offset_t fault_addr)1395 handle_watchpoint(vm_offset_t fault_addr)
1396 {
1397 	exception_type_t           exception = EXC_BREAKPOINT;
1398 	mach_exception_data_type_t codes[2]  = {EXC_ARM_DA_DEBUG};
1399 	mach_msg_type_number_t     numcodes  = 2;
1400 
1401 	codes[1] = fault_addr;
1402 	exception_triage(exception, codes, numcodes);
1403 	__builtin_unreachable();
1404 }
1405 
1406 static void
handle_abort(arm_saved_state_t * state,uint64_t esr,vm_offset_t fault_addr,abort_inspector_t inspect_abort,abort_handler_t handler,expected_fault_handler_t expected_fault_handler)1407 handle_abort(arm_saved_state_t *state, uint64_t esr, vm_offset_t fault_addr,
1408     abort_inspector_t inspect_abort, abort_handler_t handler, expected_fault_handler_t expected_fault_handler)
1409 {
1410 	fault_status_t fault_code;
1411 	vm_prot_t      fault_type;
1412 
1413 	inspect_abort(ESR_ISS(esr), &fault_code, &fault_type);
1414 	handler(state, esr, fault_addr, fault_code, fault_type, expected_fault_handler);
1415 }
1416 
1417 static void
inspect_instruction_abort(uint32_t iss,fault_status_t * fault_code,vm_prot_t * fault_type)1418 inspect_instruction_abort(uint32_t iss, fault_status_t *fault_code, vm_prot_t *fault_type)
1419 {
1420 	getCpuDatap()->cpu_stat.instr_ex_cnt++;
1421 	*fault_code = ISS_IA_FSC(iss);
1422 	*fault_type = (VM_PROT_READ | VM_PROT_EXECUTE);
1423 }
1424 
1425 static void
inspect_data_abort(uint32_t iss,fault_status_t * fault_code,vm_prot_t * fault_type)1426 inspect_data_abort(uint32_t iss, fault_status_t *fault_code, vm_prot_t *fault_type)
1427 {
1428 	getCpuDatap()->cpu_stat.data_ex_cnt++;
1429 	*fault_code = ISS_DA_FSC(iss);
1430 
1431 	/*
1432 	 * Cache maintenance operations always report faults as write access.
1433 	 * Change these to read access, unless they report a permission fault.
1434 	 * Only certain cache maintenance operations (e.g. 'dc ivac') require write
1435 	 * access to the mapping, but if a cache maintenance operation that only requires
1436 	 * read access generates a permission fault, then we will not be able to handle
1437 	 * the fault regardless of whether we treat it as a read or write fault.
1438 	 */
1439 	if ((iss & ISS_DA_WNR) && (!(iss & ISS_DA_CM) || is_permission_fault(*fault_code))) {
1440 		*fault_type = (VM_PROT_READ | VM_PROT_WRITE);
1441 	} else {
1442 		*fault_type = (VM_PROT_READ);
1443 	}
1444 }
1445 
1446 #if __has_feature(ptrauth_calls)
1447 static inline uint64_t
fault_addr_bitmask(unsigned int bit_from,unsigned int bit_to)1448 fault_addr_bitmask(unsigned int bit_from, unsigned int bit_to)
1449 {
1450 	return ((1ULL << (bit_to - bit_from + 1)) - 1) << bit_from;
1451 }
1452 
1453 static inline bool
fault_addr_bit(vm_offset_t fault_addr,unsigned int bit)1454 fault_addr_bit(vm_offset_t fault_addr, unsigned int bit)
1455 {
1456 	return (bool)((fault_addr >> bit) & 1);
1457 }
1458 
1459 extern int gARM_FEAT_FPAC;
1460 extern int gARM_FEAT_FPACCOMBINE;
1461 extern int gARM_FEAT_PAuth2;
1462 
1463 /**
1464  * Determines whether a fault address taken at EL0 contains a PAC error code
1465  * corresponding to the specified kind of ptrauth key.
1466  */
1467 static bool
user_fault_matches_pac_error_code(vm_offset_t fault_addr,uint64_t pc,bool data_key)1468 user_fault_matches_pac_error_code(vm_offset_t fault_addr, uint64_t pc, bool data_key)
1469 {
1470 	if (gARM_FEAT_FPACCOMBINE) {
1471 		/*
1472 		 * CPUs with FPACCOMBINE always raise PAC Fail exceptions during
1473 		 * PAC failure.  If the CPU took any other kind of exception, we
1474 		 * can rule out PAC as the root cause.
1475 		 */
1476 		return false;
1477 	}
1478 
1479 	if (data_key && gARM_FEAT_FPAC) {
1480 		uint32_t instr;
1481 		int err = copyin(pc, (char *)&instr, sizeof(instr));
1482 		if (!err && !ARM64_INSTR_IS_LDRAx(instr)) {
1483 			/*
1484 			 * On FPAC-enabled devices, PAC failure can only cause
1485 			 * data aborts during "combined" LDRAx instructions.  If
1486 			 * PAC fails during a discrete AUTxx + LDR/STR
1487 			 * instruction sequence, then the AUTxx instruction
1488 			 * raises a PAC Fail exception rather than poisoning its
1489 			 * output address.
1490 			 *
1491 			 * In principle the same logic applies to instruction
1492 			 * aborts.  But we have no way to identify the exact
1493 			 * instruction that caused the abort, so we can't tell
1494 			 * if it was a combined branch + auth instruction.
1495 			 */
1496 			return false;
1497 		}
1498 	}
1499 
1500 	bool instruction_tbi = !(get_tcr() & TCR_TBID0_TBI_DATA_ONLY);
1501 	bool tbi = data_key || __improbable(instruction_tbi);
1502 
1503 	if (gARM_FEAT_PAuth2) {
1504 		/*
1505 		 * EnhancedPAC2 CPUs don't encode error codes at fixed positions, so
1506 		 * treat all non-canonical address bits like potential poison bits.
1507 		 */
1508 		uint64_t mask = fault_addr_bitmask(64 - T0SZ_BOOT, 54);
1509 		if (!tbi) {
1510 			mask |= fault_addr_bitmask(56, 63);
1511 		}
1512 		return (fault_addr & mask) != 0;
1513 	} else {
1514 		unsigned int poison_shift;
1515 		if (tbi) {
1516 			poison_shift = 53;
1517 		} else {
1518 			poison_shift = 61;
1519 		}
1520 
1521 		/* PAC error codes are always in the form key_number:NOT(key_number) */
1522 		bool poison_bit_1 = fault_addr_bit(fault_addr, poison_shift);
1523 		bool poison_bit_2 = fault_addr_bit(fault_addr, poison_shift + 1);
1524 		return poison_bit_1 != poison_bit_2;
1525 	}
1526 }
1527 #endif /* __has_feature(ptrauth_calls) */
1528 
1529 /**
1530  * Determines whether the userland thread has a JIT region in RW mode, TPRO
1531  * in RW mode, or JCTL_EL0 in pointer signing mode.  A fault in any of these trusted
1532  * code paths may indicate an attack on WebKit.  Rather than letting a
1533  * potentially-compromised process try to handle the exception, it will be killed
1534  * by the kernel and a crash report will be generated.
1535  */
1536 static bool
user_fault_in_self_restrict_mode(thread_t thread __unused)1537 user_fault_in_self_restrict_mode(thread_t thread __unused)
1538 {
1539 
1540 	return false;
1541 }
1542 
1543 static void
handle_pc_align(arm_saved_state_t * ss)1544 handle_pc_align(arm_saved_state_t *ss)
1545 {
1546 	exception_type_t exc;
1547 	mach_exception_data_type_t codes[2];
1548 	mach_msg_type_number_t numcodes = 2;
1549 
1550 	if (!PSR64_IS_USER(get_saved_state_cpsr(ss))) {
1551 		panic_with_thread_kernel_state("PC alignment exception from kernel.", ss);
1552 	}
1553 
1554 	exc = EXC_BAD_ACCESS;
1555 #if __has_feature(ptrauth_calls)
1556 	uint64_t pc = get_saved_state_pc(ss);
1557 	if (user_fault_matches_pac_error_code(pc, pc, false)) {
1558 		exc |= EXC_PTRAUTH_BIT;
1559 	}
1560 #endif /* __has_feature(ptrauth_calls) */
1561 
1562 	codes[0] = EXC_ARM_DA_ALIGN;
1563 	codes[1] = get_saved_state_pc(ss);
1564 
1565 	exception_triage(exc, codes, numcodes);
1566 	__builtin_unreachable();
1567 }
1568 
1569 static void
handle_sp_align(arm_saved_state_t * ss)1570 handle_sp_align(arm_saved_state_t *ss)
1571 {
1572 	exception_type_t exc;
1573 	mach_exception_data_type_t codes[2];
1574 	mach_msg_type_number_t numcodes = 2;
1575 
1576 	if (!PSR64_IS_USER(get_saved_state_cpsr(ss))) {
1577 		panic_with_thread_kernel_state("SP alignment exception from kernel.", ss);
1578 	}
1579 
1580 	exc = EXC_BAD_ACCESS;
1581 #if __has_feature(ptrauth_calls)
1582 	if (user_fault_matches_pac_error_code(get_saved_state_sp(ss), get_saved_state_pc(ss), true)) {
1583 		exc |= EXC_PTRAUTH_BIT;
1584 	}
1585 #endif /* __has_feature(ptrauth_calls) */
1586 
1587 	codes[0] = EXC_ARM_SP_ALIGN;
1588 	codes[1] = get_saved_state_sp(ss);
1589 
1590 	exception_triage(exc, codes, numcodes);
1591 	__builtin_unreachable();
1592 }
1593 
1594 static void
handle_wf_trap(arm_saved_state_t * state)1595 handle_wf_trap(arm_saved_state_t *state)
1596 {
1597 	exception_type_t exc;
1598 	mach_exception_data_type_t codes[2];
1599 	mach_msg_type_number_t numcodes = 2;
1600 	uint32_t instr = 0;
1601 
1602 	COPYIN(get_saved_state_pc(state), (char *)&instr, sizeof(instr));
1603 
1604 	exc = EXC_BAD_INSTRUCTION;
1605 	codes[0] = EXC_ARM_UNDEFINED;
1606 	codes[1] = instr;
1607 
1608 	exception_triage(exc, codes, numcodes);
1609 	__builtin_unreachable();
1610 }
1611 
1612 static void
handle_fp_trap(arm_saved_state_t * state,uint64_t esr)1613 handle_fp_trap(arm_saved_state_t *state, uint64_t esr)
1614 {
1615 	exception_type_t exc = EXC_ARITHMETIC;
1616 	mach_exception_data_type_t codes[2];
1617 	mach_msg_type_number_t numcodes = 2;
1618 	uint32_t instr = 0;
1619 
1620 	if (PSR64_IS_KERNEL(get_saved_state_cpsr(state))) {
1621 		panic_with_thread_kernel_state("Floating point exception from kernel", state);
1622 	}
1623 
1624 	COPYIN(get_saved_state_pc(state), (char *)&instr, sizeof(instr));
1625 	codes[1] = instr;
1626 
1627 	/* The floating point trap flags are only valid if TFV is set. */
1628 	if (!fp_exceptions_enabled) {
1629 		exc = EXC_BAD_INSTRUCTION;
1630 		codes[0] = EXC_ARM_UNDEFINED;
1631 	} else if (!(esr & ISS_FP_TFV)) {
1632 		codes[0] = EXC_ARM_FP_UNDEFINED;
1633 	} else if (esr & ISS_FP_UFF) {
1634 		codes[0] = EXC_ARM_FP_UF;
1635 	} else if (esr & ISS_FP_OFF) {
1636 		codes[0] = EXC_ARM_FP_OF;
1637 	} else if (esr & ISS_FP_IOF) {
1638 		codes[0] = EXC_ARM_FP_IO;
1639 	} else if (esr & ISS_FP_DZF) {
1640 		codes[0] = EXC_ARM_FP_DZ;
1641 	} else if (esr & ISS_FP_IDF) {
1642 		codes[0] = EXC_ARM_FP_ID;
1643 	} else if (esr & ISS_FP_IXF) {
1644 		codes[0] = EXC_ARM_FP_IX;
1645 	} else {
1646 		panic("Unrecognized floating point exception, state=%p, esr=%#llx", state, esr);
1647 	}
1648 
1649 	exception_triage(exc, codes, numcodes);
1650 	__builtin_unreachable();
1651 }
1652 
1653 
1654 
1655 /*
1656  * handle_alignment_fault_from_user:
1657  *   state: Saved state
1658  *
1659  * Attempts to deal with an alignment fault from userspace (possibly by
1660  * emulating the faulting instruction).  If emulation failed due to an
1661  * unservicable fault, the ESR for that fault will be stored in the
1662  * recovery_esr field of the thread by the exception code.
1663  *
1664  * Returns:
1665  *   -1:     Emulation failed (emulation of state/instr not supported)
1666  *   0:      Successfully emulated the instruction
1667  *   EFAULT: Emulation failed (probably due to permissions)
1668  *   EINVAL: Emulation failed (probably due to a bad address)
1669  */
1670 
1671 
1672 static int
handle_alignment_fault_from_user(arm_saved_state_t * state,kern_return_t * vmfr)1673 handle_alignment_fault_from_user(arm_saved_state_t *state, kern_return_t *vmfr)
1674 {
1675 	int ret = -1;
1676 
1677 #pragma unused (state)
1678 #pragma unused (vmfr)
1679 
1680 	return ret;
1681 }
1682 
1683 
1684 
1685 #if HAS_ARM_FEAT_SME
1686 static void
handle_sme_trap(arm_saved_state_t * state,uint64_t esr)1687 handle_sme_trap(arm_saved_state_t *state, uint64_t esr)
1688 {
1689 	exception_type_t exc = EXC_BAD_INSTRUCTION;
1690 	mach_exception_data_type_t codes[2] = {EXC_ARM_UNDEFINED};
1691 	mach_msg_type_number_t numcodes = 2;
1692 
1693 	if (!PSR64_IS_USER(get_saved_state_cpsr(state))) {
1694 		panic("SME exception from kernel, state=%p, esr=%#llx", state, esr);
1695 	}
1696 	if (!arm_sme_version()) {
1697 		/*
1698 		 * If SME is disabled in software but userspace executes an SME
1699 		 * instruction anyway, then the CPU will still raise an
1700 		 * SME-specific trap.  Triage it as if the CPU raised an
1701 		 * undefined-instruction trap.
1702 		 */
1703 		exception_triage(exc, codes, numcodes);
1704 		__builtin_unreachable();
1705 	}
1706 
1707 	if (ISS_SME_SMTC(ESR_ISS(esr)) == ISS_SME_SMTC_CAPCR) {
1708 		thread_t thread = current_thread();
1709 		switch (machine_thread_sme_state_alloc(thread)) {
1710 		case KERN_SUCCESS:
1711 			return;
1712 
1713 
1714 		default:
1715 			panic("Failed to allocate SME state for thread %p", thread);
1716 		}
1717 	}
1718 
1719 	uint32_t instr;
1720 	COPYIN(get_saved_state_pc(state), (char *)&instr, sizeof(instr));
1721 	codes[1] = instr;
1722 
1723 	exception_triage(exc, codes, numcodes);
1724 	__builtin_unreachable();
1725 }
1726 #endif /* HAS_ARM_FEAT_SME */
1727 
1728 static void
handle_sw_step_debug(arm_saved_state_t * state)1729 handle_sw_step_debug(arm_saved_state_t *state)
1730 {
1731 	thread_t thread = current_thread();
1732 	exception_type_t exc;
1733 	mach_exception_data_type_t codes[2];
1734 	mach_msg_type_number_t numcodes = 2;
1735 
1736 	if (!PSR64_IS_USER(get_saved_state_cpsr(state))) {
1737 		panic_with_thread_kernel_state("SW_STEP_DEBUG exception from kernel.", state);
1738 	}
1739 
1740 	// Disable single step and unmask interrupts (in the saved state, anticipating next exception return)
1741 	if (thread->machine.DebugData != NULL) {
1742 		thread->machine.DebugData->uds.ds64.mdscr_el1 &= ~0x1;
1743 	} else {
1744 		panic_with_thread_kernel_state("SW_STEP_DEBUG exception thread DebugData is NULL.", state);
1745 	}
1746 
1747 	mask_user_saved_state_cpsr(thread->machine.upcb, 0, PSR64_SS | DAIF_ALL);
1748 
1749 	// Special encoding for gdb single step event on ARM
1750 	exc = EXC_BREAKPOINT;
1751 	codes[0] = 1;
1752 	codes[1] = 0;
1753 
1754 	exception_triage(exc, codes, numcodes);
1755 	__builtin_unreachable();
1756 }
1757 
1758 #if MACH_ASSERT
1759 TUNABLE_WRITEABLE(int, panic_on_jit_guard, "panic_on_jit_guard", 0);
1760 #endif /* MACH_ASSERT */
1761 
1762 static void
handle_user_abort(arm_saved_state_t * state,uint64_t esr,vm_offset_t fault_addr,fault_status_t fault_code,vm_prot_t fault_type,expected_fault_handler_t expected_fault_handler)1763 handle_user_abort(arm_saved_state_t *state, uint64_t esr, vm_offset_t fault_addr,
1764     fault_status_t fault_code, vm_prot_t fault_type, expected_fault_handler_t expected_fault_handler)
1765 {
1766 	exception_type_t           exc      = EXC_BAD_ACCESS;
1767 	mach_exception_data_type_t codes[2];
1768 	mach_msg_type_number_t     numcodes = 2;
1769 	thread_t                   thread   = current_thread();
1770 
1771 	(void)expected_fault_handler;
1772 
1773 	if (__improbable(!SPSR_INTERRUPTS_ENABLED(get_saved_state_cpsr(state)))) {
1774 		panic_with_thread_kernel_state("User abort from non-interruptible context", state);
1775 	}
1776 
1777 	thread->iotier_override = THROTTLE_LEVEL_NONE; /* Reset IO tier override before handling abort from userspace */
1778 
1779 	if (!is_servicible_fault(fault_code, esr) &&
1780 	    thread->t_rr_state.trr_fault_state != TRR_FAULT_NONE) {
1781 		thread_reset_pcs_done_faulting(thread);
1782 	}
1783 
1784 	if (is_vm_fault(fault_code)) {
1785 		vm_map_t        map = thread->map;
1786 		vm_offset_t     vm_fault_addr = fault_addr;
1787 		kern_return_t   result = KERN_FAILURE;
1788 
1789 		assert(map != kernel_map);
1790 
1791 		if (!(fault_type & VM_PROT_EXECUTE)) {
1792 			vm_fault_addr = VM_USER_STRIP_TBI(fault_addr);
1793 		}
1794 
1795 		/* check to see if it is just a pmap ref/modify fault */
1796 		if (!is_translation_fault(fault_code)) {
1797 			result = arm_fast_fault(map->pmap,
1798 			    vm_fault_addr,
1799 			    fault_type, (fault_code == FSC_ACCESS_FLAG_FAULT_L3), TRUE);
1800 		}
1801 		if (result != KERN_SUCCESS) {
1802 
1803 			{
1804 				/* We have to fault the page in */
1805 				result = vm_fault(map, vm_fault_addr, fault_type,
1806 				    /* change_wiring */ FALSE, VM_KERN_MEMORY_NONE, THREAD_ABORTSAFE,
1807 				    /* caller_pmap */ NULL, /* caller_pmap_addr */ 0);
1808 			}
1809 		}
1810 		if (thread->t_rr_state.trr_fault_state != TRR_FAULT_NONE) {
1811 			thread_reset_pcs_done_faulting(thread);
1812 		}
1813 		if (result == KERN_SUCCESS || result == KERN_ABORTED) {
1814 			return;
1815 		}
1816 
1817 		/*
1818 		 * vm_fault() should never return KERN_FAILURE for page faults from user space.
1819 		 * If it does, we're leaking preemption disables somewhere in the kernel.
1820 		 */
1821 		if (__improbable(result == KERN_FAILURE)) {
1822 			panic("vm_fault() KERN_FAILURE from user fault on thread %p", thread);
1823 		}
1824 
1825 		codes[0] = result;
1826 	} else if (is_alignment_fault(fault_code)) {
1827 		kern_return_t vmfkr = KERN_SUCCESS;
1828 		thread->machine.recover_esr = 0;
1829 		thread->machine.recover_far = 0;
1830 		int result = handle_alignment_fault_from_user(state, &vmfkr);
1831 		if (result == 0) {
1832 			/* Successfully emulated, or instruction
1833 			 * copyin() for decode/emulation failed.
1834 			 * Continue, or redrive instruction.
1835 			 */
1836 			thread_exception_return();
1837 		} else if (((result == EFAULT) || (result == EINVAL)) &&
1838 		    (thread->machine.recover_esr == 0)) {
1839 			/*
1840 			 * If we didn't actually take a fault, but got one of
1841 			 * these errors, then we failed basic sanity checks of
1842 			 * the fault address.  Treat this as an invalid
1843 			 * address.
1844 			 */
1845 			codes[0] = KERN_INVALID_ADDRESS;
1846 		} else if ((result == EFAULT) &&
1847 		    (thread->machine.recover_esr)) {
1848 			/*
1849 			 * Since alignment aborts are prioritized
1850 			 * ahead of translation aborts, the misaligned
1851 			 * atomic emulation flow may have triggered a
1852 			 * VM pagefault, which the VM could not resolve.
1853 			 * Report the VM fault error in codes[]
1854 			 */
1855 
1856 			codes[0] = vmfkr;
1857 			assertf(vmfkr != KERN_SUCCESS, "Unexpected vmfkr 0x%x", vmfkr);
1858 			/* Cause ESR_EC to reflect an EL0 abort */
1859 			thread->machine.recover_esr &= ~ESR_EC_MASK;
1860 			thread->machine.recover_esr |= (ESR_EC_DABORT_EL0 << ESR_EC_SHIFT);
1861 			set_saved_state_esr(thread->machine.upcb, thread->machine.recover_esr);
1862 			set_saved_state_far(thread->machine.upcb, thread->machine.recover_far);
1863 			fault_addr = thread->machine.recover_far;
1864 		} else {
1865 			/* This was just an unsupported alignment
1866 			 * exception. Misaligned atomic emulation
1867 			 * timeouts fall in this category.
1868 			 */
1869 			codes[0] = EXC_ARM_DA_ALIGN;
1870 		}
1871 	} else if (is_parity_error(fault_code)) {
1872 #if defined(APPLE_ARM64_ARCH_FAMILY)
1873 		/*
1874 		 * Platform errors are handled in sleh_sync before interrupts are enabled.
1875 		 */
1876 #else
1877 		panic("User parity error.");
1878 #endif
1879 	} else {
1880 		codes[0] = KERN_FAILURE;
1881 	}
1882 
1883 #if CODE_SIGNING_MONITOR
1884 	/*
1885 	 * If the code reaches here, it means we weren't able to resolve the fault and we're
1886 	 * going to be sending the task an exception. On systems which have the code signing
1887 	 * monitor enabled, an execute fault which cannot be handled must result in sending
1888 	 * a SIGKILL to the task.
1889 	 */
1890 	if (is_vm_fault(fault_code) && (fault_type & VM_PROT_EXECUTE)) {
1891 		csm_code_signing_violation(current_proc(), fault_addr);
1892 	}
1893 #endif
1894 
1895 	codes[1] = fault_addr;
1896 #if __has_feature(ptrauth_calls)
1897 	bool is_data_abort = (ESR_EC(esr) == ESR_EC_DABORT_EL0);
1898 	if (user_fault_matches_pac_error_code(fault_addr, get_saved_state_pc(state), is_data_abort)) {
1899 		exc |= EXC_PTRAUTH_BIT;
1900 	}
1901 #endif /* __has_feature(ptrauth_calls) */
1902 
1903 	if (user_fault_in_self_restrict_mode(thread) &&
1904 	    task_is_jit_exception_fatal(get_threadtask(thread))) {
1905 		int flags = PX_KTRIAGE;
1906 		exception_info_t info = {
1907 			.os_reason = OS_REASON_SELF_RESTRICT,
1908 			.exception_type = exc,
1909 			.mx_code = codes[0],
1910 			.mx_subcode = codes[1]
1911 		};
1912 
1913 #if MACH_ASSERT
1914 		printf("\nGUARD_REASON_JIT exc %d codes=<0x%llx,0x%llx> syscalls %d task %p thread %p va 0x%lx code 0x%x type 0x%x esr 0x%llx\n",
1915 		    exc, codes[0], codes[1], thread->syscalls_unix, current_task(), thread, fault_addr, fault_code, fault_type, esr);
1916 		if (panic_on_jit_guard &&
1917 		    current_task()->thread_count == 1 &&
1918 		    thread->syscalls_unix < 24) {
1919 			panic("GUARD_REASON_JIT exc %d codes=<0x%llx,0x%llx> syscalls %d task %p thread %p va 0x%lx code 0x%x type 0x%x esr 0x%llx state %p j %d t %d s user 0x%llx (0x%llx) jb 0x%llx (0x%llx)",
1920 			    exc, codes[0], codes[1], thread->syscalls_unix, current_task(), thread, fault_addr, fault_code, fault_type, esr, state,
1921 			    0, 0, 0ull, 0ull,
1922 			    0ull, 0ull
1923 			    );
1924 		}
1925 #endif /* MACH_ASSERT */
1926 
1927 		exit_with_mach_exception(current_proc(), info, flags);
1928 	}
1929 
1930 
1931 	exception_triage(exc, codes, numcodes);
1932 	__builtin_unreachable();
1933 }
1934 
1935 /**
1936  * Panic because the kernel abort handler tried to apply a recovery handler that
1937  * isn't inside copyio_recover_table[].
1938  *
1939  * @param state original saved-state
1940  * @param recover invalid recovery handler
1941  */
1942 __attribute__((noreturn, used))
1943 static void
panic_on_invalid_recovery_handler(arm_saved_state_t * state,struct copyio_recovery_entry * recover)1944 panic_on_invalid_recovery_handler(arm_saved_state_t *state, struct copyio_recovery_entry *recover)
1945 {
1946 	panic("attempt to set invalid recovery handler %p on kernel saved-state %p", recover, state);
1947 }
1948 
1949 /**
1950  * Update a thread saved-state to store an error code in x0 and branch to a
1951  * copyio recovery handler.
1952  *
1953  * @param state original saved-state
1954  * @param esr ESR_ELx value for the fault taken
1955  * @param fault_addr FAR_ELx value for the fault taken
1956  * @param thread target thread
1957  * @param recover destination copyio recovery handler
1958  * @param x0 error code to populate into x0
1959  */
1960 static void
handle_kernel_abort_recover_with_error_code(arm_saved_state_t * state,uint64_t esr,vm_offset_t fault_addr,thread_t thread,struct copyio_recovery_entry * _Nonnull recover,uint64_t x0)1961 handle_kernel_abort_recover_with_error_code(
1962 	arm_saved_state_t              *state,
1963 	uint64_t                        esr,
1964 	vm_offset_t                     fault_addr,
1965 	thread_t                        thread,
1966 	struct copyio_recovery_entry   *_Nonnull recover,
1967 	uint64_t                       x0)
1968 {
1969 	thread->machine.recover_esr = esr;
1970 	thread->machine.recover_far = fault_addr;
1971 	saved_state64(state)->x[0] = x0;
1972 #if defined(HAS_APPLE_PAC)
1973 	MANIPULATE_SIGNED_THREAD_STATE(state,
1974 	    "adrp	x6, _copyio_recover_table_end@page		\n"
1975 	    "add	x6, x6, _copyio_recover_table_end@pageoff	\n"
1976 	    "cmp	%[recover], x6					\n"
1977 	    "b.lt	1f						\n"
1978 	    "bl		_panic_on_invalid_recovery_handler		\n"
1979 	    "brk	#0						\n"
1980 	    "1:								\n"
1981 	    "adrp	x6, _copyio_recover_table@page			\n"
1982 	    "add	x6, x6, _copyio_recover_table@pageoff		\n"
1983 	    "subs	x7, %[recover], x6				\n"
1984 	    "b.pl	1f						\n"
1985 	    "bl		_panic_on_invalid_recovery_handler		\n"
1986 	    "brk	#0						\n"
1987 	    "1:								\n"
1988 	    "udiv	x8, x7, %[SIZEOF_RECOVER]			\n"
1989 	    "mul	x8, x8, %[SIZEOF_RECOVER]			\n"
1990 	    "cmp	x7, x8						\n"
1991 	    "b.eq	1f						\n"
1992 	    "bl		_panic_on_invalid_recovery_handler		\n"
1993 	    "brk	#0						\n"
1994 	    "1:								\n"
1995 	    "ldr	x1, [%[recover], %[CRE_RECOVERY]]		\n"
1996 	    "add	x1, x1, x6					\n"
1997 	    "str	x1, [x0, %[SS64_PC]]				\n",
1998 	    [recover] "r"(recover),
1999 	    [SIZEOF_RECOVER] "r"((sizeof(*recover))),
2000 	    [CRE_RECOVERY] "i"(offsetof(struct copyio_recovery_entry, cre_recovery))
2001 	    );
2002 #else
2003 	ptrdiff_t recover_offset = (uintptr_t)recover - (uintptr_t)copyio_recover_table;
2004 	if ((uintptr_t)recover < (uintptr_t)copyio_recover_table ||
2005 	    (uintptr_t)recover >= (uintptr_t)copyio_recover_table_end ||
2006 	    (recover_offset % sizeof(*recover)) != 0) {
2007 		panic_on_invalid_recovery_handler(state, recover);
2008 	}
2009 	saved_state64(state)->pc = copyio_recovery_addr(recover->cre_recovery);
2010 #endif
2011 }
2012 
2013 static inline void
handle_kernel_abort_recover(arm_saved_state_t * state,uint64_t esr,vm_offset_t fault_addr,thread_t thread,struct copyio_recovery_entry * _Nonnull recover)2014 handle_kernel_abort_recover(
2015 	arm_saved_state_t              *state,
2016 	uint64_t                        esr,
2017 	vm_offset_t                     fault_addr,
2018 	thread_t                        thread,
2019 	struct copyio_recovery_entry   *_Nonnull recover)
2020 {
2021 	handle_kernel_abort_recover_with_error_code(state, esr, fault_addr, thread, recover, EFAULT);
2022 }
2023 
2024 
2025 static void
handle_kernel_abort(arm_saved_state_t * state,uint64_t esr,vm_offset_t fault_addr,fault_status_t fault_code,vm_prot_t fault_type,expected_fault_handler_t expected_fault_handler)2026 handle_kernel_abort(arm_saved_state_t *state, uint64_t esr, vm_offset_t fault_addr,
2027     fault_status_t fault_code, vm_prot_t fault_type, expected_fault_handler_t expected_fault_handler)
2028 {
2029 	thread_t thread = current_thread();
2030 	struct copyio_recovery_entry *recover = find_copyio_recovery_entry(
2031 		get_saved_state_pc(state));
2032 
2033 #ifndef CONFIG_XNUPOST
2034 	(void)expected_fault_handler;
2035 #endif /* CONFIG_XNUPOST */
2036 
2037 #if CONFIG_DTRACE
2038 	if (is_vm_fault(fault_code) && thread->t_dtrace_inprobe) { /* Executing under dtrace_probe? */
2039 		if (dtrace_tally_fault(fault_addr)) { /* Should a fault under dtrace be ignored? */
2040 			/*
2041 			 * Point to next instruction, or recovery handler if set.
2042 			 */
2043 			if (recover) {
2044 				handle_kernel_abort_recover(state, esr, VM_USER_STRIP_PTR(fault_addr), thread, recover);
2045 			} else {
2046 				add_saved_state_pc(state, 4);
2047 			}
2048 			return;
2049 		} else {
2050 			panic_with_thread_kernel_state("Unexpected page fault under dtrace_probe", state);
2051 		}
2052 	}
2053 #endif
2054 
2055 	if (is_vm_fault(fault_code)) {
2056 		kern_return_t result = KERN_FAILURE;
2057 		vm_map_t      map;
2058 		int           interruptible;
2059 
2060 #ifdef CONFIG_XNUPOST
2061 		if (expected_fault_handler && expected_fault_handler(state)) {
2062 			return;
2063 		}
2064 #endif /* CONFIG_XNUPOST */
2065 
2066 		if (VM_KERNEL_ADDRESS(fault_addr) || thread == THREAD_NULL || recover == 0) {
2067 			/*
2068 			 * If no recovery handler is supplied, always drive the fault against
2069 			 * the kernel map.  If the fault was taken against a userspace VA, indicating
2070 			 * an unprotected access to user address space, vm_fault() should fail and
2071 			 * ultimately lead to a panic here.
2072 			 */
2073 			map = kernel_map;
2074 			interruptible = THREAD_UNINT;
2075 
2076 #if CONFIG_KERNEL_TAGGING
2077 			/*
2078 			 * If kernel tagging is enabled, canonicalize the address here, so that we have a
2079 			 * chance to find it in the VM ranges. Do not mess with exec fault cases.
2080 			 */
2081 			if (!((fault_type) & VM_PROT_EXECUTE)) {
2082 				fault_addr = vm_memtag_canonicalize(map, fault_addr);
2083 			}
2084 #endif /* CONFIG_KERNEL_TAGGING */
2085 		} else {
2086 			map = thread->map;
2087 
2088 			/**
2089 			 * In the case that the recovery handler is set (e.g., during copyio
2090 			 * and dtrace probes), we don't want the vm_fault() operation to be
2091 			 * aborted early. Those code paths can't handle restarting the
2092 			 * vm_fault() operation so don't allow it to return early without
2093 			 * creating the wanted mapping.
2094 			 */
2095 			interruptible = (recover) ? THREAD_UNINT : THREAD_ABORTSAFE;
2096 
2097 		}
2098 
2099 		/*
2100 		 * Ensure no faults in the physical aperture. This could happen if
2101 		 * a page table is incorrectly allocated from the read only region
2102 		 * when running with KTRR.
2103 		 */
2104 		if (__improbable(fault_addr >= physmap_base) && (fault_addr < physmap_end)) {
2105 			panic_with_thread_kernel_state("Unexpected fault in kernel physical aperture", state);
2106 		}
2107 		if (__improbable(fault_addr >= gVirtBase && fault_addr < static_memory_end)) {
2108 			panic_with_thread_kernel_state("Unexpected fault in kernel static region", state);
2109 		}
2110 
2111 		/* check to see if it is just a pmap ref/modify fault */
2112 		if (!is_translation_fault(fault_code)) {
2113 			result = arm_fast_fault(map->pmap,
2114 			    fault_addr,
2115 			    fault_type, (fault_code == FSC_ACCESS_FLAG_FAULT_L3), FALSE);
2116 			if (result == KERN_SUCCESS) {
2117 				return;
2118 			}
2119 		}
2120 
2121 		/**
2122 		 * vm_fault() can be called with preemption disabled (and indeed this is expected for
2123 		 * certain copyio() scenarios), but can't safely be called with interrupts disabled once
2124 		 * the system has gone multi-threaded.  Other than some early-boot situations such as
2125 		 * startup kext loading, kernel paging operations should never be triggered by
2126 		 * non-interruptible code in the first place, so a fault from such a context will
2127 		 * ultimately produce a kernel data abort panic anyway.  In these cases, skip calling
2128 		 * vm_fault() to avoid masking the real kernel panic with a failed VM locking assertion.
2129 		 */
2130 		if (__probable(SPSR_INTERRUPTS_ENABLED(get_saved_state_cpsr(state)) ||
2131 		    startup_phase < STARTUP_SUB_EARLY_BOOT ||
2132 		    current_cpu_datap()->cpu_hibernate)) {
2133 			if (result != KERN_PROTECTION_FAILURE) {
2134 				// VM will query this property when deciding to throttle this fault, we don't want to
2135 				// throttle kernel faults for copyio faults. The presence of a recovery entry is used as a
2136 				// proxy for being in copyio code.
2137 				bool const was_recover = thread->recover;
2138 				thread->recover = was_recover || recover;
2139 
2140 				/*
2141 				 *  We have to "fault" the page in.
2142 				 */
2143 				result = vm_fault(map, fault_addr, fault_type,
2144 				    /* change_wiring */ FALSE, VM_KERN_MEMORY_NONE, interruptible,
2145 				    /* caller_pmap */ NULL, /* caller_pmap_addr */ 0);
2146 
2147 				thread->recover = was_recover;
2148 			}
2149 
2150 			if (result == KERN_SUCCESS) {
2151 				return;
2152 			}
2153 		}
2154 
2155 		/*
2156 		 *  If we have a recover handler, invoke it now.
2157 		 */
2158 		if (recover) {
2159 			handle_kernel_abort_recover(state, esr, fault_addr, thread, recover);
2160 			return;
2161 		}
2162 
2163 		panic_fault_address = fault_addr;
2164 	} else if (is_alignment_fault(fault_code)) {
2165 		if (recover) {
2166 			handle_kernel_abort_recover(state, esr, fault_addr, thread, recover);
2167 			return;
2168 		}
2169 		panic_with_thread_kernel_state("Unaligned kernel data abort.", state);
2170 	} else if (is_parity_error(fault_code)) {
2171 #if defined(APPLE_ARM64_ARCH_FAMILY)
2172 		/*
2173 		 * Platform errors are handled in sleh_sync before interrupts are enabled.
2174 		 */
2175 #else
2176 		panic_with_thread_kernel_state("Kernel parity error.", state);
2177 #endif
2178 	} else {
2179 		kprintf("Unclassified kernel abort (fault_code=0x%x)\n", fault_code);
2180 	}
2181 
2182 	panic_with_thread_kernel_state("Kernel data abort.", state);
2183 }
2184 
2185 extern void syscall_trace(struct arm_saved_state * regs);
2186 
2187 static void
handle_svc(arm_saved_state_t * state)2188 handle_svc(arm_saved_state_t *state)
2189 {
2190 	int      trap_no = get_saved_state_svc_number(state);
2191 	thread_t thread  = current_thread();
2192 	struct   proc *p;
2193 
2194 #define handle_svc_kprintf(x...) /* kprintf("handle_svc: " x) */
2195 
2196 #define TRACE_SYSCALL 1
2197 #if TRACE_SYSCALL
2198 	syscall_trace(state);
2199 #endif
2200 
2201 	thread->iotier_override = THROTTLE_LEVEL_NONE; /* Reset IO tier override before handling SVC from userspace */
2202 
2203 	if (trap_no == (int)PLATFORM_SYSCALL_TRAP_NO) {
2204 		platform_syscall(state);
2205 		panic("Returned from platform_syscall()?");
2206 	}
2207 
2208 	current_cached_proc_cred_update();
2209 
2210 	if (trap_no < 0) {
2211 		switch (trap_no) {
2212 		case MACH_ARM_TRAP_ABSTIME:
2213 			handle_mach_absolute_time_trap(state);
2214 			return;
2215 		case MACH_ARM_TRAP_CONTTIME:
2216 			handle_mach_continuous_time_trap(state);
2217 			return;
2218 		}
2219 
2220 		/* Counting perhaps better in the handler, but this is how it's been done */
2221 		thread->syscalls_mach++;
2222 		mach_syscall(state);
2223 	} else {
2224 		/* Counting perhaps better in the handler, but this is how it's been done */
2225 		thread->syscalls_unix++;
2226 		p = get_bsdthreadtask_info(thread);
2227 
2228 		assert(p);
2229 
2230 		unix_syscall(state, thread, p);
2231 	}
2232 }
2233 
2234 static void
handle_mach_absolute_time_trap(arm_saved_state_t * state)2235 handle_mach_absolute_time_trap(arm_saved_state_t *state)
2236 {
2237 	uint64_t now = mach_absolute_time();
2238 	saved_state64(state)->x[0] = now;
2239 }
2240 
2241 static void
handle_mach_continuous_time_trap(arm_saved_state_t * state)2242 handle_mach_continuous_time_trap(arm_saved_state_t *state)
2243 {
2244 	uint64_t now = mach_continuous_time();
2245 	saved_state64(state)->x[0] = now;
2246 }
2247 
2248 
2249 __attribute__((noreturn))
2250 static void
handle_msr_trap(arm_saved_state_t * state,uint64_t esr)2251 handle_msr_trap(arm_saved_state_t *state, uint64_t esr)
2252 {
2253 	exception_type_t           exception = EXC_BAD_INSTRUCTION;
2254 	mach_exception_data_type_t codes[2]  = {EXC_ARM_UNDEFINED};
2255 	mach_msg_type_number_t     numcodes  = 2;
2256 	uint32_t                   instr     = 0;
2257 
2258 	if (!is_saved_state64(state)) {
2259 		panic("MSR/MRS trap (ESR 0x%llx) from 32-bit state", esr);
2260 	}
2261 
2262 	if (PSR64_IS_KERNEL(get_saved_state_cpsr(state))) {
2263 		panic("MSR/MRS trap (ESR 0x%llx) from kernel", esr);
2264 	}
2265 
2266 	COPYIN(get_saved_state_pc(state), (char *)&instr, sizeof(instr));
2267 	codes[1] = instr;
2268 
2269 	exception_triage(exception, codes, numcodes);
2270 	__builtin_unreachable();
2271 }
2272 
2273 #if __has_feature(ptrauth_calls)
2274 static void
stringify_gpr(unsigned int r,char reg[4])2275 stringify_gpr(unsigned int r, char reg[4])
2276 {
2277 	switch (r) {
2278 	case 29:
2279 		strncpy(reg, "fp", 4);
2280 		return;
2281 
2282 	case 30:
2283 		strncpy(reg, "lr", 4);
2284 		return;
2285 
2286 	case 31:
2287 		strncpy(reg, "xzr", 4);
2288 		return;
2289 
2290 	default:
2291 		snprintf(reg, 4, "x%u", r);
2292 		return;
2293 	}
2294 }
2295 
2296 static void
autxx_instruction_extract_reg(uint32_t instr,char reg[4])2297 autxx_instruction_extract_reg(uint32_t instr, char reg[4])
2298 {
2299 	unsigned int rd = ARM64_INSTR_AUTxx_RD_GET(instr);
2300 	stringify_gpr(rd, reg);
2301 }
2302 
2303 static const char *
autix_system_instruction_extract_reg(uint32_t instr)2304 autix_system_instruction_extract_reg(uint32_t instr)
2305 {
2306 	unsigned int crm_op2 = ARM64_INSTR_AUTIx_SYSTEM_CRM_OP2_GET(instr);
2307 	if (crm_op2 == ARM64_INSTR_AUTIx_SYSTEM_CRM_OP2_AUTIA1716 ||
2308 	    crm_op2 == ARM64_INSTR_AUTIx_SYSTEM_CRM_OP2_AUTIB1716) {
2309 		return "x17";
2310 	} else {
2311 		return "lr";
2312 	}
2313 }
2314 
2315 static void
bxrax_instruction_extract_reg(uint32_t instr,char reg[4])2316 bxrax_instruction_extract_reg(uint32_t instr, char reg[4])
2317 {
2318 	unsigned int rn = ARM64_INSTR_BxRAx_RN_GET(instr);
2319 	stringify_gpr(rn, reg);
2320 }
2321 
2322 static void
handle_pac_fail(arm_saved_state_t * state,uint64_t esr)2323 handle_pac_fail(arm_saved_state_t *state, uint64_t esr)
2324 {
2325 	exception_type_t           exception = EXC_BAD_ACCESS | EXC_PTRAUTH_BIT;
2326 	mach_exception_data_type_t codes[2]  = {EXC_ARM_PAC_FAIL};
2327 	mach_msg_type_number_t     numcodes  = 2;
2328 	uint32_t                   instr     = 0;
2329 
2330 	if (!is_saved_state64(state)) {
2331 		panic("PAC failure (ESR 0x%llx) from 32-bit state", esr);
2332 	}
2333 
2334 	COPYIN(get_saved_state_pc(state), (char *)&instr, sizeof(instr));
2335 
2336 	if (PSR64_IS_KERNEL(get_saved_state_cpsr(state))) {
2337 #define GENERIC_PAC_FAILURE_MSG_FMT "PAC failure from kernel with %s key"
2338 #define AUTXX_MSG_FMT GENERIC_PAC_FAILURE_MSG_FMT " while authing %s"
2339 #define BXRAX_MSG_FMT GENERIC_PAC_FAILURE_MSG_FMT " while branching to %s"
2340 #define RETAX_MSG_FMT GENERIC_PAC_FAILURE_MSG_FMT " while returning"
2341 #define GENERIC_MSG_FMT GENERIC_PAC_FAILURE_MSG_FMT
2342 #define MAX_PAC_MSG_FMT BXRAX_MSG_FMT
2343 
2344 		char msg[strlen(MAX_PAC_MSG_FMT)
2345 		- strlen("%s") + strlen("IA")
2346 		- strlen("%s") + strlen("xzr")
2347 		+ 1];
2348 		ptrauth_key key = (ptrauth_key)(esr & 0x3);
2349 		const char *key_str = ptrauth_key_to_string(key);
2350 
2351 		if (ARM64_INSTR_IS_AUTxx(instr)) {
2352 			char reg[4];
2353 			autxx_instruction_extract_reg(instr, reg);
2354 			snprintf(msg, sizeof(msg), AUTXX_MSG_FMT, key_str, reg);
2355 		} else if (ARM64_INSTR_IS_AUTIx_SYSTEM(instr)) {
2356 			const char *reg = autix_system_instruction_extract_reg(instr);
2357 			snprintf(msg, sizeof(msg), AUTXX_MSG_FMT, key_str, reg);
2358 		} else if (ARM64_INSTR_IS_BxRAx(instr)) {
2359 			char reg[4];
2360 			bxrax_instruction_extract_reg(instr, reg);
2361 			snprintf(msg, sizeof(msg), BXRAX_MSG_FMT, key_str, reg);
2362 		} else if (ARM64_INSTR_IS_RETAx(instr)) {
2363 			snprintf(msg, sizeof(msg), RETAX_MSG_FMT, key_str);
2364 		} else {
2365 			snprintf(msg, sizeof(msg), GENERIC_MSG_FMT, key_str);
2366 		}
2367 		panic_with_thread_kernel_state(msg, state);
2368 	}
2369 
2370 	codes[1] = instr;
2371 
2372 	exception_triage(exception, codes, numcodes);
2373 	__builtin_unreachable();
2374 }
2375 #endif /* __has_feature(ptrauth_calls) */
2376 
2377 __attribute__((noreturn))
2378 static void
handle_bti_fail(arm_saved_state_t * state,uint64_t esr)2379 handle_bti_fail(arm_saved_state_t *state, uint64_t esr)
2380 {
2381 	uint32_t btype = (uint32_t) esr & ISS_BTI_BTYPE_MASK;
2382 
2383 	if (!is_saved_state64(state)) {
2384 		/* BTI is an ARMv8 feature, this should not be possible */
2385 		panic("BTI failure for 32-bit state? (ESR=0x%llx)", esr);
2386 	}
2387 
2388 	/*
2389 	 * We currently only expect BTI to be enabled for kernel pages, so panic if
2390 	 * we detect otherwise.
2391 	 */
2392 	if (!PSR64_IS_KERNEL(get_saved_state_cpsr(state))) {
2393 		panic("Unexpected non-kernel BTI failure? (ESR=0x%llx)", esr);
2394 	}
2395 
2396 #define BTI_FAIL_PTR_FMT "%04x"
2397 #define BTI_FAIL_MSG_FMT "Kernel BTI failure (BTYPE=0x" BTI_FAIL_PTR_FMT ")"
2398 	/* Replace the pointer format with the length of the pointer message+NULL */
2399 	char msg[strlen(BTI_FAIL_MSG_FMT) - strlen(BTI_FAIL_PTR_FMT) + 8 + 1];
2400 	snprintf(msg, sizeof(msg), BTI_FAIL_MSG_FMT, btype);
2401 	panic_with_thread_kernel_state(msg, state);
2402 	__builtin_unreachable();
2403 }
2404 
2405 static void
handle_user_trapped_instruction32(arm_saved_state_t * state,uint64_t esr)2406 handle_user_trapped_instruction32(arm_saved_state_t *state, uint64_t esr)
2407 {
2408 	exception_type_t           exception = EXC_BAD_INSTRUCTION;
2409 	mach_exception_data_type_t codes[2]  = {EXC_ARM_UNDEFINED};
2410 	mach_msg_type_number_t     numcodes  = 2;
2411 	uint32_t                   instr;
2412 
2413 	if (is_saved_state64(state)) {
2414 		panic("ESR (0x%llx) for instruction trapped from U32, but saved state is 64-bit.", esr);
2415 	}
2416 
2417 	if (PSR64_IS_KERNEL(get_saved_state_cpsr(state))) {
2418 		panic("ESR (0x%llx) for instruction trapped from U32, actually came from kernel?", esr);
2419 	}
2420 
2421 	COPYIN(get_saved_state_pc(state), (char *)&instr, sizeof(instr));
2422 	codes[1] = instr;
2423 
2424 	exception_triage(exception, codes, numcodes);
2425 	__builtin_unreachable();
2426 }
2427 
2428 static void
handle_simd_trap(arm_saved_state_t * state,uint64_t esr)2429 handle_simd_trap(arm_saved_state_t *state, uint64_t esr)
2430 {
2431 	exception_type_t           exception = EXC_BAD_INSTRUCTION;
2432 	mach_exception_data_type_t codes[2]  = {EXC_ARM_UNDEFINED};
2433 	mach_msg_type_number_t     numcodes  = 2;
2434 	uint32_t                   instr     = 0;
2435 
2436 	if (PSR64_IS_KERNEL(get_saved_state_cpsr(state))) {
2437 		panic("ESR (0x%llx) for SIMD trap from userland, actually came from kernel?", esr);
2438 	}
2439 
2440 	COPYIN(get_saved_state_pc(state), (char *)&instr, sizeof(instr));
2441 	codes[1] = instr;
2442 
2443 	exception_triage(exception, codes, numcodes);
2444 	__builtin_unreachable();
2445 }
2446 
2447 void
sleh_irq(arm_saved_state_t * state)2448 sleh_irq(arm_saved_state_t *state)
2449 {
2450 	cpu_data_t * cdp __unused             = getCpuDatap();
2451 #if MACH_ASSERT
2452 	int preemption_level = sleh_get_preemption_level();
2453 #endif
2454 
2455 
2456 	sleh_interrupt_handler_prologue(state, DBG_INTR_TYPE_OTHER);
2457 
2458 #if USE_APPLEARMSMP
2459 	PE_handle_ext_interrupt();
2460 #else
2461 	/* Run the registered interrupt handler. */
2462 	cdp->interrupt_handler(cdp->interrupt_target,
2463 	    cdp->interrupt_refCon,
2464 	    cdp->interrupt_nub,
2465 	    cdp->interrupt_source);
2466 #endif
2467 
2468 	entropy_collect();
2469 
2470 
2471 	sleh_interrupt_handler_epilogue();
2472 #if MACH_ASSERT
2473 	if (preemption_level != sleh_get_preemption_level()) {
2474 		panic("irq handler %p changed preemption level from %d to %d", cdp->interrupt_handler, preemption_level, sleh_get_preemption_level());
2475 	}
2476 #endif
2477 }
2478 
2479 void
sleh_fiq(arm_saved_state_t * state)2480 sleh_fiq(arm_saved_state_t *state)
2481 {
2482 	unsigned int type   = DBG_INTR_TYPE_UNKNOWN;
2483 #if MACH_ASSERT
2484 	int preemption_level = sleh_get_preemption_level();
2485 #endif
2486 
2487 #if MONOTONIC_FIQ
2488 	uint64_t pmcr0 = 0, upmsr = 0;
2489 #endif /* MONOTONIC_FIQ */
2490 
2491 #if defined(HAS_IPI)
2492 	boolean_t    is_ipi = FALSE;
2493 	uint64_t     ipi_sr = 0;
2494 
2495 	if (gFastIPI) {
2496 		MRS(ipi_sr, "S3_5_C15_C1_1");
2497 
2498 		if (ipi_sr & ARM64_IPISR_IPI_PENDING) {
2499 			is_ipi = TRUE;
2500 		}
2501 	}
2502 
2503 	if (is_ipi) {
2504 		type = DBG_INTR_TYPE_IPI;
2505 	} else
2506 #endif /* defined(HAS_IPI) */
2507 	if (ml_get_timer_pending()) {
2508 		type = DBG_INTR_TYPE_TIMER;
2509 	}
2510 #if MONOTONIC_FIQ
2511 	/* Consult the PMI sysregs last, after IPI/timer
2512 	 * classification.
2513 	 */
2514 	else if (mt_pmi_pending(&pmcr0, &upmsr)) {
2515 		type = DBG_INTR_TYPE_PMI;
2516 	}
2517 #endif /* MONOTONIC_FIQ */
2518 
2519 	sleh_interrupt_handler_prologue(state, type);
2520 
2521 #if APPLEVIRTUALPLATFORM
2522 	uint64_t iar = __builtin_arm_rsr64("ICC_IAR0_EL1");
2523 #endif
2524 
2525 #if defined(HAS_IPI)
2526 	if (type == DBG_INTR_TYPE_IPI) {
2527 		/*
2528 		 * Order is important here: we must ack the IPI by writing IPI_SR
2529 		 * before we call cpu_signal_handler().  Otherwise, there will be
2530 		 * a window between the completion of pending-signal processing in
2531 		 * cpu_signal_handler() and the ack during which a newly-issued
2532 		 * IPI to this CPU may be lost.  ISB is required to ensure the msr
2533 		 * is retired before execution of cpu_signal_handler().
2534 		 */
2535 		MSR("S3_5_C15_C1_1", ARM64_IPISR_IPI_PENDING);
2536 		__builtin_arm_isb(ISB_SY);
2537 		cpu_signal_handler();
2538 	} else
2539 #endif /* defined(HAS_IPI) */
2540 #if MONOTONIC_FIQ
2541 	if (type == DBG_INTR_TYPE_PMI) {
2542 		INTERRUPT_MASKED_DEBUG_START(mt_fiq, DBG_INTR_TYPE_PMI);
2543 		mt_fiq(getCpuDatap(), pmcr0, upmsr);
2544 		INTERRUPT_MASKED_DEBUG_END();
2545 	} else
2546 #endif /* MONOTONIC_FIQ */
2547 	{
2548 		/*
2549 		 * We don't know that this is a timer, but we don't have insight into
2550 		 * the other interrupts that go down this path.
2551 		 */
2552 
2553 		cpu_data_t *cdp = getCpuDatap();
2554 
2555 		cdp->cpu_decrementer = -1; /* Large */
2556 
2557 		/*
2558 		 * ARM64_TODO: whether we're coming from userland is ignored right now.
2559 		 * We can easily thread it through, but not bothering for the
2560 		 * moment (AArch32 doesn't either).
2561 		 */
2562 		INTERRUPT_MASKED_DEBUG_START(rtclock_intr, DBG_INTR_TYPE_TIMER);
2563 		rtclock_intr(TRUE);
2564 		INTERRUPT_MASKED_DEBUG_END();
2565 	}
2566 
2567 #if APPLEVIRTUALPLATFORM
2568 	if (iar != GIC_SPURIOUS_IRQ) {
2569 		__builtin_arm_wsr64("ICC_EOIR0_EL1", iar);
2570 		__builtin_arm_isb(ISB_SY);
2571 	}
2572 #endif
2573 
2574 	sleh_interrupt_handler_epilogue();
2575 #if MACH_ASSERT
2576 	if (preemption_level != sleh_get_preemption_level()) {
2577 		panic("fiq type %u changed preemption level from %d to %d", type, preemption_level, sleh_get_preemption_level());
2578 	}
2579 #endif
2580 }
2581 
2582 void
sleh_serror(arm_context_t * context,uint64_t esr,vm_offset_t far)2583 sleh_serror(arm_context_t *context, uint64_t esr, vm_offset_t far)
2584 {
2585 	task_vtimer_check(current_thread());
2586 
2587 	KDBG_RELEASE(MACHDBG_CODE(DBG_MACH_EXCP_SERR_ARM, 0) | DBG_FUNC_START,
2588 	    esr, VM_KERNEL_ADDRHIDE(far));
2589 	arm_saved_state_t *state = &context->ss;
2590 #if MACH_ASSERT
2591 	int preemption_level = sleh_get_preemption_level();
2592 #endif
2593 
2594 	if (PSR64_IS_USER(get_saved_state_cpsr(state))) {
2595 		/* Sanitize FAR (only if we came from userspace) */
2596 		saved_state64(state)->far = 0;
2597 	}
2598 
2599 	ASSERT_CONTEXT_SANITY(context);
2600 	arm64_platform_error(state, esr, far, PLAT_ERR_SRC_ASYNC);
2601 #if MACH_ASSERT
2602 	if (preemption_level != sleh_get_preemption_level()) {
2603 		panic("serror changed preemption level from %d to %d", preemption_level, sleh_get_preemption_level());
2604 	}
2605 #endif
2606 	KDBG_RELEASE(MACHDBG_CODE(DBG_MACH_EXCP_SERR_ARM, 0) | DBG_FUNC_END,
2607 	    esr, VM_KERNEL_ADDRHIDE(far));
2608 }
2609 
2610 void
mach_syscall_trace_exit(unsigned int retval,unsigned int call_number)2611 mach_syscall_trace_exit(unsigned int retval,
2612     unsigned int call_number)
2613 {
2614 	KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
2615 	    MACHDBG_CODE(DBG_MACH_EXCP_SC, (call_number)) |
2616 	    DBG_FUNC_END, retval, 0, 0, 0, 0);
2617 }
2618 
2619 __attribute__((noreturn))
2620 void
thread_syscall_return(kern_return_t error)2621 thread_syscall_return(kern_return_t error)
2622 {
2623 	thread_t thread;
2624 	struct arm_saved_state *state;
2625 
2626 	thread = current_thread();
2627 	state = get_user_regs(thread);
2628 
2629 	assert(is_saved_state64(state));
2630 	saved_state64(state)->x[0] = error;
2631 
2632 #if MACH_ASSERT
2633 	kern_allocation_name_t
2634 	prior __assert_only = thread_get_kernel_state(thread)->allocation_name;
2635 	assertf(prior == NULL, "thread_set_allocation_name(\"%s\") not cleared", kern_allocation_get_name(prior));
2636 #endif /* MACH_ASSERT */
2637 
2638 	if (kdebug_enable) {
2639 		/* Invert syscall number (negative for a mach syscall) */
2640 		mach_syscall_trace_exit(error, (-1) * get_saved_state_svc_number(state));
2641 	}
2642 
2643 	thread_exception_return();
2644 }
2645 
2646 void
syscall_trace(struct arm_saved_state * regs __unused)2647 syscall_trace(
2648 	struct arm_saved_state * regs __unused)
2649 {
2650 	/* kprintf("syscall: %d\n", saved_state64(regs)->x[16]);  */
2651 }
2652 
2653 static void
sleh_interrupt_handler_prologue(arm_saved_state_t * state,unsigned int type)2654 sleh_interrupt_handler_prologue(arm_saved_state_t *state, unsigned int type)
2655 {
2656 	const bool is_user = PSR64_IS_USER(get_saved_state_cpsr(state));
2657 
2658 	if (is_user == true) {
2659 		/* Sanitize FAR (only if the interrupt occurred while the CPU was in usermode) */
2660 		saved_state64(state)->far = 0;
2661 	}
2662 
2663 	recount_enter_interrupt();
2664 
2665 	task_vtimer_check(current_thread());
2666 
2667 	uint64_t pc = is_user ? get_saved_state_pc(state) :
2668 	    VM_KERNEL_UNSLIDE(get_saved_state_pc(state));
2669 
2670 	KDBG_RELEASE(MACHDBG_CODE(DBG_MACH_EXCP_INTR, 0) | DBG_FUNC_START,
2671 	    0, pc, is_user, type);
2672 }
2673 
2674 static void
sleh_interrupt_handler_epilogue(void)2675 sleh_interrupt_handler_epilogue(void)
2676 {
2677 #if KPERF
2678 	kperf_interrupt();
2679 #endif /* KPERF */
2680 	KDBG_RELEASE(MACHDBG_CODE(DBG_MACH_EXCP_INTR, 0) | DBG_FUNC_END);
2681 	recount_leave_interrupt();
2682 }
2683 
2684 void
sleh_invalid_stack(arm_context_t * context,uint64_t esr __unused,vm_offset_t far __unused)2685 sleh_invalid_stack(arm_context_t *context, uint64_t esr __unused, vm_offset_t far __unused)
2686 {
2687 	thread_t thread = current_thread();
2688 	vm_offset_t kernel_stack_bottom, sp;
2689 
2690 	sp = get_saved_state_sp(&context->ss);
2691 	vm_offset_t kstackptr = (vm_offset_t)thread->machine.kstackptr;
2692 	kernel_stack_bottom = round_page(kstackptr) - KERNEL_STACK_SIZE;
2693 
2694 	if ((sp < kernel_stack_bottom) && (sp >= (kernel_stack_bottom - PAGE_SIZE))) {
2695 		panic_with_thread_kernel_state("Invalid kernel stack pointer (probable overflow).", &context->ss);
2696 	}
2697 
2698 	panic_with_thread_kernel_state("Invalid kernel stack pointer (probable corruption).", &context->ss);
2699 }
2700 
2701 
2702 #if MACH_ASSERT
2703 static int trap_handled;
2704 static const char *
handle_recoverable_kernel_trap(__unused void * tstate,uint16_t comment)2705 handle_recoverable_kernel_trap(
2706 	__unused void     *tstate,
2707 	uint16_t          comment)
2708 {
2709 	assert(comment == TEST_RECOVERABLE_SOFT_TRAP);
2710 
2711 	printf("Recoverable trap handled.\n");
2712 	trap_handled = 1;
2713 
2714 	return NULL;
2715 }
2716 
2717 KERNEL_BRK_DESCRIPTOR_DEFINE(test_desc,
2718     .type                = TRAP_TELEMETRY_TYPE_KERNEL_BRK_TEST,
2719     .base                = TEST_RECOVERABLE_SOFT_TRAP,
2720     .max                 = TEST_RECOVERABLE_SOFT_TRAP,
2721     .options             = BRK_TELEMETRY_OPTIONS_RECOVERABLE_DEFAULT(
2722 	    /* enable_telemetry */ false),
2723     .handle_breakpoint   = handle_recoverable_kernel_trap);
2724 
2725 static int
recoverable_kernel_trap_test(__unused int64_t in,int64_t * out)2726 recoverable_kernel_trap_test(__unused int64_t in, int64_t *out)
2727 {
2728 	ml_recoverable_trap(TEST_RECOVERABLE_SOFT_TRAP);
2729 
2730 	*out = trap_handled;
2731 	return 0;
2732 }
2733 
2734 SYSCTL_TEST_REGISTER(recoverable_kernel_trap, recoverable_kernel_trap_test);
2735 
2736 #endif
2737 
2738 #if CONFIG_SPTM
2739 /**
2740  * Evaluate the panic lockdown policy for a synchronous EL1 SP0 exception
2741  *
2742  * Returns true if panic lockdown should be initiated (but does not itself do
2743  * so)
2744  */
2745 __SECURITY_STACK_DISALLOWED_PUSH
2746 bool
sleh_panic_lockdown_should_initiate_el1_sp0_sync(uint64_t esr,uint64_t elr,uint64_t far,uint64_t spsr)2747 sleh_panic_lockdown_should_initiate_el1_sp0_sync(uint64_t esr, uint64_t elr,
2748     uint64_t far, uint64_t spsr)
2749 {
2750 	const esr_exception_class_t class = ESR_EC(esr);
2751 	const bool any_exceptions_masked = spsr & DAIF_STANDARD_DISABLE;
2752 
2753 	switch (class) {
2754 	case ESR_EC_PC_ALIGN:   /* PC misaligned (should never happen) */
2755 	case ESR_EC_IABORT_EL1: /* Potential iPAC failure (poisoned PC) */
2756 	case ESR_EC_PAC_FAIL: { /* FPAC fail */
2757 		return true;
2758 	}
2759 
2760 	case ESR_EC_BRK_AARCH64: {
2761 		/*
2762 		 * Breakpoints are used on non-FPAC systems to signal some PAC failures
2763 		 */
2764 #if HAS_TELEMETRY_KERNEL_BRK
2765 		const struct kernel_brk_descriptor *desc;
2766 		desc = find_brk_descriptor_by_comment(ISS_BRK_COMMENT(esr));
2767 		if (desc && desc->options.recoverable) {
2768 			/*
2769 			 * We matched a breakpoint and it's recoverable, skip lockdown.
2770 			 */
2771 			return false;
2772 		}
2773 #endif /* HAS_TELEMETRY_KERNEL_BRK */
2774 
2775 		/*
2776 		 * If we don't support telemetry breakpoints and/or didn't match a
2777 		 * recoverable breakpoint, the exception is fatal.
2778 		 */
2779 		return true;
2780 	}
2781 
2782 	case ESR_EC_DABORT_EL1: {
2783 		const struct copyio_recovery_entry *cre =
2784 		    find_copyio_recovery_entry(elr);
2785 		if (cre) {
2786 
2787 			/*
2788 			 * copyio faults are recoverable regardless of whether or not
2789 			 * exceptions are masked.
2790 			 */
2791 			return false;
2792 		}
2793 
2794 
2795 		/*
2796 		 * Heuristic: if FAR != XPAC(FAR), the pointer was likely corrupted
2797 		 * due to PAC.
2798 		 */
2799 		const uint64_t far_stripped =
2800 		    (uint64_t)ptrauth_strip((void *)far, ptrauth_key_asda);
2801 
2802 		if (far != far_stripped) {
2803 			/* potential dPAC failure (poisoined address) */
2804 			return true;
2805 		}
2806 
2807 		if (any_exceptions_masked && startup_phase >= STARTUP_SUB_LOCKDOWN) {
2808 			/*
2809 			 * Any data abort taken with exceptions masked is fatal if we're
2810 			 * past early boot.
2811 			 */
2812 			return true;
2813 		}
2814 
2815 		return false;
2816 	}
2817 
2818 	case ESR_EC_UNCATEGORIZED: {
2819 		/* Undefined instruction (GDBTRAP for stackshots, etc.) */
2820 		return false;
2821 	}
2822 
2823 	case ESR_EC_BTI_FAIL: {
2824 		/* Kernel BTI exceptions are recoverable only in telemetry mode */
2825 #ifdef CONFIG_BTI_TELEMETRY
2826 		return false;
2827 #else
2828 		return true;
2829 #endif /* CONFIG_BTI_TELEMETRY */
2830 	}
2831 
2832 	default: {
2833 		if (!any_exceptions_masked) {
2834 			/*
2835 			 * When exceptions are not masked, we default-allow exceptions.
2836 			 */
2837 			return false;
2838 		}
2839 
2840 		if (startup_phase < STARTUP_SUB_LOCKDOWN) {
2841 			/*
2842 			 * Ignore early boot exceptions even if exceptions are masked.
2843 			 */
2844 			return false;
2845 		}
2846 
2847 		/* Default-deny all others when exceptions are masked */
2848 		return true;
2849 	}
2850 	}
2851 }
2852 __SECURITY_STACK_DISALLOWED_POP
2853 #endif /* CONFIG_SPTM */
2854