xref: /xnu-8796.121.2/iokit/Kernel/IOSubMemoryDescriptor.cpp (revision c54f35ca767986246321eb901baf8f5ff7923f6a)
1*c54f35caSApple OSS Distributions /*
2*c54f35caSApple OSS Distributions  * Copyright (c) 1998-2007 Apple 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/IOSubMemoryDescriptor.h>
30*c54f35caSApple OSS Distributions #include <IOKit/IOLib.h>
31*c54f35caSApple OSS Distributions 
32*c54f35caSApple OSS Distributions #include "IOKitKernelInternal.h"
33*c54f35caSApple OSS Distributions 
34*c54f35caSApple OSS Distributions #define super IOMemoryDescriptor
35*c54f35caSApple OSS Distributions 
OSDefineMetaClassAndStructors(IOSubMemoryDescriptor,IOMemoryDescriptor)36*c54f35caSApple OSS Distributions OSDefineMetaClassAndStructors(IOSubMemoryDescriptor, IOMemoryDescriptor)
37*c54f35caSApple OSS Distributions 
38*c54f35caSApple OSS Distributions IOReturn
39*c54f35caSApple OSS Distributions IOSubMemoryDescriptor::redirect( task_t safeTask, bool doRedirect )
40*c54f35caSApple OSS Distributions {
41*c54f35caSApple OSS Distributions #ifdef __LP64__
42*c54f35caSApple OSS Distributions 	super::redirect( safeTask, doRedirect );
43*c54f35caSApple OSS Distributions #endif /* __LP64__ */
44*c54f35caSApple OSS Distributions 	return _parent->redirect( safeTask, doRedirect );
45*c54f35caSApple OSS Distributions }
46*c54f35caSApple OSS Distributions 
47*c54f35caSApple OSS Distributions IOSubMemoryDescriptor *
withSubRange(IOMemoryDescriptor * of,IOByteCount offset,IOByteCount length,IOOptionBits options)48*c54f35caSApple OSS Distributions IOSubMemoryDescriptor::withSubRange(IOMemoryDescriptor *        of,
49*c54f35caSApple OSS Distributions     IOByteCount             offset,
50*c54f35caSApple OSS Distributions     IOByteCount             length,
51*c54f35caSApple OSS Distributions     IOOptionBits            options)
52*c54f35caSApple OSS Distributions {
53*c54f35caSApple OSS Distributions 	IOSubMemoryDescriptor *self = new IOSubMemoryDescriptor;
54*c54f35caSApple OSS Distributions 
55*c54f35caSApple OSS Distributions 	if (self && !self->initSubRange(of, offset, length, (IODirection) options)) {
56*c54f35caSApple OSS Distributions 		self->release();
57*c54f35caSApple OSS Distributions 		self = NULL;
58*c54f35caSApple OSS Distributions 	}
59*c54f35caSApple OSS Distributions 	return self;
60*c54f35caSApple OSS Distributions }
61*c54f35caSApple OSS Distributions 
62*c54f35caSApple OSS Distributions bool
initSubRange(IOMemoryDescriptor * parent,IOByteCount offset,IOByteCount length,IODirection direction)63*c54f35caSApple OSS Distributions IOSubMemoryDescriptor::initSubRange( IOMemoryDescriptor * parent,
64*c54f35caSApple OSS Distributions     IOByteCount offset, IOByteCount length,
65*c54f35caSApple OSS Distributions     IODirection direction )
66*c54f35caSApple OSS Distributions {
67*c54f35caSApple OSS Distributions 	if (parent && ((offset + length) > parent->getLength())) {
68*c54f35caSApple OSS Distributions 		return false;
69*c54f35caSApple OSS Distributions 	}
70*c54f35caSApple OSS Distributions 
71*c54f35caSApple OSS Distributions 	/*
72*c54f35caSApple OSS Distributions 	 * We can check the _parent instance variable before having ever set it
73*c54f35caSApple OSS Distributions 	 * to an initial value because I/O Kit guarantees that all our instance
74*c54f35caSApple OSS Distributions 	 * variables are zeroed on an object's allocation.
75*c54f35caSApple OSS Distributions 	 */
76*c54f35caSApple OSS Distributions 
77*c54f35caSApple OSS Distributions 	if (!_parent) {
78*c54f35caSApple OSS Distributions 		if (!super::init()) {
79*c54f35caSApple OSS Distributions 			return false;
80*c54f35caSApple OSS Distributions 		}
81*c54f35caSApple OSS Distributions 	} else {
82*c54f35caSApple OSS Distributions 		/*
83*c54f35caSApple OSS Distributions 		 * An existing memory descriptor is being retargeted to
84*c54f35caSApple OSS Distributions 		 * point to somewhere else.  Clean up our present state.
85*c54f35caSApple OSS Distributions 		 */
86*c54f35caSApple OSS Distributions 
87*c54f35caSApple OSS Distributions 		_parent->release();
88*c54f35caSApple OSS Distributions 	}
89*c54f35caSApple OSS Distributions 
90*c54f35caSApple OSS Distributions 	if (parent) {
91*c54f35caSApple OSS Distributions 		parent->retain();
92*c54f35caSApple OSS Distributions 		_tag    = parent->getTag();
93*c54f35caSApple OSS Distributions 	} else {
94*c54f35caSApple OSS Distributions 		_tag    = 0;
95*c54f35caSApple OSS Distributions 	}
96*c54f35caSApple OSS Distributions 	_parent     = parent;
97*c54f35caSApple OSS Distributions 	_start      = offset;
98*c54f35caSApple OSS Distributions 	_length     = length;
99*c54f35caSApple OSS Distributions 	_flags      = direction;
100*c54f35caSApple OSS Distributions 	_flags |= kIOMemoryThreadSafe;
101*c54f35caSApple OSS Distributions 
102*c54f35caSApple OSS Distributions #ifndef __LP64__
103*c54f35caSApple OSS Distributions 	_direction  = (IODirection) (_flags & kIOMemoryDirectionMask);
104*c54f35caSApple OSS Distributions #endif /* !__LP64__ */
105*c54f35caSApple OSS Distributions 
106*c54f35caSApple OSS Distributions 	return true;
107*c54f35caSApple OSS Distributions }
108*c54f35caSApple OSS Distributions 
109*c54f35caSApple OSS Distributions void
free(void)110*c54f35caSApple OSS Distributions IOSubMemoryDescriptor::free( void )
111*c54f35caSApple OSS Distributions {
112*c54f35caSApple OSS Distributions 	if (_parent) {
113*c54f35caSApple OSS Distributions 		_parent->release();
114*c54f35caSApple OSS Distributions 	}
115*c54f35caSApple OSS Distributions 
116*c54f35caSApple OSS Distributions 	super::free();
117*c54f35caSApple OSS Distributions }
118*c54f35caSApple OSS Distributions 
119*c54f35caSApple OSS Distributions addr64_t
getPhysicalSegment(IOByteCount offset,IOByteCount * length,IOOptionBits options)120*c54f35caSApple OSS Distributions IOSubMemoryDescriptor::getPhysicalSegment(IOByteCount offset, IOByteCount * length, IOOptionBits options)
121*c54f35caSApple OSS Distributions {
122*c54f35caSApple OSS Distributions 	addr64_t    address;
123*c54f35caSApple OSS Distributions 	IOByteCount actualLength;
124*c54f35caSApple OSS Distributions 
125*c54f35caSApple OSS Distributions 	assert(offset <= _length);
126*c54f35caSApple OSS Distributions 
127*c54f35caSApple OSS Distributions 	if (length) {
128*c54f35caSApple OSS Distributions 		*length = 0;
129*c54f35caSApple OSS Distributions 	}
130*c54f35caSApple OSS Distributions 
131*c54f35caSApple OSS Distributions 	if (offset >= _length) {
132*c54f35caSApple OSS Distributions 		return 0;
133*c54f35caSApple OSS Distributions 	}
134*c54f35caSApple OSS Distributions 
135*c54f35caSApple OSS Distributions 	address = _parent->getPhysicalSegment( offset + _start, &actualLength, options );
136*c54f35caSApple OSS Distributions 
137*c54f35caSApple OSS Distributions 	if (address && length) {
138*c54f35caSApple OSS Distributions 		*length = min( _length - offset, actualLength );
139*c54f35caSApple OSS Distributions 	}
140*c54f35caSApple OSS Distributions 
141*c54f35caSApple OSS Distributions 	return address;
142*c54f35caSApple OSS Distributions }
143*c54f35caSApple OSS Distributions 
144*c54f35caSApple OSS Distributions IOReturn
setPurgeable(IOOptionBits newState,IOOptionBits * oldState)145*c54f35caSApple OSS Distributions IOSubMemoryDescriptor::setPurgeable( IOOptionBits newState,
146*c54f35caSApple OSS Distributions     IOOptionBits * oldState )
147*c54f35caSApple OSS Distributions {
148*c54f35caSApple OSS Distributions 	IOReturn err;
149*c54f35caSApple OSS Distributions 
150*c54f35caSApple OSS Distributions 	err = _parent->setPurgeable( newState, oldState );
151*c54f35caSApple OSS Distributions 
152*c54f35caSApple OSS Distributions 	return err;
153*c54f35caSApple OSS Distributions }
154*c54f35caSApple OSS Distributions 
155*c54f35caSApple OSS Distributions IOReturn
setOwnership(task_t newOwner,int newLedgerTag,IOOptionBits newLedgerOptions)156*c54f35caSApple OSS Distributions IOSubMemoryDescriptor::setOwnership( task_t newOwner,
157*c54f35caSApple OSS Distributions     int newLedgerTag,
158*c54f35caSApple OSS Distributions     IOOptionBits newLedgerOptions )
159*c54f35caSApple OSS Distributions {
160*c54f35caSApple OSS Distributions 	IOReturn err;
161*c54f35caSApple OSS Distributions 
162*c54f35caSApple OSS Distributions 	if (iokit_iomd_setownership_enabled == FALSE) {
163*c54f35caSApple OSS Distributions 		return kIOReturnUnsupported;
164*c54f35caSApple OSS Distributions 	}
165*c54f35caSApple OSS Distributions 
166*c54f35caSApple OSS Distributions 	err = _parent->setOwnership( newOwner, newLedgerTag, newLedgerOptions );
167*c54f35caSApple OSS Distributions 
168*c54f35caSApple OSS Distributions 	return err;
169*c54f35caSApple OSS Distributions }
170*c54f35caSApple OSS Distributions 
171*c54f35caSApple OSS Distributions IOReturn
prepare(IODirection forDirection)172*c54f35caSApple OSS Distributions IOSubMemoryDescriptor::prepare(
173*c54f35caSApple OSS Distributions 	IODirection forDirection)
174*c54f35caSApple OSS Distributions {
175*c54f35caSApple OSS Distributions 	IOReturn    err;
176*c54f35caSApple OSS Distributions 
177*c54f35caSApple OSS Distributions 	err = _parent->prepare( forDirection);
178*c54f35caSApple OSS Distributions 
179*c54f35caSApple OSS Distributions 	return err;
180*c54f35caSApple OSS Distributions }
181*c54f35caSApple OSS Distributions 
182*c54f35caSApple OSS Distributions IOReturn
complete(IODirection forDirection)183*c54f35caSApple OSS Distributions IOSubMemoryDescriptor::complete(
184*c54f35caSApple OSS Distributions 	IODirection forDirection)
185*c54f35caSApple OSS Distributions {
186*c54f35caSApple OSS Distributions 	IOReturn    err;
187*c54f35caSApple OSS Distributions 
188*c54f35caSApple OSS Distributions 	err = _parent->complete( forDirection);
189*c54f35caSApple OSS Distributions 
190*c54f35caSApple OSS Distributions 	return err;
191*c54f35caSApple OSS Distributions }
192*c54f35caSApple OSS Distributions 
193*c54f35caSApple OSS Distributions IOMemoryMap *
makeMapping(IOMemoryDescriptor * owner,task_t intoTask,IOVirtualAddress address,IOOptionBits options,IOByteCount offset,IOByteCount length)194*c54f35caSApple OSS Distributions IOSubMemoryDescriptor::makeMapping(
195*c54f35caSApple OSS Distributions 	IOMemoryDescriptor *    owner,
196*c54f35caSApple OSS Distributions 	task_t                  intoTask,
197*c54f35caSApple OSS Distributions 	IOVirtualAddress        address,
198*c54f35caSApple OSS Distributions 	IOOptionBits            options,
199*c54f35caSApple OSS Distributions 	IOByteCount             offset,
200*c54f35caSApple OSS Distributions 	IOByteCount             length )
201*c54f35caSApple OSS Distributions {
202*c54f35caSApple OSS Distributions 	IOMemoryMap * mapping = NULL;
203*c54f35caSApple OSS Distributions 
204*c54f35caSApple OSS Distributions #ifndef __LP64__
205*c54f35caSApple OSS Distributions 	if (!(kIOMap64Bit & options)) {
206*c54f35caSApple OSS Distributions 		panic("IOSubMemoryDescriptor::makeMapping !64bit");
207*c54f35caSApple OSS Distributions 	}
208*c54f35caSApple OSS Distributions #endif /* !__LP64__ */
209*c54f35caSApple OSS Distributions 
210*c54f35caSApple OSS Distributions 	mapping = (IOMemoryMap *) _parent->makeMapping(
211*c54f35caSApple OSS Distributions 		owner,
212*c54f35caSApple OSS Distributions 		intoTask,
213*c54f35caSApple OSS Distributions 		address,
214*c54f35caSApple OSS Distributions 		options, _start + offset, length );
215*c54f35caSApple OSS Distributions 
216*c54f35caSApple OSS Distributions 	return mapping;
217*c54f35caSApple OSS Distributions }
218*c54f35caSApple OSS Distributions 
219*c54f35caSApple OSS Distributions uint64_t
getPreparationID(void)220*c54f35caSApple OSS Distributions IOSubMemoryDescriptor::getPreparationID( void )
221*c54f35caSApple OSS Distributions {
222*c54f35caSApple OSS Distributions 	uint64_t pID;
223*c54f35caSApple OSS Distributions 
224*c54f35caSApple OSS Distributions 	if (!super::getKernelReserved()) {
225*c54f35caSApple OSS Distributions 		return kIOPreparationIDUnsupported;
226*c54f35caSApple OSS Distributions 	}
227*c54f35caSApple OSS Distributions 
228*c54f35caSApple OSS Distributions 	pID = _parent->getPreparationID();
229*c54f35caSApple OSS Distributions 	if (reserved->kernReserved[0] != pID) {
230*c54f35caSApple OSS Distributions 		reserved->kernReserved[0] = pID;
231*c54f35caSApple OSS Distributions 		reserved->preparationID   = kIOPreparationIDUnprepared;
232*c54f35caSApple OSS Distributions 		super::setPreparationID();
233*c54f35caSApple OSS Distributions 	}
234*c54f35caSApple OSS Distributions 
235*c54f35caSApple OSS Distributions 	return super::getPreparationID();
236*c54f35caSApple OSS Distributions }
237*c54f35caSApple OSS Distributions 
238*c54f35caSApple OSS Distributions IOReturn
getPageCounts(IOByteCount * residentPageCount,IOByteCount * dirtyPageCount)239*c54f35caSApple OSS Distributions IOSubMemoryDescriptor::getPageCounts(IOByteCount * residentPageCount,
240*c54f35caSApple OSS Distributions     IOByteCount * dirtyPageCount)
241*c54f35caSApple OSS Distributions {
242*c54f35caSApple OSS Distributions 	return _parent->getPageCounts(residentPageCount, dirtyPageCount);
243*c54f35caSApple OSS Distributions }
244