xref: /xnu-12377.41.6/iokit/Tests/TestIOMemoryDescriptor.cpp (revision bbb1b6f9e71b8cdde6e5cd6f4841f207dee3d828) !
1*bbb1b6f9SApple OSS Distributions /*
2*bbb1b6f9SApple OSS Distributions  * Copyright (c) 2014-2020 Apple Inc. All rights reserved.
3*bbb1b6f9SApple OSS Distributions  *
4*bbb1b6f9SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*bbb1b6f9SApple OSS Distributions  *
6*bbb1b6f9SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*bbb1b6f9SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*bbb1b6f9SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*bbb1b6f9SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*bbb1b6f9SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*bbb1b6f9SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*bbb1b6f9SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*bbb1b6f9SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*bbb1b6f9SApple OSS Distributions  *
15*bbb1b6f9SApple OSS Distributions  * Please obtain a copy of the License at
16*bbb1b6f9SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*bbb1b6f9SApple OSS Distributions  *
18*bbb1b6f9SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*bbb1b6f9SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*bbb1b6f9SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*bbb1b6f9SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*bbb1b6f9SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*bbb1b6f9SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*bbb1b6f9SApple OSS Distributions  * limitations under the License.
25*bbb1b6f9SApple OSS Distributions  *
26*bbb1b6f9SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*bbb1b6f9SApple OSS Distributions  */
28*bbb1b6f9SApple OSS Distributions 
29*bbb1b6f9SApple OSS Distributions #include <sys/cdefs.h>
30*bbb1b6f9SApple OSS Distributions 
31*bbb1b6f9SApple OSS Distributions #include <IOKit/assert.h>
32*bbb1b6f9SApple OSS Distributions #include <IOKit/system.h>
33*bbb1b6f9SApple OSS Distributions #include <IOKit/IOLib.h>
34*bbb1b6f9SApple OSS Distributions #include <IOKit/IOMemoryDescriptor.h>
35*bbb1b6f9SApple OSS Distributions #include <IOKit/IOMapper.h>
36*bbb1b6f9SApple OSS Distributions #include <IOKit/IODMACommand.h>
37*bbb1b6f9SApple OSS Distributions #include <IOKit/IOKitKeysPrivate.h>
38*bbb1b6f9SApple OSS Distributions #include <Kernel/IOKitKernelInternal.h>
39*bbb1b6f9SApple OSS Distributions #include <IOKit/IOUserClient.h>
40*bbb1b6f9SApple OSS Distributions #include <IOKit/IOService.h>
41*bbb1b6f9SApple OSS Distributions #include "Tests.h"
42*bbb1b6f9SApple OSS Distributions 
43*bbb1b6f9SApple OSS Distributions #ifndef __LP64__
44*bbb1b6f9SApple OSS Distributions #include <IOKit/IOSubMemoryDescriptor.h>
45*bbb1b6f9SApple OSS Distributions #endif /* !__LP64__ */
46*bbb1b6f9SApple OSS Distributions #include <IOKit/IOSubMemoryDescriptor.h>
47*bbb1b6f9SApple OSS Distributions #include <IOKit/IOMultiMemoryDescriptor.h>
48*bbb1b6f9SApple OSS Distributions #include <IOKit/IOBufferMemoryDescriptor.h>
49*bbb1b6f9SApple OSS Distributions #include <IOKit/IOGuardPageMemoryDescriptor.h>
50*bbb1b6f9SApple OSS Distributions 
51*bbb1b6f9SApple OSS Distributions #include <IOKit/IOKitDebug.h>
52*bbb1b6f9SApple OSS Distributions #include <libkern/OSDebug.h>
53*bbb1b6f9SApple OSS Distributions #include <sys/uio.h>
54*bbb1b6f9SApple OSS Distributions #include <libkern/sysctl.h>
55*bbb1b6f9SApple OSS Distributions #include <sys/sysctl.h>
56*bbb1b6f9SApple OSS Distributions 
57*bbb1b6f9SApple OSS Distributions __BEGIN_DECLS
58*bbb1b6f9SApple OSS Distributions #include <vm/pmap.h>
59*bbb1b6f9SApple OSS Distributions #include <vm/vm_pageout.h>
60*bbb1b6f9SApple OSS Distributions #include <mach/memory_object_types.h>
61*bbb1b6f9SApple OSS Distributions #include <device/device_port.h>
62*bbb1b6f9SApple OSS Distributions 
63*bbb1b6f9SApple OSS Distributions #include <mach/vm_prot.h>
64*bbb1b6f9SApple OSS Distributions #include <mach/mach_vm.h>
65*bbb1b6f9SApple OSS Distributions #include <mach/vm_param.h>
66*bbb1b6f9SApple OSS Distributions #include <vm/vm_fault.h>
67*bbb1b6f9SApple OSS Distributions #include <vm/vm_protos.h>
68*bbb1b6f9SApple OSS Distributions #include <vm/vm_map_xnu.h>
69*bbb1b6f9SApple OSS Distributions #include <vm/vm_kern_xnu.h>
70*bbb1b6f9SApple OSS Distributions __END_DECLS
71*bbb1b6f9SApple OSS Distributions 
72*bbb1b6f9SApple OSS Distributions 
73*bbb1b6f9SApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
74*bbb1b6f9SApple OSS Distributions 
75*bbb1b6f9SApple OSS Distributions #if DEVELOPMENT || DEBUG
76*bbb1b6f9SApple OSS Distributions 
77*bbb1b6f9SApple OSS Distributions extern SInt32 gIOMemoryReferenceCount;
78*bbb1b6f9SApple OSS Distributions 
79*bbb1b6f9SApple OSS Distributions static int
IOMultMemoryDescriptorTest(int newValue)80*bbb1b6f9SApple OSS Distributions IOMultMemoryDescriptorTest(int newValue)
81*bbb1b6f9SApple OSS Distributions {
82*bbb1b6f9SApple OSS Distributions 	IOMemoryDescriptor * mds[3];
83*bbb1b6f9SApple OSS Distributions 	IOMultiMemoryDescriptor * mmd;
84*bbb1b6f9SApple OSS Distributions 	IOMemoryMap * map;
85*bbb1b6f9SApple OSS Distributions 	void * addr;
86*bbb1b6f9SApple OSS Distributions 	uint8_t * data;
87*bbb1b6f9SApple OSS Distributions 	uint32_t i;
88*bbb1b6f9SApple OSS Distributions 	IOAddressRange ranges[2];
89*bbb1b6f9SApple OSS Distributions 
90*bbb1b6f9SApple OSS Distributions 	data = (typeof(data))IOMallocAligned(ptoa(8), page_size);
91*bbb1b6f9SApple OSS Distributions 	for (i = 0; i < ptoa(8); i++) {
92*bbb1b6f9SApple OSS Distributions 		data[i] = ((uint8_t) atop(i)) | 0xD0;
93*bbb1b6f9SApple OSS Distributions 	}
94*bbb1b6f9SApple OSS Distributions 
95*bbb1b6f9SApple OSS Distributions 	ranges[0].address = (IOVirtualAddress)(data + ptoa(4));
96*bbb1b6f9SApple OSS Distributions 	ranges[0].length  = ptoa(4);
97*bbb1b6f9SApple OSS Distributions 	ranges[1].address = (IOVirtualAddress)(data + ptoa(0));
98*bbb1b6f9SApple OSS Distributions 	ranges[1].length  = ptoa(4);
99*bbb1b6f9SApple OSS Distributions 
100*bbb1b6f9SApple OSS Distributions 	mds[0] = IOMemoryDescriptor::withAddressRange((mach_vm_address_t) data, 2, kIODirectionOutIn, kernel_task);
101*bbb1b6f9SApple OSS Distributions 	assert(mds[0]);
102*bbb1b6f9SApple OSS Distributions 	{
103*bbb1b6f9SApple OSS Distributions 		uint64_t dmaLen, dmaOffset;
104*bbb1b6f9SApple OSS Distributions 		dmaLen = mds[0]->getDMAMapLength(&dmaOffset);
105*bbb1b6f9SApple OSS Distributions 		assert(0 == dmaOffset);
106*bbb1b6f9SApple OSS Distributions 		assert(ptoa(1) == dmaLen);
107*bbb1b6f9SApple OSS Distributions 	}
108*bbb1b6f9SApple OSS Distributions 	mds[0]->release();
109*bbb1b6f9SApple OSS Distributions 	mds[0] = IOMemoryDescriptor::withAddressRange((mach_vm_address_t) (data + page_size - 2), 4, kIODirectionOutIn, kernel_task);
110*bbb1b6f9SApple OSS Distributions 	assert(mds[0]);
111*bbb1b6f9SApple OSS Distributions 	{
112*bbb1b6f9SApple OSS Distributions 		uint64_t dmaLen, dmaOffset;
113*bbb1b6f9SApple OSS Distributions 		dmaLen = mds[0]->getDMAMapLength(&dmaOffset);
114*bbb1b6f9SApple OSS Distributions 		assert((page_size - 2) == dmaOffset);
115*bbb1b6f9SApple OSS Distributions 		assert(ptoa(2) == dmaLen);
116*bbb1b6f9SApple OSS Distributions 	}
117*bbb1b6f9SApple OSS Distributions 	mds[0]->release();
118*bbb1b6f9SApple OSS Distributions 
119*bbb1b6f9SApple OSS Distributions 	mds[0] = IOMemoryDescriptor::withAddressRanges(&ranges[0], 2, kIODirectionOutIn, kernel_task);
120*bbb1b6f9SApple OSS Distributions 	{
121*bbb1b6f9SApple OSS Distributions 		uint64_t dmaLen, dmaOffset;
122*bbb1b6f9SApple OSS Distributions 		dmaLen = mds[0]->getDMAMapLength(&dmaOffset);
123*bbb1b6f9SApple OSS Distributions 		assert(0 == dmaOffset);
124*bbb1b6f9SApple OSS Distributions 		assert(ptoa(8) == dmaLen);
125*bbb1b6f9SApple OSS Distributions 	}
126*bbb1b6f9SApple OSS Distributions 	mds[1] = IOSubMemoryDescriptor::withSubRange(mds[0], ptoa(3), ptoa(2), kIODirectionOutIn);
127*bbb1b6f9SApple OSS Distributions 	{
128*bbb1b6f9SApple OSS Distributions 		uint64_t dmaLen, dmaOffset;
129*bbb1b6f9SApple OSS Distributions 		dmaLen = mds[1]->getDMAMapLength(&dmaOffset);
130*bbb1b6f9SApple OSS Distributions 		assert(0 == dmaOffset);
131*bbb1b6f9SApple OSS Distributions 		assert(ptoa(2) == dmaLen);
132*bbb1b6f9SApple OSS Distributions 	}
133*bbb1b6f9SApple OSS Distributions 	mds[2] = IOSubMemoryDescriptor::withSubRange(mds[0], ptoa(7), ptoa(1), kIODirectionOutIn);
134*bbb1b6f9SApple OSS Distributions 
135*bbb1b6f9SApple OSS Distributions 	mmd = IOMultiMemoryDescriptor::withDescriptors(&mds[0], sizeof(mds) / sizeof(mds[0]), kIODirectionOutIn, false);
136*bbb1b6f9SApple OSS Distributions 	{
137*bbb1b6f9SApple OSS Distributions 		uint64_t dmaLen, dmaOffset;
138*bbb1b6f9SApple OSS Distributions 		dmaLen = mmd->getDMAMapLength(&dmaOffset);
139*bbb1b6f9SApple OSS Distributions 		assert(0 == dmaOffset);
140*bbb1b6f9SApple OSS Distributions 		assert(ptoa(11) == dmaLen);
141*bbb1b6f9SApple OSS Distributions 	}
142*bbb1b6f9SApple OSS Distributions 	mds[2]->release();
143*bbb1b6f9SApple OSS Distributions 	mds[1]->release();
144*bbb1b6f9SApple OSS Distributions 	mds[0]->release();
145*bbb1b6f9SApple OSS Distributions 	map = mmd->createMappingInTask(kernel_task, 0, kIOMapAnywhere | kIOMapGuardedSmall, ptoa(7), mmd->getLength() - ptoa(7));
146*bbb1b6f9SApple OSS Distributions 	mmd->release();
147*bbb1b6f9SApple OSS Distributions 	assert(map);
148*bbb1b6f9SApple OSS Distributions 
149*bbb1b6f9SApple OSS Distributions 	addr = (void *) map->getVirtualAddress();
150*bbb1b6f9SApple OSS Distributions 	assert(ptoa(4) == map->getLength());
151*bbb1b6f9SApple OSS Distributions 	assert(0xd3d3d3d3 == ((uint32_t *)addr)[ptoa(0) / sizeof(uint32_t)]);
152*bbb1b6f9SApple OSS Distributions 	assert(0xd7d7d7d7 == ((uint32_t *)addr)[ptoa(1) / sizeof(uint32_t)]);
153*bbb1b6f9SApple OSS Distributions 	assert(0xd0d0d0d0 == ((uint32_t *)addr)[ptoa(2) / sizeof(uint32_t)]);
154*bbb1b6f9SApple OSS Distributions 	assert(0xd3d3d3d3 == ((uint32_t *)addr)[ptoa(3) / sizeof(uint32_t)]);
155*bbb1b6f9SApple OSS Distributions 	map->release();
156*bbb1b6f9SApple OSS Distributions 	IOFreeAligned(data, ptoa(8));
157*bbb1b6f9SApple OSS Distributions 
158*bbb1b6f9SApple OSS Distributions 	return 0;
159*bbb1b6f9SApple OSS Distributions }
160*bbb1b6f9SApple OSS Distributions 
161*bbb1b6f9SApple OSS Distributions 
162*bbb1b6f9SApple OSS Distributions 
163*bbb1b6f9SApple OSS Distributions // <rdar://problem/30102458>
164*bbb1b6f9SApple OSS Distributions static int
IODMACommandForceDoubleBufferTest(int newValue)165*bbb1b6f9SApple OSS Distributions IODMACommandForceDoubleBufferTest(int newValue)
166*bbb1b6f9SApple OSS Distributions {
167*bbb1b6f9SApple OSS Distributions 	IOReturn                   ret;
168*bbb1b6f9SApple OSS Distributions 	IOBufferMemoryDescriptor * bmd;
169*bbb1b6f9SApple OSS Distributions 	IODMACommand             * dma;
170*bbb1b6f9SApple OSS Distributions 	uint32_t                   dir, data;
171*bbb1b6f9SApple OSS Distributions 	IODMACommand::SegmentOptions segOptions =
172*bbb1b6f9SApple OSS Distributions 	{
173*bbb1b6f9SApple OSS Distributions 		.fStructSize      = sizeof(segOptions),
174*bbb1b6f9SApple OSS Distributions 		.fNumAddressBits  = 64,
175*bbb1b6f9SApple OSS Distributions 		.fMaxSegmentSize  = 0x2000,
176*bbb1b6f9SApple OSS Distributions 		.fMaxTransferSize = 128 * 1024,
177*bbb1b6f9SApple OSS Distributions 		.fAlignment       = 1,
178*bbb1b6f9SApple OSS Distributions 		.fAlignmentLength = 1,
179*bbb1b6f9SApple OSS Distributions 		.fAlignmentInternalSegments = 1
180*bbb1b6f9SApple OSS Distributions 	};
181*bbb1b6f9SApple OSS Distributions 	IODMACommand::Segment64 segments[1];
182*bbb1b6f9SApple OSS Distributions 	UInt32                  numSegments;
183*bbb1b6f9SApple OSS Distributions 	UInt64                  dmaOffset;
184*bbb1b6f9SApple OSS Distributions 
185*bbb1b6f9SApple OSS Distributions 
186*bbb1b6f9SApple OSS Distributions 	for (dir = kIODirectionIn;; dir++) {
187*bbb1b6f9SApple OSS Distributions 		bmd = IOBufferMemoryDescriptor::inTaskWithOptions(kernel_task,
188*bbb1b6f9SApple OSS Distributions 		    dir | kIOMemoryPageable, ptoa(8));
189*bbb1b6f9SApple OSS Distributions 		assert(bmd);
190*bbb1b6f9SApple OSS Distributions 		{
191*bbb1b6f9SApple OSS Distributions 			uint64_t dmaLen, dmaOffset;
192*bbb1b6f9SApple OSS Distributions 			dmaLen = bmd->getDMAMapLength(&dmaOffset);
193*bbb1b6f9SApple OSS Distributions 			assert(0 == dmaOffset);
194*bbb1b6f9SApple OSS Distributions 			assert(ptoa(8) == dmaLen);
195*bbb1b6f9SApple OSS Distributions 		}
196*bbb1b6f9SApple OSS Distributions 
197*bbb1b6f9SApple OSS Distributions 		((uint32_t*) bmd->getBytesNoCopy())[0] = 0x53535300 | dir;
198*bbb1b6f9SApple OSS Distributions 
199*bbb1b6f9SApple OSS Distributions 		ret = bmd->prepare((IODirection) dir);
200*bbb1b6f9SApple OSS Distributions 		assert(kIOReturnSuccess == ret);
201*bbb1b6f9SApple OSS Distributions 
202*bbb1b6f9SApple OSS Distributions 		dma = IODMACommand::withSpecification(kIODMACommandOutputHost64, &segOptions,
203*bbb1b6f9SApple OSS Distributions 		    kIODMAMapOptionMapped,
204*bbb1b6f9SApple OSS Distributions 		    NULL, NULL);
205*bbb1b6f9SApple OSS Distributions 		assert(dma);
206*bbb1b6f9SApple OSS Distributions 		ret = dma->setMemoryDescriptor(bmd, true);
207*bbb1b6f9SApple OSS Distributions 		assert(kIOReturnSuccess == ret);
208*bbb1b6f9SApple OSS Distributions 
209*bbb1b6f9SApple OSS Distributions 		ret = dma->synchronize(IODMACommand::kForceDoubleBuffer | kIODirectionOut);
210*bbb1b6f9SApple OSS Distributions 		assert(kIOReturnSuccess == ret);
211*bbb1b6f9SApple OSS Distributions 
212*bbb1b6f9SApple OSS Distributions 		dmaOffset   = 0;
213*bbb1b6f9SApple OSS Distributions 		numSegments = 1;
214*bbb1b6f9SApple OSS Distributions 		ret = dma->gen64IOVMSegments(&dmaOffset, &segments[0], &numSegments);
215*bbb1b6f9SApple OSS Distributions 		assert(kIOReturnSuccess == ret);
216*bbb1b6f9SApple OSS Distributions 		assert(1 == numSegments);
217*bbb1b6f9SApple OSS Distributions 
218*bbb1b6f9SApple OSS Distributions 		if (kIODirectionOut & dir) {
219*bbb1b6f9SApple OSS Distributions 			data = ((uint32_t*) bmd->getBytesNoCopy())[0];
220*bbb1b6f9SApple OSS Distributions 			assertf((0x53535300 | dir) == data, "mismatch 0x%x", data);
221*bbb1b6f9SApple OSS Distributions 		}
222*bbb1b6f9SApple OSS Distributions 		if (kIODirectionIn & dir) {
223*bbb1b6f9SApple OSS Distributions 			IOMappedWrite32(segments[0].fIOVMAddr, 0x11223300 | dir);
224*bbb1b6f9SApple OSS Distributions 		}
225*bbb1b6f9SApple OSS Distributions 
226*bbb1b6f9SApple OSS Distributions 		ret = dma->clearMemoryDescriptor(true);
227*bbb1b6f9SApple OSS Distributions 		assert(kIOReturnSuccess == ret);
228*bbb1b6f9SApple OSS Distributions 		dma->release();
229*bbb1b6f9SApple OSS Distributions 
230*bbb1b6f9SApple OSS Distributions 		bmd->complete((IODirection) dir);
231*bbb1b6f9SApple OSS Distributions 
232*bbb1b6f9SApple OSS Distributions 		if (kIODirectionIn & dir) {
233*bbb1b6f9SApple OSS Distributions 			data = ((uint32_t*) bmd->getBytesNoCopy())[0];
234*bbb1b6f9SApple OSS Distributions 			assertf((0x11223300 | dir) == data, "mismatch 0x%x", data);
235*bbb1b6f9SApple OSS Distributions 		}
236*bbb1b6f9SApple OSS Distributions 
237*bbb1b6f9SApple OSS Distributions 		bmd->release();
238*bbb1b6f9SApple OSS Distributions 
239*bbb1b6f9SApple OSS Distributions 		if (dir == kIODirectionInOut) {
240*bbb1b6f9SApple OSS Distributions 			break;
241*bbb1b6f9SApple OSS Distributions 		}
242*bbb1b6f9SApple OSS Distributions 	}
243*bbb1b6f9SApple OSS Distributions 
244*bbb1b6f9SApple OSS Distributions 	return 0;
245*bbb1b6f9SApple OSS Distributions }
246*bbb1b6f9SApple OSS Distributions 
247*bbb1b6f9SApple OSS Distributions // <rdar://problem/34322778>
248*bbb1b6f9SApple OSS Distributions static int __unused
IODMACommandLocalMappedNonContig(int newValue)249*bbb1b6f9SApple OSS Distributions IODMACommandLocalMappedNonContig(int newValue)
250*bbb1b6f9SApple OSS Distributions {
251*bbb1b6f9SApple OSS Distributions 	IOReturn                   kr;
252*bbb1b6f9SApple OSS Distributions 	IOMemoryDescriptor       * md;
253*bbb1b6f9SApple OSS Distributions 	IODMACommand             * dma;
254*bbb1b6f9SApple OSS Distributions 	OSDictionary             * matching;
255*bbb1b6f9SApple OSS Distributions 	IOService                * device;
256*bbb1b6f9SApple OSS Distributions 	IOMapper                 * mapper;
257*bbb1b6f9SApple OSS Distributions 	IODMACommand::SegmentOptions segOptions =
258*bbb1b6f9SApple OSS Distributions 	{
259*bbb1b6f9SApple OSS Distributions 		.fStructSize      = sizeof(segOptions),
260*bbb1b6f9SApple OSS Distributions 		.fNumAddressBits  = 64,
261*bbb1b6f9SApple OSS Distributions 		.fMaxSegmentSize  = 128 * 1024,
262*bbb1b6f9SApple OSS Distributions 		.fMaxTransferSize = 128 * 1024,
263*bbb1b6f9SApple OSS Distributions 		.fAlignment       = 1,
264*bbb1b6f9SApple OSS Distributions 		.fAlignmentLength = 1,
265*bbb1b6f9SApple OSS Distributions 		.fAlignmentInternalSegments = 1
266*bbb1b6f9SApple OSS Distributions 	};
267*bbb1b6f9SApple OSS Distributions 	IODMACommand::Segment64 segments[1];
268*bbb1b6f9SApple OSS Distributions 	UInt32                  numSegments;
269*bbb1b6f9SApple OSS Distributions 	UInt64                  dmaOffset;
270*bbb1b6f9SApple OSS Distributions 	UInt64                  segPhys;
271*bbb1b6f9SApple OSS Distributions 	mach_vm_address_t       buffer;
272*bbb1b6f9SApple OSS Distributions 	vm_size_t               bufSize = ptoa(4);
273*bbb1b6f9SApple OSS Distributions 
274*bbb1b6f9SApple OSS Distributions 	if (!IOMapper::gSystem) {
275*bbb1b6f9SApple OSS Distributions 		return 0;
276*bbb1b6f9SApple OSS Distributions 	}
277*bbb1b6f9SApple OSS Distributions 
278*bbb1b6f9SApple OSS Distributions 	buffer = 0;
279*bbb1b6f9SApple OSS Distributions 	kr = mach_vm_allocate_kernel(kernel_map, &buffer, bufSize,
280*bbb1b6f9SApple OSS Distributions 	    VM_MAP_KERNEL_FLAGS_ANYWHERE(.vm_tag = VM_KERN_MEMORY_IOKIT));
281*bbb1b6f9SApple OSS Distributions 	assert(KERN_SUCCESS == kr);
282*bbb1b6f9SApple OSS Distributions 
283*bbb1b6f9SApple OSS Distributions 	// fragment the vmentries
284*bbb1b6f9SApple OSS Distributions 	kr = mach_vm_inherit(kernel_map, buffer + ptoa(1), ptoa(1), VM_INHERIT_NONE);
285*bbb1b6f9SApple OSS Distributions 	assert(KERN_SUCCESS == kr);
286*bbb1b6f9SApple OSS Distributions 
287*bbb1b6f9SApple OSS Distributions 	md = IOMemoryDescriptor::withAddressRange(
288*bbb1b6f9SApple OSS Distributions 		buffer + 0xa00, 0x2000, kIODirectionOutIn, kernel_task);
289*bbb1b6f9SApple OSS Distributions 	assert(md);
290*bbb1b6f9SApple OSS Distributions 	kr = md->prepare(kIODirectionOutIn);
291*bbb1b6f9SApple OSS Distributions 	assert(kIOReturnSuccess == kr);
292*bbb1b6f9SApple OSS Distributions 
293*bbb1b6f9SApple OSS Distributions 	segPhys = md->getPhysicalSegment(0, NULL, 0);
294*bbb1b6f9SApple OSS Distributions 
295*bbb1b6f9SApple OSS Distributions 	matching = IOService::nameMatching("XHC1");
296*bbb1b6f9SApple OSS Distributions 	assert(matching);
297*bbb1b6f9SApple OSS Distributions 	device = IOService::copyMatchingService(matching);
298*bbb1b6f9SApple OSS Distributions 	matching->release();
299*bbb1b6f9SApple OSS Distributions 	mapper = device ? IOMapper::copyMapperForDeviceWithIndex(device, 0) : NULL;
300*bbb1b6f9SApple OSS Distributions 	OSSafeReleaseNULL(device);
301*bbb1b6f9SApple OSS Distributions 
302*bbb1b6f9SApple OSS Distributions 	dma = IODMACommand::withSpecification(kIODMACommandOutputHost64, &segOptions,
303*bbb1b6f9SApple OSS Distributions 	    kIODMAMapOptionMapped,
304*bbb1b6f9SApple OSS Distributions 	    mapper, NULL);
305*bbb1b6f9SApple OSS Distributions 	assert(dma);
306*bbb1b6f9SApple OSS Distributions 	kr = dma->setMemoryDescriptor(md, true);
307*bbb1b6f9SApple OSS Distributions 	assert(kIOReturnSuccess == kr);
308*bbb1b6f9SApple OSS Distributions 
309*bbb1b6f9SApple OSS Distributions 	dmaOffset   = 0;
310*bbb1b6f9SApple OSS Distributions 	numSegments = 1;
311*bbb1b6f9SApple OSS Distributions 	kr = dma->gen64IOVMSegments(&dmaOffset, &segments[0], &numSegments);
312*bbb1b6f9SApple OSS Distributions 	assert(kIOReturnSuccess == kr);
313*bbb1b6f9SApple OSS Distributions 	assert(1 == numSegments);
314*bbb1b6f9SApple OSS Distributions 
315*bbb1b6f9SApple OSS Distributions 	if (mapper) {
316*bbb1b6f9SApple OSS Distributions 		assertf(segments[0].fIOVMAddr != segPhys, "phys !local 0x%qx, 0x%qx, %p", segments[0].fIOVMAddr, segPhys, dma);
317*bbb1b6f9SApple OSS Distributions 	}
318*bbb1b6f9SApple OSS Distributions 
319*bbb1b6f9SApple OSS Distributions 	kr = dma->clearMemoryDescriptor(true);
320*bbb1b6f9SApple OSS Distributions 	assert(kIOReturnSuccess == kr);
321*bbb1b6f9SApple OSS Distributions 	dma->release();
322*bbb1b6f9SApple OSS Distributions 
323*bbb1b6f9SApple OSS Distributions 	kr = md->complete(kIODirectionOutIn);
324*bbb1b6f9SApple OSS Distributions 	assert(kIOReturnSuccess == kr);
325*bbb1b6f9SApple OSS Distributions 	md->release();
326*bbb1b6f9SApple OSS Distributions 
327*bbb1b6f9SApple OSS Distributions 	kr = mach_vm_deallocate(kernel_map, buffer, bufSize);
328*bbb1b6f9SApple OSS Distributions 	assert(KERN_SUCCESS == kr);
329*bbb1b6f9SApple OSS Distributions 	OSSafeReleaseNULL(mapper);
330*bbb1b6f9SApple OSS Distributions 
331*bbb1b6f9SApple OSS Distributions 	return 0;
332*bbb1b6f9SApple OSS Distributions }
333*bbb1b6f9SApple OSS Distributions 
334*bbb1b6f9SApple OSS Distributions // <rdar://problem/30102458>
335*bbb1b6f9SApple OSS Distributions static int
IOMemoryRemoteTest(int newValue)336*bbb1b6f9SApple OSS Distributions IOMemoryRemoteTest(int newValue)
337*bbb1b6f9SApple OSS Distributions {
338*bbb1b6f9SApple OSS Distributions 	IOReturn             ret;
339*bbb1b6f9SApple OSS Distributions 	IOMemoryDescriptor * md;
340*bbb1b6f9SApple OSS Distributions 	IOByteCount          offset, length;
341*bbb1b6f9SApple OSS Distributions 	addr64_t             addr;
342*bbb1b6f9SApple OSS Distributions 	uint32_t             idx;
343*bbb1b6f9SApple OSS Distributions 
344*bbb1b6f9SApple OSS Distributions 	IODMACommand       * dma;
345*bbb1b6f9SApple OSS Distributions 	IODMACommand::SegmentOptions segOptions =
346*bbb1b6f9SApple OSS Distributions 	{
347*bbb1b6f9SApple OSS Distributions 		.fStructSize      = sizeof(segOptions),
348*bbb1b6f9SApple OSS Distributions 		.fNumAddressBits  = 64,
349*bbb1b6f9SApple OSS Distributions 		.fMaxSegmentSize  = 0x2000,
350*bbb1b6f9SApple OSS Distributions 		.fMaxTransferSize = 128 * 1024,
351*bbb1b6f9SApple OSS Distributions 		.fAlignment       = 1,
352*bbb1b6f9SApple OSS Distributions 		.fAlignmentLength = 1,
353*bbb1b6f9SApple OSS Distributions 		.fAlignmentInternalSegments = 1
354*bbb1b6f9SApple OSS Distributions 	};
355*bbb1b6f9SApple OSS Distributions 	IODMACommand::Segment64 segments[1];
356*bbb1b6f9SApple OSS Distributions 	UInt32                  numSegments;
357*bbb1b6f9SApple OSS Distributions 	UInt64                  dmaOffset;
358*bbb1b6f9SApple OSS Distributions 
359*bbb1b6f9SApple OSS Distributions 	IOAddressRange ranges[2] = {
360*bbb1b6f9SApple OSS Distributions 		{ 0x1234567890123456ULL, 0x1000 }, { 0x5432109876543210, 0x2000 },
361*bbb1b6f9SApple OSS Distributions 	};
362*bbb1b6f9SApple OSS Distributions 
363*bbb1b6f9SApple OSS Distributions 	md = IOMemoryDescriptor::withAddressRanges(&ranges[0], 2, kIODirectionOutIn | kIOMemoryRemote, TASK_NULL);
364*bbb1b6f9SApple OSS Distributions 	assert(md);
365*bbb1b6f9SApple OSS Distributions 
366*bbb1b6f9SApple OSS Distributions //    md->map();
367*bbb1b6f9SApple OSS Distributions //    md->readBytes(0, &idx, sizeof(idx));
368*bbb1b6f9SApple OSS Distributions 
369*bbb1b6f9SApple OSS Distributions 	ret = md->prepare(kIODirectionOutIn);
370*bbb1b6f9SApple OSS Distributions 	assert(kIOReturnSuccess == ret);
371*bbb1b6f9SApple OSS Distributions 
372*bbb1b6f9SApple OSS Distributions 	printf("remote md flags 0x%qx, r %d\n",
373*bbb1b6f9SApple OSS Distributions 	    md->getFlags(), (0 != (kIOMemoryRemote & md->getFlags())));
374*bbb1b6f9SApple OSS Distributions 
375*bbb1b6f9SApple OSS Distributions 	for (offset = 0, idx = 0; true; offset += length, idx++) {
376*bbb1b6f9SApple OSS Distributions 		addr = md->getPhysicalSegment(offset, &length, 0);
377*bbb1b6f9SApple OSS Distributions 		if (!length) {
378*bbb1b6f9SApple OSS Distributions 			break;
379*bbb1b6f9SApple OSS Distributions 		}
380*bbb1b6f9SApple OSS Distributions 		assert(idx < 2);
381*bbb1b6f9SApple OSS Distributions 		assert(addr == ranges[idx].address);
382*bbb1b6f9SApple OSS Distributions 		assert(length == ranges[idx].length);
383*bbb1b6f9SApple OSS Distributions 	}
384*bbb1b6f9SApple OSS Distributions 	assert(offset == md->getLength());
385*bbb1b6f9SApple OSS Distributions 
386*bbb1b6f9SApple OSS Distributions 	dma = IODMACommand::withSpecification(kIODMACommandOutputHost64, &segOptions,
387*bbb1b6f9SApple OSS Distributions 	    kIODMAMapOptionUnmapped | kIODMAMapOptionIterateOnly,
388*bbb1b6f9SApple OSS Distributions 	    NULL, NULL);
389*bbb1b6f9SApple OSS Distributions 	assert(dma);
390*bbb1b6f9SApple OSS Distributions 	ret = dma->setMemoryDescriptor(md, true);
391*bbb1b6f9SApple OSS Distributions 	assert(kIOReturnSuccess == ret);
392*bbb1b6f9SApple OSS Distributions 
393*bbb1b6f9SApple OSS Distributions 	for (dmaOffset = 0, idx = 0; dmaOffset < md->getLength(); idx++) {
394*bbb1b6f9SApple OSS Distributions 		numSegments = 1;
395*bbb1b6f9SApple OSS Distributions 		ret = dma->gen64IOVMSegments(&dmaOffset, &segments[0], &numSegments);
396*bbb1b6f9SApple OSS Distributions 		assert(kIOReturnSuccess == ret);
397*bbb1b6f9SApple OSS Distributions 		assert(1 == numSegments);
398*bbb1b6f9SApple OSS Distributions 		assert(idx < 2);
399*bbb1b6f9SApple OSS Distributions 		assert(segments[0].fIOVMAddr == ranges[idx].address);
400*bbb1b6f9SApple OSS Distributions 		assert(segments[0].fLength == ranges[idx].length);
401*bbb1b6f9SApple OSS Distributions 	}
402*bbb1b6f9SApple OSS Distributions 	assert(dmaOffset == md->getLength());
403*bbb1b6f9SApple OSS Distributions 
404*bbb1b6f9SApple OSS Distributions 	ret = dma->clearMemoryDescriptor(true);
405*bbb1b6f9SApple OSS Distributions 	assert(kIOReturnSuccess == ret);
406*bbb1b6f9SApple OSS Distributions 	dma->release();
407*bbb1b6f9SApple OSS Distributions 	md->complete(kIODirectionOutIn);
408*bbb1b6f9SApple OSS Distributions 	md->release();
409*bbb1b6f9SApple OSS Distributions 
410*bbb1b6f9SApple OSS Distributions 	return 0;
411*bbb1b6f9SApple OSS Distributions }
412*bbb1b6f9SApple OSS Distributions 
413*bbb1b6f9SApple OSS Distributions static IOReturn
IOMemoryPrefaultTest(uint32_t options)414*bbb1b6f9SApple OSS Distributions IOMemoryPrefaultTest(uint32_t options)
415*bbb1b6f9SApple OSS Distributions {
416*bbb1b6f9SApple OSS Distributions 	IOBufferMemoryDescriptor * bmd;
417*bbb1b6f9SApple OSS Distributions 	IOMemoryMap              * map;
418*bbb1b6f9SApple OSS Distributions 	IOReturn       kr;
419*bbb1b6f9SApple OSS Distributions 	uint32_t       data;
420*bbb1b6f9SApple OSS Distributions 	uint32_t *     p;
421*bbb1b6f9SApple OSS Distributions 	IOSimpleLock * lock;
422*bbb1b6f9SApple OSS Distributions 
423*bbb1b6f9SApple OSS Distributions 	lock = IOSimpleLockAlloc();
424*bbb1b6f9SApple OSS Distributions 	assert(lock);
425*bbb1b6f9SApple OSS Distributions 
426*bbb1b6f9SApple OSS Distributions 	bmd = IOBufferMemoryDescriptor::inTaskWithOptions(current_task(),
427*bbb1b6f9SApple OSS Distributions 	    kIODirectionOutIn | kIOMemoryPageable, ptoa(8));
428*bbb1b6f9SApple OSS Distributions 	assert(bmd);
429*bbb1b6f9SApple OSS Distributions 	kr = bmd->prepare();
430*bbb1b6f9SApple OSS Distributions 	assert(KERN_SUCCESS == kr);
431*bbb1b6f9SApple OSS Distributions 
432*bbb1b6f9SApple OSS Distributions 	map = bmd->map(kIOMapPrefault);
433*bbb1b6f9SApple OSS Distributions 	assert(map);
434*bbb1b6f9SApple OSS Distributions 
435*bbb1b6f9SApple OSS Distributions 	p = (typeof(p))map->getVirtualAddress();
436*bbb1b6f9SApple OSS Distributions 	IOSimpleLockLock(lock);
437*bbb1b6f9SApple OSS Distributions 	data = p[0];
438*bbb1b6f9SApple OSS Distributions 	IOSimpleLockUnlock(lock);
439*bbb1b6f9SApple OSS Distributions 
440*bbb1b6f9SApple OSS Distributions 	IOLog("IOMemoryPrefaultTest %d\n", data);
441*bbb1b6f9SApple OSS Distributions 
442*bbb1b6f9SApple OSS Distributions 	map->release();
443*bbb1b6f9SApple OSS Distributions 	bmd->release();
444*bbb1b6f9SApple OSS Distributions 	IOSimpleLockFree(lock);
445*bbb1b6f9SApple OSS Distributions 
446*bbb1b6f9SApple OSS Distributions 	return kIOReturnSuccess;
447*bbb1b6f9SApple OSS Distributions }
448*bbb1b6f9SApple OSS Distributions 
449*bbb1b6f9SApple OSS Distributions static IOReturn
IOBMDOverflowTest(uint32_t options)450*bbb1b6f9SApple OSS Distributions IOBMDOverflowTest(uint32_t options)
451*bbb1b6f9SApple OSS Distributions {
452*bbb1b6f9SApple OSS Distributions 	IOBufferMemoryDescriptor * bmd;
453*bbb1b6f9SApple OSS Distributions 
454*bbb1b6f9SApple OSS Distributions 	bmd = IOBufferMemoryDescriptor::inTaskWithPhysicalMask(kernel_task, kIOMemoryPageable | kIODirectionOut,
455*bbb1b6f9SApple OSS Distributions 	    0xffffffffffffffff, 0);
456*bbb1b6f9SApple OSS Distributions 	assert(NULL == bmd);
457*bbb1b6f9SApple OSS Distributions 
458*bbb1b6f9SApple OSS Distributions 	return kIOReturnSuccess;
459*bbb1b6f9SApple OSS Distributions }
460*bbb1b6f9SApple OSS Distributions 
461*bbb1b6f9SApple OSS Distributions static IOReturn
IOBMDSetLengthMapTest(uint32_t options)462*bbb1b6f9SApple OSS Distributions IOBMDSetLengthMapTest(uint32_t options)
463*bbb1b6f9SApple OSS Distributions {
464*bbb1b6f9SApple OSS Distributions 	IOBufferMemoryDescriptor * bmd;
465*bbb1b6f9SApple OSS Distributions 	IOMemoryMap * map;
466*bbb1b6f9SApple OSS Distributions 
467*bbb1b6f9SApple OSS Distributions 	bmd = IOBufferMemoryDescriptor::inTaskWithOptions(
468*bbb1b6f9SApple OSS Distributions 		kernel_task, kIOMemoryDirectionOutIn | kIOMemoryKernelUserShared, 0x4000, 0x4000);
469*bbb1b6f9SApple OSS Distributions 	assert(bmd);
470*bbb1b6f9SApple OSS Distributions 
471*bbb1b6f9SApple OSS Distributions 	bmd->setLength(0x100);
472*bbb1b6f9SApple OSS Distributions 	map = bmd->createMappingInTask(current_task(), 0, kIOMapAnywhere, 0, 0);
473*bbb1b6f9SApple OSS Distributions 	assert(map);
474*bbb1b6f9SApple OSS Distributions 	OSSafeReleaseNULL(map);
475*bbb1b6f9SApple OSS Distributions 
476*bbb1b6f9SApple OSS Distributions 	bmd->setLength(0x200);
477*bbb1b6f9SApple OSS Distributions 	map = bmd->createMappingInTask(current_task(), 0, kIOMapAnywhere, 0, 0);
478*bbb1b6f9SApple OSS Distributions 	assert(map);
479*bbb1b6f9SApple OSS Distributions 	OSSafeReleaseNULL(map);
480*bbb1b6f9SApple OSS Distributions 
481*bbb1b6f9SApple OSS Distributions 	bmd->release();
482*bbb1b6f9SApple OSS Distributions 
483*bbb1b6f9SApple OSS Distributions 	return kIOReturnSuccess;
484*bbb1b6f9SApple OSS Distributions }
485*bbb1b6f9SApple OSS Distributions 
486*bbb1b6f9SApple OSS Distributions // <rdar://problem/26375234>
487*bbb1b6f9SApple OSS Distributions static IOReturn
ZeroLengthTest(int newValue)488*bbb1b6f9SApple OSS Distributions ZeroLengthTest(int newValue)
489*bbb1b6f9SApple OSS Distributions {
490*bbb1b6f9SApple OSS Distributions 	IOMemoryDescriptor * md;
491*bbb1b6f9SApple OSS Distributions 
492*bbb1b6f9SApple OSS Distributions 	md = IOMemoryDescriptor::withAddressRange(
493*bbb1b6f9SApple OSS Distributions 		0, 0, kIODirectionNone, current_task());
494*bbb1b6f9SApple OSS Distributions 	assert(md);
495*bbb1b6f9SApple OSS Distributions 	md->prepare();
496*bbb1b6f9SApple OSS Distributions 	md->complete();
497*bbb1b6f9SApple OSS Distributions 	md->release();
498*bbb1b6f9SApple OSS Distributions 	return 0;
499*bbb1b6f9SApple OSS Distributions }
500*bbb1b6f9SApple OSS Distributions 
501*bbb1b6f9SApple OSS Distributions // <rdar://problem/27002624>
502*bbb1b6f9SApple OSS Distributions static IOReturn
BadFixedAllocTest(int newValue)503*bbb1b6f9SApple OSS Distributions BadFixedAllocTest(int newValue)
504*bbb1b6f9SApple OSS Distributions {
505*bbb1b6f9SApple OSS Distributions 	IOBufferMemoryDescriptor * bmd;
506*bbb1b6f9SApple OSS Distributions 	IOMemoryMap              * map;
507*bbb1b6f9SApple OSS Distributions 
508*bbb1b6f9SApple OSS Distributions 	bmd = IOBufferMemoryDescriptor::inTaskWithOptions(NULL,
509*bbb1b6f9SApple OSS Distributions 	    kIODirectionIn | kIOMemoryPageable, ptoa(1));
510*bbb1b6f9SApple OSS Distributions 	assert(bmd);
511*bbb1b6f9SApple OSS Distributions 	map = bmd->createMappingInTask(kernel_task, 0x2000, 0);
512*bbb1b6f9SApple OSS Distributions 	assert(!map);
513*bbb1b6f9SApple OSS Distributions 
514*bbb1b6f9SApple OSS Distributions 	bmd->release();
515*bbb1b6f9SApple OSS Distributions 	return 0;
516*bbb1b6f9SApple OSS Distributions }
517*bbb1b6f9SApple OSS Distributions 
518*bbb1b6f9SApple OSS Distributions // <rdar://problem/26466423>
519*bbb1b6f9SApple OSS Distributions static IOReturn
IODirectionPrepareNoZeroFillTest(int newValue)520*bbb1b6f9SApple OSS Distributions IODirectionPrepareNoZeroFillTest(int newValue)
521*bbb1b6f9SApple OSS Distributions {
522*bbb1b6f9SApple OSS Distributions 	IOBufferMemoryDescriptor * bmd;
523*bbb1b6f9SApple OSS Distributions 
524*bbb1b6f9SApple OSS Distributions 	bmd = IOBufferMemoryDescriptor::inTaskWithOptions(NULL,
525*bbb1b6f9SApple OSS Distributions 	    kIODirectionIn | kIOMemoryPageable, ptoa(24));
526*bbb1b6f9SApple OSS Distributions 	assert(bmd);
527*bbb1b6f9SApple OSS Distributions 	bmd->prepare((IODirection)(kIODirectionIn | kIODirectionPrepareNoZeroFill));
528*bbb1b6f9SApple OSS Distributions 	bmd->prepare(kIODirectionIn);
529*bbb1b6f9SApple OSS Distributions 	bmd->complete((IODirection)(kIODirectionIn | kIODirectionCompleteWithDataValid));
530*bbb1b6f9SApple OSS Distributions 	bmd->complete(kIODirectionIn);
531*bbb1b6f9SApple OSS Distributions 	bmd->release();
532*bbb1b6f9SApple OSS Distributions 	return 0;
533*bbb1b6f9SApple OSS Distributions }
534*bbb1b6f9SApple OSS Distributions 
535*bbb1b6f9SApple OSS Distributions // <rdar://problem/28190483>
536*bbb1b6f9SApple OSS Distributions static IOReturn
IOMemoryMapTest(uint32_t options)537*bbb1b6f9SApple OSS Distributions IOMemoryMapTest(uint32_t options)
538*bbb1b6f9SApple OSS Distributions {
539*bbb1b6f9SApple OSS Distributions 	IOBufferMemoryDescriptor * bmd;
540*bbb1b6f9SApple OSS Distributions 	IOMemoryDescriptor       * md;
541*bbb1b6f9SApple OSS Distributions 	IOMemoryMap              * map;
542*bbb1b6f9SApple OSS Distributions 	uint32_t    data;
543*bbb1b6f9SApple OSS Distributions 	user_addr_t p;
544*bbb1b6f9SApple OSS Distributions 	uint8_t *   p2;
545*bbb1b6f9SApple OSS Distributions 	int         r;
546*bbb1b6f9SApple OSS Distributions 	uint64_t    time, nano;
547*bbb1b6f9SApple OSS Distributions 
548*bbb1b6f9SApple OSS Distributions 	bmd = IOBufferMemoryDescriptor::inTaskWithOptions(current_task(),
549*bbb1b6f9SApple OSS Distributions 	    kIODirectionOutIn | kIOMemoryPageable, 0x4018 + 0x800);
550*bbb1b6f9SApple OSS Distributions 	assert(bmd);
551*bbb1b6f9SApple OSS Distributions 	p = (typeof(p))bmd->getBytesNoCopy();
552*bbb1b6f9SApple OSS Distributions 	p += 0x800;
553*bbb1b6f9SApple OSS Distributions 	data = 0x11111111;
554*bbb1b6f9SApple OSS Distributions 	r = copyout(&data, p, sizeof(data));
555*bbb1b6f9SApple OSS Distributions 	assert(r == 0);
556*bbb1b6f9SApple OSS Distributions 	data = 0x22222222;
557*bbb1b6f9SApple OSS Distributions 	r = copyout(&data, p + 0x1000, sizeof(data));
558*bbb1b6f9SApple OSS Distributions 	assert(r == 0);
559*bbb1b6f9SApple OSS Distributions 	data = 0x33333333;
560*bbb1b6f9SApple OSS Distributions 	r = copyout(&data, p + 0x2000, sizeof(data));
561*bbb1b6f9SApple OSS Distributions 	assert(r == 0);
562*bbb1b6f9SApple OSS Distributions 	data = 0x44444444;
563*bbb1b6f9SApple OSS Distributions 	r = copyout(&data, p + 0x3000, sizeof(data));
564*bbb1b6f9SApple OSS Distributions 	assert(r == 0);
565*bbb1b6f9SApple OSS Distributions 
566*bbb1b6f9SApple OSS Distributions 	md = IOMemoryDescriptor::withAddressRange(p, 0x4018,
567*bbb1b6f9SApple OSS Distributions 	    kIODirectionOut | options,
568*bbb1b6f9SApple OSS Distributions 	    current_task());
569*bbb1b6f9SApple OSS Distributions 	assert(md);
570*bbb1b6f9SApple OSS Distributions 	time = mach_absolute_time();
571*bbb1b6f9SApple OSS Distributions 	map = md->map(kIOMapReadOnly);
572*bbb1b6f9SApple OSS Distributions 	time = mach_absolute_time() - time;
573*bbb1b6f9SApple OSS Distributions 	assert(map);
574*bbb1b6f9SApple OSS Distributions 	absolutetime_to_nanoseconds(time, &nano);
575*bbb1b6f9SApple OSS Distributions 
576*bbb1b6f9SApple OSS Distributions 	p2 = (typeof(p2))map->getVirtualAddress();
577*bbb1b6f9SApple OSS Distributions 	assert(0x11 == p2[0]);
578*bbb1b6f9SApple OSS Distributions 	assert(0x22 == p2[0x1000]);
579*bbb1b6f9SApple OSS Distributions 	assert(0x33 == p2[0x2000]);
580*bbb1b6f9SApple OSS Distributions 	assert(0x44 == p2[0x3000]);
581*bbb1b6f9SApple OSS Distributions 
582*bbb1b6f9SApple OSS Distributions 	data = 0x99999999;
583*bbb1b6f9SApple OSS Distributions 	r = copyout(&data, p + 0x2000, sizeof(data));
584*bbb1b6f9SApple OSS Distributions 	assert(r == 0);
585*bbb1b6f9SApple OSS Distributions 
586*bbb1b6f9SApple OSS Distributions 	assert(0x11 == p2[0]);
587*bbb1b6f9SApple OSS Distributions 	assert(0x22 == p2[0x1000]);
588*bbb1b6f9SApple OSS Distributions 	assert(0x44 == p2[0x3000]);
589*bbb1b6f9SApple OSS Distributions 	if (kIOMemoryMapCopyOnWrite & options) {
590*bbb1b6f9SApple OSS Distributions 		assert(0x33 == p2[0x2000]);
591*bbb1b6f9SApple OSS Distributions 	} else {
592*bbb1b6f9SApple OSS Distributions 		assert(0x99 == p2[0x2000]);
593*bbb1b6f9SApple OSS Distributions 	}
594*bbb1b6f9SApple OSS Distributions 
595*bbb1b6f9SApple OSS Distributions 	IOLog("IOMemoryMapCopyOnWriteTest map(%s) %lld ns\n",
596*bbb1b6f9SApple OSS Distributions 	    kIOMemoryMapCopyOnWrite & options ? "kIOMemoryMapCopyOnWrite" : "",
597*bbb1b6f9SApple OSS Distributions 	    nano);
598*bbb1b6f9SApple OSS Distributions 
599*bbb1b6f9SApple OSS Distributions 	map->release();
600*bbb1b6f9SApple OSS Distributions 	md->release();
601*bbb1b6f9SApple OSS Distributions 	bmd->release();
602*bbb1b6f9SApple OSS Distributions 
603*bbb1b6f9SApple OSS Distributions 	return kIOReturnSuccess;
604*bbb1b6f9SApple OSS Distributions }
605*bbb1b6f9SApple OSS Distributions 
606*bbb1b6f9SApple OSS Distributions static int
IOMemoryMapCopyOnWriteTest(int newValue)607*bbb1b6f9SApple OSS Distributions IOMemoryMapCopyOnWriteTest(int newValue)
608*bbb1b6f9SApple OSS Distributions {
609*bbb1b6f9SApple OSS Distributions 	IOMemoryMapTest(0);
610*bbb1b6f9SApple OSS Distributions 	IOMemoryMapTest(kIOMemoryMapCopyOnWrite);
611*bbb1b6f9SApple OSS Distributions 	return 0;
612*bbb1b6f9SApple OSS Distributions }
613*bbb1b6f9SApple OSS Distributions 
614*bbb1b6f9SApple OSS Distributions static int
AllocationNameTest(int newValue)615*bbb1b6f9SApple OSS Distributions AllocationNameTest(int newValue)
616*bbb1b6f9SApple OSS Distributions {
617*bbb1b6f9SApple OSS Distributions 	IOMemoryDescriptor * bmd;
618*bbb1b6f9SApple OSS Distributions 	kern_allocation_name_t name, prior;
619*bbb1b6f9SApple OSS Distributions 
620*bbb1b6f9SApple OSS Distributions 	name = kern_allocation_name_allocate("com.apple.iokit.test", 0);
621*bbb1b6f9SApple OSS Distributions 	assert(name);
622*bbb1b6f9SApple OSS Distributions 
623*bbb1b6f9SApple OSS Distributions 	prior = thread_set_allocation_name(name);
624*bbb1b6f9SApple OSS Distributions 
625*bbb1b6f9SApple OSS Distributions 	bmd = IOBufferMemoryDescriptor::inTaskWithOptions(TASK_NULL,
626*bbb1b6f9SApple OSS Distributions 	    kIODirectionOutIn | kIOMemoryPageable | kIOMemoryKernelUserShared,
627*bbb1b6f9SApple OSS Distributions 	    ptoa(13));
628*bbb1b6f9SApple OSS Distributions 	assert(bmd);
629*bbb1b6f9SApple OSS Distributions 	bmd->prepare();
630*bbb1b6f9SApple OSS Distributions 
631*bbb1b6f9SApple OSS Distributions 	thread_set_allocation_name(prior);
632*bbb1b6f9SApple OSS Distributions 	kern_allocation_name_release(name);
633*bbb1b6f9SApple OSS Distributions 
634*bbb1b6f9SApple OSS Distributions 	if (newValue != 7) {
635*bbb1b6f9SApple OSS Distributions 		bmd->release();
636*bbb1b6f9SApple OSS Distributions 	}
637*bbb1b6f9SApple OSS Distributions 
638*bbb1b6f9SApple OSS Distributions 	return 0;
639*bbb1b6f9SApple OSS Distributions }
640*bbb1b6f9SApple OSS Distributions 
641*bbb1b6f9SApple OSS Distributions static IOReturn
IOGuardPageMDTest(int newValue)642*bbb1b6f9SApple OSS Distributions IOGuardPageMDTest(int newValue)
643*bbb1b6f9SApple OSS Distributions {
644*bbb1b6f9SApple OSS Distributions 	constexpr size_t MAX_LEFT_GUARD_PAGES = 5;
645*bbb1b6f9SApple OSS Distributions 	constexpr size_t MAX_RIGHT_GUARD_PAGES = 5;
646*bbb1b6f9SApple OSS Distributions 
647*bbb1b6f9SApple OSS Distributions 	IOMemoryDescriptor * mds[3];
648*bbb1b6f9SApple OSS Distributions 	IOMemoryDescriptor * dataMD;
649*bbb1b6f9SApple OSS Distributions 	IOMultiMemoryDescriptor * mmd;
650*bbb1b6f9SApple OSS Distributions 	IOBufferMemoryDescriptor * iobmd;
651*bbb1b6f9SApple OSS Distributions 	IOMemoryMap * map;
652*bbb1b6f9SApple OSS Distributions 	void * addr;
653*bbb1b6f9SApple OSS Distributions 	uint8_t * data;
654*bbb1b6f9SApple OSS Distributions 	uint32_t i;
655*bbb1b6f9SApple OSS Distributions 
656*bbb1b6f9SApple OSS Distributions 	data = (typeof(data))IOMallocAligned(page_size, page_size);
657*bbb1b6f9SApple OSS Distributions 	for (i = 0; i < page_size; i++) {
658*bbb1b6f9SApple OSS Distributions 		data[i] = (uint8_t)(i & 0xFF);
659*bbb1b6f9SApple OSS Distributions 	}
660*bbb1b6f9SApple OSS Distributions 
661*bbb1b6f9SApple OSS Distributions 	dataMD = IOMemoryDescriptor::withAddressRange((mach_vm_address_t) data, page_size, kIODirectionOutIn, kernel_task);
662*bbb1b6f9SApple OSS Distributions 	assert(dataMD);
663*bbb1b6f9SApple OSS Distributions 
664*bbb1b6f9SApple OSS Distributions 
665*bbb1b6f9SApple OSS Distributions 	for (size_t leftGuardSize = 1; leftGuardSize < MAX_LEFT_GUARD_PAGES; leftGuardSize++) {
666*bbb1b6f9SApple OSS Distributions 		for (size_t rightGuardSize = 1; rightGuardSize < MAX_RIGHT_GUARD_PAGES; rightGuardSize++) {
667*bbb1b6f9SApple OSS Distributions 			mds[0] = IOGuardPageMemoryDescriptor::withSize(page_size * leftGuardSize);
668*bbb1b6f9SApple OSS Distributions 			assert(mds[0]);
669*bbb1b6f9SApple OSS Distributions 
670*bbb1b6f9SApple OSS Distributions 			mds[1] = dataMD;
671*bbb1b6f9SApple OSS Distributions 			mds[1]->retain();
672*bbb1b6f9SApple OSS Distributions 
673*bbb1b6f9SApple OSS Distributions 			mds[2] = IOGuardPageMemoryDescriptor::withSize(page_size * rightGuardSize);
674*bbb1b6f9SApple OSS Distributions 			assert(mds[2]);
675*bbb1b6f9SApple OSS Distributions 
676*bbb1b6f9SApple OSS Distributions 			mmd = IOMultiMemoryDescriptor::withDescriptors(&mds[0], sizeof(mds) / sizeof(mds[0]), kIODirectionOutIn, false);
677*bbb1b6f9SApple OSS Distributions 
678*bbb1b6f9SApple OSS Distributions 			OSSafeReleaseNULL(mds[2]);
679*bbb1b6f9SApple OSS Distributions 			OSSafeReleaseNULL(mds[1]);
680*bbb1b6f9SApple OSS Distributions 			OSSafeReleaseNULL(mds[0]);
681*bbb1b6f9SApple OSS Distributions 
682*bbb1b6f9SApple OSS Distributions 			map = mmd->createMappingInTask(kernel_task, 0, kIOMapAnywhere, 0, mmd->getLength());
683*bbb1b6f9SApple OSS Distributions 
684*bbb1b6f9SApple OSS Distributions 			OSSafeReleaseNULL(mmd);
685*bbb1b6f9SApple OSS Distributions 			assert(map);
686*bbb1b6f9SApple OSS Distributions 			addr = (void *)map->getAddress();
687*bbb1b6f9SApple OSS Distributions 
688*bbb1b6f9SApple OSS Distributions 			// check data
689*bbb1b6f9SApple OSS Distributions 			for (i = 0; i < page_size; i++) {
690*bbb1b6f9SApple OSS Distributions 				assert(*(uint8_t *)((uintptr_t)addr + page_size * leftGuardSize + i) == (uint8_t)(i & 0xFF));
691*bbb1b6f9SApple OSS Distributions 			}
692*bbb1b6f9SApple OSS Distributions 
693*bbb1b6f9SApple OSS Distributions 			// check map length
694*bbb1b6f9SApple OSS Distributions 			assert(page_size * leftGuardSize + page_size + page_size * rightGuardSize == map->getLength());
695*bbb1b6f9SApple OSS Distributions 
696*bbb1b6f9SApple OSS Distributions 			// check page protections
697*bbb1b6f9SApple OSS Distributions 			for (i = 0; i < leftGuardSize + 1 + rightGuardSize; i++) {
698*bbb1b6f9SApple OSS Distributions 				mach_vm_address_t regionAddr = (vm_address_t)addr + i * page_size;
699*bbb1b6f9SApple OSS Distributions 				mach_vm_size_t regionSize;
700*bbb1b6f9SApple OSS Distributions 				vm_region_extended_info regionInfo;
701*bbb1b6f9SApple OSS Distributions 				mach_msg_type_number_t count = VM_REGION_EXTENDED_INFO_COUNT;
702*bbb1b6f9SApple OSS Distributions 				mach_port_t unused;
703*bbb1b6f9SApple OSS Distributions 				kern_return_t kr = mach_vm_region(kernel_map, &regionAddr, &regionSize, VM_REGION_EXTENDED_INFO, (vm_region_info_t)&regionInfo, &count, &unused);
704*bbb1b6f9SApple OSS Distributions 				assert(kr == KERN_SUCCESS);
705*bbb1b6f9SApple OSS Distributions 				if (i < leftGuardSize || i > leftGuardSize + 1) {
706*bbb1b6f9SApple OSS Distributions 					assert(regionInfo.protection == VM_PROT_NONE);
707*bbb1b6f9SApple OSS Distributions 				}
708*bbb1b6f9SApple OSS Distributions 			}
709*bbb1b6f9SApple OSS Distributions 			OSSafeReleaseNULL(map);
710*bbb1b6f9SApple OSS Distributions 		}
711*bbb1b6f9SApple OSS Distributions 	}
712*bbb1b6f9SApple OSS Distributions 
713*bbb1b6f9SApple OSS Distributions 	OSSafeReleaseNULL(dataMD);
714*bbb1b6f9SApple OSS Distributions 	IOFreeAligned(data, page_size);
715*bbb1b6f9SApple OSS Distributions 
716*bbb1b6f9SApple OSS Distributions 	for (size_t iobmdCapacity = page_size / 8; iobmdCapacity < page_size * 10; iobmdCapacity += page_size / 8) {
717*bbb1b6f9SApple OSS Distributions 		iobmd = IOBufferMemoryDescriptor::inTaskWithGuardPages(kernel_task, kIODirectionOutIn, iobmdCapacity);
718*bbb1b6f9SApple OSS Distributions 
719*bbb1b6f9SApple OSS Distributions 		// Capacity should be rounded up to page size
720*bbb1b6f9SApple OSS Distributions 		assert(iobmd->getLength() == round_page(iobmdCapacity));
721*bbb1b6f9SApple OSS Distributions 
722*bbb1b6f9SApple OSS Distributions 		// Buffer should be page aligned
723*bbb1b6f9SApple OSS Distributions 		addr = iobmd->getBytesNoCopy();
724*bbb1b6f9SApple OSS Distributions 		assert((vm_offset_t)addr == round_page((vm_offset_t)addr));
725*bbb1b6f9SApple OSS Distributions 
726*bbb1b6f9SApple OSS Distributions 		// fill buffer
727*bbb1b6f9SApple OSS Distributions 		for (size_t i = 0; i < iobmdCapacity; i++) {
728*bbb1b6f9SApple OSS Distributions 			*((char *)addr + i) = (char)(i & 0xFF);
729*bbb1b6f9SApple OSS Distributions 		}
730*bbb1b6f9SApple OSS Distributions 
731*bbb1b6f9SApple OSS Distributions 		map = iobmd->createMappingInTask(kernel_task, 0, kIOMapAnywhere | kIOMapUnique, 0, iobmd->getLength());
732*bbb1b6f9SApple OSS Distributions 		assert(map->getLength() == iobmd->getLength());
733*bbb1b6f9SApple OSS Distributions 
734*bbb1b6f9SApple OSS Distributions 		// check buffer
735*bbb1b6f9SApple OSS Distributions 		for (size_t i = 0; i < iobmdCapacity; i++) {
736*bbb1b6f9SApple OSS Distributions 			assert(*((char *)map->getAddress() + i) == (char)(i & 0xFF));
737*bbb1b6f9SApple OSS Distributions 		}
738*bbb1b6f9SApple OSS Distributions 
739*bbb1b6f9SApple OSS Distributions 		OSSafeReleaseNULL(map);
740*bbb1b6f9SApple OSS Distributions 		OSSafeReleaseNULL(iobmd);
741*bbb1b6f9SApple OSS Distributions 	}
742*bbb1b6f9SApple OSS Distributions 
743*bbb1b6f9SApple OSS Distributions 	return kIOReturnSuccess;
744*bbb1b6f9SApple OSS Distributions }
745*bbb1b6f9SApple OSS Distributions 
746*bbb1b6f9SApple OSS Distributions static IOReturn
IOMDContextTest(int newValue)747*bbb1b6f9SApple OSS Distributions IOMDContextTest(int newValue)
748*bbb1b6f9SApple OSS Distributions {
749*bbb1b6f9SApple OSS Distributions 	IOBufferMemoryDescriptor * bmd = IOBufferMemoryDescriptor::inTaskWithOptions(TASK_NULL,
750*bbb1b6f9SApple OSS Distributions 	    kIODirectionOutIn | kIOMemoryPageable | kIOMemoryKernelUserShared,
751*bbb1b6f9SApple OSS Distributions 	    ptoa(13));
752*bbb1b6f9SApple OSS Distributions 
753*bbb1b6f9SApple OSS Distributions 	OSObject * current = NULL;
754*bbb1b6f9SApple OSS Distributions 	OSString * firstString = OSString::withCStringNoCopy("firstString");
755*bbb1b6f9SApple OSS Distributions 	OSString * secondString = OSString::withCStringNoCopy("secondString");
756*bbb1b6f9SApple OSS Distributions 
757*bbb1b6f9SApple OSS Distributions 	assert(bmd->copyContext() == NULL);
758*bbb1b6f9SApple OSS Distributions 
759*bbb1b6f9SApple OSS Distributions 	bmd->setContext(NULL);
760*bbb1b6f9SApple OSS Distributions 	assert(bmd->copyContext() == NULL);
761*bbb1b6f9SApple OSS Distributions 
762*bbb1b6f9SApple OSS Distributions 	bmd->setContext(firstString);
763*bbb1b6f9SApple OSS Distributions 	current = bmd->copyContext();
764*bbb1b6f9SApple OSS Distributions 	assert(current == firstString);
765*bbb1b6f9SApple OSS Distributions 	OSSafeReleaseNULL(current);
766*bbb1b6f9SApple OSS Distributions 
767*bbb1b6f9SApple OSS Distributions 	bmd->setContext(NULL);
768*bbb1b6f9SApple OSS Distributions 	assert(bmd->copyContext() == NULL);
769*bbb1b6f9SApple OSS Distributions 
770*bbb1b6f9SApple OSS Distributions 	bmd->setContext(secondString);
771*bbb1b6f9SApple OSS Distributions 	current = bmd->copyContext();
772*bbb1b6f9SApple OSS Distributions 	assert(current == secondString);
773*bbb1b6f9SApple OSS Distributions 	OSSafeReleaseNULL(current);
774*bbb1b6f9SApple OSS Distributions 
775*bbb1b6f9SApple OSS Distributions 	bmd->release();
776*bbb1b6f9SApple OSS Distributions 
777*bbb1b6f9SApple OSS Distributions 	assert(firstString->getRetainCount() == 1);
778*bbb1b6f9SApple OSS Distributions 	assert(secondString->getRetainCount() == 1);
779*bbb1b6f9SApple OSS Distributions 
780*bbb1b6f9SApple OSS Distributions 	firstString->release();
781*bbb1b6f9SApple OSS Distributions 	secondString->release();
782*bbb1b6f9SApple OSS Distributions 
783*bbb1b6f9SApple OSS Distributions 	return kIOReturnSuccess;
784*bbb1b6f9SApple OSS Distributions }
785*bbb1b6f9SApple OSS Distributions 
786*bbb1b6f9SApple OSS Distributions int
IOMemoryDescriptorTest(int newValue)787*bbb1b6f9SApple OSS Distributions IOMemoryDescriptorTest(int newValue)
788*bbb1b6f9SApple OSS Distributions {
789*bbb1b6f9SApple OSS Distributions 	int result;
790*bbb1b6f9SApple OSS Distributions 
791*bbb1b6f9SApple OSS Distributions 	IOLog("/IOMemoryDescriptorTest %d\n", (int) gIOMemoryReferenceCount);
792*bbb1b6f9SApple OSS Distributions 
793*bbb1b6f9SApple OSS Distributions #if 0
794*bbb1b6f9SApple OSS Distributions 	if (6 == newValue) {
795*bbb1b6f9SApple OSS Distributions 		IOMemoryDescriptor * sbmds[3];
796*bbb1b6f9SApple OSS Distributions 		IOMultiMemoryDescriptor * smmd;
797*bbb1b6f9SApple OSS Distributions 		IOMemoryDescriptor * mds[2];
798*bbb1b6f9SApple OSS Distributions 		IOMultiMemoryDescriptor * mmd;
799*bbb1b6f9SApple OSS Distributions 		IOMemoryMap * map;
800*bbb1b6f9SApple OSS Distributions 
801*bbb1b6f9SApple OSS Distributions 		sbmds[0] = IOBufferMemoryDescriptor::inTaskWithOptions(kernel_task, kIODirectionOutIn | kIOMemoryKernelUserShared, ptoa(1));
802*bbb1b6f9SApple OSS Distributions 		sbmds[1] = IOBufferMemoryDescriptor::inTaskWithOptions(kernel_task, kIODirectionOutIn | kIOMemoryKernelUserShared, ptoa(2));
803*bbb1b6f9SApple OSS Distributions 		sbmds[2] = IOBufferMemoryDescriptor::inTaskWithOptions(kernel_task, kIODirectionOutIn | kIOMemoryKernelUserShared, ptoa(3));
804*bbb1b6f9SApple OSS Distributions 		smmd = IOMultiMemoryDescriptor::withDescriptors(&sbmds[0], sizeof(sbmds) / sizeof(sbmds[0]), kIODirectionOutIn, false);
805*bbb1b6f9SApple OSS Distributions 
806*bbb1b6f9SApple OSS Distributions 		mds[0] = IOBufferMemoryDescriptor::inTaskWithOptions(kernel_task, kIODirectionOutIn | kIOMemoryKernelUserShared, ptoa(1));
807*bbb1b6f9SApple OSS Distributions 		mds[1] = smmd;
808*bbb1b6f9SApple OSS Distributions 		mmd = IOMultiMemoryDescriptor::withDescriptors(&mds[0], sizeof(mds) / sizeof(mds[0]), kIODirectionOutIn, false);
809*bbb1b6f9SApple OSS Distributions 		map = mmd->createMappingInTask(kernel_task, 0, kIOMapAnywhere | kIOMapGuardedSmall);
810*bbb1b6f9SApple OSS Distributions 		assert(map);
811*bbb1b6f9SApple OSS Distributions 		map->release();
812*bbb1b6f9SApple OSS Distributions 		mmd->release();
813*bbb1b6f9SApple OSS Distributions 		mds[0]->release();
814*bbb1b6f9SApple OSS Distributions 		mds[1]->release();
815*bbb1b6f9SApple OSS Distributions 		sbmds[0]->release();
816*bbb1b6f9SApple OSS Distributions 		sbmds[1]->release();
817*bbb1b6f9SApple OSS Distributions 		sbmds[2]->release();
818*bbb1b6f9SApple OSS Distributions 
819*bbb1b6f9SApple OSS Distributions 		return 0;
820*bbb1b6f9SApple OSS Distributions 	} else if (5 == newValue) {
821*bbb1b6f9SApple OSS Distributions 		IOReturn             ret;
822*bbb1b6f9SApple OSS Distributions 		IOMemoryDescriptor * md;
823*bbb1b6f9SApple OSS Distributions 		IODMACommand       * dma;
824*bbb1b6f9SApple OSS Distributions 		IODMACommand::SegmentOptions segOptions =
825*bbb1b6f9SApple OSS Distributions 		{
826*bbb1b6f9SApple OSS Distributions 			.fStructSize      = sizeof(segOptions),
827*bbb1b6f9SApple OSS Distributions 			.fNumAddressBits  = 64,
828*bbb1b6f9SApple OSS Distributions 			.fMaxSegmentSize  = 4096,
829*bbb1b6f9SApple OSS Distributions 			.fMaxTransferSize = 128 * 1024,
830*bbb1b6f9SApple OSS Distributions 			.fAlignment       = 4,
831*bbb1b6f9SApple OSS Distributions 			.fAlignmentLength = 4,
832*bbb1b6f9SApple OSS Distributions 			.fAlignmentInternalSegments = 0x1000
833*bbb1b6f9SApple OSS Distributions 		};
834*bbb1b6f9SApple OSS Distributions 
835*bbb1b6f9SApple OSS Distributions 		IOAddressRange ranges[3][2] =
836*bbb1b6f9SApple OSS Distributions 		{
837*bbb1b6f9SApple OSS Distributions 			{
838*bbb1b6f9SApple OSS Distributions 				{ (uintptr_t) &IOMemoryDescriptorTest, 0x2ffc },
839*bbb1b6f9SApple OSS Distributions 				{ 0, 0 },
840*bbb1b6f9SApple OSS Distributions 			},
841*bbb1b6f9SApple OSS Distributions 			{
842*bbb1b6f9SApple OSS Distributions 				{ ranges[0][0].address, 0x10 },
843*bbb1b6f9SApple OSS Distributions 				{ 0x3000 + ranges[0][0].address, 0xff0 },
844*bbb1b6f9SApple OSS Distributions 			},
845*bbb1b6f9SApple OSS Distributions 			{
846*bbb1b6f9SApple OSS Distributions 				{ ranges[0][0].address, 0x2ffc },
847*bbb1b6f9SApple OSS Distributions 				{ trunc_page(ranges[0][0].address), 0x800 },
848*bbb1b6f9SApple OSS Distributions 			},
849*bbb1b6f9SApple OSS Distributions 		};
850*bbb1b6f9SApple OSS Distributions 		static const uint32_t rangesCount[3] = { 1, 2, 2 };
851*bbb1b6f9SApple OSS Distributions 		uint32_t test;
852*bbb1b6f9SApple OSS Distributions 
853*bbb1b6f9SApple OSS Distributions 		for (test = 0; test < 3; test++) {
854*bbb1b6f9SApple OSS Distributions 			kprintf("---[%d] address 0x%qx-0x%qx, 0x%qx-0x%qx\n", test,
855*bbb1b6f9SApple OSS Distributions 			    ranges[test][0].address, ranges[test][0].length,
856*bbb1b6f9SApple OSS Distributions 			    ranges[test][1].address, ranges[test][1].length);
857*bbb1b6f9SApple OSS Distributions 
858*bbb1b6f9SApple OSS Distributions 			md = IOMemoryDescriptor::withAddressRanges((IOAddressRange*)&ranges[test][0], rangesCount[test], kIODirectionOut, kernel_task);
859*bbb1b6f9SApple OSS Distributions 			assert(md);
860*bbb1b6f9SApple OSS Distributions 			ret = md->prepare();
861*bbb1b6f9SApple OSS Distributions 			assert(kIOReturnSuccess == ret);
862*bbb1b6f9SApple OSS Distributions 			dma = IODMACommand::withSpecification(kIODMACommandOutputHost64, &segOptions,
863*bbb1b6f9SApple OSS Distributions 			    IODMACommand::kMapped, NULL, NULL);
864*bbb1b6f9SApple OSS Distributions 			assert(dma);
865*bbb1b6f9SApple OSS Distributions 			ret = dma->setMemoryDescriptor(md, true);
866*bbb1b6f9SApple OSS Distributions 			if (kIOReturnSuccess == ret) {
867*bbb1b6f9SApple OSS Distributions 				IODMACommand::Segment64 segments[1];
868*bbb1b6f9SApple OSS Distributions 				UInt32                  numSegments;
869*bbb1b6f9SApple OSS Distributions 				UInt64                  offset;
870*bbb1b6f9SApple OSS Distributions 
871*bbb1b6f9SApple OSS Distributions 				offset = 0;
872*bbb1b6f9SApple OSS Distributions 				do{
873*bbb1b6f9SApple OSS Distributions 					numSegments = 1;
874*bbb1b6f9SApple OSS Distributions 					ret = dma->gen64IOVMSegments(&offset, &segments[0], &numSegments);
875*bbb1b6f9SApple OSS Distributions 					assert(kIOReturnSuccess == ret);
876*bbb1b6f9SApple OSS Distributions 					assert(1 == numSegments);
877*bbb1b6f9SApple OSS Distributions 					kprintf("seg 0x%qx, 0x%qx\n", segments[0].fIOVMAddr, segments[0].fLength);
878*bbb1b6f9SApple OSS Distributions 				}while (offset < md->getLength());
879*bbb1b6f9SApple OSS Distributions 
880*bbb1b6f9SApple OSS Distributions 				ret = dma->clearMemoryDescriptor(true);
881*bbb1b6f9SApple OSS Distributions 				assert(kIOReturnSuccess == ret);
882*bbb1b6f9SApple OSS Distributions 				dma->release();
883*bbb1b6f9SApple OSS Distributions 			}
884*bbb1b6f9SApple OSS Distributions 			md->release();
885*bbb1b6f9SApple OSS Distributions 		}
886*bbb1b6f9SApple OSS Distributions 
887*bbb1b6f9SApple OSS Distributions 		return kIOReturnSuccess;
888*bbb1b6f9SApple OSS Distributions 	} else if (4 == newValue) {
889*bbb1b6f9SApple OSS Distributions 		IOService * isp;
890*bbb1b6f9SApple OSS Distributions 		IOMapper *  mapper;
891*bbb1b6f9SApple OSS Distributions 		IOBufferMemoryDescriptor * md1;
892*bbb1b6f9SApple OSS Distributions 		IODMACommand * dma;
893*bbb1b6f9SApple OSS Distributions 		IOReturn       ret;
894*bbb1b6f9SApple OSS Distributions 		size_t         bufSize = 8192 * 8192 * sizeof(uint32_t);
895*bbb1b6f9SApple OSS Distributions 		uint64_t start, time, nano;
896*bbb1b6f9SApple OSS Distributions 
897*bbb1b6f9SApple OSS Distributions 		isp = IOService::copyMatchingService(IOService::nameMatching("isp"));
898*bbb1b6f9SApple OSS Distributions 		assert(isp);
899*bbb1b6f9SApple OSS Distributions 		mapper = IOMapper::copyMapperForDeviceWithIndex(isp, 0);
900*bbb1b6f9SApple OSS Distributions 		assert(mapper);
901*bbb1b6f9SApple OSS Distributions 
902*bbb1b6f9SApple OSS Distributions 		md1 = IOBufferMemoryDescriptor::inTaskWithOptions(TASK_NULL,
903*bbb1b6f9SApple OSS Distributions 		    kIODirectionOutIn | kIOMemoryPersistent | kIOMemoryPageable,
904*bbb1b6f9SApple OSS Distributions 		    bufSize, page_size);
905*bbb1b6f9SApple OSS Distributions 
906*bbb1b6f9SApple OSS Distributions 		ret = md1->prepare();
907*bbb1b6f9SApple OSS Distributions 		assert(kIOReturnSuccess == ret);
908*bbb1b6f9SApple OSS Distributions 
909*bbb1b6f9SApple OSS Distributions 		IODMAMapSpecification mapSpec;
910*bbb1b6f9SApple OSS Distributions 		bzero(&mapSpec, sizeof(mapSpec));
911*bbb1b6f9SApple OSS Distributions 		uint64_t mapped;
912*bbb1b6f9SApple OSS Distributions 		uint64_t mappedLength;
913*bbb1b6f9SApple OSS Distributions 
914*bbb1b6f9SApple OSS Distributions 		start = mach_absolute_time();
915*bbb1b6f9SApple OSS Distributions 
916*bbb1b6f9SApple OSS Distributions 		ret =  md1->dmaMap(mapper, NULL, &mapSpec, 0, bufSize, &mapped, &mappedLength);
917*bbb1b6f9SApple OSS Distributions 		assert(kIOReturnSuccess == ret);
918*bbb1b6f9SApple OSS Distributions 
919*bbb1b6f9SApple OSS Distributions 		time = mach_absolute_time() - start;
920*bbb1b6f9SApple OSS Distributions 
921*bbb1b6f9SApple OSS Distributions 		absolutetime_to_nanoseconds(time, &nano);
922*bbb1b6f9SApple OSS Distributions 		kprintf("time %lld us\n", nano / 1000ULL);
923*bbb1b6f9SApple OSS Distributions 		kprintf("seg0 0x%qx, 0x%qx\n", mapped, mappedLength);
924*bbb1b6f9SApple OSS Distributions 
925*bbb1b6f9SApple OSS Distributions 		assert(md1);
926*bbb1b6f9SApple OSS Distributions 
927*bbb1b6f9SApple OSS Distributions 		dma = IODMACommand::withSpecification(kIODMACommandOutputHost32,
928*bbb1b6f9SApple OSS Distributions 		    32, 0, IODMACommand::kMapped, 0, 1, mapper, NULL);
929*bbb1b6f9SApple OSS Distributions 
930*bbb1b6f9SApple OSS Distributions 		assert(dma);
931*bbb1b6f9SApple OSS Distributions 
932*bbb1b6f9SApple OSS Distributions 		start = mach_absolute_time();
933*bbb1b6f9SApple OSS Distributions 		ret = dma->setMemoryDescriptor(md1, true);
934*bbb1b6f9SApple OSS Distributions 		assert(kIOReturnSuccess == ret);
935*bbb1b6f9SApple OSS Distributions 		time = mach_absolute_time() - start;
936*bbb1b6f9SApple OSS Distributions 
937*bbb1b6f9SApple OSS Distributions 		absolutetime_to_nanoseconds(time, &nano);
938*bbb1b6f9SApple OSS Distributions 		kprintf("time %lld us\n", nano / 1000ULL);
939*bbb1b6f9SApple OSS Distributions 
940*bbb1b6f9SApple OSS Distributions 
941*bbb1b6f9SApple OSS Distributions 		IODMACommand::Segment32 segments[1];
942*bbb1b6f9SApple OSS Distributions 		UInt32                  numSegments = 1;
943*bbb1b6f9SApple OSS Distributions 		UInt64                  offset;
944*bbb1b6f9SApple OSS Distributions 
945*bbb1b6f9SApple OSS Distributions 		offset = 0;
946*bbb1b6f9SApple OSS Distributions 		ret = dma->gen32IOVMSegments(&offset, &segments[0], &numSegments);
947*bbb1b6f9SApple OSS Distributions 		assert(kIOReturnSuccess == ret);
948*bbb1b6f9SApple OSS Distributions 		assert(1 == numSegments);
949*bbb1b6f9SApple OSS Distributions 		kprintf("seg0 0x%x, 0x%x\n", (int)segments[0].fIOVMAddr, (int)segments[0].fLength);
950*bbb1b6f9SApple OSS Distributions 
951*bbb1b6f9SApple OSS Distributions 		ret = dma->clearMemoryDescriptor(true);
952*bbb1b6f9SApple OSS Distributions 		assert(kIOReturnSuccess == ret);
953*bbb1b6f9SApple OSS Distributions 
954*bbb1b6f9SApple OSS Distributions 		md1->release();
955*bbb1b6f9SApple OSS Distributions 
956*bbb1b6f9SApple OSS Distributions 		return kIOReturnSuccess;
957*bbb1b6f9SApple OSS Distributions 	}
958*bbb1b6f9SApple OSS Distributions 
959*bbb1b6f9SApple OSS Distributions 	if (3 == newValue) {
960*bbb1b6f9SApple OSS Distributions 		IOBufferMemoryDescriptor * md1;
961*bbb1b6f9SApple OSS Distributions 		IOBufferMemoryDescriptor * md2;
962*bbb1b6f9SApple OSS Distributions 		IOMemoryMap * map1;
963*bbb1b6f9SApple OSS Distributions 		IOMemoryMap * map2;
964*bbb1b6f9SApple OSS Distributions 		uint32_t * buf1;
965*bbb1b6f9SApple OSS Distributions 		uint32_t * buf2;
966*bbb1b6f9SApple OSS Distributions 		IOReturn err;
967*bbb1b6f9SApple OSS Distributions 
968*bbb1b6f9SApple OSS Distributions 		md1 = IOBufferMemoryDescriptor::inTaskWithOptions(TASK_NULL,
969*bbb1b6f9SApple OSS Distributions 		    kIODirectionOutIn | kIOMemoryPersistent | kIOMemoryPageable,
970*bbb1b6f9SApple OSS Distributions 		    64 * 1024, page_size);
971*bbb1b6f9SApple OSS Distributions 		assert(md1);
972*bbb1b6f9SApple OSS Distributions 		map1 = md1->createMappingInTask(kernel_task, 0, kIOMapAnywhere | kIOMapUnique);
973*bbb1b6f9SApple OSS Distributions 		assert(map1);
974*bbb1b6f9SApple OSS Distributions 		buf1 = (uint32_t *) map1->getVirtualAddress();
975*bbb1b6f9SApple OSS Distributions 
976*bbb1b6f9SApple OSS Distributions 		md2 = IOBufferMemoryDescriptor::inTaskWithOptions(TASK_NULL,
977*bbb1b6f9SApple OSS Distributions 		    kIODirectionOutIn | kIOMemoryPersistent | kIOMemoryPageable,
978*bbb1b6f9SApple OSS Distributions 		    64 * 1024, page_size);
979*bbb1b6f9SApple OSS Distributions 		assert(md2);
980*bbb1b6f9SApple OSS Distributions 		map2 = md2->createMappingInTask(kernel_task, 0, kIOMapAnywhere | kIOMapUnique);
981*bbb1b6f9SApple OSS Distributions 		assert(map2);
982*bbb1b6f9SApple OSS Distributions 		buf2 = (uint32_t *) map2->getVirtualAddress();
983*bbb1b6f9SApple OSS Distributions 
984*bbb1b6f9SApple OSS Distributions 		memset(buf1, 0x11, 64 * 1024L);
985*bbb1b6f9SApple OSS Distributions 		memset(buf2, 0x22, 64 * 1024L);
986*bbb1b6f9SApple OSS Distributions 
987*bbb1b6f9SApple OSS Distributions 		kprintf("md1 %p, map1 %p, buf2 %p; md2 %p, map2 %p, buf2 %p\n", md1, map1, buf1, md2, map2, buf2);
988*bbb1b6f9SApple OSS Distributions 
989*bbb1b6f9SApple OSS Distributions 		kprintf("no redir 0x%08x, 0x%08x\n", buf1[0], buf2[0]);
990*bbb1b6f9SApple OSS Distributions 		assert(0x11111111 == buf1[0]);
991*bbb1b6f9SApple OSS Distributions 		assert(0x22222222 == buf2[0]);
992*bbb1b6f9SApple OSS Distributions 		err = map1->redirect(md2, 0, 0ULL);
993*bbb1b6f9SApple OSS Distributions 		kprintf("redir md2(0x%x) 0x%08x, 0x%08x\n", err, buf1[0], buf2[0]);
994*bbb1b6f9SApple OSS Distributions 		assert(0x11111111 == buf2[0]);
995*bbb1b6f9SApple OSS Distributions 		assert(0x22222222 == buf1[0]);
996*bbb1b6f9SApple OSS Distributions 		err = map1->redirect(md1, 0, 0ULL);
997*bbb1b6f9SApple OSS Distributions 		kprintf("redir md1(0x%x) 0x%08x, 0x%08x\n", err, buf1[0], buf2[0]);
998*bbb1b6f9SApple OSS Distributions 		assert(0x11111111 == buf1[0]);
999*bbb1b6f9SApple OSS Distributions 		assert(0x22222222 == buf2[0]);
1000*bbb1b6f9SApple OSS Distributions 		map1->release();
1001*bbb1b6f9SApple OSS Distributions 		map2->release();
1002*bbb1b6f9SApple OSS Distributions 		md1->release();
1003*bbb1b6f9SApple OSS Distributions 		md2->release();
1004*bbb1b6f9SApple OSS Distributions 	}
1005*bbb1b6f9SApple OSS Distributions #endif
1006*bbb1b6f9SApple OSS Distributions 
1007*bbb1b6f9SApple OSS Distributions //    result = IODMACommandLocalMappedNonContig(newValue);
1008*bbb1b6f9SApple OSS Distributions //    if (result) return (result);
1009*bbb1b6f9SApple OSS Distributions 
1010*bbb1b6f9SApple OSS Distributions 	result = IODMACommandForceDoubleBufferTest(newValue);
1011*bbb1b6f9SApple OSS Distributions 	if (result) {
1012*bbb1b6f9SApple OSS Distributions 		return result;
1013*bbb1b6f9SApple OSS Distributions 	}
1014*bbb1b6f9SApple OSS Distributions 
1015*bbb1b6f9SApple OSS Distributions 	result = AllocationNameTest(newValue);
1016*bbb1b6f9SApple OSS Distributions 	if (result) {
1017*bbb1b6f9SApple OSS Distributions 		return result;
1018*bbb1b6f9SApple OSS Distributions 	}
1019*bbb1b6f9SApple OSS Distributions 
1020*bbb1b6f9SApple OSS Distributions 	result = IOMemoryMapCopyOnWriteTest(newValue);
1021*bbb1b6f9SApple OSS Distributions 	if (result) {
1022*bbb1b6f9SApple OSS Distributions 		return result;
1023*bbb1b6f9SApple OSS Distributions 	}
1024*bbb1b6f9SApple OSS Distributions 
1025*bbb1b6f9SApple OSS Distributions 	result = IOMultMemoryDescriptorTest(newValue);
1026*bbb1b6f9SApple OSS Distributions 	if (result) {
1027*bbb1b6f9SApple OSS Distributions 		return result;
1028*bbb1b6f9SApple OSS Distributions 	}
1029*bbb1b6f9SApple OSS Distributions 
1030*bbb1b6f9SApple OSS Distributions 	result = IOBMDOverflowTest(newValue);
1031*bbb1b6f9SApple OSS Distributions 	if (result) {
1032*bbb1b6f9SApple OSS Distributions 		return result;
1033*bbb1b6f9SApple OSS Distributions 	}
1034*bbb1b6f9SApple OSS Distributions 
1035*bbb1b6f9SApple OSS Distributions 	result = IOBMDSetLengthMapTest(newValue);
1036*bbb1b6f9SApple OSS Distributions 	if (result) {
1037*bbb1b6f9SApple OSS Distributions 		return result;
1038*bbb1b6f9SApple OSS Distributions 	}
1039*bbb1b6f9SApple OSS Distributions 
1040*bbb1b6f9SApple OSS Distributions 	result = ZeroLengthTest(newValue);
1041*bbb1b6f9SApple OSS Distributions 	if (result) {
1042*bbb1b6f9SApple OSS Distributions 		return result;
1043*bbb1b6f9SApple OSS Distributions 	}
1044*bbb1b6f9SApple OSS Distributions 
1045*bbb1b6f9SApple OSS Distributions 	result = IODirectionPrepareNoZeroFillTest(newValue);
1046*bbb1b6f9SApple OSS Distributions 	if (result) {
1047*bbb1b6f9SApple OSS Distributions 		return result;
1048*bbb1b6f9SApple OSS Distributions 	}
1049*bbb1b6f9SApple OSS Distributions 
1050*bbb1b6f9SApple OSS Distributions 	result = BadFixedAllocTest(newValue);
1051*bbb1b6f9SApple OSS Distributions 	if (result) {
1052*bbb1b6f9SApple OSS Distributions 		return result;
1053*bbb1b6f9SApple OSS Distributions 	}
1054*bbb1b6f9SApple OSS Distributions 
1055*bbb1b6f9SApple OSS Distributions 	result = IOMemoryRemoteTest(newValue);
1056*bbb1b6f9SApple OSS Distributions 	if (result) {
1057*bbb1b6f9SApple OSS Distributions 		return result;
1058*bbb1b6f9SApple OSS Distributions 	}
1059*bbb1b6f9SApple OSS Distributions 
1060*bbb1b6f9SApple OSS Distributions 	result = IOMemoryPrefaultTest(newValue);
1061*bbb1b6f9SApple OSS Distributions 	if (result) {
1062*bbb1b6f9SApple OSS Distributions 		return result;
1063*bbb1b6f9SApple OSS Distributions 	}
1064*bbb1b6f9SApple OSS Distributions 
1065*bbb1b6f9SApple OSS Distributions 	result = IOGuardPageMDTest(newValue);
1066*bbb1b6f9SApple OSS Distributions 	if (result) {
1067*bbb1b6f9SApple OSS Distributions 		return result;
1068*bbb1b6f9SApple OSS Distributions 	}
1069*bbb1b6f9SApple OSS Distributions 
1070*bbb1b6f9SApple OSS Distributions 	result = IOMDContextTest(newValue);
1071*bbb1b6f9SApple OSS Distributions 	if (result) {
1072*bbb1b6f9SApple OSS Distributions 		return result;
1073*bbb1b6f9SApple OSS Distributions 	}
1074*bbb1b6f9SApple OSS Distributions 
1075*bbb1b6f9SApple OSS Distributions 	IOGeneralMemoryDescriptor * md;
1076*bbb1b6f9SApple OSS Distributions 	mach_vm_offset_t data[2];
1077*bbb1b6f9SApple OSS Distributions 	vm_size_t  bsize = 16 * 1024 * 1024;
1078*bbb1b6f9SApple OSS Distributions 	vm_size_t  srcsize, srcoffset, mapoffset, size;
1079*bbb1b6f9SApple OSS Distributions 	kern_return_t kr;
1080*bbb1b6f9SApple OSS Distributions 
1081*bbb1b6f9SApple OSS Distributions 	data[0] = data[1] = 0;
1082*bbb1b6f9SApple OSS Distributions 	kr = mach_vm_allocate_kernel(kernel_map, &data[0], bsize,
1083*bbb1b6f9SApple OSS Distributions 	    VM_MAP_KERNEL_FLAGS_ANYWHERE(.vm_tag = VM_KERN_MEMORY_IOKIT));
1084*bbb1b6f9SApple OSS Distributions 	assert(KERN_SUCCESS == kr);
1085*bbb1b6f9SApple OSS Distributions 
1086*bbb1b6f9SApple OSS Distributions 	mach_vm_inherit(kernel_map, data[0] + ptoa(1), ptoa(1), VM_INHERIT_NONE);
1087*bbb1b6f9SApple OSS Distributions 	mach_vm_inherit(kernel_map, data[0] + ptoa(16), ptoa(4), VM_INHERIT_NONE);
1088*bbb1b6f9SApple OSS Distributions 
1089*bbb1b6f9SApple OSS Distributions 	IOLog("data 0x%lx, 0x%lx\n", (long)data[0], (long)data[1]);
1090*bbb1b6f9SApple OSS Distributions 
1091*bbb1b6f9SApple OSS Distributions 	uint32_t idx, offidx;
1092*bbb1b6f9SApple OSS Distributions 	for (idx = 0; idx < (bsize / sizeof(uint32_t)); idx++) {
1093*bbb1b6f9SApple OSS Distributions 		((uint32_t*)data[0])[idx] = idx;
1094*bbb1b6f9SApple OSS Distributions 	}
1095*bbb1b6f9SApple OSS Distributions 
1096*bbb1b6f9SApple OSS Distributions 	for (srcoffset = 0; srcoffset < bsize; srcoffset = ((srcoffset << 2) + 0x40c)) {
1097*bbb1b6f9SApple OSS Distributions 		for (srcsize = 4; srcsize < (bsize - srcoffset - 1); srcsize = ((srcsize << 2) + 0x3fc)) {
1098*bbb1b6f9SApple OSS Distributions 			IOAddressRange ranges[3];
1099*bbb1b6f9SApple OSS Distributions 			uint32_t rangeCount = 1;
1100*bbb1b6f9SApple OSS Distributions 
1101*bbb1b6f9SApple OSS Distributions 			bzero(&ranges[0], sizeof(ranges));
1102*bbb1b6f9SApple OSS Distributions 			ranges[0].address = data[0] + srcoffset;
1103*bbb1b6f9SApple OSS Distributions 			ranges[0].length  = srcsize;
1104*bbb1b6f9SApple OSS Distributions 			ranges[1].address = ranges[2].address = data[0];
1105*bbb1b6f9SApple OSS Distributions 
1106*bbb1b6f9SApple OSS Distributions 			if (srcsize > ptoa(5)) {
1107*bbb1b6f9SApple OSS Distributions 				ranges[0].length  = 7634;
1108*bbb1b6f9SApple OSS Distributions 				ranges[1].length  = 9870;
1109*bbb1b6f9SApple OSS Distributions 				ranges[2].length  = srcsize - ranges[0].length - ranges[1].length;
1110*bbb1b6f9SApple OSS Distributions 				ranges[1].address = ranges[0].address + ranges[0].length;
1111*bbb1b6f9SApple OSS Distributions 				ranges[2].address = ranges[1].address + ranges[1].length;
1112*bbb1b6f9SApple OSS Distributions 				rangeCount = 3;
1113*bbb1b6f9SApple OSS Distributions 			} else if ((srcsize > ptoa(2)) && !(page_mask & srcoffset)) {
1114*bbb1b6f9SApple OSS Distributions 				ranges[0].length  = ptoa(1);
1115*bbb1b6f9SApple OSS Distributions 				ranges[1].length  = ptoa(1);
1116*bbb1b6f9SApple OSS Distributions 				ranges[2].length  = srcsize - ranges[0].length - ranges[1].length;
1117*bbb1b6f9SApple OSS Distributions 				ranges[0].address = data[0] + srcoffset + ptoa(1);
1118*bbb1b6f9SApple OSS Distributions 				ranges[1].address = data[0] + srcoffset;
1119*bbb1b6f9SApple OSS Distributions 				ranges[2].address = ranges[0].address + ranges[0].length;
1120*bbb1b6f9SApple OSS Distributions 				rangeCount = 3;
1121*bbb1b6f9SApple OSS Distributions 			}
1122*bbb1b6f9SApple OSS Distributions 
1123*bbb1b6f9SApple OSS Distributions 			md = OSDynamicCast(IOGeneralMemoryDescriptor,
1124*bbb1b6f9SApple OSS Distributions 			    IOMemoryDescriptor::withAddressRanges(&ranges[0], rangeCount, kIODirectionInOut, kernel_task));
1125*bbb1b6f9SApple OSS Distributions 			assert(md);
1126*bbb1b6f9SApple OSS Distributions 
1127*bbb1b6f9SApple 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",
1128*bbb1b6f9SApple OSS Distributions 			    (long) srcsize, (long) srcoffset,
1129*bbb1b6f9SApple OSS Distributions 			    (long long) ranges[0].address - data[0], (long long) ranges[0].length,
1130*bbb1b6f9SApple OSS Distributions 			    (long long) ranges[1].address - data[0], (long long) ranges[1].length,
1131*bbb1b6f9SApple OSS Distributions 			    (long long) ranges[2].address - data[0], (long long) ranges[2].length);
1132*bbb1b6f9SApple OSS Distributions 
1133*bbb1b6f9SApple OSS Distributions 			if (kIOReturnSuccess == kr) {
1134*bbb1b6f9SApple OSS Distributions 				for (mapoffset = 0; mapoffset < srcsize; mapoffset = ((mapoffset << 1) + 0xf00)) {
1135*bbb1b6f9SApple OSS Distributions 					for (size = 4; size < (srcsize - mapoffset - 1); size = ((size << 2) + 0x200)) {
1136*bbb1b6f9SApple OSS Distributions 						IOMemoryMap     * map;
1137*bbb1b6f9SApple OSS Distributions 						mach_vm_address_t addr = 0;
1138*bbb1b6f9SApple OSS Distributions 						uint32_t          data;
1139*bbb1b6f9SApple OSS Distributions 
1140*bbb1b6f9SApple OSS Distributions //			IOLog("<mapRef [0x%lx @ 0x%lx]\n", (long) size, (long) mapoffset);
1141*bbb1b6f9SApple OSS Distributions 
1142*bbb1b6f9SApple OSS Distributions 						map = md->createMappingInTask(kernel_task, 0, kIOMapAnywhere | kIOMapGuardedSmall, mapoffset, size);
1143*bbb1b6f9SApple OSS Distributions 						if (map) {
1144*bbb1b6f9SApple OSS Distributions 							addr = map->getAddress();
1145*bbb1b6f9SApple OSS Distributions 						} else {
1146*bbb1b6f9SApple OSS Distributions 							kr = kIOReturnError;
1147*bbb1b6f9SApple OSS Distributions 						}
1148*bbb1b6f9SApple OSS Distributions 
1149*bbb1b6f9SApple OSS Distributions //			IOLog(">mapRef 0x%x %llx\n", kr, addr);
1150*bbb1b6f9SApple OSS Distributions 
1151*bbb1b6f9SApple OSS Distributions 						if (kIOReturnSuccess != kr) {
1152*bbb1b6f9SApple OSS Distributions 							break;
1153*bbb1b6f9SApple OSS Distributions 						}
1154*bbb1b6f9SApple OSS Distributions 						kr = md->prepare();
1155*bbb1b6f9SApple OSS Distributions 						if (kIOReturnSuccess != kr) {
1156*bbb1b6f9SApple OSS Distributions 							panic("prepare() fail 0x%x", kr);
1157*bbb1b6f9SApple OSS Distributions 							break;
1158*bbb1b6f9SApple OSS Distributions 						}
1159*bbb1b6f9SApple OSS Distributions 
1160*bbb1b6f9SApple OSS Distributions 						IOByteCount resident, dirty, swapped;
1161*bbb1b6f9SApple OSS Distributions 						kr = md->getPageCounts(&resident, &dirty, &swapped);
1162*bbb1b6f9SApple OSS Distributions 						if (kIOReturnSuccess != kr) {
1163*bbb1b6f9SApple OSS Distributions 							panic("unable to getExtendedPageCounts");
1164*bbb1b6f9SApple OSS Distributions 							break;
1165*bbb1b6f9SApple OSS Distributions 						}
1166*bbb1b6f9SApple OSS Distributions 						IOLog("Page Counts: %llu resident, %llu dirty, %llu swapped\n",
1167*bbb1b6f9SApple OSS Distributions 						    resident, dirty, swapped);
1168*bbb1b6f9SApple OSS Distributions 						if (swapped != 0) {
1169*bbb1b6f9SApple OSS Distributions 							panic("Swapped page count is not 0 for prepared descriptor %llu", swapped);
1170*bbb1b6f9SApple OSS Distributions 						}
1171*bbb1b6f9SApple OSS Distributions 
1172*bbb1b6f9SApple OSS Distributions 						for (idx = 0; idx < size; idx += sizeof(uint32_t)) {
1173*bbb1b6f9SApple OSS Distributions 							offidx = (typeof(offidx))(idx + mapoffset + srcoffset);
1174*bbb1b6f9SApple OSS Distributions 							if ((srcsize <= ptoa(5)) && (srcsize > ptoa(2)) && !(page_mask & srcoffset)) {
1175*bbb1b6f9SApple OSS Distributions 								if (offidx < ptoa(2)) {
1176*bbb1b6f9SApple OSS Distributions 									offidx ^= ptoa(1);
1177*bbb1b6f9SApple OSS Distributions 								}
1178*bbb1b6f9SApple OSS Distributions 							}
1179*bbb1b6f9SApple OSS Distributions 							offidx /= sizeof(uint32_t);
1180*bbb1b6f9SApple OSS Distributions 
1181*bbb1b6f9SApple OSS Distributions 							if (offidx != ((uint32_t*)addr)[idx / sizeof(uint32_t)]) {
1182*bbb1b6f9SApple OSS Distributions 								panic("vm mismatch md %p map %p, @ 0x%x, 0x%lx, 0x%lx,", md, map, idx, (long) srcoffset, (long) mapoffset);
1183*bbb1b6f9SApple OSS Distributions 								kr = kIOReturnBadMedia;
1184*bbb1b6f9SApple OSS Distributions 							} else {
1185*bbb1b6f9SApple OSS Distributions 								if (sizeof(data) != md->readBytes(mapoffset + idx, &data, sizeof(data))) {
1186*bbb1b6f9SApple OSS Distributions 									data = 0;
1187*bbb1b6f9SApple OSS Distributions 								}
1188*bbb1b6f9SApple OSS Distributions 								if (offidx != data) {
1189*bbb1b6f9SApple OSS Distributions 									panic("phys mismatch md %p map %p, @ 0x%x, 0x%lx, 0x%lx,", md, map, idx, (long) srcoffset, (long) mapoffset);
1190*bbb1b6f9SApple OSS Distributions 									kr = kIOReturnBadMedia;
1191*bbb1b6f9SApple OSS Distributions 								}
1192*bbb1b6f9SApple OSS Distributions 							}
1193*bbb1b6f9SApple OSS Distributions 						}
1194*bbb1b6f9SApple OSS Distributions 						md->complete();
1195*bbb1b6f9SApple OSS Distributions 						map->release();
1196*bbb1b6f9SApple OSS Distributions //			IOLog("unmapRef %llx\n", addr);
1197*bbb1b6f9SApple OSS Distributions 					}
1198*bbb1b6f9SApple OSS Distributions 					if (kIOReturnSuccess != kr) {
1199*bbb1b6f9SApple OSS Distributions 						break;
1200*bbb1b6f9SApple OSS Distributions 					}
1201*bbb1b6f9SApple OSS Distributions 				}
1202*bbb1b6f9SApple OSS Distributions 			}
1203*bbb1b6f9SApple OSS Distributions 			md->release();
1204*bbb1b6f9SApple OSS Distributions 			if (kIOReturnSuccess != kr) {
1205*bbb1b6f9SApple OSS Distributions 				break;
1206*bbb1b6f9SApple OSS Distributions 			}
1207*bbb1b6f9SApple OSS Distributions 		}
1208*bbb1b6f9SApple OSS Distributions 		if (kIOReturnSuccess != kr) {
1209*bbb1b6f9SApple OSS Distributions 			break;
1210*bbb1b6f9SApple OSS Distributions 		}
1211*bbb1b6f9SApple OSS Distributions 	}
1212*bbb1b6f9SApple OSS Distributions 
1213*bbb1b6f9SApple OSS Distributions 	if (kIOReturnSuccess != kr) {
1214*bbb1b6f9SApple OSS Distributions 		IOLog("FAIL: src 0x%lx @ 0x%lx, map 0x%lx @ 0x%lx\n",
1215*bbb1b6f9SApple OSS Distributions 		    (long) srcsize, (long) srcoffset, (long) size, (long) mapoffset);
1216*bbb1b6f9SApple OSS Distributions 	}
1217*bbb1b6f9SApple OSS Distributions 
1218*bbb1b6f9SApple OSS Distributions 	assert(kr == kIOReturnSuccess);
1219*bbb1b6f9SApple OSS Distributions 
1220*bbb1b6f9SApple OSS Distributions 	mach_vm_deallocate(kernel_map, data[0], bsize);
1221*bbb1b6f9SApple OSS Distributions 	//mach_vm_deallocate(kernel_map, data[1], size);
1222*bbb1b6f9SApple OSS Distributions 
1223*bbb1b6f9SApple OSS Distributions 	IOLog("IOMemoryDescriptorTest/ %d\n", (int) gIOMemoryReferenceCount);
1224*bbb1b6f9SApple OSS Distributions 
1225*bbb1b6f9SApple OSS Distributions 	return 0;
1226*bbb1b6f9SApple OSS Distributions }
1227*bbb1b6f9SApple OSS Distributions 
1228*bbb1b6f9SApple OSS Distributions #if HAS_MTE
1229*bbb1b6f9SApple OSS Distributions 
1230*bbb1b6f9SApple OSS Distributions static void
alloc_and_populate_user_buffer(bool should_use_mte,mach_vm_size_t size,mach_vm_offset_t * out_addr)1231*bbb1b6f9SApple OSS Distributions alloc_and_populate_user_buffer(
1232*bbb1b6f9SApple OSS Distributions 	bool should_use_mte,
1233*bbb1b6f9SApple OSS Distributions 	mach_vm_size_t size,
1234*bbb1b6f9SApple OSS Distributions 	mach_vm_offset_t* out_addr)
1235*bbb1b6f9SApple OSS Distributions {
1236*bbb1b6f9SApple OSS Distributions 	vm_map_t user_map = current_map();
1237*bbb1b6f9SApple OSS Distributions 	mach_vm_offset_t buffer;
1238*bbb1b6f9SApple OSS Distributions 	assert(user_map != VM_MAP_NULL);
1239*bbb1b6f9SApple OSS Distributions 	assert(!vm_kernel_map_is_kernel(user_map));
1240*bbb1b6f9SApple OSS Distributions 	kern_return_t kr = mach_vm_allocate_kernel(user_map, &buffer, size,
1241*bbb1b6f9SApple OSS Distributions 	    VM_MAP_KERNEL_FLAGS_ANYWHERE(.vm_tag = VM_MEMORY_MALLOC_SMALL, .vmf_mte = should_use_mte));
1242*bbb1b6f9SApple OSS Distributions 	assert(kr == KERN_SUCCESS);
1243*bbb1b6f9SApple OSS Distributions 
1244*bbb1b6f9SApple OSS Distributions 	// Write some data into the buffer
1245*bbb1b6f9SApple OSS Distributions 	{
1246*bbb1b6f9SApple OSS Distributions 		char* bufData = (char*) IOMallocAligned(size, page_size);
1247*bbb1b6f9SApple OSS Distributions 		assert(bufData);
1248*bbb1b6f9SApple OSS Distributions 		memset(bufData, 'S', size);
1249*bbb1b6f9SApple OSS Distributions 		kr = copyout(bufData, buffer, size);
1250*bbb1b6f9SApple OSS Distributions 		assert(kr == KERN_SUCCESS);
1251*bbb1b6f9SApple OSS Distributions 		IOFreeAligned(bufData, size);
1252*bbb1b6f9SApple OSS Distributions 	}
1253*bbb1b6f9SApple OSS Distributions 
1254*bbb1b6f9SApple OSS Distributions 	if (should_use_mte) {
1255*bbb1b6f9SApple OSS Distributions 		// Set a tag on the buffer
1256*bbb1b6f9SApple OSS Distributions 		// (And switch out of and back into PAN so we can touch the user-mode buffer from kernelspace)
1257*bbb1b6f9SApple OSS Distributions 		__builtin_arm_wsr("pan", 0);
1258*bbb1b6f9SApple OSS Distributions 		buffer = (mach_vm_offset_t)vm_memtag_generate_and_store_tag((caddr_t)buffer, size);
1259*bbb1b6f9SApple OSS Distributions 		__builtin_arm_wsr("pan", 1);
1260*bbb1b6f9SApple OSS Distributions 	}
1261*bbb1b6f9SApple OSS Distributions 
1262*bbb1b6f9SApple OSS Distributions 	*out_addr = buffer;
1263*bbb1b6f9SApple OSS Distributions }
1264*bbb1b6f9SApple OSS Distributions 
1265*bbb1b6f9SApple OSS Distributions static int
IOMDCPUMapMTETest(int newValue)1266*bbb1b6f9SApple OSS Distributions IOMDCPUMapMTETest(int newValue)
1267*bbb1b6f9SApple OSS Distributions {
1268*bbb1b6f9SApple OSS Distributions 	const size_t MTE_TAG_SHIFT = 56;
1269*bbb1b6f9SApple OSS Distributions 	const uint64_t MTE_TAG_MASK = (0xFULL << MTE_TAG_SHIFT);
1270*bbb1b6f9SApple OSS Distributions 
1271*bbb1b6f9SApple OSS Distributions 	const mach_vm_size_t bufSize = PAGE_SIZE;
1272*bbb1b6f9SApple OSS Distributions 	mach_vm_offset_t buffer;
1273*bbb1b6f9SApple OSS Distributions 	alloc_and_populate_user_buffer(true, bufSize, &buffer);
1274*bbb1b6f9SApple OSS Distributions 
1275*bbb1b6f9SApple OSS Distributions 	IOMemoryDescriptor *iomd = IOMemoryDescriptor::withAddressRange(buffer, bufSize, kIODirectionOutIn, current_task());
1276*bbb1b6f9SApple OSS Distributions 	assert(iomd);
1277*bbb1b6f9SApple OSS Distributions 
1278*bbb1b6f9SApple OSS Distributions 	iomd->prepare();
1279*bbb1b6f9SApple OSS Distributions 	IOMemoryMap *map = iomd->map();
1280*bbb1b6f9SApple OSS Distributions 	assert(map);
1281*bbb1b6f9SApple OSS Distributions 
1282*bbb1b6f9SApple OSS Distributions 	/*
1283*bbb1b6f9SApple OSS Distributions 	 * The userspace buffer has an MTE tag set, which will be something other than 0xF.
1284*bbb1b6f9SApple OSS Distributions 	 * However, the pointer we get back here is an untagged kernel pointer,
1285*bbb1b6f9SApple OSS Distributions 	 * so it should be tagged with 0xF (and we assert this).
1286*bbb1b6f9SApple OSS Distributions 	 *
1287*bbb1b6f9SApple OSS Distributions 	 * Despite the mismatch, we expect to be able to access the tagged userspace
1288*bbb1b6f9SApple OSS Distributions 	 * memory without panicking since the kernel's mapping of this memory from
1289*bbb1b6f9SApple OSS Distributions 	 * IOMD::map() should be an untagged one.
1290*bbb1b6f9SApple OSS Distributions 	 */
1291*bbb1b6f9SApple OSS Distributions 	assert((map->getVirtualAddress() & MTE_TAG_MASK) == MTE_TAG_MASK);
1292*bbb1b6f9SApple OSS Distributions 	volatile char *kern_ptr = (volatile char*) map->getVirtualAddress();
1293*bbb1b6f9SApple OSS Distributions 	for (size_t i = 0; i < bufSize; i++) {
1294*bbb1b6f9SApple OSS Distributions 		assert(kern_ptr[i] == 'S');
1295*bbb1b6f9SApple OSS Distributions 		kern_ptr[i]++;
1296*bbb1b6f9SApple OSS Distributions 	}
1297*bbb1b6f9SApple OSS Distributions 	iomd->complete();
1298*bbb1b6f9SApple OSS Distributions 	map->release();
1299*bbb1b6f9SApple OSS Distributions 	iomd->release();
1300*bbb1b6f9SApple OSS Distributions 
1301*bbb1b6f9SApple OSS Distributions 	/* clean up userspace buffer */
1302*bbb1b6f9SApple OSS Distributions 	kern_return_t kr = mach_vm_deallocate(current_map(), vm_memtag_canonicalize_user(buffer), bufSize);
1303*bbb1b6f9SApple OSS Distributions 	assert(kr == KERN_SUCCESS);
1304*bbb1b6f9SApple OSS Distributions 	return kIOReturnSuccess;
1305*bbb1b6f9SApple OSS Distributions }
1306*bbb1b6f9SApple OSS Distributions 
1307*bbb1b6f9SApple OSS Distributions IOReturn
IOMemoryDescriptorCpuMapMTETest(int newValue)1308*bbb1b6f9SApple OSS Distributions IOMemoryDescriptorCpuMapMTETest(int newValue)
1309*bbb1b6f9SApple OSS Distributions {
1310*bbb1b6f9SApple OSS Distributions 	int result;
1311*bbb1b6f9SApple OSS Distributions 	IOLog("/IOMDMapMTETest %d\n", (int) gIOMemoryReferenceCount);
1312*bbb1b6f9SApple OSS Distributions 
1313*bbb1b6f9SApple OSS Distributions 	result = IOMDCPUMapMTETest(newValue);
1314*bbb1b6f9SApple OSS Distributions 	if (result) {
1315*bbb1b6f9SApple OSS Distributions 		return result;
1316*bbb1b6f9SApple OSS Distributions 	}
1317*bbb1b6f9SApple OSS Distributions 
1318*bbb1b6f9SApple OSS Distributions 	IOLog("IOMDMapMTETest/ %d\n", (int) gIOMemoryReferenceCount);
1319*bbb1b6f9SApple OSS Distributions 	return kIOReturnSuccess;
1320*bbb1b6f9SApple OSS Distributions }
1321*bbb1b6f9SApple OSS Distributions 
1322*bbb1b6f9SApple OSS Distributions static void
do_iomd_read_write_bytes_test(uint8_t flags)1323*bbb1b6f9SApple OSS Distributions do_iomd_read_write_bytes_test(uint8_t flags)
1324*bbb1b6f9SApple OSS Distributions {
1325*bbb1b6f9SApple OSS Distributions 	// Extract the test configuration from the flags
1326*bbb1b6f9SApple OSS Distributions 	bool do_write = flags & IOMD_MTE_RWB_DO_WRITE;
1327*bbb1b6f9SApple OSS Distributions 	bool enable_mte = flags & IOMD_MTE_RWB_MTE_BUFFER;
1328*bbb1b6f9SApple OSS Distributions 	bool do_tag_mismatch = flags & IOMD_MTE_RWB_DO_TAG_MISMATCH;
1329*bbb1b6f9SApple OSS Distributions 	bool do_allocation_cleanup = false;
1330*bbb1b6f9SApple OSS Distributions 
1331*bbb1b6f9SApple OSS Distributions 	if (do_tag_mismatch && !enable_mte) {
1332*bbb1b6f9SApple OSS Distributions 		panic("Invalid configuration requested: create a tag mismatch without enabling MTE");
1333*bbb1b6f9SApple OSS Distributions 	}
1334*bbb1b6f9SApple OSS Distributions 
1335*bbb1b6f9SApple OSS Distributions 	// Create the necessary user buffer to operate on
1336*bbb1b6f9SApple OSS Distributions 	const mach_vm_size_t bufSize = PAGE_SIZE;
1337*bbb1b6f9SApple OSS Distributions 	mach_vm_offset_t buffer;
1338*bbb1b6f9SApple OSS Distributions 	alloc_and_populate_user_buffer(enable_mte, bufSize, &buffer);
1339*bbb1b6f9SApple OSS Distributions 
1340*bbb1b6f9SApple OSS Distributions 	// And an IOMD pointing to the buffer
1341*bbb1b6f9SApple OSS Distributions 	IOMemoryDescriptor *iomd = IOMemoryDescriptor::withAddressRange(
1342*bbb1b6f9SApple OSS Distributions 		buffer,
1343*bbb1b6f9SApple OSS Distributions 		bufSize,
1344*bbb1b6f9SApple OSS Distributions 		kIODirectionOutIn,
1345*bbb1b6f9SApple OSS Distributions 		current_task()
1346*bbb1b6f9SApple OSS Distributions 		);
1347*bbb1b6f9SApple OSS Distributions 	assert(iomd);
1348*bbb1b6f9SApple OSS Distributions 
1349*bbb1b6f9SApple OSS Distributions 	iomd->prepare();
1350*bbb1b6f9SApple OSS Distributions 
1351*bbb1b6f9SApple OSS Distributions 	// Do we need to test the consequences of a tag mismatch?
1352*bbb1b6f9SApple OSS Distributions 	if (do_tag_mismatch) {
1353*bbb1b6f9SApple OSS Distributions 		// (Let's switch out of and back into PAN so we can touch the user-mode buffer from kernelspace)
1354*bbb1b6f9SApple OSS Distributions 		// Create a tag check fault condition in the middle of the buffer, as read/writeBytes will LDG
1355*bbb1b6f9SApple OSS Distributions 		// the initial tag.
1356*bbb1b6f9SApple OSS Distributions 		__builtin_arm_wsr("pan", 0);
1357*bbb1b6f9SApple OSS Distributions 		buffer = (mach_vm_offset_t)vm_memtag_generate_and_store_tag((caddr_t)buffer + 32, bufSize - 32);
1358*bbb1b6f9SApple OSS Distributions 		__builtin_arm_wsr("pan", 1);
1359*bbb1b6f9SApple OSS Distributions 	}
1360*bbb1b6f9SApple OSS Distributions 
1361*bbb1b6f9SApple OSS Distributions 	if (do_write) {
1362*bbb1b6f9SApple OSS Distributions 		// Write path:
1363*bbb1b6f9SApple OSS Distributions 		char dataToWrite[64];
1364*bbb1b6f9SApple OSS Distributions 		memset(dataToWrite, 'A', sizeof(dataToWrite));
1365*bbb1b6f9SApple OSS Distributions 		iomd->writeBytes(0, dataToWrite, sizeof(dataToWrite));
1366*bbb1b6f9SApple OSS Distributions 
1367*bbb1b6f9SApple OSS Distributions 
1368*bbb1b6f9SApple OSS Distributions 		if (!do_tag_mismatch) {
1369*bbb1b6f9SApple OSS Distributions 			// At this point, we expect the process to have survived because
1370*bbb1b6f9SApple OSS Distributions 			// the fixup routine in bcopy_phys_internal should have given us a correct access.
1371*bbb1b6f9SApple OSS Distributions 			// Ensure that we actually survived correctly.
1372*bbb1b6f9SApple OSS Distributions 			__builtin_arm_wsr("pan", 0);
1373*bbb1b6f9SApple OSS Distributions 			if (memcmp((const void*)buffer, dataToWrite, sizeof(dataToWrite)) != 0) {
1374*bbb1b6f9SApple OSS Distributions 				panic("Data was not successfully written");
1375*bbb1b6f9SApple OSS Distributions 			}
1376*bbb1b6f9SApple OSS Distributions 			__builtin_arm_wsr("pan", 1);
1377*bbb1b6f9SApple OSS Distributions 		}
1378*bbb1b6f9SApple OSS Distributions 
1379*bbb1b6f9SApple OSS Distributions 		// in the do_tag_mismatch case, userspace gets killed, but we get out of here cleanly.
1380*bbb1b6f9SApple OSS Distributions 		do_allocation_cleanup = true;
1381*bbb1b6f9SApple OSS Distributions 		goto out;
1382*bbb1b6f9SApple OSS Distributions 	} else {
1383*bbb1b6f9SApple OSS Distributions 		// Read path:
1384*bbb1b6f9SApple OSS Distributions 		char buf[64];
1385*bbb1b6f9SApple OSS Distributions 		// Fill buf with an arbitrary pattern, 0x41 is popular is security circles.
1386*bbb1b6f9SApple OSS Distributions 		memset(buf, 'A', sizeof(buf));
1387*bbb1b6f9SApple OSS Distributions 		// If successful this should overwrite buf contents
1388*bbb1b6f9SApple OSS Distributions 		iomd->readBytes(0, buf, sizeof(buf));
1389*bbb1b6f9SApple OSS Distributions 
1390*bbb1b6f9SApple OSS Distributions 		if (!do_tag_mismatch) {
1391*bbb1b6f9SApple OSS Distributions 			// At this point, we expect the process to have survived because
1392*bbb1b6f9SApple OSS Distributions 			// the fixup routine in bcopy_phys_internal should have given us a correct access.
1393*bbb1b6f9SApple OSS Distributions 			// Ensure that we have actually read expected data.
1394*bbb1b6f9SApple OSS Distributions 			char correct_data[64] = {0};
1395*bbb1b6f9SApple OSS Distributions 			memset(correct_data, 'S', sizeof(correct_data));
1396*bbb1b6f9SApple OSS Distributions 			if (memcmp(buf, correct_data, sizeof(correct_data)) != 0) {
1397*bbb1b6f9SApple OSS Distributions 				panic("Failed to read data from the IOMD");
1398*bbb1b6f9SApple OSS Distributions 			}
1399*bbb1b6f9SApple OSS Distributions 		}
1400*bbb1b6f9SApple OSS Distributions 
1401*bbb1b6f9SApple OSS Distributions 		// in the do_tag_mismatch case, userspace gets killed, but we get out of here cleanly.
1402*bbb1b6f9SApple OSS Distributions 		do_allocation_cleanup = true;
1403*bbb1b6f9SApple OSS Distributions 		goto out;
1404*bbb1b6f9SApple OSS Distributions 	}
1405*bbb1b6f9SApple OSS Distributions 
1406*bbb1b6f9SApple OSS Distributions out:
1407*bbb1b6f9SApple OSS Distributions 	// (Cleanup)
1408*bbb1b6f9SApple OSS Distributions 	iomd->complete();
1409*bbb1b6f9SApple OSS Distributions 	iomd->release();
1410*bbb1b6f9SApple OSS Distributions 	if (do_allocation_cleanup) {
1411*bbb1b6f9SApple OSS Distributions 		kern_return_t kr = mach_vm_deallocate(current_map(), vm_memtag_canonicalize_user(buffer), bufSize);
1412*bbb1b6f9SApple OSS Distributions 		assert(kr == KERN_SUCCESS);
1413*bbb1b6f9SApple OSS Distributions 	}
1414*bbb1b6f9SApple OSS Distributions }
1415*bbb1b6f9SApple OSS Distributions 
1416*bbb1b6f9SApple OSS Distributions IOReturn
IOMemoryDescriptorReadWriteBytesMTETest(void)1417*bbb1b6f9SApple OSS Distributions IOMemoryDescriptorReadWriteBytesMTETest(void)
1418*bbb1b6f9SApple OSS Distributions {
1419*bbb1b6f9SApple OSS Distributions 	uint8_t flags = IOMD_MTE_RWB_MTE_BUFFER;
1420*bbb1b6f9SApple OSS Distributions 
1421*bbb1b6f9SApple OSS Distributions 	/* Read path */
1422*bbb1b6f9SApple OSS Distributions 	do_iomd_read_write_bytes_test(flags);
1423*bbb1b6f9SApple OSS Distributions 
1424*bbb1b6f9SApple OSS Distributions 	/* Write path */
1425*bbb1b6f9SApple OSS Distributions 	flags |= IOMD_MTE_RWB_DO_WRITE;
1426*bbb1b6f9SApple OSS Distributions 	do_iomd_read_write_bytes_test(flags);
1427*bbb1b6f9SApple OSS Distributions 
1428*bbb1b6f9SApple OSS Distributions 	return kIOReturnSuccess;
1429*bbb1b6f9SApple OSS Distributions }
1430*bbb1b6f9SApple OSS Distributions 
1431*bbb1b6f9SApple OSS Distributions IOReturn
IOMemoryDescriptorReadWriteBytesWithoutMTETest(void)1432*bbb1b6f9SApple OSS Distributions IOMemoryDescriptorReadWriteBytesWithoutMTETest(void)
1433*bbb1b6f9SApple OSS Distributions {
1434*bbb1b6f9SApple OSS Distributions 	uint8_t flags = 0;
1435*bbb1b6f9SApple OSS Distributions 
1436*bbb1b6f9SApple OSS Distributions 	/* Read path */
1437*bbb1b6f9SApple OSS Distributions 	do_iomd_read_write_bytes_test(flags);
1438*bbb1b6f9SApple OSS Distributions 
1439*bbb1b6f9SApple OSS Distributions 	/* Write path */
1440*bbb1b6f9SApple OSS Distributions 	flags |= IOMD_MTE_RWB_DO_WRITE;
1441*bbb1b6f9SApple OSS Distributions 	do_iomd_read_write_bytes_test(flags);
1442*bbb1b6f9SApple OSS Distributions 
1443*bbb1b6f9SApple OSS Distributions 	return kIOReturnSuccess;
1444*bbb1b6f9SApple OSS Distributions }
1445*bbb1b6f9SApple OSS Distributions 
1446*bbb1b6f9SApple OSS Distributions IOReturn
IOMemoryDescriptorReadBytesMTEWithTCFTest(void)1447*bbb1b6f9SApple OSS Distributions IOMemoryDescriptorReadBytesMTEWithTCFTest(void)
1448*bbb1b6f9SApple OSS Distributions {
1449*bbb1b6f9SApple OSS Distributions 	/* Read + Induce Tag Check Fault */
1450*bbb1b6f9SApple OSS Distributions 	uint8_t flags = IOMD_MTE_RWB_MTE_BUFFER | IOMD_MTE_RWB_DO_TAG_MISMATCH;
1451*bbb1b6f9SApple OSS Distributions 
1452*bbb1b6f9SApple OSS Distributions 	do_iomd_read_write_bytes_test(flags);
1453*bbb1b6f9SApple OSS Distributions 
1454*bbb1b6f9SApple OSS Distributions 	return kIOReturnSuccess;
1455*bbb1b6f9SApple OSS Distributions }
1456*bbb1b6f9SApple OSS Distributions 
1457*bbb1b6f9SApple OSS Distributions IOReturn
IOMemoryDescriptorWriteBytesMTEWithTCFTest(void)1458*bbb1b6f9SApple OSS Distributions IOMemoryDescriptorWriteBytesMTEWithTCFTest(void)
1459*bbb1b6f9SApple OSS Distributions {
1460*bbb1b6f9SApple OSS Distributions 	/* Write + Induce Tag Check Fault */
1461*bbb1b6f9SApple OSS Distributions 	uint8_t flags = IOMD_MTE_RWB_MTE_BUFFER | IOMD_MTE_RWB_DO_WRITE | IOMD_MTE_RWB_DO_TAG_MISMATCH;
1462*bbb1b6f9SApple OSS Distributions 
1463*bbb1b6f9SApple OSS Distributions 	do_iomd_read_write_bytes_test(flags);
1464*bbb1b6f9SApple OSS Distributions 
1465*bbb1b6f9SApple OSS Distributions 	return kIOReturnSuccess;
1466*bbb1b6f9SApple OSS Distributions }
1467*bbb1b6f9SApple OSS Distributions 
1468*bbb1b6f9SApple OSS Distributions IOReturn
IOMemoryDescriptorCreateMTEMappingInOtherMapTest(void)1469*bbb1b6f9SApple OSS Distributions IOMemoryDescriptorCreateMTEMappingInOtherMapTest(void)
1470*bbb1b6f9SApple OSS Distributions {
1471*bbb1b6f9SApple OSS Distributions 	// Given a tagged userspace buffer
1472*bbb1b6f9SApple OSS Distributions 	const mach_vm_size_t bufSize = PAGE_SIZE;
1473*bbb1b6f9SApple OSS Distributions 	mach_vm_offset_t buffer;
1474*bbb1b6f9SApple OSS Distributions 	alloc_and_populate_user_buffer(true, bufSize, &buffer);
1475*bbb1b6f9SApple OSS Distributions 
1476*bbb1b6f9SApple OSS Distributions 	// And an IOMD pointing to the buffer
1477*bbb1b6f9SApple OSS Distributions 	IOMemoryDescriptor *iomd = IOMemoryDescriptor::withAddressRange(
1478*bbb1b6f9SApple OSS Distributions 		buffer,
1479*bbb1b6f9SApple OSS Distributions 		bufSize,
1480*bbb1b6f9SApple OSS Distributions 		kIODirectionOutIn,
1481*bbb1b6f9SApple OSS Distributions 		current_task()
1482*bbb1b6f9SApple OSS Distributions 		);
1483*bbb1b6f9SApple OSS Distributions 	assert(iomd);
1484*bbb1b6f9SApple OSS Distributions 
1485*bbb1b6f9SApple OSS Distributions 	iomd->prepare();
1486*bbb1b6f9SApple OSS Distributions 
1487*bbb1b6f9SApple OSS Distributions 	// When I try to enter the mapping into another task
1488*bbb1b6f9SApple OSS Distributions 	// (Use launchd as a guinea pig)
1489*bbb1b6f9SApple OSS Distributions 	proc_t dest_proc = proc_find(1);
1490*bbb1b6f9SApple OSS Distributions 	IOMemoryMap* map = iomd->createMappingInTask(proc_task(dest_proc), 0, kIOMapAnywhere, 0, 0);
1491*bbb1b6f9SApple OSS Distributions 
1492*bbb1b6f9SApple OSS Distributions 	// Then the request is allowed, because aliasing MTE mappings is always allowed
1493*bbb1b6f9SApple OSS Distributions 	assert(map != NULL);
1494*bbb1b6f9SApple OSS Distributions 	// But the aliased mapping should not have MTE enabled (so should be tagged with the user-canonical tag)
1495*bbb1b6f9SApple OSS Distributions 	uint64_t virt_addr = map->getVirtualAddress();
1496*bbb1b6f9SApple OSS Distributions 	assert(vm_memtag_extract_tag(virt_addr) == 0);
1497*bbb1b6f9SApple OSS Distributions 	assert(!pmap_is_tagged_mapping(vm_map_get_pmap(current_map()), virt_addr));
1498*bbb1b6f9SApple OSS Distributions 
1499*bbb1b6f9SApple OSS Distributions 	// (Cleanup)
1500*bbb1b6f9SApple OSS Distributions 	proc_rele(dest_proc);
1501*bbb1b6f9SApple OSS Distributions 	map->release();
1502*bbb1b6f9SApple OSS Distributions 	iomd->complete();
1503*bbb1b6f9SApple OSS Distributions 	iomd->release();
1504*bbb1b6f9SApple OSS Distributions 	kern_return_t kr = mach_vm_deallocate(current_map(), vm_memtag_canonicalize_user(buffer), bufSize);
1505*bbb1b6f9SApple OSS Distributions 	assert(kr == KERN_SUCCESS);
1506*bbb1b6f9SApple OSS Distributions 	return kIOReturnSuccess;
1507*bbb1b6f9SApple OSS Distributions }
1508*bbb1b6f9SApple OSS Distributions 
1509*bbb1b6f9SApple OSS Distributions IOReturn
IOMemoryDescriptorCreateMTEMappingInThisMapTest(void)1510*bbb1b6f9SApple OSS Distributions IOMemoryDescriptorCreateMTEMappingInThisMapTest(void)
1511*bbb1b6f9SApple OSS Distributions {
1512*bbb1b6f9SApple OSS Distributions 	// Given a tagged userspace buffer
1513*bbb1b6f9SApple OSS Distributions 	const mach_vm_size_t bufSize = PAGE_SIZE;
1514*bbb1b6f9SApple OSS Distributions 	mach_vm_offset_t buffer;
1515*bbb1b6f9SApple OSS Distributions 	alloc_and_populate_user_buffer(true, bufSize, &buffer);
1516*bbb1b6f9SApple OSS Distributions 
1517*bbb1b6f9SApple OSS Distributions 	// And an IOMD pointing to the buffer
1518*bbb1b6f9SApple OSS Distributions 	IOMemoryDescriptor *iomd = IOMemoryDescriptor::withAddressRange(
1519*bbb1b6f9SApple OSS Distributions 		buffer,
1520*bbb1b6f9SApple OSS Distributions 		bufSize,
1521*bbb1b6f9SApple OSS Distributions 		kIODirectionOutIn,
1522*bbb1b6f9SApple OSS Distributions 		current_task()
1523*bbb1b6f9SApple OSS Distributions 		);
1524*bbb1b6f9SApple OSS Distributions 	assert(iomd);
1525*bbb1b6f9SApple OSS Distributions 
1526*bbb1b6f9SApple OSS Distributions 	iomd->prepare();
1527*bbb1b6f9SApple OSS Distributions 
1528*bbb1b6f9SApple OSS Distributions 	// When I try to enter the mapping into this task
1529*bbb1b6f9SApple OSS Distributions 	IOMemoryMap* map = iomd->createMappingInTask(current_task(), 0, kIOMapAnywhere, 0, 0);
1530*bbb1b6f9SApple OSS Distributions 
1531*bbb1b6f9SApple OSS Distributions 	// Then the request is allowed, because aliasing MTE mappings within the originating task is allowed
1532*bbb1b6f9SApple OSS Distributions 	assert(map != NULL);
1533*bbb1b6f9SApple OSS Distributions 	// And the aliased mapping also has MTE enabled
1534*bbb1b6f9SApple OSS Distributions 	uint64_t virt_addr = map->getVirtualAddress();
1535*bbb1b6f9SApple OSS Distributions 	assert(vm_memtag_extract_tag(virt_addr) != 0);
1536*bbb1b6f9SApple OSS Distributions 	assert(pmap_is_tagged_mapping(vm_map_get_pmap(current_map()), vm_memtag_canonicalize_user(virt_addr)));
1537*bbb1b6f9SApple OSS Distributions 
1538*bbb1b6f9SApple OSS Distributions 	// (Cleanup)
1539*bbb1b6f9SApple OSS Distributions 	map->release();
1540*bbb1b6f9SApple OSS Distributions 	iomd->complete();
1541*bbb1b6f9SApple OSS Distributions 	iomd->release();
1542*bbb1b6f9SApple OSS Distributions 	kern_return_t kr = mach_vm_deallocate(current_map(), vm_memtag_canonicalize_user(buffer), bufSize);
1543*bbb1b6f9SApple OSS Distributions 	assert(kr == KERN_SUCCESS);
1544*bbb1b6f9SApple OSS Distributions 	return kIOReturnSuccess;
1545*bbb1b6f9SApple OSS Distributions }
1546*bbb1b6f9SApple OSS Distributions 
1547*bbb1b6f9SApple OSS Distributions IOReturn
IOMemoryDescriptorCreateMTEMappingInKernelMapTest(void)1548*bbb1b6f9SApple OSS Distributions IOMemoryDescriptorCreateMTEMappingInKernelMapTest(void)
1549*bbb1b6f9SApple OSS Distributions {
1550*bbb1b6f9SApple OSS Distributions 	// Given a tagged userspace buffer
1551*bbb1b6f9SApple OSS Distributions 	const mach_vm_size_t bufSize = PAGE_SIZE;
1552*bbb1b6f9SApple OSS Distributions 	mach_vm_offset_t buffer;
1553*bbb1b6f9SApple OSS Distributions 	alloc_and_populate_user_buffer(true, bufSize, &buffer);
1554*bbb1b6f9SApple OSS Distributions 
1555*bbb1b6f9SApple OSS Distributions 	// And an IOMD pointing to the buffer
1556*bbb1b6f9SApple OSS Distributions 	IOMemoryDescriptor *iomd = IOMemoryDescriptor::withAddressRange(
1557*bbb1b6f9SApple OSS Distributions 		buffer,
1558*bbb1b6f9SApple OSS Distributions 		bufSize,
1559*bbb1b6f9SApple OSS Distributions 		kIODirectionOutIn,
1560*bbb1b6f9SApple OSS Distributions 		current_task()
1561*bbb1b6f9SApple OSS Distributions 		);
1562*bbb1b6f9SApple OSS Distributions 	assert(iomd);
1563*bbb1b6f9SApple OSS Distributions 	iomd->prepare();
1564*bbb1b6f9SApple OSS Distributions 
1565*bbb1b6f9SApple OSS Distributions 	// When I try to enter the mapping into the kernel map
1566*bbb1b6f9SApple OSS Distributions 	IOMemoryMap* map = iomd->createMappingInTask(kernel_task, 0, kIOMapAnywhere, 0, 0);
1567*bbb1b6f9SApple OSS Distributions 
1568*bbb1b6f9SApple OSS Distributions 	// Then the request is allowed, because aliasing MTE mappings is always allowed
1569*bbb1b6f9SApple OSS Distributions 	assert(map != NULL);
1570*bbb1b6f9SApple OSS Distributions 	// But the aliased mapping should not have MTE enabled (so should be tagged with the kernel-canonical tag)
1571*bbb1b6f9SApple OSS Distributions 	uint64_t virt_addr = map->getVirtualAddress();
1572*bbb1b6f9SApple OSS Distributions 	assert(vm_memtag_extract_tag(virt_addr) == 0xf);
1573*bbb1b6f9SApple OSS Distributions 	assert(!pmap_is_tagged_mapping(vm_map_get_pmap(current_map()), virt_addr));
1574*bbb1b6f9SApple OSS Distributions 
1575*bbb1b6f9SApple OSS Distributions 	// (Cleanup)
1576*bbb1b6f9SApple OSS Distributions 	map->release();
1577*bbb1b6f9SApple OSS Distributions 	iomd->complete();
1578*bbb1b6f9SApple OSS Distributions 	iomd->release();
1579*bbb1b6f9SApple OSS Distributions 	kern_return_t kr = mach_vm_deallocate(current_map(), vm_memtag_canonicalize_user(buffer), bufSize);
1580*bbb1b6f9SApple OSS Distributions 	assert(kr == KERN_SUCCESS);
1581*bbb1b6f9SApple OSS Distributions 	return kIOReturnSuccess;
1582*bbb1b6f9SApple OSS Distributions }
1583*bbb1b6f9SApple OSS Distributions #endif /* HAS_MTE */
1584*bbb1b6f9SApple OSS Distributions 
1585*bbb1b6f9SApple OSS Distributions #endif  /* DEVELOPMENT || DEBUG */
1586