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