Loading...
iokit/Kernel/IOBufferMemoryDescriptor.cpp xnu-12377.101.15 xnu-8792.41.9
--- xnu/xnu-12377.101.15/iokit/Kernel/IOBufferMemoryDescriptor.cpp
+++ xnu/xnu-8792.41.9/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,
 };
 
 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
@@ -97,8 +88,8 @@
 	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);
+	if (kheap == KHEAP_DATA_BUFFERS) {
+		kma_flags = (kma_flags_t) (kma_flags | KMA_DATA);
 	}
 	kr = kmem_alloc(kernel_map, &vmaddr, page_size,
 	    kma_flags, VM_KERN_MEMORY_IOKIT);
@@ -189,14 +180,13 @@
 	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;
 
 	if (!capacity) {
 		return false;
@@ -228,10 +218,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 +264,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,7 +323,7 @@
 				}
 			}
 			_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;
@@ -355,12 +334,8 @@
 			if (((uint32_t) alignment) != alignment) {
 				return false;
 			}
-			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);
+			if (kheap == KHEAP_DATA_BUFFERS) {
+				kma_flags = (kma_flags_t) (kma_flags | KMA_DATA);
 			}
 
 			alignMask = (1UL << log2up((uint32_t) alignment)) - 1;
@@ -389,20 +364,13 @@
 #endif
 			}
 #endif /* defined(__x86_64__) */
-		} else {
-			zalloc_flags_t zflags = Z_ZERO_VM_TAG_BT_BIT;
-			if (noSoftLimit) {
-				zflags = (zalloc_flags_t)(zflags | Z_NOSOFTLIMIT);
-			}
-
+		} else if (alignment > 1) {
 			/* BEGIN IGNORE CODESTYLE */
 			__typed_allocators_ignore_push
-			if (alignment > 1) {
-				_buffer = IOMallocAligned_internal(kheap, capacity, alignment,
-					zflags);
-			} else {
-				_buffer = IOMalloc_internal(kheap, capacity, zflags);
-			}
+			_buffer = IOMallocAligned_internal(kheap, capacity, alignment,
+			    Z_ZERO_VM_TAG_BT_BIT);
+		} else {
+			_buffer = IOMalloc_internal(kheap, capacity, Z_ZERO_VM_TAG_BT_BIT);
 			__typed_allocators_ignore_pop
 			/* END IGNORE CODESTYLE */
 		}
@@ -426,6 +394,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 +410,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 +641,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 +678,10 @@
 	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;
 
 	if (alignment >= page_size) {
-		if (!round_page_overflow(size, &rsize)) {
-			size = rsize;
-		}
+		size = round_page(size);
 	}
 
 	if (reserved) {
@@ -733,13 +694,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
@@ -777,8 +736,7 @@
 #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(kernel_map, allocation, size + page_size * 2);
 #if IOALLOCDEBUG
 			OSAddAtomicLong(-size, &debug_iomalloc_size);
 #endif
@@ -857,7 +815,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 +842,6 @@
 void *
 IOBufferMemoryDescriptor::getBytesNoCopy()
 {
-	if (__improbable(_internalFlags & kInternalFlagAsIfUnmapped)) {
-		return NULL;
-	}
-
 	if (kIOMemoryTypePhysical64 == (_flags & kIOMemoryTypeMask)) {
 		return _buffer;
 	} else {
@@ -905,10 +859,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;