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