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