/* SPDX-License-Identifier: MIT */ /* Actual Vulkan/Turnip/KMD control flow over the software kernel peer. It runs no * shader: pending fences, retained native objects/views/IOVAs and error handling * are checked independently of rendering or hardware timing. */ #include #include "NativeAdrenoABI.h" #include #include #include #include #include #include #include #include #define REQUIRE(x) do {if (!(x)) {std::fprintf(stderr,"FAIL line %d: %s\n",__LINE__,#x);return 1;}} while(0) #define OK(x) REQUIRE((x)==VK_SUCCESS) #define LOAD(name) auto name=(PFN_##name)get(instance,#name);REQUIRE(name) struct Peer { void (*hold)(int); std::atomic release{false},forced{false}; std::thread worker; explicit Peer(void (*fn)(int)):hold(fn){hold(1);} void watchdog() { worker=std::thread([this]{ auto end=std::chrono::steady_clock::now()+std::chrono::milliseconds(500); while(!release.load() && std::chrono::steady_clock::now()=0); VkQueueFamilyProperties families[8];count=8;vkGetPhysicalDeviceQueueFamilyProperties(physical,&count,families); int family=-1;for(uint32_t i=0;i=0); float priority=1; VkDeviceQueueCreateInfo qi={.sType=VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,.queueFamilyIndex=(uint32_t)family,.queueCount=1,.pQueuePriorities=&priority}; VkDeviceCreateInfo di={.sType=VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,.queueCreateInfoCount=1,.pQueueCreateInfos=&qi}; VkDevice device;OK(vkCreateDevice(physical,&di,nullptr,&device)); VkQueue queue;vkGetDeviceQueue(device,(uint32_t)family,0,&queue); VkBufferCreateInfo bi={.sType=VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,.size=4096,.usage=VK_BUFFER_USAGE_TRANSFER_DST_BIT,.sharingMode=VK_SHARING_MODE_EXCLUSIVE}; VkBuffer buffer;OK(vkCreateBuffer(device,&bi,nullptr,&buffer)); VkMemoryRequirements req;vkGetBufferMemoryRequirements(device,buffer,&req);REQUIRE(req.memoryTypeBits&(1u< unused(pressure?257:1); uint64_t oldIova=0; for(auto &allocation:unused){OK(vkAllocateMemory(device,&ai,nullptr,&allocation));void *address=nullptr;OK(vkMapMemory(device,allocation,0,VK_WHOLE_SIZE,0,&address));if(!oldIova)oldIova=lastIova();} VkCommandPoolCreateInfo pi={.sType=VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,.queueFamilyIndex=(uint32_t)family}; VkCommandPool pool;OK(vkCreateCommandPool(device,&pi,nullptr,&pool)); VkCommandBufferAllocateInfo ci={.sType=VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,.commandPool=pool,.level=VK_COMMAND_BUFFER_LEVEL_PRIMARY,.commandBufferCount=1}; VkCommandBuffer commands;OK(vkAllocateCommandBuffers(device,&ci,&commands)); VkCommandBufferBeginInfo begin={.sType=VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO};OK(vkBeginCommandBuffer(commands,&begin)); vkCmdFillBuffer(commands,buffer,0,4096,0x5a5a5a5a);OK(vkEndCommandBuffer(commands)); Peer peer(hold); unsigned beforeSubmit=submissions(); VkSubmitInfo si={.sType=VK_STRUCTURE_TYPE_SUBMIT_INFO,.commandBufferCount=1,.pCommandBuffers=&commands}; OK(vkQueueSubmit(queue,1,&si,VK_NULL_HANDLE));REQUIRE(submissions()>beforeSubmit); const unsigned beforeLive=live(),beforeFree=successes(NativeAdrenoMethodBoFree),beforeUnmap=successes(NativeAdrenoMethodBoCpuUnmap); if(fenceError)inject(NativeAdrenoMethodWaitFence,7,1); // fixture kIOReturnIOError, before mutation if(!pressure)peer.watchdog(); for(unsigned i=0;i<(pressure?256u:1u);i++){ vkFreeMemory(device,unused[i],nullptr);unused[i]=VK_NULL_HANDLE; REQUIRE(!peer.forced.load()); REQUIRE(successes(NativeAdrenoMethodBoFree)==beforeFree); REQUIRE(successes(NativeAdrenoMethodBoCpuUnmap)==beforeUnmap); } REQUIRE(live()==beforeLive); VkDeviceMemory probe=VK_NULL_HANDLE,after=VK_NULL_HANDLE; if(cleanupError){ inject(NativeAdrenoMethodBoMap,7,1);inject(NativeAdrenoMethodBoFree,2,1); VkDeviceMemory refused=VK_NULL_HANDLE; REQUIRE(vkAllocateMemory(device,&ai,nullptr,&refused)==VK_ERROR_DEVICE_LOST); REQUIRE(live()==beforeLive+1); REQUIRE(successes(NativeAdrenoMethodBoFree)==beforeFree); REQUIRE(successes(NativeAdrenoMethodBoCpuUnmap)==beforeUnmap); } if(!fatal && !pressure){OK(vkAllocateMemory(device,&ai,nullptr,&probe));REQUIRE(lastIova()!=oldIova);REQUIRE(successes(NativeAdrenoMethodBoFree)==beforeFree);} if(pressure){ /* The deferred queue grows: the 257th free does not wait for the GPU either. */ peer.watchdog(); vkFreeMemory(device,unused.back(),nullptr);unused.back()=VK_NULL_HANDLE; REQUIRE(!peer.forced.load()); REQUIRE(successes(NativeAdrenoMethodBoFree)==beforeFree); REQUIRE(successes(NativeAdrenoMethodBoCpuUnmap)==beforeUnmap); } peer.finish(); if(!pressure&&!busy&&!fatal){ OK(vkDeviceWaitIdle(device)); /* A completed BO is released, or kept whole (handle, mapping, CPU lease) in the BO cache that hands it to the next allocation of * its class. */ const unsigned allocs=successes(NativeAdrenoMethodBoAlloc); VkDeviceMemory reuse;OK(vkAllocateMemory(device,&ai,nullptr,&reuse)); const bool cached=successes(NativeAdrenoMethodBoAlloc)==allocs; REQUIRE(cached || (successes(NativeAdrenoMethodBoFree)==beforeFree+1 && successes(NativeAdrenoMethodBoCpuUnmap)==beforeUnmap+1)); vkFreeMemory(device,reuse,nullptr); OK(vkDeviceWaitIdle(device)); } if(busy)inject(NativeAdrenoMethodBoFree,2,1); // temporary busy must retain the IOVA if(freeError)inject(NativeAdrenoMethodBoFree,7,1); if(leaseError)inject(NativeAdrenoMethodBoCpuUnmap,7,1); if(viewError)setenv("HOSTSIM_FAIL_USER_UNMAP","1",1); VkResult result=vkAllocateMemory(device,&ai,nullptr,&after); if(fatal){ REQUIRE(result==VK_ERROR_DEVICE_LOST); REQUIRE(successes(NativeAdrenoMethodBoFree)==beforeFree); REQUIRE(successes(NativeAdrenoMethodBoCpuUnmap)==beforeUnmap+(freeError?1u:0u)); } else { REQUIRE(result==VK_SUCCESS); if(busy){ REQUIRE(lastIova()!=oldIova);REQUIRE(successes(NativeAdrenoMethodBoFree)==beforeFree); VkDeviceMemory retry;OK(vkAllocateMemory(device,&ai,nullptr,&retry)); REQUIRE(successes(NativeAdrenoMethodBoFree)==beforeFree+1); REQUIRE(successes(NativeAdrenoMethodBoCpuUnmap)==beforeUnmap+1); vkFreeMemory(device,retry,nullptr); } else REQUIRE(successes(NativeAdrenoMethodBoFree)>=beforeFree); /* completed BOs may go to the cache */ } unsetenv("HOSTSIM_FAIL_USER_UNMAP");inject(~0u,0,0); if(!fatal)OK(vkDeviceWaitIdle(device)); if(probe)vkFreeMemory(device,probe,nullptr); if(after)vkFreeMemory(device,after,nullptr); vkDestroyCommandPool(device,pool,nullptr);vkDestroyBuffer(device,buffer,nullptr);vkFreeMemory(device,used,nullptr); vkDestroyDevice(device,nullptr);vkDestroyInstance(instance,nullptr); // A failed fence/view/lease release retains native ownership in this narrow peer; // it deliberately does not manufacture a successful IOKit close or recovery. if(!fatal)REQUIRE(live()==0); std::printf("PASS: deferred BO %s; native objects at exit=%u; software control only, no shader execution\n",argv[2],live()); return 0; }