feat(libSceAgcDriver): execute COND_WRITE

COND_WRITE (opcode 0x45) with a memory poll and a memory destination is
validated and executed: the driver drains the queue like WRITE_DATA,
reads the poll dword through the range synchronization, compares it
masked with the reference like a 32-bit WAIT_REG_MEM, and writes the
data dword on a match. Register polls and register or scratch
destinations are rejected.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
hugodearaujo
2026-10-06 18:05:46 -03:00
parent 66da53663f
commit 159d06d27a
4 changed files with 100 additions and 4 deletions
@@ -41,6 +41,7 @@ inline constexpr Opcode Opcodes[] = {
{0x41, "CP_DMA"},
{0x42, "PFP_SYNC_ME"},
{0x43, "SURFACE_SYNC"},
{0x45, "COND_WRITE"},
{0x46, "EVENT_WRITE"},
{0x47, "EVENT_WRITE_EOP"},
{0x48, "EVENT_WRITE_EOS"},
@@ -163,7 +163,7 @@ bool Driver::preparePacketMemory(const Submission& submission, QueueState& queue
static const bool syncFlip = std::getenv("APS5_SYNC_FLIP") != nullptr;
const bool drains = drainAll ? ((Pm4::AccessesMemory(header) && opcode != 0x16) || opcode == 0x42 || opcode == 0x46 || opcode == 0x58 || header == FlipPacketHeader)
: (!wroteOnGpu && !orderedAlready && (opcode == 0x49 || opcode == 0x37 || opcode == 0x40 || opcode == 0x50 || opcode == 0x83 || sampleDump || (drawPacket && drawDrain) || (header == FlipPacketHeader && syncFlip)));
: (!wroteOnGpu && !orderedAlready && (opcode == 0x49 || opcode == 0x37 || opcode == 0x40 || opcode == 0x45 || opcode == 0x50 || opcode == 0x83 || sampleDump || (drawPacket && drawDrain) || (header == FlipPacketHeader && syncFlip)));
if (drains) {
static const bool unlockedDrain = std::getenv("APS5_NO_UNLOCKED_DRAIN") == nullptr && !drainAll;
@@ -167,7 +167,7 @@ std::string_view UnsupportedReason(std::uint32_t header) {
}
switch (opcode) {
case 0x11: case 0x12: case 0x13: case 0x15: case 0x16: case 0x20: case 0x22: case 0x26: case 0x27:
case 0x2a: case 0x2d: case 0x2f: case 0x35: case 0x37: case 0x40: case 0x42: case 0x46: case 0x50:
case 0x2a: case 0x2d: case 0x2f: case 0x35: case 0x37: case 0x40: case 0x42: case 0x45: case 0x46: case 0x50:
case 0x58: case 0x63: case 0x64: case 0x69: case 0x76: case 0x79: case 0x7a:
case 0x81: case 0x83: case 0x9f: return {};
case 0x24: case 0x25: case 0x2c: case 0x38:
@@ -382,6 +382,15 @@ void Validate(std::span<const std::uint32_t> packet, std::uint32_t queue) {
require((packet[2] & (opcode == 0x3c ? 3u : 7u)) == 0, "misaligned WAIT_REG_MEM address");
break;
}
case 0x45: {
size(9);
require((packet[1] & ~0x317u) == 0, "COND_WRITE reserved fields are not implemented");
require((packet[1] & 0x10u) != 0, "register-space COND_WRITE poll is not implemented");
require((packet[1] & 7u) <= 6u, "invalid COND_WRITE compare function");
require(((packet[1] >> 8u) & 3u) == 1u, "register or scratch COND_WRITE destination is not implemented");
require((packet[2] & 3u) == 0 && (packet[6] & 3u) == 0, "misaligned COND_WRITE address");
break;
}
case 0x49: {
size(8);
const auto dataSelect = packet[2] >> 29u;
@@ -579,7 +588,7 @@ bool UsesGpuCacheBarrier(std::span<const std::uint32_t> packet) {
bool AccessesMemory(std::uint32_t header) {
switch ((header >> 8u) & 0xffu) {
case 0x22: case 0x27: case 0x3c: case 0x93: case 0x49: case 0x16: case 0x2d: case 0x35: case 0x24: case 0x25: case 0x2c: case 0x38: case 0x37: case 0x40: case 0x50: case 0x63: case 0x64: case 0x83: case 0x9f: return true;
case 0x22: case 0x27: case 0x3c: case 0x93: case 0x49: case 0x16: case 0x2d: case 0x35: case 0x24: case 0x25: case 0x2c: case 0x38: case 0x37: case 0x40: case 0x45: case 0x50: case 0x63: case 0x64: case 0x83: case 0x9f: return true;
default: return false;
}
}
@@ -808,6 +817,15 @@ void Execute(std::span<const std::uint32_t> packet, QueueState& queue) {
case 0x83:
GuestMemory::Write(address(packet[3], packet[4]), std::as_bytes(std::span(queue.constantRam).subspan(packet[1] / 4, packet[2])), 4);
return;
case 0x45: {
std::uint32_t value = 0;
{
const GuestMemory::ReadSiteScope site(GuestMemory::ReadSite::Wait);
GuestMemory::Read(address(packet[2], packet[3]), std::as_writable_bytes(std::span(&value, 1)), 4);
}
if (waitCompares(packet, false, value)) GuestMemory::Write(address(packet[6], packet[7]), std::as_bytes(packet.subspan(8, 1)), 4);
return;
}
case 0x37: {
const auto destination = address(packet[2], packet[3]);
if ((packet[1] & 0x10000u) != 0) {
+78 -1
View File
@@ -59,7 +59,7 @@ void testCatalog() {
const auto reason = AgcDriver::Pm4::UnsupportedReason(packet[0]);
if (!reason.empty()) expectFailure([&] { AgcDriver::Pm4::Validate(packet, 0); }, std::string(reason).c_str());
}
check(values.size() == 54, "reference opcode catalog is incomplete");
check(values.size() == 55, "reference opcode catalog is incomplete");
expectFailure([] { AgcDriver::Pm4::Validate(makePacket(0xff, {0}), 0); }, "not known");
const std::array<std::pair<std::uint32_t, const char*>, 11> custom{{
{5, "DRAW_RESET"}, {6, "WAIT_FLIP_DONE"}, {9, "DISPATCH_RESET"}, {11, "PUSH_MARKER"},
@@ -780,6 +780,81 @@ void testPredicatedSubmission() {
AgcDriverWaitIdle_nid_postfix();
}
std::vector<std::uint32_t> condWrite(std::uint32_t control, const std::uint32_t& poll, std::uint32_t reference, std::uint32_t mask, std::uint32_t& target, std::uint32_t value) {
return makePacket(0x45, {control, low(&poll), high(&poll), reference, mask, low(&target), high(&target), value});
}
void testConditionalWrite() {
AgcDriver::QueueState state;
alignas(4) std::uint32_t poll = 0x1234u;
alignas(4) std::uint32_t target = 0;
const auto valid = condWrite(0x113, poll, 0x34, 0xff, target, 7);
check(AgcDriver::Pm4::Name(valid[0]) == "COND_WRITE" && valid[0] == 0xc0074500u, "COND_WRITE header or name mismatch");
check(AgcDriver::Pm4::UnsupportedReason(valid[0]).empty() && AgcDriver::Pm4::AccessesMemory(valid[0]), "COND_WRITE is rejected or does not synchronize guest memory");
const std::array<std::pair<std::uint32_t, bool>, 7> functions{{{0, true}, {1, false}, {2, true}, {3, true}, {4, false}, {5, true}, {6, false}}};
for (const auto& [function, writes] : functions) {
target = 0;
execute(state, condWrite(0x110 | function, poll, 0x34, 0xff, target, 9));
check(target == (writes ? 9u : 0u), "COND_WRITE compared the masked poll value incorrectly");
}
target = 0;
execute(state, condWrite(0x111, poll, 0x35, 0xff, target, 10));
check(target == 10, "COND_WRITE less-than did not write");
target = 0;
execute(state, condWrite(0x116, poll, 0x1233, 0xffffffffu, target, 11));
check(target == 11, "COND_WRITE greater-than with a full mask did not write");
expectFailure([&] { AgcDriver::Pm4::Validate(makePacket(0x45, {0x113, low(&poll), high(&poll), 0, 0, low(&target), high(&target)}), 0); }, "packet size");
expectFailure([&] { AgcDriver::Pm4::Validate(condWrite(0x2000113, poll, 0, 0, target, 0), 0); }, "reserved");
expectFailure([&] { AgcDriver::Pm4::Validate(condWrite(0x103, poll, 0, 0, target, 0), 0); }, "register-space");
expectFailure([&] { AgcDriver::Pm4::Validate(condWrite(0x117, poll, 0, 0, target, 0), 0); }, "compare function");
expectFailure([&] { AgcDriver::Pm4::Validate(condWrite(0x013, poll, 0, 0, target, 0), 0); }, "destination");
expectFailure([&] { AgcDriver::Pm4::Validate(condWrite(0x213, poll, 0, 0, target, 0), 0); }, "destination");
auto misaligned = condWrite(0x113, poll, 0, 0, target, 0);
misaligned[2] += 2;
expectFailure([&] { AgcDriver::Pm4::Validate(misaligned, 0); }, "misaligned");
misaligned = condWrite(0x113, poll, 0, 0, target, 0);
misaligned[6] += 1;
expectFailure([&] { AgcDriver::Pm4::Validate(misaligned, 0); }, "misaligned");
struct MemoryState {
std::uint32_t poll = 0;
std::uint32_t target = 0;
bool read = false;
bool written = false;
};
static MemoryState memory;
memory = {};
struct FlushHookReset {
~FlushHookReset() { AgcDriver::GuestMemory::SetFlushHook(nullptr); }
} reset;
AgcDriver::GuestMemory::SetFlushHook([](std::uint64_t address, std::size_t bytes) {
check(bytes == sizeof(std::uint32_t), "COND_WRITE resolved an unrelated range");
if (address == reinterpret_cast<std::uintptr_t>(&memory.target)) {
memory.written = true;
} else {
check(address == reinterpret_cast<std::uintptr_t>(&memory.poll), "COND_WRITE resolved an unrelated poll address");
memory.poll = 5;
memory.read = true;
}
});
execute(state, condWrite(0x113, memory.poll, 5, 0xffffffffu, memory.target, 12));
check(memory.read && memory.written && memory.target == 12, "COND_WRITE used a stale poll value or skipped synchronizing its target");
}
void testConditionalWriteSubmission() {
alignas(4) static std::uint32_t poll = 0;
static std::array<std::uint32_t, 2> results{};
results.fill(0);
poll = 0;
auto words = joinPackets({writeWord(poll, 5), condWrite(0x113, poll, 5, 0xffffffffu, results[0], 51), condWrite(0x114, poll, 5, 0xffffffffu, results[1], 52)});
submitWords(words);
AgcDriverWaitIdle_nid_postfix();
check(results[0] == 51, "COND_WRITE read its poll value before an earlier packet of its queue stored it");
check(results[1] == 0, "COND_WRITE wrote although its comparison failed");
}
void testAsyncMemoryFailure() {
auto commands = makePacket(0x37, {0x100, 0x1000, 0, 1});
Packet packet{commands.data(), static_cast<std::uint32_t>(commands.size()), 0, {}};
@@ -815,6 +890,8 @@ int main(int argc, char** argv) {
testConditionReadSynchronization();
testEventWrite();
testAcquireMem();
testConditionalWrite();
testConditionalWriteSubmission();
testPredication();
testDriverSubmission();
testPredicatedSubmission();