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