Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion core/libs/prx/libSceAgc/DcbState/src/ContextState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@
extern "C" {

uint32_t* APS5_VABI sceAgcDcbContextStateOp(CommandBuffer* buf, uint32_t operation) {
return sceAgcDcbContextStateOp_0100(buf, operation);
if (operation != 1) {
return sceAgcDcbContextStateOp_0100(buf, operation);
}
Agc::Command::Reserve(buf, 5, __func__);
auto* first = Agc::Command::Emit(buf, 0x28u, {0u, 0x80018001u}, __func__);
Agc::Command::Emit(buf, 0x12u, {operation}, __func__);
return first;
}

uint64_t APS5_VABI sceAgcDcbContextStateOpGetSize(uint32_t operation) {
Expand Down
21 changes: 21 additions & 0 deletions core/libs/prx/libSceAgc/tests/ContextStateSize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "prx/libc/include/general/VabiMacros.hpp"
#include "prx/libc/include/Shutdown.hpp"

#include <algorithm>
#include <array>
#include <cstdint>
#include <cstdio>
Expand Down Expand Up @@ -35,6 +36,9 @@ void testSizes() {
check(sceAgcDcbContextStateOp_0100(&buffer, operation) == words.data(), "context state op did not start at the cursor");
const auto written = static_cast<std::uint64_t>(buffer.cursor_up - words.data()) * sizeof(std::uint32_t);
check(written == sizes[operation], "context state size differs from the written packets");
if (operation == 1) {
continue;
}

std::array<std::uint32_t, 64> unversionedWords{};
CommandBuffer unversionedBuffer{unversionedWords.data(), unversionedWords.data() + unversionedWords.size(),
Expand All @@ -47,6 +51,22 @@ void testSizes() {
}
}

void testConsolePush() {
std::array<std::uint32_t, 8> words{};
words.fill(0xdeadbeefu);
CommandBuffer buffer{words.data(), words.data() + words.size(), words.data(), words.data() + words.size(), nullptr, nullptr, 0};
const std::array<std::uint32_t, 5> expected{0xc0012800u, 0, 0x80018001u, 0xc0001200u, 1};
check(sceAgcDcbContextStateOp(&buffer, 1) == words.data(), "console context push did not start at the cursor");
check(std::equal(expected.begin(), expected.end(), words.begin()), "console context push packet mismatch");
check(buffer.cursor_up == words.data() + expected.size() && words[expected.size()] == 0xdeadbeefu, "console context push cursor advance mismatch");
check(sceAgcDcbContextStateOpGetSize(1) >= expected.size() * sizeof(std::uint32_t), "console context push is larger than its reported size");
std::array<std::uint32_t, 4> small{};
CommandBuffer full{small.data(), small.data() + small.size(), small.data(), small.data() + small.size(), nullptr, nullptr, 0};
expectFailure([&] { sceAgcDcbContextStateOp(&full, 1); });
check(full.cursor_up == small.data(), "a refused console context push advanced the cursor");
expectFailure([] { sceAgcDcbContextStateOp(nullptr, 1); });
}

void testInvalidOperations() {
for (const std::uint32_t operation : {4u, 15u, 16u, 0xffu, 0x100u, 0x103u, 0xffffffffu}) {
check(sceAgcDcbContextStateOpGetSize(operation) == 0, "an invalid context state operation did not return size 0");
Expand All @@ -61,6 +81,7 @@ void testInvalidOperations() {
int main() {
try {
testSizes();
testConsolePush();
testInvalidOperations();
LibcRunShutdown_nid_postfix();
std::puts("AGC context state size tests passed");
Expand Down
35 changes: 20 additions & 15 deletions core/libs/prx/libSceAgcDriver/Execution/src/Pm4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ std::string_view UnsupportedReason(std::uint32_t header) {
}

constexpr std::uint32_t ConditionCachePolicy = 3u << 25u;
constexpr std::uint32_t ContextStateShadowControl = 0x80018001u;
constexpr std::uint32_t ConditionalWordsMask = 0x3fffu;
constexpr std::uint32_t DmaSourceCachePolicy = 3u << 13u;
constexpr std::uint32_t DmaDestinationCachePolicy = 3u << 25u;
Expand Down Expand Up @@ -380,7 +381,7 @@ void Validate(std::span<const std::uint32_t> packet, std::uint32_t queue) {
size(4);
require((packet[1] & 0xffff0000u) == 0, "CONTEXT_REG_RMW reserved offset bits are not implemented");
break;
case 0x12: graphics(); size(2); require((packet[1] & ~0xfu) == 0, "unsupported CLEAR_STATE payload bits"); break;
case 0x12: graphics(); size(2); require(packet[1] <= 3, "CLEAR_STATE commands above 3 are not implemented"); break;
case 0x20: {
graphics();
size(4);
Expand Down Expand Up @@ -460,7 +461,7 @@ void Validate(std::span<const std::uint32_t> packet, std::uint32_t queue) {
case 0x28:
graphics();
size(3);
require((packet[1] & 0x7fffffffu) == 0 && (packet[2] & 0x7fffffffu) == 0, "CONTEXT_CONTROL register loading and shadowing are not implemented");
require((packet[1] & 0x7fffffffu) == 0 && ((packet[2] & 0x7fffffffu) == 0 || packet[2] == ContextStateShadowControl), "CONTEXT_CONTROL register loading and shadowing are not implemented");
break;
case 0x46: {
const auto eventType = packet[1] & 0x3fu;
Expand Down Expand Up @@ -1162,6 +1163,21 @@ void executeIndexAttributes(const IndexAttributes& attributes, QueueState& queue
queue.indexType = attributes.words[3];
}

void ApplyContextState(std::uint32_t operation, QueueState& queue) {
switch (operation) {
case 0: queue.ClearContext(); break;
case 1: case 3:
require(!queue.savedContext.has_value(), "context state is already pushed");
queue.PushContext();
if (operation == 3) queue.ClearContext();
break;
case 2:
require(queue.savedContext.has_value(), "context state has not been pushed");
queue.PopContext();
break;
}
}

}

void Execute(std::span<const std::uint32_t> packet, QueueState& queue) {
Expand All @@ -1178,18 +1194,7 @@ void Execute(std::span<const std::uint32_t> packet, QueueState& queue) {
case 0x1a:
if (TraceContextState()) std::fprintf(stderr, "[context] op %u: %zu registers, saved %d, cb0 %x info %x mask %x\n", packet[1], queue.context.size(), queue.savedContext.has_value() ? 1 : 0, queue.context.contains(0x318) ? queue.context.at(0x318) : 0u, queue.context.contains(0x31c) ? queue.context.at(0x31c) : 0u, queue.context.contains(0x8e) ? queue.context.at(0x8e) : 0u);
APS5_LOG_OUT_DEBUG("CONTEXT_STATE operation=%u targetMaskBefore=0x%x shaderMaskBefore=0x%x", packet[1], queue.context.contains(0x8e) ? queue.context.at(0x8e) : 0u, queue.context.contains(0x8f) ? queue.context.at(0x8f) : 0u);
switch (packet[1]) {
case 0: queue.ClearContext(); break;
case 1: case 3:
require(!queue.savedContext.has_value(), "context state is already pushed");
queue.PushContext();
if (packet[1] == 3) queue.ClearContext();
break;
case 2:
require(queue.savedContext.has_value(), "context state has not been pushed");
queue.PopContext();
break;
}
ApplyContextState(packet[1], queue);
APS5_LOG_OUT_DEBUG("CONTEXT_STATE done operation=%u targetMaskAfter=0x%x shaderMaskAfter=0x%x", packet[1], queue.context.contains(0x8e) ? queue.context.at(0x8e) : 0u, queue.context.contains(0x8f) ? queue.context.at(0x8f) : 0u);
return;
default: throw std::runtime_error("custom packet requires driver execution");
Expand All @@ -1199,7 +1204,7 @@ void Execute(std::span<const std::uint32_t> packet, QueueState& queue) {
return;
case 0x12:
APS5_LOG_OUT_DEBUG("CLEAR_STATE targetMaskBefore=0x%x shaderMaskBefore=0x%x", queue.context.contains(0x8e) ? queue.context.at(0x8e) : 0u, queue.context.contains(0x8f) ? queue.context.at(0x8f) : 0u);
queue.ClearContext(); return;
ApplyContextState(packet[1], queue); return;
case 0x13: queue.indexBufferSize = packet[1]; return;
case 0x20: {
const auto operation = (packet[1] >> 16u) & 7u;
Expand Down
15 changes: 15 additions & 0 deletions core/libs/prx/libSceAgcDriver/tests/Pm4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ void testRegisters() {
expectFailure([&] { execute(state, makePacket(0x7a, {0x20000242, 1})); }, "bank selection");
execute(state, makePacket(0x28, {0x80000000, 0x80000000}));
execute(state, makePacket(0x28, {0, 0}));
execute(state, makePacket(0x28, {0, 0x80018001u}));
expectFailure([&] { AgcDriver::Pm4::Validate(makePacket(0x28, {0, 0x80018003u}), 0); }, "shadowing");
expectFailure([&] { AgcDriver::Pm4::Validate(makePacket(0x28, {0x80000001, 0x80000000}), 0); }, "shadowing");
expectFailure([&] { AgcDriver::Pm4::Validate(makePacket(0x28, {0x80000000, 0x80000002}), 0); }, "shadowing");
expectFailure([&] { AgcDriver::Pm4::Validate(makePacket(0x28, {0x80000000, 0x80000000}), 0x20); }, "compute queue");
Expand Down Expand Up @@ -268,6 +270,19 @@ void testContextAndBases() {
check(state.context.at(0x280) == 0x00080008u && state.context.at(0x281) == 0xffff0000u, "context pop did not restore saved point size and limits");
check(state.context.at(0x10) == 17, "pop did not restore context");
expectFailure([&] { execute(state, makePacket(0x10, {2, 0}, 0x68)); }, "not been pushed");
execute(state, makePacket(0x69, {0x10, 23}));
execute(state, makePacket(0x12, {1}));
check(state.savedContext.has_value() && state.context.at(0x10) == 23, "CLEAR_STATE push changed the context or did not save it");
expectFailure([&] { execute(state, makePacket(0x12, {1})); }, "already pushed");
execute(state, makePacket(0x69, {0x10, 29}));
execute(state, makePacket(0x12, {2}));
check(!state.savedContext.has_value() && state.context.at(0x10) == 23, "CLEAR_STATE pop did not restore the pushed context");
expectFailure([&] { execute(state, makePacket(0x12, {2})); }, "not been pushed");
execute(state, makePacket(0x12, {3}));
check(state.savedContext.has_value() && state.context == AgcDriver::ClearStateContextRegisters(), "CLEAR_STATE push-clear did not save and clear the context");
execute(state, makePacket(0x12, {2}));
check(state.context.at(0x10) == 23, "pop after CLEAR_STATE push-clear did not restore the context");
for (const auto command : {4u, 0xfu}) expectFailure([&] { AgcDriver::Pm4::Validate(makePacket(0x12, {command}), 0); }, "commands above 3");
alignas(8) std::array<std::uint32_t, 4> arguments{7, 8, 9, 0};
execute(state, makePacket(0x11, {1, low(arguments.data()), high(arguments.data())}, 2));
auto packet = makePacket(0x16, {0, 0x8041});
Expand Down
6 changes: 5 additions & 1 deletion core/libs/prx/libSceAgcDriver/tests/Submit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,14 @@ void testClearState() {
packet.dw_num = 1;
expectFailure([&] { sceAgcDriverSubmitDcb(&packet); });
packet.dw_num = 2;
for (std::uint32_t state = 0; state <= 0xf; ++state) {
for (const std::uint32_t state : {0u, 1u, 2u, 3u, 2u}) {
words[1] = state;
check(sceAgcDriverSubmitDcb(&packet) == 0, "CLEAR_STATE submit failed");
}
for (const std::uint32_t state : {4u, 0xfu}) {
words[1] = state;
expectFailure([&] { sceAgcDriverSubmitDcb(&packet); });
}
AgcDriverWaitIdle_nid_postfix();
}

Expand Down
3 changes: 1 addition & 2 deletions docs/dev/debt/libSceAcm.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

### Additional callable exports

- [Additional callable exports](../../../core/libs/prx/libSceAcm/Export.cpp) - 4 entries present in both version-labelled images are import-resolution declarations only. Neither label independently authenticates retail firmware provenance. Parameter lists, return types and behavior are unverified; parameterless `int` declarations are placeholders, not verified ABI contracts. Every entry throws through `NotImplemented_nid_no_patch`. Unidentified names use literal NID aliases. No runtime functionality is implemented. Data exports require separate layout and ownership analysis. Identified candidate names match their NIDs in the [public symbol database](https://github.com/zecoxao/sce_symbols/blob/2883963a0a514ba6e77407a08ef4730a65e88254/aerolib.csv) or [supplemental catalog](https://github.com/claimore22/ps5rs/blob/2304be5131221bae0dac24b9767cabffda33f747/data/nids.csv).
- [Unnamed firmware 13.42 exports](../../../core/libs/prx/libSceAcm/Export.cpp) - 44 function NIDs that firmware 13.42's `system/common/lib/libSceAcm.sprx` exports (read from its export table) are import-resolution declarations only. The [firmware 13.60 inventory](https://github.com/xdCloudy/PS5-13.60-Function-Stub-Library/blob/bfec40deb4c47a17d7603ee2bc79aa6862c0c56b/data/firmware-13.60/libsceacm-a3566cc281.jsonl) lists the same NIDs as defined function exports with no name, and none is in the [public symbol database](https://github.com/zecoxao/sce_symbols/blob/2883963a0a514ba6e77407a08ef4730a65e88254/aerolib.csv). Names, parameter lists, return types and behavior are unknown; parameterless `int` declarations are placeholders, not verified ABI contracts, and every entry throws through `NotImplemented_nid_no_patch`.

| Name | NID |
| --- | --- |
Expand All @@ -51,7 +51,6 @@

- [Unknown NID `9+f3tPdeyP0`](../../../core/libs/prx/libSceAcm/Export.cpp) (libSceAcm) - its name and the structure it advances are unknown; the parameter and result types come from the body, not a prototype, and nothing is claimed about the object beyond the fields used. Firmware 13.42 `libSceAcm.sprx` (`0x8190`, disassembly only, not run on a console) returns null for a null argument. Otherwise it reads a base pointer at `+0` and 32-bit fields at `+8`, `+0xc` and `+0x10`, raises `+0xc` to at most `+8`, moves `+0x10` to the next slot (0 after an empty state, 0 again at `+8`) and returns the base plus 1024 bytes per slot, with the slot shifted by 8 in 32 bits. Only null is checked; a bad pointer faults there too.

- [Unnamed firmware 13.42 exports](../../../core/libs/prx/libSceAcm/Export.cpp) - 44 function NIDs that firmware 13.42's `system/common/lib/libSceAcm.sprx` exports (read from its export table) are import-resolution declarations only. The [firmware 13.60 inventory](https://github.com/xdCloudy/PS5-13.60-Function-Stub-Library/blob/bfec40deb4c47a17d7603ee2bc79aa6862c0c56b/data/firmware-13.60/libsceacm-a3566cc281.jsonl) lists the same NIDs as defined function exports with no name, and none is in the [public symbol database](https://github.com/zecoxao/sce_symbols/blob/2883963a0a514ba6e77407a08ef4730a65e88254/aerolib.csv). Names, parameter lists, return types and behavior are unknown; parameterless `int` declarations are placeholders, not verified ABI contracts, and every entry throws through `NotImplemented_nid_no_patch`.

| Name | NID |
| --- | --- |
Expand Down
1 change: 1 addition & 0 deletions docs/dev/debt/libSceAgc.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
- [sceAgcDcbAtomicGds_0900](../../../core/libs/prx/libSceAgc/DcbFlow/src/Control.cpp) (libSceAgc) - and `sceAgcDcbAtomicGds` (forwards to it with control bit 7 cleared; the unversioned exports mask the op to seven bits) - the leading `engine` argument (ENGINE_SEL, bits 30-31) and the register/stack argument order are not measured on a console; the 11-dword ATOMIC_GDS (0x1d) layout and control/mode bit mapping follow the clean-room [OpenAGC](https://github.com/OpenAGC/OpenAGC/blob/3c7e8072f0d7684e930454794fd5c02056946d73/src/game_compat.c) only; KytyPS5 and shadPS4 have no body for it. `sceAgcAcbAtomicGds` (forwards to `sceAgcAcbAtomicGds_0900` with control bit 7 cleared) takes the same arguments. Not confirmed on a title, and the driver has no ATOMIC_GDS handler
- [sceAgcGetFusedShaderSize](../../../core/libs/prx/libSceAgc/Misc/src/ShaderFusion.cpp) (libSceAgc) - follows the firmware body (read from disassembly, not measured on a console): it checks the halves and writes `back->num_sh_registers * 8` and a 32-bit alignment of 4 itself, without the geometry layout `sceAgcGetFusedShaderSize_0080` computes. The firmware dereferences null pointers; here a null shader, or a null output with valid halves, is rejected instead
- [sceAgcGetIsTrinityMode](../../../core/libs/prx/libSceAgc/Misc/src/Platform.cpp) (libSceAgc) - signature from PPSA26344 (a pointer to a one-byte flag, result ignored); a null or unwritable output pointer throws (rejecting unwritable memory is a host-safety fallback; console fault handling is unmeasured). The SDK 9.04 module stores `sceKernelIsTrinityMode`'s byte result through the pointer and returns it without an error path, so a console faults on null. Six more disassembled titles (PPSA01494, PPSA18194, PPSA21564, PPSA30839, PPSA31162, PPSA34502) call it once from an initializer with a static byte and check `flag != 0` without passing null
- [sceAgcDcbContextStateOp](../../../core/libs/prx/libSceAgc/DcbState/src/ContextState.cpp) (libSceAgc) - operation 1 writes the console's `CONTEXT_CONTROL` (load 0, shadow `0x80018001`) and `CLEAR_STATE` 1 and returns the first packet, as measured by `agcbuild_DcbContextStateOp_b`; `sceAgcDcbContextStateOpGetSize(1)` still reports KytyPS5's 108 bytes, more than the 20 written. Operations 0, 2 and 3 delegate to `sceAgcDcbContextStateOp_0100` (same packets as KytyPS5's `AgcDcbContextStateOp`) and were not compared with a real console. For operations above 3 the console writes only `CLEAR_STATE` with the low bits of the operation and returns null (`_a`, `_c`: 1 and 2, which do not tell a 2-bit from a 4-bit mask); here it throws
- [sceAgcDcbSet{Cf,Cx,Sh,Uc}RegisterDirect, sceAgcCbSet{Sh,Uc}RegisterRangeDirect](../../../core/libs/prx/libSceAgc/DcbState/src/Registers.cpp) (libSceAgc) - follow prosper's console measurements (`agcbuild_DcbSet{Cf,Cx,Sh,Uc}RegisterDirect_a/c`, `agcbuild_CbSet{Sh,Uc}RegisterRangeDirect_c`): a direct write keeps the low bits of the register offset (0x11111111 wrote 0x1111, 0x22222222 wrote 0x2222), and a CB range with no values writes its header and offset only. The measured offsets never set bits 14 or 15, so a 14-bit mask would fit them too; the 16-bit field width is used. Not measured: offset 0xffffffff, which the driver treats as naming no register; a `sceAgcDcbSetCfRegisterRangeDirect` with no values, which still throws; and a range with values whose offset does not fit, which still throws.
- [sceAgcDcbContextStateOp](../../../core/libs/prx/libSceAgc/DcbState/src/ContextState.cpp) (libSceAgc) - delegates to `sceAgcDcbContextStateOp_0100` (same packets as KytyPS5's `AgcDcbContextStateOp`); KytyPS5 returns nullptr for an operation above 3, here it throws; the packet words were not compared with a real console
- [sceAgcBranchPatchSetThenTarget_0300, sceAgcBranchPatchSetElseTarget_0300](../../../core/libs/prx/libSceAgc/Patch/src/Branch.cpp) (libSceAgc) - names from their NIDs (`7Wa3aeJgeVU`, `rP5xLdOf26k`); the `(packet, cachePolicy, target, sizeInDwords)` signature is inferred from PPSA28416's calls (a branch packet, 0, an aligned target, 4), with the policy stored where `sceAgcCbBranch` puts it
Expand Down
Loading
Loading