2 * Copyright (c) 2007-2013 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 #include <kern/thread.h>
33 #include <machine/pmap.h>
34 #include <device/device_types.h>
36 #include <mach/vm_param.h>
37 #include <mach/clock_types.h>
38 #include <mach/machine.h>
39 #include <mach/kmod.h>
40 #include <pexpert/boot.h>
41 #include <pexpert/pexpert.h>
44 #include <kern/misc_protos.h>
45 #include <kern/startup.h>
46 #include <kern/clock.h>
47 #include <kern/debug.h>
48 #include <kern/processor.h>
49 #include <kdp/kdp_core.h>
50 #if ALTERNATE_DEBUGGER
51 #include <arm64/alternate_debugger.h>
53 #include <machine/atomic.h>
54 #include <machine/trap.h>
56 #include <pexpert/pexpert.h>
57 #include <kdp/kdp_callout.h>
58 #include <kdp/kdp_dyld.h>
59 #include <kdp/kdp_internal.h>
60 #include <uuid/uuid.h>
61 #include <sys/codesign.h>
64 #include <IOKit/IOPlatformExpert.h>
66 #include <mach/vm_prot.h>
67 #include <vm/vm_map.h>
69 #include <vm/vm_shared_region.h>
70 #include <mach/time_value.h>
71 #include <machine/machparam.h> /* for btop */
73 #include <console/video_console.h>
74 #include <arm/cpu_data.h>
75 #include <arm/cpu_data_internal.h>
76 #include <arm/cpu_internal.h>
77 #include <arm/misc_protos.h>
78 #include <libkern/OSKextLibPrivate.h>
79 #include <vm/vm_kern.h>
80 #include <kern/kern_cdata.h>
83 void kdp_trap(unsigned int, struct arm_saved_state
*);
86 extern kern_return_t
do_stackshot(void *);
87 extern void kdp_snapshot_preflight(int pid
, void *tracebuf
,
88 uint32_t tracebuf_size
, uint32_t flags
,
89 kcdata_descriptor_t data_p
,
90 boolean_t enable_faulting
);
91 extern int kdp_stack_snapshot_bytes_traced(void);
94 * Increment the PANICLOG_VERSION if you change the format of the panic
97 #define PANICLOG_VERSION 13
98 static struct kcdata_descriptor kc_panic_data
;
100 extern char firmware_version
[];
101 extern volatile uint32_t debug_enabled
;
102 extern unsigned int not_in_kdp
;
104 extern int copyinframe(vm_address_t fp
, uint32_t * frame
);
105 extern void kdp_callouts(kdp_event_t event
);
107 /* #include <sys/proc.h> */
109 extern int proc_pid(void *p
);
110 extern void proc_name_kdp(task_t
, char *, int);
113 * Make sure there's enough space to include the relevant bits in the format required
114 * within the space allocated for the panic version string in the panic header.
115 * The format required by OSAnalytics/DumpPanic is 'Product Version (OS Version)'
117 #define PANIC_HEADER_VERSION_FMT_STR "%.14s (%.14s)"
119 extern const char version
[];
120 extern char osversion
[];
121 extern char osproductversion
[];
123 #if defined(XNU_TARGET_OS_BRIDGE)
124 extern char macosproductversion
[];
125 extern char macosversion
[];
128 extern uint8_t gPlatformECID
[8];
129 extern uint32_t gPlatformMemoryID
;
131 extern uint64_t last_hwaccess_thread
;
133 /*Choosing the size for gTargetTypeBuffer as 8 and size for gModelTypeBuffer as 32
134 * since the target name and model name typically doesn't exceed this size */
135 extern char gTargetTypeBuffer
[8];
136 extern char gModelTypeBuffer
[32];
138 decl_simple_lock_data(extern, clock_lock
)
139 extern struct timeval gIOLastSleepTime
;
140 extern struct timeval gIOLastWakeTime
;
141 extern boolean_t is_clock_configured
;
142 extern boolean_t kernelcache_uuid_valid
;
143 extern uuid_t kernelcache_uuid
;
145 /* Definitions for frame pointers */
146 #define FP_ALIGNMENT_MASK ((uint32_t)(0x3))
147 #define FP_LR_OFFSET ((uint32_t)4)
148 #define FP_LR_OFFSET64 ((uint32_t)8)
149 #define FP_MAX_NUM_TO_EVALUATE (50)
151 /* Timeout (in nanoseconds) for all processors responding to debug crosscall */
152 #define DEBUG_ACK_TIMEOUT ((uint64_t) 10000000)
154 /* Forward functions definitions */
155 void panic_display_times(void);
156 void panic_print_symbol_name(vm_address_t search
);
159 /* Global variables */
160 static uint32_t panic_bt_depth
;
161 boolean_t PanicInfoSaved
= FALSE
;
162 boolean_t force_immediate_debug_halt
= FALSE
;
163 unsigned int debug_ack_timeout_count
= 0;
164 volatile unsigned int debugger_sync
= 0;
165 volatile unsigned int mp_kdp_trap
= 0; /* CPUs signalled by the debug CPU will spin on this */
166 unsigned int DebugContextCount
= 0;
168 #if defined(__arm64__)
169 uint8_t PE_smc_stashed_x86_system_state
= 0xFF;
170 uint8_t PE_smc_stashed_x86_power_state
= 0xFF;
171 uint8_t PE_smc_stashed_x86_efi_boot_state
= 0xFF;
172 uint8_t PE_smc_stashed_x86_shutdown_cause
= 0xFF;
173 uint64_t PE_smc_stashed_x86_prev_power_transitions
= UINT64_MAX
;
174 uint32_t PE_pcie_stashed_link_state
= UINT32_MAX
;
178 // Convenient macros to easily validate one or more pointers if
179 // they have defined types
180 #define VALIDATE_PTR(ptr) \
181 validate_ptr((vm_offset_t)(ptr), sizeof(*(ptr)), #ptr)
183 #define VALIDATE_PTR_2(ptr0, ptr1) \
184 VALIDATE_PTR(ptr0) && VALIDATE_PTR(ptr1)
186 #define VALIDATE_PTR_3(ptr0, ptr1, ptr2) \
187 VALIDATE_PTR_2(ptr0, ptr1) && VALIDATE_PTR(ptr2)
189 #define VALIDATE_PTR_4(ptr0, ptr1, ptr2, ptr3) \
190 VALIDATE_PTR_2(ptr0, ptr1) && VALIDATE_PTR_2(ptr2, ptr3)
192 #define GET_MACRO(_1, _2, _3, _4, NAME, ...) NAME
194 #define VALIDATE_PTR_LIST(...) GET_MACRO(__VA_ARGS__, VALIDATE_PTR_4, VALIDATE_PTR_3, VALIDATE_PTR_2, VALIDATE_PTR)(__VA_ARGS__)
197 * Evaluate if a pointer is valid
198 * Print a message if pointer is invalid
202 vm_offset_t ptr
, vm_size_t size
, const char * ptr_name
)
205 if (ml_validate_nofault(ptr
, size
)) {
208 paniclog_append_noflush("Invalid %s pointer: %p size: %d\n",
209 ptr_name
, (void *)ptr
, (int)size
);
213 paniclog_append_noflush("NULL %s pointer\n", ptr_name
);
219 * Backtrace a single frame.
222 print_one_backtrace(pmap_t pmap
, vm_offset_t topfp
, const char *cur_marker
,
230 boolean_t dump_kernel_stack
;
236 if (fp
>= VM_MIN_KERNEL_ADDRESS
) {
237 dump_kernel_stack
= TRUE
;
239 dump_kernel_stack
= FALSE
;
243 if ((fp
== 0) || ((fp
& FP_ALIGNMENT_MASK
) != 0)) {
246 if (dump_kernel_stack
&& ((fp
< VM_MIN_KERNEL_ADDRESS
) || (fp
> VM_MAX_KERNEL_ADDRESS
))) {
249 if ((!dump_kernel_stack
) && (fp
>= VM_MIN_KERNEL_ADDRESS
)) {
254 * Check to see if current address will result in a different
255 * ppn than previously computed (to avoid recomputation) via
256 * (addr) ^ fp_for_ppn) >> PAGE_SHIFT)
258 if ((((fp
+ FP_LR_OFFSET
) ^ fp_for_ppn
) >> PAGE_SHIFT
) != 0x0U
) {
259 ppn
= pmap_find_phys(pmap
, fp
+ FP_LR_OFFSET
);
260 fp_for_ppn
= fp
+ (is_64_bit
? FP_LR_OFFSET64
: FP_LR_OFFSET
);
262 if (ppn
!= (ppnum_t
)NULL
) {
264 lr
= ml_phys_read_double_64(((((vm_offset_t
)ppn
) << PAGE_SHIFT
)) | ((fp
+ FP_LR_OFFSET64
) & PAGE_MASK
));
266 lr
= ml_phys_read_word(((((vm_offset_t
)ppn
) << PAGE_SHIFT
)) | ((fp
+ FP_LR_OFFSET
) & PAGE_MASK
));
270 paniclog_append_noflush("%s\t Could not read LR from frame at 0x%016llx\n", cur_marker
, fp
+ FP_LR_OFFSET64
);
272 paniclog_append_noflush("%s\t Could not read LR from frame at 0x%08x\n", cur_marker
, (uint32_t)(fp
+ FP_LR_OFFSET
));
276 if (((fp
^ fp_for_ppn
) >> PAGE_SHIFT
) != 0x0U
) {
277 ppn
= pmap_find_phys(pmap
, fp
);
280 if (ppn
!= (ppnum_t
)NULL
) {
282 fp
= ml_phys_read_double_64(((((vm_offset_t
)ppn
) << PAGE_SHIFT
)) | (fp
& PAGE_MASK
));
284 fp
= ml_phys_read_word(((((vm_offset_t
)ppn
) << PAGE_SHIFT
)) | (fp
& PAGE_MASK
));
288 paniclog_append_noflush("%s\t Could not read FP from frame at 0x%016llx\n", cur_marker
, fp
);
290 paniclog_append_noflush("%s\t Could not read FP from frame at 0x%08x\n", cur_marker
, (uint32_t)fp
);
297 paniclog_append_noflush("%s\t lr: 0x%016llx fp: 0x%016llx\n", cur_marker
, lr
, fp
);
299 paniclog_append_noflush("%s\t lr: 0x%08x fp: 0x%08x\n", cur_marker
, (uint32_t)lr
, (uint32_t)fp
);
302 } while ((++i
< FP_MAX_NUM_TO_EVALUATE
) && (fp
!= topfp
));
305 #define SANE_TASK_LIMIT 256
306 #define TOP_RUNNABLE_LIMIT 5
307 #define PANICLOG_UUID_BUF_SIZE 256
309 extern void panic_print_vnodes(void);
312 do_print_all_backtraces(
315 int logversion
= PANICLOG_VERSION
;
316 thread_t cur_thread
= current_thread();
319 int print_vnodes
= 0;
320 const char *nohilite_thread_marker
= "\t";
322 /* end_marker_bytes set to 200 for printing END marker + stackshot summary info always */
323 int bytes_traced
= 0, bytes_remaining
= 0, end_marker_bytes
= 200;
324 uint64_t bytes_used
= 0ULL;
326 char *stackshot_begin_loc
= NULL
;
329 __asm__
volatile ("mov %0, r7":"=r"(cur_fp
));
330 #elif defined(__arm64__)
331 __asm__
volatile ("add %0, xzr, fp":"=r"(cur_fp
));
333 #error Unknown architecture.
335 if (panic_bt_depth
!= 0) {
340 /* Truncate panic string to 1200 bytes -- WDT log can be ~1100 bytes */
341 paniclog_append_noflush("Debugger message: %.1200s\n", message
);
343 paniclog_append_noflush("Device: %s\n",
344 ('\0' != gTargetTypeBuffer
[0]) ? gTargetTypeBuffer
: "Not set yet");
345 paniclog_append_noflush("Hardware Model: %s\n",
346 ('\0' != gModelTypeBuffer
[0]) ? gModelTypeBuffer
:"Not set yet");
347 paniclog_append_noflush("ECID: %02X%02X%02X%02X%02X%02X%02X%02X\n", gPlatformECID
[7],
348 gPlatformECID
[6], gPlatformECID
[5], gPlatformECID
[4], gPlatformECID
[3],
349 gPlatformECID
[2], gPlatformECID
[1], gPlatformECID
[0]);
350 if (last_hwaccess_thread
) {
351 paniclog_append_noflush("AppleHWAccess Thread: 0x%llx\n", last_hwaccess_thread
);
353 paniclog_append_noflush("Boot args: %s\n", PE_boot_args());
355 paniclog_append_noflush("Memory ID: 0x%x\n", gPlatformMemoryID
);
356 paniclog_append_noflush("OS version: %.256s\n",
357 ('\0' != osversion
[0]) ? osversion
: "Not set yet");
358 #if defined(XNU_TARGET_OS_BRIDGE)
359 paniclog_append_noflush("macOS version: %.256s\n",
360 ('\0' != macosversion
[0]) ? macosversion
: "Not set");
362 paniclog_append_noflush("Kernel version: %.512s\n", version
);
364 if (kernelcache_uuid_valid
) {
365 paniclog_append_noflush("KernelCache UUID: ");
366 for (size_t index
= 0; index
< sizeof(uuid_t
); index
++) {
367 paniclog_append_noflush("%02X", kernelcache_uuid
[index
]);
369 paniclog_append_noflush("\n");
371 panic_display_kernel_uuid();
373 paniclog_append_noflush("iBoot version: %.128s\n", firmware_version
);
374 paniclog_append_noflush("secure boot?: %s\n", debug_enabled
? "NO": "YES");
375 #if defined(XNU_TARGET_OS_BRIDGE)
376 paniclog_append_noflush("x86 EFI Boot State: ");
377 if (PE_smc_stashed_x86_efi_boot_state
!= 0xFF) {
378 paniclog_append_noflush("0x%x\n", PE_smc_stashed_x86_efi_boot_state
);
380 paniclog_append_noflush("not available\n");
382 paniclog_append_noflush("x86 System State: ");
383 if (PE_smc_stashed_x86_system_state
!= 0xFF) {
384 paniclog_append_noflush("0x%x\n", PE_smc_stashed_x86_system_state
);
386 paniclog_append_noflush("not available\n");
388 paniclog_append_noflush("x86 Power State: ");
389 if (PE_smc_stashed_x86_power_state
!= 0xFF) {
390 paniclog_append_noflush("0x%x\n", PE_smc_stashed_x86_power_state
);
392 paniclog_append_noflush("not available\n");
394 paniclog_append_noflush("x86 Shutdown Cause: ");
395 if (PE_smc_stashed_x86_shutdown_cause
!= 0xFF) {
396 paniclog_append_noflush("0x%x\n", PE_smc_stashed_x86_shutdown_cause
);
398 paniclog_append_noflush("not available\n");
400 paniclog_append_noflush("x86 Previous Power Transitions: ");
401 if (PE_smc_stashed_x86_prev_power_transitions
!= UINT64_MAX
) {
402 paniclog_append_noflush("0x%llx\n", PE_smc_stashed_x86_prev_power_transitions
);
404 paniclog_append_noflush("not available\n");
406 paniclog_append_noflush("PCIeUp link state: ");
407 if (PE_pcie_stashed_link_state
!= UINT32_MAX
) {
408 paniclog_append_noflush("0x%x\n", PE_pcie_stashed_link_state
);
410 paniclog_append_noflush("not available\n");
413 if (panic_data_buffers
!= NULL
) {
414 paniclog_append_noflush("%s data: ", panic_data_buffers
->producer_name
);
415 uint8_t *panic_buffer_data
= (uint8_t *) panic_data_buffers
->buf
;
416 for (int i
= 0; i
< panic_data_buffers
->len
; i
++) {
417 paniclog_append_noflush("%02X", panic_buffer_data
[i
]);
419 paniclog_append_noflush("\n");
421 paniclog_append_noflush("Paniclog version: %d\n", logversion
);
423 panic_display_kernel_aslr();
424 panic_display_times();
425 panic_display_zprint();
427 panic_display_ztrace();
428 #endif /* CONFIG_ZLEAKS */
429 #if CONFIG_ECC_LOGGING
430 panic_display_ecc_errors();
431 #endif /* CONFIG_ECC_LOGGING */
433 #if DEVELOPMENT || DEBUG
434 if (cs_debug_unsigned_exec_failures
!= 0 || cs_debug_unsigned_mmap_failures
!= 0) {
435 paniclog_append_noflush("Unsigned code exec failures: %u\n", cs_debug_unsigned_exec_failures
);
436 paniclog_append_noflush("Unsigned code mmap failures: %u\n", cs_debug_unsigned_mmap_failures
);
440 // Just print threads with high CPU usage for WDT timeouts
441 if (strncmp(message
, "WDT timeout", 11) == 0) {
442 thread_t top_runnable
[5] = {0};
444 int total_cpu_usage
= 0;
449 for (thread
= (thread_t
)queue_first(&threads
);
450 VALIDATE_PTR(thread
) && !queue_end(&threads
, (queue_entry_t
)thread
);
451 thread
= (thread_t
)queue_next(&thread
->threads
)) {
452 total_cpu_usage
+= thread
->cpu_usage
;
454 // Look for the 5 runnable threads with highest priority
455 if (thread
->state
& TH_RUN
) {
457 thread_t comparison_thread
= thread
;
459 for (k
= 0; k
< TOP_RUNNABLE_LIMIT
; k
++) {
460 if (top_runnable
[k
] == 0) {
461 top_runnable
[k
] = comparison_thread
;
463 } else if (comparison_thread
->sched_pri
> top_runnable
[k
]->sched_pri
) {
464 thread_t temp
= top_runnable
[k
];
465 top_runnable
[k
] = comparison_thread
;
466 comparison_thread
= temp
;
467 } // if comparison thread has higher priority than previously saved thread
468 } // loop through highest priority runnable threads
469 } // Check if thread is runnable
470 } // Loop through all threads
472 // Print the relevant info for each thread identified
473 paniclog_append_noflush("Total cpu_usage: %d\n", total_cpu_usage
);
474 paniclog_append_noflush("Thread task pri cpu_usage\n");
476 for (int i
= 0; i
< TOP_RUNNABLE_LIMIT
; i
++) {
477 if (top_runnable
[i
] && VALIDATE_PTR(top_runnable
[i
]->task
) &&
478 validate_ptr((vm_offset_t
)top_runnable
[i
]->task
->bsd_info
, 1, "bsd_info")) {
479 char name
[MAXCOMLEN
+ 1];
480 proc_name_kdp(top_runnable
[i
]->task
, name
, sizeof(name
));
481 paniclog_append_noflush("%p %s %d %d\n",
482 top_runnable
[i
], name
, top_runnable
[i
]->sched_pri
, top_runnable
[i
]->cpu_usage
);
484 } // Loop through highest priority runnable threads
485 paniclog_append_noflush("\n");
486 } // Check if message is "WDT timeout"
488 // print current task info
489 if (VALIDATE_PTR_LIST(cur_thread
, cur_thread
->task
)) {
490 task
= cur_thread
->task
;
492 if (VALIDATE_PTR_LIST(task
->map
, task
->map
->pmap
)) {
493 paniclog_append_noflush("Panicked task %p: %d pages, %d threads: ",
494 task
, task
->map
->pmap
->stats
.resident_count
, task
->thread_count
);
496 paniclog_append_noflush("Panicked task %p: %d threads: ",
497 task
, task
->thread_count
);
500 if (validate_ptr((vm_offset_t
)task
->bsd_info
, 1, "bsd_info")) {
501 char name
[MAXCOMLEN
+ 1];
502 int pid
= proc_pid(task
->bsd_info
);
503 proc_name_kdp(task
, name
, sizeof(name
));
504 paniclog_append_noflush("pid %d: %s", pid
, name
);
506 paniclog_append_noflush("unknown task");
509 paniclog_append_noflush("\n");
512 if (cur_fp
< VM_MAX_KERNEL_ADDRESS
) {
513 paniclog_append_noflush("Panicked thread: %p, backtrace: 0x%llx, tid: %llu\n",
514 cur_thread
, (addr64_t
)cur_fp
, thread_tid(cur_thread
));
516 print_one_backtrace(kernel_pmap
, cur_fp
, nohilite_thread_marker
, TRUE
);
518 print_one_backtrace(kernel_pmap
, cur_fp
, nohilite_thread_marker
, FALSE
);
521 paniclog_append_noflush("Could not print panicked thread backtrace:"
522 "frame pointer outside kernel vm.\n");
525 paniclog_append_noflush("\n");
526 panic_info
->eph_panic_log_len
= PE_get_offset_into_panic_region(debug_buf_ptr
) - panic_info
->eph_panic_log_offset
;
527 /* set the os version data in the panic header in the format 'Product Version (OS Version)' (only if they have been set) */
528 if ((osversion
[0] != '\0') && (osproductversion
[0] != '\0')) {
529 snprintf((char *)&panic_info
->eph_os_version
, sizeof(panic_info
->eph_os_version
), PANIC_HEADER_VERSION_FMT_STR
,
530 osproductversion
, osversion
);
532 #if defined(XNU_TARGET_OS_BRIDGE)
533 if ((macosversion
[0] != '\0') && (macosproductversion
[0] != '\0')) {
534 snprintf((char *)&panic_info
->eph_macos_version
, sizeof(panic_info
->eph_macos_version
), PANIC_HEADER_VERSION_FMT_STR
,
535 macosproductversion
, macosversion
);
539 if (debug_ack_timeout_count
) {
540 panic_info
->eph_panic_flags
|= EMBEDDED_PANIC_HEADER_FLAG_STACKSHOT_FAILED_DEBUGGERSYNC
;
541 panic_info
->eph_other_log_offset
= PE_get_offset_into_panic_region(debug_buf_ptr
);
542 paniclog_append_noflush("!! debugger synchronization failed, no stackshot !!\n");
543 } else if (stackshot_active()) {
544 panic_info
->eph_panic_flags
|= EMBEDDED_PANIC_HEADER_FLAG_STACKSHOT_FAILED_NESTED
;
545 panic_info
->eph_other_log_offset
= PE_get_offset_into_panic_region(debug_buf_ptr
);
546 paniclog_append_noflush("!! panicked during stackshot, skipping panic stackshot !!\n");
548 /* Align the stackshot buffer to an 8-byte address (especially important for armv7k devices) */
549 debug_buf_ptr
+= (8 - ((uintptr_t)debug_buf_ptr
% 8));
550 stackshot_begin_loc
= debug_buf_ptr
;
552 bytes_remaining
= debug_buf_size
- (unsigned int)((uintptr_t)stackshot_begin_loc
- (uintptr_t)debug_buf_base
);
553 err
= kcdata_memory_static_init(&kc_panic_data
, (mach_vm_address_t
)debug_buf_ptr
,
554 KCDATA_BUFFER_BEGIN_STACKSHOT
, bytes_remaining
- end_marker_bytes
,
556 if (err
== KERN_SUCCESS
) {
557 kdp_snapshot_preflight(-1, stackshot_begin_loc
, bytes_remaining
- end_marker_bytes
,
558 (STACKSHOT_GET_GLOBAL_MEM_STATS
| STACKSHOT_SAVE_LOADINFO
| STACKSHOT_KCDATA_FORMAT
|
559 STACKSHOT_ENABLE_BT_FAULTING
| STACKSHOT_ENABLE_UUID_FAULTING
| STACKSHOT_FROM_PANIC
|
560 STACKSHOT_NO_IO_STATS
| STACKSHOT_THREAD_WAITINFO
), &kc_panic_data
, 0);
561 err
= do_stackshot(NULL
);
562 bytes_traced
= kdp_stack_snapshot_bytes_traced();
563 if (bytes_traced
> 0 && !err
) {
564 debug_buf_ptr
+= bytes_traced
;
565 panic_info
->eph_panic_flags
|= EMBEDDED_PANIC_HEADER_FLAG_STACKSHOT_SUCCEEDED
;
566 panic_info
->eph_stackshot_offset
= PE_get_offset_into_panic_region(stackshot_begin_loc
);
567 panic_info
->eph_stackshot_len
= bytes_traced
;
569 panic_info
->eph_other_log_offset
= PE_get_offset_into_panic_region(debug_buf_ptr
);
570 paniclog_append_noflush("\n** Stackshot Succeeded ** Bytes Traced %d **\n", bytes_traced
);
572 bytes_used
= kcdata_memory_get_used_bytes(&kc_panic_data
);
573 if (bytes_used
> 0) {
574 /* Zero out the stackshot data */
575 bzero(stackshot_begin_loc
, bytes_used
);
576 panic_info
->eph_panic_flags
|= EMBEDDED_PANIC_HEADER_FLAG_STACKSHOT_FAILED_INCOMPLETE
;
578 panic_info
->eph_other_log_offset
= PE_get_offset_into_panic_region(debug_buf_ptr
);
579 paniclog_append_noflush("\n** Stackshot Incomplete ** Bytes Filled %llu **\n", bytes_used
);
581 bzero(stackshot_begin_loc
, bytes_used
);
582 panic_info
->eph_panic_flags
|= EMBEDDED_PANIC_HEADER_FLAG_STACKSHOT_FAILED_ERROR
;
584 panic_info
->eph_other_log_offset
= PE_get_offset_into_panic_region(debug_buf_ptr
);
585 paniclog_append_noflush("\n!! Stackshot Failed !! Bytes Traced %d, err %d\n", bytes_traced
, err
);
589 panic_info
->eph_panic_flags
|= EMBEDDED_PANIC_HEADER_FLAG_STACKSHOT_FAILED_ERROR
;
590 panic_info
->eph_other_log_offset
= PE_get_offset_into_panic_region(debug_buf_ptr
);
591 paniclog_append_noflush("\n!! Stackshot Failed !!\nkcdata_memory_static_init returned %d", err
);
595 assert(panic_info
->eph_other_log_offset
!= 0);
597 if (print_vnodes
!= 0) {
598 panic_print_vnodes();
605 * Entry to print_all_backtraces is serialized by the debugger lock
608 print_all_backtraces(const char *message
)
610 unsigned int initial_not_in_kdp
= not_in_kdp
;
612 cpu_data_t
* cpu_data_ptr
= getCpuDatap();
614 assert(cpu_data_ptr
->PAB_active
== FALSE
);
615 cpu_data_ptr
->PAB_active
= TRUE
;
618 * Because print all backtraces uses the pmap routines, it needs to
619 * avoid taking pmap locks. Right now, this is conditionalized on
623 do_print_all_backtraces(message
);
625 not_in_kdp
= initial_not_in_kdp
;
627 cpu_data_ptr
->PAB_active
= FALSE
;
631 panic_display_times()
633 if (kdp_clock_is_locked()) {
634 paniclog_append_noflush("Warning: clock is locked. Can't get time\n");
638 if ((is_clock_configured
) && (simple_lock_try(&clock_lock
, LCK_GRP_NULL
))) {
639 clock_sec_t secs
, boot_secs
;
640 clock_usec_t usecs
, boot_usecs
;
642 simple_unlock(&clock_lock
);
644 clock_get_calendar_microtime(&secs
, &usecs
);
645 clock_get_boottime_microtime(&boot_secs
, &boot_usecs
);
647 paniclog_append_noflush("mach_absolute_time: 0x%llx\n", mach_absolute_time());
648 paniclog_append_noflush("Epoch Time: sec usec\n");
649 paniclog_append_noflush(" Boot : 0x%08x 0x%08x\n", (unsigned int)boot_secs
, (unsigned int)boot_usecs
);
650 paniclog_append_noflush(" Sleep : 0x%08x 0x%08x\n", (unsigned int)gIOLastSleepTime
.tv_sec
, (unsigned int)gIOLastSleepTime
.tv_usec
);
651 paniclog_append_noflush(" Wake : 0x%08x 0x%08x\n", (unsigned int)gIOLastWakeTime
.tv_sec
, (unsigned int)gIOLastWakeTime
.tv_usec
);
652 paniclog_append_noflush(" Calendar: 0x%08x 0x%08x\n\n", (unsigned int)secs
, (unsigned int)usecs
);
657 panic_print_symbol_name(vm_address_t search
)
659 #pragma unused(search)
660 // empty stub. Really only used on x86_64.
666 const char *message
, __unused
void *panic_data
, __unused
uint64_t panic_options
)
668 /* This should be initialized by the time we get here */
669 assert(panic_info
->eph_panic_log_offset
!= 0);
671 if (panic_options
& DEBUGGER_OPTION_PANICLOGANDREBOOT
) {
672 panic_info
->eph_panic_flags
|= EMBEDDED_PANIC_HEADER_FLAG_BUTTON_RESET_PANIC
;
675 if (panic_options
& DEBUGGER_OPTION_COPROC_INITIATED_PANIC
) {
676 panic_info
->eph_panic_flags
|= EMBEDDED_PANIC_HEADER_FLAG_COPROC_INITIATED_PANIC
;
679 #if defined(XNU_TARGET_OS_BRIDGE)
680 panic_info
->eph_x86_power_state
= PE_smc_stashed_x86_power_state
;
681 panic_info
->eph_x86_efi_boot_state
= PE_smc_stashed_x86_efi_boot_state
;
682 panic_info
->eph_x86_system_state
= PE_smc_stashed_x86_system_state
;
686 * On newer targets, panic data is stored directly into the iBoot panic region.
687 * If we re-enter SavePanicInfo (e.g. on a double panic) on such a target, update the
688 * panic CRC so that iBoot can hopefully find *something* useful in the panic region.
690 if (PanicInfoSaved
&& (debug_buf_base
>= (char*)gPanicBase
) && (debug_buf_base
< (char*)gPanicBase
+ gPanicSize
)) {
691 unsigned int pi_size
= (unsigned int)(debug_buf_ptr
- gPanicBase
);
692 PE_save_buffer_to_vram((unsigned char*)gPanicBase
, &pi_size
);
693 PE_sync_panic_buffers(); // extra precaution; panic path likely isn't reliable if we're here
696 if (PanicInfoSaved
|| (debug_buf_size
== 0)) {
700 PanicInfoSaved
= TRUE
;
702 print_all_backtraces(message
);
704 assert(panic_info
->eph_panic_log_len
!= 0);
705 panic_info
->eph_other_log_len
= PE_get_offset_into_panic_region(debug_buf_ptr
) - panic_info
->eph_other_log_offset
;
707 PEHaltRestart(kPEPanicSync
);
710 * Notifies registered IOPlatformPanicAction callbacks
711 * (which includes one to disable the memcache) and flushes
712 * the buffer contents from the cache
720 unsigned int panicbuf_length
= 0;
722 panicbuf_length
= (unsigned int)(debug_buf_ptr
- gPanicBase
);
723 if (!panicbuf_length
) {
728 * Updates the log length of the last part of the panic log.
730 panic_info
->eph_other_log_len
= PE_get_offset_into_panic_region(debug_buf_ptr
) - panic_info
->eph_other_log_offset
;
733 * Updates the metadata at the beginning of the panic buffer,
736 PE_save_buffer_to_vram((unsigned char *)gPanicBase
, &panicbuf_length
);
739 * This is currently unused by platform KEXTs on embedded but is
740 * kept for compatibility with the published IOKit interfaces.
742 PESavePanicInfo((unsigned char *)gPanicBase
, panicbuf_length
);
744 PE_sync_panic_buffers();
748 * @function DebuggerXCallEnter
750 * @abstract IPI other cores so this core can run in a single-threaded context.
752 * @discussion This function should be called with the debugger lock held. It
753 * signals the other cores to go into a busy loop so this core can run in a
754 * single-threaded context and inspect kernel memory.
756 * @param proceed_on_sync_failure If true, then go ahead and try to debug even
757 * if we can't synch with the other cores. This is inherently unsafe and should
758 * only be used if the kernel is going down in flames anyway.
760 * @result returns KERN_OPERATION_TIMED_OUT if synchronization times out and
761 * proceed_on_sync_failure is false.
765 boolean_t proceed_on_sync_failure
)
767 uint64_t max_mabs_time
, current_mabs_time
;
770 cpu_data_t
*target_cpu_datap
;
771 cpu_data_t
*cpu_data_ptr
= getCpuDatap();
773 /* Check for nested debugger entry. */
774 cpu_data_ptr
->debugger_active
++;
775 if (cpu_data_ptr
->debugger_active
!= 1) {
780 * If debugger_sync is not 0, someone responded excessively late to the last
781 * debug request (we zero the sync variable in the return function). Zero it
782 * again here. This should prevent us from getting out of sync (heh) and
783 * timing out on every entry to the debugger if we timeout once.
790 * We need a barrier here to ensure CPUs see mp_kdp_trap and spin when responding
793 __builtin_arm_dmb(DMB_ISH
);
796 * Try to signal all CPUs (except ourselves, of course). Use debugger_sync to
797 * synchronize with every CPU that we appeared to signal successfully (cpu_signal
798 * is not synchronous).
800 bool cpu_signal_failed
= false;
801 max_cpu
= ml_get_max_cpu_number();
803 boolean_t immediate_halt
= FALSE
;
804 if (proceed_on_sync_failure
&& force_immediate_debug_halt
) {
805 immediate_halt
= TRUE
;
808 if (!immediate_halt
) {
809 for (cpu
= 0; cpu
<= max_cpu
; cpu
++) {
810 target_cpu_datap
= (cpu_data_t
*)CpuDataEntries
[cpu
].cpu_data_vaddr
;
812 if ((target_cpu_datap
== NULL
) || (target_cpu_datap
== cpu_data_ptr
)) {
816 if (KERN_SUCCESS
== cpu_signal(target_cpu_datap
, SIGPdebug
, (void *)NULL
, NULL
)) {
817 (void)hw_atomic_add(&debugger_sync
, 1);
819 cpu_signal_failed
= true;
820 kprintf("cpu_signal failed in DebuggerXCallEnter\n");
824 nanoseconds_to_absolutetime(DEBUG_ACK_TIMEOUT
, &max_mabs_time
);
825 current_mabs_time
= mach_absolute_time();
826 max_mabs_time
+= current_mabs_time
;
827 assert(max_mabs_time
> current_mabs_time
);
830 * Wait for DEBUG_ACK_TIMEOUT ns for a response from everyone we IPI'd. If we
831 * timeout, that is simply too bad; we don't have a true NMI, and one CPU may be
832 * uninterruptibly spinning on someone else. The best we can hope for is that
833 * all other CPUs have either responded or are spinning in a context that is
836 while ((debugger_sync
!= 0) && (current_mabs_time
< max_mabs_time
)) {
837 current_mabs_time
= mach_absolute_time();
841 if (cpu_signal_failed
&& !proceed_on_sync_failure
) {
842 DebuggerXCallReturn();
844 } else if (immediate_halt
|| (current_mabs_time
>= max_mabs_time
)) {
846 * For the moment, we're aiming for a timeout that the user shouldn't notice,
847 * but will be sufficient to let the other core respond.
849 __builtin_arm_dmb(DMB_ISH
);
850 for (cpu
= 0; cpu
<= max_cpu
; cpu
++) {
851 target_cpu_datap
= (cpu_data_t
*)CpuDataEntries
[cpu
].cpu_data_vaddr
;
853 if ((target_cpu_datap
== NULL
) || (target_cpu_datap
== cpu_data_ptr
)) {
856 if (!(target_cpu_datap
->cpu_signal
& SIGPdebug
) && !immediate_halt
) {
859 if (proceed_on_sync_failure
) {
860 paniclog_append_noflush("Attempting to forcibly halt cpu %d\n", cpu
);
861 dbgwrap_status_t halt_status
= ml_dbgwrap_halt_cpu(cpu
, 0);
862 if (halt_status
< 0) {
863 paniclog_append_noflush("cpu %d failed to halt with error %d: %s\n", cpu
, halt_status
, ml_dbgwrap_strerror(halt_status
));
865 if (halt_status
> 0) {
866 paniclog_append_noflush("cpu %d halted with warning %d: %s\n", cpu
, halt_status
, ml_dbgwrap_strerror(halt_status
));
868 paniclog_append_noflush("cpu %d successfully halted\n", cpu
);
870 target_cpu_datap
->halt_status
= CPU_HALTED
;
873 kprintf("Debugger synch pending on cpu %d\n", cpu
);
876 if (proceed_on_sync_failure
) {
877 for (cpu
= 0; cpu
<= max_cpu
; cpu
++) {
878 target_cpu_datap
= (cpu_data_t
*)CpuDataEntries
[cpu
].cpu_data_vaddr
;
880 if ((target_cpu_datap
== NULL
) || (target_cpu_datap
== cpu_data_ptr
) ||
881 (target_cpu_datap
->halt_status
== CPU_NOT_HALTED
)) {
884 dbgwrap_status_t halt_status
= ml_dbgwrap_halt_cpu_with_state(cpu
,
885 NSEC_PER_SEC
, &target_cpu_datap
->halt_state
);
886 if ((halt_status
< 0) || (halt_status
== DBGWRAP_WARN_CPU_OFFLINE
)) {
887 paniclog_append_noflush("Unable to obtain state for cpu %d with status %d: %s\n", cpu
, halt_status
, ml_dbgwrap_strerror(halt_status
));
889 target_cpu_datap
->halt_status
= CPU_HALTED_WITH_STATE
;
892 if (immediate_halt
) {
893 paniclog_append_noflush("Immediate halt requested on all cores\n");
895 paniclog_append_noflush("Debugger synchronization timed out; waited %llu nanoseconds\n", DEBUG_ACK_TIMEOUT
);
897 debug_ack_timeout_count
++;
900 DebuggerXCallReturn();
901 return KERN_OPERATION_TIMED_OUT
;
909 * @function DebuggerXCallReturn
911 * @abstract Resume normal multicore operation after DebuggerXCallEnter()
913 * @discussion This function should be called with debugger lock held.
919 cpu_data_t
*cpu_data_ptr
= getCpuDatap();
921 cpu_data_ptr
->debugger_active
--;
922 if (cpu_data_ptr
->debugger_active
!= 0) {
929 /* Do we need a barrier here? */
930 __builtin_arm_dmb(DMB_ISH
);
937 boolean_t save_context
= FALSE
;
938 vm_offset_t kstackptr
= 0;
939 arm_saved_state_t
*regs
= (arm_saved_state_t
*) ctx
;
942 #if defined(__arm64__)
943 save_context
= PSR64_IS_KERNEL(get_saved_state_cpsr(regs
));
945 save_context
= PSR_IS_KERNEL(regs
->cpsr
);
949 kstackptr
= current_thread()->machine
.kstackptr
;
950 arm_saved_state_t
*state
= (arm_saved_state_t
*)kstackptr
;
953 /* Save the interrupted context before acknowledging the signal */
956 /* zero old state so machine_trace_thread knows not to backtrace it */
957 set_saved_state_fp(state
, 0);
958 set_saved_state_pc(state
, 0);
959 set_saved_state_lr(state
, 0);
960 set_saved_state_sp(state
, 0);
963 (void)hw_atomic_sub(&debugger_sync
, 1);
964 __builtin_arm_dmb(DMB_ISH
);
965 while (mp_kdp_trap
) {
969 /* Any cleanup for our pushed context should go here */
979 #pragma unused(reason,ctx)
980 #endif /* !MACH_KDP */
982 #if ALTERNATE_DEBUGGER
983 alternate_debugger_enter();
987 kdp_trap(reason
, (struct arm_saved_state
*)ctx
);
989 /* TODO: decide what to do if no debugger config */