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