Loading...
iokit/bsddev/IOKitBSDInit.cpp xnu-3248.50.21 xnu-4903.231.4
--- xnu/xnu-3248.50.21/iokit/bsddev/IOKitBSDInit.cpp
+++ xnu/xnu-4903.231.4/iokit/bsddev/IOKitBSDInit.cpp
@@ -38,6 +38,7 @@
 
 #include <pexpert/pexpert.h>
 #include <kern/clock.h>
+#include <mach/machine.h>
 #include <uuid/uuid.h>
 #include <sys/vnode_internal.h>
 #include <sys/mount.h>
@@ -54,17 +55,36 @@
 extern dev_t mdevadd(int devid, uint64_t base, unsigned int size, int phys);
 extern dev_t mdevlookup(int devid);
 extern void mdevremoveall(void);
+extern int mdevgetrange(int devid, uint64_t *base, uint64_t *size);
 extern void di_root_ramfile(IORegistryEntry * entry);
 
-
-#if   DEVELOPMENT
+#if CONFIG_EMBEDDED
+#define IOPOLLED_COREFILE 	(CONFIG_KDP_INTERACTIVE_DEBUGGING)
+
+#if defined(XNU_TARGET_OS_BRIDGE)
+
+#define kIOCoreDumpSize         150ULL*1024ULL*1024ULL
+// leave free space on volume:
+#define kIOCoreDumpFreeSize     150ULL*1024ULL*1024ULL
+#define kIOCoreDumpPath         "/private/var/internal/kernelcore"
+
+#else /* defined(XNU_TARGET_OS_BRIDGE) */
+#define kIOCoreDumpMinSize      350ULL*1024ULL*1024ULL
+#define kIOCoreDumpLargeSize    500ULL*1024ULL*1024ULL
+// leave free space on volume:
+#define kIOCoreDumpFreeSize     350ULL*1024ULL*1024ULL
+#define kIOCoreDumpPath         "/private/var/vm/kernelcore"
+
+#endif /* defined(XNU_TARGET_OS_BRIDGE) */
+
+#elif DEVELOPMENT /* CONFIG_EMBEDDED */
 #define IOPOLLED_COREFILE  	1
 // no sizing
 #define kIOCoreDumpSize		0ULL
 #define kIOCoreDumpFreeSize	0ULL
-#else
+#else /* CONFIG_EMBEDDED */
 #define IOPOLLED_COREFILE  	0
-#endif
+#endif /* CONFIG_EMBEDDED */
 
 
 #if IOPOLLED_COREFILE
@@ -74,6 +94,13 @@
 		   IONotifier * notifier);
 #endif /* IOPOLLED_COREFILE */
 
+#if CONFIG_KDP_INTERACTIVE_DEBUGGING
+/*
+ * Touched by IOFindBSDRoot() if a RAMDisk is used for the root device.
+ */
+extern uint64_t kdp_core_ramdisk_addr;
+extern uint64_t kdp_core_ramdisk_size;
+#endif
 
 kern_return_t
 IOKitBSDInit( void )
@@ -356,10 +383,25 @@
     static int		mountAttempts = 0;
 				
     int xchar, dchar;
-                                    
+
+    // stall here for anyone matching on the IOBSD resource to finish (filesystems)
+    matching = IOService::serviceMatching(gIOResourcesKey);
+    assert(matching);
+    matching->setObject(gIOResourceMatchedKey, gIOBSDKey);
+
+	if ((service = IOService::waitForMatchingService(matching, 30ULL * kSecondScale))) {
+		service->release();
+	} else {
+		IOLog("!BSD\n");
+	}
+    matching->release();
+	matching = NULL;
 
     if( mountAttempts++)
+    {
+        IOLog("mount(%d) failed\n", mountAttempts);
 	IOSleep( 5 * 1000 );
+    }
 
     str = (char *) IOMalloc( kMaxPathBuf + kMaxBootVar );
     if( !str)
@@ -439,13 +481,22 @@
 		if(xchar >= 0) {										/* Do we have a valid memory device name? */
 			*root = mdevlookup(xchar);							/* Find the device number */
 			if(*root >= 0) {									/* Did we find one? */
-
 				rootName[0] = 'm';								/* Build root name */
 				rootName[1] = 'd';								/* Build root name */
 				rootName[2] = dchar;							/* Build root name */
 				rootName[3] = 0;								/* Build root name */
 				IOLog("BSD root: %s, major %d, minor %d\n", rootName, major(*root), minor(*root));
 				*oflags = 0;									/* Show that this is not network */
+
+#if CONFIG_KDP_INTERACTIVE_DEBUGGING
+                /* retrieve final ramdisk range and initialize KDP variables */
+                if (mdevgetrange(xchar, &kdp_core_ramdisk_addr, &kdp_core_ramdisk_size) != 0) {
+                    IOLog("Unable to retrieve range for root memory device %d\n", xchar);
+                    kdp_core_ramdisk_addr = 0;
+                    kdp_core_ramdisk_size = 0;
+                }
+#endif
+
 				goto iofrootx;									/* Join common exit... */
 			}
 			panic("IOFindBSDRoot: specified root memory device, %s, has not been configured\n", rdBootVar);	/* Not there */
@@ -625,6 +676,37 @@
 
 void IOSecureBSDRoot(const char * rootName)
 {
+#if CONFIG_EMBEDDED
+    int              tmpInt;
+    IOReturn         result;
+    IOPlatformExpert *pe;
+    OSDictionary     *matching;
+    const OSSymbol   *functionName = OSSymbol::withCStringNoCopy("SecureRootName");
+    
+    matching = IOService::serviceMatching("IOPlatformExpert");
+    assert(matching);
+    pe = (IOPlatformExpert *) IOService::waitForMatchingService(matching, 30ULL * kSecondScale);
+    matching->release();
+    assert(pe);
+    // Returns kIOReturnNotPrivileged is the root device is not secure.
+    // Returns kIOReturnUnsupported if "SecureRootName" is not implemented.
+    result = pe->callPlatformFunction(functionName, false, (void *)rootName, (void *)0, (void *)0, (void *)0);
+    functionName->release();
+    OSSafeReleaseNULL(pe);
+    
+    if (result == kIOReturnNotPrivileged) {
+        mdevremoveall();
+    } else if (result == kIOReturnSuccess) {
+        // If we are booting with a secure root, and we have the right
+	// boot-arg, we will want to panic on exception triage.  This
+	// behavior is intended as a debug aid (we can look at why an
+	// exception occured in the kernel debugger).
+        if (PE_parse_boot_argn("-panic_on_exception_triage", &tmpInt, sizeof(tmpInt))) {
+            panic_on_exception_triage = 1;
+        }
+    }
+
+#endif  // CONFIG_EMBEDDED
 }
 
 void *
@@ -672,117 +754,6 @@
     uuid_parse( string->getCStringNoCopy( ), uuid );
 
     return KERN_SUCCESS;
-}
-
-kern_return_t IOBSDGetPlatformSerialNumber( char *serial_number_str, u_int32_t len )
-{
-    OSDictionary * platform_dict;
-    IOService *platform;
-    OSString *  string;
-
-    if (len < 1) {
-	    return 0;
-    }
-    serial_number_str[0] = '\0';
-
-    platform_dict = IOService::serviceMatching( "IOPlatformExpertDevice" );
-    if (platform_dict == NULL) {
-	    return KERN_NOT_SUPPORTED;
-    }
-
-    platform = IOService::waitForService( platform_dict );
-    if (platform) {
-	    string = ( OSString * ) platform->getProperty( kIOPlatformSerialNumberKey );
-	    if ( string == 0 ) {
-		    return KERN_NOT_SUPPORTED;
-	    } else {
-		    strlcpy( serial_number_str, string->getCStringNoCopy( ), len );
-	    }
-    }
-    
-    return KERN_SUCCESS;
-}
-
-void IOBSDIterateMediaWithContent(const char *content_uuid_cstring, int (*func)(const char *bsd_dev_name, const char *uuid_str, void *arg), void *arg)
-{
-    OSDictionary *dictionary;
-    OSString *content_uuid_string;
-
-    dictionary = IOService::serviceMatching( "IOMedia" );
-    if( dictionary ) {
-	content_uuid_string = OSString::withCString( content_uuid_cstring );
-	if( content_uuid_string ) {
-	    IOService *service;
-	    OSIterator *iter;
-
-	    dictionary->setObject( "Content", content_uuid_string );
-	    dictionary->retain();
-
-	    iter = IOService::getMatchingServices(dictionary);
-	    while (iter && (service = (IOService *)iter->getNextObject())) {
-		    if( service ) {
-			    OSString *iostr = (OSString *) service->getProperty( kIOBSDNameKey );
-			    OSString *uuidstr = (OSString *) service->getProperty( "UUID" );
-			    const char *uuid;
-
-			    if( iostr) {
-				    if (uuidstr) {
-					    uuid = uuidstr->getCStringNoCopy();
-				    } else {
-					    uuid = "00000000-0000-0000-0000-000000000000";
-				    }
-
-				    // call the callback
-				    if (func && func(iostr->getCStringNoCopy(), uuid, arg) == 0) {
-					    break;
-				    }
-			    }
-		    }
-	    }
-	    if (iter)
-		    iter->release();
-	    
-	    content_uuid_string->release();
-	}
-	dictionary->release();
-    }
-}
-
-
-int IOBSDIsMediaEjectable( const char *cdev_name )
-{
-    int ret = 0;
-    OSDictionary *dictionary;
-    OSString *dev_name;
-
-    if (strncmp(cdev_name, "/dev/", 5) == 0) {
-	    cdev_name += 5;
-    }
-
-    dictionary = IOService::serviceMatching( "IOMedia" );
-    if( dictionary ) {
-	dev_name = OSString::withCString( cdev_name );
-	if( dev_name ) {
-	    IOService *service;
-	    mach_timespec_t tv = { 5, 0 };    // wait up to "timeout" seconds for the device
-
-	    dictionary->setObject( kIOBSDNameKey, dev_name );
-	    dictionary->retain();
-	    service = IOService::waitForService( dictionary, &tv );
-	    if( service ) {
-		OSBoolean *ejectable = (OSBoolean *) service->getProperty( "Ejectable" );
-
-		if( ejectable ) {
-			ret = (int)ejectable->getValue();
-		}
-
-	    }
-	    dev_name->release();
-	}
-	dictionary->release();
-    }
-
-    return ret;
 }
 
 } /* extern "C" */
@@ -797,7 +768,7 @@
 #include <IOKit/IOBufferMemoryDescriptor.h>
 
 IOPolledFileIOVars * gIOPolledCoreFileVars;
-
+kern_return_t gIOPolledCoreFileOpenRet = kIOReturnNotReady;
 #if IOPOLLED_COREFILE
 
 static IOReturn 
@@ -805,6 +776,7 @@
 {
     IOReturn err;
     unsigned int debug;
+    uint64_t corefile_size_bytes = 0;
 
     if (gIOPolledCoreFileVars)                             return (kIOReturnBusy);
     if (!IOPolledInterface::gMetaClass.getInstanceCount()) return (kIOReturnUnsupported);
@@ -813,15 +785,89 @@
     PE_parse_boot_argn("debug", &debug, sizeof (debug));
     if (DB_DISABLE_LOCAL_CORE & debug)                     return (kIOReturnUnsupported);
 
-    err = IOPolledFileOpen(filename, kIOCoreDumpSize, kIOCoreDumpFreeSize,
-			    NULL, 0,
-			    &gIOPolledCoreFileVars, NULL, NULL, 0);
-    if (kIOReturnSuccess != err)                           return (err);
+#if CONFIG_EMBEDDED
+    unsigned int requested_corefile_size = 0;
+    if (PE_parse_boot_argn("corefile_size_mb", &requested_corefile_size, sizeof(requested_corefile_size))) {
+        IOLog("Boot-args specify %d MB kernel corefile\n", requested_corefile_size);
+
+        corefile_size_bytes = (requested_corefile_size * 1024ULL * 1024ULL);
+    }
+#endif
+
+
+    do {
+#if defined(kIOCoreDumpLargeSize)
+        if (0 == corefile_size_bytes)
+        {
+                // If no custom size was requested and we're on a device with >3GB of DRAM, attempt
+                // to allocate a large corefile otherwise use a small file.
+                if (max_mem > (3 * 1024ULL * 1024ULL * 1024ULL))
+                {
+                        corefile_size_bytes = kIOCoreDumpLargeSize;
+                        err = IOPolledFileOpen(filename,
+                                                kIOPolledFileCreate,
+                                                corefile_size_bytes, kIOCoreDumpFreeSize,
+                                                NULL, 0,
+                                                &gIOPolledCoreFileVars, NULL, NULL, 0);
+                        if (kIOReturnSuccess == err)
+                        {
+                                break;
+                        }
+                        else if (kIOReturnNoSpace == err)
+                        {
+                                IOLog("Failed to open corefile of size %llu MB (low disk space)",
+                                        (corefile_size_bytes / (1024ULL * 1024ULL)));
+                                if (corefile_size_bytes == kIOCoreDumpMinSize)
+                                {
+                                        gIOPolledCoreFileOpenRet = err;
+                                        return (err);
+                                }
+                                // Try to open a smaller corefile (set size and fall-through)
+                                corefile_size_bytes = kIOCoreDumpMinSize;
+                        }
+                        else
+                        {
+                                IOLog("Failed to open corefile of size %llu MB (returned error 0x%x)\n",
+                                        (corefile_size_bytes / (1024ULL * 1024ULL)), err);
+                                gIOPolledCoreFileOpenRet = err;
+                                return (err);
+                        }
+                }
+                else
+                {
+                        corefile_size_bytes = kIOCoreDumpMinSize;
+                }
+        }
+#else /* defined(kIOCoreDumpLargeSize) */
+        if (0 == corefile_size_bytes)
+        {
+            corefile_size_bytes = kIOCoreDumpSize;
+        }
+#endif /* defined(kIOCoreDumpLargeSize) */
+        err = IOPolledFileOpen(filename,
+                    kIOPolledFileCreate,
+                    corefile_size_bytes, kIOCoreDumpFreeSize,
+                    NULL, 0,
+                    &gIOPolledCoreFileVars, NULL, NULL, 0);
+        if (kIOReturnSuccess != err)
+        {
+                IOLog("Failed to open corefile of size %llu MB (returned error 0x%x)\n",
+                                (corefile_size_bytes / (1024ULL * 1024ULL)), err);
+                gIOPolledCoreFileOpenRet = err;
+                return (err);
+        }
+    } while (false);
 
     err = IOPolledFilePollersSetup(gIOPolledCoreFileVars, kIOPolledPreflightCoreDumpState);
     if (kIOReturnSuccess != err)
     {
-	IOPolledFileClose(&gIOPolledCoreFileVars, NULL, NULL, 0, 0, 0);
+        IOPolledFileClose(&gIOPolledCoreFileVars, NULL, NULL, 0, 0, 0);
+        IOLog("IOPolledFilePollersSetup for corefile failed with error: 0x%x\n", err);
+        gIOPolledCoreFileOpenRet = err;
+    }
+    else
+    {
+        IOLog("Opened corefile of size %llu MB\n", (corefile_size_bytes / (1024ULL * 1024ULL)));
     }
 
     return (err);
@@ -830,6 +876,7 @@
 static void 
 IOClosePolledCoreFile(void)
 {
+    gIOPolledCoreFileOpenRet = kIOReturnNotOpen;
     IOPolledFilePollersClose(gIOPolledCoreFileVars, kIOPolledPostflightCoreDumpState);
     IOPolledFileClose(&gIOPolledCoreFileVars, NULL, NULL, 0, 0, 0);
 }
@@ -881,8 +928,10 @@
 		   IOService * newService,
 		   IONotifier * notifier)
 {
+    static volatile UInt32 onlyOneCorePartition = 0;
     do
     {
+	if (!OSCompareAndSwap(0, 1, &onlyOneCorePartition)) break;
 	if (gIOPolledCoreFileVars)    break;
         if (!gIOOpenPolledCoreFileTC) break;
         newService = newService->getProvider();
@@ -925,6 +974,49 @@
     }
     while (false);
 
+#if CONFIG_EMBEDDED
+    uint64_t flags;
+    char path[128];
+    int pathLen;
+    vnode_t vn;
+    int result;
+
+    switch (op)
+    {
+	case kIOMountChangeMount:
+	case kIOMountChangeDidResize:
+
+	    if (gIOPolledCoreFileVars) break;
+	    flags = vfs_flags(mp);
+	    if (MNT_RDONLY & flags) break;
+	    if (!(MNT_LOCAL & flags)) break;
+
+	    vn = vfs_vnodecovered(mp);
+	    if (!vn) break;
+	    pathLen = sizeof(path);
+	    result = vn_getpath(vn, &path[0], &pathLen);
+	    vnode_put(vn);
+	    if (0 != result) break;
+	    if (!pathLen) break;
+#if defined(XNU_TARGET_OS_BRIDGE)
+	    // on bridgeOS systems we put the core in /private/var/internal. We don't
+	    // want to match with /private/var because /private/var/internal is often mounted
+	    // over /private/var
+	    if ((pathLen - 1) < (int) strlen("/private/var/internal")) break;
+#endif
+	    if (0 != strncmp(path, kIOCoreDumpPath, pathLen - 1)) break;
+	    IOOpenPolledCoreFile(kIOCoreDumpPath);
+	    break;
+
+	case kIOMountChangeUnmount:
+	case kIOMountChangeWillResize:
+	    if (gIOPolledCoreFileVars && (mp == kern_file_mount(gIOPolledCoreFileVars->fileRef)))
+	    {
+		IOClosePolledCoreFile();
+	    }
+	    break;
+    }
+#endif /* CONFIG_EMBEDDED */
 #endif /* IOPOLLED_COREFILE */
 }