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>
62 #include <kern/lock.h>
64 #include <kern/thread.h>
65 #include <kern/assert.h>
66 #include <kern/sched_prim.h>
67 #include <kern/misc_protos.h>
68 #include <kern/clock.h>
69 #include <kern/telemetry.h>
70 #include <vm/vm_kern.h>
74 #include <kdp/kdp_udp.h>
77 #if defined(__i386__) || defined(__x86_64__)
78 #include <i386/cpu_threads.h>
79 #include <i386/pmCPU.h>
82 #include <IOKit/IOPlatformExpert.h>
83 #include <machine/pal_routines.h>
85 #include <sys/kdebug.h>
86 #include <libkern/OSKextLibPrivate.h>
87 #include <libkern/OSAtomic.h>
88 #include <libkern/kernel_mach_header.h>
89 #include <uuid/uuid.h>
91 unsigned int halt_in_debugger
= 0;
92 unsigned int switch_debugger
= 0;
93 unsigned int current_debugger
= 0;
94 unsigned int active_debugger
= 0;
95 unsigned int debug_mode
=0;
96 unsigned int disable_debug_output
= TRUE
;
97 unsigned int systemLogDiags
= FALSE
;
98 unsigned int panicDebugging
= FALSE
;
99 unsigned int logPanicDataToScreen
= FALSE
;
103 const char *panicstr
= (char *) 0;
104 decl_simple_lock_data(,panic_lock
)
106 volatile int panicwait
;
107 volatile unsigned int nestedpanic
= 0;
108 unsigned int panic_is_inited
= 0;
109 unsigned int return_on_panic
= 0;
110 unsigned long panic_caller
;
112 #define DEBUG_BUF_SIZE (3 * PAGE_SIZE)
114 char debug_buf
[DEBUG_BUF_SIZE
];
115 char *debug_buf_ptr
= debug_buf
;
116 unsigned int debug_buf_size
= sizeof(debug_buf
);
118 static char model_name
[64];
119 unsigned char *kernel_uuid
;
120 /* uuid_string_t */ char kernel_uuid_string
[37];
122 static spl_t
panic_prologue(const char *str
);
123 static void panic_epilogue(spl_t s
);
134 } __attribute__((packed
));
136 typedef struct pasc pasc_t
;
138 /* Prevent CPP from breaking the definition below */
139 #if CONFIG_NO_PANIC_STRINGS
147 const char *expression
150 int saved_return_on_panic
;
156 saved_return_on_panic
= return_on_panic
;
159 * If we don't have a debugger configured, returning from an
160 * assert is a bad, bad idea; there is no guarantee that we
161 * didn't simply assert before we were able to restart the
164 if (current_debugger
!= NO_CUR_DB
)
167 panic_plain("%s:%d Assertion failed: %s", file
, line
, expression
);
169 return_on_panic
= saved_return_on_panic
;
173 * Carefully use the panic_lock. There's always a chance that
174 * somehow we'll call panic before getting to initialize the
175 * panic_lock -- in this case, we'll assume that the world is
176 * in uniprocessor mode and just avoid using the panic lock.
178 #define PANIC_LOCK() \
180 if (panic_is_inited) \
181 simple_lock(&panic_lock); \
184 #define PANIC_UNLOCK() \
186 if (panic_is_inited) \
187 simple_unlock(&panic_lock); \
193 unsigned long uuidlen
= 0;
196 uuid
= getuuidfromheader(&_mh_execute_header
, &uuidlen
);
197 if ((uuid
!= NULL
) && (uuidlen
== sizeof(uuid_t
))) {
199 uuid_unparse_upper(*(uuid_t
*)uuid
, kernel_uuid_string
);
202 simple_lock_init(&panic_lock
, 0);
210 if (debug_buf_size
!= 0)
212 debug_buf_ptr
= debug_buf
;
213 debug_buf_size
= sizeof(debug_buf
);
216 #if defined(__i386__) || defined(__x86_64__)
217 #define panic_stop() pmCPUHalt(PM_HALT_PANIC)
218 #define panic_safe() pmSafeMode(x86_lcpu(), PM_SAFE_FL_SAFE)
219 #define panic_normal() pmSafeMode(x86_lcpu(), PM_SAFE_FL_NORMAL)
221 #define panic_stop() { while (1) ; }
223 #define panic_normal()
227 * Prevent CPP from breaking the definition below,
228 * since all clients get a #define to prepend line numbers
232 void _consume_panic_args(int a __unused
, ...)
238 panic_prologue(const char *str
)
243 if (get_preemption_level() == 0 && !ml_at_interrupt_context()) {
244 ml_set_interrupts_enabled(TRUE
);
245 kdbg_dump_trace_to_file("/var/tmp/panic.trace");
250 disable_preemption();
252 #if defined(__i386__) || defined(__x86_64__)
253 /* Attempt to display the unparsed panic string */
254 const char *tstr
= str
;
256 kprintf("Panic initiated, string: ");
257 while (tstr
&& *tstr
)
258 kprintf("%c", *tstr
++);
264 if( logPanicDataToScreen
)
265 disable_debug_output
= FALSE
;
273 if (cpu_number() != paniccpu
) {
276 * Wait until message has been printed to identify correct
277 * cpu that made the first panic.
285 Debugger("double panic");
286 printf("double panic: We are hanging here...\n");
292 paniccpu
= cpu_number();
301 panic_epilogue(spl_t s
)
304 * Release panicstr so that we can handle normally other panics.
307 panicstr
= (char *)0;
310 if (return_on_panic
) {
316 kdb_printf("panic: We are hanging here...\n");
322 panic(const char *str
, ...)
328 /* panic_caller is initialized to 0. If set, don't change it */
329 if ( ! panic_caller
)
330 panic_caller
= (unsigned long)(char *)__builtin_return_address(0);
332 s
= panic_prologue(str
);
333 kdb_printf("panic(cpu %d caller 0x%lx): ", (unsigned) paniccpu
, panic_caller
);
335 va_start(listp
, str
);
336 _doprnt(str
, &listp
, consdebug_putc
, 0);
342 * Release panicwait indicator so that other cpus may call Debugger().
350 panic_context(unsigned int reason
, void *ctx
, const char *str
, ...)
356 /* panic_caller is initialized to 0. If set, don't change it */
357 if ( ! panic_caller
)
358 panic_caller
= (unsigned long)(char *)__builtin_return_address(0);
360 s
= panic_prologue(str
);
361 kdb_printf("panic(cpu %d caller 0x%lx): ", (unsigned) paniccpu
, panic_caller
);
363 va_start(listp
, str
);
364 _doprnt(str
, &listp
, consdebug_putc
, 0);
370 * Release panicwait indicator so that other cpus may call Debugger().
373 DebuggerWithContext(reason
, ctx
, "panic");
378 log(__unused
int level
, char *fmt
, ...)
386 disable_preemption();
387 va_start(listp
, fmt
);
388 _doprnt(fmt
, &listp
, conslog_putc
, 0);
397 if ((debug_buf_size
!= 0) &&
398 ((debug_buf_ptr
-debug_buf
) < (int)debug_buf_size
)) {
404 /* In-place packing routines -- inefficient, but they're called at most once.
405 * Assumes "buflen" is a multiple of 8.
408 int packA(char *inbuf
, uint32_t length
, uint32_t buflen
)
410 unsigned int i
, j
= 0;
413 length
= MIN(((length
+ 7) & ~7), buflen
);
415 for (i
= 0; i
< length
; i
+=8)
425 bcopy ((char *) &pack
, inbuf
+ j
, 7);
431 void unpackA(char *inbuf
, uint32_t length
)
435 length
= (length
* 8)/7;
438 packs
= *(pasc_t
*)&inbuf
[i
];
439 bcopy(&inbuf
[i
+7], &inbuf
[i
+8], MAX(0, (int) (length
- i
- 8)));
440 inbuf
[i
++] = packs
.a
;
441 inbuf
[i
++] = packs
.b
;
442 inbuf
[i
++] = packs
.c
;
443 inbuf
[i
++] = packs
.d
;
444 inbuf
[i
++] = packs
.e
;
445 inbuf
[i
++] = packs
.f
;
446 inbuf
[i
++] = packs
.g
;
447 inbuf
[i
++] = packs
.h
;
451 extern void *proc_name_address(void *p
);
454 panic_display_process_name(void) {
455 char proc_name
[32] = "Unknown";
459 if (ml_nofault_copy((vm_offset_t
)¤t_thread()->task
, (vm_offset_t
) &ctask
, sizeof(task_t
)) == sizeof(task_t
))
460 if(ml_nofault_copy((vm_offset_t
)&ctask
->bsd_info
, (vm_offset_t
)&cbsd_info
, sizeof(&ctask
->bsd_info
)) == sizeof(&ctask
->bsd_info
))
461 if (cbsd_info
&& (ml_nofault_copy((vm_offset_t
) proc_name_address(cbsd_info
), (vm_offset_t
) &proc_name
, sizeof(proc_name
)) > 0))
462 proc_name
[sizeof(proc_name
) - 1] = '\0';
463 kdb_printf("\nBSD process name corresponding to current thread: %s\n", proc_name
);
466 unsigned panic_active(void) {
467 return ((panicstr
!= (char *) 0));
470 void populate_model_name(char *model_string
) {
471 strlcpy(model_name
, model_string
, sizeof(model_name
));
474 static void panic_display_model_name(void) {
475 char tmp_model_name
[sizeof(model_name
)];
477 if (ml_nofault_copy((vm_offset_t
) &model_name
, (vm_offset_t
) &tmp_model_name
, sizeof(model_name
)) != sizeof(model_name
))
480 tmp_model_name
[sizeof(tmp_model_name
) - 1] = '\0';
482 if (tmp_model_name
[0] != 0)
483 kdb_printf("System model name: %s\n", tmp_model_name
);
486 static void panic_display_kernel_uuid(void) {
487 char tmp_kernel_uuid
[sizeof(kernel_uuid_string
)];
489 if (ml_nofault_copy((vm_offset_t
) &kernel_uuid_string
, (vm_offset_t
) &tmp_kernel_uuid
, sizeof(kernel_uuid_string
)) != sizeof(kernel_uuid_string
))
492 if (tmp_kernel_uuid
[0] != '\0')
493 kdb_printf("Kernel UUID: %s\n", tmp_kernel_uuid
);
496 void panic_display_kernel_aslr(void) {
497 if (vm_kernel_slide
) {
498 kdb_printf("Kernel slide: 0x%016lx\n", (unsigned long) vm_kernel_slide
);
499 kdb_printf("Kernel text base: %p\n", (void *) vm_kernel_stext
);
503 static void panic_display_uptime(void) {
505 absolutetime_to_nanoseconds(mach_absolute_time(), &uptime
);
507 kdb_printf("\nSystem uptime in nanoseconds: %llu\n", uptime
);
510 extern const char version
[];
511 extern char osversion
[];
513 static volatile uint32_t config_displayed
= 0;
515 __private_extern__
void panic_display_system_configuration(void) {
517 panic_display_process_name();
518 if (OSCompareAndSwap(0, 1, &config_displayed
)) {
520 if (strlcpy(buf
, PE_boot_args(), sizeof(buf
)))
521 kdb_printf("Boot args: %s\n", buf
);
522 kdb_printf("\nMac OS version:\n%s\n",
523 (osversion
[0] != 0) ? osversion
: "Not yet set");
524 kdb_printf("\nKernel version:\n%s\n",version
);
525 panic_display_kernel_uuid();
526 panic_display_kernel_aslr();
527 panic_display_pal_info();
528 panic_display_model_name();
529 panic_display_uptime();
530 panic_display_zprint();
532 panic_display_ztrace();
533 #endif /* CONFIG_ZLEAKS */
534 kext_dump_panic_lists(&kdb_log
);
538 extern zone_t first_zone
;
539 extern unsigned int num_zones
, stack_total
;
540 extern unsigned long long stack_allocs
;
542 #if defined(__i386__) || defined (__x86_64__)
543 extern unsigned int inuse_ptepages_count
;
544 extern long long alloc_ptepages_count
;
547 extern boolean_t panic_include_zprint
;
549 __private_extern__
void panic_display_zprint()
551 if(panic_include_zprint
== TRUE
) {
554 struct zone zone_copy
;
556 if(first_zone
!=NULL
) {
557 if(ml_nofault_copy((vm_offset_t
)first_zone
, (vm_offset_t
)&zone_copy
, sizeof(struct zone
)) == sizeof(struct zone
)) {
558 for (i
= 0; i
< num_zones
; i
++) {
559 if(zone_copy
.cur_size
> (1024*1024)) {
560 kdb_printf("%.20s:%lu\n",zone_copy
.zone_name
,(uintptr_t)zone_copy
.cur_size
);
563 if(zone_copy
.next_zone
== NULL
) {
567 if(ml_nofault_copy((vm_offset_t
)zone_copy
.next_zone
, (vm_offset_t
)&zone_copy
, sizeof(struct zone
)) != sizeof(struct zone
)) {
574 kdb_printf("Kernel Stacks:%lu\n",(uintptr_t)(kernel_stack_size
* stack_total
));
576 #if defined(__i386__) || defined (__x86_64__)
577 kdb_printf("PageTables:%lu\n",(uintptr_t)(PAGE_SIZE
* inuse_ptepages_count
));
580 kdb_printf("Kalloc.Large:%lu\n",(uintptr_t)kalloc_large_total
);
585 extern boolean_t panic_include_ztrace
;
586 extern struct ztrace
* top_ztrace
;
588 * Prints the backtrace most suspected of being a leaker, if we paniced in the zone allocator.
589 * top_ztrace and panic_include_ztrace comes from osfmk/kern/zalloc.c
591 __private_extern__
void panic_display_ztrace(void)
593 if(panic_include_ztrace
== TRUE
) {
595 struct ztrace top_ztrace_copy
;
597 /* Make sure not to trip another panic if there's something wrong with memory */
598 if(ml_nofault_copy((vm_offset_t
)top_ztrace
, (vm_offset_t
)&top_ztrace_copy
, sizeof(struct ztrace
)) == sizeof(struct ztrace
)) {
599 kdb_printf("\nBacktrace suspected of leaking: (outstanding bytes: %lu)\n", (uintptr_t)top_ztrace_copy
.zt_size
);
600 /* Print the backtrace addresses */
601 for (i
= 0; (i
< top_ztrace_copy
.zt_depth
&& i
< MAX_ZTRACE_DEPTH
) ; i
++) {
602 kdb_printf("%p\n", top_ztrace_copy
.zt_stack
[i
]);
604 /* Print any kexts in that backtrace, along with their link addresses so we can properly blame them */
605 kmod_panic_dump((vm_offset_t
*)&top_ztrace_copy
.zt_stack
[0], top_ztrace_copy
.zt_depth
);
608 kdb_printf("\nCan't access top_ztrace...\n");
613 #endif /* CONFIG_ZLEAKS */
616 static struct kdp_ether_addr kdp_current_mac_address
= {{0, 0, 0, 0, 0, 0}};
618 /* XXX ugly forward declares to stop warnings */
619 void *kdp_get_interface(void);
620 void kdp_set_ip_and_mac_addresses(struct kdp_in_addr
*, struct kdp_ether_addr
*);
621 void kdp_set_gateway_mac(void *);
622 void kdp_set_interface(void *);
623 void kdp_register_send_receive(void *, void *);
624 void kdp_unregister_send_receive(void *, void *);
625 void kdp_snapshot_preflight(int, void *, uint32_t, uint32_t);
626 int kdp_stack_snapshot_geterror(void);
627 int kdp_stack_snapshot_bytes_traced(void);
630 kdp_get_interface( void)
636 kdp_get_ip_address(void )
639 struct kdp_ether_addr
640 kdp_get_mac_addr(void)
642 return kdp_current_mac_address
;
646 kdp_set_ip_and_mac_addresses(
647 __unused
struct kdp_in_addr
*ipaddr
,
648 __unused
struct kdp_ether_addr
*macaddr
)
652 kdp_set_gateway_mac(__unused
void *gatewaymac
)
656 kdp_set_interface(__unused
void *ifp
)
660 kdp_register_send_receive(__unused
void *send
, __unused
void *receive
)
664 kdp_unregister_send_receive(__unused
void *send
, __unused
void *receive
)
668 kdp_snapshot_preflight(__unused
int pid
, __unused
void * tracebuf
,
669 __unused
uint32_t tracebuf_size
, __unused
uint32_t options
)
673 kdp_stack_snapshot_geterror(void)
679 kdp_stack_snapshot_bytes_traced(void)
686 #if !CONFIG_TELEMETRY
687 int telemetry_gather(user_addr_t buffer __unused
, uint32_t *length __unused
, boolean_t mark __unused
)
689 return KERN_NOT_SUPPORTED
;