mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
feat(agc): implement the sceAgcBranchPatch setters
sceAgcBranchPatchSetCompareAddress, sceAgcBranchPatchSetThenTarget and sceAgcBranchPatchSetElseTarget rewrite the compare address and the two buffer targets of the packet sceAgcCbBranch emits. The then target is the first buffer and the else target the second, as in AMD's COND_INDIRECT_BUFFER packet. The cache policy bits are kept. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -87,6 +87,24 @@ if(BUILD_TESTING)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
add_test_executable(agc_branch_patch_tests EXCLUDE_FROM_ALL
|
||||
tests/BranchPatch.cpp
|
||||
Command/src/Packet.cpp
|
||||
Cb/src/Branch.cpp
|
||||
Patch/src/Branch.cpp
|
||||
)
|
||||
target_include_directories(agc_branch_patch_tests PRIVATE ${LIBS_INCLUDE_DIR})
|
||||
target_link_libraries(agc_branch_patch_tests PRIVATE libc)
|
||||
configure_windows_unwind(agc_branch_patch_tests)
|
||||
if(BUILD_TESTING)
|
||||
set_target_properties(agc_branch_patch_tests PROPERTIES EXCLUDE_FROM_ALL FALSE)
|
||||
add_test(NAME agc_branch_patch_tests COMMAND agc_branch_patch_tests)
|
||||
if(WIN32)
|
||||
set_tests_properties(agc_branch_patch_tests PROPERTIES ENVIRONMENT_MODIFICATION
|
||||
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
|
||||
endif()
|
||||
endif()
|
||||
|
||||
add_test_executable(agc_shader_fusion_tests EXCLUDE_FROM_ALL tests/ShaderFusion.cpp)
|
||||
target_include_directories(agc_shader_fusion_tests PRIVATE ${LIBS_INCLUDE_DIR})
|
||||
target_link_libraries(agc_shader_fusion_tests PRIVATE libSceAgc libc)
|
||||
|
||||
@@ -7,28 +7,38 @@
|
||||
#include "SceTypes.hpp"
|
||||
#include "prx/libc/include/General.hpp"
|
||||
|
||||
namespace {
|
||||
|
||||
void PatchBranchTarget(std::uint32_t* cmd, std::uint32_t field, const volatile std::uint32_t* target, std::uint32_t sizeInDwords, const char* function) {
|
||||
Agc::Command::ValidatePacket(cmd, 0x3fu, 14, function);
|
||||
const auto address = reinterpret_cast<std::uintptr_t>(target);
|
||||
Agc::Command::CheckAddress(address, 4, function);
|
||||
Agc::Command::CheckBits(sizeInDwords, 0xfffffu, function);
|
||||
cmd[field] = static_cast<std::uint32_t>(address);
|
||||
cmd[field + 1u] = static_cast<std::uint32_t>(address >> 32u);
|
||||
cmd[field + 2u] = (cmd[field + 2u] & ~0xfffffu) | sizeInDwords;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
|
||||
int APS5_VABI sceAgcBranchPatchSetCompareAddress(std::uint32_t* cmd, const volatile std::uint64_t* compareAddress) {
|
||||
(void)cmd;
|
||||
(void)compareAddress;
|
||||
NotImplemented_nid_no_patch(__func__);
|
||||
Agc::Command::ValidatePacket(cmd, 0x3fu, 14, __func__);
|
||||
const auto address = reinterpret_cast<std::uintptr_t>(compareAddress);
|
||||
Agc::Command::CheckAddress(address, 8, __func__);
|
||||
cmd[2] = static_cast<std::uint32_t>(address);
|
||||
cmd[3] = static_cast<std::uint32_t>(address >> 32u);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int APS5_VABI sceAgcBranchPatchSetThenTarget(std::uint32_t* cmd, const volatile std::uint32_t* target, std::uint32_t sizeInDwords) {
|
||||
(void)cmd;
|
||||
(void)target;
|
||||
(void)sizeInDwords;
|
||||
NotImplemented_nid_no_patch(__func__);
|
||||
PatchBranchTarget(cmd, 8, target, sizeInDwords, __func__);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int APS5_VABI sceAgcBranchPatchSetElseTarget(std::uint32_t* cmd, const volatile std::uint32_t* target, std::uint32_t sizeInDwords) {
|
||||
(void)cmd;
|
||||
(void)target;
|
||||
(void)sizeInDwords;
|
||||
NotImplemented_nid_no_patch(__func__);
|
||||
PatchBranchTarget(cmd, 11, target, sizeInDwords, __func__);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,122 @@
|
||||
#include "prx/libSceAgc/Command/include/Packet.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <stdexcept>
|
||||
|
||||
extern "C" {
|
||||
std::uint32_t* APS5_VABI sceAgcCbBranch(CommandBuffer*, std::uint8_t, std::uint8_t, const volatile std::uint64_t*, std::uint64_t, std::uint64_t, std::uint8_t, const volatile std::uint32_t*, std::uint32_t, std::uint8_t, const volatile std::uint32_t*, std::uint32_t);
|
||||
std::uint32_t APS5_VABI sceAgcCbBranchGetSize();
|
||||
int APS5_VABI sceAgcBranchPatchSetCompareAddress(std::uint32_t*, const volatile std::uint64_t*);
|
||||
int APS5_VABI sceAgcBranchPatchSetThenTarget(std::uint32_t*, const volatile std::uint32_t*, std::uint32_t);
|
||||
int APS5_VABI sceAgcBranchPatchSetElseTarget(std::uint32_t*, const volatile std::uint32_t*, std::uint32_t);
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
void check(bool condition, const char* message) {
|
||||
if (!condition) throw std::runtime_error(message);
|
||||
}
|
||||
|
||||
template <typename TAction>
|
||||
void expectFailure(TAction action) {
|
||||
try {
|
||||
action();
|
||||
} catch (const std::runtime_error&) {
|
||||
return;
|
||||
}
|
||||
throw std::runtime_error("expected invalid input to fail");
|
||||
}
|
||||
|
||||
const volatile std::uint64_t* compareAt(std::uint64_t address) {
|
||||
return reinterpret_cast<const volatile std::uint64_t*>(static_cast<std::uintptr_t>(address));
|
||||
}
|
||||
|
||||
const volatile std::uint32_t* targetAt(std::uint64_t address) {
|
||||
return reinterpret_cast<const volatile std::uint32_t*>(static_cast<std::uintptr_t>(address));
|
||||
}
|
||||
|
||||
struct Storage {
|
||||
std::array<std::uint32_t, 20> words{};
|
||||
CommandBuffer buffer{words.data(), words.data() + words.size(), words.data(), words.data() + words.size(), nullptr, nullptr, 0};
|
||||
std::uint32_t* packet = nullptr;
|
||||
|
||||
Storage() {
|
||||
words.fill(0xabcdef01u);
|
||||
packet = sceAgcCbBranch(&buffer, 2, 3, compareAt(0x0000123456789ab8ull), 0x1122334455667788ull, 0x99aabbccddeeff00ull, 1, targetAt(0x0000002233445564ull), 0x123u, 2, targetAt(0x0000006677889900ull), 0x456u);
|
||||
}
|
||||
};
|
||||
|
||||
void testWriter() {
|
||||
Storage storage;
|
||||
const std::array expected{0xc00c3f00u, 0x302u, 0x56789ab8u, 0x1234u, 0x55667788u, 0x11223344u, 0xddeeff00u, 0x99aabbccu, 0x33445564u, 0x22u, 0x10000123u, 0x77889900u, 0x66u, 0x20000456u};
|
||||
check(storage.packet == storage.words.data() && std::equal(expected.begin(), expected.end(), storage.packet), "incorrect branch packet");
|
||||
check(storage.buffer.cursor_up == storage.packet + expected.size() && sceAgcCbBranchGetSize() == expected.size() * sizeof(std::uint32_t), "branch size/cursor mismatch");
|
||||
check(storage.packet[expected.size()] == 0xabcdef01u, "branch command overwrote following word");
|
||||
}
|
||||
|
||||
void testCompareAddress() {
|
||||
Storage storage;
|
||||
auto expected = storage.words;
|
||||
expected[2] = 0xfedcba98u;
|
||||
expected[3] = 0x7654u;
|
||||
check(sceAgcBranchPatchSetCompareAddress(storage.packet, compareAt(0x00007654fedcba98ull)) == 0, "compare address setter failed");
|
||||
check(storage.words == expected, "compare address setter wrote the wrong words");
|
||||
expectFailure([&] { sceAgcBranchPatchSetCompareAddress(storage.packet, nullptr); });
|
||||
expectFailure([&] { sceAgcBranchPatchSetCompareAddress(storage.packet, compareAt(0x00007654fedcba9cull)); });
|
||||
expectFailure([&] { sceAgcBranchPatchSetCompareAddress(nullptr, compareAt(0x1000u)); });
|
||||
check(storage.words == expected, "invalid compare address modified the packet");
|
||||
}
|
||||
|
||||
template <typename TSetter>
|
||||
void testTarget(TSetter setter, std::size_t field, std::uint32_t cachePolicyBits, const char* name) {
|
||||
Storage storage;
|
||||
auto expected = storage.words;
|
||||
expected[field] = 0x89abcdecu;
|
||||
expected[field + 1] = 0x4567u;
|
||||
expected[field + 2] = cachePolicyBits | 0xfffffu;
|
||||
check(setter(storage.packet, targetAt(0x0000456789abcdecull), 0xfffffu) == 0, name);
|
||||
check(storage.words == expected, name);
|
||||
expected[field] = 0x1000u;
|
||||
expected[field + 1] = 0;
|
||||
expected[field + 2] = cachePolicyBits;
|
||||
check(setter(storage.packet, targetAt(0x1000u), 0) == 0, name);
|
||||
check(storage.words == expected, name);
|
||||
expectFailure([&] { setter(storage.packet, nullptr, 1); });
|
||||
expectFailure([&] { setter(storage.packet, targetAt(0x1002u), 1); });
|
||||
expectFailure([&] { setter(storage.packet, targetAt(0x2000u), 0x100000u); });
|
||||
expectFailure([&] { setter(nullptr, targetAt(0x2000u), 1); });
|
||||
check(storage.words == expected, name);
|
||||
}
|
||||
|
||||
void testMalformedPackets() {
|
||||
for (const auto header : {0xc0023f00u, 0xc00b3f00u, 0xc00c3f01u, 0xc00c2200u}) {
|
||||
Storage storage;
|
||||
storage.packet[0] = header;
|
||||
const auto malformed = storage.words;
|
||||
expectFailure([&] { sceAgcBranchPatchSetCompareAddress(storage.packet, compareAt(0x1000u)); });
|
||||
expectFailure([&] { sceAgcBranchPatchSetThenTarget(storage.packet, targetAt(0x2000u), 1); });
|
||||
expectFailure([&] { sceAgcBranchPatchSetElseTarget(storage.packet, targetAt(0x2000u), 1); });
|
||||
check(storage.words == malformed, "setter modified a packet that is not a branch");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int main() {
|
||||
try {
|
||||
testWriter();
|
||||
testCompareAddress();
|
||||
testTarget(sceAgcBranchPatchSetThenTarget, 8, 0x10000000u, "then target setter wrote the wrong words");
|
||||
testTarget(sceAgcBranchPatchSetElseTarget, 11, 0x20000000u, "else target setter wrote the wrong words");
|
||||
testMalformedPackets();
|
||||
std::puts("AGC branch patch tests passed");
|
||||
return 0;
|
||||
} catch (const std::exception& error) {
|
||||
std::fprintf(stderr, "%s\n", error.what());
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user