]> git.saurik.com Git - apple/xnu.git/blobdiff - libkern/c++/OSKext.cpp
xnu-4570.1.46.tar.gz
[apple/xnu.git] / libkern / c++ / OSKext.cpp
index 5f77f045be558e49162be24136267729340f26e0..797fd38a25101b6ee998442653c8c53733c0c87a 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2008-2012 Apple Inc. All rights reserved.
+ * Copyright (c) 2008-2016 Apple Inc. All rights reserved.
  *
  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
  * 
  */
 
 extern "C" {
+#include <string.h>
 #include <kern/clock.h>
 #include <kern/host.h>
 #include <kern/kext_alloc.h>
+#include <firehose/tracepoint_private.h>
+#include <firehose/chunk_private.h>
+#include <os/firehose_buffer_private.h>
 #include <vm/vm_kern.h>
 #include <kextd/kextd_mach.h>
 #include <libkern/kernel_mach_header.h>
@@ -67,6 +71,8 @@ extern "C" {
 #include <IOKit/IOStatisticsPrivate.h>
 #include <IOKit/IOBSD.h>
 
+#include <san/kasan.h>
+
 #if PRAGMA_MARK
 #pragma mark External & Internal Function Protos
 #endif
@@ -117,6 +123,9 @@ static void * GetAppleTEXTHashForKext(OSKext * theKext, OSDictionary *theInfoDic
 #define VM_MAPPED_KEXTS 1
 #define KASLR_KEXT_DEBUG 0
 #define KASLR_IOREG_DEBUG 0
+#elif __arm__ || __arm64__
+#define VM_MAPPED_KEXTS 0
+#define KASLR_KEXT_DEBUG 0
 #else
 #error Unsupported architecture
 #endif
@@ -180,6 +189,12 @@ static void * GetAppleTEXTHashForKext(OSKext * theKext, OSDictionary *theInfoDic
  */
 #define _kOSKextExecutableExternalDataKey    "_OSKextExecutableExternalData"
 
+#define OS_LOG_HDR_VERSION  1
+#define NUM_OS_LOG_SECTIONS 2
+
+#define OS_LOG_SECT_IDX     0
+#define CSTRING_SECT_IDX    1
+
 #if PRAGMA_MARK
 #pragma mark Typedefs
 #endif
@@ -187,6 +202,22 @@ static void * GetAppleTEXTHashForKext(OSKext * theKext, OSDictionary *theInfoDic
 * Typedefs
 *********************************************************************/
 
+/*********************************************************************
+* osLogDataHeaderRef describes the header information of an OSData
+* object that is returned when querying for kOSBundleLogStringsKey.
+* We currently return information regarding 2 sections - os_log and
+* cstring. In the case that the os_log section doesn't exist, we just
+* return an offset and length of 0 for that section.
+*********************************************************************/
+typedef struct osLogDataHeader {
+    uint32_t version;
+    uint32_t sect_count;
+    struct {
+         uint32_t sect_offset;
+         uint32_t sect_size;
+    } sections[0];
+} osLogDataHeaderRef;
+
 /*********************************************************************
 * MkextEntryRef describes the contents of an OSData object
 * referencing a file entry from an mkext so that we can uncompress
@@ -288,7 +319,7 @@ kmod_info_t g_kernel_kmod_info = {
     /* version         */ "0",               // filled in in OSKext::initialize()
     /* reference_count */ -1,                // never adjusted; kernel never unloads
     /* reference_list  */ NULL,
-    /* address         */ NULL,
+    /* address         */ 0,
     /* size            */ 0,                 // filled in in OSKext::initialize()
     /* hdr_size        */ 0,
     /* start           */ 0,
@@ -363,9 +394,10 @@ static IOSimpleLock           * sKextAccountsLock = &vm_allocation_sites_lock;
 
 void (*sLoadedKextSummariesUpdated)(void) = OSKextLoadedKextSummariesUpdated;
 OSKextLoadedKextSummaryHeader * gLoadedKextSummaries __attribute__((used)) = NULL;
+uint64_t gLoadedKextSummariesTimestamp __attribute__((used)) = 0;
 static size_t sLoadedKextSummariesAllocSize = 0;
 
-static OSKextActiveAccount    * sKextAccounts;
+static OSKextActiveAccount * sKextAccounts;
 static uint32_t                 sKextAccountsCount;
 };
 
@@ -379,7 +411,7 @@ static  const OSKextLogSpec kDefaultKernelLogFilter    = kOSKextLogBasicLevel |
 static  OSKextLogSpec       sKernelLogFilter           = kDefaultKernelLogFilter;
 static  bool                sBootArgLogFilterFound     = false;
 SYSCTL_UINT(_debug, OID_AUTO, kextlog, CTLFLAG_RW | CTLFLAG_LOCKED, &sKernelLogFilter,
-    sKernelLogFilter, "kernel kext logging");
+    0, "kernel kext logging");
 
 static  OSKextLogSpec       sUserSpaceKextLogFilter    = kOSKextLogSilentFilter;
 static  OSArray           * sUserSpaceLogSpecArray     = NULL;
@@ -494,7 +526,7 @@ finish:
         result = 0;
     }
 
-    OSSafeRelease(linkBuffer);
+    OSSafeReleaseNULL(linkBuffer);
 
     return (kxld_addr_t)result;
 }
@@ -654,6 +686,10 @@ OSKext::initialize(void)
     }
 
     PE_parse_boot_argn("keepsyms", &sKeepSymbols, sizeof(sKeepSymbols));
+#if KASAN_DYNAMIC_BLACKLIST
+    /* needed for function lookup */
+    sKeepSymbols = true;
+#endif
 
    /* Set up an OSKext instance to represent the kernel itself.
     */
@@ -667,10 +703,11 @@ OSKext::initialize(void)
     assert(kernelExecutable);
 
 #if KASLR_KEXT_DEBUG 
-    IOLog("kaslr: kernel start 0x%lx end 0x%lx length %lu \n", 
+    IOLog("kaslr: kernel start 0x%lx end 0x%lx length %lu vm_kernel_slide %llu (0x%016lx) \n",
           (unsigned long)kernelStart, 
           (unsigned long)getlastaddr(),
-          kernelLength);
+          kernelLength,
+          vm_kernel_slide, vm_kernel_slide);
 #endif
 
     sKernelKext->loadTag = sNextLoadTag++;  // the kernel is load tag 0
@@ -742,8 +779,8 @@ OSKext::initialize(void)
     registryRoot->setProperty(kOSKernelCPUTypeKey, kernelCPUType);
     registryRoot->setProperty(kOSKernelCPUSubtypeKey, kernelCPUSubtype);
 
-    OSSafeRelease(kernelCPUType);
-    OSSafeRelease(kernelCPUSubtype);
+    OSSafeReleaseNULL(kernelCPUType);
+    OSSafeReleaseNULL(kernelCPUSubtype);
 
     timestamp = __OSAbsoluteTimePtr(&last_loaded_timestamp);
     *timestamp = 0;
@@ -784,6 +821,11 @@ OSKext::removeKextBootstrap(void)
 
     kernel_segment_command_t * seg_to_remove         = NULL;
 
+#if __arm__ || __arm64__
+    const char               * dt_segment_name       = NULL;
+    void                     * segment_paddress      = NULL;
+    int                        segment_size          = 0;
+#endif
 
    /* This must be the very first thing done by this function.
     */
@@ -826,7 +868,21 @@ OSKext::removeKextBootstrap(void)
         OSRuntimeUnloadCPPForSegment(seg_to_remove);
     }
 
-#if   __i386__ || __x86_64__
+#if __arm__ || __arm64__
+#if !(defined(KERNEL_INTEGRITY_KTRR))
+   /* Free the memory that was set up by bootx.
+    */
+    dt_segment_name = "Kernel-__KLD";
+    if (0 == IODTGetLoaderInfo(dt_segment_name, &segment_paddress, &segment_size)) {
+       /* We cannot free this with KTRR enabled, as we cannot
+        * update the permissions on the KLD range this late
+        * in the boot process.
+        */
+        IODTFreeLoaderInfo(dt_segment_name, (void *)segment_paddress,
+            (int)segment_size);
+    }
+#endif /* !(defined(KERNEL_INTEGRITY_KTRR)) */
+#elif __i386__ || __x86_64__
    /* On x86, use the mapping data from the segment load command to
     * unload KLD directly.
     * This may invalidate any assumptions about  "avail_start"
@@ -862,6 +918,9 @@ OSKext::removeKextBootstrap(void)
      * managed memory, then copy the segment back in.
      */
 #if CONFIG_KXLD
+#if (__arm__ || __arm64__)
+#error CONFIG_KXLD not expected for this arch
+#endif
     if (!sKeepSymbols) {
         kern_return_t mem_result;
         void *seg_copy = NULL;
@@ -902,6 +961,8 @@ OSKext::removeKextBootstrap(void)
             &seg_offset,
             seg_length, /* mask */ 0, 
             VM_FLAGS_FIXED | VM_FLAGS_OVERWRITE, 
+           VM_MAP_KERNEL_FLAGS_NONE,
+           VM_KERN_MEMORY_NONE,
             (ipc_port_t)NULL,
             (vm_object_offset_t) 0,
             /* copy */ FALSE,
@@ -928,13 +989,15 @@ OSKext::removeKextBootstrap(void)
         kmem_free(kernel_map, seg_copy_offset, seg_length);
     }
 #else /* we are not CONFIG_KXLD */
+#if !(__arm__ || __arm64__)
 #error CONFIG_KXLD is expected for this arch
+#endif
 
     /*****
     * Dump the LINKEDIT segment, unless keepsyms is set.
     */
     if (!sKeepSymbols) {
-        const char *dt_segment_name = "Kernel-__LINKEDIT";
+        dt_segment_name = "Kernel-__LINKEDIT";
         if (0 == IODTGetLoaderInfo(dt_segment_name,
             &segment_paddress, &segment_size)) {
 #ifdef SECURE_KERNEL
@@ -1051,9 +1114,9 @@ OSKext::flushNonloadedKexts(
 finish:
     IORecursiveLockUnlock(sKextLock);
 
-    OSSafeRelease(prelinkedKexts);
-    OSSafeRelease(kextIterator);
-    OSSafeRelease(prelinkIterator);
+    OSSafeReleaseNULL(prelinkedKexts);
+    OSSafeReleaseNULL(kextIterator);
+    OSSafeReleaseNULL(prelinkIterator);
 
     return;
 }
@@ -1176,7 +1239,7 @@ finish:
 
     IORecursiveLockUnlock(sKextLock);
 
-    OSSafeRelease(exitRequest);
+    OSSafeReleaseNULL(exitRequest);
     return;
 }
 
@@ -1364,11 +1427,12 @@ OSKext::getKernelRequestsEnabled(void)
 *********************************************************************/
 OSKext *
 OSKext::withPrelinkedInfoDict(
-    OSDictionary * anInfoDict)
+    OSDictionary * anInfoDict,
+    bool doCoalesedSlides)
 {
     OSKext * newKext = new OSKext;
 
-    if (newKext && !newKext->initWithPrelinkedInfoDict(anInfoDict)) {
+    if (newKext && !newKext->initWithPrelinkedInfoDict(anInfoDict, doCoalesedSlides)) {
         newKext->release();
         return NULL;
     }
@@ -1380,7 +1444,8 @@ OSKext::withPrelinkedInfoDict(
 *********************************************************************/
 bool
 OSKext::initWithPrelinkedInfoDict(
-    OSDictionary * anInfoDict)
+    OSDictionary * anInfoDict,
+    bool doCoalesedSlides)
 {
     bool            result              = false;
     OSString      * kextPath            = NULL;  // do not release
@@ -1404,7 +1469,7 @@ OSKext::initWithPrelinkedInfoDict(
         goto finish;
     }
 #if KASLR_KEXT_DEBUG
-    IOLog("kaslr: kext %s \n", getIdentifierCString());
+    IOLog("kaslr: doCoalesedSlides %d kext %s \n", doCoalesedSlides, getIdentifierCString());
 #endif
 
    /* Also get the executable's bundle-relative path if present.
@@ -1441,7 +1506,7 @@ OSKext::initWithPrelinkedInfoDict(
         length = (uint32_t) (lengthNum->unsigned32BitValue());
 
 #if KASLR_KEXT_DEBUG
-        IOLog("kaslr: unslid 0x%lx slid 0x%lx length %u - prelink executable \n", 
+        IOLog("kaslr: unslid 0x%lx slid 0x%lx length %u - prelink executable \n",
               (unsigned long)VM_KERNEL_UNSLIDE(data), 
               (unsigned long)data,
               length);
@@ -1450,42 +1515,42 @@ OSKext::initWithPrelinkedInfoDict(
         anInfoDict->removeObject(kPrelinkExecutableLoadKey);
         anInfoDict->removeObject(kPrelinkExecutableSizeKey);
 
-       /* If the kext's load address differs from its source address, allocate
-        * space in the kext map at the load address and copy the kext over.
-        */
+        /* If the kext's load address differs from its source address, allocate
+         * space in the kext map at the load address and copy the kext over.
+         */
         addressNum = OSDynamicCast(OSNumber, anInfoDict->getObject(kPrelinkExecutableSourceKey));
         if (addressNum) {
             srcData = (void *) ((intptr_t) (addressNum->unsigned64BitValue()) + vm_kernel_slide);
-
+            
 #if KASLR_KEXT_DEBUG
-            IOLog("kaslr: unslid 0x%lx slid 0x%lx - prelink executable source \n", 
-                  (unsigned long)VM_KERNEL_UNSLIDE(srcData), 
+            IOLog("kaslr: unslid 0x%lx slid 0x%lx - prelink executable source \n",
+                  (unsigned long)VM_KERNEL_UNSLIDE(srcData),
                   (unsigned long)srcData);
 #endif
-
+            
             if (data != srcData) {
 #if __LP64__
                 kern_return_t alloc_result;
-
+                
                 alloc_result = kext_alloc((vm_offset_t *)&data, length, /* fixed */ TRUE);
                 if (alloc_result != KERN_SUCCESS) {
                     OSKextLog(this,
-                        kOSKextLogErrorLevel | kOSKextLogGeneralFlag,
-                        "Failed to allocate space for prelinked kext %s.",
-                        getIdentifierCString());
+                              kOSKextLogErrorLevel | kOSKextLogGeneralFlag,
+                              "Failed to allocate space for prelinked kext %s.",
+                              getIdentifierCString());
                     goto finish;
                 }
                 memcpy(data, srcData, length);
 #else
                 OSKextLog(this,
-                    kOSKextLogErrorLevel | kOSKextLogGeneralFlag,
-                    "Error: prelinked kext %s - source and load addresses "
-                    "differ on ILP32 architecture.",
-                    getIdentifierCString());
+                          kOSKextLogErrorLevel | kOSKextLogGeneralFlag,
+                          "Error: prelinked kext %s - source and load addresses "
+                          "differ on ILP32 architecture.",
+                          getIdentifierCString());
                 goto finish;
 #endif /* __LP64__ */
             }
-
+            
             anInfoDict->removeObject(kPrelinkExecutableSourceKey);
         }
 
@@ -1520,7 +1585,7 @@ OSKext::initWithPrelinkedInfoDict(
             kmod_info = (kmod_info_t *) (intptr_t) (addressNum->unsigned64BitValue() + vm_kernel_slide);
             kmod_info->address += vm_kernel_slide;
 #if KASLR_KEXT_DEBUG
-            IOLog("kaslr: unslid 0x%lx slid 0x%lx - kmod_info \n", 
+            IOLog("kaslr: unslid 0x%lx slid 0x%lx - kmod_info \n",
                   (unsigned long)VM_KERNEL_UNSLIDE(kmod_info), 
                   (unsigned long)kmod_info);
             IOLog("kaslr: unslid 0x%lx slid 0x%lx - kmod_info->address \n", 
@@ -1543,17 +1608,19 @@ OSKext::initWithPrelinkedInfoDict(
         }
     }
 
-    result = slidePrelinkedExecutable();
+    result = slidePrelinkedExecutable(doCoalesedSlides);
     if (result != kOSReturnSuccess) {
         goto finish;
     }
 
-    /* set VM protections now, wire later at kext load */
-    result = setVMAttributes(true, false);
-    if (result != KERN_SUCCESS) {
-        goto finish;
+    if (doCoalesedSlides == false) {
+        /* set VM protections now, wire later at kext load */
+        result = setVMAttributes(true, false);
+        if (result != KERN_SUCCESS) {
+            goto finish;
+        }
     }
-
+    
     flags.prelinked = true;
 
    /* If we created a kext from prelink info,
@@ -1564,10 +1631,45 @@ OSKext::initWithPrelinkedInfoDict(
     result = registerIdentifier();
 
 finish:
-    OSSafeRelease(prelinkedExecutable);
+    OSSafeReleaseNULL(prelinkedExecutable);
 
     return result;
 }
+
+/*********************************************************************
+ *********************************************************************/
+/* static */
+void OSKext::setAllVMAttributes(void)
+{
+    OSCollectionIterator * kextIterator     = NULL;  // must release
+    const OSSymbol * thisID                 = NULL;  // do not release
+
+    IORecursiveLockLock(sKextLock);
+
+    kextIterator = OSCollectionIterator::withCollection(sKextsByID);
+    if (!kextIterator) {
+        goto finish;
+    }
+    
+    while ((thisID = OSDynamicCast(OSSymbol, kextIterator->getNextObject()))) {
+        OSKext *    thisKext;  // do not release
+        
+        thisKext = OSDynamicCast(OSKext, sKextsByID->getObject(thisID));
+        if (!thisKext || thisKext->isInterface() || !thisKext->declaresExecutable()) {
+            continue;
+        }
+       
+        /* set VM protections now, wire later at kext load */
+        thisKext->setVMAttributes(true, false);
+    }
+    
+finish:
+    IORecursiveLockUnlock(sKextLock);
+    OSSafeReleaseNULL(kextIterator);
+
+    return;
+}
+
 /*********************************************************************
 *********************************************************************/
 OSKext *
@@ -1743,10 +1845,10 @@ OSKext::initWithBooterData(
     result = registerIdentifier();
 
 finish:
-    OSSafeRelease(parsedXML);
-    OSSafeRelease(kextPath);
-    OSSafeRelease(errorString);
-    OSSafeRelease(executable);
+    OSSafeReleaseNULL(parsedXML);
+    OSSafeReleaseNULL(kextPath);
+    OSSafeReleaseNULL(errorString);
+    OSSafeReleaseNULL(executable);
 
     return result;
 }
@@ -1943,8 +2045,8 @@ finish:
             getIdentifierCString(), newVersionCString);
     }
 
-    OSSafeRelease(newUUID);
-    OSSafeRelease(existingUUID);
+    OSSafeReleaseNULL(newUUID);
+    OSSafeReleaseNULL(existingUUID);
 
     return result;
 }
@@ -2275,14 +2377,14 @@ OSKext::free(void)
         panic("Attempt to free loaded kext %s.", getIdentifierCString());
     }
 
-    OSSafeRelease(infoDict);
-    OSSafeRelease(bundleID);
-    OSSafeRelease(path);
-    OSSafeRelease(executableRelPath);
-    OSSafeRelease(dependencies);
-    OSSafeRelease(linkedExecutable);
-    OSSafeRelease(metaClasses);
-    OSSafeRelease(interfaceUUID);
+    OSSafeReleaseNULL(infoDict);
+    OSSafeReleaseNULL(bundleID);
+    OSSafeReleaseNULL(path);
+    OSSafeReleaseNULL(executableRelPath);
+    OSSafeReleaseNULL(dependencies);
+    OSSafeReleaseNULL(linkedExecutable);
+    OSSafeReleaseNULL(metaClasses);
+    OSSafeReleaseNULL(interfaceUUID);
 
     if (isInterface() && kmod_info) {
         kfree(kmod_info, sizeof(kmod_info_t));
@@ -2503,14 +2605,16 @@ OSKext::readMkext2Archive(
 
 
         infoDict = OSDynamicCast(OSDictionary,
-            mkextInfoDictArray->getObject(i));
+                                 mkextInfoDictArray->getObject(i));
         
        /* Create the kext for the entry, then release it, because the
         * kext system keeps them around until explicitly removed.
         * Any creation/registration failures are already logged for us.
         */
-        OSKext * newKext = OSKext::withMkext2Info(infoDict, mkextData);
-        OSSafeRelease(newKext);
+        if (infoDict) {
+            OSKext * newKext = OSKext::withMkext2Info(infoDict, mkextData);
+            OSSafeReleaseNULL(newKext);
+        }
     }
 
    /* Even if we didn't keep any kexts from the mkext, we may have a load
@@ -2520,9 +2624,9 @@ OSKext::readMkext2Archive(
 
 finish:
 
-    OSSafeRelease(parsedXML);
-    OSSafeRelease(mkextPlistUncompressedData);
-    OSSafeRelease(errorString);
+    OSSafeReleaseNULL(parsedXML);
+    OSSafeReleaseNULL(mkextPlistUncompressedData);
+    OSSafeReleaseNULL(errorString);
 
     return result;
 }
@@ -2558,14 +2662,14 @@ OSKext::initWithMkext2Info(
     OSCollectionIterator * iterator            = NULL;  // must release
     OSData               * executable          = NULL;  // must release
 
-    if (!super::init()) {
+    if (anInfoDict == NULL || !super::init()) {
         goto finish;
     }
 
    /* Get the path. Don't look for an arch-specific path property.
     */
     kextPath = OSDynamicCast(OSString,
-        anInfoDict->getObject(kMKEXTBundlePathKey));
+                             anInfoDict->getObject(kMKEXTBundlePathKey));
 
     if (!setInfoDictionaryAndPath(anInfoDict, kextPath)) {
         goto finish;
@@ -2602,8 +2706,8 @@ OSKext::initWithMkext2Info(
 
 finish:
 
-    OSSafeRelease(executable);
-    OSSafeRelease(iterator);
+    OSSafeReleaseNULL(executable);
+    OSSafeReleaseNULL(iterator);
     return result;
 }
 
@@ -2861,7 +2965,7 @@ finish:
     if (zstream_inited) inflateEnd(&zstream);
 
     if (!result) {
-        OSSafeRelease(uncompressedData);
+        OSSafeReleaseNULL(uncompressedData);
     }
 
     return result;
@@ -3072,10 +3176,10 @@ finish:
 
     IORecursiveLockUnlock(sKextLock);
 
-    OSSafeRelease(mkextData);
-    OSSafeRelease(mkextPlist);
-    OSSafeRelease(serializer);
-    OSSafeRelease(logInfoArray);
+    OSSafeReleaseNULL(mkextData);
+    OSSafeReleaseNULL(mkextPlist);
+    OSSafeReleaseNULL(serializer);
+    OSSafeReleaseNULL(logInfoArray);
 
     return result;
 }
@@ -3147,7 +3251,7 @@ OSKext::serializeLogInfo(
     
     result = kOSReturnSuccess;
 finish:
-    OSSafeRelease(serializer);
+    OSSafeReleaseNULL(serializer);
     return result;
 }
 
@@ -3223,7 +3327,6 @@ OSKext::lookupKextWithAddress(vm_address_t address)
                 (vm_address_t)thisKext->linkedExecutable->getBytesNoCopy();
             vm_address_t kext_end = kext_start +
                 thisKext->linkedExecutable->getLength();
-            
             if ((kext_start <= address) && (address < kext_end)) {
                 foundKext = thisKext;
                 foundKext->retain();
@@ -3238,6 +3341,52 @@ finish:
     return foundKext;
 }
 
+OSData *
+OSKext::copyKextUUIDForAddress(OSNumber *address)
+{
+       OSData *uuid = NULL;
+
+       if (!address) {
+               return NULL;
+       }
+
+       uintptr_t addr = (uintptr_t)address->unsigned64BitValue() + vm_kernel_slide;
+
+#if CONFIG_MACF
+       /* Is the calling process allowed to query kext info? */
+       if (current_task() != kernel_task) {
+               int macCheckResult = 0;
+               kauth_cred_t cred = NULL;
+
+               cred = kauth_cred_get_with_ref();
+               macCheckResult = mac_kext_check_query(cred);
+               kauth_cred_unref(&cred);
+
+               if (macCheckResult != 0) {
+                       OSKextLog(/* kext */ NULL,
+                                       kOSKextLogErrorLevel | kOSKextLogLoadFlag,
+                                       "Failed to query kext UUID (MAC policy error 0x%x).",
+                                       macCheckResult);
+                       return NULL;
+               }
+       }
+#endif
+
+       if (((vm_offset_t)addr >= vm_kernel_stext) && ((vm_offset_t)addr < vm_kernel_etext)) {
+               /* address in xnu proper */
+               unsigned long uuid_len = 0;
+               uuid = OSData::withBytes(getuuidfromheader(&_mh_execute_header, &uuid_len), uuid_len);
+       } else {
+               IOLockLock(sKextSummariesLock);
+               OSKextLoadedKextSummary *summary = OSKext::summaryForAddress(addr);
+               if (summary) {
+                       uuid = OSData::withBytes(summary->uuid, sizeof(uuid_t));
+               }
+               IOLockUnlock(sKextSummariesLock);
+       }
+
+       return uuid;
+}
 
 /*********************************************************************
 *********************************************************************/
@@ -3313,8 +3462,21 @@ bool OSKext::isKextWithIdentifierLoaded(const char * kextIdentifier)
 OSReturn
 OSKext::removeKext(
     OSKext * aKext,
+#if CONFIG_EMBEDDED
+    __unused
+#endif
     bool     terminateServicesAndRemovePersonalitiesFlag)
  {
+#if CONFIG_EMBEDDED
+    OSKextLog(aKext,
+        kOSKextLogErrorLevel |
+        kOSKextLogKextBookkeepingFlag,
+        "removeKext() called for %s, not supported on embedded",
+        aKext->getIdentifier() ? aKext->getIdentifierCString() : "unknown kext");
+
+    return kOSReturnSuccess;
+#else /* CONFIG_EMBEDDED */
+
     OSReturn result    = kOSKextReturnInUse;
     OSKext * checkKext = NULL;   // do not release
 #if CONFIG_MACF
@@ -3409,6 +3571,7 @@ OSKext::removeKext(
 finish:
     IORecursiveLockUnlock(sKextLock);
     return result;
+#endif /* CONFIG_EMBEDDED */
  }
 
 /*********************************************************************
@@ -3600,17 +3763,20 @@ OSKext::createExcludeListFromBooterData(
             myTempDict = OSDynamicCast(
                                        OSDictionary,
                                        theInfoDict->getObject("OSKextExcludeList"));
-            if ( myTempDict ) {
-                IORecursiveLockLock(sKextLock);
-                
-                /* get rid of old exclusion list */
-                if (sExcludeListByID) {
-                    sExcludeListByID->flushCollection();
-                    OSSafeRelease(sExcludeListByID);
-                }
-                sExcludeListByID = OSDictionary::withDictionary(myTempDict, 0);
-                IORecursiveLockUnlock(sKextLock);
+            if ( NULL == myTempDict ) {
+                /* 25322874 */
+                panic("Missing OSKextExcludeList dictionary\n");
+            }
+            
+            IORecursiveLockLock(sKextLock);
+            
+            /* get rid of old exclusion list */
+            if (sExcludeListByID) {
+                OSSafeReleaseNULL(sExcludeListByID);
             }
+            sExcludeListByID = OSDictionary::withDictionary(myTempDict, 0);
+            IORecursiveLockUnlock(sKextLock);
+
             break;
         }
         
@@ -3647,17 +3813,19 @@ OSKext::createExcludeListFromPrelinkInfo( OSArray * theInfoArray )
             OSDictionary *      myTempDict;     // do not free
             myTempDict = OSDynamicCast(OSDictionary,
                                        myInfoDict->getObject("OSKextExcludeList"));
-            if ( myTempDict ) {
-                IORecursiveLockLock(sKextLock);
-                // get rid of old exclude list
-                if (sExcludeListByID) {
-                    sExcludeListByID->flushCollection();
-                    OSSafeRelease(sExcludeListByID);
-                }
-                
-                sExcludeListByID = OSDictionary::withDictionary(myTempDict, 0);
-                IORecursiveLockUnlock(sKextLock);
+            if ( NULL == myTempDict ) {
+                /* 25322874 */
+                panic("Missing OSKextExcludeList dictionary\n");
+            }
+            
+            IORecursiveLockLock(sKextLock);
+            // get rid of old exclude list
+            if (sExcludeListByID) {
+                OSSafeReleaseNULL(sExcludeListByID);
             }
+            
+            sExcludeListByID = OSDictionary::withDictionary(myTempDict, 0);
+            IORecursiveLockUnlock(sKextLock);
             break;
         }
     } // for (i = 0; i < theInfoArray->getCount()...
@@ -3786,7 +3954,7 @@ OSKext::getExecutable(void)
 
 finish:
 
-    OSSafeRelease(extractedExecutable);
+    OSSafeReleaseNULL(extractedExecutable);
 
     return result;
 }
@@ -3992,9 +4160,30 @@ finish:
 
 /*********************************************************************
 *********************************************************************/
+#if defined (__arm__)
+#include <arm/arch.h>
+#endif
 
 #if   defined (__x86_64__)
 #define ARCHNAME "x86_64"
+#elif defined (__arm64__)
+#define ARCHNAME "arm64"
+#elif defined (__arm__)
+
+#if defined (__ARM_ARCH_7S__)
+#define ARCHNAME "armv7s"
+#elif defined (__ARM_ARCH_7F__)
+#define ARCHNAME "armv7f"
+#elif defined (__ARM_ARCH_7K__)
+#define ARCHNAME "armv7k"
+#elif defined (_ARM_ARCH_7) /* umbrella for all remaining */
+#define ARCHNAME "armv7"
+#elif defined (_ARM_ARCH_6) /* umbrella for all armv6 */
+#define ARCHNAME "armv6"
+#endif
+
+#elif defined (__arm64__)
+#define ARCHNAME "arm64"
 #else
 #error architecture not supported
 #endif
@@ -4198,7 +4387,7 @@ OSKext::loadKextWithIdentifier(
         startOpt, startMatchingOpt, personalityNames);
         
 finish:
-    OSSafeRelease(kextIdentifier);
+    OSSafeReleaseNULL(kextIdentifier);
     return result;
 }
 
@@ -4319,8 +4508,8 @@ OSKext::loadKextWithIdentifier(
     }
 
 finish:
-    OSSafeRelease(loadRequest);
-    OSSafeRelease(kextIdentifierSymbol);
+    OSSafeReleaseNULL(loadRequest);
+    OSSafeReleaseNULL(kextIdentifierSymbol);
     
     IORecursiveLockUnlock(sKextLock);
 
@@ -4348,6 +4537,7 @@ OSKext::recordIdentifierRequest(
         goto finish;
     }
 
+    IORecursiveLockLock(sKextLock);
     if (!sAllKextLoadIdentifiers->containsObject(kextIdentifierSymbol)) {
         if (!sAllKextLoadIdentifiers->setObject(kextIdentifierSymbol)) {
             fail = true;
@@ -4361,6 +4551,8 @@ OSKext::recordIdentifierRequest(
                 kextIdentifier->getCStringNoCopy());
         }
     }
+    IORecursiveLockUnlock(sKextLock);
+
 finish:
 
     if (fail) {
@@ -4370,7 +4562,7 @@ finish:
             "Failed to record kext %s as a candidate for inclusion in prelinked kernel.",
             kextIdentifier->getCStringNoCopy());
     }
-    OSSafeRelease(kextIdentifierSymbol);
+    OSSafeReleaseNULL(kextIdentifierSymbol);
     return;
 }
 
@@ -4577,7 +4769,9 @@ OSKext::load(
     }
     bzero(account, sizeof(*account));
     account->loadTag = kmod_info->id;
+    account->site.refcount = 0;
     account->site.flags = VM_TAG_KMOD;
+    account->kext = this;
 
     flags.loaded = true;
 
@@ -4758,24 +4952,25 @@ OSKext::lookupSection(const char *segname, const char *secname)
 *********************************************************************/
 
 OSReturn
-OSKext::slidePrelinkedExecutable()
-{
-    OSReturn                   result           = kOSKextReturnBadData;
-    kernel_mach_header_t     * mh               = NULL;
-    kernel_segment_command_t * seg              = NULL;
-    kernel_segment_command_t * linkeditSeg      = NULL;
-    kernel_section_t         * sec              = NULL;
-    char                     * linkeditBase     = NULL;
-    bool                       haveLinkeditBase = false;
-    char                     * relocBase        = NULL;
-    bool                       haveRelocBase    = false;
-    struct dysymtab_command  * dysymtab         = NULL;
-    struct symtab_command    * symtab           = NULL;
-    kernel_nlist_t           * sym              = NULL;
-    struct relocation_info   * reloc            = NULL;
-    uint32_t                   i                = 0; 
-    int                        reloc_size;
-    vm_offset_t                new_kextsize;
+OSKext::slidePrelinkedExecutable(bool doCoalesedSlides)
+{
+    OSReturn                       result           = kOSKextReturnBadData;
+    kernel_mach_header_t         * mh               = NULL;
+    kernel_segment_command_t     * seg              = NULL;
+    kernel_segment_command_t     * linkeditSeg      = NULL;
+    kernel_section_t             * sec              = NULL;
+    char                         * linkeditBase     = NULL;
+    bool                           haveLinkeditBase = false;
+    char                         * relocBase        = NULL;
+    bool                           haveRelocBase    = false;
+    struct dysymtab_command      * dysymtab         = NULL;
+    struct linkedit_data_command * segmentSplitInfo = NULL;
+    struct symtab_command        * symtab           = NULL;
+    kernel_nlist_t               * sym              = NULL;
+    struct relocation_info       * reloc            = NULL;
+    uint32_t                       i                = 0;
+    int                            reloc_size;
+    vm_offset_t                    new_kextsize;
 
     if (linkedExecutable == NULL || vm_kernel_slide == 0) {
         result = kOSReturnSuccess;
@@ -4783,6 +4978,7 @@ OSKext::slidePrelinkedExecutable()
     }
 
     mh = (kernel_mach_header_t *)linkedExecutable->getBytesNoCopy();
+    segmentSplitInfo = (struct linkedit_data_command *) getcommandfromheader(mh, LC_SEGMENT_SPLIT_INFO);
 
     for (seg = firstsegfromheader(mh); seg != NULL; seg = nextsegfromheader(mh, seg)) {
         if (!seg->vmaddr) {
@@ -4791,7 +4987,7 @@ OSKext::slidePrelinkedExecutable()
         seg->vmaddr += vm_kernel_slide;
                 
 #if KASLR_KEXT_DEBUG
-        IOLog("kaslr: segname %s unslid 0x%lx slid 0x%lx \n", 
+        IOLog("kaslr: segname %s unslid 0x%lx slid 0x%lx \n",
               seg->segname,
               (unsigned long)VM_KERNEL_UNSLIDE(seg->vmaddr), 
               (unsigned long)seg->vmaddr);
@@ -4810,7 +5006,7 @@ OSKext::slidePrelinkedExecutable()
             sec->addr += vm_kernel_slide;
 
 #if KASLR_KEXT_DEBUG
-            IOLog("kaslr: sectname %s unslid 0x%lx slid 0x%lx \n", 
+            IOLog("kaslr: sectname %s unslid 0x%lx slid 0x%lx \n",
                   sec->sectname,
                   (unsigned long)VM_KERNEL_UNSLIDE(sec->addr), 
                   (unsigned long)sec->addr);
@@ -4822,7 +5018,7 @@ OSKext::slidePrelinkedExecutable()
 
     symtab = (struct symtab_command *) getcommandfromheader(mh, LC_SYMTAB);
 
-    if (symtab != NULL) {
+    if (symtab != NULL && doCoalesedSlides == false) {
       /* Some pseudo-kexts have symbol tables without segments.
        * Ignore them. */
         if (symtab->nsyms > 0 && haveLinkeditBase) {
@@ -4844,8 +5040,8 @@ OSKext::slidePrelinkedExecutable()
             }
         }
     }
-
-    if (dysymtab != NULL) {
+    
+    if (dysymtab != NULL && doCoalesedSlides == false) {
         if (dysymtab->nextrel > 0) {
             OSKextLog(this,
                 kOSKextLogErrorLevel | kOSKextLogLoadFlag |
@@ -4910,10 +5106,11 @@ OSKext::slidePrelinkedExecutable()
 
             /* We should free these relocations, not just delete the reference to them.
              * <rdar://problem/10535549> Free relocations from PIE kexts.
+             *
+             * For now, we do not free LINKEDIT for kexts with split segments.
              */
             new_kextsize = round_page(kmod_info->size - reloc_size);
-            
-            if ((kmod_info->size - new_kextsize) > PAGE_SIZE) {
+            if (((kmod_info->size - new_kextsize) > PAGE_SIZE) && (!segmentSplitInfo)) {
                 vm_offset_t     endofkext = kmod_info->address + kmod_info->size;
                 vm_offset_t     new_endofkext = kmod_info->address + new_kextsize;
                 vm_offset_t     endofrelocInfo = (vm_offset_t) (((uint8_t *)reloc) + reloc_size);
@@ -5277,10 +5474,25 @@ register_kmod:
         goto finish;
     }
 
+#if KASAN
+    kasan_load_kext((vm_offset_t)linkedExecutable->getBytesNoCopy(),
+                    linkedExecutable->getLength(), getIdentifierCString());
+#else
+    if (lookupSection(KASAN_GLOBAL_SEGNAME, KASAN_GLOBAL_SECTNAME)) {
+        OSKextLog(this,
+                kOSKextLogErrorLevel | kOSKextLogLoadFlag,
+                "KASAN: cannot load KASAN-ified kext %s on a non-KASAN kernel\n",
+                getIdentifierCString()
+                );
+        result = KERN_FAILURE;
+        goto finish;
+    }
+#endif
+
     result = kOSReturnSuccess;
 
 finish:
-    OSSafeRelease(linkDependencies);
+    OSSafeReleaseNULL(linkDependencies);
 
    /* Clear up locally allocated dependency info.
     */
@@ -5552,7 +5764,40 @@ OSKext::unregisterWithDTrace(void)
 * called only by loadExecutable()
 *********************************************************************/
 #if !VM_MAPPED_KEXTS
+#if defined(__arm__) || defined(__arm64__)
+static inline kern_return_t
+OSKext_protect(
+    vm_map_t   map,
+    vm_map_offset_t    start,
+    vm_map_offset_t    end,
+    vm_prot_t  new_prot,
+    boolean_t  set_max)
+{
+#pragma unused(map)
+    assert(map == kernel_map); // we can handle KEXTs arising from the PRELINK segment and no others
+    assert(start <= end);
+    if (start >= end)
+        return KERN_SUCCESS; // Punt segments of length zero (e.g., headers) or less (i.e., blunders)
+    else if (set_max)
+        return KERN_SUCCESS; // Punt set_max, as there's no mechanism to record that state
+    else
+        return ml_static_protect(start, end - start, new_prot);
+}
+
+static inline kern_return_t
+OSKext_wire(
+    vm_map_t   map,
+    vm_map_offset_t    start,
+    vm_map_offset_t    end,
+    vm_prot_t  access_type,
+    boolean_t       user_wire)
+{
+#pragma unused(map,start,end,access_type,user_wire)
+       return KERN_SUCCESS; // No-op as PRELINK kexts are cemented into physical memory at boot
+}
+#else
 #error Unrecognized architecture 
+#endif
 #else
 static inline kern_return_t
 OSKext_protect(
@@ -5576,7 +5821,7 @@ OSKext_wire(
     vm_prot_t  access_type,
     boolean_t       user_wire)
 {
-       return vm_map_wire(map, start, end, access_type | VM_PROT_MEMORY_TAG_MAKE(VM_KERN_MEMORY_KEXT), user_wire);
+       return vm_map_wire_kernel(map, start, end, access_type, VM_KERN_MEMORY_KEXT, user_wire);
 }
 #endif
 
@@ -5601,6 +5846,20 @@ OSKext::setVMAttributes(bool protect, bool wire)
         goto finish;
     }
 
+#if !VM_MAPPED_KEXTS
+    if (getcommandfromheader((kernel_mach_header_t *)kmod_info->address, LC_SEGMENT_SPLIT_INFO)) {
+         /* This is a split kext in a prelinked kernelcache; we'll let the
+          * platform code take care of protecting it.  It is already wired.
+          */
+         /* TODO: Should this still allow protections for the first segment
+          * to go through, in the event that we have a mix of split and
+          * unsplit kexts?
+          */
+        result = KERN_SUCCESS;
+        goto finish;
+    }
+#endif
+
     /* Protect the headers as read-only; they do not need to be wired */
     result = (protect) ? OSKext_protect(kext_map, kmod_info->address, 
         kmod_info->address + kmod_info->hdr_size, VM_PROT_READ, TRUE)
@@ -5613,6 +5872,11 @@ OSKext::setVMAttributes(bool protect, bool wire)
     seg = firstsegfromheader((kernel_mach_header_t *)kmod_info->address);
     while (seg) {
 
+#if __arm__
+        /* We build all ARM kexts, so we can ensure they are aligned */
+        assert((seg->vmaddr & PAGE_MASK) == 0);
+        assert((seg->vmsize & PAGE_MASK) == 0);
+#endif
 
         start = round_page(seg->vmaddr);
         end = trunc_page(seg->vmaddr + seg->vmsize);
@@ -5718,22 +5982,52 @@ OSKext::validateKextMapping(bool startFlag)
 
    /* Verify that the start/stop function lies within the kext's address range.
     */
-    if (address < kmod_info->address + kmod_info->hdr_size ||
-        kmod_info->address + kmod_info->size <= address)
-    {
-        OSKextLog(this,
-            kOSKextLogErrorLevel |
-            kOSKextLogLoadFlag,
-            "Kext %s module %s pointer is outside of kext range "
-            "(%s %p - kext at %p-%p)..",
-            getIdentifierCString(),
-            whichOp,
-            whichOp,
-            (void *)VM_KERNEL_UNSLIDE(address),
-            (void *)VM_KERNEL_UNSLIDE(kmod_info->address),
-            (void *)(VM_KERNEL_UNSLIDE(kmod_info->address) + kmod_info->size));
-        result = kOSKextReturnBadData;
-        goto finish;
+    if (getcommandfromheader((kernel_mach_header_t *)kmod_info->address, LC_SEGMENT_SPLIT_INFO)) { 
+       /* This will likely be how we deal with split kexts; walk the segments to
+        * check that the function lies inside one of the segments of this kext.
+        */
+        for (seg = firstsegfromheader((kernel_mach_header_t *)kmod_info->address);
+             seg != NULL;
+             seg = nextsegfromheader((kernel_mach_header_t *)kmod_info->address, seg)) {
+            if ((address >= seg->vmaddr) && address < (seg->vmaddr + seg->vmsize)) {
+                break;
+            }
+        }
+
+        if (!seg) {
+            OSKextLog(this,
+                kOSKextLogErrorLevel |
+                kOSKextLogLoadFlag,
+                "Kext %s module %s pointer is outside of kext range "
+                "(%s %p - kext starts at %p).",
+                getIdentifierCString(),
+                whichOp,
+                whichOp,
+                (void *)VM_KERNEL_UNSLIDE(address),
+                (void *)VM_KERNEL_UNSLIDE(kmod_info->address));
+            result = kOSKextReturnBadData;
+            goto finish;
+        }
+
+        seg = NULL;
+    } else {
+        if (address < kmod_info->address + kmod_info->hdr_size ||
+            kmod_info->address + kmod_info->size <= address)
+        {
+            OSKextLog(this,
+                kOSKextLogErrorLevel |
+                kOSKextLogLoadFlag,
+                "Kext %s module %s pointer is outside of kext range "
+                "(%s %p - kext at %p-%p).",
+                getIdentifierCString(),
+                whichOp,
+                whichOp,
+                (void *)VM_KERNEL_UNSLIDE(address),
+                (void *)VM_KERNEL_UNSLIDE(kmod_info->address),
+                (void *)(VM_KERNEL_UNSLIDE(kmod_info->address) + kmod_info->size));
+            result = kOSKextReturnBadData;
+            goto finish;
+        }
     }
 
    /* Only do these checks before calling the start function;
@@ -5816,6 +6110,35 @@ OSKext::verifySegmentMapping(kernel_segment_command_t *seg)
     return true;
 }
 
+/*********************************************************************
+*********************************************************************/
+static void
+OSKextLogKextInfo(OSKext *aKext, uint64_t address, uint64_t size, firehose_tracepoint_code_t code)
+{
+
+    uint64_t                            stamp = 0;
+    firehose_tracepoint_id_u            trace_id;
+    struct firehose_trace_uuid_info_s   uuid_info_s;
+    firehose_trace_uuid_info_t          uuid_info = &uuid_info_s;
+    size_t                              uuid_info_len = sizeof(struct firehose_trace_uuid_info_s);
+    OSData                              *uuid_data;
+
+    stamp = firehose_tracepoint_time(firehose_activity_flags_default);
+    trace_id.ftid_value = FIREHOSE_TRACE_ID_MAKE(firehose_tracepoint_namespace_metadata, _firehose_tracepoint_type_metadata_kext, (firehose_tracepoint_flags_t)0, code);
+
+    uuid_data = aKext->copyUUID();
+    if (uuid_data) {
+        memcpy(uuid_info->ftui_uuid, uuid_data->getBytesNoCopy(), sizeof(uuid_info->ftui_uuid));
+        OSSafeReleaseNULL(uuid_data);
+    }
+
+    uuid_info->ftui_size    = size;
+    uuid_info->ftui_address = VM_KERNEL_UNSLIDE(address);
+
+    firehose_trace_metadata(firehose_stream_metadata, trace_id, stamp, uuid_info, uuid_info_len);
+    return;
+}
+
 /*********************************************************************
 *********************************************************************/
 OSReturn
@@ -5904,6 +6227,9 @@ OSKext::start(bool startDependenciesFlag)
 
     flags.starting = 1;
 
+    // Drop a log message so logd can grab the needed information to decode this kext
+    OSKextLogKextInfo(this, kmod_info->address, kmod_info->size, firehose_tracepoint_code_load);
+
 #if !CONFIG_STATIC_CPPINIT
     result = OSRuntimeInitializeCPP(kmod_info, NULL);
     if (result == KERN_SUCCESS) {
@@ -5922,7 +6248,6 @@ OSKext::start(bool startDependenciesFlag)
         }
 #endif
 #endif // CONFIG_KEC_FIPS 
-
         result = startfunc(kmod_info, kmodStartData);
 
 #if !CONFIG_STATIC_CPPINIT
@@ -6002,7 +6327,7 @@ OSKext::stop(void)
 {
     OSReturn result = kOSReturnError;
     kern_return_t (*stopfunc)(kmod_info_t *, void *);
-    
+
     if (!isStarted() || isInterface()) {
         result = kOSReturnSuccess;
         goto finish;
@@ -6090,6 +6415,8 @@ OSKext::stop(void)
     }
 
 finish:
+    // Drop a log message so logd can update this kext's metadata
+    OSKextLogKextInfo(this, kmod_info->address, kmod_info->size, firehose_tracepoint_code_unload);
     return result;
 }
 
@@ -6128,7 +6455,17 @@ OSKext::unload(void)
         goto finish;
     }
 
-    if (hasOSMetaClassInstances()) {
+    if (!isLoaded()) {
+        result = kOSReturnSuccess;
+        goto finish;
+    }
+
+    if (isKernelComponent()) {
+        result = kOSKextReturnInvalidArgument;
+        goto finish;
+    }
+
+    if (metaClasses && !OSMetaClass::removeClasses(metaClasses)) {
         OSKextLog(this,
             kOSKextLogErrorLevel |
             kOSKextLogLoadFlag | kOSKextLogKextBookkeepingFlag,
@@ -6139,16 +6476,6 @@ OSKext::unload(void)
         result = kOSKextReturnInUse;
         goto finish;
     }
-
-    if (!isLoaded()) {
-        result = kOSReturnSuccess;
-        goto finish;
-    }
-
-    if (isKernelComponent()) {
-        result = kOSKextReturnInvalidArgument;
-        goto finish;
-    }
     
    /* Note that the kext is unloading before running any code that
     * might be in the kext (request callbacks, module stop function).
@@ -6261,6 +6588,7 @@ OSKext::unload(void)
 
     freeAccount = NULL;
     IOSimpleLockLock(sKextAccountsLock);
+    account->kext = NULL;
     if (account->site.tag) account->site.flags |= VM_TAG_UNLOAD;
     else                   freeAccount = account;
     IOSimpleLockUnlock(sKextAccountsLock);
@@ -6269,6 +6597,10 @@ OSKext::unload(void)
     /* Unwire and free the linked executable.
      */
     if (linkedExecutable) {
+#if KASAN
+        kasan_unload_kext((vm_offset_t)linkedExecutable->getBytesNoCopy(), linkedExecutable->getLength());
+#endif
+
 #if VM_MAPPED_KEXTS
         if (!isInterface()) {
             kernel_segment_command_t *seg = NULL;
@@ -6400,7 +6732,7 @@ OSKext::queueKextNotification(
     result = kOSReturnSuccess;
 
 finish:
-    OSSafeRelease(loadRequest);
+    OSSafeReleaseNULL(loadRequest);
 
     return result;
 }
@@ -6676,6 +7008,7 @@ finish:
 *********************************************************************/
 extern "C" {
 
+IOReturn OSKextSystemSleepOrWake(UInt32 messageType);
 IOReturn OSKextSystemSleepOrWake(UInt32 messageType)
 {
     IORecursiveLockLock(sKextInnerLock);
@@ -6824,8 +7157,8 @@ OSKext::considerRebuildOfPrelinkedKernel(void)
     
 finish:
     IORecursiveLockUnlock(sKextLock);
-    OSSafeRelease(prelinkRequest);
-    OSSafeRelease(kextIterator);
+    OSSafeReleaseNULL(prelinkRequest);
+    OSSafeReleaseNULL(kextIterator);
     
     return;
 }
@@ -7225,8 +7558,8 @@ finish:
             getIdentifierCString());
     }
 
-    OSSafeRelease(localLoopStack);
-    OSSafeRelease(libraryIterator);
+    OSSafeReleaseNULL(localLoopStack);
+    OSSafeReleaseNULL(libraryIterator);
 
     return result;
 }
@@ -7476,7 +7809,7 @@ OSKext::hasOSMetaClassInstances(void)
 
 finish:
     
-    OSSafeRelease(classIterator);
+    OSSafeReleaseNULL(classIterator);
     return result;
 }
 
@@ -7497,7 +7830,7 @@ OSKext::reportOSMetaClassInstances(
     
     theKext->reportOSMetaClassInstances(msgLogSpec);
 finish:
-    OSSafeRelease(theKext);
+    OSSafeReleaseNULL(theKext);
     return;
 }
 
@@ -7530,7 +7863,7 @@ OSKext::reportOSMetaClassInstances(OSKextLogSpec msgLogSpec)
     }
 
 finish:
-    OSSafeRelease(classIterator);
+    OSSafeReleaseNULL(classIterator);
     return;
 }
 
@@ -7721,7 +8054,22 @@ OSKext::handleRequest(
     } else if (predicate->isEqualTo(kKextRequestPredicateSendResource)) {
         result = OSKext::dispatchResource(requestDict);
 
-    } else if (predicate->isEqualTo(kKextRequestPredicateGetLoaded)) {
+    } else if (predicate->isEqualTo(kKextRequestPredicateGetUUIDByAddress)) {
+
+         OSNumber     *lookupNum   = NULL;
+         lookupNum = OSDynamicCast(OSNumber,
+              _OSKextGetRequestArgument(requestDict,
+                  kKextRequestArgumentLookupAddressKey));
+
+         responseObject = OSKext::copyKextUUIDForAddress(lookupNum);
+         if (responseObject) {
+             result = kOSReturnSuccess;
+         } else {
+             goto finish;
+         }
+
+    } else if (predicate->isEqualTo(kKextRequestPredicateGetLoaded) ||
+               predicate->isEqualTo(kKextRequestPredicateGetLoadedByUUID)) {
         OSBoolean    * delayAutounloadBool = NULL;
         OSObject     * infoKeysRaw         = NULL;
         OSArray      * infoKeys            = NULL;
@@ -7762,7 +8110,12 @@ OSKext::handleRequest(
             }
         }
 
-        responseObject = OSKext::copyLoadedKextInfo(kextIdentifiers, infoKeys);
+        if (predicate->isEqualTo(kKextRequestPredicateGetLoaded)) {
+             responseObject = OSKext::copyLoadedKextInfo(kextIdentifiers, infoKeys);
+        }
+        else if (predicate->isEqualTo(kKextRequestPredicateGetLoadedByUUID)) {
+             responseObject = OSKext::copyLoadedKextInfoByUUID(kextIdentifiers, infoKeys);
+        }
         if (!responseObject) {
             result = kOSKextReturnInternalError;
         } else {
@@ -7882,11 +8235,11 @@ finish:
 
     IORecursiveLockUnlock(sKextLock);
 
-    OSSafeRelease(parsedXML);
-    OSSafeRelease(errorString);
-    OSSafeRelease(responseObject);
-    OSSafeRelease(serializer);
-    OSSafeRelease(logInfoArray);
+    OSSafeReleaseNULL(parsedXML);
+    OSSafeReleaseNULL(errorString);
+    OSSafeReleaseNULL(responseObject);
+    OSSafeReleaseNULL(serializer);
+    OSSafeReleaseNULL(logInfoArray);
 
     return result;
 }
@@ -7951,7 +8304,7 @@ void OSKextPgoMetadataPutAll(OSKext *kext,
                              size_t bufferSize,
                              uint32_t *num_pairs)
 {
-    assert_static(sizeof(clock_sec_t) % 2 == 0);
+    _static_assert_1_arg(sizeof(clock_sec_t) % 2 == 0);
     //log_10 2^16 â‰ˆ 4.82
     const size_t max_secs_string_size = 5 * sizeof(clock_sec_t)/2;
     const size_t max_timestamp_string_size = max_secs_string_size + 1 + 6;
@@ -7972,7 +8325,7 @@ void OSKextPgoMetadataPutAll(OSKext *kext,
         uuid_data = kext->copyUUID();
         if (uuid_data) {
             memcpy(uuid, uuid_data->getBytesNoCopy(), sizeof(uuid));
-            OSSafeRelease(uuid_data);
+            OSSafeReleaseNULL(uuid_data);
             uuid_unparse(uuid, uuid_string);
             OSKextPgoMetadataPut(pBuffer, position, bufferSize, num_pairs,
                                  "UUID", uuid_string);
@@ -7983,7 +8336,7 @@ void OSKextPgoMetadataPutAll(OSKext *kext,
         clock_get_calendar_microtime(&secs, &usecs);
         assert(usecs < 1000000);
         char timestamp[max_timestamp_string_size + 1];
-        assert_static(sizeof(long) >= sizeof(clock_sec_t));
+        _static_assert_1_arg(sizeof(long) >= sizeof(clock_sec_t));
         snprintf(timestamp, sizeof(timestamp), "%lu.%06d", (unsigned long)secs, (int)usecs);
         OSKextPgoMetadataPut(pBuffer, position, bufferSize, num_pairs,
                              "TIMESTAMP", timestamp);
@@ -8008,7 +8361,6 @@ size_t OSKextPgoMetadataSize(OSKext *kext)
     return position;
 }
 
-
 int OSKextGrabPgoDataLocked(OSKext *kext,
                             bool metadata,
                             uuid_t instance_uuid,
@@ -8016,7 +8368,6 @@ int OSKextGrabPgoDataLocked(OSKext *kext,
                             char *pBuffer,
                             uint64_t bufferSize)
 {
-
     int err = 0;
 
     kernel_section_t *sect_prf_data = NULL;
@@ -8158,6 +8509,154 @@ OSKextGrabPgoData(uuid_t uuid,
     return err;
 }
 
+void
+OSKextResetPgoCountersLock()
+{
+    IORecursiveLockLock(sKextLock);
+}
+
+void
+OSKextResetPgoCountersUnlock()
+{
+    IORecursiveLockUnlock(sKextLock);
+}
+
+
+extern unsigned int not_in_kdp;
+
+void
+OSKextResetPgoCounters()
+{
+    assert(!not_in_kdp);
+    uint32_t count = sLoadedKexts->getCount();
+    for (uint32_t i = 0; i < count; i++) {
+        OSKext *kext = OSDynamicCast(OSKext, sLoadedKexts->getObject(i));
+        kernel_section_t *sect_prf_cnts = kext->lookupSection("__DATA", "__llvm_prf_cnts");
+        if (!sect_prf_cnts) {
+            continue;
+        }
+        memset((void*)sect_prf_cnts->addr, 0, sect_prf_cnts->size);
+    }
+}
+
+OSDictionary *
+OSKext::copyLoadedKextInfoByUUID(
+    OSArray * kextIdentifiers,
+    OSArray * infoKeys)
+{
+    OSDictionary * result = NULL;
+    OSDictionary * kextInfo = NULL;  // must release
+    uint32_t       count, i;
+    uint32_t       idCount = 0;
+    uint32_t       idIndex = 0;
+
+    IORecursiveLockLock(sKextLock);
+
+#if CONFIG_MACF
+    /* Is the calling process allowed to query kext info? */
+    if (current_task() != kernel_task) {
+        int                 macCheckResult      = 0;
+        kauth_cred_t        cred                = NULL;
+
+        cred = kauth_cred_get_with_ref();
+        macCheckResult = mac_kext_check_query(cred);
+        kauth_cred_unref(&cred);
+
+        if (macCheckResult != 0) {
+            OSKextLog(/* kext */ NULL,
+                      kOSKextLogErrorLevel | kOSKextLogLoadFlag,
+                      "Failed to query kext info (MAC policy error 0x%x).",
+                      macCheckResult);
+            goto finish;
+        }
+   }
+#endif
+
+   /* Empty list of UUIDs is equivalent to no list (get all).
+    */
+    if (kextIdentifiers && !kextIdentifiers->getCount()) {
+        kextIdentifiers = NULL;
+    } else if (kextIdentifiers) {
+        idCount = kextIdentifiers->getCount();
+    }
+
+   /* Same for keys.
+    */
+    if (infoKeys && !infoKeys->getCount()) {
+        infoKeys = NULL;
+    }
+
+    count = sLoadedKexts->getCount();
+    result = OSDictionary::withCapacity(count);
+    if (!result) {
+        goto finish;
+    }
+
+    for (i = 0; i < count; i++) {
+        OSKext       *thisKext     = NULL;  // do not release
+        Boolean       includeThis  = true;
+        uuid_t        thisKextUUID;
+        OSData       *uuid_data;
+        uuid_string_t uuid_key;
+
+        if (kextInfo) {
+            kextInfo->release();
+            kextInfo = NULL;
+        }
+
+        thisKext = OSDynamicCast(OSKext, sLoadedKexts->getObject(i));
+        if (!thisKext) {
+            continue;
+        }
+
+        uuid_data = thisKext->copyUUID();
+        if (!uuid_data) {
+            continue;
+        }
+
+       memcpy(&thisKextUUID, uuid_data->getBytesNoCopy(), sizeof(thisKextUUID));
+       OSSafeReleaseNULL(uuid_data);
+
+       uuid_unparse(thisKextUUID, uuid_key);
+
+       /* Skip current kext if we have a list of UUIDs and
+        * it isn't in the list.
+        */
+        if (kextIdentifiers) {
+            includeThis = false;
+
+            for (idIndex = 0; idIndex < idCount; idIndex++) {
+                const OSString* wantedUUID = OSDynamicCast(OSString,
+                    kextIdentifiers->getObject(idIndex));
+
+                uuid_t uuid;
+                uuid_parse(wantedUUID->getCStringNoCopy(), uuid);
+
+                if (0 == uuid_compare(uuid, thisKextUUID)) {
+                    includeThis = true;
+                    break;
+                }
+
+            }
+        }
+
+        if (!includeThis) {
+            continue;
+        }
+
+        kextInfo = thisKext->copyInfo(infoKeys);
+        if (kextInfo) {
+            result->setObject(uuid_key, kextInfo);
+        }
+    }
+
+finish:
+    IORecursiveLockUnlock(sKextLock);
+
+    if (kextInfo) kextInfo->release();
+
+    return result;
+}
 
 /*********************************************************************
 *********************************************************************/
@@ -8315,10 +8814,11 @@ OSKext::copyInfo(OSArray * infoKeys)
     OSDictionary         * result                      = NULL;
     bool                   success                     = false;
     OSData               * headerData                  = NULL;  // must release
+    OSData               * logData                     = NULL;  // must release
     OSNumber             * cpuTypeNumber               = NULL;  // must release
     OSNumber             * cpuSubtypeNumber            = NULL;  // must release
     OSString             * versionString               = NULL;  // do not release
-    uint32_t               executablePathCStringSize = 0;
+    uint32_t               executablePathCStringSize   = 0;
     char                 * executablePathCString       = NULL;  // must release
     OSString             * executablePathString        = NULL;  // must release
     OSData               * uuid                        = NULL;  // must release
@@ -8348,6 +8848,7 @@ OSKext::copyInfo(OSArray * infoKeys)
     */
     if (!infoKeys ||
         _OSArrayContainsCString(infoKeys, kOSBundleMachOHeadersKey) ||
+        _OSArrayContainsCString(infoKeys, kOSBundleLogStringsKey) ||
         _OSArrayContainsCString(infoKeys, kOSBundleCPUTypeKey) ||
         _OSArrayContainsCString(infoKeys, kOSBundleCPUSubtypeKey))
     {
@@ -8391,6 +8892,7 @@ OSKext::copyInfo(OSArray * infoKeys)
                                 segp->filesize = 0;
                             }
                         }
+
 #if 0
                         OSKextLog(/* kext */ NULL,
                                   kOSKextLogErrorLevel |
@@ -8412,7 +8914,7 @@ OSKext::copyInfo(OSArray * infoKeys)
                         }
 #endif
                         segp->vmaddr = VM_KERNEL_UNSLIDE(segp->vmaddr);
-                        
+
                         for (secp = firstsect(segp); secp != NULL; secp = nextsect(segp, secp)) {
                             secp->addr = VM_KERNEL_UNSLIDE(secp->addr);
                         }
@@ -8423,6 +8925,55 @@ OSKext::copyInfo(OSArray * infoKeys)
             }
 #endif // SECURE_KERNEL
 
+            if (_OSArrayContainsCString(infoKeys, kOSBundleLogStringsKey)) {
+                 osLogDataHeaderRef *header;
+                 char headerBytes[offsetof(osLogDataHeaderRef, sections) + NUM_OS_LOG_SECTIONS * sizeof(header->sections[0])];
+
+                 void *os_log_data          = NULL;
+                 void *cstring_data         = NULL;
+                 unsigned long os_log_size  = 0;
+                 unsigned long cstring_size = 0;
+                 uint32_t os_log_offset     = 0;
+                 uint32_t cstring_offset    = 0;
+                 bool res;
+
+                 os_log_data       = getsectdatafromheader(kext_mach_hdr, "__TEXT", "__os_log", &os_log_size);
+                 os_log_offset     = getsectoffsetfromheader(kext_mach_hdr, "__TEXT", "__os_log");
+                 cstring_data      = getsectdatafromheader(kext_mach_hdr, "__TEXT", "__cstring", &cstring_size);
+                 cstring_offset    = getsectoffsetfromheader(kext_mach_hdr, "__TEXT", "__cstring");
+
+                 header             = (osLogDataHeaderRef *) headerBytes;
+                 header->version    = OS_LOG_HDR_VERSION;
+                 header->sect_count = NUM_OS_LOG_SECTIONS;
+                 header->sections[OS_LOG_SECT_IDX].sect_offset  = os_log_offset;
+                 header->sections[OS_LOG_SECT_IDX].sect_size    = (uint32_t) os_log_size;
+                 header->sections[CSTRING_SECT_IDX].sect_offset = cstring_offset;
+                 header->sections[CSTRING_SECT_IDX].sect_size   = (uint32_t) cstring_size;
+
+
+                 logData = OSData::withBytes(header, (u_int) (sizeof(osLogDataHeaderRef)));
+                 if (!logData) {
+                      goto finish;
+                 }
+                 res = logData->appendBytes(&(header->sections[0]), (u_int)(header->sect_count * sizeof(header->sections[0])));
+                 if (!res) {
+                      goto finish;
+                 }
+                 if (os_log_data) {
+                      res = logData->appendBytes(os_log_data, (u_int)header->sections[OS_LOG_SECT_IDX].sect_size);
+                      if (!res) {
+                           goto finish;
+                      }
+                 }
+                 if (cstring_data) {
+                      res = logData->appendBytes(cstring_data, (u_int)header->sections[CSTRING_SECT_IDX].sect_size);
+                      if (!res) {
+                           goto finish;
+                      }
+                 }
+                 result->setObject(kOSBundleLogStringsKey, logData);
+            }
+
             if (!infoKeys || _OSArrayContainsCString(infoKeys, kOSBundleCPUTypeKey)) {
                 cpuTypeNumber = OSNumber::withNumber(
                     (uint64_t) kext_mach_hdr->cputype,
@@ -8502,7 +9053,7 @@ OSKext::copyInfo(OSArray * infoKeys)
 
             executablePathString = OSString::withCString(executablePathCString);
 
-            if (!executablePathCString) {
+            if (!executablePathString) {
                 goto finish;
             }
 
@@ -8559,15 +9110,19 @@ OSKext::copyInfo(OSArray * infoKeys)
     if (!infoKeys ||
         _OSArrayContainsCString(infoKeys, kOSBundleLoadAddressKey) ||
         _OSArrayContainsCString(infoKeys, kOSBundleLoadSizeKey) ||
+        _OSArrayContainsCString(infoKeys, kOSBundleExecLoadAddressKey) ||
+        _OSArrayContainsCString(infoKeys, kOSBundleExecLoadSizeKey) ||
         _OSArrayContainsCString(infoKeys, kOSBundleWiredSizeKey))
     {
         if (isInterface() || linkedExecutable) {
            /* These go to userspace via serialization, so we don't want any doubts
             * about their size.
             */
-            uint64_t    loadAddress = 0;
-            uint32_t    loadSize    = 0;
-            uint32_t    wiredSize   = 0;
+            uint64_t    loadAddress     = 0;
+            uint32_t    loadSize        = 0;
+            uint32_t    wiredSize       = 0;
+            uint64_t    execLoadAddress = 0;
+            uint32_t    execLoadSize    = 0;
 
            /* Interfaces always report 0 load address & size.
             * Just the way they roll.
@@ -8576,10 +9131,25 @@ OSKext::copyInfo(OSArray * infoKeys)
             * xxx - shouldn't have one!
             */
             if (linkedExecutable /* && !isInterface() */) {
+                kernel_mach_header_t     *mh  = NULL;
+                kernel_segment_command_t *seg = NULL;
+
                 loadAddress = (uint64_t)linkedExecutable->getBytesNoCopy();
+                mh = (kernel_mach_header_t *)loadAddress;
                 loadAddress = VM_KERNEL_UNSLIDE(loadAddress);
                 loadSize = linkedExecutable->getLength();
-                
+
+               /* Walk through the kext, looking for the first executable
+                * segment in case we were asked for its size/address.
+                */
+                for (seg = firstsegfromheader(mh); seg != NULL; seg = nextsegfromheader(mh, seg)) {
+                    if (seg->initprot & VM_PROT_EXECUTE) {
+                        execLoadAddress = VM_KERNEL_UNSLIDE(seg->vmaddr);
+                        execLoadSize = seg->vmsize;
+                        break;
+                    }
+                }
+
                /* If we have a kmod_info struct, calculated the wired size
                 * from that. Otherwise it's the full load size.
                 */
@@ -8600,6 +9170,16 @@ OSKext::copyInfo(OSArray * infoKeys)
                 result->setObject(kOSBundleLoadAddressKey, scratchNumber);
                 OSSafeReleaseNULL(scratchNumber);
             }
+            if (!infoKeys || _OSArrayContainsCString(infoKeys, kOSBundleExecLoadAddressKey)) {
+                scratchNumber = OSNumber::withNumber(
+                    (unsigned long long)(execLoadAddress),
+                    /* numBits */ 8 * sizeof(execLoadAddress));
+                if (!scratchNumber) {
+                    goto finish;
+                }
+                result->setObject(kOSBundleExecLoadAddressKey, scratchNumber);
+                OSSafeReleaseNULL(scratchNumber);
+            }
             if (!infoKeys || _OSArrayContainsCString(infoKeys, kOSBundleLoadSizeKey)) {
                 scratchNumber = OSNumber::withNumber(
                     (unsigned long long)(loadSize),
@@ -8610,6 +9190,16 @@ OSKext::copyInfo(OSArray * infoKeys)
                 result->setObject(kOSBundleLoadSizeKey, scratchNumber);
                 OSSafeReleaseNULL(scratchNumber);
             }
+            if (!infoKeys || _OSArrayContainsCString(infoKeys, kOSBundleExecLoadSizeKey)) {
+                scratchNumber = OSNumber::withNumber(
+                    (unsigned long long)(execLoadSize),
+                    /* numBits */ 8 * sizeof(execLoadSize));
+                if (!scratchNumber) {
+                    goto finish;
+                }
+                result->setObject(kOSBundleExecLoadSizeKey, scratchNumber);
+                OSSafeReleaseNULL(scratchNumber);
+            }
             if (!infoKeys || _OSArrayContainsCString(infoKeys, kOSBundleWiredSizeKey)) {
                 scratchNumber = OSNumber::withNumber(
                     (unsigned long long)(wiredSize),
@@ -8724,19 +9314,20 @@ OSKext::copyInfo(OSArray * infoKeys)
     success = true;
 
 finish:
-    OSSafeRelease(headerData);
-    OSSafeRelease(cpuTypeNumber);
-    OSSafeRelease(cpuSubtypeNumber);
-    OSSafeRelease(executablePathString);
-    if (executablePathString) kfree(executablePathCString, executablePathCStringSize);
-    OSSafeRelease(uuid);
-    OSSafeRelease(scratchNumber);
-    OSSafeRelease(dependencyLoadTags);
-    OSSafeRelease(metaClassIterator);
-    OSSafeRelease(metaClassInfo);
-    OSSafeRelease(metaClassDict);
-    OSSafeRelease(metaClassName);
-    OSSafeRelease(superclassName);
+    OSSafeReleaseNULL(headerData);
+    OSSafeReleaseNULL(logData);
+    OSSafeReleaseNULL(cpuTypeNumber);
+    OSSafeReleaseNULL(cpuSubtypeNumber);
+    OSSafeReleaseNULL(executablePathString);
+    if (executablePathCString) kfree(executablePathCString, executablePathCStringSize);
+    OSSafeReleaseNULL(uuid);
+    OSSafeReleaseNULL(scratchNumber);
+    OSSafeReleaseNULL(dependencyLoadTags);
+    OSSafeReleaseNULL(metaClassIterator);
+    OSSafeReleaseNULL(metaClassInfo);
+    OSSafeReleaseNULL(metaClassDict);
+    OSSafeReleaseNULL(metaClassName);
+    OSSafeReleaseNULL(superclassName);
     if (!success) {
         OSSafeReleaseNULL(result);
     }
@@ -8951,7 +9542,7 @@ OSKext::dequeueCallbackForRequestTag(
         callbackRecordOut);
 
 finish:
-    OSSafeRelease(requestTagNum);
+    OSSafeReleaseNULL(requestTagNum);
 
     return result;
 }
@@ -9007,6 +9598,17 @@ finish:
     return result;
 }
 
+
+/*********************************************************************
+* Busy timeout triage
+*********************************************************************/
+/* static */
+bool
+OSKext::isWaitingKextd(void)
+{
+    return sRequestCallbackRecords && sRequestCallbackRecords->getCount();
+}
+
 /*********************************************************************
 * Assumes sKextLock is held.
 *********************************************************************/
@@ -9901,30 +10503,21 @@ inline const char * colorForFlags(OSKextLogSpec flags)
     switch (logLevel) {
     case kOSKextLogErrorLevel:
         return VTRED VTBOLD;
-        break;
     case kOSKextLogWarningLevel:
         return VTRED;
-        break;
     case kOSKextLogBasicLevel:
         return VTYELLOW VTUNDER;
-        break;
     case kOSKextLogProgressLevel:
         return VTYELLOW;
-        break;
     case kOSKextLogStepLevel:
         return VTGREEN;
-        break;
     case kOSKextLogDetailLevel:
         return VTCYAN;
-        break;
     case kOSKextLogDebugLevel:
         return VTMAGENTA;
-        break;
     default:
         return "";  // white
-        break;
     }
-    return "";
 }
 
 inline bool logSpecMatch(
@@ -10077,8 +10670,8 @@ finish:
     if (allocBuffer) {
         kfree(allocBuffer, (length + 1) * sizeof(char));
     }
-    OSSafeRelease(logString);
-    OSSafeRelease(logSpecNum);
+    OSSafeReleaseNULL(logString);
+    OSSafeReleaseNULL(logSpecNum);
     return;
 }
 
@@ -10285,15 +10878,14 @@ OSKext::printKextsInBacktrace(
     vm_offset_t  * addr,
     unsigned int   cnt,
     int         (* printf_func)(const char *fmt, ...),
-    bool           lockFlag,
-    bool           doUnslide)
+    uint32_t       flags)
 {
     addr64_t    summary_page = 0;
     addr64_t    last_summary_page = 0;
     bool        found_kmod = false;
     u_int       i = 0;
 
-    if (lockFlag) {
+    if (kPrintKextsLock & flags) {
         if (!sKextSummariesLock) return;
         IOLockLock(sKextSummariesLock);
     }
@@ -10326,15 +10918,17 @@ OSKext::printKextsInBacktrace(
         }
         
         if (!found_kmod) {
-            (*printf_func)("      Kernel Extensions in backtrace:\n");
+            if (!(kPrintKextsTerse & flags)) {
+                (*printf_func)("      Kernel Extensions in backtrace:\n");
+            }
             found_kmod = true;
         }
 
-        printSummary(summary, printf_func, doUnslide);
+        printSummary(summary, printf_func, flags);
     }
 
 finish:
-    if (lockFlag) {
+    if (kPrintKextsLock & flags) {
         IOLockUnlock(sKextSummariesLock);
     }
 
@@ -10365,6 +10959,69 @@ OSKext::summaryIsInBacktrace(
     return FALSE;
 }
 
+/*
+ * Get the kext summary object for the kext where 'addr' lies. Must be called with
+ * sKextSummariesLock held.
+ */
+OSKextLoadedKextSummary *
+OSKext::summaryForAddress(const uintptr_t addr)
+{
+       for (unsigned i = 0; i < gLoadedKextSummaries->numSummaries; ++i) {
+
+               OSKextLoadedKextSummary *summary = &gLoadedKextSummaries->summaries[i];
+               if (!summary->address) {
+                       continue;
+               }
+
+#if VM_MAPPED_KEXTS
+               /* On our platforms that use VM_MAPPED_KEXTS, we currently do not
+                * support split kexts, but we also may unmap the kexts, which can
+                * race with the above codepath (see OSKext::unload).  As such,
+                * use a simple range lookup if we are using VM_MAPPED_KEXTS.
+                */
+               if ((addr >= summary->address) && (addr < (summary->address + summary->size))) {
+                       return summary;
+               }
+#else
+               kernel_mach_header_t *mh = (kernel_mach_header_t *)summary->address;
+               kernel_segment_command_t *seg;
+
+               for (seg = firstsegfromheader(mh); seg != NULL; seg = nextsegfromheader(mh, seg)) {
+                       if ((addr >= seg->vmaddr) && (addr < (seg->vmaddr + seg->vmsize))) {
+                               return summary;
+                       }
+               }
+#endif
+       }
+
+       /* addr did not map to any kext */
+       return NULL;
+}
+
+/* static */
+void *
+OSKext::kextForAddress(const void *addr)
+{
+       void *image = NULL;
+
+       if (((vm_offset_t)(uintptr_t)addr >= vm_kernel_stext) &&
+                       ((vm_offset_t)(uintptr_t)addr < vm_kernel_etext)) {
+               return (void *)&_mh_execute_header;
+       }
+
+       if (!sKextSummariesLock) {
+               return NULL;
+       }
+       IOLockLock(sKextSummariesLock);
+       OSKextLoadedKextSummary *summary = OSKext::summaryForAddress((uintptr_t)addr);
+       if (summary) {
+               image = (void *)summary->address;
+       }
+       IOLockUnlock(sKextSummariesLock);
+
+       return image;
+}
+
 /*********************************************************************
  * scan list of loaded kext summaries looking for a load address match and if
  * found return the UUID C string.  If not found then set empty string.
@@ -10400,7 +11057,7 @@ static void findSummaryUUID(
 void OSKext::printSummary(
     OSKextLoadedKextSummary * summary,
     int                    (* printf_func)(const char *fmt, ...),
-    bool                      doUnslide)
+    uint32_t                  flags)
 {
     kmod_reference_t * kmod_ref = NULL;
     uuid_string_t uuid;
@@ -10412,15 +11069,18 @@ void OSKext::printSummary(
     }
     (void) uuid_unparse(summary->uuid, uuid);
     
-    if (doUnslide) {
+    if (kPrintKextsUnslide & flags) {
         tmpAddr = VM_KERNEL_UNSLIDE(summary->address);
     }
     else {
         tmpAddr = summary->address;
     }
-    (*printf_func)("         %s(%s)[%s]@0x%llx->0x%llx\n",
+    (*printf_func)("%s%s(%s)[%s]@0x%llx->0x%llx\n",
+               (kPrintKextsTerse & flags) ? "" : "         ",
         summary->name, version, uuid,
         tmpAddr, tmpAddr + summary->size - 1);
+
+    if (kPrintKextsTerse & flags) return;
     
     /* print dependency info */
     for (kmod_ref = (kmod_reference_t *) summary->reference_list; 
@@ -10430,14 +11090,16 @@ void OSKext::printSummary(
         
         if (pmap_find_phys(kernel_pmap, (addr64_t)((uintptr_t)kmod_ref)) == 0) {
             (*printf_func)("            kmod dependency scan stopped "
-                           "due to missing dependency page: %p\n", kmod_ref);
+                           "due to missing dependency page: %p\n",
+                          (kPrintKextsUnslide & flags) ? (void *)VM_KERNEL_UNSLIDE(kmod_ref) : kmod_ref);
             break;
         }
         rinfo = kmod_ref->info;
         
         if (pmap_find_phys(kernel_pmap, (addr64_t)((uintptr_t)rinfo)) == 0) {
             (*printf_func)("            kmod dependency scan stopped "
-                           "due to missing kmod page: %p\n", rinfo);
+                           "due to missing kmod page: %p\n",
+                          (kPrintKextsUnslide & flags) ? (void *)VM_KERNEL_UNSLIDE(rinfo) : rinfo);
             break;
         }
         
@@ -10448,7 +11110,7 @@ void OSKext::printSummary(
         /* locate UUID in gLoadedKextSummaries */
         findSummaryUUID(rinfo->id, uuid);
         
-        if (doUnslide) {
+        if (kPrintKextsUnslide & flags) {
             tmpAddr = VM_KERNEL_UNSLIDE(rinfo->address);
         }
         else {
@@ -10892,15 +11554,12 @@ OSKext::updateLoadedKextSummaries(void)
     
     if (gLoadedKextSummaries == NULL || sLoadedKextSummariesAllocSize < size) {
         if (gLoadedKextSummaries) {
-            kmem_free(kernel_map,
-                      (vm_offset_t)gLoadedKextSummaries,
-                      sLoadedKextSummariesAllocSize);
+            kmem_free(kernel_map, (vm_offset_t)gLoadedKextSummaries, sLoadedKextSummariesAllocSize);
             gLoadedKextSummaries = NULL;
+            gLoadedKextSummariesTimestamp = mach_absolute_time();
             sLoadedKextSummariesAllocSize = 0;
         }
-        result = kmem_alloc(kernel_map,
-                            (vm_offset_t*)&summaryHeaderAlloc,
-                            size, VM_KERN_MEMORY_OSKEXT);
+        result = kmem_alloc(kernel_map, (vm_offset_t *)&summaryHeaderAlloc, size, VM_KERN_MEMORY_OSKEXT);
         if (result != KERN_SUCCESS) goto finish;
         summaryHeader = summaryHeaderAlloc;
         summarySize = size;
@@ -10967,14 +11626,16 @@ OSKext::updateLoadedKextSummaries(void)
     
     start = (vm_map_offset_t) summaryHeader;
     end = start + summarySize;
-    
+
     result = vm_map_protect(kernel_map, start, end, VM_PROT_READ, FALSE);
-    if (result != KERN_SUCCESS) goto finish;
-    
+    if (result != KERN_SUCCESS)
+        goto finish;
+
     gLoadedKextSummaries = summaryHeader;
+    gLoadedKextSummariesTimestamp = mach_absolute_time();
     sLoadedKextSummariesAllocSize = summarySize;
     summaryHeaderAlloc = NULL;
-    
+
    /* Call the magic breakpoint function through a static function pointer so
     * the compiler can't optimize the function away.
     */
@@ -11016,7 +11677,7 @@ OSKext::updateLoadedKextSummary(OSKextLoadedKextSummary *summary)
     uuid = copyUUID();
     if (uuid) {
         memcpy(summary->uuid, uuid->getBytesNoCopy(), sizeof(summary->uuid));
-        OSSafeRelease(uuid);
+        OSSafeReleaseNULL(uuid);
     }
 
     summary->address = kmod_info->address;
@@ -11033,14 +11694,43 @@ OSKext::updateLoadedKextSummary(OSKextLoadedKextSummary *summary)
 *********************************************************************/
 
 void
-OSKext::updateActiveAccount(OSKextActiveAccount *account)
+OSKext::updateActiveAccount(OSKextActiveAccount *accountp)
 {
-    bzero(account, sizeof(*account));
-    account->address = kmod_info->address;
-    if (account->address) {
-        account->address_end = kmod_info->address + kmod_info->size;
+    kernel_mach_header_t     *hdr = NULL;
+    kernel_segment_command_t *seg = NULL;
+
+    hdr = (kernel_mach_header_t *)kmod_info->address;
+
+    if (getcommandfromheader(hdr, LC_SEGMENT_SPLIT_INFO)) {
+        /* If this kext supports split segments, use the first
+         * executable segment as the range for instructions
+         * (and thus for backtracing.
+         */
+        for (seg = firstsegfromheader(hdr); seg != NULL; seg = nextsegfromheader(hdr, seg)) {
+            if (seg->initprot & VM_PROT_EXECUTE) {
+                break;
+            }
+        }
+    }
+
+    bzero(accountp, sizeof(*accountp));
+    if (seg) {
+        accountp->address = seg->vmaddr;
+        if (accountp->address) {
+            accountp->address_end = seg->vmaddr + seg->vmsize;
+        }
+    } else {
+        /* For non-split kexts and for kexts without executable
+         * segments, just use the kmod_info range (as the kext
+         * is either all in one range or should not show up in
+         * instruction backtraces).
+         */
+        accountp->address = kmod_info->address;
+        if (accountp->address) {
+            accountp->address_end = kmod_info->address + kmod_info->size;
+        }
     }
-    account->account = this->account;
+    accountp->account = this->account;
 }
 
 extern "C" const vm_allocation_site_t * 
@@ -11048,11 +11738,14 @@ OSKextGetAllocationSiteForCaller(uintptr_t address)
 {
     OSKextActiveAccount *  active;
     vm_allocation_site_t * site;
+    vm_allocation_site_t * releasesite;
+
     uint32_t baseIdx;
     uint32_t lim;
 
     IOSimpleLockLock(sKextAccountsLock);
-    site = NULL;
+    site = releasesite = NULL;
+    
     // bsearch sKextAccounts list
     for (baseIdx = 0, lim = sKextAccountsCount; lim; lim >>= 1)
     {
@@ -11060,7 +11753,7 @@ OSKextGetAllocationSiteForCaller(uintptr_t address)
        if ((address >= active->address) && (address < active->address_end))
        {
            site = &active->account->site;
-           if (!site->tag) vm_tag_alloc_locked(site);
+           if (!site->tag) vm_tag_alloc_locked(site, &releasesite);
            break;
        }
        else if (address > active->address) 
@@ -11072,14 +11765,24 @@ OSKextGetAllocationSiteForCaller(uintptr_t address)
        // else move left
     }
     IOSimpleLockUnlock(sKextAccountsLock);
+    if (releasesite) kern_allocation_name_release(releasesite);
 
     return (site);
 }
 
 extern "C" uint32_t 
-OSKextGetKmodIDForSite(vm_allocation_site_t * site)
+OSKextGetKmodIDForSite(const vm_allocation_site_t * site, char * name, vm_size_t namelen)
 {
     OSKextAccount * account = (typeof(account)) site;
+    const char    * kname;
+
+    if (name)
+    {
+        if (account->kext) kname = account->kext->getIdentifierCString();
+        else               kname = "<>";
+        strlcpy(name, kname, namelen);
+    }
+
     return (account->loadTag);
 }
 
@@ -11105,22 +11808,30 @@ OSKextFreeSite(vm_allocation_site_t * site)
 static void * 
 GetAppleTEXTHashForKext(OSKext * theKext, OSDictionary *theInfoDict)
 {
-    AppleTEXTHash_t         my_ath = {1, 0, NULL};
+    AppleTEXTHash_t         my_ath = {2, 0, NULL};
     AppleTEXTHash_t *       my_athp = NULL;         // do not release
-    OSDictionary *          textHashDict = NULL;    // do not release
     OSData *                segmentHash = NULL;     // do not release
     
     if (theKext == NULL || theInfoDict == NULL) {
         return(NULL);
     }
     
-    textHashDict = OSDynamicCast(OSDictionary, theInfoDict->getObject(kAppleTextHashesKey));
-    if (textHashDict == NULL) {
-        return(NULL);
-    }
-    
-    segmentHash = OSDynamicCast(OSData,
-                                textHashDict->getObject(ARCHNAME));
+    // Get the part of the plist associate with kAppleTextHashesKey and let
+    // the crypto library do further parsing (slice/architecture)
+    segmentHash = OSDynamicCast(OSData, theInfoDict->getObject(kAppleTextHashesKey));
+    // Support for ATH v1 while rolling out ATH v2 without revision locking submissions
+    // Remove this when v2 PLIST are supported
+    if (segmentHash == NULL) {
+        // If this fails, we may be dealing with a v1 PLIST
+        OSDictionary *          textHashDict = NULL;    // do not release
+        textHashDict = OSDynamicCast(OSDictionary, theInfoDict->getObject(kAppleTextHashesKey));
+        if (textHashDict == NULL) {
+            return(NULL);
+        }
+        my_ath.ath_version=1;
+        segmentHash = OSDynamicCast(OSData,textHashDict->getObject(ARCHNAME));
+    } // end of v2 rollout
+
     if (segmentHash == NULL) {
         return(NULL);
     }
@@ -11154,3 +11865,22 @@ GetAppleTEXTHashForKext(OSKext * theKext, OSDictionary *theInfoDict)
     
 #endif // CONFIG_KEC_FIPS
 
+#if CONFIG_IMAGEBOOT
+int OSKextGetUUIDForName(const char *name, uuid_t uuid)
+{
+       OSKext *kext = OSKext::lookupKextWithIdentifier(name);
+       if (!kext) {
+               return 1;
+       }
+
+       OSData *uuid_data = kext->copyUUID();
+       if (uuid_data) {
+               memcpy(uuid, uuid_data->getBytesNoCopy(), sizeof(uuid_t));
+               OSSafeReleaseNULL(uuid_data);
+               return 0;
+       }
+
+       return 1;
+}
+#endif
+