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