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