xref: /xnu-8792.81.2/iokit/Kernel/IOMemoryDescriptor.cpp (revision 19c3b8c28c31cb8130e034cfb5df6bf9ba342d90)
1*19c3b8c2SApple OSS Distributions /*
2*19c3b8c2SApple OSS Distributions  * Copyright (c) 1998-2021 Apple Inc. All rights reserved.
3*19c3b8c2SApple OSS Distributions  *
4*19c3b8c2SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*19c3b8c2SApple OSS Distributions  *
6*19c3b8c2SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*19c3b8c2SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*19c3b8c2SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*19c3b8c2SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*19c3b8c2SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*19c3b8c2SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*19c3b8c2SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*19c3b8c2SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*19c3b8c2SApple OSS Distributions  *
15*19c3b8c2SApple OSS Distributions  * Please obtain a copy of the License at
16*19c3b8c2SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*19c3b8c2SApple OSS Distributions  *
18*19c3b8c2SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*19c3b8c2SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*19c3b8c2SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*19c3b8c2SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*19c3b8c2SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*19c3b8c2SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*19c3b8c2SApple OSS Distributions  * limitations under the License.
25*19c3b8c2SApple OSS Distributions  *
26*19c3b8c2SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*19c3b8c2SApple OSS Distributions  */
28*19c3b8c2SApple OSS Distributions #define IOKIT_ENABLE_SHARED_PTR
29*19c3b8c2SApple OSS Distributions 
30*19c3b8c2SApple OSS Distributions #include <sys/cdefs.h>
31*19c3b8c2SApple OSS Distributions 
32*19c3b8c2SApple OSS Distributions #include <IOKit/assert.h>
33*19c3b8c2SApple OSS Distributions #include <IOKit/system.h>
34*19c3b8c2SApple OSS Distributions #include <IOKit/IOLib.h>
35*19c3b8c2SApple OSS Distributions #include <IOKit/IOMemoryDescriptor.h>
36*19c3b8c2SApple OSS Distributions #include <IOKit/IOMapper.h>
37*19c3b8c2SApple OSS Distributions #include <IOKit/IODMACommand.h>
38*19c3b8c2SApple OSS Distributions #include <IOKit/IOKitKeysPrivate.h>
39*19c3b8c2SApple OSS Distributions 
40*19c3b8c2SApple OSS Distributions #include <IOKit/IOSubMemoryDescriptor.h>
41*19c3b8c2SApple OSS Distributions #include <IOKit/IOMultiMemoryDescriptor.h>
42*19c3b8c2SApple OSS Distributions #include <IOKit/IOBufferMemoryDescriptor.h>
43*19c3b8c2SApple OSS Distributions 
44*19c3b8c2SApple OSS Distributions #include <IOKit/IOKitDebug.h>
45*19c3b8c2SApple OSS Distributions #include <IOKit/IOTimeStamp.h>
46*19c3b8c2SApple OSS Distributions #include <libkern/OSDebug.h>
47*19c3b8c2SApple OSS Distributions #include <libkern/OSKextLibPrivate.h>
48*19c3b8c2SApple OSS Distributions 
49*19c3b8c2SApple OSS Distributions #include "IOKitKernelInternal.h"
50*19c3b8c2SApple OSS Distributions 
51*19c3b8c2SApple OSS Distributions #include <libkern/c++/OSAllocation.h>
52*19c3b8c2SApple OSS Distributions #include <libkern/c++/OSContainers.h>
53*19c3b8c2SApple OSS Distributions #include <libkern/c++/OSDictionary.h>
54*19c3b8c2SApple OSS Distributions #include <libkern/c++/OSArray.h>
55*19c3b8c2SApple OSS Distributions #include <libkern/c++/OSSymbol.h>
56*19c3b8c2SApple OSS Distributions #include <libkern/c++/OSNumber.h>
57*19c3b8c2SApple OSS Distributions #include <os/overflow.h>
58*19c3b8c2SApple OSS Distributions #include <os/cpp_util.h>
59*19c3b8c2SApple OSS Distributions #include <os/base_private.h>
60*19c3b8c2SApple OSS Distributions 
61*19c3b8c2SApple OSS Distributions #include <sys/uio.h>
62*19c3b8c2SApple OSS Distributions 
63*19c3b8c2SApple OSS Distributions __BEGIN_DECLS
64*19c3b8c2SApple OSS Distributions #include <vm/pmap.h>
65*19c3b8c2SApple OSS Distributions #include <vm/vm_pageout.h>
66*19c3b8c2SApple OSS Distributions #include <mach/memory_object_types.h>
67*19c3b8c2SApple OSS Distributions #include <device/device_port.h>
68*19c3b8c2SApple OSS Distributions 
69*19c3b8c2SApple OSS Distributions #include <mach/vm_prot.h>
70*19c3b8c2SApple OSS Distributions #include <mach/mach_vm.h>
71*19c3b8c2SApple OSS Distributions #include <mach/memory_entry.h>
72*19c3b8c2SApple OSS Distributions #include <vm/vm_fault.h>
73*19c3b8c2SApple OSS Distributions #include <vm/vm_protos.h>
74*19c3b8c2SApple OSS Distributions 
75*19c3b8c2SApple OSS Distributions extern ppnum_t pmap_find_phys(pmap_t pmap, addr64_t va);
76*19c3b8c2SApple OSS Distributions extern void ipc_port_release_send(ipc_port_t port);
77*19c3b8c2SApple OSS Distributions 
78*19c3b8c2SApple OSS Distributions extern kern_return_t
79*19c3b8c2SApple OSS Distributions mach_memory_entry_ownership(
80*19c3b8c2SApple OSS Distributions 	ipc_port_t      entry_port,
81*19c3b8c2SApple OSS Distributions 	task_t          owner,
82*19c3b8c2SApple OSS Distributions 	int             ledger_tag,
83*19c3b8c2SApple OSS Distributions 	int             ledger_flags);
84*19c3b8c2SApple OSS Distributions 
85*19c3b8c2SApple OSS Distributions __END_DECLS
86*19c3b8c2SApple OSS Distributions 
87*19c3b8c2SApple OSS Distributions #define kIOMapperWaitSystem     ((IOMapper *) 1)
88*19c3b8c2SApple OSS Distributions 
89*19c3b8c2SApple OSS Distributions static IOMapper * gIOSystemMapper = NULL;
90*19c3b8c2SApple OSS Distributions 
91*19c3b8c2SApple OSS Distributions ppnum_t           gIOLastPage;
92*19c3b8c2SApple OSS Distributions 
93*19c3b8c2SApple OSS Distributions enum {
94*19c3b8c2SApple OSS Distributions 	kIOMapGuardSizeLarge = 65536
95*19c3b8c2SApple OSS Distributions };
96*19c3b8c2SApple OSS Distributions 
97*19c3b8c2SApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
98*19c3b8c2SApple OSS Distributions 
99*19c3b8c2SApple OSS Distributions OSDefineMetaClassAndAbstractStructors( IOMemoryDescriptor, OSObject )
100*19c3b8c2SApple OSS Distributions 
101*19c3b8c2SApple OSS Distributions #define super IOMemoryDescriptor
102*19c3b8c2SApple OSS Distributions 
103*19c3b8c2SApple OSS Distributions OSDefineMetaClassAndStructorsWithZone(IOGeneralMemoryDescriptor,
104*19c3b8c2SApple OSS Distributions     IOMemoryDescriptor, ZC_ZFREE_CLEARMEM)
105*19c3b8c2SApple OSS Distributions 
106*19c3b8c2SApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
107*19c3b8c2SApple OSS Distributions 
108*19c3b8c2SApple OSS Distributions static IORecursiveLock * gIOMemoryLock;
109*19c3b8c2SApple OSS Distributions 
110*19c3b8c2SApple OSS Distributions #define LOCK    IORecursiveLockLock( gIOMemoryLock)
111*19c3b8c2SApple OSS Distributions #define UNLOCK  IORecursiveLockUnlock( gIOMemoryLock)
112*19c3b8c2SApple OSS Distributions #define SLEEP   IORecursiveLockSleep( gIOMemoryLock, (void *)this, THREAD_UNINT)
113*19c3b8c2SApple OSS Distributions #define WAKEUP  \
114*19c3b8c2SApple OSS Distributions     IORecursiveLockWakeup( gIOMemoryLock, (void *)this, /* one-thread */ false)
115*19c3b8c2SApple OSS Distributions 
116*19c3b8c2SApple OSS Distributions #if 0
117*19c3b8c2SApple OSS Distributions #define DEBG(fmt, args...)      { kprintf(fmt, ## args); }
118*19c3b8c2SApple OSS Distributions #else
119*19c3b8c2SApple OSS Distributions #define DEBG(fmt, args...)      {}
120*19c3b8c2SApple OSS Distributions #endif
121*19c3b8c2SApple OSS Distributions 
122*19c3b8c2SApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
123*19c3b8c2SApple OSS Distributions 
124*19c3b8c2SApple OSS Distributions // Some data structures and accessor macros used by the initWithOptions
125*19c3b8c2SApple OSS Distributions // Function
126*19c3b8c2SApple OSS Distributions 
127*19c3b8c2SApple OSS Distributions enum ioPLBlockFlags {
128*19c3b8c2SApple OSS Distributions 	kIOPLOnDevice  = 0x00000001,
129*19c3b8c2SApple OSS Distributions 	kIOPLExternUPL = 0x00000002,
130*19c3b8c2SApple OSS Distributions };
131*19c3b8c2SApple OSS Distributions 
132*19c3b8c2SApple OSS Distributions struct IOMDPersistentInitData {
133*19c3b8c2SApple OSS Distributions 	const IOGeneralMemoryDescriptor * fMD;
134*19c3b8c2SApple OSS Distributions 	IOMemoryReference               * fMemRef;
135*19c3b8c2SApple OSS Distributions };
136*19c3b8c2SApple OSS Distributions 
137*19c3b8c2SApple OSS Distributions struct ioPLBlock {
138*19c3b8c2SApple OSS Distributions 	upl_t fIOPL;
139*19c3b8c2SApple OSS Distributions 	vm_address_t fPageInfo; // Pointer to page list or index into it
140*19c3b8c2SApple OSS Distributions 	uint64_t fIOMDOffset;       // The offset of this iopl in descriptor
141*19c3b8c2SApple OSS Distributions 	ppnum_t fMappedPage;        // Page number of first page in this iopl
142*19c3b8c2SApple OSS Distributions 	unsigned int fPageOffset;   // Offset within first page of iopl
143*19c3b8c2SApple OSS Distributions 	unsigned int fFlags;        // Flags
144*19c3b8c2SApple OSS Distributions };
145*19c3b8c2SApple OSS Distributions 
146*19c3b8c2SApple OSS Distributions enum { kMaxWireTags = 6 };
147*19c3b8c2SApple OSS Distributions 
148*19c3b8c2SApple OSS Distributions struct ioGMDData {
149*19c3b8c2SApple OSS Distributions 	IOMapper *  fMapper;
150*19c3b8c2SApple OSS Distributions 	uint64_t    fDMAMapAlignment;
151*19c3b8c2SApple OSS Distributions 	uint64_t    fMappedBase;
152*19c3b8c2SApple OSS Distributions 	uint64_t    fMappedLength;
153*19c3b8c2SApple OSS Distributions 	uint64_t    fPreparationID;
154*19c3b8c2SApple OSS Distributions #if IOTRACKING
155*19c3b8c2SApple OSS Distributions 	IOTracking  fWireTracking;
156*19c3b8c2SApple OSS Distributions #endif /* IOTRACKING */
157*19c3b8c2SApple OSS Distributions 	unsigned int      fPageCnt;
158*19c3b8c2SApple OSS Distributions 	uint8_t           fDMAMapNumAddressBits;
159*19c3b8c2SApple OSS Distributions 	unsigned char     fCompletionError:1;
160*19c3b8c2SApple OSS Distributions 	unsigned char     fMappedBaseValid:1;
161*19c3b8c2SApple OSS Distributions 	unsigned char     _resv:4;
162*19c3b8c2SApple OSS Distributions 	unsigned char     fDMAAccess:2;
163*19c3b8c2SApple OSS Distributions 
164*19c3b8c2SApple OSS Distributions 	/* variable length arrays */
165*19c3b8c2SApple OSS Distributions 	upl_page_info_t fPageList[1]
166*19c3b8c2SApple OSS Distributions #if __LP64__
167*19c3b8c2SApple OSS Distributions 	// align fPageList as for ioPLBlock
168*19c3b8c2SApple OSS Distributions 	__attribute__((aligned(sizeof(upl_t))))
169*19c3b8c2SApple OSS Distributions #endif
170*19c3b8c2SApple OSS Distributions 	;
171*19c3b8c2SApple OSS Distributions 	//ioPLBlock fBlocks[1];
172*19c3b8c2SApple OSS Distributions };
173*19c3b8c2SApple OSS Distributions 
174*19c3b8c2SApple OSS Distributions #pragma GCC visibility push(hidden)
175*19c3b8c2SApple OSS Distributions 
176*19c3b8c2SApple OSS Distributions class _IOMemoryDescriptorMixedData : public OSObject
177*19c3b8c2SApple OSS Distributions {
178*19c3b8c2SApple OSS Distributions 	OSDeclareDefaultStructors(_IOMemoryDescriptorMixedData);
179*19c3b8c2SApple OSS Distributions 
180*19c3b8c2SApple OSS Distributions public:
181*19c3b8c2SApple OSS Distributions 	static OSPtr<_IOMemoryDescriptorMixedData> withCapacity(size_t capacity);
182*19c3b8c2SApple OSS Distributions 	virtual bool initWithCapacity(size_t capacity);
183*19c3b8c2SApple OSS Distributions 	virtual void free() APPLE_KEXT_OVERRIDE;
184*19c3b8c2SApple OSS Distributions 
185*19c3b8c2SApple OSS Distributions 	virtual bool appendBytes(const void * bytes, size_t length);
186*19c3b8c2SApple OSS Distributions 	virtual void setLength(size_t length);
187*19c3b8c2SApple OSS Distributions 
188*19c3b8c2SApple OSS Distributions 	virtual const void * getBytes() const;
189*19c3b8c2SApple OSS Distributions 	virtual size_t getLength() const;
190*19c3b8c2SApple OSS Distributions 
191*19c3b8c2SApple OSS Distributions private:
192*19c3b8c2SApple OSS Distributions 	void freeMemory();
193*19c3b8c2SApple OSS Distributions 
194*19c3b8c2SApple OSS Distributions 	void *  _data = nullptr;
195*19c3b8c2SApple OSS Distributions 	size_t  _length = 0;
196*19c3b8c2SApple OSS Distributions 	size_t  _capacity = 0;
197*19c3b8c2SApple OSS Distributions };
198*19c3b8c2SApple OSS Distributions 
199*19c3b8c2SApple OSS Distributions #pragma GCC visibility pop
200*19c3b8c2SApple OSS Distributions 
201*19c3b8c2SApple OSS Distributions #define getDataP(osd)   ((ioGMDData *) (osd)->getBytes())
202*19c3b8c2SApple OSS Distributions #define getIOPLList(d)  ((ioPLBlock *) (void *)&(d->fPageList[d->fPageCnt]))
203*19c3b8c2SApple OSS Distributions #define getNumIOPL(osd, d)      \
204*19c3b8c2SApple OSS Distributions     ((UInt)(((osd)->getLength() - ((char *) getIOPLList(d) - (char *) d)) / sizeof(ioPLBlock)))
205*19c3b8c2SApple OSS Distributions #define getPageList(d)  (&(d->fPageList[0]))
206*19c3b8c2SApple OSS Distributions #define computeDataSize(p, u) \
207*19c3b8c2SApple OSS Distributions     (offsetof(ioGMDData, fPageList) + p * sizeof(upl_page_info_t) + u * sizeof(ioPLBlock))
208*19c3b8c2SApple OSS Distributions 
209*19c3b8c2SApple OSS Distributions enum { kIOMemoryHostOrRemote = kIOMemoryHostOnly | kIOMemoryRemote };
210*19c3b8c2SApple OSS Distributions 
211*19c3b8c2SApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
212*19c3b8c2SApple OSS Distributions 
213*19c3b8c2SApple OSS Distributions extern "C" {
214*19c3b8c2SApple OSS Distributions kern_return_t
device_data_action(uintptr_t device_handle,ipc_port_t device_pager,vm_prot_t protection,vm_object_offset_t offset,vm_size_t size)215*19c3b8c2SApple OSS Distributions device_data_action(
216*19c3b8c2SApple OSS Distributions 	uintptr_t               device_handle,
217*19c3b8c2SApple OSS Distributions 	ipc_port_t              device_pager,
218*19c3b8c2SApple OSS Distributions 	vm_prot_t               protection,
219*19c3b8c2SApple OSS Distributions 	vm_object_offset_t      offset,
220*19c3b8c2SApple OSS Distributions 	vm_size_t               size)
221*19c3b8c2SApple OSS Distributions {
222*19c3b8c2SApple OSS Distributions 	kern_return_t        kr;
223*19c3b8c2SApple OSS Distributions 	IOMemoryDescriptorReserved * ref = (IOMemoryDescriptorReserved *) device_handle;
224*19c3b8c2SApple OSS Distributions 	OSSharedPtr<IOMemoryDescriptor> memDesc;
225*19c3b8c2SApple OSS Distributions 
226*19c3b8c2SApple OSS Distributions 	LOCK;
227*19c3b8c2SApple OSS Distributions 	if (ref->dp.memory) {
228*19c3b8c2SApple OSS Distributions 		memDesc.reset(ref->dp.memory, OSRetain);
229*19c3b8c2SApple OSS Distributions 		kr = memDesc->handleFault(device_pager, offset, size);
230*19c3b8c2SApple OSS Distributions 		memDesc.reset();
231*19c3b8c2SApple OSS Distributions 	} else {
232*19c3b8c2SApple OSS Distributions 		kr = KERN_ABORTED;
233*19c3b8c2SApple OSS Distributions 	}
234*19c3b8c2SApple OSS Distributions 	UNLOCK;
235*19c3b8c2SApple OSS Distributions 
236*19c3b8c2SApple OSS Distributions 	return kr;
237*19c3b8c2SApple OSS Distributions }
238*19c3b8c2SApple OSS Distributions 
239*19c3b8c2SApple OSS Distributions kern_return_t
device_close(uintptr_t device_handle)240*19c3b8c2SApple OSS Distributions device_close(
241*19c3b8c2SApple OSS Distributions 	uintptr_t     device_handle)
242*19c3b8c2SApple OSS Distributions {
243*19c3b8c2SApple OSS Distributions 	IOMemoryDescriptorReserved * ref = (IOMemoryDescriptorReserved *) device_handle;
244*19c3b8c2SApple OSS Distributions 
245*19c3b8c2SApple OSS Distributions 	IOFreeType( ref, IOMemoryDescriptorReserved );
246*19c3b8c2SApple OSS Distributions 
247*19c3b8c2SApple OSS Distributions 	return kIOReturnSuccess;
248*19c3b8c2SApple OSS Distributions }
249*19c3b8c2SApple OSS Distributions };      // end extern "C"
250*19c3b8c2SApple OSS Distributions 
251*19c3b8c2SApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
252*19c3b8c2SApple OSS Distributions 
253*19c3b8c2SApple OSS Distributions // Note this inline function uses C++ reference arguments to return values
254*19c3b8c2SApple OSS Distributions // This means that pointers are not passed and NULLs don't have to be
255*19c3b8c2SApple OSS Distributions // checked for as a NULL reference is illegal.
256*19c3b8c2SApple OSS Distributions static inline void
getAddrLenForInd(mach_vm_address_t & addr,mach_vm_size_t & len,UInt32 type,IOGeneralMemoryDescriptor::Ranges r,UInt32 ind)257*19c3b8c2SApple OSS Distributions getAddrLenForInd(mach_vm_address_t &addr, mach_vm_size_t &len, // Output variables
258*19c3b8c2SApple OSS Distributions     UInt32 type, IOGeneralMemoryDescriptor::Ranges r, UInt32 ind)
259*19c3b8c2SApple OSS Distributions {
260*19c3b8c2SApple OSS Distributions 	assert(kIOMemoryTypeUIO == type
261*19c3b8c2SApple OSS Distributions 	    || kIOMemoryTypeVirtual == type || kIOMemoryTypeVirtual64 == type
262*19c3b8c2SApple OSS Distributions 	    || kIOMemoryTypePhysical == type || kIOMemoryTypePhysical64 == type);
263*19c3b8c2SApple OSS Distributions 	if (kIOMemoryTypeUIO == type) {
264*19c3b8c2SApple OSS Distributions 		user_size_t us;
265*19c3b8c2SApple OSS Distributions 		user_addr_t ad;
266*19c3b8c2SApple OSS Distributions 		uio_getiov((uio_t) r.uio, ind, &ad, &us); addr = ad; len = us;
267*19c3b8c2SApple OSS Distributions 	}
268*19c3b8c2SApple OSS Distributions #ifndef __LP64__
269*19c3b8c2SApple OSS Distributions 	else if ((kIOMemoryTypeVirtual64 == type) || (kIOMemoryTypePhysical64 == type)) {
270*19c3b8c2SApple OSS Distributions 		IOAddressRange cur = r.v64[ind];
271*19c3b8c2SApple OSS Distributions 		addr = cur.address;
272*19c3b8c2SApple OSS Distributions 		len  = cur.length;
273*19c3b8c2SApple OSS Distributions 	}
274*19c3b8c2SApple OSS Distributions #endif /* !__LP64__ */
275*19c3b8c2SApple OSS Distributions 	else {
276*19c3b8c2SApple OSS Distributions 		IOVirtualRange cur = r.v[ind];
277*19c3b8c2SApple OSS Distributions 		addr = cur.address;
278*19c3b8c2SApple OSS Distributions 		len  = cur.length;
279*19c3b8c2SApple OSS Distributions 	}
280*19c3b8c2SApple OSS Distributions }
281*19c3b8c2SApple OSS Distributions 
282*19c3b8c2SApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
283*19c3b8c2SApple OSS Distributions 
284*19c3b8c2SApple OSS Distributions static IOReturn
purgeableControlBits(IOOptionBits newState,vm_purgable_t * control,int * state)285*19c3b8c2SApple OSS Distributions purgeableControlBits(IOOptionBits newState, vm_purgable_t * control, int * state)
286*19c3b8c2SApple OSS Distributions {
287*19c3b8c2SApple OSS Distributions 	IOReturn err = kIOReturnSuccess;
288*19c3b8c2SApple OSS Distributions 
289*19c3b8c2SApple OSS Distributions 	*control = VM_PURGABLE_SET_STATE;
290*19c3b8c2SApple OSS Distributions 
291*19c3b8c2SApple OSS Distributions 	enum { kIOMemoryPurgeableControlMask = 15 };
292*19c3b8c2SApple OSS Distributions 
293*19c3b8c2SApple OSS Distributions 	switch (kIOMemoryPurgeableControlMask & newState) {
294*19c3b8c2SApple OSS Distributions 	case kIOMemoryPurgeableKeepCurrent:
295*19c3b8c2SApple OSS Distributions 		*control = VM_PURGABLE_GET_STATE;
296*19c3b8c2SApple OSS Distributions 		break;
297*19c3b8c2SApple OSS Distributions 
298*19c3b8c2SApple OSS Distributions 	case kIOMemoryPurgeableNonVolatile:
299*19c3b8c2SApple OSS Distributions 		*state = VM_PURGABLE_NONVOLATILE;
300*19c3b8c2SApple OSS Distributions 		break;
301*19c3b8c2SApple OSS Distributions 	case kIOMemoryPurgeableVolatile:
302*19c3b8c2SApple OSS Distributions 		*state = VM_PURGABLE_VOLATILE | (newState & ~kIOMemoryPurgeableControlMask);
303*19c3b8c2SApple OSS Distributions 		break;
304*19c3b8c2SApple OSS Distributions 	case kIOMemoryPurgeableEmpty:
305*19c3b8c2SApple OSS Distributions 		*state = VM_PURGABLE_EMPTY | (newState & ~kIOMemoryPurgeableControlMask);
306*19c3b8c2SApple OSS Distributions 		break;
307*19c3b8c2SApple OSS Distributions 	default:
308*19c3b8c2SApple OSS Distributions 		err = kIOReturnBadArgument;
309*19c3b8c2SApple OSS Distributions 		break;
310*19c3b8c2SApple OSS Distributions 	}
311*19c3b8c2SApple OSS Distributions 
312*19c3b8c2SApple OSS Distributions 	if (*control == VM_PURGABLE_SET_STATE) {
313*19c3b8c2SApple OSS Distributions 		// let VM know this call is from the kernel and is allowed to alter
314*19c3b8c2SApple OSS Distributions 		// the volatility of the memory entry even if it was created with
315*19c3b8c2SApple OSS Distributions 		// MAP_MEM_PURGABLE_KERNEL_ONLY
316*19c3b8c2SApple OSS Distributions 		*control = VM_PURGABLE_SET_STATE_FROM_KERNEL;
317*19c3b8c2SApple OSS Distributions 	}
318*19c3b8c2SApple OSS Distributions 
319*19c3b8c2SApple OSS Distributions 	return err;
320*19c3b8c2SApple OSS Distributions }
321*19c3b8c2SApple OSS Distributions 
322*19c3b8c2SApple OSS Distributions static IOReturn
purgeableStateBits(int * state)323*19c3b8c2SApple OSS Distributions purgeableStateBits(int * state)
324*19c3b8c2SApple OSS Distributions {
325*19c3b8c2SApple OSS Distributions 	IOReturn err = kIOReturnSuccess;
326*19c3b8c2SApple OSS Distributions 
327*19c3b8c2SApple OSS Distributions 	switch (VM_PURGABLE_STATE_MASK & *state) {
328*19c3b8c2SApple OSS Distributions 	case VM_PURGABLE_NONVOLATILE:
329*19c3b8c2SApple OSS Distributions 		*state = kIOMemoryPurgeableNonVolatile;
330*19c3b8c2SApple OSS Distributions 		break;
331*19c3b8c2SApple OSS Distributions 	case VM_PURGABLE_VOLATILE:
332*19c3b8c2SApple OSS Distributions 		*state = kIOMemoryPurgeableVolatile;
333*19c3b8c2SApple OSS Distributions 		break;
334*19c3b8c2SApple OSS Distributions 	case VM_PURGABLE_EMPTY:
335*19c3b8c2SApple OSS Distributions 		*state = kIOMemoryPurgeableEmpty;
336*19c3b8c2SApple OSS Distributions 		break;
337*19c3b8c2SApple OSS Distributions 	default:
338*19c3b8c2SApple OSS Distributions 		*state = kIOMemoryPurgeableNonVolatile;
339*19c3b8c2SApple OSS Distributions 		err = kIOReturnNotReady;
340*19c3b8c2SApple OSS Distributions 		break;
341*19c3b8c2SApple OSS Distributions 	}
342*19c3b8c2SApple OSS Distributions 	return err;
343*19c3b8c2SApple OSS Distributions }
344*19c3b8c2SApple OSS Distributions 
345*19c3b8c2SApple OSS Distributions typedef struct {
346*19c3b8c2SApple OSS Distributions 	unsigned int wimg;
347*19c3b8c2SApple OSS Distributions 	unsigned int object_type;
348*19c3b8c2SApple OSS Distributions } iokit_memtype_entry;
349*19c3b8c2SApple OSS Distributions 
350*19c3b8c2SApple OSS Distributions static const iokit_memtype_entry iomd_mem_types[] = {
351*19c3b8c2SApple OSS Distributions 	[kIODefaultCache] = {VM_WIMG_DEFAULT, MAP_MEM_NOOP},
352*19c3b8c2SApple OSS Distributions 	[kIOInhibitCache] = {VM_WIMG_IO, MAP_MEM_IO},
353*19c3b8c2SApple OSS Distributions 	[kIOWriteThruCache] = {VM_WIMG_WTHRU, MAP_MEM_WTHRU},
354*19c3b8c2SApple OSS Distributions 	[kIOWriteCombineCache] = {VM_WIMG_WCOMB, MAP_MEM_WCOMB},
355*19c3b8c2SApple OSS Distributions 	[kIOCopybackCache] = {VM_WIMG_COPYBACK, MAP_MEM_COPYBACK},
356*19c3b8c2SApple OSS Distributions 	[kIOCopybackInnerCache] = {VM_WIMG_INNERWBACK, MAP_MEM_INNERWBACK},
357*19c3b8c2SApple OSS Distributions 	[kIOPostedWrite] = {VM_WIMG_POSTED, MAP_MEM_POSTED},
358*19c3b8c2SApple OSS Distributions 	[kIORealTimeCache] = {VM_WIMG_RT, MAP_MEM_RT},
359*19c3b8c2SApple OSS Distributions 	[kIOPostedReordered] = {VM_WIMG_POSTED_REORDERED, MAP_MEM_POSTED_REORDERED},
360*19c3b8c2SApple OSS Distributions 	[kIOPostedCombinedReordered] = {VM_WIMG_POSTED_COMBINED_REORDERED, MAP_MEM_POSTED_COMBINED_REORDERED},
361*19c3b8c2SApple OSS Distributions };
362*19c3b8c2SApple OSS Distributions 
363*19c3b8c2SApple OSS Distributions static vm_prot_t
vmProtForCacheMode(IOOptionBits cacheMode)364*19c3b8c2SApple OSS Distributions vmProtForCacheMode(IOOptionBits cacheMode)
365*19c3b8c2SApple OSS Distributions {
366*19c3b8c2SApple OSS Distributions 	assert(cacheMode < (sizeof(iomd_mem_types) / sizeof(iomd_mem_types[0])));
367*19c3b8c2SApple OSS Distributions 	vm_prot_t prot = 0;
368*19c3b8c2SApple OSS Distributions 	SET_MAP_MEM(iomd_mem_types[cacheMode].object_type, prot);
369*19c3b8c2SApple OSS Distributions 	return prot;
370*19c3b8c2SApple OSS Distributions }
371*19c3b8c2SApple OSS Distributions 
372*19c3b8c2SApple OSS Distributions static unsigned int
pagerFlagsForCacheMode(IOOptionBits cacheMode)373*19c3b8c2SApple OSS Distributions pagerFlagsForCacheMode(IOOptionBits cacheMode)
374*19c3b8c2SApple OSS Distributions {
375*19c3b8c2SApple OSS Distributions 	assert(cacheMode < (sizeof(iomd_mem_types) / sizeof(iomd_mem_types[0])));
376*19c3b8c2SApple OSS Distributions 	if (cacheMode == kIODefaultCache) {
377*19c3b8c2SApple OSS Distributions 		return -1U;
378*19c3b8c2SApple OSS Distributions 	}
379*19c3b8c2SApple OSS Distributions 	return iomd_mem_types[cacheMode].wimg;
380*19c3b8c2SApple OSS Distributions }
381*19c3b8c2SApple OSS Distributions 
382*19c3b8c2SApple OSS Distributions static IOOptionBits
cacheModeForPagerFlags(unsigned int pagerFlags)383*19c3b8c2SApple OSS Distributions cacheModeForPagerFlags(unsigned int pagerFlags)
384*19c3b8c2SApple OSS Distributions {
385*19c3b8c2SApple OSS Distributions 	pagerFlags &= VM_WIMG_MASK;
386*19c3b8c2SApple OSS Distributions 	IOOptionBits cacheMode = kIODefaultCache;
387*19c3b8c2SApple OSS Distributions 	for (IOOptionBits i = 0; i < (sizeof(iomd_mem_types) / sizeof(iomd_mem_types[0])); ++i) {
388*19c3b8c2SApple OSS Distributions 		if (iomd_mem_types[i].wimg == pagerFlags) {
389*19c3b8c2SApple OSS Distributions 			cacheMode = i;
390*19c3b8c2SApple OSS Distributions 			break;
391*19c3b8c2SApple OSS Distributions 		}
392*19c3b8c2SApple OSS Distributions 	}
393*19c3b8c2SApple OSS Distributions 	return (cacheMode == kIODefaultCache) ? kIOCopybackCache : cacheMode;
394*19c3b8c2SApple OSS Distributions }
395*19c3b8c2SApple OSS Distributions 
396*19c3b8c2SApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
397*19c3b8c2SApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
398*19c3b8c2SApple OSS Distributions 
399*19c3b8c2SApple OSS Distributions struct IOMemoryEntry {
400*19c3b8c2SApple OSS Distributions 	ipc_port_t entry;
401*19c3b8c2SApple OSS Distributions 	int64_t    offset;
402*19c3b8c2SApple OSS Distributions 	uint64_t   size;
403*19c3b8c2SApple OSS Distributions 	uint64_t   start;
404*19c3b8c2SApple OSS Distributions };
405*19c3b8c2SApple OSS Distributions 
406*19c3b8c2SApple OSS Distributions struct IOMemoryReference {
407*19c3b8c2SApple OSS Distributions 	volatile SInt32             refCount;
408*19c3b8c2SApple OSS Distributions 	vm_prot_t                   prot;
409*19c3b8c2SApple OSS Distributions 	uint32_t                    capacity;
410*19c3b8c2SApple OSS Distributions 	uint32_t                    count;
411*19c3b8c2SApple OSS Distributions 	struct IOMemoryReference  * mapRef;
412*19c3b8c2SApple OSS Distributions 	IOMemoryEntry               entries[0];
413*19c3b8c2SApple OSS Distributions };
414*19c3b8c2SApple OSS Distributions 
415*19c3b8c2SApple OSS Distributions enum{
416*19c3b8c2SApple OSS Distributions 	kIOMemoryReferenceReuse = 0x00000001,
417*19c3b8c2SApple OSS Distributions 	kIOMemoryReferenceWrite = 0x00000002,
418*19c3b8c2SApple OSS Distributions 	kIOMemoryReferenceCOW   = 0x00000004,
419*19c3b8c2SApple OSS Distributions };
420*19c3b8c2SApple OSS Distributions 
421*19c3b8c2SApple OSS Distributions SInt32 gIOMemoryReferenceCount;
422*19c3b8c2SApple OSS Distributions 
423*19c3b8c2SApple OSS Distributions IOMemoryReference *
memoryReferenceAlloc(uint32_t capacity,IOMemoryReference * realloc)424*19c3b8c2SApple OSS Distributions IOGeneralMemoryDescriptor::memoryReferenceAlloc(uint32_t capacity, IOMemoryReference * realloc)
425*19c3b8c2SApple OSS Distributions {
426*19c3b8c2SApple OSS Distributions 	IOMemoryReference * ref;
427*19c3b8c2SApple OSS Distributions 	size_t              oldCapacity;
428*19c3b8c2SApple OSS Distributions 
429*19c3b8c2SApple OSS Distributions 	if (realloc) {
430*19c3b8c2SApple OSS Distributions 		oldCapacity = realloc->capacity;
431*19c3b8c2SApple OSS Distributions 	} else {
432*19c3b8c2SApple OSS Distributions 		oldCapacity = 0;
433*19c3b8c2SApple OSS Distributions 	}
434*19c3b8c2SApple OSS Distributions 
435*19c3b8c2SApple OSS Distributions 	// Use the kalloc API instead of manually handling the reallocation
436*19c3b8c2SApple OSS Distributions 	ref = krealloc_type(IOMemoryReference, IOMemoryEntry,
437*19c3b8c2SApple OSS Distributions 	    oldCapacity, capacity, realloc, Z_WAITOK_ZERO);
438*19c3b8c2SApple OSS Distributions 	if (ref) {
439*19c3b8c2SApple OSS Distributions 		if (oldCapacity == 0) {
440*19c3b8c2SApple OSS Distributions 			ref->refCount = 1;
441*19c3b8c2SApple OSS Distributions 			OSIncrementAtomic(&gIOMemoryReferenceCount);
442*19c3b8c2SApple OSS Distributions 		}
443*19c3b8c2SApple OSS Distributions 		ref->capacity = capacity;
444*19c3b8c2SApple OSS Distributions 	}
445*19c3b8c2SApple OSS Distributions 	return ref;
446*19c3b8c2SApple OSS Distributions }
447*19c3b8c2SApple OSS Distributions 
448*19c3b8c2SApple OSS Distributions void
memoryReferenceFree(IOMemoryReference * ref)449*19c3b8c2SApple OSS Distributions IOGeneralMemoryDescriptor::memoryReferenceFree(IOMemoryReference * ref)
450*19c3b8c2SApple OSS Distributions {
451*19c3b8c2SApple OSS Distributions 	IOMemoryEntry * entries;
452*19c3b8c2SApple OSS Distributions 
453*19c3b8c2SApple OSS Distributions 	if (ref->mapRef) {
454*19c3b8c2SApple OSS Distributions 		memoryReferenceFree(ref->mapRef);
455*19c3b8c2SApple OSS Distributions 		ref->mapRef = NULL;
456*19c3b8c2SApple OSS Distributions 	}
457*19c3b8c2SApple OSS Distributions 
458*19c3b8c2SApple OSS Distributions 	entries = ref->entries + ref->count;
459*19c3b8c2SApple OSS Distributions 	while (entries > &ref->entries[0]) {
460*19c3b8c2SApple OSS Distributions 		entries--;
461*19c3b8c2SApple OSS Distributions 		ipc_port_release_send(entries->entry);
462*19c3b8c2SApple OSS Distributions 	}
463*19c3b8c2SApple OSS Distributions 	kfree_type(IOMemoryReference, IOMemoryEntry, ref->capacity, ref);
464*19c3b8c2SApple OSS Distributions 
465*19c3b8c2SApple OSS Distributions 	OSDecrementAtomic(&gIOMemoryReferenceCount);
466*19c3b8c2SApple OSS Distributions }
467*19c3b8c2SApple OSS Distributions 
468*19c3b8c2SApple OSS Distributions void
memoryReferenceRelease(IOMemoryReference * ref)469*19c3b8c2SApple OSS Distributions IOGeneralMemoryDescriptor::memoryReferenceRelease(IOMemoryReference * ref)
470*19c3b8c2SApple OSS Distributions {
471*19c3b8c2SApple OSS Distributions 	if (1 == OSDecrementAtomic(&ref->refCount)) {
472*19c3b8c2SApple OSS Distributions 		memoryReferenceFree(ref);
473*19c3b8c2SApple OSS Distributions 	}
474*19c3b8c2SApple OSS Distributions }
475*19c3b8c2SApple OSS Distributions 
476*19c3b8c2SApple OSS Distributions 
477*19c3b8c2SApple OSS Distributions IOReturn
memoryReferenceCreate(IOOptionBits options,IOMemoryReference ** reference)478*19c3b8c2SApple OSS Distributions IOGeneralMemoryDescriptor::memoryReferenceCreate(
479*19c3b8c2SApple OSS Distributions 	IOOptionBits         options,
480*19c3b8c2SApple OSS Distributions 	IOMemoryReference ** reference)
481*19c3b8c2SApple OSS Distributions {
482*19c3b8c2SApple OSS Distributions 	enum { kCapacity = 4, kCapacityInc = 4 };
483*19c3b8c2SApple OSS Distributions 
484*19c3b8c2SApple OSS Distributions 	kern_return_t        err;
485*19c3b8c2SApple OSS Distributions 	IOMemoryReference *  ref;
486*19c3b8c2SApple OSS Distributions 	IOMemoryEntry *      entries;
487*19c3b8c2SApple OSS Distributions 	IOMemoryEntry *      cloneEntries = NULL;
488*19c3b8c2SApple OSS Distributions 	vm_map_t             map;
489*19c3b8c2SApple OSS Distributions 	ipc_port_t           entry, cloneEntry;
490*19c3b8c2SApple OSS Distributions 	vm_prot_t            prot;
491*19c3b8c2SApple OSS Distributions 	memory_object_size_t actualSize;
492*19c3b8c2SApple OSS Distributions 	uint32_t             rangeIdx;
493*19c3b8c2SApple OSS Distributions 	uint32_t             count;
494*19c3b8c2SApple OSS Distributions 	mach_vm_address_t    entryAddr, endAddr, entrySize;
495*19c3b8c2SApple OSS Distributions 	mach_vm_size_t       srcAddr, srcLen;
496*19c3b8c2SApple OSS Distributions 	mach_vm_size_t       nextAddr, nextLen;
497*19c3b8c2SApple OSS Distributions 	mach_vm_size_t       offset, remain;
498*19c3b8c2SApple OSS Distributions 	vm_map_offset_t      overmap_start = 0, overmap_end = 0;
499*19c3b8c2SApple OSS Distributions 	int                  misaligned_start = 0, misaligned_end = 0;
500*19c3b8c2SApple OSS Distributions 	IOByteCount          physLen;
501*19c3b8c2SApple OSS Distributions 	IOOptionBits         type = (_flags & kIOMemoryTypeMask);
502*19c3b8c2SApple OSS Distributions 	IOOptionBits         cacheMode;
503*19c3b8c2SApple OSS Distributions 	unsigned int         pagerFlags;
504*19c3b8c2SApple OSS Distributions 	vm_tag_t             tag;
505*19c3b8c2SApple OSS Distributions 	vm_named_entry_kernel_flags_t vmne_kflags;
506*19c3b8c2SApple OSS Distributions 
507*19c3b8c2SApple OSS Distributions 	ref = memoryReferenceAlloc(kCapacity, NULL);
508*19c3b8c2SApple OSS Distributions 	if (!ref) {
509*19c3b8c2SApple OSS Distributions 		return kIOReturnNoMemory;
510*19c3b8c2SApple OSS Distributions 	}
511*19c3b8c2SApple OSS Distributions 
512*19c3b8c2SApple OSS Distributions 	tag = (vm_tag_t) getVMTag(kernel_map);
513*19c3b8c2SApple OSS Distributions 	vmne_kflags = VM_NAMED_ENTRY_KERNEL_FLAGS_NONE;
514*19c3b8c2SApple OSS Distributions 	entries = &ref->entries[0];
515*19c3b8c2SApple OSS Distributions 	count = 0;
516*19c3b8c2SApple OSS Distributions 	err = KERN_SUCCESS;
517*19c3b8c2SApple OSS Distributions 
518*19c3b8c2SApple OSS Distributions 	offset = 0;
519*19c3b8c2SApple OSS Distributions 	rangeIdx = 0;
520*19c3b8c2SApple OSS Distributions 	remain = _length;
521*19c3b8c2SApple OSS Distributions 	if (_task) {
522*19c3b8c2SApple OSS Distributions 		getAddrLenForInd(nextAddr, nextLen, type, _ranges, rangeIdx);
523*19c3b8c2SApple OSS Distributions 
524*19c3b8c2SApple OSS Distributions 		// account for IOBMD setLength(), use its capacity as length
525*19c3b8c2SApple OSS Distributions 		IOBufferMemoryDescriptor * bmd;
526*19c3b8c2SApple OSS Distributions 		if ((bmd = OSDynamicCast(IOBufferMemoryDescriptor, this))) {
527*19c3b8c2SApple OSS Distributions 			nextLen = bmd->getCapacity();
528*19c3b8c2SApple OSS Distributions 			remain  = nextLen;
529*19c3b8c2SApple OSS Distributions 		}
530*19c3b8c2SApple OSS Distributions 	} else {
531*19c3b8c2SApple OSS Distributions 		nextAddr = getPhysicalSegment(offset, &physLen, kIOMemoryMapperNone);
532*19c3b8c2SApple OSS Distributions 		nextLen = physLen;
533*19c3b8c2SApple OSS Distributions 
534*19c3b8c2SApple OSS Distributions 		// default cache mode for physical
535*19c3b8c2SApple OSS Distributions 		if (kIODefaultCache == ((_flags & kIOMemoryBufferCacheMask) >> kIOMemoryBufferCacheShift)) {
536*19c3b8c2SApple OSS Distributions 			IOOptionBits mode = cacheModeForPagerFlags(IODefaultCacheBits(nextAddr));
537*19c3b8c2SApple OSS Distributions 			_flags |= (mode << kIOMemoryBufferCacheShift);
538*19c3b8c2SApple OSS Distributions 		}
539*19c3b8c2SApple OSS Distributions 	}
540*19c3b8c2SApple OSS Distributions 
541*19c3b8c2SApple OSS Distributions 	// cache mode & vm_prot
542*19c3b8c2SApple OSS Distributions 	prot = VM_PROT_READ;
543*19c3b8c2SApple OSS Distributions 	cacheMode = ((_flags & kIOMemoryBufferCacheMask) >> kIOMemoryBufferCacheShift);
544*19c3b8c2SApple OSS Distributions 	prot |= vmProtForCacheMode(cacheMode);
545*19c3b8c2SApple OSS Distributions 	// VM system requires write access to change cache mode
546*19c3b8c2SApple OSS Distributions 	if (kIODefaultCache != cacheMode) {
547*19c3b8c2SApple OSS Distributions 		prot |= VM_PROT_WRITE;
548*19c3b8c2SApple OSS Distributions 	}
549*19c3b8c2SApple OSS Distributions 	if (kIODirectionOut != (kIODirectionOutIn & _flags)) {
550*19c3b8c2SApple OSS Distributions 		prot |= VM_PROT_WRITE;
551*19c3b8c2SApple OSS Distributions 	}
552*19c3b8c2SApple OSS Distributions 	if (kIOMemoryReferenceWrite & options) {
553*19c3b8c2SApple OSS Distributions 		prot |= VM_PROT_WRITE;
554*19c3b8c2SApple OSS Distributions 	}
555*19c3b8c2SApple OSS Distributions 	if (kIOMemoryReferenceCOW   & options) {
556*19c3b8c2SApple OSS Distributions 		prot |= MAP_MEM_VM_COPY;
557*19c3b8c2SApple OSS Distributions 	}
558*19c3b8c2SApple OSS Distributions 
559*19c3b8c2SApple OSS Distributions 	if (kIOMemoryUseReserve & _flags) {
560*19c3b8c2SApple OSS Distributions 		prot |= MAP_MEM_GRAB_SECLUDED;
561*19c3b8c2SApple OSS Distributions 	}
562*19c3b8c2SApple OSS Distributions 
563*19c3b8c2SApple OSS Distributions 	if ((kIOMemoryReferenceReuse & options) && _memRef) {
564*19c3b8c2SApple OSS Distributions 		cloneEntries = &_memRef->entries[0];
565*19c3b8c2SApple OSS Distributions 		prot |= MAP_MEM_NAMED_REUSE;
566*19c3b8c2SApple OSS Distributions 	}
567*19c3b8c2SApple OSS Distributions 
568*19c3b8c2SApple OSS Distributions 	if (_task) {
569*19c3b8c2SApple OSS Distributions 		// virtual ranges
570*19c3b8c2SApple OSS Distributions 
571*19c3b8c2SApple OSS Distributions 		if (kIOMemoryBufferPageable & _flags) {
572*19c3b8c2SApple OSS Distributions 			int ledger_tag, ledger_no_footprint;
573*19c3b8c2SApple OSS Distributions 
574*19c3b8c2SApple OSS Distributions 			// IOBufferMemoryDescriptor alloc - set flags for entry + object create
575*19c3b8c2SApple OSS Distributions 			prot |= MAP_MEM_NAMED_CREATE;
576*19c3b8c2SApple OSS Distributions 
577*19c3b8c2SApple OSS Distributions 			// default accounting settings:
578*19c3b8c2SApple OSS Distributions 			//   + "none" ledger tag
579*19c3b8c2SApple OSS Distributions 			//   + include in footprint
580*19c3b8c2SApple OSS Distributions 			// can be changed later with ::setOwnership()
581*19c3b8c2SApple OSS Distributions 			ledger_tag = VM_LEDGER_TAG_NONE;
582*19c3b8c2SApple OSS Distributions 			ledger_no_footprint = 0;
583*19c3b8c2SApple OSS Distributions 
584*19c3b8c2SApple OSS Distributions 			if (kIOMemoryBufferPurgeable & _flags) {
585*19c3b8c2SApple OSS Distributions 				prot |= (MAP_MEM_PURGABLE | MAP_MEM_PURGABLE_KERNEL_ONLY);
586*19c3b8c2SApple OSS Distributions 				if (VM_KERN_MEMORY_SKYWALK == tag) {
587*19c3b8c2SApple OSS Distributions 					// Skywalk purgeable memory accounting:
588*19c3b8c2SApple OSS Distributions 					//    + "network" ledger tag
589*19c3b8c2SApple OSS Distributions 					//    + not included in footprint
590*19c3b8c2SApple OSS Distributions 					ledger_tag = VM_LEDGER_TAG_NETWORK;
591*19c3b8c2SApple OSS Distributions 					ledger_no_footprint = 1;
592*19c3b8c2SApple OSS Distributions 				} else {
593*19c3b8c2SApple OSS Distributions 					// regular purgeable memory accounting:
594*19c3b8c2SApple OSS Distributions 					//    + no ledger tag
595*19c3b8c2SApple OSS Distributions 					//    + included in footprint
596*19c3b8c2SApple OSS Distributions 					ledger_tag = VM_LEDGER_TAG_NONE;
597*19c3b8c2SApple OSS Distributions 					ledger_no_footprint = 0;
598*19c3b8c2SApple OSS Distributions 				}
599*19c3b8c2SApple OSS Distributions 			}
600*19c3b8c2SApple OSS Distributions 			vmne_kflags.vmnekf_ledger_tag = ledger_tag;
601*19c3b8c2SApple OSS Distributions 			vmne_kflags.vmnekf_ledger_no_footprint = ledger_no_footprint;
602*19c3b8c2SApple OSS Distributions 			if (kIOMemoryUseReserve & _flags) {
603*19c3b8c2SApple OSS Distributions 				prot |= MAP_MEM_GRAB_SECLUDED;
604*19c3b8c2SApple OSS Distributions 			}
605*19c3b8c2SApple OSS Distributions 
606*19c3b8c2SApple OSS Distributions 			prot |= VM_PROT_WRITE;
607*19c3b8c2SApple OSS Distributions 			map = NULL;
608*19c3b8c2SApple OSS Distributions 		} else {
609*19c3b8c2SApple OSS Distributions 			prot |= MAP_MEM_USE_DATA_ADDR;
610*19c3b8c2SApple OSS Distributions 			map = get_task_map(_task);
611*19c3b8c2SApple OSS Distributions 		}
612*19c3b8c2SApple OSS Distributions 		DEBUG4K_IOKIT("map %p _length 0x%llx prot 0x%x\n", map, (uint64_t)_length, prot);
613*19c3b8c2SApple OSS Distributions 
614*19c3b8c2SApple OSS Distributions 		while (remain) {
615*19c3b8c2SApple OSS Distributions 			srcAddr  = nextAddr;
616*19c3b8c2SApple OSS Distributions 			srcLen   = nextLen;
617*19c3b8c2SApple OSS Distributions 			nextAddr = 0;
618*19c3b8c2SApple OSS Distributions 			nextLen  = 0;
619*19c3b8c2SApple OSS Distributions 			// coalesce addr range
620*19c3b8c2SApple OSS Distributions 			for (++rangeIdx; rangeIdx < _rangesCount; rangeIdx++) {
621*19c3b8c2SApple OSS Distributions 				getAddrLenForInd(nextAddr, nextLen, type, _ranges, rangeIdx);
622*19c3b8c2SApple OSS Distributions 				if ((srcAddr + srcLen) != nextAddr) {
623*19c3b8c2SApple OSS Distributions 					break;
624*19c3b8c2SApple OSS Distributions 				}
625*19c3b8c2SApple OSS Distributions 				srcLen += nextLen;
626*19c3b8c2SApple OSS Distributions 			}
627*19c3b8c2SApple OSS Distributions 
628*19c3b8c2SApple OSS Distributions 			if (MAP_MEM_USE_DATA_ADDR & prot) {
629*19c3b8c2SApple OSS Distributions 				entryAddr = srcAddr;
630*19c3b8c2SApple OSS Distributions 				endAddr   = srcAddr + srcLen;
631*19c3b8c2SApple OSS Distributions 			} else {
632*19c3b8c2SApple OSS Distributions 				entryAddr = trunc_page_64(srcAddr);
633*19c3b8c2SApple OSS Distributions 				endAddr   = round_page_64(srcAddr + srcLen);
634*19c3b8c2SApple OSS Distributions 			}
635*19c3b8c2SApple OSS Distributions 			if (vm_map_page_mask(get_task_map(_task)) < PAGE_MASK) {
636*19c3b8c2SApple OSS Distributions 				DEBUG4K_IOKIT("IOMemRef %p _flags 0x%x prot 0x%x _ranges[%d]: 0x%llx 0x%llx\n", ref, (uint32_t)_flags, prot, rangeIdx - 1, srcAddr, srcLen);
637*19c3b8c2SApple OSS Distributions 			}
638*19c3b8c2SApple OSS Distributions 
639*19c3b8c2SApple OSS Distributions 			do{
640*19c3b8c2SApple OSS Distributions 				entrySize = (endAddr - entryAddr);
641*19c3b8c2SApple OSS Distributions 				if (!entrySize) {
642*19c3b8c2SApple OSS Distributions 					break;
643*19c3b8c2SApple OSS Distributions 				}
644*19c3b8c2SApple OSS Distributions 				actualSize = entrySize;
645*19c3b8c2SApple OSS Distributions 
646*19c3b8c2SApple OSS Distributions 				cloneEntry = MACH_PORT_NULL;
647*19c3b8c2SApple OSS Distributions 				if (MAP_MEM_NAMED_REUSE & prot) {
648*19c3b8c2SApple OSS Distributions 					if (cloneEntries < &_memRef->entries[_memRef->count]) {
649*19c3b8c2SApple OSS Distributions 						cloneEntry = cloneEntries->entry;
650*19c3b8c2SApple OSS Distributions 					} else {
651*19c3b8c2SApple OSS Distributions 						prot &= ~MAP_MEM_NAMED_REUSE;
652*19c3b8c2SApple OSS Distributions 					}
653*19c3b8c2SApple OSS Distributions 				}
654*19c3b8c2SApple OSS Distributions 
655*19c3b8c2SApple OSS Distributions 				err = mach_make_memory_entry_internal(map,
656*19c3b8c2SApple OSS Distributions 				    &actualSize, entryAddr, prot, vmne_kflags, &entry, cloneEntry);
657*19c3b8c2SApple OSS Distributions 
658*19c3b8c2SApple OSS Distributions 				if (KERN_SUCCESS != err) {
659*19c3b8c2SApple OSS Distributions 					DEBUG4K_ERROR("make_memory_entry(map %p, addr 0x%llx, size 0x%llx, prot 0x%x) err 0x%x\n", map, entryAddr, actualSize, prot, err);
660*19c3b8c2SApple OSS Distributions 					break;
661*19c3b8c2SApple OSS Distributions 				}
662*19c3b8c2SApple OSS Distributions 				if (MAP_MEM_USE_DATA_ADDR & prot) {
663*19c3b8c2SApple OSS Distributions 					if (actualSize > entrySize) {
664*19c3b8c2SApple OSS Distributions 						actualSize = entrySize;
665*19c3b8c2SApple OSS Distributions 					}
666*19c3b8c2SApple OSS Distributions 				} else if (actualSize > entrySize) {
667*19c3b8c2SApple OSS Distributions 					panic("mach_make_memory_entry_64 actualSize");
668*19c3b8c2SApple OSS Distributions 				}
669*19c3b8c2SApple OSS Distributions 
670*19c3b8c2SApple OSS Distributions 				memory_entry_check_for_adjustment(map, entry, &overmap_start, &overmap_end);
671*19c3b8c2SApple OSS Distributions 
672*19c3b8c2SApple OSS Distributions 				if (count && overmap_start) {
673*19c3b8c2SApple OSS Distributions 					/*
674*19c3b8c2SApple OSS Distributions 					 * Track misaligned start for all
675*19c3b8c2SApple OSS Distributions 					 * except the first entry.
676*19c3b8c2SApple OSS Distributions 					 */
677*19c3b8c2SApple OSS Distributions 					misaligned_start++;
678*19c3b8c2SApple OSS Distributions 				}
679*19c3b8c2SApple OSS Distributions 
680*19c3b8c2SApple OSS Distributions 				if (overmap_end) {
681*19c3b8c2SApple OSS Distributions 					/*
682*19c3b8c2SApple OSS Distributions 					 * Ignore misaligned end for the
683*19c3b8c2SApple OSS Distributions 					 * last entry.
684*19c3b8c2SApple OSS Distributions 					 */
685*19c3b8c2SApple OSS Distributions 					if ((entryAddr + actualSize) != endAddr) {
686*19c3b8c2SApple OSS Distributions 						misaligned_end++;
687*19c3b8c2SApple OSS Distributions 					}
688*19c3b8c2SApple OSS Distributions 				}
689*19c3b8c2SApple OSS Distributions 
690*19c3b8c2SApple OSS Distributions 				if (count) {
691*19c3b8c2SApple OSS Distributions 					/* Middle entries */
692*19c3b8c2SApple OSS Distributions 					if (misaligned_start || misaligned_end) {
693*19c3b8c2SApple OSS Distributions 						DEBUG4K_IOKIT("stopped at entryAddr 0x%llx\n", entryAddr);
694*19c3b8c2SApple OSS Distributions 						ipc_port_release_send(entry);
695*19c3b8c2SApple OSS Distributions 						err = KERN_NOT_SUPPORTED;
696*19c3b8c2SApple OSS Distributions 						break;
697*19c3b8c2SApple OSS Distributions 					}
698*19c3b8c2SApple OSS Distributions 				}
699*19c3b8c2SApple OSS Distributions 
700*19c3b8c2SApple OSS Distributions 				if (count >= ref->capacity) {
701*19c3b8c2SApple OSS Distributions 					ref = memoryReferenceAlloc(ref->capacity + kCapacityInc, ref);
702*19c3b8c2SApple OSS Distributions 					entries = &ref->entries[count];
703*19c3b8c2SApple OSS Distributions 				}
704*19c3b8c2SApple OSS Distributions 				entries->entry  = entry;
705*19c3b8c2SApple OSS Distributions 				entries->size   = actualSize;
706*19c3b8c2SApple OSS Distributions 				entries->offset = offset + (entryAddr - srcAddr);
707*19c3b8c2SApple OSS Distributions 				entries->start = entryAddr;
708*19c3b8c2SApple OSS Distributions 				entryAddr += actualSize;
709*19c3b8c2SApple OSS Distributions 				if (MAP_MEM_NAMED_REUSE & prot) {
710*19c3b8c2SApple OSS Distributions 					if ((cloneEntries->entry == entries->entry)
711*19c3b8c2SApple OSS Distributions 					    && (cloneEntries->size == entries->size)
712*19c3b8c2SApple OSS Distributions 					    && (cloneEntries->offset == entries->offset)) {
713*19c3b8c2SApple OSS Distributions 						cloneEntries++;
714*19c3b8c2SApple OSS Distributions 					} else {
715*19c3b8c2SApple OSS Distributions 						prot &= ~MAP_MEM_NAMED_REUSE;
716*19c3b8c2SApple OSS Distributions 					}
717*19c3b8c2SApple OSS Distributions 				}
718*19c3b8c2SApple OSS Distributions 				entries++;
719*19c3b8c2SApple OSS Distributions 				count++;
720*19c3b8c2SApple OSS Distributions 			}while (true);
721*19c3b8c2SApple OSS Distributions 			offset += srcLen;
722*19c3b8c2SApple OSS Distributions 			remain -= srcLen;
723*19c3b8c2SApple OSS Distributions 		}
724*19c3b8c2SApple OSS Distributions 	} else {
725*19c3b8c2SApple OSS Distributions 		// _task == 0, physical or kIOMemoryTypeUPL
726*19c3b8c2SApple OSS Distributions 		memory_object_t pager;
727*19c3b8c2SApple OSS Distributions 		vm_size_t       size = ptoa_64(_pages);
728*19c3b8c2SApple OSS Distributions 
729*19c3b8c2SApple OSS Distributions 		if (!getKernelReserved()) {
730*19c3b8c2SApple OSS Distributions 			panic("getKernelReserved");
731*19c3b8c2SApple OSS Distributions 		}
732*19c3b8c2SApple OSS Distributions 
733*19c3b8c2SApple OSS Distributions 		reserved->dp.pagerContig = (1 == _rangesCount);
734*19c3b8c2SApple OSS Distributions 		reserved->dp.memory      = this;
735*19c3b8c2SApple OSS Distributions 
736*19c3b8c2SApple OSS Distributions 		pagerFlags = pagerFlagsForCacheMode(cacheMode);
737*19c3b8c2SApple OSS Distributions 		if (-1U == pagerFlags) {
738*19c3b8c2SApple OSS Distributions 			panic("phys is kIODefaultCache");
739*19c3b8c2SApple OSS Distributions 		}
740*19c3b8c2SApple OSS Distributions 		if (reserved->dp.pagerContig) {
741*19c3b8c2SApple OSS Distributions 			pagerFlags |= DEVICE_PAGER_CONTIGUOUS;
742*19c3b8c2SApple OSS Distributions 		}
743*19c3b8c2SApple OSS Distributions 
744*19c3b8c2SApple OSS Distributions 		pager = device_pager_setup((memory_object_t) NULL, (uintptr_t) reserved,
745*19c3b8c2SApple OSS Distributions 		    size, pagerFlags);
746*19c3b8c2SApple OSS Distributions 		assert(pager);
747*19c3b8c2SApple OSS Distributions 		if (!pager) {
748*19c3b8c2SApple OSS Distributions 			DEBUG4K_ERROR("pager setup failed size 0x%llx flags 0x%x\n", (uint64_t)size, pagerFlags);
749*19c3b8c2SApple OSS Distributions 			err = kIOReturnVMError;
750*19c3b8c2SApple OSS Distributions 		} else {
751*19c3b8c2SApple OSS Distributions 			srcAddr  = nextAddr;
752*19c3b8c2SApple OSS Distributions 			entryAddr = trunc_page_64(srcAddr);
753*19c3b8c2SApple OSS Distributions 			err = mach_memory_object_memory_entry_64((host_t) 1, false /*internal*/,
754*19c3b8c2SApple OSS Distributions 			    size, VM_PROT_READ | VM_PROT_WRITE, pager, &entry);
755*19c3b8c2SApple OSS Distributions 			assert(KERN_SUCCESS == err);
756*19c3b8c2SApple OSS Distributions 			if (KERN_SUCCESS != err) {
757*19c3b8c2SApple OSS Distributions 				device_pager_deallocate(pager);
758*19c3b8c2SApple OSS Distributions 			} else {
759*19c3b8c2SApple OSS Distributions 				reserved->dp.devicePager = pager;
760*19c3b8c2SApple OSS Distributions 				entries->entry  = entry;
761*19c3b8c2SApple OSS Distributions 				entries->size   = size;
762*19c3b8c2SApple OSS Distributions 				entries->offset = offset + (entryAddr - srcAddr);
763*19c3b8c2SApple OSS Distributions 				entries++;
764*19c3b8c2SApple OSS Distributions 				count++;
765*19c3b8c2SApple OSS Distributions 			}
766*19c3b8c2SApple OSS Distributions 		}
767*19c3b8c2SApple OSS Distributions 	}
768*19c3b8c2SApple OSS Distributions 
769*19c3b8c2SApple OSS Distributions 	ref->count = count;
770*19c3b8c2SApple OSS Distributions 	ref->prot  = prot;
771*19c3b8c2SApple OSS Distributions 
772*19c3b8c2SApple OSS Distributions 	if (_task && (KERN_SUCCESS == err)
773*19c3b8c2SApple OSS Distributions 	    && (kIOMemoryMapCopyOnWrite & _flags)
774*19c3b8c2SApple OSS Distributions 	    && !(kIOMemoryReferenceCOW & options)) {
775*19c3b8c2SApple OSS Distributions 		err = memoryReferenceCreate(options | kIOMemoryReferenceCOW, &ref->mapRef);
776*19c3b8c2SApple OSS Distributions 		if (KERN_SUCCESS != err) {
777*19c3b8c2SApple OSS Distributions 			DEBUG4K_ERROR("ref %p options 0x%x err 0x%x\n", ref, (unsigned int)options, err);
778*19c3b8c2SApple OSS Distributions 		}
779*19c3b8c2SApple OSS Distributions 	}
780*19c3b8c2SApple OSS Distributions 
781*19c3b8c2SApple OSS Distributions 	if (KERN_SUCCESS == err) {
782*19c3b8c2SApple OSS Distributions 		if (MAP_MEM_NAMED_REUSE & prot) {
783*19c3b8c2SApple OSS Distributions 			memoryReferenceFree(ref);
784*19c3b8c2SApple OSS Distributions 			OSIncrementAtomic(&_memRef->refCount);
785*19c3b8c2SApple OSS Distributions 			ref = _memRef;
786*19c3b8c2SApple OSS Distributions 		}
787*19c3b8c2SApple OSS Distributions 	} else {
788*19c3b8c2SApple OSS Distributions 		DEBUG4K_ERROR("ref %p err 0x%x\n", ref, err);
789*19c3b8c2SApple OSS Distributions 		memoryReferenceFree(ref);
790*19c3b8c2SApple OSS Distributions 		ref = NULL;
791*19c3b8c2SApple OSS Distributions 	}
792*19c3b8c2SApple OSS Distributions 
793*19c3b8c2SApple OSS Distributions 	*reference = ref;
794*19c3b8c2SApple OSS Distributions 
795*19c3b8c2SApple OSS Distributions 	return err;
796*19c3b8c2SApple OSS Distributions }
797*19c3b8c2SApple OSS Distributions 
798*19c3b8c2SApple OSS Distributions static mach_vm_size_t
IOMemoryDescriptorMapGuardSize(vm_map_t map,IOOptionBits options)799*19c3b8c2SApple OSS Distributions IOMemoryDescriptorMapGuardSize(vm_map_t map, IOOptionBits options)
800*19c3b8c2SApple OSS Distributions {
801*19c3b8c2SApple OSS Distributions 	switch (kIOMapGuardedMask & options) {
802*19c3b8c2SApple OSS Distributions 	default:
803*19c3b8c2SApple OSS Distributions 	case kIOMapGuardedSmall:
804*19c3b8c2SApple OSS Distributions 		return vm_map_page_size(map);
805*19c3b8c2SApple OSS Distributions 	case kIOMapGuardedLarge:
806*19c3b8c2SApple OSS Distributions 		assert(0 == (kIOMapGuardSizeLarge & vm_map_page_mask(map)));
807*19c3b8c2SApple OSS Distributions 		return kIOMapGuardSizeLarge;
808*19c3b8c2SApple OSS Distributions 	}
809*19c3b8c2SApple OSS Distributions 	;
810*19c3b8c2SApple OSS Distributions }
811*19c3b8c2SApple OSS Distributions 
812*19c3b8c2SApple OSS Distributions static kern_return_t
IOMemoryDescriptorMapDealloc(IOOptionBits options,vm_map_t map,vm_map_offset_t addr,mach_vm_size_t size)813*19c3b8c2SApple OSS Distributions IOMemoryDescriptorMapDealloc(IOOptionBits options, vm_map_t map,
814*19c3b8c2SApple OSS Distributions     vm_map_offset_t addr, mach_vm_size_t size)
815*19c3b8c2SApple OSS Distributions {
816*19c3b8c2SApple OSS Distributions 	kern_return_t   kr;
817*19c3b8c2SApple OSS Distributions 	vm_map_offset_t actualAddr;
818*19c3b8c2SApple OSS Distributions 	mach_vm_size_t  actualSize;
819*19c3b8c2SApple OSS Distributions 
820*19c3b8c2SApple OSS Distributions 	actualAddr = vm_map_trunc_page(addr, vm_map_page_mask(map));
821*19c3b8c2SApple OSS Distributions 	actualSize = vm_map_round_page(addr + size, vm_map_page_mask(map)) - actualAddr;
822*19c3b8c2SApple OSS Distributions 
823*19c3b8c2SApple OSS Distributions 	if (kIOMapGuardedMask & options) {
824*19c3b8c2SApple OSS Distributions 		mach_vm_size_t guardSize = IOMemoryDescriptorMapGuardSize(map, options);
825*19c3b8c2SApple OSS Distributions 		actualAddr -= guardSize;
826*19c3b8c2SApple OSS Distributions 		actualSize += 2 * guardSize;
827*19c3b8c2SApple OSS Distributions 	}
828*19c3b8c2SApple OSS Distributions 	kr = mach_vm_deallocate(map, actualAddr, actualSize);
829*19c3b8c2SApple OSS Distributions 
830*19c3b8c2SApple OSS Distributions 	return kr;
831*19c3b8c2SApple OSS Distributions }
832*19c3b8c2SApple OSS Distributions 
833*19c3b8c2SApple OSS Distributions kern_return_t
IOMemoryDescriptorMapAlloc(vm_map_t map,void * _ref)834*19c3b8c2SApple OSS Distributions IOMemoryDescriptorMapAlloc(vm_map_t map, void * _ref)
835*19c3b8c2SApple OSS Distributions {
836*19c3b8c2SApple OSS Distributions 	IOMemoryDescriptorMapAllocRef * ref = (typeof(ref))_ref;
837*19c3b8c2SApple OSS Distributions 	IOReturn                        err;
838*19c3b8c2SApple OSS Distributions 	vm_map_offset_t                 addr;
839*19c3b8c2SApple OSS Distributions 	mach_vm_size_t                  size;
840*19c3b8c2SApple OSS Distributions 	mach_vm_size_t                  guardSize;
841*19c3b8c2SApple OSS Distributions 	vm_map_kernel_flags_t           vmk_flags = VM_MAP_KERNEL_FLAGS_NONE;
842*19c3b8c2SApple OSS Distributions 
843*19c3b8c2SApple OSS Distributions 	addr = ref->mapped;
844*19c3b8c2SApple OSS Distributions 	size = ref->size;
845*19c3b8c2SApple OSS Distributions 	guardSize = 0;
846*19c3b8c2SApple OSS Distributions 
847*19c3b8c2SApple OSS Distributions 	if (kIOMapGuardedMask & ref->options) {
848*19c3b8c2SApple OSS Distributions 		if (!(kIOMapAnywhere & ref->options)) {
849*19c3b8c2SApple OSS Distributions 			return kIOReturnBadArgument;
850*19c3b8c2SApple OSS Distributions 		}
851*19c3b8c2SApple OSS Distributions 		guardSize = IOMemoryDescriptorMapGuardSize(map, ref->options);
852*19c3b8c2SApple OSS Distributions 		size += 2 * guardSize;
853*19c3b8c2SApple OSS Distributions 	}
854*19c3b8c2SApple OSS Distributions 
855*19c3b8c2SApple OSS Distributions 	/*
856*19c3b8c2SApple OSS Distributions 	 * Mapping memory into the kernel_map using IOMDs use the data range.
857*19c3b8c2SApple OSS Distributions 	 * Memory being mapped should not contain kernel pointers.
858*19c3b8c2SApple OSS Distributions 	 */
859*19c3b8c2SApple OSS Distributions 	if (map == kernel_map) {
860*19c3b8c2SApple OSS Distributions 		vmk_flags.vmkf_range_id = KMEM_RANGE_ID_DATA;
861*19c3b8c2SApple OSS Distributions 	}
862*19c3b8c2SApple OSS Distributions 
863*19c3b8c2SApple OSS Distributions 	err = vm_map_enter_mem_object(map, &addr, size,
864*19c3b8c2SApple OSS Distributions #if __ARM_MIXED_PAGE_SIZE__
865*19c3b8c2SApple OSS Distributions 	    // TODO4K this should not be necessary...
866*19c3b8c2SApple OSS Distributions 	    (vm_map_offset_t)((ref->options & kIOMapAnywhere) ? max(PAGE_MASK, vm_map_page_mask(map)) : 0),
867*19c3b8c2SApple OSS Distributions #else /* __ARM_MIXED_PAGE_SIZE__ */
868*19c3b8c2SApple OSS Distributions 	    (vm_map_offset_t) 0,
869*19c3b8c2SApple OSS Distributions #endif /* __ARM_MIXED_PAGE_SIZE__ */
870*19c3b8c2SApple OSS Distributions 	    (((ref->options & kIOMapAnywhere)
871*19c3b8c2SApple OSS Distributions 	    ? VM_FLAGS_ANYWHERE
872*19c3b8c2SApple OSS Distributions 	    : VM_FLAGS_FIXED)),
873*19c3b8c2SApple OSS Distributions 	    vmk_flags,
874*19c3b8c2SApple OSS Distributions 	    ref->tag,
875*19c3b8c2SApple OSS Distributions 	    IPC_PORT_NULL,
876*19c3b8c2SApple OSS Distributions 	    (memory_object_offset_t) 0,
877*19c3b8c2SApple OSS Distributions 	    false,                       /* copy */
878*19c3b8c2SApple OSS Distributions 	    ref->prot,
879*19c3b8c2SApple OSS Distributions 	    ref->prot,
880*19c3b8c2SApple OSS Distributions 	    VM_INHERIT_NONE);
881*19c3b8c2SApple OSS Distributions 	if (KERN_SUCCESS == err) {
882*19c3b8c2SApple OSS Distributions 		ref->mapped = (mach_vm_address_t) addr;
883*19c3b8c2SApple OSS Distributions 		ref->map = map;
884*19c3b8c2SApple OSS Distributions 		if (kIOMapGuardedMask & ref->options) {
885*19c3b8c2SApple OSS Distributions 			vm_map_offset_t lastpage = vm_map_trunc_page(addr + size - guardSize, vm_map_page_mask(map));
886*19c3b8c2SApple OSS Distributions 
887*19c3b8c2SApple OSS Distributions 			err = vm_map_protect(map, addr, addr + guardSize, VM_PROT_NONE, false /*set_max*/);
888*19c3b8c2SApple OSS Distributions 			assert(KERN_SUCCESS == err);
889*19c3b8c2SApple OSS Distributions 			err = vm_map_protect(map, lastpage, lastpage + guardSize, VM_PROT_NONE, false /*set_max*/);
890*19c3b8c2SApple OSS Distributions 			assert(KERN_SUCCESS == err);
891*19c3b8c2SApple OSS Distributions 			ref->mapped += guardSize;
892*19c3b8c2SApple OSS Distributions 		}
893*19c3b8c2SApple OSS Distributions 	}
894*19c3b8c2SApple OSS Distributions 
895*19c3b8c2SApple OSS Distributions 	return err;
896*19c3b8c2SApple OSS Distributions }
897*19c3b8c2SApple OSS Distributions 
898*19c3b8c2SApple OSS Distributions IOReturn
memoryReferenceMap(IOMemoryReference * ref,vm_map_t map,mach_vm_size_t inoffset,mach_vm_size_t size,IOOptionBits options,mach_vm_address_t * inaddr)899*19c3b8c2SApple OSS Distributions IOGeneralMemoryDescriptor::memoryReferenceMap(
900*19c3b8c2SApple OSS Distributions 	IOMemoryReference * ref,
901*19c3b8c2SApple OSS Distributions 	vm_map_t            map,
902*19c3b8c2SApple OSS Distributions 	mach_vm_size_t      inoffset,
903*19c3b8c2SApple OSS Distributions 	mach_vm_size_t      size,
904*19c3b8c2SApple OSS Distributions 	IOOptionBits        options,
905*19c3b8c2SApple OSS Distributions 	mach_vm_address_t * inaddr)
906*19c3b8c2SApple OSS Distributions {
907*19c3b8c2SApple OSS Distributions 	IOReturn        err;
908*19c3b8c2SApple OSS Distributions 	int64_t         offset = inoffset;
909*19c3b8c2SApple OSS Distributions 	uint32_t        rangeIdx, entryIdx;
910*19c3b8c2SApple OSS Distributions 	vm_map_offset_t addr, mapAddr;
911*19c3b8c2SApple OSS Distributions 	vm_map_offset_t pageOffset, entryOffset, remain, chunk;
912*19c3b8c2SApple OSS Distributions 
913*19c3b8c2SApple OSS Distributions 	mach_vm_address_t nextAddr;
914*19c3b8c2SApple OSS Distributions 	mach_vm_size_t    nextLen;
915*19c3b8c2SApple OSS Distributions 	IOByteCount       physLen;
916*19c3b8c2SApple OSS Distributions 	IOMemoryEntry   * entry;
917*19c3b8c2SApple OSS Distributions 	vm_prot_t         prot, memEntryCacheMode;
918*19c3b8c2SApple OSS Distributions 	IOOptionBits      type;
919*19c3b8c2SApple OSS Distributions 	IOOptionBits      cacheMode;
920*19c3b8c2SApple OSS Distributions 	vm_tag_t          tag;
921*19c3b8c2SApple OSS Distributions 	// for the kIOMapPrefault option.
922*19c3b8c2SApple OSS Distributions 	upl_page_info_t * pageList = NULL;
923*19c3b8c2SApple OSS Distributions 	UInt              currentPageIndex = 0;
924*19c3b8c2SApple OSS Distributions 	bool              didAlloc;
925*19c3b8c2SApple OSS Distributions 
926*19c3b8c2SApple OSS Distributions 	DEBUG4K_IOKIT("ref %p map %p inoffset 0x%llx size 0x%llx options 0x%x *inaddr 0x%llx\n", ref, map, inoffset, size, (uint32_t)options, *inaddr);
927*19c3b8c2SApple OSS Distributions 
928*19c3b8c2SApple OSS Distributions 	if (ref->mapRef) {
929*19c3b8c2SApple OSS Distributions 		err = memoryReferenceMap(ref->mapRef, map, inoffset, size, options, inaddr);
930*19c3b8c2SApple OSS Distributions 		return err;
931*19c3b8c2SApple OSS Distributions 	}
932*19c3b8c2SApple OSS Distributions 
933*19c3b8c2SApple OSS Distributions 	if (MAP_MEM_USE_DATA_ADDR & ref->prot) {
934*19c3b8c2SApple OSS Distributions 		err = memoryReferenceMapNew(ref, map, inoffset, size, options, inaddr);
935*19c3b8c2SApple OSS Distributions 		return err;
936*19c3b8c2SApple OSS Distributions 	}
937*19c3b8c2SApple OSS Distributions 
938*19c3b8c2SApple OSS Distributions 	type = _flags & kIOMemoryTypeMask;
939*19c3b8c2SApple OSS Distributions 
940*19c3b8c2SApple OSS Distributions 	prot = VM_PROT_READ;
941*19c3b8c2SApple OSS Distributions 	if (!(kIOMapReadOnly & options)) {
942*19c3b8c2SApple OSS Distributions 		prot |= VM_PROT_WRITE;
943*19c3b8c2SApple OSS Distributions 	}
944*19c3b8c2SApple OSS Distributions 	prot &= ref->prot;
945*19c3b8c2SApple OSS Distributions 
946*19c3b8c2SApple OSS Distributions 	cacheMode = ((options & kIOMapCacheMask) >> kIOMapCacheShift);
947*19c3b8c2SApple OSS Distributions 	if (kIODefaultCache != cacheMode) {
948*19c3b8c2SApple OSS Distributions 		// VM system requires write access to update named entry cache mode
949*19c3b8c2SApple OSS Distributions 		memEntryCacheMode = (MAP_MEM_ONLY | VM_PROT_WRITE | prot | vmProtForCacheMode(cacheMode));
950*19c3b8c2SApple OSS Distributions 	}
951*19c3b8c2SApple OSS Distributions 
952*19c3b8c2SApple OSS Distributions 	tag = (typeof(tag))getVMTag(map);
953*19c3b8c2SApple OSS Distributions 
954*19c3b8c2SApple OSS Distributions 	if (_task) {
955*19c3b8c2SApple OSS Distributions 		// Find first range for offset
956*19c3b8c2SApple OSS Distributions 		if (!_rangesCount) {
957*19c3b8c2SApple OSS Distributions 			return kIOReturnBadArgument;
958*19c3b8c2SApple OSS Distributions 		}
959*19c3b8c2SApple OSS Distributions 		for (remain = offset, rangeIdx = 0; rangeIdx < _rangesCount; rangeIdx++) {
960*19c3b8c2SApple OSS Distributions 			getAddrLenForInd(nextAddr, nextLen, type, _ranges, rangeIdx);
961*19c3b8c2SApple OSS Distributions 			if (remain < nextLen) {
962*19c3b8c2SApple OSS Distributions 				break;
963*19c3b8c2SApple OSS Distributions 			}
964*19c3b8c2SApple OSS Distributions 			remain -= nextLen;
965*19c3b8c2SApple OSS Distributions 		}
966*19c3b8c2SApple OSS Distributions 	} else {
967*19c3b8c2SApple OSS Distributions 		rangeIdx = 0;
968*19c3b8c2SApple OSS Distributions 		remain   = 0;
969*19c3b8c2SApple OSS Distributions 		nextAddr = getPhysicalSegment(offset, &physLen, kIOMemoryMapperNone);
970*19c3b8c2SApple OSS Distributions 		nextLen  = size;
971*19c3b8c2SApple OSS Distributions 	}
972*19c3b8c2SApple OSS Distributions 
973*19c3b8c2SApple OSS Distributions 	assert(remain < nextLen);
974*19c3b8c2SApple OSS Distributions 	if (remain >= nextLen) {
975*19c3b8c2SApple OSS Distributions 		DEBUG4K_ERROR("map %p inoffset 0x%llx size 0x%llx options 0x%x inaddr 0x%llx remain 0x%llx nextLen 0x%llx\n", map, inoffset, size, (uint32_t)options, *inaddr, (uint64_t)remain, nextLen);
976*19c3b8c2SApple OSS Distributions 		return kIOReturnBadArgument;
977*19c3b8c2SApple OSS Distributions 	}
978*19c3b8c2SApple OSS Distributions 
979*19c3b8c2SApple OSS Distributions 	nextAddr  += remain;
980*19c3b8c2SApple OSS Distributions 	nextLen   -= remain;
981*19c3b8c2SApple OSS Distributions #if __ARM_MIXED_PAGE_SIZE__
982*19c3b8c2SApple OSS Distributions 	pageOffset = (vm_map_page_mask(map) & nextAddr);
983*19c3b8c2SApple OSS Distributions #else /* __ARM_MIXED_PAGE_SIZE__ */
984*19c3b8c2SApple OSS Distributions 	pageOffset = (page_mask & nextAddr);
985*19c3b8c2SApple OSS Distributions #endif /* __ARM_MIXED_PAGE_SIZE__ */
986*19c3b8c2SApple OSS Distributions 	addr       = 0;
987*19c3b8c2SApple OSS Distributions 	didAlloc   = false;
988*19c3b8c2SApple OSS Distributions 
989*19c3b8c2SApple OSS Distributions 	if (!(options & kIOMapAnywhere)) {
990*19c3b8c2SApple OSS Distributions 		addr = *inaddr;
991*19c3b8c2SApple OSS Distributions 		if (pageOffset != (vm_map_page_mask(map) & addr)) {
992*19c3b8c2SApple OSS Distributions 			DEBUG4K_ERROR("map %p inoffset 0x%llx size 0x%llx options 0x%x inaddr 0x%llx addr 0x%llx page_mask 0x%llx pageOffset 0x%llx\n", map, inoffset, size, (uint32_t)options, *inaddr, (uint64_t)addr, (uint64_t)page_mask, (uint64_t)pageOffset);
993*19c3b8c2SApple OSS Distributions 		}
994*19c3b8c2SApple OSS Distributions 		addr -= pageOffset;
995*19c3b8c2SApple OSS Distributions 	}
996*19c3b8c2SApple OSS Distributions 
997*19c3b8c2SApple OSS Distributions 	// find first entry for offset
998*19c3b8c2SApple OSS Distributions 	for (entryIdx = 0;
999*19c3b8c2SApple OSS Distributions 	    (entryIdx < ref->count) && (offset >= ref->entries[entryIdx].offset);
1000*19c3b8c2SApple OSS Distributions 	    entryIdx++) {
1001*19c3b8c2SApple OSS Distributions 	}
1002*19c3b8c2SApple OSS Distributions 	entryIdx--;
1003*19c3b8c2SApple OSS Distributions 	entry = &ref->entries[entryIdx];
1004*19c3b8c2SApple OSS Distributions 
1005*19c3b8c2SApple OSS Distributions 	// allocate VM
1006*19c3b8c2SApple OSS Distributions #if __ARM_MIXED_PAGE_SIZE__
1007*19c3b8c2SApple OSS Distributions 	size = round_page_mask_64(size + pageOffset, vm_map_page_mask(map));
1008*19c3b8c2SApple OSS Distributions #else
1009*19c3b8c2SApple OSS Distributions 	size = round_page_64(size + pageOffset);
1010*19c3b8c2SApple OSS Distributions #endif
1011*19c3b8c2SApple OSS Distributions 	if (kIOMapOverwrite & options) {
1012*19c3b8c2SApple OSS Distributions 		if ((map == kernel_map) && (kIOMemoryBufferPageable & _flags)) {
1013*19c3b8c2SApple OSS Distributions 			map = IOPageableMapForAddress(addr);
1014*19c3b8c2SApple OSS Distributions 		}
1015*19c3b8c2SApple OSS Distributions 		err = KERN_SUCCESS;
1016*19c3b8c2SApple OSS Distributions 	} else {
1017*19c3b8c2SApple OSS Distributions 		IOMemoryDescriptorMapAllocRef ref;
1018*19c3b8c2SApple OSS Distributions 		ref.map     = map;
1019*19c3b8c2SApple OSS Distributions 		ref.tag     = tag;
1020*19c3b8c2SApple OSS Distributions 		ref.options = options;
1021*19c3b8c2SApple OSS Distributions 		ref.size    = size;
1022*19c3b8c2SApple OSS Distributions 		ref.prot    = prot;
1023*19c3b8c2SApple OSS Distributions 		if (options & kIOMapAnywhere) {
1024*19c3b8c2SApple OSS Distributions 			// vm_map looks for addresses above here, even when VM_FLAGS_ANYWHERE
1025*19c3b8c2SApple OSS Distributions 			ref.mapped = 0;
1026*19c3b8c2SApple OSS Distributions 		} else {
1027*19c3b8c2SApple OSS Distributions 			ref.mapped = addr;
1028*19c3b8c2SApple OSS Distributions 		}
1029*19c3b8c2SApple OSS Distributions 		if ((ref.map == kernel_map) && (kIOMemoryBufferPageable & _flags)) {
1030*19c3b8c2SApple OSS Distributions 			err = IOIteratePageableMaps( ref.size, &IOMemoryDescriptorMapAlloc, &ref );
1031*19c3b8c2SApple OSS Distributions 		} else {
1032*19c3b8c2SApple OSS Distributions 			err = IOMemoryDescriptorMapAlloc(ref.map, &ref);
1033*19c3b8c2SApple OSS Distributions 		}
1034*19c3b8c2SApple OSS Distributions 		if (KERN_SUCCESS == err) {
1035*19c3b8c2SApple OSS Distributions 			addr     = ref.mapped;
1036*19c3b8c2SApple OSS Distributions 			map      = ref.map;
1037*19c3b8c2SApple OSS Distributions 			didAlloc = true;
1038*19c3b8c2SApple OSS Distributions 		}
1039*19c3b8c2SApple OSS Distributions 	}
1040*19c3b8c2SApple OSS Distributions 
1041*19c3b8c2SApple OSS Distributions 	/*
1042*19c3b8c2SApple OSS Distributions 	 * If the memory is associated with a device pager but doesn't have a UPL,
1043*19c3b8c2SApple OSS Distributions 	 * it will be immediately faulted in through the pager via populateDevicePager().
1044*19c3b8c2SApple OSS Distributions 	 * kIOMapPrefault is redundant in that case, so don't try to use it for UPL
1045*19c3b8c2SApple OSS Distributions 	 * operations.
1046*19c3b8c2SApple OSS Distributions 	 */
1047*19c3b8c2SApple OSS Distributions 	if ((reserved != NULL) && (reserved->dp.devicePager) && (_wireCount != 0)) {
1048*19c3b8c2SApple OSS Distributions 		options &= ~kIOMapPrefault;
1049*19c3b8c2SApple OSS Distributions 	}
1050*19c3b8c2SApple OSS Distributions 
1051*19c3b8c2SApple OSS Distributions 	/*
1052*19c3b8c2SApple OSS Distributions 	 * Prefaulting is only possible if we wired the memory earlier. Check the
1053*19c3b8c2SApple OSS Distributions 	 * memory type, and the underlying data.
1054*19c3b8c2SApple OSS Distributions 	 */
1055*19c3b8c2SApple OSS Distributions 	if (options & kIOMapPrefault) {
1056*19c3b8c2SApple OSS Distributions 		/*
1057*19c3b8c2SApple OSS Distributions 		 * The memory must have been wired by calling ::prepare(), otherwise
1058*19c3b8c2SApple OSS Distributions 		 * we don't have the UPL. Without UPLs, pages cannot be pre-faulted
1059*19c3b8c2SApple OSS Distributions 		 */
1060*19c3b8c2SApple OSS Distributions 		assert(_wireCount != 0);
1061*19c3b8c2SApple OSS Distributions 		assert(_memoryEntries != NULL);
1062*19c3b8c2SApple OSS Distributions 		if ((_wireCount == 0) ||
1063*19c3b8c2SApple OSS Distributions 		    (_memoryEntries == NULL)) {
1064*19c3b8c2SApple OSS Distributions 			DEBUG4K_ERROR("map %p inoffset 0x%llx size 0x%llx options 0x%x inaddr 0x%llx\n", map, inoffset, size, (uint32_t)options, *inaddr);
1065*19c3b8c2SApple OSS Distributions 			return kIOReturnBadArgument;
1066*19c3b8c2SApple OSS Distributions 		}
1067*19c3b8c2SApple OSS Distributions 
1068*19c3b8c2SApple OSS Distributions 		// Get the page list.
1069*19c3b8c2SApple OSS Distributions 		ioGMDData* dataP = getDataP(_memoryEntries);
1070*19c3b8c2SApple OSS Distributions 		ioPLBlock const* ioplList = getIOPLList(dataP);
1071*19c3b8c2SApple OSS Distributions 		pageList = getPageList(dataP);
1072*19c3b8c2SApple OSS Distributions 
1073*19c3b8c2SApple OSS Distributions 		// Get the number of IOPLs.
1074*19c3b8c2SApple OSS Distributions 		UInt numIOPLs = getNumIOPL(_memoryEntries, dataP);
1075*19c3b8c2SApple OSS Distributions 
1076*19c3b8c2SApple OSS Distributions 		/*
1077*19c3b8c2SApple OSS Distributions 		 * Scan through the IOPL Info Blocks, looking for the first block containing
1078*19c3b8c2SApple OSS Distributions 		 * the offset. The research will go past it, so we'll need to go back to the
1079*19c3b8c2SApple OSS Distributions 		 * right range at the end.
1080*19c3b8c2SApple OSS Distributions 		 */
1081*19c3b8c2SApple OSS Distributions 		UInt ioplIndex = 0;
1082*19c3b8c2SApple OSS Distributions 		while ((ioplIndex < numIOPLs) && (((uint64_t) offset) >= ioplList[ioplIndex].fIOMDOffset)) {
1083*19c3b8c2SApple OSS Distributions 			ioplIndex++;
1084*19c3b8c2SApple OSS Distributions 		}
1085*19c3b8c2SApple OSS Distributions 		ioplIndex--;
1086*19c3b8c2SApple OSS Distributions 
1087*19c3b8c2SApple OSS Distributions 		// Retrieve the IOPL info block.
1088*19c3b8c2SApple OSS Distributions 		ioPLBlock ioplInfo = ioplList[ioplIndex];
1089*19c3b8c2SApple OSS Distributions 
1090*19c3b8c2SApple OSS Distributions 		/*
1091*19c3b8c2SApple OSS Distributions 		 * For external UPLs, the fPageInfo points directly to the UPL's page_info_t
1092*19c3b8c2SApple OSS Distributions 		 * array.
1093*19c3b8c2SApple OSS Distributions 		 */
1094*19c3b8c2SApple OSS Distributions 		if (ioplInfo.fFlags & kIOPLExternUPL) {
1095*19c3b8c2SApple OSS Distributions 			pageList = (upl_page_info_t*) ioplInfo.fPageInfo;
1096*19c3b8c2SApple OSS Distributions 		} else {
1097*19c3b8c2SApple OSS Distributions 			pageList = &pageList[ioplInfo.fPageInfo];
1098*19c3b8c2SApple OSS Distributions 		}
1099*19c3b8c2SApple OSS Distributions 
1100*19c3b8c2SApple OSS Distributions 		// Rebase [offset] into the IOPL in order to looks for the first page index.
1101*19c3b8c2SApple OSS Distributions 		mach_vm_size_t offsetInIOPL = offset - ioplInfo.fIOMDOffset + ioplInfo.fPageOffset;
1102*19c3b8c2SApple OSS Distributions 
1103*19c3b8c2SApple OSS Distributions 		// Retrieve the index of the first page corresponding to the offset.
1104*19c3b8c2SApple OSS Distributions 		currentPageIndex = atop_32(offsetInIOPL);
1105*19c3b8c2SApple OSS Distributions 	}
1106*19c3b8c2SApple OSS Distributions 
1107*19c3b8c2SApple OSS Distributions 	// enter mappings
1108*19c3b8c2SApple OSS Distributions 	remain  = size;
1109*19c3b8c2SApple OSS Distributions 	mapAddr = addr;
1110*19c3b8c2SApple OSS Distributions 	addr    += pageOffset;
1111*19c3b8c2SApple OSS Distributions 
1112*19c3b8c2SApple OSS Distributions 	while (remain && (KERN_SUCCESS == err)) {
1113*19c3b8c2SApple OSS Distributions 		entryOffset = offset - entry->offset;
1114*19c3b8c2SApple OSS Distributions 		if ((min(vm_map_page_mask(map), page_mask) & entryOffset) != pageOffset) {
1115*19c3b8c2SApple OSS Distributions 			err = kIOReturnNotAligned;
1116*19c3b8c2SApple OSS Distributions 			DEBUG4K_ERROR("map %p inoffset 0x%llx size 0x%llx options 0x%x inaddr 0x%llx entryOffset 0x%llx pageOffset 0x%llx\n", map, inoffset, size, (uint32_t)options, *inaddr, (uint64_t)entryOffset, (uint64_t)pageOffset);
1117*19c3b8c2SApple OSS Distributions 			break;
1118*19c3b8c2SApple OSS Distributions 		}
1119*19c3b8c2SApple OSS Distributions 
1120*19c3b8c2SApple OSS Distributions 		if (kIODefaultCache != cacheMode) {
1121*19c3b8c2SApple OSS Distributions 			vm_size_t unused = 0;
1122*19c3b8c2SApple OSS Distributions 			err = mach_make_memory_entry(NULL /*unused*/, &unused, 0 /*unused*/,
1123*19c3b8c2SApple OSS Distributions 			    memEntryCacheMode, NULL, entry->entry);
1124*19c3b8c2SApple OSS Distributions 			assert(KERN_SUCCESS == err);
1125*19c3b8c2SApple OSS Distributions 		}
1126*19c3b8c2SApple OSS Distributions 
1127*19c3b8c2SApple OSS Distributions 		entryOffset -= pageOffset;
1128*19c3b8c2SApple OSS Distributions 		if (entryOffset >= entry->size) {
1129*19c3b8c2SApple OSS Distributions 			panic("entryOffset");
1130*19c3b8c2SApple OSS Distributions 		}
1131*19c3b8c2SApple OSS Distributions 		chunk = entry->size - entryOffset;
1132*19c3b8c2SApple OSS Distributions 		if (chunk) {
1133*19c3b8c2SApple OSS Distributions 			vm_map_kernel_flags_t vmk_flags;
1134*19c3b8c2SApple OSS Distributions 
1135*19c3b8c2SApple OSS Distributions 			vmk_flags = VM_MAP_KERNEL_FLAGS_NONE;
1136*19c3b8c2SApple OSS Distributions 			vmk_flags.vmkf_iokit_acct = TRUE; /* iokit accounting */
1137*19c3b8c2SApple OSS Distributions 
1138*19c3b8c2SApple OSS Distributions 			if (chunk > remain) {
1139*19c3b8c2SApple OSS Distributions 				chunk = remain;
1140*19c3b8c2SApple OSS Distributions 			}
1141*19c3b8c2SApple OSS Distributions 			if (options & kIOMapPrefault) {
1142*19c3b8c2SApple OSS Distributions 				UInt nb_pages = (typeof(nb_pages))round_page(chunk) / PAGE_SIZE;
1143*19c3b8c2SApple OSS Distributions 
1144*19c3b8c2SApple OSS Distributions 				err = vm_map_enter_mem_object_prefault(map,
1145*19c3b8c2SApple OSS Distributions 				    &mapAddr,
1146*19c3b8c2SApple OSS Distributions 				    chunk, 0 /* mask */,
1147*19c3b8c2SApple OSS Distributions 				    (VM_FLAGS_FIXED
1148*19c3b8c2SApple OSS Distributions 				    | VM_FLAGS_OVERWRITE),
1149*19c3b8c2SApple OSS Distributions 				    vmk_flags,
1150*19c3b8c2SApple OSS Distributions 				    tag,
1151*19c3b8c2SApple OSS Distributions 				    entry->entry,
1152*19c3b8c2SApple OSS Distributions 				    entryOffset,
1153*19c3b8c2SApple OSS Distributions 				    prot,                        // cur
1154*19c3b8c2SApple OSS Distributions 				    prot,                        // max
1155*19c3b8c2SApple OSS Distributions 				    &pageList[currentPageIndex],
1156*19c3b8c2SApple OSS Distributions 				    nb_pages);
1157*19c3b8c2SApple OSS Distributions 
1158*19c3b8c2SApple OSS Distributions 				if (err || vm_map_page_mask(map) < PAGE_MASK) {
1159*19c3b8c2SApple OSS Distributions 					DEBUG4K_IOKIT("IOMemRef %p mapped in map %p (pgshift %d) at 0x%llx size 0x%llx err 0x%x\n", ref, map, vm_map_page_shift(map), (uint64_t)mapAddr, (uint64_t)chunk, err);
1160*19c3b8c2SApple OSS Distributions 				}
1161*19c3b8c2SApple OSS Distributions 				// Compute the next index in the page list.
1162*19c3b8c2SApple OSS Distributions 				currentPageIndex += nb_pages;
1163*19c3b8c2SApple OSS Distributions 				assert(currentPageIndex <= _pages);
1164*19c3b8c2SApple OSS Distributions 			} else {
1165*19c3b8c2SApple OSS Distributions 				err = vm_map_enter_mem_object(map,
1166*19c3b8c2SApple OSS Distributions 				    &mapAddr,
1167*19c3b8c2SApple OSS Distributions 				    chunk, 0 /* mask */,
1168*19c3b8c2SApple OSS Distributions 				    (VM_FLAGS_FIXED
1169*19c3b8c2SApple OSS Distributions 				    | VM_FLAGS_OVERWRITE),
1170*19c3b8c2SApple OSS Distributions 				    vmk_flags,
1171*19c3b8c2SApple OSS Distributions 				    tag,
1172*19c3b8c2SApple OSS Distributions 				    entry->entry,
1173*19c3b8c2SApple OSS Distributions 				    entryOffset,
1174*19c3b8c2SApple OSS Distributions 				    false,               // copy
1175*19c3b8c2SApple OSS Distributions 				    prot,               // cur
1176*19c3b8c2SApple OSS Distributions 				    prot,               // max
1177*19c3b8c2SApple OSS Distributions 				    VM_INHERIT_NONE);
1178*19c3b8c2SApple OSS Distributions 			}
1179*19c3b8c2SApple OSS Distributions 			if (KERN_SUCCESS != err) {
1180*19c3b8c2SApple OSS Distributions 				DEBUG4K_ERROR("IOMemRef %p mapped in map %p (pgshift %d) at 0x%llx size 0x%llx err 0x%x\n", ref, map, vm_map_page_shift(map), (uint64_t)mapAddr, (uint64_t)chunk, err);
1181*19c3b8c2SApple OSS Distributions 				break;
1182*19c3b8c2SApple OSS Distributions 			}
1183*19c3b8c2SApple OSS Distributions 			remain -= chunk;
1184*19c3b8c2SApple OSS Distributions 			if (!remain) {
1185*19c3b8c2SApple OSS Distributions 				break;
1186*19c3b8c2SApple OSS Distributions 			}
1187*19c3b8c2SApple OSS Distributions 			mapAddr  += chunk;
1188*19c3b8c2SApple OSS Distributions 			offset   += chunk - pageOffset;
1189*19c3b8c2SApple OSS Distributions 		}
1190*19c3b8c2SApple OSS Distributions 		pageOffset = 0;
1191*19c3b8c2SApple OSS Distributions 		entry++;
1192*19c3b8c2SApple OSS Distributions 		entryIdx++;
1193*19c3b8c2SApple OSS Distributions 		if (entryIdx >= ref->count) {
1194*19c3b8c2SApple OSS Distributions 			err = kIOReturnOverrun;
1195*19c3b8c2SApple OSS Distributions 			DEBUG4K_ERROR("map %p inoffset 0x%llx size 0x%llx options 0x%x inaddr 0x%llx entryIdx %d ref->count %d\n", map, inoffset, size, (uint32_t)options, *inaddr, entryIdx, ref->count);
1196*19c3b8c2SApple OSS Distributions 			break;
1197*19c3b8c2SApple OSS Distributions 		}
1198*19c3b8c2SApple OSS Distributions 	}
1199*19c3b8c2SApple OSS Distributions 
1200*19c3b8c2SApple OSS Distributions 	if ((KERN_SUCCESS != err) && didAlloc) {
1201*19c3b8c2SApple OSS Distributions 		(void) IOMemoryDescriptorMapDealloc(options, map, trunc_page_64(addr), size);
1202*19c3b8c2SApple OSS Distributions 		addr = 0;
1203*19c3b8c2SApple OSS Distributions 	}
1204*19c3b8c2SApple OSS Distributions 	*inaddr = addr;
1205*19c3b8c2SApple OSS Distributions 
1206*19c3b8c2SApple OSS Distributions 	if (err /* || vm_map_page_mask(map) < PAGE_MASK */) {
1207*19c3b8c2SApple OSS Distributions 		DEBUG4K_ERROR("map %p (%d) inoffset 0x%llx size 0x%llx options 0x%x inaddr 0x%llx err 0x%x\n", map, vm_map_page_shift(map), inoffset, size, (uint32_t)options, *inaddr, err);
1208*19c3b8c2SApple OSS Distributions 	}
1209*19c3b8c2SApple OSS Distributions 	return err;
1210*19c3b8c2SApple OSS Distributions }
1211*19c3b8c2SApple OSS Distributions 
1212*19c3b8c2SApple OSS Distributions #define LOGUNALIGN 0
1213*19c3b8c2SApple OSS Distributions IOReturn
memoryReferenceMapNew(IOMemoryReference * ref,vm_map_t map,mach_vm_size_t inoffset,mach_vm_size_t size,IOOptionBits options,mach_vm_address_t * inaddr)1214*19c3b8c2SApple OSS Distributions IOGeneralMemoryDescriptor::memoryReferenceMapNew(
1215*19c3b8c2SApple OSS Distributions 	IOMemoryReference * ref,
1216*19c3b8c2SApple OSS Distributions 	vm_map_t            map,
1217*19c3b8c2SApple OSS Distributions 	mach_vm_size_t      inoffset,
1218*19c3b8c2SApple OSS Distributions 	mach_vm_size_t      size,
1219*19c3b8c2SApple OSS Distributions 	IOOptionBits        options,
1220*19c3b8c2SApple OSS Distributions 	mach_vm_address_t * inaddr)
1221*19c3b8c2SApple OSS Distributions {
1222*19c3b8c2SApple OSS Distributions 	IOReturn            err;
1223*19c3b8c2SApple OSS Distributions 	int64_t             offset = inoffset;
1224*19c3b8c2SApple OSS Distributions 	uint32_t            entryIdx, firstEntryIdx;
1225*19c3b8c2SApple OSS Distributions 	vm_map_offset_t     addr, mapAddr, mapAddrOut;
1226*19c3b8c2SApple OSS Distributions 	vm_map_offset_t     entryOffset, remain, chunk;
1227*19c3b8c2SApple OSS Distributions 
1228*19c3b8c2SApple OSS Distributions 	IOMemoryEntry    * entry;
1229*19c3b8c2SApple OSS Distributions 	vm_prot_t          prot, memEntryCacheMode;
1230*19c3b8c2SApple OSS Distributions 	IOOptionBits       type;
1231*19c3b8c2SApple OSS Distributions 	IOOptionBits       cacheMode;
1232*19c3b8c2SApple OSS Distributions 	vm_tag_t           tag;
1233*19c3b8c2SApple OSS Distributions 	// for the kIOMapPrefault option.
1234*19c3b8c2SApple OSS Distributions 	upl_page_info_t  * pageList = NULL;
1235*19c3b8c2SApple OSS Distributions 	UInt               currentPageIndex = 0;
1236*19c3b8c2SApple OSS Distributions 	bool               didAlloc;
1237*19c3b8c2SApple OSS Distributions 
1238*19c3b8c2SApple OSS Distributions 	DEBUG4K_IOKIT("ref %p map %p inoffset 0x%llx size 0x%llx options 0x%x *inaddr 0x%llx\n", ref, map, inoffset, size, (uint32_t)options, *inaddr);
1239*19c3b8c2SApple OSS Distributions 
1240*19c3b8c2SApple OSS Distributions 	if (ref->mapRef) {
1241*19c3b8c2SApple OSS Distributions 		err = memoryReferenceMap(ref->mapRef, map, inoffset, size, options, inaddr);
1242*19c3b8c2SApple OSS Distributions 		return err;
1243*19c3b8c2SApple OSS Distributions 	}
1244*19c3b8c2SApple OSS Distributions 
1245*19c3b8c2SApple OSS Distributions #if LOGUNALIGN
1246*19c3b8c2SApple OSS Distributions 	printf("MAP offset %qx, %qx\n", inoffset, size);
1247*19c3b8c2SApple OSS Distributions #endif
1248*19c3b8c2SApple OSS Distributions 
1249*19c3b8c2SApple OSS Distributions 	type = _flags & kIOMemoryTypeMask;
1250*19c3b8c2SApple OSS Distributions 
1251*19c3b8c2SApple OSS Distributions 	prot = VM_PROT_READ;
1252*19c3b8c2SApple OSS Distributions 	if (!(kIOMapReadOnly & options)) {
1253*19c3b8c2SApple OSS Distributions 		prot |= VM_PROT_WRITE;
1254*19c3b8c2SApple OSS Distributions 	}
1255*19c3b8c2SApple OSS Distributions 	prot &= ref->prot;
1256*19c3b8c2SApple OSS Distributions 
1257*19c3b8c2SApple OSS Distributions 	cacheMode = ((options & kIOMapCacheMask) >> kIOMapCacheShift);
1258*19c3b8c2SApple OSS Distributions 	if (kIODefaultCache != cacheMode) {
1259*19c3b8c2SApple OSS Distributions 		// VM system requires write access to update named entry cache mode
1260*19c3b8c2SApple OSS Distributions 		memEntryCacheMode = (MAP_MEM_ONLY | VM_PROT_WRITE | prot | vmProtForCacheMode(cacheMode));
1261*19c3b8c2SApple OSS Distributions 	}
1262*19c3b8c2SApple OSS Distributions 
1263*19c3b8c2SApple OSS Distributions 	tag = (vm_tag_t) getVMTag(map);
1264*19c3b8c2SApple OSS Distributions 
1265*19c3b8c2SApple OSS Distributions 	addr       = 0;
1266*19c3b8c2SApple OSS Distributions 	didAlloc   = false;
1267*19c3b8c2SApple OSS Distributions 
1268*19c3b8c2SApple OSS Distributions 	if (!(options & kIOMapAnywhere)) {
1269*19c3b8c2SApple OSS Distributions 		addr = *inaddr;
1270*19c3b8c2SApple OSS Distributions 	}
1271*19c3b8c2SApple OSS Distributions 
1272*19c3b8c2SApple OSS Distributions 	// find first entry for offset
1273*19c3b8c2SApple OSS Distributions 	for (firstEntryIdx = 0;
1274*19c3b8c2SApple OSS Distributions 	    (firstEntryIdx < ref->count) && (offset >= ref->entries[firstEntryIdx].offset);
1275*19c3b8c2SApple OSS Distributions 	    firstEntryIdx++) {
1276*19c3b8c2SApple OSS Distributions 	}
1277*19c3b8c2SApple OSS Distributions 	firstEntryIdx--;
1278*19c3b8c2SApple OSS Distributions 
1279*19c3b8c2SApple OSS Distributions 	// calculate required VM space
1280*19c3b8c2SApple OSS Distributions 
1281*19c3b8c2SApple OSS Distributions 	entryIdx = firstEntryIdx;
1282*19c3b8c2SApple OSS Distributions 	entry = &ref->entries[entryIdx];
1283*19c3b8c2SApple OSS Distributions 
1284*19c3b8c2SApple OSS Distributions 	remain  = size;
1285*19c3b8c2SApple OSS Distributions 	int64_t iteroffset = offset;
1286*19c3b8c2SApple OSS Distributions 	uint64_t mapSize = 0;
1287*19c3b8c2SApple OSS Distributions 	while (remain) {
1288*19c3b8c2SApple OSS Distributions 		entryOffset = iteroffset - entry->offset;
1289*19c3b8c2SApple OSS Distributions 		if (entryOffset >= entry->size) {
1290*19c3b8c2SApple OSS Distributions 			panic("entryOffset");
1291*19c3b8c2SApple OSS Distributions 		}
1292*19c3b8c2SApple OSS Distributions 
1293*19c3b8c2SApple OSS Distributions #if LOGUNALIGN
1294*19c3b8c2SApple OSS Distributions 		printf("[%d] size %qx offset %qx start %qx iter %qx\n",
1295*19c3b8c2SApple OSS Distributions 		    entryIdx, entry->size, entry->offset, entry->start, iteroffset);
1296*19c3b8c2SApple OSS Distributions #endif
1297*19c3b8c2SApple OSS Distributions 
1298*19c3b8c2SApple OSS Distributions 		chunk = entry->size - entryOffset;
1299*19c3b8c2SApple OSS Distributions 		if (chunk) {
1300*19c3b8c2SApple OSS Distributions 			if (chunk > remain) {
1301*19c3b8c2SApple OSS Distributions 				chunk = remain;
1302*19c3b8c2SApple OSS Distributions 			}
1303*19c3b8c2SApple OSS Distributions 			mach_vm_size_t entrySize;
1304*19c3b8c2SApple OSS Distributions 			err = mach_memory_entry_map_size(entry->entry, map, entryOffset, chunk, &entrySize);
1305*19c3b8c2SApple OSS Distributions 			assert(KERN_SUCCESS == err);
1306*19c3b8c2SApple OSS Distributions 			mapSize += entrySize;
1307*19c3b8c2SApple OSS Distributions 
1308*19c3b8c2SApple OSS Distributions 			remain -= chunk;
1309*19c3b8c2SApple OSS Distributions 			if (!remain) {
1310*19c3b8c2SApple OSS Distributions 				break;
1311*19c3b8c2SApple OSS Distributions 			}
1312*19c3b8c2SApple OSS Distributions 			iteroffset   += chunk; // - pageOffset;
1313*19c3b8c2SApple OSS Distributions 		}
1314*19c3b8c2SApple OSS Distributions 		entry++;
1315*19c3b8c2SApple OSS Distributions 		entryIdx++;
1316*19c3b8c2SApple OSS Distributions 		if (entryIdx >= ref->count) {
1317*19c3b8c2SApple OSS Distributions 			panic("overrun");
1318*19c3b8c2SApple OSS Distributions 			err = kIOReturnOverrun;
1319*19c3b8c2SApple OSS Distributions 			break;
1320*19c3b8c2SApple OSS Distributions 		}
1321*19c3b8c2SApple OSS Distributions 	}
1322*19c3b8c2SApple OSS Distributions 
1323*19c3b8c2SApple OSS Distributions 	if (kIOMapOverwrite & options) {
1324*19c3b8c2SApple OSS Distributions 		if ((map == kernel_map) && (kIOMemoryBufferPageable & _flags)) {
1325*19c3b8c2SApple OSS Distributions 			map = IOPageableMapForAddress(addr);
1326*19c3b8c2SApple OSS Distributions 		}
1327*19c3b8c2SApple OSS Distributions 		err = KERN_SUCCESS;
1328*19c3b8c2SApple OSS Distributions 	} else {
1329*19c3b8c2SApple OSS Distributions 		IOMemoryDescriptorMapAllocRef ref;
1330*19c3b8c2SApple OSS Distributions 		ref.map     = map;
1331*19c3b8c2SApple OSS Distributions 		ref.tag     = tag;
1332*19c3b8c2SApple OSS Distributions 		ref.options = options;
1333*19c3b8c2SApple OSS Distributions 		ref.size    = mapSize;
1334*19c3b8c2SApple OSS Distributions 		ref.prot    = prot;
1335*19c3b8c2SApple OSS Distributions 		if (options & kIOMapAnywhere) {
1336*19c3b8c2SApple OSS Distributions 			// vm_map looks for addresses above here, even when VM_FLAGS_ANYWHERE
1337*19c3b8c2SApple OSS Distributions 			ref.mapped = 0;
1338*19c3b8c2SApple OSS Distributions 		} else {
1339*19c3b8c2SApple OSS Distributions 			ref.mapped = addr;
1340*19c3b8c2SApple OSS Distributions 		}
1341*19c3b8c2SApple OSS Distributions 		if ((ref.map == kernel_map) && (kIOMemoryBufferPageable & _flags)) {
1342*19c3b8c2SApple OSS Distributions 			err = IOIteratePageableMaps( ref.size, &IOMemoryDescriptorMapAlloc, &ref );
1343*19c3b8c2SApple OSS Distributions 		} else {
1344*19c3b8c2SApple OSS Distributions 			err = IOMemoryDescriptorMapAlloc(ref.map, &ref);
1345*19c3b8c2SApple OSS Distributions 		}
1346*19c3b8c2SApple OSS Distributions 
1347*19c3b8c2SApple OSS Distributions 		if (KERN_SUCCESS == err) {
1348*19c3b8c2SApple OSS Distributions 			addr     = ref.mapped;
1349*19c3b8c2SApple OSS Distributions 			map      = ref.map;
1350*19c3b8c2SApple OSS Distributions 			didAlloc = true;
1351*19c3b8c2SApple OSS Distributions 		}
1352*19c3b8c2SApple OSS Distributions #if LOGUNALIGN
1353*19c3b8c2SApple OSS Distributions 		IOLog("map err %x size %qx addr %qx\n", err, mapSize, addr);
1354*19c3b8c2SApple OSS Distributions #endif
1355*19c3b8c2SApple OSS Distributions 	}
1356*19c3b8c2SApple OSS Distributions 
1357*19c3b8c2SApple OSS Distributions 	/*
1358*19c3b8c2SApple OSS Distributions 	 * If the memory is associated with a device pager but doesn't have a UPL,
1359*19c3b8c2SApple OSS Distributions 	 * it will be immediately faulted in through the pager via populateDevicePager().
1360*19c3b8c2SApple OSS Distributions 	 * kIOMapPrefault is redundant in that case, so don't try to use it for UPL
1361*19c3b8c2SApple OSS Distributions 	 * operations.
1362*19c3b8c2SApple OSS Distributions 	 */
1363*19c3b8c2SApple OSS Distributions 	if ((reserved != NULL) && (reserved->dp.devicePager) && (_wireCount != 0)) {
1364*19c3b8c2SApple OSS Distributions 		options &= ~kIOMapPrefault;
1365*19c3b8c2SApple OSS Distributions 	}
1366*19c3b8c2SApple OSS Distributions 
1367*19c3b8c2SApple OSS Distributions 	/*
1368*19c3b8c2SApple OSS Distributions 	 * Prefaulting is only possible if we wired the memory earlier. Check the
1369*19c3b8c2SApple OSS Distributions 	 * memory type, and the underlying data.
1370*19c3b8c2SApple OSS Distributions 	 */
1371*19c3b8c2SApple OSS Distributions 	if (options & kIOMapPrefault) {
1372*19c3b8c2SApple OSS Distributions 		/*
1373*19c3b8c2SApple OSS Distributions 		 * The memory must have been wired by calling ::prepare(), otherwise
1374*19c3b8c2SApple OSS Distributions 		 * we don't have the UPL. Without UPLs, pages cannot be pre-faulted
1375*19c3b8c2SApple OSS Distributions 		 */
1376*19c3b8c2SApple OSS Distributions 		assert(_wireCount != 0);
1377*19c3b8c2SApple OSS Distributions 		assert(_memoryEntries != NULL);
1378*19c3b8c2SApple OSS Distributions 		if ((_wireCount == 0) ||
1379*19c3b8c2SApple OSS Distributions 		    (_memoryEntries == NULL)) {
1380*19c3b8c2SApple OSS Distributions 			return kIOReturnBadArgument;
1381*19c3b8c2SApple OSS Distributions 		}
1382*19c3b8c2SApple OSS Distributions 
1383*19c3b8c2SApple OSS Distributions 		// Get the page list.
1384*19c3b8c2SApple OSS Distributions 		ioGMDData* dataP = getDataP(_memoryEntries);
1385*19c3b8c2SApple OSS Distributions 		ioPLBlock const* ioplList = getIOPLList(dataP);
1386*19c3b8c2SApple OSS Distributions 		pageList = getPageList(dataP);
1387*19c3b8c2SApple OSS Distributions 
1388*19c3b8c2SApple OSS Distributions 		// Get the number of IOPLs.
1389*19c3b8c2SApple OSS Distributions 		UInt numIOPLs = getNumIOPL(_memoryEntries, dataP);
1390*19c3b8c2SApple OSS Distributions 
1391*19c3b8c2SApple OSS Distributions 		/*
1392*19c3b8c2SApple OSS Distributions 		 * Scan through the IOPL Info Blocks, looking for the first block containing
1393*19c3b8c2SApple OSS Distributions 		 * the offset. The research will go past it, so we'll need to go back to the
1394*19c3b8c2SApple OSS Distributions 		 * right range at the end.
1395*19c3b8c2SApple OSS Distributions 		 */
1396*19c3b8c2SApple OSS Distributions 		UInt ioplIndex = 0;
1397*19c3b8c2SApple OSS Distributions 		while ((ioplIndex < numIOPLs) && (((uint64_t) offset) >= ioplList[ioplIndex].fIOMDOffset)) {
1398*19c3b8c2SApple OSS Distributions 			ioplIndex++;
1399*19c3b8c2SApple OSS Distributions 		}
1400*19c3b8c2SApple OSS Distributions 		ioplIndex--;
1401*19c3b8c2SApple OSS Distributions 
1402*19c3b8c2SApple OSS Distributions 		// Retrieve the IOPL info block.
1403*19c3b8c2SApple OSS Distributions 		ioPLBlock ioplInfo = ioplList[ioplIndex];
1404*19c3b8c2SApple OSS Distributions 
1405*19c3b8c2SApple OSS Distributions 		/*
1406*19c3b8c2SApple OSS Distributions 		 * For external UPLs, the fPageInfo points directly to the UPL's page_info_t
1407*19c3b8c2SApple OSS Distributions 		 * array.
1408*19c3b8c2SApple OSS Distributions 		 */
1409*19c3b8c2SApple OSS Distributions 		if (ioplInfo.fFlags & kIOPLExternUPL) {
1410*19c3b8c2SApple OSS Distributions 			pageList = (upl_page_info_t*) ioplInfo.fPageInfo;
1411*19c3b8c2SApple OSS Distributions 		} else {
1412*19c3b8c2SApple OSS Distributions 			pageList = &pageList[ioplInfo.fPageInfo];
1413*19c3b8c2SApple OSS Distributions 		}
1414*19c3b8c2SApple OSS Distributions 
1415*19c3b8c2SApple OSS Distributions 		// Rebase [offset] into the IOPL in order to looks for the first page index.
1416*19c3b8c2SApple OSS Distributions 		mach_vm_size_t offsetInIOPL = offset - ioplInfo.fIOMDOffset + ioplInfo.fPageOffset;
1417*19c3b8c2SApple OSS Distributions 
1418*19c3b8c2SApple OSS Distributions 		// Retrieve the index of the first page corresponding to the offset.
1419*19c3b8c2SApple OSS Distributions 		currentPageIndex = atop_32(offsetInIOPL);
1420*19c3b8c2SApple OSS Distributions 	}
1421*19c3b8c2SApple OSS Distributions 
1422*19c3b8c2SApple OSS Distributions 	// enter mappings
1423*19c3b8c2SApple OSS Distributions 	remain   = size;
1424*19c3b8c2SApple OSS Distributions 	mapAddr  = addr;
1425*19c3b8c2SApple OSS Distributions 	entryIdx = firstEntryIdx;
1426*19c3b8c2SApple OSS Distributions 	entry = &ref->entries[entryIdx];
1427*19c3b8c2SApple OSS Distributions 
1428*19c3b8c2SApple OSS Distributions 	while (remain && (KERN_SUCCESS == err)) {
1429*19c3b8c2SApple OSS Distributions #if LOGUNALIGN
1430*19c3b8c2SApple OSS Distributions 		printf("offset %qx, %qx\n", offset, entry->offset);
1431*19c3b8c2SApple OSS Distributions #endif
1432*19c3b8c2SApple OSS Distributions 		if (kIODefaultCache != cacheMode) {
1433*19c3b8c2SApple OSS Distributions 			vm_size_t unused = 0;
1434*19c3b8c2SApple OSS Distributions 			err = mach_make_memory_entry(NULL /*unused*/, &unused, 0 /*unused*/,
1435*19c3b8c2SApple OSS Distributions 			    memEntryCacheMode, NULL, entry->entry);
1436*19c3b8c2SApple OSS Distributions 			assert(KERN_SUCCESS == err);
1437*19c3b8c2SApple OSS Distributions 		}
1438*19c3b8c2SApple OSS Distributions 		entryOffset = offset - entry->offset;
1439*19c3b8c2SApple OSS Distributions 		if (entryOffset >= entry->size) {
1440*19c3b8c2SApple OSS Distributions 			panic("entryOffset");
1441*19c3b8c2SApple OSS Distributions 		}
1442*19c3b8c2SApple OSS Distributions 		chunk = entry->size - entryOffset;
1443*19c3b8c2SApple OSS Distributions #if LOGUNALIGN
1444*19c3b8c2SApple OSS Distributions 		printf("entryIdx %d, chunk %qx\n", entryIdx, chunk);
1445*19c3b8c2SApple OSS Distributions #endif
1446*19c3b8c2SApple OSS Distributions 		if (chunk) {
1447*19c3b8c2SApple OSS Distributions 			vm_map_kernel_flags_t vmk_flags;
1448*19c3b8c2SApple OSS Distributions 
1449*19c3b8c2SApple OSS Distributions 			vmk_flags = VM_MAP_KERNEL_FLAGS_NONE;
1450*19c3b8c2SApple OSS Distributions 			vmk_flags.vmkf_iokit_acct = TRUE; /* iokit accounting */
1451*19c3b8c2SApple OSS Distributions 
1452*19c3b8c2SApple OSS Distributions 			if (chunk > remain) {
1453*19c3b8c2SApple OSS Distributions 				chunk = remain;
1454*19c3b8c2SApple OSS Distributions 			}
1455*19c3b8c2SApple OSS Distributions 			mapAddrOut = mapAddr;
1456*19c3b8c2SApple OSS Distributions 			if (options & kIOMapPrefault) {
1457*19c3b8c2SApple OSS Distributions 				UInt nb_pages = (typeof(nb_pages))round_page(chunk) / PAGE_SIZE;
1458*19c3b8c2SApple OSS Distributions 
1459*19c3b8c2SApple OSS Distributions 				err = vm_map_enter_mem_object_prefault(map,
1460*19c3b8c2SApple OSS Distributions 				    &mapAddrOut,
1461*19c3b8c2SApple OSS Distributions 				    chunk, 0 /* mask */,
1462*19c3b8c2SApple OSS Distributions 				    (VM_FLAGS_FIXED
1463*19c3b8c2SApple OSS Distributions 				    | VM_FLAGS_OVERWRITE
1464*19c3b8c2SApple OSS Distributions 				    | VM_FLAGS_RETURN_DATA_ADDR),
1465*19c3b8c2SApple OSS Distributions 				    vmk_flags,
1466*19c3b8c2SApple OSS Distributions 				    tag,
1467*19c3b8c2SApple OSS Distributions 				    entry->entry,
1468*19c3b8c2SApple OSS Distributions 				    entryOffset,
1469*19c3b8c2SApple OSS Distributions 				    prot,                        // cur
1470*19c3b8c2SApple OSS Distributions 				    prot,                        // max
1471*19c3b8c2SApple OSS Distributions 				    &pageList[currentPageIndex],
1472*19c3b8c2SApple OSS Distributions 				    nb_pages);
1473*19c3b8c2SApple OSS Distributions 
1474*19c3b8c2SApple OSS Distributions 				// Compute the next index in the page list.
1475*19c3b8c2SApple OSS Distributions 				currentPageIndex += nb_pages;
1476*19c3b8c2SApple OSS Distributions 				assert(currentPageIndex <= _pages);
1477*19c3b8c2SApple OSS Distributions 			} else {
1478*19c3b8c2SApple OSS Distributions #if LOGUNALIGN
1479*19c3b8c2SApple OSS Distributions 				printf("mapAddr i %qx chunk %qx\n", mapAddr, chunk);
1480*19c3b8c2SApple OSS Distributions #endif
1481*19c3b8c2SApple OSS Distributions 				err = vm_map_enter_mem_object(map,
1482*19c3b8c2SApple OSS Distributions 				    &mapAddrOut,
1483*19c3b8c2SApple OSS Distributions 				    chunk, 0 /* mask */,
1484*19c3b8c2SApple OSS Distributions 				    (VM_FLAGS_FIXED
1485*19c3b8c2SApple OSS Distributions 				    | VM_FLAGS_OVERWRITE
1486*19c3b8c2SApple OSS Distributions 				    | VM_FLAGS_RETURN_DATA_ADDR),
1487*19c3b8c2SApple OSS Distributions 				    vmk_flags,
1488*19c3b8c2SApple OSS Distributions 				    tag,
1489*19c3b8c2SApple OSS Distributions 				    entry->entry,
1490*19c3b8c2SApple OSS Distributions 				    entryOffset,
1491*19c3b8c2SApple OSS Distributions 				    false,               // copy
1492*19c3b8c2SApple OSS Distributions 				    prot,               // cur
1493*19c3b8c2SApple OSS Distributions 				    prot,               // max
1494*19c3b8c2SApple OSS Distributions 				    VM_INHERIT_NONE);
1495*19c3b8c2SApple OSS Distributions 			}
1496*19c3b8c2SApple OSS Distributions 			if (KERN_SUCCESS != err) {
1497*19c3b8c2SApple OSS Distributions 				panic("map enter err %x", err);
1498*19c3b8c2SApple OSS Distributions 				break;
1499*19c3b8c2SApple OSS Distributions 			}
1500*19c3b8c2SApple OSS Distributions #if LOGUNALIGN
1501*19c3b8c2SApple OSS Distributions 			printf("mapAddr o %qx\n", mapAddrOut);
1502*19c3b8c2SApple OSS Distributions #endif
1503*19c3b8c2SApple OSS Distributions 			if (entryIdx == firstEntryIdx) {
1504*19c3b8c2SApple OSS Distributions 				addr = mapAddrOut;
1505*19c3b8c2SApple OSS Distributions 			}
1506*19c3b8c2SApple OSS Distributions 			remain -= chunk;
1507*19c3b8c2SApple OSS Distributions 			if (!remain) {
1508*19c3b8c2SApple OSS Distributions 				break;
1509*19c3b8c2SApple OSS Distributions 			}
1510*19c3b8c2SApple OSS Distributions 			mach_vm_size_t entrySize;
1511*19c3b8c2SApple OSS Distributions 			err = mach_memory_entry_map_size(entry->entry, map, entryOffset, chunk, &entrySize);
1512*19c3b8c2SApple OSS Distributions 			assert(KERN_SUCCESS == err);
1513*19c3b8c2SApple OSS Distributions 			mapAddr += entrySize;
1514*19c3b8c2SApple OSS Distributions 			offset  += chunk;
1515*19c3b8c2SApple OSS Distributions 		}
1516*19c3b8c2SApple OSS Distributions 
1517*19c3b8c2SApple OSS Distributions 		entry++;
1518*19c3b8c2SApple OSS Distributions 		entryIdx++;
1519*19c3b8c2SApple OSS Distributions 		if (entryIdx >= ref->count) {
1520*19c3b8c2SApple OSS Distributions 			err = kIOReturnOverrun;
1521*19c3b8c2SApple OSS Distributions 			break;
1522*19c3b8c2SApple OSS Distributions 		}
1523*19c3b8c2SApple OSS Distributions 	}
1524*19c3b8c2SApple OSS Distributions 
1525*19c3b8c2SApple OSS Distributions 	if (KERN_SUCCESS != err) {
1526*19c3b8c2SApple OSS Distributions 		DEBUG4K_ERROR("size 0x%llx err 0x%x\n", size, err);
1527*19c3b8c2SApple OSS Distributions 	}
1528*19c3b8c2SApple OSS Distributions 
1529*19c3b8c2SApple OSS Distributions 	if ((KERN_SUCCESS != err) && didAlloc) {
1530*19c3b8c2SApple OSS Distributions 		(void) IOMemoryDescriptorMapDealloc(options, map, trunc_page_64(addr), size);
1531*19c3b8c2SApple OSS Distributions 		addr = 0;
1532*19c3b8c2SApple OSS Distributions 	}
1533*19c3b8c2SApple OSS Distributions 	*inaddr = addr;
1534*19c3b8c2SApple OSS Distributions 
1535*19c3b8c2SApple OSS Distributions 	return err;
1536*19c3b8c2SApple OSS Distributions }
1537*19c3b8c2SApple OSS Distributions 
1538*19c3b8c2SApple OSS Distributions uint64_t
memoryReferenceGetDMAMapLength(IOMemoryReference * ref,uint64_t * offset)1539*19c3b8c2SApple OSS Distributions IOGeneralMemoryDescriptor::memoryReferenceGetDMAMapLength(
1540*19c3b8c2SApple OSS Distributions 	IOMemoryReference * ref,
1541*19c3b8c2SApple OSS Distributions 	uint64_t          * offset)
1542*19c3b8c2SApple OSS Distributions {
1543*19c3b8c2SApple OSS Distributions 	kern_return_t kr;
1544*19c3b8c2SApple OSS Distributions 	vm_object_offset_t data_offset = 0;
1545*19c3b8c2SApple OSS Distributions 	uint64_t total;
1546*19c3b8c2SApple OSS Distributions 	uint32_t idx;
1547*19c3b8c2SApple OSS Distributions 
1548*19c3b8c2SApple OSS Distributions 	assert(ref->count);
1549*19c3b8c2SApple OSS Distributions 	if (offset) {
1550*19c3b8c2SApple OSS Distributions 		*offset = (uint64_t) data_offset;
1551*19c3b8c2SApple OSS Distributions 	}
1552*19c3b8c2SApple OSS Distributions 	total = 0;
1553*19c3b8c2SApple OSS Distributions 	for (idx = 0; idx < ref->count; idx++) {
1554*19c3b8c2SApple OSS Distributions 		kr = mach_memory_entry_phys_page_offset(ref->entries[idx].entry,
1555*19c3b8c2SApple OSS Distributions 		    &data_offset);
1556*19c3b8c2SApple OSS Distributions 		if (KERN_SUCCESS != kr) {
1557*19c3b8c2SApple OSS Distributions 			DEBUG4K_ERROR("ref %p entry %p kr 0x%x\n", ref, ref->entries[idx].entry, kr);
1558*19c3b8c2SApple OSS Distributions 		} else if (0 != data_offset) {
1559*19c3b8c2SApple OSS Distributions 			DEBUG4K_IOKIT("ref %p entry %p offset 0x%llx kr 0x%x\n", ref, ref->entries[0].entry, data_offset, kr);
1560*19c3b8c2SApple OSS Distributions 		}
1561*19c3b8c2SApple OSS Distributions 		if (offset && !idx) {
1562*19c3b8c2SApple OSS Distributions 			*offset = (uint64_t) data_offset;
1563*19c3b8c2SApple OSS Distributions 		}
1564*19c3b8c2SApple OSS Distributions 		total += round_page(data_offset + ref->entries[idx].size);
1565*19c3b8c2SApple OSS Distributions 	}
1566*19c3b8c2SApple OSS Distributions 
1567*19c3b8c2SApple OSS Distributions 	DEBUG4K_IOKIT("ref %p offset 0x%llx total 0x%llx\n", ref,
1568*19c3b8c2SApple OSS Distributions 	    (offset ? *offset : (vm_object_offset_t)-1), total);
1569*19c3b8c2SApple OSS Distributions 
1570*19c3b8c2SApple OSS Distributions 	return total;
1571*19c3b8c2SApple OSS Distributions }
1572*19c3b8c2SApple OSS Distributions 
1573*19c3b8c2SApple OSS Distributions 
1574*19c3b8c2SApple OSS Distributions IOReturn
memoryReferenceGetPageCounts(IOMemoryReference * ref,IOByteCount * residentPageCount,IOByteCount * dirtyPageCount)1575*19c3b8c2SApple OSS Distributions IOGeneralMemoryDescriptor::memoryReferenceGetPageCounts(
1576*19c3b8c2SApple OSS Distributions 	IOMemoryReference * ref,
1577*19c3b8c2SApple OSS Distributions 	IOByteCount       * residentPageCount,
1578*19c3b8c2SApple OSS Distributions 	IOByteCount       * dirtyPageCount)
1579*19c3b8c2SApple OSS Distributions {
1580*19c3b8c2SApple OSS Distributions 	IOReturn        err;
1581*19c3b8c2SApple OSS Distributions 	IOMemoryEntry * entries;
1582*19c3b8c2SApple OSS Distributions 	unsigned int resident, dirty;
1583*19c3b8c2SApple OSS Distributions 	unsigned int totalResident, totalDirty;
1584*19c3b8c2SApple OSS Distributions 
1585*19c3b8c2SApple OSS Distributions 	totalResident = totalDirty = 0;
1586*19c3b8c2SApple OSS Distributions 	err = kIOReturnSuccess;
1587*19c3b8c2SApple OSS Distributions 	entries = ref->entries + ref->count;
1588*19c3b8c2SApple OSS Distributions 	while (entries > &ref->entries[0]) {
1589*19c3b8c2SApple OSS Distributions 		entries--;
1590*19c3b8c2SApple OSS Distributions 		err = mach_memory_entry_get_page_counts(entries->entry, &resident, &dirty);
1591*19c3b8c2SApple OSS Distributions 		if (KERN_SUCCESS != err) {
1592*19c3b8c2SApple OSS Distributions 			break;
1593*19c3b8c2SApple OSS Distributions 		}
1594*19c3b8c2SApple OSS Distributions 		totalResident += resident;
1595*19c3b8c2SApple OSS Distributions 		totalDirty    += dirty;
1596*19c3b8c2SApple OSS Distributions 	}
1597*19c3b8c2SApple OSS Distributions 
1598*19c3b8c2SApple OSS Distributions 	if (residentPageCount) {
1599*19c3b8c2SApple OSS Distributions 		*residentPageCount = totalResident;
1600*19c3b8c2SApple OSS Distributions 	}
1601*19c3b8c2SApple OSS Distributions 	if (dirtyPageCount) {
1602*19c3b8c2SApple OSS Distributions 		*dirtyPageCount    = totalDirty;
1603*19c3b8c2SApple OSS Distributions 	}
1604*19c3b8c2SApple OSS Distributions 	return err;
1605*19c3b8c2SApple OSS Distributions }
1606*19c3b8c2SApple OSS Distributions 
1607*19c3b8c2SApple OSS Distributions IOReturn
memoryReferenceSetPurgeable(IOMemoryReference * ref,IOOptionBits newState,IOOptionBits * oldState)1608*19c3b8c2SApple OSS Distributions IOGeneralMemoryDescriptor::memoryReferenceSetPurgeable(
1609*19c3b8c2SApple OSS Distributions 	IOMemoryReference * ref,
1610*19c3b8c2SApple OSS Distributions 	IOOptionBits        newState,
1611*19c3b8c2SApple OSS Distributions 	IOOptionBits      * oldState)
1612*19c3b8c2SApple OSS Distributions {
1613*19c3b8c2SApple OSS Distributions 	IOReturn        err;
1614*19c3b8c2SApple OSS Distributions 	IOMemoryEntry * entries;
1615*19c3b8c2SApple OSS Distributions 	vm_purgable_t   control;
1616*19c3b8c2SApple OSS Distributions 	int             totalState, state;
1617*19c3b8c2SApple OSS Distributions 
1618*19c3b8c2SApple OSS Distributions 	totalState = kIOMemoryPurgeableNonVolatile;
1619*19c3b8c2SApple OSS Distributions 	err = kIOReturnSuccess;
1620*19c3b8c2SApple OSS Distributions 	entries = ref->entries + ref->count;
1621*19c3b8c2SApple OSS Distributions 	while (entries > &ref->entries[0]) {
1622*19c3b8c2SApple OSS Distributions 		entries--;
1623*19c3b8c2SApple OSS Distributions 
1624*19c3b8c2SApple OSS Distributions 		err = purgeableControlBits(newState, &control, &state);
1625*19c3b8c2SApple OSS Distributions 		if (KERN_SUCCESS != err) {
1626*19c3b8c2SApple OSS Distributions 			break;
1627*19c3b8c2SApple OSS Distributions 		}
1628*19c3b8c2SApple OSS Distributions 		err = memory_entry_purgeable_control_internal(entries->entry, control, &state);
1629*19c3b8c2SApple OSS Distributions 		if (KERN_SUCCESS != err) {
1630*19c3b8c2SApple OSS Distributions 			break;
1631*19c3b8c2SApple OSS Distributions 		}
1632*19c3b8c2SApple OSS Distributions 		err = purgeableStateBits(&state);
1633*19c3b8c2SApple OSS Distributions 		if (KERN_SUCCESS != err) {
1634*19c3b8c2SApple OSS Distributions 			break;
1635*19c3b8c2SApple OSS Distributions 		}
1636*19c3b8c2SApple OSS Distributions 
1637*19c3b8c2SApple OSS Distributions 		if (kIOMemoryPurgeableEmpty == state) {
1638*19c3b8c2SApple OSS Distributions 			totalState = kIOMemoryPurgeableEmpty;
1639*19c3b8c2SApple OSS Distributions 		} else if (kIOMemoryPurgeableEmpty == totalState) {
1640*19c3b8c2SApple OSS Distributions 			continue;
1641*19c3b8c2SApple OSS Distributions 		} else if (kIOMemoryPurgeableVolatile == totalState) {
1642*19c3b8c2SApple OSS Distributions 			continue;
1643*19c3b8c2SApple OSS Distributions 		} else if (kIOMemoryPurgeableVolatile == state) {
1644*19c3b8c2SApple OSS Distributions 			totalState = kIOMemoryPurgeableVolatile;
1645*19c3b8c2SApple OSS Distributions 		} else {
1646*19c3b8c2SApple OSS Distributions 			totalState = kIOMemoryPurgeableNonVolatile;
1647*19c3b8c2SApple OSS Distributions 		}
1648*19c3b8c2SApple OSS Distributions 	}
1649*19c3b8c2SApple OSS Distributions 
1650*19c3b8c2SApple OSS Distributions 	if (oldState) {
1651*19c3b8c2SApple OSS Distributions 		*oldState = totalState;
1652*19c3b8c2SApple OSS Distributions 	}
1653*19c3b8c2SApple OSS Distributions 	return err;
1654*19c3b8c2SApple OSS Distributions }
1655*19c3b8c2SApple OSS Distributions 
1656*19c3b8c2SApple OSS Distributions IOReturn
memoryReferenceSetOwnership(IOMemoryReference * ref,task_t newOwner,int newLedgerTag,IOOptionBits newLedgerOptions)1657*19c3b8c2SApple OSS Distributions IOGeneralMemoryDescriptor::memoryReferenceSetOwnership(
1658*19c3b8c2SApple OSS Distributions 	IOMemoryReference * ref,
1659*19c3b8c2SApple OSS Distributions 	task_t              newOwner,
1660*19c3b8c2SApple OSS Distributions 	int                 newLedgerTag,
1661*19c3b8c2SApple OSS Distributions 	IOOptionBits        newLedgerOptions)
1662*19c3b8c2SApple OSS Distributions {
1663*19c3b8c2SApple OSS Distributions 	IOReturn        err, totalErr;
1664*19c3b8c2SApple OSS Distributions 	IOMemoryEntry * entries;
1665*19c3b8c2SApple OSS Distributions 
1666*19c3b8c2SApple OSS Distributions 	totalErr = kIOReturnSuccess;
1667*19c3b8c2SApple OSS Distributions 	entries = ref->entries + ref->count;
1668*19c3b8c2SApple OSS Distributions 	while (entries > &ref->entries[0]) {
1669*19c3b8c2SApple OSS Distributions 		entries--;
1670*19c3b8c2SApple OSS Distributions 
1671*19c3b8c2SApple OSS Distributions 		err = mach_memory_entry_ownership(entries->entry, newOwner, newLedgerTag, newLedgerOptions);
1672*19c3b8c2SApple OSS Distributions 		if (KERN_SUCCESS != err) {
1673*19c3b8c2SApple OSS Distributions 			totalErr = err;
1674*19c3b8c2SApple OSS Distributions 		}
1675*19c3b8c2SApple OSS Distributions 	}
1676*19c3b8c2SApple OSS Distributions 
1677*19c3b8c2SApple OSS Distributions 	return totalErr;
1678*19c3b8c2SApple OSS Distributions }
1679*19c3b8c2SApple OSS Distributions 
1680*19c3b8c2SApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1681*19c3b8c2SApple OSS Distributions 
1682*19c3b8c2SApple OSS Distributions OSSharedPtr<IOMemoryDescriptor>
withAddress(void * address,IOByteCount length,IODirection direction)1683*19c3b8c2SApple OSS Distributions IOMemoryDescriptor::withAddress(void *      address,
1684*19c3b8c2SApple OSS Distributions     IOByteCount   length,
1685*19c3b8c2SApple OSS Distributions     IODirection direction)
1686*19c3b8c2SApple OSS Distributions {
1687*19c3b8c2SApple OSS Distributions 	return IOMemoryDescriptor::
1688*19c3b8c2SApple OSS Distributions 	       withAddressRange((IOVirtualAddress) address, length, direction | kIOMemoryAutoPrepare, kernel_task);
1689*19c3b8c2SApple OSS Distributions }
1690*19c3b8c2SApple OSS Distributions 
1691*19c3b8c2SApple OSS Distributions #ifndef __LP64__
1692*19c3b8c2SApple OSS Distributions OSSharedPtr<IOMemoryDescriptor>
withAddress(IOVirtualAddress address,IOByteCount length,IODirection direction,task_t task)1693*19c3b8c2SApple OSS Distributions IOMemoryDescriptor::withAddress(IOVirtualAddress address,
1694*19c3b8c2SApple OSS Distributions     IOByteCount  length,
1695*19c3b8c2SApple OSS Distributions     IODirection  direction,
1696*19c3b8c2SApple OSS Distributions     task_t       task)
1697*19c3b8c2SApple OSS Distributions {
1698*19c3b8c2SApple OSS Distributions 	OSSharedPtr<IOGeneralMemoryDescriptor> that = OSMakeShared<IOGeneralMemoryDescriptor>();
1699*19c3b8c2SApple OSS Distributions 	if (that) {
1700*19c3b8c2SApple OSS Distributions 		if (that->initWithAddress(address, length, direction, task)) {
1701*19c3b8c2SApple OSS Distributions 			return os::move(that);
1702*19c3b8c2SApple OSS Distributions 		}
1703*19c3b8c2SApple OSS Distributions 	}
1704*19c3b8c2SApple OSS Distributions 	return nullptr;
1705*19c3b8c2SApple OSS Distributions }
1706*19c3b8c2SApple OSS Distributions #endif /* !__LP64__ */
1707*19c3b8c2SApple OSS Distributions 
1708*19c3b8c2SApple OSS Distributions OSSharedPtr<IOMemoryDescriptor>
withPhysicalAddress(IOPhysicalAddress address,IOByteCount length,IODirection direction)1709*19c3b8c2SApple OSS Distributions IOMemoryDescriptor::withPhysicalAddress(
1710*19c3b8c2SApple OSS Distributions 	IOPhysicalAddress       address,
1711*19c3b8c2SApple OSS Distributions 	IOByteCount             length,
1712*19c3b8c2SApple OSS Distributions 	IODirection             direction )
1713*19c3b8c2SApple OSS Distributions {
1714*19c3b8c2SApple OSS Distributions 	return IOMemoryDescriptor::withAddressRange(address, length, direction, TASK_NULL);
1715*19c3b8c2SApple OSS Distributions }
1716*19c3b8c2SApple OSS Distributions 
1717*19c3b8c2SApple OSS Distributions #ifndef __LP64__
1718*19c3b8c2SApple OSS Distributions OSSharedPtr<IOMemoryDescriptor>
withRanges(IOVirtualRange * ranges,UInt32 withCount,IODirection direction,task_t task,bool asReference)1719*19c3b8c2SApple OSS Distributions IOMemoryDescriptor::withRanges( IOVirtualRange * ranges,
1720*19c3b8c2SApple OSS Distributions     UInt32           withCount,
1721*19c3b8c2SApple OSS Distributions     IODirection      direction,
1722*19c3b8c2SApple OSS Distributions     task_t           task,
1723*19c3b8c2SApple OSS Distributions     bool             asReference)
1724*19c3b8c2SApple OSS Distributions {
1725*19c3b8c2SApple OSS Distributions 	OSSharedPtr<IOGeneralMemoryDescriptor> that = OSMakeShared<IOGeneralMemoryDescriptor>();
1726*19c3b8c2SApple OSS Distributions 	if (that) {
1727*19c3b8c2SApple OSS Distributions 		if (that->initWithRanges(ranges, withCount, direction, task, asReference)) {
1728*19c3b8c2SApple OSS Distributions 			return os::move(that);
1729*19c3b8c2SApple OSS Distributions 		}
1730*19c3b8c2SApple OSS Distributions 	}
1731*19c3b8c2SApple OSS Distributions 	return nullptr;
1732*19c3b8c2SApple OSS Distributions }
1733*19c3b8c2SApple OSS Distributions #endif /* !__LP64__ */
1734*19c3b8c2SApple OSS Distributions 
1735*19c3b8c2SApple OSS Distributions OSSharedPtr<IOMemoryDescriptor>
withAddressRange(mach_vm_address_t address,mach_vm_size_t length,IOOptionBits options,task_t task)1736*19c3b8c2SApple OSS Distributions IOMemoryDescriptor::withAddressRange(mach_vm_address_t address,
1737*19c3b8c2SApple OSS Distributions     mach_vm_size_t length,
1738*19c3b8c2SApple OSS Distributions     IOOptionBits   options,
1739*19c3b8c2SApple OSS Distributions     task_t         task)
1740*19c3b8c2SApple OSS Distributions {
1741*19c3b8c2SApple OSS Distributions 	IOAddressRange range = { address, length };
1742*19c3b8c2SApple OSS Distributions 	return IOMemoryDescriptor::withAddressRanges(&range, 1, options, task);
1743*19c3b8c2SApple OSS Distributions }
1744*19c3b8c2SApple OSS Distributions 
1745*19c3b8c2SApple OSS Distributions OSSharedPtr<IOMemoryDescriptor>
withAddressRanges(IOAddressRange * ranges,UInt32 rangeCount,IOOptionBits options,task_t task)1746*19c3b8c2SApple OSS Distributions IOMemoryDescriptor::withAddressRanges(IOAddressRange *   ranges,
1747*19c3b8c2SApple OSS Distributions     UInt32           rangeCount,
1748*19c3b8c2SApple OSS Distributions     IOOptionBits     options,
1749*19c3b8c2SApple OSS Distributions     task_t           task)
1750*19c3b8c2SApple OSS Distributions {
1751*19c3b8c2SApple OSS Distributions 	OSSharedPtr<IOGeneralMemoryDescriptor> that = OSMakeShared<IOGeneralMemoryDescriptor>();
1752*19c3b8c2SApple OSS Distributions 	if (that) {
1753*19c3b8c2SApple OSS Distributions 		if (task) {
1754*19c3b8c2SApple OSS Distributions 			options |= kIOMemoryTypeVirtual64;
1755*19c3b8c2SApple OSS Distributions 		} else {
1756*19c3b8c2SApple OSS Distributions 			options |= kIOMemoryTypePhysical64;
1757*19c3b8c2SApple OSS Distributions 		}
1758*19c3b8c2SApple OSS Distributions 
1759*19c3b8c2SApple OSS Distributions 		if (that->initWithOptions(ranges, rangeCount, 0, task, options, /* mapper */ NULL)) {
1760*19c3b8c2SApple OSS Distributions 			return os::move(that);
1761*19c3b8c2SApple OSS Distributions 		}
1762*19c3b8c2SApple OSS Distributions 	}
1763*19c3b8c2SApple OSS Distributions 
1764*19c3b8c2SApple OSS Distributions 	return nullptr;
1765*19c3b8c2SApple OSS Distributions }
1766*19c3b8c2SApple OSS Distributions 
1767*19c3b8c2SApple OSS Distributions 
1768*19c3b8c2SApple OSS Distributions /*
1769*19c3b8c2SApple OSS Distributions  * withOptions:
1770*19c3b8c2SApple OSS Distributions  *
1771*19c3b8c2SApple OSS Distributions  * Create a new IOMemoryDescriptor. The buffer is made up of several
1772*19c3b8c2SApple OSS Distributions  * virtual address ranges, from a given task.
1773*19c3b8c2SApple OSS Distributions  *
1774*19c3b8c2SApple OSS Distributions  * Passing the ranges as a reference will avoid an extra allocation.
1775*19c3b8c2SApple OSS Distributions  */
1776*19c3b8c2SApple OSS Distributions OSSharedPtr<IOMemoryDescriptor>
withOptions(void * buffers,UInt32 count,UInt32 offset,task_t task,IOOptionBits opts,IOMapper * mapper)1777*19c3b8c2SApple OSS Distributions IOMemoryDescriptor::withOptions(void *          buffers,
1778*19c3b8c2SApple OSS Distributions     UInt32          count,
1779*19c3b8c2SApple OSS Distributions     UInt32          offset,
1780*19c3b8c2SApple OSS Distributions     task_t          task,
1781*19c3b8c2SApple OSS Distributions     IOOptionBits    opts,
1782*19c3b8c2SApple OSS Distributions     IOMapper *      mapper)
1783*19c3b8c2SApple OSS Distributions {
1784*19c3b8c2SApple OSS Distributions 	OSSharedPtr<IOGeneralMemoryDescriptor> self = OSMakeShared<IOGeneralMemoryDescriptor>();
1785*19c3b8c2SApple OSS Distributions 
1786*19c3b8c2SApple OSS Distributions 	if (self
1787*19c3b8c2SApple OSS Distributions 	    && !self->initWithOptions(buffers, count, offset, task, opts, mapper)) {
1788*19c3b8c2SApple OSS Distributions 		return nullptr;
1789*19c3b8c2SApple OSS Distributions 	}
1790*19c3b8c2SApple OSS Distributions 
1791*19c3b8c2SApple OSS Distributions 	return os::move(self);
1792*19c3b8c2SApple OSS Distributions }
1793*19c3b8c2SApple OSS Distributions 
1794*19c3b8c2SApple OSS Distributions bool
initWithOptions(void * buffers,UInt32 count,UInt32 offset,task_t task,IOOptionBits options,IOMapper * mapper)1795*19c3b8c2SApple OSS Distributions IOMemoryDescriptor::initWithOptions(void *         buffers,
1796*19c3b8c2SApple OSS Distributions     UInt32         count,
1797*19c3b8c2SApple OSS Distributions     UInt32         offset,
1798*19c3b8c2SApple OSS Distributions     task_t         task,
1799*19c3b8c2SApple OSS Distributions     IOOptionBits   options,
1800*19c3b8c2SApple OSS Distributions     IOMapper *     mapper)
1801*19c3b8c2SApple OSS Distributions {
1802*19c3b8c2SApple OSS Distributions 	return false;
1803*19c3b8c2SApple OSS Distributions }
1804*19c3b8c2SApple OSS Distributions 
1805*19c3b8c2SApple OSS Distributions #ifndef __LP64__
1806*19c3b8c2SApple OSS Distributions OSSharedPtr<IOMemoryDescriptor>
withPhysicalRanges(IOPhysicalRange * ranges,UInt32 withCount,IODirection direction,bool asReference)1807*19c3b8c2SApple OSS Distributions IOMemoryDescriptor::withPhysicalRanges( IOPhysicalRange * ranges,
1808*19c3b8c2SApple OSS Distributions     UInt32          withCount,
1809*19c3b8c2SApple OSS Distributions     IODirection     direction,
1810*19c3b8c2SApple OSS Distributions     bool            asReference)
1811*19c3b8c2SApple OSS Distributions {
1812*19c3b8c2SApple OSS Distributions 	OSSharedPtr<IOGeneralMemoryDescriptor> that = OSMakeShared<IOGeneralMemoryDescriptor>();
1813*19c3b8c2SApple OSS Distributions 	if (that) {
1814*19c3b8c2SApple OSS Distributions 		if (that->initWithPhysicalRanges(ranges, withCount, direction, asReference)) {
1815*19c3b8c2SApple OSS Distributions 			return os::move(that);
1816*19c3b8c2SApple OSS Distributions 		}
1817*19c3b8c2SApple OSS Distributions 	}
1818*19c3b8c2SApple OSS Distributions 	return nullptr;
1819*19c3b8c2SApple OSS Distributions }
1820*19c3b8c2SApple OSS Distributions 
1821*19c3b8c2SApple OSS Distributions OSSharedPtr<IOMemoryDescriptor>
withSubRange(IOMemoryDescriptor * of,IOByteCount offset,IOByteCount length,IODirection direction)1822*19c3b8c2SApple OSS Distributions IOMemoryDescriptor::withSubRange(IOMemoryDescriptor *   of,
1823*19c3b8c2SApple OSS Distributions     IOByteCount             offset,
1824*19c3b8c2SApple OSS Distributions     IOByteCount             length,
1825*19c3b8c2SApple OSS Distributions     IODirection             direction)
1826*19c3b8c2SApple OSS Distributions {
1827*19c3b8c2SApple OSS Distributions 	return IOSubMemoryDescriptor::withSubRange(of, offset, length, direction);
1828*19c3b8c2SApple OSS Distributions }
1829*19c3b8c2SApple OSS Distributions #endif /* !__LP64__ */
1830*19c3b8c2SApple OSS Distributions 
1831*19c3b8c2SApple OSS Distributions OSSharedPtr<IOMemoryDescriptor>
withPersistentMemoryDescriptor(IOMemoryDescriptor * originalMD)1832*19c3b8c2SApple OSS Distributions IOMemoryDescriptor::withPersistentMemoryDescriptor(IOMemoryDescriptor *originalMD)
1833*19c3b8c2SApple OSS Distributions {
1834*19c3b8c2SApple OSS Distributions 	IOGeneralMemoryDescriptor *origGenMD =
1835*19c3b8c2SApple OSS Distributions 	    OSDynamicCast(IOGeneralMemoryDescriptor, originalMD);
1836*19c3b8c2SApple OSS Distributions 
1837*19c3b8c2SApple OSS Distributions 	if (origGenMD) {
1838*19c3b8c2SApple OSS Distributions 		return IOGeneralMemoryDescriptor::
1839*19c3b8c2SApple OSS Distributions 		       withPersistentMemoryDescriptor(origGenMD);
1840*19c3b8c2SApple OSS Distributions 	} else {
1841*19c3b8c2SApple OSS Distributions 		return nullptr;
1842*19c3b8c2SApple OSS Distributions 	}
1843*19c3b8c2SApple OSS Distributions }
1844*19c3b8c2SApple OSS Distributions 
1845*19c3b8c2SApple OSS Distributions OSSharedPtr<IOMemoryDescriptor>
withPersistentMemoryDescriptor(IOGeneralMemoryDescriptor * originalMD)1846*19c3b8c2SApple OSS Distributions IOGeneralMemoryDescriptor::withPersistentMemoryDescriptor(IOGeneralMemoryDescriptor *originalMD)
1847*19c3b8c2SApple OSS Distributions {
1848*19c3b8c2SApple OSS Distributions 	IOMemoryReference * memRef;
1849*19c3b8c2SApple OSS Distributions 	OSSharedPtr<IOGeneralMemoryDescriptor> self;
1850*19c3b8c2SApple OSS Distributions 
1851*19c3b8c2SApple OSS Distributions 	if (kIOReturnSuccess != originalMD->memoryReferenceCreate(kIOMemoryReferenceReuse, &memRef)) {
1852*19c3b8c2SApple OSS Distributions 		return nullptr;
1853*19c3b8c2SApple OSS Distributions 	}
1854*19c3b8c2SApple OSS Distributions 
1855*19c3b8c2SApple OSS Distributions 	if (memRef == originalMD->_memRef) {
1856*19c3b8c2SApple OSS Distributions 		self.reset(originalMD, OSRetain);
1857*19c3b8c2SApple OSS Distributions 		originalMD->memoryReferenceRelease(memRef);
1858*19c3b8c2SApple OSS Distributions 		return os::move(self);
1859*19c3b8c2SApple OSS Distributions 	}
1860*19c3b8c2SApple OSS Distributions 
1861*19c3b8c2SApple OSS Distributions 	self = OSMakeShared<IOGeneralMemoryDescriptor>();
1862*19c3b8c2SApple OSS Distributions 	IOMDPersistentInitData initData = { originalMD, memRef };
1863*19c3b8c2SApple OSS Distributions 
1864*19c3b8c2SApple OSS Distributions 	if (self
1865*19c3b8c2SApple OSS Distributions 	    && !self->initWithOptions(&initData, 1, 0, NULL, kIOMemoryTypePersistentMD, NULL)) {
1866*19c3b8c2SApple OSS Distributions 		return nullptr;
1867*19c3b8c2SApple OSS Distributions 	}
1868*19c3b8c2SApple OSS Distributions 	return os::move(self);
1869*19c3b8c2SApple OSS Distributions }
1870*19c3b8c2SApple OSS Distributions 
1871*19c3b8c2SApple OSS Distributions #ifndef __LP64__
1872*19c3b8c2SApple OSS Distributions bool
initWithAddress(void * address,IOByteCount withLength,IODirection withDirection)1873*19c3b8c2SApple OSS Distributions IOGeneralMemoryDescriptor::initWithAddress(void *      address,
1874*19c3b8c2SApple OSS Distributions     IOByteCount   withLength,
1875*19c3b8c2SApple OSS Distributions     IODirection withDirection)
1876*19c3b8c2SApple OSS Distributions {
1877*19c3b8c2SApple OSS Distributions 	_singleRange.v.address = (vm_offset_t) address;
1878*19c3b8c2SApple OSS Distributions 	_singleRange.v.length  = withLength;
1879*19c3b8c2SApple OSS Distributions 
1880*19c3b8c2SApple OSS Distributions 	return initWithRanges(&_singleRange.v, 1, withDirection, kernel_task, true);
1881*19c3b8c2SApple OSS Distributions }
1882*19c3b8c2SApple OSS Distributions 
1883*19c3b8c2SApple OSS Distributions bool
initWithAddress(IOVirtualAddress address,IOByteCount withLength,IODirection withDirection,task_t withTask)1884*19c3b8c2SApple OSS Distributions IOGeneralMemoryDescriptor::initWithAddress(IOVirtualAddress address,
1885*19c3b8c2SApple OSS Distributions     IOByteCount    withLength,
1886*19c3b8c2SApple OSS Distributions     IODirection  withDirection,
1887*19c3b8c2SApple OSS Distributions     task_t       withTask)
1888*19c3b8c2SApple OSS Distributions {
1889*19c3b8c2SApple OSS Distributions 	_singleRange.v.address = address;
1890*19c3b8c2SApple OSS Distributions 	_singleRange.v.length  = withLength;
1891*19c3b8c2SApple OSS Distributions 
1892*19c3b8c2SApple OSS Distributions 	return initWithRanges(&_singleRange.v, 1, withDirection, withTask, true);
1893*19c3b8c2SApple OSS Distributions }
1894*19c3b8c2SApple OSS Distributions 
1895*19c3b8c2SApple OSS Distributions bool
initWithPhysicalAddress(IOPhysicalAddress address,IOByteCount withLength,IODirection withDirection)1896*19c3b8c2SApple OSS Distributions IOGeneralMemoryDescriptor::initWithPhysicalAddress(
1897*19c3b8c2SApple OSS Distributions 	IOPhysicalAddress      address,
1898*19c3b8c2SApple OSS Distributions 	IOByteCount            withLength,
1899*19c3b8c2SApple OSS Distributions 	IODirection            withDirection )
1900*19c3b8c2SApple OSS Distributions {
1901*19c3b8c2SApple OSS Distributions 	_singleRange.p.address = address;
1902*19c3b8c2SApple OSS Distributions 	_singleRange.p.length  = withLength;
1903*19c3b8c2SApple OSS Distributions 
1904*19c3b8c2SApple OSS Distributions 	return initWithPhysicalRanges( &_singleRange.p, 1, withDirection, true);
1905*19c3b8c2SApple OSS Distributions }
1906*19c3b8c2SApple OSS Distributions 
1907*19c3b8c2SApple OSS Distributions bool
initWithPhysicalRanges(IOPhysicalRange * ranges,UInt32 count,IODirection direction,bool reference)1908*19c3b8c2SApple OSS Distributions IOGeneralMemoryDescriptor::initWithPhysicalRanges(
1909*19c3b8c2SApple OSS Distributions 	IOPhysicalRange * ranges,
1910*19c3b8c2SApple OSS Distributions 	UInt32            count,
1911*19c3b8c2SApple OSS Distributions 	IODirection       direction,
1912*19c3b8c2SApple OSS Distributions 	bool              reference)
1913*19c3b8c2SApple OSS Distributions {
1914*19c3b8c2SApple OSS Distributions 	IOOptionBits mdOpts = direction | kIOMemoryTypePhysical;
1915*19c3b8c2SApple OSS Distributions 
1916*19c3b8c2SApple OSS Distributions 	if (reference) {
1917*19c3b8c2SApple OSS Distributions 		mdOpts |= kIOMemoryAsReference;
1918*19c3b8c2SApple OSS Distributions 	}
1919*19c3b8c2SApple OSS Distributions 
1920*19c3b8c2SApple OSS Distributions 	return initWithOptions(ranges, count, 0, NULL, mdOpts, /* mapper */ NULL);
1921*19c3b8c2SApple OSS Distributions }
1922*19c3b8c2SApple OSS Distributions 
1923*19c3b8c2SApple OSS Distributions bool
initWithRanges(IOVirtualRange * ranges,UInt32 count,IODirection direction,task_t task,bool reference)1924*19c3b8c2SApple OSS Distributions IOGeneralMemoryDescriptor::initWithRanges(
1925*19c3b8c2SApple OSS Distributions 	IOVirtualRange * ranges,
1926*19c3b8c2SApple OSS Distributions 	UInt32           count,
1927*19c3b8c2SApple OSS Distributions 	IODirection      direction,
1928*19c3b8c2SApple OSS Distributions 	task_t           task,
1929*19c3b8c2SApple OSS Distributions 	bool             reference)
1930*19c3b8c2SApple OSS Distributions {
1931*19c3b8c2SApple OSS Distributions 	IOOptionBits mdOpts = direction;
1932*19c3b8c2SApple OSS Distributions 
1933*19c3b8c2SApple OSS Distributions 	if (reference) {
1934*19c3b8c2SApple OSS Distributions 		mdOpts |= kIOMemoryAsReference;
1935*19c3b8c2SApple OSS Distributions 	}
1936*19c3b8c2SApple OSS Distributions 
1937*19c3b8c2SApple OSS Distributions 	if (task) {
1938*19c3b8c2SApple OSS Distributions 		mdOpts |= kIOMemoryTypeVirtual;
1939*19c3b8c2SApple OSS Distributions 
1940*19c3b8c2SApple OSS Distributions 		// Auto-prepare if this is a kernel memory descriptor as very few
1941*19c3b8c2SApple OSS Distributions 		// clients bother to prepare() kernel memory.
1942*19c3b8c2SApple OSS Distributions 		// But it was not enforced so what are you going to do?
1943*19c3b8c2SApple OSS Distributions 		if (task == kernel_task) {
1944*19c3b8c2SApple OSS Distributions 			mdOpts |= kIOMemoryAutoPrepare;
1945*19c3b8c2SApple OSS Distributions 		}
1946*19c3b8c2SApple OSS Distributions 	} else {
1947*19c3b8c2SApple OSS Distributions 		mdOpts |= kIOMemoryTypePhysical;
1948*19c3b8c2SApple OSS Distributions 	}
1949*19c3b8c2SApple OSS Distributions 
1950*19c3b8c2SApple OSS Distributions 	return initWithOptions(ranges, count, 0, task, mdOpts, /* mapper */ NULL);
1951*19c3b8c2SApple OSS Distributions }
1952*19c3b8c2SApple OSS Distributions #endif /* !__LP64__ */
1953*19c3b8c2SApple OSS Distributions 
1954*19c3b8c2SApple OSS Distributions /*
1955*19c3b8c2SApple OSS Distributions  * initWithOptions:
1956*19c3b8c2SApple OSS Distributions  *
1957*19c3b8c2SApple OSS Distributions  *  IOMemoryDescriptor. The buffer is made up of several virtual address ranges,
1958*19c3b8c2SApple OSS Distributions  * from a given task, several physical ranges, an UPL from the ubc
1959*19c3b8c2SApple OSS Distributions  * system or a uio (may be 64bit) from the BSD subsystem.
1960*19c3b8c2SApple OSS Distributions  *
1961*19c3b8c2SApple OSS Distributions  * Passing the ranges as a reference will avoid an extra allocation.
1962*19c3b8c2SApple OSS Distributions  *
1963*19c3b8c2SApple OSS Distributions  * An IOMemoryDescriptor can be re-used by calling initWithOptions again on an
1964*19c3b8c2SApple OSS Distributions  * existing instance -- note this behavior is not commonly supported in other
1965*19c3b8c2SApple OSS Distributions  * I/O Kit classes, although it is supported here.
1966*19c3b8c2SApple OSS Distributions  */
1967*19c3b8c2SApple OSS Distributions 
1968*19c3b8c2SApple OSS Distributions bool
initWithOptions(void * buffers,UInt32 count,UInt32 offset,task_t task,IOOptionBits options,IOMapper * mapper)1969*19c3b8c2SApple OSS Distributions IOGeneralMemoryDescriptor::initWithOptions(void *       buffers,
1970*19c3b8c2SApple OSS Distributions     UInt32       count,
1971*19c3b8c2SApple OSS Distributions     UInt32       offset,
1972*19c3b8c2SApple OSS Distributions     task_t       task,
1973*19c3b8c2SApple OSS Distributions     IOOptionBits options,
1974*19c3b8c2SApple OSS Distributions     IOMapper *   mapper)
1975*19c3b8c2SApple OSS Distributions {
1976*19c3b8c2SApple OSS Distributions 	IOOptionBits type = options & kIOMemoryTypeMask;
1977*19c3b8c2SApple OSS Distributions 
1978*19c3b8c2SApple OSS Distributions #ifndef __LP64__
1979*19c3b8c2SApple OSS Distributions 	if (task
1980*19c3b8c2SApple OSS Distributions 	    && (kIOMemoryTypeVirtual == type)
1981*19c3b8c2SApple OSS Distributions 	    && vm_map_is_64bit(get_task_map(task))
1982*19c3b8c2SApple OSS Distributions 	    && ((IOVirtualRange *) buffers)->address) {
1983*19c3b8c2SApple OSS Distributions 		OSReportWithBacktrace("IOMemoryDescriptor: attempt to create 32b virtual in 64b task, use ::withAddressRange()");
1984*19c3b8c2SApple OSS Distributions 		return false;
1985*19c3b8c2SApple OSS Distributions 	}
1986*19c3b8c2SApple OSS Distributions #endif /* !__LP64__ */
1987*19c3b8c2SApple OSS Distributions 
1988*19c3b8c2SApple OSS Distributions 	// Grab the original MD's configuation data to initialse the
1989*19c3b8c2SApple OSS Distributions 	// arguments to this function.
1990*19c3b8c2SApple OSS Distributions 	if (kIOMemoryTypePersistentMD == type) {
1991*19c3b8c2SApple OSS Distributions 		IOMDPersistentInitData *initData = (typeof(initData))buffers;
1992*19c3b8c2SApple OSS Distributions 		const IOGeneralMemoryDescriptor *orig = initData->fMD;
1993*19c3b8c2SApple OSS Distributions 		ioGMDData *dataP = getDataP(orig->_memoryEntries);
1994*19c3b8c2SApple OSS Distributions 
1995*19c3b8c2SApple OSS Distributions 		// Only accept persistent memory descriptors with valid dataP data.
1996*19c3b8c2SApple OSS Distributions 		assert(orig->_rangesCount == 1);
1997*19c3b8c2SApple OSS Distributions 		if (!(orig->_flags & kIOMemoryPersistent) || !dataP) {
1998*19c3b8c2SApple OSS Distributions 			return false;
1999*19c3b8c2SApple OSS Distributions 		}
2000*19c3b8c2SApple OSS Distributions 
2001*19c3b8c2SApple OSS Distributions 		_memRef = initData->fMemRef; // Grab the new named entry
2002*19c3b8c2SApple OSS Distributions 		options = orig->_flags & ~kIOMemoryAsReference;
2003*19c3b8c2SApple OSS Distributions 		type = options & kIOMemoryTypeMask;
2004*19c3b8c2SApple OSS Distributions 		buffers = orig->_ranges.v;
2005*19c3b8c2SApple OSS Distributions 		count = orig->_rangesCount;
2006*19c3b8c2SApple OSS Distributions 
2007*19c3b8c2SApple OSS Distributions 		// Now grab the original task and whatever mapper was previously used
2008*19c3b8c2SApple OSS Distributions 		task = orig->_task;
2009*19c3b8c2SApple OSS Distributions 		mapper = dataP->fMapper;
2010*19c3b8c2SApple OSS Distributions 
2011*19c3b8c2SApple OSS Distributions 		// We are ready to go through the original initialisation now
2012*19c3b8c2SApple OSS Distributions 	}
2013*19c3b8c2SApple OSS Distributions 
2014*19c3b8c2SApple OSS Distributions 	switch (type) {
2015*19c3b8c2SApple OSS Distributions 	case kIOMemoryTypeUIO:
2016*19c3b8c2SApple OSS Distributions 	case kIOMemoryTypeVirtual:
2017*19c3b8c2SApple OSS Distributions #ifndef __LP64__
2018*19c3b8c2SApple OSS Distributions 	case kIOMemoryTypeVirtual64:
2019*19c3b8c2SApple OSS Distributions #endif /* !__LP64__ */
2020*19c3b8c2SApple OSS Distributions 		assert(task);
2021*19c3b8c2SApple OSS Distributions 		if (!task) {
2022*19c3b8c2SApple OSS Distributions 			return false;
2023*19c3b8c2SApple OSS Distributions 		}
2024*19c3b8c2SApple OSS Distributions 		break;
2025*19c3b8c2SApple OSS Distributions 
2026*19c3b8c2SApple OSS Distributions 	case kIOMemoryTypePhysical:     // Neither Physical nor UPL should have a task
2027*19c3b8c2SApple OSS Distributions #ifndef __LP64__
2028*19c3b8c2SApple OSS Distributions 	case kIOMemoryTypePhysical64:
2029*19c3b8c2SApple OSS Distributions #endif /* !__LP64__ */
2030*19c3b8c2SApple OSS Distributions 	case kIOMemoryTypeUPL:
2031*19c3b8c2SApple OSS Distributions 		assert(!task);
2032*19c3b8c2SApple OSS Distributions 		break;
2033*19c3b8c2SApple OSS Distributions 	default:
2034*19c3b8c2SApple OSS Distributions 		return false; /* bad argument */
2035*19c3b8c2SApple OSS Distributions 	}
2036*19c3b8c2SApple OSS Distributions 
2037*19c3b8c2SApple OSS Distributions 	assert(buffers);
2038*19c3b8c2SApple OSS Distributions 	assert(count);
2039*19c3b8c2SApple OSS Distributions 
2040*19c3b8c2SApple OSS Distributions 	/*
2041*19c3b8c2SApple OSS Distributions 	 * We can check the _initialized  instance variable before having ever set
2042*19c3b8c2SApple OSS Distributions 	 * it to an initial value because I/O Kit guarantees that all our instance
2043*19c3b8c2SApple OSS Distributions 	 * variables are zeroed on an object's allocation.
2044*19c3b8c2SApple OSS Distributions 	 */
2045*19c3b8c2SApple OSS Distributions 
2046*19c3b8c2SApple OSS Distributions 	if (_initialized) {
2047*19c3b8c2SApple OSS Distributions 		/*
2048*19c3b8c2SApple OSS Distributions 		 * An existing memory descriptor is being retargeted to point to
2049*19c3b8c2SApple OSS Distributions 		 * somewhere else.  Clean up our present state.
2050*19c3b8c2SApple OSS Distributions 		 */
2051*19c3b8c2SApple OSS Distributions 		IOOptionBits type = _flags & kIOMemoryTypeMask;
2052*19c3b8c2SApple OSS Distributions 		if ((kIOMemoryTypePhysical != type) && (kIOMemoryTypePhysical64 != type)) {
2053*19c3b8c2SApple OSS Distributions 			while (_wireCount) {
2054*19c3b8c2SApple OSS Distributions 				complete();
2055*19c3b8c2SApple OSS Distributions 			}
2056*19c3b8c2SApple OSS Distributions 		}
2057*19c3b8c2SApple OSS Distributions 		if (_ranges.v && !(kIOMemoryAsReference & _flags)) {
2058*19c3b8c2SApple OSS Distributions 			if (kIOMemoryTypeUIO == type) {
2059*19c3b8c2SApple OSS Distributions 				uio_free((uio_t) _ranges.v);
2060*19c3b8c2SApple OSS Distributions 			}
2061*19c3b8c2SApple OSS Distributions #ifndef __LP64__
2062*19c3b8c2SApple OSS Distributions 			else if ((kIOMemoryTypeVirtual64 == type) || (kIOMemoryTypePhysical64 == type)) {
2063*19c3b8c2SApple OSS Distributions 				IODelete(_ranges.v64, IOAddressRange, _rangesCount);
2064*19c3b8c2SApple OSS Distributions 			}
2065*19c3b8c2SApple OSS Distributions #endif /* !__LP64__ */
2066*19c3b8c2SApple OSS Distributions 			else {
2067*19c3b8c2SApple OSS Distributions 				IODelete(_ranges.v, IOVirtualRange, _rangesCount);
2068*19c3b8c2SApple OSS Distributions 			}
2069*19c3b8c2SApple OSS Distributions 		}
2070*19c3b8c2SApple OSS Distributions 
2071*19c3b8c2SApple OSS Distributions 		options |= (kIOMemoryRedirected & _flags);
2072*19c3b8c2SApple OSS Distributions 		if (!(kIOMemoryRedirected & options)) {
2073*19c3b8c2SApple OSS Distributions 			if (_memRef) {
2074*19c3b8c2SApple OSS Distributions 				memoryReferenceRelease(_memRef);
2075*19c3b8c2SApple OSS Distributions 				_memRef = NULL;
2076*19c3b8c2SApple OSS Distributions 			}
2077*19c3b8c2SApple OSS Distributions 			if (_mappings) {
2078*19c3b8c2SApple OSS Distributions 				_mappings->flushCollection();
2079*19c3b8c2SApple OSS Distributions 			}
2080*19c3b8c2SApple OSS Distributions 		}
2081*19c3b8c2SApple OSS Distributions 	} else {
2082*19c3b8c2SApple OSS Distributions 		if (!super::init()) {
2083*19c3b8c2SApple OSS Distributions 			return false;
2084*19c3b8c2SApple OSS Distributions 		}
2085*19c3b8c2SApple OSS Distributions 		_initialized = true;
2086*19c3b8c2SApple OSS Distributions 	}
2087*19c3b8c2SApple OSS Distributions 
2088*19c3b8c2SApple OSS Distributions 	// Grab the appropriate mapper
2089*19c3b8c2SApple OSS Distributions 	if (kIOMemoryHostOrRemote & options) {
2090*19c3b8c2SApple OSS Distributions 		options |= kIOMemoryMapperNone;
2091*19c3b8c2SApple OSS Distributions 	}
2092*19c3b8c2SApple OSS Distributions 	if (kIOMemoryMapperNone & options) {
2093*19c3b8c2SApple OSS Distributions 		mapper = NULL; // No Mapper
2094*19c3b8c2SApple OSS Distributions 	} else if (mapper == kIOMapperSystem) {
2095*19c3b8c2SApple OSS Distributions 		IOMapper::checkForSystemMapper();
2096*19c3b8c2SApple OSS Distributions 		gIOSystemMapper = mapper = IOMapper::gSystem;
2097*19c3b8c2SApple OSS Distributions 	}
2098*19c3b8c2SApple OSS Distributions 
2099*19c3b8c2SApple OSS Distributions 	// Remove the dynamic internal use flags from the initial setting
2100*19c3b8c2SApple OSS Distributions 	options               &= ~(kIOMemoryPreparedReadOnly);
2101*19c3b8c2SApple OSS Distributions 	_flags                 = options;
2102*19c3b8c2SApple OSS Distributions 	_task                  = task;
2103*19c3b8c2SApple OSS Distributions 
2104*19c3b8c2SApple OSS Distributions #ifndef __LP64__
2105*19c3b8c2SApple OSS Distributions 	_direction             = (IODirection) (_flags & kIOMemoryDirectionMask);
2106*19c3b8c2SApple OSS Distributions #endif /* !__LP64__ */
2107*19c3b8c2SApple OSS Distributions 
2108*19c3b8c2SApple OSS Distributions 	_dmaReferences = 0;
2109*19c3b8c2SApple OSS Distributions 	__iomd_reservedA = 0;
2110*19c3b8c2SApple OSS Distributions 	__iomd_reservedB = 0;
2111*19c3b8c2SApple OSS Distributions 	_highestPage = 0;
2112*19c3b8c2SApple OSS Distributions 
2113*19c3b8c2SApple OSS Distributions 	if (kIOMemoryThreadSafe & options) {
2114*19c3b8c2SApple OSS Distributions 		if (!_prepareLock) {
2115*19c3b8c2SApple OSS Distributions 			_prepareLock = IOLockAlloc();
2116*19c3b8c2SApple OSS Distributions 		}
2117*19c3b8c2SApple OSS Distributions 	} else if (_prepareLock) {
2118*19c3b8c2SApple OSS Distributions 		IOLockFree(_prepareLock);
2119*19c3b8c2SApple OSS Distributions 		_prepareLock = NULL;
2120*19c3b8c2SApple OSS Distributions 	}
2121*19c3b8c2SApple OSS Distributions 
2122*19c3b8c2SApple OSS Distributions 	if (kIOMemoryTypeUPL == type) {
2123*19c3b8c2SApple OSS Distributions 		ioGMDData *dataP;
2124*19c3b8c2SApple OSS Distributions 		unsigned int dataSize = computeDataSize(/* pages */ 0, /* upls */ 1);
2125*19c3b8c2SApple OSS Distributions 
2126*19c3b8c2SApple OSS Distributions 		if (!initMemoryEntries(dataSize, mapper)) {
2127*19c3b8c2SApple OSS Distributions 			return false;
2128*19c3b8c2SApple OSS Distributions 		}
2129*19c3b8c2SApple OSS Distributions 		dataP = getDataP(_memoryEntries);
2130*19c3b8c2SApple OSS Distributions 		dataP->fPageCnt = 0;
2131*19c3b8c2SApple OSS Distributions 		switch (kIOMemoryDirectionMask & options) {
2132*19c3b8c2SApple OSS Distributions 		case kIODirectionOut:
2133*19c3b8c2SApple OSS Distributions 			dataP->fDMAAccess = kIODMAMapReadAccess;
2134*19c3b8c2SApple OSS Distributions 			break;
2135*19c3b8c2SApple OSS Distributions 		case kIODirectionIn:
2136*19c3b8c2SApple OSS Distributions 			dataP->fDMAAccess = kIODMAMapWriteAccess;
2137*19c3b8c2SApple OSS Distributions 			break;
2138*19c3b8c2SApple OSS Distributions 		case kIODirectionNone:
2139*19c3b8c2SApple OSS Distributions 		case kIODirectionOutIn:
2140*19c3b8c2SApple OSS Distributions 		default:
2141*19c3b8c2SApple OSS Distributions 			panic("bad dir for upl 0x%x", (int) options);
2142*19c3b8c2SApple OSS Distributions 			break;
2143*19c3b8c2SApple OSS Distributions 		}
2144*19c3b8c2SApple OSS Distributions 		//       _wireCount++;	// UPLs start out life wired
2145*19c3b8c2SApple OSS Distributions 
2146*19c3b8c2SApple OSS Distributions 		_length    = count;
2147*19c3b8c2SApple OSS Distributions 		_pages    += atop_32(offset + count + PAGE_MASK) - atop_32(offset);
2148*19c3b8c2SApple OSS Distributions 
2149*19c3b8c2SApple OSS Distributions 		ioPLBlock iopl;
2150*19c3b8c2SApple OSS Distributions 		iopl.fIOPL = (upl_t) buffers;
2151*19c3b8c2SApple OSS Distributions 		upl_set_referenced(iopl.fIOPL, true);
2152*19c3b8c2SApple OSS Distributions 		upl_page_info_t *pageList = UPL_GET_INTERNAL_PAGE_LIST(iopl.fIOPL);
2153*19c3b8c2SApple OSS Distributions 
2154*19c3b8c2SApple OSS Distributions 		if (upl_get_size(iopl.fIOPL) < (count + offset)) {
2155*19c3b8c2SApple OSS Distributions 			panic("short external upl");
2156*19c3b8c2SApple OSS Distributions 		}
2157*19c3b8c2SApple OSS Distributions 
2158*19c3b8c2SApple OSS Distributions 		_highestPage = upl_get_highest_page(iopl.fIOPL);
2159*19c3b8c2SApple OSS Distributions 		DEBUG4K_IOKIT("offset 0x%x task %p options 0x%x -> _highestPage 0x%x\n", (uint32_t)offset, task, (uint32_t)options, _highestPage);
2160*19c3b8c2SApple OSS Distributions 
2161*19c3b8c2SApple OSS Distributions 		// Set the flag kIOPLOnDevice convieniently equal to 1
2162*19c3b8c2SApple OSS Distributions 		iopl.fFlags  = pageList->device | kIOPLExternUPL;
2163*19c3b8c2SApple OSS Distributions 		if (!pageList->device) {
2164*19c3b8c2SApple OSS Distributions 			// Pre-compute the offset into the UPL's page list
2165*19c3b8c2SApple OSS Distributions 			pageList = &pageList[atop_32(offset)];
2166*19c3b8c2SApple OSS Distributions 			offset &= PAGE_MASK;
2167*19c3b8c2SApple OSS Distributions 		}
2168*19c3b8c2SApple OSS Distributions 		iopl.fIOMDOffset = 0;
2169*19c3b8c2SApple OSS Distributions 		iopl.fMappedPage = 0;
2170*19c3b8c2SApple OSS Distributions 		iopl.fPageInfo = (vm_address_t) pageList;
2171*19c3b8c2SApple OSS Distributions 		iopl.fPageOffset = offset;
2172*19c3b8c2SApple OSS Distributions 		_memoryEntries->appendBytes(&iopl, sizeof(iopl));
2173*19c3b8c2SApple OSS Distributions 	} else {
2174*19c3b8c2SApple OSS Distributions 		// kIOMemoryTypeVirtual  | kIOMemoryTypeVirtual64 | kIOMemoryTypeUIO
2175*19c3b8c2SApple OSS Distributions 		// kIOMemoryTypePhysical | kIOMemoryTypePhysical64
2176*19c3b8c2SApple OSS Distributions 
2177*19c3b8c2SApple OSS Distributions 		// Initialize the memory descriptor
2178*19c3b8c2SApple OSS Distributions 		if (options & kIOMemoryAsReference) {
2179*19c3b8c2SApple OSS Distributions #ifndef __LP64__
2180*19c3b8c2SApple OSS Distributions 			_rangesIsAllocated = false;
2181*19c3b8c2SApple OSS Distributions #endif /* !__LP64__ */
2182*19c3b8c2SApple OSS Distributions 
2183*19c3b8c2SApple OSS Distributions 			// Hack assignment to get the buffer arg into _ranges.
2184*19c3b8c2SApple OSS Distributions 			// I'd prefer to do _ranges = (Ranges) buffers, but that doesn't
2185*19c3b8c2SApple OSS Distributions 			// work, C++ sigh.
2186*19c3b8c2SApple OSS Distributions 			// This also initialises the uio & physical ranges.
2187*19c3b8c2SApple OSS Distributions 			_ranges.v = (IOVirtualRange *) buffers;
2188*19c3b8c2SApple OSS Distributions 		} else {
2189*19c3b8c2SApple OSS Distributions #ifndef __LP64__
2190*19c3b8c2SApple OSS Distributions 			_rangesIsAllocated = true;
2191*19c3b8c2SApple OSS Distributions #endif /* !__LP64__ */
2192*19c3b8c2SApple OSS Distributions 			switch (type) {
2193*19c3b8c2SApple OSS Distributions 			case kIOMemoryTypeUIO:
2194*19c3b8c2SApple OSS Distributions 				_ranges.v = (IOVirtualRange *) uio_duplicate((uio_t) buffers);
2195*19c3b8c2SApple OSS Distributions 				break;
2196*19c3b8c2SApple OSS Distributions 
2197*19c3b8c2SApple OSS Distributions #ifndef __LP64__
2198*19c3b8c2SApple OSS Distributions 			case kIOMemoryTypeVirtual64:
2199*19c3b8c2SApple OSS Distributions 			case kIOMemoryTypePhysical64:
2200*19c3b8c2SApple OSS Distributions 				if (count == 1
2201*19c3b8c2SApple OSS Distributions #ifndef __arm__
2202*19c3b8c2SApple OSS Distributions 				    && (((IOAddressRange *) buffers)->address + ((IOAddressRange *) buffers)->length) <= 0x100000000ULL
2203*19c3b8c2SApple OSS Distributions #endif
2204*19c3b8c2SApple OSS Distributions 				    ) {
2205*19c3b8c2SApple OSS Distributions 					if (kIOMemoryTypeVirtual64 == type) {
2206*19c3b8c2SApple OSS Distributions 						type = kIOMemoryTypeVirtual;
2207*19c3b8c2SApple OSS Distributions 					} else {
2208*19c3b8c2SApple OSS Distributions 						type = kIOMemoryTypePhysical;
2209*19c3b8c2SApple OSS Distributions 					}
2210*19c3b8c2SApple OSS Distributions 					_flags = (_flags & ~kIOMemoryTypeMask) | type | kIOMemoryAsReference;
2211*19c3b8c2SApple OSS Distributions 					_rangesIsAllocated = false;
2212*19c3b8c2SApple OSS Distributions 					_ranges.v = &_singleRange.v;
2213*19c3b8c2SApple OSS Distributions 					_singleRange.v.address = ((IOAddressRange *) buffers)->address;
2214*19c3b8c2SApple OSS Distributions 					_singleRange.v.length  = ((IOAddressRange *) buffers)->length;
2215*19c3b8c2SApple OSS Distributions 					break;
2216*19c3b8c2SApple OSS Distributions 				}
2217*19c3b8c2SApple OSS Distributions 				_ranges.v64 = IONew(IOAddressRange, count);
2218*19c3b8c2SApple OSS Distributions 				if (!_ranges.v64) {
2219*19c3b8c2SApple OSS Distributions 					return false;
2220*19c3b8c2SApple OSS Distributions 				}
2221*19c3b8c2SApple OSS Distributions 				bcopy(buffers, _ranges.v, count * sizeof(IOAddressRange));
2222*19c3b8c2SApple OSS Distributions 				break;
2223*19c3b8c2SApple OSS Distributions #endif /* !__LP64__ */
2224*19c3b8c2SApple OSS Distributions 			case kIOMemoryTypeVirtual:
2225*19c3b8c2SApple OSS Distributions 			case kIOMemoryTypePhysical:
2226*19c3b8c2SApple OSS Distributions 				if (count == 1) {
2227*19c3b8c2SApple OSS Distributions 					_flags |= kIOMemoryAsReference;
2228*19c3b8c2SApple OSS Distributions #ifndef __LP64__
2229*19c3b8c2SApple OSS Distributions 					_rangesIsAllocated = false;
2230*19c3b8c2SApple OSS Distributions #endif /* !__LP64__ */
2231*19c3b8c2SApple OSS Distributions 					_ranges.v = &_singleRange.v;
2232*19c3b8c2SApple OSS Distributions 				} else {
2233*19c3b8c2SApple OSS Distributions 					_ranges.v = IONew(IOVirtualRange, count);
2234*19c3b8c2SApple OSS Distributions 					if (!_ranges.v) {
2235*19c3b8c2SApple OSS Distributions 						return false;
2236*19c3b8c2SApple OSS Distributions 					}
2237*19c3b8c2SApple OSS Distributions 				}
2238*19c3b8c2SApple OSS Distributions 				bcopy(buffers, _ranges.v, count * sizeof(IOVirtualRange));
2239*19c3b8c2SApple OSS Distributions 				break;
2240*19c3b8c2SApple OSS Distributions 			}
2241*19c3b8c2SApple OSS Distributions 		}
2242*19c3b8c2SApple OSS Distributions #if CONFIG_PROB_GZALLOC
2243*19c3b8c2SApple OSS Distributions 		if (task == kernel_task) {
2244*19c3b8c2SApple OSS Distributions 			for (UInt32 i = 0; i < count; i++) {
2245*19c3b8c2SApple OSS Distributions 				_ranges.v[i].address = pgz_decode(_ranges.v[i].address, _ranges.v[i].length);
2246*19c3b8c2SApple OSS Distributions 			}
2247*19c3b8c2SApple OSS Distributions 		}
2248*19c3b8c2SApple OSS Distributions #endif /* CONFIG_PROB_GZALLOC */
2249*19c3b8c2SApple OSS Distributions 		_rangesCount = count;
2250*19c3b8c2SApple OSS Distributions 
2251*19c3b8c2SApple OSS Distributions 		// Find starting address within the vector of ranges
2252*19c3b8c2SApple OSS Distributions 		Ranges vec = _ranges;
2253*19c3b8c2SApple OSS Distributions 		mach_vm_size_t totalLength = 0;
2254*19c3b8c2SApple OSS Distributions 		unsigned int ind, pages = 0;
2255*19c3b8c2SApple OSS Distributions 		for (ind = 0; ind < count; ind++) {
2256*19c3b8c2SApple OSS Distributions 			mach_vm_address_t addr;
2257*19c3b8c2SApple OSS Distributions 			mach_vm_address_t endAddr;
2258*19c3b8c2SApple OSS Distributions 			mach_vm_size_t    len;
2259*19c3b8c2SApple OSS Distributions 
2260*19c3b8c2SApple OSS Distributions 			// addr & len are returned by this function
2261*19c3b8c2SApple OSS Distributions 			getAddrLenForInd(addr, len, type, vec, ind);
2262*19c3b8c2SApple OSS Distributions 			if (_task) {
2263*19c3b8c2SApple OSS Distributions 				mach_vm_size_t phys_size;
2264*19c3b8c2SApple OSS Distributions 				kern_return_t kret;
2265*19c3b8c2SApple OSS Distributions 				kret = vm_map_range_physical_size(get_task_map(_task), addr, len, &phys_size);
2266*19c3b8c2SApple OSS Distributions 				if (KERN_SUCCESS != kret) {
2267*19c3b8c2SApple OSS Distributions 					break;
2268*19c3b8c2SApple OSS Distributions 				}
2269*19c3b8c2SApple OSS Distributions 				if (os_add_overflow(pages, atop_64(phys_size), &pages)) {
2270*19c3b8c2SApple OSS Distributions 					break;
2271*19c3b8c2SApple OSS Distributions 				}
2272*19c3b8c2SApple OSS Distributions 			} else {
2273*19c3b8c2SApple OSS Distributions 				if (os_add3_overflow(addr, len, PAGE_MASK, &endAddr)) {
2274*19c3b8c2SApple OSS Distributions 					break;
2275*19c3b8c2SApple OSS Distributions 				}
2276*19c3b8c2SApple OSS Distributions 				if (!(kIOMemoryRemote & options) && (atop_64(endAddr) > UINT_MAX)) {
2277*19c3b8c2SApple OSS Distributions 					break;
2278*19c3b8c2SApple OSS Distributions 				}
2279*19c3b8c2SApple OSS Distributions 				if (os_add_overflow(pages, (atop_64(endAddr) - atop_64(addr)), &pages)) {
2280*19c3b8c2SApple OSS Distributions 					break;
2281*19c3b8c2SApple OSS Distributions 				}
2282*19c3b8c2SApple OSS Distributions 			}
2283*19c3b8c2SApple OSS Distributions 			if (os_add_overflow(totalLength, len, &totalLength)) {
2284*19c3b8c2SApple OSS Distributions 				break;
2285*19c3b8c2SApple OSS Distributions 			}
2286*19c3b8c2SApple OSS Distributions 			if ((kIOMemoryTypePhysical == type) || (kIOMemoryTypePhysical64 == type)) {
2287*19c3b8c2SApple OSS Distributions 				uint64_t highPage = atop_64(addr + len - 1);
2288*19c3b8c2SApple OSS Distributions 				if ((highPage > _highestPage) && (highPage <= UINT_MAX)) {
2289*19c3b8c2SApple OSS Distributions 					_highestPage = (ppnum_t) highPage;
2290*19c3b8c2SApple OSS Distributions 					DEBUG4K_IOKIT("offset 0x%x task %p options 0x%x -> _highestPage 0x%x\n", (uint32_t)offset, task, (uint32_t)options, _highestPage);
2291*19c3b8c2SApple OSS Distributions 				}
2292*19c3b8c2SApple OSS Distributions 			}
2293*19c3b8c2SApple OSS Distributions 		}
2294*19c3b8c2SApple OSS Distributions 		if ((ind < count)
2295*19c3b8c2SApple OSS Distributions 		    || (totalLength != ((IOByteCount) totalLength))) {
2296*19c3b8c2SApple OSS Distributions 			return false;                                   /* overflow */
2297*19c3b8c2SApple OSS Distributions 		}
2298*19c3b8c2SApple OSS Distributions 		_length      = totalLength;
2299*19c3b8c2SApple OSS Distributions 		_pages       = pages;
2300*19c3b8c2SApple OSS Distributions 
2301*19c3b8c2SApple OSS Distributions 		// Auto-prepare memory at creation time.
2302*19c3b8c2SApple OSS Distributions 		// Implied completion when descriptor is free-ed
2303*19c3b8c2SApple OSS Distributions 
2304*19c3b8c2SApple OSS Distributions 
2305*19c3b8c2SApple OSS Distributions 		if ((kIOMemoryTypePhysical == type) || (kIOMemoryTypePhysical64 == type)) {
2306*19c3b8c2SApple OSS Distributions 			_wireCount++; // Physical MDs are, by definition, wired
2307*19c3b8c2SApple OSS Distributions 		} else { /* kIOMemoryTypeVirtual | kIOMemoryTypeVirtual64 | kIOMemoryTypeUIO */
2308*19c3b8c2SApple OSS Distributions 			ioGMDData *dataP;
2309*19c3b8c2SApple OSS Distributions 			unsigned dataSize;
2310*19c3b8c2SApple OSS Distributions 
2311*19c3b8c2SApple OSS Distributions 			if (_pages > atop_64(max_mem)) {
2312*19c3b8c2SApple OSS Distributions 				return false;
2313*19c3b8c2SApple OSS Distributions 			}
2314*19c3b8c2SApple OSS Distributions 
2315*19c3b8c2SApple OSS Distributions 			dataSize = computeDataSize(_pages, /* upls */ count * 2);
2316*19c3b8c2SApple OSS Distributions 			if (!initMemoryEntries(dataSize, mapper)) {
2317*19c3b8c2SApple OSS Distributions 				return false;
2318*19c3b8c2SApple OSS Distributions 			}
2319*19c3b8c2SApple OSS Distributions 			dataP = getDataP(_memoryEntries);
2320*19c3b8c2SApple OSS Distributions 			dataP->fPageCnt = _pages;
2321*19c3b8c2SApple OSS Distributions 
2322*19c3b8c2SApple OSS Distributions 			if (((_task != kernel_task) || (kIOMemoryBufferPageable & _flags))
2323*19c3b8c2SApple OSS Distributions 			    && (VM_KERN_MEMORY_NONE == _kernelTag)) {
2324*19c3b8c2SApple OSS Distributions 				_kernelTag = IOMemoryTag(kernel_map);
2325*19c3b8c2SApple OSS Distributions 				if (_kernelTag == gIOSurfaceTag) {
2326*19c3b8c2SApple OSS Distributions 					_userTag = VM_MEMORY_IOSURFACE;
2327*19c3b8c2SApple OSS Distributions 				}
2328*19c3b8c2SApple OSS Distributions 			}
2329*19c3b8c2SApple OSS Distributions 
2330*19c3b8c2SApple OSS Distributions 			if ((kIOMemoryPersistent & _flags) && !_memRef) {
2331*19c3b8c2SApple OSS Distributions 				IOReturn
2332*19c3b8c2SApple OSS Distributions 				    err = memoryReferenceCreate(0, &_memRef);
2333*19c3b8c2SApple OSS Distributions 				if (kIOReturnSuccess != err) {
2334*19c3b8c2SApple OSS Distributions 					return false;
2335*19c3b8c2SApple OSS Distributions 				}
2336*19c3b8c2SApple OSS Distributions 			}
2337*19c3b8c2SApple OSS Distributions 
2338*19c3b8c2SApple OSS Distributions 			if ((_flags & kIOMemoryAutoPrepare)
2339*19c3b8c2SApple OSS Distributions 			    && prepare() != kIOReturnSuccess) {
2340*19c3b8c2SApple OSS Distributions 				return false;
2341*19c3b8c2SApple OSS Distributions 			}
2342*19c3b8c2SApple OSS Distributions 		}
2343*19c3b8c2SApple OSS Distributions 	}
2344*19c3b8c2SApple OSS Distributions 
2345*19c3b8c2SApple OSS Distributions 	return true;
2346*19c3b8c2SApple OSS Distributions }
2347*19c3b8c2SApple OSS Distributions 
2348*19c3b8c2SApple OSS Distributions /*
2349*19c3b8c2SApple OSS Distributions  * free
2350*19c3b8c2SApple OSS Distributions  *
2351*19c3b8c2SApple OSS Distributions  * Free resources.
2352*19c3b8c2SApple OSS Distributions  */
2353*19c3b8c2SApple OSS Distributions void
free()2354*19c3b8c2SApple OSS Distributions IOGeneralMemoryDescriptor::free()
2355*19c3b8c2SApple OSS Distributions {
2356*19c3b8c2SApple OSS Distributions 	IOOptionBits type = _flags & kIOMemoryTypeMask;
2357*19c3b8c2SApple OSS Distributions 
2358*19c3b8c2SApple OSS Distributions 	if (reserved && reserved->dp.memory) {
2359*19c3b8c2SApple OSS Distributions 		LOCK;
2360*19c3b8c2SApple OSS Distributions 		reserved->dp.memory = NULL;
2361*19c3b8c2SApple OSS Distributions 		UNLOCK;
2362*19c3b8c2SApple OSS Distributions 	}
2363*19c3b8c2SApple OSS Distributions 	if ((kIOMemoryTypePhysical == type) || (kIOMemoryTypePhysical64 == type)) {
2364*19c3b8c2SApple OSS Distributions 		ioGMDData * dataP;
2365*19c3b8c2SApple OSS Distributions 		if (_memoryEntries && (dataP = getDataP(_memoryEntries)) && dataP->fMappedBaseValid) {
2366*19c3b8c2SApple OSS Distributions 			dmaUnmap(dataP->fMapper, NULL, 0, dataP->fMappedBase, dataP->fMappedLength);
2367*19c3b8c2SApple OSS Distributions 			dataP->fMappedBaseValid = dataP->fMappedBase = 0;
2368*19c3b8c2SApple OSS Distributions 		}
2369*19c3b8c2SApple OSS Distributions 	} else {
2370*19c3b8c2SApple OSS Distributions 		while (_wireCount) {
2371*19c3b8c2SApple OSS Distributions 			complete();
2372*19c3b8c2SApple OSS Distributions 		}
2373*19c3b8c2SApple OSS Distributions 	}
2374*19c3b8c2SApple OSS Distributions 
2375*19c3b8c2SApple OSS Distributions 	if (_memoryEntries) {
2376*19c3b8c2SApple OSS Distributions 		_memoryEntries.reset();
2377*19c3b8c2SApple OSS Distributions 	}
2378*19c3b8c2SApple OSS Distributions 
2379*19c3b8c2SApple OSS Distributions 	if (_ranges.v && !(kIOMemoryAsReference & _flags)) {
2380*19c3b8c2SApple OSS Distributions 		if (kIOMemoryTypeUIO == type) {
2381*19c3b8c2SApple OSS Distributions 			uio_free((uio_t) _ranges.v);
2382*19c3b8c2SApple OSS Distributions 		}
2383*19c3b8c2SApple OSS Distributions #ifndef __LP64__
2384*19c3b8c2SApple OSS Distributions 		else if ((kIOMemoryTypeVirtual64 == type) || (kIOMemoryTypePhysical64 == type)) {
2385*19c3b8c2SApple OSS Distributions 			IODelete(_ranges.v64, IOAddressRange, _rangesCount);
2386*19c3b8c2SApple OSS Distributions 		}
2387*19c3b8c2SApple OSS Distributions #endif /* !__LP64__ */
2388*19c3b8c2SApple OSS Distributions 		else {
2389*19c3b8c2SApple OSS Distributions 			IODelete(_ranges.v, IOVirtualRange, _rangesCount);
2390*19c3b8c2SApple OSS Distributions 		}
2391*19c3b8c2SApple OSS Distributions 
2392*19c3b8c2SApple OSS Distributions 		_ranges.v = NULL;
2393*19c3b8c2SApple OSS Distributions 	}
2394*19c3b8c2SApple OSS Distributions 
2395*19c3b8c2SApple OSS Distributions 	if (reserved) {
2396*19c3b8c2SApple OSS Distributions 		cleanKernelReserved(reserved);
2397*19c3b8c2SApple OSS Distributions 		if (reserved->dp.devicePager) {
2398*19c3b8c2SApple OSS Distributions 			// memEntry holds a ref on the device pager which owns reserved
2399*19c3b8c2SApple OSS Distributions 			// (IOMemoryDescriptorReserved) so no reserved access after this point
2400*19c3b8c2SApple OSS Distributions 			device_pager_deallocate((memory_object_t) reserved->dp.devicePager );
2401*19c3b8c2SApple OSS Distributions 		} else {
2402*19c3b8c2SApple OSS Distributions 			IOFreeType(reserved, IOMemoryDescriptorReserved);
2403*19c3b8c2SApple OSS Distributions 		}
2404*19c3b8c2SApple OSS Distributions 		reserved = NULL;
2405*19c3b8c2SApple OSS Distributions 	}
2406*19c3b8c2SApple OSS Distributions 
2407*19c3b8c2SApple OSS Distributions 	if (_memRef) {
2408*19c3b8c2SApple OSS Distributions 		memoryReferenceRelease(_memRef);
2409*19c3b8c2SApple OSS Distributions 	}
2410*19c3b8c2SApple OSS Distributions 	if (_prepareLock) {
2411*19c3b8c2SApple OSS Distributions 		IOLockFree(_prepareLock);
2412*19c3b8c2SApple OSS Distributions 	}
2413*19c3b8c2SApple OSS Distributions 
2414*19c3b8c2SApple OSS Distributions 	super::free();
2415*19c3b8c2SApple OSS Distributions }
2416*19c3b8c2SApple OSS Distributions 
2417*19c3b8c2SApple OSS Distributions #ifndef __LP64__
2418*19c3b8c2SApple OSS Distributions void
unmapFromKernel()2419*19c3b8c2SApple OSS Distributions IOGeneralMemoryDescriptor::unmapFromKernel()
2420*19c3b8c2SApple OSS Distributions {
2421*19c3b8c2SApple OSS Distributions 	panic("IOGMD::unmapFromKernel deprecated");
2422*19c3b8c2SApple OSS Distributions }
2423*19c3b8c2SApple OSS Distributions 
2424*19c3b8c2SApple OSS Distributions void
mapIntoKernel(unsigned rangeIndex)2425*19c3b8c2SApple OSS Distributions IOGeneralMemoryDescriptor::mapIntoKernel(unsigned rangeIndex)
2426*19c3b8c2SApple OSS Distributions {
2427*19c3b8c2SApple OSS Distributions 	panic("IOGMD::mapIntoKernel deprecated");
2428*19c3b8c2SApple OSS Distributions }
2429*19c3b8c2SApple OSS Distributions #endif /* !__LP64__ */
2430*19c3b8c2SApple OSS Distributions 
2431*19c3b8c2SApple OSS Distributions /*
2432*19c3b8c2SApple OSS Distributions  * getDirection:
2433*19c3b8c2SApple OSS Distributions  *
2434*19c3b8c2SApple OSS Distributions  * Get the direction of the transfer.
2435*19c3b8c2SApple OSS Distributions  */
2436*19c3b8c2SApple OSS Distributions IODirection
getDirection() const2437*19c3b8c2SApple OSS Distributions IOMemoryDescriptor::getDirection() const
2438*19c3b8c2SApple OSS Distributions {
2439*19c3b8c2SApple OSS Distributions #ifndef __LP64__
2440*19c3b8c2SApple OSS Distributions 	if (_direction) {
2441*19c3b8c2SApple OSS Distributions 		return _direction;
2442*19c3b8c2SApple OSS Distributions 	}
2443*19c3b8c2SApple OSS Distributions #endif /* !__LP64__ */
2444*19c3b8c2SApple OSS Distributions 	return (IODirection) (_flags & kIOMemoryDirectionMask);
2445*19c3b8c2SApple OSS Distributions }
2446*19c3b8c2SApple OSS Distributions 
2447*19c3b8c2SApple OSS Distributions /*
2448*19c3b8c2SApple OSS Distributions  * getLength:
2449*19c3b8c2SApple OSS Distributions  *
2450*19c3b8c2SApple OSS Distributions  * Get the length of the transfer (over all ranges).
2451*19c3b8c2SApple OSS Distributions  */
2452*19c3b8c2SApple OSS Distributions IOByteCount
getLength() const2453*19c3b8c2SApple OSS Distributions IOMemoryDescriptor::getLength() const
2454*19c3b8c2SApple OSS Distributions {
2455*19c3b8c2SApple OSS Distributions 	return _length;
2456*19c3b8c2SApple OSS Distributions }
2457*19c3b8c2SApple OSS Distributions 
2458*19c3b8c2SApple OSS Distributions void
setTag(IOOptionBits tag)2459*19c3b8c2SApple OSS Distributions IOMemoryDescriptor::setTag( IOOptionBits tag )
2460*19c3b8c2SApple OSS Distributions {
2461*19c3b8c2SApple OSS Distributions 	_tag = tag;
2462*19c3b8c2SApple OSS Distributions }
2463*19c3b8c2SApple OSS Distributions 
2464*19c3b8c2SApple OSS Distributions IOOptionBits
getTag(void)2465*19c3b8c2SApple OSS Distributions IOMemoryDescriptor::getTag( void )
2466*19c3b8c2SApple OSS Distributions {
2467*19c3b8c2SApple OSS Distributions 	return _tag;
2468*19c3b8c2SApple OSS Distributions }
2469*19c3b8c2SApple OSS Distributions 
2470*19c3b8c2SApple OSS Distributions uint64_t
getFlags(void)2471*19c3b8c2SApple OSS Distributions IOMemoryDescriptor::getFlags(void)
2472*19c3b8c2SApple OSS Distributions {
2473*19c3b8c2SApple OSS Distributions 	return _flags;
2474*19c3b8c2SApple OSS Distributions }
2475*19c3b8c2SApple OSS Distributions 
2476*19c3b8c2SApple OSS Distributions OSObject *
copyContext(void) const2477*19c3b8c2SApple OSS Distributions IOMemoryDescriptor::copyContext(void) const
2478*19c3b8c2SApple OSS Distributions {
2479*19c3b8c2SApple OSS Distributions 	if (reserved) {
2480*19c3b8c2SApple OSS Distributions 		OSObject * context = reserved->contextObject;
2481*19c3b8c2SApple OSS Distributions 		if (context) {
2482*19c3b8c2SApple OSS Distributions 			context->retain();
2483*19c3b8c2SApple OSS Distributions 		}
2484*19c3b8c2SApple OSS Distributions 		return context;
2485*19c3b8c2SApple OSS Distributions 	} else {
2486*19c3b8c2SApple OSS Distributions 		return NULL;
2487*19c3b8c2SApple OSS Distributions 	}
2488*19c3b8c2SApple OSS Distributions }
2489*19c3b8c2SApple OSS Distributions 
2490*19c3b8c2SApple OSS Distributions void
setContext(OSObject * obj)2491*19c3b8c2SApple OSS Distributions IOMemoryDescriptor::setContext(OSObject * obj)
2492*19c3b8c2SApple OSS Distributions {
2493*19c3b8c2SApple OSS Distributions 	if (this->reserved == NULL && obj == NULL) {
2494*19c3b8c2SApple OSS Distributions 		// No existing object, and no object to set
2495*19c3b8c2SApple OSS Distributions 		return;
2496*19c3b8c2SApple OSS Distributions 	}
2497*19c3b8c2SApple OSS Distributions 
2498*19c3b8c2SApple OSS Distributions 	IOMemoryDescriptorReserved * reserved = getKernelReserved();
2499*19c3b8c2SApple OSS Distributions 	if (reserved) {
2500*19c3b8c2SApple OSS Distributions 		OSObject * oldObject = reserved->contextObject;
2501*19c3b8c2SApple OSS Distributions 		if (oldObject && OSCompareAndSwapPtr(oldObject, NULL, &reserved->contextObject)) {
2502*19c3b8c2SApple OSS Distributions 			oldObject->release();
2503*19c3b8c2SApple OSS Distributions 		}
2504*19c3b8c2SApple OSS Distributions 		if (obj != NULL) {
2505*19c3b8c2SApple OSS Distributions 			obj->retain();
2506*19c3b8c2SApple OSS Distributions 			reserved->contextObject = obj;
2507*19c3b8c2SApple OSS Distributions 		}
2508*19c3b8c2SApple OSS Distributions 	}
2509*19c3b8c2SApple OSS Distributions }
2510*19c3b8c2SApple OSS Distributions 
2511*19c3b8c2SApple OSS Distributions #ifndef __LP64__
2512*19c3b8c2SApple OSS Distributions #pragma clang diagnostic push
2513*19c3b8c2SApple OSS Distributions #pragma clang diagnostic ignored "-Wdeprecated-declarations"
2514*19c3b8c2SApple OSS Distributions 
2515*19c3b8c2SApple OSS Distributions // @@@ gvdl: who is using this API?  Seems like a wierd thing to implement.
2516*19c3b8c2SApple OSS Distributions IOPhysicalAddress
getSourceSegment(IOByteCount offset,IOByteCount * length)2517*19c3b8c2SApple OSS Distributions IOMemoryDescriptor::getSourceSegment( IOByteCount   offset, IOByteCount * length )
2518*19c3b8c2SApple OSS Distributions {
2519*19c3b8c2SApple OSS Distributions 	addr64_t physAddr = 0;
2520*19c3b8c2SApple OSS Distributions 
2521*19c3b8c2SApple OSS Distributions 	if (prepare() == kIOReturnSuccess) {
2522*19c3b8c2SApple OSS Distributions 		physAddr = getPhysicalSegment64( offset, length );
2523*19c3b8c2SApple OSS Distributions 		complete();
2524*19c3b8c2SApple OSS Distributions 	}
2525*19c3b8c2SApple OSS Distributions 
2526*19c3b8c2SApple OSS Distributions 	return (IOPhysicalAddress) physAddr; // truncated but only page offset is used
2527*19c3b8c2SApple OSS Distributions }
2528*19c3b8c2SApple OSS Distributions 
2529*19c3b8c2SApple OSS Distributions #pragma clang diagnostic pop
2530*19c3b8c2SApple OSS Distributions 
2531*19c3b8c2SApple OSS Distributions #endif /* !__LP64__ */
2532*19c3b8c2SApple OSS Distributions 
2533*19c3b8c2SApple OSS Distributions IOByteCount
readBytes(IOByteCount offset,void * bytes,IOByteCount length)2534*19c3b8c2SApple OSS Distributions IOMemoryDescriptor::readBytes
2535*19c3b8c2SApple OSS Distributions (IOByteCount offset, void *bytes, IOByteCount length)
2536*19c3b8c2SApple OSS Distributions {
2537*19c3b8c2SApple OSS Distributions 	addr64_t dstAddr = CAST_DOWN(addr64_t, bytes);
2538*19c3b8c2SApple OSS Distributions 	IOByteCount endoffset;
2539*19c3b8c2SApple OSS Distributions 	IOByteCount remaining;
2540*19c3b8c2SApple OSS Distributions 
2541*19c3b8c2SApple OSS Distributions 
2542*19c3b8c2SApple OSS Distributions 	// Check that this entire I/O is within the available range
2543*19c3b8c2SApple OSS Distributions 	if ((offset > _length)
2544*19c3b8c2SApple OSS Distributions 	    || os_add_overflow(length, offset, &endoffset)
2545*19c3b8c2SApple OSS Distributions 	    || (endoffset > _length)) {
2546*19c3b8c2SApple OSS Distributions 		assertf(false, "readBytes exceeds length (0x%lx, 0x%lx) > 0x%lx", (long) offset, (long) length, (long) _length);
2547*19c3b8c2SApple OSS Distributions 		return 0;
2548*19c3b8c2SApple OSS Distributions 	}
2549*19c3b8c2SApple OSS Distributions 	if (offset >= _length) {
2550*19c3b8c2SApple OSS Distributions 		return 0;
2551*19c3b8c2SApple OSS Distributions 	}
2552*19c3b8c2SApple OSS Distributions 
2553*19c3b8c2SApple OSS Distributions 	assert(!(kIOMemoryRemote & _flags));
2554*19c3b8c2SApple OSS Distributions 	if (kIOMemoryRemote & _flags) {
2555*19c3b8c2SApple OSS Distributions 		return 0;
2556*19c3b8c2SApple OSS Distributions 	}
2557*19c3b8c2SApple OSS Distributions 
2558*19c3b8c2SApple OSS Distributions 	if (kIOMemoryThreadSafe & _flags) {
2559*19c3b8c2SApple OSS Distributions 		LOCK;
2560*19c3b8c2SApple OSS Distributions 	}
2561*19c3b8c2SApple OSS Distributions 
2562*19c3b8c2SApple OSS Distributions 	remaining = length = min(length, _length - offset);
2563*19c3b8c2SApple OSS Distributions 	while (remaining) { // (process another target segment?)
2564*19c3b8c2SApple OSS Distributions 		addr64_t        srcAddr64;
2565*19c3b8c2SApple OSS Distributions 		IOByteCount     srcLen;
2566*19c3b8c2SApple OSS Distributions 
2567*19c3b8c2SApple OSS Distributions 		srcAddr64 = getPhysicalSegment(offset, &srcLen, kIOMemoryMapperNone);
2568*19c3b8c2SApple OSS Distributions 		if (!srcAddr64) {
2569*19c3b8c2SApple OSS Distributions 			break;
2570*19c3b8c2SApple OSS Distributions 		}
2571*19c3b8c2SApple OSS Distributions 
2572*19c3b8c2SApple OSS Distributions 		// Clip segment length to remaining
2573*19c3b8c2SApple OSS Distributions 		if (srcLen > remaining) {
2574*19c3b8c2SApple OSS Distributions 			srcLen = remaining;
2575*19c3b8c2SApple OSS Distributions 		}
2576*19c3b8c2SApple OSS Distributions 
2577*19c3b8c2SApple OSS Distributions 		if (srcLen > (UINT_MAX - PAGE_SIZE + 1)) {
2578*19c3b8c2SApple OSS Distributions 			srcLen = (UINT_MAX - PAGE_SIZE + 1);
2579*19c3b8c2SApple OSS Distributions 		}
2580*19c3b8c2SApple OSS Distributions 		copypv(srcAddr64, dstAddr, (unsigned int) srcLen,
2581*19c3b8c2SApple OSS Distributions 		    cppvPsrc | cppvNoRefSrc | cppvFsnk | cppvKmap);
2582*19c3b8c2SApple OSS Distributions 
2583*19c3b8c2SApple OSS Distributions 		dstAddr   += srcLen;
2584*19c3b8c2SApple OSS Distributions 		offset    += srcLen;
2585*19c3b8c2SApple OSS Distributions 		remaining -= srcLen;
2586*19c3b8c2SApple OSS Distributions 	}
2587*19c3b8c2SApple OSS Distributions 
2588*19c3b8c2SApple OSS Distributions 	if (kIOMemoryThreadSafe & _flags) {
2589*19c3b8c2SApple OSS Distributions 		UNLOCK;
2590*19c3b8c2SApple OSS Distributions 	}
2591*19c3b8c2SApple OSS Distributions 
2592*19c3b8c2SApple OSS Distributions 	assert(!remaining);
2593*19c3b8c2SApple OSS Distributions 
2594*19c3b8c2SApple OSS Distributions 	return length - remaining;
2595*19c3b8c2SApple OSS Distributions }
2596*19c3b8c2SApple OSS Distributions 
2597*19c3b8c2SApple OSS Distributions IOByteCount
writeBytes(IOByteCount inoffset,const void * bytes,IOByteCount length)2598*19c3b8c2SApple OSS Distributions IOMemoryDescriptor::writeBytes
2599*19c3b8c2SApple OSS Distributions (IOByteCount inoffset, const void *bytes, IOByteCount length)
2600*19c3b8c2SApple OSS Distributions {
2601*19c3b8c2SApple OSS Distributions 	addr64_t srcAddr = CAST_DOWN(addr64_t, bytes);
2602*19c3b8c2SApple OSS Distributions 	IOByteCount remaining;
2603*19c3b8c2SApple OSS Distributions 	IOByteCount endoffset;
2604*19c3b8c2SApple OSS Distributions 	IOByteCount offset = inoffset;
2605*19c3b8c2SApple OSS Distributions 
2606*19c3b8c2SApple OSS Distributions 	assert( !(kIOMemoryPreparedReadOnly & _flags));
2607*19c3b8c2SApple OSS Distributions 
2608*19c3b8c2SApple OSS Distributions 	// Check that this entire I/O is within the available range
2609*19c3b8c2SApple OSS Distributions 	if ((offset > _length)
2610*19c3b8c2SApple OSS Distributions 	    || os_add_overflow(length, offset, &endoffset)
2611*19c3b8c2SApple OSS Distributions 	    || (endoffset > _length)) {
2612*19c3b8c2SApple OSS Distributions 		assertf(false, "writeBytes exceeds length (0x%lx, 0x%lx) > 0x%lx", (long) inoffset, (long) length, (long) _length);
2613*19c3b8c2SApple OSS Distributions 		return 0;
2614*19c3b8c2SApple OSS Distributions 	}
2615*19c3b8c2SApple OSS Distributions 	if (kIOMemoryPreparedReadOnly & _flags) {
2616*19c3b8c2SApple OSS Distributions 		return 0;
2617*19c3b8c2SApple OSS Distributions 	}
2618*19c3b8c2SApple OSS Distributions 	if (offset >= _length) {
2619*19c3b8c2SApple OSS Distributions 		return 0;
2620*19c3b8c2SApple OSS Distributions 	}
2621*19c3b8c2SApple OSS Distributions 
2622*19c3b8c2SApple OSS Distributions 	assert(!(kIOMemoryRemote & _flags));
2623*19c3b8c2SApple OSS Distributions 	if (kIOMemoryRemote & _flags) {
2624*19c3b8c2SApple OSS Distributions 		return 0;
2625*19c3b8c2SApple OSS Distributions 	}
2626*19c3b8c2SApple OSS Distributions 
2627*19c3b8c2SApple OSS Distributions 	if (kIOMemoryThreadSafe & _flags) {
2628*19c3b8c2SApple OSS Distributions 		LOCK;
2629*19c3b8c2SApple OSS Distributions 	}
2630*19c3b8c2SApple OSS Distributions 
2631*19c3b8c2SApple OSS Distributions 	remaining = length = min(length, _length - offset);
2632*19c3b8c2SApple OSS Distributions 	while (remaining) { // (process another target segment?)
2633*19c3b8c2SApple OSS Distributions 		addr64_t    dstAddr64;
2634*19c3b8c2SApple OSS Distributions 		IOByteCount dstLen;
2635*19c3b8c2SApple OSS Distributions 
2636*19c3b8c2SApple OSS Distributions 		dstAddr64 = getPhysicalSegment(offset, &dstLen, kIOMemoryMapperNone);
2637*19c3b8c2SApple OSS Distributions 		if (!dstAddr64) {
2638*19c3b8c2SApple OSS Distributions 			break;
2639*19c3b8c2SApple OSS Distributions 		}
2640*19c3b8c2SApple OSS Distributions 
2641*19c3b8c2SApple OSS Distributions 		// Clip segment length to remaining
2642*19c3b8c2SApple OSS Distributions 		if (dstLen > remaining) {
2643*19c3b8c2SApple OSS Distributions 			dstLen = remaining;
2644*19c3b8c2SApple OSS Distributions 		}
2645*19c3b8c2SApple OSS Distributions 
2646*19c3b8c2SApple OSS Distributions 		if (dstLen > (UINT_MAX - PAGE_SIZE + 1)) {
2647*19c3b8c2SApple OSS Distributions 			dstLen = (UINT_MAX - PAGE_SIZE + 1);
2648*19c3b8c2SApple OSS Distributions 		}
2649*19c3b8c2SApple OSS Distributions 		if (!srcAddr) {
2650*19c3b8c2SApple OSS Distributions 			bzero_phys(dstAddr64, (unsigned int) dstLen);
2651*19c3b8c2SApple OSS Distributions 		} else {
2652*19c3b8c2SApple OSS Distributions 			copypv(srcAddr, (addr64_t) dstAddr64, (unsigned int) dstLen,
2653*19c3b8c2SApple OSS Distributions 			    cppvPsnk | cppvFsnk | cppvNoRefSrc | cppvNoModSnk | cppvKmap);
2654*19c3b8c2SApple OSS Distributions 			srcAddr   += dstLen;
2655*19c3b8c2SApple OSS Distributions 		}
2656*19c3b8c2SApple OSS Distributions 		offset    += dstLen;
2657*19c3b8c2SApple OSS Distributions 		remaining -= dstLen;
2658*19c3b8c2SApple OSS Distributions 	}
2659*19c3b8c2SApple OSS Distributions 
2660*19c3b8c2SApple OSS Distributions 	if (kIOMemoryThreadSafe & _flags) {
2661*19c3b8c2SApple OSS Distributions 		UNLOCK;
2662*19c3b8c2SApple OSS Distributions 	}
2663*19c3b8c2SApple OSS Distributions 
2664*19c3b8c2SApple OSS Distributions 	assert(!remaining);
2665*19c3b8c2SApple OSS Distributions 
2666*19c3b8c2SApple OSS Distributions #if defined(__x86_64__)
2667*19c3b8c2SApple OSS Distributions 	// copypv does not cppvFsnk on intel
2668*19c3b8c2SApple OSS Distributions #else
2669*19c3b8c2SApple OSS Distributions 	if (!srcAddr) {
2670*19c3b8c2SApple OSS Distributions 		performOperation(kIOMemoryIncoherentIOFlush, inoffset, length);
2671*19c3b8c2SApple OSS Distributions 	}
2672*19c3b8c2SApple OSS Distributions #endif
2673*19c3b8c2SApple OSS Distributions 
2674*19c3b8c2SApple OSS Distributions 	return length - remaining;
2675*19c3b8c2SApple OSS Distributions }
2676*19c3b8c2SApple OSS Distributions 
2677*19c3b8c2SApple OSS Distributions #ifndef __LP64__
2678*19c3b8c2SApple OSS Distributions void
setPosition(IOByteCount position)2679*19c3b8c2SApple OSS Distributions IOGeneralMemoryDescriptor::setPosition(IOByteCount position)
2680*19c3b8c2SApple OSS Distributions {
2681*19c3b8c2SApple OSS Distributions 	panic("IOGMD::setPosition deprecated");
2682*19c3b8c2SApple OSS Distributions }
2683*19c3b8c2SApple OSS Distributions #endif /* !__LP64__ */
2684*19c3b8c2SApple OSS Distributions 
2685*19c3b8c2SApple OSS Distributions static volatile SInt64 gIOMDPreparationID __attribute__((aligned(8))) = (1ULL << 32);
2686*19c3b8c2SApple OSS Distributions static volatile SInt64 gIOMDDescriptorID __attribute__((aligned(8))) = (kIODescriptorIDInvalid + 1ULL);
2687*19c3b8c2SApple OSS Distributions 
2688*19c3b8c2SApple OSS Distributions uint64_t
getPreparationID(void)2689*19c3b8c2SApple OSS Distributions IOGeneralMemoryDescriptor::getPreparationID( void )
2690*19c3b8c2SApple OSS Distributions {
2691*19c3b8c2SApple OSS Distributions 	ioGMDData *dataP;
2692*19c3b8c2SApple OSS Distributions 
2693*19c3b8c2SApple OSS Distributions 	if (!_wireCount) {
2694*19c3b8c2SApple OSS Distributions 		return kIOPreparationIDUnprepared;
2695*19c3b8c2SApple OSS Distributions 	}
2696*19c3b8c2SApple OSS Distributions 
2697*19c3b8c2SApple OSS Distributions 	if (((kIOMemoryTypeMask & _flags) == kIOMemoryTypePhysical)
2698*19c3b8c2SApple OSS Distributions 	    || ((kIOMemoryTypeMask & _flags) == kIOMemoryTypePhysical64)) {
2699*19c3b8c2SApple OSS Distributions 		IOMemoryDescriptor::setPreparationID();
2700*19c3b8c2SApple OSS Distributions 		return IOMemoryDescriptor::getPreparationID();
2701*19c3b8c2SApple OSS Distributions 	}
2702*19c3b8c2SApple OSS Distributions 
2703*19c3b8c2SApple OSS Distributions 	if (!_memoryEntries || !(dataP = getDataP(_memoryEntries))) {
2704*19c3b8c2SApple OSS Distributions 		return kIOPreparationIDUnprepared;
2705*19c3b8c2SApple OSS Distributions 	}
2706*19c3b8c2SApple OSS Distributions 
2707*19c3b8c2SApple OSS Distributions 	if (kIOPreparationIDUnprepared == dataP->fPreparationID) {
2708*19c3b8c2SApple OSS Distributions 		SInt64 newID = OSIncrementAtomic64(&gIOMDPreparationID);
2709*19c3b8c2SApple OSS Distributions 		OSCompareAndSwap64(kIOPreparationIDUnprepared, newID, &dataP->fPreparationID);
2710*19c3b8c2SApple OSS Distributions 	}
2711*19c3b8c2SApple OSS Distributions 	return dataP->fPreparationID;
2712*19c3b8c2SApple OSS Distributions }
2713*19c3b8c2SApple OSS Distributions 
2714*19c3b8c2SApple OSS Distributions void
cleanKernelReserved(IOMemoryDescriptorReserved * reserved)2715*19c3b8c2SApple OSS Distributions IOMemoryDescriptor::cleanKernelReserved( IOMemoryDescriptorReserved * reserved )
2716*19c3b8c2SApple OSS Distributions {
2717*19c3b8c2SApple OSS Distributions 	if (reserved->creator) {
2718*19c3b8c2SApple OSS Distributions 		task_deallocate(reserved->creator);
2719*19c3b8c2SApple OSS Distributions 		reserved->creator = NULL;
2720*19c3b8c2SApple OSS Distributions 	}
2721*19c3b8c2SApple OSS Distributions 
2722*19c3b8c2SApple OSS Distributions 	if (reserved->contextObject) {
2723*19c3b8c2SApple OSS Distributions 		reserved->contextObject->release();
2724*19c3b8c2SApple OSS Distributions 		reserved->contextObject = NULL;
2725*19c3b8c2SApple OSS Distributions 	}
2726*19c3b8c2SApple OSS Distributions }
2727*19c3b8c2SApple OSS Distributions 
2728*19c3b8c2SApple OSS Distributions IOMemoryDescriptorReserved *
getKernelReserved(void)2729*19c3b8c2SApple OSS Distributions IOMemoryDescriptor::getKernelReserved( void )
2730*19c3b8c2SApple OSS Distributions {
2731*19c3b8c2SApple OSS Distributions 	if (!reserved) {
2732*19c3b8c2SApple OSS Distributions 		reserved = IOMallocType(IOMemoryDescriptorReserved);
2733*19c3b8c2SApple OSS Distributions 	}
2734*19c3b8c2SApple OSS Distributions 	return reserved;
2735*19c3b8c2SApple OSS Distributions }
2736*19c3b8c2SApple OSS Distributions 
2737*19c3b8c2SApple OSS Distributions void
setPreparationID(void)2738*19c3b8c2SApple OSS Distributions IOMemoryDescriptor::setPreparationID( void )
2739*19c3b8c2SApple OSS Distributions {
2740*19c3b8c2SApple OSS Distributions 	if (getKernelReserved() && (kIOPreparationIDUnprepared == reserved->preparationID)) {
2741*19c3b8c2SApple OSS Distributions 		SInt64 newID = OSIncrementAtomic64(&gIOMDPreparationID);
2742*19c3b8c2SApple OSS Distributions 		OSCompareAndSwap64(kIOPreparationIDUnprepared, newID, &reserved->preparationID);
2743*19c3b8c2SApple OSS Distributions 	}
2744*19c3b8c2SApple OSS Distributions }
2745*19c3b8c2SApple OSS Distributions 
2746*19c3b8c2SApple OSS Distributions uint64_t
getPreparationID(void)2747*19c3b8c2SApple OSS Distributions IOMemoryDescriptor::getPreparationID( void )
2748*19c3b8c2SApple OSS Distributions {
2749*19c3b8c2SApple OSS Distributions 	if (reserved) {
2750*19c3b8c2SApple OSS Distributions 		return reserved->preparationID;
2751*19c3b8c2SApple OSS Distributions 	} else {
2752*19c3b8c2SApple OSS Distributions 		return kIOPreparationIDUnsupported;
2753*19c3b8c2SApple OSS Distributions 	}
2754*19c3b8c2SApple OSS Distributions }
2755*19c3b8c2SApple OSS Distributions 
2756*19c3b8c2SApple OSS Distributions void
setDescriptorID(void)2757*19c3b8c2SApple OSS Distributions IOMemoryDescriptor::setDescriptorID( void )
2758*19c3b8c2SApple OSS Distributions {
2759*19c3b8c2SApple OSS Distributions 	if (getKernelReserved() && (kIODescriptorIDInvalid == reserved->descriptorID)) {
2760*19c3b8c2SApple OSS Distributions 		SInt64 newID = OSIncrementAtomic64(&gIOMDDescriptorID);
2761*19c3b8c2SApple OSS Distributions 		OSCompareAndSwap64(kIODescriptorIDInvalid, newID, &reserved->descriptorID);
2762*19c3b8c2SApple OSS Distributions 	}
2763*19c3b8c2SApple OSS Distributions }
2764*19c3b8c2SApple OSS Distributions 
2765*19c3b8c2SApple OSS Distributions uint64_t
getDescriptorID(void)2766*19c3b8c2SApple OSS Distributions IOMemoryDescriptor::getDescriptorID( void )
2767*19c3b8c2SApple OSS Distributions {
2768*19c3b8c2SApple OSS Distributions 	setDescriptorID();
2769*19c3b8c2SApple OSS Distributions 
2770*19c3b8c2SApple OSS Distributions 	if (reserved) {
2771*19c3b8c2SApple OSS Distributions 		return reserved->descriptorID;
2772*19c3b8c2SApple OSS Distributions 	} else {
2773*19c3b8c2SApple OSS Distributions 		return kIODescriptorIDInvalid;
2774*19c3b8c2SApple OSS Distributions 	}
2775*19c3b8c2SApple OSS Distributions }
2776*19c3b8c2SApple OSS Distributions 
2777*19c3b8c2SApple OSS Distributions IOReturn
ktraceEmitPhysicalSegments(void)2778*19c3b8c2SApple OSS Distributions IOMemoryDescriptor::ktraceEmitPhysicalSegments( void )
2779*19c3b8c2SApple OSS Distributions {
2780*19c3b8c2SApple OSS Distributions 	if (!kdebug_debugid_enabled(IODBG_IOMDPA(IOMDPA_MAPPED))) {
2781*19c3b8c2SApple OSS Distributions 		return kIOReturnSuccess;
2782*19c3b8c2SApple OSS Distributions 	}
2783*19c3b8c2SApple OSS Distributions 
2784*19c3b8c2SApple OSS Distributions 	assert(getPreparationID() >= kIOPreparationIDAlwaysPrepared);
2785*19c3b8c2SApple OSS Distributions 	if (getPreparationID() < kIOPreparationIDAlwaysPrepared) {
2786*19c3b8c2SApple OSS Distributions 		return kIOReturnBadArgument;
2787*19c3b8c2SApple OSS Distributions 	}
2788*19c3b8c2SApple OSS Distributions 
2789*19c3b8c2SApple OSS Distributions 	uint64_t descriptorID = getDescriptorID();
2790*19c3b8c2SApple OSS Distributions 	assert(descriptorID != kIODescriptorIDInvalid);
2791*19c3b8c2SApple OSS Distributions 	if (getDescriptorID() == kIODescriptorIDInvalid) {
2792*19c3b8c2SApple OSS Distributions 		return kIOReturnBadArgument;
2793*19c3b8c2SApple OSS Distributions 	}
2794*19c3b8c2SApple OSS Distributions 
2795*19c3b8c2SApple OSS Distributions 	IOTimeStampConstant(IODBG_IOMDPA(IOMDPA_MAPPED), descriptorID, VM_KERNEL_ADDRHIDE(this), getLength());
2796*19c3b8c2SApple OSS Distributions 
2797*19c3b8c2SApple OSS Distributions #if __LP64__
2798*19c3b8c2SApple OSS Distributions 	static const uint8_t num_segments_page = 8;
2799*19c3b8c2SApple OSS Distributions #else
2800*19c3b8c2SApple OSS Distributions 	static const uint8_t num_segments_page = 4;
2801*19c3b8c2SApple OSS Distributions #endif
2802*19c3b8c2SApple OSS Distributions 	static const uint8_t num_segments_long = 2;
2803*19c3b8c2SApple OSS Distributions 
2804*19c3b8c2SApple OSS Distributions 	IOPhysicalAddress segments_page[num_segments_page];
2805*19c3b8c2SApple OSS Distributions 	IOPhysicalRange   segments_long[num_segments_long];
2806*19c3b8c2SApple OSS Distributions 	memset(segments_page, UINT32_MAX, sizeof(segments_page));
2807*19c3b8c2SApple OSS Distributions 	memset(segments_long, 0, sizeof(segments_long));
2808*19c3b8c2SApple OSS Distributions 
2809*19c3b8c2SApple OSS Distributions 	uint8_t segment_page_idx = 0;
2810*19c3b8c2SApple OSS Distributions 	uint8_t segment_long_idx = 0;
2811*19c3b8c2SApple OSS Distributions 
2812*19c3b8c2SApple OSS Distributions 	IOPhysicalRange physical_segment;
2813*19c3b8c2SApple OSS Distributions 	for (IOByteCount offset = 0; offset < getLength(); offset += physical_segment.length) {
2814*19c3b8c2SApple OSS Distributions 		physical_segment.address = getPhysicalSegment(offset, &physical_segment.length);
2815*19c3b8c2SApple OSS Distributions 
2816*19c3b8c2SApple OSS Distributions 		if (physical_segment.length == 0) {
2817*19c3b8c2SApple OSS Distributions 			break;
2818*19c3b8c2SApple OSS Distributions 		}
2819*19c3b8c2SApple OSS Distributions 
2820*19c3b8c2SApple OSS Distributions 		/**
2821*19c3b8c2SApple OSS Distributions 		 * Most IOMemoryDescriptors are made up of many individual physically discontiguous pages.  To optimize for trace
2822*19c3b8c2SApple OSS Distributions 		 * buffer memory, pack segment events according to the following.
2823*19c3b8c2SApple OSS Distributions 		 *
2824*19c3b8c2SApple OSS Distributions 		 * Mappings must be emitted in ascending order starting from offset 0.  Mappings can be associated with the previous
2825*19c3b8c2SApple OSS Distributions 		 * IOMDPA_MAPPED event emitted on by the current thread_id.
2826*19c3b8c2SApple OSS Distributions 		 *
2827*19c3b8c2SApple OSS Distributions 		 * IOMDPA_SEGMENTS_PAGE        = up to 8 virtually contiguous page aligned mappings of PAGE_SIZE length
2828*19c3b8c2SApple OSS Distributions 		 * - (ppn_0 << 32 | ppn_1), ..., (ppn_6 << 32 | ppn_7)
2829*19c3b8c2SApple OSS Distributions 		 * - unmapped pages will have a ppn of MAX_INT_32
2830*19c3b8c2SApple OSS Distributions 		 * IOMDPA_SEGMENTS_LONG	= up to 2 virtually contiguous mappings of variable length
2831*19c3b8c2SApple OSS Distributions 		 * - address_0, length_0, address_0, length_1
2832*19c3b8c2SApple OSS Distributions 		 * - unmapped pages will have an address of 0
2833*19c3b8c2SApple OSS Distributions 		 *
2834*19c3b8c2SApple OSS Distributions 		 * During each iteration do the following depending on the length of the mapping:
2835*19c3b8c2SApple OSS Distributions 		 * 1. add the current segment to the appropriate queue of pending segments
2836*19c3b8c2SApple OSS Distributions 		 * 1. check if we are operating on the same type of segment (PAGE/LONG) as the previous pass
2837*19c3b8c2SApple OSS Distributions 		 * 1a. if FALSE emit and reset all events in the previous queue
2838*19c3b8c2SApple OSS Distributions 		 * 2. check if we have filled up the current queue of pending events
2839*19c3b8c2SApple OSS Distributions 		 * 2a. if TRUE emit and reset all events in the pending queue
2840*19c3b8c2SApple OSS Distributions 		 * 3. after completing all iterations emit events in the current queue
2841*19c3b8c2SApple OSS Distributions 		 */
2842*19c3b8c2SApple OSS Distributions 
2843*19c3b8c2SApple OSS Distributions 		bool emit_page = false;
2844*19c3b8c2SApple OSS Distributions 		bool emit_long = false;
2845*19c3b8c2SApple OSS Distributions 		if ((physical_segment.address & PAGE_MASK) == 0 && physical_segment.length == PAGE_SIZE) {
2846*19c3b8c2SApple OSS Distributions 			segments_page[segment_page_idx] = physical_segment.address;
2847*19c3b8c2SApple OSS Distributions 			segment_page_idx++;
2848*19c3b8c2SApple OSS Distributions 
2849*19c3b8c2SApple OSS Distributions 			emit_long = segment_long_idx != 0;
2850*19c3b8c2SApple OSS Distributions 			emit_page = segment_page_idx == num_segments_page;
2851*19c3b8c2SApple OSS Distributions 
2852*19c3b8c2SApple OSS Distributions 			if (os_unlikely(emit_long)) {
2853*19c3b8c2SApple OSS Distributions 				IOTimeStampConstant(IODBG_IOMDPA(IOMDPA_SEGMENTS_LONG),
2854*19c3b8c2SApple OSS Distributions 				    segments_long[0].address, segments_long[0].length,
2855*19c3b8c2SApple OSS Distributions 				    segments_long[1].address, segments_long[1].length);
2856*19c3b8c2SApple OSS Distributions 			}
2857*19c3b8c2SApple OSS Distributions 
2858*19c3b8c2SApple OSS Distributions 			if (os_unlikely(emit_page)) {
2859*19c3b8c2SApple OSS Distributions #if __LP64__
2860*19c3b8c2SApple OSS Distributions 				IOTimeStampConstant(IODBG_IOMDPA(IOMDPA_SEGMENTS_PAGE),
2861*19c3b8c2SApple OSS Distributions 				    ((uintptr_t) atop_64(segments_page[0]) << 32) | (ppnum_t) atop_64(segments_page[1]),
2862*19c3b8c2SApple OSS Distributions 				    ((uintptr_t) atop_64(segments_page[2]) << 32) | (ppnum_t) atop_64(segments_page[3]),
2863*19c3b8c2SApple OSS Distributions 				    ((uintptr_t) atop_64(segments_page[4]) << 32) | (ppnum_t) atop_64(segments_page[5]),
2864*19c3b8c2SApple OSS Distributions 				    ((uintptr_t) atop_64(segments_page[6]) << 32) | (ppnum_t) atop_64(segments_page[7]));
2865*19c3b8c2SApple OSS Distributions #else
2866*19c3b8c2SApple OSS Distributions 				IOTimeStampConstant(IODBG_IOMDPA(IOMDPA_SEGMENTS_PAGE),
2867*19c3b8c2SApple OSS Distributions 				    (ppnum_t) atop_32(segments_page[1]),
2868*19c3b8c2SApple OSS Distributions 				    (ppnum_t) atop_32(segments_page[2]),
2869*19c3b8c2SApple OSS Distributions 				    (ppnum_t) atop_32(segments_page[3]),
2870*19c3b8c2SApple OSS Distributions 				    (ppnum_t) atop_32(segments_page[4]));
2871*19c3b8c2SApple OSS Distributions #endif
2872*19c3b8c2SApple OSS Distributions 			}
2873*19c3b8c2SApple OSS Distributions 		} else {
2874*19c3b8c2SApple OSS Distributions 			segments_long[segment_long_idx] = physical_segment;
2875*19c3b8c2SApple OSS Distributions 			segment_long_idx++;
2876*19c3b8c2SApple OSS Distributions 
2877*19c3b8c2SApple OSS Distributions 			emit_page = segment_page_idx != 0;
2878*19c3b8c2SApple OSS Distributions 			emit_long = segment_long_idx == num_segments_long;
2879*19c3b8c2SApple OSS Distributions 
2880*19c3b8c2SApple OSS Distributions 			if (os_unlikely(emit_page)) {
2881*19c3b8c2SApple OSS Distributions #if __LP64__
2882*19c3b8c2SApple OSS Distributions 				IOTimeStampConstant(IODBG_IOMDPA(IOMDPA_SEGMENTS_PAGE),
2883*19c3b8c2SApple OSS Distributions 				    ((uintptr_t) atop_64(segments_page[0]) << 32) | (ppnum_t) atop_64(segments_page[1]),
2884*19c3b8c2SApple OSS Distributions 				    ((uintptr_t) atop_64(segments_page[2]) << 32) | (ppnum_t) atop_64(segments_page[3]),
2885*19c3b8c2SApple OSS Distributions 				    ((uintptr_t) atop_64(segments_page[4]) << 32) | (ppnum_t) atop_64(segments_page[5]),
2886*19c3b8c2SApple OSS Distributions 				    ((uintptr_t) atop_64(segments_page[6]) << 32) | (ppnum_t) atop_64(segments_page[7]));
2887*19c3b8c2SApple OSS Distributions #else
2888*19c3b8c2SApple OSS Distributions 				IOTimeStampConstant(IODBG_IOMDPA(IOMDPA_SEGMENTS_PAGE),
2889*19c3b8c2SApple OSS Distributions 				    (ppnum_t) atop_32(segments_page[1]),
2890*19c3b8c2SApple OSS Distributions 				    (ppnum_t) atop_32(segments_page[2]),
2891*19c3b8c2SApple OSS Distributions 				    (ppnum_t) atop_32(segments_page[3]),
2892*19c3b8c2SApple OSS Distributions 				    (ppnum_t) atop_32(segments_page[4]));
2893*19c3b8c2SApple OSS Distributions #endif
2894*19c3b8c2SApple OSS Distributions 			}
2895*19c3b8c2SApple OSS Distributions 
2896*19c3b8c2SApple OSS Distributions 			if (emit_long) {
2897*19c3b8c2SApple OSS Distributions 				IOTimeStampConstant(IODBG_IOMDPA(IOMDPA_SEGMENTS_LONG),
2898*19c3b8c2SApple OSS Distributions 				    segments_long[0].address, segments_long[0].length,
2899*19c3b8c2SApple OSS Distributions 				    segments_long[1].address, segments_long[1].length);
2900*19c3b8c2SApple OSS Distributions 			}
2901*19c3b8c2SApple OSS Distributions 		}
2902*19c3b8c2SApple OSS Distributions 
2903*19c3b8c2SApple OSS Distributions 		if (os_unlikely(emit_page)) {
2904*19c3b8c2SApple OSS Distributions 			memset(segments_page, UINT32_MAX, sizeof(segments_page));
2905*19c3b8c2SApple OSS Distributions 			segment_page_idx = 0;
2906*19c3b8c2SApple OSS Distributions 		}
2907*19c3b8c2SApple OSS Distributions 
2908*19c3b8c2SApple OSS Distributions 		if (os_unlikely(emit_long)) {
2909*19c3b8c2SApple OSS Distributions 			memset(segments_long, 0, sizeof(segments_long));
2910*19c3b8c2SApple OSS Distributions 			segment_long_idx = 0;
2911*19c3b8c2SApple OSS Distributions 		}
2912*19c3b8c2SApple OSS Distributions 	}
2913*19c3b8c2SApple OSS Distributions 
2914*19c3b8c2SApple OSS Distributions 	if (segment_page_idx != 0) {
2915*19c3b8c2SApple OSS Distributions 		assert(segment_long_idx == 0);
2916*19c3b8c2SApple OSS Distributions #if __LP64__
2917*19c3b8c2SApple OSS Distributions 		IOTimeStampConstant(IODBG_IOMDPA(IOMDPA_SEGMENTS_PAGE),
2918*19c3b8c2SApple OSS Distributions 		    ((uintptr_t) atop_64(segments_page[0]) << 32) | (ppnum_t) atop_64(segments_page[1]),
2919*19c3b8c2SApple OSS Distributions 		    ((uintptr_t) atop_64(segments_page[2]) << 32) | (ppnum_t) atop_64(segments_page[3]),
2920*19c3b8c2SApple OSS Distributions 		    ((uintptr_t) atop_64(segments_page[4]) << 32) | (ppnum_t) atop_64(segments_page[5]),
2921*19c3b8c2SApple OSS Distributions 		    ((uintptr_t) atop_64(segments_page[6]) << 32) | (ppnum_t) atop_64(segments_page[7]));
2922*19c3b8c2SApple OSS Distributions #else
2923*19c3b8c2SApple OSS Distributions 		IOTimeStampConstant(IODBG_IOMDPA(IOMDPA_SEGMENTS_PAGE),
2924*19c3b8c2SApple OSS Distributions 		    (ppnum_t) atop_32(segments_page[1]),
2925*19c3b8c2SApple OSS Distributions 		    (ppnum_t) atop_32(segments_page[2]),
2926*19c3b8c2SApple OSS Distributions 		    (ppnum_t) atop_32(segments_page[3]),
2927*19c3b8c2SApple OSS Distributions 		    (ppnum_t) atop_32(segments_page[4]));
2928*19c3b8c2SApple OSS Distributions #endif
2929*19c3b8c2SApple OSS Distributions 	} else if (segment_long_idx != 0) {
2930*19c3b8c2SApple OSS Distributions 		assert(segment_page_idx == 0);
2931*19c3b8c2SApple OSS Distributions 		IOTimeStampConstant(IODBG_IOMDPA(IOMDPA_SEGMENTS_LONG),
2932*19c3b8c2SApple OSS Distributions 		    segments_long[0].address, segments_long[0].length,
2933*19c3b8c2SApple OSS Distributions 		    segments_long[1].address, segments_long[1].length);
2934*19c3b8c2SApple OSS Distributions 	}
2935*19c3b8c2SApple OSS Distributions 
2936*19c3b8c2SApple OSS Distributions 	return kIOReturnSuccess;
2937*19c3b8c2SApple OSS Distributions }
2938*19c3b8c2SApple OSS Distributions 
2939*19c3b8c2SApple OSS Distributions void
setVMTags(uint32_t kernelTag,uint32_t userTag)2940*19c3b8c2SApple OSS Distributions IOMemoryDescriptor::setVMTags(uint32_t kernelTag, uint32_t userTag)
2941*19c3b8c2SApple OSS Distributions {
2942*19c3b8c2SApple OSS Distributions 	_kernelTag = (vm_tag_t) kernelTag;
2943*19c3b8c2SApple OSS Distributions 	_userTag   = (vm_tag_t) userTag;
2944*19c3b8c2SApple OSS Distributions }
2945*19c3b8c2SApple OSS Distributions 
2946*19c3b8c2SApple OSS Distributions uint32_t
getVMTag(vm_map_t map)2947*19c3b8c2SApple OSS Distributions IOMemoryDescriptor::getVMTag(vm_map_t map)
2948*19c3b8c2SApple OSS Distributions {
2949*19c3b8c2SApple OSS Distributions 	if (vm_kernel_map_is_kernel(map)) {
2950*19c3b8c2SApple OSS Distributions 		if (VM_KERN_MEMORY_NONE != _kernelTag) {
2951*19c3b8c2SApple OSS Distributions 			return (uint32_t) _kernelTag;
2952*19c3b8c2SApple OSS Distributions 		}
2953*19c3b8c2SApple OSS Distributions 	} else {
2954*19c3b8c2SApple OSS Distributions 		if (VM_KERN_MEMORY_NONE != _userTag) {
2955*19c3b8c2SApple OSS Distributions 			return (uint32_t) _userTag;
2956*19c3b8c2SApple OSS Distributions 		}
2957*19c3b8c2SApple OSS Distributions 	}
2958*19c3b8c2SApple OSS Distributions 	return IOMemoryTag(map);
2959*19c3b8c2SApple OSS Distributions }
2960*19c3b8c2SApple OSS Distributions 
2961*19c3b8c2SApple OSS Distributions IOReturn
dmaCommandOperation(DMACommandOps op,void * vData,UInt dataSize) const2962*19c3b8c2SApple OSS Distributions IOGeneralMemoryDescriptor::dmaCommandOperation(DMACommandOps op, void *vData, UInt dataSize) const
2963*19c3b8c2SApple OSS Distributions {
2964*19c3b8c2SApple OSS Distributions 	IOReturn err = kIOReturnSuccess;
2965*19c3b8c2SApple OSS Distributions 	DMACommandOps params;
2966*19c3b8c2SApple OSS Distributions 	IOGeneralMemoryDescriptor * md = const_cast<IOGeneralMemoryDescriptor *>(this);
2967*19c3b8c2SApple OSS Distributions 	ioGMDData *dataP;
2968*19c3b8c2SApple OSS Distributions 
2969*19c3b8c2SApple OSS Distributions 	params = (op & ~kIOMDDMACommandOperationMask & op);
2970*19c3b8c2SApple OSS Distributions 	op &= kIOMDDMACommandOperationMask;
2971*19c3b8c2SApple OSS Distributions 
2972*19c3b8c2SApple OSS Distributions 	if (kIOMDDMAMap == op) {
2973*19c3b8c2SApple OSS Distributions 		if (dataSize < sizeof(IOMDDMAMapArgs)) {
2974*19c3b8c2SApple OSS Distributions 			return kIOReturnUnderrun;
2975*19c3b8c2SApple OSS Distributions 		}
2976*19c3b8c2SApple OSS Distributions 
2977*19c3b8c2SApple OSS Distributions 		IOMDDMAMapArgs * data = (IOMDDMAMapArgs *) vData;
2978*19c3b8c2SApple OSS Distributions 
2979*19c3b8c2SApple OSS Distributions 		if (!_memoryEntries
2980*19c3b8c2SApple OSS Distributions 		    && !md->initMemoryEntries(computeDataSize(0, 0), kIOMapperWaitSystem)) {
2981*19c3b8c2SApple OSS Distributions 			return kIOReturnNoMemory;
2982*19c3b8c2SApple OSS Distributions 		}
2983*19c3b8c2SApple OSS Distributions 
2984*19c3b8c2SApple OSS Distributions 		if (_memoryEntries && data->fMapper) {
2985*19c3b8c2SApple OSS Distributions 			bool remap, keepMap;
2986*19c3b8c2SApple OSS Distributions 			dataP = getDataP(_memoryEntries);
2987*19c3b8c2SApple OSS Distributions 
2988*19c3b8c2SApple OSS Distributions 			if (data->fMapSpec.numAddressBits < dataP->fDMAMapNumAddressBits) {
2989*19c3b8c2SApple OSS Distributions 				dataP->fDMAMapNumAddressBits = data->fMapSpec.numAddressBits;
2990*19c3b8c2SApple OSS Distributions 			}
2991*19c3b8c2SApple OSS Distributions 			if (data->fMapSpec.alignment > dataP->fDMAMapAlignment) {
2992*19c3b8c2SApple OSS Distributions 				dataP->fDMAMapAlignment      = data->fMapSpec.alignment;
2993*19c3b8c2SApple OSS Distributions 			}
2994*19c3b8c2SApple OSS Distributions 
2995*19c3b8c2SApple OSS Distributions 			keepMap = (data->fMapper == gIOSystemMapper);
2996*19c3b8c2SApple OSS Distributions 			keepMap &= ((data->fOffset == 0) && (data->fLength == _length));
2997*19c3b8c2SApple OSS Distributions 
2998*19c3b8c2SApple OSS Distributions 			if ((data->fMapper == gIOSystemMapper) && _prepareLock) {
2999*19c3b8c2SApple OSS Distributions 				IOLockLock(_prepareLock);
3000*19c3b8c2SApple OSS Distributions 			}
3001*19c3b8c2SApple OSS Distributions 
3002*19c3b8c2SApple OSS Distributions 			remap = (!keepMap);
3003*19c3b8c2SApple OSS Distributions 			remap |= (dataP->fDMAMapNumAddressBits < 64)
3004*19c3b8c2SApple OSS Distributions 			    && ((dataP->fMappedBase + _length) > (1ULL << dataP->fDMAMapNumAddressBits));
3005*19c3b8c2SApple OSS Distributions 			remap |= (dataP->fDMAMapAlignment > page_size);
3006*19c3b8c2SApple OSS Distributions 
3007*19c3b8c2SApple OSS Distributions 			if (remap || !dataP->fMappedBaseValid) {
3008*19c3b8c2SApple OSS Distributions 				err = md->dmaMap(data->fMapper, md, data->fCommand, &data->fMapSpec, data->fOffset, data->fLength, &data->fAlloc, &data->fAllocLength);
3009*19c3b8c2SApple OSS Distributions 				if (keepMap && (kIOReturnSuccess == err) && !dataP->fMappedBaseValid) {
3010*19c3b8c2SApple OSS Distributions 					dataP->fMappedBase      = data->fAlloc;
3011*19c3b8c2SApple OSS Distributions 					dataP->fMappedBaseValid = true;
3012*19c3b8c2SApple OSS Distributions 					dataP->fMappedLength    = data->fAllocLength;
3013*19c3b8c2SApple OSS Distributions 					data->fAllocLength      = 0;    // IOMD owns the alloc now
3014*19c3b8c2SApple OSS Distributions 				}
3015*19c3b8c2SApple OSS Distributions 			} else {
3016*19c3b8c2SApple OSS Distributions 				data->fAlloc = dataP->fMappedBase;
3017*19c3b8c2SApple OSS Distributions 				data->fAllocLength = 0;         // give out IOMD map
3018*19c3b8c2SApple OSS Distributions 				md->dmaMapRecord(data->fMapper, data->fCommand, dataP->fMappedLength);
3019*19c3b8c2SApple OSS Distributions 			}
3020*19c3b8c2SApple OSS Distributions 
3021*19c3b8c2SApple OSS Distributions 			if ((data->fMapper == gIOSystemMapper) && _prepareLock) {
3022*19c3b8c2SApple OSS Distributions 				IOLockUnlock(_prepareLock);
3023*19c3b8c2SApple OSS Distributions 			}
3024*19c3b8c2SApple OSS Distributions 		}
3025*19c3b8c2SApple OSS Distributions 		return err;
3026*19c3b8c2SApple OSS Distributions 	}
3027*19c3b8c2SApple OSS Distributions 	if (kIOMDDMAUnmap == op) {
3028*19c3b8c2SApple OSS Distributions 		if (dataSize < sizeof(IOMDDMAMapArgs)) {
3029*19c3b8c2SApple OSS Distributions 			return kIOReturnUnderrun;
3030*19c3b8c2SApple OSS Distributions 		}
3031*19c3b8c2SApple OSS Distributions 		IOMDDMAMapArgs * data = (IOMDDMAMapArgs *) vData;
3032*19c3b8c2SApple OSS Distributions 
3033*19c3b8c2SApple OSS Distributions 		err = md->dmaUnmap(data->fMapper, data->fCommand, data->fOffset, data->fAlloc, data->fAllocLength);
3034*19c3b8c2SApple OSS Distributions 
3035*19c3b8c2SApple OSS Distributions 		return kIOReturnSuccess;
3036*19c3b8c2SApple OSS Distributions 	}
3037*19c3b8c2SApple OSS Distributions 
3038*19c3b8c2SApple OSS Distributions 	if (kIOMDAddDMAMapSpec == op) {
3039*19c3b8c2SApple OSS Distributions 		if (dataSize < sizeof(IODMAMapSpecification)) {
3040*19c3b8c2SApple OSS Distributions 			return kIOReturnUnderrun;
3041*19c3b8c2SApple OSS Distributions 		}
3042*19c3b8c2SApple OSS Distributions 
3043*19c3b8c2SApple OSS Distributions 		IODMAMapSpecification * data = (IODMAMapSpecification *) vData;
3044*19c3b8c2SApple OSS Distributions 
3045*19c3b8c2SApple OSS Distributions 		if (!_memoryEntries
3046*19c3b8c2SApple OSS Distributions 		    && !md->initMemoryEntries(computeDataSize(0, 0), kIOMapperWaitSystem)) {
3047*19c3b8c2SApple OSS Distributions 			return kIOReturnNoMemory;
3048*19c3b8c2SApple OSS Distributions 		}
3049*19c3b8c2SApple OSS Distributions 
3050*19c3b8c2SApple OSS Distributions 		if (_memoryEntries) {
3051*19c3b8c2SApple OSS Distributions 			dataP = getDataP(_memoryEntries);
3052*19c3b8c2SApple OSS Distributions 			if (data->numAddressBits < dataP->fDMAMapNumAddressBits) {
3053*19c3b8c2SApple OSS Distributions 				dataP->fDMAMapNumAddressBits = data->numAddressBits;
3054*19c3b8c2SApple OSS Distributions 			}
3055*19c3b8c2SApple OSS Distributions 			if (data->alignment > dataP->fDMAMapAlignment) {
3056*19c3b8c2SApple OSS Distributions 				dataP->fDMAMapAlignment = data->alignment;
3057*19c3b8c2SApple OSS Distributions 			}
3058*19c3b8c2SApple OSS Distributions 		}
3059*19c3b8c2SApple OSS Distributions 		return kIOReturnSuccess;
3060*19c3b8c2SApple OSS Distributions 	}
3061*19c3b8c2SApple OSS Distributions 
3062*19c3b8c2SApple OSS Distributions 	if (kIOMDGetCharacteristics == op) {
3063*19c3b8c2SApple OSS Distributions 		if (dataSize < sizeof(IOMDDMACharacteristics)) {
3064*19c3b8c2SApple OSS Distributions 			return kIOReturnUnderrun;
3065*19c3b8c2SApple OSS Distributions 		}
3066*19c3b8c2SApple OSS Distributions 
3067*19c3b8c2SApple OSS Distributions 		IOMDDMACharacteristics *data = (IOMDDMACharacteristics *) vData;
3068*19c3b8c2SApple OSS Distributions 		data->fLength = _length;
3069*19c3b8c2SApple OSS Distributions 		data->fSGCount = _rangesCount;
3070*19c3b8c2SApple OSS Distributions 		data->fPages = _pages;
3071*19c3b8c2SApple OSS Distributions 		data->fDirection = getDirection();
3072*19c3b8c2SApple OSS Distributions 		if (!_wireCount) {
3073*19c3b8c2SApple OSS Distributions 			data->fIsPrepared = false;
3074*19c3b8c2SApple OSS Distributions 		} else {
3075*19c3b8c2SApple OSS Distributions 			data->fIsPrepared = true;
3076*19c3b8c2SApple OSS Distributions 			data->fHighestPage = _highestPage;
3077*19c3b8c2SApple OSS Distributions 			if (_memoryEntries) {
3078*19c3b8c2SApple OSS Distributions 				dataP = getDataP(_memoryEntries);
3079*19c3b8c2SApple OSS Distributions 				ioPLBlock *ioplList = getIOPLList(dataP);
3080*19c3b8c2SApple OSS Distributions 				UInt count = getNumIOPL(_memoryEntries, dataP);
3081*19c3b8c2SApple OSS Distributions 				if (count == 1) {
3082*19c3b8c2SApple OSS Distributions 					data->fPageAlign = (ioplList[0].fPageOffset & PAGE_MASK) | ~PAGE_MASK;
3083*19c3b8c2SApple OSS Distributions 				}
3084*19c3b8c2SApple OSS Distributions 			}
3085*19c3b8c2SApple OSS Distributions 		}
3086*19c3b8c2SApple OSS Distributions 
3087*19c3b8c2SApple OSS Distributions 		return kIOReturnSuccess;
3088*19c3b8c2SApple OSS Distributions 	} else if (kIOMDDMAActive == op) {
3089*19c3b8c2SApple OSS Distributions 		if (params) {
3090*19c3b8c2SApple OSS Distributions 			int16_t prior;
3091*19c3b8c2SApple OSS Distributions 			prior = OSAddAtomic16(1, &md->_dmaReferences);
3092*19c3b8c2SApple OSS Distributions 			if (!prior) {
3093*19c3b8c2SApple OSS Distributions 				md->_mapName = NULL;
3094*19c3b8c2SApple OSS Distributions 			}
3095*19c3b8c2SApple OSS Distributions 		} else {
3096*19c3b8c2SApple OSS Distributions 			if (md->_dmaReferences) {
3097*19c3b8c2SApple OSS Distributions 				OSAddAtomic16(-1, &md->_dmaReferences);
3098*19c3b8c2SApple OSS Distributions 			} else {
3099*19c3b8c2SApple OSS Distributions 				panic("_dmaReferences underflow");
3100*19c3b8c2SApple OSS Distributions 			}
3101*19c3b8c2SApple OSS Distributions 		}
3102*19c3b8c2SApple OSS Distributions 	} else if (kIOMDWalkSegments != op) {
3103*19c3b8c2SApple OSS Distributions 		return kIOReturnBadArgument;
3104*19c3b8c2SApple OSS Distributions 	}
3105*19c3b8c2SApple OSS Distributions 
3106*19c3b8c2SApple OSS Distributions 	// Get the next segment
3107*19c3b8c2SApple OSS Distributions 	struct InternalState {
3108*19c3b8c2SApple OSS Distributions 		IOMDDMAWalkSegmentArgs fIO;
3109*19c3b8c2SApple OSS Distributions 		mach_vm_size_t fOffset2Index;
3110*19c3b8c2SApple OSS Distributions 		mach_vm_size_t fNextOffset;
3111*19c3b8c2SApple OSS Distributions 		UInt fIndex;
3112*19c3b8c2SApple OSS Distributions 	} *isP;
3113*19c3b8c2SApple OSS Distributions 
3114*19c3b8c2SApple OSS Distributions 	// Find the next segment
3115*19c3b8c2SApple OSS Distributions 	if (dataSize < sizeof(*isP)) {
3116*19c3b8c2SApple OSS Distributions 		return kIOReturnUnderrun;
3117*19c3b8c2SApple OSS Distributions 	}
3118*19c3b8c2SApple OSS Distributions 
3119*19c3b8c2SApple OSS Distributions 	isP = (InternalState *) vData;
3120*19c3b8c2SApple OSS Distributions 	uint64_t offset = isP->fIO.fOffset;
3121*19c3b8c2SApple OSS Distributions 	uint8_t mapped = isP->fIO.fMapped;
3122*19c3b8c2SApple OSS Distributions 	uint64_t mappedBase;
3123*19c3b8c2SApple OSS Distributions 
3124*19c3b8c2SApple OSS Distributions 	if (mapped && (kIOMemoryRemote & _flags)) {
3125*19c3b8c2SApple OSS Distributions 		return kIOReturnNotAttached;
3126*19c3b8c2SApple OSS Distributions 	}
3127*19c3b8c2SApple OSS Distributions 
3128*19c3b8c2SApple OSS Distributions 	if (IOMapper::gSystem && mapped
3129*19c3b8c2SApple OSS Distributions 	    && (!(kIOMemoryHostOnly & _flags))
3130*19c3b8c2SApple OSS Distributions 	    && (!_memoryEntries || !getDataP(_memoryEntries)->fMappedBaseValid)) {
3131*19c3b8c2SApple OSS Distributions //	&& (_memoryEntries && !getDataP(_memoryEntries)->fMappedBaseValid))
3132*19c3b8c2SApple OSS Distributions 		if (!_memoryEntries
3133*19c3b8c2SApple OSS Distributions 		    && !md->initMemoryEntries(computeDataSize(0, 0), kIOMapperWaitSystem)) {
3134*19c3b8c2SApple OSS Distributions 			return kIOReturnNoMemory;
3135*19c3b8c2SApple OSS Distributions 		}
3136*19c3b8c2SApple OSS Distributions 
3137*19c3b8c2SApple OSS Distributions 		dataP = getDataP(_memoryEntries);
3138*19c3b8c2SApple OSS Distributions 		if (dataP->fMapper) {
3139*19c3b8c2SApple OSS Distributions 			IODMAMapSpecification mapSpec;
3140*19c3b8c2SApple OSS Distributions 			bzero(&mapSpec, sizeof(mapSpec));
3141*19c3b8c2SApple OSS Distributions 			mapSpec.numAddressBits = dataP->fDMAMapNumAddressBits;
3142*19c3b8c2SApple OSS Distributions 			mapSpec.alignment = dataP->fDMAMapAlignment;
3143*19c3b8c2SApple OSS Distributions 			err = md->dmaMap(dataP->fMapper, md, NULL, &mapSpec, 0, _length, &dataP->fMappedBase, &dataP->fMappedLength);
3144*19c3b8c2SApple OSS Distributions 			if (kIOReturnSuccess != err) {
3145*19c3b8c2SApple OSS Distributions 				return err;
3146*19c3b8c2SApple OSS Distributions 			}
3147*19c3b8c2SApple OSS Distributions 			dataP->fMappedBaseValid = true;
3148*19c3b8c2SApple OSS Distributions 		}
3149*19c3b8c2SApple OSS Distributions 	}
3150*19c3b8c2SApple OSS Distributions 
3151*19c3b8c2SApple OSS Distributions 	if (mapped) {
3152*19c3b8c2SApple OSS Distributions 		if (IOMapper::gSystem
3153*19c3b8c2SApple OSS Distributions 		    && (!(kIOMemoryHostOnly & _flags))
3154*19c3b8c2SApple OSS Distributions 		    && _memoryEntries
3155*19c3b8c2SApple OSS Distributions 		    && (dataP = getDataP(_memoryEntries))
3156*19c3b8c2SApple OSS Distributions 		    && dataP->fMappedBaseValid) {
3157*19c3b8c2SApple OSS Distributions 			mappedBase = dataP->fMappedBase;
3158*19c3b8c2SApple OSS Distributions 		} else {
3159*19c3b8c2SApple OSS Distributions 			mapped = 0;
3160*19c3b8c2SApple OSS Distributions 		}
3161*19c3b8c2SApple OSS Distributions 	}
3162*19c3b8c2SApple OSS Distributions 
3163*19c3b8c2SApple OSS Distributions 	if (offset >= _length) {
3164*19c3b8c2SApple OSS Distributions 		return (offset == _length)? kIOReturnOverrun : kIOReturnInternalError;
3165*19c3b8c2SApple OSS Distributions 	}
3166*19c3b8c2SApple OSS Distributions 
3167*19c3b8c2SApple OSS Distributions 	// Validate the previous offset
3168*19c3b8c2SApple OSS Distributions 	UInt ind;
3169*19c3b8c2SApple OSS Distributions 	mach_vm_size_t off2Ind = isP->fOffset2Index;
3170*19c3b8c2SApple OSS Distributions 	if (!params
3171*19c3b8c2SApple OSS Distributions 	    && offset
3172*19c3b8c2SApple OSS Distributions 	    && (offset == isP->fNextOffset || off2Ind <= offset)) {
3173*19c3b8c2SApple OSS Distributions 		ind = isP->fIndex;
3174*19c3b8c2SApple OSS Distributions 	} else {
3175*19c3b8c2SApple OSS Distributions 		ind = off2Ind = 0; // Start from beginning
3176*19c3b8c2SApple OSS Distributions 	}
3177*19c3b8c2SApple OSS Distributions 	mach_vm_size_t length;
3178*19c3b8c2SApple OSS Distributions 	UInt64 address;
3179*19c3b8c2SApple OSS Distributions 
3180*19c3b8c2SApple OSS Distributions 	if ((_flags & kIOMemoryTypeMask) == kIOMemoryTypePhysical) {
3181*19c3b8c2SApple OSS Distributions 		// Physical address based memory descriptor
3182*19c3b8c2SApple OSS Distributions 		const IOPhysicalRange *physP = (IOPhysicalRange *) &_ranges.p[0];
3183*19c3b8c2SApple OSS Distributions 
3184*19c3b8c2SApple OSS Distributions 		// Find the range after the one that contains the offset
3185*19c3b8c2SApple OSS Distributions 		mach_vm_size_t len;
3186*19c3b8c2SApple OSS Distributions 		for (len = 0; off2Ind <= offset; ind++) {
3187*19c3b8c2SApple OSS Distributions 			len = physP[ind].length;
3188*19c3b8c2SApple OSS Distributions 			off2Ind += len;
3189*19c3b8c2SApple OSS Distributions 		}
3190*19c3b8c2SApple OSS Distributions 
3191*19c3b8c2SApple OSS Distributions 		// Calculate length within range and starting address
3192*19c3b8c2SApple OSS Distributions 		length   = off2Ind - offset;
3193*19c3b8c2SApple OSS Distributions 		address  = physP[ind - 1].address + len - length;
3194*19c3b8c2SApple OSS Distributions 
3195*19c3b8c2SApple OSS Distributions 		if (true && mapped) {
3196*19c3b8c2SApple OSS Distributions 			address = mappedBase + offset;
3197*19c3b8c2SApple OSS Distributions 		} else {
3198*19c3b8c2SApple OSS Distributions 			// see how far we can coalesce ranges
3199*19c3b8c2SApple OSS Distributions 			while (ind < _rangesCount && address + length == physP[ind].address) {
3200*19c3b8c2SApple OSS Distributions 				len = physP[ind].length;
3201*19c3b8c2SApple OSS Distributions 				length += len;
3202*19c3b8c2SApple OSS Distributions 				off2Ind += len;
3203*19c3b8c2SApple OSS Distributions 				ind++;
3204*19c3b8c2SApple OSS Distributions 			}
3205*19c3b8c2SApple OSS Distributions 		}
3206*19c3b8c2SApple OSS Distributions 
3207*19c3b8c2SApple OSS Distributions 		// correct contiguous check overshoot
3208*19c3b8c2SApple OSS Distributions 		ind--;
3209*19c3b8c2SApple OSS Distributions 		off2Ind -= len;
3210*19c3b8c2SApple OSS Distributions 	}
3211*19c3b8c2SApple OSS Distributions #ifndef __LP64__
3212*19c3b8c2SApple OSS Distributions 	else if ((_flags & kIOMemoryTypeMask) == kIOMemoryTypePhysical64) {
3213*19c3b8c2SApple OSS Distributions 		// Physical address based memory descriptor
3214*19c3b8c2SApple OSS Distributions 		const IOAddressRange *physP = (IOAddressRange *) &_ranges.v64[0];
3215*19c3b8c2SApple OSS Distributions 
3216*19c3b8c2SApple OSS Distributions 		// Find the range after the one that contains the offset
3217*19c3b8c2SApple OSS Distributions 		mach_vm_size_t len;
3218*19c3b8c2SApple OSS Distributions 		for (len = 0; off2Ind <= offset; ind++) {
3219*19c3b8c2SApple OSS Distributions 			len = physP[ind].length;
3220*19c3b8c2SApple OSS Distributions 			off2Ind += len;
3221*19c3b8c2SApple OSS Distributions 		}
3222*19c3b8c2SApple OSS Distributions 
3223*19c3b8c2SApple OSS Distributions 		// Calculate length within range and starting address
3224*19c3b8c2SApple OSS Distributions 		length   = off2Ind - offset;
3225*19c3b8c2SApple OSS Distributions 		address  = physP[ind - 1].address + len - length;
3226*19c3b8c2SApple OSS Distributions 
3227*19c3b8c2SApple OSS Distributions 		if (true && mapped) {
3228*19c3b8c2SApple OSS Distributions 			address = mappedBase + offset;
3229*19c3b8c2SApple OSS Distributions 		} else {
3230*19c3b8c2SApple OSS Distributions 			// see how far we can coalesce ranges
3231*19c3b8c2SApple OSS Distributions 			while (ind < _rangesCount && address + length == physP[ind].address) {
3232*19c3b8c2SApple OSS Distributions 				len = physP[ind].length;
3233*19c3b8c2SApple OSS Distributions 				length += len;
3234*19c3b8c2SApple OSS Distributions 				off2Ind += len;
3235*19c3b8c2SApple OSS Distributions 				ind++;
3236*19c3b8c2SApple OSS Distributions 			}
3237*19c3b8c2SApple OSS Distributions 		}
3238*19c3b8c2SApple OSS Distributions 		// correct contiguous check overshoot
3239*19c3b8c2SApple OSS Distributions 		ind--;
3240*19c3b8c2SApple OSS Distributions 		off2Ind -= len;
3241*19c3b8c2SApple OSS Distributions 	}
3242*19c3b8c2SApple OSS Distributions #endif /* !__LP64__ */
3243*19c3b8c2SApple OSS Distributions 	else {
3244*19c3b8c2SApple OSS Distributions 		do {
3245*19c3b8c2SApple OSS Distributions 			if (!_wireCount) {
3246*19c3b8c2SApple OSS Distributions 				panic("IOGMD: not wired for the IODMACommand");
3247*19c3b8c2SApple OSS Distributions 			}
3248*19c3b8c2SApple OSS Distributions 
3249*19c3b8c2SApple OSS Distributions 			assert(_memoryEntries);
3250*19c3b8c2SApple OSS Distributions 
3251*19c3b8c2SApple OSS Distributions 			dataP = getDataP(_memoryEntries);
3252*19c3b8c2SApple OSS Distributions 			const ioPLBlock *ioplList = getIOPLList(dataP);
3253*19c3b8c2SApple OSS Distributions 			UInt numIOPLs = getNumIOPL(_memoryEntries, dataP);
3254*19c3b8c2SApple OSS Distributions 			upl_page_info_t *pageList = getPageList(dataP);
3255*19c3b8c2SApple OSS Distributions 
3256*19c3b8c2SApple OSS Distributions 			assert(numIOPLs > 0);
3257*19c3b8c2SApple OSS Distributions 
3258*19c3b8c2SApple OSS Distributions 			// Scan through iopl info blocks looking for block containing offset
3259*19c3b8c2SApple OSS Distributions 			while (ind < numIOPLs && offset >= ioplList[ind].fIOMDOffset) {
3260*19c3b8c2SApple OSS Distributions 				ind++;
3261*19c3b8c2SApple OSS Distributions 			}
3262*19c3b8c2SApple OSS Distributions 
3263*19c3b8c2SApple OSS Distributions 			// Go back to actual range as search goes past it
3264*19c3b8c2SApple OSS Distributions 			ioPLBlock ioplInfo = ioplList[ind - 1];
3265*19c3b8c2SApple OSS Distributions 			off2Ind = ioplInfo.fIOMDOffset;
3266*19c3b8c2SApple OSS Distributions 
3267*19c3b8c2SApple OSS Distributions 			if (ind < numIOPLs) {
3268*19c3b8c2SApple OSS Distributions 				length = ioplList[ind].fIOMDOffset;
3269*19c3b8c2SApple OSS Distributions 			} else {
3270*19c3b8c2SApple OSS Distributions 				length = _length;
3271*19c3b8c2SApple OSS Distributions 			}
3272*19c3b8c2SApple OSS Distributions 			length -= offset;       // Remainder within iopl
3273*19c3b8c2SApple OSS Distributions 
3274*19c3b8c2SApple OSS Distributions 			// Subtract offset till this iopl in total list
3275*19c3b8c2SApple OSS Distributions 			offset -= off2Ind;
3276*19c3b8c2SApple OSS Distributions 
3277*19c3b8c2SApple OSS Distributions 			// If a mapped address is requested and this is a pre-mapped IOPL
3278*19c3b8c2SApple OSS Distributions 			// then just need to compute an offset relative to the mapped base.
3279*19c3b8c2SApple OSS Distributions 			if (mapped) {
3280*19c3b8c2SApple OSS Distributions 				offset += (ioplInfo.fPageOffset & PAGE_MASK);
3281*19c3b8c2SApple OSS Distributions 				address = trunc_page_64(mappedBase) + ptoa_64(ioplInfo.fMappedPage) + offset;
3282*19c3b8c2SApple OSS Distributions 				continue; // Done leave do/while(false) now
3283*19c3b8c2SApple OSS Distributions 			}
3284*19c3b8c2SApple OSS Distributions 
3285*19c3b8c2SApple OSS Distributions 			// The offset is rebased into the current iopl.
3286*19c3b8c2SApple OSS Distributions 			// Now add the iopl 1st page offset.
3287*19c3b8c2SApple OSS Distributions 			offset += ioplInfo.fPageOffset;
3288*19c3b8c2SApple OSS Distributions 
3289*19c3b8c2SApple OSS Distributions 			// For external UPLs the fPageInfo field points directly to
3290*19c3b8c2SApple OSS Distributions 			// the upl's upl_page_info_t array.
3291*19c3b8c2SApple OSS Distributions 			if (ioplInfo.fFlags & kIOPLExternUPL) {
3292*19c3b8c2SApple OSS Distributions 				pageList = (upl_page_info_t *) ioplInfo.fPageInfo;
3293*19c3b8c2SApple OSS Distributions 			} else {
3294*19c3b8c2SApple OSS Distributions 				pageList = &pageList[ioplInfo.fPageInfo];
3295*19c3b8c2SApple OSS Distributions 			}
3296*19c3b8c2SApple OSS Distributions 
3297*19c3b8c2SApple OSS Distributions 			// Check for direct device non-paged memory
3298*19c3b8c2SApple OSS Distributions 			if (ioplInfo.fFlags & kIOPLOnDevice) {
3299*19c3b8c2SApple OSS Distributions 				address = ptoa_64(pageList->phys_addr) + offset;
3300*19c3b8c2SApple OSS Distributions 				continue; // Done leave do/while(false) now
3301*19c3b8c2SApple OSS Distributions 			}
3302*19c3b8c2SApple OSS Distributions 
3303*19c3b8c2SApple OSS Distributions 			// Now we need compute the index into the pageList
3304*19c3b8c2SApple OSS Distributions 			UInt pageInd = atop_32(offset);
3305*19c3b8c2SApple OSS Distributions 			offset &= PAGE_MASK;
3306*19c3b8c2SApple OSS Distributions 
3307*19c3b8c2SApple OSS Distributions 			// Compute the starting address of this segment
3308*19c3b8c2SApple OSS Distributions 			IOPhysicalAddress pageAddr = pageList[pageInd].phys_addr;
3309*19c3b8c2SApple OSS Distributions 			if (!pageAddr) {
3310*19c3b8c2SApple OSS Distributions 				panic("!pageList phys_addr");
3311*19c3b8c2SApple OSS Distributions 			}
3312*19c3b8c2SApple OSS Distributions 
3313*19c3b8c2SApple OSS Distributions 			address = ptoa_64(pageAddr) + offset;
3314*19c3b8c2SApple OSS Distributions 
3315*19c3b8c2SApple OSS Distributions 			// length is currently set to the length of the remainider of the iopl.
3316*19c3b8c2SApple OSS Distributions 			// We need to check that the remainder of the iopl is contiguous.
3317*19c3b8c2SApple OSS Distributions 			// This is indicated by pageList[ind].phys_addr being sequential.
3318*19c3b8c2SApple OSS Distributions 			IOByteCount contigLength = PAGE_SIZE - offset;
3319*19c3b8c2SApple OSS Distributions 			while (contigLength < length
3320*19c3b8c2SApple OSS Distributions 			    && ++pageAddr == pageList[++pageInd].phys_addr) {
3321*19c3b8c2SApple OSS Distributions 				contigLength += PAGE_SIZE;
3322*19c3b8c2SApple OSS Distributions 			}
3323*19c3b8c2SApple OSS Distributions 
3324*19c3b8c2SApple OSS Distributions 			if (contigLength < length) {
3325*19c3b8c2SApple OSS Distributions 				length = contigLength;
3326*19c3b8c2SApple OSS Distributions 			}
3327*19c3b8c2SApple OSS Distributions 
3328*19c3b8c2SApple OSS Distributions 
3329*19c3b8c2SApple OSS Distributions 			assert(address);
3330*19c3b8c2SApple OSS Distributions 			assert(length);
3331*19c3b8c2SApple OSS Distributions 		} while (false);
3332*19c3b8c2SApple OSS Distributions 	}
3333*19c3b8c2SApple OSS Distributions 
3334*19c3b8c2SApple OSS Distributions 	// Update return values and state
3335*19c3b8c2SApple OSS Distributions 	isP->fIO.fIOVMAddr = address;
3336*19c3b8c2SApple OSS Distributions 	isP->fIO.fLength   = length;
3337*19c3b8c2SApple OSS Distributions 	isP->fIndex        = ind;
3338*19c3b8c2SApple OSS Distributions 	isP->fOffset2Index = off2Ind;
3339*19c3b8c2SApple OSS Distributions 	isP->fNextOffset   = isP->fIO.fOffset + length;
3340*19c3b8c2SApple OSS Distributions 
3341*19c3b8c2SApple OSS Distributions 	return kIOReturnSuccess;
3342*19c3b8c2SApple OSS Distributions }
3343*19c3b8c2SApple OSS Distributions 
3344*19c3b8c2SApple OSS Distributions addr64_t
getPhysicalSegment(IOByteCount offset,IOByteCount * lengthOfSegment,IOOptionBits options)3345*19c3b8c2SApple OSS Distributions IOGeneralMemoryDescriptor::getPhysicalSegment(IOByteCount offset, IOByteCount *lengthOfSegment, IOOptionBits options)
3346*19c3b8c2SApple OSS Distributions {
3347*19c3b8c2SApple OSS Distributions 	IOReturn          ret;
3348*19c3b8c2SApple OSS Distributions 	mach_vm_address_t address = 0;
3349*19c3b8c2SApple OSS Distributions 	mach_vm_size_t    length  = 0;
3350*19c3b8c2SApple OSS Distributions 	IOMapper *        mapper  = gIOSystemMapper;
3351*19c3b8c2SApple OSS Distributions 	IOOptionBits      type    = _flags & kIOMemoryTypeMask;
3352*19c3b8c2SApple OSS Distributions 
3353*19c3b8c2SApple OSS Distributions 	if (lengthOfSegment) {
3354*19c3b8c2SApple OSS Distributions 		*lengthOfSegment = 0;
3355*19c3b8c2SApple OSS Distributions 	}
3356*19c3b8c2SApple OSS Distributions 
3357*19c3b8c2SApple OSS Distributions 	if (offset >= _length) {
3358*19c3b8c2SApple OSS Distributions 		return 0;
3359*19c3b8c2SApple OSS Distributions 	}
3360*19c3b8c2SApple OSS Distributions 
3361*19c3b8c2SApple OSS Distributions 	// IOMemoryDescriptor::doMap() cannot use getPhysicalSegment() to obtain the page offset, since it must
3362*19c3b8c2SApple OSS Distributions 	// support the unwired memory case in IOGeneralMemoryDescriptor, and hibernate_write_image() cannot use
3363*19c3b8c2SApple OSS Distributions 	// map()->getVirtualAddress() to obtain the kernel pointer, since it must prevent the memory allocation
3364*19c3b8c2SApple OSS Distributions 	// due to IOMemoryMap, so _kIOMemorySourceSegment is a necessary evil until all of this gets cleaned up
3365*19c3b8c2SApple OSS Distributions 
3366*19c3b8c2SApple OSS Distributions 	if ((options & _kIOMemorySourceSegment) && (kIOMemoryTypeUPL != type)) {
3367*19c3b8c2SApple OSS Distributions 		unsigned rangesIndex = 0;
3368*19c3b8c2SApple OSS Distributions 		Ranges vec = _ranges;
3369*19c3b8c2SApple OSS Distributions 		mach_vm_address_t addr;
3370*19c3b8c2SApple OSS Distributions 
3371*19c3b8c2SApple OSS Distributions 		// Find starting address within the vector of ranges
3372*19c3b8c2SApple OSS Distributions 		for (;;) {
3373*19c3b8c2SApple OSS Distributions 			getAddrLenForInd(addr, length, type, vec, rangesIndex);
3374*19c3b8c2SApple OSS Distributions 			if (offset < length) {
3375*19c3b8c2SApple OSS Distributions 				break;
3376*19c3b8c2SApple OSS Distributions 			}
3377*19c3b8c2SApple OSS Distributions 			offset -= length; // (make offset relative)
3378*19c3b8c2SApple OSS Distributions 			rangesIndex++;
3379*19c3b8c2SApple OSS Distributions 		}
3380*19c3b8c2SApple OSS Distributions 
3381*19c3b8c2SApple OSS Distributions 		// Now that we have the starting range,
3382*19c3b8c2SApple OSS Distributions 		// lets find the last contiguous range
3383*19c3b8c2SApple OSS Distributions 		addr   += offset;
3384*19c3b8c2SApple OSS Distributions 		length -= offset;
3385*19c3b8c2SApple OSS Distributions 
3386*19c3b8c2SApple OSS Distributions 		for (++rangesIndex; rangesIndex < _rangesCount; rangesIndex++) {
3387*19c3b8c2SApple OSS Distributions 			mach_vm_address_t newAddr;
3388*19c3b8c2SApple OSS Distributions 			mach_vm_size_t    newLen;
3389*19c3b8c2SApple OSS Distributions 
3390*19c3b8c2SApple OSS Distributions 			getAddrLenForInd(newAddr, newLen, type, vec, rangesIndex);
3391*19c3b8c2SApple OSS Distributions 			if (addr + length != newAddr) {
3392*19c3b8c2SApple OSS Distributions 				break;
3393*19c3b8c2SApple OSS Distributions 			}
3394*19c3b8c2SApple OSS Distributions 			length += newLen;
3395*19c3b8c2SApple OSS Distributions 		}
3396*19c3b8c2SApple OSS Distributions 		if (addr) {
3397*19c3b8c2SApple OSS Distributions 			address = (IOPhysicalAddress) addr; // Truncate address to 32bit
3398*19c3b8c2SApple OSS Distributions 		}
3399*19c3b8c2SApple OSS Distributions 	} else {
3400*19c3b8c2SApple OSS Distributions 		IOMDDMAWalkSegmentState _state;
3401*19c3b8c2SApple OSS Distributions 		IOMDDMAWalkSegmentArgs * state = (IOMDDMAWalkSegmentArgs *) (void *)&_state;
3402*19c3b8c2SApple OSS Distributions 
3403*19c3b8c2SApple OSS Distributions 		state->fOffset = offset;
3404*19c3b8c2SApple OSS Distributions 		state->fLength = _length - offset;
3405*19c3b8c2SApple OSS Distributions 		state->fMapped = (0 == (options & kIOMemoryMapperNone)) && !(_flags & kIOMemoryHostOrRemote);
3406*19c3b8c2SApple OSS Distributions 
3407*19c3b8c2SApple OSS Distributions 		ret = dmaCommandOperation(kIOMDFirstSegment, _state, sizeof(_state));
3408*19c3b8c2SApple OSS Distributions 
3409*19c3b8c2SApple OSS Distributions 		if ((kIOReturnSuccess != ret) && (kIOReturnOverrun != ret)) {
3410*19c3b8c2SApple OSS Distributions 			DEBG("getPhysicalSegment dmaCommandOperation(%lx), %p, offset %qx, addr %qx, len %qx\n",
3411*19c3b8c2SApple OSS Distributions 			    ret, this, state->fOffset,
3412*19c3b8c2SApple OSS Distributions 			    state->fIOVMAddr, state->fLength);
3413*19c3b8c2SApple OSS Distributions 		}
3414*19c3b8c2SApple OSS Distributions 		if (kIOReturnSuccess == ret) {
3415*19c3b8c2SApple OSS Distributions 			address = state->fIOVMAddr;
3416*19c3b8c2SApple OSS Distributions 			length  = state->fLength;
3417*19c3b8c2SApple OSS Distributions 		}
3418*19c3b8c2SApple OSS Distributions 
3419*19c3b8c2SApple OSS Distributions 		// dmaCommandOperation() does not distinguish between "mapped" and "unmapped" physical memory, even
3420*19c3b8c2SApple OSS Distributions 		// with fMapped set correctly, so we must handle the transformation here until this gets cleaned up
3421*19c3b8c2SApple OSS Distributions 
3422*19c3b8c2SApple OSS Distributions 		if (mapper && ((kIOMemoryTypePhysical == type) || (kIOMemoryTypePhysical64 == type))) {
3423*19c3b8c2SApple OSS Distributions 			if ((options & kIOMemoryMapperNone) && !(_flags & kIOMemoryMapperNone)) {
3424*19c3b8c2SApple OSS Distributions 				addr64_t    origAddr = address;
3425*19c3b8c2SApple OSS Distributions 				IOByteCount origLen  = length;
3426*19c3b8c2SApple OSS Distributions 
3427*19c3b8c2SApple OSS Distributions 				address = mapper->mapToPhysicalAddress(origAddr);
3428*19c3b8c2SApple OSS Distributions 				length = page_size - (address & (page_size - 1));
3429*19c3b8c2SApple OSS Distributions 				while ((length < origLen)
3430*19c3b8c2SApple OSS Distributions 				    && ((address + length) == mapper->mapToPhysicalAddress(origAddr + length))) {
3431*19c3b8c2SApple OSS Distributions 					length += page_size;
3432*19c3b8c2SApple OSS Distributions 				}
3433*19c3b8c2SApple OSS Distributions 				if (length > origLen) {
3434*19c3b8c2SApple OSS Distributions 					length = origLen;
3435*19c3b8c2SApple OSS Distributions 				}
3436*19c3b8c2SApple OSS Distributions 			}
3437*19c3b8c2SApple OSS Distributions 		}
3438*19c3b8c2SApple OSS Distributions 	}
3439*19c3b8c2SApple OSS Distributions 
3440*19c3b8c2SApple OSS Distributions 	if (!address) {
3441*19c3b8c2SApple OSS Distributions 		length = 0;
3442*19c3b8c2SApple OSS Distributions 	}
3443*19c3b8c2SApple OSS Distributions 
3444*19c3b8c2SApple OSS Distributions 	if (lengthOfSegment) {
3445*19c3b8c2SApple OSS Distributions 		*lengthOfSegment = length;
3446*19c3b8c2SApple OSS Distributions 	}
3447*19c3b8c2SApple OSS Distributions 
3448*19c3b8c2SApple OSS Distributions 	return address;
3449*19c3b8c2SApple OSS Distributions }
3450*19c3b8c2SApple OSS Distributions 
3451*19c3b8c2SApple OSS Distributions #ifndef __LP64__
3452*19c3b8c2SApple OSS Distributions #pragma clang diagnostic push
3453*19c3b8c2SApple OSS Distributions #pragma clang diagnostic ignored "-Wdeprecated-declarations"
3454*19c3b8c2SApple OSS Distributions 
3455*19c3b8c2SApple OSS Distributions addr64_t
getPhysicalSegment(IOByteCount offset,IOByteCount * lengthOfSegment,IOOptionBits options)3456*19c3b8c2SApple OSS Distributions IOMemoryDescriptor::getPhysicalSegment(IOByteCount offset, IOByteCount *lengthOfSegment, IOOptionBits options)
3457*19c3b8c2SApple OSS Distributions {
3458*19c3b8c2SApple OSS Distributions 	addr64_t address = 0;
3459*19c3b8c2SApple OSS Distributions 
3460*19c3b8c2SApple OSS Distributions 	if (options & _kIOMemorySourceSegment) {
3461*19c3b8c2SApple OSS Distributions 		address = getSourceSegment(offset, lengthOfSegment);
3462*19c3b8c2SApple OSS Distributions 	} else if (options & kIOMemoryMapperNone) {
3463*19c3b8c2SApple OSS Distributions 		address = getPhysicalSegment64(offset, lengthOfSegment);
3464*19c3b8c2SApple OSS Distributions 	} else {
3465*19c3b8c2SApple OSS Distributions 		address = getPhysicalSegment(offset, lengthOfSegment);
3466*19c3b8c2SApple OSS Distributions 	}
3467*19c3b8c2SApple OSS Distributions 
3468*19c3b8c2SApple OSS Distributions 	return address;
3469*19c3b8c2SApple OSS Distributions }
3470*19c3b8c2SApple OSS Distributions #pragma clang diagnostic pop
3471*19c3b8c2SApple OSS Distributions 
3472*19c3b8c2SApple OSS Distributions addr64_t
getPhysicalSegment64(IOByteCount offset,IOByteCount * lengthOfSegment)3473*19c3b8c2SApple OSS Distributions IOGeneralMemoryDescriptor::getPhysicalSegment64(IOByteCount offset, IOByteCount *lengthOfSegment)
3474*19c3b8c2SApple OSS Distributions {
3475*19c3b8c2SApple OSS Distributions 	return getPhysicalSegment(offset, lengthOfSegment, kIOMemoryMapperNone);
3476*19c3b8c2SApple OSS Distributions }
3477*19c3b8c2SApple OSS Distributions 
3478*19c3b8c2SApple OSS Distributions IOPhysicalAddress
getPhysicalSegment(IOByteCount offset,IOByteCount * lengthOfSegment)3479*19c3b8c2SApple OSS Distributions IOGeneralMemoryDescriptor::getPhysicalSegment(IOByteCount offset, IOByteCount *lengthOfSegment)
3480*19c3b8c2SApple OSS Distributions {
3481*19c3b8c2SApple OSS Distributions 	addr64_t    address = 0;
3482*19c3b8c2SApple OSS Distributions 	IOByteCount length  = 0;
3483*19c3b8c2SApple OSS Distributions 
3484*19c3b8c2SApple OSS Distributions 	address = getPhysicalSegment(offset, lengthOfSegment, 0);
3485*19c3b8c2SApple OSS Distributions 
3486*19c3b8c2SApple OSS Distributions 	if (lengthOfSegment) {
3487*19c3b8c2SApple OSS Distributions 		length = *lengthOfSegment;
3488*19c3b8c2SApple OSS Distributions 	}
3489*19c3b8c2SApple OSS Distributions 
3490*19c3b8c2SApple OSS Distributions 	if ((address + length) > 0x100000000ULL) {
3491*19c3b8c2SApple OSS Distributions 		panic("getPhysicalSegment() out of 32b range 0x%qx, len 0x%lx, class %s",
3492*19c3b8c2SApple OSS Distributions 		    address, (long) length, (getMetaClass())->getClassName());
3493*19c3b8c2SApple OSS Distributions 	}
3494*19c3b8c2SApple OSS Distributions 
3495*19c3b8c2SApple OSS Distributions 	return (IOPhysicalAddress) address;
3496*19c3b8c2SApple OSS Distributions }
3497*19c3b8c2SApple OSS Distributions 
3498*19c3b8c2SApple OSS Distributions addr64_t
getPhysicalSegment64(IOByteCount offset,IOByteCount * lengthOfSegment)3499*19c3b8c2SApple OSS Distributions IOMemoryDescriptor::getPhysicalSegment64(IOByteCount offset, IOByteCount *lengthOfSegment)
3500*19c3b8c2SApple OSS Distributions {
3501*19c3b8c2SApple OSS Distributions 	IOPhysicalAddress phys32;
3502*19c3b8c2SApple OSS Distributions 	IOByteCount       length;
3503*19c3b8c2SApple OSS Distributions 	addr64_t          phys64;
3504*19c3b8c2SApple OSS Distributions 	IOMapper *        mapper = NULL;
3505*19c3b8c2SApple OSS Distributions 
3506*19c3b8c2SApple OSS Distributions 	phys32 = getPhysicalSegment(offset, lengthOfSegment);
3507*19c3b8c2SApple OSS Distributions 	if (!phys32) {
3508*19c3b8c2SApple OSS Distributions 		return 0;
3509*19c3b8c2SApple OSS Distributions 	}
3510*19c3b8c2SApple OSS Distributions 
3511*19c3b8c2SApple OSS Distributions 	if (gIOSystemMapper) {
3512*19c3b8c2SApple OSS Distributions 		mapper = gIOSystemMapper;
3513*19c3b8c2SApple OSS Distributions 	}
3514*19c3b8c2SApple OSS Distributions 
3515*19c3b8c2SApple OSS Distributions 	if (mapper) {
3516*19c3b8c2SApple OSS Distributions 		IOByteCount origLen;
3517*19c3b8c2SApple OSS Distributions 
3518*19c3b8c2SApple OSS Distributions 		phys64 = mapper->mapToPhysicalAddress(phys32);
3519*19c3b8c2SApple OSS Distributions 		origLen = *lengthOfSegment;
3520*19c3b8c2SApple OSS Distributions 		length = page_size - (phys64 & (page_size - 1));
3521*19c3b8c2SApple OSS Distributions 		while ((length < origLen)
3522*19c3b8c2SApple OSS Distributions 		    && ((phys64 + length) == mapper->mapToPhysicalAddress(phys32 + length))) {
3523*19c3b8c2SApple OSS Distributions 			length += page_size;
3524*19c3b8c2SApple OSS Distributions 		}
3525*19c3b8c2SApple OSS Distributions 		if (length > origLen) {
3526*19c3b8c2SApple OSS Distributions 			length = origLen;
3527*19c3b8c2SApple OSS Distributions 		}
3528*19c3b8c2SApple OSS Distributions 
3529*19c3b8c2SApple OSS Distributions 		*lengthOfSegment = length;
3530*19c3b8c2SApple OSS Distributions 	} else {
3531*19c3b8c2SApple OSS Distributions 		phys64 = (addr64_t) phys32;
3532*19c3b8c2SApple OSS Distributions 	}
3533*19c3b8c2SApple OSS Distributions 
3534*19c3b8c2SApple OSS Distributions 	return phys64;
3535*19c3b8c2SApple OSS Distributions }
3536*19c3b8c2SApple OSS Distributions 
3537*19c3b8c2SApple OSS Distributions IOPhysicalAddress
getPhysicalSegment(IOByteCount offset,IOByteCount * lengthOfSegment)3538*19c3b8c2SApple OSS Distributions IOMemoryDescriptor::getPhysicalSegment(IOByteCount offset, IOByteCount *lengthOfSegment)
3539*19c3b8c2SApple OSS Distributions {
3540*19c3b8c2SApple OSS Distributions 	return (IOPhysicalAddress) getPhysicalSegment(offset, lengthOfSegment, 0);
3541*19c3b8c2SApple OSS Distributions }
3542*19c3b8c2SApple OSS Distributions 
3543*19c3b8c2SApple OSS Distributions IOPhysicalAddress
getSourceSegment(IOByteCount offset,IOByteCount * lengthOfSegment)3544*19c3b8c2SApple OSS Distributions IOGeneralMemoryDescriptor::getSourceSegment(IOByteCount offset, IOByteCount *lengthOfSegment)
3545*19c3b8c2SApple OSS Distributions {
3546*19c3b8c2SApple OSS Distributions 	return (IOPhysicalAddress) getPhysicalSegment(offset, lengthOfSegment, _kIOMemorySourceSegment);
3547*19c3b8c2SApple OSS Distributions }
3548*19c3b8c2SApple OSS Distributions 
3549*19c3b8c2SApple OSS Distributions #pragma clang diagnostic push
3550*19c3b8c2SApple OSS Distributions #pragma clang diagnostic ignored "-Wdeprecated-declarations"
3551*19c3b8c2SApple OSS Distributions 
3552*19c3b8c2SApple OSS Distributions void *
getVirtualSegment(IOByteCount offset,IOByteCount * lengthOfSegment)3553*19c3b8c2SApple OSS Distributions IOGeneralMemoryDescriptor::getVirtualSegment(IOByteCount offset,
3554*19c3b8c2SApple OSS Distributions     IOByteCount * lengthOfSegment)
3555*19c3b8c2SApple OSS Distributions {
3556*19c3b8c2SApple OSS Distributions 	if (_task == kernel_task) {
3557*19c3b8c2SApple OSS Distributions 		return (void *) getSourceSegment(offset, lengthOfSegment);
3558*19c3b8c2SApple OSS Distributions 	} else {
3559*19c3b8c2SApple OSS Distributions 		panic("IOGMD::getVirtualSegment deprecated");
3560*19c3b8c2SApple OSS Distributions 	}
3561*19c3b8c2SApple OSS Distributions 
3562*19c3b8c2SApple OSS Distributions 	return NULL;
3563*19c3b8c2SApple OSS Distributions }
3564*19c3b8c2SApple OSS Distributions #pragma clang diagnostic pop
3565*19c3b8c2SApple OSS Distributions #endif /* !__LP64__ */
3566*19c3b8c2SApple OSS Distributions 
3567*19c3b8c2SApple OSS Distributions IOReturn
dmaCommandOperation(DMACommandOps op,void * vData,UInt dataSize) const3568*19c3b8c2SApple OSS Distributions IOMemoryDescriptor::dmaCommandOperation(DMACommandOps op, void *vData, UInt dataSize) const
3569*19c3b8c2SApple OSS Distributions {
3570*19c3b8c2SApple OSS Distributions 	IOMemoryDescriptor *md = const_cast<IOMemoryDescriptor *>(this);
3571*19c3b8c2SApple OSS Distributions 	DMACommandOps params;
3572*19c3b8c2SApple OSS Distributions 	IOReturn err;
3573*19c3b8c2SApple OSS Distributions 
3574*19c3b8c2SApple OSS Distributions 	params = (op & ~kIOMDDMACommandOperationMask & op);
3575*19c3b8c2SApple OSS Distributions 	op &= kIOMDDMACommandOperationMask;
3576*19c3b8c2SApple OSS Distributions 
3577*19c3b8c2SApple OSS Distributions 	if (kIOMDGetCharacteristics == op) {
3578*19c3b8c2SApple OSS Distributions 		if (dataSize < sizeof(IOMDDMACharacteristics)) {
3579*19c3b8c2SApple OSS Distributions 			return kIOReturnUnderrun;
3580*19c3b8c2SApple OSS Distributions 		}
3581*19c3b8c2SApple OSS Distributions 
3582*19c3b8c2SApple OSS Distributions 		IOMDDMACharacteristics *data = (IOMDDMACharacteristics *) vData;
3583*19c3b8c2SApple OSS Distributions 		data->fLength = getLength();
3584*19c3b8c2SApple OSS Distributions 		data->fSGCount = 0;
3585*19c3b8c2SApple OSS Distributions 		data->fDirection = getDirection();
3586*19c3b8c2SApple OSS Distributions 		data->fIsPrepared = true; // Assume prepared - fails safe
3587*19c3b8c2SApple OSS Distributions 	} else if (kIOMDWalkSegments == op) {
3588*19c3b8c2SApple OSS Distributions 		if (dataSize < sizeof(IOMDDMAWalkSegmentArgs)) {
3589*19c3b8c2SApple OSS Distributions 			return kIOReturnUnderrun;
3590*19c3b8c2SApple OSS Distributions 		}
3591*19c3b8c2SApple OSS Distributions 
3592*19c3b8c2SApple OSS Distributions 		IOMDDMAWalkSegmentArgs *data = (IOMDDMAWalkSegmentArgs *) vData;
3593*19c3b8c2SApple OSS Distributions 		IOByteCount offset  = (IOByteCount) data->fOffset;
3594*19c3b8c2SApple OSS Distributions 		IOPhysicalLength length, nextLength;
3595*19c3b8c2SApple OSS Distributions 		addr64_t         addr, nextAddr;
3596*19c3b8c2SApple OSS Distributions 
3597*19c3b8c2SApple OSS Distributions 		if (data->fMapped) {
3598*19c3b8c2SApple OSS Distributions 			panic("fMapped %p %s %qx", this, getMetaClass()->getClassName(), (uint64_t) getLength());
3599*19c3b8c2SApple OSS Distributions 		}
3600*19c3b8c2SApple OSS Distributions 		addr = md->getPhysicalSegment(offset, &length, kIOMemoryMapperNone);
3601*19c3b8c2SApple OSS Distributions 		offset += length;
3602*19c3b8c2SApple OSS Distributions 		while (offset < getLength()) {
3603*19c3b8c2SApple OSS Distributions 			nextAddr = md->getPhysicalSegment(offset, &nextLength, kIOMemoryMapperNone);
3604*19c3b8c2SApple OSS Distributions 			if ((addr + length) != nextAddr) {
3605*19c3b8c2SApple OSS Distributions 				break;
3606*19c3b8c2SApple OSS Distributions 			}
3607*19c3b8c2SApple OSS Distributions 			length += nextLength;
3608*19c3b8c2SApple OSS Distributions 			offset += nextLength;
3609*19c3b8c2SApple OSS Distributions 		}
3610*19c3b8c2SApple OSS Distributions 		data->fIOVMAddr = addr;
3611*19c3b8c2SApple OSS Distributions 		data->fLength   = length;
3612*19c3b8c2SApple OSS Distributions 	} else if (kIOMDAddDMAMapSpec == op) {
3613*19c3b8c2SApple OSS Distributions 		return kIOReturnUnsupported;
3614*19c3b8c2SApple OSS Distributions 	} else if (kIOMDDMAMap == op) {
3615*19c3b8c2SApple OSS Distributions 		if (dataSize < sizeof(IOMDDMAMapArgs)) {
3616*19c3b8c2SApple OSS Distributions 			return kIOReturnUnderrun;
3617*19c3b8c2SApple OSS Distributions 		}
3618*19c3b8c2SApple OSS Distributions 		IOMDDMAMapArgs * data = (IOMDDMAMapArgs *) vData;
3619*19c3b8c2SApple OSS Distributions 
3620*19c3b8c2SApple OSS Distributions 		err = md->dmaMap(data->fMapper, md, data->fCommand, &data->fMapSpec, data->fOffset, data->fLength, &data->fAlloc, &data->fAllocLength);
3621*19c3b8c2SApple OSS Distributions 
3622*19c3b8c2SApple OSS Distributions 		return err;
3623*19c3b8c2SApple OSS Distributions 	} else if (kIOMDDMAUnmap == op) {
3624*19c3b8c2SApple OSS Distributions 		if (dataSize < sizeof(IOMDDMAMapArgs)) {
3625*19c3b8c2SApple OSS Distributions 			return kIOReturnUnderrun;
3626*19c3b8c2SApple OSS Distributions 		}
3627*19c3b8c2SApple OSS Distributions 		IOMDDMAMapArgs * data = (IOMDDMAMapArgs *) vData;
3628*19c3b8c2SApple OSS Distributions 
3629*19c3b8c2SApple OSS Distributions 		err = md->dmaUnmap(data->fMapper, data->fCommand, data->fOffset, data->fAlloc, data->fAllocLength);
3630*19c3b8c2SApple OSS Distributions 
3631*19c3b8c2SApple OSS Distributions 		return kIOReturnSuccess;
3632*19c3b8c2SApple OSS Distributions 	} else {
3633*19c3b8c2SApple OSS Distributions 		return kIOReturnBadArgument;
3634*19c3b8c2SApple OSS Distributions 	}
3635*19c3b8c2SApple OSS Distributions 
3636*19c3b8c2SApple OSS Distributions 	return kIOReturnSuccess;
3637*19c3b8c2SApple OSS Distributions }
3638*19c3b8c2SApple OSS Distributions 
3639*19c3b8c2SApple OSS Distributions IOReturn
setPurgeable(IOOptionBits newState,IOOptionBits * oldState)3640*19c3b8c2SApple OSS Distributions IOGeneralMemoryDescriptor::setPurgeable( IOOptionBits newState,
3641*19c3b8c2SApple OSS Distributions     IOOptionBits * oldState )
3642*19c3b8c2SApple OSS Distributions {
3643*19c3b8c2SApple OSS Distributions 	IOReturn      err = kIOReturnSuccess;
3644*19c3b8c2SApple OSS Distributions 
3645*19c3b8c2SApple OSS Distributions 	vm_purgable_t control;
3646*19c3b8c2SApple OSS Distributions 	int           state;
3647*19c3b8c2SApple OSS Distributions 
3648*19c3b8c2SApple OSS Distributions 	assert(!(kIOMemoryRemote & _flags));
3649*19c3b8c2SApple OSS Distributions 	if (kIOMemoryRemote & _flags) {
3650*19c3b8c2SApple OSS Distributions 		return kIOReturnNotAttached;
3651*19c3b8c2SApple OSS Distributions 	}
3652*19c3b8c2SApple OSS Distributions 
3653*19c3b8c2SApple OSS Distributions 	if (_memRef) {
3654*19c3b8c2SApple OSS Distributions 		err = super::setPurgeable(newState, oldState);
3655*19c3b8c2SApple OSS Distributions 	} else {
3656*19c3b8c2SApple OSS Distributions 		if (kIOMemoryThreadSafe & _flags) {
3657*19c3b8c2SApple OSS Distributions 			LOCK;
3658*19c3b8c2SApple OSS Distributions 		}
3659*19c3b8c2SApple OSS Distributions 		do{
3660*19c3b8c2SApple OSS Distributions 			// Find the appropriate vm_map for the given task
3661*19c3b8c2SApple OSS Distributions 			vm_map_t curMap;
3662*19c3b8c2SApple OSS Distributions 			if (_task == kernel_task && (kIOMemoryBufferPageable & _flags)) {
3663*19c3b8c2SApple OSS Distributions 				err = kIOReturnNotReady;
3664*19c3b8c2SApple OSS Distributions 				break;
3665*19c3b8c2SApple OSS Distributions 			} else if (!_task) {
3666*19c3b8c2SApple OSS Distributions 				err = kIOReturnUnsupported;
3667*19c3b8c2SApple OSS Distributions 				break;
3668*19c3b8c2SApple OSS Distributions 			} else {
3669*19c3b8c2SApple OSS Distributions 				curMap = get_task_map(_task);
3670*19c3b8c2SApple OSS Distributions 				if (NULL == curMap) {
3671*19c3b8c2SApple OSS Distributions 					err = KERN_INVALID_ARGUMENT;
3672*19c3b8c2SApple OSS Distributions 					break;
3673*19c3b8c2SApple OSS Distributions 				}
3674*19c3b8c2SApple OSS Distributions 			}
3675*19c3b8c2SApple OSS Distributions 
3676*19c3b8c2SApple OSS Distributions 			// can only do one range
3677*19c3b8c2SApple OSS Distributions 			Ranges vec = _ranges;
3678*19c3b8c2SApple OSS Distributions 			IOOptionBits type = _flags & kIOMemoryTypeMask;
3679*19c3b8c2SApple OSS Distributions 			mach_vm_address_t addr;
3680*19c3b8c2SApple OSS Distributions 			mach_vm_size_t    len;
3681*19c3b8c2SApple OSS Distributions 			getAddrLenForInd(addr, len, type, vec, 0);
3682*19c3b8c2SApple OSS Distributions 
3683*19c3b8c2SApple OSS Distributions 			err = purgeableControlBits(newState, &control, &state);
3684*19c3b8c2SApple OSS Distributions 			if (kIOReturnSuccess != err) {
3685*19c3b8c2SApple OSS Distributions 				break;
3686*19c3b8c2SApple OSS Distributions 			}
3687*19c3b8c2SApple OSS Distributions 			err = vm_map_purgable_control(curMap, addr, control, &state);
3688*19c3b8c2SApple OSS Distributions 			if (oldState) {
3689*19c3b8c2SApple OSS Distributions 				if (kIOReturnSuccess == err) {
3690*19c3b8c2SApple OSS Distributions 					err = purgeableStateBits(&state);
3691*19c3b8c2SApple OSS Distributions 					*oldState = state;
3692*19c3b8c2SApple OSS Distributions 				}
3693*19c3b8c2SApple OSS Distributions 			}
3694*19c3b8c2SApple OSS Distributions 		}while (false);
3695*19c3b8c2SApple OSS Distributions 		if (kIOMemoryThreadSafe & _flags) {
3696*19c3b8c2SApple OSS Distributions 			UNLOCK;
3697*19c3b8c2SApple OSS Distributions 		}
3698*19c3b8c2SApple OSS Distributions 	}
3699*19c3b8c2SApple OSS Distributions 
3700*19c3b8c2SApple OSS Distributions 	return err;
3701*19c3b8c2SApple OSS Distributions }
3702*19c3b8c2SApple OSS Distributions 
3703*19c3b8c2SApple OSS Distributions IOReturn
setPurgeable(IOOptionBits newState,IOOptionBits * oldState)3704*19c3b8c2SApple OSS Distributions IOMemoryDescriptor::setPurgeable( IOOptionBits newState,
3705*19c3b8c2SApple OSS Distributions     IOOptionBits * oldState )
3706*19c3b8c2SApple OSS Distributions {
3707*19c3b8c2SApple OSS Distributions 	IOReturn err = kIOReturnNotReady;
3708*19c3b8c2SApple OSS Distributions 
3709*19c3b8c2SApple OSS Distributions 	if (kIOMemoryThreadSafe & _flags) {
3710*19c3b8c2SApple OSS Distributions 		LOCK;
3711*19c3b8c2SApple OSS Distributions 	}
3712*19c3b8c2SApple OSS Distributions 	if (_memRef) {
3713*19c3b8c2SApple OSS Distributions 		err = IOGeneralMemoryDescriptor::memoryReferenceSetPurgeable(_memRef, newState, oldState);
3714*19c3b8c2SApple OSS Distributions 	}
3715*19c3b8c2SApple OSS Distributions 	if (kIOMemoryThreadSafe & _flags) {
3716*19c3b8c2SApple OSS Distributions 		UNLOCK;
3717*19c3b8c2SApple OSS Distributions 	}
3718*19c3b8c2SApple OSS Distributions 
3719*19c3b8c2SApple OSS Distributions 	return err;
3720*19c3b8c2SApple OSS Distributions }
3721*19c3b8c2SApple OSS Distributions 
3722*19c3b8c2SApple OSS Distributions IOReturn
setOwnership(task_t newOwner,int newLedgerTag,IOOptionBits newLedgerOptions)3723*19c3b8c2SApple OSS Distributions IOGeneralMemoryDescriptor::setOwnership( task_t newOwner,
3724*19c3b8c2SApple OSS Distributions     int newLedgerTag,
3725*19c3b8c2SApple OSS Distributions     IOOptionBits newLedgerOptions )
3726*19c3b8c2SApple OSS Distributions {
3727*19c3b8c2SApple OSS Distributions 	IOReturn      err = kIOReturnSuccess;
3728*19c3b8c2SApple OSS Distributions 
3729*19c3b8c2SApple OSS Distributions 	assert(!(kIOMemoryRemote & _flags));
3730*19c3b8c2SApple OSS Distributions 	if (kIOMemoryRemote & _flags) {
3731*19c3b8c2SApple OSS Distributions 		return kIOReturnNotAttached;
3732*19c3b8c2SApple OSS Distributions 	}
3733*19c3b8c2SApple OSS Distributions 
3734*19c3b8c2SApple OSS Distributions 	if (iokit_iomd_setownership_enabled == FALSE) {
3735*19c3b8c2SApple OSS Distributions 		return kIOReturnUnsupported;
3736*19c3b8c2SApple OSS Distributions 	}
3737*19c3b8c2SApple OSS Distributions 
3738*19c3b8c2SApple OSS Distributions 	if (_memRef) {
3739*19c3b8c2SApple OSS Distributions 		err = super::setOwnership(newOwner, newLedgerTag, newLedgerOptions);
3740*19c3b8c2SApple OSS Distributions 	} else {
3741*19c3b8c2SApple OSS Distributions 		err = kIOReturnUnsupported;
3742*19c3b8c2SApple OSS Distributions 	}
3743*19c3b8c2SApple OSS Distributions 
3744*19c3b8c2SApple OSS Distributions 	return err;
3745*19c3b8c2SApple OSS Distributions }
3746*19c3b8c2SApple OSS Distributions 
3747*19c3b8c2SApple OSS Distributions IOReturn
setOwnership(task_t newOwner,int newLedgerTag,IOOptionBits newLedgerOptions)3748*19c3b8c2SApple OSS Distributions IOMemoryDescriptor::setOwnership( task_t newOwner,
3749*19c3b8c2SApple OSS Distributions     int newLedgerTag,
3750*19c3b8c2SApple OSS Distributions     IOOptionBits newLedgerOptions )
3751*19c3b8c2SApple OSS Distributions {
3752*19c3b8c2SApple OSS Distributions 	IOReturn err = kIOReturnNotReady;
3753*19c3b8c2SApple OSS Distributions 
3754*19c3b8c2SApple OSS Distributions 	assert(!(kIOMemoryRemote & _flags));
3755*19c3b8c2SApple OSS Distributions 	if (kIOMemoryRemote & _flags) {
3756*19c3b8c2SApple OSS Distributions 		return kIOReturnNotAttached;
3757*19c3b8c2SApple OSS Distributions 	}
3758*19c3b8c2SApple OSS Distributions 
3759*19c3b8c2SApple OSS Distributions 	if (iokit_iomd_setownership_enabled == FALSE) {
3760*19c3b8c2SApple OSS Distributions 		return kIOReturnUnsupported;
3761*19c3b8c2SApple OSS Distributions 	}
3762*19c3b8c2SApple OSS Distributions 
3763*19c3b8c2SApple OSS Distributions 	if (kIOMemoryThreadSafe & _flags) {
3764*19c3b8c2SApple OSS Distributions 		LOCK;
3765*19c3b8c2SApple OSS Distributions 	}
3766*19c3b8c2SApple OSS Distributions 	if (_memRef) {
3767*19c3b8c2SApple OSS Distributions 		err = IOGeneralMemoryDescriptor::memoryReferenceSetOwnership(_memRef, newOwner, newLedgerTag, newLedgerOptions);
3768*19c3b8c2SApple OSS Distributions 	} else {
3769*19c3b8c2SApple OSS Distributions 		IOMultiMemoryDescriptor * mmd;
3770*19c3b8c2SApple OSS Distributions 		IOSubMemoryDescriptor   * smd;
3771*19c3b8c2SApple OSS Distributions 		if ((smd = OSDynamicCast(IOSubMemoryDescriptor, this))) {
3772*19c3b8c2SApple OSS Distributions 			err = smd->setOwnership(newOwner, newLedgerTag, newLedgerOptions);
3773*19c3b8c2SApple OSS Distributions 		} else if ((mmd = OSDynamicCast(IOMultiMemoryDescriptor, this))) {
3774*19c3b8c2SApple OSS Distributions 			err = mmd->setOwnership(newOwner, newLedgerTag, newLedgerOptions);
3775*19c3b8c2SApple OSS Distributions 		}
3776*19c3b8c2SApple OSS Distributions 	}
3777*19c3b8c2SApple OSS Distributions 	if (kIOMemoryThreadSafe & _flags) {
3778*19c3b8c2SApple OSS Distributions 		UNLOCK;
3779*19c3b8c2SApple OSS Distributions 	}
3780*19c3b8c2SApple OSS Distributions 
3781*19c3b8c2SApple OSS Distributions 	return err;
3782*19c3b8c2SApple OSS Distributions }
3783*19c3b8c2SApple OSS Distributions 
3784*19c3b8c2SApple OSS Distributions 
3785*19c3b8c2SApple OSS Distributions uint64_t
getDMAMapLength(uint64_t * offset)3786*19c3b8c2SApple OSS Distributions IOMemoryDescriptor::getDMAMapLength(uint64_t * offset)
3787*19c3b8c2SApple OSS Distributions {
3788*19c3b8c2SApple OSS Distributions 	uint64_t length;
3789*19c3b8c2SApple OSS Distributions 
3790*19c3b8c2SApple OSS Distributions 	if (_memRef) {
3791*19c3b8c2SApple OSS Distributions 		length = IOGeneralMemoryDescriptor::memoryReferenceGetDMAMapLength(_memRef, offset);
3792*19c3b8c2SApple OSS Distributions 	} else {
3793*19c3b8c2SApple OSS Distributions 		IOByteCount       iterate, segLen;
3794*19c3b8c2SApple OSS Distributions 		IOPhysicalAddress sourceAddr, sourceAlign;
3795*19c3b8c2SApple OSS Distributions 
3796*19c3b8c2SApple OSS Distributions 		if (kIOMemoryThreadSafe & _flags) {
3797*19c3b8c2SApple OSS Distributions 			LOCK;
3798*19c3b8c2SApple OSS Distributions 		}
3799*19c3b8c2SApple OSS Distributions 		length = 0;
3800*19c3b8c2SApple OSS Distributions 		iterate = 0;
3801*19c3b8c2SApple OSS Distributions 		while ((sourceAddr = getPhysicalSegment(iterate, &segLen, _kIOMemorySourceSegment))) {
3802*19c3b8c2SApple OSS Distributions 			sourceAlign = (sourceAddr & page_mask);
3803*19c3b8c2SApple OSS Distributions 			if (offset && !iterate) {
3804*19c3b8c2SApple OSS Distributions 				*offset = sourceAlign;
3805*19c3b8c2SApple OSS Distributions 			}
3806*19c3b8c2SApple OSS Distributions 			length += round_page(sourceAddr + segLen) - trunc_page(sourceAddr);
3807*19c3b8c2SApple OSS Distributions 			iterate += segLen;
3808*19c3b8c2SApple OSS Distributions 		}
3809*19c3b8c2SApple OSS Distributions 		if (!iterate) {
3810*19c3b8c2SApple OSS Distributions 			length = getLength();
3811*19c3b8c2SApple OSS Distributions 			if (offset) {
3812*19c3b8c2SApple OSS Distributions 				*offset = 0;
3813*19c3b8c2SApple OSS Distributions 			}
3814*19c3b8c2SApple OSS Distributions 		}
3815*19c3b8c2SApple OSS Distributions 		if (kIOMemoryThreadSafe & _flags) {
3816*19c3b8c2SApple OSS Distributions 			UNLOCK;
3817*19c3b8c2SApple OSS Distributions 		}
3818*19c3b8c2SApple OSS Distributions 	}
3819*19c3b8c2SApple OSS Distributions 
3820*19c3b8c2SApple OSS Distributions 	return length;
3821*19c3b8c2SApple OSS Distributions }
3822*19c3b8c2SApple OSS Distributions 
3823*19c3b8c2SApple OSS Distributions 
3824*19c3b8c2SApple OSS Distributions IOReturn
getPageCounts(IOByteCount * residentPageCount,IOByteCount * dirtyPageCount)3825*19c3b8c2SApple OSS Distributions IOMemoryDescriptor::getPageCounts( IOByteCount * residentPageCount,
3826*19c3b8c2SApple OSS Distributions     IOByteCount * dirtyPageCount )
3827*19c3b8c2SApple OSS Distributions {
3828*19c3b8c2SApple OSS Distributions 	IOReturn err = kIOReturnNotReady;
3829*19c3b8c2SApple OSS Distributions 
3830*19c3b8c2SApple OSS Distributions 	assert(!(kIOMemoryRemote & _flags));
3831*19c3b8c2SApple OSS Distributions 	if (kIOMemoryRemote & _flags) {
3832*19c3b8c2SApple OSS Distributions 		return kIOReturnNotAttached;
3833*19c3b8c2SApple OSS Distributions 	}
3834*19c3b8c2SApple OSS Distributions 
3835*19c3b8c2SApple OSS Distributions 	if (kIOMemoryThreadSafe & _flags) {
3836*19c3b8c2SApple OSS Distributions 		LOCK;
3837*19c3b8c2SApple OSS Distributions 	}
3838*19c3b8c2SApple OSS Distributions 	if (_memRef) {
3839*19c3b8c2SApple OSS Distributions 		err = IOGeneralMemoryDescriptor::memoryReferenceGetPageCounts(_memRef, residentPageCount, dirtyPageCount);
3840*19c3b8c2SApple OSS Distributions 	} else {
3841*19c3b8c2SApple OSS Distributions 		IOMultiMemoryDescriptor * mmd;
3842*19c3b8c2SApple OSS Distributions 		IOSubMemoryDescriptor   * smd;
3843*19c3b8c2SApple OSS Distributions 		if ((smd = OSDynamicCast(IOSubMemoryDescriptor, this))) {
3844*19c3b8c2SApple OSS Distributions 			err = smd->getPageCounts(residentPageCount, dirtyPageCount);
3845*19c3b8c2SApple OSS Distributions 		} else if ((mmd = OSDynamicCast(IOMultiMemoryDescriptor, this))) {
3846*19c3b8c2SApple OSS Distributions 			err = mmd->getPageCounts(residentPageCount, dirtyPageCount);
3847*19c3b8c2SApple OSS Distributions 		}
3848*19c3b8c2SApple OSS Distributions 	}
3849*19c3b8c2SApple OSS Distributions 	if (kIOMemoryThreadSafe & _flags) {
3850*19c3b8c2SApple OSS Distributions 		UNLOCK;
3851*19c3b8c2SApple OSS Distributions 	}
3852*19c3b8c2SApple OSS Distributions 
3853*19c3b8c2SApple OSS Distributions 	return err;
3854*19c3b8c2SApple OSS Distributions }
3855*19c3b8c2SApple OSS Distributions 
3856*19c3b8c2SApple OSS Distributions 
3857*19c3b8c2SApple OSS Distributions #if defined(__arm64__)
3858*19c3b8c2SApple OSS Distributions extern "C" void dcache_incoherent_io_flush64(addr64_t pa, unsigned int count, unsigned int remaining, unsigned int *res);
3859*19c3b8c2SApple OSS Distributions extern "C" void dcache_incoherent_io_store64(addr64_t pa, unsigned int count, unsigned int remaining, unsigned int *res);
3860*19c3b8c2SApple OSS Distributions #else /* defined(__arm64__) */
3861*19c3b8c2SApple OSS Distributions extern "C" void dcache_incoherent_io_flush64(addr64_t pa, unsigned int count);
3862*19c3b8c2SApple OSS Distributions extern "C" void dcache_incoherent_io_store64(addr64_t pa, unsigned int count);
3863*19c3b8c2SApple OSS Distributions #endif /* defined(__arm64__) */
3864*19c3b8c2SApple OSS Distributions 
3865*19c3b8c2SApple OSS Distributions static void
SetEncryptOp(addr64_t pa,unsigned int count)3866*19c3b8c2SApple OSS Distributions SetEncryptOp(addr64_t pa, unsigned int count)
3867*19c3b8c2SApple OSS Distributions {
3868*19c3b8c2SApple OSS Distributions 	ppnum_t page, end;
3869*19c3b8c2SApple OSS Distributions 
3870*19c3b8c2SApple OSS Distributions 	page = (ppnum_t) atop_64(round_page_64(pa));
3871*19c3b8c2SApple OSS Distributions 	end  = (ppnum_t) atop_64(trunc_page_64(pa + count));
3872*19c3b8c2SApple OSS Distributions 	for (; page < end; page++) {
3873*19c3b8c2SApple OSS Distributions 		pmap_clear_noencrypt(page);
3874*19c3b8c2SApple OSS Distributions 	}
3875*19c3b8c2SApple OSS Distributions }
3876*19c3b8c2SApple OSS Distributions 
3877*19c3b8c2SApple OSS Distributions static void
ClearEncryptOp(addr64_t pa,unsigned int count)3878*19c3b8c2SApple OSS Distributions ClearEncryptOp(addr64_t pa, unsigned int count)
3879*19c3b8c2SApple OSS Distributions {
3880*19c3b8c2SApple OSS Distributions 	ppnum_t page, end;
3881*19c3b8c2SApple OSS Distributions 
3882*19c3b8c2SApple OSS Distributions 	page = (ppnum_t) atop_64(round_page_64(pa));
3883*19c3b8c2SApple OSS Distributions 	end  = (ppnum_t) atop_64(trunc_page_64(pa + count));
3884*19c3b8c2SApple OSS Distributions 	for (; page < end; page++) {
3885*19c3b8c2SApple OSS Distributions 		pmap_set_noencrypt(page);
3886*19c3b8c2SApple OSS Distributions 	}
3887*19c3b8c2SApple OSS Distributions }
3888*19c3b8c2SApple OSS Distributions 
3889*19c3b8c2SApple OSS Distributions IOReturn
performOperation(IOOptionBits options,IOByteCount offset,IOByteCount length)3890*19c3b8c2SApple OSS Distributions IOMemoryDescriptor::performOperation( IOOptionBits options,
3891*19c3b8c2SApple OSS Distributions     IOByteCount offset, IOByteCount length )
3892*19c3b8c2SApple OSS Distributions {
3893*19c3b8c2SApple OSS Distributions 	IOByteCount remaining;
3894*19c3b8c2SApple OSS Distributions 	unsigned int res;
3895*19c3b8c2SApple OSS Distributions 	void (*func)(addr64_t pa, unsigned int count) = NULL;
3896*19c3b8c2SApple OSS Distributions #if defined(__arm64__)
3897*19c3b8c2SApple OSS Distributions 	void (*func_ext)(addr64_t pa, unsigned int count, unsigned int remaining, unsigned int *result) = NULL;
3898*19c3b8c2SApple OSS Distributions #endif
3899*19c3b8c2SApple OSS Distributions 
3900*19c3b8c2SApple OSS Distributions 	assert(!(kIOMemoryRemote & _flags));
3901*19c3b8c2SApple OSS Distributions 	if (kIOMemoryRemote & _flags) {
3902*19c3b8c2SApple OSS Distributions 		return kIOReturnNotAttached;
3903*19c3b8c2SApple OSS Distributions 	}
3904*19c3b8c2SApple OSS Distributions 
3905*19c3b8c2SApple OSS Distributions 	switch (options) {
3906*19c3b8c2SApple OSS Distributions 	case kIOMemoryIncoherentIOFlush:
3907*19c3b8c2SApple OSS Distributions #if defined(__arm64__)
3908*19c3b8c2SApple OSS Distributions 		func_ext = &dcache_incoherent_io_flush64;
3909*19c3b8c2SApple OSS Distributions #if __ARM_COHERENT_IO__
3910*19c3b8c2SApple OSS Distributions 		func_ext(0, 0, 0, &res);
3911*19c3b8c2SApple OSS Distributions 		return kIOReturnSuccess;
3912*19c3b8c2SApple OSS Distributions #else /* __ARM_COHERENT_IO__ */
3913*19c3b8c2SApple OSS Distributions 		break;
3914*19c3b8c2SApple OSS Distributions #endif /* __ARM_COHERENT_IO__ */
3915*19c3b8c2SApple OSS Distributions #else /* defined(__arm64__) */
3916*19c3b8c2SApple OSS Distributions 		func = &dcache_incoherent_io_flush64;
3917*19c3b8c2SApple OSS Distributions 		break;
3918*19c3b8c2SApple OSS Distributions #endif /* defined(__arm64__) */
3919*19c3b8c2SApple OSS Distributions 	case kIOMemoryIncoherentIOStore:
3920*19c3b8c2SApple OSS Distributions #if defined(__arm64__)
3921*19c3b8c2SApple OSS Distributions 		func_ext = &dcache_incoherent_io_store64;
3922*19c3b8c2SApple OSS Distributions #if __ARM_COHERENT_IO__
3923*19c3b8c2SApple OSS Distributions 		func_ext(0, 0, 0, &res);
3924*19c3b8c2SApple OSS Distributions 		return kIOReturnSuccess;
3925*19c3b8c2SApple OSS Distributions #else /* __ARM_COHERENT_IO__ */
3926*19c3b8c2SApple OSS Distributions 		break;
3927*19c3b8c2SApple OSS Distributions #endif /* __ARM_COHERENT_IO__ */
3928*19c3b8c2SApple OSS Distributions #else /* defined(__arm64__) */
3929*19c3b8c2SApple OSS Distributions 		func = &dcache_incoherent_io_store64;
3930*19c3b8c2SApple OSS Distributions 		break;
3931*19c3b8c2SApple OSS Distributions #endif /* defined(__arm64__) */
3932*19c3b8c2SApple OSS Distributions 
3933*19c3b8c2SApple OSS Distributions 	case kIOMemorySetEncrypted:
3934*19c3b8c2SApple OSS Distributions 		func = &SetEncryptOp;
3935*19c3b8c2SApple OSS Distributions 		break;
3936*19c3b8c2SApple OSS Distributions 	case kIOMemoryClearEncrypted:
3937*19c3b8c2SApple OSS Distributions 		func = &ClearEncryptOp;
3938*19c3b8c2SApple OSS Distributions 		break;
3939*19c3b8c2SApple OSS Distributions 	}
3940*19c3b8c2SApple OSS Distributions 
3941*19c3b8c2SApple OSS Distributions #if defined(__arm64__)
3942*19c3b8c2SApple OSS Distributions 	if ((func == NULL) && (func_ext == NULL)) {
3943*19c3b8c2SApple OSS Distributions 		return kIOReturnUnsupported;
3944*19c3b8c2SApple OSS Distributions 	}
3945*19c3b8c2SApple OSS Distributions #else /* defined(__arm64__) */
3946*19c3b8c2SApple OSS Distributions 	if (!func) {
3947*19c3b8c2SApple OSS Distributions 		return kIOReturnUnsupported;
3948*19c3b8c2SApple OSS Distributions 	}
3949*19c3b8c2SApple OSS Distributions #endif /* defined(__arm64__) */
3950*19c3b8c2SApple OSS Distributions 
3951*19c3b8c2SApple OSS Distributions 	if (kIOMemoryThreadSafe & _flags) {
3952*19c3b8c2SApple OSS Distributions 		LOCK;
3953*19c3b8c2SApple OSS Distributions 	}
3954*19c3b8c2SApple OSS Distributions 
3955*19c3b8c2SApple OSS Distributions 	res = 0x0UL;
3956*19c3b8c2SApple OSS Distributions 	remaining = length = min(length, getLength() - offset);
3957*19c3b8c2SApple OSS Distributions 	while (remaining) {
3958*19c3b8c2SApple OSS Distributions 		// (process another target segment?)
3959*19c3b8c2SApple OSS Distributions 		addr64_t    dstAddr64;
3960*19c3b8c2SApple OSS Distributions 		IOByteCount dstLen;
3961*19c3b8c2SApple OSS Distributions 
3962*19c3b8c2SApple OSS Distributions 		dstAddr64 = getPhysicalSegment(offset, &dstLen, kIOMemoryMapperNone);
3963*19c3b8c2SApple OSS Distributions 		if (!dstAddr64) {
3964*19c3b8c2SApple OSS Distributions 			break;
3965*19c3b8c2SApple OSS Distributions 		}
3966*19c3b8c2SApple OSS Distributions 
3967*19c3b8c2SApple OSS Distributions 		// Clip segment length to remaining
3968*19c3b8c2SApple OSS Distributions 		if (dstLen > remaining) {
3969*19c3b8c2SApple OSS Distributions 			dstLen = remaining;
3970*19c3b8c2SApple OSS Distributions 		}
3971*19c3b8c2SApple OSS Distributions 		if (dstLen > (UINT_MAX - PAGE_SIZE + 1)) {
3972*19c3b8c2SApple OSS Distributions 			dstLen = (UINT_MAX - PAGE_SIZE + 1);
3973*19c3b8c2SApple OSS Distributions 		}
3974*19c3b8c2SApple OSS Distributions 		if (remaining > UINT_MAX) {
3975*19c3b8c2SApple OSS Distributions 			remaining = UINT_MAX;
3976*19c3b8c2SApple OSS Distributions 		}
3977*19c3b8c2SApple OSS Distributions 
3978*19c3b8c2SApple OSS Distributions #if defined(__arm64__)
3979*19c3b8c2SApple OSS Distributions 		if (func) {
3980*19c3b8c2SApple OSS Distributions 			(*func)(dstAddr64, (unsigned int) dstLen);
3981*19c3b8c2SApple OSS Distributions 		}
3982*19c3b8c2SApple OSS Distributions 		if (func_ext) {
3983*19c3b8c2SApple OSS Distributions 			(*func_ext)(dstAddr64, (unsigned int) dstLen, (unsigned int) remaining, &res);
3984*19c3b8c2SApple OSS Distributions 			if (res != 0x0UL) {
3985*19c3b8c2SApple OSS Distributions 				remaining = 0;
3986*19c3b8c2SApple OSS Distributions 				break;
3987*19c3b8c2SApple OSS Distributions 			}
3988*19c3b8c2SApple OSS Distributions 		}
3989*19c3b8c2SApple OSS Distributions #else /* defined(__arm64__) */
3990*19c3b8c2SApple OSS Distributions 		(*func)(dstAddr64, (unsigned int) dstLen);
3991*19c3b8c2SApple OSS Distributions #endif /* defined(__arm64__) */
3992*19c3b8c2SApple OSS Distributions 
3993*19c3b8c2SApple OSS Distributions 		offset    += dstLen;
3994*19c3b8c2SApple OSS Distributions 		remaining -= dstLen;
3995*19c3b8c2SApple OSS Distributions 	}
3996*19c3b8c2SApple OSS Distributions 
3997*19c3b8c2SApple OSS Distributions 	if (kIOMemoryThreadSafe & _flags) {
3998*19c3b8c2SApple OSS Distributions 		UNLOCK;
3999*19c3b8c2SApple OSS Distributions 	}
4000*19c3b8c2SApple OSS Distributions 
4001*19c3b8c2SApple OSS Distributions 	return remaining ? kIOReturnUnderrun : kIOReturnSuccess;
4002*19c3b8c2SApple OSS Distributions }
4003*19c3b8c2SApple OSS Distributions 
4004*19c3b8c2SApple OSS Distributions /*
4005*19c3b8c2SApple OSS Distributions  *
4006*19c3b8c2SApple OSS Distributions  */
4007*19c3b8c2SApple OSS Distributions 
4008*19c3b8c2SApple OSS Distributions #if defined(__i386__) || defined(__x86_64__)
4009*19c3b8c2SApple OSS Distributions 
4010*19c3b8c2SApple OSS Distributions extern vm_offset_t kc_highest_nonlinkedit_vmaddr;
4011*19c3b8c2SApple OSS Distributions 
4012*19c3b8c2SApple OSS Distributions /* XXX: By extending io_kernel_static_end to the highest virtual address in the KC,
4013*19c3b8c2SApple OSS Distributions  * we're opening up this path to IOMemoryDescriptor consumers who can now create UPLs to
4014*19c3b8c2SApple OSS Distributions  * kernel non-text data -- should we just add another range instead?
4015*19c3b8c2SApple OSS Distributions  */
4016*19c3b8c2SApple OSS Distributions #define io_kernel_static_start  vm_kernel_stext
4017*19c3b8c2SApple OSS Distributions #define io_kernel_static_end    (kc_highest_nonlinkedit_vmaddr ? kc_highest_nonlinkedit_vmaddr : vm_kernel_etext)
4018*19c3b8c2SApple OSS Distributions 
4019*19c3b8c2SApple OSS Distributions #elif defined(__arm64__)
4020*19c3b8c2SApple OSS Distributions 
4021*19c3b8c2SApple OSS Distributions extern vm_offset_t              static_memory_end;
4022*19c3b8c2SApple OSS Distributions 
4023*19c3b8c2SApple OSS Distributions #if defined(__arm64__)
4024*19c3b8c2SApple OSS Distributions #define io_kernel_static_start vm_kext_base
4025*19c3b8c2SApple OSS Distributions #else /* defined(__arm64__) */
4026*19c3b8c2SApple OSS Distributions #define io_kernel_static_start vm_kernel_stext
4027*19c3b8c2SApple OSS Distributions #endif /* defined(__arm64__) */
4028*19c3b8c2SApple OSS Distributions 
4029*19c3b8c2SApple OSS Distributions #define io_kernel_static_end    static_memory_end
4030*19c3b8c2SApple OSS Distributions 
4031*19c3b8c2SApple OSS Distributions #else
4032*19c3b8c2SApple OSS Distributions #error io_kernel_static_end is undefined for this architecture
4033*19c3b8c2SApple OSS Distributions #endif
4034*19c3b8c2SApple OSS Distributions 
4035*19c3b8c2SApple OSS Distributions static kern_return_t
io_get_kernel_static_upl(vm_map_t,uintptr_t offset,upl_size_t * upl_size,unsigned int * page_offset,upl_t * upl,upl_page_info_array_t page_list,unsigned int * count,ppnum_t * highest_page)4036*19c3b8c2SApple OSS Distributions io_get_kernel_static_upl(
4037*19c3b8c2SApple OSS Distributions 	vm_map_t                /* map */,
4038*19c3b8c2SApple OSS Distributions 	uintptr_t               offset,
4039*19c3b8c2SApple OSS Distributions 	upl_size_t              *upl_size,
4040*19c3b8c2SApple OSS Distributions 	unsigned int            *page_offset,
4041*19c3b8c2SApple OSS Distributions 	upl_t                   *upl,
4042*19c3b8c2SApple OSS Distributions 	upl_page_info_array_t   page_list,
4043*19c3b8c2SApple OSS Distributions 	unsigned int            *count,
4044*19c3b8c2SApple OSS Distributions 	ppnum_t                 *highest_page)
4045*19c3b8c2SApple OSS Distributions {
4046*19c3b8c2SApple OSS Distributions 	unsigned int pageCount, page;
4047*19c3b8c2SApple OSS Distributions 	ppnum_t phys;
4048*19c3b8c2SApple OSS Distributions 	ppnum_t highestPage = 0;
4049*19c3b8c2SApple OSS Distributions 
4050*19c3b8c2SApple OSS Distributions 	pageCount = atop_32(round_page(*upl_size + (page_mask & offset)));
4051*19c3b8c2SApple OSS Distributions 	if (pageCount > *count) {
4052*19c3b8c2SApple OSS Distributions 		pageCount = *count;
4053*19c3b8c2SApple OSS Distributions 	}
4054*19c3b8c2SApple OSS Distributions 	*upl_size = (upl_size_t) ptoa_64(pageCount);
4055*19c3b8c2SApple OSS Distributions 
4056*19c3b8c2SApple OSS Distributions 	*upl = NULL;
4057*19c3b8c2SApple OSS Distributions 	*page_offset = ((unsigned int) page_mask & offset);
4058*19c3b8c2SApple OSS Distributions 
4059*19c3b8c2SApple OSS Distributions 	for (page = 0; page < pageCount; page++) {
4060*19c3b8c2SApple OSS Distributions 		phys = pmap_find_phys(kernel_pmap, ((addr64_t)offset) + ptoa_64(page));
4061*19c3b8c2SApple OSS Distributions 		if (!phys) {
4062*19c3b8c2SApple OSS Distributions 			break;
4063*19c3b8c2SApple OSS Distributions 		}
4064*19c3b8c2SApple OSS Distributions 		page_list[page].phys_addr = phys;
4065*19c3b8c2SApple OSS Distributions 		page_list[page].free_when_done = 0;
4066*19c3b8c2SApple OSS Distributions 		page_list[page].absent    = 0;
4067*19c3b8c2SApple OSS Distributions 		page_list[page].dirty     = 0;
4068*19c3b8c2SApple OSS Distributions 		page_list[page].precious  = 0;
4069*19c3b8c2SApple OSS Distributions 		page_list[page].device    = 0;
4070*19c3b8c2SApple OSS Distributions 		if (phys > highestPage) {
4071*19c3b8c2SApple OSS Distributions 			highestPage = phys;
4072*19c3b8c2SApple OSS Distributions 		}
4073*19c3b8c2SApple OSS Distributions 	}
4074*19c3b8c2SApple OSS Distributions 
4075*19c3b8c2SApple OSS Distributions 	*highest_page = highestPage;
4076*19c3b8c2SApple OSS Distributions 
4077*19c3b8c2SApple OSS Distributions 	return (page >= pageCount) ? kIOReturnSuccess : kIOReturnVMError;
4078*19c3b8c2SApple OSS Distributions }
4079*19c3b8c2SApple OSS Distributions 
4080*19c3b8c2SApple OSS Distributions IOReturn
wireVirtual(IODirection forDirection)4081*19c3b8c2SApple OSS Distributions IOGeneralMemoryDescriptor::wireVirtual(IODirection forDirection)
4082*19c3b8c2SApple OSS Distributions {
4083*19c3b8c2SApple OSS Distributions 	IOOptionBits type = _flags & kIOMemoryTypeMask;
4084*19c3b8c2SApple OSS Distributions 	IOReturn error = kIOReturnSuccess;
4085*19c3b8c2SApple OSS Distributions 	ioGMDData *dataP;
4086*19c3b8c2SApple OSS Distributions 	upl_page_info_array_t pageInfo;
4087*19c3b8c2SApple OSS Distributions 	ppnum_t mapBase;
4088*19c3b8c2SApple OSS Distributions 	vm_tag_t tag = VM_KERN_MEMORY_NONE;
4089*19c3b8c2SApple OSS Distributions 	mach_vm_size_t numBytesWired = 0;
4090*19c3b8c2SApple OSS Distributions 
4091*19c3b8c2SApple OSS Distributions 	assert(kIOMemoryTypeVirtual == type || kIOMemoryTypeVirtual64 == type || kIOMemoryTypeUIO == type);
4092*19c3b8c2SApple OSS Distributions 
4093*19c3b8c2SApple OSS Distributions 	if ((kIODirectionOutIn & forDirection) == kIODirectionNone) {
4094*19c3b8c2SApple OSS Distributions 		forDirection = (IODirection) (forDirection | getDirection());
4095*19c3b8c2SApple OSS Distributions 	}
4096*19c3b8c2SApple OSS Distributions 
4097*19c3b8c2SApple OSS Distributions 	dataP = getDataP(_memoryEntries);
4098*19c3b8c2SApple OSS Distributions 	upl_control_flags_t uplFlags; // This Mem Desc's default flags for upl creation
4099*19c3b8c2SApple OSS Distributions 	switch (kIODirectionOutIn & forDirection) {
4100*19c3b8c2SApple OSS Distributions 	case kIODirectionOut:
4101*19c3b8c2SApple OSS Distributions 		// Pages do not need to be marked as dirty on commit
4102*19c3b8c2SApple OSS Distributions 		uplFlags = UPL_COPYOUT_FROM;
4103*19c3b8c2SApple OSS Distributions 		dataP->fDMAAccess = kIODMAMapReadAccess;
4104*19c3b8c2SApple OSS Distributions 		break;
4105*19c3b8c2SApple OSS Distributions 
4106*19c3b8c2SApple OSS Distributions 	case kIODirectionIn:
4107*19c3b8c2SApple OSS Distributions 		dataP->fDMAAccess = kIODMAMapWriteAccess;
4108*19c3b8c2SApple OSS Distributions 		uplFlags = 0;   // i.e. ~UPL_COPYOUT_FROM
4109*19c3b8c2SApple OSS Distributions 		break;
4110*19c3b8c2SApple OSS Distributions 
4111*19c3b8c2SApple OSS Distributions 	default:
4112*19c3b8c2SApple OSS Distributions 		dataP->fDMAAccess = kIODMAMapReadAccess | kIODMAMapWriteAccess;
4113*19c3b8c2SApple OSS Distributions 		uplFlags = 0;   // i.e. ~UPL_COPYOUT_FROM
4114*19c3b8c2SApple OSS Distributions 		break;
4115*19c3b8c2SApple OSS Distributions 	}
4116*19c3b8c2SApple OSS Distributions 
4117*19c3b8c2SApple OSS Distributions 	if (_wireCount) {
4118*19c3b8c2SApple OSS Distributions 		if ((kIOMemoryPreparedReadOnly & _flags) && !(UPL_COPYOUT_FROM & uplFlags)) {
4119*19c3b8c2SApple OSS Distributions 			OSReportWithBacktrace("IOMemoryDescriptor 0x%zx prepared read only",
4120*19c3b8c2SApple OSS Distributions 			    (size_t)VM_KERNEL_ADDRPERM(this));
4121*19c3b8c2SApple OSS Distributions 			error = kIOReturnNotWritable;
4122*19c3b8c2SApple OSS Distributions 		}
4123*19c3b8c2SApple OSS Distributions 	} else {
4124*19c3b8c2SApple OSS Distributions 		IOTimeStampIntervalConstantFiltered traceInterval(IODBG_MDESC(IOMDESC_WIRE), VM_KERNEL_ADDRHIDE(this), forDirection);
4125*19c3b8c2SApple OSS Distributions 		IOMapper *mapper;
4126*19c3b8c2SApple OSS Distributions 
4127*19c3b8c2SApple OSS Distributions 		mapper = dataP->fMapper;
4128*19c3b8c2SApple OSS Distributions 		dataP->fMappedBaseValid = dataP->fMappedBase = 0;
4129*19c3b8c2SApple OSS Distributions 
4130*19c3b8c2SApple OSS Distributions 		uplFlags |= UPL_SET_IO_WIRE | UPL_SET_LITE;
4131*19c3b8c2SApple OSS Distributions 		tag = _kernelTag;
4132*19c3b8c2SApple OSS Distributions 		if (VM_KERN_MEMORY_NONE == tag) {
4133*19c3b8c2SApple OSS Distributions 			tag = IOMemoryTag(kernel_map);
4134*19c3b8c2SApple OSS Distributions 		}
4135*19c3b8c2SApple OSS Distributions 
4136*19c3b8c2SApple OSS Distributions 		if (kIODirectionPrepareToPhys32 & forDirection) {
4137*19c3b8c2SApple OSS Distributions 			if (!mapper) {
4138*19c3b8c2SApple OSS Distributions 				uplFlags |= UPL_NEED_32BIT_ADDR;
4139*19c3b8c2SApple OSS Distributions 			}
4140*19c3b8c2SApple OSS Distributions 			if (dataP->fDMAMapNumAddressBits > 32) {
4141*19c3b8c2SApple OSS Distributions 				dataP->fDMAMapNumAddressBits = 32;
4142*19c3b8c2SApple OSS Distributions 			}
4143*19c3b8c2SApple OSS Distributions 		}
4144*19c3b8c2SApple OSS Distributions 		if (kIODirectionPrepareNoFault    & forDirection) {
4145*19c3b8c2SApple OSS Distributions 			uplFlags |= UPL_REQUEST_NO_FAULT;
4146*19c3b8c2SApple OSS Distributions 		}
4147*19c3b8c2SApple OSS Distributions 		if (kIODirectionPrepareNoZeroFill & forDirection) {
4148*19c3b8c2SApple OSS Distributions 			uplFlags |= UPL_NOZEROFILLIO;
4149*19c3b8c2SApple OSS Distributions 		}
4150*19c3b8c2SApple OSS Distributions 		if (kIODirectionPrepareNonCoherent & forDirection) {
4151*19c3b8c2SApple OSS Distributions 			uplFlags |= UPL_REQUEST_FORCE_COHERENCY;
4152*19c3b8c2SApple OSS Distributions 		}
4153*19c3b8c2SApple OSS Distributions 
4154*19c3b8c2SApple OSS Distributions 		mapBase = 0;
4155*19c3b8c2SApple OSS Distributions 
4156*19c3b8c2SApple OSS Distributions 		// Note that appendBytes(NULL) zeros the data up to the desired length
4157*19c3b8c2SApple OSS Distributions 		size_t uplPageSize = dataP->fPageCnt * sizeof(upl_page_info_t);
4158*19c3b8c2SApple OSS Distributions 		if (uplPageSize > ((unsigned int)uplPageSize)) {
4159*19c3b8c2SApple OSS Distributions 			error = kIOReturnNoMemory;
4160*19c3b8c2SApple OSS Distributions 			traceInterval.setEndArg2(error);
4161*19c3b8c2SApple OSS Distributions 			return error;
4162*19c3b8c2SApple OSS Distributions 		}
4163*19c3b8c2SApple OSS Distributions 		if (!_memoryEntries->appendBytes(NULL, uplPageSize)) {
4164*19c3b8c2SApple OSS Distributions 			error = kIOReturnNoMemory;
4165*19c3b8c2SApple OSS Distributions 			traceInterval.setEndArg2(error);
4166*19c3b8c2SApple OSS Distributions 			return error;
4167*19c3b8c2SApple OSS Distributions 		}
4168*19c3b8c2SApple OSS Distributions 		dataP = NULL;
4169*19c3b8c2SApple OSS Distributions 
4170*19c3b8c2SApple OSS Distributions 		// Find the appropriate vm_map for the given task
4171*19c3b8c2SApple OSS Distributions 		vm_map_t curMap;
4172*19c3b8c2SApple OSS Distributions 		if ((NULL != _memRef) || ((_task == kernel_task && (kIOMemoryBufferPageable & _flags)))) {
4173*19c3b8c2SApple OSS Distributions 			curMap = NULL;
4174*19c3b8c2SApple OSS Distributions 		} else {
4175*19c3b8c2SApple OSS Distributions 			curMap = get_task_map(_task);
4176*19c3b8c2SApple OSS Distributions 		}
4177*19c3b8c2SApple OSS Distributions 
4178*19c3b8c2SApple OSS Distributions 		// Iterate over the vector of virtual ranges
4179*19c3b8c2SApple OSS Distributions 		Ranges vec = _ranges;
4180*19c3b8c2SApple OSS Distributions 		unsigned int pageIndex  = 0;
4181*19c3b8c2SApple OSS Distributions 		IOByteCount mdOffset    = 0;
4182*19c3b8c2SApple OSS Distributions 		ppnum_t highestPage     = 0;
4183*19c3b8c2SApple OSS Distributions 		bool         byteAlignUPL;
4184*19c3b8c2SApple OSS Distributions 
4185*19c3b8c2SApple OSS Distributions 		IOMemoryEntry * memRefEntry = NULL;
4186*19c3b8c2SApple OSS Distributions 		if (_memRef) {
4187*19c3b8c2SApple OSS Distributions 			memRefEntry = &_memRef->entries[0];
4188*19c3b8c2SApple OSS Distributions 			byteAlignUPL = (0 != (MAP_MEM_USE_DATA_ADDR & _memRef->prot));
4189*19c3b8c2SApple OSS Distributions 		} else {
4190*19c3b8c2SApple OSS Distributions 			byteAlignUPL = true;
4191*19c3b8c2SApple OSS Distributions 		}
4192*19c3b8c2SApple OSS Distributions 
4193*19c3b8c2SApple OSS Distributions 		for (UInt range = 0; mdOffset < _length; range++) {
4194*19c3b8c2SApple OSS Distributions 			ioPLBlock iopl;
4195*19c3b8c2SApple OSS Distributions 			mach_vm_address_t startPage, startPageOffset;
4196*19c3b8c2SApple OSS Distributions 			mach_vm_size_t    numBytes;
4197*19c3b8c2SApple OSS Distributions 			ppnum_t highPage = 0;
4198*19c3b8c2SApple OSS Distributions 
4199*19c3b8c2SApple OSS Distributions 			if (_memRef) {
4200*19c3b8c2SApple OSS Distributions 				if (range >= _memRef->count) {
4201*19c3b8c2SApple OSS Distributions 					panic("memRefEntry");
4202*19c3b8c2SApple OSS Distributions 				}
4203*19c3b8c2SApple OSS Distributions 				memRefEntry = &_memRef->entries[range];
4204*19c3b8c2SApple OSS Distributions 				numBytes    = memRefEntry->size;
4205*19c3b8c2SApple OSS Distributions 				startPage   = -1ULL;
4206*19c3b8c2SApple OSS Distributions 				if (byteAlignUPL) {
4207*19c3b8c2SApple OSS Distributions 					startPageOffset = 0;
4208*19c3b8c2SApple OSS Distributions 				} else {
4209*19c3b8c2SApple OSS Distributions 					startPageOffset = (memRefEntry->start & PAGE_MASK);
4210*19c3b8c2SApple OSS Distributions 				}
4211*19c3b8c2SApple OSS Distributions 			} else {
4212*19c3b8c2SApple OSS Distributions 				// Get the startPage address and length of vec[range]
4213*19c3b8c2SApple OSS Distributions 				getAddrLenForInd(startPage, numBytes, type, vec, range);
4214*19c3b8c2SApple OSS Distributions 				if (byteAlignUPL) {
4215*19c3b8c2SApple OSS Distributions 					startPageOffset = 0;
4216*19c3b8c2SApple OSS Distributions 				} else {
4217*19c3b8c2SApple OSS Distributions 					startPageOffset = startPage & PAGE_MASK;
4218*19c3b8c2SApple OSS Distributions 					startPage = trunc_page_64(startPage);
4219*19c3b8c2SApple OSS Distributions 				}
4220*19c3b8c2SApple OSS Distributions 			}
4221*19c3b8c2SApple OSS Distributions 			iopl.fPageOffset = (typeof(iopl.fPageOffset))startPageOffset;
4222*19c3b8c2SApple OSS Distributions 			numBytes += startPageOffset;
4223*19c3b8c2SApple OSS Distributions 
4224*19c3b8c2SApple OSS Distributions 			if (mapper) {
4225*19c3b8c2SApple OSS Distributions 				iopl.fMappedPage = mapBase + pageIndex;
4226*19c3b8c2SApple OSS Distributions 			} else {
4227*19c3b8c2SApple OSS Distributions 				iopl.fMappedPage = 0;
4228*19c3b8c2SApple OSS Distributions 			}
4229*19c3b8c2SApple OSS Distributions 
4230*19c3b8c2SApple OSS Distributions 			// Iterate over the current range, creating UPLs
4231*19c3b8c2SApple OSS Distributions 			while (numBytes) {
4232*19c3b8c2SApple OSS Distributions 				vm_address_t kernelStart = (vm_address_t) startPage;
4233*19c3b8c2SApple OSS Distributions 				vm_map_t theMap;
4234*19c3b8c2SApple OSS Distributions 				if (curMap) {
4235*19c3b8c2SApple OSS Distributions 					theMap = curMap;
4236*19c3b8c2SApple OSS Distributions 				} else if (_memRef) {
4237*19c3b8c2SApple OSS Distributions 					theMap = NULL;
4238*19c3b8c2SApple OSS Distributions 				} else {
4239*19c3b8c2SApple OSS Distributions 					assert(_task == kernel_task);
4240*19c3b8c2SApple OSS Distributions 					theMap = IOPageableMapForAddress(kernelStart);
4241*19c3b8c2SApple OSS Distributions 				}
4242*19c3b8c2SApple OSS Distributions 
4243*19c3b8c2SApple OSS Distributions 				// ioplFlags is an in/out parameter
4244*19c3b8c2SApple OSS Distributions 				upl_control_flags_t ioplFlags = uplFlags;
4245*19c3b8c2SApple OSS Distributions 				dataP = getDataP(_memoryEntries);
4246*19c3b8c2SApple OSS Distributions 				pageInfo = getPageList(dataP);
4247*19c3b8c2SApple OSS Distributions 				upl_page_list_ptr_t baseInfo = &pageInfo[pageIndex];
4248*19c3b8c2SApple OSS Distributions 
4249*19c3b8c2SApple OSS Distributions 				mach_vm_size_t ioplPhysSize;
4250*19c3b8c2SApple OSS Distributions 				upl_size_t     ioplSize;
4251*19c3b8c2SApple OSS Distributions 				unsigned int   numPageInfo;
4252*19c3b8c2SApple OSS Distributions 
4253*19c3b8c2SApple OSS Distributions 				if (_memRef) {
4254*19c3b8c2SApple OSS Distributions 					error = mach_memory_entry_map_size(memRefEntry->entry, NULL /*physical*/, 0, memRefEntry->size, &ioplPhysSize);
4255*19c3b8c2SApple OSS Distributions 					DEBUG4K_IOKIT("_memRef %p memRefEntry %p entry %p startPage 0x%llx numBytes 0x%llx ioplPhysSize 0x%llx\n", _memRef, memRefEntry, memRefEntry->entry, startPage, numBytes, ioplPhysSize);
4256*19c3b8c2SApple OSS Distributions 				} else {
4257*19c3b8c2SApple OSS Distributions 					error = vm_map_range_physical_size(theMap, startPage, numBytes, &ioplPhysSize);
4258*19c3b8c2SApple OSS Distributions 					DEBUG4K_IOKIT("_memRef %p theMap %p startPage 0x%llx numBytes 0x%llx ioplPhysSize 0x%llx\n", _memRef, theMap, startPage, numBytes, ioplPhysSize);
4259*19c3b8c2SApple OSS Distributions 				}
4260*19c3b8c2SApple OSS Distributions 				if (error != KERN_SUCCESS) {
4261*19c3b8c2SApple OSS Distributions 					if (_memRef) {
4262*19c3b8c2SApple OSS Distributions 						DEBUG4K_ERROR("_memRef %p memRefEntry %p entry %p theMap %p startPage 0x%llx numBytes 0x%llx error 0x%x\n", _memRef, memRefEntry, memRefEntry->entry, theMap, startPage, numBytes, error);
4263*19c3b8c2SApple OSS Distributions 					} else {
4264*19c3b8c2SApple OSS Distributions 						DEBUG4K_ERROR("_memRef %p theMap %p startPage 0x%llx numBytes 0x%llx error 0x%x\n", _memRef, theMap, startPage, numBytes, error);
4265*19c3b8c2SApple OSS Distributions 					}
4266*19c3b8c2SApple OSS Distributions 					printf("entry size error %d\n", error);
4267*19c3b8c2SApple OSS Distributions 					goto abortExit;
4268*19c3b8c2SApple OSS Distributions 				}
4269*19c3b8c2SApple OSS Distributions 				ioplPhysSize    = (ioplPhysSize <= MAX_UPL_SIZE_BYTES) ? ioplPhysSize : MAX_UPL_SIZE_BYTES;
4270*19c3b8c2SApple OSS Distributions 				numPageInfo = atop_32(ioplPhysSize);
4271*19c3b8c2SApple OSS Distributions 				if (byteAlignUPL) {
4272*19c3b8c2SApple OSS Distributions 					if (numBytes > ioplPhysSize) {
4273*19c3b8c2SApple OSS Distributions 						ioplSize = ((typeof(ioplSize))ioplPhysSize);
4274*19c3b8c2SApple OSS Distributions 					} else {
4275*19c3b8c2SApple OSS Distributions 						ioplSize = ((typeof(ioplSize))numBytes);
4276*19c3b8c2SApple OSS Distributions 					}
4277*19c3b8c2SApple OSS Distributions 				} else {
4278*19c3b8c2SApple OSS Distributions 					ioplSize = ((typeof(ioplSize))ioplPhysSize);
4279*19c3b8c2SApple OSS Distributions 				}
4280*19c3b8c2SApple OSS Distributions 
4281*19c3b8c2SApple OSS Distributions 				if (_memRef) {
4282*19c3b8c2SApple OSS Distributions 					memory_object_offset_t entryOffset;
4283*19c3b8c2SApple OSS Distributions 
4284*19c3b8c2SApple OSS Distributions 					entryOffset = mdOffset;
4285*19c3b8c2SApple OSS Distributions 					if (byteAlignUPL) {
4286*19c3b8c2SApple OSS Distributions 						entryOffset = (entryOffset - memRefEntry->offset);
4287*19c3b8c2SApple OSS Distributions 					} else {
4288*19c3b8c2SApple OSS Distributions 						entryOffset = (entryOffset - iopl.fPageOffset - memRefEntry->offset);
4289*19c3b8c2SApple OSS Distributions 					}
4290*19c3b8c2SApple OSS Distributions 					if (ioplSize > (memRefEntry->size - entryOffset)) {
4291*19c3b8c2SApple OSS Distributions 						ioplSize =  ((typeof(ioplSize))(memRefEntry->size - entryOffset));
4292*19c3b8c2SApple OSS Distributions 					}
4293*19c3b8c2SApple OSS Distributions 					error = memory_object_iopl_request(memRefEntry->entry,
4294*19c3b8c2SApple OSS Distributions 					    entryOffset,
4295*19c3b8c2SApple OSS Distributions 					    &ioplSize,
4296*19c3b8c2SApple OSS Distributions 					    &iopl.fIOPL,
4297*19c3b8c2SApple OSS Distributions 					    baseInfo,
4298*19c3b8c2SApple OSS Distributions 					    &numPageInfo,
4299*19c3b8c2SApple OSS Distributions 					    &ioplFlags,
4300*19c3b8c2SApple OSS Distributions 					    tag);
4301*19c3b8c2SApple OSS Distributions 				} else if ((theMap == kernel_map)
4302*19c3b8c2SApple OSS Distributions 				    && (kernelStart >= io_kernel_static_start)
4303*19c3b8c2SApple OSS Distributions 				    && (kernelStart < io_kernel_static_end)) {
4304*19c3b8c2SApple OSS Distributions 					error = io_get_kernel_static_upl(theMap,
4305*19c3b8c2SApple OSS Distributions 					    kernelStart,
4306*19c3b8c2SApple OSS Distributions 					    &ioplSize,
4307*19c3b8c2SApple OSS Distributions 					    &iopl.fPageOffset,
4308*19c3b8c2SApple OSS Distributions 					    &iopl.fIOPL,
4309*19c3b8c2SApple OSS Distributions 					    baseInfo,
4310*19c3b8c2SApple OSS Distributions 					    &numPageInfo,
4311*19c3b8c2SApple OSS Distributions 					    &highPage);
4312*19c3b8c2SApple OSS Distributions 				} else {
4313*19c3b8c2SApple OSS Distributions 					assert(theMap);
4314*19c3b8c2SApple OSS Distributions 					error = vm_map_create_upl(theMap,
4315*19c3b8c2SApple OSS Distributions 					    startPage,
4316*19c3b8c2SApple OSS Distributions 					    (upl_size_t*)&ioplSize,
4317*19c3b8c2SApple OSS Distributions 					    &iopl.fIOPL,
4318*19c3b8c2SApple OSS Distributions 					    baseInfo,
4319*19c3b8c2SApple OSS Distributions 					    &numPageInfo,
4320*19c3b8c2SApple OSS Distributions 					    &ioplFlags,
4321*19c3b8c2SApple OSS Distributions 					    tag);
4322*19c3b8c2SApple OSS Distributions 				}
4323*19c3b8c2SApple OSS Distributions 
4324*19c3b8c2SApple OSS Distributions 				if (error != KERN_SUCCESS) {
4325*19c3b8c2SApple OSS Distributions 					traceInterval.setEndArg2(error);
4326*19c3b8c2SApple OSS Distributions 					DEBUG4K_ERROR("UPL create error 0x%x theMap %p (kernel:%d) _memRef %p startPage 0x%llx ioplSize 0x%x\n", error, theMap, (theMap == kernel_map), _memRef, startPage, ioplSize);
4327*19c3b8c2SApple OSS Distributions 					goto abortExit;
4328*19c3b8c2SApple OSS Distributions 				}
4329*19c3b8c2SApple OSS Distributions 
4330*19c3b8c2SApple OSS Distributions 				assert(ioplSize);
4331*19c3b8c2SApple OSS Distributions 
4332*19c3b8c2SApple OSS Distributions 				if (iopl.fIOPL) {
4333*19c3b8c2SApple OSS Distributions 					highPage = upl_get_highest_page(iopl.fIOPL);
4334*19c3b8c2SApple OSS Distributions 				}
4335*19c3b8c2SApple OSS Distributions 				if (highPage > highestPage) {
4336*19c3b8c2SApple OSS Distributions 					highestPage = highPage;
4337*19c3b8c2SApple OSS Distributions 				}
4338*19c3b8c2SApple OSS Distributions 
4339*19c3b8c2SApple OSS Distributions 				if (baseInfo->device) {
4340*19c3b8c2SApple OSS Distributions 					numPageInfo = 1;
4341*19c3b8c2SApple OSS Distributions 					iopl.fFlags = kIOPLOnDevice;
4342*19c3b8c2SApple OSS Distributions 				} else {
4343*19c3b8c2SApple OSS Distributions 					iopl.fFlags = 0;
4344*19c3b8c2SApple OSS Distributions 				}
4345*19c3b8c2SApple OSS Distributions 
4346*19c3b8c2SApple OSS Distributions 				if (byteAlignUPL) {
4347*19c3b8c2SApple OSS Distributions 					if (iopl.fIOPL) {
4348*19c3b8c2SApple OSS Distributions 						DEBUG4K_UPL("startPage 0x%llx numBytes 0x%llx iopl.fPageOffset 0x%x upl_get_data_offset(%p) 0x%llx\n", startPage, numBytes, iopl.fPageOffset, iopl.fIOPL, upl_get_data_offset(iopl.fIOPL));
4349*19c3b8c2SApple OSS Distributions 						iopl.fPageOffset = (typeof(iopl.fPageOffset))upl_get_data_offset(iopl.fIOPL);
4350*19c3b8c2SApple OSS Distributions 					}
4351*19c3b8c2SApple OSS Distributions 					if (startPage != (mach_vm_address_t)-1) {
4352*19c3b8c2SApple OSS Distributions 						// assert(iopl.fPageOffset == (startPage & PAGE_MASK));
4353*19c3b8c2SApple OSS Distributions 						startPage -= iopl.fPageOffset;
4354*19c3b8c2SApple OSS Distributions 					}
4355*19c3b8c2SApple OSS Distributions 					ioplSize = ((typeof(ioplSize))ptoa_64(numPageInfo));
4356*19c3b8c2SApple OSS Distributions 					numBytes += iopl.fPageOffset;
4357*19c3b8c2SApple OSS Distributions 				}
4358*19c3b8c2SApple OSS Distributions 
4359*19c3b8c2SApple OSS Distributions 				iopl.fIOMDOffset = mdOffset;
4360*19c3b8c2SApple OSS Distributions 				iopl.fPageInfo = pageIndex;
4361*19c3b8c2SApple OSS Distributions 
4362*19c3b8c2SApple OSS Distributions 				if (!_memoryEntries->appendBytes(&iopl, sizeof(iopl))) {
4363*19c3b8c2SApple OSS Distributions 					// Clean up partial created and unsaved iopl
4364*19c3b8c2SApple OSS Distributions 					if (iopl.fIOPL) {
4365*19c3b8c2SApple OSS Distributions 						upl_abort(iopl.fIOPL, 0);
4366*19c3b8c2SApple OSS Distributions 						upl_deallocate(iopl.fIOPL);
4367*19c3b8c2SApple OSS Distributions 					}
4368*19c3b8c2SApple OSS Distributions 					error = kIOReturnNoMemory;
4369*19c3b8c2SApple OSS Distributions 					traceInterval.setEndArg2(error);
4370*19c3b8c2SApple OSS Distributions 					goto abortExit;
4371*19c3b8c2SApple OSS Distributions 				}
4372*19c3b8c2SApple OSS Distributions 				dataP = NULL;
4373*19c3b8c2SApple OSS Distributions 
4374*19c3b8c2SApple OSS Distributions 				// Check for a multiple iopl's in one virtual range
4375*19c3b8c2SApple OSS Distributions 				pageIndex += numPageInfo;
4376*19c3b8c2SApple OSS Distributions 				mdOffset -= iopl.fPageOffset;
4377*19c3b8c2SApple OSS Distributions 				numBytesWired += ioplSize;
4378*19c3b8c2SApple OSS Distributions 				if (ioplSize < numBytes) {
4379*19c3b8c2SApple OSS Distributions 					numBytes -= ioplSize;
4380*19c3b8c2SApple OSS Distributions 					if (startPage != (mach_vm_address_t)-1) {
4381*19c3b8c2SApple OSS Distributions 						startPage += ioplSize;
4382*19c3b8c2SApple OSS Distributions 					}
4383*19c3b8c2SApple OSS Distributions 					mdOffset += ioplSize;
4384*19c3b8c2SApple OSS Distributions 					iopl.fPageOffset = 0;
4385*19c3b8c2SApple OSS Distributions 					if (mapper) {
4386*19c3b8c2SApple OSS Distributions 						iopl.fMappedPage = mapBase + pageIndex;
4387*19c3b8c2SApple OSS Distributions 					}
4388*19c3b8c2SApple OSS Distributions 				} else {
4389*19c3b8c2SApple OSS Distributions 					mdOffset += numBytes;
4390*19c3b8c2SApple OSS Distributions 					break;
4391*19c3b8c2SApple OSS Distributions 				}
4392*19c3b8c2SApple OSS Distributions 			}
4393*19c3b8c2SApple OSS Distributions 		}
4394*19c3b8c2SApple OSS Distributions 
4395*19c3b8c2SApple OSS Distributions 		_highestPage = highestPage;
4396*19c3b8c2SApple OSS Distributions 		DEBUG4K_IOKIT("-> _highestPage 0x%x\n", _highestPage);
4397*19c3b8c2SApple OSS Distributions 
4398*19c3b8c2SApple OSS Distributions 		if (UPL_COPYOUT_FROM & uplFlags) {
4399*19c3b8c2SApple OSS Distributions 			_flags |= kIOMemoryPreparedReadOnly;
4400*19c3b8c2SApple OSS Distributions 		}
4401*19c3b8c2SApple OSS Distributions 		traceInterval.setEndCodes(numBytesWired, error);
4402*19c3b8c2SApple OSS Distributions 	}
4403*19c3b8c2SApple OSS Distributions 
4404*19c3b8c2SApple OSS Distributions #if IOTRACKING
4405*19c3b8c2SApple OSS Distributions 	if (!(_flags & kIOMemoryAutoPrepare) && (kIOReturnSuccess == error)) {
4406*19c3b8c2SApple OSS Distributions 		dataP = getDataP(_memoryEntries);
4407*19c3b8c2SApple OSS Distributions 		if (!dataP->fWireTracking.link.next) {
4408*19c3b8c2SApple OSS Distributions 			IOTrackingAdd(gIOWireTracking, &dataP->fWireTracking, ptoa(_pages), false, tag);
4409*19c3b8c2SApple OSS Distributions 		}
4410*19c3b8c2SApple OSS Distributions 	}
4411*19c3b8c2SApple OSS Distributions #endif /* IOTRACKING */
4412*19c3b8c2SApple OSS Distributions 
4413*19c3b8c2SApple OSS Distributions 	return error;
4414*19c3b8c2SApple OSS Distributions 
4415*19c3b8c2SApple OSS Distributions abortExit:
4416*19c3b8c2SApple OSS Distributions 	{
4417*19c3b8c2SApple OSS Distributions 		dataP = getDataP(_memoryEntries);
4418*19c3b8c2SApple OSS Distributions 		UInt done = getNumIOPL(_memoryEntries, dataP);
4419*19c3b8c2SApple OSS Distributions 		ioPLBlock *ioplList = getIOPLList(dataP);
4420*19c3b8c2SApple OSS Distributions 
4421*19c3b8c2SApple OSS Distributions 		for (UInt ioplIdx = 0; ioplIdx < done; ioplIdx++) {
4422*19c3b8c2SApple OSS Distributions 			if (ioplList[ioplIdx].fIOPL) {
4423*19c3b8c2SApple OSS Distributions 				upl_abort(ioplList[ioplIdx].fIOPL, 0);
4424*19c3b8c2SApple OSS Distributions 				upl_deallocate(ioplList[ioplIdx].fIOPL);
4425*19c3b8c2SApple OSS Distributions 			}
4426*19c3b8c2SApple OSS Distributions 		}
4427*19c3b8c2SApple OSS Distributions 		_memoryEntries->setLength(computeDataSize(0, 0));
4428*19c3b8c2SApple OSS Distributions 	}
4429*19c3b8c2SApple OSS Distributions 
4430*19c3b8c2SApple OSS Distributions 	if (error == KERN_FAILURE) {
4431*19c3b8c2SApple OSS Distributions 		error = kIOReturnCannotWire;
4432*19c3b8c2SApple OSS Distributions 	} else if (error == KERN_MEMORY_ERROR) {
4433*19c3b8c2SApple OSS Distributions 		error = kIOReturnNoResources;
4434*19c3b8c2SApple OSS Distributions 	}
4435*19c3b8c2SApple OSS Distributions 
4436*19c3b8c2SApple OSS Distributions 	return error;
4437*19c3b8c2SApple OSS Distributions }
4438*19c3b8c2SApple OSS Distributions 
4439*19c3b8c2SApple OSS Distributions bool
initMemoryEntries(size_t size,IOMapper * mapper)4440*19c3b8c2SApple OSS Distributions IOGeneralMemoryDescriptor::initMemoryEntries(size_t size, IOMapper * mapper)
4441*19c3b8c2SApple OSS Distributions {
4442*19c3b8c2SApple OSS Distributions 	ioGMDData * dataP;
4443*19c3b8c2SApple OSS Distributions 
4444*19c3b8c2SApple OSS Distributions 	if (size > UINT_MAX) {
4445*19c3b8c2SApple OSS Distributions 		return false;
4446*19c3b8c2SApple OSS Distributions 	}
4447*19c3b8c2SApple OSS Distributions 	if (!_memoryEntries) {
4448*19c3b8c2SApple OSS Distributions 		_memoryEntries = _IOMemoryDescriptorMixedData::withCapacity(size);
4449*19c3b8c2SApple OSS Distributions 		if (!_memoryEntries) {
4450*19c3b8c2SApple OSS Distributions 			return false;
4451*19c3b8c2SApple OSS Distributions 		}
4452*19c3b8c2SApple OSS Distributions 	} else if (!_memoryEntries->initWithCapacity(size)) {
4453*19c3b8c2SApple OSS Distributions 		return false;
4454*19c3b8c2SApple OSS Distributions 	}
4455*19c3b8c2SApple OSS Distributions 
4456*19c3b8c2SApple OSS Distributions 	_memoryEntries->appendBytes(NULL, computeDataSize(0, 0));
4457*19c3b8c2SApple OSS Distributions 	dataP = getDataP(_memoryEntries);
4458*19c3b8c2SApple OSS Distributions 
4459*19c3b8c2SApple OSS Distributions 	if (mapper == kIOMapperWaitSystem) {
4460*19c3b8c2SApple OSS Distributions 		IOMapper::checkForSystemMapper();
4461*19c3b8c2SApple OSS Distributions 		mapper = IOMapper::gSystem;
4462*19c3b8c2SApple OSS Distributions 	}
4463*19c3b8c2SApple OSS Distributions 	dataP->fMapper               = mapper;
4464*19c3b8c2SApple OSS Distributions 	dataP->fPageCnt              = 0;
4465*19c3b8c2SApple OSS Distributions 	dataP->fMappedBase           = 0;
4466*19c3b8c2SApple OSS Distributions 	dataP->fDMAMapNumAddressBits = 64;
4467*19c3b8c2SApple OSS Distributions 	dataP->fDMAMapAlignment      = 0;
4468*19c3b8c2SApple OSS Distributions 	dataP->fPreparationID        = kIOPreparationIDUnprepared;
4469*19c3b8c2SApple OSS Distributions 	dataP->fCompletionError      = false;
4470*19c3b8c2SApple OSS Distributions 	dataP->fMappedBaseValid      = false;
4471*19c3b8c2SApple OSS Distributions 
4472*19c3b8c2SApple OSS Distributions 	return true;
4473*19c3b8c2SApple OSS Distributions }
4474*19c3b8c2SApple OSS Distributions 
4475*19c3b8c2SApple OSS Distributions IOReturn
dmaMap(IOMapper * mapper,IOMemoryDescriptor * memory,IODMACommand * command,const IODMAMapSpecification * mapSpec,uint64_t offset,uint64_t length,uint64_t * mapAddress,uint64_t * mapLength)4476*19c3b8c2SApple OSS Distributions IOMemoryDescriptor::dmaMap(
4477*19c3b8c2SApple OSS Distributions 	IOMapper                    * mapper,
4478*19c3b8c2SApple OSS Distributions 	IOMemoryDescriptor          * memory,
4479*19c3b8c2SApple OSS Distributions 	IODMACommand                * command,
4480*19c3b8c2SApple OSS Distributions 	const IODMAMapSpecification * mapSpec,
4481*19c3b8c2SApple OSS Distributions 	uint64_t                      offset,
4482*19c3b8c2SApple OSS Distributions 	uint64_t                      length,
4483*19c3b8c2SApple OSS Distributions 	uint64_t                    * mapAddress,
4484*19c3b8c2SApple OSS Distributions 	uint64_t                    * mapLength)
4485*19c3b8c2SApple OSS Distributions {
4486*19c3b8c2SApple OSS Distributions 	IOReturn err;
4487*19c3b8c2SApple OSS Distributions 	uint32_t mapOptions;
4488*19c3b8c2SApple OSS Distributions 
4489*19c3b8c2SApple OSS Distributions 	mapOptions = 0;
4490*19c3b8c2SApple OSS Distributions 	mapOptions |= kIODMAMapReadAccess;
4491*19c3b8c2SApple OSS Distributions 	if (!(kIOMemoryPreparedReadOnly & _flags)) {
4492*19c3b8c2SApple OSS Distributions 		mapOptions |= kIODMAMapWriteAccess;
4493*19c3b8c2SApple OSS Distributions 	}
4494*19c3b8c2SApple OSS Distributions 
4495*19c3b8c2SApple OSS Distributions 	err = mapper->iovmMapMemory(memory, offset, length, mapOptions,
4496*19c3b8c2SApple OSS Distributions 	    mapSpec, command, NULL, mapAddress, mapLength);
4497*19c3b8c2SApple OSS Distributions 
4498*19c3b8c2SApple OSS Distributions 	if (kIOReturnSuccess == err) {
4499*19c3b8c2SApple OSS Distributions 		dmaMapRecord(mapper, command, *mapLength);
4500*19c3b8c2SApple OSS Distributions 	}
4501*19c3b8c2SApple OSS Distributions 
4502*19c3b8c2SApple OSS Distributions 	return err;
4503*19c3b8c2SApple OSS Distributions }
4504*19c3b8c2SApple OSS Distributions 
4505*19c3b8c2SApple OSS Distributions void
dmaMapRecord(IOMapper * mapper,IODMACommand * command,uint64_t mapLength)4506*19c3b8c2SApple OSS Distributions IOMemoryDescriptor::dmaMapRecord(
4507*19c3b8c2SApple OSS Distributions 	IOMapper                    * mapper,
4508*19c3b8c2SApple OSS Distributions 	IODMACommand                * command,
4509*19c3b8c2SApple OSS Distributions 	uint64_t                      mapLength)
4510*19c3b8c2SApple OSS Distributions {
4511*19c3b8c2SApple OSS Distributions 	IOTimeStampIntervalConstantFiltered traceInterval(IODBG_MDESC(IOMDESC_DMA_MAP), VM_KERNEL_ADDRHIDE(this));
4512*19c3b8c2SApple OSS Distributions 	kern_allocation_name_t alloc;
4513*19c3b8c2SApple OSS Distributions 	int16_t                prior;
4514*19c3b8c2SApple OSS Distributions 
4515*19c3b8c2SApple OSS Distributions 	if ((alloc = mapper->fAllocName) /* && mapper != IOMapper::gSystem */) {
4516*19c3b8c2SApple OSS Distributions 		kern_allocation_update_size(mapper->fAllocName, mapLength);
4517*19c3b8c2SApple OSS Distributions 	}
4518*19c3b8c2SApple OSS Distributions 
4519*19c3b8c2SApple OSS Distributions 	if (!command) {
4520*19c3b8c2SApple OSS Distributions 		return;
4521*19c3b8c2SApple OSS Distributions 	}
4522*19c3b8c2SApple OSS Distributions 	prior = OSAddAtomic16(1, &_dmaReferences);
4523*19c3b8c2SApple OSS Distributions 	if (!prior) {
4524*19c3b8c2SApple OSS Distributions 		if (alloc && (VM_KERN_MEMORY_NONE != _kernelTag)) {
4525*19c3b8c2SApple OSS Distributions 			_mapName  = alloc;
4526*19c3b8c2SApple OSS Distributions 			mapLength = _length;
4527*19c3b8c2SApple OSS Distributions 			kern_allocation_update_subtotal(alloc, _kernelTag, mapLength);
4528*19c3b8c2SApple OSS Distributions 		} else {
4529*19c3b8c2SApple OSS Distributions 			_mapName = NULL;
4530*19c3b8c2SApple OSS Distributions 		}
4531*19c3b8c2SApple OSS Distributions 	}
4532*19c3b8c2SApple OSS Distributions }
4533*19c3b8c2SApple OSS Distributions 
4534*19c3b8c2SApple OSS Distributions IOReturn
dmaUnmap(IOMapper * mapper,IODMACommand * command,uint64_t offset,uint64_t mapAddress,uint64_t mapLength)4535*19c3b8c2SApple OSS Distributions IOMemoryDescriptor::dmaUnmap(
4536*19c3b8c2SApple OSS Distributions 	IOMapper                    * mapper,
4537*19c3b8c2SApple OSS Distributions 	IODMACommand                * command,
4538*19c3b8c2SApple OSS Distributions 	uint64_t                      offset,
4539*19c3b8c2SApple OSS Distributions 	uint64_t                      mapAddress,
4540*19c3b8c2SApple OSS Distributions 	uint64_t                      mapLength)
4541*19c3b8c2SApple OSS Distributions {
4542*19c3b8c2SApple OSS Distributions 	IOTimeStampIntervalConstantFiltered traceInterval(IODBG_MDESC(IOMDESC_DMA_UNMAP), VM_KERNEL_ADDRHIDE(this));
4543*19c3b8c2SApple OSS Distributions 	IOReturn ret;
4544*19c3b8c2SApple OSS Distributions 	kern_allocation_name_t alloc;
4545*19c3b8c2SApple OSS Distributions 	kern_allocation_name_t mapName;
4546*19c3b8c2SApple OSS Distributions 	int16_t prior;
4547*19c3b8c2SApple OSS Distributions 
4548*19c3b8c2SApple OSS Distributions 	mapName = NULL;
4549*19c3b8c2SApple OSS Distributions 	prior = 0;
4550*19c3b8c2SApple OSS Distributions 	if (command) {
4551*19c3b8c2SApple OSS Distributions 		mapName = _mapName;
4552*19c3b8c2SApple OSS Distributions 		if (_dmaReferences) {
4553*19c3b8c2SApple OSS Distributions 			prior = OSAddAtomic16(-1, &_dmaReferences);
4554*19c3b8c2SApple OSS Distributions 		} else {
4555*19c3b8c2SApple OSS Distributions 			panic("_dmaReferences underflow");
4556*19c3b8c2SApple OSS Distributions 		}
4557*19c3b8c2SApple OSS Distributions 	}
4558*19c3b8c2SApple OSS Distributions 
4559*19c3b8c2SApple OSS Distributions 	if (!mapLength) {
4560*19c3b8c2SApple OSS Distributions 		traceInterval.setEndArg1(kIOReturnSuccess);
4561*19c3b8c2SApple OSS Distributions 		return kIOReturnSuccess;
4562*19c3b8c2SApple OSS Distributions 	}
4563*19c3b8c2SApple OSS Distributions 
4564*19c3b8c2SApple OSS Distributions 	ret = mapper->iovmUnmapMemory(this, command, mapAddress, mapLength);
4565*19c3b8c2SApple OSS Distributions 
4566*19c3b8c2SApple OSS Distributions 	if ((alloc = mapper->fAllocName)) {
4567*19c3b8c2SApple OSS Distributions 		kern_allocation_update_size(alloc, -mapLength);
4568*19c3b8c2SApple OSS Distributions 		if ((1 == prior) && mapName && (VM_KERN_MEMORY_NONE != _kernelTag)) {
4569*19c3b8c2SApple OSS Distributions 			mapLength = _length;
4570*19c3b8c2SApple OSS Distributions 			kern_allocation_update_subtotal(mapName, _kernelTag, -mapLength);
4571*19c3b8c2SApple OSS Distributions 		}
4572*19c3b8c2SApple OSS Distributions 	}
4573*19c3b8c2SApple OSS Distributions 
4574*19c3b8c2SApple OSS Distributions 	traceInterval.setEndArg1(ret);
4575*19c3b8c2SApple OSS Distributions 	return ret;
4576*19c3b8c2SApple OSS Distributions }
4577*19c3b8c2SApple OSS Distributions 
4578*19c3b8c2SApple OSS Distributions IOReturn
dmaMap(IOMapper * mapper,IOMemoryDescriptor * memory,IODMACommand * command,const IODMAMapSpecification * mapSpec,uint64_t offset,uint64_t length,uint64_t * mapAddress,uint64_t * mapLength)4579*19c3b8c2SApple OSS Distributions IOGeneralMemoryDescriptor::dmaMap(
4580*19c3b8c2SApple OSS Distributions 	IOMapper                    * mapper,
4581*19c3b8c2SApple OSS Distributions 	IOMemoryDescriptor          * memory,
4582*19c3b8c2SApple OSS Distributions 	IODMACommand                * command,
4583*19c3b8c2SApple OSS Distributions 	const IODMAMapSpecification * mapSpec,
4584*19c3b8c2SApple OSS Distributions 	uint64_t                      offset,
4585*19c3b8c2SApple OSS Distributions 	uint64_t                      length,
4586*19c3b8c2SApple OSS Distributions 	uint64_t                    * mapAddress,
4587*19c3b8c2SApple OSS Distributions 	uint64_t                    * mapLength)
4588*19c3b8c2SApple OSS Distributions {
4589*19c3b8c2SApple OSS Distributions 	IOReturn          err = kIOReturnSuccess;
4590*19c3b8c2SApple OSS Distributions 	ioGMDData *       dataP;
4591*19c3b8c2SApple OSS Distributions 	IOOptionBits      type = _flags & kIOMemoryTypeMask;
4592*19c3b8c2SApple OSS Distributions 
4593*19c3b8c2SApple OSS Distributions 	*mapAddress = 0;
4594*19c3b8c2SApple OSS Distributions 	if (kIOMemoryHostOnly & _flags) {
4595*19c3b8c2SApple OSS Distributions 		return kIOReturnSuccess;
4596*19c3b8c2SApple OSS Distributions 	}
4597*19c3b8c2SApple OSS Distributions 	if (kIOMemoryRemote & _flags) {
4598*19c3b8c2SApple OSS Distributions 		return kIOReturnNotAttached;
4599*19c3b8c2SApple OSS Distributions 	}
4600*19c3b8c2SApple OSS Distributions 
4601*19c3b8c2SApple OSS Distributions 	if ((type == kIOMemoryTypePhysical) || (type == kIOMemoryTypePhysical64)
4602*19c3b8c2SApple OSS Distributions 	    || offset || (length != _length)) {
4603*19c3b8c2SApple OSS Distributions 		err = super::dmaMap(mapper, memory, command, mapSpec, offset, length, mapAddress, mapLength);
4604*19c3b8c2SApple OSS Distributions 	} else if (_memoryEntries && _pages && (dataP = getDataP(_memoryEntries))) {
4605*19c3b8c2SApple OSS Distributions 		const ioPLBlock * ioplList = getIOPLList(dataP);
4606*19c3b8c2SApple OSS Distributions 		upl_page_info_t * pageList;
4607*19c3b8c2SApple OSS Distributions 		uint32_t          mapOptions = 0;
4608*19c3b8c2SApple OSS Distributions 
4609*19c3b8c2SApple OSS Distributions 		IODMAMapSpecification mapSpec;
4610*19c3b8c2SApple OSS Distributions 		bzero(&mapSpec, sizeof(mapSpec));
4611*19c3b8c2SApple OSS Distributions 		mapSpec.numAddressBits = dataP->fDMAMapNumAddressBits;
4612*19c3b8c2SApple OSS Distributions 		mapSpec.alignment = dataP->fDMAMapAlignment;
4613*19c3b8c2SApple OSS Distributions 
4614*19c3b8c2SApple OSS Distributions 		// For external UPLs the fPageInfo field points directly to
4615*19c3b8c2SApple OSS Distributions 		// the upl's upl_page_info_t array.
4616*19c3b8c2SApple OSS Distributions 		if (ioplList->fFlags & kIOPLExternUPL) {
4617*19c3b8c2SApple OSS Distributions 			pageList = (upl_page_info_t *) ioplList->fPageInfo;
4618*19c3b8c2SApple OSS Distributions 			mapOptions |= kIODMAMapPagingPath;
4619*19c3b8c2SApple OSS Distributions 		} else {
4620*19c3b8c2SApple OSS Distributions 			pageList = getPageList(dataP);
4621*19c3b8c2SApple OSS Distributions 		}
4622*19c3b8c2SApple OSS Distributions 
4623*19c3b8c2SApple OSS Distributions 		if ((_length == ptoa_64(_pages)) && !(page_mask & ioplList->fPageOffset)) {
4624*19c3b8c2SApple OSS Distributions 			mapOptions |= kIODMAMapPageListFullyOccupied;
4625*19c3b8c2SApple OSS Distributions 		}
4626*19c3b8c2SApple OSS Distributions 
4627*19c3b8c2SApple OSS Distributions 		assert(dataP->fDMAAccess);
4628*19c3b8c2SApple OSS Distributions 		mapOptions |= dataP->fDMAAccess;
4629*19c3b8c2SApple OSS Distributions 
4630*19c3b8c2SApple OSS Distributions 		// Check for direct device non-paged memory
4631*19c3b8c2SApple OSS Distributions 		if (ioplList->fFlags & kIOPLOnDevice) {
4632*19c3b8c2SApple OSS Distributions 			mapOptions |= kIODMAMapPhysicallyContiguous;
4633*19c3b8c2SApple OSS Distributions 		}
4634*19c3b8c2SApple OSS Distributions 
4635*19c3b8c2SApple OSS Distributions 		IODMAMapPageList dmaPageList =
4636*19c3b8c2SApple OSS Distributions 		{
4637*19c3b8c2SApple OSS Distributions 			.pageOffset    = (uint32_t)(ioplList->fPageOffset & page_mask),
4638*19c3b8c2SApple OSS Distributions 			.pageListCount = _pages,
4639*19c3b8c2SApple OSS Distributions 			.pageList      = &pageList[0]
4640*19c3b8c2SApple OSS Distributions 		};
4641*19c3b8c2SApple OSS Distributions 		err = mapper->iovmMapMemory(memory, offset, length, mapOptions, &mapSpec,
4642*19c3b8c2SApple OSS Distributions 		    command, &dmaPageList, mapAddress, mapLength);
4643*19c3b8c2SApple OSS Distributions 
4644*19c3b8c2SApple OSS Distributions 		if (kIOReturnSuccess == err) {
4645*19c3b8c2SApple OSS Distributions 			dmaMapRecord(mapper, command, *mapLength);
4646*19c3b8c2SApple OSS Distributions 		}
4647*19c3b8c2SApple OSS Distributions 	}
4648*19c3b8c2SApple OSS Distributions 
4649*19c3b8c2SApple OSS Distributions 	return err;
4650*19c3b8c2SApple OSS Distributions }
4651*19c3b8c2SApple OSS Distributions 
4652*19c3b8c2SApple OSS Distributions /*
4653*19c3b8c2SApple OSS Distributions  * prepare
4654*19c3b8c2SApple OSS Distributions  *
4655*19c3b8c2SApple OSS Distributions  * Prepare the memory for an I/O transfer.  This involves paging in
4656*19c3b8c2SApple OSS Distributions  * the memory, if necessary, and wiring it down for the duration of
4657*19c3b8c2SApple OSS Distributions  * the transfer.  The complete() method completes the processing of
4658*19c3b8c2SApple OSS Distributions  * the memory after the I/O transfer finishes.  This method needn't
4659*19c3b8c2SApple OSS Distributions  * called for non-pageable memory.
4660*19c3b8c2SApple OSS Distributions  */
4661*19c3b8c2SApple OSS Distributions 
4662*19c3b8c2SApple OSS Distributions IOReturn
prepare(IODirection forDirection)4663*19c3b8c2SApple OSS Distributions IOGeneralMemoryDescriptor::prepare(IODirection forDirection)
4664*19c3b8c2SApple OSS Distributions {
4665*19c3b8c2SApple OSS Distributions 	IOReturn     error    = kIOReturnSuccess;
4666*19c3b8c2SApple OSS Distributions 	IOOptionBits type = _flags & kIOMemoryTypeMask;
4667*19c3b8c2SApple OSS Distributions 	IOTimeStampIntervalConstantFiltered traceInterval(IODBG_MDESC(IOMDESC_PREPARE), VM_KERNEL_ADDRHIDE(this), forDirection);
4668*19c3b8c2SApple OSS Distributions 
4669*19c3b8c2SApple OSS Distributions 	if ((kIOMemoryTypePhysical == type) || (kIOMemoryTypePhysical64 == type)) {
4670*19c3b8c2SApple OSS Distributions 		traceInterval.setEndArg1(kIOReturnSuccess);
4671*19c3b8c2SApple OSS Distributions 		return kIOReturnSuccess;
4672*19c3b8c2SApple OSS Distributions 	}
4673*19c3b8c2SApple OSS Distributions 
4674*19c3b8c2SApple OSS Distributions 	assert(!(kIOMemoryRemote & _flags));
4675*19c3b8c2SApple OSS Distributions 	if (kIOMemoryRemote & _flags) {
4676*19c3b8c2SApple OSS Distributions 		traceInterval.setEndArg1(kIOReturnNotAttached);
4677*19c3b8c2SApple OSS Distributions 		return kIOReturnNotAttached;
4678*19c3b8c2SApple OSS Distributions 	}
4679*19c3b8c2SApple OSS Distributions 
4680*19c3b8c2SApple OSS Distributions 	if (_prepareLock) {
4681*19c3b8c2SApple OSS Distributions 		IOLockLock(_prepareLock);
4682*19c3b8c2SApple OSS Distributions 	}
4683*19c3b8c2SApple OSS Distributions 
4684*19c3b8c2SApple OSS Distributions 	if (kIOMemoryTypeVirtual == type || kIOMemoryTypeVirtual64 == type || kIOMemoryTypeUIO == type) {
4685*19c3b8c2SApple OSS Distributions 		if ((forDirection & kIODirectionPrepareAvoidThrottling) && NEED_TO_HARD_THROTTLE_THIS_TASK()) {
4686*19c3b8c2SApple OSS Distributions 			error = kIOReturnNotReady;
4687*19c3b8c2SApple OSS Distributions 			goto finish;
4688*19c3b8c2SApple OSS Distributions 		}
4689*19c3b8c2SApple OSS Distributions 		error = wireVirtual(forDirection);
4690*19c3b8c2SApple OSS Distributions 	}
4691*19c3b8c2SApple OSS Distributions 
4692*19c3b8c2SApple OSS Distributions 	if (kIOReturnSuccess == error) {
4693*19c3b8c2SApple OSS Distributions 		if (1 == ++_wireCount) {
4694*19c3b8c2SApple OSS Distributions 			if (kIOMemoryClearEncrypt & _flags) {
4695*19c3b8c2SApple OSS Distributions 				performOperation(kIOMemoryClearEncrypted, 0, _length);
4696*19c3b8c2SApple OSS Distributions 			}
4697*19c3b8c2SApple OSS Distributions 
4698*19c3b8c2SApple OSS Distributions 			ktraceEmitPhysicalSegments();
4699*19c3b8c2SApple OSS Distributions 		}
4700*19c3b8c2SApple OSS Distributions 	}
4701*19c3b8c2SApple OSS Distributions 
4702*19c3b8c2SApple OSS Distributions finish:
4703*19c3b8c2SApple OSS Distributions 
4704*19c3b8c2SApple OSS Distributions 	if (_prepareLock) {
4705*19c3b8c2SApple OSS Distributions 		IOLockUnlock(_prepareLock);
4706*19c3b8c2SApple OSS Distributions 	}
4707*19c3b8c2SApple OSS Distributions 	traceInterval.setEndArg1(error);
4708*19c3b8c2SApple OSS Distributions 
4709*19c3b8c2SApple OSS Distributions 	return error;
4710*19c3b8c2SApple OSS Distributions }
4711*19c3b8c2SApple OSS Distributions 
4712*19c3b8c2SApple OSS Distributions /*
4713*19c3b8c2SApple OSS Distributions  * complete
4714*19c3b8c2SApple OSS Distributions  *
4715*19c3b8c2SApple OSS Distributions  * Complete processing of the memory after an I/O transfer finishes.
4716*19c3b8c2SApple OSS Distributions  * This method should not be called unless a prepare was previously
4717*19c3b8c2SApple OSS Distributions  * issued; the prepare() and complete() must occur in pairs, before
4718*19c3b8c2SApple OSS Distributions  * before and after an I/O transfer involving pageable memory.
4719*19c3b8c2SApple OSS Distributions  */
4720*19c3b8c2SApple OSS Distributions 
4721*19c3b8c2SApple OSS Distributions IOReturn
complete(IODirection forDirection)4722*19c3b8c2SApple OSS Distributions IOGeneralMemoryDescriptor::complete(IODirection forDirection)
4723*19c3b8c2SApple OSS Distributions {
4724*19c3b8c2SApple OSS Distributions 	IOOptionBits type = _flags & kIOMemoryTypeMask;
4725*19c3b8c2SApple OSS Distributions 	ioGMDData  * dataP;
4726*19c3b8c2SApple OSS Distributions 	IOTimeStampIntervalConstantFiltered traceInterval(IODBG_MDESC(IOMDESC_COMPLETE), VM_KERNEL_ADDRHIDE(this), forDirection);
4727*19c3b8c2SApple OSS Distributions 
4728*19c3b8c2SApple OSS Distributions 	if ((kIOMemoryTypePhysical == type) || (kIOMemoryTypePhysical64 == type)) {
4729*19c3b8c2SApple OSS Distributions 		traceInterval.setEndArg1(kIOReturnSuccess);
4730*19c3b8c2SApple OSS Distributions 		return kIOReturnSuccess;
4731*19c3b8c2SApple OSS Distributions 	}
4732*19c3b8c2SApple OSS Distributions 
4733*19c3b8c2SApple OSS Distributions 	assert(!(kIOMemoryRemote & _flags));
4734*19c3b8c2SApple OSS Distributions 	if (kIOMemoryRemote & _flags) {
4735*19c3b8c2SApple OSS Distributions 		traceInterval.setEndArg1(kIOReturnNotAttached);
4736*19c3b8c2SApple OSS Distributions 		return kIOReturnNotAttached;
4737*19c3b8c2SApple OSS Distributions 	}
4738*19c3b8c2SApple OSS Distributions 
4739*19c3b8c2SApple OSS Distributions 	if (_prepareLock) {
4740*19c3b8c2SApple OSS Distributions 		IOLockLock(_prepareLock);
4741*19c3b8c2SApple OSS Distributions 	}
4742*19c3b8c2SApple OSS Distributions 	do{
4743*19c3b8c2SApple OSS Distributions 		assert(_wireCount);
4744*19c3b8c2SApple OSS Distributions 		if (!_wireCount) {
4745*19c3b8c2SApple OSS Distributions 			break;
4746*19c3b8c2SApple OSS Distributions 		}
4747*19c3b8c2SApple OSS Distributions 		dataP = getDataP(_memoryEntries);
4748*19c3b8c2SApple OSS Distributions 		if (!dataP) {
4749*19c3b8c2SApple OSS Distributions 			break;
4750*19c3b8c2SApple OSS Distributions 		}
4751*19c3b8c2SApple OSS Distributions 
4752*19c3b8c2SApple OSS Distributions 		if (kIODirectionCompleteWithError & forDirection) {
4753*19c3b8c2SApple OSS Distributions 			dataP->fCompletionError = true;
4754*19c3b8c2SApple OSS Distributions 		}
4755*19c3b8c2SApple OSS Distributions 
4756*19c3b8c2SApple OSS Distributions 		if ((kIOMemoryClearEncrypt & _flags) && (1 == _wireCount)) {
4757*19c3b8c2SApple OSS Distributions 			performOperation(kIOMemorySetEncrypted, 0, _length);
4758*19c3b8c2SApple OSS Distributions 		}
4759*19c3b8c2SApple OSS Distributions 
4760*19c3b8c2SApple OSS Distributions 		_wireCount--;
4761*19c3b8c2SApple OSS Distributions 		if (!_wireCount || (kIODirectionCompleteWithDataValid & forDirection)) {
4762*19c3b8c2SApple OSS Distributions 			ioPLBlock *ioplList = getIOPLList(dataP);
4763*19c3b8c2SApple OSS Distributions 			UInt ind, count = getNumIOPL(_memoryEntries, dataP);
4764*19c3b8c2SApple OSS Distributions 
4765*19c3b8c2SApple OSS Distributions 			if (_wireCount) {
4766*19c3b8c2SApple OSS Distributions 				// kIODirectionCompleteWithDataValid & forDirection
4767*19c3b8c2SApple OSS Distributions 				if (kIOMemoryTypeVirtual == type || kIOMemoryTypeVirtual64 == type || kIOMemoryTypeUIO == type) {
4768*19c3b8c2SApple OSS Distributions 					vm_tag_t tag;
4769*19c3b8c2SApple OSS Distributions 					tag = (typeof(tag))getVMTag(kernel_map);
4770*19c3b8c2SApple OSS Distributions 					for (ind = 0; ind < count; ind++) {
4771*19c3b8c2SApple OSS Distributions 						if (ioplList[ind].fIOPL) {
4772*19c3b8c2SApple OSS Distributions 							iopl_valid_data(ioplList[ind].fIOPL, tag);
4773*19c3b8c2SApple OSS Distributions 						}
4774*19c3b8c2SApple OSS Distributions 					}
4775*19c3b8c2SApple OSS Distributions 				}
4776*19c3b8c2SApple OSS Distributions 			} else {
4777*19c3b8c2SApple OSS Distributions 				if (_dmaReferences) {
4778*19c3b8c2SApple OSS Distributions 					panic("complete() while dma active");
4779*19c3b8c2SApple OSS Distributions 				}
4780*19c3b8c2SApple OSS Distributions 
4781*19c3b8c2SApple OSS Distributions 				if (dataP->fMappedBaseValid) {
4782*19c3b8c2SApple OSS Distributions 					dmaUnmap(dataP->fMapper, NULL, 0, dataP->fMappedBase, dataP->fMappedLength);
4783*19c3b8c2SApple OSS Distributions 					dataP->fMappedBaseValid = dataP->fMappedBase = 0;
4784*19c3b8c2SApple OSS Distributions 				}
4785*19c3b8c2SApple OSS Distributions #if IOTRACKING
4786*19c3b8c2SApple OSS Distributions 				if (dataP->fWireTracking.link.next) {
4787*19c3b8c2SApple OSS Distributions 					IOTrackingRemove(gIOWireTracking, &dataP->fWireTracking, ptoa(_pages));
4788*19c3b8c2SApple OSS Distributions 				}
4789*19c3b8c2SApple OSS Distributions #endif /* IOTRACKING */
4790*19c3b8c2SApple OSS Distributions 				// Only complete iopls that we created which are for TypeVirtual
4791*19c3b8c2SApple OSS Distributions 				if (kIOMemoryTypeVirtual == type || kIOMemoryTypeVirtual64 == type || kIOMemoryTypeUIO == type) {
4792*19c3b8c2SApple OSS Distributions 					for (ind = 0; ind < count; ind++) {
4793*19c3b8c2SApple OSS Distributions 						if (ioplList[ind].fIOPL) {
4794*19c3b8c2SApple OSS Distributions 							if (dataP->fCompletionError) {
4795*19c3b8c2SApple OSS Distributions 								upl_abort(ioplList[ind].fIOPL, 0 /*!UPL_ABORT_DUMP_PAGES*/);
4796*19c3b8c2SApple OSS Distributions 							} else {
4797*19c3b8c2SApple OSS Distributions 								upl_commit(ioplList[ind].fIOPL, NULL, 0);
4798*19c3b8c2SApple OSS Distributions 							}
4799*19c3b8c2SApple OSS Distributions 							upl_deallocate(ioplList[ind].fIOPL);
4800*19c3b8c2SApple OSS Distributions 						}
4801*19c3b8c2SApple OSS Distributions 					}
4802*19c3b8c2SApple OSS Distributions 				} else if (kIOMemoryTypeUPL == type) {
4803*19c3b8c2SApple OSS Distributions 					upl_set_referenced(ioplList[0].fIOPL, false);
4804*19c3b8c2SApple OSS Distributions 				}
4805*19c3b8c2SApple OSS Distributions 
4806*19c3b8c2SApple OSS Distributions 				_memoryEntries->setLength(computeDataSize(0, 0));
4807*19c3b8c2SApple OSS Distributions 
4808*19c3b8c2SApple OSS Distributions 				dataP->fPreparationID = kIOPreparationIDUnprepared;
4809*19c3b8c2SApple OSS Distributions 				_flags &= ~kIOMemoryPreparedReadOnly;
4810*19c3b8c2SApple OSS Distributions 
4811*19c3b8c2SApple OSS Distributions 				if (kdebug_debugid_explicitly_enabled(IODBG_IOMDPA(IOMDPA_UNMAPPED))) {
4812*19c3b8c2SApple OSS Distributions 					IOTimeStampConstantFiltered(IODBG_IOMDPA(IOMDPA_UNMAPPED), getDescriptorID(), VM_KERNEL_ADDRHIDE(this));
4813*19c3b8c2SApple OSS Distributions 				}
4814*19c3b8c2SApple OSS Distributions 			}
4815*19c3b8c2SApple OSS Distributions 		}
4816*19c3b8c2SApple OSS Distributions 	}while (false);
4817*19c3b8c2SApple OSS Distributions 
4818*19c3b8c2SApple OSS Distributions 	if (_prepareLock) {
4819*19c3b8c2SApple OSS Distributions 		IOLockUnlock(_prepareLock);
4820*19c3b8c2SApple OSS Distributions 	}
4821*19c3b8c2SApple OSS Distributions 
4822*19c3b8c2SApple OSS Distributions 	traceInterval.setEndArg1(kIOReturnSuccess);
4823*19c3b8c2SApple OSS Distributions 	return kIOReturnSuccess;
4824*19c3b8c2SApple OSS Distributions }
4825*19c3b8c2SApple OSS Distributions 
4826*19c3b8c2SApple OSS Distributions IOReturn
doMap(vm_map_t __addressMap,IOVirtualAddress * __address,IOOptionBits options,IOByteCount __offset,IOByteCount __length)4827*19c3b8c2SApple OSS Distributions IOGeneralMemoryDescriptor::doMap(
4828*19c3b8c2SApple OSS Distributions 	vm_map_t                __addressMap,
4829*19c3b8c2SApple OSS Distributions 	IOVirtualAddress *      __address,
4830*19c3b8c2SApple OSS Distributions 	IOOptionBits            options,
4831*19c3b8c2SApple OSS Distributions 	IOByteCount             __offset,
4832*19c3b8c2SApple OSS Distributions 	IOByteCount             __length )
4833*19c3b8c2SApple OSS Distributions {
4834*19c3b8c2SApple OSS Distributions 	IOTimeStampIntervalConstantFiltered traceInterval(IODBG_MDESC(IOMDESC_MAP), VM_KERNEL_ADDRHIDE(this), VM_KERNEL_ADDRHIDE(*__address), __length);
4835*19c3b8c2SApple OSS Distributions 	traceInterval.setEndArg1(kIOReturnSuccess);
4836*19c3b8c2SApple OSS Distributions #ifndef __LP64__
4837*19c3b8c2SApple OSS Distributions 	if (!(kIOMap64Bit & options)) {
4838*19c3b8c2SApple OSS Distributions 		panic("IOGeneralMemoryDescriptor::doMap !64bit");
4839*19c3b8c2SApple OSS Distributions 	}
4840*19c3b8c2SApple OSS Distributions #endif /* !__LP64__ */
4841*19c3b8c2SApple OSS Distributions 
4842*19c3b8c2SApple OSS Distributions 	kern_return_t  err;
4843*19c3b8c2SApple OSS Distributions 
4844*19c3b8c2SApple OSS Distributions 	IOMemoryMap *  mapping = (IOMemoryMap *) *__address;
4845*19c3b8c2SApple OSS Distributions 	mach_vm_size_t offset  = mapping->fOffset + __offset;
4846*19c3b8c2SApple OSS Distributions 	mach_vm_size_t length  = mapping->fLength;
4847*19c3b8c2SApple OSS Distributions 
4848*19c3b8c2SApple OSS Distributions 	IOOptionBits type = _flags & kIOMemoryTypeMask;
4849*19c3b8c2SApple OSS Distributions 	Ranges vec = _ranges;
4850*19c3b8c2SApple OSS Distributions 
4851*19c3b8c2SApple OSS Distributions 	mach_vm_address_t range0Addr = 0;
4852*19c3b8c2SApple OSS Distributions 	mach_vm_size_t    range0Len = 0;
4853*19c3b8c2SApple OSS Distributions 
4854*19c3b8c2SApple OSS Distributions 	if ((offset >= _length) || ((offset + length) > _length)) {
4855*19c3b8c2SApple OSS Distributions 		traceInterval.setEndArg1(kIOReturnBadArgument);
4856*19c3b8c2SApple OSS Distributions 		DEBUG4K_ERROR("map %p offset 0x%llx length 0x%llx _length 0x%llx kIOReturnBadArgument\n", __addressMap, offset, length, (uint64_t)_length);
4857*19c3b8c2SApple OSS Distributions 		// assert(offset == 0 && _length == 0 && length == 0);
4858*19c3b8c2SApple OSS Distributions 		return kIOReturnBadArgument;
4859*19c3b8c2SApple OSS Distributions 	}
4860*19c3b8c2SApple OSS Distributions 
4861*19c3b8c2SApple OSS Distributions 	assert(!(kIOMemoryRemote & _flags));
4862*19c3b8c2SApple OSS Distributions 	if (kIOMemoryRemote & _flags) {
4863*19c3b8c2SApple OSS Distributions 		return 0;
4864*19c3b8c2SApple OSS Distributions 	}
4865*19c3b8c2SApple OSS Distributions 
4866*19c3b8c2SApple OSS Distributions 	if (vec.v) {
4867*19c3b8c2SApple OSS Distributions 		getAddrLenForInd(range0Addr, range0Len, type, vec, 0);
4868*19c3b8c2SApple OSS Distributions 	}
4869*19c3b8c2SApple OSS Distributions 
4870*19c3b8c2SApple OSS Distributions 	// mapping source == dest? (could be much better)
4871*19c3b8c2SApple OSS Distributions 	if (_task
4872*19c3b8c2SApple OSS Distributions 	    && (mapping->fAddressTask == _task)
4873*19c3b8c2SApple OSS Distributions 	    && (mapping->fAddressMap == get_task_map(_task))
4874*19c3b8c2SApple OSS Distributions 	    && (options & kIOMapAnywhere)
4875*19c3b8c2SApple OSS Distributions 	    && (!(kIOMapUnique & options))
4876*19c3b8c2SApple OSS Distributions 	    && (!(kIOMapGuardedMask & options))
4877*19c3b8c2SApple OSS Distributions 	    && (1 == _rangesCount)
4878*19c3b8c2SApple OSS Distributions 	    && (0 == offset)
4879*19c3b8c2SApple OSS Distributions 	    && range0Addr
4880*19c3b8c2SApple OSS Distributions 	    && (length <= range0Len)) {
4881*19c3b8c2SApple OSS Distributions 		mapping->fAddress = range0Addr;
4882*19c3b8c2SApple OSS Distributions 		mapping->fOptions |= kIOMapStatic;
4883*19c3b8c2SApple OSS Distributions 
4884*19c3b8c2SApple OSS Distributions 		return kIOReturnSuccess;
4885*19c3b8c2SApple OSS Distributions 	}
4886*19c3b8c2SApple OSS Distributions 
4887*19c3b8c2SApple OSS Distributions 	if (!_memRef) {
4888*19c3b8c2SApple OSS Distributions 		IOOptionBits createOptions = 0;
4889*19c3b8c2SApple OSS Distributions 		if (!(kIOMapReadOnly & options)) {
4890*19c3b8c2SApple OSS Distributions 			createOptions |= kIOMemoryReferenceWrite;
4891*19c3b8c2SApple OSS Distributions #if DEVELOPMENT || DEBUG
4892*19c3b8c2SApple OSS Distributions 			if ((kIODirectionOut == (kIODirectionOutIn & _flags))
4893*19c3b8c2SApple OSS Distributions 			    && (!reserved || (reserved->creator != mapping->fAddressTask))) {
4894*19c3b8c2SApple OSS Distributions 				OSReportWithBacktrace("warning: creating writable mapping from IOMemoryDescriptor(kIODirectionOut) - use kIOMapReadOnly or change direction");
4895*19c3b8c2SApple OSS Distributions 			}
4896*19c3b8c2SApple OSS Distributions #endif
4897*19c3b8c2SApple OSS Distributions 		}
4898*19c3b8c2SApple OSS Distributions 		err = memoryReferenceCreate(createOptions, &_memRef);
4899*19c3b8c2SApple OSS Distributions 		if (kIOReturnSuccess != err) {
4900*19c3b8c2SApple OSS Distributions 			traceInterval.setEndArg1(err);
4901*19c3b8c2SApple OSS Distributions 			DEBUG4K_ERROR("map %p err 0x%x\n", __addressMap, err);
4902*19c3b8c2SApple OSS Distributions 			return err;
4903*19c3b8c2SApple OSS Distributions 		}
4904*19c3b8c2SApple OSS Distributions 	}
4905*19c3b8c2SApple OSS Distributions 
4906*19c3b8c2SApple OSS Distributions 	memory_object_t pager;
4907*19c3b8c2SApple OSS Distributions 	pager = (memory_object_t) (reserved ? reserved->dp.devicePager : NULL);
4908*19c3b8c2SApple OSS Distributions 
4909*19c3b8c2SApple OSS Distributions 	// <upl_transpose //
4910*19c3b8c2SApple OSS Distributions 	if ((kIOMapReference | kIOMapUnique) == ((kIOMapReference | kIOMapUnique) & options)) {
4911*19c3b8c2SApple OSS Distributions 		do{
4912*19c3b8c2SApple OSS Distributions 			upl_t               redirUPL2;
4913*19c3b8c2SApple OSS Distributions 			upl_size_t          size;
4914*19c3b8c2SApple OSS Distributions 			upl_control_flags_t flags;
4915*19c3b8c2SApple OSS Distributions 			unsigned int        lock_count;
4916*19c3b8c2SApple OSS Distributions 
4917*19c3b8c2SApple OSS Distributions 			if (!_memRef || (1 != _memRef->count)) {
4918*19c3b8c2SApple OSS Distributions 				err = kIOReturnNotReadable;
4919*19c3b8c2SApple OSS Distributions 				DEBUG4K_ERROR("map %p err 0x%x\n", __addressMap, err);
4920*19c3b8c2SApple OSS Distributions 				break;
4921*19c3b8c2SApple OSS Distributions 			}
4922*19c3b8c2SApple OSS Distributions 
4923*19c3b8c2SApple OSS Distributions 			size = (upl_size_t) round_page(mapping->fLength);
4924*19c3b8c2SApple OSS Distributions 			flags = UPL_COPYOUT_FROM | UPL_SET_INTERNAL
4925*19c3b8c2SApple OSS Distributions 			    | UPL_SET_LITE | UPL_SET_IO_WIRE | UPL_BLOCK_ACCESS;
4926*19c3b8c2SApple OSS Distributions 
4927*19c3b8c2SApple OSS Distributions 			if (KERN_SUCCESS != memory_object_iopl_request(_memRef->entries[0].entry, 0, &size, &redirUPL2,
4928*19c3b8c2SApple OSS Distributions 			    NULL, NULL,
4929*19c3b8c2SApple OSS Distributions 			    &flags, (vm_tag_t) getVMTag(kernel_map))) {
4930*19c3b8c2SApple OSS Distributions 				redirUPL2 = NULL;
4931*19c3b8c2SApple OSS Distributions 			}
4932*19c3b8c2SApple OSS Distributions 
4933*19c3b8c2SApple OSS Distributions 			for (lock_count = 0;
4934*19c3b8c2SApple OSS Distributions 			    IORecursiveLockHaveLock(gIOMemoryLock);
4935*19c3b8c2SApple OSS Distributions 			    lock_count++) {
4936*19c3b8c2SApple OSS Distributions 				UNLOCK;
4937*19c3b8c2SApple OSS Distributions 			}
4938*19c3b8c2SApple OSS Distributions 			err = upl_transpose(redirUPL2, mapping->fRedirUPL);
4939*19c3b8c2SApple OSS Distributions 			for (;
4940*19c3b8c2SApple OSS Distributions 			    lock_count;
4941*19c3b8c2SApple OSS Distributions 			    lock_count--) {
4942*19c3b8c2SApple OSS Distributions 				LOCK;
4943*19c3b8c2SApple OSS Distributions 			}
4944*19c3b8c2SApple OSS Distributions 
4945*19c3b8c2SApple OSS Distributions 			if (kIOReturnSuccess != err) {
4946*19c3b8c2SApple OSS Distributions 				IOLog("upl_transpose(%x)\n", err);
4947*19c3b8c2SApple OSS Distributions 				err = kIOReturnSuccess;
4948*19c3b8c2SApple OSS Distributions 			}
4949*19c3b8c2SApple OSS Distributions 
4950*19c3b8c2SApple OSS Distributions 			if (redirUPL2) {
4951*19c3b8c2SApple OSS Distributions 				upl_commit(redirUPL2, NULL, 0);
4952*19c3b8c2SApple OSS Distributions 				upl_deallocate(redirUPL2);
4953*19c3b8c2SApple OSS Distributions 				redirUPL2 = NULL;
4954*19c3b8c2SApple OSS Distributions 			}
4955*19c3b8c2SApple OSS Distributions 			{
4956*19c3b8c2SApple OSS Distributions 				// swap the memEntries since they now refer to different vm_objects
4957*19c3b8c2SApple OSS Distributions 				IOMemoryReference * me = _memRef;
4958*19c3b8c2SApple OSS Distributions 				_memRef = mapping->fMemory->_memRef;
4959*19c3b8c2SApple OSS Distributions 				mapping->fMemory->_memRef = me;
4960*19c3b8c2SApple OSS Distributions 			}
4961*19c3b8c2SApple OSS Distributions 			if (pager) {
4962*19c3b8c2SApple OSS Distributions 				err = populateDevicePager( pager, mapping->fAddressMap, mapping->fAddress, offset, length, options );
4963*19c3b8c2SApple OSS Distributions 			}
4964*19c3b8c2SApple OSS Distributions 		}while (false);
4965*19c3b8c2SApple OSS Distributions 	}
4966*19c3b8c2SApple OSS Distributions 	// upl_transpose> //
4967*19c3b8c2SApple OSS Distributions 	else {
4968*19c3b8c2SApple OSS Distributions 		err = memoryReferenceMap(_memRef, mapping->fAddressMap, offset, length, options, &mapping->fAddress);
4969*19c3b8c2SApple OSS Distributions 		if (err) {
4970*19c3b8c2SApple OSS Distributions 			DEBUG4K_ERROR("map %p err 0x%x\n", mapping->fAddressMap, err);
4971*19c3b8c2SApple OSS Distributions 		}
4972*19c3b8c2SApple OSS Distributions #if IOTRACKING
4973*19c3b8c2SApple OSS Distributions 		if ((err == KERN_SUCCESS) && ((kIOTracking & gIOKitDebug) || _task)) {
4974*19c3b8c2SApple OSS Distributions 			// only dram maps in the default on developement case
4975*19c3b8c2SApple OSS Distributions 			IOTrackingAddUser(gIOMapTracking, &mapping->fTracking, mapping->fLength);
4976*19c3b8c2SApple OSS Distributions 		}
4977*19c3b8c2SApple OSS Distributions #endif /* IOTRACKING */
4978*19c3b8c2SApple OSS Distributions 		if ((err == KERN_SUCCESS) && pager) {
4979*19c3b8c2SApple OSS Distributions 			err = populateDevicePager(pager, mapping->fAddressMap, mapping->fAddress, offset, length, options);
4980*19c3b8c2SApple OSS Distributions 
4981*19c3b8c2SApple OSS Distributions 			if (err != KERN_SUCCESS) {
4982*19c3b8c2SApple OSS Distributions 				doUnmap(mapping->fAddressMap, (IOVirtualAddress) mapping, 0);
4983*19c3b8c2SApple OSS Distributions 			} else if (kIOMapDefaultCache == (options & kIOMapCacheMask)) {
4984*19c3b8c2SApple OSS Distributions 				mapping->fOptions |= ((_flags & kIOMemoryBufferCacheMask) >> kIOMemoryBufferCacheShift);
4985*19c3b8c2SApple OSS Distributions 			}
4986*19c3b8c2SApple OSS Distributions 		}
4987*19c3b8c2SApple OSS Distributions 	}
4988*19c3b8c2SApple OSS Distributions 
4989*19c3b8c2SApple OSS Distributions 	traceInterval.setEndArg1(err);
4990*19c3b8c2SApple OSS Distributions 	if (err) {
4991*19c3b8c2SApple OSS Distributions 		DEBUG4K_ERROR("map %p err 0x%x\n", __addressMap, err);
4992*19c3b8c2SApple OSS Distributions 	}
4993*19c3b8c2SApple OSS Distributions 	return err;
4994*19c3b8c2SApple OSS Distributions }
4995*19c3b8c2SApple OSS Distributions 
4996*19c3b8c2SApple OSS Distributions #if IOTRACKING
4997*19c3b8c2SApple OSS Distributions IOReturn
IOMemoryMapTracking(IOTrackingUser * tracking,task_t * task,mach_vm_address_t * address,mach_vm_size_t * size)4998*19c3b8c2SApple OSS Distributions IOMemoryMapTracking(IOTrackingUser * tracking, task_t * task,
4999*19c3b8c2SApple OSS Distributions     mach_vm_address_t * address, mach_vm_size_t * size)
5000*19c3b8c2SApple OSS Distributions {
5001*19c3b8c2SApple OSS Distributions #define iomap_offsetof(type, field) ((size_t)(&((type *)NULL)->field))
5002*19c3b8c2SApple OSS Distributions 
5003*19c3b8c2SApple OSS Distributions 	IOMemoryMap * map = (typeof(map))(((uintptr_t) tracking) - iomap_offsetof(IOMemoryMap, fTracking));
5004*19c3b8c2SApple OSS Distributions 
5005*19c3b8c2SApple OSS Distributions 	if (!map->fAddressMap || (map->fAddressMap != get_task_map(map->fAddressTask))) {
5006*19c3b8c2SApple OSS Distributions 		return kIOReturnNotReady;
5007*19c3b8c2SApple OSS Distributions 	}
5008*19c3b8c2SApple OSS Distributions 
5009*19c3b8c2SApple OSS Distributions 	*task    = map->fAddressTask;
5010*19c3b8c2SApple OSS Distributions 	*address = map->fAddress;
5011*19c3b8c2SApple OSS Distributions 	*size    = map->fLength;
5012*19c3b8c2SApple OSS Distributions 
5013*19c3b8c2SApple OSS Distributions 	return kIOReturnSuccess;
5014*19c3b8c2SApple OSS Distributions }
5015*19c3b8c2SApple OSS Distributions #endif /* IOTRACKING */
5016*19c3b8c2SApple OSS Distributions 
5017*19c3b8c2SApple OSS Distributions IOReturn
doUnmap(vm_map_t addressMap,IOVirtualAddress __address,IOByteCount __length)5018*19c3b8c2SApple OSS Distributions IOGeneralMemoryDescriptor::doUnmap(
5019*19c3b8c2SApple OSS Distributions 	vm_map_t                addressMap,
5020*19c3b8c2SApple OSS Distributions 	IOVirtualAddress        __address,
5021*19c3b8c2SApple OSS Distributions 	IOByteCount             __length )
5022*19c3b8c2SApple OSS Distributions {
5023*19c3b8c2SApple OSS Distributions 	IOTimeStampIntervalConstantFiltered traceInterval(IODBG_MDESC(IOMDESC_UNMAP), VM_KERNEL_ADDRHIDE(this), VM_KERNEL_ADDRHIDE(__address), __length);
5024*19c3b8c2SApple OSS Distributions 	IOReturn ret;
5025*19c3b8c2SApple OSS Distributions 	ret = super::doUnmap(addressMap, __address, __length);
5026*19c3b8c2SApple OSS Distributions 	traceInterval.setEndArg1(ret);
5027*19c3b8c2SApple OSS Distributions 	return ret;
5028*19c3b8c2SApple OSS Distributions }
5029*19c3b8c2SApple OSS Distributions 
5030*19c3b8c2SApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
5031*19c3b8c2SApple OSS Distributions 
5032*19c3b8c2SApple OSS Distributions #undef super
5033*19c3b8c2SApple OSS Distributions #define super OSObject
5034*19c3b8c2SApple OSS Distributions 
5035*19c3b8c2SApple OSS Distributions OSDefineMetaClassAndStructorsWithZone( IOMemoryMap, OSObject, ZC_NONE )
5036*19c3b8c2SApple OSS Distributions 
5037*19c3b8c2SApple OSS Distributions OSMetaClassDefineReservedUnused(IOMemoryMap, 0);
5038*19c3b8c2SApple OSS Distributions OSMetaClassDefineReservedUnused(IOMemoryMap, 1);
5039*19c3b8c2SApple OSS Distributions OSMetaClassDefineReservedUnused(IOMemoryMap, 2);
5040*19c3b8c2SApple OSS Distributions OSMetaClassDefineReservedUnused(IOMemoryMap, 3);
5041*19c3b8c2SApple OSS Distributions OSMetaClassDefineReservedUnused(IOMemoryMap, 4);
5042*19c3b8c2SApple OSS Distributions OSMetaClassDefineReservedUnused(IOMemoryMap, 5);
5043*19c3b8c2SApple OSS Distributions OSMetaClassDefineReservedUnused(IOMemoryMap, 6);
5044*19c3b8c2SApple OSS Distributions OSMetaClassDefineReservedUnused(IOMemoryMap, 7);
5045*19c3b8c2SApple OSS Distributions 
5046*19c3b8c2SApple OSS Distributions /* ex-inline function implementation */
5047*19c3b8c2SApple OSS Distributions IOPhysicalAddress
getPhysicalAddress()5048*19c3b8c2SApple OSS Distributions IOMemoryMap::getPhysicalAddress()
5049*19c3b8c2SApple OSS Distributions {
5050*19c3b8c2SApple OSS Distributions 	return getPhysicalSegment( 0, NULL );
5051*19c3b8c2SApple OSS Distributions }
5052*19c3b8c2SApple OSS Distributions 
5053*19c3b8c2SApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
5054*19c3b8c2SApple OSS Distributions 
5055*19c3b8c2SApple OSS Distributions bool
init(task_t intoTask,mach_vm_address_t toAddress,IOOptionBits _options,mach_vm_size_t _offset,mach_vm_size_t _length)5056*19c3b8c2SApple OSS Distributions IOMemoryMap::init(
5057*19c3b8c2SApple OSS Distributions 	task_t                  intoTask,
5058*19c3b8c2SApple OSS Distributions 	mach_vm_address_t       toAddress,
5059*19c3b8c2SApple OSS Distributions 	IOOptionBits            _options,
5060*19c3b8c2SApple OSS Distributions 	mach_vm_size_t          _offset,
5061*19c3b8c2SApple OSS Distributions 	mach_vm_size_t          _length )
5062*19c3b8c2SApple OSS Distributions {
5063*19c3b8c2SApple OSS Distributions 	if (!intoTask) {
5064*19c3b8c2SApple OSS Distributions 		return false;
5065*19c3b8c2SApple OSS Distributions 	}
5066*19c3b8c2SApple OSS Distributions 
5067*19c3b8c2SApple OSS Distributions 	if (!super::init()) {
5068*19c3b8c2SApple OSS Distributions 		return false;
5069*19c3b8c2SApple OSS Distributions 	}
5070*19c3b8c2SApple OSS Distributions 
5071*19c3b8c2SApple OSS Distributions 	fAddressMap  = get_task_map(intoTask);
5072*19c3b8c2SApple OSS Distributions 	if (!fAddressMap) {
5073*19c3b8c2SApple OSS Distributions 		return false;
5074*19c3b8c2SApple OSS Distributions 	}
5075*19c3b8c2SApple OSS Distributions 	vm_map_reference(fAddressMap);
5076*19c3b8c2SApple OSS Distributions 
5077*19c3b8c2SApple OSS Distributions 	fAddressTask = intoTask;
5078*19c3b8c2SApple OSS Distributions 	fOptions     = _options;
5079*19c3b8c2SApple OSS Distributions 	fLength      = _length;
5080*19c3b8c2SApple OSS Distributions 	fOffset      = _offset;
5081*19c3b8c2SApple OSS Distributions 	fAddress     = toAddress;
5082*19c3b8c2SApple OSS Distributions 
5083*19c3b8c2SApple OSS Distributions 	return true;
5084*19c3b8c2SApple OSS Distributions }
5085*19c3b8c2SApple OSS Distributions 
5086*19c3b8c2SApple OSS Distributions bool
setMemoryDescriptor(IOMemoryDescriptor * _memory,mach_vm_size_t _offset)5087*19c3b8c2SApple OSS Distributions IOMemoryMap::setMemoryDescriptor(IOMemoryDescriptor * _memory, mach_vm_size_t _offset)
5088*19c3b8c2SApple OSS Distributions {
5089*19c3b8c2SApple OSS Distributions 	if (!_memory) {
5090*19c3b8c2SApple OSS Distributions 		return false;
5091*19c3b8c2SApple OSS Distributions 	}
5092*19c3b8c2SApple OSS Distributions 
5093*19c3b8c2SApple OSS Distributions 	if (!fSuperMap) {
5094*19c3b8c2SApple OSS Distributions 		if ((_offset + fLength) > _memory->getLength()) {
5095*19c3b8c2SApple OSS Distributions 			return false;
5096*19c3b8c2SApple OSS Distributions 		}
5097*19c3b8c2SApple OSS Distributions 		fOffset = _offset;
5098*19c3b8c2SApple OSS Distributions 	}
5099*19c3b8c2SApple OSS Distributions 
5100*19c3b8c2SApple OSS Distributions 
5101*19c3b8c2SApple OSS Distributions 	OSSharedPtr<IOMemoryDescriptor> tempval(_memory, OSRetain);
5102*19c3b8c2SApple OSS Distributions 	if (fMemory) {
5103*19c3b8c2SApple OSS Distributions 		if (fMemory != _memory) {
5104*19c3b8c2SApple OSS Distributions 			fMemory->removeMapping(this);
5105*19c3b8c2SApple OSS Distributions 		}
5106*19c3b8c2SApple OSS Distributions 	}
5107*19c3b8c2SApple OSS Distributions 	fMemory = os::move(tempval);
5108*19c3b8c2SApple OSS Distributions 
5109*19c3b8c2SApple OSS Distributions 	return true;
5110*19c3b8c2SApple OSS Distributions }
5111*19c3b8c2SApple OSS Distributions 
5112*19c3b8c2SApple OSS Distributions IOReturn
doMap(vm_map_t __addressMap,IOVirtualAddress * __address,IOOptionBits options,IOByteCount __offset,IOByteCount __length)5113*19c3b8c2SApple OSS Distributions IOMemoryDescriptor::doMap(
5114*19c3b8c2SApple OSS Distributions 	vm_map_t                __addressMap,
5115*19c3b8c2SApple OSS Distributions 	IOVirtualAddress *      __address,
5116*19c3b8c2SApple OSS Distributions 	IOOptionBits            options,
5117*19c3b8c2SApple OSS Distributions 	IOByteCount             __offset,
5118*19c3b8c2SApple OSS Distributions 	IOByteCount             __length )
5119*19c3b8c2SApple OSS Distributions {
5120*19c3b8c2SApple OSS Distributions 	return kIOReturnUnsupported;
5121*19c3b8c2SApple OSS Distributions }
5122*19c3b8c2SApple OSS Distributions 
5123*19c3b8c2SApple OSS Distributions IOReturn
handleFault(void * _pager,mach_vm_size_t sourceOffset,mach_vm_size_t length)5124*19c3b8c2SApple OSS Distributions IOMemoryDescriptor::handleFault(
5125*19c3b8c2SApple OSS Distributions 	void *                  _pager,
5126*19c3b8c2SApple OSS Distributions 	mach_vm_size_t          sourceOffset,
5127*19c3b8c2SApple OSS Distributions 	mach_vm_size_t          length)
5128*19c3b8c2SApple OSS Distributions {
5129*19c3b8c2SApple OSS Distributions 	if (kIOMemoryRedirected & _flags) {
5130*19c3b8c2SApple OSS Distributions #if DEBUG
5131*19c3b8c2SApple OSS Distributions 		IOLog("sleep mem redirect %p, %qx\n", this, sourceOffset);
5132*19c3b8c2SApple OSS Distributions #endif
5133*19c3b8c2SApple OSS Distributions 		do {
5134*19c3b8c2SApple OSS Distributions 			SLEEP;
5135*19c3b8c2SApple OSS Distributions 		} while (kIOMemoryRedirected & _flags);
5136*19c3b8c2SApple OSS Distributions 	}
5137*19c3b8c2SApple OSS Distributions 	return kIOReturnSuccess;
5138*19c3b8c2SApple OSS Distributions }
5139*19c3b8c2SApple OSS Distributions 
5140*19c3b8c2SApple OSS Distributions IOReturn
populateDevicePager(void * _pager,vm_map_t addressMap,mach_vm_address_t address,mach_vm_size_t sourceOffset,mach_vm_size_t length,IOOptionBits options)5141*19c3b8c2SApple OSS Distributions IOMemoryDescriptor::populateDevicePager(
5142*19c3b8c2SApple OSS Distributions 	void *                  _pager,
5143*19c3b8c2SApple OSS Distributions 	vm_map_t                addressMap,
5144*19c3b8c2SApple OSS Distributions 	mach_vm_address_t       address,
5145*19c3b8c2SApple OSS Distributions 	mach_vm_size_t          sourceOffset,
5146*19c3b8c2SApple OSS Distributions 	mach_vm_size_t          length,
5147*19c3b8c2SApple OSS Distributions 	IOOptionBits            options )
5148*19c3b8c2SApple OSS Distributions {
5149*19c3b8c2SApple OSS Distributions 	IOReturn            err = kIOReturnSuccess;
5150*19c3b8c2SApple OSS Distributions 	memory_object_t     pager = (memory_object_t) _pager;
5151*19c3b8c2SApple OSS Distributions 	mach_vm_size_t      size;
5152*19c3b8c2SApple OSS Distributions 	mach_vm_size_t      bytes;
5153*19c3b8c2SApple OSS Distributions 	mach_vm_size_t      page;
5154*19c3b8c2SApple OSS Distributions 	mach_vm_size_t      pageOffset;
5155*19c3b8c2SApple OSS Distributions 	mach_vm_size_t      pagerOffset;
5156*19c3b8c2SApple OSS Distributions 	IOPhysicalLength    segLen, chunk;
5157*19c3b8c2SApple OSS Distributions 	addr64_t            physAddr;
5158*19c3b8c2SApple OSS Distributions 	IOOptionBits        type;
5159*19c3b8c2SApple OSS Distributions 
5160*19c3b8c2SApple OSS Distributions 	type = _flags & kIOMemoryTypeMask;
5161*19c3b8c2SApple OSS Distributions 
5162*19c3b8c2SApple OSS Distributions 	if (reserved->dp.pagerContig) {
5163*19c3b8c2SApple OSS Distributions 		sourceOffset = 0;
5164*19c3b8c2SApple OSS Distributions 		pagerOffset  = 0;
5165*19c3b8c2SApple OSS Distributions 	}
5166*19c3b8c2SApple OSS Distributions 
5167*19c3b8c2SApple OSS Distributions 	physAddr = getPhysicalSegment( sourceOffset, &segLen, kIOMemoryMapperNone );
5168*19c3b8c2SApple OSS Distributions 	assert( physAddr );
5169*19c3b8c2SApple OSS Distributions 	pageOffset = physAddr - trunc_page_64( physAddr );
5170*19c3b8c2SApple OSS Distributions 	pagerOffset = sourceOffset;
5171*19c3b8c2SApple OSS Distributions 
5172*19c3b8c2SApple OSS Distributions 	size = length + pageOffset;
5173*19c3b8c2SApple OSS Distributions 	physAddr -= pageOffset;
5174*19c3b8c2SApple OSS Distributions 
5175*19c3b8c2SApple OSS Distributions 	segLen += pageOffset;
5176*19c3b8c2SApple OSS Distributions 	bytes = size;
5177*19c3b8c2SApple OSS Distributions 	do{
5178*19c3b8c2SApple OSS Distributions 		// in the middle of the loop only map whole pages
5179*19c3b8c2SApple OSS Distributions 		if (segLen >= bytes) {
5180*19c3b8c2SApple OSS Distributions 			segLen = bytes;
5181*19c3b8c2SApple OSS Distributions 		} else if (segLen != trunc_page_64(segLen)) {
5182*19c3b8c2SApple OSS Distributions 			err = kIOReturnVMError;
5183*19c3b8c2SApple OSS Distributions 		}
5184*19c3b8c2SApple OSS Distributions 		if (physAddr != trunc_page_64(physAddr)) {
5185*19c3b8c2SApple OSS Distributions 			err = kIOReturnBadArgument;
5186*19c3b8c2SApple OSS Distributions 		}
5187*19c3b8c2SApple OSS Distributions 
5188*19c3b8c2SApple OSS Distributions 		if (kIOReturnSuccess != err) {
5189*19c3b8c2SApple OSS Distributions 			break;
5190*19c3b8c2SApple OSS Distributions 		}
5191*19c3b8c2SApple OSS Distributions 
5192*19c3b8c2SApple OSS Distributions #if DEBUG || DEVELOPMENT
5193*19c3b8c2SApple OSS Distributions 		if ((kIOMemoryTypeUPL != type)
5194*19c3b8c2SApple OSS Distributions 		    && pmap_has_managed_page((ppnum_t) atop_64(physAddr), (ppnum_t) atop_64(physAddr + segLen - 1))) {
5195*19c3b8c2SApple OSS Distributions 			OSReportWithBacktrace("IOMemoryDescriptor physical with managed page 0x%qx:0x%qx",
5196*19c3b8c2SApple OSS Distributions 			    physAddr, (uint64_t)segLen);
5197*19c3b8c2SApple OSS Distributions 		}
5198*19c3b8c2SApple OSS Distributions #endif /* DEBUG || DEVELOPMENT */
5199*19c3b8c2SApple OSS Distributions 
5200*19c3b8c2SApple OSS Distributions 		chunk = (reserved->dp.pagerContig ? round_page(segLen) : page_size);
5201*19c3b8c2SApple OSS Distributions 		for (page = 0;
5202*19c3b8c2SApple OSS Distributions 		    (page < segLen) && (KERN_SUCCESS == err);
5203*19c3b8c2SApple OSS Distributions 		    page += chunk) {
5204*19c3b8c2SApple OSS Distributions 			err = device_pager_populate_object(pager, pagerOffset,
5205*19c3b8c2SApple OSS Distributions 			    (ppnum_t)(atop_64(physAddr + page)), chunk);
5206*19c3b8c2SApple OSS Distributions 			pagerOffset += chunk;
5207*19c3b8c2SApple OSS Distributions 		}
5208*19c3b8c2SApple OSS Distributions 
5209*19c3b8c2SApple OSS Distributions 		assert(KERN_SUCCESS == err);
5210*19c3b8c2SApple OSS Distributions 		if (err) {
5211*19c3b8c2SApple OSS Distributions 			break;
5212*19c3b8c2SApple OSS Distributions 		}
5213*19c3b8c2SApple OSS Distributions 
5214*19c3b8c2SApple OSS Distributions 		// This call to vm_fault causes an early pmap level resolution
5215*19c3b8c2SApple OSS Distributions 		// of the mappings created above for kernel mappings, since
5216*19c3b8c2SApple OSS Distributions 		// faulting in later can't take place from interrupt level.
5217*19c3b8c2SApple OSS Distributions 		if ((addressMap == kernel_map) && !(kIOMemoryRedirected & _flags)) {
5218*19c3b8c2SApple OSS Distributions 			err = vm_fault(addressMap,
5219*19c3b8c2SApple OSS Distributions 			    (vm_map_offset_t)trunc_page_64(address),
5220*19c3b8c2SApple OSS Distributions 			    options & kIOMapReadOnly ? VM_PROT_READ : VM_PROT_READ | VM_PROT_WRITE,
5221*19c3b8c2SApple OSS Distributions 			    FALSE, VM_KERN_MEMORY_NONE,
5222*19c3b8c2SApple OSS Distributions 			    THREAD_UNINT, NULL,
5223*19c3b8c2SApple OSS Distributions 			    (vm_map_offset_t)0);
5224*19c3b8c2SApple OSS Distributions 
5225*19c3b8c2SApple OSS Distributions 			if (KERN_SUCCESS != err) {
5226*19c3b8c2SApple OSS Distributions 				break;
5227*19c3b8c2SApple OSS Distributions 			}
5228*19c3b8c2SApple OSS Distributions 		}
5229*19c3b8c2SApple OSS Distributions 
5230*19c3b8c2SApple OSS Distributions 		sourceOffset += segLen - pageOffset;
5231*19c3b8c2SApple OSS Distributions 		address += segLen;
5232*19c3b8c2SApple OSS Distributions 		bytes -= segLen;
5233*19c3b8c2SApple OSS Distributions 		pageOffset = 0;
5234*19c3b8c2SApple OSS Distributions 	}while (bytes && (physAddr = getPhysicalSegment( sourceOffset, &segLen, kIOMemoryMapperNone )));
5235*19c3b8c2SApple OSS Distributions 
5236*19c3b8c2SApple OSS Distributions 	if (bytes) {
5237*19c3b8c2SApple OSS Distributions 		err = kIOReturnBadArgument;
5238*19c3b8c2SApple OSS Distributions 	}
5239*19c3b8c2SApple OSS Distributions 
5240*19c3b8c2SApple OSS Distributions 	return err;
5241*19c3b8c2SApple OSS Distributions }
5242*19c3b8c2SApple OSS Distributions 
5243*19c3b8c2SApple OSS Distributions IOReturn
doUnmap(vm_map_t addressMap,IOVirtualAddress __address,IOByteCount __length)5244*19c3b8c2SApple OSS Distributions IOMemoryDescriptor::doUnmap(
5245*19c3b8c2SApple OSS Distributions 	vm_map_t                addressMap,
5246*19c3b8c2SApple OSS Distributions 	IOVirtualAddress        __address,
5247*19c3b8c2SApple OSS Distributions 	IOByteCount             __length )
5248*19c3b8c2SApple OSS Distributions {
5249*19c3b8c2SApple OSS Distributions 	IOReturn          err;
5250*19c3b8c2SApple OSS Distributions 	IOMemoryMap *     mapping;
5251*19c3b8c2SApple OSS Distributions 	mach_vm_address_t address;
5252*19c3b8c2SApple OSS Distributions 	mach_vm_size_t    length;
5253*19c3b8c2SApple OSS Distributions 
5254*19c3b8c2SApple OSS Distributions 	if (__length) {
5255*19c3b8c2SApple OSS Distributions 		panic("doUnmap");
5256*19c3b8c2SApple OSS Distributions 	}
5257*19c3b8c2SApple OSS Distributions 
5258*19c3b8c2SApple OSS Distributions 	mapping = (IOMemoryMap *) __address;
5259*19c3b8c2SApple OSS Distributions 	addressMap = mapping->fAddressMap;
5260*19c3b8c2SApple OSS Distributions 	address    = mapping->fAddress;
5261*19c3b8c2SApple OSS Distributions 	length     = mapping->fLength;
5262*19c3b8c2SApple OSS Distributions 
5263*19c3b8c2SApple OSS Distributions 	if (kIOMapOverwrite & mapping->fOptions) {
5264*19c3b8c2SApple OSS Distributions 		err = KERN_SUCCESS;
5265*19c3b8c2SApple OSS Distributions 	} else {
5266*19c3b8c2SApple OSS Distributions 		if ((addressMap == kernel_map) && (kIOMemoryBufferPageable & _flags)) {
5267*19c3b8c2SApple OSS Distributions 			addressMap = IOPageableMapForAddress( address );
5268*19c3b8c2SApple OSS Distributions 		}
5269*19c3b8c2SApple OSS Distributions #if DEBUG
5270*19c3b8c2SApple OSS Distributions 		if (kIOLogMapping & gIOKitDebug) {
5271*19c3b8c2SApple OSS Distributions 			IOLog("IOMemoryDescriptor::doUnmap map %p, 0x%qx:0x%qx\n",
5272*19c3b8c2SApple OSS Distributions 			    addressMap, address, length );
5273*19c3b8c2SApple OSS Distributions 		}
5274*19c3b8c2SApple OSS Distributions #endif
5275*19c3b8c2SApple OSS Distributions 		err = IOMemoryDescriptorMapDealloc(mapping->fOptions, addressMap, address, length );
5276*19c3b8c2SApple OSS Distributions 		if (vm_map_page_mask(addressMap) < PAGE_MASK) {
5277*19c3b8c2SApple OSS Distributions 			DEBUG4K_IOKIT("map %p address 0x%llx length 0x%llx err 0x%x\n", addressMap, address, length, err);
5278*19c3b8c2SApple OSS Distributions 		}
5279*19c3b8c2SApple OSS Distributions 	}
5280*19c3b8c2SApple OSS Distributions 
5281*19c3b8c2SApple OSS Distributions #if IOTRACKING
5282*19c3b8c2SApple OSS Distributions 	IOTrackingRemoveUser(gIOMapTracking, &mapping->fTracking);
5283*19c3b8c2SApple OSS Distributions #endif /* IOTRACKING */
5284*19c3b8c2SApple OSS Distributions 
5285*19c3b8c2SApple OSS Distributions 	return err;
5286*19c3b8c2SApple OSS Distributions }
5287*19c3b8c2SApple OSS Distributions 
5288*19c3b8c2SApple OSS Distributions IOReturn
redirect(task_t safeTask,bool doRedirect)5289*19c3b8c2SApple OSS Distributions IOMemoryDescriptor::redirect( task_t safeTask, bool doRedirect )
5290*19c3b8c2SApple OSS Distributions {
5291*19c3b8c2SApple OSS Distributions 	IOReturn            err = kIOReturnSuccess;
5292*19c3b8c2SApple OSS Distributions 	IOMemoryMap *       mapping = NULL;
5293*19c3b8c2SApple OSS Distributions 	OSSharedPtr<OSIterator>        iter;
5294*19c3b8c2SApple OSS Distributions 
5295*19c3b8c2SApple OSS Distributions 	LOCK;
5296*19c3b8c2SApple OSS Distributions 
5297*19c3b8c2SApple OSS Distributions 	if (doRedirect) {
5298*19c3b8c2SApple OSS Distributions 		_flags |= kIOMemoryRedirected;
5299*19c3b8c2SApple OSS Distributions 	} else {
5300*19c3b8c2SApple OSS Distributions 		_flags &= ~kIOMemoryRedirected;
5301*19c3b8c2SApple OSS Distributions 	}
5302*19c3b8c2SApple OSS Distributions 
5303*19c3b8c2SApple OSS Distributions 	do {
5304*19c3b8c2SApple OSS Distributions 		if ((iter = OSCollectionIterator::withCollection( _mappings.get()))) {
5305*19c3b8c2SApple OSS Distributions 			memory_object_t   pager;
5306*19c3b8c2SApple OSS Distributions 
5307*19c3b8c2SApple OSS Distributions 			if (reserved) {
5308*19c3b8c2SApple OSS Distributions 				pager = (memory_object_t) reserved->dp.devicePager;
5309*19c3b8c2SApple OSS Distributions 			} else {
5310*19c3b8c2SApple OSS Distributions 				pager = MACH_PORT_NULL;
5311*19c3b8c2SApple OSS Distributions 			}
5312*19c3b8c2SApple OSS Distributions 
5313*19c3b8c2SApple OSS Distributions 			while ((mapping = (IOMemoryMap *) iter->getNextObject())) {
5314*19c3b8c2SApple OSS Distributions 				mapping->redirect( safeTask, doRedirect );
5315*19c3b8c2SApple OSS Distributions 				if (!doRedirect && !safeTask && pager && (kernel_map == mapping->fAddressMap)) {
5316*19c3b8c2SApple OSS Distributions 					err = populateDevicePager(pager, mapping->fAddressMap, mapping->fAddress, mapping->fOffset, mapping->fLength, kIOMapDefaultCache );
5317*19c3b8c2SApple OSS Distributions 				}
5318*19c3b8c2SApple OSS Distributions 			}
5319*19c3b8c2SApple OSS Distributions 
5320*19c3b8c2SApple OSS Distributions 			iter.reset();
5321*19c3b8c2SApple OSS Distributions 		}
5322*19c3b8c2SApple OSS Distributions 	} while (false);
5323*19c3b8c2SApple OSS Distributions 
5324*19c3b8c2SApple OSS Distributions 	if (!doRedirect) {
5325*19c3b8c2SApple OSS Distributions 		WAKEUP;
5326*19c3b8c2SApple OSS Distributions 	}
5327*19c3b8c2SApple OSS Distributions 
5328*19c3b8c2SApple OSS Distributions 	UNLOCK;
5329*19c3b8c2SApple OSS Distributions 
5330*19c3b8c2SApple OSS Distributions #ifndef __LP64__
5331*19c3b8c2SApple OSS Distributions 	// temporary binary compatibility
5332*19c3b8c2SApple OSS Distributions 	IOSubMemoryDescriptor * subMem;
5333*19c3b8c2SApple OSS Distributions 	if ((subMem = OSDynamicCast( IOSubMemoryDescriptor, this))) {
5334*19c3b8c2SApple OSS Distributions 		err = subMem->redirect( safeTask, doRedirect );
5335*19c3b8c2SApple OSS Distributions 	} else {
5336*19c3b8c2SApple OSS Distributions 		err = kIOReturnSuccess;
5337*19c3b8c2SApple OSS Distributions 	}
5338*19c3b8c2SApple OSS Distributions #endif /* !__LP64__ */
5339*19c3b8c2SApple OSS Distributions 
5340*19c3b8c2SApple OSS Distributions 	return err;
5341*19c3b8c2SApple OSS Distributions }
5342*19c3b8c2SApple OSS Distributions 
5343*19c3b8c2SApple OSS Distributions IOReturn
redirect(task_t safeTask,bool doRedirect)5344*19c3b8c2SApple OSS Distributions IOMemoryMap::redirect( task_t safeTask, bool doRedirect )
5345*19c3b8c2SApple OSS Distributions {
5346*19c3b8c2SApple OSS Distributions 	IOReturn err = kIOReturnSuccess;
5347*19c3b8c2SApple OSS Distributions 
5348*19c3b8c2SApple OSS Distributions 	if (fSuperMap) {
5349*19c3b8c2SApple OSS Distributions //        err = ((IOMemoryMap *)superMap)->redirect( safeTask, doRedirect );
5350*19c3b8c2SApple OSS Distributions 	} else {
5351*19c3b8c2SApple OSS Distributions 		LOCK;
5352*19c3b8c2SApple OSS Distributions 
5353*19c3b8c2SApple OSS Distributions 		do{
5354*19c3b8c2SApple OSS Distributions 			if (!fAddress) {
5355*19c3b8c2SApple OSS Distributions 				break;
5356*19c3b8c2SApple OSS Distributions 			}
5357*19c3b8c2SApple OSS Distributions 			if (!fAddressMap) {
5358*19c3b8c2SApple OSS Distributions 				break;
5359*19c3b8c2SApple OSS Distributions 			}
5360*19c3b8c2SApple OSS Distributions 
5361*19c3b8c2SApple OSS Distributions 			if ((!safeTask || (get_task_map(safeTask) != fAddressMap))
5362*19c3b8c2SApple OSS Distributions 			    && (0 == (fOptions & kIOMapStatic))) {
5363*19c3b8c2SApple OSS Distributions 				IOUnmapPages( fAddressMap, fAddress, fLength );
5364*19c3b8c2SApple OSS Distributions 				err = kIOReturnSuccess;
5365*19c3b8c2SApple OSS Distributions #if DEBUG
5366*19c3b8c2SApple OSS Distributions 				IOLog("IOMemoryMap::redirect(%d, %p) 0x%qx:0x%qx from %p\n", doRedirect, this, fAddress, fLength, fAddressMap);
5367*19c3b8c2SApple OSS Distributions #endif
5368*19c3b8c2SApple OSS Distributions 			} else if (kIOMapWriteCombineCache == (fOptions & kIOMapCacheMask)) {
5369*19c3b8c2SApple OSS Distributions 				IOOptionBits newMode;
5370*19c3b8c2SApple OSS Distributions 				newMode = (fOptions & ~kIOMapCacheMask) | (doRedirect ? kIOMapInhibitCache : kIOMapWriteCombineCache);
5371*19c3b8c2SApple OSS Distributions 				IOProtectCacheMode(fAddressMap, fAddress, fLength, newMode);
5372*19c3b8c2SApple OSS Distributions 			}
5373*19c3b8c2SApple OSS Distributions 		}while (false);
5374*19c3b8c2SApple OSS Distributions 		UNLOCK;
5375*19c3b8c2SApple OSS Distributions 	}
5376*19c3b8c2SApple OSS Distributions 
5377*19c3b8c2SApple OSS Distributions 	if ((((fMemory->_flags & kIOMemoryTypeMask) == kIOMemoryTypePhysical)
5378*19c3b8c2SApple OSS Distributions 	    || ((fMemory->_flags & kIOMemoryTypeMask) == kIOMemoryTypePhysical64))
5379*19c3b8c2SApple OSS Distributions 	    && safeTask
5380*19c3b8c2SApple OSS Distributions 	    && (doRedirect != (0 != (fMemory->_flags & kIOMemoryRedirected)))) {
5381*19c3b8c2SApple OSS Distributions 		fMemory->redirect(safeTask, doRedirect);
5382*19c3b8c2SApple OSS Distributions 	}
5383*19c3b8c2SApple OSS Distributions 
5384*19c3b8c2SApple OSS Distributions 	return err;
5385*19c3b8c2SApple OSS Distributions }
5386*19c3b8c2SApple OSS Distributions 
5387*19c3b8c2SApple OSS Distributions IOReturn
unmap(void)5388*19c3b8c2SApple OSS Distributions IOMemoryMap::unmap( void )
5389*19c3b8c2SApple OSS Distributions {
5390*19c3b8c2SApple OSS Distributions 	IOReturn    err;
5391*19c3b8c2SApple OSS Distributions 
5392*19c3b8c2SApple OSS Distributions 	LOCK;
5393*19c3b8c2SApple OSS Distributions 
5394*19c3b8c2SApple OSS Distributions 	if (fAddress && fAddressMap && (NULL == fSuperMap) && fMemory
5395*19c3b8c2SApple OSS Distributions 	    && (0 == (kIOMapStatic & fOptions))) {
5396*19c3b8c2SApple OSS Distributions 		err = fMemory->doUnmap(fAddressMap, (IOVirtualAddress) this, 0);
5397*19c3b8c2SApple OSS Distributions 	} else {
5398*19c3b8c2SApple OSS Distributions 		err = kIOReturnSuccess;
5399*19c3b8c2SApple OSS Distributions 	}
5400*19c3b8c2SApple OSS Distributions 
5401*19c3b8c2SApple OSS Distributions 	if (fAddressMap) {
5402*19c3b8c2SApple OSS Distributions 		vm_map_deallocate(fAddressMap);
5403*19c3b8c2SApple OSS Distributions 		fAddressMap = NULL;
5404*19c3b8c2SApple OSS Distributions 	}
5405*19c3b8c2SApple OSS Distributions 
5406*19c3b8c2SApple OSS Distributions 	fAddress = 0;
5407*19c3b8c2SApple OSS Distributions 
5408*19c3b8c2SApple OSS Distributions 	UNLOCK;
5409*19c3b8c2SApple OSS Distributions 
5410*19c3b8c2SApple OSS Distributions 	return err;
5411*19c3b8c2SApple OSS Distributions }
5412*19c3b8c2SApple OSS Distributions 
5413*19c3b8c2SApple OSS Distributions void
taskDied(void)5414*19c3b8c2SApple OSS Distributions IOMemoryMap::taskDied( void )
5415*19c3b8c2SApple OSS Distributions {
5416*19c3b8c2SApple OSS Distributions 	LOCK;
5417*19c3b8c2SApple OSS Distributions 	if (fUserClientUnmap) {
5418*19c3b8c2SApple OSS Distributions 		unmap();
5419*19c3b8c2SApple OSS Distributions 	}
5420*19c3b8c2SApple OSS Distributions #if IOTRACKING
5421*19c3b8c2SApple OSS Distributions 	else {
5422*19c3b8c2SApple OSS Distributions 		IOTrackingRemoveUser(gIOMapTracking, &fTracking);
5423*19c3b8c2SApple OSS Distributions 	}
5424*19c3b8c2SApple OSS Distributions #endif /* IOTRACKING */
5425*19c3b8c2SApple OSS Distributions 
5426*19c3b8c2SApple OSS Distributions 	if (fAddressMap) {
5427*19c3b8c2SApple OSS Distributions 		vm_map_deallocate(fAddressMap);
5428*19c3b8c2SApple OSS Distributions 		fAddressMap = NULL;
5429*19c3b8c2SApple OSS Distributions 	}
5430*19c3b8c2SApple OSS Distributions 	fAddressTask = NULL;
5431*19c3b8c2SApple OSS Distributions 	fAddress     = 0;
5432*19c3b8c2SApple OSS Distributions 	UNLOCK;
5433*19c3b8c2SApple OSS Distributions }
5434*19c3b8c2SApple OSS Distributions 
5435*19c3b8c2SApple OSS Distributions IOReturn
userClientUnmap(void)5436*19c3b8c2SApple OSS Distributions IOMemoryMap::userClientUnmap( void )
5437*19c3b8c2SApple OSS Distributions {
5438*19c3b8c2SApple OSS Distributions 	fUserClientUnmap = true;
5439*19c3b8c2SApple OSS Distributions 	return kIOReturnSuccess;
5440*19c3b8c2SApple OSS Distributions }
5441*19c3b8c2SApple OSS Distributions 
5442*19c3b8c2SApple OSS Distributions // Overload the release mechanism.  All mappings must be a member
5443*19c3b8c2SApple OSS Distributions // of a memory descriptors _mappings set.  This means that we
5444*19c3b8c2SApple OSS Distributions // always have 2 references on a mapping.  When either of these mappings
5445*19c3b8c2SApple OSS Distributions // are released we need to free ourselves.
5446*19c3b8c2SApple OSS Distributions void
taggedRelease(const void * tag) const5447*19c3b8c2SApple OSS Distributions IOMemoryMap::taggedRelease(const void *tag) const
5448*19c3b8c2SApple OSS Distributions {
5449*19c3b8c2SApple OSS Distributions 	LOCK;
5450*19c3b8c2SApple OSS Distributions 	super::taggedRelease(tag, 2);
5451*19c3b8c2SApple OSS Distributions 	UNLOCK;
5452*19c3b8c2SApple OSS Distributions }
5453*19c3b8c2SApple OSS Distributions 
5454*19c3b8c2SApple OSS Distributions void
free()5455*19c3b8c2SApple OSS Distributions IOMemoryMap::free()
5456*19c3b8c2SApple OSS Distributions {
5457*19c3b8c2SApple OSS Distributions 	unmap();
5458*19c3b8c2SApple OSS Distributions 
5459*19c3b8c2SApple OSS Distributions 	if (fMemory) {
5460*19c3b8c2SApple OSS Distributions 		LOCK;
5461*19c3b8c2SApple OSS Distributions 		fMemory->removeMapping(this);
5462*19c3b8c2SApple OSS Distributions 		UNLOCK;
5463*19c3b8c2SApple OSS Distributions 		fMemory.reset();
5464*19c3b8c2SApple OSS Distributions 	}
5465*19c3b8c2SApple OSS Distributions 
5466*19c3b8c2SApple OSS Distributions 	if (fSuperMap) {
5467*19c3b8c2SApple OSS Distributions 		fSuperMap.reset();
5468*19c3b8c2SApple OSS Distributions 	}
5469*19c3b8c2SApple OSS Distributions 
5470*19c3b8c2SApple OSS Distributions 	if (fRedirUPL) {
5471*19c3b8c2SApple OSS Distributions 		upl_commit(fRedirUPL, NULL, 0);
5472*19c3b8c2SApple OSS Distributions 		upl_deallocate(fRedirUPL);
5473*19c3b8c2SApple OSS Distributions 	}
5474*19c3b8c2SApple OSS Distributions 
5475*19c3b8c2SApple OSS Distributions 	super::free();
5476*19c3b8c2SApple OSS Distributions }
5477*19c3b8c2SApple OSS Distributions 
5478*19c3b8c2SApple OSS Distributions IOByteCount
getLength()5479*19c3b8c2SApple OSS Distributions IOMemoryMap::getLength()
5480*19c3b8c2SApple OSS Distributions {
5481*19c3b8c2SApple OSS Distributions 	return fLength;
5482*19c3b8c2SApple OSS Distributions }
5483*19c3b8c2SApple OSS Distributions 
5484*19c3b8c2SApple OSS Distributions IOVirtualAddress
getVirtualAddress()5485*19c3b8c2SApple OSS Distributions IOMemoryMap::getVirtualAddress()
5486*19c3b8c2SApple OSS Distributions {
5487*19c3b8c2SApple OSS Distributions #ifndef __LP64__
5488*19c3b8c2SApple OSS Distributions 	if (fSuperMap) {
5489*19c3b8c2SApple OSS Distributions 		fSuperMap->getVirtualAddress();
5490*19c3b8c2SApple OSS Distributions 	} else if (fAddressMap
5491*19c3b8c2SApple OSS Distributions 	    && vm_map_is_64bit(fAddressMap)
5492*19c3b8c2SApple OSS Distributions 	    && (sizeof(IOVirtualAddress) < 8)) {
5493*19c3b8c2SApple OSS Distributions 		OSReportWithBacktrace("IOMemoryMap::getVirtualAddress(0x%qx) called on 64b map; use ::getAddress()", fAddress);
5494*19c3b8c2SApple OSS Distributions 	}
5495*19c3b8c2SApple OSS Distributions #endif /* !__LP64__ */
5496*19c3b8c2SApple OSS Distributions 
5497*19c3b8c2SApple OSS Distributions 	return fAddress;
5498*19c3b8c2SApple OSS Distributions }
5499*19c3b8c2SApple OSS Distributions 
5500*19c3b8c2SApple OSS Distributions #ifndef __LP64__
5501*19c3b8c2SApple OSS Distributions mach_vm_address_t
getAddress()5502*19c3b8c2SApple OSS Distributions IOMemoryMap::getAddress()
5503*19c3b8c2SApple OSS Distributions {
5504*19c3b8c2SApple OSS Distributions 	return fAddress;
5505*19c3b8c2SApple OSS Distributions }
5506*19c3b8c2SApple OSS Distributions 
5507*19c3b8c2SApple OSS Distributions mach_vm_size_t
getSize()5508*19c3b8c2SApple OSS Distributions IOMemoryMap::getSize()
5509*19c3b8c2SApple OSS Distributions {
5510*19c3b8c2SApple OSS Distributions 	return fLength;
5511*19c3b8c2SApple OSS Distributions }
5512*19c3b8c2SApple OSS Distributions #endif /* !__LP64__ */
5513*19c3b8c2SApple OSS Distributions 
5514*19c3b8c2SApple OSS Distributions 
5515*19c3b8c2SApple OSS Distributions task_t
getAddressTask()5516*19c3b8c2SApple OSS Distributions IOMemoryMap::getAddressTask()
5517*19c3b8c2SApple OSS Distributions {
5518*19c3b8c2SApple OSS Distributions 	if (fSuperMap) {
5519*19c3b8c2SApple OSS Distributions 		return fSuperMap->getAddressTask();
5520*19c3b8c2SApple OSS Distributions 	} else {
5521*19c3b8c2SApple OSS Distributions 		return fAddressTask;
5522*19c3b8c2SApple OSS Distributions 	}
5523*19c3b8c2SApple OSS Distributions }
5524*19c3b8c2SApple OSS Distributions 
5525*19c3b8c2SApple OSS Distributions IOOptionBits
getMapOptions()5526*19c3b8c2SApple OSS Distributions IOMemoryMap::getMapOptions()
5527*19c3b8c2SApple OSS Distributions {
5528*19c3b8c2SApple OSS Distributions 	return fOptions;
5529*19c3b8c2SApple OSS Distributions }
5530*19c3b8c2SApple OSS Distributions 
5531*19c3b8c2SApple OSS Distributions IOMemoryDescriptor *
getMemoryDescriptor()5532*19c3b8c2SApple OSS Distributions IOMemoryMap::getMemoryDescriptor()
5533*19c3b8c2SApple OSS Distributions {
5534*19c3b8c2SApple OSS Distributions 	return fMemory.get();
5535*19c3b8c2SApple OSS Distributions }
5536*19c3b8c2SApple OSS Distributions 
5537*19c3b8c2SApple OSS Distributions IOMemoryMap *
copyCompatible(IOMemoryMap * newMapping)5538*19c3b8c2SApple OSS Distributions IOMemoryMap::copyCompatible(
5539*19c3b8c2SApple OSS Distributions 	IOMemoryMap * newMapping )
5540*19c3b8c2SApple OSS Distributions {
5541*19c3b8c2SApple OSS Distributions 	task_t              task      = newMapping->getAddressTask();
5542*19c3b8c2SApple OSS Distributions 	mach_vm_address_t   toAddress = newMapping->fAddress;
5543*19c3b8c2SApple OSS Distributions 	IOOptionBits        _options  = newMapping->fOptions;
5544*19c3b8c2SApple OSS Distributions 	mach_vm_size_t      _offset   = newMapping->fOffset;
5545*19c3b8c2SApple OSS Distributions 	mach_vm_size_t      _length   = newMapping->fLength;
5546*19c3b8c2SApple OSS Distributions 
5547*19c3b8c2SApple OSS Distributions 	if ((!task) || (!fAddressMap) || (fAddressMap != get_task_map(task))) {
5548*19c3b8c2SApple OSS Distributions 		return NULL;
5549*19c3b8c2SApple OSS Distributions 	}
5550*19c3b8c2SApple OSS Distributions 	if ((fOptions ^ _options) & kIOMapReadOnly) {
5551*19c3b8c2SApple OSS Distributions 		return NULL;
5552*19c3b8c2SApple OSS Distributions 	}
5553*19c3b8c2SApple OSS Distributions 	if ((fOptions ^ _options) & kIOMapGuardedMask) {
5554*19c3b8c2SApple OSS Distributions 		return NULL;
5555*19c3b8c2SApple OSS Distributions 	}
5556*19c3b8c2SApple OSS Distributions 	if ((kIOMapDefaultCache != (_options & kIOMapCacheMask))
5557*19c3b8c2SApple OSS Distributions 	    && ((fOptions ^ _options) & kIOMapCacheMask)) {
5558*19c3b8c2SApple OSS Distributions 		return NULL;
5559*19c3b8c2SApple OSS Distributions 	}
5560*19c3b8c2SApple OSS Distributions 
5561*19c3b8c2SApple OSS Distributions 	if ((0 == (_options & kIOMapAnywhere)) && (fAddress != toAddress)) {
5562*19c3b8c2SApple OSS Distributions 		return NULL;
5563*19c3b8c2SApple OSS Distributions 	}
5564*19c3b8c2SApple OSS Distributions 
5565*19c3b8c2SApple OSS Distributions 	if (_offset < fOffset) {
5566*19c3b8c2SApple OSS Distributions 		return NULL;
5567*19c3b8c2SApple OSS Distributions 	}
5568*19c3b8c2SApple OSS Distributions 
5569*19c3b8c2SApple OSS Distributions 	_offset -= fOffset;
5570*19c3b8c2SApple OSS Distributions 
5571*19c3b8c2SApple OSS Distributions 	if ((_offset + _length) > fLength) {
5572*19c3b8c2SApple OSS Distributions 		return NULL;
5573*19c3b8c2SApple OSS Distributions 	}
5574*19c3b8c2SApple OSS Distributions 
5575*19c3b8c2SApple OSS Distributions 	if ((fLength == _length) && (!_offset)) {
5576*19c3b8c2SApple OSS Distributions 		retain();
5577*19c3b8c2SApple OSS Distributions 		newMapping = this;
5578*19c3b8c2SApple OSS Distributions 	} else {
5579*19c3b8c2SApple OSS Distributions 		newMapping->fSuperMap.reset(this, OSRetain);
5580*19c3b8c2SApple OSS Distributions 		newMapping->fOffset   = fOffset + _offset;
5581*19c3b8c2SApple OSS Distributions 		newMapping->fAddress  = fAddress + _offset;
5582*19c3b8c2SApple OSS Distributions 	}
5583*19c3b8c2SApple OSS Distributions 
5584*19c3b8c2SApple OSS Distributions 	return newMapping;
5585*19c3b8c2SApple OSS Distributions }
5586*19c3b8c2SApple OSS Distributions 
5587*19c3b8c2SApple OSS Distributions IOReturn
wireRange(uint32_t options,mach_vm_size_t offset,mach_vm_size_t length)5588*19c3b8c2SApple OSS Distributions IOMemoryMap::wireRange(
5589*19c3b8c2SApple OSS Distributions 	uint32_t                options,
5590*19c3b8c2SApple OSS Distributions 	mach_vm_size_t          offset,
5591*19c3b8c2SApple OSS Distributions 	mach_vm_size_t          length)
5592*19c3b8c2SApple OSS Distributions {
5593*19c3b8c2SApple OSS Distributions 	IOReturn kr;
5594*19c3b8c2SApple OSS Distributions 	mach_vm_address_t start = trunc_page_64(fAddress + offset);
5595*19c3b8c2SApple OSS Distributions 	mach_vm_address_t end   = round_page_64(fAddress + offset + length);
5596*19c3b8c2SApple OSS Distributions 	vm_prot_t prot;
5597*19c3b8c2SApple OSS Distributions 
5598*19c3b8c2SApple OSS Distributions 	prot = (kIODirectionOutIn & options);
5599*19c3b8c2SApple OSS Distributions 	if (prot) {
5600*19c3b8c2SApple OSS Distributions 		kr = vm_map_wire_kernel(fAddressMap, start, end, prot, (vm_tag_t) fMemory->getVMTag(kernel_map), FALSE);
5601*19c3b8c2SApple OSS Distributions 	} else {
5602*19c3b8c2SApple OSS Distributions 		kr = vm_map_unwire(fAddressMap, start, end, FALSE);
5603*19c3b8c2SApple OSS Distributions 	}
5604*19c3b8c2SApple OSS Distributions 
5605*19c3b8c2SApple OSS Distributions 	return kr;
5606*19c3b8c2SApple OSS Distributions }
5607*19c3b8c2SApple OSS Distributions 
5608*19c3b8c2SApple OSS Distributions 
5609*19c3b8c2SApple OSS Distributions IOPhysicalAddress
5610*19c3b8c2SApple OSS Distributions #ifdef __LP64__
getPhysicalSegment(IOByteCount _offset,IOPhysicalLength * _length,IOOptionBits _options)5611*19c3b8c2SApple OSS Distributions IOMemoryMap::getPhysicalSegment( IOByteCount _offset, IOPhysicalLength * _length, IOOptionBits _options)
5612*19c3b8c2SApple OSS Distributions #else /* !__LP64__ */
5613*19c3b8c2SApple OSS Distributions IOMemoryMap::getPhysicalSegment( IOByteCount _offset, IOPhysicalLength * _length)
5614*19c3b8c2SApple OSS Distributions #endif /* !__LP64__ */
5615*19c3b8c2SApple OSS Distributions {
5616*19c3b8c2SApple OSS Distributions 	IOPhysicalAddress   address;
5617*19c3b8c2SApple OSS Distributions 
5618*19c3b8c2SApple OSS Distributions 	LOCK;
5619*19c3b8c2SApple OSS Distributions #ifdef __LP64__
5620*19c3b8c2SApple OSS Distributions 	address = fMemory->getPhysicalSegment( fOffset + _offset, _length, _options );
5621*19c3b8c2SApple OSS Distributions #else /* !__LP64__ */
5622*19c3b8c2SApple OSS Distributions 	address = fMemory->getPhysicalSegment( fOffset + _offset, _length );
5623*19c3b8c2SApple OSS Distributions #endif /* !__LP64__ */
5624*19c3b8c2SApple OSS Distributions 	UNLOCK;
5625*19c3b8c2SApple OSS Distributions 
5626*19c3b8c2SApple OSS Distributions 	return address;
5627*19c3b8c2SApple OSS Distributions }
5628*19c3b8c2SApple OSS Distributions 
5629*19c3b8c2SApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
5630*19c3b8c2SApple OSS Distributions 
5631*19c3b8c2SApple OSS Distributions #undef super
5632*19c3b8c2SApple OSS Distributions #define super OSObject
5633*19c3b8c2SApple OSS Distributions 
5634*19c3b8c2SApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
5635*19c3b8c2SApple OSS Distributions 
5636*19c3b8c2SApple OSS Distributions void
initialize(void)5637*19c3b8c2SApple OSS Distributions IOMemoryDescriptor::initialize( void )
5638*19c3b8c2SApple OSS Distributions {
5639*19c3b8c2SApple OSS Distributions 	if (NULL == gIOMemoryLock) {
5640*19c3b8c2SApple OSS Distributions 		gIOMemoryLock = IORecursiveLockAlloc();
5641*19c3b8c2SApple OSS Distributions 	}
5642*19c3b8c2SApple OSS Distributions 
5643*19c3b8c2SApple OSS Distributions 	gIOLastPage = IOGetLastPageNumber();
5644*19c3b8c2SApple OSS Distributions }
5645*19c3b8c2SApple OSS Distributions 
5646*19c3b8c2SApple OSS Distributions void
free(void)5647*19c3b8c2SApple OSS Distributions IOMemoryDescriptor::free( void )
5648*19c3b8c2SApple OSS Distributions {
5649*19c3b8c2SApple OSS Distributions 	if (_mappings) {
5650*19c3b8c2SApple OSS Distributions 		_mappings.reset();
5651*19c3b8c2SApple OSS Distributions 	}
5652*19c3b8c2SApple OSS Distributions 
5653*19c3b8c2SApple OSS Distributions 	if (reserved) {
5654*19c3b8c2SApple OSS Distributions 		cleanKernelReserved(reserved);
5655*19c3b8c2SApple OSS Distributions 		IOFreeType(reserved, IOMemoryDescriptorReserved);
5656*19c3b8c2SApple OSS Distributions 		reserved = NULL;
5657*19c3b8c2SApple OSS Distributions 	}
5658*19c3b8c2SApple OSS Distributions 	super::free();
5659*19c3b8c2SApple OSS Distributions }
5660*19c3b8c2SApple OSS Distributions 
5661*19c3b8c2SApple OSS Distributions OSSharedPtr<IOMemoryMap>
setMapping(task_t intoTask,IOVirtualAddress mapAddress,IOOptionBits options)5662*19c3b8c2SApple OSS Distributions IOMemoryDescriptor::setMapping(
5663*19c3b8c2SApple OSS Distributions 	task_t                  intoTask,
5664*19c3b8c2SApple OSS Distributions 	IOVirtualAddress        mapAddress,
5665*19c3b8c2SApple OSS Distributions 	IOOptionBits            options )
5666*19c3b8c2SApple OSS Distributions {
5667*19c3b8c2SApple OSS Distributions 	return createMappingInTask( intoTask, mapAddress,
5668*19c3b8c2SApple OSS Distributions 	           options | kIOMapStatic,
5669*19c3b8c2SApple OSS Distributions 	           0, getLength());
5670*19c3b8c2SApple OSS Distributions }
5671*19c3b8c2SApple OSS Distributions 
5672*19c3b8c2SApple OSS Distributions OSSharedPtr<IOMemoryMap>
map(IOOptionBits options)5673*19c3b8c2SApple OSS Distributions IOMemoryDescriptor::map(
5674*19c3b8c2SApple OSS Distributions 	IOOptionBits            options )
5675*19c3b8c2SApple OSS Distributions {
5676*19c3b8c2SApple OSS Distributions 	return createMappingInTask( kernel_task, 0,
5677*19c3b8c2SApple OSS Distributions 	           options | kIOMapAnywhere,
5678*19c3b8c2SApple OSS Distributions 	           0, getLength());
5679*19c3b8c2SApple OSS Distributions }
5680*19c3b8c2SApple OSS Distributions 
5681*19c3b8c2SApple OSS Distributions #ifndef __LP64__
5682*19c3b8c2SApple OSS Distributions OSSharedPtr<IOMemoryMap>
map(task_t intoTask,IOVirtualAddress atAddress,IOOptionBits options,IOByteCount offset,IOByteCount length)5683*19c3b8c2SApple OSS Distributions IOMemoryDescriptor::map(
5684*19c3b8c2SApple OSS Distributions 	task_t                  intoTask,
5685*19c3b8c2SApple OSS Distributions 	IOVirtualAddress        atAddress,
5686*19c3b8c2SApple OSS Distributions 	IOOptionBits            options,
5687*19c3b8c2SApple OSS Distributions 	IOByteCount             offset,
5688*19c3b8c2SApple OSS Distributions 	IOByteCount             length )
5689*19c3b8c2SApple OSS Distributions {
5690*19c3b8c2SApple OSS Distributions 	if ((!(kIOMapAnywhere & options)) && vm_map_is_64bit(get_task_map(intoTask))) {
5691*19c3b8c2SApple OSS Distributions 		OSReportWithBacktrace("IOMemoryDescriptor::map() in 64b task, use ::createMappingInTask()");
5692*19c3b8c2SApple OSS Distributions 		return NULL;
5693*19c3b8c2SApple OSS Distributions 	}
5694*19c3b8c2SApple OSS Distributions 
5695*19c3b8c2SApple OSS Distributions 	return createMappingInTask(intoTask, atAddress,
5696*19c3b8c2SApple OSS Distributions 	           options, offset, length);
5697*19c3b8c2SApple OSS Distributions }
5698*19c3b8c2SApple OSS Distributions #endif /* !__LP64__ */
5699*19c3b8c2SApple OSS Distributions 
5700*19c3b8c2SApple OSS Distributions OSSharedPtr<IOMemoryMap>
createMappingInTask(task_t intoTask,mach_vm_address_t atAddress,IOOptionBits options,mach_vm_size_t offset,mach_vm_size_t length)5701*19c3b8c2SApple OSS Distributions IOMemoryDescriptor::createMappingInTask(
5702*19c3b8c2SApple OSS Distributions 	task_t                  intoTask,
5703*19c3b8c2SApple OSS Distributions 	mach_vm_address_t       atAddress,
5704*19c3b8c2SApple OSS Distributions 	IOOptionBits            options,
5705*19c3b8c2SApple OSS Distributions 	mach_vm_size_t          offset,
5706*19c3b8c2SApple OSS Distributions 	mach_vm_size_t          length)
5707*19c3b8c2SApple OSS Distributions {
5708*19c3b8c2SApple OSS Distributions 	IOMemoryMap * result;
5709*19c3b8c2SApple OSS Distributions 	IOMemoryMap * mapping;
5710*19c3b8c2SApple OSS Distributions 
5711*19c3b8c2SApple OSS Distributions 	if (0 == length) {
5712*19c3b8c2SApple OSS Distributions 		length = getLength();
5713*19c3b8c2SApple OSS Distributions 	}
5714*19c3b8c2SApple OSS Distributions 
5715*19c3b8c2SApple OSS Distributions 	mapping = new IOMemoryMap;
5716*19c3b8c2SApple OSS Distributions 
5717*19c3b8c2SApple OSS Distributions 	if (mapping
5718*19c3b8c2SApple OSS Distributions 	    && !mapping->init( intoTask, atAddress,
5719*19c3b8c2SApple OSS Distributions 	    options, offset, length )) {
5720*19c3b8c2SApple OSS Distributions 		mapping->release();
5721*19c3b8c2SApple OSS Distributions 		mapping = NULL;
5722*19c3b8c2SApple OSS Distributions 	}
5723*19c3b8c2SApple OSS Distributions 
5724*19c3b8c2SApple OSS Distributions 	if (mapping) {
5725*19c3b8c2SApple OSS Distributions 		result = makeMapping(this, intoTask, (IOVirtualAddress) mapping, options | kIOMap64Bit, 0, 0);
5726*19c3b8c2SApple OSS Distributions 	} else {
5727*19c3b8c2SApple OSS Distributions 		result = nullptr;
5728*19c3b8c2SApple OSS Distributions 	}
5729*19c3b8c2SApple OSS Distributions 
5730*19c3b8c2SApple OSS Distributions #if DEBUG
5731*19c3b8c2SApple OSS Distributions 	if (!result) {
5732*19c3b8c2SApple OSS Distributions 		IOLog("createMappingInTask failed desc %p, addr %qx, options %x, offset %qx, length %llx\n",
5733*19c3b8c2SApple OSS Distributions 		    this, atAddress, (uint32_t) options, offset, length);
5734*19c3b8c2SApple OSS Distributions 	}
5735*19c3b8c2SApple OSS Distributions #endif
5736*19c3b8c2SApple OSS Distributions 
5737*19c3b8c2SApple OSS Distributions 	// already retained through makeMapping
5738*19c3b8c2SApple OSS Distributions 	OSSharedPtr<IOMemoryMap> retval(result, OSNoRetain);
5739*19c3b8c2SApple OSS Distributions 
5740*19c3b8c2SApple OSS Distributions 	return retval;
5741*19c3b8c2SApple OSS Distributions }
5742*19c3b8c2SApple OSS Distributions 
5743*19c3b8c2SApple OSS Distributions #ifndef __LP64__ // there is only a 64 bit version for LP64
5744*19c3b8c2SApple OSS Distributions IOReturn
redirect(IOMemoryDescriptor * newBackingMemory,IOOptionBits options,IOByteCount offset)5745*19c3b8c2SApple OSS Distributions IOMemoryMap::redirect(IOMemoryDescriptor * newBackingMemory,
5746*19c3b8c2SApple OSS Distributions     IOOptionBits         options,
5747*19c3b8c2SApple OSS Distributions     IOByteCount          offset)
5748*19c3b8c2SApple OSS Distributions {
5749*19c3b8c2SApple OSS Distributions 	return redirect(newBackingMemory, options, (mach_vm_size_t)offset);
5750*19c3b8c2SApple OSS Distributions }
5751*19c3b8c2SApple OSS Distributions #endif
5752*19c3b8c2SApple OSS Distributions 
5753*19c3b8c2SApple OSS Distributions IOReturn
redirect(IOMemoryDescriptor * newBackingMemory,IOOptionBits options,mach_vm_size_t offset)5754*19c3b8c2SApple OSS Distributions IOMemoryMap::redirect(IOMemoryDescriptor * newBackingMemory,
5755*19c3b8c2SApple OSS Distributions     IOOptionBits         options,
5756*19c3b8c2SApple OSS Distributions     mach_vm_size_t       offset)
5757*19c3b8c2SApple OSS Distributions {
5758*19c3b8c2SApple OSS Distributions 	IOReturn err = kIOReturnSuccess;
5759*19c3b8c2SApple OSS Distributions 	OSSharedPtr<IOMemoryDescriptor> physMem;
5760*19c3b8c2SApple OSS Distributions 
5761*19c3b8c2SApple OSS Distributions 	LOCK;
5762*19c3b8c2SApple OSS Distributions 
5763*19c3b8c2SApple OSS Distributions 	if (fAddress && fAddressMap) {
5764*19c3b8c2SApple OSS Distributions 		do{
5765*19c3b8c2SApple OSS Distributions 			if (((fMemory->_flags & kIOMemoryTypeMask) == kIOMemoryTypePhysical)
5766*19c3b8c2SApple OSS Distributions 			    || ((fMemory->_flags & kIOMemoryTypeMask) == kIOMemoryTypePhysical64)) {
5767*19c3b8c2SApple OSS Distributions 				physMem = fMemory;
5768*19c3b8c2SApple OSS Distributions 			}
5769*19c3b8c2SApple OSS Distributions 
5770*19c3b8c2SApple OSS Distributions 			if (!fRedirUPL && fMemory->_memRef && (1 == fMemory->_memRef->count)) {
5771*19c3b8c2SApple OSS Distributions 				upl_size_t          size = (typeof(size))round_page(fLength);
5772*19c3b8c2SApple OSS Distributions 				upl_control_flags_t flags = UPL_COPYOUT_FROM | UPL_SET_INTERNAL
5773*19c3b8c2SApple OSS Distributions 				    | UPL_SET_LITE | UPL_SET_IO_WIRE | UPL_BLOCK_ACCESS;
5774*19c3b8c2SApple OSS Distributions 				if (KERN_SUCCESS != memory_object_iopl_request(fMemory->_memRef->entries[0].entry, 0, &size, &fRedirUPL,
5775*19c3b8c2SApple OSS Distributions 				    NULL, NULL,
5776*19c3b8c2SApple OSS Distributions 				    &flags, (vm_tag_t) fMemory->getVMTag(kernel_map))) {
5777*19c3b8c2SApple OSS Distributions 					fRedirUPL = NULL;
5778*19c3b8c2SApple OSS Distributions 				}
5779*19c3b8c2SApple OSS Distributions 
5780*19c3b8c2SApple OSS Distributions 				if (physMem) {
5781*19c3b8c2SApple OSS Distributions 					IOUnmapPages( fAddressMap, fAddress, fLength );
5782*19c3b8c2SApple OSS Distributions 					if ((false)) {
5783*19c3b8c2SApple OSS Distributions 						physMem->redirect(NULL, true);
5784*19c3b8c2SApple OSS Distributions 					}
5785*19c3b8c2SApple OSS Distributions 				}
5786*19c3b8c2SApple OSS Distributions 			}
5787*19c3b8c2SApple OSS Distributions 
5788*19c3b8c2SApple OSS Distributions 			if (newBackingMemory) {
5789*19c3b8c2SApple OSS Distributions 				if (newBackingMemory != fMemory) {
5790*19c3b8c2SApple OSS Distributions 					fOffset = 0;
5791*19c3b8c2SApple OSS Distributions 					if (this != newBackingMemory->makeMapping(newBackingMemory, fAddressTask, (IOVirtualAddress) this,
5792*19c3b8c2SApple OSS Distributions 					    options | kIOMapUnique | kIOMapReference | kIOMap64Bit,
5793*19c3b8c2SApple OSS Distributions 					    offset, fLength)) {
5794*19c3b8c2SApple OSS Distributions 						err = kIOReturnError;
5795*19c3b8c2SApple OSS Distributions 					}
5796*19c3b8c2SApple OSS Distributions 				}
5797*19c3b8c2SApple OSS Distributions 				if (fRedirUPL) {
5798*19c3b8c2SApple OSS Distributions 					upl_commit(fRedirUPL, NULL, 0);
5799*19c3b8c2SApple OSS Distributions 					upl_deallocate(fRedirUPL);
5800*19c3b8c2SApple OSS Distributions 					fRedirUPL = NULL;
5801*19c3b8c2SApple OSS Distributions 				}
5802*19c3b8c2SApple OSS Distributions 				if ((false) && physMem) {
5803*19c3b8c2SApple OSS Distributions 					physMem->redirect(NULL, false);
5804*19c3b8c2SApple OSS Distributions 				}
5805*19c3b8c2SApple OSS Distributions 			}
5806*19c3b8c2SApple OSS Distributions 		}while (false);
5807*19c3b8c2SApple OSS Distributions 	}
5808*19c3b8c2SApple OSS Distributions 
5809*19c3b8c2SApple OSS Distributions 	UNLOCK;
5810*19c3b8c2SApple OSS Distributions 
5811*19c3b8c2SApple OSS Distributions 	return err;
5812*19c3b8c2SApple OSS Distributions }
5813*19c3b8c2SApple OSS Distributions 
5814*19c3b8c2SApple OSS Distributions IOMemoryMap *
makeMapping(IOMemoryDescriptor * owner,task_t __intoTask,IOVirtualAddress __address,IOOptionBits options,IOByteCount __offset,IOByteCount __length)5815*19c3b8c2SApple OSS Distributions IOMemoryDescriptor::makeMapping(
5816*19c3b8c2SApple OSS Distributions 	IOMemoryDescriptor *    owner,
5817*19c3b8c2SApple OSS Distributions 	task_t                  __intoTask,
5818*19c3b8c2SApple OSS Distributions 	IOVirtualAddress        __address,
5819*19c3b8c2SApple OSS Distributions 	IOOptionBits            options,
5820*19c3b8c2SApple OSS Distributions 	IOByteCount             __offset,
5821*19c3b8c2SApple OSS Distributions 	IOByteCount             __length )
5822*19c3b8c2SApple OSS Distributions {
5823*19c3b8c2SApple OSS Distributions #ifndef __LP64__
5824*19c3b8c2SApple OSS Distributions 	if (!(kIOMap64Bit & options)) {
5825*19c3b8c2SApple OSS Distributions 		panic("IOMemoryDescriptor::makeMapping !64bit");
5826*19c3b8c2SApple OSS Distributions 	}
5827*19c3b8c2SApple OSS Distributions #endif /* !__LP64__ */
5828*19c3b8c2SApple OSS Distributions 
5829*19c3b8c2SApple OSS Distributions 	OSSharedPtr<IOMemoryDescriptor> mapDesc;
5830*19c3b8c2SApple OSS Distributions 	__block IOMemoryMap * result  = NULL;
5831*19c3b8c2SApple OSS Distributions 
5832*19c3b8c2SApple OSS Distributions 	IOMemoryMap *  mapping = (IOMemoryMap *) __address;
5833*19c3b8c2SApple OSS Distributions 	mach_vm_size_t offset  = mapping->fOffset + __offset;
5834*19c3b8c2SApple OSS Distributions 	mach_vm_size_t length  = mapping->fLength;
5835*19c3b8c2SApple OSS Distributions 
5836*19c3b8c2SApple OSS Distributions 	mapping->fOffset = offset;
5837*19c3b8c2SApple OSS Distributions 
5838*19c3b8c2SApple OSS Distributions 	LOCK;
5839*19c3b8c2SApple OSS Distributions 
5840*19c3b8c2SApple OSS Distributions 	do{
5841*19c3b8c2SApple OSS Distributions 		if (kIOMapStatic & options) {
5842*19c3b8c2SApple OSS Distributions 			result = mapping;
5843*19c3b8c2SApple OSS Distributions 			addMapping(mapping);
5844*19c3b8c2SApple OSS Distributions 			mapping->setMemoryDescriptor(this, 0);
5845*19c3b8c2SApple OSS Distributions 			continue;
5846*19c3b8c2SApple OSS Distributions 		}
5847*19c3b8c2SApple OSS Distributions 
5848*19c3b8c2SApple OSS Distributions 		if (kIOMapUnique & options) {
5849*19c3b8c2SApple OSS Distributions 			addr64_t phys;
5850*19c3b8c2SApple OSS Distributions 			IOByteCount       physLen;
5851*19c3b8c2SApple OSS Distributions 
5852*19c3b8c2SApple OSS Distributions //	    if (owner != this)		continue;
5853*19c3b8c2SApple OSS Distributions 
5854*19c3b8c2SApple OSS Distributions 			if (((_flags & kIOMemoryTypeMask) == kIOMemoryTypePhysical)
5855*19c3b8c2SApple OSS Distributions 			    || ((_flags & kIOMemoryTypeMask) == kIOMemoryTypePhysical64)) {
5856*19c3b8c2SApple OSS Distributions 				phys = getPhysicalSegment(offset, &physLen, kIOMemoryMapperNone);
5857*19c3b8c2SApple OSS Distributions 				if (!phys || (physLen < length)) {
5858*19c3b8c2SApple OSS Distributions 					continue;
5859*19c3b8c2SApple OSS Distributions 				}
5860*19c3b8c2SApple OSS Distributions 
5861*19c3b8c2SApple OSS Distributions 				mapDesc = IOMemoryDescriptor::withAddressRange(
5862*19c3b8c2SApple OSS Distributions 					phys, length, getDirection() | kIOMemoryMapperNone, NULL);
5863*19c3b8c2SApple OSS Distributions 				if (!mapDesc) {
5864*19c3b8c2SApple OSS Distributions 					continue;
5865*19c3b8c2SApple OSS Distributions 				}
5866*19c3b8c2SApple OSS Distributions 				offset = 0;
5867*19c3b8c2SApple OSS Distributions 				mapping->fOffset = offset;
5868*19c3b8c2SApple OSS Distributions 			}
5869*19c3b8c2SApple OSS Distributions 		} else {
5870*19c3b8c2SApple OSS Distributions 			// look for a compatible existing mapping
5871*19c3b8c2SApple OSS Distributions 			if (_mappings) {
5872*19c3b8c2SApple OSS Distributions 				_mappings->iterateObjects(^(OSObject * object)
5873*19c3b8c2SApple OSS Distributions 				{
5874*19c3b8c2SApple OSS Distributions 					IOMemoryMap * lookMapping = (IOMemoryMap *) object;
5875*19c3b8c2SApple OSS Distributions 					if ((result = lookMapping->copyCompatible(mapping))) {
5876*19c3b8c2SApple OSS Distributions 					        addMapping(result);
5877*19c3b8c2SApple OSS Distributions 					        result->setMemoryDescriptor(this, offset);
5878*19c3b8c2SApple OSS Distributions 					        return true;
5879*19c3b8c2SApple OSS Distributions 					}
5880*19c3b8c2SApple OSS Distributions 					return false;
5881*19c3b8c2SApple OSS Distributions 				});
5882*19c3b8c2SApple OSS Distributions 			}
5883*19c3b8c2SApple OSS Distributions 			if (result || (options & kIOMapReference)) {
5884*19c3b8c2SApple OSS Distributions 				if (result != mapping) {
5885*19c3b8c2SApple OSS Distributions 					mapping->release();
5886*19c3b8c2SApple OSS Distributions 					mapping = NULL;
5887*19c3b8c2SApple OSS Distributions 				}
5888*19c3b8c2SApple OSS Distributions 				continue;
5889*19c3b8c2SApple OSS Distributions 			}
5890*19c3b8c2SApple OSS Distributions 		}
5891*19c3b8c2SApple OSS Distributions 
5892*19c3b8c2SApple OSS Distributions 		if (!mapDesc) {
5893*19c3b8c2SApple OSS Distributions 			mapDesc.reset(this, OSRetain);
5894*19c3b8c2SApple OSS Distributions 		}
5895*19c3b8c2SApple OSS Distributions 		IOReturn
5896*19c3b8c2SApple OSS Distributions 		    kr = mapDesc->doMap( NULL, (IOVirtualAddress *) &mapping, options, 0, 0 );
5897*19c3b8c2SApple OSS Distributions 		if (kIOReturnSuccess == kr) {
5898*19c3b8c2SApple OSS Distributions 			result = mapping;
5899*19c3b8c2SApple OSS Distributions 			mapDesc->addMapping(result);
5900*19c3b8c2SApple OSS Distributions 			result->setMemoryDescriptor(mapDesc.get(), offset);
5901*19c3b8c2SApple OSS Distributions 		} else {
5902*19c3b8c2SApple OSS Distributions 			mapping->release();
5903*19c3b8c2SApple OSS Distributions 			mapping = NULL;
5904*19c3b8c2SApple OSS Distributions 		}
5905*19c3b8c2SApple OSS Distributions 	}while (false);
5906*19c3b8c2SApple OSS Distributions 
5907*19c3b8c2SApple OSS Distributions 	UNLOCK;
5908*19c3b8c2SApple OSS Distributions 
5909*19c3b8c2SApple OSS Distributions 	return result;
5910*19c3b8c2SApple OSS Distributions }
5911*19c3b8c2SApple OSS Distributions 
5912*19c3b8c2SApple OSS Distributions void
addMapping(IOMemoryMap * mapping)5913*19c3b8c2SApple OSS Distributions IOMemoryDescriptor::addMapping(
5914*19c3b8c2SApple OSS Distributions 	IOMemoryMap * mapping )
5915*19c3b8c2SApple OSS Distributions {
5916*19c3b8c2SApple OSS Distributions 	if (mapping) {
5917*19c3b8c2SApple OSS Distributions 		if (NULL == _mappings) {
5918*19c3b8c2SApple OSS Distributions 			_mappings = OSSet::withCapacity(1);
5919*19c3b8c2SApple OSS Distributions 		}
5920*19c3b8c2SApple OSS Distributions 		if (_mappings) {
5921*19c3b8c2SApple OSS Distributions 			_mappings->setObject( mapping );
5922*19c3b8c2SApple OSS Distributions 		}
5923*19c3b8c2SApple OSS Distributions 	}
5924*19c3b8c2SApple OSS Distributions }
5925*19c3b8c2SApple OSS Distributions 
5926*19c3b8c2SApple OSS Distributions void
removeMapping(IOMemoryMap * mapping)5927*19c3b8c2SApple OSS Distributions IOMemoryDescriptor::removeMapping(
5928*19c3b8c2SApple OSS Distributions 	IOMemoryMap * mapping )
5929*19c3b8c2SApple OSS Distributions {
5930*19c3b8c2SApple OSS Distributions 	if (_mappings) {
5931*19c3b8c2SApple OSS Distributions 		_mappings->removeObject( mapping);
5932*19c3b8c2SApple OSS Distributions 	}
5933*19c3b8c2SApple OSS Distributions }
5934*19c3b8c2SApple OSS Distributions 
5935*19c3b8c2SApple OSS Distributions void
setMapperOptions(uint16_t options)5936*19c3b8c2SApple OSS Distributions IOMemoryDescriptor::setMapperOptions( uint16_t options)
5937*19c3b8c2SApple OSS Distributions {
5938*19c3b8c2SApple OSS Distributions 	_iomapperOptions = options;
5939*19c3b8c2SApple OSS Distributions }
5940*19c3b8c2SApple OSS Distributions 
5941*19c3b8c2SApple OSS Distributions uint16_t
getMapperOptions(void)5942*19c3b8c2SApple OSS Distributions IOMemoryDescriptor::getMapperOptions( void )
5943*19c3b8c2SApple OSS Distributions {
5944*19c3b8c2SApple OSS Distributions 	return _iomapperOptions;
5945*19c3b8c2SApple OSS Distributions }
5946*19c3b8c2SApple OSS Distributions 
5947*19c3b8c2SApple OSS Distributions #ifndef __LP64__
5948*19c3b8c2SApple OSS Distributions // obsolete initializers
5949*19c3b8c2SApple OSS Distributions // - initWithOptions is the designated initializer
5950*19c3b8c2SApple OSS Distributions bool
initWithAddress(void * address,IOByteCount length,IODirection direction)5951*19c3b8c2SApple OSS Distributions IOMemoryDescriptor::initWithAddress(void *      address,
5952*19c3b8c2SApple OSS Distributions     IOByteCount   length,
5953*19c3b8c2SApple OSS Distributions     IODirection direction)
5954*19c3b8c2SApple OSS Distributions {
5955*19c3b8c2SApple OSS Distributions 	return false;
5956*19c3b8c2SApple OSS Distributions }
5957*19c3b8c2SApple OSS Distributions 
5958*19c3b8c2SApple OSS Distributions bool
initWithAddress(IOVirtualAddress address,IOByteCount length,IODirection direction,task_t task)5959*19c3b8c2SApple OSS Distributions IOMemoryDescriptor::initWithAddress(IOVirtualAddress address,
5960*19c3b8c2SApple OSS Distributions     IOByteCount    length,
5961*19c3b8c2SApple OSS Distributions     IODirection  direction,
5962*19c3b8c2SApple OSS Distributions     task_t       task)
5963*19c3b8c2SApple OSS Distributions {
5964*19c3b8c2SApple OSS Distributions 	return false;
5965*19c3b8c2SApple OSS Distributions }
5966*19c3b8c2SApple OSS Distributions 
5967*19c3b8c2SApple OSS Distributions bool
initWithPhysicalAddress(IOPhysicalAddress address,IOByteCount length,IODirection direction)5968*19c3b8c2SApple OSS Distributions IOMemoryDescriptor::initWithPhysicalAddress(
5969*19c3b8c2SApple OSS Distributions 	IOPhysicalAddress      address,
5970*19c3b8c2SApple OSS Distributions 	IOByteCount            length,
5971*19c3b8c2SApple OSS Distributions 	IODirection            direction )
5972*19c3b8c2SApple OSS Distributions {
5973*19c3b8c2SApple OSS Distributions 	return false;
5974*19c3b8c2SApple OSS Distributions }
5975*19c3b8c2SApple OSS Distributions 
5976*19c3b8c2SApple OSS Distributions bool
initWithRanges(IOVirtualRange * ranges,UInt32 withCount,IODirection direction,task_t task,bool asReference)5977*19c3b8c2SApple OSS Distributions IOMemoryDescriptor::initWithRanges(
5978*19c3b8c2SApple OSS Distributions 	IOVirtualRange * ranges,
5979*19c3b8c2SApple OSS Distributions 	UInt32           withCount,
5980*19c3b8c2SApple OSS Distributions 	IODirection      direction,
5981*19c3b8c2SApple OSS Distributions 	task_t           task,
5982*19c3b8c2SApple OSS Distributions 	bool             asReference)
5983*19c3b8c2SApple OSS Distributions {
5984*19c3b8c2SApple OSS Distributions 	return false;
5985*19c3b8c2SApple OSS Distributions }
5986*19c3b8c2SApple OSS Distributions 
5987*19c3b8c2SApple OSS Distributions bool
initWithPhysicalRanges(IOPhysicalRange * ranges,UInt32 withCount,IODirection direction,bool asReference)5988*19c3b8c2SApple OSS Distributions IOMemoryDescriptor::initWithPhysicalRanges(     IOPhysicalRange * ranges,
5989*19c3b8c2SApple OSS Distributions     UInt32           withCount,
5990*19c3b8c2SApple OSS Distributions     IODirection      direction,
5991*19c3b8c2SApple OSS Distributions     bool             asReference)
5992*19c3b8c2SApple OSS Distributions {
5993*19c3b8c2SApple OSS Distributions 	return false;
5994*19c3b8c2SApple OSS Distributions }
5995*19c3b8c2SApple OSS Distributions 
5996*19c3b8c2SApple OSS Distributions void *
getVirtualSegment(IOByteCount offset,IOByteCount * lengthOfSegment)5997*19c3b8c2SApple OSS Distributions IOMemoryDescriptor::getVirtualSegment(IOByteCount offset,
5998*19c3b8c2SApple OSS Distributions     IOByteCount * lengthOfSegment)
5999*19c3b8c2SApple OSS Distributions {
6000*19c3b8c2SApple OSS Distributions 	return NULL;
6001*19c3b8c2SApple OSS Distributions }
6002*19c3b8c2SApple OSS Distributions #endif /* !__LP64__ */
6003*19c3b8c2SApple OSS Distributions 
6004*19c3b8c2SApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
6005*19c3b8c2SApple OSS Distributions 
6006*19c3b8c2SApple OSS Distributions bool
serialize(OSSerialize * s) const6007*19c3b8c2SApple OSS Distributions IOGeneralMemoryDescriptor::serialize(OSSerialize * s) const
6008*19c3b8c2SApple OSS Distributions {
6009*19c3b8c2SApple OSS Distributions 	OSSharedPtr<OSSymbol const>     keys[2] = {NULL};
6010*19c3b8c2SApple OSS Distributions 	OSSharedPtr<OSObject>           values[2] = {NULL};
6011*19c3b8c2SApple OSS Distributions 	OSSharedPtr<OSArray>            array;
6012*19c3b8c2SApple OSS Distributions 
6013*19c3b8c2SApple OSS Distributions 	struct SerData {
6014*19c3b8c2SApple OSS Distributions 		user_addr_t address;
6015*19c3b8c2SApple OSS Distributions 		user_size_t length;
6016*19c3b8c2SApple OSS Distributions 	};
6017*19c3b8c2SApple OSS Distributions 
6018*19c3b8c2SApple OSS Distributions 	unsigned int index;
6019*19c3b8c2SApple OSS Distributions 
6020*19c3b8c2SApple OSS Distributions 	IOOptionBits type = _flags & kIOMemoryTypeMask;
6021*19c3b8c2SApple OSS Distributions 
6022*19c3b8c2SApple OSS Distributions 	if (s == NULL) {
6023*19c3b8c2SApple OSS Distributions 		return false;
6024*19c3b8c2SApple OSS Distributions 	}
6025*19c3b8c2SApple OSS Distributions 
6026*19c3b8c2SApple OSS Distributions 	array = OSArray::withCapacity(4);
6027*19c3b8c2SApple OSS Distributions 	if (!array) {
6028*19c3b8c2SApple OSS Distributions 		return false;
6029*19c3b8c2SApple OSS Distributions 	}
6030*19c3b8c2SApple OSS Distributions 
6031*19c3b8c2SApple OSS Distributions 	OSDataAllocation<struct SerData> vcopy(_rangesCount, OSAllocateMemory);
6032*19c3b8c2SApple OSS Distributions 	if (!vcopy) {
6033*19c3b8c2SApple OSS Distributions 		return false;
6034*19c3b8c2SApple OSS Distributions 	}
6035*19c3b8c2SApple OSS Distributions 
6036*19c3b8c2SApple OSS Distributions 	keys[0] = OSSymbol::withCString("address");
6037*19c3b8c2SApple OSS Distributions 	keys[1] = OSSymbol::withCString("length");
6038*19c3b8c2SApple OSS Distributions 
6039*19c3b8c2SApple OSS Distributions 	// Copy the volatile data so we don't have to allocate memory
6040*19c3b8c2SApple OSS Distributions 	// while the lock is held.
6041*19c3b8c2SApple OSS Distributions 	LOCK;
6042*19c3b8c2SApple OSS Distributions 	if (vcopy.size() == _rangesCount) {
6043*19c3b8c2SApple OSS Distributions 		Ranges vec = _ranges;
6044*19c3b8c2SApple OSS Distributions 		for (index = 0; index < vcopy.size(); index++) {
6045*19c3b8c2SApple OSS Distributions 			mach_vm_address_t addr; mach_vm_size_t len;
6046*19c3b8c2SApple OSS Distributions 			getAddrLenForInd(addr, len, type, vec, index);
6047*19c3b8c2SApple OSS Distributions 			vcopy[index].address = addr;
6048*19c3b8c2SApple OSS Distributions 			vcopy[index].length  = len;
6049*19c3b8c2SApple OSS Distributions 		}
6050*19c3b8c2SApple OSS Distributions 	} else {
6051*19c3b8c2SApple OSS Distributions 		// The descriptor changed out from under us.  Give up.
6052*19c3b8c2SApple OSS Distributions 		UNLOCK;
6053*19c3b8c2SApple OSS Distributions 		return false;
6054*19c3b8c2SApple OSS Distributions 	}
6055*19c3b8c2SApple OSS Distributions 	UNLOCK;
6056*19c3b8c2SApple OSS Distributions 
6057*19c3b8c2SApple OSS Distributions 	for (index = 0; index < vcopy.size(); index++) {
6058*19c3b8c2SApple OSS Distributions 		user_addr_t addr = vcopy[index].address;
6059*19c3b8c2SApple OSS Distributions 		IOByteCount len = (IOByteCount) vcopy[index].length;
6060*19c3b8c2SApple OSS Distributions 		values[0] = OSNumber::withNumber(addr, sizeof(addr) * 8);
6061*19c3b8c2SApple OSS Distributions 		if (values[0] == NULL) {
6062*19c3b8c2SApple OSS Distributions 			return false;
6063*19c3b8c2SApple OSS Distributions 		}
6064*19c3b8c2SApple OSS Distributions 		values[1] = OSNumber::withNumber(len, sizeof(len) * 8);
6065*19c3b8c2SApple OSS Distributions 		if (values[1] == NULL) {
6066*19c3b8c2SApple OSS Distributions 			return false;
6067*19c3b8c2SApple OSS Distributions 		}
6068*19c3b8c2SApple OSS Distributions 		OSSharedPtr<OSDictionary> dict = OSDictionary::withObjects((const OSObject **)values, (const OSSymbol **)keys, 2);
6069*19c3b8c2SApple OSS Distributions 		if (dict == NULL) {
6070*19c3b8c2SApple OSS Distributions 			return false;
6071*19c3b8c2SApple OSS Distributions 		}
6072*19c3b8c2SApple OSS Distributions 		array->setObject(dict.get());
6073*19c3b8c2SApple OSS Distributions 		dict.reset();
6074*19c3b8c2SApple OSS Distributions 		values[0].reset();
6075*19c3b8c2SApple OSS Distributions 		values[1].reset();
6076*19c3b8c2SApple OSS Distributions 	}
6077*19c3b8c2SApple OSS Distributions 
6078*19c3b8c2SApple OSS Distributions 	return array->serialize(s);
6079*19c3b8c2SApple OSS Distributions }
6080*19c3b8c2SApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
6081*19c3b8c2SApple OSS Distributions 
6082*19c3b8c2SApple OSS Distributions OSMetaClassDefineReservedUsedX86(IOMemoryDescriptor, 0);
6083*19c3b8c2SApple OSS Distributions #ifdef __LP64__
6084*19c3b8c2SApple OSS Distributions OSMetaClassDefineReservedUnused(IOMemoryDescriptor, 1);
6085*19c3b8c2SApple OSS Distributions OSMetaClassDefineReservedUnused(IOMemoryDescriptor, 2);
6086*19c3b8c2SApple OSS Distributions OSMetaClassDefineReservedUnused(IOMemoryDescriptor, 3);
6087*19c3b8c2SApple OSS Distributions OSMetaClassDefineReservedUnused(IOMemoryDescriptor, 4);
6088*19c3b8c2SApple OSS Distributions OSMetaClassDefineReservedUnused(IOMemoryDescriptor, 5);
6089*19c3b8c2SApple OSS Distributions OSMetaClassDefineReservedUnused(IOMemoryDescriptor, 6);
6090*19c3b8c2SApple OSS Distributions OSMetaClassDefineReservedUnused(IOMemoryDescriptor, 7);
6091*19c3b8c2SApple OSS Distributions #else /* !__LP64__ */
6092*19c3b8c2SApple OSS Distributions OSMetaClassDefineReservedUsedX86(IOMemoryDescriptor, 1);
6093*19c3b8c2SApple OSS Distributions OSMetaClassDefineReservedUsedX86(IOMemoryDescriptor, 2);
6094*19c3b8c2SApple OSS Distributions OSMetaClassDefineReservedUsedX86(IOMemoryDescriptor, 3);
6095*19c3b8c2SApple OSS Distributions OSMetaClassDefineReservedUsedX86(IOMemoryDescriptor, 4);
6096*19c3b8c2SApple OSS Distributions OSMetaClassDefineReservedUsedX86(IOMemoryDescriptor, 5);
6097*19c3b8c2SApple OSS Distributions OSMetaClassDefineReservedUsedX86(IOMemoryDescriptor, 6);
6098*19c3b8c2SApple OSS Distributions OSMetaClassDefineReservedUsedX86(IOMemoryDescriptor, 7);
6099*19c3b8c2SApple OSS Distributions #endif /* !__LP64__ */
6100*19c3b8c2SApple OSS Distributions OSMetaClassDefineReservedUnused(IOMemoryDescriptor, 8);
6101*19c3b8c2SApple OSS Distributions OSMetaClassDefineReservedUnused(IOMemoryDescriptor, 9);
6102*19c3b8c2SApple OSS Distributions OSMetaClassDefineReservedUnused(IOMemoryDescriptor, 10);
6103*19c3b8c2SApple OSS Distributions OSMetaClassDefineReservedUnused(IOMemoryDescriptor, 11);
6104*19c3b8c2SApple OSS Distributions OSMetaClassDefineReservedUnused(IOMemoryDescriptor, 12);
6105*19c3b8c2SApple OSS Distributions OSMetaClassDefineReservedUnused(IOMemoryDescriptor, 13);
6106*19c3b8c2SApple OSS Distributions OSMetaClassDefineReservedUnused(IOMemoryDescriptor, 14);
6107*19c3b8c2SApple OSS Distributions OSMetaClassDefineReservedUnused(IOMemoryDescriptor, 15);
6108*19c3b8c2SApple OSS Distributions 
6109*19c3b8c2SApple OSS Distributions /* ex-inline function implementation */
6110*19c3b8c2SApple OSS Distributions IOPhysicalAddress
getPhysicalAddress()6111*19c3b8c2SApple OSS Distributions IOMemoryDescriptor::getPhysicalAddress()
6112*19c3b8c2SApple OSS Distributions {
6113*19c3b8c2SApple OSS Distributions 	return getPhysicalSegment( 0, NULL );
6114*19c3b8c2SApple OSS Distributions }
6115*19c3b8c2SApple OSS Distributions 
OSDefineMetaClassAndStructors(_IOMemoryDescriptorMixedData,OSObject)6116*19c3b8c2SApple OSS Distributions OSDefineMetaClassAndStructors(_IOMemoryDescriptorMixedData, OSObject)
6117*19c3b8c2SApple OSS Distributions 
6118*19c3b8c2SApple OSS Distributions OSPtr<_IOMemoryDescriptorMixedData>
6119*19c3b8c2SApple OSS Distributions _IOMemoryDescriptorMixedData::withCapacity(size_t capacity)
6120*19c3b8c2SApple OSS Distributions {
6121*19c3b8c2SApple OSS Distributions 	OSSharedPtr<_IOMemoryDescriptorMixedData> me = OSMakeShared<_IOMemoryDescriptorMixedData>();
6122*19c3b8c2SApple OSS Distributions 	if (me && !me->initWithCapacity(capacity)) {
6123*19c3b8c2SApple OSS Distributions 		return nullptr;
6124*19c3b8c2SApple OSS Distributions 	}
6125*19c3b8c2SApple OSS Distributions 	return me;
6126*19c3b8c2SApple OSS Distributions }
6127*19c3b8c2SApple OSS Distributions 
6128*19c3b8c2SApple OSS Distributions /*
6129*19c3b8c2SApple OSS Distributions  * Ignore -Wxnu-typed-allocators within IOMemoryDescriptorMixedData
6130*19c3b8c2SApple OSS Distributions  * because it implements an allocator.
6131*19c3b8c2SApple OSS Distributions  */
6132*19c3b8c2SApple OSS Distributions __typed_allocators_ignore_push
6133*19c3b8c2SApple OSS Distributions 
6134*19c3b8c2SApple OSS Distributions bool
initWithCapacity(size_t capacity)6135*19c3b8c2SApple OSS Distributions _IOMemoryDescriptorMixedData::initWithCapacity(size_t capacity)
6136*19c3b8c2SApple OSS Distributions {
6137*19c3b8c2SApple OSS Distributions 	if (_data && (!capacity || (_capacity < capacity))) {
6138*19c3b8c2SApple OSS Distributions 		freeMemory();
6139*19c3b8c2SApple OSS Distributions 	}
6140*19c3b8c2SApple OSS Distributions 
6141*19c3b8c2SApple OSS Distributions 	if (!OSObject::init()) {
6142*19c3b8c2SApple OSS Distributions 		return false;
6143*19c3b8c2SApple OSS Distributions 	}
6144*19c3b8c2SApple OSS Distributions 
6145*19c3b8c2SApple OSS Distributions 	if (!_data && capacity) {
6146*19c3b8c2SApple OSS Distributions 		_data = IOMalloc(capacity);
6147*19c3b8c2SApple OSS Distributions 		if (!_data) {
6148*19c3b8c2SApple OSS Distributions 			return false;
6149*19c3b8c2SApple OSS Distributions 		}
6150*19c3b8c2SApple OSS Distributions 		_capacity = capacity;
6151*19c3b8c2SApple OSS Distributions 	}
6152*19c3b8c2SApple OSS Distributions 
6153*19c3b8c2SApple OSS Distributions 	_length = 0;
6154*19c3b8c2SApple OSS Distributions 
6155*19c3b8c2SApple OSS Distributions 	return true;
6156*19c3b8c2SApple OSS Distributions }
6157*19c3b8c2SApple OSS Distributions 
6158*19c3b8c2SApple OSS Distributions void
free()6159*19c3b8c2SApple OSS Distributions _IOMemoryDescriptorMixedData::free()
6160*19c3b8c2SApple OSS Distributions {
6161*19c3b8c2SApple OSS Distributions 	freeMemory();
6162*19c3b8c2SApple OSS Distributions 	OSObject::free();
6163*19c3b8c2SApple OSS Distributions }
6164*19c3b8c2SApple OSS Distributions 
6165*19c3b8c2SApple OSS Distributions void
freeMemory()6166*19c3b8c2SApple OSS Distributions _IOMemoryDescriptorMixedData::freeMemory()
6167*19c3b8c2SApple OSS Distributions {
6168*19c3b8c2SApple OSS Distributions 	IOFree(_data, _capacity);
6169*19c3b8c2SApple OSS Distributions 	_data = nullptr;
6170*19c3b8c2SApple OSS Distributions 	_capacity = _length = 0;
6171*19c3b8c2SApple OSS Distributions }
6172*19c3b8c2SApple OSS Distributions 
6173*19c3b8c2SApple OSS Distributions bool
appendBytes(const void * bytes,size_t length)6174*19c3b8c2SApple OSS Distributions _IOMemoryDescriptorMixedData::appendBytes(const void * bytes, size_t length)
6175*19c3b8c2SApple OSS Distributions {
6176*19c3b8c2SApple OSS Distributions 	const auto oldLength = getLength();
6177*19c3b8c2SApple OSS Distributions 	size_t newLength;
6178*19c3b8c2SApple OSS Distributions 	if (os_add_overflow(oldLength, length, &newLength)) {
6179*19c3b8c2SApple OSS Distributions 		return false;
6180*19c3b8c2SApple OSS Distributions 	}
6181*19c3b8c2SApple OSS Distributions 
6182*19c3b8c2SApple OSS Distributions 	if (newLength > _capacity) {
6183*19c3b8c2SApple OSS Distributions 		void * const newData = IOMalloc(newLength);
6184*19c3b8c2SApple OSS Distributions 		if (!newData) {
6185*19c3b8c2SApple OSS Distributions 			return false;
6186*19c3b8c2SApple OSS Distributions 		}
6187*19c3b8c2SApple OSS Distributions 		if (_data) {
6188*19c3b8c2SApple OSS Distributions 			bcopy(_data, newData, oldLength);
6189*19c3b8c2SApple OSS Distributions 			IOFree(_data, _capacity);
6190*19c3b8c2SApple OSS Distributions 		}
6191*19c3b8c2SApple OSS Distributions 		_data = newData;
6192*19c3b8c2SApple OSS Distributions 		_capacity = newLength;
6193*19c3b8c2SApple OSS Distributions 	}
6194*19c3b8c2SApple OSS Distributions 
6195*19c3b8c2SApple OSS Distributions 	unsigned char * const dest = &(((unsigned char *)_data)[oldLength]);
6196*19c3b8c2SApple OSS Distributions 	if (bytes) {
6197*19c3b8c2SApple OSS Distributions 		bcopy(bytes, dest, length);
6198*19c3b8c2SApple OSS Distributions 	} else {
6199*19c3b8c2SApple OSS Distributions 		bzero(dest, length);
6200*19c3b8c2SApple OSS Distributions 	}
6201*19c3b8c2SApple OSS Distributions 
6202*19c3b8c2SApple OSS Distributions 	_length = newLength;
6203*19c3b8c2SApple OSS Distributions 
6204*19c3b8c2SApple OSS Distributions 	return true;
6205*19c3b8c2SApple OSS Distributions }
6206*19c3b8c2SApple OSS Distributions 
6207*19c3b8c2SApple OSS Distributions void
setLength(size_t length)6208*19c3b8c2SApple OSS Distributions _IOMemoryDescriptorMixedData::setLength(size_t length)
6209*19c3b8c2SApple OSS Distributions {
6210*19c3b8c2SApple OSS Distributions 	if (!_data || (length > _capacity)) {
6211*19c3b8c2SApple OSS Distributions 		void * const newData = IOMallocZero(length);
6212*19c3b8c2SApple OSS Distributions 		if (_data) {
6213*19c3b8c2SApple OSS Distributions 			bcopy(_data, newData, _length);
6214*19c3b8c2SApple OSS Distributions 			IOFree(_data, _capacity);
6215*19c3b8c2SApple OSS Distributions 		}
6216*19c3b8c2SApple OSS Distributions 		_data = newData;
6217*19c3b8c2SApple OSS Distributions 		_capacity = length;
6218*19c3b8c2SApple OSS Distributions 	}
6219*19c3b8c2SApple OSS Distributions 	_length = length;
6220*19c3b8c2SApple OSS Distributions }
6221*19c3b8c2SApple OSS Distributions 
6222*19c3b8c2SApple OSS Distributions __typed_allocators_ignore_pop
6223*19c3b8c2SApple OSS Distributions 
6224*19c3b8c2SApple OSS Distributions const void *
getBytes() const6225*19c3b8c2SApple OSS Distributions _IOMemoryDescriptorMixedData::getBytes() const
6226*19c3b8c2SApple OSS Distributions {
6227*19c3b8c2SApple OSS Distributions 	return _length ? _data : nullptr;
6228*19c3b8c2SApple OSS Distributions }
6229*19c3b8c2SApple OSS Distributions 
6230*19c3b8c2SApple OSS Distributions size_t
getLength() const6231*19c3b8c2SApple OSS Distributions _IOMemoryDescriptorMixedData::getLength() const
6232*19c3b8c2SApple OSS Distributions {
6233*19c3b8c2SApple OSS Distributions 	return _data ? _length : 0;
6234*19c3b8c2SApple OSS Distributions }
6235