xref: /xnu-10002.81.5/iokit/Kernel/IOSubMemoryDescriptor.cpp (revision 5e3eaea39dcf651e66cb99ba7d70e32cc4a99587)
1*5e3eaea3SApple OSS Distributions /*
2*5e3eaea3SApple OSS Distributions  * Copyright (c) 1998-2007 Apple Inc. All rights reserved.
3*5e3eaea3SApple OSS Distributions  *
4*5e3eaea3SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*5e3eaea3SApple OSS Distributions  *
6*5e3eaea3SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*5e3eaea3SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*5e3eaea3SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*5e3eaea3SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*5e3eaea3SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*5e3eaea3SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*5e3eaea3SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*5e3eaea3SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*5e3eaea3SApple OSS Distributions  *
15*5e3eaea3SApple OSS Distributions  * Please obtain a copy of the License at
16*5e3eaea3SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*5e3eaea3SApple OSS Distributions  *
18*5e3eaea3SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*5e3eaea3SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*5e3eaea3SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*5e3eaea3SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*5e3eaea3SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*5e3eaea3SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*5e3eaea3SApple OSS Distributions  * limitations under the License.
25*5e3eaea3SApple OSS Distributions  *
26*5e3eaea3SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*5e3eaea3SApple OSS Distributions  */
28*5e3eaea3SApple OSS Distributions 
29*5e3eaea3SApple OSS Distributions #include <IOKit/IOSubMemoryDescriptor.h>
30*5e3eaea3SApple OSS Distributions #include <IOKit/IOLib.h>
31*5e3eaea3SApple OSS Distributions 
32*5e3eaea3SApple OSS Distributions #include "IOKitKernelInternal.h"
33*5e3eaea3SApple OSS Distributions 
34*5e3eaea3SApple OSS Distributions #define super IOMemoryDescriptor
35*5e3eaea3SApple OSS Distributions 
OSDefineMetaClassAndStructors(IOSubMemoryDescriptor,IOMemoryDescriptor)36*5e3eaea3SApple OSS Distributions OSDefineMetaClassAndStructors(IOSubMemoryDescriptor, IOMemoryDescriptor)
37*5e3eaea3SApple OSS Distributions 
38*5e3eaea3SApple OSS Distributions IOReturn
39*5e3eaea3SApple OSS Distributions IOSubMemoryDescriptor::redirect( task_t safeTask, bool doRedirect )
40*5e3eaea3SApple OSS Distributions {
41*5e3eaea3SApple OSS Distributions #ifdef __LP64__
42*5e3eaea3SApple OSS Distributions 	super::redirect( safeTask, doRedirect );
43*5e3eaea3SApple OSS Distributions #endif /* __LP64__ */
44*5e3eaea3SApple OSS Distributions 	return _parent->redirect( safeTask, doRedirect );
45*5e3eaea3SApple OSS Distributions }
46*5e3eaea3SApple OSS Distributions 
47*5e3eaea3SApple OSS Distributions IOSubMemoryDescriptor *
withSubRange(IOMemoryDescriptor * of,IOByteCount offset,IOByteCount length,IOOptionBits options)48*5e3eaea3SApple OSS Distributions IOSubMemoryDescriptor::withSubRange(IOMemoryDescriptor *        of,
49*5e3eaea3SApple OSS Distributions     IOByteCount             offset,
50*5e3eaea3SApple OSS Distributions     IOByteCount             length,
51*5e3eaea3SApple OSS Distributions     IOOptionBits            options)
52*5e3eaea3SApple OSS Distributions {
53*5e3eaea3SApple OSS Distributions 	IOSubMemoryDescriptor *self = new IOSubMemoryDescriptor;
54*5e3eaea3SApple OSS Distributions 
55*5e3eaea3SApple OSS Distributions 	if (self && !self->initSubRange(of, offset, length, (IODirection) options)) {
56*5e3eaea3SApple OSS Distributions 		self->release();
57*5e3eaea3SApple OSS Distributions 		self = NULL;
58*5e3eaea3SApple OSS Distributions 	}
59*5e3eaea3SApple OSS Distributions 	return self;
60*5e3eaea3SApple OSS Distributions }
61*5e3eaea3SApple OSS Distributions 
62*5e3eaea3SApple OSS Distributions bool
initSubRange(IOMemoryDescriptor * parent,IOByteCount offset,IOByteCount length,IODirection direction)63*5e3eaea3SApple OSS Distributions IOSubMemoryDescriptor::initSubRange( IOMemoryDescriptor * parent,
64*5e3eaea3SApple OSS Distributions     IOByteCount offset, IOByteCount length,
65*5e3eaea3SApple OSS Distributions     IODirection direction )
66*5e3eaea3SApple OSS Distributions {
67*5e3eaea3SApple OSS Distributions 	if (parent && ((offset + length) > parent->getLength())) {
68*5e3eaea3SApple OSS Distributions 		return false;
69*5e3eaea3SApple OSS Distributions 	}
70*5e3eaea3SApple OSS Distributions 
71*5e3eaea3SApple OSS Distributions 	/*
72*5e3eaea3SApple OSS Distributions 	 * We can check the _parent instance variable before having ever set it
73*5e3eaea3SApple OSS Distributions 	 * to an initial value because I/O Kit guarantees that all our instance
74*5e3eaea3SApple OSS Distributions 	 * variables are zeroed on an object's allocation.
75*5e3eaea3SApple OSS Distributions 	 */
76*5e3eaea3SApple OSS Distributions 
77*5e3eaea3SApple OSS Distributions 	if (!_parent) {
78*5e3eaea3SApple OSS Distributions 		if (!super::init()) {
79*5e3eaea3SApple OSS Distributions 			return false;
80*5e3eaea3SApple OSS Distributions 		}
81*5e3eaea3SApple OSS Distributions 	} else {
82*5e3eaea3SApple OSS Distributions 		/*
83*5e3eaea3SApple OSS Distributions 		 * An existing memory descriptor is being retargeted to
84*5e3eaea3SApple OSS Distributions 		 * point to somewhere else.  Clean up our present state.
85*5e3eaea3SApple OSS Distributions 		 */
86*5e3eaea3SApple OSS Distributions 
87*5e3eaea3SApple OSS Distributions 		_parent->release();
88*5e3eaea3SApple OSS Distributions 	}
89*5e3eaea3SApple OSS Distributions 
90*5e3eaea3SApple OSS Distributions 	if (parent) {
91*5e3eaea3SApple OSS Distributions 		parent->retain();
92*5e3eaea3SApple OSS Distributions 		_tag    = parent->getTag();
93*5e3eaea3SApple OSS Distributions 	} else {
94*5e3eaea3SApple OSS Distributions 		_tag    = 0;
95*5e3eaea3SApple OSS Distributions 	}
96*5e3eaea3SApple OSS Distributions 	_parent     = parent;
97*5e3eaea3SApple OSS Distributions 	_start      = offset;
98*5e3eaea3SApple OSS Distributions 	_length     = length;
99*5e3eaea3SApple OSS Distributions 	_flags      = direction;
100*5e3eaea3SApple OSS Distributions 	_flags |= kIOMemoryThreadSafe;
101*5e3eaea3SApple OSS Distributions 
102*5e3eaea3SApple OSS Distributions #ifndef __LP64__
103*5e3eaea3SApple OSS Distributions 	_direction  = (IODirection) (_flags & kIOMemoryDirectionMask);
104*5e3eaea3SApple OSS Distributions #endif /* !__LP64__ */
105*5e3eaea3SApple OSS Distributions 
106*5e3eaea3SApple OSS Distributions 	return true;
107*5e3eaea3SApple OSS Distributions }
108*5e3eaea3SApple OSS Distributions 
109*5e3eaea3SApple OSS Distributions void
free(void)110*5e3eaea3SApple OSS Distributions IOSubMemoryDescriptor::free( void )
111*5e3eaea3SApple OSS Distributions {
112*5e3eaea3SApple OSS Distributions 	if (_parent) {
113*5e3eaea3SApple OSS Distributions 		_parent->release();
114*5e3eaea3SApple OSS Distributions 	}
115*5e3eaea3SApple OSS Distributions 
116*5e3eaea3SApple OSS Distributions 	super::free();
117*5e3eaea3SApple OSS Distributions }
118*5e3eaea3SApple OSS Distributions 
119*5e3eaea3SApple OSS Distributions addr64_t
getPhysicalSegment(IOByteCount offset,IOByteCount * length,IOOptionBits options)120*5e3eaea3SApple OSS Distributions IOSubMemoryDescriptor::getPhysicalSegment(IOByteCount offset, IOByteCount * length, IOOptionBits options)
121*5e3eaea3SApple OSS Distributions {
122*5e3eaea3SApple OSS Distributions 	addr64_t    address;
123*5e3eaea3SApple OSS Distributions 	IOByteCount actualLength;
124*5e3eaea3SApple OSS Distributions 
125*5e3eaea3SApple OSS Distributions 	assert(offset <= _length);
126*5e3eaea3SApple OSS Distributions 
127*5e3eaea3SApple OSS Distributions 	if (length) {
128*5e3eaea3SApple OSS Distributions 		*length = 0;
129*5e3eaea3SApple OSS Distributions 	}
130*5e3eaea3SApple OSS Distributions 
131*5e3eaea3SApple OSS Distributions 	if (offset >= _length) {
132*5e3eaea3SApple OSS Distributions 		return 0;
133*5e3eaea3SApple OSS Distributions 	}
134*5e3eaea3SApple OSS Distributions 
135*5e3eaea3SApple OSS Distributions 	address = _parent->getPhysicalSegment( offset + _start, &actualLength, options );
136*5e3eaea3SApple OSS Distributions 
137*5e3eaea3SApple OSS Distributions 	if (address && length) {
138*5e3eaea3SApple OSS Distributions 		*length = min( _length - offset, actualLength );
139*5e3eaea3SApple OSS Distributions 	}
140*5e3eaea3SApple OSS Distributions 
141*5e3eaea3SApple OSS Distributions 	return address;
142*5e3eaea3SApple OSS Distributions }
143*5e3eaea3SApple OSS Distributions 
144*5e3eaea3SApple OSS Distributions IOReturn
setPurgeable(IOOptionBits newState,IOOptionBits * oldState)145*5e3eaea3SApple OSS Distributions IOSubMemoryDescriptor::setPurgeable( IOOptionBits newState,
146*5e3eaea3SApple OSS Distributions     IOOptionBits * oldState )
147*5e3eaea3SApple OSS Distributions {
148*5e3eaea3SApple OSS Distributions 	IOReturn err;
149*5e3eaea3SApple OSS Distributions 
150*5e3eaea3SApple OSS Distributions 	err = _parent->setPurgeable( newState, oldState );
151*5e3eaea3SApple OSS Distributions 
152*5e3eaea3SApple OSS Distributions 	return err;
153*5e3eaea3SApple OSS Distributions }
154*5e3eaea3SApple OSS Distributions 
155*5e3eaea3SApple OSS Distributions IOReturn
setOwnership(task_t newOwner,int newLedgerTag,IOOptionBits newLedgerOptions)156*5e3eaea3SApple OSS Distributions IOSubMemoryDescriptor::setOwnership( task_t newOwner,
157*5e3eaea3SApple OSS Distributions     int newLedgerTag,
158*5e3eaea3SApple OSS Distributions     IOOptionBits newLedgerOptions )
159*5e3eaea3SApple OSS Distributions {
160*5e3eaea3SApple OSS Distributions 	IOReturn err;
161*5e3eaea3SApple OSS Distributions 
162*5e3eaea3SApple OSS Distributions 	if (iokit_iomd_setownership_enabled == FALSE) {
163*5e3eaea3SApple OSS Distributions 		return kIOReturnUnsupported;
164*5e3eaea3SApple OSS Distributions 	}
165*5e3eaea3SApple OSS Distributions 
166*5e3eaea3SApple OSS Distributions 	err = _parent->setOwnership( newOwner, newLedgerTag, newLedgerOptions );
167*5e3eaea3SApple OSS Distributions 
168*5e3eaea3SApple OSS Distributions 	return err;
169*5e3eaea3SApple OSS Distributions }
170*5e3eaea3SApple OSS Distributions 
171*5e3eaea3SApple OSS Distributions IOReturn
prepare(IODirection forDirection)172*5e3eaea3SApple OSS Distributions IOSubMemoryDescriptor::prepare(
173*5e3eaea3SApple OSS Distributions 	IODirection forDirection)
174*5e3eaea3SApple OSS Distributions {
175*5e3eaea3SApple OSS Distributions 	IOReturn    err;
176*5e3eaea3SApple OSS Distributions 
177*5e3eaea3SApple OSS Distributions 	err = _parent->prepare( forDirection);
178*5e3eaea3SApple OSS Distributions 
179*5e3eaea3SApple OSS Distributions 	return err;
180*5e3eaea3SApple OSS Distributions }
181*5e3eaea3SApple OSS Distributions 
182*5e3eaea3SApple OSS Distributions IOReturn
complete(IODirection forDirection)183*5e3eaea3SApple OSS Distributions IOSubMemoryDescriptor::complete(
184*5e3eaea3SApple OSS Distributions 	IODirection forDirection)
185*5e3eaea3SApple OSS Distributions {
186*5e3eaea3SApple OSS Distributions 	IOReturn    err;
187*5e3eaea3SApple OSS Distributions 
188*5e3eaea3SApple OSS Distributions 	err = _parent->complete( forDirection);
189*5e3eaea3SApple OSS Distributions 
190*5e3eaea3SApple OSS Distributions 	return err;
191*5e3eaea3SApple OSS Distributions }
192*5e3eaea3SApple OSS Distributions 
193*5e3eaea3SApple OSS Distributions IOMemoryMap *
makeMapping(IOMemoryDescriptor * owner,task_t intoTask,IOVirtualAddress address,IOOptionBits options,IOByteCount offset,IOByteCount length)194*5e3eaea3SApple OSS Distributions IOSubMemoryDescriptor::makeMapping(
195*5e3eaea3SApple OSS Distributions 	IOMemoryDescriptor *    owner,
196*5e3eaea3SApple OSS Distributions 	task_t                  intoTask,
197*5e3eaea3SApple OSS Distributions 	IOVirtualAddress        address,
198*5e3eaea3SApple OSS Distributions 	IOOptionBits            options,
199*5e3eaea3SApple OSS Distributions 	IOByteCount             offset,
200*5e3eaea3SApple OSS Distributions 	IOByteCount             length )
201*5e3eaea3SApple OSS Distributions {
202*5e3eaea3SApple OSS Distributions 	IOMemoryMap * mapping = NULL;
203*5e3eaea3SApple OSS Distributions 
204*5e3eaea3SApple OSS Distributions #ifndef __LP64__
205*5e3eaea3SApple OSS Distributions 	if (!(kIOMap64Bit & options)) {
206*5e3eaea3SApple OSS Distributions 		panic("IOSubMemoryDescriptor::makeMapping !64bit");
207*5e3eaea3SApple OSS Distributions 	}
208*5e3eaea3SApple OSS Distributions #endif /* !__LP64__ */
209*5e3eaea3SApple OSS Distributions 
210*5e3eaea3SApple OSS Distributions 	mapping = (IOMemoryMap *) _parent->makeMapping(
211*5e3eaea3SApple OSS Distributions 		owner,
212*5e3eaea3SApple OSS Distributions 		intoTask,
213*5e3eaea3SApple OSS Distributions 		address,
214*5e3eaea3SApple OSS Distributions 		options, _start + offset, length );
215*5e3eaea3SApple OSS Distributions 
216*5e3eaea3SApple OSS Distributions 	return mapping;
217*5e3eaea3SApple OSS Distributions }
218*5e3eaea3SApple OSS Distributions 
219*5e3eaea3SApple OSS Distributions uint64_t
getPreparationID(void)220*5e3eaea3SApple OSS Distributions IOSubMemoryDescriptor::getPreparationID( void )
221*5e3eaea3SApple OSS Distributions {
222*5e3eaea3SApple OSS Distributions 	uint64_t pID;
223*5e3eaea3SApple OSS Distributions 
224*5e3eaea3SApple OSS Distributions 	if (!super::getKernelReserved()) {
225*5e3eaea3SApple OSS Distributions 		return kIOPreparationIDUnsupported;
226*5e3eaea3SApple OSS Distributions 	}
227*5e3eaea3SApple OSS Distributions 
228*5e3eaea3SApple OSS Distributions 	pID = _parent->getPreparationID();
229*5e3eaea3SApple OSS Distributions 	if (reserved->kernReserved[0] != pID) {
230*5e3eaea3SApple OSS Distributions 		reserved->kernReserved[0] = pID;
231*5e3eaea3SApple OSS Distributions 		reserved->preparationID   = kIOPreparationIDUnprepared;
232*5e3eaea3SApple OSS Distributions 		super::setPreparationID();
233*5e3eaea3SApple OSS Distributions 	}
234*5e3eaea3SApple OSS Distributions 
235*5e3eaea3SApple OSS Distributions 	return super::getPreparationID();
236*5e3eaea3SApple OSS Distributions }
237*5e3eaea3SApple OSS Distributions 
238*5e3eaea3SApple OSS Distributions IOReturn
getPageCounts(IOByteCount * residentPageCount,IOByteCount * dirtyPageCount)239*5e3eaea3SApple OSS Distributions IOSubMemoryDescriptor::getPageCounts(IOByteCount * residentPageCount,
240*5e3eaea3SApple OSS Distributions     IOByteCount * dirtyPageCount)
241*5e3eaea3SApple OSS Distributions {
242*5e3eaea3SApple OSS Distributions 	return _parent->getPageCounts(residentPageCount, dirtyPageCount);
243*5e3eaea3SApple OSS Distributions }
244