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