xref: /xnu-10002.81.5/iokit/Kernel/IOMultiMemoryDescriptor.cpp (revision 5e3eaea39dcf651e66cb99ba7d70e32cc4a99587)
1*5e3eaea3SApple OSS Distributions /*
2*5e3eaea3SApple OSS Distributions  * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
3*5e3eaea3SApple OSS Distributions  *
4*5e3eaea3SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*5e3eaea3SApple OSS Distributions  *
6*5e3eaea3SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*5e3eaea3SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*5e3eaea3SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*5e3eaea3SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*5e3eaea3SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*5e3eaea3SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*5e3eaea3SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*5e3eaea3SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*5e3eaea3SApple OSS Distributions  *
15*5e3eaea3SApple OSS Distributions  * Please obtain a copy of the License at
16*5e3eaea3SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*5e3eaea3SApple OSS Distributions  *
18*5e3eaea3SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*5e3eaea3SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*5e3eaea3SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*5e3eaea3SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*5e3eaea3SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*5e3eaea3SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*5e3eaea3SApple OSS Distributions  * limitations under the License.
25*5e3eaea3SApple OSS Distributions  *
26*5e3eaea3SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*5e3eaea3SApple OSS Distributions  */
28*5e3eaea3SApple OSS Distributions 
29*5e3eaea3SApple OSS Distributions #include <IOKit/IOLib.h>
30*5e3eaea3SApple OSS Distributions #include <IOKit/IOMultiMemoryDescriptor.h>
31*5e3eaea3SApple OSS Distributions 
32*5e3eaea3SApple OSS Distributions #define super IOMemoryDescriptor
OSDefineMetaClassAndStructors(IOMultiMemoryDescriptor,IOMemoryDescriptor)33*5e3eaea3SApple OSS Distributions OSDefineMetaClassAndStructors(IOMultiMemoryDescriptor, IOMemoryDescriptor)
34*5e3eaea3SApple OSS Distributions 
35*5e3eaea3SApple OSS Distributions IOMultiMemoryDescriptor * IOMultiMemoryDescriptor::withDescriptors(
36*5e3eaea3SApple OSS Distributions 	IOMemoryDescriptor * *descriptors,
37*5e3eaea3SApple OSS Distributions 	UInt32                withCount,
38*5e3eaea3SApple OSS Distributions 	IODirection           withDirection,
39*5e3eaea3SApple OSS Distributions 	bool                  asReference )
40*5e3eaea3SApple OSS Distributions {
41*5e3eaea3SApple OSS Distributions 	//
42*5e3eaea3SApple OSS Distributions 	// Create a new IOMultiMemoryDescriptor.  The "buffer" is made up of several
43*5e3eaea3SApple OSS Distributions 	// memory descriptors, that are to be chained end-to-end to make up a single
44*5e3eaea3SApple OSS Distributions 	// memory descriptor.
45*5e3eaea3SApple OSS Distributions 	//
46*5e3eaea3SApple OSS Distributions 	// Passing the ranges as a reference will avoid an extra allocation.
47*5e3eaea3SApple OSS Distributions 	//
48*5e3eaea3SApple OSS Distributions 
49*5e3eaea3SApple OSS Distributions 	IOMultiMemoryDescriptor * me = new IOMultiMemoryDescriptor;
50*5e3eaea3SApple OSS Distributions 
51*5e3eaea3SApple OSS Distributions 	if (me && me->initWithDescriptors(
52*5e3eaea3SApple OSS Distributions 		    /* descriptors   */ descriptors,
53*5e3eaea3SApple OSS Distributions 		    /* withCount     */ withCount,
54*5e3eaea3SApple OSS Distributions 		    /* withDirection */ withDirection,
55*5e3eaea3SApple OSS Distributions 		    /* asReference   */ asReference ) == false) {
56*5e3eaea3SApple OSS Distributions 		me->release();
57*5e3eaea3SApple OSS Distributions 		me = NULL;
58*5e3eaea3SApple OSS Distributions 	}
59*5e3eaea3SApple OSS Distributions 
60*5e3eaea3SApple OSS Distributions 	return me;
61*5e3eaea3SApple OSS Distributions }
62*5e3eaea3SApple OSS Distributions 
63*5e3eaea3SApple OSS Distributions bool
initWithDescriptors(IOMemoryDescriptor ** descriptors,UInt32 withCount,IODirection withDirection,bool asReference)64*5e3eaea3SApple OSS Distributions IOMultiMemoryDescriptor::initWithDescriptors(
65*5e3eaea3SApple OSS Distributions 	IOMemoryDescriptor ** descriptors,
66*5e3eaea3SApple OSS Distributions 	UInt32                withCount,
67*5e3eaea3SApple OSS Distributions 	IODirection           withDirection,
68*5e3eaea3SApple OSS Distributions 	bool                  asReference )
69*5e3eaea3SApple OSS Distributions {
70*5e3eaea3SApple OSS Distributions 	unsigned index;
71*5e3eaea3SApple OSS Distributions 	IOOptionBits copyFlags;
72*5e3eaea3SApple OSS Distributions 	//
73*5e3eaea3SApple OSS Distributions 	// Initialize an IOMultiMemoryDescriptor. The "buffer" is made up of several
74*5e3eaea3SApple OSS Distributions 	// memory descriptors, that are to be chained end-to-end to make up a single
75*5e3eaea3SApple OSS Distributions 	// memory descriptor.
76*5e3eaea3SApple OSS Distributions 	//
77*5e3eaea3SApple OSS Distributions 	// Passing the ranges as a reference will avoid an extra allocation.
78*5e3eaea3SApple OSS Distributions 	//
79*5e3eaea3SApple OSS Distributions 
80*5e3eaea3SApple OSS Distributions 	assert(descriptors);
81*5e3eaea3SApple OSS Distributions 
82*5e3eaea3SApple OSS Distributions 	// Release existing descriptors, if any
83*5e3eaea3SApple OSS Distributions 	if (_descriptors) {
84*5e3eaea3SApple OSS Distributions 		for (unsigned index = 0; index < _descriptorsCount; index++) {
85*5e3eaea3SApple OSS Distributions 			_descriptors[index]->release();
86*5e3eaea3SApple OSS Distributions 		}
87*5e3eaea3SApple OSS Distributions 
88*5e3eaea3SApple OSS Distributions 		if (_descriptorsIsAllocated) {
89*5e3eaea3SApple OSS Distributions 			IODelete(_descriptors, IOMemoryDescriptor *, _descriptorsCount);
90*5e3eaea3SApple OSS Distributions 		}
91*5e3eaea3SApple OSS Distributions 	} else {
92*5e3eaea3SApple OSS Distributions 		// Ask our superclass' opinion.
93*5e3eaea3SApple OSS Distributions 		if (super::init() == false) {
94*5e3eaea3SApple OSS Distributions 			return false;
95*5e3eaea3SApple OSS Distributions 		}
96*5e3eaea3SApple OSS Distributions 	}
97*5e3eaea3SApple OSS Distributions 
98*5e3eaea3SApple OSS Distributions 	// Initialize our minimal state.
99*5e3eaea3SApple OSS Distributions 
100*5e3eaea3SApple OSS Distributions 	_descriptors            = NULL;
101*5e3eaea3SApple OSS Distributions 	_descriptorsCount       = withCount;
102*5e3eaea3SApple OSS Distributions 	_descriptorsIsAllocated = asReference ? false : true;
103*5e3eaea3SApple OSS Distributions 	_flags                  = withDirection;
104*5e3eaea3SApple OSS Distributions #ifndef __LP64__
105*5e3eaea3SApple OSS Distributions 	_direction              = (IODirection) (_flags & kIOMemoryDirectionMask);
106*5e3eaea3SApple OSS Distributions #endif /* !__LP64__ */
107*5e3eaea3SApple OSS Distributions 	_length                 = 0;
108*5e3eaea3SApple OSS Distributions 	_mappings               = NULL;
109*5e3eaea3SApple OSS Distributions 	_tag                    = 0;
110*5e3eaea3SApple OSS Distributions 
111*5e3eaea3SApple OSS Distributions 	if (asReference) {
112*5e3eaea3SApple OSS Distributions 		_descriptors = descriptors;
113*5e3eaea3SApple OSS Distributions 	} else {
114*5e3eaea3SApple OSS Distributions 		_descriptors = IONew(IOMemoryDescriptor *, withCount);
115*5e3eaea3SApple OSS Distributions 		if (_descriptors == NULL) {
116*5e3eaea3SApple OSS Distributions 			return false;
117*5e3eaea3SApple OSS Distributions 		}
118*5e3eaea3SApple OSS Distributions 
119*5e3eaea3SApple OSS Distributions 		bcopy( /* from  */ descriptors,
120*5e3eaea3SApple OSS Distributions 		    /* to    */ _descriptors,
121*5e3eaea3SApple OSS Distributions 		    /* bytes */ withCount * sizeof(IOMemoryDescriptor *));
122*5e3eaea3SApple OSS Distributions 	}
123*5e3eaea3SApple OSS Distributions 
124*5e3eaea3SApple OSS Distributions 	for (index = 0; index < withCount; index++) {
125*5e3eaea3SApple OSS Distributions 		descriptors[index]->retain();
126*5e3eaea3SApple OSS Distributions 		_length += descriptors[index]->getLength();
127*5e3eaea3SApple OSS Distributions 		if (_tag == 0) {
128*5e3eaea3SApple OSS Distributions 			_tag = descriptors[index]->getTag();
129*5e3eaea3SApple OSS Distributions 		}
130*5e3eaea3SApple OSS Distributions 		assert(descriptors[index]->getDirection() ==
131*5e3eaea3SApple OSS Distributions 		    (withDirection & kIOMemoryDirectionMask));
132*5e3eaea3SApple OSS Distributions 	}
133*5e3eaea3SApple OSS Distributions 
134*5e3eaea3SApple OSS Distributions 	enum { kCopyFlags = kIOMemoryBufferPageable };
135*5e3eaea3SApple OSS Distributions 	copyFlags = 0;
136*5e3eaea3SApple OSS Distributions 	for (index = 0; index < withCount; index++) {
137*5e3eaea3SApple OSS Distributions 		if (!index) {
138*5e3eaea3SApple OSS Distributions 			copyFlags =  (kCopyFlags & descriptors[index]->_flags);
139*5e3eaea3SApple OSS Distributions 		} else if (copyFlags != (kCopyFlags & descriptors[index]->_flags)) {
140*5e3eaea3SApple OSS Distributions 			break;
141*5e3eaea3SApple OSS Distributions 		}
142*5e3eaea3SApple OSS Distributions 	}
143*5e3eaea3SApple OSS Distributions 	if (index < withCount) {
144*5e3eaea3SApple OSS Distributions 		return false;
145*5e3eaea3SApple OSS Distributions 	}
146*5e3eaea3SApple OSS Distributions 	_flags |= copyFlags;
147*5e3eaea3SApple OSS Distributions 
148*5e3eaea3SApple OSS Distributions 	return true;
149*5e3eaea3SApple OSS Distributions }
150*5e3eaea3SApple OSS Distributions 
151*5e3eaea3SApple OSS Distributions void
free()152*5e3eaea3SApple OSS Distributions IOMultiMemoryDescriptor::free()
153*5e3eaea3SApple OSS Distributions {
154*5e3eaea3SApple OSS Distributions 	//
155*5e3eaea3SApple OSS Distributions 	// Free all of this object's outstanding resources.
156*5e3eaea3SApple OSS Distributions 	//
157*5e3eaea3SApple OSS Distributions 
158*5e3eaea3SApple OSS Distributions 	if (_descriptors) {
159*5e3eaea3SApple OSS Distributions 		for (unsigned index = 0; index < _descriptorsCount; index++) {
160*5e3eaea3SApple OSS Distributions 			_descriptors[index]->release();
161*5e3eaea3SApple OSS Distributions 		}
162*5e3eaea3SApple OSS Distributions 
163*5e3eaea3SApple OSS Distributions 		if (_descriptorsIsAllocated) {
164*5e3eaea3SApple OSS Distributions 			IODelete(_descriptors, IOMemoryDescriptor *, _descriptorsCount);
165*5e3eaea3SApple OSS Distributions 		}
166*5e3eaea3SApple OSS Distributions 	}
167*5e3eaea3SApple OSS Distributions 
168*5e3eaea3SApple OSS Distributions 	super::free();
169*5e3eaea3SApple OSS Distributions }
170*5e3eaea3SApple OSS Distributions 
171*5e3eaea3SApple OSS Distributions IOReturn
prepare(IODirection forDirection)172*5e3eaea3SApple OSS Distributions IOMultiMemoryDescriptor::prepare(IODirection forDirection)
173*5e3eaea3SApple OSS Distributions {
174*5e3eaea3SApple OSS Distributions 	//
175*5e3eaea3SApple OSS Distributions 	// Prepare the memory for an I/O transfer.
176*5e3eaea3SApple OSS Distributions 	//
177*5e3eaea3SApple OSS Distributions 	// This involves paging in the memory and wiring it down for the duration
178*5e3eaea3SApple OSS Distributions 	// of the transfer.  The complete() method finishes the processing of the
179*5e3eaea3SApple OSS Distributions 	// memory after the I/O transfer finishes.
180*5e3eaea3SApple OSS Distributions 	//
181*5e3eaea3SApple OSS Distributions 
182*5e3eaea3SApple OSS Distributions 	unsigned index;
183*5e3eaea3SApple OSS Distributions 	IOReturn status = kIOReturnInternalError;
184*5e3eaea3SApple OSS Distributions 	IOReturn statusUndo;
185*5e3eaea3SApple OSS Distributions 
186*5e3eaea3SApple OSS Distributions 	if (forDirection == kIODirectionNone) {
187*5e3eaea3SApple OSS Distributions 		forDirection = getDirection();
188*5e3eaea3SApple OSS Distributions 	}
189*5e3eaea3SApple OSS Distributions 
190*5e3eaea3SApple OSS Distributions 	for (index = 0; index < _descriptorsCount; index++) {
191*5e3eaea3SApple OSS Distributions 		status = _descriptors[index]->prepare(forDirection);
192*5e3eaea3SApple OSS Distributions 		if (status != kIOReturnSuccess) {
193*5e3eaea3SApple OSS Distributions 			break;
194*5e3eaea3SApple OSS Distributions 		}
195*5e3eaea3SApple OSS Distributions 	}
196*5e3eaea3SApple OSS Distributions 
197*5e3eaea3SApple OSS Distributions 	if (status != kIOReturnSuccess) {
198*5e3eaea3SApple OSS Distributions 		for (unsigned indexUndo = 0; indexUndo < index; indexUndo++) {
199*5e3eaea3SApple OSS Distributions 			statusUndo = _descriptors[indexUndo]->complete(forDirection);
200*5e3eaea3SApple OSS Distributions 			assert(statusUndo == kIOReturnSuccess);
201*5e3eaea3SApple OSS Distributions 		}
202*5e3eaea3SApple OSS Distributions 	}
203*5e3eaea3SApple OSS Distributions 
204*5e3eaea3SApple OSS Distributions 	return status;
205*5e3eaea3SApple OSS Distributions }
206*5e3eaea3SApple OSS Distributions 
207*5e3eaea3SApple OSS Distributions IOReturn
complete(IODirection forDirection)208*5e3eaea3SApple OSS Distributions IOMultiMemoryDescriptor::complete(IODirection forDirection)
209*5e3eaea3SApple OSS Distributions {
210*5e3eaea3SApple OSS Distributions 	//
211*5e3eaea3SApple OSS Distributions 	// Complete processing of the memory after an I/O transfer finishes.
212*5e3eaea3SApple OSS Distributions 	//
213*5e3eaea3SApple OSS Distributions 	// This method shouldn't be called unless a prepare() was previously issued;
214*5e3eaea3SApple OSS Distributions 	// the prepare() and complete() must occur in pairs, before and after an I/O
215*5e3eaea3SApple OSS Distributions 	// transfer.
216*5e3eaea3SApple OSS Distributions 	//
217*5e3eaea3SApple OSS Distributions 
218*5e3eaea3SApple OSS Distributions 	IOReturn status;
219*5e3eaea3SApple OSS Distributions 	IOReturn statusFinal = kIOReturnSuccess;
220*5e3eaea3SApple OSS Distributions 
221*5e3eaea3SApple OSS Distributions 	if (forDirection == kIODirectionNone) {
222*5e3eaea3SApple OSS Distributions 		forDirection = getDirection();
223*5e3eaea3SApple OSS Distributions 	}
224*5e3eaea3SApple OSS Distributions 
225*5e3eaea3SApple OSS Distributions 	for (unsigned index = 0; index < _descriptorsCount; index++) {
226*5e3eaea3SApple OSS Distributions 		status = _descriptors[index]->complete(forDirection);
227*5e3eaea3SApple OSS Distributions 		if (status != kIOReturnSuccess) {
228*5e3eaea3SApple OSS Distributions 			statusFinal = status;
229*5e3eaea3SApple OSS Distributions 		}
230*5e3eaea3SApple OSS Distributions 		assert(status == kIOReturnSuccess);
231*5e3eaea3SApple OSS Distributions 	}
232*5e3eaea3SApple OSS Distributions 
233*5e3eaea3SApple OSS Distributions 	return statusFinal;
234*5e3eaea3SApple OSS Distributions }
235*5e3eaea3SApple OSS Distributions 
236*5e3eaea3SApple OSS Distributions addr64_t
getPhysicalSegment(IOByteCount offset,IOByteCount * length,IOOptionBits options)237*5e3eaea3SApple OSS Distributions IOMultiMemoryDescriptor::getPhysicalSegment(IOByteCount   offset,
238*5e3eaea3SApple OSS Distributions     IOByteCount * length,
239*5e3eaea3SApple OSS Distributions     IOOptionBits  options)
240*5e3eaea3SApple OSS Distributions {
241*5e3eaea3SApple OSS Distributions 	//
242*5e3eaea3SApple OSS Distributions 	// This method returns the physical address of the byte at the given offset
243*5e3eaea3SApple OSS Distributions 	// into the memory,  and optionally the length of the physically contiguous
244*5e3eaea3SApple OSS Distributions 	// segment from that offset.
245*5e3eaea3SApple OSS Distributions 	//
246*5e3eaea3SApple OSS Distributions 
247*5e3eaea3SApple OSS Distributions 	assert(offset <= _length);
248*5e3eaea3SApple OSS Distributions 
249*5e3eaea3SApple OSS Distributions 	for (unsigned index = 0; index < _descriptorsCount; index++) {
250*5e3eaea3SApple OSS Distributions 		if (offset < _descriptors[index]->getLength()) {
251*5e3eaea3SApple OSS Distributions 			return _descriptors[index]->getPhysicalSegment(offset, length, options);
252*5e3eaea3SApple OSS Distributions 		}
253*5e3eaea3SApple OSS Distributions 		offset -= _descriptors[index]->getLength();
254*5e3eaea3SApple OSS Distributions 	}
255*5e3eaea3SApple OSS Distributions 
256*5e3eaea3SApple OSS Distributions 	if (length) {
257*5e3eaea3SApple OSS Distributions 		*length = 0;
258*5e3eaea3SApple OSS Distributions 	}
259*5e3eaea3SApple OSS Distributions 
260*5e3eaea3SApple OSS Distributions 	return 0;
261*5e3eaea3SApple OSS Distributions }
262*5e3eaea3SApple OSS Distributions 
263*5e3eaea3SApple OSS Distributions #include "IOKitKernelInternal.h"
264*5e3eaea3SApple OSS Distributions 
265*5e3eaea3SApple OSS Distributions IOReturn
doMap(vm_map_t __addressMap,IOVirtualAddress * __address,IOOptionBits options,IOByteCount __offset,IOByteCount __length)266*5e3eaea3SApple OSS Distributions IOMultiMemoryDescriptor::doMap(vm_map_t           __addressMap,
267*5e3eaea3SApple OSS Distributions     IOVirtualAddress *  __address,
268*5e3eaea3SApple OSS Distributions     IOOptionBits       options,
269*5e3eaea3SApple OSS Distributions     IOByteCount        __offset,
270*5e3eaea3SApple OSS Distributions     IOByteCount        __length)
271*5e3eaea3SApple OSS Distributions {
272*5e3eaea3SApple OSS Distributions 	IOMemoryMap *     mapping = (IOMemoryMap *) *__address;
273*5e3eaea3SApple OSS Distributions 	vm_map_t          map     = mapping->fAddressMap;
274*5e3eaea3SApple OSS Distributions 	mach_vm_size_t    offset  = mapping->fOffset;
275*5e3eaea3SApple OSS Distributions 	mach_vm_size_t    length  = mapping->fLength;
276*5e3eaea3SApple OSS Distributions 	mach_vm_address_t address = mapping->fAddress;
277*5e3eaea3SApple OSS Distributions 
278*5e3eaea3SApple OSS Distributions 	kern_return_t     err;
279*5e3eaea3SApple OSS Distributions 	IOOptionBits      subOptions;
280*5e3eaea3SApple OSS Distributions 	mach_vm_size_t    mapOffset;
281*5e3eaea3SApple OSS Distributions 	mach_vm_size_t    bytesRemaining, chunk;
282*5e3eaea3SApple OSS Distributions 	mach_vm_address_t nextAddress;
283*5e3eaea3SApple OSS Distributions 	IOMemoryDescriptorMapAllocRef ref;
284*5e3eaea3SApple OSS Distributions 	vm_prot_t                     prot;
285*5e3eaea3SApple OSS Distributions 
286*5e3eaea3SApple OSS Distributions 	do{
287*5e3eaea3SApple OSS Distributions 		prot = VM_PROT_READ;
288*5e3eaea3SApple OSS Distributions 		if (!(kIOMapReadOnly & options)) {
289*5e3eaea3SApple OSS Distributions 			prot |= VM_PROT_WRITE;
290*5e3eaea3SApple OSS Distributions 		}
291*5e3eaea3SApple OSS Distributions 
292*5e3eaea3SApple OSS Distributions 		if (kIOMapOverwrite & options) {
293*5e3eaea3SApple OSS Distributions 			if ((map == kernel_map) && (kIOMemoryBufferPageable & _flags)) {
294*5e3eaea3SApple OSS Distributions 				map = IOPageableMapForAddress(address);
295*5e3eaea3SApple OSS Distributions 			}
296*5e3eaea3SApple OSS Distributions 			err = KERN_SUCCESS;
297*5e3eaea3SApple OSS Distributions 		} else {
298*5e3eaea3SApple OSS Distributions 			ref.map     = map;
299*5e3eaea3SApple OSS Distributions 			ref.tag     = IOMemoryTag(map);
300*5e3eaea3SApple OSS Distributions 			ref.options = options;
301*5e3eaea3SApple OSS Distributions 			ref.size    = length;
302*5e3eaea3SApple OSS Distributions 			ref.prot    = prot;
303*5e3eaea3SApple OSS Distributions 			if (options & kIOMapAnywhere) {
304*5e3eaea3SApple OSS Distributions 				// vm_map looks for addresses above here, even when VM_FLAGS_ANYWHERE
305*5e3eaea3SApple OSS Distributions 				ref.mapped = 0;
306*5e3eaea3SApple OSS Distributions 			} else {
307*5e3eaea3SApple OSS Distributions 				ref.mapped = mapping->fAddress;
308*5e3eaea3SApple OSS Distributions 			}
309*5e3eaea3SApple OSS Distributions 
310*5e3eaea3SApple OSS Distributions 			if ((ref.map == kernel_map) && (kIOMemoryBufferPageable & _flags)) {
311*5e3eaea3SApple OSS Distributions 				err = IOIteratePageableMaps(ref.size, &IOMemoryDescriptorMapAlloc, &ref);
312*5e3eaea3SApple OSS Distributions 			} else {
313*5e3eaea3SApple OSS Distributions 				err = IOMemoryDescriptorMapAlloc(ref.map, &ref);
314*5e3eaea3SApple OSS Distributions 			}
315*5e3eaea3SApple OSS Distributions 
316*5e3eaea3SApple OSS Distributions 			if (KERN_SUCCESS != err) {
317*5e3eaea3SApple OSS Distributions 				break;
318*5e3eaea3SApple OSS Distributions 			}
319*5e3eaea3SApple OSS Distributions 
320*5e3eaea3SApple OSS Distributions 			address = ref.mapped;
321*5e3eaea3SApple OSS Distributions 			mapping->fAddress = address;
322*5e3eaea3SApple OSS Distributions 		}
323*5e3eaea3SApple OSS Distributions 
324*5e3eaea3SApple OSS Distributions 		mapOffset = offset;
325*5e3eaea3SApple OSS Distributions 		bytesRemaining = length;
326*5e3eaea3SApple OSS Distributions 		nextAddress = address;
327*5e3eaea3SApple OSS Distributions 		assert(mapOffset <= _length);
328*5e3eaea3SApple OSS Distributions 		subOptions = (options & ~kIOMapAnywhere) | kIOMapOverwrite;
329*5e3eaea3SApple OSS Distributions 
330*5e3eaea3SApple OSS Distributions 		for (unsigned index = 0; bytesRemaining && (index < _descriptorsCount); index++) {
331*5e3eaea3SApple OSS Distributions 			chunk = _descriptors[index]->getLength();
332*5e3eaea3SApple OSS Distributions 			if (mapOffset >= chunk) {
333*5e3eaea3SApple OSS Distributions 				mapOffset -= chunk;
334*5e3eaea3SApple OSS Distributions 				continue;
335*5e3eaea3SApple OSS Distributions 			}
336*5e3eaea3SApple OSS Distributions 			chunk -= mapOffset;
337*5e3eaea3SApple OSS Distributions 			if (chunk > bytesRemaining) {
338*5e3eaea3SApple OSS Distributions 				chunk = bytesRemaining;
339*5e3eaea3SApple OSS Distributions 			}
340*5e3eaea3SApple OSS Distributions 			IOMemoryMap * subMap;
341*5e3eaea3SApple OSS Distributions 			subMap = _descriptors[index]->createMappingInTask(mapping->fAddressTask, nextAddress, subOptions, mapOffset, chunk );
342*5e3eaea3SApple OSS Distributions 			if (!subMap) {
343*5e3eaea3SApple OSS Distributions 				break;
344*5e3eaea3SApple OSS Distributions 			}
345*5e3eaea3SApple OSS Distributions 			subMap->release(); // kIOMapOverwrite means it will not deallocate
346*5e3eaea3SApple OSS Distributions 
347*5e3eaea3SApple OSS Distributions 			bytesRemaining -= chunk;
348*5e3eaea3SApple OSS Distributions 			nextAddress += chunk;
349*5e3eaea3SApple OSS Distributions 			mapOffset = 0;
350*5e3eaea3SApple OSS Distributions 		}
351*5e3eaea3SApple OSS Distributions 		if (bytesRemaining) {
352*5e3eaea3SApple OSS Distributions 			err = kIOReturnUnderrun;
353*5e3eaea3SApple OSS Distributions 		}
354*5e3eaea3SApple OSS Distributions 	}while (false);
355*5e3eaea3SApple OSS Distributions 
356*5e3eaea3SApple OSS Distributions 	if (kIOReturnSuccess == err) {
357*5e3eaea3SApple OSS Distributions #if IOTRACKING
358*5e3eaea3SApple OSS Distributions 		IOTrackingAddUser(gIOMapTracking, &mapping->fTracking, mapping->fLength);
359*5e3eaea3SApple OSS Distributions #endif
360*5e3eaea3SApple OSS Distributions 	}
361*5e3eaea3SApple OSS Distributions 
362*5e3eaea3SApple OSS Distributions 	return err;
363*5e3eaea3SApple OSS Distributions }
364*5e3eaea3SApple OSS Distributions 
365*5e3eaea3SApple OSS Distributions IOReturn
setPurgeable(IOOptionBits newState,IOOptionBits * oldState)366*5e3eaea3SApple OSS Distributions IOMultiMemoryDescriptor::setPurgeable( IOOptionBits newState,
367*5e3eaea3SApple OSS Distributions     IOOptionBits * oldState )
368*5e3eaea3SApple OSS Distributions {
369*5e3eaea3SApple OSS Distributions 	IOReturn     err;
370*5e3eaea3SApple OSS Distributions 	IOOptionBits totalState, state;
371*5e3eaea3SApple OSS Distributions 
372*5e3eaea3SApple OSS Distributions 	totalState = kIOMemoryPurgeableNonVolatile;
373*5e3eaea3SApple OSS Distributions 	err = kIOReturnSuccess;
374*5e3eaea3SApple OSS Distributions 	for (unsigned index = 0; index < _descriptorsCount; index++) {
375*5e3eaea3SApple OSS Distributions 		err = _descriptors[index]->setPurgeable(newState, &state);
376*5e3eaea3SApple OSS Distributions 		if (kIOReturnSuccess != err) {
377*5e3eaea3SApple OSS Distributions 			break;
378*5e3eaea3SApple OSS Distributions 		}
379*5e3eaea3SApple OSS Distributions 
380*5e3eaea3SApple OSS Distributions 		if (kIOMemoryPurgeableEmpty == state) {
381*5e3eaea3SApple OSS Distributions 			totalState = kIOMemoryPurgeableEmpty;
382*5e3eaea3SApple OSS Distributions 		} else if (kIOMemoryPurgeableEmpty == totalState) {
383*5e3eaea3SApple OSS Distributions 			continue;
384*5e3eaea3SApple OSS Distributions 		} else if (kIOMemoryPurgeableVolatile == totalState) {
385*5e3eaea3SApple OSS Distributions 			continue;
386*5e3eaea3SApple OSS Distributions 		} else if (kIOMemoryPurgeableVolatile == state) {
387*5e3eaea3SApple OSS Distributions 			totalState = kIOMemoryPurgeableVolatile;
388*5e3eaea3SApple OSS Distributions 		} else {
389*5e3eaea3SApple OSS Distributions 			totalState = kIOMemoryPurgeableNonVolatile;
390*5e3eaea3SApple OSS Distributions 		}
391*5e3eaea3SApple OSS Distributions 	}
392*5e3eaea3SApple OSS Distributions 	if (oldState) {
393*5e3eaea3SApple OSS Distributions 		*oldState = totalState;
394*5e3eaea3SApple OSS Distributions 	}
395*5e3eaea3SApple OSS Distributions 
396*5e3eaea3SApple OSS Distributions 	return err;
397*5e3eaea3SApple OSS Distributions }
398*5e3eaea3SApple OSS Distributions 
399*5e3eaea3SApple OSS Distributions IOReturn
setOwnership(task_t newOwner,int newLedgerTag,IOOptionBits newLedgerOptions)400*5e3eaea3SApple OSS Distributions IOMultiMemoryDescriptor::setOwnership( task_t newOwner,
401*5e3eaea3SApple OSS Distributions     int newLedgerTag,
402*5e3eaea3SApple OSS Distributions     IOOptionBits newLedgerOptions )
403*5e3eaea3SApple OSS Distributions {
404*5e3eaea3SApple OSS Distributions 	IOReturn     err;
405*5e3eaea3SApple OSS Distributions 
406*5e3eaea3SApple OSS Distributions 	if (iokit_iomd_setownership_enabled == FALSE) {
407*5e3eaea3SApple OSS Distributions 		return kIOReturnUnsupported;
408*5e3eaea3SApple OSS Distributions 	}
409*5e3eaea3SApple OSS Distributions 
410*5e3eaea3SApple OSS Distributions 	err = kIOReturnSuccess;
411*5e3eaea3SApple OSS Distributions 	for (unsigned index = 0; index < _descriptorsCount; index++) {
412*5e3eaea3SApple OSS Distributions 		err = _descriptors[index]->setOwnership(newOwner, newLedgerTag, newLedgerOptions);
413*5e3eaea3SApple OSS Distributions 		if (kIOReturnSuccess != err) {
414*5e3eaea3SApple OSS Distributions 			break;
415*5e3eaea3SApple OSS Distributions 		}
416*5e3eaea3SApple OSS Distributions 	}
417*5e3eaea3SApple OSS Distributions 
418*5e3eaea3SApple OSS Distributions 	return err;
419*5e3eaea3SApple OSS Distributions }
420*5e3eaea3SApple OSS Distributions 
421*5e3eaea3SApple OSS Distributions IOReturn
getPageCounts(IOByteCount * pResidentPageCount,IOByteCount * pDirtyPageCount)422*5e3eaea3SApple OSS Distributions IOMultiMemoryDescriptor::getPageCounts(IOByteCount * pResidentPageCount,
423*5e3eaea3SApple OSS Distributions     IOByteCount * pDirtyPageCount)
424*5e3eaea3SApple OSS Distributions {
425*5e3eaea3SApple OSS Distributions 	IOReturn    err;
426*5e3eaea3SApple OSS Distributions 	IOByteCount totalResidentPageCount, totalDirtyPageCount;
427*5e3eaea3SApple OSS Distributions 	IOByteCount residentPageCount, dirtyPageCount;
428*5e3eaea3SApple OSS Distributions 
429*5e3eaea3SApple OSS Distributions 	err = kIOReturnSuccess;
430*5e3eaea3SApple OSS Distributions 	totalResidentPageCount = totalDirtyPageCount = 0;
431*5e3eaea3SApple OSS Distributions 	for (unsigned index = 0; index < _descriptorsCount; index++) {
432*5e3eaea3SApple OSS Distributions 		err = _descriptors[index]->getPageCounts(&residentPageCount, &dirtyPageCount);
433*5e3eaea3SApple OSS Distributions 		if (kIOReturnSuccess != err) {
434*5e3eaea3SApple OSS Distributions 			break;
435*5e3eaea3SApple OSS Distributions 		}
436*5e3eaea3SApple OSS Distributions 		totalResidentPageCount += residentPageCount;
437*5e3eaea3SApple OSS Distributions 		totalDirtyPageCount    += dirtyPageCount;
438*5e3eaea3SApple OSS Distributions 	}
439*5e3eaea3SApple OSS Distributions 
440*5e3eaea3SApple OSS Distributions 	if (pResidentPageCount) {
441*5e3eaea3SApple OSS Distributions 		*pResidentPageCount = totalResidentPageCount;
442*5e3eaea3SApple OSS Distributions 	}
443*5e3eaea3SApple OSS Distributions 	if (pDirtyPageCount) {
444*5e3eaea3SApple OSS Distributions 		*pDirtyPageCount = totalDirtyPageCount;
445*5e3eaea3SApple OSS Distributions 	}
446*5e3eaea3SApple OSS Distributions 
447*5e3eaea3SApple OSS Distributions 	return err;
448*5e3eaea3SApple OSS Distributions }
449*5e3eaea3SApple OSS Distributions 
450*5e3eaea3SApple OSS Distributions uint64_t
getPreparationID(void)451*5e3eaea3SApple OSS Distributions IOMultiMemoryDescriptor::getPreparationID( void )
452*5e3eaea3SApple OSS Distributions {
453*5e3eaea3SApple OSS Distributions 	if (!super::getKernelReserved()) {
454*5e3eaea3SApple OSS Distributions 		return kIOPreparationIDUnsupported;
455*5e3eaea3SApple OSS Distributions 	}
456*5e3eaea3SApple OSS Distributions 
457*5e3eaea3SApple OSS Distributions 	for (unsigned index = 0; index < _descriptorsCount; index++) {
458*5e3eaea3SApple OSS Distributions 		uint64_t preparationID = _descriptors[index]->getPreparationID();
459*5e3eaea3SApple OSS Distributions 
460*5e3eaea3SApple OSS Distributions 		if (preparationID == kIOPreparationIDUnsupported) {
461*5e3eaea3SApple OSS Distributions 			return kIOPreparationIDUnsupported;
462*5e3eaea3SApple OSS Distributions 		}
463*5e3eaea3SApple OSS Distributions 
464*5e3eaea3SApple OSS Distributions 		if (preparationID == kIOPreparationIDUnprepared) {
465*5e3eaea3SApple OSS Distributions 			return kIOPreparationIDUnprepared;
466*5e3eaea3SApple OSS Distributions 		}
467*5e3eaea3SApple OSS Distributions 	}
468*5e3eaea3SApple OSS Distributions 
469*5e3eaea3SApple OSS Distributions 	super::setPreparationID();
470*5e3eaea3SApple OSS Distributions 
471*5e3eaea3SApple OSS Distributions 	return super::getPreparationID();
472*5e3eaea3SApple OSS Distributions }
473