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