From 636fb5edae5d972da56990daf83a4b43488e37a2 Mon Sep 17 00:00:00 2001 From: Chad Date: Mon, 5 Oct 2026 15:02:30 -0500 Subject: [PATCH] feat(libs): implement the AMPR 04_00 counter commands and the construct helpers sceAmprCommandBufferWaitOnCounter_04_00 and WriteCounter_04_00 take the counter access size and offset, the wait mask and the write operation, with their measure functions; ConstructNop records a nop with an optional leading word and a payload of up to 60 bytes, and ConstructMarker records the set, push and pop markers by type. Signatures and checks follow ampr_emu. The executor now reads and writes counters by access: the 8-byte access of counter n spans n and n + 1, the 2- and 1-byte accesses a half or a byte of the counter. Waits compare at the access width, so the signed and wrapped compares see the field's own sign bit, and the atomic writes wrap within the field. The legacy counter commands record a 4-byte access. Co-Authored-By: Claude Opus 5.5 --- core/libs/prx/libSceAmpr/Export.cpp | 68 ++++++++--- .../Apr/include/AprCommandBuffer.hpp | 28 ++++- core/libs/prx/libkernel/Apr/src/Apr.cpp | 63 +++++++++- core/libs/tests/GuestAmpr.cpp | 114 ++++++++++++++++++ docs/dev/TechnicalDebt.md | 1 + 5 files changed, 248 insertions(+), 26 deletions(-) diff --git a/core/libs/prx/libSceAmpr/Export.cpp b/core/libs/prx/libSceAmpr/Export.cpp index df54ef647..f5bb93e89 100644 --- a/core/libs/prx/libSceAmpr/Export.cpp +++ b/core/libs/prx/libSceAmpr/Export.cpp @@ -36,6 +36,14 @@ static bool ValidWait(std::uint32_t compare, std::uint32_t flush) { return compare < 4u && flush < 2u; } +static bool ValidWaitOnCounter_04_00(std::uint32_t access, std::uint32_t compare, std::uint32_t maskOperation, std::uint32_t flush) { + return access < 8u && compare <= 6u && maskOperation < 2u && flush < 2u; +} + +static bool ValidWriteCounter_04_00(std::uint32_t counter, std::uint32_t access, std::uint32_t operation) { + return (counter & 0x80u) == 0u && access < 8u && operation < 5u; +} + static constexpr std::uint64_t MeasureInvalid = static_cast(SCE_KERNEL_ERROR_EINVAL); static std::uint64_t NopBytes(std::uint32_t dwords) { @@ -88,7 +96,7 @@ int APS5_VABI sceAmprCommandBufferWriteAddressOnCompletion(Apr::CommandBufferObj int APS5_VABI sceAmprCommandBufferWriteCounterOnCompletion(Apr::CommandBufferObject* buffer, std::uint8_t counter, std::uint32_t value) { if (!ValidCounter(counter)) return SCE_KERNEL_ERROR_EINVAL; - return AppendCommand(buffer, Apr::Opcode::WriteCounter, Apr::WriteCounterCommand{{}, counter, value}); + return AppendCommand(buffer, Apr::Opcode::WriteCounter, Apr::WriteCounterCommand{{}, counter, Apr::CounterAccess::Size4, Apr::CounterOperation::Store, 0, value}); } int APS5_VABI sceAmprCommandBufferWaitOnAddress(Apr::CommandBufferObject* buffer, volatile std::uint64_t* address, std::uint64_t reference, std::uint8_t compare, std::uint8_t flush) { @@ -98,7 +106,7 @@ int APS5_VABI sceAmprCommandBufferWaitOnAddress(Apr::CommandBufferObject* buffer int APS5_VABI sceAmprCommandBufferWaitOnCounter(Apr::CommandBufferObject* buffer, std::uint8_t counter, std::uint32_t reference, std::uint8_t compare, std::uint8_t flush) { if (!ValidCounter(counter) || !ValidWait(compare, flush)) return SCE_KERNEL_ERROR_EINVAL; - return AppendCommand(buffer, Apr::Opcode::WaitOnCounter, Apr::WaitCommand{{}, 0, reference, ~0ull, counter, compare}); + return AppendCommand(buffer, Apr::Opcode::WaitOnCounter, Apr::WaitCommand{{}, 0, reference, ~0ull, counter, compare, Apr::CounterAccess::Size4, 0}); } int APS5_VABI sceAmprCommandBufferWriteKernelEventQueueOnCompletion(Apr::CommandBufferObject* buffer, std::uint64_t equeue, std::int32_t ident, std::uint64_t data) { @@ -196,14 +204,32 @@ int APS5_VABI sceAmprCommandBufferClearBuffer(Apr::CommandBufferObject* buffer) return 0; } -int APS5_VABI sceAmprCommandBufferConstructMarker() { - NotImplemented_nid_no_patch(__func__); - return 0; +int APS5_VABI sceAmprCommandBufferConstructMarker(Apr::CommandBufferObject* buffer, std::uint32_t type, const char* text, const std::uint32_t* color) { + switch (type) { + case 1: return AppendMarker(buffer, Apr::Opcode::SetMarker, text); + case 2: return AppendMarker(buffer, Apr::Opcode::PushMarker, text); + case 3: return AppendCommand(buffer, Apr::Opcode::PopMarker, Apr::MarkerCommand{}); + case 5: return color ? AppendMarker(buffer, Apr::Opcode::SetMarker, text) : SCE_KERNEL_ERROR_EINVAL; + case 6: return color ? AppendMarker(buffer, Apr::Opcode::PushMarker, text) : SCE_KERNEL_ERROR_EINVAL; + default: return SCE_KERNEL_ERROR_EINVAL; + } } -int APS5_VABI sceAmprCommandBufferConstructNop() { - NotImplemented_nid_no_patch(__func__); - return 0; +int APS5_VABI sceAmprCommandBufferConstructNop(Apr::CommandBufferObject* buffer, std::int16_t type, const void* payload, std::uint32_t payloadBytes, const std::uint32_t* word) { + (void)type; + if (payloadBytes > (word ? 56u : 60u)) return SCE_KERNEL_ERROR_EINVAL; + if (payloadBytes != 0u && !payload) throw std::invalid_argument("sceAmprCommandBufferConstructNop: null payload"); + const std::uint32_t dwords = (payloadBytes + 3u) / 4u + (word ? 1u : 0u); + const std::uint32_t offset = buffer ? buffer->offset : 0u; + const int result = AppendNop(buffer, dwords, nullptr); + if (result != 0) return result; + std::uint8_t* data = buffer->base + offset + sizeof(Apr::CommandHeader); + if (word) { + std::memcpy(data, word, sizeof(*word)); + data += sizeof(*word); + } + if (payloadBytes != 0u) std::memcpy(data, payload, payloadBytes); + return 0; } int APS5_VABI sceAmprCommandBufferConstructor(Apr::CommandBufferObject* buffer) { @@ -288,9 +314,10 @@ int APS5_VABI sceAmprCommandBufferWaitOnAddress_04_00(Apr::CommandBufferObject* return AppendCommand(buffer, Apr::Opcode::WaitOnAddress, Apr::WaitCommand{{}, reinterpret_cast(address), reference, ~0ull, 0, compare}); } -int APS5_VABI sceAmprCommandBufferWaitOnCounter_04_00() { - NotImplemented_nid_no_patch(__func__); - return 0; +int APS5_VABI sceAmprCommandBufferWaitOnCounter_04_00(Apr::CommandBufferObject* buffer, std::uint8_t counter, std::uint8_t access, std::uint64_t reference, std::uint8_t compare, std::uint8_t maskOperation, std::uint64_t mask, std::uint8_t flush) { + if (!ValidWaitOnCounter_04_00(access, compare, maskOperation, flush)) return SCE_KERNEL_ERROR_EINVAL; + const std::uint64_t applied = maskOperation ? mask : ~0ull; + return AppendCommand(buffer, Apr::Opcode::WaitOnCounter, Apr::WaitCommand{{}, 0, reference, applied, counter, compare, static_cast(access), 0}); } int APS5_VABI sceAmprCommandBufferWriteAddressFromCounterPair_04_00(Apr::CommandBufferObject* buffer, volatile std::uint64_t* address, std::uint8_t counter, std::uint64_t atStart) { @@ -317,9 +344,10 @@ int APS5_VABI sceAmprCommandBufferWriteAddress_04_00(Apr::CommandBufferObject* b return Append(buffer, &command, sizeof(command)); } -int APS5_VABI sceAmprCommandBufferWriteCounter_04_00() { - NotImplemented_nid_no_patch(__func__); - return 0; +int APS5_VABI sceAmprCommandBufferWriteCounter_04_00(Apr::CommandBufferObject* buffer, std::uint8_t counter, std::uint8_t access, std::uint64_t value, std::uint8_t operation, std::uint8_t atStart) { + (void)atStart; + if (!ValidWriteCounter_04_00(counter, access, operation)) return SCE_KERNEL_ERROR_EINVAL; + return AppendCommand(buffer, Apr::Opcode::WriteCounter, Apr::WriteCounterCommand{{}, counter, static_cast(access), static_cast(operation), 0, value}); } int APS5_VABI sceAmprCommandBufferWriteKernelEventQueue_04_00(Apr::CommandBufferObject* buffer, std::uint64_t equeue, std::int32_t ident, std::uint64_t data, std::uint64_t atStart) { @@ -403,9 +431,9 @@ std::uint64_t APS5_VABI sceAmprMeasureCommandSizeWaitOnAddress_04_00(volatile st return sizeof(Apr::WaitCommand); } -int APS5_VABI sceAmprMeasureCommandSizeWaitOnCounter_04_00() { - NotImplemented_nid_no_patch(__func__); - return 0; +std::uint64_t APS5_VABI sceAmprMeasureCommandSizeWaitOnCounter_04_00(std::uint8_t, std::uint8_t access, std::uint64_t, std::uint8_t compare, std::uint8_t maskOperation, std::uint64_t, std::uint8_t flush) { + if (!ValidWaitOnCounter_04_00(access, compare, maskOperation, flush)) return MeasureInvalid; + return sizeof(Apr::WaitCommand); } std::uint64_t APS5_VABI sceAmprMeasureCommandSizeWriteAddressFromCounterPair_04_00(volatile std::uint64_t* address, std::uint8_t counter) { @@ -428,9 +456,9 @@ uint32_t APS5_VABI sceAmprMeasureCommandSizeWriteAddress_04_00(void) { return sizeof(Apr::WriteAddressCommand); } -int APS5_VABI sceAmprMeasureCommandSizeWriteCounter_04_00() { - NotImplemented_nid_no_patch(__func__); - return 0; +std::uint64_t APS5_VABI sceAmprMeasureCommandSizeWriteCounter_04_00(std::uint8_t counter, std::uint8_t access, std::uint64_t, std::uint8_t operation) { + if (!ValidWriteCounter_04_00(counter, access, operation)) return MeasureInvalid; + return sizeof(Apr::WriteCounterCommand); } std::uint64_t APS5_VABI sceAmprMeasureCommandSizeWriteKernelEventQueue_04_00(std::uint64_t, std::int32_t, std::uint64_t) { diff --git a/core/libs/prx/libkernel/Apr/include/AprCommandBuffer.hpp b/core/libs/prx/libkernel/Apr/include/AprCommandBuffer.hpp index ecdbd67e6..1ac5932a8 100644 --- a/core/libs/prx/libkernel/Apr/include/AprCommandBuffer.hpp +++ b/core/libs/prx/libkernel/Apr/include/AprCommandBuffer.hpp @@ -60,10 +60,32 @@ struct WriteAddressCommand { std::uint32_t reserved; }; +enum class CounterAccess : std::uint8_t { + Size8 = 0, + Size4 = 1, + Size2Offset0 = 2, + Size2Offset1 = 3, + Size1Offset0 = 4, + Size1Offset1 = 5, + Size1Offset2 = 6, + Size1Offset3 = 7, +}; + +enum class CounterOperation : std::uint8_t { + Store = 0, + AtomicOr = 1, + AtomicAndComplement = 2, + AtomicXor = 3, + AtomicAdd = 4, +}; + struct WriteCounterCommand { CommandHeader header; std::uint32_t counter; - std::uint32_t value; + CounterAccess access; + CounterOperation operation; + std::uint16_t reserved; + std::uint64_t value; }; struct WaitCommand { @@ -72,7 +94,9 @@ struct WaitCommand { std::uint64_t reference; std::uint64_t mask; std::uint32_t counter; - std::uint32_t compare; + std::uint8_t compare; + CounterAccess access; + std::uint16_t reserved; }; struct WriteKernelEventQueueCommand { diff --git a/core/libs/prx/libkernel/Apr/src/Apr.cpp b/core/libs/prx/libkernel/Apr/src/Apr.cpp index 9152f9419..79ab4fde2 100644 --- a/core/libs/prx/libkernel/Apr/src/Apr.cpp +++ b/core/libs/prx/libkernel/Apr/src/Apr.cpp @@ -161,10 +161,63 @@ void _writeAddress(const Apr::WriteAddressCommand& command) { std::array, 256> g_counters{}; +std::mutex g_counterWrites; + std::uint32_t _counter(std::uint32_t index) { return g_counters[index % g_counters.size()].load(std::memory_order_acquire); } +struct CounterField { + std::uint32_t bits; + std::uint32_t shift; +}; + +CounterField _counterField(Apr::CounterAccess access) { + const auto value = static_cast(access); + if (value == 0) return {64, 0}; + if (value == 1) return {32, 0}; + if (value < 4) return {16, (value - 2) * 16}; + if (value < 8) return {8, (value - 4) * 8}; + throw std::runtime_error("APR: counter access " + std::to_string(value) + " not implemented"); +} + +std::uint64_t _fieldMask(std::uint32_t bits) { + return bits == 64 ? ~std::uint64_t{0} : (std::uint64_t{1} << bits) - 1u; +} + +std::uint64_t _readCounter(std::uint32_t index, Apr::CounterAccess access) { + const auto field = _counterField(access); + if (field.bits == 64) return _counter(index) | static_cast(_counter(index + 1u)) << 32u; + return (_counter(index) >> field.shift) & _fieldMask(field.bits); +} + +std::uint64_t _applyCounterOperation(Apr::CounterOperation operation, std::uint64_t current, std::uint64_t value) { + switch (operation) { + case Apr::CounterOperation::Store: return value; + case Apr::CounterOperation::AtomicOr: return current | value; + case Apr::CounterOperation::AtomicAndComplement: return current & ~value; + case Apr::CounterOperation::AtomicXor: return current ^ value; + case Apr::CounterOperation::AtomicAdd: return current + value; + } + throw std::runtime_error("APR: counter operation " + std::to_string(static_cast(operation)) + " not implemented"); +} + +void _writeCounter(const Apr::WriteCounterCommand& command) { + const auto field = _counterField(command.access); + const std::lock_guard lock(g_counterWrites); + const std::uint64_t current = _readCounter(command.counter, command.access); + const std::uint64_t next = _applyCounterOperation(command.operation, current, command.value) & _fieldMask(field.bits); + auto& low = g_counters[command.counter % g_counters.size()]; + if (field.bits == 64) { + g_counters[(command.counter + 1u) % g_counters.size()].store(static_cast(next >> 32u), std::memory_order_release); + low.store(static_cast(next), std::memory_order_release); + return; + } + const auto mask = static_cast(_fieldMask(field.bits) << field.shift); + const auto bits = static_cast(next << field.shift); + low.store((low.load(std::memory_order_relaxed) & ~mask) | bits, std::memory_order_release); +} + bool _waitSatisfied(std::uint32_t compare, std::uint64_t value, std::uint64_t reference) { constexpr std::uint64_t sign = std::uint64_t{1} << 63u; switch (compare) { @@ -212,18 +265,20 @@ void _execute(const Apr::CommandBufferObject& buffer) { break; } case Apr::Opcode::WriteCounter: { - const auto command = _read(buffer, cursor); - g_counters[command.counter % g_counters.size()].store(command.value, std::memory_order_release); + _writeCounter(_read(buffer, cursor)); break; } case Apr::Opcode::WaitOnAddress: case Apr::Opcode::WaitOnCounter: { const auto command = _read(buffer, cursor); + const bool counter = header.opcode == Apr::Opcode::WaitOnCounter; + const std::uint32_t unused = counter ? 64u - _counterField(command.access).bits : 0u; const auto current = [&]() -> std::uint64_t { - if (header.opcode == Apr::Opcode::WaitOnCounter) return _counter(command.counter); + if (counter) return _readCounter(command.counter, command.access); return std::atomic_ref(*reinterpret_cast(command.address)).load(std::memory_order_acquire); }; - while (!_waitSatisfied(command.compare, current() & command.mask, command.reference & command.mask)) std::this_thread::sleep_for(std::chrono::microseconds(50)); + const std::uint64_t reference = (command.reference & command.mask) << unused; + while (!_waitSatisfied(command.compare, (current() & command.mask) << unused, reference)) std::this_thread::sleep_for(std::chrono::microseconds(50)); break; } case Apr::Opcode::WriteKernelEventQueue: { diff --git a/core/libs/tests/GuestAmpr.cpp b/core/libs/tests/GuestAmpr.cpp index 80b105261..b2ff45a58 100644 --- a/core/libs/tests/GuestAmpr.cpp +++ b/core/libs/tests/GuestAmpr.cpp @@ -7,6 +7,7 @@ #include #include #include +#include extern "C" { int APS5_VABI sceAmprCommandBufferConstructor(Apr::CommandBufferObject*); @@ -46,6 +47,12 @@ int APS5_VABI sceAmprCommandBufferWriteKernelEventQueue_04_00(Apr::CommandBuffer std::uint64_t APS5_VABI sceAmprMeasureCommandSizeWriteAddressFromTimeCounter_04_00(volatile std::uint64_t*); std::uint64_t APS5_VABI sceAmprMeasureCommandSizeWriteAddressFromCounter_04_00(volatile std::uint64_t*, std::uint8_t); std::uint64_t APS5_VABI sceAmprMeasureCommandSizeWriteAddressFromCounterPair_04_00(volatile std::uint64_t*, std::uint8_t); +int APS5_VABI sceAmprCommandBufferWaitOnCounter_04_00(Apr::CommandBufferObject*, std::uint8_t, std::uint8_t, std::uint64_t, std::uint8_t, std::uint8_t, std::uint64_t, std::uint8_t); +int APS5_VABI sceAmprCommandBufferWriteCounter_04_00(Apr::CommandBufferObject*, std::uint8_t, std::uint8_t, std::uint64_t, std::uint8_t, std::uint8_t); +std::uint64_t APS5_VABI sceAmprMeasureCommandSizeWaitOnCounter_04_00(std::uint8_t, std::uint8_t, std::uint64_t, std::uint8_t, std::uint8_t, std::uint64_t, std::uint8_t); +std::uint64_t APS5_VABI sceAmprMeasureCommandSizeWriteCounter_04_00(std::uint8_t, std::uint8_t, std::uint64_t, std::uint8_t); +int APS5_VABI sceAmprCommandBufferConstructNop(Apr::CommandBufferObject*, std::int16_t, const void*, std::uint32_t, const std::uint32_t*); +int APS5_VABI sceAmprCommandBufferConstructMarker(Apr::CommandBufferObject*, std::uint32_t, const char*, const std::uint32_t*); } static void Require(bool value) { if (!value) std::abort(); } @@ -288,6 +295,111 @@ void TestVersionedCommands() { Require(sceAmprMeasureCommandSizeWriteAddressFromCounterPair_04_00(&pair, 10) == sizeof(Apr::WriteAddressFromCounterCommand)); } +void SubmitWithin10Seconds(const Recorder& recorder) { + auto submitted = std::async(std::launch::async, [&]() { return sceKernelAprSubmitCommandBuffer(&recorder.buffer, 0); }); + Require(submitted.wait_for(std::chrono::seconds(10)) == std::future_status::ready); + Require(submitted.get() == 0); +} + +void TestVersionedCounters() { + enum : std::uint8_t { size8, size4, size2Offset0, size2Offset1, size1Offset0, size1Offset1, size1Offset2, size1Offset3 }; + enum : std::uint8_t { store, atomicOr, atomicAndComplement, atomicXor, atomicAdd }; + Recorder recorder; + alignas(8) std::uint64_t fields = 0; + alignas(8) std::uint64_t wide = 0; + alignas(8) std::uint64_t bits = 0; + Require(sceAmprCommandBufferWriteCounter_04_00(&recorder.buffer, 20, size4, 0x11223344u, store, 0) == 0); + Require(sceAmprCommandBufferWriteCounter_04_00(&recorder.buffer, 20, size1Offset2, 0x1AAu, store, 1) == 0); + Require(sceAmprCommandBufferWriteCounter_04_00(&recorder.buffer, 20, size2Offset0, 0xFFFFu, atomicAdd, 0) == 0); + Require(sceAmprCommandBufferWriteCounter_04_00(&recorder.buffer, 22, size8, 0x0000000500000001ull, store, 0) == 0); + Require(sceAmprCommandBufferWriteCounter_04_00(&recorder.buffer, 22, size8, 0xFFFFFFFFu, atomicAdd, 0) == 0); + Require(sceAmprCommandBufferWriteCounter_04_00(&recorder.buffer, 24, size4, 0xF0u, store, 0) == 0); + Require(sceAmprCommandBufferWriteCounter_04_00(&recorder.buffer, 24, size4, 0x0Fu, atomicOr, 0) == 0); + Require(sceAmprCommandBufferWriteCounter_04_00(&recorder.buffer, 24, size4, 0x3Cu, atomicAndComplement, 0) == 0); + Require(sceAmprCommandBufferWriteCounter_04_00(&recorder.buffer, 24, size4, 0xFFu, atomicXor, 0) == 0); + Require(sceAmprCommandBufferWaitOnCounter_04_00(&recorder.buffer, 20, size1Offset3, 0x11u, 0, 0, 0, 0) == 0); + Require(sceAmprCommandBufferWaitOnCounter_04_00(&recorder.buffer, 20, size1Offset2, 1u, 6, 0, 0, 1) == 0); + Require(sceAmprCommandBufferWaitOnCounter_04_00(&recorder.buffer, 20, size2Offset0, 0xF000u, 4, 0, 0, 0) == 0); + Require(sceAmprCommandBufferWaitOnCounter_04_00(&recorder.buffer, 20, size2Offset1, 0x11ABu, 2, 0, 0, 0) == 0); + Require(sceAmprCommandBufferWaitOnCounter_04_00(&recorder.buffer, 22, size8, 0x0000000600000000ull, 0, 0, 0, 0) == 0); + Require(sceAmprCommandBufferWaitOnCounter_04_00(&recorder.buffer, 24, size4, 0xFCu, 0, 1, 0x0Fu, 0) == 0); + Require(sceAmprCommandBufferWaitOnCounter_04_00(&recorder.buffer, 24, size1Offset0, 0x3Cu, 0, 0, 0x0Fu, 0) == 0); + Require(sceAmprCommandBufferWriteAddressFromCounterOnCompletion(&recorder.buffer, &fields, 20) == 0); + Require(sceAmprCommandBufferWriteAddressFromCounterPairOnCompletion(&recorder.buffer, &wide, 22) == 0); + Require(sceAmprCommandBufferWriteAddressFromCounterOnCompletion(&recorder.buffer, &bits, 24) == 0); + Require(recorder.Commands() == 19); + SubmitWithin10Seconds(recorder); + Require(fields == 0x11AA3343u && wide == 0x0000000600000000ull && bits == 0x3Cu); + + const auto rejected = static_cast(static_cast(invalidArgument)); + Recorder empty; + Require(sceAmprCommandBufferWaitOnCounter_04_00(&empty.buffer, 0, 8, 0, 0, 0, 0, 0) == invalidArgument); + Require(sceAmprCommandBufferWaitOnCounter_04_00(&empty.buffer, 0, size4, 0, 7, 0, 0, 0) == invalidArgument); + Require(sceAmprCommandBufferWaitOnCounter_04_00(&empty.buffer, 0, size4, 0, 0, 2, 0, 0) == invalidArgument); + Require(sceAmprCommandBufferWaitOnCounter_04_00(&empty.buffer, 0, size4, 0, 0, 0, 0, 2) == invalidArgument); + Require(sceAmprCommandBufferWriteCounter_04_00(&empty.buffer, 128, size4, 0, store, 0) == invalidArgument); + Require(sceAmprCommandBufferWriteCounter_04_00(&empty.buffer, 0, 8, 0, store, 0) == invalidArgument); + Require(sceAmprCommandBufferWriteCounter_04_00(&empty.buffer, 0, size4, 0, 5, 0) == invalidArgument); + Require(sceAmprCommandBufferWriteCounter_04_00(nullptr, 0, size4, 0, store, 0) == invalidArgument); + Require(empty.Offset() == 0 && empty.Commands() == 0); + Require(sceAmprMeasureCommandSizeWaitOnCounter_04_00(200, size1Offset3, 0, 6, 1, 0, 1) == sizeof(Apr::WaitCommand)); + Require(sceAmprMeasureCommandSizeWaitOnCounter_04_00(0, 8, 0, 0, 0, 0, 0) == rejected); + Require(sceAmprMeasureCommandSizeWaitOnCounter_04_00(0, size4, 0, 7, 0, 0, 0) == rejected); + Require(sceAmprMeasureCommandSizeWaitOnCounter_04_00(0, size4, 0, 0, 2, 0, 0) == rejected); + Require(sceAmprMeasureCommandSizeWaitOnCounter_04_00(0, size4, 0, 0, 0, 0, 2) == rejected); + Require(sceAmprMeasureCommandSizeWriteCounter_04_00(127, size8, 0, atomicAdd) == sizeof(Apr::WriteCounterCommand)); + Require(sceAmprMeasureCommandSizeWriteCounter_04_00(128, size4, 0, store) == rejected); + Require(sceAmprMeasureCommandSizeWriteCounter_04_00(0, 8, 0, store) == rejected); + Require(sceAmprMeasureCommandSizeWriteCounter_04_00(0, size4, 0, 5) == rejected); +} + +void TestConstructed() { + Recorder recorder; + const std::uint8_t payload[5] = {1, 2, 3, 4, 5}; + const std::uint32_t word = 0xCAFEF00Du; + auto offset = recorder.Offset(); + auto commands = recorder.Commands(); + Require(sceAmprCommandBufferConstructNop(&recorder.buffer, 7, payload, sizeof(payload), &word) == 0); + RequireAppended(recorder, offset, commands, Apr::Opcode::Nop, sceAmprMeasureCommandSizeNopWithData(4)); + const std::uint8_t* data = recorder.memory.data() + offset + sizeof(Apr::CommandHeader); + const std::uint8_t padding[3] = {}; + Require(std::memcmp(data, &word, sizeof(word)) == 0 && std::memcmp(data + 4, payload, sizeof(payload)) == 0 && std::memcmp(data + 9, padding, sizeof(padding)) == 0); + + const std::array large{}; + offset = recorder.Offset(); + commands = recorder.Commands(); + Require(sceAmprCommandBufferConstructNop(&recorder.buffer, 0, large.data(), 60, nullptr) == 0); + RequireAppended(recorder, offset, commands, Apr::Opcode::Nop, sceAmprMeasureCommandSizeNopWithData(16)); + offset = recorder.Offset(); + commands = recorder.Commands(); + Require(sceAmprCommandBufferConstructNop(&recorder.buffer, 0, nullptr, 0, nullptr) == 0); + RequireAppended(recorder, offset, commands, Apr::Opcode::Nop, sceAmprMeasureCommandSizeNopWithData(1)); + + const std::pair markers[] = {{1, Apr::Opcode::SetMarker}, {2, Apr::Opcode::PushMarker}, {5, Apr::Opcode::SetMarker}, {6, Apr::Opcode::PushMarker}}; + for (const auto& [type, opcode] : markers) { + offset = recorder.Offset(); + commands = recorder.Commands(); + Require(sceAmprCommandBufferConstructMarker(&recorder.buffer, type, "stream", &color) == 0); + RequireRecorded(recorder, offset, commands, opcode, sceAmprMeasureCommandSizeSetMarker("stream"), "stream"); + } + offset = recorder.Offset(); + commands = recorder.Commands(); + Require(sceAmprCommandBufferConstructMarker(&recorder.buffer, 3, nullptr, nullptr) == 0); + RequireAppended(recorder, offset, commands, Apr::Opcode::PopMarker, sceAmprMeasureCommandSizePopMarker()); + Require(sceKernelAprSubmitCommandBuffer(&recorder.buffer, 0) == 0); + + Recorder empty; + Require(sceAmprCommandBufferConstructNop(&empty.buffer, 0, large.data(), 61, nullptr) == invalidArgument); + Require(sceAmprCommandBufferConstructNop(&empty.buffer, 0, large.data(), 57, &word) == invalidArgument); + Require(sceAmprCommandBufferConstructNop(nullptr, 0, large.data(), 4, nullptr) == invalidArgument); + Require(sceAmprCommandBufferConstructMarker(&empty.buffer, 5, "stream", nullptr) == invalidArgument); + Require(sceAmprCommandBufferConstructMarker(&empty.buffer, 6, "stream", nullptr) == invalidArgument); + Require(sceAmprCommandBufferConstructMarker(&empty.buffer, 1, nullptr, nullptr) == invalidArgument); + Require(sceAmprCommandBufferConstructMarker(&empty.buffer, 0, "stream", &color) == invalidArgument); + Require(sceAmprCommandBufferConstructMarker(&empty.buffer, 4, "stream", &color) == invalidArgument); + Require(empty.Offset() == 0 && empty.Commands() == 0); +} + } int main() { @@ -304,5 +416,7 @@ int main() { TestRejectedWaitsAndCounters(); TestNops(); TestVersionedCommands(); + TestVersionedCounters(); + TestConstructed(); return 0; } diff --git a/docs/dev/TechnicalDebt.md b/docs/dev/TechnicalDebt.md index 8122ef8fa..5615df0f0 100644 --- a/docs/dev/TechnicalDebt.md +++ b/docs/dev/TechnicalDebt.md @@ -33,6 +33,7 @@ Throughout the project, every function at every stage either **does exactly what - [sceRtcParseDateTime](../../core/libs/prx/libSceRtc/Export.cpp) (libSceRtc) - accepted format assumed to be the ISO 8601 subset `YYYY-MM-DDTHH:MM:SS[.ffffff]` (T/t/space separator, optional fractional seconds) with the sibling RFC 3339 zone suffix (`Z` or `±HH:MM`); a string without a zone is taken as UTC, and anything outside this shape throws instead of returning a parse error, unverified against hardware - [AMPR WriteAddressFromCounterPairOnCompletion](../../core/libs/prx/libSceAmpr/Export.cpp) (libSceAmpr) - the pair of an even counter n is assumed to give counter n in the low 32 bits and n + 1 in the high 32 bits. Wait compare values, wait flush and counter index ranges follow the [ampr_emu](https://github.com/drakmor/ampr_emu) reimplementation; the flush is accepted and has no effect, as commands run in order - [AMPR command sizes](../../core/libs/prx/libSceAmpr/Export.cpp) (libSceAmpr) - commands use the emulator's own encoding with an 8-byte header, so `Nop(n)` takes 8 + 4n bytes where the console record is n dwords including its header (`NopWithData(n)`: (n + 1) dwords). The measure functions agree with the appended sizes, but a title that pads to an exact offset or the end of the buffer with `Nop(remaining / 4)` gets `SCE_AMPR_ERROR_BUFFER_FULL` or a shifted offset. The at-start flag of the `_04_00` writes is ignored; commands run in order, each after the previous one completes +- [AMPR counter access](../../core/libs/prx/libkernel/Apr/src/Apr.cpp) (libSceAmpr) - signatures and checks of `WaitOnCounter_04_00`, `WriteCounter_04_00`, `ConstructNop` and `ConstructMarker` taken from the [ampr_emu](https://github.com/drakmor/ampr_emu) reimplementation, which hands counter commands to the console and does not show their effect. The counters are taken as 32 bits each: the 8-byte access of counter n spans n (low) and n + 1 (high), the 2- and 1-byte accesses select a half or a byte of the counter by its offset, waits compare at the access width (signed and wrapped compares included), the AND mask applies to both sides and the atomic writes wrap within the access. The `ConstructNop` type is not recorded - [AMPR markers](../../core/libs/prx/libSceAmpr/Export.cpp) (libSceAmpr) - signatures taken from the [ampr_emu](https://github.com/drakmor/ampr_emu) reimplementation, not confirmed on a title: `sceAmprCommandBufferSetMarkerWithColor` takes the color by pointer, the push and measure variants by value. Colors are not recorded - [sceFontGetRenderScaledKerning](../../core/libs/prx/libSceFont/src/Render.cpp) (libSceFont) - arguments, the cleared output and the bound renderer check taken from `cellFontGetRenderScaledKerning` in [RPCS3](https://github.com/RPCS3/rpcs3/blob/master/rpcs3/Emu/Cell/Modules/cellFont.cpp) (PS3 predecessor of the library), not confirmed on a title. The kerning is the `sceFontGetKerning` one at the render scale - [sceFontGetPixelResolution](../../core/libs/prx/libSceFont/src/Library.cpp) (libSceFont) - signature taken from the IDA type database of [Orbital](https://github.com/AlexAltea/orbital/blob/master/tools/ida/db_types.json), not confirmed on a title. The value is the one the library's driver reports (64 for FreeType); the argument checks and the cleared output follow `sceFontGetLibrary`