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