Loading...
iokit/Kernel/IOBufferMemoryDescriptor.cpp xnu-12377.101.15 xnu-11215.1.10
--- xnu/xnu-12377.101.15/iokit/Kernel/IOBufferMemoryDescriptor.cpp
+++ xnu/xnu-11215.1.10/iokit/Kernel/IOBufferMemoryDescriptor.cpp
@@ -73,12 +73,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 +91,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 +183,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;
@@ -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,7 +326,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 +337,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 +367,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 +397,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 +413,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;
 	}
 
@@ -714,7 +683,7 @@
 	IOMemoryMap *    map       = NULL;
 	IOAddressRange * range     = _ranges.v64;
 	vm_offset_t      alignment = _alignment;
-	kalloc_heap_t    kheap     = KHEAP_DATA_SHARED;
+	kalloc_heap_t    kheap     = KHEAP_DATA_BUFFERS;
 	vm_size_t        rsize;
 
 	if (alignment >= page_size) {
@@ -777,8 +746,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 +825,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 +852,6 @@
 void *
 IOBufferMemoryDescriptor::getBytesNoCopy()
 {
-	if (__improbable(_internalFlags & kInternalFlagAsIfUnmapped)) {
-		return NULL;
-	}
-
 	if (kIOMemoryTypePhysical64 == (_flags & kIOMemoryTypeMask)) {
 		return _buffer;
 	} else {
@@ -905,10 +869,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;