xref: /xnu-10002.1.13/iokit/Kernel/IOBufferMemoryDescriptor.cpp (revision 1031c584a5e37aff177559b9f69dbd3c8c3fd30a)
1*1031c584SApple OSS Distributions /*
2*1031c584SApple OSS Distributions  * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
3*1031c584SApple OSS Distributions  *
4*1031c584SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*1031c584SApple OSS Distributions  *
6*1031c584SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*1031c584SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*1031c584SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*1031c584SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*1031c584SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*1031c584SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*1031c584SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*1031c584SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*1031c584SApple OSS Distributions  *
15*1031c584SApple OSS Distributions  * Please obtain a copy of the License at
16*1031c584SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*1031c584SApple OSS Distributions  *
18*1031c584SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*1031c584SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*1031c584SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*1031c584SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*1031c584SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*1031c584SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*1031c584SApple OSS Distributions  * limitations under the License.
25*1031c584SApple OSS Distributions  *
26*1031c584SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*1031c584SApple OSS Distributions  */
28*1031c584SApple OSS Distributions #define IOKIT_ENABLE_SHARED_PTR
29*1031c584SApple OSS Distributions 
30*1031c584SApple OSS Distributions #define _IOMEMORYDESCRIPTOR_INTERNAL_
31*1031c584SApple OSS Distributions 
32*1031c584SApple OSS Distributions #include <IOKit/assert.h>
33*1031c584SApple OSS Distributions #include <IOKit/system.h>
34*1031c584SApple OSS Distributions 
35*1031c584SApple OSS Distributions #include <IOKit/IOLib.h>
36*1031c584SApple OSS Distributions #include <IOKit/IOMapper.h>
37*1031c584SApple OSS Distributions #include <IOKit/IOBufferMemoryDescriptor.h>
38*1031c584SApple OSS Distributions #include <libkern/OSDebug.h>
39*1031c584SApple OSS Distributions #include <mach/mach_vm.h>
40*1031c584SApple OSS Distributions 
41*1031c584SApple OSS Distributions #include "IOKitKernelInternal.h"
42*1031c584SApple OSS Distributions 
43*1031c584SApple OSS Distributions #ifdef IOALLOCDEBUG
44*1031c584SApple OSS Distributions #include <libkern/c++/OSCPPDebug.h>
45*1031c584SApple OSS Distributions #endif
46*1031c584SApple OSS Distributions #include <IOKit/IOStatisticsPrivate.h>
47*1031c584SApple OSS Distributions 
48*1031c584SApple OSS Distributions #if IOKITSTATS
49*1031c584SApple OSS Distributions #define IOStatisticsAlloc(type, size) \
50*1031c584SApple OSS Distributions do { \
51*1031c584SApple OSS Distributions 	IOStatistics::countAlloc(type, size); \
52*1031c584SApple OSS Distributions } while (0)
53*1031c584SApple OSS Distributions #else
54*1031c584SApple OSS Distributions #define IOStatisticsAlloc(type, size)
55*1031c584SApple OSS Distributions #endif /* IOKITSTATS */
56*1031c584SApple OSS Distributions 
57*1031c584SApple OSS Distributions 
58*1031c584SApple OSS Distributions __BEGIN_DECLS
59*1031c584SApple OSS Distributions void ipc_port_release_send(ipc_port_t port);
60*1031c584SApple OSS Distributions #include <vm/pmap.h>
61*1031c584SApple OSS Distributions 
62*1031c584SApple OSS Distributions KALLOC_HEAP_DEFINE(KHEAP_IOBMD_CONTROL, "IOBMD_control", KHEAP_ID_KT_VAR);
63*1031c584SApple OSS Distributions __END_DECLS
64*1031c584SApple OSS Distributions 
65*1031c584SApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
66*1031c584SApple OSS Distributions 
67*1031c584SApple OSS Distributions enum{
68*1031c584SApple OSS Distributions 	kInternalFlagPhysical      = 0x00000001,
69*1031c584SApple OSS Distributions 	kInternalFlagPageSized     = 0x00000002,
70*1031c584SApple OSS Distributions 	kInternalFlagPageAllocated = 0x00000004,
71*1031c584SApple OSS Distributions 	kInternalFlagInit          = 0x00000008,
72*1031c584SApple OSS Distributions 	kInternalFlagHasPointers   = 0x00000010,
73*1031c584SApple OSS Distributions 	kInternalFlagGuardPages    = 0x00000020,
74*1031c584SApple OSS Distributions };
75*1031c584SApple OSS Distributions 
76*1031c584SApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
77*1031c584SApple OSS Distributions 
78*1031c584SApple OSS Distributions #define super IOGeneralMemoryDescriptor
79*1031c584SApple OSS Distributions OSDefineMetaClassAndStructorsWithZone(IOBufferMemoryDescriptor,
80*1031c584SApple OSS Distributions     IOGeneralMemoryDescriptor, ZC_ZFREE_CLEARMEM);
81*1031c584SApple OSS Distributions 
82*1031c584SApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
83*1031c584SApple OSS Distributions 
84*1031c584SApple OSS Distributions #if defined(__x86_64__)
85*1031c584SApple OSS Distributions static uintptr_t
IOBMDPageProc(kalloc_heap_t kheap,iopa_t * a)86*1031c584SApple OSS Distributions IOBMDPageProc(kalloc_heap_t kheap, iopa_t * a)
87*1031c584SApple OSS Distributions {
88*1031c584SApple OSS Distributions 	kern_return_t kr;
89*1031c584SApple OSS Distributions 	vm_address_t  vmaddr  = 0;
90*1031c584SApple OSS Distributions 	kma_flags_t kma_flags = KMA_ZERO;
91*1031c584SApple OSS Distributions 
92*1031c584SApple OSS Distributions 	if (kheap == KHEAP_DATA_BUFFERS) {
93*1031c584SApple OSS Distributions 		kma_flags = (kma_flags_t) (kma_flags | KMA_DATA);
94*1031c584SApple OSS Distributions 	}
95*1031c584SApple OSS Distributions 	kr = kmem_alloc(kernel_map, &vmaddr, page_size,
96*1031c584SApple OSS Distributions 	    kma_flags, VM_KERN_MEMORY_IOKIT);
97*1031c584SApple OSS Distributions 
98*1031c584SApple OSS Distributions 	if (KERN_SUCCESS != kr) {
99*1031c584SApple OSS Distributions 		vmaddr = 0;
100*1031c584SApple OSS Distributions 	}
101*1031c584SApple OSS Distributions 
102*1031c584SApple OSS Distributions 	return (uintptr_t) vmaddr;
103*1031c584SApple OSS Distributions }
104*1031c584SApple OSS Distributions #endif /* defined(__x86_64__) */
105*1031c584SApple OSS Distributions 
106*1031c584SApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
107*1031c584SApple OSS Distributions 
108*1031c584SApple OSS Distributions #ifndef __LP64__
109*1031c584SApple OSS Distributions bool
initWithOptions(IOOptionBits options,vm_size_t capacity,vm_offset_t alignment,task_t inTask)110*1031c584SApple OSS Distributions IOBufferMemoryDescriptor::initWithOptions(
111*1031c584SApple OSS Distributions 	IOOptionBits options,
112*1031c584SApple OSS Distributions 	vm_size_t    capacity,
113*1031c584SApple OSS Distributions 	vm_offset_t  alignment,
114*1031c584SApple OSS Distributions 	task_t       inTask)
115*1031c584SApple OSS Distributions {
116*1031c584SApple OSS Distributions 	mach_vm_address_t physicalMask = 0;
117*1031c584SApple OSS Distributions 	return initWithPhysicalMask(inTask, options, capacity, alignment, physicalMask);
118*1031c584SApple OSS Distributions }
119*1031c584SApple OSS Distributions #endif /* !__LP64__ */
120*1031c584SApple OSS Distributions 
121*1031c584SApple OSS Distributions OSSharedPtr<IOBufferMemoryDescriptor>
withCopy(task_t inTask,IOOptionBits options,vm_map_t sourceMap,mach_vm_address_t source,mach_vm_size_t size)122*1031c584SApple OSS Distributions IOBufferMemoryDescriptor::withCopy(
123*1031c584SApple OSS Distributions 	task_t                inTask,
124*1031c584SApple OSS Distributions 	IOOptionBits      options,
125*1031c584SApple OSS Distributions 	vm_map_t              sourceMap,
126*1031c584SApple OSS Distributions 	mach_vm_address_t source,
127*1031c584SApple OSS Distributions 	mach_vm_size_t    size)
128*1031c584SApple OSS Distributions {
129*1031c584SApple OSS Distributions 	OSSharedPtr<IOBufferMemoryDescriptor> inst;
130*1031c584SApple OSS Distributions 	kern_return_t              err;
131*1031c584SApple OSS Distributions 	vm_map_copy_t              copy;
132*1031c584SApple OSS Distributions 	vm_map_address_t           address;
133*1031c584SApple OSS Distributions 
134*1031c584SApple OSS Distributions 	copy = NULL;
135*1031c584SApple OSS Distributions 	do {
136*1031c584SApple OSS Distributions 		err = kIOReturnNoMemory;
137*1031c584SApple OSS Distributions 		inst = OSMakeShared<IOBufferMemoryDescriptor>();
138*1031c584SApple OSS Distributions 		if (!inst) {
139*1031c584SApple OSS Distributions 			break;
140*1031c584SApple OSS Distributions 		}
141*1031c584SApple OSS Distributions 		inst->_ranges.v64 = IOMallocType(IOAddressRange);
142*1031c584SApple OSS Distributions 
143*1031c584SApple OSS Distributions 		err = vm_map_copyin(sourceMap, source, size,
144*1031c584SApple OSS Distributions 		    false /* src_destroy */, &copy);
145*1031c584SApple OSS Distributions 		if (KERN_SUCCESS != err) {
146*1031c584SApple OSS Distributions 			break;
147*1031c584SApple OSS Distributions 		}
148*1031c584SApple OSS Distributions 
149*1031c584SApple OSS Distributions 		err = vm_map_copyout(get_task_map(inTask), &address, copy);
150*1031c584SApple OSS Distributions 		if (KERN_SUCCESS != err) {
151*1031c584SApple OSS Distributions 			break;
152*1031c584SApple OSS Distributions 		}
153*1031c584SApple OSS Distributions 		copy = NULL;
154*1031c584SApple OSS Distributions 
155*1031c584SApple OSS Distributions 		inst->_ranges.v64->address = address;
156*1031c584SApple OSS Distributions 		inst->_ranges.v64->length  = size;
157*1031c584SApple OSS Distributions 
158*1031c584SApple OSS Distributions 		if (!inst->initWithPhysicalMask(inTask, options, size, page_size, 0)) {
159*1031c584SApple OSS Distributions 			err = kIOReturnError;
160*1031c584SApple OSS Distributions 		}
161*1031c584SApple OSS Distributions 	} while (false);
162*1031c584SApple OSS Distributions 
163*1031c584SApple OSS Distributions 	if (KERN_SUCCESS == err) {
164*1031c584SApple OSS Distributions 		return inst;
165*1031c584SApple OSS Distributions 	}
166*1031c584SApple OSS Distributions 
167*1031c584SApple OSS Distributions 	if (copy) {
168*1031c584SApple OSS Distributions 		vm_map_copy_discard(copy);
169*1031c584SApple OSS Distributions 	}
170*1031c584SApple OSS Distributions 
171*1031c584SApple OSS Distributions 	return nullptr;
172*1031c584SApple OSS Distributions }
173*1031c584SApple OSS Distributions 
174*1031c584SApple OSS Distributions 
175*1031c584SApple OSS Distributions bool
initWithPhysicalMask(task_t inTask,IOOptionBits options,mach_vm_size_t capacity,mach_vm_address_t alignment,mach_vm_address_t physicalMask)176*1031c584SApple OSS Distributions IOBufferMemoryDescriptor::initWithPhysicalMask(
177*1031c584SApple OSS Distributions 	task_t            inTask,
178*1031c584SApple OSS Distributions 	IOOptionBits      options,
179*1031c584SApple OSS Distributions 	mach_vm_size_t    capacity,
180*1031c584SApple OSS Distributions 	mach_vm_address_t alignment,
181*1031c584SApple OSS Distributions 	mach_vm_address_t physicalMask)
182*1031c584SApple OSS Distributions {
183*1031c584SApple OSS Distributions 	task_t                mapTask = NULL;
184*1031c584SApple OSS Distributions 	kalloc_heap_t         kheap = KHEAP_DATA_BUFFERS;
185*1031c584SApple OSS Distributions 	mach_vm_address_t     highestMask = 0;
186*1031c584SApple OSS Distributions 	IOOptionBits          iomdOptions = kIOMemoryTypeVirtual64 | kIOMemoryAsReference;
187*1031c584SApple OSS Distributions 	IODMAMapSpecification mapSpec;
188*1031c584SApple OSS Distributions 	bool                  mapped = false;
189*1031c584SApple OSS Distributions 	bool                  withCopy = false;
190*1031c584SApple OSS Distributions 	bool                  mappedOrShared = false;
191*1031c584SApple OSS Distributions 
192*1031c584SApple OSS Distributions 	if (!capacity) {
193*1031c584SApple OSS Distributions 		return false;
194*1031c584SApple OSS Distributions 	}
195*1031c584SApple OSS Distributions 
196*1031c584SApple OSS Distributions 	/*
197*1031c584SApple OSS Distributions 	 * The IOKit constructor requests the allocator for zeroed memory
198*1031c584SApple OSS Distributions 	 * so the members of the class do not need to be explicitly zeroed.
199*1031c584SApple OSS Distributions 	 */
200*1031c584SApple OSS Distributions 	_options          = options;
201*1031c584SApple OSS Distributions 	_capacity         = capacity;
202*1031c584SApple OSS Distributions 
203*1031c584SApple OSS Distributions 	if (!_ranges.v64) {
204*1031c584SApple OSS Distributions 		_ranges.v64 = IOMallocType(IOAddressRange);
205*1031c584SApple OSS Distributions 		_ranges.v64->address = 0;
206*1031c584SApple OSS Distributions 		_ranges.v64->length  = 0;
207*1031c584SApple OSS Distributions 	} else {
208*1031c584SApple OSS Distributions 		if (!_ranges.v64->address) {
209*1031c584SApple OSS Distributions 			return false;
210*1031c584SApple OSS Distributions 		}
211*1031c584SApple OSS Distributions 		if (!(kIOMemoryPageable & options)) {
212*1031c584SApple OSS Distributions 			return false;
213*1031c584SApple OSS Distributions 		}
214*1031c584SApple OSS Distributions 		if (!inTask) {
215*1031c584SApple OSS Distributions 			return false;
216*1031c584SApple OSS Distributions 		}
217*1031c584SApple OSS Distributions 		_buffer = (void *) _ranges.v64->address;
218*1031c584SApple OSS Distributions 		withCopy = true;
219*1031c584SApple OSS Distributions 	}
220*1031c584SApple OSS Distributions 
221*1031c584SApple OSS Distributions 	/*
222*1031c584SApple OSS Distributions 	 * Set kalloc_heap to KHEAP_IOBMD_CONTROL if allocation contains pointers
223*1031c584SApple OSS Distributions 	 */
224*1031c584SApple OSS Distributions 	if (kInternalFlagHasPointers & _internalFlags) {
225*1031c584SApple OSS Distributions 		kheap = KHEAP_IOBMD_CONTROL;
226*1031c584SApple OSS Distributions 	}
227*1031c584SApple OSS Distributions 
228*1031c584SApple OSS Distributions 	//  make sure super::free doesn't dealloc _ranges before super::init
229*1031c584SApple OSS Distributions 	_flags = kIOMemoryAsReference;
230*1031c584SApple OSS Distributions 
231*1031c584SApple OSS Distributions 	// Grab IOMD bits from the Buffer MD options
232*1031c584SApple OSS Distributions 	iomdOptions  |= (options & kIOBufferDescriptorMemoryFlags);
233*1031c584SApple OSS Distributions 
234*1031c584SApple OSS Distributions 	if (!(kIOMemoryMapperNone & options)) {
235*1031c584SApple OSS Distributions 		IOMapper::checkForSystemMapper();
236*1031c584SApple OSS Distributions 		mapped = (NULL != IOMapper::gSystem);
237*1031c584SApple OSS Distributions 	}
238*1031c584SApple OSS Distributions 
239*1031c584SApple OSS Distributions 	if (physicalMask && (alignment <= 1)) {
240*1031c584SApple OSS Distributions 		alignment   = ((physicalMask ^ (-1ULL)) & (physicalMask - 1));
241*1031c584SApple OSS Distributions 		highestMask = (physicalMask | alignment);
242*1031c584SApple OSS Distributions 		alignment++;
243*1031c584SApple OSS Distributions 		if (alignment < page_size) {
244*1031c584SApple OSS Distributions 			alignment = page_size;
245*1031c584SApple OSS Distributions 		}
246*1031c584SApple OSS Distributions 	}
247*1031c584SApple OSS Distributions 
248*1031c584SApple OSS Distributions 	if ((options & (kIOMemorySharingTypeMask | kIOMapCacheMask | kIOMemoryClearEncrypt)) && (alignment < page_size)) {
249*1031c584SApple OSS Distributions 		alignment = page_size;
250*1031c584SApple OSS Distributions 	}
251*1031c584SApple OSS Distributions 
252*1031c584SApple OSS Distributions 	if (alignment >= page_size) {
253*1031c584SApple OSS Distributions 		if (round_page_overflow(capacity, &capacity)) {
254*1031c584SApple OSS Distributions 			return false;
255*1031c584SApple OSS Distributions 		}
256*1031c584SApple OSS Distributions 	}
257*1031c584SApple OSS Distributions 
258*1031c584SApple OSS Distributions 	if (alignment > page_size) {
259*1031c584SApple OSS Distributions 		options |= kIOMemoryPhysicallyContiguous;
260*1031c584SApple OSS Distributions 	}
261*1031c584SApple OSS Distributions 
262*1031c584SApple OSS Distributions 	_alignment = alignment;
263*1031c584SApple OSS Distributions 
264*1031c584SApple OSS Distributions 	if ((capacity + alignment) < _capacity) {
265*1031c584SApple OSS Distributions 		return false;
266*1031c584SApple OSS Distributions 	}
267*1031c584SApple OSS Distributions 
268*1031c584SApple OSS Distributions 	if ((inTask != kernel_task) && !(options & kIOMemoryPageable)) {
269*1031c584SApple OSS Distributions 		return false;
270*1031c584SApple OSS Distributions 	}
271*1031c584SApple OSS Distributions 
272*1031c584SApple OSS Distributions 	bzero(&mapSpec, sizeof(mapSpec));
273*1031c584SApple OSS Distributions 	mapSpec.alignment      = _alignment;
274*1031c584SApple OSS Distributions 	mapSpec.numAddressBits = 64;
275*1031c584SApple OSS Distributions 	if (highestMask && mapped) {
276*1031c584SApple OSS Distributions 		if (highestMask <= 0xFFFFFFFF) {
277*1031c584SApple OSS Distributions 			mapSpec.numAddressBits = (uint8_t)(32 - __builtin_clz((unsigned int) highestMask));
278*1031c584SApple OSS Distributions 		} else {
279*1031c584SApple OSS Distributions 			mapSpec.numAddressBits = (uint8_t)(64 - __builtin_clz((unsigned int) (highestMask >> 32)));
280*1031c584SApple OSS Distributions 		}
281*1031c584SApple OSS Distributions 		highestMask = 0;
282*1031c584SApple OSS Distributions 	}
283*1031c584SApple OSS Distributions 
284*1031c584SApple OSS Distributions 	// set memory entry cache mode, pageable, purgeable
285*1031c584SApple OSS Distributions 	iomdOptions |= ((options & kIOMapCacheMask) >> kIOMapCacheShift) << kIOMemoryBufferCacheShift;
286*1031c584SApple OSS Distributions 	if (options & kIOMemoryPageable) {
287*1031c584SApple OSS Distributions 		if (_internalFlags & kInternalFlagGuardPages) {
288*1031c584SApple OSS Distributions 			printf("IOBMD: Unsupported use of guard pages with pageable memory.\n");
289*1031c584SApple OSS Distributions 			return false;
290*1031c584SApple OSS Distributions 		}
291*1031c584SApple OSS Distributions 		iomdOptions |= kIOMemoryBufferPageable;
292*1031c584SApple OSS Distributions 		if (options & kIOMemoryPurgeable) {
293*1031c584SApple OSS Distributions 			iomdOptions |= kIOMemoryBufferPurgeable;
294*1031c584SApple OSS Distributions 		}
295*1031c584SApple OSS Distributions 	} else {
296*1031c584SApple OSS Distributions 		// Buffer shouldn't auto prepare they should be prepared explicitly
297*1031c584SApple OSS Distributions 		// But it never was enforced so what are you going to do?
298*1031c584SApple OSS Distributions 		iomdOptions |= kIOMemoryAutoPrepare;
299*1031c584SApple OSS Distributions 
300*1031c584SApple OSS Distributions 		/* Allocate a wired-down buffer inside kernel space. */
301*1031c584SApple OSS Distributions 
302*1031c584SApple OSS Distributions 		bool contig = (0 != (options & kIOMemoryHostPhysicallyContiguous));
303*1031c584SApple OSS Distributions 
304*1031c584SApple OSS Distributions 		if (!contig && (0 != (options & kIOMemoryPhysicallyContiguous))) {
305*1031c584SApple OSS Distributions 			contig |= (!mapped);
306*1031c584SApple OSS Distributions 			contig |= (0 != (kIOMemoryMapperNone & options));
307*1031c584SApple OSS Distributions #if 0
308*1031c584SApple OSS Distributions 			// treat kIOMemoryPhysicallyContiguous as kIOMemoryHostPhysicallyContiguous for now
309*1031c584SApple OSS Distributions 			contig |= true;
310*1031c584SApple OSS Distributions #endif
311*1031c584SApple OSS Distributions 		}
312*1031c584SApple OSS Distributions 
313*1031c584SApple OSS Distributions 		mappedOrShared = (mapped || (0 != (kIOMemorySharingTypeMask & options)));
314*1031c584SApple OSS Distributions 		if (contig || highestMask || (alignment > page_size)) {
315*1031c584SApple OSS Distributions 			if (_internalFlags & kInternalFlagGuardPages) {
316*1031c584SApple OSS Distributions 				printf("IOBMD: Unsupported use of guard pages with physical mask or contiguous memory.\n");
317*1031c584SApple OSS Distributions 				return false;
318*1031c584SApple OSS Distributions 			}
319*1031c584SApple OSS Distributions 			_internalFlags |= kInternalFlagPhysical;
320*1031c584SApple OSS Distributions 			if (highestMask) {
321*1031c584SApple OSS Distributions 				_internalFlags |= kInternalFlagPageSized;
322*1031c584SApple OSS Distributions 				if (round_page_overflow(capacity, &capacity)) {
323*1031c584SApple OSS Distributions 					return false;
324*1031c584SApple OSS Distributions 				}
325*1031c584SApple OSS Distributions 			}
326*1031c584SApple OSS Distributions 			_buffer = (void *) IOKernelAllocateWithPhysicalRestrict(kheap,
327*1031c584SApple OSS Distributions 			    capacity, highestMask, alignment, contig);
328*1031c584SApple OSS Distributions 		} else if (_internalFlags & kInternalFlagGuardPages) {
329*1031c584SApple OSS Distributions 			vm_offset_t address = 0;
330*1031c584SApple OSS Distributions 			kern_return_t kr;
331*1031c584SApple OSS Distributions 			uintptr_t alignMask;
332*1031c584SApple OSS Distributions 			kma_flags_t kma_flags = (kma_flags_t) (KMA_GUARD_FIRST |
333*1031c584SApple OSS Distributions 			    KMA_GUARD_LAST | KMA_ZERO);
334*1031c584SApple OSS Distributions 
335*1031c584SApple OSS Distributions 			if (((uint32_t) alignment) != alignment) {
336*1031c584SApple OSS Distributions 				return false;
337*1031c584SApple OSS Distributions 			}
338*1031c584SApple OSS Distributions 			if (kheap == KHEAP_DATA_BUFFERS) {
339*1031c584SApple OSS Distributions 				kma_flags = (kma_flags_t) (kma_flags | KMA_DATA);
340*1031c584SApple OSS Distributions 			}
341*1031c584SApple OSS Distributions 
342*1031c584SApple OSS Distributions 			alignMask = (1UL << log2up((uint32_t) alignment)) - 1;
343*1031c584SApple OSS Distributions 			kr = kernel_memory_allocate(kernel_map, &address,
344*1031c584SApple OSS Distributions 			    capacity + page_size * 2, alignMask, kma_flags,
345*1031c584SApple OSS Distributions 			    IOMemoryTag(kernel_map));
346*1031c584SApple OSS Distributions 			if (kr != KERN_SUCCESS || address == 0) {
347*1031c584SApple OSS Distributions 				return false;
348*1031c584SApple OSS Distributions 			}
349*1031c584SApple OSS Distributions #if IOALLOCDEBUG
350*1031c584SApple OSS Distributions 			OSAddAtomicLong(capacity, &debug_iomalloc_size);
351*1031c584SApple OSS Distributions #endif
352*1031c584SApple OSS Distributions 			IOStatisticsAlloc(kIOStatisticsMallocAligned, capacity);
353*1031c584SApple OSS Distributions 			_buffer = (void *)(address + page_size);
354*1031c584SApple OSS Distributions #if defined(__x86_64__)
355*1031c584SApple OSS Distributions 		} else if (mappedOrShared
356*1031c584SApple OSS Distributions 		    && (capacity + alignment) <= (page_size - gIOPageAllocChunkBytes)) {
357*1031c584SApple OSS Distributions 			_internalFlags |= kInternalFlagPageAllocated;
358*1031c584SApple OSS Distributions 			_buffer         = (void *) iopa_alloc(&gIOBMDPageAllocator,
359*1031c584SApple OSS Distributions 			    &IOBMDPageProc, kheap, capacity, alignment);
360*1031c584SApple OSS Distributions 			if (_buffer) {
361*1031c584SApple OSS Distributions 				bzero(_buffer, capacity);
362*1031c584SApple OSS Distributions 				IOStatisticsAlloc(kIOStatisticsMallocAligned, capacity);
363*1031c584SApple OSS Distributions #if IOALLOCDEBUG
364*1031c584SApple OSS Distributions 				OSAddAtomicLong(capacity, &debug_iomalloc_size);
365*1031c584SApple OSS Distributions #endif
366*1031c584SApple OSS Distributions 			}
367*1031c584SApple OSS Distributions #endif /* defined(__x86_64__) */
368*1031c584SApple OSS Distributions 		} else if (alignment > 1) {
369*1031c584SApple OSS Distributions 			/* BEGIN IGNORE CODESTYLE */
370*1031c584SApple OSS Distributions 			__typed_allocators_ignore_push
371*1031c584SApple OSS Distributions 			_buffer = IOMallocAligned_internal(kheap, capacity, alignment,
372*1031c584SApple OSS Distributions 			    Z_ZERO_VM_TAG_BT_BIT);
373*1031c584SApple OSS Distributions 		} else {
374*1031c584SApple OSS Distributions 			_buffer = IOMalloc_internal(kheap, capacity, Z_ZERO_VM_TAG_BT_BIT);
375*1031c584SApple OSS Distributions 			__typed_allocators_ignore_pop
376*1031c584SApple OSS Distributions 			/* END IGNORE CODESTYLE */
377*1031c584SApple OSS Distributions 		}
378*1031c584SApple OSS Distributions 		if (!_buffer) {
379*1031c584SApple OSS Distributions 			return false;
380*1031c584SApple OSS Distributions 		}
381*1031c584SApple OSS Distributions 	}
382*1031c584SApple OSS Distributions 
383*1031c584SApple OSS Distributions 	if ((options & (kIOMemoryPageable | kIOMapCacheMask))) {
384*1031c584SApple OSS Distributions 		vm_size_t       size = round_page(capacity);
385*1031c584SApple OSS Distributions 
386*1031c584SApple OSS Distributions 		// initWithOptions will create memory entry
387*1031c584SApple OSS Distributions 		if (!withCopy) {
388*1031c584SApple OSS Distributions 			iomdOptions |= kIOMemoryPersistent;
389*1031c584SApple OSS Distributions 		}
390*1031c584SApple OSS Distributions 
391*1031c584SApple OSS Distributions 		if (options & kIOMemoryPageable) {
392*1031c584SApple OSS Distributions #if IOALLOCDEBUG
393*1031c584SApple OSS Distributions 			OSAddAtomicLong(size, &debug_iomallocpageable_size);
394*1031c584SApple OSS Distributions #endif
395*1031c584SApple OSS Distributions 			if (!withCopy) {
396*1031c584SApple OSS Distributions 				mapTask = inTask;
397*1031c584SApple OSS Distributions 			}
398*1031c584SApple OSS Distributions 			if (NULL == inTask) {
399*1031c584SApple OSS Distributions 				inTask = kernel_task;
400*1031c584SApple OSS Distributions 			}
401*1031c584SApple OSS Distributions 		} else if (options & kIOMapCacheMask) {
402*1031c584SApple OSS Distributions 			// Prefetch each page to put entries into the pmap
403*1031c584SApple OSS Distributions 			volatile UInt8 *    startAddr = (UInt8 *)_buffer;
404*1031c584SApple OSS Distributions 			volatile UInt8 *    endAddr   = (UInt8 *)_buffer + capacity;
405*1031c584SApple OSS Distributions 
406*1031c584SApple OSS Distributions 			while (startAddr < endAddr) {
407*1031c584SApple OSS Distributions 				UInt8 dummyVar = *startAddr;
408*1031c584SApple OSS Distributions 				(void) dummyVar;
409*1031c584SApple OSS Distributions 				startAddr += page_size;
410*1031c584SApple OSS Distributions 			}
411*1031c584SApple OSS Distributions 		}
412*1031c584SApple OSS Distributions 	}
413*1031c584SApple OSS Distributions 
414*1031c584SApple OSS Distributions 	_ranges.v64->address = (mach_vm_address_t) pgz_decode(_buffer, _capacity);
415*1031c584SApple OSS Distributions 	_ranges.v64->length  = _capacity;
416*1031c584SApple OSS Distributions 
417*1031c584SApple OSS Distributions 	if (!super::initWithOptions(_ranges.v64, 1, 0,
418*1031c584SApple OSS Distributions 	    inTask, iomdOptions, /* System mapper */ NULL)) {
419*1031c584SApple OSS Distributions 		return false;
420*1031c584SApple OSS Distributions 	}
421*1031c584SApple OSS Distributions 
422*1031c584SApple OSS Distributions 	_internalFlags |= kInternalFlagInit;
423*1031c584SApple OSS Distributions #if IOTRACKING
424*1031c584SApple OSS Distributions 	if (!(options & kIOMemoryPageable)) {
425*1031c584SApple OSS Distributions 		trackingAccumSize(capacity);
426*1031c584SApple OSS Distributions 	}
427*1031c584SApple OSS Distributions #endif /* IOTRACKING */
428*1031c584SApple OSS Distributions 
429*1031c584SApple OSS Distributions 	// give any system mapper the allocation params
430*1031c584SApple OSS Distributions 	if (kIOReturnSuccess != dmaCommandOperation(kIOMDAddDMAMapSpec,
431*1031c584SApple OSS Distributions 	    &mapSpec, sizeof(mapSpec))) {
432*1031c584SApple OSS Distributions 		return false;
433*1031c584SApple OSS Distributions 	}
434*1031c584SApple OSS Distributions 
435*1031c584SApple OSS Distributions 	if (mapTask) {
436*1031c584SApple OSS Distributions 		if (!reserved) {
437*1031c584SApple OSS Distributions 			reserved = IOMallocType(ExpansionData);
438*1031c584SApple OSS Distributions 			if (!reserved) {
439*1031c584SApple OSS Distributions 				return false;
440*1031c584SApple OSS Distributions 			}
441*1031c584SApple OSS Distributions 		}
442*1031c584SApple OSS Distributions 		reserved->map = createMappingInTask(mapTask, 0,
443*1031c584SApple OSS Distributions 		    kIOMapAnywhere | (options & kIOMapPrefault) | (options & kIOMapCacheMask), 0, 0).detach();
444*1031c584SApple OSS Distributions 		if (!reserved->map) {
445*1031c584SApple OSS Distributions 			_buffer = NULL;
446*1031c584SApple OSS Distributions 			return false;
447*1031c584SApple OSS Distributions 		}
448*1031c584SApple OSS Distributions 		release();  // map took a retain on this
449*1031c584SApple OSS Distributions 		reserved->map->retain();
450*1031c584SApple OSS Distributions 		removeMapping(reserved->map);
451*1031c584SApple OSS Distributions 		mach_vm_address_t buffer = reserved->map->getAddress();
452*1031c584SApple OSS Distributions 		_buffer = (void *) buffer;
453*1031c584SApple OSS Distributions 		if (kIOMemoryTypeVirtual64 == (kIOMemoryTypeMask & iomdOptions)) {
454*1031c584SApple OSS Distributions 			_ranges.v64->address = buffer;
455*1031c584SApple OSS Distributions 		}
456*1031c584SApple OSS Distributions 	}
457*1031c584SApple OSS Distributions 
458*1031c584SApple OSS Distributions 	setLength(_capacity);
459*1031c584SApple OSS Distributions 
460*1031c584SApple OSS Distributions 	return true;
461*1031c584SApple OSS Distributions }
462*1031c584SApple OSS Distributions 
463*1031c584SApple OSS Distributions bool
initControlWithPhysicalMask(task_t inTask,IOOptionBits options,mach_vm_size_t capacity,mach_vm_address_t alignment,mach_vm_address_t physicalMask)464*1031c584SApple OSS Distributions IOBufferMemoryDescriptor::initControlWithPhysicalMask(
465*1031c584SApple OSS Distributions 	task_t            inTask,
466*1031c584SApple OSS Distributions 	IOOptionBits      options,
467*1031c584SApple OSS Distributions 	mach_vm_size_t    capacity,
468*1031c584SApple OSS Distributions 	mach_vm_address_t alignment,
469*1031c584SApple OSS Distributions 	mach_vm_address_t physicalMask)
470*1031c584SApple OSS Distributions {
471*1031c584SApple OSS Distributions 	_internalFlags = kInternalFlagHasPointers;
472*1031c584SApple OSS Distributions 	return initWithPhysicalMask(inTask, options, capacity, alignment,
473*1031c584SApple OSS Distributions 	           physicalMask);
474*1031c584SApple OSS Distributions }
475*1031c584SApple OSS Distributions 
476*1031c584SApple OSS Distributions bool
initWithGuardPages(task_t inTask,IOOptionBits options,mach_vm_size_t capacity)477*1031c584SApple OSS Distributions IOBufferMemoryDescriptor::initWithGuardPages(
478*1031c584SApple OSS Distributions 	task_t            inTask,
479*1031c584SApple OSS Distributions 	IOOptionBits      options,
480*1031c584SApple OSS Distributions 	mach_vm_size_t    capacity)
481*1031c584SApple OSS Distributions {
482*1031c584SApple OSS Distributions 	mach_vm_size_t roundedCapacity;
483*1031c584SApple OSS Distributions 
484*1031c584SApple OSS Distributions 	_internalFlags = kInternalFlagGuardPages;
485*1031c584SApple OSS Distributions 
486*1031c584SApple OSS Distributions 	if (round_page_overflow(capacity, &roundedCapacity)) {
487*1031c584SApple OSS Distributions 		return false;
488*1031c584SApple OSS Distributions 	}
489*1031c584SApple OSS Distributions 
490*1031c584SApple OSS Distributions 	return initWithPhysicalMask(inTask, options, roundedCapacity, page_size,
491*1031c584SApple OSS Distributions 	           (mach_vm_address_t)0);
492*1031c584SApple OSS Distributions }
493*1031c584SApple OSS Distributions 
494*1031c584SApple OSS Distributions OSSharedPtr<IOBufferMemoryDescriptor>
inTaskWithOptions(task_t inTask,IOOptionBits options,vm_size_t capacity,vm_offset_t alignment)495*1031c584SApple OSS Distributions IOBufferMemoryDescriptor::inTaskWithOptions(
496*1031c584SApple OSS Distributions 	task_t       inTask,
497*1031c584SApple OSS Distributions 	IOOptionBits options,
498*1031c584SApple OSS Distributions 	vm_size_t    capacity,
499*1031c584SApple OSS Distributions 	vm_offset_t  alignment)
500*1031c584SApple OSS Distributions {
501*1031c584SApple OSS Distributions 	OSSharedPtr<IOBufferMemoryDescriptor> me = OSMakeShared<IOBufferMemoryDescriptor>();
502*1031c584SApple OSS Distributions 
503*1031c584SApple OSS Distributions 	if (me && !me->initWithPhysicalMask(inTask, options, capacity, alignment, 0)) {
504*1031c584SApple OSS Distributions 		me.reset();
505*1031c584SApple OSS Distributions 	}
506*1031c584SApple OSS Distributions 	return me;
507*1031c584SApple OSS Distributions }
508*1031c584SApple OSS Distributions 
509*1031c584SApple OSS Distributions OSSharedPtr<IOBufferMemoryDescriptor>
inTaskWithOptions(task_t inTask,IOOptionBits options,vm_size_t capacity,vm_offset_t alignment,uint32_t kernTag,uint32_t userTag)510*1031c584SApple OSS Distributions IOBufferMemoryDescriptor::inTaskWithOptions(
511*1031c584SApple OSS Distributions 	task_t       inTask,
512*1031c584SApple OSS Distributions 	IOOptionBits options,
513*1031c584SApple OSS Distributions 	vm_size_t    capacity,
514*1031c584SApple OSS Distributions 	vm_offset_t  alignment,
515*1031c584SApple OSS Distributions 	uint32_t     kernTag,
516*1031c584SApple OSS Distributions 	uint32_t     userTag)
517*1031c584SApple OSS Distributions {
518*1031c584SApple OSS Distributions 	OSSharedPtr<IOBufferMemoryDescriptor> me = OSMakeShared<IOBufferMemoryDescriptor>();
519*1031c584SApple OSS Distributions 
520*1031c584SApple OSS Distributions 	if (me) {
521*1031c584SApple OSS Distributions 		me->setVMTags(kernTag, userTag);
522*1031c584SApple OSS Distributions 
523*1031c584SApple OSS Distributions 		if (!me->initWithPhysicalMask(inTask, options, capacity, alignment, 0)) {
524*1031c584SApple OSS Distributions 			me.reset();
525*1031c584SApple OSS Distributions 		}
526*1031c584SApple OSS Distributions 	}
527*1031c584SApple OSS Distributions 	return me;
528*1031c584SApple OSS Distributions }
529*1031c584SApple OSS Distributions 
530*1031c584SApple OSS Distributions OSSharedPtr<IOBufferMemoryDescriptor>
inTaskWithPhysicalMask(task_t inTask,IOOptionBits options,mach_vm_size_t capacity,mach_vm_address_t physicalMask)531*1031c584SApple OSS Distributions IOBufferMemoryDescriptor::inTaskWithPhysicalMask(
532*1031c584SApple OSS Distributions 	task_t            inTask,
533*1031c584SApple OSS Distributions 	IOOptionBits      options,
534*1031c584SApple OSS Distributions 	mach_vm_size_t    capacity,
535*1031c584SApple OSS Distributions 	mach_vm_address_t physicalMask)
536*1031c584SApple OSS Distributions {
537*1031c584SApple OSS Distributions 	OSSharedPtr<IOBufferMemoryDescriptor> me = OSMakeShared<IOBufferMemoryDescriptor>();
538*1031c584SApple OSS Distributions 
539*1031c584SApple OSS Distributions 	if (me && !me->initWithPhysicalMask(inTask, options, capacity, 1, physicalMask)) {
540*1031c584SApple OSS Distributions 		me.reset();
541*1031c584SApple OSS Distributions 	}
542*1031c584SApple OSS Distributions 	return me;
543*1031c584SApple OSS Distributions }
544*1031c584SApple OSS Distributions 
545*1031c584SApple OSS Distributions OSSharedPtr<IOBufferMemoryDescriptor>
inTaskWithGuardPages(task_t inTask,IOOptionBits options,mach_vm_size_t capacity)546*1031c584SApple OSS Distributions IOBufferMemoryDescriptor::inTaskWithGuardPages(
547*1031c584SApple OSS Distributions 	task_t            inTask,
548*1031c584SApple OSS Distributions 	IOOptionBits      options,
549*1031c584SApple OSS Distributions 	mach_vm_size_t    capacity)
550*1031c584SApple OSS Distributions {
551*1031c584SApple OSS Distributions 	OSSharedPtr<IOBufferMemoryDescriptor> me = OSMakeShared<IOBufferMemoryDescriptor>();
552*1031c584SApple OSS Distributions 
553*1031c584SApple OSS Distributions 	if (me && !me->initWithGuardPages(inTask, options, capacity)) {
554*1031c584SApple OSS Distributions 		me.reset();
555*1031c584SApple OSS Distributions 	}
556*1031c584SApple OSS Distributions 	return me;
557*1031c584SApple OSS Distributions }
558*1031c584SApple OSS Distributions 
559*1031c584SApple OSS Distributions #ifndef __LP64__
560*1031c584SApple OSS Distributions bool
initWithOptions(IOOptionBits options,vm_size_t capacity,vm_offset_t alignment)561*1031c584SApple OSS Distributions IOBufferMemoryDescriptor::initWithOptions(
562*1031c584SApple OSS Distributions 	IOOptionBits options,
563*1031c584SApple OSS Distributions 	vm_size_t    capacity,
564*1031c584SApple OSS Distributions 	vm_offset_t  alignment)
565*1031c584SApple OSS Distributions {
566*1031c584SApple OSS Distributions 	return initWithPhysicalMask(kernel_task, options, capacity, alignment, (mach_vm_address_t)0);
567*1031c584SApple OSS Distributions }
568*1031c584SApple OSS Distributions #endif /* !__LP64__ */
569*1031c584SApple OSS Distributions 
570*1031c584SApple OSS Distributions OSSharedPtr<IOBufferMemoryDescriptor>
withOptions(IOOptionBits options,vm_size_t capacity,vm_offset_t alignment)571*1031c584SApple OSS Distributions IOBufferMemoryDescriptor::withOptions(
572*1031c584SApple OSS Distributions 	IOOptionBits options,
573*1031c584SApple OSS Distributions 	vm_size_t    capacity,
574*1031c584SApple OSS Distributions 	vm_offset_t  alignment)
575*1031c584SApple OSS Distributions {
576*1031c584SApple OSS Distributions 	OSSharedPtr<IOBufferMemoryDescriptor> me = OSMakeShared<IOBufferMemoryDescriptor>();
577*1031c584SApple OSS Distributions 
578*1031c584SApple OSS Distributions 	if (me && !me->initWithPhysicalMask(kernel_task, options, capacity, alignment, 0)) {
579*1031c584SApple OSS Distributions 		me.reset();
580*1031c584SApple OSS Distributions 	}
581*1031c584SApple OSS Distributions 	return me;
582*1031c584SApple OSS Distributions }
583*1031c584SApple OSS Distributions 
584*1031c584SApple OSS Distributions 
585*1031c584SApple OSS Distributions /*
586*1031c584SApple OSS Distributions  * withCapacity:
587*1031c584SApple OSS Distributions  *
588*1031c584SApple OSS Distributions  * Returns a new IOBufferMemoryDescriptor with a buffer large enough to
589*1031c584SApple OSS Distributions  * hold capacity bytes.  The descriptor's length is initially set to the capacity.
590*1031c584SApple OSS Distributions  */
591*1031c584SApple OSS Distributions OSSharedPtr<IOBufferMemoryDescriptor>
withCapacity(vm_size_t inCapacity,IODirection inDirection,bool inContiguous)592*1031c584SApple OSS Distributions IOBufferMemoryDescriptor::withCapacity(vm_size_t   inCapacity,
593*1031c584SApple OSS Distributions     IODirection inDirection,
594*1031c584SApple OSS Distributions     bool        inContiguous)
595*1031c584SApple OSS Distributions {
596*1031c584SApple OSS Distributions 	return IOBufferMemoryDescriptor::withOptions(
597*1031c584SApple OSS Distributions 		inDirection | kIOMemoryUnshared
598*1031c584SApple OSS Distributions 		| (inContiguous ? kIOMemoryPhysicallyContiguous : 0),
599*1031c584SApple OSS Distributions 		inCapacity, inContiguous ? inCapacity : 1 );
600*1031c584SApple OSS Distributions }
601*1031c584SApple OSS Distributions 
602*1031c584SApple OSS Distributions #ifndef __LP64__
603*1031c584SApple OSS Distributions /*
604*1031c584SApple OSS Distributions  * initWithBytes:
605*1031c584SApple OSS Distributions  *
606*1031c584SApple OSS Distributions  * Initialize a new IOBufferMemoryDescriptor preloaded with bytes (copied).
607*1031c584SApple OSS Distributions  * The descriptor's length and capacity are set to the input buffer's size.
608*1031c584SApple OSS Distributions  */
609*1031c584SApple OSS Distributions bool
initWithBytes(const void * inBytes,vm_size_t inLength,IODirection inDirection,bool inContiguous)610*1031c584SApple OSS Distributions IOBufferMemoryDescriptor::initWithBytes(const void * inBytes,
611*1031c584SApple OSS Distributions     vm_size_t    inLength,
612*1031c584SApple OSS Distributions     IODirection  inDirection,
613*1031c584SApple OSS Distributions     bool         inContiguous)
614*1031c584SApple OSS Distributions {
615*1031c584SApple OSS Distributions 	if (!initWithPhysicalMask(kernel_task, inDirection | kIOMemoryUnshared
616*1031c584SApple OSS Distributions 	    | (inContiguous ? kIOMemoryPhysicallyContiguous : 0),
617*1031c584SApple OSS Distributions 	    inLength, inLength, (mach_vm_address_t)0)) {
618*1031c584SApple OSS Distributions 		return false;
619*1031c584SApple OSS Distributions 	}
620*1031c584SApple OSS Distributions 
621*1031c584SApple OSS Distributions 	// start out with no data
622*1031c584SApple OSS Distributions 	setLength(0);
623*1031c584SApple OSS Distributions 
624*1031c584SApple OSS Distributions 	if (!appendBytes(inBytes, inLength)) {
625*1031c584SApple OSS Distributions 		return false;
626*1031c584SApple OSS Distributions 	}
627*1031c584SApple OSS Distributions 
628*1031c584SApple OSS Distributions 	return true;
629*1031c584SApple OSS Distributions }
630*1031c584SApple OSS Distributions #endif /* !__LP64__ */
631*1031c584SApple OSS Distributions 
632*1031c584SApple OSS Distributions /*
633*1031c584SApple OSS Distributions  * withBytes:
634*1031c584SApple OSS Distributions  *
635*1031c584SApple OSS Distributions  * Returns a new IOBufferMemoryDescriptor preloaded with bytes (copied).
636*1031c584SApple OSS Distributions  * The descriptor's length and capacity are set to the input buffer's size.
637*1031c584SApple OSS Distributions  */
638*1031c584SApple OSS Distributions OSSharedPtr<IOBufferMemoryDescriptor>
withBytes(const void * inBytes,vm_size_t inLength,IODirection inDirection,bool inContiguous)639*1031c584SApple OSS Distributions IOBufferMemoryDescriptor::withBytes(const void * inBytes,
640*1031c584SApple OSS Distributions     vm_size_t    inLength,
641*1031c584SApple OSS Distributions     IODirection  inDirection,
642*1031c584SApple OSS Distributions     bool         inContiguous)
643*1031c584SApple OSS Distributions {
644*1031c584SApple OSS Distributions 	OSSharedPtr<IOBufferMemoryDescriptor> me = OSMakeShared<IOBufferMemoryDescriptor>();
645*1031c584SApple OSS Distributions 	mach_vm_address_t alignment;
646*1031c584SApple OSS Distributions 
647*1031c584SApple OSS Distributions 	alignment = (inLength <= page_size) ? inLength : page_size;
648*1031c584SApple OSS Distributions 	if (me && !me->initWithPhysicalMask(
649*1031c584SApple OSS Distributions 		    kernel_task, inDirection | kIOMemoryUnshared
650*1031c584SApple OSS Distributions 		    | (inContiguous ? kIOMemoryPhysicallyContiguous : 0),
651*1031c584SApple OSS Distributions 		    inLength, alignment, 0 )) {
652*1031c584SApple OSS Distributions 		me.reset();
653*1031c584SApple OSS Distributions 	}
654*1031c584SApple OSS Distributions 
655*1031c584SApple OSS Distributions 	if (me) {
656*1031c584SApple OSS Distributions 		// start out with no data
657*1031c584SApple OSS Distributions 		me->setLength(0);
658*1031c584SApple OSS Distributions 
659*1031c584SApple OSS Distributions 		if (!me->appendBytes(inBytes, inLength)) {
660*1031c584SApple OSS Distributions 			me.reset();
661*1031c584SApple OSS Distributions 		}
662*1031c584SApple OSS Distributions 	}
663*1031c584SApple OSS Distributions 	return me;
664*1031c584SApple OSS Distributions }
665*1031c584SApple OSS Distributions 
666*1031c584SApple OSS Distributions /*
667*1031c584SApple OSS Distributions  * free:
668*1031c584SApple OSS Distributions  *
669*1031c584SApple OSS Distributions  * Free resources
670*1031c584SApple OSS Distributions  */
671*1031c584SApple OSS Distributions void
free()672*1031c584SApple OSS Distributions IOBufferMemoryDescriptor::free()
673*1031c584SApple OSS Distributions {
674*1031c584SApple OSS Distributions 	// Cache all of the relevant information on the stack for use
675*1031c584SApple OSS Distributions 	// after we call super::free()!
676*1031c584SApple OSS Distributions 	IOOptionBits     flags         = _flags;
677*1031c584SApple OSS Distributions 	IOOptionBits     internalFlags = _internalFlags;
678*1031c584SApple OSS Distributions 	IOOptionBits     options   = _options;
679*1031c584SApple OSS Distributions 	vm_size_t        size      = _capacity;
680*1031c584SApple OSS Distributions 	void *           buffer    = _buffer;
681*1031c584SApple OSS Distributions 	IOMemoryMap *    map       = NULL;
682*1031c584SApple OSS Distributions 	IOAddressRange * range     = _ranges.v64;
683*1031c584SApple OSS Distributions 	vm_offset_t      alignment = _alignment;
684*1031c584SApple OSS Distributions 	kalloc_heap_t    kheap     = KHEAP_DATA_BUFFERS;
685*1031c584SApple OSS Distributions 	vm_size_t        rsize;
686*1031c584SApple OSS Distributions 
687*1031c584SApple OSS Distributions 	if (alignment >= page_size) {
688*1031c584SApple OSS Distributions 		if (!round_page_overflow(size, &rsize)) {
689*1031c584SApple OSS Distributions 			size = rsize;
690*1031c584SApple OSS Distributions 		}
691*1031c584SApple OSS Distributions 	}
692*1031c584SApple OSS Distributions 
693*1031c584SApple OSS Distributions 	if (reserved) {
694*1031c584SApple OSS Distributions 		map = reserved->map;
695*1031c584SApple OSS Distributions 		IOFreeType(reserved, ExpansionData);
696*1031c584SApple OSS Distributions 		if (map) {
697*1031c584SApple OSS Distributions 			map->release();
698*1031c584SApple OSS Distributions 		}
699*1031c584SApple OSS Distributions 	}
700*1031c584SApple OSS Distributions 
701*1031c584SApple OSS Distributions 	if ((options & kIOMemoryPageable)
702*1031c584SApple OSS Distributions 	    || (kInternalFlagPageSized & internalFlags)) {
703*1031c584SApple OSS Distributions 		if (!round_page_overflow(size, &rsize)) {
704*1031c584SApple OSS Distributions 			size = rsize;
705*1031c584SApple OSS Distributions 		}
706*1031c584SApple OSS Distributions 	}
707*1031c584SApple OSS Distributions 
708*1031c584SApple OSS Distributions 	if (internalFlags & kInternalFlagHasPointers) {
709*1031c584SApple OSS Distributions 		kheap = KHEAP_IOBMD_CONTROL;
710*1031c584SApple OSS Distributions 	}
711*1031c584SApple OSS Distributions 
712*1031c584SApple OSS Distributions #if IOTRACKING
713*1031c584SApple OSS Distributions 	if (!(options & kIOMemoryPageable)
714*1031c584SApple OSS Distributions 	    && buffer
715*1031c584SApple OSS Distributions 	    && (kInternalFlagInit & _internalFlags)) {
716*1031c584SApple OSS Distributions 		trackingAccumSize(-size);
717*1031c584SApple OSS Distributions 	}
718*1031c584SApple OSS Distributions #endif /* IOTRACKING */
719*1031c584SApple OSS Distributions 
720*1031c584SApple OSS Distributions 	/* super::free may unwire - deallocate buffer afterwards */
721*1031c584SApple OSS Distributions 	super::free();
722*1031c584SApple OSS Distributions 
723*1031c584SApple OSS Distributions 	if (options & kIOMemoryPageable) {
724*1031c584SApple OSS Distributions #if IOALLOCDEBUG
725*1031c584SApple OSS Distributions 		OSAddAtomicLong(-size, &debug_iomallocpageable_size);
726*1031c584SApple OSS Distributions #endif
727*1031c584SApple OSS Distributions 	} else if (buffer) {
728*1031c584SApple OSS Distributions 		if (kInternalFlagPhysical & internalFlags) {
729*1031c584SApple OSS Distributions 			IOKernelFreePhysical(kheap, (mach_vm_address_t) buffer, size);
730*1031c584SApple OSS Distributions 		} else if (kInternalFlagPageAllocated & internalFlags) {
731*1031c584SApple OSS Distributions #if defined(__x86_64__)
732*1031c584SApple OSS Distributions 			uintptr_t page;
733*1031c584SApple OSS Distributions 			page = iopa_free(&gIOBMDPageAllocator, (uintptr_t) buffer, size);
734*1031c584SApple OSS Distributions 			if (page) {
735*1031c584SApple OSS Distributions 				kmem_free(kernel_map, page, page_size);
736*1031c584SApple OSS Distributions 			}
737*1031c584SApple OSS Distributions #if IOALLOCDEBUG
738*1031c584SApple OSS Distributions 			OSAddAtomicLong(-size, &debug_iomalloc_size);
739*1031c584SApple OSS Distributions #endif
740*1031c584SApple OSS Distributions 			IOStatisticsAlloc(kIOStatisticsFreeAligned, size);
741*1031c584SApple OSS Distributions #else /* !defined(__x86_64__) */
742*1031c584SApple OSS Distributions 			/* should be unreachable */
743*1031c584SApple OSS Distributions 			panic("Attempting to free IOBMD with page allocated flag");
744*1031c584SApple OSS Distributions #endif /* defined(__x86_64__) */
745*1031c584SApple OSS Distributions 		} else if (kInternalFlagGuardPages & internalFlags) {
746*1031c584SApple OSS Distributions 			vm_offset_t allocation = (vm_offset_t)buffer - page_size;
747*1031c584SApple OSS Distributions 			kmem_free(kernel_map, allocation, size + page_size * 2);
748*1031c584SApple OSS Distributions #if IOALLOCDEBUG
749*1031c584SApple OSS Distributions 			OSAddAtomicLong(-size, &debug_iomalloc_size);
750*1031c584SApple OSS Distributions #endif
751*1031c584SApple OSS Distributions 			IOStatisticsAlloc(kIOStatisticsFreeAligned, size);
752*1031c584SApple OSS Distributions 		} else if (alignment > 1) {
753*1031c584SApple OSS Distributions 			/* BEGIN IGNORE CODESTYLE */
754*1031c584SApple OSS Distributions 			__typed_allocators_ignore_push
755*1031c584SApple OSS Distributions 			IOFreeAligned_internal(kheap, buffer, size);
756*1031c584SApple OSS Distributions 		} else {
757*1031c584SApple OSS Distributions 			IOFree_internal(kheap, buffer, size);
758*1031c584SApple OSS Distributions 			__typed_allocators_ignore_pop
759*1031c584SApple OSS Distributions 			/* END IGNORE CODESTYLE */
760*1031c584SApple OSS Distributions 		}
761*1031c584SApple OSS Distributions 	}
762*1031c584SApple OSS Distributions 	if (range && (kIOMemoryAsReference & flags)) {
763*1031c584SApple OSS Distributions 		IOFreeType(range, IOAddressRange);
764*1031c584SApple OSS Distributions 	}
765*1031c584SApple OSS Distributions }
766*1031c584SApple OSS Distributions 
767*1031c584SApple OSS Distributions /*
768*1031c584SApple OSS Distributions  * getCapacity:
769*1031c584SApple OSS Distributions  *
770*1031c584SApple OSS Distributions  * Get the buffer capacity
771*1031c584SApple OSS Distributions  */
772*1031c584SApple OSS Distributions vm_size_t
getCapacity() const773*1031c584SApple OSS Distributions IOBufferMemoryDescriptor::getCapacity() const
774*1031c584SApple OSS Distributions {
775*1031c584SApple OSS Distributions 	return _capacity;
776*1031c584SApple OSS Distributions }
777*1031c584SApple OSS Distributions 
778*1031c584SApple OSS Distributions /*
779*1031c584SApple OSS Distributions  * setLength:
780*1031c584SApple OSS Distributions  *
781*1031c584SApple OSS Distributions  * Change the buffer length of the memory descriptor.  When a new buffer
782*1031c584SApple OSS Distributions  * is created, the initial length of the buffer is set to be the same as
783*1031c584SApple OSS Distributions  * the capacity.  The length can be adjusted via setLength for a shorter
784*1031c584SApple OSS Distributions  * transfer (there is no need to create more buffer descriptors when you
785*1031c584SApple OSS Distributions  * can reuse an existing one, even for different transfer sizes).   Note
786*1031c584SApple OSS Distributions  * that the specified length must not exceed the capacity of the buffer.
787*1031c584SApple OSS Distributions  */
788*1031c584SApple OSS Distributions void
setLength(vm_size_t length)789*1031c584SApple OSS Distributions IOBufferMemoryDescriptor::setLength(vm_size_t length)
790*1031c584SApple OSS Distributions {
791*1031c584SApple OSS Distributions 	assert(length <= _capacity);
792*1031c584SApple OSS Distributions 	if (length > _capacity) {
793*1031c584SApple OSS Distributions 		return;
794*1031c584SApple OSS Distributions 	}
795*1031c584SApple OSS Distributions 
796*1031c584SApple OSS Distributions 	_length = length;
797*1031c584SApple OSS Distributions 	_ranges.v64->length = length;
798*1031c584SApple OSS Distributions }
799*1031c584SApple OSS Distributions 
800*1031c584SApple OSS Distributions /*
801*1031c584SApple OSS Distributions  * setDirection:
802*1031c584SApple OSS Distributions  *
803*1031c584SApple OSS Distributions  * Change the direction of the transfer.  This method allows one to redirect
804*1031c584SApple OSS Distributions  * the descriptor's transfer direction.  This eliminates the need to destroy
805*1031c584SApple OSS Distributions  * and create new buffers when different transfer directions are needed.
806*1031c584SApple OSS Distributions  */
807*1031c584SApple OSS Distributions void
setDirection(IODirection direction)808*1031c584SApple OSS Distributions IOBufferMemoryDescriptor::setDirection(IODirection direction)
809*1031c584SApple OSS Distributions {
810*1031c584SApple OSS Distributions 	_flags = (_flags & ~kIOMemoryDirectionMask) | direction;
811*1031c584SApple OSS Distributions #ifndef __LP64__
812*1031c584SApple OSS Distributions 	_direction = (IODirection) (_flags & kIOMemoryDirectionMask);
813*1031c584SApple OSS Distributions #endif /* !__LP64__ */
814*1031c584SApple OSS Distributions }
815*1031c584SApple OSS Distributions 
816*1031c584SApple OSS Distributions /*
817*1031c584SApple OSS Distributions  * appendBytes:
818*1031c584SApple OSS Distributions  *
819*1031c584SApple OSS Distributions  * Add some data to the end of the buffer.  This method automatically
820*1031c584SApple OSS Distributions  * maintains the memory descriptor buffer length.  Note that appendBytes
821*1031c584SApple OSS Distributions  * will not copy past the end of the memory descriptor's current capacity.
822*1031c584SApple OSS Distributions  */
823*1031c584SApple OSS Distributions bool
appendBytes(const void * bytes,vm_size_t withLength)824*1031c584SApple OSS Distributions IOBufferMemoryDescriptor::appendBytes(const void * bytes, vm_size_t withLength)
825*1031c584SApple OSS Distributions {
826*1031c584SApple OSS Distributions 	vm_size_t   actualBytesToCopy = min(withLength, _capacity - _length);
827*1031c584SApple OSS Distributions 	IOByteCount offset;
828*1031c584SApple OSS Distributions 
829*1031c584SApple OSS Distributions 	assert(_length <= _capacity);
830*1031c584SApple OSS Distributions 
831*1031c584SApple OSS Distributions 	offset = _length;
832*1031c584SApple OSS Distributions 	_length += actualBytesToCopy;
833*1031c584SApple OSS Distributions 	_ranges.v64->length += actualBytesToCopy;
834*1031c584SApple OSS Distributions 
835*1031c584SApple OSS Distributions 	if (_task == kernel_task) {
836*1031c584SApple OSS Distributions 		bcopy(/* from */ bytes, (void *)(_ranges.v64->address + offset),
837*1031c584SApple OSS Distributions 		    actualBytesToCopy);
838*1031c584SApple OSS Distributions 	} else {
839*1031c584SApple OSS Distributions 		writeBytes(offset, bytes, actualBytesToCopy);
840*1031c584SApple OSS Distributions 	}
841*1031c584SApple OSS Distributions 
842*1031c584SApple OSS Distributions 	return true;
843*1031c584SApple OSS Distributions }
844*1031c584SApple OSS Distributions 
845*1031c584SApple OSS Distributions /*
846*1031c584SApple OSS Distributions  * getBytesNoCopy:
847*1031c584SApple OSS Distributions  *
848*1031c584SApple OSS Distributions  * Return the virtual address of the beginning of the buffer
849*1031c584SApple OSS Distributions  */
850*1031c584SApple OSS Distributions void *
getBytesNoCopy()851*1031c584SApple OSS Distributions IOBufferMemoryDescriptor::getBytesNoCopy()
852*1031c584SApple OSS Distributions {
853*1031c584SApple OSS Distributions 	if (kIOMemoryTypePhysical64 == (_flags & kIOMemoryTypeMask)) {
854*1031c584SApple OSS Distributions 		return _buffer;
855*1031c584SApple OSS Distributions 	} else {
856*1031c584SApple OSS Distributions 		return (void *)_ranges.v64->address;
857*1031c584SApple OSS Distributions 	}
858*1031c584SApple OSS Distributions }
859*1031c584SApple OSS Distributions 
860*1031c584SApple OSS Distributions 
861*1031c584SApple OSS Distributions /*
862*1031c584SApple OSS Distributions  * getBytesNoCopy:
863*1031c584SApple OSS Distributions  *
864*1031c584SApple OSS Distributions  * Return the virtual address of an offset from the beginning of the buffer
865*1031c584SApple OSS Distributions  */
866*1031c584SApple OSS Distributions void *
getBytesNoCopy(vm_size_t start,vm_size_t withLength)867*1031c584SApple OSS Distributions IOBufferMemoryDescriptor::getBytesNoCopy(vm_size_t start, vm_size_t withLength)
868*1031c584SApple OSS Distributions {
869*1031c584SApple OSS Distributions 	IOVirtualAddress address;
870*1031c584SApple OSS Distributions 
871*1031c584SApple OSS Distributions 	if ((start + withLength) < start) {
872*1031c584SApple OSS Distributions 		return NULL;
873*1031c584SApple OSS Distributions 	}
874*1031c584SApple OSS Distributions 
875*1031c584SApple OSS Distributions 	if (kIOMemoryTypePhysical64 == (_flags & kIOMemoryTypeMask)) {
876*1031c584SApple OSS Distributions 		address = (IOVirtualAddress) _buffer;
877*1031c584SApple OSS Distributions 	} else {
878*1031c584SApple OSS Distributions 		address = _ranges.v64->address;
879*1031c584SApple OSS Distributions 	}
880*1031c584SApple OSS Distributions 
881*1031c584SApple OSS Distributions 	if (start < _length && (start + withLength) <= _length) {
882*1031c584SApple OSS Distributions 		return (void *)(address + start);
883*1031c584SApple OSS Distributions 	}
884*1031c584SApple OSS Distributions 	return NULL;
885*1031c584SApple OSS Distributions }
886*1031c584SApple OSS Distributions 
887*1031c584SApple OSS Distributions #ifndef __LP64__
888*1031c584SApple OSS Distributions void *
getVirtualSegment(IOByteCount offset,IOByteCount * lengthOfSegment)889*1031c584SApple OSS Distributions IOBufferMemoryDescriptor::getVirtualSegment(IOByteCount offset,
890*1031c584SApple OSS Distributions     IOByteCount * lengthOfSegment)
891*1031c584SApple OSS Distributions {
892*1031c584SApple OSS Distributions 	void * bytes = getBytesNoCopy(offset, 0);
893*1031c584SApple OSS Distributions 
894*1031c584SApple OSS Distributions 	if (bytes && lengthOfSegment) {
895*1031c584SApple OSS Distributions 		*lengthOfSegment = _length - offset;
896*1031c584SApple OSS Distributions 	}
897*1031c584SApple OSS Distributions 
898*1031c584SApple OSS Distributions 	return bytes;
899*1031c584SApple OSS Distributions }
900*1031c584SApple OSS Distributions #endif /* !__LP64__ */
901*1031c584SApple OSS Distributions 
902*1031c584SApple OSS Distributions #ifdef __LP64__
903*1031c584SApple OSS Distributions OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor, 0);
904*1031c584SApple OSS Distributions OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor, 1);
905*1031c584SApple OSS Distributions #else /* !__LP64__ */
906*1031c584SApple OSS Distributions OSMetaClassDefineReservedUsedX86(IOBufferMemoryDescriptor, 0);
907*1031c584SApple OSS Distributions OSMetaClassDefineReservedUsedX86(IOBufferMemoryDescriptor, 1);
908*1031c584SApple OSS Distributions #endif /* !__LP64__ */
909*1031c584SApple OSS Distributions OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor, 2);
910*1031c584SApple OSS Distributions OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor, 3);
911*1031c584SApple OSS Distributions OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor, 4);
912*1031c584SApple OSS Distributions OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor, 5);
913*1031c584SApple OSS Distributions OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor, 6);
914*1031c584SApple OSS Distributions OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor, 7);
915*1031c584SApple OSS Distributions OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor, 8);
916*1031c584SApple OSS Distributions OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor, 9);
917*1031c584SApple OSS Distributions OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor, 10);
918*1031c584SApple OSS Distributions OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor, 11);
919*1031c584SApple OSS Distributions OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor, 12);
920*1031c584SApple OSS Distributions OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor, 13);
921*1031c584SApple OSS Distributions OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor, 14);
922*1031c584SApple OSS Distributions OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor, 15);
923