2 * Copyright (c) 2000-2016 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>
78 #include <stdatomic.h>
80 #include <console/serial_protos.h>
82 #if !(MACH_KDP && CONFIG_KDP_INTERACTIVE_DEBUGGING)
83 #include <kdp/kdp_udp.h>
85 #include <kern/processor.h>
87 #if defined(__i386__) || defined(__x86_64__)
88 #include <i386/cpu_threads.h>
89 #include <i386/pmCPU.h>
92 #include <IOKit/IOPlatformExpert.h>
93 #include <machine/pal_routines.h>
95 #include <sys/kdebug.h>
96 #include <libkern/OSKextLibPrivate.h>
97 #include <libkern/OSAtomic.h>
98 #include <libkern/kernel_mach_header.h>
99 #include <libkern/section_keywords.h>
100 #include <uuid/uuid.h>
101 #include <mach_debug/zone_info.h>
103 #include <os/log_private.h>
106 #include <pexpert/pexpert.h> /* For gPanicBase */
107 #include <arm/caches_internal.h>
108 #include <arm/misc_protos.h>
109 extern volatile struct xnu_hw_shmem_dbg_command_info
*hwsd_info
;
113 #include <tests/xnupost.h>
114 extern int vsnprintf(char *, size_t, const char *, va_list);
117 unsigned int halt_in_debugger
= 0;
118 unsigned int current_debugger
= 0;
119 unsigned int active_debugger
= 0;
120 unsigned int panicDebugging
= FALSE
;
121 unsigned int kdebug_serial
= FALSE
;
122 unsigned int kernel_debugger_entry_count
= 0;
126 #define TRAP_DEBUGGER __asm__ volatile("trap")
127 #elif defined(__arm64__)
129 * Magic number; this should be identical to the __arm__ encoding for trap.
131 #define TRAP_DEBUGGER __asm__ volatile(".long 0xe7ffdeff")
132 #elif defined (__x86_64__)
133 #define TRAP_DEBUGGER __asm__("int3")
135 #error No TRAP_DEBUGGER for this architecture
138 #if defined(__i386__) || defined(__x86_64__)
139 #define panic_stop() pmCPUHalt(PM_HALT_PANIC)
141 #define panic_stop() panic_spin_forever()
144 #define CPUDEBUGGEROP PROCESSOR_DATA(current_processor(), debugger_state).db_current_op
145 #define CPUDEBUGGERMSG PROCESSOR_DATA(current_processor(), debugger_state).db_message
146 #define CPUPANICSTR PROCESSOR_DATA(current_processor(), debugger_state).db_panic_str
147 #define CPUPANICARGS PROCESSOR_DATA(current_processor(), debugger_state).db_panic_args
148 #define CPUPANICOPTS PROCESSOR_DATA(current_processor(), debugger_state).db_panic_options
149 #define CPUPANICDATAPTR PROCESSOR_DATA(current_processor(), debugger_state).db_panic_data_ptr
150 #define CPUDEBUGGERSYNC PROCESSOR_DATA(current_processor(), debugger_state).db_proceed_on_sync_failure
151 #define CPUDEBUGGERCOUNT PROCESSOR_DATA(current_processor(), debugger_state).db_entry_count
152 #define CPUDEBUGGERRET PROCESSOR_DATA(current_processor(), debugger_state).db_op_return
153 #define CPUPANICCALLER PROCESSOR_DATA(current_processor(), debugger_state).db_panic_caller
155 #if DEVELOPMENT || DEBUG
156 #define DEBUGGER_DEBUGGING_NESTED_PANIC_IF_REQUESTED(requested) \
159 volatile int *badpointer = (int *)4; \
163 #endif /* DEVELOPMENT || DEBUG */
165 debugger_op debugger_current_op
= DBOP_NONE
;
166 const char *debugger_panic_str
= NULL
;
167 va_list *debugger_panic_args
= NULL
;
168 void *debugger_panic_data
= NULL
;
169 uint64_t debugger_panic_options
= 0;
170 const char *debugger_message
= NULL
;
171 unsigned long debugger_panic_caller
= 0;
173 void panic_trap_to_debugger(const char *panic_format_str
, va_list *panic_args
, unsigned int reason
, void *ctx
,
174 uint64_t panic_options_mask
, void *panic_data
, unsigned long panic_caller
);
175 static void kdp_machine_reboot_type(unsigned int type
);
176 __attribute__((noreturn
)) void panic_spin_forever(void);
177 extern kern_return_t
do_stackshot(void);
181 #define NESTEDDEBUGGERENTRYMAX 5
184 #define DEBUG_BUF_SIZE (4096)
185 #define KDBG_TRACE_PANIC_FILENAME "/var/log/panic.trace"
188 * EXTENDED_/DEBUG_BUF_SIZE can't grow without updates to SMC and iBoot to store larger panic logs on co-processor systems */
189 #define DEBUG_BUF_SIZE ((3 * PAGE_SIZE) + offsetof(struct macos_panic_header, mph_data))
190 #define EXTENDED_DEBUG_BUF_SIZE 0x0013ff80
191 static_assert(((EXTENDED_DEBUG_BUF_SIZE
% PANIC_FLUSH_BOUNDARY
) == 0), "Extended debug buf size must match SMC alignment requirements");
192 #define KDBG_TRACE_PANIC_FILENAME "/var/tmp/panic.trace"
195 /* debug_buf is directly linked with iBoot panic region for embedded targets */
197 char *debug_buf_base
= NULL
;
198 char *debug_buf_ptr
= NULL
;
199 unsigned int debug_buf_size
= 0;
201 char debug_buf
[DEBUG_BUF_SIZE
];
202 struct macos_panic_header
*panic_info
= (struct macos_panic_header
*)debug_buf
;
203 char *debug_buf_base
= (debug_buf
+ offsetof(struct macos_panic_header
, mph_data
));
204 char *debug_buf_ptr
= (debug_buf
+ offsetof(struct macos_panic_header
, mph_data
));
207 * We don't include the size of the panic header in the length of the data we actually write.
208 * On co-processor platforms, we lose sizeof(struct macos_panic_header) bytes from the end of
209 * the end of the log because we only support writing (3*PAGESIZE) bytes.
211 unsigned int debug_buf_size
= (DEBUG_BUF_SIZE
- offsetof(struct macos_panic_header
, mph_data
));
213 boolean_t extended_debug_log_enabled
= FALSE
;
217 atomic_int debugger_cpu
= ATOMIC_VAR_INIT(DEBUGGER_NO_CPU
);
218 boolean_t debugger_allcpus_halted
= FALSE
;
219 boolean_t debugger_safe_to_return
= TRUE
;
220 unsigned int debugger_context
= 0;
222 static char model_name
[64];
223 unsigned char *kernel_uuid
;
225 boolean_t kernelcache_uuid_valid
= FALSE
;
226 uuid_t kernelcache_uuid
;
227 uuid_string_t kernelcache_uuid_string
;
230 * By default we treat Debugger() the same as calls to panic(), unless
231 * we have debug boot-args present and the DB_KERN_DUMP_ON_NMI *NOT* set.
232 * If DB_KERN_DUMP_ON_NMI is *NOT* set, return from Debugger() is supported.
234 * Return from Debugger() is currently only implemented on x86
236 static boolean_t debugger_is_panic
= TRUE
;
238 #if DEVELOPMENT || DEBUG
239 boolean_t debug_boot_arg_inited
= FALSE
;
242 SECURITY_READ_ONLY_LATE(unsigned int) debug_boot_arg
;
244 char kernel_uuid_string
[37]; /* uuid_string_t */
245 char kernelcache_uuid_string
[37]; /* uuid_string_t */
246 char panic_disk_error_description
[512];
247 size_t panic_disk_error_description_size
= sizeof(panic_disk_error_description
);
249 extern unsigned int write_trace_on_panic
;
250 int kext_assertions_enable
=
251 #if DEBUG || DEVELOPMENT
260 unsigned long uuidlen
= 0;
263 uuid
= getuuidfromheader(&_mh_execute_header
, &uuidlen
);
264 if ((uuid
!= NULL
) && (uuidlen
== sizeof(uuid_t
))) {
266 uuid_unparse_upper(*(uuid_t
*)uuid
, kernel_uuid_string
);
269 if (!PE_parse_boot_argn("assertions", &mach_assert
, sizeof(mach_assert
))) {
274 * Initialize the value of the debug boot-arg
277 #if ((CONFIG_EMBEDDED && MACH_KDP) || defined(__x86_64__))
278 if (PE_parse_boot_argn("debug", &debug_boot_arg
, sizeof (debug_boot_arg
))) {
279 #if DEVELOPMENT || DEBUG
280 if (debug_boot_arg
& DB_HALT
) {
286 if (debug_boot_arg
& DB_NMI
) {
287 panicDebugging
= TRUE
;
290 panicDebugging
= TRUE
;
291 #if KDEBUG_MOJO_TRACE
292 if (debug_boot_arg
& DB_PRT_KDEBUG
) {
293 kdebug_serial
= TRUE
;
296 #endif /* CONFIG_EMBEDDED */
298 #endif /* ((CONFIG_EMBEDDED && MACH_KDP) || defined(__x86_64__)) */
300 #if DEVELOPMENT || DEBUG
301 debug_boot_arg_inited
= TRUE
;
306 * By default we treat Debugger() the same as calls to panic(), unless
307 * we have debug boot-args present and the DB_KERN_DUMP_ON_NMI *NOT* set.
308 * If DB_KERN_DUMP_ON_NMI is *NOT* set, return from Debugger() is supported.
309 * This is because writing an on-device corefile is a destructive operation.
311 * Return from Debugger() is currently only implemented on x86
313 if (PE_i_can_has_debugger(NULL
) && !(debug_boot_arg
& DB_KERN_DUMP_ON_NMI
)) {
314 debugger_is_panic
= FALSE
;
320 #if defined (__x86_64__)
322 extended_debug_log_init(void)
324 assert(coprocessor_paniclog_flush
);
326 * Allocate an extended panic log buffer that has space for the panic
327 * stackshot at the end. Update the debug buf pointers appropriately
328 * to point at this new buffer.
330 char *new_debug_buf
= kalloc(EXTENDED_DEBUG_BUF_SIZE
);
332 * iBoot pre-initializes the panic region with the NULL character. We set this here
333 * so we can accurately calculate the CRC for the region without needing to flush the
334 * full region over SMC.
336 memset(new_debug_buf
, '\0', EXTENDED_DEBUG_BUF_SIZE
);
338 panic_info
= (struct macos_panic_header
*)new_debug_buf
;
339 debug_buf_ptr
= debug_buf_base
= (new_debug_buf
+ offsetof(struct macos_panic_header
, mph_data
));
340 debug_buf_size
= (EXTENDED_DEBUG_BUF_SIZE
- offsetof(struct macos_panic_header
, mph_data
));
342 extended_debug_log_enabled
= TRUE
;
344 #endif /* defined (__x86_64__) */
351 printf("debug_log_init: Error!! gPanicBase is still not initialized\n");
354 /* Shift debug buf start location and size by the length of the panic header */
355 debug_buf_base
= (char *)gPanicBase
+ sizeof(struct embedded_panic_header
);
356 debug_buf_ptr
= debug_buf_base
;
357 debug_buf_size
= gPanicSize
- sizeof(struct embedded_panic_header
);
359 bzero(panic_info
, DEBUG_BUF_SIZE
);
361 assert(debug_buf_base
!= NULL
);
362 assert(debug_buf_ptr
!= NULL
);
363 assert(debug_buf_size
!= 0);
370 int my_cpu
= cpu_number();
371 int debugger_exp_cpu
= DEBUGGER_NO_CPU
;
372 assert(ml_get_interrupts_enabled() == FALSE
);
374 if (debugger_cpu
== my_cpu
) {
378 while(!atomic_compare_exchange_strong(&debugger_cpu
, &debugger_exp_cpu
, my_cpu
)) {
379 debugger_exp_cpu
= DEBUGGER_NO_CPU
;
388 assert(debugger_cpu
== cpu_number());
391 * We don't do an atomic exchange here in case
392 * there's another CPU spinning to acquire the debugger_lock
393 * and we never get a chance to update it. We already have the
394 * lock so we can simply store DEBUGGER_NO_CPU and follow with
397 debugger_cpu
= DEBUGGER_NO_CPU
;
404 DebuggerHaltOtherCores(boolean_t proceed_on_failure
)
407 return DebuggerXCallEnter(proceed_on_failure
);
408 #else /* CONFIG_EMBEDDED */
409 #pragma unused(proceed_on_failure)
410 mp_kdp_enter(proceed_on_failure
);
416 DebuggerResumeOtherCores()
419 DebuggerXCallReturn();
420 #else /* CONFIG_EMBEDDED */
426 DebuggerSaveState(debugger_op db_op
, const char *db_message
, const char *db_panic_str
,
427 va_list *db_panic_args
, uint64_t db_panic_options
, void *db_panic_data_ptr
,
428 boolean_t db_proceed_on_sync_failure
, unsigned long db_panic_caller
)
430 CPUDEBUGGEROP
= db_op
;
432 /* Preserve the original panic message */
433 if (CPUDEBUGGERCOUNT
== 1 || CPUPANICSTR
== NULL
) {
434 CPUDEBUGGERMSG
= db_message
;
435 CPUPANICSTR
= db_panic_str
;
436 CPUPANICARGS
= db_panic_args
;
437 CPUPANICDATAPTR
= db_panic_data_ptr
;
438 CPUPANICCALLER
= db_panic_caller
;
439 } else if (CPUDEBUGGERCOUNT
> 1 && db_panic_str
!= NULL
) {
440 kprintf("Nested panic detected:");
441 if (db_panic_str
!= NULL
)
442 _doprnt(db_panic_str
, db_panic_args
, PE_kputc
, 0);
445 CPUDEBUGGERSYNC
= db_proceed_on_sync_failure
;
446 CPUDEBUGGERRET
= KERN_SUCCESS
;
448 /* Reset these on any nested panics */
449 CPUPANICOPTS
= db_panic_options
;
455 * Save the requested debugger state/action into the current processor's processor_data
456 * and trap to the debugger.
459 DebuggerTrapWithState(debugger_op db_op
, const char *db_message
, const char *db_panic_str
,
460 va_list *db_panic_args
, uint64_t db_panic_options
, void *db_panic_data_ptr
,
461 boolean_t db_proceed_on_sync_failure
, unsigned long db_panic_caller
)
465 assert(ml_get_interrupts_enabled() == FALSE
);
466 DebuggerSaveState(db_op
, db_message
, db_panic_str
, db_panic_args
,
467 db_panic_options
, db_panic_data_ptr
,
468 db_proceed_on_sync_failure
, db_panic_caller
);
472 ret
= CPUDEBUGGERRET
;
474 DebuggerSaveState(DBOP_NONE
, NULL
, NULL
, NULL
, 0, NULL
, FALSE
, 0);
479 void __attribute__((noinline
))
483 const char *expression
487 kprintf("%s:%d non-fatal Assertion: %s", file
, line
, expression
);
491 panic_plain("%s:%d Assertion failed: %s", file
, line
, expression
);
496 Debugger(const char *message
)
498 DebuggerWithContext(0, NULL
, message
, DEBUGGER_OPTION_NONE
);
502 DebuggerWithContext(unsigned int reason
, void *ctx
, const char *message
,
503 uint64_t debugger_options_mask
)
505 spl_t previous_interrupts_state
;
506 boolean_t old_doprnt_hide_pointers
= doprnt_hide_pointers
;
508 previous_interrupts_state
= ml_set_interrupts_enabled(FALSE
);
509 disable_preemption();
513 if (CPUDEBUGGERCOUNT
> NESTEDDEBUGGERENTRYMAX
) {
514 static boolean_t in_panic_kprintf
= FALSE
;
516 /* Notify any listeners that we've started a panic */
517 PEHaltRestart(kPEPanicBegin
);
519 if (!in_panic_kprintf
) {
520 in_panic_kprintf
= TRUE
;
521 kprintf("Detected nested debugger entry count exceeding %d\n",
522 NESTEDDEBUGGERENTRYMAX
);
523 in_panic_kprintf
= FALSE
;
526 if (!panicDebugging
) {
527 kdp_machine_reboot_type(kPEPanicRestartCPU
);
530 panic_spin_forever();
533 #if DEVELOPMENT || DEBUG
534 DEBUGGER_DEBUGGING_NESTED_PANIC_IF_REQUESTED((debugger_options_mask
& DEBUGGER_OPTION_RECURPANIC_ENTRY
));
537 doprnt_hide_pointers
= FALSE
;
540 DebuggerSaveState(DBOP_DEBUGGER
, message
,
541 NULL
, NULL
, debugger_options_mask
, NULL
, TRUE
, 0);
542 handle_debugger_trap(reason
, 0, 0, ctx
);
543 DebuggerSaveState(DBOP_NONE
, NULL
, NULL
,
544 NULL
, 0, NULL
, FALSE
, 0);
546 DebuggerTrapWithState(DBOP_DEBUGGER
, message
,
547 NULL
, NULL
, debugger_options_mask
, NULL
, TRUE
, 0);
551 doprnt_hide_pointers
= old_doprnt_hide_pointers
;
553 ml_set_interrupts_enabled(previous_interrupts_state
);
556 static struct kdp_callout
{
557 struct kdp_callout
* callout_next
;
558 kdp_callout_fn_t callout_fn
;
559 boolean_t callout_in_progress
;
561 } * kdp_callout_list
= NULL
;
564 * Called from kernel context to register a kdp event callout.
567 kdp_register_callout(kdp_callout_fn_t fn
, void * arg
)
569 struct kdp_callout
* kcp
;
570 struct kdp_callout
* list_head
;
572 kcp
= kalloc(sizeof(*kcp
));
574 panic("kdp_register_callout() kalloc failed");
576 kcp
->callout_fn
= fn
;
577 kcp
->callout_arg
= arg
;
578 kcp
->callout_in_progress
= FALSE
;
580 /* Lock-less list insertion using compare and exchange. */
582 list_head
= kdp_callout_list
;
583 kcp
->callout_next
= list_head
;
584 } while (!OSCompareAndSwapPtr(list_head
, kcp
, &kdp_callout_list
));
588 kdp_callouts(kdp_event_t event
)
590 struct kdp_callout
*kcp
= kdp_callout_list
;
593 if (!kcp
->callout_in_progress
) {
594 kcp
->callout_in_progress
= TRUE
;
595 kcp
->callout_fn(kcp
->callout_arg
, event
);
596 kcp
->callout_in_progress
= FALSE
;
598 kcp
= kcp
->callout_next
;
603 * An overview of the xnu panic path:
605 * Several panic wrappers (panic(), panic_with_options(), etc.) all funnel into panic_trap_to_debugger().
606 * panic_trap_to_debugger() sets the panic state in the current processor's processor_data_t prior
607 * to trapping into the debugger. Once we trap to the debugger, we end up in handle_debugger_trap()
608 * which tries to acquire the panic lock by atomically swapping the current CPU number into debugger_cpu.
609 * debugger_cpu acts as a synchronization point, from which the winning CPU can halt the other cores and
610 * continue to debugger_collect_diagnostics() where we write the paniclog, corefile (if appropriate) and proceed
611 * according to the device's boot-args.
615 panic(const char *str
, ...)
617 va_list panic_str_args
;
619 va_start(panic_str_args
, str
);
620 panic_trap_to_debugger(str
, &panic_str_args
, 0, NULL
, 0, NULL
, (unsigned long)(char *)__builtin_return_address(0));
621 va_end(panic_str_args
);
625 panic_with_options(unsigned int reason
, void *ctx
, uint64_t debugger_options_mask
, const char *str
, ...)
627 va_list panic_str_args
;
629 va_start(panic_str_args
, str
);
630 panic_trap_to_debugger(str
, &panic_str_args
, reason
, ctx
, (debugger_options_mask
& ~DEBUGGER_INTERNAL_OPTIONS_MASK
),
631 NULL
, (unsigned long)(char *)__builtin_return_address(0));
632 va_end(panic_str_args
);
635 #if defined (__x86_64__)
637 * panic_with_thread_context() is used on x86 platforms to specify a different thread that should be backtraced in the paniclog.
638 * We don't generally need this functionality on embedded platforms because embedded platforms include a panic time stackshot
639 * from customer devices. We plumb the thread pointer via the debugger trap mechanism and backtrace the kernel stack from the
640 * thread when writing the panic log.
642 * NOTE: panic_with_thread_context() should be called with an explicit thread reference held on the passed thread.
645 panic_with_thread_context(unsigned int reason
, void *ctx
, uint64_t debugger_options_mask
, thread_t thread
, const char *str
, ...)
647 va_list panic_str_args
;
648 __assert_only
uint32_t th_ref_count
;
650 assert_thread_magic(thread
);
651 th_ref_count
= atomic_load_explicit(&thread
->ref_count
, memory_order_acquire
);
652 assertf(th_ref_count
> 0, "panic_with_thread_context called with invalid thread %p with refcount %u", thread
, th_ref_count
);
654 /* Take a reference on the thread so it doesn't disappear by the time we try to backtrace it */
655 thread_reference(thread
);
657 va_start(panic_str_args
, str
);
658 panic_trap_to_debugger(str
, &panic_str_args
, reason
, ctx
, ((debugger_options_mask
& ~DEBUGGER_INTERNAL_OPTIONS_MASK
) | DEBUGGER_INTERNAL_OPTION_THREAD_BACKTRACE
),
659 thread
, (unsigned long)(char *)__builtin_return_address(0));
661 va_end(panic_str_args
);
664 #endif /* defined (__x86_64__) */
666 #pragma clang diagnostic push
667 #pragma clang diagnostic ignored "-Wmissing-noreturn"
669 panic_trap_to_debugger(const char *panic_format_str
, va_list *panic_args
, unsigned int reason
, void *ctx
,
670 uint64_t panic_options_mask
, void *panic_data_ptr
, unsigned long panic_caller
)
672 #pragma clang diagnostic pop
674 if (ml_wants_panic_trap_to_debugger()) {
675 ml_panic_trap_to_debugger(panic_format_str
, panic_args
, reason
, ctx
, panic_options_mask
, panic_caller
);
678 * This should not return, but we return here for the tail call
679 * as it simplifies the backtrace.
686 if (CPUDEBUGGERCOUNT
> NESTEDDEBUGGERENTRYMAX
) {
687 static boolean_t in_panic_kprintf
= FALSE
;
689 /* Notify any listeners that we've started a panic */
690 PEHaltRestart(kPEPanicBegin
);
692 if (!in_panic_kprintf
) {
693 in_panic_kprintf
= TRUE
;
694 kprintf("Detected nested debugger entry count exceeding %d\n",
695 NESTEDDEBUGGERENTRYMAX
);
696 in_panic_kprintf
= FALSE
;
699 if (!panicDebugging
) {
700 kdp_machine_reboot_type(kPEPanicRestartCPU
);
703 panic_spin_forever();
706 #if DEVELOPMENT || DEBUG
707 DEBUGGER_DEBUGGING_NESTED_PANIC_IF_REQUESTED((panic_options_mask
& DEBUGGER_OPTION_RECURPANIC_ENTRY
));
711 if (PE_arm_debug_panic_hook
)
712 PE_arm_debug_panic_hook(panic_format_str
);
715 #if defined (__x86_64__)
719 if (write_trace_on_panic
&& kdebug_enable
) {
720 if (get_preemption_level() == 0 && !ml_at_interrupt_context()) {
721 ml_set_interrupts_enabled(TRUE
);
722 KDBG_RELEASE(TRACE_PANIC
);
723 kdbg_dump_trace_to_file(KDBG_TRACE_PANIC_FILENAME
);
727 ml_set_interrupts_enabled(FALSE
);
728 disable_preemption();
730 #if defined (__x86_64__)
731 pmSafeMode(x86_lcpu(), PM_SAFE_FL_SAFE
);
732 #endif /* defined (__x86_64__) */
734 /* Never hide pointers from panic logs. */
735 doprnt_hide_pointers
= FALSE
;
739 * We called into panic from a trap, no need to trap again. Set the
740 * state on the current CPU and then jump to handle_debugger_trap.
742 DebuggerSaveState(DBOP_PANIC
, "panic",
743 panic_format_str
, panic_args
,
744 panic_options_mask
, panic_data_ptr
, TRUE
, panic_caller
);
745 handle_debugger_trap(reason
, 0, 0, ctx
);
748 #if defined(__arm64__)
750 * Signal to fastsim that it should open debug ports (nop on hardware)
752 __asm__
volatile("HINT 0x45");
753 #endif /* defined(__arm64__) */
755 DebuggerTrapWithState(DBOP_PANIC
, "panic", panic_format_str
,
756 panic_args
, panic_options_mask
, panic_data_ptr
, TRUE
, panic_caller
);
764 __attribute__((noreturn
))
768 paniclog_append_noflush("\nPlease go to https://panic.apple.com to report this panic\n");
774 kdp_machine_reboot_type(unsigned int type
)
776 printf("Attempting system restart...");
782 kdp_machine_reboot(void)
784 kdp_machine_reboot_type(kPEPanicRestartCPU
);
788 * Gather and save diagnostic information about a panic (or Debugger call).
790 * On embedded, Debugger and Panic are treated very similarly -- WDT uses Debugger so we can
791 * theoretically return from it. On desktop, Debugger is treated as a conventional debugger -- i.e no
792 * paniclog is written and no core is written unless we request a core on NMI.
794 * This routine handles kicking off local coredumps, paniclogs, calling into the Debugger/KDP (if it's configured),
795 * and calling out to any other functions we have for collecting diagnostic info.
798 debugger_collect_diagnostics(unsigned int exception
, unsigned int code
, unsigned int subcode
, void *state
)
800 #if DEVELOPMENT || DEBUG
801 DEBUGGER_DEBUGGING_NESTED_PANIC_IF_REQUESTED((debugger_panic_options
& DEBUGGER_OPTION_RECURPANIC_PRELOG
));
804 #if defined(__x86_64__)
805 kprintf("Debugger called: <%s>\n", debugger_message
? debugger_message
: "");
808 * DB_HALT (halt_in_debugger) can be requested on startup, we shouldn't generate
809 * a coredump/paniclog for this type of debugger entry. If KDP isn't configured,
810 * we'll just spin in kdp_raise_exception.
812 if (debugger_current_op
== DBOP_DEBUGGER
&& halt_in_debugger
) {
813 kdp_raise_exception(exception
, code
, subcode
, state
);
814 if (debugger_safe_to_return
&& !debugger_is_panic
) {
819 if ((debugger_current_op
== DBOP_PANIC
) ||
820 ((debugger_current_op
== DBOP_DEBUGGER
) && debugger_is_panic
)) {
822 * Attempt to notify listeners once and only once that we've started
823 * panicking. Only do this for Debugger() calls if we're treating
824 * Debugger() calls like panic().
826 PEHaltRestart(kPEPanicBegin
);
829 * Set the begin pointer in the panic log structure. We key off of this
830 * static variable rather than contents from the panic header itself in case someone
831 * has stomped over the panic_info structure. Also initializes the header magic.
833 static boolean_t began_writing_paniclog
= FALSE
;
834 if (!began_writing_paniclog
) {
835 PE_init_panicheader();
836 began_writing_paniclog
= TRUE
;
839 * If we reached here, update the panic header to keep it as consistent
840 * as possible during a nested panic
842 PE_update_panicheader_nestedpanic();
847 * Write panic string if this was a panic.
849 * TODO: Consider moving to SavePanicInfo as this is part of the panic log.
851 if (debugger_current_op
== DBOP_PANIC
) {
852 paniclog_append_noflush("panic(cpu %d caller 0x%lx): ", (unsigned) cpu_number(), debugger_panic_caller
);
853 if (debugger_panic_str
) {
854 _doprnt(debugger_panic_str
, debugger_panic_args
, consdebug_putc
, 0);
856 paniclog_append_noflush("\n");
858 #if defined(__x86_64__)
859 else if (((debugger_current_op
== DBOP_DEBUGGER
) && debugger_is_panic
)) {
860 paniclog_append_noflush("Debugger called: <%s>\n", debugger_message
? debugger_message
: "");
864 * Debugger() is treated like panic() on embedded -- for example we use it for WDT
865 * panics (so we need to write a paniclog). On desktop Debugger() is used in the
866 * conventional sense.
868 if (debugger_current_op
== DBOP_PANIC
|| ((debugger_current_op
== DBOP_DEBUGGER
) && debugger_is_panic
))
871 kdp_callouts(KDP_EVENT_PANICLOG
);
874 * Write paniclog and panic stackshot (if supported)
875 * TODO: Need to clear panic log when return from debugger
876 * hooked up for embedded
878 SavePanicInfo(debugger_message
, debugger_panic_data
, debugger_panic_options
);
880 #if DEVELOPMENT || DEBUG
881 DEBUGGER_DEBUGGING_NESTED_PANIC_IF_REQUESTED((debugger_panic_options
& DEBUGGER_OPTION_RECURPANIC_POSTLOG
));
884 /* DEBUGGER_OPTION_PANICLOGANDREBOOT is used for two finger resets on embedded so we get a paniclog */
885 if (debugger_panic_options
& DEBUGGER_OPTION_PANICLOGANDREBOOT
)
886 PEHaltRestart(kPEPanicRestartCPU
);
889 #if CONFIG_KDP_INTERACTIVE_DEBUGGING
891 * If reboot on panic is enabled and the caller of panic indicated that we should skip
892 * local coredumps, don't try to write these and instead go straight to reboot. This
893 * allows us to persist any data that's stored in the panic log.
895 if ((debugger_panic_options
& DEBUGGER_OPTION_SKIP_LOCAL_COREDUMP
) &&
896 (debug_boot_arg
& DB_REBOOT_POST_CORE
)) {
897 kdp_machine_reboot_type(kPEPanicRestartCPU
);
901 * Consider generating a local corefile if the infrastructure is configured
902 * and we haven't disabled on-device coredumps.
904 if (!(debug_boot_arg
& DB_DISABLE_LOCAL_CORE
)) {
905 if (!kdp_has_polled_corefile()) {
906 if (debug_boot_arg
& (DB_KERN_DUMP_ON_PANIC
| DB_KERN_DUMP_ON_NMI
)) {
907 paniclog_append_noflush("skipping local kernel core because core file could not be opened prior to panic (error : 0x%x)",
908 kdp_polled_corefile_error());
910 panic_info
->eph_panic_flags
|= EMBEDDED_PANIC_HEADER_FLAG_COREDUMP_FAILED
;
912 #else /* CONFIG_EMBEDDED */
913 if (panic_info
->mph_panic_log_offset
!= 0) {
914 panic_info
->mph_panic_flags
|= MACOS_PANIC_HEADER_FLAG_COREDUMP_FAILED
;
917 #endif /* CONFIG_EMBEDDED */
922 #if defined (__x86_64__)
923 /* On x86 we don't do a coredump on Debugger unless the DB_KERN_DUMP_ON_NMI boot-arg is specified. */
924 if (debugger_current_op
!= DBOP_DEBUGGER
|| (debug_boot_arg
& DB_KERN_DUMP_ON_NMI
))
928 * Doing an on-device coredump leaves the disk driver in a state
929 * that can not be resumed.
931 debugger_safe_to_return
= FALSE
;
932 begin_panic_transfer();
933 ret
= kern_dump(KERN_DUMP_DISK
);
934 abort_panic_transfer();
936 #if DEVELOPMENT || DEBUG
937 DEBUGGER_DEBUGGING_NESTED_PANIC_IF_REQUESTED((debugger_panic_options
& DEBUGGER_OPTION_RECURPANIC_POSTCORE
));
942 * If DB_REBOOT_POST_CORE is set, then reboot if coredump is sucessfully saved
943 * or if option to ignore failures is set.
945 if ((debug_boot_arg
& DB_REBOOT_POST_CORE
) &&
946 ((ret
== 0) || (debugger_panic_options
& DEBUGGER_OPTION_ATTEMPTCOREDUMPANDREBOOT
))) {
947 kdp_machine_reboot_type(kPEPanicRestartCPU
);
952 if (debug_boot_arg
& DB_REBOOT_ALWAYS
) {
953 kdp_machine_reboot_type(kPEPanicRestartCPU
);
956 /* If KDP is configured, try to trap to the debugger */
957 if (current_debugger
!= NO_CUR_DB
) {
958 kdp_raise_exception(exception
, code
, subcode
, state
);
960 * Only return if we entered via Debugger and it's safe to return
961 * (we halted the other cores successfully, this isn't a nested panic, etc)
963 if (debugger_current_op
== DBOP_DEBUGGER
&&
964 debugger_safe_to_return
&&
965 kernel_debugger_entry_count
== 1 &&
966 !debugger_is_panic
) {
972 if (panicDebugging
) {
973 /* If panic debugging is configured, spin for astris to connect */
976 #endif /* CONFIG_EMBEDDED */
977 #endif /* CONFIG_KDP_INTERACTIVE_DEBUGGING */
979 if (!panicDebugging
) {
980 kdp_machine_reboot_type(kPEPanicRestartCPU
);
983 panic_spin_forever();
986 #if INTERRUPT_MASKED_DEBUG
987 uint64_t debugger_trap_timestamps
[9];
988 # define DEBUGGER_TRAP_TIMESTAMP(i) debugger_trap_timestamps[i] = mach_absolute_time();
990 # define DEBUGGER_TRAP_TIMESTAMP(i)
994 handle_debugger_trap(unsigned int exception
, unsigned int code
, unsigned int subcode
, void *state
)
996 unsigned int initial_not_in_kdp
= not_in_kdp
;
998 debugger_op db_prev_op
= debugger_current_op
;
1000 DEBUGGER_TRAP_TIMESTAMP(0);
1003 ret
= DebuggerHaltOtherCores(CPUDEBUGGERSYNC
);
1005 DEBUGGER_TRAP_TIMESTAMP(1);
1007 #if INTERRUPT_MASKED_DEBUG
1008 if (serialmode
& SERIALMODE_OUTPUT
) {
1009 ml_spin_debug_reset(current_thread());
1012 if (ret
!= KERN_SUCCESS
) {
1013 CPUDEBUGGERRET
= ret
;
1018 /* Update the global panic/debugger nested entry level */
1019 kernel_debugger_entry_count
= CPUDEBUGGERCOUNT
;
1022 * TODO: Should we do anything special for nested panics here? i.e. if we've trapped more than twice
1023 * should we call into the debugger if it's configured and then reboot if the panic log has been written?
1026 if (CPUDEBUGGEROP
== DBOP_NONE
) {
1027 /* If there was no debugger context setup, we trapped due to a software breakpoint */
1028 debugger_current_op
= DBOP_BREAKPOINT
;
1030 /* Not safe to return from a nested panic/debugger call */
1031 if (debugger_current_op
== DBOP_PANIC
||
1032 debugger_current_op
== DBOP_DEBUGGER
) {
1033 debugger_safe_to_return
= FALSE
;
1036 debugger_current_op
= CPUDEBUGGEROP
;
1038 /* Only overwrite the panic message if there is none already - save the data from the first call */
1039 if (debugger_panic_str
== NULL
) {
1040 debugger_panic_str
= CPUPANICSTR
;
1041 debugger_panic_args
= CPUPANICARGS
;
1042 debugger_panic_data
= CPUPANICDATAPTR
;
1043 debugger_message
= CPUDEBUGGERMSG
;
1044 debugger_panic_caller
= CPUPANICCALLER
;
1047 debugger_panic_options
= CPUPANICOPTS
;
1051 * Clear the op from the processor debugger context so we can handle
1052 * breakpoints in the debugger
1054 CPUDEBUGGEROP
= DBOP_NONE
;
1056 DEBUGGER_TRAP_TIMESTAMP(2);
1058 kdp_callouts(KDP_EVENT_ENTER
);
1061 DEBUGGER_TRAP_TIMESTAMP(3);
1063 if (debugger_current_op
== DBOP_BREAKPOINT
) {
1064 kdp_raise_exception(exception
, code
, subcode
, state
);
1065 } else if (debugger_current_op
== DBOP_STACKSHOT
) {
1066 CPUDEBUGGERRET
= do_stackshot();
1068 } else if (debugger_current_op
== DBOP_RESET_PGO_COUNTERS
) {
1069 CPUDEBUGGERRET
= do_pgo_reset_counters();
1072 debugger_collect_diagnostics(exception
, code
, subcode
, state
);
1075 DEBUGGER_TRAP_TIMESTAMP(4);
1077 not_in_kdp
= initial_not_in_kdp
;
1078 kdp_callouts(KDP_EVENT_EXIT
);
1080 DEBUGGER_TRAP_TIMESTAMP(5);
1082 if (debugger_current_op
!= DBOP_BREAKPOINT
) {
1083 debugger_panic_str
= NULL
;
1084 debugger_panic_args
= NULL
;
1085 debugger_panic_data
= NULL
;
1086 debugger_panic_options
= 0;
1087 debugger_message
= NULL
;
1090 /* Restore the previous debugger state */
1091 debugger_current_op
= db_prev_op
;
1093 DEBUGGER_TRAP_TIMESTAMP(6);
1095 DebuggerResumeOtherCores();
1097 DEBUGGER_TRAP_TIMESTAMP(7);
1101 DEBUGGER_TRAP_TIMESTAMP(8);
1106 __attribute__((noinline
,not_tail_called
))
1107 void log(__unused
int level
, char *fmt
, ...)
1109 void *caller
= __builtin_return_address(0);
1118 va_start(listp
, fmt
);
1119 va_copy(listp2
, listp
);
1121 disable_preemption();
1122 _doprnt(fmt
, &listp
, cons_putc_locked
, 0);
1123 enable_preemption();
1127 os_log_with_args(OS_LOG_DEFAULT
, OS_LOG_TYPE_DEFAULT
, fmt
, listp2
, caller
);
1133 * Per <rdar://problem/24974766>, skip appending log messages to
1134 * the new logging infrastructure in contexts where safety is
1135 * uncertain. These contexts include:
1136 * - When we're in the debugger
1137 * - We're in a panic
1138 * - Interrupts are disabled
1139 * - Or Pre-emption is disabled
1140 * In all the above cases, it is potentially unsafe to log messages.
1144 oslog_is_safe(void) {
1145 return (kernel_debugger_entry_count
== 0 &&
1147 get_preemption_level() == 0 &&
1148 ml_get_interrupts_enabled() == TRUE
);
1152 debug_mode_active(void)
1154 return ((0 != kernel_debugger_entry_count
!= 0) || (0 == not_in_kdp
));
1160 if ((debug_buf_size
!= 0) &&
1161 ((debug_buf_ptr
- debug_buf_base
) < (int)debug_buf_size
)) {
1167 #if defined (__x86_64__)
1177 } __attribute__((packed
));
1179 typedef struct pasc pasc_t
;
1182 * In-place packing routines -- inefficient, but they're called at most once.
1183 * Assumes "buflen" is a multiple of 8. Used for compressing paniclogs on x86.
1186 packA(char *inbuf
, uint32_t length
, uint32_t buflen
)
1188 unsigned int i
, j
= 0;
1191 length
= MIN(((length
+ 7) & ~7), buflen
);
1193 for (i
= 0; i
< length
; i
+=8)
1196 pack
.b
= inbuf
[i
+1];
1197 pack
.c
= inbuf
[i
+2];
1198 pack
.d
= inbuf
[i
+3];
1199 pack
.e
= inbuf
[i
+4];
1200 pack
.f
= inbuf
[i
+5];
1201 pack
.g
= inbuf
[i
+6];
1202 pack
.h
= inbuf
[i
+7];
1203 bcopy ((char *) &pack
, inbuf
+ j
, 7);
1210 unpackA(char *inbuf
, uint32_t length
)
1214 length
= (length
* 8)/7;
1216 while (i
< length
) {
1217 packs
= *(pasc_t
*)&inbuf
[i
];
1218 bcopy(&inbuf
[i
+7], &inbuf
[i
+8], MAX(0, (int) (length
- i
- 8)));
1219 inbuf
[i
++] = packs
.a
;
1220 inbuf
[i
++] = packs
.b
;
1221 inbuf
[i
++] = packs
.c
;
1222 inbuf
[i
++] = packs
.d
;
1223 inbuf
[i
++] = packs
.e
;
1224 inbuf
[i
++] = packs
.f
;
1225 inbuf
[i
++] = packs
.g
;
1226 inbuf
[i
++] = packs
.h
;
1229 #endif /* defined (__x86_64__) */
1231 extern void *proc_name_address(void *p
);
1234 panic_display_process_name(void) {
1235 /* because of scoping issues len(p_comm) from proc_t is hard coded here */
1236 char proc_name
[17] = "Unknown";
1238 void *cbsd_info
= 0;
1240 if (ml_nofault_copy((vm_offset_t
)¤t_thread()->task
, (vm_offset_t
) &ctask
, sizeof(task_t
)) == sizeof(task_t
))
1241 if(ml_nofault_copy((vm_offset_t
)&ctask
->bsd_info
, (vm_offset_t
)&cbsd_info
, sizeof(cbsd_info
)) == sizeof(cbsd_info
))
1242 if (cbsd_info
&& (ml_nofault_copy((vm_offset_t
) proc_name_address(cbsd_info
), (vm_offset_t
) &proc_name
, sizeof(proc_name
)) > 0))
1243 proc_name
[sizeof(proc_name
) - 1] = '\0';
1244 paniclog_append_noflush("\nBSD process name corresponding to current thread: %s\n", proc_name
);
1248 panic_active(void) {
1249 return ((debugger_panic_str
!= (char *) 0));
1253 populate_model_name(char *model_string
) {
1254 strlcpy(model_name
, model_string
, sizeof(model_name
));
1258 panic_display_model_name(void) {
1259 char tmp_model_name
[sizeof(model_name
)];
1261 if (ml_nofault_copy((vm_offset_t
) &model_name
, (vm_offset_t
) &tmp_model_name
, sizeof(model_name
)) != sizeof(model_name
))
1264 tmp_model_name
[sizeof(tmp_model_name
) - 1] = '\0';
1266 if (tmp_model_name
[0] != 0)
1267 paniclog_append_noflush("System model name: %s\n", tmp_model_name
);
1271 panic_display_kernel_uuid(void) {
1272 char tmp_kernel_uuid
[sizeof(kernel_uuid_string
)];
1274 if (ml_nofault_copy((vm_offset_t
) &kernel_uuid_string
, (vm_offset_t
) &tmp_kernel_uuid
, sizeof(kernel_uuid_string
)) != sizeof(kernel_uuid_string
))
1277 if (tmp_kernel_uuid
[0] != '\0')
1278 paniclog_append_noflush("Kernel UUID: %s\n", tmp_kernel_uuid
);
1282 panic_display_kernel_aslr(void) {
1283 if (vm_kernel_slide
) {
1284 paniclog_append_noflush("Kernel slide: 0x%016lx\n", (unsigned long) vm_kernel_slide
);
1285 paniclog_append_noflush("Kernel text base: %p\n", (void *) vm_kernel_stext
);
1290 panic_display_hibb(void) {
1291 #if defined(__i386__) || defined (__x86_64__)
1292 paniclog_append_noflush("__HIB text base: %p\n", (void *) vm_hib_base
);
1297 panic_display_uptime(void) {
1299 absolutetime_to_nanoseconds(mach_absolute_time(), &uptime
);
1301 paniclog_append_noflush("\nSystem uptime in nanoseconds: %llu\n", uptime
);
1305 panic_display_disk_errors(void) {
1307 if (panic_disk_error_description
[0]) {
1308 panic_disk_error_description
[sizeof(panic_disk_error_description
) - 1] = '\0';
1309 paniclog_append_noflush("Root disk errors: \"%s\"\n", panic_disk_error_description
);
1313 extern const char version
[];
1314 extern char osversion
[];
1316 static volatile uint32_t config_displayed
= 0;
1318 __private_extern__
void
1319 panic_display_system_configuration(boolean_t launchd_exit
) {
1321 if (!launchd_exit
) panic_display_process_name();
1322 if (OSCompareAndSwap(0, 1, &config_displayed
)) {
1324 if (!launchd_exit
&& strlcpy(buf
, PE_boot_args(), sizeof(buf
)))
1325 paniclog_append_noflush("Boot args: %s\n", buf
);
1326 paniclog_append_noflush("\nMac OS version:\n%s\n",
1327 (osversion
[0] != 0) ? osversion
: "Not yet set");
1328 paniclog_append_noflush("\nKernel version:\n%s\n",version
);
1329 panic_display_kernel_uuid();
1330 if (!launchd_exit
) {
1331 panic_display_kernel_aslr();
1332 panic_display_hibb();
1333 panic_display_pal_info();
1335 panic_display_model_name();
1336 panic_display_disk_errors();
1337 if (!launchd_exit
) {
1338 panic_display_uptime();
1339 panic_display_zprint();
1341 panic_display_ztrace();
1342 #endif /* CONFIG_ZLEAKS */
1343 kext_dump_panic_lists(&paniclog_append_noflush
);
1348 extern unsigned int stack_total
;
1349 extern unsigned long long stack_allocs
;
1351 #if defined (__x86_64__)
1352 extern unsigned int inuse_ptepages_count
;
1353 extern long long alloc_ptepages_count
;
1356 extern boolean_t panic_include_zprint
;
1357 extern mach_memory_info_t
*panic_kext_memory_info
;
1358 extern vm_size_t panic_kext_memory_size
;
1360 __private_extern__
void
1361 panic_display_zprint()
1363 if(panic_include_zprint
== TRUE
) {
1366 struct zone zone_copy
;
1368 paniclog_append_noflush("%-20s %10s %10s\n", "Zone Name", "Cur Size", "Free Size");
1369 for (i
= 0; i
< num_zones
; i
++) {
1370 if(ml_nofault_copy((vm_offset_t
)(&zone_array
[i
]), (vm_offset_t
)&zone_copy
, sizeof(struct zone
)) == sizeof(struct zone
)) {
1371 if(zone_copy
.cur_size
> (1024*1024)) {
1372 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
));
1377 paniclog_append_noflush("%-20s %10lu\n", "Kernel Stacks", (uintptr_t)(kernel_stack_size
* stack_total
));
1379 #if defined (__x86_64__)
1380 paniclog_append_noflush("%-20s %10lu\n", "PageTables",(uintptr_t)(PAGE_SIZE
* inuse_ptepages_count
));
1383 paniclog_append_noflush("%-20s %10lu\n", "Kalloc.Large", (uintptr_t)kalloc_large_total
);
1384 if (panic_kext_memory_info
) {
1385 mach_memory_info_t
*mem_info
= panic_kext_memory_info
;
1386 paniclog_append_noflush("\n%-5s %10s\n", "Kmod", "Size");
1387 for (i
= 0; i
< (panic_kext_memory_size
/ sizeof(mach_zone_info_t
)); i
++) {
1388 if (((mem_info
[i
].flags
& VM_KERN_SITE_TYPE
) == VM_KERN_SITE_KMOD
) && (mem_info
[i
].size
> (1024 * 1024))) {
1389 paniclog_append_noflush("%-5lld %10lld\n", mem_info
[i
].site
, mem_info
[i
].size
);
1396 #if CONFIG_ECC_LOGGING
1397 __private_extern__
void
1398 panic_display_ecc_errors()
1400 uint32_t count
= ecc_log_get_correction_count();
1403 paniclog_append_noflush("ECC Corrections:%u\n", count
);
1406 #endif /* CONFIG_ECC_LOGGING */
1409 extern boolean_t panic_include_ztrace
;
1410 extern struct ztrace
* top_ztrace
;
1411 void panic_print_symbol_name(vm_address_t search
);
1414 * Prints the backtrace most suspected of being a leaker, if we paniced in the zone allocator.
1415 * top_ztrace and panic_include_ztrace comes from osfmk/kern/zalloc.c
1417 __private_extern__
void
1418 panic_display_ztrace(void)
1420 if(panic_include_ztrace
== TRUE
) {
1422 boolean_t keepsyms
= FALSE
;
1424 PE_parse_boot_argn("keepsyms", &keepsyms
, sizeof (keepsyms
));
1425 struct ztrace top_ztrace_copy
;
1427 /* Make sure not to trip another panic if there's something wrong with memory */
1428 if(ml_nofault_copy((vm_offset_t
)top_ztrace
, (vm_offset_t
)&top_ztrace_copy
, sizeof(struct ztrace
)) == sizeof(struct ztrace
)) {
1429 paniclog_append_noflush("\nBacktrace suspected of leaking: (outstanding bytes: %lu)\n", (uintptr_t)top_ztrace_copy
.zt_size
);
1430 /* Print the backtrace addresses */
1431 for (i
= 0; (i
< top_ztrace_copy
.zt_depth
&& i
< MAX_ZTRACE_DEPTH
) ; i
++) {
1432 paniclog_append_noflush("%p ", top_ztrace_copy
.zt_stack
[i
]);
1434 panic_print_symbol_name((vm_address_t
)top_ztrace_copy
.zt_stack
[i
]);
1436 paniclog_append_noflush("\n");
1438 /* Print any kexts in that backtrace, along with their link addresses so we can properly blame them */
1439 kmod_panic_dump((vm_offset_t
*)&top_ztrace_copy
.zt_stack
[0], top_ztrace_copy
.zt_depth
);
1442 paniclog_append_noflush("\nCan't access top_ztrace...\n");
1444 paniclog_append_noflush("\n");
1447 #endif /* CONFIG_ZLEAKS */
1449 #if !CONFIG_TELEMETRY
1451 telemetry_gather(user_addr_t buffer __unused
, uint32_t *length __unused
, boolean_t mark __unused
)
1453 return KERN_NOT_SUPPORTED
;
1457 #include <machine/machine_cpu.h>
1459 uint32_t kern_feature_overrides
= 0;
1461 boolean_t
kern_feature_override(uint32_t fmask
) {
1462 if (kern_feature_overrides
== 0) {
1463 uint32_t fdisables
= 0;
1464 /* Expected to be first invoked early, in a single-threaded
1467 if (PE_parse_boot_argn("validation_disables", &fdisables
, sizeof(fdisables
))) {
1468 fdisables
|= KF_INITIALIZED
;
1469 kern_feature_overrides
= fdisables
;
1471 kern_feature_overrides
|= KF_INITIALIZED
;
1474 return ((kern_feature_overrides
& fmask
) == fmask
);