Loading...
iokit/Kernel/IOBufferMemoryDescriptor.cpp xnu-12377.101.15 xnu-8020.101.4
--- xnu/xnu-12377.101.15/iokit/Kernel/IOBufferMemoryDescriptor.cpp
+++ xnu/xnu-8020.101.4/iokit/Kernel/IOBufferMemoryDescriptor.cpp
@@ -38,8 +38,6 @@
 #include <libkern/OSDebug.h>
 #include <mach/mach_vm.h>
 
-#include <vm/vm_kern_xnu.h>
-
 #include "IOKitKernelInternal.h"
 
 #ifdef IOALLOCDEBUG
@@ -61,7 +59,6 @@
 void ipc_port_release_send(ipc_port_t port);
 #include <vm/pmap.h>
 
-KALLOC_HEAP_DEFINE(KHEAP_IOBMD_CONTROL, "IOBMD_control", KHEAP_ID_KT_VAR);
 __END_DECLS
 
 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
@@ -73,12 +70,6 @@
 	kInternalFlagInit          = 0x00000008,
 	kInternalFlagHasPointers   = 0x00000010,
 	kInternalFlagGuardPages    = 0x00000020,
-	/**
-	 * Should the IOBMD behave as if it has no kernel mapping for the
-	 * underlying buffer? Note that this does not necessarily imply the
-	 * existence (or non-existence) of a kernel mapping.
-	 */
-	kInternalFlagAsIfUnmapped  = 0x00000040,
 };
 
 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
@@ -95,13 +86,9 @@
 {
 	kern_return_t kr;
 	vm_address_t  vmaddr  = 0;
-	kma_flags_t kma_flags = KMA_ZERO;
-
-	if (kheap == KHEAP_DATA_SHARED) {
-		kma_flags = (kma_flags_t) (kma_flags | KMA_DATA_SHARED);
-	}
-	kr = kmem_alloc(kernel_map, &vmaddr, page_size,
-	    kma_flags, VM_KERN_MEMORY_IOKIT);
+
+	kr = kernel_memory_allocate(kheap->kh_fallback_map, &vmaddr,
+	    page_size, 0, (kma_flags_t) (KMA_NONE | KMA_ZERO), VM_KERN_MEMORY_IOKIT);
 
 	if (KERN_SUCCESS != kr) {
 		vmaddr = 0;
@@ -189,14 +176,20 @@
 	mach_vm_address_t physicalMask)
 {
 	task_t                mapTask = NULL;
-	kalloc_heap_t         kheap = KHEAP_DATA_SHARED;
+	kalloc_heap_t         kheap = KHEAP_DATA_BUFFERS;
 	mach_vm_address_t     highestMask = 0;
 	IOOptionBits          iomdOptions = kIOMemoryTypeVirtual64 | kIOMemoryAsReference;
 	IODMAMapSpecification mapSpec;
 	bool                  mapped = false;
 	bool                  withCopy = false;
 	bool                  mappedOrShared = false;
-	bool                  noSoftLimit = false;
+
+	/*
+	 * Temporarily use default heap on intel due to rdar://74982985
+	 */
+#if __x86_64__
+	kheap = KHEAP_DEFAULT;
+#endif
 
 	if (!capacity) {
 		return false;
@@ -228,10 +221,10 @@
 	}
 
 	/*
-	 * Set kalloc_heap to KHEAP_IOBMD_CONTROL if allocation contains pointers
+	 * Set kalloc_heap to default if allocation contains pointers
 	 */
 	if (kInternalFlagHasPointers & _internalFlags) {
-		kheap = KHEAP_IOBMD_CONTROL;
+		kheap = KHEAP_DEFAULT;
 	}
 
 	//  make sure super::free doesn't dealloc _ranges before super::init
@@ -274,19 +267,8 @@
 		return false;
 	}
 
-	if (inTask) {
-		if ((inTask != kernel_task) && !(options & kIOMemoryPageable)) {
-			// Cannot create non-pageable memory in user tasks
-			return false;
-		}
-	} else {
-		// Not passing a task implies the memory should not be mapped (or, at
-		// least, should behave as if it were not mapped)
-		_internalFlags |= kInternalFlagAsIfUnmapped;
-
-		// Disable the soft-limit since the mapping, if any, will not escape the
-		// IOBMD.
-		noSoftLimit = true;
+	if ((inTask != kernel_task) && !(options & kIOMemoryPageable)) {
+		return false;
 	}
 
 	bzero(&mapSpec, sizeof(mapSpec));
@@ -344,29 +326,19 @@
 				}
 			}
 			_buffer = (void *) IOKernelAllocateWithPhysicalRestrict(kheap,
-			    capacity, highestMask, alignment, contig, noSoftLimit);
+			    capacity, highestMask, alignment, contig);
 		} else if (_internalFlags & kInternalFlagGuardPages) {
 			vm_offset_t address = 0;
 			kern_return_t kr;
 			uintptr_t alignMask;
-			kma_flags_t kma_flags = (kma_flags_t) (KMA_GUARD_FIRST |
-			    KMA_GUARD_LAST | KMA_ZERO);
 
 			if (((uint32_t) alignment) != alignment) {
-				return false;
+				return NULL;
 			}
-			if (kheap == KHEAP_DATA_SHARED) {
-				kma_flags = (kma_flags_t) (kma_flags | KMA_DATA_SHARED);
-			}
-
-			if (noSoftLimit) {
-				kma_flags = (kma_flags_t)(kma_flags | KMA_NOSOFTLIMIT);
-			}
 
 			alignMask = (1UL << log2up((uint32_t) alignment)) - 1;
-			kr = kernel_memory_allocate(kernel_map, &address,
-			    capacity + page_size * 2, alignMask, kma_flags,
-			    IOMemoryTag(kernel_map));
+			kr = kernel_memory_allocate(kheap->kh_fallback_map, &address,
+			    capacity + page_size * 2, alignMask, (kma_flags_t)(KMA_GUARD_FIRST | KMA_GUARD_LAST), IOMemoryTag(kernel_map));
 			if (kr != KERN_SUCCESS || address == 0) {
 				return false;
 			}
@@ -382,33 +354,21 @@
 			_buffer         = (void *) iopa_alloc(&gIOBMDPageAllocator,
 			    &IOBMDPageProc, kheap, capacity, alignment);
 			if (_buffer) {
-				bzero(_buffer, capacity);
 				IOStatisticsAlloc(kIOStatisticsMallocAligned, capacity);
 #if IOALLOCDEBUG
 				OSAddAtomicLong(capacity, &debug_iomalloc_size);
 #endif
 			}
 #endif /* defined(__x86_64__) */
+		} else if (alignment > 1) {
+			_buffer = IOMallocAligned_internal(kheap, capacity, alignment);
 		} else {
-			zalloc_flags_t zflags = Z_ZERO_VM_TAG_BT_BIT;
-			if (noSoftLimit) {
-				zflags = (zalloc_flags_t)(zflags | Z_NOSOFTLIMIT);
-			}
-
-			/* BEGIN IGNORE CODESTYLE */
-			__typed_allocators_ignore_push
-			if (alignment > 1) {
-				_buffer = IOMallocAligned_internal(kheap, capacity, alignment,
-					zflags);
-			} else {
-				_buffer = IOMalloc_internal(kheap, capacity, zflags);
-			}
-			__typed_allocators_ignore_pop
-			/* END IGNORE CODESTYLE */
+			_buffer = IOMalloc_internal(kheap, capacity);
 		}
 		if (!_buffer) {
 			return false;
 		}
+		bzero(_buffer, capacity);
 	}
 
 	if ((options & (kIOMemoryPageable | kIOMapCacheMask))) {
@@ -426,6 +386,9 @@
 			if (!withCopy) {
 				mapTask = inTask;
 			}
+			if (NULL == inTask) {
+				inTask = kernel_task;
+			}
 		} else if (options & kIOMapCacheMask) {
 			// Prefetch each page to put entries into the pmap
 			volatile UInt8 *    startAddr = (UInt8 *)_buffer;
@@ -439,16 +402,11 @@
 		}
 	}
 
-	_ranges.v64->address = (mach_vm_address_t) _buffer;
+	_ranges.v64->address = (mach_vm_address_t) pgz_decode(_buffer, _capacity);
 	_ranges.v64->length  = _capacity;
 
-	if (!super::initWithOptions(
-		    /* buffers */ _ranges.v64, /* count */ 1, /* offset */ 0,
-		    // Since we handle all "unmapped" behavior internally and our superclass
-		    // requires a task, default all unbound IOBMDs to the kernel task.
-		    /* task */ inTask ?: kernel_task,
-		    /* options */ iomdOptions,
-		    /* System mapper */ NULL)) {
+	if (!super::initWithOptions(_ranges.v64, 1, 0,
+	    inTask, iomdOptions, /* System mapper */ NULL)) {
 		return false;
 	}
 
@@ -675,13 +633,11 @@
     bool         inContiguous)
 {
 	OSSharedPtr<IOBufferMemoryDescriptor> me = OSMakeShared<IOBufferMemoryDescriptor>();
-	mach_vm_address_t alignment;
-
-	alignment = (inLength <= page_size) ? inLength : page_size;
+
 	if (me && !me->initWithPhysicalMask(
 		    kernel_task, inDirection | kIOMemoryUnshared
 		    | (inContiguous ? kIOMemoryPhysicallyContiguous : 0),
-		    inLength, alignment, 0 )) {
+		    inLength, inLength, 0 )) {
 		me.reset();
 	}
 
@@ -714,13 +670,17 @@
 	IOMemoryMap *    map       = NULL;
 	IOAddressRange * range     = _ranges.v64;
 	vm_offset_t      alignment = _alignment;
-	kalloc_heap_t    kheap     = KHEAP_DATA_SHARED;
-	vm_size_t        rsize;
+	kalloc_heap_t    kheap     = KHEAP_DATA_BUFFERS;
+
+	/*
+	 * Temporarily use default heap on intel due to rdar://74982985
+	 */
+#if __x86_64__
+	kheap = KHEAP_DEFAULT;
+#endif
 
 	if (alignment >= page_size) {
-		if (!round_page_overflow(size, &rsize)) {
-			size = rsize;
-		}
+		size = round_page(size);
 	}
 
 	if (reserved) {
@@ -733,13 +693,11 @@
 
 	if ((options & kIOMemoryPageable)
 	    || (kInternalFlagPageSized & internalFlags)) {
-		if (!round_page_overflow(size, &rsize)) {
-			size = rsize;
-		}
+		size = round_page(size);
 	}
 
 	if (internalFlags & kInternalFlagHasPointers) {
-		kheap = KHEAP_IOBMD_CONTROL;
+		kheap = KHEAP_DEFAULT;
 	}
 
 #if IOTRACKING
@@ -765,7 +723,7 @@
 			uintptr_t page;
 			page = iopa_free(&gIOBMDPageAllocator, (uintptr_t) buffer, size);
 			if (page) {
-				kmem_free(kernel_map, page, page_size);
+				kmem_free(kheap->kh_fallback_map, page, page_size);
 			}
 #if IOALLOCDEBUG
 			OSAddAtomicLong(-size, &debug_iomalloc_size);
@@ -777,20 +735,15 @@
 #endif /* defined(__x86_64__) */
 		} else if (kInternalFlagGuardPages & internalFlags) {
 			vm_offset_t allocation = (vm_offset_t)buffer - page_size;
-			kmem_free(kernel_map, allocation, size + page_size * 2,
-			    (kmf_flags_t)(KMF_GUARD_FIRST | KMF_GUARD_LAST));
+			kmem_free(kheap->kh_fallback_map, allocation, size + page_size * 2);
 #if IOALLOCDEBUG
 			OSAddAtomicLong(-size, &debug_iomalloc_size);
 #endif
 			IOStatisticsAlloc(kIOStatisticsFreeAligned, size);
 		} else if (alignment > 1) {
-			/* BEGIN IGNORE CODESTYLE */
-			__typed_allocators_ignore_push
 			IOFreeAligned_internal(kheap, buffer, size);
 		} else {
 			IOFree_internal(kheap, buffer, size);
-			__typed_allocators_ignore_pop
-			/* END IGNORE CODESTYLE */
 		}
 	}
 	if (range && (kIOMemoryAsReference & flags)) {
@@ -857,7 +810,7 @@
 bool
 IOBufferMemoryDescriptor::appendBytes(const void * bytes, vm_size_t withLength)
 {
-	vm_size_t   actualBytesToCopy = IOMin(withLength, _capacity - _length);
+	vm_size_t   actualBytesToCopy = min(withLength, _capacity - _length);
 	IOByteCount offset;
 
 	assert(_length <= _capacity);
@@ -884,10 +837,6 @@
 void *
 IOBufferMemoryDescriptor::getBytesNoCopy()
 {
-	if (__improbable(_internalFlags & kInternalFlagAsIfUnmapped)) {
-		return NULL;
-	}
-
 	if (kIOMemoryTypePhysical64 == (_flags & kIOMemoryTypeMask)) {
 		return _buffer;
 	} else {
@@ -905,10 +854,6 @@
 IOBufferMemoryDescriptor::getBytesNoCopy(vm_size_t start, vm_size_t withLength)
 {
 	IOVirtualAddress address;
-
-	if (__improbable(_internalFlags & kInternalFlagAsIfUnmapped)) {
-		return NULL;
-	}
 
 	if ((start + withLength) < start) {
 		return NULL;