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