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