1 /*
2 * Copyright (c) 2007-2020 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 #include <debug.h>
30 #include <mach_kdp.h>
31 #include <kern/kern_stackshot.h>
32
33 #include <kern/thread.h>
34 #include <machine/pmap.h>
35 #include <device/device_types.h>
36
37 #include <mach/vm_param.h>
38 #include <mach/clock_types.h>
39 #include <mach/machine.h>
40 #include <mach/kmod.h>
41 #include <pexpert/boot.h>
42 #include <pexpert/pexpert.h>
43
44 #include <ptrauth.h>
45
46 #include <kern/misc_protos.h>
47 #include <kern/startup.h>
48 #include <kern/clock.h>
49 #include <kern/debug.h>
50 #include <kern/processor.h>
51 #include <kdp/kdp_core.h>
52 #if ALTERNATE_DEBUGGER
53 #include <arm64/alternate_debugger.h>
54 #endif
55 #include <machine/atomic.h>
56 #include <machine/trap.h>
57 #include <kern/spl.h>
58 #include <pexpert/pexpert.h>
59 #include <kdp/kdp_callout.h>
60 #include <kdp/kdp_dyld.h>
61 #include <kdp/kdp_internal.h>
62 #include <kdp/kdp_common.h>
63 #include <uuid/uuid.h>
64 #include <sys/codesign.h>
65 #include <sys/time.h>
66
67 #if CONFIG_SPTM
68 #include <kern/percpu.h>
69 #include <arm64/sptm/pmap/pmap_data.h>
70 #endif
71
72 #include <IOKit/IOPlatformExpert.h>
73 #include <IOKit/IOKitServer.h>
74
75 #include <mach/vm_prot.h>
76 #include <vm/vm_map_xnu.h>
77 #include <vm/pmap.h>
78 #include <vm/vm_shared_region.h>
79 #include <mach/time_value.h>
80 #include <machine/machparam.h> /* for btop */
81
82 #include <console/video_console.h>
83 #include <console/serial_protos.h>
84 #include <arm/cpu_data.h>
85 #include <arm/cpu_data_internal.h>
86 #include <arm/cpu_internal.h>
87 #include <arm/misc_protos.h>
88 #include <libkern/OSKextLibPrivate.h>
89 #include <vm/vm_kern.h>
90 #include <kern/kern_cdata.h>
91 #include <kern/ledger.h>
92 #include <san/kcov_ksancov.h>
93
94
95 #if DEVELOPMENT || DEBUG
96 #include <kern/ext_paniclog.h>
97 #endif
98
99 #if CONFIG_EXCLAVES
100 #include <kern/exclaves_panic.h>
101 #include <kern/exclaves_inspection.h>
102 #endif
103
104
105 #if MACH_KDP
106 void kdp_trap(unsigned int, struct arm_saved_state *);
107 #endif
108
109 /*
110 * Increment the PANICLOG_VERSION if you change the format of the panic
111 * log in any way.
112 */
113 #define PANICLOG_VERSION 15
114 static struct kcdata_descriptor kc_panic_data;
115
116 extern char iBoot_version[];
117 #if defined(TARGET_OS_OSX) && defined(__arm64__)
118 extern char iBoot_Stage_2_version[];
119 #endif /* defined(TARGET_OS_OSX) && defined(__arm64__) */
120
121 extern volatile uint32_t debug_enabled;
122 extern unsigned int not_in_kdp;
123
124 extern int copyinframe(vm_address_t fp, uint32_t * frame);
125 extern void kdp_callouts(kdp_event_t event);
126
127 #define MAX_PROCNAME_LEN 32
128 /* #include <sys/proc.h> */
129 struct proc;
130 extern int proc_pid(struct proc *p);
131 extern void proc_name_kdp(struct proc *, char *, int);
132
133 /*
134 * Make sure there's enough space to include the relevant bits in the format required
135 * within the space allocated for the panic version string in the panic header.
136 * The format required by OSAnalytics/DumpPanic is 'Product Version (OS Version)'.
137 */
138 #define PANIC_HEADER_VERSION_FMT_STR "%.14s (%.14s)"
139
140 extern const char version[];
141 extern char osversion[];
142 extern char osproductversion[];
143 extern char osreleasetype[];
144
145 #if defined(XNU_TARGET_OS_BRIDGE)
146 extern char macosproductversion[];
147 extern char macosversion[];
148 #endif
149
150 extern uint8_t gPlatformECID[8];
151 extern uint32_t gPlatformMemoryID;
152
153 extern uint64_t last_hwaccess_thread;
154 extern uint8_t last_hwaccess_type; /* 0 : read, 1 : write. */
155 extern uint8_t last_hwaccess_size;
156 extern uint64_t last_hwaccess_paddr;
157
158 /*Choosing the size for gTargetTypeBuffer as 16 and size for gModelTypeBuffer as 32
159 * since the target name and model name typically doesn't exceed this size */
160 extern char gTargetTypeBuffer[16];
161 extern char gModelTypeBuffer[32];
162
163 extern struct timeval gIOLastSleepTime;
164 extern struct timeval gIOLastWakeTime;
165 extern boolean_t is_clock_configured;
166 extern boolean_t kernelcache_uuid_valid;
167 extern uuid_t kernelcache_uuid;
168 extern uuid_string_t bootsessionuuid_string;
169
170 extern uint64_t roots_installed;
171
172 /* Definitions for frame pointers */
173 #define FP_ALIGNMENT_MASK ((uint32_t)(0x3))
174 #define FP_LR_OFFSET ((uint32_t)4)
175 #define FP_LR_OFFSET64 ((uint32_t)8)
176 #define FP_MAX_NUM_TO_EVALUATE (50)
177
178 /* Timeout for all processors responding to debug crosscall */
179 MACHINE_TIMEOUT_ALWAYS_ENABLED(debug_ack_timeout, "debug-ack", 240000, MACHINE_TIMEOUT_UNIT_TIMEBASE);
180
181 /* Forward functions definitions */
182 void panic_display_times(void);
183 void panic_print_symbol_name(vm_address_t search);
184
185
186 /* Global variables */
187 static uint32_t panic_bt_depth;
188 boolean_t PanicInfoSaved = FALSE;
189 boolean_t force_immediate_debug_halt = FALSE;
190 unsigned int debug_ack_timeout_count = 0;
191 _Atomic unsigned int debugger_sync = 0;
192 _Atomic unsigned int mp_kdp_trap = 0; /* CPUs signalled by the debug CPU will spin on this */
193 _Atomic unsigned int debug_cpus_spinning = 0; /* Number of signalled CPUs still spinning on mp_kdp_trap (in DebuggerXCall). */
194 unsigned int DebugContextCount = 0;
195 bool trap_is_stackshot = false; /* Whether the trap is for a stackshot */
196
197 #if defined(__arm64__)
198 uint8_t PE_smc_stashed_x86_system_state = 0xFF;
199 uint8_t PE_smc_stashed_x86_power_state = 0xFF;
200 uint8_t PE_smc_stashed_x86_efi_boot_state = 0xFF;
201 uint8_t PE_smc_stashed_x86_shutdown_cause = 0xFF;
202 uint64_t PE_smc_stashed_x86_prev_power_transitions = UINT64_MAX;
203 uint32_t PE_pcie_stashed_link_state = UINT32_MAX;
204 uint64_t PE_nvram_stashed_x86_macos_slide = UINT64_MAX;
205 #endif
206
207
208 static void
do_print_backtrace_internal(pmap_t pmap,vm_offset_t topfp,const char * cur_marker,boolean_t is_64_bit,boolean_t print_kexts_in_backtrace)209 do_print_backtrace_internal(pmap_t pmap, vm_offset_t topfp, const char *cur_marker,
210 boolean_t is_64_bit, boolean_t print_kexts_in_backtrace)
211 {
212 unsigned int i = 0;
213 addr64_t lr = 0;
214 addr64_t fp = topfp;
215 addr64_t fp_for_ppn = 0;
216 ppnum_t ppn = (ppnum_t)NULL;
217 vm_offset_t raddrs[FP_MAX_NUM_TO_EVALUATE] = { 0 };
218 bool dump_kernel_stack = (fp >= VM_MIN_KERNEL_ADDRESS);
219
220 do {
221 if ((fp == 0) || ((fp & FP_ALIGNMENT_MASK) != 0)) {
222 break;
223 }
224
225 if ((!dump_kernel_stack) && (fp >= VM_MIN_KERNEL_ADDRESS)) {
226 break;
227 }
228
229 /*
230 * Check to see if current address will result in a different
231 * ppn than previously computed (to avoid recomputation) via
232 * (addr) ^ fp_for_ppn) >> PAGE_SHIFT)
233 */
234 if ((((fp + FP_LR_OFFSET) ^ fp_for_ppn) >> PAGE_SHIFT) != 0x0U) {
235 ppn = pmap_find_phys(pmap, fp + FP_LR_OFFSET);
236 fp_for_ppn = fp + (is_64_bit ? FP_LR_OFFSET64 : FP_LR_OFFSET);
237 }
238 if (ppn != (ppnum_t)NULL) {
239 if (is_64_bit) {
240 lr = ml_phys_read_double_64(((((vm_offset_t)ppn) << PAGE_SHIFT)) | ((fp + FP_LR_OFFSET64) & PAGE_MASK));
241 #if defined(HAS_APPLE_PAC)
242 /* return addresses on stack will be signed by arm64e ABI */
243 lr = (addr64_t) ptrauth_strip((void *)lr, ptrauth_key_return_address);
244 #endif
245 } else {
246 lr = ml_phys_read_word(((((vm_offset_t)ppn) << PAGE_SHIFT)) | ((fp + FP_LR_OFFSET) & PAGE_MASK));
247 }
248 } else {
249 if (is_64_bit) {
250 paniclog_append_noflush("%s\t Could not read LR from frame at 0x%016llx\n", cur_marker, fp + FP_LR_OFFSET64);
251 } else {
252 paniclog_append_noflush("%s\t Could not read LR from frame at 0x%08x\n", cur_marker, (uint32_t)(fp + FP_LR_OFFSET));
253 }
254 break;
255 }
256 if (((fp ^ fp_for_ppn) >> PAGE_SHIFT) != 0x0U) {
257 ppn = pmap_find_phys(pmap, fp);
258 fp_for_ppn = fp;
259 }
260 if (ppn != (ppnum_t)NULL) {
261 if (is_64_bit) {
262 fp = ml_phys_read_double_64(((((vm_offset_t)ppn) << PAGE_SHIFT)) | (fp & PAGE_MASK));
263 #if defined(HAS_APPLE_PAC)
264 /* frame pointers on stack will be signed by arm64e ABI */
265 fp = (addr64_t) ptrauth_strip((void *)fp, ptrauth_key_frame_pointer);
266 #endif
267 } else {
268 fp = ml_phys_read_word(((((vm_offset_t)ppn) << PAGE_SHIFT)) | (fp & PAGE_MASK));
269 }
270 } else {
271 if (is_64_bit) {
272 paniclog_append_noflush("%s\t Could not read FP from frame at 0x%016llx\n", cur_marker, fp);
273 } else {
274 paniclog_append_noflush("%s\t Could not read FP from frame at 0x%08x\n", cur_marker, (uint32_t)fp);
275 }
276 break;
277 }
278 /*
279 * Counter 'i' may == FP_MAX_NUM_TO_EVALUATE when running one
280 * extra round to check whether we have all frames in order to
281 * indicate (in)complete backtrace below. This happens in a case
282 * where total frame count and FP_MAX_NUM_TO_EVALUATE are equal.
283 * Do not capture anything.
284 */
285 if (i < FP_MAX_NUM_TO_EVALUATE && lr) {
286 if (is_64_bit) {
287 paniclog_append_noflush("%s\t lr: 0x%016llx fp: 0x%016llx\n", cur_marker, lr, fp);
288 } else {
289 paniclog_append_noflush("%s\t lr: 0x%08x fp: 0x%08x\n", cur_marker, (uint32_t)lr, (uint32_t)fp);
290 }
291 raddrs[i] = lr;
292 }
293 } while ((++i <= FP_MAX_NUM_TO_EVALUATE) && (fp != topfp));
294
295 if (i > FP_MAX_NUM_TO_EVALUATE && fp != 0) {
296 paniclog_append_noflush("Backtrace continues...\n");
297 }
298
299 if (print_kexts_in_backtrace && i > 0) {
300 kmod_panic_dump(&raddrs[0], i);
301 }
302 }
303
304 #define SANE_TASK_LIMIT 256
305 #define TOP_RUNNABLE_LIMIT 5
306 #define PANICLOG_UUID_BUF_SIZE 256
307
308 extern void panic_print_vnodes(void);
309
310 static void
panic_display_tpidrs(void)311 panic_display_tpidrs(void)
312 {
313 #if defined(__arm64__)
314 paniclog_append_noflush("TPIDRx_ELy = {1: 0x%016llx 0: 0x%016llx 0ro: 0x%016llx }\n",
315 __builtin_arm_rsr64("TPIDR_EL1"), __builtin_arm_rsr64("TPIDR_EL0"),
316 __builtin_arm_rsr64("TPIDRRO_EL0"));
317 #endif //defined(__arm64__)
318 }
319
320
321
322 static void
panic_display_hung_cpus_help(void)323 panic_display_hung_cpus_help(void)
324 {
325 #if defined(__arm64__)
326 const uint32_t pcsr_offset = 0x90;
327
328 /*
329 * Print some info that might help in cases where nothing
330 * else does
331 */
332 const ml_topology_info_t *info = ml_get_topology_info();
333 if (info) {
334 unsigned i, retry;
335
336 for (i = 0; i < info->num_cpus; i++) {
337 ml_topology_cpu_t *cpu = &info->cpus[i];
338 char cluster_name[16], cluster_letter;
339
340 switch (cpu->cluster_type) {
341 case CLUSTER_TYPE_E:
342 cluster_letter = 'E';
343 break;
344 case CLUSTER_TYPE_P:
345 cluster_letter = 'P';
346 break;
347 default:
348 cluster_letter = '?';
349 }
350 snprintf(cluster_name, sizeof(cluster_name), "%cACC%d", cluster_letter, cpu->cluster_id);
351
352 if (!PE_cpu_power_check_kdp(i)) {
353 paniclog_append_noflush("CORE %u [%s] is offline, skipping\n", i, cluster_name);
354 continue;
355 }
356 if (cpu->cpu_UTTDBG_regs) {
357 volatile uint64_t *pcsr = (volatile uint64_t*)(cpu->cpu_UTTDBG_regs + pcsr_offset);
358 volatile uint32_t *pcsrTrigger = (volatile uint32_t*)pcsr;
359 uint64_t pc = 0;
360
361 // a number of retries are needed till this works
362 for (retry = 1024; retry && !pc; retry--) {
363 //a 32-bit read is required to make a PC sample be produced, else we'll only get a zero
364 (void)*pcsrTrigger;
365 pc = *pcsr;
366 }
367
368 //postprocessing (same as astris does)
369 if (pc >> 48) {
370 pc |= 0xffff000000000000ull;
371 }
372 paniclog_append_noflush("CORE %u [%s] recently retired instr at 0x%016llx\n", i, cluster_name, pc);
373 }
374 }
375 }
376 #endif //defined(__arm64__)
377 }
378
379
380
381 static void
panic_display_pvhs_locked(void)382 panic_display_pvhs_locked(void)
383 {
384 }
385
386 static void
panic_display_pvh_to_lock(void)387 panic_display_pvh_to_lock(void)
388 {
389 }
390
391 static void
panic_display_last_pc_lr(void)392 panic_display_last_pc_lr(void)
393 {
394 #if defined(__arm64__)
395 const int max_cpu = ml_get_max_cpu_number();
396
397 for (int cpu = 0; cpu <= max_cpu; cpu++) {
398 cpu_data_t *current_cpu_datap = cpu_datap(cpu);
399
400 if (current_cpu_datap == NULL) {
401 continue;
402 }
403
404 if (current_cpu_datap == getCpuDatap()) {
405 /**
406 * Skip printing the PC/LR if this is the CPU
407 * that initiated the panic.
408 */
409 paniclog_append_noflush("CORE %u is the one that panicked. Check the full backtrace for details.\n", cpu);
410 continue;
411 }
412
413 paniclog_append_noflush("CORE %u: PC=0x%016llx, LR=0x%016llx, FP=0x%016llx\n", cpu,
414 current_cpu_datap->ipi_pc, (uint64_t)VM_KERNEL_STRIP_PTR(current_cpu_datap->ipi_lr),
415 (uint64_t)VM_KERNEL_STRIP_PTR(current_cpu_datap->ipi_fp));
416 }
417 #endif
418 }
419
420 #if CONFIG_EXCLAVES
421 static void
panic_report_exclaves_stackshot(void)422 panic_report_exclaves_stackshot(void)
423 {
424 if (exclaves_panic_ss_status == EXCLAVES_PANIC_STACKSHOT_FOUND) {
425 paniclog_append_noflush("** Exclaves panic stackshot found\n");
426 } else if (exclaves_panic_ss_status == EXCLAVES_PANIC_STACKSHOT_NOT_FOUND) {
427 paniclog_append_noflush("** Exclaves panic stackshot not found\n");
428 } else if (exclaves_panic_ss_status == EXCLAVES_PANIC_STACKSHOT_DECODE_FAILED) {
429 paniclog_append_noflush("!! Exclaves panic stackshot decode failed !!\n");
430 }
431 }
432 #endif /* CONFIG_EXCLAVES */
433
434 __attribute__((always_inline))
435 static inline void
print_backtrace_internal(thread_t thread,bool filesetKC)436 print_backtrace_internal(thread_t thread, bool filesetKC)
437 {
438 uintptr_t cur_fp = (uintptr_t)__builtin_frame_address(0);
439 const char *nohilite_thread_marker = "\t";
440
441 #if defined(HAS_APPLE_PAC)
442 cur_fp = (addr64_t)ptrauth_strip((void *)cur_fp, ptrauth_key_frame_pointer);
443 #endif
444
445 if (cur_fp < VM_MAX_KERNEL_ADDRESS) {
446 paniclog_append_noflush("Panicked thread: %p, backtrace: 0x%llx, tid: %llu\n",
447 thread, (addr64_t)cur_fp, thread_tid(thread));
448 #if __LP64__
449 do_print_backtrace_internal(kernel_pmap, cur_fp, nohilite_thread_marker, TRUE, filesetKC);
450 #else
451 do_print_backtrace_internal(kernel_pmap, cur_fp, nohilite_thread_marker, FALSE, filesetKC);
452 #endif
453 } else {
454 paniclog_append_noflush("Could not print panicked thread backtrace:"
455 "frame pointer outside kernel vm.\n");
456 }
457 }
458
459 static bool
is_filesetKC(void)460 is_filesetKC(void)
461 {
462 kc_format_t kc_format;
463 bool filesetKC = false;
464
465 __unused bool result = PE_get_primary_kc_format(&kc_format);
466 assert(result == true);
467 filesetKC = kc_format == KCFormatFileset;
468 return filesetKC;
469 }
470
471
472 static void
do_print_all_panic_info(const char * message,uint64_t panic_options,const char * panic_initiator)473 do_print_all_panic_info(const char *message, uint64_t panic_options, const char *panic_initiator)
474 {
475 int logversion = PANICLOG_VERSION;
476 thread_t cur_thread = current_thread();
477 task_t task;
478 struct proc *proc;
479 int print_vnodes = 0;
480
481 /* end_marker_bytes set to 200 for printing END marker + stackshot summary info always */
482 int bytes_traced = 0, bytes_remaining = 0, end_marker_bytes = 200;
483 int bytes_uncompressed = 0;
484 uint64_t bytes_used = 0ULL;
485 int err = 0;
486 char *stackshot_begin_loc = NULL;
487 bool filesetKC = is_filesetKC();
488 uint32_t panic_initiator_len = 0;
489 #if CONFIG_EXT_PANICLOG
490 uint32_t ext_paniclog_bytes = 0;
491 #endif
492
493 if (panic_bt_depth != 0) {
494 return;
495 }
496 panic_bt_depth++;
497
498 /* Truncate panic string to 1200 bytes */
499 paniclog_append_noflush("Debugger message: %.1200s\n", message);
500 if (debug_enabled) {
501 paniclog_append_noflush("Device: %s\n",
502 ('\0' != gTargetTypeBuffer[0]) ? gTargetTypeBuffer : "Not set yet");
503 paniclog_append_noflush("Hardware Model: %s\n",
504 ('\0' != gModelTypeBuffer[0]) ? gModelTypeBuffer:"Not set yet");
505 paniclog_append_noflush("ECID: %02X%02X%02X%02X%02X%02X%02X%02X\n", gPlatformECID[7],
506 gPlatformECID[6], gPlatformECID[5], gPlatformECID[4], gPlatformECID[3],
507 gPlatformECID[2], gPlatformECID[1], gPlatformECID[0]);
508 if (last_hwaccess_thread) {
509 paniclog_append_noflush("AppleHWAccess Thread: 0x%llx\n", last_hwaccess_thread);
510 if (!last_hwaccess_size) {
511 paniclog_append_noflush("AppleHWAccess last access: no access data, this is unexpected.\n");
512 } else {
513 const char *typ = last_hwaccess_type ? "write" : "read";
514 paniclog_append_noflush("AppleHWAccess last access: %s of size %u at address 0x%llx\n", typ, last_hwaccess_size, last_hwaccess_paddr);
515 }
516 }
517 paniclog_append_noflush("Boot args: %s\n", PE_boot_args());
518 }
519 paniclog_append_noflush("Memory ID: 0x%x\n", gPlatformMemoryID);
520 paniclog_append_noflush("OS release type: %.256s\n",
521 ('\0' != osreleasetype[0]) ? osreleasetype : "Not set yet");
522 paniclog_append_noflush("OS version: %.256s\n",
523 ('\0' != osversion[0]) ? osversion : "Not set yet");
524 #if defined(XNU_TARGET_OS_BRIDGE)
525 paniclog_append_noflush("macOS version: %.256s\n",
526 ('\0' != macosversion[0]) ? macosversion : "Not set");
527 #endif
528 paniclog_append_noflush("Kernel version: %.512s\n", version);
529
530 #if CONFIG_EXCLAVES
531 exclaves_panic_append_info();
532 #endif
533
534 if (kernelcache_uuid_valid) {
535 if (filesetKC) {
536 paniclog_append_noflush("Fileset Kernelcache UUID: ");
537 } else {
538 paniclog_append_noflush("KernelCache UUID: ");
539 }
540 for (size_t index = 0; index < sizeof(uuid_t); index++) {
541 paniclog_append_noflush("%02X", kernelcache_uuid[index]);
542 }
543 paniclog_append_noflush("\n");
544 }
545 panic_display_kernel_uuid();
546
547 if (bootsessionuuid_string[0] != '\0') {
548 paniclog_append_noflush("Boot session UUID: %s\n", bootsessionuuid_string);
549 } else {
550 paniclog_append_noflush("Boot session UUID not yet initialized\n");
551 }
552
553 paniclog_append_noflush("iBoot version: %.128s\n", iBoot_version);
554 #if defined(TARGET_OS_OSX) && defined(__arm64__)
555 paniclog_append_noflush("iBoot Stage 2 version: %.128s\n", iBoot_Stage_2_version);
556 #endif /* defined(TARGET_OS_OSX) && defined(__arm64__) */
557
558 paniclog_append_noflush("secure boot?: %s\n", debug_enabled ? "NO": "YES");
559 paniclog_append_noflush("roots installed: %lld\n", roots_installed);
560 #if defined(XNU_TARGET_OS_BRIDGE)
561 paniclog_append_noflush("x86 EFI Boot State: ");
562 if (PE_smc_stashed_x86_efi_boot_state != 0xFF) {
563 paniclog_append_noflush("0x%x\n", PE_smc_stashed_x86_efi_boot_state);
564 } else {
565 paniclog_append_noflush("not available\n");
566 }
567 paniclog_append_noflush("x86 System State: ");
568 if (PE_smc_stashed_x86_system_state != 0xFF) {
569 paniclog_append_noflush("0x%x\n", PE_smc_stashed_x86_system_state);
570 } else {
571 paniclog_append_noflush("not available\n");
572 }
573 paniclog_append_noflush("x86 Power State: ");
574 if (PE_smc_stashed_x86_power_state != 0xFF) {
575 paniclog_append_noflush("0x%x\n", PE_smc_stashed_x86_power_state);
576 } else {
577 paniclog_append_noflush("not available\n");
578 }
579 paniclog_append_noflush("x86 Shutdown Cause: ");
580 if (PE_smc_stashed_x86_shutdown_cause != 0xFF) {
581 paniclog_append_noflush("0x%x\n", PE_smc_stashed_x86_shutdown_cause);
582 } else {
583 paniclog_append_noflush("not available\n");
584 }
585 paniclog_append_noflush("x86 Previous Power Transitions: ");
586 if (PE_smc_stashed_x86_prev_power_transitions != UINT64_MAX) {
587 paniclog_append_noflush("0x%llx\n", PE_smc_stashed_x86_prev_power_transitions);
588 } else {
589 paniclog_append_noflush("not available\n");
590 }
591 paniclog_append_noflush("PCIeUp link state: ");
592 if (PE_pcie_stashed_link_state != UINT32_MAX) {
593 paniclog_append_noflush("0x%x\n", PE_pcie_stashed_link_state);
594 } else {
595 paniclog_append_noflush("not available\n");
596 }
597 paniclog_append_noflush("macOS kernel slide: ");
598 if (PE_nvram_stashed_x86_macos_slide != UINT64_MAX) {
599 paniclog_append_noflush("%#llx\n", PE_nvram_stashed_x86_macos_slide);
600 } else {
601 paniclog_append_noflush("not available\n");
602 }
603 #endif
604 if (panic_data_buffers != NULL) {
605 paniclog_append_noflush("%s data: ", panic_data_buffers->producer_name);
606 uint8_t *panic_buffer_data = (uint8_t *) panic_data_buffers->buf;
607 for (int i = 0; i < panic_data_buffers->len; i++) {
608 paniclog_append_noflush("%02X", panic_buffer_data[i]);
609 }
610 paniclog_append_noflush("\n");
611 }
612 paniclog_append_noflush("Paniclog version: %d\n", logversion);
613
614 panic_display_kernel_aslr();
615 panic_display_times();
616 panic_display_zalloc();
617 panic_display_hung_cpus_help();
618 panic_display_tpidrs();
619
620
621 panic_display_pvhs_locked();
622 panic_display_pvh_to_lock();
623 panic_display_last_pc_lr();
624 #if CONFIG_ECC_LOGGING
625 panic_display_ecc_errors();
626 #endif /* CONFIG_ECC_LOGGING */
627 panic_display_compressor_stats();
628
629 #if DEVELOPMENT || DEBUG
630 if (cs_debug_unsigned_exec_failures != 0 || cs_debug_unsigned_mmap_failures != 0) {
631 paniclog_append_noflush("Unsigned code exec failures: %u\n", cs_debug_unsigned_exec_failures);
632 paniclog_append_noflush("Unsigned code mmap failures: %u\n", cs_debug_unsigned_mmap_failures);
633 }
634 #endif
635
636 // Highlight threads that used high amounts of CPU in the panic log if requested (historically requested for watchdog panics)
637 if (panic_options & DEBUGGER_OPTION_PRINT_CPU_USAGE_PANICLOG) {
638 thread_t top_runnable[5] = {0};
639 thread_t thread;
640 int total_cpu_usage = 0;
641
642 print_vnodes = 1;
643
644
645 for (thread = (thread_t)queue_first(&threads);
646 PANIC_VALIDATE_PTR(thread) && !queue_end(&threads, (queue_entry_t)thread);
647 thread = (thread_t)queue_next(&thread->threads)) {
648 total_cpu_usage += thread->cpu_usage;
649
650 // Look for the 5 runnable threads with highest priority
651 if (thread->state & TH_RUN) {
652 int k;
653 thread_t comparison_thread = thread;
654
655 for (k = 0; k < TOP_RUNNABLE_LIMIT; k++) {
656 if (top_runnable[k] == 0) {
657 top_runnable[k] = comparison_thread;
658 break;
659 } else if (comparison_thread->sched_pri > top_runnable[k]->sched_pri) {
660 thread_t temp = top_runnable[k];
661 top_runnable[k] = comparison_thread;
662 comparison_thread = temp;
663 } // if comparison thread has higher priority than previously saved thread
664 } // loop through highest priority runnable threads
665 } // Check if thread is runnable
666 } // Loop through all threads
667
668 // Print the relevant info for each thread identified
669 paniclog_append_noflush("Total cpu_usage: %d\n", total_cpu_usage);
670 paniclog_append_noflush("Thread task pri cpu_usage\n");
671
672 for (int i = 0; i < TOP_RUNNABLE_LIMIT; i++) {
673 if (top_runnable[i] &&
674 panic_get_thread_proc_task(top_runnable[i], &task, &proc) && proc) {
675 char name[MAX_PROCNAME_LEN + 1];
676 proc_name_kdp(proc, name, sizeof(name));
677 paniclog_append_noflush("%p %s %d %d\n",
678 top_runnable[i], name, top_runnable[i]->sched_pri, top_runnable[i]->cpu_usage);
679 }
680 } // Loop through highest priority runnable threads
681 paniclog_append_noflush("\n");
682 }
683
684 // print current task info
685 if (panic_get_thread_proc_task(cur_thread, &task, &proc)) {
686 if (PANIC_VALIDATE_PTR(task->map) &&
687 PANIC_VALIDATE_PTR(task->map->pmap)) {
688 ledger_amount_t resident = 0;
689 if (task != kernel_task) {
690 ledger_get_balance(task->ledger, task_ledgers.phys_mem, &resident);
691 resident >>= VM_MAP_PAGE_SHIFT(task->map);
692 }
693 paniclog_append_noflush("Panicked task %p: %lld pages, %d threads: ",
694 task, resident, task->thread_count);
695 } else {
696 paniclog_append_noflush("Panicked task %p: %d threads: ",
697 task, task->thread_count);
698 }
699
700 if (proc) {
701 char name[MAX_PROCNAME_LEN + 1];
702 proc_name_kdp(proc, name, sizeof(name));
703 paniclog_append_noflush("pid %d: %s", proc_pid(proc), name);
704 } else {
705 paniclog_append_noflush("unknown task");
706 }
707
708 paniclog_append_noflush("\n");
709 }
710
711 print_backtrace_internal(cur_thread, filesetKC);
712
713 paniclog_append_noflush("\n");
714 dump_cpu_event_log(&paniclog_append_noflush);
715
716 paniclog_append_noflush("\n");
717 if (filesetKC) {
718 kext_dump_panic_lists(&paniclog_append_noflush);
719 paniclog_append_noflush("\n");
720 }
721 panic_info->eph_panic_log_len = PE_get_offset_into_panic_region(debug_buf_ptr) - panic_info->eph_panic_log_offset;
722 /* set the os version data in the panic header in the format 'Product Version (OS Version)' (only if they have been set) */
723 if ((osversion[0] != '\0') && (osproductversion[0] != '\0')) {
724 snprintf((char *)&panic_info->eph_os_version, sizeof(panic_info->eph_os_version), PANIC_HEADER_VERSION_FMT_STR,
725 osproductversion, osversion);
726 }
727 #if defined(XNU_TARGET_OS_BRIDGE)
728 if ((macosversion[0] != '\0') && (macosproductversion[0] != '\0')) {
729 snprintf((char *)&panic_info->eph_macos_version, sizeof(panic_info->eph_macos_version), PANIC_HEADER_VERSION_FMT_STR,
730 macosproductversion, macosversion);
731 }
732 #endif
733 if (bootsessionuuid_string[0] != '\0') {
734 memcpy(panic_info->eph_bootsessionuuid_string, bootsessionuuid_string,
735 sizeof(panic_info->eph_bootsessionuuid_string));
736 }
737 panic_info->eph_roots_installed = roots_installed;
738
739 if (panic_initiator != NULL) {
740 bytes_remaining = debug_buf_size - (unsigned int)((uintptr_t)debug_buf_ptr - (uintptr_t)debug_buf_base);
741 // If panic_initiator isn't null, safely copy up to MAX_PANIC_INITIATOR_SIZE
742 panic_initiator_len = strnlen(panic_initiator, MAX_PANIC_INITIATOR_SIZE);
743 // Calculate the bytes to write, accounting for remaining buffer space, and ensuring the lowest size we can have is 0
744 panic_initiator_len = MAX(0, MIN(panic_initiator_len, bytes_remaining));
745 panic_info->eph_panic_initiator_offset = (panic_initiator_len != 0) ? PE_get_offset_into_panic_region(debug_buf_ptr) : 0;
746 panic_info->eph_panic_initiator_len = panic_initiator_len;
747 memcpy(debug_buf_ptr, panic_initiator, panic_initiator_len);
748 debug_buf_ptr += panic_initiator_len;
749 }
750
751 if (debug_ack_timeout_count) {
752 panic_info->eph_panic_flags |= EMBEDDED_PANIC_HEADER_FLAG_STACKSHOT_FAILED_DEBUGGERSYNC;
753 panic_info->eph_other_log_offset = PE_get_offset_into_panic_region(debug_buf_ptr);
754 paniclog_append_noflush("!! debugger synchronization failed, no stackshot !!\n");
755 } else if (panic_stackshot_active()) {
756 panic_info->eph_panic_flags |= EMBEDDED_PANIC_HEADER_FLAG_STACKSHOT_FAILED_NESTED;
757 panic_info->eph_other_log_offset = PE_get_offset_into_panic_region(debug_buf_ptr);
758 paniclog_append_noflush("!! panicked during stackshot, skipping panic stackshot !!\n");
759 } else {
760 /* Align the stackshot buffer to an 8-byte address (especially important for armv7k devices) */
761 debug_buf_ptr += (8 - ((uintptr_t)debug_buf_ptr % 8));
762 stackshot_begin_loc = debug_buf_ptr;
763
764 bytes_remaining = debug_buf_size - (unsigned int)((uintptr_t)stackshot_begin_loc - (uintptr_t)debug_buf_base);
765 err = kcdata_memory_static_init(&kc_panic_data, (mach_vm_address_t)debug_buf_ptr,
766 KCDATA_BUFFER_BEGIN_COMPRESSED, bytes_remaining - end_marker_bytes,
767 KCFLAG_USE_MEMCOPY);
768 if (err == KERN_SUCCESS) {
769 uint64_t stackshot_flags = (STACKSHOT_GET_GLOBAL_MEM_STATS | STACKSHOT_SAVE_LOADINFO | STACKSHOT_KCDATA_FORMAT |
770 STACKSHOT_ENABLE_BT_FAULTING | STACKSHOT_ENABLE_UUID_FAULTING | STACKSHOT_FROM_PANIC | STACKSHOT_DO_COMPRESS |
771 STACKSHOT_DISABLE_LATENCY_INFO | STACKSHOT_NO_IO_STATS | STACKSHOT_THREAD_WAITINFO | STACKSHOT_GET_DQ |
772 STACKSHOT_COLLECT_SHAREDCACHE_LAYOUT);
773
774 err = kcdata_init_compress(&kc_panic_data, KCDATA_BUFFER_BEGIN_STACKSHOT, kdp_memcpy, KCDCT_ZLIB);
775 if (err != KERN_SUCCESS) {
776 panic_info->eph_panic_flags |= EMBEDDED_PANIC_HEADER_FLAG_COMPRESS_FAILED;
777 stackshot_flags &= ~STACKSHOT_DO_COMPRESS;
778 }
779 if (filesetKC) {
780 stackshot_flags |= STACKSHOT_SAVE_KEXT_LOADINFO;
781 }
782
783 kdp_snapshot_preflight(-1, stackshot_begin_loc, bytes_remaining - end_marker_bytes,
784 stackshot_flags, &kc_panic_data, 0, 0);
785 err = do_panic_stackshot(NULL);
786 bytes_traced = kdp_stack_snapshot_bytes_traced();
787 if (bytes_traced > 0 && !err) {
788 debug_buf_ptr += bytes_traced;
789 panic_info->eph_panic_flags |= EMBEDDED_PANIC_HEADER_FLAG_STACKSHOT_SUCCEEDED;
790 panic_info->eph_stackshot_offset = PE_get_offset_into_panic_region(stackshot_begin_loc);
791 panic_info->eph_stackshot_len = bytes_traced;
792
793 panic_info->eph_other_log_offset = PE_get_offset_into_panic_region(debug_buf_ptr);
794 #if CONFIG_EXCLAVES
795 panic_report_exclaves_stackshot();
796 #endif /* CONFIG_EXCLAVES */
797 if (stackshot_flags & STACKSHOT_DO_COMPRESS) {
798 panic_info->eph_panic_flags |= EMBEDDED_PANIC_HEADER_FLAG_STACKSHOT_DATA_COMPRESSED;
799 bytes_uncompressed = kdp_stack_snapshot_bytes_uncompressed();
800 paniclog_append_noflush("\n** Stackshot Succeeded ** Bytes Traced %d (Uncompressed %d) **\n", bytes_traced, bytes_uncompressed);
801 } else {
802 paniclog_append_noflush("\n** Stackshot Succeeded ** Bytes Traced %d **\n", bytes_traced);
803 }
804 } else {
805 bytes_used = kcdata_memory_get_used_bytes(&kc_panic_data);
806 #if CONFIG_EXCLAVES
807 panic_report_exclaves_stackshot();
808 #endif /* CONFIG_EXCLAVES */
809 if (bytes_used > 0) {
810 /* Zero out the stackshot data */
811 bzero(stackshot_begin_loc, bytes_used);
812 panic_info->eph_panic_flags |= EMBEDDED_PANIC_HEADER_FLAG_STACKSHOT_FAILED_INCOMPLETE;
813
814 panic_info->eph_other_log_offset = PE_get_offset_into_panic_region(debug_buf_ptr);
815 paniclog_append_noflush("\n** Stackshot Incomplete ** Bytes Filled %llu, err %d **\n", bytes_used, err);
816 } else {
817 bzero(stackshot_begin_loc, bytes_used);
818 panic_info->eph_panic_flags |= EMBEDDED_PANIC_HEADER_FLAG_STACKSHOT_FAILED_ERROR;
819
820 panic_info->eph_other_log_offset = PE_get_offset_into_panic_region(debug_buf_ptr);
821 paniclog_append_noflush("\n!! Stackshot Failed !! Bytes Traced %d, err %d\n", bytes_traced, err);
822 }
823 }
824 } else {
825 panic_info->eph_panic_flags |= EMBEDDED_PANIC_HEADER_FLAG_STACKSHOT_FAILED_ERROR;
826 panic_info->eph_other_log_offset = PE_get_offset_into_panic_region(debug_buf_ptr);
827 paniclog_append_noflush("\n!! Stackshot Failed !!\nkcdata_memory_static_init returned %d", err);
828 }
829 }
830
831 ksancov_on_panic_log();
832
833 #if CONFIG_EXT_PANICLOG
834 // Write ext paniclog at the end of the paniclog region.
835 ext_paniclog_bytes = ext_paniclog_write_panicdata();
836 panic_info->eph_ext_paniclog_offset = (ext_paniclog_bytes != 0) ?
837 PE_get_offset_into_panic_region((debug_buf_base + debug_buf_size) - ext_paniclog_bytes) :
838 0;
839 panic_info->eph_ext_paniclog_len = ext_paniclog_bytes;
840 #endif
841
842 assert(panic_info->eph_other_log_offset != 0);
843
844 if (print_vnodes != 0) {
845 panic_print_vnodes();
846 }
847
848 panic_bt_depth--;
849 }
850
851 /*
852 * Entry to print_all_panic_info is serialized by the debugger lock
853 */
854 static void
print_all_panic_info(const char * message,uint64_t panic_options,const char * panic_initiator)855 print_all_panic_info(const char *message, uint64_t panic_options, const char *panic_initiator)
856 {
857 unsigned int initial_not_in_kdp = not_in_kdp;
858
859 cpu_data_t * cpu_data_ptr = getCpuDatap();
860
861 assert(cpu_data_ptr->PAB_active == FALSE);
862 cpu_data_ptr->PAB_active = TRUE;
863
864 /*
865 * Because print all backtraces uses the pmap routines, it needs to
866 * avoid taking pmap locks. Right now, this is conditionalized on
867 * not_in_kdp.
868 */
869 not_in_kdp = 0;
870 do_print_all_panic_info(message, panic_options, panic_initiator);
871
872 not_in_kdp = initial_not_in_kdp;
873
874 cpu_data_ptr->PAB_active = FALSE;
875 }
876
877 void
print_curr_backtrace(void)878 print_curr_backtrace(void)
879 {
880 print_backtrace_internal(current_thread(), is_filesetKC());
881 }
882
883 void
panic_display_times()884 panic_display_times()
885 {
886 if (kdp_clock_is_locked()) {
887 paniclog_append_noflush("Warning: clock is locked. Can't get time\n");
888 return;
889 }
890
891 extern lck_ticket_t clock_lock;
892 extern lck_grp_t clock_lock_grp;
893
894 if ((is_clock_configured) && (lck_ticket_lock_try(&clock_lock, &clock_lock_grp))) {
895 clock_sec_t secs, boot_secs;
896 clock_usec_t usecs, boot_usecs;
897
898 lck_ticket_unlock(&clock_lock);
899
900 clock_get_calendar_microtime(&secs, &usecs);
901 clock_get_boottime_microtime(&boot_secs, &boot_usecs);
902
903 paniclog_append_noflush("mach_absolute_time: 0x%llx\n", mach_absolute_time());
904 paniclog_append_noflush("Epoch Time: sec usec\n");
905 paniclog_append_noflush(" Boot : 0x%08x 0x%08x\n", (unsigned int)boot_secs, (unsigned int)boot_usecs);
906 paniclog_append_noflush(" Sleep : 0x%08x 0x%08x\n", (unsigned int)gIOLastSleepTime.tv_sec, (unsigned int)gIOLastSleepTime.tv_usec);
907 paniclog_append_noflush(" Wake : 0x%08x 0x%08x\n", (unsigned int)gIOLastWakeTime.tv_sec, (unsigned int)gIOLastWakeTime.tv_usec);
908 paniclog_append_noflush(" Calendar: 0x%08x 0x%08x\n\n", (unsigned int)secs, (unsigned int)usecs);
909 }
910 }
911
912 void
panic_print_symbol_name(vm_address_t search)913 panic_print_symbol_name(vm_address_t search)
914 {
915 #pragma unused(search)
916 // empty stub. Really only used on x86_64.
917 return;
918 }
919
920 void
SavePanicInfo(const char * message,__unused void * panic_data,uint64_t panic_options,const char * panic_initiator)921 SavePanicInfo(
922 const char *message, __unused void *panic_data, uint64_t panic_options, const char* panic_initiator)
923 {
924 /*
925 * This should be initialized by the time we get here, but
926 * if it is not, asserting about it will be of no use (it will
927 * come right back to here), so just loop right here and now.
928 * This prevents early-boot panics from becoming recursive and
929 * thus makes them easier to debug. If you attached to a device
930 * and see your PC here, look down a few frames to see your
931 * early-boot panic there.
932 */
933 while (!panic_info || panic_info->eph_panic_log_offset == 0) {
934 // rdar://87170225 (PanicHardening: audit panic code for naked spinloops)
935 // rdar://88094367 (Add test hooks for panic at different stages in XNU)
936 ;
937 }
938
939 if (panic_options & DEBUGGER_OPTION_PANICLOGANDREBOOT) {
940 panic_info->eph_panic_flags |= EMBEDDED_PANIC_HEADER_FLAG_BUTTON_RESET_PANIC;
941 }
942
943 if (panic_options & DEBUGGER_OPTION_COMPANION_PROC_INITIATED_PANIC) {
944 panic_info->eph_panic_flags |= EMBEDDED_PANIC_HEADER_FLAG_COMPANION_PROC_INITIATED_PANIC;
945 }
946
947 if (panic_options & DEBUGGER_OPTION_INTEGRATED_COPROC_INITIATED_PANIC) {
948 panic_info->eph_panic_flags |= EMBEDDED_PANIC_HEADER_FLAG_INTEGRATED_COPROC_INITIATED_PANIC;
949 }
950
951 if (panic_options & DEBUGGER_OPTION_USERSPACE_INITIATED_PANIC) {
952 panic_info->eph_panic_flags |= EMBEDDED_PANIC_HEADER_FLAG_USERSPACE_INITIATED_PANIC;
953 }
954
955 #if defined(XNU_TARGET_OS_BRIDGE)
956 panic_info->eph_x86_power_state = PE_smc_stashed_x86_power_state;
957 panic_info->eph_x86_efi_boot_state = PE_smc_stashed_x86_efi_boot_state;
958 panic_info->eph_x86_system_state = PE_smc_stashed_x86_system_state;
959 #endif
960
961 /*
962 * On newer targets, panic data is stored directly into the iBoot panic region.
963 * If we re-enter SavePanicInfo (e.g. on a double panic) on such a target, update the
964 * panic CRC so that iBoot can hopefully find *something* useful in the panic region.
965 */
966 if (PanicInfoSaved && (debug_buf_base >= (char*)gPanicBase) && (debug_buf_base < (char*)gPanicBase + gPanicSize)) {
967 unsigned int pi_size = (unsigned int)(debug_buf_ptr - gPanicBase);
968 PE_update_panic_crc((unsigned char*)gPanicBase, &pi_size);
969 PE_sync_panic_buffers(); // extra precaution; panic path likely isn't reliable if we're here
970 }
971
972 if (PanicInfoSaved || (debug_buf_size == 0)) {
973 return;
974 }
975
976 PanicInfoSaved = TRUE;
977
978
979 print_all_panic_info(message, panic_options, panic_initiator);
980
981 assert(panic_info->eph_panic_log_len != 0);
982 panic_info->eph_other_log_len = PE_get_offset_into_panic_region(debug_buf_ptr) - panic_info->eph_other_log_offset;
983
984 PEHaltRestart(kPEPanicSync);
985
986 /*
987 * Notifies registered IOPlatformPanicAction callbacks
988 * (which includes one to disable the memcache) and flushes
989 * the buffer contents from the cache
990 */
991 paniclog_flush();
992 }
993
994 void
paniclog_flush()995 paniclog_flush()
996 {
997 unsigned int panicbuf_length = 0;
998
999 panicbuf_length = (unsigned int)(debug_buf_ptr - gPanicBase);
1000 if (!debug_buf_ptr || !panicbuf_length) {
1001 return;
1002 }
1003
1004 /*
1005 * Updates the log length of the last part of the panic log.
1006 */
1007 panic_info->eph_other_log_len = PE_get_offset_into_panic_region(debug_buf_ptr) - panic_info->eph_other_log_offset;
1008
1009 /*
1010 * Updates the metadata at the beginning of the panic buffer,
1011 * updates the CRC.
1012 */
1013 PE_update_panic_crc((unsigned char *)gPanicBase, &panicbuf_length);
1014
1015 /*
1016 * This is currently unused by platform KEXTs on embedded but is
1017 * kept for compatibility with the published IOKit interfaces.
1018 */
1019 PESavePanicInfo((unsigned char *)gPanicBase, panicbuf_length);
1020
1021 PE_sync_panic_buffers();
1022 }
1023
1024 #if CONFIG_SPTM
1025 /*
1026 * Patch thread state to appear as if a debugger stop IPI occurred, when a thread
1027 * is parked in SPTM panic loop. This allows stackshot to proceed as usual.
1028 */
1029 static void
DebuggerPatchupThreadState(int cpu,xnu_saved_registers_t * regp)1030 DebuggerPatchupThreadState(
1031 int cpu, xnu_saved_registers_t *regp)
1032 {
1033 cpu_data_t *target_cpu_datap;
1034 arm_saved_state_t *statep;
1035 vm_offset_t kstackptr;
1036
1037 target_cpu_datap = (cpu_data_t *)CpuDataEntries[cpu].cpu_data_vaddr;
1038 statep = target_cpu_datap->cpu_active_thread->machine.kpcb;
1039 kstackptr = (vm_offset_t)target_cpu_datap->cpu_active_thread->machine.kstackptr;
1040
1041 target_cpu_datap->ipi_pc = regp->pc;
1042 target_cpu_datap->ipi_lr = regp->lr;
1043 target_cpu_datap->ipi_fp = regp->fp;
1044
1045 if (statep != NULL) {
1046 statep->ss_64.fp = regp->fp;
1047 statep->ss_64.lr = regp->lr;
1048 statep->ss_64.sp = regp->sp;
1049 statep->ss_64.pc = regp->pc;
1050 } else if ((void *)kstackptr != NULL) {
1051 arm_kernel_saved_state_t *kstatep = (arm_kernel_saved_state_t *)kstackptr;
1052 kstatep->fp = regp->fp;
1053 kstatep->lr = regp->lr;
1054 kstatep->sp = regp->sp;
1055 }
1056 }
1057 #endif
1058
1059 /*
1060 * @function DebuggerXCallEnter
1061 *
1062 * @abstract IPI other cores so this core can run in a single-threaded context.
1063 *
1064 * @discussion This function should be called with the debugger lock held. It
1065 * signals the other cores to go into a busy loop so this core can run in a
1066 * single-threaded context and inspect kernel memory.
1067 *
1068 * @param proceed_on_sync_failure If true, then go ahead and try to debug even
1069 * if we can't synch with the other cores. This is inherently unsafe and should
1070 * only be used if the kernel is going down in flames anyway.
1071 *
1072 * @param is_stackshot If true, this is a stackshot request.
1073 *
1074 * @result returns KERN_OPERATION_TIMED_OUT if synchronization times out and
1075 * proceed_on_sync_failure is false.
1076 */
1077 kern_return_t
DebuggerXCallEnter(boolean_t proceed_on_sync_failure,bool is_stackshot)1078 DebuggerXCallEnter(
1079 boolean_t proceed_on_sync_failure, bool is_stackshot)
1080 {
1081 uint64_t max_mabs_time, current_mabs_time;
1082 int cpu;
1083 int timeout_cpu = -1;
1084 int max_cpu;
1085 unsigned int sync_pending;
1086 cpu_data_t *target_cpu_datap;
1087 cpu_data_t *cpu_data_ptr = getCpuDatap();
1088
1089 /* Check for nested debugger entry. */
1090 cpu_data_ptr->debugger_active++;
1091 if (cpu_data_ptr->debugger_active != 1) {
1092 return KERN_SUCCESS;
1093 }
1094
1095 /*
1096 * If debugger_sync is not 0, someone responded excessively late to the last
1097 * debug request (we zero the sync variable in the return function). Zero it
1098 * again here. This should prevent us from getting out of sync (heh) and
1099 * timing out on every entry to the debugger if we timeout once.
1100 */
1101
1102 os_atomic_store(&debugger_sync, 0, relaxed);
1103 os_atomic_store(&mp_kdp_trap, 1, relaxed);
1104 os_atomic_store(&debug_cpus_spinning, 0, relaxed);
1105 trap_is_stackshot = is_stackshot;
1106
1107
1108 /*
1109 * Try to signal all CPUs (except ourselves, of course). Use debugger_sync to
1110 * synchronize with every CPU that we appeared to signal successfully (cpu_signal
1111 * is not synchronous).
1112 */
1113 max_cpu = ml_get_max_cpu_number();
1114
1115 boolean_t immediate_halt = FALSE;
1116 if (proceed_on_sync_failure && force_immediate_debug_halt) {
1117 immediate_halt = TRUE;
1118 }
1119
1120 if (!immediate_halt) {
1121 for (cpu = 0; cpu <= max_cpu; cpu++) {
1122 target_cpu_datap = (cpu_data_t *)CpuDataEntries[cpu].cpu_data_vaddr;
1123
1124 if ((target_cpu_datap == NULL) || (target_cpu_datap == cpu_data_ptr)) {
1125 continue;
1126 }
1127
1128 kern_return_t ret = cpu_signal(target_cpu_datap, SIGPdebug, (void *)NULL, NULL);
1129 if (ret == KERN_SUCCESS) {
1130 os_atomic_inc(&debugger_sync, relaxed);
1131 os_atomic_inc(&debug_cpus_spinning, relaxed);
1132 } else {
1133 kprintf("%s: cpu_signal failed. cpu=%d ret=%d proceed=%d\n", __func__, cpu, ret, proceed_on_sync_failure);
1134 }
1135 }
1136
1137 max_mabs_time = os_atomic_load(&debug_ack_timeout, relaxed);
1138
1139 if (max_mabs_time > 0) {
1140 current_mabs_time = mach_absolute_time();
1141 max_mabs_time += current_mabs_time;
1142 assert(max_mabs_time > current_mabs_time);
1143 }
1144
1145 /*
1146 * Wait for DEBUG_ACK_TIMEOUT ns for a response from everyone we IPI'd. If we
1147 * timeout, that is simply too bad; we don't have a true NMI, and one CPU may be
1148 * uninterruptibly spinning on someone else. The best we can hope for is that
1149 * all other CPUs have either responded or are spinning in a context that is
1150 * debugger safe.
1151 */
1152 do {
1153 current_mabs_time = mach_absolute_time();
1154 sync_pending = os_atomic_load(&debugger_sync, acquire);
1155 } while ((sync_pending != 0) && (max_mabs_time == 0 || current_mabs_time < max_mabs_time));
1156 }
1157
1158 if (!immediate_halt && max_mabs_time > 0 && current_mabs_time >= max_mabs_time) {
1159 /*
1160 * We timed out trying to IPI the other CPUs. Skip counting any CPUs that
1161 * are offline; then we must account for the remainder, either counting
1162 * them as halted, or trying to dbgwrap them to get them to halt in the
1163 * case where the system is going down and we are running a dev fused
1164 * device.
1165 */
1166 __builtin_arm_dmb(DMB_ISH);
1167 for (cpu = 0; cpu <= max_cpu; cpu++) {
1168 target_cpu_datap = (cpu_data_t *)CpuDataEntries[cpu].cpu_data_vaddr;
1169
1170 if ((target_cpu_datap == NULL) || (target_cpu_datap == cpu_data_ptr)) {
1171 continue;
1172 }
1173 if (!(target_cpu_datap->cpu_signal & SIGPdebug)) {
1174 continue;
1175 }
1176 if (processor_array[cpu]->state <= PROCESSOR_PENDING_OFFLINE) {
1177 int dbg_sync_count;
1178
1179 /*
1180 * This is a processor that was successfully sent a SIGPdebug signal
1181 * but which hasn't acknowledged it because it went offline with
1182 * interrupts disabled before the IPI was delivered, so count it
1183 * as halted here.
1184 */
1185 dbg_sync_count = os_atomic_dec(&debugger_sync, relaxed);
1186 kprintf("%s>found CPU %d offline, debugger_sync=%d\n", __FUNCTION__, cpu, dbg_sync_count);
1187 continue;
1188 }
1189 kprintf("%s>Debugger synch pending on cpu %d\n", __FUNCTION__, cpu);
1190 timeout_cpu = cpu;
1191 #if CONFIG_SPTM
1192 if (proceed_on_sync_failure) {
1193 /*
1194 * If a core is spinning in the SPTM panic loop, consider it
1195 * as sync'd, and try to patch up the thread state from the
1196 * SPTM callee saved registers.
1197 */
1198 bool sptm_panic_loop = false;
1199 vm_offset_t base = other_percpu_base(cpu);
1200 pmap_sptm_percpu_data_t *sptm_pcpu = PERCPU_GET_WITH_BASE(base, pmap_sptm_percpu);
1201 uint64_t sptm_cpuid = sptm_pcpu->sptm_cpu_id;
1202
1203 if (sptm_get_cpu_state(sptm_cpuid, CPUSTATE_PANIC_SPIN, &sptm_panic_loop)
1204 == SPTM_SUCCESS && sptm_panic_loop) {
1205 xnu_saved_registers_t regs;
1206
1207 if (sptm_copy_callee_saved_state(sptm_cpuid, ®s)
1208 == LIBSPTM_SUCCESS) {
1209 DebuggerPatchupThreadState(cpu, ®s);
1210 }
1211
1212 kprintf("%s>found CPU %d in SPTM\n", __FUNCTION__, cpu);
1213 os_atomic_dec(&debugger_sync, relaxed);
1214 }
1215 }
1216 #endif
1217 }
1218
1219 if (debugger_sync == 0) {
1220 return KERN_SUCCESS;
1221 } else if (!proceed_on_sync_failure) {
1222 panic("%s>Debugger synch pending on cpu %d\n",
1223 __FUNCTION__, timeout_cpu);
1224 }
1225 }
1226 if (immediate_halt || (max_mabs_time > 0 && current_mabs_time >= max_mabs_time)) {
1227 if (immediate_halt) {
1228 __builtin_arm_dmb(DMB_ISH);
1229 }
1230 for (cpu = 0; cpu <= max_cpu; cpu++) {
1231 target_cpu_datap = (cpu_data_t *)CpuDataEntries[cpu].cpu_data_vaddr;
1232
1233 if ((target_cpu_datap == NULL) || (target_cpu_datap == cpu_data_ptr)) {
1234 continue;
1235 }
1236 paniclog_append_noflush("Attempting to forcibly halt cpu %d\n", cpu);
1237 dbgwrap_status_t halt_status = ml_dbgwrap_halt_cpu(cpu, 0);
1238 if (halt_status < 0) {
1239 paniclog_append_noflush("cpu %d failed to halt with error %d: %s\n", cpu, halt_status, ml_dbgwrap_strerror(halt_status));
1240 } else {
1241 if (halt_status > 0) {
1242 paniclog_append_noflush("cpu %d halted with warning %d: %s\n", cpu, halt_status, ml_dbgwrap_strerror(halt_status));
1243 }
1244 target_cpu_datap->halt_status = CPU_HALTED;
1245 }
1246 }
1247 for (cpu = 0; cpu <= max_cpu; cpu++) {
1248 target_cpu_datap = (cpu_data_t *)CpuDataEntries[cpu].cpu_data_vaddr;
1249
1250 if ((target_cpu_datap == NULL) || (target_cpu_datap == cpu_data_ptr)) {
1251 continue;
1252 }
1253 dbgwrap_status_t halt_status = ml_dbgwrap_halt_cpu_with_state(cpu,
1254 NSEC_PER_SEC, &target_cpu_datap->halt_state);
1255 if ((halt_status < 0) || (halt_status == DBGWRAP_WARN_CPU_OFFLINE)) {
1256 paniclog_append_noflush("Unable to obtain state for cpu %d with status %d: %s\n", cpu, halt_status, ml_dbgwrap_strerror(halt_status));
1257 debug_ack_timeout_count++;
1258 } else {
1259 paniclog_append_noflush("cpu %d successfully halted\n", cpu);
1260 target_cpu_datap->halt_status = CPU_HALTED_WITH_STATE;
1261 }
1262 }
1263 if (immediate_halt) {
1264 paniclog_append_noflush("Immediate halt requested on all cores\n");
1265 } else {
1266 paniclog_append_noflush("Debugger synchronization timed out; timeout %llu nanoseconds\n",
1267 os_atomic_load(&debug_ack_timeout, relaxed));
1268 }
1269 }
1270 return KERN_SUCCESS;
1271 }
1272
1273 /*
1274 * @function DebuggerXCallReturn
1275 *
1276 * @abstract Resume normal multicore operation after DebuggerXCallEnter()
1277 *
1278 * @discussion This function should be called with debugger lock held.
1279 */
1280 void
DebuggerXCallReturn(void)1281 DebuggerXCallReturn(
1282 void)
1283 {
1284 cpu_data_t *cpu_data_ptr = getCpuDatap();
1285 uint64_t max_mabs_time, current_mabs_time;
1286
1287 cpu_data_ptr->debugger_active--;
1288 if (cpu_data_ptr->debugger_active != 0) {
1289 return;
1290 }
1291
1292 os_atomic_store(&mp_kdp_trap, 0, release);
1293 os_atomic_store(&debugger_sync, 0, relaxed);
1294
1295 max_mabs_time = os_atomic_load(&debug_ack_timeout, relaxed);
1296
1297 if (max_mabs_time > 0) {
1298 current_mabs_time = mach_absolute_time();
1299 max_mabs_time += current_mabs_time;
1300 assert(max_mabs_time > current_mabs_time);
1301 }
1302
1303 /*
1304 * Wait for other CPUs to stop spinning on mp_kdp_trap (see DebuggerXCall).
1305 * It's possible for one or more CPUs to not decrement debug_cpus_spinning,
1306 * since they may be stuck somewhere else with interrupts disabled.
1307 * Wait for DEBUG_ACK_TIMEOUT ns for a response and move on if we don't get it.
1308 *
1309 * Note that the same is done in DebuggerXCallEnter, when we wait for other
1310 * CPUS to update debugger_sync. If we time out, let's hope for all CPUs to be
1311 * spinning in a debugger-safe context
1312 */
1313 while ((os_atomic_load_exclusive(&debug_cpus_spinning, acquire) != 0) &&
1314 (max_mabs_time == 0 || current_mabs_time < max_mabs_time)) {
1315 __builtin_arm_wfe();
1316 current_mabs_time = mach_absolute_time();
1317 }
1318 os_atomic_clear_exclusive();
1319
1320 // checking debug_ack_timeout != 0 is a workaround for rdar://124242354
1321 if (current_mabs_time >= max_mabs_time && os_atomic_load(&debug_ack_timeout, relaxed) != 0) {
1322 panic("Resuming from debugger synchronization failed: waited %llu nanoseconds\n", os_atomic_load(&debug_ack_timeout, relaxed));
1323 }
1324 }
1325
1326 extern void wait_while_mp_kdp_trap(bool check_SIGPdebug);
1327 /*
1328 * Spin while mp_kdp_trap is set.
1329 *
1330 * processor_offline() calls this with check_SIGPdebug=true
1331 * to break out of the spin loop if the cpu has SIGPdebug
1332 * pending.
1333 */
1334 void
wait_while_mp_kdp_trap(bool check_SIGPdebug)1335 wait_while_mp_kdp_trap(bool check_SIGPdebug)
1336 {
1337 bool found_mp_kdp_trap = false;
1338 bool found_SIGPdebug = false;
1339
1340 while (os_atomic_load_exclusive(&mp_kdp_trap, acquire) != 0) {
1341 found_mp_kdp_trap = true;
1342 if (check_SIGPdebug && cpu_has_SIGPdebug_pending()) {
1343 found_SIGPdebug = true;
1344 break;
1345 }
1346 __builtin_arm_wfe();
1347 }
1348 os_atomic_clear_exclusive();
1349
1350 if (check_SIGPdebug && found_mp_kdp_trap) {
1351 kprintf("%s>found_mp_kdp_trap=true found_SIGPdebug=%s\n", __FUNCTION__, found_SIGPdebug ? "true" : "false");
1352 }
1353 }
1354
1355 void
DebuggerXCall(void * ctx)1356 DebuggerXCall(
1357 void *ctx)
1358 {
1359 boolean_t save_context = FALSE;
1360 vm_offset_t kstackptr = 0;
1361 arm_saved_state_t *regs = (arm_saved_state_t *) ctx;
1362
1363 if (regs != NULL) {
1364 #if defined(__arm64__)
1365 current_cpu_datap()->ipi_pc = (uint64_t)get_saved_state_pc(regs);
1366 current_cpu_datap()->ipi_lr = (uint64_t)get_saved_state_lr(regs);
1367 current_cpu_datap()->ipi_fp = (uint64_t)get_saved_state_fp(regs);
1368 save_context = PSR64_IS_KERNEL(get_saved_state_cpsr(regs));
1369 #endif
1370 }
1371
1372 kstackptr = (vm_offset_t)current_thread()->machine.kstackptr;
1373
1374 #if defined(__arm64__)
1375 arm_kernel_saved_state_t *state = (arm_kernel_saved_state_t *)kstackptr;
1376
1377 if (save_context) {
1378 /* Save the interrupted context before acknowledging the signal */
1379 current_thread()->machine.kpcb = regs;
1380 } else if (regs) {
1381 /* zero old state so machine_trace_thread knows not to backtrace it */
1382 state->fp = 0;
1383 state->pc_was_in_userspace = true;
1384 state->lr = 0;
1385 state->sp = 0;
1386 state->ssbs = 0;
1387 state->uao = 0;
1388 state->dit = 0;
1389 #if HAS_MTE
1390 state->tco = 0;
1391 #endif
1392 }
1393 #endif
1394
1395 /*
1396 * When running in serial mode, the core capturing the dump may hold interrupts disabled
1397 * for a time longer than the timeout. That path includes logic to reset the timestamp
1398 * so that we do not eventually trigger the interrupt timeout assert().
1399 *
1400 * Here we check whether other cores have already gone over the timeout at this point
1401 * before spinning, so we at least cover the IPI reception path. After spinning, however,
1402 * we reset the timestamp so as to avoid hitting the interrupt timeout assert().
1403 */
1404 if ((serialmode & SERIALMODE_OUTPUT) || trap_is_stackshot) {
1405 ml_interrupt_masked_debug_end();
1406 }
1407
1408 /*
1409 * Before we decrement debugger sync, do stackshot preflight work (if applicable).
1410 * Namely, we want to signal that we're available to do stackshot work, and we need to
1411 * signal so before the stackshot-calling CPU starts work.
1412 */
1413
1414 if (trap_is_stackshot) {
1415 stackshot_cpu_preflight();
1416 }
1417
1418 os_atomic_dec(&debugger_sync, release);
1419
1420 /* If we trapped because we're doing a stackshot, do our work first. */
1421 if (trap_is_stackshot) {
1422 stackshot_aux_cpu_entry();
1423 }
1424
1425
1426 wait_while_mp_kdp_trap(false);
1427
1428 /**
1429 * Alert the triggering CPU that this CPU is done spinning. The CPU that
1430 * signalled all of the other CPUs will wait (in DebuggerXCallReturn) for
1431 * all of the CPUs to exit the above loop before continuing.
1432 */
1433 os_atomic_dec(&debug_cpus_spinning, release);
1434
1435 #if SCHED_HYGIENE_DEBUG
1436 /*
1437 * We also abandon the measurement for preemption disable
1438 * timeouts, if any. Normally, time in interrupt handlers would be
1439 * subtracted from preemption disable time, and this will happen
1440 * up to this point here, but since we here "end" the interrupt
1441 * handler prematurely (from the point of view of interrupt masked
1442 * debugging), the time spinning would otherwise still be
1443 * attributed to preemption disable time, and potentially trigger
1444 * an event, which could be a panic.
1445 */
1446 abandon_preemption_disable_measurement();
1447
1448 if ((serialmode & SERIALMODE_OUTPUT) || trap_is_stackshot) {
1449 ml_interrupt_masked_debug_start((void *)current_thread()->machine.int_handler_addr, current_thread()->machine.int_type);
1450 }
1451 #endif /* SCHED_HYGIENE_DEBUG */
1452
1453 #if defined(__arm64__)
1454 current_thread()->machine.kpcb = NULL;
1455 #endif /* defined(__arm64__) */
1456
1457 /* Any cleanup for our pushed context should go here */
1458 }
1459
1460 void
DebuggerCall(unsigned int reason,void * ctx)1461 DebuggerCall(
1462 unsigned int reason,
1463 void *ctx)
1464 {
1465 #if !MACH_KDP
1466 #pragma unused(reason,ctx)
1467 #endif /* !MACH_KDP */
1468
1469 #if ALTERNATE_DEBUGGER
1470 alternate_debugger_enter();
1471 #endif
1472
1473 #if MACH_KDP
1474 kdp_trap(reason, (struct arm_saved_state *)ctx);
1475 #else
1476 /* TODO: decide what to do if no debugger config */
1477 #endif
1478 }
1479
1480 boolean_t
bootloader_valid_page(ppnum_t ppn)1481 bootloader_valid_page(ppnum_t ppn)
1482 {
1483 return pmap_bootloader_page(ppn);
1484 }
1485