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