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_user_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 ©_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
619 #if KASAN_TBI
620 kasan_unpoison_curstack(true);
621 #endif /* KASAN_TBI */
622 arm64_thread_exception_return();
623 __builtin_unreachable();
624 }
625
626 /*
627 * check whether task vtimers are running and set thread and CPU BSD AST
628 *
629 * must be called with interrupts masked so updates of fields are atomic
630 * must be emitted inline to avoid generating an FBT probe on the exception path
631 *
632 */
633 __attribute__((__always_inline__))
634 static inline void
task_vtimer_check(thread_t thread)635 task_vtimer_check(thread_t thread)
636 {
637 task_t task = get_threadtask_early(thread);
638
639 if (__improbable(task != NULL && task->vtimers)) {
640 thread_ast_set(thread, AST_BSD);
641 thread->machine.CpuDatap->cpu_pending_ast |= AST_BSD;
642 }
643 }
644
645 #if MACH_ASSERT
646 /**
647 * A version of get_preemption_level() that works in early boot.
648 *
649 * If an exception is raised in early boot before the initial thread has been
650 * set up, then calling get_preemption_level() in the SLEH will trigger an
651 * infinitely-recursing exception. This function handles this edge case.
652 */
653 static inline int
sleh_get_preemption_level(void)654 sleh_get_preemption_level(void)
655 {
656 if (__improbable(current_thread() == NULL)) {
657 return 0;
658 }
659 return get_preemption_level();
660 }
661 #endif // MACH_ASSERT
662
663 static inline bool
is_platform_error(uint64_t esr)664 is_platform_error(uint64_t esr)
665 {
666 esr_exception_class_t class = ESR_EC(esr);
667 uint32_t iss = ESR_ISS(esr);
668 fault_status_t fault_code;
669
670 if (class == ESR_EC_DABORT_EL0 || class == ESR_EC_DABORT_EL1) {
671 fault_code = ISS_DA_FSC(iss);
672 } else if (class == ESR_EC_IABORT_EL0 || class == ESR_EC_IABORT_EL1) {
673 fault_code = ISS_IA_FSC(iss);
674 } else {
675 return false;
676 }
677
678 return is_parity_error(fault_code) || is_sync_external_abort(fault_code) ||
679 is_table_walk_error(fault_code);
680 }
681
682 void
sleh_synchronous(arm_context_t * context,uint64_t esr,vm_offset_t far,__unused bool did_initiate_panic_lockdown)683 sleh_synchronous(arm_context_t *context, uint64_t esr, vm_offset_t far, __unused bool did_initiate_panic_lockdown)
684 {
685 esr_exception_class_t class = ESR_EC(esr);
686 arm_saved_state_t * state = &context->ss;
687 thread_t thread = current_thread();
688 #if MACH_ASSERT
689 int preemption_level = sleh_get_preemption_level();
690 #endif
691 expected_fault_handler_t expected_fault_handler = NULL;
692 #ifdef CONFIG_XNUPOST
693 expected_fault_handler_t saved_expected_fault_handler = NULL;
694 uintptr_t saved_expected_fault_addr = 0;
695 uintptr_t saved_expected_fault_pc = 0;
696 #endif /* CONFIG_XNUPOST */
697
698 ASSERT_CONTEXT_SANITY(context);
699
700 task_vtimer_check(thread);
701
702 #if CONFIG_DTRACE
703 /*
704 * Handle kernel DTrace probes as early as possible to minimize the likelihood
705 * that this path will itself trigger a DTrace probe, which would lead to infinite
706 * probe recursion.
707 */
708 if (__improbable((class == ESR_EC_UNCATEGORIZED) && tempDTraceTrapHook &&
709 (tempDTraceTrapHook(EXC_BAD_INSTRUCTION, state, 0, 0) == KERN_SUCCESS))) {
710 #if CONFIG_SPTM
711 if (__improbable(did_initiate_panic_lockdown)) {
712 panic("Unexpectedly initiated lockdown for DTrace probe?");
713 }
714 #endif
715 return;
716 }
717 #endif
718 bool is_user = PSR64_IS_USER(get_saved_state_cpsr(state));
719
720 #if CONFIG_SPTM
721 // Lockdown should only be initiated for kernel exceptions
722 assert(!(is_user && did_initiate_panic_lockdown));
723 #endif /* CONFIG_SPTM */
724
725 /*
726 * Use KERNEL_DEBUG_CONSTANT_IST here to avoid producing tracepoints
727 * that would disclose the behavior of PT_DENY_ATTACH processes.
728 */
729 if (is_user) {
730 /* Sanitize FAR (but only if the exception was taken from userspace) */
731 switch (class) {
732 case ESR_EC_IABORT_EL1:
733 case ESR_EC_IABORT_EL0:
734 /* If this is a SEA, since we can't trust FnV, just clear FAR from the save area. */
735 if (ISS_IA_FSC(ESR_ISS(esr)) == FSC_SYNC_EXT_ABORT) {
736 saved_state64(state)->far = 0;
737 }
738 break;
739 case ESR_EC_DABORT_EL1:
740 case ESR_EC_DABORT_EL0:
741 /* If this is a SEA, since we can't trust FnV, just clear FAR from the save area. */
742 if (ISS_DA_FSC(ESR_ISS(esr)) == FSC_SYNC_EXT_ABORT) {
743 saved_state64(state)->far = 0;
744 }
745 break;
746 case ESR_EC_WATCHPT_MATCH_EL1:
747 case ESR_EC_WATCHPT_MATCH_EL0:
748 case ESR_EC_PC_ALIGN:
749 break; /* FAR_ELx is valid */
750 default:
751 saved_state64(state)->far = 0;
752 break;
753 }
754
755 thread->machine.exception_trace_code = (uint16_t)(ARM64_KDBG_CODE_USER | class);
756 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
757 MACHDBG_CODE(DBG_MACH_EXCP_SYNC_ARM, thread->machine.exception_trace_code) | DBG_FUNC_START,
758 esr, far, get_saved_state_pc(state), 0, 0);
759 } else {
760 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
761 MACHDBG_CODE(DBG_MACH_EXCP_SYNC_ARM, ARM64_KDBG_CODE_KERNEL | class) | DBG_FUNC_START,
762 esr, VM_KERNEL_ADDRHIDE(far), VM_KERNEL_UNSLIDE(get_saved_state_pc(state)), 0, 0);
763 }
764
765 if (__improbable(ESR_INSTR_IS_2BYTES(esr))) {
766 /*
767 * We no longer support 32-bit, which means no 2-byte
768 * instructions.
769 */
770 if (is_user) {
771 panic("Exception on 2-byte instruction, "
772 "context=%p, esr=%#llx, far=%p",
773 context, esr, (void *)far);
774 } else {
775 panic_with_thread_kernel_state("Exception on 2-byte instruction", state);
776 }
777 }
778
779 #ifdef CONFIG_XNUPOST
780 if (thread->machine.expected_fault_handler != NULL) {
781 bool matching_fault_pc = false;
782 saved_expected_fault_handler = thread->machine.expected_fault_handler;
783 saved_expected_fault_addr = thread->machine.expected_fault_addr;
784 saved_expected_fault_pc = thread->machine.expected_fault_pc;
785
786 thread->machine.expected_fault_handler = NULL;
787 thread->machine.expected_fault_addr = 0;
788 thread->machine.expected_fault_pc = 0;
789
790 #if __has_feature(ptrauth_calls)
791 /*
792 * Compare only the bits of PC which make up the virtual address.
793 * This ignores the upper bits, which may have been corrupted by HW in
794 * platform dependent ways to signal pointer authentication fault.
795 */
796 uint64_t fault_addr_mask = fault_addr_bitmask(0, 64 - T1SZ_BOOT - 1);
797 uint64_t masked_expected_pc = saved_expected_fault_pc & fault_addr_mask;
798 uint64_t masked_saved_pc = get_saved_state_pc(state) & fault_addr_mask;
799 matching_fault_pc = masked_expected_pc == masked_saved_pc;
800 #else
801 matching_fault_pc =
802 (saved_expected_fault_pc == get_saved_state_pc(state));
803 #endif /* ptrauth_call */
804 if (saved_expected_fault_addr == far ||
805 matching_fault_pc) {
806 expected_fault_handler = saved_expected_fault_handler;
807 }
808 }
809 #endif /* CONFIG_XNUPOST */
810
811 if (__improbable(is_platform_error(esr))) {
812 /*
813 * Must gather error info in platform error handler before
814 * thread is preempted to another core/cluster to guarantee
815 * accurate error details
816 */
817
818 arm64_platform_error(state, esr, far, PLAT_ERR_SRC_SYNC);
819 #if CONFIG_SPTM
820 if (__improbable(did_initiate_panic_lockdown)) {
821 panic("Panic lockdown initiated for platform error");
822 }
823 #endif
824 return;
825 }
826
827 if (is_user && class == ESR_EC_DABORT_EL0) {
828 thread_reset_pcs_will_fault(thread);
829 }
830
831 #if CONFIG_SPTM
832 if (__improbable(did_initiate_panic_lockdown && current_thread() != NULL)) {
833 /*
834 * If we initiated panic lockdown, we must disable preemption before
835 * enabling interrupts. While unlikely, preempting the panicked thread
836 * after lockdown has occurred may hang the system if all cores end up
837 * blocked while attempting to return to user space.
838 */
839 disable_preemption();
840 }
841 #endif /* CONFIG_SPTM */
842
843 /* Inherit the interrupt masks from previous context */
844 if (SPSR_INTERRUPTS_ENABLED(get_saved_state_cpsr(state))) {
845 ml_set_interrupts_enabled(TRUE);
846 }
847
848
849 switch (class) {
850 case ESR_EC_SVC_64:
851 if (!is_saved_state64(state) || !is_user) {
852 panic("Invalid SVC_64 context");
853 }
854
855 handle_svc(state);
856 break;
857
858 case ESR_EC_DABORT_EL0:
859 handle_abort(state, esr, far, inspect_data_abort, handle_user_abort, expected_fault_handler);
860 break;
861
862 case ESR_EC_MSR_TRAP:
863 handle_msr_trap(state, esr);
864 break;
865 /**
866 * Some APPLEVIRTUALPLATFORM targets do not specify armv8.6, but it's still possible for
867 * them to be hosted by a host that implements ARM_FPAC. There's no way for such a host
868 * to disable it or trap it without substantial performance penalty. Therefore, the FPAC
869 * handler here needs to be built into the guest kernels to prevent the exception to fall
870 * through.
871 */
872 #if __has_feature(ptrauth_calls)
873 case ESR_EC_PAC_FAIL:
874 #ifdef CONFIG_XNUPOST
875 if (expected_fault_handler != NULL && expected_fault_handler(state)) {
876 break;
877 }
878 #endif /* CONFIG_XNUPOST */
879 handle_pac_fail(state, esr);
880 __builtin_unreachable();
881
882 #endif /* __has_feature(ptrauth_calls) */
883
884 #if HAS_ARM_FEAT_SME
885 case ESR_EC_SME:
886 handle_sme_trap(state, esr);
887 break;
888 #endif /* HAS_ARM_FEAT_SME */
889
890 case ESR_EC_IABORT_EL0:
891 handle_abort(state, esr, far, inspect_instruction_abort, handle_user_abort, expected_fault_handler);
892 break;
893
894 case ESR_EC_IABORT_EL1:
895 #ifdef CONFIG_XNUPOST
896 if ((expected_fault_handler != NULL) && expected_fault_handler(state)) {
897 break;
898 }
899 #endif /* CONFIG_XNUPOST */
900
901 panic_with_thread_kernel_state("Kernel instruction fetch abort", state);
902
903 case ESR_EC_PC_ALIGN:
904 handle_pc_align(state);
905 __builtin_unreachable();
906
907 case ESR_EC_DABORT_EL1:
908 handle_abort(state, esr, far, inspect_data_abort, handle_kernel_abort, expected_fault_handler);
909 break;
910
911 case ESR_EC_UNCATEGORIZED:
912 assert(!ESR_ISS(esr));
913
914 #if CONFIG_XNUPOST
915 if (!is_user && (expected_fault_handler != NULL) && expected_fault_handler(state)) {
916 /*
917 * The fault handler accepted the exception and handled it on its
918 * own. Don't trap to the debugger/panic.
919 */
920 break;
921 }
922 #endif /* CONFIG_XNUPOST */
923 handle_uncategorized(&context->ss);
924 break;
925
926 case ESR_EC_SP_ALIGN:
927 handle_sp_align(state);
928 __builtin_unreachable();
929
930 case ESR_EC_BKPT_AARCH32:
931 handle_user_breakpoint(state, esr);
932 __builtin_unreachable();
933
934 case ESR_EC_BRK_AARCH64:
935 #ifdef CONFIG_XNUPOST
936 if ((expected_fault_handler != NULL) && expected_fault_handler(state)) {
937 break;
938 }
939 #endif /* CONFIG_XNUPOST */
940 if (PSR64_IS_KERNEL(get_saved_state_cpsr(state))) {
941 handle_kernel_breakpoint(state, esr);
942 break;
943 } else {
944 handle_user_breakpoint(state, esr);
945 __builtin_unreachable();
946 }
947
948 case ESR_EC_BKPT_REG_MATCH_EL0:
949 if (FSC_DEBUG_FAULT == ISS_SSDE_FSC(esr)) {
950 handle_user_breakpoint(state, esr);
951 }
952 panic("Unsupported Class %u event code. state=%p class=%u esr=%llu far=%p",
953 class, state, class, esr, (void *)far);
954 __builtin_unreachable();
955
956 case ESR_EC_BKPT_REG_MATCH_EL1:
957 panic_with_thread_kernel_state("Hardware Breakpoint Debug exception from kernel. Panic (by design)", state);
958 __builtin_unreachable();
959
960 case ESR_EC_SW_STEP_DEBUG_EL0:
961 if (FSC_DEBUG_FAULT == ISS_SSDE_FSC(esr)) {
962 handle_sw_step_debug(state);
963 }
964 panic("Unsupported Class %u event code. state=%p class=%u esr=%llu far=%p",
965 class, state, class, esr, (void *)far);
966 __builtin_unreachable();
967
968 case ESR_EC_SW_STEP_DEBUG_EL1:
969 panic_with_thread_kernel_state("Software Step Debug exception from kernel. Panic (by design)", state);
970 __builtin_unreachable();
971
972 case ESR_EC_WATCHPT_MATCH_EL0:
973 if (FSC_DEBUG_FAULT == ISS_SSDE_FSC(esr)) {
974 handle_watchpoint(far);
975 }
976 panic("Unsupported Class %u event code. state=%p class=%u esr=%llu far=%p",
977 class, state, class, esr, (void *)far);
978 __builtin_unreachable();
979
980 case ESR_EC_WATCHPT_MATCH_EL1:
981 /*
982 * If we hit a watchpoint in kernel mode, probably in a copyin/copyout which we don't want to
983 * abort. Turn off watchpoints and keep going; we'll turn them back on in return_from_exception..
984 */
985 if (FSC_DEBUG_FAULT == ISS_SSDE_FSC(esr)) {
986 arm_debug_set(NULL);
987 break; /* return to first level handler */
988 }
989 panic("Unsupported Class %u event code. state=%p class=%u esr=%llu far=%p",
990 class, state, class, esr, (void *)far);
991 __builtin_unreachable();
992
993 case ESR_EC_TRAP_SIMD_FP:
994 handle_simd_trap(state, esr);
995 __builtin_unreachable();
996
997 case ESR_EC_ILLEGAL_INSTR_SET:
998 panic("Illegal instruction set exception. state=%p class=%u esr=%llu far=%p spsr=0x%x",
999 state, class, esr, (void *)far, get_saved_state_cpsr(state));
1000 __builtin_unreachable();
1001
1002 case ESR_EC_MCR_MRC_CP15_TRAP:
1003 case ESR_EC_MCRR_MRRC_CP15_TRAP:
1004 case ESR_EC_MCR_MRC_CP14_TRAP:
1005 case ESR_EC_LDC_STC_CP14_TRAP:
1006 case ESR_EC_MCRR_MRRC_CP14_TRAP:
1007 handle_user_trapped_instruction32(state, esr);
1008 __builtin_unreachable();
1009
1010 case ESR_EC_WFI_WFE:
1011 // Use of WFI or WFE instruction when they have been disabled for EL0
1012 handle_wf_trap(state);
1013 __builtin_unreachable();
1014
1015 case ESR_EC_FLOATING_POINT_64:
1016 handle_fp_trap(state, esr);
1017 __builtin_unreachable();
1018 case ESR_EC_BTI_FAIL:
1019 #ifdef CONFIG_XNUPOST
1020 if ((expected_fault_handler != NULL) && expected_fault_handler(state)) {
1021 break;
1022 }
1023 #endif /* CONFIG_XNUPOST */
1024 #ifdef CONFIG_BTI_TELEMETRY
1025 if (bti_telemetry_handle_exception(state)) {
1026 /* Telemetry has accepted and corrected the exception, continue */
1027 break;
1028 }
1029 #endif /* CONFIG_BTI_TELEMETRY */
1030 handle_bti_fail(state, esr);
1031 __builtin_unreachable();
1032
1033 default:
1034 handle_uncategorized(state);
1035 }
1036
1037 #ifdef CONFIG_XNUPOST
1038 if (saved_expected_fault_handler != NULL) {
1039 thread->machine.expected_fault_handler = saved_expected_fault_handler;
1040 thread->machine.expected_fault_addr = saved_expected_fault_addr;
1041 thread->machine.expected_fault_pc = saved_expected_fault_pc;
1042 }
1043 #endif /* CONFIG_XNUPOST */
1044
1045 if (is_user) {
1046 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
1047 MACHDBG_CODE(DBG_MACH_EXCP_SYNC_ARM, thread->machine.exception_trace_code) | DBG_FUNC_END,
1048 esr, far, get_saved_state_pc(state), 0, 0);
1049 thread->machine.exception_trace_code = 0;
1050 } else {
1051 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
1052 MACHDBG_CODE(DBG_MACH_EXCP_SYNC_ARM, ARM64_KDBG_CODE_KERNEL | class) | DBG_FUNC_END,
1053 esr, VM_KERNEL_ADDRHIDE(far), VM_KERNEL_UNSLIDE(get_saved_state_pc(state)), 0, 0);
1054 }
1055
1056 #if CONFIG_SPTM
1057 if (__improbable(did_initiate_panic_lockdown)) {
1058 #if CONFIG_XNUPOST
1059 bool can_recover = !!(expected_fault_handler);
1060 #else
1061 bool can_recover = false;
1062 #endif /* CONFIG_XNU_POST */
1063
1064 if (can_recover) {
1065 /*
1066 * If we matched an exception handler, this was a simulated lockdown
1067 * and so we can recover. Re-enable preemption if we disabled it.
1068 */
1069 if (current_thread() != NULL) {
1070 enable_preemption();
1071 }
1072 } else {
1073 /*
1074 * fleh already triggered a lockdown but we, for whatever reason,
1075 * didn't end up finding a reason to panic. Catch all panic in this
1076 * case.
1077 * Note that the panic here has no security benefit as the system is
1078 * already hosed, this is merely for telemetry.
1079 */
1080 panic_with_thread_kernel_state("Panic lockdown initiated", state);
1081 }
1082 }
1083 #endif /* CONFIG_SPTM */
1084
1085 #if MACH_ASSERT
1086 if (preemption_level != sleh_get_preemption_level()) {
1087 panic("synchronous exception changed preemption level from %d to %d", preemption_level, sleh_get_preemption_level());
1088 }
1089 #endif
1090
1091 }
1092
1093 /*
1094 * Uncategorized exceptions are a catch-all for general execution errors.
1095 * ARM64_TODO: For now, we assume this is for undefined instruction exceptions.
1096 */
1097 static void
handle_uncategorized(arm_saved_state_t * state)1098 handle_uncategorized(arm_saved_state_t *state)
1099 {
1100 exception_type_t exception = EXC_BAD_INSTRUCTION;
1101 mach_exception_data_type_t codes[2] = {EXC_ARM_UNDEFINED};
1102 mach_msg_type_number_t numcodes = 2;
1103 uint32_t instr = 0;
1104
1105 COPYIN(get_saved_state_pc(state), (char *)&instr, sizeof(instr));
1106
1107 #if CONFIG_DTRACE
1108
1109 if (PSR64_IS_USER64(get_saved_state_cpsr(state))) {
1110 /*
1111 * For a 64bit user process, we care about all 4 bytes of the
1112 * instr.
1113 */
1114 if (instr == FASTTRAP_ARM64_INSTR || instr == FASTTRAP_ARM64_RET_INSTR) {
1115 if (dtrace_user_probe(state) == KERN_SUCCESS) {
1116 return;
1117 }
1118 }
1119 } else if (PSR64_IS_USER32(get_saved_state_cpsr(state))) {
1120 /*
1121 * For a 32bit user process, we check for thumb mode, in
1122 * which case we only care about a 2 byte instruction length.
1123 * For non-thumb mode, we care about all 4 bytes of the instructin.
1124 */
1125 if (get_saved_state_cpsr(state) & PSR64_MODE_USER32_THUMB) {
1126 if (((uint16_t)instr == FASTTRAP_THUMB32_INSTR) ||
1127 ((uint16_t)instr == FASTTRAP_THUMB32_RET_INSTR)) {
1128 if (dtrace_user_probe(state) == KERN_SUCCESS) {
1129 return;
1130 }
1131 }
1132 } else {
1133 if ((instr == FASTTRAP_ARM32_INSTR) ||
1134 (instr == FASTTRAP_ARM32_RET_INSTR)) {
1135 if (dtrace_user_probe(state) == KERN_SUCCESS) {
1136 return;
1137 }
1138 }
1139 }
1140 }
1141
1142 #endif /* CONFIG_DTRACE */
1143
1144 if (PSR64_IS_KERNEL(get_saved_state_cpsr(state))) {
1145 if (IS_ARM_GDB_TRAP(instr)) {
1146 boolean_t interrupt_state;
1147 exception = EXC_BREAKPOINT;
1148
1149 interrupt_state = ml_set_interrupts_enabled(FALSE);
1150
1151 /* Save off the context here (so that the debug logic
1152 * can see the original state of this thread).
1153 */
1154 current_thread()->machine.kpcb = state;
1155
1156 /* Hop into the debugger (typically either due to a
1157 * fatal exception, an explicit panic, or a stackshot
1158 * request.
1159 */
1160 DebuggerCall(exception, state);
1161
1162 current_thread()->machine.kpcb = NULL;
1163 (void) ml_set_interrupts_enabled(interrupt_state);
1164 return;
1165 } else {
1166 panic("Undefined kernel instruction: pc=%p instr=%x", (void*)get_saved_state_pc(state), instr);
1167 }
1168 }
1169
1170 /*
1171 * Check for GDB breakpoint via illegal opcode.
1172 */
1173 if (IS_ARM_GDB_TRAP(instr)) {
1174 exception = EXC_BREAKPOINT;
1175 codes[0] = EXC_ARM_BREAKPOINT;
1176 codes[1] = instr;
1177 } else {
1178 codes[1] = instr;
1179 }
1180
1181 exception_triage(exception, codes, numcodes);
1182 __builtin_unreachable();
1183 }
1184
1185 #if __has_feature(ptrauth_calls)
1186 static inline const char *
ptrauth_key_to_string(ptrauth_key key)1187 ptrauth_key_to_string(ptrauth_key key)
1188 {
1189 switch (key) {
1190 case ptrauth_key_asia:
1191 return "IA";
1192 case ptrauth_key_asib:
1193 return "IB";
1194 case ptrauth_key_asda:
1195 return "DA";
1196 case ptrauth_key_asdb:
1197 return "DB";
1198 default:
1199 __builtin_unreachable();
1200 }
1201 }
1202
1203 static const char *
ptrauth_handle_brk_trap(void * tstate,uint16_t comment)1204 ptrauth_handle_brk_trap(void *tstate, uint16_t comment)
1205 {
1206 kernel_panic_reason_t pr = PERCPU_GET(panic_reason);
1207 arm_saved_state_t *state = (arm_saved_state_t *)tstate;
1208
1209 ptrauth_key key = (ptrauth_key)(comment - PTRAUTH_TRAP_START);
1210 const char *key_str = ptrauth_key_to_string(key);
1211
1212 snprintf(pr->buf, sizeof(pr->buf),
1213 "Break 0x%04X instruction exception from kernel. "
1214 "Ptrauth failure with %s key resulted in 0x%016llx",
1215 comment, key_str, saved_state64(state)->x[16]);
1216
1217 return pr->buf;
1218 }
1219 #endif /* __has_feature(ptrauth_calls) */
1220
1221 #if HAS_TELEMETRY_KERNEL_BRK
1222 static uint32_t bound_chk_violations_event;
1223
1224 static const char *
xnu_soft_trap_handle_breakpoint(void * tstate,uint16_t comment)1225 xnu_soft_trap_handle_breakpoint(
1226 void *tstate,
1227 uint16_t comment)
1228 {
1229 #if CONFIG_UBSAN_MINIMAL
1230 if (comment == UBSAN_SOFT_TRAP_SIGNED_OF) {
1231 ubsan_handle_brk_trap(tstate, comment);
1232 }
1233 #else
1234 (void)tstate;
1235 #endif
1236
1237 if (comment == CLANG_SOFT_TRAP_BOUND_CHK) {
1238 os_atomic_inc(&bound_chk_violations_event, relaxed);
1239 }
1240 return NULL;
1241 }
1242 #endif /* HAS_TELEMETRY_KERNEL_BRK */
1243
1244 static const char *
xnu_hard_trap_handle_breakpoint(void * tstate,uint16_t comment)1245 xnu_hard_trap_handle_breakpoint(void *tstate, uint16_t comment)
1246 {
1247 kernel_panic_reason_t pr = PERCPU_GET(panic_reason);
1248 arm_saved_state64_t *state = saved_state64(tstate);
1249
1250 switch (comment) {
1251 case XNU_HARD_TRAP_SAFE_UNLINK:
1252 snprintf(pr->buf, sizeof(pr->buf),
1253 "panic: corrupt list around element %p",
1254 (void *)state->x[8]);
1255 return pr->buf;
1256
1257 case XNU_HARD_TRAP_STRING_CHK:
1258 return "panic: string operation caused an overflow";
1259
1260 case XNU_HARD_TRAP_ASSERT_FAILURE:
1261 /*
1262 * Read the implicit assert arguments, see:
1263 * ML_TRAP_REGISTER_1: x8
1264 * ML_TRAP_REGISTER_2: x16
1265 * ML_TRAP_REGISTER_3: x17
1266 */
1267 panic_assert_format(pr->buf, sizeof(pr->buf),
1268 (struct mach_assert_hdr *)state->x[8],
1269 state->x[16], state->x[17]);
1270 return pr->buf;
1271
1272 default:
1273 return NULL;
1274 }
1275 }
1276
1277 #if __has_feature(ptrauth_calls)
1278 KERNEL_BRK_DESCRIPTOR_DEFINE(ptrauth_desc,
1279 .type = TRAP_TELEMETRY_TYPE_KERNEL_BRK_PTRAUTH,
1280 .base = PTRAUTH_TRAP_START,
1281 .max = PTRAUTH_TRAP_END,
1282 .options = BRK_TELEMETRY_OPTIONS_FATAL_DEFAULT,
1283 .handle_breakpoint = ptrauth_handle_brk_trap);
1284 #endif
1285
1286 KERNEL_BRK_DESCRIPTOR_DEFINE(clang_desc,
1287 .type = TRAP_TELEMETRY_TYPE_KERNEL_BRK_CLANG,
1288 .base = CLANG_ARM_TRAP_START,
1289 .max = CLANG_ARM_TRAP_END,
1290 .options = BRK_TELEMETRY_OPTIONS_FATAL_DEFAULT,
1291 .handle_breakpoint = NULL);
1292
1293 KERNEL_BRK_DESCRIPTOR_DEFINE(libcxx_desc,
1294 .type = TRAP_TELEMETRY_TYPE_KERNEL_BRK_LIBCXX,
1295 .base = LIBCXX_TRAP_START,
1296 .max = LIBCXX_TRAP_END,
1297 .options = BRK_TELEMETRY_OPTIONS_FATAL_DEFAULT,
1298 .handle_breakpoint = NULL);
1299
1300 #if HAS_TELEMETRY_KERNEL_BRK
1301 KERNEL_BRK_DESCRIPTOR_DEFINE(xnu_soft_traps_desc,
1302 .type = TRAP_TELEMETRY_TYPE_KERNEL_BRK_TELEMETRY,
1303 .base = XNU_SOFT_TRAP_START,
1304 .max = XNU_SOFT_TRAP_END,
1305 .options = BRK_TELEMETRY_OPTIONS_RECOVERABLE_DEFAULT(
1306 /* enable_telemetry */ true),
1307 .handle_breakpoint = xnu_soft_trap_handle_breakpoint);
1308 #endif /* HAS_TELEMETRY_KERNEL_BRK */
1309
1310 KERNEL_BRK_DESCRIPTOR_DEFINE(xnu_hard_traps_desc,
1311 .type = TRAP_TELEMETRY_TYPE_KERNEL_BRK_XNU,
1312 .base = XNU_HARD_TRAP_START,
1313 .max = XNU_HARD_TRAP_END,
1314 .options = BRK_TELEMETRY_OPTIONS_FATAL_DEFAULT,
1315 .handle_breakpoint = xnu_hard_trap_handle_breakpoint);
1316
1317 static void
1318 #if !HAS_TELEMETRY_KERNEL_BRK
1319 __attribute__((noreturn))
1320 #endif
handle_kernel_breakpoint(arm_saved_state_t * state,uint64_t esr)1321 handle_kernel_breakpoint(arm_saved_state_t *state, uint64_t esr)
1322 {
1323 uint16_t comment = ISS_BRK_COMMENT(esr);
1324 const struct kernel_brk_descriptor *desc;
1325 const char *msg = NULL;
1326
1327 desc = find_kernel_brk_descriptor_by_comment(comment);
1328
1329 if (!desc) {
1330 goto brk_out;
1331 }
1332
1333 #if HAS_TELEMETRY_KERNEL_BRK
1334 if (desc->options.enable_trap_telemetry) {
1335 trap_telemetry_report_exception(
1336 /* trap_type */ desc->type,
1337 /* trap_code */ comment,
1338 /* options */ desc->options.telemetry_options,
1339 /* saved_state */ (void *)state);
1340 }
1341 #endif
1342
1343 if (desc->handle_breakpoint) {
1344 msg = desc->handle_breakpoint(state, comment);
1345 }
1346
1347 #if HAS_TELEMETRY_KERNEL_BRK
1348 /* Still alive? Check if we should recover. */
1349 if (desc->options.recoverable) {
1350 add_saved_state_pc(state, 4);
1351 return;
1352 }
1353 #endif
1354
1355 brk_out:
1356 if (msg == NULL) {
1357 kernel_panic_reason_t pr = PERCPU_GET(panic_reason);
1358
1359 msg = tsnprintf(pr->buf, sizeof(pr->buf),
1360 "Break 0x%04X instruction exception from kernel. "
1361 "Panic (by design)",
1362 comment);
1363 }
1364
1365 panic_with_thread_kernel_state(msg, state);
1366 __builtin_unreachable();
1367 #undef MSG_FMT
1368 }
1369
1370 /*
1371 * Similar in spirit to kernel_brk_descriptor, but with less flexible semantics:
1372 * each descriptor defines a `brk` label range for use from userspace.
1373 * When used, system policy may decide to kill the calling process without giving them opportunity to
1374 * catch the exception or continue execution from a signal handler.
1375 * This is used to enforce security boundaries: userspace code may use this mechanism
1376 * to reliably terminate when internal inconsistencies are detected.
1377 * Note that we don't invariably terminate without giving the process a say: we might only enforce
1378 * such a policy if a security feature is enabled, for example.
1379 */
1380 typedef struct user_brk_label_range_descriptor {
1381 uint16_t base;
1382 uint16_t max;
1383 } user_brk_label_range_descriptor_t;
1384
1385 const user_brk_label_range_descriptor_t user_brk_descriptors[] = {
1386 #if __has_feature(ptrauth_calls)
1387 /* PAC failures detected in data by userspace */
1388 {
1389 /* Use the exact same label range as kernel PAC */
1390 .base = PTRAUTH_TRAP_START,
1391 .max = PTRAUTH_TRAP_END,
1392 },
1393 #endif /* __has_feature(ptrauth_calls) */
1394 /* Available for use by system libraries when detecting disallowed conditions */
1395 {
1396 /* Note this uses the same range as the kernel-specific XNU_HARD_TRAP range */
1397 .base = 0xB000,
1398 .max = 0xBFFF,
1399 }
1400 };
1401 const int user_brk_descriptor_count = sizeof(user_brk_descriptors) / sizeof(user_brk_descriptors[0]);
1402
1403 const static inline user_brk_label_range_descriptor_t *
find_user_brk_descriptor_by_comment(uint16_t comment)1404 find_user_brk_descriptor_by_comment(uint16_t comment)
1405 {
1406 for (int desc_idx = 0; desc_idx < user_brk_descriptor_count; desc_idx++) {
1407 const user_brk_label_range_descriptor_t* des = &user_brk_descriptors[desc_idx];
1408 if (comment >= des->base && comment <= des->max) {
1409 return des;
1410 }
1411 }
1412
1413 return NULL;
1414 }
1415
1416 static void
handle_user_breakpoint(arm_saved_state_t * state,uint64_t esr __unused)1417 handle_user_breakpoint(arm_saved_state_t *state, uint64_t esr __unused)
1418 {
1419 exception_type_t exception = EXC_BREAKPOINT;
1420 mach_exception_data_type_t codes[2] = {EXC_ARM_BREAKPOINT};
1421 mach_msg_type_number_t numcodes = 2;
1422
1423 if (ESR_EC(esr) == ESR_EC_BRK_AARCH64) {
1424 /*
1425 * Consult the trap labels we know about to decide whether userspace
1426 * should be given the opportunity to handle the exception.
1427 */
1428 uint16_t brk_label = ISS_BRK_COMMENT(esr);
1429 const struct user_brk_label_range_descriptor* descriptor = find_user_brk_descriptor_by_comment(brk_label);
1430 /*
1431 * Note it's no problem if we don't recognize the label.
1432 * In this case we'll just go through normal exception delivery.
1433 */
1434 if (descriptor != NULL) {
1435 exception |= EXC_MAY_BE_UNRECOVERABLE_BIT;
1436
1437 #if __has_feature(ptrauth_calls)
1438 /*
1439 * We have additional policy specifically for PAC violations.
1440 * To make the rest of the code easier to follow, don't set
1441 * EXC_MAY_BE_UNRECOVERABLE_BIT here and just set EXC_PTRAUTH_BIT instead.
1442 * Conceptually a PAC failure is absolutely 'maybe unrecoverable', but it's
1443 * not really worth excising the discrepency from the plumbing.
1444 */
1445 if (descriptor->base == PTRAUTH_TRAP_START) {
1446 exception &= ~(EXC_MAY_BE_UNRECOVERABLE_BIT);
1447 exception |= EXC_PTRAUTH_BIT;
1448 }
1449 #endif /* __has_feature(ptrauth_calls) */
1450 }
1451 }
1452
1453 codes[1] = get_saved_state_pc(state);
1454 exception_triage(exception, codes, numcodes);
1455 __builtin_unreachable();
1456 }
1457
1458 static void
handle_watchpoint(vm_offset_t fault_addr)1459 handle_watchpoint(vm_offset_t fault_addr)
1460 {
1461 exception_type_t exception = EXC_BREAKPOINT;
1462 mach_exception_data_type_t codes[2] = {EXC_ARM_DA_DEBUG};
1463 mach_msg_type_number_t numcodes = 2;
1464
1465 codes[1] = fault_addr;
1466 exception_triage(exception, codes, numcodes);
1467 __builtin_unreachable();
1468 }
1469
1470 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)1471 handle_abort(arm_saved_state_t *state, uint64_t esr, vm_offset_t fault_addr,
1472 abort_inspector_t inspect_abort, abort_handler_t handler, expected_fault_handler_t expected_fault_handler)
1473 {
1474 fault_status_t fault_code;
1475 vm_prot_t fault_type;
1476
1477 inspect_abort(ESR_ISS(esr), &fault_code, &fault_type);
1478 handler(state, esr, fault_addr, fault_code, fault_type, expected_fault_handler);
1479 }
1480
1481 static void
inspect_instruction_abort(uint32_t iss,fault_status_t * fault_code,vm_prot_t * fault_type)1482 inspect_instruction_abort(uint32_t iss, fault_status_t *fault_code, vm_prot_t *fault_type)
1483 {
1484 getCpuDatap()->cpu_stat.instr_ex_cnt++;
1485 *fault_code = ISS_IA_FSC(iss);
1486 *fault_type = (VM_PROT_READ | VM_PROT_EXECUTE);
1487 }
1488
1489 static void
inspect_data_abort(uint32_t iss,fault_status_t * fault_code,vm_prot_t * fault_type)1490 inspect_data_abort(uint32_t iss, fault_status_t *fault_code, vm_prot_t *fault_type)
1491 {
1492 getCpuDatap()->cpu_stat.data_ex_cnt++;
1493 *fault_code = ISS_DA_FSC(iss);
1494
1495 /*
1496 * Cache maintenance operations always report faults as write access.
1497 * Change these to read access, unless they report a permission fault.
1498 * Only certain cache maintenance operations (e.g. 'dc ivac') require write
1499 * access to the mapping, but if a cache maintenance operation that only requires
1500 * read access generates a permission fault, then we will not be able to handle
1501 * the fault regardless of whether we treat it as a read or write fault.
1502 */
1503 if ((iss & ISS_DA_WNR) && (!(iss & ISS_DA_CM) || is_permission_fault(*fault_code))) {
1504 *fault_type = (VM_PROT_READ | VM_PROT_WRITE);
1505 } else {
1506 *fault_type = (VM_PROT_READ);
1507 }
1508 }
1509
1510 #if __has_feature(ptrauth_calls)
1511 static inline uint64_t
fault_addr_bitmask(unsigned int bit_from,unsigned int bit_to)1512 fault_addr_bitmask(unsigned int bit_from, unsigned int bit_to)
1513 {
1514 return ((1ULL << (bit_to - bit_from + 1)) - 1) << bit_from;
1515 }
1516
1517 static inline bool
fault_addr_bit(vm_offset_t fault_addr,unsigned int bit)1518 fault_addr_bit(vm_offset_t fault_addr, unsigned int bit)
1519 {
1520 return (bool)((fault_addr >> bit) & 1);
1521 }
1522
1523 extern int gARM_FEAT_FPAC;
1524 extern int gARM_FEAT_FPACCOMBINE;
1525 extern int gARM_FEAT_PAuth2;
1526
1527 /**
1528 * Determines whether a fault address taken at EL0 contains a PAC error code
1529 * corresponding to the specified kind of ptrauth key.
1530 */
1531 static bool
user_fault_matches_pac_error_code(vm_offset_t fault_addr,uint64_t pc,bool data_key)1532 user_fault_matches_pac_error_code(vm_offset_t fault_addr, uint64_t pc, bool data_key)
1533 {
1534 if (gARM_FEAT_FPACCOMBINE) {
1535 /*
1536 * CPUs with FPACCOMBINE always raise PAC Fail exceptions during
1537 * PAC failure. If the CPU took any other kind of exception, we
1538 * can rule out PAC as the root cause.
1539 */
1540 return false;
1541 }
1542
1543 if (data_key && gARM_FEAT_FPAC) {
1544 uint32_t instr;
1545 int err = copyin(pc, (char *)&instr, sizeof(instr));
1546 if (!err && !ARM64_INSTR_IS_LDRAx(instr)) {
1547 /*
1548 * On FPAC-enabled devices, PAC failure can only cause
1549 * data aborts during "combined" LDRAx instructions. If
1550 * PAC fails during a discrete AUTxx + LDR/STR
1551 * instruction sequence, then the AUTxx instruction
1552 * raises a PAC Fail exception rather than poisoning its
1553 * output address.
1554 *
1555 * In principle the same logic applies to instruction
1556 * aborts. But we have no way to identify the exact
1557 * instruction that caused the abort, so we can't tell
1558 * if it was a combined branch + auth instruction.
1559 */
1560 return false;
1561 }
1562 }
1563
1564 bool instruction_tbi = !(get_tcr() & TCR_TBID0_TBI_DATA_ONLY);
1565 bool tbi = data_key || __improbable(instruction_tbi);
1566
1567 if (gARM_FEAT_PAuth2) {
1568 /*
1569 * EnhancedPAC2 CPUs don't encode error codes at fixed positions, so
1570 * treat all non-canonical address bits like potential poison bits.
1571 */
1572 uint64_t mask = fault_addr_bitmask(64 - T0SZ_BOOT, 54);
1573 if (!tbi) {
1574 mask |= fault_addr_bitmask(56, 63);
1575 }
1576 return (fault_addr & mask) != 0;
1577 } else {
1578 unsigned int poison_shift;
1579 if (tbi) {
1580 poison_shift = 53;
1581 } else {
1582 poison_shift = 61;
1583 }
1584
1585 /* PAC error codes are always in the form key_number:NOT(key_number) */
1586 bool poison_bit_1 = fault_addr_bit(fault_addr, poison_shift);
1587 bool poison_bit_2 = fault_addr_bit(fault_addr, poison_shift + 1);
1588 return poison_bit_1 != poison_bit_2;
1589 }
1590 }
1591 #endif /* __has_feature(ptrauth_calls) */
1592
1593 /**
1594 * Determines whether the userland thread has a JIT region in RW mode, TPRO
1595 * in RW mode, or JCTL_EL0 in pointer signing mode. A fault in any of these trusted
1596 * code paths may indicate an attack on WebKit. Rather than letting a
1597 * potentially-compromised process try to handle the exception, it will be killed
1598 * by the kernel and a crash report will be generated.
1599 */
1600 static bool
user_fault_in_self_restrict_mode(thread_t thread __unused)1601 user_fault_in_self_restrict_mode(thread_t thread __unused)
1602 {
1603
1604 return false;
1605 }
1606
1607 static void
handle_pc_align(arm_saved_state_t * ss)1608 handle_pc_align(arm_saved_state_t *ss)
1609 {
1610 exception_type_t exc;
1611 mach_exception_data_type_t codes[2];
1612 mach_msg_type_number_t numcodes = 2;
1613
1614 if (!PSR64_IS_USER(get_saved_state_cpsr(ss))) {
1615 panic_with_thread_kernel_state("PC alignment exception from kernel.", ss);
1616 }
1617
1618 exc = EXC_BAD_ACCESS;
1619 #if __has_feature(ptrauth_calls)
1620 uint64_t pc = get_saved_state_pc(ss);
1621 if (user_fault_matches_pac_error_code(pc, pc, false)) {
1622 exc |= EXC_PTRAUTH_BIT;
1623 }
1624 #endif /* __has_feature(ptrauth_calls) */
1625
1626 codes[0] = EXC_ARM_DA_ALIGN;
1627 codes[1] = get_saved_state_pc(ss);
1628
1629 exception_triage(exc, codes, numcodes);
1630 __builtin_unreachable();
1631 }
1632
1633 static void
handle_sp_align(arm_saved_state_t * ss)1634 handle_sp_align(arm_saved_state_t *ss)
1635 {
1636 exception_type_t exc;
1637 mach_exception_data_type_t codes[2];
1638 mach_msg_type_number_t numcodes = 2;
1639
1640 if (!PSR64_IS_USER(get_saved_state_cpsr(ss))) {
1641 panic_with_thread_kernel_state("SP alignment exception from kernel.", ss);
1642 }
1643
1644 exc = EXC_BAD_ACCESS;
1645 #if __has_feature(ptrauth_calls)
1646 if (user_fault_matches_pac_error_code(get_saved_state_sp(ss), get_saved_state_pc(ss), true)) {
1647 exc |= EXC_PTRAUTH_BIT;
1648 }
1649 #endif /* __has_feature(ptrauth_calls) */
1650
1651 codes[0] = EXC_ARM_SP_ALIGN;
1652 codes[1] = get_saved_state_sp(ss);
1653
1654 exception_triage(exc, codes, numcodes);
1655 __builtin_unreachable();
1656 }
1657
1658 static void
handle_wf_trap(arm_saved_state_t * state)1659 handle_wf_trap(arm_saved_state_t *state)
1660 {
1661 exception_type_t exc;
1662 mach_exception_data_type_t codes[2];
1663 mach_msg_type_number_t numcodes = 2;
1664 uint32_t instr = 0;
1665
1666 COPYIN(get_saved_state_pc(state), (char *)&instr, sizeof(instr));
1667
1668 exc = EXC_BAD_INSTRUCTION;
1669 codes[0] = EXC_ARM_UNDEFINED;
1670 codes[1] = instr;
1671
1672 exception_triage(exc, codes, numcodes);
1673 __builtin_unreachable();
1674 }
1675
1676 static void
handle_fp_trap(arm_saved_state_t * state,uint64_t esr)1677 handle_fp_trap(arm_saved_state_t *state, uint64_t esr)
1678 {
1679 exception_type_t exc = EXC_ARITHMETIC;
1680 mach_exception_data_type_t codes[2];
1681 mach_msg_type_number_t numcodes = 2;
1682 uint32_t instr = 0;
1683
1684 if (PSR64_IS_KERNEL(get_saved_state_cpsr(state))) {
1685 panic_with_thread_kernel_state("Floating point exception from kernel", state);
1686 }
1687
1688 COPYIN(get_saved_state_pc(state), (char *)&instr, sizeof(instr));
1689 codes[1] = instr;
1690
1691 /* The floating point trap flags are only valid if TFV is set. */
1692 if (!fp_exceptions_enabled) {
1693 exc = EXC_BAD_INSTRUCTION;
1694 codes[0] = EXC_ARM_UNDEFINED;
1695 } else if (!(esr & ISS_FP_TFV)) {
1696 codes[0] = EXC_ARM_FP_UNDEFINED;
1697 } else if (esr & ISS_FP_UFF) {
1698 codes[0] = EXC_ARM_FP_UF;
1699 } else if (esr & ISS_FP_OFF) {
1700 codes[0] = EXC_ARM_FP_OF;
1701 } else if (esr & ISS_FP_IOF) {
1702 codes[0] = EXC_ARM_FP_IO;
1703 } else if (esr & ISS_FP_DZF) {
1704 codes[0] = EXC_ARM_FP_DZ;
1705 } else if (esr & ISS_FP_IDF) {
1706 codes[0] = EXC_ARM_FP_ID;
1707 } else if (esr & ISS_FP_IXF) {
1708 codes[0] = EXC_ARM_FP_IX;
1709 } else {
1710 panic("Unrecognized floating point exception, state=%p, esr=%#llx", state, esr);
1711 }
1712
1713 exception_triage(exc, codes, numcodes);
1714 __builtin_unreachable();
1715 }
1716
1717
1718
1719 /*
1720 * handle_alignment_fault_from_user:
1721 * state: Saved state
1722 *
1723 * Attempts to deal with an alignment fault from userspace (possibly by
1724 * emulating the faulting instruction). If emulation failed due to an
1725 * unservicable fault, the ESR for that fault will be stored in the
1726 * recovery_esr field of the thread by the exception code.
1727 *
1728 * Returns:
1729 * -1: Emulation failed (emulation of state/instr not supported)
1730 * 0: Successfully emulated the instruction
1731 * EFAULT: Emulation failed (probably due to permissions)
1732 * EINVAL: Emulation failed (probably due to a bad address)
1733 */
1734
1735
1736 static int
handle_alignment_fault_from_user(arm_saved_state_t * state,kern_return_t * vmfr)1737 handle_alignment_fault_from_user(arm_saved_state_t *state, kern_return_t *vmfr)
1738 {
1739 int ret = -1;
1740
1741 #pragma unused (state)
1742 #pragma unused (vmfr)
1743
1744 return ret;
1745 }
1746
1747
1748
1749 #if HAS_ARM_FEAT_SME
1750 static void
handle_sme_trap(arm_saved_state_t * state,uint64_t esr)1751 handle_sme_trap(arm_saved_state_t *state, uint64_t esr)
1752 {
1753 exception_type_t exc = EXC_BAD_INSTRUCTION;
1754 mach_exception_data_type_t codes[2] = {EXC_ARM_UNDEFINED};
1755 mach_msg_type_number_t numcodes = 2;
1756
1757 if (!PSR64_IS_USER(get_saved_state_cpsr(state))) {
1758 panic("SME exception from kernel, state=%p, esr=%#llx", state, esr);
1759 }
1760 if (!arm_sme_version()) {
1761 /*
1762 * If SME is disabled in software but userspace executes an SME
1763 * instruction anyway, then the CPU will still raise an
1764 * SME-specific trap. Triage it as if the CPU raised an
1765 * undefined-instruction trap.
1766 */
1767 exception_triage(exc, codes, numcodes);
1768 __builtin_unreachable();
1769 }
1770
1771 if (ISS_SME_SMTC(ESR_ISS(esr)) == ISS_SME_SMTC_CAPCR) {
1772 thread_t thread = current_thread();
1773 switch (machine_thread_sme_state_alloc(thread)) {
1774 case KERN_SUCCESS:
1775 return;
1776
1777
1778 default:
1779 panic("Failed to allocate SME state for thread %p", thread);
1780 }
1781 }
1782
1783 uint32_t instr;
1784 COPYIN(get_saved_state_pc(state), (char *)&instr, sizeof(instr));
1785 codes[1] = instr;
1786
1787 exception_triage(exc, codes, numcodes);
1788 __builtin_unreachable();
1789 }
1790 #endif /* HAS_ARM_FEAT_SME */
1791
1792 static void
handle_sw_step_debug(arm_saved_state_t * state)1793 handle_sw_step_debug(arm_saved_state_t *state)
1794 {
1795 thread_t thread = current_thread();
1796 exception_type_t exc;
1797 mach_exception_data_type_t codes[2];
1798 mach_msg_type_number_t numcodes = 2;
1799
1800 if (!PSR64_IS_USER(get_saved_state_cpsr(state))) {
1801 panic_with_thread_kernel_state("SW_STEP_DEBUG exception from kernel.", state);
1802 }
1803
1804 // Disable single step and unmask interrupts (in the saved state, anticipating next exception return)
1805 if (thread->machine.DebugData != NULL) {
1806 thread->machine.DebugData->uds.ds64.mdscr_el1 &= ~0x1;
1807 } else {
1808 panic_with_thread_kernel_state("SW_STEP_DEBUG exception thread DebugData is NULL.", state);
1809 }
1810
1811 mask_user_saved_state_cpsr(thread->machine.upcb, 0, PSR64_SS | DAIF_ALL);
1812
1813 // Special encoding for gdb single step event on ARM
1814 exc = EXC_BREAKPOINT;
1815 codes[0] = 1;
1816 codes[1] = 0;
1817
1818 exception_triage(exc, codes, numcodes);
1819 __builtin_unreachable();
1820 }
1821
1822 #if MACH_ASSERT
1823 TUNABLE_WRITEABLE(int, panic_on_jit_guard, "panic_on_jit_guard", 0);
1824 #endif /* MACH_ASSERT */
1825
1826
1827 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)1828 handle_user_abort(arm_saved_state_t *state, uint64_t esr, vm_offset_t fault_addr,
1829 fault_status_t fault_code, vm_prot_t fault_type, expected_fault_handler_t expected_fault_handler)
1830 {
1831 exception_type_t exc = EXC_BAD_ACCESS;
1832 mach_exception_data_type_t codes[2];
1833 mach_msg_type_number_t numcodes = 2;
1834 thread_t thread = current_thread();
1835
1836 (void)expected_fault_handler;
1837
1838 if (__improbable(!SPSR_INTERRUPTS_ENABLED(get_saved_state_cpsr(state)))) {
1839 panic_with_thread_kernel_state("User abort from non-interruptible context", state);
1840 }
1841
1842 thread->iotier_override = THROTTLE_LEVEL_NONE; /* Reset IO tier override before handling abort from userspace */
1843
1844 if (!is_servicible_fault(fault_code, esr) &&
1845 thread->t_rr_state.trr_fault_state != TRR_FAULT_NONE) {
1846 thread_reset_pcs_done_faulting(thread);
1847 }
1848
1849 if (is_vm_fault(fault_code)) {
1850 vm_map_t map = thread->map;
1851 vm_offset_t vm_fault_addr = fault_addr;
1852 kern_return_t result = KERN_FAILURE;
1853
1854 assert(map != kernel_map);
1855
1856 if (!(fault_type & VM_PROT_EXECUTE)) {
1857 vm_fault_addr = VM_USER_STRIP_TBI(fault_addr);
1858 }
1859
1860 /* check to see if it is just a pmap ref/modify fault */
1861 if (!is_translation_fault(fault_code)) {
1862 result = arm_fast_fault(map->pmap,
1863 vm_fault_addr,
1864 fault_type, (fault_code == FSC_ACCESS_FLAG_FAULT_L3), TRUE);
1865 }
1866 if (result != KERN_SUCCESS) {
1867
1868 {
1869 /* We have to fault the page in */
1870 result = vm_fault(map, vm_fault_addr, fault_type,
1871 /* change_wiring */ FALSE, VM_KERN_MEMORY_NONE, THREAD_ABORTSAFE,
1872 /* caller_pmap */ NULL, /* caller_pmap_addr */ 0);
1873 }
1874 }
1875 if (thread->t_rr_state.trr_fault_state != TRR_FAULT_NONE) {
1876 thread_reset_pcs_done_faulting(thread);
1877 }
1878 if (result == KERN_SUCCESS || result == KERN_ABORTED) {
1879 return;
1880 }
1881
1882 /*
1883 * vm_fault() should never return KERN_FAILURE for page faults from user space.
1884 * If it does, we're leaking preemption disables somewhere in the kernel.
1885 */
1886 if (__improbable(result == KERN_FAILURE)) {
1887 panic("vm_fault() KERN_FAILURE from user fault on thread %p", thread);
1888 }
1889
1890 codes[0] = result;
1891 } else if (is_alignment_fault(fault_code)) {
1892 kern_return_t vmfkr = KERN_SUCCESS;
1893 thread->machine.recover_esr = 0;
1894 thread->machine.recover_far = 0;
1895 int result = handle_alignment_fault_from_user(state, &vmfkr);
1896 if (result == 0) {
1897 /* Successfully emulated, or instruction
1898 * copyin() for decode/emulation failed.
1899 * Continue, or redrive instruction.
1900 */
1901 thread_exception_return();
1902 } else if (((result == EFAULT) || (result == EINVAL)) &&
1903 (thread->machine.recover_esr == 0)) {
1904 /*
1905 * If we didn't actually take a fault, but got one of
1906 * these errors, then we failed basic sanity checks of
1907 * the fault address. Treat this as an invalid
1908 * address.
1909 */
1910 codes[0] = KERN_INVALID_ADDRESS;
1911 } else if ((result == EFAULT) &&
1912 (thread->machine.recover_esr)) {
1913 /*
1914 * Since alignment aborts are prioritized
1915 * ahead of translation aborts, the misaligned
1916 * atomic emulation flow may have triggered a
1917 * VM pagefault, which the VM could not resolve.
1918 * Report the VM fault error in codes[]
1919 */
1920
1921 codes[0] = vmfkr;
1922 assertf(vmfkr != KERN_SUCCESS, "Unexpected vmfkr 0x%x", vmfkr);
1923 /* Cause ESR_EC to reflect an EL0 abort */
1924 thread->machine.recover_esr &= ~ESR_EC_MASK;
1925 thread->machine.recover_esr |= (ESR_EC_DABORT_EL0 << ESR_EC_SHIFT);
1926 set_saved_state_esr(thread->machine.upcb, thread->machine.recover_esr);
1927 set_saved_state_far(thread->machine.upcb, thread->machine.recover_far);
1928 fault_addr = thread->machine.recover_far;
1929 } else {
1930 /* This was just an unsupported alignment
1931 * exception. Misaligned atomic emulation
1932 * timeouts fall in this category.
1933 */
1934 codes[0] = EXC_ARM_DA_ALIGN;
1935 }
1936 } else if (is_parity_error(fault_code)) {
1937 #if defined(APPLE_ARM64_ARCH_FAMILY)
1938 /*
1939 * Platform errors are handled in sleh_sync before interrupts are enabled.
1940 */
1941 #else
1942 panic("User parity error.");
1943 #endif
1944 } else {
1945 codes[0] = KERN_FAILURE;
1946 }
1947
1948 #if CODE_SIGNING_MONITOR
1949 /*
1950 * If the code reaches here, it means we weren't able to resolve the fault and we're
1951 * going to be sending the task an exception. On systems which have the code signing
1952 * monitor enabled, an execute fault which cannot be handled must result in sending
1953 * a SIGKILL to the task.
1954 */
1955 if (is_vm_fault(fault_code) && (fault_type & VM_PROT_EXECUTE)) {
1956 csm_code_signing_violation(current_proc(), fault_addr);
1957 }
1958 #endif
1959
1960 codes[1] = fault_addr;
1961 #if __has_feature(ptrauth_calls)
1962 bool is_data_abort = (ESR_EC(esr) == ESR_EC_DABORT_EL0);
1963 if (user_fault_matches_pac_error_code(fault_addr, get_saved_state_pc(state), is_data_abort)) {
1964 exc |= EXC_PTRAUTH_BIT;
1965 }
1966 #endif /* __has_feature(ptrauth_calls) */
1967
1968 if (user_fault_in_self_restrict_mode(thread) &&
1969 task_is_jit_exception_fatal(get_threadtask(thread))) {
1970 int flags = PX_KTRIAGE;
1971 exception_info_t info = {
1972 .os_reason = OS_REASON_SELF_RESTRICT,
1973 .exception_type = exc,
1974 .mx_code = codes[0],
1975 .mx_subcode = codes[1]
1976 };
1977
1978 #if MACH_ASSERT
1979 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",
1980 exc, codes[0], codes[1], thread->syscalls_unix, current_task(), thread, fault_addr, fault_code, fault_type, esr);
1981 if (panic_on_jit_guard &&
1982 current_task()->thread_count == 1 &&
1983 thread->syscalls_unix < 24) {
1984 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)",
1985 exc, codes[0], codes[1], thread->syscalls_unix, current_task(), thread, fault_addr, fault_code, fault_type, esr, state,
1986 0, 0, 0ull, 0ull,
1987 0ull, 0ull
1988 );
1989 }
1990 #endif /* MACH_ASSERT */
1991
1992 exit_with_mach_exception(current_proc(), info, flags);
1993 }
1994
1995
1996 exception_triage(exc, codes, numcodes);
1997 __builtin_unreachable();
1998 }
1999
2000 /**
2001 * Panic because the kernel abort handler tried to apply a recovery handler that
2002 * isn't inside copyio_recover_table[].
2003 *
2004 * @param state original saved-state
2005 * @param recover invalid recovery handler
2006 */
2007 __attribute__((noreturn, used))
2008 static void
panic_on_invalid_recovery_handler(arm_saved_state_t * state,struct copyio_recovery_entry * recover)2009 panic_on_invalid_recovery_handler(arm_saved_state_t *state, struct copyio_recovery_entry *recover)
2010 {
2011 panic("attempt to set invalid recovery handler %p on kernel saved-state %p", recover, state);
2012 }
2013
2014 /**
2015 * Update a thread saved-state to store an error code in x0 and branch to a
2016 * copyio recovery handler.
2017 *
2018 * @param state original saved-state
2019 * @param esr ESR_ELx value for the fault taken
2020 * @param fault_addr FAR_ELx value for the fault taken
2021 * @param thread target thread
2022 * @param recover destination copyio recovery handler
2023 * @param x0 error code to populate into x0
2024 */
2025 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)2026 handle_kernel_abort_recover_with_error_code(
2027 arm_saved_state_t *state,
2028 uint64_t esr,
2029 vm_offset_t fault_addr,
2030 thread_t thread,
2031 struct copyio_recovery_entry *_Nonnull recover,
2032 uint64_t x0)
2033 {
2034 thread->machine.recover_esr = esr;
2035 thread->machine.recover_far = fault_addr;
2036 saved_state64(state)->x[0] = x0;
2037 #if defined(HAS_APPLE_PAC)
2038 MANIPULATE_SIGNED_THREAD_STATE(state,
2039 "adrp x6, _copyio_recover_table_end@page \n"
2040 "add x6, x6, _copyio_recover_table_end@pageoff \n"
2041 "cmp %[recover], x6 \n"
2042 "b.lt 1f \n"
2043 "bl _panic_on_invalid_recovery_handler \n"
2044 "brk #0 \n"
2045 "1: \n"
2046 "adrp x6, _copyio_recover_table@page \n"
2047 "add x6, x6, _copyio_recover_table@pageoff \n"
2048 "subs x7, %[recover], x6 \n"
2049 "b.pl 1f \n"
2050 "bl _panic_on_invalid_recovery_handler \n"
2051 "brk #0 \n"
2052 "1: \n"
2053 "udiv x8, x7, %[SIZEOF_RECOVER] \n"
2054 "mul x8, x8, %[SIZEOF_RECOVER] \n"
2055 "cmp x7, x8 \n"
2056 "b.eq 1f \n"
2057 "bl _panic_on_invalid_recovery_handler \n"
2058 "brk #0 \n"
2059 "1: \n"
2060 "ldr x1, [%[recover], %[CRE_RECOVERY]] \n"
2061 "add x1, x1, x6 \n"
2062 "str x1, [x0, %[SS64_PC]] \n",
2063 [recover] "r"(recover),
2064 [SIZEOF_RECOVER] "r"((sizeof(*recover))),
2065 [CRE_RECOVERY] "i"(offsetof(struct copyio_recovery_entry, cre_recovery))
2066 );
2067 #else
2068 ptrdiff_t recover_offset = (uintptr_t)recover - (uintptr_t)copyio_recover_table;
2069 if ((uintptr_t)recover < (uintptr_t)copyio_recover_table ||
2070 (uintptr_t)recover >= (uintptr_t)copyio_recover_table_end ||
2071 (recover_offset % sizeof(*recover)) != 0) {
2072 panic_on_invalid_recovery_handler(state, recover);
2073 }
2074 saved_state64(state)->pc = copyio_recovery_addr(recover->cre_recovery);
2075 #endif
2076 }
2077
2078 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)2079 handle_kernel_abort_recover(
2080 arm_saved_state_t *state,
2081 uint64_t esr,
2082 vm_offset_t fault_addr,
2083 thread_t thread,
2084 struct copyio_recovery_entry *_Nonnull recover)
2085 {
2086 handle_kernel_abort_recover_with_error_code(state, esr, fault_addr, thread, recover, EFAULT);
2087 }
2088
2089
2090 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)2091 handle_kernel_abort(arm_saved_state_t *state, uint64_t esr, vm_offset_t fault_addr,
2092 fault_status_t fault_code, vm_prot_t fault_type, expected_fault_handler_t expected_fault_handler)
2093 {
2094 thread_t thread = current_thread();
2095 struct copyio_recovery_entry *recover = find_copyio_recovery_entry(
2096 get_saved_state_pc(state));
2097
2098 #ifndef CONFIG_XNUPOST
2099 (void)expected_fault_handler;
2100 #endif /* CONFIG_XNUPOST */
2101
2102 #if CONFIG_DTRACE
2103 if (is_vm_fault(fault_code) && thread->t_dtrace_inprobe) { /* Executing under dtrace_probe? */
2104 if (dtrace_tally_fault(fault_addr)) { /* Should a fault under dtrace be ignored? */
2105 /*
2106 * Point to next instruction, or recovery handler if set.
2107 */
2108 if (recover) {
2109 handle_kernel_abort_recover(state, esr, VM_USER_STRIP_PTR(fault_addr), thread, recover);
2110 } else {
2111 add_saved_state_pc(state, 4);
2112 }
2113 return;
2114 } else {
2115 panic_with_thread_kernel_state("Unexpected page fault under dtrace_probe", state);
2116 }
2117 }
2118 #endif
2119
2120 if (is_vm_fault(fault_code)) {
2121 kern_return_t result = KERN_FAILURE;
2122 vm_map_t map;
2123 int interruptible;
2124
2125 #ifdef CONFIG_XNUPOST
2126 if (expected_fault_handler && expected_fault_handler(state)) {
2127 return;
2128 }
2129 #endif /* CONFIG_XNUPOST */
2130
2131 if (VM_KERNEL_ADDRESS(fault_addr) || thread == THREAD_NULL || recover == 0) {
2132 /*
2133 * If no recovery handler is supplied, always drive the fault against
2134 * the kernel map. If the fault was taken against a userspace VA, indicating
2135 * an unprotected access to user address space, vm_fault() should fail and
2136 * ultimately lead to a panic here.
2137 */
2138 map = kernel_map;
2139 interruptible = THREAD_UNINT;
2140
2141 #if CONFIG_KERNEL_TAGGING
2142 /*
2143 * If kernel tagging is enabled, canonicalize the address here, so that we have a
2144 * chance to find it in the VM ranges. Do not mess with exec fault cases.
2145 */
2146 if (!((fault_type) & VM_PROT_EXECUTE)) {
2147 fault_addr = vm_memtag_canonicalize(map, fault_addr);
2148 }
2149 #endif /* CONFIG_KERNEL_TAGGING */
2150 } else {
2151 map = thread->map;
2152
2153 /**
2154 * In the case that the recovery handler is set (e.g., during copyio
2155 * and dtrace probes), we don't want the vm_fault() operation to be
2156 * aborted early. Those code paths can't handle restarting the
2157 * vm_fault() operation so don't allow it to return early without
2158 * creating the wanted mapping.
2159 */
2160 interruptible = (recover) ? THREAD_UNINT : THREAD_ABORTSAFE;
2161
2162 }
2163
2164 /*
2165 * Ensure no faults in the physical aperture. This could happen if
2166 * a page table is incorrectly allocated from the read only region
2167 * when running with KTRR.
2168 */
2169 if (__improbable(fault_addr >= physmap_base) && (fault_addr < physmap_end)) {
2170 panic_with_thread_kernel_state("Unexpected fault in kernel physical aperture", state);
2171 }
2172 if (__improbable(fault_addr >= gVirtBase && fault_addr < static_memory_end)) {
2173 panic_with_thread_kernel_state("Unexpected fault in kernel static region", state);
2174 }
2175
2176 /* check to see if it is just a pmap ref/modify fault */
2177 if (!is_translation_fault(fault_code)) {
2178 result = arm_fast_fault(map->pmap,
2179 fault_addr,
2180 fault_type, (fault_code == FSC_ACCESS_FLAG_FAULT_L3), FALSE);
2181 if (result == KERN_SUCCESS) {
2182 return;
2183 }
2184 }
2185
2186 /**
2187 * vm_fault() can be called with preemption disabled (and indeed this is expected for
2188 * certain copyio() scenarios), but can't safely be called with interrupts disabled once
2189 * the system has gone multi-threaded. Other than some early-boot situations such as
2190 * startup kext loading, kernel paging operations should never be triggered by
2191 * non-interruptible code in the first place, so a fault from such a context will
2192 * ultimately produce a kernel data abort panic anyway. In these cases, skip calling
2193 * vm_fault() to avoid masking the real kernel panic with a failed VM locking assertion.
2194 */
2195 if (__probable(SPSR_INTERRUPTS_ENABLED(get_saved_state_cpsr(state)) ||
2196 startup_phase < STARTUP_SUB_EARLY_BOOT ||
2197 current_cpu_datap()->cpu_hibernate)) {
2198 if (result != KERN_PROTECTION_FAILURE) {
2199 // VM will query this property when deciding to throttle this fault, we don't want to
2200 // throttle kernel faults for copyio faults. The presence of a recovery entry is used as a
2201 // proxy for being in copyio code.
2202 bool const was_recover = thread->recover;
2203 thread->recover = was_recover || recover;
2204
2205 /*
2206 * We have to "fault" the page in.
2207 */
2208 result = vm_fault(map, fault_addr, fault_type,
2209 /* change_wiring */ FALSE, VM_KERN_MEMORY_NONE, interruptible,
2210 /* caller_pmap */ NULL, /* caller_pmap_addr */ 0);
2211
2212 thread->recover = was_recover;
2213 }
2214
2215 if (result == KERN_SUCCESS) {
2216 return;
2217 }
2218 }
2219
2220 /*
2221 * If we have a recover handler, invoke it now.
2222 */
2223 if (recover) {
2224 handle_kernel_abort_recover(state, esr, fault_addr, thread, recover);
2225 return;
2226 }
2227
2228 panic_fault_address = fault_addr;
2229 } else if (is_alignment_fault(fault_code)) {
2230 if (recover) {
2231 handle_kernel_abort_recover(state, esr, fault_addr, thread, recover);
2232 return;
2233 }
2234 panic_with_thread_kernel_state("Unaligned kernel data abort.", state);
2235 } else if (is_parity_error(fault_code)) {
2236 #if defined(APPLE_ARM64_ARCH_FAMILY)
2237 /*
2238 * Platform errors are handled in sleh_sync before interrupts are enabled.
2239 */
2240 #else
2241 panic_with_thread_kernel_state("Kernel parity error.", state);
2242 #endif
2243 } else {
2244 kprintf("Unclassified kernel abort (fault_code=0x%x)\n", fault_code);
2245 }
2246
2247 panic_with_thread_kernel_state("Kernel data abort.", state);
2248 }
2249
2250 extern void syscall_trace(struct arm_saved_state * regs);
2251
2252 static void
handle_svc(arm_saved_state_t * state)2253 handle_svc(arm_saved_state_t *state)
2254 {
2255 int trap_no = get_saved_state_svc_number(state);
2256 thread_t thread = current_thread();
2257 struct proc *p;
2258
2259 #define handle_svc_kprintf(x...) /* kprintf("handle_svc: " x) */
2260
2261 #define TRACE_SYSCALL 1
2262 #if TRACE_SYSCALL
2263 syscall_trace(state);
2264 #endif
2265
2266 thread->iotier_override = THROTTLE_LEVEL_NONE; /* Reset IO tier override before handling SVC from userspace */
2267
2268 if (trap_no == (int)PLATFORM_SYSCALL_TRAP_NO) {
2269 platform_syscall(state);
2270 panic("Returned from platform_syscall()?");
2271 }
2272
2273 current_cached_proc_cred_update();
2274
2275 if (trap_no < 0) {
2276 switch (trap_no) {
2277 case MACH_ARM_TRAP_ABSTIME:
2278 handle_mach_absolute_time_trap(state);
2279 return;
2280 case MACH_ARM_TRAP_CONTTIME:
2281 handle_mach_continuous_time_trap(state);
2282 return;
2283 }
2284
2285 /* Counting perhaps better in the handler, but this is how it's been done */
2286 thread->syscalls_mach++;
2287 mach_syscall(state);
2288 } else {
2289 /* Counting perhaps better in the handler, but this is how it's been done */
2290 thread->syscalls_unix++;
2291 p = get_bsdthreadtask_info(thread);
2292
2293 assert(p);
2294
2295 unix_syscall(state, thread, p);
2296 }
2297 }
2298
2299 static void
handle_mach_absolute_time_trap(arm_saved_state_t * state)2300 handle_mach_absolute_time_trap(arm_saved_state_t *state)
2301 {
2302 uint64_t now = mach_absolute_time();
2303 saved_state64(state)->x[0] = now;
2304 }
2305
2306 static void
handle_mach_continuous_time_trap(arm_saved_state_t * state)2307 handle_mach_continuous_time_trap(arm_saved_state_t *state)
2308 {
2309 uint64_t now = mach_continuous_time();
2310 saved_state64(state)->x[0] = now;
2311 }
2312
2313
2314 __attribute__((noreturn))
2315 static void
handle_msr_trap(arm_saved_state_t * state,uint64_t esr)2316 handle_msr_trap(arm_saved_state_t *state, uint64_t esr)
2317 {
2318 exception_type_t exception = EXC_BAD_INSTRUCTION;
2319 mach_exception_data_type_t codes[2] = {EXC_ARM_UNDEFINED};
2320 mach_msg_type_number_t numcodes = 2;
2321 uint32_t instr = 0;
2322
2323 if (!is_saved_state64(state)) {
2324 panic("MSR/MRS trap (ESR 0x%llx) from 32-bit state", esr);
2325 }
2326
2327 if (PSR64_IS_KERNEL(get_saved_state_cpsr(state))) {
2328 panic("MSR/MRS trap (ESR 0x%llx) from kernel", esr);
2329 }
2330
2331 COPYIN(get_saved_state_pc(state), (char *)&instr, sizeof(instr));
2332 codes[1] = instr;
2333
2334 exception_triage(exception, codes, numcodes);
2335 __builtin_unreachable();
2336 }
2337
2338 #if __has_feature(ptrauth_calls)
2339 static void
stringify_gpr(unsigned int r,char reg[4])2340 stringify_gpr(unsigned int r, char reg[4])
2341 {
2342 switch (r) {
2343 case 29:
2344 strncpy(reg, "fp", 4);
2345 return;
2346
2347 case 30:
2348 strncpy(reg, "lr", 4);
2349 return;
2350
2351 case 31:
2352 strncpy(reg, "xzr", 4);
2353 return;
2354
2355 default:
2356 snprintf(reg, 4, "x%u", r);
2357 return;
2358 }
2359 }
2360
2361 static void
autxx_instruction_extract_reg(uint32_t instr,char reg[4])2362 autxx_instruction_extract_reg(uint32_t instr, char reg[4])
2363 {
2364 unsigned int rd = ARM64_INSTR_AUTxx_RD_GET(instr);
2365 stringify_gpr(rd, reg);
2366 }
2367
2368 static const char *
autix_system_instruction_extract_reg(uint32_t instr)2369 autix_system_instruction_extract_reg(uint32_t instr)
2370 {
2371 unsigned int crm_op2 = ARM64_INSTR_AUTIx_SYSTEM_CRM_OP2_GET(instr);
2372 if (crm_op2 == ARM64_INSTR_AUTIx_SYSTEM_CRM_OP2_AUTIA1716 ||
2373 crm_op2 == ARM64_INSTR_AUTIx_SYSTEM_CRM_OP2_AUTIB1716) {
2374 return "x17";
2375 } else {
2376 return "lr";
2377 }
2378 }
2379
2380 static void
bxrax_instruction_extract_reg(uint32_t instr,char reg[4])2381 bxrax_instruction_extract_reg(uint32_t instr, char reg[4])
2382 {
2383 unsigned int rn = ARM64_INSTR_BxRAx_RN_GET(instr);
2384 stringify_gpr(rn, reg);
2385 }
2386
2387 static void
handle_pac_fail(arm_saved_state_t * state,uint64_t esr)2388 handle_pac_fail(arm_saved_state_t *state, uint64_t esr)
2389 {
2390 exception_type_t exception = EXC_BAD_ACCESS | EXC_PTRAUTH_BIT;
2391 mach_exception_data_type_t codes[2] = {EXC_ARM_PAC_FAIL};
2392 mach_msg_type_number_t numcodes = 2;
2393 uint32_t instr = 0;
2394
2395 if (!is_saved_state64(state)) {
2396 panic("PAC failure (ESR 0x%llx) from 32-bit state", esr);
2397 }
2398
2399 COPYIN(get_saved_state_pc(state), (char *)&instr, sizeof(instr));
2400
2401 if (PSR64_IS_KERNEL(get_saved_state_cpsr(state))) {
2402 #define GENERIC_PAC_FAILURE_MSG_FMT "PAC failure from kernel with %s key"
2403 #define AUTXX_MSG_FMT GENERIC_PAC_FAILURE_MSG_FMT " while authing %s"
2404 #define BXRAX_MSG_FMT GENERIC_PAC_FAILURE_MSG_FMT " while branching to %s"
2405 #define RETAX_MSG_FMT GENERIC_PAC_FAILURE_MSG_FMT " while returning"
2406 #define GENERIC_MSG_FMT GENERIC_PAC_FAILURE_MSG_FMT
2407 #define MAX_PAC_MSG_FMT BXRAX_MSG_FMT
2408
2409 char msg[strlen(MAX_PAC_MSG_FMT)
2410 - strlen("%s") + strlen("IA")
2411 - strlen("%s") + strlen("xzr")
2412 + 1];
2413 ptrauth_key key = (ptrauth_key)(esr & 0x3);
2414 const char *key_str = ptrauth_key_to_string(key);
2415
2416 if (ARM64_INSTR_IS_AUTxx(instr)) {
2417 char reg[4];
2418 autxx_instruction_extract_reg(instr, reg);
2419 snprintf(msg, sizeof(msg), AUTXX_MSG_FMT, key_str, reg);
2420 } else if (ARM64_INSTR_IS_AUTIx_SYSTEM(instr)) {
2421 const char *reg = autix_system_instruction_extract_reg(instr);
2422 snprintf(msg, sizeof(msg), AUTXX_MSG_FMT, key_str, reg);
2423 } else if (ARM64_INSTR_IS_BxRAx(instr)) {
2424 char reg[4];
2425 bxrax_instruction_extract_reg(instr, reg);
2426 snprintf(msg, sizeof(msg), BXRAX_MSG_FMT, key_str, reg);
2427 } else if (ARM64_INSTR_IS_RETAx(instr)) {
2428 snprintf(msg, sizeof(msg), RETAX_MSG_FMT, key_str);
2429 } else {
2430 snprintf(msg, sizeof(msg), GENERIC_MSG_FMT, key_str);
2431 }
2432 panic_with_thread_kernel_state(msg, state);
2433 }
2434
2435 codes[1] = instr;
2436
2437 exception_triage(exception, codes, numcodes);
2438 __builtin_unreachable();
2439 }
2440 #endif /* __has_feature(ptrauth_calls) */
2441
2442 __attribute__((noreturn))
2443 static void
handle_bti_fail(arm_saved_state_t * state,uint64_t esr)2444 handle_bti_fail(arm_saved_state_t *state, uint64_t esr)
2445 {
2446 uint32_t btype = (uint32_t) esr & ISS_BTI_BTYPE_MASK;
2447
2448 if (!is_saved_state64(state)) {
2449 /* BTI is an ARMv8 feature, this should not be possible */
2450 panic("BTI failure for 32-bit state? (ESR=0x%llx)", esr);
2451 }
2452
2453 /*
2454 * We currently only expect BTI to be enabled for kernel pages, so panic if
2455 * we detect otherwise.
2456 */
2457 if (!PSR64_IS_KERNEL(get_saved_state_cpsr(state))) {
2458 panic("Unexpected non-kernel BTI failure? (ESR=0x%llx)", esr);
2459 }
2460
2461 #define BTI_FAIL_PTR_FMT "%04x"
2462 #define BTI_FAIL_MSG_FMT "Kernel BTI failure (BTYPE=0x" BTI_FAIL_PTR_FMT ")"
2463 /* Replace the pointer format with the length of the pointer message+NULL */
2464 char msg[strlen(BTI_FAIL_MSG_FMT) - strlen(BTI_FAIL_PTR_FMT) + 8 + 1];
2465 snprintf(msg, sizeof(msg), BTI_FAIL_MSG_FMT, btype);
2466 panic_with_thread_kernel_state(msg, state);
2467 __builtin_unreachable();
2468 }
2469
2470 static void
handle_user_trapped_instruction32(arm_saved_state_t * state,uint64_t esr)2471 handle_user_trapped_instruction32(arm_saved_state_t *state, uint64_t esr)
2472 {
2473 exception_type_t exception = EXC_BAD_INSTRUCTION;
2474 mach_exception_data_type_t codes[2] = {EXC_ARM_UNDEFINED};
2475 mach_msg_type_number_t numcodes = 2;
2476 uint32_t instr;
2477
2478 if (is_saved_state64(state)) {
2479 panic("ESR (0x%llx) for instruction trapped from U32, but saved state is 64-bit.", esr);
2480 }
2481
2482 if (PSR64_IS_KERNEL(get_saved_state_cpsr(state))) {
2483 panic("ESR (0x%llx) for instruction trapped from U32, actually came from kernel?", esr);
2484 }
2485
2486 COPYIN(get_saved_state_pc(state), (char *)&instr, sizeof(instr));
2487 codes[1] = instr;
2488
2489 exception_triage(exception, codes, numcodes);
2490 __builtin_unreachable();
2491 }
2492
2493 static void
handle_simd_trap(arm_saved_state_t * state,uint64_t esr)2494 handle_simd_trap(arm_saved_state_t *state, uint64_t esr)
2495 {
2496 exception_type_t exception = EXC_BAD_INSTRUCTION;
2497 mach_exception_data_type_t codes[2] = {EXC_ARM_UNDEFINED};
2498 mach_msg_type_number_t numcodes = 2;
2499 uint32_t instr = 0;
2500
2501 if (PSR64_IS_KERNEL(get_saved_state_cpsr(state))) {
2502 panic("ESR (0x%llx) for SIMD trap from userland, actually came from kernel?", esr);
2503 }
2504
2505 COPYIN(get_saved_state_pc(state), (char *)&instr, sizeof(instr));
2506 codes[1] = instr;
2507
2508 exception_triage(exception, codes, numcodes);
2509 __builtin_unreachable();
2510 }
2511
2512 void
sleh_irq(arm_saved_state_t * state)2513 sleh_irq(arm_saved_state_t *state)
2514 {
2515 cpu_data_t * cdp __unused = getCpuDatap();
2516 #if MACH_ASSERT
2517 int preemption_level = sleh_get_preemption_level();
2518 #endif
2519
2520
2521 sleh_interrupt_handler_prologue(state, DBG_INTR_TYPE_OTHER);
2522
2523 #if USE_APPLEARMSMP
2524 PE_handle_ext_interrupt();
2525 #else
2526 /* Run the registered interrupt handler. */
2527 cdp->interrupt_handler(cdp->interrupt_target,
2528 cdp->interrupt_refCon,
2529 cdp->interrupt_nub,
2530 cdp->interrupt_source);
2531 #endif
2532
2533 entropy_collect();
2534
2535
2536 sleh_interrupt_handler_epilogue();
2537 #if MACH_ASSERT
2538 if (preemption_level != sleh_get_preemption_level()) {
2539 panic("irq handler %p changed preemption level from %d to %d", cdp->interrupt_handler, preemption_level, sleh_get_preemption_level());
2540 }
2541 #endif
2542 }
2543
2544 void
sleh_fiq(arm_saved_state_t * state)2545 sleh_fiq(arm_saved_state_t *state)
2546 {
2547 unsigned int type = DBG_INTR_TYPE_UNKNOWN;
2548 #if MACH_ASSERT
2549 int preemption_level = sleh_get_preemption_level();
2550 #endif
2551
2552 #if MONOTONIC_FIQ
2553 uint64_t pmcr0 = 0, upmsr = 0;
2554 #endif /* MONOTONIC_FIQ */
2555
2556 #if defined(HAS_IPI)
2557 boolean_t is_ipi = FALSE;
2558 uint64_t ipi_sr = 0;
2559
2560 if (gFastIPI) {
2561 MRS(ipi_sr, "S3_5_C15_C1_1");
2562
2563 if (ipi_sr & ARM64_IPISR_IPI_PENDING) {
2564 is_ipi = TRUE;
2565 }
2566 }
2567
2568 if (is_ipi) {
2569 type = DBG_INTR_TYPE_IPI;
2570 } else
2571 #endif /* defined(HAS_IPI) */
2572 if (ml_get_timer_pending()) {
2573 type = DBG_INTR_TYPE_TIMER;
2574 }
2575 #if MONOTONIC_FIQ
2576 /* Consult the PMI sysregs last, after IPI/timer
2577 * classification.
2578 */
2579 else if (mt_pmi_pending(&pmcr0, &upmsr)) {
2580 type = DBG_INTR_TYPE_PMI;
2581 }
2582 #endif /* MONOTONIC_FIQ */
2583
2584 sleh_interrupt_handler_prologue(state, type);
2585
2586 #if APPLEVIRTUALPLATFORM
2587 uint64_t iar = __builtin_arm_rsr64("ICC_IAR0_EL1");
2588 #endif
2589
2590 #if defined(HAS_IPI)
2591 if (type == DBG_INTR_TYPE_IPI) {
2592 /*
2593 * Order is important here: we must ack the IPI by writing IPI_SR
2594 * before we call cpu_signal_handler(). Otherwise, there will be
2595 * a window between the completion of pending-signal processing in
2596 * cpu_signal_handler() and the ack during which a newly-issued
2597 * IPI to this CPU may be lost. ISB is required to ensure the msr
2598 * is retired before execution of cpu_signal_handler().
2599 */
2600 MSR("S3_5_C15_C1_1", ARM64_IPISR_IPI_PENDING);
2601 __builtin_arm_isb(ISB_SY);
2602 cpu_signal_handler();
2603 } else
2604 #endif /* defined(HAS_IPI) */
2605 #if MONOTONIC_FIQ
2606 if (type == DBG_INTR_TYPE_PMI) {
2607 INTERRUPT_MASKED_DEBUG_START(mt_fiq, DBG_INTR_TYPE_PMI);
2608 mt_fiq(getCpuDatap(), pmcr0, upmsr);
2609 INTERRUPT_MASKED_DEBUG_END();
2610 } else
2611 #endif /* MONOTONIC_FIQ */
2612 {
2613 /*
2614 * We don't know that this is a timer, but we don't have insight into
2615 * the other interrupts that go down this path.
2616 */
2617
2618 cpu_data_t *cdp = getCpuDatap();
2619
2620 cdp->cpu_decrementer = -1; /* Large */
2621
2622 /*
2623 * ARM64_TODO: whether we're coming from userland is ignored right now.
2624 * We can easily thread it through, but not bothering for the
2625 * moment (AArch32 doesn't either).
2626 */
2627 INTERRUPT_MASKED_DEBUG_START(rtclock_intr, DBG_INTR_TYPE_TIMER);
2628 rtclock_intr(TRUE);
2629 INTERRUPT_MASKED_DEBUG_END();
2630 }
2631
2632 #if APPLEVIRTUALPLATFORM
2633 if (iar != GIC_SPURIOUS_IRQ) {
2634 __builtin_arm_wsr64("ICC_EOIR0_EL1", iar);
2635 __builtin_arm_isb(ISB_SY);
2636 }
2637 #endif
2638
2639 sleh_interrupt_handler_epilogue();
2640 #if MACH_ASSERT
2641 if (preemption_level != sleh_get_preemption_level()) {
2642 panic("fiq type %u changed preemption level from %d to %d", type, preemption_level, sleh_get_preemption_level());
2643 }
2644 #endif
2645 }
2646
2647 void
sleh_serror(arm_context_t * context,uint64_t esr,vm_offset_t far)2648 sleh_serror(arm_context_t *context, uint64_t esr, vm_offset_t far)
2649 {
2650 task_vtimer_check(current_thread());
2651
2652 KDBG_RELEASE(MACHDBG_CODE(DBG_MACH_EXCP_SERR_ARM, 0) | DBG_FUNC_START,
2653 esr, VM_KERNEL_ADDRHIDE(far));
2654 arm_saved_state_t *state = &context->ss;
2655 #if MACH_ASSERT
2656 int preemption_level = sleh_get_preemption_level();
2657 #endif
2658
2659 if (PSR64_IS_USER(get_saved_state_cpsr(state))) {
2660 /* Sanitize FAR (only if we came from userspace) */
2661 saved_state64(state)->far = 0;
2662 }
2663
2664 ASSERT_CONTEXT_SANITY(context);
2665 arm64_platform_error(state, esr, far, PLAT_ERR_SRC_ASYNC);
2666 #if MACH_ASSERT
2667 if (preemption_level != sleh_get_preemption_level()) {
2668 panic("serror changed preemption level from %d to %d", preemption_level, sleh_get_preemption_level());
2669 }
2670 #endif
2671 KDBG_RELEASE(MACHDBG_CODE(DBG_MACH_EXCP_SERR_ARM, 0) | DBG_FUNC_END,
2672 esr, VM_KERNEL_ADDRHIDE(far));
2673 }
2674
2675 void
mach_syscall_trace_exit(unsigned int retval,unsigned int call_number)2676 mach_syscall_trace_exit(unsigned int retval,
2677 unsigned int call_number)
2678 {
2679 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
2680 MACHDBG_CODE(DBG_MACH_EXCP_SC, (call_number)) |
2681 DBG_FUNC_END, retval, 0, 0, 0, 0);
2682 }
2683
2684 __attribute__((noreturn))
2685 void
thread_syscall_return(kern_return_t error)2686 thread_syscall_return(kern_return_t error)
2687 {
2688 thread_t thread;
2689 struct arm_saved_state *state;
2690
2691 thread = current_thread();
2692 state = get_user_regs(thread);
2693
2694 assert(is_saved_state64(state));
2695 saved_state64(state)->x[0] = error;
2696
2697 #if MACH_ASSERT
2698 kern_allocation_name_t
2699 prior __assert_only = thread_get_kernel_state(thread)->allocation_name;
2700 assertf(prior == NULL, "thread_set_allocation_name(\"%s\") not cleared", kern_allocation_get_name(prior));
2701 #endif /* MACH_ASSERT */
2702
2703 if (kdebug_enable) {
2704 /* Invert syscall number (negative for a mach syscall) */
2705 mach_syscall_trace_exit(error, (-1) * get_saved_state_svc_number(state));
2706 }
2707
2708 thread_exception_return();
2709 }
2710
2711 void
syscall_trace(struct arm_saved_state * regs __unused)2712 syscall_trace(
2713 struct arm_saved_state * regs __unused)
2714 {
2715 /* kprintf("syscall: %d\n", saved_state64(regs)->x[16]); */
2716 }
2717
2718 static void
sleh_interrupt_handler_prologue(arm_saved_state_t * state,unsigned int type)2719 sleh_interrupt_handler_prologue(arm_saved_state_t *state, unsigned int type)
2720 {
2721 const bool is_user = PSR64_IS_USER(get_saved_state_cpsr(state));
2722
2723 if (is_user == true) {
2724 /* Sanitize FAR (only if the interrupt occurred while the CPU was in usermode) */
2725 saved_state64(state)->far = 0;
2726 }
2727
2728 recount_enter_interrupt();
2729
2730 task_vtimer_check(current_thread());
2731
2732 uint64_t pc = is_user ? get_saved_state_pc(state) :
2733 VM_KERNEL_UNSLIDE(get_saved_state_pc(state));
2734
2735 KDBG_RELEASE(MACHDBG_CODE(DBG_MACH_EXCP_INTR, 0) | DBG_FUNC_START,
2736 0, pc, is_user, type);
2737 }
2738
2739 static void
sleh_interrupt_handler_epilogue(void)2740 sleh_interrupt_handler_epilogue(void)
2741 {
2742 #if KPERF
2743 kperf_interrupt();
2744 #endif /* KPERF */
2745 KDBG_RELEASE(MACHDBG_CODE(DBG_MACH_EXCP_INTR, 0) | DBG_FUNC_END);
2746 recount_leave_interrupt();
2747 }
2748
2749 void
sleh_invalid_stack(arm_context_t * context,uint64_t esr __unused,vm_offset_t far __unused)2750 sleh_invalid_stack(arm_context_t *context, uint64_t esr __unused, vm_offset_t far __unused)
2751 {
2752 thread_t thread = current_thread();
2753 vm_offset_t kernel_stack_bottom, sp;
2754
2755 sp = get_saved_state_sp(&context->ss);
2756 vm_offset_t kstackptr = (vm_offset_t)thread->machine.kstackptr;
2757 kernel_stack_bottom = round_page(kstackptr) - KERNEL_STACK_SIZE;
2758
2759 if ((sp < kernel_stack_bottom) && (sp >= (kernel_stack_bottom - PAGE_SIZE))) {
2760 panic_with_thread_kernel_state("Invalid kernel stack pointer (probable overflow).", &context->ss);
2761 }
2762
2763 panic_with_thread_kernel_state("Invalid kernel stack pointer (probable corruption).", &context->ss);
2764 }
2765
2766
2767 #if MACH_ASSERT
2768 static int trap_handled;
2769 static const char *
handle_recoverable_kernel_trap(__unused void * tstate,uint16_t comment)2770 handle_recoverable_kernel_trap(
2771 __unused void *tstate,
2772 uint16_t comment)
2773 {
2774 assert(comment == TEST_RECOVERABLE_SOFT_TRAP);
2775
2776 printf("Recoverable trap handled.\n");
2777 trap_handled = 1;
2778
2779 return NULL;
2780 }
2781
2782 KERNEL_BRK_DESCRIPTOR_DEFINE(test_desc,
2783 .type = TRAP_TELEMETRY_TYPE_KERNEL_BRK_TEST,
2784 .base = TEST_RECOVERABLE_SOFT_TRAP,
2785 .max = TEST_RECOVERABLE_SOFT_TRAP,
2786 .options = BRK_TELEMETRY_OPTIONS_RECOVERABLE_DEFAULT(
2787 /* enable_telemetry */ false),
2788 .handle_breakpoint = handle_recoverable_kernel_trap);
2789
2790 static int
recoverable_kernel_trap_test(__unused int64_t in,int64_t * out)2791 recoverable_kernel_trap_test(__unused int64_t in, int64_t *out)
2792 {
2793 ml_recoverable_trap(TEST_RECOVERABLE_SOFT_TRAP);
2794
2795 *out = trap_handled;
2796 return 0;
2797 }
2798
2799 SYSCTL_TEST_REGISTER(recoverable_kernel_trap, recoverable_kernel_trap_test);
2800
2801 #endif
2802
2803 #if CONFIG_SPTM
2804 /**
2805 * Evaluate the panic lockdown policy for a synchronous EL1 SP0 exception
2806 *
2807 * Returns true if panic lockdown should be initiated (but does not itself do
2808 * so)
2809 */
2810 __SECURITY_STACK_DISALLOWED_PUSH
2811 bool
sleh_panic_lockdown_should_initiate_el1_sp0_sync(uint64_t esr,uint64_t elr,uint64_t far,uint64_t spsr)2812 sleh_panic_lockdown_should_initiate_el1_sp0_sync(uint64_t esr, uint64_t elr,
2813 uint64_t far, uint64_t spsr)
2814 {
2815 const esr_exception_class_t class = ESR_EC(esr);
2816 const bool any_exceptions_masked = spsr & DAIF_STANDARD_DISABLE;
2817
2818 switch (class) {
2819 case ESR_EC_PC_ALIGN: /* PC misaligned (should never happen) */
2820 case ESR_EC_IABORT_EL1: /* Potential iPAC failure (poisoned PC) */
2821 case ESR_EC_PAC_FAIL: { /* FPAC fail */
2822 return true;
2823 }
2824
2825 case ESR_EC_BRK_AARCH64: {
2826 /*
2827 * Breakpoints are used on non-FPAC systems to signal some PAC failures
2828 */
2829 #if HAS_TELEMETRY_KERNEL_BRK
2830 const struct kernel_brk_descriptor *desc;
2831 desc = find_kernel_brk_descriptor_by_comment(ISS_BRK_COMMENT(esr));
2832 if (desc && desc->options.recoverable) {
2833 /*
2834 * We matched a breakpoint and it's recoverable, skip lockdown.
2835 */
2836 return false;
2837 }
2838 #endif /* HAS_TELEMETRY_KERNEL_BRK */
2839
2840 /*
2841 * If we don't support telemetry breakpoints and/or didn't match a
2842 * recoverable breakpoint, the exception is fatal.
2843 */
2844 return true;
2845 }
2846
2847 case ESR_EC_DABORT_EL1: {
2848 const struct copyio_recovery_entry *cre =
2849 find_copyio_recovery_entry(elr);
2850 if (cre) {
2851
2852 /*
2853 * copyio faults are recoverable regardless of whether or not
2854 * exceptions are masked.
2855 */
2856 return false;
2857 }
2858
2859
2860 /*
2861 * Heuristic: if FAR != XPAC(FAR), the pointer was likely corrupted
2862 * due to PAC.
2863 */
2864 const uint64_t far_stripped =
2865 (uint64_t)ptrauth_strip((void *)far, ptrauth_key_asda);
2866
2867 if (far != far_stripped) {
2868 /* potential dPAC failure (poisoined address) */
2869 return true;
2870 }
2871
2872 if (any_exceptions_masked && startup_phase >= STARTUP_SUB_LOCKDOWN) {
2873 /*
2874 * Any data abort taken with exceptions masked is fatal if we're
2875 * past early boot.
2876 */
2877 return true;
2878 }
2879
2880 return false;
2881 }
2882
2883 case ESR_EC_UNCATEGORIZED: {
2884 /* Undefined instruction (GDBTRAP for stackshots, etc.) */
2885 return false;
2886 }
2887
2888 case ESR_EC_BTI_FAIL: {
2889 /* Kernel BTI exceptions are recoverable only in telemetry mode */
2890 #ifdef CONFIG_BTI_TELEMETRY
2891 return false;
2892 #else
2893 return true;
2894 #endif /* CONFIG_BTI_TELEMETRY */
2895 }
2896
2897 default: {
2898 if (!any_exceptions_masked) {
2899 /*
2900 * When exceptions are not masked, we default-allow exceptions.
2901 */
2902 return false;
2903 }
2904
2905 if (startup_phase < STARTUP_SUB_LOCKDOWN) {
2906 /*
2907 * Ignore early boot exceptions even if exceptions are masked.
2908 */
2909 return false;
2910 }
2911
2912 /* Default-deny all others when exceptions are masked */
2913 return true;
2914 }
2915 }
2916 }
2917 __SECURITY_STACK_DISALLOWED_POP
2918 #endif /* CONFIG_SPTM */
2919