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