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