mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
Merge pull request #61 from oneandonlydean/feat/agc-dma-gds
feat(agc): DMA_DATA cache policy hints and GDS source/destination - **Cache policy:** the source (bits 13–14) and destination (bits 25–26) cache-policy fields of `DMA_DATA` dword 1 are accepted. They're cache hints with no effect on the copied data. The remaining reserved bits are still rejected. - **GDS storage:** GDS is modelled as one 64 KiB, zero-initialised store shared by the whole device, behind a mutex. - **GDS source/destination:** select `1` reads from or writes to that store: - the validator requires the high address dword to be 0 and the range to fit inside the 64 KiB; - `Execute` copies between memory, immediate data and GDS in any combination; - `ResolveStore` returns nothing for a GDS destination, since there's no guest memory to write, and for a GDS source it resolves the bytes from the store. - Register and prefetch destinations are still rejected. - Nothing is faked for shaders: a shader that uses GDS still fails validation (unsupported descriptor role).
This commit is contained in:
@@ -94,6 +94,31 @@ std::vector<std::byte> 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<std::byte, GdsBytes> 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<std::byte> dmaSourceBytes(std::span<const std::uint32_t> 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<void*>(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<const std::uint32_t> packet, std::uint32_t queue) {
|
||||
require(!packet.empty(), "truncated PM4 header");
|
||||
const auto header = packet[0];
|
||||
@@ -355,9 +383,11 @@ void Validate(std::span<const std::uint32_t> 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<StoreWrite> ResolveStore(std::span<const std::uint32_t> 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<const std::uint32_t> 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<void*>(destination), bytes, 1, true);
|
||||
GuestMemory::Write(destination, data);
|
||||
return;
|
||||
}
|
||||
default: throw std::runtime_error("packet requires driver execution");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include "prx/libSceAgcDriver/Submit/include/Dcb.hpp"
|
||||
#include "prx/libc/include/Shutdown.hpp"
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <limits>
|
||||
#include <set>
|
||||
#include <stdexcept>
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user