2 * Copyright (c) 2000-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 * 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 <kern/cpu_number.h>
61 #include <kern/kalloc.h>
63 #include <kern/thread.h>
64 #include <kern/assert.h>
65 #include <kern/sched_prim.h>
66 #include <kern/misc_protos.h>
67 #include <kern/clock.h>
68 #include <kern/telemetry.h>
70 #include <kern/kern_cdata.h>
71 #include <vm/vm_kern.h>
74 #if !(MACH_KDP && CONFIG_KDP_INTERACTIVE_DEBUGGING)
75 #include <kdp/kdp_udp.h>
78 #if defined(__i386__) || defined(__x86_64__)
79 #include <i386/cpu_threads.h>
80 #include <i386/pmCPU.h>
83 #include <IOKit/IOPlatformExpert.h>
84 #include <machine/pal_routines.h>
86 #include <sys/kdebug.h>
87 #include <libkern/OSKextLibPrivate.h>
88 #include <libkern/OSAtomic.h>
89 #include <libkern/kernel_mach_header.h>
90 #include <uuid/uuid.h>
91 #include <mach_debug/zone_info.h>
93 #if (defined(__arm64__) || defined(NAND_PANIC_DEVICE)) && !defined(LEGACY_PANIC_LOGS)
94 #include <pexpert/pexpert.h> /* For gPanicBase */
97 unsigned int halt_in_debugger
= 0;
98 unsigned int switch_debugger
= 0;
99 unsigned int current_debugger
= 0;
100 unsigned int active_debugger
= 0;
101 unsigned int debug_mode
=0;
102 unsigned int disable_debug_output
= TRUE
;
103 unsigned int systemLogDiags
= FALSE
;
104 unsigned int panicDebugging
= FALSE
;
105 unsigned int logPanicDataToScreen
= FALSE
;
106 unsigned int kdebug_serial
= FALSE
;
110 const char *panicstr
= (char *) 0;
111 decl_simple_lock_data(,panic_lock
)
113 volatile int panicwait
;
114 volatile unsigned int nestedpanic
= 0;
115 unsigned int panic_is_inited
= 0;
116 unsigned int return_on_panic
= 0;
117 unsigned long panic_caller
;
119 #define DEBUG_BUF_SIZE (3 * PAGE_SIZE)
121 /* debug_buf is directly linked with iBoot panic region for ARM64 targets */
122 #if (defined(__arm64__) || defined(NAND_PANIC_DEVICE)) && !defined(LEGACY_PANIC_LOGS)
123 char *debug_buf_addr
= NULL
;
124 char *debug_buf_ptr
= NULL
;
125 unsigned int debug_buf_size
= 0;
127 char debug_buf
[DEBUG_BUF_SIZE
];
128 __used
char *debug_buf_addr
= debug_buf
;
129 char *debug_buf_ptr
= debug_buf
;
130 unsigned int debug_buf_size
= sizeof(debug_buf
);
133 static char model_name
[64];
134 unsigned char *kernel_uuid
;
135 /* uuid_string_t */ char kernel_uuid_string
[37];
137 static spl_t
panic_prologue(const char *str
);
138 static void panic_epilogue(spl_t s
);
149 } __attribute__((packed
));
151 typedef struct pasc pasc_t
;
153 /* Prevent CPP from breaking the definition below */
154 #if CONFIG_NO_PANIC_STRINGS
158 void __attribute__((noinline
))
162 const char *expression
165 int saved_return_on_panic
;
168 kprintf("%s:%d non-fatal Assertion: %s", file
, line
, expression
);
172 saved_return_on_panic
= return_on_panic
;
175 * If we don't have a debugger configured, returning from an
176 * assert is a bad, bad idea; there is no guarantee that we
177 * didn't simply assert before we were able to restart the
180 if (current_debugger
!= NO_CUR_DB
)
183 panic_plain("%s:%d Assertion failed: %s", file
, line
, expression
);
185 return_on_panic
= saved_return_on_panic
;
189 * Carefully use the panic_lock. There's always a chance that
190 * somehow we'll call panic before getting to initialize the
191 * panic_lock -- in this case, we'll assume that the world is
192 * in uniprocessor mode and just avoid using the panic lock.
194 #define PANIC_LOCK() \
196 if (panic_is_inited) \
197 simple_lock(&panic_lock); \
200 #define PANIC_UNLOCK() \
202 if (panic_is_inited) \
203 simple_unlock(&panic_lock); \
209 unsigned long uuidlen
= 0;
212 uuid
= getuuidfromheader(&_mh_execute_header
, &uuidlen
);
213 if ((uuid
!= NULL
) && (uuidlen
== sizeof(uuid_t
))) {
215 uuid_unparse_upper(*(uuid_t
*)uuid
, kernel_uuid_string
);
218 simple_lock_init(&panic_lock
, 0);
222 if (!PE_parse_boot_argn("assertions", &mach_assert
, sizeof(mach_assert
))) {
230 if (debug_buf_size
!= 0)
232 #if (defined(__arm64__) || defined(NAND_PANIC_DEVICE)) && !defined(LEGACY_PANIC_LOGS)
234 printf("debug_log_init: Error!! gPanicBase is still not initialized\n");
237 /* Shift debug buf start location and size by 8 bytes for magic header and crc value */
238 debug_buf_addr
= (char*)gPanicBase
+ 8;
239 debug_buf_ptr
= debug_buf_addr
;
240 debug_buf_size
= gPanicSize
- 8;
242 debug_buf_addr
= debug_buf
;
243 debug_buf_ptr
= debug_buf
;
244 debug_buf_size
= sizeof(debug_buf
);
248 #if defined(__i386__) || defined(__x86_64__)
249 #define panic_stop() pmCPUHalt(PM_HALT_PANIC)
250 #define panic_safe() pmSafeMode(x86_lcpu(), PM_SAFE_FL_SAFE)
251 #define panic_normal() pmSafeMode(x86_lcpu(), PM_SAFE_FL_NORMAL)
253 #define panic_stop() { while (1) ; }
255 #define panic_normal()
259 * Prevent CPP from breaking the definition below,
260 * since all clients get a #define to prepend line numbers
264 void _consume_panic_args(int a __unused
, ...)
269 extern unsigned int write_trace_on_panic
;
272 panic_prologue(const char *str
)
276 if (write_trace_on_panic
&& kdebug_enable
) {
277 if (get_preemption_level() == 0 && !ml_at_interrupt_context()) {
278 ml_set_interrupts_enabled(TRUE
);
279 kdbg_dump_trace_to_file("/var/tmp/panic.trace");
284 disable_preemption();
286 #if defined(__i386__) || defined(__x86_64__)
287 /* Attempt to display the unparsed panic string */
288 const char *tstr
= str
;
290 kprintf("Panic initiated, string: ");
291 while (tstr
&& *tstr
)
292 kprintf("%c", *tstr
++);
298 if( logPanicDataToScreen
)
299 disable_debug_output
= FALSE
;
307 if (cpu_number() != paniccpu
) {
310 * Wait until message has been printed to identify correct
311 * cpu that made the first panic.
319 Debugger("double panic");
320 // a printf statement here was removed to avoid a panic-loop caused
321 // by a panic from printf
327 paniccpu
= cpu_number();
336 panic_epilogue(spl_t s
)
339 * Release panicstr so that we can handle normally other panics.
342 panicstr
= (char *)0;
345 #if DEVELOPMENT || DEBUG
346 if (return_on_panic
) {
355 kdb_printf("panic: We are hanging here...\n");
361 panic(const char *str
, ...)
365 boolean_t old_doprnt_hide_pointers
= doprnt_hide_pointers
;
368 /* panic_caller is initialized to 0. If set, don't change it */
369 if ( ! panic_caller
)
370 panic_caller
= (unsigned long)(char *)__builtin_return_address(0);
372 s
= panic_prologue(str
);
374 /* Never hide pointers from panic logs. */
375 doprnt_hide_pointers
= FALSE
;
377 kdb_printf("panic(cpu %d caller 0x%lx): ", (unsigned) paniccpu
, panic_caller
);
379 va_start(listp
, str
);
380 _doprnt(str
, &listp
, consdebug_putc
, 0);
386 * Release panicwait indicator so that other cpus may call Debugger().
391 doprnt_hide_pointers
= old_doprnt_hide_pointers
;
397 panic_context(unsigned int reason
, void *ctx
, const char *str
, ...)
403 /* panic_caller is initialized to 0. If set, don't change it */
404 if ( ! panic_caller
)
405 panic_caller
= (unsigned long)(char *)__builtin_return_address(0);
407 s
= panic_prologue(str
);
408 kdb_printf("panic(cpu %d caller 0x%lx): ", (unsigned) paniccpu
, panic_caller
);
410 va_start(listp
, str
);
411 _doprnt(str
, &listp
, consdebug_putc
, 0);
417 * Release panicwait indicator so that other cpus may call Debugger().
420 DebuggerWithContext(reason
, ctx
, "panic");
425 log(__unused
int level
, char *fmt
, ...)
433 disable_preemption();
434 va_start(listp
, fmt
);
435 _doprnt(fmt
, &listp
, conslog_putc
, 0);
444 if ((debug_buf_size
!= 0) &&
445 ((debug_buf_ptr
-debug_buf_addr
) < (int)debug_buf_size
)) {
451 /* In-place packing routines -- inefficient, but they're called at most once.
452 * Assumes "buflen" is a multiple of 8.
455 int packA(char *inbuf
, uint32_t length
, uint32_t buflen
)
457 unsigned int i
, j
= 0;
460 length
= MIN(((length
+ 7) & ~7), buflen
);
462 for (i
= 0; i
< length
; i
+=8)
472 bcopy ((char *) &pack
, inbuf
+ j
, 7);
478 void unpackA(char *inbuf
, uint32_t length
)
482 length
= (length
* 8)/7;
485 packs
= *(pasc_t
*)&inbuf
[i
];
486 bcopy(&inbuf
[i
+7], &inbuf
[i
+8], MAX(0, (int) (length
- i
- 8)));
487 inbuf
[i
++] = packs
.a
;
488 inbuf
[i
++] = packs
.b
;
489 inbuf
[i
++] = packs
.c
;
490 inbuf
[i
++] = packs
.d
;
491 inbuf
[i
++] = packs
.e
;
492 inbuf
[i
++] = packs
.f
;
493 inbuf
[i
++] = packs
.g
;
494 inbuf
[i
++] = packs
.h
;
498 extern void *proc_name_address(void *p
);
501 panic_display_process_name(void) {
502 /* because of scoping issues len(p_comm) from proc_t is hard coded here */
503 char proc_name
[17] = "Unknown";
507 if (ml_nofault_copy((vm_offset_t
)¤t_thread()->task
, (vm_offset_t
) &ctask
, sizeof(task_t
)) == sizeof(task_t
))
508 if(ml_nofault_copy((vm_offset_t
)&ctask
->bsd_info
, (vm_offset_t
)&cbsd_info
, sizeof(cbsd_info
)) == sizeof(cbsd_info
))
509 if (cbsd_info
&& (ml_nofault_copy((vm_offset_t
) proc_name_address(cbsd_info
), (vm_offset_t
) &proc_name
, sizeof(proc_name
)) > 0))
510 proc_name
[sizeof(proc_name
) - 1] = '\0';
511 kdb_printf("\nBSD process name corresponding to current thread: %s\n", proc_name
);
514 unsigned panic_active(void) {
515 return ((panicstr
!= (char *) 0));
518 void populate_model_name(char *model_string
) {
519 strlcpy(model_name
, model_string
, sizeof(model_name
));
522 void panic_display_model_name(void) {
523 char tmp_model_name
[sizeof(model_name
)];
525 if (ml_nofault_copy((vm_offset_t
) &model_name
, (vm_offset_t
) &tmp_model_name
, sizeof(model_name
)) != sizeof(model_name
))
528 tmp_model_name
[sizeof(tmp_model_name
) - 1] = '\0';
530 if (tmp_model_name
[0] != 0)
531 kdb_printf("System model name: %s\n", tmp_model_name
);
534 void panic_display_kernel_uuid(void) {
535 char tmp_kernel_uuid
[sizeof(kernel_uuid_string
)];
537 if (ml_nofault_copy((vm_offset_t
) &kernel_uuid_string
, (vm_offset_t
) &tmp_kernel_uuid
, sizeof(kernel_uuid_string
)) != sizeof(kernel_uuid_string
))
540 if (tmp_kernel_uuid
[0] != '\0')
541 kdb_printf("Kernel UUID: %s\n", tmp_kernel_uuid
);
544 void panic_display_kernel_aslr(void) {
545 if (vm_kernel_slide
) {
546 kdb_printf("Kernel slide: 0x%016lx\n", (unsigned long) vm_kernel_slide
);
547 kdb_printf("Kernel text base: %p\n", (void *) vm_kernel_stext
);
551 void panic_display_hibb(void) {
552 #if defined(__i386__) || defined (__x86_64__)
553 kdb_printf("__HIB text base: %p\n", (void *) vm_hib_base
);
557 static void panic_display_uptime(void) {
559 absolutetime_to_nanoseconds(mach_absolute_time(), &uptime
);
561 kdb_printf("\nSystem uptime in nanoseconds: %llu\n", uptime
);
564 extern const char version
[];
565 extern char osversion
[];
567 static volatile uint32_t config_displayed
= 0;
569 __private_extern__
void panic_display_system_configuration(void) {
571 panic_display_process_name();
572 if (OSCompareAndSwap(0, 1, &config_displayed
)) {
574 if (strlcpy(buf
, PE_boot_args(), sizeof(buf
)))
575 kdb_printf("Boot args: %s\n", buf
);
576 kdb_printf("\nMac OS version:\n%s\n",
577 (osversion
[0] != 0) ? osversion
: "Not yet set");
578 kdb_printf("\nKernel version:\n%s\n",version
);
579 panic_display_kernel_uuid();
580 panic_display_kernel_aslr();
581 panic_display_hibb();
582 panic_display_pal_info();
583 panic_display_model_name();
584 panic_display_uptime();
585 panic_display_zprint();
587 panic_display_ztrace();
588 #endif /* CONFIG_ZLEAKS */
589 kext_dump_panic_lists(&kdb_log
);
593 extern zone_t first_zone
;
594 extern unsigned int num_zones
, stack_total
;
595 extern unsigned long long stack_allocs
;
597 #if defined(__i386__) || defined (__x86_64__)
598 extern unsigned int inuse_ptepages_count
;
599 extern long long alloc_ptepages_count
;
602 extern boolean_t panic_include_zprint
;
603 extern vm_offset_t panic_kext_memory_info
;
604 extern vm_size_t panic_kext_memory_size
;
606 __private_extern__
void panic_display_zprint()
608 if(panic_include_zprint
== TRUE
) {
611 struct zone zone_copy
;
613 kdb_printf("%-20s %10s %10s\n", "Zone Name", "Cur Size", "Free Size");
614 if(first_zone
!=NULL
) {
615 if(ml_nofault_copy((vm_offset_t
)first_zone
, (vm_offset_t
)&zone_copy
, sizeof(struct zone
)) == sizeof(struct zone
)) {
616 for (i
= 0; i
< num_zones
; i
++) {
617 if(zone_copy
.cur_size
> (1024*1024)) {
618 kdb_printf("%-20s %10lu %10lu\n",zone_copy
.zone_name
, (uintptr_t)zone_copy
.cur_size
,(uintptr_t)(zone_copy
.countfree
* zone_copy
.elem_size
));
621 if(zone_copy
.next_zone
== NULL
) {
625 if(ml_nofault_copy((vm_offset_t
)zone_copy
.next_zone
, (vm_offset_t
)&zone_copy
, sizeof(struct zone
)) != sizeof(struct zone
)) {
632 kdb_printf("%-20s %10lu\n", "Kernel Stacks", (uintptr_t)(kernel_stack_size
* stack_total
));
634 #if defined(__i386__) || defined (__x86_64__)
635 kdb_printf("%-20s %10lu\n", "PageTables",(uintptr_t)(PAGE_SIZE
* inuse_ptepages_count
));
638 kdb_printf("%-20s %10lu\n", "Kalloc.Large", (uintptr_t)kalloc_large_total
);
639 if (panic_kext_memory_info
) {
640 mach_memory_info_t
*mem_info
= (mach_memory_info_t
*)panic_kext_memory_info
;
641 kdb_printf("\n%-5s %10s\n", "Kmod", "Size");
642 for (i
= 0; i
< VM_KERN_MEMORY_COUNT
+ VM_KERN_COUNTER_COUNT
; i
++) {
643 if (((mem_info
[i
].flags
& VM_KERN_SITE_TYPE
) == VM_KERN_SITE_KMOD
) && (mem_info
[i
].size
> (1024 * 1024))) {
644 kdb_printf("%-5lld %10lld\n", mem_info
[i
].site
, mem_info
[i
].size
);
651 #if CONFIG_ECC_LOGGING
652 __private_extern__
void panic_display_ecc_errors()
654 uint32_t count
= ecc_log_get_correction_count();
657 kdb_printf("ECC Corrections:%u\n", count
);
660 #endif /* CONFIG_ECC_LOGGING */
663 extern boolean_t panic_include_ztrace
;
664 extern struct ztrace
* top_ztrace
;
665 void panic_print_symbol_name(vm_address_t search
);
668 * Prints the backtrace most suspected of being a leaker, if we paniced in the zone allocator.
669 * top_ztrace and panic_include_ztrace comes from osfmk/kern/zalloc.c
671 __private_extern__
void panic_display_ztrace(void)
673 if(panic_include_ztrace
== TRUE
) {
675 boolean_t keepsyms
= FALSE
;
677 PE_parse_boot_argn("keepsyms", &keepsyms
, sizeof (keepsyms
));
678 struct ztrace top_ztrace_copy
;
680 /* Make sure not to trip another panic if there's something wrong with memory */
681 if(ml_nofault_copy((vm_offset_t
)top_ztrace
, (vm_offset_t
)&top_ztrace_copy
, sizeof(struct ztrace
)) == sizeof(struct ztrace
)) {
682 kdb_printf("\nBacktrace suspected of leaking: (outstanding bytes: %lu)\n", (uintptr_t)top_ztrace_copy
.zt_size
);
683 /* Print the backtrace addresses */
684 for (i
= 0; (i
< top_ztrace_copy
.zt_depth
&& i
< MAX_ZTRACE_DEPTH
) ; i
++) {
685 kdb_printf("%p ", top_ztrace_copy
.zt_stack
[i
]);
687 panic_print_symbol_name((vm_address_t
)top_ztrace_copy
.zt_stack
[i
]);
691 /* Print any kexts in that backtrace, along with their link addresses so we can properly blame them */
692 kmod_panic_dump((vm_offset_t
*)&top_ztrace_copy
.zt_stack
[0], top_ztrace_copy
.zt_depth
);
695 kdb_printf("\nCan't access top_ztrace...\n");
700 #endif /* CONFIG_ZLEAKS */
702 #if ! (MACH_KDP && CONFIG_KDP_INTERACTIVE_DEBUGGING)
703 static struct kdp_ether_addr kdp_current_mac_address
= {{0, 0, 0, 0, 0, 0}};
705 /* XXX ugly forward declares to stop warnings */
706 void *kdp_get_interface(void);
707 void kdp_set_ip_and_mac_addresses(struct kdp_in_addr
*, struct kdp_ether_addr
*);
708 void kdp_set_gateway_mac(void *);
709 void kdp_set_interface(void *);
710 void kdp_register_send_receive(void *, void *);
711 void kdp_unregister_send_receive(void *, void *);
712 void kdp_snapshot_preflight(int, void *, uint32_t, uint32_t, kcdata_descriptor_t
, boolean_t enable_faulting
);
713 int kdp_stack_snapshot_geterror(void);
714 uint32_t kdp_stack_snapshot_bytes_traced(void);
717 kdp_get_interface( void)
723 kdp_get_ip_address(void )
726 struct kdp_ether_addr
727 kdp_get_mac_addr(void)
729 return kdp_current_mac_address
;
733 kdp_set_ip_and_mac_addresses(
734 __unused
struct kdp_in_addr
*ipaddr
,
735 __unused
struct kdp_ether_addr
*macaddr
)
739 kdp_set_gateway_mac(__unused
void *gatewaymac
)
743 kdp_set_interface(__unused
void *ifp
)
747 kdp_register_send_receive(__unused
void *send
, __unused
void *receive
)
751 kdp_unregister_send_receive(__unused
void *send
, __unused
void *receive
)
754 void kdp_register_link(__unused kdp_link_t link
, __unused kdp_mode_t mode
)
757 void kdp_unregister_link(__unused kdp_link_t link
, __unused kdp_mode_t mode
)
762 #if !CONFIG_TELEMETRY
763 int telemetry_gather(user_addr_t buffer __unused
, uint32_t *length __unused
, boolean_t mark __unused
)
765 return KERN_NOT_SUPPORTED
;