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