Loading...
--- xnu/xnu-12377.101.15/iokit/Kernel/IOBufferMemoryDescriptor.cpp
+++ xnu/xnu-7195.141.2/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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
@@ -70,15 +67,7 @@
kInternalFlagPhysical = 0x00000001,
kInternalFlagPageSized = 0x00000002,
kInternalFlagPageAllocated = 0x00000004,
- 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,
+ kInternalFlagInit = 0x00000008
};
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
@@ -89,27 +78,23 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
-#if defined(__x86_64__)
static uintptr_t
-IOBMDPageProc(kalloc_heap_t kheap, iopa_t * a)
+IOBMDPageProc(iopa_t * a)
{
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(kernel_map, &vmaddr,
+ page_size, 0, KMA_NONE, VM_KERN_MEMORY_IOKIT);
if (KERN_SUCCESS != kr) {
vmaddr = 0;
+ } else {
+ bzero((void *) vmaddr, page_size);
}
return (uintptr_t) vmaddr;
}
-#endif /* defined(__x86_64__) */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
@@ -146,7 +131,10 @@
if (!inst) {
break;
}
- inst->_ranges.v64 = IOMallocType(IOAddressRange);
+ inst->_ranges.v64 = IONew(IOAddressRange, 1);
+ if (!inst->_ranges.v64) {
+ break;
+ }
err = vm_map_copyin(sourceMap, source, size,
false /* src_destroy */, ©);
@@ -189,28 +177,29 @@
mach_vm_address_t physicalMask)
{
task_t mapTask = NULL;
- kalloc_heap_t kheap = KHEAP_DATA_SHARED;
+ vm_map_t vmmap = NULL;
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;
}
- /*
- * The IOKit constructor requests the allocator for zeroed memory
- * so the members of the class do not need to be explicitly zeroed.
- */
_options = options;
_capacity = capacity;
+ _internalFlags = 0;
+ _internalReserved = 0;
+ _buffer = NULL;
if (!_ranges.v64) {
- _ranges.v64 = IOMallocType(IOAddressRange);
+ _ranges.v64 = IONew(IOAddressRange, 1);
+ if (!_ranges.v64) {
+ return false;
+ }
_ranges.v64->address = 0;
_ranges.v64->length = 0;
} else {
@@ -226,14 +215,6 @@
_buffer = (void *) _ranges.v64->address;
withCopy = true;
}
-
- /*
- * Set kalloc_heap to KHEAP_IOBMD_CONTROL if allocation contains pointers
- */
- if (kInternalFlagHasPointers & _internalFlags) {
- kheap = KHEAP_IOBMD_CONTROL;
- }
-
// make sure super::free doesn't dealloc _ranges before super::init
_flags = kIOMemoryAsReference;
@@ -274,19 +255,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));
@@ -304,15 +274,13 @@
// set memory entry cache mode, pageable, purgeable
iomdOptions |= ((options & kIOMapCacheMask) >> kIOMapCacheShift) << kIOMemoryBufferCacheShift;
if (options & kIOMemoryPageable) {
- if (_internalFlags & kInternalFlagGuardPages) {
- printf("IOBMD: Unsupported use of guard pages with pageable memory.\n");
- return false;
- }
iomdOptions |= kIOMemoryBufferPageable;
if (options & kIOMemoryPurgeable) {
iomdOptions |= kIOMemoryBufferPurgeable;
}
} else {
+ vmmap = kernel_map;
+
// Buffer shouldn't auto prepare they should be prepared explicitly
// But it never was enforced so what are you going to do?
iomdOptions |= kIOMemoryAutoPrepare;
@@ -332,10 +300,6 @@
mappedOrShared = (mapped || (0 != (kIOMemorySharingTypeMask & options)));
if (contig || highestMask || (alignment > page_size)) {
- if (_internalFlags & kInternalFlagGuardPages) {
- printf("IOBMD: Unsupported use of guard pages with physical mask or contiguous memory.\n");
- return false;
- }
_internalFlags |= kInternalFlagPhysical;
if (highestMask) {
_internalFlags |= kInternalFlagPageSized;
@@ -343,72 +307,27 @@
return false;
}
}
- _buffer = (void *) IOKernelAllocateWithPhysicalRestrict(kheap,
- capacity, highestMask, alignment, contig, noSoftLimit);
- } 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;
- }
- 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));
- if (kr != KERN_SUCCESS || address == 0) {
- return false;
- }
-#if IOALLOCDEBUG
- OSAddAtomicLong(capacity, &debug_iomalloc_size);
-#endif
- IOStatisticsAlloc(kIOStatisticsMallocAligned, capacity);
- _buffer = (void *)(address + page_size);
-#if defined(__x86_64__)
+ _buffer = (void *) IOKernelAllocateWithPhysicalRestrict(
+ capacity, highestMask, alignment, contig);
} else if (mappedOrShared
&& (capacity + alignment) <= (page_size - gIOPageAllocChunkBytes)) {
_internalFlags |= kInternalFlagPageAllocated;
- _buffer = (void *) iopa_alloc(&gIOBMDPageAllocator,
- &IOBMDPageProc, kheap, capacity, alignment);
+ _buffer = (void *) iopa_alloc(&gIOBMDPageAllocator, &IOBMDPageProc, 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(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(capacity);
}
if (!_buffer) {
return false;
}
+ bzero(_buffer, capacity);
}
if ((options & (kIOMemoryPageable | kIOMapCacheMask))) {
@@ -426,6 +345,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;
@@ -442,13 +364,8 @@
_ranges.v64->address = (mach_vm_address_t) _buffer;
_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;
}
@@ -467,7 +384,7 @@
if (mapTask) {
if (!reserved) {
- reserved = IOMallocType(ExpansionData);
+ reserved = IONew( ExpansionData, 1 );
if (!reserved) {
return false;
}
@@ -491,37 +408,6 @@
setLength(_capacity);
return true;
-}
-
-bool
-IOBufferMemoryDescriptor::initControlWithPhysicalMask(
- task_t inTask,
- IOOptionBits options,
- mach_vm_size_t capacity,
- mach_vm_address_t alignment,
- mach_vm_address_t physicalMask)
-{
- _internalFlags = kInternalFlagHasPointers;
- return initWithPhysicalMask(inTask, options, capacity, alignment,
- physicalMask);
-}
-
-bool
-IOBufferMemoryDescriptor::initWithGuardPages(
- task_t inTask,
- IOOptionBits options,
- mach_vm_size_t capacity)
-{
- mach_vm_size_t roundedCapacity;
-
- _internalFlags = kInternalFlagGuardPages;
-
- if (round_page_overflow(capacity, &roundedCapacity)) {
- return false;
- }
-
- return initWithPhysicalMask(inTask, options, roundedCapacity, page_size,
- (mach_vm_address_t)0);
}
OSSharedPtr<IOBufferMemoryDescriptor>
@@ -575,20 +461,6 @@
return me;
}
-OSSharedPtr<IOBufferMemoryDescriptor>
-IOBufferMemoryDescriptor::inTaskWithGuardPages(
- task_t inTask,
- IOOptionBits options,
- mach_vm_size_t capacity)
-{
- OSSharedPtr<IOBufferMemoryDescriptor> me = OSMakeShared<IOBufferMemoryDescriptor>();
-
- if (me && !me->initWithGuardPages(inTask, options, capacity)) {
- me.reset();
- }
- return me;
-}
-
#ifndef __LP64__
bool
IOBufferMemoryDescriptor::initWithOptions(
@@ -675,13 +547,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,18 +584,14 @@
IOMemoryMap * map = NULL;
IOAddressRange * range = _ranges.v64;
vm_offset_t alignment = _alignment;
- kalloc_heap_t kheap = KHEAP_DATA_SHARED;
- vm_size_t rsize;
if (alignment >= page_size) {
- if (!round_page_overflow(size, &rsize)) {
- size = rsize;
- }
+ size = round_page(size);
}
if (reserved) {
map = reserved->map;
- IOFreeType(reserved, ExpansionData);
+ IODelete( reserved, ExpansionData, 1 );
if (map) {
map->release();
}
@@ -733,13 +599,7 @@
if ((options & kIOMemoryPageable)
|| (kInternalFlagPageSized & internalFlags)) {
- if (!round_page_overflow(size, &rsize)) {
- size = rsize;
- }
- }
-
- if (internalFlags & kInternalFlagHasPointers) {
- kheap = KHEAP_IOBMD_CONTROL;
+ size = round_page(size);
}
#if IOTRACKING
@@ -759,9 +619,8 @@
#endif
} else if (buffer) {
if (kInternalFlagPhysical & internalFlags) {
- IOKernelFreePhysical(kheap, (mach_vm_address_t) buffer, size);
+ IOKernelFreePhysical((mach_vm_address_t) buffer, size);
} else if (kInternalFlagPageAllocated & internalFlags) {
-#if defined(__x86_64__)
uintptr_t page;
page = iopa_free(&gIOBMDPageAllocator, (uintptr_t) buffer, size);
if (page) {
@@ -771,30 +630,14 @@
OSAddAtomicLong(-size, &debug_iomalloc_size);
#endif
IOStatisticsAlloc(kIOStatisticsFreeAligned, size);
-#else /* !defined(__x86_64__) */
- /* should be unreachable */
- panic("Attempting to free IOBMD with page allocated flag");
-#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));
-#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);
+ IOFreeAligned(buffer, size);
} else {
- IOFree_internal(kheap, buffer, size);
- __typed_allocators_ignore_pop
- /* END IGNORE CODESTYLE */
+ IOFree(buffer, size);
}
}
if (range && (kIOMemoryAsReference & flags)) {
- IOFreeType(range, IOAddressRange);
+ IODelete(range, IOAddressRange, 1);
}
}
@@ -857,7 +700,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 +727,6 @@
void *
IOBufferMemoryDescriptor::getBytesNoCopy()
{
- if (__improbable(_internalFlags & kInternalFlagAsIfUnmapped)) {
- return NULL;
- }
-
if (kIOMemoryTypePhysical64 == (_flags & kIOMemoryTypeMask)) {
return _buffer;
} else {
@@ -905,10 +744,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;