diff --git a/core/libs/prx/libSceAgcDriver/Execution/src/Pm4.cpp b/core/libs/prx/libSceAgcDriver/Execution/src/Pm4.cpp index a89bc0eca..ff230342a 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/src/Pm4.cpp +++ b/core/libs/prx/libSceAgcDriver/Execution/src/Pm4.cpp @@ -94,6 +94,31 @@ std::vector copySource(std::uint64_t source, std::size_t bytes, bool return data; } +constexpr std::uint32_t DmaSelectGds = 1; +constexpr std::size_t GdsBytes = 0x10000; + +struct GdsStorage { + std::mutex mutex; + std::array bytes{}; +}; + +GdsStorage& Gds() { + static GdsStorage storage; + return storage; +} + +bool gdsRange(std::uint64_t offset, std::size_t bytes) { + return offset <= GdsBytes && bytes <= GdsBytes - offset; +} + +std::vector dmaSourceBytes(std::span packet) { + const std::size_t bytes = packet[6] & 0x3ffffffu; + if (dmaSource(packet) != DmaSelectGds) return copySource(address(packet[2], packet[3]), bytes, dmaSource(packet) == 2); + auto& gds = Gds(); + std::lock_guard lock(gds.mutex); + return {gds.bytes.begin() + packet[2], gds.bytes.begin() + packet[2] + bytes}; +} + void copyMemory(std::uint64_t source, std::uint64_t destination, std::size_t bytes, bool immediate) { if (bytes == 0) return; GuestMemory::CheckRange(reinterpret_cast(destination), bytes, 1, true); @@ -164,6 +189,9 @@ std::string_view UnsupportedReason(std::uint32_t header) { } } +constexpr std::uint32_t DmaSourceCachePolicy = 3u << 13u; +constexpr std::uint32_t DmaDestinationCachePolicy = 3u << 25u; + void Validate(std::span packet, std::uint32_t queue) { require(!packet.empty(), "truncated PM4 header"); const auto header = packet[0]; @@ -355,9 +383,11 @@ void Validate(std::span packet, std::uint32_t queue) { } case 0x50: size(7); - require((packet[1] & ~0xe0300001u) == 0, "DMA_DATA cache or reserved fields are not implemented"); - require(memorySelector(dmaDestination(packet)), "DMA_DATA register, GDS or prefetch destination is not implemented"); - require(memorySelector(dmaSource(packet)) || dmaSource(packet) == 2, "DMA_DATA register or GDS source is not implemented"); + require((packet[1] & ~(0xe0300001u | DmaSourceCachePolicy | DmaDestinationCachePolicy)) == 0, "DMA_DATA reserved fields are not implemented"); + require(memorySelector(dmaDestination(packet)) || dmaDestination(packet) == DmaSelectGds, "DMA_DATA register or prefetch destination is not implemented"); + require(memorySelector(dmaSource(packet)) || dmaSource(packet) == 2 || dmaSource(packet) == DmaSelectGds, "DMA_DATA register source is not implemented"); + require(dmaSource(packet) != DmaSelectGds || (packet[3] == 0 && gdsRange(packet[2], packet[6] & 0x3ffffffu)), "DMA_DATA GDS source range exceeds the GDS"); + require(dmaDestination(packet) != DmaSelectGds || (packet[5] == 0 && gdsRange(packet[4], packet[6] & 0x3ffffffu)), "DMA_DATA GDS destination range exceeds the GDS"); require(dmaSource(packet) != 2 || packet[3] == 0, "DMA_DATA immediate exceeds 32 bits"); break; default: throw std::runtime_error("known packet has no validator"); @@ -451,11 +481,11 @@ std::optional ResolveStore(std::span packet, co return StoreWrite{destination, {}, copySource(address(packet[2], packet[3]), bytes, source >= 10)}; } case 0x50: { - if (packet.size() < 7) return std::nullopt; + if (packet.size() < 7 || dmaDestination(packet) == DmaSelectGds) return std::nullopt; const std::size_t bytes = packet[6] & 0x3ffffffu; const auto destination = address(packet[4], packet[5]); if (!fits(destination, bytes)) return std::nullopt; - return StoreWrite{destination, {}, copySource(address(packet[2], packet[3]), bytes, dmaSource(packet) == 2)}; + return StoreWrite{destination, {}, dmaSourceBytes(packet)}; } case 0x83: { if (packet.size() < 5 || packet[1] / 4 > queue.constantRam.size() || packet[2] > queue.constantRam.size() - packet[1] / 4) return std::nullopt; @@ -709,9 +739,21 @@ void Execute(std::span packet, QueueState& queue) { copyMemory(address(packet[2], packet[3]), address(packet[4], packet[5]), (packet[1] & 0x10000u) != 0 ? 8 : 4, source >= 10); return; } - case 0x50: - copyMemory(address(packet[2], packet[3]), address(packet[4], packet[5]), packet[6] & 0x3ffffffu, dmaSource(packet) == 2); + case 0x50: { + const std::size_t bytes = packet[6] & 0x3ffffffu; + if (bytes == 0) return; + auto data = dmaSourceBytes(packet); + if (dmaDestination(packet) == DmaSelectGds) { + auto& gds = Gds(); + std::lock_guard lock(gds.mutex); + std::copy(data.begin(), data.end(), gds.bytes.begin() + packet[4]); + return; + } + const auto destination = address(packet[4], packet[5]); + GuestMemory::CheckRange(reinterpret_cast(destination), bytes, 1, true); + GuestMemory::Write(destination, data); return; + } default: throw std::runtime_error("packet requires driver execution"); } } diff --git a/core/libs/prx/libSceAgcDriver/tests/Pm4.cpp b/core/libs/prx/libSceAgcDriver/tests/Pm4.cpp index 057638e04..e18b7baf1 100644 --- a/core/libs/prx/libSceAgcDriver/tests/Pm4.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/Pm4.cpp @@ -5,6 +5,7 @@ #include "prx/libSceAgcDriver/Submit/include/Dcb.hpp" #include "prx/libc/include/Shutdown.hpp" #include +#include #include #include #include @@ -270,7 +271,24 @@ void testCopies() { check(source == destination, "DMA_DATA copy failed"); execute(state, makePacket(0x50, {0x40000000, 0x44332211, 0, low(destination.data()), high(destination.data()), 6})); check(destination[0] == 0x44332211 && destination[1] == 0x00002211, "DMA_DATA byte fill failed"); - expectFailure([&] { execute(state, makePacket(0x50, {0x60100000, low(source.data()), high(source.data()), 0, 0, 4})); }, "GDS"); + constexpr std::uint32_t cachePolicies = (1u << 13u) | (2u << 25u); + const auto toGds = makePacket(0x50, {0x60100000 | cachePolicies, low(source.data()), high(source.data()), 0x100, 0, 16}); + check(!AgcDriver::Pm4::ResolveStore(toGds, state, 64).has_value(), "DMA_DATA to GDS resolved as a memory store"); + execute(state, toGds); + execute(state, makePacket(0x50, {0x20100000, 0x104, 0, 0xfff8, 0, 8})); + destination = {}; + execute(state, makePacket(0x50, {0x20000000 | cachePolicies, 0xfff8, 0, low(destination.data()), high(destination.data()), 8})); + check(destination[0] == 12 && destination[1] == 13 && destination[2] == 0, "DMA_DATA GDS to GDS round trip failed"); + const auto fromGds = makePacket(0x50, {0x20000000, 0x100, 0, low(destination.data()), high(destination.data()), 16}); + const auto store = AgcDriver::Pm4::ResolveStore(fromGds, state, 64); + check(store.has_value() && store->Bytes().size() == 16 && std::memcmp(store->Bytes().data(), source.data(), 16) == 0, "DMA_DATA from GDS did not resolve its source bytes"); + destination = {}; + execute(state, fromGds); + check(source == destination, "DMA_DATA GDS round trip failed"); + expectFailure([&] { execute(state, makePacket(0x50, {0x60100000, low(source.data()), high(source.data()), 0xfffc, 0, 8})); }, "exceeds the GDS"); + expectFailure([&] { execute(state, makePacket(0x50, {0x20000000, 0, 1, low(destination.data()), high(destination.data()), 4})); }, "exceeds the GDS"); + expectFailure([&] { execute(state, makePacket(0x50, {0x60200000, low(source.data()), high(source.data()), 0, 0, 4})); }, "destination is not implemented"); + expectFailure([&] { execute(state, makePacket(0x50, {0x60000000 | (1u << 15u), low(source.data()), high(source.data()), low(destination.data()), high(destination.data()), 4})); }, "reserved fields"); expectFailure([&] { execute(state, makePacket(0x37, {0x100, 0x1000, 0, 1})); }, "guest"); #ifdef _WIN32 auto* memory = VirtualAlloc(nullptr, 4096, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);