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