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