From c16e59bfd5ebda8dcfead9a188e2402697fdba65 Mon Sep 17 00:00:00 2001 From: Chad Date: Mon, 5 Oct 2026 15:29:33 -0500 Subject: [PATCH] feat(libs): implement the AMM map, map direct and unmap commands and the APR map region libSceAmpr gains the AMM command buffer (constructor, destructor, map, map direct and unmap, with their GPU mask forms and measures), sceAmprAmmGiveDirectMemory, sceAmprAmmGetVirtualAddressRanges, the three submit forms and sceAmprAmmWaitCommandBufferCompletion. The APR MapBegin, MapDirectBegin and MapEnd commands and their measures record the same map commands. The libkernel executor now maps: a plain map takes pages from the direct memory given to the mapper with usage 1, a direct map uses the given offset, and an unmap returns the pages to that pool and leaves the range reserved. The AMM ranges are reserved in the guest arena on first use. Commands are walked by the buffer's byte offset so an AMM submission can run raw command memory. Signatures, the protection mask and the APR map rules follow ampr_emu; the mapper model follows PS5PCEM. While an APR map region is open, a second begin and every completion write return EPERM. Co-Authored-By: Claude Opus 5.5 --- core/libs/prx/libSceAmpr/CMakeLists.txt | 1 + core/libs/prx/libSceAmpr/Export.cpp | 232 +++++++++++++++--- .../Apr/include/AprCommandBuffer.hpp | 31 +++ core/libs/prx/libkernel/Apr/src/Apr.cpp | 145 ++++++++++- core/libs/tests/GuestAmpr.cpp | 164 +++++++++++++ docs/dev/TechnicalDebt.md | 2 + 6 files changed, 539 insertions(+), 36 deletions(-) diff --git a/core/libs/prx/libSceAmpr/CMakeLists.txt b/core/libs/prx/libSceAmpr/CMakeLists.txt index b16579e4b..83e3668bf 100644 --- a/core/libs/prx/libSceAmpr/CMakeLists.txt +++ b/core/libs/prx/libSceAmpr/CMakeLists.txt @@ -1,5 +1,6 @@ add_library(libSceAmpr SHARED EXCLUDE_FROM_ALL Export.cpp) target_include_directories(libSceAmpr PRIVATE ${LIBS_INCLUDE_DIR}) +target_link_libraries(libSceAmpr PRIVATE libkernel) configure_windows_unwind(libSceAmpr) if(MINGW) diff --git a/core/libs/prx/libSceAmpr/Export.cpp b/core/libs/prx/libSceAmpr/Export.cpp index bfb04045c..0f4f9b32a 100644 --- a/core/libs/prx/libSceAmpr/Export.cpp +++ b/core/libs/prx/libSceAmpr/Export.cpp @@ -8,6 +8,9 @@ static constexpr int SCE_AMPR_ERROR_BUFFER_FULL = 0x8002001C; static constexpr int SCE_KERNEL_ERROR_EINVAL = 0x80020016; +static constexpr int SCE_KERNEL_ERROR_EPERM = 0x80020001; +static constexpr int SCE_KERNEL_ERROR_ESRCH = 0x80020003; +static constexpr int SCE_KERNEL_ERROR_EBUSY = 0x80020010; static int Append(Apr::CommandBufferObject* buffer, const void* command, uint32_t bytes) { if (!buffer->base || bytes > buffer->size - buffer->offset) return SCE_AMPR_ERROR_BUFFER_FULL; @@ -110,15 +113,77 @@ static int AppendMarker(Apr::CommandBufferObject* buffer, Apr::Opcode opcode, co return 0; } +static bool InMap(const Apr::CommandBufferObject* buffer) { + return buffer && (buffer->recording & Apr::MapActive) != 0u; +} + +static bool ValidMapRange(std::uint64_t address, std::uint64_t size) { + constexpr std::uint64_t pageMask = 0x3FFFu; + return address != 0u && size != 0u && ((address | size) & pageMask) == 0u && address + size > address; +} + +static bool ValidMap(std::uint64_t address, std::uint64_t directOffset, std::uint64_t size, std::int32_t protection) { + return ValidMapRange(address, size) && (directOffset & 0x3FFFu) == 0u && (static_cast(protection) & 0xFFFFFC0Cu) == 0u; +} + +static constexpr std::int64_t AmmMeasureInvalid = SCE_KERNEL_ERROR_EINVAL; + +static int AppendAmm(Apr::CommandBufferObject* buffer, const void* command, std::uint32_t bytes) { + if (bytes > buffer->size - buffer->offset) return SCE_KERNEL_ERROR_EBUSY; + return Append(buffer, command, bytes); +} + +static int RecordAmmMap(Apr::CommandBufferObject* buffer, Apr::Opcode opcode, std::uint64_t address, std::uint64_t directOffset, std::uint64_t size, std::int32_t type, std::int32_t protection) { + if (!buffer) return SCE_KERNEL_ERROR_EINVAL; + if (!buffer->base) return SCE_KERNEL_ERROR_EPERM; + if (!ValidMap(address, directOffset, size, protection)) return SCE_KERNEL_ERROR_EINVAL; + const Apr::AmmMapCommand command{{opcode, sizeof(command)}, address, directOffset, size, type, protection}; + return AppendAmm(buffer, &command, sizeof(command)); +} + +static int RecordMapBegin(Apr::CommandBufferObject* buffer, Apr::Opcode opcode, std::uint64_t address, std::uint64_t directOffset, std::uint64_t size, std::int32_t type, std::int32_t protection) { + if (!ValidMap(address, directOffset, size, protection)) return SCE_KERNEL_ERROR_EINVAL; + if (InMap(buffer)) return SCE_KERNEL_ERROR_EPERM; + const int result = AppendCommand(buffer, opcode, Apr::AmmMapCommand{{}, address, directOffset, size, type, protection}); + if (result == 0) buffer->recording |= Apr::MapActive; + return result; +} + +static int RecordWriteKernelEventQueue(Apr::CommandBufferObject* buffer, std::uint64_t equeue, std::int32_t ident, std::uint64_t data, bool completion) { + if (!equeue) return SCE_KERNEL_ERROR_EINVAL; + if (completion && InMap(buffer)) return SCE_KERNEL_ERROR_EPERM; + return AppendCommand(buffer, Apr::Opcode::WriteKernelEventQueue, Apr::WriteKernelEventQueueCommand{{}, equeue, static_cast(ident), data, 0}); +} + +static int RecordWriteAddressFromTimeCounter(Apr::CommandBufferObject* buffer, volatile std::uint64_t* address, bool completion) { + if (!ValidWriteAddress(address)) return SCE_KERNEL_ERROR_EINVAL; + if (completion && InMap(buffer)) return SCE_KERNEL_ERROR_EPERM; + return AppendCommand(buffer, Apr::Opcode::WriteAddressFromTimeCounter, Apr::WriteAddressFromCounterCommand{{}, reinterpret_cast(address), 0, 0}); +} + +static int RecordWriteAddressFromCounter(Apr::CommandBufferObject* buffer, volatile std::uint64_t* address, std::uint8_t counter, bool completion) { + if (!ValidWriteAddress(address) || !ValidCounter(counter)) return SCE_KERNEL_ERROR_EINVAL; + if (completion && InMap(buffer)) return SCE_KERNEL_ERROR_EPERM; + return AppendCommand(buffer, Apr::Opcode::WriteAddressFromCounter, Apr::WriteAddressFromCounterCommand{{}, reinterpret_cast(address), counter, 0}); +} + +static int RecordWriteAddressFromCounterPair(Apr::CommandBufferObject* buffer, volatile std::uint64_t* address, std::uint8_t counter, bool completion) { + if (!ValidWriteAddress(address) || (counter & 0x81u) != 0u) return SCE_KERNEL_ERROR_EINVAL; + if (completion && InMap(buffer)) return SCE_KERNEL_ERROR_EPERM; + return AppendCommand(buffer, Apr::Opcode::WriteAddressFromCounterPair, Apr::WriteAddressFromCounterCommand{{}, reinterpret_cast(address), counter, counter + 1u}); +} + extern "C" { int APS5_VABI sceAmprCommandBufferWriteAddressOnCompletion(Apr::CommandBufferObject* buffer, volatile std::uint64_t* address, std::uint64_t value) { if (!ValidWriteAddress(address)) return SCE_KERNEL_ERROR_EINVAL; + if (InMap(buffer)) return SCE_KERNEL_ERROR_EPERM; return AppendCommand(buffer, Apr::Opcode::WriteAddress, Apr::WriteAddressCommand{{}, reinterpret_cast(address), value, 0, 0}); } int APS5_VABI sceAmprCommandBufferWriteCounterOnCompletion(Apr::CommandBufferObject* buffer, std::uint8_t counter, std::uint32_t value) { if (!ValidCounter(counter)) return SCE_KERNEL_ERROR_EINVAL; + if (InMap(buffer)) return SCE_KERNEL_ERROR_EPERM; return AppendCommand(buffer, Apr::Opcode::WriteCounter, Apr::WriteCounterCommand{{}, counter, Apr::CounterAccess::Size4, Apr::CounterOperation::Store, 0, value}); } @@ -133,23 +198,19 @@ int APS5_VABI sceAmprCommandBufferWaitOnCounter(Apr::CommandBufferObject* buffer } int APS5_VABI sceAmprCommandBufferWriteKernelEventQueueOnCompletion(Apr::CommandBufferObject* buffer, std::uint64_t equeue, std::int32_t ident, std::uint64_t data) { - if (!equeue) return SCE_KERNEL_ERROR_EINVAL; - return AppendCommand(buffer, Apr::Opcode::WriteKernelEventQueue, Apr::WriteKernelEventQueueCommand{{}, equeue, static_cast(ident), data, 0}); + return RecordWriteKernelEventQueue(buffer, equeue, ident, data, true); } int APS5_VABI sceAmprCommandBufferWriteAddressFromTimeCounterOnCompletion(Apr::CommandBufferObject* buffer, volatile std::uint64_t* address) { - if (!ValidWriteAddress(address)) return SCE_KERNEL_ERROR_EINVAL; - return AppendCommand(buffer, Apr::Opcode::WriteAddressFromTimeCounter, Apr::WriteAddressFromCounterCommand{{}, reinterpret_cast(address), 0, 0}); + return RecordWriteAddressFromTimeCounter(buffer, address, true); } int APS5_VABI sceAmprCommandBufferWriteAddressFromCounterOnCompletion(Apr::CommandBufferObject* buffer, volatile std::uint64_t* address, std::uint8_t counter) { - if (!ValidWriteAddress(address) || !ValidCounter(counter)) return SCE_KERNEL_ERROR_EINVAL; - return AppendCommand(buffer, Apr::Opcode::WriteAddressFromCounter, Apr::WriteAddressFromCounterCommand{{}, reinterpret_cast(address), counter, 0}); + return RecordWriteAddressFromCounter(buffer, address, counter, true); } int APS5_VABI sceAmprCommandBufferWriteAddressFromCounterPairOnCompletion(Apr::CommandBufferObject* buffer, volatile std::uint64_t* address, std::uint8_t counter) { - if (!ValidWriteAddress(address) || (counter & 0x81u) != 0u) return SCE_KERNEL_ERROR_EINVAL; - return AppendCommand(buffer, Apr::Opcode::WriteAddressFromCounterPair, Apr::WriteAddressFromCounterCommand{{}, reinterpret_cast(address), counter, counter + 1u}); + return RecordWriteAddressFromCounterPair(buffer, address, counter, true); } std::uint64_t APS5_VABI sceAmprMeasureCommandSizeWriteAddressOnCompletion() { return sizeof(Apr::WriteAddressCommand); } @@ -172,19 +233,20 @@ int APS5_VABI sceAmprAprCommandBufferDestructor(Apr::CommandBufferObject* buffer return 0; } -int APS5_VABI sceAmprAprCommandBufferMapBegin() { - NotImplemented_nid_no_patch(__func__); - return 0; +int APS5_VABI sceAmprAprCommandBufferMapBegin(Apr::CommandBufferObject* buffer, std::uint64_t address, std::uint64_t size, std::int32_t type, std::int32_t protection) { + return RecordMapBegin(buffer, Apr::Opcode::AmmMap, address, 0, size, type, protection); } -int APS5_VABI sceAmprAprCommandBufferMapDirectBegin() { - NotImplemented_nid_no_patch(__func__); - return 0; +int APS5_VABI sceAmprAprCommandBufferMapDirectBegin(Apr::CommandBufferObject* buffer, std::uint64_t address, std::uint64_t directOffset, std::uint64_t size, std::int32_t type, std::int32_t protection) { + return RecordMapBegin(buffer, Apr::Opcode::AmmMapDirect, address, directOffset, size, type, protection); } -int APS5_VABI sceAmprAprCommandBufferMapEnd() { - NotImplemented_nid_no_patch(__func__); - return 0; +int APS5_VABI sceAmprAprCommandBufferMapEnd(Apr::CommandBufferObject* buffer) { + if (!buffer) return SCE_KERNEL_ERROR_EINVAL; + if (!InMap(buffer)) return SCE_KERNEL_ERROR_EPERM; + const int result = AppendCommand(buffer, Apr::Opcode::MapEnd, Apr::MapEndCommand{}); + if (result == 0) buffer->recording &= static_cast(~Apr::MapActive); + return result; } int APS5_VABI sceAmprAprCommandBufferReadFile(Apr::CommandBufferObject* buffer, uint64_t* gatherState, uint64_t* scatterState, uint32_t fileId, void* destination, uint64_t size, uint64_t offset) { @@ -346,18 +408,15 @@ int APS5_VABI sceAmprCommandBufferWaitOnCounter_04_00(Apr::CommandBufferObject* } 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); + return RecordWriteAddressFromCounterPair(buffer, address, counter, atStart == 0u); } 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); + return RecordWriteAddressFromCounter(buffer, address, counter, atStart == 0u); } int APS5_VABI sceAmprCommandBufferWriteAddressFromTimeCounter_04_00(Apr::CommandBufferObject* buffer, volatile std::uint64_t* address, std::uint64_t atStart) { - (void)atStart; - return sceAmprCommandBufferWriteAddressFromTimeCounterOnCompletion(buffer, address); + return RecordWriteAddressFromTimeCounter(buffer, address, atStart == 0u); } int APS5_VABI sceAmprCommandBufferWriteAddress_04_00(Apr::CommandBufferObject* buffer, uint64_t* address, uint64_t value, uint32_t flags) { @@ -370,29 +429,29 @@ int APS5_VABI sceAmprCommandBufferWriteAddress_04_00(Apr::CommandBufferObject* b } 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; + if (atStart == 0u && InMap(buffer)) return SCE_KERNEL_ERROR_EPERM; 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) { - (void)atStart; - return sceAmprCommandBufferWriteKernelEventQueueOnCompletion(buffer, equeue, ident, data); + return RecordWriteKernelEventQueue(buffer, equeue, ident, data, atStart == 0u); } -int APS5_VABI sceAmprMeasureCommandSizeMapBegin() { - NotImplemented_nid_no_patch(__func__); - return 0; +std::uint64_t APS5_VABI sceAmprMeasureCommandSizeMapBegin(std::uint64_t address, std::uint64_t size, std::uint32_t type, std::uint32_t protection) { + (void)type; + if (!ValidMap(address, 0, size, static_cast(protection))) return MeasureInvalid; + return sizeof(Apr::AmmMapCommand); } -int APS5_VABI sceAmprMeasureCommandSizeMapDirectBegin() { - NotImplemented_nid_no_patch(__func__); - return 0; +std::uint64_t APS5_VABI sceAmprMeasureCommandSizeMapDirectBegin(std::uint64_t address, std::uint64_t directOffset, std::uint64_t size, std::uint32_t type, std::uint32_t protection) { + (void)type; + if (!ValidMap(address, directOffset, size, static_cast(protection))) return MeasureInvalid; + return sizeof(Apr::AmmMapCommand); } -int APS5_VABI sceAmprMeasureCommandSizeMapEnd() { - NotImplemented_nid_no_patch(__func__); - return 0; +std::uint64_t APS5_VABI sceAmprMeasureCommandSizeMapEnd() { + return sizeof(Apr::MapEndCommand); } std::uint64_t APS5_VABI sceAmprMeasureCommandSizeNop(std::uint32_t dwords) { @@ -489,4 +548,107 @@ std::uint64_t APS5_VABI sceAmprMeasureCommandSizeWriteKernelEventQueue_04_00(std return sizeof(Apr::WriteKernelEventQueueCommand); } +struct AmmSubmitResult { + std::int32_t result; + std::uint32_t errorOffset; +}; + +int APS5_VABI sceAmprAmmCommandBufferConstructor(Apr::CommandBufferObject* buffer) { + (void)buffer; + return 0; +} + +int APS5_VABI sceAmprAmmCommandBufferDestructor(Apr::CommandBufferObject* buffer) { + (void)buffer; + return 0; +} + +int APS5_VABI sceAmprAmmCommandBufferMap(Apr::CommandBufferObject* buffer, std::uint64_t address, std::uint64_t size, std::int32_t type, std::int32_t protection) { + return RecordAmmMap(buffer, Apr::Opcode::AmmMap, address, 0, size, type, protection); +} + +int APS5_VABI sceAmprAmmCommandBufferMapWithGpuMaskId(Apr::CommandBufferObject* buffer, std::uint64_t address, std::uint64_t size, std::int32_t type, std::int32_t protection, std::uint8_t gpuMaskId) { + (void)gpuMaskId; + return RecordAmmMap(buffer, Apr::Opcode::AmmMap, address, 0, size, type, protection); +} + +int APS5_VABI sceAmprAmmCommandBufferMapDirect(Apr::CommandBufferObject* buffer, std::uint64_t address, std::uint64_t directOffset, std::uint64_t size, std::int32_t type, std::int32_t protection) { + return RecordAmmMap(buffer, Apr::Opcode::AmmMapDirect, address, directOffset, size, type, protection); +} + +int APS5_VABI sceAmprAmmCommandBufferMapDirectWithGpuMaskId(Apr::CommandBufferObject* buffer, std::uint64_t address, std::uint64_t directOffset, std::uint64_t size, std::int32_t type, std::int32_t protection, std::uint8_t gpuMaskId) { + (void)gpuMaskId; + return RecordAmmMap(buffer, Apr::Opcode::AmmMapDirect, address, directOffset, size, type, protection); +} + +int APS5_VABI sceAmprAmmCommandBufferUnmap(Apr::CommandBufferObject* buffer, std::uint64_t address, std::uint64_t size) { + if (!buffer) return SCE_KERNEL_ERROR_EINVAL; + if (!buffer->base) return SCE_KERNEL_ERROR_EPERM; + if (!ValidMapRange(address, size)) return SCE_KERNEL_ERROR_EINVAL; + const Apr::AmmUnmapCommand command{{Apr::Opcode::AmmUnmap, sizeof(command)}, address, size}; + return AppendAmm(buffer, &command, sizeof(command)); +} + +std::int64_t APS5_VABI sceAmprAmmMeasureAmmCommandSizeMap(std::uint64_t address, std::uint64_t size, std::int32_t type, std::int32_t protection) { + (void)type; + return ValidMap(address, 0, size, protection) ? std::int64_t{sizeof(Apr::AmmMapCommand)} : AmmMeasureInvalid; +} + +std::int64_t APS5_VABI sceAmprAmmMeasureAmmCommandSizeMapWithGpuMaskId(std::uint64_t address, std::uint64_t size, std::int32_t type, std::int32_t protection, std::uint8_t gpuMaskId) { + (void)gpuMaskId; + return sceAmprAmmMeasureAmmCommandSizeMap(address, size, type, protection); +} + +std::int64_t APS5_VABI sceAmprAmmMeasureAmmCommandSizeMapDirect(std::uint64_t address, std::uint64_t directOffset, std::uint64_t size, std::int32_t type, std::int32_t protection) { + (void)type; + return ValidMap(address, directOffset, size, protection) ? std::int64_t{sizeof(Apr::AmmMapCommand)} : AmmMeasureInvalid; +} + +std::int64_t APS5_VABI sceAmprAmmMeasureAmmCommandSizeMapDirectWithGpuMaskId(std::uint64_t address, std::uint64_t directOffset, std::uint64_t size, std::int32_t type, std::int32_t protection, std::uint8_t gpuMaskId) { + (void)gpuMaskId; + return sceAmprAmmMeasureAmmCommandSizeMapDirect(address, directOffset, size, type, protection); +} + +std::int64_t APS5_VABI sceAmprAmmMeasureAmmCommandSizeUnmap(std::uint64_t address, std::uint64_t size) { + return ValidMapRange(address, size) ? std::int64_t{sizeof(Apr::AmmUnmapCommand)} : AmmMeasureInvalid; +} + +int APS5_VABI sceAmprAmmGiveDirectMemory(std::int64_t searchStart, std::int64_t searchEnd, std::size_t size, std::size_t alignment, int usage, std::int64_t* offset) { + return AmmGiveDirectMemory_nid_no_patch(searchStart, searchEnd, size, alignment, usage, offset); +} + +int APS5_VABI sceAmprAmmGetVirtualAddressRanges(std::uint64_t* start, std::uint64_t* end, std::uint64_t* multimapStart, std::uint64_t* multimapEnd) { + if (!start || !end || !multimapStart || !multimapEnd) throw std::invalid_argument("sceAmprAmmGetVirtualAddressRanges: null output"); + AmmVirtualAddressRanges_nid_no_patch(start, end, multimapStart, multimapEnd); + return 0; +} + +int APS5_VABI sceAmprAmmSubmitCommandBuffer(void* base, std::uint32_t offset, std::uint32_t priority) { + (void)priority; + if (!base) return SCE_KERNEL_ERROR_EINVAL; + AmmSubmit_nid_no_patch(base, offset); + return 0; +} + +int APS5_VABI sceAmprAmmSubmitCommandBuffer2(void* base, std::uint32_t offset, std::uint32_t priority, AmmSubmitResult* result, std::uint32_t* id) { + (void)priority; + if (!base) return SCE_KERNEL_ERROR_EINVAL; + const auto submitted = AmmSubmit_nid_no_patch(base, offset); + if (result) *result = {0, 0}; + if (id) *id = submitted; + return 0; +} + +int APS5_VABI sceAmprAmmSubmitCommandBuffer3(void* base, std::uint32_t offset, std::uint32_t priority, std::uint32_t* id) { + (void)priority; + if (!base) return SCE_KERNEL_ERROR_EINVAL; + const auto submitted = AmmSubmit_nid_no_patch(base, offset); + if (id) *id = submitted; + return 0; +} + +int APS5_VABI sceAmprAmmWaitCommandBufferCompletion(std::uint32_t id) { + return AmmSubmitted_nid_no_patch(id) ? 0 : SCE_KERNEL_ERROR_ESRCH; +} + } diff --git a/core/libs/prx/libkernel/Apr/include/AprCommandBuffer.hpp b/core/libs/prx/libkernel/Apr/include/AprCommandBuffer.hpp index 99bd8beb7..e3125675e 100644 --- a/core/libs/prx/libkernel/Apr/include/AprCommandBuffer.hpp +++ b/core/libs/prx/libkernel/Apr/include/AprCommandBuffer.hpp @@ -24,6 +24,7 @@ struct CommandBufferObject { static_assert(sizeof(CommandBufferObject) == 0x18, "guest reserves 0x18 bytes for sce::Ampr::CommandBuffer"); inline constexpr std::uint16_t ScatterGatherValid = 1; +inline constexpr std::uint16_t MapActive = 2; enum class Opcode : std::uint32_t { Nop = 0, @@ -43,6 +44,10 @@ enum class Opcode : std::uint32_t { ReadFileScatter = 14, ReadFileGatherScatter = 15, ResetGatherScatterState = 16, + AmmMap = 17, + AmmMapDirect = 18, + AmmUnmap = 19, + MapEnd = 20, }; struct CommandHeader { @@ -63,6 +68,25 @@ struct ResetGatherScatterStateCommand { CommandHeader header; }; +struct AmmMapCommand { + CommandHeader header; + std::uint64_t address; + std::uint64_t directOffset; + std::uint64_t size; + std::int32_t type; + std::int32_t protection; +}; + +struct AmmUnmapCommand { + CommandHeader header; + std::uint64_t address; + std::uint64_t size; +}; + +struct MapEndCommand { + CommandHeader header; +}; + struct WriteAddressCommand { CommandHeader header; std::uint64_t address; @@ -131,4 +155,11 @@ struct MarkerCommand { } +extern "C" { +int AmmGiveDirectMemory_nid_no_patch(std::int64_t searchStart, std::int64_t searchEnd, std::size_t size, std::size_t alignment, int usage, std::int64_t* offset); +void AmmVirtualAddressRanges_nid_no_patch(std::uint64_t* start, std::uint64_t* end, std::uint64_t* multimapStart, std::uint64_t* multimapEnd); +std::uint32_t AmmSubmit_nid_no_patch(void* base, std::uint32_t bytes); +bool AmmSubmitted_nid_no_patch(std::uint32_t id); +} + #endif diff --git a/core/libs/prx/libkernel/Apr/src/Apr.cpp b/core/libs/prx/libkernel/Apr/src/Apr.cpp index 7c652701b..c48e0a7b2 100644 --- a/core/libs/prx/libkernel/Apr/src/Apr.cpp +++ b/core/libs/prx/libkernel/Apr/src/Apr.cpp @@ -4,6 +4,7 @@ #include "prx/libc/include/General.hpp" #include "prx/libc/include/GuestArena.hpp" #include "prx/libkernel/Apr/include/AprCommandBuffer.hpp" +#include "prx/libkernel/DirectMemory/DirectMemory.hpp" #include "prx/libkernel/File/include/NativeStat.hpp" #include "prx/libkernel/Equeue/Equeue.hpp" #include "prx/libkernel/Time/include/Time.hpp" @@ -16,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -239,6 +241,103 @@ TCommand _read(const Apr::CommandBufferObject& buffer, std::uint32_t cursor) { return command; } +constexpr std::uint64_t AmmRangeBytes = 32ull << 30; +constexpr int GuestMapFixed = 0x10; + +struct AmmState { + std::mutex lock; + std::uintptr_t base = 0; + std::map pool; + std::map autoPages; + std::atomic lastSubmit{0}; +}; + +AmmState& _amm() { + static AmmState state; + return state; +} + +std::uintptr_t _ammBase(AmmState& state) { + if (state.base == 0) { + void* address = nullptr; + if (DoReserveVirtual(&address, 2 * AmmRangeBytes, 0, 0x200000) != 0) throw std::runtime_error("AMM: cannot reserve the virtual address range"); + state.base = reinterpret_cast(address); + } + return state.base; +} + +void _ammCheckRange(AmmState& state, std::uint64_t address, std::uint64_t size) { + const auto base = _ammBase(state); + if (address < base || size > 2 * AmmRangeBytes || address - base > 2 * AmmRangeBytes - size) throw std::runtime_error("AMM: range outside the AMM virtual address range"); +} + +int _ammHostProtection(std::int32_t protection) { + int host = protection & 0x33; + if ((protection & 0x140) != 0) host |= 1; + if ((protection & 0x280) != 0) host |= 3; + if ((host & 2) != 0) host |= 1; + return host; +} + +void _returnPoolPages(AmmState& state, std::uint64_t offset, std::uint64_t bytes) { + auto next = state.pool.lower_bound(offset); + if (next != state.pool.end() && next->first == offset + bytes) { + bytes += next->second; + next = state.pool.erase(next); + } + if (next != state.pool.begin()) { + const auto previous = std::prev(next); + if (previous->first + previous->second == offset) { + previous->second += bytes; + return; + } + } + state.pool.emplace(offset, bytes); +} + +std::uint64_t _takePoolPages(AmmState& state, std::uint64_t bytes) { + for (auto it = state.pool.begin(); it != state.pool.end(); ++it) { + if (it->second < bytes) continue; + const auto offset = it->first; + const auto remaining = it->second - bytes; + state.pool.erase(it); + if (remaining != 0) state.pool.emplace(offset + bytes, remaining); + return offset; + } + throw std::runtime_error("AMM: no free run of " + std::to_string(bytes) + " bytes in the direct memory given to the mapper"); +} + +void _releaseAutoPages(AmmState& state, std::uint64_t address, std::uint64_t size) { + for (auto it = state.autoPages.lower_bound(address); it != state.autoPages.end() && it->first < address + size;) { + _returnPoolPages(state, it->second, PS5_PAGE_SIZE); + it = state.autoPages.erase(it); + } +} + +void _ammUnmap(const Apr::AmmUnmapCommand& command) { + auto& state = _amm(); + const std::lock_guard lock(state.lock); + _ammCheckRange(state, command.address, command.size); + _releaseAutoPages(state, command.address, command.size); + void* address = reinterpret_cast(command.address); + if (DoReserveVirtual(&address, command.size, GuestMapFixed, 0) != 0) throw std::runtime_error("AMM: unmap failed"); +} + +void _ammMap(const Apr::AmmMapCommand& command, bool direct) { + auto& state = _amm(); + const std::lock_guard lock(state.lock); + _ammCheckRange(state, command.address, command.size); + _releaseAutoPages(state, command.address, command.size); + const auto physical = direct ? command.directOffset : _takePoolPages(state, command.size); + void* address = reinterpret_cast(command.address); + if (DoMapDirect(&address, command.size, _ammHostProtection(command.protection), GuestMapFixed, static_cast(physical), 0) != 0) { + if (!direct) _returnPoolPages(state, physical, command.size); + throw std::runtime_error("AMM: map failed"); + } + if (direct) return; + for (std::uint64_t offset = 0; offset < command.size; offset += PS5_PAGE_SIZE) state.autoPages.emplace(command.address + offset, physical + offset); +} + struct ReadCursor { bool valid = false; std::uint32_t fileId = 0; @@ -260,7 +359,7 @@ void _readResolved(Apr::Opcode opcode, Apr::ReadFileCommand command, ReadCursor& void _execute(const Apr::CommandBufferObject& buffer) { std::uint32_t cursor = 0; ReadCursor read; - for (std::uint32_t index = 0; index < buffer.numCommands; ++index) { + while (cursor < buffer.offset) { if (cursor + sizeof(Apr::CommandHeader) > buffer.offset) throw std::runtime_error("APR: truncated command buffer"); Apr::CommandHeader header; std::memcpy(&header, buffer.base + cursor, sizeof(header)); @@ -280,6 +379,15 @@ void _execute(const Apr::CommandBufferObject& buffer) { case Apr::Opcode::ResetGatherScatterState: read = {}; break; + case Apr::Opcode::AmmMap: + case Apr::Opcode::AmmMapDirect: + _ammMap(_read(buffer, cursor), header.opcode == Apr::Opcode::AmmMapDirect); + break; + case Apr::Opcode::AmmUnmap: + _ammUnmap(_read(buffer, cursor)); + break; + case Apr::Opcode::MapEnd: + break; case Apr::Opcode::WriteAddress: { Apr::WriteAddressCommand command; std::memcpy(&command, buffer.base + cursor, sizeof(command)); @@ -332,6 +440,41 @@ void _execute(const Apr::CommandBufferObject& buffer) { extern "C" { +int APS5_VABI sceKernelAllocateDirectMemory(int64_t searchStart, int64_t searchEnd, size_t length, size_t alignment, int memoryType, int64_t* offset); + +int AmmGiveDirectMemory_nid_no_patch(std::int64_t searchStart, std::int64_t searchEnd, std::size_t size, std::size_t alignment, int usage, std::int64_t* offset) { + if (!offset || (usage != 0 && usage != 1)) return SCE_KERNEL_ERROR_EINVAL; + const int result = sceKernelAllocateDirectMemory(searchStart, searchEnd, size, alignment, 0, offset); + if (result != 0 || usage == 0) return result; + auto& state = _amm(); + const std::lock_guard lock(state.lock); + _returnPoolPages(state, static_cast(*offset), size); + return 0; +} + +void AmmVirtualAddressRanges_nid_no_patch(std::uint64_t* start, std::uint64_t* end, std::uint64_t* multimapStart, std::uint64_t* multimapEnd) { + auto& state = _amm(); + const std::lock_guard lock(state.lock); + const auto base = _ammBase(state); + if (start) *start = base; + if (end) *end = base + AmmRangeBytes; + if (multimapStart) *multimapStart = base + AmmRangeBytes; + if (multimapEnd) *multimapEnd = base + 2 * AmmRangeBytes; +} + +std::uint32_t AmmSubmit_nid_no_patch(void* base, std::uint32_t bytes) { + _execute(Apr::CommandBufferObject{static_cast(base), bytes, bytes, 0, Apr::BufferType::Generic, 0}); + auto& last = _amm().lastSubmit; + std::uint32_t id = last.fetch_add(1) + 1; + while (id == 0) id = last.fetch_add(1) + 1; + return id; +} + +bool AmmSubmitted_nid_no_patch(std::uint32_t id) { + return id != 0 && id <= _amm().lastSubmit.load(); +} + + int APS5_VABI sceKernelAprResolveFilepathsToIds(const char** paths, uint32_t count, uint32_t* ids, uint32_t* error_index) { if (!paths || !ids) return _fail(GUEST_EINVAL); for (uint32_t index = 0; index < count; ++index) { diff --git a/core/libs/tests/GuestAmpr.cpp b/core/libs/tests/GuestAmpr.cpp index 23106e798..06ba7f696 100644 --- a/core/libs/tests/GuestAmpr.cpp +++ b/core/libs/tests/GuestAmpr.cpp @@ -65,6 +65,31 @@ std::uint64_t APS5_VABI sceAmprMeasureCommandSizeReadFileGather(std::uint64_t, s std::uint64_t APS5_VABI sceAmprMeasureCommandSizeReadFileScatter(void*, std::uint64_t); std::uint64_t APS5_VABI sceAmprMeasureCommandSizeReadFileGatherScatter(void*, std::uint64_t, std::uint64_t); std::uint64_t APS5_VABI sceAmprMeasureCommandSizeResetGatherScatterState(); +std::size_t APS5_VABI sceKernelGetDirectMemorySize(); +int APS5_VABI sceAmprAmmCommandBufferConstructor(Apr::CommandBufferObject*); +int APS5_VABI sceAmprAmmCommandBufferMap(Apr::CommandBufferObject*, std::uint64_t, std::uint64_t, std::int32_t, std::int32_t); +int APS5_VABI sceAmprAmmCommandBufferMapWithGpuMaskId(Apr::CommandBufferObject*, std::uint64_t, std::uint64_t, std::int32_t, std::int32_t, std::uint8_t); +int APS5_VABI sceAmprAmmCommandBufferMapDirect(Apr::CommandBufferObject*, std::uint64_t, std::uint64_t, std::uint64_t, std::int32_t, std::int32_t); +int APS5_VABI sceAmprAmmCommandBufferMapDirectWithGpuMaskId(Apr::CommandBufferObject*, std::uint64_t, std::uint64_t, std::uint64_t, std::int32_t, std::int32_t, std::uint8_t); +int APS5_VABI sceAmprAmmCommandBufferUnmap(Apr::CommandBufferObject*, std::uint64_t, std::uint64_t); +std::int64_t APS5_VABI sceAmprAmmMeasureAmmCommandSizeMap(std::uint64_t, std::uint64_t, std::int32_t, std::int32_t); +std::int64_t APS5_VABI sceAmprAmmMeasureAmmCommandSizeMapWithGpuMaskId(std::uint64_t, std::uint64_t, std::int32_t, std::int32_t, std::uint8_t); +std::int64_t APS5_VABI sceAmprAmmMeasureAmmCommandSizeMapDirect(std::uint64_t, std::uint64_t, std::uint64_t, std::int32_t, std::int32_t); +std::int64_t APS5_VABI sceAmprAmmMeasureAmmCommandSizeMapDirectWithGpuMaskId(std::uint64_t, std::uint64_t, std::uint64_t, std::int32_t, std::int32_t, std::uint8_t); +std::int64_t APS5_VABI sceAmprAmmMeasureAmmCommandSizeUnmap(std::uint64_t, std::uint64_t); +int APS5_VABI sceAmprAmmGiveDirectMemory(std::int64_t, std::int64_t, std::size_t, std::size_t, int, std::int64_t*); +int APS5_VABI sceAmprAmmGetVirtualAddressRanges(std::uint64_t*, std::uint64_t*, std::uint64_t*, std::uint64_t*); +int APS5_VABI sceAmprAmmSubmitCommandBuffer(void*, std::uint32_t, std::uint32_t); +int APS5_VABI sceAmprAmmSubmitCommandBuffer2(void*, std::uint32_t, std::uint32_t, std::uint64_t*, std::uint32_t*); +int APS5_VABI sceAmprAmmSubmitCommandBuffer3(void*, std::uint32_t, std::uint32_t, std::uint32_t*); +int APS5_VABI sceAmprAmmWaitCommandBufferCompletion(std::uint32_t); +int APS5_VABI sceAmprAprCommandBufferMapBegin(Apr::CommandBufferObject*, std::uint64_t, std::uint64_t, std::int32_t, std::int32_t); +int APS5_VABI sceAmprAprCommandBufferMapDirectBegin(Apr::CommandBufferObject*, std::uint64_t, std::uint64_t, std::uint64_t, std::int32_t, std::int32_t); +int APS5_VABI sceAmprAprCommandBufferMapEnd(Apr::CommandBufferObject*); +std::uint64_t APS5_VABI sceAmprMeasureCommandSizeMapBegin(std::uint64_t, std::uint64_t, std::uint32_t, std::uint32_t); +std::uint64_t APS5_VABI sceAmprMeasureCommandSizeMapDirectBegin(std::uint64_t, std::uint64_t, std::uint64_t, std::uint32_t, std::uint32_t); +std::uint64_t APS5_VABI sceAmprMeasureCommandSizeMapEnd(); +int APS5_VABI sceAmprCommandBufferWriteCounter_04_00(Apr::CommandBufferObject*, std::uint8_t, std::uint8_t, std::uint64_t, std::uint8_t, std::uint8_t); } static void Require(bool value) { if (!value) std::abort(); } @@ -496,6 +521,144 @@ void TestGatherScatter() { Require(sceAmprMeasureCommandSizeReadFileGatherScatter(nullptr, 4, 0) == sizeof(Apr::ReadFileCommand)); } +constexpr std::uint64_t page = 0x4000; +constexpr int permissionDenied = static_cast(0x80020001); +constexpr int busy = static_cast(0x80020010); +constexpr int noSuchSubmission = static_cast(0x80020003); +constexpr std::int32_t cpuReadWrite = 0x03; +constexpr std::int32_t cpuGpuReadWrite = 0x33; +constexpr std::int32_t amprReadWrite = 0xC0; + +std::uint64_t& At(std::uint64_t address) { + return *reinterpret_cast(address); +} + +void TestAmm() { + std::uint64_t start = 0; + std::uint64_t end = 0; + std::uint64_t multimapStart = 0; + std::uint64_t multimapEnd = 0; + Require(sceAmprAmmGetVirtualAddressRanges(&start, &end, &multimapStart, &multimapEnd) == 0); + Require(start != 0 && start % page == 0 && start < end && end <= multimapStart && multimapStart < multimapEnd); + + const auto directMemory = static_cast(sceKernelGetDirectMemorySize()); + std::int64_t pool = -1; + std::int64_t direct = -1; + Require(sceAmprAmmGiveDirectMemory(0, directMemory, 7 * page, page, 1, &pool) == 0 && pool >= 0); + Require(sceAmprAmmGiveDirectMemory(0, directMemory, 2 * page, page, 0, &direct) == 0 && direct >= 0); + Require(sceAmprAmmGiveDirectMemory(0, directMemory, page, page, 2, &direct) == invalidArgument); + Require(sceAmprAmmGiveDirectMemory(0, directMemory, page, page, 1, nullptr) == invalidArgument); + + const std::uint64_t automatic = start; + const std::uint64_t first = start + 0x10000; + const std::uint64_t alias = start + 0x20000; + const std::uint64_t reused = start + 0x30000; + const std::uint64_t region = start + 0x60000; + const std::uint64_t directRegion = start + 0x70000; + + Recorder maps; + Require(sceAmprAmmCommandBufferConstructor(&maps.buffer) == 0); + auto offset = maps.Offset(); + auto commands = maps.Commands(); + Require(sceAmprAmmCommandBufferMap(&maps.buffer, automatic, 2 * page, 0, cpuReadWrite) == 0); + RequireAppended(maps, offset, commands, Apr::Opcode::AmmMap, sceAmprAmmMeasureAmmCommandSizeMap(automatic, 2 * page, 0, cpuReadWrite)); + offset = maps.Offset(); + commands = maps.Commands(); + Require(sceAmprAmmCommandBufferMapDirect(&maps.buffer, first, direct, 2 * page, 0, cpuGpuReadWrite) == 0); + RequireAppended(maps, offset, commands, Apr::Opcode::AmmMapDirect, sceAmprAmmMeasureAmmCommandSizeMapDirect(first, direct, 2 * page, 0, cpuGpuReadWrite)); + Require(sceAmprAmmCommandBufferMapDirectWithGpuMaskId(&maps.buffer, alias, direct, 2 * page, 0, amprReadWrite, 3) == 0); + std::uint32_t id = 0; + Require(sceAmprAmmSubmitCommandBuffer3(maps.memory.data(), maps.Offset(), 0, &id) == 0 && id != 0); + Require(sceAmprAmmWaitCommandBufferCompletion(id) == 0); + Require(sceAmprAmmWaitCommandBufferCompletion(id + 1000) == noSuchSubmission); + At(automatic) = 0x1111; + At(automatic + 2 * page - 8) = 0x2222; + Require(At(automatic) == 0x1111 && At(automatic + 2 * page - 8) == 0x2222); + At(first + page) = 0x3333; + Require(At(alias + page) == 0x3333); + At(alias) = 0x4444; + Require(At(first) == 0x4444); + + Recorder remaps; + Require(sceAmprAmmCommandBufferConstructor(&remaps.buffer) == 0); + offset = remaps.Offset(); + commands = remaps.Commands(); + Require(sceAmprAmmCommandBufferUnmap(&remaps.buffer, automatic, 2 * page) == 0); + RequireAppended(remaps, offset, commands, Apr::Opcode::AmmUnmap, sceAmprAmmMeasureAmmCommandSizeUnmap(automatic, 2 * page)); + Require(sceAmprAmmCommandBufferMapWithGpuMaskId(&remaps.buffer, reused, 6 * page, 0, cpuReadWrite, 1) == 0); + std::uint64_t result = ~0ull; + Require(sceAmprAmmSubmitCommandBuffer2(remaps.memory.data(), remaps.Offset(), 0, &result, &id) == 0 && result == 0); + Require(sceAmprAmmWaitCommandBufferCompletion(id) == 0); + At(reused + 6 * page - 8) = 0x5555; + Require(At(reused + 6 * page - 8) == 0x5555); + + Recorder apr; + alignas(8) std::uint64_t done = 0; + offset = apr.Offset(); + commands = apr.Commands(); + Require(sceAmprAprCommandBufferMapBegin(&apr.buffer, region, page, 0, amprReadWrite) == 0); + RequireAppended(apr, offset, commands, Apr::Opcode::AmmMap, sceAmprMeasureCommandSizeMapBegin(region, page, 0, amprReadWrite)); + Require(sceAmprAprCommandBufferMapBegin(&apr.buffer, region, page, 0, amprReadWrite) == permissionDenied); + Require(sceAmprAprCommandBufferMapDirectBegin(&apr.buffer, directRegion, direct, page, 0, amprReadWrite) == permissionDenied); + Require(sceAmprCommandBufferWriteAddressOnCompletion(&apr.buffer, &done, 1) == permissionDenied); + Require(sceAmprCommandBufferWriteCounterOnCompletion(&apr.buffer, 30, 1) == permissionDenied); + Require(sceAmprCommandBufferWriteKernelEventQueueOnCompletion(&apr.buffer, 1, 1, 0) == permissionDenied); + Require(sceAmprCommandBufferWriteAddressFromCounter_04_00(&apr.buffer, &done, 30, 0) == permissionDenied); + Require(sceAmprCommandBufferWriteCounter_04_00(&apr.buffer, 30, 1, 5, 0, 0) == permissionDenied); + Require(sceAmprCommandBufferWriteCounter_04_00(&apr.buffer, 30, 1, 5, 0, 1) == 0); + offset = apr.Offset(); + commands = apr.Commands(); + Require(sceAmprAprCommandBufferMapEnd(&apr.buffer) == 0); + RequireAppended(apr, offset, commands, Apr::Opcode::MapEnd, sceAmprMeasureCommandSizeMapEnd()); + Require(sceAmprAprCommandBufferMapEnd(&apr.buffer) == permissionDenied); + Require(sceAmprAprCommandBufferMapDirectBegin(&apr.buffer, directRegion, direct, page, 0, cpuReadWrite) == 0); + Require(sceAmprAprCommandBufferMapEnd(&apr.buffer) == 0); + Require(sceAmprCommandBufferWriteAddressFromCounterOnCompletion(&apr.buffer, &done, 30) == 0); + SubmitWithin10Seconds(apr); + Require(done == 5); + At(region + page - 8) = 0x6666; + Require(At(region + page - 8) == 0x6666); + Require(At(directRegion) == 0x4444); + + const auto rejected = static_cast(static_cast(invalidArgument)); + const std::int64_t ammRejected = invalidArgument; + Recorder empty; + Require(sceAmprAmmCommandBufferMap(&empty.buffer, start, page, 0, 0x04) == invalidArgument); + Require(sceAmprAmmCommandBufferMap(&empty.buffer, start, page, 0, 0x400) == invalidArgument); + Require(sceAmprAmmCommandBufferMap(&empty.buffer, start + 0x1000, page, 0, cpuReadWrite) == invalidArgument); + Require(sceAmprAmmCommandBufferMap(&empty.buffer, start, page + 0x1000, 0, cpuReadWrite) == invalidArgument); + Require(sceAmprAmmCommandBufferMap(&empty.buffer, start, 0, 0, cpuReadWrite) == invalidArgument); + Require(sceAmprAmmCommandBufferMap(&empty.buffer, 0, page, 0, cpuReadWrite) == invalidArgument); + Require(sceAmprAmmCommandBufferMap(&empty.buffer, ~(page - 1), 2 * page, 0, cpuReadWrite) == invalidArgument); + Require(sceAmprAmmCommandBufferMapDirect(&empty.buffer, start, direct + 0x1000, page, 0, cpuReadWrite) == invalidArgument); + Require(sceAmprAmmCommandBufferUnmap(&empty.buffer, start + 8, page) == invalidArgument); + Require(sceAmprAmmCommandBufferMap(nullptr, start, page, 0, cpuReadWrite) == invalidArgument); + Require(sceAmprAprCommandBufferMapBegin(&empty.buffer, start, page, 0, 0x04) == invalidArgument); + Require(sceAmprAprCommandBufferMapEnd(nullptr) == invalidArgument); + Require(empty.Offset() == 0 && empty.Commands() == 0); + + Apr::CommandBufferObject unbound{}; + Require(sceAmprCommandBufferConstructor(&unbound) == 0); + Require(sceAmprAmmCommandBufferMap(&unbound, start, page, 0, cpuReadWrite) == permissionDenied); + Require(sceAmprAmmCommandBufferUnmap(&unbound, start, page) == permissionDenied); + const auto measured = static_cast(sceAmprAmmMeasureAmmCommandSizeUnmap(start, page)); + Recorder exact(measured); + Require(sceAmprAmmCommandBufferUnmap(&exact.buffer, start, page) == 0); + Require(sceAmprAmmCommandBufferUnmap(&exact.buffer, start, page) == busy); + Require(sceAmprAmmCommandBufferMap(&exact.buffer, start, page, 0, cpuReadWrite) == busy); + Require(sceAmprAmmSubmitCommandBuffer(nullptr, 0, 0) == invalidArgument); + + Require(sceAmprAmmMeasureAmmCommandSizeMap(start, page, 0, 0x04) == ammRejected); + Require(sceAmprAmmMeasureAmmCommandSizeMapWithGpuMaskId(start + 8, page, 0, cpuReadWrite, 0) == ammRejected); + Require(sceAmprAmmMeasureAmmCommandSizeMapWithGpuMaskId(start, page, 0, cpuReadWrite, 0) == std::int64_t{sizeof(Apr::AmmMapCommand)}); + Require(sceAmprAmmMeasureAmmCommandSizeMapDirect(start, direct + 8, page, 0, cpuReadWrite) == ammRejected); + Require(sceAmprAmmMeasureAmmCommandSizeMapDirectWithGpuMaskId(start, direct, page, 0, cpuReadWrite, 0) == std::int64_t{sizeof(Apr::AmmMapCommand)}); + Require(sceAmprAmmMeasureAmmCommandSizeUnmap(start, 0) == ammRejected); + Require(sceAmprMeasureCommandSizeMapBegin(start, page, 0, 0x04) == rejected); + Require(sceAmprMeasureCommandSizeMapDirectBegin(start, direct + 8, page, 0, cpuReadWrite) == rejected); + Require(sceAmprMeasureCommandSizeMapDirectBegin(start, direct, page, 0, cpuReadWrite) == sizeof(Apr::AmmMapCommand)); +} + } int main() { @@ -515,5 +678,6 @@ int main() { TestVersionedCounters(); TestConstructed(); TestGatherScatter(); + TestAmm(); return 0; } diff --git a/docs/dev/TechnicalDebt.md b/docs/dev/TechnicalDebt.md index cc45f9093..da450c83a 100644 --- a/docs/dev/TechnicalDebt.md +++ b/docs/dev/TechnicalDebt.md @@ -35,6 +35,8 @@ Throughout the project, every function at every stage either **does exactly what - [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 - [APR gather and scatter reads](../../core/libs/prx/libkernel/Apr/src/Apr.cpp) (libSceAmpr) - signatures, argument checks and the read cursor taken from the [ampr_emu](https://github.com/drakmor/ampr_emu) reimplementation, not confirmed on a title: each read leaves the file, the next file offset and the next destination; a gather reads that file at a new offset into the next destination, a scatter continues in the file into a new destination, a gather-scatter takes both. Recording one needs a read file since the last `ResetGatherScatterState`; like ampr_emu, `sceAmprCommandBufferReset` keeps that state, and a gather or scatter with no read before it in the submitted buffer throws instead of returning `SCE_AMPR_ERROR_APR_INVALIDGATHERSCATTERSTATE` +- [AMM map, map direct and unmap](../../core/libs/prx/libkernel/Apr/src/Apr.cpp) (libSceAmpr) - signatures and the protection mask (CPU, GPU, AMPR and ACP read and write) taken from the [ampr_emu](https://github.com/drakmor/ampr_emu) reimplementation, which hands the remaining checks and the mapping to the console kernel; the mapper model taken from [PS5PCEM](https://github.com/iStark/PS5PCEM): `sceAmprAmmGiveDirectMemory` usage 1 gives the allocated direct memory to the pool plain maps take pages from, usage 0 only allocates it. Not confirmed on a title. The AMM virtual address ranges are a 32 GiB range and a 32 GiB multimap range reserved in the guest arena on first use, not the console's; maps outside them throw. Addresses, sizes and direct offsets must be 16 KiB aligned. The memory type and the GPU mask are ignored, AMPR and ACP access maps as CPU access, unmapped pages go back to the pool and stay reserved. Submission runs the buffer before returning, the submit result is always 0 and a failing command throws; the AMM measures return errors sign-extended, as ampr_emu does. `sceAmprAmmGetVirtualAddressRanges` throws for a null output +- [APR map begin and end](../../core/libs/prx/libSceAmpr/Export.cpp) (libSceAmpr) - as in ampr_emu, `MapBegin`/`MapDirectBegin` map when they run and `MapEnd` only closes the region; while a region is open a second begin and every completion write return `EPERM`, the at-start `_04_00` writes are accepted - [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`