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