2 * Copyright (c) 2000-2019 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 * Mach Operating System
33 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
34 * All Rights Reserved.
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
57 #include <mach_assert.h>
60 #include <kdp/kdp_core.h>
61 #include <kdp/kdp_internal.h>
62 #include <kdp/kdp_callout.h>
63 #include <kern/cpu_number.h>
64 #include <kern/kalloc.h>
66 #include <kern/thread.h>
67 #include <kern/assert.h>
68 #include <kern/sched_prim.h>
69 #include <kern/misc_protos.h>
70 #include <kern/clock.h>
71 #include <kern/telemetry.h>
73 #include <kern/kern_cdata.h>
74 #include <kern/zalloc.h>
75 #include <vm/vm_kern.h>
76 #include <vm/vm_map.h>
79 #include <stdatomic.h>
81 #include <console/serial_protos.h>
83 #if !(MACH_KDP && CONFIG_KDP_INTERACTIVE_DEBUGGING)
84 #include <kdp/kdp_udp.h>
86 #include <kern/processor.h>
88 #if defined(__i386__) || defined(__x86_64__)
89 #include <IOKit/IOBSD.h>
91 #include <i386/cpu_threads.h>
92 #include <i386/pmCPU.h>
95 #include <IOKit/IOPlatformExpert.h>
96 #include <machine/pal_routines.h>
98 #include <sys/kdebug.h>
99 #include <libkern/OSKextLibPrivate.h>
100 #include <libkern/OSAtomic.h>
101 #include <libkern/kernel_mach_header.h>
102 #include <libkern/section_keywords.h>
103 #include <uuid/uuid.h>
104 #include <mach_debug/zone_info.h>
106 #include <os/log_private.h>
109 #include <pexpert/pexpert.h> /* For gPanicBase */
110 #include <arm/caches_internal.h>
111 #include <arm/misc_protos.h>
112 extern volatile struct xnu_hw_shmem_dbg_command_info
*hwsd_info
;
116 #include <tests/xnupost.h>
117 extern int vsnprintf(char *, size_t, const char *, va_list);
120 unsigned int halt_in_debugger
= 0;
121 unsigned int current_debugger
= 0;
122 unsigned int active_debugger
= 0;
123 unsigned int panicDebugging
= FALSE
;
124 unsigned int kdebug_serial
= FALSE
;
125 unsigned int kernel_debugger_entry_count
= 0;
127 #if !defined (__x86_64__)
128 struct additional_panic_data_buffer
*panic_data_buffers
= NULL
;
132 #define TRAP_DEBUGGER __asm__ volatile("trap")
133 #elif defined(__arm64__)
135 * Magic number; this should be identical to the __arm__ encoding for trap.
137 #define TRAP_DEBUGGER __asm__ volatile(".long 0xe7ffdeff")
138 #elif defined (__x86_64__)
139 #define TRAP_DEBUGGER __asm__("int3")
141 #error No TRAP_DEBUGGER for this architecture
144 #if defined(__i386__) || defined(__x86_64__)
145 #define panic_stop() pmCPUHalt(PM_HALT_PANIC)
147 #define panic_stop() panic_spin_forever()
150 #define CPUDEBUGGEROP PROCESSOR_DATA(current_processor(), debugger_state).db_current_op
151 #define CPUDEBUGGERMSG PROCESSOR_DATA(current_processor(), debugger_state).db_message
152 #define CPUPANICSTR PROCESSOR_DATA(current_processor(), debugger_state).db_panic_str
153 #define CPUPANICARGS PROCESSOR_DATA(current_processor(), debugger_state).db_panic_args
154 #define CPUPANICOPTS PROCESSOR_DATA(current_processor(), debugger_state).db_panic_options
155 #define CPUPANICDATAPTR PROCESSOR_DATA(current_processor(), debugger_state).db_panic_data_ptr
156 #define CPUDEBUGGERSYNC PROCESSOR_DATA(current_processor(), debugger_state).db_proceed_on_sync_failure
157 #define CPUDEBUGGERCOUNT PROCESSOR_DATA(current_processor(), debugger_state).db_entry_count
158 #define CPUDEBUGGERRET PROCESSOR_DATA(current_processor(), debugger_state).db_op_return
159 #define CPUPANICCALLER PROCESSOR_DATA(current_processor(), debugger_state).db_panic_caller
161 #if DEVELOPMENT || DEBUG
162 #define DEBUGGER_DEBUGGING_NESTED_PANIC_IF_REQUESTED(requested) \
165 volatile int *badpointer = (int *)4; \
169 #endif /* DEVELOPMENT || DEBUG */
171 debugger_op debugger_current_op
= DBOP_NONE
;
172 const char *debugger_panic_str
= NULL
;
173 va_list *debugger_panic_args
= NULL
;
174 void *debugger_panic_data
= NULL
;
175 uint64_t debugger_panic_options
= 0;
176 const char *debugger_message
= NULL
;
177 unsigned long debugger_panic_caller
= 0;
179 void panic_trap_to_debugger(const char *panic_format_str
, va_list *panic_args
,
180 unsigned int reason
, void *ctx
, uint64_t panic_options_mask
, void *panic_data
,
181 unsigned long panic_caller
) __dead2
;
182 static void kdp_machine_reboot_type(unsigned int type
, uint64_t debugger_flags
);
183 void panic_spin_forever(void) __dead2
;
184 extern kern_return_t
do_stackshot(void);
185 extern void PE_panic_hook(const char*);
187 #if CONFIG_NONFATAL_ASSERTS
191 #define NESTEDDEBUGGERENTRYMAX 5
192 static unsigned int max_debugger_entry_count
= NESTEDDEBUGGERENTRYMAX
;
195 #define DEBUG_BUF_SIZE (4096)
196 #define KDBG_TRACE_PANIC_FILENAME "/var/log/panic.trace"
198 #define DEBUG_BUF_SIZE ((3 * PAGE_SIZE) + offsetof(struct macos_panic_header, mph_data))
199 /* EXTENDED_DEBUG_BUF_SIZE definition is now in debug.h */
200 static_assert(((EXTENDED_DEBUG_BUF_SIZE
% PANIC_FLUSH_BOUNDARY
) == 0), "Extended debug buf size must match SMC alignment requirements");
201 #define KDBG_TRACE_PANIC_FILENAME "/var/tmp/panic.trace"
204 /* debug_buf is directly linked with iBoot panic region for embedded targets */
206 char *debug_buf_base
= NULL
;
207 char *debug_buf_ptr
= NULL
;
208 unsigned int debug_buf_size
= 0;
210 char debug_buf
[DEBUG_BUF_SIZE
];
211 struct macos_panic_header
*panic_info
= (struct macos_panic_header
*)debug_buf
;
212 char *debug_buf_base
= (debug_buf
+ offsetof(struct macos_panic_header
, mph_data
));
213 char *debug_buf_ptr
= (debug_buf
+ offsetof(struct macos_panic_header
, mph_data
));
216 * We don't include the size of the panic header in the length of the data we actually write.
217 * On co-processor platforms, we lose sizeof(struct macos_panic_header) bytes from the end of
218 * the end of the log because we only support writing (3*PAGESIZE) bytes.
220 unsigned int debug_buf_size
= (DEBUG_BUF_SIZE
- offsetof(struct macos_panic_header
, mph_data
));
222 boolean_t extended_debug_log_enabled
= FALSE
;
226 atomic_int debugger_cpu
= ATOMIC_VAR_INIT(DEBUGGER_NO_CPU
);
227 boolean_t debugger_allcpus_halted
= FALSE
;
228 boolean_t debugger_safe_to_return
= TRUE
;
229 unsigned int debugger_context
= 0;
231 static char model_name
[64];
232 unsigned char *kernel_uuid
;
234 boolean_t kernelcache_uuid_valid
= FALSE
;
235 uuid_t kernelcache_uuid
;
236 uuid_string_t kernelcache_uuid_string
;
239 * By default we treat Debugger() the same as calls to panic(), unless
240 * we have debug boot-args present and the DB_KERN_DUMP_ON_NMI *NOT* set.
241 * If DB_KERN_DUMP_ON_NMI is *NOT* set, return from Debugger() is supported.
243 * Return from Debugger() is currently only implemented on x86
245 static boolean_t debugger_is_panic
= TRUE
;
247 #if DEVELOPMENT || DEBUG
248 boolean_t debug_boot_arg_inited
= FALSE
;
251 SECURITY_READ_ONLY_LATE(unsigned int) debug_boot_arg
;
253 char kernel_uuid_string
[37]; /* uuid_string_t */
254 char kernelcache_uuid_string
[37]; /* uuid_string_t */
255 char panic_disk_error_description
[512];
256 size_t panic_disk_error_description_size
= sizeof(panic_disk_error_description
);
258 extern unsigned int write_trace_on_panic
;
259 int kext_assertions_enable
=
260 #if DEBUG || DEVELOPMENT
267 * Maintain the physically-contiguous carveout for the `phys_carveout_mb`
270 SECURITY_READ_ONLY_LATE(vm_offset_t
) phys_carveout
= 0;
271 SECURITY_READ_ONLY_LATE(uintptr_t) phys_carveout_pa
= 0;
272 SECURITY_READ_ONLY_LATE(size_t) phys_carveout_size
= 0;
277 unsigned long uuidlen
= 0;
280 uuid
= getuuidfromheader(&_mh_execute_header
, &uuidlen
);
281 if ((uuid
!= NULL
) && (uuidlen
== sizeof(uuid_t
))) {
283 uuid_unparse_upper(*(uuid_t
*)uuid
, kernel_uuid_string
);
286 #if CONFIG_NONFATAL_ASSERTS
287 if (!PE_parse_boot_argn("assertions", &mach_assert
, sizeof(mach_assert
))) {
293 * Initialize the value of the debug boot-arg
296 #if ((CONFIG_EMBEDDED && MACH_KDP) || defined(__x86_64__))
297 if (PE_parse_boot_argn("debug", &debug_boot_arg
, sizeof(debug_boot_arg
))) {
298 #if DEVELOPMENT || DEBUG
299 if (debug_boot_arg
& DB_HALT
) {
300 halt_in_debugger
= 1;
305 if (debug_boot_arg
& DB_NMI
) {
306 panicDebugging
= TRUE
;
309 panicDebugging
= TRUE
;
310 #if KDEBUG_MOJO_TRACE
311 if (debug_boot_arg
& DB_PRT_KDEBUG
) {
312 kdebug_serial
= TRUE
;
315 #endif /* CONFIG_EMBEDDED */
318 if (!PE_parse_boot_argn("nested_panic_max", &max_debugger_entry_count
, sizeof(max_debugger_entry_count
))) {
319 max_debugger_entry_count
= NESTEDDEBUGGERENTRYMAX
;
322 #endif /* ((CONFIG_EMBEDDED && MACH_KDP) || defined(__x86_64__)) */
324 #if DEVELOPMENT || DEBUG
325 debug_boot_arg_inited
= TRUE
;
330 * By default we treat Debugger() the same as calls to panic(), unless
331 * we have debug boot-args present and the DB_KERN_DUMP_ON_NMI *NOT* set.
332 * If DB_KERN_DUMP_ON_NMI is *NOT* set, return from Debugger() is supported.
333 * This is because writing an on-device corefile is a destructive operation.
335 * Return from Debugger() is currently only implemented on x86
337 if (PE_i_can_has_debugger(NULL
) && !(debug_boot_arg
& DB_KERN_DUMP_ON_NMI
)) {
338 debugger_is_panic
= FALSE
;
343 #if defined (__x86_64__)
345 extended_debug_log_init(void)
347 assert(coprocessor_paniclog_flush
);
349 * Allocate an extended panic log buffer that has space for the panic
350 * stackshot at the end. Update the debug buf pointers appropriately
351 * to point at this new buffer.
353 char *new_debug_buf
= kalloc(EXTENDED_DEBUG_BUF_SIZE
);
355 * iBoot pre-initializes the panic region with the NULL character. We set this here
356 * so we can accurately calculate the CRC for the region without needing to flush the
357 * full region over SMC.
359 memset(new_debug_buf
, '\0', EXTENDED_DEBUG_BUF_SIZE
);
361 panic_info
= (struct macos_panic_header
*)new_debug_buf
;
362 debug_buf_ptr
= debug_buf_base
= (new_debug_buf
+ offsetof(struct macos_panic_header
, mph_data
));
363 debug_buf_size
= (EXTENDED_DEBUG_BUF_SIZE
- offsetof(struct macos_panic_header
, mph_data
));
365 extended_debug_log_enabled
= TRUE
;
368 * Insert a compiler barrier so we don't free the other panic stackshot buffer
369 * until after we've marked the new one as available
371 __compiler_barrier();
372 kmem_free(kernel_map
, panic_stackshot_buf
, panic_stackshot_buf_len
);
373 panic_stackshot_buf
= 0;
374 panic_stackshot_buf_len
= 0;
376 #endif /* defined (__x86_64__) */
383 printf("debug_log_init: Error!! gPanicBase is still not initialized\n");
386 /* Shift debug buf start location and size by the length of the panic header */
387 debug_buf_base
= (char *)gPanicBase
+ sizeof(struct embedded_panic_header
);
388 debug_buf_ptr
= debug_buf_base
;
389 debug_buf_size
= gPanicSize
- sizeof(struct embedded_panic_header
);
391 kern_return_t kr
= KERN_SUCCESS
;
392 bzero(panic_info
, DEBUG_BUF_SIZE
);
394 assert(debug_buf_base
!= NULL
);
395 assert(debug_buf_ptr
!= NULL
);
396 assert(debug_buf_size
!= 0);
399 * We allocate a buffer to store a panic time stackshot. If we later discover that this is a
400 * system that supports flushing a stackshot via an extended debug log (see above), we'll free this memory
401 * as it's not necessary on this platform. This information won't be available until the IOPlatform has come
404 kr
= kmem_alloc(kernel_map
, &panic_stackshot_buf
, PANIC_STACKSHOT_BUFSIZE
, VM_KERN_MEMORY_DIAG
);
405 assert(kr
== KERN_SUCCESS
);
406 if (kr
== KERN_SUCCESS
) {
407 panic_stackshot_buf_len
= PANIC_STACKSHOT_BUFSIZE
;
413 phys_carveout_init(void)
415 if (!PE_i_can_has_debugger(NULL
)) {
419 unsigned int phys_carveout_mb
= 0;
421 if (!PE_parse_boot_argn("phys_carveout_mb", &phys_carveout_mb
,
422 sizeof(phys_carveout_mb
))) {
425 if (phys_carveout_mb
== 0) {
430 if (os_mul_overflow(phys_carveout_mb
, 1024 * 1024, &size
)) {
431 printf("phys_carveout_mb size overflowed (%uMB)\n",
436 kern_return_t kr
= kmem_alloc_contig(kernel_map
, &phys_carveout
, size
,
437 VM_MAP_PAGE_MASK(kernel_map
), 0, 0, KMA_NOPAGEWAIT
,
438 VM_KERN_MEMORY_DIAG
);
439 if (kr
!= KERN_SUCCESS
) {
440 printf("failed to allocate %uMB for phys_carveout_mb: %u\n",
441 phys_carveout_mb
, (unsigned int)kr
);
445 phys_carveout_pa
= kvtophys(phys_carveout
);
446 phys_carveout_size
= size
;
452 int my_cpu
= cpu_number();
453 int debugger_exp_cpu
= DEBUGGER_NO_CPU
;
454 assert(ml_get_interrupts_enabled() == FALSE
);
456 if (atomic_load(&debugger_cpu
) == my_cpu
) {
460 while (!atomic_compare_exchange_strong(&debugger_cpu
, &debugger_exp_cpu
, my_cpu
)) {
461 debugger_exp_cpu
= DEBUGGER_NO_CPU
;
470 assert(atomic_load_explicit(&debugger_cpu
, memory_order_relaxed
) == cpu_number());
473 * We don't do an atomic exchange here in case
474 * there's another CPU spinning to acquire the debugger_lock
475 * and we never get a chance to update it. We already have the
476 * lock so we can simply store DEBUGGER_NO_CPU and follow with
479 atomic_store(&debugger_cpu
, DEBUGGER_NO_CPU
);
486 DebuggerHaltOtherCores(boolean_t proceed_on_failure
)
489 return DebuggerXCallEnter(proceed_on_failure
);
490 #else /* CONFIG_EMBEDDED */
491 #pragma unused(proceed_on_failure)
492 mp_kdp_enter(proceed_on_failure
);
498 DebuggerResumeOtherCores()
501 DebuggerXCallReturn();
502 #else /* CONFIG_EMBEDDED */
508 DebuggerSaveState(debugger_op db_op
, const char *db_message
, const char *db_panic_str
,
509 va_list *db_panic_args
, uint64_t db_panic_options
, void *db_panic_data_ptr
,
510 boolean_t db_proceed_on_sync_failure
, unsigned long db_panic_caller
)
512 CPUDEBUGGEROP
= db_op
;
514 /* Preserve the original panic message */
515 if (CPUDEBUGGERCOUNT
== 1 || CPUPANICSTR
== NULL
) {
516 CPUDEBUGGERMSG
= db_message
;
517 CPUPANICSTR
= db_panic_str
;
518 CPUPANICARGS
= db_panic_args
;
519 CPUPANICDATAPTR
= db_panic_data_ptr
;
520 CPUPANICCALLER
= db_panic_caller
;
521 } else if (CPUDEBUGGERCOUNT
> 1 && db_panic_str
!= NULL
) {
522 kprintf("Nested panic detected:");
523 if (db_panic_str
!= NULL
) {
524 _doprnt(db_panic_str
, db_panic_args
, PE_kputc
, 0);
528 CPUDEBUGGERSYNC
= db_proceed_on_sync_failure
;
529 CPUDEBUGGERRET
= KERN_SUCCESS
;
531 /* Reset these on any nested panics */
532 CPUPANICOPTS
= db_panic_options
;
538 * Save the requested debugger state/action into the current processor's processor_data
539 * and trap to the debugger.
542 DebuggerTrapWithState(debugger_op db_op
, const char *db_message
, const char *db_panic_str
,
543 va_list *db_panic_args
, uint64_t db_panic_options
, void *db_panic_data_ptr
,
544 boolean_t db_proceed_on_sync_failure
, unsigned long db_panic_caller
)
548 assert(ml_get_interrupts_enabled() == FALSE
);
549 DebuggerSaveState(db_op
, db_message
, db_panic_str
, db_panic_args
,
550 db_panic_options
, db_panic_data_ptr
,
551 db_proceed_on_sync_failure
, db_panic_caller
);
555 ret
= CPUDEBUGGERRET
;
557 DebuggerSaveState(DBOP_NONE
, NULL
, NULL
, NULL
, 0, NULL
, FALSE
, 0);
562 void __attribute__((noinline
))
566 const char *expression
569 #if CONFIG_NONFATAL_ASSERTS
571 kprintf("%s:%d non-fatal Assertion: %s", file
, line
, expression
);
576 panic_plain("%s:%d Assertion failed: %s", file
, line
, expression
);
581 Debugger(const char *message
)
583 DebuggerWithContext(0, NULL
, message
, DEBUGGER_OPTION_NONE
);
587 DebuggerWithContext(unsigned int reason
, void *ctx
, const char *message
,
588 uint64_t debugger_options_mask
)
590 spl_t previous_interrupts_state
;
591 boolean_t old_doprnt_hide_pointers
= doprnt_hide_pointers
;
593 previous_interrupts_state
= ml_set_interrupts_enabled(FALSE
);
594 disable_preemption();
598 if (CPUDEBUGGERCOUNT
> max_debugger_entry_count
) {
599 static boolean_t in_panic_kprintf
= FALSE
;
601 /* Notify any listeners that we've started a panic */
602 PEHaltRestart(kPEPanicBegin
);
604 if (!in_panic_kprintf
) {
605 in_panic_kprintf
= TRUE
;
606 kprintf("Detected nested debugger entry count exceeding %d\n",
607 max_debugger_entry_count
);
608 in_panic_kprintf
= FALSE
;
611 if (!panicDebugging
) {
612 kdp_machine_reboot_type(kPEPanicRestartCPU
, debugger_options_mask
);
615 panic_spin_forever();
618 #if DEVELOPMENT || DEBUG
619 DEBUGGER_DEBUGGING_NESTED_PANIC_IF_REQUESTED((debugger_options_mask
& DEBUGGER_OPTION_RECURPANIC_ENTRY
));
622 doprnt_hide_pointers
= FALSE
;
625 DebuggerSaveState(DBOP_DEBUGGER
, message
,
626 NULL
, NULL
, debugger_options_mask
, NULL
, TRUE
, 0);
627 handle_debugger_trap(reason
, 0, 0, ctx
);
628 DebuggerSaveState(DBOP_NONE
, NULL
, NULL
,
629 NULL
, 0, NULL
, FALSE
, 0);
631 DebuggerTrapWithState(DBOP_DEBUGGER
, message
,
632 NULL
, NULL
, debugger_options_mask
, NULL
, TRUE
, 0);
636 doprnt_hide_pointers
= old_doprnt_hide_pointers
;
638 ml_set_interrupts_enabled(previous_interrupts_state
);
641 static struct kdp_callout
{
642 struct kdp_callout
* callout_next
;
643 kdp_callout_fn_t callout_fn
;
644 boolean_t callout_in_progress
;
646 } * kdp_callout_list
= NULL
;
649 * Called from kernel context to register a kdp event callout.
652 kdp_register_callout(kdp_callout_fn_t fn
, void * arg
)
654 struct kdp_callout
* kcp
;
655 struct kdp_callout
* list_head
;
657 kcp
= kalloc(sizeof(*kcp
));
659 panic("kdp_register_callout() kalloc failed");
662 kcp
->callout_fn
= fn
;
663 kcp
->callout_arg
= arg
;
664 kcp
->callout_in_progress
= FALSE
;
666 /* Lock-less list insertion using compare and exchange. */
668 list_head
= kdp_callout_list
;
669 kcp
->callout_next
= list_head
;
670 } while (!OSCompareAndSwapPtr(list_head
, kcp
, &kdp_callout_list
));
674 kdp_callouts(kdp_event_t event
)
676 struct kdp_callout
*kcp
= kdp_callout_list
;
679 if (!kcp
->callout_in_progress
) {
680 kcp
->callout_in_progress
= TRUE
;
681 kcp
->callout_fn(kcp
->callout_arg
, event
);
682 kcp
->callout_in_progress
= FALSE
;
684 kcp
= kcp
->callout_next
;
688 #if !defined (__x86_64__)
690 * Register an additional buffer with data to include in the panic log
692 * <rdar://problem/50137705> tracks supporting more than one buffer
694 * Note that producer_name and buf should never be de-allocated as we reference these during panic.
697 register_additional_panic_data_buffer(const char *producer_name
, void *buf
, int len
)
699 if (panic_data_buffers
!= NULL
) {
700 panic("register_additional_panic_data_buffer called with buffer already registered");
703 if (producer_name
== NULL
|| (strlen(producer_name
) == 0)) {
704 panic("register_additional_panic_data_buffer called with invalid producer_name");
708 panic("register_additional_panic_data_buffer called with invalid buffer pointer");
711 if ((len
<= 0) || (len
> ADDITIONAL_PANIC_DATA_BUFFER_MAX_LEN
)) {
712 panic("register_additional_panic_data_buffer called with invalid length");
715 struct additional_panic_data_buffer
*new_panic_data_buffer
= kalloc(sizeof(struct additional_panic_data_buffer
));
716 new_panic_data_buffer
->producer_name
= producer_name
;
717 new_panic_data_buffer
->buf
= buf
;
718 new_panic_data_buffer
->len
= len
;
720 if (!OSCompareAndSwapPtr(NULL
, new_panic_data_buffer
, &panic_data_buffers
)) {
721 panic("register_additional_panic_data_buffer called with buffer already registered");
726 #endif /* !defined (__x86_64__) */
729 * An overview of the xnu panic path:
731 * Several panic wrappers (panic(), panic_with_options(), etc.) all funnel into panic_trap_to_debugger().
732 * panic_trap_to_debugger() sets the panic state in the current processor's processor_data_t prior
733 * to trapping into the debugger. Once we trap to the debugger, we end up in handle_debugger_trap()
734 * which tries to acquire the panic lock by atomically swapping the current CPU number into debugger_cpu.
735 * debugger_cpu acts as a synchronization point, from which the winning CPU can halt the other cores and
736 * continue to debugger_collect_diagnostics() where we write the paniclog, corefile (if appropriate) and proceed
737 * according to the device's boot-args.
741 panic(const char *str
, ...)
743 va_list panic_str_args
;
745 va_start(panic_str_args
, str
);
746 panic_trap_to_debugger(str
, &panic_str_args
, 0, NULL
, 0, NULL
, (unsigned long)(char *)__builtin_return_address(0));
747 va_end(panic_str_args
);
751 panic_with_options(unsigned int reason
, void *ctx
, uint64_t debugger_options_mask
, const char *str
, ...)
753 va_list panic_str_args
;
755 va_start(panic_str_args
, str
);
756 panic_trap_to_debugger(str
, &panic_str_args
, reason
, ctx
, (debugger_options_mask
& ~DEBUGGER_INTERNAL_OPTIONS_MASK
),
757 NULL
, (unsigned long)(char *)__builtin_return_address(0));
758 va_end(panic_str_args
);
761 #if defined (__x86_64__)
763 * panic_with_thread_context() is used on x86 platforms to specify a different thread that should be backtraced in the paniclog.
764 * We don't generally need this functionality on embedded platforms because embedded platforms include a panic time stackshot
765 * from customer devices. We plumb the thread pointer via the debugger trap mechanism and backtrace the kernel stack from the
766 * thread when writing the panic log.
768 * NOTE: panic_with_thread_context() should be called with an explicit thread reference held on the passed thread.
771 panic_with_thread_context(unsigned int reason
, void *ctx
, uint64_t debugger_options_mask
, thread_t thread
, const char *str
, ...)
773 va_list panic_str_args
;
774 __assert_only os_ref_count_t th_ref_count
;
776 assert_thread_magic(thread
);
777 th_ref_count
= os_ref_get_count(&thread
->ref_count
);
778 assertf(th_ref_count
> 0, "panic_with_thread_context called with invalid thread %p with refcount %u", thread
, th_ref_count
);
780 /* Take a reference on the thread so it doesn't disappear by the time we try to backtrace it */
781 thread_reference(thread
);
783 va_start(panic_str_args
, str
);
784 panic_trap_to_debugger(str
, &panic_str_args
, reason
, ctx
, ((debugger_options_mask
& ~DEBUGGER_INTERNAL_OPTIONS_MASK
) | DEBUGGER_INTERNAL_OPTION_THREAD_BACKTRACE
),
785 thread
, (unsigned long)(char *)__builtin_return_address(0));
787 va_end(panic_str_args
);
789 #endif /* defined (__x86_64__) */
791 #pragma clang diagnostic push
792 #pragma clang diagnostic ignored "-Wmissing-noreturn"
794 panic_trap_to_debugger(const char *panic_format_str
, va_list *panic_args
, unsigned int reason
, void *ctx
,
795 uint64_t panic_options_mask
, void *panic_data_ptr
, unsigned long panic_caller
)
797 #pragma clang diagnostic pop
799 #if defined(__x86_64__) && (DEVELOPMENT || DEBUG)
800 /* Turn off I/O tracing once we've panicked */
801 mmiotrace_enabled
= 0;
804 if (ml_wants_panic_trap_to_debugger()) {
805 ml_panic_trap_to_debugger(panic_format_str
, panic_args
, reason
, ctx
, panic_options_mask
, panic_caller
);
811 if (CPUDEBUGGERCOUNT
> max_debugger_entry_count
) {
812 static boolean_t in_panic_kprintf
= FALSE
;
814 /* Notify any listeners that we've started a panic */
815 PEHaltRestart(kPEPanicBegin
);
817 if (!in_panic_kprintf
) {
818 in_panic_kprintf
= TRUE
;
819 kprintf("Detected nested debugger entry count exceeding %d\n",
820 max_debugger_entry_count
);
821 in_panic_kprintf
= FALSE
;
824 if (!panicDebugging
) {
825 kdp_machine_reboot_type(kPEPanicRestartCPU
, panic_options_mask
);
828 panic_spin_forever();
831 #if DEVELOPMENT || DEBUG
832 DEBUGGER_DEBUGGING_NESTED_PANIC_IF_REQUESTED((panic_options_mask
& DEBUGGER_OPTION_RECURPANIC_ENTRY
));
835 PE_panic_hook(panic_format_str
);
837 #if defined (__x86_64__)
841 if (write_trace_on_panic
&& kdebug_enable
) {
842 if (get_preemption_level() == 0 && !ml_at_interrupt_context()) {
843 ml_set_interrupts_enabled(TRUE
);
844 KDBG_RELEASE(TRACE_PANIC
);
845 kdbg_dump_trace_to_file(KDBG_TRACE_PANIC_FILENAME
);
849 ml_set_interrupts_enabled(FALSE
);
850 disable_preemption();
852 #if defined (__x86_64__)
853 pmSafeMode(x86_lcpu(), PM_SAFE_FL_SAFE
);
854 #endif /* defined (__x86_64__) */
856 /* Never hide pointers from panic logs. */
857 doprnt_hide_pointers
= FALSE
;
861 * We called into panic from a trap, no need to trap again. Set the
862 * state on the current CPU and then jump to handle_debugger_trap.
864 DebuggerSaveState(DBOP_PANIC
, "panic",
865 panic_format_str
, panic_args
,
866 panic_options_mask
, panic_data_ptr
, TRUE
, panic_caller
);
867 handle_debugger_trap(reason
, 0, 0, ctx
);
870 #if defined(__arm64__)
872 * Signal to fastsim that it should open debug ports (nop on hardware)
874 __asm__
volatile ("HINT 0x45");
875 #endif /* defined(__arm64__) */
877 DebuggerTrapWithState(DBOP_PANIC
, "panic", panic_format_str
,
878 panic_args
, panic_options_mask
, panic_data_ptr
, TRUE
, panic_caller
);
884 __builtin_unreachable();
888 panic_spin_forever(void)
890 paniclog_append_noflush("\nPlease go to https://panic.apple.com to report this panic\n");
897 kdp_machine_reboot_type(unsigned int type
, uint64_t debugger_flags
)
899 printf("Attempting system restart...");
900 if ((type
== kPEPanicRestartCPU
) && (debugger_flags
& DEBUGGER_OPTION_SKIP_PANICEND_CALLOUTS
)) {
901 PEHaltRestart(kPEPanicRestartCPUNoPanicEndCallouts
);
909 kdp_machine_reboot(void)
911 kdp_machine_reboot_type(kPEPanicRestartCPU
, 0);
915 * Gather and save diagnostic information about a panic (or Debugger call).
917 * On embedded, Debugger and Panic are treated very similarly -- WDT uses Debugger so we can
918 * theoretically return from it. On desktop, Debugger is treated as a conventional debugger -- i.e no
919 * paniclog is written and no core is written unless we request a core on NMI.
921 * This routine handles kicking off local coredumps, paniclogs, calling into the Debugger/KDP (if it's configured),
922 * and calling out to any other functions we have for collecting diagnostic info.
925 debugger_collect_diagnostics(unsigned int exception
, unsigned int code
, unsigned int subcode
, void *state
)
927 #if DEVELOPMENT || DEBUG
928 DEBUGGER_DEBUGGING_NESTED_PANIC_IF_REQUESTED((debugger_panic_options
& DEBUGGER_OPTION_RECURPANIC_PRELOG
));
931 #if defined(__x86_64__)
932 kprintf("Debugger called: <%s>\n", debugger_message
? debugger_message
: "");
935 * DB_HALT (halt_in_debugger) can be requested on startup, we shouldn't generate
936 * a coredump/paniclog for this type of debugger entry. If KDP isn't configured,
937 * we'll just spin in kdp_raise_exception.
939 if (debugger_current_op
== DBOP_DEBUGGER
&& halt_in_debugger
) {
940 kdp_raise_exception(exception
, code
, subcode
, state
);
941 if (debugger_safe_to_return
&& !debugger_is_panic
) {
946 if ((debugger_current_op
== DBOP_PANIC
) ||
947 ((debugger_current_op
== DBOP_DEBUGGER
) && debugger_is_panic
)) {
949 * Attempt to notify listeners once and only once that we've started
950 * panicking. Only do this for Debugger() calls if we're treating
951 * Debugger() calls like panic().
953 PEHaltRestart(kPEPanicBegin
);
956 * Set the begin pointer in the panic log structure. We key off of this
957 * static variable rather than contents from the panic header itself in case someone
958 * has stomped over the panic_info structure. Also initializes the header magic.
960 static boolean_t began_writing_paniclog
= FALSE
;
961 if (!began_writing_paniclog
) {
962 PE_init_panicheader();
963 began_writing_paniclog
= TRUE
;
966 * If we reached here, update the panic header to keep it as consistent
967 * as possible during a nested panic
969 PE_update_panicheader_nestedpanic();
974 * Write panic string if this was a panic.
976 * TODO: Consider moving to SavePanicInfo as this is part of the panic log.
978 if (debugger_current_op
== DBOP_PANIC
) {
979 paniclog_append_noflush("panic(cpu %d caller 0x%lx): ", (unsigned) cpu_number(), debugger_panic_caller
);
980 if (debugger_panic_str
) {
981 _doprnt(debugger_panic_str
, debugger_panic_args
, consdebug_putc
, 0);
983 paniclog_append_noflush("\n");
985 #if defined(__x86_64__)
986 else if (((debugger_current_op
== DBOP_DEBUGGER
) && debugger_is_panic
)) {
987 paniclog_append_noflush("Debugger called: <%s>\n", debugger_message
? debugger_message
: "");
991 * Debugger() is treated like panic() on embedded -- for example we use it for WDT
992 * panics (so we need to write a paniclog). On desktop Debugger() is used in the
993 * conventional sense.
995 if (debugger_current_op
== DBOP_PANIC
|| ((debugger_current_op
== DBOP_DEBUGGER
) && debugger_is_panic
))
998 kdp_callouts(KDP_EVENT_PANICLOG
);
1001 * Write paniclog and panic stackshot (if supported)
1002 * TODO: Need to clear panic log when return from debugger
1003 * hooked up for embedded
1005 SavePanicInfo(debugger_message
, debugger_panic_data
, debugger_panic_options
);
1007 #if DEVELOPMENT || DEBUG
1008 DEBUGGER_DEBUGGING_NESTED_PANIC_IF_REQUESTED((debugger_panic_options
& DEBUGGER_OPTION_RECURPANIC_POSTLOG
));
1011 /* DEBUGGER_OPTION_PANICLOGANDREBOOT is used for two finger resets on embedded so we get a paniclog */
1012 if (debugger_panic_options
& DEBUGGER_OPTION_PANICLOGANDREBOOT
) {
1013 PEHaltRestart(kPEPanicRestartCPUNoCallouts
);
1017 #if CONFIG_KDP_INTERACTIVE_DEBUGGING
1019 * If reboot on panic is enabled and the caller of panic indicated that we should skip
1020 * local coredumps, don't try to write these and instead go straight to reboot. This
1021 * allows us to persist any data that's stored in the panic log.
1023 if ((debugger_panic_options
& DEBUGGER_OPTION_SKIP_LOCAL_COREDUMP
) &&
1024 (debug_boot_arg
& DB_REBOOT_POST_CORE
)) {
1025 kdp_machine_reboot_type(kPEPanicRestartCPU
, debugger_panic_options
);
1029 * Consider generating a local corefile if the infrastructure is configured
1030 * and we haven't disabled on-device coredumps.
1032 if (on_device_corefile_enabled()) {
1033 if (!kdp_has_polled_corefile()) {
1034 if (debug_boot_arg
& (DB_KERN_DUMP_ON_PANIC
| DB_KERN_DUMP_ON_NMI
)) {
1035 paniclog_append_noflush("skipping local kernel core because core file could not be opened prior to panic (error : 0x%x)",
1036 kdp_polled_corefile_error());
1038 panic_info
->eph_panic_flags
|= EMBEDDED_PANIC_HEADER_FLAG_COREDUMP_FAILED
;
1040 #else /* CONFIG_EMBEDDED */
1041 if (panic_info
->mph_panic_log_offset
!= 0) {
1042 panic_info
->mph_panic_flags
|= MACOS_PANIC_HEADER_FLAG_COREDUMP_FAILED
;
1045 #endif /* CONFIG_EMBEDDED */
1050 #if defined (__x86_64__)
1051 /* On x86 we don't do a coredump on Debugger unless the DB_KERN_DUMP_ON_NMI boot-arg is specified. */
1052 if (debugger_current_op
!= DBOP_DEBUGGER
|| (debug_boot_arg
& DB_KERN_DUMP_ON_NMI
))
1056 * Doing an on-device coredump leaves the disk driver in a state
1057 * that can not be resumed.
1059 debugger_safe_to_return
= FALSE
;
1060 begin_panic_transfer();
1061 ret
= kern_dump(KERN_DUMP_DISK
);
1062 abort_panic_transfer();
1064 #if DEVELOPMENT || DEBUG
1065 DEBUGGER_DEBUGGING_NESTED_PANIC_IF_REQUESTED((debugger_panic_options
& DEBUGGER_OPTION_RECURPANIC_POSTCORE
));
1070 * If DB_REBOOT_POST_CORE is set, then reboot if coredump is sucessfully saved
1071 * or if option to ignore failures is set.
1073 if ((debug_boot_arg
& DB_REBOOT_POST_CORE
) &&
1074 ((ret
== 0) || (debugger_panic_options
& DEBUGGER_OPTION_ATTEMPTCOREDUMPANDREBOOT
))) {
1075 kdp_machine_reboot_type(kPEPanicRestartCPU
, debugger_panic_options
);
1080 if (debug_boot_arg
& DB_REBOOT_ALWAYS
) {
1081 kdp_machine_reboot_type(kPEPanicRestartCPU
, debugger_panic_options
);
1084 /* If KDP is configured, try to trap to the debugger */
1085 if (current_debugger
!= NO_CUR_DB
) {
1086 kdp_raise_exception(exception
, code
, subcode
, state
);
1088 * Only return if we entered via Debugger and it's safe to return
1089 * (we halted the other cores successfully, this isn't a nested panic, etc)
1091 if (debugger_current_op
== DBOP_DEBUGGER
&&
1092 debugger_safe_to_return
&&
1093 kernel_debugger_entry_count
== 1 &&
1094 !debugger_is_panic
) {
1100 if (panicDebugging
) {
1101 /* If panic debugging is configured, spin for astris to connect */
1102 panic_spin_shmcon();
1104 #endif /* CONFIG_EMBEDDED */
1105 #endif /* CONFIG_KDP_INTERACTIVE_DEBUGGING */
1107 if (!panicDebugging
) {
1108 kdp_machine_reboot_type(kPEPanicRestartCPU
, debugger_panic_options
);
1111 panic_spin_forever();
1114 #if INTERRUPT_MASKED_DEBUG
1115 uint64_t debugger_trap_timestamps
[9];
1116 # define DEBUGGER_TRAP_TIMESTAMP(i) debugger_trap_timestamps[i] = mach_absolute_time();
1118 # define DEBUGGER_TRAP_TIMESTAMP(i)
1122 handle_debugger_trap(unsigned int exception
, unsigned int code
, unsigned int subcode
, void *state
)
1124 unsigned int initial_not_in_kdp
= not_in_kdp
;
1126 debugger_op db_prev_op
= debugger_current_op
;
1128 DEBUGGER_TRAP_TIMESTAMP(0);
1131 ret
= DebuggerHaltOtherCores(CPUDEBUGGERSYNC
);
1133 DEBUGGER_TRAP_TIMESTAMP(1);
1135 #if INTERRUPT_MASKED_DEBUG
1136 if (serialmode
& SERIALMODE_OUTPUT
) {
1137 ml_spin_debug_reset(current_thread());
1140 if (ret
!= KERN_SUCCESS
) {
1141 CPUDEBUGGERRET
= ret
;
1146 /* Update the global panic/debugger nested entry level */
1147 kernel_debugger_entry_count
= CPUDEBUGGERCOUNT
;
1150 * TODO: Should we do anything special for nested panics here? i.e. if we've trapped more than twice
1151 * should we call into the debugger if it's configured and then reboot if the panic log has been written?
1154 if (CPUDEBUGGEROP
== DBOP_NONE
) {
1155 /* If there was no debugger context setup, we trapped due to a software breakpoint */
1156 debugger_current_op
= DBOP_BREAKPOINT
;
1158 /* Not safe to return from a nested panic/debugger call */
1159 if (debugger_current_op
== DBOP_PANIC
||
1160 debugger_current_op
== DBOP_DEBUGGER
) {
1161 debugger_safe_to_return
= FALSE
;
1164 debugger_current_op
= CPUDEBUGGEROP
;
1166 /* Only overwrite the panic message if there is none already - save the data from the first call */
1167 if (debugger_panic_str
== NULL
) {
1168 debugger_panic_str
= CPUPANICSTR
;
1169 debugger_panic_args
= CPUPANICARGS
;
1170 debugger_panic_data
= CPUPANICDATAPTR
;
1171 debugger_message
= CPUDEBUGGERMSG
;
1172 debugger_panic_caller
= CPUPANICCALLER
;
1175 debugger_panic_options
= CPUPANICOPTS
;
1179 * Clear the op from the processor debugger context so we can handle
1180 * breakpoints in the debugger
1182 CPUDEBUGGEROP
= DBOP_NONE
;
1184 DEBUGGER_TRAP_TIMESTAMP(2);
1186 kdp_callouts(KDP_EVENT_ENTER
);
1189 DEBUGGER_TRAP_TIMESTAMP(3);
1191 if (debugger_current_op
== DBOP_BREAKPOINT
) {
1192 kdp_raise_exception(exception
, code
, subcode
, state
);
1193 } else if (debugger_current_op
== DBOP_STACKSHOT
) {
1194 CPUDEBUGGERRET
= do_stackshot();
1196 } else if (debugger_current_op
== DBOP_RESET_PGO_COUNTERS
) {
1197 CPUDEBUGGERRET
= do_pgo_reset_counters();
1200 debugger_collect_diagnostics(exception
, code
, subcode
, state
);
1203 DEBUGGER_TRAP_TIMESTAMP(4);
1205 not_in_kdp
= initial_not_in_kdp
;
1206 kdp_callouts(KDP_EVENT_EXIT
);
1208 DEBUGGER_TRAP_TIMESTAMP(5);
1210 if (debugger_current_op
!= DBOP_BREAKPOINT
) {
1211 debugger_panic_str
= NULL
;
1212 debugger_panic_args
= NULL
;
1213 debugger_panic_data
= NULL
;
1214 debugger_panic_options
= 0;
1215 debugger_message
= NULL
;
1218 /* Restore the previous debugger state */
1219 debugger_current_op
= db_prev_op
;
1221 DEBUGGER_TRAP_TIMESTAMP(6);
1223 DebuggerResumeOtherCores();
1225 DEBUGGER_TRAP_TIMESTAMP(7);
1229 DEBUGGER_TRAP_TIMESTAMP(8);
1234 __attribute__((noinline
, not_tail_called
))
1236 log(__unused
int level
, char *fmt
, ...)
1238 void *caller
= __builtin_return_address(0);
1247 va_start(listp
, fmt
);
1248 va_copy(listp2
, listp
);
1250 disable_preemption();
1251 _doprnt(fmt
, &listp
, cons_putc_locked
, 0);
1252 enable_preemption();
1256 os_log_with_args(OS_LOG_DEFAULT
, OS_LOG_TYPE_DEFAULT
, fmt
, listp2
, caller
);
1262 * Per <rdar://problem/24974766>, skip appending log messages to
1263 * the new logging infrastructure in contexts where safety is
1264 * uncertain. These contexts include:
1265 * - When we're in the debugger
1266 * - We're in a panic
1267 * - Interrupts are disabled
1268 * - Or Pre-emption is disabled
1269 * In all the above cases, it is potentially unsafe to log messages.
1275 return kernel_debugger_entry_count
== 0 &&
1277 get_preemption_level() == 0 &&
1278 ml_get_interrupts_enabled() == TRUE
;
1282 debug_mode_active(void)
1284 return (0 != kernel_debugger_entry_count
!= 0) || (0 == not_in_kdp
);
1290 if ((debug_buf_size
!= 0) &&
1291 ((debug_buf_ptr
- debug_buf_base
) < (int)debug_buf_size
)) {
1297 #if defined (__x86_64__)
1307 } __attribute__((packed
));
1309 typedef struct pasc pasc_t
;
1312 * In-place packing routines -- inefficient, but they're called at most once.
1313 * Assumes "buflen" is a multiple of 8. Used for compressing paniclogs on x86.
1316 packA(char *inbuf
, uint32_t length
, uint32_t buflen
)
1318 unsigned int i
, j
= 0;
1321 length
= MIN(((length
+ 7) & ~7), buflen
);
1323 for (i
= 0; i
< length
; i
+= 8) {
1325 pack
.b
= inbuf
[i
+ 1];
1326 pack
.c
= inbuf
[i
+ 2];
1327 pack
.d
= inbuf
[i
+ 3];
1328 pack
.e
= inbuf
[i
+ 4];
1329 pack
.f
= inbuf
[i
+ 5];
1330 pack
.g
= inbuf
[i
+ 6];
1331 pack
.h
= inbuf
[i
+ 7];
1332 bcopy((char *) &pack
, inbuf
+ j
, 7);
1339 unpackA(char *inbuf
, uint32_t length
)
1343 length
= (length
* 8) / 7;
1345 while (i
< length
) {
1346 packs
= *(pasc_t
*)&inbuf
[i
];
1347 bcopy(&inbuf
[i
+ 7], &inbuf
[i
+ 8], MAX(0, (int) (length
- i
- 8)));
1348 inbuf
[i
++] = packs
.a
;
1349 inbuf
[i
++] = packs
.b
;
1350 inbuf
[i
++] = packs
.c
;
1351 inbuf
[i
++] = packs
.d
;
1352 inbuf
[i
++] = packs
.e
;
1353 inbuf
[i
++] = packs
.f
;
1354 inbuf
[i
++] = packs
.g
;
1355 inbuf
[i
++] = packs
.h
;
1358 #endif /* defined (__x86_64__) */
1360 extern void *proc_name_address(void *p
);
1363 panic_display_process_name(void)
1365 /* because of scoping issues len(p_comm) from proc_t is hard coded here */
1366 char proc_name
[17] = "Unknown";
1368 void *cbsd_info
= 0;
1370 if (ml_nofault_copy((vm_offset_t
)¤t_thread()->task
, (vm_offset_t
) &ctask
, sizeof(task_t
)) == sizeof(task_t
)) {
1371 if (ml_nofault_copy((vm_offset_t
)&ctask
->bsd_info
, (vm_offset_t
)&cbsd_info
, sizeof(cbsd_info
)) == sizeof(cbsd_info
)) {
1372 if (cbsd_info
&& (ml_nofault_copy((vm_offset_t
) proc_name_address(cbsd_info
), (vm_offset_t
) &proc_name
, sizeof(proc_name
)) > 0)) {
1373 proc_name
[sizeof(proc_name
) - 1] = '\0';
1377 paniclog_append_noflush("\nBSD process name corresponding to current thread: %s\n", proc_name
);
1383 return debugger_panic_str
!= (char *) 0;
1387 populate_model_name(char *model_string
)
1389 strlcpy(model_name
, model_string
, sizeof(model_name
));
1393 panic_display_model_name(void)
1395 char tmp_model_name
[sizeof(model_name
)];
1397 if (ml_nofault_copy((vm_offset_t
) &model_name
, (vm_offset_t
) &tmp_model_name
, sizeof(model_name
)) != sizeof(model_name
)) {
1401 tmp_model_name
[sizeof(tmp_model_name
) - 1] = '\0';
1403 if (tmp_model_name
[0] != 0) {
1404 paniclog_append_noflush("System model name: %s\n", tmp_model_name
);
1409 panic_display_kernel_uuid(void)
1411 char tmp_kernel_uuid
[sizeof(kernel_uuid_string
)];
1413 if (ml_nofault_copy((vm_offset_t
) &kernel_uuid_string
, (vm_offset_t
) &tmp_kernel_uuid
, sizeof(kernel_uuid_string
)) != sizeof(kernel_uuid_string
)) {
1417 if (tmp_kernel_uuid
[0] != '\0') {
1418 paniclog_append_noflush("Kernel UUID: %s\n", tmp_kernel_uuid
);
1423 panic_display_kernel_aslr(void)
1425 if (vm_kernel_slide
) {
1426 paniclog_append_noflush("Kernel slide: 0x%016lx\n", (unsigned long) vm_kernel_slide
);
1427 paniclog_append_noflush("Kernel text base: %p\n", (void *) vm_kernel_stext
);
1432 panic_display_hibb(void)
1434 #if defined(__i386__) || defined (__x86_64__)
1435 paniclog_append_noflush("__HIB text base: %p\n", (void *) vm_hib_base
);
1440 panic_display_uptime(void)
1443 absolutetime_to_nanoseconds(mach_absolute_time(), &uptime
);
1445 paniclog_append_noflush("\nSystem uptime in nanoseconds: %llu\n", uptime
);
1449 panic_display_disk_errors(void)
1451 if (panic_disk_error_description
[0]) {
1452 panic_disk_error_description
[sizeof(panic_disk_error_description
) - 1] = '\0';
1453 paniclog_append_noflush("Root disk errors: \"%s\"\n", panic_disk_error_description
);
1458 panic_display_shutdown_status(void)
1460 #if defined(__i386__) || defined(__x86_64__)
1461 paniclog_append_noflush("System shutdown begun: %s\n", IOPMRootDomainGetWillShutdown() ? "YES" : "NO");
1462 if (gIOPolledCoreFileMode
== kIOPolledCoreFileModeNotInitialized
) {
1463 paniclog_append_noflush("Panic diags file unavailable, panic occurred prior to initialization\n");
1464 } else if (gIOPolledCoreFileMode
!= kIOPolledCoreFileModeDisabled
) {
1466 * If we haven't marked the corefile as explicitly disabled, and we've made it past initialization, then we know the current
1467 * system was configured to use disk based diagnostics at some point.
1469 paniclog_append_noflush("Panic diags file available: %s (0x%x)\n", (gIOPolledCoreFileMode
!= kIOPolledCoreFileModeClosed
) ? "YES" : "NO", kdp_polled_corefile_error());
1474 extern const char version
[];
1475 extern char osversion
[];
1477 static volatile uint32_t config_displayed
= 0;
1479 __private_extern__
void
1480 panic_display_system_configuration(boolean_t launchd_exit
)
1482 if (!launchd_exit
) {
1483 panic_display_process_name();
1485 if (OSCompareAndSwap(0, 1, &config_displayed
)) {
1487 if (!launchd_exit
&& strlcpy(buf
, PE_boot_args(), sizeof(buf
))) {
1488 paniclog_append_noflush("Boot args: %s\n", buf
);
1490 paniclog_append_noflush("\nMac OS version:\n%s\n",
1491 (osversion
[0] != 0) ? osversion
: "Not yet set");
1492 paniclog_append_noflush("\nKernel version:\n%s\n", version
);
1493 panic_display_kernel_uuid();
1494 if (!launchd_exit
) {
1495 panic_display_kernel_aslr();
1496 panic_display_hibb();
1497 panic_display_pal_info();
1499 panic_display_model_name();
1500 panic_display_disk_errors();
1501 panic_display_shutdown_status();
1502 if (!launchd_exit
) {
1503 panic_display_uptime();
1504 panic_display_zprint();
1506 panic_display_ztrace();
1507 #endif /* CONFIG_ZLEAKS */
1508 kext_dump_panic_lists(&paniclog_append_noflush
);
1513 extern unsigned int stack_total
;
1514 extern unsigned long long stack_allocs
;
1516 #if defined (__x86_64__)
1517 extern unsigned int inuse_ptepages_count
;
1518 extern long long alloc_ptepages_count
;
1521 extern boolean_t panic_include_zprint
;
1522 extern mach_memory_info_t
*panic_kext_memory_info
;
1523 extern vm_size_t panic_kext_memory_size
;
1525 __private_extern__
void
1526 panic_display_zprint()
1528 if (panic_include_zprint
== TRUE
) {
1530 struct zone zone_copy
;
1532 paniclog_append_noflush("%-20s %10s %10s\n", "Zone Name", "Cur Size", "Free Size");
1533 for (i
= 0; i
< num_zones
; i
++) {
1534 if (ml_nofault_copy((vm_offset_t
)(&zone_array
[i
]), (vm_offset_t
)&zone_copy
, sizeof(struct zone
)) == sizeof(struct zone
)) {
1535 if (zone_copy
.cur_size
> (1024 * 1024)) {
1536 paniclog_append_noflush("%-20s %10lu %10lu\n", zone_copy
.zone_name
, (uintptr_t)zone_copy
.cur_size
, (uintptr_t)(zone_copy
.countfree
* zone_copy
.elem_size
));
1541 paniclog_append_noflush("%-20s %10lu\n", "Kernel Stacks", (uintptr_t)(kernel_stack_size
* stack_total
));
1543 #if defined (__x86_64__)
1544 paniclog_append_noflush("%-20s %10lu\n", "PageTables", (uintptr_t)(PAGE_SIZE
* inuse_ptepages_count
));
1547 paniclog_append_noflush("%-20s %10lu\n", "Kalloc.Large", (uintptr_t)kalloc_large_total
);
1548 if (panic_kext_memory_info
) {
1549 mach_memory_info_t
*mem_info
= panic_kext_memory_info
;
1550 paniclog_append_noflush("\n%-5s %10s\n", "Kmod", "Size");
1551 for (i
= 0; i
< (panic_kext_memory_size
/ sizeof(mach_zone_info_t
)); i
++) {
1552 if (((mem_info
[i
].flags
& VM_KERN_SITE_TYPE
) == VM_KERN_SITE_KMOD
) && (mem_info
[i
].size
> (1024 * 1024))) {
1553 paniclog_append_noflush("%-5lld %10lld\n", mem_info
[i
].site
, mem_info
[i
].size
);
1560 #if CONFIG_ECC_LOGGING
1561 __private_extern__
void
1562 panic_display_ecc_errors()
1564 uint32_t count
= ecc_log_get_correction_count();
1567 paniclog_append_noflush("ECC Corrections:%u\n", count
);
1570 #endif /* CONFIG_ECC_LOGGING */
1573 extern boolean_t panic_include_ztrace
;
1574 extern struct ztrace
* top_ztrace
;
1575 void panic_print_symbol_name(vm_address_t search
);
1578 * Prints the backtrace most suspected of being a leaker, if we paniced in the zone allocator.
1579 * top_ztrace and panic_include_ztrace comes from osfmk/kern/zalloc.c
1581 __private_extern__
void
1582 panic_display_ztrace(void)
1584 if (panic_include_ztrace
== TRUE
) {
1586 boolean_t keepsyms
= FALSE
;
1588 PE_parse_boot_argn("keepsyms", &keepsyms
, sizeof(keepsyms
));
1589 struct ztrace top_ztrace_copy
;
1591 /* Make sure not to trip another panic if there's something wrong with memory */
1592 if (ml_nofault_copy((vm_offset_t
)top_ztrace
, (vm_offset_t
)&top_ztrace_copy
, sizeof(struct ztrace
)) == sizeof(struct ztrace
)) {
1593 paniclog_append_noflush("\nBacktrace suspected of leaking: (outstanding bytes: %lu)\n", (uintptr_t)top_ztrace_copy
.zt_size
);
1594 /* Print the backtrace addresses */
1595 for (i
= 0; (i
< top_ztrace_copy
.zt_depth
&& i
< MAX_ZTRACE_DEPTH
); i
++) {
1596 paniclog_append_noflush("%p ", top_ztrace_copy
.zt_stack
[i
]);
1598 panic_print_symbol_name((vm_address_t
)top_ztrace_copy
.zt_stack
[i
]);
1600 paniclog_append_noflush("\n");
1602 /* Print any kexts in that backtrace, along with their link addresses so we can properly blame them */
1603 kmod_panic_dump((vm_offset_t
*)&top_ztrace_copy
.zt_stack
[0], top_ztrace_copy
.zt_depth
);
1605 paniclog_append_noflush("\nCan't access top_ztrace...\n");
1607 paniclog_append_noflush("\n");
1610 #endif /* CONFIG_ZLEAKS */
1612 #if !CONFIG_TELEMETRY
1614 telemetry_gather(user_addr_t buffer __unused
, uint32_t *length __unused
, boolean_t mark __unused
)
1616 return KERN_NOT_SUPPORTED
;
1620 #include <machine/machine_cpu.h>
1622 uint32_t kern_feature_overrides
= 0;
1625 kern_feature_override(uint32_t fmask
)
1627 if (kern_feature_overrides
== 0) {
1628 uint32_t fdisables
= 0;
1630 * Expected to be first invoked early, in a single-threaded
1633 if (PE_parse_boot_argn("validation_disables", &fdisables
, sizeof(fdisables
))) {
1634 fdisables
|= KF_INITIALIZED
;
1635 kern_feature_overrides
= fdisables
;
1637 kern_feature_overrides
|= KF_INITIALIZED
;
1640 return (kern_feature_overrides
& fmask
) == fmask
;
1644 on_device_corefile_enabled(void)
1646 assert(debug_boot_arg_inited
);
1647 #if CONFIG_KDP_INTERACTIVE_DEBUGGING
1648 if ((debug_boot_arg
!= 0) && !(debug_boot_arg
& DB_DISABLE_LOCAL_CORE
)) {
1656 panic_stackshot_to_disk_enabled(void)
1658 assert(debug_boot_arg_inited
);
1659 #if defined(__x86_64__)
1660 if (PEGetCoprocessorVersion() < kCoprocessorVersion2
) {
1661 /* Only enabled on pre-Gibraltar machines where it hasn't been disabled explicitly */
1662 if ((debug_boot_arg
!= 0) && (debug_boot_arg
& DB_DISABLE_STACKSHOT_TO_DISK
)) {