]> git.saurik.com Git - apple/xnu.git/blobdiff - iokit/Kernel/IOLib.cpp
xnu-3789.51.2.tar.gz
[apple/xnu.git] / iokit / Kernel / IOLib.cpp
index 44a436346612d99a6441fcab26d86e26379af1fe..73a0c67a80191d0f859bc8fcb58f3677ca857b83 100644 (file)
@@ -58,6 +58,7 @@
 #include "libkern/OSAtomic.h"
 #include <libkern/c++/OSKext.h>
 #include <IOKit/IOStatisticsPrivate.h>
+#include <os/log_private.h>
 #include <sys/msgbuf.h>
 
 #if IOKITSTATS
@@ -172,9 +173,17 @@ void IOLibInit(void)
 
 #if IOTRACKING
     IOTrackingInit();
-    gIOMallocTracking = IOTrackingQueueAlloc(kIOMallocTrackingName, 0, 0,         true);
-    gIOWireTracking   = IOTrackingQueueAlloc(kIOWireTrackingName,   0, page_size, false);
-    gIOMapTracking    = IOTrackingQueueAlloc(kIOMapTrackingName,    0, page_size, false);
+    gIOMallocTracking = IOTrackingQueueAlloc(kIOMallocTrackingName, 0, 0, 0,
+                                                kIOTrackingQueueTypeAlloc,
+                                                37);
+    gIOWireTracking   = IOTrackingQueueAlloc(kIOWireTrackingName,   0, 0, page_size, 0, 0);
+
+    size_t mapCaptureSize = (kIOTracking & gIOKitDebug) ? page_size : (1024*1024);
+    gIOMapTracking    = IOTrackingQueueAlloc(kIOMapTrackingName,    0, 0, mapCaptureSize,
+                                                kIOTrackingQueueTypeDefaultOn
+                                                | kIOTrackingQueueTypeMap
+                                                | kIOTrackingQueueTypeUser,
+                                            0);
 #endif
 
     gIOKitPageableSpace.maps[0].address = 0;
@@ -286,15 +295,25 @@ void * IOMalloc(vm_size_t size)
     return address;
 }
 
-void IOFree(void * address, vm_size_t size)
+void IOFree(void * inAddress, vm_size_t size)
 {
-    if (address) {
+    void * address;
 
+    if ((address = inAddress))
+    {
        address = (typeof(address)) (((uintptr_t) address) - sizeofIOLibMallocHeader);
        
 #if IOTRACKING
-       if (TRACK_ALLOC) {
+       if (TRACK_ALLOC)
+       {
            IOLibMallocHeader * hdr;
+           struct ptr_reference{ void * ptr; };
+           volatile struct ptr_reference ptr;
+
+            // we're about to block in IOTrackingRemove(), make sure the original pointer
+            // exists in memory or a register for leak scanning to find
+            ptr.ptr = inAddress;
+
            hdr = (typeof(hdr)) address;
             if (size != hdr->tracking.size)
            {
@@ -302,6 +321,7 @@ void IOFree(void * address, vm_size_t size)
                size = hdr->tracking.size;
            }
            IOTrackingRemove(gIOMallocTracking, &hdr->tracking.tracking, size);
+            ptr.ptr = NULL;
        }
 #endif
 
@@ -737,7 +757,7 @@ kern_return_t IOIteratePageableMaps(vm_size_t size,
             else
                 index = gIOKitPageableSpace.count - 1;
         }
-        if( KERN_SUCCESS == kr)
+        if (KERN_NO_SPACE != kr)
             break;
 
         lck_mtx_lock( gIOKitPageableSpace.lock );
@@ -1117,30 +1137,36 @@ static void _iolog_consputc(int ch, void *arg __unused)
     cons_putc_locked(ch);
 }
 
-static void _iolog_logputc(int ch, void *arg __unused)
-{
-    log_putc_locked(ch);
-}
+static void _IOLogv(const char *format, va_list ap, void *caller);
 
+__attribute__((noinline,not_tail_called))
 void IOLog(const char *format, ...)
 {
+    void *caller = __builtin_return_address(0);
     va_list ap;
 
     va_start(ap, format);
-    IOLogv(format, ap);
+    _IOLogv(format, ap, caller);
     va_end(ap);
 }
 
+__attribute__((noinline,not_tail_called))
 void IOLogv(const char *format, va_list ap)
+{
+    void *caller = __builtin_return_address(0);
+    _IOLogv(format, ap, caller);
+}
+
+void _IOLogv(const char *format, va_list ap, void *caller)
 {
     va_list ap2;
 
+    /* Ideally not called at interrupt context or with interrupts disabled. Needs further validate */
+    /* assert(TRUE == ml_get_interrupts_enabled()); */
+
     va_copy(ap2, ap);
 
-    bsd_log_lock();
-    __doprnt(format, ap, _iolog_logputc, NULL, 16, TRUE);
-    bsd_log_unlock();
-    logwakeup();
+    os_log_with_args(OS_LOG_DEFAULT, OS_LOG_TYPE_DEFAULT, format, ap, caller);
 
     __doprnt(format, ap2, _iolog_consputc, NULL, 16, TRUE);
     va_end(ap2);