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