1*1031c584SApple OSS Distributions /*
2*1031c584SApple OSS Distributions * Copyright (c) 2014-2020 Apple Inc. All rights reserved.
3*1031c584SApple OSS Distributions *
4*1031c584SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*1031c584SApple OSS Distributions *
6*1031c584SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*1031c584SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*1031c584SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*1031c584SApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*1031c584SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*1031c584SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*1031c584SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*1031c584SApple OSS Distributions * terms of an Apple operating system software license agreement.
14*1031c584SApple OSS Distributions *
15*1031c584SApple OSS Distributions * Please obtain a copy of the License at
16*1031c584SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*1031c584SApple OSS Distributions *
18*1031c584SApple OSS Distributions * The Original Code and all software distributed under the License are
19*1031c584SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*1031c584SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*1031c584SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*1031c584SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*1031c584SApple OSS Distributions * Please see the License for the specific language governing rights and
24*1031c584SApple OSS Distributions * limitations under the License.
25*1031c584SApple OSS Distributions *
26*1031c584SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*1031c584SApple OSS Distributions */
28*1031c584SApple OSS Distributions
29*1031c584SApple OSS Distributions #include <sys/cdefs.h>
30*1031c584SApple OSS Distributions
31*1031c584SApple OSS Distributions #include <IOKit/assert.h>
32*1031c584SApple OSS Distributions #include <IOKit/system.h>
33*1031c584SApple OSS Distributions #include <IOKit/IOLib.h>
34*1031c584SApple OSS Distributions #include <IOKit/IOMemoryDescriptor.h>
35*1031c584SApple OSS Distributions #include <IOKit/IOMapper.h>
36*1031c584SApple OSS Distributions #include <IOKit/IODMACommand.h>
37*1031c584SApple OSS Distributions #include <IOKit/IOKitKeysPrivate.h>
38*1031c584SApple OSS Distributions #include <Kernel/IOKitKernelInternal.h>
39*1031c584SApple OSS Distributions #include <IOKit/IOUserClient.h>
40*1031c584SApple OSS Distributions #include <IOKit/IOService.h>
41*1031c584SApple OSS Distributions #include "Tests.h"
42*1031c584SApple OSS Distributions
43*1031c584SApple OSS Distributions #ifndef __LP64__
44*1031c584SApple OSS Distributions #include <IOKit/IOSubMemoryDescriptor.h>
45*1031c584SApple OSS Distributions #endif /* !__LP64__ */
46*1031c584SApple OSS Distributions #include <IOKit/IOSubMemoryDescriptor.h>
47*1031c584SApple OSS Distributions #include <IOKit/IOMultiMemoryDescriptor.h>
48*1031c584SApple OSS Distributions #include <IOKit/IOBufferMemoryDescriptor.h>
49*1031c584SApple OSS Distributions #include <IOKit/IOGuardPageMemoryDescriptor.h>
50*1031c584SApple OSS Distributions
51*1031c584SApple OSS Distributions #include <IOKit/IOKitDebug.h>
52*1031c584SApple OSS Distributions #include <libkern/OSDebug.h>
53*1031c584SApple OSS Distributions #include <sys/uio.h>
54*1031c584SApple OSS Distributions #include <libkern/sysctl.h>
55*1031c584SApple OSS Distributions #include <sys/sysctl.h>
56*1031c584SApple OSS Distributions
57*1031c584SApple OSS Distributions __BEGIN_DECLS
58*1031c584SApple OSS Distributions #include <vm/pmap.h>
59*1031c584SApple OSS Distributions #include <vm/vm_pageout.h>
60*1031c584SApple OSS Distributions #include <mach/memory_object_types.h>
61*1031c584SApple OSS Distributions #include <device/device_port.h>
62*1031c584SApple OSS Distributions
63*1031c584SApple OSS Distributions #include <mach/vm_prot.h>
64*1031c584SApple OSS Distributions #include <mach/mach_vm.h>
65*1031c584SApple OSS Distributions #include <mach/vm_param.h>
66*1031c584SApple OSS Distributions #include <vm/vm_fault.h>
67*1031c584SApple OSS Distributions #include <vm/vm_protos.h>
68*1031c584SApple OSS Distributions __END_DECLS
69*1031c584SApple OSS Distributions
70*1031c584SApple OSS Distributions
71*1031c584SApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
72*1031c584SApple OSS Distributions
73*1031c584SApple OSS Distributions #if DEVELOPMENT || DEBUG
74*1031c584SApple OSS Distributions
75*1031c584SApple OSS Distributions extern SInt32 gIOMemoryReferenceCount;
76*1031c584SApple OSS Distributions
77*1031c584SApple OSS Distributions static int
IOMultMemoryDescriptorTest(int newValue)78*1031c584SApple OSS Distributions IOMultMemoryDescriptorTest(int newValue)
79*1031c584SApple OSS Distributions {
80*1031c584SApple OSS Distributions IOMemoryDescriptor * mds[3];
81*1031c584SApple OSS Distributions IOMultiMemoryDescriptor * mmd;
82*1031c584SApple OSS Distributions IOMemoryMap * map;
83*1031c584SApple OSS Distributions void * addr;
84*1031c584SApple OSS Distributions uint8_t * data;
85*1031c584SApple OSS Distributions uint32_t i;
86*1031c584SApple OSS Distributions IOAddressRange ranges[2];
87*1031c584SApple OSS Distributions
88*1031c584SApple OSS Distributions data = (typeof(data))IOMallocAligned(ptoa(8), page_size);
89*1031c584SApple OSS Distributions for (i = 0; i < ptoa(8); i++) {
90*1031c584SApple OSS Distributions data[i] = ((uint8_t) atop(i)) | 0xD0;
91*1031c584SApple OSS Distributions }
92*1031c584SApple OSS Distributions
93*1031c584SApple OSS Distributions ranges[0].address = (IOVirtualAddress)(data + ptoa(4));
94*1031c584SApple OSS Distributions ranges[0].length = ptoa(4);
95*1031c584SApple OSS Distributions ranges[1].address = (IOVirtualAddress)(data + ptoa(0));
96*1031c584SApple OSS Distributions ranges[1].length = ptoa(4);
97*1031c584SApple OSS Distributions
98*1031c584SApple OSS Distributions mds[0] = IOMemoryDescriptor::withAddressRange((mach_vm_address_t) data, 2, kIODirectionOutIn, kernel_task);
99*1031c584SApple OSS Distributions assert(mds[0]);
100*1031c584SApple OSS Distributions {
101*1031c584SApple OSS Distributions uint64_t dmaLen, dmaOffset;
102*1031c584SApple OSS Distributions dmaLen = mds[0]->getDMAMapLength(&dmaOffset);
103*1031c584SApple OSS Distributions assert(0 == dmaOffset);
104*1031c584SApple OSS Distributions assert(ptoa(1) == dmaLen);
105*1031c584SApple OSS Distributions }
106*1031c584SApple OSS Distributions mds[0]->release();
107*1031c584SApple OSS Distributions mds[0] = IOMemoryDescriptor::withAddressRange((mach_vm_address_t) (data + page_size - 2), 4, kIODirectionOutIn, kernel_task);
108*1031c584SApple OSS Distributions assert(mds[0]);
109*1031c584SApple OSS Distributions {
110*1031c584SApple OSS Distributions uint64_t dmaLen, dmaOffset;
111*1031c584SApple OSS Distributions dmaLen = mds[0]->getDMAMapLength(&dmaOffset);
112*1031c584SApple OSS Distributions assert((page_size - 2) == dmaOffset);
113*1031c584SApple OSS Distributions assert(ptoa(2) == dmaLen);
114*1031c584SApple OSS Distributions }
115*1031c584SApple OSS Distributions mds[0]->release();
116*1031c584SApple OSS Distributions
117*1031c584SApple OSS Distributions mds[0] = IOMemoryDescriptor::withAddressRanges(&ranges[0], 2, kIODirectionOutIn, kernel_task);
118*1031c584SApple OSS Distributions {
119*1031c584SApple OSS Distributions uint64_t dmaLen, dmaOffset;
120*1031c584SApple OSS Distributions dmaLen = mds[0]->getDMAMapLength(&dmaOffset);
121*1031c584SApple OSS Distributions assert(0 == dmaOffset);
122*1031c584SApple OSS Distributions assert(ptoa(8) == dmaLen);
123*1031c584SApple OSS Distributions }
124*1031c584SApple OSS Distributions mds[1] = IOSubMemoryDescriptor::withSubRange(mds[0], ptoa(3), ptoa(2), kIODirectionOutIn);
125*1031c584SApple OSS Distributions {
126*1031c584SApple OSS Distributions uint64_t dmaLen, dmaOffset;
127*1031c584SApple OSS Distributions dmaLen = mds[1]->getDMAMapLength(&dmaOffset);
128*1031c584SApple OSS Distributions assert(0 == dmaOffset);
129*1031c584SApple OSS Distributions assert(ptoa(2) == dmaLen);
130*1031c584SApple OSS Distributions }
131*1031c584SApple OSS Distributions mds[2] = IOSubMemoryDescriptor::withSubRange(mds[0], ptoa(7), ptoa(1), kIODirectionOutIn);
132*1031c584SApple OSS Distributions
133*1031c584SApple OSS Distributions mmd = IOMultiMemoryDescriptor::withDescriptors(&mds[0], sizeof(mds) / sizeof(mds[0]), kIODirectionOutIn, false);
134*1031c584SApple OSS Distributions {
135*1031c584SApple OSS Distributions uint64_t dmaLen, dmaOffset;
136*1031c584SApple OSS Distributions dmaLen = mmd->getDMAMapLength(&dmaOffset);
137*1031c584SApple OSS Distributions assert(0 == dmaOffset);
138*1031c584SApple OSS Distributions assert(ptoa(11) == dmaLen);
139*1031c584SApple OSS Distributions }
140*1031c584SApple OSS Distributions mds[2]->release();
141*1031c584SApple OSS Distributions mds[1]->release();
142*1031c584SApple OSS Distributions mds[0]->release();
143*1031c584SApple OSS Distributions map = mmd->createMappingInTask(kernel_task, 0, kIOMapAnywhere | kIOMapGuardedSmall, ptoa(7), mmd->getLength() - ptoa(7));
144*1031c584SApple OSS Distributions mmd->release();
145*1031c584SApple OSS Distributions assert(map);
146*1031c584SApple OSS Distributions
147*1031c584SApple OSS Distributions addr = (void *) map->getVirtualAddress();
148*1031c584SApple OSS Distributions assert(ptoa(4) == map->getLength());
149*1031c584SApple OSS Distributions assert(0xd3d3d3d3 == ((uint32_t *)addr)[ptoa(0) / sizeof(uint32_t)]);
150*1031c584SApple OSS Distributions assert(0xd7d7d7d7 == ((uint32_t *)addr)[ptoa(1) / sizeof(uint32_t)]);
151*1031c584SApple OSS Distributions assert(0xd0d0d0d0 == ((uint32_t *)addr)[ptoa(2) / sizeof(uint32_t)]);
152*1031c584SApple OSS Distributions assert(0xd3d3d3d3 == ((uint32_t *)addr)[ptoa(3) / sizeof(uint32_t)]);
153*1031c584SApple OSS Distributions map->release();
154*1031c584SApple OSS Distributions IOFreeAligned(data, ptoa(8));
155*1031c584SApple OSS Distributions
156*1031c584SApple OSS Distributions return 0;
157*1031c584SApple OSS Distributions }
158*1031c584SApple OSS Distributions
159*1031c584SApple OSS Distributions
160*1031c584SApple OSS Distributions
161*1031c584SApple OSS Distributions // <rdar://problem/30102458>
162*1031c584SApple OSS Distributions static int
IODMACommandForceDoubleBufferTest(int newValue)163*1031c584SApple OSS Distributions IODMACommandForceDoubleBufferTest(int newValue)
164*1031c584SApple OSS Distributions {
165*1031c584SApple OSS Distributions IOReturn ret;
166*1031c584SApple OSS Distributions IOBufferMemoryDescriptor * bmd;
167*1031c584SApple OSS Distributions IODMACommand * dma;
168*1031c584SApple OSS Distributions uint32_t dir, data;
169*1031c584SApple OSS Distributions IODMACommand::SegmentOptions segOptions =
170*1031c584SApple OSS Distributions {
171*1031c584SApple OSS Distributions .fStructSize = sizeof(segOptions),
172*1031c584SApple OSS Distributions .fNumAddressBits = 64,
173*1031c584SApple OSS Distributions .fMaxSegmentSize = 0x2000,
174*1031c584SApple OSS Distributions .fMaxTransferSize = 128 * 1024,
175*1031c584SApple OSS Distributions .fAlignment = 1,
176*1031c584SApple OSS Distributions .fAlignmentLength = 1,
177*1031c584SApple OSS Distributions .fAlignmentInternalSegments = 1
178*1031c584SApple OSS Distributions };
179*1031c584SApple OSS Distributions IODMACommand::Segment64 segments[1];
180*1031c584SApple OSS Distributions UInt32 numSegments;
181*1031c584SApple OSS Distributions UInt64 dmaOffset;
182*1031c584SApple OSS Distributions
183*1031c584SApple OSS Distributions
184*1031c584SApple OSS Distributions for (dir = kIODirectionIn;; dir++) {
185*1031c584SApple OSS Distributions bmd = IOBufferMemoryDescriptor::inTaskWithOptions(kernel_task,
186*1031c584SApple OSS Distributions dir | kIOMemoryPageable, ptoa(8));
187*1031c584SApple OSS Distributions assert(bmd);
188*1031c584SApple OSS Distributions {
189*1031c584SApple OSS Distributions uint64_t dmaLen, dmaOffset;
190*1031c584SApple OSS Distributions dmaLen = bmd->getDMAMapLength(&dmaOffset);
191*1031c584SApple OSS Distributions assert(0 == dmaOffset);
192*1031c584SApple OSS Distributions assert(ptoa(8) == dmaLen);
193*1031c584SApple OSS Distributions }
194*1031c584SApple OSS Distributions
195*1031c584SApple OSS Distributions ((uint32_t*) bmd->getBytesNoCopy())[0] = 0x53535300 | dir;
196*1031c584SApple OSS Distributions
197*1031c584SApple OSS Distributions ret = bmd->prepare((IODirection) dir);
198*1031c584SApple OSS Distributions assert(kIOReturnSuccess == ret);
199*1031c584SApple OSS Distributions
200*1031c584SApple OSS Distributions dma = IODMACommand::withSpecification(kIODMACommandOutputHost64, &segOptions,
201*1031c584SApple OSS Distributions kIODMAMapOptionMapped,
202*1031c584SApple OSS Distributions NULL, NULL);
203*1031c584SApple OSS Distributions assert(dma);
204*1031c584SApple OSS Distributions ret = dma->setMemoryDescriptor(bmd, true);
205*1031c584SApple OSS Distributions assert(kIOReturnSuccess == ret);
206*1031c584SApple OSS Distributions
207*1031c584SApple OSS Distributions ret = dma->synchronize(IODMACommand::kForceDoubleBuffer | kIODirectionOut);
208*1031c584SApple OSS Distributions assert(kIOReturnSuccess == ret);
209*1031c584SApple OSS Distributions
210*1031c584SApple OSS Distributions dmaOffset = 0;
211*1031c584SApple OSS Distributions numSegments = 1;
212*1031c584SApple OSS Distributions ret = dma->gen64IOVMSegments(&dmaOffset, &segments[0], &numSegments);
213*1031c584SApple OSS Distributions assert(kIOReturnSuccess == ret);
214*1031c584SApple OSS Distributions assert(1 == numSegments);
215*1031c584SApple OSS Distributions
216*1031c584SApple OSS Distributions if (kIODirectionOut & dir) {
217*1031c584SApple OSS Distributions data = ((uint32_t*) bmd->getBytesNoCopy())[0];
218*1031c584SApple OSS Distributions assertf((0x53535300 | dir) == data, "mismatch 0x%x", data);
219*1031c584SApple OSS Distributions }
220*1031c584SApple OSS Distributions if (kIODirectionIn & dir) {
221*1031c584SApple OSS Distributions IOMappedWrite32(segments[0].fIOVMAddr, 0x11223300 | dir);
222*1031c584SApple OSS Distributions }
223*1031c584SApple OSS Distributions
224*1031c584SApple OSS Distributions ret = dma->clearMemoryDescriptor(true);
225*1031c584SApple OSS Distributions assert(kIOReturnSuccess == ret);
226*1031c584SApple OSS Distributions dma->release();
227*1031c584SApple OSS Distributions
228*1031c584SApple OSS Distributions bmd->complete((IODirection) dir);
229*1031c584SApple OSS Distributions
230*1031c584SApple OSS Distributions if (kIODirectionIn & dir) {
231*1031c584SApple OSS Distributions data = ((uint32_t*) bmd->getBytesNoCopy())[0];
232*1031c584SApple OSS Distributions assertf((0x11223300 | dir) == data, "mismatch 0x%x", data);
233*1031c584SApple OSS Distributions }
234*1031c584SApple OSS Distributions
235*1031c584SApple OSS Distributions bmd->release();
236*1031c584SApple OSS Distributions
237*1031c584SApple OSS Distributions if (dir == kIODirectionInOut) {
238*1031c584SApple OSS Distributions break;
239*1031c584SApple OSS Distributions }
240*1031c584SApple OSS Distributions }
241*1031c584SApple OSS Distributions
242*1031c584SApple OSS Distributions return 0;
243*1031c584SApple OSS Distributions }
244*1031c584SApple OSS Distributions
245*1031c584SApple OSS Distributions // <rdar://problem/34322778>
246*1031c584SApple OSS Distributions static int __unused
IODMACommandLocalMappedNonContig(int newValue)247*1031c584SApple OSS Distributions IODMACommandLocalMappedNonContig(int newValue)
248*1031c584SApple OSS Distributions {
249*1031c584SApple OSS Distributions IOReturn kr;
250*1031c584SApple OSS Distributions IOMemoryDescriptor * md;
251*1031c584SApple OSS Distributions IODMACommand * dma;
252*1031c584SApple OSS Distributions OSDictionary * matching;
253*1031c584SApple OSS Distributions IOService * device;
254*1031c584SApple OSS Distributions IOMapper * mapper;
255*1031c584SApple OSS Distributions IODMACommand::SegmentOptions segOptions =
256*1031c584SApple OSS Distributions {
257*1031c584SApple OSS Distributions .fStructSize = sizeof(segOptions),
258*1031c584SApple OSS Distributions .fNumAddressBits = 64,
259*1031c584SApple OSS Distributions .fMaxSegmentSize = 128 * 1024,
260*1031c584SApple OSS Distributions .fMaxTransferSize = 128 * 1024,
261*1031c584SApple OSS Distributions .fAlignment = 1,
262*1031c584SApple OSS Distributions .fAlignmentLength = 1,
263*1031c584SApple OSS Distributions .fAlignmentInternalSegments = 1
264*1031c584SApple OSS Distributions };
265*1031c584SApple OSS Distributions IODMACommand::Segment64 segments[1];
266*1031c584SApple OSS Distributions UInt32 numSegments;
267*1031c584SApple OSS Distributions UInt64 dmaOffset;
268*1031c584SApple OSS Distributions UInt64 segPhys;
269*1031c584SApple OSS Distributions mach_vm_address_t buffer;
270*1031c584SApple OSS Distributions vm_size_t bufSize = ptoa(4);
271*1031c584SApple OSS Distributions
272*1031c584SApple OSS Distributions if (!IOMapper::gSystem) {
273*1031c584SApple OSS Distributions return 0;
274*1031c584SApple OSS Distributions }
275*1031c584SApple OSS Distributions
276*1031c584SApple OSS Distributions buffer = 0;
277*1031c584SApple OSS Distributions kr = mach_vm_allocate_kernel(kernel_map, &buffer, bufSize, VM_FLAGS_ANYWHERE, VM_KERN_MEMORY_IOKIT);
278*1031c584SApple OSS Distributions assert(KERN_SUCCESS == kr);
279*1031c584SApple OSS Distributions
280*1031c584SApple OSS Distributions // fragment the vmentries
281*1031c584SApple OSS Distributions kr = mach_vm_inherit(kernel_map, buffer + ptoa(1), ptoa(1), VM_INHERIT_NONE);
282*1031c584SApple OSS Distributions assert(KERN_SUCCESS == kr);
283*1031c584SApple OSS Distributions
284*1031c584SApple OSS Distributions md = IOMemoryDescriptor::withAddressRange(
285*1031c584SApple OSS Distributions buffer + 0xa00, 0x2000, kIODirectionOutIn, kernel_task);
286*1031c584SApple OSS Distributions assert(md);
287*1031c584SApple OSS Distributions kr = md->prepare(kIODirectionOutIn);
288*1031c584SApple OSS Distributions assert(kIOReturnSuccess == kr);
289*1031c584SApple OSS Distributions
290*1031c584SApple OSS Distributions segPhys = md->getPhysicalSegment(0, NULL, 0);
291*1031c584SApple OSS Distributions
292*1031c584SApple OSS Distributions matching = IOService::nameMatching("XHC1");
293*1031c584SApple OSS Distributions assert(matching);
294*1031c584SApple OSS Distributions device = IOService::copyMatchingService(matching);
295*1031c584SApple OSS Distributions matching->release();
296*1031c584SApple OSS Distributions mapper = device ? IOMapper::copyMapperForDeviceWithIndex(device, 0) : NULL;
297*1031c584SApple OSS Distributions OSSafeReleaseNULL(device);
298*1031c584SApple OSS Distributions
299*1031c584SApple OSS Distributions dma = IODMACommand::withSpecification(kIODMACommandOutputHost64, &segOptions,
300*1031c584SApple OSS Distributions kIODMAMapOptionMapped,
301*1031c584SApple OSS Distributions mapper, NULL);
302*1031c584SApple OSS Distributions assert(dma);
303*1031c584SApple OSS Distributions kr = dma->setMemoryDescriptor(md, true);
304*1031c584SApple OSS Distributions assert(kIOReturnSuccess == kr);
305*1031c584SApple OSS Distributions
306*1031c584SApple OSS Distributions dmaOffset = 0;
307*1031c584SApple OSS Distributions numSegments = 1;
308*1031c584SApple OSS Distributions kr = dma->gen64IOVMSegments(&dmaOffset, &segments[0], &numSegments);
309*1031c584SApple OSS Distributions assert(kIOReturnSuccess == kr);
310*1031c584SApple OSS Distributions assert(1 == numSegments);
311*1031c584SApple OSS Distributions
312*1031c584SApple OSS Distributions if (mapper) {
313*1031c584SApple OSS Distributions assertf(segments[0].fIOVMAddr != segPhys, "phys !local 0x%qx, 0x%qx, %p", segments[0].fIOVMAddr, segPhys, dma);
314*1031c584SApple OSS Distributions }
315*1031c584SApple OSS Distributions
316*1031c584SApple OSS Distributions kr = dma->clearMemoryDescriptor(true);
317*1031c584SApple OSS Distributions assert(kIOReturnSuccess == kr);
318*1031c584SApple OSS Distributions dma->release();
319*1031c584SApple OSS Distributions
320*1031c584SApple OSS Distributions kr = md->complete(kIODirectionOutIn);
321*1031c584SApple OSS Distributions assert(kIOReturnSuccess == kr);
322*1031c584SApple OSS Distributions md->release();
323*1031c584SApple OSS Distributions
324*1031c584SApple OSS Distributions kr = mach_vm_deallocate(kernel_map, buffer, bufSize);
325*1031c584SApple OSS Distributions assert(KERN_SUCCESS == kr);
326*1031c584SApple OSS Distributions OSSafeReleaseNULL(mapper);
327*1031c584SApple OSS Distributions
328*1031c584SApple OSS Distributions return 0;
329*1031c584SApple OSS Distributions }
330*1031c584SApple OSS Distributions
331*1031c584SApple OSS Distributions // <rdar://problem/30102458>
332*1031c584SApple OSS Distributions static int
IOMemoryRemoteTest(int newValue)333*1031c584SApple OSS Distributions IOMemoryRemoteTest(int newValue)
334*1031c584SApple OSS Distributions {
335*1031c584SApple OSS Distributions IOReturn ret;
336*1031c584SApple OSS Distributions IOMemoryDescriptor * md;
337*1031c584SApple OSS Distributions IOByteCount offset, length;
338*1031c584SApple OSS Distributions addr64_t addr;
339*1031c584SApple OSS Distributions uint32_t idx;
340*1031c584SApple OSS Distributions
341*1031c584SApple OSS Distributions IODMACommand * dma;
342*1031c584SApple OSS Distributions IODMACommand::SegmentOptions segOptions =
343*1031c584SApple OSS Distributions {
344*1031c584SApple OSS Distributions .fStructSize = sizeof(segOptions),
345*1031c584SApple OSS Distributions .fNumAddressBits = 64,
346*1031c584SApple OSS Distributions .fMaxSegmentSize = 0x2000,
347*1031c584SApple OSS Distributions .fMaxTransferSize = 128 * 1024,
348*1031c584SApple OSS Distributions .fAlignment = 1,
349*1031c584SApple OSS Distributions .fAlignmentLength = 1,
350*1031c584SApple OSS Distributions .fAlignmentInternalSegments = 1
351*1031c584SApple OSS Distributions };
352*1031c584SApple OSS Distributions IODMACommand::Segment64 segments[1];
353*1031c584SApple OSS Distributions UInt32 numSegments;
354*1031c584SApple OSS Distributions UInt64 dmaOffset;
355*1031c584SApple OSS Distributions
356*1031c584SApple OSS Distributions IOAddressRange ranges[2] = {
357*1031c584SApple OSS Distributions { 0x1234567890123456ULL, 0x1000 }, { 0x5432109876543210, 0x2000 },
358*1031c584SApple OSS Distributions };
359*1031c584SApple OSS Distributions
360*1031c584SApple OSS Distributions md = IOMemoryDescriptor::withAddressRanges(&ranges[0], 2, kIODirectionOutIn | kIOMemoryRemote, TASK_NULL);
361*1031c584SApple OSS Distributions assert(md);
362*1031c584SApple OSS Distributions
363*1031c584SApple OSS Distributions // md->map();
364*1031c584SApple OSS Distributions // md->readBytes(0, &idx, sizeof(idx));
365*1031c584SApple OSS Distributions
366*1031c584SApple OSS Distributions ret = md->prepare(kIODirectionOutIn);
367*1031c584SApple OSS Distributions assert(kIOReturnSuccess == ret);
368*1031c584SApple OSS Distributions
369*1031c584SApple OSS Distributions printf("remote md flags 0x%qx, r %d\n",
370*1031c584SApple OSS Distributions md->getFlags(), (0 != (kIOMemoryRemote & md->getFlags())));
371*1031c584SApple OSS Distributions
372*1031c584SApple OSS Distributions for (offset = 0, idx = 0; true; offset += length, idx++) {
373*1031c584SApple OSS Distributions addr = md->getPhysicalSegment(offset, &length, 0);
374*1031c584SApple OSS Distributions if (!length) {
375*1031c584SApple OSS Distributions break;
376*1031c584SApple OSS Distributions }
377*1031c584SApple OSS Distributions assert(idx < 2);
378*1031c584SApple OSS Distributions assert(addr == ranges[idx].address);
379*1031c584SApple OSS Distributions assert(length == ranges[idx].length);
380*1031c584SApple OSS Distributions }
381*1031c584SApple OSS Distributions assert(offset == md->getLength());
382*1031c584SApple OSS Distributions
383*1031c584SApple OSS Distributions dma = IODMACommand::withSpecification(kIODMACommandOutputHost64, &segOptions,
384*1031c584SApple OSS Distributions kIODMAMapOptionUnmapped | kIODMAMapOptionIterateOnly,
385*1031c584SApple OSS Distributions NULL, NULL);
386*1031c584SApple OSS Distributions assert(dma);
387*1031c584SApple OSS Distributions ret = dma->setMemoryDescriptor(md, true);
388*1031c584SApple OSS Distributions assert(kIOReturnSuccess == ret);
389*1031c584SApple OSS Distributions
390*1031c584SApple OSS Distributions for (dmaOffset = 0, idx = 0; dmaOffset < md->getLength(); idx++) {
391*1031c584SApple OSS Distributions numSegments = 1;
392*1031c584SApple OSS Distributions ret = dma->gen64IOVMSegments(&dmaOffset, &segments[0], &numSegments);
393*1031c584SApple OSS Distributions assert(kIOReturnSuccess == ret);
394*1031c584SApple OSS Distributions assert(1 == numSegments);
395*1031c584SApple OSS Distributions assert(idx < 2);
396*1031c584SApple OSS Distributions assert(segments[0].fIOVMAddr == ranges[idx].address);
397*1031c584SApple OSS Distributions assert(segments[0].fLength == ranges[idx].length);
398*1031c584SApple OSS Distributions }
399*1031c584SApple OSS Distributions assert(dmaOffset == md->getLength());
400*1031c584SApple OSS Distributions
401*1031c584SApple OSS Distributions ret = dma->clearMemoryDescriptor(true);
402*1031c584SApple OSS Distributions assert(kIOReturnSuccess == ret);
403*1031c584SApple OSS Distributions dma->release();
404*1031c584SApple OSS Distributions md->complete(kIODirectionOutIn);
405*1031c584SApple OSS Distributions md->release();
406*1031c584SApple OSS Distributions
407*1031c584SApple OSS Distributions return 0;
408*1031c584SApple OSS Distributions }
409*1031c584SApple OSS Distributions
410*1031c584SApple OSS Distributions static IOReturn
IOMemoryPrefaultTest(uint32_t options)411*1031c584SApple OSS Distributions IOMemoryPrefaultTest(uint32_t options)
412*1031c584SApple OSS Distributions {
413*1031c584SApple OSS Distributions IOBufferMemoryDescriptor * bmd;
414*1031c584SApple OSS Distributions IOMemoryMap * map;
415*1031c584SApple OSS Distributions IOReturn kr;
416*1031c584SApple OSS Distributions uint32_t data;
417*1031c584SApple OSS Distributions uint32_t * p;
418*1031c584SApple OSS Distributions IOSimpleLock * lock;
419*1031c584SApple OSS Distributions
420*1031c584SApple OSS Distributions lock = IOSimpleLockAlloc();
421*1031c584SApple OSS Distributions assert(lock);
422*1031c584SApple OSS Distributions
423*1031c584SApple OSS Distributions bmd = IOBufferMemoryDescriptor::inTaskWithOptions(current_task(),
424*1031c584SApple OSS Distributions kIODirectionOutIn | kIOMemoryPageable, ptoa(8));
425*1031c584SApple OSS Distributions assert(bmd);
426*1031c584SApple OSS Distributions kr = bmd->prepare();
427*1031c584SApple OSS Distributions assert(KERN_SUCCESS == kr);
428*1031c584SApple OSS Distributions
429*1031c584SApple OSS Distributions map = bmd->map(kIOMapPrefault);
430*1031c584SApple OSS Distributions assert(map);
431*1031c584SApple OSS Distributions
432*1031c584SApple OSS Distributions p = (typeof(p))map->getVirtualAddress();
433*1031c584SApple OSS Distributions IOSimpleLockLock(lock);
434*1031c584SApple OSS Distributions data = p[0];
435*1031c584SApple OSS Distributions IOSimpleLockUnlock(lock);
436*1031c584SApple OSS Distributions
437*1031c584SApple OSS Distributions IOLog("IOMemoryPrefaultTest %d\n", data);
438*1031c584SApple OSS Distributions
439*1031c584SApple OSS Distributions map->release();
440*1031c584SApple OSS Distributions bmd->release();
441*1031c584SApple OSS Distributions IOSimpleLockFree(lock);
442*1031c584SApple OSS Distributions
443*1031c584SApple OSS Distributions return kIOReturnSuccess;
444*1031c584SApple OSS Distributions }
445*1031c584SApple OSS Distributions
446*1031c584SApple OSS Distributions static IOReturn
IOBMDOverflowTest(uint32_t options)447*1031c584SApple OSS Distributions IOBMDOverflowTest(uint32_t options)
448*1031c584SApple OSS Distributions {
449*1031c584SApple OSS Distributions IOBufferMemoryDescriptor * bmd;
450*1031c584SApple OSS Distributions
451*1031c584SApple OSS Distributions bmd = IOBufferMemoryDescriptor::inTaskWithPhysicalMask(kernel_task, kIOMemoryPageable | kIODirectionOut,
452*1031c584SApple OSS Distributions 0xffffffffffffffff, 0);
453*1031c584SApple OSS Distributions assert(NULL == bmd);
454*1031c584SApple OSS Distributions
455*1031c584SApple OSS Distributions return kIOReturnSuccess;
456*1031c584SApple OSS Distributions }
457*1031c584SApple OSS Distributions
458*1031c584SApple OSS Distributions static IOReturn
IOBMDSetLengthMapTest(uint32_t options)459*1031c584SApple OSS Distributions IOBMDSetLengthMapTest(uint32_t options)
460*1031c584SApple OSS Distributions {
461*1031c584SApple OSS Distributions IOBufferMemoryDescriptor * bmd;
462*1031c584SApple OSS Distributions IOMemoryMap * map;
463*1031c584SApple OSS Distributions
464*1031c584SApple OSS Distributions bmd = IOBufferMemoryDescriptor::inTaskWithOptions(
465*1031c584SApple OSS Distributions kernel_task, kIOMemoryDirectionOutIn | kIOMemoryKernelUserShared, 0x4000, 0x4000);
466*1031c584SApple OSS Distributions assert(bmd);
467*1031c584SApple OSS Distributions
468*1031c584SApple OSS Distributions bmd->setLength(0x100);
469*1031c584SApple OSS Distributions map = bmd->createMappingInTask(current_task(), 0, kIOMapAnywhere, 0, 0);
470*1031c584SApple OSS Distributions assert(map);
471*1031c584SApple OSS Distributions OSSafeReleaseNULL(map);
472*1031c584SApple OSS Distributions
473*1031c584SApple OSS Distributions bmd->setLength(0x200);
474*1031c584SApple OSS Distributions map = bmd->createMappingInTask(current_task(), 0, kIOMapAnywhere, 0, 0);
475*1031c584SApple OSS Distributions assert(map);
476*1031c584SApple OSS Distributions OSSafeReleaseNULL(map);
477*1031c584SApple OSS Distributions
478*1031c584SApple OSS Distributions bmd->release();
479*1031c584SApple OSS Distributions
480*1031c584SApple OSS Distributions return kIOReturnSuccess;
481*1031c584SApple OSS Distributions }
482*1031c584SApple OSS Distributions
483*1031c584SApple OSS Distributions // <rdar://problem/26375234>
484*1031c584SApple OSS Distributions static IOReturn
ZeroLengthTest(int newValue)485*1031c584SApple OSS Distributions ZeroLengthTest(int newValue)
486*1031c584SApple OSS Distributions {
487*1031c584SApple OSS Distributions IOMemoryDescriptor * md;
488*1031c584SApple OSS Distributions
489*1031c584SApple OSS Distributions md = IOMemoryDescriptor::withAddressRange(
490*1031c584SApple OSS Distributions 0, 0, kIODirectionNone, current_task());
491*1031c584SApple OSS Distributions assert(md);
492*1031c584SApple OSS Distributions md->prepare();
493*1031c584SApple OSS Distributions md->complete();
494*1031c584SApple OSS Distributions md->release();
495*1031c584SApple OSS Distributions return 0;
496*1031c584SApple OSS Distributions }
497*1031c584SApple OSS Distributions
498*1031c584SApple OSS Distributions // <rdar://problem/27002624>
499*1031c584SApple OSS Distributions static IOReturn
BadFixedAllocTest(int newValue)500*1031c584SApple OSS Distributions BadFixedAllocTest(int newValue)
501*1031c584SApple OSS Distributions {
502*1031c584SApple OSS Distributions IOBufferMemoryDescriptor * bmd;
503*1031c584SApple OSS Distributions IOMemoryMap * map;
504*1031c584SApple OSS Distributions
505*1031c584SApple OSS Distributions bmd = IOBufferMemoryDescriptor::inTaskWithOptions(NULL,
506*1031c584SApple OSS Distributions kIODirectionIn | kIOMemoryPageable, ptoa(1));
507*1031c584SApple OSS Distributions assert(bmd);
508*1031c584SApple OSS Distributions map = bmd->createMappingInTask(kernel_task, 0x2000, 0);
509*1031c584SApple OSS Distributions assert(!map);
510*1031c584SApple OSS Distributions
511*1031c584SApple OSS Distributions bmd->release();
512*1031c584SApple OSS Distributions return 0;
513*1031c584SApple OSS Distributions }
514*1031c584SApple OSS Distributions
515*1031c584SApple OSS Distributions // <rdar://problem/26466423>
516*1031c584SApple OSS Distributions static IOReturn
IODirectionPrepareNoZeroFillTest(int newValue)517*1031c584SApple OSS Distributions IODirectionPrepareNoZeroFillTest(int newValue)
518*1031c584SApple OSS Distributions {
519*1031c584SApple OSS Distributions IOBufferMemoryDescriptor * bmd;
520*1031c584SApple OSS Distributions
521*1031c584SApple OSS Distributions bmd = IOBufferMemoryDescriptor::inTaskWithOptions(NULL,
522*1031c584SApple OSS Distributions kIODirectionIn | kIOMemoryPageable, ptoa(24));
523*1031c584SApple OSS Distributions assert(bmd);
524*1031c584SApple OSS Distributions bmd->prepare((IODirection)(kIODirectionIn | kIODirectionPrepareNoZeroFill));
525*1031c584SApple OSS Distributions bmd->prepare(kIODirectionIn);
526*1031c584SApple OSS Distributions bmd->complete((IODirection)(kIODirectionIn | kIODirectionCompleteWithDataValid));
527*1031c584SApple OSS Distributions bmd->complete(kIODirectionIn);
528*1031c584SApple OSS Distributions bmd->release();
529*1031c584SApple OSS Distributions return 0;
530*1031c584SApple OSS Distributions }
531*1031c584SApple OSS Distributions
532*1031c584SApple OSS Distributions // <rdar://problem/28190483>
533*1031c584SApple OSS Distributions static IOReturn
IOMemoryMapTest(uint32_t options)534*1031c584SApple OSS Distributions IOMemoryMapTest(uint32_t options)
535*1031c584SApple OSS Distributions {
536*1031c584SApple OSS Distributions IOBufferMemoryDescriptor * bmd;
537*1031c584SApple OSS Distributions IOMemoryDescriptor * md;
538*1031c584SApple OSS Distributions IOMemoryMap * map;
539*1031c584SApple OSS Distributions uint32_t data;
540*1031c584SApple OSS Distributions user_addr_t p;
541*1031c584SApple OSS Distributions uint8_t * p2;
542*1031c584SApple OSS Distributions int r;
543*1031c584SApple OSS Distributions uint64_t time, nano;
544*1031c584SApple OSS Distributions
545*1031c584SApple OSS Distributions bmd = IOBufferMemoryDescriptor::inTaskWithOptions(current_task(),
546*1031c584SApple OSS Distributions kIODirectionOutIn | kIOMemoryPageable, 0x4018 + 0x800);
547*1031c584SApple OSS Distributions assert(bmd);
548*1031c584SApple OSS Distributions p = (typeof(p))bmd->getBytesNoCopy();
549*1031c584SApple OSS Distributions p += 0x800;
550*1031c584SApple OSS Distributions data = 0x11111111;
551*1031c584SApple OSS Distributions r = copyout(&data, p, sizeof(data));
552*1031c584SApple OSS Distributions assert(r == 0);
553*1031c584SApple OSS Distributions data = 0x22222222;
554*1031c584SApple OSS Distributions r = copyout(&data, p + 0x1000, sizeof(data));
555*1031c584SApple OSS Distributions assert(r == 0);
556*1031c584SApple OSS Distributions data = 0x33333333;
557*1031c584SApple OSS Distributions r = copyout(&data, p + 0x2000, sizeof(data));
558*1031c584SApple OSS Distributions assert(r == 0);
559*1031c584SApple OSS Distributions data = 0x44444444;
560*1031c584SApple OSS Distributions r = copyout(&data, p + 0x3000, sizeof(data));
561*1031c584SApple OSS Distributions assert(r == 0);
562*1031c584SApple OSS Distributions
563*1031c584SApple OSS Distributions md = IOMemoryDescriptor::withAddressRange(p, 0x4018,
564*1031c584SApple OSS Distributions kIODirectionOut | options,
565*1031c584SApple OSS Distributions current_task());
566*1031c584SApple OSS Distributions assert(md);
567*1031c584SApple OSS Distributions time = mach_absolute_time();
568*1031c584SApple OSS Distributions map = md->map(kIOMapReadOnly);
569*1031c584SApple OSS Distributions time = mach_absolute_time() - time;
570*1031c584SApple OSS Distributions assert(map);
571*1031c584SApple OSS Distributions absolutetime_to_nanoseconds(time, &nano);
572*1031c584SApple OSS Distributions
573*1031c584SApple OSS Distributions p2 = (typeof(p2))map->getVirtualAddress();
574*1031c584SApple OSS Distributions assert(0x11 == p2[0]);
575*1031c584SApple OSS Distributions assert(0x22 == p2[0x1000]);
576*1031c584SApple OSS Distributions assert(0x33 == p2[0x2000]);
577*1031c584SApple OSS Distributions assert(0x44 == p2[0x3000]);
578*1031c584SApple OSS Distributions
579*1031c584SApple OSS Distributions data = 0x99999999;
580*1031c584SApple OSS Distributions r = copyout(&data, p + 0x2000, sizeof(data));
581*1031c584SApple OSS Distributions assert(r == 0);
582*1031c584SApple OSS Distributions
583*1031c584SApple OSS Distributions assert(0x11 == p2[0]);
584*1031c584SApple OSS Distributions assert(0x22 == p2[0x1000]);
585*1031c584SApple OSS Distributions assert(0x44 == p2[0x3000]);
586*1031c584SApple OSS Distributions if (kIOMemoryMapCopyOnWrite & options) {
587*1031c584SApple OSS Distributions assert(0x33 == p2[0x2000]);
588*1031c584SApple OSS Distributions } else {
589*1031c584SApple OSS Distributions assert(0x99 == p2[0x2000]);
590*1031c584SApple OSS Distributions }
591*1031c584SApple OSS Distributions
592*1031c584SApple OSS Distributions IOLog("IOMemoryMapCopyOnWriteTest map(%s) %lld ns\n",
593*1031c584SApple OSS Distributions kIOMemoryMapCopyOnWrite & options ? "kIOMemoryMapCopyOnWrite" : "",
594*1031c584SApple OSS Distributions nano);
595*1031c584SApple OSS Distributions
596*1031c584SApple OSS Distributions map->release();
597*1031c584SApple OSS Distributions md->release();
598*1031c584SApple OSS Distributions bmd->release();
599*1031c584SApple OSS Distributions
600*1031c584SApple OSS Distributions return kIOReturnSuccess;
601*1031c584SApple OSS Distributions }
602*1031c584SApple OSS Distributions
603*1031c584SApple OSS Distributions static int
IOMemoryMapCopyOnWriteTest(int newValue)604*1031c584SApple OSS Distributions IOMemoryMapCopyOnWriteTest(int newValue)
605*1031c584SApple OSS Distributions {
606*1031c584SApple OSS Distributions IOMemoryMapTest(0);
607*1031c584SApple OSS Distributions IOMemoryMapTest(kIOMemoryMapCopyOnWrite);
608*1031c584SApple OSS Distributions return 0;
609*1031c584SApple OSS Distributions }
610*1031c584SApple OSS Distributions
611*1031c584SApple OSS Distributions static int
AllocationNameTest(int newValue)612*1031c584SApple OSS Distributions AllocationNameTest(int newValue)
613*1031c584SApple OSS Distributions {
614*1031c584SApple OSS Distributions IOMemoryDescriptor * bmd;
615*1031c584SApple OSS Distributions kern_allocation_name_t name, prior;
616*1031c584SApple OSS Distributions
617*1031c584SApple OSS Distributions name = kern_allocation_name_allocate("com.apple.iokit.test", 0);
618*1031c584SApple OSS Distributions assert(name);
619*1031c584SApple OSS Distributions
620*1031c584SApple OSS Distributions prior = thread_set_allocation_name(name);
621*1031c584SApple OSS Distributions
622*1031c584SApple OSS Distributions bmd = IOBufferMemoryDescriptor::inTaskWithOptions(TASK_NULL,
623*1031c584SApple OSS Distributions kIODirectionOutIn | kIOMemoryPageable | kIOMemoryKernelUserShared,
624*1031c584SApple OSS Distributions ptoa(13));
625*1031c584SApple OSS Distributions assert(bmd);
626*1031c584SApple OSS Distributions bmd->prepare();
627*1031c584SApple OSS Distributions
628*1031c584SApple OSS Distributions thread_set_allocation_name(prior);
629*1031c584SApple OSS Distributions kern_allocation_name_release(name);
630*1031c584SApple OSS Distributions
631*1031c584SApple OSS Distributions if (newValue != 7) {
632*1031c584SApple OSS Distributions bmd->release();
633*1031c584SApple OSS Distributions }
634*1031c584SApple OSS Distributions
635*1031c584SApple OSS Distributions return 0;
636*1031c584SApple OSS Distributions }
637*1031c584SApple OSS Distributions
638*1031c584SApple OSS Distributions static IOReturn
IOGuardPageMDTest(int newValue)639*1031c584SApple OSS Distributions IOGuardPageMDTest(int newValue)
640*1031c584SApple OSS Distributions {
641*1031c584SApple OSS Distributions constexpr size_t MAX_LEFT_GUARD_PAGES = 5;
642*1031c584SApple OSS Distributions constexpr size_t MAX_RIGHT_GUARD_PAGES = 5;
643*1031c584SApple OSS Distributions
644*1031c584SApple OSS Distributions IOMemoryDescriptor * mds[3];
645*1031c584SApple OSS Distributions IOMemoryDescriptor * dataMD;
646*1031c584SApple OSS Distributions IOMultiMemoryDescriptor * mmd;
647*1031c584SApple OSS Distributions IOBufferMemoryDescriptor * iobmd;
648*1031c584SApple OSS Distributions IOMemoryMap * map;
649*1031c584SApple OSS Distributions void * addr;
650*1031c584SApple OSS Distributions uint8_t * data;
651*1031c584SApple OSS Distributions uint32_t i;
652*1031c584SApple OSS Distributions
653*1031c584SApple OSS Distributions data = (typeof(data))IOMallocAligned(page_size, page_size);
654*1031c584SApple OSS Distributions for (i = 0; i < page_size; i++) {
655*1031c584SApple OSS Distributions data[i] = (uint8_t)(i & 0xFF);
656*1031c584SApple OSS Distributions }
657*1031c584SApple OSS Distributions
658*1031c584SApple OSS Distributions dataMD = IOMemoryDescriptor::withAddressRange((mach_vm_address_t) data, page_size, kIODirectionOutIn, kernel_task);
659*1031c584SApple OSS Distributions assert(dataMD);
660*1031c584SApple OSS Distributions
661*1031c584SApple OSS Distributions
662*1031c584SApple OSS Distributions for (size_t leftGuardSize = 1; leftGuardSize < MAX_LEFT_GUARD_PAGES; leftGuardSize++) {
663*1031c584SApple OSS Distributions for (size_t rightGuardSize = 1; rightGuardSize < MAX_RIGHT_GUARD_PAGES; rightGuardSize++) {
664*1031c584SApple OSS Distributions mds[0] = IOGuardPageMemoryDescriptor::withSize(page_size * leftGuardSize);
665*1031c584SApple OSS Distributions assert(mds[0]);
666*1031c584SApple OSS Distributions
667*1031c584SApple OSS Distributions mds[1] = dataMD;
668*1031c584SApple OSS Distributions mds[1]->retain();
669*1031c584SApple OSS Distributions
670*1031c584SApple OSS Distributions mds[2] = IOGuardPageMemoryDescriptor::withSize(page_size * rightGuardSize);
671*1031c584SApple OSS Distributions assert(mds[2]);
672*1031c584SApple OSS Distributions
673*1031c584SApple OSS Distributions mmd = IOMultiMemoryDescriptor::withDescriptors(&mds[0], sizeof(mds) / sizeof(mds[0]), kIODirectionOutIn, false);
674*1031c584SApple OSS Distributions
675*1031c584SApple OSS Distributions OSSafeReleaseNULL(mds[2]);
676*1031c584SApple OSS Distributions OSSafeReleaseNULL(mds[1]);
677*1031c584SApple OSS Distributions OSSafeReleaseNULL(mds[0]);
678*1031c584SApple OSS Distributions
679*1031c584SApple OSS Distributions map = mmd->createMappingInTask(kernel_task, 0, kIOMapAnywhere, 0, mmd->getLength());
680*1031c584SApple OSS Distributions
681*1031c584SApple OSS Distributions OSSafeReleaseNULL(mmd);
682*1031c584SApple OSS Distributions assert(map);
683*1031c584SApple OSS Distributions addr = (void *)map->getAddress();
684*1031c584SApple OSS Distributions
685*1031c584SApple OSS Distributions // check data
686*1031c584SApple OSS Distributions for (i = 0; i < page_size; i++) {
687*1031c584SApple OSS Distributions assert(*(uint8_t *)((uintptr_t)addr + page_size * leftGuardSize + i) == (uint8_t)(i & 0xFF));
688*1031c584SApple OSS Distributions }
689*1031c584SApple OSS Distributions
690*1031c584SApple OSS Distributions // check map length
691*1031c584SApple OSS Distributions assert(page_size * leftGuardSize + page_size + page_size * rightGuardSize == map->getLength());
692*1031c584SApple OSS Distributions
693*1031c584SApple OSS Distributions // check page protections
694*1031c584SApple OSS Distributions for (i = 0; i < leftGuardSize + 1 + rightGuardSize; i++) {
695*1031c584SApple OSS Distributions mach_vm_address_t regionAddr = (vm_address_t)addr + i * page_size;
696*1031c584SApple OSS Distributions mach_vm_size_t regionSize;
697*1031c584SApple OSS Distributions vm_region_extended_info regionInfo;
698*1031c584SApple OSS Distributions mach_msg_type_number_t count = VM_REGION_EXTENDED_INFO_COUNT;
699*1031c584SApple OSS Distributions mach_port_t unused;
700*1031c584SApple OSS Distributions kern_return_t kr = mach_vm_region(kernel_map, ®ionAddr, ®ionSize, VM_REGION_EXTENDED_INFO, (vm_region_info_t)®ionInfo, &count, &unused);
701*1031c584SApple OSS Distributions assert(kr == KERN_SUCCESS);
702*1031c584SApple OSS Distributions if (i < leftGuardSize || i > leftGuardSize + 1) {
703*1031c584SApple OSS Distributions assert(regionInfo.protection == VM_PROT_NONE);
704*1031c584SApple OSS Distributions }
705*1031c584SApple OSS Distributions }
706*1031c584SApple OSS Distributions OSSafeReleaseNULL(map);
707*1031c584SApple OSS Distributions }
708*1031c584SApple OSS Distributions }
709*1031c584SApple OSS Distributions
710*1031c584SApple OSS Distributions OSSafeReleaseNULL(dataMD);
711*1031c584SApple OSS Distributions IOFreeAligned(data, page_size);
712*1031c584SApple OSS Distributions
713*1031c584SApple OSS Distributions for (size_t iobmdCapacity = page_size / 8; iobmdCapacity < page_size * 10; iobmdCapacity += page_size / 8) {
714*1031c584SApple OSS Distributions iobmd = IOBufferMemoryDescriptor::inTaskWithGuardPages(kernel_task, kIODirectionOutIn, iobmdCapacity);
715*1031c584SApple OSS Distributions
716*1031c584SApple OSS Distributions // Capacity should be rounded up to page size
717*1031c584SApple OSS Distributions assert(iobmd->getLength() == round_page(iobmdCapacity));
718*1031c584SApple OSS Distributions
719*1031c584SApple OSS Distributions // Buffer should be page aligned
720*1031c584SApple OSS Distributions addr = iobmd->getBytesNoCopy();
721*1031c584SApple OSS Distributions assert((vm_offset_t)addr == round_page((vm_offset_t)addr));
722*1031c584SApple OSS Distributions
723*1031c584SApple OSS Distributions // fill buffer
724*1031c584SApple OSS Distributions for (size_t i = 0; i < iobmdCapacity; i++) {
725*1031c584SApple OSS Distributions *((char *)addr + i) = (char)(i & 0xFF);
726*1031c584SApple OSS Distributions }
727*1031c584SApple OSS Distributions
728*1031c584SApple OSS Distributions map = iobmd->createMappingInTask(kernel_task, 0, kIOMapAnywhere | kIOMapUnique, 0, iobmd->getLength());
729*1031c584SApple OSS Distributions assert(map->getLength() == iobmd->getLength());
730*1031c584SApple OSS Distributions
731*1031c584SApple OSS Distributions // check buffer
732*1031c584SApple OSS Distributions for (size_t i = 0; i < iobmdCapacity; i++) {
733*1031c584SApple OSS Distributions assert(*((char *)map->getAddress() + i) == (char)(i & 0xFF));
734*1031c584SApple OSS Distributions }
735*1031c584SApple OSS Distributions
736*1031c584SApple OSS Distributions OSSafeReleaseNULL(map);
737*1031c584SApple OSS Distributions OSSafeReleaseNULL(iobmd);
738*1031c584SApple OSS Distributions }
739*1031c584SApple OSS Distributions
740*1031c584SApple OSS Distributions return kIOReturnSuccess;
741*1031c584SApple OSS Distributions }
742*1031c584SApple OSS Distributions
743*1031c584SApple OSS Distributions static IOReturn
IOMDContextTest(int newValue)744*1031c584SApple OSS Distributions IOMDContextTest(int newValue)
745*1031c584SApple OSS Distributions {
746*1031c584SApple OSS Distributions IOBufferMemoryDescriptor * bmd = IOBufferMemoryDescriptor::inTaskWithOptions(TASK_NULL,
747*1031c584SApple OSS Distributions kIODirectionOutIn | kIOMemoryPageable | kIOMemoryKernelUserShared,
748*1031c584SApple OSS Distributions ptoa(13));
749*1031c584SApple OSS Distributions
750*1031c584SApple OSS Distributions OSObject * current = NULL;
751*1031c584SApple OSS Distributions OSString * firstString = OSString::withCStringNoCopy("firstString");
752*1031c584SApple OSS Distributions OSString * secondString = OSString::withCStringNoCopy("secondString");
753*1031c584SApple OSS Distributions
754*1031c584SApple OSS Distributions assert(bmd->copyContext() == NULL);
755*1031c584SApple OSS Distributions
756*1031c584SApple OSS Distributions bmd->setContext(NULL);
757*1031c584SApple OSS Distributions assert(bmd->copyContext() == NULL);
758*1031c584SApple OSS Distributions
759*1031c584SApple OSS Distributions bmd->setContext(firstString);
760*1031c584SApple OSS Distributions current = bmd->copyContext();
761*1031c584SApple OSS Distributions assert(current == firstString);
762*1031c584SApple OSS Distributions OSSafeReleaseNULL(current);
763*1031c584SApple OSS Distributions
764*1031c584SApple OSS Distributions bmd->setContext(NULL);
765*1031c584SApple OSS Distributions assert(bmd->copyContext() == NULL);
766*1031c584SApple OSS Distributions
767*1031c584SApple OSS Distributions bmd->setContext(secondString);
768*1031c584SApple OSS Distributions current = bmd->copyContext();
769*1031c584SApple OSS Distributions assert(current == secondString);
770*1031c584SApple OSS Distributions OSSafeReleaseNULL(current);
771*1031c584SApple OSS Distributions
772*1031c584SApple OSS Distributions bmd->release();
773*1031c584SApple OSS Distributions
774*1031c584SApple OSS Distributions assert(firstString->getRetainCount() == 1);
775*1031c584SApple OSS Distributions assert(secondString->getRetainCount() == 1);
776*1031c584SApple OSS Distributions
777*1031c584SApple OSS Distributions firstString->release();
778*1031c584SApple OSS Distributions secondString->release();
779*1031c584SApple OSS Distributions
780*1031c584SApple OSS Distributions return kIOReturnSuccess;
781*1031c584SApple OSS Distributions }
782*1031c584SApple OSS Distributions
783*1031c584SApple OSS Distributions int
IOMemoryDescriptorTest(int newValue)784*1031c584SApple OSS Distributions IOMemoryDescriptorTest(int newValue)
785*1031c584SApple OSS Distributions {
786*1031c584SApple OSS Distributions int result;
787*1031c584SApple OSS Distributions
788*1031c584SApple OSS Distributions IOLog("/IOMemoryDescriptorTest %d\n", (int) gIOMemoryReferenceCount);
789*1031c584SApple OSS Distributions
790*1031c584SApple OSS Distributions #if 0
791*1031c584SApple OSS Distributions if (6 == newValue) {
792*1031c584SApple OSS Distributions IOMemoryDescriptor * sbmds[3];
793*1031c584SApple OSS Distributions IOMultiMemoryDescriptor * smmd;
794*1031c584SApple OSS Distributions IOMemoryDescriptor * mds[2];
795*1031c584SApple OSS Distributions IOMultiMemoryDescriptor * mmd;
796*1031c584SApple OSS Distributions IOMemoryMap * map;
797*1031c584SApple OSS Distributions
798*1031c584SApple OSS Distributions sbmds[0] = IOBufferMemoryDescriptor::inTaskWithOptions(kernel_task, kIODirectionOutIn | kIOMemoryKernelUserShared, ptoa(1));
799*1031c584SApple OSS Distributions sbmds[1] = IOBufferMemoryDescriptor::inTaskWithOptions(kernel_task, kIODirectionOutIn | kIOMemoryKernelUserShared, ptoa(2));
800*1031c584SApple OSS Distributions sbmds[2] = IOBufferMemoryDescriptor::inTaskWithOptions(kernel_task, kIODirectionOutIn | kIOMemoryKernelUserShared, ptoa(3));
801*1031c584SApple OSS Distributions smmd = IOMultiMemoryDescriptor::withDescriptors(&sbmds[0], sizeof(sbmds) / sizeof(sbmds[0]), kIODirectionOutIn, false);
802*1031c584SApple OSS Distributions
803*1031c584SApple OSS Distributions mds[0] = IOBufferMemoryDescriptor::inTaskWithOptions(kernel_task, kIODirectionOutIn | kIOMemoryKernelUserShared, ptoa(1));
804*1031c584SApple OSS Distributions mds[1] = smmd;
805*1031c584SApple OSS Distributions mmd = IOMultiMemoryDescriptor::withDescriptors(&mds[0], sizeof(mds) / sizeof(mds[0]), kIODirectionOutIn, false);
806*1031c584SApple OSS Distributions map = mmd->createMappingInTask(kernel_task, 0, kIOMapAnywhere | kIOMapGuardedSmall);
807*1031c584SApple OSS Distributions assert(map);
808*1031c584SApple OSS Distributions map->release();
809*1031c584SApple OSS Distributions mmd->release();
810*1031c584SApple OSS Distributions mds[0]->release();
811*1031c584SApple OSS Distributions mds[1]->release();
812*1031c584SApple OSS Distributions sbmds[0]->release();
813*1031c584SApple OSS Distributions sbmds[1]->release();
814*1031c584SApple OSS Distributions sbmds[2]->release();
815*1031c584SApple OSS Distributions
816*1031c584SApple OSS Distributions return 0;
817*1031c584SApple OSS Distributions } else if (5 == newValue) {
818*1031c584SApple OSS Distributions IOReturn ret;
819*1031c584SApple OSS Distributions IOMemoryDescriptor * md;
820*1031c584SApple OSS Distributions IODMACommand * dma;
821*1031c584SApple OSS Distributions IODMACommand::SegmentOptions segOptions =
822*1031c584SApple OSS Distributions {
823*1031c584SApple OSS Distributions .fStructSize = sizeof(segOptions),
824*1031c584SApple OSS Distributions .fNumAddressBits = 64,
825*1031c584SApple OSS Distributions .fMaxSegmentSize = 4096,
826*1031c584SApple OSS Distributions .fMaxTransferSize = 128 * 1024,
827*1031c584SApple OSS Distributions .fAlignment = 4,
828*1031c584SApple OSS Distributions .fAlignmentLength = 4,
829*1031c584SApple OSS Distributions .fAlignmentInternalSegments = 0x1000
830*1031c584SApple OSS Distributions };
831*1031c584SApple OSS Distributions
832*1031c584SApple OSS Distributions IOAddressRange ranges[3][2] =
833*1031c584SApple OSS Distributions {
834*1031c584SApple OSS Distributions {
835*1031c584SApple OSS Distributions { (uintptr_t) &IOMemoryDescriptorTest, 0x2ffc },
836*1031c584SApple OSS Distributions { 0, 0 },
837*1031c584SApple OSS Distributions },
838*1031c584SApple OSS Distributions {
839*1031c584SApple OSS Distributions { ranges[0][0].address, 0x10 },
840*1031c584SApple OSS Distributions { 0x3000 + ranges[0][0].address, 0xff0 },
841*1031c584SApple OSS Distributions },
842*1031c584SApple OSS Distributions {
843*1031c584SApple OSS Distributions { ranges[0][0].address, 0x2ffc },
844*1031c584SApple OSS Distributions { trunc_page(ranges[0][0].address), 0x800 },
845*1031c584SApple OSS Distributions },
846*1031c584SApple OSS Distributions };
847*1031c584SApple OSS Distributions static const uint32_t rangesCount[3] = { 1, 2, 2 };
848*1031c584SApple OSS Distributions uint32_t test;
849*1031c584SApple OSS Distributions
850*1031c584SApple OSS Distributions for (test = 0; test < 3; test++) {
851*1031c584SApple OSS Distributions kprintf("---[%d] address 0x%qx-0x%qx, 0x%qx-0x%qx\n", test,
852*1031c584SApple OSS Distributions ranges[test][0].address, ranges[test][0].length,
853*1031c584SApple OSS Distributions ranges[test][1].address, ranges[test][1].length);
854*1031c584SApple OSS Distributions
855*1031c584SApple OSS Distributions md = IOMemoryDescriptor::withAddressRanges((IOAddressRange*)&ranges[test][0], rangesCount[test], kIODirectionOut, kernel_task);
856*1031c584SApple OSS Distributions assert(md);
857*1031c584SApple OSS Distributions ret = md->prepare();
858*1031c584SApple OSS Distributions assert(kIOReturnSuccess == ret);
859*1031c584SApple OSS Distributions dma = IODMACommand::withSpecification(kIODMACommandOutputHost64, &segOptions,
860*1031c584SApple OSS Distributions IODMACommand::kMapped, NULL, NULL);
861*1031c584SApple OSS Distributions assert(dma);
862*1031c584SApple OSS Distributions ret = dma->setMemoryDescriptor(md, true);
863*1031c584SApple OSS Distributions if (kIOReturnSuccess == ret) {
864*1031c584SApple OSS Distributions IODMACommand::Segment64 segments[1];
865*1031c584SApple OSS Distributions UInt32 numSegments;
866*1031c584SApple OSS Distributions UInt64 offset;
867*1031c584SApple OSS Distributions
868*1031c584SApple OSS Distributions offset = 0;
869*1031c584SApple OSS Distributions do{
870*1031c584SApple OSS Distributions numSegments = 1;
871*1031c584SApple OSS Distributions ret = dma->gen64IOVMSegments(&offset, &segments[0], &numSegments);
872*1031c584SApple OSS Distributions assert(kIOReturnSuccess == ret);
873*1031c584SApple OSS Distributions assert(1 == numSegments);
874*1031c584SApple OSS Distributions kprintf("seg 0x%qx, 0x%qx\n", segments[0].fIOVMAddr, segments[0].fLength);
875*1031c584SApple OSS Distributions }while (offset < md->getLength());
876*1031c584SApple OSS Distributions
877*1031c584SApple OSS Distributions ret = dma->clearMemoryDescriptor(true);
878*1031c584SApple OSS Distributions assert(kIOReturnSuccess == ret);
879*1031c584SApple OSS Distributions dma->release();
880*1031c584SApple OSS Distributions }
881*1031c584SApple OSS Distributions md->release();
882*1031c584SApple OSS Distributions }
883*1031c584SApple OSS Distributions
884*1031c584SApple OSS Distributions return kIOReturnSuccess;
885*1031c584SApple OSS Distributions } else if (4 == newValue) {
886*1031c584SApple OSS Distributions IOService * isp;
887*1031c584SApple OSS Distributions IOMapper * mapper;
888*1031c584SApple OSS Distributions IOBufferMemoryDescriptor * md1;
889*1031c584SApple OSS Distributions IODMACommand * dma;
890*1031c584SApple OSS Distributions IOReturn ret;
891*1031c584SApple OSS Distributions size_t bufSize = 8192 * 8192 * sizeof(uint32_t);
892*1031c584SApple OSS Distributions uint64_t start, time, nano;
893*1031c584SApple OSS Distributions
894*1031c584SApple OSS Distributions isp = IOService::copyMatchingService(IOService::nameMatching("isp"));
895*1031c584SApple OSS Distributions assert(isp);
896*1031c584SApple OSS Distributions mapper = IOMapper::copyMapperForDeviceWithIndex(isp, 0);
897*1031c584SApple OSS Distributions assert(mapper);
898*1031c584SApple OSS Distributions
899*1031c584SApple OSS Distributions md1 = IOBufferMemoryDescriptor::inTaskWithOptions(TASK_NULL,
900*1031c584SApple OSS Distributions kIODirectionOutIn | kIOMemoryPersistent | kIOMemoryPageable,
901*1031c584SApple OSS Distributions bufSize, page_size);
902*1031c584SApple OSS Distributions
903*1031c584SApple OSS Distributions ret = md1->prepare();
904*1031c584SApple OSS Distributions assert(kIOReturnSuccess == ret);
905*1031c584SApple OSS Distributions
906*1031c584SApple OSS Distributions IODMAMapSpecification mapSpec;
907*1031c584SApple OSS Distributions bzero(&mapSpec, sizeof(mapSpec));
908*1031c584SApple OSS Distributions uint64_t mapped;
909*1031c584SApple OSS Distributions uint64_t mappedLength;
910*1031c584SApple OSS Distributions
911*1031c584SApple OSS Distributions start = mach_absolute_time();
912*1031c584SApple OSS Distributions
913*1031c584SApple OSS Distributions ret = md1->dmaMap(mapper, NULL, &mapSpec, 0, bufSize, &mapped, &mappedLength);
914*1031c584SApple OSS Distributions assert(kIOReturnSuccess == ret);
915*1031c584SApple OSS Distributions
916*1031c584SApple OSS Distributions time = mach_absolute_time() - start;
917*1031c584SApple OSS Distributions
918*1031c584SApple OSS Distributions absolutetime_to_nanoseconds(time, &nano);
919*1031c584SApple OSS Distributions kprintf("time %lld us\n", nano / 1000ULL);
920*1031c584SApple OSS Distributions kprintf("seg0 0x%qx, 0x%qx\n", mapped, mappedLength);
921*1031c584SApple OSS Distributions
922*1031c584SApple OSS Distributions assert(md1);
923*1031c584SApple OSS Distributions
924*1031c584SApple OSS Distributions dma = IODMACommand::withSpecification(kIODMACommandOutputHost32,
925*1031c584SApple OSS Distributions 32, 0, IODMACommand::kMapped, 0, 1, mapper, NULL);
926*1031c584SApple OSS Distributions
927*1031c584SApple OSS Distributions assert(dma);
928*1031c584SApple OSS Distributions
929*1031c584SApple OSS Distributions start = mach_absolute_time();
930*1031c584SApple OSS Distributions ret = dma->setMemoryDescriptor(md1, true);
931*1031c584SApple OSS Distributions assert(kIOReturnSuccess == ret);
932*1031c584SApple OSS Distributions time = mach_absolute_time() - start;
933*1031c584SApple OSS Distributions
934*1031c584SApple OSS Distributions absolutetime_to_nanoseconds(time, &nano);
935*1031c584SApple OSS Distributions kprintf("time %lld us\n", nano / 1000ULL);
936*1031c584SApple OSS Distributions
937*1031c584SApple OSS Distributions
938*1031c584SApple OSS Distributions IODMACommand::Segment32 segments[1];
939*1031c584SApple OSS Distributions UInt32 numSegments = 1;
940*1031c584SApple OSS Distributions UInt64 offset;
941*1031c584SApple OSS Distributions
942*1031c584SApple OSS Distributions offset = 0;
943*1031c584SApple OSS Distributions ret = dma->gen32IOVMSegments(&offset, &segments[0], &numSegments);
944*1031c584SApple OSS Distributions assert(kIOReturnSuccess == ret);
945*1031c584SApple OSS Distributions assert(1 == numSegments);
946*1031c584SApple OSS Distributions kprintf("seg0 0x%x, 0x%x\n", (int)segments[0].fIOVMAddr, (int)segments[0].fLength);
947*1031c584SApple OSS Distributions
948*1031c584SApple OSS Distributions ret = dma->clearMemoryDescriptor(true);
949*1031c584SApple OSS Distributions assert(kIOReturnSuccess == ret);
950*1031c584SApple OSS Distributions
951*1031c584SApple OSS Distributions md1->release();
952*1031c584SApple OSS Distributions
953*1031c584SApple OSS Distributions return kIOReturnSuccess;
954*1031c584SApple OSS Distributions }
955*1031c584SApple OSS Distributions
956*1031c584SApple OSS Distributions if (3 == newValue) {
957*1031c584SApple OSS Distributions IOBufferMemoryDescriptor * md1;
958*1031c584SApple OSS Distributions IOBufferMemoryDescriptor * md2;
959*1031c584SApple OSS Distributions IOMemoryMap * map1;
960*1031c584SApple OSS Distributions IOMemoryMap * map2;
961*1031c584SApple OSS Distributions uint32_t * buf1;
962*1031c584SApple OSS Distributions uint32_t * buf2;
963*1031c584SApple OSS Distributions IOReturn err;
964*1031c584SApple OSS Distributions
965*1031c584SApple OSS Distributions md1 = IOBufferMemoryDescriptor::inTaskWithOptions(TASK_NULL,
966*1031c584SApple OSS Distributions kIODirectionOutIn | kIOMemoryPersistent | kIOMemoryPageable,
967*1031c584SApple OSS Distributions 64 * 1024, page_size);
968*1031c584SApple OSS Distributions assert(md1);
969*1031c584SApple OSS Distributions map1 = md1->createMappingInTask(kernel_task, 0, kIOMapAnywhere | kIOMapUnique);
970*1031c584SApple OSS Distributions assert(map1);
971*1031c584SApple OSS Distributions buf1 = (uint32_t *) map1->getVirtualAddress();
972*1031c584SApple OSS Distributions
973*1031c584SApple OSS Distributions md2 = IOBufferMemoryDescriptor::inTaskWithOptions(TASK_NULL,
974*1031c584SApple OSS Distributions kIODirectionOutIn | kIOMemoryPersistent | kIOMemoryPageable,
975*1031c584SApple OSS Distributions 64 * 1024, page_size);
976*1031c584SApple OSS Distributions assert(md2);
977*1031c584SApple OSS Distributions map2 = md2->createMappingInTask(kernel_task, 0, kIOMapAnywhere | kIOMapUnique);
978*1031c584SApple OSS Distributions assert(map2);
979*1031c584SApple OSS Distributions buf2 = (uint32_t *) map2->getVirtualAddress();
980*1031c584SApple OSS Distributions
981*1031c584SApple OSS Distributions memset(buf1, 0x11, 64 * 1024L);
982*1031c584SApple OSS Distributions memset(buf2, 0x22, 64 * 1024L);
983*1031c584SApple OSS Distributions
984*1031c584SApple OSS Distributions kprintf("md1 %p, map1 %p, buf2 %p; md2 %p, map2 %p, buf2 %p\n", md1, map1, buf1, md2, map2, buf2);
985*1031c584SApple OSS Distributions
986*1031c584SApple OSS Distributions kprintf("no redir 0x%08x, 0x%08x\n", buf1[0], buf2[0]);
987*1031c584SApple OSS Distributions assert(0x11111111 == buf1[0]);
988*1031c584SApple OSS Distributions assert(0x22222222 == buf2[0]);
989*1031c584SApple OSS Distributions err = map1->redirect(md2, 0, 0ULL);
990*1031c584SApple OSS Distributions kprintf("redir md2(0x%x) 0x%08x, 0x%08x\n", err, buf1[0], buf2[0]);
991*1031c584SApple OSS Distributions assert(0x11111111 == buf2[0]);
992*1031c584SApple OSS Distributions assert(0x22222222 == buf1[0]);
993*1031c584SApple OSS Distributions err = map1->redirect(md1, 0, 0ULL);
994*1031c584SApple OSS Distributions kprintf("redir md1(0x%x) 0x%08x, 0x%08x\n", err, buf1[0], buf2[0]);
995*1031c584SApple OSS Distributions assert(0x11111111 == buf1[0]);
996*1031c584SApple OSS Distributions assert(0x22222222 == buf2[0]);
997*1031c584SApple OSS Distributions map1->release();
998*1031c584SApple OSS Distributions map2->release();
999*1031c584SApple OSS Distributions md1->release();
1000*1031c584SApple OSS Distributions md2->release();
1001*1031c584SApple OSS Distributions }
1002*1031c584SApple OSS Distributions #endif
1003*1031c584SApple OSS Distributions
1004*1031c584SApple OSS Distributions // result = IODMACommandLocalMappedNonContig(newValue);
1005*1031c584SApple OSS Distributions // if (result) return (result);
1006*1031c584SApple OSS Distributions
1007*1031c584SApple OSS Distributions result = IODMACommandForceDoubleBufferTest(newValue);
1008*1031c584SApple OSS Distributions if (result) {
1009*1031c584SApple OSS Distributions return result;
1010*1031c584SApple OSS Distributions }
1011*1031c584SApple OSS Distributions
1012*1031c584SApple OSS Distributions result = AllocationNameTest(newValue);
1013*1031c584SApple OSS Distributions if (result) {
1014*1031c584SApple OSS Distributions return result;
1015*1031c584SApple OSS Distributions }
1016*1031c584SApple OSS Distributions
1017*1031c584SApple OSS Distributions result = IOMemoryMapCopyOnWriteTest(newValue);
1018*1031c584SApple OSS Distributions if (result) {
1019*1031c584SApple OSS Distributions return result;
1020*1031c584SApple OSS Distributions }
1021*1031c584SApple OSS Distributions
1022*1031c584SApple OSS Distributions result = IOMultMemoryDescriptorTest(newValue);
1023*1031c584SApple OSS Distributions if (result) {
1024*1031c584SApple OSS Distributions return result;
1025*1031c584SApple OSS Distributions }
1026*1031c584SApple OSS Distributions
1027*1031c584SApple OSS Distributions result = IOBMDOverflowTest(newValue);
1028*1031c584SApple OSS Distributions if (result) {
1029*1031c584SApple OSS Distributions return result;
1030*1031c584SApple OSS Distributions }
1031*1031c584SApple OSS Distributions
1032*1031c584SApple OSS Distributions result = IOBMDSetLengthMapTest(newValue);
1033*1031c584SApple OSS Distributions if (result) {
1034*1031c584SApple OSS Distributions return result;
1035*1031c584SApple OSS Distributions }
1036*1031c584SApple OSS Distributions
1037*1031c584SApple OSS Distributions result = ZeroLengthTest(newValue);
1038*1031c584SApple OSS Distributions if (result) {
1039*1031c584SApple OSS Distributions return result;
1040*1031c584SApple OSS Distributions }
1041*1031c584SApple OSS Distributions
1042*1031c584SApple OSS Distributions result = IODirectionPrepareNoZeroFillTest(newValue);
1043*1031c584SApple OSS Distributions if (result) {
1044*1031c584SApple OSS Distributions return result;
1045*1031c584SApple OSS Distributions }
1046*1031c584SApple OSS Distributions
1047*1031c584SApple OSS Distributions result = BadFixedAllocTest(newValue);
1048*1031c584SApple OSS Distributions if (result) {
1049*1031c584SApple OSS Distributions return result;
1050*1031c584SApple OSS Distributions }
1051*1031c584SApple OSS Distributions
1052*1031c584SApple OSS Distributions result = IOMemoryRemoteTest(newValue);
1053*1031c584SApple OSS Distributions if (result) {
1054*1031c584SApple OSS Distributions return result;
1055*1031c584SApple OSS Distributions }
1056*1031c584SApple OSS Distributions
1057*1031c584SApple OSS Distributions result = IOMemoryPrefaultTest(newValue);
1058*1031c584SApple OSS Distributions if (result) {
1059*1031c584SApple OSS Distributions return result;
1060*1031c584SApple OSS Distributions }
1061*1031c584SApple OSS Distributions
1062*1031c584SApple OSS Distributions result = IOGuardPageMDTest(newValue);
1063*1031c584SApple OSS Distributions if (result) {
1064*1031c584SApple OSS Distributions return result;
1065*1031c584SApple OSS Distributions }
1066*1031c584SApple OSS Distributions
1067*1031c584SApple OSS Distributions result = IOMDContextTest(newValue);
1068*1031c584SApple OSS Distributions if (result) {
1069*1031c584SApple OSS Distributions return result;
1070*1031c584SApple OSS Distributions }
1071*1031c584SApple OSS Distributions
1072*1031c584SApple OSS Distributions IOGeneralMemoryDescriptor * md;
1073*1031c584SApple OSS Distributions mach_vm_offset_t data[2];
1074*1031c584SApple OSS Distributions vm_size_t bsize = 16 * 1024 * 1024;
1075*1031c584SApple OSS Distributions vm_size_t srcsize, srcoffset, mapoffset, size;
1076*1031c584SApple OSS Distributions kern_return_t kr;
1077*1031c584SApple OSS Distributions
1078*1031c584SApple OSS Distributions data[0] = data[1] = 0;
1079*1031c584SApple OSS Distributions kr = mach_vm_allocate_kernel(kernel_map, &data[0], bsize, VM_FLAGS_ANYWHERE, VM_KERN_MEMORY_IOKIT);
1080*1031c584SApple OSS Distributions assert(KERN_SUCCESS == kr);
1081*1031c584SApple OSS Distributions
1082*1031c584SApple OSS Distributions mach_vm_inherit(kernel_map, data[0] + ptoa(1), ptoa(1), VM_INHERIT_NONE);
1083*1031c584SApple OSS Distributions mach_vm_inherit(kernel_map, data[0] + ptoa(16), ptoa(4), VM_INHERIT_NONE);
1084*1031c584SApple OSS Distributions
1085*1031c584SApple OSS Distributions IOLog("data 0x%lx, 0x%lx\n", (long)data[0], (long)data[1]);
1086*1031c584SApple OSS Distributions
1087*1031c584SApple OSS Distributions uint32_t idx, offidx;
1088*1031c584SApple OSS Distributions for (idx = 0; idx < (bsize / sizeof(uint32_t)); idx++) {
1089*1031c584SApple OSS Distributions ((uint32_t*)data[0])[idx] = idx;
1090*1031c584SApple OSS Distributions }
1091*1031c584SApple OSS Distributions
1092*1031c584SApple OSS Distributions for (srcoffset = 0; srcoffset < bsize; srcoffset = ((srcoffset << 2) + 0x40c)) {
1093*1031c584SApple OSS Distributions for (srcsize = 4; srcsize < (bsize - srcoffset - 1); srcsize = ((srcsize << 2) + 0x3fc)) {
1094*1031c584SApple OSS Distributions IOAddressRange ranges[3];
1095*1031c584SApple OSS Distributions uint32_t rangeCount = 1;
1096*1031c584SApple OSS Distributions
1097*1031c584SApple OSS Distributions bzero(&ranges[0], sizeof(ranges));
1098*1031c584SApple OSS Distributions ranges[0].address = data[0] + srcoffset;
1099*1031c584SApple OSS Distributions ranges[0].length = srcsize;
1100*1031c584SApple OSS Distributions ranges[1].address = ranges[2].address = data[0];
1101*1031c584SApple OSS Distributions
1102*1031c584SApple OSS Distributions if (srcsize > ptoa(5)) {
1103*1031c584SApple OSS Distributions ranges[0].length = 7634;
1104*1031c584SApple OSS Distributions ranges[1].length = 9870;
1105*1031c584SApple OSS Distributions ranges[2].length = srcsize - ranges[0].length - ranges[1].length;
1106*1031c584SApple OSS Distributions ranges[1].address = ranges[0].address + ranges[0].length;
1107*1031c584SApple OSS Distributions ranges[2].address = ranges[1].address + ranges[1].length;
1108*1031c584SApple OSS Distributions rangeCount = 3;
1109*1031c584SApple OSS Distributions } else if ((srcsize > ptoa(2)) && !(page_mask & srcoffset)) {
1110*1031c584SApple OSS Distributions ranges[0].length = ptoa(1);
1111*1031c584SApple OSS Distributions ranges[1].length = ptoa(1);
1112*1031c584SApple OSS Distributions ranges[2].length = srcsize - ranges[0].length - ranges[1].length;
1113*1031c584SApple OSS Distributions ranges[0].address = data[0] + srcoffset + ptoa(1);
1114*1031c584SApple OSS Distributions ranges[1].address = data[0] + srcoffset;
1115*1031c584SApple OSS Distributions ranges[2].address = ranges[0].address + ranges[0].length;
1116*1031c584SApple OSS Distributions rangeCount = 3;
1117*1031c584SApple OSS Distributions }
1118*1031c584SApple OSS Distributions
1119*1031c584SApple OSS Distributions md = OSDynamicCast(IOGeneralMemoryDescriptor,
1120*1031c584SApple OSS Distributions IOMemoryDescriptor::withAddressRanges(&ranges[0], rangeCount, kIODirectionInOut, kernel_task));
1121*1031c584SApple OSS Distributions assert(md);
1122*1031c584SApple OSS Distributions
1123*1031c584SApple OSS Distributions IOLog("IOMemoryDescriptor::withAddressRanges [0x%lx @ 0x%lx]\n[0x%llx, 0x%llx],\n[0x%llx, 0x%llx],\n[0x%llx, 0x%llx]\n",
1124*1031c584SApple OSS Distributions (long) srcsize, (long) srcoffset,
1125*1031c584SApple OSS Distributions (long long) ranges[0].address - data[0], (long long) ranges[0].length,
1126*1031c584SApple OSS Distributions (long long) ranges[1].address - data[0], (long long) ranges[1].length,
1127*1031c584SApple OSS Distributions (long long) ranges[2].address - data[0], (long long) ranges[2].length);
1128*1031c584SApple OSS Distributions
1129*1031c584SApple OSS Distributions if (kIOReturnSuccess == kr) {
1130*1031c584SApple OSS Distributions for (mapoffset = 0; mapoffset < srcsize; mapoffset = ((mapoffset << 1) + 0xf00)) {
1131*1031c584SApple OSS Distributions for (size = 4; size < (srcsize - mapoffset - 1); size = ((size << 2) + 0x200)) {
1132*1031c584SApple OSS Distributions IOMemoryMap * map;
1133*1031c584SApple OSS Distributions mach_vm_address_t addr = 0;
1134*1031c584SApple OSS Distributions uint32_t data;
1135*1031c584SApple OSS Distributions
1136*1031c584SApple OSS Distributions // IOLog("<mapRef [0x%lx @ 0x%lx]\n", (long) size, (long) mapoffset);
1137*1031c584SApple OSS Distributions
1138*1031c584SApple OSS Distributions map = md->createMappingInTask(kernel_task, 0, kIOMapAnywhere | kIOMapGuardedSmall, mapoffset, size);
1139*1031c584SApple OSS Distributions if (map) {
1140*1031c584SApple OSS Distributions addr = map->getAddress();
1141*1031c584SApple OSS Distributions } else {
1142*1031c584SApple OSS Distributions kr = kIOReturnError;
1143*1031c584SApple OSS Distributions }
1144*1031c584SApple OSS Distributions
1145*1031c584SApple OSS Distributions // IOLog(">mapRef 0x%x %llx\n", kr, addr);
1146*1031c584SApple OSS Distributions
1147*1031c584SApple OSS Distributions if (kIOReturnSuccess != kr) {
1148*1031c584SApple OSS Distributions break;
1149*1031c584SApple OSS Distributions }
1150*1031c584SApple OSS Distributions kr = md->prepare();
1151*1031c584SApple OSS Distributions if (kIOReturnSuccess != kr) {
1152*1031c584SApple OSS Distributions panic("prepare() fail 0x%x", kr);
1153*1031c584SApple OSS Distributions break;
1154*1031c584SApple OSS Distributions }
1155*1031c584SApple OSS Distributions for (idx = 0; idx < size; idx += sizeof(uint32_t)) {
1156*1031c584SApple OSS Distributions offidx = (typeof(offidx))(idx + mapoffset + srcoffset);
1157*1031c584SApple OSS Distributions if ((srcsize <= ptoa(5)) && (srcsize > ptoa(2)) && !(page_mask & srcoffset)) {
1158*1031c584SApple OSS Distributions if (offidx < ptoa(2)) {
1159*1031c584SApple OSS Distributions offidx ^= ptoa(1);
1160*1031c584SApple OSS Distributions }
1161*1031c584SApple OSS Distributions }
1162*1031c584SApple OSS Distributions offidx /= sizeof(uint32_t);
1163*1031c584SApple OSS Distributions
1164*1031c584SApple OSS Distributions if (offidx != ((uint32_t*)addr)[idx / sizeof(uint32_t)]) {
1165*1031c584SApple OSS Distributions panic("vm mismatch md %p map %p, @ 0x%x, 0x%lx, 0x%lx,", md, map, idx, (long) srcoffset, (long) mapoffset);
1166*1031c584SApple OSS Distributions kr = kIOReturnBadMedia;
1167*1031c584SApple OSS Distributions } else {
1168*1031c584SApple OSS Distributions if (sizeof(data) != md->readBytes(mapoffset + idx, &data, sizeof(data))) {
1169*1031c584SApple OSS Distributions data = 0;
1170*1031c584SApple OSS Distributions }
1171*1031c584SApple OSS Distributions if (offidx != data) {
1172*1031c584SApple OSS Distributions panic("phys mismatch md %p map %p, @ 0x%x, 0x%lx, 0x%lx,", md, map, idx, (long) srcoffset, (long) mapoffset);
1173*1031c584SApple OSS Distributions kr = kIOReturnBadMedia;
1174*1031c584SApple OSS Distributions }
1175*1031c584SApple OSS Distributions }
1176*1031c584SApple OSS Distributions }
1177*1031c584SApple OSS Distributions md->complete();
1178*1031c584SApple OSS Distributions map->release();
1179*1031c584SApple OSS Distributions // IOLog("unmapRef %llx\n", addr);
1180*1031c584SApple OSS Distributions }
1181*1031c584SApple OSS Distributions if (kIOReturnSuccess != kr) {
1182*1031c584SApple OSS Distributions break;
1183*1031c584SApple OSS Distributions }
1184*1031c584SApple OSS Distributions }
1185*1031c584SApple OSS Distributions }
1186*1031c584SApple OSS Distributions md->release();
1187*1031c584SApple OSS Distributions if (kIOReturnSuccess != kr) {
1188*1031c584SApple OSS Distributions break;
1189*1031c584SApple OSS Distributions }
1190*1031c584SApple OSS Distributions }
1191*1031c584SApple OSS Distributions if (kIOReturnSuccess != kr) {
1192*1031c584SApple OSS Distributions break;
1193*1031c584SApple OSS Distributions }
1194*1031c584SApple OSS Distributions }
1195*1031c584SApple OSS Distributions
1196*1031c584SApple OSS Distributions if (kIOReturnSuccess != kr) {
1197*1031c584SApple OSS Distributions IOLog("FAIL: src 0x%lx @ 0x%lx, map 0x%lx @ 0x%lx\n",
1198*1031c584SApple OSS Distributions (long) srcsize, (long) srcoffset, (long) size, (long) mapoffset);
1199*1031c584SApple OSS Distributions }
1200*1031c584SApple OSS Distributions
1201*1031c584SApple OSS Distributions assert(kr == kIOReturnSuccess);
1202*1031c584SApple OSS Distributions
1203*1031c584SApple OSS Distributions mach_vm_deallocate(kernel_map, data[0], bsize);
1204*1031c584SApple OSS Distributions //mach_vm_deallocate(kernel_map, data[1], size);
1205*1031c584SApple OSS Distributions
1206*1031c584SApple OSS Distributions IOLog("IOMemoryDescriptorTest/ %d\n", (int) gIOMemoryReferenceCount);
1207*1031c584SApple OSS Distributions
1208*1031c584SApple OSS Distributions return 0;
1209*1031c584SApple OSS Distributions }
1210*1031c584SApple OSS Distributions
1211*1031c584SApple OSS Distributions
1212*1031c584SApple OSS Distributions #endif /* DEVELOPMENT || DEBUG */
1213