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