]> git.saurik.com Git - apple/xnu.git/blobdiff - osfmk/kern/debug.c
xnu-2782.30.5.tar.gz
[apple/xnu.git] / osfmk / kern / debug.c
index 39aa1f425972716180df81c687d0f18a59145874..6f527a66fdb67e6c44fdbb5ff665e28b3d651177 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
+ * Copyright (c) 2000-2013 Apple Inc. All rights reserved.
  *
  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
  * 
  */
 
 #include <mach_assert.h>
-#include <mach_kdb.h>
-#include <mach_kgdb.h>
 #include <mach_kdp.h>
 
 #include <kern/cpu_number.h>
 #include <kern/kalloc.h>
-#include <kern/lock.h>
 #include <kern/spl.h>
 #include <kern/thread.h>
 #include <kern/assert.h>
 #include <kern/sched_prim.h>
 #include <kern/misc_protos.h>
 #include <kern/clock.h>
+#include <kern/telemetry.h>
+#include <kern/ecc.h>
 #include <vm/vm_kern.h>
 #include <vm/pmap.h>
 #include <stdarg.h>
-#if !MACH_KDP
+#if !(MACH_KDP && CONFIG_KDP_INTERACTIVE_DEBUGGING)
 #include <kdp/kdp_udp.h>
 #endif
 
-#ifdef __ppc__
-#include <ppc/Firmware.h>
-#include <ppc/low_trace.h>
-#endif
-
 #if defined(__i386__) || defined(__x86_64__)
 #include <i386/cpu_threads.h>
 #include <i386/pmCPU.h>
 #endif
 
 #include <IOKit/IOPlatformExpert.h>
+#include <machine/pal_routines.h>
 
 #include <sys/kdebug.h>
 #include <libkern/OSKextLibPrivate.h>
+#include <libkern/OSAtomic.h>
+#include <libkern/kernel_mach_header.h>
+#include <uuid/uuid.h>
+
+#if (defined(__arm64__) || defined(NAND_PANIC_DEVICE)) && !defined(LEGACY_PANIC_LOGS)
+#include <pexpert/pexpert.h> /* For gPanicBase */
+#endif
 
 unsigned int   halt_in_debugger = 0;
 unsigned int   switch_debugger = 0;
@@ -99,6 +101,7 @@ unsigned int         disable_debug_output = TRUE;
 unsigned int   systemLogDiags = FALSE;
 unsigned int   panicDebugging = FALSE;
 unsigned int   logPanicDataToScreen = FALSE;
+unsigned int   kdebug_serial = FALSE;
 
 int mach_assert = 1;
 
@@ -111,17 +114,26 @@ unsigned int              panic_is_inited = 0;
 unsigned int           return_on_panic = 0;
 unsigned long          panic_caller;
 
-#if CONFIG_EMBEDDED
-#define DEBUG_BUF_SIZE (PAGE_SIZE)
-#else
 #define DEBUG_BUF_SIZE (3 * PAGE_SIZE)
-#endif
 
+/* debug_buf is directly linked with iBoot panic region for ARM64 targets */
+#if (defined(__arm64__) || defined(NAND_PANIC_DEVICE)) && !defined(LEGACY_PANIC_LOGS)
+char *debug_buf_addr = NULL;
+char *debug_buf_ptr = NULL;
+unsigned int debug_buf_size = 0;
+#else
 char debug_buf[DEBUG_BUF_SIZE];
+__used char *debug_buf_addr = debug_buf;
 char *debug_buf_ptr = debug_buf;
 unsigned int debug_buf_size = sizeof(debug_buf);
+#endif
 
 static char model_name[64];
+unsigned char *kernel_uuid;
+/* uuid_string_t */ char kernel_uuid_string[37];
+
+static spl_t panic_prologue(const char *str);
+static void panic_epilogue(spl_t s);
 
 struct pasc {
   unsigned a: 7;
@@ -141,7 +153,7 @@ typedef struct pasc pasc_t;
 #undef Assert
 #endif
 
-void
+void __attribute__((noinline))
 Assert(
        const char      *file,
        int             line,
@@ -155,7 +167,15 @@ Assert(
        }
 
        saved_return_on_panic = return_on_panic;
-       return_on_panic = 1;
+
+       /*
+        * If we don't have a debugger configured, returning from an
+        * assert is a bad, bad idea; there is no guarantee that we
+        * didn't simply assert before we were able to restart the
+        * platform.
+        */
+       if (current_debugger != NO_CUR_DB)
+               return_on_panic = 1;
 
        panic_plain("%s:%d Assertion failed: %s", file, line, expression);
 
@@ -180,10 +200,18 @@ MACRO_BEGIN                                                               \
                simple_unlock(&panic_lock);                             \
 MACRO_END
 
-
 void
 panic_init(void)
 {
+       unsigned long uuidlen = 0;
+       void *uuid;
+
+       uuid = getuuidfromheader(&_mh_execute_header, &uuidlen);
+       if ((uuid != NULL) && (uuidlen == sizeof(uuid_t))) {
+               kernel_uuid = uuid;
+               uuid_unparse_upper(*(uuid_t *)uuid, kernel_uuid_string);
+       }
+
        simple_lock_init(&panic_lock, 0);
        panic_is_inited = 1;
        panic_caller = 0;
@@ -194,8 +222,20 @@ debug_log_init(void)
 {
        if (debug_buf_size != 0)
                return;
+#if (defined(__arm64__) || defined(NAND_PANIC_DEVICE)) && !defined(LEGACY_PANIC_LOGS)
+       if (!gPanicBase) {
+               printf("debug_log_init: Error!! gPanicBase is still not initialized\n");
+               return;
+       }
+       /* Shift debug buf start location and size by 8 bytes for magic header and crc value */
+       debug_buf_addr = (char*)gPanicBase + 8;
+       debug_buf_ptr = debug_buf_addr;
+       debug_buf_size = gPanicSize - 8;
+#else
+       debug_buf_addr = debug_buf;
        debug_buf_ptr = debug_buf;
        debug_buf_size = sizeof(debug_buf);
+#endif
 }
 
 #if defined(__i386__) || defined(__x86_64__)
@@ -216,45 +256,46 @@ debug_log_init(void)
 
 void _consume_panic_args(int a __unused, ...)
 {
-    panic(NULL);
+    panic("panic");
 }
 
-void
-panic(const char *str, ...)
+extern unsigned int write_trace_on_panic;
+
+static spl_t
+panic_prologue(const char *str)
 {
-       va_list listp;
        spl_t   s;
-       thread_t thread;
-       wait_queue_t wq;
 
-
-       if (kdebug_enable)
-               kdbg_dump_trace_to_file("/var/tmp/panic.trace");
+       if (write_trace_on_panic && kdebug_enable) {
+               if (get_preemption_level() == 0 && !ml_at_interrupt_context()) {
+                       ml_set_interrupts_enabled(TRUE);
+                       kdbg_dump_trace_to_file("/var/tmp/panic.trace");
+               }
+       }
 
        s = splhigh();
        disable_preemption();
 
-       panic_safe();
+#if    defined(__i386__) || defined(__x86_64__)
+       /* Attempt to display the unparsed panic string */
+       const char *tstr = str;
 
-#ifdef __ppc__
-       lastTrace = LLTraceSet(0);              /* Disable low-level tracing */
+       kprintf("Panic initiated, string: ");
+       while (tstr && *tstr)
+               kprintf("%c", *tstr++);
+       kprintf("\n");
 #endif
 
-       thread = current_thread();              /* Get failing thread */
-       wq = thread->wait_queue;                /* Save the old value */
-       thread->wait_queue = NULL;              /* Clear the wait so we do not get double panics when we try locks */
+       panic_safe();
 
        if( logPanicDataToScreen )
                disable_debug_output = FALSE;
                
        debug_mode = TRUE;
 
-       /* panic_caller is initialized to 0.  If set, don't change it */
-       if ( ! panic_caller )
-               panic_caller = (unsigned long)(char *)__builtin_return_address(0);
-       
 restart:
        PANIC_LOCK();
+
        if (panicstr) {
                if (cpu_number() != paniccpu) {
                        PANIC_UNLOCK();
@@ -279,26 +320,19 @@ restart:
        panicwait = 1;
 
        PANIC_UNLOCK();
-       kdb_printf("panic(cpu %d caller 0x%lx): ", (unsigned) paniccpu, panic_caller);
-       if (str) {
-               va_start(listp, str);
-               _doprnt(str, &listp, consdebug_putc, 0);
-               va_end(listp);
-       }
-       kdb_printf("\n");
+       return(s);
+}
 
-       /*
-        * Release panicwait indicator so that other cpus may call Debugger().
-        */
-       panicwait = 0;
-       Debugger("panic");
+
+static void
+panic_epilogue(spl_t   s)
+{
        /*
         * Release panicstr so that we can handle normally other panics.
         */
        PANIC_LOCK();
        panicstr = (char *)0;
        PANIC_UNLOCK();
-       thread->wait_queue = wq;        /* Restore the wait queue */
 
        if (return_on_panic) {
                panic_normal();
@@ -306,12 +340,67 @@ restart:
                splx(s);
                return;
        }
-
        kdb_printf("panic: We are hanging here...\n");
        panic_stop();
        /* NOTREACHED */
 }
 
+void
+panic(const char *str, ...)
+{
+       va_list listp;
+       spl_t   s;
+
+
+       /* panic_caller is initialized to 0.  If set, don't change it */
+       if ( ! panic_caller )
+               panic_caller = (unsigned long)(char *)__builtin_return_address(0);
+       
+       s = panic_prologue(str);
+       kdb_printf("panic(cpu %d caller 0x%lx): ", (unsigned) paniccpu, panic_caller);
+       if (str) {
+               va_start(listp, str);
+               _doprnt(str, &listp, consdebug_putc, 0);
+               va_end(listp);
+       }
+       kdb_printf("\n");
+
+       /*
+        * Release panicwait indicator so that other cpus may call Debugger().
+        */
+       panicwait = 0;
+       Debugger("panic");
+       panic_epilogue(s);
+}
+
+void
+panic_context(unsigned int reason, void *ctx, const char *str, ...)
+{
+       va_list listp;
+       spl_t   s;
+
+
+       /* panic_caller is initialized to 0.  If set, don't change it */
+       if ( ! panic_caller )
+               panic_caller = (unsigned long)(char *)__builtin_return_address(0);
+       
+       s = panic_prologue(str);
+       kdb_printf("panic(cpu %d caller 0x%lx): ", (unsigned) paniccpu, panic_caller);
+       if (str) {
+               va_start(listp, str);
+               _doprnt(str, &listp, consdebug_putc, 0);
+               va_end(listp);
+       }
+       kdb_printf("\n");
+
+       /*
+        * Release panicwait indicator so that other cpus may call Debugger().
+        */
+       panicwait = 0;
+       DebuggerWithContext(reason, ctx, "panic");
+       panic_epilogue(s);
+}
+
 void
 log(__unused int level, char *fmt, ...)
 {
@@ -333,13 +422,14 @@ void
 debug_putc(char c)
 {
        if ((debug_buf_size != 0) &&
-               ((debug_buf_ptr-debug_buf) < (int)debug_buf_size)) {
+               ((debug_buf_ptr-debug_buf_addr) < (int)debug_buf_size)) {
                *debug_buf_ptr=c;
                debug_buf_ptr++;
        }
 }
 
 /* In-place packing routines -- inefficient, but they're called at most once.
+ * Assumes "buflen" is a multiple of 8.
  */
 
 int packA(char *inbuf, uint32_t length, uint32_t buflen)
@@ -347,7 +437,7 @@ int packA(char *inbuf, uint32_t length, uint32_t buflen)
   unsigned int i, j = 0;
   pasc_t pack;
   
-  length = MIN(((length & ~7) +8), buflen);
+  length = MIN(((length + 7) & ~7), buflen);
 
   for (i = 0; i < length; i+=8)
     {
@@ -362,7 +452,7 @@ int packA(char *inbuf, uint32_t length, uint32_t buflen)
       bcopy ((char *) &pack, inbuf + j, 7);
       j += 7;
     }
-  return ((length * 7)/8);
+  return j;
 }
 
 void unpackA(char *inbuf, uint32_t length)
@@ -408,16 +498,39 @@ void populate_model_name(char *model_string) {
        strlcpy(model_name, model_string, sizeof(model_name));
 }
 
-static void panic_display_model_name(void) {
+void panic_display_model_name(void) {
        char tmp_model_name[sizeof(model_name)];
 
        if (ml_nofault_copy((vm_offset_t) &model_name, (vm_offset_t) &tmp_model_name, sizeof(model_name)) != sizeof(model_name))
                return;
 
-       model_name[sizeof(model_name) - 1] = '\0';
+       tmp_model_name[sizeof(tmp_model_name) - 1] = '\0';
+
+       if (tmp_model_name[0] != 0)
+               kdb_printf("System model name: %s\n", tmp_model_name);
+}
+
+void panic_display_kernel_uuid(void) {
+       char tmp_kernel_uuid[sizeof(kernel_uuid_string)];
+
+       if (ml_nofault_copy((vm_offset_t) &kernel_uuid_string, (vm_offset_t) &tmp_kernel_uuid, sizeof(kernel_uuid_string)) != sizeof(kernel_uuid_string))
+               return;
+
+       if (tmp_kernel_uuid[0] != '\0')
+               kdb_printf("Kernel UUID: %s\n", tmp_kernel_uuid);
+}
 
-       if (model_name[0] != 0)
-               kdb_printf("System model name: %s\n", model_name);
+void panic_display_kernel_aslr(void) {
+       if (vm_kernel_slide) {
+               kdb_printf("Kernel slide:     0x%016lx\n", (unsigned long) vm_kernel_slide);
+               kdb_printf("Kernel text base: %p\n", (void *) vm_kernel_stext);
+       }
+}
+
+void panic_display_hibb(void) {
+#if defined(__i386__) || defined (__x86_64__)
+       kdb_printf("__HIB  text base: %p\n", (void *) vm_hib_base);
+#endif
 }
 
 static void panic_display_uptime(void) {
@@ -430,30 +543,39 @@ static void panic_display_uptime(void) {
 extern const char version[];
 extern char osversion[];
 
+static volatile uint32_t config_displayed = 0;
+
 __private_extern__ void panic_display_system_configuration(void) {
-       static volatile boolean_t config_displayed = FALSE;
 
        panic_display_process_name();
-       if (config_displayed == FALSE) {
-               config_displayed = TRUE;
+       if (OSCompareAndSwap(0, 1, &config_displayed)) {
+               char buf[256];
+               if (strlcpy(buf, PE_boot_args(), sizeof(buf)))
+                       kdb_printf("Boot args: %s\n", buf);
                kdb_printf("\nMac OS version:\n%s\n",
                    (osversion[0] != 0) ? osversion : "Not yet set");
                kdb_printf("\nKernel version:\n%s\n",version);
+               panic_display_kernel_uuid();
+               panic_display_kernel_aslr();
+               panic_display_hibb();
+               panic_display_pal_info();
                panic_display_model_name();
                panic_display_uptime();
-#if    defined(__i386__) || defined(__x86_64__)
-               pmap_pagetable_corruption_msg_log(&kdb_printf);
-#endif /* i386 || x86_64 */
                panic_display_zprint();
+#if CONFIG_ZLEAKS
+               panic_display_ztrace();
+#endif /* CONFIG_ZLEAKS */
                kext_dump_panic_lists(&kdb_log);
        }
 }
 
 extern zone_t          first_zone;
 extern unsigned int    num_zones, stack_total;
+extern unsigned long long stack_allocs;
 
 #if defined(__i386__) || defined (__x86_64__)
 extern unsigned int    inuse_ptepages_count;
+extern long long alloc_ptepages_count;
 #endif
 
 extern boolean_t       panic_include_zprint;
@@ -493,12 +615,63 @@ __private_extern__ void panic_display_zprint()
        }
 }
 
-#if !MACH_KDP
-static struct ether_addr kdp_current_mac_address = {{0, 0, 0, 0, 0, 0}};
+#if CONFIG_ECC_LOGGING
+__private_extern__ void panic_display_ecc_errors() 
+{
+       uint32_t count = ecc_log_get_correction_count();
+
+       if (count > 0) {
+               kdb_printf("ECC Corrections:%u\n", count);
+       }
+}
+#endif /* CONFIG_ECC_LOGGING */
+
+#if CONFIG_ZLEAKS
+extern boolean_t       panic_include_ztrace;
+extern struct ztrace* top_ztrace;
+void panic_print_symbol_name(vm_address_t search);
+
+/*
+ * Prints the backtrace most suspected of being a leaker, if we paniced in the zone allocator.
+ * top_ztrace and panic_include_ztrace comes from osfmk/kern/zalloc.c
+ */
+__private_extern__ void panic_display_ztrace(void)
+{
+       if(panic_include_ztrace == TRUE) {
+               unsigned int i = 0;
+               boolean_t keepsyms = FALSE;
+
+               PE_parse_boot_argn("keepsyms", &keepsyms, sizeof (keepsyms));
+               struct ztrace top_ztrace_copy;
+               
+               /* Make sure not to trip another panic if there's something wrong with memory */
+               if(ml_nofault_copy((vm_offset_t)top_ztrace, (vm_offset_t)&top_ztrace_copy, sizeof(struct ztrace)) == sizeof(struct ztrace)) {
+                       kdb_printf("\nBacktrace suspected of leaking: (outstanding bytes: %lu)\n", (uintptr_t)top_ztrace_copy.zt_size);
+                       /* Print the backtrace addresses */
+                       for (i = 0; (i < top_ztrace_copy.zt_depth && i < MAX_ZTRACE_DEPTH) ; i++) {
+                               kdb_printf("%p ", top_ztrace_copy.zt_stack[i]);
+                               if (keepsyms) {
+                                       panic_print_symbol_name((vm_address_t)top_ztrace_copy.zt_stack[i]);
+                               }
+                               kdb_printf("\n");
+                       }
+                       /* Print any kexts in that backtrace, along with their link addresses so we can properly blame them */
+                       kmod_panic_dump((vm_offset_t *)&top_ztrace_copy.zt_stack[0], top_ztrace_copy.zt_depth);
+               }
+               else {
+                       kdb_printf("\nCan't access top_ztrace...\n");
+               }
+               kdb_printf("\n");
+       }
+}
+#endif /* CONFIG_ZLEAKS */
+
+#if ! (MACH_KDP && CONFIG_KDP_INTERACTIVE_DEBUGGING)
+static struct kdp_ether_addr kdp_current_mac_address = {{0, 0, 0, 0, 0, 0}};
 
 /* XXX ugly forward declares to stop warnings */
 void *kdp_get_interface(void);
-void kdp_set_ip_and_mac_addresses(struct in_addr *, struct ether_addr *);
+void kdp_set_ip_and_mac_addresses(struct kdp_in_addr *, struct kdp_ether_addr *);
 void kdp_set_gateway_mac(void *);
 void kdp_set_interface(void *);
 void kdp_register_send_receive(void *, void *);
@@ -517,7 +690,7 @@ unsigned int
 kdp_get_ip_address(void )
 { return 0; }
 
-struct ether_addr
+struct kdp_ether_addr
 kdp_get_mac_addr(void)
 {       
         return kdp_current_mac_address;
@@ -525,8 +698,8 @@ kdp_get_mac_addr(void)
 
 void
 kdp_set_ip_and_mac_addresses(   
-        __unused struct in_addr          *ipaddr,
-        __unused struct ether_addr       *macaddr)
+        __unused struct kdp_in_addr          *ipaddr,
+        __unused struct kdp_ether_addr       *macaddr)
 {}
 
 void
@@ -545,21 +718,17 @@ void
 kdp_unregister_send_receive(__unused void *send, __unused void *receive)
 {}
 
-void
-kdp_snapshot_preflight(__unused int pid, __unused void * tracebuf,
-               __unused uint32_t tracebuf_size, __unused uint32_t options)
+void kdp_register_link(__unused kdp_link_t link, __unused kdp_mode_t mode)
 {}
 
-int
-kdp_stack_snapshot_geterror(void)
-{       
-        return -1;
-}
+void kdp_unregister_link(__unused kdp_link_t link, __unused kdp_mode_t mode)
+{}
 
-int
-kdp_stack_snapshot_bytes_traced(void)
-{       
-        return 0;
-}
+#endif
 
+#if !CONFIG_TELEMETRY
+int telemetry_gather(user_addr_t buffer __unused, uint32_t *length __unused, boolean_t mark __unused)
+{
+       return KERN_NOT_SUPPORTED;
+}
 #endif