diff --git a/core/libs/prx/libSceAmpr/Export.cpp b/core/libs/prx/libSceAmpr/Export.cpp index 51d8032fd..df54ef647 100644 --- a/core/libs/prx/libSceAmpr/Export.cpp +++ b/core/libs/prx/libSceAmpr/Export.cpp @@ -4,6 +4,7 @@ #include "prx/libc/include/General.hpp" #include "prx/libkernel/Apr/include/AprCommandBuffer.hpp" #include +#include static constexpr int SCE_AMPR_ERROR_BUFFER_FULL = 0x8002001C; static constexpr int SCE_KERNEL_ERROR_EINVAL = 0x80020016; @@ -35,6 +36,26 @@ static bool ValidWait(std::uint32_t compare, std::uint32_t flush) { return compare < 4u && flush < 2u; } +static constexpr std::uint64_t MeasureInvalid = static_cast(SCE_KERNEL_ERROR_EINVAL); + +static std::uint64_t NopBytes(std::uint32_t dwords) { + return sizeof(Apr::CommandHeader) + std::uint64_t{dwords} * 4u; +} + +static int AppendNop(Apr::CommandBufferObject* buffer, std::uint32_t dwords, const std::uint32_t* data) { + if (!buffer) return SCE_KERNEL_ERROR_EINVAL; + const std::uint64_t bytes = NopBytes(dwords); + if (!buffer->base || bytes > buffer->size - buffer->offset) return SCE_AMPR_ERROR_BUFFER_FULL; + const Apr::CommandHeader header{Apr::Opcode::Nop, static_cast(bytes)}; + std::uint8_t* destination = buffer->base + buffer->offset; + std::memset(destination, 0, bytes); + std::memcpy(destination, &header, sizeof(header)); + if (data) std::memcpy(destination + sizeof(header), data, std::uint64_t{dwords} * 4u); + buffer->offset += static_cast(bytes); + ++buffer->numCommands; + return 0; +} + static std::uint64_t MarkerBytes(const char* text) { return sizeof(Apr::MarkerCommand) + ((std::strlen(text) + 8) & ~std::uint64_t{7}); } @@ -215,14 +236,15 @@ uint32_t APS5_VABI sceAmprCommandBufferGetType(const Apr::CommandBufferObject* b return static_cast(buffer->type); } -int APS5_VABI sceAmprCommandBufferNop() { - NotImplemented_nid_no_patch(__func__); - return 0; +int APS5_VABI sceAmprCommandBufferNop(Apr::CommandBufferObject* buffer, std::uint32_t dwords) { + if (dwords == 0u || dwords > 16u) return SCE_KERNEL_ERROR_EINVAL; + return AppendNop(buffer, dwords, nullptr); } -int APS5_VABI sceAmprCommandBufferNopWithData() { - NotImplemented_nid_no_patch(__func__); - return 0; +int APS5_VABI sceAmprCommandBufferNopWithData(Apr::CommandBufferObject* buffer, std::uint32_t dwords, const std::uint32_t* data) { + if (dwords > 15u) return SCE_KERNEL_ERROR_EINVAL; + if (dwords != 0u && !data) throw std::invalid_argument("sceAmprCommandBufferNopWithData: null data"); + return AppendNop(buffer, dwords, data); } int APS5_VABI sceAmprCommandBufferPopMarker(Apr::CommandBufferObject* buffer) { @@ -261,9 +283,9 @@ int APS5_VABI sceAmprCommandBufferSetMarkerWithColor(Apr::CommandBufferObject* b return AppendMarker(buffer, Apr::Opcode::SetMarker, text); } -int APS5_VABI sceAmprCommandBufferWaitOnAddress_04_00() { - NotImplemented_nid_no_patch(__func__); - return 0; +int APS5_VABI sceAmprCommandBufferWaitOnAddress_04_00(Apr::CommandBufferObject* buffer, volatile std::uint64_t* address, std::uint64_t reference, std::uint8_t compare, std::uint8_t flush) { + if (!ValidWriteAddress(address) || compare > 6u || flush > 1u) return SCE_KERNEL_ERROR_EINVAL; + return AppendCommand(buffer, Apr::Opcode::WaitOnAddress, Apr::WaitCommand{{}, reinterpret_cast(address), reference, ~0ull, 0, compare}); } int APS5_VABI sceAmprCommandBufferWaitOnCounter_04_00() { @@ -271,19 +293,19 @@ int APS5_VABI sceAmprCommandBufferWaitOnCounter_04_00() { return 0; } -int APS5_VABI sceAmprCommandBufferWriteAddressFromCounterPair_04_00() { - NotImplemented_nid_no_patch(__func__); - return 0; +int APS5_VABI sceAmprCommandBufferWriteAddressFromCounterPair_04_00(Apr::CommandBufferObject* buffer, volatile std::uint64_t* address, std::uint8_t counter, std::uint64_t atStart) { + (void)atStart; + return sceAmprCommandBufferWriteAddressFromCounterPairOnCompletion(buffer, address, counter); } -int APS5_VABI sceAmprCommandBufferWriteAddressFromCounter_04_00() { - NotImplemented_nid_no_patch(__func__); - return 0; +int APS5_VABI sceAmprCommandBufferWriteAddressFromCounter_04_00(Apr::CommandBufferObject* buffer, volatile std::uint64_t* address, std::uint8_t counter, std::uint64_t atStart) { + (void)atStart; + return sceAmprCommandBufferWriteAddressFromCounterOnCompletion(buffer, address, counter); } -int APS5_VABI sceAmprCommandBufferWriteAddressFromTimeCounter_04_00() { - NotImplemented_nid_no_patch(__func__); - return 0; +int APS5_VABI sceAmprCommandBufferWriteAddressFromTimeCounter_04_00(Apr::CommandBufferObject* buffer, volatile std::uint64_t* address, std::uint64_t atStart) { + (void)atStart; + return sceAmprCommandBufferWriteAddressFromTimeCounterOnCompletion(buffer, address); } int APS5_VABI sceAmprCommandBufferWriteAddress_04_00(Apr::CommandBufferObject* buffer, uint64_t* address, uint64_t value, uint32_t flags) { @@ -300,9 +322,9 @@ int APS5_VABI sceAmprCommandBufferWriteCounter_04_00() { return 0; } -int APS5_VABI sceAmprCommandBufferWriteKernelEventQueue_04_00() { - NotImplemented_nid_no_patch(__func__); - return 0; +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) { + (void)atStart; + return sceAmprCommandBufferWriteKernelEventQueueOnCompletion(buffer, equeue, ident, data); } int APS5_VABI sceAmprMeasureCommandSizeMapBegin() { @@ -320,14 +342,14 @@ int APS5_VABI sceAmprMeasureCommandSizeMapEnd() { return 0; } -int APS5_VABI sceAmprMeasureCommandSizeNop() { - NotImplemented_nid_no_patch(__func__); - return 0; +std::uint64_t APS5_VABI sceAmprMeasureCommandSizeNop(std::uint32_t dwords) { + if (dwords == 0u || dwords > 16u) return MeasureInvalid; + return NopBytes(dwords); } -int APS5_VABI sceAmprMeasureCommandSizeNopWithData() { - NotImplemented_nid_no_patch(__func__); - return 0; +std::uint64_t APS5_VABI sceAmprMeasureCommandSizeNopWithData(std::uint32_t dwords) { + if (dwords == 0u || dwords > 16u) return MeasureInvalid; + return NopBytes(dwords - 1u); } std::uint64_t APS5_VABI sceAmprMeasureCommandSizePopMarker() { @@ -376,9 +398,9 @@ std::uint64_t APS5_VABI sceAmprMeasureCommandSizeSetMarkerWithColor(const char* return MeasureMarker(text); } -int APS5_VABI sceAmprMeasureCommandSizeWaitOnAddress_04_00() { - NotImplemented_nid_no_patch(__func__); - return 0; +std::uint64_t APS5_VABI sceAmprMeasureCommandSizeWaitOnAddress_04_00(volatile std::uint64_t* address, std::uint64_t, std::uint8_t compare, std::uint8_t flush) { + if (!ValidWriteAddress(address) || compare > 6u || flush > 1u) return MeasureInvalid; + return sizeof(Apr::WaitCommand); } int APS5_VABI sceAmprMeasureCommandSizeWaitOnCounter_04_00() { @@ -386,19 +408,20 @@ int APS5_VABI sceAmprMeasureCommandSizeWaitOnCounter_04_00() { return 0; } -int APS5_VABI sceAmprMeasureCommandSizeWriteAddressFromCounterPair_04_00() { - NotImplemented_nid_no_patch(__func__); - return 0; +std::uint64_t APS5_VABI sceAmprMeasureCommandSizeWriteAddressFromCounterPair_04_00(volatile std::uint64_t* address, std::uint8_t counter) { + if (!ValidWriteAddress(address) || (counter & 1u) != 0u) return MeasureInvalid; + return sizeof(Apr::WriteAddressFromCounterCommand); } -int APS5_VABI sceAmprMeasureCommandSizeWriteAddressFromCounter_04_00() { - NotImplemented_nid_no_patch(__func__); - return 0; +std::uint64_t APS5_VABI sceAmprMeasureCommandSizeWriteAddressFromCounter_04_00(volatile std::uint64_t* address, std::uint8_t counter) { + (void)counter; + if (!ValidWriteAddress(address)) return MeasureInvalid; + return sizeof(Apr::WriteAddressFromCounterCommand); } -int APS5_VABI sceAmprMeasureCommandSizeWriteAddressFromTimeCounter_04_00() { - NotImplemented_nid_no_patch(__func__); - return 0; +std::uint64_t APS5_VABI sceAmprMeasureCommandSizeWriteAddressFromTimeCounter_04_00(volatile std::uint64_t* address) { + if (!ValidWriteAddress(address)) return MeasureInvalid; + return sizeof(Apr::WriteAddressFromCounterCommand); } uint32_t APS5_VABI sceAmprMeasureCommandSizeWriteAddress_04_00(void) { @@ -410,9 +433,8 @@ int APS5_VABI sceAmprMeasureCommandSizeWriteCounter_04_00() { return 0; } -int APS5_VABI sceAmprMeasureCommandSizeWriteKernelEventQueue_04_00() { - NotImplemented_nid_no_patch(__func__); - return 0; +std::uint64_t APS5_VABI sceAmprMeasureCommandSizeWriteKernelEventQueue_04_00(std::uint64_t, std::int32_t, std::uint64_t) { + return sizeof(Apr::WriteKernelEventQueueCommand); } } diff --git a/core/libs/tests/GuestAmpr.cpp b/core/libs/tests/GuestAmpr.cpp index 74dad2ece..80b105261 100644 --- a/core/libs/tests/GuestAmpr.cpp +++ b/core/libs/tests/GuestAmpr.cpp @@ -33,6 +33,19 @@ int APS5_VABI sceAmprCommandBufferWriteAddressFromTimeCounterOnCompletion(Apr::C int APS5_VABI sceAmprCommandBufferWriteAddressFromCounterOnCompletion(Apr::CommandBufferObject*, volatile std::uint64_t*, std::uint8_t); int APS5_VABI sceAmprCommandBufferWriteAddressFromCounterPairOnCompletion(Apr::CommandBufferObject*, volatile std::uint64_t*, std::uint8_t); int APS5_VABI sceAmprCommandBufferWriteKernelEventQueueOnCompletion(Apr::CommandBufferObject*, std::uint64_t, std::int32_t, std::uint64_t); +int APS5_VABI sceAmprCommandBufferNop(Apr::CommandBufferObject*, std::uint32_t); +int APS5_VABI sceAmprCommandBufferNopWithData(Apr::CommandBufferObject*, std::uint32_t, const std::uint32_t*); +std::uint64_t APS5_VABI sceAmprMeasureCommandSizeNop(std::uint32_t); +std::uint64_t APS5_VABI sceAmprMeasureCommandSizeNopWithData(std::uint32_t); +int APS5_VABI sceAmprCommandBufferWaitOnAddress_04_00(Apr::CommandBufferObject*, volatile std::uint64_t*, std::uint64_t, std::uint8_t, std::uint8_t); +std::uint64_t APS5_VABI sceAmprMeasureCommandSizeWaitOnAddress_04_00(volatile std::uint64_t*, std::uint64_t, std::uint8_t, std::uint8_t); +int APS5_VABI sceAmprCommandBufferWriteAddressFromTimeCounter_04_00(Apr::CommandBufferObject*, volatile std::uint64_t*, std::uint64_t); +int APS5_VABI sceAmprCommandBufferWriteAddressFromCounter_04_00(Apr::CommandBufferObject*, volatile std::uint64_t*, std::uint8_t, std::uint64_t); +int APS5_VABI sceAmprCommandBufferWriteAddressFromCounterPair_04_00(Apr::CommandBufferObject*, volatile std::uint64_t*, std::uint8_t, std::uint64_t); +int APS5_VABI sceAmprCommandBufferWriteKernelEventQueue_04_00(Apr::CommandBufferObject*, std::uint64_t, std::int32_t, std::uint64_t, std::uint64_t); +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); } static void Require(bool value) { if (!value) std::abort(); } @@ -212,6 +225,69 @@ void TestRejectedWaitsAndCounters() { Require(recorder.Offset() == 0 && recorder.Commands() == 0); } +void TestNops() { + Recorder recorder; + const std::uint32_t data[3] = {0x11111111u, 0x22222222u, 0x33333333u}; + for (std::uint32_t dwords = 1; dwords <= 16; ++dwords) { + const auto offset = recorder.Offset(); + const auto commands = recorder.Commands(); + Require(sceAmprCommandBufferNop(&recorder.buffer, dwords) == 0); + RequireAppended(recorder, offset, commands, Apr::Opcode::Nop, sceAmprMeasureCommandSizeNop(dwords)); + } + const auto offset = recorder.Offset(); + const auto commands = recorder.Commands(); + Require(sceAmprCommandBufferNopWithData(&recorder.buffer, 3, data) == 0); + RequireAppended(recorder, offset, commands, Apr::Opcode::Nop, sceAmprMeasureCommandSizeNopWithData(4)); + Require(std::memcmp(recorder.memory.data() + offset + sizeof(Apr::CommandHeader), data, sizeof(data)) == 0); + Require(sceAmprCommandBufferNopWithData(&recorder.buffer, 0, nullptr) == 0); + Require(sceKernelAprSubmitCommandBuffer(&recorder.buffer, 0) == 0); + + const auto rejected = static_cast(static_cast(invalidArgument)); + Require(sceAmprCommandBufferNop(&recorder.buffer, 0) == invalidArgument); + Require(sceAmprCommandBufferNop(&recorder.buffer, 17) == invalidArgument); + Require(sceAmprCommandBufferNopWithData(&recorder.buffer, 16, data) == invalidArgument); + Require(sceAmprMeasureCommandSizeNop(0) == rejected && sceAmprMeasureCommandSizeNop(17) == rejected); + Require(sceAmprMeasureCommandSizeNopWithData(0) == rejected && sceAmprMeasureCommandSizeNopWithData(17) == rejected); +} + +void TestVersionedCommands() { + Recorder recorder; + alignas(8) std::uint64_t value = 0x8000000000000005ull; + alignas(8) std::uint64_t single = 0; + alignas(8) std::uint64_t pair = 0; + alignas(8) std::uint64_t time = 0; + Require(sceAmprCommandBufferWaitOnAddress_04_00(&recorder.buffer, &value, 0x8000000000000003ull, 4, 0) == 0); + Require(sceAmprCommandBufferWaitOnAddress_04_00(&recorder.buffer, &value, 1, 6, 1) == 0); + Require(sceAmprCommandBufferWaitOnAddress_04_00(&recorder.buffer, &value, 0x8000000000000000ull, 5, 0) == 0); + Require(sceAmprCommandBufferWriteCounterOnCompletion(&recorder.buffer, 10, 3) == 0); + Require(sceAmprCommandBufferWriteCounterOnCompletion(&recorder.buffer, 11, 4) == 0); + Require(sceAmprCommandBufferWriteAddressFromCounter_04_00(&recorder.buffer, &single, 10, 1) == 0); + Require(sceAmprCommandBufferWriteAddressFromCounterPair_04_00(&recorder.buffer, &pair, 10, 0) == 0); + Require(sceAmprCommandBufferWriteAddressFromTimeCounter_04_00(&recorder.buffer, &time, 1) == 0); + 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 && single == 3 && pair == (3ull | (4ull << 32u)) && time != 0); + + const auto rejected = static_cast(static_cast(invalidArgument)); + Recorder empty; + Require(sceAmprCommandBufferWaitOnAddress_04_00(&empty.buffer, nullptr, 0, 0, 0) == invalidArgument); + Require(sceAmprCommandBufferWaitOnAddress_04_00(&empty.buffer, &value, 0, 7, 0) == invalidArgument); + Require(sceAmprCommandBufferWaitOnAddress_04_00(&empty.buffer, &value, 0, 0, 2) == invalidArgument); + Require(sceAmprCommandBufferWriteAddressFromCounterPair_04_00(&empty.buffer, &pair, 11, 0) == invalidArgument); + Require(sceAmprCommandBufferWriteAddressFromTimeCounter_04_00(&empty.buffer, nullptr, 0) == invalidArgument); + Require(sceAmprCommandBufferWriteKernelEventQueue_04_00(&empty.buffer, 0, 1, 0, 0) == invalidArgument); + Require(empty.Offset() == 0 && empty.Commands() == 0); + Require(sceAmprMeasureCommandSizeWaitOnAddress_04_00(nullptr, 0, 0, 0) == rejected); + Require(sceAmprMeasureCommandSizeWaitOnAddress_04_00(&value, 0, 7, 0) == rejected); + Require(sceAmprMeasureCommandSizeWriteAddressFromTimeCounter_04_00(nullptr) == rejected); + Require(sceAmprMeasureCommandSizeWriteAddressFromCounter_04_00(&single, 128) == sizeof(Apr::WriteAddressFromCounterCommand)); + Require(sceAmprMeasureCommandSizeWriteAddressFromCounter_04_00(nullptr, 0) == rejected); + Require(sceAmprMeasureCommandSizeWriteAddressFromCounterPair_04_00(&pair, 128) == sizeof(Apr::WriteAddressFromCounterCommand)); + Require(sceAmprMeasureCommandSizeWriteAddressFromCounterPair_04_00(&pair, 11) == rejected); + Require(sceAmprMeasureCommandSizeWaitOnAddress_04_00(&value, 0, 6, 1) == sizeof(Apr::WaitCommand)); + Require(sceAmprMeasureCommandSizeWriteAddressFromCounterPair_04_00(&pair, 10) == sizeof(Apr::WriteAddressFromCounterCommand)); +} + } int main() { @@ -226,5 +302,7 @@ int main() { TestWaits(); TestCounters(); TestRejectedWaitsAndCounters(); + TestNops(); + TestVersionedCommands(); return 0; } diff --git a/docs/dev/TechnicalDebt.md b/docs/dev/TechnicalDebt.md index 5fd46685b..5ac2fde02 100644 --- a/docs/dev/TechnicalDebt.md +++ b/docs/dev/TechnicalDebt.md @@ -32,6 +32,7 @@ Throughout the project, every function at every stage either **does exactly what - [sceVideoOutOpen](../../core/libs/prx/libSceVideoOut/src/Output.cpp) (libSceVideoOut) - the open param's first word is unknown (PPSA21564 passes 16; it is not the byte size, since the affinity mask is at offset 16); only 16 is accepted. Whether the param continues past offset 24 is also unknown - [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 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`