+ {
+ { (uintptr_t) &IOMemoryDescriptorTest, 0x2ffc },
+ { 0, 0 },
+ },
+ {
+ { ranges[0][0].address, 0x10 },
+ { 0x3000 + ranges[0][0].address, 0xff0 },
+ },
+ {
+ { ranges[0][0].address, 0x2ffc },
+ { trunc_page(ranges[0][0].address), 0x800 },
+ },
+ };
+ static const uint32_t rangesCount[3] = { 1, 2, 2 };
+ uint32_t test;
+
+ for (test = 0; test < 3; test++) {
+ kprintf("---[%d] address 0x%qx-0x%qx, 0x%qx-0x%qx\n", test,
+ ranges[test][0].address, ranges[test][0].length,
+ ranges[test][1].address, ranges[test][1].length);
+
+ md = IOMemoryDescriptor::withAddressRanges((IOAddressRange*)&ranges[test][0], rangesCount[test], kIODirectionOut, kernel_task);
+ assert(md);
+ ret = md->prepare();
+ assert(kIOReturnSuccess == ret);
+ dma = IODMACommand::withSpecification(kIODMACommandOutputHost64, &segOptions,
+ IODMACommand::kMapped, NULL, NULL);
+ assert(dma);
+ ret = dma->setMemoryDescriptor(md, true);
+ if (kIOReturnSuccess == ret) {
+ IODMACommand::Segment64 segments[1];
+ UInt32 numSegments;
+ UInt64 offset;
+
+ offset = 0;
+ do{
+ numSegments = 1;
+ ret = dma->gen64IOVMSegments(&offset, &segments[0], &numSegments);
+ assert(kIOReturnSuccess == ret);
+ assert(1 == numSegments);
+ kprintf("seg 0x%qx, 0x%qx\n", segments[0].fIOVMAddr, segments[0].fLength);
+ }while (offset < md->getLength());
+
+ ret = dma->clearMemoryDescriptor(true);
+ assert(kIOReturnSuccess == ret);
+ dma->release();
+ }
+ md->release();
+ }
+
+ return kIOReturnSuccess;
+ } else if (4 == newValue) {
+ IOService * isp;
+ IOMapper * mapper;
+ IOBufferMemoryDescriptor * md1;
+ IODMACommand * dma;
+ IOReturn ret;
+ size_t bufSize = 8192 * 8192 * sizeof(uint32_t);
+ uint64_t start, time, nano;
+
+ isp = IOService::copyMatchingService(IOService::nameMatching("isp"));
+ assert(isp);
+ mapper = IOMapper::copyMapperForDeviceWithIndex(isp, 0);
+ assert(mapper);
+
+ md1 = IOBufferMemoryDescriptor::inTaskWithOptions(TASK_NULL,
+ kIODirectionOutIn | kIOMemoryPersistent | kIOMemoryPageable,
+ bufSize, page_size);
+
+ ret = md1->prepare();
+ assert(kIOReturnSuccess == ret);
+
+ IODMAMapSpecification mapSpec;
+ bzero(&mapSpec, sizeof(mapSpec));
+ uint64_t mapped;
+ uint64_t mappedLength;
+
+ start = mach_absolute_time();
+
+ ret = md1->dmaMap(mapper, NULL, &mapSpec, 0, bufSize, &mapped, &mappedLength);
+ assert(kIOReturnSuccess == ret);
+
+ time = mach_absolute_time() - start;
+
+ absolutetime_to_nanoseconds(time, &nano);
+ kprintf("time %lld us\n", nano / 1000ULL);
+ kprintf("seg0 0x%qx, 0x%qx\n", mapped, mappedLength);
+
+ assert(md1);
+
+ dma = IODMACommand::withSpecification(kIODMACommandOutputHost32,
+ 32, 0, IODMACommand::kMapped, 0, 1, mapper, NULL);
+
+ assert(dma);
+
+ start = mach_absolute_time();
+ ret = dma->setMemoryDescriptor(md1, true);
+ assert(kIOReturnSuccess == ret);
+ time = mach_absolute_time() - start;
+
+ absolutetime_to_nanoseconds(time, &nano);
+ kprintf("time %lld us\n", nano / 1000ULL);
+
+
+ IODMACommand::Segment32 segments[1];
+ UInt32 numSegments = 1;
+ UInt64 offset;
+
+ offset = 0;
+ ret = dma->gen32IOVMSegments(&offset, &segments[0], &numSegments);
+ assert(kIOReturnSuccess == ret);
+ assert(1 == numSegments);
+ kprintf("seg0 0x%x, 0x%x\n", (int)segments[0].fIOVMAddr, (int)segments[0].fLength);
+
+ ret = dma->clearMemoryDescriptor(true);
+ assert(kIOReturnSuccess == ret);
+
+ md1->release();
+
+ return kIOReturnSuccess;
+ }
+
+ if (3 == newValue) {
+ IOBufferMemoryDescriptor * md1;
+ IOBufferMemoryDescriptor * md2;
+ IOMemoryMap * map1;
+ IOMemoryMap * map2;
+ uint32_t * buf1;
+ uint32_t * buf2;
+ IOReturn err;
+
+ md1 = IOBufferMemoryDescriptor::inTaskWithOptions(TASK_NULL,
+ kIODirectionOutIn | kIOMemoryPersistent | kIOMemoryPageable,
+ 64 * 1024, page_size);
+ assert(md1);
+ map1 = md1->createMappingInTask(kernel_task, 0, kIOMapAnywhere | kIOMapUnique);
+ assert(map1);
+ buf1 = (uint32_t *) map1->getVirtualAddress();
+
+ md2 = IOBufferMemoryDescriptor::inTaskWithOptions(TASK_NULL,
+ kIODirectionOutIn | kIOMemoryPersistent | kIOMemoryPageable,
+ 64 * 1024, page_size);
+ assert(md2);
+ map2 = md2->createMappingInTask(kernel_task, 0, kIOMapAnywhere | kIOMapUnique);
+ assert(map2);
+ buf2 = (uint32_t *) map2->getVirtualAddress();
+
+ memset(buf1, 0x11, 64 * 1024L);
+ memset(buf2, 0x22, 64 * 1024L);
+
+ kprintf("md1 %p, map1 %p, buf2 %p; md2 %p, map2 %p, buf2 %p\n", md1, map1, buf1, md2, map2, buf2);
+
+ kprintf("no redir 0x%08x, 0x%08x\n", buf1[0], buf2[0]);
+ assert(0x11111111 == buf1[0]);
+ assert(0x22222222 == buf2[0]);
+ err = map1->redirect(md2, 0, 0ULL);
+ kprintf("redir md2(0x%x) 0x%08x, 0x%08x\n", err, buf1[0], buf2[0]);
+ assert(0x11111111 == buf2[0]);
+ assert(0x22222222 == buf1[0]);
+ err = map1->redirect(md1, 0, 0ULL);
+ kprintf("redir md1(0x%x) 0x%08x, 0x%08x\n", err, buf1[0], buf2[0]);
+ assert(0x11111111 == buf1[0]);
+ assert(0x22222222 == buf2[0]);
+ map1->release();
+ map2->release();
+ md1->release();
+ md2->release();
+ }
+#endif
+
+// result = IODMACommandLocalMappedNonContig(newValue);
+// if (result) return (result);
+
+ result = IODMACommandForceDoubleBufferTest(newValue);
+ if (result) {
+ return result;
+ }
+
+ result = AllocationNameTest(newValue);
+ if (result) {
+ return result;
+ }
+
+ result = IOMemoryMapCopyOnWriteTest(newValue);
+ if (result) {
+ return result;
+ }
+
+ result = IOMultMemoryDescriptorTest(newValue);
+ if (result) {
+ return result;
+ }
+
+ result = ZeroLengthTest(newValue);
+ if (result) {
+ return result;
+ }
+
+ result = IODirectionPrepareNoZeroFillTest(newValue);
+ if (result) {
+ return result;
+ }
+
+ result = BadFixedAllocTest(newValue);
+ if (result) {
+ return result;
+ }
+
+ result = IOMemoryRemoteTest(newValue);
+ if (result) {
+ return result;
+ }
+
+ result = IOMemoryPrefaultTest(newValue);
+ if (result) {
+ return result;
+ }
+
+ IOGeneralMemoryDescriptor * md;
+ vm_offset_t data[2];
+ vm_size_t bsize = 16 * 1024 * 1024;
+ vm_size_t srcsize, srcoffset, mapoffset, size;
+ kern_return_t kr;
+
+ data[0] = data[1] = 0;
+ kr = vm_allocate_kernel(kernel_map, &data[0], bsize, VM_FLAGS_ANYWHERE, VM_KERN_MEMORY_IOKIT);
+ assert(KERN_SUCCESS == kr);
+
+ vm_inherit(kernel_map, data[0] + ptoa(1), ptoa(1), VM_INHERIT_NONE);
+ vm_inherit(kernel_map, data[0] + ptoa(16), ptoa(4), VM_INHERIT_NONE);
+
+ IOLog("data 0x%lx, 0x%lx\n", (long)data[0], (long)data[1]);
+
+ uint32_t idx, offidx;
+ for (idx = 0; idx < (bsize / sizeof(uint32_t)); idx++) {
+ ((uint32_t*)data[0])[idx] = idx;
+ }
+
+ for (srcoffset = 0; srcoffset < bsize; srcoffset = ((srcoffset << 2) + 0x40c)) {
+ for (srcsize = 4; srcsize < (bsize - srcoffset - 1); srcsize = ((srcsize << 2) + 0x3fc)) {
+ IOAddressRange ranges[3];
+ uint32_t rangeCount = 1;
+
+ bzero(&ranges[0], sizeof(ranges));
+ ranges[0].address = data[0] + srcoffset;
+ ranges[0].length = srcsize;
+ ranges[1].address = ranges[2].address = data[0];
+
+ if (srcsize > ptoa(5)) {
+ ranges[0].length = 7634;
+ ranges[1].length = 9870;
+ ranges[2].length = srcsize - ranges[0].length - ranges[1].length;
+ ranges[1].address = ranges[0].address + ranges[0].length;
+ ranges[2].address = ranges[1].address + ranges[1].length;
+ rangeCount = 3;
+ } else if ((srcsize > ptoa(2)) && !(page_mask & srcoffset)) {
+ ranges[0].length = ptoa(1);
+ ranges[1].length = ptoa(1);
+ ranges[2].length = srcsize - ranges[0].length - ranges[1].length;
+ ranges[0].address = data[0] + srcoffset + ptoa(1);
+ ranges[1].address = data[0] + srcoffset;
+ ranges[2].address = ranges[0].address + ranges[0].length;
+ rangeCount = 3;
+ }
+
+ md = OSDynamicCast(IOGeneralMemoryDescriptor,
+ IOMemoryDescriptor::withAddressRanges(&ranges[0], rangeCount, kIODirectionInOut, kernel_task));
+ assert(md);
+
+ IOLog("IOMemoryDescriptor::withAddressRanges [0x%lx @ 0x%lx]\n[0x%llx, 0x%llx],\n[0x%llx, 0x%llx],\n[0x%llx, 0x%llx]\n",
+ (long) srcsize, (long) srcoffset,
+ (long long) ranges[0].address - data[0], (long long) ranges[0].length,
+ (long long) ranges[1].address - data[0], (long long) ranges[1].length,
+ (long long) ranges[2].address - data[0], (long long) ranges[2].length);
+
+ if (kIOReturnSuccess == kr) {
+ for (mapoffset = 0; mapoffset < srcsize; mapoffset = ((mapoffset << 1) + 0xf00)) {
+ for (size = 4; size < (srcsize - mapoffset - 1); size = ((size << 2) + 0x200)) {
+ IOMemoryMap * map;
+ mach_vm_address_t addr = 0;
+ uint32_t data;