diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 3dbe96d99d..7813e6b53e 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -641,7 +641,7 @@ jobs: uses: actions/cache/restore@v6 with: path: configure-seed.cmake - key: configure-seed-${{ env.WINLIBS_ZIP }}-${{ steps.submodules.outputs.key }}-${{ hashFiles('CMakeLists.txt', 'cmake/**', 'core/libs/CMakeLists.txt', '.github/workflows/build.yml') }} + key: configure-seed-${{ env.WINLIBS_ZIP }}-${{ steps.submodules.outputs.key }}-${{ hashFiles('CMakeLists.txt', 'cmake/**', 'core/libs/CMakeLists.txt', 'tools/configure_seed.py', '.github/workflows/build.yml') }} - shell: cmd run: if exist configure-seed.cmake (cmake -C configure-seed.cmake -S . -B build %CONFIGURE% "-DANYPS5_AGC_EXECUTION_TESTS=F16ToF32Unpack;F64FmaSubnormals;RegistrationPrepareShutdown") else (cmake -S . -B build %CONFIGURE% "-DANYPS5_AGC_EXECUTION_TESTS=F16ToF32Unpack;F64FmaSubnormals;RegistrationPrepareShutdown") - if: steps.seed-cache.outputs.cache-hit != 'true' diff --git a/CMakeLists.txt b/CMakeLists.txt index 25d411aa58..e097e7820c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,13 +6,12 @@ endif() project(Relinker CXX) set(ANYPS5_TEST_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/tests") -set(ANYPS5_WINDOWS_TEST_EXIT "${CMAKE_CURRENT_SOURCE_DIR}/cmake/WindowsTestExit.cpp") function(add_test_executable target) add_executable(${target} ${ARGN}) set_target_properties(${target} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "$<1:${ANYPS5_TEST_OUTPUT_DIRECTORY}>") if(MINGW) - target_sources(${target} PRIVATE "${ANYPS5_WINDOWS_TEST_EXIT}") + target_sources(${target} PRIVATE $) target_link_options(${target} PRIVATE LINKER:--wrap=main) endif() endfunction() @@ -96,6 +95,10 @@ if(APPLE AND NOT ANYPS5_RELINKER_ONLY) add_link_options(-fexperimental-library) endif() +if(MINGW) + add_library(anyps5_windows_test_exit OBJECT cmake/WindowsTestExit.cpp) +endif() + if(BUILD_TESTING) add_test_executable(shader_cache_file_read_tests ${CMAKE_CURRENT_SOURCE_DIR}/core/shader/recompiler/tests/CacheFileReadTests.cpp) diff --git a/core/libs/CMakeLists.txt b/core/libs/CMakeLists.txt index 8a8deb82fa..7d10d9716e 100644 --- a/core/libs/CMakeLists.txt +++ b/core/libs/CMakeLists.txt @@ -382,7 +382,7 @@ if(BUILD_TESTING) if(WIN32) add_test_executable(windows_write_watch_tests tests/WindowsWriteWatch.cpp) target_include_directories(windows_write_watch_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) - target_compile_definitions(windows_write_watch_tests PRIVATE _WIN32_WINNT=0x0A00) + target_compile_definitions(windows_write_watch_tests PRIVATE _WIN32_WINNT=0x0A00 APS5_COUNT_MAPPING_QUERIES=1) if(MINGW) target_compile_options(windows_write_watch_tests PRIVATE -fasynchronous-unwind-tables) endif() @@ -748,6 +748,16 @@ if(BUILD_TESTING) "PATH=path_list_prepend:$") endif() + add_test_executable(guest_np_cpp_web_api_offline_tests tests/GuestNpCppWebApiOffline.cpp) + target_include_directories(guest_np_cpp_web_api_offline_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) + target_link_libraries(guest_np_cpp_web_api_offline_tests PRIVATE libSceNpCppWebApi libc) + configure_windows_unwind(guest_np_cpp_web_api_offline_tests) + add_test(NAME guest_np_cpp_web_api_offline COMMAND guest_np_cpp_web_api_offline_tests) + if(WIN32) + set_tests_properties(guest_np_cpp_web_api_offline PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + endif() + add_test_executable(savedata_core_write_replacement_tests tests/SaveDataWriteReplacement.cpp) target_include_directories(savedata_core_write_replacement_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) target_link_libraries(savedata_core_write_replacement_tests PRIVATE libSceSaveData) @@ -850,6 +860,16 @@ if(BUILD_TESTING) "PATH=path_list_prepend:$") endif() + add_test_executable(guest_gnm_neo_embedded_tests tests/GuestGnmNeoEmbedded.cpp) + target_include_directories(guest_gnm_neo_embedded_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) + target_link_libraries(guest_gnm_neo_embedded_tests PRIVATE libSceGnmDriverForNeoMode libc) + configure_windows_unwind(guest_gnm_neo_embedded_tests) + add_test(NAME guest_gnm_neo_embedded COMMAND guest_gnm_neo_embedded_tests) + if(WIN32) + set_tests_properties(guest_gnm_neo_embedded PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + endif() + add_test_executable(guest_np_game_intent_tests tests/GuestNpGameIntent.cpp) target_include_directories(guest_np_game_intent_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) target_link_libraries(guest_np_game_intent_tests PRIVATE libSceNpGameIntent libc) @@ -860,6 +880,16 @@ if(BUILD_TESTING) "PATH=path_list_prepend:$") endif() + add_test_executable(guest_gnm_neo_shaders_tests tests/GuestGnmNeoShaders.cpp) + target_include_directories(guest_gnm_neo_shaders_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) + target_link_libraries(guest_gnm_neo_shaders_tests PRIVATE libSceGnmDriverForNeoMode libc) + configure_windows_unwind(guest_gnm_neo_shaders_tests) + add_test(NAME guest_gnm_neo_shaders COMMAND guest_gnm_neo_shaders_tests) + if(WIN32) + set_tests_properties(guest_gnm_neo_shaders PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + endif() + add_test_executable(guest_vorbis_dec_tests tests/GuestVorbisDec.cpp) target_include_directories(guest_vorbis_dec_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) target_compile_definitions(guest_vorbis_dec_tests PRIVATE OV_EXCLUDE_STATIC_CALLBACKS) diff --git a/core/libs/prx/libSceAgc/Acb/src/Control.cpp b/core/libs/prx/libSceAgc/Acb/src/Control.cpp index 567bfd5e18..1dd0314800 100644 --- a/core/libs/prx/libSceAgc/Acb/src/Control.cpp +++ b/core/libs/prx/libSceAgc/Acb/src/Control.cpp @@ -102,6 +102,18 @@ uint32_t* APS5_VABI sceAgcAcbPushMarker(CommandBuffer* buf, const char* str, uin return Agc::Marker::Push(buf, str, __func__); } +uint32_t* APS5_VABI sceAgcAcbPushMarkerSpan(CommandBuffer* buf, const char* str, uint32_t length, uint32_t color) { + (void)color; + return Agc::Marker::PushSpan(buf, str, length, __func__); +} + +uint32_t* APS5_VABI sceAgcAcbSetMarkerSpan(CommandBuffer* buf, const char* str, uint32_t length, uint32_t color) { + (void)color; + auto* packet = Agc::Marker::PushSpan(buf, str, length, __func__); + Agc::Marker::Pop(buf, __func__); + return packet; +} + uint32_t* APS5_VABI sceAgcAcbPopMarker(CommandBuffer* buf) { return Agc::Marker::Pop(buf, __func__); } diff --git a/core/libs/prx/libSceAgc/DcbState/include/Marker.hpp b/core/libs/prx/libSceAgc/DcbState/include/Marker.hpp index c0ccfdea02..688c0a80b2 100644 --- a/core/libs/prx/libSceAgc/DcbState/include/Marker.hpp +++ b/core/libs/prx/libSceAgc/DcbState/include/Marker.hpp @@ -7,6 +7,7 @@ namespace Agc::Marker { std::uint32_t* Push(CommandBuffer* buf, const char* str, const char* function); +std::uint32_t* PushSpan(CommandBuffer* buf, const char* str, std::uint32_t length, const char* function); std::uint32_t* Pop(CommandBuffer* buf, const char* function); } diff --git a/core/libs/prx/libSceAgc/DcbState/src/Marker.cpp b/core/libs/prx/libSceAgc/DcbState/src/Marker.cpp index 7e88856ccc..12304f16a3 100644 --- a/core/libs/prx/libSceAgc/DcbState/src/Marker.cpp +++ b/core/libs/prx/libSceAgc/DcbState/src/Marker.cpp @@ -22,13 +22,21 @@ namespace Agc::Marker { std::uint32_t* Push(CommandBuffer* buf, const char* str, const char* function) { Agc::Command::Require(buf != nullptr, function, "null command buffer"); const char* text = str ? str : ""; - const std::size_t bytes = std::strlen(text) + 1; + const std::size_t length = std::strlen(text); + Agc::Command::Require(length < 0x10000u, function, "marker text too long"); + return PushSpan(buf, text, static_cast(length), function); +} + +std::uint32_t* PushSpan(CommandBuffer* buf, const char* str, std::uint32_t length, const char* function) { + Agc::Command::Require(buf != nullptr, function, "null command buffer"); + Agc::Command::Require(str != nullptr || length == 0, function, "null marker text"); + const std::size_t bytes = std::size_t{length} + 1; const auto payload = static_cast((bytes + 3) / 4); Agc::Command::Require(payload <= 0x4000u, function, "marker text too long"); auto* packet = Agc::Command::Allocate(buf, payload + 1, function); packet[0] = Agc::Command::Header(OpcodeNop, payload + 1, CustomPushMarker << 2); std::memset(packet + 1, 0, payload * sizeof(std::uint32_t)); - std::memcpy(packet + 1, text, bytes); + if (length != 0) std::memcpy(packet + 1, str, length); return packet; } @@ -60,4 +68,16 @@ uint32_t* APS5_VABI sceAgcDcbPushMarker(CommandBuffer* buf, const char* str, uin return Agc::Marker::Push(buf, str, __func__); } +uint32_t* APS5_VABI sceAgcDcbPushMarkerSpan(CommandBuffer* buf, const char* str, uint32_t length, uint32_t color) { + (void)color; + return Agc::Marker::PushSpan(buf, str, length, __func__); +} + +uint32_t* APS5_VABI sceAgcDcbSetMarkerSpan(CommandBuffer* buf, const char* str, uint32_t length, uint32_t color) { + (void)color; + auto* packet = Agc::Marker::PushSpan(buf, str, length, __func__); + Agc::Marker::Pop(buf, __func__); + return packet; +} + } diff --git a/core/libs/prx/libSceAgc/Misc/src/Semaphore.cpp b/core/libs/prx/libSceAgc/Misc/src/Semaphore.cpp index 3ff95978d3..c7ec65c170 100644 --- a/core/libs/prx/libSceAgc/Misc/src/Semaphore.cpp +++ b/core/libs/prx/libSceAgc/Misc/src/Semaphore.cpp @@ -11,37 +11,55 @@ namespace { +constexpr int GRAPHICS5_ERROR_INVALID_ALIGNMENT = static_cast(0x8a6c0002u); +constexpr int GRAPHICS5_ERROR_OUT_OF_RANGE = static_cast(0x8a6c000bu); +constexpr int GRAPHICS5_ERROR_SEMAPHORE_MEMORY_REGISTERED = static_cast(0x8a6c0048u); +constexpr int GRAPHICS5_ERROR_SEMAPHORE_MEMORY_UNREGISTERED = static_cast(0x8a6c0049u); constexpr std::uint64_t semaphoreMemoryAlignment = 0x4000u; +constexpr std::uint64_t semaphoreMemoryLimit = 0x40000000u; constexpr std::uint64_t semaphoreLabelBytes = 32u; std::mutex semaphoreMutex; std::uint8_t* semaphoreMemory = nullptr; std::uint64_t semaphoreBytes = 0; +bool aligned(std::uint64_t value) { + return (value & (semaphoreMemoryAlignment - 1u)) == 0; +} +int registerSemaphoreMemory(void* memory, std::uint64_t sizeInBytes, bool limited, const char* name) { + const auto address = reinterpret_cast(memory); + std::lock_guard lock(semaphoreMutex); + if (semaphoreBytes != 0) return GRAPHICS5_ERROR_SEMAPHORE_MEMORY_REGISTERED; + if (!aligned(sizeInBytes)) return GRAPHICS5_ERROR_INVALID_ALIGNMENT; + if (limited && sizeInBytes > semaphoreMemoryLimit) return GRAPHICS5_ERROR_OUT_OF_RANGE; + if (!aligned(address)) return GRAPHICS5_ERROR_INVALID_ALIGNMENT; + if (sizeInBytes != 0) { + Agc::Command::Require(memory != nullptr, name, "null semaphore memory with a non-zero size"); + Agc::Command::Require(sizeInBytes - 1u <= std::numeric_limits::max() - address, name, "semaphore memory wraps the address space"); + std::memset(memory, 0, sizeInBytes); + } + semaphoreMemory = static_cast(memory); + semaphoreBytes = sizeInBytes; + return 0; +} } extern "C" { int APS5_VABI sceAgcSetAmmSemaphoreMemory(void* memory, std::uint64_t sizeInBytes) { - const auto address = reinterpret_cast(memory); - std::lock_guard lock(semaphoreMutex); - if (semaphoreBytes != 0) return static_cast(0x8a6c0048u); - if (((address | sizeInBytes) & (semaphoreMemoryAlignment - 1u)) != 0) return static_cast(0x8a6c0002u); - Agc::Command::Require(memory != nullptr && sizeInBytes != 0, __func__, "null or empty AMM semaphore memory; the console's error is unknown"); - Agc::Command::Require(sizeInBytes - 1u <= std::numeric_limits::max() - address, __func__, "AMM semaphore memory wraps the address space"); - auto* labels = static_cast(memory); - std::memset(labels, 0, sizeInBytes); - semaphoreMemory = labels; - semaphoreBytes = sizeInBytes; - return 0; + return registerSemaphoreMemory(memory, sizeInBytes, false, __func__); +} + +int APS5_VABI sceAgcSetSemaphoreMemory(void* memory, std::uint64_t sizeInBytes) { + return registerSemaphoreMemory(memory, sizeInBytes, true, __func__); } int APS5_VABI sceAgcGetSemaphoreLabel(std::uint32_t index, void** label) { std::lock_guard lock(semaphoreMutex); - if (semaphoreBytes == 0) return static_cast(0x8a6c0049u); - if (index >= semaphoreBytes / semaphoreLabelBytes) return static_cast(0x8a6c000bu); + if (semaphoreBytes == 0) return GRAPHICS5_ERROR_SEMAPHORE_MEMORY_UNREGISTERED; + if (index >= semaphoreBytes / semaphoreLabelBytes) return GRAPHICS5_ERROR_OUT_OF_RANGE; Agc::Command::Require(label != nullptr, __func__, "null semaphore label output"); *label = semaphoreMemory + static_cast(index) * semaphoreLabelBytes; return 0; diff --git a/core/libs/prx/libSceAgc/Unimplemented.cpp b/core/libs/prx/libSceAgc/Unimplemented.cpp index d7751bec51..487d737193 100644 --- a/core/libs/prx/libSceAgc/Unimplemented.cpp +++ b/core/libs/prx/libSceAgc/Unimplemented.cpp @@ -5,39 +5,19 @@ extern "C" { -std::uint32_t* APS5_VABI sceAgcAcbPushMarkerSpan(CommandBuffer* buffer, const char* text, std::uint32_t length, std::uint32_t color) { - NotImplemented_nid_no_patch(__func__); - return 0; -} - -std::uint32_t* APS5_VABI sceAgcAcbSetMarkerSpan(CommandBuffer* buffer, const char* text, std::uint32_t length, std::uint32_t color) { - NotImplemented_nid_no_patch(__func__); - return 0; -} - int APS5_VABI sceAgcAcquireMemSetEngine(std::uint32_t* packet, std::uint8_t engine) { NotImplemented_nid_no_patch(__func__); return 0; } -std::uint32_t* APS5_VABI sceAgcDcbPushMarkerSpan(CommandBuffer* buffer, const char* text, std::uint32_t length, std::uint32_t color) { - NotImplemented_nid_no_patch(__func__); - return 0; -} - -std::uint32_t* APS5_VABI sceAgcDcbSetMarkerSpan(CommandBuffer* buffer, const char* text, std::uint32_t length, std::uint32_t color) { - NotImplemented_nid_no_patch(__func__); - return 0; -} - -int APS5_VABI sceAgcSetSemaphoreMemory(void* memory, std::uint64_t size) { - NotImplemented_nid_no_patch(__func__); - return 0; -} - std::uint32_t* APS5_VABI sceAgcCbMemsetExclusive(CommandBuffer* buffer, void* destination, const void* pattern, std::uint64_t size) { NotImplemented_nid_no_patch(__func__); return 0; } +int APS5_VABI sceAgcAcbInitializeDefaultHardwareState() { + NotImplemented_nid_no_patch(__func__); + return 0; +} + } diff --git a/core/libs/prx/libSceAgc/tests/Commands.cpp b/core/libs/prx/libSceAgc/tests/Commands.cpp index e497ed1fa5..bcda442f6b 100644 --- a/core/libs/prx/libSceAgc/tests/Commands.cpp +++ b/core/libs/prx/libSceAgc/tests/Commands.cpp @@ -48,6 +48,10 @@ extern "C" std::uint32_t* APS5_VABI sceAgcAcbSetWorkloadStreamInactive(CommandBu extern "C" std::uint32_t* APS5_VABI sceAgcDcbSetMarker(CommandBuffer* buf, const char* str, std::uint32_t color); extern "C" std::uint32_t* APS5_VABI sceAgcAcbPushMarker(CommandBuffer* buf, const char* str, std::uint32_t color); extern "C" std::uint32_t* APS5_VABI sceAgcAcbPopMarker(CommandBuffer* buf); +extern "C" std::uint32_t* APS5_VABI sceAgcAcbPushMarkerSpan(CommandBuffer* buf, const char* str, std::uint32_t length, std::uint32_t color); +extern "C" std::uint32_t* APS5_VABI sceAgcAcbSetMarkerSpan(CommandBuffer* buf, const char* str, std::uint32_t length, std::uint32_t color); +extern "C" std::uint32_t* APS5_VABI sceAgcDcbPushMarkerSpan(CommandBuffer* buf, const char* str, std::uint32_t length, std::uint32_t color); +extern "C" std::uint32_t* APS5_VABI sceAgcDcbSetMarkerSpan(CommandBuffer* buf, const char* str, std::uint32_t length, std::uint32_t color); extern "C" std::uint32_t* APS5_VABI sceAgcAcbSetMarker(CommandBuffer* buf, const char* str, std::uint32_t color); extern "C" std::uint32_t* APS5_VABI sceAgcDcbSetIndexBuffer(CommandBuffer* buf, std::uint64_t indexAddress); extern "C" std::uint32_t* APS5_VABI sceAgcDcbSetIndexIndirectArgs(CommandBuffer* buf, std::uint64_t attributeAddress, std::uint32_t attributeIndex); @@ -225,6 +229,29 @@ void testMarkers() { expectFailure([] { sceAgcAcbSetMarker(nullptr, "frame", 0); }); } +void testMarkerSpans() { + Storage dcb; + Storage acb; + const char text[] = "framebuffer"; + const auto* acbPush = sceAgcAcbPushMarkerSpan(&acb.buffer, text, 5, 0x00ff00u); + const auto* dcbPush = sceAgcDcbPushMarkerSpan(&dcb.buffer, text, 5, 0xff0000u); + check(acbPush == acb.words.data() && acbPush[0] == Agc::Command::Header(0x10, 3, 0x0bu << 2u), "ACB push marker span header mismatch"); + check(std::strcmp(reinterpret_cast(acbPush + 1), "frame") == 0, "ACB push marker span does not stop at its length"); + check(dcbPush == dcb.words.data(), "DCB push marker span did not return its packet"); + const auto* acbSet = sceAgcAcbSetMarkerSpan(&acb.buffer, nullptr, 0, 0); + const auto* dcbSet = sceAgcDcbSetMarkerSpan(&dcb.buffer, nullptr, 0, 0); + check(acbSet == acb.words.data() + 3 && dcbSet == dcb.words.data() + 3, "set marker span did not return its push packet"); + check(acbSet[0] == Agc::Command::Header(0x10, 2, 0x0bu << 2u) && acbSet[1] == 0 && acbSet[2] == Agc::Command::Header(0x10, 2, 0x0cu << 2u), "ACB set marker span is not a push and pop pair"); + check(dcb.words == acb.words && acb.buffer.cursor_up == acb.words.data() + 7, "ACB and DCB marker spans differ"); + Storage plain; + sceAgcAcbPushMarker(&plain.buffer, "frame", 0); + check(std::equal(plain.words.begin(), plain.words.begin() + 3, acb.words.begin()), "marker span differs from the same text pushed whole"); + expectFailure([] { sceAgcAcbPushMarkerSpan(nullptr, "frame", 5, 0); }); + expectFailure([] { sceAgcAcbPushMarker(nullptr, reinterpret_cast(std::uintptr_t{0x10}), 0); }); + expectFailure([] { sceAgcDcbSetMarkerSpan(nullptr, "frame", 5, 0); }); + expectFailure([&] { sceAgcDcbPushMarkerSpan(&dcb.buffer, nullptr, 4, 0); }); +} + void testWorkloads() { Storage dcb; const std::uint32_t ids[2] = {3, 4}; @@ -822,6 +849,7 @@ int main(int argc, char** argv) { testClearState(); testIndexedIndirectDraws(); testMarkers(); + testMarkerSpans(); testWorkloads(); testWorkloadStreams(); testIndexBuffer(); diff --git a/core/libs/prx/libSceAgc/tests/Semaphore.cpp b/core/libs/prx/libSceAgc/tests/Semaphore.cpp index 362d180ad6..4ab4f08a83 100644 --- a/core/libs/prx/libSceAgc/tests/Semaphore.cpp +++ b/core/libs/prx/libSceAgc/tests/Semaphore.cpp @@ -13,6 +13,7 @@ #include extern "C" int APS5_VABI sceAgcSetAmmSemaphoreMemory(void* memory, std::uint64_t sizeInBytes); +extern "C" int APS5_VABI sceAgcSetSemaphoreMemory(void* memory, std::uint64_t sizeInBytes); extern "C" int APS5_VABI sceAgcGetSemaphoreLabel(std::uint32_t index, void** label); extern "C" std::uint32_t* APS5_VABI sceAgcCbMemSemaphore(CommandBuffer* buf, const volatile void* address, std::uint32_t operation, std::uint32_t modifier, std::uint32_t value); extern "C" std::uint32_t* APS5_VABI sceAgcAcbMemSemaphore(CommandBuffer* buf, const volatile void* address, std::uint8_t waitForMailbox, std::uint8_t signalType, std::uint8_t operation); @@ -22,12 +23,15 @@ namespace { constexpr std::uint64_t blockBytes = 0x4000u; constexpr std::uint64_t labelBytes = 32u; -constexpr std::uint64_t counterBytes = 8u; constexpr std::uint64_t memoryBytes = 2u * blockBytes; constexpr std::uint64_t storageBytes = 4u * blockBytes; constexpr std::uint32_t labelCount = static_cast(memoryBytes / labelBytes); constexpr std::uint8_t guardByte = 0xa5u; -constexpr std::uint8_t counterByte = 0x5au; +constexpr std::uint8_t labelByte = 0x5au; +constexpr int InvalidAlignment = static_cast(0x8a6c0002u); +constexpr int OutOfRange = static_cast(0x8a6c000bu); +constexpr int AlreadyRegistered = static_cast(0x8a6c0048u); +constexpr int NotRegistered = static_cast(0x8a6c0049u); struct AlignedRelease { void operator()(std::uint8_t* memory) const { ::operator delete(memory, std::align_val_t{blockBytes}); } @@ -64,28 +68,28 @@ void checkUnchanged(const std::vector& before, const char* message void fillStorage() { std::fill(storage.get(), storage.get() + storageBytes, guardByte); - for (std::uint64_t label = 0; label < memoryBytes; label += labelBytes) { - std::fill(memory() + label, memory() + label + counterBytes, counterByte); - std::fill(memory() + label + counterBytes, memory() + label + labelBytes, std::uint8_t{0}); - } + std::fill(memory(), memory() + memoryBytes, labelByte); } void testUnregistered() { std::array labels{storage.get(), storage.get(), storage.get()}; - check(sceAgcGetSemaphoreLabel(0, &labels[1]) == static_cast(0x8a6c0049u), "unregistered label error mismatch"); - check(sceAgcGetSemaphoreLabel(0, nullptr) == static_cast(0x8a6c0049u), "unregistered null label error mismatch"); + check(sceAgcGetSemaphoreLabel(0, &labels[1]) == NotRegistered, "a lookup before registration was not rejected as unregistered"); + check(sceAgcGetSemaphoreLabel(0, nullptr) == NotRegistered, "a null lookup before registration was not rejected as unregistered"); check(labels[0] == storage.get() && labels[1] == storage.get() && labels[2] == storage.get(), "a rejected semaphore label changed the output"); } void testRegistrationRejections() { const auto before = snapshot(); expectFailure([] { sceAgcSetAmmSemaphoreMemory(nullptr, memoryBytes); }); - expectFailure([] { sceAgcSetAmmSemaphoreMemory(nullptr, 0); }); - expectFailure([] { sceAgcSetAmmSemaphoreMemory(memory(), 0); }); - check(sceAgcSetAmmSemaphoreMemory(memory() + 1, memoryBytes) == static_cast(0x8a6c0002u), "unaligned semaphore memory error mismatch"); - check(sceAgcSetAmmSemaphoreMemory(memory() + labelBytes, blockBytes) == static_cast(0x8a6c0002u), "unaligned semaphore memory error mismatch"); - check(sceAgcSetAmmSemaphoreMemory(memory(), blockBytes - 1u) == static_cast(0x8a6c0002u), "unaligned semaphore memory error mismatch"); - check(sceAgcSetAmmSemaphoreMemory(memory(), blockBytes + labelBytes) == static_cast(0x8a6c0002u), "unaligned semaphore memory error mismatch"); + check(sceAgcSetAmmSemaphoreMemory(memory() + 1, memoryBytes) == InvalidAlignment, "unaligned memory was not rejected"); + check(sceAgcSetAmmSemaphoreMemory(memory() + labelBytes, blockBytes) == InvalidAlignment, "memory aligned only to a label was not rejected"); + check(sceAgcSetAmmSemaphoreMemory(memory(), blockBytes - 1u) == InvalidAlignment, "an unaligned size was not rejected"); + check(sceAgcSetAmmSemaphoreMemory(memory(), blockBytes + labelBytes) == InvalidAlignment, "a size aligned only to a label was not rejected"); + check(sceAgcSetSemaphoreMemory(memory() + 1, blockBytes) == InvalidAlignment, "unaligned memory was not rejected by the non-AMM call"); + check(sceAgcSetSemaphoreMemory(memory(), 0x40000000u + blockBytes) == OutOfRange, "a size above 1 GiB was not rejected by the non-AMM call"); + check(sceAgcSetSemaphoreMemory(memory() + 1, 0x40000000u + blockBytes) == OutOfRange, "the size limit was not checked before the memory alignment"); + check(sceAgcSetSemaphoreMemory(memory(), blockBytes - 1u) == InvalidAlignment, "the size alignment was not checked first"); + check(sceAgcSetAmmSemaphoreMemory(nullptr, 0) == 0, "an empty registration was rejected"); expectFailure([] { const auto top = std::numeric_limits::max() & ~static_cast(blockBytes - 1u); sceAgcSetAmmSemaphoreMemory(reinterpret_cast(top), memoryBytes); @@ -98,7 +102,7 @@ void testRegistration() { std::fill(memory(), memory() + memoryBytes, std::uint8_t{0x77}); const auto before = snapshot(); check(sceAgcSetAmmSemaphoreMemory(memory(), memoryBytes) == 0, "registering the AMM semaphore memory failed"); - check(std::all_of(memory(), memory() + memoryBytes, [](std::uint8_t byte) { return byte == 0; }), "registration did not clear the semaphore counters"); + check(std::all_of(memory(), memory() + memoryBytes, [](std::uint8_t byte) { return byte == 0; }), "registration did not clear the whole semaphore memory"); check(std::equal(before.begin(), before.begin() + blockBytes, storage.get()), "registration wrote before the semaphore memory"); check(std::equal(before.begin() + blockBytes + memoryBytes, before.end(), memory() + memoryBytes), "registration wrote past the semaphore memory"); } @@ -115,7 +119,7 @@ void testLabels() { void testLabelRejections() { for (const std::uint32_t index : {labelCount, labelCount + 1u, std::numeric_limits::max()}) { void* label = storage.get(); - check(sceAgcGetSemaphoreLabel(index, &label) == static_cast(0x8a6c000bu), "out-of-range label error mismatch"); + check(sceAgcGetSemaphoreLabel(index, &label) == OutOfRange, "a label past the semaphore memory was not rejected"); check(label == storage.get(), "a rejected semaphore label changed the output"); } expectFailure([] { sceAgcGetSemaphoreLabel(0, nullptr); }); @@ -124,9 +128,10 @@ void testLabelRejections() { void testRepeatedRegistration() { std::fill(memory(), memory() + memoryBytes, std::uint8_t{0x3c}); const auto before = snapshot(); - check(sceAgcSetAmmSemaphoreMemory(memory(), memoryBytes) == static_cast(0x8a6c0048u), "repeated registration error mismatch"); - check(sceAgcSetAmmSemaphoreMemory(storage.get(), blockBytes) == static_cast(0x8a6c0048u), "repeated registration error mismatch"); - check(sceAgcSetAmmSemaphoreMemory(nullptr, 0) == static_cast(0x8a6c0048u), "repeated registration error mismatch"); + check(sceAgcSetAmmSemaphoreMemory(memory(), memoryBytes) == AlreadyRegistered, "a repeated registration was not rejected"); + check(sceAgcSetAmmSemaphoreMemory(storage.get() + 1, blockBytes) == AlreadyRegistered, "registration was not checked before alignment"); + check(sceAgcSetSemaphoreMemory(storage.get(), blockBytes) == AlreadyRegistered, "the non-AMM call did not share the registration"); + check(sceAgcSetAmmSemaphoreMemory(nullptr, 0) == AlreadyRegistered, "an empty registration was not rejected once memory is registered"); checkUnchanged(before, "a repeated registration changed memory"); void* label = nullptr; check(sceAgcGetSemaphoreLabel(labelCount - 1u, &label) == 0 && label == memory() + memoryBytes - labelBytes, "a repeated registration replaced the semaphore memory"); diff --git a/core/libs/prx/libSceAgcDriver/CMakeLists.txt b/core/libs/prx/libSceAgcDriver/CMakeLists.txt index 2c79ef109e..613780e647 100644 --- a/core/libs/prx/libSceAgcDriver/CMakeLists.txt +++ b/core/libs/prx/libSceAgcDriver/CMakeLists.txt @@ -142,28 +142,6 @@ add_custom_command( VERBATIM ) -set(agcDepthSampleShaderSource ${CMAKE_CURRENT_SOURCE_DIR}/Graphics/shaders/DepthSample.comp) -foreach(agcDepthSampleKind IN ITEMS Single Multisample) - string(TOUPPER ${agcDepthSampleKind} agcDepthSampleSymbol) - if(agcDepthSampleKind STREQUAL "Multisample") - set(agcDepthSampleMultisampled 1) - else() - set(agcDepthSampleMultisampled 0) - endif() - set(agcDepthSampleShaderSpv ${agcTextureDetileGeneratedDir}/DepthSample${agcDepthSampleKind}.spv) - set(agcDepthSampleShaderHeader ${agcTextureDetileGeneratedDir}/DepthSample${agcDepthSampleKind}_spv.h) - add_custom_command( - OUTPUT "${agcDepthSampleShaderHeader}" - COMMAND ${CMAKE_COMMAND} -E make_directory "${agcTextureDetileGeneratedDir}" - COMMAND "$" -V --target-env vulkan1.1 -DMULTISAMPLED=${agcDepthSampleMultisampled} -o "${agcDepthSampleShaderSpv}" "${agcDepthSampleShaderSource}" - COMMAND ${CMAKE_COMMAND} -DINPUT=${agcDepthSampleShaderSpv} -DOUTPUT=${agcDepthSampleShaderHeader} - -DSYMBOL=DEPTH_SAMPLE_${agcDepthSampleSymbol}_SPV -P "${CMAKE_CURRENT_SOURCE_DIR}/embed_spirv.cmake" - DEPENDS "${agcDepthSampleShaderSource}" glslang-standalone - VERBATIM - ) - list(APPEND agcDepthSampleShaderHeaders ${agcDepthSampleShaderHeader}) -endforeach() - set(agcSampleCounterShaderSource ${CMAKE_CURRENT_SOURCE_DIR}/Graphics/shaders/SampleCounter.comp) set(agcSampleCounterShaderSpv ${agcTextureDetileGeneratedDir}/SampleCounter.spv) set(agcSampleCounterShaderHeader ${agcTextureDetileGeneratedDir}/SampleCounter_spv.h) @@ -329,6 +307,8 @@ set(agcDriverSources ${CMAKE_CURRENT_SOURCE_DIR}/Execution/src/ShaderDeviceProfile.cpp ${CMAKE_CURRENT_SOURCE_DIR}/Execution/src/Pm4.cpp ${CMAKE_CURRENT_SOURCE_DIR}/Execution/src/SubmissionCapture.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/Execution/src/FrameCapture.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/Execution/src/FrameRecorder.cpp ${CMAKE_CURRENT_SOURCE_DIR}/Execution/src/VulkanDevice.cpp ${CMAKE_CURRENT_SOURCE_DIR}/Execution/src/FloatFlushProbe.cpp ${CMAKE_CURRENT_SOURCE_DIR}/Execution/src/AspectFit.cpp @@ -379,7 +359,6 @@ set(agcDriverSources ${agcReadbackHeaders} ${agcMultisampleUploadHeader} ${agcCmaskClearShaderHeader} - ${agcDepthSampleShaderHeaders} ${agcMeshArgumentsShaderHeader} ${agcTextureDetileImageShaderHeader} ${agcTextureRepackShaderHeader} @@ -441,6 +420,7 @@ target_link_libraries(libSceAgcDriver PRIVATE libc libkernel jpeg_turbo + libzstd_static ${CMAKE_DL_LIBS} ) if(APS5_ENABLE_TRACY) @@ -508,6 +488,18 @@ if(BUILD_TESTING) "PATH=path_list_prepend:$") endif() + add_test_executable(agc_frame_capture_tests tests/FrameCapture.cpp Execution/src/FrameCapture.cpp) + target_include_directories(agc_frame_capture_tests PRIVATE ${LIBS_INCLUDE_DIR}) + target_link_libraries(agc_frame_capture_tests PRIVATE libc libzstd_static) + set_target_properties(agc_frame_capture_tests PROPERTIES CXX_EXTENSIONS OFF) + configure_windows_unwind(agc_frame_capture_tests) + add_test(NAME agc_frame_capture COMMAND agc_frame_capture_tests) + set_tests_properties(agc_frame_capture PROPERTIES TIMEOUT 30) + if(WIN32) + set_tests_properties(agc_frame_capture PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + endif() + add_test_executable(agc_write_watch_coverage_tests tests/WriteWatchCoverage.cpp) target_include_directories(agc_write_watch_coverage_tests PRIVATE ${LIBS_INCLUDE_DIR}) target_link_libraries(agc_write_watch_coverage_tests PRIVATE libc) @@ -573,6 +565,42 @@ if(BUILD_TESTING) set_tests_properties(agc_handoff_spin PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$") endif() + + add_test_executable(agc_capture_trace_tests tests/CaptureTrace.cpp) + target_include_directories(agc_capture_trace_tests PRIVATE ${LIBS_INCLUDE_DIR}) + target_link_libraries(agc_capture_trace_tests PRIVATE libc) + configure_windows_unwind(agc_capture_trace_tests) + set(agcCaptureTraceDirectory ${CMAKE_CURRENT_BINARY_DIR}/capture_trace_tests) + file(MAKE_DIRECTORY "${agcCaptureTraceDirectory}/default") + file(MAKE_DIRECTORY "${agcCaptureTraceDirectory}/explicit") + file(MAKE_DIRECTORY "${agcCaptureTraceDirectory}/failure") + file(MAKE_DIRECTORY "${agcCaptureTraceDirectory}/best-effort") + file(MAKE_DIRECTORY "${agcCaptureTraceDirectory}/disabled") + set(agcCaptureTraceExplicitPath "${agcCaptureTraceDirectory}/explicit/trace.log") + set(agcCaptureTraceFailurePath "${agcCaptureTraceDirectory}/failure/missing-parent/trace.log") + set(agcCaptureTraceBestEffortPath "${agcCaptureTraceDirectory}/best-effort/trace.log") + add_test(NAME agc_capture_trace_path_default COMMAND agc_capture_trace_tests path-default) + add_test(NAME agc_capture_trace_path_empty COMMAND agc_capture_trace_tests path-empty) + set_tests_properties(agc_capture_trace_path_empty PROPERTIES ENVIRONMENT "APS5_CAPTURE_TRACE_PATH=") + add_test(NAME agc_capture_trace_path_explicit COMMAND agc_capture_trace_tests path-explicit) + set_tests_properties(agc_capture_trace_path_explicit PROPERTIES ENVIRONMENT "APS5_CAPTURE_TRACE_PATH=${agcCaptureTraceExplicitPath}") + add_test(NAME agc_capture_trace_explicit_writes COMMAND agc_capture_trace_tests explicit-writes) + set_tests_properties(agc_capture_trace_explicit_writes PROPERTIES ENVIRONMENT "APS5_CAPTURE_TRACE=1;APS5_CAPTURE_TRACE_PATH=${agcCaptureTraceExplicitPath}") + add_test(NAME agc_capture_trace_default_writes COMMAND agc_capture_trace_tests default-writes) + set_tests_properties(agc_capture_trace_default_writes PROPERTIES ENVIRONMENT "APS5_CAPTURE_TRACE=1" WORKING_DIRECTORY "${agcCaptureTraceDirectory}/default") + add_test(NAME agc_capture_trace_open_failure COMMAND agc_capture_trace_tests open-failure) + set_tests_properties(agc_capture_trace_open_failure PROPERTIES ENVIRONMENT "APS5_CAPTURE_TRACE=1;APS5_CAPTURE_TRACE_PATH=${agcCaptureTraceFailurePath}" WORKING_DIRECTORY "${agcCaptureTraceDirectory}/failure") + add_test(NAME agc_capture_trace_best_effort_overflow COMMAND agc_capture_trace_tests best-effort-overflow) + set_tests_properties(agc_capture_trace_best_effort_overflow PROPERTIES ENVIRONMENT "APS5_CAPTURE_TRACE=1;APS5_CAPTURE_TRACE_PATH=${agcCaptureTraceBestEffortPath}" WORKING_DIRECTORY "${agcCaptureTraceDirectory}/best-effort") + add_test(NAME agc_capture_trace_disabled COMMAND agc_capture_trace_tests disabled) + set_tests_properties(agc_capture_trace_disabled PROPERTIES WORKING_DIRECTORY "${agcCaptureTraceDirectory}/disabled") + if(WIN32) + set_tests_properties(agc_capture_trace_path_default agc_capture_trace_path_empty + agc_capture_trace_path_explicit agc_capture_trace_explicit_writes + agc_capture_trace_default_writes agc_capture_trace_open_failure agc_capture_trace_best_effort_overflow + agc_capture_trace_disabled PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + endif() endif() if(WIN32) @@ -771,7 +799,13 @@ if(BUILD_TESTING) set_tests_properties(agc_write_tracking_metadata_commit PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 30) add_test(NAME agc_resident_direct_remap COMMAND agc_write_tracking_tests --resident-direct-remap) set_tests_properties(agc_resident_direct_remap PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 20) - set_tests_properties(agc_write_tracking agc_write_tracking_metadata_commit agc_resident_direct_remap PROPERTIES ENVIRONMENT_MODIFICATION + add_test(NAME agc_rewatched_direct_shared COMMAND agc_write_tracking_tests --rewatched-direct-shared) + set_tests_properties(agc_rewatched_direct_shared PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 20) + add_test(NAME agc_import_alias_shared COMMAND agc_write_tracking_tests --import-alias-shared) + set_tests_properties(agc_import_alias_shared PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 20) + add_test(NAME agc_compared_shared_generations COMMAND agc_write_tracking_tests --compared-shared-generations) + set_tests_properties(agc_compared_shared_generations PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 20) + set_tests_properties(agc_write_tracking agc_write_tracking_metadata_commit agc_resident_direct_remap agc_rewatched_direct_shared agc_import_alias_shared agc_compared_shared_generations PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$") endif() endif() @@ -2141,6 +2175,12 @@ if(BUILD_TESTING) set_tests_properties(agc_driver_rcp_hardware_exact PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$") endif() + add_test(NAME agc_driver_depth_plane_sampling_evicting COMMAND agc_driver_depth_plane_sampling_tests --evicting) + set_tests_properties(agc_driver_depth_plane_sampling_evicting PROPERTIES ENVIRONMENT "APS5_DEPTH_PLANE_COPY_MIB=1" SKIP_RETURN_CODE 77 TIMEOUT 120) + if(WIN32) + set_tests_properties(agc_driver_depth_plane_sampling_evicting PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + endif() endif() foreach(agcExecutionTestName IN LISTS ANYPS5_AGC_EXECUTION_TESTS) if(NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/tests/execution/${agcExecutionTestName}.cpp) @@ -2166,6 +2206,9 @@ if(NOT ANYPS5_AGC_EXECUTION_TESTS OR "PooledBufferRange" IN_LIST ANYPS5_AGC_EXEC endif() target_link_libraries(agc_driver_gpu_map_on_fiber_tests PRIVATE libSceFiber) +if(ANYPS5_ENABLE_SPIRV_TOOLS) + target_link_libraries(agc_driver_optimize_on_fiber_tests PRIVATE libSceFiber shader_recompiler_agc_driver) +endif() target_link_libraries(agc_driver_rect_list_corner_tests PRIVATE shader_recompiler_agc_driver) target_link_libraries(agc_driver_queue_zero_late_interrupt_tests PRIVATE libkernel) @@ -2182,6 +2225,24 @@ target_include_directories(agc_driver_image_table_cache_bounds_tests PRIVATE $) +if(BUILD_TESTING) + add_test(NAME agc_submit_progress COMMAND agc_driver_submit_progress_tests) + set_tests_properties(agc_submit_progress PROPERTIES + ENVIRONMENT "APS5_SUBMIT_PROGRESS=1;ANYPS5_REQUIRE_VULKAN=1" TIMEOUT 30 SKIP_RETURN_CODE 77 WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}") + if(WIN32) + set_tests_properties(agc_submit_progress PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$;PATH=path_list_prepend:$") + endif() + add_test(NAME agc_submit_progress_no_timeline COMMAND agc_driver_submit_progress_tests --fallback-only) + set_tests_properties(agc_submit_progress_no_timeline PROPERTIES + ENVIRONMENT "APS5_SUBMIT_PROGRESS=1;APS5_NO_TIMELINE=1;ANYPS5_REQUIRE_VULKAN=1" + TIMEOUT 30 SKIP_RETURN_CODE 77 WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}") + if(WIN32) + set_tests_properties(agc_submit_progress_no_timeline PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$;PATH=path_list_prepend:$") + endif() +endif() + if(BUILD_TESTING) add_test(NAME agc_driver_store_pending_identity COMMAND agc_driver_store_over_untracked_tests --pending-identity) set_tests_properties(agc_driver_store_pending_identity PROPERTIES @@ -2240,6 +2301,7 @@ if(BUILD_TESTING) endif() endif() target_include_directories(agc_driver_native_multisample_depth_tests PRIVATE $) +target_include_directories(agc_driver_partially_resident_snapshot_tests PRIVATE $) foreach(agcReadbackTestKind IN ITEMS Vertex Uint Depth) string(TOUPPER ${agcReadbackTestKind} agcReadbackTestSymbol) @@ -2288,6 +2350,8 @@ target_include_directories(agc_driver_packed_unorm4444_multisample_partial_write target_include_directories(agc_driver_color1555_target_tests PRIVATE $) +target_include_directories(agc_driver_color_logic_op_tests PRIVATE $) + target_include_directories(agc_driver_color565_target_tests PRIVATE $) target_include_directories(agc_driver_depth_range_remap_draw_tests PRIVATE $) @@ -2478,10 +2542,21 @@ if(BUILD_TESTING) "PATH=path_list_prepend:$") endif() add_test(NAME agc_driver_sparse_indirect_indices_unreadable COMMAND agc_driver_sparse_indirect_indices_tests --unreadable) - set_tests_properties(agc_driver_sparse_indirect_indices agc_driver_sparse_indirect_indices_unreadable PROPERTIES + add_test(NAME agc_driver_sparse_indirect_indices_no_size COMMAND agc_driver_sparse_indirect_indices_tests --no-size) + add_test(NAME agc_driver_sparse_indirect_indices_no_size_unreadable COMMAND agc_driver_sparse_indirect_indices_tests --no-size-unreadable) + add_test(NAME agc_driver_sparse_indirect_indices_no_size_unreadable_record COMMAND agc_driver_sparse_indirect_indices_tests --no-size-unreadable-record) + set_tests_properties(agc_driver_sparse_indirect_indices agc_driver_sparse_indirect_indices_unreadable agc_driver_sparse_indirect_indices_no_size agc_driver_sparse_indirect_indices_no_size_unreadable agc_driver_sparse_indirect_indices_no_size_unreadable_record PROPERTIES ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1" SKIP_RETURN_CODE 77 TIMEOUT 30 WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}") if(WIN32) - set_tests_properties(agc_driver_sparse_indirect_indices_unreadable PROPERTIES ENVIRONMENT_MODIFICATION + set_tests_properties(agc_driver_sparse_indirect_indices_unreadable agc_driver_sparse_indirect_indices_no_size agc_driver_sparse_indirect_indices_no_size_unreadable agc_driver_sparse_indirect_indices_no_size_unreadable_record PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + endif() + add_test(NAME agc_driver_draw_cache_relocation_evicted COMMAND agc_driver_draw_cache_relocation_tests --evicted) + add_test(NAME agc_driver_draw_cache_relocation_kept COMMAND agc_driver_draw_cache_relocation_tests --kept) + set_tests_properties(agc_driver_draw_cache_relocation_evicted agc_driver_draw_cache_relocation_kept PROPERTIES + ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1;APS5_DRAW_CACHE_ENTRIES=2" SKIP_RETURN_CODE 77 WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}") + if(WIN32) + set_tests_properties(agc_driver_draw_cache_relocation_evicted agc_driver_draw_cache_relocation_kept PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$") endif() set_tests_properties(agc_driver_pixel_implicit_derivatives PROPERTIES TIMEOUT 30) @@ -2595,6 +2670,9 @@ if(BUILD_TESTING) add_test(NAME agc_driver_untracked_snapshot_hint COMMAND agc_driver_partially_resident_snapshot_tests --untracked-hint) set_tests_properties(agc_driver_untracked_snapshot_hint PROPERTIES ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1" SKIP_RETURN_CODE 77 WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" TIMEOUT 30 ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$") + add_test(NAME agc_driver_compared_shared_snapshot COMMAND agc_driver_partially_resident_snapshot_tests --compared-shared) + set_tests_properties(agc_driver_compared_shared_snapshot PROPERTIES ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1" SKIP_RETURN_CODE 77 WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" TIMEOUT 30 + ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$") endif() set_tests_properties(agc_driver_registration_trace PROPERTIES ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1;APS5_TRACE_FRAMES=0:1;APS5_TRACE_EVENTS=64" TIMEOUT 30) @@ -2748,6 +2826,28 @@ if(BUILD_TESTING) if(WIN32) set_tests_properties(agc_driver_f16_to_f32_unpack_spirv PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$") endif() + set(agcDrawShaderFailureTraceDirectory ${CMAKE_CURRENT_BINARY_DIR}/draw_shader_failure_tests) + file(MAKE_DIRECTORY "${agcDrawShaderFailureTraceDirectory}/failure") + file(MAKE_DIRECTORY "${agcDrawShaderFailureTraceDirectory}/success") + file(MAKE_DIRECTORY "${agcDrawShaderFailureTraceDirectory}/disabled") + file(MAKE_DIRECTORY "${agcDrawShaderFailureTraceDirectory}/failure_correlation_disabled") + set_tests_properties(agc_driver_draw_shader_failure PROPERTIES ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1;APS5_CAPTURE_TRACE=1;APS5_TRACE_SHADER_CORRELATION=1;APS5_CAPTURE_TRACE_PATH=${agcDrawShaderFailureTraceDirectory}/failure/trace.log" SKIP_RETURN_CODE 77 WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}") + add_test(NAME agc_driver_draw_shader_failure_successful COMMAND agc_driver_draw_shader_failure_tests successful-draw) + set_tests_properties(agc_driver_draw_shader_failure_successful PROPERTIES ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1;APS5_CAPTURE_TRACE=1;APS5_TRACE_SHADER_CORRELATION=1;APS5_CAPTURE_TRACE_PATH=${agcDrawShaderFailureTraceDirectory}/success/trace.log" SKIP_RETURN_CODE 77 WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}") + add_test(NAME agc_driver_draw_shader_failure_correlation_disabled COMMAND agc_driver_draw_shader_failure_tests disabled-correlation) + set_tests_properties(agc_driver_draw_shader_failure_correlation_disabled PROPERTIES ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1;APS5_CAPTURE_TRACE=1;APS5_CAPTURE_TRACE_PATH=${agcDrawShaderFailureTraceDirectory}/disabled/trace.log" SKIP_RETURN_CODE 77 WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}") + add_test(NAME agc_driver_draw_shader_failure_request_correlation_disabled COMMAND agc_driver_draw_shader_failure_tests request-failure-correlation-disabled) + set_tests_properties(agc_driver_draw_shader_failure_request_correlation_disabled PROPERTIES ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1;APS5_CAPTURE_TRACE=1;APS5_CAPTURE_TRACE_PATH=${agcDrawShaderFailureTraceDirectory}/failure_correlation_disabled/trace.log" SKIP_RETURN_CODE 77 WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}") + file(MAKE_DIRECTORY "${agcDrawShaderFailureTraceDirectory}/materialization") + file(MAKE_DIRECTORY "${agcDrawShaderFailureTraceDirectory}/materialization_correlation_disabled") + add_test(NAME agc_driver_draw_shader_failure_materialization COMMAND agc_driver_draw_shader_failure_tests materialization-failure) + set_tests_properties(agc_driver_draw_shader_failure_materialization PROPERTIES ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1;APS5_CAPTURE_TRACE=1;APS5_TRACE_SHADER_CORRELATION=1;APS5_CAPTURE_TRACE_PATH=${agcDrawShaderFailureTraceDirectory}/materialization/trace.log" SKIP_RETURN_CODE 77 WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}") + add_test(NAME agc_driver_draw_shader_failure_materialization_correlation_disabled COMMAND agc_driver_draw_shader_failure_tests materialization-failure-correlation-disabled) + set_tests_properties(agc_driver_draw_shader_failure_materialization_correlation_disabled PROPERTIES ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1;APS5_CAPTURE_TRACE=1;APS5_CAPTURE_TRACE_PATH=${agcDrawShaderFailureTraceDirectory}/materialization_correlation_disabled/trace.log" SKIP_RETURN_CODE 77 WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}") + if(WIN32) + set_tests_properties(agc_driver_draw_shader_failure agc_driver_draw_shader_failure_successful agc_driver_draw_shader_failure_correlation_disabled agc_driver_draw_shader_failure_request_correlation_disabled agc_driver_draw_shader_failure_materialization agc_driver_draw_shader_failure_materialization_correlation_disabled PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + endif() add_test(NAME agc_structurizer_intersection_off COMMAND agc_structurizer_tests) set_tests_properties(agc_structurizer_intersection_off PROPERTIES ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1;APS5_NO_PERF_DOM_INTERSECTION=1" WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}") add_test(NAME agc_structurizer_iteration_selection_off COMMAND agc_structurizer_tests) @@ -3004,6 +3104,13 @@ if(BUILD_TESTING) endif() endforeach() endforeach() + add_test(NAME agc_driver_depth_plane_partial_store_unimported COMMAND agc_driver_depth_plane_partial_store_tests) + set_tests_properties(agc_driver_depth_plane_partial_store_unimported PROPERTIES + ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1;APS5_NO_HOST_IMPORT=1" TIMEOUT 30 SKIP_RETURN_CODE 77 WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}") + if(WIN32) + set_tests_properties(agc_driver_depth_plane_partial_store_unimported PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + endif() add_test(NAME agc_driver_array_layer_target_cpu COMMAND agc_driver_array_layer_target_tests) set_tests_properties(agc_driver_array_layer_target_cpu PROPERTIES ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1;APS5_NO_HOST_IMPORT=1" TIMEOUT 30 SKIP_RETURN_CODE 77) @@ -3217,6 +3324,18 @@ if(BUILD_TESTING) endif() endif() +add_executable(agc_frame_replay EXCLUDE_FROM_ALL + tools/FrameReplay.cpp + Execution/src/FrameCapture.cpp +) +target_include_directories(agc_frame_replay PRIVATE ${LIBS_INCLUDE_DIR}) +target_link_libraries(agc_frame_replay PRIVATE libSceAgcDriver libkernel libc libzstd_static) +if(WIN32) + target_link_options(agc_frame_replay PRIVATE LINKER:--image-base=0x500000000000) +endif() +set_target_properties(agc_frame_replay PROPERTIES CXX_EXTENSIONS OFF) +configure_windows_unwind(agc_frame_replay) + add_executable(agc_pm4_replay EXCLUDE_FROM_ALL tools/Pm4Replay.cpp tools/Pm4Analysis.cpp @@ -3263,6 +3382,58 @@ if(BUILD_TESTING) set_tests_properties(agc_driver_pm4_replay agc_driver_pm4_replay_tool PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$") endif() + + if(WIN32) + add_test_executable(agc_driver_frame_capture_eop_tests tests/FrameCaptureEop.cpp Execution/src/FrameCapture.cpp) + target_include_directories(agc_driver_frame_capture_eop_tests PRIVATE ${LIBS_INCLUDE_DIR} ${CMAKE_SOURCE_DIR}/core/shader/recompiler ${ANYPS5_VULKAN_HEADERS_INCLUDE_DIR} ${ANYPS5_SPIRV_HEADERS_INCLUDE_DIR}) + target_link_libraries(agc_driver_frame_capture_eop_tests PRIVATE libSceAgcDriver libkernel libc libzstd_static) + set_target_properties(agc_driver_frame_capture_eop_tests PROPERTIES CXX_EXTENSIONS OFF) + configure_windows_unwind(agc_driver_frame_capture_eop_tests) + add_test(NAME agc_driver_frame_capture_eop COMMAND agc_driver_frame_capture_eop_tests) + set_tests_properties(agc_driver_frame_capture_eop PROPERTIES + ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1;APS5_FRAME_CAPTURE=${CMAKE_CURRENT_BINARY_DIR}/frame-capture-eop;APS5_FRAME_CAPTURE_AT_FLIP=1;APS5_FRAME_CAPTURE_FRAMES=3" + TIMEOUT 120) + set_tests_properties(agc_driver_frame_capture_eop PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + endif() + + add_test_executable(agc_driver_frame_replay_state_tests tests/FrameReplayState.cpp Execution/src/FrameCapture.cpp) + target_include_directories(agc_driver_frame_replay_state_tests PRIVATE ${LIBS_INCLUDE_DIR}) + target_link_libraries(agc_driver_frame_replay_state_tests PRIVATE libSceAgcDriver libkernel libc libzstd_static) + set_target_properties(agc_driver_frame_replay_state_tests PROPERTIES CXX_EXTENSIONS OFF) + configure_windows_unwind(agc_driver_frame_replay_state_tests) + set(agcFrameReplayModes order tokens blocking) + if(WIN32) + list(APPEND agcFrameReplayModes zero shader shader_host) + endif() + foreach(mode IN LISTS agcFrameReplayModes) + set(capture "${CMAKE_CURRENT_BINARY_DIR}/frame-replay-${mode}") + add_test(NAME agc_driver_frame_replay_${mode} COMMAND agc_driver_frame_replay_state_tests ${mode} "${capture}") + set_tests_properties(agc_driver_frame_replay_${mode} PROPERTIES TIMEOUT 30) + if(mode MATCHES "^shader") + set_tests_properties(agc_driver_frame_replay_${mode} PROPERTIES ENVIRONMENT + "APS5_FRAME_CAPTURE=${capture};APS5_FRAME_CAPTURE_AT_FLIP=1;APS5_FRAME_CAPTURE_FRAMES=1") + endif() + if(WIN32) + set_tests_properties(agc_driver_frame_replay_${mode} PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + endif() + endforeach() + + if(WIN32) + add_test_executable(agc_driver_frame_replay_tests tests/FrameReplay.cpp Execution/src/FrameCapture.cpp) + target_include_directories(agc_driver_frame_replay_tests PRIVATE ${LIBS_INCLUDE_DIR}) + target_link_libraries(agc_driver_frame_replay_tests PRIVATE libSceAgc libSceAgcDriver libkernel libc libzstd_static) + set_target_properties(agc_driver_frame_replay_tests PROPERTIES CXX_EXTENSIONS OFF) + configure_windows_unwind(agc_driver_frame_replay_tests) + add_dependencies(agc_driver_frame_replay_tests agc_frame_replay) + add_test(NAME agc_driver_frame_replay COMMAND agc_driver_frame_replay_tests $) + set_tests_properties(agc_driver_frame_replay PROPERTIES + ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1;APS5_FRAME_CAPTURE=${CMAKE_CURRENT_BINARY_DIR}/frame-replay-capture;APS5_FRAME_CAPTURE_AT_FLIP=1;APS5_FRAME_CAPTURE_FRAMES=2" + TIMEOUT 120) + set_tests_properties(agc_driver_frame_replay PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + endif() endif() add_executable(agc_compute_pipeline_replay EXCLUDE_FROM_ALL tools/ComputePipelineReplay.cpp) @@ -3513,6 +3684,14 @@ if(BUILD_TESTING) set_tests_properties(scalar_call PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$") endif() + add_test_executable(ir_dump_memory_tests ${LIBS_INCLUDE_DIR}/tests/IrDumpMemory.cpp) + target_link_libraries(ir_dump_memory_tests PRIVATE shader_recompiler_agc_driver libc) + configure_windows_unwind(ir_dump_memory_tests) + add_test(NAME ir_dump_memory COMMAND ir_dump_memory_tests) + if(WIN32) + set_tests_properties(ir_dump_memory PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + endif() add_test_executable(image_sample_gather_tests ${LIBS_INCLUDE_DIR}/tests/ImageSampleGather.cpp) target_link_libraries(image_sample_gather_tests PRIVATE shader_recompiler_agc_driver libc) configure_windows_unwind(image_sample_gather_tests) diff --git a/core/libs/prx/libSceAgcDriver/Execution/include/CaptureTrace.hpp b/core/libs/prx/libSceAgcDriver/Execution/include/CaptureTrace.hpp index 56dd9215f3..501c86b224 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/include/CaptureTrace.hpp +++ b/core/libs/prx/libSceAgcDriver/Execution/include/CaptureTrace.hpp @@ -13,6 +13,12 @@ namespace AgcDriver { +inline const char* CaptureTracePath() { + const char* configuredPath = std::getenv("APS5_CAPTURE_TRACE_PATH"); + if (configuredPath != nullptr && configuredPath[0] != '\0') return configuredPath; + return "capture-trace.log"; +} + class CaptureTrace { public: static bool Enabled() { @@ -30,6 +36,14 @@ public: trace.append(std::string(line.data(), static_cast(size))); } + template + static void LogBestEffort(const char* format, TArgs... args) noexcept { + try { + Log(format, args...); + } catch (...) { + } + } + private: static CaptureTrace& instance() { static CaptureTrace trace; @@ -37,8 +51,13 @@ private: } CaptureTrace() { + const char* path = CaptureTracePath(); + output.open(path, std::ios::binary); + if (!output.is_open()) { + std::fprintf(stderr, "Capture trace open failed for %s\n", path); + return; + } output.exceptions(std::ios::badbit | std::ios::failbit); - output.open("capture-trace.log", std::ios::binary); worker = std::thread([this] { run(); }); } @@ -48,10 +67,11 @@ private: stopping = true; } changed.notify_one(); - worker.join(); + if (worker.joinable()) worker.join(); } void append(std::string line) { + if (!output.is_open()) return; const auto elapsed = std::chrono::duration_cast(std::chrono::steady_clock::now() - start).count(); std::lock_guard lock(mutex); if (pending.size() + line.size() + 64 > 16 * 1024 * 1024) throw std::runtime_error("Capture trace queue exceeded 16 MiB"); diff --git a/core/libs/prx/libSceAgcDriver/Execution/include/DeviceFault.hpp b/core/libs/prx/libSceAgcDriver/Execution/include/DeviceFault.hpp index c82357c4ec..4646531f5a 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/include/DeviceFault.hpp +++ b/core/libs/prx/libSceAgcDriver/Execution/include/DeviceFault.hpp @@ -16,6 +16,17 @@ namespace AgcDriver { +struct DeviceDiagnostics { + bool config = false; + bool checkpoints = false; + VkDeviceDiagnosticsConfigFlagsNV flags = 0; +}; + +inline DeviceDiagnostics ChooseDeviceDiagnostics(bool requested, bool configSupported, bool checkpointsSupported) { + if (!requested || !configSupported) return {}; + return {true, checkpointsSupported, VK_DEVICE_DIAGNOSTICS_CONFIG_ENABLE_SHADER_DEBUG_INFO_BIT_NV | VK_DEVICE_DIAGNOSTICS_CONFIG_ENABLE_RESOURCE_TRACKING_BIT_NV | VK_DEVICE_DIAGNOSTICS_CONFIG_ENABLE_AUTOMATIC_CHECKPOINTS_BIT_NV | VK_DEVICE_DIAGNOSTICS_CONFIG_ENABLE_SHADER_ERROR_REPORTING_BIT_NV}; +} + inline const char* DeviceFaultAddressKind(VkDeviceFaultAddressTypeEXT type) { switch (type) { case VK_DEVICE_FAULT_ADDRESS_TYPE_NONE_EXT: return "none"; diff --git a/core/libs/prx/libSceAgcDriver/Execution/include/Driver.hpp b/core/libs/prx/libSceAgcDriver/Execution/include/Driver.hpp index 384c601ccc..e02db9f381 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/include/Driver.hpp +++ b/core/libs/prx/libSceAgcDriver/Execution/include/Driver.hpp @@ -1,6 +1,7 @@ #ifndef CORE_LIBS_PRX_LIBSCEAGCDRIVER_EXECUTION_INCLUDE_DRIVER_HPP #define CORE_LIBS_PRX_LIBSCEAGCDRIVER_EXECUTION_INCLUDE_DRIVER_HPP +#include #include #include #include @@ -11,6 +12,14 @@ namespace AgcDriver { void Submit(const Packet* packet, std::uint32_t queue); +struct AwaitedWait { + std::uint64_t address = 0; + std::uint64_t reference = 0; + std::uint64_t mask = 0; + std::uint32_t function = 0; + std::uint32_t bytes = 0; +}; + } extern "C" void AgcDriverPrepareCommandWrite_nid_no_patch(std::uint64_t address, std::size_t bytes); @@ -21,6 +30,8 @@ extern "C" void AgcDriverUnlockVulkanLoader_nid_postfix(); extern "C" void AgcDriverShutdown_nid_postfix(); extern "C" void AgcDriverRequestScreenshot_nid_postfix(); extern "C" void AgcDriverSuspendPoint_nid_postfix(); +extern "C" void AgcDriverSerialProgress_nid_postfix(std::uint64_t* accepted, std::uint64_t* completed); +extern "C" std::size_t AgcDriverAwaitedWaits_nid_postfix(AgcDriver::AwaitedWait* waits, std::size_t capacity); extern "C" std::uint64_t AgcDriverDispatchesAdoptedAhead_nid_postfix(); extern "C" void AgcDriverRegisterShader_nid_postfix(const Shader* shader); extern "C" void AgcDriverResolveShaderAbi_nid_postfix(const Shader* shader, std::span context, std::span primitive); diff --git a/core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp b/core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp index 922c68099c..18303f9f7b 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp +++ b/core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp @@ -2,6 +2,7 @@ #define CORE_LIBS_PRX_LIBSCEAGCDRIVER_EXECUTION_INCLUDE_DRIVER_DRIVER_HPP #include "prx/libSceAgcDriver/Execution/include/Driver/Queues/Submission.hpp" +#include "prx/libSceAgcDriver/Execution/include/Driver/Queues/SubmissionRing.hpp" #include "prx/libSceAgcDriver/Execution/include/Driver/DeviceAccess.hpp" #include "prx/libSceAgcDriver/Execution/include/Driver/Packets/PacketHistory.hpp" #include "prx/libSceAgcDriver/Execution/include/Driver/Dispatch/DispatchCache.hpp" @@ -64,6 +65,12 @@ public: void SubmitFlip(const FlipInfo& info); void SuspendPoint(); std::uint32_t ReserveEopFlip(const FlipInfo& info); + std::uint32_t ReserveEopFlipAt(const FlipInfo& info, std::uint32_t token); + std::vector PendingEopFlipTokens(); + std::pair SerialProgress(); + std::uint64_t FlipSubmissions() const; + std::vector AwaitedWaits(); + bool WaitIdleFor(std::chrono::milliseconds timeout); void RegisterVideoOutput(std::uint32_t handle, const std::shared_ptr& output); void UnregisterVideoOutput(std::uint32_t handle, const std::shared_ptr& output); void AttachWindow(const PresentationWindow& window); @@ -109,6 +116,7 @@ private: Driver(); void rethrowFailure() const; void checkStopping() const; + std::uint32_t reserveEopFlip(const FlipInfo& info, std::uint32_t token); static bool& onWorkerThread(); static void copyCommands(Submission& submission, const std::uint32_t* guest, std::size_t words); static bool copySegment(Submission& submission, const std::uint32_t* guest, std::size_t words, std::size_t& budget); @@ -126,9 +134,9 @@ private: static void forgetUnfinishedWrites(QueueWorker& worker, std::span labelWrites); static bool& completionDeferred(); void deferCompletion(const Submission& submission); - void submitOpenWork(); + void submitOpenWork(const std::shared_ptr& progress = {}); void wakeQueueZeroForCompletions(); - void completeSubmission(std::uint64_t serial, std::uint64_t received, std::span labelWrites); + void completeSubmission(std::uint64_t serial, std::uint64_t received, std::span labelWrites, const std::shared_ptr& progress); static bool waitFree(const Submission& submission); bool queue0Before(std::uint64_t received) const; bool orderReleased(std::uint32_t queue, std::uint64_t received) const; @@ -183,13 +191,13 @@ private: std::shared_ptr decodeDraw(const QueueState& queue, const Submission& submission, const Graphics::Context& context); void resolveDrawDecode(const QueueState& queue, const Submission& submission, const Graphics::Context& context, std::shared_ptr& decode, bool registerKey, std::uint64_t drawKey, bool profile); void lookupDraw(const Submission& submission, const std::shared_ptr& localDevice, const Graphics::State& graphics, const ShaderRecompiler::ShaderPixelStageInfo& pixel, const std::vector& programs, const std::vector& roles, const std::vector>& vertexInfos, bool useDrawEntries, bool registerKey, bool profile, std::uint64_t& drawKey, std::shared_ptr& entry, std::vector>& matched, std::vector>& matchedRegions, bool& drawHit, bool& verifyHit, DrawPhaseTiming& phaseTiming, std::array& phaseMs, const std::shared_ptr& dataEntry, DrawDataCandidates& dataCandidates, std::vector& stageData); - std::shared_ptr materializeDrawStage(std::size_t i, std::uint32_t pushOffset, const QueueState& queue, const Submission& submission, const std::vector& programs, const Graphics::State& graphics, const ShaderRecompiler::ShaderPixelStageInfo& pixel, const std::vector>& vertexInfos, std::vector& memory, const std::vector& linked, const Pm4::DrawParameters& drawParameters, const std::shared_ptr& localDevice, ShaderMemory& shaderMemory, std::vector& stageCaptures, std::vector& recompiled, bool drawHit, const std::vector>& matched, const std::vector>& matchedRegions, bool profile, std::uint64_t dumpTarget, std::uint64_t dumpSlot1, std::uint64_t& captures, DrawPhaseTiming& phaseTiming, std::array& phaseMs, std::string& rejected, const std::optional& meshBarycentrics); + std::shared_ptr materializeDrawStage(std::size_t i, std::uint32_t pushOffset, const QueueState& queue, const Submission& submission, std::size_t packetOffset, std::uint64_t traceToken, const std::vector& programs, const Graphics::State& graphics, const ShaderRecompiler::ShaderPixelStageInfo& pixel, const std::vector>& vertexInfos, std::vector& memory, const std::vector& linked, const Pm4::DrawParameters& drawParameters, const std::shared_ptr& localDevice, ShaderMemory& shaderMemory, std::vector& stageCaptures, std::vector& recompiled, bool drawHit, const std::vector>& matched, const std::vector>& matchedRegions, bool profile, std::uint64_t dumpTarget, std::uint64_t dumpSlot1, std::uint64_t& captures, DrawPhaseTiming& phaseTiming, std::array& phaseMs, std::string& rejected, const std::optional& meshBarycentrics); static void recordStageDataPositions(DispatchVariant& variant, const ShaderRecompiler::ResourceCapture& capture, const std::vector& decodeReads); void cacheDrawStages(bool useDrawEntries, bool drawHit, const Pm4::DrawParameters& drawParameters, const std::optional& indirectCpu, const std::vector& programs, const std::vector& stageCaptures, const std::vector>& vertexInfos, const std::vector>& decodeReads, bool verifyHit, const std::vector>& matched, const std::vector& stageData, std::vector>& fresh, std::uint64_t drawKey, bool registerKey, const std::shared_ptr& decode, DrawPhaseTiming& phaseTiming, const std::vector& reused, const std::shared_ptr& entry, std::uint64_t shapeKey, bool shapeNeedsEntry = false); static bool drawPrecheck(); - std::optional precheckDraw(const QueueState& queue, const Submission& submission, std::span packet, const Pm4::DrawParameters& drawParameters, std::string& rejected, bool& traceIndirect); + std::optional precheckDraw(const QueueState& queue, const Submission& submission, std::size_t packetOffset, std::span packet, const Pm4::DrawParameters& drawParameters, std::string& rejected, bool& traceIndirect); static std::uint32_t drawUserWord(const DrawProgram& program, std::int32_t sgpr); - DrawVerdict draw(QueueState& queue, std::span packet, const Submission& submission, std::string& rejected); + DrawVerdict draw(QueueState& queue, std::span packet, const Submission& submission, std::size_t packetOffset, std::string& rejected); DrawVerdict finishDraw(DrawTail& tail, PerformanceTimer& timing); void addDriverPhases(DispatchClass which, const std::array& ms, bool hit, bool validated); static PendingDispatchPhases& pendingDispatchPhases(); @@ -262,7 +270,7 @@ private: void noteForeignWriter(std::uint64_t begin, std::uint64_t end, std::uint32_t queue); void noteDrawWriters(std::span stages, std::uint32_t queue); static std::vector> drawWriteRanges(const Graphics::State& graphics, std::span stages); - void commitDraw(std::shared_ptr localDevice, std::uint32_t queue, const Graphics::State& graphics, const Pm4::DrawParameters& drawParameters, std::span stages, std::span snapshots, const std::shared_ptr& recipe, const std::vector>& recipeStages, std::uint64_t drawKey); + void commitDraw(std::shared_ptr localDevice, std::uint32_t queue, const Graphics::State& graphics, const Pm4::DrawParameters& drawParameters, std::span stages, std::span snapshots, const std::shared_ptr& recipe, const std::vector>& recipeStages, std::uint64_t drawKey, VulkanDevice::DrawTraceContext trace = {}); bool labelPacketQueues(std::span packet, std::uint32_t opcode); bool enqueueLabelPacket(std::span packet, std::uint32_t opcode, std::uint32_t queue); bool enqueueDmaPacket(std::span packet, std::uint32_t opcode, std::uint32_t queue, const QueueState& state); @@ -367,6 +375,14 @@ private: std::shared_ptr pendingFrameTiming; std::atomic flipsCounted{0}; + struct AwaitedSlot { + std::atomic address{0}; + std::atomic reference{0}; + std::atomic mask{0}; + std::atomic shape{0}; + }; + std::array awaitedSlots{}; + void noteWaitFields(std::uint32_t queue, std::span packet); std::atomic flipSerial{0}; std::atomic flipBatchesUnsignaled{0}; @@ -400,7 +416,7 @@ private: std::unordered_set traceShapes, traceKeysSeen, traceKeysEvicted; std::list drawOrder; std::unordered_map> drawRelocations; - std::unordered_map> drawAliases; + std::unordered_map drawAliases; DrawRelocationPending drawRelocationPending; std::unordered_map drawRelocationTrials; DrawRelocationCounters drawRelocationCounters; diff --git a/core/libs/prx/libSceAgcDriver/Execution/include/Driver/Queues/Submission.hpp b/core/libs/prx/libSceAgcDriver/Execution/include/Driver/Queues/Submission.hpp index e71457e0f3..e45f794362 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/include/Driver/Queues/Submission.hpp +++ b/core/libs/prx/libSceAgcDriver/Execution/include/Driver/Queues/Submission.hpp @@ -3,6 +3,7 @@ #include "prx/libSceAgcDriver/Execution/include/Driver/Shaders/ShaderRegistry.hpp" #include "prx/libSceAgcDriver/Execution/include/VideoOutput.hpp" +#include "prx/libSceAgcDriver/Execution/include/Driver/Queues/SubmissionProgress.hpp" #include #include #include @@ -22,6 +23,7 @@ struct Submission { std::uint64_t serial; std::uint32_t queue; std::vector commands; + std::shared_ptr progress; std::shared_ptr shaders; std::map> flips; diff --git a/core/libs/prx/libSceAgcDriver/Execution/include/Driver/Queues/SubmissionProgress.hpp b/core/libs/prx/libSceAgcDriver/Execution/include/Driver/Queues/SubmissionProgress.hpp new file mode 100644 index 0000000000..5194af7ce4 --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/Execution/include/Driver/Queues/SubmissionProgress.hpp @@ -0,0 +1,44 @@ +#ifndef CORE_LIBS_PRX_LIBSCEAGCDRIVER_EXECUTION_INCLUDE_DRIVER_QUEUES_SUBMISSIONPROGRESS_HPP +#define CORE_LIBS_PRX_LIBSCEAGCDRIVER_EXECUTION_INCLUDE_DRIVER_QUEUES_SUBMISSIONPROGRESS_HPP + +#include +#include +#include +#include +#include +#include + +namespace AgcDriver { class VulkanDevice; } + +namespace AgcDriver::DriverDetail { + +class SubmissionProgress { +public: + using Clock = std::chrono::steady_clock; + using Ready = std::pair, std::uint64_t>; + + void Publish(std::shared_ptr device, std::uint64_t epoch) { + { + std::lock_guard lock(mutex); + result = {std::move(device), epoch}; + ready = true; + } + changed.notify_one(); + } + + Ready WaitUntil(Clock::time_point deadline) { + std::unique_lock lock(mutex); + if (!changed.wait_until(lock, deadline, [&] { return ready; })) return {}; + return result; + } + +private: + std::mutex mutex; + std::condition_variable changed; + Ready result; + bool ready = false; +}; + +} + +#endif diff --git a/core/libs/prx/libSceAgcDriver/Execution/include/Driver/Shaders/ShaderRegistry.hpp b/core/libs/prx/libSceAgcDriver/Execution/include/Driver/Shaders/ShaderRegistry.hpp index ea7403c228..b1907257c4 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/include/Driver/Shaders/ShaderRegistry.hpp +++ b/core/libs/prx/libSceAgcDriver/Execution/include/Driver/Shaders/ShaderRegistry.hpp @@ -175,6 +175,8 @@ struct RegisteredShaderState { Registers shader; Registers context; Registers userConfig; + + bool operator==(const RegisteredShaderState&) const = default; }; struct ShaderSnapshot { @@ -210,6 +212,9 @@ std::shared_ptr PrepareShaderWithDiagnosti std::uint64_t NullPixelProgramAddress(); ShaderSnapshot PrepareNullPixelProgram(const VulkanDevice& device); +std::uint64_t DepthCopyPixelProgramAddress(bool multisampled); +std::uint32_t DepthCopyPixelUserWords(bool multisampled); +ShaderSnapshot PrepareDepthCopyPixelProgram(bool multisampled); std::optional RegisteredFloatMode(const ShaderSnapshot& snapshot); std::optional DispatchFloatMode(const ShaderSnapshot& snapshot, const Registers& live); std::shared_ptr RegisteredHeader(const ShaderRegistry& registry, const Shader* shader); diff --git a/core/libs/prx/libSceAgcDriver/Execution/include/FrameCapture.hpp b/core/libs/prx/libSceAgcDriver/Execution/include/FrameCapture.hpp new file mode 100644 index 0000000000..b3721a17d5 --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/Execution/include/FrameCapture.hpp @@ -0,0 +1,138 @@ +#ifndef CORE_LIBS_PRX_LIBSCEAGCDRIVER_EXECUTION_INCLUDE_FRAMECAPTURE_HPP +#define CORE_LIBS_PRX_LIBSCEAGCDRIVER_EXECUTION_INCLUDE_FRAMECAPTURE_HPP + +#include "SceShaders.hpp" +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace AgcDriver { + +enum class FrameCallKind : std::uint32_t { + RegisterShader = 1, + ResolveShaderAbi, + ResolveGraphicsAbi, + ResolveGraphicsStagesAbi, + RegisterVideoOutput, + SetTessellationFactorRing, + Submit, + Flip, + ReserveEopFlip, + Suspend, + Memory, + FrameStart, + End, + DisplayBuffer, + UnregisterVideoOutput, +}; + +struct FrameCall { + FrameCallKind kind = FrameCallKind::End; + std::uint64_t ordinal = 0; + std::uint64_t timeNs = 0; + std::uint64_t thread = 0; + std::uint64_t accepted = 0; + std::uint64_t completed = 0; + std::uint32_t queue = 0; + std::uint32_t flags = 0; + bool cpuFlip = false; + std::uint64_t address = 0; + std::uint64_t size = 0; + std::vector values; + std::vector context; + std::vector primitive; +}; + +enum class FrameMemoryKind : std::uint32_t { Range = 1, Snapshot, Delta, Reference }; + +enum class FrameRangeKind : std::uint32_t { Watched = 1, Unwatched, Section, Alias, File, Host }; + +struct FrameRangeAccess { + static constexpr std::uint32_t Readable = 1; + static constexpr std::uint32_t Writable = 2; + static constexpr std::uint32_t Gpu = 4; +}; + +struct FrameMemoryChunk { + FrameMemoryKind kind = FrameMemoryKind::Range; + FrameRangeKind rangeKind = FrameRangeKind::Watched; + std::uint32_t protection = 0; + std::uint32_t access = 0; + std::uint64_t group = 0; + std::uint64_t completed = 0; + std::uint64_t address = 0; + std::uint64_t bytes = 0; + std::uint64_t compressedBytes = 0; + std::uint64_t offset = 0; +}; + +class FrameCaptureWriter { +public: + static constexpr std::string_view CallsName = "calls.bin"; + static constexpr std::string_view MemoryName = "memory.bin"; + static constexpr std::string_view HeaderName = "capture.json"; + static constexpr std::string_view CallsMagic = "aps5-frame-calls 1\n"; + static constexpr std::string_view MemoryMagic = "aps5-frame-memory 1\n"; + static constexpr std::size_t BlockBytes = 64u * 1024u; + static constexpr std::size_t ChunkBytes = 4u * 1024u * 1024u; + + explicit FrameCaptureWriter(const std::filesystem::path& directory, int level = 1); + FrameCaptureWriter(const FrameCaptureWriter&) = delete; + FrameCaptureWriter& operator=(const FrameCaptureWriter&) = delete; + + void WriteCall(const FrameCall& call); + void WriteRange(std::uint64_t address, std::uint64_t bytes, FrameRangeKind kind, std::uint32_t protection, std::uint32_t access); + std::uint64_t WriteSnapshot(std::uint64_t address, std::span bytes); + void WriteDelta(std::uint64_t group, std::uint64_t completed, std::uint64_t address, std::span bytes); + void WriteReference(std::uint64_t index, std::uint64_t address, std::span pixels); + void Finish(std::string_view header); + + std::uint64_t RawBytes() const; + std::uint64_t StoredBytes() const; + const std::filesystem::path& Directory() const; + +private: + void writeChunk(const FrameMemoryChunk& chunk, std::span payload); + std::vector compress(std::span bytes) const; + + std::filesystem::path directory; + int level; + mutable std::mutex mutex; + std::ofstream calls; + std::ofstream memory; + std::uint64_t rawBytes = 0; + std::uint64_t storedBytes = 0; + bool finished = false; +}; + +class FrameCaptureReader { +public: + explicit FrameCaptureReader(const std::filesystem::path& directory); + + const std::vector& Calls() const; + const std::vector& Chunks() const; + const std::string& Header() const; + bool Truncated() const; + void Read(const FrameMemoryChunk& chunk, std::span destination) const; + std::vector Read(const FrameMemoryChunk& chunk) const; + +private: + std::filesystem::path directory; + std::vector calls; + std::vector chunks; + std::string header; + bool truncated = false; +}; + +std::string_view FrameCallName(FrameCallKind kind); +bool AllZero(std::span bytes); + +} + +#endif diff --git a/core/libs/prx/libSceAgcDriver/Execution/include/FrameRecorder.hpp b/core/libs/prx/libSceAgcDriver/Execution/include/FrameRecorder.hpp new file mode 100644 index 0000000000..bad806af09 --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/Execution/include/FrameRecorder.hpp @@ -0,0 +1,113 @@ +#ifndef CORE_LIBS_PRX_LIBSCEAGCDRIVER_EXECUTION_INCLUDE_FRAMERECORDER_HPP +#define CORE_LIBS_PRX_LIBSCEAGCDRIVER_EXECUTION_INCLUDE_FRAMERECORDER_HPP + +#include "prx/libSceAgcDriver/Execution/include/DisplayBuffer.hpp" +#include "prx/libSceAgcDriver/Execution/include/FrameCapture.hpp" +#include "prx/libSceAgcDriver/Execution/include/VideoOutput.hpp" +#include "SceShaders.hpp" +#include "SceTypes.hpp" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace AgcDriver { + +class FrameRecorder { +public: + static constexpr std::uint64_t DefaultFrames = 10; + static constexpr std::size_t ReservationHistory = 256; + static constexpr std::size_t SnapshotPieceBytes = 64u * 1024u * 1024u; + static constexpr std::uint64_t MinimumFreeBytes = std::uint64_t{4} << 30; + static constexpr std::size_t HostPageBytes = 4096; + + static FrameRecorder* Active(); + + void RegisterShader(const Shader* shader); + void ResolveShaderAbi(const Shader* shader, std::span context, std::span primitive); + void ResolveGraphicsStagesAbi(std::span stages, std::span context, std::span primitive); + void ResolveGraphicsAbi(const Shader* vertex, const Shader* pixel, std::uint32_t primitiveType); + void RegisterVideoOutput(std::uint32_t handle); + void UnregisterVideoOutput(std::uint32_t handle); + void Submit(const Packet& packet, std::uint32_t queue); + void Flip(const FlipInfo& info); + void ReserveEopFlip(const FlipInfo& info, std::uint32_t token); + void Suspend(); + void PresentBuffer(const DisplayBuffer& buffer); + void FrameBoundary(); + +private: + enum class State { Waiting, Recording, Done }; + + struct Range { + std::uint64_t address = 0; + std::uint64_t bytes = 0; + FrameRangeKind kind = FrameRangeKind::Watched; + std::uint32_t protection = 0; + std::uint32_t access = 0; + bool readable = false; + }; + + FrameRecorder(std::filesystem::path directory, std::filesystem::path trigger, std::uint64_t atBoundary, std::uint64_t frames); + struct Span { + std::uint64_t address = 0; + std::uint64_t bytes = 0; + std::uint64_t since = 0; + }; + + FrameCall make(FrameCallKind kind); + void record(FrameCall call, bool prologueCall, std::span shaders = {}); + void emitDelta(); + void emitShaderDelta(std::span shaders); + void writeDelta(std::span spans, std::span hostPages, std::span added = {}); + bool triggered(); + void start(); + void finish(); + void snapshot(const Range& range); + void writeReferenceFrames(); + bool abandonIfDiskLow(); + std::string header() const; + std::vector committedRanges(); + std::vector newRanges(); + + std::filesystem::path directory; + std::filesystem::path trigger; + std::uint64_t atBoundary = 0; + std::uint64_t frames = DefaultFrames; + std::mutex mutex; + State state = State::Waiting; + std::unique_ptr writer; + std::vector prologue; + std::deque reservations; + std::vector ranges; + std::map shaderMarks; + std::map hostPageHashes; + std::mutex displayMutex; + std::map displayBuffers; + std::uint64_t ordinal = 0; + std::uint64_t boundaries = 0; + std::uint64_t startBoundary = 0; + std::uint64_t recordedFrames = 0; + std::uint64_t generation = 0; + std::uint64_t deltaGroups = 0; + std::uint64_t deltaBytes = 0; + std::uint64_t snapshotBytes = 0; + std::uint64_t recordedCalls = 0; + std::uint64_t startAttempts = 0; + std::uint64_t unreadableBytes = 0; + std::uint64_t allocationsGeneration = 0; + std::uint64_t lateRanges = 0; + std::vector warnings; + std::chrono::steady_clock::time_point origin; + std::chrono::steady_clock::time_point startedAt; +}; + +} + +#endif diff --git a/core/libs/prx/libSceAgcDriver/Execution/include/GuestMemory.hpp b/core/libs/prx/libSceAgcDriver/Execution/include/GuestMemory.hpp index 0e9c7b751a..c360a6a579 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/include/GuestMemory.hpp +++ b/core/libs/prx/libSceAgcDriver/Execution/include/GuestMemory.hpp @@ -81,6 +81,8 @@ struct UnchangedQuery { bool WrittenSince(std::uint64_t address, std::size_t bytes, std::uint64_t generation); bool UnchangedSinceAll(std::span queries); std::uint64_t CollectCommittedWrites(std::uint64_t address, std::size_t bytes, std::vector>& ranges); +std::uint64_t WriteGeneration(); +std::uint64_t CollectWrittenBlocks(std::uint64_t address, std::size_t bytes, std::uint64_t since, std::vector>& written); bool UnchangedCommittedSince(std::span> ranges, std::uint64_t generation); // Returns the generation the blocks were stamped with (0 when the arena is not write-watched): // UnchangedSince(range, it) holds until the next store over the range. diff --git a/core/libs/prx/libSceAgcDriver/Execution/include/Pm4.hpp b/core/libs/prx/libSceAgcDriver/Execution/include/Pm4.hpp index e7d86a28bb..0b9bc1f9f8 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/include/Pm4.hpp +++ b/core/libs/prx/libSceAgcDriver/Execution/include/Pm4.hpp @@ -46,6 +46,7 @@ struct DrawParameters { std::uint32_t drawIndexLocation; bool drawIndexEnabled; std::uint32_t indxOffset; + bool indexLimitKnown = true; enum class Rule : std::uint8_t { InPlace, Constant }; Rule vertexRule = Rule::Constant; Rule instanceRule = Rule::Constant; diff --git a/core/libs/prx/libSceAgcDriver/Execution/include/QueueState.hpp b/core/libs/prx/libSceAgcDriver/Execution/include/QueueState.hpp index 8a178ac8fd..7c10d006f5 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/include/QueueState.hpp +++ b/core/libs/prx/libSceAgcDriver/Execution/include/QueueState.hpp @@ -272,7 +272,7 @@ struct QueueState { std::uint64_t indexBase = 0; std::uint64_t drawIndirectBase = 0; std::uint64_t dispatchIndirectBase = 0; - std::uint32_t indexBufferSize = 0; + std::optional indexBufferSize; std::uint32_t indexType = 0; std::uint32_t instanceCount = 1; std::vector markers; diff --git a/core/libs/prx/libSceAgcDriver/Execution/include/VideoOutput.hpp b/core/libs/prx/libSceAgcDriver/Execution/include/VideoOutput.hpp index 2c7443e9a5..c642371e8e 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/include/VideoOutput.hpp +++ b/core/libs/prx/libSceAgcDriver/Execution/include/VideoOutput.hpp @@ -55,6 +55,7 @@ public: extern "C" std::uint32_t AgcDriverReserveEopFlip_nid_postfix(const AgcDriver::FlipInfo& info); extern "C" void AgcDriverSubmitFlip_nid_postfix(const AgcDriver::FlipInfo& info); +extern "C" std::uint32_t AgcDriverReserveEopFlipAt_nid_postfix(const AgcDriver::FlipInfo& info, std::uint32_t token); extern "C" void AgcDriverRegisterVideoOutput_nid_postfix(std::uint32_t handle, const std::shared_ptr& output); extern "C" void AgcDriverUnregisterVideoOutput_nid_postfix(std::uint32_t handle, const std::shared_ptr& output); diff --git a/core/libs/prx/libSceAgcDriver/Execution/include/VulkanDevice.hpp b/core/libs/prx/libSceAgcDriver/Execution/include/VulkanDevice.hpp index 0627cd4ea3..8dbf80c316 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/include/VulkanDevice.hpp +++ b/core/libs/prx/libSceAgcDriver/Execution/include/VulkanDevice.hpp @@ -105,6 +105,8 @@ public: bool CanWaitUnlocked() const; std::uint64_t SubmitAndEpoch(); void WaitRecorded(std::uint64_t serial); + std::uint64_t SubmittedEpoch() const; + bool WaitRecordedFor(std::uint64_t serial, std::uint64_t timeoutNanos); void ReapRecorded(std::uint64_t serial); // Completes the batches that already finished (non-blocking); under the mutex. void ReapRecorded(); @@ -332,17 +334,17 @@ public: static void NoteRecipe(RecipeEvent event, RecipeKind kind); enum class DrawRecipePrecheck : std::size_t { NoRecipe, Device }; static void NoteDrawRecipeMiss(DrawRecipePrecheck miss); + using DrawTraceContext = Graphics::DrawTraceContext; // `recipe`, when given, receives the DrawRecipe a recorded, cacheable, reusable, direct draw // built for its draw-cache entry (design_cpu_final M8); null otherwise. - void Draw(const Graphics::State& graphics, const Pm4::DrawParameters& draw, std::span shaders, std::span snapshots = {}, std::shared_ptr* recipe = nullptr); + void Draw(const Graphics::State& graphics, const Pm4::DrawParameters& draw, std::span shaders, std::span snapshots = {}, std::shared_ptr* recipe = nullptr, const DrawTraceContext* trace = nullptr); bool DrawInputsInPlace() const; std::optional KnownDrawRejection(const Graphics::State& graphics, std::span shaders) const; void ColorMetadataPass(const Graphics::ColorMetadataPass& pass); - void DepthCopyPass(const Graphics::DepthCopyPass& pass); // A draw-cache hit recorded from its recipe (Graphics::DrawWithRecipe), under the mutex after // the packet's labels; Rebuild when the recipe's device is not this one or DrawWithRecipe // missed (nothing recorded: the caller runs Draw with the hit's stages and re-attaches). - RecipeOutcome DrawFromRecipe(const Graphics::State& graphics, const Pm4::DrawParameters& draw, std::span shaders, std::span snapshots, const std::shared_ptr& recipe); + RecipeOutcome DrawFromRecipe(const Graphics::State& graphics, const Pm4::DrawParameters& draw, std::span shaders, std::span snapshots, const std::shared_ptr& recipe, const DrawTraceContext* trace = nullptr); // The indirect draw features the device enabled, for the driver's decision whether an indirect // draw's records can be read by the GPU (Graphics::Draw) or must be resolved on the CPU. struct IndirectDrawSupport { diff --git a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/Draw.cpp b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/Draw.cpp index 175c487d29..477c943f72 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/Draw.cpp +++ b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/Draw.cpp @@ -1,5 +1,6 @@ #include "prx/libSceAgcDriver/Execution/include/CaptureTrace.hpp" #include "prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp" +#include "prx/libSceAgcDriver/Execution/include/CaptureTrace.hpp" #include "prx/libSceAgcDriver/Execution/include/Driver/Diagnostics.hpp" #include "prx/libSceAgcDriver/Execution/include/Driver/Draw/DrawPipeline.hpp" #include "prx/libSceAgcDriver/Execution/include/Driver/Memory/DrawWriteRanges.hpp" @@ -9,6 +10,7 @@ #include "prx/libSceAgcDriver/Execution/include/GuestMemory.hpp" #include "prx/libSceAgcDriver/Execution/include/PerformanceTimer.hpp" #include "prx/libSceAgcDriver/Execution/include/Pm4.hpp" +#include "prx/libSceAgcDriver/Execution/include/ProfileOutput.hpp" #include "Optimization/ResourceProgram.hpp" #include "prx/libc/include/HostThreadLocal.hpp" #include @@ -19,12 +21,106 @@ #include #include #include +#include +#include #include #include #include +#include +#include +#include +#include +#include +#include +#include namespace AgcDriver::DriverDetail { +namespace { + +bool TraceShaderCorrelation() { + static const bool enabled = std::getenv("APS5_TRACE_SHADER_CORRELATION") != nullptr; + return enabled && AgcDriver::CaptureTrace::Enabled(); +} + +std::atomic& NextDrawTraceToken() { + static std::atomic next{0}; + return next; +} + +std::string ExceptionReason(const std::exception_ptr& failure) { + try { + if (failure != nullptr) std::rethrow_exception(failure); + } catch (const std::exception& error) { + return error.what(); + } catch (...) { + } + return "unknown exception"; +} + +const char* ExceptionType(const std::exception_ptr& failure) { + try { + if (failure != nullptr) std::rethrow_exception(failure); + } catch (const std::exception& error) { + return typeid(error).name(); + } catch (...) { + } + return "unknown"; +} + +void TraceMaterializationFailure(std::uint64_t token, const Submission& submission, std::size_t packetOffset, const char* phase, std::size_t completedSubdraws, const std::exception_ptr& failure) { + if (token == 0) return; + try { + const auto reason = ExceptionReason(failure); + AgcDriver::CaptureTrace::Log("draw-materialization-failed token=%llu submission=%llu queue=%x offset=%zu phase=%s exception=%s reason=%.256s completedSubdraws=%zu", + static_cast(token), static_cast(submission.serial), submission.queue, packetOffset, phase, ExceptionType(failure), reason.c_str(), completedSubdraws); + } catch (...) { + + } +} + +void TraceSubmissionFailure(std::uint64_t token, const Submission& submission, std::size_t packetOffset, std::size_t completedSubdraws, const std::exception_ptr& failure) { + if (token == 0) return; + try { + const auto reason = ExceptionReason(failure); + AgcDriver::CaptureTrace::Log("draw-submission-failed token=%llu submission=%llu queue=%x offset=%zu exception=%s reason=%.256s completedSubdraws=%zu", + static_cast(token), static_cast(submission.serial), submission.queue, packetOffset, ExceptionType(failure), reason.c_str(), completedSubdraws); + } catch (...) { + + } +} + +void TraceDrawAttemptFailure(std::uint64_t token, const char* phase, const char* outcome, std::uint64_t submission, std::uint32_t queue, std::size_t packetOffset, std::string_view reason, std::string_view exceptionType = "none") { + if (token == 0) return; + AgcDriver::CaptureTrace::LogBestEffort("draw-attempt-failed token=%llu phase=%s outcome=%s submission=%llu queue=%x offset=%zu exception=%s reason=%.256s", + static_cast(token), phase, outcome, static_cast(submission), queue, packetOffset, exceptionType.data(), reason.data()); +} + +void TraceDrawAttemptException(std::uint64_t token, const char* phase, std::uint64_t submission, std::uint32_t queue, std::size_t packetOffset, const std::exception_ptr& failure) noexcept { + try { + TraceDrawAttemptFailure(token, phase, "exception", submission, queue, packetOffset, ExceptionReason(failure), ExceptionType(failure)); + } catch (...) { + + } +} + +const char* DrawVerdictName(DrawVerdict verdict) { + switch (verdict) { + case DrawVerdict::Drawn: return "Drawn"; + case DrawVerdict::Nothing: return "Nothing"; + case DrawVerdict::Rejected: return "Rejected"; + } + return "Unknown"; +} + +std::string_view TrimmedReason(std::string_view reason) { + if (reason.size() > 200) reason = reason.substr(0, 200); + return reason; + } + +} + + struct DrawTail { using GpuLock = std::unique_lock>; bool profile = false; @@ -46,7 +142,8 @@ struct DrawTail { std::shared_ptr dataEntry; std::uint64_t shapeKey = 0; ShaderRecompiler::ShaderPixelStageInfo pixel{}; - std::vector programs; + std::vector writablePrograms; + const std::vector* programs = nullptr; std::vector memory; std::vector linked; std::vector> vertexInfos; @@ -79,6 +176,8 @@ struct DrawTail { QueueState* queue = nullptr; std::span packet; std::string* rejected = nullptr; + std::uint64_t traceToken = 0; + VulkanDevice::DrawTraceContext trace{}; std::string ownRejected; GpuLock* gpuLock = nullptr; GpuLock ownLock{GuestMemory::GpuMutex(), std::defer_lock}; @@ -94,7 +193,7 @@ void noteFinisherHandOff(bool handed) { if (!stats) return; const auto tenSeconds = std::chrono::duration_cast(std::chrono::steady_clock::now().time_since_epoch()).count() / 10; if (reportedAt.exchange(tenSeconds, std::memory_order_relaxed) == tenSeconds) return; - std::fprintf(stderr, "[finisher] draws (10 s): %llu handed to the draw finisher, %llu finished on the worker%s\n", static_cast(handedOff.exchange(0)), static_cast(kept.exchange(0)), DrawFinisher::DrainText().c_str()); + AgcDriver::ProfilePrint_nid_no_patch("[finisher] draws (10 s): %llu handed to the draw finisher, %llu finished on the worker%s\n", static_cast(handedOff.exchange(0)), static_cast(kept.exchange(0)), DrawFinisher::DrainText().c_str()); } } @@ -147,7 +246,7 @@ DrawFinisher::Pending heldWrites(const DrawTail& tail) { noteHeld(HeldReason::NoDecode); return held; } - if (tail.matched.size() != tail.programs.size()) { + if (tail.matched.size() != tail.programs->size()) { noteHeld(HeldReason::Size); return held; } @@ -157,10 +256,10 @@ DrawFinisher::Pending heldWrites(const DrawTail& tail) { const auto& variant = tail.matched[i]; if (variant == nullptr || variant->compiled == nullptr) { const bool candidates = !tail.drawHit && i < tail.dataCandidates.size() && !tail.dataCandidates[i].empty(); - const auto& program = tail.programs[i]; + const auto& program = (*tail.programs)[i]; if (!candidates && program.snapshot != nullptr && !ProgramWritesMemory(*program.snapshot, program.codeOffset) && (program.tailContinuation == nullptr || !ProgramWritesMemory(*program.tailContinuation, 0))) continue; if (!candidates) { - const auto stage = tail.programs[i].binary.stage; + const auto stage = (*tail.programs)[i].binary.stage; noteHeld(stage == ShaderRecompiler::ShaderStage::Fragment ? HeldReason::NullFragment : stage == ShaderRecompiler::ShaderStage::Vertex ? HeldReason::NullVertex : HeldReason::NullOther); return held; } @@ -206,7 +305,7 @@ std::unordered_map& CpuIndirectPrograms() { } -DrawVerdict Driver::draw(QueueState& queue, std::span packet, const Submission& submission, std::string& rejected) { +DrawVerdict Driver::draw(QueueState& queue, std::span packet, const Submission& submission, std::size_t packetOffset, std::string& rejected) { PerformanceTimer timing("Driver.Draw"); static const bool profile = std::getenv("APS5_PROFILE_DRAW") != nullptr; auto tailHolder = std::make_shared(); @@ -230,8 +329,18 @@ DrawVerdict Driver::draw(QueueState& queue, std::span packe pending.tailAt = phaseLap; return DrawVerdict::Drawn; }; + const std::uint64_t traceToken = TraceShaderCorrelation() ? NextDrawTraceToken().fetch_add(1, std::memory_order_relaxed) + 1 : 0; + if (traceToken != 0) { + AgcDriver::CaptureTrace::LogBestEffort("draw-attempt token=%llu submission=%llu queue=%x offset=%zu", + static_cast(traceToken), static_cast(submission.serial), submission.queue, packetOffset); + } auto& drawParameters = tail.drawParameters; - drawParameters = Pm4::ResolveDraw(packet, queue); + try { + drawParameters = Pm4::ResolveDraw(packet, queue); + } catch (...) { + TraceDrawAttemptException(traceToken, "resolve-draw", submission.serial, submission.queue, packetOffset, std::current_exception()); + throw; + } tail.pipelined = DrawPipeline::Active(); const bool pipelined = tail.pipelined; static const bool pipelinedIndirect = std::getenv("APS5_NO_PIPELINED_INDIRECT") == nullptr; @@ -242,7 +351,10 @@ DrawVerdict Driver::draw(QueueState& queue, std::span packe if (!pipelinedIndirect || overlaps) pipeline.Drain(DrawPipeline::DrainReason::Indirect); } auto& traceIndirect = tail.traceIndirect; - if (const auto verdict = precheckDraw(queue, submission, packet, drawParameters, rejected, traceIndirect)) return *verdict; + if (const auto verdict = precheckDraw(queue, submission, packetOffset, packet, drawParameters, rejected, traceIndirect)) { + TraceDrawAttemptFailure(traceToken, "precheck", "rejected", submission.serial, submission.queue, packetOffset, rejected); + return *verdict; + } phaseTiming.Phase(DrawRowPrecheck); using Stage = ShaderRecompiler::ShaderStage; using Role = ShaderRecompiler::ProgramRole; @@ -391,17 +503,25 @@ DrawVerdict Driver::draw(QueueState& queue, std::span packe } decode = std::move(refreshed); } - resolveDrawDecode(queue, submission, localDevice->graphicsContext(), decode, registerKey, drawKey, profile); + try { + resolveDrawDecode(queue, submission, localDevice->graphicsContext(), decode, registerKey, drawKey, profile); + } catch (...) { + TraceDrawAttemptFailure(traceToken, "decode", "exception", submission.serial, submission.queue, packetOffset, ExceptionReason(std::current_exception())); + throw; + } const auto& graphics = decode->state; auto& pixel = tail.pixel; pixel = decode->pixel; - auto& programs = tail.programs; - programs = decode->programs; - for (const auto& patch : userPatches) programs[patch.program].userData[patch.index] = patch.value; + const bool writesPrograms = graphics.stages.mesh || drawParameters.indirect || !userPatches.empty(); + auto& writablePrograms = tail.writablePrograms; + if (writesPrograms) writablePrograms = decode->programs; + for (const auto& patch : userPatches) writablePrograms[patch.program].userData[patch.index] = patch.value; + tail.programs = writesPrograms ? &writablePrograms : &decode->programs; + const auto& programs = *tail.programs; if (const auto skipped = SkippedShader(); skipped != 0 && std::any_of(programs.begin(), programs.end(), [skipped](const DrawProgram& program) { return program.binary.codeAddress == skipped; })) return drawn(); const bool emptyIndirectSkip = EmptyIndirectSkip(); auto& cpuIndirectPrograms = CpuIndirectPrograms(); - const auto recordDraws = [&](const Pm4::DrawArguments& arguments) { return arguments.count != 0 && arguments.instances != 0 && (!drawParameters.indexed || arguments.firstVertexOrIndex < drawParameters.indexCount); }; + const auto recordDraws = [&](const Pm4::DrawArguments& arguments) { return arguments.count != 0 && arguments.instances != 0 && (!drawParameters.indexed || !drawParameters.indirect->indexLimitKnown || arguments.firstVertexOrIndex < drawParameters.indexCount); }; if (emptyIndirectSkip && drawParameters.indirect && !programs.empty()) { const auto known = cpuIndirectPrograms.find(programs.front().binary.codeAddress); if (known != cpuIndirectPrograms.end() && known->second) { @@ -420,7 +540,7 @@ DrawVerdict Driver::draw(QueueState& queue, std::span packe } const auto setMeshIndexBuffer = [&](const Pm4::DrawParameters& parameters) { if (!graphics.stages.mesh) return; - auto& words = programs.front().userData; + auto& words = writablePrograms.front().userData; require(programs.front().firstUserSgpr == 0 && words.size() >= ShaderRecompiler::MeshIndexBufferUserWord + 4, "mesh program lacks the hidden user words"); const auto descriptor = Graphics::MeshIndexBufferDescriptor(parameters); std::copy(descriptor.begin(), descriptor.end(), words.begin() + ShaderRecompiler::MeshIndexBufferUserWord); @@ -497,7 +617,6 @@ DrawVerdict Driver::draw(QueueState& queue, std::span packe auto& compiledStages = tail.compiledStages; compiledStages.reserve(programs.size()); auto& stages = tail.stages; - results.reserve(programs.size() + 2u); stages.reserve(programs.size()); auto& pushCursorBytes = tail.pushCursorBytes; @@ -526,7 +645,12 @@ DrawVerdict Driver::draw(QueueState& queue, std::span packe auto& verifyHit = tail.verifyHit; tail.stageEntries = useDrawEntries && !(graphics.stages.tessellation && !drawParameters.indirect && drawParameters.instanceCount > 1u); const bool stageEntries = tail.stageEntries; - lookupDraw(submission, localDevice, graphics, pixel, programs, roles, vertexInfos, stageEntries, registerKey, profile, drawKey, entry, matched, matchedRegions, drawHit, verifyHit, phaseTiming, phaseMs, dataEntry, dataCandidates, stageData); + try { + lookupDraw(submission, localDevice, graphics, pixel, programs, roles, vertexInfos, stageEntries, registerKey, profile, drawKey, entry, matched, matchedRegions, drawHit, verifyHit, phaseTiming, phaseMs, dataEntry, dataCandidates, stageData); + } catch (...) { + TraceDrawAttemptException(traceToken, "lookup", submission.serial, submission.queue, packetOffset, std::current_exception()); + throw; + } for (std::size_t i = 0; i < matched.size(); ++i) { if (matched[i] == nullptr || matched[i]->shaderDataPatches.empty()) continue; applyShaderDataPatches(*matched[i]); @@ -538,6 +662,14 @@ DrawVerdict Driver::draw(QueueState& queue, std::span packe if (drawHit || verifyHit) { ++drawRelocationCounters.hits; ++drawRelocationTrials[drawIdentity].hits; + if (const auto alias = drawAliases.find(drawAlias); alias != drawAliases.end()) { + const auto donor = drawCache.find(alias->second); + if (donor != drawCache.end() && drawCacheHits - donor->second->touched > drawCacheEntries() / 8) { + drawOrder.splice(drawOrder.begin(), drawOrder, donor->second->order); + donor->second->touched = drawCacheHits; + ++drawEntryCounters.touches; + } + } } if (profile && drawRelocationCounters.lookups >= 100000) reportDrawRelocation(); } @@ -548,6 +680,22 @@ DrawVerdict Driver::draw(QueueState& queue, std::span packe } tail.patchedStages = std::any_of(stageData.begin(), stageData.end(), [](const StageDataHit& hit) { return hit.compiled != nullptr; }); tail.relocatedDecode = relocatedDecode; + std::uint64_t stageDataVerified = 0; + const auto target = graphics.hasColorTarget ? graphics.color.address : 0; + const VulkanDevice::DrawTraceContext trace{traceToken, submission.serial, submission.queue, packetOffset, target}; + const auto* tracePtr = trace.token != 0 ? &trace : nullptr; + tail.traceToken = traceToken; + tail.trace = trace; + if (trace.token != 0) { + for (std::size_t i = 0; i < programs.size(); ++i) { + const auto& program = programs[i]; + AgcDriver::CaptureTrace::LogBestEffort("draw-selected token=%llu submission=%llu queue=%x offset=%zu snapshot=0x%llx code=%llx entryOffset=%zu entry=%llx stage=%u role=%u drawCacheHit=%u variantHit=%u target=%llx", + static_cast(trace.token), static_cast(submission.serial), submission.queue, packetOffset, + static_cast(reinterpret_cast(program.snapshot.get())), static_cast(program.snapshot->codeAddress), program.codeOffset, + static_cast(program.snapshot->codeAddress + program.codeOffset), static_cast(program.binary.stage), static_cast(roles[i]), + drawHit ? 1u : 0u, matched[i] != nullptr ? 1u : 0u, static_cast(target)); + } + } if (registerKey) { @@ -578,6 +726,7 @@ DrawVerdict Driver::draw(QueueState& queue, std::span packe tail.reg207 = readRegister(queue.context, 0x207); tail.dumpTarget = dumpTarget; tail.dumpSlot1 = dumpSlot1; + tail.packetOffset = packetOffset; const bool handOff = pipelined && !drawParameters.indirect && !profile && !lockedPrepare && dumpTarget == 0 && dumpSlot1 == 0 && !tracePrograms && !verifyDrawDataHits() && deferredLabels().labels.empty() && DrawFinisher::Enabled(); if (handOff) { tail.epoch = DrawPipeline::EpochToken().load(std::memory_order_relaxed); @@ -617,6 +766,11 @@ DrawVerdict Driver::finishDraw(DrawTail& tail, PerformanceTimer& timing) { using Stage = ShaderRecompiler::ShaderStage; using Role = ShaderRecompiler::ProgramRole; const bool profile = tail.profile; + const auto traceToken = tail.traceToken; + const auto& trace = tail.trace; + const auto* tracePtr = trace.token != 0 ? &trace : nullptr; + const auto packetOffset = tail.packetOffset; + const auto packet = tail.packet; auto& phaseMs = tail.phaseMs; auto& phaseLap = tail.phaseLap; auto& captures = tail.captures; @@ -640,7 +794,8 @@ DrawVerdict Driver::finishDraw(DrawTail& tail, PerformanceTimer& timing) { auto& shapeKey = tail.shapeKey; const auto& graphics = decode->state; auto& pixel = tail.pixel; - auto& programs = tail.programs; + auto& writablePrograms = tail.writablePrograms; + const auto& programs = *tail.programs; const std::vector& roles = decode->roles; auto& memory = tail.memory; auto& linked = tail.linked; @@ -682,7 +837,7 @@ DrawVerdict Driver::finishDraw(DrawTail& tail, PerformanceTimer& timing) { }; const auto setMeshIndexBuffer = [&](const Pm4::DrawParameters& parameters) { if (!graphics.stages.mesh) return; - auto& words = programs.front().userData; + auto& words = writablePrograms.front().userData; require(programs.front().firstUserSgpr == 0 && words.size() >= ShaderRecompiler::MeshIndexBufferUserWord + 4, "mesh program lacks the hidden user words"); const auto descriptor = Graphics::MeshIndexBufferDescriptor(parameters); std::copy(descriptor.begin(), descriptor.end(), words.begin() + ShaderRecompiler::MeshIndexBufferUserWord); @@ -746,6 +901,7 @@ DrawVerdict Driver::finishDraw(DrawTail& tail, PerformanceTimer& timing) { throw std::runtime_error(message); }; std::optional indirectCpu; + std::size_t completedSubdraws = 0; std::vector pushOffsets(programs.size(), 0); std::optional meshBarycentrics; std::vector resultIndex(programs.size(), 0); @@ -775,7 +931,7 @@ DrawVerdict Driver::finishDraw(DrawTail& tail, PerformanceTimer& timing) { break; } } - compiledStages.push_back(materializeDrawStage(i, pushCursorBytes, queue, submission, programs, graphics, pixel, vertexInfos, memory, linked, drawParameters, localDevice, *shaderMemory, stageCaptures, recompiled, drawHit, matched, matchedRegions, profile, dumpTarget, dumpSlot1, captures, phaseTiming, phaseMs, rejected, meshBarycentrics)); + compiledStages.push_back(materializeDrawStage(i, pushCursorBytes, queue, submission, packetOffset, trace.token, programs, graphics, pixel, vertexInfos, memory, linked, drawParameters, localDevice, *shaderMemory, stageCaptures, recompiled, drawHit, matched, matchedRegions, profile, dumpTarget, dumpSlot1, captures, phaseTiming, phaseMs, rejected, meshBarycentrics)); if (!rejected.empty()) return DrawVerdict::Rejected; programResults[i] = compiledStages.back().get(); if (candidate != dataCandidates[i].end()) verifyDataStage(i, *candidate->first); @@ -791,10 +947,15 @@ DrawVerdict Driver::finishDraw(DrawTail& tail, PerformanceTimer& timing) { } else if (i == 0) { fold(result, drawParameters); } - const auto slotEnd = (pushCursorBytes / Graphics::PipelinePushSlotBytes + 1u) * Graphics::PipelinePushSlotBytes; - require(result.pushConstants.size() <= slotEnd - pushCursorBytes, "stage push constants exceed the pipeline push constant block"); - stages.push_back({program.binary.stage, &result, result.pushConstants.empty() ? 0u : pushCursorBytes}); - pushCursorBytes += static_cast(result.pushConstants.size()); + try { + const auto slotEnd = (pushCursorBytes / Graphics::PipelinePushSlotBytes + 1u) * Graphics::PipelinePushSlotBytes; + require(result.pushConstants.size() <= slotEnd - pushCursorBytes, "stage push constants exceed the pipeline push constant block"); + stages.push_back({program.binary.stage, &result, result.pushConstants.empty() ? 0u : pushCursorBytes}); + pushCursorBytes += static_cast(result.pushConstants.size()); + } catch (...) { + TraceMaterializationFailure(trace.token, submission, packetOffset, "push-constant-validation", completedSubdraws, std::current_exception()); + throw; + } } if (stageDataVerified != 0) { @@ -836,29 +997,35 @@ DrawVerdict Driver::finishDraw(DrawTail& tail, PerformanceTimer& timing) { } if (tail.queue != nullptr && pipelined && !deferredLabels().labels.empty()) DrawPipeline::Queue0().Drain(DrawPipeline::DrainReason::Labels); - if (tail.queue != nullptr && recordQueuedLabelsAfterCapture(submission.queue, memory)) return draw(*tail.queue, tail.packet, submission, rejected); + if (recordQueuedLabelsAfterCapture(submission.queue, memory)) return draw(*tail.queue, packet, submission, packetOffset, rejected); bool rectListBuilt = false; std::size_t rectIndex = 0; const auto buildRectList = [&] { - phaseTiming.Phase(DrawRowVectors); - require(programs.size() == 2 && programResults[0] != nullptr && programResults[1] != nullptr, "rect-list requires vertex and fragment programs"); - auto rectangle = DrawRectangle(*programs[0].snapshot, programs[1].snapshot, programResults[0]->variantId, programResults[1]->variantId, localDevice->Target()); - auto& control = graphics.provokingVertexMode == VK_PROVOKING_VERTEX_MODE_LAST_VERTEX_EXT ? rectangle.lastVertexControl : rectangle.control; - if (rectListBuilt) { - results[rectIndex] = std::move(control); - results[rectIndex + 1] = std::move(rectangle.evaluation); + try { + phaseTiming.Phase(DrawRowVectors); + require(programs.size() == 2 && programResults[0] != nullptr && programResults[1] != nullptr, "rect-list requires vertex and fragment programs"); + auto rectangle = DrawRectangle(*programs[0].snapshot, programs[1].snapshot, programResults[0]->variantId, programResults[1]->variantId, localDevice->Target()); + auto& control = graphics.provokingVertexMode == VK_PROVOKING_VERTEX_MODE_LAST_VERTEX_EXT ? rectangle.lastVertexControl : rectangle.control; + if (rectListBuilt) { + results[rectIndex] = std::move(control); + results[rectIndex + 1] = std::move(rectangle.evaluation); + phaseTiming.Phase(DrawRowRectList); + return; + } + require(stages.size() == 2, "rect-list requires vertex and fragment programs"); + results.reserve(programs.size() + 2u); + rectIndex = results.size(); + results.push_back(std::move(control)); + results.push_back(std::move(rectangle.evaluation)); + stages.insert(stages.begin() + 1, {{Stage::TessellationControl, &results[rectIndex], 0}, {Stage::TessellationEvaluation, &results[rectIndex + 1], 0}}); + rectListBuilt = true; phaseTiming.Phase(DrawRowRectList); - return; + } catch (...) { + TraceMaterializationFailure(trace.token, submission, packetOffset, "rect-list", completedSubdraws, std::current_exception()); + throw; } - require(stages.size() == 2, "rect-list requires vertex and fragment programs"); - rectIndex = results.size(); - results.push_back(std::move(control)); - results.push_back(std::move(rectangle.evaluation)); - stages.insert(stages.begin() + 1, {{Stage::TessellationControl, &results[rectIndex], 0}, {Stage::TessellationEvaluation, &results[rectIndex + 1], 0}}); - rectListBuilt = true; - phaseTiming.Phase(DrawRowRectList); }; if (graphics.rectList) buildRectList(); if (!programs.empty() && programResults.back() != nullptr && programResults.back()->barycentricEmulation.active && graphics.stages.path == Graphics::ShaderPath::Geometry) { @@ -868,7 +1035,7 @@ DrawVerdict Driver::finishDraw(DrawTail& tail, PerformanceTimer& timing) { if (programResults[0]->meshBarycentrics != meshBarycentrics) { const auto pushBytes = programResults[0]->pushConstants.size(); const auto exports = programResults[0]->parameterExports; - compiledStages.push_back(materializeDrawStage(0, pushOffsets[0], queue, submission, programs, graphics, pixel, vertexInfos, memory, linked, drawParameters, localDevice, *shaderMemory, stageCaptures, recompiled, drawHit, matched, matchedRegions, profile, dumpTarget, dumpSlot1, captures, phaseTiming, phaseMs, rejected, meshBarycentrics)); + compiledStages.push_back(materializeDrawStage(0, pushOffsets[0], queue, submission, packetOffset, traceToken, programs, graphics, pixel, vertexInfos, memory, linked, drawParameters, localDevice, *shaderMemory, stageCaptures, recompiled, drawHit, matched, matchedRegions, profile, dumpTarget, dumpSlot1, captures, phaseTiming, phaseMs, rejected, meshBarycentrics)); if (!rejected.empty()) return DrawVerdict::Rejected; programResults[0] = compiledStages.back().get(); stages[0].program = programResults[0]; @@ -878,6 +1045,7 @@ DrawVerdict Driver::finishDraw(DrawTail& tail, PerformanceTimer& timing) { } else if (!graphics.rectList && !programs.empty() && programResults.back() != nullptr && programResults.back()->barycentricEmulation.active) { require(graphics.stages.path == Graphics::ShaderPath::Vertex && programs.size() == 2 && programResults[0] != nullptr && stages.size() == 2, "a pixel shader that reads barycentrics without VK_KHR_fragment_shader_barycentric needs a vertex shader before it"); const auto geometryLimits = localDevice->GeometryLimits(); + results.reserve(programs.size() + 2u); if (geometryLimits || !localDevice->Target().tessellation) { results.push_back(ShaderRecompiler::BuildBarycentricGeometryShader(*programResults[0], *programResults[1], localDevice->Target(), geometryLimits)); stages.insert(stages.begin() + 1, Graphics::CompiledShader{Stage::Geometry, &results.back(), 0}); @@ -919,6 +1087,7 @@ DrawVerdict Driver::finishDraw(DrawTail& tail, PerformanceTimer& timing) { if (drawParameters.indirect && indirectCpu) { const auto indirect = *drawParameters.indirect; + results.reserve(programs.size() + 2u); for (std::size_t i = 0; i < programs.size(); ++i) { if (programResults[i] == nullptr) continue; resultIndex[i] = results.size(); @@ -937,7 +1106,7 @@ DrawVerdict Driver::finishDraw(DrawTail& tail, PerformanceTimer& timing) { const auto baseVertexWords = UserDataWords(programs, roles, indirect.baseVertexLocation); const auto startInstanceWords = UserDataWords(programs, roles, indirect.startInstanceLocation); const auto drawIndexWords = indirect.drawIndexEnabled ? UserDataWords(programs, roles, indirect.drawIndexLocation) : std::vector>{}; - const auto drawable = [&](const Pm4::DrawArguments& arguments) { return arguments.count != 0 && arguments.instances != 0 && (!drawParameters.indexed || arguments.firstVertexOrIndex < drawParameters.indexCount); }; + const auto drawable = [&](const Pm4::DrawArguments& arguments) { return arguments.count != 0 && arguments.instances != 0 && (!drawParameters.indexed || !indirect.indexLimitKnown || arguments.firstVertexOrIndex < drawParameters.indexCount); }; std::size_t lastDrawable = records.size(); for (std::size_t record = 0; record < records.size(); ++record) { if (drawable(records[record])) lastDrawable = record; @@ -949,7 +1118,7 @@ DrawVerdict Driver::finishDraw(DrawTail& tail, PerformanceTimer& timing) { std::set patched; const auto patch = [&](const std::vector>& words, std::uint32_t value) { for (const auto& [program, word] : words) { - programs[program].userData[word] = value; + writablePrograms[program].userData[word] = value; patched.insert(program); } }; @@ -963,10 +1132,16 @@ DrawVerdict Driver::finishDraw(DrawTail& tail, PerformanceTimer& timing) { } Pm4::DrawParameters direct{0, arguments.count, 0, arguments.instances, drawParameters.flags, drawParameters.indexed, 0, 0}; if (drawParameters.indexed) { - - if (arguments.firstVertexOrIndex >= drawParameters.indexCount) continue; + if (indirect.indexLimitKnown) { + if (arguments.firstVertexOrIndex >= drawParameters.indexCount) continue; + direct.indexCount = std::min(arguments.count, drawParameters.indexCount - arguments.firstVertexOrIndex); + } else { + const auto end = static_cast(arguments.firstVertexOrIndex) + arguments.count; + require(end <= std::numeric_limits::max(), "indirect draw index range overflow"); + require(end * drawParameters.indexSize <= std::numeric_limits::max() - drawParameters.indexAddress, "indirect draw index range address overflow"); + direct.indexCount = arguments.count; + } direct.indexAddress = drawParameters.indexAddress + static_cast(arguments.firstVertexOrIndex) * drawParameters.indexSize; - direct.indexCount = std::min(arguments.count, drawParameters.indexCount - arguments.firstVertexOrIndex); direct.indexSize = drawParameters.indexSize; } else { direct.firstVertex = indirect.indxOffset; @@ -979,7 +1154,7 @@ DrawVerdict Driver::finishDraw(DrawTail& tail, PerformanceTimer& timing) { auto& result = results[resultIndex[programIndex]]; const auto pushBytes = result.pushConstants.size(); decodeVertexInfo(programIndex); - result = *materializeDrawStage(programIndex, pushOffsets[programIndex], queue, submission, programs, graphics, pixel, vertexInfos, memory, linked, programs[programIndex].binary.stage == Stage::TessellationControl || programs[programIndex].binary.stage == Stage::TessellationEvaluation ? direct : drawParameters, localDevice, *shaderMemory, stageCaptures, recompiled, drawHit, matched, matchedRegions, profile, dumpTarget, dumpSlot1, captures, phaseTiming, phaseMs, rejected, meshBarycentrics); + result = *materializeDrawStage(programIndex, pushOffsets[programIndex], queue, submission, packetOffset, trace.token, programs, graphics, pixel, vertexInfos, memory, linked, programs[programIndex].binary.stage == Stage::TessellationControl || programs[programIndex].binary.stage == Stage::TessellationEvaluation ? direct : drawParameters, localDevice, *shaderMemory, stageCaptures, recompiled, drawHit, matched, matchedRegions, profile, dumpTarget, dumpSlot1, captures, phaseTiming, phaseMs, rejected, meshBarycentrics); if (!rejected.empty()) return DrawVerdict::Rejected; require(result.pushConstants.size() == pushBytes, "patched program changed its push constant layout"); } @@ -996,7 +1171,7 @@ DrawVerdict Driver::finishDraw(DrawTail& tail, PerformanceTimer& timing) { const auto commitQueue = submission.queue; const auto commitPacket = tail.packetOpcode; const auto epoch = DrawPipeline::EpochToken().load(std::memory_order_relaxed); - auto commit = [this, localDevice = std::move(localDevice), decode = std::move(decode), direct, stages = std::move(stages), snapshots = std::move(snapshots), commitQueue, commitPacket, epoch, programs = std::move(programs), results = std::move(results), memory = std::move(memory), linked = std::move(linked), stageCaptures = std::move(stageCaptures), matched = std::move(matched), matchedRegions = std::move(matchedRegions), fresh = std::move(fresh), decodeReads = std::move(decodeReads), shaderMemory = std::move(shaderMemory), entry = std::move(entry), dataEntry = std::move(dataEntry), compiledStages = std::move(compiledStages), stageData = std::move(stageData)] { + auto commit = [this, localDevice = std::move(localDevice), decode = std::move(decode), direct, stages = std::move(stages), snapshots = std::move(snapshots), commitQueue, commitPacket, epoch, writablePrograms = std::move(writablePrograms), results = std::move(results), memory = std::move(memory), linked = std::move(linked), stageCaptures = std::move(stageCaptures), matched = std::move(matched), matchedRegions = std::move(matchedRegions), fresh = std::move(fresh), decodeReads = std::move(decodeReads), shaderMemory = std::move(shaderMemory), entry = std::move(entry), dataEntry = std::move(dataEntry), compiledStages = std::move(compiledStages), stageData = std::move(stageData)] { DrawPipeline::FollowEpoch(epoch); GuestMemory::SetCurrentPacket(commitPacket, commitQueue); commitDraw(localDevice, commitQueue, decode->state, direct, stages, snapshots, nullptr, {}, 0); @@ -1011,7 +1186,7 @@ DrawVerdict Driver::finishDraw(DrawTail& tail, PerformanceTimer& timing) { lockForDraw(); noteDrawWriters(stages, submission.queue); phaseTiming.Phase(DrawRowVectors); - localDevice->Draw(graphics, direct, stages, snapshots); + localDevice->Draw(graphics, direct, stages, snapshots, nullptr, tracePtr); phaseTiming.Phase(DrawRowGraphics); } timing.Mark("draw_and_resource_release"); @@ -1042,10 +1217,10 @@ DrawVerdict Driver::finishDraw(DrawTail& tail, PerformanceTimer& timing) { const auto commitPacket = tail.packetOpcode; const auto epoch = DrawFinisher::OnFinisherThread() ? tail.epoch : DrawPipeline::EpochToken().load(std::memory_order_relaxed); auto reads = QueuedDrawReadRanges(drawParameters, stages); - auto commit = [this, localDevice = std::move(localDevice), decode = std::move(decode), drawParameters, stages = std::move(stages), snapshots = std::move(snapshots), recipe = std::move(recipe), recipeStages = std::move(recipeStages), drawKey, commitQueue, commitPacket, epoch, programs = std::move(programs), results = std::move(results), memory = std::move(memory), linked = std::move(linked), stageCaptures = std::move(stageCaptures), matched = std::move(matched), matchedRegions = std::move(matchedRegions), fresh = std::move(fresh), decodeReads = std::move(decodeReads), shaderMemory = std::move(shaderMemory), entry = std::move(entry), dataEntry = std::move(dataEntry), compiledStages = std::move(compiledStages), stageData = std::move(stageData)]() mutable { + auto commit = [this, localDevice = std::move(localDevice), decode = std::move(decode), drawParameters, stages = std::move(stages), snapshots = std::move(snapshots), recipe = std::move(recipe), recipeStages = std::move(recipeStages), drawKey, commitQueue, commitPacket, epoch, trace = trace, writablePrograms = std::move(writablePrograms), results = std::move(results), memory = std::move(memory), linked = std::move(linked), stageCaptures = std::move(stageCaptures), matched = std::move(matched), matchedRegions = std::move(matchedRegions), fresh = std::move(fresh), decodeReads = std::move(decodeReads), shaderMemory = std::move(shaderMemory), entry = std::move(entry), dataEntry = std::move(dataEntry), compiledStages = std::move(compiledStages), stageData = std::move(stageData)]() mutable { DrawPipeline::FollowEpoch(epoch); GuestMemory::SetCurrentPacket(commitPacket, commitQueue); - commitDraw(localDevice, commitQueue, decode->state, drawParameters, stages, snapshots, recipe, recipeStages, drawKey); + commitDraw(localDevice, commitQueue, decode->state, drawParameters, stages, snapshots, recipe, recipeStages, drawKey, trace); }; auto held = std::make_shared(std::move(commit)); DrawPipeline::Queue0().Enqueue([held] { (*held)(); }, std::move(writes), 0, {}, std::move(reads), true); @@ -1058,7 +1233,7 @@ DrawVerdict Driver::finishDraw(DrawTail& tail, PerformanceTimer& timing) { noteDrawWriters(stages, submission.queue); phaseTiming.Phase(DrawRowVectors); if (recipe != nullptr) { - if (localDevice->DrawFromRecipe(graphics, drawParameters, stages, snapshots, recipe) == RecipeOutcome::Recorded) { + if (localDevice->DrawFromRecipe(graphics, drawParameters, stages, snapshots, recipe, tracePtr) == RecipeOutcome::Recorded) { phaseTiming.Phase(DrawRowGraphics); timing.Mark("draw_and_resource_release"); return drawn(); @@ -1067,14 +1242,15 @@ DrawVerdict Driver::finishDraw(DrawTail& tail, PerformanceTimer& timing) { VulkanDevice::NoteRecipe(VulkanDevice::RecipeEvent::Restart, VulkanDevice::RecipeKind::Draw); } std::shared_ptr built; - localDevice->Draw(graphics, drawParameters, stages, snapshots, recipeStages.empty() ? nullptr : &built); + localDevice->Draw(graphics, drawParameters, stages, snapshots, recipeStages.empty() ? nullptr : &built, tracePtr); phaseTiming.Phase(DrawRowGraphics); if (built != nullptr) attachDrawRecipe(drawKey, recipeStages, std::move(built)); timing.Mark("draw_and_resource_release"); return drawn(); } -void Driver::commitDraw(std::shared_ptr localDevice, std::uint32_t queue, const Graphics::State& graphics, const Pm4::DrawParameters& drawParameters, std::span stages, std::span snapshots, const std::shared_ptr& recipe, const std::vector>& recipeStages, std::uint64_t drawKey) { +void Driver::commitDraw(std::shared_ptr localDevice, std::uint32_t queue, const Graphics::State& graphics, const Pm4::DrawParameters& drawParameters, std::span stages, std::span snapshots, const std::shared_ptr& recipe, const std::vector>& recipeStages, std::uint64_t drawKey, VulkanDevice::DrawTraceContext trace) { + const auto* tracePtr = trace.token != 0 ? &trace : nullptr; GuestMemory::TagGpuLockSite(GuestMemory::GpuLockSite::Draw); std::lock_guard gpuLock(GuestMemory::GpuMutex()); if (auto current = device.Load(); current != nullptr && current != localDevice) localDevice = std::move(current); @@ -1088,11 +1264,11 @@ void Driver::commitDraw(std::shared_ptr localDevice, std::uint32_t recordLabelsForPacket(localDevice.get(), queue); noteDrawWriters(stages, queue); if (recipe != nullptr) { - if (localDevice->DrawFromRecipe(graphics, drawParameters, stages, snapshots, recipe) == RecipeOutcome::Recorded) return; + if (localDevice->DrawFromRecipe(graphics, drawParameters, stages, snapshots, recipe, tracePtr) == RecipeOutcome::Recorded) return; VulkanDevice::NoteRecipe(VulkanDevice::RecipeEvent::Restart, VulkanDevice::RecipeKind::Draw); } std::shared_ptr built; - localDevice->Draw(graphics, drawParameters, stages, snapshots, recipeStages.empty() ? nullptr : &built); + localDevice->Draw(graphics, drawParameters, stages, snapshots, recipeStages.empty() ? nullptr : &built, tracePtr); if (built != nullptr) attachDrawRecipe(drawKey, recipeStages, std::move(built)); } diff --git a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawCapture.cpp b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawCapture.cpp index 288ca494b9..a6f6b5ebd9 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawCapture.cpp +++ b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawCapture.cpp @@ -1,4 +1,5 @@ #include "prx/libSceAgcDriver/Execution/include/PerformanceTimer.hpp" +#include "prx/libSceAgcDriver/Execution/include/CaptureTrace.hpp" #include "prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp" #include "prx/libSceAgcDriver/Execution/include/Driver/Diagnostics.hpp" #include "prx/libSceAgcDriver/Execution/include/Driver/Shaders/ShaderRegistry.hpp" @@ -11,10 +12,55 @@ #include #include #include +#include +#include namespace AgcDriver::DriverDetail { -std::shared_ptr Driver::materializeDrawStage(std::size_t i, std::uint32_t pushOffset, const QueueState& queue, const Submission& submission, const std::vector& programs, const Graphics::State& graphics, const ShaderRecompiler::ShaderPixelStageInfo& pixel, const std::vector>& vertexInfos, std::vector& memory, const std::vector& linked, const Pm4::DrawParameters& drawParameters, const std::shared_ptr& localDevice, ShaderMemory& shaderMemory, std::vector& stageCaptures, std::vector& recompiled, bool drawHit, const std::vector>& matched, const std::vector>& matchedRegions, bool profile, std::uint64_t dumpTarget, std::uint64_t dumpSlot1, std::uint64_t& captures, DrawPhaseTiming& phaseTiming, std::array& phaseMs, std::string& rejected, const std::optional& meshBarycentrics) try { +namespace { + +void TraceMaterializationFailure(std::uint64_t token, const Submission& submission, std::size_t packetOffset, std::size_t stage, const char* phase, const std::exception_ptr& failure) { + if (token == 0) return; + const char* type = "unknown"; + std::string reason = "unknown exception"; + try { + if (failure != nullptr) std::rethrow_exception(failure); + } catch (const std::exception& error) { + type = typeid(error).name(); + reason = error.what(); + } catch (...) { + } + try { + AgcDriver::CaptureTrace::Log("draw-materialization-failed token=%llu submission=%llu queue=%x offset=%zu stage=%zu phase=%s exception=%s reason=%.256s", + static_cast(token), static_cast(submission.serial), submission.queue, packetOffset, stage, phase, type, reason.c_str()); + } catch (...) { + } +} + +void TraceShaderRequestFailure(std::uint64_t token, const Submission& submission, std::size_t packetOffset, const DrawProgram& program, const std::exception_ptr& failure) noexcept { + if (token == 0) return; + try { + std::string reason = "unknown exception"; + const char* type = "unknown"; + try { + if (failure != nullptr) std::rethrow_exception(failure); + } catch (const std::exception& error) { + type = typeid(error).name(); + reason = error.what(); + } catch (...) { + } + AgcDriver::CaptureTrace::LogBestEffort("draw-shader-request-failed token=%llu submission=%llu queue=%x offset=%zu snapshot=0x%llx code=%llx codeOffset=%zu stage=%u type=%u reason=%.256s", + static_cast(token), static_cast(submission.serial), submission.queue, packetOffset, + static_cast(reinterpret_cast(program.snapshot.get())), + static_cast(program.snapshot->codeAddress), program.codeOffset, + static_cast(program.binary.stage), static_cast(program.snapshot->type), reason.c_str()); + } catch (...) { + } +} + +} + +std::shared_ptr Driver::materializeDrawStage(std::size_t i, std::uint32_t pushOffset, const QueueState& queue, const Submission& submission, std::size_t packetOffset, std::uint64_t traceToken, const std::vector& programs, const Graphics::State& graphics, const ShaderRecompiler::ShaderPixelStageInfo& pixel, const std::vector>& vertexInfos, std::vector& memory, const std::vector& linked, const Pm4::DrawParameters& drawParameters, const std::shared_ptr& localDevice, ShaderMemory& shaderMemory, std::vector& stageCaptures, std::vector& recompiled, bool drawHit, const std::vector>& matched, const std::vector>& matchedRegions, bool profile, std::uint64_t dumpTarget, std::uint64_t dumpSlot1, std::uint64_t& captures, DrawPhaseTiming& phaseTiming, std::array& phaseMs, std::string& rejected, const std::optional& meshBarycentrics) try { PerformanceTimer timing("Shader.DrawStage"); using Stage = ShaderRecompiler::ShaderStage; phaseTiming.Phase(DrawRowVectors); @@ -28,7 +74,14 @@ std::shared_ptr Driver::materializeDraw ShaderRecompiler::GraphicsCompileContext{program.firstUserSgpr, linked, graphics.stages.mesh, graphics.stages.tessellation, {drawParameters.indexAddress, drawParameters.indexCount, drawParameters.indexSize, drawParameters.instanceCount}, program.binary.stage == Stage::Mesh ? meshBarycentrics : std::nullopt} }; const auto waitedBefore = traceCapSync() || profile ? Graphics::Recorder::ThreadWaitedMs() : 0.0; - auto invocation = InvocationFor(*program.snapshot, program.codeOffset, request); + auto invocation = [&] { + try { + return InvocationFor(*program.snapshot, program.codeOffset, request); + } catch (...) { + TraceShaderRequestFailure(traceToken, submission, packetOffset, program, std::current_exception()); + throw; + } + }(); timing.Mark("prepared_invocation"); auto& stageCapture = stageCaptures[i]; stageCapture.forgetSerial = GuestMemory::ForgetSerial(); @@ -73,16 +126,16 @@ std::shared_ptr Driver::materializeDraw ++captures; phaseTiming.Phase(DrawRowCapture); - const auto waited = std::min(Graphics::Recorder::ThreadWaitedMs() - waitedBefore, phaseMs[DrawRowCapture]); - phaseMs[DrawRowCapture] -= waited; - phaseMs[DrawRowCaptureHookWaits] += waited; - } - if (dumpTarget != 0) { + const auto waited = std::min(Graphics::Recorder::ThreadWaitedMs() - waitedBefore, phaseMs[DrawRowCapture]); + phaseMs[DrawRowCapture] -= waited; + phaseMs[DrawRowCaptureHookWaits] += waited; + } + if (dumpTarget != 0) { const auto slot0 = (static_cast(readRegister(queue.context, 0x390)) << 40u) | (static_cast(readRegister(queue.context, 0x318)) << 8u); if ((graphics.hasColorTarget && graphics.color.address == dumpTarget) || slot0 == dumpTarget) static_cast(dumpRequest(program.binary.codeAddress, request)); - } - if (dumpSlot1 != 0) { + } + if (dumpSlot1 != 0) { const auto value = [&](std::uint32_t offset) -> std::uint64_t { const auto it = queue.context.find(offset); return it == queue.context.end() ? 0u : it->second; }; const auto slot1 = (value(0x391) << 40u) | (value(0x327) << 8u); if (slot1 == dumpSlot1) { @@ -94,17 +147,18 @@ std::shared_ptr Driver::materializeDraw std::fclose(file); } } - } + } - phaseTiming.Phase(DrawRowCapture); + phaseTiming.Phase(DrawRowCapture); timing.Mark("capture_resources"); try { stageCapture.compiled = invocation.Materialize(*capture); + stageCapture.capture = capture; } catch (const std::exception& error) { + TraceMaterializationFailure(traceToken, submission, packetOffset, i, "capture-or-materialize", std::current_exception()); throw named(error, request.context.memory); } - stageCapture.capture = capture; timing.Mark("materialize"); const auto walked = capture->userDataUse.asValue | capture->userDataUse.asBase; auto mask = walked == 0 ? ~std::uint64_t{0} : walked | stageCapture.compiled->userDataRegisterReads; diff --git a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawDecode.cpp b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawDecode.cpp index 309a87a2aa..0672150150 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawDecode.cpp +++ b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawDecode.cpp @@ -179,6 +179,7 @@ std::shared_ptr Driver::decodeDraw(const QueueState& queue, const Su auto product = std::make_shared(); product->state = Graphics::DecodeState(queue, VertexWorkgroupRequired(queue, *submission.shaders)); DecodeGraphicsPrograms(*product, queue, *submission.shaders, false, true); + product->state.depthCopySource = std::ranges::any_of(product->programs, [](const DrawProgram& program) { return program.binary.codeAddress == DepthCopyPixelProgramAddress(true); }); product->pixel = Graphics::DecodePixelStageInfo(queue.context, Graphics::ExportMappings(product->state), Graphics::PixelProgramSkipped(queue)); Graphics::NormalizeUnusedViewportDepth(product->state, product->pixel); Graphics::ApplyDepthRemap(context, product->state); diff --git a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawPipeline.cpp b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawPipeline.cpp index c207db4cc2..a1a5ebaab1 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawPipeline.cpp +++ b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawPipeline.cpp @@ -3,6 +3,7 @@ #include "prx/libSceAgcDriver/Execution/include/GuestMemory.hpp" #include "prx/libSceAgcDriver/Execution/include/HandoffSpin.hpp" #include "prx/libSceAgcDriver/Execution/include/PerformanceTrace.hpp" +#include "prx/libSceAgcDriver/Execution/include/ProfileOutput.hpp" #include "prx/libSceAgcDriver/Execution/include/WorkerSampler.hpp" #include "prx/libc/include/HostThreadLocal.hpp" #include @@ -364,7 +365,7 @@ void DrawPipeline::report(std::chrono::steady_clock::time_point now) { std::snprintf(reads, sizeof(reads), " %llu read ranges, %llu stores held, %llu decode drains", static_cast(heldReadRanges.load(std::memory_order_relaxed)), static_cast(heldReadHits.exchange(0, std::memory_order_relaxed)), static_cast(decodeDrainCount - reportedDecodeDrains)); reportedDecodeDrains = decodeDrainCount; drainText += reads; - std::fprintf(stderr, "[draw] pipelined (10 s, depth %zu): %llu commits at %.1f us each on the committer, %llu failed; %llu enqueued behind %.2f on average, waiting %.1f us per draw for room, %.2f us for the lock; %llu committer wakes;%s; drains:%s\n", Depth(), static_cast(commits), perCommit, static_cast(commitErrors), static_cast(enqueued), queued, perDraw, enqueued == 0 ? 0.0 : static_cast(lockWaitNs) / 1e3 / static_cast(enqueued), static_cast(wakes), reads, drainText.c_str()); + AgcDriver::ProfilePrint_nid_no_patch("[draw] pipelined (10 s, depth %zu): %llu commits at %.1f us each on the committer, %llu failed; %llu enqueued behind %.2f on average, waiting %.1f us per draw for room, %.2f us for the lock; %llu committer wakes;%s; drains:%s\n", Depth(), static_cast(commits), perCommit, static_cast(commitErrors), static_cast(enqueued), queued, perDraw, enqueued == 0 ? 0.0 : static_cast(lockWaitNs) / 1e3 / static_cast(enqueued), static_cast(wakes), reads, drainText.c_str()); commits = commitNs = commitErrors = enqueued = fullWaitNs = depthSum = wakes = lockWaitNs = 0; drains = {}; drainWaitNs = {}; diff --git a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawPrecheck.cpp b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawPrecheck.cpp index b6749edc50..033f0313a8 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawPrecheck.cpp +++ b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawPrecheck.cpp @@ -1,6 +1,8 @@ #include "prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp" #include "prx/libSceAgcDriver/Execution/include/Driver/Draw/DrawPipeline.hpp" #include "prx/libSceAgcDriver/Execution/include/Driver/Diagnostics.hpp" +#include "prx/libSceAgcDriver/Execution/include/Driver/Shaders/ShaderRegistry.hpp" +#include "prx/libSceAgcDriver/Graphics/include/DepthSurface.hpp" #include "prx/libSceAgcDriver/Execution/include/GuestMemory.hpp" #include "prx/libSceAgcDriver/Execution/include/Pm4.hpp" #include "prx/libSceAgcDriver/Execution/include/Driver/Synchronization/DeferredLabels.hpp" @@ -8,12 +10,39 @@ namespace AgcDriver::DriverDetail { +namespace { + +QueueState DepthCopyDraw(const QueueState& queue, const Graphics::DepthCopyPass& copy) { + const bool multisampled = copy.source.samples != 1u; + const auto address = DepthCopyPixelProgramAddress(multisampled); + const auto words = Graphics::DepthCopySourceWords(copy.source); + QueueState draw = queue; + draw.context.insert_or_assign(0x000u, 0u); + draw.shader.insert_or_assign(0x008u, static_cast(address >> 8u)); + draw.shader.insert_or_assign(0x009u, static_cast(address >> 40u)); + draw.shader.insert_or_assign(0x00au, 0u); + draw.shader.insert_or_assign(0x00bu, DepthCopyPixelUserWords(multisampled) << 1u); + for (std::uint32_t index = 0; index < words.size(); ++index) draw.shader.insert_or_assign(0x00cu + index, words[index]); + if (multisampled) draw.shader.insert_or_assign(0x014u, copy.sample); + draw.context.insert_or_assign(0x1b3u, 0x8002u); + draw.context.insert_or_assign(0x1b4u, 0x8002u); + draw.context.insert_or_assign(0x1b6u, 0u); + draw.context.insert_or_assign(0x1c4u, 0u); + draw.context.insert_or_assign(0x1c5u, 1u); + draw.context.insert_or_assign(0x08fu, 1u); + draw.context.insert_or_assign(0x203u, 0u); + if (const auto alpha = draw.context.find(0x2dcu); alpha != draw.context.end()) draw.context.insert_or_assign(0x2dcu, alpha->second & ~1u); + return draw; +} + +} + bool Driver::drawPrecheck() { static const bool precheck = std::getenv("APS5_NO_DRAW_PRECHECK") == nullptr; return precheck; } -std::optional Driver::precheckDraw(const QueueState& queue, const Submission& submission, std::span packet, const Pm4::DrawParameters& drawParameters, std::string& rejected, bool& traceIndirect) { +std::optional Driver::precheckDraw(const QueueState& queue, const Submission& submission, std::size_t packetOffset, std::span packet, const Pm4::DrawParameters& drawParameters, std::string& rejected, bool& traceIndirect) { if (!drawParameters.indirect && !drawParameters.indexed && (drawParameters.indexCount == 0 || drawParameters.instanceCount == 0)) return DrawVerdict::Nothing; if (const auto pass = Graphics::DecodeColorMetadataPass(queue)) { require(!drawParameters.indirect, "indirect CB metadata passes are unsupported"); @@ -47,14 +76,8 @@ std::optional Driver::precheckDraw(const QueueState& queue, const S } if (const auto copy = Graphics::DecodeDepthCopyPass(queue)) { require(!drawParameters.indirect, "indirect DB depth copies are unsupported"); - if (DrawPipeline::Active()) DrawPipeline::Queue0().Drain(DrawPipeline::DrainReason::Packet, (packet[0] >> 8u) & 0xffu); - GuestMemory::TagGpuLockSite(GuestMemory::GpuLockSite::Draw); - std::lock_guard gpuLock(GuestMemory::GpuMutex()); - if (device == nullptr) device = std::make_shared(); - const std::shared_ptr localDevice = device; - recordLabelsForPacket(localDevice.get(), submission.queue); - localDevice->DepthCopyPass(*copy); - return DrawVerdict::Drawn; + auto copyDraw = DepthCopyDraw(queue, *copy); + return draw(copyDraw, packet, submission, packetOffset, rejected); } if (Graphics::DepthResummarizeDrawsNothing(queue)) return DrawVerdict::Nothing; rejected = Graphics::DepthMaintenanceRejection(queue); diff --git a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawRegisterKey.cpp b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawRegisterKey.cpp index 62fea0e6ae..7acaf98715 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawRegisterKey.cpp +++ b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawRegisterKey.cpp @@ -251,7 +251,7 @@ bool Driver::sameDecode(const DrawDecode& a, const DrawDecode& b) { for (std::size_t i = 0; i < s.blends.size(); ++i) { if (!sameBlend(s.blends[i], t.blends[i])) return false; } - if (s.hasColorTarget != t.hasColorTarget || s.rectList != t.rectList || s.renderExtent.width != t.renderExtent.width || s.renderExtent.height != t.renderExtent.height || s.topology != t.topology || s.negativeOneToOne != t.negativeOneToOne || s.depthClamp != t.depthClamp || s.conservativeRasterization != t.conservativeRasterization || s.perSampleShading != t.perSampleShading || s.cullMode != t.cullMode || s.polygonMode != t.polygonMode || s.frontFace != t.frontFace || !sameBlend(s.blend, t.blend) || s.blendConstants != t.blendConstants) return false; + if (s.colorRop != t.colorRop || s.hasColorTarget != t.hasColorTarget || s.rectList != t.rectList || s.renderExtent.width != t.renderExtent.width || s.renderExtent.height != t.renderExtent.height || s.topology != t.topology || s.negativeOneToOne != t.negativeOneToOne || s.depthClamp != t.depthClamp || s.conservativeRasterization != t.conservativeRasterization || s.perSampleShading != t.perSampleShading || s.cullMode != t.cullMode || s.polygonMode != t.polygonMode || s.frontFace != t.frontFace || !sameBlend(s.blend, t.blend) || s.blendConstants != t.blendConstants) return false; if (std::memcmp(&s.viewport, &t.viewport, sizeof(VkViewport)) != 0 || std::memcmp(&s.scissor, &t.scissor, sizeof(VkRect2D)) != 0) return false; const auto& p = a.pixel; const auto& q = b.pixel; diff --git a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawRelocate.cpp b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawRelocate.cpp index 33665a380c..c03e05baea 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawRelocate.cpp +++ b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawRelocate.cpp @@ -173,15 +173,21 @@ void Driver::learnPendingRelocation() { } if (pending.alias == 0) return; if (drawAliases.size() >= MaxAliases) drawRelocationCounters.evicted += dropEveryOther(drawAliases); - drawAliases[pending.alias] = inserted->second; + drawAliases[pending.alias] = pending.key; } void Driver::relocateDrawEntry(const QueueState& queue, std::uint64_t alias, std::shared_ptr& entry, std::shared_ptr& decode, std::vector& patches) { auto& counters = drawRelocationCounters; ++counters.lookups; const auto found = drawAliases.find(alias); - if (found == drawAliases.end() || found->second->decode == nullptr) return; - const auto donor = found->second; + if (found == drawAliases.end()) return; + const auto cached = drawCache.find(found->second); + if (cached == drawCache.end()) { + drawAliases.erase(found); + return; + } + if (cached->second->decode == nullptr) return; + const auto donor = cached->second; const auto& programs = donor->decode->programs; if (donor->stages.size() != programs.size()) return; struct Move { diff --git a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/IndirectDraw.cpp b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/IndirectDraw.cpp index d3049765aa..fa1676e68c 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/IndirectDraw.cpp +++ b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/IndirectDraw.cpp @@ -29,7 +29,8 @@ std::optional Driver::ClassifyIndirectDraw(const Sha using Path = Graphics::IndirectDrawPath; const auto support = localDevice->DrawIndirectSupport(); const auto fail = [&](Path reason) { if (!indirectCpu) indirectCpu = reason; }; - if (drawParameters.indexed && !GuestMemory::Accessible(reinterpret_cast(drawParameters.indexAddress), static_cast(drawParameters.indexCount) * drawParameters.indexSize)) fail(Path::IndexRange); + if (drawParameters.indexed && !indirect.indexLimitKnown) fail(Path::IndexLimitUnknown); + else if (drawParameters.indexed && !GuestMemory::Accessible(reinterpret_cast(drawParameters.indexAddress), static_cast(drawParameters.indexCount) * drawParameters.indexSize)) fail(Path::IndexRange); constexpr std::uint32_t NoLocation = 0x280u; const bool meshPath = graphics.stages.mesh.has_value() && !graphics.stages.tessellation && !graphics.rectList; const bool meshGpuSide = meshPath && drawParameters.indexed && indirect.count == 1 && !indirect.countIndirect && indirect.baseVertexLocation == NoLocation && indirect.startInstanceLocation == NoLocation && (!indirect.drawIndexEnabled || indirect.drawIndexLocation == NoLocation); diff --git a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/EntryPoints.cpp b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/EntryPoints.cpp index 8e846cf2fa..af6202cb08 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/EntryPoints.cpp +++ b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/EntryPoints.cpp @@ -1,15 +1,28 @@ #include "prx/libSceAgcDriver/Execution/include/GuestMemory.hpp" #include "prx/libSceAgcDriver/Execution/include/ThreadPriority.hpp" #include "prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp" +#include "prx/libSceAgcDriver/Execution/include/FrameRecorder.hpp" +#include "prx/libSceAgcDriver/Execution/include/GuestMemory.hpp" #include "prx/libSceAgcDriver/Execution/include/Screenshot.hpp" #include "prx/libkernel/Time/include/SleepWake.hpp" +#include #include namespace AgcDriver { void Submit(const Packet* packet, std::uint32_t queue) try { KernelNoteGpuSubmit_nid_no_patch(); - DriverDetail::Driver::Get().Submit(packet, queue); + auto& driver = DriverDetail::Driver::Get(); + auto* const recorder = FrameRecorder::Active(); + if (recorder == nullptr) { + driver.Submit(packet, queue); + return; + } + GuestMemory::CheckRange(packet, sizeof(Packet), alignof(Packet)); + recorder->Submit(*packet, queue); + const auto flips = driver.FlipSubmissions(); + driver.Submit(packet, queue); + if (driver.FlipSubmissions() != flips) recorder->FrameBoundary(); } catch (const ProcessShutdown&) { LibcAwaitExit_nid_postfix(); } @@ -23,18 +36,22 @@ void Shutdown() { } void RegisterShader(const Shader* shader) { + if (auto* const recorder = FrameRecorder::Active()) recorder->RegisterShader(shader); DriverDetail::Driver::Get().RegisterShader(shader); } void SuspendPoint() { + if (auto* const recorder = FrameRecorder::Active()) recorder->Suspend(); DriverDetail::Driver::Get().SuspendPoint(); } void RegisterVideoOutput(std::uint32_t handle, const std::shared_ptr& output) { + if (auto* const recorder = FrameRecorder::Active()) recorder->RegisterVideoOutput(handle); DriverDetail::Driver::Get().RegisterVideoOutput(handle, output); } void UnregisterVideoOutput(std::uint32_t handle, const std::shared_ptr& output) { + if (auto* const recorder = FrameRecorder::Active()) recorder->UnregisterVideoOutput(handle); DriverDetail::Driver::Get().UnregisterVideoOutput(handle, output); } @@ -47,6 +64,7 @@ void PresentClear(const PresentationWindow& window, bool opaque, void (*gpuReady } void PresentBuffer(const PresentationWindow& window, const DisplayBuffer& buffer, void (*gpuReady)(void*), void* context) { + if (auto* const recorder = FrameRecorder::Active()) recorder->PresentBuffer(buffer); DriverDetail::Driver::Get().Present(window, &buffer, true, gpuReady, context); } @@ -104,11 +122,33 @@ extern "C" void AgcDriverSuspendPoint_nid_postfix() try { } extern "C" std::uint32_t AgcDriverReserveEopFlip_nid_postfix(const AgcDriver::FlipInfo& info) { - return AgcDriver::DriverDetail::Driver::Get().ReserveEopFlip(info); + const auto token = AgcDriver::DriverDetail::Driver::Get().ReserveEopFlip(info); + if (auto* const recorder = AgcDriver::FrameRecorder::Active(); recorder != nullptr && token != 0) recorder->ReserveEopFlip(info, token); + return token; +} + +extern "C" std::uint32_t AgcDriverReserveEopFlipAt_nid_postfix(const AgcDriver::FlipInfo& info, std::uint32_t token) { + return AgcDriver::DriverDetail::Driver::Get().ReserveEopFlipAt(info, token); +} + +extern "C" std::size_t AgcDriverAwaitedWaits_nid_postfix(AgcDriver::AwaitedWait* waits, std::size_t capacity) { + const auto awaited = AgcDriver::DriverDetail::Driver::Get().AwaitedWaits(); + const auto count = std::min(capacity, awaited.size()); + std::copy_n(awaited.begin(), count, waits); + return count; +} + +extern "C" void AgcDriverSerialProgress_nid_postfix(std::uint64_t* accepted, std::uint64_t* completed) { + const auto [submitted, done] = AgcDriver::DriverDetail::Driver::Get().SerialProgress(); + *accepted = submitted; + *completed = done; } extern "C" void AgcDriverSubmitFlip_nid_postfix(const AgcDriver::FlipInfo& info) try { + auto* const recorder = AgcDriver::FrameRecorder::Active(); + if (recorder != nullptr) recorder->Flip(info); AgcDriver::DriverDetail::Driver::Get().SubmitFlip(info); + if (recorder != nullptr) recorder->FrameBoundary(); } catch (const ProcessShutdown&) { LibcAwaitExit_nid_postfix(); } @@ -146,17 +186,20 @@ extern "C" void AgcDriverReportFailure_nid_postfix(std::exception_ptr error) { } extern "C" void AgcDriverResolveShaderAbi_nid_postfix(const Shader* shader, std::span context, std::span primitive) try { + if (auto* const recorder = AgcDriver::FrameRecorder::Active()) recorder->ResolveShaderAbi(shader, context, primitive); AgcDriver::DriverDetail::Driver::Get().ResolveShaderAbi(shader, context, primitive); } catch (const ProcessShutdown&) { LibcAwaitExit_nid_postfix(); } extern "C" void AgcDriverResolveGraphicsAbi_nid_postfix(const Shader* vertex, const Shader* pixel, std::uint32_t primitiveType) try { + if (auto* const recorder = AgcDriver::FrameRecorder::Active()) recorder->ResolveGraphicsAbi(vertex, pixel, primitiveType); AgcDriver::DriverDetail::Driver::Get().ResolveGraphicsAbi(vertex, pixel, primitiveType); } catch (const ProcessShutdown&) { LibcAwaitExit_nid_postfix(); } extern "C" void AgcDriverResolveGraphicsStagesAbi_nid_postfix(std::span stages, std::span context, std::span primitive) try { + if (auto* const recorder = AgcDriver::FrameRecorder::Active()) recorder->ResolveGraphicsStagesAbi(stages, context, primitive); AgcDriver::DriverDetail::Driver::Get().ResolveGraphicsStagesAbi(stages, context, primitive); } catch (const ProcessShutdown&) { LibcAwaitExit_nid_postfix(); diff --git a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Lifecycle.cpp b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Lifecycle.cpp index b138f1b720..ba19bb4a60 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Lifecycle.cpp +++ b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Lifecycle.cpp @@ -247,6 +247,40 @@ void Driver::WaitIdle() { if (const auto localDevice = device.Load()) localDevice->WaitIdle(); } +bool Driver::WaitIdleFor(std::chrono::milliseconds timeout) { + require(!onWorkerThread(), "worker cannot wait for itself"); + std::unique_lock lock(mutex); + const auto target = accepted; + ++idleWaiters; + const bool idle = changed.wait_for(lock, timeout, [&] { return failure != nullptr || stopping || completed >= target; }); + --idleWaiters; + rethrowFailure(); + checkStopping(); + return idle; +} + +std::vector Driver::AwaitedWaits() { + std::vector result; + for (const auto& slot : awaitedSlots) { + const auto address = slot.address.load(std::memory_order_acquire); + if (address == 0) continue; + AwaitedWait wait; + wait.address = address; + wait.reference = slot.reference.load(std::memory_order_relaxed); + wait.mask = slot.mask.load(std::memory_order_relaxed); + const auto shape = slot.shape.load(std::memory_order_relaxed); + wait.function = static_cast(shape); + wait.bytes = static_cast(shape >> 32u); + if (slot.address.load(std::memory_order_acquire) == address) result.push_back(wait); + } + return result; +} + +std::pair Driver::SerialProgress() { + std::lock_guard lock(mutex); + return {accepted, completed}; +} + void Driver::CheckFailure() { if (failed.load(std::memory_order_acquire)) { std::lock_guard lock(mutex); diff --git a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Packets/PacketExecution.cpp b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Packets/PacketExecution.cpp index 08b69be4d4..6328766099 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Packets/PacketExecution.cpp +++ b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Packets/PacketExecution.cpp @@ -475,7 +475,7 @@ void Driver::execute(const Submission& submission) { try { std::string rejected; - const auto verdict = draw(queue, packet, submission, rejected); + const auto verdict = draw(queue, packet, submission, cursor, rejected); drawn = verdict == DrawVerdict::Drawn; CaptureTrace::Log("draw submission=%llu queue=%x offset=%zu target=%llx mask=%x verdict=%d reason=%.256s", static_cast(submission.serial), submission.queue, cursor, static_cast(color), readRegister(queue.context, 0x8e), static_cast(verdict), rejected.c_str()); if (verdict == DrawVerdict::Rejected) { @@ -569,8 +569,8 @@ void Driver::execute(const Submission& submission) { return; } if ((std::atomic_ref(*const_cast(submission.rewindTail - 1)).load(std::memory_order_acquire) & 0x80000000u) != 0) { - DrawPipeline::Queue0().Enqueue([this] { - submitOpenWork(); + DrawPipeline::Queue0().Enqueue([this, progress = submission.progress] { + submitOpenWork(progress); wakeQueueZeroForCompletions(); }, {}); endTiming.Finish(); @@ -594,7 +594,10 @@ void Driver::execute(const Submission& submission) { if (localDevice != nullptr) localDevice->SubmitRecorded(submission.queue == 0); } endTiming.Finish(); - if (submission.rewindTail != nullptr) executeRewindTail(submission); + if (submission.rewindTail != nullptr) { + if (submission.progress) submitOpenWork(submission.progress); + executeRewindTail(submission); + } } } diff --git a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Presentation.cpp b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Presentation.cpp index d071d049fc..a2fa486528 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Presentation.cpp +++ b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Presentation.cpp @@ -12,19 +12,35 @@ namespace AgcDriver::DriverDetail { std::uint32_t Driver::ReserveEopFlip(const FlipInfo& info) { std::lock_guard lock(mutex); + require(nextEopToken <= EopFlipTokenLimit, "EOP flip token space exhausted"); + return reserveEopFlip(info, nextEopToken); +} + +std::uint32_t Driver::ReserveEopFlipAt(const FlipInfo& info, std::uint32_t token) { + std::lock_guard lock(mutex); + return reserveEopFlip(info, token); +} + +std::uint32_t Driver::reserveEopFlip(const FlipInfo& info, std::uint32_t token) { rethrowFailure(); checkStopping(); - require(nextEopToken <= EopFlipTokenLimit, "EOP flip token space exhausted"); + require(token >= EopFlipTokenBase && token <= EopFlipTokenLimit && !eopFlips.contains(token), "EOP flip token is not free"); const auto output = outputs.find(info.handle); require(output != outputs.end(), "EOP flip references an unregistered video output"); auto request = output->second->TryReserve(info); if (!request) return 0; - const auto token = nextEopToken; eopFlips.emplace(token, EopFlip{info.handle, std::move(request)}); - ++nextEopToken; + nextEopToken = std::max(nextEopToken, token + 1); return token; } +std::vector Driver::PendingEopFlipTokens() { + std::lock_guard lock(mutex); + std::vector tokens; + for (const auto& [token, flip] : eopFlips) tokens.push_back(token); + return tokens; +} + void Driver::RegisterVideoOutput(std::uint32_t handle, const std::shared_ptr& output) { require(output != nullptr, "null video output"); std::lock_guard lock(mutex); diff --git a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Queues/QueueWorker.cpp b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Queues/QueueWorker.cpp index ed1cb37640..546be83644 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Queues/QueueWorker.cpp +++ b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Queues/QueueWorker.cpp @@ -70,19 +70,23 @@ void Driver::deferCompletion(const Submission& submission) { queue0Uncommitted.push_back(received); } completionDeferred() = true; - DrawPipeline::Queue0().Enqueue([this, serial = submission.serial, received, labelWrites = submission.labelWrites] { - completeSubmission(serial, received, labelWrites); + DrawPipeline::Queue0().Enqueue([this, serial = submission.serial, received, labelWrites = submission.labelWrites, progress = submission.progress] { + completeSubmission(serial, received, labelWrites, progress); }, {}); } -void Driver::submitOpenWork() { - if (deferredLabels().labels.empty() && !Graphics::Recorder::PendingLabelSince().has_value() && Graphics::Recorder::RecordedWorkSinceSubmit() == 0) return; +void Driver::submitOpenWork(const std::shared_ptr& progress) { + const bool work = !deferredLabels().labels.empty() || Graphics::Recorder::PendingLabelSince().has_value() || Graphics::Recorder::RecordedWorkSinceSubmit() != 0; + if (!work && !progress) return; ++submissionCosts(0).endSubmits; GuestMemory::TagGpuLockSite(GuestMemory::GpuLockSite::End); std::lock_guard gpuLock(GuestMemory::GpuMutex()); const auto localDevice = device.Load(); - recordDeferredLabels(localDevice.get(), 0); - if (localDevice != nullptr) localDevice->SubmitRecorded(true); + if (work) { + recordDeferredLabels(localDevice.get(), 0); + if (localDevice != nullptr) localDevice->SubmitRecorded(true); + } + if (progress) progress->Publish(localDevice, localDevice ? localDevice->SubmittedEpoch() : 0); } void Driver::wakeQueueZeroForCompletions() { @@ -91,7 +95,7 @@ void Driver::wakeQueueZeroForCompletions() { changed.notify_all(); } -void Driver::completeSubmission(std::uint64_t serial, std::uint64_t received, std::span labelWrites) { +void Driver::completeSubmission(std::uint64_t serial, std::uint64_t received, std::span labelWrites, const std::shared_ptr& progress) { struct Complete { Driver& driver; std::uint64_t serial, received; @@ -109,7 +113,7 @@ void Driver::completeSubmission(std::uint64_t serial, std::uint64_t received, st if (notify) driver.changed.notify_all(); } } complete{*this, serial, received, labelWrites}; - submitOpenWork(); + submitOpenWork(progress); } const std::atomic*& Driver::workerQueued() { @@ -261,6 +265,7 @@ void Driver::run(std::uint32_t id) noexcept { if (profile && submission.enqueuedAt != std::chrono::steady_clock::time_point{}) costs.dequeueNs += static_cast(std::chrono::duration_cast(std::chrono::steady_clock::now() - submission.enqueuedAt).count()); } execute(submission); + if (submission.progress && submission.rewindTail == nullptr && !completionDeferred()) submitOpenWork(submission.progress); if (traceGpu) std::fprintf(stderr, "[gpu] %.1f done serial=%llu queue=0x%x\n", TraceMs(), static_cast(submission.serial), id); const auto completeStart = profile ? std::chrono::steady_clock::now() : std::chrono::steady_clock::time_point{}; PerformanceContext completionContext(id == 0 ? frameTiming() : FrameTiming::Async()); diff --git a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Queues/Submission.cpp b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Queues/Submission.cpp index 5249610775..eeaea3e126 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Queues/Submission.cpp +++ b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Queues/Submission.cpp @@ -1,3 +1,4 @@ +#include "prx/libSceAgcDriver/Execution/include/ProfileOutput.hpp" #include "prx/libSceAgcDriver/Execution/include/PerformanceTimer.hpp" #include "prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp" #include "prx/libSceAgcDriver/Execution/include/Driver/Queues/Submission.hpp" @@ -21,6 +22,61 @@ namespace AgcDriver::DriverDetail { namespace { +bool& submitsQueue0() { + struct SubmitsQueue0Storage {}; + return HostThreadLocal(); +} + +std::uint64_t& flipSubmissionsOfThread() { + struct FlipSubmissionsStorage {}; + return HostThreadLocal(); +} + +bool progressEligible(const Submission& submission) { + if (submission.queue != 0 || !submission.flips.empty() + || !submission.renderingWaits.empty() || !submission.conditionalEnds.empty()) return false; + bool terminalRelease = false; + std::optional knownWrite; + for (std::size_t at = 0; at < submission.commands.size();) { + const auto packet = std::span(submission.commands).subspan(at); + const auto count = Pm4::PacketWords(packet[0]); + if (count == 0 || count > packet.size()) return false; + const auto opcode = (packet[0] >> 8u) & 0xffu; + const bool type3 = (packet[0] >> 30u) == 3; + if (type3 && (opcode == 0x3c || opcode == 0x93)) { + if (!knownWrite || Pm4::Predicated(packet[0]) || ((packet[1] >> 4u) & 3u) != 1u) return false; + const auto wait = Pm4::DecodeWait(packet.first(count)); + const auto bytes = knownWrite->Bytes(); + if (wait.address < knownWrite->address) return false; + const auto offset = wait.address - knownWrite->address; + if (offset > bytes.size() || wait.bytes > bytes.size() - offset) return false; + std::uint64_t value = 0; + std::memcpy(&value, bytes.data() + offset, wait.bytes); + if (!Pm4::WaitComparesValue(packet.first(count), value)) return false; + terminalRelease = false; + at += count; + continue; + } + if (type3 && opcode == 0x59 && submission.rewindTail != nullptr && at + count == submission.commands.size()) { + at += count; + continue; + } + if (!Pm4::FillerPacket(packet[0]) && !(type3 && opcode == 0x10) && !Pm4::IsTagMarker(packet.first(count))) { + if (type3 && (opcode == 0x49 || opcode == 0x37)) { + const auto write = Pm4::DecodeLabelWrite(packet.first(count)); + terminalRelease = opcode == 0x49 && write.has_value(); + if (write && !Pm4::Predicated(packet[0]) && (opcode == 0x37 || (packet[2] >> 29u) < 3)) knownWrite = *write; + else if (opcode != 0x49 || (packet[2] >> 29u) != 0) knownWrite.reset(); + } else { + terminalRelease = false; + if (!type3 || (opcode != 0x42 && opcode != 0x58)) knownWrite.reset(); + } + } + at += count; + } + return terminalRelease; +} + std::uint64_t captureTime() { return static_cast(std::llround(TraceMs() * 1000.0)); } @@ -38,11 +94,10 @@ bool EopRelease(std::span words) { return words.size() >= 8 && (words[0] >> 30u) == 3u && Pm4::PacketWords(words[0]) == 8u && ((words[0] >> 8u) & 0xffu) == 0x49u && ((words[2] >> 24u) & 7u) == 2u; } -bool& submitsQueue0() { - struct SubmitsQueue0Storage {}; - return HostThreadLocal(); } +std::uint64_t Driver::FlipSubmissions() const { + return flipSubmissionsOfThread(); } std::deque& Driver::releasedTails() { @@ -367,6 +422,10 @@ void Driver::Submit(const Packet* packet, std::uint32_t queue, bool cpuFlip) { const auto validated = profile ? std::chrono::steady_clock::now() : start; auto* const capture = SubmissionCapture::Active(); auto captured = capture != nullptr ? SubmissionCapture::Prepare(queue, SubmissionKind::Submit, submission.commands, submission.conditionalEnds) : CapturedSubmission{}; + static const bool progressEnabled = std::getenv("APS5_SUBMIT_PROGRESS") != nullptr; + const auto progress = progressEnabled && progressEligible(submission) ? std::make_shared() : nullptr; + submission.progress = progress; + const auto progressDeadline = progress ? std::chrono::steady_clock::now() + std::chrono::milliseconds(2) : std::chrono::steady_clock::time_point{}; { std::unique_lock lock(mutex); rethrowFailure(); @@ -402,11 +461,31 @@ void Driver::Submit(const Packet* packet, std::uint32_t queue, bool cpuFlip) { submission.holdsFlip = true; flipsAhead.fetch_add(1, std::memory_order_acq_rel); } + if (!submission.flips.empty()) ++flipSubmissionsOfThread(); enqueue(std::move(submission)); ++accepted; } changed.notify_all(); if (capture != nullptr) writeCaptured(*this, *capture, captured); + if (progress) { + PerformanceTimer timing("Driver.SubmitProgress"); + const auto [localDevice, epoch] = progress->WaitUntil(progressDeadline); + const auto now = std::chrono::steady_clock::now(); + bool gpuReady = false; + if (localDevice && epoch != 0 && now < progressDeadline) { + const auto remaining = std::chrono::duration_cast(progressDeadline - now).count(); + gpuReady = localDevice->WaitRecordedFor(epoch, static_cast(remaining)); + } + if (profile) { + static std::atomic eligible{0}, ready{0}, expired{0}; + if (gpuReady) ready.fetch_add(1, std::memory_order_relaxed); + else expired.fetch_add(1, std::memory_order_relaxed); + const auto count = eligible.fetch_add(1, std::memory_order_relaxed) + 1; + if (count % 1000 == 0) AgcDriver::ProfilePrint_nid_no_patch("[submit-progress] %llu eligible terminal releases, %llu GPU-ready returns, %llu expired or unsupported\n", + static_cast(count), static_cast(ready.load()), static_cast(expired.load())); + } + CheckFailure(); + } } void Driver::SuspendPoint() { @@ -466,7 +545,18 @@ bool Driver::orderReleased(std::uint32_t queue, std::uint64_t received) const { return runningWorkers.load(std::memory_order_acquire) == 0 && !completionsPending() && !Graphics::Recorder::SnapshotWriteOverlaps(awaited, 4); } +void Driver::noteWaitFields(std::uint32_t queue, std::span packet) { + const auto fields = Pm4::DecodeWait(packet); + auto& slot = awaitedSlots[queue == 0 ? 0 : queue - 0x20 + 1]; + slot.reference.store(fields.reference, std::memory_order_relaxed); + slot.mask.store(fields.mask, std::memory_order_relaxed); + slot.shape.store(static_cast(fields.function) | static_cast(fields.bytes) << 32u, std::memory_order_relaxed); +} + void Driver::noteWaitBlocked(std::uint32_t queue, std::uint64_t awaited, bool blocked) { + auto& slot = awaitedSlots[queue == 0 ? 0 : queue - 0x20 + 1]; + if (!blocked) slot.shape.store(0, std::memory_order_relaxed); + slot.address.store(blocked ? awaited : 0, std::memory_order_release); if (blocked) { if (runningWorkers.fetch_sub(1, std::memory_order_acq_rel) == 1) { idleEpoch.fetch_add(1, std::memory_order_release); diff --git a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cpp b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cpp index 8dd6aabfa6..cccf175abf 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cpp +++ b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cpp @@ -1702,7 +1702,7 @@ void PrepareRegisteredInBackground(std::shared_ptr snapsho try { entries = PrepareRegistered(*plan); } catch (const std::exception& error) { - APS5_LOG_ERR("Shader 0x%llx could not be prepared after registration: %s", static_cast(snapshot->codeAddress), error.what()); + APS5_LOG_ERR("Shader 0x%llx snapshot=0x%llx could not be prepared after registration: %s", static_cast(snapshot->codeAddress), static_cast(reinterpret_cast(snapshot.get())), error.what()); failure = std::current_exception(); } catch (...) { failure = std::current_exception(); @@ -2111,6 +2111,37 @@ std::uint64_t NullPixelProgramAddress() { return reinterpret_cast(NullPixelCode); } +alignas(256) static const std::uint32_t DepthCopyMultisamplePixelCode[64] = {0x360604ffu, 0x0000ffffu, 0x2c080490u, 0x7e0a0208u, 0xf0000130u, 0x00000603u, 0xbf8c3f70u, 0xf8001801u, 0x00000006u, 0xbf810000u}; +alignas(256) static const std::uint32_t DepthCopySinglePixelCode[64] = {0x360604ffu, 0x0000ffffu, 0x2c080490u, 0xf0000108u, 0x00000603u, 0xbf8c3f70u, 0xf8001801u, 0x00000006u, 0xbf810000u}; +static const Shader DepthCopyMultisamplePixelShader = [] { + Shader shader{}; + shader.file_header = 0x34333231u; + shader.version = 0x18u; + shader.code = DepthCopyMultisamplePixelCode; + shader.header_size = sizeof(Shader); + shader.shader_size = sizeof(DepthCopyMultisamplePixelCode); + shader.type = 1; + return shader; +}(); +static const Shader DepthCopySinglePixelShader = [] { + Shader shader{}; + shader.file_header = 0x34333231u; + shader.version = 0x18u; + shader.code = DepthCopySinglePixelCode; + shader.header_size = sizeof(Shader); + shader.shader_size = sizeof(DepthCopySinglePixelCode); + shader.type = 1; + return shader; +}(); + +std::uint64_t DepthCopyPixelProgramAddress(bool multisampled) { + return reinterpret_cast(multisampled ? DepthCopyMultisamplePixelCode : DepthCopySinglePixelCode); +} + +std::uint32_t DepthCopyPixelUserWords(bool multisampled) { + return multisampled ? 9u : 8u; +} + void Driver::ResolveGraphicsAbi(const Shader* vertex, const Shader* pixel, std::uint32_t primitiveType) { BindTitleThread(); PerformanceContext timingContext(FrameTiming::Preparation()); @@ -2251,6 +2282,25 @@ ShaderSnapshot PrepareNullPixelProgram(const VulkanDevice& device) { return null; } +ShaderSnapshot PrepareDepthCopyPixelProgram(bool multisampled) { + const auto& shader = multisampled ? DepthCopyMultisamplePixelShader : DepthCopySinglePixelShader; + const auto* code = multisampled ? DepthCopyMultisamplePixelCode : DepthCopySinglePixelCode; + ShaderSnapshot copy{DepthCopyPixelProgramAddress(multisampled), reinterpret_cast(&shader), shader.type, {}, {}}; + copy.code.assign(code, code + 64); + copy.header.resize(sizeof(Shader)); + std::memcpy(copy.header.data(), &shader, sizeof(Shader)); + auto state = DecodeRegisteredState(copy); + state.shader.insert_or_assign(0x008u, static_cast(copy.codeAddress >> 8u)); + state.shader.insert_or_assign(0x009u, static_cast(copy.codeAddress >> 40u)); + state.shader.insert_or_assign(0x00bu, DepthCopyPixelUserWords(multisampled) << 1u); + state.context.insert_or_assign(0x1b3u, 0x8002u); + state.context.insert_or_assign(0x1b4u, 0x8002u); + state.context.insert_or_assign(0x1c5u, 1u); + copy.registeredState = std::make_shared(std::move(state)); + copy.prepared->deferred = true; + return copy; +} + std::shared_ptr Driver::RegisteredShaders() { std::lock_guard lock(mutex); return shaders; @@ -2304,6 +2354,18 @@ void Driver::RegisterShader(const Shader* shader) { localDevice = device; } snapshot.registeredState = std::make_shared(DecodeRegisteredState(snapshot)); + { + std::lock_guard lock(mutex); + if (shaders != nullptr) { + if (const auto found = shaders->find(snapshot.codeAddress); found != shaders->end()) { + for (const auto& current : found->second) { + if (current->registeredState == nullptr || current->type != snapshot.type || current->code != snapshot.code || *current->registeredState != *snapshot.registeredState) continue; + snapshot.registeredState = current->registeredState; + break; + } + } + } + } QueueState registered{}; registered.shader = snapshot.registeredState->shader; registered.context = snapshot.registeredState->context; @@ -2347,6 +2409,9 @@ void Driver::RegisterShader(const Shader* shader) { if (shaders->find(NullPixelProgramAddress()) == shaders->end()) { PublishRegisteredShader(shaders, std::make_shared(PrepareNullPixelProgram(*localDevice))); } + for (const bool multisampled : {false, true}) { + if (shaders->find(DepthCopyPixelProgramAddress(multisampled)) == shaders->end()) PublishRegisteredShader(shaders, std::make_shared(PrepareDepthCopyPixelProgram(multisampled))); + } transaction.Commit(); if (preparation != nullptr) PrepareRegisteredInBackground(published, std::move(preparation)); } diff --git a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Synchronization/WaitMemory.cpp b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Synchronization/WaitMemory.cpp index 6aead1a2cc..f1b7df1757 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Synchronization/WaitMemory.cpp +++ b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Synchronization/WaitMemory.cpp @@ -322,6 +322,7 @@ void Driver::waitMemory(std::span packet, std::uint32_t que if (entryTryFailed && pollService(false)) return; + noteWaitFields(queue, packet); noteWaitBlocked(queue, awaited, true); struct BlockedWait { Driver& driver; diff --git a/core/libs/prx/libSceAgcDriver/Execution/src/FrameCapture.cpp b/core/libs/prx/libSceAgcDriver/Execution/src/FrameCapture.cpp new file mode 100644 index 0000000000..7505860321 --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/Execution/src/FrameCapture.cpp @@ -0,0 +1,455 @@ +#include "prx/libSceAgcDriver/Execution/include/FrameCapture.hpp" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace AgcDriver { +namespace { + +static_assert(std::endian::native == std::endian::little, "frame captures are stored in little-endian order"); + +constexpr std::size_t ChunkHeaderBytes = 64; + +void requireCapture(bool condition, const std::string& reason) { + if (!condition) throw std::runtime_error("frame capture: " + reason); +} + +class Encoder { +public: + template + void Put(TValue value) { + const auto at = bytes.size(); + bytes.resize(at + sizeof(TValue)); + std::memcpy(bytes.data() + at, &value, sizeof(TValue)); + } + + std::vector bytes; +}; + +class Decoder { +public: + explicit Decoder(std::span bytes) : bytes(bytes) {} + + template + TValue Take(const char* what) { + requireCapture(sizeof(TValue) <= bytes.size() - at, std::string("record ends inside ") + what); + TValue value{}; + std::memcpy(&value, bytes.data() + at, sizeof(TValue)); + at += sizeof(TValue); + return value; + } + + std::size_t Count(std::size_t elementBytes, const char* what) { + const auto count = Take(what); + requireCapture(count <= (bytes.size() - at) / elementBytes, std::string("count of ") + what + " exceeds its record"); + return count; + } + + bool Done() const { return at == bytes.size(); } + +private: + std::span bytes; + std::size_t at = 0; +}; + +void putRegisters(Encoder& encoder, const std::vector& registers) { + encoder.Put(static_cast(registers.size())); + for (const auto& value : registers) { + encoder.Put(value.offset); + encoder.Put(value.value); + } +} + +std::vector takeRegisters(Decoder& decoder) { + std::vector registers(decoder.Count(8, "registers")); + for (auto& value : registers) { + value.offset = decoder.Take("register offset"); + value.value = decoder.Take("register value"); + } + return registers; +} + +std::vector encodeCall(const FrameCall& call) { + Encoder encoder; + encoder.Put(call.ordinal); + encoder.Put(call.timeNs); + encoder.Put(call.thread); + encoder.Put(call.accepted); + encoder.Put(call.completed); + encoder.Put(call.queue); + encoder.Put(call.flags); + encoder.Put(static_cast(call.cpuFlip ? 1u : 0u)); + encoder.Put(call.address); + encoder.Put(call.size); + encoder.Put(static_cast(call.values.size())); + for (const auto value : call.values) encoder.Put(value); + putRegisters(encoder, call.context); + putRegisters(encoder, call.primitive); + return std::move(encoder.bytes); +} + +FrameCall decodeCall(FrameCallKind kind, std::span payload) { + Decoder decoder(payload); + FrameCall call; + call.kind = kind; + call.ordinal = decoder.Take("ordinal"); + call.timeNs = decoder.Take("time"); + call.thread = decoder.Take("thread"); + call.accepted = decoder.Take("accepted serial"); + call.completed = decoder.Take("completed serial"); + call.queue = decoder.Take("queue"); + call.flags = decoder.Take("flags"); + const auto cpuFlip = decoder.Take("cpu flip"); + requireCapture(cpuFlip <= 1, "cpu flip is not 0 or 1"); + call.cpuFlip = cpuFlip == 1; + call.address = decoder.Take("address"); + call.size = decoder.Take("size"); + call.values.resize(decoder.Count(8, "values")); + for (auto& value : call.values) value = decoder.Take("value"); + call.context = takeRegisters(decoder); + call.primitive = takeRegisters(decoder); + requireCapture(decoder.Done(), "call record has trailing bytes"); + return call; +} + +bool knownCall(std::uint32_t kind) { + return kind >= static_cast(FrameCallKind::RegisterShader) && kind <= static_cast(FrameCallKind::UnregisterVideoOutput); +} + +std::vector readFile(const std::filesystem::path& path) { + std::ifstream file(path, std::ios::binary); + requireCapture(file.is_open(), "cannot open " + path.string()); + std::vector bytes; + file.seekg(0, std::ios::end); + bytes.resize(static_cast(file.tellg())); + file.seekg(0); + file.read(reinterpret_cast(bytes.data()), static_cast(bytes.size())); + requireCapture(file.good() || bytes.empty(), "cannot read " + path.string()); + return bytes; +} + +void requireMagic(std::span bytes, std::string_view magic, const std::filesystem::path& path) { + requireCapture(bytes.size() >= magic.size() && std::memcmp(bytes.data(), magic.data(), magic.size()) == 0, path.string() + " is not a frame capture file"); +} + +struct Run { + std::uint64_t address; + std::span bytes; +}; + +} + +bool AllZero(std::span bytes) { + constexpr std::size_t Word = sizeof(std::uint64_t); + std::size_t at = 0; + for (; at + Word <= bytes.size(); at += Word) { + std::uint64_t value = 0; + std::memcpy(&value, bytes.data() + at, Word); + if (value != 0) return false; + } + return std::all_of(bytes.begin() + static_cast(at), bytes.end(), [](std::byte value) { return value == std::byte{0}; }); +} + +std::string_view FrameCallName(FrameCallKind kind) { + switch (kind) { + case FrameCallKind::RegisterShader: return "register-shader"; + case FrameCallKind::ResolveShaderAbi: return "resolve-shader-abi"; + case FrameCallKind::ResolveGraphicsAbi: return "resolve-graphics-abi"; + case FrameCallKind::ResolveGraphicsStagesAbi: return "resolve-graphics-stages-abi"; + case FrameCallKind::RegisterVideoOutput: return "register-video-output"; + case FrameCallKind::UnregisterVideoOutput: return "unregister-video-output"; + case FrameCallKind::SetTessellationFactorRing: return "set-tf-ring"; + case FrameCallKind::Submit: return "submit"; + case FrameCallKind::Flip: return "flip"; + case FrameCallKind::ReserveEopFlip: return "reserve-eop-flip"; + case FrameCallKind::Suspend: return "suspend"; + case FrameCallKind::Memory: return "memory"; + case FrameCallKind::FrameStart: return "frame-start"; + case FrameCallKind::End: return "end"; + case FrameCallKind::DisplayBuffer: return "display-buffer"; + } + throw std::runtime_error("frame capture: unknown call kind " + std::to_string(static_cast(kind))); +} + +FrameCaptureWriter::FrameCaptureWriter(const std::filesystem::path& directory, int level) : directory(directory), level(level) { + std::error_code error; + std::filesystem::create_directories(directory, error); + requireCapture(!error && std::filesystem::is_directory(directory), "cannot create the directory " + directory.string()); + for (const auto name : {CallsName, MemoryName, HeaderName}) std::filesystem::remove(directory / name, error); + calls.open(directory / CallsName, std::ios::binary | std::ios::trunc); + memory.open(directory / MemoryName, std::ios::binary | std::ios::trunc); + requireCapture(calls.is_open() && memory.is_open(), "cannot create the capture files in " + directory.string()); + calls.write(CallsMagic.data(), static_cast(CallsMagic.size())); + memory.write(MemoryMagic.data(), static_cast(MemoryMagic.size())); + requireCapture(calls.good() && memory.good(), "cannot write the capture files in " + directory.string()); +} + +void FrameCaptureWriter::WriteCall(const FrameCall& call) { + FrameCallName(call.kind); + const auto payload = encodeCall(call); + requireCapture(payload.size() <= UINT32_MAX, "call record too large"); + std::lock_guard lock(mutex); + requireCapture(!finished, "call written after the capture finished"); + const auto kind = static_cast(call.kind); + const auto bytes = static_cast(payload.size()); + calls.write(reinterpret_cast(&kind), sizeof(kind)); + calls.write(reinterpret_cast(&bytes), sizeof(bytes)); + calls.write(reinterpret_cast(payload.data()), static_cast(payload.size())); + requireCapture(calls.good(), "cannot write " + (directory / CallsName).string()); +} + +void FrameCaptureWriter::WriteRange(std::uint64_t address, std::uint64_t bytes, FrameRangeKind kind, std::uint32_t protection, std::uint32_t access) { + FrameMemoryChunk chunk; + chunk.kind = FrameMemoryKind::Range; + chunk.rangeKind = kind; + chunk.protection = protection; + chunk.access = access; + chunk.address = address; + chunk.bytes = bytes; + std::lock_guard lock(mutex); + writeChunk(chunk, {}); +} + +std::uint64_t FrameCaptureWriter::WriteSnapshot(std::uint64_t address, std::span bytes) { + std::vector runs; + for (std::size_t at = 0; at < bytes.size();) { + const auto block = bytes.subspan(at, std::min(BlockBytes, bytes.size() - at)); + if (AllZero(block)) { + at += block.size(); + continue; + } + if (!runs.empty() && runs.back().address + runs.back().bytes.size() == address + at && runs.back().bytes.size() + block.size() <= ChunkBytes) { + runs.back().bytes = bytes.subspan(runs.back().address - address, runs.back().bytes.size() + block.size()); + } else { + runs.push_back({address + at, block}); + } + at += block.size(); + } + const auto workers = std::max(1, std::thread::hardware_concurrency() / 2); + std::uint64_t stored = 0; + for (std::size_t first = 0; first < runs.size(); first += workers) { + const auto last = std::min(runs.size(), first + workers); + std::vector>> compressed; + for (auto index = first; index < last; ++index) { + compressed.push_back(std::async(std::launch::async, [this, run = runs[index]] { return compress(run.bytes); })); + } + std::lock_guard lock(mutex); + for (auto index = first; index < last; ++index) { + const auto payload = compressed[index - first].get(); + FrameMemoryChunk chunk; + chunk.kind = FrameMemoryKind::Snapshot; + chunk.address = runs[index].address; + chunk.bytes = runs[index].bytes.size(); + chunk.compressedBytes = payload.size(); + writeChunk(chunk, payload); + stored += chunk.bytes; + } + } + return stored; +} + +void FrameCaptureWriter::WriteDelta(std::uint64_t group, std::uint64_t completed, std::uint64_t address, std::span bytes) { + for (std::size_t at = 0; at < bytes.size(); at += ChunkBytes) { + const auto part = bytes.subspan(at, std::min(ChunkBytes, bytes.size() - at)); + const auto payload = compress(part); + FrameMemoryChunk chunk; + chunk.kind = FrameMemoryKind::Delta; + chunk.group = group; + chunk.completed = completed; + chunk.address = address + at; + chunk.bytes = part.size(); + chunk.compressedBytes = payload.size(); + std::lock_guard lock(mutex); + writeChunk(chunk, payload); + } +} + +void FrameCaptureWriter::WriteReference(std::uint64_t index, std::uint64_t address, std::span pixels) { + const auto payload = compress(pixels); + FrameMemoryChunk chunk; + chunk.kind = FrameMemoryKind::Reference; + chunk.group = index; + chunk.address = address; + chunk.bytes = pixels.size(); + chunk.compressedBytes = payload.size(); + std::lock_guard lock(mutex); + writeChunk(chunk, payload); +} + +void FrameCaptureWriter::Finish(std::string_view header) { + std::lock_guard lock(mutex); + requireCapture(!finished, "capture finished twice"); + finished = true; + calls.flush(); + memory.flush(); + requireCapture(calls.good() && memory.good(), "cannot flush the capture files in " + directory.string()); + calls.close(); + memory.close(); + std::ofstream file(directory / HeaderName, std::ios::binary | std::ios::trunc); + file.write(header.data(), static_cast(header.size())); + file.flush(); + requireCapture(file.good(), "cannot write " + (directory / HeaderName).string()); +} + +std::uint64_t FrameCaptureWriter::RawBytes() const { + std::lock_guard lock(mutex); + return rawBytes; +} + +std::uint64_t FrameCaptureWriter::StoredBytes() const { + std::lock_guard lock(mutex); + return storedBytes; +} + +const std::filesystem::path& FrameCaptureWriter::Directory() const { + return directory; +} + +void FrameCaptureWriter::writeChunk(const FrameMemoryChunk& chunk, std::span payload) { + requireCapture(!finished, "memory written after the capture finished"); + Encoder encoder; + encoder.Put(static_cast(chunk.kind)); + encoder.Put(static_cast(chunk.rangeKind)); + encoder.Put(chunk.protection); + encoder.Put(chunk.access); + encoder.Put(chunk.group); + encoder.Put(chunk.completed); + encoder.Put(chunk.address); + encoder.Put(chunk.bytes); + encoder.Put(static_cast(payload.size())); + encoder.Put(std::uint64_t{0}); + memory.write(reinterpret_cast(encoder.bytes.data()), static_cast(encoder.bytes.size())); + memory.write(reinterpret_cast(payload.data()), static_cast(payload.size())); + requireCapture(memory.good(), "cannot write " + (directory / MemoryName).string()); + if (chunk.kind != FrameMemoryKind::Range) rawBytes += chunk.bytes; + storedBytes += payload.size(); +} + +std::vector FrameCaptureWriter::compress(std::span bytes) const { + std::vector output(ZSTD_compressBound(bytes.size())); + const auto written = ZSTD_compress(output.data(), output.size(), bytes.data(), bytes.size(), level); + requireCapture(ZSTD_isError(written) == 0, std::string("zstd: ") + ZSTD_getErrorName(written)); + output.resize(written); + return output; +} + +FrameCaptureReader::FrameCaptureReader(const std::filesystem::path& directory) : directory(directory) { + const auto callsPath = directory / FrameCaptureWriter::CallsName; + const auto callBytes = readFile(callsPath); + requireMagic(callBytes, FrameCaptureWriter::CallsMagic, callsPath); + std::size_t at = FrameCaptureWriter::CallsMagic.size(); + while (at < callBytes.size()) { + if (callBytes.size() - at < 8) { + truncated = true; + break; + } + std::uint32_t kind = 0; + std::uint32_t bytes = 0; + std::memcpy(&kind, callBytes.data() + at, 4); + std::memcpy(&bytes, callBytes.data() + at + 4, 4); + requireCapture(knownCall(kind), callsPath.string() + ": unknown call kind " + std::to_string(kind) + " at byte " + std::to_string(at)); + if (callBytes.size() - at - 8 < bytes) { + truncated = true; + break; + } + calls.push_back(decodeCall(static_cast(kind), std::span(callBytes).subspan(at + 8, bytes))); + at += 8 + static_cast(bytes); + } + + const auto memoryPath = directory / FrameCaptureWriter::MemoryName; + std::ifstream memory(memoryPath, std::ios::binary); + requireCapture(memory.is_open(), "cannot open " + memoryPath.string()); + memory.seekg(0, std::ios::end); + const auto size = static_cast(memory.tellg()); + std::string magic(FrameCaptureWriter::MemoryMagic.size(), '\0'); + memory.seekg(0); + memory.read(magic.data(), static_cast(magic.size())); + requireCapture(memory.good() && magic == FrameCaptureWriter::MemoryMagic, memoryPath.string() + " is not a frame capture file"); + std::uint64_t offset = magic.size(); + while (offset < size) { + if (size - offset < ChunkHeaderBytes) { + truncated = true; + break; + } + std::array raw{}; + memory.seekg(static_cast(offset)); + memory.read(reinterpret_cast(raw.data()), static_cast(raw.size())); + requireCapture(memory.good(), "cannot read " + memoryPath.string()); + Decoder decoder(raw); + FrameMemoryChunk chunk; + const auto kind = decoder.Take("memory kind"); + const auto rangeKind = decoder.Take("range kind"); + requireCapture(kind >= 1 && kind <= 4 && rangeKind >= 1 && rangeKind <= 6, memoryPath.string() + ": corrupt chunk header at byte " + std::to_string(offset)); + chunk.kind = static_cast(kind); + chunk.rangeKind = static_cast(rangeKind); + chunk.protection = decoder.Take("protection"); + chunk.access = decoder.Take("access"); + chunk.group = decoder.Take("group"); + chunk.completed = decoder.Take("completed serial"); + chunk.address = decoder.Take("address"); + chunk.bytes = decoder.Take("bytes"); + chunk.compressedBytes = decoder.Take("compressed bytes"); + requireCapture((chunk.kind == FrameMemoryKind::Range) == (chunk.compressedBytes == 0), memoryPath.string() + ": chunk payload does not match its kind at byte " + std::to_string(offset)); + chunk.offset = offset + ChunkHeaderBytes; + if (size - chunk.offset < chunk.compressedBytes) { + truncated = true; + break; + } + chunks.push_back(chunk); + offset = chunk.offset + chunk.compressedBytes; + } + + const auto headerPath = directory / FrameCaptureWriter::HeaderName; + if (std::filesystem::exists(headerPath)) { + const auto bytes = readFile(headerPath); + header.assign(reinterpret_cast(bytes.data()), bytes.size()); + } else { + truncated = true; + } +} + +const std::vector& FrameCaptureReader::Calls() const { + return calls; +} + +const std::vector& FrameCaptureReader::Chunks() const { + return chunks; +} + +const std::string& FrameCaptureReader::Header() const { + return header; +} + +bool FrameCaptureReader::Truncated() const { + return truncated; +} + +void FrameCaptureReader::Read(const FrameMemoryChunk& chunk, std::span destination) const { + requireCapture(chunk.kind != FrameMemoryKind::Range, "a range chunk has no bytes"); + requireCapture(destination.size() == chunk.bytes, "destination size does not match the chunk"); + const auto path = directory / FrameCaptureWriter::MemoryName; + std::ifstream memory(path, std::ios::binary); + requireCapture(memory.is_open(), "cannot open " + path.string()); + std::vector compressed(chunk.compressedBytes); + memory.seekg(static_cast(chunk.offset)); + memory.read(reinterpret_cast(compressed.data()), static_cast(compressed.size())); + requireCapture(memory.good(), "cannot read " + path.string()); + const auto written = ZSTD_decompress(destination.data(), destination.size(), compressed.data(), compressed.size()); + requireCapture(ZSTD_isError(written) == 0, std::string("zstd: ") + ZSTD_getErrorName(written)); + requireCapture(written == chunk.bytes, "chunk at " + std::to_string(chunk.address) + " decompressed to the wrong size"); +} + +std::vector FrameCaptureReader::Read(const FrameMemoryChunk& chunk) const { + std::vector bytes(chunk.bytes); + Read(chunk, bytes); + return bytes; +} + +} diff --git a/core/libs/prx/libSceAgcDriver/Execution/src/FrameRecorder.cpp b/core/libs/prx/libSceAgcDriver/Execution/src/FrameRecorder.cpp new file mode 100644 index 0000000000..0b64a184c8 --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/Execution/src/FrameRecorder.cpp @@ -0,0 +1,587 @@ +#include "prx/libSceAgcDriver/Execution/include/FrameRecorder.hpp" +#include "prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp" +#include "prx/libSceAgcDriver/Execution/include/GuestMemory.hpp" +#include "prx/libSceAgcDriver/TraceRevision.hpp" +#include "prx/libc/include/GuestAllocations.hpp" +#include "prx/libc/include/GuestArena.hpp" +#include "prx/libc/include/General.hpp" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#ifdef _WIN32 +#include +#else +#include +#endif + +extern "C" int APS5_VABI sceAgcDriverGetTFRing(uintptr_t* base, uint32_t* size); + +namespace AgcDriver { +namespace { + +std::uint64_t readNumber(const char* text, std::uint64_t fallback) { + if (text == nullptr || *text == '\0') return fallback; + std::uint64_t value = 0; + const std::string_view view(text); + const auto [last, error] = std::from_chars(view.data(), view.data() + view.size(), value); + if (error != std::errc{} || last != view.data() + view.size()) throw std::runtime_error("frame capture: '" + std::string(view) + "' is not a number"); + return value; +} + +std::string jsonString(std::string_view text) { + std::string result = "\""; + for (const char character : text) { + if (character == '"' || character == '\\') { + result += '\\'; + result += character; + } else if (static_cast(character) < 0x20) { + char escaped[8]; + std::snprintf(escaped, sizeof(escaped), "\\u%04x", static_cast(static_cast(character))); + result += escaped; + } else { + result += character; + } + } + return result + "\""; +} + +std::vector> captureEnvironment() { +#ifdef _WIN32 + char** entries = _environ; +#else + char** entries = environ; +#endif + std::vector> result; + for (; entries != nullptr && *entries != nullptr; ++entries) { + const std::string_view entry = *entries; + const auto equals = entry.find('='); + if (!entry.starts_with("APS5_") || equals == std::string_view::npos) continue; + result.emplace_back(std::string(entry.substr(0, equals)), std::string(entry.substr(equals + 1))); + } + std::sort(result.begin(), result.end()); + return result; +} + +std::uint64_t threadId() { + return static_cast(std::hash{}(std::this_thread::get_id())); +} + +std::vector copyRegisters(std::span registers) { + return {registers.begin(), registers.end()}; +} + +std::uint64_t pointer(const void* value) { + return static_cast(reinterpret_cast(value)); +} + +std::uint64_t flipValue(std::int64_t value) { + return static_cast(value); +} + +std::uint64_t contentHash(std::span bytes) { + std::uint64_t hash = 0xcbf29ce484222325ull; + for (const auto value : bytes) hash = (hash ^ static_cast(value)) * 0x100000001b3ull; + return hash == 0 ? 1 : hash; +} + +} + +FrameRecorder* FrameRecorder::Active() { + static FrameRecorder* const recorder = []() -> FrameRecorder* { + const char* directory = std::getenv("APS5_FRAME_CAPTURE"); + if (directory == nullptr || *directory == '\0') return nullptr; + const char* trigger = std::getenv("APS5_FRAME_CAPTURE_TRIGGER"); + const auto atBoundary = readNumber(std::getenv("APS5_FRAME_CAPTURE_AT_FLIP"), 0); + const auto frames = readNumber(std::getenv("APS5_FRAME_CAPTURE_FRAMES"), DefaultFrames); + if (frames == 0) throw std::runtime_error("frame capture: APS5_FRAME_CAPTURE_FRAMES must be at least 1"); + return new FrameRecorder(directory, trigger != nullptr ? trigger : "", atBoundary, frames); + }(); + return recorder; +} + +FrameRecorder::FrameRecorder(std::filesystem::path directory, std::filesystem::path trigger, std::uint64_t atBoundary, std::uint64_t frames) + : directory(std::move(directory)), trigger(std::move(trigger)), atBoundary(atBoundary), frames(frames), origin(std::chrono::steady_clock::now()) {} + +FrameCall FrameRecorder::make(FrameCallKind kind) { + FrameCall call; + call.kind = kind; + call.timeNs = static_cast(std::chrono::duration_cast(std::chrono::steady_clock::now() - origin).count()); + call.thread = threadId(); + return call; +} + +void FrameRecorder::RegisterShader(const Shader* shader) { + auto call = make(FrameCallKind::RegisterShader); + call.address = pointer(shader); + const std::array shaders{shader}; + record(std::move(call), true, shaders); +} + +void FrameRecorder::ResolveShaderAbi(const Shader* shader, std::span context, std::span primitive) { + auto call = make(FrameCallKind::ResolveShaderAbi); + call.address = pointer(shader); + call.context = copyRegisters(context); + call.primitive = copyRegisters(primitive); + const std::array shaders{shader}; + record(std::move(call), true, shaders); +} + +void FrameRecorder::ResolveGraphicsStagesAbi(std::span stages, std::span context, std::span primitive) { + auto call = make(FrameCallKind::ResolveGraphicsStagesAbi); + for (const auto* stage : stages) call.values.push_back(pointer(stage)); + call.context = copyRegisters(context); + call.primitive = copyRegisters(primitive); + record(std::move(call), true, stages); +} + +void FrameRecorder::ResolveGraphicsAbi(const Shader* vertex, const Shader* pixel, std::uint32_t primitiveType) { + auto call = make(FrameCallKind::ResolveGraphicsAbi); + call.values = {pointer(vertex), pointer(pixel), primitiveType}; + const std::array shaders{vertex, pixel}; + record(std::move(call), true, shaders); +} + +void FrameRecorder::RegisterVideoOutput(std::uint32_t handle) { + auto call = make(FrameCallKind::RegisterVideoOutput); + call.values = {handle}; + record(std::move(call), true); +} + +void FrameRecorder::UnregisterVideoOutput(std::uint32_t handle) { + auto call = make(FrameCallKind::UnregisterVideoOutput); + call.values = {handle}; + record(std::move(call), true); +} + +void FrameRecorder::Submit(const Packet& packet, std::uint32_t queue) { + auto call = make(FrameCallKind::Submit); + call.queue = queue; + call.address = pointer(packet.addr); + call.size = packet.dw_num; + call.flags = packet.flags; + record(std::move(call), false); +} + +void FrameRecorder::Flip(const FlipInfo& info) { + auto call = make(FrameCallKind::Flip); + call.cpuFlip = true; + call.values = {info.handle, flipValue(info.index), info.mode, flipValue(info.argument)}; + record(std::move(call), false); +} + +void FrameRecorder::ReserveEopFlip(const FlipInfo& info, std::uint32_t token) { + auto call = make(FrameCallKind::ReserveEopFlip); + call.values = {info.handle, flipValue(info.index), info.mode, flipValue(info.argument), token}; + std::lock_guard lock(mutex); + if (state == State::Waiting) { + call.ordinal = ++ordinal; + reservations.push_back(std::move(call)); + if (reservations.size() > ReservationHistory) reservations.pop_front(); + return; + } + if (state != State::Recording) return; + emitDelta(); + if (state != State::Recording) return; + call.ordinal = ++ordinal; + std::tie(call.accepted, call.completed) = DriverDetail::Driver::Get().SerialProgress(); + writer->WriteCall(call); + ++recordedCalls; +} + +void FrameRecorder::Suspend() { + record(make(FrameCallKind::Suspend), false); +} + +void FrameRecorder::PresentBuffer(const DisplayBuffer& buffer) { + std::lock_guard lock(displayMutex); + displayBuffers[buffer.address] = buffer; +} + +void FrameRecorder::writeReferenceFrames() { + std::map presented; + { + std::lock_guard lock(displayMutex); + presented = displayBuffers; + } + if (presented.empty()) return; + if (!DriverDetail::Driver::Get().WaitIdleFor(std::chrono::seconds(10))) { + warnings.push_back("no reference frames: the driver did not go idle after the last frame"); + return; + } + std::uint64_t index = 0; + for (const auto& [address, buffer] : presented) { + try { + const auto pixels = ReadDisplayBuffer(buffer); + writer->WriteReference(index, address, pixels); + } catch (const std::exception& failure) { + warnings.push_back(std::string("no reference frame for a display buffer: ") + failure.what()); + continue; + } + auto call = make(FrameCallKind::DisplayBuffer); + call.ordinal = ++ordinal; + call.address = buffer.address; + call.values = {index, buffer.pixelFormat, buffer.width, buffer.height, buffer.tilingMode, buffer.pitchInPixel, buffer.dccAddress, buffer.dccClearColor, buffer.alignment}; + writer->WriteCall(call); + ++index; + } +} + +void FrameRecorder::record(FrameCall call, bool prologueCall, std::span shaders) { + std::lock_guard lock(mutex); + if (state == State::Waiting) { + if (!prologueCall) return; + call.ordinal = ++ordinal; + prologue.push_back(std::move(call)); + return; + } + if (state != State::Recording) return; + if (prologueCall) emitShaderDelta(shaders); + else emitDelta(); + if (state != State::Recording) return; + call.ordinal = ++ordinal; + std::tie(call.accepted, call.completed) = DriverDetail::Driver::Get().SerialProgress(); + writer->WriteCall(call); + ++recordedCalls; +} + +void FrameRecorder::FrameBoundary() { + std::lock_guard lock(mutex); + ++boundaries; + if (state == State::Recording) { + if (++recordedFrames >= frames) finish(); + return; + } + if (state == State::Waiting && triggered()) start(); +} + +bool FrameRecorder::triggered() { + if (!trigger.empty()) { + std::error_code error; + if (!std::filesystem::exists(trigger, error)) return false; + std::ifstream file(trigger); + std::uint64_t requested = 0; + if (file >> requested && requested != 0) frames = requested; + return true; + } + return boundaries >= std::max(atBoundary, 1); +} + +void FrameRecorder::emitDelta() { + std::vector spans; + for (const auto& range : ranges) { + if (range.kind == FrameRangeKind::Watched || range.kind == FrameRangeKind::Unwatched) spans.push_back({range.address, range.bytes, generation}); + } + const auto mark = GuestMemory::WriteGeneration(); + const auto added = newRanges(); + writeDelta(spans, {}, added); + generation = mark; + shaderMarks.clear(); +} + +std::vector FrameRecorder::newRanges() { + const auto current = GuestAllocations::GuestAllocationsGeneration_nid_postfix(); + if (current == allocationsGeneration) return {}; + allocationsGeneration = current; + std::map covered; + for (const auto& range : ranges) { + auto& end = covered[range.address]; + end = std::max(end, range.address + range.bytes); + } + std::vector added; + for (const auto& range : committedRanges()) { + const auto end = range.address + range.bytes; + for (auto at = range.address; at < end;) { + const auto next = covered.upper_bound(at); + if (next != covered.begin() && std::prev(next)->second > at) { + at = std::prev(next)->second; + continue; + } + auto piece = range; + piece.address = at; + piece.bytes = (next == covered.end() ? end : std::min(end, next->first)) - at; + added.push_back(piece); + at += piece.bytes; + } + } + return added; +} + +void FrameRecorder::emitShaderDelta(std::span shaders) { + std::set blocks; + std::set hostPages; + std::uintptr_t arenaBase = 0; + std::size_t arenaBytes = 0; + GuestArena::GuestArenaRange_nid_postfix(&arenaBase, &arenaBytes); + const auto note = [&](const void* address, std::uint64_t bytes = 0) { + if (address == nullptr) return; + const auto at = pointer(address); + const bool arena = at >= arenaBase && at - arenaBase < arenaBytes; + const auto blockBytes = arena ? FrameCaptureWriter::BlockBytes : HostPageBytes; + const auto begin = at & ~std::uint64_t{blockBytes - 1}; + const auto count = bytes != 0 ? (at - begin + bytes + blockBytes - 1) / blockBytes : 2; + auto& pages = arena ? blocks : hostPages; + for (std::uint64_t index = 0; index < count; ++index) pages.insert(begin + index * blockBytes); + }; + for (const auto* shader : shaders) { + if (shader == nullptr || !GuestMemory::Accessible(shader, sizeof(Shader))) continue; + note(shader); + note(const_cast(shader->code), shader->shader_size); + note(shader->user_data); + note(shader->cx_registers); + note(shader->sh_registers); + note(shader->specials); + note(shader->input_semantics); + note(shader->output_semantics); + if (shader->user_data == nullptr || !GuestMemory::Accessible(shader->user_data, sizeof(ShaderUserData))) continue; + note(shader->user_data->direct_resource_offset); + for (const auto* sharps : shader->user_data->sharp_resource_offset) note(sharps); + } + if (blocks.empty() && hostPages.empty()) return; + const auto mark = GuestMemory::WriteGeneration(); + std::vector spans; + for (const auto block : blocks) { + const auto found = shaderMarks.find(block); + spans.push_back({block, FrameCaptureWriter::BlockBytes, found != shaderMarks.end() ? std::max(found->second, generation) : generation}); + } + const std::vector pages(hostPages.begin(), hostPages.end()); + writeDelta(spans, pages); + for (const auto block : blocks) shaderMarks[block] = mark; +} + +void FrameRecorder::writeDelta(std::span spans, std::span hostPages, std::span added) { + if (abandonIfDiskLow()) return; + const auto [accepted, completed] = DriverDetail::Driver::Get().SerialProgress(); + const auto group = deltaGroups + 1; + bool any = false; + std::vector> written; + std::vector bytes; + for (const auto& range : added) { + writer->WriteRange(range.address, range.bytes, range.kind, range.protection, range.access); + ranges.push_back(range); + ++lateRanges; + if (!range.readable) { + unreadableBytes += range.bytes; + continue; + } + for (std::uint64_t at = 0; at < range.bytes;) { + bytes.resize(static_cast(std::min(FrameCaptureWriter::BlockBytes, range.bytes - at))); + GuestMemory::ReadCommittedUnsynced(range.address + at, bytes); + const bool zero = std::all_of(bytes.begin(), bytes.end(), [](std::byte value) { return value == std::byte{0}; }); + if (!zero) { + writer->WriteDelta(group, completed, range.address + at, bytes); + deltaBytes += bytes.size(); + any = true; + } + at += bytes.size(); + } + } + for (const auto page : hostPages) { + if (!GuestMemory::Accessible(reinterpret_cast(static_cast(page)), HostPageBytes)) continue; + bytes.resize(HostPageBytes); + std::memcpy(bytes.data(), reinterpret_cast(static_cast(page)), HostPageBytes); + const auto hash = contentHash(bytes); + auto& known = hostPageHashes[page]; + if (known == hash) continue; + known = hash; + writer->WriteDelta(group, completed, page, bytes); + deltaBytes += bytes.size(); + any = true; + } + for (const auto& span : spans) { + GuestMemory::CollectWrittenBlocks(span.address, static_cast(span.bytes), span.since, written); + for (const auto& [begin, end] : written) { + bytes.resize(static_cast(end - begin)); + GuestMemory::ReadCommittedUnsynced(begin, bytes); + writer->WriteDelta(group, completed, begin, bytes); + deltaBytes += bytes.size(); + any = true; + } + } + if (!any) return; + deltaGroups = group; + auto call = make(FrameCallKind::Memory); + call.ordinal = ++ordinal; + call.accepted = accepted; + call.completed = completed; + call.values = {group}; + writer->WriteCall(call); +} + +void FrameRecorder::snapshot(const Range& range) { + writer->WriteRange(range.address, range.bytes, range.kind, range.protection, range.access); + if (!range.readable) { + unreadableBytes += range.bytes; + return; + } + std::vector bytes; + for (std::uint64_t at = 0; at < range.bytes; at += SnapshotPieceBytes) { + if (abandonIfDiskLow()) return; + bytes.resize(static_cast(std::min(SnapshotPieceBytes, range.bytes - at))); + GuestMemory::ReadCommitted(range.address + at, bytes); + snapshotBytes += writer->WriteSnapshot(range.address + at, bytes); + } +} + +bool FrameRecorder::abandonIfDiskLow() { + std::error_code error; + const auto space = std::filesystem::space(directory, error); + if (error || space.available >= MinimumFreeBytes) return false; + warnings.push_back("stopped: less than 4 GiB free on the capture volume"); + std::fprintf(stderr, "[frame-capture] stopped: less than 4 GiB free on the capture volume\n"); + try { + writer->Finish(header()); + } catch (const std::exception& failure) { + std::fprintf(stderr, "[frame-capture] %s\n", failure.what()); + } + writer.reset(); + state = State::Done; + return true; +} + +void FrameRecorder::start() { + ++startAttempts; + auto& driver = DriverDetail::Driver::Get(); + if (!driver.WaitIdleFor(std::chrono::seconds(10))) { + std::fprintf(stderr, "[frame-capture] the driver did not go idle within 10 s at frame boundary %llu (attempt %llu)\n", static_cast(boundaries), static_cast(startAttempts)); + if (startAttempts >= 5) { + std::fprintf(stderr, "[frame-capture] giving up: no idle frame boundary\n"); + state = State::Done; + } + return; + } + startedAt = std::chrono::steady_clock::now(); + startBoundary = boundaries; + allocationsGeneration = GuestAllocations::GuestAllocationsGeneration_nid_postfix(); + ranges = committedRanges(); + writer = std::make_unique(directory); + std::vector> written; + for (const auto& range : ranges) { + if (range.kind == FrameRangeKind::Watched || range.kind == FrameRangeKind::Unwatched) GuestMemory::CollectWrittenBlocks(range.address, static_cast(range.bytes), std::numeric_limits::max(), written); + } + generation = GuestMemory::WriteGeneration(); + for (const auto& range : ranges) { + snapshot(range); + if (!writer) return; + } + for (const auto& call : prologue) writer->WriteCall(call); + std::uintptr_t ringBase = 0; + std::uint32_t ringSize = 0; + sceAgcDriverGetTFRing(&ringBase, &ringSize); + auto ring = make(FrameCallKind::SetTessellationFactorRing); + ring.ordinal = ++ordinal; + ring.address = ringBase; + ring.size = ringSize; + writer->WriteCall(ring); + const auto pending = driver.PendingEopFlipTokens(); + const std::set open(pending.begin(), pending.end()); + for (const auto& call : reservations) { + if (open.contains(call.values.back())) writer->WriteCall(call); + } + auto begin = make(FrameCallKind::FrameStart); + begin.ordinal = ++ordinal; + std::tie(begin.accepted, begin.completed) = driver.SerialProgress(); + begin.values = {boundaries}; + writer->WriteCall(begin); + state = State::Recording; + recordedFrames = 0; + std::fprintf(stderr, "[frame-capture] started at frame boundary %llu: %zu ranges, %.1f MiB snapshot in %.1f s\n", static_cast(boundaries), ranges.size(), + static_cast(snapshotBytes) / (1024.0 * 1024.0), std::chrono::duration(std::chrono::steady_clock::now() - startedAt).count()); +} + +void FrameRecorder::finish() { + emitDelta(); + if (!writer) return; + auto end = make(FrameCallKind::End); + end.ordinal = ++ordinal; + std::tie(end.accepted, end.completed) = DriverDetail::Driver::Get().SerialProgress(); + writer->WriteCall(end); + writeReferenceFrames(); + writer->Finish(header()); + std::fprintf(stderr, "[frame-capture] %llu frames, %llu calls, %llu memory deltas (%.1f MiB), %.1f MiB raw, %.1f MiB stored in %s\n", static_cast(recordedFrames), + static_cast(recordedCalls), static_cast(deltaGroups), static_cast(deltaBytes) / (1024.0 * 1024.0), + static_cast(writer->RawBytes()) / (1024.0 * 1024.0), static_cast(writer->StoredBytes()) / (1024.0 * 1024.0), directory.string().c_str()); + writer.reset(); + state = State::Done; +} + +std::string FrameRecorder::header() const { + std::uintptr_t arenaBase = 0; + std::size_t arenaBytes = 0; + GuestArena::GuestArenaRange_nid_postfix(&arenaBase, &arenaBytes); + std::string text = "{\"magic\":\"aps5-frame-capture 1\""; + text += ",\"commit\":" + jsonString(APS5_TRACE_COMMIT); + text += std::string(",\"dirty\":") + (APS5_TRACE_DIRTY < 0 ? "null" : APS5_TRACE_DIRTY != 0 ? "true" : "false"); + text += ",\"arena_base\":" + std::to_string(arenaBase) + ",\"arena_bytes\":" + std::to_string(arenaBytes); + text += ",\"start_boundary\":" + std::to_string(startBoundary) + ",\"frames\":" + std::to_string(recordedFrames); + text += ",\"calls\":" + std::to_string(recordedCalls) + ",\"prologue_calls\":" + std::to_string(prologue.size()); + text += ",\"ranges\":" + std::to_string(ranges.size()) + ",\"late_ranges\":" + std::to_string(lateRanges) + ",\"snapshot_bytes\":" + std::to_string(snapshotBytes) + ",\"unreadable_bytes\":" + std::to_string(unreadableBytes); + text += ",\"memory_groups\":" + std::to_string(deltaGroups) + ",\"delta_bytes\":" + std::to_string(deltaBytes); + text += ",\"capture_seconds\":" + std::to_string(std::chrono::duration(std::chrono::steady_clock::now() - startedAt).count()); + text += ",\"env\":{"; + bool first = true; + for (const auto& [name, value] : captureEnvironment()) { + text += (first ? "" : ",") + jsonString(name) + ":" + jsonString(value); + first = false; + } + text += "},\"warnings\":["; + first = true; + for (const auto& warning : warnings) { + text += (first ? "" : ",") + jsonString(warning); + first = false; + } + return text + "]}\n"; +} + +std::vector FrameRecorder::committedRanges() { +#ifdef _WIN32 + std::uintptr_t base = 0; + std::size_t size = 0; + GuestArena::GuestArenaRange_nid_postfix(&base, &size); + if (size == 0) throw std::runtime_error("frame capture: the guest arena is not reserved"); + std::vector result; + for (const auto& allocation : GuestAllocations::GuestAllocationsAcquireAll_nid_postfix()) { + if (!allocation->readable) continue; + const auto first = allocation->address; + const auto end = first + allocation->bytes; + const bool host = first < base || end > base + size; + const auto access = FrameRangeAccess::Readable | (allocation->writable ? FrameRangeAccess::Writable : 0u) | (allocation->gpu ? FrameRangeAccess::Gpu : 0u); + for (auto address = first; address < end;) { + MEMORY_BASIC_INFORMATION info{}; + if (VirtualQuery(reinterpret_cast(static_cast(address)), &info, sizeof(info)) == 0) throw std::runtime_error("frame capture: VirtualQuery failed inside the guest arena"); + const auto regionEnd = std::min(end, reinterpret_cast(info.BaseAddress) + info.RegionSize); + if (info.State == MEM_COMMIT) { + Range range; + range.address = address; + range.bytes = regionEnd - address; + range.protection = info.Protect; + range.access = access; + const auto page = info.Protect & 0xffu; + range.readable = (info.Protect & PAGE_GUARD) == 0 && (page == PAGE_READONLY || page == PAGE_READWRITE || page == PAGE_WRITECOPY || page == PAGE_EXECUTE_READ || page == PAGE_EXECUTE_READWRITE || page == PAGE_EXECUTE_WRITECOPY); + if (host) range.kind = FrameRangeKind::Host; + else if (info.Type == MEM_PRIVATE) range.kind = GuestMemory::Watched(range.address, static_cast(range.bytes)) ? FrameRangeKind::Watched : FrameRangeKind::Unwatched; + else range.kind = FrameRangeKind::Section; + auto& last = result.empty() ? range : result.back(); + if (!result.empty() && last.address + last.bytes == range.address && last.kind == range.kind && last.protection == range.protection && last.readable == range.readable && last.access == range.access) last.bytes += range.bytes; + else result.push_back(range); + } + address = regionEnd; + } + } + return result; +#else + throw std::runtime_error("frame capture: guest memory capture is implemented on Windows only"); +#endif +} + +} diff --git a/core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp b/core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp index 960cf4a11c..fe8241fcfc 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp +++ b/core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -1041,6 +1042,7 @@ struct WriteTracker { }; std::unordered_map cpuPages; std::unordered_map walkPages; + std::map refused; void noteCpuStore(std::uint64_t begin, std::uint64_t end, std::uint32_t stampGeneration) { notePages(cpuPages, begin, end, stampGeneration); @@ -1245,7 +1247,16 @@ struct WriteTracker { return false; } + bool refusedSince(std::uint64_t address, std::uint64_t end, std::uint64_t since) const { + const auto last = blockOf(end - 1); + for (auto it = refused.lower_bound(blockOf(address)); it != refused.end() && it->first <= last; ++it) { + if (it->second > since) return true; + } + return false; + } + bool rangeUnchanged(std::uint64_t address, std::size_t bytes, std::uint64_t since) const { + if (refusedSince(address, address + bytes, since)) return false; return unchanged(blockOf(address), blockOf(address + bytes - 1), since) || !storedOver(address, address + bytes, since, true); } @@ -1450,13 +1461,32 @@ bool collectWindowsWrites(StampRuns& runs, std::uint64_t first, std::uint64_t st if (runs.stamped != 0) collectDirty.fetch_add(1, std::memory_order_relaxed); return fresh || singlePass || runs.stamped == 0 || walk(true); } + +void noteRefusedWalk(WriteTracker& tracker, std::uint64_t first, std::uint64_t stop) { + const auto pagesOf = [&](auto list, auto&& each) { + auto cursor = first; + while (cursor < stop) { + std::size_t count = tracker.pages.size(); + list(static_cast(cursor), static_cast(stop - cursor), tracker.pages.data(), &count); + for (std::size_t i = 0; i < count; ++i) each(reinterpret_cast(tracker.pages[i])); + if (count < tracker.pages.size()) break; + cursor = reinterpret_cast(tracker.pages[count - 1]) + WritePageBytes; + } + }; + StampRuns runs{tracker, StampKind::Cpu}; + pagesOf(&GuestArena::GuestArenaTakeUnreportedPages_nid_no_patch, [&](std::uintptr_t page) { stampWrittenRun(&runs, page, page + WritePageBytes); }); + const auto generation = tracker.generation.load(std::memory_order_relaxed); + pagesOf(&GuestArena::GuestArenaComparedPages_nid_no_patch, [&](std::uintptr_t page) { tracker.refused[tracker.blockOf(page)] = generation; }); +} #endif bool walkWrites(WriteTracker& tracker, std::uint64_t first, std::uint64_t stop, StampKind kind, bool armedOnly = false, const WindowCopy* window = nullptr) { ++tracker.generation; StampRuns runs{tracker, kind, 0, window}; #ifdef _WIN32 - return collectWindowsWrites(runs, first, stop, armedOnly); + if (collectWindowsWrites(runs, first, stop, armedOnly)) return true; + noteRefusedWalk(tracker, first, stop); + return false; #else const auto dirtyRuns = collectDirtyRuns.load(std::memory_order_relaxed); const auto collect = armedOnly ? &GuestWriteWatch::GuestWriteWatchCollectArmed_nid_postfix : &GuestWriteWatch::GuestWriteWatchCollect_nid_postfix; @@ -1549,6 +1579,71 @@ std::uint64_t CollectWritesUncached(std::uint64_t address, std::size_t bytes) { return collectWrites(address, bytes, false); } +std::uint64_t WriteGeneration() { + return Tracker().generation.load(std::memory_order_acquire); +} + +std::uint64_t CollectWrittenBlocks(std::uint64_t address, std::size_t bytes, std::uint64_t since, std::vector>& written) { + written.clear(); + if (bytes == 0 || bytes > std::numeric_limits::max() - address) return 0; + auto& tracker = Tracker(); + const auto lock = lockTracker(tracker); + tracker.initialize(); + if (!tracker.watched) return 0; +#ifdef _WIN32 + if (address < tracker.base || bytes > tracker.size || address - tracker.base > tracker.size - bytes) return 0; +#endif + const auto end = address + bytes; + const auto first = tracker.blockOf(address); + const auto last = tracker.blockOf(end - 1); + const auto clip = [&](std::uint64_t block) { + const auto begin = tracker.blockBegin(block); + return std::pair{std::max(address, begin), std::min(end, begin + WriteBlockBytes)}; + }; + std::uint64_t spanBegin = 0; + std::uint64_t spanEnd = 0; + const auto walkSpan = [&] { + if (spanEnd > spanBegin) walkWrites(tracker, spanBegin & ~std::uint64_t{WritePageBytes - 1}, (spanEnd + WritePageBytes - 1) & ~std::uint64_t{WritePageBytes - 1}, StampKind::Cpu); + spanBegin = spanEnd = 0; + }; + for (auto block = first; block <= last; ++block) { + const auto [begin, stop] = clip(block); + if (!tracker.covers(begin, stop - begin)) { + walkSpan(); + continue; + } + if (spanEnd != begin) { + walkSpan(); + spanBegin = begin; + } + spanEnd = stop; + } + walkSpan(); + const auto append = [&](std::uint64_t begin, std::uint64_t stop) { + if (!written.empty() && written.back().second == begin) written.back().second = stop; + else written.emplace_back(begin, stop); + }; + for (auto block = first; block <= last; ++block) { + if (tracker.cpuStampOf(block) <= since) continue; + const auto [begin, stop] = clip(block); + const auto pages = tracker.cpuPages.find(block); + bool any = false; + if (pages != tracker.cpuPages.end()) { + const auto blockStart = tracker.blockBegin(block); + for (std::size_t page = 0; page < WritePagesPerBlock; ++page) { + if (pages->second.pages[page] <= since) continue; + const auto pageBegin = std::max(begin, blockStart + page * WritePageBytes); + const auto pageEnd = std::min(stop, blockStart + (page + 1) * WritePageBytes); + if (pageBegin >= pageEnd) continue; + append(pageBegin, pageEnd); + any = true; + } + } + if (!any) append(begin, stop); + } + return tracker.generation.load(std::memory_order_relaxed); +} + void BumpCollectEpoch() { const auto epoch = nextCollectEpoch.fetch_add(1, std::memory_order_relaxed) + 1; HostThreadSlot::Set(epoch); @@ -1658,7 +1753,9 @@ void Rewatch(std::span> ranges) { for (const auto& [begin, end] : ranges) { if (end <= begin || begin < tracker.base || end - tracker.base > tracker.size || (begin - tracker.base) % WriteBlockBytes != 0 || (end - tracker.base) % WriteBlockBytes != 0) continue; const auto bytes = static_cast(end - begin); - if (tracker.coverage.Covers(begin, bytes) || !walkWrites(tracker, begin, end, StampKind::Cpu) || !tracker.coverage.Restore(begin, bytes)) continue; + if (tracker.coverage.Covers(begin, bytes)) continue; + GuestArena::GuestArenaCompareWhenShared_nid_no_patch(static_cast(begin), bytes); + if (!walkWrites(tracker, begin, end, StampKind::Cpu) || !tracker.coverage.Restore(begin, bytes)) continue; ++tracker.generation; tracker.noteCpuStore(begin, end, tracker.generation); for (auto block = tracker.blockOf(begin); block <= tracker.blockOf(end - 1); ++block) { @@ -1729,6 +1826,9 @@ bool ImportWatched(std::uint64_t address, std::size_t bytes, const std::function auto unwatchedFrom = first; for (const auto& [begin, end] : spans) { if (!walkWrites(tracker, begin, end, StampKind::Cpu, true, &window)) continue; +#ifdef _WIN32 + GuestArena::GuestArenaCompareResidentWhenShared_nid_no_patch(static_cast(begin), static_cast(end - begin)); +#endif if (unwatchedFrom < begin) unwatchLocked(tracker, unwatchedFrom, static_cast(begin - unwatchedFrom)); unwatchedFrom = end; } @@ -1791,7 +1891,7 @@ bool UnchangedCommittedSince(std::span(end - begin)) || tracker.newestStamp(begin, end) > generation) return false; + if (end <= begin || !tracker.covers(begin, static_cast(end - begin)) || tracker.newestStamp(begin, end) > generation || tracker.refusedSince(begin, end, generation)) return false; } return true; } diff --git a/core/libs/prx/libSceAgcDriver/Execution/src/HostAllocator.cpp b/core/libs/prx/libSceAgcDriver/Execution/src/HostAllocator.cpp index cbbb9c549d..d33999f9e3 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/src/HostAllocator.cpp +++ b/core/libs/prx/libSceAgcDriver/Execution/src/HostAllocator.cpp @@ -24,6 +24,41 @@ struct Cache { std::uint16_t counts[Classes]; }; +constexpr std::uint16_t KeptLists = 8; + +struct Depot { + SRWLOCK lock = SRWLOCK_INIT; + void* lists = nullptr; + std::uint16_t count = 0; +}; + +Depot depots[Classes]; + +bool KeepList(std::size_t sizeClass, void* list) noexcept { + auto& depot = depots[sizeClass]; + AcquireSRWLockExclusive(&depot.lock); + const bool kept = depot.count < KeptLists; + if (kept) { + static_cast(list)[1] = depot.lists; + depot.lists = list; + ++depot.count; + } + ReleaseSRWLockExclusive(&depot.lock); + return kept; +} + +void* TakeList(std::size_t sizeClass) noexcept { + auto& depot = depots[sizeClass]; + AcquireSRWLockExclusive(&depot.lock); + void* list = depot.lists; + if (list != nullptr) { + depot.lists = static_cast(list)[1]; + --depot.count; + } + ReleaseSRWLockExclusive(&depot.lock); + return list; +} + Cache* const Closed = reinterpret_cast(std::uintptr_t{1}); constexpr unsigned long TlsSlotsOffset = 0x1480; @@ -114,6 +149,12 @@ void* TryAllocate(std::size_t bytes) noexcept { if (bytes <= LargestCached) { const auto sizeClass = (bytes - 1) / Granule; if (auto* cache = CurrentCache()) { + if (cache->heads[sizeClass] == nullptr) { + if (void* list = TakeList(sizeClass)) { + cache->heads[sizeClass] = list; + cache->counts[sizeClass] = Depth(sizeClass); + } + } if (void* block = cache->heads[sizeClass]) { cache->heads[sizeClass] = *static_cast(block); --cache->counts[sizeClass]; @@ -140,6 +181,10 @@ void Release(void* block, std::size_t bytes = 0) noexcept { const auto usable = bytes != 0 && bytes % Granule == 0 ? bytes : _msize(block); if (usable >= Granule && usable < LargestCached + Granule) { const auto sizeClass = usable / Granule - 1; + if (cache->counts[sizeClass] == Depth(sizeClass) && KeepList(sizeClass, cache->heads[sizeClass])) { + cache->heads[sizeClass] = nullptr; + cache->counts[sizeClass] = 0; + } if (cache->counts[sizeClass] < Depth(sizeClass)) { *static_cast(block) = cache->heads[sizeClass]; cache->heads[sizeClass] = block; diff --git a/core/libs/prx/libSceAgcDriver/Execution/src/Pm4.cpp b/core/libs/prx/libSceAgcDriver/Execution/src/Pm4.cpp index ea98ec2c06..f742e7b945 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/src/Pm4.cpp +++ b/core/libs/prx/libSceAgcDriver/Execution/src/Pm4.cpp @@ -3,6 +3,7 @@ #include "prx/libc/include/General.hpp" #include "prx/libc/include/PreciseWait.hpp" #include +#include #include #include #include @@ -226,8 +227,7 @@ std::string_view UnsupportedReason(std::uint32_t header) { return "graphics draw, shader stages and guest render-target materialization are not implemented"; case 0x33: case 0x3f: return "nested command buffers, branching and nested flip reservation are not implemented"; case 0x3c: case 0x93: return {}; - case 0x39: - return "cooperative command-queue waits are not implemented"; + case 0x39: return {}; case 0x59: return {}; case 0x84: case 0x85: case 0x86: case 0x88: return "separate CE/DE execution and counter synchronization are not implemented"; @@ -458,6 +458,12 @@ void Validate(std::span packet, std::uint32_t queue) { require((packet[1] & 7u) <= 6u, "invalid WAIT_REG_MEM compare function"); break; } + case 0x39: + size(4); + require((packet[3] >> 29u) == 6u || (packet[3] >> 29u) == 7u, "invalid MEM_SEMAPHORE operation"); + require(address(packet[1], packet[2]) != 0 && (packet[1] & 7u) == 0, "null or misaligned MEM_SEMAPHORE address"); + require((packet[3] & 0x10000u) == 0, "MEM_SEMAPHORE mailbox waits are not implemented"); + break; case 0x45: { size(9); require((packet[1] & ~0x317u) == 0, "COND_WRITE reserved fields are not implemented"); @@ -716,6 +722,7 @@ std::vector DecodeStores(std::span packet) { if (!label.has_value()) return {}; return {known(label->address, label->Bytes())}; } + case 0x39: return {runTime(address(packet[1], packet[2]), 8)}; case 0x40: { require(packet.size() == 6, "invalid COPY_DATA size"); if (CopyDataToRegister(packet)) return {}; @@ -771,7 +778,7 @@ bool UsesGpuCacheBarrier(std::span packet) { bool AccessesMemory(std::uint32_t header) { switch ((header >> 8u) & 0xffu) { - case 0x22: case 0x27: case 0x3c: case 0x93: case 0x49: case 0x16: case 0x2d: case 0x35: case 0x24: case 0x25: case 0x2c: case 0x38: case 0x37: case 0x40: case 0x45: case 0x50: case 0x63: case 0x64: case 0x83: case 0x91: case 0x9f: return true; + case 0x22: case 0x27: case 0x39: case 0x3c: case 0x93: case 0x49: case 0x16: case 0x2d: case 0x35: case 0x24: case 0x25: case 0x2c: case 0x38: case 0x37: case 0x40: case 0x45: case 0x50: case 0x63: case 0x64: case 0x83: case 0x91: case 0x9f: return true; default: return false; } } @@ -856,12 +863,17 @@ DrawParameters resolveIndirectDraw(std::span packet, const indirect.indxOffset = offset->second; } else { const auto indexSize = indexSizeOf(queue); - require(queue.indexBufferSize != 0, "INDEX_BUFFER_SIZE has not been set"); - const auto bytes = static_cast(queue.indexBufferSize) * indexSize; - require(bytes <= std::numeric_limits::max() && bytes <= std::numeric_limits::max() - queue.indexBase, "index range size overflow"); draw.indexAddress = queue.indexBase; - draw.indexCount = queue.indexBufferSize; draw.indexSize = indexSize; + if (queue.indexBufferSize) { + require(*queue.indexBufferSize != 0, "INDEX_BUFFER_SIZE is zero"); + const auto bytes = static_cast(*queue.indexBufferSize) * indexSize; + require(bytes <= std::numeric_limits::max() && bytes <= std::numeric_limits::max() - queue.indexBase, "index range size overflow"); + draw.indexCount = *queue.indexBufferSize; + } else { + draw.indexCount = 0; + indirect.indexLimitKnown = false; + } } draw.indirect = indirect; return draw; @@ -1072,6 +1084,47 @@ void Execute(std::span packet, QueueState& queue) { } return; } + case 0x39: { + const auto semaphore = address(packet[1], packet[2]); + auto* word = reinterpret_cast(semaphore); + std::atomic_ref shared(*word); + const auto prepare = [&] { + GuestMemory::GuardRead(semaphore, sizeof(*word)); + GuestMemory::FlushGpuWrites(semaphore, sizeof(*word)); + GuestMemory::CheckRange(word, sizeof(*word), std::atomic_ref::required_alignment, true); + }; + if ((packet[3] >> 29u) == 6u) { + prepare(); + const bool set = (packet[3] & 0x100000u) != 0; + GuestMemory::StoreOwnBytes(semaphore, sizeof(*word), [&] { + if (set) shared.store(1, std::memory_order_release); + else shared.fetch_add(1, std::memory_order_acq_rel); + }); + return; + } + const auto start = std::chrono::steady_clock::now(); + bool warned = false; + for (;;) { + { + const GuestMemory::ReadSiteScope site(GuestMemory::ReadSite::Wait); + prepare(); + } + auto value = shared.load(std::memory_order_acquire); + if (value != 0) { + bool taken = false; + GuestMemory::StoreOwnBytes(semaphore, sizeof(*word), [&] { + taken = shared.compare_exchange_strong(value, value - 1, std::memory_order_acq_rel); + }); + if (taken) return; + continue; + } + PreciseSleepUs(50); + if (!warned && std::chrono::steady_clock::now() - start > std::chrono::seconds(5)) { + warned = true; + std::fprintf(stderr, "[gpu] MEM_SEMAPHORE wait at 0x%llx still waiting after 5s\n", static_cast(semaphore)); + } + } + } case 0x49: { // Earlier work has drained by the time the driver reaches this packet, so the end-of-pipe // write can happen immediately. diff --git a/core/libs/prx/libSceAgcDriver/Execution/src/ShaderMemory.cpp b/core/libs/prx/libSceAgcDriver/Execution/src/ShaderMemory.cpp index f0e0b777f2..4fc0fb89bc 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/src/ShaderMemory.cpp +++ b/core/libs/prx/libSceAgcDriver/Execution/src/ShaderMemory.cpp @@ -392,7 +392,7 @@ bool ShaderMemory::readWords(void* context, std::uint64_t address, std::uint32_t } auto last = first + BlockWords; while (last < end && !page->valid.TestBlock(last)) last += BlockWords; - std::memcpy(&page->words[first], reinterpret_cast(base + first * sizeof(std::uint32_t)), (last - first) * sizeof(std::uint32_t)); + GuestMemory::CopyGuestBytes(&page->words[first], reinterpret_cast(base + first * sizeof(std::uint32_t)), (last - first) * sizeof(std::uint32_t)); page->valid.SetRun(first, last - first); first = last; } @@ -495,8 +495,8 @@ bool ShaderMemory::place(std::uint64_t address, const std::uint32_t* values, std if (!page.valid.Test(index + word)) page.words[index + word] = values[word]; } } - if (headOpen && head < index) std::memcpy(&page.words[head], reinterpret_cast(base + head * sizeof(std::uint32_t)), (index - head) * sizeof(std::uint32_t)); - if (tailOpen && index + words < tail) std::memcpy(&page.words[index + words], reinterpret_cast(base + (index + words) * sizeof(std::uint32_t)), (tail - index - words) * sizeof(std::uint32_t)); + if (headOpen && head < index) GuestMemory::CopyGuestBytes(&page.words[head], reinterpret_cast(base + head * sizeof(std::uint32_t)), (index - head) * sizeof(std::uint32_t)); + if (tailOpen && index + words < tail) GuestMemory::CopyGuestBytes(&page.words[index + words], reinterpret_cast(base + (index + words) * sizeof(std::uint32_t)), (tail - index - words) * sizeof(std::uint32_t)); page.valid.SetRun(page.lazy ? head : index, page.lazy ? tail - head : words); page.read.SetRun(index, words); if (recent) page.recent.SetRun(index, words); diff --git a/core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp b/core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp index 5673acfd53..70fdef29e3 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp +++ b/core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp @@ -422,6 +422,7 @@ struct VulkanDevice::State { bool depthBounds = false; bool depthBiasClamp = false; bool dualSrcBlend = false; + bool logicOp = false; bool occlusionQueryPrecise = false; VkDeviceSize hostImportAlignment = 0; bool drawInputsInPlace = true; @@ -1775,6 +1776,8 @@ VulkanDevice::VulkanDevice(const PresentationWindow* window, bool compileOnly) : enabled.depthBiasClamp = available.depthBiasClamp; state->depthBiasClamp = enabled.depthBiasClamp == VK_TRUE; enabled.dualSrcBlend = available.dualSrcBlend; + enabled.logicOp = available.logicOp; + state->logicOp = enabled.logicOp == VK_TRUE; state->dualSrcBlend = enabled.dualSrcBlend == VK_TRUE; enabled.occlusionQueryPrecise = available.occlusionQueryPrecise; state->occlusionQueryPrecise = enabled.occlusionQueryPrecise == VK_TRUE; @@ -2033,6 +2036,29 @@ VulkanDevice::VulkanDevice(const PresentationWindow* window, bool compileOnly) : deviceInfo.enabledExtensionCount = static_cast(deviceExtensions.size()); deviceInfo.ppEnabledExtensionNames = deviceExtensions.data(); } + const bool diagnosticsRequested = std::getenv("APS5_NV_DIAGNOSTICS") != nullptr; + VkPhysicalDeviceDiagnosticsConfigFeaturesNV diagnosticsFeatures{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DIAGNOSTICS_CONFIG_FEATURES_NV}; + if (diagnosticsRequested && hasExtension(VK_NV_DEVICE_DIAGNOSTICS_CONFIG_EXTENSION_NAME)) { + VkPhysicalDeviceFeatures2 features{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2, &diagnosticsFeatures}; + state->InstanceFunction("vkGetPhysicalDeviceFeatures2")(selected, &features); + } + const auto diagnostics = ChooseDeviceDiagnostics(diagnosticsRequested, diagnosticsFeatures.diagnosticsConfig == VK_TRUE, hasExtension(VK_NV_DEVICE_DIAGNOSTIC_CHECKPOINTS_EXTENSION_NAME)); + VkDeviceDiagnosticsConfigCreateInfoNV diagnosticsConfig{VK_STRUCTURE_TYPE_DEVICE_DIAGNOSTICS_CONFIG_CREATE_INFO_NV}; + if (diagnostics.config) { + diagnosticsFeatures = {VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DIAGNOSTICS_CONFIG_FEATURES_NV}; + diagnosticsFeatures.diagnosticsConfig = VK_TRUE; + diagnosticsConfig.flags = diagnostics.flags; + deviceExtensions.push_back(VK_NV_DEVICE_DIAGNOSTICS_CONFIG_EXTENSION_NAME); + if (diagnostics.checkpoints && !checkpoints) deviceExtensions.push_back(VK_NV_DEVICE_DIAGNOSTIC_CHECKPOINTS_EXTENSION_NAME); + deviceInfo.enabledExtensionCount = static_cast(deviceExtensions.size()); + deviceInfo.ppEnabledExtensionNames = deviceExtensions.data(); + diagnosticsConfig.pNext = byteFeatures.pNext; + diagnosticsFeatures.pNext = &diagnosticsConfig; + byteFeatures.pNext = &diagnosticsFeatures; + std::fprintf(stderr, "[gpu] APS5_NV_DIAGNOSTICS: VK_NV_device_diagnostics_config enabled (shader debug info, resource tracking, automatic checkpoints, shader error reporting)\n"); + } else if (diagnosticsRequested) { + std::fprintf(stderr, "[gpu] APS5_NV_DIAGNOSTICS: the device does not support VK_NV_device_diagnostics_config\n"); + } bdaFeatures.pNext = &byteFeatures; VkPhysicalDeviceRobustness2FeaturesEXT robustness2{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ROBUSTNESS_2_FEATURES_EXT}; if (hasExtension(VK_EXT_ROBUSTNESS_2_EXTENSION_NAME)) { @@ -2354,6 +2380,14 @@ void VulkanDevice::WaitRecorded(std::uint64_t serial) { if (state->recorder) state->recorder->WaitSerial(serial); } +std::uint64_t VulkanDevice::SubmittedEpoch() const { + return state->recorder ? state->recorder->NewestSubmitted() : 0; +} + +bool VulkanDevice::WaitRecordedFor(std::uint64_t serial, std::uint64_t timeoutNanos) { + return state->recorder && state->recorder->WaitSerialFor(serial, timeoutNanos); +} + void VulkanDevice::ReapRecorded(std::uint64_t serial) { if (!state->recorder) return; // The drain's site in the [recorder] table is this call's caller (Driver::execute's drain, @@ -3940,6 +3974,7 @@ Graphics::Context VulkanDevice::buildContext() const { context.drawIndirectFirstInstance = state->drawIndirectFirstInstance; context.multiDrawIndirect = state->multiDrawIndirect; context.dualSrcBlend = state->dualSrcBlend; + context.logicOp = state->logicOp; context.depthBounds = state->depthBounds; context.depthBiasClamp = state->depthBiasClamp; context.samplerFilterMinmax = state->samplerFilterMinmax; @@ -4011,10 +4046,6 @@ std::optional VulkanDevice::KnownDrawRejection(const Graphics::Stat return Graphics::KnownValidationFailure(graphicsContext(), shaders, graphics); } -void VulkanDevice::DepthCopyPass(const Graphics::DepthCopyPass& pass) { - Graphics::RunDepthCopyPass(graphicsContext(), pass); -} - void VulkanDevice::ColorMetadataPass(const Graphics::ColorMetadataPass& pass) { Graphics::RunColorMetadataPass(graphicsContext(), pass); } @@ -4057,7 +4088,7 @@ bool VulkanDevice::PrebuildGraphicsLibraries(const Graphics::ShaderStages& stage return Graphics::Pipeline::Prebuild(context, state, Graphics::VertexInputLayout{}, layouts.View(), group); } -void VulkanDevice::Draw(const Graphics::State& graphics, const Pm4::DrawParameters& draw, std::span shaders, std::span snapshots, std::shared_ptr* recipe) { +void VulkanDevice::Draw(const Graphics::State& graphics, const Pm4::DrawParameters& draw, std::span shaders, std::span snapshots, std::shared_ptr* recipe, const DrawTraceContext* trace) { startJournalReplay(); for (const auto& shader : shaders) { require(shader.program != nullptr, "missing compiled shader"); @@ -4065,11 +4096,11 @@ void VulkanDevice::Draw(const Graphics::State& graphics, const Pm4::DrawParamete } // Two stdout lines per draw cost ~1.3 ms per frame of the queue-0 worker (part of it under the // GPU mutex); APS5_TRACE_DRAWS=1 restores them. - static const bool trace = std::getenv("APS5_TRACE_DRAWS") != nullptr; - if (trace) APS5_LOG_OUT("VulkanDevice::Draw indices=%u instances=%u indexSize=%u address=0x%llx shaders=%zu colorTarget=%u", draw.indexCount, draw.instanceCount, draw.indexSize, static_cast(draw.indexAddress), shaders.size(), static_cast(graphics.hasColorTarget)); + static const bool traceDraws = std::getenv("APS5_TRACE_DRAWS") != nullptr; + if (traceDraws) APS5_LOG_OUT("VulkanDevice::Draw indices=%u instances=%u indexSize=%u address=0x%llx shaders=%zu colorTarget=%u", draw.indexCount, draw.instanceCount, draw.indexSize, static_cast(draw.indexAddress), shaders.size(), static_cast(graphics.hasColorTarget)); const auto context = graphicsContext(); - Graphics::Draw(context, graphics, draw, shaders, snapshots, recipe); - if (trace) APS5_LOG_CHARS_OUT("VulkanDevice::Draw complete"); + Graphics::Draw(context, graphics, draw, shaders, snapshots, recipe, trace); + if (traceDraws) APS5_LOG_CHARS_OUT("VulkanDevice::Draw complete"); } bool VulkanDevice::DrawInputsInPlace() const { @@ -4618,7 +4649,7 @@ void VulkanDevice::NoteDrawRecipeMiss(DrawRecipePrecheck miss) { counters.precheckMisses[miss == DrawRecipePrecheck::NoRecipe ? MissNoRecipe : MissDevice].fetch_add(1, std::memory_order_relaxed); } -RecipeOutcome VulkanDevice::DrawFromRecipe(const Graphics::State& graphics, const Pm4::DrawParameters& draw, std::span shaders, std::span snapshots, const std::shared_ptr& recipe) { +RecipeOutcome VulkanDevice::DrawFromRecipe(const Graphics::State& graphics, const Pm4::DrawParameters& draw, std::span shaders, std::span snapshots, const std::shared_ptr& recipe, const DrawTraceContext* trace) { PerformanceTimer timing("Vulkan.DrawFromRecipe"); static const bool profile = std::getenv("APS5_PROFILE_DRAW") != nullptr; auto& counters = Recipes().kinds[static_cast(RecipeKind::Draw)]; @@ -4629,7 +4660,7 @@ RecipeOutcome VulkanDevice::DrawFromRecipe(const Graphics::State& graphics, cons return RecipeOutcome::Rebuild; } const auto context = graphicsContext(); - const auto outcome = Graphics::DrawWithRecipe(context, graphics, draw, shaders, snapshots, *recipe); + const auto outcome = Graphics::DrawWithRecipe(context, graphics, draw, shaders, snapshots, *recipe, trace); if (!outcome.recorded) { switch (outcome.miss) { case Graphics::DrawRecipeMiss::NotRecordable: counters.precheckMisses[MissNotRecordable].fetch_add(1, std::memory_order_relaxed); break; diff --git a/core/libs/prx/libSceAgcDriver/Graphics/include/Context.hpp b/core/libs/prx/libSceAgcDriver/Graphics/include/Context.hpp index 286666da03..7e0e07d07b 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/include/Context.hpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/include/Context.hpp @@ -213,6 +213,7 @@ struct Context { bool depthBounds = false; bool depthBiasClamp = false; bool dualSrcBlend = false; + bool logicOp = false; bool samplerFilterMinmax = false; bool nonSeamlessCubeMap = false; VkSampleCountFlags mergedCoverageSampleCounts = 0; diff --git a/core/libs/prx/libSceAgcDriver/Graphics/include/DepthSurface.hpp b/core/libs/prx/libSceAgcDriver/Graphics/include/DepthSurface.hpp index 835b80f59b..ef9852b1e0 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/include/DepthSurface.hpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/include/DepthSurface.hpp @@ -72,11 +72,22 @@ VkImageAspectFlags HtileFillClears(std::uint32_t pattern, bool stencilInHtile); std::uint64_t HtileMetadataBytes(VkExtent2D extent); bool HtileFillCovers(std::uint64_t htile, VkExtent2D extent, std::uint64_t address, std::size_t bytes); std::shared_ptr DepthSurfaceTexture(const Context& context, std::span words, const GuestTextureResource& resource, VkComponentMapping components, VkImageViewType multisampleViewType = VK_IMAGE_VIEW_TYPE_2D); +std::size_t DepthPlaneCopyCount(); VkFormat HostDepthFormat(const Context& context, VkFormat format); float HostDepthBiasConstant(const Context& context, VkFormat format, float constant); std::shared_ptr DepthSurfaceStorage(const Context& context, std::span words, const GuestTextureResource& resource, std::uint32_t mip); bool DepthSurfaceStoreBack(const Context& context, const StorageTexture& storage); -void CopyDepthSample(const Context& context, std::uint64_t depthAddress, std::uint32_t samples, std::uint32_t sample, StorageTexture& destination, VkImageView destinationView, VkExtent2D extent); +std::array DepthCopySourceWords(const DepthTarget& target); +class DepthCopySourceScope { +public: + explicit DepthCopySourceScope(bool active); + ~DepthCopySourceScope(); + DepthCopySourceScope(const DepthCopySourceScope&) = delete; + DepthCopySourceScope& operator=(const DepthCopySourceScope&) = delete; + +private: + bool previous; +}; struct StorageImageClear { std::array image{}; diff --git a/core/libs/prx/libSceAgcDriver/Graphics/include/Draw.hpp b/core/libs/prx/libSceAgcDriver/Graphics/include/Draw.hpp index e2ffa0f98d..ea02167400 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/include/Draw.hpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/include/Draw.hpp @@ -14,10 +14,18 @@ namespace AgcDriver::Graphics { struct HostImport; +struct DrawTraceContext { + std::uint64_t token = 0; + std::uint64_t submission = 0; + std::uint32_t queue = 0; + std::size_t offset = 0; + std::uint64_t target = 0; +}; + // `recipe`, when given, receives the DrawRecipe a recorded, cacheable, reusable, non-indirect draw // built for its draw-cache entry (design_cpu_final M8; null otherwise, and always under // APS5_NO_DRAW_RECIPE=1). -void Draw(const Context& context, const State& state, const Pm4::DrawParameters& draw, std::span shaders, std::span snapshots = {}, std::shared_ptr* recipe = nullptr); +void Draw(const Context& context, const State& state, const Pm4::DrawParameters& draw, std::span shaders, std::span snapshots = {}, std::shared_ptr* recipe = nullptr, const DrawTraceContext* trace = nullptr); struct PixelFunctionLdsLayouts { VkDescriptorSetLayout empty = VK_NULL_HANDLE; @@ -92,7 +100,7 @@ struct DrawRecipeOutcome { // the template's proof (ProveCurrent) with the alias checks repeated, the pipeline and framebuffer // from the recipe, then the record as Draw's (pass continued or begun, Kept, MarkGpuWrites). A // miss records nothing. -DrawRecipeOutcome DrawWithRecipe(const Context& context, const State& state, const Pm4::DrawParameters& draw, std::span shaders, std::span snapshots, const DrawRecipe& recipe); +DrawRecipeOutcome DrawWithRecipe(const Context& context, const State& state, const Pm4::DrawParameters& draw, std::span shaders, std::span snapshots, const DrawRecipe& recipe, const DrawTraceContext* trace = nullptr); // Whether recorded draws build recipes and draw-cache hits use them (APS5_NO_DRAW_RECIPE unset). bool DrawRecipes(); @@ -102,7 +110,7 @@ bool DrawRecipes(); // draw index, the vertex ranges, the device features, APS5_NO_GPU_INDIRECT_DRAW) or under the device // lock in Draw (the memory state of the records). `readMs` is the CPU read's time (its sync); // `rewritten` counts a GPU-side draw whose records were copied and patched with a constant. -enum class IndirectDrawPath : std::uint8_t { Gpu, NotFolded, FetchUnknown, NonVertexPath, IndxOffset, DrawIndex, VertexRange, FeatureGap, PendingImage, PendingLabelOrCopy, NotImported, Disabled, IndexRange, Count }; +enum class IndirectDrawPath : std::uint8_t { Gpu, NotFolded, FetchUnknown, NonVertexPath, IndxOffset, DrawIndex, VertexRange, FeatureGap, PendingImage, PendingLabelOrCopy, NotImported, Disabled, IndexRange, IndexLimitUnknown, Count }; void CountIndirectDraw(IndirectDrawPath path, double readMs, bool rewritten = false); std::array(IndirectDrawPath::Count)> IndirectDrawCounts(); void CountEmptyIndirectDraw(double readMs); @@ -140,7 +148,6 @@ void CountDrawSkip(DrawSkip kind, double us); std::shared_ptr>> DrawCopiedWriters(); void RunColorMetadataPass(const Context& context, const ColorMetadataPass& pass); -void RunDepthCopyPass(const Context& context, const DepthCopyPass& pass); std::vector> PipelinedColorMetadataPassWrites(const ColorMetadataPass& pass); bool DrawPipelineEnabled(); bool CommitTrims(); diff --git a/core/libs/prx/libSceAgcDriver/Graphics/include/MultisampleTarget.hpp b/core/libs/prx/libSceAgcDriver/Graphics/include/MultisampleTarget.hpp index 68eb4d07db..04f7e5ccbd 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/include/MultisampleTarget.hpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/include/MultisampleTarget.hpp @@ -81,6 +81,7 @@ std::shared_ptr MultisampleTargetFmaskTexture_nid_no_patch(const Contex void ClearMultisampleTargets(VkDevice device); void InstallResidentImageReadGuard_nid_no_patch(); void UpdateResidentImageUnmapObserver_nid_no_patch(); +void GuardOneFragmentFmask(const Context& context, const ColorTarget& target); void RequireNoMultisampleTarget(std::uint64_t address, std::size_t bytes, const char* access); void RequireNoMultisampleColorTarget(std::uint64_t address, std::size_t bytes, const char* access); diff --git a/core/libs/prx/libSceAgcDriver/Graphics/include/PipelineJournal.hpp b/core/libs/prx/libSceAgcDriver/Graphics/include/PipelineJournal.hpp index c2cf90476e..d905e217cf 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/include/PipelineJournal.hpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/include/PipelineJournal.hpp @@ -61,11 +61,14 @@ struct PipelineJournalCounters { std::uint64_t replayFailed = 0; double replayMs = 0; std::uint32_t replayThreads = 0; + std::uint64_t replayHeldPeakBytes = 0; std::uint64_t recordedGraphics = 0; std::uint64_t recordedCompute = 0; std::uint64_t recordedModules = 0; std::uint64_t writtenBytes = 0; std::uint64_t writeFailures = 0; + std::uint64_t evictedRecords = 0; + std::uint64_t evictedBytes = 0; }; // A decimal switch of the pipeline store or journal, or its default when unset; anything else throws. diff --git a/core/libs/prx/libSceAgcDriver/Graphics/include/Recorder.hpp b/core/libs/prx/libSceAgcDriver/Graphics/include/Recorder.hpp index 2cbd239cf1..8f1c131eba 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/include/Recorder.hpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/include/Recorder.hpp @@ -107,7 +107,8 @@ public: static constexpr std::size_t DrawInputEntries = 16384; std::shared_ptr UnwrittenDrawSnapshot(std::uint64_t address, std::size_t bytes, const std::array& stamp, VkDeviceSize* offset = nullptr); void NoteUnwrittenDrawSnapshot(std::uint64_t address, std::size_t bytes, const std::array& stamp, std::shared_ptr buffer, VkDeviceSize offset = 0); - std::shared_ptr ReusableDrawSnapshot(std::uint64_t address, std::size_t bytes, SnapshotUse use = SnapshotUse::Storage, std::uint32_t* derived = nullptr, std::uint64_t generation = 0); + std::shared_ptr ReusableDrawSnapshot(std::uint64_t address, std::size_t bytes, SnapshotUse use = SnapshotUse::Storage, std::uint32_t* derived = nullptr, std::uint64_t generation = 0, VkDeviceSize* offset = nullptr); + static constexpr std::size_t ContainingSnapshotProbes = 16; void KeepDrawSnapshot(std::uint64_t address, std::size_t bytes, std::uint64_t generation, std::uint64_t registryGeneration, std::shared_ptr buffer, SnapshotUse use = SnapshotUse::Storage, std::uint32_t derived = 0); static constexpr std::size_t DrawUploadChunk = std::size_t{1} << 20u; struct UploadSlice { @@ -202,12 +203,14 @@ public: // Submits the open batch and returns the serial of the newest submitted batch (0 when nothing was // ever submitted); WaitSerial(serial) then covers all recorded work. std::uint64_t SubmitAndEpoch(); + std::uint64_t OpenBatchSerial(); bool HasTimeline() const { return timeline != VK_NULL_HANDLE; } bool RecordDispatchArguments(VkCommandBuffer commands, VkDeviceAddress source, VkDeviceAddress arguments, const std::array* threadGroup = nullptr); // Waits until every batch up to `serial` completed on the GPU. Called WITHOUT GuestMemory::GpuMutex: // it touches only the immutable timeline semaphore (the caller keeps the device alive). Requires // HasTimeline(); the debug switch APS5_NO_TIMELINE makes the device create the recorder without one. void WaitSerial(std::uint64_t serial); + bool WaitSerialFor(std::uint64_t serial, std::uint64_t timeoutNanos); // Under the mutex: finishes (completions, release) the in-flight batches up to `serial`, from the // front only; tolerates batches another thread finished meanwhile. Later batches stay in flight. void FinishUpTo(std::uint64_t serial); @@ -219,6 +222,7 @@ public: std::uint64_t SamplesTotal(); bool DumpSamples(VkDeviceAddress target, std::uint64_t address = 0); void NoteSampledDraw(VkCommandBuffer commands); + void NoteDrawTraceToken(std::uint64_t token) noexcept; bool QueuesSampleDumps() const; void PrepareSampleSlot(); void EndPassSamples(); @@ -677,6 +681,8 @@ private: std::uint64_t end; }; std::vector keyStores; + std::vector drawTraceTokens; + std::unordered_set drawTraceTokenSet; }; struct LabelEntry { std::uint32_t value; @@ -887,6 +893,7 @@ private: std::map indexRanges; std::list indexRangeRecency; bool refreshDrawSnapshot(DrawSnapshot& entry, std::uint64_t address, std::size_t bytes); + std::shared_ptr containingDrawSnapshot(std::uint64_t address, std::size_t bytes, VkDeviceSize* offset); mutable std::weak_ptr publishedWriteRanges; mutable bool publishedWriteRangesValid = false; }; diff --git a/core/libs/prx/libSceAgcDriver/Graphics/include/ShaderResources.hpp b/core/libs/prx/libSceAgcDriver/Graphics/include/ShaderResources.hpp index 6a3d503158..b3d8776df4 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/include/ShaderResources.hpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/include/ShaderResources.hpp @@ -33,7 +33,7 @@ void ClearCachedTextures(VkDevice device); std::uint64_t ReleaseTextureCaches(std::uint64_t bytes, bool newImage = false); std::uint64_t TextureCacheBudget(const VkPhysicalDeviceMemoryProperties& memory); std::uint64_t StorageTextureBudget(const VkPhysicalDeviceMemoryProperties& memory, const VkPhysicalDeviceMemoryBudgetPropertiesEXT* reported, std::uint64_t sampledBytes, std::uint64_t storageBytes); -std::uint64_t SampledTextureBudget(const VkPhysicalDeviceMemoryProperties& memory, const VkPhysicalDeviceMemoryBudgetPropertiesEXT* reported, std::uint64_t textureBytes); +std::uint64_t SampledTextureBudget(const VkPhysicalDeviceMemoryProperties& memory, const VkPhysicalDeviceMemoryBudgetPropertiesEXT* reported, std::uint64_t textureBytes, std::uint64_t storageBytes = 0); bool SampledBudgetReportDue(std::uint64_t reported, std::uint64_t budget, std::chrono::steady_clock::duration sinceReport); std::optional LargestDeviceLocalHeap(const VkPhysicalDeviceMemoryProperties& memory); std::uint64_t StorageTextureCacheBudget(const Context& context); diff --git a/core/libs/prx/libSceAgcDriver/Graphics/include/State.hpp b/core/libs/prx/libSceAgcDriver/Graphics/include/State.hpp index 7cf40177ed..4e7174f582 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/include/State.hpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/include/State.hpp @@ -67,6 +67,7 @@ struct ColorTarget { bool uintExport = false; std::uint32_t pipeBankXor = 0; std::uint32_t samples = 1; + std::uint32_t coverageSamples = 1; std::uint32_t dccControl = 0; std::uint64_t fmaskAddress = 0; std::uint32_t fmaskPipeBankXor = 0; @@ -98,6 +99,7 @@ struct State { std::optional depth; bool depthTest = false; bool depthWrite = false; + bool depthCopySource = false; VkCompareOp depthCompare = VK_COMPARE_OP_ALWAYS; bool depthBoundsTest = false; float minDepthBounds = 0.0f; @@ -152,6 +154,7 @@ struct State { VkProvokingVertexModeEXT provokingVertexMode = VK_PROVOKING_VERTEX_MODE_FIRST_VERTEX_EXT; VkPipelineColorBlendAttachmentState blend; std::array blendConstants; + std::uint8_t colorRop = 0xccu; std::uint32_t samples = 1; std::array sampleLocations{}; bool customSampleLocations = false; @@ -170,6 +173,9 @@ struct State { // distance vectors); a vertex program that exports pos1..pos3 depends on them. inline constexpr std::uint32_t PositionExportLayoutBits = 0xffffu | (1u << 21u) | (1u << 22u) | (1u << 23u); +inline constexpr std::uint8_t CopyRop = 0xccu; +VkLogicOp LogicOpFromRop(std::uint8_t rop); + ShaderStages DecodeShaderStages(const QueueState& queue, bool vertexWorkgroup = false); void ApplyPointSize(const State& state, ShaderRecompiler::ShaderVertexStageInfo& vertex); State DecodeState(const QueueState& queue, bool vertexWorkgroup = false); @@ -217,9 +223,7 @@ struct ColorMetadataPass { }; std::optional DecodeColorMetadataPass(const QueueState& queue); struct DepthCopyPass { - std::uint64_t depthAddress; - VkExtent2D extent; - std::uint32_t samples; + DepthTarget source; std::uint32_t sample; ColorTarget color; }; @@ -293,7 +297,7 @@ inline constexpr std::array DrawKeyRegisters{{ // PA_SU_VTX_CNTL, the sample masks, PA_SC_CONSERVATIVE_RASTERIZATION_CNTL. {RegisterBank::Context, 0x200, 8}, {RegisterBank::Context, 0x286, 2}, {RegisterBank::Context, 0x292, 2}, {RegisterBank::Context, 0x29b, 1}, {RegisterBank::Context, 0x2ab, 1}, {RegisterBank::Context, 0x2af, 1}, {RegisterBank::Context, 0x2ce, 1}, {RegisterBank::Context, 0x2d5, 2}, {RegisterBank::Context, 0x2db, 2}, {RegisterBank::Context, 0x2de, 6}, {RegisterBank::Context, 0x2f8, 2}, {RegisterBank::Context, 0x2fe, 16}, {RegisterBank::Context, 0x30e, 2}, {RegisterBank::Context, 0x313, 1}, // CB_COLOR0..7_BASE .. DCC_BASE (15 words a slot), CB_COLOR0..7_BASE_EXT, DCC_BASE_EXT, ATTRIB2, ATTRIB3. - {RegisterBank::Context, 0x318, 0x78}, {RegisterBank::Context, 0x390, 8}, {RegisterBank::Context, 0x398, 8}, {RegisterBank::Context, 0x3a8, 0x18}, + {RegisterBank::Context, 0x318, 0x78}, {RegisterBank::Context, 0x390, 8}, {RegisterBank::Context, 0x398, 0x10}, {RegisterBank::Context, 0x3a8, 0x18}, // The pixel program address, RSRC2 and user words; the geometry-back user pointer and program // address; the vertex/geometry-front RSRC1/RSRC2 and user words; the vertex program address; // the hull user pointer, program address, RSRC2 and user words; the local program address. diff --git a/core/libs/prx/libSceAgcDriver/Graphics/include/TextureDetiler.hpp b/core/libs/prx/libSceAgcDriver/Graphics/include/TextureDetiler.hpp index e0dc3531e4..d0b15ac51c 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/include/TextureDetiler.hpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/include/TextureDetiler.hpp @@ -56,7 +56,6 @@ namespace AgcDriver::Graphics { void DispatchCmaskClear(VkCommandBuffer commands, VkBuffer cmask, std::uint64_t cmaskOffset, std::size_t cmaskBytes, VkImageView view, std::uint32_t width, std::uint32_t height, std::uint32_t elementBytes, const std::array& clearWords, bool write); std::uint32_t CmaskErrors(); void DispatchMultisampleReadback(VkCommandBuffer commands, VkFormat format, VkBuffer destination, std::uint64_t offset, std::uint64_t bytes, VkImageView arrayView, std::uint32_t width, std::uint32_t height, std::uint32_t layers, std::uint32_t fragments, std::uint32_t firstElement, std::uint32_t elements); - void DispatchDepthSample(VkCommandBuffer commands, VkImageView depth, bool multisampled, VkImageView target, std::uint32_t width, std::uint32_t height, std::uint32_t sample); void DispatchMultisampleImage(VkCommandBuffer commands, std::uint32_t elementBytes, VkBuffer source, std::uint64_t offset, std::uint64_t bytes, VkImageView arrayView, std::uint32_t width, std::uint32_t height, std::uint32_t layers, std::uint32_t fragments, std::uint32_t firstElement, std::uint32_t elements); private: @@ -85,11 +84,6 @@ namespace AgcDriver::Graphics { VkPipelineLayout cmaskPipelineLayout = VK_NULL_HANDLE; VkPipeline cmaskPipeline = VK_NULL_HANDLE; DescriptorRing cmaskDescriptorSets; - VkDescriptorSetLayout depthSampleLayout = VK_NULL_HANDLE; - VkPipelineLayout depthSamplePipelineLayout = VK_NULL_HANDLE; - std::array depthSamplePipelines{}; - VkSampler depthSampler = VK_NULL_HANDLE; - DescriptorRing depthSampleSets; std::unique_ptr cmaskErrors; VkDescriptorSetLayout imageDescriptorLayout = VK_NULL_HANDLE; VkPipelineLayout imagePipelineLayout = VK_NULL_HANDLE; diff --git a/core/libs/prx/libSceAgcDriver/Graphics/include/VertexInput.hpp b/core/libs/prx/libSceAgcDriver/Graphics/include/VertexInput.hpp index aad386bfc1..85f0dbd804 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/include/VertexInput.hpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/include/VertexInput.hpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -29,13 +30,13 @@ inline bool NullVertexDescriptor(const ShaderRecompiler::VertexAttribute& attrib return fields[0] == 0 && fields[1] == 0 && fields[2] == 0 && fields[3] == 0; } -inline VertexFormat DecodeVertexFormat(const ShaderRecompiler::VertexAttribute& attribute) { +inline VertexFormat DecodeVertexFormat(const ShaderRecompiler::VertexAttribute& attribute, std::uint32_t minimumComponents = 0u) { Require(attribute.components >= 1 && attribute.components <= 4, "invalid vertex attribute component count"); if (NullVertexDescriptor(attribute)) { const std::array formats{VK_FORMAT_R32_SFLOAT, VK_FORMAT_R32G32_SFLOAT, VK_FORMAT_R32G32B32_SFLOAT, VK_FORMAT_R32G32B32A32_SFLOAT}; return {formats[attribute.components - 1], attribute.components * 4u, 4u, "f32"}; } - const auto components = attribute.formatComponents == 0u ? attribute.components : attribute.formatComponents; + const auto components = std::max(attribute.formatComponents == 0u ? attribute.components : attribute.formatComponents, minimumComponents); Require(components >= 1u && components <= 4u, "invalid vertex format component count"); const auto format = (attribute.resource.fields[3] >> 12u) & 0x7fu; switch (format) { @@ -106,11 +107,27 @@ inline std::string VertexAttributeSignature(const ShaderRecompiler::VertexAttrib return std::string(format.scalar) + (attribute.components == 1 ? "" : "x" + std::to_string(attribute.components)); } +inline std::uint32_t VertexReadBytes(const ShaderRecompiler::VertexAttribute& attribute, VkFormat host) { + for (auto components = 1u; components <= 4u; ++components) { + const auto format = DecodeVertexFormat(attribute, components); + if (format.format == host) return format.bytes; + } + throw std::runtime_error("AGC graphics: vertex format " + std::to_string(host) + " is not a form of the attribute's buffer format"); +} + struct VertexInputLayout { std::vector bindings; std::vector attributes; }; +inline std::array VertexDescriptorFlagsMessage(const ShaderRecompiler::VertexAttribute& attribute) { + std::array message{}; + const auto& fields = attribute.resource.fields; + std::snprintf(message.data(), message.size(), "unsupported vertex buffer descriptor flags at location %u (V#=[%08x %08x %08x %08x])", + attribute.location, fields[0], fields[1], fields[2], fields[3]); + return message; +} + inline VertexInputLayout BuildVertexInputLayout(const Context& context, std::span attributes) { Require(attributes.size() <= context.limits.maxVertexInputBindings && attributes.size() <= context.limits.maxVertexInputAttributes, "vertex input count exceeds device limits"); VertexInputLayout result; @@ -120,18 +137,29 @@ inline VertexInputLayout BuildVertexInputLayout(const Context& context, std::spa const auto format = DecodeVertexFormat(attribute); Require(attribute.location < context.limits.maxVertexInputAttributes && locations.insert(attribute.location).second, "invalid or duplicate vertex attribute location"); Require(attribute.fetchIndex <= 1, "unsupported vertex fetch index"); - Require((fields[1] & 0x80000000u) == 0 && (fields[3] & 0x00800000u) == 0 && (fields[3] >> 30u) == 0, "unsupported vertex buffer descriptor flags"); + if ((fields[1] & 0x80000000u) != 0 || (fields[3] & 0x00800000u) != 0 || (fields[3] >> 30u) != 0) { + Require(false, VertexDescriptorFlagsMessage(attribute).data()); + } const auto stride = (fields[1] >> 16u) & 0x3fffu; const auto address = fields[0] | (static_cast(fields[1] & 0xffffu) << 32u); Require(NullVertexDescriptor(attribute) || (address != 0 && address % format.alignment == 0 && stride % format.alignment == 0), "unaligned vertex buffer"); Require(stride <= context.limits.maxVertexInputBindingStride, "vertex stride exceeds device limits"); Require(context.formatProperties != nullptr, "missing vertex format property query"); - VkFormatProperties properties{}; - context.formatProperties(context.physical, format.format, &properties); - Require((properties.bufferFeatures & VK_FORMAT_FEATURE_VERTEX_BUFFER_BIT) != 0, "device does not support vertex format " + std::to_string(format.format)); + const auto supported = [&](VkFormat candidate) { + VkFormatProperties properties{}; + context.formatProperties(context.physical, candidate, &properties); + return (properties.bufferFeatures & VK_FORMAT_FEATURE_VERTEX_BUFFER_BIT) != 0; + }; + auto host = format.format; + auto hostSupported = supported(host); + for (auto components = 1u; !hostSupported && components <= 4u; ++components) { + host = DecodeVertexFormat(attribute, components).format; + hostSupported = supported(host); + } + Require(hostSupported, "device does not support vertex format " + std::to_string(format.format)); const auto binding = static_cast(result.bindings.size()); result.bindings.push_back({binding, stride, attribute.fetchIndex == 0 ? VK_VERTEX_INPUT_RATE_VERTEX : VK_VERTEX_INPUT_RATE_INSTANCE}); - result.attributes.push_back({attribute.location, binding, format.format, 0}); + result.attributes.push_back({attribute.location, binding, host, 0}); } return result; } diff --git a/core/libs/prx/libSceAgcDriver/Graphics/shaders/DepthSample.comp b/core/libs/prx/libSceAgcDriver/Graphics/shaders/DepthSample.comp deleted file mode 100644 index 36f1883556..0000000000 --- a/core/libs/prx/libSceAgcDriver/Graphics/shaders/DepthSample.comp +++ /dev/null @@ -1,27 +0,0 @@ -#version 450 - -layout(local_size_x = 8, local_size_y = 8) in; - -#if MULTISAMPLED -layout(set = 0, binding = 0) uniform sampler2DMS depth; -#else -layout(set = 0, binding = 0) uniform sampler2D depth; -#endif -layout(set = 0, binding = 1, r32f) writeonly uniform image2D target; - -layout(push_constant) uniform Push { - uint width; - uint height; - int sampleIndex; -} push; - -void main() { - uvec2 pixel = gl_GlobalInvocationID.xy; - if (pixel.x >= push.width || pixel.y >= push.height) return; -#if MULTISAMPLED - float value = texelFetch(depth, ivec2(pixel), push.sampleIndex).r; -#else - float value = texelFetch(depth, ivec2(pixel), 0).r; -#endif - imageStore(target, ivec2(pixel), vec4(value, 0.0, 0.0, 0.0)); -} diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp index 458af384d5..7273cb82e3 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp @@ -427,8 +427,8 @@ public: return (ResolveTextureFormat(resource.format) == expected || depthBits || UnormStencilView(resource)) && oneSlice && resource.width == target.extent.width && resource.height == target.extent.height && resource.baseLevel == 0 && resource.lastLevel == 0 && resource.baseArray == 0; } - std::shared_ptr Sampled(std::span words, const GuestTextureResource& resource, VkComponentMapping components, bool drawn = false, VkImageViewType multisampleViewType = VK_IMAGE_VIEW_TYPE_2D) { - CheckCurrent(); + std::shared_ptr Sampled(std::span words, const GuestTextureResource& resource, VkComponentMapping components, bool drawn = false, VkImageViewType multisampleViewType = VK_IMAGE_VIEW_TYPE_2D, bool copySource = false) { + if (!copySource) CheckCurrent(); CheckResource(resource); std::array key{}; std::copy_n(words.begin(), std::min(words.size(), 8), key.begin()); @@ -468,7 +468,7 @@ public: const auto sampledImage = useSampleShadow ? sampleShadow : owned; auto texture = std::make_shared(context, useSampleShadow ? sampleShadow->image : image, hostFormat, stencil ? VK_IMAGE_ASPECT_STENCIL_BIT : VK_IMAGE_ASPECT_DEPTH_BIT, components, viewType, 0, sampledImage); if (multisampled) { - Require(samplingActive, "native multisample depth sampling requires its creation-time owner"); + Require(copySource || samplingActive, "native multisample depth sampling requires its creation-time owner"); texture->MarkRefreshedPerUse(); } textures.emplace(key, texture); @@ -511,30 +511,6 @@ public: else Recorder::CountBarriers(Recorder::CommandClass::Draw, 2); } - void CopySample(std::uint32_t sample, StorageTexture& destination, VkImageView destinationView) { - auto* recorder = Recorder::Active(); - Require(recorder != nullptr, "depth sample copies need an active recorder"); - Require(sampleable, "DB depth copy from a depth surface the device cannot sample at its sample count"); - ApplyFastClear(); - if (useSampleShadow) { - if (sampleShadow == nullptr) createSampleShadow(); - refreshSampleShadow(false); - recorder->Keep(sampleShadow); - } - if (depthView == VK_NULL_HANDLE) { - VkImageViewCreateInfo viewInfo{VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO}; - viewInfo.image = useSampleShadow ? sampleShadow->image : image; - viewInfo.viewType = VK_IMAGE_VIEW_TYPE_2D; - viewInfo.format = hostFormat; - viewInfo.subresourceRange = {VK_IMAGE_ASPECT_DEPTH_BIT, 0, 1, 0, 1}; - Check(context.Function("vkCreateImageView")(context.device, &viewInfo, nullptr, &depthView), "vkCreateImageView depth sample copy"); - } - Require(context.detiler != nullptr, "depth sample copies need the texture detiler"); - context.detiler->DispatchDepthSample(recorder->Commands(), depthView, target.samples > 1, destinationView, target.extent.width, target.extent.height, sample); - Recorder::CountBarriers(Recorder::CommandClass::Copy, 2); - destination.MarkDirty(); - } - bool Holds(const Texture* texture) const { const auto held = [&](const auto& entry) { return entry.second.get() == texture; }; return std::any_of(textures.begin(), textures.end(), held) || std::any_of(stencilCopy.textures.begin(), stencilCopy.textures.end(), held); @@ -915,7 +891,6 @@ public: const VkFormat hostFormat; VkImage image = VK_NULL_HANDLE; VkImageView view = VK_NULL_HANDLE; - VkImageView depthView = VK_NULL_HANDLE; VkDeviceMemory memory = VK_NULL_HANDLE; std::shared_ptr> active = std::make_shared>(true); bool sampleable = true; @@ -1175,7 +1150,6 @@ private: releaseStencilCopy(); mirrorPlane = {}; textures.clear(); - if (depthView) context.Function("vkDestroyImageView")(context.device, depthView, nullptr); if (view) context.Function("vkDestroyImageView")(context.device, view, nullptr); if (owned != nullptr) owned.reset(); else { @@ -1218,8 +1192,8 @@ public: allocation.memoryTypeIndex = context.MemoryType(requirements.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT); memory = AllocateImageMemory(context, allocation, requirements.memoryTypeBits, "vkAllocateMemory depth plane copy"); Check(context.Function("vkBindImageMemory")(context.device, image, memory, 0), "vkBindImageMemory depth plane copy"); - stagingSize = static_cast(sliceBytes()) * layers; - staging = std::make_unique(context, stagingSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT); + imageBytes = requirements.size; + owned = std::make_shared(context, image, memory); } catch (...) { release(); throw; @@ -1238,12 +1212,7 @@ public: std::snprintf(text, sizeof(text), "AGC graphics: depth plane copy geometry does not match its layers (copy %ux%u, %u layers, %llu bytes a slice; texture %ux%u guest format %u, dimension %d, tile mode %u, %u layers, %llu bytes a slice, %zu mips)", extent.width, extent.height, layers, static_cast(sliceBytes()), resource.width, resource.height, resource.format, static_cast(resource.dimension), resource.tileMode, geometry.layers, static_cast(geometry.sliceLinearBytes), geometry.mips.size()); throw std::runtime_error(text); } - const auto stagingBytes = static_cast(geometry.sliceLinearBytes) * layers; - if (stagingBytes > stagingSize) { - retiredStaging.push_back(std::move(staging)); - staging = std::make_unique(context, stagingBytes, VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT); - stagingSize = stagingBytes; - } + const auto stagingBytes = std::max(static_cast(geometry.sliceLinearBytes), static_cast(sliceBytes())) * layers; std::vector> uploads; std::vector uploadLayers; std::vector copiedLayers; @@ -1269,11 +1238,14 @@ public: uploadLayers.push_back(layer); held = current; } - if (!copiedLayers.empty() || !uploads.empty()) record(slices, resource, geometry, copiedLayers, uploads, uploadLayers); + if (!copiedLayers.empty() || !uploads.empty()) { + if (staging == nullptr || stagingBytes > staging->Size()) staging = std::make_shared(context, stagingBytes, VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT); + record(slices, resource, geometry, copiedLayers, uploads, uploadLayers, staging); + } const std::array key{static_cast(components.r), static_cast(components.g), static_cast(components.b), static_cast(components.a), static_cast(viewType), baseLayer}; auto& texture = textures[key]; if (texture == nullptr) { - texture = std::make_shared(context, image, format, aspect(), components, viewType, baseLayer); + texture = std::make_shared(context, image, format, aspect(), components, viewType, baseLayer, owned); texture->MarkRefreshedPerUse(); } return texture; @@ -1283,6 +1255,9 @@ public: const VkExtent2D extent; const VkFormat format; const std::uint32_t layers; + VkDeviceSize imageBytes = 0; + std::uint64_t lastUse = 0; + VkDeviceSize HeldBytes() const { return imageBytes + (staging != nullptr ? staging->Size() : 0u); } private: struct GuestLayer { @@ -1295,15 +1270,14 @@ private: VkImage image = VK_NULL_HANDLE; VkDeviceMemory memory = VK_NULL_HANDLE; - std::unique_ptr staging; - std::size_t stagingSize = 0; - std::vector> retiredStaging; + std::shared_ptr owned; + std::shared_ptr staging; std::map, std::shared_ptr> textures; std::vector guestLayers; std::vector surfaceLayers; bool filled = false; - void record(std::span slices, const GuestTextureResource& resource, const SurfaceGeometry& geometry, std::span copiedLayers, std::span> uploads, std::span uploadLayers) { + void record(std::span slices, const GuestTextureResource& resource, const SurfaceGeometry& geometry, std::span copiedLayers, std::span> uploads, std::span uploadLayers, const std::shared_ptr& staging) { if (!uploads.empty()) { Require(context.detiler != nullptr, "depth plane copy requires a texture detiler"); context.detiler->BeginBatch(); @@ -1360,7 +1334,11 @@ private: barrier(commands, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, 0, 0, nullptr, 0, nullptr, 1, &toGeneral); filled = true; if (batch) batch->SubmitAndWait(); - else Recorder::CountBarriers(Recorder::CommandClass::Draw, 3); + else { + Recorder::CountBarriers(Recorder::CommandClass::Draw, 3); + recorder->Keep(staging, staging->Size()); + if (owned != nullptr) recorder->Keep(owned); + } } VkDeviceSize sliceBytes() const { @@ -1374,8 +1352,11 @@ private: void release() noexcept { textures.clear(); staging.reset(); - if (image) context.Function("vkDestroyImage")(context.device, image, nullptr); - if (memory) context.Function("vkFreeMemory")(context.device, memory, nullptr); + if (owned != nullptr) owned.reset(); + else { + if (image) context.Function("vkDestroyImage")(context.device, image, nullptr); + if (memory) context.Function("vkFreeMemory")(context.device, memory, nullptr); + } image = VK_NULL_HANDLE; memory = VK_NULL_HANDLE; } @@ -1386,6 +1367,32 @@ std::map& keep) { + static const VkDeviceSize budget = [] { + const char* text = std::getenv("APS5_DEPTH_PLANE_COPY_MIB"); + return static_cast(text != nullptr ? std::strtoull(text, nullptr, 10) : 256ull) << 20u; + }(); + if (budget == 0) return; + auto& copies = planeCopies(); + VkDeviceSize total = 0; + for (const auto& entry : copies) total += entry.second->HeldBytes(); + while (total > budget) { + auto oldest = copies.end(); + for (auto it = copies.begin(); it != copies.end(); ++it) { + if (it->first == keep) continue; + if (oldest == copies.end() || it->second->lastUse < oldest->second->lastUse) oldest = it; + } + if (oldest == copies.end()) break; + total -= oldest->second->HeldBytes(); + copies.erase(oldest); + } +} + struct LayeredSlice { DepthSurface* surface; std::uint32_t layer; @@ -2098,6 +2105,12 @@ std::vector> FlushDepthPlanes(std::uint6 const bool overlapped = !surface->syncing && plane < end && address < plane + planeBytes; if (overlapped && (surface->pendingClear & VK_IMAGE_ASPECT_DEPTH_BIT) != 0) surface->MarkAllStale(planeBytes); if (overlapped && (scope == DepthPlaneScope::Whole || anyUnit(surface->StaleUnitsIn(planeBytes, address, end)))) { + if (GuestMemory::CommittedRanges(plane, static_cast(planeBytes)).empty()) { + static std::atomic reported{false}; + if (!reported.exchange(true)) std::fprintf(stderr, "[gpu] depth plane 0x%llx+0x%llx was unmapped with GPU results pending; they are dropped\n", static_cast(plane), static_cast(planeBytes)); + surface->ClearStale(); + continue; + } surface->SyncFromMemory(resource); auto units = scope == DepthPlaneScope::Whole ? surface->staleUnits : surface->StaleUnitsIn(planeBytes, address, end); if (anyUnit(units)) { @@ -2311,6 +2324,16 @@ void ClearDepthSurfaces(VkDevice device) { UpdateResidentImageUnmapObserver_nid_no_patch(); } +std::size_t DepthPlaneCopyCount() { + return planeCopies().size(); +} + +namespace { + +thread_local bool depthCopySourceActive = false; + +} + std::shared_ptr DepthSurfaceTexture(const Context& context, std::span words, const GuestTextureResource& resource, VkComponentMapping components, VkImageViewType multisampleViewType) { const auto held = [&](const auto& surface) { return surface->active->load(std::memory_order_acquire) && surface->context.device == context.device && !surface->retired && (surface->target.address == resource.baseAddress || (surface->target.stencilAddress != 0 && surface->target.stencilAddress == resource.baseAddress)); @@ -2331,12 +2354,17 @@ std::shared_ptr DepthSurfaceTexture(const Context& context, std::span 1u, "multisample reader overlaps a single-sample depth attachment"); auto* recorder = RecorderFor(context); Require(recorder != nullptr, "a native multisampled depth target is sampled only by recorded draws and dispatches"); - found->CheckCurrent(); - found->CheckResource(resource); - Require(found->pendingClear == 0u, "native multisample depth fast-clear transition is not implemented"); + if (depthCopySourceActive) { + found->CheckResource(resource); + found->ApplyFastClear(); + } else { + found->CheckCurrent(); + found->CheckResource(resource); + Require(found->pendingClear == 0u, "native multisample depth fast-clear transition is not implemented"); + } RecordMemoryBarrier(context, recorder->Commands(), VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT | VK_ACCESS_TRANSFER_WRITE_BIT, VK_ACCESS_SHADER_READ_BIT); Recorder::CountBarriers(Recorder::CommandClass::Draw, 1); - return found->Sampled(words, resource, components, found == drawnDepth, multisampleViewType); + return found->Sampled(words, resource, components, found == drawnDepth, multisampleViewType, depthCopySourceActive); } if (resource.width != base.extent.width || resource.height != base.extent.height || resource.baseLevel != 0) return nullptr; const bool d16 = base.format == VK_FORMAT_D16_UNORM || base.format == VK_FORMAT_D16_UNORM_S8_UINT; @@ -2372,11 +2400,14 @@ std::shared_ptr DepthSurfaceTexture(const Context& context, std::spanimage, slice == drawnDepth ? 0u : slice->written}); } const auto imageFormat = integer ? VK_FORMAT_R16_UINT : d16 ? VK_FORMAT_D16_UNORM : VK_FORMAT_D32_SFLOAT; - auto& copy = planeCopies()[{context.device, base.address, base.extent.width, base.extent.height, imageFormat, layers}]; - if (copy == nullptr) { - copy = std::make_unique(context, base.extent, imageFormat, layers); - } - return copy->Refresh(slices, resource, components, cube || resource.dimension == TextureDimension::k2DArray ? VK_IMAGE_VIEW_TYPE_2D_ARRAY : VK_IMAGE_VIEW_TYPE_2D, layered && !cube ? resource.baseArray : 0u); + auto& copies = planeCopies(); + const std::tuple copyKey{context.device, base.address, base.extent.width, base.extent.height, imageFormat, layers}; + auto& copy = copies[copyKey]; + if (copy == nullptr) copy = std::make_unique(context, base.extent, imageFormat, layers); + copy->lastUse = ++planeCopyClock(); + auto texture = copy->Refresh(slices, resource, components, cube || resource.dimension == TextureDimension::k2DArray ? VK_IMAGE_VIEW_TYPE_2D_ARRAY : VK_IMAGE_VIEW_TYPE_2D, layered && !cube ? resource.baseArray : 0u); + evictPlaneCopies(copyKey); + return texture; } bool DepthSurfaceHolds(const Context& context, const GuestTextureResource& resource, const Texture* texture) { @@ -2387,7 +2418,7 @@ bool DepthSurfaceHolds(const Context& context, const GuestTextureResource& resou std::lock_guard lock(surfacesMutex()); auto* found = planeSurface(context.device, address); if (found == nullptr || !found->Holds(texture)) return false; - found->CheckCurrent(); + if (!depthCopySourceActive) found->CheckCurrent(); found->CheckResource(resource); return true; } @@ -2522,15 +2553,23 @@ std::shared_ptr DepthSurfaceStorage(const Context& context, std: return layeredDepthStorage(context, surface, resource, mip); } -void CopyDepthSample(const Context& context, std::uint64_t depthAddress, std::uint32_t samples, std::uint32_t sample, StorageTexture& destination, VkImageView destinationView, VkExtent2D extent) { - std::lock_guard lock(surfacesMutex()); - auto* found = planeSurface(context.device, depthAddress); - Require(found != nullptr && found->target.address == depthAddress, "DB depth copy from a depth plane no draw rendered"); - Require(found->target.format == VK_FORMAT_D32_SFLOAT || found->target.format == VK_FORMAT_D32_SFLOAT_S8_UINT, "DB depth copy from a depth surface that is not 32-bit float"); - Require(found->target.samples == samples, "DB depth copy from a depth surface of another sample count"); - Require(found->target.extent.width == extent.width && found->target.extent.height == extent.height, "DB depth copy from a depth surface of another size"); - Require(sample < found->target.samples, "DB depth copy of a sample the depth surface does not have"); - found->CopySample(sample, destination, destinationView); +DepthCopySourceScope::DepthCopySourceScope(bool active) : previous(depthCopySourceActive) { + depthCopySourceActive = active; +} + +DepthCopySourceScope::~DepthCopySourceScope() { + depthCopySourceActive = previous; +} + +std::array DepthCopySourceWords(const DepthTarget& target) { + const bool multisampled = target.samples != 1u; + const auto base = (target.address | target.pipeBankXor) >> 8u; + const auto metadata = multisampled ? target.htileAddress >> 8u : 0u; + const auto count = static_cast(std::countr_zero(target.samples)); + const auto width = target.extent.width - 1u, height = target.extent.height - 1u; + const auto type = multisampled ? 14u : 9u; + const auto word6 = (multisampled && target.iterate256 ? 1u << 10u : 0u) | (multisampled && target.htilePipeAligned ? 1u << 19u : 0u) | (metadata != 0 ? 1u << 21u : 0u) | (static_cast(metadata & 255u) << 24u); + return {static_cast(base), static_cast((base >> 32u) & 255u) | (22u << 20u) | ((width & 3u) << 30u), (width >> 2u) | (height << 14u), (type << 28u) | (target.depthSwizzle << 20u) | (count << 16u) | 0x204u, 0u, count << 4u, word6, static_cast(metadata >> 8u)}; } bool DepthSurfaceStoreBack(const Context& context, const StorageTexture& storage) { diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp index b34f12db41..a91fc9d2ff 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp @@ -16,6 +16,7 @@ #include "prx/libSceAgcDriver/Execution/include/CaptureTrace.hpp" #include "prx/libSceAgcDriver/Execution/include/PerformanceTimer.hpp" #include "prx/libSceAgcDriver/Execution/include/Mutex.hpp" +#include "prx/libSceAgcDriver/Execution/include/ProfileOutput.hpp" #include "ThreadOwned.hpp" #include "prx/libc/include/General.hpp" #include "prx/libc/include/GuestAllocations.hpp" @@ -408,6 +409,19 @@ CmaskState readCmask(const ColorTarget& color, const CmaskLayout& layout, std::v std::vector mask(bytes); GuestMemory::Read(address, mask, CmaskLayout::Alignment); const auto uniform = [&](std::byte value) { return std::all_of(mask.begin(), mask.end(), [&](std::byte entry) { return entry == value; }); }; + if (color.coverageSamples > color.samples) { + for (std::uint32_t tileY = 0; tileY < layout.TilesY(); ++tileY) { + for (std::uint32_t tileX = 0; tileX < layout.TilesX(); ++tileX) { + const auto nibble = layout.Nibble(tileX, tileY); + const auto code = (std::to_integer(mask[nibble / 2u]) >> ((nibble % 2u) * 4u)) & 0xfu; + if (code != 0u && (code >> 2u) != 3u) { + char text[128]; + std::snprintf(text, sizeof(text), "AGC graphics: CMASK code 0x%x of a one-fragment color target is not implemented", code); + throw std::runtime_error(text); + } + } + } + } if (uniform(std::byte{0xff})) return CmaskState::Expanded; if (uniform(std::byte{0})) return CmaskState::Cleared; for (std::uint32_t tileY = 0; tileY < layout.TilesY(); ++tileY) { @@ -464,6 +478,10 @@ bool materializeCmaskClear(const Context& context, const ColorTarget& color, con const auto bytes = layout.Bytes(); Require(color.cmaskBytes == bytes, "CMASK size differs from the color target's CMASK layout"); if (cmaskProved(color.cmaskAddress, bytes)) return false; + if (color.coverageSamples > color.samples) { + std::vector codes; + static_cast(readCmask(color, layout, codes)); + } auto* recorder = Recorder::Active(); if (resident != nullptr && context.detiler != nullptr && recorder != nullptr && GuestMemory::GpuMutex().HeldByThisThread()) { const auto* import = HostImportFor(context, color.cmaskAddress, bytes); @@ -536,7 +554,7 @@ constexpr std::array DrawPhaseNames{"validate", "vertex enum SyncReason : std::size_t { SyncNone, SyncNotResident, SyncNoRecorder, SyncDisabled, SyncCopiedWrites, SyncLease, SyncCount }; constexpr std::array SyncReasonNames{"none", "non-resident target", "no recorder", "disabled", "copied writes", "lease"}; constexpr std::size_t IndirectPathCount = static_cast(IndirectDrawPath::Count); -constexpr std::array IndirectDrawPathNames{"gpu-side", "patched SGPR not folded", "fetch offset unknown", "non-vertex path", "GE_INDX_OFFSET", "draw index", "vertex range too large", "feature gap", "pending image results", "pending label or copied write", "not imported", "disabled", "index range not fully readable"}; +constexpr std::array IndirectDrawPathNames{"gpu-side", "patched SGPR not folded", "fetch offset unknown", "non-vertex path", "GE_INDX_OFFSET", "draw index", "vertex range too large", "feature gap", "pending image results", "pending label or copied write", "not imported", "disabled", "index range not fully readable", "index limit unknown"}; // What became of one draw, for the totals. struct DrawOutcome { @@ -763,8 +781,8 @@ void reportDraw(const std::array& us, const ShaderResources: for (std::size_t i = 1; i < profile.recipeMisses.size() && room(); ++i) { n += std::snprintf(line + n, sizeof(line) - static_cast(n), " %s %llu", DrawRecipeMissName(static_cast(i)), static_cast(profile.recipeMisses[i])); } - std::fprintf(stderr, "%s\n", line); - std::fprintf(stderr, "[rescache] draws: %llu hits, %llu misses, %llu invalidated, %llu uncacheable; validation memo %llu hits / %llu misses (which key words the misses differ in: the miss churn line)\n", static_cast(profile.cacheHits), static_cast(profile.cacheMisses), static_cast(profile.cacheInvalidated), static_cast(profile.uncacheable), static_cast(profile.validateHits), static_cast(profile.validateMisses)); + AgcDriver::ProfilePrint_nid_no_patch("%s\n", line); + AgcDriver::ProfilePrint_nid_no_patch("[rescache] draws: %llu hits, %llu misses, %llu invalidated, %llu uncacheable; validation memo %llu hits / %llu misses (which key words the misses differ in: the miss churn line)\n", static_cast(profile.cacheHits), static_cast(profile.cacheMisses), static_cast(profile.cacheInvalidated), static_cast(profile.uncacheable), static_cast(profile.validateHits), static_cast(profile.validateMisses)); profile.totalsUs.fill(0); profile.maxUs.fill(0); profile.maxDrawUs = profile.hookWaitUs = profile.maxHookWaitUs = profile.ownSyncUs = 0; @@ -1278,8 +1296,8 @@ bool StripPatches(const State& state, std::span shaders) { return state.topology == VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP && BarycentricPatches(state, shaders); } -std::shared_ptr zeroVertexBuffer(const Context& context, const ShaderRecompiler::VertexAttribute& attribute) { - auto zero = std::make_shared(context, DecodeVertexFormat(attribute).bytes, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT); +std::shared_ptr zeroVertexBuffer(const Context& context, std::size_t bytes) { + auto zero = std::make_shared(context, bytes, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT); std::fill(zero->Bytes().begin(), zero->Bytes().end(), std::byte{0}); return zero; } @@ -1524,14 +1542,19 @@ DrawInputs prepareDrawInputs(const Context& context, const State& state, const P fetchValid.reserve(attributes.size()); std::vector fetchOf(attributes.size(), 0); std::vector> zeroed(attributes.size()); + const auto& layout = inputs.VertexLayout(); for (std::size_t i = 0; i < attributes.size(); ++i) { const auto& attribute = attributes[i]; + const auto described = std::find_if(layout.attributes.begin(), layout.attributes.end(), [&](const auto& entry) { return entry.location == attribute.location; }); + Require(described != layout.attributes.end(), "vertex attribute missing from its input layout"); + const auto narrow = DecodeVertexFormat(attribute); + const std::size_t widened = described->format == narrow.format ? 0u : VertexReadBytes(attribute, described->format) - narrow.bytes; if (NullVertexDescriptor(attribute) || VertexFetchOutOfRange(attribute)) { - zeroed[i] = zeroVertexBuffer(context, attribute); + zeroed[i] = zeroVertexBuffer(context, narrow.bytes + widened); continue; } // An indirect draw's counts are unknown here: the descriptor's whole range is copied. - const auto bytes = args != nullptr ? VertexBufferExtent(attribute) : VertexBufferReadSize(attribute, inputs.maxIndex, draw.instanceCount, draw.firstInstance); + const auto bytes = (args != nullptr ? VertexBufferExtent(attribute) : VertexBufferReadSize(attribute, inputs.maxIndex, draw.instanceCount, draw.firstInstance)) + widened; const auto& fields = attribute.resource.fields; const auto address = fields[0] | (static_cast(fields[1] & 0xffffu) << 32u); const auto firstByte = args == nullptr ? VertexBufferReadOffset(attribute, draw.firstVertex, draw.firstInstance) : 0; @@ -1712,16 +1735,24 @@ struct IndirectRecord { // The draw commands of one draw: the vertex and index buffer binds, then the direct draw, the // GPU-side indirect draw from `argumentBuffer` or the CPU-read records with the driver's rules. -void recordDrawCommands(const Context& context, VkCommandBuffer commands, const State& state, const Pm4::DrawParameters& draw, const DrawInputs& inputs, const IndirectRecord* indirect, VkBuffer argumentBuffer, VkDeviceSize argumentOffset, std::uint32_t firstIndex = 0) { +void recordDrawCommands(const Context& context, VkCommandBuffer commands, const State& state, const Pm4::DrawParameters& draw, const DrawInputs& inputs, const IndirectRecord* indirect, VkBuffer argumentBuffer, VkDeviceSize argumentOffset, const DrawTraceContext* trace, std::uint32_t& subdraw, std::uint32_t firstIndex = 0) { if (context.recorder != nullptr) context.recorder->NoteSampledDraw(commands); + const auto recorded = [&](std::uint32_t index) { + if (trace != nullptr && context.recorder != nullptr) { + context.recorder->NoteDrawTraceToken(trace->token); + CaptureTrace::Log("draw-recorded token=%llu batch=%llu submission=%llu queue=%u offset=%zu target=%llx subdraw=%u", static_cast(trace->token), static_cast(context.recorder->OpenBatchSerial()), static_cast(trace->submission), trace->queue, trace->offset, static_cast(trace->target), index); + } + }; const auto* args = indirect != nullptr ? indirect->args : nullptr; if (state.stages.mesh && args != nullptr) { context.Function("vkCmdDrawMeshTasksIndirectEXT")(commands, argumentBuffer, argumentOffset, 1, sizeof(VkDrawMeshTasksIndirectCommandEXT)); + recorded(subdraw++); return; } if (state.stages.mesh) { APS5_LOG_OUT_DEBUG("vkCmdDrawMeshTasksEXT groups=%u instances=%u", inputs.meshGroups, draw.instanceCount); context.Function("vkCmdDrawMeshTasksEXT")(commands, inputs.meshGroups, draw.instanceCount, 1); + recorded(subdraw++); return; } if (!inputs.vertexHandles.empty()) context.Resolved(&DeviceFunctions::cmdBindVertexBuffers, "vkCmdBindVertexBuffers")(commands, 0, static_cast(inputs.vertexHandles.size()), inputs.vertexHandles.data(), inputs.vertexOffsets.data()); @@ -1732,8 +1763,13 @@ void recordDrawCommands(const Context& context, VkCommandBuffer commands, const } if (draw.indexed) context.Resolved(&DeviceFunctions::cmdBindIndexBuffer, "vkCmdBindIndexBuffer")(commands, inputs.indexHandle, inputs.indexOffset, draw.indexSize == 2 ? VK_INDEX_TYPE_UINT16 : VK_INDEX_TYPE_UINT32); if (args == nullptr) { - if (draw.indexed) context.Resolved(&DeviceFunctions::cmdDrawIndexed, "vkCmdDrawIndexed")(commands, draw.indexCount, draw.instanceCount, firstIndex, static_cast(draw.firstVertex), draw.firstInstance); - else context.Resolved(&DeviceFunctions::cmdDraw, "vkCmdDraw")(commands, draw.indexCount, draw.instanceCount, draw.firstVertex, draw.firstInstance); + if (draw.indexed) { + context.Resolved(&DeviceFunctions::cmdDrawIndexed, "vkCmdDrawIndexed")(commands, draw.indexCount, draw.instanceCount, firstIndex, static_cast(draw.firstVertex), draw.firstInstance); + recorded(subdraw++); + } else { + context.Resolved(&DeviceFunctions::cmdDraw, "vkCmdDraw")(commands, draw.indexCount, draw.instanceCount, draw.firstVertex, draw.firstInstance); + recorded(subdraw++); + } } else if (indirect->path == IndirectDrawPath::Gpu) { if (args->countIndirect) { if (draw.indexed) context.Function("vkCmdDrawIndexedIndirectCountKHR")(commands, argumentBuffer, argumentOffset, indirect->countImport->buffer, HostImportOffset(*indirect->countImport, args->countAddress), args->count, args->stride); @@ -1986,6 +2022,7 @@ void recordStencilReplay(const Context& context, VkCommandBuffer commands, const const auto referenceOffset = fragment->pushConstantOffset + fragment->program->stencilReferenceDword * sizeof(std::uint32_t); Require(referenceOffset <= block.size() - sizeof(std::uint32_t), "stencil replay reference exceeds the push block"); const auto run = [&](const Pm4::DrawParameters& piece, std::uint32_t firstIndex) { + std::uint32_t unusedSubdraw = 0; for (const auto& [reference, pipeline] : replay) { selected.stencilReplayReference = reference; selected.stencilFront.reference = reference; @@ -1993,7 +2030,7 @@ void recordStencilReplay(const Context& context, VkCommandBuffer commands, const context.Resolved(&DeviceFunctions::cmdSetStencilReference, "vkCmdSetStencilReference")(commands, VK_STENCIL_FACE_FRONT_AND_BACK, reference); std::memcpy(block.data() + referenceOffset, &reference, sizeof(reference)); pipeline->PushConstants(commands, stages, block); - recordDrawCommands(context, commands, selected, piece, inputs, nullptr, VK_NULL_HANDLE, 0, firstIndex); + recordDrawCommands(context, commands, selected, piece, inputs, nullptr, VK_NULL_HANDLE, 0, nullptr, unusedSubdraw, firstIndex); } }; if (replay.size() == 1u) { @@ -2126,7 +2163,8 @@ bool WriteOnlyPasses() { return enabled; } -void recordDraw(const Context& context, const State& state, const Pm4::DrawParameters& draw, std::span shaders, DrawInputs& inputs, RecordedDraw& record, DrawOutcome& outcome, DrawTimer& timer, double& ownWaitedMs) { +void recordDraw(const Context& context, const State& state, const Pm4::DrawParameters& draw, std::span shaders, DrawInputs& inputs, RecordedDraw& record, DrawOutcome& outcome, DrawTimer& timer, double& ownWaitedMs, const DrawTraceContext* trace = nullptr) { + std::uint32_t subdraw = 0; auto* recorder = record.recorder; auto& resources = *record.resources; const auto* args = record.indirect != nullptr ? record.indirect->args : nullptr; @@ -2285,18 +2323,18 @@ void recordDraw(const Context& context, const State& state, const Pm4::DrawParam APS5_LOG_CHARS_OUT_DEBUG("Resources bound"); pushDrawConstants(*record.pipeline, commands, state, draw, shaders, resources, record.pushBytes, record.pushStages, meshArguments != nullptr ? meshArguments->DeviceAddress() : 0, !inputs.restartTable.empty()); APS5_LOG_CHARS_OUT_DEBUG("Push constants recorded"); - if (record.stencilReplay.empty()) recordDrawCommands(context, commands, state, draw, inputs, record.indirect, argumentBuffer, argumentOffset); + if (record.stencilReplay.empty()) recordDrawCommands(context, commands, state, draw, inputs, record.indirect, argumentBuffer, argumentOffset, trace, subdraw); else { recordStencilReplay(context, commands, state, draw, inputs, record.stencilReplay, shaders, resources, record.pushBytes, record.pushStages); for (const auto& [reference, pipeline] : record.stencilReplay) recorder->Keep(pipeline); } if (record.pipeline->SplitsFaces()) { record.pipeline->ContinueBackFaces(commands, state); - recordDrawCommands(context, commands, state, draw, inputs, record.indirect, argumentBuffer, argumentOffset); + recordDrawCommands(context, commands, state, draw, inputs, record.indirect, argumentBuffer, argumentOffset, trace, subdraw); } if (record.pipeline->SplitsSecondChannel()) { record.pipeline->ContinueSecondChannel(commands); - recordDrawCommands(context, commands, state, draw, inputs, record.indirect, argumentBuffer, argumentOffset); + recordDrawCommands(context, commands, state, draw, inputs, record.indirect, argumentBuffer, argumentOffset, trace, subdraw); } if (meshArguments != nullptr) recorder->Keep(meshArguments); if (args != nullptr) CountIndirectDraw(record.indirect->path, record.indirect->readMs, rewritten); @@ -2411,6 +2449,7 @@ struct PreparedDraw { PreparedDraw(const PreparedDraw&) = delete; PreparedDraw& operator=(const PreparedDraw&) = delete; std::shared_ptr* recipeOut = nullptr; + const DrawTraceContext* trace = nullptr; std::vector stencilReferences; std::optional timing; DrawTimer timer; @@ -2519,6 +2558,7 @@ bool prepareDraw(const Context& context, const State& state, const Pm4::DrawPara const ColorTargetLayout colorLayout(color.extent.width, color.extent.height, color.tileMode, color.elementBytes); static_cast(FlushDepthPlanes(color.address, colorLayout.Bytes(), DepthPlaneScope::Whole)); RetireDepthSurfaces(context.device, color.address, colorLayout.Bytes()); + if (color.fmaskAddress != 0) GuardOneFragmentFmask(context, color); constexpr VkBufferUsageFlags copies = VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT; timer.phase(PhaseSetup); // Debug aid: APS5_NO_RESIDENT_TARGETS=1 copies every target in and out again. @@ -2881,7 +2921,7 @@ void recordPreparedDraw(const Context& context, const State& state, const Pm4::D record.reordered = reordered; record.reorderPass = reorderPass; record.depthLocations = depthLocations; - recordDraw(context, state, draw, shaders, inputs, record, outcome, timer, ownWaitedMs); + recordDraw(context, state, draw, shaders, inputs, record, outcome, timer, ownWaitedMs, prepared.trace); // The recipe for the caller's draw-cache entry (design_cpu_final M8, rule R3 for draws): // only a template the resource cache serves under this content key (reusable: no lease, // no copied writes, every direct region import- or mirror-served) of a direct draw, so a @@ -3017,18 +3057,19 @@ void recordPreparedDraw(const Context& context, const State& state, const Pm4::D APS5_LOG_CHARS_OUT_DEBUG("Resources bound"); pushDrawConstants(*pipeline, commands, state, draw, shaders, *resources, nullptr, 0, meshArguments != nullptr ? meshArguments->DeviceAddress() : 0, !inputs.restartTable.empty()); APS5_LOG_CHARS_OUT_DEBUG("Push constants recorded"); - if (stencilReplay.empty()) recordDrawCommands(context, commands, state, draw, inputs, args != nullptr ? &indirect : nullptr, argumentBuffer, argumentOffset); + std::uint32_t subdraw = 0; + if (stencilReplay.empty()) recordDrawCommands(context, commands, state, draw, inputs, args != nullptr ? &indirect : nullptr, argumentBuffer, argumentOffset, prepared.trace, subdraw); else { recordStencilReplay(context, commands, state, draw, inputs, stencilReplay, shaders, *resources); if (recorded) for (const auto& [reference, selectedPipeline] : stencilReplay) recorder->Keep(selectedPipeline); } if (pipeline->SplitsFaces()) { pipeline->ContinueBackFaces(commands, state); - recordDrawCommands(context, commands, state, draw, inputs, args != nullptr ? &indirect : nullptr, argumentBuffer, argumentOffset); + recordDrawCommands(context, commands, state, draw, inputs, args != nullptr ? &indirect : nullptr, argumentBuffer, argumentOffset, prepared.trace, subdraw); } if (pipeline->SplitsSecondChannel()) { pipeline->ContinueSecondChannel(commands); - recordDrawCommands(context, commands, state, draw, inputs, args != nullptr ? &indirect : nullptr, argumentBuffer, argumentOffset); + recordDrawCommands(context, commands, state, draw, inputs, args != nullptr ? &indirect : nullptr, argumentBuffer, argumentOffset, prepared.trace, subdraw); } if (meshArguments != nullptr && recorded) recorder->Keep(meshArguments); if (args != nullptr) CountIndirectDraw(indirect.path, indirect.readMs, rewritten); @@ -3185,14 +3226,17 @@ void recordPreparedDraw(const Context& context, const State& state, const Pm4::D } -void Draw(const Context& context, const State& state, const Pm4::DrawParameters& draw, std::span shaders, std::span snapshots, std::shared_ptr* recipeOut) { +void Draw(const Context& context, const State& state, const Pm4::DrawParameters& draw, std::span shaders, std::span snapshots, std::shared_ptr* recipeOut, const DrawTraceContext* trace) { + const DepthCopySourceScope depthCopySource(state.depthCopySource); static const bool profile = std::getenv("APS5_PROFILE_DRAW") != nullptr; PreparedDraw prepared(profile); prepared.recipeOut = recipeOut; + prepared.trace = trace; if (prepareDraw(context, state, draw, shaders, snapshots, prepared)) recordPreparedDraw(context, state, draw, shaders, prepared); } -DrawRecipeOutcome DrawWithRecipe(const Context& context, const State& state, const Pm4::DrawParameters& draw, std::span shaders, std::span snapshots, const DrawRecipe& recipe) { +DrawRecipeOutcome DrawWithRecipe(const Context& context, const State& state, const Pm4::DrawParameters& draw, std::span shaders, std::span snapshots, const DrawRecipe& recipe, const DrawTraceContext* trace) { + const DepthCopySourceScope depthCopySource(state.depthCopySource); if (context.recorder != nullptr) context.recorder->BoundKeptBytes(); static_cast(snapshots); PerformanceTimer timing("Graphics.DrawWithRecipe"); @@ -3233,6 +3277,7 @@ DrawRecipeOutcome DrawWithRecipe(const Context& context, const State& state, con targets.reserve(recipe.targets.size()); for (std::size_t index = 0; index < recipe.targets.size(); ++index) { timer.phase(PhaseSetup); + if (state.colors[index].fmaskAddress != 0) GuardOneFragmentFmask(context, state.colors[index]); auto stored = recipe.targets[index].lock(); if (stored == nullptr || !StorageImageCached(context, stored.get()) || !StorageImageServesKeys(*stored, state.colors[index].dccAddress)) return miss(DrawRecipeMiss::TargetGone); auto resident = refreshResidentTarget(context, state, state.colors[index], outcome, profile, [&] { @@ -3300,7 +3345,7 @@ DrawRecipeOutcome DrawWithRecipe(const Context& context, const State& state, con record.pushBytes = &recipe.pushBytes; record.pushStages = recipe.pushStages; record.depthLocations = passDepthLocations(context, state, *record.pipeline); - recordDraw(context, state, draw, shaders, inputs, record, outcome, timer, ownWaitedMs); + recordDraw(context, state, draw, shaders, inputs, record, outcome, timer, ownWaitedMs, trace); if (profile) { result.recordUs = std::chrono::duration(std::chrono::steady_clock::now() - recordStart).count(); auto& stats = Profile(); @@ -3323,12 +3368,6 @@ std::shared_ptr metadataPassResident(const Context& context, con } -void RunDepthCopyPass(const Context& context, const DepthCopyPass& pass) { - Require(pass.color.tileMode != ColorTileMode::Linear && context.detiler != nullptr, "DB depth copy into a target without a resident image is not implemented"); - const auto resident = CachedStorageSurface(context, SurfaceForTarget(pass.color)); - CopyDepthSample(context, pass.depthAddress, pass.samples, pass.sample, *resident, resident->StorageView(0, false), pass.extent); -} - std::vector> PipelinedColorMetadataPassWrites(const ColorMetadataPass& pass) { std::vector> writes; if (pass.mode == ColorMetadataPass::Mode::Resolve) { diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp index 1b20040ba3..12939ea058 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp @@ -1365,7 +1365,7 @@ ImportRun joinedRun(const GuestAllocations::Lease& lease, std::uint64_t begin, s auto run = crossedRun(lease, begin, end); if (run.ranges.empty()) return run; if (std::all_of(run.ranges.begin(), run.ranges.end(), [window](const auto& range) { return range->bytes > window; })) return run; - if (begin == run.base && end == run.base + run.bytes) return run; + if (begin == run.base) return run; return {}; } @@ -1531,16 +1531,24 @@ std::atomic importMaxBytes{0}; // The import of an access to a registered range larger than the range window: the cells the access touches, grown to // the union of the range's imports it overlaps, so accesses across neighbouring cell edges share one import instead of // replacing each other's. -ImportRun windowRun(const HostImports& state, ImportRun run, std::uint64_t begin, std::uint64_t end, std::uint64_t alignment) { +std::optional> importWindowBounds(const GuestAllocations::Range& range, std::uint64_t begin, std::uint64_t end, std::uint64_t alignment) { const auto window = rangeImportWindow.load(std::memory_order_relaxed); - if (run.ranges.size() != 1 || window == 0 || alignment == 0 || window % alignment != 0) return run; - const auto& range = *run.ranges.front(); - if (range.bytes <= window || begin < range.address || end > range.address + range.bytes || end <= begin) return run; + if (window == 0 || alignment == 0 || window % alignment != 0 || range.bytes <= window || begin < range.address || end > range.address + range.bytes || end <= begin) return std::nullopt; const auto lastCell = range.bytes / window - 1; const auto cell = [&](std::uint64_t address) { return std::min((address - range.address) / window, lastCell); }; - auto windowBegin = range.address + cell(begin) * window; + const auto windowBegin = range.address + cell(begin) * window; const auto endCell = cell(end - 1); - auto windowEnd = endCell == lastCell ? range.address + range.bytes : range.address + (endCell + 1) * window; + const auto windowEnd = endCell == lastCell ? range.address + range.bytes : range.address + (endCell + 1) * window; + if (windowBegin % alignment != 0 || (windowEnd - windowBegin) % alignment != 0) return std::nullopt; + return std::pair{windowBegin, windowEnd}; +} + +ImportRun windowRun(const HostImports& state, ImportRun run, std::uint64_t begin, std::uint64_t end, std::uint64_t alignment) { + if (run.ranges.size() != 1) return run; + const auto& range = *run.ranges.front(); + const auto bounds = importWindowBounds(range, begin, end, alignment); + if (!bounds) return run; + auto [windowBegin, windowEnd] = *bounds; for (bool grown = true; grown;) { grown = false; auto it = state.imports.upper_bound(windowBegin); @@ -1615,7 +1623,10 @@ const HostImport* importAllocation(const Context& context, HostImports& state, s } } const auto budget = evict ? 0 : effectiveBudget(context, state); - if (const auto index = leasedIndexContaining(lease, address); budget != 0 && index != lease.size() && stillRefused(state, rememberedRun(state.refusals, lease, index, index), budget) && findImport(state, address, address + 1) == nullptr) { + const auto index = leasedIndexContaining(lease, address); + const auto windowBounds = index != lease.size() && end != 0 ? importWindowBounds(*lease[index], address, end, context.hostImportAlignment) : std::nullopt; + const bool complete = !windowBounds || (windowBounds->first == lease[index]->address && windowBounds->second == lease[index]->address + lease[index]->bytes); + if (budget != 0 && index != lease.size() && complete && stillRefused(state, rememberedRun(state.refusals, lease, index, index), budget) && findImport(state, address, address + 1) == nullptr) { auto& counts = state.counts[static_cast(requester)]; ++counts.refused; counts.refusedBytes += lease[index]->bytes; @@ -1660,6 +1671,40 @@ std::vector importPieces(const Context& context, HostImports& state return pieces; } +std::vector budgetedImportPieces(const Context& context, HostImports& state, const GuestAllocations::Range& range, const GuestAllocations::Lease& lease) { + const auto window = rangeImportWindow.load(std::memory_order_relaxed); + const auto alignment = context.hostImportAlignment; + if (!range.releasable || !GuestMemory::Watched(range.address, range.bytes) || window == 0 || alignment == 0 || window % alignment != 0 || range.address % alignment != 0 || range.bytes <= window) return {}; + const auto end = range.address + range.bytes; + std::vector pieces; + for (auto cursor = range.address; cursor < end;) { + const HostImport* entry = nullptr; + if (const auto* previous = peekImport(state, cursor, cursor + 1); previous != nullptr && previous->base >= range.address && previous->base + previous->bytes <= end) { + entry = importAllocation(context, state, cursor, lease, ImportRequester::Build, std::min(end, previous->base + previous->bytes), false); + } + if (entry == nullptr) { + const auto budget = effectiveBudget(context, state); + auto room = budget - std::min(budget, state.liveBytes); + if (const auto limit = importMaxBytes.load(std::memory_order_relaxed); limit != 0) room = std::min(room, limit); + if (room < window) break; + auto wanted = cursor + std::min(end - cursor, window); + if (const auto bounds = importWindowBounds(range, cursor, wanted, alignment); bounds && bounds->second - bounds->first > room) { + wanted = range.address + (range.bytes / window - 1) * window; + if (wanted <= cursor) break; + } + entry = importAllocation(context, state, cursor, lease, ImportRequester::Build, wanted, false); + } + if (entry == nullptr || entry->base > cursor) break; + const auto to = std::min(end, entry->base + entry->bytes); + if (to <= cursor) break; + pieces.push_back({cursor, to, entry}); + cursor = to; + } + if (pieces.empty()) return {}; + if (pieces.back().end != end) pieces.push_back({pieces.back().end, end, nullptr}); + return pieces; +} + const HostImport* importJoined(const Context& context, HostImports& state, std::uint64_t begin, std::uint64_t end, const GuestAllocations::Lease& lease) { if (const auto* low = peekImport(state, begin, begin + 1); low != nullptr && low->joined) begin = low->base; if (const auto* high = peekImport(state, end - 1, end); high != nullptr && high->joined) end = high->base + high->bytes; @@ -2666,7 +2711,13 @@ bool importsNow(const Context& context, HostImports& state, const GuestAllocatio // The mirror for a leased image range: the existing one, or a new one when none exists or the // registered Range object changed (a protection change). Null when the range cannot be mirrored. An // existing writable mirror's blocks are appended to `blocks` for the caller's one compare of every -std::shared_ptr acquireMirror(const Context& context, const std::shared_ptr& range, std::vector& blocks, bool heap) { +std::shared_ptr acquireMirror(const Context& context, const std::shared_ptr& range, std::vector& blocks, bool heap, std::uint64_t begin = 0, std::uint64_t bytes = 0) { + auto selected = *range; + if (bytes != 0) { + Require(begin >= range->address && bytes <= range->bytes && begin - range->address <= range->bytes - bytes, "heap mirror selection exceeds its registered range"); + selected.address = begin; + selected.bytes = static_cast(bytes); + } auto& state = Mirrors(); std::shared_ptr mirror; { @@ -2679,28 +2730,28 @@ std::shared_ptr acquireMirror(const Context& context, const std::sh state.device = context.device; ++state.version; } - if (state.failed.contains(range->address)) return nullptr; - const auto found = state.entries.find(range->address); - if (found != state.entries.end() && sameRange(found->second->range, range) && found->second->reserved == range->bytes && found->second->writable == range->writable && found->second->heap == heap) mirror = found->second; + if (state.failed.contains(selected.address)) return nullptr; + const auto found = state.entries.find(selected.address); + if (found != state.entries.end() && sameRange(found->second->range, range) && found->second->reserved == selected.bytes && found->second->writable == range->writable && found->second->heap == heap) mirror = found->second; } if (mirror != nullptr) { if (!mirror->sparse) { - if (!heap && prepareRefresh(*mirror, range->address, range->bytes)) appendBlocks(blocks, *mirror, range->address, range->bytes); + if (!heap && prepareRefresh(*mirror, selected.address, selected.bytes)) appendBlocks(blocks, *mirror, selected.address, selected.bytes); return mirror; } if (refreshSparseMirror(*mirror, blocks)) return mirror; } Pieces pieces; - if (!GuestMemory::Accessible(reinterpret_cast(range->address), range->bytes, range->writable && !heap)) { + if (!GuestMemory::Accessible(reinterpret_cast(selected.address), selected.bytes, range->writable && !heap)) { if (!heap || !sparseMirrorsEnabled()) return nullptr; - pieces = GuestMemory::DescribeCommitted(range->address, range->bytes).ranges; + pieces = GuestMemory::DescribeCommitted(selected.address, selected.bytes).ranges; if (pieces.empty()) return nullptr; } const bool sparse = !pieces.empty(); mirror = std::make_shared(); - mirror->base = range->address; - mirror->bytes = sparse ? sparseExtent(range->address, range->bytes, pieces) : range->bytes; - mirror->reserved = range->bytes; + mirror->base = selected.address; + mirror->bytes = sparse ? sparseExtent(selected.address, selected.bytes, pieces) : selected.bytes; + mirror->reserved = selected.bytes; mirror->sparse = sparse; mirror->writable = range->writable; mirror->heap = heap; @@ -2713,8 +2764,8 @@ std::shared_ptr acquireMirror(const Context& context, const std::sh syncRange(mirror->base, mirror->bytes); if (GuestMemory::Watched(mirror->base, static_cast(mirror->bytes))) generation = collectPieces(pieces); } else { - prepareRefresh(*mirror, range->address, range->bytes); - generation = GuestMemory::CollectWrites(range->address, range->bytes); + prepareRefresh(*mirror, selected.address, selected.bytes); + generation = GuestMemory::CollectWrites(selected.address, selected.bytes); } constexpr std::uint64_t block = 65536; mirror->generations.assign(static_cast(((mirror->base + mirror->bytes + block - 1) / block) - mirror->base / block), generation); @@ -2722,7 +2773,7 @@ std::shared_ptr acquireMirror(const Context& context, const std::sh std::shared_ptr stale; std::lock_guard lock(state.mutex); ++state.heapUnwatched; - if (const auto found = state.entries.find(range->address); found != state.entries.end() && found->second->heap) { + if (const auto found = state.entries.find(selected.address); found != state.entries.end() && found->second->heap) { unlistMirror(state, *found->second); stale = std::move(found->second); state.entries.erase(found); @@ -2733,7 +2784,7 @@ std::shared_ptr acquireMirror(const Context& context, const std::sh mirror->watchedSerial.store(serial, std::memory_order_relaxed); if (context.sparseBinding && context.sparseResidencyBuffer && context.sparseResidencyAliased) backed = GuestNonzeroBlocks(sparse ? clipPieces(pieces, mirror->base, mirror->base + mirror->bytes) : GuestRuns{{mirror->base, mirror->base + mirror->bytes}}); auto need = blockBytes(backed); - reserveHeapBytes(range->address, range->bytes, need); + reserveHeapBytes(selected.address, selected.bytes, need); try { mirror->resident = ResidentBuffer::Create(context, mirror->base, mirror->bytes, backed); } catch (const std::runtime_error& error) { @@ -2743,7 +2794,7 @@ std::shared_ptr acquireMirror(const Context& context, const std::sh state.heapBytes -= need; held = state.heapBytes; } - throw std::runtime_error(heapMirrorError(range->address, range->bytes, held, error.what())); + throw std::runtime_error(heapMirrorError(selected.address, selected.bytes, held, error.what())); } if (mirror->resident == nullptr) { { @@ -2751,7 +2802,7 @@ std::shared_ptr acquireMirror(const Context& context, const std::sh state.heapBytes -= need; } need = mirror->bytes; - reserveHeapBytes(range->address, range->bytes, need); + reserveHeapBytes(selected.address, selected.bytes, need); } mirror->accounted = need; } @@ -2771,9 +2822,9 @@ std::shared_ptr acquireMirror(const Context& context, const std::sh std::lock_guard lock(state.mutex); ++state.rebuilds; mirror->serial = (1ull << 63u) | ++state.serials; - if (const auto found = state.entries.find(range->address); found != state.entries.end() && found->second->heap) unlistMirror(state, *found->second); + if (const auto found = state.entries.find(selected.address); found != state.entries.end() && found->second->heap) unlistMirror(state, *found->second); mirror->listed = true; - state.entries[range->address] = mirror; + state.entries[selected.address] = mirror; ++state.version; return mirror; } @@ -2787,11 +2838,11 @@ std::shared_ptr acquireMirror(const Context& context, const std::sh state.heapBytes -= mirror->accounted; held = state.heapBytes; } - throw std::runtime_error(heapMirrorError(range->address, range->bytes, held, error.what())); + throw std::runtime_error(heapMirrorError(selected.address, selected.bytes, held, error.what())); } - std::fprintf(stderr, "[gpu] image mirror of 0x%llx+0x%llx failed: %s; falling back to copies\n", static_cast(range->address), static_cast(range->bytes), error.what()); + std::fprintf(stderr, "[gpu] image mirror of 0x%llx+0x%llx failed: %s; falling back to copies\n", static_cast(selected.address), static_cast(selected.bytes), error.what()); std::lock_guard lock(state.mutex); - state.failed.insert(range->address); + state.failed.insert(selected.address); return nullptr; } // Read stores GPU results pending in the range first (write-backs of earlier copies of it); @@ -2799,7 +2850,7 @@ std::shared_ptr acquireMirror(const Context& context, const std::sh { const GuestMemory::ReadSiteScope site(GuestMemory::ReadSite::MirrorRefresh); const auto target = mirror->buffer->Bytes().first(static_cast(mirror->bytes)); - if (!sparse) GuestMemory::Read(range->address, target); + if (!sparse) GuestMemory::Read(selected.address, target); std::size_t cursor = 0; for (const auto& [first, last] : pieces) { const auto at = static_cast(first - mirror->base); @@ -2815,9 +2866,9 @@ std::shared_ptr acquireMirror(const Context& context, const std::sh ++state.rebuilds; mirror->serial = (1ull << 63u) | ++state.serials; // A replaced mirror lives on in the regions of recorded work that bound it. - if (const auto found = state.entries.find(range->address); found != state.entries.end() && found->second->heap) unlistMirror(state, *found->second); + if (const auto found = state.entries.find(selected.address); found != state.entries.end() && found->second->heap) unlistMirror(state, *found->second); mirror->listed = heap; - state.entries[range->address] = mirror; + state.entries[selected.address] = mirror; ++state.version; return mirror; } @@ -2860,20 +2911,28 @@ void sweepMirrors() { state.pinnedVersion = state.version; } -void releaseImportedMirrors(const Context& context, const std::vector& imported) { +void releaseImportedMirrors(const Context& context, std::span> imported) { if (imported.empty()) return; std::vector> released; auto& state = Mirrors(); std::lock_guard lock(state.mutex); if (state.device != context.device || state.entries.empty()) return; - for (const auto base : imported) { - const auto found = state.entries.find(base); - if (found == state.entries.end() || !found->second->heap) continue; - state.heapImportedBytes += found->second->accounted; - unlistMirror(state, *found->second); - ++state.heapImported; - released.push_back(std::move(found->second)); - state.entries.erase(found); + for (const auto& [begin, end] : imported) { + auto it = state.entries.upper_bound(begin); + if (it != state.entries.begin()) --it; + while (it != state.entries.end() && it->first < end) { + const auto& mirror = it->second; + if (!mirror->heap || mirror->base + mirror->bytes <= begin) { + ++it; + continue; + } + ++state.heapImported; + state.heapImportedBytes += mirror->accounted; + unlistMirror(state, *mirror); + released.push_back(std::move(it->second)); + it = state.entries.erase(it); + ++state.version; + } } } @@ -3671,10 +3730,11 @@ bool ImportMappedRanges(const Context& context, const GuestAllocations::Mapped& } else if (state.refreshedGeneration > generation) { return true; } - if (!adopting) current = GuestAllocations::GuestAllocationsAcquire_nid_postfix(); for (const auto& mapped : ranges) { const auto range = mapped.lock(); - if (range == nullptr || leasedRangeOwner(current, range->address) != range) continue; + if (range == nullptr || range->bytes <= 0x10000) continue; + if (current.empty()) current = GuestAllocations::GuestAllocationsAcquire_nid_postfix(); + if (leasedRangeOwner(current, range->address) != range) continue; try { if (!GuestMemory::GpuMutex().HeldByThisThread()) { const auto run = importRun(current, range->address, std::numeric_limits::max(), effectiveBudget(context, state)); @@ -3866,7 +3926,7 @@ void GuestBufferMemory::AcquireRegistered() { // must not happen under it. regions.reserve(lease.size()); std::vector copies; - std::vector imported; + std::vector> imported; const HostImport* covering = nullptr; std::uint64_t coveredBegin = 0; std::uint64_t coveredEnd = 0; @@ -3893,15 +3953,33 @@ void GuestBufferMemory::AcquireRegistered() { coveredEnd = entry->base + entry->bytes; } } + if (importable && mirrorsEnabled() && entry == nullptr && pieces.empty()) { + auto& state = Imports(); + std::lock_guard lock(state.mutex); + pieces = budgetedImportPieces(context, state, *range, lease); + } lap(timing.importsUs, at); if (!pieces.empty()) { + const auto firstImported = imported.size(); + for (const auto& piece : pieces) if (piece.entry != nullptr) imported.emplace_back(piece.begin, piece.end); + releaseImportedMirrors(context, std::span(imported).subspan(firstImported)); for (const auto& piece : pieces) { Region part{piece.begin, piece.end, range->writable, {}, nullptr}; - part.hostBacked = true; - part.direct = piece.entry; + if (piece.entry != nullptr) { + part.hostBacked = true; + part.direct = piece.entry; + } else { + part.mirror = acquireMirror(context, range, blocks, true, piece.begin, piece.end - piece.begin); + lap(timing.mirrorsUs, at); + Require(part.mirror != nullptr, "a partial heap import has no mirror for its remaining bytes"); + mirrored = true; + if (part.mirror->sparse) { + part.sparse = true; + part.backed = part.mirror->committed; + } else heaps.push_back(part.mirror.get()); + } regions.push_back(std::move(part)); } - imported.push_back(range->address); continue; } if (entry != nullptr) { @@ -3909,7 +3987,7 @@ void GuestBufferMemory::AcquireRegistered() { // Reused by Upload while no import was dropped since (see importsEpoch). region.direct = entry; regions.push_back(std::move(region)); - imported.push_back(range->address); + imported.emplace_back(range->address, range->address + range->bytes); continue; } if (mirrorsEnabled()) { diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/MultisampleTarget.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/MultisampleTarget.cpp index 7a9856eff4..0520466262 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/MultisampleTarget.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/MultisampleTarget.cpp @@ -228,7 +228,7 @@ public: viewInfo.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}; VkImageViewUsageCreateInfo viewUsage{VK_STRUCTURE_TYPE_IMAGE_VIEW_USAGE_CREATE_INFO}; viewUsage.usage = imageUsage & ~VK_IMAGE_USAGE_STORAGE_BIT; - if (orderFormat != VK_FORMAT_UNDEFINED) viewInfo.pNext = &viewUsage; + if ((imageUsage & VK_IMAGE_USAGE_STORAGE_BIT) != 0) viewInfo.pNext = &viewUsage; Check(context.Function("vkCreateImageView")(context.device, &viewInfo, nullptr, &created.view), "vkCreateImageView multisampled target"); } catch (...) { destroy(created); @@ -660,6 +660,7 @@ struct GuardedRange { std::uint32_t samples; std::shared_ptr> active; std::shared_ptr> metadataValid; + bool fmask = false; }; class PrimedSharedMutex : public std::shared_mutex { @@ -1152,6 +1153,12 @@ VkComponentMapping swapRedBlue(VkComponentMapping components) { return {swapped(components.r, VK_COMPONENT_SWIZZLE_R), swapped(components.g, VK_COMPONENT_SWIZZLE_G), swapped(components.b, VK_COMPONENT_SWIZZLE_B), swapped(components.a, VK_COMPONENT_SWIZZLE_A)}; } +bool sameStorageForm(const Context& context, VkFormat rendered, VkFormat requested) { + if (rendered == requested) return true; + const auto storage = HostStorageFormat(context, rendered); + return storage != VK_FORMAT_UNDEFINED && storage == HostStorageFormat(context, requested); +} + } std::shared_ptr MultisampleTargetTexture(const Context& context, const GuestTextureResource& resource, VkComponentMapping components, VkImageViewType viewType) { @@ -1214,7 +1221,7 @@ VkImageView MultisampleTargetStorageView(const Context& context, const GuestText if (!target->active->load(std::memory_order_acquire)) continue; if (target->context.device != context.device || target->target.surfaceAddress != resource.baseAddress) continue; const auto& rendered = target->target; - if (rendered.format == format && rendered.extent.width == resource.width && rendered.extent.height == resource.height && rendered.samples == resource.samples && rendered.mip == 0) return target->Storage(StorageFormatForGuest(context, resource.format)); + if (sameStorageForm(context, rendered.format, format) && rendered.extent.width == resource.width && rendered.extent.height == resource.height && rendered.samples == resource.samples && rendered.mip == 0) return target->Storage(StorageFormatForGuest(context, resource.format)); other = target.get(); } if (other == nullptr) { @@ -1315,6 +1322,17 @@ void ClearMultisampleTargets(VkDevice device) { UpdateResidentImageUnmapObserver_nid_no_patch(); } +void GuardOneFragmentFmask(const Context& context, const ColorTarget& target) { + const auto bits = static_cast(target.extent.width) * target.extent.height * target.coverageSamples; + const auto bytes = std::max((bits / 8u + 0xffffu) & ~0xffffull, 0x10000u); + std::unique_lock lock(guardedMutex()); + const bool known = std::ranges::any_of(guarded(), [&](const auto& range) { return range.fmask && range.device == context.device && range.address == target.fmaskAddress && range.bytes == bytes; }); + if (!known) guarded().push_back({context.device, target.fmaskAddress, bytes, target.coverageSamples, std::make_shared>(true), nullptr, true}); + anyGuarded().store(true, std::memory_order_release); + lock.unlock(); + InstallResidentImageReadGuard_nid_no_patch(); +} + void RequireNoMultisampleTarget(std::uint64_t address, std::size_t bytes, const char* access) { RequireNoResidentDepthRead_nid_no_patch(address, bytes); RequireNoMultisampleColorTarget(address, bytes, access); @@ -1326,6 +1344,10 @@ void RequireNoMultisampleColorTarget(std::uint64_t address, std::size_t bytes, c for (const auto& range : guarded()) { if (address >= range.address + range.bytes || range.address >= address + bytes) continue; char text[256]; + if (range.fmask) { + std::snprintf(text, sizeof(text), "AGC graphics: %s of 0x%llx+0x%zx overlaps the FMASK 0x%llx+0x%llx of a one-fragment color target of %u samples drawn on the host, which does not write it: guest access to it is not implemented", access, static_cast(address), bytes, static_cast(range.address), static_cast(range.bytes), range.samples); + throw std::runtime_error(text); + } std::snprintf(text, sizeof(text), "AGC graphics: %s of 0x%llx+0x%zx overlaps multisampled color target 0x%llx+0x%llx (%u samples), whose samples are only on the host: guest access to it is not implemented", access, static_cast(address), bytes, static_cast(range.address), static_cast(range.bytes), range.samples); throw std::runtime_error(text); } diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/Pipeline.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/Pipeline.cpp index 00c006e96b..fcad4ff7bf 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/Pipeline.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/Pipeline.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -27,6 +28,34 @@ namespace AgcDriver::Graphics { +namespace { + +bool LogicOpFormat(VkFormat format) { + switch (format) { + case VK_FORMAT_R8_UNORM: + case VK_FORMAT_R8G8_UNORM: + case VK_FORMAT_R8G8B8A8_UNORM: + case VK_FORMAT_B8G8R8A8_UNORM: + case VK_FORMAT_A2B10G10R10_UNORM_PACK32: + case VK_FORMAT_A2R10G10B10_UNORM_PACK32: + case VK_FORMAT_R16_UNORM: + case VK_FORMAT_R16G16_UNORM: + case VK_FORMAT_R16G16B16A16_UNORM: + case VK_FORMAT_R8_UINT: + case VK_FORMAT_R8G8_UINT: + case VK_FORMAT_R8G8B8A8_UINT: + case VK_FORMAT_R16_UINT: + case VK_FORMAT_R16G16_UINT: + case VK_FORMAT_R16G16B16A16_UINT: + case VK_FORMAT_R32_UINT: + case VK_FORMAT_R32G32_UINT: + case VK_FORMAT_R32G32B32A32_UINT: return true; + default: return false; + } +} + +} + void LogPipelineStatistics_nid_no_patch(const Context& context, VkPipeline pipeline) { if (!context.pipelineExecutableInfo) return; const auto getProperties = context.Function("vkGetPipelineExecutablePropertiesKHR"); @@ -105,6 +134,18 @@ bool BarycentricPatches(const State& state, std::span shad return !state.rectList && state.stages.path == ShaderPath::Vertex && std::any_of(shaders.begin(), shaders.end(), [](const CompiledShader& shader) { return shader.stage == ShaderRecompiler::ShaderStage::TessellationControl; }); } +bool FragmentInterlock(std::span shaders) { + return std::any_of(shaders.begin(), shaders.end(), [](const CompiledShader& shader) { + if (shader.stage != ShaderRecompiler::ShaderStage::Fragment || shader.program == nullptr) return false; + const auto words = shader.program->spirv.Words(); + for (std::size_t cursor = 5; cursor + 1 < words.size() && (words[cursor] & 0xffffu) == spv::OpCapability && (words[cursor] >> 16u) == 2u; cursor += 2) { + const auto capability = words[cursor + 1]; + if (capability == spv::CapabilityFragmentShaderPixelInterlockEXT || capability == spv::CapabilityFragmentShaderSampleInterlockEXT || capability == spv::CapabilityFragmentShaderShadingRateInterlockEXT) return true; + } + return false; + }); +} + void ValidateProvokingVertex(const Context& context, const State& state, std::span shaders) { Require(state.provokingVertexMode == VK_PROVOKING_VERTEX_MODE_FIRST_VERTEX_EXT || state.provokingVertexMode == VK_PROVOKING_VERTEX_MODE_LAST_VERTEX_EXT, "invalid provoking vertex mode"); if (state.provokingVertexMode == VK_PROVOKING_VERTEX_MODE_FIRST_VERTEX_EXT) { @@ -280,7 +321,7 @@ void appendKey(std::vector& key, const TValue& value) { bool libraryKeys(PipelineLibraryKeys& keys, const Context& context, const State& state, const VertexInputLayout& input, const PipelineSetLayouts& resources, std::span shaders) { using Stage = ShaderRecompiler::ShaderStage; - if (!context.graphicsPipelineLibrary || context.pipelineExecutableInfo || state.depthBiasPerFace || state.rectList || BarycentricPatches(state, shaders) || SplitsAlternateAlpha(state) || std::ranges::any_of(shaders, [](const CompiledShader& shader) { return shader.program != nullptr && shader.program->functionLdsBuffer; })) return false; + if (!context.graphicsPipelineLibrary || context.pipelineExecutableInfo || state.depthBiasPerFace || state.rectList || BarycentricPatches(state, shaders) || SplitsAlternateAlpha(state) || std::ranges::any_of(shaders, [](const CompiledShader& shader) { return shader.program != nullptr && shader.program->functionLdsBuffer; }) || FragmentInterlock(shaders)) return false; keys = {}; for (const auto& shader : shaders) { const auto identity = shader.program->Identity(); @@ -350,6 +391,7 @@ bool libraryKeys(PipelineLibraryKeys& keys, const Context& context, const State& } } for (const auto& blend : state.blends) appendKey(keys.fragmentOutput, blend); + appendKey(keys.fragmentOutput, state.colorRop); for (const auto value : state.blendConstants) appendKey(keys.fragmentOutput, value); appendKey(keys.renderPass, state.blends.size()); for (const auto& color : state.colors) { @@ -416,6 +458,8 @@ Pipeline::Pipeline(const Context& context, const State& state, const VertexInput Require(!depthBias || state.depthBiasClamp == 0.0f || context.depthBiasClamp, "device does not support depth bias clamping"); Require(!state.dualSourceBlend || (context.dualSrcBlend && state.blends.size() <= context.limits.maxFragmentDualSrcAttachments), "device does not support dual-source blending into this many attachments"); Require(state.reversedBlend == 0 || (context.dualSrcBlend && state.blends.size() <= context.limits.maxFragmentDualSrcAttachments), "blending into a color target with a reversed component order through a second source color needs dual-source blending, which the device does not support"); + Require(LogicOpFromRop(state.colorRop) == VK_LOGIC_OP_COPY || context.logicOp, "device does not support color logic operations"); + Require(LogicOpFromRop(state.colorRop) == VK_LOGIC_OP_COPY || std::ranges::all_of(state.colors, [](const ColorTarget& color) { return LogicOpFormat(color.format); }), "color logic operations on a color target that is not a normalized or integer format are not implemented"); Require(!state.negativeOneToOne || context.depthClipControl, "negative-one-to-one depth clipping requires VK_EXT_depth_clip_control with depthClipControl enabled"); Require(!state.perSampleShading || context.sampleRateShading, "per-sample pixel shading requires the sampleRateShading device feature"); Require(!state.mergedCoverage || (context.mergedCoverageSampleCounts & state.samples) != 0, "an overrasterized draw (a multisampled rasterizer into one-sample color targets) requires VK_NV_framebuffer_mixed_samples with merged coverage reduction"); @@ -639,6 +683,10 @@ Pipeline::Pipeline(const Context& context, const State& state, const VertexInput secondChannels[index].colorBlendOp = state.blends[index].alphaBlendOp; } } + if (const auto logicOp = LogicOpFromRop(state.colorRop); logicOp != VK_LOGIC_OP_COPY) { + blend.logicOpEnable = VK_TRUE; + blend.logicOp = logicOp; + } blend.attachmentCount = static_cast(firstChannels.size()); blend.pAttachments = firstChannels.empty() ? nullptr : firstChannels.data(); std::copy(state.blendConstants.begin(), state.blendConstants.end(), blend.blendConstants); @@ -982,6 +1030,7 @@ void pipelineKey(std::vector& key, const Context& context, const Stat append(key, state.conservativeRasterization); append(key, state.blends.size()); for (const auto& blend : state.blends) append(key, blend); + append(key, state.colorRop); for (const auto value : state.blendConstants) append(key, value); append(key, state.samples); append(key, state.perSampleShading); diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/PipelineCache.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/PipelineCache.cpp index 73fac4402d..28df53956c 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/PipelineCache.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/PipelineCache.cpp @@ -197,7 +197,7 @@ void PipelineCache::save(bool final, bool waitForPipelines) { settledBytes = liveBytes; settledFile = file; }; - const auto disk = fileUnchanged ? File{} : readFile(); + auto disk = fileUnchanged ? File{} : readFile(); if (disk.state == FileState::Unreadable) { std::fprintf(stderr, "[pipeline-cache] cannot read %s; not saving over it\n", path.string().c_str()); settle(readHeader()); @@ -206,12 +206,16 @@ void PipelineCache::save(bool final, bool waitForPipelines) { if (disk.state == FileState::Invalid) std::fprintf(stderr, "[pipeline-cache] replacing %s: %s\n", path.string().c_str(), disk.invalidReason); std::size_t floor = fileUnchanged ? std::max(loadedBytes, settledFile->dataBytes) : 0; VkPipelineCache merged = VK_NULL_HANDLE; + bool mergedWithFile = false; struct DestroyMerged { const Context& context; - const VkPipelineCache& cache; - ~DestroyMerged() { - if (cache != VK_NULL_HANDLE) context.Function("vkDestroyPipelineCache")(context.device, cache, nullptr); + VkPipelineCache& cache; + void Reset() { + if (cache == VK_NULL_HANDLE) return; + context.Function("vkDestroyPipelineCache")(context.device, cache, nullptr); + cache = VK_NULL_HANDLE; } + ~DestroyMerged() { Reset(); } } destroyMerged{context, merged}; if (disk.state == FileState::Valid) { const auto data = disk.Payload(); @@ -221,11 +225,13 @@ void PipelineCache::save(bool final, bool waitForPipelines) { const auto result = context.Function("vkCreatePipelineCache")(context.device, &info, nullptr, &merged); if (result == VK_SUCCESS) { floor = std::max(loadedBytes, data.size()); + mergedWithFile = true; } else { merged = VK_NULL_HANDLE; std::fprintf(stderr, "[pipeline-cache] replacing %s: the driver refused it (Vulkan result %d)\n", path.string().c_str(), static_cast(result)); } } + std::vector().swap(disk.contents); if (merged != VK_NULL_HANDLE) { auto synchronizationLock = snapshot(); if (!synchronizationLock.owns_lock()) return; @@ -251,6 +257,7 @@ void PipelineCache::save(bool final, bool waitForPipelines) { if (result != VK_SUCCESS && result != VK_INCOMPLETE) return; file.resize(sizeof(FileHeader) + size); } + destroyMerged.Reset(); if (size < floor) { std::fprintf(stderr, "[pipeline-cache] keeping %s: the merged cache has %.1f KiB, less than the %.1f KiB on disk or loaded at start\n", path.string().c_str(), static_cast(size) / 1024.0, static_cast(floor) / 1024.0); settle(fileUnchanged ? *settledFile : disk.header); @@ -263,7 +270,7 @@ void PipelineCache::save(bool final, bool waitForPipelines) { return; } settle(header); - if (final || profiling()) std::fprintf(stderr, "[pipeline-cache] saved %.1f KiB to %s in %.1f ms%s%s\n", static_cast(size) / 1024.0, path.string().c_str(), std::chrono::duration(std::chrono::steady_clock::now() - started).count(), merged != VK_NULL_HANDLE ? " (merged with the file)" : "", final ? " (teardown)" : ""); + if (final || profiling()) std::fprintf(stderr, "[pipeline-cache] saved %.1f KiB to %s in %.1f ms%s%s\n", static_cast(size) / 1024.0, path.string().c_str(), std::chrono::duration(std::chrono::steady_clock::now() - started).count(), mergedWithFile ? " (merged with the file)" : "", final ? " (teardown)" : ""); } void PipelineCache::SaveNow() { diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/PipelineJournal.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/PipelineJournal.cpp index 5f81a61e05..7eba2c79ac 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/PipelineJournal.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/PipelineJournal.cpp @@ -1,11 +1,13 @@ #include "prx/libSceAgcDriver/Graphics/include/PipelineJournal.hpp" #include "prx/libSceAgcDriver/Graphics/include/Pipeline.hpp" #include "prx/libSceAgcDriver/Execution/include/ProfileOutput.hpp" +#include "prx/libc/include/HostThreadLocal.hpp" #include "ShaderCacheDirectory.hpp" #include "ShaderDiskCache.hpp" #include #include #include +#include #include #include #include @@ -23,6 +25,7 @@ #else #include #include +#include #include #endif @@ -35,7 +38,7 @@ static_assert(sizeof(VkStencilOpState) == 28, "VkStencilOpState changed: update static_assert(sizeof(VkVertexInputBindingDescription) == 12, "VkVertexInputBindingDescription changed: update the recipe codec"); static_assert(sizeof(VkVertexInputAttributeDescription) == 16, "VkVertexInputAttributeDescription changed: update the recipe codec"); #if defined(__linux__) && defined(__x86_64__) && defined(__GLIBCXX__) -static_assert(sizeof(State) == 824, "State changed: update CanonicalPipelineState and the recipe codec"); +static_assert(sizeof(State) == 832, "State changed: update CanonicalPipelineState and the recipe codec"); static_assert(sizeof(ShaderStages) == 112, "ShaderStages changed: update the recipe codec"); static_assert(sizeof(ShaderRecompiler::MeshConfiguration) == 40, "MeshConfiguration changed: update the recipe codec"); static_assert(sizeof(ShaderRecompiler::TessellationConfiguration) == 40, "TessellationConfiguration changed: update the recipe codec"); @@ -44,7 +47,7 @@ static_assert(sizeof(ShaderRecompiler::TessellationConfiguration) == 40, "Tessel constexpr std::uint32_t FileMagic = 0x4c4a5041u; // Bumped when the recipe codec, CanonicalPipelineState or the store key change in a way the // layout guards above do not catch (a field renumbered or reinterpreted at the same size). -constexpr std::uint32_t FileFormat = 9u; +constexpr std::uint32_t FileFormat = 10u; constexpr std::uint32_t ModuleRecord = 1u; constexpr std::uint32_t GraphicsRecord = 2u; constexpr std::uint32_t ComputeRecord = 3u; @@ -203,6 +206,7 @@ void encodeState(Writer& writer, const State& state) { writer.Bool(state.rectList); writer.Bool(state.dualSourceBlend); writer.U32(state.reversedBlend); + writer.U32(state.colorRop); writer.U32(static_cast(state.topology)); writer.Bool(state.primitiveRestart); writer.Bool(state.negativeOneToOne); @@ -270,6 +274,7 @@ void decodeState(Reader& reader, State& state) { state.rectList = reader.Bool(); state.dualSourceBlend = reader.Bool(); state.reversedBlend = reader.U32(); + state.colorRop = static_cast(reader.U32()); state.topology = static_cast(reader.U32()); state.primitiveRestart = reader.Bool(); state.negativeOneToOne = reader.Bool(); @@ -339,6 +344,10 @@ std::uint64_t padded(std::uint64_t bytes) { return (bytes + RecordAlignment - 1u) / RecordAlignment * RecordAlignment; } +std::uint64_t recordBytes(std::uint64_t payloadBytes) { + return padded(sizeof(RecordHeader) + payloadBytes); +} + bool profiling() { static const bool profile = std::getenv("APS5_PROFILE_DRAW") != nullptr; return profile; @@ -359,6 +368,16 @@ std::uint64_t configured(const char* name, std::uint64_t fallback) { // second run of the same install) takes the same lock file, so its appends and this run's never // interleave and neither cuts a record the other is writing. Taken before the journal's mutex, // never while holding it. +bool replaceFile(const std::filesystem::path& temporary, const std::filesystem::path& target) { +#ifdef _WIN32 + return ::MoveFileExW(temporary.c_str(), target.c_str(), MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH) != 0; +#else + std::error_code error; + std::filesystem::rename(temporary, target, error); + return !error; +#endif +} + class FileLock { public: explicit FileLock(const std::filesystem::path& path) { @@ -409,13 +428,100 @@ private: #endif }; +class JournalFile { +public: + explicit JournalFile(const std::filesystem::path& path) { +#ifdef _WIN32 + handle = CreateFileW(path.c_str(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, nullptr); + LARGE_INTEGER length{}; + if (handle != INVALID_HANDLE_VALUE && GetFileSizeEx(handle, &length) && length.QuadPart >= 0) { + size = static_cast(length.QuadPart); + } else if (handle != INVALID_HANDLE_VALUE) { + CloseHandle(handle); + handle = INVALID_HANDLE_VALUE; + } +#else + descriptor = ::open(path.c_str(), O_RDONLY | O_CLOEXEC); + struct stat status{}; + if (descriptor >= 0 && ::fstat(descriptor, &status) == 0 && status.st_size >= 0) { + size = static_cast(status.st_size); + } else if (descriptor >= 0) { + ::close(descriptor); + descriptor = -1; + } +#endif + } + + ~JournalFile() { +#ifdef _WIN32 + if (handle != INVALID_HANDLE_VALUE) CloseHandle(handle); +#else + if (descriptor >= 0) ::close(descriptor); +#endif + } + + JournalFile(const JournalFile&) = delete; + JournalFile& operator=(const JournalFile&) = delete; + +#ifdef _WIN32 + [[nodiscard]] bool Open() const { return handle != INVALID_HANDLE_VALUE; } +#else + [[nodiscard]] bool Open() const { return descriptor >= 0; } +#endif + [[nodiscard]] std::uint64_t Size() const { return size; } + + bool Read(std::uint64_t offset, std::span bytes) const { + for (std::size_t done = 0; done < bytes.size();) { + const auto position = offset + done; +#ifdef _WIN32 + struct ReadEvent { + HANDLE event = CreateEventW(nullptr, TRUE, FALSE, nullptr); + ~ReadEvent() { + if (event != nullptr) CloseHandle(event); + } + }; + auto& completion = HostThreadLocal(); + if (completion.event == nullptr) return false; + OVERLAPPED at{}; + at.Offset = static_cast(position); + at.OffsetHigh = static_cast(position >> 32u); + at.hEvent = completion.event; + DWORD count = 0; + if (!ReadFile(handle, bytes.data() + done, static_cast(std::min(bytes.size() - done, MAXDWORD)), nullptr, &at) && GetLastError() != ERROR_IO_PENDING) return false; + if (!GetOverlappedResult(handle, &at, &count, TRUE) || count == 0) return false; +#else + const auto count = ::pread(descriptor, bytes.data() + done, bytes.size() - done, static_cast(position)); + if (count < 0 && errno == EINTR) continue; + if (count <= 0) return false; +#endif + done += static_cast(count); + } + return true; + } + + template + bool ReadValue(std::uint64_t offset, TValue& value) const requires std::is_trivially_copyable_v { + return Read(offset, std::as_writable_bytes(std::span(&value, 1))); + } + +private: +#ifdef _WIN32 + HANDLE handle = INVALID_HANDLE_VALUE; +#else + int descriptor = -1; +#endif + std::uint64_t size = 0; +}; + // The end of the last whole record in [from, size) of the file: records another process appended // after this one loaded the file are kept, the torn tail of a record a crashed run left is cut. std::uint64_t validTail(const std::filesystem::path& path, std::uint64_t from, std::uint64_t size); struct LoadedModule { ShaderRecompiler::ShaderStage stage; - std::shared_ptr result; + std::uint64_t payloadOffset = 0; + std::uint64_t payloadBytes = 0; + std::uint64_t payloadHash = 0; }; struct LoadedEntry { @@ -426,6 +532,7 @@ struct LoadedEntry { }; struct LoadedJournal { + std::shared_ptr file; std::map modules; std::vector entries; std::set indexModules; @@ -436,6 +543,178 @@ struct LoadedJournal { bool headerValid = false; }; +struct RecordSpan { + std::uint64_t offset = 0; + std::uint64_t bytes = 0; + std::uint32_t kind = 0; + std::uint64_t hash = 0; + ShaderRecompiler::PipelineIdentity identity{}; + std::vector needs; + bool decodable = false; +}; + +class PayloadHash { +public: + explicit PayloadHash(std::uint64_t bytes) : hash(bytes * Multiplier) {} + + void Update(std::span bytes) { + while (!bytes.empty()) { + const auto take = std::min(bytes.size(), carry.size() - carryBytes); + std::memcpy(carry.data() + carryBytes, bytes.data(), take); + carryBytes += take; + bytes = bytes.subspan(take); + if (carryBytes == carry.size()) { + std::uint64_t chunk = 0; + std::memcpy(&chunk, carry.data(), sizeof(chunk)); + Mix(chunk); + carryBytes = 0; + } + } + } + + [[nodiscard]] std::uint64_t Finish() { + if (carryBytes != 0) { + std::uint64_t chunk = 0; + std::memcpy(&chunk, carry.data(), carryBytes); + Mix(chunk ^ 0x8000000000000000ull); + carryBytes = 0; + } + hash ^= hash >> 33u; + hash *= 0xff51afd7ed558ccdull; + hash ^= hash >> 33u; + hash *= 0xc4ceb9fe1a85ec53ull; + hash ^= hash >> 33u; + return hash; + } + +private: + static constexpr std::uint64_t Multiplier = 0x9e3779b97f4a7c15ull; + + void Mix(std::uint64_t chunk) { + chunk *= 0xff51afd7ed558ccdull; + chunk ^= chunk >> 32u; + hash = (hash ^ chunk) * Multiplier; + hash = (hash << 27u) | (hash >> 37u); + } + + std::uint64_t hash; + std::array carry{}; + std::size_t carryBytes = 0; +}; + +struct SpanScan { + bool headerValid = false; + std::vector spans; +}; + +bool readSpanPayload(std::ifstream& file, const RecordHeader& record, std::array& prefix, std::vector& graphics, std::uint64_t& hash) { + constexpr std::uint64_t MaxGraphicsBytes = 16u * 1024u * 1024u; + PayloadHash hasher(record.payloadBytes); + std::array chunk{}; + const bool capture = record.kind == GraphicsRecord && record.payloadBytes <= MaxGraphicsBytes; + if (capture) graphics.resize(static_cast(record.payloadBytes)); + std::size_t copied = 0; + std::uint64_t left = record.payloadBytes; + while (left != 0) { + const auto take = static_cast(std::min(left, chunk.size())); + file.read(reinterpret_cast(chunk.data()), static_cast(take)); + if (file.gcount() != static_cast(take)) return false; + const auto bytes = std::span(chunk.data(), take); + hasher.Update(bytes); + const auto prefixBytes = std::min(take, prefix.size() - copied); + std::memcpy(prefix.data() + copied, bytes.data(), prefixBytes); + if (prefixBytes < take) copied = prefix.size(); + else copied += take; + if (capture) std::memcpy(graphics.data() + (static_cast(record.payloadBytes) - static_cast(left)), bytes.data(), take); + left -= take; + } + hash = hasher.Finish(); + return true; +} + +bool scanSpans(const std::filesystem::path& path, std::uint64_t device, SpanScan& scan) { + std::error_code error; + const auto fileSize = std::filesystem::file_size(path, error); + if (error) return false; + std::ifstream file(path, std::ios::binary); + if (!file) return false; + FileHeader header{}; + file.read(reinterpret_cast(&header), sizeof(header)); + if (file.gcount() != sizeof(header)) return false; + if (header.magic != FileMagic || header.format != FileFormat || header.runtimeAbi != ShaderRecompiler::RuntimeAbi::Version || + header.sourceVersion != ShaderRecompiler::ShaderDiskCache::SourceVersion() || header.device != device) return false; + scan.headerValid = true; + auto cursor = padded(sizeof(FileHeader)); + while (cursor + sizeof(RecordHeader) <= fileSize) { + file.clear(); + file.seekg(static_cast(cursor), std::ios::beg); + RecordHeader record{}; + file.read(reinterpret_cast(&record), sizeof(record)); + if (file.gcount() != sizeof(record) || record.kind == 0 || record.kind > ComputeRecord) break; + const auto payloadOffset = cursor + sizeof(RecordHeader); + if (record.payloadBytes > fileSize - payloadOffset) break; + const auto end = padded(payloadOffset + record.payloadBytes); + if (end > fileSize) break; + std::array prefix{}; + std::vector graphics; + std::uint64_t hash = 0; + if (!readSpanPayload(file, record, prefix, graphics, hash) || hash != record.payloadHash) break; + RecordSpan span; + span.offset = cursor; + span.bytes = end - cursor; + span.kind = record.kind; + span.hash = record.payloadHash; + if (record.kind == ModuleRecord) { + if (record.payloadBytes < 20u) break; + std::memcpy(&span.identity.low, prefix.data(), sizeof(span.identity.low)); + std::memcpy(&span.identity.high, prefix.data() + 8, sizeof(span.identity.high)); + } else if (record.kind == ComputeRecord) { + if (record.payloadBytes >= 16u) { + std::memcpy(&span.identity.low, prefix.data(), sizeof(span.identity.low)); + std::memcpy(&span.identity.high, prefix.data() + 8, sizeof(span.identity.high)); + span.needs.push_back(span.identity); + span.decodable = true; + } + } else { + GraphicsPipelineRecipe recipe; + if (!graphics.empty() && DecodeGraphicsPipelineRecipe(graphics, recipe)) { + for (const auto& stage : recipe.stages) span.needs.push_back(stage.identity); + span.decodable = true; + } + } + scan.spans.push_back(std::move(span)); + cursor = end; + } + return true; +} + +bool copySpans(const std::filesystem::path& path, const SpanScan& scan, const std::set& kept, const std::filesystem::path& temporary) { + std::ifstream source(path, std::ios::binary); + if (!source) return false; + std::ofstream out(temporary, std::ios::binary | std::ios::trunc); + if (!out) return false; + std::array chunk{}; + source.read(reinterpret_cast(chunk.data()), static_cast(padded(sizeof(FileHeader)))); + if (source.gcount() != static_cast(padded(sizeof(FileHeader)))) return false; + out.write(reinterpret_cast(chunk.data()), static_cast(padded(sizeof(FileHeader)))); + for (std::size_t position = 0; position < scan.spans.size(); ++position) { + if (!kept.contains(position)) continue; + const auto& span = scan.spans[position]; + source.clear(); + source.seekg(static_cast(span.offset), std::ios::beg); + auto left = span.bytes; + while (left != 0) { + const auto take = static_cast(std::min(left, chunk.size())); + source.read(reinterpret_cast(chunk.data()), static_cast(take)); + if (source.gcount() != static_cast(take)) return false; + out.write(reinterpret_cast(chunk.data()), static_cast(take)); + left -= take; + } + } + out.flush(); + return !out.fail(); +} + class Journal { public: static Journal& Instance() { @@ -464,20 +743,19 @@ public: return false; } - // Reads the whole file (modules keep views into the buffer, which lives as long as they do), - // fills the dedupe index, and remembers how many bytes parsed so a torn tail is cut before the - // next append. + // Parses the file a record at a time and keeps it open for the replay, fills the dedupe index, + // and remembers how many bytes parsed so a torn tail is cut before the next append. LoadedJournal Load() { LoadedJournal loaded; if (!Enabled() || !bound.load(std::memory_order_acquire)) return loaded; const auto started = std::chrono::steady_clock::now(); - auto file = std::make_shared>(); bool read = false; { FileLock fileLock(lockPath); - read = ShaderRecompiler::ReadWholeFile(path, *file); + auto file = std::make_shared(path); + read = file->Open() && parse(*file, loaded, true); + if (read) loaded.file = std::move(file); } - if (read) parse(*file, file, loaded, true); const auto ms = std::chrono::duration(std::chrono::steady_clock::now() - started).count(); { std::lock_guard lock(mutex); @@ -543,12 +821,15 @@ public: totals.recordedModules = counters.recordedModules.load(std::memory_order_relaxed); totals.writtenBytes = counters.writtenBytes.load(std::memory_order_relaxed); totals.writeFailures = counters.writeFailures.load(std::memory_order_relaxed); + totals.evictedRecords = counters.evictedRecords.load(std::memory_order_relaxed); + totals.evictedBytes = counters.evictedBytes.load(std::memory_order_relaxed); return totals; } struct Counters { std::atomic loadedRecipes{0}, loadedModules{0}, loadedBytes{0}, droppedRecords{0}; std::atomic recordedGraphics{0}, recordedCompute{0}, recordedModules{0}, writtenBytes{0}, writeFailures{0}; + std::atomic evictedRecords{0}, evictedBytes{0}; }; private: @@ -594,27 +875,30 @@ private: // Parses the file: with `loaded` set every record is decoded, otherwise only the index (the // module identities and record hashes) is built. Stops at the first record that does not // parse; the bytes before it stay valid. - void parse(const std::vector& file, const std::shared_ptr>& owner, LoadedJournal& loaded, bool decode) { + bool parse(const JournalFile& file, LoadedJournal& loaded, bool decode, std::vector* spans = nullptr) { std::uint64_t valid = 0; bool header = false; std::uint64_t dropped = 0; - const auto bytes = std::span(file); - if (bytes.size() >= sizeof(FileHeader)) { + const auto size = file.Size(); + if (size >= sizeof(FileHeader)) { FileHeader fileHeader; - std::memcpy(&fileHeader, bytes.data(), sizeof(fileHeader)); + if (!file.ReadValue(0, fileHeader)) return false; header = fileHeader.magic == FileMagic && fileHeader.format == FileFormat && fileHeader.runtimeAbi == ShaderRecompiler::RuntimeAbi::Version && fileHeader.sourceVersion == ShaderRecompiler::ShaderDiskCache::SourceVersion() && fileHeader.device == device.load(std::memory_order_relaxed); } if (header) { valid = padded(sizeof(FileHeader)); std::uint64_t cursor = valid; - while (cursor + sizeof(RecordHeader) <= bytes.size()) { + std::vector bytes; + while (cursor + sizeof(RecordHeader) <= size) { RecordHeader record; - std::memcpy(&record, bytes.data() + cursor, sizeof(record)); + if (!file.ReadValue(cursor, record)) return false; const auto payloadOffset = cursor + sizeof(RecordHeader); - if (record.payloadBytes > bytes.size() - payloadOffset) break; + if (record.payloadBytes > size - payloadOffset) break; const auto end = padded(payloadOffset + record.payloadBytes); - if (end > bytes.size()) break; - const auto payload = bytes.subspan(static_cast(payloadOffset), static_cast(record.payloadBytes)); + if (end > size) break; + bytes.resize(static_cast(record.payloadBytes)); + if (!file.Read(payloadOffset, bytes)) return false; + const auto payload = std::span(bytes); if (ShaderRecompiler::HashBytes(payload) != record.payloadHash) break; if (record.kind == ModuleRecord) { if (payload.size() < 20u) break; @@ -625,15 +909,24 @@ private: std::memcpy(&stage, payload.data() + 16, sizeof(stage)); bool decoded = !decode; if (decode && !loaded.indexModules.contains(identity)) { - auto result = std::make_shared(); - decoded = ShaderRecompiler::ShaderDiskCache::DecodeResult(payload.subspan(20), *result, owner) && result->Identity() == identity; - if (decoded) loaded.modules.emplace(identity, LoadedModule{static_cast(stage), std::move(result)}); + ShaderRecompiler::RecompileResult result; + decoded = ShaderRecompiler::ShaderDiskCache::DecodeResult(payload.subspan(20), result) && result.Identity() == identity; + if (decoded) loaded.modules.emplace(identity, LoadedModule{static_cast(stage), payloadOffset, record.payloadBytes, record.payloadHash}); else ++dropped; } if (decoded) loaded.indexModules.insert(identity); + if (spans != nullptr) { + RecordSpan span; + span.offset = cursor; + span.bytes = end - cursor; + span.kind = record.kind; + span.hash = record.payloadHash; + span.identity = identity; + spans->push_back(std::move(span)); + } } else if (record.kind == GraphicsRecord || record.kind == ComputeRecord) { if (decode && !loaded.indexRecords.contains(record.payloadHash)) { - LoadedEntry entry; + LoadedEntry entry{}; entry.kind = record.kind; bool decoded = false; if (record.kind == GraphicsRecord) { @@ -650,6 +943,28 @@ private: else ++dropped; } loaded.indexRecords.insert(record.payloadHash); + if (spans != nullptr) { + RecordSpan span; + span.offset = cursor; + span.bytes = end - cursor; + span.kind = record.kind; + span.hash = record.payloadHash; + if (record.kind == ComputeRecord) { + if (payload.size() >= 16u) { + std::memcpy(&span.identity.low, payload.data(), sizeof(span.identity.low)); + std::memcpy(&span.identity.high, payload.data() + 8, sizeof(span.identity.high)); + span.needs.push_back(span.identity); + span.decodable = true; + } + } else { + GraphicsPipelineRecipe recipe; + if (DecodeGraphicsPipelineRecipe(payload, recipe)) { + for (const auto& stage : recipe.stages) span.needs.push_back(stage.identity); + span.decodable = true; + } + } + spans->push_back(std::move(span)); + } } else { break; } @@ -657,11 +972,12 @@ private: cursor = end; } } - if (valid < bytes.size()) ++dropped; + if (valid < size) ++dropped; loaded.validBytes = valid; - loaded.bytes = bytes.size(); + loaded.bytes = size; loaded.dropped = dropped; loaded.headerValid = header; + return true; } // The index the writer dedupes against, replaced from the file: what it holds and where it @@ -669,12 +985,12 @@ private: // written until it can be (the next record tries again) and the file is never started over for // a read failure. bool indexLocked() { - std::vector file; LoadedJournal index; std::error_code error; - const bool exists = std::filesystem::exists(path, error); - if (exists && !ShaderRecompiler::ReadWholeFile(path, file)) return false; - if (exists) parse(file, nullptr, index, false); + if (std::filesystem::exists(path, error)) { + const JournalFile file(path); + if (!file.Open() || !parse(file, index, false)) return false; + } std::lock_guard lock(mutex); indexed = true; validBytes = index.validBytes; @@ -735,43 +1051,119 @@ private: // tail of a crashed run (bytes past the last parsed record) was cut. void write(const Pending& pending) { if (!ensureIndexed()) throw std::runtime_error("the journal could not be read"); - for (unsigned attempt = 0; attempt < 2; ++attempt) { + for (unsigned attempt = 0; attempt < 3; ++attempt) { if (append(pending)) return; } throw std::runtime_error("the journal keeps changing under this run"); } + bool evictUnderLock(std::uint64_t reserve) { + SpanScan scan; + if (!scanSpans(path, device.load(std::memory_order_relaxed), scan) || !scan.headerValid) return false; + const auto& spans = scan.spans; + std::map moduleSpans; + for (std::size_t position = 0; position < spans.size(); ++position) { + if (spans[position].kind == ModuleRecord) moduleSpans[spans[position].identity] = position; + } + const auto headerBytes = padded(sizeof(FileHeader)); + const auto target = maxBytes - maxBytes / 4u; + auto budget = target > reserve ? target - reserve : headerBytes; + if (budget < headerBytes) budget = headerBytes; + std::set keptModules; + std::set kept; + std::uint64_t used = headerBytes; + for (std::size_t position = spans.size(); position-- > 0;) { + const auto& span = spans[position]; + if (span.kind == ModuleRecord) continue; + if (!span.decodable) continue; + std::uint64_t added = span.bytes; + std::vector modules; + bool complete = true; + for (const auto& identity : span.needs) { + if (keptModules.contains(identity)) continue; + const auto found = moduleSpans.find(identity); + if (found == moduleSpans.end()) { + complete = false; + break; + } + modules.push_back(found->second); + added += spans[found->second].bytes; + } + if (!complete) continue; + if (used + added > budget) break; + used += added; + for (const auto module : modules) keptModules.insert(spans[module].identity); + kept.insert(position); + } + for (const auto& identity : keptModules) kept.insert(moduleSpans[identity]); + const auto temporary = path.string() + ".evict"; + if (!copySpans(path, scan, kept, temporary)) { + std::error_code error; + std::filesystem::remove(temporary, error); + return false; + } + if (!replaceFile(temporary, path)) { + std::error_code error; + std::filesystem::remove(temporary, error); + return false; + } + std::uint64_t evicted = 0; + std::uint64_t evictedBytes = 0; + { + std::lock_guard lock(mutex); + for (std::size_t position = 0; position < spans.size(); ++position) { + if (kept.contains(position)) continue; + ++evicted; + evictedBytes += spans[position].bytes; + } + writtenModules.clear(); + writtenRecords.clear(); + for (const auto& identity : keptModules) writtenModules.insert(identity); + for (const auto position : kept) { + if (spans[position].kind != ModuleRecord) writtenRecords.insert(spans[position].hash); + } + validBytes = used; + headerValid = true; + } + counters.evictedRecords.fetch_add(evicted, std::memory_order_relaxed); + counters.evictedBytes.fetch_add(evictedBytes, std::memory_order_relaxed); + std::fprintf(stderr, "[pipeline-journal] %s reached its size cap; %llu records (%.1f MiB) were dropped and the file was rewritten to %.1f MiB\n", path.string().c_str(), static_cast(evicted), static_cast(evictedBytes) / (1024.0 * 1024.0), static_cast(used) / (1024.0 * 1024.0)); + return true; + } + + std::vector EncodeModule(const PendingModule& module) { + std::vector payload; + Writer writer(payload); + writer.U64(module.identity.low); + writer.U64(module.identity.high); + writer.U32(static_cast(module.stage)); + ShaderRecompiler::ShaderDiskCache::EncodeResult(*module.result, payload); + return payload; + } + // One attempt: false when the file was cut or replaced by someone else since it was indexed - // (it is indexed again, and the batch is rebuilt against that index). + // (it is indexed again, and the batch is rebuilt against that index), or when the file was + // rewritten to make room for the record (the batch is rebuilt against that file). bool append(const Pending& pending) { const auto recordHash = ShaderRecompiler::HashBytes(pending.payload); - std::vector batch; - std::vector newModules; + std::vector missing; { std::lock_guard lock(mutex); if (writtenRecords.contains(recordHash)) return true; for (const auto& module : pending.modules) { - if (!writtenModules.contains(module.identity) && std::ranges::find(newModules, module.identity) == newModules.end()) newModules.push_back(module.identity); + if (writtenModules.contains(module.identity)) continue; + if (std::ranges::find(missing, module.identity) != missing.end()) continue; + missing.push_back(module.identity); } } - const auto appendRecord = [&](std::uint32_t kind, std::span payload) { - const RecordHeader header{kind, 0u, payload.size(), ShaderRecompiler::HashBytes(payload)}; - Writer writer(batch); - writer.Raw(header); - batch.insert(batch.end(), payload.begin(), payload.end()); - batch.resize(static_cast(padded(batch.size()))); - }; - for (const auto& identity : newModules) { + std::vector> missingPayloads; + missingPayloads.reserve(missing.size()); + for (const auto& identity : missing) { const auto module = std::ranges::find_if(pending.modules, [&](const PendingModule& candidate) { return candidate.identity == identity; }); - std::vector payload; - Writer writer(payload); - writer.U64(identity.low); - writer.U64(identity.high); - writer.U32(static_cast(module->stage)); - ShaderRecompiler::ShaderDiskCache::EncodeResult(*module->result, payload); - appendRecord(ModuleRecord, payload); + missingPayloads.push_back(EncodeModule(*module)); } - appendRecord(pending.kind, pending.payload); + std::uint64_t batchSize = recordBytes(pending.payload.size()); + for (const auto& payload : missingPayloads) batchSize += recordBytes(payload.size()); std::error_code error; std::filesystem::create_directories(path.parent_path(), error); FileLock fileLock(lockPath); @@ -782,6 +1174,7 @@ private: return true; } bool fresh = false; + bool overCap = false; { std::lock_guard lock(mutex); fresh = !headerValid; @@ -795,13 +1188,41 @@ private: else validBytes = tail; } } - if (!fresh && validBytes + batch.size() > maxBytes) { + overCap = !fresh && validBytes + batchSize > maxBytes; + } + if (overCap) { + std::uint64_t needed = batchSize; + std::vector held; + for (const auto& module : pending.modules) { + if (std::ranges::find(missing, module.identity) != missing.end()) continue; + if (std::ranges::find(held, module.identity) != held.end()) continue; + held.push_back(module.identity); + needed += recordBytes(EncodeModule(module).size()); + } + if (needed + padded(sizeof(FileHeader)) > maxBytes) { + static std::atomic reported{false}; + if (!reported.exchange(true)) std::fprintf(stderr, "[pipeline-journal] %s cannot hold a record of %llu bytes under APS5_PIPELINE_JOURNAL_MAX_MIB; it is not recorded\n", path.string().c_str(), static_cast(needed)); + counters.writeFailures.fetch_add(1, std::memory_order_relaxed); + return true; + } + if (!evictUnderLock(needed)) { static std::atomic reported{false}; if (!reported.exchange(true)) std::fprintf(stderr, "[pipeline-journal] %s reached APS5_PIPELINE_JOURNAL_MAX_MIB; later pipelines are not recorded\n", path.string().c_str()); counters.writeFailures.fetch_add(1, std::memory_order_relaxed); return true; } + return false; } + std::vector batch; + const auto appendRecord = [&](std::uint32_t kind, std::span payload) { + const RecordHeader header{kind, 0u, payload.size(), ShaderRecompiler::HashBytes(payload)}; + Writer writer(batch); + writer.Raw(header); + batch.insert(batch.end(), payload.begin(), payload.end()); + batch.resize(static_cast(padded(batch.size()))); + }; + for (const auto& payload : missingPayloads) appendRecord(ModuleRecord, payload); + appendRecord(pending.kind, pending.payload); if (!fresh) { const auto size = std::filesystem::file_size(path, error); if (!error && size < validBytes) { @@ -825,9 +1246,9 @@ private: std::lock_guard lock(mutex); headerValid = true; validBytes = (fresh ? padded(sizeof(FileHeader)) : validBytes) + batch.size(); - for (const auto& identity : newModules) writtenModules.insert(identity); + for (const auto& identity : missing) writtenModules.insert(identity); writtenRecords.insert(recordHash); - counters.recordedModules.fetch_add(newModules.size(), std::memory_order_relaxed); + counters.recordedModules.fetch_add(missing.size(), std::memory_order_relaxed); if (pending.kind == GraphicsRecord) counters.recordedGraphics.fetch_add(1, std::memory_order_relaxed); else counters.recordedCompute.fetch_add(1, std::memory_order_relaxed); counters.writtenBytes.fetch_add(prefix.size() + batch.size(), std::memory_order_relaxed); @@ -881,6 +1302,7 @@ std::uint64_t validTail(const std::filesystem::path& path, std::uint64_t from, s struct ReplayCounters { std::atomic graphics{0}, compute{0}, present{0}, skipped{0}, rejected{0}, failed{0}; + std::atomic held{0}, heldPeak{0}; std::atomic threads{0}; std::atomic ms{0}; }; @@ -901,6 +1323,7 @@ public: Replay(Context context, PipelineJournalHooks hooks, unsigned threads) : context(std::move(context)), hooks(std::move(hooks)), active(threads) { started = std::chrono::steady_clock::now(); replayCounters().threads.store(threads, std::memory_order_relaxed); + replayCounters().heldPeak.store(0, std::memory_order_relaxed); workers.reserve(threads); for (unsigned index = 0; index < threads; ++index) workers.emplace_back([this] { run(); }); } @@ -927,7 +1350,7 @@ private: try { auto journal = Journal::Instance().Load(); const auto limit = configured("APS5_PIPELINE_JOURNAL_ENTRIES", 1024u); - if (journal.entries.size() > limit) journal.entries.resize(static_cast(limit)); + if (journal.entries.size() > limit) journal.entries.erase(journal.entries.begin(), journal.entries.end() - static_cast(limit)); loaded = std::move(journal); } catch (const std::exception& error) { std::fprintf(stderr, "[pipeline-journal] cannot load %s: %s\n", Journal::Instance().Path().string().c_str(), error.what()); @@ -954,6 +1377,27 @@ private: } } + std::shared_ptr readModule(const ShaderRecompiler::PipelineIdentity& identity, const LoadedModule& module) const { + auto bytes = std::make_shared>(static_cast(module.payloadBytes)); + if (loaded.file == nullptr || !loaded.file->Read(module.payloadOffset, *bytes) || ShaderRecompiler::HashBytes(*bytes) != module.payloadHash) return nullptr; + auto result = std::make_shared(); + if (!ShaderRecompiler::ShaderDiskCache::DecodeResult(std::span(*bytes).subspan(20), *result, bytes) || !(result->Identity() == identity)) return nullptr; + return result; + } + + struct Held { + explicit Held(std::uint64_t bytes) : bytes(bytes) { + auto& counters = replayCounters(); + const auto now = counters.held.fetch_add(bytes, std::memory_order_relaxed) + bytes; + auto peak = counters.heldPeak.load(std::memory_order_relaxed); + while (peak < now && !counters.heldPeak.compare_exchange_weak(peak, now, std::memory_order_relaxed)) {} + } + ~Held() { replayCounters().held.fetch_sub(bytes, std::memory_order_relaxed); } + Held(const Held&) = delete; + Held& operator=(const Held&) = delete; + std::uint64_t bytes; + }; + void replay(const LoadedEntry& entry) { struct Replaying { Replaying() { replayingHere = true; } @@ -966,19 +1410,27 @@ private: counters.rejected.fetch_add(1, std::memory_order_relaxed); return; } - (hooks.compute(*module->second.result, entry.pushConstants) ? counters.compute : counters.present).fetch_add(1, std::memory_order_relaxed); + const Held held(module->second.payloadBytes); + const auto result = readModule(module->first, module->second); + if (result == nullptr) { + counters.rejected.fetch_add(1, std::memory_order_relaxed); + return; + } + (hooks.compute(*result, entry.pushConstants) ? counters.compute : counters.present).fetch_add(1, std::memory_order_relaxed); return; } - std::vector shaders; + std::vector*> modules; + std::uint64_t bytes = 0; for (const auto& stage : entry.graphics.stages) { const auto module = loaded.modules.find(stage.identity); if (module == loaded.modules.end() || module->second.stage != stage.stage) { counters.rejected.fetch_add(1, std::memory_order_relaxed); return; } - shaders.push_back({stage.stage, module->second.result.get(), stage.pushConstantOffset}); + modules.push_back(&*module); + bytes += module->second.payloadBytes; } - if (shaders.empty() || context.descriptorCache == nullptr) { + if (modules.empty() || context.descriptorCache == nullptr) { counters.rejected.fetch_add(1, std::memory_order_relaxed); return; } @@ -987,6 +1439,18 @@ private: counters.skipped.fetch_add(1, std::memory_order_relaxed); return; } + const Held held(bytes); + std::vector> results; + std::vector shaders; + for (std::size_t index = 0; index < modules.size(); ++index) { + auto result = readModule(modules[index]->first, modules[index]->second); + if (result == nullptr) { + counters.rejected.fetch_add(1, std::memory_order_relaxed); + return; + } + shaders.push_back({entry.graphics.stages[index].stage, result.get(), entry.graphics.stages[index].pushConstantOffset}); + results.push_back(std::move(result)); + } const auto layouts = DescribeSetLayouts(context, shaders); if (!std::ranges::equal(layouts.mainKey, entry.graphics.mainLayoutKey) || !std::ranges::equal(layouts.fragmentKey, entry.graphics.fragmentLayoutKey) || layouts.layouts.split != entry.graphics.splitSets) { counters.rejected.fetch_add(1, std::memory_order_relaxed); @@ -1049,6 +1513,7 @@ State CanonicalPipelineState(const State& state) { canonical.rectList = state.rectList; canonical.dualSourceBlend = state.dualSourceBlend; canonical.reversedBlend = state.reversedBlend; + canonical.colorRop = state.colorRop; canonical.topology = state.topology; canonical.primitiveRestart = state.primitiveRestart; canonical.negativeOneToOne = state.negativeOneToOne; @@ -1133,6 +1598,7 @@ PipelineJournalCounters PipelineJournalTotals() { totals.replayFailed = replay.failed.load(std::memory_order_relaxed); totals.replayMs = replay.ms.load(std::memory_order_relaxed); totals.replayThreads = replay.threads.load(std::memory_order_relaxed); + totals.replayHeldPeakBytes = replay.heldPeak.load(std::memory_order_relaxed); return totals; } @@ -1152,10 +1618,10 @@ void FlushPipelineJournal() { void ReportPipelineJournal() { if (!profiling() || !Journal::Instance().Enabled()) return; const auto totals = PipelineJournalTotals(); - AgcDriver::ProfilePrint_nid_no_patch("[pipeline-journal] loaded %llu pipelines, %llu modules (%.1f MiB, %.0f ms); replay on %u threads: %llu graphics and %llu compute built, %llu present, %llu skipped (store full), %llu rejected, %llu failed in %.0f ms; recorded this run: %llu graphics, %llu compute, %llu modules (%.1f MiB), %llu write failures\n", + AgcDriver::ProfilePrint_nid_no_patch("[pipeline-journal] loaded %llu pipelines, %llu modules (%.1f MiB, %.0f ms); replay on %u threads: %llu graphics and %llu compute built, %llu present, %llu skipped (store full), %llu rejected, %llu failed in %.0f ms, holding at most %.1f MiB of modules; recorded this run: %llu graphics, %llu compute, %llu modules (%.1f MiB), %llu write failures, %llu records evicted (%.1f MiB)\n", static_cast(totals.loadedRecipes), static_cast(totals.loadedModules), static_cast(totals.loadedBytes) / (1024.0 * 1024.0), totals.loadMs, totals.replayThreads, - static_cast(totals.replayedGraphics), static_cast(totals.replayedCompute), static_cast(totals.replayPresent), static_cast(totals.replaySkipped), static_cast(totals.replayRejected), static_cast(totals.replayFailed), totals.replayMs, - static_cast(totals.recordedGraphics), static_cast(totals.recordedCompute), static_cast(totals.recordedModules), static_cast(totals.writtenBytes) / (1024.0 * 1024.0), static_cast(totals.writeFailures)); + static_cast(totals.replayedGraphics), static_cast(totals.replayedCompute), static_cast(totals.replayPresent), static_cast(totals.replaySkipped), static_cast(totals.replayRejected), static_cast(totals.replayFailed), totals.replayMs, static_cast(totals.replayHeldPeakBytes) / (1024.0 * 1024.0), + static_cast(totals.recordedGraphics), static_cast(totals.recordedCompute), static_cast(totals.recordedModules), static_cast(totals.writtenBytes) / (1024.0 * 1024.0), static_cast(totals.writeFailures), static_cast(totals.evictedRecords), static_cast(totals.evictedBytes) / (1024.0 * 1024.0)); } std::uint64_t PipelineJournalDeviceFingerprint(const VkPhysicalDeviceProperties& properties) { diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp index 63948985f7..80eb8c355d 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp @@ -32,6 +32,7 @@ #include #include #include +#include #include #include #include @@ -2134,6 +2135,22 @@ void Recorder::NoteSampledDraw(VkCommandBuffer commands) { if (open->sampleActive) open->samplesDrawn = true; } +void Recorder::NoteDrawTraceToken(std::uint64_t token) noexcept { + if (token == 0 || open == nullptr) return; + try { + auto inserted = open->drawTraceTokenSet.insert(token); + if (!inserted.second) return; + try { + open->drawTraceTokens.push_back(token); + } catch (...) { + open->drawTraceTokenSet.erase(inserted.first); + throw; + } + } catch (...) { + AgcDriver::CaptureTrace::Log("draw-token-note-failed token=%llu", static_cast(token)); + } +} + bool Recorder::gpuSampleCounter() { if (sampleCounterState != 0) return sampleCounterState > 0; sampleCounterState = -1; @@ -2794,9 +2811,13 @@ void Recorder::NoteUnwrittenDrawSnapshot(std::uint64_t address, std::size_t byte unwrittenBytes += bytes; } -std::shared_ptr Recorder::ReusableDrawSnapshot(std::uint64_t address, std::size_t bytes, SnapshotUse use, std::uint32_t* derived, std::uint64_t generation) { +std::shared_ptr Recorder::ReusableDrawSnapshot(std::uint64_t address, std::size_t bytes, SnapshotUse use, std::uint32_t* derived, std::uint64_t generation, VkDeviceSize* offset) { auto found = use == SnapshotUse::Vertex ? drawSnapshots.lower_bound({address, use, bytes}) : drawSnapshots.find({address, use, bytes}); - if (found == drawSnapshots.end() || std::get<0>(found->first) != address || std::get<1>(found->first) != use) return {}; + if (found == drawSnapshots.end() || std::get<0>(found->first) != address || std::get<1>(found->first) != use) { + if (use != SnapshotUse::Storage || offset == nullptr) return {}; + return containingDrawSnapshot(address, bytes, offset); + } + if (offset != nullptr) *offset = 0; const bool sameRegistry = found->second.registryGeneration == GuestAllocations::GuestAllocationsGeneration_nid_postfix(); if (!sameRegistry || !GuestMemory::UnchangedSince(address, bytes, found->second.generation)) { if (!sameRegistry || generation == 0 || use != SnapshotUse::Storage || !refreshDrawSnapshot(found->second, address, bytes)) { @@ -2811,6 +2832,23 @@ std::shared_ptr Recorder::ReusableDrawSnapshot(std::uint64_t address, st return found->second.buffer; } +std::shared_ptr Recorder::containingDrawSnapshot(std::uint64_t address, std::size_t bytes, VkDeviceSize* offset) { + const VkDeviceSize alignment = std::max(context.limits.minStorageBufferOffsetAlignment, 4); + const auto registry = GuestAllocations::GuestAllocationsGeneration_nid_postfix(); + auto entry = drawSnapshots.upper_bound({address, SnapshotUse::Storage, std::numeric_limits::max()}); + for (std::size_t probe = 0; probe < ContainingSnapshotProbes && entry != drawSnapshots.begin(); ++probe) { + --entry; + const auto& [start, use, held] = entry->first; + if (use != SnapshotUse::Storage || start + held < address + bytes || (address - start) % alignment != 0) continue; + if (entry->second.registryGeneration != registry || !GuestMemory::UnchangedSince(address, bytes, entry->second.generation)) continue; + auto& recency = drawSnapshotPools[SnapshotPool(use)].recency; + recency.splice(recency.end(), recency, entry->second.recent); + *offset = address - start; + return entry->second.buffer; + } + return {}; +} + bool Recorder::refreshDrawSnapshot(DrawSnapshot& entry, std::uint64_t address, std::size_t bytes) { const auto held = entry.buffer->Bytes(); if (held.size() != bytes || bytes == 0) return false; @@ -3596,7 +3634,13 @@ void Recorder::Submit() { submission.pCommandBuffers = &batch->commands; // The timeline reaches this batch's serial when it completes (see WaitSerial). const std::uint64_t serial = submissions + 1; - CaptureTrace::Log("submit batch=%llu reads=%zu writes=%zu", static_cast(serial), batch->reads.size(), batch->writes.size()); + std::string tokensStr = "["; + for (std::size_t i = 0; i < batch->drawTraceTokens.size(); ++i) { + if (i > 0) tokensStr += ","; + tokensStr += std::to_string(batch->drawTraceTokens[i]); + } + tokensStr += "]"; + CaptureTrace::Log("submit batch=%llu tokens=%s", static_cast(serial), tokensStr.c_str()); VkTimelineSemaphoreSubmitInfoKHR timelineInfo{VK_STRUCTURE_TYPE_TIMELINE_SEMAPHORE_SUBMIT_INFO_KHR}; const VkSemaphore hold = std::exchange(submitHold, VK_NULL_HANDLE); const std::uint64_t holdValue = submitHoldValue; @@ -3625,6 +3669,7 @@ void Recorder::Submit() { batch->submitted = true; batch->serial = ++submissions; batch->submittedAt = std::chrono::steady_clock::now(); + CaptureTrace::Log("submit-confirmed batch=%llu tokens=%s", static_cast(batch->serial), tokensStr.c_str()); if (DrawProfiled()) { const auto us = std::chrono::duration(batch->submittedAt - submitStart).count(); ++submitCount; @@ -3654,6 +3699,10 @@ std::uint64_t Recorder::SubmitAndEpoch() { return inFlight.empty() ? 0 : submissions; } +std::uint64_t Recorder::OpenBatchSerial() { + return open != nullptr ? submissions + 1 : 0; +} + namespace { // One thread's registration as a timeline waiter (see `unlockedWaiters`): constructed while the @@ -3707,6 +3756,20 @@ void Recorder::WaitSerial(std::uint64_t serial) { Check(result, "vkWaitSemaphoresKHR recorder"); } +bool Recorder::WaitSerialFor(std::uint64_t serial, std::uint64_t timeoutNanos) { + PerformanceTimer frameTiming("Recorder.WaitSerialFor"); + if (timeline == VK_NULL_HANDLE || serial == 0) return false; + const UnlockedWaiter waiter{id}; + VkSemaphoreWaitInfoKHR wait{VK_STRUCTURE_TYPE_SEMAPHORE_WAIT_INFO_KHR}; + wait.semaphoreCount = 1; + wait.pSemaphores = &timeline; + wait.pValues = &serial; + const auto result = context.Function("vkWaitSemaphoresKHR")(context.device, &wait, timeoutNanos); + if (result == VK_TIMEOUT) return false; + Check(result, "vkWaitSemaphoresKHR submission progress"); + return true; +} + void Recorder::FinishUpTo(std::uint64_t serial) { // A drain: the wait already happened (unlocked), the fences are signaled, so the source-0 wait // recorded here is the bookkeeping only (a caller that did not wait first, WaitForLeases, shows diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp index e437e9d1d5..84bc841971 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp @@ -646,7 +646,7 @@ std::uint64_t sampledBudget(const Context& context, TextureCache& cache) { context.memoryProperties2(context.physical, &properties); const auto sampled = SampledTextureMemory(); const auto storage = storageCacheBytes(); - const auto budget = SampledTextureBudget(context.memory, &reported, sampled + storage); + const auto budget = SampledTextureBudget(context.memory, &reported, sampled + storage, storage); const bool fresh = cache.budgetDevice != context.physical; const auto storageLimit = storageBudget(context); if (const auto heap = largestDeviceLocalHeap(context.memory); heap && (fresh || SampledBudgetReportDue(cache.reportedBudget, budget, now - cache.reportedAt) || SampledBudgetReportDue(cache.reportedStorage, storageLimit, now - cache.reportedAt))) { @@ -1196,12 +1196,12 @@ std::shared_ptr cachedStorageTexture(const Context& context, std reportGuestAllocation(viewed, viewedBytes); } } - if (DepthPlanesStale() || written) static_cast(FlushDepthPlanes(viewed.baseAddress, static_cast(viewedBytes), written ? DepthPlaneScope::Whole : DepthPlaneScope::Overlapped)); + static const bool disabled = std::getenv("APS5_NO_TEXTURE_CACHE") != nullptr; + if ((DepthPlanesStale() || written) && (written || !layers || disabled)) static_cast(FlushDepthPlanes(viewed.baseAddress, static_cast(viewedBytes), written ? DepthPlaneScope::Whole : DepthPlaneScope::Overlapped)); if (written && DepthSurfaceOverlaps(context.device, viewed.baseAddress, viewedBytes)) { const auto storage = WrittenStorageBytes(viewed, mip); RetireCoveredDepthSurfaces(context.device, storage.bytes, storage.extents); } - static const bool disabled = std::getenv("APS5_NO_TEXTURE_CACHE") != nullptr; if (disabled) return StorageTexture::Create(context, *context.detiler, viewed, mip); static_cast(words); if (auto* recorder = Recorder::Active(); recorder != nullptr) recorder->BoundKeptBytes(); @@ -1289,16 +1289,16 @@ std::uint64_t StorageTextureBudget(const VkPhysicalDeviceMemoryProperties& memor const auto budget = reported->heapBudget[*heap]; const auto usage = reported->heapUsage[*heap]; const auto reserved = usage - std::min(usage, sampledBytes + storageBytes) + sampledBytes + budget / 8u; - const auto cap = std::min(TextureCacheBudget(memory), budget / 4u); + const auto cap = std::min(memory.memoryHeaps[*heap].size / 2u, budget / 2u); return std::max(512ull << 20u, std::min(cap, budget > reserved ? budget - reserved : 0u)); } -std::uint64_t SampledTextureBudget(const VkPhysicalDeviceMemoryProperties& memory, const VkPhysicalDeviceMemoryBudgetPropertiesEXT* reported, std::uint64_t textureBytes) { +std::uint64_t SampledTextureBudget(const VkPhysicalDeviceMemoryProperties& memory, const VkPhysicalDeviceMemoryBudgetPropertiesEXT* reported, std::uint64_t textureBytes, std::uint64_t storageBytes) { const auto heap = largestDeviceLocalHeap(memory); if (reported == nullptr || !heap || reported->heapBudget[*heap] == 0) return TextureCacheBudget(memory); const auto budget = reported->heapBudget[*heap]; const auto usage = reported->heapUsage[*heap]; - const auto reserved = usage - std::min(usage, textureBytes) + TextureCacheBudget(memory) + budget / 8u; + const auto reserved = usage - std::min(usage, textureBytes) + std::max(TextureCacheBudget(memory), storageBytes) + budget / 8u; return std::max(2048ull << 20u, budget > reserved ? budget - reserved : 0u); } @@ -5671,8 +5671,9 @@ std::shared_ptr ShaderResources::PrepareDrawBindi if (verifySnapshots && buffer != nullptr) { static std::atomic checked{0}, differing{0}; const auto fast = buffer->Bytes().subspan(static_cast(offset), bytes); - const auto slow = recorder.ReusableDrawSnapshot(begin, bytes, Recorder::SnapshotUse::Storage, nullptr, GuestMemory::CollectWrites(begin, bytes)); - const bool same = slow == buffer || (slow != nullptr && slow->Bytes().size() == fast.size() && std::memcmp(slow->Bytes().data(), fast.data(), fast.size()) == 0) || (slow == nullptr && std::memcmp(fast.data(), reinterpret_cast(begin), bytes) == 0); + VkDeviceSize slowOffset = 0; + const auto slow = recorder.ReusableDrawSnapshot(begin, bytes, Recorder::SnapshotUse::Storage, nullptr, GuestMemory::CollectWrites(begin, bytes), &slowOffset); + const bool same = (slow == buffer && slowOffset == offset) || (slow != nullptr && slow->Bytes().size() >= slowOffset + fast.size() && std::memcmp(slow->Bytes().data() + slowOffset, fast.data(), fast.size()) == 0) || (slow == nullptr && std::memcmp(fast.data(), reinterpret_cast(begin), bytes) == 0); if (!same) differing.fetch_add(1); if (checked.fetch_add(1) % 200000 == 199999) std::fprintf(stderr, "[draw-snapshots] verify: %llu fast answers checked, %llu differing%c", static_cast(checked.load()), static_cast(differing.load()), 10); } @@ -5685,7 +5686,7 @@ std::shared_ptr ShaderResources::PrepareDrawBindi offset = slice.offset; } else { const auto generation = GuestMemory::CollectWrites(begin, bytes); - buffer = recorder.ReusableDrawSnapshot(begin, bytes, Recorder::SnapshotUse::Storage, nullptr, generation); + buffer = recorder.ReusableDrawSnapshot(begin, bytes, Recorder::SnapshotUse::Storage, nullptr, generation, &offset); if (buffer == nullptr) { buffer = std::make_shared(context, bytes, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT); std::memcpy(buffer->Bytes().data(), reinterpret_cast(begin), bytes); diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp index 56436ff48f..de94232f92 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp @@ -382,30 +382,19 @@ VkStencilOpState stencilFace(std::uint32_t compare, std::uint32_t ops, std::uint return face; } -void decodeDepth(const Registers& cx, std::uint32_t depthControl, bool testValExport, bool opValExport, State& result) { - const bool opValReference = opValExport && StencilOperationValueReplaces(cx); - zero(cx, 0x000, 0x0000101cu, "depth copy, resummarize or decompress draws (DB_RENDER_CONTROL)"); - const bool depthClear = (read(cx, 0x000) & 1u) != 0; - const auto view = read(cx, 0x002); - zero(cx, 0x010, 0x00001000u, "partially resident depth (DB_Z_INFO)"); +DepthTarget decodeDepthPlane(const Registers& cx) { + const auto viewRegister = find(cx, 0x002); + const auto view = viewRegister == cx.end() ? 0u : viewRegister->second; const auto viewMip = (view >> 26u) & 0xfu; const auto maxMip = (read(cx, 0x010) >> 16u) & 0xfu; - zero(cx, 0x011, 0x00001000u, "partially resident stencil (DB_STENCIL_INFO)"); const auto zFormat = read(cx, 0x010) & 3u; - const bool stencil = (read(cx, 0x011) & 1u) != 0; - const bool stencilClear = stencil && (read(cx, 0x000) & 2u) != 0; - Require(zFormat != 2, "Z_24 depth is unsupported"); - if (zFormat == 0) depthControl &= ~6u; - if (!stencil) depthControl &= ~1u; + const auto stencilRegister = find(cx, 0x011); + const auto stencilInfo = stencilRegister == cx.end() ? 0u : stencilRegister->second; + const bool stencil = (stencilInfo & 1u) != 0; const auto base = [&](std::uint32_t low, std::uint32_t highOffset) { const auto high = find(cx, highOffset); return (high == cx.end() ? 0ull : static_cast(high->second & 0xffu) << 40u) | (static_cast(read(cx, low)) << 8u); }; - const bool depthReadOnly = (view & 0x01000000u) != 0; - const bool stencilReadOnly = (view & 0x02000000u) != 0; - Require(!stencilClear || !stencilReadOnly, "stencil clear requires a writable stencil plane"); - Require(!stencilClear || !testValExport, "stencil clear with a pixel shader that exports the stencil test value is unsupported"); - Require(!depthClear || (zFormat != 0 && !depthReadOnly), "depth clear requires a writable depth plane"); DepthTarget depth{}; depth.samples = 1u << ((read(cx, 0x010) >> 2u) & 3u); depth.depthSwizzle = (read(cx, 0x010) >> 4u) & 31u; @@ -414,9 +403,6 @@ void decodeDepth(const Registers& cx, std::uint32_t depthControl, bool testValEx depth.lastSlice = (view >> 13u) & 0x7ffu; depth.address = zFormat != 0 ? base(0x012, 0x01a) : 0; depth.stencilAddress = stencil ? base(0x013, 0x01b) : 0; - const bool depthWritten = zFormat != 0 && !depthReadOnly && (depthClear || (depthControl & 6u) == 6u || (read(cx, 0x000) & 0x60u) != 0); - Require(!depthWritten || base(0x014, 0x01c) == depth.address, "depth read and written at different addresses is unsupported"); - Require(!stencil || stencilReadOnly || base(0x015, 0x01d) == depth.stencilAddress, "stencil read and written at different addresses is unsupported"); if (depth.samples != 1u) { depth.pipeBankXor = static_cast(depth.address & 65535u); depth.address &= ~std::uint64_t{65535u}; @@ -453,13 +439,44 @@ void decodeDepth(const Registers& cx, std::uint32_t depthControl, bool testValEx if (depth.stencilAddress != 0) depth.stencilAddress += static_cast(slice) * DepthSliceBytes(depth.extent, 1u); } depth.format = zFormat == 1 ? (stencil ? VK_FORMAT_D16_UNORM_S8_UINT : VK_FORMAT_D16_UNORM) : (stencil ? VK_FORMAT_D32_SFLOAT_S8_UINT : VK_FORMAT_D32_SFLOAT); - depth.clearDepth = readFloat(cx, 0x00b); - depth.clearStencil = static_cast(read(cx, 0x00a) & 0xffu); - depth.htileDepthOnly = (read(cx, 0x011) & (1u << 29u)) != 0u; + depth.htileDepthOnly = (stencilInfo & (1u << 29u)) != 0u; if (const auto htile = find(cx, 0x005); zFormat != 0 && htile != cx.end() && htile->second != 0 && (read(cx, 0x010) & (1u << 29u)) != 0) { depth.htileAddress = base(0x005, 0x01e) + static_cast(view & 0x1fffu) * HtileMetadataBytes(depth.extent); depth.htileStencil = stencil && !depth.htileDepthOnly; } + return depth; +} + +void decodeDepth(const Registers& cx, std::uint32_t depthControl, bool testValExport, bool opValExport, State& result) { + const bool opValReference = opValExport && StencilOperationValueReplaces(cx); + zero(cx, 0x000, 0x0000101cu, "depth copy, resummarize or decompress draws (DB_RENDER_CONTROL)"); + const bool depthClear = (read(cx, 0x000) & 1u) != 0; + const auto view = read(cx, 0x002); + zero(cx, 0x010, 0x00001000u, "partially resident depth (DB_Z_INFO)"); + zero(cx, 0x011, 0x00001000u, "partially resident stencil (DB_STENCIL_INFO)"); + const auto zFormat = read(cx, 0x010) & 3u; + const bool stencil = (read(cx, 0x011) & 1u) != 0; + const bool stencilClear = stencil && (read(cx, 0x000) & 2u) != 0; + Require(zFormat != 2, "Z_24 depth is unsupported"); + if (zFormat == 0) depthControl &= ~6u; + if (!stencil) depthControl &= ~1u; + const auto base = [&](std::uint32_t low, std::uint32_t highOffset) { + const auto high = find(cx, highOffset); + return (high == cx.end() ? 0ull : static_cast(high->second & 0xffu) << 40u) | (static_cast(read(cx, low)) << 8u); + }; + const bool depthReadOnly = (view & 0x01000000u) != 0; + const bool stencilReadOnly = (view & 0x02000000u) != 0; + Require(!stencilClear || !stencilReadOnly, "stencil clear requires a writable stencil plane"); + Require(!stencilClear || !testValExport, "stencil clear with a pixel shader that exports the stencil test value is unsupported"); + Require(!depthClear || (zFormat != 0 && !depthReadOnly), "depth clear requires a writable depth plane"); + const auto readAddress = zFormat != 0 ? base(0x012, 0x01a) : 0; + const auto stencilReadAddress = stencil ? base(0x013, 0x01b) : 0; + const bool depthWritten = zFormat != 0 && !depthReadOnly && (depthClear || (depthControl & 6u) == 6u || (read(cx, 0x000) & 0x60u) != 0); + Require(!depthWritten || base(0x014, 0x01c) == readAddress, "depth read and written at different addresses is unsupported"); + Require(!stencil || stencilReadOnly || base(0x015, 0x01d) == stencilReadAddress, "stencil read and written at different addresses is unsupported"); + auto depth = decodeDepthPlane(cx); + depth.clearDepth = readFloat(cx, 0x00b); + depth.clearStencil = static_cast(read(cx, 0x00a) & 0xffu); result.depth = depth; result.depthTest = (depthControl & 2u) != 0; result.depthWrite = result.depthTest && (depthControl & 4u) != 0 && !depthReadOnly; @@ -590,6 +607,11 @@ constexpr std::uint32_t CoveredCentroidIsCenter = 1u << 29u; constexpr std::uint32_t SampleZeroRefusedInputs = ShaderRecompiler::PixelInputBit(ShaderRecompiler::PixelInput::PerspectiveCentroid) | ShaderRecompiler::PixelInputBit(ShaderRecompiler::PixelInput::LinearCentroid) | ShaderRecompiler::PixelInputBit(ShaderRecompiler::PixelInput::SampleCoverage); constexpr std::uint32_t CentroidInputs = ShaderRecompiler::PixelInputBit(ShaderRecompiler::PixelInput::PerspectiveCentroid) | ShaderRecompiler::PixelInputBit(ShaderRecompiler::PixelInput::LinearCentroid); +bool staticAnchors(const Registers& cx, std::uint32_t depthSamples) { + const auto eqaa = find(cx, 0x201); + return eqaa != cx.end() && (eqaa->second & 7u) == static_cast(std::countr_zero(depthSamples)) && (eqaa->second & 0x100000u) != 0; +} + bool overrasterized(const Registers& cx, std::uint32_t samples) { const auto eqaa = find(cx, 0x201); const auto mode = find(cx, 0x292); @@ -713,15 +735,19 @@ void rasterizeSampleZero(State& state) { state.sampleLocationGrid = sampleZero[8] != sampleZero[0] || sampleZero[16] != sampleZero[0] || sampleZero[24] != sampleZero[0]; } +bool documentedRop(std::uint32_t rop) { + return (rop >> 4u) == (rop & 0xfu) || ((rop >> 2u) & 0x33u) == (rop & 0x33u); +} + bool colorControlSupported(std::uint32_t colorControl, bool hasColorTarget) { colorControl &= ~1u; - return colorControl == 0xcc0010u || ((colorControl >> 4u) & 7u) == 0u || (!hasColorTarget && (colorControl & ~0x70u) == 0xcc0000u); + return ((colorControl & ~0xff0000u) == 0x10u && documentedRop((colorControl >> 16u) & 0xffu)) || ((colorControl >> 4u) & 7u) == 0u || (!hasColorTarget && (colorControl & ~0x70u) == 0xcc0000u); } std::string colorControlMessage(std::uint32_t colorControl) { static constexpr const char* modes[8] = {"disable", "normal", "eliminate fast clear", "resolve", "decompress", "FMASK decompress", "DCC decompress", "reserved"}; char text[160]; - std::snprintf(text, sizeof(text), "AGC graphics: only normal color rendering with copy ROP is supported (CB_COLOR_CONTROL 0x%08x, mode %s)", colorControl, modes[(colorControl >> 4u) & 7u]); + std::snprintf(text, sizeof(text), "AGC graphics: only normal color rendering with a ROP3 code of the source or brush form is supported (CB_COLOR_CONTROL 0x%08x, mode %s)", colorControl, modes[(colorControl >> 4u) & 7u]); return text; } @@ -1095,6 +1121,21 @@ std::array QuadSampleMasks(const State& state, const SampleOrd return {mask, mask, mask, mask}; } +VkLogicOp LogicOpFromRop(std::uint8_t rop) { + if (!documentedRop(rop)) { + char text[96]; + std::snprintf(text, sizeof(text), "AGC graphics: ROP3 code 0x%02x combines source and brush, which is not implemented", rop); + throw std::runtime_error(text); + } + static constexpr std::array operations{ + VK_LOGIC_OP_CLEAR, VK_LOGIC_OP_NOR, VK_LOGIC_OP_AND_INVERTED, VK_LOGIC_OP_COPY_INVERTED, + VK_LOGIC_OP_AND_REVERSE, VK_LOGIC_OP_INVERT, VK_LOGIC_OP_XOR, VK_LOGIC_OP_NAND, + VK_LOGIC_OP_AND, VK_LOGIC_OP_EQUIVALENT, VK_LOGIC_OP_NO_OP, VK_LOGIC_OP_OR_INVERTED, + VK_LOGIC_OP_COPY, VK_LOGIC_OP_OR_REVERSE, VK_LOGIC_OP_OR, VK_LOGIC_OP_SET}; + const std::uint32_t index = (rop & 3u) | ((rop >> 4u) & 4u) | ((rop >> 4u) & 8u); + return operations[index]; +} + std::array SampleLocations(const State& state) { Require(state.samples == 1 || state.samples == 2 || state.samples == 4 || state.samples == 8, "invalid sample count"); std::array locations{}; @@ -1395,8 +1436,9 @@ State DecodeState(const QueueState& queue, bool vertexWorkgroup) { zero(cx, 0x292, ScanModeMask & ~1u, "scan conversion mode"); if (depthKeepsSampleZero && (read(cx, 0x292) & 1u) == 0) { const bool filled = result.topology != VK_PRIMITIVE_TOPOLOGY_POINT_LIST && result.topology != VK_PRIMITIVE_TOPOLOGY_LINE_LIST && result.topology != VK_PRIMITIVE_TOPOLOGY_LINE_STRIP; - const auto sampleInputs = read(cx, 0x1b3) & read(cx, 0x1b4) & (ShaderRecompiler::PixelInputBit(ShaderRecompiler::PixelInput::Ancillary) | ShaderRecompiler::PixelInputBit(ShaderRecompiler::PixelInput::SampleCoverage)); - Require(filled && !result.alphaToCoverage && (PixelProgramSkipped(queue) || sampleInputs == 0) && (read(cx, 0x203) & 0x100u) == 0 && (read(cx, 0x201) & 0x0f080000u) == 0, "a one-sample depth target under a multisampled rasterizer without MSAA_ENABLE is unsupported for points or lines and with alpha-to-coverage, a sample index or coverage input, a sample-mask export, or DB_EQAA source Z interpolation or overrasterization"); + const auto sampleIndex = (read(cx, 0x293) & PixelShaderIteratesSamples) != 0u ? ShaderRecompiler::PixelInputBit(ShaderRecompiler::PixelInput::Ancillary) : 0u; + const auto sampleInputs = read(cx, 0x1b3) & read(cx, 0x1b4) & (sampleIndex | ShaderRecompiler::PixelInputBit(ShaderRecompiler::PixelInput::SampleCoverage)); + Require(filled && !result.alphaToCoverage && (PixelProgramSkipped(queue) || sampleInputs == 0) && (read(cx, 0x203) & 0x100u) == 0 && (read(cx, 0x201) & 0x0f080000u) == 0, "a one-sample depth target under a multisampled rasterizer without MSAA_ENABLE is unsupported for points or lines and with alpha-to-coverage, a sample index input under PS_ITER_SAMPLE, a coverage input, a sample-mask export, or DB_EQAA source Z interpolation or overrasterization"); result.samples = 1; result.customSampleLocations = false; result.sampleLocations.fill(0); @@ -1437,7 +1479,7 @@ State DecodeState(const QueueState& queue, bool vertexWorkgroup) { if (auto reason = sampleMaskRejection(queue, read(cx, 0x2f8), read(cx, 0x30e), read(cx, 0x30f)); !reason.empty()) throw std::runtime_error(reason); const auto viewportControl = read(cx, 0x206); if (viewportControl != 0x43fu) throw std::runtime_error(vteMessage(viewportControl)); - zero(cx, 0x204, ClipControlMask, "unsupported PA_CL_CLIP_CNTL flags"); + zero(cx, 0x204, ClipControlMask, "a PA_CL_CLIP_CNTL flag"); result.depthClamp = (read(cx, 0x204) & 0x0c000000u) != 0; result.negativeOneToOne = (read(cx, 0x204) & 0x80000u) == 0; const auto raster = read(cx, 0x205); @@ -1474,6 +1516,7 @@ State DecodeState(const QueueState& queue, bool vertexWorkgroup) { // CB_COLOR_CONTROL mode 0 disables color writes, which only matters when a target is written. if (const auto colorControl = read(cx, 0x202); !colorControlSupported(colorControl, result.hasColorTarget)) throw std::runtime_error(colorControlMessage(colorControl)); + if (const auto colorControl = read(cx, 0x202); ((colorControl >> 4u) & 7u) == 1u) result.colorRop = static_cast(colorControl >> 16u); zero(cx, 0x1c4, zFormatSupported(read(cx, 0x1c4)) ? 0u : ~0u, "depth or sample-mask export"); const auto exportFormat = result.hasColorTarget ? read(cx, 0x1c5) : 0u; APS5_LOG_OUT_DEBUG("Export format=%u", exportFormat); @@ -1492,9 +1535,10 @@ State DecodeState(const QueueState& queue, bool vertexWorkgroup) { color.uintExport = slotExport == 7; if (color.samples == 1 && result.samples > 1 && result.colors.empty() && !result.depth && (read(cx, 0x292) & 1u) == 0) { const auto sampleInputs = read(cx, 0x1b3) & read(cx, 0x1b4) & (ShaderRecompiler::PixelInputBit(ShaderRecompiler::PixelInput::Ancillary) | ShaderRecompiler::PixelInputBit(ShaderRecompiler::PixelInput::SampleCoverage)); - Require(!result.perSampleShading && sampleInputs == 0 && (read(cx, 0x203) & 0x100u) == 0, "a one-sample color target under a multisampled rasterizer without MSAA_ENABLE is unsupported with PS_ITER_SAMPLE, a sample index or coverage input, or a sample-mask export"); + Require(sampleInputs == 0 && (read(cx, 0x203) & 0x100u) == 0, "a one-sample color target under a multisampled rasterizer without MSAA_ENABLE is unsupported with a sample index or coverage input, or a sample-mask export"); result.samples = 1; result.customSampleLocations = false; + result.perSampleShading = false; } if (color.samples > 1 && result.samples == 1 && result.colors.empty() && !result.depth && !result.alphaToCoverage && (read(cx, 0x292) & 1u) == 0) { Require(result.conservativeRasterization == VK_CONSERVATIVE_RASTERIZATION_MODE_DISABLED_EXT || (read(cx, 0x1b3) & 0x44u) == 0, "conservative rasterization with centroid interpolation of a draw raised to a multisampled target is unsupported"); @@ -1502,10 +1546,12 @@ State DecodeState(const QueueState& queue, bool vertexWorkgroup) { result.customSampleLocations = true; result.sampleLocations.fill(0); } - const bool merged = color.samples == 1u && !result.depth && !result.perSampleShading && (result.colors.empty() || result.mergedCoverage) && overrasterized(cx, result.samples); - if (!merged && color.samples == 1 && result.colors.empty() && !result.depth && rasterizesSampleZeroOnly(cx, result)) rasterizeSampleZero(result); + const bool merged = color.samples == 1u && color.coverageSamples == 1u && !result.depth && !result.perSampleShading && (result.colors.empty() || result.mergedCoverage) && overrasterized(cx, result.samples); + if (!merged && color.samples == 1 && color.coverageSamples == 1u && result.colors.empty() && !result.depth && rasterizesSampleZeroOnly(cx, result)) rasterizeSampleZero(result); + Require(color.coverageSamples == color.samples || color.coverageSamples == result.samples || (result.samples == 1u && (read(cx, 0x292) & 1u) == 0), "a one-fragment color target's sample count differs from the rasterizer's"); const bool truncated = color.samples == 1u && result.samples > 1u && result.depth && result.depth->samples == result.samples && (result.colors.empty() || result.truncatedCoverage) && !overrasterized(cx, result.samples); Require((color.samples == result.samples && !result.mergedCoverage && !result.truncatedCoverage) || merged || truncated, "a color target's sample count differs from the rasterizer's"); + Require(color.coverageSamples == color.samples || result.samples == 1u || staticAnchors(cx, result.depth->samples), "a one-fragment color target is supported only with static DB_EQAA anchors at the depth target's sample count"); result.mergedCoverage = merged; result.truncatedCoverage = truncated; APS5_LOG_OUT_DEBUG("Color %u address=0x%llx extent=%ux%u bytes=%llu VkFormat=%u", slot, static_cast(color.address), color.extent.width, color.extent.height, static_cast(color.bytes), static_cast(color.format)); @@ -1689,6 +1735,9 @@ State DecodeState(const QueueState& queue, bool vertexWorkgroup) { result.color = result.colors.front(); result.blend = result.blends[result.colors.front().exportIndex]; } + if (LogicOpFromRop(result.colorRop) != VK_LOGIC_OP_COPY) { + for (const auto& blend : result.blends) Require(blend.blendEnable == VK_FALSE, "a color logic operation together with blending is not implemented"); + } Require(result.reversedBlend == 0 || (exportSlots.size() == 1 && exportCount == 1), "blending into a color target with a reversed component order through a second source color needs that target to be the shader's only color export"); if (result.dualSourceBlend) { Require(exportSlots.size() == 2 && exportSlots[0] == 0 && exportSlots[1] == 1 && exportCount == 1, "dual-source blending needs pixel exports to MRT slots 0 and 1 and color writes to slot 0 alone"); @@ -1881,10 +1930,12 @@ ColorTarget DecodeColorBuffer(const Registers& cx, std::uint32_t slot, bool allo const auto decoded = DecodeColorFormat(format, number, swap); const auto attrib = read(cx, 0x31d + stride); zero(cx, 0x31d + stride, ~0x3f000u, "color destination alpha override or other CB_COLOR_ATTRIB modes"); - color.samples = 1u << ((attrib >> 12u) & 7u); - Require(color.samples <= 8u, "color targets with more than eight samples are unsupported"); - Require((1u << ((attrib >> 15u) & 3u)) == color.samples, "color targets with fewer fragments than samples (EQAA) are unsupported"); - const auto multisampleInfo = color.samples > 1 ? 0x0c006000u : 0u; + color.coverageSamples = 1u << ((attrib >> 12u) & 7u); + Require(color.coverageSamples <= 8u, "color targets with more than eight samples are unsupported"); + color.samples = 1u << ((attrib >> 15u) & 3u); + Require(color.samples <= color.coverageSamples, "color targets with more fragments than samples are unsupported"); + Require(color.samples == color.coverageSamples || color.samples == 1u, "color targets with more than one but fewer fragments than samples (EQAA) are unsupported"); + const auto multisampleInfo = color.coverageSamples > 1 ? 0x0c006000u : 0u; // ROUND_MODE (bit 18) only affects unorm rounding and packed float truncation. With DCC_ENABLE (bit 28) the target is written if ((info & ~(0x00039f7cu | 0x00040000u | 0x10000000u | 0x00002000u | multisampleInfo)) != 0) throw std::runtime_error("AGC graphics: color compression, DCC, endian conversion, nonstandard rounding or color optimization is unsupported (CB_COLOR_INFO 0x" + [&] { char text[16]; std::snprintf(text, sizeof(text), "%08x", info); return std::string(text); }() + ")"); Require((info & 0x8000u) != 0 || number <= 1u || number == 7 || number == 4 || number == 5, "unclamped normalized color is unsupported"); @@ -2024,6 +2075,11 @@ ColorTarget DecodeColorBuffer(const Registers& cx, std::uint32_t slot, bool allo color.fmaskAddress = address - color.fmaskPipeBankXor; } } + if (color.coverageSamples > color.samples && (info & 0x4000u) != 0) { + const auto fmaskHigh = find(cx, 0x3a0 + slot); + color.fmaskAddress = (((fmaskHigh == cx.end() ? 0ull : static_cast(fmaskHigh->second & 0xffu)) << 40u) | (static_cast(read(cx, 0x321 + stride)) << 8u)) & ~0xffffull; + Require(color.fmaskAddress != 0, "a compressed one-fragment color target without an FMASK address is unsupported"); + } if ((info & 0x10000000u) != 0) { if (maxMip == 0) { const auto dccHigh = find(cx, 0x3a8 + slot); @@ -2080,28 +2136,20 @@ std::optional DecodeDepthCopyPass(const QueueState& queue) { Require((control->second & ~0xf84u) == 0, "DB depth copy combined with a stencil copy, clear, resummarize or decompress"); Require((read(cx, 0x200) & 0xfu) == 0, "DB depth copy with depth or stencil tests"); Require((read(cx, 0x010) & 3u) == 3u, "DB depth copy from a depth plane that is not Z_32_FLOAT"); - Require(ColorWriteMask(cx) == 1u, "DB depth copy into anything but the red channel of color target 0"); const auto viewRegister = find(cx, 0x002); - const auto view = viewRegister == cx.end() ? 0u : viewRegister->second; - Require((read(cx, 0x010) & 0x000f1000u) == 0 && (view & 0x3c000000u) == 0, "DB depth copy from a mipmapped or partially resident depth plane"); + Require((read(cx, 0x010) & 0x000f1000u) == 0 && ((viewRegister == cx.end() ? 0u : viewRegister->second) & 0x3c000000u) == 0, "DB depth copy from a mipmapped or partially resident depth plane"); + Require(ColorWriteMask(cx) == 1u, "DB depth copy into anything but the red channel of color target 0"); + Require((read(cx, 0x1e0) & (1u << 30u)) == 0, "DB depth copy with color target 0 blending is unsupported"); DepthCopyPass pass{}; - const auto high = find(cx, 0x01a); - pass.depthAddress = (high == cx.end() ? 0ull : static_cast(high->second & 0xffu) << 40u) | (static_cast(read(cx, 0x012)) << 8u); - const auto size = read(cx, 0x007); - pass.extent = {(size & 0x3fffu) + 1u, ((size >> 16u) & 0x3fffu) + 1u}; - pass.samples = 1u << ((read(cx, 0x010) >> 2u) & 3u); - if (pass.samples != 1u) pass.depthAddress &= ~std::uint64_t{65535u}; - if (const auto slice = view & 0x1fffu; slice != 0) pass.depthAddress += static_cast(slice) * DepthSliceBytes(pass.extent, 4u); + pass.source = decodeDepthPlane(cx); const auto maskLow = find(cx, 0x30e); const auto maskHigh = find(cx, 0x30f); Require((maskLow == cx.end() || maskLow->second == 0xffffffffu) && (maskHigh == cx.end() || maskHigh->second == 0xffffffffu), "DB depth copy with a sample mask is unsupported"); pass.sample = (control->second & 0x80u) != 0 ? 0u : (control->second >> 8u) & 0xfu; - Require(pass.sample < pass.samples, "DB depth copy of a sample the depth plane does not have"); + Require(pass.sample < pass.source.samples, "DB depth copy of a sample the depth plane does not have"); pass.color = DecodeColorBuffer(cx, 0); Require(pass.color.format == VK_FORMAT_R32_SFLOAT && pass.color.samples == 1 && pass.color.mip == 0 && pass.color.layers == 1, "DB depth copy into a color target that is not a single-sample R32 float"); - Require(pass.color.extent.width == pass.extent.width && pass.color.extent.height == pass.extent.height, "DB depth copy into a color target of another size"); - Require(pass.color.cmaskAddress == 0, "DB depth copy into a color target with CMASK fast clears is unsupported"); - Require(drawCovers(cx, pass.extent), "DB depth copy over part of the depth surface"); + Require(pass.color.extent.width == pass.source.extent.width && pass.color.extent.height == pass.source.extent.height, "DB depth copy into a color target of another size"); return pass; } @@ -2249,7 +2297,7 @@ std::string DrawRejection(const QueueState& queue, bool indexed) { if (auto reason = sampleMaskRejection(queue, config, word, other); !reason.empty()) return reason; } if (value(cx, 0x206, word) && word != 0x43fu) return vteMessage(word); - if (auto reason = nonzero(cx, 0x204, ClipControlMask, "unsupported PA_CL_CLIP_CNTL flags"); !reason.empty()) return reason; + if (auto reason = nonzero(cx, 0x204, ClipControlMask, "a PA_CL_CLIP_CNTL flag"); !reason.empty()) return reason; std::uint32_t targetMask = 0, shaderMask = 0; if (value(cx, 0x8e, targetMask) && value(cx, 0x8f, shaderMask) && value(cx, 0x202, word) && !colorControlSupported(word, ColorWriteMask(cx) != 0)) return colorControlMessage(word); if (auto reason = nonzero(cx, 0x1c4, zFormatSupported(zFormat) ? 0u : ~0u, "depth or sample-mask export"); !reason.empty()) return reason; diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/Texture.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/Texture.cpp index 5f4a53f178..d8394b6c5e 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/Texture.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/Texture.cpp @@ -2066,16 +2066,22 @@ bool StorageTexture::Refresh() { } bool StorageTexture::RefreshLayers(std::uint32_t first, std::uint32_t count) { - if (count == 0 || geometry.thick || geometry.imageLayers <= 1 || first >= geometry.imageLayers || count > geometry.imageLayers - first || geometry.layerBytes == 0 || trackedLayerBytes == 0 || descriptor.dccAddress != 0) return Refresh(); + const auto whole = [this] { + static_cast(FlushDepthPlanes(descriptor.baseAddress, static_cast(guestBytes))); + return Refresh(); + }; + if (count == 0 || geometry.thick || geometry.imageLayers <= 1 || first >= geometry.imageLayers || count > geometry.imageLayers - first || geometry.layerBytes == 0 || trackedLayerBytes == 0 || descriptor.dccAddress != 0) return whole(); const auto begin = geometry.GuestLayerOffset(first); const auto end = geometry.GuestLayerOffset(first + count - 1) + geometry.layerBytes; const auto firstLayer = static_cast(begin / trackedLayerBytes); const auto lastLayer = static_cast(std::min((end + trackedLayerBytes - 1) / trackedLayerBytes, trackedLayers)); - if (firstLayer >= lastLayer || (firstLayer == 0 && lastLayer == trackedLayers)) return Refresh(); + if (firstLayer >= lastLayer || (firstLayer == 0 && lastLayer == trackedLayers)) return whole(); for (std::uint32_t layer = 0; layer < trackedLayers; ++layer) { - if (layerPending[layer] && (layer < firstLayer || layer >= lastLayer)) return Refresh(); + if (layerPending[layer] && (layer < firstLayer || layer >= lastLayer)) return whole(); } - if (pendingAlias() != nullptr) return Refresh(); + if (pendingAlias() != nullptr) return whole(); + const auto scopeBegin = layerBegin(firstLayer); + static_cast(FlushDepthPlanes(scopeBegin, static_cast(layerBegin(lastLayer - 1) + layerBytes(lastLayer - 1) - scopeBegin))); return refresh(firstLayer, lastLayer); } diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/TextureDetiler.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/TextureDetiler.cpp index c571bd1710..344d80c594 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/TextureDetiler.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/TextureDetiler.cpp @@ -4,8 +4,6 @@ #include "prx/libSceAgcDriver/Graphics/include/TextureTiling.hpp" #include "prx/libSceAgcDriver/Graphics/shaders/TextureDetile_spv.h" #include "prx/libSceAgcDriver/Graphics/shaders/CmaskClear_spv.h" -#include "prx/libSceAgcDriver/Graphics/shaders/DepthSampleMultisample_spv.h" -#include "prx/libSceAgcDriver/Graphics/shaders/DepthSampleSingle_spv.h" #include "prx/libSceAgcDriver/Graphics/shaders/TextureDetileImage_spv.h" #include "prx/libSceAgcDriver/Graphics/shaders/TextureRepack_spv.h" #include @@ -120,11 +118,6 @@ void TextureDetiler::release() noexcept { cmaskErrors.reset(); for (const auto pool : descriptorSets.pools) context.Function("vkDestroyDescriptorPool")(context.device, pool, nullptr); for (const auto pool : imageDescriptorSets.pools) context.Function("vkDestroyDescriptorPool")(context.device, pool, nullptr); - for (const auto pool : depthSampleSets.pools) context.Function("vkDestroyDescriptorPool")(context.device, pool, nullptr); - for (const auto pipeline : depthSamplePipelines) if (pipeline) context.Function("vkDestroyPipeline")(context.device, pipeline, nullptr); - if (depthSamplePipelineLayout) context.Function("vkDestroyPipelineLayout")(context.device, depthSamplePipelineLayout, nullptr); - if (depthSampleLayout) context.Function("vkDestroyDescriptorSetLayout")(context.device, depthSampleLayout, nullptr); - if (depthSampler) context.Function("vkDestroySampler")(context.device, depthSampler, nullptr); if (imageModule) context.Function("vkDestroyShaderModule")(context.device, imageModule, nullptr); if (repackPipeline) context.Function("vkDestroyPipeline")(context.device, repackPipeline, nullptr); if (repackModule) context.Function("vkDestroyShaderModule")(context.device, repackModule, nullptr); @@ -402,79 +395,6 @@ void TextureDetiler::DispatchCmaskClear(VkCommandBuffer commands, VkBuffer cmask barrier(VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT | VK_PIPELINE_STAGE_HOST_BIT, VK_ACCESS_TRANSFER_WRITE_BIT, anyAccess); } -void TextureDetiler::DispatchDepthSample(VkCommandBuffer commands, VkImageView depth, bool multisampled, VkImageView target, std::uint32_t width, std::uint32_t height, std::uint32_t sample) { - Require(commands != VK_NULL_HANDLE && depth != VK_NULL_HANDLE && target != VK_NULL_HANDLE, "a depth sample copy needs a command buffer, the depth view and the target view"); - if (depthSamplePipelineLayout == VK_NULL_HANDLE) { - VkSamplerCreateInfo samplerInfo{VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO}; - samplerInfo.magFilter = VK_FILTER_NEAREST; - samplerInfo.minFilter = VK_FILTER_NEAREST; - samplerInfo.addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE; - samplerInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE; - samplerInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE; - Check(context.Function("vkCreateSampler")(context.device, &samplerInfo, nullptr, &depthSampler), "vkCreateSampler depth sample copy"); - std::array bindings{}; - bindings[0] = {0, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, VK_SHADER_STAGE_COMPUTE_BIT, &depthSampler}; - bindings[1] = {1, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1, VK_SHADER_STAGE_COMPUTE_BIT, nullptr}; - VkDescriptorSetLayoutCreateInfo layoutInfo{VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO}; - layoutInfo.bindingCount = static_cast(bindings.size()); - layoutInfo.pBindings = bindings.data(); - Check(context.Function("vkCreateDescriptorSetLayout")(context.device, &layoutInfo, nullptr, &depthSampleLayout), "vkCreateDescriptorSetLayout depth sample copy"); - const VkPushConstantRange pushRange{VK_SHADER_STAGE_COMPUTE_BIT, 0, 3 * sizeof(std::uint32_t)}; - VkPipelineLayoutCreateInfo pipelineLayoutInfo{VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO}; - pipelineLayoutInfo.setLayoutCount = 1; - pipelineLayoutInfo.pSetLayouts = &depthSampleLayout; - pipelineLayoutInfo.pushConstantRangeCount = 1; - pipelineLayoutInfo.pPushConstantRanges = &pushRange; - Check(context.Function("vkCreatePipelineLayout")(context.device, &pipelineLayoutInfo, nullptr, &depthSamplePipelineLayout), "vkCreatePipelineLayout depth sample copy"); - } - auto& depthSamplePipeline = depthSamplePipelines[multisampled ? 1u : 0u]; - if (depthSamplePipeline == VK_NULL_HANDLE) { - VkShaderModuleCreateInfo moduleInfo{VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO}; - moduleInfo.codeSize = multisampled ? sizeof(DEPTH_SAMPLE_MULTISAMPLE_SPV) : sizeof(DEPTH_SAMPLE_SINGLE_SPV); - moduleInfo.pCode = multisampled ? DEPTH_SAMPLE_MULTISAMPLE_SPV : DEPTH_SAMPLE_SINGLE_SPV; - VkShaderModule depthModule = VK_NULL_HANDLE; - Check(context.Function("vkCreateShaderModule")(context.device, &moduleInfo, nullptr, &depthModule), "vkCreateShaderModule depth sample copy"); - VkComputePipelineCreateInfo pipelineInfo{VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO}; - pipelineInfo.stage = {VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO}; - pipelineInfo.stage.stage = VK_SHADER_STAGE_COMPUTE_BIT; - pipelineInfo.stage.module = depthModule; - pipelineInfo.stage.pName = "main"; - pipelineInfo.layout = depthSamplePipelineLayout; - const auto created = context.Function("vkCreateComputePipelines")(context.device, context.pipelineCache, 1, &pipelineInfo, nullptr, &depthSamplePipeline); - context.Function("vkDestroyShaderModule")(context.device, depthModule, nullptr); - Check(created, "vkCreateComputePipelines depth sample copy"); - } - const auto set = allocateFrom(depthSampleSets, depthSampleLayout, {{VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 64}, {VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 64}}, "vkCreateDescriptorPool depth sample copy", "vkAllocateDescriptorSets depth sample copy", "vkResetDescriptorPool depth sample copy"); - const VkDescriptorImageInfo depthInfo{VK_NULL_HANDLE, depth, VK_IMAGE_LAYOUT_GENERAL}; - const VkDescriptorImageInfo targetInfo{VK_NULL_HANDLE, target, VK_IMAGE_LAYOUT_GENERAL}; - std::array writes{}; - for (std::uint32_t index = 0; index < writes.size(); ++index) { - writes[index].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; - writes[index].dstSet = set; - writes[index].dstBinding = index; - writes[index].descriptorCount = 1; - writes[index].descriptorType = index == 0 ? VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER : VK_DESCRIPTOR_TYPE_STORAGE_IMAGE; - } - writes[0].pImageInfo = &depthInfo; - writes[1].pImageInfo = &targetInfo; - context.Function("vkUpdateDescriptorSets")(context.device, static_cast(writes.size()), writes.data(), 0, nullptr); - const auto barrier = [&](VkPipelineStageFlags source, VkPipelineStageFlags destination, VkAccessFlags sourceAccess, VkAccessFlags destinationAccess) { - VkMemoryBarrier memory{VK_STRUCTURE_TYPE_MEMORY_BARRIER}; - memory.srcAccessMask = sourceAccess; - memory.dstAccessMask = destinationAccess; - context.Function("vkCmdPipelineBarrier")(commands, source, destination, 0, 1, &memory, 0, nullptr, 0, nullptr); - }; - constexpr VkAccessFlags anyWrite = VK_ACCESS_SHADER_WRITE_BIT | VK_ACCESS_TRANSFER_WRITE_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT | VK_ACCESS_HOST_WRITE_BIT; - constexpr VkAccessFlags anyAccess = anyWrite | VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_TRANSFER_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_HOST_READ_BIT; - barrier(VK_PIPELINE_STAGE_ALL_COMMANDS_BIT | VK_PIPELINE_STAGE_HOST_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, anyWrite, VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT); - context.Function("vkCmdBindPipeline")(commands, VK_PIPELINE_BIND_POINT_COMPUTE, depthSamplePipeline); - context.Function("vkCmdBindDescriptorSets")(commands, VK_PIPELINE_BIND_POINT_COMPUTE, depthSamplePipelineLayout, 0, 1, &set, 0, nullptr); - const std::array push{width, height, sample}; - context.Function("vkCmdPushConstants")(commands, depthSamplePipelineLayout, VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(push), push.data()); - context.Function("vkCmdDispatch")(commands, (width + 7u) / 8u, (height + 7u) / 8u, 1); - barrier(VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT | VK_PIPELINE_STAGE_HOST_BIT, VK_ACCESS_SHADER_WRITE_BIT, anyAccess); -} - void TextureDetiler::DispatchImage(VkCommandBuffer commands, TextureTileMode tileMode, std::uint32_t elementBytes, VkBuffer tiled, std::uint64_t tiledOffset, VkImageView view, const TileMipLayout& layout, bool retile, std::uint32_t slice, const DetileWindow& window) { Require(commands != VK_NULL_HANDLE, "texture detiling requires an active command buffer"); Require(window.rangeBegin < window.rangeEnd && window.tiledBase <= window.rangeBegin, "texture detiling window is empty"); diff --git a/core/libs/prx/libSceAgcDriver/tests/AsyncLog.cpp b/core/libs/prx/libSceAgcDriver/tests/AsyncLog.cpp index f334f4b036..a370fe2802 100644 --- a/core/libs/prx/libSceAgcDriver/tests/AsyncLog.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/AsyncLog.cpp @@ -243,7 +243,12 @@ void FullQueueBlocksCaller() { constexpr int ChildLines = 50; [[noreturn]] void EndChild(const std::string& mode, const char* output) { - std::FILE* file = std::fopen(output, "wb"); + const auto path = std::filesystem::temp_directory_path() / output; +#ifdef _WIN32 + std::FILE* file = _wfopen(path.c_str(), L"wb"); +#else + std::FILE* file = std::fopen(path.c_str(), "wb"); +#endif Check(file != nullptr, "child output file"); #ifdef _WIN32 SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX); @@ -288,12 +293,18 @@ std::string Quote(const std::string& text) { void LinesSurviveEnd(const std::string& self, const std::string& mode) { const auto output = std::filesystem::temp_directory_path() / ("aps5-async-log-" + mode + "-" + std::to_string(Clock::now().time_since_epoch().count()) + ".txt"); - std::string command = Quote(self) + " child " + mode + " " + Quote(output.string()); #ifdef _WIN32 - command = Quote(command); -#endif + (void)self; + std::wstring executable(32768, L'\0'); + executable.resize(GetModuleFileNameW(nullptr, executable.data(), static_cast(executable.size()))); + const std::wstring command = L"\"\"" + executable + L"\" child " + std::filesystem::path(mode).wstring() + L" \"" + output.filename().wstring() + L"\"\""; + const auto start = Clock::now(); + const int status = _wsystem(command.c_str()); +#else + const std::string command = Quote(self) + " child " + mode + " " + Quote(output.filename().string()); const auto start = Clock::now(); const int status = std::system(command.c_str()); +#endif const auto lived = Clock::now() - start; std::error_code error; const bool started = std::filesystem::exists(output, error); diff --git a/core/libs/prx/libSceAgcDriver/tests/BdaResources.cpp b/core/libs/prx/libSceAgcDriver/tests/BdaResources.cpp index 3050513ebb..c2c44c6aed 100644 --- a/core/libs/prx/libSceAgcDriver/tests/BdaResources.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/BdaResources.cpp @@ -602,6 +602,9 @@ void importJoinTests(const Context& context) { Require(pieces != nullptr && pieces->base == heapBase && pieces->bytes == 2 * piece && piecesBuffer == pieces->buffer && piecesCopied == 0, "a buffer bound exactly over two pieces no larger than the import window was not imported as one"); change(heap + 2 * piece, piece, true); static_cast(bind(heapBase + 2 * piece - 16)); + const auto [prefixBuffer, prefixCopied] = bind(heapBase, 2 * piece + 16); + const auto* prefix = HostImportFor(importing, heapBase, 2 * piece + 16); + Require(prefix != nullptr && prefix->base == heapBase && prefix->bytes == 3 * piece && prefixBuffer == prefix->buffer && prefixCopied == 0, "a buffer bound from the first of three pieces into the last was not imported over the pieces it crosses"); const auto* kept = HostImportFor(importing, first, 16); Require(kept != nullptr && kept->buffer == joinedBuffer && kept->bytes == 2 * large, "a registry change elsewhere re-imported the joined ranges"); change(guest + 2 * large, large, true); @@ -880,7 +883,7 @@ private: void importMappedFailureGenerationTests(const Context& context) { ClearHostImports(context.device); - WindowedRange range(context, WindowedRange::piece, 1); + WindowedRange range(context, 2 * WindowedRange::piece, 1); ImportAllocationProbe probe(range.importing); probe.failure = VK_ERROR_INVALID_EXTERNAL_HANDLE; GuestAllocations::Mapped mapped; @@ -1081,7 +1084,7 @@ void importNeighbouringEdgesTests(const Context& context) { // observer holds no GPU lock, so it must not evict (before, it evicted the least recently used import to fit). void importAtMapOverBudgetTests(const Context& context) { ClearHostImports(context.device); - constexpr std::size_t bytes = 65536; + constexpr std::size_t bytes = 2 * 65536; auto importing = context; importing.hostImportAlignment = bytes; void* kept = ::operator new(bytes, std::align_val_t{bytes}); @@ -1142,17 +1145,32 @@ void importRequesterTests(const Context& context, const BdaTestAccess& access) { const auto counted = [](ImportRequester requester) { return HostImportCounters().requesters[static_cast(requester)]; }; const auto made = [&](ImportRequester requester) { return counted(requester).made; }; Require(std::string(ImportRequesterName(ImportRequester::Map)) == "map" && std::string(ImportRequesterName(ImportRequester::Build)) == "build" && std::string(ImportRequesterName(ImportRequester::Access)) == "access" && std::string(ImportRequesterName(ImportRequester::Join)) == "join" && std::string(ImportRequesterName(ImportRequester::Segment)) == "segment", "an import requester has the wrong name"); - change(0, true); - { + const auto mappedRange = [&](std::size_t index) { GuestAllocations::Mapped ranges; const auto lease = GuestAllocations::GuestAllocationsAcquire_nid_postfix(); for (const auto& range : lease) { - if (range->address == address(0)) ranges.push_back(range); + if (range->address == address(index)) ranges.push_back(range); } Require(ranges.size() == 1, "the requester test's mapped range is not registered"); + return ranges; + }; + { + GuestAllocations::Mutation mutation; + mutation.Add(block + 6 * piece, 2 * piece, true, true, true); + } + { const auto before = made(ImportRequester::Map); - static_cast(ImportMappedRanges(importing, ranges, GuestAllocations::GuestAllocationsGeneration_nid_postfix(), true)); - Require(made(ImportRequester::Map) == before + 1 && HostImportCovers(importing, address(0), piece), "a range imported at map time was not counted for the map requester"); + static_cast(ImportMappedRanges(importing, mappedRange(6), GuestAllocations::GuestAllocationsGeneration_nid_postfix(), true)); + Require(made(ImportRequester::Map) == before + 1 && HostImportCovers(importing, address(6), 2 * piece), "a range imported at map time was not counted for the map requester"); + } + change(0, true); + { + const auto map = made(ImportRequester::Map); + const auto access = made(ImportRequester::Access); + static_cast(ImportMappedRanges(importing, mappedRange(0), GuestAllocations::GuestAllocationsGeneration_nid_postfix(), true)); + Require(made(ImportRequester::Map) == map && !HostImportCovers(importing, address(0), 16), "a range of 64 KiB was imported at map time"); + Require(HostImportFor(importing, address(0), 16) != nullptr && HostImportCovers(importing, address(0), piece) && made(ImportRequester::Access) == access + 1 && made(ImportRequester::Map) == map, + "a range of 64 KiB left to its first GPU use was not imported there, or not counted for that use's requester"); } change(1, true); { @@ -1230,6 +1248,10 @@ void importRequesterTests(const Context& context, const BdaTestAccess& access) { const auto counts = HostImportCounters(); Require(counts.live >= 4 && counts.liveBytes == HostImportLiveBytes(), "the live import counts disagree with the live bytes"); for (std::size_t index = 0; index < 6; ++index) change(index, false); + { + GuestAllocations::Mutation mutation; + mutation.Remove(block + 6 * piece); + } ClearHostImports(importing.device); ::operator delete(block, std::align_val_t{piece}); } @@ -2170,6 +2192,119 @@ ShaderRecompiler::DescriptorBinding binding(Role role, std::uint32_t slot) { return {ShaderRecompiler::DescriptorKind::StorageBuffer, role, 0, slot, 1, {}, false}; } +void partialHeapImportBudgetCase(const Context& context, const BdaTestAccess& access, std::size_t bytes, std::size_t budgetBytes, std::size_t prefixBytes) { + if (!GuestArena::GuestArenaAvailable_nid_postfix() || !GuestArena::GuestArenaWriteWatched_nid_postfix()) return; + constexpr std::size_t window = 2 * 65536; + const auto allocated = bytes + 65536; + void* block = GuestArena::GuestArenaAllocate_nid_postfix(allocated, 65536); +#ifdef _WIN32 + GuestArena::GuestArenaCommit_nid_postfix(block, allocated, PAGE_READWRITE, allocated); +#endif + auto* guest = static_cast(block); + std::memset(block, 0x39, bytes); + std::memset(guest + bytes, 0xa7, allocated - bytes); + const auto first = reinterpret_cast(block); + auto importing = context; + importing.hostImportAlignment = 65536; + const auto live = HostImportLiveBytes(); + const auto before = MirrorCounters(); + SetHostImportRangeWindow(window); + SetHostImportBudget(live + budgetBytes); + const auto registry = [&](bool add) { + auto* mutation = GuestAllocations::GuestAllocationsBegin_nid_postfix(); + if (add) GuestAllocations::GuestAllocationsAdd_nid_postfix(mutation, block, bytes, true, true, true); + else GuestAllocations::GuestAllocationsRemove_nid_postfix(mutation, block); + GuestAllocations::GuestAllocationsEnd_nid_postfix(mutation); + }; + registry(true); + { + auto withoutImports = context; + withoutImports.hostImportAlignment = 0; + GuestBufferMemory leased(withoutImports); + leased.AcquireRegistered(); + leased.AddWritable(first + bytes - 16, 4); + leased.Upload(true); + std::uint32_t adjustment = 0; + const auto view = leased.Descriptor(first + bytes - 16, 4, adjustment); + access.bytes(view.buffer)[view.offset + adjustment] = std::byte{0x43}; + leased.WriteBack(); + } + Require(MirrorCounters().heapBytes == before.heapBytes + bytes && guest[bytes - 16] == 0x43, "the partial-import fixture did not start with a valid full heap mirror"); + GuestAllocations::GuestAllocationsEnd_nid_postfix(GuestAllocations::GuestAllocationsBegin_nid_postfix()); + const auto build = [&](bool write) { + GuestBufferMemory leased(importing); + leased.AcquireRegistered(); + if (write) { + leased.AddWritable(first + 16, 32); + leased.AddWritable(first + bytes - 48, 32); + } + leased.Upload(true); + const auto* prefix = ExistingHostImportFor(importing, first, window); + Require(prefix != nullptr, "a heap exceeding the import budget did not import its fitting prefix"); + Require(HostImportLiveBytes() <= live + budgetBytes, "partial heap imports exceeded the configured budget"); + Require(MirrorCounters().heapBytes == before.heapBytes + bytes - prefixBytes, "a partially imported heap mirrored bytes already imported in place"); + auto covered = first; + for (const auto& entry : leased.AddressRanges()) { + if (entry.end <= first || entry.begin >= first + bytes) continue; + Require(entry.begin == covered && entry.end <= first + bytes, "partial heap BDA ranges have a gap or overlap"); + if (entry.end <= first + prefixBytes) { + const auto* imported = ExistingHostImportFor(importing, entry.begin, static_cast(entry.end - entry.begin)); + Require(imported != nullptr && imported->bytes <= window && entry.deviceAddress == imported->address + entry.begin - imported->base, "the partial heap prefix is not served by bounded imports"); + } + if (entry.begin >= first + prefixBytes) { + std::uint32_t adjustment = 0; + const auto view = leased.Descriptor(entry.begin, static_cast(entry.end - entry.begin), adjustment); + const auto data = access.bytes(view.buffer).subspan(view.offset + adjustment); + Require(data[0] == static_cast(guest[entry.begin - first]) && data[entry.end - entry.begin - 1] == static_cast(guest[entry.end - first - 1]), "a partial heap mirror lost its first or last bytes"); + } + covered = entry.end; + } + Require(covered == first + bytes, "a partially imported heap does not cover its registered range"); + if (write) { + for (const auto at : {first + 16, first + bytes - 48}) { + if (at < first + prefixBytes) guest[at - first] = 0x71; + else { + std::uint32_t adjustment = 0; + const auto view = leased.Descriptor(at, 32, adjustment); + access.bytes(view.buffer)[view.offset + adjustment] = std::byte{0x71}; + } + guest[at - first + 8] = 0x52; + } + } + leased.WriteBack(); + }; + build(true); + Require(guest[16] == 0x71 && guest[24] == 0x52 && guest[bytes - 48] == 0x71 && guest[bytes - 40] == 0x52, "partial heap write-back lost GPU or genuine CPU stores"); + guest[bytes - 1] = 0x67; + build(false); + const auto quiet = MirrorCounters(); + guest[bytes + 9] = 0x8a; + build(false); + Require(MirrorCounters().blocksCopied == quiet.blocksCopied, "a neighboring CPU write refreshed an unrelated partial heap mirror"); + Require(guest[bytes + 9] == 0x8a && guest[bytes] == 0xa7 && guest[allocated - 1] == 0xa7, "partial heap uses changed unregistered neighboring memory"); + registry(false); + { + GuestBufferMemory leased(importing); + leased.AcquireRegistered(); + leased.Upload(true); + leased.WriteBack(); + } + Require(MirrorCounters().heapBytes == before.heapBytes, "a partial heap mirror outlived its registered range"); + SetHostImportBudget(0); + SetHostImportRangeWindow(std::uint64_t{64} << 20u); + GuestArena::GuestArenaReset_nid_postfix(block, allocated); + GuestArena::GuestArenaRelease_nid_postfix(block, allocated); +} + +void partialHeapImportBudgetTests(const Context& context, const BdaTestAccess& access) { + constexpr std::size_t window = 2 * 65536; + partialHeapImportBudgetCase(context, access, 3 * window, 2 * window, 2 * window); + partialHeapImportBudgetCase(context, access, 3 * window + 65536, 3 * window, 2 * window); + SetHostImportMaxBytes(window); + partialHeapImportBudgetCase(context, access, 3 * window, 2 * window, 2 * window); + SetHostImportMaxBytes(0); +} + void importedHeapMirrorTests(const Context& context, const BdaTestAccess& access) { if (!GuestArena::GuestArenaAvailable_nid_postfix() || !GuestArena::GuestArenaWriteWatched_nid_postfix()) { std::cout << "guest arena unavailable or not write-watched: heap mirrors of imported ranges not tested\n"; @@ -3706,6 +3841,7 @@ void RunBdaResourceTests(const Context& context, const BdaTestAccess& access) { importReportRefusalMemoryTests(context, access); sparseImportedHeapTests(context, access); heapMirrorTests(context, access); + partialHeapImportBudgetTests(context, access); importedHeapMirrorTests(context, access); importWatchTests(); importedFreshTests(context); diff --git a/core/libs/prx/libSceAgcDriver/tests/CaptureTrace.cpp b/core/libs/prx/libSceAgcDriver/tests/CaptureTrace.cpp new file mode 100644 index 0000000000..b263f9e7b1 --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/tests/CaptureTrace.cpp @@ -0,0 +1,160 @@ +#if !defined(_WIN32) && !defined(_POSIX_C_SOURCE) +#define _POSIX_C_SOURCE 200809L +#endif + +#include "prx/libSceAgcDriver/Execution/include/CaptureTrace.hpp" + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace { + +void Check(bool condition, const char* message) { + if (!condition) throw std::runtime_error(message); +} + +void RemoveEnvironmentVariable(const char* name) { +#ifdef _WIN32 + _putenv_s(name, ""); +#else + unsetenv(name); +#endif +} + +bool FileExists(const char* path) { + std::error_code error; + return std::filesystem::exists(path, error) && !error; +} + +std::string ReadFile(const char* path) { + std::ifstream stream(path, std::ios::binary); + Check(stream.is_open(), "the trace file was not created at the expected path"); + std::ostringstream buffer; + buffer << stream.rdbuf(); + return buffer.str(); +} + +bool WaitForRecord(const char* path, const std::string& record) { + for (int attempt = 0; attempt < 100; ++attempt) { + if (FileExists(path) && ReadFile(path).find(record) != std::string::npos) return true; + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + } + return false; +} + +const char* RequiredPathVariable() { + const char* configured = std::getenv("APS5_CAPTURE_TRACE_PATH"); + Check(configured != nullptr && configured[0] != '\0', + "this case requires a non-empty APS5_CAPTURE_TRACE_PATH"); + return configured; +} + +void PathDefault() { + RemoveEnvironmentVariable("APS5_CAPTURE_TRACE_PATH"); + Check(std::string(AgcDriver::CaptureTracePath()) == "capture-trace.log", + "an absent APS5_CAPTURE_TRACE_PATH must select capture-trace.log"); +} + +void PathEmpty() { + const char* configured = std::getenv("APS5_CAPTURE_TRACE_PATH"); + Check(configured != nullptr && configured[0] == '\0', + "this case requires an empty APS5_CAPTURE_TRACE_PATH"); + Check(std::string(AgcDriver::CaptureTracePath()) == "capture-trace.log", + "an empty APS5_CAPTURE_TRACE_PATH must select capture-trace.log"); +} + +void PathExplicit() { + const char* configured = RequiredPathVariable(); + Check(std::string(AgcDriver::CaptureTracePath()) == std::string(configured), + "a non-empty APS5_CAPTURE_TRACE_PATH must be selected verbatim"); +} + +void ExplicitWrites() { + const char* configured = RequiredPathVariable(); + Check(AgcDriver::CaptureTrace::Enabled(), "APS5_CAPTURE_TRACE must enable capture tracing"); + AgcDriver::CaptureTrace::Log("capture-trace-explicit label=%d", 7); + Check(WaitForRecord(configured, "capture-trace-explicit label=7"), + "the configured trace path did not receive the capture record"); + std::remove(configured); +} + +void DefaultWrites() { + RemoveEnvironmentVariable("APS5_CAPTURE_TRACE_PATH"); + Check(AgcDriver::CaptureTrace::Enabled(), "APS5_CAPTURE_TRACE must enable capture tracing"); + Check(std::string(AgcDriver::CaptureTracePath()) == "capture-trace.log", + "an absent APS5_CAPTURE_TRACE_PATH must keep the default path"); + AgcDriver::CaptureTrace::Log("capture-trace-default label=%d", 5); + Check(WaitForRecord("capture-trace.log", "capture-trace-default label=5"), + "the default trace path did not receive the capture record"); + std::remove("capture-trace.log"); +} + +void OpenFailure() { + const char* configured = RequiredPathVariable(); + Check(!FileExists(configured), "the configured trace path must not already exist"); + Check(AgcDriver::CaptureTrace::Enabled(), "APS5_CAPTURE_TRACE must enable capture tracing"); + bool threw = false; + try { + AgcDriver::CaptureTrace::Log("capture-trace-failure label=%d", 9); + } catch (const std::exception& error) { + threw = true; + std::cerr << "unexpected exception: " << error.what() << '\n'; + } + Check(!threw, "an unopenable trace path must not throw from Log"); + Check(!FileExists(configured), "an unopenable trace path must not create the requested file"); + Check(!FileExists("capture-trace.log"), + "an unopenable trace path must not fall back to capture-trace.log"); +} + +void BestEffortOverflow() { + const char* configured = RequiredPathVariable(); + Check(AgcDriver::CaptureTrace::Enabled(), "APS5_CAPTURE_TRACE must enable capture tracing"); + const std::string oversized(4096, 'x'); + bool threw = false; + try { + AgcDriver::CaptureTrace::LogBestEffort("capture-trace-best-effort payload=%s", oversized.c_str()); + } catch (...) { + threw = true; + } + Check(!threw, "best-effort capture logging must not propagate formatting failures"); + std::remove(configured); +} + +void Disabled() { + RemoveEnvironmentVariable("APS5_CAPTURE_TRACE"); + Check(!AgcDriver::CaptureTrace::Enabled(), + "capture tracing must stay disabled when APS5_CAPTURE_TRACE is absent"); + AgcDriver::CaptureTrace::Log("capture-trace-disabled label=%d", 3); + Check(!FileExists("capture-trace.log"), + "disabled tracing must not initialize or write capture-trace.log"); +} + +} + +int main(int argc, char** argv) { + try { + if (argc != 2) throw std::runtime_error("expected exactly one test mode argument"); + const std::string mode = argv[1]; + if (mode == "path-default") PathDefault(); + else if (mode == "path-empty") PathEmpty(); + else if (mode == "path-explicit") PathExplicit(); + else if (mode == "explicit-writes") ExplicitWrites(); + else if (mode == "default-writes") DefaultWrites(); + else if (mode == "open-failure") OpenFailure(); + else if (mode == "best-effort-overflow") BestEffortOverflow(); + else if (mode == "disabled") Disabled(); + else throw std::runtime_error("unknown capture trace test mode: " + mode); + std::cout << "capture trace tests passed\n"; + return 0; + } catch (const std::exception& error) { + std::cerr << error.what() << '\n'; + return 1; + } +} diff --git a/core/libs/prx/libSceAgcDriver/tests/CmaskFastClear.cpp b/core/libs/prx/libSceAgcDriver/tests/CmaskFastClear.cpp index dfadd4aabe..5cf4e302e4 100644 --- a/core/libs/prx/libSceAgcDriver/tests/CmaskFastClear.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/CmaskFastClear.cpp @@ -155,6 +155,32 @@ void unknownCode(AgcDriver::VulkanDevice& device) { expect(device, color, [](std::uint32_t, std::uint32_t) { return Clear; }, "fast-cleared target eliminated after a CMASK code 3"); } +void expectRefused(const std::function& action, const std::string& message, const std::string& what) { + try { + action(); + } catch (const std::exception& error) { + Require(std::string(error.what()).find(message) != std::string::npos, what + ": refused with " + error.what()); + return; + } + throw std::runtime_error(what + ": was not refused"); +} + +void oneFragment(AgcDriver::VulkanDevice& device) { + auto color = target(ColorTileMode::RenderTarget); + color.coverageSamples = 4; + prepare(color, [](std::uint32_t tileX, std::uint32_t tileY) { return tileX == 2 && tileY == 0; }, 3); + expectRefused([&] { eliminate(device, color); }, "CMASK code 0x3 of a one-fragment color target is not implemented", "a one-fragment target with CMASK code 3"); + prepare(color, [](std::uint32_t, std::uint32_t) { return true; }); + eliminate(device, color); + expect(device, color, [](std::uint32_t, std::uint32_t) { return Clear; }, "a fast-cleared one-fragment target eliminated"); + prepare(color, [](std::uint32_t tileX, std::uint32_t tileY) { return tileX == 7 && tileY == 1; }); + eliminate(device, color); + expect(device, color, [](std::uint32_t x, std::uint32_t y) { return x >= 56 && y >= 8 ? Clear : Stale; }, "a one-fragment target with one fast-cleared tile eliminated"); + prepare(color, [](std::uint32_t tileX, std::uint32_t) { return tileX < 4; }, 0xc); + eliminate(device, color); + expect(device, color, [](std::uint32_t, std::uint32_t) { return Stale; }, "a one-fragment target with CMASK codes 0xc and 0xf eliminated"); +} + } int main() { @@ -175,6 +201,7 @@ int main() { run(*device, ColorTileMode::Linear, "linear"); run(*device, ColorTileMode::RenderTarget, "SW_64KB_R_X"); unknownCode(*device); + oneFragment(*device); device->WaitIdle(); device.reset(); { diff --git a/core/libs/prx/libSceAgcDriver/tests/ControlFlow.cpp b/core/libs/prx/libSceAgcDriver/tests/ControlFlow.cpp index 3febba4f40..b59161ab49 100644 --- a/core/libs/prx/libSceAgcDriver/tests/ControlFlow.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/ControlFlow.cpp @@ -445,6 +445,26 @@ void verifyProgram(const Program& program) { } } +const std::vector TestedHeaderLatchStore{ + 0xbe880380u, 0x7e020280u, 0xbf0a8408u, 0xbf840004u, 0xe0700000u, 0x80000100u, 0x80088108u, 0xbf82fffau, 0xbf810000u}; +const std::vector SelfLoopLatchStore{ + 0xbe880380u, 0x7e020280u, 0xe0700000u, 0x80000100u, 0x80088108u, 0xbf0a8408u, 0xbf85fffbu, 0xbf810000u}; + +void verifyEmptyLoopContinues() { + for (const auto& [name, code] : {std::pair{"a loop whose latch stores behind a tested header", &TestedHeaderLatchStore}, std::pair{"a single-block loop that stores", &SelfLoopLatchStore}}) { + auto graph = GraphBuilder{}.Build(RdnaInstructionDecoder{}.Decode(*code)); + Structurizer{}.Structurize(graph); + bool loops = false; + for (const auto& block : graph.blocks) { + if (!block.terminator.loopHeader) continue; + loops = true; + const auto& continueTarget = graph.FindBlock(block.terminator.continueBlock); + require(continueTarget.instructionBegin == continueTarget.instructionEnd, std::string(name) + ": the continue block " + std::to_string(continueTarget.id) + " of the loop at block " + std::to_string(block.id) + " holds instructions"); + } + require(loops, std::string(name) + ": no loop was structured"); + } +} + const std::vector DwordTableGetpcFirst{ 0xbe8c1f00u, 0x800cff0cu, 0x00000058u, 0x820d800du, 0x83928202u, 0xd4c40016u, 0x02000500u, 0x8f128212u, 0xf4000386u, 0x24000000u, 0xbf8cc07fu, 0x7d820003u, 0x808c0e0cu, 0x828d800du, 0xbefd210cu, 0x7e020281u, 0xbf820003u, 0x7e020282u, 0xbf820001u, 0x7e020283u, @@ -711,6 +731,18 @@ loop: s_cbranch_scc1 loop s_endpgm)", {0xbe880380u, 0x7e020280u, 0xe0700000u, 0x80000100u, 0x80088108u, 0xbf0a8408u, 0xbf85fffbu, 0xbf810000u}, Split::None}, + {"memory access in a loop latch behind a tested header", R"( + s_mov_b32 s8, 0 + v_mov_b32 v1, 0 +loop: + s_cmp_lt_u32 s8, 4 + s_cbranch_scc0 done + buffer_store_dword v1, off, s[0:3], 0 + s_add_u32 s8, s8, 1 + s_branch loop +done: + s_endpgm)", + TestedHeaderLatchStore, Split::None}, {"shared tail in a loop", R"( s_mov_b32 s8, 0 v_mov_b32 v1, 0 @@ -1421,6 +1453,7 @@ done: verifyCallLinkMaskReadByScalarInstruction(); verifyLargeLoopExitFallback(); verifyNestedExitForwarders(); + verifyEmptyLoopContinues(); verifyIrreducibleLoopEntry(); } catch (const std::exception& error) { std::fprintf(stderr, "%s\n", error.what()); diff --git a/core/libs/prx/libSceAgcDriver/tests/DepthSurfaceReuse.cpp b/core/libs/prx/libSceAgcDriver/tests/DepthSurfaceReuse.cpp index 79878c2766..cd876ad472 100644 --- a/core/libs/prx/libSceAgcDriver/tests/DepthSurfaceReuse.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/DepthSurfaceReuse.cpp @@ -1,4 +1,5 @@ #include "GraphicsTests.hpp" +#include "prx/libSceAgcDriver/Execution/include/GuestMemory.hpp" #include "prx/libSceAgcDriver/Graphics/include/DepthSurface.hpp" #include "prx/libSceAgcDriver/Graphics/include/GuestTextureResource.hpp" #include "prx/libSceAgcDriver/Graphics/include/Texture.hpp" @@ -13,6 +14,14 @@ #include #include #include +#ifdef _WIN32 +#ifndef NOMINMAX +#define NOMINMAX +#endif +#include +#else +#include +#endif namespace { @@ -417,6 +426,62 @@ void TestRetiredSurfaceRelease(const Context& context) { ClearDepthSurfaces(context.device); } +void TestUnmappedStalePlaneFlush(const Context& context) { + ClearDepthSurfaces(context.device); + constexpr VkExtent2D extent{256, 256}; + constexpr std::uint64_t unmapped = 0xa0000000; + DepthTarget target{unmapped, 0, extent, VK_FORMAT_D32_SFLOAT, 1.0f, 0}; + target.depthSwizzle = XorSwizzleMode(TextureTileMode::kZ64KBX); + Require(DepthSurfaceView(context, target, true) != VK_NULL_HANDLE, "a mirrorable depth surface must render"); + Require(DepthPlanesStale(), "a written mirrorable depth surface must leave its plane stale"); + std::vector> stored; + try { + stored = FlushDepthPlanes(unmapped, DepthSliceBytes(extent, 4u)); + } catch (const std::exception& error) { + Require(false, std::string("flushing a depth plane whose guest memory is not mapped must drop its results: ") + error.what()); + } + Require(stored.empty(), "a depth plane without mapped guest memory must not be stored"); + Require(!DepthPlanesStale(), "a dropped depth plane must not stay stale"); + ClearDepthSurfaces(context.device); +} + +void TestReadOnlyStalePlaneFlush(const Context& context) { + ClearDepthSurfaces(context.device); + constexpr std::size_t bytes = 1024 * 1024; +#ifdef _WIN32 + const auto release = [](void* pointer) { VirtualFree(pointer, 0, MEM_RELEASE); }; + std::unique_ptr mapping(VirtualAlloc(nullptr, bytes, MEM_RESERVE | MEM_COMMIT, PAGE_READONLY), release); + Require(mapping != nullptr, "cannot map a read-only depth plane"); +#else + const auto release = [](void* pointer) { munmap(pointer, bytes); }; + std::unique_ptr mapping(mmap(nullptr, bytes, PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0), release); + Require(mapping.get() != MAP_FAILED, "cannot map a read-only depth plane"); +#endif + const auto address = (reinterpret_cast(mapping.get()) + 65535u) & ~std::uintptr_t{65535u}; + constexpr VkExtent2D extent{256, 256}; + const auto planeBytes = DepthSliceBytes(extent, 4u); + Require(!AgcDriver::GuestMemory::CommittedRanges(address, planeBytes).empty(), "a read-only depth plane must remain readable"); + Require(AgcDriver::GuestMemory::CommittedRanges(address, planeBytes, true).empty(), "a read-only depth plane must not be writable"); + auto storageContext = context; + storageContext.formatProperties = [](VkPhysicalDevice, VkFormat, VkFormatProperties* properties) { + *properties = {}; + properties->optimalTilingFeatures = VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT | VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT | VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT; + }; + DepthTarget target{address, 0, extent, VK_FORMAT_D32_SFLOAT, 1.0f, 0}; + target.depthSwizzle = XorSwizzleMode(TextureTileMode::kZ64KBX); + Require(DepthSurfaceView(storageContext, target, true) != VK_NULL_HANDLE, "a read-only depth plane must render"); + Require(DepthPlanesStale(), "the read-only depth plane must have pending results"); + bool rejected = false; + try { + static_cast(FlushDepthPlanes(address, planeBytes)); + } catch (const std::runtime_error& error) { + rejected = std::string_view(error.what()).find("storage texture has no committed guest pages") != std::string_view::npos; + } + Require(rejected, "a mapped read-only depth plane must retain the unsupported-store rejection"); + Require(DepthPlanesStale(), "a mapped read-only depth plane must retain its pending results"); + ClearDepthSurfaces(context.device); +} + void RunDepthSurfaceReuseTests() { static int deviceTag = 0; Context context{}; @@ -432,6 +497,8 @@ void RunDepthSurfaceReuseTests() { TestMultisampleDepthOwnership(context); TestMultisampleStencilAliases(context); TestStencilUnormView(context); + TestUnmappedStalePlaneFlush(context); + TestReadOnlyStalePlaneFlush(context); constexpr std::uint64_t depth32 = 0x40000000; constexpr std::uint64_t depth16 = 0x50000000; constexpr std::uint64_t depthStencil = 0x60000000; diff --git a/core/libs/prx/libSceAgcDriver/tests/DeviceFault.cpp b/core/libs/prx/libSceAgcDriver/tests/DeviceFault.cpp index aceac056e4..b2df523ef9 100644 --- a/core/libs/prx/libSceAgcDriver/tests/DeviceFault.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/DeviceFault.cpp @@ -60,6 +60,25 @@ VkQueue FakeQueue() { return reinterpret_cast(static_cast(0x2000)); } +void Diagnostics() { + using AgcDriver::ChooseDeviceDiagnostics; + constexpr VkDeviceDiagnosticsConfigFlagsNV all = VK_DEVICE_DIAGNOSTICS_CONFIG_ENABLE_SHADER_DEBUG_INFO_BIT_NV | VK_DEVICE_DIAGNOSTICS_CONFIG_ENABLE_RESOURCE_TRACKING_BIT_NV | VK_DEVICE_DIAGNOSTICS_CONFIG_ENABLE_AUTOMATIC_CHECKPOINTS_BIT_NV | VK_DEVICE_DIAGNOSTICS_CONFIG_ENABLE_SHADER_ERROR_REPORTING_BIT_NV; + for (const bool configSupported : {false, true}) { + for (const bool checkpointsSupported : {false, true}) { + const auto off = ChooseDeviceDiagnostics(false, configSupported, checkpointsSupported); + Require(!off.config && !off.checkpoints && off.flags == 0, "diagnostics were chosen without APS5_NV_DIAGNOSTICS"); + } + } + for (const bool checkpointsSupported : {false, true}) { + const auto unsupported = ChooseDeviceDiagnostics(true, false, checkpointsSupported); + Require(!unsupported.config && !unsupported.checkpoints && unsupported.flags == 0, "diagnostics were chosen on a device without VK_NV_device_diagnostics_config"); + } + const auto alone = ChooseDeviceDiagnostics(true, true, false); + Require(alone.config && !alone.checkpoints && alone.flags == all, "the diagnostics config without checkpoint support did not enable every crash-dump flag alone"); + const auto both = ChooseDeviceDiagnostics(true, true, true); + Require(both.config && both.checkpoints && both.flags == all, "the diagnostics config did not add the diagnostic checkpoints the device supports"); +} + void FaultInfo() { faultCalls = 0; faultFails = false; @@ -139,6 +158,7 @@ void AddressKinds() { int main() { try { + Diagnostics(); FaultInfo(); FaultInfoFails(); Checkpoints(); diff --git a/core/libs/prx/libSceAgcDriver/tests/Flip.cpp b/core/libs/prx/libSceAgcDriver/tests/Flip.cpp index ec73dea01a..9e89fdd61c 100644 --- a/core/libs/prx/libSceAgcDriver/tests/Flip.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/Flip.cpp @@ -355,7 +355,11 @@ void testFailureLog() { std::fflush(stderr); const int saved = APS5_DUP(APS5_FILENO(stderr)); check(saved >= 0, "stderr could not be saved"); - check(std::freopen(path.string().c_str(), "wb", stderr) != nullptr, "stderr could not be redirected"); +#ifdef _WIN32 + check(_wfreopen(path.wstring().c_str(), L"wb", stderr) != nullptr, "stderr could not be redirected"); +#else + check(std::freopen(path.c_str(), "wb", stderr) != nullptr, "stderr could not be redirected"); +#endif std::string message; std::string error; try { diff --git a/core/libs/prx/libSceAgcDriver/tests/FrameCapture.cpp b/core/libs/prx/libSceAgcDriver/tests/FrameCapture.cpp new file mode 100644 index 0000000000..63bfa42046 --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/tests/FrameCapture.cpp @@ -0,0 +1,238 @@ +#include "prx/libSceAgcDriver/Execution/include/FrameCapture.hpp" +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef _WIN32 +#include +#define getpid _getpid +#else +#include +#endif + +namespace { + +using AgcDriver::FrameCall; +using AgcDriver::FrameCallKind; +using AgcDriver::FrameCaptureReader; +using AgcDriver::FrameCaptureWriter; +using AgcDriver::FrameMemoryChunk; +using AgcDriver::FrameMemoryKind; +using AgcDriver::FrameRangeKind; + +void check(bool condition, const std::string& message) { + if (!condition) { + std::fprintf(stderr, "FAILED: %s\n", message.c_str()); + std::exit(1); + } +} + +template +void checkThrows(TFunction&& function, const std::string& message) { + try { + function(); + } catch (const std::runtime_error&) { + return; + } + check(false, message); +} + +bool sameRegisters(const std::vector& left, const std::vector& right) { + if (left.size() != right.size()) return false; + for (std::size_t index = 0; index < left.size(); ++index) { + if (left[index].offset != right[index].offset || left[index].value != right[index].value) return false; + } + return true; +} + +bool sameCall(const FrameCall& left, const FrameCall& right) { + return left.kind == right.kind && left.ordinal == right.ordinal && left.timeNs == right.timeNs && left.thread == right.thread && left.accepted == right.accepted && left.completed == right.completed && left.queue == right.queue && + left.flags == right.flags && left.cpuFlip == right.cpuFlip && left.address == right.address && left.size == right.size && left.values == right.values && sameRegisters(left.context, right.context) && + sameRegisters(left.primitive, right.primitive); +} + +std::vector sampleCalls() { + std::vector calls; + FrameCall shader; + shader.kind = FrameCallKind::RegisterShader; + shader.ordinal = 1; + shader.address = 0x2000010000; + calls.push_back(shader); + FrameCall abi; + abi.kind = FrameCallKind::ResolveGraphicsStagesAbi; + abi.ordinal = 2; + abi.values = {0x2000010000, 0x2000020000, 0}; + abi.context = {{0x1a0, 7}, {0x1a1, 0xffffffffu}}; + abi.primitive = {{0x242, 3}}; + calls.push_back(abi); + FrameCall submit; + submit.kind = FrameCallKind::Submit; + submit.ordinal = 3; + submit.timeNs = 123456789; + submit.thread = 4242; + submit.accepted = 20; + submit.completed = 17; + submit.queue = 0x21; + submit.address = 0x2000030000; + submit.size = 4096; + calls.push_back(submit); + FrameCall flip; + flip.kind = FrameCallKind::Flip; + flip.ordinal = 4; + flip.cpuFlip = true; + flip.values = {1, 2, 0, static_cast(-5)}; + calls.push_back(flip); + FrameCall unregister; + unregister.kind = FrameCallKind::UnregisterVideoOutput; + unregister.ordinal = 5; + unregister.values = {1}; + calls.push_back(unregister); + FrameCall end; + end.kind = FrameCallKind::End; + end.ordinal = 6; + calls.push_back(end); + return calls; +} + +std::vector sampleMemory() { + constexpr std::size_t Block = FrameCaptureWriter::BlockBytes; + std::vector memory(Block * 150 + 4096); + for (std::size_t block = 0; block < 150; ++block) { + if (block % 3 == 1 || (block >= 10 && block < 90)) { + for (std::size_t at = 0; at < Block; at += 97) memory[block * Block + at] = static_cast((block * 131 + at) & 0xff); + } + } + memory[memory.size() - 1] = std::byte{0x5a}; + return memory; +} + +void truncateFile(const std::filesystem::path& path, std::uintmax_t removed) { + const auto size = std::filesystem::file_size(path); + std::filesystem::resize_file(path, size - removed); +} + +void testRoundTrip(const std::filesystem::path& directory) { + constexpr std::uint64_t Base = 0x2000000000; + const auto calls = sampleCalls(); + const auto memory = sampleMemory(); + std::vector delta(8192); + for (std::size_t at = 0; at < delta.size(); ++at) delta[at] = static_cast(at * 7); + std::uint64_t stored = 0; + { + FrameCaptureWriter writer(directory); + writer.WriteRange(Base, memory.size(), FrameRangeKind::Watched, 3, AgcDriver::FrameRangeAccess::Readable | AgcDriver::FrameRangeAccess::Gpu); + stored = writer.WriteSnapshot(Base, memory); + for (const auto& call : calls) writer.WriteCall(call); + writer.WriteDelta(1, 17, Base + 0x3000, delta); + writer.Finish("{\"magic\":\"aps5-frame-capture 1\"}\n"); + check(writer.RawBytes() == stored + delta.size(), "raw byte count does not add the snapshot and the delta"); + } + std::size_t nonzero = 0; + for (std::size_t at = 0; at < memory.size(); at += FrameCaptureWriter::BlockBytes) { + const auto bytes = std::min(FrameCaptureWriter::BlockBytes, memory.size() - at); + if (!AgcDriver::AllZero(std::span(memory).subspan(at, bytes))) nonzero += bytes; + } + check(stored == nonzero, "the snapshot stored zero blocks or lost data blocks"); + + const FrameCaptureReader reader(directory); + check(!reader.Truncated(), "a finished capture reads as truncated"); + check(reader.Header() == "{\"magic\":\"aps5-frame-capture 1\"}\n", "header does not round-trip"); + check(reader.Calls().size() == calls.size(), "call count does not round-trip"); + for (std::size_t index = 0; index < calls.size(); ++index) check(sameCall(reader.Calls()[index], calls[index]), "call " + std::to_string(index) + " does not round-trip"); + + std::vector restored(memory.size()); + std::size_t ranges = 0, snapshots = 0, deltas = 0; + for (const auto& chunk : reader.Chunks()) { + if (chunk.kind == FrameMemoryKind::Range) { + ++ranges; + check(chunk.address == Base && chunk.bytes == memory.size() && chunk.rangeKind == FrameRangeKind::Watched && chunk.protection == 3 && + chunk.access == (AgcDriver::FrameRangeAccess::Readable | AgcDriver::FrameRangeAccess::Gpu), "range does not round-trip"); + continue; + } + if (chunk.kind == FrameMemoryKind::Snapshot) { + ++snapshots; + check(chunk.bytes <= FrameCaptureWriter::ChunkBytes, "snapshot chunk is larger than the chunk limit"); + check(chunk.address >= Base && chunk.address - Base + chunk.bytes <= restored.size(), "snapshot chunk outside its range"); + reader.Read(chunk, std::span(restored).subspan(chunk.address - Base, chunk.bytes)); + continue; + } + ++deltas; + check(chunk.group == 1 && chunk.completed == 17 && chunk.address == Base + 0x3000, "delta header does not round-trip"); + check(reader.Read(chunk) == delta, "delta bytes do not round-trip"); + } + check(ranges == 1 && deltas == 1 && snapshots >= 2, "unexpected chunk counts"); + check(restored == memory, "snapshot does not restore the memory"); + std::vector wrong(1); + checkThrows([&] { reader.Read(reader.Chunks()[1], wrong); }, "a read into a wrongly sized destination does not throw"); +} + +void testTruncated(const std::filesystem::path& source, const std::filesystem::path& directory) { + std::filesystem::remove_all(directory); + std::filesystem::copy(source, directory, std::filesystem::copy_options::recursive); + truncateFile(directory / FrameCaptureWriter::CallsName, 3); + truncateFile(directory / FrameCaptureWriter::MemoryName, 10); + std::filesystem::remove(directory / FrameCaptureWriter::HeaderName); + const FrameCaptureReader reader(directory); + const auto calls = sampleCalls(); + check(reader.Truncated(), "a cut capture does not read as truncated"); + check(reader.Calls().size() == calls.size() - 1, "a cut call record is not dropped"); + check(reader.Chunks().back().kind != FrameMemoryKind::Delta, "a cut memory chunk is not dropped"); +} + +void testCorrupt(const std::filesystem::path& source, const std::filesystem::path& directory) { + std::filesystem::remove_all(directory); + std::filesystem::copy(source, directory, std::filesystem::copy_options::recursive); + { + std::fstream file(directory / FrameCaptureWriter::CallsName, std::ios::binary | std::ios::in | std::ios::out); + file.seekp(static_cast(FrameCaptureWriter::CallsMagic.size())); + const std::uint32_t kind = 99; + file.write(reinterpret_cast(&kind), sizeof(kind)); + } + checkThrows([&] { FrameCaptureReader reader(directory); }, "an unknown call kind does not throw"); + std::ofstream(directory / FrameCaptureWriter::CallsName, std::ios::binary | std::ios::trunc) << "not a capture"; + checkThrows([&] { FrameCaptureReader reader(directory); }, "a file without the magic does not throw"); +} + +void testWriter(const std::filesystem::path& directory) { + FrameCaptureWriter writer(directory); + writer.Finish("{}"); + FrameCall call; + call.kind = FrameCallKind::Suspend; + checkThrows([&] { writer.WriteCall(call); }, "a call after Finish does not throw"); + checkThrows([&] { writer.Finish("{}"); }, "a second Finish does not throw"); + FrameCall unknown; + unknown.kind = static_cast(77); + FrameCaptureWriter other(directory / "other"); + checkThrows([&] { other.WriteCall(unknown); }, "an unknown call kind is written"); +} + +void testAllZero() { + std::vector bytes(37); + check(AgcDriver::AllZero(bytes), "zero bytes are not all zero"); + check(AgcDriver::AllZero({}), "an empty span is not all zero"); + bytes[36] = std::byte{1}; + check(!AgcDriver::AllZero(bytes), "a nonzero tail byte is missed"); + bytes[36] = std::byte{0}; + bytes[3] = std::byte{1}; + check(!AgcDriver::AllZero(bytes), "a nonzero word is missed"); +} + +} + +int main() { + const auto root = std::filesystem::temp_directory_path() / ("aps5-frame-capture-test-" + std::to_string(getpid())); + std::filesystem::remove_all(root); + testAllZero(); + testRoundTrip(root / "capture"); + testTruncated(root / "capture", root / "truncated"); + testCorrupt(root / "capture", root / "corrupt"); + testWriter(root / "writer"); + std::filesystem::remove_all(root); + std::puts("frame capture tests passed"); + return 0; +} diff --git a/core/libs/prx/libSceAgcDriver/tests/FrameCaptureEop.cpp b/core/libs/prx/libSceAgcDriver/tests/FrameCaptureEop.cpp new file mode 100644 index 0000000000..88d8cb144e --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/tests/FrameCaptureEop.cpp @@ -0,0 +1,143 @@ +#include "prx/libSceAgcDriver/Execution/include/Driver.hpp" +#include "prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp" +#include "prx/libSceAgcDriver/Execution/include/FrameCapture.hpp" +#include "prx/libSceAgcDriver/Execution/include/VideoOutput.hpp" +#include "prx/libSceAgcDriver/Submit/include/Dcb.hpp" +#include "prx/libc/include/GuestAllocations.hpp" +#include "prx/libc/include/GuestArena.hpp" +#include "SceTypes.hpp" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#ifdef _WIN32 +#include +#endif + +namespace { + +using AgcDriver::FrameCallKind; +using AgcDriver::FrameCaptureReader; + +constexpr std::uint32_t Handle = 7; +constexpr std::size_t BlockBytes = 1u << 20; +constexpr std::size_t Frames = 6; +constexpr std::size_t CapturedFrames = 3; + +void check(bool condition, const std::string& reason) { + if (!condition) throw std::runtime_error(reason); +} + +class Request final : public AgcDriver::IFlipRequest { +public: + explicit Request(std::atomic& ready) : ready(ready) {} + void GpuReady(const std::shared_ptr&) override { ++ready; } + void Fail(std::exception_ptr error) noexcept override { + if (!error) std::terminate(); + } + +private: + std::atomic& ready; +}; + +class Output final : public AgcDriver::IVideoOutput { +public: + std::shared_ptr Reserve(const AgcDriver::FlipInfo&) override { return std::make_shared(ready); } + void Fail(std::exception_ptr error) noexcept override { + if (!error) std::terminate(); + } + std::atomic ready = 0; +}; + +std::byte* allocate() { + auto* const block = static_cast(GuestArena::GuestArenaAllocate_nid_postfix(BlockBytes, 65536)); + check(block != nullptr, "cannot allocate guest memory"); + GuestArena::GuestArenaCommit_nid_postfix(block, BlockBytes, 0x04, BlockBytes); + GuestAllocations::Mutation mutation; + mutation.Add(block, BlockBytes, true, true, true); + return block; +} + +void submitEopFrame(std::byte* block, std::size_t frame) { + const auto token = AgcDriverReserveEopFlip_nid_postfix(AgcDriver::FlipInfo{Handle, 0, 1, static_cast(frame)}); + check(token != 0, "the EOP flip of frame " + std::to_string(frame) + " was not reserved"); + auto* const words = reinterpret_cast(block + frame * 256); + const auto label = reinterpret_cast(block + BlockBytes / 2 + frame * 8); + const std::array release{0xc0064900u, 0x06200504u, 0x42010000u, static_cast(label), static_cast(label >> 32u), 1u, 0u, token}; + std::memcpy(words, release.data(), sizeof(release)); + Packet packet{words, static_cast(release.size()), 0, {}}; + check(sceAgcDriverSubmitDcb(&packet) == 0, "the EOP submission of frame " + std::to_string(frame) + " failed"); +} + +void testEopFramesCountOnce() { + const char* directory = std::getenv("APS5_FRAME_CAPTURE"); + check(directory != nullptr && *directory != '\0', "APS5_FRAME_CAPTURE is not set"); + auto* const block = allocate(); + auto output = std::make_shared(); + AgcDriverRegisterVideoOutput_nid_postfix(Handle, output); + for (std::size_t frame = 0; frame < Frames; ++frame) submitEopFrame(block, frame); + AgcDriverWaitIdle_nid_postfix(); + check(output->ready.load() == static_cast(Frames), "the driver presented " + std::to_string(output->ready.load()) + " of the EOP flips"); + + const FrameCaptureReader reader{std::filesystem::path(directory)}; + check(!reader.Truncated(), "the capture is truncated"); + check(reader.Header().find("\"frames\":" + std::to_string(CapturedFrames)) != std::string::npos, "the capture header does not record " + std::to_string(CapturedFrames) + " frames: " + reader.Header()); + const auto& calls = reader.Calls(); + const auto start = std::find_if(calls.begin(), calls.end(), [](const auto& call) { return call.kind == FrameCallKind::FrameStart; }); + check(start != calls.end(), "the capture has no frame start"); + std::size_t submits = 0, reservations = 0; + FrameCallKind first = FrameCallKind::End; + for (auto call = start + 1; call != calls.end(); ++call) { + if (call->kind == FrameCallKind::Submit || call->kind == FrameCallKind::ReserveEopFlip) { + if (first == FrameCallKind::End) first = call->kind; + submits += call->kind == FrameCallKind::Submit; + reservations += call->kind == FrameCallKind::ReserveEopFlip; + } + } + check(first == FrameCallKind::ReserveEopFlip, "the capture started between a reservation and the submission that flips it"); + check(submits == CapturedFrames && reservations == CapturedFrames, + "the captured frames hold " + std::to_string(submits) + " submissions and " + std::to_string(reservations) + " reservations, expected " + std::to_string(CapturedFrames) + " of each"); +} + +void testFlipsCountPerThread() { + auto* const block = allocate(); + const auto& driver = AgcDriver::DriverDetail::Driver::Get(); + const auto before = driver.FlipSubmissions(); + std::uint64_t otherBefore = 0, otherAfter = 0; + std::exception_ptr failure; + std::thread submitter([&] { + try { + otherBefore = driver.FlipSubmissions(); + submitEopFrame(block, 0); + otherAfter = driver.FlipSubmissions(); + } catch (...) { + failure = std::current_exception(); + } + }); + submitter.join(); + if (failure) std::rethrow_exception(failure); + AgcDriverWaitIdle_nid_postfix(); + check(otherAfter == otherBefore + 1, "the flipping submission did not count on the thread that made it"); + check(driver.FlipSubmissions() == before, "a flip submitted on another thread changed this thread's flip count, so a submission of this thread that overlaps it would end a captured frame"); +} + +} + +int main() { + try { + testEopFramesCountOnce(); + testFlipsCountPerThread(); + } catch (const std::exception& error) { + std::fprintf(stderr, "FAILED: %s\n", error.what()); + return 1; + } + std::puts("frame capture EOP tests passed"); + return 0; +} diff --git a/core/libs/prx/libSceAgcDriver/tests/FrameDumpIo.cpp b/core/libs/prx/libSceAgcDriver/tests/FrameDumpIo.cpp index ec52e5f1d9..f6b12a62e6 100644 --- a/core/libs/prx/libSceAgcDriver/tests/FrameDumpIo.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/FrameDumpIo.cpp @@ -120,13 +120,15 @@ void DisableAfterFailure() { AgcDriver::FrameDumpOutput output; const std::array pixels{}; Check(output.Enabled_nid_no_patch(), "BMP output was disabled before any failure"); - const auto blocked = directory.string(); + const auto previous = std::filesystem::current_path(); + std::filesystem::current_path(directory.parent_path()); + const auto blocked = directory.filename().string(); Check(!output.Write_nid_no_patch(blocked.c_str(), 2, 2, pixels), "BMP output accepted a directory as a file"); Check(!output.Enabled_nid_no_patch(), "failed BMP output remained enabled"); - const auto skipped = directory / "must-not-exist.bmp"; - const auto path = skipped.string(); + const auto path = (directory.filename() / "must-not-exist.bmp").string(); for (unsigned i = 0; i < 64; ++i) Check(!output.Write_nid_no_patch(path.c_str(), 2, 2, pixels), "disabled BMP output wrote another file"); - Check(!std::filesystem::exists(skipped), "disabled BMP output created a file"); + std::filesystem::current_path(previous); + Check(!std::filesystem::exists(directory / "must-not-exist.bmp"), "disabled BMP output created a file"); Check(std::filesystem::remove(directory), "cannot remove empty fixture directory"); } diff --git a/core/libs/prx/libSceAgcDriver/tests/FrameReplay.cpp b/core/libs/prx/libSceAgcDriver/tests/FrameReplay.cpp new file mode 100644 index 0000000000..56d6b1c40d --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/tests/FrameReplay.cpp @@ -0,0 +1,209 @@ +#include "prx/libSceAgcDriver/Execution/include/Driver.hpp" +#include "prx/libSceAgcDriver/Execution/include/FrameCapture.hpp" +#include "prx/libSceAgcDriver/Execution/include/VideoOutput.hpp" +#include "prx/libSceAgcDriver/Submit/include/Dcb.hpp" +#include "prx/libc/include/GuestAllocations.hpp" +#include "prx/libc/include/GuestArena.hpp" +#include "SceTypes.hpp" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#ifdef _WIN32 +#include +#endif + +extern "C" { +std::uint32_t* APS5_VABI sceAgcDcbWriteData(CommandBuffer*, std::uint8_t, std::uint8_t, std::uint64_t, const void*, std::uint32_t, std::uint8_t, std::uint8_t); +std::uint32_t* APS5_VABI sceAgcDcbDmaData(CommandBuffer*, std::uint8_t, std::uint8_t, std::uint8_t, std::uint64_t, std::uint8_t, std::uint8_t, std::uint64_t, std::uint32_t, std::uint8_t, std::uint8_t, std::uint8_t); +} + +namespace { + +using AgcDriver::FrameCallKind; +using AgcDriver::FrameCaptureReader; +using AgcDriver::FrameMemoryKind; + +constexpr std::uint32_t Handle = 7; +constexpr std::uint8_t GraphicsMemory = 0x0a; +constexpr std::uint8_t DmaMemory = 0; +constexpr std::size_t BlockBytes = 1u << 20; +constexpr std::size_t CommandBytes = 64u * 1024u; +constexpr std::size_t DataBytes = 4096; +constexpr std::uint32_t Done = 1; + +void check(bool condition, const std::string& reason) { + if (!condition) throw std::runtime_error(reason); +} + +std::uint64_t at(const volatile void* pointer) { + return static_cast(reinterpret_cast(pointer)); +} + +class Request final : public AgcDriver::IFlipRequest { +public: + explicit Request(std::atomic& ready) : ready(ready) {} + void GpuReady(const std::shared_ptr&) override { ++ready; } + void Fail(std::exception_ptr error) noexcept override { + if (!error) std::terminate(); + } + +private: + std::atomic& ready; +}; + +class Output final : public AgcDriver::IVideoOutput { +public: + std::shared_ptr Reserve(const AgcDriver::FlipInfo&) override { return std::make_shared(ready); } + void Fail(std::exception_ptr error) noexcept override { + if (!error) std::terminate(); + } + std::atomic ready = 0; +}; + +struct Layout { + std::byte* base = nullptr; + std::uint32_t* firstCommands = nullptr; + std::uint32_t* secondCommands = nullptr; + std::byte* source = nullptr; + std::byte* firstTarget = nullptr; + std::byte* secondTarget = nullptr; + std::uint32_t* label = nullptr; +}; + +Layout allocate() { + auto* const block = static_cast(GuestArena::GuestArenaAllocate_nid_postfix(BlockBytes, 65536)); + check(block != nullptr, "cannot allocate guest memory"); + GuestArena::GuestArenaCommit_nid_postfix(block, BlockBytes, 0x04, BlockBytes); + { + GuestAllocations::Mutation mutation; + mutation.Add(block, BlockBytes, true, true, true); + } + Layout layout; + layout.base = block; + layout.firstCommands = reinterpret_cast(block); + layout.secondCommands = reinterpret_cast(block + CommandBytes); + layout.source = block + 2 * CommandBytes; + layout.firstTarget = layout.source + DataBytes; + layout.secondTarget = layout.firstTarget + DataBytes; + layout.label = reinterpret_cast(layout.secondTarget + DataBytes); + return layout; +} + +std::vector pattern(std::uint8_t seed) { + std::vector bytes(DataBytes); + for (std::size_t index = 0; index < bytes.size(); ++index) bytes[index] = static_cast((index * 31 + seed) & 0xff); + return bytes; +} + +void copyFrame(std::uint32_t* words, const Layout& layout, std::byte* target) { + CommandBuffer buffer{words, words + CommandBytes / sizeof(std::uint32_t), words, words + CommandBytes / sizeof(std::uint32_t), nullptr, nullptr, 0}; + sceAgcDcbDmaData(&buffer, 0, DmaMemory, 0, at(target), DmaMemory, 0, at(layout.source), DataBytes, 0, 1, 0); + sceAgcDcbWriteData(&buffer, GraphicsMemory, 0, at(layout.label), &Done, 1, 0, 1); + Packet packet{words, static_cast(buffer.cursor_up - words), 0, {}}; + check(sceAgcDriverSubmitDcb(&packet) == 0, "submission failed"); +} + +void flip() { + AgcDriverSubmitFlip_nid_postfix(AgcDriver::FlipInfo{Handle, -1, 1, 0}); +} + +std::vector readFile(const std::filesystem::path& path) { + std::ifstream file(path, std::ios::binary); + check(file.is_open(), "cannot open " + path.string()); + std::vector bytes((std::istreambuf_iterator(file)), std::istreambuf_iterator()); + std::vector result(bytes.size()); + std::memcpy(result.data(), bytes.data(), bytes.size()); + return result; +} + +bool equal(const std::byte* bytes, const std::vector& expected) { + return std::memcmp(bytes, expected.data(), expected.size()) == 0; +} + +void testCaptureAndReplay(const std::filesystem::path& tool) { + const char* directory = std::getenv("APS5_FRAME_CAPTURE"); + check(directory != nullptr && *directory != '\0', "APS5_FRAME_CAPTURE is not set"); + const std::filesystem::path capture(directory); + const auto layout = allocate(); + const auto first = pattern(3); + const auto second = pattern(101); + auto output = std::make_shared(); + AgcDriverRegisterVideoOutput_nid_postfix(Handle, output); + AgcDriverUnregisterVideoOutput_nid_postfix(Handle, output); + output = std::make_shared(); + AgcDriverRegisterVideoOutput_nid_postfix(Handle, output); + + flip(); + std::memcpy(layout.source, first.data(), first.size()); + copyFrame(layout.firstCommands, layout, layout.firstTarget); + flip(); + AgcDriverWaitIdle_nid_postfix(); + AgcDriverUnregisterVideoOutput_nid_postfix(Handle, output); + output = std::make_shared(); + AgcDriverRegisterVideoOutput_nid_postfix(Handle, output); + const auto late = allocate(); + std::memcpy(late.source, second.data(), second.size()); + copyFrame(late.firstCommands, late, late.firstTarget); + flip(); + AgcDriverWaitIdle_nid_postfix(); + check(equal(layout.firstTarget, first) && equal(late.firstTarget, second), "the driver did not run the copies while capturing"); + + const FrameCaptureReader reader(capture); + check(!reader.Truncated(), "the capture is truncated"); + check(reader.Header().find("\"frames\":2") != std::string::npos, "the capture header does not record two frames: " + reader.Header()); + std::size_t submits = 0, flips = 0, memory = 0, starts = 0, registrations = 0, unregistrations = 0; + for (const auto& call : reader.Calls()) { + submits += call.kind == FrameCallKind::Submit; + flips += call.kind == FrameCallKind::Flip; + memory += call.kind == FrameCallKind::Memory; + starts += call.kind == FrameCallKind::FrameStart; + registrations += call.kind == FrameCallKind::RegisterVideoOutput; + unregistrations += call.kind == FrameCallKind::UnregisterVideoOutput; + } + check(starts == 1 && submits == 2 && flips == 2, "the capture holds " + std::to_string(submits) + " submits and " + std::to_string(flips) + " flips after " + std::to_string(starts) + " frame starts"); + check(memory >= 2, "the CPU writes before each copy were not recorded as memory deltas"); + check(registrations == 3 && unregistrations == 2, "video output close and reopen was not recorded before and during capture"); + const auto recorded = [&](const std::byte* base) { + return std::any_of(reader.Chunks().begin(), reader.Chunks().end(), [&](const auto& chunk) { + return chunk.kind == FrameMemoryKind::Range && chunk.address <= at(base) && at(base) + BlockBytes <= chunk.address + chunk.bytes; + }); + }; + check(recorded(layout.base), "the guest block is not in the snapshot ranges"); + check(recorded(late.base), "the block committed during the capture is not in its ranges"); + check(reader.Header().find("\"late_ranges\":1") != std::string::npos, "the capture header does not count one range committed during the capture: " + reader.Header()); + + const auto firstDump = capture / "first.bin"; + const auto secondDump = capture / "second.bin"; + const auto command = "\"\"" + tool.string() + "\" \"" + capture.string() + "\" --strict --dump " + std::to_string(at(layout.firstTarget)) + ":" + std::to_string(DataBytes) + ":\"" + firstDump.string() + + "\" --dump " + std::to_string(at(late.firstTarget)) + ":" + std::to_string(DataBytes) + ":\"" + secondDump.string() + "\"\""; + const auto code = std::system(command.c_str()); + check(code == 0, "agc_frame_replay exited with " + std::to_string(code)); + check(readFile(firstDump) == first, "the replay did not reproduce the first copy"); + check(readFile(secondDump) == second, "the replay did not reproduce the second copy"); +} + +} + +int main(int argc, char** argv) { + if (argc != 2) { + std::fprintf(stderr, "usage: agc_driver_frame_replay_tests \n"); + return 2; + } + try { + testCaptureAndReplay(argv[1]); + } catch (const std::exception& error) { + std::fprintf(stderr, "FAILED: %s\n", error.what()); + return 1; + } + std::puts("frame replay tests passed"); + return 0; +} diff --git a/core/libs/prx/libSceAgcDriver/tests/FrameReplayState.cpp b/core/libs/prx/libSceAgcDriver/tests/FrameReplayState.cpp new file mode 100644 index 0000000000..c54b01466a --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/tests/FrameReplayState.cpp @@ -0,0 +1,241 @@ +#define main frameReplayToolMain +#include "../tools/FrameReplay.cpp" +#undef main +#include "prx/libSceAgcDriver/Execution/include/FrameRecorder.hpp" +#include + +namespace { + +using AgcDriver::FrameCaptureWriter; + +void check(bool condition, const char* reason) { + if (!condition) throw std::runtime_error(reason); +} + +std::uint64_t address(const void* pointer) { + return reinterpret_cast(pointer); +} + +std::byte* allocate(std::size_t size, bool gpu = true) { + auto* bytes = static_cast(GuestArena::GuestArenaAllocate_nid_postfix(size, 65536)); + check(bytes != nullptr, "guest allocation failed"); + GuestArena::GuestArenaCommit_nid_postfix(bytes, size, 0x04, size); + GuestAllocations::Mutation mutation; + mutation.Add(bytes, size, true, true, gpu); + return bytes; +} + +void collectGroups(Replay& replay) { + for (const auto& chunk : replay.reader.Chunks()) { + if (chunk.kind == FrameMemoryKind::Delta) replay.groups[chunk.group].push_back(&chunk); + } +} + +FrameCall memoryCall(std::uint64_t group, std::uint64_t thread) { + FrameCall call; + call.kind = FrameCallKind::Memory; + call.values = {group}; + call.thread = thread; + return call; +} + +void testMemoryOrder(const std::filesystem::path& directory) { + constexpr std::size_t Size = 64u * 1024u * 1024u; + auto* bytes = allocate(Size); + const std::vector first(Size, std::byte{0x11}); + const std::vector second(4096, std::byte{0x22}); + FrameCaptureWriter writer(directory); + writer.WriteDelta(1, 0, address(bytes), first); + writer.WriteDelta(2, 0, address(bytes + Size - second.size()), second); + writer.Finish("{}"); + const FrameCaptureReader reader(directory); + Options options; + options.release = false; + Replay replay{options, reader}; + replay.recording = true; + collectGroups(replay); + const std::array calls{memoryCall(1, 1), memoryCall(2, 2)}; + replay.runWindow(calls); + check(std::all_of(bytes, bytes + Size - second.size(), [](std::byte value) { return value == std::byte{0x11}; }), "first delta was not restored"); + check(std::equal(second.begin(), second.end(), bytes + Size - second.size()), "an earlier delta overwrote the later thread's delta"); +} + +void testReusedMemory(const std::filesystem::path& directory) { +#ifdef _WIN32 + constexpr std::size_t Size = 2 * FrameCaptureWriter::BlockBytes; + auto* bytes = allocate(Size); + auto* cpu = allocate(FrameCaptureWriter::BlockBytes, false); + std::fill_n(bytes, Size, std::byte{0x7f}); + std::fill_n(cpu, FrameCaptureWriter::BlockBytes, std::byte{0x55}); + std::vector expected(Size); + std::fill(expected.begin() + FrameCaptureWriter::BlockBytes, expected.end(), std::byte{0x22}); + std::uintptr_t base = 0; + std::size_t arenaSize = 0; + GuestArena::GuestArenaRange_nid_postfix(&base, &arenaSize); + FrameCaptureWriter writer(directory); + writer.WriteRange(address(bytes), Size, FrameRangeKind::Watched, PAGE_READWRITE, FrameRangeAccess::Readable | FrameRangeAccess::Writable | FrameRangeAccess::Gpu); + writer.WriteRange(address(cpu), FrameCaptureWriter::BlockBytes, FrameRangeKind::Unwatched, PAGE_READWRITE, FrameRangeAccess::Readable | FrameRangeAccess::Writable); + check(writer.WriteSnapshot(address(bytes), expected) == FrameCaptureWriter::BlockBytes, "zero snapshot block was not elided"); + writer.Finish("{\"arena_base\":" + std::to_string(base) + "}"); + const FrameCaptureReader reader(directory); + const auto skipped = restoreMemory(reader); + check(std::equal(expected.begin(), expected.end(), bytes), "reused GPU memory did not restore zero and nonzero snapshot blocks"); + check(skipped.bytes == FrameCaptureWriter::BlockBytes, "unexpected skipped memory"); + check(std::all_of(cpu, cpu + FrameCaptureWriter::BlockBytes, [](std::byte value) { return value == std::byte{0x55}; }), "reused CPU memory was overwritten"); +#endif +} + +void testEopTokens() { + Frames frames; + auto output = std::make_shared(frames); + AgcDriverRegisterVideoOutput_nid_postfix(7, output); + std::barrier together(2); + std::atomic wrong{0}; + const auto worker = [&](int lane) { + for (int round = 0; round < 2000; ++round) { + const auto requested = AgcDriver::EopFlipTokenBase + round * 8 + lane * 2; + together.arrive_and_wait(); + try { + if (AgcDriverReserveEopFlipAt_nid_postfix({7, -1, 1, 0}, requested) != requested) ++wrong; + } catch (const std::exception&) { ++wrong; } + } + }; + std::thread first(worker, 0), second(worker, 1); + first.join(); + second.join(); + check(wrong.load() == 0, "concurrent explicit EOP reservations returned the wrong token"); + const auto high = AgcDriver::EopFlipTokenBase + 20000; + check(AgcDriverReserveEopFlipAt_nid_postfix({7, -1, 1, 0}, high) == high, "explicit high token was not reserved"); + check(AgcDriverReserveEopFlipAt_nid_postfix({7, -1, 1, 0}, high - 1) == high - 1, "explicit lower token was not reserved"); + check(AgcDriverReserveEopFlip_nid_postfix({7, -1, 1, 0}) == high + 1, "ordinary reservation reused an explicit token"); + bool rejected = false; + try { AgcDriverReserveEopFlipAt_nid_postfix({7, -1, 1, 0}, high); } + catch (const std::runtime_error&) { rejected = true; } + check(rejected, "duplicate explicit token was not rejected"); + AgcDriverUnregisterVideoOutput_nid_postfix(7, output); +} + +class WaitingOutput final : public AgcDriver::IVideoOutput { +public: + WaitingOutput(Frames& frames, const std::uint32_t* label) : frames(frames), label(label) {} + std::shared_ptr Reserve(const AgcDriver::FlipInfo&) override { + const auto deadline = Clock::now() + std::chrono::seconds(2); + while (*static_cast(label) == 0) { + check(Clock::now() < deadline, "a blocking driver call prevented the later memory update"); + std::this_thread::yield(); + } + return std::make_shared(frames); + } + void Fail(std::exception_ptr error) noexcept override { frames.Failed(error); } + +private: + Frames& frames; + const std::uint32_t* label; +}; + +void testBlockingSubmission(const std::filesystem::path& directory) { + auto* bytes = allocate(FrameCaptureWriter::BlockBytes); + auto* label = reinterpret_cast(bytes); + const std::uint32_t ready = 1; + FrameCaptureWriter writer(directory); + writer.WriteDelta(1, 0, address(label), std::as_bytes(std::span(&ready, 1))); + writer.Finish("{}"); + const FrameCaptureReader reader(directory); + Options options; + options.pace = false; + options.release = false; + Replay replay{options, reader}; + collectGroups(replay); + auto output = std::make_shared(replay.frames, label); + AgcDriverRegisterVideoOutput_nid_postfix(7, output); + std::array words{AgcDriver::FlipPacketHeader, 7, 0xffffffffu, 1, 0, 0}; + for (const auto kind : {FrameCallKind::Submit, FrameCallKind::Flip, FrameCallKind::ReserveEopFlip}) { + *label = 0; + FrameCall submit; + submit.kind = kind; + submit.thread = 1; + submit.address = address(words.data()); + submit.size = words.size(); + submit.values = {7, static_cast(-1), 1, 0, AgcDriver::EopFlipTokenBase}; + const std::array calls{submit, memoryCall(1, 2)}; + replay.runWindow(calls); + check(*label == ready && replay.unfinished == 0, "blocking submission did not complete"); + } + check(replay.frames.Times().size() == 2, "blocking submissions lost a flip"); + AgcDriverUnregisterVideoOutput_nid_postfix(7, output); +} + +void testShaderCode(const std::filesystem::path& directory, bool host) { + auto* bytes = allocate(1u << 20); + auto* shader = reinterpret_cast(bytes); + auto* code = bytes + (1u << 19) + 256; + constexpr std::size_t CodeBytes = 3 * FrameCaptureWriter::BlockBytes; +#ifdef _WIN32 + if (host) { + auto* memory = static_cast(VirtualAlloc(nullptr, CodeBytes + 4096, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)); + check(memory != nullptr, "host shader allocation failed"); + GuestAllocations::Mutation mutation; + mutation.Add(memory, CodeBytes + 4096, true, true, true); + code = memory + 256; + } +#endif + *shader = {}; + shader->file_header = 0x34333231u; + shader->version = 0x18u; + shader->header_size = sizeof(Shader); + shader->shader_size = CodeBytes; + shader->code = code; + auto* recorder = AgcDriver::FrameRecorder::Active(); + check(recorder != nullptr, "frame recorder is disabled"); + recorder->FrameBoundary(); + std::vector expected(CodeBytes); + for (std::size_t i = 0; i < expected.size(); ++i) expected[i] = static_cast(i * 31 + 17); + std::memcpy(code, expected.data(), expected.size()); + recorder->RegisterShader(shader); + recorder->FrameBoundary(); + const FrameCaptureReader reader(directory); + std::vector captured(CodeBytes); + const auto apply = [&](const FrameMemoryChunk& chunk) { + const auto begin = std::max(address(code), chunk.address); + const auto end = std::min(address(code) + CodeBytes, chunk.address + chunk.bytes); + if (begin >= end) return; + const auto data = reader.Read(chunk); + std::memcpy(captured.data() + begin - address(code), data.data() + begin - chunk.address, end - begin); + }; + for (const auto& chunk : reader.Chunks()) if (chunk.kind == FrameMemoryKind::Snapshot) apply(chunk); + bool registered = false; + for (const auto& call : reader.Calls()) { + if (call.kind == FrameCallKind::RegisterShader) { + registered = true; + break; + } + if (call.kind != FrameCallKind::Memory) continue; + for (const auto& chunk : reader.Chunks()) { + if (chunk.kind == FrameMemoryKind::Delta && chunk.group == call.values.at(0)) apply(chunk); + } + } + check(registered && captured == expected, "the complete separate shader code was not captured before registration"); +} + +} + +int main(int argc, char** argv) { + try { + check(argc == 3, "expected a test name and capture directory"); + const std::string mode = argv[1]; + const std::filesystem::path directory = argv[2]; + if (mode == "order") testMemoryOrder(directory); + else if (mode == "zero") testReusedMemory(directory); + else if (mode == "tokens") testEopTokens(); + else if (mode == "blocking") testBlockingSubmission(directory); + else if (mode == "shader" || mode == "shader_host") testShaderCode(directory, mode == "shader_host"); + else throw std::runtime_error("unknown test"); + AgcDriverShutdown_nid_postfix(); + std::puts("frame replay state test passed"); + return 0; + } catch (const std::exception& error) { + std::fprintf(stderr, "%s\n", error.what()); + try { AgcDriverShutdown_nid_postfix(); } catch (...) {} + return 1; + } +} diff --git a/core/libs/prx/libSceAgcDriver/tests/Graphics.cpp b/core/libs/prx/libSceAgcDriver/tests/Graphics.cpp index abdb669923..7afcaac27e 100644 --- a/core/libs/prx/libSceAgcDriver/tests/Graphics.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/Graphics.cpp @@ -10,7 +10,6 @@ #include "prx/libSceAgcDriver/Graphics/include/Draw.hpp" #include "prx/libSceAgcDriver/Graphics/include/BufferPool.hpp" #include "prx/libSceAgcDriver/Graphics/include/GuestBufferMemory.hpp" -#include "prx/libSceAgcDriver/Graphics/include/DepthSurface.hpp" #include "ShaderDiskCache.hpp" #include "ShaderCacheDirectory.hpp" #include "prx/libSceAgcDriver/Graphics/include/MultisampleTarget.hpp" @@ -824,7 +823,7 @@ void msaaEnableClearOneSampleTargetTests() { queue = drawC(); queue.context[0x1b3] |= ShaderRecompiler::PixelInputBit(ShaderRecompiler::PixelInput::Ancillary); requireOneSample(queue, "draw C with an enabled but unaddressed ANCILLARY input"); - constexpr std::string_view refused = "a one-sample color target under a multisampled rasterizer without MSAA_ENABLE is unsupported with PS_ITER_SAMPLE, a sample index or coverage input, or a sample-mask export"; + constexpr std::string_view refused = "a one-sample color target under a multisampled rasterizer without MSAA_ENABLE is unsupported with a sample index or coverage input, or a sample-mask export"; for (const auto input : {ShaderRecompiler::PixelInput::Ancillary, ShaderRecompiler::PixelInput::SampleCoverage}) { queue = drawC(); queue.context[0x1b3] = queue.context[0x1b4] = 2u | ShaderRecompiler::PixelInputBit(input); @@ -832,6 +831,10 @@ void msaaEnableClearOneSampleTargetTests() { } queue = drawC(); queue.context[0x293] = 0x10000u; + queue.context[0x1b3] = queue.context[0x1b4] = 2u | ShaderRecompiler::PixelInputBit(ShaderRecompiler::PixelInput::PerspectiveSample); + requireOneSample(queue, "draw C with PS_ITER_SAMPLE and a sample-interpolated input"); + Require(!DecodeState(queue).perSampleShading, "PS_ITER_SAMPLE into a one-sample color target without MSAA_ENABLE asked for sample shading"); + queue.context[0x1b3] = queue.context[0x1b4] = 2u | ShaderRecompiler::PixelInputBit(ShaderRecompiler::PixelInput::Ancillary); expectFailure([&] { DecodeState(queue); }, refused); queue = drawC(); queue.context[0x1c4] = 9; @@ -906,44 +909,25 @@ void msaaEnableClearOneSampleTargetTests() { queue.context[0x1b3] = queue.context[0x1b4] = 2u | ShaderRecompiler::PixelInputBit(ShaderRecompiler::PixelInput::Ancillary) | ShaderRecompiler::PixelInputBit(ShaderRecompiler::PixelInput::SampleCoverage); Require(AgcDriver::Graphics::PixelProgramSkipped(queue), "the depth-only draw did not skip its pixel program"); requireOneSampleDepth(queue, 0, "a one-sample depth target whose skipped pixel program has PS_ITER_SAMPLE and sample index and coverage inputs"); - constexpr std::string_view depthRefused = "a one-sample depth target under a multisampled rasterizer without MSAA_ENABLE is unsupported for points or lines and with alpha-to-coverage, a sample index or coverage input, a sample-mask export, or DB_EQAA source Z interpolation or overrasterization"; for (const bool color : {false, true}) { - for (const auto input : {ShaderRecompiler::PixelInput::Ancillary, ShaderRecompiler::PixelInput::SampleCoverage}) { - queue = depthTarget(color); - queue.context[0x203] |= 0x40u; - queue.context[0x1b3] = queue.context[0x1b4] = 2u | ShaderRecompiler::PixelInputBit(input); - expectFailure([&] { DecodeState(queue); }, depthRefused); - } + queue = depthTarget(color); + queue.context[0x203] |= 0x40u; + queue.context[0x1b3] = queue.context[0x1b4] = 2u | ShaderRecompiler::PixelInputBit(ShaderRecompiler::PixelInput::Ancillary); + Require(!AgcDriver::Graphics::PixelProgramSkipped(queue), "the ANCILLARY draw skipped its pixel program"); + requireOneSampleDepth(queue, color ? 1u : 0u, "a one-sample depth target whose running pixel program reads ANCILLARY without PS_ITER_SAMPLE"); + queue.context[0x1b3] = queue.context[0x1b4] = 0x2b01u; + requireOneSampleDepth(queue, color ? 1u : 0u, "a one-sample depth target whose running pixel program reads PERSP_SAMPLE, the position and ANCILLARY without PS_ITER_SAMPLE"); queue = depthTarget(color); queue.context[0x203] |= 0x40u; queue.context[0x293] = 0x10000u; Require(!AgcDriver::Graphics::PixelProgramSkipped(queue), "the PS_ITER_SAMPLE draw skipped its pixel program"); requireOneSampleDepth(queue, color ? 1u : 0u, "a one-sample depth target whose running pixel program has PS_ITER_SAMPLE and no sample index or coverage input"); Require(!DecodeState(queue).perSampleShading, "a PS_ITER_SAMPLE program drawn as one sample still shades per sample"); - queue.context[0x1b3] = queue.context[0x1b4] = 2u | ShaderRecompiler::PixelInputBit(ShaderRecompiler::PixelInput::Ancillary); - expectFailure([&] { DecodeState(queue); }, depthRefused); - queue = depthTarget(color); - queue.context[0x1c4] = 9; - queue.context[0x203] |= 0x100u; - expectFailure([&] { DecodeState(queue); }, depthRefused); - queue = depthTarget(color); - queue.context[0x2dc] = 0x0000aa01u; - expectFailure([&] { DecodeState(queue); }, depthRefused); - for (const auto primitive : {2u, 3u}) { - queue = depthTarget(color); - queue.userConfig[0x242] = primitive; - expectFailure([&] { DecodeState(queue); }, depthRefused); - } for (const auto eqaa : {0x00040000u, 0x00050000u}) { queue = depthTarget(color); queue.context[0x201] = eqaa; requireOneSampleDepth(queue, color ? 1u : 0u, "a one-sample depth target under a 4x PA_SC_AA_CONFIG without MSAA_ENABLE with DB_EQAA INTERPOLATE_COMP_Z"); } - for (const auto eqaa : {0x00090002u, 0x000d0000u, 0x01010002u, 0x08010002u}) { - queue = depthTarget(color); - queue.context[0x201] = eqaa; - expectFailure([&] { DecodeState(queue); }, depthRefused); - } } queue = depthTarget(true); queue.context[0x31d] = 0x12000; @@ -1941,8 +1925,35 @@ void TuningFieldTests() { Require(AgcDriver::Graphics::DrawRejection(queue, false).empty(), "DISABLE_DUAL_QUAD was rejected"); Require(state.hasColorTarget && state.blend.colorWriteMask == baseline.blend.colorWriteMask, "DISABLE_DUAL_QUAD changed color output"); queue.context[0x202] = 0xcc0013; - expectFailure([&] { AgcDriver::Graphics::DecodeState(queue); }, "copy ROP"); + expectFailure([&] { AgcDriver::Graphics::DecodeState(queue); }, "source or brush form"); + queue.context[0x202] = 0x5b0010; + expectFailure([&] { AgcDriver::Graphics::DecodeState(queue); }, "source or brush form"); + queue.context[0x202] = 0x5a0010; + state = AgcDriver::Graphics::DecodeState(queue); + Require(AgcDriver::Graphics::DrawRejection(queue, false).empty(), "an XOR ROP was rejected"); + Require(state.hasColorTarget && state.colorRop == 0x5a && AgcDriver::Graphics::LogicOpFromRop(state.colorRop) == VK_LOGIC_OP_XOR, "an XOR ROP did not decode"); queue.context[0x202] = 0xcc0010; + Require(AgcDriver::Graphics::DecodeState(queue).colorRop == AgcDriver::Graphics::CopyRop, "the copy ROP did not decode as the copy operation"); + const std::array, 14> ropOperations{{ + {0x0f, VK_LOGIC_OP_COPY_INVERTED}, {0xa0, VK_LOGIC_OP_AND}, {0xf0, VK_LOGIC_OP_COPY}, {0xfa, VK_LOGIC_OP_OR}, + {0x00, VK_LOGIC_OP_CLEAR}, {0x11, VK_LOGIC_OP_NOR}, {0x33, VK_LOGIC_OP_COPY_INVERTED}, {0x55, VK_LOGIC_OP_INVERT}, {0x66, VK_LOGIC_OP_XOR}, + {0x88, VK_LOGIC_OP_AND}, {0xaa, VK_LOGIC_OP_NO_OP}, {0xcc, VK_LOGIC_OP_COPY}, {0xee, VK_LOGIC_OP_OR}, {0xff, VK_LOGIC_OP_SET}}}; + for (const auto& [rop, operation] : ropOperations) Require(AgcDriver::Graphics::LogicOpFromRop(rop) == operation, "a ROP3 code mapped to the wrong logic operation"); + std::uint32_t documentedRops = 0; + for (std::uint32_t rop = 0; rop < 256u; ++rop) { + try { + static_cast(AgcDriver::Graphics::LogicOpFromRop(static_cast(rop))); + ++documentedRops; + } catch (const std::runtime_error&) { + } + } + Require(documentedRops == 28u, "a ROP3 code outside the source and brush forms was accepted, or one inside them was refused"); + auto blendedLogic = queue; + blendedLogic.context[0x202] = 0x5a0010; + blendedLogic.context[0x1e0] = 0x40000101u; + for (std::uint32_t index = 0; index < 4u; ++index) blendedLogic.context[0x105 + index] = 0u; + blendedLogic.context[0x31c] |= 0x8000u; + expectFailure([&] { AgcDriver::Graphics::DecodeState(blendedLogic); }, "logic operation together with blending"); for (const auto groups : {1u, 2u, 15u}) { queue.context[0x2d5] = 0x2000u | (groups << 15u); state = AgcDriver::Graphics::DecodeState(queue); @@ -2982,7 +2993,12 @@ void depthCopyPassTests() { queue.context[0x31c] = 0x40710; queue.context[0x8e] = 1; auto pass = DecodeDepthCopyPass(queue); - Require(pass.has_value() && pass->depthAddress == 0x100000 && pass->samples == 2 && pass->sample == 0 && pass->extent.width == 64 && pass->extent.height == 4, "a centroid DB depth copy of a two-sample depth plane decoded differently"); + Require(pass.has_value() && pass->source.address == 0x100000 && pass->source.samples == 2 && pass->sample == 0 && pass->source.extent.width == 64 && pass->source.extent.height == 4, "a centroid DB depth copy of a two-sample depth plane decoded differently"); + { + const auto words = AgcDriver::Graphics::DepthCopySourceWords(pass->source); + const auto resource = AgcDriver::Graphics::DecodeTextureResource(words); + Require(resource.baseAddress == pass->source.address && resource.dimension == AgcDriver::Graphics::TextureDimension::k2DMsaa && resource.samples == 2 && resource.width == 64 && resource.height == 4 && resource.tileMode == AgcDriver::Graphics::TextureTileMode::kZ64KBX && resource.pipeBankXor == pass->source.pipeBankXor && resource.dccAddress == pass->source.htileAddress, "the DB depth copy source descriptor of a two-sample plane decoded differently"); + } Require(pass->color.format == VK_FORMAT_R32_SFLOAT && pass->color.samples == 1 && pass->color.address == reinterpret_cast(colorMemory.data()), "the DB depth copy target decoded differently"); queue.context[0x000] = 0x104; Require(DecodeDepthCopyPass(queue)->sample == 1, "COPY_SAMPLE without COPY_CENTROID did not select the sample"); @@ -3007,23 +3023,31 @@ void depthCopyPassTests() { expectFailure([&] { (void)DecodeDepthCopyPass(queue); }, "single-sample R32 float"); queue.context[0x31c] = 0x40710; queue.context[0x10f] = std::bit_cast(16.0f); - expectFailure([&] { (void)DecodeDepthCopyPass(queue); }, "over part of the depth surface"); + Require(DecodeDepthCopyPass(queue).has_value(), "a DB depth copy whose viewport covers part of the depth surface was refused"); queue.context[0x10f] = std::bit_cast(32.0f); + queue.context[0x1e0] = 1u << 30u; + expectFailure([&] { (void)DecodeDepthCopyPass(queue); }, "blending"); + queue.context[0x1e0] = 0; queue.context[0x010] = 0x80001187; expectFailure([&] { (void)DecodeDepthCopyPass(queue); }, "mipmapped or partially resident"); queue.context[0x010] = 0x80000187; queue.context[0x012] = 0x00001003; pass = DecodeDepthCopyPass(queue); - Require(pass.has_value() && pass->depthAddress == 0x100000, "a DB depth copy kept the pipe-bank XOR of a two-sample depth plane in its address"); + Require(pass.has_value() && pass->source.address == 0x100000 && pass->source.pipeBankXor == 0x300, "a DB depth copy kept the pipe-bank XOR of a two-sample depth plane in its address"); queue.context[0x012] = 0x00001000; queue.context[0x002] = 1u | (1u << 13u); pass = DecodeDepthCopyPass(queue); - Require(pass.has_value() && pass->depthAddress == 0x100000 + AgcDriver::Graphics::DepthSliceBytes({64, 4}, 4u), "a DB depth copy of slice 1 did not address the slice the depth surface registers"); + Require(pass.has_value() && pass->source.address == 0x100000 + AgcDriver::Graphics::DepthSliceBytes({64, 4}, 4u), "a DB depth copy of slice 1 did not address the slice the depth surface registers"); queue.context[0x002] = 0; Require(DecodeDepthCopyPass(queue).has_value(), "the restored DB depth copy was refused"); queue.context[0x010] = 0x80000183; pass = DecodeDepthCopyPass(queue); - Require(pass.has_value() && pass->samples == 1 && pass->sample == 0, "a centroid DB depth copy of a one-sample depth plane decoded differently"); + Require(pass.has_value() && pass->source.samples == 1 && pass->sample == 0, "a centroid DB depth copy of a one-sample depth plane decoded differently"); + { + const auto words = AgcDriver::Graphics::DepthCopySourceWords(pass->source); + const auto resource = AgcDriver::Graphics::DecodeTextureResource(words); + Require(resource.baseAddress == pass->source.address && resource.dimension == AgcDriver::Graphics::TextureDimension::k2D && resource.samples == 1 && resource.width == 64 && resource.height == 4 && resource.dccAddress == 0, "the DB depth copy source descriptor of a one-sample plane decoded differently"); + } queue.context[0x000] = 0x104; expectFailure([&] { (void)DecodeDepthCopyPass(queue); }, "a sample the depth plane does not have"); } @@ -3847,7 +3871,7 @@ void multisampleTests() { } queue = multisampled(); queue.context[0x31d] = 0x1000; - expectFailure([&] { DecodeState(queue); }, "fewer fragments than samples"); + expectFailure([&] { DecodeState(queue); }, "a color target's sample count differs from the rasterizer's"); queue.context[0x31d] = 0; queue.context[0x293] = 0x10000u; Require(DecodeState(queue).samples == 1 && !DecodeState(queue).perSampleShading, "PS_ITER_SAMPLE into a one-sample target did not rasterize sample 0 alone"); @@ -4343,6 +4367,53 @@ void multisampledDepthTests() { queue = truncatedTwoSample(); queue.context[0x31d] = 0x12000; expectFailure([&] { DecodeState(queue); }, "a color target's sample count differs from the rasterizer's"); + const auto oneFragment = [&] { + auto queue = singleSampleScan(); + queue.context[0x2f8] = 0x00210002; + queue.context[0x292] = 3; + for (std::uint32_t pixel = 0; pixel < 4; ++pixel) queue.context[0x2fe + pixel * 4u] = 0x622ae6ae; + queue.context[0x31d] = 0x2000; + queue.context[0x201] = 0x112202; + return queue; + }; + queue = oneFragment(); + state = DecodeState(queue); + Require(state.samples == 4 && !state.customSampleLocations && state.depth && state.depth->samples == 4 && state.colors.size() == 1 && state.colors[0].samples == 1 && state.truncatedCoverage && !state.mergedCoverage && !state.perSampleShading, "a 4-sample color target with one fragment beside a 4x depth target (PPSA02015's registers) did not keep sample 0's color in its one fragment"); + Require(AgcDriver::Graphics::DrawRejection(queue, false).empty(), "the precheck refused a 4-sample color target with one fragment beside a 4x depth target"); + queue = oneFragment(); + queue.context[0x31c] |= 0x4000u; + queue.context[0x321] = 0x00c3a1b2u; + queue.context[0x3a0] = 0x01u; + state = DecodeState(queue); + Require(state.colors.size() == 1 && state.colors[0].fmaskAddress == ((1ull << 40u) | 0xc3a10000ull), "a compressed one-fragment color target did not decode its FMASK address"); + queue.context[0x321] = 0u; + queue.context[0x3a0] = 0u; + expectFailure([&] { DecodeState(queue); }, "a compressed one-fragment color target without an FMASK address is unsupported"); + queue.context[0x321] = 0xa5u; + expectFailure([&] { DecodeState(queue); }, "a compressed one-fragment color target without an FMASK address is unsupported"); + queue = oneFragment(); + Require(DecodeState(queue).colors[0].fmaskAddress == 0u, "an uncompressed one-fragment color target noted an FMASK address"); + queue = oneFragment(); + queue.context[0x31d] = 0xa000; + expectFailure([&] { DecodeState(queue); }, "color targets with more than one but fewer fragments than samples (EQAA) are unsupported"); + queue = oneFragment(); + queue.context[0x31d] = 0x8000; + expectFailure([&] { DecodeState(queue); }, "color targets with more fragments than samples are unsupported"); + queue = oneFragment(); + queue.context[0x201] = 0x012202; + expectFailure([&] { DecodeState(queue); }, "a one-fragment color target is supported only with static DB_EQAA anchors at the depth target's sample count"); + queue = oneFragment(); + queue.context[0x201] = 0x112201; + expectFailure([&] { DecodeState(queue); }, "a one-fragment color target is supported only with static DB_EQAA anchors at the depth target's sample count"); + queue = oneFragment(); + queue.context[0x2f8] = 0x00100001; + queue.context[0x010] = 0x22900987; + for (std::uint32_t pixel = 0; pixel < 4; ++pixel) queue.context[0x2fe + pixel * 4u] = 0xcc44; + expectFailure([&] { DecodeState(queue); }, "a one-fragment color target's sample count differs from the rasterizer's"); + queue = oneFragment(); + queue.context[0x010] = 0; + queue.context[0x011] = 0; + expectFailure([&] { DecodeState(queue); }, "a color target's sample count differs from the rasterizer's"); queue = singleSampleScan(); queue.context[0x010] = 0x22900983; expectFailure([&] { DecodeState(queue); }, "a color target's sample count differs from the rasterizer's"); @@ -6919,8 +6990,37 @@ void sampledTextureBudgetTests() { Require(SampledTextureBudget(memory, &reported, 5 * GiB) == 5 * GiB + 128 * MiB, "a 15 GiB budget with 4 GiB used outside the texture caches does not leave 5 GiB 128 MiB after the 4 GiB storage cache and 1 GiB 896 MiB of headroom"); reported.heapUsage[2] = 3 * GiB; Require(SampledTextureBudget(memory, &reported, 5 * GiB) == 9 * GiB + 128 * MiB, "usage below the cached bytes is not read as no other device memory in use"); + reported.heapUsage[2] = 9 * GiB; + Require(SampledTextureBudget(memory, &reported, 5 * GiB, 4 * GiB + 512 * MiB) == 4 * GiB + 640 * MiB, "a storage image cache larger than TextureCacheBudget was not reserved in full"); + Require(SampledTextureBudget(memory, &reported, 5 * GiB, 1 * GiB) == 5 * GiB + 128 * MiB, "a storage image cache smaller than TextureCacheBudget did not keep the TextureCacheBudget reservation"); reported.heapUsage[2] = 12 * GiB; Require(SampledTextureBudget(memory, &reported, 1 * GiB) == 2 * GiB, "a heap whose other users leave no room does not keep the 2 GiB floor"); + { + using AgcDriver::Graphics::StorageTextureBudget; + auto small = memory; + small.memoryHeaps[2].size = 8 * GiB; + VkPhysicalDeviceMemoryBudgetPropertiesEXT example{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MEMORY_BUDGET_PROPERTIES_EXT}; + example.heapBudget[2] = 7373 * MiB; + example.heapUsage[2] = 2 * GiB + 3686 * MiB; + Require(SampledTextureBudget(small, &example, 3686 * MiB, 3686 * MiB) == 2 * GiB, "3686 MiB of storage images on an 8 GiB heap with a 7373 MiB budget and 2 GiB of other use left the sampled cache above its floor"); + for (const auto heapGiB : {8u, 16u}) { + small.memoryHeaps[2].size = heapGiB * GiB; + for (std::uint64_t budget = 4 * GiB; budget <= heapGiB * GiB; budget += GiB) { + example.heapBudget[2] = budget; + for (std::uint64_t other = 0; other <= budget; other += GiB) { + for (std::uint64_t sampled = 0; sampled <= 4 * GiB; sampled += 512 * MiB) { + for (std::uint64_t storage = 0; storage <= heapGiB * GiB / 2u; storage += 512 * MiB) { + example.heapUsage[2] = other + sampled + storage; + const auto sampledLimit = SampledTextureBudget(small, &example, sampled + storage, storage); + const auto storageLimit = StorageTextureBudget(small, &example, sampled, storage); + if (sampledLimit > 2 * GiB) Require(other + storage + sampledLimit + budget / 8u <= budget, "the sampled cache budget and the storage images held exceed the reported budget less its eighth"); + if (storageLimit > 512 * MiB) Require(other + sampled + storageLimit + budget / 8u <= budget, "the storage cache budget and the sampled textures held exceed the reported budget less its eighth"); + } + } + } + } + } + } memory.memoryHeaps[0].flags = 0; memory.memoryHeaps[2].flags = 0; Require(SampledTextureBudget(memory, &reported, 0) == 2 * GiB, "a device without a device-local heap does not keep 2 GiB of sampled textures"); @@ -6941,29 +7041,29 @@ void storageTextureBudgetTests() { reported.heapBudget[1] = 100 * GiB; Require(StorageTextureBudget(memory, &reported, 0, 0) == 4 * GiB, "a zero budget for the largest device-local heap did not fall back to a quarter of the heap"); reported.heapBudget[2] = 10 * GiB; - Require(StorageTextureBudget(memory, &reported, 0, 0) == 2560 * MiB, "a 10 GiB budget on the 16 GiB heap does not limit the storage image cache to a quarter of the budget"); + Require(StorageTextureBudget(memory, &reported, 0, 0) == 5 * GiB, "a 10 GiB budget on the 16 GiB heap does not limit the storage image cache to half of the budget"); reported.heapUsage[2] = 5 * GiB; - Require(StorageTextureBudget(memory, &reported, 1 * GiB, 2 * GiB) == 2560 * MiB, "2 GiB used outside the texture caches on a 10 GiB budget lowered the storage image cache below its quarter"); + Require(StorageTextureBudget(memory, &reported, 1 * GiB, 2 * GiB) == 5 * GiB, "2 GiB used outside the texture caches on a 10 GiB budget lowered the storage image cache below its half"); reported.heapUsage[2] = 8 * GiB + 512 * MiB; Require(StorageTextureBudget(memory, &reported, 1 * GiB, 2 * GiB) == 2304 * MiB, "a 10 GiB budget with 5632 MiB used outside the texture caches does not leave 2304 MiB after 1 GiB of sampled textures and 1280 MiB of headroom"); reported.heapUsage[2] = 9 * GiB; Require(StorageTextureBudget(memory, &reported, 3 * GiB, 2 * GiB) == 1792 * MiB, "3 GiB of sampled textures were not reserved in full"); reported.heapUsage[2] = 1 * GiB; - Require(StorageTextureBudget(memory, &reported, 1 * GiB, 2 * GiB) == 2560 * MiB, "usage below the cached bytes is not read as no other device memory in use"); + Require(StorageTextureBudget(memory, &reported, 1 * GiB, 2 * GiB) == 5 * GiB, "usage below the cached bytes is not read as no other device memory in use"); Require(StorageTextureBudget(memory, &reported, 7 * GiB, 1 * GiB) == 1792 * MiB, "7 GiB of sampled textures with the usage below the cached bytes do not leave 1792 MiB after their reservation and 1280 MiB of headroom"); reported.heapUsage[2] = 9 * GiB + 512 * MiB; Require(StorageTextureBudget(memory, &reported, 2 * GiB, 512 * MiB) == 512 * MiB, "a heap whose other users leave no room does not keep the 512 MiB floor"); reported.heapUsage[2] = 0; reported.heapBudget[2] = 6 * GiB; - Require(StorageTextureBudget(memory, &reported, 0, 0) == 1536 * MiB, "a 6 GiB budget on the 16 GiB heap does not limit the storage image cache to a quarter of the budget"); + Require(StorageTextureBudget(memory, &reported, 0, 0) == 3 * GiB, "a 6 GiB budget on the 16 GiB heap does not limit the storage image cache to half of the budget"); reported.heapBudget[2] = 1 * GiB; Require(StorageTextureBudget(memory, &reported, 0, 0) == 512 * MiB, "a 1 GiB budget does not keep the 512 MiB floor"); reported.heapBudget[2] = 40 * GiB; - Require(StorageTextureBudget(memory, &reported, 0, 0) == 4 * GiB, "a budget above the heap size raised the storage image cache past a quarter of the heap"); + Require(StorageTextureBudget(memory, &reported, 0, 0) == 8 * GiB, "a budget above the heap size raised the storage image cache past half of the heap"); memory.memoryHeaps[2].size = 8 * GiB; reported.heapBudget[2] = 7189 * MiB; reported.heapUsage[2] = 4491 * MiB; - Require(StorageTextureBudget(memory, &reported, 49 * MiB, 506 * MiB) == 7189 * MiB / 4u, "49 MiB of sampled textures on an 8 GiB heap with a 7189 MiB budget and 3936 MiB used outside the texture caches held the storage image cache below a quarter of the budget"); + Require(StorageTextureBudget(memory, &reported, 49 * MiB, 506 * MiB) == 7189 * MiB - 3936 * MiB - 49 * MiB - 7189 * MiB / 8u, "49 MiB of sampled textures on an 8 GiB heap with a 7189 MiB budget and 3936 MiB used outside the texture caches did not leave the storage image cache what the budget has left after them and its eighth"); reported.heapUsage[2] = 6490 * MiB; Require(StorageTextureBudget(memory, &reported, 2 * GiB, 506 * MiB) == 512 * MiB, "2 GiB of sampled textures on an 8 GiB heap with a 7189 MiB budget and 3936 MiB used outside the texture caches did not take the storage image cache back to its 512 MiB floor"); memory.memoryHeaps[2].size = 10 * GiB; @@ -6980,8 +7080,8 @@ void storageTextureBudgetTests() { reported.heapUsage[2] = outside + sampled + 512 * MiB; const auto limit = StorageTextureBudget(memory, &reported, sampled, 512 * MiB); Require(limit >= 512 * MiB, "pressure sweep lost the storage cache floor"); - Require(limit <= std::max(512 * MiB, budget / 4u), "pressure sweep exceeded a quarter of the reported budget above the floor"); - Require(limit <= std::max(2 * GiB, heapGiB * GiB / 4u), "pressure sweep exceeded the heap storage cache cap"); + Require(limit <= std::max(512 * MiB, budget / 2u), "pressure sweep exceeded half of the reported budget above the floor"); + Require(limit <= std::max(512 * MiB, heapGiB * GiB / 2u), "pressure sweep exceeded half of the heap"); if (outside != 0) Require(limit <= previous, "additional non-cache memory usage grew the storage cache budget"); if (limit > 512 * MiB) Require(outside + sampled + budget / 8u + limit <= budget, "storage cache budget consumed live sampled memory or pressure headroom"); previous = limit; @@ -10714,6 +10814,21 @@ void validationTests() { attribute.components = 2; attribute.resource.fields[1] |= 0x80000000u; expectFailure([&] { AgcDriver::Graphics::BuildVertexInputLayout(context, std::span(&attribute, 1)); }, "descriptor flags"); + attribute.resource.fields = {0x1000, 8u << 16u, 3, 71u << 12u}; + attribute.components = 3; + context.formatProperties = [](VkPhysicalDevice, VkFormat format, VkFormatProperties* properties) { + *properties = {}; + if (format == VK_FORMAT_R16G16B16A16_SFLOAT || format == VK_FORMAT_R8G8B8A8_UNORM) properties->bufferFeatures = VK_FORMAT_FEATURE_VERTEX_BUFFER_BIT; + }; + Require(AgcDriver::Graphics::DecodeVertexFormat(attribute).format == VK_FORMAT_R16G16B16_SFLOAT && AgcDriver::Graphics::DecodeVertexFormat(attribute).bytes == 6, "a 3-component 16_16_16_16 float attribute was not decoded as 3 components"); + const auto widened = AgcDriver::Graphics::BuildVertexInputLayout(context, std::span(&attribute, 1)); + Require(widened.attributes[0].format == VK_FORMAT_R16G16B16A16_SFLOAT && widened.bindings[0].stride == 8 && AgcDriver::Graphics::VertexReadBytes(attribute, widened.attributes[0].format) == 8, "a 3-component 16_16_16_16 float attribute was not widened to the format the device supports"); + Require(AgcDriver::Graphics::VertexAttributeSignature(attribute) == "f32x3", "widening the vertex format changed the shader input"); + attribute.resource.fields = {0x1000, 4u << 16u, 3, 56u << 12u}; + Require(AgcDriver::Graphics::BuildVertexInputLayout(context, std::span(&attribute, 1)).attributes[0].format == VK_FORMAT_R8G8B8A8_UNORM, "a 3-component 8_8_8_8 unorm attribute was not widened to the format the device supports"); + attribute.resource.fields = {0x1000, 12u << 16u, 3, 74u << 12u}; + expectFailure([&] { AgcDriver::Graphics::BuildVertexInputLayout(context, std::span(&attribute, 1)); }, "device does not support vertex format 106"); + expectFailure([&] { AgcDriver::Graphics::VertexReadBytes(attribute, VK_FORMAT_R32G32B32A32_SFLOAT); }, "is not a form of"); } for (const auto capability : {spv::CapabilityInt64Atomics, spv::CapabilityInt64ImageEXT}) { ShaderRecompiler::RecompileResult vertex; diff --git a/core/libs/prx/libSceAgcDriver/tests/HostAllocator.cpp b/core/libs/prx/libSceAgcDriver/tests/HostAllocator.cpp index e6b6ff60c5..43c38ce262 100644 --- a/core/libs/prx/libSceAgcDriver/tests/HostAllocator.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/HostAllocator.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -112,6 +113,50 @@ std::size_t PrivateBytes() { #endif } +void BlocksFlowToAnotherThread() { + constexpr int rounds = 12; + constexpr int perRound = 200000; + std::size_t afterEarlyRounds = 0; + for (int round = 0; round < rounds; ++round) { + std::mutex lock; + std::vector handed; + std::atomic done{false}; + std::thread freer([&] { + std::vector taken; + for (;;) { + const bool finished = done.load(std::memory_order_acquire); + { + std::lock_guard guard(lock); + taken.swap(handed); + } + for (auto* block : taken) { + const auto size = static_cast(block[0]) * 4 + 1; + Check(block[size - 1] == block[0], "a block freed on another thread keeps its bytes until then"); + ::operator delete(block); + } + taken.clear(); + if (finished) break; + std::this_thread::yield(); + } + }); + for (int i = 0; i < perRound; ++i) { + const auto seed = static_cast(i % 256); + const auto size = static_cast(seed) * 4 + 1; + auto* block = static_cast(::operator new(size)); + block[0] = seed; + block[size - 1] = seed; + std::lock_guard guard(lock); + handed.push_back(block); + } + done.store(true, std::memory_order_release); + freer.join(); + if (round == 3) afterEarlyRounds = PrivateBytes(); + } + const auto afterLastRound = PrivateBytes(); + std::printf("private bytes after 4 and after %d rounds of blocks freed on another thread: %zu, %zu\n", rounds, afterEarlyRounds, afterLastRound); + Check(afterLastRound < afterEarlyRounds + (64u << 20), "blocks freed on another thread do not pile up"); +} + void ThreadsEndWithCachedBlocks() { constexpr int waves = 24; std::size_t afterEarlyWaves = 0; @@ -144,6 +189,7 @@ int main() { SizedDeletesKeepTheirClass(); BlocksCrossIntoLibstdcxx(); BlocksMoveBetweenThreads(); + BlocksFlowToAnotherThread(); ThreadsEndWithCachedBlocks(); if (failures != 0) { std::fprintf(stderr, "%d host allocator checks failed\n", failures); diff --git a/core/libs/prx/libSceAgcDriver/tests/LdsLockBudget.cpp b/core/libs/prx/libSceAgcDriver/tests/LdsLockBudget.cpp index d409fc1d07..55c6fc91b1 100644 --- a/core/libs/prx/libSceAgcDriver/tests/LdsLockBudget.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/LdsLockBudget.cpp @@ -114,7 +114,7 @@ void verify(std::uint32_t waveSize, std::uint32_t limit, bool int64Atomics) { const bool native = int64Atomics && limit < 68u; require(widths.contains(64u) == native, native ? "device-memory LDS did not use a 64-bit compare exchange" : "a 64-bit compare exchange appeared without device-memory LDS and shaderBufferInt64Atomics"); require(widths.contains(32u) != native, native ? "device-memory LDS with shaderBufferInt64Atomics kept the workgroup lock" : "the 64-bit LDS atomic lost its workgroup lock"); - require(result.bdaAbiVersion == (limit < 68u ? 1u : 0u), "the fault buffer for a misaligned 64-bit LDS atomic does not follow device-memory LDS at limit " + std::to_string(limit)); + require(result.bdaAbiVersion == 1u, "a 64-bit LDS atomic did not get the fault buffer for a misaligned address at limit " + std::to_string(limit)); } } diff --git a/core/libs/prx/libSceAgcDriver/tests/PerformanceOverlay.cpp b/core/libs/prx/libSceAgcDriver/tests/PerformanceOverlay.cpp index b5db144ac8..2c138fecf3 100644 --- a/core/libs/prx/libSceAgcDriver/tests/PerformanceOverlay.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/PerformanceOverlay.cpp @@ -105,6 +105,8 @@ public: Device() { #ifdef _WIN32 library = SDL_LoadObject("vulkan-1.dll"); +#elif defined(__APPLE__) + library = SDL_LoadObject("libvulkan.1.dylib"); #else library = SDL_LoadObject("libvulkan.so.1"); #endif @@ -116,6 +118,12 @@ public: application.apiVersion = VK_API_VERSION_1_1; VkInstanceCreateInfo info{VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO}; info.pApplicationInfo = &application; +#ifdef __APPLE__ + const char* portability = VK_KHR_PORTABILITY_ENUMERATION_EXTENSION_NAME; + info.flags = VK_INSTANCE_CREATE_ENUMERATE_PORTABILITY_BIT_KHR; + info.enabledExtensionCount = 1; + info.ppEnabledExtensionNames = &portability; +#endif Check(function("vkCreateInstance")(&info, nullptr, &instance), "vkCreateInstance"); std::uint32_t count = 0; const auto enumerate = function("vkEnumeratePhysicalDevices"); diff --git a/core/libs/prx/libSceAgcDriver/tests/Pm4.cpp b/core/libs/prx/libSceAgcDriver/tests/Pm4.cpp index 0b219aa514..a7ea1d923b 100644 --- a/core/libs/prx/libSceAgcDriver/tests/Pm4.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/Pm4.cpp @@ -402,8 +402,17 @@ void testIndirectDraw() { alignas(4) std::array indices{}; state.indexBase = reinterpret_cast(indices.data()); state.indexType = 0; + state.indexBufferSize.reset(); + const auto unlimited = AgcDriver::Pm4::ResolveDraw(makePacket(0x25, {0, 0x8c, 0x280, 0}), state); + check(unlimited.indexed && unlimited.indirect && !unlimited.indirect->indexLimitKnown && unlimited.indexCount == 0 && unlimited.indexAddress == state.indexBase && unlimited.indexSize == 2 && unlimited.indirect->recordBytes == 20, "an indexed indirect draw without INDEX_BUFFER_SIZE did not resolve with an unknown limit"); + const auto unlimitedMulti = AgcDriver::Pm4::ResolveDraw(makePacket(0x38, {0, 0x8c, 0x280, 0x280, 3, 0, 0, 20, 0}), state); + check(unlimitedMulti.indirect && !unlimitedMulti.indirect->indexLimitKnown && unlimitedMulti.indirect->count == 3 && unlimitedMulti.indirect->stride == 20, "a multi indexed indirect draw without INDEX_BUFFER_SIZE did not resolve with an unknown limit"); + execute(state, makePacket(0x13, {0})); + check(state.indexBufferSize.has_value() && *state.indexBufferSize == 0, "INDEX_BUFFER_SIZE of zero was not recorded as written"); expectFailure([&] { AgcDriver::Pm4::ResolveDraw(makePacket(0x25, {0, 0x8c, 0x280, 0}), state); }, "INDEX_BUFFER_SIZE"); + expectFailure([&] { AgcDriver::Pm4::ResolveDraw(makePacket(0x38, {0, 0x8c, 0x280, 0x280, 3, 0, 0, 20, 0}), state); }, "INDEX_BUFFER_SIZE"); state.indexBufferSize = 8; + check(AgcDriver::Pm4::ResolveDraw(makePacket(0x25, {0, 0x8c, 0x280, 0}), state).indirect->indexLimitKnown, "a written INDEX_BUFFER_SIZE resolved with an unknown limit"); const auto indexed = AgcDriver::Pm4::ResolveDraw(makePacket(0x25, {0, 0x8c, 0x280, 0}), state); check(indexed.indexed && indexed.indexAddress == state.indexBase && indexed.indexCount == 8 && indexed.indexSize == 2 && indexed.indirect->recordBytes == 20 && indexed.indirect->stride == 20 && indexed.firstVertex == 0 && indexed.indirect->indxOffset == 0, "indexed indirect draw mismatch"); check(indexed.indirect->VertexDwordOffset() == 12 && indexed.indirect->InstanceDwordOffset() == 16, "indexed record geometry mismatch"); @@ -1128,6 +1137,63 @@ void testReleaseMemDataSelects() { for (const auto select : {6u, 7u}) expectFailure([&] { AgcDriver::Pm4::Validate(release(select), 0); }, "reserved RELEASE_MEM DATA_SEL"); } +void testMemSemaphore() { + alignas(8) static std::uint64_t semaphore = 0; + alignas(8) static std::uint32_t marker = 0; + const auto packet = [](std::uint32_t control) { return makePacket(0x39, {low(&semaphore), high(&semaphore), control}); }; + check(AgcDriver::Pm4::AccessesMemory(packet(0xc0100000u)[0]), "MEM_SEMAPHORE must synchronize guest memory"); + for (const auto queue : {0u, 0x20u}) { + AgcDriver::Pm4::Validate(packet(0xc0100000u), queue); + AgcDriver::Pm4::Validate(packet(0xe0000000u), queue); + } + expectFailure([&] { AgcDriver::Pm4::Validate(packet(0xe0010000u), 0); }, "mailbox"); + expectFailure([&] { AgcDriver::Pm4::Validate(packet(0xa0000000u), 0); }, "MEM_SEMAPHORE operation"); + expectFailure([&] { AgcDriver::Pm4::Validate(makePacket(0x39, {low(&semaphore), high(&semaphore), 0xc0100000u, 0}), 0); }, "packet size"); + expectFailure([&] { AgcDriver::Pm4::Validate(makePacket(0x39, {0, 0, 0xc0100000u}), 0); }, "MEM_SEMAPHORE address"); + expectFailure([&] { AgcDriver::Pm4::Validate(makePacket(0x39, {low(&semaphore) + 4, high(&semaphore), 0xc0100000u}), 0); }, "MEM_SEMAPHORE address"); + + semaphore = 0x500000000ull; + auto words = packet(0xc0100000u); + submitWords(words); + AgcDriverWaitIdle_nid_postfix(); + check(std::atomic_ref(semaphore).load() == 1, "a MEM_SEMAPHORE signal of type 1 did not write 1"); + + semaphore = 5; + words = packet(0xc0000000u); + submitWords(words); + AgcDriverWaitIdle_nid_postfix(); + check(std::atomic_ref(semaphore).load() == 6, "a MEM_SEMAPHORE signal of type 0 did not increment"); + + constexpr std::uint64_t queueSignals = 2000, cpuSignals = 200000; + semaphore = 0; + std::vector graphicsWords, computeWords; + for (std::uint64_t index = 0; index < queueSignals; ++index) { + const auto signal = packet(0xc0000000u); + graphicsWords.insert(graphicsWords.end(), signal.begin(), signal.end()); + computeWords.insert(computeWords.end(), signal.begin(), signal.end()); + } + std::thread cpu([] { + for (std::uint64_t index = 0; index < cpuSignals; ++index) std::atomic_ref(semaphore).fetch_add(1); + }); + submitWords(graphicsWords); + submitWords(computeWords, 0x20); + cpu.join(); + AgcDriverWaitIdle_nid_postfix(); + check(std::atomic_ref(semaphore).load() == 2 * queueSignals + cpuSignals, "competing MEM_SEMAPHORE signals of type 0 lost an increment"); + + semaphore = 0; + marker = 0; + words = joinPackets({packet(0xe0000000u), makePacket(0x49, {0x0030c514, 0x20000000, low(&marker), high(&marker), 1, 0, 0})}); + submitWords(words); + std::this_thread::sleep_for(std::chrono::milliseconds(20)); + check(std::atomic_ref(marker).load() == 0, "a MEM_SEMAPHORE wait passed a zero semaphore"); + std::atomic_ref(semaphore).store(2); + for (int waited = 0; waited < 2000 && std::atomic_ref(marker).load() == 0; ++waited) std::this_thread::sleep_for(std::chrono::milliseconds(1)); + AgcDriverWaitIdle_nid_postfix(); + check(std::atomic_ref(marker).load() == 1, "work after a signalled MEM_SEMAPHORE wait never ran"); + check(std::atomic_ref(semaphore).load() == 1, "a MEM_SEMAPHORE wait did not take one from the semaphore"); +} + void testSuspendPointWritesQueuedLabels() { alignas(8) static std::uint32_t gate = 0, marker = 0; gate = 0; @@ -2015,6 +2081,7 @@ int main(int argc, char** argv) { testPredicatedSubmission(); testConditionalSubmission(); testSuspendPointWritesQueuedLabels(); + testMemSemaphore(); testNopPadSubmission(); testBranchSubmission(); testWaitRegMemMasking(); diff --git a/core/libs/prx/libSceAgcDriver/tests/PresentationPass.cpp b/core/libs/prx/libSceAgcDriver/tests/PresentationPass.cpp index f55704d524..11915c8575 100644 --- a/core/libs/prx/libSceAgcDriver/tests/PresentationPass.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/PresentationPass.cpp @@ -7,6 +7,7 @@ #include "prx/libSceAgcDriver/Graphics/include/ColorTargetLayout.hpp" #include "prx/libSceAgcDriver/Graphics/include/Resources.hpp" #include "prx/libSceAgcDriver/Graphics/include/TextureFormat.hpp" +#include "execution/FixtureDeviceSelection.hpp" #include #include #include @@ -38,6 +39,8 @@ public: Device() { #ifdef _WIN32 library = SDL_LoadObject("vulkan-1.dll"); +#elif defined(__APPLE__) + library = SDL_LoadObject("libvulkan.1.dylib"); #else library = SDL_LoadObject("libvulkan.so.1"); #endif @@ -49,6 +52,12 @@ public: application.apiVersion = VK_API_VERSION_1_1; VkInstanceCreateInfo info{VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO}; info.pApplicationInfo = &application; +#ifdef __APPLE__ + const char* portability = VK_KHR_PORTABILITY_ENUMERATION_EXTENSION_NAME; + info.flags = VK_INSTANCE_CREATE_ENUMERATE_PORTABILITY_BIT_KHR; + info.enabledExtensionCount = 1; + info.ppEnabledExtensionNames = &portability; +#endif Check(function("vkCreateInstance")(&info, nullptr, &instance), "vkCreateInstance"); std::uint32_t count = 0; const auto enumerate = function("vkEnumeratePhysicalDevices"); @@ -56,7 +65,7 @@ public: Require(count != 0, "no Vulkan device"); std::vector devices(count); Check(enumerate(instance, &count, devices.data()), "vkEnumeratePhysicalDevices"); - context.physical = devices.front(); + context.physical = SelectFixturePhysicalDevice(devices, function("vkGetPhysicalDeviceProperties")); const auto queues = function("vkGetPhysicalDeviceQueueFamilyProperties"); queues(context.physical, &count, nullptr); std::vector families(count); diff --git a/core/libs/prx/libSceAgcDriver/tests/Recorder.cpp b/core/libs/prx/libSceAgcDriver/tests/Recorder.cpp index dcb157bff2..a86232068f 100644 --- a/core/libs/prx/libSceAgcDriver/tests/Recorder.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/Recorder.cpp @@ -2561,7 +2561,7 @@ void remappedImportTests(const Device& device) { void hostImportRetirementTests(const Device& device) { const auto& context = device.GetContext(); - constexpr std::size_t UnitBytes = 65536; + constexpr std::size_t UnitBytes = 2 * 65536; constexpr std::size_t AllocationBytes = 4 * UnitBytes; constexpr std::size_t CopyBytes = 256; #ifdef _WIN32 @@ -3955,10 +3955,11 @@ void drawSnapshotTableSetTests(const Device& device, Recorder& recorder) { } constexpr std::uint32_t width = 4; constexpr std::uint32_t height = 4; - alignas(256) static std::array texels{}; + alignas(256) static std::array texels{}; texels.fill(1.0f); const auto address = static_cast(reinterpret_cast(texels.data())); const std::array texture{static_cast(address >> 8u), static_cast((address >> 40u) & 0xffu) | (22u << 20u) | (((width - 1u) & 3u) << 30u), ((width - 1u) >> 2u) | ((height - 1u) << 14u), 0xfacu | (9u << 28u), 0u, 0u, 0u, 0u}; + Require(DescribeSurface(DecodeTextureResource(texture)).guestBytes <= sizeof(texels), "the draw snapshot table set test T# reads past its texels"); constexpr auto bindingCount = static_cast(ShaderRecompiler::RuntimeAbi::Binding::Count); const auto addTable = [&](ShaderRecompiler::RecompileResult& program, ShaderRecompiler::RuntimeAbi::Stage stage, std::uint32_t set, std::uint32_t elements) { ShaderRecompiler::DescriptorBinding map{}; @@ -4552,6 +4553,46 @@ void drawSnapshotEvictionTests(const Device& device) { Require(cache.ReusableDrawSnapshot(address, 16) == nullptr && cache.ReusableDrawSnapshot(address, 16) == nullptr, "a snapshot outlived a CPU store"); } +void containingSnapshotTests(const Device& device) { + using namespace AgcDriver::GuestMemory; + constexpr std::size_t bytes = 131072; + void* block = WriteWatched() ? AllocateWatched(bytes, 65536) : nullptr; + if (block == nullptr) { + std::cout << "write watching unavailable: containing draw snapshots not tested\n"; + return; + } + struct Release { + void* block; + ~Release() { ReleaseWatched(block, bytes); } + } release{block}; + const auto address = reinterpret_cast(block); + const VkDeviceSize alignment = std::max(device.GetContext().limits.minStorageBufferOffsetAlignment, 4); + Recorder cache(device.GetContext()); + const auto whole = std::make_shared(device.GetContext(), bytes, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT); + const auto exact = std::make_shared(device.GetContext(), 4096, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT); + const auto registry = GuestAllocations::GuestAllocationsGeneration_nid_postfix(); + const auto generation = CollectWrites(address, bytes); + Require(generation != 0, "the watched block has no generation"); + cache.KeepDrawSnapshot(address, bytes, generation, registry, whole); + const auto inner = address + 65536 + 16 * alignment; + VkDeviceSize offset = 1; + Require(cache.ReusableDrawSnapshot(inner, 4096) == nullptr, "a lookup without an offset took a containing snapshot"); + Require(cache.ReusableDrawSnapshot(inner, 4096, Recorder::SnapshotUse::Storage, nullptr, 0, &offset) == whole && offset == inner - address, "an unchanged range inside a kept snapshot was not taken from it at its offset"); + Require(cache.ReusableDrawSnapshot(inner, address + bytes - inner + 4, Recorder::SnapshotUse::Storage, nullptr, 0, &offset) == nullptr, "a range past the end of a kept snapshot was taken from it"); + Require(cache.ReusableDrawSnapshot(inner + alignment / 2, 4096, Recorder::SnapshotUse::Storage, nullptr, 0, &offset) == nullptr, "a misaligned range was taken from a kept snapshot"); + Require(cache.ReusableDrawSnapshot(inner, 4096, Recorder::SnapshotUse::Vertex, nullptr, 0, &offset) == nullptr, "a vertex lookup took a storage snapshot"); + cache.KeepDrawSnapshot(inner, 4096, generation, registry, exact); + offset = 1; + Require(cache.ReusableDrawSnapshot(inner, 4096, Recorder::SnapshotUse::Storage, nullptr, 0, &offset) == exact && offset == 0, "an exact snapshot lost to a containing one"); + std::memset(block, 0x5a, 16); + CollectWrites(address, 16); + Require(cache.ReusableDrawSnapshot(inner + 4096, 4096, Recorder::SnapshotUse::Storage, nullptr, 0, &offset) == whole && offset == inner + 4096 - address, "a store outside the range stopped it from being taken from a containing snapshot"); + Require(cache.ReusableDrawSnapshot(address, 4096, Recorder::SnapshotUse::Storage, nullptr, 0, &offset) == nullptr, "a range over a CPU store was taken from a containing snapshot"); + std::memset(reinterpret_cast(inner + 4096), 0x5a, 16); + CollectWrites(inner + 4096, 16); + Require(cache.ReusableDrawSnapshot(inner + 4096, 4096, Recorder::SnapshotUse::Storage, nullptr, 0, &offset) == nullptr, "a containing snapshot outlived a CPU store into the range"); +} + void unwrittenSnapshotTests(const Device& device) { Recorder cache(device.GetContext()); const auto buffer = std::make_shared(device.GetContext(), 16, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT); @@ -8711,6 +8752,106 @@ void multisampleGuestAccessTests(const Device& device, Recorder& recorder) { std::cout << "Multisampled target guest access tests passed\n"; } +void oneFragmentFmaskGuardTests(const Device& device) { + const auto& base = device.GetContext(); + if (!device.NullDescriptors()) { + std::cout << "nullDescriptor unavailable: one-fragment FMASK guard not tested\n"; + return; + } + constexpr std::uint32_t side = 64; + constexpr std::size_t bytes = 3u * 65536u; +#ifdef _WIN32 + void* block = VirtualAlloc(nullptr, bytes, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); +#else + void* block = std::aligned_alloc(65536, bytes); +#endif + Require(block != nullptr, "cannot allocate the one-fragment FMASK block"); + std::memset(block, 0, bytes); + const auto address = reinterpret_cast(block); + { + GuestAllocations::Mutation mutation; + mutation.Add(block, bytes, true, true, true); + } + struct Unregister { + const Context& context; + void* block; + ~Unregister() { + ClearMultisampleTargets(context.device); + ClearCachedTextures(context.device); + { + GuestAllocations::Mutation mutation; + mutation.Remove(block); + } +#ifdef _WIN32 + VirtualFree(block, 0, MEM_RELEASE); +#else + std::free(block); +#endif + } + } unregister{base, block}; + TextureDetiler detiler(base); + auto context = base; + context.detiler = &detiler; + context.nullDescriptors = true; + TextureCache cache(context); + context.textureCache = &cache; + const auto refused = [](const std::function& access, const std::string& what) { + std::string message; + try { + access(); + } catch (const std::exception& error) { + message = error.what(); + } + Require(message.find("FMASK") != std::string::npos && message.find("one-fragment color target") != std::string::npos, what + " of a host-drawn one-fragment target's FMASK did not throw: " + message); + }; + ColorTarget target{}; + target.extent = {128, 128}; + target.samples = 1; + target.coverageSamples = 4; + target.fmaskAddress = address; + GuardOneFragmentFmask(context, target); + GuardOneFragmentFmask(context, target); + std::array words{}; + refused([&] { AgcDriver::GuestMemory::Read(address + 256u, words); }, "a CPU read"); + refused([&] { RequireNoMultisampleTarget(address + 65535u, 1u, "a DMA copy"); }, "a DMA copy of the last guarded byte"); + RequireNoMultisampleTarget(address + 65536u, 64u, "a DMA copy"); + ShaderRecompiler::RecompileResult program; + ShaderRecompiler::DescriptorBinding binding; + binding.kind = ShaderRecompiler::DescriptorKind::SampledImage; + binding.role = ShaderRecompiler::DescriptorRole::GuestImages; + binding.descriptorSet = 0; + binding.binding = ShaderRecompiler::RuntimeAbi::FirstImageBinding; + binding.count = 1; + binding.imageShape = ShaderRecompiler::DescriptorImageShape::Image2D; + binding.imageSamplers = {0}; + binding.guestDescriptor = { + static_cast(address >> 8u), + static_cast((address >> 40u) & 0xffu) | (56u << 20u) | (((side - 1u) & 3u) << 30u), + ((side - 1u) >> 2u) | ((side - 1u) << 14u), + 0xfacu | (9u << 28u), + 0u, + 0u, + 0u, + 0u, + }; + program.bindings.push_back(binding); + const CompiledShader compute{ShaderRecompiler::ShaderStage::Compute, &program, 0}; + for (const char* what : {"a texture read", "a repeated texture read"}) { + refused([&] { + std::lock_guard gpu(GpuMutex()); + ShaderResources resources(context, compute); + }, what); + } + { + GuestAllocations::Mutation mutation; + mutation.Unmap(block, bytes, [](const void*, std::size_t, const void*, bool) {}); + mutation.Add(block, bytes, true, true, true); + } + AgcDriver::GuestMemory::Read(address + 256u, words); + RequireNoMultisampleTarget(address, 65536u, "a DMA copy"); + std::cout << "One-fragment FMASK guard tests passed\n"; +} + void fmaskDecompressMsaaTests(const Device& device, Recorder& recorder) { const auto& base = device.GetContext(); if ((base.limits.framebufferColorSampleCounts & VK_SAMPLE_COUNT_4_BIT) == 0 || base.hostImportAlignment == 0) { @@ -9867,7 +10008,7 @@ int pipelineCacheFileTests(const Device& device) { } removeDirectory{directory}; #ifdef _WIN32 _putenv_s("ANYPS5_NO_SHADER_CACHE", "0"); - _putenv_s("ANYPS5_SHADER_CACHE_DIR", directory.string().c_str()); + _wputenv_s(L"ANYPS5_SHADER_CACHE_DIR", directory.wstring().c_str()); #else setenv("ANYPS5_NO_SHADER_CACHE", "0", 1); setenv("ANYPS5_SHADER_CACHE_DIR", directory.string().c_str(), 1); @@ -11165,6 +11306,7 @@ int main(int argc, char** argv) { } if (argc == 2 && std::string_view(argv[1]) == "--multisample-only") { multisampleGuestAccessTests(device, recorder); + oneFragmentFmaskGuardTests(device); fmaskDecompressMsaaTests(device, recorder); return 0; } @@ -11183,6 +11325,7 @@ int main(int argc, char** argv) { } if (argc == 2 && std::string_view(argv[1]) == "--draw-snapshot-cap-only") { drawSnapshotCapTests(device, recorder); + containingSnapshotTests(device); unwrittenSnapshotTests(device); drawSnapshotUploadTests(device, recorder); std::cout << "Draw snapshot cap tests passed\n"; @@ -11225,6 +11368,7 @@ int main(int argc, char** argv) { bdaStagingTests(device, recorder); depthSurfaceProofTests(device, recorder); multisampleGuestAccessTests(device, recorder); + oneFragmentFmaskGuardTests(device); tiledResolveTests(device, recorder); fmaskDecompressMsaaTests(device, recorder); misalignedSnapshotTests(device, recorder); @@ -11235,6 +11379,7 @@ int main(int argc, char** argv) { imageTableFirstLayerRevalidateTests(device, recorder); drawSnapshotCapTests(device, recorder); drawSnapshotEvictionTests(device); + containingSnapshotTests(device); unwrittenSnapshotTests(device); drawSnapshotUploadTests(device, recorder); drawInputReuseTests(device, recorder); diff --git a/core/libs/prx/libSceAgcDriver/tests/RedundantValueElimination.cpp b/core/libs/prx/libSceAgcDriver/tests/RedundantValueElimination.cpp index cc88c42cfe..8dfde5e87e 100644 --- a/core/libs/prx/libSceAgcDriver/tests/RedundantValueElimination.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/RedundantValueElimination.cpp @@ -1,4 +1,5 @@ #include "IntermediateRepresentation/IrProgram.hpp" +#include "Optimization/DeadCodeEliminator.hpp" #include "Optimization/RedundantValueEliminator.hpp" #include "Recompiler.hpp" #include @@ -505,6 +506,55 @@ int main() { return b.eliminate() == 1u && b.program.Metadata().blockInfo.front().condition == &first; }); + passed &= run("descriptor dwords name the surviving values after redundancy and dead code elimination", [] { + Builder b; + auto& input = b.unknown(); + DescriptorSource descriptor; + descriptor.dwordCount = 8u; + std::array kept{}; + std::array removed{}; + for (std::uint32_t index = 0; index < 8u; ++index) { + auto& first = b.emit(IrOpcode::IAdd32, IrType::U32, {&input, &b.constant(index + 1u)}); + auto& second = b.emit(IrOpcode::IAdd32, IrType::U32, {&input, &b.constant(index + 1u)}); + b.keep(first); + b.keep(second); + descriptor.dwords[index] = &second; + kept[index] = &first; + removed[index] = &second; + } + b.program.Resources().descriptorSources.push_back(descriptor); + b.program.Resources().descriptorSources.emplace_back(); + if (b.eliminate() != 8u) return false; + DeadCodeEliminator{}.Eliminate(b.program); + const auto& descriptors = b.program.Resources().descriptorSources; + for (std::size_t index = 0; index < kept.size(); ++index) { + if (descriptors[0].dwords[index] != kept[index] || kept[index]->Parent() != b.block || removed[index]->Parent() != nullptr || descriptors[1].dwords[index] != nullptr) return false; + } + return true; + }); + + passed &= run("a selected table column keeps its surviving runtime offset", [] { + Builder b; + auto& input = b.unknown(); + auto& first = b.emit(IrOpcode::IMul32, IrType::U32, {&input, &b.constant(48u)}); + auto& second = b.emit(IrOpcode::IMul32, IrType::U32, {&input, &b.constant(48u)}); + b.keep(first); + b.keep(second); + DescriptorSource selection; + selection.dwordCount = 1u; + selection.dwords[0] = &second; + DescriptorSource image; + image.tableColumn.emplace(); + image.tableColumn->selected = true; + image.tableColumn->selection = 0u; + b.program.Resources().descriptorSources = {selection, image}; + if (b.eliminate() != 1u) return false; + DeadCodeEliminator{}.Eliminate(b.program); + const auto& descriptors = b.program.Resources().descriptorSources; + const auto selected = descriptors[1].tableColumn->selection; + return descriptors[selected].dwords[0] == &first && first.Parent() == b.block && second.Parent() == nullptr; + }); + passed &= run("lane reads are not merged", [] { Builder b; auto& first = b.unknown(); diff --git a/core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp b/core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp index 2b6dee3b5e..cc86441030 100644 --- a/core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp @@ -62,11 +62,19 @@ void setEnvironment(const char* name, const std::string& value) { #endif } +void setPathEnvironment(const char* name, const std::filesystem::path& value) { +#ifdef _WIN32 + _wputenv_s(std::filesystem::path(name).wstring().c_str(), value.wstring().c_str()); +#else + setenv(name, value.c_str(), 1); +#endif +} + #ifdef _WIN32 void verifyHostStandardError() { const auto directory = std::filesystem::temp_directory_path() / ("aps5-shader-host-stderr-" + std::to_string(std::random_device{}())); setEnvironment("ANYPS5_NO_SHADER_CACHE", "0"); - setEnvironment("ANYPS5_SHADER_CACHE_DIR", directory.string()); + setPathEnvironment("ANYPS5_SHADER_CACHE_DIR", directory); int capture[2], guest[2]; require(::_pipe(capture, 65536, _O_BINARY) == 0, "cannot create host capture"); require(::_pipe(guest, 65536, _O_BINARY) == 0, "cannot create guest capture"); @@ -324,6 +332,7 @@ CompiledVariant sampleVariant() { image.volumeArraySampleCompatible = true; image.flatLineCompatible = false; image.flatVolumeLineCompatible = false; + image.dimensionQueryOnly = false; image.foldTexelOffsetsOnly = true; image.remappedDepth1D = true; image.byElements = 4; @@ -1073,7 +1082,7 @@ void verifyLongHostPaths() { const auto extended = [](const auto& path) { return std::filesystem::path(L"\\\\?\\" + path.native()); }; std::filesystem::create_directories(extended(directory)); setEnvironment("ANYPS5_NO_SHADER_CACHE", "0"); - setEnvironment("ANYPS5_SHADER_CACHE_DIR", directory.string()); + setPathEnvironment("ANYPS5_SHADER_CACHE_DIR", directory); std::vector files; std::filesystem::path entries; struct Cleanup { @@ -2120,6 +2129,36 @@ void verifyMalformedPlans() { require(!ShaderDiskCache::EncodePlan(unblocked, bytes), "a phi without predecessor blocks was encoded"); } +void verifySharedPlans() { + auto fixture = planFixtures().front(); + const auto request = fixture.Request(false); + const auto plan = GetResourcePlan(request); + const std::vector first{std::byte{0x5a}, std::byte{0x01}, std::byte{0x02}}; + const std::vector second{std::byte{0x5a}, std::byte{0x01}, std::byte{0x03}}; + for (const auto* key : {&first, &second}) { + const auto entry = ShaderDiskCache::EncodePlanEntry(*key, *plan); + require(WriteFileAtomically(ShaderDiskCache::EntryDirectory() / ShaderDiskCache::EntryName(*key), entry), "cannot write the shared plan entries"); + } + constexpr std::uint64_t address = 0x7000u; + IrResourcePlan expected; + require(ShaderDiskCache::DecodePlan(encodedPlan(*plan), expected), "the shared plan fixture does not decode"); + expected.shaderHash = address; + const auto hits = ShaderDiskCache::Totals().planHits; + auto a = ShaderDiskCache::LoadSharedPlan(first, address); + auto b = ShaderDiskCache::LoadSharedPlan(second, address); + require(a != nullptr && b != nullptr && ShaderDiskCache::Totals().planHits == hits + 2u, "the shared plan entries did not load"); + require(a.get() == b.get(), "two plan entries with the same payload for one shader decoded into separate plans"); + require(samePlan(*a, expected), "a shared plan differs from the stored plan"); + const auto other = ShaderDiskCache::LoadSharedPlan(second, address + 0x100u); + require(other != nullptr && other.get() != a.get() && other->shaderHash == address + 0x100u && a->shaderHash == address, "a plan of another shader address was shared"); + const std::weak_ptr released = a; + a.reset(); + b.reset(); + require(released.expired(), "the shared plan table kept a plan no source uses"); + const auto again = ShaderDiskCache::LoadSharedPlan(first, address); + require(again != nullptr && samePlan(*again, expected), "a released shared plan did not load again"); +} + void verifyPlanRoundTrip() { const auto handBuilt = handBuiltPlan(); const auto handBytes = encodedPlan(handBuilt); @@ -2499,7 +2538,7 @@ int main(int argc, char** argv) { std::filesystem::remove_all(directory); verifyDefaultDirectory(argv[0]); setEnvironment("ANYPS5_NO_SHADER_CACHE", "0"); - setEnvironment("ANYPS5_SHADER_CACHE_DIR", directory.string()); + setPathEnvironment("ANYPS5_SHADER_CACHE_DIR", directory); verifyAtomicReplacement(directory); verifyResultRoundTrip(); verifyEntryRoundTrip(); @@ -2510,6 +2549,7 @@ int main(int argc, char** argv) { verifyFloatModeSerialization(); verifyPlanRoundTrip(); verifyPlanKey(); + verifySharedPlans(); verifyStore(); verifyPersist(); verifyAcrossProcesses(argv[0]); diff --git a/core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp b/core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp index d050d40a1f..4d7e4dc551 100644 --- a/core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp @@ -1327,6 +1327,69 @@ void verifyLaneMaskGuardedDescriptorSources() { } } +void verifyExecGuardedDescriptorSources() { + using namespace ShaderRecompiler; + constexpr std::uint32_t Format8888UNorm = 56; + constexpr std::uint32_t Type2D = 9; + struct alignas(256) Texture { std::array bytes{}; }; + static Texture texture; + const auto base = static_cast(reinterpret_cast(texture.bytes.data())); + const std::array valid{static_cast(base >> 8u), static_cast((base >> 40u) & 0xffu) | (Format8888UNorm << 20u) | (3u << 30u), 3u << 14u, 0xfacu | (Type2D << 28u), 0u, 0u, 0u, 0u}; + const std::array invalid{0x43700000u, 0x43070000u, 0x3b888889u, 0x3bf2b9d6u, 0x3b088889u, 0x3b72b9d6u, 0x3f7f7777u, 0x3f7f0d46u}; + const std::array sampler{0u, 0u, 0u, 0u}; + std::array output{}; + const auto outputAddress = static_cast(reinterpret_cast(output.data())); + const std::array outputV{static_cast(outputAddress), static_cast((outputAddress >> 32u) & 0xffffu), 16u, 0xfacu}; + std::array srt{}; + const auto fill = [&](const std::array& image, std::uint32_t mode) { + std::copy(image.begin(), image.end(), srt.begin()); + std::copy(sampler.begin(), sampler.end(), srt.begin() + 8); + std::copy(outputV.begin(), outputV.end(), srt.begin() + 16); + srt[20] = mode; + }; + const auto srtAddress = static_cast(reinterpret_cast(srt.data())); + const std::array userData{static_cast(srtAddress), static_cast(srtAddress >> 32u)}; + const std::array capabilities{29u, spv::CapabilityInt64, spv::CapabilityPhysicalStorageBufferAddresses, spv::CapabilityStorageBuffer8BitAccess}; + const std::array extensions{"SPV_KHR_physical_storage_buffer", "SPV_KHR_8bit_storage"}; + RecompileRequest request{}; + const std::vector code{0xf4000400u, 0xfa000050u, 0xf4080500u, 0xfa000020u, 0xf4080600u, 0xfa000040u, 0xbf8cc07fu, 0x7da80010u, 0xbf880006u, 0xf40c0200u, 0xfa000000u, 0xbf8cc07fu, 0xf09c0f08u, 0x00a20000u, 0xbf8c3f70u, 0xe0700000u, 0x80060000u, 0xbf810000u}; + request.shader = {ShaderStage::Compute, 0x24000u, code, 0, {}}; + request.context.waveSize = 32; + request.context.userDataBaseRegister = 0; + request.context.userData = userData; + request.context.compute = ShaderComputeStageInfo{{32u, 1u, 1u}, 0u, {false, false, false}, false, 1u}; + request.target.vulkanVersion = 0x00401000u; + request.target.spirvVersion = 0x00010300u; + request.target.subgroupSize = 32; + request.target.supportedCapabilities = capabilities; + request.target.supportedExtensions = extensions; + request.target.bdaAbiVersion = BdaAbi::Version; + request.target.fragmentShaderBarycentricEnabled = false; + request.layout.pushConstantSizeBytes = 128; + + fill(invalid, 0u); + const auto plan = GetResourcePlan(request); + require(plan->info.images.size() == 1u && plan->info.samplers.size() == 1u, "exec guarded sources: the image and its sampler were not planned"); + require(!plan->controlFlow.empty() && std::ranges::any_of(plan->controlFlow, [](const ResourceBlock& block) { return block.condition != nullptr; }), "exec guarded sources: the EXEC branch around the image kept no condition"); + { + fill(invalid, 0u); + AgcDriver::ShaderMemory memory({}); + const auto capture = memory.Capture(request); + require(capture->snapshot.images.size() == 1u && std::ranges::all_of(capture->snapshot.images[0].dwords, [](std::uint32_t word) { return word == 0u; }), "exec guarded sources: the T# behind a branch no lane takes was not left null"); + } + fill(invalid, 1u); + { + AgcDriver::ShaderMemory memory({}); + expectFailure([&] { static_cast(memory.Capture(request)); }, "runtime image descriptor is invalid", "exec guarded sources: an invalid T# behind a branch the lanes may take was accepted"); + } + fill(valid, 1u); + { + AgcDriver::ShaderMemory memory({}); + const auto capture = memory.Capture(request); + require(capture->snapshot.images.size() == 1u && std::equal(valid.begin(), valid.end(), capture->snapshot.images[0].dwords.begin()), "exec guarded sources: the T# behind a branch the lanes may take was not read"); + } +} + void verifyGuardedDescriptorSources() { using namespace ShaderRecompiler; constexpr std::uint32_t Format8888UNorm = 56; @@ -1713,6 +1776,41 @@ std::array TableSelected(const ShaderRecompiler::ImageTableSna return column.words.at(column.codes[0]).dwords; } +void verifyTargetFeatureKey() { + using namespace ShaderRecompiler; + static constexpr std::array code{0xbf810000u}; + RecompileRequest request{}; + request.shader = {ShaderStage::Compute, 0x10000u, code, 0, {}}; + request.context.waveSize = 64; + const std::vector capabilities{1u, 4448u, 5347u}; + const std::vector extensions{"SPV_KHR_8bit_storage", "SPV_KHR_physical_storage_buffer"}; + const auto build = [&](std::span capabilityList, std::span extensionList) { + auto keyed = request; + keyed.target.supportedCapabilities = capabilityList; + keyed.target.supportedExtensions = extensionList; + std::vector key; + RecompileCacheKey::Build(keyed, key); + return key; + }; + const auto base = build(capabilities, extensions); + const std::vector capabilityCopy = capabilities; + const std::vector extensionStorage{"SPV_KHR_8bit_storage", "SPV_KHR_physical_storage_buffer"}; + const std::vector extensionCopy{extensionStorage[0], extensionStorage[1]}; + require(build(capabilityCopy, extensionCopy) == base, "target feature key: equal capability and extension lists in other storage gave another key"); + auto renamed = extensionStorage; + renamed[1].back() = 'X'; + const std::vector renamedViews{renamed[0], renamed[1]}; + require(build(capabilities, renamedViews) != base, "target feature key: an extension name that differs in its last byte gave the same key"); + const std::vector moved{"SPV_KHR_8bit_storageS", "PV_KHR_physical_storage_buffer"}; + require(build(capabilities, moved) != base, "target feature key: a byte moved between two extension names gave the same key"); + const std::vector more{1u, 4448u, 5347u, 11u}; + require(build(more, extensions) != base, "target feature key: an added capability gave the same key"); + std::vector many; + for (std::uint32_t index = 0; index < 40u; ++index) many.push_back("SPV_EXT_descriptor_indexing_" + std::to_string(index)); + const std::vector manyViews(many.begin(), many.end()); + require(build(capabilities, manyViews).size() == base.size(), "target feature key: the key grows with the device's extension names"); +} + void verifyImageTableKeys() { using namespace ShaderRecompiler; namespace Abi = ImageTableAbi; @@ -8427,7 +8525,9 @@ int main(int argc, char** argv) { verifyPlanCopyAnswers(); verifyGuardedDescriptorSources(); verifyLaneMaskGuardedDescriptorSources(); + verifyExecGuardedDescriptorSources(); verifySrtTape(); + verifyTargetFeatureKey(); verifyImageTableKeys(); verifyImageTables(); verifyImageTableRegistration(); diff --git a/core/libs/prx/libSceAgcDriver/tests/SpirvLdsAtomicLock.cpp b/core/libs/prx/libSceAgcDriver/tests/SpirvLdsAtomicLock.cpp index 8155564e0b..2bb13e5283 100644 --- a/core/libs/prx/libSceAgcDriver/tests/SpirvLdsAtomicLock.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/SpirvLdsAtomicLock.cpp @@ -77,10 +77,18 @@ Usage Scan(std::span words) { std::uint32_t laneZeroBody = 0u; std::uint32_t lockPointer = 0u; std::map constants; + std::uint32_t faultFunction = 0u; + bool inFaultFunction = false; for (std::size_t cursor = 5; cursor < words.size();) { const auto count = words[cursor] >> spv::WordCountShift; const auto op = static_cast(words[cursor] & spv::OpCodeMask); Require(count != 0 && cursor + count <= words.size(), "truncated SPIR-V instruction"); + if (op == spv::OpName && count >= 3u && std::string_view(reinterpret_cast(words.data() + cursor + 2u)) == "record_bda_fault") faultFunction = words[cursor + 1u]; + if (op == spv::OpFunction && count >= 3u) inFaultFunction = words[cursor + 2u] == faultFunction; + if (inFaultFunction) { + cursor += count; + continue; + } if (op == spv::OpDecorate && count >= 4u && words[cursor + 2u] == spv::DecorationBuiltIn && words[cursor + 3u] == spv::BuiltInLocalInvocationIndex) { localIndexVariable = words[cursor + 1u]; } diff --git a/core/libs/prx/libSceAgcDriver/tests/VectorWindow.cpp b/core/libs/prx/libSceAgcDriver/tests/VectorWindow.cpp index 6e07c138b6..0efed550e8 100644 --- a/core/libs/prx/libSceAgcDriver/tests/VectorWindow.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/VectorWindow.cpp @@ -144,6 +144,57 @@ std::size_t WindowStores(std::span words) { return stores; } +constexpr std::array WindowInit{ + 0x34020086, 0x34060088, 0x4a140680, 0x4a160681, 0x4a180682, 0x4a1a0683, 0x4a1c0684, 0x4a1e0685, + 0x4a200686, 0x4a220687, 0x4a240688, 0x4a260689, 0x4a28068a, 0x4a2a068b, 0x4a2c068c, 0x4a2e068d, + 0x4a30068e, 0x4a32068f, 0x4a340690, 0x4a360691, 0x4a380692, 0x4a3a0693, 0x4a3c0694, 0x4a3e0695, + 0x4a400696, 0x4a420697, 0x4a440698, 0x4a460699, 0x4a48069a, 0x4a4a069b, 0x4a4c069c, 0x4a4e069d, + 0x4a50069e, 0x4a52069f, +}; +constexpr std::uint32_t MoveM0BoundedS8 = 0x877c8308u; +constexpr std::uint32_t MoveM0S9 = 0xbefc0309u; +constexpr std::uint32_t MovrelsV4V10 = 0x7e08870au; + +std::vector NarrowingProgram(std::uint32_t unbounded) { + std::vector code(WindowInit.begin(), WindowInit.end()); + code.push_back(MoveM0BoundedS8); + code.push_back(MovrelsV4V10); + code.push_back(0xe0701ffcu); + code.push_back(0x80010401u); + code.push_back(MoveM0S9); + for (std::uint32_t read = 0; read < unbounded; ++read) { + const std::uint32_t destination = 42u + read % 100u; + code.push_back((MovrelsV4V10 & ~(0xffu << 17u)) | (destination << 17u)); + code.push_back(0xe0701000u | ((read * 4u) & 0xfffu)); + code.push_back(0x80010001u | destination << 8u); + } + code.push_back(EndProgram); + return code; +} + +std::vector RecompileNarrowing(std::uint32_t unbounded) { + std::array userData{0u, 0u, 0u, 0u, 0x10000000u, 0u, 0x40u, 0x31016facu, 3u, 5u}; + return RecompileProgram(NarrowingProgram(unbounded), userData); +} + +struct Narrowing { + std::uint32_t functionVariables = 0; + std::uint32_t switchCases = 0; +}; + +Narrowing ScanNarrowing(std::span words) { + Narrowing result; + for (std::size_t cursor = 5; cursor < words.size();) { + const auto count = words[cursor] >> spv::WordCountShift; + const auto op = static_cast(words[cursor] & spv::OpCodeMask); + Require(count != 0 && cursor + count <= words.size(), "truncated SPIR-V instruction"); + if (op == spv::OpVariable && words[cursor + 3] == spv::StorageClassFunction) ++result.functionVariables; + if (op == spv::OpSwitch) result.switchCases += (count - 3u) / 2u; + cursor += count; + } + return result; +} + IrProgram TranslateStage(ShaderStageKind stage, std::uint32_t waveSize, std::uint32_t base = 0u, std::uint32_t fetchStart = 0u, std::uint32_t fetchCount = 0u) { ShaderComputeInputInfo compute{}; compute.threadIdsNum = 3; @@ -358,6 +409,19 @@ int main(int argc, char** argv) { return twice == single && single >= 3u; }); + passed &= run("a window with a few unproven relative reads keeps its unindexed slots out of the array", [] { + const auto few = ScanNarrowing(RecompileNarrowing(2)); + if (few.functionVariables <= 8u || few.switchCases == 0u) std::fprintf(stderr, " two unproven reads: %u function variables, %u switch cases\n", few.functionVariables, few.switchCases); + return few.functionVariables > 8u && few.switchCases != 0u; + }); + + passed &= run("a window with hundreds of unproven relative reads does not grow a switch for each of them", [] { + const auto many = ScanNarrowing(RecompileNarrowing(300)); + const auto few = ScanNarrowing(RecompileNarrowing(2)); + if (many.switchCases > 4096u || many.functionVariables >= few.functionVariables) std::fprintf(stderr, " 300 unproven reads: %u function variables, %u switch cases (2 reads: %u, %u)\n", many.functionVariables, many.switchCases, few.functionVariables, few.switchCases); + return many.switchCases <= 4096u && many.functionVariables < few.functionVariables; + }); + if (passed) std::puts("Vector window shape and SSA control tests passed"); return passed ? 0 : 1; } diff --git a/core/libs/prx/libSceAgcDriver/tests/VertexInput.cpp b/core/libs/prx/libSceAgcDriver/tests/VertexInput.cpp index 5d56b47f05..48d26f71df 100644 --- a/core/libs/prx/libSceAgcDriver/tests/VertexInput.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/VertexInput.cpp @@ -58,8 +58,36 @@ template void fails(TAction&& fn) { Require(failed, "invalid vertex input was accepted"); } +void descriptorDiagnostics(const Context& ctx) { + auto input = attribute(7); + input.resource.fields = {0x89abcde0u, 0x0010abcdu, 0x12345678u, 0x0104dfacu}; + Require(BuildVertexInputLayout(ctx, std::span(&input, 1)).attributes.size() == 1, "valid diagnostic control was rejected"); + struct Case { + unsigned field; + std::uint32_t bits; + const char* words; + }; + constexpr std::array cases{ + Case{1, 0x80000000u, "V#=[89abcde0 8010abcd 12345678 0104dfac]"}, + Case{3, 0x00800000u, "V#=[89abcde0 0010abcd 12345678 0184dfac]"}, + Case{3, 0x40000000u, "V#=[89abcde0 0010abcd 12345678 4104dfac]"}, + Case{3, 0x80000000u, "V#=[89abcde0 0010abcd 12345678 8104dfac]"}, + Case{3, 0xc0800000u, "V#=[89abcde0 0010abcd 12345678 c184dfac]"} + }; + for (const auto& value : cases) { + auto described = input; + described.resource.fields[value.field] |= value.bits; + const auto before = described.resource.fields; + const std::string message(VertexDescriptorFlagsMessage(described).data()); + Require(message.find("unsupported vertex buffer descriptor flags at location 7") != std::string::npos && message.find(value.words) != std::string::npos, + "vertex flag message did not identify the attribute and all four descriptor words: " + message); + Require(described.resource.fields == before, "formatting changed the descriptor"); + } +} + void run() { auto ctx = context(); + descriptorDiagnostics(ctx); VertexInputCache cache(ctx, 2); std::array attributes{attribute(0), attribute(1)}; auto first = get(cache, ctx, 1, attributes); diff --git a/core/libs/prx/libSceAgcDriver/tests/WriteTracking.cpp b/core/libs/prx/libSceAgcDriver/tests/WriteTracking.cpp index 267c945d64..af8efbc48e 100644 --- a/core/libs/prx/libSceAgcDriver/tests/WriteTracking.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/WriteTracking.cpp @@ -15,6 +15,7 @@ #ifdef __GLIBC__ #include #endif +#include #include #include #include @@ -35,6 +36,9 @@ constexpr std::size_t Block = 65536; #ifdef _WIN32 constexpr wchar_t HostHoleMode[] = L"--host-hole"; constexpr wchar_t ResidentRemapMode[] = L"--resident-direct-remap"; +constexpr wchar_t RewatchedSharedMode[] = L"--rewatched-direct-shared"; +constexpr wchar_t ImportAliasSharedMode[] = L"--import-alias-shared"; +constexpr wchar_t SharedGenerationsMode[] = L"--compared-shared-generations"; #endif void Require(bool condition, const std::string& message) { @@ -736,6 +740,243 @@ void CheckRewatch() { GuestArena::GuestArenaRelease_nid_postfix(memory, 4 * Block); } +void CheckRewatchedDirectMemoryComparedWhenShared() { + constexpr std::size_t bytes = 2 * Block; + auto* memory = static_cast(GuestArena::GuestArenaAllocate_nid_postfix(2 * bytes, Block)); + Require(memory != nullptr, "cannot reserve the compared sharing fixture"); + struct Cleanup { + std::uint8_t* memory; + HANDLE section = nullptr; + ~Cleanup() { + GuestArena::GuestArenaReset_nid_postfix(memory, 2 * bytes); + GuestArena::GuestArenaRelease_nid_postfix(memory, 2 * bytes); + if (section == nullptr) return; + GuestArena::GuestArenaForgetSection_nid_postfix(section); + CloseHandle(section); + } + } cleanup{memory}; + cleanup.section = CreateFileMappingW(INVALID_HANDLE_VALUE, nullptr, PAGE_EXECUTE_READWRITE, 0, bytes, nullptr); + Require(cleanup.section != nullptr, "cannot create the compared sharing backing"); + const auto base = reinterpret_cast(memory); + const auto native = [](const std::uint8_t* address) { + MEMORY_BASIC_INFORMATION info{}; + Require(VirtualQuery(address, &info, sizeof(info)) == sizeof(info), "cannot query the compared sharing fixture"); + return info; + }; + GuestArena::GuestArenaMapDirect_nid_postfix(memory, bytes, cleanup.section, 0, PAGE_READWRITE); + Require(native(memory).Type == MEM_PRIVATE && Watched(base, bytes), "the direct memory is not resident and watched"); + Unwatch(base, Block); + const std::array, 1> imported{{{base, base + Block}}}; + Rewatch(imported); + Require(Watched(base, Block), "the rewatched direct memory is not watched"); + BumpCollectEpoch(); + Require(CollectWritesUncached(base, Block) != 0, "rewatched resident direct memory is not collected"); + GuestArena::GuestArenaMapDirect_nid_postfix(memory + bytes, bytes, cleanup.section, 0, PAGE_READWRITE); + Require(native(memory).Type == MEM_MAPPED && native(memory + bytes).Type == MEM_MAPPED, "a second mapping kept the direct memory resident"); + BumpCollectEpoch(); + Require(CollectWritesUncached(base, Block) == 0 && CollectWritesUncached(base + bytes, Block) == 0, "rewatched direct memory was collected once shared"); + Require(native(memory).Protect == PAGE_READWRITE && native(memory + bytes).Protect == PAGE_READWRITE, "rewatched direct memory was armed once shared"); + memory[8] = 0x5a; + Require(memory[bytes + 8] == 0x5a, "the second mapping does not see a store into the first"); + Require(CollectWritesUncached(base + Block, Block) != 0, "shared direct memory that never left the watch stopped being collected"); + std::cout << "rewatched direct memory: compared once shared, not armed; other shared direct memory still collected\n"; +} + +struct ComparedSharing { + static constexpr std::size_t bytes = 2 * Block; + std::uint8_t* memory = static_cast(GuestArena::GuestArenaAllocate_nid_postfix(2 * bytes, Block)); + HANDLE section = CreateFileMappingW(INVALID_HANDLE_VALUE, nullptr, PAGE_EXECUTE_READWRITE, 0, bytes, nullptr); + std::uint64_t base = reinterpret_cast(memory); + + ComparedSharing() { + Require(memory != nullptr && section != nullptr, "cannot create the compared sharing generation fixture"); + GuestArena::GuestArenaForgetSection_nid_postfix(section); + GuestArena::GuestArenaMapDirect_nid_postfix(memory, bytes, section, 0, PAGE_READWRITE); + Require(Watched(base, bytes), "the compared sharing generation fixture is not watched"); + Unwatch(base, Block); + const std::array, 1> imported{{{base, base + Block}}}; + Rewatch(imported); + Require(Watched(base, Block), "the rewatched direct memory of the generation fixture is not watched"); + BumpCollectEpoch(); + Require(CollectWritesUncached(base, Block) != 0, "rewatched resident direct memory of the generation fixture is not collected"); + } + ~ComparedSharing() { + GuestArena::GuestArenaReset_nid_postfix(memory, 2 * bytes); + GuestArena::GuestArenaRelease_nid_postfix(memory, 2 * bytes); + GuestArena::GuestArenaForgetSection_nid_postfix(section); + CloseHandle(section); + } + ComparedSharing(const ComparedSharing&) = delete; + ComparedSharing& operator=(const ComparedSharing&) = delete; + + void Share() { + GuestArena::GuestArenaMapDirect_nid_postfix(memory + bytes, bytes, section, 0, PAGE_READWRITE); + MEMORY_BASIC_INFORMATION info{}; + Require(VirtualQuery(memory, &info, sizeof(info)) == sizeof(info) && info.Type == MEM_MAPPED, "a second mapping kept the generation fixture resident"); + } + std::uint64_t Collect(std::uint64_t address) { + BumpCollectEpoch(); + return CollectWrites(address, Block); + } +}; + +void CheckComparedSharingGenerations() { + { + ComparedSharing fixture; + const auto g0 = TrackerGeneration(); + Require(UnchangedSince(fixture.base, Block, g0) && !WrittenSince(fixture.base, Block, g0), "rewatched resident direct memory reads as written before any store"); + fixture.memory[16] = 0x21; + Require(fixture.Collect(fixture.base) != 0 && !UnchangedSince(fixture.base, Block, g0) && WrittenSince(fixture.base, Block, g0), "a store into rewatched resident direct memory was missed before it was shared"); + const auto cached = TrackerGeneration(); + Require(UnchangedSince(fixture.base, Block, cached), "rewatched resident direct memory reads as changed after its collect"); + fixture.Share(); + fixture.memory[8] = 0x5a; + Require(fixture.memory[ComparedSharing::bytes + 8] == 0x5a, "the second mapping does not see the store into the generation fixture"); + Require(fixture.Collect(fixture.base) == 0, "rewatched direct memory was collected once shared"); + Require(!UnchangedSince(fixture.base, Block, cached), "a store into rewatched direct memory once shared reads as unchanged since a generation before the sharing"); + Require(WrittenSince(fixture.base, Block, cached), "a store into rewatched direct memory once shared reads as not written since a generation before the sharing"); + } + { + ComparedSharing fixture; + const auto g0 = TrackerGeneration(); + fixture.Share(); + Require(fixture.Collect(fixture.base) == 0, "rewatched direct memory was collected once shared, before any store"); + Require(!UnchangedSince(fixture.base, Block, g0) && WrittenSince(fixture.base, Block, g0), "sharing rewatched direct memory did not report it written once"); + const auto g1 = TrackerGeneration(); + Require(fixture.Collect(fixture.base) == 0 && fixture.Collect(fixture.base) == 0, "rewatched direct memory was collected again once shared"); + std::array generations{g1}; + std::array changed{}; + std::array cpu{}; + Require(!WrittenSince(fixture.base, Block, g1) && ChangedBlocks(fixture.base, Block, generations, changed, cpu) && changed[0] == BlockUnchanged && cpu[0] == 0, "refused collects of shared compared memory reported it written again without a store"); + Require(!UnchangedSince(fixture.base, Block, g1), "shared compared memory read as unchanged since a generation taken after a refused collect"); + const auto neighbour = fixture.Collect(fixture.base + Block); + const auto spanned = std::max(fixture.Collect(fixture.base), neighbour); + Require(spanned != 0 && UnchangedSince(fixture.base + Block, Block, spanned), "shared direct memory that never left the watch is not collected beside compared memory"); + fixture.memory[24] = 0x3c; + Require(fixture.Collect(fixture.base) == 0, "rewatched direct memory was collected once shared, after a store"); + Require(!UnchangedSince(fixture.base, Block, g1), "a store into shared compared memory reads as unchanged since a generation taken after a refused collect"); + Require(!UnchangedSince(fixture.base, Block, spanned), "a store into shared compared memory reads as unchanged since the newest generation of a collect over several ranges"); + Require(fixture.Collect(fixture.base + Block) != 0 && UnchangedSince(fixture.base + Block, Block, spanned), "a refused collect of compared memory made its never-compared neighbour read as changed"); + const std::array queries{{{fixture.base + Block, Block, spanned}, {fixture.base, Block, spanned}}}; + Require(!UnchangedSinceAll(queries), "a batch of queries over shared compared memory read as unchanged"); + const std::array, 1> committed{{{fixture.base, fixture.base + Block}}}; + Require(!UnchangedCommittedSince(committed, spanned), "committed ranges over shared compared memory read as unchanged"); + } + std::cout << "shared compared memory: reported written once at the sharing, never unchanged since an older generation, its neighbour still trusted\n"; +} + +void CheckImportedDirectMemoryComparedWhenShared() { + constexpr std::size_t bytes = 2 * Block; + auto* memory = static_cast(GuestArena::GuestArenaAllocate_nid_postfix(2 * bytes, Block)); + Require(memory != nullptr, "cannot reserve the imported sharing fixture"); + struct Cleanup { + std::uint8_t* memory; + HANDLE section = nullptr; + ~Cleanup() { + GuestArena::GuestArenaReset_nid_postfix(memory, 2 * bytes); + GuestArena::GuestArenaRelease_nid_postfix(memory, 2 * bytes); + if (section == nullptr) return; + GuestArena::GuestArenaForgetSection_nid_postfix(section); + CloseHandle(section); + } + } cleanup{memory}; + cleanup.section = CreateFileMappingW(INVALID_HANDLE_VALUE, nullptr, PAGE_EXECUTE_READWRITE, 0, bytes, nullptr); + Require(cleanup.section != nullptr, "cannot create the imported sharing backing"); + const auto base = reinterpret_cast(memory); + const auto native = [](const std::uint8_t* address) { + MEMORY_BASIC_INFORMATION info{}; + Require(VirtualQuery(address, &info, sizeof(info)) == sizeof(info), "cannot query the imported sharing fixture"); + return info; + }; + GuestArena::GuestArenaMapDirect_nid_postfix(memory, bytes, cleanup.section, 0, PAGE_READWRITE); + Require(native(memory).Type == MEM_PRIVATE && Watched(base, bytes), "the imported direct memory is not resident and watched"); + Require(ImportWatched(base, Block, [] { return true; }), "the watched import of resident direct memory failed"); + Require(Watched(base, Block), "a watched import of resident direct memory left the write watch"); + BumpCollectEpoch(); + Require(CollectWritesUncached(base, Block) != 0, "imported resident direct memory is not collected"); + GuestArena::GuestArenaMapDirect_nid_postfix(memory + bytes, bytes, cleanup.section, 0, PAGE_READWRITE); + Require(native(memory).Type == MEM_MAPPED && native(memory + bytes).Type == MEM_MAPPED, "a second mapping kept the imported direct memory resident"); + BumpCollectEpoch(); + Require(CollectWritesUncached(base, Block) == 0 && CollectWritesUncached(base + bytes, Block) == 0, "imported direct memory was collected once shared"); + Require(native(memory).Protect == PAGE_READWRITE && native(memory + bytes).Protect == PAGE_READWRITE, "imported direct memory was armed once shared"); + memory[8] = 0x5a; + Require(memory[bytes + 8] == 0x5a, "the second mapping does not see a store into the imported first"); + Require(CollectWritesUncached(base + Block, Block) != 0, "shared direct memory that was never imported stopped being collected"); + std::cout << "imported direct memory: compared once shared, not armed; unimported shared direct memory still collected\n"; +} + +void CheckImportAliasDemotedComparedWhenShared() { + constexpr std::size_t page = 0x4000; + auto* memory = static_cast(GuestArena::GuestArenaAllocate_nid_postfix(5 * Block, Block)); + Require(memory != nullptr, "cannot reserve the import alias fixture"); + struct Cleanup { + std::uint8_t* memory; + std::array sections{}; + std::array aliases{}; + ~Cleanup() { + for (const auto alias : aliases) GuestArena::GuestArenaUnmapAlias_nid_postfix(alias); + GuestArena::GuestArenaReset_nid_postfix(memory, 5 * Block); + GuestArena::GuestArenaRelease_nid_postfix(memory, 5 * Block); + for (const auto section : sections) { + if (section == nullptr) continue; + GuestArena::GuestArenaForgetSection_nid_postfix(section); + CloseHandle(section); + } + } + } cleanup{memory}; + for (auto& section : cleanup.sections) { + section = CreateFileMappingW(INVALID_HANDLE_VALUE, nullptr, PAGE_EXECUTE_READWRITE, 0, Block, nullptr); + Require(section != nullptr, "cannot create the import alias backing"); + GuestArena::GuestArenaForgetSection_nid_postfix(section); + } + const auto base = reinterpret_cast(memory); + const auto native = [](const std::uint8_t* address) { + MEMORY_BASIC_INFORMATION info{}; + Require(VirtualQuery(address, &info, sizeof(info)) == sizeof(info), "cannot query the import alias fixture"); + return info; + }; + const auto clearingCollect = [](std::uint64_t address) { + std::array pages{}; + std::size_t count = pages.size(); + return GuestArena::GuestArenaCollectWrites_nid_postfix(static_cast(address), page, pages.data(), &count, true); + }; + GuestArena::GuestArenaMapDirect_nid_postfix(memory, page, cleanup.sections[0], 0, PAGE_READWRITE); + Require(native(memory).Type == MEM_PRIVATE && Watched(base, page), "the aliased direct memory is not resident and watched"); + BumpCollectEpoch(); + Require(CollectWritesUncached(base, page) != 0, "resident direct memory is not collected before its import"); + cleanup.aliases[0] = GuestArena::GuestArenaMapAlias_nid_postfix(static_cast(base), page); + Require(cleanup.aliases[0] != nullptr && native(memory).Type == MEM_MAPPED, "the import alias did not demote the resident run"); + Require(ImportWatched(base, page, [] { return true; }) && Watched(base, page), "the import of aliased direct memory did not stay watched"); + Require(native(memory).Protect == PAGE_READWRITE, "memory an import's alias demoted was armed by the import"); + Require(!clearingCollect(base), "a clearing collect over memory an import's alias demoted was not refused"); + Require(native(memory).Protect == PAGE_READWRITE, "memory an import's alias demoted was armed by a clearing collect"); + BumpCollectEpoch(); + Require(CollectWritesUncached(base, page) == 0, "memory an import's alias demoted was collected once shared"); + memory[8] = 0x5a; + Require(static_cast(cleanup.aliases[0])[8] == 0x5a, "the import alias does not see a store into the guest view"); + + GuestArena::GuestArenaMapDirect_nid_postfix(memory + Block, page, cleanup.sections[1], 0, PAGE_READWRITE); + GuestArena::GuestArenaMapDirect_nid_postfix(memory + 2 * Block, page, cleanup.sections[1], 0, PAGE_READWRITE); + Require(native(memory + Block).Type == MEM_MAPPED && native(memory + 2 * Block).Type == MEM_MAPPED, "a second mapping kept the control direct memory resident"); + cleanup.aliases[1] = GuestArena::GuestArenaMapAlias_nid_postfix(static_cast(base + Block), page); + Require(cleanup.aliases[1] != nullptr, "the control import alias was refused"); + Require(ImportWatched(base + Block, page, [] { return true; }) && Watched(base + Block, page), "the control import did not stay watched"); + Require(clearingCollect(base + Block), "memory shared by a second mapping before its import alias stopped being collected"); + Require(native(memory + Block).Protect == PAGE_READONLY && native(memory + 2 * Block).Protect == PAGE_READONLY, "memory shared by a second mapping before its import alias was not armed"); + + GuestArena::GuestArenaMapDirect_nid_postfix(memory + 3 * Block, page, cleanup.sections[2], 0, PAGE_READWRITE); + auto* failed = GuestArena::GuestArenaMapAlias_nid_postfix(static_cast(base + 3 * Block), page); + Require(failed != nullptr && native(memory + 3 * Block).Type == MEM_MAPPED, "the alias of a failed import did not demote the resident run"); + GuestArena::GuestArenaUnmapAlias_nid_postfix(failed); + GuestArena::GuestArenaMapDirect_nid_postfix(memory + 4 * Block, page, cleanup.sections[2], 0, PAGE_READWRITE); + cleanup.aliases[2] = GuestArena::GuestArenaMapAlias_nid_postfix(static_cast(base + 3 * Block), page); + Require(cleanup.aliases[2] != nullptr, "the import alias after a second mapping was refused"); + Require(ImportWatched(base + 3 * Block, page, [] { return true; }) && Watched(base + 3 * Block, page), "the import after a second mapping did not stay watched"); + Require(clearingCollect(base + 3 * Block), "memory a second mapping shared after an earlier alias demoted it stopped being collected"); + Require(native(memory + 3 * Block).Protect == PAGE_READONLY && native(memory + 4 * Block).Protect == PAGE_READONLY, "memory a second mapping shared after an earlier alias demoted it was not armed"); + std::cout << "direct memory an import's alias demoted: compared once shared, not armed; memory shared before the alias still collected\n"; +} + void CheckResidentDirectMappingReuse() { constexpr std::size_t bytes = 5 * Block; auto* memory = static_cast(GuestArena::GuestArenaAllocate_nid_postfix(bytes, Block)); @@ -1532,6 +1773,19 @@ int main() { CheckResidentDirectMappingReuse(); return 0; } + if (std::wstring_view(GetCommandLineW()).ends_with(RewatchedSharedMode)) { + CheckRewatchedDirectMemoryComparedWhenShared(); + CheckImportedDirectMemoryComparedWhenShared(); + return 0; + } + if (std::wstring_view(GetCommandLineW()).ends_with(ImportAliasSharedMode)) { + CheckImportAliasDemotedComparedWhenShared(); + return 0; + } + if (std::wstring_view(GetCommandLineW()).ends_with(SharedGenerationsMode)) { + CheckComparedSharingGenerations(); + return 0; + } #endif CheckSharedBlock(); CheckStoredOverUsesPageEvidence(); diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/ColorLogicOp.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/ColorLogicOp.cpp new file mode 100644 index 0000000000..89963aafb1 --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/tests/execution/ColorLogicOp.cpp @@ -0,0 +1,155 @@ +#include "prx/libSceAgcDriver/Execution/include/VulkanDevice.hpp" +#include "prx/libSceAgcDriver/Graphics/include/Draw.hpp" +#include "Recompiler.hpp" +#include "VulkanTestDevice.hpp" +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace { + +using AgcDriver::Graphics::Require; +using ShaderRecompiler::ShaderStage; + +constexpr std::uint32_t Width = 64; +constexpr std::uint32_t Height = 16; +constexpr std::uint32_t WaveSize = 64; +alignas(256) std::array Pixels{}; + +alignas(256) constexpr std::array VertexCode{ + 0xe0382000, 0x80000005, 0xbf8c3f70, 0xf80008cf, 0x03020100, 0xbf810000, +}; + +alignas(256) constexpr std::array PixelCode{ + 0x7e0802ff, 0x3e4ccccd, 0x7e0a02ff, 0x3ecccccd, 0x7e0c02ff, 0x3f19999a, 0x7e0e02ff, 0x3f4ccccd, + 0xf800180f, 0x07060504, 0xbf810000, +}; + +constexpr std::array Source{0x33, 0x66, 0x99, 0xcc}; + +constexpr std::array, 3> Vertices{{ + {-1.0f, -1.0f, 0.5f, 1.0f}, {3.0f, -1.0f, 0.5f, 1.0f}, {-1.0f, 3.0f, 0.5f, 1.0f} +}}; +alignas(256) std::array, 3> VertexStorage = Vertices; +std::array Before{}; + + +std::array BufferDescriptor(const void* data, std::uint32_t stride, std::uint32_t count) { + const auto address = reinterpret_cast(data); + return {static_cast(address), static_cast((address >> 32u) & 0xffffu) | (stride << 16u), count, 0x01016facu}; +} + +void Draw(AgcDriver::VulkanDevice& device, std::uint8_t rop) { + for (std::size_t index = 0; index < Pixels.size(); ++index) Before[index] = Pixels[index] = static_cast((index * 37u + 11u) & 0xffu); + const auto target = device.Target(); + + std::vector vertexUserData(4, 0u); + const auto vertexBuffer = BufferDescriptor(VertexStorage.data(), 16u, static_cast(VertexStorage.size())); + std::copy(vertexBuffer.begin(), vertexBuffer.end(), vertexUserData.begin()); + const std::array vertexMemory{{{reinterpret_cast(VertexCode.data()), std::as_bytes(std::span(VertexCode))}}}; + ShaderRecompiler::RecompileRequest vertex{ + {ShaderStage::Vertex, reinterpret_cast(VertexCode.data()), VertexCode, 0, {}}, + {WaveSize, 0, vertexUserData, std::nullopt, std::nullopt, ShaderRecompiler::ShaderVertexStageInfo{}, vertexMemory}, + target, + {0, 0, 0, 64} + }; + vertex.useCache = false; + const auto vertexResult = ShaderRecompiler::Recompile(vertex); + const auto vertexPush = static_cast(vertexResult.pushConstants.size()); + + ShaderRecompiler::ShaderPixelStageInfo pixel{}; + pixel.inputAddr = ShaderRecompiler::PixelInputBit(ShaderRecompiler::PixelInput::PositionX) | ShaderRecompiler::PixelInputBit(ShaderRecompiler::PixelInput::PositionY); + pixel.posX = true; + pixel.posY = true; + pixel.targetOutputMode[0] = 9; + pixel.targetExportMapping.fill(0xe4u); + const std::vector pixelUserData(8, 0u); + const std::array pixelMemory{{{reinterpret_cast(PixelCode.data()), std::as_bytes(std::span(PixelCode))}}}; + ShaderRecompiler::RecompileRequest fragment{ + {ShaderStage::Fragment, reinterpret_cast(PixelCode.data()), PixelCode, 0, {}}, + {WaveSize, 0, pixelUserData, std::nullopt, pixel, std::nullopt, pixelMemory}, + target, + PixelPushLayout(vertexPush, target) + }; + fragment.useCache = false; + const auto pixelResult = ShaderRecompiler::Recompile(fragment); + const std::array shaders{{ + {ShaderStage::Vertex, &vertexResult, 0}, + {ShaderStage::Fragment, &pixelResult, PixelPushOffset(vertexPush, target)} + }}; + + AgcDriver::Graphics::State state{}; + state.stages = {AgcDriver::Graphics::ShaderPath::Vertex, 0u, WaveSize, WaveSize, std::nullopt, std::nullopt}; + state.color = {reinterpret_cast(Pixels.data()), {Width, Height}, VK_FORMAT_R8G8B8A8_UNORM, Pixels.size(), 0xe4u}; + state.colors = {state.color}; + state.hasColorTarget = true; + state.renderExtent = {Width, Height}; + state.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST; + state.viewport = {0, static_cast(Height), static_cast(Width), -static_cast(Height), 0, 1}; + state.negativeOneToOne = false; + state.colorRop = rop; + state.scissor = {{0, 0}, {Width, Height}}; + state.cullMode = VK_CULL_MODE_NONE; + state.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE; + state.blend.colorWriteMask = 15; + state.blends = {state.blend}; + state.blendConstants = {}; + const AgcDriver::Pm4::DrawParameters draw{0, static_cast(VertexStorage.size()), 0, 1, 0, false}; + device.Draw(state, draw, shaders); + device.WaitIdle(); +} + +std::uint8_t Combine(std::uint8_t rop, std::uint8_t destination, std::uint8_t source) { + switch (rop) { + case 0xcc: return source; + case 0x5a: return static_cast(destination ^ source); + case 0x88: return static_cast(destination & source); + case 0xee: return static_cast(destination | source); + default: throw std::runtime_error("the test has no expectation for this ROP"); + } +} + +void Check(std::uint8_t rop, std::string_view what) { + for (std::uint32_t y = 0; y < Height; ++y) { + for (std::uint32_t x = 0; x < Width; ++x) { + for (std::uint32_t channel = 0; channel < 4; ++channel) { + const auto index = (y * Width + x) * 4u + channel; + const auto expected = Combine(rop, static_cast(Before[index]), Source[channel]); + const auto actual = static_cast(Pixels[index]); + Require(actual == expected, std::string(what) + ": pixel (" + std::to_string(x) + ", " + std::to_string(y) + ") channel " + std::to_string(channel) + " is " + std::to_string(actual) + ", expected " + std::to_string(expected)); + } + } + } +} + +} + +int main() { + try { + const auto device = OpenVulkanTestDevice(); + if (!device) return VulkanTestSkipped; + for (const std::uint8_t rop : {0xcc, 0x5a, 0x88, 0xee, 0xcc}) { + try { + Draw(*device, rop); + } catch (const std::exception& error) { + Require(std::string_view(error.what()).find("device does not support color logic operations") != std::string_view::npos, std::string("unexpected rejection: ") + error.what()); + std::puts("skipped, the device has no logicOp feature"); + return VulkanTestSkipped; + } + char what[48]; + std::snprintf(what, sizeof(what), "ROP3 0x%02x into an 8_8_8_8 unorm target", rop); + Check(rop, what); + } + std::puts("color logic operation tests passed"); + return 0; + } catch (const std::exception& error) { + std::cerr << error.what() << '\n'; + return 1; + } +} diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/DepthCopyDraw.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/DepthCopyDraw.cpp new file mode 100644 index 0000000000..b7c8199b67 --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/tests/execution/DepthCopyDraw.cpp @@ -0,0 +1,166 @@ +#include "prx/libSceAgcDriver/Execution/include/Driver.hpp" +#include "prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp" +#include "prx/libSceAgcDriver/Execution/include/GuestMemory.hpp" +#include "prx/libSceAgcDriver/Submit/include/Dcb.hpp" +#include "VulkanTestDevice.hpp" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace { + +using AgcDriver::Graphics::Require; + +constexpr std::uint32_t Width = 64; +constexpr std::uint32_t Height = 4; +constexpr float DrawnDepth = 0.25f; +constexpr float MemoryDepth = 0.75f; +constexpr float Untouched = -1.0f; + +alignas(256) constexpr std::array VertexCode{ + 0x7e000d05u, 0x080200f4u, 0x10020300u, 0x100202f4u, 0x060202f3u, 0x060400f3u, 0x10040500u, 0x060404f3u, + 0x7e060280u, 0x7e0802f2u, 0xf80008cfu, 0x04030201u, 0xbf810000u, 0xbf810000u, 0xbf810000u, 0xbf810000u, +}; +alignas(256) constexpr std::array WhitePixelCode{0x7e0e02f2u, 0xf800180fu, 0x07070707u, 0xbf810000u}; +alignas(256) std::array CopyTarget{}; +alignas(256) std::array ReferenceTarget{}; +constexpr std::size_t DepthFloats = 16384; +constexpr std::size_t DepthAlignment = 65536; +std::array DepthStorage{}; +const std::span DepthMemory{reinterpret_cast((reinterpret_cast(DepthStorage.data()) + DepthAlignment - 1) & ~(DepthAlignment - 1)), DepthFloats}; + +struct Header { + Shader shader{}; + ShaderUserData userData{}; + std::array registers{}; +}; + +alignas(8) Header VertexHeader{}; +alignas(8) Header PixelHeader{}; + +void Registered(Header& header, const std::uint32_t* code, std::size_t bytes, std::uint8_t type) { + header.shader.file_header = 0x34333231u; + header.shader.version = 0x18u; + header.shader.header_size = sizeof(Header); + header.shader.shader_size = static_cast(bytes); + header.shader.code = code; + header.shader.type = type; + header.shader.user_data = &header.userData; + if (type == 1) { + const auto address = reinterpret_cast(code); + header.registers = {{{0x008, static_cast(address >> 8u)}, {0x009, static_cast(address >> 40u)}, {0x00b, 0}, {0x00a, 0}}}; + header.shader.sh_registers = header.registers.data(); + header.shader.num_sh_registers = header.registers.size(); + } + AgcDriverRegisterShader_nid_postfix(&header.shader); +} + +void Append(std::vector& commands, std::uint32_t opcode, std::initializer_list payload) { + commands.push_back(0xc0000000u | (static_cast(payload.size() - 1) << 16u) | (opcode << 8u)); + commands.insert(commands.end(), payload); +} + +struct DrawOptions { + float* color = nullptr; + std::uint32_t targetMask = 1; + std::uint32_t renderControl = 0; + std::uint32_t depthControl = 0; +}; + +void AppendDraw(std::vector& commands, const DrawOptions& options) { + const auto vertex = reinterpret_cast(VertexCode.data()); + const auto pixel = reinterpret_cast(WhitePixelCode.data()); + const auto color = reinterpret_cast(options.color); + const auto depth = reinterpret_cast(DepthMemory.data()); + Require(depth % DepthAlignment == 0, "the test depth plane is not 64 KiB aligned"); + const std::vector> context{ + {0x2d5, 0x2000}, + {0x1b3, 0x2}, {0x1b4, 0x2}, {0x1b6, 0}, {0x207, 0}, {0x200, options.depthControl}, {0x203, 0}, + {0x2dc, 0xaa00}, {0x2f8, 0}, {0x293, 0}, + {0x80, 0}, {0x8d, 0}, {0x83, 0xffff}, {0x8c, 0xa}, + {0x2f9, 0x2d}, {0x313, 0x6000}, {0x30e, 0xffffffff}, {0x30f, 0xffffffff}, + {0x206, 0x43f}, {0x204, 0x80000}, {0x205, 0x240}, + {0x8e, options.targetMask}, {0x8f, 0xf}, {0x202, 0xcc0010}, + {0x1c4, 0}, {0x1c5, 1}, {0x1c3, 4}, {0x31c, 0x8710}, + {0x31b, 0}, {0x31d, 0}, {0x3b0, ((Width - 1u) << 14u) | (Height - 1u)}, + {0x3b8, 0x9000000}, {0x1e0, 0}, + {0xc, 0}, {0xd, (Height << 16u) | Width}, + {0x81, 0x80000000}, {0x82, (Height << 16u) | Width}, + {0x90, 0x80000000}, {0x91, (Height << 16u) | Width}, + {0x94, 0x80000000}, {0x95, (Height << 16u) | Width}, + {0x10f, std::bit_cast(Width / 2.0f)}, {0x110, std::bit_cast(Width / 2.0f)}, + {0x111, std::bit_cast(-(Height / 2.0f))}, {0x112, std::bit_cast(Height / 2.0f)}, + {0x113, std::bit_cast(1.0f)}, {0x114, std::bit_cast(DrawnDepth)}, + {0xb4, 0}, {0xb5, std::bit_cast(1.0f)}, + {0x2fe, 0}, {0x302, 0}, {0x306, 0}, {0x30a, 0}, + {0x318, static_cast(color >> 8u)}, {0x390, static_cast(color >> 40u)}, {0x292, 2}, + {0x000, options.renderControl}, {0x002, 0}, {0x007, ((Height - 1u) << 16u) | (Width - 1u)}, + {0x008, 0}, {0x009, std::bit_cast(1.0f)}, {0x00a, 0}, {0x00b, 0}, + {0x010, 3}, {0x011, 0}, + {0x012, static_cast(depth >> 8u)}, {0x014, static_cast(depth >> 8u)}, + {0x01a, static_cast(depth >> 40u)}, {0x01c, static_cast(depth >> 40u)}, + }; + const std::vector> shader{ + {0xc8, static_cast(vertex >> 8u)}, {0xc9, static_cast(vertex >> 40u)}, {0x8b, 1u << 1u}, {0x8c, 0}, + {0x008, static_cast(pixel >> 8u)}, {0x009, static_cast(pixel >> 40u)}, {0x00a, 0}, {0x00b, 0}, + }; + Append(commands, 0x79, {0x242, 4}); + for (const auto& [offset, value] : context) Append(commands, 0x69, {offset, value}); + for (const auto& [offset, value] : shader) Append(commands, 0x76, {offset, value}); + Append(commands, 0x2d, {3, 2}); +} + +void CopyCoversTheDraw() { + CopyTarget.fill(Untouched); + ReferenceTarget.fill(Untouched); + std::ranges::fill(DepthMemory, MemoryDepth); + Registered(VertexHeader, VertexCode.data(), sizeof(VertexCode), 2); + Registered(PixelHeader, WhitePixelCode.data(), sizeof(WhitePixelCode), 1); + std::vector commands; + AppendDraw(commands, {.color = CopyTarget.data(), .targetMask = 0, .depthControl = 0x76}); + AppendDraw(commands, {.color = CopyTarget.data(), .renderControl = 0x84}); + AppendDraw(commands, {.color = ReferenceTarget.data()}); + Packet packet{commands.data(), static_cast(commands.size()), 0, {}}; + Require(sceAgcDriverSubmitDcb(&packet) == 0, "the depth write, DB depth copy and reference draws were not submitted"); + AgcDriverWaitIdle_nid_postfix(); + AgcDriver::GuestMemory::FlushGpuWrites(reinterpret_cast(CopyTarget.data()), sizeof(CopyTarget)); + AgcDriver::GuestMemory::FlushGpuWrites(reinterpret_cast(ReferenceTarget.data()), sizeof(ReferenceTarget)); + std::size_t covered = 0; + for (std::size_t index = 0; index < CopyTarget.size(); ++index) { + const bool drawn = ReferenceTarget[index] == 1.0f; + Require(drawn || ReferenceTarget[index] == Untouched, "the reference draw wrote " + std::to_string(ReferenceTarget[index]) + " at texel " + std::to_string(index)); + covered += drawn; + const auto expected = drawn ? DrawnDepth : Untouched; + Require(CopyTarget[index] == expected, "the DB depth copy holds " + std::to_string(CopyTarget[index]) + " at texel " + std::to_string(index) + ", expected " + std::to_string(expected) + (drawn ? " (the triangle covers it)" : " (outside the triangle)")); + } + Require(covered != 0 && covered != CopyTarget.size(), "the triangle covered " + std::to_string(covered) + " of " + std::to_string(CopyTarget.size()) + " texels, expected an edge through the target"); +} + +} + +int main() { + try { + { + const auto device = OpenVulkanTestDevice(); + if (!device) return VulkanTestSkipped; + } + CopyCoversTheDraw(); + AgcDriverShutdown_nid_postfix(); + std::puts("the DB depth copy wrote the drawn depth exactly where its draw rasterized"); + return 0; + } catch (const std::exception& error) { + std::cerr << error.what() << '\n'; + AgcDriverShutdown_nid_postfix(); + return 1; + } +} diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/DepthPlaneGuestLayers.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/DepthPlaneGuestLayers.cpp index cdb94c5363..4145b544b5 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/DepthPlaneGuestLayers.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/DepthPlaneGuestLayers.cpp @@ -7,6 +7,7 @@ #include "prx/libc/include/GuestAllocations.hpp" #include "prx/libc/include/GuestArena.hpp" #include "prx/libc/include/GuestWriteWatch.hpp" +#include "FixtureDeviceSelection.hpp" #include #ifdef _WIN32 #ifndef NOMINMAX @@ -75,6 +76,8 @@ public: Device() { #ifdef _WIN32 library = SDL_LoadObject("vulkan-1.dll"); +#elif defined(__APPLE__) + library = SDL_LoadObject("libvulkan.1.dylib"); #else library = SDL_LoadObject("libvulkan.so.1"); #endif @@ -86,6 +89,12 @@ public: application.apiVersion = VK_API_VERSION_1_1; VkInstanceCreateInfo info{VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO}; info.pApplicationInfo = &application; +#ifdef __APPLE__ + const char* portability = VK_KHR_PORTABILITY_ENUMERATION_EXTENSION_NAME; + info.flags = VK_INSTANCE_CREATE_ENUMERATE_PORTABILITY_BIT_KHR; + info.enabledExtensionCount = 1; + info.ppEnabledExtensionNames = &portability; +#endif Check(function("vkCreateInstance")(&info, nullptr, &instance), "vkCreateInstance"); std::uint32_t count = 0; const auto enumerate = function("vkEnumeratePhysicalDevices"); @@ -93,7 +102,7 @@ public: Require(count != 0, "no Vulkan device"); std::vector devices(count); Check(enumerate(instance, &count, devices.data()), "vkEnumeratePhysicalDevices"); - context.physical = devices.front(); + context.physical = SelectFixturePhysicalDevice(devices, function("vkGetPhysicalDeviceProperties")); const auto extensions = function("vkEnumerateDeviceExtensionProperties"); Check(extensions(context.physical, nullptr, &count, nullptr), "vkEnumerateDeviceExtensionProperties"); std::vector available(count); diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/DepthPlanePartialStore.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/DepthPlanePartialStore.cpp index 43b8815b56..8bcdfb60dc 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/DepthPlanePartialStore.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/DepthPlanePartialStore.cpp @@ -2,6 +2,9 @@ #include "prx/libSceAgcDriver/Execution/include/VulkanDevice.hpp" #include "prx/libSceAgcDriver/Graphics/include/DepthSurface.hpp" #include "prx/libSceAgcDriver/Graphics/include/GuestTextureResource.hpp" +#include "prx/libSceAgcDriver/Graphics/include/ShaderResources.hpp" +#include "prx/libSceAgcDriver/Graphics/include/Texture.hpp" +#include "prx/libSceAgcDriver/Graphics/include/TextureTiling.hpp" #include "prx/libc/include/GuestAllocations.hpp" #include "prx/libc/include/GuestArena.hpp" #include "prx/libc/include/GuestWriteWatch.hpp" @@ -225,6 +228,62 @@ int main() { for (std::size_t unit = 1; unit < Units; ++unit) RequireUnit(loose, unit, 1.0f, UnitTexels, "unwatched plane after a read of the first unit, a unit it does not overlap"); Require(draw(unwatched, {.minDepth = 0.6f, .maxDepth = 0.6f}) == 0u, "a draw at depth 0.6 passed LESS over unwatched units still holding depth 0.5 on the GPU"); } + + auto* arrayBlocks = static_cast(AllocateBlocks(3u * PlaneBytes, true)); + auto* layerPlane = reinterpret_cast(arrayBlocks + PlaneBytes); + AgcDriver::Graphics::GuestTextureResource array{}; + array.baseAddress = reinterpret_cast(arrayBlocks); + array.width = PlaneWidth; + array.height = PlaneHeight; + array.depthOrLastArray = 2; + array.mipCount = 1; + array.tileMode = AgcDriver::Graphics::TextureTileMode::kR64KBX; + array.dimension = AgcDriver::Graphics::TextureDimension::k2DArray; + array.format = 22; + array.dstSelX = 4; + array.dstSelY = 5; + array.dstSelZ = 6; + array.dstSelW = 7; + Require(AgcDriver::Graphics::DescribeSurface(array).layerBytes == PlaneBytes, "a layer of the 384x200 R32 array is not six 64 KiB tiles"); + const auto context = AgcDriver::VulkanDeviceTestAccess::ContextOf(*device); + const auto lookup = [&](std::uint32_t first, std::uint32_t count) { + std::lock_guard gpu(AgcDriver::GuestMemory::GpuMutex()); + return AgcDriver::Graphics::CachedStorageLayers(context, array, first, count); + }; + Fill(layerPlane, 1.0f); + const auto arrayImage = lookup(0, 1); + device->WaitIdle(); + const auto provedBefore = AgcDriver::Graphics::StorageTexture::RefreshesProved(); + Require(lookup(0, 1) == arrayImage && lookup(0, 1) == arrayImage, "a lookup of the first layer of an unchanged array replaced its cached image"); + if (!AgcDriver::GuestMemory::WriteWatched() || !AgcDriver::GuestMemory::Watched(array.baseAddress, 3u * PlaneBytes)) { + std::puts("guest memory is not write-watched here: lookups of array layers through a refresh proof are not exercised"); + } else { + Require(AgcDriver::Graphics::StorageTexture::RefreshesProved() != provedBefore, "repeated lookups of the first layer of an unchanged array proved no refresh"); + } + const Surface layerDepth{reinterpret_cast(layerPlane), 0, 0, false, VK_FORMAT_D32_SFLOAT, 0, 0, swizzle}; + Require(draw(layerDepth, writeHalf) == PlaneTexels, "the draw writing depth 0.5 over the second layer of an array did not cover the plane"); + before = AgcDriver::Graphics::DepthPlaneStoreCounts(); + Require(lookup(0, 1) == arrayImage, "a depth draw over the second layer of an array replaced the array's cached image"); + device->WaitIdle(); + stored = Since(before); + Require(stored.stores == 0, "a lookup of the first layer of an array stored the depth plane over its second layer"); + for (std::size_t unit = 0; unit < Units; ++unit) RequireUnit(layerPlane, unit, 1.0f, UnitTexels, "a lookup of the first layer of an array, the depth plane over its second layer"); + Require(AgcDriver::Graphics::DepthPlanesStale(), "a lookup of the first layer of an array left no stale plane"); + + before = AgcDriver::Graphics::DepthPlaneStoreCounts(); + static_cast(lookup(0, 2)); + device->WaitIdle(); + stored = Since(before); + Require(stored.stores == 1 && stored.units == Units, "a lookup of the first two layers of a three-layer array did not store the depth plane over its second layer"); + for (std::size_t unit = 0; unit < Units; ++unit) RequireUnit(layerPlane, unit, 0.5f, Texels(unit), "a lookup of the first two layers of a three-layer array, the depth plane over its second layer"); + + Require(draw(layerDepth, {.depthWrite = true, .depthCompare = VK_COMPARE_OP_ALWAYS, .minDepth = 0.25f, .maxDepth = 0.25f}) == PlaneTexels, "the draw writing depth 0.25 over the second layer of an array did not cover the plane"); + before = AgcDriver::Graphics::DepthPlaneStoreCounts(); + static_cast(lookup(0, 3)); + device->WaitIdle(); + stored = Since(before); + Require(stored.stores == 1 && stored.units == Units, "a lookup of every layer of an array did not store the depth plane over its second layer"); + for (std::size_t unit = 0; unit < Units; ++unit) RequireUnit(layerPlane, unit, 0.25f, Texels(unit), "a lookup of every layer of an array, the depth plane over its second layer"); std::puts("depth plane partial store tests passed"); return 0; } catch (const std::exception& error) { diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/DepthPlaneSampling.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/DepthPlaneSampling.cpp index 33c6cfdb6b..f77432e092 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/DepthPlaneSampling.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/DepthPlaneSampling.cpp @@ -434,8 +434,9 @@ void RefusalCase(AgcDriver::VulkanDevice& device, GuestBlock& block) { } } -int main() { +int main(int argc, char** argv) { try { + const bool evicting = argc > 1 && std::string(argv[1]) == "--evicting"; GuestBlock block; const auto device = OpenVulkanTestDevice(); if (!device) return VulkanTestSkipped; @@ -452,6 +453,10 @@ int main() { PaddedD16CubeCase(*device, block, output); BaseSliceCase(*device, block, output); RefusalCase(*device, block); + if (evicting) { + const auto kept = AgcDriver::Graphics::DepthPlaneCopyCount(); + Require(kept >= 1 && kept < 8, std::to_string(kept) + " depth plane copies were kept with a 1 MiB budget, expected at least the newest and fewer than the 8 an unbounded cache keeps"); + } std::puts("depth plane sampling tests passed"); return 0; } catch (const std::exception& error) { diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/DepthStorage.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/DepthStorage.cpp index 6752099605..0c706f407d 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/DepthStorage.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/DepthStorage.cpp @@ -1,15 +1,13 @@ #include "prx/libSceAgcDriver/Execution/include/GuestMemory.hpp" #include "prx/libSceAgcDriver/Execution/include/BdaFeatures.hpp" -#include "prx/libSceAgcDriver/Graphics/include/ColorTargetLayout.hpp" #include "prx/libSceAgcDriver/Graphics/include/DepthSurface.hpp" -#include "prx/libSceAgcDriver/Graphics/include/Draw.hpp" -#include "prx/libSceAgcDriver/Graphics/include/ShaderResources.hpp" #include "prx/libSceAgcDriver/Graphics/include/Recorder.hpp" #include "prx/libSceAgcDriver/Graphics/include/Texture.hpp" #include "prx/libSceAgcDriver/Graphics/include/TextureDetiler.hpp" #include "prx/libSceAgcDriver/Graphics/include/TextureTiling.hpp" #include "prx/libc/include/GuestAllocations.hpp" #include "VulkanTestDevice.hpp" +#include "FixtureDeviceSelection.hpp" #include #include #include @@ -32,7 +30,6 @@ using namespace AgcDriver::Graphics; constexpr std::uint32_t Side = 64; constexpr std::size_t BlockBytes = 65536; alignas(4096) std::array Storage{}; -alignas(4096) std::array CopyStorage{}; constexpr std::uint32_t Format32Float = 22; constexpr std::uint32_t Format8Uint = 5; @@ -41,6 +38,8 @@ public: Device() { #ifdef _WIN32 library = SDL_LoadObject("vulkan-1.dll"); +#elif defined(__APPLE__) + library = SDL_LoadObject("libvulkan.1.dylib"); #else library = SDL_LoadObject("libvulkan.so.1"); #endif @@ -52,6 +51,12 @@ public: application.apiVersion = VK_API_VERSION_1_1; VkInstanceCreateInfo info{VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO}; info.pApplicationInfo = &application; +#ifdef __APPLE__ + const char* portability = VK_KHR_PORTABILITY_ENUMERATION_EXTENSION_NAME; + info.flags = VK_INSTANCE_CREATE_ENUMERATE_PORTABILITY_BIT_KHR; + info.enabledExtensionCount = 1; + info.ppEnabledExtensionNames = &portability; +#endif Check(function("vkCreateInstance")(&info, nullptr, &instance), "vkCreateInstance"); std::uint32_t count = 0; const auto enumerate = function("vkEnumeratePhysicalDevices"); @@ -59,7 +64,7 @@ public: Require(count != 0, "no Vulkan device"); std::vector devices(count); Check(enumerate(instance, &count, devices.data()), "vkEnumeratePhysicalDevices"); - context.physical = devices.front(); + context.physical = SelectFixturePhysicalDevice(devices, function("vkGetPhysicalDeviceProperties")); const auto extensions = function("vkEnumerateDeviceExtensionProperties"); Check(extensions(context.physical, nullptr, &count, nullptr), "vkEnumerateDeviceExtensionProperties"); std::vector available(count); @@ -331,69 +336,6 @@ void RunPairings(Context context, std::uint8_t* block) { ExpectFailure([&] { static_cast(DepthSurfaceStoreBack(context, *depth)); }, "through a view that another view of the plane", "a store back through a replaced view was taken by an older pairing that covers the plane"); } -void DepthCopyCase(Context context, std::uint8_t* block) { - const auto colorAddress = reinterpret_cast(block); - const auto depthAddress = colorAddress + BlockBytes / 4; - Recorder recorder(context); - recorder.Activate(); - const auto submit = [&] { - recorder.Submit(); - Check(context.Function("vkQueueWaitIdle")(context.queue), "vkQueueWaitIdle"); - }; - ColorTarget color{}; - color.address = colorAddress; - color.surfaceAddress = colorAddress; - color.extent = {Side, Side}; - color.surfaceExtent = color.extent; - color.format = VK_FORMAT_R32_SFLOAT; - color.elementBytes = 4; - color.tileMode = ColorTileMode::RenderTarget; - color.bytes = ColorTargetLayout(Side, Side, ColorTileMode::RenderTarget, 4).Bytes(); - const auto copy = [&](std::uint32_t samples, std::uint32_t sample, VkExtent2D extent) { - RunDepthCopyPass(context, DepthCopyPass{depthAddress, extent, samples, sample, color}); - submit(); - const auto resident = CachedStorageSurface(context, SurfaceForTarget(color)); - resident->MarkDirty(); - resident->WriteBack(); - submit(); - }; - const auto countBlock = [&](float value) { - std::size_t count = 0; - for (std::size_t offset = 0; offset < BlockBytes; offset += sizeof(float)) { - float texel = 0; - std::memcpy(&texel, block + offset, sizeof(texel)); - if (texel == value) ++count; - } - return count; - }; - std::fill(block, block + BlockBytes, std::uint8_t{0}); - ExpectFailure([&] { copy(1, 0, {Side, Side}); }, "no draw rendered", "a DB depth copy from memory without a depth surface was accepted"); - const auto sampledCounts = context.limits.sampledImageDepthSampleCounts & context.limits.framebufferDepthSampleCounts; - const std::uint32_t samples = (sampledCounts & VK_SAMPLE_COUNT_2_BIT) != 0 ? 2u : (sampledCounts & VK_SAMPLE_COUNT_4_BIT) != 0 ? 4u : 0u; - if (samples != 0) { - Require(DepthSurfaceView(context, DepthTarget{depthAddress, 0, {Side, Side}, VK_FORMAT_D32_SFLOAT, 0.375f, 0, 0, false, samples}) != VK_NULL_HANDLE, "the multisample depth surface was not created"); - for (std::uint32_t sample = 0; sample < samples; ++sample) { - std::fill(block, block + BlockBytes, std::uint8_t{0}); - copy(samples, sample, {Side, Side}); - Require(countBlock(0.375f) == Side * Side, "the DB depth copy of sample " + std::to_string(sample) + " of " + std::to_string(samples) + " did not store the cleared depth in every texel of the R32 float target"); - } - ExpectFailure([&] { copy(samples, samples, {Side, Side}); }, "does not have", "a DB depth copy of a sample past the last of a multisample surface was accepted"); - ExpectFailure([&] { copy(samples, 0, {Side / 2, Side / 2}); }, "of another size", "a DB depth copy of another extent than the depth surface was accepted"); - ClearDepthSurfaces(context.device); - } - if ((context.limits.sampledImageDepthSampleCounts & VK_SAMPLE_COUNT_2_BIT) == 0) { - Require(DepthSurfaceView(context, DepthTarget{depthAddress, 0, {Side, Side}, VK_FORMAT_D32_SFLOAT, 0.375f, 0, 0, false, 2}) != VK_NULL_HANDLE, "the unsampleable two-sample depth surface was not created"); - ExpectFailure([&] { copy(2, 0, {Side, Side}); }, "cannot sample at its sample count", "a DB depth copy from an unsampleable two-sample depth surface was accepted"); - ClearDepthSurfaces(context.device); - std::puts("DB depth copy: unsupported two-sample source refused cleanly"); - } - Require(DepthSurfaceView(context, DepthTarget{depthAddress, 0, {Side, Side}, VK_FORMAT_D32_SFLOAT, 0.625f, 0, 0, false, 1}) != VK_NULL_HANDLE, "the one-sample depth surface was not created"); - std::fill(block, block + BlockBytes, std::uint8_t{0}); - copy(1, 0, {Side, Side}); - Require(countBlock(0.625f) == Side * Side, "the DB depth copy of a one-sample depth surface did not store the cleared depth in every texel of the R32 float target"); - ExpectFailure([&] { copy(1, 1, {Side, Side}); }, "does not have", "a DB depth copy of a second sample of a one-sample surface was accepted"); -} - } int main() { @@ -414,11 +356,6 @@ int main() { { std::lock_guard gpu(AgcDriver::GuestMemory::GpuMutex()); Run(context, block); - auto* copyBlock = CopyStorage.data() + (BlockBytes - reinterpret_cast(CopyStorage.data()) % BlockBytes) % BlockBytes; - GuestAllocations::Mutation().Add(copyBlock, BlockBytes, true, true, true); - DepthCopyCase(context, copyBlock); - ClearCachedTextures(context.device); - GuestAllocations::Mutation().Remove(copyBlock); ClearDepthSurfaces(context.device); RunPairings(context, block); ClearDepthSurfaces(context.device); diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/DepthSurfaceLastWriter.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/DepthSurfaceLastWriter.cpp index b7f809f6ee..cdb726b3bd 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/DepthSurfaceLastWriter.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/DepthSurfaceLastWriter.cpp @@ -4,6 +4,7 @@ #include "prx/libc/include/GuestArena.hpp" #include "prx/libc/include/GuestWriteWatch.hpp" #include "VulkanTestDevice.hpp" +#include "FixtureDeviceSelection.hpp" #include #ifdef _WIN32 #ifndef NOMINMAX @@ -36,6 +37,8 @@ public: Device() { #ifdef _WIN32 library = SDL_LoadObject("vulkan-1.dll"); +#elif defined(__APPLE__) + library = SDL_LoadObject("libvulkan.1.dylib"); #else library = SDL_LoadObject("libvulkan.so.1"); #endif @@ -47,6 +50,12 @@ public: application.apiVersion = VK_API_VERSION_1_1; VkInstanceCreateInfo info{VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO}; info.pApplicationInfo = &application; +#ifdef __APPLE__ + const char* portability = VK_KHR_PORTABILITY_ENUMERATION_EXTENSION_NAME; + info.flags = VK_INSTANCE_CREATE_ENUMERATE_PORTABILITY_BIT_KHR; + info.enabledExtensionCount = 1; + info.ppEnabledExtensionNames = &portability; +#endif Check(function("vkCreateInstance")(&info, nullptr, &instance), "vkCreateInstance"); std::uint32_t count = 0; const auto enumerate = function("vkEnumeratePhysicalDevices"); @@ -54,7 +63,7 @@ public: Require(count != 0, "no Vulkan device"); std::vector devices(count); Check(enumerate(instance, &count, devices.data()), "vkEnumeratePhysicalDevices"); - context.physical = devices.front(); + context.physical = SelectFixturePhysicalDevice(devices, function("vkGetPhysicalDeviceProperties")); const auto queues = function("vkGetPhysicalDeviceQueueFamilyProperties"); queues(context.physical, &count, nullptr); std::vector families(count); diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/DrawCacheRelocation.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/DrawCacheRelocation.cpp index a82e34a36a..45f1e60352 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/DrawCacheRelocation.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/DrawCacheRelocation.cpp @@ -135,6 +135,69 @@ void MovedDataHitsTheCache(bool libraries) { Require(again.lookups == 10 && again.hits == 5, mode + ": a draw at an address seen before did not reuse the cached draw (" + std::to_string(again.hits) + " hits)"); } +void EvictedEntriesServeNoRelocation(bool libraries) { + Registered(VertexHeader, VertexCode.data(), sizeof(VertexCode), 2); + Registered(PixelHeader, PixelCode.data(), sizeof(PixelCode), 1); + Registered(SecondVertexHeader, SecondVertexCode.data(), sizeof(SecondVertexCode), 2); + const auto mode = std::string(libraries ? "with" : "without") + " graphics pipeline libraries"; + const auto draw = [&](const float* source, const std::uint32_t* vertexCode) { + auto commands = DrawFrom({source}, vertexCode); + Packet packet{commands.data(), static_cast(commands.size()), 0, {}}; + Require(sceAgcDriverSubmitDcb(&packet) == 0, "the draw submission failed"); + AgcDriverWaitIdle_nid_postfix(); + return AgcDriver::DriverDetail::Driver::Get().DrawCacheCounters(); + }; + static std::array, 6> same{}; + for (auto& entry : same) entry = {1.0f, 0.0f, 0.0f, 0.0f}; + alignas(16) static std::array, 3> others{}; + for (std::size_t i = 0; i < others.size(); ++i) others[i] = {0.25f + 0.03125f * static_cast(i), 0.0f, 0.0f, 0.0f}; + AgcDriver::DriverDetail::DrawEntryCounters counters{}; + for (std::size_t i = 0; i < 4; ++i) counters = draw(same[i].data(), VertexCode.data()); + Require(counters.lookups == 4 && counters.hits == 0 && counters.inserts == 2, mode + ": the draws that teach the moved pointer and its contents made " + std::to_string(counters.hits) + " hits and " + std::to_string(counters.inserts) + " inserts, expected 0 and 2"); + const auto relocated = draw(same[4].data(), VertexCode.data()); + Require(relocated.hits == 1, mode + ": a draw whose data moved after the teaching draws did not reuse the cached draw (" + std::to_string(relocated.hits) + " hits)"); + auto evicted = relocated; + for (const auto& entry : others) { + draw(entry.data(), SecondVertexCode.data()); + evicted = draw(entry.data(), SecondVertexCode.data()); + } + Require(evicted.evictions - relocated.evictions >= 2 && evicted.inserts - relocated.inserts >= 2, mode + ": draws of another vertex shader did not evict the two taught entries from the draw cache (" + std::to_string(evicted.inserts - relocated.inserts) + " inserts, " + std::to_string(evicted.evictions - relocated.evictions) + " evictions; run with APS5_DRAW_CACHE_ENTRIES=2)"); + const auto after = draw(same[5].data(), VertexCode.data()); + Require(after.lookups == evicted.lookups + 1 && after.hits == evicted.hits, mode + ": a draw whose data moved reused a draw the cache had evicted, through its relocation alias (" + std::to_string(after.hits - evicted.hits) + " hits)"); +} + +void RelocatingDrawsKeepTheirDonor(bool libraries) { + Registered(VertexHeader, VertexCode.data(), sizeof(VertexCode), 2); + Registered(PixelHeader, PixelCode.data(), sizeof(PixelCode), 1); + Registered(SecondVertexHeader, SecondVertexCode.data(), sizeof(SecondVertexCode), 2); + const auto mode = std::string(libraries ? "with" : "without") + " graphics pipeline libraries"; + const auto draw = [&](const float* source, const std::uint32_t* vertexCode) { + auto commands = DrawFrom({source}, vertexCode); + Packet packet{commands.data(), static_cast(commands.size()), 0, {}}; + Require(sceAgcDriverSubmitDcb(&packet) == 0, "the draw submission failed"); + AgcDriverWaitIdle_nid_postfix(); + return AgcDriver::DriverDetail::Driver::Get().DrawCacheCounters(); + }; + constexpr std::size_t Rounds = 3; + alignas(16) static std::array, Rounds> others{}; + for (std::size_t i = 0; i < others.size(); ++i) others[i] = {0.25f + 0.03125f * static_cast(i), 0.0f, 0.0f, 0.0f}; + static std::array, 5 + Rounds> same{}; + for (auto& entry : same) entry = {1.0f, 0.0f, 0.0f, 0.0f}; + AgcDriver::DriverDetail::DrawEntryCounters counters{}; + for (std::size_t i = 0; i < 4; ++i) counters = draw(same[i].data(), VertexCode.data()); + Require(counters.lookups == 4 && counters.hits == 0 && counters.inserts == 2, mode + ": the draws that teach the moved pointer and its contents made " + std::to_string(counters.hits) + " hits and " + std::to_string(counters.inserts) + " inserts, expected 0 and 2"); + const auto start = draw(same[4].data(), VertexCode.data()); + Require(start.hits == 1, mode + ": a draw whose data moved after the teaching draws did not reuse the cached draw (" + std::to_string(start.hits) + " hits)"); + auto relocated = start; + for (std::size_t i = 0; i < others.size(); ++i) { + draw(others[i].data(), SecondVertexCode.data()); + const auto inserted = draw(others[i].data(), SecondVertexCode.data()); + relocated = draw(same[5 + i].data(), VertexCode.data()); + Require(relocated.hits == inserted.hits + 1, mode + ": a draw whose data moved again lost the cached draw it reused before each of " + std::to_string(i + 1) + " rounds of another vertex shader's draws (" + std::to_string(inserted.inserts - start.inserts) + " inserts, " + std::to_string(inserted.evictions - start.evictions) + " evictions)"); + } + Require(relocated.evictions - start.evictions >= 2, mode + ": draws of another vertex shader evicted " + std::to_string(relocated.evictions - start.evictions) + " entries, expected at least 2 (run with APS5_DRAW_CACHE_ENTRIES=2)"); +} + void SplitRunsNameTheirSharedWords() { alignas(16) static std::array data{1u, 2u, 3u, 4u}; auto table = std::make_shared(); @@ -228,9 +291,11 @@ void MovedForwardedWordReachesStageDataHit(bool libraries) { } -int main() { +int main(int argc, char** argv) { try { - SplitRunsNameTheirSharedWords(); + const bool evicted = argc == 2 && std::string(argv[1]) == "--evicted"; + const bool kept = argc == 2 && std::string(argv[1]) == "--kept"; + if (!evicted && !kept) SplitRunsNameTheirSharedWords(); bool libraries = false; { const auto device = OpenVulkanTestDevice(); @@ -238,6 +303,18 @@ int main() { libraries = device->GraphicsPipelineLibraries(); } if (!libraries && std::getenv("APS5_NO_GPL") == nullptr) return VulkanTestSkipped; + if (evicted) { + EvictedEntriesServeNoRelocation(libraries); + AgcDriverShutdown_nid_postfix(); + std::cout << "An evicted draw served no relocation " << (libraries ? "with" : "without") << " graphics pipeline libraries\n"; + return 0; + } + if (kept) { + RelocatingDrawsKeepTheirDonor(libraries); + AgcDriverShutdown_nid_postfix(); + std::cout << "A draw relocated from a cached draw kept it cached " << (libraries ? "with" : "without") << " graphics pipeline libraries\n"; + return 0; + } MovedDataHitsTheCache(libraries); UnrelocatedAliasesStayOut(libraries); MovedForwardedWordReachesStageDataHit(libraries); diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/DrawDataHitUserWords.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/DrawDataHitUserWords.cpp index 2da8fc562c..ed4b5280c1 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/DrawDataHitUserWords.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/DrawDataHitUserWords.cpp @@ -34,6 +34,7 @@ alignas(256) constexpr std::array TriangleVertexCode{ 0x7e060280u, 0x7e0802f2u, 0xf80008cfu, 0x04030201u, 0xbf810000u, }; alignas(256) constexpr std::array ArithmeticPixelCode{0xf4000100u, 0xfa000000u, 0xbf8cc07fu, 0x7e000204u, 0x06000002u, 0x7e020280u, 0xf800180fu, 0x00010100u, 0xbf810000u, 0xbf810000u}; +alignas(256) constexpr std::array DirectBufferPixelCode{0xe0300000u, 0x80000000u, 0xbf8c3f70u, 0x7e020280u, 0xf800180fu, 0x00010100u, 0xbf810000u, 0xbf810000u}; alignas(256) std::array PixelConstant{}; alignas(256) std::array ColorMemory{}; @@ -47,6 +48,7 @@ struct Header { alignas(8) Header VertexHeader{}; alignas(8) Header PixelHeader{}; alignas(8) Header ArithmeticPixelHeader{}; +alignas(8) Header DirectBufferPixelHeader{}; void Registered(Header& header, const std::uint32_t* code, std::size_t bytes, std::uint8_t type) { header.shader.file_header = 0x34333231u; @@ -83,10 +85,10 @@ void PixelUserData(std::vector& commands, const std::uint32_t* po Append(commands, 0x76, {0x00e, unread}); } -std::vector Setup(bool triangle = false) { +std::vector Setup(bool triangle = false, const std::uint32_t* pixelCode = nullptr, std::uint32_t pixelUserSgprs = 3) { const auto color = reinterpret_cast(ColorMemory.data()); const auto vertex = reinterpret_cast(triangle ? TriangleVertexCode.data() : VertexCode.data()); - const auto pixel = reinterpret_cast(triangle ? ArithmeticPixelCode.data() : PixelCode.data()); + const auto pixel = reinterpret_cast(pixelCode ? pixelCode : triangle ? ArithmeticPixelCode.data() : PixelCode.data()); std::vector> context{ {0x2d5, 0x2000}, {0x1b6, 0}, {0x207, 0}, {0x200, 0}, {0x203, 0x800}, @@ -110,7 +112,7 @@ std::vector Setup(bool triangle = false) { }; const std::vector> shader{ {0xc8, static_cast(vertex >> 8u)}, {0xc9, static_cast(vertex >> 40u)}, {0x8b, triangle ? 0u : 1u << 1u}, {0x8c, 0}, - {0x008, static_cast(pixel >> 8u)}, {0x009, static_cast(pixel >> 40u)}, {0x00b, 3u << 1u}, + {0x008, static_cast(pixel >> 8u)}, {0x009, static_cast(pixel >> 40u)}, {0x00b, pixelUserSgprs << 1u}, }; if (triangle) context.insert(context.end(), {{0x2fe, 0}, {0x302, 0}, {0x306, 0}, {0x30a, 0}}); std::vector commands; @@ -223,6 +225,39 @@ void ReadUserWordsChangeTheOutput() { Require(closeTo(second, 191), "a pixel stage adding user word 2 changed to 0.5 wrote " + std::to_string(second) + ", expected 191 (" + Counts(after) + ", before: " + Counts(before) + ")"); } +void DirectBufferBaseMovesWithItsPayload() { + Registered(VertexHeader, TriangleVertexCode.data(), sizeof(TriangleVertexCode), 2); + Registered(DirectBufferPixelHeader, DirectBufferPixelCode.data(), sizeof(DirectBufferPixelCode), 1); + auto* ring = GuestBlock(); + ColorMemory.fill(std::byte{0}); + const auto slot = [&](std::size_t index, float value) { + auto* data = ring + 1024 + index * 64; + data[0] = std::bit_cast(value); + return data; + }; + const auto draw = [&](const std::uint32_t* data, bool setup) { + auto commands = setup ? Setup(true, DirectBufferPixelCode.data(), 4) : std::vector{}; + const auto address = reinterpret_cast(data); + Append(commands, 0x76, {0x00c, static_cast(address)}); + Append(commands, 0x76, {0x00d, static_cast(address >> 32u) & 0xffffu}); + Append(commands, 0x76, {0x00e, 16}); + Append(commands, 0x76, {0x00f, 0x3004bfacu}); + Append(commands, 0x2d, {3, 2}); + Submit(std::move(commands)); + return BrightestColorByte(); + }; + const auto first = slot(0, 0.25f); + const auto second = slot(1, 1.0f); + draw(first, true); + const auto repeated = draw(first, false); + const auto before = AgcDriver::DriverDetail::Driver::Get().DrawCacheCounters(); + const auto moved = draw(second, false); + const auto after = AgcDriver::DriverDetail::Driver::Get().DrawCacheCounters(); + Require(moved > repeated, "a draw whose descriptor base and payload both moved read the old payload: " + std::to_string(repeated) + " then " + std::to_string(moved) + " (" + Counts(after) + ", before: " + Counts(before) + ")"); + Require(after.dataHits == before.dataHits + 1, "a draw whose descriptor base and payload moved was not a data-only hit (" + Counts(after) + ", before: " + Counts(before) + ")"); + Require(after.dataStagesCompiled - before.dataStagesCompiled >= 1, "a draw whose descriptor base moved reused the pixel stage built for the old base: the rebuild this test documents is gone, flip it to a reuse check (" + Counts(after) + ", before: " + Counts(before) + ")"); +} + } int main() { @@ -237,6 +272,7 @@ int main() { const bool verify = std::getenv("APS5_VERIFY_DRAW_DATA_HITS") != nullptr; UnreadUserWordsKeepTheStage(verify); ReadUserWordsChangeTheOutput(); + DirectBufferBaseMovesWithItsPayload(); AgcDriverShutdown_nid_postfix(); std::cout << "Data-only hits " << (verify ? "verified" : "reused") << " the stages whose read user words did not change\n"; return 0; diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/DrawShaderFailure.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/DrawShaderFailure.cpp new file mode 100644 index 0000000000..2051585c2d --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/tests/execution/DrawShaderFailure.cpp @@ -0,0 +1,355 @@ +#include "prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp" +#include "prx/libSceAgcDriver/Submit/include/Dcb.hpp" +#include "prx/libc/include/GuestAllocations.hpp" +#include "VulkanTestDevice.hpp" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef _WIN32 +#ifndef NOMINMAX +#define NOMINMAX +#endif +#include +#else +#include +#endif + +namespace { + +using AgcDriver::Graphics::Require; + +alignas(256) constexpr std::array FailingVertexCode{0xbe800200u, 0xbf810000u}; +alignas(256) constexpr std::array RequestFailureVertexCode{0xbe901f00u, 0x809010c0u, 0xbe802010u, 0xbf810000u}; +alignas(256) constexpr std::array ValidVertexCode{0x7e000208u, 0x7e020280u, 0x7e040280u, 0x7e0602f2u, 0xf80008cfu, 0x03020100u, 0xbf810000u, 0xbf810000u}; +alignas(256) constexpr std::array PixelCode{0x7e000200u, 0x7e020280u, 0xf800180fu, 0x00010100u, 0xbf810000u, 0xbf810000u, 0xbf810000u, 0xbf810000u}; + +struct ColorBuffer { + static constexpr std::size_t Size = 65536; + ColorBuffer() { +#ifdef _WIN32 + data = static_cast(VirtualAlloc(nullptr, Size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)); + Require(data != nullptr, "cannot allocate the color target"); +#else + void* raw = mmap(nullptr, 2 * Size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + Require(raw != MAP_FAILED, "cannot map the color target"); + const auto begin = reinterpret_cast(raw); + const auto aligned = (begin + Size - 1) & ~(static_cast(Size) - 1); + if (aligned != begin) munmap(raw, aligned - begin); + if (aligned + Size != begin + 2 * Size) munmap(reinterpret_cast(aligned + Size), begin + 2 * Size - (aligned + Size)); + data = reinterpret_cast(aligned); +#endif + std::memset(data, 0, Size); + GuestAllocations::Mutation().Add(data, Size, true, true, true); + } + ~ColorBuffer() { + if (data != nullptr) { + GuestAllocations::Mutation().Remove(data); +#ifdef _WIN32 + VirtualFree(data, 0, MEM_RELEASE); +#else + munmap(data, Size); +#endif + data = nullptr; + } + } + ColorBuffer(const ColorBuffer&) = delete; + ColorBuffer& operator=(const ColorBuffer&) = delete; + + std::byte* data = nullptr; +}; + +struct Header { + Shader shader{}; + ShaderUserData userData{}; +}; + +alignas(8) Header VertexHeader{}; +alignas(8) Header PixelHeader{}; + +const Shader* Registered(Header& header, const std::uint32_t* code, std::size_t bytes, std::uint8_t type) { + header.shader.file_header = 0x34333231u; + header.shader.version = 0x18u; + header.shader.header_size = sizeof(Header); + header.shader.shader_size = static_cast(bytes); + header.shader.code = code; + header.shader.type = type; + header.shader.user_data = &header.userData; + AgcDriverRegisterShader_nid_postfix(&header.shader); + return &header.shader; +} + +void Append(std::vector& commands, std::uint32_t opcode, std::initializer_list payload) { + commands.push_back(0xc0000000u | (static_cast(payload.size() - 1) << 16u) | (opcode << 8u)); + commands.insert(commands.end(), payload); +} + +std::vector DrawPacket(std::uintptr_t color, const std::uint32_t* vertex, const std::uint32_t* pixel) { + const auto vertexAddress = reinterpret_cast(vertex); + const auto pixelAddress = reinterpret_cast(pixel); + const std::vector> context{ + {0x2d5, 0x2000}, + {0x1b6, 0}, {0x207, 0}, {0x200, 0}, {0x203, 0x800}, + {0x2dc, 0xaa00}, {0x2f8, 0}, {0x292, 2}, {0x293, 0}, + {0x80, 0}, {0x8d, 0}, {0x83, 0xffff}, {0x8c, 0xa}, + {0x2f9, 0x2d}, {0x313, 0x6000}, {0x30e, 0xffffffff}, {0x30f, 0xffffffff}, + {0x206, 0x43f}, {0x204, 0x80000}, {0x205, 0x240}, + {0x8e, 0xf}, {0x8f, 0xf}, {0x202, 0xcc0010}, + {0x1c4, 0}, {0x1c5, 9}, {0x1c3, 4}, {0x31c, 0x28028}, + {0x31b, 0}, {0x31d, 0}, {0x3b0, (63u << 14u) | 3u}, + {0x3b8, 0x4dc6c000}, {0x1e0, 0}, + {0xc, 0}, {0xd, 0x40040}, + {0x81, 0x80000000}, {0x82, 0x40040}, + {0x90, 0x80000000}, {0x91, 0x40040}, + {0x94, 0x80000000}, {0x95, 0x40040}, + {0x318, static_cast(color >> 8u)}, {0x390, static_cast(color >> 40u)}, + {0x10f, std::bit_cast(32.0f)}, {0x110, std::bit_cast(32.0f)}, + {0x111, std::bit_cast(-2.0f)}, {0x112, std::bit_cast(2.0f)}, + {0x113, std::bit_cast(1.0f)}, {0x114, 0}, + {0xb4, 0}, {0xb5, std::bit_cast(1.0f)}, + }; + const std::vector> shader{ + {0xc8, static_cast(vertexAddress >> 8u)}, {0xc9, static_cast(vertexAddress >> 40u)}, {0x8b, 1u << 1u}, {0x8c, 0}, + {0x008, static_cast(pixelAddress >> 8u)}, {0x009, static_cast(pixelAddress >> 40u)}, {0x00b, 1u << 1u}, {0x00c, std::bit_cast(1.0f)}, + }; + std::vector commands; + Append(commands, 0x79, {0x242, 4}); + for (const auto& [offset, value] : context) Append(commands, 0x69, {offset, value}); + for (const auto& [offset, value] : shader) Append(commands, 0x76, {offset, value}); + Append(commands, 0x2d, {3, 2}); + return commands; +} + +std::string ReadTraceFile(const char* path) { + std::ifstream stream(path, std::ios::binary); + if (!stream.is_open()) return {}; + std::ostringstream buffer; + buffer << stream.rdbuf(); + return buffer.str(); +} + +bool WaitForTraceRecord(const char* path, const std::string& record) { + for (int attempt = 0; attempt < 200; ++attempt) { + if (ReadTraceFile(path).find(record) != std::string::npos) return true; + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + } + return false; +} + +std::string TraceLine(const std::string& trace, const std::string& record) { + const auto position = trace.find(record); + if (position == std::string::npos) return {}; + const auto end = trace.find('\n', position); + return trace.substr(position, end == std::string::npos ? std::string::npos : end - position); +} + +std::string TraceField(const std::string& line, const char* name) { + const auto field = line.find(std::string(name) + "="); + if (field == std::string::npos) return {}; + const auto start = field + std::strlen(name) + 1; + const auto end = line.find(' ', start); + return line.substr(start, end == std::string::npos ? std::string::npos : end - start); +} + +std::string SubmissionSerialOf(const std::string& trace, const std::string& record) { + const auto position = trace.find(record); + if (position == std::string::npos) return {}; + const auto field = trace.find("submission=", position); + if (field == std::string::npos) return {}; + const auto start = field + std::string("submission=").size(); + const auto end = trace.find(' ', start); + return trace.substr(start, end == std::string::npos ? std::string::npos : end - start); +} + +void SubmitDraw(std::uintptr_t color, const std::uint32_t* vertex, const std::uint32_t* pixel) { + auto commands = DrawPacket(color, vertex, pixel); + Packet packet{commands.data(), static_cast(commands.size()), 0, {}}; + Require(sceAgcDriverSubmitDcb(&packet) == 0, "the draw submission was not accepted"); +} + +void FailingShaderDecodeRecordsTheDraw(const char* tracePath, bool correlationExpected = true) { + ColorBuffer color; + Registered(VertexHeader, FailingVertexCode.data(), FailingVertexCode.size() * sizeof(std::uint32_t), 2); + Registered(PixelHeader, PixelCode.data(), PixelCode.size() * sizeof(std::uint32_t), 1); + SubmitDraw(reinterpret_cast(color.data), FailingVertexCode.data(), PixelCode.data()); + std::string failure; + try { + AgcDriverWaitIdle_nid_postfix(); + Require(false, "the failing shader request did not fail the submission"); + } catch (const std::exception& error) { + failure = error.what(); + } + Require(failure.find("unsupported SOP1 opcode 2") != std::string::npos, "the submission failure lost the original shader-request error: " + failure); + if (!correlationExpected) { + const auto trace = ReadTraceFile(tracePath); + Require(trace.find("draw-attempt") == std::string::npos, "correlation-disabled failure emitted draw-attempt"); + Require(trace.find("draw-shader-request-failed") == std::string::npos, "correlation-disabled failure emitted draw-shader-request-failed"); + Require(trace.find("draw-materialization-failed") == std::string::npos, "correlation-disabled failure emitted draw-materialization-failed"); + Require(trace.find("draw-submission-failed") == std::string::npos, "correlation-disabled failure emitted draw-submission-failed"); + Require(trace.find("draw-selected") == std::string::npos, "correlation-disabled failure emitted draw-selected"); + Require(trace.find("draw-recorded") == std::string::npos, "correlation-disabled failure emitted draw-recorded"); + return; + } + Require(WaitForTraceRecord(tracePath, "draw-attempt-failed"), "the capture trace received no draw-attempt-failed record"); + const auto trace = ReadTraceFile(tracePath); + const auto attempt = TraceLine(trace, "draw-attempt "); + Require(!attempt.empty(), "the failed draw emitted no draw-attempt record"); + const auto token = TraceField(attempt, "token"); + Require(!token.empty() && token != "0", "the draw-attempt record carries no draw token"); + const auto failed = TraceLine(trace, "draw-attempt-failed"); + Require(!failed.empty(), "the failed draw emitted no draw-attempt-failed record"); + Require(failed.find("token=" + token) != std::string::npos, "the decode failure lost the draw token"); + Require(failed.find("phase=decode") != std::string::npos, "the SOP1 failure was not classified at the decode stage"); + Require(failed.find("offset=" + TraceField(attempt, "offset")) != std::string::npos, "the decode failure lost the packet offset"); + Require(failed.find("unsupported SOP1 opcode 2") != std::string::npos, "the decode failure lost the shader-decoder reason"); + Require(trace.find("draw-selected") == std::string::npos, "a pre-selection decode failure emitted draw-selected"); + Require(trace.find("draw-shader-request-failed") == std::string::npos, "a pre-request decode failure emitted draw-shader-request-failed"); + Require(trace.find("draw-recorded token=" + token) == std::string::npos, "a failed draw emitted draw-recorded"); + Require(trace.find("tokens=[" + token + "]") == std::string::npos, "a failed draw token was attributed to a submitted batch"); + const auto errorSerial = SubmissionSerialOf(trace, "draw-error"); + Require(!errorSerial.empty(), "the draw-error record carries no submission serial"); + Require(errorSerial == SubmissionSerialOf(trace, "draw-attempt"), "the draw-error record (submission " + errorSerial + ") does not correlate with the draw attempt"); + Require(trace.find("draw-error submission=" + errorSerial + " offset=" + TraceField(attempt, "offset")) != std::string::npos, "the draw-error record lost the attempted packet offset"); + Require(trace.find("unsupported SOP1 opcode 2") != std::string::npos, "the trace lost the shader-decoder reason"); +} + +void FailingShaderMaterializationRecordsTheDraw(const char* tracePath, bool correlationExpected = true) { + ColorBuffer color; + Registered(VertexHeader, RequestFailureVertexCode.data(), RequestFailureVertexCode.size() * sizeof(std::uint32_t), 2); + Registered(PixelHeader, PixelCode.data(), PixelCode.size() * sizeof(std::uint32_t), 1); + SubmitDraw(reinterpret_cast(color.data), RequestFailureVertexCode.data(), PixelCode.data()); + std::string failure; + try { + AgcDriverWaitIdle_nid_postfix(); + Require(false, "the failing shader request did not fail the submission"); + } catch (const std::exception& error) { + failure = error.what(); + } + Require(failure.find("unsupported dynamic s_setpc_b64") != std::string::npos, "the submission failure lost the original shader-request error: " + failure); + if (!correlationExpected) { + const auto trace = ReadTraceFile(tracePath); + Require(trace.find("draw-attempt") == std::string::npos, "correlation-disabled failure emitted draw-attempt"); + Require(trace.find("draw-shader-request-failed") == std::string::npos, "correlation-disabled failure emitted draw-shader-request-failed"); + Require(trace.find("draw-materialization-failed") == std::string::npos, "correlation-disabled failure emitted draw-materialization-failed"); + Require(trace.find("draw-submission-failed") == std::string::npos, "correlation-disabled failure emitted draw-submission-failed"); + Require(trace.find("draw-selected") == std::string::npos, "correlation-disabled failure emitted draw-selected"); + Require(trace.find("draw-recorded") == std::string::npos, "correlation-disabled failure emitted draw-recorded"); + return; + } + Require(WaitForTraceRecord(tracePath, "draw-shader-request-failed"), "the capture trace received no draw-shader-request-failed record"); + const auto trace = ReadTraceFile(tracePath); + const auto attempt = TraceLine(trace, "draw-attempt "); + Require(!attempt.empty(), "the failed draw emitted no draw-attempt record"); + const auto token = TraceField(attempt, "token"); + Require(!token.empty() && token != "0", "the draw-attempt record carries no draw token"); + const auto request = TraceLine(trace, "draw-shader-request-failed"); + Require(!request.empty(), "the failed draw emitted no request-failure line"); + Require(request.find("token=" + token) != std::string::npos, "the request failure lost the draw token"); + Require(request.find("offset=" + TraceField(attempt, "offset")) != std::string::npos, "the request failure lost the packet offset"); + const auto snapshot = TraceField(request, "snapshot"); + Require(!snapshot.empty() && snapshot != "0x0", "the request failure carries no snapshot identity"); + const auto selected = TraceLine(trace, "draw-selected token=" + token); + Require(!selected.empty(), "the failed draw emitted no draw-selected record for its token"); + Require(selected.find("stage=1") != std::string::npos, "the first selected program is not the failing vertex stage"); + Require(selected.find("snapshot=" + snapshot) != std::string::npos, "the request failure lost the selected snapshot identity"); + Require(trace.find("draw-recorded token=" + token) == std::string::npos, "a failed draw emitted draw-recorded"); + char codeAddress[19]; + std::snprintf(codeAddress, sizeof(codeAddress), "%llx", static_cast(reinterpret_cast(RequestFailureVertexCode.data()))); + Require(request.find(std::string("code=") + codeAddress + " codeOffset=0 stage=1 type=2") != std::string::npos, "the draw-shader-request-failed record does not identify the failing vertex shader"); + Require(trace.find("tokens=[" + token + "]") == std::string::npos, "a failed draw token was attributed to a submitted batch"); + const auto requestSerial = SubmissionSerialOf(trace, "draw-shader-request-failed"); + Require(!requestSerial.empty(), "the draw-shader-request-failed record carries no submission serial"); + const auto errorSerial = SubmissionSerialOf(trace, "draw-error"); + Require(requestSerial == errorSerial, "the draw-shader-request-failed record (submission " + requestSerial + ") does not correlate with the draw-error record (submission " + errorSerial + ")"); + Require(trace.find("draw-error submission=" + requestSerial + " offset=" + TraceField(attempt, "offset")) != std::string::npos, "the draw-error record lost the attempted packet offset"); + Require(trace.find("unsupported dynamic s_setpc_b64") != std::string::npos, "the draw-shader-request-failed record lost the shader-request reason"); +} + +void SuccessfulDrawEmitsNoFailureRecord(const char* tracePath) { + ColorBuffer color; + Registered(VertexHeader, ValidVertexCode.data(), ValidVertexCode.size() * sizeof(std::uint32_t), 2); + Registered(PixelHeader, PixelCode.data(), PixelCode.size() * sizeof(std::uint32_t), 1); + SubmitDraw(reinterpret_cast(color.data), ValidVertexCode.data(), PixelCode.data()); + AgcDriverWaitIdle_nid_postfix(); + Require(WaitForTraceRecord(tracePath, "draw submission="), "the capture trace received no draw submission record"); + const auto trace = ReadTraceFile(tracePath); + const auto selected = TraceLine(trace, "draw-selected"); + const auto attempt = TraceLine(trace, "draw-attempt"); + Require(!selected.empty(), "the successful draw emitted no draw-selected record"); + Require(!attempt.empty(), "the successful draw emitted no draw-attempt record"); + const auto token = TraceField(selected, "token"); + Require(!token.empty() && token != "0", "the successful selection record carries no draw token"); + Require(attempt.find("token=" + token) != std::string::npos, "the successful draw-attempt record lost the draw token"); + Require(WaitForTraceRecord(tracePath, "draw-recorded token=" + token), "the selected draw emitted no draw-recorded record"); + const auto recorded = TraceLine(ReadTraceFile(tracePath), "draw-recorded token=" + token); + const auto batch = TraceField(recorded, "batch"); + Require(!batch.empty() && batch != "0", "the draw-recorded record carries no actual batch serial"); + Require(ReadTraceFile(tracePath).find("submit batch=" + batch + " ") != std::string::npos, "the recorded batch was not submitted"); + const auto submit = TraceLine(ReadTraceFile(tracePath), "submit batch=" + batch + " "); + Require(submit == "submit batch=" + batch + " tokens=[" + token + "]", "the submit record lost the draw token set"); + Require(ReadTraceFile(tracePath).find("submit-confirmed batch=" + batch + " tokens=[" + token + "]") != std::string::npos, "the confirmed submit record lost the draw token set"); + Require(trace.find("draw-shader-request-failed") == std::string::npos, "a successful draw emitted a draw-shader-request-failed record"); +} + +void DisabledCorrelationTraceLeavesDrawBehaviorUnchanged(const char* tracePath) { + ColorBuffer color; + Registered(VertexHeader, ValidVertexCode.data(), ValidVertexCode.size() * sizeof(std::uint32_t), 2); + Registered(PixelHeader, PixelCode.data(), PixelCode.size() * sizeof(std::uint32_t), 1); + SubmitDraw(reinterpret_cast(color.data), ValidVertexCode.data(), PixelCode.data()); + AgcDriverWaitIdle_nid_postfix(); + Require(WaitForTraceRecord(tracePath, "draw submission="), "the disabled-trace draw did not complete"); + const auto trace = ReadTraceFile(tracePath); + Require(trace.find("draw-attempt") == std::string::npos, "the disabled trace emitted draw-attempt"); + Require(trace.find("draw-materialization-failed") == std::string::npos, "the disabled trace emitted draw-materialization-failed"); + Require(trace.find("draw-submission-failed") == std::string::npos, "the disabled trace emitted draw-submission-failed"); + Require(trace.find("draw-selected") == std::string::npos, "the disabled trace emitted draw-selected"); + Require(trace.find("draw-recorded") == std::string::npos, "the disabled trace emitted draw-recorded"); +} + +} + +int main(int argc, char** argv) { + const std::string mode = argc > 1 ? argv[1] : "request-failure"; + const bool expectFailure = mode != "successful-draw" && mode != "disabled-correlation"; + const char* tracePath = std::getenv("APS5_CAPTURE_TRACE_PATH"); + try { + if (!tracePath || tracePath[0] == '\0') throw std::runtime_error("this case requires a non-empty APS5_CAPTURE_TRACE_PATH"); + const auto device = OpenVulkanTestDevice(); + if (!device) return VulkanTestSkipped; + if (mode == "request-failure") FailingShaderDecodeRecordsTheDraw(tracePath); + else if (mode == "request-failure-correlation-disabled") FailingShaderDecodeRecordsTheDraw(tracePath, false); + else if (mode == "materialization-failure") FailingShaderMaterializationRecordsTheDraw(tracePath); + else if (mode == "materialization-failure-correlation-disabled") FailingShaderMaterializationRecordsTheDraw(tracePath, false); + else if (mode == "successful-draw") SuccessfulDrawEmitsNoFailureRecord(tracePath); + else if (mode == "disabled-correlation") DisabledCorrelationTraceLeavesDrawBehaviorUnchanged(tracePath); + else throw std::runtime_error("unknown draw shader failure test mode: " + mode); + try { + AgcDriverShutdown_nid_postfix(); + Require(!expectFailure, "the failed draw was not rethrown at driver shutdown"); + } catch (const std::exception&) { + Require(expectFailure, "driver shutdown failed unexpectedly"); + } + std::cout << "draw shader failure tests passed\n"; + return 0; + } catch (const std::exception& error) { + std::cerr << error.what() << '\n'; + try { + AgcDriverShutdown_nid_postfix(); + } catch (...) { + } + return 1; + } +} diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/F32AddMulDenormalModes.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/F32AddMulDenormalModes.cpp new file mode 100644 index 0000000000..7b5780067d --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/tests/execution/F32AddMulDenormalModes.cpp @@ -0,0 +1,244 @@ +#include "prx/libSceAgcDriver/Execution/include/VulkanDevice.hpp" +#include "prx/libSceAgcDriver/Graphics/include/Draw.hpp" +#include "Recompiler.hpp" +#include "VulkanTestDevice.hpp" +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace { + +using AgcDriver::Graphics::Require; +using ShaderRecompiler::ShaderStage; + +constexpr std::uint32_t Threads = 96; +constexpr std::uint32_t Inputs = 4; +constexpr std::uint32_t Results = 16; +constexpr std::uint32_t Instructions = 7; +constexpr std::uint32_t FirstMultiply = 3; +constexpr std::uint32_t FirstConstantFactor = 5; +alignas(256) std::array Input{}; +alignas(256) std::array Output{}; + +alignas(256) constexpr std::array Code{ + 0x34020084u, 0x34040086u, 0xe0381000u, 0x80000401u, 0xbf8c3f70u, 0x06140b04u, 0x08160b04u, 0x0a180b04u, + 0x101a0b04u, 0x0e1c0b04u, 0x101e08ffu, 0x3fc00000u, 0xd5070010u, 0x0001ff04u, 0x3fc00000u, 0xe0781000u, + 0x80010a02u, 0xe07c1010u, 0x80010e02u, 0xbf810000u, +}; + +constexpr const char* Names[Instructions] = {"v_add_f32", "v_sub_f32", "v_subrev_f32", "v_mul_f32", "v_mul_legacy_f32", "v_mul_f32 1.5", "v_mul_legacy_f32 1.5"}; + +template +struct Vector { + std::uint32_t operands[2]; + std::uint32_t outputsFlushed[Count]; + std::uint32_t inputsFlushed[Count]; +}; + +constexpr std::array, 40> ArithmeticVectors{{ + {{0x00000000u, 0x00000001u}, {0x00000000u, 0x80000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}}, + {{0x00000001u, 0x00800002u}, {0x00800003u, 0x80800001u, 0x00800001u, 0x00000000u, 0x00000000u}, {0x00800002u, 0x80800002u, 0x00800002u, 0x00000000u, 0x00000000u}}, + {{0x00000001u, 0x81000001u}, {0x81000000u, 0x01000002u, 0x81000002u, 0x80000000u, 0x80000000u}, {0x81000001u, 0x01000001u, 0x81000001u, 0x80000000u, 0x00000000u}}, + {{0x00000002u, 0x3f800000u}, {0x3f800000u, 0xbf800000u, 0x3f800000u, 0x00000000u, 0x00000000u}, {0x3f800000u, 0xbf800000u, 0x3f800000u, 0x00000000u, 0x00000000u}}, + {{0x00000002u, 0xff7fffffu}, {0xff7fffffu, 0x7f7fffffu, 0xff7fffffu, 0xb57fffffu, 0xb57fffffu}, {0xff7fffffu, 0x7f7fffffu, 0xff7fffffu, 0x80000000u, 0x00000000u}}, + {{0x00000003u, 0x80000001u}, {0x00000000u, 0x00000000u, 0x80000000u, 0x80000000u, 0x80000000u}, {0x00000000u, 0x00000000u, 0x80000000u, 0x80000000u, 0x00000000u}}, + {{0x00400000u, 0x00000002u}, {0x00000000u, 0x00000000u, 0x80000000u, 0x00000000u, 0x00000000u}, {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}}, + {{0x00400000u, 0x807fffffu}, {0x80000000u, 0x00bfffffu, 0x80bfffffu, 0x80000000u, 0x80000000u}, {0x00000000u, 0x00000000u, 0x80000000u, 0x80000000u, 0x00000000u}}, + {{0x007fffffu, 0x00800000u}, {0x00ffffffu, 0x80000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x00800000u, 0x80800000u, 0x00800000u, 0x00000000u, 0x00000000u}}, + {{0x007fffffu, 0x80800002u}, {0x80000000u, 0x01000000u, 0x81000000u, 0x80000000u, 0x80000000u}, {0x80800002u, 0x00800002u, 0x80800002u, 0x80000000u, 0x00000000u}}, + {{0x00800000u, 0x80000001u}, {0x00000000u, 0x00800001u, 0x80800001u, 0x80000000u, 0x80000000u}, {0x00800000u, 0x00800000u, 0x80800000u, 0x80000000u, 0x00000000u}}, + {{0x00800001u, 0x33800000u}, {0x33800000u, 0xb3800000u, 0x33800000u, 0x00000000u, 0x00000000u}, {0x33800000u, 0xb3800000u, 0x33800000u, 0x00000001u, 0x00000001u}}, + {{0x00800002u, 0x00800000u}, {0x01000001u, 0x00000000u, 0x80000000u, 0x00000000u, 0x00000000u}, {0x01000001u, 0x00000002u, 0x80000002u, 0x00000000u, 0x00000000u}}, + {{0x00ffffffu, 0x00000002u}, {0x01000000u, 0x00fffffdu, 0x80fffffdu, 0x00000000u, 0x00000000u}, {0x00ffffffu, 0x00ffffffu, 0x80ffffffu, 0x00000000u, 0x00000000u}}, + {{0x00ffffffu, 0x80800002u}, {0x00000000u, 0x01400000u, 0x81400000u, 0x80000000u, 0x80000000u}, {0x007ffffdu, 0x01400000u, 0x81400000u, 0x80000000u, 0x80000000u}}, + {{0x01000000u, 0x80000003u}, {0x00fffffdu, 0x01000002u, 0x81000002u, 0x80000000u, 0x80000000u}, {0x01000000u, 0x01000000u, 0x81000000u, 0x80000000u, 0x00000000u}}, + {{0x01000001u, 0x80000001u}, {0x01000000u, 0x01000002u, 0x81000002u, 0x80000000u, 0x80000000u}, {0x01000001u, 0x01000001u, 0x81000001u, 0x80000000u, 0x00000000u}}, + {{0x3f800000u, 0x807fffffu}, {0x3f800000u, 0x3f800000u, 0xbf800000u, 0x80000000u, 0x80000000u}, {0x3f800000u, 0x3f800000u, 0xbf800000u, 0x80000000u, 0x00000000u}}, + {{0x34000000u, 0x00800001u}, {0x34000000u, 0x34000000u, 0xb4000000u, 0x00000000u, 0x00000000u}, {0x34000000u, 0x34000000u, 0xb4000000u, 0x00000001u, 0x00000001u}}, + {{0x7f7fffffu, 0x00000003u}, {0x7f7fffffu, 0x7f7fffffu, 0xff7fffffu, 0x35bfffffu, 0x35bfffffu}, {0x7f7fffffu, 0x7f7fffffu, 0xff7fffffu, 0x00000000u, 0x00000000u}}, + {{0x80000000u, 0x00000001u}, {0x00000000u, 0x80000000u, 0x00000000u, 0x80000000u, 0x00000000u}, {0x00000000u, 0x80000000u, 0x00000000u, 0x80000000u, 0x00000000u}}, + {{0x80000001u, 0x00800002u}, {0x00800001u, 0x80800003u, 0x00800003u, 0x80000000u, 0x80000000u}, {0x00800002u, 0x80800002u, 0x00800002u, 0x80000000u, 0x00000000u}}, + {{0x80000001u, 0x80ffffffu}, {0x81000000u, 0x00fffffeu, 0x80fffffeu, 0x00000000u, 0x00000000u}, {0x80ffffffu, 0x00ffffffu, 0x80ffffffu, 0x00000000u, 0x00000000u}}, + {{0x80000002u, 0x3f800000u}, {0x3f800000u, 0xbf800000u, 0x3f800000u, 0x80000000u, 0x80000000u}, {0x3f800000u, 0xbf800000u, 0x3f800000u, 0x80000000u, 0x00000000u}}, + {{0x80000002u, 0xff7fffffu}, {0xff7fffffu, 0x7f7fffffu, 0xff7fffffu, 0x357fffffu, 0x357fffffu}, {0xff7fffffu, 0x7f7fffffu, 0xff7fffffu, 0x00000000u, 0x00000000u}}, + {{0x80000003u, 0x7f800000u}, {0x7f800000u, 0xff800000u, 0x7f800000u, 0xff800000u, 0xff800000u}, {0x7f800000u, 0xff800000u, 0x7f800000u, 0xffc00000u, 0x00000000u}}, + {{0x80400000u, 0x00000002u}, {0x80000000u, 0x80000000u, 0x00000000u, 0x80000000u, 0x80000000u}, {0x00000000u, 0x80000000u, 0x00000000u, 0x80000000u, 0x00000000u}}, + {{0x80400000u, 0x80000003u}, {0x80000000u, 0x80000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x80000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}}, + {{0x807fffffu, 0x00800000u}, {0x00000000u, 0x80ffffffu, 0x00ffffffu, 0x80000000u, 0x80000000u}, {0x00800000u, 0x80800000u, 0x00800000u, 0x80000000u, 0x00000000u}}, + {{0x807fffffu, 0x80800001u}, {0x81000000u, 0x00000000u, 0x80000000u, 0x00000000u, 0x00000000u}, {0x80800001u, 0x00800001u, 0x80800001u, 0x00000000u, 0x00000000u}}, + {{0x80800000u, 0x80000001u}, {0x80800001u, 0x80000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x80800000u, 0x80800000u, 0x00800000u, 0x00000000u, 0x00000000u}}, + {{0x80800001u, 0x33800000u}, {0x33800000u, 0xb3800000u, 0x33800000u, 0x80000000u, 0x80000000u}, {0x33800000u, 0xb3800000u, 0x33800000u, 0x80000001u, 0x80000001u}}, + {{0x80800002u, 0x00800000u}, {0x80000000u, 0x81000001u, 0x01000001u, 0x80000000u, 0x80000000u}, {0x80000002u, 0x81000001u, 0x01000001u, 0x80000000u, 0x80000000u}}, + {{0x80ffffffu, 0x00000002u}, {0x80fffffdu, 0x81000000u, 0x01000000u, 0x80000000u, 0x80000000u}, {0x80ffffffu, 0x80ffffffu, 0x00ffffffu, 0x80000000u, 0x00000000u}}, + {{0x80ffffffu, 0x80800002u}, {0x81400000u, 0x80000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x81400000u, 0x807ffffdu, 0x007ffffdu, 0x00000000u, 0x00000000u}}, + {{0x81000000u, 0x80000003u}, {0x81000002u, 0x80fffffdu, 0x00fffffdu, 0x00000000u, 0x00000000u}, {0x81000000u, 0x81000000u, 0x01000000u, 0x00000000u, 0x00000000u}}, + {{0x81000001u, 0x80000001u}, {0x81000002u, 0x81000000u, 0x01000000u, 0x00000000u, 0x00000000u}, {0x81000001u, 0x81000001u, 0x01000001u, 0x00000000u, 0x00000000u}}, + {{0xbf800000u, 0x807fffffu}, {0xbf800000u, 0xbf800000u, 0x3f800000u, 0x00000000u, 0x00000000u}, {0xbf800000u, 0xbf800000u, 0x3f800000u, 0x00000000u, 0x00000000u}}, + {{0xb4000000u, 0x00000003u}, {0xb4000000u, 0xb4000000u, 0x34000000u, 0x80000000u, 0x80000000u}, {0xb4000000u, 0xb4000000u, 0x34000000u, 0x80000000u, 0x00000000u}}, + {{0xff7fffffu, 0x00000003u}, {0xff7fffffu, 0xff7fffffu, 0x7f7fffffu, 0xb5bfffffu, 0xb5bfffffu}, {0xff7fffffu, 0xff7fffffu, 0x7f7fffffu, 0x80000000u, 0x00000000u}}, +}}; + +constexpr std::array, 24> TinyProductVectors{{ + {{0x3f7ffffeu, 0x00800001u}, {0x00800000u, 0x00800000u}, {0x00800000u, 0x00800000u}}, + {{0x3f7ffffeu, 0x80800001u}, {0x80800000u, 0x80800000u}, {0x80800000u, 0x80800000u}}, + {{0xbf7ffffeu, 0x00800001u}, {0x80800000u, 0x80800000u}, {0x80800000u, 0x80800000u}}, + {{0xbf7ffffeu, 0x80800001u}, {0x00800000u, 0x00800000u}, {0x00800000u, 0x00800000u}}, + {{0x3f800001u, 0x007fffffu}, {0x00800000u, 0x00800000u}, {0x00000000u, 0x00000000u}}, + {{0x3f800001u, 0x807fffffu}, {0x80800000u, 0x80800000u}, {0x80000000u, 0x00000000u}}, + {{0xbf800001u, 0x007fffffu}, {0x80800000u, 0x80800000u}, {0x80000000u, 0x00000000u}}, + {{0xbf800001u, 0x807fffffu}, {0x00800000u, 0x00800000u}, {0x00000000u, 0x00000000u}}, + {{0x3f800002u, 0x007ffffeu}, {0x00800000u, 0x00800000u}, {0x00000000u, 0x00000000u}}, + {{0x3f800002u, 0x807ffffeu}, {0x80800000u, 0x80800000u}, {0x80000000u, 0x00000000u}}, + {{0xbf800002u, 0x007ffffeu}, {0x80800000u, 0x80800000u}, {0x80000000u, 0x00000000u}}, + {{0xbf800002u, 0x807ffffeu}, {0x00800000u, 0x00800000u}, {0x00000000u, 0x00000000u}}, + {{0x3f7ffffeu, 0x00800000u}, {0x00000000u, 0x00000000u}, {0x007fffffu, 0x007fffffu}}, + {{0xbf7fffffu, 0x00400000u}, {0x80000000u, 0x80000000u}, {0x80000000u, 0x00000000u}}, + {{0x3f800001u, 0x80000001u}, {0x80000000u, 0x80000000u}, {0x80000000u, 0x00000000u}}, + {{0xbfffffffu, 0x00400000u}, {0x80000000u, 0x80000000u}, {0x80000000u, 0x00000000u}}, + {{0x3f400000u, 0x00800000u}, {0x00000000u, 0x00000000u}, {0x00600000u, 0x00600000u}}, + {{0x40000000u, 0x807fffffu}, {0x80fffffeu, 0x80fffffeu}, {0x80000000u, 0x00000000u}}, + {{0xcb000000u, 0x007ffffeu}, {0x8bfffffcu, 0x8bfffffcu}, {0x80000000u, 0x00000000u}}, + {{0xcb7fffffu, 0x807ffffeu}, {0x0c7ffffbu, 0x0c7ffffbu}, {0x00000000u, 0x00000000u}}, + {{0x5f800000u, 0x00400000u}, {0x20000000u, 0x20000000u}, {0x00000000u, 0x00000000u}}, + {{0x7f000000u, 0x80400000u}, {0xbf800000u, 0xbf800000u}, {0x80000000u, 0x00000000u}}, + {{0xb3800000u, 0x00ffffffu}, {0x80000000u, 0x80000000u}, {0x80000001u, 0x80000001u}}, + {{0xb4000000u, 0x80800000u}, {0x00000000u, 0x00000000u}, {0x00000001u, 0x00000001u}}, +}}; + +constexpr std::array, 32> ConstantFactorVectors{{ + {{0x00000001u, 0x00000001u}, {0x00000000u, 0x00000000u}, {0x00000000u, 0x00000000u}}, + {{0x00000002u, 0x00000001u}, {0x00000000u, 0x00000000u}, {0x00000000u, 0x00000000u}}, + {{0x00000003u, 0x00000001u}, {0x00000000u, 0x00000000u}, {0x00000000u, 0x00000000u}}, + {{0x00155555u, 0x00000001u}, {0x00000000u, 0x00000000u}, {0x00000000u, 0x00000000u}}, + {{0x003fffffu, 0x00000001u}, {0x00000000u, 0x00000000u}, {0x00000000u, 0x00000000u}}, + {{0x00400000u, 0x00000001u}, {0x00000000u, 0x00000000u}, {0x00000000u, 0x00000000u}}, + {{0x00400001u, 0x00000001u}, {0x00000000u, 0x00000000u}, {0x00000000u, 0x00000000u}}, + {{0x00555555u, 0x00000001u}, {0x00000000u, 0x00000000u}, {0x00000000u, 0x00000000u}}, + {{0x007ffffeu, 0x00000001u}, {0x00bffffdu, 0x00bffffdu}, {0x00000000u, 0x00000000u}}, + {{0x007fffffu, 0x00000001u}, {0x00bffffeu, 0x00bffffeu}, {0x00000000u, 0x00000000u}}, + {{0x00800000u, 0x00000001u}, {0x00c00000u, 0x00c00000u}, {0x00c00000u, 0x00c00000u}}, + {{0x00800001u, 0x00000001u}, {0x00c00002u, 0x00c00002u}, {0x00c00002u, 0x00c00002u}}, + {{0x00aaaaabu, 0x00000001u}, {0x01000000u, 0x01000000u}, {0x01000000u, 0x01000000u}}, + {{0x00ffffffu, 0x00000001u}, {0x013fffffu, 0x013fffffu}, {0x013fffffu, 0x013fffffu}}, + {{0x01000000u, 0x00000001u}, {0x01400000u, 0x01400000u}, {0x01400000u, 0x01400000u}}, + {{0x3f800000u, 0x00000001u}, {0x3fc00000u, 0x3fc00000u}, {0x3fc00000u, 0x3fc00000u}}, + {{0x00000000u, 0x00000001u}, {0x00000000u, 0x00000000u}, {0x00000000u, 0x00000000u}}, + {{0x80000001u, 0x00000001u}, {0x80000000u, 0x80000000u}, {0x80000000u, 0x00000000u}}, + {{0x80000002u, 0x00000001u}, {0x80000000u, 0x80000000u}, {0x80000000u, 0x00000000u}}, + {{0x80000003u, 0x00000001u}, {0x80000000u, 0x80000000u}, {0x80000000u, 0x00000000u}}, + {{0x80155555u, 0x00000001u}, {0x80000000u, 0x80000000u}, {0x80000000u, 0x00000000u}}, + {{0x803fffffu, 0x00000001u}, {0x80000000u, 0x80000000u}, {0x80000000u, 0x00000000u}}, + {{0x80400000u, 0x00000001u}, {0x80000000u, 0x80000000u}, {0x80000000u, 0x00000000u}}, + {{0x80400001u, 0x00000001u}, {0x80000000u, 0x80000000u}, {0x80000000u, 0x00000000u}}, + {{0x80555555u, 0x00000001u}, {0x80000000u, 0x80000000u}, {0x80000000u, 0x00000000u}}, + {{0x807ffffeu, 0x00000001u}, {0x80bffffdu, 0x80bffffdu}, {0x80000000u, 0x00000000u}}, + {{0x807fffffu, 0x00000001u}, {0x80bffffeu, 0x80bffffeu}, {0x80000000u, 0x00000000u}}, + {{0x80800000u, 0x00000001u}, {0x80c00000u, 0x80c00000u}, {0x80c00000u, 0x80c00000u}}, + {{0x80800001u, 0x00000001u}, {0x80c00002u, 0x80c00002u}, {0x80c00002u, 0x80c00002u}}, + {{0x80aaaaabu, 0x00000001u}, {0x81000000u, 0x81000000u}, {0x81000000u, 0x81000000u}}, + {{0x80ffffffu, 0x00000001u}, {0x813fffffu, 0x813fffffu}, {0x813fffffu, 0x813fffffu}}, + {{0x81000000u, 0x00000001u}, {0x81400000u, 0x81400000u}, {0x81400000u, 0x81400000u}}, +}}; + +constexpr std::uint32_t FirstTinyProductLane = static_cast(ArithmeticVectors.size()); +constexpr std::uint32_t FirstConstantFactorLane = FirstTinyProductLane + static_cast(TinyProductVectors.size()); +static_assert(FirstConstantFactorLane + ConstantFactorVectors.size() <= Threads); + +std::array BufferDescriptor(const void* data, std::uint32_t bytes) { + const auto address = reinterpret_cast(data); + return {static_cast(address), static_cast((address >> 32u) & 0xffffu), bytes, 0x31016facu}; +} + +std::string Hex(std::uint32_t value) { + char text[16]; + std::snprintf(text, sizeof(text), "0x%08x", value); + return text; +} + +template +void Fill(const std::array, Size>& vectors, std::uint32_t firstLane) { + for (std::uint32_t row = 0; row < Size; ++row) { + Input[(firstLane + row) * Inputs] = vectors[row].operands[0]; + Input[(firstLane + row) * Inputs + 1u] = vectors[row].operands[1]; + } +} + +bool Run(AgcDriver::VulkanDevice& device, ShaderRecompiler::ShaderFloatMode floatMode) { + Input.fill(0u); + Fill(ArithmeticVectors, 0u); + Fill(TinyProductVectors, FirstTinyProductLane); + Fill(ConstantFactorVectors, FirstConstantFactorLane); + Output.fill(0xdeadbeefu); + std::vector userData(8, 0u); + const auto input = BufferDescriptor(Input.data(), static_cast(Input.size() * 4u)); + const auto output = BufferDescriptor(Output.data(), static_cast(Output.size() * 4u)); + std::copy(input.begin(), input.end(), userData.begin()); + std::copy(output.begin(), output.end(), userData.begin() + 4); + const std::span code(Code); + const std::array memory{{{reinterpret_cast(code.data()), std::as_bytes(code)}}}; + const ShaderRecompiler::ShaderComputeStageInfo compute{{Threads, 1, 1}, 0, {false, false, false}, false, 1}; + ShaderRecompiler::RecompileRequest request{ + {ShaderStage::Compute, reinterpret_cast(code.data()), code, 0, {}}, + {32, 0, userData, compute, std::nullopt, std::nullopt, memory}, + device.Target(), + {0, 0, 0, 128} + }; + request.useCache = false; + request.context.floatMode = floatMode; + std::optional result; + try { + result.emplace(ShaderRecompiler::Recompile(request)); + } catch (const std::exception& error) { + const std::string message = error.what(); + const bool keepsDenormals = request.target.float32DenormPreserve && !request.target.float32FlushToZero; + Require(!keepsDenormals && message.find("needs DenormPreserve 32") != std::string::npos, "f32 add and multiply FLOAT_MODE " + Hex(floatMode.floatMode) + ": " + message); + return false; + } + device.Dispatch(*result, 1, 1, 1, {}, reinterpret_cast(code.data())); + device.WaitIdle(); + return true; +} + +template +void Check(const std::array, Size>& vectors, std::uint32_t firstLane, std::uint32_t firstInstruction, std::uint32_t denormals, bool ieee) { + for (std::uint32_t row = 0; row < Size; ++row) { + const auto& vector = vectors[row]; + for (std::uint32_t index = 0; index < Count; ++index) { + const std::uint32_t instruction = firstInstruction + index; + const auto actual = Output[(firstLane + row) * Results + instruction]; + const auto expected = denormals == 1u ? vector.outputsFlushed[index] : vector.inputsFlushed[index]; + Require(actual == expected, std::string(Names[instruction]) + " f32 denormal mode " + std::to_string(denormals) + (ieee ? ", IEEE=1" : ", IEEE=0") + " of " + + Hex(vector.operands[0]) + ", " + Hex(vector.operands[1]) + " is " + Hex(actual) + ", expected " + Hex(expected)); + } + } +} + +} + +int main() { + try { + const auto device = OpenVulkanTestDevice(); + if (!device) return VulkanTestSkipped; + std::uint32_t checked = 0u; + for (const std::uint32_t denormals : {1u, 2u}) { + for (const bool ieee : {false, true}) { + if (!Run(*device, ShaderRecompiler::ShaderFloatMode{0xc0u | denormals << 4u, true, ieee, false})) continue; + ++checked; + Check(ArithmeticVectors, 0u, 0u, denormals, ieee); + Check(TinyProductVectors, FirstTinyProductLane, FirstMultiply, denormals, ieee); + Check(ConstantFactorVectors, FirstConstantFactorLane, FirstConstantFactor, denormals, ieee); + } + } + std::printf("f32 add and multiply denormal mode 1 and 2 tests passed (%u of 4 modes run; the others need DenormPreserve 32)\n", checked); + return 0; + } catch (const std::exception& error) { + std::cerr << error.what() << '\n'; + return 1; + } +} diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/F32MinMaxDenormalModes.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/F32MinMaxDenormalModes.cpp new file mode 100644 index 0000000000..a9d0238d0b --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/tests/execution/F32MinMaxDenormalModes.cpp @@ -0,0 +1,181 @@ +#include "prx/libSceAgcDriver/Execution/include/VulkanDevice.hpp" +#include "prx/libSceAgcDriver/Graphics/include/Draw.hpp" +#include "Recompiler.hpp" +#include "VulkanTestDevice.hpp" +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace { + +using AgcDriver::Graphics::Require; +using ShaderRecompiler::ShaderStage; + +constexpr std::uint32_t Threads = 64; +constexpr std::uint32_t Inputs = 4; +constexpr std::uint32_t Results = 16; +constexpr std::uint32_t Instructions = 5; +alignas(256) std::array Input{}; +alignas(256) std::array Output{}; + +alignas(256) constexpr std::array Code{ + 0x34020084u, 0x34040086u, 0xe0381000u, 0x80000401u, 0xbf8c3f70u, 0x1e140b04u, 0x20160b04u, 0xd551000cu, + 0x041a0b04u, 0xd554000du, 0x041a0b04u, 0xd557000eu, 0x041a0b04u, 0xe0781000u, 0x80010a02u, 0xe0701010u, + 0x80010e02u, 0xbf810000u, +}; + +constexpr const char* Names[Instructions] = {"v_min_f32", "v_max_f32", "v_min3_f32", "v_max3_f32", "v_med3_f32"}; + +struct MinMaxVector { + std::uint32_t operands[3]; + std::uint32_t inputsKept[Instructions]; + std::uint32_t inputsFlushed[Instructions]; +}; + +constexpr std::array Vectors{{ + {0x00000000u, 0x00000000u, 0x00000001u, {0x00000000u, 0x00000000u, 0x00000000u, 0x00000001u, 0x00000000u}, {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}}, + {0x00000000u, 0x80000001u, 0x80000000u, {0x80000001u, 0x00000000u, 0x80000001u, 0x00000000u, 0x80000000u}, {0x80000000u, 0x00000000u, 0x80000000u, 0x00000000u, 0x80000000u}}, + {0x00000000u, 0x007fffffu, 0x7f800000u, {0x00000000u, 0x007fffffu, 0x00000000u, 0x7f800000u, 0x007fffffu}, {0x00000000u, 0x00000000u, 0x00000000u, 0x7f800000u, 0x00000000u}}, + {0x00000000u, 0x80800000u, 0x807fffffu, {0x80800000u, 0x00000000u, 0x80800000u, 0x00000000u, 0x807fffffu}, {0x80800000u, 0x00000000u, 0x80800000u, 0x00000000u, 0x80000000u}}, + {0x80000000u, 0x80000000u, 0x00000001u, {0x80000000u, 0x80000000u, 0x80000000u, 0x00000001u, 0x80000000u}, {0x80000000u, 0x80000000u, 0x80000000u, 0x00000000u, 0x80000000u}}, + {0x80000000u, 0x80000001u, 0x807fffffu, {0x80000001u, 0x80000000u, 0x807fffffu, 0x80000000u, 0x80000001u}, {0x80000000u, 0x80000000u, 0x80000000u, 0x80000000u, 0x80000000u}}, + {0x80000000u, 0x807fffffu, 0x00000001u, {0x807fffffu, 0x80000000u, 0x807fffffu, 0x00000001u, 0x80000000u}, {0x80000000u, 0x80000000u, 0x80000000u, 0x00000000u, 0x80000000u}}, + {0x80000000u, 0x3f800000u, 0x807fffffu, {0x80000000u, 0x3f800000u, 0x807fffffu, 0x3f800000u, 0x80000000u}, {0x80000000u, 0x3f800000u, 0x80000000u, 0x3f800000u, 0x80000000u}}, + {0x00000001u, 0x00000000u, 0x3f800000u, {0x00000000u, 0x00000001u, 0x00000000u, 0x3f800000u, 0x00000001u}, {0x00000000u, 0x00000000u, 0x00000000u, 0x3f800000u, 0x00000000u}}, + {0x00000001u, 0x00000001u, 0x807fffffu, {0x00000001u, 0x00000001u, 0x807fffffu, 0x00000001u, 0x00000001u}, {0x00000000u, 0x00000000u, 0x80000000u, 0x00000000u, 0x00000000u}}, + {0x00000001u, 0x007fffffu, 0x00000001u, {0x00000001u, 0x007fffffu, 0x00000001u, 0x007fffffu, 0x00000001u}, {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}}, + {0x00000001u, 0x807fffffu, 0xff800000u, {0x807fffffu, 0x00000001u, 0xff800000u, 0x00000001u, 0x807fffffu}, {0x80000000u, 0x00000000u, 0xff800000u, 0x00000000u, 0x80000000u}}, + {0x00000001u, 0x80800000u, 0x3f800000u, {0x80800000u, 0x00000001u, 0x80800000u, 0x3f800000u, 0x00000001u}, {0x80800000u, 0x00000000u, 0x80800000u, 0x3f800000u, 0x00000000u}}, + {0x00000001u, 0xbf800000u, 0x807fffffu, {0xbf800000u, 0x00000001u, 0xbf800000u, 0x00000001u, 0x807fffffu}, {0xbf800000u, 0x00000000u, 0xbf800000u, 0x00000000u, 0x80000000u}}, + {0x00000001u, 0xff800000u, 0x00000001u, {0xff800000u, 0x00000001u, 0xff800000u, 0x00000001u, 0x00000001u}, {0xff800000u, 0x00000000u, 0xff800000u, 0x00000000u, 0x00000000u}}, + {0x80000001u, 0x00000000u, 0xff800000u, {0x80000001u, 0x00000000u, 0xff800000u, 0x00000000u, 0x80000001u}, {0x80000000u, 0x00000000u, 0xff800000u, 0x00000000u, 0x80000000u}}, + {0x80000001u, 0x00000001u, 0x3f800000u, {0x80000001u, 0x00000001u, 0x80000001u, 0x3f800000u, 0x00000001u}, {0x80000000u, 0x00000000u, 0x80000000u, 0x3f800000u, 0x00000000u}}, + {0x80000001u, 0x007fffffu, 0x807fffffu, {0x80000001u, 0x007fffffu, 0x807fffffu, 0x007fffffu, 0x80000001u}, {0x80000000u, 0x00000000u, 0x80000000u, 0x00000000u, 0x80000000u}}, + {0x80000001u, 0x00800000u, 0x00000001u, {0x80000001u, 0x00800000u, 0x80000001u, 0x00800000u, 0x00000001u}, {0x80000000u, 0x00800000u, 0x80000000u, 0x00800000u, 0x00000000u}}, + {0x80000001u, 0x80800000u, 0xff800000u, {0x80800000u, 0x80000001u, 0xff800000u, 0x80000001u, 0x80800000u}, {0x80800000u, 0x80000000u, 0xff800000u, 0x80000000u, 0x80800000u}}, + {0x80000001u, 0xbf800000u, 0x3f800000u, {0xbf800000u, 0x80000001u, 0xbf800000u, 0x3f800000u, 0x80000001u}, {0xbf800000u, 0x80000000u, 0xbf800000u, 0x3f800000u, 0x80000000u}}, + {0x80000001u, 0xff800000u, 0x807fffffu, {0xff800000u, 0x80000001u, 0xff800000u, 0x80000001u, 0x807fffffu}, {0xff800000u, 0x80000000u, 0xff800000u, 0x80000000u, 0x80000000u}}, + {0x007fffffu, 0x80000000u, 0x00000001u, {0x80000000u, 0x007fffffu, 0x80000000u, 0x007fffffu, 0x00000001u}, {0x80000000u, 0x00000000u, 0x80000000u, 0x00000000u, 0x00000000u}}, + {0x007fffffu, 0x00000001u, 0xff800000u, {0x00000001u, 0x007fffffu, 0xff800000u, 0x007fffffu, 0x00000001u}, {0x00000000u, 0x00000000u, 0xff800000u, 0x00000000u, 0x00000000u}}, + {0x007fffffu, 0x007fffffu, 0x3f800000u, {0x007fffffu, 0x007fffffu, 0x007fffffu, 0x3f800000u, 0x007fffffu}, {0x00000000u, 0x00000000u, 0x00000000u, 0x3f800000u, 0x00000000u}}, + {0x007fffffu, 0x00800000u, 0x807fffffu, {0x007fffffu, 0x00800000u, 0x807fffffu, 0x00800000u, 0x007fffffu}, {0x00000000u, 0x00800000u, 0x80000000u, 0x00800000u, 0x00000000u}}, + {0x007fffffu, 0x3f800000u, 0x00000001u, {0x007fffffu, 0x3f800000u, 0x00000001u, 0x3f800000u, 0x007fffffu}, {0x00000000u, 0x3f800000u, 0x00000000u, 0x3f800000u, 0x00000000u}}, + {0x007fffffu, 0xbf800000u, 0xff800000u, {0xbf800000u, 0x007fffffu, 0xff800000u, 0x007fffffu, 0xbf800000u}, {0xbf800000u, 0x00000000u, 0xff800000u, 0x00000000u, 0xbf800000u}}, + {0x007fffffu, 0xff800000u, 0x3f800000u, {0xff800000u, 0x007fffffu, 0xff800000u, 0x3f800000u, 0x007fffffu}, {0xff800000u, 0x00000000u, 0xff800000u, 0x3f800000u, 0x00000000u}}, + {0x807fffffu, 0x80000000u, 0x807fffffu, {0x807fffffu, 0x80000000u, 0x807fffffu, 0x80000000u, 0x807fffffu}, {0x80000000u, 0x80000000u, 0x80000000u, 0x80000000u, 0x80000000u}}, + {0x807fffffu, 0x80000001u, 0x00000001u, {0x807fffffu, 0x80000001u, 0x807fffffu, 0x00000001u, 0x80000001u}, {0x80000000u, 0x80000000u, 0x80000000u, 0x00000000u, 0x80000000u}}, + {0x807fffffu, 0x007fffffu, 0xff800000u, {0x807fffffu, 0x007fffffu, 0xff800000u, 0x007fffffu, 0x807fffffu}, {0x80000000u, 0x00000000u, 0xff800000u, 0x00000000u, 0x80000000u}}, + {0x807fffffu, 0x00800000u, 0x3f800000u, {0x807fffffu, 0x00800000u, 0x807fffffu, 0x3f800000u, 0x00800000u}, {0x80000000u, 0x00800000u, 0x80000000u, 0x3f800000u, 0x00800000u}}, + {0x807fffffu, 0x3f800000u, 0x807fffffu, {0x807fffffu, 0x3f800000u, 0x807fffffu, 0x3f800000u, 0x807fffffu}, {0x80000000u, 0x3f800000u, 0x80000000u, 0x3f800000u, 0x80000000u}}, + {0x807fffffu, 0x7f800000u, 0x00000001u, {0x807fffffu, 0x7f800000u, 0x807fffffu, 0x7f800000u, 0x00000001u}, {0x80000000u, 0x7f800000u, 0x80000000u, 0x7f800000u, 0x00000000u}}, + {0x807fffffu, 0xff800000u, 0xff800000u, {0xff800000u, 0x807fffffu, 0xff800000u, 0x807fffffu, 0xff800000u}, {0xff800000u, 0x80000000u, 0xff800000u, 0x80000000u, 0xff800000u}}, + {0x00800000u, 0x80000001u, 0x00000000u, {0x80000001u, 0x00800000u, 0x80000001u, 0x00800000u, 0x00000000u}, {0x80000000u, 0x00800000u, 0x80000000u, 0x00800000u, 0x00000000u}}, + {0x00800000u, 0x007fffffu, 0xbf800000u, {0x007fffffu, 0x00800000u, 0xbf800000u, 0x00800000u, 0x007fffffu}, {0x00000000u, 0x00800000u, 0xbf800000u, 0x00800000u, 0x00000000u}}, + {0x00800000u, 0x80800000u, 0x007fffffu, {0x80800000u, 0x00800000u, 0x80800000u, 0x00800000u, 0x007fffffu}, {0x80800000u, 0x00800000u, 0x80800000u, 0x00800000u, 0x00000000u}}, + {0x80800000u, 0x00000000u, 0x807fffffu, {0x80800000u, 0x00000000u, 0x80800000u, 0x00000000u, 0x807fffffu}, {0x80800000u, 0x00000000u, 0x80800000u, 0x00000000u, 0x80000000u}}, + {0x80800000u, 0x80000001u, 0x007fffffu, {0x80800000u, 0x80000001u, 0x80800000u, 0x007fffffu, 0x80000001u}, {0x80800000u, 0x80000000u, 0x80800000u, 0x00000000u, 0x80000000u}}, + {0x80800000u, 0x807fffffu, 0x80000000u, {0x80800000u, 0x807fffffu, 0x80800000u, 0x80000000u, 0x807fffffu}, {0x80800000u, 0x80000000u, 0x80800000u, 0x80000000u, 0x80000000u}}, + {0x80800000u, 0x3f800000u, 0x007fffffu, {0x80800000u, 0x3f800000u, 0x80800000u, 0x3f800000u, 0x007fffffu}, {0x80800000u, 0x3f800000u, 0x80800000u, 0x3f800000u, 0x00000000u}}, + {0x3f800000u, 0x80000000u, 0x807fffffu, {0x80000000u, 0x3f800000u, 0x807fffffu, 0x3f800000u, 0x80000000u}, {0x80000000u, 0x3f800000u, 0x80000000u, 0x3f800000u, 0x80000000u}}, + {0x3f800000u, 0x80000001u, 0x3f800000u, {0x80000001u, 0x3f800000u, 0x80000001u, 0x3f800000u, 0x3f800000u}, {0x80000000u, 0x3f800000u, 0x80000000u, 0x3f800000u, 0x3f800000u}}, + {0x3f800000u, 0x807fffffu, 0x807fffffu, {0x807fffffu, 0x3f800000u, 0x807fffffu, 0x3f800000u, 0x807fffffu}, {0x80000000u, 0x3f800000u, 0x80000000u, 0x3f800000u, 0x80000000u}}, + {0x3f800000u, 0xbf800000u, 0x007fffffu, {0xbf800000u, 0x3f800000u, 0xbf800000u, 0x3f800000u, 0x007fffffu}, {0xbf800000u, 0x3f800000u, 0xbf800000u, 0x3f800000u, 0x00000000u}}, + {0xbf800000u, 0x00000001u, 0x80000001u, {0xbf800000u, 0x00000001u, 0xbf800000u, 0x00000001u, 0x80000001u}, {0xbf800000u, 0x00000000u, 0xbf800000u, 0x00000000u, 0x80000000u}}, + {0xbf800000u, 0x007fffffu, 0x00000000u, {0xbf800000u, 0x007fffffu, 0xbf800000u, 0x007fffffu, 0x00000000u}, {0xbf800000u, 0x00000000u, 0xbf800000u, 0x00000000u, 0x00000000u}}, + {0xbf800000u, 0x807fffffu, 0xbf800000u, {0xbf800000u, 0x807fffffu, 0xbf800000u, 0x807fffffu, 0xbf800000u}, {0xbf800000u, 0x80000000u, 0xbf800000u, 0x80000000u, 0xbf800000u}}, + {0xbf800000u, 0x7f800000u, 0x007fffffu, {0xbf800000u, 0x7f800000u, 0xbf800000u, 0x7f800000u, 0x007fffffu}, {0xbf800000u, 0x7f800000u, 0xbf800000u, 0x7f800000u, 0x00000000u}}, + {0x7f800000u, 0x00000001u, 0x80800000u, {0x00000001u, 0x7f800000u, 0x80800000u, 0x7f800000u, 0x00000001u}, {0x00000000u, 0x7f800000u, 0x80800000u, 0x7f800000u, 0x00000000u}}, + {0x7f800000u, 0x007fffffu, 0x007fffffu, {0x007fffffu, 0x7f800000u, 0x007fffffu, 0x7f800000u, 0x007fffffu}, {0x00000000u, 0x7f800000u, 0x00000000u, 0x7f800000u, 0x00000000u}}, + {0x7f800000u, 0x00800000u, 0x80000001u, {0x00800000u, 0x7f800000u, 0x80000001u, 0x7f800000u, 0x00800000u}, {0x00800000u, 0x7f800000u, 0x80000000u, 0x7f800000u, 0x00800000u}}, + {0x7f800000u, 0xff800000u, 0x007fffffu, {0xff800000u, 0x7f800000u, 0xff800000u, 0x7f800000u, 0x007fffffu}, {0xff800000u, 0x7f800000u, 0xff800000u, 0x7f800000u, 0x00000000u}}, + {0xff800000u, 0x00000001u, 0xff800000u, {0xff800000u, 0x00000001u, 0xff800000u, 0x00000001u, 0xff800000u}, {0xff800000u, 0x00000000u, 0xff800000u, 0x00000000u, 0xff800000u}}, +}}; + +std::array BufferDescriptor(const void* data, std::uint32_t bytes) { + const auto address = reinterpret_cast(data); + return {static_cast(address), static_cast((address >> 32u) & 0xffffu), bytes, 0x31016facu}; +} + +std::string Hex(std::uint32_t value) { + char text[16]; + std::snprintf(text, sizeof(text), "0x%08x", value); + return text; +} + +bool Run(AgcDriver::VulkanDevice& device, ShaderRecompiler::ShaderFloatMode floatMode) { + Input.fill(0u); + for (std::uint32_t lane = 0; lane < Vectors.size(); ++lane) { + std::copy(std::begin(Vectors[lane].operands), std::end(Vectors[lane].operands), Input.begin() + lane * Inputs); + } + Output.fill(0xdeadbeefu); + std::vector userData(8, 0u); + const auto input = BufferDescriptor(Input.data(), static_cast(Input.size() * 4u)); + const auto output = BufferDescriptor(Output.data(), static_cast(Output.size() * 4u)); + std::copy(input.begin(), input.end(), userData.begin()); + std::copy(output.begin(), output.end(), userData.begin() + 4); + const std::span code(Code); + const std::array memory{{{reinterpret_cast(code.data()), std::as_bytes(code)}}}; + const ShaderRecompiler::ShaderComputeStageInfo compute{{Threads, 1, 1}, 0, {false, false, false}, false, 1}; + ShaderRecompiler::RecompileRequest request{ + {ShaderStage::Compute, reinterpret_cast(code.data()), code, 0, {}}, + {32, 0, userData, compute, std::nullopt, std::nullopt, memory}, + device.Target(), + {0, 0, 0, 128} + }; + request.useCache = false; + request.context.floatMode = floatMode; + std::optional result; + try { + result.emplace(ShaderRecompiler::Recompile(request)); + } catch (const std::exception& error) { + const std::string message = error.what(); + const bool keepsDenormals = request.target.float32DenormPreserve && !request.target.float32FlushToZero; + Require(!keepsDenormals && ((floatMode.floatMode >> 4u) & 3u) == 1u && message.find("needs DenormPreserve 32") != std::string::npos, + "f32 min/max FLOAT_MODE " + Hex(floatMode.floatMode) + ": " + message); + return false; + } + device.Dispatch(*result, 1, 1, 1, {}, reinterpret_cast(code.data())); + device.WaitIdle(); + return true; +} + +void Check(std::uint32_t denormals, bool ieee) { + for (std::uint32_t lane = 0; lane < Vectors.size(); ++lane) { + const auto& vector = Vectors[lane]; + for (std::uint32_t instruction = 0; instruction < Instructions; ++instruction) { + const auto actual = Output[lane * Results + instruction]; + const auto expected = denormals == 1u ? vector.inputsKept[instruction] : vector.inputsFlushed[instruction]; + Require(actual == expected, std::string(Names[instruction]) + " f32 denormal mode " + std::to_string(denormals) + (ieee ? ", IEEE=1" : ", IEEE=0") + " of " + + Hex(vector.operands[0]) + ", " + Hex(vector.operands[1]) + ", " + Hex(vector.operands[2]) + " is " + Hex(actual) + + ", expected " + Hex(expected)); + } + } +} + +} + +int main() { + try { + const auto device = OpenVulkanTestDevice(); + if (!device) return VulkanTestSkipped; + std::uint32_t checked = 0u; + for (const std::uint32_t denormals : {1u, 2u}) { + for (const bool ieee : {false, true}) { + if (!Run(*device, ShaderRecompiler::ShaderFloatMode{0xc0u | denormals << 4u, true, ieee, false})) continue; + ++checked; + Check(denormals, ieee); + } + } + std::printf("f32 min/max denormal mode 1 and 2 tests passed (%u of 4 modes run; mode 1 needs DenormPreserve 32)\n", checked); + return 0; + } catch (const std::exception& error) { + std::cerr << error.what() << '\n'; + return 1; + } +} diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/FixtureDeviceSelection.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/FixtureDeviceSelection.cpp new file mode 100644 index 0000000000..5d3e6ebf24 --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/tests/execution/FixtureDeviceSelection.cpp @@ -0,0 +1,106 @@ +#include "FixtureDeviceSelection.hpp" +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace { + +void Require(bool condition, const std::string& message) { + if (!condition) throw std::runtime_error(message); +} + +void SetRequest(const char* value) { +#ifdef _WIN32 + if (_putenv_s("ANYPS5_GPU", value) != 0) throw std::runtime_error("cannot set ANYPS5_GPU"); +#else + if (setenv("ANYPS5_GPU", value, 1) != 0) throw std::runtime_error("cannot set ANYPS5_GPU"); +#endif +} + +VkPhysicalDeviceProperties Properties(const char* name, VkPhysicalDeviceType type) { + VkPhysicalDeviceProperties result{}; + result.apiVersion = VK_API_VERSION_1_1; + result.deviceType = type; + std::memcpy(result.deviceName, name, std::strlen(name) + 1u); + return result; +} + +std::array described{ + Properties("llvmpipe (LLVM 20.1.2, 256 bits)", VK_PHYSICAL_DEVICE_TYPE_CPU), + Properties("AMD Radeon(TM) Graphics", VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU), + Properties("NVIDIA GeForce RTX 5080", VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU), +}; + +VkPhysicalDevice Handle(std::size_t index) { + return reinterpret_cast(static_cast(index + 1u)); +} + +void VKAPI_CALL Describe(VkPhysicalDevice device, VkPhysicalDeviceProperties* properties) { + *properties = described[reinterpret_cast(device) - 1u]; +} + +std::string SelectionError(const char* request) { + SetRequest(request); + try { + static_cast(SelectFixturePhysicalDevice(std::array{Handle(0), Handle(1), Handle(2)}, Describe)); + } catch (const std::runtime_error& error) { + return error.what(); + } + throw std::runtime_error(std::string("ANYPS5_GPU=\"") + request + "\" selected a device"); +} + +void SelectionTests() { + std::array devices = described; + Require(!SelectFixtureDevice({}, {}).has_value(), "an empty device list selected a device"); + Require(SelectFixtureDevice(devices, {}) == 2u, "default selection did not prefer the discrete GPU"); + Require(SelectFixtureDevice(std::span(devices).first(2), {}) == 1u, "default selection did not prefer the integrated GPU to the CPU device"); + Require(SelectFixtureDevice(devices, "AMD") == 1u, "an AMD request lost to the discrete GPU"); + Require(SelectFixtureDevice(devices, "aMd rAdEoN") == 1u, "GPU name matching was not case-insensitive"); + Require(SelectFixtureDevice(devices, "RTX 5080") == 2u, "partial GPU name matching failed"); + Require(SelectFixtureDevice(devices, "llvmpipe") == 0u, "a requested CPU device was not selected"); + Require(!SelectFixtureDevice(devices, "missing device").has_value(), "an unmatched GPU request selected a device"); + Require(!SelectFixtureDevice(devices, "NVIDIA GeForce RTX 5080 with a longer name").has_value(), "a request longer than the device name matched"); + const std::array radeons{ + Properties("AMD Radeon(TM) Graphics", VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU), + Properties("AMD Radeon RX 7900 XTX", VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU), + }; + Require(SelectFixtureDevice(radeons, {}) == 1u, "default selection did not prefer the discrete Radeon"); + Require(SelectFixtureDevice(radeons, "radeon") == 0u, "a request matching an integrated and a later discrete GPU did not take the first match"); + devices[0] = devices[1]; + Require(SelectFixtureDevice(devices, "AMD") == 0u, "equally ranked matches did not keep enumeration order"); + devices[1].apiVersion = VK_API_VERSION_1_0; + devices[2].apiVersion = VK_API_VERSION_1_0; + Require(!SelectFixtureDevice(devices, "NVIDIA").has_value(), "a requested device without Vulkan 1.1 was selected"); +} + +void EnvironmentTests() { + const std::array handles{Handle(0), Handle(1), Handle(2)}; + SetRequest(""); + Require(SelectFixturePhysicalDevice(handles, Describe) == Handle(2), "an empty ANYPS5_GPU did not select the discrete GPU"); + SetRequest("radeon"); + Require(SelectFixturePhysicalDevice(handles, Describe) == Handle(1), "ANYPS5_GPU=radeon did not select the integrated GPU"); + SetRequest("LLVMPIPE"); + Require(SelectFixturePhysicalDevice(handles, Describe) == Handle(0), "ANYPS5_GPU=LLVMPIPE did not select the CPU device"); + const std::string error = SelectionError("no such device 1d3d154f"); + Require(error == "no Vulkan 1.1 device whose name contains ANYPS5_GPU=\"no such device 1d3d154f\"; devices: llvmpipe (LLVM 20.1.2, 256 bits), AMD Radeon(TM) Graphics, NVIDIA GeForce RTX 5080", "unexpected error: " + error); +} + +} + +int main() { + try { + SelectionTests(); + EnvironmentTests(); + std::puts("fixture device selection tests passed"); + return 0; + } catch (const std::exception& error) { + std::cerr << error.what() << '\n'; + return 1; + } +} diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/FixtureDeviceSelection.hpp b/core/libs/prx/libSceAgcDriver/tests/execution/FixtureDeviceSelection.hpp new file mode 100644 index 0000000000..7bc494e0ec --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/tests/execution/FixtureDeviceSelection.hpp @@ -0,0 +1,57 @@ +#ifndef CORE_LIBS_PRX_LIBSCEAGCDRIVER_TESTS_EXECUTION_FIXTUREDEVICESELECTION_HPP +#define CORE_LIBS_PRX_LIBSCEAGCDRIVER_TESTS_EXECUTION_FIXTUREDEVICESELECTION_HPP + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +inline std::optional SelectFixtureDevice(std::span devices, std::string_view request) { + const auto rankDeviceType = [](VkPhysicalDeviceType type) { + switch (type) { + case VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU: return 3; + case VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU: return 2; + case VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU: return 1; + default: return 0; + } + }; + std::optional selected; + int selectedRank = -1; + for (std::size_t index = 0; index < devices.size(); ++index) { + const auto& candidate = devices[index]; + if (candidate.apiVersion < VK_API_VERSION_1_1) continue; + const std::string_view name(candidate.deviceName); + if (!request.empty() && std::search(name.begin(), name.end(), request.begin(), request.end(), [](char left, char right) { + return std::tolower(static_cast(left)) == std::tolower(static_cast(right)); + }) == name.end()) continue; + const int rank = request.empty() ? rankDeviceType(candidate.deviceType) : 4; + if (rank <= selectedRank) continue; + selected = index; + selectedRank = rank; + } + return selected; +} + +inline VkPhysicalDevice SelectFixturePhysicalDevice(std::span devices, PFN_vkGetPhysicalDeviceProperties physicalProperties) { + std::vector properties(devices.size()); + for (std::size_t index = 0; index < devices.size(); ++index) physicalProperties(devices[index], &properties[index]); + const char* requestedName = std::getenv("ANYPS5_GPU"); + const std::string_view request = requestedName != nullptr ? requestedName : ""; + if (const auto selected = SelectFixtureDevice(properties, request)) return devices[*selected]; + if (request.empty()) throw std::runtime_error("no Vulkan 1.1 device"); + std::string names; + for (const auto& candidate : properties) { + if (!names.empty()) names += ", "; + names += candidate.deviceName; + } + throw std::runtime_error("no Vulkan 1.1 device whose name contains ANYPS5_GPU=\"" + std::string(request) + "\"; devices: " + names); +} + +#endif diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/GuardedNullPointer.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/GuardedNullPointer.cpp index 172c6a26aa..d05f480585 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/GuardedNullPointer.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/GuardedNullPointer.cpp @@ -270,7 +270,7 @@ void RunTests(AgcDriver::VulkanDevice& device) { Require(!Faulted(someLanes), "guarded pointer: a mapped pointer under EXEC faulted:\n" + someLanes.log); RequireWords(someLanes, 5u, Payload, "guarded pointer: a mapped pointer read under EXEC"); const auto noLanes = Run(device, ExecCode, 0u, 0u); - Require(!Faulted(noLanes) && noLanes.poisoned == 1u, "guarded pointer: a null pointer read skipped by s_cbranch_execz faulted:\n" + noLanes.log); + Require(!Faulted(noLanes) && noLanes.poisoned == 0u && noLanes.cacheable, "guarded pointer: a null pointer read skipped by s_cbranch_execz under an EXEC decided empty at capture faulted, was poisoned or left the caches:\n" + noLanes.log); RequireWords(noLanes, 0u, 0u, "guarded pointer: a read skipped by s_cbranch_execz"); const auto oneLane = Run(device, ExecCode, 0u, 1u); Require(Faulted(oneLane) && oneLane.log.find(Fault(0u, 0x34u)) != std::string::npos, "guarded pointer: a null pointer read with one EXEC lane did not fault:\n" + oneLane.log); diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/HostImportWindows.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/HostImportWindows.cpp index e6d9bd6b30..35e4f777cd 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/HostImportWindows.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/HostImportWindows.cpp @@ -21,6 +21,14 @@ #include #include +namespace AgcDriver { + +struct VulkanDeviceTestAccess { + static Graphics::Context ContextOf(const VulkanDevice& device) { return device.graphicsContext(); } +}; + +} + namespace { namespace Graphics = AgcDriver::Graphics; @@ -163,6 +171,41 @@ Outcome ResidentMappingsImportAfterCollection() { return Outcome::Passed; } +Outcome SmallMappingsImportOnFirstUse() { + GuestBlock small(GuestAlignment); + GuestBlock large(2 * GuestAlignment); + auto device = OpenVulkanTestDevice(); + if (!device) return Outcome::NoDevice; + const auto context = AgcDriver::VulkanDeviceTestAccess::ContextOf(*device); + if (context.hostImportAlignment == 0) return Outcome::NoImport; + for (auto* guest : {&small, &large}) { + Graphics::ClearHostImports(device->Device()); + GuestAllocations::Mapped ranges; + for (const auto& range : GuestAllocations::GuestAllocationsAcquire_nid_postfix()) { + if (range->address == guest->Address()) ranges.push_back(range); + } + Require(ranges.size() == 1, "mapped import test lost its registered range"); + { + std::lock_guard lock(AgcDriver::GuestMemory::GpuMutex()); + Require(Graphics::ImportMappedRanges(context, ranges, GuestAllocations::GuestAllocationsGeneration_nid_postfix(), true), "mapped import test could not adopt the device"); + } + Require(Graphics::HostImportCovers(context, guest->Address(), ProbeBytes) == (guest == &large), "mapping did not defer exactly the small import"); + Require(Fill(*device, guest->Address()), "first GPU use could not import the mapped buffer"); + Require(Graphics::HostImportCovers(context, guest->Address(), ProbeBytes), "first GPU use left the mapped buffer unimported"); + constexpr std::array pattern{0x11111111u, 0x22222222u, 0x33333333u, 0x44444444u}; + for (std::size_t offset = 0; offset < ProbeBytes; offset += sizeof(std::uint32_t)) { + std::uint32_t actual = 0; + std::memcpy(&actual, guest->Data() + offset, sizeof(actual)); + Require(actual == pattern[(offset / sizeof(actual)) % pattern.size()], "first GPU use changed the fill result"); + } + for (std::size_t offset = ProbeBytes; offset < GuestAlignment; ++offset) { + Require(guest->Data()[offset] == 0, "first GPU use overwrote bytes outside the fill"); + } + } + Graphics::ClearHostImports(device->Device()); + return Outcome::Passed; +} + } int main() { @@ -176,6 +219,7 @@ int main() { const auto windows = WindowsFollowTheirRange(); Require(windows == Outcome::Passed, "host import windows: the device stopped importing guest memory between two scenarios"); Require(ResidentMappingsImportAfterCollection() == Outcome::Passed, "resident import test failed"); + Require(SmallMappingsImportOnFirstUse() == Outcome::Passed, "mapped import test lost its Vulkan device or host import support"); std::puts("host import windows tests passed"); return 0; } catch (const std::exception& error) { diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/ImageResinfoDimension.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/ImageResinfoDimension.cpp new file mode 100644 index 0000000000..01fd310101 --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/tests/execution/ImageResinfoDimension.cpp @@ -0,0 +1,108 @@ +#include "prx/libSceAgcDriver/Execution/include/VulkanDevice.hpp" +#include "prx/libSceAgcDriver/Graphics/include/Draw.hpp" +#include "Recompiler.hpp" +#include "VulkanTestDevice.hpp" +#include +#include +#include +#include +#include +#include +#include +#include + +namespace { + +using AgcDriver::Graphics::Require; +using ShaderRecompiler::ShaderStage; + +constexpr std::uint32_t Threads = 32; +constexpr std::uint32_t Size = 8; +constexpr std::uint32_t Format32Float = 22; +constexpr std::uint32_t Type3D = 10; +constexpr std::uint32_t TypeCube = 11; +constexpr std::uint32_t Type2DArray = 13; +alignas(256) std::array Output{}; +alignas(256) std::array Texels{}; + +constexpr std::array Code(std::uint32_t dim) { + return {0x34060084u, 0x7e020280u, 0xf0380f00u | (dim << 3u), 0x00010a01u, 0xbf8c3f70u, 0xe0781000u, 0x80000a03u, 0xbf810000u}; +} + +alignas(256) constexpr auto Query1D = Code(0u); +alignas(256) constexpr auto Query3D = Code(2u); +alignas(256) constexpr auto QueryCube = Code(3u); +alignas(256) constexpr auto Query2DArray = Code(5u); + +std::array BufferDescriptor(const void* data, std::uint32_t bytes) { + const auto address = reinterpret_cast(data); + return {static_cast(address), static_cast((address >> 32u) & 0xffffu), bytes, 0x31016facu}; +} + +std::array Descriptor(std::uint32_t type, std::uint32_t depth) { + const auto address = static_cast(reinterpret_cast(Texels.data())); + return { + static_cast(address >> 8u), + static_cast((address >> 40u) & 0xffu) | (Format32Float << 20u) | (((Size - 1u) & 3u) << 30u), + ((Size - 1u) >> 2u) | ((Size - 1u) << 14u), + 0xfacu | (type << 28u), + depth - 1u, + 0u, + 0u, + 0u, + }; +} + +std::array Query(AgcDriver::VulkanDevice& device, std::span code, const std::array& texture) { + Output.fill(0xdeadbeefu); + std::vector userData(16, 0u); + const auto output = BufferDescriptor(Output.data(), static_cast(Output.size() * 4u)); + std::copy(output.begin(), output.end(), userData.begin()); + std::copy(texture.begin(), texture.end(), userData.begin() + 4); + const std::array memory{{{reinterpret_cast(code.data()), std::as_bytes(code)}}}; + const ShaderRecompiler::ShaderComputeStageInfo compute{{Threads, 1, 1}, 0u, {false, false, false}, false, 1}; + ShaderRecompiler::RecompileRequest request{ + {ShaderStage::Compute, reinterpret_cast(code.data()), code, 0, {}}, + {32, 0, userData, compute, std::nullopt, std::nullopt, memory}, + device.Target(), + {0, 0, 0, 128} + }; + request.useCache = false; + const auto result = ShaderRecompiler::Recompile(request); + device.Dispatch(result, 1, 1, 1, {}, reinterpret_cast(code.data())); + device.WaitIdle(); + for (std::uint32_t tid = 1; tid < Threads; ++tid) { + Require(std::equal(Output.begin(), Output.begin() + 4, Output.begin() + tid * 4u), "image_get_resinfo: lanes disagree"); + } + return {Output[0], Output[1], Output[2], Output[3]}; +} + +std::string Text(const std::array& value) { + return std::to_string(value[0]) + "x" + std::to_string(value[1]) + "x" + std::to_string(value[2]) + " levels " + std::to_string(value[3]); +} + +using Result = std::array; + +void Check(AgcDriver::VulkanDevice& device, std::span native, const std::array& texture, const char* what, const Result& own, const Result& oneDimensional) { + const auto nativeResult = Query(device, native, texture); + Require(nativeResult == own, std::string("image_get_resinfo: the ") + what + " query returned " + Text(nativeResult) + ", RDNA 2 returns " + Text(own)); + const auto lineResult = Query(device, Query1D, texture); + Require(lineResult == oneDimensional, std::string("image_get_resinfo: DIM 1D on a ") + what + " T# returned " + Text(lineResult) + ", RDNA 2 returns " + Text(oneDimensional)); +} + +} + +int main() { + try { + const auto device = OpenVulkanTestDevice(); + if (!device) return VulkanTestSkipped; + Check(*device, QueryCube, Descriptor(TypeCube, 6u), "cube", {Size, Size, 6u, 1u}, {Size, Size, 0u, 1u}); + Check(*device, Query3D, Descriptor(Type3D, 4u), "3D", {Size, Size, 4u, 1u}, {Size, Size, 4u, 1u}); + Check(*device, Query2DArray, Descriptor(Type2DArray, 3u), "2D array", {Size, Size, 3u, 1u}, {Size, Size, 0u, 1u}); + std::puts("image resinfo dimension tests passed"); + return 0; + } catch (const std::exception& error) { + std::cerr << error.what() << '\n'; + return 1; + } +} diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/InterpolationModes.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/InterpolationModes.cpp index 9c332e8aaa..913426408a 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/InterpolationModes.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/InterpolationModes.cpp @@ -262,6 +262,7 @@ struct Generator { if (half) return bits; const auto value = std::bit_cast(bits); if (!std::isfinite(value) || std::fabs(value) > 65504.0f) return Half(); + if (value == 0.0f) return bits; const auto exponent = std::max(std::ilogb(value) - 10, -24); return std::bit_cast(static_cast(std::ldexp(std::nearbyint(std::ldexp(value, -exponent)), exponent))); } diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/LdsMixedWidthAtomics.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/LdsMixedWidthAtomics.cpp index 1f8032e93c..250f80264a 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/LdsMixedWidthAtomics.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/LdsMixedWidthAtomics.cpp @@ -325,8 +325,9 @@ void Run(AgcDriver::VulkanDevice& device, std::span code, c if (expectedDeviceDwords) std::printf("lds lock budget: %s GPU readback verified across 256 lanes\n", name); } -void Misaligned(AgcDriver::VulkanDevice& device, std::uint32_t ldsDwords) { +void Misaligned(AgcDriver::VulkanDevice& device, std::uint32_t ldsDwords, const char* lds) { const auto result = Compile(device, MisalignedCode, ldsDwords); + Output.fill(0xdeadbeefu); StderrCapture capture; try { device.Dispatch(result, 1, 1, 1, {}, reinterpret_cast(MisalignedCode.data())); @@ -336,8 +337,9 @@ void Misaligned(AgcDriver::VulkanDevice& device, std::uint32_t ldsDwords) { throw; } const auto log = capture.Finish(); - Require(log.find("BDA access failed: address=0x4 instruction=0x2c bytes=8 ") != std::string::npos && log.find("reason=7") != std::string::npos, "lds lock budget: misaligned ds_add_u64 on device-backed LDS did not fault"); - std::puts("lds lock budget: misaligned ds_add_u64 faulted"); + Require(log.find("BDA access failed: address=0x4 instruction=0x2c bytes=8 ") != std::string::npos && log.find("reason=7") != std::string::npos, std::string("lds lock budget: misaligned ds_add_u64 on ") + lds + " LDS did not fault"); + for (std::uint32_t tid = 0; tid < Threads; ++tid) Require(Output[tid * 2u] == 0u && Output[tid * 2u + 1u] == 0u, std::string("lds lock budget: misaligned ds_add_u64 on ") + lds + " LDS changed the slot: lane " + std::to_string(tid) + " read " + Hex(Output[tid * 2u] | (static_cast(Output[tid * 2u + 1u]) << 32u))); + std::printf("lds lock budget: misaligned ds_add_u64 on %s LDS faulted\n", lds); } void LockBudget(AgcDriver::VulkanDevice& device) { @@ -349,7 +351,8 @@ void LockBudget(AgcDriver::VulkanDevice& device) { const auto operations = static_cast(Threads) * Iterations; Run(device, UniformCode, "limit", operations, 1, limit / 4u, (limit / 4u + 2u) & ~1u); const auto& capabilities = target.supportedCapabilities; - if (std::find(capabilities.begin(), capabilities.end(), static_cast(spv::CapabilityInt64Atomics)) != capabilities.end()) Misaligned(device, limit / 4u); + if (std::find(capabilities.begin(), capabilities.end(), static_cast(spv::CapabilityInt64Atomics)) != capabilities.end()) Misaligned(device, limit / 4u, "device-backed"); + Misaligned(device, LdsDwords, "workgroup"); Run(device, UniformCode, "limit-4", operations, 1, (limit - 4u) / 4u, 0u); std::puts("lds lock budget tests passed"); } diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/MullitF32DenormalModes.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/MullitF32DenormalModes.cpp new file mode 100644 index 0000000000..2dd9886984 --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/tests/execution/MullitF32DenormalModes.cpp @@ -0,0 +1,226 @@ +#include "prx/libSceAgcDriver/Execution/include/VulkanDevice.hpp" +#include "prx/libSceAgcDriver/Graphics/include/Draw.hpp" +#include "Recompiler.hpp" +#include "VulkanTestDevice.hpp" +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace { + +using AgcDriver::Graphics::Require; +using ShaderRecompiler::ShaderStage; + +constexpr std::uint32_t Threads = 64; +constexpr std::uint32_t Inputs = 4; +constexpr std::uint32_t ConstantForms = 4; +alignas(256) std::array Input{}; +alignas(256) std::array Output{}; + +alignas(256) constexpr std::array MullitCode{ + 0x34020084u, 0x34040082u, 0xe0381000u, 0x80000401u, 0xbf8c3f70u, 0x7e140280u, 0xd550000au, 0x041a0b04u, + 0xe0701000u, 0x80010a02u, 0xbf810000u, +}; + +alignas(256) constexpr std::array ConstantMullitCode{ + 0x34020084u, 0x34040084u, 0xe0381000u, 0x80000401u, 0xbf8c3f70u, 0xd550000au, 0x041a08f2u, 0xd550000bu, + 0x0419e904u, 0xd550000cu, 0x041a0a81u, 0xd550000du, 0x041a08ffu, 0x3fc00000u, 0xe0781000u, 0x80010a02u, + 0xbf810000u, +}; + +constexpr const char* ConstantNames[ConstantForms] = {"v_mullit_f32 1.0, v4, v6", "v_mullit_f32 v4, 2.0, v6", "v_mullit_f32 0x1, v5, v6", "v_mullit_f32 0x3fc00000, v4, v6"}; + +struct Vector { + std::uint32_t lhs; + std::uint32_t rhs; + std::uint32_t limit; + std::uint32_t expected[8]; +}; + +constexpr Vector Vectors[] = { + {0x00000000u, 0x00000000u, 0x00000001u, {0xff7fffffu, 0xff7fffffu, 0x00000000u, 0x00000000u, 0xff7fffffu, 0xff7fffffu, 0x00000000u, 0x00000000u}}, + {0x00000000u, 0x00800000u, 0x00000001u, {0xff7fffffu, 0xff7fffffu, 0x00000000u, 0x00000000u, 0xff7fffffu, 0xff7fffffu, 0x00000000u, 0x00000000u}}, + {0x00000001u, 0x80000001u, 0x00800000u, {0x00000000u, 0x00000000u, 0x80000000u, 0x80000000u, 0x00000000u, 0x00000000u, 0x80000000u, 0x80000000u}}, + {0x00000001u, 0x3f800000u, 0x00000001u, {0xff7fffffu, 0xff7fffffu, 0x00000000u, 0x00000000u, 0xff7fffffu, 0xff7fffffu, 0x00000001u, 0x00000001u}}, + {0x00000001u, 0x3f800000u, 0x00800000u, {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000001u, 0x00000001u}}, + {0x00000001u, 0xbf800000u, 0x00800000u, {0x00000000u, 0x00000000u, 0x80000000u, 0x80000000u, 0x00000000u, 0x00000000u, 0x80000001u, 0x80000001u}}, + {0x00000001u, 0x7f7fffffu, 0x00800000u, {0x00000000u, 0x00000000u, 0x34ffffffu, 0x34ffffffu, 0x00000000u, 0x00000000u, 0x34ffffffu, 0x34ffffffu}}, + {0x00800000u, 0x00000000u, 0x00000001u, {0xff7fffffu, 0xff7fffffu, 0x00000000u, 0x00000000u, 0xff7fffffu, 0xff7fffffu, 0x00000000u, 0x00000000u}}, + {0x00800000u, 0x80000001u, 0x00800000u, {0x00000000u, 0x00000000u, 0x80000000u, 0x80000000u, 0x00000000u, 0x00000000u, 0x80000000u, 0x80000000u}}, + {0x00800000u, 0x00800000u, 0x00000001u, {0xff7fffffu, 0xff7fffffu, 0x00000000u, 0x00000000u, 0xff7fffffu, 0xff7fffffu, 0x00000000u, 0x00000000u}}, + {0x00800000u, 0x3f000000u, 0x00000001u, {0xff7fffffu, 0xff7fffffu, 0x00000000u, 0x00000000u, 0xff7fffffu, 0xff7fffffu, 0x00400000u, 0x00400000u}}, + {0x00800000u, 0x3f000000u, 0x00800000u, {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00400000u, 0x00400000u, 0x00400000u, 0x00400000u}}, + {0x3f000000u, 0x007fffffu, 0x00000001u, {0xff7fffffu, 0xff7fffffu, 0x00000000u, 0x00000000u, 0xff7fffffu, 0xff7fffffu, 0x00400000u, 0x00400000u}}, + {0x3f000000u, 0x007fffffu, 0x00800000u, {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00400000u, 0x00400000u}}, + {0x3f000000u, 0x807fffffu, 0x00800000u, {0x00000000u, 0x00000000u, 0x80000000u, 0x80000000u, 0x00000000u, 0x00000000u, 0x80400000u, 0x80400000u}}, + {0x7f800001u, 0x00000001u, 0x00000001u, {0xff7fffffu, 0xff7fffffu, 0x7f800001u, 0x7fc00001u, 0xff7fffffu, 0xff7fffffu, 0x7f800001u, 0x7fc00001u}}, + {0x7f800001u, 0x00000001u, 0x00800000u, {0x00000000u, 0x00000000u, 0x7f800001u, 0x7fc00001u, 0x00000000u, 0x00000000u, 0x7f800001u, 0x7fc00001u}}, + {0x7f800001u, 0x00800000u, 0x00000001u, {0xff7fffffu, 0xff7fffffu, 0x7f800001u, 0x7fc00001u, 0xff7fffffu, 0xff7fffffu, 0x7f800001u, 0x7fc00001u}}, + {0x7f800001u, 0x00800000u, 0x00800000u, {0x7f800001u, 0x7fc00001u, 0x7f800001u, 0x7fc00001u, 0x7f800001u, 0x7fc00001u, 0x7f800001u, 0x7fc00001u}}, + {0x3f800000u, 0x40000000u, 0x3f800000u, {0x40000000u, 0x40000000u, 0x40000000u, 0x40000000u, 0x40000000u, 0x40000000u, 0x40000000u, 0x40000000u}}, + {0x3f800000u, 0xff7fffffu, 0x3f800000u, {0xff7fffffu, 0xff7fffffu, 0xff7fffffu, 0xff7fffffu, 0xff7fffffu, 0xff7fffffu, 0xff7fffffu, 0xff7fffffu}}, + {0x3f800000u, 0x3f800000u, 0x80000000u, {0xff7fffffu, 0xff7fffffu, 0xff7fffffu, 0xff7fffffu, 0xff7fffffu, 0xff7fffffu, 0xff7fffffu, 0xff7fffffu}}, + {0x7f7fffffu, 0x7f7fffffu, 0x3f800000u, {0x7f800000u, 0x7f800000u, 0x7f800000u, 0x7f800000u, 0x7f800000u, 0x7f800000u, 0x7f800000u, 0x7f800000u}}, +}; + +struct ConstantVector { + std::uint32_t inputs[3]; + std::uint32_t expected[4][ConstantForms]; +}; + +constexpr ConstantVector ConstantVectors[] = { + {{0x00000001u, 0x00000001u, 0x3f800000u}, {{0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x00000001u, 0x00000002u, 0x00000000u, 0x00000002u}}}, + {{0x00000001u, 0x00800001u, 0x3f800000u}, {{0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x00000001u, 0x00000002u, 0x00000000u, 0x00000002u}}}, + {{0x00000002u, 0x00000001u, 0x3f800000u}, {{0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x00000002u, 0x00000004u, 0x00000000u, 0x00000003u}}}, + {{0x00000002u, 0x00800001u, 0x3f800000u}, {{0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x00000002u, 0x00000004u, 0x00000000u, 0x00000003u}}}, + {{0x00000003u, 0x00000001u, 0x3f800000u}, {{0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x00000003u, 0x00000006u, 0x00000000u, 0x00000004u}}}, + {{0x00000003u, 0x00800001u, 0x3f800000u}, {{0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x00000003u, 0x00000006u, 0x00000000u, 0x00000004u}}}, + {{0x00155555u, 0x00000001u, 0x3f800000u}, {{0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x00155555u, 0x002aaaaau, 0x00000000u, 0x00200000u}}}, + {{0x00155555u, 0x00800001u, 0x3f800000u}, {{0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x00155555u, 0x002aaaaau, 0x00000000u, 0x00200000u}}}, + {{0x003fffffu, 0x00000001u, 0x3f800000u}, {{0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x003fffffu, 0x007ffffeu, 0x00000000u, 0x005ffffeu}}}, + {{0x003fffffu, 0x00800001u, 0x3f800000u}, {{0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x003fffffu, 0x007ffffeu, 0x00000000u, 0x005ffffeu}}}, + {{0x00400000u, 0x00000001u, 0x3f800000u}, {{0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x00000000u, 0x00800000u, 0x00000000u, 0x00000000u}, {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x00400000u, 0x00800000u, 0x00000000u, 0x00600000u}}}, + {{0x00400000u, 0x00800001u, 0x3f800000u}, {{0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x00000000u, 0x00800000u, 0x00000000u, 0x00000000u}, {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x00400000u, 0x00800000u, 0x00000000u, 0x00600000u}}}, + {{0x00400001u, 0x00000001u, 0x3f800000u}, {{0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x00000000u, 0x00800002u, 0x00000000u, 0x00000000u}, {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x00400001u, 0x00800002u, 0x00000000u, 0x00600002u}}}, + {{0x00400001u, 0x00800001u, 0x3f800000u}, {{0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x00000000u, 0x00800002u, 0x00000000u, 0x00000000u}, {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x00400001u, 0x00800002u, 0x00000000u, 0x00600002u}}}, + {{0x00555555u, 0x00000001u, 0x3f800000u}, {{0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x00000000u, 0x00aaaaaau, 0x00000000u, 0x00000000u}, {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x00555555u, 0x00aaaaaau, 0x00000000u, 0x00800000u}}}, + {{0x00555555u, 0x00800001u, 0x3f800000u}, {{0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x00000000u, 0x00aaaaaau, 0x00000000u, 0x00000000u}, {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x00555555u, 0x00aaaaaau, 0x00000000u, 0x00800000u}}}, + {{0x007ffffeu, 0x00000001u, 0x3f800000u}, {{0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x00000000u, 0x00fffffcu, 0x00000000u, 0x00bffffdu}, {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x007ffffeu, 0x00fffffcu, 0x00000000u, 0x00bffffdu}}}, + {{0x007ffffeu, 0x00800001u, 0x3f800000u}, {{0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x00000000u, 0x00fffffcu, 0x00000000u, 0x00bffffdu}, {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x007ffffeu, 0x00fffffcu, 0x00000000u, 0x00bffffdu}}}, + {{0x007fffffu, 0x00000001u, 0x3f800000u}, {{0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x00000000u, 0x00fffffeu, 0x00000000u, 0x00bffffeu}, {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x007fffffu, 0x00fffffeu, 0x00000000u, 0x00bffffeu}}}, + {{0x007fffffu, 0x00800001u, 0x3f800000u}, {{0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x00000000u, 0x00fffffeu, 0x00000000u, 0x00bffffeu}, {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x007fffffu, 0x00fffffeu, 0x00000000u, 0x00bffffeu}}}, + {{0x80000001u, 0x00000001u, 0x3f800000u}, {{0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x80000000u, 0x80000000u, 0x00000000u, 0x80000000u}, {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x80000001u, 0x80000002u, 0x00000000u, 0x80000002u}}}, + {{0x80000001u, 0x00800001u, 0x3f800000u}, {{0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x80000000u, 0x80000000u, 0x00000000u, 0x80000000u}, {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x80000001u, 0x80000002u, 0x00000000u, 0x80000002u}}}, + {{0x80000002u, 0x00000001u, 0x3f800000u}, {{0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x80000000u, 0x80000000u, 0x00000000u, 0x80000000u}, {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x80000002u, 0x80000004u, 0x00000000u, 0x80000003u}}}, + {{0x80000002u, 0x00800001u, 0x3f800000u}, {{0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x80000000u, 0x80000000u, 0x00000000u, 0x80000000u}, {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x80000002u, 0x80000004u, 0x00000000u, 0x80000003u}}}, + {{0x80000003u, 0x00000001u, 0x3f800000u}, {{0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x80000000u, 0x80000000u, 0x00000000u, 0x80000000u}, {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x80000003u, 0x80000006u, 0x00000000u, 0x80000004u}}}, + {{0x80000003u, 0x00800001u, 0x3f800000u}, {{0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x80000000u, 0x80000000u, 0x00000000u, 0x80000000u}, {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x80000003u, 0x80000006u, 0x00000000u, 0x80000004u}}}, + {{0x80155555u, 0x00000001u, 0x3f800000u}, {{0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x80000000u, 0x80000000u, 0x00000000u, 0x80000000u}, {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x80155555u, 0x802aaaaau, 0x00000000u, 0x80200000u}}}, + {{0x80155555u, 0x00800001u, 0x3f800000u}, {{0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x80000000u, 0x80000000u, 0x00000000u, 0x80000000u}, {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x80155555u, 0x802aaaaau, 0x00000000u, 0x80200000u}}}, + {{0x803fffffu, 0x00000001u, 0x3f800000u}, {{0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x80000000u, 0x80000000u, 0x00000000u, 0x80000000u}, {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x803fffffu, 0x807ffffeu, 0x00000000u, 0x805ffffeu}}}, + {{0x803fffffu, 0x00800001u, 0x3f800000u}, {{0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x80000000u, 0x80000000u, 0x00000000u, 0x80000000u}, {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x803fffffu, 0x807ffffeu, 0x00000000u, 0x805ffffeu}}}, + {{0x80400000u, 0x00000001u, 0x3f800000u}, {{0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x80000000u, 0x80800000u, 0x00000000u, 0x80000000u}, {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x80400000u, 0x80800000u, 0x00000000u, 0x80600000u}}}, + {{0x80400000u, 0x00800001u, 0x3f800000u}, {{0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x80000000u, 0x80800000u, 0x00000000u, 0x80000000u}, {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x80400000u, 0x80800000u, 0x00000000u, 0x80600000u}}}, + {{0x80400001u, 0x00000001u, 0x3f800000u}, {{0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x80000000u, 0x80800002u, 0x00000000u, 0x80000000u}, {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x80400001u, 0x80800002u, 0x00000000u, 0x80600002u}}}, + {{0x80400001u, 0x00800001u, 0x3f800000u}, {{0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x80000000u, 0x80800002u, 0x00000000u, 0x80000000u}, {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x80400001u, 0x80800002u, 0x00000000u, 0x80600002u}}}, + {{0x80555555u, 0x00000001u, 0x3f800000u}, {{0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x80000000u, 0x80aaaaaau, 0x00000000u, 0x80000000u}, {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x80555555u, 0x80aaaaaau, 0x00000000u, 0x80800000u}}}, + {{0x80555555u, 0x00800001u, 0x3f800000u}, {{0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x80000000u, 0x80aaaaaau, 0x00000000u, 0x80000000u}, {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x80555555u, 0x80aaaaaau, 0x00000000u, 0x80800000u}}}, + {{0x807ffffeu, 0x00000001u, 0x3f800000u}, {{0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x80000000u, 0x80fffffcu, 0x00000000u, 0x80bffffdu}, {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x807ffffeu, 0x80fffffcu, 0x00000000u, 0x80bffffdu}}}, + {{0x807ffffeu, 0x00800001u, 0x3f800000u}, {{0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x80000000u, 0x80fffffcu, 0x00000000u, 0x80bffffdu}, {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x807ffffeu, 0x80fffffcu, 0x00000000u, 0x80bffffdu}}}, + {{0x807fffffu, 0x00000001u, 0x3f800000u}, {{0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x80000000u, 0x80fffffeu, 0x00000000u, 0x80bffffeu}, {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x807fffffu, 0x80fffffeu, 0x00000000u, 0x80bffffeu}}}, + {{0x807fffffu, 0x00800001u, 0x3f800000u}, {{0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x80000000u, 0x80fffffeu, 0x00000000u, 0x80bffffeu}, {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u}, {0x807fffffu, 0x80fffffeu, 0x00000000u, 0x80bffffeu}}}, +}; + +static_assert(std::size(Vectors) <= Threads && std::size(ConstantVectors) <= Threads); + +std::array BufferDescriptor(const void* data, std::uint32_t bytes) { + const auto address = reinterpret_cast(data); + return {static_cast(address), static_cast((address >> 32u) & 0xffffu), bytes, 0x31016facu}; +} + +std::string Hex(std::uint32_t value) { + char text[16]; + std::snprintf(text, sizeof(text), "0x%08x", value); + return text; +} + +bool KeepsDenormals(const AgcDriver::VulkanDevice& device) { + const auto target = device.Target(); + return target.float32DenormPreserve && !target.float32FlushToZero; +} + +bool Run(AgcDriver::VulkanDevice& device, std::span code, std::uint32_t denorm32, bool ieee) { + Output.fill(0xdeadbeefu); + std::vector userData(8, 0u); + const auto input = BufferDescriptor(Input.data(), static_cast(Input.size() * 4u)); + const auto output = BufferDescriptor(Output.data(), static_cast(Output.size() * 4u)); + std::copy(input.begin(), input.end(), userData.begin()); + std::copy(output.begin(), output.end(), userData.begin() + 4); + const std::array memory{{{reinterpret_cast(code.data()), std::as_bytes(code)}}}; + const ShaderRecompiler::ShaderComputeStageInfo compute{{Threads, 1, 1}, 0u, {false, false, false}, false, 1}; + ShaderRecompiler::RecompileRequest request{ + {ShaderStage::Compute, reinterpret_cast(code.data()), code, 0, {}}, + {32, 0, userData, compute, std::nullopt, std::nullopt, memory}, + device.Target(), + {0, 0, 0, 128} + }; + request.useCache = false; + request.context.floatMode = ShaderRecompiler::ShaderFloatMode{(denorm32 << 4u) | (3u << 6u), true, ieee, false}; + std::optional result; + try { + result.emplace(ShaderRecompiler::Recompile(request)); + } catch (const std::exception& error) { + const std::string message = error.what(); + Require(!KeepsDenormals(device) && (denorm32 == 1u || denorm32 == 2u) && message.find("needs DenormPreserve 32") != std::string::npos, + "v_mullit_f32 denorm32 " + std::to_string(denorm32) + ": " + message); + return false; + } + device.Dispatch(*result, 1, 1, 1, {}, reinterpret_cast(code.data())); + device.WaitIdle(); + return true; +} + +void CheckRegisters(std::uint32_t denorm32, bool ieee) { + for (std::uint32_t row = 0; row < std::size(Vectors); ++row) { + const auto& vector = Vectors[row]; + const auto actual = Output[row]; + const auto expected = vector.expected[denorm32 * 2u + (ieee ? 1u : 0u)]; + Require(actual == expected, "v_mullit_f32 denorm32 " + std::to_string(denorm32) + (ieee ? " ieee" : " no ieee") + " inputs " + Hex(vector.lhs) + " " + Hex(vector.rhs) + " " + + Hex(vector.limit) + " is " + Hex(actual) + ", expected " + Hex(expected)); + } +} + +void CheckConstants(std::uint32_t denorm32, bool ieee) { + for (std::uint32_t row = 0; row < std::size(ConstantVectors); ++row) { + const auto& vector = ConstantVectors[row]; + for (std::uint32_t form = 0; form < ConstantForms; ++form) { + const auto actual = Output[row * ConstantForms + form]; + const auto expected = vector.expected[denorm32][form]; + Require(actual == expected, std::string(ConstantNames[form]) + " denorm32 " + std::to_string(denorm32) + (ieee ? " ieee" : " no ieee") + " inputs " + Hex(vector.inputs[0]) + " " + + Hex(vector.inputs[1]) + " " + Hex(vector.inputs[2]) + " is " + Hex(actual) + ", expected " + Hex(expected)); + } + } +} + +} + +int main() { + try { + const auto device = OpenVulkanTestDevice(); + if (!device) return VulkanTestSkipped; + const bool keeps = KeepsDenormals(*device); + std::uint32_t checked = 0u; + for (std::uint32_t denorm32 = 0u; denorm32 < 4u; ++denorm32) { + if (denorm32 == 3u && !keeps) continue; + for (const bool ieee : {false, true}) { + Input.fill(0u); + for (std::uint32_t row = 0; row < std::size(Vectors); ++row) { + Input[row * Inputs] = Vectors[row].lhs; + Input[row * Inputs + 1] = Vectors[row].rhs; + Input[row * Inputs + 2] = Vectors[row].limit; + } + if (Run(*device, MullitCode, denorm32, ieee)) { + CheckRegisters(denorm32, ieee); + ++checked; + } + Input.fill(0u); + for (std::uint32_t row = 0; row < std::size(ConstantVectors); ++row) std::copy(std::begin(ConstantVectors[row].inputs), std::end(ConstantVectors[row].inputs), Input.begin() + row * Inputs); + if (Run(*device, ConstantMullitCode, denorm32, ieee)) { + CheckConstants(denorm32, ieee); + ++checked; + } + } + } + std::printf("v_mullit_f32 denormal mode tests passed (%u of 16 runs checked; denormal modes 1 and 2 need DenormPreserve 32, mode 3 is checked only where the device keeps f32 denormals)\n", checked); + return 0; + } catch (const std::exception& error) { + std::cerr << error.what() << '\n'; + return 1; + } +} diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/MultisampleRetirement.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/MultisampleRetirement.cpp index 27f7d4ae49..5cc437a819 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/MultisampleRetirement.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/MultisampleRetirement.cpp @@ -265,11 +265,11 @@ private: bool mapped = false; }; -ColorTarget Target(const GuestBlock& block) { +ColorTarget Target(const GuestBlock& block, VkFormat format = VK_FORMAT_R8G8B8A8_UNORM) { ColorTarget target{}; target.address = target.surfaceAddress = block.Address(); target.extent = target.surfaceExtent = {Side, Side}; - target.format = VK_FORMAT_R8G8B8A8_UNORM; + target.format = format; target.elementBytes = 4; target.samples = 4; target.tileMode = ColorTileMode::RenderTarget; @@ -556,6 +556,36 @@ public: AwaitImages(0, "MSAA targets with twin views were not fully released"); } + void StorageSrgbTwin() { + if (!context.storageImageMultisample || HostStorageFormat(context, VK_FORMAT_R8G8B8A8_SRGB) != VK_FORMAT_R8G8B8A8_UNORM) { + std::puts("the device has no UNORM storage form for a multisampled sRGB target; its storage twin case is skipped"); + return; + } + GuestBlock block; + const auto target = Target(block, VK_FORMAT_R8G8B8A8_SRGB); + auto resource = Resource(block); + VkImageView storageView = VK_NULL_HANDLE; + { + std::lock_guard lock(AgcDriver::GuestMemory::GpuMutex()); + MultisampleTargetView(context, target); + storageView = MultisampleTargetStorageView(context, resource); + Require(storageView != VK_NULL_HANDLE, "a UNORM storage access to a multisampled sRGB target has no view"); + Require(MultisampleTargetStorageView(context, resource) == storageView, "a repeated UNORM storage access to a multisampled sRGB target changed its view"); + resource.format = 60; + bool refused = false; + try { + MultisampleTargetStorageView(context, resource); + } catch (const std::exception&) { + refused = true; + } + Require(refused, "a storage access in an unrelated format reached a multisampled sRGB target"); + } + Sync(); + block.Unmap(); + Clear(); + AwaitImages(0, "the sRGB storage twin targets were not fully released"); + } + private: AgcDriver::VulkanDevice& device; Context context; @@ -574,6 +604,7 @@ int main() { run.RecordedRemap(); run.SampledRemap(); run.StorageRemap(); + run.StorageSrgbTwin(); run.OrderRemap(); if (context.storageImageMultisample) run.StorageTwin(); std::puts("retired MSAA allocations stay bounded, recorded clears and storage promotions retain their images, sampled aliases release their parents after the last batch, and sample-order targets keep their retired images until their batch completes"); diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/NativeMultisampleDepth.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/NativeMultisampleDepth.cpp index 641eaf93b0..5548d5d7fa 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/NativeMultisampleDepth.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/NativeMultisampleDepth.cpp @@ -15,6 +15,7 @@ #include "prx/libc/include/GuestWriteWatch.hpp" #include "SpirvBackend/SpirvModule.hpp" #include "VulkanTestDevice.hpp" +#include "FixtureDeviceSelection.hpp" #include #include #include @@ -54,6 +55,8 @@ public: Device() { #ifdef _WIN32 library = SDL_LoadObject("vulkan-1.dll"); +#elif defined(__APPLE__) + library = SDL_LoadObject("libvulkan.1.dylib"); #else library = SDL_LoadObject("libvulkan.so.1"); #endif @@ -65,26 +68,22 @@ public: app.apiVersion = VK_API_VERSION_1_1; VkInstanceCreateInfo info{VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO}; info.pApplicationInfo = &app; +#ifdef __APPLE__ + const char* portability = VK_KHR_PORTABILITY_ENUMERATION_EXTENSION_NAME; + info.flags = VK_INSTANCE_CREATE_ENUMERATE_PORTABILITY_BIT_KHR; + info.enabledExtensionCount = 1; + info.ppEnabledExtensionNames = &portability; +#endif Check(function("vkCreateInstance")(&info, nullptr, &instance), "vkCreateInstance"); std::uint32_t count = 0u; const auto enumerate = function("vkEnumeratePhysicalDevices"); Check(enumerate(instance, &count, nullptr), "vkEnumeratePhysicalDevices"); std::vector devices(count); Check(enumerate(instance, &count, devices.data()), "vkEnumeratePhysicalDevices"); - int selected = -1; - for (const auto physical : devices) { - VkPhysicalDeviceProperties properties{}; - function("vkGetPhysicalDeviceProperties")(physical, &properties); - if (const auto* wanted = std::getenv("ANYPS5_GPU"); wanted != nullptr && std::string(properties.deviceName).find(wanted) == std::string::npos) continue; - const int rank = properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU ? 3 : properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU ? 2 : 1; - if (properties.apiVersion < VK_API_VERSION_1_1 || rank <= selected) continue; - context.physical = physical; - context.limits = properties.limits; - selected = rank; - } - Require(context.physical != VK_NULL_HANDLE, "no Vulkan 1.1 device"); + context.physical = SelectFixturePhysicalDevice(devices, function("vkGetPhysicalDeviceProperties")); VkPhysicalDeviceProperties selectedProperties{}; function("vkGetPhysicalDeviceProperties")(context.physical, &selectedProperties); + context.limits = selectedProperties.limits; std::printf("Selected GPU name=%s vendor=0x%x device=0x%x driver=%u\n", selectedProperties.deviceName, selectedProperties.vendorID, selectedProperties.deviceID, selectedProperties.driverVersion); const auto queues = function("vkGetPhysicalDeviceQueueFamilyProperties"); queues(context.physical, &count, nullptr); diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/OptimizeOnFiber.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/OptimizeOnFiber.cpp new file mode 100644 index 0000000000..36fc0c7c91 --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/tests/execution/OptimizeOnFiber.cpp @@ -0,0 +1,148 @@ +#if defined(ANYPS5_ENABLE_SPIRV_TOOLS) && ANYPS5_ENABLE_SPIRV_TOOLS +#include "SpirvBackend/SpirvOptimizer.hpp" +#include "prx/libc/include/general/VabiMacros.hpp" +#include "SceTypes.hpp" +#include +#include +#include +#include +#include +#include +#include + +using GuestFiberEntry = void (APS5_VABI*)(std::uint64_t, std::uint64_t); + +extern "C" { +std::int32_t APS5_VABI _sceFiberInitializeImpl_nid_postfix(FiberObject*, const char*, GuestFiberEntry, std::uint64_t, void*, std::uint64_t, const void*, std::uint32_t); +std::int32_t APS5_VABI sceFiberFinalize(FiberObject*); +std::int32_t APS5_VABI sceFiberRun_nid_postfix(FiberObject*, std::uint64_t, std::uint64_t*); +std::int32_t APS5_VABI sceFiberReturnToThread(std::uint64_t, std::uint64_t*); +} + +namespace { + +constexpr std::size_t ContextBytes = 64 * 1024; +constexpr std::size_t FiberStackLimit = 8 * 1024; +constexpr std::uint32_t VulkanVersion = 0x00401000u; +constexpr std::uint32_t SpirvVersion = 0x00010300u; +constexpr std::uint64_t Fill = 0x5aa5c33c5aa5c33cull; + +alignas(16) std::array FiberContext; +struct alignas(8) FiberStorage { + unsigned char bytes[0x100]; +}; +FiberStorage FiberObjectStorage; + +constexpr std::array Module{ + 0x07230203u, 0x00010300u, 0x0008000bu, 0x00000044u, 0x00000000u, 0x00020011u, 0x00000001u, 0x0006000bu, + 0x00000001u, 0x4c534c47u, 0x6474732eu, 0x3035342eu, 0x00000000u, 0x0003000eu, 0x00000000u, 0x00000001u, + 0x0006000fu, 0x00000005u, 0x00000004u, 0x6e69616du, 0x00000000u, 0x0000000bu, 0x00060010u, 0x00000004u, + 0x00000011u, 0x00000040u, 0x00000001u, 0x00000001u, 0x00030003u, 0x00000002u, 0x000001c2u, 0x00040005u, + 0x00000004u, 0x6e69616du, 0x00000000u, 0x00040005u, 0x00000008u, 0x65646e69u, 0x00000078u, 0x00080005u, + 0x0000000bu, 0x475f6c67u, 0x61626f6cu, 0x766e496cu, 0x7461636fu, 0x496e6f69u, 0x00000044u, 0x00030005u, + 0x00000010u, 0x006d7573u, 0x00040005u, 0x00000011u, 0x74617473u, 0x00000065u, 0x00040005u, 0x00000013u, + 0x61746144u, 0x00000000u, 0x00050006u, 0x00000013u, 0x00000000u, 0x756c6176u, 0x00007365u, 0x00030005u, + 0x00000015u, 0x00000000u, 0x00030005u, 0x0000001cu, 0x00000069u, 0x00040047u, 0x0000000bu, 0x0000000bu, + 0x0000001cu, 0x00040047u, 0x00000012u, 0x00000006u, 0x00000004u, 0x00030047u, 0x00000013u, 0x00000002u, + 0x00050048u, 0x00000013u, 0x00000000u, 0x00000023u, 0x00000000u, 0x00040047u, 0x00000015u, 0x00000021u, + 0x00000000u, 0x00040047u, 0x00000015u, 0x00000022u, 0x00000000u, 0x00040047u, 0x00000043u, 0x0000000bu, + 0x00000019u, 0x00020013u, 0x00000002u, 0x00030021u, 0x00000003u, 0x00000002u, 0x00040015u, 0x00000006u, + 0x00000020u, 0x00000000u, 0x00040020u, 0x00000007u, 0x00000007u, 0x00000006u, 0x00040017u, 0x00000009u, + 0x00000006u, 0x00000003u, 0x00040020u, 0x0000000au, 0x00000001u, 0x00000009u, 0x0004003bu, 0x0000000au, + 0x0000000bu, 0x00000001u, 0x0004002bu, 0x00000006u, 0x0000000cu, 0x00000000u, 0x00040020u, 0x0000000du, + 0x00000001u, 0x00000006u, 0x0003001du, 0x00000012u, 0x00000006u, 0x0003001eu, 0x00000013u, 0x00000012u, + 0x00040020u, 0x00000014u, 0x0000000cu, 0x00000013u, 0x0004003bu, 0x00000014u, 0x00000015u, 0x0000000cu, + 0x00040015u, 0x00000016u, 0x00000020u, 0x00000001u, 0x0004002bu, 0x00000016u, 0x00000017u, 0x00000000u, + 0x00040020u, 0x00000019u, 0x0000000cu, 0x00000006u, 0x0004002bu, 0x00000006u, 0x00000023u, 0x00000010u, + 0x00020014u, 0x00000024u, 0x0004002bu, 0x00000006u, 0x00000027u, 0x00000001u, 0x0004002bu, 0x00000006u, + 0x00000038u, 0x0019660du, 0x0004002bu, 0x00000006u, 0x0000003au, 0x3c6ef35fu, 0x0004002bu, 0x00000016u, + 0x0000003du, 0x00000001u, 0x0004002bu, 0x00000006u, 0x00000042u, 0x00000040u, 0x0006002cu, 0x00000009u, + 0x00000043u, 0x00000042u, 0x00000027u, 0x00000027u, 0x00050036u, 0x00000002u, 0x00000004u, 0x00000000u, + 0x00000003u, 0x000200f8u, 0x00000005u, 0x0004003bu, 0x00000007u, 0x00000008u, 0x00000007u, 0x0004003bu, + 0x00000007u, 0x00000010u, 0x00000007u, 0x0004003bu, 0x00000007u, 0x00000011u, 0x00000007u, 0x0004003bu, + 0x00000007u, 0x0000001cu, 0x00000007u, 0x00050041u, 0x0000000du, 0x0000000eu, 0x0000000bu, 0x0000000cu, + 0x0004003du, 0x00000006u, 0x0000000fu, 0x0000000eu, 0x0003003eu, 0x00000008u, 0x0000000fu, 0x0003003eu, + 0x00000010u, 0x0000000cu, 0x0004003du, 0x00000006u, 0x00000018u, 0x00000008u, 0x00060041u, 0x00000019u, + 0x0000001au, 0x00000015u, 0x00000017u, 0x00000018u, 0x0004003du, 0x00000006u, 0x0000001bu, 0x0000001au, + 0x0003003eu, 0x00000011u, 0x0000001bu, 0x0003003eu, 0x0000001cu, 0x0000000cu, 0x000200f9u, 0x0000001du, + 0x000200f8u, 0x0000001du, 0x000400f6u, 0x0000001fu, 0x00000020u, 0x00000000u, 0x000200f9u, 0x00000021u, + 0x000200f8u, 0x00000021u, 0x0004003du, 0x00000006u, 0x00000022u, 0x0000001cu, 0x000500b0u, 0x00000024u, + 0x00000025u, 0x00000022u, 0x00000023u, 0x000400fau, 0x00000025u, 0x0000001eu, 0x0000001fu, 0x000200f8u, + 0x0000001eu, 0x0004003du, 0x00000006u, 0x00000026u, 0x00000011u, 0x000500c7u, 0x00000006u, 0x00000028u, + 0x00000026u, 0x00000027u, 0x000500abu, 0x00000024u, 0x00000029u, 0x00000028u, 0x0000000cu, 0x000300f7u, + 0x0000002bu, 0x00000000u, 0x000400fau, 0x00000029u, 0x0000002au, 0x00000031u, 0x000200f8u, 0x0000002au, + 0x0004003du, 0x00000006u, 0x0000002cu, 0x00000011u, 0x0004003du, 0x00000006u, 0x0000002du, 0x0000001cu, + 0x00050084u, 0x00000006u, 0x0000002eu, 0x0000002cu, 0x0000002du, 0x0004003du, 0x00000006u, 0x0000002fu, + 0x00000010u, 0x00050080u, 0x00000006u, 0x00000030u, 0x0000002fu, 0x0000002eu, 0x0003003eu, 0x00000010u, + 0x00000030u, 0x000200f9u, 0x0000002bu, 0x000200f8u, 0x00000031u, 0x0004003du, 0x00000006u, 0x00000032u, + 0x00000011u, 0x0004003du, 0x00000006u, 0x00000033u, 0x0000001cu, 0x00050080u, 0x00000006u, 0x00000034u, + 0x00000032u, 0x00000033u, 0x0004003du, 0x00000006u, 0x00000035u, 0x00000010u, 0x000500c6u, 0x00000006u, + 0x00000036u, 0x00000035u, 0x00000034u, 0x0003003eu, 0x00000010u, 0x00000036u, 0x000200f9u, 0x0000002bu, + 0x000200f8u, 0x0000002bu, 0x0004003du, 0x00000006u, 0x00000037u, 0x00000011u, 0x00050084u, 0x00000006u, + 0x00000039u, 0x00000037u, 0x00000038u, 0x00050080u, 0x00000006u, 0x0000003bu, 0x00000039u, 0x0000003au, + 0x0003003eu, 0x00000011u, 0x0000003bu, 0x000200f9u, 0x00000020u, 0x000200f8u, 0x00000020u, 0x0004003du, + 0x00000006u, 0x0000003cu, 0x0000001cu, 0x00050080u, 0x00000006u, 0x0000003eu, 0x0000003cu, 0x0000003du, + 0x0003003eu, 0x0000001cu, 0x0000003eu, 0x000200f9u, 0x0000001du, 0x000200f8u, 0x0000001fu, 0x0004003du, + 0x00000006u, 0x0000003fu, 0x00000008u, 0x0004003du, 0x00000006u, 0x00000040u, 0x00000010u, 0x00060041u, + 0x00000019u, 0x00000041u, 0x00000015u, 0x00000017u, 0x0000003fu, 0x0003003eu, 0x00000041u, 0x00000040u, + 0x000100fdu, 0x00010038u +}; + +std::vector Optimized; +std::exception_ptr Failure; + +void Require(bool condition, const std::string& message) { + if (!condition) throw std::runtime_error(message); +} + +std::vector Optimize() { + return ShaderRecompiler::ValidateAndOptimizeSpirv(Module, VulkanVersion, SpirvVersion); +} + +void APS5_VABI OptimizeOnFiber(std::uint64_t, std::uint64_t) { + try { + Optimized = Optimize(); + } catch (...) { + Failure = std::current_exception(); + } + sceFiberReturnToThread(0, nullptr); +} + +std::size_t FiberStackUsed() { + std::size_t untouched = 0; + while (untouched < FiberContext.size() && FiberContext[untouched] == Fill) ++untouched; + return (FiberContext.size() - untouched) * sizeof(std::uint64_t); +} + +} + +int main() { + try { + FiberContext.fill(Fill); + auto* fiber = reinterpret_cast(&FiberObjectStorage); + Require(_sceFiberInitializeImpl_nid_postfix(fiber, "optimize", OptimizeOnFiber, 0, FiberContext.data(), ContextBytes, nullptr, 0) == 0, "SPIR-V optimization on a fiber: the fiber was not initialized"); + const auto before = ShaderRecompiler::OptimizerHostThreads(); + Require(sceFiberRun_nid_postfix(fiber, 0, nullptr) == 0, "SPIR-V optimization on a fiber: the fiber did not run"); + const auto onFiber = ShaderRecompiler::OptimizerHostThreads() - before; + Require(sceFiberFinalize(fiber) == 0, "SPIR-V optimization on a fiber: the fiber was not finalized"); + if (Failure) std::rethrow_exception(Failure); + const auto used = FiberStackUsed(); + const auto beforeThread = ShaderRecompiler::OptimizerHostThreads(); + const auto onThreadResult = Optimize(); + const auto onThread = ShaderRecompiler::OptimizerHostThreads() - beforeThread; + Require(Optimized == onThreadResult, "SPIR-V optimization on a fiber returned another module than on the thread"); + Require(used <= FiberStackLimit, "SPIR-V optimization on a fiber running on its context used " + std::to_string(used) + " bytes of it, more than " + std::to_string(FiberStackLimit) + ": the optimizer ran on it"); + Require(onFiber == 1, "SPIR-V optimization on a fiber running on its context started " + std::to_string(onFiber) + " host threads instead of one"); + Require(onThread == 0, "SPIR-V optimization on an ordinary thread started " + std::to_string(onThread) + " host threads instead of optimizing on its own stack"); + std::cout << "SPIR-V optimization on a fiber used " << used << " bytes of its context\n"; + return 0; + } catch (const std::exception& error) { + std::cerr << error.what() << '\n'; + return 1; + } +} +#else +int main() { + return 77; +} +#endif diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/OversizedDescriptorSets.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/OversizedDescriptorSets.cpp index 8984302a91..213d8c7aaf 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/OversizedDescriptorSets.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/OversizedDescriptorSets.cpp @@ -2,6 +2,7 @@ #include "prx/libSceAgcDriver/Graphics/include/Resources.hpp" #include "prx/libSceAgcDriver/Graphics/include/ShaderResources.hpp" #include "VulkanTestDevice.hpp" +#include "FixtureDeviceSelection.hpp" #include #include #include @@ -50,15 +51,7 @@ public: std::vector devices(count); Check(enumerate(instance, &count, devices.data()), "vkEnumeratePhysicalDevices"); const auto properties = function("vkGetPhysicalDeviceProperties"); - context.physical = devices.front(); - for (const auto candidate : devices) { - VkPhysicalDeviceProperties described{}; - properties(candidate, &described); - if (described.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU) { - context.physical = candidate; - break; - } - } + context.physical = SelectFixturePhysicalDevice(devices, properties); VkPhysicalDeviceProperties selected{}; properties(context.physical, &selected); context.limits = selected.limits; diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/PartiallyResidentSnapshot.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/PartiallyResidentSnapshot.cpp index 0afb33266f..7d4ab406e6 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/PartiallyResidentSnapshot.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/PartiallyResidentSnapshot.cpp @@ -2,13 +2,16 @@ #include "prx/libSceAgcDriver/Execution/include/GuestMemory.hpp" #include "prx/libSceAgcDriver/Graphics/include/GuestTextureResource.hpp" #include "prx/libSceAgcDriver/Graphics/include/Recorder.hpp" +#include "prx/libSceAgcDriver/Graphics/include/Resources.hpp" #include "prx/libSceAgcDriver/Graphics/include/ShaderResources.hpp" #include "prx/libSceAgcDriver/Graphics/include/Texture.hpp" #include "prx/libSceAgcDriver/Graphics/include/TextureDetiler.hpp" #include "prx/libSceAgcDriver/Graphics/include/TextureTiling.hpp" +#include "FixtureDeviceSelection.hpp" #include "prx/libc/include/GuestAllocations.hpp" #include "prx/libc/include/GuestArena.hpp" #include "prx/libc/include/GuestWriteWatch.hpp" +#include "SpirvBackend/SpirvModule.hpp" #include #ifdef _WIN32 #ifndef NOMINMAX @@ -18,6 +21,7 @@ #else #include #endif +#include #include #include #include @@ -29,6 +33,7 @@ #include #include #include +#include #include #include #include @@ -54,6 +59,8 @@ public: Device() { #ifdef _WIN32 library = SDL_LoadObject("vulkan-1.dll"); +#elif defined(__APPLE__) + library = SDL_LoadObject("libvulkan.1.dylib"); #else library = SDL_LoadObject("libvulkan.so.1"); #endif @@ -65,6 +72,12 @@ public: application.apiVersion = VK_API_VERSION_1_1; VkInstanceCreateInfo info{VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO}; info.pApplicationInfo = &application; +#ifdef __APPLE__ + const char* portability = VK_KHR_PORTABILITY_ENUMERATION_EXTENSION_NAME; + info.flags = VK_INSTANCE_CREATE_ENUMERATE_PORTABILITY_BIT_KHR; + info.enabledExtensionCount = 1; + info.ppEnabledExtensionNames = &portability; +#endif Check(function("vkCreateInstance")(&info, nullptr, &instance), "vkCreateInstance"); std::uint32_t count = 0; const auto enumerate = function("vkEnumeratePhysicalDevices"); @@ -72,7 +85,7 @@ public: Require(count != 0, "no Vulkan device"); std::vector devices(count); Check(enumerate(instance, &count, devices.data()), "vkEnumeratePhysicalDevices"); - context.physical = devices.front(); + context.physical = SelectFixturePhysicalDevice(devices, function("vkGetPhysicalDeviceProperties")); const auto extensions = function("vkEnumerateDeviceExtensionProperties"); Check(extensions(context.physical, nullptr, &count, nullptr), "vkEnumerateDeviceExtensionProperties"); std::vector available(count); @@ -353,6 +366,201 @@ void UntrackedHint(const Context& context, ReservedSurface& surface) { } #endif +#ifdef _WIN32 +std::vector TexelFetchShader(std::uint32_t side) { + ShaderRecompiler::SpirvModule module; + module.EmitCapability(spv::CapabilityShader); + module.AddMemoryModel(spv::AddressingModelLogical, spv::MemoryModelGLSL450); + const auto u32 = module.Type(spv::OpTypeInt, 32u, 0u); + const auto i32 = module.Type(spv::OpTypeInt, 32u, 1u); + const auto v4 = module.Type(spv::OpTypeVector, u32, 4u); + const auto v3 = module.Type(spv::OpTypeVector, u32, 3u); + const auto v2 = module.Type(spv::OpTypeVector, i32, 2u); + const auto voidType = module.Type(spv::OpTypeVoid); + const auto constant = [&](std::uint32_t value) { return module.Constant(spv::OpConstant, u32, value); }; + const auto array = module.Type(spv::OpTypeRuntimeArray, v4); + module.AddAnnotation(spv::OpDecorate, array, spv::DecorationArrayStride, 16u); + const auto block = module.Type(spv::OpTypeStruct, array); + module.AddAnnotation(spv::OpDecorate, block, spv::DecorationBlock); + module.AddAnnotation(spv::OpMemberDecorate, block, 0u, spv::DecorationOffset, 0u); + const auto buffer = module.DefineGlobalVariable(module.Type(spv::OpTypePointer, spv::StorageClassStorageBuffer, block), spv::StorageClassStorageBuffer); + module.AddAnnotation(spv::OpDecorate, buffer, spv::DecorationDescriptorSet, 0u); + module.AddAnnotation(spv::OpDecorate, buffer, spv::DecorationBinding, 1u); + const auto imageType = module.Type(spv::OpTypeImage, u32, spv::Dim2D, 0u, 0u, 0u, 1u, spv::ImageFormatUnknown); + const auto image = module.DefineGlobalVariable(module.Type(spv::OpTypePointer, spv::StorageClassUniformConstant, imageType), spv::StorageClassUniformConstant); + module.AddAnnotation(spv::OpDecorate, image, spv::DecorationDescriptorSet, 0u); + module.AddAnnotation(spv::OpDecorate, image, spv::DecorationBinding, 0u); + const auto group = module.DefineGlobalVariable(module.Type(spv::OpTypePointer, spv::StorageClassInput, v3), spv::StorageClassInput); + module.AddAnnotation(spv::OpDecorate, group, spv::DecorationBuiltIn, spv::BuiltInWorkgroupId); + const auto main = module.AllocateId(); + module.AddFunction(spv::OpFunction, voidType, main, spv::FunctionControlMaskNone, module.Type(spv::OpTypeFunction, voidType)); + module.AddFunction(spv::OpLabel, module.AllocateId()); + const auto loaded = module.AllocateId(); + module.AddFunction(spv::OpLoad, v3, loaded, group); + const auto index = module.AllocateId(); + module.AddFunction(spv::OpCompositeExtract, u32, index, loaded, 0u); + const auto binary = [&](spv::Op op, std::uint32_t a, std::uint32_t b) { + const auto result = module.AllocateId(); + module.AddFunction(op, u32, result, a, b); + return result; + }; + const auto signedValue = [&](std::uint32_t value) { + const auto result = module.AllocateId(); + module.AddFunction(spv::OpBitcast, i32, result, value); + return result; + }; + const auto coordinate = module.AllocateId(); + module.AddFunction(spv::OpCompositeConstruct, v2, coordinate, signedValue(binary(spv::OpUMod, index, constant(side))), signedValue(binary(spv::OpUDiv, index, constant(side)))); + const auto object = module.AllocateId(); + module.AddFunction(spv::OpLoad, imageType, object, image); + const auto value = module.AllocateId(); + module.AddFunction(spv::OpImageFetch, v4, value, object, coordinate, spv::ImageOperandsLodMask, constant(0u)); + const auto pointer = module.AllocateId(); + module.AddFunction(spv::OpAccessChain, module.Type(spv::OpTypePointer, spv::StorageClassStorageBuffer, v4), pointer, buffer, constant(0u), index); + module.AddFunction(spv::OpStore, pointer, value); + module.AddFunction(spv::OpReturn); + module.AddFunction(spv::OpFunctionEnd); + module.EmitEntryPoint(spv::ExecutionModelGLCompute, main, "main", {group}); + module.AddExecutionMode(main, spv::ExecutionModeLocalSize, 1u, 1u, 1u); + return module.Finalize(); +} + +class TexelFetch { +public: + TexelFetch(const Context& context, const Texture& texture, Buffer& output, std::span code) : context(context) { + try { + const std::array bindings{{{0u, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 1u, VK_SHADER_STAGE_COMPUTE_BIT, nullptr}, {1u, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1u, VK_SHADER_STAGE_COMPUTE_BIT, nullptr}}}; + VkDescriptorSetLayoutCreateInfo info{VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO}; + info.bindingCount = 2u; + info.pBindings = bindings.data(); + Check(context.Function("vkCreateDescriptorSetLayout")(context.device, &info, nullptr, &setLayout), "texel fetch descriptor layout"); + const std::array sizes{{{VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 1u}, {VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1u}}}; + VkDescriptorPoolCreateInfo poolInfo{VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO}; + poolInfo.maxSets = 1u; + poolInfo.poolSizeCount = 2u; + poolInfo.pPoolSizes = sizes.data(); + Check(context.Function("vkCreateDescriptorPool")(context.device, &poolInfo, nullptr, &pool), "texel fetch descriptor pool"); + const VkDescriptorSetAllocateInfo allocate{VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO, nullptr, pool, 1u, &setLayout}; + Check(context.Function("vkAllocateDescriptorSets")(context.device, &allocate, &set), "texel fetch descriptor set"); + const VkDescriptorImageInfo image{VK_NULL_HANDLE, texture.View(), texture.Layout()}; + const VkDescriptorBufferInfo buffer{output.Handle(), 0u, output.Bytes().size()}; + const std::array writes{{{VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, nullptr, set, 0u, 0u, 1u, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, &image, nullptr, nullptr}, {VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, nullptr, set, 1u, 0u, 1u, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, nullptr, &buffer, nullptr}}}; + context.Function("vkUpdateDescriptorSets")(context.device, 2u, writes.data(), 0u, nullptr); + VkPipelineLayoutCreateInfo layoutInfo{VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO}; + layoutInfo.setLayoutCount = 1u; + layoutInfo.pSetLayouts = &setLayout; + Check(context.Function("vkCreatePipelineLayout")(context.device, &layoutInfo, nullptr, &layout), "texel fetch pipeline layout"); + VkShaderModuleCreateInfo shaderInfo{VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO}; + shaderInfo.codeSize = code.size_bytes(); + shaderInfo.pCode = code.data(); + Check(context.Function("vkCreateShaderModule")(context.device, &shaderInfo, nullptr, &module), "texel fetch shader module"); + VkComputePipelineCreateInfo pipelineInfo{VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO}; + pipelineInfo.layout = layout; + pipelineInfo.stage = {VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, nullptr, 0u, VK_SHADER_STAGE_COMPUTE_BIT, module, "main", nullptr}; + Check(context.Function("vkCreateComputePipelines")(context.device, VK_NULL_HANDLE, 1u, &pipelineInfo, nullptr, &pipeline), "texel fetch pipeline"); + } catch (...) { + release(); + throw; + } + } + ~TexelFetch() { release(); } + TexelFetch(const TexelFetch&) = delete; + TexelFetch& operator=(const TexelFetch&) = delete; + + void Run(VkCommandBuffer commands, std::uint32_t groups) const { + context.Function("vkCmdBindPipeline")(commands, VK_PIPELINE_BIND_POINT_COMPUTE, pipeline); + context.Function("vkCmdBindDescriptorSets")(commands, VK_PIPELINE_BIND_POINT_COMPUTE, layout, 0u, 1u, &set, 0u, nullptr); + context.Function("vkCmdDispatch")(commands, groups, 1u, 1u); + } + +private: + void release() noexcept { + if (pipeline != VK_NULL_HANDLE) context.Function("vkDestroyPipeline")(context.device, pipeline, nullptr); + if (module != VK_NULL_HANDLE) context.Function("vkDestroyShaderModule")(context.device, module, nullptr); + if (layout != VK_NULL_HANDLE) context.Function("vkDestroyPipelineLayout")(context.device, layout, nullptr); + if (pool != VK_NULL_HANDLE) context.Function("vkDestroyDescriptorPool")(context.device, pool, nullptr); + if (setLayout != VK_NULL_HANDLE) context.Function("vkDestroyDescriptorSetLayout")(context.device, setLayout, nullptr); + } + + Context context; + VkDescriptorSetLayout setLayout = VK_NULL_HANDLE; + VkDescriptorPool pool = VK_NULL_HANDLE; + VkDescriptorSet set = VK_NULL_HANDLE; + VkPipelineLayout layout = VK_NULL_HANDLE; + VkShaderModule module = VK_NULL_HANDLE; + VkPipeline pipeline = VK_NULL_HANDLE; +}; + +std::vector FetchTexels(const Context& context, const Texture& texture, std::uint32_t side) { + const auto count = side * side; + Buffer output(context, std::size_t{count} * 16u, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT); + std::memset(output.Bytes().data(), 0xa5, output.Bytes().size()); + const TexelFetch fetch(context, texture, output, TexelFetchShader(side)); + CommandBatch batch(context); + fetch.Run(batch.Handle(), count); + RecordMemoryBarrier(context, batch.Handle(), VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_PIPELINE_STAGE_HOST_BIT, VK_ACCESS_SHADER_WRITE_BIT, VK_ACCESS_HOST_READ_BIT); + batch.SubmitAndWait(); + output.Invalidate(); + std::vector texels(count); + for (std::uint32_t index = 0; index < count; ++index) std::memcpy(&texels[index], output.Bytes().data() + std::size_t{index} * 16u, sizeof(std::uint32_t)); + return texels; +} + +void ComparedShared(const Context& context) { + constexpr std::uint32_t side = 128; + constexpr std::size_t bytes = 2 * BlockBytes; + constexpr std::uint32_t before = 0x5a5a5a5au; + constexpr std::uint32_t stored = 0x13579bdfu; + struct Direct { + std::uint8_t* memory = static_cast(GuestArena::GuestArenaAllocate_nid_postfix(2 * bytes, BlockBytes)); + HANDLE section = CreateFileMappingW(INVALID_HANDLE_VALUE, nullptr, PAGE_EXECUTE_READWRITE, 0, bytes, nullptr); + bool registered = false; + ~Direct() { + if (registered) GuestAllocations::Mutation().Remove(memory); + if (memory != nullptr) { + GuestArena::GuestArenaReset_nid_postfix(memory, 2 * bytes); + GuestArena::GuestArenaRelease_nid_postfix(memory, 2 * bytes); + } + if (section == nullptr) return; + GuestArena::GuestArenaForgetSection_nid_postfix(section); + CloseHandle(section); + } + } direct; + Require(direct.memory != nullptr && direct.section != nullptr, "compared shared snapshot: cannot create the direct memory"); + GuestArena::GuestArenaForgetSection_nid_postfix(direct.section); + GuestArena::GuestArenaMapDirect_nid_postfix(direct.memory, bytes, direct.section, 0, PAGE_READWRITE); + GuestAllocations::Mutation().Add(direct.memory, bytes, true, true, true); + direct.registered = true; + const auto address = static_cast(reinterpret_cast(direct.memory)); + std::fill_n(reinterpret_cast(direct.memory), bytes / sizeof(std::uint32_t), before); + Require(AgcDriver::GuestMemory::Watched(address, bytes), "compared shared snapshot: the direct memory is not watched"); + AgcDriver::GuestMemory::Unwatch(address, BlockBytes); + const std::array, 1> imported{{{address, address + BlockBytes}}}; + AgcDriver::GuestMemory::Rewatch(imported); + Require(AgcDriver::GuestMemory::Watched(address, BlockBytes), "compared shared snapshot: the rewatched block is not watched"); + const auto words = Descriptor(address, side); + Require(DescribeSurface(DecodeTextureResource(words)).guestBytes == BlockBytes, "compared shared snapshot: the descriptor does not describe the rewatched block"); + const auto lookup = [&] { + AgcDriver::GuestMemory::BumpCollectEpoch(); + return CachedSampledTexture(context, words); + }; + const auto cached = lookup(); + Require(cached != nullptr && !cached->ViewsStorageImage(), "compared shared snapshot: the rewatched block was not sampled through a snapshot"); + Require(lookup() == cached, "compared shared snapshot: the unchanged rewatched block did not reuse its snapshot"); + const auto initial = FetchTexels(context, *cached, side); + Require(std::all_of(initial.begin(), initial.end(), [](std::uint32_t texel) { return texel == before; }), "compared shared snapshot: the GPU did not read the cached texels"); + GuestArena::GuestArenaMapDirect_nid_postfix(direct.memory + bytes, bytes, direct.section, 0, PAGE_READWRITE); + *reinterpret_cast(direct.memory) = stored; + const auto fresh = lookup(); + Require(fresh != nullptr && fresh != cached, "the first lookup after a CPU store into shared compared memory returned the snapshot cached before the sharing"); + const auto texels = FetchTexels(context, *fresh, side); + Require(texels.front() == stored, "the GPU read the old texel on the first lookup after a CPU store into shared compared memory"); + Require(std::all_of(texels.begin() + 1, texels.end(), [](std::uint32_t texel) { return texel == before; }), "the GPU read texels the CPU store into shared compared memory did not write"); + Require(lookup() == fresh, "the snapshot of shared compared memory was not reused after its compare"); + ClearCachedTextures(context.device); +} +#endif + void PendingStores(const Context& context, ReservedSurface& surface) { const auto address = surface.Address(); const auto words = Descriptor(address); @@ -421,6 +629,14 @@ int main(int argc, char** argv) { LargeTracked(context, surface, LargeSurfaceBytes); LargeTracked(context, surface, BlockBytes); } else if (argc == 2 && std::string_view(argv[1]) == "--pending-stores") PendingStores(context, surface); + else if (argc == 2 && std::string_view(argv[1]) == "--compared-shared") { +#ifdef _WIN32 + ComparedShared(context); +#else + std::puts("skipped, only Windows direct memory is compared once shared"); + return Skipped; +#endif + } else if (argc == 2 && std::string_view(argv[1]) == "--untracked-hint") { #ifdef _WIN32 UntrackedHint(context, surface); diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/PipelineJournal.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/PipelineJournal.cpp index 16d3742761..403380ca02 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/PipelineJournal.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/PipelineJournal.cpp @@ -11,6 +11,7 @@ #include "prx/libc/include/GuestAllocations.hpp" #include "prx/libc/include/GuestArena.hpp" #include "prx/libc/include/GuestWriteWatch.hpp" +#include "ShaderCacheDirectory.hpp" #include "../NarrowConstantStoreFixture.hpp" #include "Recompiler.hpp" #include "Optimization/ResourceProgram.hpp" @@ -31,6 +32,7 @@ #include #include #include +#include #include #include #include @@ -149,7 +151,7 @@ AgcDriver::Graphics::ColorTarget DecodeTarget(const Block& block) { return color; } -void Draw(AgcDriver::VulkanDevice& device, const AgcDriver::Graphics::ColorTarget& color, std::span pixelCode) { +void Draw(AgcDriver::VulkanDevice& device, const AgcDriver::Graphics::ColorTarget& color, std::span pixelCode, std::uint32_t colorWriteMask = 0xf) { const auto target = device.Target(); constexpr std::uint32_t waveSize = 64; std::vector vertexUserData(4, 0u); @@ -180,7 +182,7 @@ void Draw(AgcDriver::VulkanDevice& device, const AgcDriver::Graphics::ColorTarge state.cullMode = VK_CULL_MODE_NONE; state.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE; VkPipelineColorBlendAttachmentState blend{}; - blend.colorWriteMask = 0xf; + blend.colorWriteMask = colorWriteMask; state.blends = {blend}; state.blend = blend; state.blendConstants = {}; @@ -344,6 +346,36 @@ void JournalTests() { } } +void HeldModuleTests() { + using namespace NarrowConstantStoreFixture; + constexpr std::uint32_t programs = 16; + Block compute(BlockBytes); + const auto words = AddressWords(compute); + std::vector> codes; + for (std::uint32_t salt = 1; salt <= programs; ++salt) codes.push_back(ComputeCode(salt)); + { + const auto device = OpenVulkanTestDevice(); + Require(device != nullptr, "no device to record the compute programs"); + device->WaitForPipelineJournalReplay(); + std::vector results; + for (const auto& code : codes) results.push_back(CompileCompute(*device, code, words, 32)); + for (std::size_t index = 0; index < results.size(); ++index) device->Dispatch(results[index], 1, 1, 1, {}, reinterpret_cast(codes[index].data())); + device->WaitIdle(); + AgcDriver::Graphics::FlushPipelineJournal(); + } + SetEnvironment("APS5_PIPELINE_JOURNAL_THREADS", "1"); + const auto before = Snapshot(); + const auto device = OpenVulkanTestDevice(); + Require(device != nullptr, "no device to replay the compute programs"); + device->WaitForPipelineJournalReplay(); + const auto replayed = Snapshot(); + SetEnvironment("APS5_PIPELINE_JOURNAL_THREADS", "2"); + Require(replayed.journal.loadedRecipes == programs + 2 && replayed.journal.loadedModules == programs + 3 && replayed.journal.droppedRecords == 0, "the journal does not hold the 16 compute programs beside the first two pipelines"); + Require(replayed.journal.replayedCompute - before.journal.replayedCompute == programs + 1 && replayed.journal.replayedGraphics - before.journal.replayedGraphics == 1 && replayed.journal.replayRejected == before.journal.replayRejected && replayed.journal.replayFailed == before.journal.replayFailed, "the one-thread replay did not build every recorded pipeline"); + const auto held = replayed.journal.replayHeldPeakBytes; + Require(held > 0 && held * 4 < replayed.journal.loadedBytes, "the one-thread replay held " + std::to_string(held) + " bytes of modules at once from a " + std::to_string(replayed.journal.loadedBytes) + "-byte journal"); +} + // --benchmark [count]: `count` distinct compute programs, dispatched for the first time on three // devices in turn. The first device starts without the driver's pipeline cache file (a driver with // an on-disk shader cache of its own, lavapipe's Mesa cache here, still serves from it unless that @@ -395,6 +427,272 @@ void Benchmark(std::uint32_t count) { std::printf(" warm driver cache, journal: %llu pipelines built by the replay in %.1f ms of replay work (%.1f ms from the device opening), then %llu built at their dispatches, %u dispatches in %.1f ms (%.2f ms each)\n", static_cast(replayed.journal.replayedCompute), replayed.journal.replayMs, replayed.openMs, static_cast(replayed.created), count, replayed.dispatchMs, replayed.dispatchMs / count); } +struct JournalRecordHeader { + std::uint32_t kind; + std::uint32_t reserved; + std::uint64_t payloadBytes; + std::uint64_t payloadHash; +}; +static_assert(sizeof(JournalRecordHeader) == 24); + +constexpr std::uint32_t JournalModuleRecord = 1u; +constexpr std::uint32_t JournalComputeRecord = 3u; +constexpr std::uint32_t JournalHeaderBytes = 32u; + +void AppendJournalRecord(std::vector& file, std::uint32_t kind, const std::vector& payload) { + const JournalRecordHeader header{kind, 0u, payload.size(), ShaderRecompiler::HashBytes(payload)}; + const auto offset = file.size(); + file.resize(offset + sizeof(header) + payload.size()); + std::memcpy(file.data() + offset, &header, sizeof(header)); + std::memcpy(file.data() + offset + sizeof(header), payload.data(), payload.size()); + while ((file.size() - offset - sizeof(header)) % 8u != 0) file.push_back(std::byte{0}); +} + +std::vector ReadJournalComputeIdentities(const std::filesystem::path& path) { + std::vector file; + std::ifstream input(path, std::ios::binary); + const std::vector bytes((std::istreambuf_iterator(input)), std::istreambuf_iterator()); + file.reserve(bytes.size()); + for (const auto byte : bytes) file.push_back(static_cast(byte)); + std::vector identities; + std::size_t cursor = JournalHeaderBytes; + while (cursor + sizeof(JournalRecordHeader) <= file.size()) { + JournalRecordHeader header{}; + std::memcpy(&header, file.data() + cursor, sizeof(header)); + const auto payload = cursor + sizeof(header); + if (header.payloadBytes > file.size() - payload) break; + if (header.kind == JournalComputeRecord && header.payloadBytes >= 17u) { + ShaderRecompiler::PipelineIdentity identity; + std::memcpy(&identity.low, file.data() + payload, sizeof(identity.low)); + std::memcpy(&identity.high, file.data() + payload + 8, sizeof(identity.high)); + identities.push_back(identity); + } + cursor = payload + header.payloadBytes; + while (cursor % 8u != 0) ++cursor; + } + return identities; +} + +void NewestEntriesTests() { + using namespace NarrowConstantStoreFixture; + Block compute(BlockBytes); + const auto words = AddressWords(compute); + const auto recorded = Snapshot(); + { + const auto device = OpenVulkanTestDevice(); + Require(device != nullptr, "the device for the newest entries test could not be opened"); + device->WaitForPipelineJournalReplay(); + for (std::uint32_t salt = 1; salt <= 3; ++salt) { + const auto code = ComputeCode(salt); + const auto result = CompileCompute(*device, code, words, 32); + device->Dispatch(result, 1, 1, 1, {}, reinterpret_cast(code.data())); + } + device->WaitIdle(); + AgcDriver::Graphics::FlushPipelineJournal(); + const auto after = Snapshot(); + Require(after.journal.recordedCompute == recorded.journal.recordedCompute + 3, "the three compute pipelines were not recorded"); + } + SetEnvironment("APS5_PIPELINE_JOURNAL_ENTRIES", "2"); + const auto before = Snapshot(); + { + const auto device = OpenVulkanTestDevice(); + Require(device != nullptr, "the device for the newest entries replay could not be opened"); + device->WaitForPipelineJournalReplay(); + const auto replayed = Snapshot(); + Require(replayed.journal.replayedCompute == before.journal.replayedCompute + 2, "the replay did not build the two newest entries: built " + std::to_string(replayed.journal.replayedCompute - before.journal.replayedCompute) + " compute and " + std::to_string(replayed.journal.replayedGraphics - before.journal.replayedGraphics) + " graphics, " + std::to_string(replayed.journal.replayRejected - before.journal.replayRejected) + " rejected, " + std::to_string(replayed.journal.replayPresent - before.journal.replayPresent) + " present, " + std::to_string(replayed.journal.replaySkipped - before.journal.replaySkipped) + " skipped"); + Require(replayed.journal.replayedGraphics == before.journal.replayedGraphics, "the replay built an entry that is not among the newest"); + const auto created = device->ComputePipelinesCreated(); + const auto newest = ComputeCode(3); + const auto result = CompileCompute(*device, newest, words, 32); + device->Dispatch(result, 1, 1, 1, {}, reinterpret_cast(newest.data())); + device->WaitIdle(); + Require(device->ComputePipelinesCreated() == created, "the newest pipeline was not built by the replay"); + const auto oldest = ComputeCode(0); + const auto old = CompileCompute(*device, oldest, words, 32); + device->Dispatch(old, 1, 1, 1, {}, reinterpret_cast(oldest.data())); + device->WaitIdle(); + Require(device->ComputePipelinesCreated() == created + 1, "the oldest pipeline was built by the replay"); + } + SetEnvironment("APS5_PIPELINE_JOURNAL_ENTRIES", "1024"); +} + +void EvictionTests() { + using namespace NarrowConstantStoreFixture; + const auto path = AgcDriver::Graphics::PipelineJournalPath(); + AgcDriver::Graphics::FlushPipelineJournal(); + constexpr std::size_t Modules = 24; + constexpr std::size_t ModuleBytes = 64u * 1024u; + std::vector file; + for (std::size_t index = 0; index < Modules; ++index) { + std::vector module(20u + ModuleBytes, std::byte{0x5a}); + const std::uint64_t low = index + 1u; + const std::uint64_t high = 0u; + std::memcpy(module.data(), &low, sizeof(low)); + std::memcpy(module.data() + 8, &high, sizeof(high)); + AppendJournalRecord(file, JournalModuleRecord, module); + std::vector compute(17u, std::byte{0}); + std::memcpy(compute.data(), &low, sizeof(low)); + std::memcpy(compute.data() + 8, &high, sizeof(high)); + AppendJournalRecord(file, JournalComputeRecord, compute); + } + { + std::ofstream out(path, std::ios::binary | std::ios::app); + Require(static_cast(out), "the synthetic journal records could not be written"); + out.write(reinterpret_cast(file.data()), static_cast(file.size())); + out.flush(); + Require(!out.fail(), "the synthetic journal records could not be written"); + } + Require(std::filesystem::file_size(path) > 1024u * 1024u, "the journal is smaller than the cap"); + const auto before = Snapshot(); + { + const auto device = OpenVulkanTestDevice(); + Require(device != nullptr, "the device for the eviction test could not be opened"); + device->WaitForPipelineJournalReplay(); + Block compute(BlockBytes); + Block target(ColorTargetLayout(Width, Height, ColorTileMode::Linear, 2).Bytes()); + const auto words = AddressWords(compute); + const auto code = ComputeCode(7); + const auto result = CompileCompute(*device, code, words, 32); + device->Dispatch(result, 1, 1, 1, {}, reinterpret_cast(code.data())); + device->WaitIdle(); + std::memset(target.data, 0, target.bytes); + Draw(*device, DecodeTarget(target), Program(std::array{1.0f, 0.0f, 0.0f, 1.0f})); + device->WaitIdle(); + AgcDriver::Graphics::FlushPipelineJournal(); + } + const auto after = Snapshot(); + Require(after.journal.evictedRecords > before.journal.evictedRecords, "the journal was not rewritten when it reached its cap: " + std::to_string(after.journal.evictedRecords) + " records evicted, " + std::to_string(after.journal.writeFailures - before.journal.writeFailures) + " write failures"); + const auto size = std::filesystem::file_size(path); + Require(size <= 1024u * 1024u + 64u * 1024u, "the rewritten journal is larger than the cap"); + const auto identities = ReadJournalComputeIdentities(path); + Require(!identities.empty(), "the rewritten journal holds no compute pipeline"); + bool newest = false; + for (const auto& identity : identities) { + if (identity.low == Modules) newest = true; + Require(identity.low != 1u, "the oldest pipeline survived the rewrite"); + } + Require(newest, "the newest pipeline did not survive the rewrite"); +} + +void EvictionWatermarkTests() { + using namespace NarrowConstantStoreFixture; + const auto path = AgcDriver::Graphics::PipelineJournalPath(); + const auto before = Snapshot(); + Block compute(BlockBytes); + const auto words = AddressWords(compute); + { + const auto device = OpenVulkanTestDevice(); + Require(device != nullptr, "the device for the eviction watermark test could not be opened"); + device->WaitForPipelineJournalReplay(); + for (std::uint32_t salt = 8; salt <= 10; ++salt) { + auto result = CompileCompute(*device, ComputeCode(salt), words, 32); + result.shaderDataUserWords.assign(16u * 1024u, salt); + AgcDriver::Graphics::RecordComputePipeline(result, true); + } + AgcDriver::Graphics::FlushPipelineJournal(); + } + const auto after = Snapshot(); + Require(after.journal.recordedCompute == before.journal.recordedCompute + 3, "the records after the rewrite were not recorded"); + Require(after.journal.writeFailures == before.journal.writeFailures, "the records after the rewrite failed to write"); + Require(after.journal.evictedRecords == before.journal.evictedRecords, "three consecutive records after a rewrite triggered another rewrite"); + Require(std::filesystem::file_size(path) <= 1024u * 1024u, "the journal exceeded its cap after the consecutive records"); +} + +void EvictionKeepsPendingModules() { + using namespace NarrowConstantStoreFixture; + const auto path = AgcDriver::Graphics::PipelineJournalPath(); + Block target(ColorTargetLayout(Width, Height, ColorTileMode::Linear, 2).Bytes()); + const auto color = DecodeTarget(target); + const std::array red{1.0f, 0.0f, 0.0f, 1.0f}; + { + const auto device = OpenVulkanTestDevice(); + Require(device != nullptr, "the device for the pending module test could not be opened"); + device->WaitForPipelineJournalReplay(); + std::memset(target.data, 0, target.bytes); + Draw(*device, color, Program(red)); + device->WaitIdle(); + AgcDriver::Graphics::FlushPipelineJournal(); + } + constexpr std::size_t FillerBytes = 600u * 1024u; + { + std::vector file; + std::vector module(20u + FillerBytes, std::byte{0x5a}); + const std::uint64_t low = 0x5a5au; + const std::uint64_t high = 0u; + std::memcpy(module.data(), &low, sizeof(low)); + std::memcpy(module.data() + 8, &high, sizeof(high)); + AppendJournalRecord(file, JournalModuleRecord, module); + std::vector compute(17u + FillerBytes, std::byte{0}); + std::memcpy(compute.data(), &low, sizeof(low)); + std::memcpy(compute.data() + 8, &high, sizeof(high)); + AppendJournalRecord(file, JournalComputeRecord, compute); + std::ofstream out(path, std::ios::binary | std::ios::app); + Require(static_cast(out), "the filler records could not be written"); + out.write(reinterpret_cast(file.data()), static_cast(file.size())); + out.flush(); + Require(!out.fail(), "the filler records could not be written"); + } + Require(std::filesystem::file_size(path) > 1024u * 1024u, "the journal is smaller than the cap"); + { + const auto device = OpenVulkanTestDevice(); + Require(device != nullptr, "the device for the pending module test could not be opened"); + device->WaitForPipelineJournalReplay(); + std::memset(target.data, 0, target.bytes); + Draw(*device, color, Program(red), 0x7); + device->WaitIdle(); + AgcDriver::Graphics::FlushPipelineJournal(); + } + const auto before = Snapshot(); + { + const auto device = OpenVulkanTestDevice(); + Require(device != nullptr, "the device for the pending module replay could not be opened"); + device->WaitForPipelineJournalReplay(); + const auto replayed = Snapshot(); + Require(replayed.journal.replayedGraphics == before.journal.replayedGraphics + 1, "the record written after the eviction was not replayed: " + std::to_string(replayed.journal.replayedGraphics - before.journal.replayedGraphics) + " graphics built, " + std::to_string(replayed.journal.replayRejected - before.journal.replayRejected) + " rejected"); + Require(replayed.journal.replayRejected == before.journal.replayRejected, "the record written after the eviction was rejected: the eviction dropped a module it needed"); + } +} + +void OversizedRecordKeepsTheJournal() { + using namespace NarrowConstantStoreFixture; + const auto path = AgcDriver::Graphics::PipelineJournalPath(); + Block compute(BlockBytes); + const auto words = AddressWords(compute); + { + const auto device = OpenVulkanTestDevice(); + Require(device != nullptr, "the device for the oversized record test could not be opened"); + device->WaitForPipelineJournalReplay(); + const auto code = ComputeCode(0); + const auto result = CompileCompute(*device, code, words, 32); + device->Dispatch(result, 1, 1, 1, {}, reinterpret_cast(code.data())); + device->WaitIdle(); + AgcDriver::Graphics::FlushPipelineJournal(); + } + const auto before = Snapshot(); + const auto sizeBefore = std::filesystem::file_size(path); + { + const auto device = OpenVulkanTestDevice(); + Require(device != nullptr, "the device for the oversized record test could not be opened"); + device->WaitForPipelineJournalReplay(); + const auto code = ComputeCode(2); + auto result = CompileCompute(*device, code, words, 32); + result.shaderDataUserWords.assign(400000u, 0u); + AgcDriver::Graphics::RecordComputePipeline(result, false); + AgcDriver::Graphics::FlushPipelineJournal(); + } + const auto after = Snapshot(); + Require(after.journal.writeFailures == before.journal.writeFailures + 1, "the oversized record was not counted as a write failure"); + Require(std::filesystem::file_size(path) == sizeBefore, "the oversized record emptied the journal: " + std::to_string(sizeBefore) + " bytes became " + std::to_string(std::filesystem::file_size(path))); + const auto before2 = Snapshot(); + { + const auto device = OpenVulkanTestDevice(); + Require(device != nullptr, "the device for the oversized record replay could not be opened"); + device->WaitForPipelineJournalReplay(); + const auto replayed = Snapshot(); + Require(replayed.journal.replayedCompute == before2.journal.replayedCompute + 1, "the record that was in the journal did not survive the oversized one"); + } +} + } int main(int argc, char** argv) { @@ -406,6 +704,7 @@ int main(int argc, char** argv) { SetEnvironment("APS5_NO_PIPELINE_JOURNAL", "0"); SetEnvironment("APS5_VERIFY_SET_LAYOUTS", "1"); SetEnvironment("APS5_PIPELINE_JOURNAL_THREADS", "2"); + SetEnvironment("APS5_PIPELINE_JOURNAL_MAX_MIB", "1"); struct Cleanup { std::filesystem::path directory; ~Cleanup() { @@ -422,6 +721,12 @@ int main(int argc, char** argv) { return 0; } JournalTests(); + NewestEntriesTests(); + EvictionTests(); + EvictionWatermarkTests(); + EvictionKeepsPendingModules(); + OversizedRecordKeepsTheJournal(); + HeldModuleTests(); std::puts("pipeline journal record and replay passed"); return 0; } catch (const std::exception& error) { diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/PixelInterlock.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/PixelInterlock.cpp index 116235fa11..74adeac4c0 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/PixelInterlock.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/PixelInterlock.cpp @@ -1,5 +1,6 @@ #include "prx/libSceAgcDriver/Execution/include/VulkanDevice.hpp" #include "prx/libSceAgcDriver/Graphics/include/Draw.hpp" +#include "prx/libSceAgcDriver/Graphics/include/PipelineLibrary.hpp" #include "prx/libc/include/GuestAllocations.hpp" #include "Recompiler.hpp" #include "VulkanTestDevice.hpp" @@ -228,6 +229,7 @@ int main() { Draw(*device, waveSize); Check(waveSize, 3); } + Require(!device->GraphicsPipelineLibraries() || AgcDriver::Graphics::PipelineLibraryCountersOf(device->Device()).linked == 0, "pixel interlock: a primitive-ordered pixel shader's pipeline was linked from graphics pipeline libraries"); std::printf("pixel interlock tests passed: 10 draws of %u overlapping triangles over %u pixels, 2 of them with a killed BDA fault in every %uth column\n", Layers, Width * Height, FaultColumns); return 0; } catch (const std::exception& error) { diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/PixelSampleCoverage.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/PixelSampleCoverage.cpp index ce79a9b375..f3e0672a31 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/PixelSampleCoverage.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/PixelSampleCoverage.cpp @@ -67,7 +67,7 @@ constexpr std::array, 3> OtherHalfTriangle{{ {1.0f, -1.0f, 0.5f, 1.0f}, {1.0f, 1.0f, 0.5f, 1.0f}, {-1.0f, 1.0f, 0.5f, 1.0f} }}; -alignas(256) std::array, 8> Programs{}; +alignas(256) std::array, 16> Programs{}; alignas(256) std::array DepthMemory{}; std::size_t programCount = 0; @@ -164,12 +164,12 @@ struct Block { std::uint8_t* data = nullptr; }; -AgcDriver::Graphics::ColorTarget DecodeTarget(const Block& block, std::uint32_t info) { +AgcDriver::Graphics::ColorTarget DecodeTarget(const Block& block, std::uint32_t info, std::uint32_t attrib = 0) { AgcDriver::Registers cx; cx[0x318] = static_cast(block.Address() >> 8u); cx[0x31b] = 0; cx[0x31c] = info; - cx[0x31d] = 0; + cx[0x31d] = attrib; cx[0x323] = 0; cx[0x324] = 0; cx[0x3b0] = ((Width - 1u) << 14u) | (Height - 1u); @@ -374,6 +374,51 @@ std::string ExpectTruncated(AgcDriver::VulkanDevice& device) { return {}; } +std::string ExpectOneFragment(AgcDriver::VulkanDevice& device) { + if ((AgcDriver::VulkanDeviceTestAccess::Context(device).limits.framebufferDepthSampleCounts & VK_SAMPLE_COUNT_4_BIT) == 0) return "the device has no four-sample depth targets (framebufferDepthSampleCounts)"; + constexpr std::size_t bytes = std::size_t{Width} * Height * 16u; + AgcDriver::Graphics::DepthTarget depth{reinterpret_cast(DepthMemory.data()), 0u, {Width, Height}, VK_FORMAT_D32_SFLOAT, 0.0f, 0}; + depth.samples = 4; + std::array written{}; + std::uint32_t untouched = 0; + for (const auto* triangle : {&HalfTriangle, &OtherHalfTriangle}) { + Block block(bytes); + std::fill(block.data, block.data + bytes, std::uint8_t{0xff}); + const auto color = DecodeTarget(block, Rgba32FloatInfo, 0x2000u); + Require(color.samples == 1u, "a 4-sample color target with one fragment did not decode as one stored sample"); + try { + Draw(device, color, ShaderRecompiler::PixelInputBit(ShaderRecompiler::PixelInput::SampleCoverage), Program(0u), *triangle, 4u, &depth); + } catch (const std::exception& error) { + if (std::string_view(error.what()).find("truncated coverage reduction") == std::string_view::npos) throw; + return "the device lacks VK_AMD_mixed_attachment_samples and VK_NV_framebuffer_mixed_samples with truncated coverage reduction"; + } + const auto stored = ReadBack(device, block.Address(), bytes); + for (std::uint32_t y = 0; y < Height; ++y) { + for (std::uint32_t x = 0; x < Width; ++x) { + const auto* texel = stored.data() + (static_cast(y) * Width + x) * 16u; + if (std::all_of(texel, texel + 16, [](std::uint8_t byte) { return byte == 0xffu; })) { + ++untouched; + continue; + } + std::array value{}; + std::memcpy(value.data(), texel, sizeof(value)); + const auto mask = static_cast(value[0]); + Require(value[0] == static_cast(mask) && mask >= 1u && mask <= 15u && value[1] == 0.0f && value[2] == 0.0f && value[3] == 1.0f, "one-fragment coverage: pixel (" + std::to_string(x) + ", " + std::to_string(y) + ") holds (" + std::to_string(value[0]) + ", " + std::to_string(value[1]) + ", " + std::to_string(value[2]) + ", " + std::to_string(value[3]) + ")"); + ++written[mask]; + } + } + } + std::uint32_t withoutSampleZero = 0; + std::uint32_t partial = 0; + for (std::uint32_t mask = 1; mask < 16u; ++mask) { + if ((mask & 1u) == 0) withoutSampleZero += written[mask]; + else if (mask != 15u) partial += written[mask]; + } + Require(withoutSampleZero == 0, "one-fragment coverage: " + std::to_string(withoutSampleZero) + " pixels whose coverage lacks sample 0 were written into the one fragment"); + Require(written[15] != 0 && partial != 0 && untouched != 0, "one-fragment coverage: two half-covering triangles wrote " + std::to_string(written[15]) + " fully covered and " + std::to_string(partial) + " partly covered pixels and left " + std::to_string(untouched) + " untouched"); + return {}; +} + } int main() { @@ -387,6 +432,7 @@ int main() { Expect(*device, PixelInputBit(PixelInput::Ancillary) | PixelInputBit(PixelInput::SampleCoverage), 1u, "SAMPLE_COVERAGE after ANCILLARY"); if (!ExpectMerged(*device)) std::puts("merged coverage not tested: the device lacks VK_NV_framebuffer_mixed_samples with merged coverage reduction"); if (const auto skipped = ExpectTruncated(*device); !skipped.empty()) std::printf("truncated coverage not tested: %s\n", skipped.c_str()); + if (const auto skipped = ExpectOneFragment(*device); !skipped.empty()) std::printf("one-fragment coverage not tested: %s\n", skipped.c_str()); std::puts("pixel sample coverage tests passed"); return 0; } catch (const std::exception& error) { diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/PooledBufferRange.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/PooledBufferRange.cpp index 785886aa84..988207259f 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/PooledBufferRange.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/PooledBufferRange.cpp @@ -3,6 +3,7 @@ #include "prx/libSceAgcDriver/Graphics/include/BufferPool.hpp" #include "prx/libSceAgcDriver/Graphics/include/Resources.hpp" #include "VulkanTestDevice.hpp" +#include "FixtureDeviceSelection.hpp" #include "../AllocationFailure.hpp" #include "../SlabReturn.hpp" #include @@ -143,6 +144,8 @@ public: Device() { #ifdef _WIN32 library = SDL_LoadObject("vulkan-1.dll"); +#elif defined(__APPLE__) + library = SDL_LoadObject("libvulkan.1.dylib"); #else library = SDL_LoadObject("libvulkan.so.1"); #endif @@ -154,6 +157,12 @@ public: application.apiVersion = VK_API_VERSION_1_1; VkInstanceCreateInfo info{VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO}; info.pApplicationInfo = &application; +#ifdef __APPLE__ + const char* portability = VK_KHR_PORTABILITY_ENUMERATION_EXTENSION_NAME; + info.flags = VK_INSTANCE_CREATE_ENUMERATE_PORTABILITY_BIT_KHR; + info.enabledExtensionCount = 1; + info.ppEnabledExtensionNames = &portability; +#endif Check(function("vkCreateInstance")(&info, nullptr, &instance), "vkCreateInstance"); std::uint32_t count = 0; const auto enumerate = function("vkEnumeratePhysicalDevices"); @@ -162,22 +171,11 @@ public: std::vector devices(count); Check(enumerate(instance, &count, devices.data()), "vkEnumeratePhysicalDevices"); const auto properties = function("vkGetPhysicalDeviceProperties"); - const auto* requested = std::getenv("ANYPS5_GPU"); - const bool named = requested != nullptr && *requested != '\0'; - context.physical = devices.front(); - for (const auto candidate : devices) { - VkPhysicalDeviceProperties described{}; - properties(candidate, &described); - if (named ? std::strstr(described.deviceName, requested) != nullptr : described.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU) { - context.physical = candidate; - break; - } - } + context.physical = SelectFixturePhysicalDevice(devices, properties); VkPhysicalDeviceProperties selected{}; properties(context.physical, &selected); context.limits = selected.limits; std::printf("device %s vendor=0x%x device=0x%x driver=0x%x\n", selected.deviceName, selected.vendorID, selected.deviceID, selected.driverVersion); - Require(requested == nullptr || std::strstr(selected.deviceName, requested) != nullptr, "selected device does not match ANYPS5_GPU"); VkPhysicalDeviceFeatures supported{}; function("vkGetPhysicalDeviceFeatures")(context.physical, &supported); Require(supported.robustBufferAccess == VK_TRUE, "the device lacks robustBufferAccess"); diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/PreparedShaders.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/PreparedShaders.cpp index 141be5d300..88ecd918f8 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/PreparedShaders.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/PreparedShaders.cpp @@ -437,6 +437,15 @@ void SharedCodeHeaders() { AgcDriverRegisterShader_nid_postfix(&mesh.shader); AgcDriverResolveShaderAbi_nid_postfix(&vertex.shader, {}, primitive); AgcDriverRegisterShader_nid_postfix(&mesh.shader); + const auto snapshots = [&] { + const auto registry = AgcDriver::DriverDetail::Driver::Get().RegisteredShaders(); + Require(registry != nullptr, "shared-code headers published no shader registry"); + auto pair = std::pair{AgcDriver::DriverDetail::RegisteredHeader(*registry, &vertex.shader), AgcDriver::DriverDetail::RegisteredHeader(*registry, &mesh.shader)}; + Require(pair.first != nullptr && pair.second != nullptr && pair.first != pair.second, "the shared-code headers were not registered apart"); + return pair; + }; + const auto [vertexSnapshot, meshSnapshot] = snapshots(); + Require(vertexSnapshot->registeredState != meshSnapshot->registeredState, "a header with other registers shared another header's registered state"); Shader copy = vertex.shader; copy.user_data = nullptr; AgcDriverResolveShaderAbi_nid_postfix(©, {}, primitive); @@ -461,6 +470,8 @@ void SharedCodeHeaders() { mesh.shader.cx_registers = mesh.context.data(); mesh.shader.num_cx_registers = mesh.context.size(); AgcDriverRegisterShader_nid_postfix(&mesh.shader); + const auto [sameVertex, sameMesh] = snapshots(); + Require(sameVertex->registeredState == sameMesh->registeredState && sameMesh->registeredState->context.at(0x2d5) == 0x2000u, "two headers with the same code and registers decoded their registered state twice"); AgcDriverResolveShaderAbi_nid_postfix(&mesh.shader, {}, primitive); AgcDriverResolveShaderAbi_nid_postfix(&vertex.shader, {}, primitive); Require(sceAgcDriverSubmitDcb(&packet) == 0, "same-type registered header draw failed"); diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/RectListCorner.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/RectListCorner.cpp index 0657f99bca..eeeb0fd05c 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/RectListCorner.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/RectListCorner.cpp @@ -2,6 +2,9 @@ #include "prx/libSceAgcDriver/Graphics/include/Draw.hpp" #include "Recompiler.hpp" #include "SpirvBackend/SpirvModule.hpp" +#if ANYPS5_ENABLE_SPIRV_TOOLS +#include "SpirvBackend/SpirvOptimizer.hpp" +#endif #include "VulkanTestDevice.hpp" #include #include @@ -46,8 +49,11 @@ public: builder.EmitEntryPoint(spv::ExecutionModelVertex, main, "main", {vertexIndex, position, identity}); result(spv::OpLabel); const auto index = result(spv::OpLoad, intType, vertexIndex); - const auto first = result(spv::OpIEqual, boolType, index, constant(intType, 0u)); - const auto second = result(spv::OpIEqual, boolType, index, constant(intType, 1u)); + const auto boolVectorType = type(spv::OpTypeVector, boolType, 4u); + const auto firstScalar = result(spv::OpIEqual, boolType, index, constant(intType, 0u)); + const auto secondScalar = result(spv::OpIEqual, boolType, index, constant(intType, 1u)); + const auto first = result(spv::OpCompositeConstruct, boolVectorType, firstScalar, firstScalar, firstScalar, firstScalar); + const auto second = result(spv::OpCompositeConstruct, boolVectorType, secondScalar, secondScalar, secondScalar, secondScalar); const auto positionValue = result(spv::OpSelect, vec4Type, first, vector(-1.0f, -1.0f, 0.0f, 1.0f), result(spv::OpSelect, vec4Type, second, vector(1.0f, -1.0f, 0.0f, 1.0f), vector(-1.0f, 1.0f, 0.0f, 1.0f))); const auto identityValue = result(spv::OpSelect, vec4Type, first, vector(0.0f, 0.0f, 0.0f, 1.0f), result(spv::OpSelect, vec4Type, second, vector(1.0f, 0.0f, 0.0f, 1.0f), vector(0.0f, 1.0f, 0.0f, 1.0f))); emit(spv::OpStore, position, positionValue); @@ -128,19 +134,24 @@ std::uint32_t VertexOf(std::uint32_t x, std::uint32_t y) { return (red >= 128u ? 1u : 0u) + (green >= 128u ? 2u : 0u); } -int Run(AgcDriver::VulkanDevice& device) { +int Run(AgcDriver::VulkanDevice& device, std::uint32_t maximumVersion) { std::cout << "device: " << device.DeviceName() << "\n"; auto target = device.Target(); if (!target.tessellation.has_value() || !TargetHasCapability(target, spv::CapabilityTessellation)) { std::cout << "skipped, device has no tessellation\n"; return VulkanTestSkipped; } - if (target.spirvVersion > 0x00010400u) target.spirvVersion = 0x00010400u; + if (target.spirvVersion > maximumVersion) target.spirvVersion = maximumVersion; Require(target.spirvVersion >= 0x00010300u, "rect-list corner draw needs SPIR-V 1.3"); + std::cout << "SPIR-V " << (target.spirvVersion >> 16u) << "." << ((target.spirvVersion >> 8u) & 0xffu) << "\n"; ShaderRecompiler::RecompileResult vertex; ShaderRecompiler::RecompileResult fragment; vertex.spirv = ShaderRecompiler::SharedSpirv(Emitter(target.spirvVersion).Vertex()); fragment.spirv = ShaderRecompiler::SharedSpirv(Emitter(target.spirvVersion).Fragment()); +#if ANYPS5_ENABLE_SPIRV_TOOLS + static_cast(ShaderRecompiler::ValidateAndOptimizeSpirv(vertex.spirv, target.vulkanVersion, target.spirvVersion, target.nonConstantImageOffsets, false)); + static_cast(ShaderRecompiler::ValidateAndOptimizeSpirv(fragment.spirv, target.vulkanVersion, target.spirvVersion, target.nonConstantImageOffsets, false)); +#endif vertex.parameterExports = {0u}; fragment.fragmentParameters = {{0u, 0u, false, false}}; const auto rectangle = ShaderRecompiler::BuildRectListShaders(vertex, fragment, target); @@ -194,8 +205,12 @@ int main() { try { const auto device = OpenVulkanTestDevice(); if (!device) return VulkanTestSkipped; - const auto result = Run(*device); + auto result = Run(*device, 0x00010300u); if (result != 0) return result; + if (device->Target().spirvVersion >= 0x00010400u) { + result = Run(*device, 0x00010400u); + if (result != 0) return result; + } std::cout << "rect-list corner tests passed\n"; return 0; } catch (const std::exception& error) { diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/ResidentHeapMirror.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/ResidentHeapMirror.cpp index 1ea57a498b..bf1cc2eae4 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/ResidentHeapMirror.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/ResidentHeapMirror.cpp @@ -270,6 +270,45 @@ void CopiedTests(AgcDriver::VulkanDevice& device) { CheckStores(written, "copied, after a guest write"); } +struct ImportWindow { + explicit ImportWindow(std::uint64_t bytes) { AgcDriver::Graphics::SetHostImportRangeWindow(bytes); } + ~ImportWindow() { AgcDriver::Graphics::SetHostImportRangeWindow(std::uint64_t{64} << 20u); } + ImportWindow(const ImportWindow&) = delete; + ImportWindow& operator=(const ImportWindow&) = delete; +}; + +void PartialImportTests(AgcDriver::VulkanDevice& device, bool copied, bool readFirst) { + const AddressSpaceLimit limit(copied ? 1 : 0); + const ImportWindow window(2 * Block); + GuestHeap first(!readFirst); + GuestHeap second(readFirst); + const auto& read = readFirst ? first : second; + const auto& written = readFirst ? second : first; + const OutputBlock output; + const auto tail = HeapBytes - Block; + for (std::uint32_t thread = 0; thread * ThreadStride < Block; ++thread) read.Store(thread * ThreadStride, Pattern(thread)); + for (std::uint32_t thread = 0; thread < Threads; ++thread) { + read.Store(tail + thread * sizeof(std::uint32_t), Pattern(thread)); + written.Store(tail + thread * sizeof(std::uint32_t), Pattern(thread)); + } + const ImportBudget budget(AgcDriver::Graphics::HostImportLiveBytes() + 3 * Block); + Dispatch(device, read, written, output); + CheckReads(output, "partial heap import", false); + const auto& selected = read.Address() < written.Address() ? read : written; + const auto* prefix = AgcDriver::Graphics::ExistingHostImportFor(device.graphicsContext(), selected.Address(), 2 * Block); + Require(prefix != nullptr && prefix->base == selected.Address() && prefix->bytes == 2 * Block, "partial heap import: the first registered heap has no exact two-block imported prefix"); + const auto held = AgcDriver::Graphics::HeapMirrorsWithin(selected.Address(), selected.Address() + HeapBytes); + const auto expected = copied ? HeapBytes - prefix->bytes : (selected.Address() == read.Address() ? Block : 2 * Block); + Require(held.mirrors == 1 && held.bytes == expected, "partial heap import: its mirror holds " + std::to_string(held.bytes) + " bytes, expected " + std::to_string(expected)); + Copy(device, selected.Address() + tail, output); + for (std::uint32_t thread = 0; thread < Threads; ++thread) Require(output.Data()[thread] == Pattern(thread), "partial heap import: GPU read of the mirrored tail lost its contents"); + selected.Store(tail + 2 * sizeof(std::uint32_t), 0x12345678u); + Copy(device, selected.Address() + tail, output); + for (std::uint32_t thread = 0; thread < Threads; ++thread) Require(output.Data()[thread] == (thread == 2 ? 0x12345678u : Pattern(thread)), "partial heap import: refresh lost a CPU write or its unchanged neighbors"); + const auto refreshed = AgcDriver::Graphics::HeapMirrorsWithin(selected.Address(), selected.Address() + HeapBytes); + Require(refreshed.mirrors == 1 && refreshed.bytes == expected, "partial heap import: refreshing the selected tail changed its accounted extent"); +} + void ReusedAddressTests(AgcDriver::VulkanDevice& device) { GuestHeap written(true); const OutputBlock output; @@ -342,6 +381,9 @@ int main() { ReusedAddressTests(*device); JoinedImportTests(*device); CopiedTests(*device); + for (const bool copied : {false, true}) { + for (const bool readFirst : {false, true}) PartialImportTests(*device, copied, readFirst); + } std::puts("resident heap mirror tests passed"); return 0; } catch (const std::exception& error) { diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/SampleOrder.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/SampleOrder.cpp index 9e534056ab..ba9346b3e7 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/SampleOrder.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/SampleOrder.cpp @@ -1122,6 +1122,22 @@ Samples ReadTarget(const Context& context, VkImageView view, bool color, std::ui return ReadValues(values, count); } +Samples ReadDepth(const Context& context, const AgcDriver::Graphics::DepthTarget& depth, std::uint32_t samples) { + const auto target = AgcDriver::Graphics::DepthSurfaceOrderTarget(context, depth); + const VkImageAspectFlags aspects = VK_IMAGE_ASPECT_DEPTH_BIT | (AgcDriver::Graphics::HasStencil(target.format) ? VK_IMAGE_ASPECT_STENCIL_BIT : 0u); + Image copy(context, target.format, samples, ReferenceExtent, DepthUsage, 0u); + VkImageCopy region{}; + region.srcSubresource = {VK_IMAGE_ASPECT_DEPTH_BIT, 0u, 0u, 1u}; + region.dstSubresource = region.srcSubresource; + region.extent = {ReferenceExtent.width, ReferenceExtent.height, 1u}; + Submit(context, [&](VkCommandBuffer commands) { + copy.ToGeneral(commands, aspects); + FullBarrier(context, commands); + context.Function("vkCmdCopyImage")(commands, target.image, VK_IMAGE_LAYOUT_GENERAL, copy.Handle(), VK_IMAGE_LAYOUT_GENERAL, 1u, ®ion); + }); + return ReadTarget(context, copy.View(target.format, VK_IMAGE_ASPECT_DEPTH_BIT, VK_IMAGE_USAGE_SAMPLED_BIT), false, samples); +} + Rendered RenderCheckerboard(AgcDriver::VulkanDevice& device, const Context& context, std::uint32_t samples, std::size_t run, const std::optional>& locations) { AgcDriver::Graphics::ColorTarget color{}; color.extent = ReferenceExtent; @@ -1169,7 +1185,7 @@ Rendered RenderCheckerboard(AgcDriver::VulkanDevice& device, const Context& cont Settle(device); Rendered rendered; rendered.color = ReadTarget(context, AgcDriver::Graphics::MultisampleTargetView(context, color), true, samples); - rendered.depth = ReadTarget(context, AgcDriver::Graphics::DepthSurfaceOrderTarget(context, depth).view, false, samples); + rendered.depth = ReadDepth(context, depth, samples); return rendered; } @@ -1348,7 +1364,7 @@ void CheckRecipeDepth(AgcDriver::VulkanDevice& device, const Context& context, c std::printf("%s: builds no recipe, since a draw that reorders its samples is built in full each time, with its sample swaps\n", what.c_str()); return; } - const auto read = [&] { return ReadTarget(context, AgcDriver::Graphics::DepthSurfaceOrderTarget(context, depth).view, false, samples); }; + const auto read = [&] { return ReadDepth(context, depth, samples); }; Samples recorded; slopes.beforeReplay = [&] { Settle(device); diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/SampledCacheBudget.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/SampledCacheBudget.cpp index 10ccf51718..6cfb074620 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/SampledCacheBudget.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/SampledCacheBudget.cpp @@ -1004,7 +1004,7 @@ std::uint64_t ExpectedBudget(const Context& context) { VkPhysicalDeviceMemoryBudgetPropertiesEXT reported{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MEMORY_BUDGET_PROPERTIES_EXT}; VkPhysicalDeviceMemoryProperties2 properties{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MEMORY_PROPERTIES_2, &reported}; ReportBudget(context.physical, &properties); - return SampledTextureBudget(context.memory, &reported, SampledTextureMemory() + TextureCacheUsage().storageBytes); + return SampledTextureBudget(context.memory, &reported, SampledTextureMemory() + TextureCacheUsage().storageBytes, TextureCacheUsage().storageBytes); } std::uint64_t ReadBudget(const Context& context) { @@ -1049,8 +1049,10 @@ void storageMemoryTests(Context context) { context.memoryProperties2 = &ReportBudget; Block block(context); auto held = Sampled(context, block.Surface(0)); - const auto quarter = std::min(StorageTextureBudget(context.memory, nullptr, 0, 0), ReportedBudget / 4u); - Require(ReadStorageBudget(context) == quarter, "52 GiB free of a 64 GiB budget does not leave the storage image cache its quarter"); + const auto heap = LargestDeviceLocalHeap(context.memory); + Require(heap.has_value(), "the device has no device-local heap"); + const auto half = std::min(context.memory.memoryHeaps[*heap].size / 2u, ReportedBudget / 2u); + Require(ReadStorageBudget(context) == half, "52 GiB free of a 64 GiB budget does not leave the storage image cache half of the heap or of the budget"); context.memoryProperties2 = &ReportPressure; const auto pressed = ReadStorageBudget(context); const auto expected = GiB + TextureCacheUsage().storageBytes; diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/SgprPatternFillKeys.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/SgprPatternFillKeys.cpp index c6dd1e4ca1..8d0bdf3153 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/SgprPatternFillKeys.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/SgprPatternFillKeys.cpp @@ -98,7 +98,7 @@ void MatchTests() { alignas(8) std::uint32_t Finished = 0; std::uint32_t Submitted = 0; -constexpr std::uint32_t MaxResubmits = 16; +constexpr std::uint32_t MaxResubmits = 512; void Submit(const std::vector& data, std::uint32_t groups) { std::vector words; diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/SparseBufferHoles.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/SparseBufferHoles.cpp index 64835ea370..55cae19285 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/SparseBufferHoles.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/SparseBufferHoles.cpp @@ -2,6 +2,7 @@ #include "prx/libSceAgcDriver/Graphics/include/GuestBufferMemory.hpp" #include "prx/libSceAgcDriver/Graphics/include/Resources.hpp" #include "VulkanTestDevice.hpp" +#include "FixtureDeviceSelection.hpp" #include #ifdef _WIN32 #ifndef NOMINMAX @@ -40,6 +41,8 @@ public: Device() { #ifdef _WIN32 library = SDL_LoadObject("vulkan-1.dll"); +#elif defined(__APPLE__) + library = SDL_LoadObject("libvulkan.1.dylib"); #else library = SDL_LoadObject("libvulkan.so.1"); #endif @@ -51,6 +54,12 @@ public: application.apiVersion = VK_API_VERSION_1_1; VkInstanceCreateInfo info{VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO}; info.pApplicationInfo = &application; +#ifdef __APPLE__ + const char* portability = VK_KHR_PORTABILITY_ENUMERATION_EXTENSION_NAME; + info.flags = VK_INSTANCE_CREATE_ENUMERATE_PORTABILITY_BIT_KHR; + info.enabledExtensionCount = 1; + info.ppEnabledExtensionNames = &portability; +#endif Check(function("vkCreateInstance")(&info, nullptr, &instance), "vkCreateInstance"); std::uint32_t count = 0; const auto enumerate = function("vkEnumeratePhysicalDevices"); @@ -58,18 +67,7 @@ public: Require(count != 0, "no Vulkan device"); std::vector devices(count); Check(enumerate(instance, &count, devices.data()), "vkEnumeratePhysicalDevices"); - const auto properties = function("vkGetPhysicalDeviceProperties"); - int rank = -1; - for (const auto candidate : devices) { - VkPhysicalDeviceProperties described{}; - properties(candidate, &described); - if (described.apiVersion < VK_API_VERSION_1_1) continue; - const int candidateRank = described.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU ? 2 : described.deviceType == VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU ? 1 : 0; - if (candidateRank <= rank) continue; - context.physical = candidate; - rank = candidateRank; - } - Require(context.physical != VK_NULL_HANDLE, "no Vulkan 1.1 device"); + context.physical = SelectFixturePhysicalDevice(devices, function("vkGetPhysicalDeviceProperties")); VkPhysicalDeviceSubgroupProperties subgroup{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_PROPERTIES}; VkPhysicalDeviceProperties2 selected{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2, &subgroup}; function("vkGetPhysicalDeviceProperties2")(context.physical, &selected); diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/SparseIndirectIndices.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/SparseIndirectIndices.cpp index 809321d1b5..20a4dc48e2 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/SparseIndirectIndices.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/SparseIndirectIndices.cpp @@ -168,7 +168,7 @@ void Target(std::vector& words, void* pointer) { Append(words, 0x69, {0x390, static_cast(address >> 40u)}); } -void Run(SparseIndices& indices, std::uint32_t size, bool multi, bool afterGap, bool unreadable) { +void Run(SparseIndices& indices, std::uint32_t size, bool multi, bool afterGap, bool unreadable, bool sized = true, std::uint32_t second = DeclaredIndices) { indices.Fill(size); Reference.fill(Kept); Actual.fill(Kept); @@ -177,7 +177,7 @@ void Run(SparseIndices& indices, std::uint32_t size, bool multi, bool afterGap, const auto first = unreadable ? 16u / size : afterGap ? indices.AfterGap(size) : 0u; Records[8] = 3; Records[9] = 1; - Records[10] = DeclaredIndices; + Records[10] = second; Records[16] = 3; Records[17] = 1; Records[18] = 16u / size; @@ -192,7 +192,7 @@ void Run(SparseIndices& indices, std::uint32_t size, bool multi, bool afterGap, Target(words, Actual.data()); Append(words, 0x2a, {size == 2 ? 0u : 1u}); Append(words, 0x26, {Low(indices.Base()), High(indices.Base())}); - Append(words, 0x13, {DeclaredIndices}); + if (sized) Append(words, 0x13, {DeclaredIndices}); Append(words, 0x11, {1, Low(Records.data()), High(Records.data())}); if (multi) Append(words, 0x38, {0, 0, 0, 0x40000280u, 3, Low(Count.data()), High(Count.data()), 32, 0}); else Append(words, 0x25, {0, 0, 0, 0}); @@ -204,7 +204,7 @@ void Run(SparseIndices& indices, std::uint32_t size, bool multi, bool afterGap, const auto covered = std::count_if(Reference.begin(), Reference.end(), [](std::byte value) { return value != Kept; }); Require(covered != 0, "the reference triangle did not draw"); Require(Actual == Reference, "the sparse indexed indirect draw differs from the reference triangle"); - std::cout << "sparse indices " << size << " bytes, " << (multi ? "counted multi" : "single") << ", " << (afterGap ? "after gap" : "before gap") << " passed\n"; + std::cout << "sparse indices " << size << " bytes, " << (multi ? "counted multi" : "single") << ", " << (afterGap ? "after gap" : "before gap") << (sized ? "" : ", no index size") << " passed\n"; } } @@ -218,10 +218,13 @@ int main(int argc, char** argv) { Register(VertexHeader, VertexCode.data(), sizeof(VertexCode), 2); Register(PixelHeader, PixelCode.data(), sizeof(PixelCode), 1); SparseIndices indices; - if (argc == 2 && std::string(argv[1]) == "--unreadable") { + const std::string mode = argc == 2 ? argv[1] : ""; + if (mode == "--unreadable" || mode == "--no-size-unreadable" || mode == "--no-size-unreadable-record") { bool refused = false; try { - Run(indices, 2, false, false, true); + if (mode == "--unreadable") Run(indices, 2, false, false, true); + else if (mode == "--no-size-unreadable") Run(indices, 2, false, false, true, false); + else Run(indices, 2, true, false, false, false); } catch (const std::runtime_error& error) { const std::string message = error.what(); refused = message.find("guest memory is not readable") != std::string::npos && message.find("+0x6)") != std::string::npos; @@ -232,10 +235,11 @@ int main(int argc, char** argv) { std::cout << "the unreadable indirect index range was refused\n"; return 0; } + const bool sized = mode != "--no-size"; for (const auto size : {2u, 4u}) { for (const auto multi : {false, true}) { - Run(indices, size, multi, false, false); - Run(indices, size, multi, true, false); + Run(indices, size, multi, false, false, sized, sized ? DeclaredIndices : 0u); + Run(indices, size, multi, true, false, sized, sized ? DeclaredIndices : 0u); } } AgcDriverShutdown_nid_postfix(); diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/StorageFileWrite.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/StorageFileWrite.cpp index 6fd7ebae81..d50aceebbc 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/StorageFileWrite.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/StorageFileWrite.cpp @@ -237,7 +237,7 @@ bool CheckWrite(Api api, const char* name, GuestSurface& surface, GuestSurface& KernelAioResult result{}; KernelAioRwRequest request{FileOffset, TransferBytes, source, &result, output}; std::int32_t id = 0; - Require((api == Api::Aio ? sceKernelAioSubmitWriteCommands(&request, 1, 0, &id) : sceKernelAioSubmitWriteCommandsMultiple(&request, 1, 0, &id)) == 0, "file write: AIO submission failed"); + Require((api == Api::Aio ? sceKernelAioSubmitWriteCommands(&request, 1, 1, &id) : sceKernelAioSubmitWriteCommandsMultiple(&request, 1, 1, &id)) == 0, "file write: AIO submission failed"); std::int32_t removed = -1; Require(sceKernelAioDeleteRequest(id, &removed) == 0 && removed == 0, "file write: AIO deletion failed"); Require(result.state == 3, "file write: AIO did not complete"); diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/StoreIdleResults.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/StoreIdleResults.cpp index b2935dd9c9..c9c8e08c3a 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/StoreIdleResults.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/StoreIdleResults.cpp @@ -112,7 +112,7 @@ int main(int argc, char** argv) { Store(texels); if (wake) { - const auto pendingDeadline = std::chrono::steady_clock::now() + std::chrono::seconds(2); + const auto pendingDeadline = std::chrono::steady_clock::now() + std::chrono::seconds(10); while (!AgcDriver::Graphics::StorageTexture::AnyPending() && std::chrono::steady_clock::now() < pendingDeadline) std::this_thread::sleep_for(std::chrono::milliseconds(1)); Require(AgcDriver::Graphics::StorageTexture::AnyPending(), "idle wake fixture did not observe pending storage results"); std::this_thread::sleep_for(std::chrono::milliseconds(5)); @@ -127,7 +127,7 @@ int main(int argc, char** argv) { cpu = nextTexels; Store(nextTexels); } - const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(2); + const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(10); while (!stored() && std::chrono::steady_clock::now() < deadline) std::this_thread::sleep_for(std::chrono::milliseconds(1)); Require(stored(), "the queue-idle store did not bring the result to guest memory without a direct flush or further submission"); diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/SubmitProgress.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/SubmitProgress.cpp new file mode 100644 index 0000000000..1996782ef5 --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/tests/execution/SubmitProgress.cpp @@ -0,0 +1,154 @@ +#include "VulkanTestDevice.hpp" +#include "prx/libSceAgcDriver/Execution/include/Driver.hpp" +#include "prx/libSceAgcDriver/Execution/include/GuestMemory.hpp" +#include "prx/libc/include/GuestAllocations.hpp" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#ifdef _WIN32 +#include +#endif + +namespace { +void Require(bool condition, const char* message) { if (!condition) throw std::runtime_error(message); } +alignas(256) std::array Code{0xf4080200u, 0xfa000000u, 0xbe840381u, 0x9304ff04u, 0x0019660du, 0x8004ff04u, 0x3c6ef35fu, 0x80828102u, 0xbf078002u, 0xbf85fff9u, 0xbf8cc07fu, 0x7e000204u, 0xe0700000u, 0x80020000u, 0xbf810000u}; +std::uint32_t Low(const void* p) { return static_cast(reinterpret_cast(p)); } +std::uint32_t High(const void* p) { return static_cast(reinterpret_cast(p) >> 32u); } +void Set(std::vector& words, std::uint32_t reg, std::uint32_t value) { words.insert(words.end(), {0xc0017600u, reg, value}); } +void Dispatch(std::vector& words, const void* descriptor, std::uint32_t iterations = 32) { + const auto address = reinterpret_cast(Code.data()); + Set(words, 0x20c, static_cast(address >> 8u)); + Set(words, 0x20d, static_cast(address >> 40u)); + for (auto reg : {0x207u, 0x208u, 0x209u}) Set(words, reg, 1); + Set(words, 0x212, 0); Set(words, 0x213, 6); Set(words, 0x240, Low(descriptor)); Set(words, 0x241, High(descriptor)); Set(words, 0x242, iterations); + words.insert(words.end(), {0xc0031500u, 1, 1, 1, 0x8041u}); +} +void Release(std::vector& words, const void* label, std::uint32_t value) { + const std::uint32_t interrupt = value % 2 == 0 ? 2 : 0; + words.insert(words.end(), {0xc0064900u, 0x514u, (2u << 29u) | (interrupt << 24u), Low(label), High(label), value, 0u, 0u}); +} +void Await(const std::uint32_t* label, std::uint32_t value) { + const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(2); + while (*static_cast(label) != value) { + Require(std::chrono::steady_clock::now() < deadline, "submission lost its real GPU completion store"); + std::this_thread::yield(); + } + AgcDriverWaitIdle_nid_postfix(); +} +} + +int main() { + void* block = nullptr; + try { + auto device = OpenVulkanTestDevice(); if (!device) return VulkanTestSkipped; device.reset(); +#ifdef _WIN32 + block = VirtualAlloc(nullptr, 65536, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); +#else + block = std::aligned_alloc(65536, 65536); +#endif + Require(block != nullptr, "cannot allocate submit-progress guest memory"); + std::memset(block, 0, 65536); + GuestAllocations::Mutation().Add(block, 65536, true, true, true); + auto* output = static_cast(block); + auto* descriptor = output + 64; auto* label = output + 128; auto* neighbor = output + 192; + descriptor[0] = Low(output); descriptor[1] = High(output) & 0xffffu; descriptor[2] = 16; descriptor[3] = 0x31016facu; + *neighbor = 0xabcdef12u; + const auto submit = [&](std::uint32_t value) { + *label = 0; label[1] = 0xfeedbeefu; + AgcDriver::GuestMemory::MarkWritten(reinterpret_cast(label), 8); + std::vector words; Dispatch(words, descriptor); + if (value % 2 != 0) { + auto* selfGate = output + 384; selfGate[0] = 0; selfGate[1] = 0; + AgcDriver::GuestMemory::MarkWritten(reinterpret_cast(selfGate), 8); + Release(words, selfGate, 1); + if (value % 3 == 0) words.insert(words.end(), {0xc0079300u, 0x13u, Low(selfGate), High(selfGate), 1, 0, 0xffffffffu, 0xffffffffu, 4}); + else words.insert(words.end(), {0xc0053c00u, 0x13u, Low(selfGate), High(selfGate), 1, 0xffffffffu, 4}); + } + Release(words, label, value); + Packet packet{words.data(), static_cast(words.size()), 0, {}}; + AgcDriver::Submit(&packet, 0); + }; + submit(1); Await(label, 1); + std::uint32_t ready = 0; + for (std::uint32_t value = 2; value < 66; ++value) { + submit(value); + ready += *static_cast(label) == value; + Await(label, value); + Require(*label == value, "submission lost its real GPU completion store"); + Require(label[1] == 0, "64-bit GPU marker did not replace its genuine CPU-written high word"); + Require(*neighbor == 0xabcdef12u, "progress wait damaged neighboring guest bytes"); + } + std::cout << ready << "/64 small dependent submissions completed at return\n"; + std::uint32_t prefixesReady = 0; + for (std::uint32_t index = 0; index < 16; ++index) { + auto* continued = output + 320; + *continued = 0; *label = 0; + AgcDriver::GuestMemory::MarkWritten(reinterpret_cast(label), 8); + AgcDriver::GuestMemory::MarkWritten(reinterpret_cast(continued), 8); + std::vector prefix; Dispatch(prefix, descriptor); Release(prefix, label, 100 + index); + prefix.insert(prefix.end(), {0xc0005900u, 0u}); + const auto control = prefix.size() - 1; + Release(prefix, continued, 200 + index); + Packet prefixPacket{prefix.data(), static_cast(prefix.size()), 0, {}}; + AgcDriver::Submit(&prefixPacket, 0); + prefixesReady += *static_cast(label) == 100 + index; + Require(*continued == 0, "an unreleased REWIND continuation executed early"); + std::atomic logicalDone{false}, waiterFailed{false}; + std::thread waiter; + bool earlyLogicalCompletion = false; + if (index == 0) { + waiter = std::thread([&] { + try { AgcDriverWaitIdle_nid_postfix(); logicalDone.store(true, std::memory_order_release); } + catch (...) { waiterFailed.store(true, std::memory_order_release); } + }); + std::this_thread::sleep_for(std::chrono::milliseconds(2)); + earlyLogicalCompletion = logicalDone.load(std::memory_order_acquire); + } + std::atomic_ref(prefix[control]).store(0x80000000u, std::memory_order_release); + AgcDriver::GuestMemory::MarkWritten(reinterpret_cast(&prefix[control]), 4); + Await(label, 100 + index); Await(continued, 200 + index); + if (waiter.joinable()) waiter.join(); + Require(!waiterFailed.load() && !earlyLogicalCompletion, "prefix acknowledgement completed the unreleased logical submission"); + } + std::cout << prefixesReady << "/16 finite REWIND prefixes completed at return\n"; + auto* gate = output + 256; *gate = 0; *label = 0; + AgcDriver::GuestMemory::MarkWritten(reinterpret_cast(gate), 4); + AgcDriver::GuestMemory::MarkWritten(reinterpret_cast(label), 4); + std::vector waiting{0xc0053c00u, 0x13u, Low(gate), High(gate), 1, 0xffffffffu, 0x19u}; + Release(waiting, label, 77); + Packet waitPacket{waiting.data(), static_cast(waiting.size()), 0, {}}; + AgcDriver::Submit(&waitPacket, 0); + Require(*label == 0, "dependent wait fabricated its completion"); + *gate = 1; + AgcDriver::GuestMemory::MarkWritten(reinterpret_cast(gate), 4); + Await(label, 77); + *label = 0; + AgcDriver::GuestMemory::MarkWritten(reinterpret_cast(label), 4); + std::vector slow; Dispatch(slow, descriptor, 1u << 22u); Release(slow, label, 78); + Packet slowPacket{slow.data(), static_cast(slow.size()), 0, {}}; + const auto began = std::chrono::steady_clock::now(); + AgcDriver::Submit(&slowPacket, 0); + Require(std::chrono::steady_clock::now() - began < std::chrono::milliseconds(100), "progress budget became an unbounded GPU drain"); + Await(label, 78); + Require(*neighbor == 0xabcdef12u, "pending progress work damaged neighboring guest bytes"); + AgcDriverShutdown_nid_postfix(); + GuestAllocations::Mutation().Remove(block); +#ifdef _WIN32 + VirtualFree(block, 0, MEM_RELEASE); +#else + std::free(block); +#endif + return 0; + } catch (const std::exception& error) { + std::cerr << error.what() << '\n'; + try { AgcDriverShutdown_nid_postfix(); } catch (...) {} + return 1; + } +} diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/Tessellation.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/Tessellation.cpp index 8eee7b840b..7f13530d09 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/Tessellation.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/Tessellation.cpp @@ -157,6 +157,22 @@ alignas(256) constexpr std::array LocalIndexAsData{ 0x0000030a, 0xd8340010, 0x0000070a, 0xbf8cc07f, 0xbefd2106, }; +alignas(256) constexpr std::array LocalInactiveIndex{ + 0x7e108704, 0x7da80b02, 0x160e06a8, 0x7e060302, 0xd9340020, 0x00000207, 0xbe802006, +}; + +alignas(256) constexpr std::array LocalRestoredIndex{ + 0x7e108704, 0x160e06a8, 0x7da80b02, 0x7e060302, 0xbefe04c1, 0xd9340020, 0x00000207, 0xbe802006, +}; + +alignas(256) constexpr std::array LocalObservedWindow{ + 0xbefc0383, 0x7da80b02, 0x160e06a8, 0x7e108704, 0xd9340020, 0x00000807, 0xbe802006, +}; + +alignas(256) constexpr std::array LocalNegatedConjunction{ + 0x7d880b02, 0x160e06a8, 0x02100e80, 0x7da20d02, 0x87fe6a7e, 0xbefe087e, 0xd9340020, 0x00000807, 0xbe802006, +}; + alignas(256) constexpr std::array HullDescendingRecords{ 0xd7650000, 0x000100c1, 0xd7660000, 0x000200c1, 0xbf8a0000, 0x7da206f9, 0x81060000, 0xd5480014, 0x02151101, 0x362402ff, 0x000000ff, 0x160028a0, 0xd5430015, 0x040224ff, 0x00000060, 0xdbfc0000, @@ -1005,6 +1021,13 @@ int main() { ExpectRefusal(target, ShaderStage::TessellationEvaluation, DomainNeighbourPoint, "neither its patch's control points nor a patch-constant record", RecordPatches); ExpectRefusal(target, ShaderStage::TessellationEvaluation, DomainCulledPrimitive, "passthrough primitive"); ExpectRefusal(target, ShaderStage::Local, LocalIndexAsData, "outside a tessellation ring or LDS address"); + { + std::uint32_t cursor = 0; + Require(!Compile(target, ShaderStage::Local, LocalInactiveIndex, cursor).spirv.empty(), "a local program whose relative index survives only on inactive lanes was not compiled"); + } + ExpectRefusal(target, ShaderStage::Local, LocalRestoredIndex, "outside a tessellation ring or LDS address"); + ExpectRefusal(target, ShaderStage::Local, LocalObservedWindow, "outside a tessellation ring or LDS address"); + ExpectRefusal(target, ShaderStage::Local, LocalNegatedConjunction, "outside a tessellation ring or LDS address"); ExpectRefusal(target, ShaderStage::TessellationControl, HullRelativeData, "GE_CNTL.PRIM_GRP_SIZE is 0", RecordPatches); ExpectRefusal(target, ShaderStage::TessellationEvaluation, DomainRelativeData, "GE_CNTL.PRIM_GRP_SIZE is 0", RecordPatches); ExpectRefusal(target, ShaderStage::TessellationControl, HullCode, "outside its patch's quad factors", {{3u, 3u, 2u, 2u, 2u}, Indices}); diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/ZeroStrideVertexFetch.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/ZeroStrideVertexFetch.cpp index e5804bf70a..0642450135 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/ZeroStrideVertexFetch.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/ZeroStrideVertexFetch.cpp @@ -124,6 +124,28 @@ void Draw(AgcDriver::VulkanDevice& device, const Case& value) { device.WaitIdle(); } +void DescriptorDiagnostics(AgcDriver::VulkanDevice& device) { + for (std::uint32_t component = 0; component < 4u; ++component) { + for (const auto selector : {2u, 3u}) { + const auto shift = component * 3u; + const Case value{3u, 16u, true, "reserved selector", (0xfacu & ~(7u << shift)) | (selector << shift)}; + const auto fields = VertexBufferDescriptor(value); + char expected[192]; + std::snprintf(expected, sizeof(expected), "reserved prepared vertex component selector %u at location 0 component %u (V#=[%08x %08x %08x %08x])", + selector, component, fields[0], fields[1], fields[2], fields[3]); + bool rejected = false; + try { + Draw(device, value); + } catch (const std::runtime_error& error) { + const std::string message(error.what()); + Require(message.find(expected) != std::string::npos, "reserved selector error did not identify the component and descriptor: " + message); + rejected = true; + } + Require(rejected, "a reserved vertex component selector was accepted"); + } + } +} + void Check(const Case& value) { for (std::uint32_t vertex = 0; vertex < Vertices; ++vertex) { for (std::uint32_t component = 0; component < 4u; ++component) { @@ -169,6 +191,7 @@ int main() { Draw(*device, value); Check(value); } + DescriptorDiagnostics(*device); std::puts("zero-stride vertex fetch tests passed"); return 0; } catch (const std::exception& error) { diff --git a/core/libs/prx/libSceAgcDriver/tools/FrameReplay.cpp b/core/libs/prx/libSceAgcDriver/tools/FrameReplay.cpp new file mode 100644 index 0000000000..ac6a434a0e --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/tools/FrameReplay.cpp @@ -0,0 +1,744 @@ +#include "prx/libSceAgcDriver/Execution/include/DisplayBuffer.hpp" +#include "prx/libSceAgcDriver/Execution/include/Driver.hpp" +#include "prx/libSceAgcDriver/Execution/include/FrameCapture.hpp" +#include "prx/libSceAgcDriver/Execution/include/FrameDumpIo.hpp" +#include "prx/libSceAgcDriver/Execution/include/VideoOutput.hpp" +#include "prx/libSceAgcDriver/Submit/include/Acb.hpp" +#include "prx/libSceAgcDriver/Submit/include/Dcb.hpp" +#include "prx/libc/include/GuestAllocations.hpp" +#include "prx/libc/include/GuestArena.hpp" +#include "SceTypes.hpp" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#ifdef _WIN32 +#include +#endif + +extern "C" int APS5_VABI sceAgcDriverSetTFRing(const volatile void* base, std::uint32_t size); + +namespace { + +using AgcDriver::FrameCall; +using AgcDriver::FrameCallKind; +using AgcDriver::FrameCaptureReader; +using AgcDriver::FrameMemoryChunk; +using AgcDriver::FrameMemoryKind; +using AgcDriver::FrameRangeAccess; +using AgcDriver::FrameRangeKind; +using Clock = std::chrono::steady_clock; + +struct Dump { + std::uint64_t address = 0; + std::uint64_t bytes = 0; + std::filesystem::path path; +}; + +struct Options { + std::filesystem::path directory; + std::vector dumps; + std::filesystem::path frames; + bool pace = true; + bool strict = false; + std::chrono::milliseconds paceTimeout{10000}; + std::chrono::milliseconds memoryPaceTimeout{10000}; + std::chrono::milliseconds releaseAfter{3}; + std::chrono::milliseconds drainTimeout{10000}; + bool release = true; + bool synthesize = true; +}; + +class Frames { +public: + void MarkReady() { + std::lock_guard lock(mutex); + ready.push_back(Clock::now()); + } + + void Failed(std::exception_ptr error) { + std::lock_guard lock(mutex); + if (!failure) failure = error; + } + + std::vector Times() const { + std::lock_guard lock(mutex); + return ready; + } + + std::exception_ptr Failure() const { + std::lock_guard lock(mutex); + return failure; + } + +private: + mutable std::mutex mutex; + std::vector ready; + std::exception_ptr failure; +}; + +class ReplayFlip final : public AgcDriver::IFlipRequest { +public: + explicit ReplayFlip(Frames& frames) : frames(frames) {} + void GpuReady(const std::shared_ptr&) override { frames.MarkReady(); } + void Fail(std::exception_ptr error) noexcept override { frames.Failed(error); } + +private: + Frames& frames; +}; + +class ReplayRenderingWait final : public AgcDriver::IRenderingWait { +public: + void Wait() override {} +}; + +class ReplayOutput final : public AgcDriver::IVideoOutput { +public: + explicit ReplayOutput(Frames& frames) : frames(frames) {} + std::shared_ptr Reserve(const AgcDriver::FlipInfo&) override { return std::make_shared(frames); } + std::shared_ptr CaptureRenderingWait(std::uint32_t) override { return std::make_shared(); } + void Fail(std::exception_ptr error) noexcept override { frames.Failed(error); } + +private: + Frames& frames; +}; + +std::uint64_t headerNumber(const std::string& header, std::string_view key) { + const auto quoted = "\"" + std::string(key) + "\":"; + const auto at = header.find(quoted); + if (at == std::string::npos) throw std::runtime_error("frame replay: the capture header has no " + std::string(key)); + return std::strtoull(header.c_str() + at + quoted.size(), nullptr, 10); +} + +void restore(const FrameCaptureReader& reader, const FrameMemoryChunk& chunk) { + reader.Read(chunk, std::span(reinterpret_cast(static_cast(chunk.address)), static_cast(chunk.bytes))); +} + +bool committedWritable(std::uint64_t address, std::uint64_t bytes) { +#ifdef _WIN32 + for (auto at = address; at < address + bytes;) { + MEMORY_BASIC_INFORMATION info{}; + if (VirtualQuery(reinterpret_cast(static_cast(at)), &info, sizeof(info)) == 0 || info.State != MEM_COMMIT) return false; + const auto access = info.Protect & 0xffu; + if ((info.Protect & PAGE_GUARD) != 0 || (access != PAGE_READWRITE && access != PAGE_EXECUTE_READWRITE)) return false; + at = reinterpret_cast(info.BaseAddress) + info.RegionSize; + } + return true; +#else + static_cast(address); + static_cast(bytes); + return false; +#endif +} + +std::pair progress(); + +struct Skipped { + std::vector> ranges; + std::uint64_t bytes = 0; + + void Add(const FrameMemoryChunk& chunk) { + ranges.emplace_back(chunk.address, chunk.address + chunk.bytes); + bytes += chunk.bytes; + } + + bool Overlaps(const FrameMemoryChunk& chunk) const { + return std::any_of(ranges.begin(), ranges.end(), [&](const auto& range) { return chunk.address < range.second && range.first < chunk.address + chunk.bytes; }); + } +}; + +void mapHostRanges(const FrameCaptureReader& reader, Skipped& skipped) { + std::vector host; + for (const auto& chunk : reader.Chunks()) { + if (chunk.kind == FrameMemoryKind::Range && chunk.rangeKind == FrameRangeKind::Host) host.push_back(&chunk); + } +#ifdef _WIN32 + constexpr std::uint64_t Granularity = 64u * 1024u; + std::vector> envelopes; + for (const auto* chunk : host) { + const auto begin = chunk->address & ~(Granularity - 1); + const auto end = (chunk->address + chunk->bytes + Granularity - 1) & ~(Granularity - 1); + if (!envelopes.empty() && begin <= envelopes.back().second) envelopes.back().second = std::max(envelopes.back().second, end); + else envelopes.emplace_back(begin, end); + } + std::vector> refused; + for (const auto& [begin, end] : envelopes) { + if (VirtualAlloc(reinterpret_cast(static_cast(begin)), static_cast(end - begin), MEM_RESERVE, PAGE_NOACCESS) == nullptr) refused.emplace_back(begin, end); + } + for (const auto* chunk : host) { + const bool blocked = std::any_of(refused.begin(), refused.end(), [&](const auto& range) { return chunk->address < range.second && range.first < chunk->address + chunk->bytes; }); + if (!blocked && VirtualAlloc(reinterpret_cast(static_cast(chunk->address)), static_cast(chunk->bytes), MEM_COMMIT, PAGE_READWRITE) != nullptr) continue; + if ((chunk->access & FrameRangeAccess::Gpu) != 0) throw std::runtime_error("frame replay: cannot map the GPU-visible host range at " + std::to_string(chunk->address)); + skipped.Add(*chunk); + } +#else + for (const auto* chunk : host) skipped.Add(*chunk); +#endif +} + +Skipped restoreMemory(const FrameCaptureReader& reader) { + std::uintptr_t base = 0; + std::size_t size = 0; + GuestArena::GuestArenaRange_nid_postfix(&base, &size); + if (!reader.Header().empty() && headerNumber(reader.Header(), "arena_base") != base) throw std::runtime_error("frame replay: the guest arena is at a different address than in the capture"); + progress(); + Skipped skipped; + mapHostRanges(reader, skipped); + { + GuestAllocations::Mutation mutation; + for (const auto& chunk : reader.Chunks()) { + if (chunk.kind != FrameMemoryKind::Range || chunk.rangeKind == FrameRangeKind::Host) continue; + const bool gpu = (chunk.access & FrameRangeAccess::Gpu) != 0; + if (committedWritable(chunk.address, chunk.bytes)) { + if (!gpu) skipped.Add(chunk); + else std::memset(reinterpret_cast(static_cast(chunk.address)), 0, static_cast(chunk.bytes)); + continue; + } + auto* const pointer = reinterpret_cast(static_cast(chunk.address)); + const auto bytes = static_cast(chunk.bytes); + try { + GuestArena::GuestArenaMarkUsed_nid_postfix(pointer, bytes); + } catch (const std::exception&) { + if (gpu) throw; + skipped.Add(chunk); + continue; + } + GuestArena::GuestArenaCommit_nid_postfix(pointer, bytes, 0x04, bytes); + mutation.Add(pointer, bytes, (chunk.access & FrameRangeAccess::Readable) != 0, (chunk.access & FrameRangeAccess::Writable) != 0, gpu); + } + } + std::vector snapshot; + for (const auto& chunk : reader.Chunks()) { + if (chunk.kind == FrameMemoryKind::Snapshot && !skipped.Overlaps(chunk)) snapshot.push_back(&chunk); + } + const auto workers = std::max(1, std::thread::hardware_concurrency()); + for (std::size_t first = 0; first < snapshot.size(); first += workers) { + std::vector> pending; + for (auto index = first; index < std::min(snapshot.size(), first + workers); ++index) { + pending.push_back(std::async(std::launch::async, [&reader, chunk = snapshot[index]] { restore(reader, *chunk); })); + } + for (auto& future : pending) future.get(); + } + return skipped; +} + +std::pair progress() { + std::uint64_t accepted = 0; + std::uint64_t completed = 0; + AgcDriverSerialProgress_nid_postfix(&accepted, &completed); + return {accepted, completed}; +} + +struct Replay { + const Options& options; + const FrameCaptureReader& reader; + Frames frames; + std::map> outputs; + std::mutex memoryMutex; + std::map> groups; + Skipped skipped; + std::atomic paceTimeouts{0}; + std::atomic staleRegistrations{0}; + std::atomic failedRegistrations{0}; + std::atomic submits{0}; + std::atomic deltaBytes{0}; + std::atomic paceWaitedNs{0}; + std::atomic stalled{false}; + std::atomic stalledAt{0}; + std::atomic earlyReleases{0}; + std::atomic lateReleases{0}; + std::atomic syntheticReleases{0}; + std::atomic sequenced{0}; + std::uint64_t unfinished = 0; + std::size_t threads = 0; + bool recording = false; + Clock::time_point startedAt{}; + Clock::time_point endedAt{}; + + double paceWaitedSeconds() const { + return static_cast(paceWaitedNs.load()) / 1e9; + } + + void report() const { + const auto [accepted, completed] = progress(); + std::fprintf(stderr, "[frame-replay] %llu submits, %zu frames ready, %.1f s since the first frame, pacing waited %.1f s (%llu timeouts), serials %llu/%llu\n", + static_cast(submits.load()), frames.Times().size(), std::chrono::duration(Clock::now() - startedAt).count(), paceWaitedSeconds(), + static_cast(paceTimeouts.load()), static_cast(completed), static_cast(accepted)); + } + + static bool paced(FrameCallKind kind) { + return kind == FrameCallKind::Submit || kind == FrameCallKind::Flip || kind == FrameCallKind::ReserveEopFlip || kind == FrameCallKind::Suspend || kind == FrameCallKind::Memory; + } + + void pace(const FrameCall& call) { + if (!options.pace || !recording || !paced(call.kind) || call.accepted < call.completed) return; + if (stalled.load()) { + if (progress().second == stalledAt.load()) return; + stalled.store(false); + } + const auto inFlight = call.accepted - call.completed; + const bool memory = call.kind == FrameCallKind::Memory; + const auto began = Clock::now(); + const auto deadline = began + (memory ? options.memoryPaceTimeout : options.paceTimeout); + const auto waited = [&] { paceWaitedNs += std::chrono::duration_cast(Clock::now() - began).count(); }; + while (true) { + const auto [accepted, completed] = progress(); + if (accepted - completed <= inFlight) return waited(); + if (Clock::now() >= deadline) { + waited(); + if (memory) return; + stalled.store(true); + stalledAt.store(completed); + std::fprintf(stderr, "[frame-replay] pacing gave up at call %llu (%s): serials %llu/%llu, the capture had %llu in flight\n", static_cast(call.ordinal), + std::string(AgcDriver::FrameCallName(call.kind)).c_str(), static_cast(completed), static_cast(accepted), static_cast(inFlight)); + ++paceTimeouts; + if (options.strict) throw std::runtime_error("frame replay: call " + std::to_string(call.ordinal) + " waited longer than the pacing timeout"); + return; + } + std::this_thread::sleep_for(std::chrono::microseconds(50)); + } + } + + void execute(const FrameCall& call) { + pace(call); + switch (call.kind) { + case FrameCallKind::RegisterShader: + registration([&] { AgcDriverRegisterShader_nid_postfix(shader(call.address)); }); + return; + case FrameCallKind::ResolveShaderAbi: + registration([&] { AgcDriverResolveShaderAbi_nid_postfix(shader(call.address), call.context, call.primitive); }); + return; + case FrameCallKind::ResolveGraphicsAbi: + registration([&] { AgcDriverResolveGraphicsAbi_nid_postfix(shader(call.values.at(0)), shader(call.values.at(1)), static_cast(call.values.at(2))); }); + return; + case FrameCallKind::ResolveGraphicsStagesAbi: { + std::vector stages; + for (const auto value : call.values) stages.push_back(shader(value)); + registration([&] { AgcDriverResolveGraphicsStagesAbi_nid_postfix(stages, call.context, call.primitive); }); + return; + } + case FrameCallKind::RegisterVideoOutput: { + const auto handle = static_cast(call.values.at(0)); + auto output = std::make_shared(frames); + AgcDriverRegisterVideoOutput_nid_postfix(handle, output); + outputs.emplace(handle, std::move(output)); + return; + } + case FrameCallKind::UnregisterVideoOutput: { + const auto handle = static_cast(call.values.at(0)); + AgcDriverUnregisterVideoOutput_nid_postfix(handle, outputs.at(handle)); + outputs.erase(handle); + return; + } + case FrameCallKind::SetTessellationFactorRing: + sceAgcDriverSetTFRing(reinterpret_cast(static_cast(call.address)), static_cast(call.size)); + return; + case FrameCallKind::Submit: { + const Packet packet{reinterpret_cast(static_cast(call.address)), static_cast(call.size), static_cast(call.flags), {}}; + if (call.queue == 0) sceAgcDriverSubmitDcb(&packet); + else sceAgcDriverSubmitAcb(call.queue, &packet); + if (++submits % 25 == 0) report(); + return; + } + case FrameCallKind::Flip: + AgcDriverSubmitFlip_nid_postfix(flipInfo(call)); + return; + case FrameCallKind::ReserveEopFlip: { + const auto token = static_cast(call.values.at(4)); + if (AgcDriverReserveEopFlipAt_nid_postfix(flipInfo(call), token) != token) throw std::runtime_error("frame replay: EOP flip token " + std::to_string(token) + " was not reserved"); + return; + } + case FrameCallKind::Suspend: + AgcDriverSuspendPoint_nid_postfix(); + return; + case FrameCallKind::Memory: { + std::lock_guard lock(memoryMutex); + const auto found = groups.find(call.values.at(0)); + if (found == groups.end()) return; + for (const auto* chunk : found->second) { + if (skipped.Overlaps(*chunk)) continue; + restore(reader, *chunk); + deltaBytes += chunk->bytes; + } + return; + } + case FrameCallKind::FrameStart: + case FrameCallKind::End: + case FrameCallKind::DisplayBuffer: + return; + } + throw std::runtime_error("frame replay: unknown call kind"); + } + + static std::uint64_t load(std::uint64_t address, std::size_t bytes) { + std::uint64_t value = 0; + std::memcpy(&value, reinterpret_cast(static_cast(address)), bytes); + return value; + } + + static bool satisfies(const AgcDriver::AwaitedWait& wait, std::uint64_t value) { + const auto masked = value & wait.mask; + const auto reference = wait.reference & wait.mask; + switch (wait.function) { + case 1: return masked < reference; + case 2: return masked <= reference; + case 3: return masked == reference; + case 4: return masked != reference; + case 5: return masked >= reference; + case 6: return masked > reference; + default: return false; + } + } + + static std::optional satisfying(const AgcDriver::AwaitedWait& wait, std::uint64_t current) { + const auto keep = current & ~wait.mask; + const auto reference = wait.reference & wait.mask; + switch (wait.function) { + case 2: + case 3: + case 5: return keep | reference; + case 1: return reference == 0 ? std::nullopt : std::optional(keep); + case 4: return keep | (~reference & wait.mask); + case 6: return reference == wait.mask ? std::nullopt : std::optional(keep | ((reference + 1) & wait.mask)); + default: return std::nullopt; + } + } + + bool writeRecordedValue(const FrameCall& call, const AgcDriver::AwaitedWait& wait) { + if (call.kind != FrameCallKind::Memory) return false; + const auto found = groups.find(call.values.at(0)); + if (found == groups.end()) return false; + const auto address = wait.address; + for (const auto* chunk : found->second) { + if (chunk->address > address || address + 4 > chunk->address + chunk->bytes || skipped.Overlaps(*chunk)) continue; + const auto bytes = reader.Read(*chunk); + const auto length = static_cast(std::min(wait.bytes == 8 ? 8 : 4, chunk->address + chunk->bytes - address)); + const auto* const recorded = bytes.data() + (address - chunk->address); + auto* const target = reinterpret_cast(static_cast(address)); + if (std::memcmp(target, recorded, length) == 0) return false; + std::uint64_t value = 0; + std::memcpy(&value, recorded, length); + if (wait.function != 0 && !satisfies(wait, value)) return false; + std::memcpy(target, recorded, length); + return true; + } + return false; + } + + void releaseAwaited(std::span calls, std::size_t from) { + std::lock_guard lock(memoryMutex); + std::array awaited{}; + const auto count = AgcDriverAwaitedWaits_nid_postfix(awaited.data(), awaited.size()); + for (std::size_t slot = 0; slot < count; ++slot) { + const auto& wait = awaited[slot]; + if (wait.address == 0) continue; + const std::size_t bytes = wait.bytes == 8 ? 8 : 4; + if (wait.function != 0 && satisfies(wait, load(wait.address, bytes))) continue; + bool released = false; + for (auto index = from; index < calls.size() && !released; ++index) released = writeRecordedValue(calls[index], wait); + for (auto index = std::min(from, calls.size()); index > 0 && !released; --index) { + released = writeRecordedValue(calls[index - 1], wait); + if (released) ++lateReleases; + } + if (!released && options.synthesize) { + if (const auto value = satisfying(wait, load(wait.address, bytes))) { + std::memcpy(reinterpret_cast(static_cast(wait.address)), &*value, bytes); + ++syntheticReleases; + released = true; + } + } + if (released) ++earlyReleases; + } + } + + void runWindow(std::span calls) { + std::map> byThread; + for (std::size_t index = 0; index < calls.size(); ++index) byThread[calls[index].thread].push_back(index); + threads = byThread.size(); + std::mutex mutex; + std::condition_variable turn; + std::size_t started = 0; + std::exception_ptr failure; + std::atomic finished{false}; + std::thread breaker([&] { + if (!options.release) return; + auto lastCompleted = progress().second; + auto lastProgress = Clock::now(); + while (!finished.load()) { + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + const auto completed = progress().second; + if (completed != lastCompleted) { + lastCompleted = completed; + lastProgress = Clock::now(); + continue; + } + if (Clock::now() - lastProgress < options.releaseAfter) continue; + releaseAwaited(calls, sequenced.load()); + lastProgress = Clock::now(); + } + }); + std::vector workers; + for (const auto& entry : byThread) { + workers.emplace_back([&, &indices = entry.second] { + for (const auto index : indices) { + std::unique_lock lock(mutex); + turn.wait(lock, [&] { return started == index || failure != nullptr; }); + if (failure != nullptr) return; + const auto advance = [&] { + ++started; + sequenced.store(started); + lock.unlock(); + turn.notify_all(); + }; + const auto kind = calls[index].kind; + const bool ordered = kind == FrameCallKind::Memory || kind == FrameCallKind::RegisterVideoOutput || kind == FrameCallKind::UnregisterVideoOutput; + if (!ordered) advance(); + try { + execute(calls[index]); + if (ordered) advance(); + } catch (...) { + if (!lock.owns_lock()) lock.lock(); + if (failure == nullptr) failure = std::current_exception(); + turn.notify_all(); + return; + } + } + }); + } + for (auto& worker : workers) worker.join(); + if (failure == nullptr) drain(); + finished.store(true); + breaker.join(); + if (failure != nullptr) std::rethrow_exception(failure); + } + + void drain() { + auto lastCompleted = progress().second; + auto lastProgress = Clock::now(); + while (true) { + const auto [accepted, completed] = progress(); + unfinished = accepted - completed; + if (unfinished == 0) return; + if (completed != lastCompleted) { + lastCompleted = completed; + lastProgress = Clock::now(); + } else if (Clock::now() - lastProgress >= options.drainTimeout) { + std::fprintf(stderr, "[frame-replay] %llu submissions did not complete after the last captured call (serials %llu/%llu)\n", static_cast(unfinished), + static_cast(completed), static_cast(accepted)); + return; + } + std::this_thread::sleep_for(std::chrono::milliseconds(5)); + } + } + + template + void registration(TFunction&& function) { + try { + function(); + } catch (const std::exception& error) { + if (recording && options.strict) throw; + ++(recording ? failedRegistrations : staleRegistrations); + std::fprintf(stderr, "[frame-replay] %s registration failed: %s\n", recording ? "recorded" : "prologue", error.what()); + } + } + + static const Shader* shader(std::uint64_t address) { + return reinterpret_cast(static_cast(address)); + } + + static AgcDriver::FlipInfo flipInfo(const FrameCall& call) { + return {static_cast(call.values.at(0)), static_cast(call.values.at(1)), static_cast(call.values.at(2)), static_cast(call.values.at(3))}; + } +}; + +int usage() { + std::fprintf(stderr, "usage: agc_frame_replay [--no-pace] [--no-release] [--release-after-ms ] [--strict]\n" + " [--dump
::]...\n" + " replays the driver calls and guest memory that APS5_FRAME_CAPTURE= recorded, through this\n" + " build's AGC driver with no title running, and prints one JSON line with the frame times\n" + " --no-pace issues calls without waiting for the in-flight depth the title had\n" + " --no-release never copies recorded CPU stores early to release a GPU wait\n" + " --release-after-ms milliseconds without a completion before awaited dwords are released (3)\n" + " --no-synthesize releases a wait only with a value the capture recorded, never with its reference value\n" + " --strict fails on a pacing timeout or a failed registration inside the captured frames\n" + " --dump writes guest memory to a file after the replay (hexadecimal or decimal)\n" + " --frames writes each recorded and replayed display buffer as reference-.bmp and replay-.bmp\n"); + return 2; +} + +Dump parseDump(std::string_view text) { + const auto first = text.find(':'); + const auto second = first == std::string_view::npos ? std::string_view::npos : text.find(':', first + 1); + if (second == std::string_view::npos) throw std::invalid_argument("frame replay: --dump takes
::"); + const auto number = [](std::string_view part) { + const std::string value(part); + char* end = nullptr; + const auto parsed = std::strtoull(value.c_str(), &end, 0); + if (value.empty() || end != value.c_str() + value.size()) throw std::invalid_argument("frame replay: '" + value + "' is not a number"); + return static_cast(parsed); + }; + return {number(text.substr(0, first)), number(text.substr(first + 1, second - first - 1)), std::filesystem::path(std::string(text.substr(second + 1)))}; +} + +void writeDump(const Dump& dump) { + auto* const pointer = reinterpret_cast(static_cast(dump.address)); + GuestArena::GuestArenaPrepareHostRead_nid_no_patch(pointer, static_cast(dump.bytes)); + std::ofstream file(dump.path, std::ios::binary | std::ios::trunc); + file.write(reinterpret_cast(pointer), static_cast(dump.bytes)); + if (!file.good()) throw std::runtime_error("frame replay: cannot write " + dump.path.string()); +} + +struct FrameCheck { + std::size_t compared = 0; + double identical = 1.0; + int maxDifference = 0; +}; + +void writeFrame(const std::filesystem::path& path, const AgcDriver::DisplayBuffer& buffer, std::span pixels) { + std::FILE* file = std::fopen(path.string().c_str(), "wb"); + const auto failure = file != nullptr ? AgcDriver::WriteBmpAndClose_nid_no_patch(file, buffer.width, buffer.height, pixels) : AgcDriver::FrameDumpIoFailure{"open", errno}; + if (failure) std::fprintf(stderr, "[frame-replay] cannot write %s (%s failed)\n", path.string().c_str(), failure.operation); +} + +FrameCheck compareFrames(const FrameCaptureReader& reader, const Options& options) { + FrameCheck check; + if (!options.frames.empty()) std::filesystem::create_directories(options.frames); + for (const auto& call : reader.Calls()) { + if (call.kind != FrameCallKind::DisplayBuffer || call.values.size() < 9) continue; + AgcDriver::DisplayBuffer buffer{}; + buffer.address = call.address; + buffer.pixelFormat = call.values[1]; + buffer.width = static_cast(call.values[2]); + buffer.height = static_cast(call.values[3]); + buffer.tilingMode = static_cast(call.values[4]); + buffer.pitchInPixel = static_cast(call.values[5]); + buffer.dccAddress = call.values[6]; + buffer.dccClearColor = call.values[7]; + buffer.alignment = static_cast(call.values[8]); + const auto chunk = std::find_if(reader.Chunks().begin(), reader.Chunks().end(), [&](const FrameMemoryChunk& candidate) { return candidate.kind == FrameMemoryKind::Reference && candidate.group == call.values[0]; }); + if (chunk == reader.Chunks().end()) continue; + const auto reference = reader.Read(*chunk); + const auto replayed = AgcDriver::ReadDisplayBuffer(buffer); + ++check.compared; + std::size_t same = 0; + const auto pixels = std::min(reference.size(), replayed.size()) / 4; + for (std::size_t pixel = 0; pixel < pixels; ++pixel) { + bool equal = true; + for (std::size_t channel = 0; channel < 4; ++channel) { + const auto difference = std::abs(static_cast(reference[pixel * 4 + channel]) - static_cast(replayed[pixel * 4 + channel])); + check.maxDifference = std::max(check.maxDifference, difference); + equal = equal && difference == 0; + } + same += equal ? 1 : 0; + } + const auto total = std::max(reference.size(), replayed.size()) / 4; + check.identical = std::min(check.identical, total == 0 ? 1.0 : static_cast(same) / static_cast(total)); + if (options.frames.empty()) continue; + const auto index = std::to_string(call.values[0]); + writeFrame(options.frames / ("reference-" + index + ".bmp"), buffer, reference); + writeFrame(options.frames / ("replay-" + index + ".bmp"), buffer, replayed); + } + return check; +} + +int run(const Options& options) { + const auto loadStart = Clock::now(); + const FrameCaptureReader reader(options.directory); + if (reader.Truncated()) std::fprintf(stderr, "[frame-replay] the capture is truncated; replaying the complete records\n"); + auto skipped = restoreMemory(reader); + const auto loaded = Clock::now(); + Replay replay{options, reader}; + replay.skipped = std::move(skipped); + for (const auto& chunk : reader.Chunks()) { + if (chunk.kind == FrameMemoryKind::Delta) replay.groups[chunk.group].push_back(&chunk); + } + const auto& calls = reader.Calls(); + const auto frameStart = std::find_if(calls.begin(), calls.end(), [](const FrameCall& call) { return call.kind == FrameCallKind::FrameStart; }); + for (auto call = calls.begin(); call != frameStart; ++call) replay.execute(*call); + replay.recording = true; + replay.startedAt = Clock::now(); + if (frameStart != calls.end()) replay.runWindow(std::span(calls).subspan(static_cast(frameStart - calls.begin()) + 1)); + if (frameStart == calls.end()) AgcDriverWaitIdle_nid_postfix(); + replay.endedAt = Clock::now(); + if (const auto failure = replay.frames.Failure()) std::rethrow_exception(failure); + FrameCheck check; + if (replay.unfinished == 0) { + for (const auto& dump : options.dumps) writeDump(dump); + check = compareFrames(reader, options); + } else if (!options.dumps.empty()) { + std::fprintf(stderr, "[frame-replay] no dumps written: the GPU work did not finish\n"); + } + const auto ready = replay.frames.Times(); + std::vector frameMs; + for (std::size_t index = 1; index < ready.size(); ++index) frameMs.push_back(std::chrono::duration(ready[index] - ready[index - 1]).count()); + std::string frameList; + for (const auto value : frameMs) frameList += (frameList.empty() ? "" : ",") + std::to_string(static_cast(value)); + std::sort(frameMs.begin(), frameMs.end()); + const auto percentile = [&](double fraction) { return frameMs.empty() ? 0.0 : frameMs[std::min(frameMs.size() - 1, static_cast(fraction * static_cast(frameMs.size())))]; }; + const auto seconds = std::chrono::duration(replay.endedAt - replay.startedAt).count(); + std::printf("{\"frames\":%zu,\"replay_s\":%.3f,\"fps\":%.3f,\"frame_ms\":{\"p50\":%.3f,\"p90\":%.3f,\"max\":%.3f,\"each\":[%s]},\"submits\":%llu,\"delta_mib\":%.1f,\"pace_timeouts\":%llu,\"pace_wait_s\":%.3f,\"stale_registrations\":%llu,\"failed_registrations\":%llu,\"skipped_mib\":%.1f,\"threads\":%zu,\"released_waits\":%llu,\"of_them_from_earlier_writes\":%llu,\"of_them_synthesized\":%llu,\"unfinished\":%llu,\"frames_compared\":%zu,\"pixels_identical\":%.6f,\"max_channel_diff\":%d,\"load_s\":%.3f}\n", + ready.size(), seconds, seconds > 0 ? static_cast(ready.size()) / seconds : 0.0, percentile(0.5), percentile(0.9), frameMs.empty() ? 0.0 : frameMs.back(), frameList.c_str(), + static_cast(replay.submits.load()), static_cast(replay.deltaBytes.load()) / (1024.0 * 1024.0), static_cast(replay.paceTimeouts.load()), + replay.paceWaitedSeconds(), static_cast(replay.staleRegistrations.load()), static_cast(replay.failedRegistrations.load()), + static_cast(replay.skipped.bytes) / (1024.0 * 1024.0), replay.threads, static_cast(replay.earlyReleases.load()), static_cast(replay.lateReleases.load()), + static_cast(replay.syntheticReleases.load()), + static_cast(replay.unfinished), check.compared, check.identical, check.maxDifference, std::chrono::duration(loaded - loadStart).count()); + std::fflush(stdout); + return replay.unfinished == 0 ? 0 : 3; +} + +} + +int main(int argc, char** argv) { + Options options; + for (int index = 1; index < argc; ++index) { + const std::string_view argument = argv[index]; + if (argument == "--no-pace") options.pace = false; + else if (argument == "--no-release") options.release = false; + else if (argument == "--no-synthesize") options.synthesize = false; + else if (argument == "--release-after-ms" && index + 1 < argc) options.releaseAfter = std::chrono::milliseconds(std::strtoll(argv[++index], nullptr, 10)); + else if (argument == "--strict") options.strict = true; + else if (argument == "--dump" && index + 1 < argc) options.dumps.push_back(parseDump(argv[++index])); + else if (argument == "--frames" && index + 1 < argc) options.frames = argv[++index]; + else if (!argument.starts_with("-") && options.directory.empty()) options.directory = argument; + else return usage(); + } + if (options.directory.empty()) return usage(); +#ifdef _WIN32 + _putenv_s("APS5_FRAME_CAPTURE", ""); +#else + unsetenv("APS5_FRAME_CAPTURE"); +#endif + int code = 2; + try { + code = run(options); + if (code == 0) { + AgcDriverShutdown_nid_postfix(); + return 0; + } + } catch (const std::exception& error) { + std::fprintf(stderr, "agc_frame_replay: %s\n", error.what()); + } + std::fflush(stdout); + std::fflush(stderr); + std::_Exit(code); +} diff --git a/core/libs/prx/libSceAppInstUtil/Export.cpp b/core/libs/prx/libSceAppInstUtil/Export.cpp index 5925f41ddd..87f1cb3954 100644 --- a/core/libs/prx/libSceAppInstUtil/Export.cpp +++ b/core/libs/prx/libSceAppInstUtil/Export.cpp @@ -84,4 +84,494 @@ int APS5_VABI sceAppInstUtilGetPrimaryAppSlot(const char* titleId, void* slot) { return 0; } +int APS5_VABI sceAppInstUtilAppGetAppInstallStatusByAppInfo(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilAppUnInstall2(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilAppUnInstallForRemote(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilAppUnInstallForRemote2(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilAppGetAddcontListTotalSize(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilAppGetAddcontInfo(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilAppGetAddcontInfoByContentId(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilGetAddcontInstalledStatus(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilAppGetInsertedDiscTitleId(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilAppUpdateStartTime(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilAppInstallSharePlayLink(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilAppInstallSharePlayLinkByUser(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilAppGetAddcontNum(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilAppCancelUnInstall(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilAppDestroyMetadata(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilCheckAppSystemVer(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilAppConvertAppDownloadToDisc(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilGetMainAppTitleIdForSP(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilGetSubAppTitleIdForSP(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilGetMainAppTitleId(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilGetSubAppTitleId(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilAppGetAppInstallStatus(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilAppDestroyPkg(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilJson(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilGetInsertedDiscRoleType(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilRequestInstallDiscGame(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilRequestDataDiscCopy(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilCancelDataDiscCopy(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilGetDataDiscCopyProgress(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilSetPrimaryAppSlot(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilGetAppSlotInfoList(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilGetAppEmptySlot(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilAppInstallGetLinkState(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilAppUnInstallForMultiInstall(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilAppInstallPs4CloudGame(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilAppStoreCompilationDiscInfo(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilAppGetCompilationDiscInfo(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilAppGetStoreCompilationDiscInfo(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilAppGetInsertedDiscTitleIdList(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilAppInstallRequestCompilationDiscApp(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilAppGetNoRelatedAddcontTitleList(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilAppGetIrremovableAppList(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilAppRequestMoveApps(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilAppProhibitDownloadInstall(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilAppAllowDownloadInstall(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilGetAppMoveProgressInfo(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilAppCancelMoveApps(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilAppGetMoveErrorAppList(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilAppGetAllAppSize(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilAppCancelGetAllAppSize(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilAppGetInsertedDiscMasterDataId(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilAppGetAppOtherSize(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilAppCancelGetAppOtherSize(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilGetInsertedDiscTotal(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilAppSetRemasterInstallType(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilGetInstallProgressInfoForMultiDisc(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilMountBdEmu(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilUnmountBdEmu(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilGetInstallStatus(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilResumeInstall(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilPauseInstall(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilRegisterDownload(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilInstall(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilStartInstall(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilInstallPatch(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilGetPatchInstallStatus(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilResumePatchInstall(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilPausePatchInstall(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilCancelInstallPatch(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilCancelInstallAddcont(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilCancelInstall(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilCancelForMultiInstall(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilInstallByPackage(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilIsRestoreState(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilGetRestoreState(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilGetAddons(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilGetInstallOptions(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilGetContentSize(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilCancelGetInformation(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilGetWorkspaceStatus(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilDeleteWorkspace(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilGetParentalControlLevel(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilCheckLatestPatch(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilCancelCheckPatch(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilAppInstallPsNowIcon(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilGetAddcontInfoList2(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilGetAddcontInfoList3(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilClearAddcontInfoCache(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilGetContentSize2(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilAppInstallContentPush(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilGetAvailableStorage(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilSwitchInstallLocation(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilGetConfigList(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilSwitchConfig(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilRestoreDataSource(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilGetNeededStorageSize(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilAppGetStorageDestType(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAppInstUtilInstallAddons(void) { + NotImplemented_nid_no_patch(__func__); + return 0; +} + } diff --git a/core/libs/prx/libSceAudioOut/src/AudioOut.cpp b/core/libs/prx/libSceAudioOut/src/AudioOut.cpp index e973018f35..c5051500a0 100644 --- a/core/libs/prx/libSceAudioOut/src/AudioOut.cpp +++ b/core/libs/prx/libSceAudioOut/src/AudioOut.cpp @@ -401,12 +401,23 @@ static int allocatePort(int type, std::uint32_t len, std::uint32_t freq, Format return -2144993275; } +static std::atomic g_audioOut2Initialized{false}; + +bool AudioOut2IsInitialized_nid_no_patch() { + return g_audioOut2Initialized.load(); +} + extern "C" { -int APS5_VABI sceAudioOutInit() { +int APS5_VABI sceAudioOut2Initialize(void) { + g_audioOut2Initialized.store(true); return 0; } +int APS5_VABI sceAudioOutInit() { + return sceAudioOut2Initialize(); +} + int APS5_VABI sceAudioOutOpen(int userId, int type, int index, std::uint32_t len, std::uint32_t freq, std::uint32_t param) { (void)userId; diff --git a/core/libs/prx/libSceAudioOut/src/AudioOut2Internal.hpp b/core/libs/prx/libSceAudioOut/src/AudioOut2Internal.hpp index c58bd4c542..42363a9998 100644 --- a/core/libs/prx/libSceAudioOut/src/AudioOut2Internal.hpp +++ b/core/libs/prx/libSceAudioOut/src/AudioOut2Internal.hpp @@ -25,6 +25,7 @@ bool AudioOut2TraceEnabled(); // Seconds since the first AudioOut2 call, for the trace. double AudioOut2TraceSeconds(); +bool AudioOut2IsInitialized_nid_no_patch(); #define AUDIOOUT2_TRACE(...) \ do { \ diff --git a/core/libs/prx/libSceAudioOut/src/AudioOut2Mastering.cpp b/core/libs/prx/libSceAudioOut/src/AudioOut2Mastering.cpp index 8f7e6403dc..2a5d1be319 100644 --- a/core/libs/prx/libSceAudioOut/src/AudioOut2Mastering.cpp +++ b/core/libs/prx/libSceAudioOut/src/AudioOut2Mastering.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -29,10 +30,54 @@ constexpr std::uint32_t AUDIO_OUT2_MASTERING_STRUCT_ID_COMPRESSOR_DEFAULT = 0x01 constexpr std::uint32_t AUDIO_OUT2_MASTERING_STRUCT_ID_COMPRESSOR_V2 = 0x01020001; constexpr std::uint32_t AUDIO_OUT2_MASTERING_STRUCT_ID_LIMITER_DEFAULT = 0x01010003; +constexpr std::uint32_t AUDIO_OUT_MASTERING_PARAMS_ID_DEFAULT = 1; +constexpr std::uint32_t AUDIO_OUT_MASTERING_STATES_ID_DEFAULT = 1; + +constexpr std::size_t AUDIO_OUT_MASTERING_COMPRESSOR_INPUT_RMS_OFFSET = 32; +constexpr std::size_t AUDIO_OUT_MASTERING_COMPRESSOR_GAIN_OFFSET = 128; +constexpr std::size_t AUDIO_OUT_MASTERING_LIMITER_INPUT_PEAK_OFFSET = 240; +constexpr std::size_t AUDIO_OUT_MASTERING_LIMITER_OUTPUT_PEAK_OFFSET = 272; +constexpr std::size_t AUDIO_OUT_MASTERING_LIMITER_GAIN_PEAK_OFFSET = 304; + +static_assert(sizeof(AudioOut2MasteringStates) == 336); +static_assert(offsetof(AudioOut2MasteringStates, compressor_states) + offsetof(AudioOut2MasteringCompressorStates, input_rms) == AUDIO_OUT_MASTERING_COMPRESSOR_INPUT_RMS_OFFSET); +static_assert(offsetof(AudioOut2MasteringStates, compressor_states) + offsetof(AudioOut2MasteringCompressorStates, compression_coeff) == AUDIO_OUT_MASTERING_COMPRESSOR_GAIN_OFFSET); +static_assert(offsetof(AudioOut2MasteringStates, limiter_states) + offsetof(AudioOut2MasteringLimiterStates, input_peak) == AUDIO_OUT_MASTERING_LIMITER_INPUT_PEAK_OFFSET); +static_assert(offsetof(AudioOut2MasteringStates, limiter_states) + offsetof(AudioOut2MasteringLimiterStates, output_peak) == AUDIO_OUT_MASTERING_LIMITER_OUTPUT_PEAK_OFFSET); +static_assert(offsetof(AudioOut2MasteringStates, limiter_states) + offsetof(AudioOut2MasteringLimiterStates, gain_peak) == AUDIO_OUT_MASTERING_LIMITER_GAIN_PEAK_OFFSET); + } extern "C" { +int APS5_VABI sceAudioOutMasteringInit(uint32_t flags) { + if (flags != 0) NotImplemented_nid_no_patch(__func__); + return 0; +} + +int APS5_VABI sceAudioOutMasteringTerm(void) { + return 0; +} + +int APS5_VABI sceAudioOutMasteringGetState(AudioOut2MasteringStatesHeader* state) { + if (state == nullptr) APS5_INVALID_ARG_EX; + if (state->states_id != AUDIO_OUT_MASTERING_STATES_ID_DEFAULT) NotImplemented_nid_no_patch(__func__); + auto* full = static_cast(static_cast(state)); + std::memset(full->compressor_states.input_rms, 0, sizeof(full->compressor_states.input_rms)); + std::memset(full->compressor_states.compression_coeff, 0, sizeof(full->compressor_states.compression_coeff)); + std::memset(full->limiter_states.input_peak, 0, sizeof(full->limiter_states.input_peak)); + std::memset(full->limiter_states.output_peak, 0, sizeof(full->limiter_states.output_peak)); + std::memset(full->limiter_states.gain_peak, 0, sizeof(full->limiter_states.gain_peak)); + return 0; +} + +int APS5_VABI sceAudioOutMasteringSetParam(const AudioOut2MasteringParamsHeader* param, uint32_t flags) { + if (param == nullptr) APS5_INVALID_ARG_EX; + if (flags != 0) NotImplemented_nid_no_patch(__func__); + if (param->params_id != AUDIO_OUT_MASTERING_PARAMS_ID_DEFAULT) NotImplemented_nid_no_patch(__func__); + return 0; +} + int APS5_VABI sceAudioOut2MasteringInit(uint32_t flags) { return InitializeMastering(flags, 0); } diff --git a/core/libs/prx/libSceAudioOut/src/AudioOut2SpeakerArray.cpp b/core/libs/prx/libSceAudioOut/src/AudioOut2SpeakerArray.cpp index 7b6a07017e..cee71ecc16 100644 --- a/core/libs/prx/libSceAudioOut/src/AudioOut2SpeakerArray.cpp +++ b/core/libs/prx/libSceAudioOut/src/AudioOut2SpeakerArray.cpp @@ -7,6 +7,7 @@ #include #include "SceTypes.hpp" #include "prx/libc/include/General.hpp" +#include "AudioOut2Internal.hpp" namespace { @@ -120,8 +121,13 @@ size_t APS5_VABI sceAudioOut2GetSpeakerArrayMemorySize(uint32_t num_speakers, ui } int APS5_VABI sceAudioOut2GetSpeakerInfo(AudioOut2SpeakerInfo* info, uint32_t flags) { - (void)flags; - if (!info) return static_cast(0x80260502); + constexpr int ErrorInvalidParam = static_cast(0x80268001); + constexpr int ErrorNotInitialized = static_cast(0x80268006); + constexpr int ErrorInvalidPointer = static_cast(0x8026800C); + constexpr std::uint32_t MaxFlags = 1; + if (!AudioOut2IsInitialized_nid_no_patch()) return ErrorNotInitialized; + if (!info) return ErrorInvalidPointer; + if (flags > MaxFlags) return ErrorInvalidParam; constexpr std::uint8_t SpeakerTypeTelevision = 0; constexpr std::uint32_t FrontLeftAndRight = 0x3; constexpr AudioOut2SpeakerAngle NoSpeaker = {-360, -360}; diff --git a/core/libs/prx/libSceAudioOut/src/AudioOut2System.cpp b/core/libs/prx/libSceAudioOut/src/AudioOut2System.cpp index 89c681ad9c..0be9bc4cf3 100644 --- a/core/libs/prx/libSceAudioOut/src/AudioOut2System.cpp +++ b/core/libs/prx/libSceAudioOut/src/AudioOut2System.cpp @@ -12,9 +12,7 @@ static std::atomic g_chatEnabled{false}; extern "C" { -int APS5_VABI sceAudioOut2Initialize(void) { - return 0; -} +int APS5_VABI sceAudioOut2Initialize(void); int APS5_VABI sceAudioOut2InitializeEx(std::uint32_t param) { (void)param; diff --git a/core/libs/prx/libSceAudioOut/src/Unimplemented.cpp b/core/libs/prx/libSceAudioOut/src/Unimplemented.cpp index 0f7cdd8540..4afa07f903 100644 --- a/core/libs/prx/libSceAudioOut/src/Unimplemented.cpp +++ b/core/libs/prx/libSceAudioOut/src/Unimplemented.cpp @@ -97,26 +97,6 @@ int APS5_VABI sceAudioOutGetInfo(void) { return 0; } -int APS5_VABI sceAudioOutMasteringInit(void) { - NotImplemented_nid_no_patch(__func__); - return 0; -} - -int APS5_VABI sceAudioOutMasteringSetParam(void) { - NotImplemented_nid_no_patch(__func__); - return 0; -} - -int APS5_VABI sceAudioOutMasteringGetState(void) { - NotImplemented_nid_no_patch(__func__); - return 0; -} - -int APS5_VABI sceAudioOutMasteringTerm(void) { - NotImplemented_nid_no_patch(__func__); - return 0; -} - int APS5_VABI sceAudioOutSetPadSpkVolume(void) { NotImplemented_nid_no_patch(__func__); return 0; diff --git a/core/libs/prx/libSceAudioOut/tests/AudioOut2DeviceState.cpp b/core/libs/prx/libSceAudioOut/tests/AudioOut2DeviceState.cpp index 404037d497..5ada5b5529 100644 --- a/core/libs/prx/libSceAudioOut/tests/AudioOut2DeviceState.cpp +++ b/core/libs/prx/libSceAudioOut/tests/AudioOut2DeviceState.cpp @@ -9,6 +9,8 @@ #include extern "C" { +int APS5_VABI sceAudioOutInit(); +int APS5_VABI sceAudioOut2Initialize(); int APS5_VABI sceAudioOut2ContextResetParam(AudioOut2ContextParam*); int APS5_VABI sceAudioOut2ContextCreate(const AudioOut2ContextParam*, void*, std::size_t, AudioOut2ContextHandle*); int APS5_VABI sceAudioOut2ContextDestroy(AudioOut2ContextHandle); @@ -25,6 +27,9 @@ namespace { constexpr int errorInvalidArgument = static_cast(0x80260502); constexpr int errorInvalidHandle = static_cast(0x80260503); +constexpr int errorInvalidParam = static_cast(0x80268001); +constexpr int errorNotInitialized = static_cast(0x80268006); +constexpr int errorInvalidPointer = static_cast(0x8026800C); constexpr std::uint16_t portTypeMain = 0; constexpr std::uint16_t outputMain = 1; constexpr std::int16_t volumeMax = 127; @@ -101,7 +106,38 @@ void SpeakerInfoReportsTheFrontPair() { Require(info.speaker_angle[0].azimuth == 30 && info.speaker_angle[0].elevation == 0); Require(info.speaker_angle[1].azimuth == -30 && info.speaker_angle[1].elevation == 0); for (int index = 2; index < 16; index++) Require(info.speaker_angle[index].azimuth == -360 && info.speaker_angle[index].elevation == -360); - Require(sceAudioOut2GetSpeakerInfo(nullptr, 0) == errorInvalidArgument); + Require(sceAudioOut2GetSpeakerInfo(nullptr, 0) == errorInvalidPointer); +} + +void SpeakerInfoRequiresInitialization() { + AudioOut2SpeakerInfo info; + std::memset(&info, 0xAB, sizeof(info)); + const AudioOut2SpeakerInfo untouched = info; + Require(sceAudioOut2GetSpeakerInfo(&info, 0) == errorNotInitialized); + Require(sceAudioOut2GetSpeakerInfo(nullptr, 0) == errorNotInitialized); + Require(sceAudioOut2GetSpeakerInfo(&info, 2) == errorNotInitialized); + Require(std::memcmp(&info, &untouched, sizeof(info)) == 0); +} + +void SpeakerInfoAfterAudioOutInit() { + AudioOut2SpeakerInfo info; + std::memset(&info, 0xAB, sizeof(info)); + Require(sceAudioOutInit() == 0); + Require(sceAudioOut2GetSpeakerInfo(&info, 0) == 0); +} + +void SpeakerInfoRejectsBadFlags() { + AudioOut2SpeakerInfo info; + std::memset(&info, 0xAB, sizeof(info)); + const AudioOut2SpeakerInfo untouched = info; + Require(sceAudioOut2GetSpeakerInfo(&info, 2) == errorInvalidParam); + Require(sceAudioOut2GetSpeakerInfo(&info, 0x80000000) == errorInvalidParam); + Require(sceAudioOut2GetSpeakerInfo(nullptr, 2) == errorInvalidPointer); + Require(std::memcmp(&info, &untouched, sizeof(info)) == 0); + Require(sceAudioOut2GetSpeakerInfo(&info, 1) == 0); + Require(info.type == 0); + Require(info.available_bits == 0x3); + Require(info.speaker_angle[0].azimuth == 30 && info.speaker_angle[1].azimuth == -30); } void SystemStateIsSilentAndRejectsNull() { @@ -119,6 +155,9 @@ int main() { std::filesystem::remove(path); SetEnvironment("SDL_DISKAUDIOFILE", path.string()); + SpeakerInfoRequiresInitialization(); + SpeakerInfoAfterAudioOutInit(); + AudioOut2ContextParam params{}; Require(sceAudioOut2ContextResetParam(¶ms) == 0); params.num_grains = 256; @@ -129,6 +168,7 @@ int main() { PortStateFollowsTheDataFormat(context); PortStateRejectsBadArguments(context); SpeakerInfoReportsTheFrontPair(); + SpeakerInfoRejectsBadFlags(); SystemStateIsSilentAndRejectsNull(); Require(sceAudioOut2ContextDestroy(context) == 0); diff --git a/core/libs/prx/libSceAudioOut/tests/AudioOut2Latency.cpp b/core/libs/prx/libSceAudioOut/tests/AudioOut2Latency.cpp index dd018acae0..df12a6d6a9 100644 --- a/core/libs/prx/libSceAudioOut/tests/AudioOut2Latency.cpp +++ b/core/libs/prx/libSceAudioOut/tests/AudioOut2Latency.cpp @@ -2,10 +2,12 @@ #include "prx/libc/include/general/VabiMacros.hpp" #include +#include #include #include #include #include +#include extern "C" { int APS5_VABI sceAudioOut2Initialize(); @@ -19,6 +21,10 @@ int APS5_VABI sceAudioOut2MasteringTerm(); int APS5_VABI sceAudioOut2MasteringSetParam(const void*, std::uint32_t, std::uint32_t); int APS5_VABI sceAudioOut2MasteringGetState(void*, std::uint32_t, std::uint64_t); int APS5_VABI sceAudioOut2SetSystemDebugState(const void*); +int APS5_VABI sceAudioOutMasteringInit(std::uint32_t); +int APS5_VABI sceAudioOutMasteringTerm(); +int APS5_VABI sceAudioOutMasteringSetParam(const void*, std::uint32_t); +int APS5_VABI sceAudioOutMasteringGetState(void*); int APS5_VABI sceAudioOut2EnableChat(); int APS5_VABI sceAudioOut2DisableChat(); } @@ -39,6 +45,16 @@ static bool ThrowsRuntimeError(TFunction function) { return false; } +template +static bool ThrowsException(TFunction function) { + try { + function(); + } catch (const std::exception&) { + return true; + } + return false; +} + namespace { static_assert(sizeof(AudioOut2MasteringCompressorStates) == 208); @@ -127,6 +143,67 @@ void TestMasteringGetState() { Require(sceAudioOut2MasteringGetState(storage, 0, 0) == errorMasteringInvalidStatesId, "states id 0 is refused"); } +void TestLegacyMasteringInit() { + Require(sceAudioOutMasteringInit(0) == 0, "legacy flags 0 must be accepted"); + Require(ThrowsException([] { sceAudioOutMasteringInit(1); }), "legacy flags SwapEqCompressor must throw"); + Require(sceAudioOutMasteringInit(0) == 0, "a repeated legacy initialization must be accepted"); + Require(ThrowsException([] { sceAudioOutMasteringInit(2); }), "legacy flags bit 1 must throw"); + Require(ThrowsException([] { sceAudioOutMasteringInit(3); }), "legacy flags bits 0 and 1 must throw"); + Require(ThrowsException([] { sceAudioOutMasteringInit(0x80000000u); }), "legacy flags bit 31 must throw"); + Require(ThrowsException([] { sceAudioOutMasteringInit(0xFFFFFFFFu); }), "legacy flags 0xFFFFFFFF must throw"); +} + +void TestLegacyMasteringTerm() { + Require(sceAudioOutMasteringTerm() == 0, "legacy termination must be accepted"); + Require(sceAudioOutMasteringTerm() == 0, "legacy termination must be accepted for each initialization"); +} + +void TestLegacyMasteringSetParam() { + alignas(16) std::uint32_t params[16]; + std::fill(params, params + 16, 0xA5A5A5A5u); + params[0] = 1u; + Require(sceAudioOutMasteringSetParam(params, 0) == 0, "legacy mastering parameters must be accepted"); + Require(ThrowsException([&] { sceAudioOutMasteringSetParam(params, 1); }), "legacy parameters with SwapEqCompressor must throw"); + Require(params[0] == 1u && params[1] == 0xA5A5A5A5u && params[15] == 0xA5A5A5A5u, "legacy mastering parameters are not modified"); + Require(ThrowsException([&] { sceAudioOutMasteringSetParam(params, 2); }), "legacy parameters with flags bit 1 must throw"); + Require(ThrowsException([&] { sceAudioOutMasteringSetParam(params, 0xFFFFFFFFu); }), "legacy parameters with unknown flags must throw"); + Require(ThrowsException([] { sceAudioOutMasteringSetParam(nullptr, 0); }), "null legacy parameters must throw"); + Require(ThrowsException([] { sceAudioOutMasteringSetParam(nullptr, 2); }), "null legacy parameters with unknown flags must throw"); + params[0] = 0u; + Require(ThrowsException([&] { sceAudioOutMasteringSetParam(params, 0); }), "legacy params id 0 must throw"); + params[0] = 2u; + Require(ThrowsException([&] { sceAudioOutMasteringSetParam(params, 0); }), "legacy params id 2 must throw"); + params[0] = 0x01010001u; + Require(ThrowsException([&] { sceAudioOutMasteringSetParam(params, 0); }), "an unknown legacy params id must throw"); +} + +void TestLegacyMasteringGetState() { + constexpr std::size_t size = 512; + constexpr std::size_t statesSize = 336; + alignas(16) std::uint8_t storage[size]; + auto* header = reinterpret_cast(storage); + std::fill(storage, storage + size, 0xA5); + *header = 1u; + Require(sceAudioOutMasteringGetState(storage) == 0, "legacy states id 1 must be accepted"); + Require(*header == 1u, "legacy states id 1 is kept"); + const std::pair meters[] = {{32, 128}, {128, 224}, {240, 272}, {272, 304}, {304, 336}}; + for (const auto& meter : meters) { + Require(std::all_of(storage + meter.first, storage + meter.second, [](std::uint8_t byte) { return byte == 0; }), "the legacy mastering meters are zeroed"); + } + Require(std::all_of(storage + 4, storage + 32, [](std::uint8_t byte) { return byte == 0xA5; }), "the bytes before the compressor meters are not modified"); + Require(std::all_of(storage + 224, storage + 240, [](std::uint8_t byte) { return byte == 0xA5; }), "the limiter descriptor bytes are not modified"); + Require(std::all_of(storage + statesSize, storage + size, [](std::uint8_t byte) { return byte == 0xA5; }), "the bytes after the states are not modified"); + Require(sceAudioOutMasteringGetState(storage) == 0, "legacy states id 1 must be accepted again"); + + for (std::uint32_t id : {0u, 2u, 3u, 0x01010001u, 0xFFFFFFFFu}) { + std::fill(storage, storage + size, 0xA5); + *header = id; + Require(ThrowsException([&] { sceAudioOutMasteringGetState(storage); }), "an unknown legacy states id must throw"); + Require(*header == id && std::all_of(storage + 4, storage + size, [](std::uint8_t byte) { return byte == 0xA5; }), "a refused legacy states id leaves the buffer unmodified"); + } + Require(ThrowsException([] { sceAudioOutMasteringGetState(nullptr); }), "a null legacy state must throw"); +} + void TestSetSystemDebugState() { std::uint32_t loudness[2] = {1, 0}; const auto set = [&loudness](std::uint32_t id, void* data, std::size_t size) { @@ -174,6 +251,10 @@ int main() { TestMasteringTerm(); TestMasteringSetParam(); TestMasteringGetState(); + TestLegacyMasteringInit(); + TestLegacyMasteringTerm(); + TestLegacyMasteringSetParam(); + TestLegacyMasteringGetState(); TestSetSystemDebugState(); TestEnableChat(); TestLowLatencyService(); diff --git a/core/libs/prx/libSceAvPlayer/Export.cpp b/core/libs/prx/libSceAvPlayer/Export.cpp index 93d3d8fb0f..8eba9a183b 100644 --- a/core/libs/prx/libSceAvPlayer/Export.cpp +++ b/core/libs/prx/libSceAvPlayer/Export.cpp @@ -198,5 +198,28 @@ void SetAvPlayerClockForTest_nid_no_patch(AvPlayerInternal* h, double millisecon if (h) ToPlayer(h)->SetClockTimeForTest(milliseconds); } +void HoldAvPlayerAudioEofForTest_nid_no_patch(AvPlayerInternal* h, bool hold) { + if (h) ToPlayer(h)->SetHoldAudioEofForTest(hold); +} + +bool AvPlayerAudioEofHeldForTest_nid_no_patch(AvPlayerInternal* h) { + return h && ToPlayer(h)->AudioEofHeldForTest(); +} + +bool AvPlayerAudioEndedForTest_nid_no_patch(AvPlayerInternal* h) { + return h && ToPlayer(h)->AudioEndedForTest(); +} + +int QueuedAvPlayerAudioForTest_nid_no_patch(AvPlayerInternal* h) { + return h ? ToPlayer(h)->QueuedAudioForTest() : 0; +} + +bool NextQueuedAvPlayerAudioTimestampForTest_nid_no_patch(AvPlayerInternal* h, std::uint64_t* timestamp) { + std::uint64_t value = 0; + if (!h || !timestamp || !ToPlayer(h)->NextQueuedAudioTimestampForTest(value)) return false; + *timestamp = value; + return true; +} + } #pragma GCC visibility pop diff --git a/core/libs/prx/libSceAvPlayer/include/AvPlayer.hpp b/core/libs/prx/libSceAvPlayer/include/AvPlayer.hpp index 1c82453073..4e7f7f7759 100644 --- a/core/libs/prx/libSceAvPlayer/include/AvPlayer.hpp +++ b/core/libs/prx/libSceAvPlayer/include/AvPlayer.hpp @@ -87,6 +87,11 @@ public: virtual std::uint64_t CurrentTime() = 0; virtual bool Finished() = 0; virtual void SetClockTimeForTest(double milliseconds) = 0; + virtual void SetHoldAudioEofForTest(bool hold) = 0; + virtual bool AudioEofHeldForTest() = 0; + virtual bool AudioEndedForTest() = 0; + virtual int QueuedAudioForTest() = 0; + virtual bool NextQueuedAudioTimestampForTest(std::uint64_t& timestamp) = 0; }; std::unique_ptr OpenSource(const SourceSettings& settings, const std::string& path, ISourceEvents& events); @@ -94,6 +99,11 @@ std::unique_ptr OpenSource(const SourceSettings& settings, const std::s std::uint32_t DetectSourceType(std::string_view path); void SetAvPlayerClockForTest_nid_no_patch(AvPlayerInternal* h, double milliseconds); +void HoldAvPlayerAudioEofForTest_nid_no_patch(AvPlayerInternal* h, bool hold); +bool AvPlayerAudioEofHeldForTest_nid_no_patch(AvPlayerInternal* h); +bool AvPlayerAudioEndedForTest_nid_no_patch(AvPlayerInternal* h); +int QueuedAvPlayerAudioForTest_nid_no_patch(AvPlayerInternal* h); +bool NextQueuedAvPlayerAudioTimestampForTest_nid_no_patch(AvPlayerInternal* h, std::uint64_t* timestamp); class Player final : public AvPlayerInternal, private ISourceEvents { public: @@ -124,6 +134,11 @@ public: bool IsActive(); std::uint64_t CurrentTime(); void SetClockTimeForTest(double milliseconds); + void SetHoldAudioEofForTest(bool hold); + bool AudioEofHeldForTest(); + bool AudioEndedForTest(); + int QueuedAudioForTest(); + bool NextQueuedAudioTimestampForTest(std::uint64_t& timestamp); private: enum class State { Initial, Ready, Play, Pause, Stop, EndOfFile, Error }; diff --git a/core/libs/prx/libSceAvPlayer/src/Player.cpp b/core/libs/prx/libSceAvPlayer/src/Player.cpp index 90c3b57dba..f253ee42f4 100644 --- a/core/libs/prx/libSceAvPlayer/src/Player.cpp +++ b/core/libs/prx/libSceAvPlayer/src/Player.cpp @@ -391,4 +391,29 @@ void Player::SetClockTimeForTest(double milliseconds) { if (source) source->SetClockTimeForTest(milliseconds); } +void Player::SetHoldAudioEofForTest(bool hold) { + std::lock_guard lock(mutex); + if (source) source->SetHoldAudioEofForTest(hold); +} + +bool Player::AudioEofHeldForTest() { + std::lock_guard lock(mutex); + return source && source->AudioEofHeldForTest(); +} + +bool Player::AudioEndedForTest() { + std::lock_guard lock(mutex); + return source && source->AudioEndedForTest(); +} + +int Player::QueuedAudioForTest() { + std::lock_guard lock(mutex); + return source ? source->QueuedAudioForTest() : 0; +} + +bool Player::NextQueuedAudioTimestampForTest(std::uint64_t& timestamp) { + std::lock_guard lock(mutex); + return source && source->NextQueuedAudioTimestampForTest(timestamp); +} + } diff --git a/core/libs/prx/libSceAvPlayer/src/Source.cpp b/core/libs/prx/libSceAvPlayer/src/Source.cpp index 4d31c38f1d..452e4a88c8 100644 --- a/core/libs/prx/libSceAvPlayer/src/Source.cpp +++ b/core/libs/prx/libSceAvPlayer/src/Source.cpp @@ -151,6 +151,7 @@ public: base = milliseconds; wall = std::chrono::steady_clock::now(); running = true; + if (overrideTime >= 0.0) overrideTime = milliseconds; } void Reset(double milliseconds) { @@ -354,6 +355,9 @@ public: audioDriving = false; presented = false; lastPresented = 0; + audioHasTimestamp = false; + lastAudioTimestamp = 0.0; + reconciledAudioClock = false; presentationClock.Reset(static_cast(start)); for (auto* decoder : {&video, &audio}) { decoder->switchTo = -1; @@ -444,6 +448,34 @@ public: presentationClock.SetOverrideForTest(milliseconds); } + void SetHoldAudioEofForTest(bool hold) override { + std::lock_guard lock(mutex); + holdAudioEof = hold; + condition.notify_all(); + } + + bool AudioEofHeldForTest() override { + std::lock_guard lock(mutex); + return audioEofHeld; + } + + bool AudioEndedForTest() override { + std::lock_guard lock(mutex); + return audio.stream >= 0 && audio.ended; + } + + int QueuedAudioForTest() override { + std::lock_guard lock(mutex); + return static_cast(audio.frames.size()); + } + + bool NextQueuedAudioTimestampForTest(std::uint64_t& timestamp) override { + std::lock_guard lock(mutex); + if (audio.frames.empty()) return false; + timestamp = audio.frames.front().info.timestamp; + return true; + } + bool GetVideoData(AvPlayerFrameInfoEx& info) override { bool taken = false; { @@ -475,6 +507,7 @@ public: std::uint64_t CurrentTime() override { std::lock_guard lock(mutex); + reconcileExhaustedAudioClock(); return clockMillis(); } @@ -510,6 +543,34 @@ private: return duration != 0 ? std::min(milliseconds, duration) : milliseconds; } + bool audioExhausted() const { + return audioHasTimestamp && demuxEnded && !reposition && audio.ended && audio.packets.empty() && audio.frames.empty(); + } + + bool audioClockReconciled() const { + return reconciledAudioClock && reconciledAudioEpoch == clockEpoch; + } + + void markAudioClockReconciled() { + reconciledAudioClock = true; + reconciledAudioEpoch = clockEpoch; + } + + void reconcileExhaustedAudioClock() { + if (!audioDriving || !audioExhausted() || audioClockReconciled()) return; + presentationClock.Rebase(lastAudioTimestamp); + markAudioClockReconciled(); + } + + void waitBeforeAudioEof() { + std::unique_lock lock(mutex); + if (!holdAudioEof || stopping) return; + audioEofHeld = true; + condition.notify_all(); + condition.wait(lock, [&] { return stopping || !holdAudioEof; }); + audioEofHeld = false; + } + bool takeVideo(AvPlayerFrameInfoEx& info) { if (!started || video.stream < 0 || speed < 0) return false; if (speed != NormalSpeed) dropLateAudio(); @@ -542,12 +603,23 @@ private: if (!started || paused || audio.stream < 0 || speed != NormalSpeed) return false; auto& frames = audio.frames; while (!frames.empty() && frames.front().epoch < minEpoch) recycleFront(audio); - if (frames.empty()) return false; + if (frames.empty()) { + reconcileExhaustedAudioClock(); + return false; + } const auto epoch = frames.front().epoch; present(audio, info); + const auto currentClock = presentationClock.Now(); + const auto timestamp = static_cast(info.timestamp); + lastAudioTimestamp = timestamp; + audioHasTimestamp = true; + const bool drifted = currentClock > timestamp + 50.0 || timestamp > currentClock + 50.0; + const bool exhausted = audioExhausted() && !audioClockReconciled(); + const bool shouldRebase = !audioDriving || epoch > clockEpoch || drifted || exhausted; audioDriving = true; clockEpoch = std::max(clockEpoch, epoch); - presentationClock.Rebase(static_cast(info.timestamp)); + if (shouldRebase) presentationClock.Rebase(timestamp); + if (exhausted) markAudioClockReconciled(); return true; } @@ -795,6 +867,7 @@ private: pushAll(PacketKind::Flush, demuxEpoch, requested->target); demuxEnded = false; audioDriving = false; + reconciledAudioClock = false; } condition.notify_all(); if (requested->announce) events.OnWarning(SCE_AVPLAYER_ERROR_WAR_JUMP_COMPLETE); @@ -889,9 +962,11 @@ private: receiveFrames(decoder, frame); avcodec_flush_buffers(decoder.context); } + if (item.kind == PacketKind::End && !decoder.video) waitBeforeAudioEof(); std::lock_guard lock(mutex); if (item.kind == PacketKind::End) { decoder.ended = true; + if (!decoder.video) reconcileExhaustedAudioClock(); } else { decoder.epoch = item.epoch; decoder.skipBefore = 0; @@ -1098,6 +1173,12 @@ private: std::uint64_t clockEpoch = 0; std::deque loopEpochs; bool audioDriving = false; + bool audioHasTimestamp = false; + double lastAudioTimestamp = 0.0; + bool reconciledAudioClock = false; + std::uint64_t reconciledAudioEpoch = 0; + bool holdAudioEof = false; + bool audioEofHeld = false; bool presented = false; std::uint64_t lastPresented = 0; Clock presentationClock; diff --git a/core/libs/prx/libSceBgft/Export.cpp b/core/libs/prx/libSceBgft/Export.cpp index 483969400b..68c25e4d3a 100644 --- a/core/libs/prx/libSceBgft/Export.cpp +++ b/core/libs/prx/libSceBgft/Export.cpp @@ -373,4 +373,73 @@ int APS5_VABI sceBgftServiceIntUploadUnregisterTaskAll(void) { return 0; } +int APS5_VABI sceBgftServiceIntDebugPlayGoClearSetFreeZone() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceBgftServiceIntDebugPlayGoIsSetFreeZone() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceBgftServiceIntDownloadCheckPatchUpdateState() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceBgftServiceIntDownloadClearStats() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceBgftServiceIntDownloadGetApplicationDataSize() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceBgftServiceIntDownloadGetPatchGoProgress() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceBgftServiceIntDownloadHideTask() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceBgftServiceIntDownloadSetResumeState() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceBgftServiceIntDownloadSetStartState() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceBgftServiceIntPlayGoCheckDiscIniChunksInstall() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceBgftServiceIntPlayGoStartDataDiscInstall() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceBgftServiceIntSetTaskVisible() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceBgftServiceIntUploadGetAvailableTaskCount() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceBgftServiceIntUploadGetTaskInfo() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceBgftServiceIntUploadHideTask() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceBgftServiceIntUploadSetStartState() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceBgftServiceIntUploadSetUploadError() { + NotImplemented_nid_no_patch(__func__); + return 0; +} + } diff --git a/core/libs/prx/libSceFiber/Export.cpp b/core/libs/prx/libSceFiber/Export.cpp index 54b0930521..e214387ae3 100644 --- a/core/libs/prx/libSceFiber/Export.cpp +++ b/core/libs/prx/libSceFiber/Export.cpp @@ -36,6 +36,11 @@ static constexpr std::size_t FIBER_MIN_CONTEXT_SIZE = 512; static constexpr std::uint64_t FIBER_CONTEXT_FILL = 0xdeadbeefdeadbeefull; static constexpr std::size_t FIBER_HOST_STACK_MARGIN = 1u << 20; static constexpr std::size_t FIBER_GUEST_STACK_MIN = 64u << 10; +static constexpr std::uint32_t FIBER_FLAG_NO_ULOBJMGR = 0x1; +static constexpr std::uint32_t FIBER_FLAG_CONTEXT_SIZE_CHECK = 0x10; +static constexpr std::uint32_t FIBER_FLAG_SET_FPU_REGISTERS = 0x100; +static constexpr std::uint32_t FIBER_DEFAULT_MXCSR = 0x9fc0; +static constexpr std::uint16_t FIBER_DEFAULT_FPU_CONTROL = 0x037f; using GuestFiberEntry = FiberEntry; @@ -60,6 +65,7 @@ struct Fiber { void* savedStack; char name[FIBER_MAX_NAME_LENGTH + 1]; bool contextSizeCheck; + bool setFpuRegisters; std::uint8_t* hostStack; std::uint64_t hostStackBytes; }; @@ -365,9 +371,13 @@ static void PrepareInitialStack(Fiber* fiber) { const auto top = reinterpret_cast(StackTop(fiber)) & ~static_cast(15); auto* frame = reinterpret_cast(top - 256); std::memset(frame, 0, sizeof(*frame)); - frame->mxcsr = _mm_getcsr(); - std::uint16_t control = 0; - asm volatile("fnstcw %0" : "=m"(control)); + std::uint16_t control = FIBER_DEFAULT_FPU_CONTROL; + if (fiber->setFpuRegisters) { + frame->mxcsr = FIBER_DEFAULT_MXCSR; + } else { + frame->mxcsr = _mm_getcsr(); + asm volatile("fnstcw %0" : "=m"(control)); + } frame->fpuControl = control; frame->r12 = reinterpret_cast(fiber); frame->returnAddress = reinterpret_cast(&Aps5FiberTrampoline_nid_no_patch); @@ -383,12 +393,58 @@ static bool AcquireForResume(Fiber* target) { } if (state != FiberState::Idle && state != FiberState::Suspended) return false; if (target->state.compare_exchange_weak(state, FiberState::Running, std::memory_order_acq_rel)) { - if (state == FiberState::Idle) PrepareInitialStack(target); + if (state == FiberState::Idle) { + if (!target->context) { + target->state.store(FiberState::Idle, std::memory_order_release); + throw std::runtime_error(std::string("sceFiber: fiber '") + target->name + "' has no context; running a fiber on the stack of its caller is not supported"); + } + PrepareInitialStack(target); + } return true; } } } +static int32_t ValidateContext(const void* addr_context, uint64_t size_context) { + if (addr_context == nullptr && size_context != 0) return SCE_FIBER_ERROR_INVALID; + if ((reinterpret_cast(addr_context) & 15) != 0 || (size_context & 15) != 0) return SCE_FIBER_ERROR_ALIGNMENT; + if (addr_context != nullptr && size_context < FIBER_MIN_CONTEXT_SIZE) return SCE_FIBER_ERROR_RANGE; + return SCE_OK; +} + +static void AttachStack(Fiber* fiber, void* addr_context, uint64_t size_context) { + fiber->context = static_cast(addr_context); + fiber->contextSize = size_context; + if (size_context < FIBER_GUEST_STACK_MIN) AllocateHostStack(fiber); + if (fiber->contextSizeCheck) { + auto* words = SizedWindow(fiber); + std::fill(words, words + size_context / sizeof(std::uint64_t), FIBER_CONTEXT_FILL); + } + PinStack(addr_context, size_context); +} + +static void DetachStack(Fiber* fiber) { + if (!fiber->context) return; + UnpinStack(fiber->context, fiber->contextSize); + ReleaseHostStack(fiber); +} + +static int32_t AcquireWithContext(Fiber* target, void* addr_context, uint64_t size_context) { + if (addr_context == nullptr && size_context == 0) return AcquireForResume(target) ? SCE_OK : SCE_FIBER_ERROR_STATE; + if (addr_context == nullptr) return SCE_FIBER_ERROR_INVALID; + if (const auto valid = ValidateContext(addr_context, size_context); valid != SCE_OK) return valid; + auto expected = FiberState::Idle; + if (!target->state.compare_exchange_strong(expected, FiberState::Running, std::memory_order_acq_rel)) return SCE_FIBER_ERROR_INVALID; + if (target->context) { + target->state.store(FiberState::Idle, std::memory_order_release); + return SCE_FIBER_ERROR_INVALID; + } + AttachStack(target, addr_context, size_context); + if (TraceFibers()) std::fprintf(stderr, "[fiber] attach %s context=%p+0x%llx\n", target->name, addr_context, static_cast(size_context)); + PrepareInitialStack(target); + return SCE_OK; +} + static void Resume(Fiber* target, void** save, std::uint64_t argOnRun) { const auto* frame = static_cast(target->savedStack); if (frame->returnAddress == 0) { @@ -405,58 +461,32 @@ static void Resume(Fiber* target, void** save, std::uint64_t argOnRun) { Aps5FiberSwitchStack_nid_no_patch(save, target->savedStack); } -extern "C" { - -int32_t APS5_VABI _sceFiberInitializeImpl_nid_postfix(FiberObject* object, const char* name, GuestFiberEntry entry, uint64_t arg_on_initialize, void* addr_context, uint64_t size_context, const void* opt_param, uint32_t build_version) { - (void)opt_param; - (void)build_version; +static int32_t InitializeFiber(FiberObject* object, const char* name, GuestFiberEntry entry, uint64_t arg_on_initialize, void* addr_context, uint64_t size_context, uint32_t flags) { if (!object || !name || !entry) return SCE_FIBER_ERROR_NULL; if ((reinterpret_cast(object) & 7) != 0) return SCE_FIBER_ERROR_ALIGNMENT; - if (addr_context == nullptr && size_context != 0) return SCE_FIBER_ERROR_INVALID; - if ((reinterpret_cast(addr_context) & 15) != 0 || (size_context & 15) != 0) return SCE_FIBER_ERROR_ALIGNMENT; - if (addr_context == nullptr) return SCE_FIBER_ERROR_INVALID; - if (size_context < FIBER_MIN_CONTEXT_SIZE) return SCE_FIBER_ERROR_RANGE; + if (const auto valid = ValidateContext(addr_context, size_context); valid != SCE_OK) return valid; + if ((flags & ~(FIBER_FLAG_NO_ULOBJMGR | FIBER_FLAG_CONTEXT_SIZE_CHECK | FIBER_FLAG_SET_FPU_REGISTERS)) != 0) throw std::runtime_error("_sceFiberInitializeWithInternalOptionImpl: unsupported flags " + std::to_string(flags)); auto* fiber = reinterpret_cast(object); - if (fiber->magic == FIBER_MAGIC) { - UnpinStack(fiber->context, fiber->contextSize); - ReleaseHostStack(fiber); - } + if (fiber->magic == FIBER_MAGIC) DetachStack(fiber); std::memset(object, 0, FIBER_OBJECT_SIZE); fiber->magic = FIBER_MAGIC; fiber->state.store(FiberState::Idle, std::memory_order_relaxed); fiber->entry = entry; fiber->argOnInitialize = arg_on_initialize; - fiber->context = static_cast(addr_context); - fiber->contextSize = size_context; std::strncpy(fiber->name, name, FIBER_MAX_NAME_LENGTH); - if (size_context < FIBER_GUEST_STACK_MIN) AllocateHostStack(fiber); - fiber->contextSizeCheck = g_contextSizeCheck.load(std::memory_order_relaxed); - if (fiber->contextSizeCheck) { - auto* words = SizedWindow(fiber); - std::fill(words, words + size_context / sizeof(std::uint64_t), FIBER_CONTEXT_FILL); - } - PinStack(addr_context, size_context); - if (TraceFibers()) std::fprintf(stderr, "[fiber] init %s object=%p context=%p+0x%llx entry=%p\n", fiber->name, static_cast(object), addr_context, static_cast(size_context), reinterpret_cast(entry)); + fiber->contextSizeCheck = g_contextSizeCheck.load(std::memory_order_relaxed) || (flags & FIBER_FLAG_CONTEXT_SIZE_CHECK) != 0; + fiber->setFpuRegisters = (flags & FIBER_FLAG_SET_FPU_REGISTERS) != 0; + if (addr_context) AttachStack(fiber, addr_context, size_context); + if (TraceFibers()) std::fprintf(stderr, "[fiber] init %s object=%p context=%p+0x%llx entry=%p flags=0x%x\n", fiber->name, static_cast(object), addr_context, static_cast(size_context), reinterpret_cast(entry), flags); return SCE_OK; } -int32_t APS5_VABI sceFiberFinalize(FiberObject* object) { - auto* fiber = AsFiber(object); - if (!fiber) return object ? SCE_FIBER_ERROR_INVALID : SCE_FIBER_ERROR_NULL; - const auto state = fiber->state.load(std::memory_order_acquire); - if (state == FiberState::Running || state == FiberState::Suspending) return SCE_FIBER_ERROR_STATE; - fiber->magic = 0; - UnpinStack(fiber->context, fiber->contextSize); - ReleaseHostStack(fiber); - return SCE_OK; -} - -int32_t APS5_VABI sceFiberRun_nid_postfix(FiberObject* object, uint64_t arg_on_run, uint64_t* arg_on_return) { +static int32_t RunFiber(FiberObject* object, void* addr_context, uint64_t size_context, uint64_t arg_on_run, uint64_t* arg_on_return, std::uint64_t threadFramePointer) { auto* fiber = AsFiber(object); if (!fiber) return object ? SCE_FIBER_ERROR_INVALID : SCE_FIBER_ERROR_NULL; if (ThreadState().current) return SCE_FIBER_ERROR_PERMISSION; - if (!AcquireForResume(fiber)) return SCE_FIBER_ERROR_STATE; - ThreadState().threadFramePointer = reinterpret_cast(static_cast(__builtin_frame_address(0))[0]); + if (const auto acquired = AcquireWithContext(fiber, addr_context, size_context); acquired != SCE_OK) return acquired; + ThreadState().threadFramePointer = threadFramePointer; ThreadState().threadBounds = CurrentBounds(); Resume(fiber, &ThreadState().threadStack, arg_on_run); CompletePendingSuspend(); @@ -465,12 +495,13 @@ int32_t APS5_VABI sceFiberRun_nid_postfix(FiberObject* object, uint64_t arg_on_r return SCE_OK; } -int32_t APS5_VABI sceFiberSwitch(FiberObject* object, uint64_t arg_on_run, uint64_t* arg_on_run_out) { +static int32_t SwitchFiber(FiberObject* object, void* addr_context, uint64_t size_context, uint64_t arg_on_run, uint64_t* arg_on_run_out) { auto* target = AsFiber(object); if (!target) return object ? SCE_FIBER_ERROR_INVALID : SCE_FIBER_ERROR_NULL; auto* self = ThreadState().current; if (!self) return SCE_FIBER_ERROR_PERMISSION; - if (target == self || !AcquireForResume(target)) return SCE_FIBER_ERROR_STATE; + if (target == self) return SCE_FIBER_ERROR_STATE; + if (const auto acquired = AcquireWithContext(target, addr_context, size_context); acquired != SCE_OK) return acquired; if (TraceFibers()) { auto** frame = static_cast(__builtin_frame_address(0)); void* chain[6] = {}; @@ -489,6 +520,36 @@ int32_t APS5_VABI sceFiberSwitch(FiberObject* object, uint64_t arg_on_run, uint6 return SCE_OK; } +static std::uint64_t CallerFramePointer(void* frame) { + return reinterpret_cast(static_cast(frame)[0]); +} + +extern "C" { + +int32_t APS5_VABI _sceFiberInitializeImpl_nid_postfix(FiberObject* object, const char* name, GuestFiberEntry entry, uint64_t arg_on_initialize, void* addr_context, uint64_t size_context, const void* opt_param, uint32_t build_version) { + (void)opt_param; + (void)build_version; + return InitializeFiber(object, name, entry, arg_on_initialize, addr_context, size_context, 0); +} + +int32_t APS5_VABI sceFiberFinalize(FiberObject* object) { + auto* fiber = AsFiber(object); + if (!fiber) return object ? SCE_FIBER_ERROR_INVALID : SCE_FIBER_ERROR_NULL; + const auto state = fiber->state.load(std::memory_order_acquire); + if (state == FiberState::Running || state == FiberState::Suspending) return SCE_FIBER_ERROR_STATE; + fiber->magic = 0; + DetachStack(fiber); + return SCE_OK; +} + +int32_t APS5_VABI sceFiberRun_nid_postfix(FiberObject* object, uint64_t arg_on_run, uint64_t* arg_on_return) { + return RunFiber(object, nullptr, 0, arg_on_run, arg_on_return, CallerFramePointer(__builtin_frame_address(0))); +} + +int32_t APS5_VABI sceFiberSwitch(FiberObject* object, uint64_t arg_on_run, uint64_t* arg_on_run_out) { + return SwitchFiber(object, nullptr, 0, arg_on_run, arg_on_run_out); +} + int32_t APS5_VABI sceFiberReturnToThread(uint64_t arg_on_return, uint64_t* arg_on_run) { auto* self = ThreadState().current; if (!self) return SCE_FIBER_ERROR_PERMISSION; @@ -565,24 +626,18 @@ int32_t APS5_VABI sceFiberStopContextSizeCheck(void) { return g_contextSizeCheck.compare_exchange_strong(expected, false) ? SCE_OK : SCE_FIBER_ERROR_STATE; } -int APS5_VABI _sceFiberAttachContextAndRun(FiberObject* fiber, void* addr_context, uint64_t size_context, - uint64_t arg_on_run, uint64_t* arg_on_return) { - NotImplemented_nid_no_patch(__func__); - return 0; +int32_t APS5_VABI _sceFiberAttachContextAndRun(FiberObject* object, void* addr_context, uint64_t size_context, uint64_t arg_on_run, uint64_t* arg_on_return) { + return RunFiber(object, addr_context, size_context, arg_on_run, arg_on_return, CallerFramePointer(__builtin_frame_address(0))); } -int APS5_VABI _sceFiberAttachContextAndSwitch(FiberObject* fiber, void* addr_context, uint64_t size_context, - uint64_t arg_on_run, uint64_t* arg_on_run_out) { - NotImplemented_nid_no_patch(__func__); - return 0; +int32_t APS5_VABI _sceFiberAttachContextAndSwitch(FiberObject* object, void* addr_context, uint64_t size_context, uint64_t arg_on_run, uint64_t* arg_on_run_out) { + return SwitchFiber(object, addr_context, size_context, arg_on_run, arg_on_run_out); } -int APS5_VABI _sceFiberInitializeWithInternalOptionImpl(FiberObject* fiber, const char* name, GuestFiberEntry entry, - uint64_t arg_on_initialize, void* addr_context, - uint64_t size_context, const FiberOptParam* opt_param, - uint32_t flags, uint32_t build_version) { - NotImplemented_nid_no_patch(__func__); - return 0; +int32_t APS5_VABI _sceFiberInitializeWithInternalOptionImpl(FiberObject* object, const char* name, GuestFiberEntry entry, uint64_t arg_on_initialize, void* addr_context, uint64_t size_context, const FiberOptParam* opt_param, uint32_t flags, uint32_t build_version) { + (void)opt_param; + (void)build_version; + return InitializeFiber(object, name, entry, arg_on_initialize, addr_context, size_context, flags); } } diff --git a/core/libs/prx/libSceFreeType/CMakeLists.txt b/core/libs/prx/libSceFreeType/CMakeLists.txt index 689682460c..4fec867345 100644 --- a/core/libs/prx/libSceFreeType/CMakeLists.txt +++ b/core/libs/prx/libSceFreeType/CMakeLists.txt @@ -1,10 +1,12 @@ add_library(libSceFreeType SHARED EXCLUDE_FROM_ALL Export.cpp + src/Bitmap.cpp src/Outline.cpp src/Charmap.cpp src/FaceInfo.cpp src/GuestObjects.cpp src/Names.cpp + src/Trigonometry.cpp ) target_include_directories(libSceFreeType PRIVATE ${LIBS_INCLUDE_DIR}) target_link_libraries(libSceFreeType PRIVATE freetype) @@ -29,6 +31,16 @@ endif() configure_windows_unwind(libSceFreeType) if(BUILD_TESTING) + add_test_executable(guest_freetype_bitmap_tests ${LIBS_INCLUDE_DIR}/tests/GuestFreeTypeBitmap.cpp) + target_include_directories(guest_freetype_bitmap_tests PRIVATE ${LIBS_INCLUDE_DIR}) + target_link_libraries(guest_freetype_bitmap_tests PRIVATE libSceFreeType freetype libc) + configure_windows_unwind(guest_freetype_bitmap_tests) + add_test(NAME guest_freetype_bitmap COMMAND guest_freetype_bitmap_tests) + if(WIN32) + set_tests_properties(guest_freetype_bitmap PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + endif() + add_test_executable(guest_freetype_outline_tests ${LIBS_INCLUDE_DIR}/tests/GuestFreeTypeOutline.cpp) target_include_directories(guest_freetype_outline_tests PRIVATE ${LIBS_INCLUDE_DIR}) target_link_libraries(guest_freetype_outline_tests PRIVATE libSceFreeType freetype libc) diff --git a/core/libs/prx/libSceFreeType/Export.cpp b/core/libs/prx/libSceFreeType/Export.cpp index 6b29a99924..29fd059381 100644 --- a/core/libs/prx/libSceFreeType/Export.cpp +++ b/core/libs/prx/libSceFreeType/Export.cpp @@ -4,6 +4,7 @@ #include "prx/libc/include/General.hpp" #include FT_GASP_H +#include FT_CID_H #include #include @@ -18,6 +19,8 @@ using namespace FreeType; +static_assert(sizeof(FT_Bool) == 1 && sizeof(FT_Int) == 4 && sizeof(FT_UInt) == 4); + namespace { void FinalizeSlot(void* object) { @@ -201,6 +204,22 @@ int APS5_VABI FT_Load_Glyph_nid_postfix(GuestFaceRec* face, std::uint32_t glyph_ return error; } +int APS5_VABI FT_Get_CID_Registry_Ordering_Supplement_nid_postfix(GuestFaceRec* face, const char** registry, + const char** ordering, std::int32_t* supplement) { + const GuestFace* owner = FaceOf(face); + return FT_Get_CID_Registry_Ordering_Supplement(owner ? owner->host : nullptr, registry, ordering, supplement); +} + +int APS5_VABI FT_Get_CID_Is_Internally_CID_Keyed_nid_postfix(GuestFaceRec* face, std::uint8_t* isCid) { + const GuestFace* owner = FaceOf(face); + return FT_Get_CID_Is_Internally_CID_Keyed(owner ? owner->host : nullptr, isCid); +} + +int APS5_VABI FT_Get_CID_From_Glyph_Index_nid_postfix(GuestFaceRec* face, std::uint32_t glyphIndex, std::uint32_t* cid) { + const GuestFace* owner = FaceOf(face); + return FT_Get_CID_From_Glyph_Index(owner ? owner->host : nullptr, glyphIndex, cid); +} + int APS5_VABI FT_Get_Gasp_nid_postfix(GuestFaceRec* face, std::uint32_t ppem) { const GuestFace* owner = FaceOf(face); return FT_Get_Gasp(owner ? owner->host : nullptr, ppem); diff --git a/core/libs/prx/libSceFreeType/include/GuestObjects.hpp b/core/libs/prx/libSceFreeType/include/GuestObjects.hpp index d237147b89..9ae0c12315 100644 --- a/core/libs/prx/libSceFreeType/include/GuestObjects.hpp +++ b/core/libs/prx/libSceFreeType/include/GuestObjects.hpp @@ -19,6 +19,8 @@ struct GuestLibrary { std::list> faces; }; +void UnregisterBitmap_nid_no_patch(const GuestBitmap* bitmap); + struct GuestFace { GuestFaceRec rec{}; GuestGlyphSlotRec slot{}; @@ -30,8 +32,12 @@ struct GuestFace { std::vector charmaps; std::vector charmapPointers; std::vector outlinePoints; + + ~GuestFace() { UnregisterBitmap_nid_no_patch(&slot.bitmap); } }; +void RegisterBitmap_nid_no_patch(GuestFace& face); + GuestFace* FaceOf(const GuestFaceRec* face); GuestFace* FaceOf(const GuestGlyphSlotRec* slot); void SyncFace(GuestFace& face); diff --git a/core/libs/prx/libSceFreeType/src/Bitmap.cpp b/core/libs/prx/libSceFreeType/src/Bitmap.cpp new file mode 100644 index 0000000000..2e8a5d750e --- /dev/null +++ b/core/libs/prx/libSceFreeType/src/Bitmap.cpp @@ -0,0 +1,100 @@ +#include "prx/libSceFreeType/include/GuestObjects.hpp" +#include "prx/libc/include/general/VabiMacros.hpp" + +#include FT_BITMAP_H + +#include +#include +#include +#include + +using namespace FreeType; + +namespace { + +struct BitmapOwners { + std::mutex mutex; + std::unordered_map bitmaps; +}; + +BitmapOwners& Owners() { + static BitmapOwners owners; + return owners; +} + +FT_Bitmap NativeBitmap(const GuestBitmap& bitmap) { + return {bitmap.rows, bitmap.width, bitmap.pitch, bitmap.buffer, bitmap.num_grays, bitmap.pixel_mode, bitmap.palette_mode, bitmap.palette}; +} + +void SyncBitmap(GuestBitmap& guest, const FT_Bitmap& bitmap) { + guest = {bitmap.rows, bitmap.width, bitmap.pitch, bitmap.buffer, bitmap.num_grays, bitmap.pixel_mode, bitmap.palette_mode, bitmap.palette}; + auto& owners = Owners(); + std::lock_guard lock(owners.mutex); + const auto found = owners.bitmaps.find(&guest); + if (found != owners.bitmaps.end() && found->second) *found->second = bitmap; +} + +} + +namespace FreeType { + +void RegisterBitmap_nid_no_patch(GuestFace& face) { + auto& owners = Owners(); + std::lock_guard lock(owners.mutex); + owners.bitmaps.insert_or_assign(&face.slot.bitmap, face.host->glyph ? &face.host->glyph->bitmap : nullptr); +} + +void UnregisterBitmap_nid_no_patch(const GuestBitmap* bitmap) { + auto& owners = Owners(); + std::lock_guard lock(owners.mutex); + owners.bitmaps.erase(bitmap); +} + +} + +extern "C" { + +void APS5_VABI FT_Bitmap_New_nid_postfix(GuestBitmap* bitmap) { + if (bitmap) SyncBitmap(*bitmap, {}); +} + +int APS5_VABI FT_Bitmap_Copy_nid_postfix(GuestLibrary* library, const GuestBitmap* source, GuestBitmap* target) { + if (!library) return FT_Err_Invalid_Library_Handle; + if (!source || !target) return FT_Err_Invalid_Argument; + if (source == target) { + SyncBitmap(*target, NativeBitmap(*target)); + return 0; + } + if (source->buffer && source->buffer == target->buffer) { + throw std::runtime_error("FT_Bitmap_Copy: overlapping source and target buffers are not implemented"); + } + const FT_Bitmap nativeSource = NativeBitmap(*source); + FT_Bitmap nativeTarget = NativeBitmap(*target); + const FT_Error error = FT_Bitmap_Copy(library->host, &nativeSource, &nativeTarget); + SyncBitmap(*target, nativeTarget); + return error; +} + +int APS5_VABI FT_Bitmap_Convert_nid_postfix(GuestLibrary* library, const GuestBitmap* source, GuestBitmap* target, std::int32_t alignment) { + if (!library) return FT_Err_Invalid_Library_Handle; + if (!source || !target) return FT_Err_Invalid_Argument; + if (source == target || (source->buffer && source->buffer == target->buffer)) { + throw std::runtime_error("FT_Bitmap_Convert: overlapping source and target buffers are not implemented"); + } + const FT_Bitmap nativeSource = NativeBitmap(*source); + FT_Bitmap nativeTarget = NativeBitmap(*target); + const FT_Error error = FT_Bitmap_Convert(library->host, &nativeSource, &nativeTarget, alignment); + SyncBitmap(*target, nativeTarget); + return error; +} + +int APS5_VABI FT_Bitmap_Done_nid_postfix(GuestLibrary* library, GuestBitmap* bitmap) { + if (!library) return FT_Err_Invalid_Library_Handle; + if (!bitmap) return FT_Err_Invalid_Argument; + FT_Bitmap native = NativeBitmap(*bitmap); + const FT_Error error = FT_Bitmap_Done(library->host, &native); + SyncBitmap(*bitmap, native); + return error; +} + +} diff --git a/core/libs/prx/libSceFreeType/src/GuestObjects.cpp b/core/libs/prx/libSceFreeType/src/GuestObjects.cpp index 79662344d2..17d1888a21 100644 --- a/core/libs/prx/libSceFreeType/src/GuestObjects.cpp +++ b/core/libs/prx/libSceFreeType/src/GuestObjects.cpp @@ -13,6 +13,7 @@ GuestFace* FaceOf(const GuestGlyphSlotRec* slot) { } void SyncFace(GuestFace& face) { + RegisterBitmap_nid_no_patch(face); const FT_Face host = face.host; auto& rec = face.rec; rec.num_faces = host->num_faces; diff --git a/core/libs/prx/libSceFreeType/src/Trigonometry.cpp b/core/libs/prx/libSceFreeType/src/Trigonometry.cpp new file mode 100644 index 0000000000..a8f85d47fc --- /dev/null +++ b/core/libs/prx/libSceFreeType/src/Trigonometry.cpp @@ -0,0 +1,81 @@ +#include "prx/libSceFreeType/include/GuestFreeType.hpp" + +#include +#include FT_TRIGONOMETRY_H + +#include +#include +#include +#include + +using namespace FreeType; + +namespace { + +FT_Angle NormalizeAngle(std::int64_t angle) { + return static_cast(angle % (360ll << 16)); +} + +GuestVector TangentVector(std::int64_t angle) { + constexpr std::array angles{ + 1740967, 919879, 466945, 234379, 117304, 58666, 29335, 14668, 7334, 3667, 1833, + 917, 458, 229, 115, 57, 29, 14, 7, 4, 2, 1}; + std::int64_t remaining = NormalizeAngle(angle); + GuestVector vector{1ll << 24, 0}; + while (remaining < -(45ll << 16)) { + vector = {vector.y, -vector.x}; + remaining += 90ll << 16; + } + while (remaining > (45ll << 16)) { + vector = {-vector.y, vector.x}; + remaining -= 90ll << 16; + } + std::int64_t rounding = 1; + for (std::size_t iteration = 1; iteration <= angles.size(); ++iteration) { + const std::int64_t direction = remaining < 0 ? 1 : -1; + const GuestVector step{(vector.y + rounding) >> iteration, (vector.x + rounding) >> iteration}; + vector.x += direction * step.x; + vector.y -= direction * step.y; + remaining += direction * angles[iteration - 1]; + rounding <<= 1; + } + return vector; +} + +} + +extern "C" { + +std::int64_t APS5_VABI FT_Cos_nid_postfix(std::int64_t angle) { + return FT_Cos(NormalizeAngle(angle)); +} + +std::int64_t APS5_VABI FT_Sin_nid_postfix(std::int64_t angle) { + return FT_Sin(NormalizeAngle(angle)); +} + +std::int64_t APS5_VABI FT_Tan_nid_postfix(std::int64_t angle) { + const GuestVector vector = TangentVector(angle); + const std::uint64_t x = static_cast(vector.x < 0 ? -vector.x : vector.x); + const std::uint64_t y = static_cast(vector.y < 0 ? -vector.y : vector.y); + const auto quotient = static_cast(x == 0 ? 0x7FFFFFFFull : ((y << 16) + (x >> 1)) / x); + return (vector.x < 0) != (vector.y < 0) ? -quotient : quotient; +} + +void APS5_VABI FT_Vector_Unit_nid_postfix(GuestVector* vector, std::int64_t angle) { + if (!vector) return; + FT_Vector native{}; + FT_Vector_Unit(&native, NormalizeAngle(angle)); + vector->x = native.x; + vector->y = native.y; +} + +std::int64_t APS5_VABI FT_Angle_Diff_nid_postfix(std::int64_t first, std::int64_t second) { + if ((first > 0 && second < std::numeric_limits::min() + first) || + (first < 0 && second > std::numeric_limits::max() + first)) { + throw std::runtime_error("FT_Angle_Diff: overflowing signed angle subtraction is not implemented"); + } + return FT_Angle_Diff(0, NormalizeAngle(second - first)); +} + +} diff --git a/core/libs/prx/libSceGnmDriverForNeoMode/Export.cpp b/core/libs/prx/libSceGnmDriverForNeoMode/Export.cpp index b817e55c45..4bddc78a76 100644 --- a/core/libs/prx/libSceGnmDriverForNeoMode/Export.cpp +++ b/core/libs/prx/libSceGnmDriverForNeoMode/Export.cpp @@ -1,9 +1,11 @@ #include #include #include +#include #include "SceTypes.hpp" #include "prx/libc/include/General.hpp" #include "prx/libSceGnmDriverForNeoMode/include/InitSequences.hpp" +#include "prx/libSceGnmDriverForNeoMode/include/EmbeddedShaders.hpp" namespace { @@ -27,6 +29,23 @@ int WriteContextState(std::uint32_t* cmdbuf, std::uint32_t size, std::uint32_t p return static_cast(packetWords); } +constexpr std::uint32_t OpSetContextReg = 0x69; +constexpr std::uint32_t OpSetShReg = 0x76; + +std::uint32_t* EmitPacket(std::uint32_t* at, std::uint32_t opcode, std::initializer_list body) { + *at++ = 0xc0000000u | ((static_cast(body.size()) - 1) << 16) | (opcode << 8); + for (const auto word : body) *at++ = word; + return at; +} + +std::uint32_t AddressLo(const void* code) { + return static_cast(reinterpret_cast(code) >> 8); +} + +std::uint32_t AddressHi(const void* code) { + return static_cast(reinterpret_cast(code) >> 40); +} + constexpr int Failure = static_cast(0x8EEE00FFu); constexpr int NoInterface = static_cast(0x80000000u); constexpr int ValidationNotEnabled = static_cast(0x80D13FFFu); @@ -69,13 +88,25 @@ bool IndexedIndirectStage(std::uint32_t stage) { constexpr std::uint32_t OpSetUconfigReg = 0x79; constexpr std::uint32_t IaMultiVgtParam = 0x40000258u; +std::uint32_t* UpdatePsRegisters(std::uint32_t* cmdbuf, const std::uint32_t* regs) { + auto* at = EmitPacket(cmdbuf, OpSetShReg, {8, regs[0], 0}); + at = EmitPacket(at, OpSetShReg, {10, regs[2], regs[3]}); + at = EmitPacket(at, OpNop, {0xc01e01c4u, regs[4], regs[5]}); + at = EmitPacket(at, OpNop, {0xc01e01b3u, regs[6], regs[7]}); + at = EmitPacket(at, OpNop, {0xc01e01b6u, regs[8]}); + at = EmitPacket(at, OpNop, {0xc01e01b8u, regs[9]}); + at = EmitPacket(at, OpNop, {0xc01e0203u, regs[10]}); + return EmitPacket(at, OpNop, {0xc01e008fu, regs[11]}); +} + } // namespace extern "C" { -int APS5_VABI sceGnmBeginWorkload(void) { - NotImplemented_nid_no_patch(__func__); - return 0; +int APS5_VABI sceGnmBeginWorkload(std::uint32_t stream, std::uint64_t* workload) { + if (!workload) return 3; + *workload = stream < 0x10 ? 1 : 0; + return stream < 0x10 ? 0 : 1; } int APS5_VABI sceGnmComputeWaitOnAddress(std::uint32_t* cmdbuf, std::uint32_t size, std::uint64_t address, std::uint32_t mask, std::uint32_t compare, std::uint32_t reference) { @@ -103,8 +134,9 @@ int APS5_VABI sceGnmComputeWaitSemaphore(std::uint32_t* cmdbuf, std::uint32_t si return 0; } -int APS5_VABI sceGnmCreateWorkloadStream(void) { - NotImplemented_nid_no_patch(__func__); +int APS5_VABI sceGnmCreateWorkloadStream(std::uint64_t name, std::uint32_t* stream) { + if (name == 0 || !stream) return 3; + *stream = 1; return 0; } @@ -340,9 +372,9 @@ int APS5_VABI sceGnmDriverTraceInProgress(void) { return 0; } -int APS5_VABI sceGnmEndWorkload(void) { - NotImplemented_nid_no_patch(__func__); - return 0; +int APS5_VABI sceGnmEndWorkload(std::uint64_t workload) { + if (workload == 0) return 2; + return ((workload >> 56) & 0xff) > 0xf ? 2 : 0; } int APS5_VABI sceGnmFindResources(void) { @@ -378,8 +410,7 @@ int APS5_VABI sceGnmGetGpuBlockStatus(void) { } int APS5_VABI sceGnmGetGpuCoreClockFrequency(void) { - NotImplemented_nid_no_patch(__func__); - return 0; + return 911000000; } int APS5_VABI sceGnmGetGpuInfoStatus(void) { @@ -398,8 +429,7 @@ int APS5_VABI sceGnmGetNumTcaUnits(void) { } int APS5_VABI sceGnmGetOffChipTessellationBufferSize(void) { - NotImplemented_nid_no_patch(__func__); - return 0; + return 0x800000; } int APS5_VABI sceGnmGetOwnerName(void) { @@ -578,23 +608,65 @@ int APS5_VABI sceGnmSdmaOpen(void) { return Failure; } -int APS5_VABI sceGnmSetCsShaderWithModifier(void) { - NotImplemented_nid_no_patch(__func__); +int APS5_VABI sceGnmSetCsShaderWithModifier(std::uint32_t* cmdbuf, std::uint32_t size, const std::uint32_t* regs, std::uint32_t modifier) { + if (!cmdbuf || size <= 0x18 || !regs || (modifier & 0xfffffc3fu) != 0 || regs[1] != 0) return CommandFailed; + const std::uint32_t rsrc1 = modifier == 0 ? regs[2] : (regs[2] & 0xfffffc3fu) | modifier; + auto* at = EmitPacket(cmdbuf, OpSetShReg, {0x20c, regs[0], 0}); + at = EmitPacket(at, OpSetShReg, {0x212, rsrc1, regs[3]}); + at = EmitPacket(at, OpSetShReg, {0x207, regs[4], regs[5], regs[6]}); + Nop(at, 11); return 0; } -int APS5_VABI sceGnmSetEmbeddedPsShader(void) { - NotImplemented_nid_no_patch(__func__); +int APS5_VABI sceGnmSetEmbeddedPsShader(std::uint32_t* cmdbuf, std::uint32_t, std::uint32_t shaderId, std::uint32_t) { + if (shaderId > 1) return Failure; + const bool black = shaderId == 0; + const auto* code = black ? GnmNeo::BlackPs.data() : GnmNeo::DepthOnlyPs.data(); + auto* at = EmitPacket(cmdbuf, OpSetShReg, {8, AddressLo(code), AddressHi(code)}); + at = EmitPacket(at, OpSetShReg, {10, black ? 0xc0000u : 0x2000u, black ? 4u : 0u}); + at = EmitPacket(at, OpSetContextReg, {0x1c4, 0, black ? 4u : 2u}); + at = EmitPacket(at, OpSetContextReg, {0x1b3, 2, 2}); + at = EmitPacket(at, OpSetContextReg, {0x1b6, 0}); + at = EmitPacket(at, OpSetContextReg, {0x1b8, 0}); + at = EmitPacket(at, OpSetContextReg, {0x203, 0x10}); + at = EmitPacket(at, OpSetContextReg, {0x8f, black ? 0xfu : 3u}); + at[0] = 0xc00a1000u; + at[1] = 0; return 0; } -int APS5_VABI sceGnmSetEmbeddedVsShader(void) { - NotImplemented_nid_no_patch(__func__); +int APS5_VABI sceGnmSetEmbeddedVsShader(std::uint32_t* cmdbuf, std::uint32_t, std::uint32_t shaderId, std::uint32_t) { + if (shaderId != 0) return Failure; + const auto* code = GnmNeo::FullscreenVs.data(); + auto* at = EmitPacket(cmdbuf, OpSetShReg, {0x48, AddressLo(code), AddressHi(code)}); + at = EmitPacket(at, OpSetShReg, {0x4a, 0xc0000, 4}); + at = EmitPacket(at, OpSetContextReg, {0x207, 0}); + at = EmitPacket(at, OpSetContextReg, {0x1b1, 0}); + at = EmitPacket(at, OpSetContextReg, {0x1c3, 4}); + at[0] = 0xc00a1000u; + at[1] = 0; return 0; } -int APS5_VABI sceGnmSetPsShader350(void) { - NotImplemented_nid_no_patch(__func__); +int APS5_VABI sceGnmSetPsShader350(std::uint32_t* cmdbuf, std::uint32_t size, const std::uint32_t* regs) { + if (!cmdbuf || size <= 0x27) return CommandFailed; + if (!regs) { + auto* at = EmitPacket(cmdbuf, OpSetShReg, {8, 0, 0}); + at = EmitPacket(at, OpSetContextReg, {0x203, 0}); + at = EmitPacket(at, OpSetContextReg, {0x8f, 0xf}); + Nop(at, 0x1d); + return 0; + } + if (regs[1] != 0) return CommandFailed; + auto* at = EmitPacket(cmdbuf, OpSetShReg, {8, regs[0], 0}); + at = EmitPacket(at, OpSetShReg, {10, regs[2], regs[3]}); + at = EmitPacket(at, OpSetContextReg, {0x1c4, regs[4], regs[5]}); + at = EmitPacket(at, OpSetContextReg, {0x1b3, regs[6], regs[7]}); + at = EmitPacket(at, OpSetContextReg, {0x1b6, regs[8]}); + at = EmitPacket(at, OpSetContextReg, {0x1b8, regs[9]}); + at = EmitPacket(at, OpSetContextReg, {0x203, regs[10]}); + at = EmitPacket(at, OpSetContextReg, {0x8f, regs[11]}); + Nop(at, 11); return 0; } @@ -659,28 +731,64 @@ int APS5_VABI sceGnmUnregisterResource(void) { return Failure; } -int APS5_VABI sceGnmUpdateGsShader(void) { - NotImplemented_nid_no_patch(__func__); +int APS5_VABI sceGnmUpdateGsShader(std::uint32_t* cmdbuf, std::uint32_t size, const std::uint32_t* regs) { + if (!cmdbuf || size < 0x1d || !regs || regs[1] != 0) return CommandFailed; + auto* at = EmitPacket(cmdbuf, OpSetShReg, {0x88, regs[0], 0}); + at = EmitPacket(at, OpSetShReg, {0x8a, regs[2], regs[3]}); + at = EmitPacket(at, OpNop, {0xc01e02e5u, regs[4]}); + at = EmitPacket(at, OpNop, {0xc01e029bu, regs[5]}); + at = EmitPacket(at, OpNop, {0xc01e02e4u, regs[6]}); + Nop(at, 11); return 0; } -int APS5_VABI sceGnmUpdateHsShader(void) { - NotImplemented_nid_no_patch(__func__); +int APS5_VABI sceGnmUpdateHsShader(std::uint32_t* cmdbuf, std::uint32_t size, const std::uint32_t* regs, std::uint32_t lsHsConfig) { + if (!cmdbuf || size <= 0x1c || !regs || regs[1] != 0) return CommandFailed; + auto* at = EmitPacket(cmdbuf, OpSetShReg, {0x108, regs[0], 0}); + at = EmitPacket(at, OpSetShReg, {0x10a, regs[2], regs[3]}); + at = EmitPacket(at, OpNop, {0xc01e0286u, regs[5], regs[6]}); + at = EmitPacket(at, OpNop, {0xc01e02dbu, regs[4]}); + at = EmitPacket(at, OpNop, {0xc01e02d6u, lsHsConfig}); + Nop(at, 11); return 0; } -int APS5_VABI sceGnmUpdatePsShader(void) { - NotImplemented_nid_no_patch(__func__); +int APS5_VABI sceGnmUpdatePsShader(std::uint32_t* cmdbuf, std::uint32_t size, const std::uint32_t* regs) { + if (!cmdbuf || size <= 0x27) return CommandFailed; + if (!regs) { + auto* at = EmitPacket(cmdbuf, OpSetShReg, {8, 0, 0}); + at = EmitPacket(at, OpNop, {0xc01e0203u, 0}); + Nop(at, 0x20); + return 0; + } + if (regs[1] != 0) return CommandFailed; + Nop(UpdatePsRegisters(cmdbuf, regs), 11); return 0; } -int APS5_VABI sceGnmUpdatePsShader350(void) { - NotImplemented_nid_no_patch(__func__); +int APS5_VABI sceGnmUpdatePsShader350(std::uint32_t* cmdbuf, std::uint32_t size, const std::uint32_t* regs) { + if (!cmdbuf || size <= 0x27) return CommandFailed; + if (!regs) { + auto* at = EmitPacket(cmdbuf, OpSetShReg, {8, 0, 0}); + at = EmitPacket(at, OpNop, {0xc01e0203u, 0}); + at = EmitPacket(at, OpSetContextReg, {0x8f, 0xf}); + Nop(at, 0x1d); + return 0; + } + if (regs[1] != 0) return CommandFailed; + Nop(UpdatePsRegisters(cmdbuf, regs), 11); return 0; } -int APS5_VABI sceGnmUpdateVsShader(void) { - NotImplemented_nid_no_patch(__func__); +int APS5_VABI sceGnmUpdateVsShader(std::uint32_t* cmdbuf, std::uint32_t size, const std::uint32_t* regs, std::uint32_t modifier) { + if (!cmdbuf || size <= 0x1c || !regs || (modifier & 0xfcfffc3fu) != 0 || regs[1] != 0) return CommandFailed; + const std::uint32_t rsrc1 = modifier == 0 ? regs[2] : (regs[2] & 0xfcfffc3fu) | modifier; + auto* at = EmitPacket(cmdbuf, OpSetShReg, {0x48, regs[0], 0}); + at = EmitPacket(at, OpSetShReg, {0x4a, rsrc1, regs[3]}); + at = EmitPacket(at, OpNop, {0xc01e0207u, regs[6]}); + at = EmitPacket(at, OpNop, {0xc01e01b1u, regs[4]}); + at = EmitPacket(at, OpNop, {0xc01e01c3u, regs[5]}); + Nop(at, 11); return 0; } diff --git a/core/libs/prx/libSceGnmDriverForNeoMode/include/EmbeddedShaders.hpp b/core/libs/prx/libSceGnmDriverForNeoMode/include/EmbeddedShaders.hpp new file mode 100644 index 0000000000..621e98ef3d --- /dev/null +++ b/core/libs/prx/libSceGnmDriverForNeoMode/include/EmbeddedShaders.hpp @@ -0,0 +1,27 @@ +// SPDX-FileCopyrightText: Copyright 2024-2026 shadPS4 Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#ifndef CORE_LIBS_PRX_LIBSCEGNMDRIVERFORNEOMODE_INCLUDE_EMBEDDEDSHADERS_HPP +#define CORE_LIBS_PRX_LIBSCEGNMDRIVERFORNEOMODE_INCLUDE_EMBEDDEDSHADERS_HPP + +#include +#include + +namespace GnmNeo { + +alignas(256) constexpr std::array FullscreenVs{ + 0xbeeb0387u, 0x36020081u, 0x34020281u, 0x360000c2u, 0x4a0202c1u, 0x4a0000c1u, 0x7e020b01u, 0x7e000b00u, + 0x7e040280u, 0x7e0602f2u, 0xf80008cfu, 0x03020001u, 0xf800020fu, 0x03030303u, 0xbf810000u, +}; + +alignas(256) constexpr std::array BlackPs{ + 0xbeeb0383u, 0x7e000280u, 0x5e000100u, 0xbf800000u, 0xf8001c0fu, 0x00000000u, 0xbf810000u, +}; + +alignas(256) constexpr std::array DepthOnlyPs{ + 0xbeeb0383u, 0x7e040280u, 0xf8001803u, 0x00000202u, 0xbf810000u, +}; + +} // namespace GnmNeo + +#endif diff --git a/core/libs/prx/libSceHttp/Export.cpp b/core/libs/prx/libSceHttp/Export.cpp index e54397a282..1b49b9b016 100644 --- a/core/libs/prx/libSceHttp/Export.cpp +++ b/core/libs/prx/libSceHttp/Export.cpp @@ -138,6 +138,7 @@ struct HttpEpoll { static std::mutex g_stateMutex; static std::unordered_set g_contexts; +static std::unordered_map g_contextPoolSizes; static std::unordered_map> g_epolls; static std::shared_ptr FindEpoll(HttpEpollHandle eh) { @@ -184,6 +185,16 @@ int APS5_VABI sceHttpCookieFlush(int httpCtxId) { return 0; } +int APS5_VABI sceHttpGetMemoryPoolStats(int httpCtxId, HttpMemoryPoolStats* currentStat) { + if (!currentStat) APS5_INVALID_ARG_EX; + std::lock_guard lock(g_stateMutex); + if (g_contexts.empty()) return ERROR_BEFORE_INIT; + const auto pool = g_contextPoolSizes.find(httpCtxId); + if (pool == g_contextPoolSizes.end()) return ERROR_INVALID_ID; + *currentStat = {static_cast(pool->second), 0, 0}; + return 0; +} + int APS5_VABI sceHttpCreateConnection(int tmpl_id, const char* server_name, const char* scheme, uint16_t port, int enable_keep_alive) { (void)server_name; (void)scheme; @@ -295,9 +306,9 @@ int APS5_VABI sceHttpGetStatusCode(int request_id, int* status_code) { int APS5_VABI sceHttpInit_nid_postfix(int memid, int ssl_ctx_id, uint64_t pool_size) { (void)memid; (void)ssl_ctx_id; - (void)pool_size; const int id = g_nextHandle.fetch_add(1, std::memory_order_relaxed); std::lock_guard lock(g_stateMutex); + g_contextPoolSizes[id] = pool_size; g_contexts.insert(id); AddHttpContext(id); return id; @@ -420,6 +431,7 @@ int APS5_VABI sceHttpTerm_nid_postfix(int http_ctx_id) { std::vector> epolls; { std::lock_guard lock(g_stateMutex); + g_contextPoolSizes.erase(http_ctx_id); if (g_contexts.empty()) return ERROR_BEFORE_INIT; if (g_contexts.erase(http_ctx_id) == 0) return ERROR_INVALID_ID; RemoveHttpContext(http_ctx_id); @@ -689,11 +701,6 @@ int APS5_VABI sceHttpDbgShowStat(void) { return 0; } -int APS5_VABI sceHttpGetMemoryPoolStats(int contextId, HttpMemoryPoolStats* stats) { - NotImplemented_nid_no_patch(__func__); - return 0; -} - int APS5_VABI sceHttpDbgShowConnectionStat(void) { NotImplemented_nid_no_patch(__func__); return 0; diff --git a/core/libs/prx/libSceJson2/Export.cpp b/core/libs/prx/libSceJson2/Export.cpp index 529fee64d0..9155bbed45 100644 --- a/core/libs/prx/libSceJson2/Export.cpp +++ b/core/libs/prx/libSceJson2/Export.cpp @@ -14,6 +14,7 @@ #include #include #include +#include #include "SceTypes.hpp" #include "prx/libc/include/General.hpp" @@ -725,6 +726,34 @@ String* APS5_VABI _ZNK3sce4Json6String6substrEmm(String* result, const String* s } std::size_t APS5_VABI _ZNK3sce4Json6String4findEcm(const String* self, char character, std::size_t position) { return self->text->find(character, position); } std::size_t APS5_VABI _ZNK3sce4Json6String5rfindEcm(const String* self, char character, std::size_t position) { return self->text->rfind(character, position); } +char APS5_VABI _ZNK3sce4Json6String2atEm(const String* self, std::size_t position) { + if (position >= self->text->size()) throw std::out_of_range("sce::Json::String::at: position past the end"); + return (*self->text)[position]; +} +std::size_t APS5_VABI _ZNK3sce4Json6String4findEPKcm(const String* self, const char* text, std::size_t position) { + if (text == nullptr) throw std::invalid_argument("sce::Json::String::find: null text"); + return self->text->find(text, position); +} +std::size_t APS5_VABI _ZNK3sce4Json6String4findEPKcmm(const String* self, const char* text, std::size_t position, std::size_t length) { + if (text == nullptr && length > 0) throw std::invalid_argument("sce::Json::String::find: null text"); + return self->text->find(std::string_view(text == nullptr ? "" : text, length), position); +} +std::size_t APS5_VABI _ZNK3sce4Json6String4findERKS1_m(const String* self, const String* other, std::size_t position) { + if (other == nullptr || other->text == nullptr) throw std::invalid_argument("sce::Json::String::find: null other"); + return self->text->find(*other->text, position); +} +std::size_t APS5_VABI _ZNK3sce4Json6String5rfindEPKcm(const String* self, const char* text, std::size_t position) { + if (text == nullptr) throw std::invalid_argument("sce::Json::String::rfind: null text"); + return self->text->rfind(text, position); +} +std::size_t APS5_VABI _ZNK3sce4Json6String5rfindEPKcmm(const String* self, const char* text, std::size_t position, std::size_t length) { + if (text == nullptr && length > 0) throw std::invalid_argument("sce::Json::String::rfind: null text"); + return self->text->rfind(std::string_view(text == nullptr ? "" : text, length), position); +} +std::size_t APS5_VABI _ZNK3sce4Json6String5rfindERKS1_m(const String* self, const String* other, std::size_t position) { + if (other == nullptr || other->text == nullptr) throw std::invalid_argument("sce::Json::String::rfind: null other"); + return self->text->rfind(*other->text, position); +} int APS5_VABI _ZNK3sce4Json6String7compareEPKc(const String* self, const char* text) { if (text == nullptr) throw std::invalid_argument("sce::Json::String::compare: null text"); return CompareText(self->text->c_str(), text); diff --git a/core/libs/prx/libSceJson2/InventoryExports.cpp b/core/libs/prx/libSceJson2/InventoryExports.cpp index 51220c9a00..c509808eb3 100644 --- a/core/libs/prx/libSceJson2/InventoryExports.cpp +++ b/core/libs/prx/libSceJson2/InventoryExports.cpp @@ -401,46 +401,4 @@ int APS5_VABI sceJson2Unknown_067_nid_no_patch() { } APS5_EXPORT("_ZNK3sce4Json5ValueixERKNS0_6StringE", sceJson2Unknown_067_nid_no_patch); -int APS5_VABI sceJson2Unknown_068_nid_no_patch() { - NotImplemented_nid_no_patch(__func__); - return 0; -} -APS5_EXPORT("_ZNK3sce4Json6String2atEm", sceJson2Unknown_068_nid_no_patch); - -int APS5_VABI sceJson2Unknown_069_nid_no_patch() { - NotImplemented_nid_no_patch(__func__); - return 0; -} -APS5_EXPORT("_ZNK3sce4Json6String4findEPKcm", sceJson2Unknown_069_nid_no_patch); - -int APS5_VABI sceJson2Unknown_070_nid_no_patch() { - NotImplemented_nid_no_patch(__func__); - return 0; -} -APS5_EXPORT("_ZNK3sce4Json6String4findEPKcmm", sceJson2Unknown_070_nid_no_patch); - -int APS5_VABI sceJson2Unknown_071_nid_no_patch() { - NotImplemented_nid_no_patch(__func__); - return 0; -} -APS5_EXPORT("_ZNK3sce4Json6String4findERKS1_m", sceJson2Unknown_071_nid_no_patch); - -int APS5_VABI sceJson2Unknown_072_nid_no_patch() { - NotImplemented_nid_no_patch(__func__); - return 0; -} -APS5_EXPORT("_ZNK3sce4Json6String5rfindEPKcm", sceJson2Unknown_072_nid_no_patch); - -int APS5_VABI sceJson2Unknown_073_nid_no_patch() { - NotImplemented_nid_no_patch(__func__); - return 0; -} -APS5_EXPORT("_ZNK3sce4Json6String5rfindEPKcmm", sceJson2Unknown_073_nid_no_patch); - -int APS5_VABI sceJson2Unknown_074_nid_no_patch() { - NotImplemented_nid_no_patch(__func__); - return 0; -} -APS5_EXPORT("_ZNK3sce4Json6String5rfindERKS1_m", sceJson2Unknown_074_nid_no_patch); - } diff --git a/core/libs/prx/libSceLibcInternal/CMakeLists.txt b/core/libs/prx/libSceLibcInternal/CMakeLists.txt index 1297f6abcf..8087b230e5 100644 --- a/core/libs/prx/libSceLibcInternal/CMakeLists.txt +++ b/core/libs/prx/libSceLibcInternal/CMakeLists.txt @@ -3,10 +3,24 @@ target_include_directories(libSceLibcInternal PRIVATE ${LIBS_INCLUDE_DIR}) set_source_files_properties(UserContext.cpp PROPERTIES COMPILE_OPTIONS -fno-omit-frame-pointer) target_link_libraries(libSceLibcInternal PRIVATE ${CMAKE_DL_LIBS}) target_sources(libSceLibcInternal PRIVATE Directory.cpp) +target_sources(libSceLibcInternal PRIVATE InitOnce.cpp) +if(BUILD_TESTING) + add_test_executable(guest_init_once_tests ${LIBS_INCLUDE_DIR}/tests/GuestInitOnce.cpp) + target_include_directories(guest_init_once_tests PRIVATE ${LIBS_INCLUDE_DIR}) + target_link_libraries(guest_init_once_tests PRIVATE libSceLibcInternal libkernel libc) + configure_windows_unwind(guest_init_once_tests) + add_test(NAME guest_init_once COMMAND guest_init_once_tests) + set_tests_properties(guest_init_once PROPERTIES TIMEOUT 20) + if(WIN32) + set_tests_properties(guest_init_once PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + endif() +endif() target_sources(libSceLibcInternal PRIVATE Err.cpp) configure_windows_unwind(libSceLibcInternal) target_sources(libSceLibcInternal PRIVATE FilesystemStatistics.cpp PathStatistics.cpp) target_sources(libSceLibcInternal PRIVATE Inet.cpp) +target_sources(libSceLibcInternal PRIVATE Filesystem.cpp) if(BUILD_TESTING) add_test_executable(guest_statvfs_tests ${LIBS_INCLUDE_DIR}/tests/GuestStatvfs.cpp) @@ -149,3 +163,15 @@ if(BUILD_TESTING) "PATH=path_list_prepend:$") endif() endif() + +if(BUILD_TESTING) + add_test_executable(guest_libc_filesystem_tests ${LIBS_INCLUDE_DIR}/tests/GuestLibcFilesystem.cpp) + target_include_directories(guest_libc_filesystem_tests PRIVATE ${LIBS_INCLUDE_DIR}) + target_link_libraries(guest_libc_filesystem_tests PRIVATE libSceLibcInternal libkernel libc) + configure_windows_unwind(guest_libc_filesystem_tests) + add_test(NAME guest_libc_filesystem COMMAND guest_libc_filesystem_tests) + if(WIN32) + set_tests_properties(guest_libc_filesystem PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + endif() +endif() diff --git a/core/libs/prx/libSceLibcInternal/Export.cpp b/core/libs/prx/libSceLibcInternal/Export.cpp index b8c3288715..d7c86c15f1 100644 --- a/core/libs/prx/libSceLibcInternal/Export.cpp +++ b/core/libs/prx/libSceLibcInternal/Export.cpp @@ -67,7 +67,7 @@ void APS5_VABI RunThreadDestructors_nid_no_patch() { bool FindDsoModule(const void* dsoSymbol, KernelModule& handle) { ModuleInfoEx info{}; info.st_size = sizeof(ModuleInfoEx); - if (sceKernelGetModuleInfoFromAddr(reinterpret_cast(dsoSymbol), 2, &info) != 0) { + if (sceKernelGetModuleInfoFromAddr(reinterpret_cast(dsoSymbol), 1, &info) != 0) { handle = 0; return false; } diff --git a/core/libs/prx/libSceLibcInternal/Filesystem.cpp b/core/libs/prx/libSceLibcInternal/Filesystem.cpp new file mode 100644 index 0000000000..55347ce843 --- /dev/null +++ b/core/libs/prx/libSceLibcInternal/Filesystem.cpp @@ -0,0 +1,194 @@ +#include "prx/libc/include/GuestDirectory.hpp" +#include "prx/libc/include/General.hpp" +#include "SceTypes.hpp" +#include +#include +#include + +extern "C" { +int APS5_VABI stat_nid_postfix(const char* path, FileStat* sb); +int APS5_VABI mkdir_nid_postfix(const char* path, std::uint16_t mode); +int APS5_VABI rmdir_nid_postfix(const char* path); +int APS5_VABI unlink_nid_postfix(const char* path); +int APS5_VABI chmod_nid_postfix(const char* path, int mode); +char* APS5_VABI getcwd_nid_postfix(char* buffer, std::size_t size); +void* APS5_VABI opendir_nid_postfix(const char* path); +int APS5_VABI readdir_r_nid_postfix(void* handle, GuestDirectoryEntry* entry, GuestDirectoryEntry** result); +int APS5_VABI closedir_nid_postfix(void* handle); +int* APS5_VABI __error_nid_postfix(); +} + +namespace { + +constexpr std::size_t NameCapacity = 260; +constexpr std::size_t CurrentCapacity = 256; +constexpr int NoSuchFile = 2; +constexpr int Exists = 17; +constexpr int NotDirectory = 20; +constexpr int Invalid = 22; +constexpr int NameTooLong = 63; +constexpr std::uint16_t DefaultDirectoryMode = 0777; +constexpr std::uint16_t PermissionBits = 07777; + +enum FileType : int { + TypeNotFound = -1, + TypeNone = 0, + TypeRegular = 1, + TypeDirectory = 2, + TypeSymlink = 3, + TypeBlock = 4, + TypeCharacter = 5, + TypeFifo = 6, + TypeSocket = 7, + TypeUnknown = 8, +}; + +int& GuestErrno() { + return *__error_nid_postfix(); +} + +int TypeOfMode(std::uint16_t mode) { + switch (mode & 0170000) { + case 0010000: return TypeFifo; + case 0020000: return TypeCharacter; + case 0040000: return TypeDirectory; + case 0060000: return TypeBlock; + case 0100000: return TypeRegular; + case 0120000: return TypeSymlink; + case 0140000: return TypeSocket; + default: return TypeUnknown; + } +} + +int StatType(const char* path, int* perms, int* error) { + GuestErrno() = 0; + FileStat status{}; + int type; + if (stat_nid_postfix(path, &status) == 0) { + GuestErrno() = 0; + if (perms) *perms = status.st_mode & PermissionBits; + type = TypeOfMode(status.st_mode); + } else { + const int code = GuestErrno(); + type = code == NoSuchFile || code == NotDirectory || code == Invalid || code == NameTooLong ? TypeNotFound : TypeNone; + } + if (error) *error = GuestErrno(); + return type; +} + +void CopyName(char* target, const char* source) { + std::size_t length = 0; + while (length < NameCapacity - 1 && source[length] != '\0') { + target[length] = source[length]; + ++length; + } + target[length] = '\0'; +} + +bool IsDotEntry(const char* name) { + return name[0] == '.' && (name[1] == '\0' || (name[1] == '.' && name[2] == '\0')); +} + +} + +extern "C" { + +int APS5_VABI _ZNSt10filesystem5_StatEPKcPNS_5permsEPi_nid_postfix(const char* path, int* perms, int* error) { + return StatType(path, perms, error); +} + +int APS5_VABI _ZNSt10filesystem6_LstatEPKcPNS_5permsEPi_nid_postfix(const char* path, int* perms, int* error) { + return StatType(path, perms, error); +} + +std::uint64_t APS5_VABI _ZNSt10filesystem10_File_sizeEPKcPi_nid_postfix(const char* path, int* error) { + FileStat status{}; + if (stat_nid_postfix(path, &status) != 0) { + if (error) *error = GuestErrno(); + return UINT64_MAX; + } + return static_cast(status.st_size); +} + +int APS5_VABI _ZNSt10filesystem16_Last_write_timeEPKcPlS2__nid_postfix(const char* path, std::int64_t* seconds, std::int64_t* nanoseconds) { + FileStat status{}; + if (stat_nid_postfix(path, &status) != 0) return GuestErrno(); + *seconds = status.st_mtim.tv_sec; + *nanoseconds = status.st_mtim.tv_nsec; + return 0; +} + +int APS5_VABI _ZNSt10filesystem9_Make_dirEPKcS1__nid_postfix(const char* path, const char* existing) { + GuestErrno() = 0; + std::uint16_t mode = DefaultDirectoryMode; + if (existing) { + FileStat status{}; + if (stat_nid_postfix(existing, &status) != 0) return -GuestErrno(); + mode = status.st_mode; + } + if (mkdir_nid_postfix(path, mode) == 0) return 1; + if (GuestErrno() == Exists) return 0; + return -GuestErrno(); +} + +bool APS5_VABI _ZNSt10filesystem11_Remove_dirEPKc_nid_postfix(const char* path) { + return rmdir_nid_postfix(path) == 0; +} + +int APS5_VABI _ZNSt10filesystem7_UnlinkEPKc_nid_postfix(const char* path) { + GuestErrno() = 0; + return unlink_nid_postfix(path) == 0 ? 0 : GuestErrno(); +} + +int APS5_VABI _ZNSt10filesystem6_ChmodEPKcNS_5permsE_nid_postfix(const char* path, int perms) { + return chmod_nid_postfix(path, static_cast(perms)); +} + +bool APS5_VABI _ZNSt10filesystem12_Current_getERA260_c_nid_postfix(char* target) { + char current[CurrentCapacity]; + const bool found = getcwd_nid_postfix(current, sizeof(current)) != nullptr; + if (found) CopyName(target, current); + else target[0] = '\0'; + return found; +} + +char* APS5_VABI _ZNSt10filesystem9_Read_dirERA260_cPvRNS_9file_typeE_nid_postfix(char* target, void* handle, int* type) { + *type = TypeUnknown; + GuestDirectoryEntry entry{}; + GuestDirectoryEntry* result = nullptr; + while (readdir_r_nid_postfix(handle, &entry, &result) == 0 && result) { + if (IsDotEntry(entry.name)) continue; + CopyName(target, entry.name); + return target; + } + target[0] = '\0'; + return target; +} + +void* APS5_VABI _ZNSt10filesystem9_Open_dirERA260_cPKcRiRNS_9file_typeE_nid_postfix(char* target, const char* path, int* error, int* type) { + GuestErrno() = 0; + void* handle = opendir_nid_postfix(path); + if (!handle) { + *error = GuestErrno(); + return nullptr; + } + *error = 0; + _ZNSt10filesystem9_Read_dirERA260_cPvRNS_9file_typeE_nid_postfix(target, handle, type); + if (target[0] != '\0') return handle; + closedir_nid_postfix(handle); + return nullptr; +} + +int APS5_VABI _ZNSt10filesystem10_Close_dirEPv_nid_postfix(void* handle) { + return closedir_nid_postfix(handle); +} + +void APS5_VABI _ZNSt10filesystem18_Xfilesystem_errorEPKcSt10error_code_nid_postfix() { + NotImplemented_nid_no_patch(__func__); +} + +void APS5_VABI _ZNSt10filesystem18_Xfilesystem_errorEPKcRKNS_4pathESt10error_code_nid_postfix() { + NotImplemented_nid_no_patch(__func__); +} + +} diff --git a/core/libs/prx/libSceLibcInternal/InitOnce.cpp b/core/libs/prx/libSceLibcInternal/InitOnce.cpp new file mode 100644 index 0000000000..56f54923a0 --- /dev/null +++ b/core/libs/prx/libSceLibcInternal/InitOnce.cpp @@ -0,0 +1,58 @@ +#include "SceTypes.hpp" +#include "prx/libc/include/general/VabiMacros.hpp" +#include +#include +#include +#include + +extern "C" int APS5_VABI sceKernelSyncOnAddressWait(std::uint32_t* address, std::uint32_t expected, const KernelUseconds* timeout, const char* name); +extern "C" int APS5_VABI sceKernelSyncOnAddressWake(void* address, std::int32_t count); + +namespace { + +constexpr std::uintptr_t OnceIdle = 0; +constexpr std::uintptr_t OnceRunning = 1; +constexpr std::uintptr_t OnceDone = 2; +constexpr unsigned long InitFailed = 4; + +std::atomic& OnceState(void** once) { + return *reinterpret_cast*>(once); +} + +} + +extern "C" { + +int APS5_VABI __std_init_once_begin_initialize_clr_nid_postfix(void** once, unsigned long flags, int* pending, void** context) { + (void)flags; + (void)context; + auto& state = OnceState(once); + for (;;) { + std::uintptr_t current = state.load(std::memory_order_acquire); + if (current == OnceIdle) { + if (state.compare_exchange_weak(current, OnceRunning, std::memory_order_acq_rel)) { + *pending = 1; + return 1; + } + continue; + } + if (current == OnceDone) { + *pending = 0; + return 1; + } + sceKernelSyncOnAddressWait(reinterpret_cast(once), static_cast(OnceRunning), nullptr, nullptr); + } +} + +int APS5_VABI __std_init_once_complete_clr_nid_postfix(void** once, unsigned long flags, void* context) { + (void)context; + OnceState(once).store(flags == InitFailed ? OnceIdle : OnceDone, std::memory_order_release); + sceKernelSyncOnAddressWake(once, INT_MAX); + return 1; +} + +[[noreturn]] void APS5_VABI __std_init_once_link_alternate_names_and_abort_nid_postfix() { + std::abort(); +} + +} diff --git a/core/libs/prx/libSceLibreSsl/CMakeLists.txt b/core/libs/prx/libSceLibreSsl/CMakeLists.txt index ad5ccb6a95..ea084db6e0 100644 --- a/core/libs/prx/libSceLibreSsl/CMakeLists.txt +++ b/core/libs/prx/libSceLibreSsl/CMakeLists.txt @@ -1,4 +1,4 @@ -add_library(libSceLibreSsl SHARED EXCLUDE_FROM_ALL ${CMAKE_CURRENT_SOURCE_DIR}/Export.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Digest.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Runtime.cpp) +add_library(libSceLibreSsl SHARED EXCLUDE_FROM_ALL ${CMAKE_CURRENT_SOURCE_DIR}/Export.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Digest.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Runtime.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Aes.cpp) target_include_directories(libSceLibreSsl PRIVATE ${LIBS_INCLUDE_DIR}) target_link_libraries(libSceLibreSsl PRIVATE mbedcrypto) configure_windows_unwind(libSceLibreSsl) @@ -47,4 +47,15 @@ if(BUILD_TESTING) set_tests_properties(guest_libressl_runtime PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$") endif() + + add_test_executable(guest_libressl_aes_tests ${LIBS_INCLUDE_DIR}/tests/GuestLibreSslAes.cpp) + target_include_directories(guest_libressl_aes_tests PRIVATE ${LIBS_INCLUDE_DIR}) + target_link_libraries(guest_libressl_aes_tests PRIVATE libSceLibreSsl libc) + configure_windows_unwind(guest_libressl_aes_tests) + add_test(NAME guest_libressl_aes COMMAND guest_libressl_aes_tests) + set_tests_properties(guest_libressl_aes PROPERTIES TIMEOUT 30) + if(WIN32) + set_tests_properties(guest_libressl_aes PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + endif() endif() diff --git a/core/libs/prx/libSceLibreSsl/Export.cpp b/core/libs/prx/libSceLibreSsl/Export.cpp index c18fb7ef2c..0de9afd97e 100644 --- a/core/libs/prx/libSceLibreSsl/Export.cpp +++ b/core/libs/prx/libSceLibreSsl/Export.cpp @@ -18,46 +18,6 @@ constexpr std::size_t DesScheduleBytes = 128; extern "C" { APS5_DUMMY_FUN -int APS5_VABI AES_cbc_encrypt_nid_postfix(void) { - NotImplemented_nid_no_patch(__func__); - return 0; -} - -int APS5_VABI AES_cfb128_encrypt_nid_postfix(void) { - NotImplemented_nid_no_patch(__func__); - return 0; -} - -int APS5_VABI AES_ctr128_encrypt_nid_postfix(void) { - NotImplemented_nid_no_patch(__func__); - return 0; -} - -int APS5_VABI AES_decrypt_nid_postfix(void) { - NotImplemented_nid_no_patch(__func__); - return 0; -} - -int APS5_VABI AES_ecb_encrypt_nid_postfix(void) { - NotImplemented_nid_no_patch(__func__); - return 0; -} - -int APS5_VABI AES_encrypt_nid_postfix(void) { - NotImplemented_nid_no_patch(__func__); - return 0; -} - -int APS5_VABI AES_set_decrypt_key_nid_postfix(void) { - NotImplemented_nid_no_patch(__func__); - return 0; -} - -int APS5_VABI AES_set_encrypt_key_nid_postfix(void) { - NotImplemented_nid_no_patch(__func__); - return 0; -} - int APS5_VABI ASN1_INTEGER_get_nid_postfix(void) { NotImplemented_nid_no_patch(__func__); return 0; diff --git a/core/libs/prx/libSceLibreSsl/src/Aes.cpp b/core/libs/prx/libSceLibreSsl/src/Aes.cpp new file mode 100644 index 0000000000..3009da6ec1 --- /dev/null +++ b/core/libs/prx/libSceLibreSsl/src/Aes.cpp @@ -0,0 +1,291 @@ +#include +#include +#include +#include +#include +#include +#include +#include "prx/libc/include/General.hpp" + +namespace { +constexpr int MaxRounds = 14; +constexpr std::size_t BlockSize = 16; + +struct AesKey { + std::uint32_t roundKeys[4 * (MaxRounds + 1)]; + std::int32_t rounds; +}; + +static_assert(sizeof(AesKey) == 244); + +constexpr std::uint8_t Multiply(std::uint8_t a, std::uint8_t b) { + std::uint8_t product = 0; + while (b) { + if (b & 1) product ^= a; + a = static_cast((a << 1) ^ ((a & 0x80) ? 0x1b : 0)); + b >>= 1; + } + return product; +} + +constexpr std::array MakeSubstitution() { + std::array box {}; + for (int value = 0; value < 256; ++value) { + std::uint8_t inverse = 0; + for (int candidate = 1; candidate < 256 && value; ++candidate) + if (Multiply(static_cast(value), static_cast(candidate)) == 1) inverse = static_cast(candidate); + std::uint8_t result = 0x63; + for (int shift = 0; shift < 5; ++shift) + result ^= static_cast((inverse << shift) | (inverse >> ((8 - shift) & 7))); + box[value] = result; + } + return box; +} + +constexpr std::array MakeInverse(const std::array& box) { + std::array inverse {}; + for (int value = 0; value < 256; ++value) inverse[box[value]] = static_cast(value); + return inverse; +} + +constexpr auto Substitution = MakeSubstitution(); +constexpr auto InverseSubstitution = MakeInverse(Substitution); +static_assert(Substitution[0x00] == 0x63 && Substitution[0x53] == 0xed && Substitution[0xff] == 0x16); + +std::uint32_t LoadBig32(const unsigned char* p) { + return std::uint32_t(p[0]) << 24 | std::uint32_t(p[1]) << 16 | std::uint32_t(p[2]) << 8 | std::uint32_t(p[3]); +} + +void StoreBig32(unsigned char* p, std::uint32_t value) { + for (int i = 0; i < 4; ++i) p[i] = static_cast(value >> (24 - 8 * i)); +} + +std::uint32_t SubstituteWord(std::uint32_t word) { + return std::uint32_t(Substitution[word >> 24]) << 24 | std::uint32_t(Substitution[(word >> 16) & 0xff]) << 16 | + std::uint32_t(Substitution[(word >> 8) & 0xff]) << 8 | Substitution[word & 0xff]; +} + +std::uint32_t InverseMixWord(std::uint32_t word) { + const std::uint8_t a[4] {std::uint8_t(word >> 24), std::uint8_t(word >> 16), std::uint8_t(word >> 8), std::uint8_t(word)}; + std::uint32_t result = 0; + for (int row = 0; row < 4; ++row) { + const std::uint8_t value = Multiply(a[row], 14) ^ Multiply(a[(row + 1) % 4], 11) ^ Multiply(a[(row + 2) % 4], 13) ^ Multiply(a[(row + 3) % 4], 9); + result |= std::uint32_t(value) << (24 - 8 * row); + } + return result; +} + +int RoundsForBits(int bits) { + if (bits == 128) return 10; + if (bits == 192) return 12; + if (bits == 256) return 14; + return 0; +} + +void ExpandKey(const unsigned char* userKey, int bits, AesKey& key) { + const int words = bits / 32; + const int total = 4 * (key.rounds + 1); + auto* w = key.roundKeys; + for (int i = 0; i < words; ++i) w[i] = LoadBig32(userKey + 4 * i); + std::uint8_t roundConstant = 1; + for (int i = words; i < total; ++i) { + std::uint32_t temp = w[i - 1]; + if (i % words == 0) { + temp = SubstituteWord(temp << 8 | temp >> 24) ^ (std::uint32_t(roundConstant) << 24); + roundConstant = Multiply(roundConstant, 2); + } else if (words > 6 && i % words == 4) { + temp = SubstituteWord(temp); + } + w[i] = w[i - words] ^ temp; + } +} + +const AesKey& RequireKey(const AesKey* key, const char* function) { + if (!key) throw std::invalid_argument(std::string(function) + ": null key"); + if (key->rounds != 10 && key->rounds != 12 && key->rounds != 14) + throw std::invalid_argument(std::string(function) + ": key schedule with " + std::to_string(key->rounds) + " rounds"); + return *key; +} + +void RequireBlocks(const void* in, const void* out, std::size_t length, const char* function) { + if (length && (!in || !out)) throw std::invalid_argument(std::string(function) + ": null input or output"); +} + +void LoadState(std::uint8_t (&state)[16], const unsigned char* in, const std::uint32_t* roundKey) { + for (int column = 0; column < 4; ++column) + for (int row = 0; row < 4; ++row) + state[4 * column + row] = in[4 * column + row] ^ std::uint8_t(roundKey[column] >> (24 - 8 * row)); +} + +void AddRoundKey(std::uint8_t (&state)[16], const std::uint32_t* roundKey) { + for (int column = 0; column < 4; ++column) + for (int row = 0; row < 4; ++row) state[4 * column + row] ^= std::uint8_t(roundKey[column] >> (24 - 8 * row)); +} + +void EncryptBlock(const unsigned char* in, unsigned char* out, const AesKey& key) { + std::uint8_t state[16]; + LoadState(state, in, key.roundKeys); + for (int round = 1; round <= key.rounds; ++round) { + std::uint8_t shifted[16]; + for (int column = 0; column < 4; ++column) + for (int row = 0; row < 4; ++row) shifted[4 * column + row] = Substitution[state[4 * ((column + row) % 4) + row]]; + if (round != key.rounds) { + for (int column = 0; column < 4; ++column) { + const std::uint8_t* a = shifted + 4 * column; + const std::uint8_t mixed[4] { + std::uint8_t(Multiply(a[0], 2) ^ Multiply(a[1], 3) ^ a[2] ^ a[3]), + std::uint8_t(a[0] ^ Multiply(a[1], 2) ^ Multiply(a[2], 3) ^ a[3]), + std::uint8_t(a[0] ^ a[1] ^ Multiply(a[2], 2) ^ Multiply(a[3], 3)), + std::uint8_t(Multiply(a[0], 3) ^ a[1] ^ a[2] ^ Multiply(a[3], 2)), + }; + std::memcpy(state + 4 * column, mixed, 4); + } + } else { + std::memcpy(state, shifted, sizeof(state)); + } + AddRoundKey(state, key.roundKeys + 4 * round); + } + std::memcpy(out, state, sizeof(state)); +} + +void DecryptBlock(const unsigned char* in, unsigned char* out, const AesKey& key) { + std::uint8_t state[16]; + LoadState(state, in, key.roundKeys); + for (int round = 1; round <= key.rounds; ++round) { + std::uint8_t shifted[16]; + for (int column = 0; column < 4; ++column) + for (int row = 0; row < 4; ++row) shifted[4 * column + row] = InverseSubstitution[state[4 * ((column + 4 - row) % 4) + row]]; + if (round != key.rounds) { + for (int column = 0; column < 4; ++column) { + const std::uint8_t* a = shifted + 4 * column; + const std::uint8_t mixed[4] { + std::uint8_t(Multiply(a[0], 14) ^ Multiply(a[1], 11) ^ Multiply(a[2], 13) ^ Multiply(a[3], 9)), + std::uint8_t(Multiply(a[0], 9) ^ Multiply(a[1], 14) ^ Multiply(a[2], 11) ^ Multiply(a[3], 13)), + std::uint8_t(Multiply(a[0], 13) ^ Multiply(a[1], 9) ^ Multiply(a[2], 14) ^ Multiply(a[3], 11)), + std::uint8_t(Multiply(a[0], 11) ^ Multiply(a[1], 13) ^ Multiply(a[2], 9) ^ Multiply(a[3], 14)), + }; + std::memcpy(state + 4 * column, mixed, 4); + } + } else { + std::memcpy(state, shifted, sizeof(state)); + } + AddRoundKey(state, key.roundKeys + 4 * round); + } + std::memcpy(out, state, sizeof(state)); +} + +unsigned RequirePosition(int position, const char* function) { + if (position < 0 || position >= static_cast(BlockSize)) + throw std::invalid_argument(std::string(function) + ": block position " + std::to_string(position) + " is outside 0-15"); + return static_cast(position); +} + +void IncrementCounter(unsigned char* counter) { + for (int i = 15; i >= 0; --i) + if (++counter[i]) return; +} +} + +extern "C" { + +int APS5_VABI AES_set_encrypt_key_nid_postfix(const unsigned char* userKey, int bits, AesKey* key) { + if (!userKey || !key) return -1; + const int rounds = RoundsForBits(bits); + if (!rounds) return -2; + key->rounds = rounds; + ExpandKey(userKey, bits, *key); + return 0; +} + +int APS5_VABI AES_set_decrypt_key_nid_postfix(const unsigned char* userKey, int bits, AesKey* key) { + const int result = AES_set_encrypt_key_nid_postfix(userKey, bits, key); + if (result) return result; + auto* w = key->roundKeys; + for (int i = 0, j = 4 * key->rounds; i < j; i += 4, j -= 4) + for (int k = 0; k < 4; ++k) std::swap(w[i + k], w[j + k]); + for (int i = 4; i < 4 * key->rounds; ++i) w[i] = InverseMixWord(w[i]); + return 0; +} + +void APS5_VABI AES_encrypt_nid_postfix(const unsigned char* in, unsigned char* out, const AesKey* key) { + const auto& k = RequireKey(key, __func__); + RequireBlocks(in, out, BlockSize, __func__); + EncryptBlock(in, out, k); +} + +void APS5_VABI AES_decrypt_nid_postfix(const unsigned char* in, unsigned char* out, const AesKey* key) { + const auto& k = RequireKey(key, __func__); + RequireBlocks(in, out, BlockSize, __func__); + DecryptBlock(in, out, k); +} + +void APS5_VABI AES_ecb_encrypt_nid_postfix(const unsigned char* in, unsigned char* out, const AesKey* key, int encrypt) { + if (encrypt == 1) AES_encrypt_nid_postfix(in, out, key); + else AES_decrypt_nid_postfix(in, out, key); +} + +void APS5_VABI AES_cbc_encrypt_nid_postfix(const unsigned char* in, unsigned char* out, std::size_t length, const AesKey* key, unsigned char* ivec, int encrypt) { + const auto& k = RequireKey(key, __func__); + RequireBlocks(in, out, length, __func__); + if (!ivec) throw std::invalid_argument(std::string(__func__) + ": null iv"); + std::uint8_t block[BlockSize]; + if (encrypt) { + while (length) { + const std::size_t count = length < BlockSize ? length : BlockSize; + for (std::size_t n = 0; n < BlockSize; ++n) block[n] = n < count ? in[n] ^ ivec[n] : ivec[n]; + EncryptBlock(block, out, k); + std::memcpy(ivec, out, BlockSize); + if (length <= BlockSize) break; + length -= BlockSize; + in += BlockSize; + out += BlockSize; + } + return; + } + while (length) { + const std::size_t count = length < BlockSize ? length : BlockSize; + std::uint8_t cipher[BlockSize]; + std::memcpy(cipher, in, BlockSize); + DecryptBlock(cipher, block, k); + for (std::size_t n = 0; n < count; ++n) out[n] = block[n] ^ ivec[n]; + std::memcpy(ivec, cipher, BlockSize); + if (length <= BlockSize) break; + length -= BlockSize; + in += BlockSize; + out += BlockSize; + } +} + +void APS5_VABI AES_cfb128_encrypt_nid_postfix(const unsigned char* in, unsigned char* out, std::size_t length, const AesKey* key, unsigned char* ivec, int* num, int encrypt) { + const auto& k = RequireKey(key, __func__); + RequireBlocks(in, out, length, __func__); + if (!ivec || !num) throw std::invalid_argument(std::string(__func__) + ": null iv or position"); + unsigned n = RequirePosition(*num, __func__); + for (std::size_t i = 0; i < length; ++i) { + if (!n) EncryptBlock(ivec, ivec, k); + const auto c = in[i]; + out[i] = ivec[n] ^ c; + ivec[n] = encrypt ? out[i] : c; + n = (n + 1) % BlockSize; + } + *num = static_cast(n); +} + +void APS5_VABI AES_ctr128_encrypt_nid_postfix(const unsigned char* in, unsigned char* out, std::size_t length, const AesKey* key, unsigned char* ivec, unsigned char* ecountBuffer, unsigned* num) { + const auto& k = RequireKey(key, __func__); + RequireBlocks(in, out, length, __func__); + if (!ivec || !ecountBuffer || !num) throw std::invalid_argument(std::string(__func__) + ": null counter, keystream or position"); + if (*num >= BlockSize) throw std::invalid_argument(std::string(__func__) + ": block position " + std::to_string(*num) + " is outside 0-15"); + unsigned n = *num; + for (std::size_t i = 0; i < length; ++i) { + if (!n) { + EncryptBlock(ivec, ecountBuffer, k); + IncrementCounter(ivec); + } + out[i] = in[i] ^ ecountBuffer[n]; + n = (n + 1) % BlockSize; + } + *num = n; +} + +} diff --git a/core/libs/prx/libSceNet/CMakeLists.txt b/core/libs/prx/libSceNet/CMakeLists.txt index 9ecb525065..dbf0992fae 100644 --- a/core/libs/prx/libSceNet/CMakeLists.txt +++ b/core/libs/prx/libSceNet/CMakeLists.txt @@ -77,15 +77,34 @@ if(BUILD_TESTING) "PATH=path_list_prepend:$") endforeach() endif() + if(WIN32) + add_test_executable(net_empty_stream_tests ${LIBS_INCLUDE_DIR}/tests/NetEmptyStream.cpp) + target_include_directories(net_empty_stream_tests PRIVATE ${LIBS_INCLUDE_DIR}) + target_link_libraries(net_empty_stream_tests PRIVATE libSceNet libkernel libc) + configure_windows_unwind(net_empty_stream_tests) + foreach(api IN ITEMS recv recvfrom recvmsg) + foreach(mode IN ITEMS empty dontwait peek nonblocking queued nullbuf errors datagram) + add_test(NAME net_empty_stream_${api}_${mode} COMMAND net_empty_stream_tests ${api} ${mode}) + set_tests_properties(net_empty_stream_${api}_${mode} PROPERTIES TIMEOUT 20 + ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$") + endforeach() + endforeach() + endif() add_test_executable(guest_sce_net_tests ${LIBS_INCLUDE_DIR}/tests/GuestSceNet.cpp) target_include_directories(guest_sce_net_tests PRIVATE ${LIBS_INCLUDE_DIR}) target_link_libraries(guest_sce_net_tests PRIVATE libSceNet libSceNetCtl libkernel libc) configure_windows_unwind(guest_sce_net_tests) add_test(NAME guest_sce_net COMMAND guest_sce_net_tests) + add_test_executable(guest_net_thread_tests ${LIBS_INCLUDE_DIR}/tests/GuestNetThread.cpp) + target_include_directories(guest_net_thread_tests PRIVATE ${LIBS_INCLUDE_DIR}) + target_link_libraries(guest_net_thread_tests PRIVATE libSceNet libkernel libc) + configure_windows_unwind(guest_net_thread_tests) + add_test(NAME guest_net_thread COMMAND guest_net_thread_tests) + set_tests_properties(guest_net_thread PROPERTIES TIMEOUT 20) set_tests_properties(guest_sce_net PROPERTIES TIMEOUT 20 SKIP_RETURN_CODE 77) if(WIN32) target_link_libraries(guest_sce_net_tests PRIVATE ws2_32) - set_tests_properties(guest_sce_net PROPERTIES ENVIRONMENT_MODIFICATION + set_tests_properties(guest_sce_net guest_net_thread PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$") endif() add_test_executable(net_epoll_lifetime_tests ${LIBS_INCLUDE_DIR}/tests/NetEpollLifetime.cpp) diff --git a/core/libs/prx/libSceNet/Export.cpp b/core/libs/prx/libSceNet/Export.cpp index 29efb3e552..99b804e54e 100644 --- a/core/libs/prx/libSceNet/Export.cpp +++ b/core/libs/prx/libSceNet/Export.cpp @@ -44,6 +44,7 @@ #include "prx/libkernel/Socket/include/SocketPoll.hpp" #include "prx/libkernel/Socket/include/SocketRuntime.hpp" #include "prx/libkernel/Time/include/Time.hpp" +#include "prx/libkernel/Pthread/include/ThreadLifecycle.hpp" #include "src/ResolverTestHooks.hpp" // Guest socket descriptors retain PS5 semantics while their transport is backed by host sockets. @@ -97,7 +98,12 @@ struct NetDataTraffic; struct NetEmulationParameter; struct NetResolverConnection; -struct NetThreadParameter; +struct NetThreadParameter { + void (APS5_VABI *entry)(void*); + void* arg; + std::uint64_t reserved; + std::uint32_t flags; +}; struct NetIpv4InterfaceAddress; struct NetStatisticsInfoInternal; struct NetInitializeParameter; @@ -396,6 +402,7 @@ struct Sock { std::shared_ptr native; bool bound = false; bool listening = false; + bool connectAttempted = false; bool aborted = false; bool read_shut = false; int reported_error = 0; @@ -501,6 +508,15 @@ int fail(int err) { return NET_ERROR_BASE | err; } +extern "C" int APS5_VABI scePthreadCreate(Pthread* thread, const PthreadAttr* attr, PthreadEntry entry, void* arg, const char* name); +extern "C" int APS5_VABI scePthreadJoin(Pthread thread, void** retval); + +void* APS5_VABI RunNetThread(void* param) { + const auto* thread = static_cast(param); + thread->entry(thread->arg); + return nullptr; +} + int set_native_timeout(NativeSocket native, int optname, std::int64_t microseconds) { const int native_option = optname == NET_SO_RCVTIMEO ? SO_RCVTIMEO : SO_SNDTIMEO; #ifdef _WIN32 @@ -734,7 +750,24 @@ int wait_for_datagram(const Sock& socket, std::chrono::steady_clock::time_point } int wait_for_local_address(int s, const Sock& socket, bool empty, int flags) { - if (socket.type == NET_SOCK_STREAM || socket.bound) return 0; + if (socket.type == NET_SOCK_STREAM) { +#ifdef _WIN32 + if (!empty) return 0; + sockaddr_storage peer{}; + NativeLength length = sizeof(peer); + if (::getpeername(socket.native->value, reinterpret_cast(&peer), &length) != 0) { + const int error = native_error(); + return !socket.connectAttempted && !socket.aborted && !socket.read_shut && error == NET_ENOTCONN ? error : 0; + } + WSAPOLLFD state{}; + state.fd = socket.native->value; + state.events = POLLRDNORM; + return WSAPoll(&state, 1, 0) >= 0 && (state.revents & (POLLERR | POLLHUP | POLLNVAL)) == 0 ? RECEIVE_NOTHING : 0; +#else + return 0; +#endif + } + if (socket.bound) return 0; const bool named = has_local_address(socket.native->value); if (!named && (socket.read_shut || empty)) return RECEIVE_NOTHING; if (!named && (socket.native->nonblock || (flags & NET_MSG_DONTWAIT) != 0)) return NET_EAGAIN; @@ -1148,6 +1181,7 @@ int APS5_VABI sceNetAccept(int s, void* addr, uint32_t* addrlen) { Sock connection; connection.family = listener.family; connection.type = NET_SOCK_STREAM; + connection.connectAttempted = true; connection.native = std::move(handle); #ifdef _WIN32 connection.native->receiveTimeout = listener.native->receiveTimeout.load(); @@ -1230,6 +1264,11 @@ int APS5_VABI sceNetConnect(int s, const void* addr, uint32_t addrlen) { if (socket.type == NET_SOCK_STREAM) RefuseClosedLoopbackAtOnce_nid_no_patch(socket.native->value, reinterpret_cast(&native)); #endif if (native.ss_family != (socket.family == NET_AF_INET ? AF_INET : AF_INET6)) return fail(NET_EAFNOSUPPORT); + if (socket.type == NET_SOCK_STREAM) { + std::lock_guard lock(g_mutex); + const auto current = g_socks.find(s); + if (current != g_socks.end() && current->second.native == socket.native) current->second.connectAttempted = true; + } if (::connect(socket.native->value, reinterpret_cast(&native), native_length) == 0) { #if defined(_WIN32) || defined(__APPLE__) note_local_address(s, socket); @@ -1656,7 +1695,9 @@ int APS5_VABI sceNetGetpeername(int s, void* addr, uint32_t* addrlen) { constexpr int NET_SOCKINFO_F_SELF = 0x1; constexpr int NET_SOCKINFO_F_NONBLOCK = 0x200000; +constexpr int NET_SOCKINFO_F_IPV6 = 0x400000; constexpr int NET_SOCKINFO_TIME_WAIT = 0x40; +constexpr int NET_SOCKINFO6_IPV46 = 0x1000; struct NetSockInfo { char name[32]; @@ -1693,6 +1734,44 @@ struct NetSockInfo { static_assert(sizeof(NetSockInfo) == 0xA0 && offsetof(NetSockInfo, s) == 0x24 && offsetof(NetSockInfo, local_adr) == 0x34 && offsetof(NetSockInfo, local_port) == 0x3C && offsetof(NetSockInfo, recv_buffer_size) == 0x64); +struct NetSockInfo6 { + char name[32]; + std::int32_t pid; + std::int32_t s; + std::int8_t socket_type; + std::int8_t policy; + std::int8_t priority; + std::int8_t reserved8; + std::int32_t recv_queue_length; + std::int32_t send_queue_length; + std::uint32_t local_adr; + std::uint32_t remote_adr; + std::uint16_t local_port; + std::uint16_t remote_port; + std::uint16_t local_vport; + std::uint16_t remote_vport; + std::int32_t state; + std::int32_t flags; + std::int32_t tx_bps; + std::int32_t rx_bps; + std::int32_t max_tx_bps; + std::int32_t max_rx_bps; + std::int32_t tx_vbps; + std::int32_t rx_vbps; + std::int32_t recv_buffer_size; + std::int32_t send_buffer_size; + std::uint8_t local_adr6[16]; + std::uint8_t remote_adr6[16]; + std::int32_t tx_drops; + std::int32_t rx_drops; + std::int32_t tx_wait; + std::int32_t reserved[2]; +}; +static_assert(sizeof(NetSockInfo6) == sizeof(NetSockInfo) && offsetof(NetSockInfo6, s) == offsetof(NetSockInfo, s) && + offsetof(NetSockInfo6, local_port) == offsetof(NetSockInfo, local_port) && offsetof(NetSockInfo6, flags) == offsetof(NetSockInfo, flags) && + offsetof(NetSockInfo6, recv_buffer_size) == offsetof(NetSockInfo, recv_buffer_size) && offsetof(NetSockInfo6, local_adr6) == 0x6C && + offsetof(NetSockInfo6, remote_adr6) == 0x7C && offsetof(NetSockInfo6, tx_drops) == offsetof(NetSockInfo, tx_drops)); + static int sock_buffer_size(const Sock& socket, bool send, std::int32_t& value) { value = send ? socket.snd_buffer : socket.rcv_buffer; if (value != 0) return 0; @@ -1766,6 +1845,86 @@ int APS5_VABI sceNetGetSockInfo(int s, void* info, int n, int flags) { return written; } +static int fill_sock_info6(int s, NetSockInfo6& record, bool includeIpv4) { + Sock socket; + { + std::lock_guard lk(g_mutex); + const auto it = g_socks.find(s); + if (it == g_socks.end()) return NET_EBADF; + socket = it->second; + } + const bool ipv6 = socket.family == NET_AF_INET6; + if (!ipv6 && !(includeIpv4 && socket.family == NET_AF_INET)) return NET_EAFNOSUPPORT; + sockaddr_storage local{}; + NativeLength length = sizeof(local); + if (::getsockname(socket.native->value, reinterpret_cast(&local), &length) != 0) { +#ifdef _WIN32 + if (WSAGetLastError() != WSAEINVAL) return native_error(); +#else + return native_error(); +#endif + } + sockaddr_storage remote{}; + length = sizeof(remote); + if (::getpeername(socket.native->value, reinterpret_cast(&remote), &length) != 0) { + const int error = native_error(); + if (error != NET_ENOTCONN) return error; + } + record = {}; + if (const int error = sock_buffer_size(socket, false, record.recv_buffer_size)) return error; + if (const int error = sock_buffer_size(socket, true, record.send_buffer_size)) return error; + record.s = s; + record.socket_type = static_cast(socket.type); + record.flags = NET_SOCKINFO_F_SELF | (socket.native->nonblock ? NET_SOCKINFO_F_NONBLOCK : 0); + if (ipv6) { + const auto& local6 = reinterpret_cast(local); + const auto& remote6 = reinterpret_cast(remote); + std::memcpy(record.local_adr6, &local6.sin6_addr, sizeof(record.local_adr6)); + std::memcpy(record.remote_adr6, &remote6.sin6_addr, sizeof(record.remote_adr6)); + record.local_port = local6.sin6_port; + record.remote_port = remote6.sin6_port; + record.flags |= NET_SOCKINFO_F_IPV6; + } else { + const auto& local4 = reinterpret_cast(local); + const auto& remote4 = reinterpret_cast(remote); + record.local_adr = local4.sin_addr.s_addr; + record.remote_adr = remote4.sin_addr.s_addr; + record.local_port = local4.sin_port; + record.remote_port = remote4.sin_port; + } + return 0; +} + +int APS5_VABI sceNetGetSockInfo6(std::int32_t s, NetSockInfo6* info, std::uint32_t capacity, std::uint32_t flags) { + if ((flags & ~(NET_SOCKINFO_TIME_WAIT | NET_SOCKINFO6_IPV46)) != 0) return fail(NET_EINVAL); + const bool includeIpv4 = (flags & NET_SOCKINFO6_IPV46) != 0; + std::vector listed; + if (s < 0) { + std::lock_guard lk(g_mutex); + for (const auto& [descriptor, socket] : g_socks) { + if (socket.family == NET_AF_INET6 || (includeIpv4 && socket.family == NET_AF_INET)) listed.push_back(descriptor); + } + } + if (!info) return s < 0 ? static_cast(listed.size()) : fail(NET_EINVAL); + if (static_cast(capacity) < 1) return fail(NET_EINVAL); + NetSockInfo6 record; + if (s >= 0) { + if (const int error = fill_sock_info6(s, record, includeIpv4)) return fail(error); + *info = record; + return 1; + } + int written = 0; + for (const int descriptor : listed) { + if (written == static_cast(capacity)) break; + const int error = fill_sock_info6(descriptor, record, includeIpv4); + if (error == NET_EBADF || error == NET_EAFNOSUPPORT) continue; + if (error != 0) return fail(error); + info[written] = record; + ++written; + } + return written; +} + int APS5_VABI sceNetEpollCreate(const char* name, int flags) { (void)name; (void)flags; @@ -2292,7 +2451,6 @@ int APS5_VABI sceNetShowIfconfigWithMemory(std::int32_t poolId) { } int APS5_VABI sceNetShowPolicy(void) { - NotImplemented_nid_no_patch(__func__); return 0; } @@ -2306,7 +2464,8 @@ int APS5_VABI sceNetShowRoute(void) { } int APS5_VABI sceNetShowRouteWithMemory(std::int32_t poolId) { - NotImplemented_nid_no_patch(__func__); + std::lock_guard lk(g_mutex); + if (g_pools.find(poolId) == g_pools.end()) return fail(NET_EBADF); return 0; } @@ -2330,11 +2489,6 @@ int APS5_VABI sceNetResolverConnectDestroy(NetResolverConnection* connection) { return 0; } -int APS5_VABI sceNetGetSockInfo6(std::int32_t socket, NetSockInfo6* info, std::uint32_t capacity, std::uint32_t flags) { - NotImplemented_nid_no_patch(__func__); - return 0; -} - int APS5_VABI sceNetSetDns6Info(const NetDns6Info* info, std::uint32_t flags) { NotImplemented_nid_no_patch(__func__); return 0; @@ -2600,18 +2754,20 @@ int APS5_VABI sceNetSysctl(const int* name, std::uint32_t nameLength, void* oldV } int APS5_VABI sceNetThreadCreate(Pthread* thread, const NetThreadParameter* parameter, const char* name) { - NotImplemented_nid_no_patch(__func__); + if (!parameter) return fail(NET_EINVAL); + Pthread created = nullptr; + const int result = scePthreadCreate(&created, nullptr, RunNetThread, const_cast(parameter), name != nullptr ? name : ""); + if (result != 0) return result; + if (thread) *thread = created; return 0; } -int APS5_VABI sceNetThreadExit(void) { - NotImplemented_nid_no_patch(__func__); - return 0; +void APS5_VABI sceNetThreadExit() { + scePthreadExit(nullptr); } int APS5_VABI sceNetThreadJoin(void* thread) { - NotImplemented_nid_no_patch(__func__); - return 0; + return scePthreadJoin(static_cast(thread), nullptr); } int APS5_VABI sceNetUsleep(int microseconds) { @@ -2638,7 +2794,7 @@ int APS5_VABI sceNetShowNetstatForBuffer(char* buffer, std::int32_t bufferSize, } int APS5_VABI sceNetShowNetstatEx(std::uint32_t flags) { - NotImplemented_nid_no_patch(__func__); + (void)flags; return 0; } @@ -3167,14 +3323,12 @@ int APS5_VABI sceNetEmulationDebugSettingsGet(NetEmulationParameter* parameter, return 0; } -int APS5_VABI sceNetEmulationSet(const NetEmulationParameter* parameter, std::uint32_t flags) { - NotImplemented_nid_no_patch(__func__); - return 0; +int APS5_VABI sceNetEmulationSet(const NetEmulationParameter*, std::uint32_t) { + return fail(NET_EOPNOTSUPP); } -int APS5_VABI sceNetEmulationGet(NetEmulationParameter* parameter, std::uint32_t flags) { - NotImplemented_nid_no_patch(__func__); - return 0; +int APS5_VABI sceNetEmulationGet(NetEmulationParameter*, std::uint32_t) { + return fail(NET_EOPNOTSUPP); } } diff --git a/core/libs/prx/libSceNetCtl/Export.cpp b/core/libs/prx/libSceNetCtl/Export.cpp index 54c9d941fe..c8b0e482f3 100644 --- a/core/libs/prx/libSceNetCtl/Export.cpp +++ b/core/libs/prx/libSceNetCtl/Export.cpp @@ -99,6 +99,7 @@ static std::atomic_bool g_initialized{true}; static std::mutex g_callbackLock; static CallbackSlot g_callbacks[MAX_CALLBACKS] = {}; static CallbackSlot g_callbacksV6[MAX_CALLBACKS] = {}; +static CallbackSlot g_callbacksNpToolkit[MAX_CALLBACKS] = {}; static int RegisterCallback(CallbackSlot (&callbacks)[MAX_CALLBACKS], NetCtlCallback func, void* arg, int* cid) { if (!g_initialized.load()) return SCE_NET_CTL_ERROR_NOT_INITIALIZED; @@ -123,9 +124,9 @@ static int UnregisterCallback(CallbackSlot (&callbacks)[MAX_CALLBACKS], int cid) return 0; } -static bool HasIpv4Callback() { +static bool HasCallback(const CallbackSlot (&callbacks)[MAX_CALLBACKS]) { std::lock_guard lock(g_callbackLock); - for (const auto& slot : g_callbacks) { + for (const auto& slot : callbacks) { if (slot.func) return true; } return false; @@ -512,17 +513,15 @@ static void HostTraffic(std::uint64_t& sent, std::uint64_t& received) { #endif } -extern "C" { - -int APS5_VABI sceNetCtlCheckCallback(void) { +static int CheckCallbacks(CallbackSlot (&callbacks)[MAX_CALLBACKS]) { if (!g_initialized.load()) return SCE_NET_CTL_ERROR_NOT_INITIALIZED; - if (!HasIpv4Callback()) return 0; + if (!HasCallback(callbacks)) return 0; const int event = HostHasAddress(AF_UNSPEC) ? NET_CTL_EVENT_IPOBTAINED : NET_CTL_EVENT_DISCONNECTED; CallbackSlot due[MAX_CALLBACKS]; int count = 0; { std::lock_guard lock(g_callbackLock); - for (auto& slot : g_callbacks) { + for (auto& slot : callbacks) { if (!slot.func || slot.lastEvent == event) continue; slot.lastEvent = event; due[count++] = slot; @@ -532,6 +531,12 @@ int APS5_VABI sceNetCtlCheckCallback(void) { return 0; } +extern "C" { + +int APS5_VABI sceNetCtlCheckCallback(void) { + return CheckCallbacks(g_callbacks); +} + int APS5_VABI sceNetCtlGetIfStat(NetCtlIfStat* stat) { if (!g_initialized.load()) return SCE_NET_CTL_ERROR_NOT_INITIALIZED; if (!stat) return SCE_NET_CTL_ERROR_INVALID_ADDR; @@ -584,7 +589,11 @@ int APS5_VABI sceNetCtlGetNatInfo(NetCtlNatInfo* info) { int APS5_VABI sceNetCtlGetResult(int event, int* result) { if (!g_initialized.load()) return SCE_NET_CTL_ERROR_NOT_INITIALIZED; if (!result) return SCE_NET_CTL_ERROR_INVALID_ADDR; - throw std::logic_error("sceNetCtlGetResult: service event results are not implemented"); + if (event < NET_CTL_EVENT_DISCONNECTED || event > NET_CTL_EVENT_IPOBTAINED) { + throw std::runtime_error("sceNetCtlGetResult: event type " + std::to_string(event) + " is not implemented"); + } + *result = 0; + return 0; } int APS5_VABI sceNetCtlGetState(int* state) { @@ -633,8 +642,7 @@ int APS5_VABI sceNetCtlUnregisterCallbackV6(int cid) { } int APS5_VABI sceNetCtlRegisterCallbackForNpToolkit(NetCtlCallback callback, void* argument, int* callbackId) { - NotImplemented_nid_no_patch(__func__); - return 0; + return RegisterCallback(g_callbacksNpToolkit, callback, argument, callbackId); } int APS5_VABI sceNetCtlRegisterCallbackIpcInt(NetCtlCallback callback, void* argument, int* callbackId) { @@ -648,8 +656,7 @@ int APS5_VABI sceNetCtlRegisterCallbackForLibIpcInt(NetCtlCallback callback, voi } int APS5_VABI sceNetCtlUnregisterCallbackForNpToolkit(int callbackId) { - NotImplemented_nid_no_patch(__func__); - return 0; + return UnregisterCallback(g_callbacksNpToolkit, callbackId); } int APS5_VABI sceNetCtlUnregisterCallbackIpcInt(int callbackId) { @@ -663,8 +670,7 @@ int APS5_VABI sceNetCtlUnregisterCallbackForLibIpcInt(int callbackId) { } int APS5_VABI sceNetCtlCheckCallbackForNpToolkit(void) { - NotImplemented_nid_no_patch(__func__); - return 0; + return CheckCallbacks(g_callbacksNpToolkit); } int APS5_VABI sceNetCtlCheckCallbackForLibIpcInt(void) { diff --git a/core/libs/prx/libSceNetCtlAp/Export.cpp b/core/libs/prx/libSceNetCtlAp/Export.cpp index 9da3953097..cc3d9a2492 100644 --- a/core/libs/prx/libSceNetCtlAp/Export.cpp +++ b/core/libs/prx/libSceNetCtlAp/Export.cpp @@ -77,6 +77,7 @@ int APS5_VABI sceNetCtlApRegisterCallback(NetCtlApCallback func, void* arg, int* return 0; } } + *cid = -1; return NET_CTL_AP_ERROR_CALLBACK_MAX; } diff --git a/core/libs/prx/libSceNgs2.native/include/Ngs2Types.hpp b/core/libs/prx/libSceNgs2.native/include/Ngs2Types.hpp index 54093115a2..eda5e07ef3 100644 --- a/core/libs/prx/libSceNgs2.native/include/Ngs2Types.hpp +++ b/core/libs/prx/libSceNgs2.native/include/Ngs2Types.hpp @@ -43,6 +43,7 @@ static constexpr std::uint32_t SCE_NGS2_RACK_ID_REVERB = 0x2001; static constexpr std::uint32_t SCE_NGS2_RACK_ID_MASTERING = 0x3000; static constexpr std::uint32_t SCE_NGS2_RACK_ID_CUSTOM_SAMPLER = 0x4001; static constexpr std::uint32_t SCE_NGS2_RACK_ID_CUSTOM_SUBMIXER = 0x4002; +static constexpr std::uint32_t SCE_NGS2_RACK_ID_CUSTOM_MASTERING = 0x4003; static constexpr std::uint32_t SCE_NGS2_WAVEFORM_TYPE_PCM_I16L = 0x12; static constexpr std::uint32_t SCE_NGS2_WAVEFORM_TYPE_PCM_I16B = 0x13; @@ -87,6 +88,9 @@ static constexpr std::uint32_t SCE_NGS2_CUSTOM_SAMPLER_VOICE_PARAM_ADD_WAVEFORM_ static constexpr std::uint32_t SCE_NGS2_CUSTOM_SAMPLER_VOICE_PARAM_EXIT_LOOP = 0x40010004; static constexpr std::uint32_t SCE_NGS2_CUSTOM_SAMPLER_VOICE_PARAM_PITCH = 0x40010005; static constexpr std::uint32_t SCE_NGS2_CUSTOM_SUBMIXER_VOICE_PARAM_SETUP = 0x40020000; +static constexpr std::uint32_t SCE_NGS2_CUSTOM_MASTERING_VOICE_PARAM_SETUP = 0x40030000; +static constexpr std::uint32_t SCE_NGS2_CUSTOM_MASTERING_VOICE_PARAM_GAIN = 0x40030004; +static constexpr std::uint32_t SCE_NGS2_CUSTOM_MASTERING_VOICE_PARAM_OUTPUT = 0x40030005; static constexpr std::uint32_t SCE_NGS2_CUSTOM_VOICE_PARAM_FILTER = 0x40001300; static constexpr std::uint32_t SCE_NGS2_CUSTOM_VOICE_PARAM_GAIN = 0x40001500; static constexpr std::uint32_t SCE_NGS2_CUSTOM_VOICE_PARAM_USER_FX = 0x40001900; @@ -360,6 +364,8 @@ struct Ngs2CustomSubmixerRackOption { }; static_assert(sizeof(Ngs2CustomSubmixerRackOption) == 1288); +using Ngs2CustomMasteringRackOption = Ngs2CustomSubmixerRackOption; + struct Ngs2RackInfo { char name[64]; Ngs2Handle rack_handle; diff --git a/core/libs/prx/libSceNgs2.native/src/Custom.cpp b/core/libs/prx/libSceNgs2.native/src/Custom.cpp index e87845360e..5d066d889a 100644 --- a/core/libs/prx/libSceNgs2.native/src/Custom.cpp +++ b/core/libs/prx/libSceNgs2.native/src/Custom.cpp @@ -61,6 +61,9 @@ static void CheckDelayModule(std::uint32_t rackId, const Ngs2CustomRackModuleInf } int Ngs2CheckCustomRack(std::uint32_t rackId, const Ngs2CustomRackOption& option, std::uint32_t maxInputs) { + if (rackId == SCE_NGS2_RACK_ID_CUSTOM_MASTERING && option.num_modules != 0) { + throw std::runtime_error("NGS2: custom mastering racks with modules are not implemented"); + } if (option.num_buffers == 0 || option.num_buffers > SCE_NGS2_CUSTOM_MAX_BUFFERS || option.num_buffers < maxInputs) { return SCE_NGS2_ERROR_INVALID_NUM_BUFFERS; } diff --git a/core/libs/prx/libSceNgs2.native/src/Ngs2Internal.hpp b/core/libs/prx/libSceNgs2.native/src/Ngs2Internal.hpp index ac5ec0321a..73270afab9 100644 --- a/core/libs/prx/libSceNgs2.native/src/Ngs2Internal.hpp +++ b/core/libs/prx/libSceNgs2.native/src/Ngs2Internal.hpp @@ -118,6 +118,7 @@ struct Ngs2Voice { Ngs2PlayState state = Ngs2PlayState::Empty; std::uint32_t stateFlags = 0; std::uint32_t channels = 0; + std::uint32_t inputChannels = 0; std::uint32_t sampleRate = 0; std::uint32_t waveformType = 0; Ngs2Atrac9 atrac9; @@ -153,6 +154,7 @@ struct Ngs2Voice { void SetEvent(std::uint32_t eventId); void ResetSetup(); const std::uint8_t* WaveformData() const; + std::uint32_t InputChannels() const { return inputChannels != 0 ? inputChannels : channels; } }; struct Ngs2Rack { diff --git a/core/libs/prx/libSceNgs2.native/src/Rack.cpp b/core/libs/prx/libSceNgs2.native/src/Rack.cpp index 21d0cb17ba..fd8f22db33 100644 --- a/core/libs/prx/libSceNgs2.native/src/Rack.cpp +++ b/core/libs/prx/libSceNgs2.native/src/Rack.cpp @@ -19,6 +19,7 @@ union RackOptions { Ngs2ReverbRackOption reverb; Ngs2CustomSamplerRackOption customSampler; Ngs2CustomSubmixerRackOption customSubmixer; + Ngs2CustomMasteringRackOption customMastering; }; static std::size_t RackOptionSize(std::uint32_t rackId) { @@ -29,12 +30,13 @@ static std::size_t RackOptionSize(std::uint32_t rackId) { case SCE_NGS2_RACK_ID_REVERB: return sizeof(Ngs2ReverbRackOption); case SCE_NGS2_RACK_ID_CUSTOM_SAMPLER: return sizeof(Ngs2CustomSamplerRackOption); case SCE_NGS2_RACK_ID_CUSTOM_SUBMIXER: return sizeof(Ngs2CustomSubmixerRackOption); + case SCE_NGS2_RACK_ID_CUSTOM_MASTERING: return sizeof(Ngs2CustomMasteringRackOption); default: throw std::runtime_error("NGS2: rack id " + Ngs2Hex(rackId) + " is not implemented"); } } static RackOptions DefaultRackOption(std::uint32_t rackId) { - if (rackId == SCE_NGS2_RACK_ID_CUSTOM_SAMPLER || rackId == SCE_NGS2_RACK_ID_CUSTOM_SUBMIXER) { + if (rackId == SCE_NGS2_RACK_ID_CUSTOM_SAMPLER || rackId == SCE_NGS2_RACK_ID_CUSTOM_SUBMIXER || rackId == SCE_NGS2_RACK_ID_CUSTOM_MASTERING) { throw std::runtime_error("NGS2: default custom rack options are not implemented"); } RackOptions options{}; @@ -83,7 +85,7 @@ static std::uint32_t RackMaxChannels(std::uint32_t rackId, const RackOptions& op if (rackId == SCE_NGS2_RACK_ID_SUBMIXER) return options.submixer.max_channels; if (rackId == SCE_NGS2_RACK_ID_MASTERING) return options.mastering.max_channels; if (rackId == SCE_NGS2_RACK_ID_REVERB) return options.reverb.max_channels; - if (rackId == SCE_NGS2_RACK_ID_CUSTOM_SUBMIXER) return options.customSubmixer.max_channels; + if (rackId == SCE_NGS2_RACK_ID_CUSTOM_SUBMIXER || rackId == SCE_NGS2_RACK_ID_CUSTOM_MASTERING) return options.customSubmixer.max_channels; return NGS2_MAX_CHANNELS; } @@ -97,17 +99,17 @@ static int CheckedRackOption(std::uint32_t rackId, const Ngs2RackOption* option, options = {}; std::memcpy(&options, option, size); const auto maxChannels = RackMaxChannels(rackId, options); - if (rackId == SCE_NGS2_RACK_ID_CUSTOM_SUBMIXER && maxChannels > SCE_NGS2_CUSTOM_SUBMIXER_MAX_CHANNELS) return SCE_NGS2_ERROR_INVALID_NUM_CHANNELS; - if (rackId == SCE_NGS2_RACK_ID_CUSTOM_SUBMIXER && (options.customSubmixer.max_inputs == 0 || options.customSubmixer.max_inputs > SCE_NGS2_CUSTOM_SUBMIXER_MAX_INPUTS)) { + if ((rackId == SCE_NGS2_RACK_ID_CUSTOM_SUBMIXER || rackId == SCE_NGS2_RACK_ID_CUSTOM_MASTERING) && maxChannels > SCE_NGS2_CUSTOM_SUBMIXER_MAX_CHANNELS) return SCE_NGS2_ERROR_INVALID_NUM_CHANNELS; + if ((rackId == SCE_NGS2_RACK_ID_CUSTOM_SUBMIXER || rackId == SCE_NGS2_RACK_ID_CUSTOM_MASTERING) && (options.customSubmixer.max_inputs == 0 || options.customSubmixer.max_inputs > SCE_NGS2_CUSTOM_SUBMIXER_MAX_INPUTS)) { throw std::runtime_error("NGS2: custom submixer racks with " + std::to_string(options.customSubmixer.max_inputs) + " inputs are not implemented"); } if (options.common.max_voices == 0 || maxChannels == 0) throw std::invalid_argument("NGS2: invalid rack voice or channel count"); int result = SCE_NGS2_OK; if (rackId == SCE_NGS2_RACK_ID_CUSTOM_SAMPLER) result = Ngs2CheckCustomRack(rackId, options.customSampler.custom_rack_option, 0); - if (rackId == SCE_NGS2_RACK_ID_CUSTOM_SUBMIXER) result = Ngs2CheckCustomRack(rackId, options.customSubmixer.custom_rack_option, options.customSubmixer.max_inputs); + if (rackId == SCE_NGS2_RACK_ID_CUSTOM_SUBMIXER || rackId == SCE_NGS2_RACK_ID_CUSTOM_MASTERING) result = Ngs2CheckCustomRack(rackId, options.customSubmixer.custom_rack_option, options.customSubmixer.max_inputs); if (result != SCE_NGS2_OK) return result; if (maxChannels > NGS2_MAX_CHANNELS) { - if (rackId == SCE_NGS2_RACK_ID_CUSTOM_SUBMIXER) { + if (rackId == SCE_NGS2_RACK_ID_CUSTOM_SUBMIXER || rackId == SCE_NGS2_RACK_ID_CUSTOM_MASTERING) { throw std::runtime_error("NGS2: custom submixer racks with " + std::to_string(maxChannels) + " channels are not implemented"); } throw std::invalid_argument("NGS2: invalid rack voice or channel count"); @@ -129,7 +131,7 @@ static int CreateRack(Ngs2Handle systemHandle, std::uint32_t rackId, const RackO rack->maxChannels = RackMaxChannels(rackId, options); rack->maxFilters = rackId == SCE_NGS2_RACK_ID_SAMPLER ? options.sampler.max_filters : rackId == SCE_NGS2_RACK_ID_CUSTOM_SAMPLER ? options.customSampler.custom_rack_option.num_modules - : rackId == SCE_NGS2_RACK_ID_CUSTOM_SUBMIXER ? options.customSubmixer.custom_rack_option.num_modules : 0; + : (rackId == SCE_NGS2_RACK_ID_CUSTOM_SUBMIXER || rackId == SCE_NGS2_RACK_ID_CUSTOM_MASTERING) ? options.customSubmixer.custom_rack_option.num_modules : 0; rack->uid = nextUid++; std::memcpy(rack->name, options.common.name, sizeof(rack->name)); rack->name[sizeof(rack->name) - 1] = '\0'; @@ -137,7 +139,7 @@ static int CreateRack(Ngs2Handle systemHandle, std::uint32_t rackId, const RackO if (rackId == SCE_NGS2_RACK_ID_SAMPLER) rack->maxChannelWorks = options.sampler.max_channel_works; if (rackId == SCE_NGS2_RACK_ID_CUSTOM_SAMPLER) rack->maxChannelWorks = options.customSampler.max_channel_works; if (rackId == SCE_NGS2_RACK_ID_SUBMIXER) rack->maxInputs = options.submixer.max_inputs; - if (rackId == SCE_NGS2_RACK_ID_CUSTOM_SUBMIXER) rack->maxInputs = options.customSubmixer.max_inputs; + if (rackId == SCE_NGS2_RACK_ID_CUSTOM_SUBMIXER || rackId == SCE_NGS2_RACK_ID_CUSTOM_MASTERING) rack->maxInputs = options.customSubmixer.max_inputs; rack->bufferInfo = bufferInfo; rack->allocator = allocator; rack->voices.resize(options.common.max_voices); @@ -147,7 +149,7 @@ static int CreateRack(Ngs2Handle systemHandle, std::uint32_t rackId, const RackO voice.matrices.resize(options.common.max_matrices); } if (rackId == SCE_NGS2_RACK_ID_CUSTOM_SAMPLER) Ngs2SetupUserFx(*rack, options.customSampler.custom_rack_option); - if (rackId == SCE_NGS2_RACK_ID_CUSTOM_SUBMIXER) Ngs2SetupUserFx(*rack, options.customSubmixer.custom_rack_option); + if (rackId == SCE_NGS2_RACK_ID_CUSTOM_SUBMIXER || rackId == SCE_NGS2_RACK_ID_CUSTOM_MASTERING) Ngs2SetupUserFx(*rack, options.customSubmixer.custom_rack_option); system->racks.push_back(rack); *handle = reinterpret_cast(rack); return SCE_NGS2_OK; diff --git a/core/libs/prx/libSceNgs2.native/src/Render.cpp b/core/libs/prx/libSceNgs2.native/src/Render.cpp index fa764d375a..3fb3689e23 100644 --- a/core/libs/prx/libSceNgs2.native/src/Render.cpp +++ b/core/libs/prx/libSceNgs2.native/src/Render.cpp @@ -221,10 +221,11 @@ float Ngs2DefaultLevel(std::uint32_t sourceChannels, std::uint32_t source, std:: static void MixPort(Ngs2Voice& voice, const Ngs2Voice& source, const Ngs2Port& port, std::uint32_t grain) { const auto* matrix = port.matrix < 0 || source.matrices[port.matrix].empty() ? nullptr : &source.matrices[port.matrix]; - const std::size_t outputs = matrix == nullptr ? voice.channels : std::min(voice.channels, matrix->size() / source.channels); + const auto channels = voice.InputChannels(); + const std::size_t outputs = matrix == nullptr ? channels : std::min(channels, matrix->size() / source.channels); for (std::size_t dst = 0; dst < outputs; dst++) { for (std::uint32_t src = 0; src < source.channels; src++) { - const float level = port.volume * (matrix == nullptr ? Ngs2DefaultLevel(source.channels, src, voice.channels, static_cast(dst)) : (*matrix)[dst * source.channels + src]); + const float level = port.volume * (matrix == nullptr ? Ngs2DefaultLevel(source.channels, src, channels, static_cast(dst)) : (*matrix)[dst * source.channels + src]); if (level == 0.0f) continue; for (std::uint32_t i = 0; i < grain; i++) voice.samples[dst * grain + i] += source.samples[src * grain + i] * level; } @@ -251,7 +252,7 @@ static void RenderVoice(Ngs2Voice& voice, const std::vector& voices, bool& active; ~RenderingScope() { active = false; } } renderingScope{voice.rendering}; - voice.samples.assign(static_cast(grain) * voice.channels, 0.0f); + voice.samples.assign(static_cast(grain) * voice.InputChannels(), 0.0f); voice.hasSamples = false; if (voice.state == Ngs2PlayState::Playing && voice.channels != 0) { if (voice.rack->rackId == SCE_NGS2_RACK_ID_SAMPLER || voice.rack->rackId == SCE_NGS2_RACK_ID_CUSTOM_SAMPLER) { @@ -263,6 +264,7 @@ static void RenderVoice(Ngs2Voice& voice, const std::vector& voices, Ngs2ProcessLegacyUserFx(voice, grain, systemRate); Ngs2ProcessUserFx(voice, grain, systemRate); } + if (voice.samples.size() != static_cast(grain) * voice.channels) voice.samples.assign(static_cast(grain) * voice.channels, 0.0f); voice.rendered = true; } @@ -336,7 +338,7 @@ void Ngs2RenderSystem(Ngs2System& system, const Ngs2RenderBufferInfo* bufferInfo std::vector> mixes(numBufferInfo); for (auto* voice : voices) { RenderVoice(*voice, voices, grain, system.option.sample_rate); - if (voice->rack->rackId != SCE_NGS2_RACK_ID_MASTERING || !voice->hasSamples) continue; + if ((voice->rack->rackId != SCE_NGS2_RACK_ID_MASTERING && voice->rack->rackId != SCE_NGS2_RACK_ID_CUSTOM_MASTERING) || !voice->hasSamples) continue; auto& mix = OutputMix(mixes, bufferInfo, numBufferInfo, *voice, grain); const std::uint32_t outputChannels = bufferInfo[voice->outputId].num_channels; for (std::uint32_t channel = 0; channel < voice->channels; channel++) { diff --git a/core/libs/prx/libSceNgs2.native/src/Reverb.cpp b/core/libs/prx/libSceNgs2.native/src/Reverb.cpp index 131fb321f7..ab3849fbdd 100644 --- a/core/libs/prx/libSceNgs2.native/src/Reverb.cpp +++ b/core/libs/prx/libSceNgs2.native/src/Reverb.cpp @@ -292,9 +292,23 @@ bool Ngs2ProcessReverb(Ngs2Voice& voice, std::uint32_t grain, std::uint32_t samp Reset(reverb); reverb.changed = true; } + const std::uint32_t inputChannels = voice.InputChannels(); const std::vector input = voice.samples; std::vector mono(grain), early(grain), late(grain); - MixDown(input.data(), channels, grain, mono.data()); + MixDown(input.data(), inputChannels, grain, mono.data()); + std::vector mapped; + if (inputChannels != channels) { + mapped.assign(static_cast(grain) * channels, 0.0f); + for (std::uint32_t dst = 0; dst < channels; dst++) { + for (std::uint32_t src = 0; src < inputChannels; src++) { + const float level = Ngs2DefaultLevel(inputChannels, src, channels, dst); + if (level == 0.0f) continue; + for (std::uint32_t i = 0; i < grain; i++) mapped[static_cast(dst) * grain + i] += input[static_cast(src) * grain + i] * level; + } + } + voice.samples.assign(static_cast(grain) * channels, 0.0f); + } + const std::vector& direct = inputChannels != channels ? mapped : input; if (reverb.changed) { std::vector oldEarly(grain), oldLate(grain); RunTaps(reverb, reverb.taps, mono.data(), grain, oldEarly.data(), oldLate.data()); @@ -319,7 +333,7 @@ bool Ngs2ProcessReverb(Ngs2Voice& voice, std::uint32_t grain, std::uint32_t samp for (std::uint32_t channel = 0; channel < channels; channel++) { auto& state = reverb.channels[channel]; float* out = voice.samples.data() + static_cast(channel) * grain; - const float* in = input.data() + static_cast(channel) * grain; + const float* in = direct.data() + static_cast(channel) * grain; if (channel == LFE_CHANNEL) { std::copy(in, in + grain, out); } else { diff --git a/core/libs/prx/libSceNgs2.native/src/Voice.cpp b/core/libs/prx/libSceNgs2.native/src/Voice.cpp index 8f9c3fabc4..5ff13687d9 100644 --- a/core/libs/prx/libSceNgs2.native/src/Voice.cpp +++ b/core/libs/prx/libSceNgs2.native/src/Voice.cpp @@ -56,6 +56,7 @@ void Ngs2Voice::ResetSetup() { fbwLevel = 1.0f; lfeLevel = 1.0f; channels = 0; + inputChannels = 0; sampleRate = 0; waveformType = 0; atrac9 = {}; @@ -357,9 +358,11 @@ static void ApplyParam(Ngs2Voice& voice, const Ngs2VoiceParamHeader& param) { if (param.id == SCE_NGS2_REVERB_VOICE_PARAM_SETUP) { const auto& setup = ParamAs(param); if (setup.flags != 0) throw std::runtime_error("NGS2: reverb setup flags are not implemented"); - if (setup.num_input_channels != setup.num_output_channels) throw std::runtime_error("NGS2: reverb channel conversion is not implemented"); - if (setup.num_output_channels != 1 && setup.num_output_channels != 2 && setup.num_output_channels != 6 && setup.num_output_channels != 8) APS5_INVALID_ARG_EX; + for (const auto count : {setup.num_input_channels, setup.num_output_channels}) { + if ((count != 1 && count != 2 && count != 6 && count != 8) || count > voice.rack->maxChannels) APS5_INVALID_ARG_EX; + } SetupMixer(voice, setup.num_output_channels); + voice.inputChannels = setup.num_input_channels; Ngs2SetupReverb(voice); return; } @@ -369,18 +372,19 @@ static void ApplyParam(Ngs2Voice& voice, const Ngs2VoiceParamHeader& param) { } break; case SCE_NGS2_RACK_ID_MASTERING: - if (param.id == SCE_NGS2_MASTERING_VOICE_PARAM_SETUP) { + case SCE_NGS2_RACK_ID_CUSTOM_MASTERING: + if (param.id == SCE_NGS2_MASTERING_VOICE_PARAM_SETUP || param.id == SCE_NGS2_CUSTOM_MASTERING_VOICE_PARAM_SETUP) { SetupMixer(voice, ParamAs(param).num_io_channels); return; } - if (param.id == SCE_NGS2_MASTERING_VOICE_PARAM_GAIN) { + if (param.id == SCE_NGS2_MASTERING_VOICE_PARAM_GAIN || param.id == SCE_NGS2_CUSTOM_MASTERING_VOICE_PARAM_GAIN) { const auto& gain = ParamAs(param); if (!std::isfinite(gain.fbw_level) || !std::isfinite(gain.lfe_level)) APS5_INVALID_ARG_EX; voice.fbwLevel = gain.fbw_level; voice.lfeLevel = gain.lfe_level; return; } - if (param.id == SCE_NGS2_MASTERING_VOICE_PARAM_OUTPUT) { + if (param.id == SCE_NGS2_MASTERING_VOICE_PARAM_OUTPUT || param.id == SCE_NGS2_CUSTOM_MASTERING_VOICE_PARAM_OUTPUT) { voice.outputId = ParamAs(param).output_id; return; } @@ -472,6 +476,12 @@ int APS5_VABI sceNgs2VoiceGetState(uintptr_t voice_handle, Ngs2VoiceState* state sampler.num_decoded_samples = voice.decodedSamples; return SCE_NGS2_OK; } + case SCE_NGS2_RACK_ID_CUSTOM_MASTERING: { + if (state_size != sizeof(Ngs2VoiceState)) return SCE_NGS2_ERROR_INVALID_OUT_SIZE; + *state = {}; + state->state_flags = voice.stateFlags; + return SCE_NGS2_OK; + } default: throw std::runtime_error("NGS2: voice state of rack " + Ngs2Hex(voice.rack->rackId) + " is not implemented"); } } diff --git a/core/libs/prx/libSceNgs2.native/tests/Ngs2Custom.cpp b/core/libs/prx/libSceNgs2.native/tests/Ngs2Custom.cpp index 1e30889e34..80d9d2cb34 100644 --- a/core/libs/prx/libSceNgs2.native/tests/Ngs2Custom.cpp +++ b/core/libs/prx/libSceNgs2.native/tests/Ngs2Custom.cpp @@ -103,6 +103,21 @@ static bool CreateSamplerThrows(const Ngs2CustomSamplerRackOption& option) { return false; } +static Ngs2CustomMasteringRackOption MasteringRackOption(std::uint32_t channels, std::uint32_t inputs) { + Ngs2CustomMasteringRackOption option{}; + auto& common = option.custom_rack_option.rack_option; + common.size = sizeof(option); + common.max_grain_samples = 512; + common.max_voices = 2; + common.max_matrices = 1; + common.max_ports = 1; + option.custom_rack_option.num_buffers = 1; + option.custom_rack_option.num_modules = 0; + option.max_channels = channels; + option.max_inputs = inputs; + return option; +} + template static bool ControlThrows(uintptr_t voice, std::uint32_t id, TParam param) { param.header = {static_cast(sizeof(TParam)), 0, id}; @@ -825,6 +840,52 @@ static void TestCustomModuleInfo() { Require(sceNgs2SystemDestroy(system, nullptr) == SCE_NGS2_OK); } +static void TestCustomMastering() { + const auto system = CreateSystem(); + const auto good = MasteringRackOption(2, 1); + + Ngs2ContextBufferInfo query{}; + Require(sceNgs2RackQueryBufferSize(SCE_NGS2_RACK_ID_CUSTOM_MASTERING, &good.custom_rack_option.rack_option, &query) == SCE_NGS2_OK); + const auto info = Buffer(query); + uintptr_t rack = 0; + Require(sceNgs2RackCreate(system, SCE_NGS2_RACK_ID_CUSTOM_MASTERING, &good.custom_rack_option.rack_option, &info, &rack) == SCE_NGS2_OK); + const auto voice = Voice(rack); + + Control(voice, SCE_NGS2_CUSTOM_MASTERING_VOICE_PARAM_SETUP, Ngs2MasteringVoiceSetupParam{{}, 2, 0}); + Control(voice, SCE_NGS2_CUSTOM_MASTERING_VOICE_PARAM_GAIN, Ngs2MasteringVoiceGainParam{{}, 1.0f, 1.0f}); + Control(voice, SCE_NGS2_CUSTOM_MASTERING_VOICE_PARAM_OUTPUT, Ngs2MasteringVoiceOutputParam{{}, 0, 0}); + Event(voice, SCE_NGS2_VOICE_EVENT_PLAY); + + Ngs2VoiceState state{}; + Require(sceNgs2VoiceGetState(voice, &state, sizeof(state)) == SCE_NGS2_OK); + Require((state.state_flags & SCE_NGS2_VOICE_STATE_FLAG_INUSE) != 0); + Require(sceNgs2VoiceGetState(voice, &state, sizeof(state) - 1) == SCE_NGS2_ERROR_INVALID_OUT_SIZE); + + std::vector pcm(Grain * 2 * 8, 16384); + const auto sampler = Sampler(system, pcm); + Patch(sampler, voice); + Event(sampler, SCE_NGS2_VOICE_EVENT_PLAY); + + auto out = Render(system); + Require(out.size() == Grain * 2); + Require(out[0] != 0.0f); + + Require(sceNgs2VoiceGetState(voice, &state, sizeof(state)) == SCE_NGS2_OK); + Require((state.state_flags & SCE_NGS2_VOICE_STATE_FLAG_PLAYING) != 0); + + auto createMastering = [&](const Ngs2CustomMasteringRackOption& opt) { + uintptr_t r = 0; + return sceNgs2RackCreate(system, SCE_NGS2_RACK_ID_CUSTOM_MASTERING, &opt.custom_rack_option.rack_option, &info, &r); + }; + + auto invalidChannels = good; + invalidChannels.max_channels = SCE_NGS2_CUSTOM_SUBMIXER_MAX_CHANNELS + 1; + Require(createMastering(invalidChannels) == SCE_NGS2_ERROR_INVALID_NUM_CHANNELS); + + Require(sceNgs2RackDestroy(rack, nullptr) == SCE_NGS2_OK); + Require(sceNgs2SystemDestroy(system, nullptr) == SCE_NGS2_OK); +} + int main() { for (const auto count : {1u, 2u, 24u}) { for (const auto delay : {DelayOption{}, DelayOption{20, 1, 1, 1.0f, 0}, DelayOption{20, 1, 8, 20000.0f, 0}}) { @@ -839,5 +900,6 @@ int main() { for (const auto moduleIndex : {0u, 1u, 23u}) TestCustomLegacyUserFx(moduleIndex); TestCustomGain(); TestCustomModuleInfo(); + TestCustomMastering(); return 0; } diff --git a/core/libs/prx/libSceNgs2.native/tests/Ngs2Render.cpp b/core/libs/prx/libSceNgs2.native/tests/Ngs2Render.cpp index c9f7cd7fcb..0e044309fa 100644 --- a/core/libs/prx/libSceNgs2.native/tests/Ngs2Render.cpp +++ b/core/libs/prx/libSceNgs2.native/tests/Ngs2Render.cpp @@ -1,6 +1,7 @@ #include "Ngs2Test.hpp" #include +#include #include #include #include @@ -686,6 +687,73 @@ static void TestReverb() { } } +static std::vector RenderReverbChannels(std::uint32_t inputs, std::uint32_t outputs, const Ngs2ReverbI3DL2Param& params, const std::vector& pcm, std::uint32_t frames) { + const auto firstBuffer = usedBuffers; + const auto system = CreateSystem(); + Require(sceNgs2SystemSetGrainSamples(system, 256) == SCE_NGS2_OK); + const auto master = Mastering(system, outputs); + const auto reverb = Voice(CreateRack(system, SCE_NGS2_RACK_ID_REVERB)); + Control(reverb, SCE_NGS2_REVERB_VOICE_PARAM_SETUP, Ngs2ReverbVoiceSetupParam{{}, inputs, outputs, 0, 0}); + Control(reverb, SCE_NGS2_REVERB_VOICE_PARAM_I3DL2, Ngs2ReverbVoiceI3DL2Param{{}, params}); + Patch(reverb, master); + Event(reverb, SCE_NGS2_VOICE_EVENT_PLAY); + const auto sampler = Voice(CreateRack(system, SCE_NGS2_RACK_ID_SAMPLER)); + Control(sampler, SCE_NGS2_SAMPLER_VOICE_PARAM_SETUP, Ngs2SamplerVoiceSetupParam{{}, {SCE_NGS2_WAVEFORM_TYPE_PCM_I16L, inputs, 48000, 0, 0, 0}}); + const Ngs2WaveformBlock block{0, pcm.size() * sizeof(std::int16_t), 0, 0, static_cast(pcm.size() / inputs), 0, 0}; + Control(sampler, SCE_NGS2_SAMPLER_VOICE_PARAM_ADD_WAVEFORM_BLOCKS, Ngs2SamplerVoiceWaveformBlocksParam{{}, pcm.data(), 0, 1, &block}); + Patch(sampler, reverb); + std::vector identity(inputs * inputs, 0.0f); + for (std::uint32_t c = 0; c < inputs; c++) identity[c * inputs + c] = 1.0f; + Control(sampler, SCE_NGS2_VOICE_PARAM_MATRIX_LEVELS, Ngs2VoiceMatrixLevelsParam{{}, 0, inputs * inputs, identity.data()}); + Control(sampler, SCE_NGS2_VOICE_PARAM_PORT_MATRIX, Ngs2VoicePortMatrixParam{{}, 0, 0}); + Event(sampler, SCE_NGS2_VOICE_EVENT_PLAY); + std::vector all; + std::vector out(256 * outputs); + for (std::uint32_t grain = 0; grain * 256 < frames; grain++) { + const Ngs2RenderBufferInfo info{out.data(), out.size() * sizeof(float), SCE_NGS2_WAVEFORM_TYPE_PCM_F32L, outputs}; + Require(sceNgs2SystemRender(system, &info, 1) == SCE_NGS2_OK); + all.insert(all.end(), out.begin(), out.end()); + } + Require(sceNgs2SystemDestroy(system, nullptr) == SCE_NGS2_OK); + usedBuffers = firstBuffer; + return all; +} + +static void TestReverbConversion() { + Ngs2ReverbI3DL2Param mixed{1.0f, 1.0f, -1000, 0, 0, 2.91f, 1.3f, -602, 0.015f, -302, 0.022f, 100.0f, 100.0f, 5000.0f, {}}; + std::vector stereo(4096 * 2, 0); + std::uint32_t seed = 1; + for (std::uint32_t i = 0; i < 3000 * 2; i++) { + seed = seed * 1664525u + 1013904223u; + stereo[i] = static_cast(static_cast((seed >> 16) & 0x3fff) - 0x2000); + } + const auto same = RenderReverbChannels(2, 2, mixed, stereo, 8192); + for (std::uint32_t outputs : {6u, 8u}) { + const auto surround = RenderReverbChannels(2, outputs, mixed, stereo, 8192); + float sides = 0.0f; + float lfe = 0.0f; + for (std::uint32_t frame = 0; frame < 8192; frame++) { + for (std::uint32_t c = 0; c < 2; c++) Require(std::fabs(surround[frame * outputs + c] - same[frame * 2 + c]) <= 1e-7f + std::fabs(same[frame * 2 + c]) * 1e-5f); + for (std::uint32_t c = 2; c < outputs; c++) { + if (c != 3) sides = std::max(sides, std::fabs(surround[frame * outputs + c])); + } + lfe = std::max(lfe, std::fabs(surround[frame * outputs + 3])); + } + Require(sides > 1e-4f && lfe == 0.0f); + } + + auto dry = mixed; + dry.wet = 0.0f; + const std::int16_t levels[6] = {8192, 4096, 2048, 1024, 512, 256}; + std::vector six(4096 * 6); + for (std::size_t i = 0; i < six.size(); i++) six[i] = levels[i % 6]; + const auto down = RenderReverbChannels(6, 2, dry, six, 2048); + const float half = std::numbers::sqrt2_v / 2.0f; + const float left = (8192.0f + 2048.0f * half + 512.0f * 0.5f) / 32768.0f; + const float right = (4096.0f + 2048.0f * half + 256.0f * 0.5f) / 32768.0f; + Require(std::fabs(down[1024 * 2] - left) <= left * 1e-5f && std::fabs(down[1024 * 2 + 1] - right) <= right * 1e-5f); +} + static void TestReverbSetup() { const auto system = CreateSystem(); const auto master = Mastering(system, 2); @@ -695,12 +763,12 @@ static void TestReverbSetup() { try { Control(reverb, SCE_NGS2_REVERB_VOICE_PARAM_SETUP, Ngs2ReverbVoiceSetupParam{{}, channels, channels, 0, 0}); } catch (const std::invalid_argument&) { rejected = true; } Require(rejected); } - bool conversion = false; - try { Control(reverb, SCE_NGS2_REVERB_VOICE_PARAM_SETUP, Ngs2ReverbVoiceSetupParam{{}, 1, 2, 0, 0}); } catch (const std::runtime_error&) { conversion = true; } - Require(conversion); - bool flags = false; - try { Control(reverb, SCE_NGS2_REVERB_VOICE_PARAM_SETUP, Ngs2ReverbVoiceSetupParam{{}, 2, 2, 1, 0}); } catch (const std::runtime_error&) { flags = true; } - Require(flags); + for (const auto& counts : {std::array{3, 2}, std::array{2, 7}}) { + bool rejected = false; + try { Control(reverb, SCE_NGS2_REVERB_VOICE_PARAM_SETUP, Ngs2ReverbVoiceSetupParam{{}, counts[0], counts[1], 0, 0}); } catch (const std::invalid_argument&) { rejected = true; } + Require(rejected); + } + Control(reverb, SCE_NGS2_REVERB_VOICE_PARAM_SETUP, Ngs2ReverbVoiceSetupParam{{}, 1, 2, 0, 0}); Control(reverb, SCE_NGS2_REVERB_VOICE_PARAM_SETUP, Ngs2ReverbVoiceSetupParam{{}, 2, 2, 0, 0}); Ngs2ReverbI3DL2Param invalid{1.0f, 0.0f, 0, 0, 0, 1.0f, 1.0f, 0, 0.0f, 0, 0.0f, 100.0f, 100.0f, 5000.0f, {}}; invalid.decay_time = NAN; @@ -1589,6 +1657,7 @@ int main() { TestSubmixerMatrix(); TestReverb(); TestReverbSetup(); + TestReverbConversion(); TestSampleRate(); TestSystemOptionFlags(); TestUserData(); diff --git a/core/libs/prx/libSceNpCppWebApi/Export.cpp b/core/libs/prx/libSceNpCppWebApi/Export.cpp index c8eec1ea1a..33bfb17ed2 100644 --- a/core/libs/prx/libSceNpCppWebApi/Export.cpp +++ b/core/libs/prx/libSceNpCppWebApi/Export.cpp @@ -1487,124 +1487,102 @@ int APS5_VABI _ZN3sce2Np9CppWebApi6Common11TransactionINS2_15DefaultResponseENS2 return 0; } -int APS5_VABI _ZN3sce2Np9CppWebApi6Common13ConstIteratorINS2_12IntrusivePtrINS1_13InGameCatalog2V57ProductEEEED2Ev(void) { - NotImplemented_nid_no_patch(__func__); +int APS5_VABI _ZN3sce2Np9CppWebApi6Common13ConstIteratorINS2_12IntrusivePtrINS1_13InGameCatalog2V57ProductEEEED2Ev(void* self) { + (void)self; return 0; } -int APS5_VABI _ZN3sce2Np9CppWebApi6Common13ConstIteratorINS2_12IntrusivePtrINS1_17TitleCloudStorage2V110DataStatusEEEED2Ev(void) { - NotImplemented_nid_no_patch(__func__); +int APS5_VABI _ZN3sce2Np9CppWebApi6Common13ConstIteratorINS2_12IntrusivePtrINS1_17TitleCloudStorage2V110DataStatusEEEED2Ev(void* self) { + (void)self; return 0; } -int APS5_VABI _ZN3sce2Np9CppWebApi6Common12IntrusivePtrINS2_6VectorINS3_INS1_13InGameCatalog2V59ContainerEEEEEEC1Ev(void) { - NotImplemented_nid_no_patch(__func__); - return 0; +void APS5_VABI _ZN3sce2Np9CppWebApi6Common12IntrusivePtrINS2_6VectorINS3_INS1_13InGameCatalog2V59ContainerEEEEEEC1Ev(IntrusivePtr* self) { + *self = {}; } -int APS5_VABI _ZNK3sce2Np9CppWebApi6Common12IntrusivePtrINS2_6VectorINS3_INS1_13InGameCatalog2V59ContainerEEEEEEdeEv(void) { - NotImplemented_nid_no_patch(__func__); - return 0; +void* APS5_VABI _ZNK3sce2Np9CppWebApi6Common12IntrusivePtrINS2_6VectorINS3_INS1_13InGameCatalog2V59ContainerEEEEEEdeEv(const IntrusivePtr* self) { + return self->object; } -int APS5_VABI _ZNK3sce2Np9CppWebApi6Common12IntrusivePtrINS2_6VectorINS3_INS1_13InGameCatalog2V59ContainerEEEEEEptEv(void) { - NotImplemented_nid_no_patch(__func__); - return 0; +void* APS5_VABI _ZNK3sce2Np9CppWebApi6Common12IntrusivePtrINS2_6VectorINS3_INS1_13InGameCatalog2V59ContainerEEEEEEptEv(const IntrusivePtr* self) { + return self->object; } -int APS5_VABI _ZN3sce2Np9CppWebApi6Common12IntrusivePtrINS2_6VectorINS3_INS1_13InGameCatalog2V59ContainerEEEEEED1Ev(void) { - NotImplemented_nid_no_patch(__func__); - return 0; +void APS5_VABI _ZN3sce2Np9CppWebApi6Common12IntrusivePtrINS2_6VectorINS3_INS1_13InGameCatalog2V59ContainerEEEEEED1Ev(IntrusivePtr* self) { + ReleaseIntrusivePtr(self, 0x30, __func__); } -int APS5_VABI _ZNK3sce2Np9CppWebApi6Common12IntrusivePtrINS2_6VectorINS3_INS1_13InGameCatalog2V57ProductEEEEEEptEv(void) { - NotImplemented_nid_no_patch(__func__); - return 0; +void* APS5_VABI _ZNK3sce2Np9CppWebApi6Common12IntrusivePtrINS2_6VectorINS3_INS1_13InGameCatalog2V57ProductEEEEEEptEv(const IntrusivePtr* self) { + return self->object; } -int APS5_VABI _ZN3sce2Np9CppWebApi6Common12IntrusivePtrINS2_6VectorINS3_INS1_13InGameCatalog2V57ProductEEEEEED1Ev(void) { - NotImplemented_nid_no_patch(__func__); - return 0; +void APS5_VABI _ZN3sce2Np9CppWebApi6Common12IntrusivePtrINS2_6VectorINS3_INS1_13InGameCatalog2V57ProductEEEEEED1Ev(IntrusivePtr* self) { + ReleaseIntrusivePtr(self, 0x30, __func__); } -int APS5_VABI _ZNK3sce2Np9CppWebApi6Common12IntrusivePtrINS2_6VectorINS3_INS1_13InGameCatalog2V53SkuEEEEEEptEv(void) { - NotImplemented_nid_no_patch(__func__); - return 0; +void* APS5_VABI _ZNK3sce2Np9CppWebApi6Common12IntrusivePtrINS2_6VectorINS3_INS1_13InGameCatalog2V53SkuEEEEEEptEv(const IntrusivePtr* self) { + return self->object; } -int APS5_VABI _ZN3sce2Np9CppWebApi6Common12IntrusivePtrINS2_6VectorINS3_INS1_13InGameCatalog2V53SkuEEEEEED1Ev(void) { - NotImplemented_nid_no_patch(__func__); - return 0; +void APS5_VABI _ZN3sce2Np9CppWebApi6Common12IntrusivePtrINS2_6VectorINS3_INS1_13InGameCatalog2V53SkuEEEEEED1Ev(IntrusivePtr* self) { + ReleaseIntrusivePtr(self, 0x30, __func__); } -int APS5_VABI _ZNK3sce2Np9CppWebApi6Common12IntrusivePtrINS2_6VectorINS3_INS1_17TitleCloudStorage2V110DataStatusEEEEEEdeEv(void) { - NotImplemented_nid_no_patch(__func__); - return 0; +void* APS5_VABI _ZNK3sce2Np9CppWebApi6Common12IntrusivePtrINS2_6VectorINS3_INS1_17TitleCloudStorage2V110DataStatusEEEEEEdeEv(const IntrusivePtr* self) { + return self->object; } -int APS5_VABI _ZN3sce2Np9CppWebApi6Common12IntrusivePtrINS2_6VectorINS3_INS1_17TitleCloudStorage2V110DataStatusEEEEEED1Ev(void) { - NotImplemented_nid_no_patch(__func__); - return 0; +void APS5_VABI _ZN3sce2Np9CppWebApi6Common12IntrusivePtrINS2_6VectorINS3_INS1_17TitleCloudStorage2V110DataStatusEEEEEED1Ev(IntrusivePtr* self) { + ReleaseIntrusivePtr(self, 0x30, __func__); } -int APS5_VABI _ZN3sce2Np9CppWebApi6Common12IntrusivePtrINS1_13InGameCatalog2V59ContainerEEC1ERS7_(void) { - NotImplemented_nid_no_patch(__func__); - return 0; +void APS5_VABI _ZN3sce2Np9CppWebApi6Common12IntrusivePtrINS1_13InGameCatalog2V59ContainerEEC1ERS7_(IntrusivePtr* self, const IntrusivePtr* source) { + CopyIntrusivePtr(self, source, 0x00); } -int APS5_VABI _ZNK3sce2Np9CppWebApi6Common12IntrusivePtrINS1_13InGameCatalog2V59ContainerEEptEv(void) { - NotImplemented_nid_no_patch(__func__); - return 0; +void* APS5_VABI _ZNK3sce2Np9CppWebApi6Common12IntrusivePtrINS1_13InGameCatalog2V59ContainerEEptEv(const IntrusivePtr* self) { + return self->object; } -int APS5_VABI _ZN3sce2Np9CppWebApi6Common12IntrusivePtrINS1_13InGameCatalog2V59ContainerEED1Ev(void) { - NotImplemented_nid_no_patch(__func__); - return 0; +void APS5_VABI _ZN3sce2Np9CppWebApi6Common12IntrusivePtrINS1_13InGameCatalog2V59ContainerEED1Ev(IntrusivePtr* self) { + ReleaseIntrusivePtr(self, 0x00, __func__); } -int APS5_VABI _ZN3sce2Np9CppWebApi6Common12IntrusivePtrINS1_13InGameCatalog2V57ProductEEC1ERS7_(void) { - NotImplemented_nid_no_patch(__func__); - return 0; +void APS5_VABI _ZN3sce2Np9CppWebApi6Common12IntrusivePtrINS1_13InGameCatalog2V57ProductEEC1ERS7_(IntrusivePtr* self, const IntrusivePtr* source) { + CopyIntrusivePtr(self, source, 0x00); } -int APS5_VABI _ZNK3sce2Np9CppWebApi6Common12IntrusivePtrINS1_13InGameCatalog2V57ProductEEptEv(void) { - NotImplemented_nid_no_patch(__func__); - return 0; +void* APS5_VABI _ZNK3sce2Np9CppWebApi6Common12IntrusivePtrINS1_13InGameCatalog2V57ProductEEptEv(const IntrusivePtr* self) { + return self->object; } -int APS5_VABI _ZN3sce2Np9CppWebApi6Common12IntrusivePtrINS1_13InGameCatalog2V57ProductEED1Ev(void) { - NotImplemented_nid_no_patch(__func__); - return 0; +void APS5_VABI _ZN3sce2Np9CppWebApi6Common12IntrusivePtrINS1_13InGameCatalog2V57ProductEED1Ev(IntrusivePtr* self) { + ReleaseIntrusivePtr(self, 0x00, __func__); } -int APS5_VABI _ZNK3sce2Np9CppWebApi6Common12IntrusivePtrINS1_17TitleCloudStorage2V110DataStatusEEptEv(void) { - NotImplemented_nid_no_patch(__func__); - return 0; +void* APS5_VABI _ZNK3sce2Np9CppWebApi6Common12IntrusivePtrINS1_17TitleCloudStorage2V110DataStatusEEptEv(const IntrusivePtr* self) { + return self->object; } -int APS5_VABI _ZN3sce2Np9CppWebApi6Common12IntrusivePtrINS1_17TitleCloudStorage2V132GetMultiDataStatusesResponseBodyEEC1Ev(void) { - NotImplemented_nid_no_patch(__func__); - return 0; +void APS5_VABI _ZN3sce2Np9CppWebApi6Common12IntrusivePtrINS1_17TitleCloudStorage2V132GetMultiDataStatusesResponseBodyEEC1Ev(IntrusivePtr* self) { + *self = {}; } -int APS5_VABI _ZNK3sce2Np9CppWebApi6Common12IntrusivePtrINS1_17TitleCloudStorage2V132GetMultiDataStatusesResponseBodyEEptEv(void) { - NotImplemented_nid_no_patch(__func__); - return 0; +void* APS5_VABI _ZNK3sce2Np9CppWebApi6Common12IntrusivePtrINS1_17TitleCloudStorage2V132GetMultiDataStatusesResponseBodyEEptEv(const IntrusivePtr* self) { + return self->object; } -int APS5_VABI _ZN3sce2Np9CppWebApi6Common12IntrusivePtrINS1_17TitleCloudStorage2V132GetMultiDataStatusesResponseBodyEED1Ev(void) { - NotImplemented_nid_no_patch(__func__); - return 0; +void APS5_VABI _ZN3sce2Np9CppWebApi6Common12IntrusivePtrINS1_17TitleCloudStorage2V132GetMultiDataStatusesResponseBodyEED1Ev(IntrusivePtr* self) { + ReleaseIntrusivePtr(self, 0x00, __func__); } -int APS5_VABI _ZNK3sce2Np9CppWebApi6Common12IntrusivePtrINS1_17TitleCloudStorage2V15OwnerEEptEv(void) { - NotImplemented_nid_no_patch(__func__); - return 0; +void* APS5_VABI _ZNK3sce2Np9CppWebApi6Common12IntrusivePtrINS1_17TitleCloudStorage2V15OwnerEEptEv(const IntrusivePtr* self) { + return self->object; } -int APS5_VABI _ZN3sce2Np9CppWebApi6Common12IntrusivePtrINS1_17TitleCloudStorage2V15OwnerEED1Ev(void) { - NotImplemented_nid_no_patch(__func__); - return 0; +void APS5_VABI _ZN3sce2Np9CppWebApi6Common12IntrusivePtrINS1_17TitleCloudStorage2V15OwnerEED1Ev(IntrusivePtr* self) { + ReleaseIntrusivePtr(self, 0x00, __func__); } bool APS5_VABI _ZNK3sce2Np9CppWebApi6Common8IteratorINS2_12IntrusivePtrINS1_13InGameCatalog2V57ProductEEEEneERKS9_(const void* self, const void* other) { diff --git a/core/libs/prx/libSceNpMatching2/CMakeLists.txt b/core/libs/prx/libSceNpMatching2/CMakeLists.txt new file mode 100644 index 0000000000..06978af7ad --- /dev/null +++ b/core/libs/prx/libSceNpMatching2/CMakeLists.txt @@ -0,0 +1,16 @@ +add_library(libSceNpMatching2 SHARED EXCLUDE_FROM_ALL Export.cpp) +target_include_directories(libSceNpMatching2 PRIVATE ${LIBS_INCLUDE_DIR}) +configure_windows_unwind(libSceNpMatching2) + +if(BUILD_TESTING) + add_test_executable(guest_np_matching2_offline_tests ${LIBS_INCLUDE_DIR}/tests/GuestNpMatching2Offline.cpp) + target_include_directories(guest_np_matching2_offline_tests PRIVATE ${LIBS_INCLUDE_DIR}) + target_link_libraries(guest_np_matching2_offline_tests PRIVATE libSceNpMatching2) + configure_windows_unwind(guest_np_matching2_offline_tests) + add_test(NAME guest_np_matching2_offline COMMAND guest_np_matching2_offline_tests) + set_tests_properties(guest_np_matching2_offline PROPERTIES TIMEOUT 20) + if(WIN32) + set_tests_properties(guest_np_matching2_offline PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + endif() +endif() diff --git a/core/libs/prx/libSceNpMatching2/Export.cpp b/core/libs/prx/libSceNpMatching2/Export.cpp index d7fdbb3817..c7dd08b7af 100644 --- a/core/libs/prx/libSceNpMatching2/Export.cpp +++ b/core/libs/prx/libSceNpMatching2/Export.cpp @@ -1,12 +1,19 @@ #include #include #include +#include #include "SceTypes.hpp" #include "prx/libc/include/General.hpp" static constexpr int SCE_NP_MATCHING2_ERROR_ALREADY_INITIALIZED = static_cast(0x80550C02); static constexpr int SCE_NP_MATCHING2_ERROR_NOT_INITIALIZED = static_cast(0x80550C03); static constexpr int SCE_NP_MATCHING2_ERROR_INVALID_ARGUMENT = static_cast(0x80550C0A); +static constexpr int SCE_NP_MATCHING2_ERROR_CONTEXT_MAX = static_cast(0x80550C04); +static constexpr int SCE_NP_MATCHING2_ERROR_CONTEXT_NOT_STARTED = static_cast(0x80550C08); +static constexpr int SCE_NP_MATCHING2_ERROR_INVALID_CONTEXT_ID = static_cast(0x80550C0B); +static constexpr int SCE_NET_ERROR_RESOLVER_ETIMEDOUT = static_cast(0x804101E2); +static constexpr int MAX_CONTEXTS = 10; +static constexpr std::uint64_t CREATE_CONTEXT_PARAMETER_SIZE = 0x28; namespace { @@ -22,26 +29,56 @@ struct NpMatching2InitializeParameter { static_assert(offsetof(NpMatching2InitializeParameter, size) == 0x20); static_assert(sizeof(NpMatching2InitializeParameter) == 0x30); +struct NpMatching2CreateContextParameter { + const void* npId; + const void* npCommunicationId; + const void* npPassphrase; + std::uint32_t serviceLabel; + std::uint64_t size; +}; +static_assert(sizeof(NpMatching2CreateContextParameter) == CREATE_CONTEXT_PARAMETER_SIZE); + std::mutex matching2Mutex; bool matching2Initialized = false; +std::unordered_set contexts; +int nextContextId = 1; + +int CheckContext(std::uint16_t ctxId) { + if (!matching2Initialized) return SCE_NP_MATCHING2_ERROR_NOT_INITIALIZED; + return contexts.contains(ctxId) ? 0 : SCE_NP_MATCHING2_ERROR_INVALID_CONTEXT_ID; +} } extern "C" { -int APS5_VABI sceNpMatching2ContextStart() { - NotImplemented_nid_no_patch(__func__); - return 0; +int APS5_VABI sceNpMatching2ContextStart(std::uint16_t ctxId, std::uint64_t timeout) { + (void)ctxId; + (void)timeout; + std::lock_guard lock(matching2Mutex); + if (!matching2Initialized) return SCE_NP_MATCHING2_ERROR_NOT_INITIALIZED; + return SCE_NET_ERROR_RESOLVER_ETIMEDOUT; } -int APS5_VABI sceNpMatching2ContextStop() { - NotImplemented_nid_no_patch(__func__); - return 0; +int APS5_VABI sceNpMatching2ContextStop(std::uint16_t ctxId) { + std::lock_guard lock(matching2Mutex); + if (const int error = CheckContext(ctxId)) return error; + return SCE_NP_MATCHING2_ERROR_CONTEXT_NOT_STARTED; } -int APS5_VABI sceNpMatching2CreateContext() { - NotImplemented_nid_no_patch(__func__); - return 0; +int APS5_VABI sceNpMatching2CreateContext(const NpMatching2CreateContextParameter* param, std::uint16_t* ctxId) { + std::lock_guard lock(matching2Mutex); + if (!matching2Initialized) return SCE_NP_MATCHING2_ERROR_NOT_INITIALIZED; + if (!param || param->size != CREATE_CONTEXT_PARAMETER_SIZE || !ctxId) return SCE_NP_MATCHING2_ERROR_INVALID_ARGUMENT; + for (int tried = 0; tried < MAX_CONTEXTS; ++tried) { + const auto candidate = static_cast((nextContextId - 1 + tried) % MAX_CONTEXTS + 1); + if (contexts.contains(candidate)) continue; + contexts.insert(candidate); + nextContextId = candidate % MAX_CONTEXTS + 1; + *ctxId = candidate; + return 0; + } + return SCE_NP_MATCHING2_ERROR_CONTEXT_MAX; } int APS5_VABI sceNpMatching2CreateJoinRoom() { @@ -49,8 +86,10 @@ int APS5_VABI sceNpMatching2CreateJoinRoom() { return 0; } -int APS5_VABI sceNpMatching2DestroyContext() { - NotImplemented_nid_no_patch(__func__); +int APS5_VABI sceNpMatching2DestroyContext(std::uint16_t ctxId) { + std::lock_guard lock(matching2Mutex); + if (const int error = CheckContext(ctxId)) return error; + contexts.erase(ctxId); return 0; } @@ -107,24 +146,32 @@ int APS5_VABI sceNpMatching2LeaveRoom() { return 0; } -int APS5_VABI sceNpMatching2RegisterContextCallback() { - NotImplemented_nid_no_patch(__func__); - return 0; +int APS5_VABI sceNpMatching2RegisterContextCallback(void* callback, void* userdata) { + (void)callback; + (void)userdata; + std::lock_guard lock(matching2Mutex); + return matching2Initialized ? 0 : SCE_NP_MATCHING2_ERROR_NOT_INITIALIZED; } -int APS5_VABI sceNpMatching2RegisterRoomEventCallback() { - NotImplemented_nid_no_patch(__func__); - return 0; +int APS5_VABI sceNpMatching2RegisterRoomEventCallback(std::uint16_t ctxId, void* callback, void* userdata) { + (void)callback; + (void)userdata; + std::lock_guard lock(matching2Mutex); + return CheckContext(ctxId); } -int APS5_VABI sceNpMatching2RegisterRoomMessageCallback() { - NotImplemented_nid_no_patch(__func__); - return 0; +int APS5_VABI sceNpMatching2RegisterRoomMessageCallback(std::uint16_t ctxId, void* callback, void* userdata) { + (void)callback; + (void)userdata; + std::lock_guard lock(matching2Mutex); + return CheckContext(ctxId); } -int APS5_VABI sceNpMatching2RegisterSignalingCallback() { - NotImplemented_nid_no_patch(__func__); - return 0; +int APS5_VABI sceNpMatching2RegisterSignalingCallback(std::uint16_t ctxId, void* callback, void* userdata) { + (void)callback; + (void)userdata; + std::lock_guard lock(matching2Mutex); + return CheckContext(ctxId); } int APS5_VABI sceNpMatching2SearchRoom() { @@ -142,9 +189,11 @@ int APS5_VABI sceNpMatching2SendRoomMessage() { return 0; } -int APS5_VABI sceNpMatching2SetDefaultRequestOptParam() { - NotImplemented_nid_no_patch(__func__); - return 0; +int APS5_VABI sceNpMatching2SetDefaultRequestOptParam(std::uint16_t ctxId, const void* requestOpt) { + std::lock_guard lock(matching2Mutex); + if (!matching2Initialized) return SCE_NP_MATCHING2_ERROR_NOT_INITIALIZED; + if (!requestOpt) return SCE_NP_MATCHING2_ERROR_INVALID_ARGUMENT; + return CheckContext(ctxId); } int APS5_VABI sceNpMatching2SetRoomDataExternal() { @@ -176,6 +225,8 @@ int APS5_VABI sceNpMatching2Terminate(void) { std::lock_guard lock(matching2Mutex); if (!matching2Initialized) return SCE_NP_MATCHING2_ERROR_NOT_INITIALIZED; matching2Initialized = false; + contexts.clear(); + nextContextId = 1; return 0; } diff --git a/core/libs/prx/libSceNpTrophy2/src/Context.cpp b/core/libs/prx/libSceNpTrophy2/src/Context.cpp index ef3a23e905..f59a014de6 100644 --- a/core/libs/prx/libSceNpTrophy2/src/Context.cpp +++ b/core/libs/prx/libSceNpTrophy2/src/Context.cpp @@ -83,9 +83,10 @@ int APS5_VABI sceNpTrophy2GetRewardIcon(int context, int handle, int reward_id, (void)handle; (void)reward_id; (void)buffer; - (void)size; - NotImplemented_nid_no_patch(__func__); - return 0; + if (size != nullptr) { + *size = NP_TROPHY2_ICON_SIZE_NONE; + } + return SCE_NP_TROPHY2_ERROR_ICON_FILE_NOT_FOUND; } int APS5_VABI sceNpTrophy2ShowTrophyList(int context) { diff --git a/core/libs/prx/libSceNpUniversalDataSystem/CMakeLists.txt b/core/libs/prx/libSceNpUniversalDataSystem/CMakeLists.txt index d9163d1dc7..62016d6a55 100644 --- a/core/libs/prx/libSceNpUniversalDataSystem/CMakeLists.txt +++ b/core/libs/prx/libSceNpUniversalDataSystem/CMakeLists.txt @@ -1,4 +1,5 @@ add_library(libSceNpUniversalDataSystem SHARED EXCLUDE_FROM_ALL + ${CMAKE_CURRENT_SOURCE_DIR}/PendingExports.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/System.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Context.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Handle.cpp diff --git a/core/libs/prx/libSceNpUniversalDataSystem/PendingExports.cpp b/core/libs/prx/libSceNpUniversalDataSystem/PendingExports.cpp new file mode 100644 index 0000000000..b19e95eb61 --- /dev/null +++ b/core/libs/prx/libSceNpUniversalDataSystem/PendingExports.cpp @@ -0,0 +1,137 @@ +#include +#include +#include "SceTypes.hpp" +#include "prx/libc/include/General.hpp" + +extern "C" { + +int APS5_VABI sceNpUniversalDataSystemIntCreateRecordArray() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpUniversalDataSystemIntCreateRecordData() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpUniversalDataSystemIntCreateRecordObject() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpUniversalDataSystemIntDeleteAllRecord() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpUniversalDataSystemIntDestroyRecordArray() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpUniversalDataSystemIntDestroyRecordData() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpUniversalDataSystemIntDestroyRecordObject() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpUniversalDataSystemIntGetMemoryStat() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpUniversalDataSystemIntPostRecordData() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpUniversalDataSystemIntRecordArraySetArray() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpUniversalDataSystemIntRecordArraySetBinary() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpUniversalDataSystemIntRecordArraySetBool() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpUniversalDataSystemIntRecordArraySetFloat32() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpUniversalDataSystemIntRecordArraySetFloat64() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpUniversalDataSystemIntRecordArraySetInt32() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpUniversalDataSystemIntRecordArraySetInt64() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpUniversalDataSystemIntRecordArraySetObject() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpUniversalDataSystemIntRecordArraySetString() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpUniversalDataSystemIntRecordArraySetUInt32() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpUniversalDataSystemIntRecordArraySetUInt64() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpUniversalDataSystemIntRecordDataEstimateSize() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpUniversalDataSystemIntRecordObjectSetArray() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpUniversalDataSystemIntRecordObjectSetBinary() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpUniversalDataSystemIntRecordObjectSetBool() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpUniversalDataSystemIntRecordObjectSetFloat32() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpUniversalDataSystemIntRecordObjectSetFloat64() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpUniversalDataSystemIntRecordObjectSetInt32() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpUniversalDataSystemIntRecordObjectSetInt64() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpUniversalDataSystemIntRecordObjectSetObject() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpUniversalDataSystemIntRecordObjectSetString() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpUniversalDataSystemIntRecordObjectSetUInt32() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpUniversalDataSystemIntRecordObjectSetUInt64() { + NotImplemented_nid_no_patch(__func__); + return 0; +} + +} diff --git a/core/libs/prx/libSceNpUtility/Export.cpp b/core/libs/prx/libSceNpUtility/Export.cpp index f2f1a6a84a..aae2f22f12 100644 --- a/core/libs/prx/libSceNpUtility/Export.cpp +++ b/core/libs/prx/libSceNpUtility/Export.cpp @@ -124,6 +124,310 @@ int APS5_VABI sceNpBandwidthTestShutdown(int context_id, void* result) { return SCE_NP_ERROR_INVALID_ARGUMENT; } +int APS5_VABI sceNpAppInfoIntAbortRequest() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpAppInfoIntCheckAvailability() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpAppInfoIntCheckAvailabilityA() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpAppInfoIntCheckAvailabilityAll() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpAppInfoIntCheckAvailabilityAllA() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpAppInfoIntCheckServiceAvailability() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpAppInfoIntCheckServiceAvailabilityA() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpAppInfoIntCheckServiceAvailabilityAll() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpAppInfoIntCheckServiceAvailabilityAllA() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpAppInfoIntCreateRequest() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpAppInfoIntDestroyRequest() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpAppInfoIntFinalize() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpAppInfoIntInitialize() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpAppLaunchLink2IntAbortRequest() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpAppLaunchLink2IntCreateRequest() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpAppLaunchLink2IntDestroyRequest() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpAppLaunchLink2IntFinalize() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpAppLaunchLink2IntGetCompatibleTitleIdList() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpAppLaunchLink2IntGetCompatibleTitleIdNum() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpAppLaunchLink2IntInitialize() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpAppLaunchLinkIntAbortRequest() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpAppLaunchLinkIntCreateRequest() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpAppLaunchLinkIntDestroyRequest() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpAppLaunchLinkIntFinalize() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpAppLaunchLinkIntGetCompatibleTitleIdList() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpAppLaunchLinkIntGetCompatibleTitleIdNum() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpAppLaunchLinkIntInitialize() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpBandwidthTestShutdownWithDetailedInfo() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpLookupAbortRequest() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpLookupCreateAsyncRequest() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpLookupCreateTitleCtxA() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpLookupNetAbortRequest() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpLookupNetCensorComment() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpLookupNetConvertJidToNpId() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpLookupNetConvertNpIdToJid() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpLookupNetCreateRequest() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpLookupNetCreateTitleCtx() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpLookupNetDeleteRequest() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpLookupNetDeleteTitleCtx() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpLookupNetInit() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpLookupNetInitWithFunctionPointer() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpLookupNetInitWithMemoryPool() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpLookupNetIsInit() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpLookupNetNpId() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpLookupNetSanitizeComment() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpLookupNetSetTimeout() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpLookupNetTerm() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpLookupPollAsync() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpLookupSetTimeout() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpLookupWaitAsync() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpServiceChecker2IntAbortRequest() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpServiceChecker2IntCreateRequest() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpServiceChecker2IntDestroyRequest() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpServiceChecker2IntFinalize() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpServiceChecker2IntGetServiceAvailability() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpServiceChecker2IntGetServiceAvailabilityA() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpServiceChecker2IntInitialize() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpServiceChecker2IntIsSetServiceType() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpServiceCheckerIntAbortRequest() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpServiceCheckerIntCreateRequest() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpServiceCheckerIntDestroyRequest() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpServiceCheckerIntFinalize() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpServiceCheckerIntGetAvailability() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpServiceCheckerIntGetAvailabilityList() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpServiceCheckerIntInitialize() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpServiceCheckerIntIsCached() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpTitleMetadataIntAbortRequest() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpTitleMetadataIntCreateRequest() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpTitleMetadataIntDeleteRequest() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpTitleMetadataIntGetInfo() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpTitleMetadataIntGetNpTitleId() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpUtilityInit() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpUtilityTerm() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpWordFilterCreateTitleCtxA() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpWordFilterSetTimeout() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceNpWordFilterWaitAsync() { + NotImplemented_nid_no_patch(__func__); + return 0; +} } extern "C" { diff --git a/core/libs/prx/libScePad/Export.cpp b/core/libs/prx/libScePad/Export.cpp index b8099f0eaf..15cc1a4805 100644 --- a/core/libs/prx/libScePad/Export.cpp +++ b/core/libs/prx/libScePad/Export.cpp @@ -110,7 +110,9 @@ constexpr int PAD_ERROR_DEVICE_NO_HANDLE = static_cast(0x80920008); constexpr int PAD_ERROR_ALREADY_OPENED = static_cast(0x80920004); constexpr int PAD_ERROR_UNKNOWN_USER = static_cast(0x809b0081); -bool g_opened = false; +std::array g_opened{false}; +std::array g_everOpened{false}; +bool g_remoteOpened = false; std::array g_specialUsers{}; std::atomic_bool g_initialized{false}; @@ -126,8 +128,14 @@ int SpecialHandleOf(int userId) { return PAD_ERROR_DEVICE_NO_HANDLE; } +int PortOf(int handle) { + const int port = handle - PAD_HANDLE_BASE; + if (port < 0 || port >= PAD_MAX_CONTROLLERS || !g_everOpened[port]) return -1; + return port; +} + bool KnownHandle(int handle) { - return handle == PAD_HANDLE || SpecialHandle(handle); + return PortOf(handle) >= 0 || SpecialHandle(handle); } PadData SpecialData() { @@ -138,10 +146,27 @@ PadData SpecialData() { return data; } +bool SystemRemote(int userId, int type) { + return userId == PAD_USER_ID_SYSTEM && type == PAD_PORT_TYPE_REMOTE; +} + +bool RemoteHandle(int handle) { + return g_remoteOpened && handle == PAD_REMOTE_HANDLE; +} + +int PortForUser(int userId, int type, int index) { + if (index != 0) return -1; + if (type == PAD_PORT_TYPE_STANDARD) { + const int port = userId - 0x10000000; + if (port >= 0 && port < PAD_MAX_CONTROLLERS) return port; + } + return -1; +} + bool ValidPort(int userId, int type, int index) { - const bool personalPort = type == PAD_PORT_TYPE_STANDARD || type == PAD_PORT_TYPE_SPECIAL; - const bool systemRemote = userId == PAD_USER_ID_SYSTEM && type == PAD_PORT_TYPE_REMOTE; - return index == 0 && (personalPort || systemRemote); + if (index != 0) return false; + if (type == PAD_PORT_TYPE_SPECIAL || SystemRemote(userId, type)) return true; + return PortForUser(userId, type, index) >= 0; } } @@ -165,14 +190,17 @@ static_assert(sizeof(PadMotionSensorPosition) == 12); extern "C" { int APS5_VABI scePadClose_nid_postfix(int handle) { + if (RemoteHandle(handle)) { + g_remoteOpened = false; + return PAD_OK; + } if (SpecialHandle(handle)) { g_specialUsers[handle - PAD_SPECIAL_HANDLE] = 0; return PAD_OK; } - if (handle != PAD_HANDLE) { - return PAD_ERROR_INVALID_HANDLE; - } - g_opened = false; + const int port = PortOf(handle); + if (port < 0) return PAD_ERROR_INVALID_HANDLE; + g_opened[port] = false; return PAD_OK; } @@ -233,22 +261,42 @@ int APS5_VABI scePadGetControllerInformation(int handle, PadControllerInformatio int APS5_VABI scePadGetHandle(int user_id, int type, int index) { if (!ValidPort(user_id, type, index)) return PAD_ERROR_DEVICE_NO_HANDLE; if (type == PAD_PORT_TYPE_SPECIAL) return SpecialHandleOf(user_id); - if (!g_opened) return PAD_ERROR_DEVICE_NO_HANDLE; - return PAD_HANDLE; + if (SystemRemote(user_id, type)) return g_remoteOpened ? PAD_REMOTE_HANDLE : PAD_ERROR_DEVICE_NO_HANDLE; + const int port = PortForUser(user_id, type, index); + if (port < 0 || !g_opened[port]) return PAD_ERROR_DEVICE_NO_HANDLE; + return PAD_HANDLE_BASE + port; } int APS5_VABI scePadGetInfo_nid_postfix(PadInfo* info) { if (info == nullptr) return PAD_ERROR_INVALID_ARG; std::memset(info, 0, sizeof(*info)); - info->maxConnectCount = 4; - info->connectedCount[0] = 1; + info->maxConnectCount = PAD_MAX_CONTROLLERS; + std::uint8_t count = 0; + for (int port = 0; port < PAD_MAX_CONTROLLERS; ++port) { + if (Pad::IsConnected(port)) { + PadInfo::PadTypeInfo& slot = info->padTypeInfo[count]; + slot.connectPort = static_cast(port); + slot.status = 1; + slot.deviceType = PAD_DEVICE_TYPE_DUAL_SENSE; + slot.connectType = PAD_CONNECT_TYPE_BLUETOOTH; + std::uint8_t mac[6]; + std::memcpy(mac, PAD_BD_ADDRESS, sizeof(mac)); + mac[5] = static_cast(mac[5] + port); + std::memcpy(slot.bluetoothMacAddress, mac, sizeof(slot.bluetoothMacAddress)); + ++count; + } + } + if (count == 0) { + PadInfo::PadTypeInfo& slot = info->padTypeInfo[0]; + slot.connectPort = 0; + slot.status = 1; + slot.deviceType = PAD_DEVICE_TYPE_DUAL_SENSE; + slot.connectType = PAD_CONNECT_TYPE_BLUETOOTH; + std::memcpy(slot.bluetoothMacAddress, PAD_BD_ADDRESS, sizeof(slot.bluetoothMacAddress)); + count = 1; + } + info->connectedCount[0] = count; info->connectedCount[1] = 0; - PadInfo::PadTypeInfo& slot = info->padTypeInfo[0]; - slot.connectPort = 0; - slot.status = 1; - slot.deviceType = PAD_DEVICE_TYPE_DUAL_SENSE; - slot.connectType = PAD_CONNECT_TYPE_BLUETOOTH; - std::memcpy(slot.bluetoothMacAddress, PAD_BD_ADDRESS, sizeof(slot.bluetoothMacAddress)); return PAD_OK; } @@ -308,8 +356,17 @@ int APS5_VABI scePadOpen_nid_postfix(int userId, int type, int index, const void } return PAD_ERROR_UNKNOWN_USER; } - g_opened = true; - return PAD_HANDLE; + if (SystemRemote(userId, type)) { + g_remoteOpened = true; + return PAD_REMOTE_HANDLE; + } + const int port = PortForUser(userId, type, index); + if (port < 0) { + return PAD_ERROR_INVALID_ARG; + } + g_opened[port] = true; + g_everOpened[port] = true; + return PAD_HANDLE_BASE + port; } int APS5_VABI scePadOpenExt(int userId, int type, int index, const void* param) { @@ -329,20 +386,24 @@ int APS5_VABI scePadSetFeatureReport(std::int32_t handle, std::uint8_t reportId, int APS5_VABI scePadOutputReport(std::int32_t handle, std::uint8_t reportId, const void* data, std::uint64_t size) { NotImplemented_nid_no_patch(__func__); return 0; } int APS5_VABI scePadRead_nid_postfix(int handle, PadData* data, int num) { - if (!KnownHandle(handle)) return PAD_ERROR_INVALID_HANDLE; + if (!KnownHandle(handle) && !RemoteHandle(handle)) return PAD_ERROR_INVALID_HANDLE; if (data == nullptr || num <= 0) return PAD_ERROR_INVALID_ARG; + if (RemoteHandle(handle)) { + data[0] = PadData{}; + return 1; + } if (SpecialHandle(handle)) { data[0] = SpecialData(); return 1; } - return Pad::Read(data, num); + return Pad::Read(data, num, PortOf(handle)); } int APS5_VABI scePadReadState(int handle, PadData* data) { - if (!KnownHandle(handle)) return PAD_ERROR_INVALID_HANDLE; + if (!KnownHandle(handle) && !RemoteHandle(handle)) return PAD_ERROR_INVALID_HANDLE; if (data == nullptr) return PAD_ERROR_INVALID_ARG; - *data = SpecialHandle(handle) ? SpecialData() : Pad::ReadState(); + *data = RemoteHandle(handle) ? PadData{} : SpecialHandle(handle) ? SpecialData() : Pad::ReadState(PortOf(handle)); return 0; } @@ -350,21 +411,21 @@ int APS5_VABI scePadReadState(int handle, PadData* data) { int APS5_VABI scePadResetLightBar(int handle) { if (!KnownHandle(handle)) return PAD_ERROR_INVALID_HANDLE; if (SpecialHandle(handle)) return PAD_OK; - Pad::SetLightBar(false, 0, 0, 0); + Pad::SetLightBar(PortOf(handle), false, 0, 0, 0); return PAD_OK; } int APS5_VABI scePadResetOrientation(int handle) { if (!KnownHandle(handle)) return PAD_ERROR_INVALID_HANDLE; if (SpecialHandle(handle)) return PAD_OK; - Pad::ResetOrientation(); + Pad::ResetOrientation(PortOf(handle)); return PAD_OK; } int APS5_VABI scePadSetAngularVelocityDeadbandState(int handle, bool enable) { if (!KnownHandle(handle)) return PAD_ERROR_INVALID_HANDLE; if (SpecialHandle(handle)) return PAD_OK; - Pad::SetAngularVelocityDeadband(enable); + Pad::SetAngularVelocityDeadband(PortOf(handle), enable); return PAD_OK; } @@ -372,21 +433,21 @@ int APS5_VABI scePadSetLightBar(int handle, const PadLightBarParam* param) { if (!KnownHandle(handle)) return PAD_ERROR_INVALID_HANDLE; if (param == nullptr) return PAD_ERROR_INVALID_ARG; if (SpecialHandle(handle)) return PAD_OK; - Pad::SetLightBar(true, param->r, param->g, param->b); + Pad::SetLightBar(PortOf(handle), true, param->r, param->g, param->b); return PAD_OK; } int APS5_VABI scePadSetMotionSensorState(int handle, bool enable) { if (!KnownHandle(handle)) return PAD_ERROR_INVALID_HANDLE; if (SpecialHandle(handle)) return PAD_OK; - Pad::SetMotionEnabled(enable); + Pad::SetMotionEnabled(PortOf(handle), enable); return PAD_OK; } int APS5_VABI scePadSetTiltCorrectionState(int handle, bool enabled) { if (!KnownHandle(handle)) return PAD_ERROR_INVALID_HANDLE; if (SpecialHandle(handle)) return PAD_OK; - Pad::SetTiltCorrection(enabled); + Pad::SetTiltCorrection(PortOf(handle), enabled); return PAD_OK; } @@ -404,8 +465,9 @@ int APS5_VABI scePadSetTriggerEffect(int handle, const void* param) { if (mode[0] > 6 || mode[1] > 6) { return PAD_ERROR_INVALID_ARG; } + const int port = PortOf(handle); for (int trigger = 0; trigger < 2; ++trigger) { - if ((mask & (1u << trigger)) != 0) Pad::SetTriggerCommand(trigger, bytes + 8 + 56 * trigger); + if ((mask & (1u << trigger)) != 0) Pad::SetTriggerCommand(port, trigger, bytes + 8 + 56 * trigger); } return PAD_OK; } @@ -414,7 +476,7 @@ int APS5_VABI scePadSetVibration(int handle, const PadVibrationParam* param) { if (!KnownHandle(handle)) return PAD_ERROR_INVALID_HANDLE; if (param == nullptr) return PAD_ERROR_INVALID_ARG; if (SpecialHandle(handle)) return PAD_OK; - Pad::SetVibration(param->large_motor, param->small_motor); + Pad::SetVibration(PortOf(handle), param->large_motor, param->small_motor); return PAD_OK; } @@ -424,7 +486,7 @@ int APS5_VABI scePadSetVibrationMode(int handle, int mode) { if (!KnownHandle(handle)) return PAD_ERROR_INVALID_HANDLE; if (mode > 3 && mode != 5) return PAD_ERROR_INVALID_ARG; if (SpecialHandle(handle)) return PAD_OK; - Pad::SetVibrationMode(mode); + Pad::SetVibrationMode(PortOf(handle), mode); return PAD_OK; } @@ -642,8 +704,11 @@ int APS5_VABI scePadGetVersionInfo(std::int32_t handle, PadVersionInfo* info) { } int APS5_VABI scePadGetControllerType(std::int32_t handle, std::int32_t* type) { - NotImplemented_nid_no_patch(__func__); - return 0; + if (!Pad::IsInitialized()) return PAD_ERROR_NOT_INITIALIZED; + if (type == nullptr) return PAD_ERROR_INVALID_ARG; + if (PortOf(handle) < 0) return PAD_ERROR_INVALID_HANDLE; + *type = PAD_CONTROLLER_TYPE_DUAL_SENSE; + return PAD_OK; } int APS5_VABI scePadDisableDataConversion(std::int32_t handle, bool disabled) { diff --git a/core/libs/prx/libScePad/include/Pad.hpp b/core/libs/prx/libScePad/include/Pad.hpp index 135bd54b59..03958f4eed 100644 --- a/core/libs/prx/libScePad/include/Pad.hpp +++ b/core/libs/prx/libScePad/include/Pad.hpp @@ -12,7 +12,10 @@ constexpr int PAD_PORT_TYPE_STANDARD = 0; constexpr int PAD_PORT_TYPE_SPECIAL = 2; constexpr int PAD_PORT_TYPE_REMOTE = 16; constexpr int PAD_USER_ID_SYSTEM = 0xff; +constexpr int PAD_MAX_CONTROLLERS = 4; +constexpr int PAD_HANDLE_BASE = 1; constexpr int PAD_HANDLE = 1; +constexpr int PAD_REMOTE_HANDLE = 0x10; constexpr int PAD_SPECIAL_HANDLE = 0x20; constexpr int PAD_SPECIAL_HANDLE_COUNT = 16; @@ -25,6 +28,8 @@ constexpr int PAD_DEVICE_TYPE_DUAL_SHOCK_4 = 2; constexpr int PAD_DEVICE_TYPE_DUAL_SENSE = 3; constexpr int PAD_DEVICE_TYPE_DUAL_SENSE_EDGE = 4; +constexpr int PAD_CONTROLLER_TYPE_DUAL_SENSE = 2; + constexpr int PAD_CONNECT_TYPE_UNREGISTERED = 0; constexpr int PAD_CONNECT_TYPE_USB = 1; constexpr int PAD_CONNECT_TYPE_BLUETOOTH = 2; diff --git a/core/libs/prx/libScePad/include/PadState.hpp b/core/libs/prx/libScePad/include/PadState.hpp index fb621171ee..aee2c05569 100644 --- a/core/libs/prx/libScePad/include/PadState.hpp +++ b/core/libs/prx/libScePad/include/PadState.hpp @@ -56,23 +56,36 @@ struct PadOutputState { namespace Pad { void Initialize(); bool IsInitialized(); -PadData ReadState(); -int Read(PadData* data, int num); +PadData ReadState(int port = 0); +int Read(PadData* data, int num, int port = 0); +void SetVibration(int port, std::uint8_t large, std::uint8_t small); void SetVibration(std::uint8_t large, std::uint8_t small); +void SetVibrationMode(int port, int mode); void SetVibrationMode(int mode); +void SetLightBar(int port, bool valid, std::uint8_t r, std::uint8_t g, std::uint8_t b); void SetLightBar(bool valid, std::uint8_t r, std::uint8_t g, std::uint8_t b); +void SetTriggerCommand(int port, int trigger, const std::uint8_t* command); void SetTriggerCommand(int trigger, const std::uint8_t* command); -void ResetOrientation(); +void ResetOrientation(int port = 0); +void SetMotionEnabled(int port, bool enabled); void SetMotionEnabled(bool enabled); +void SetTiltCorrection(int port, bool enabled); void SetTiltCorrection(bool enabled); +void SetAngularVelocityDeadband(int port, bool enabled); void SetAngularVelocityDeadband(bool enabled); +void SetConnected(int port, bool connected); +bool IsConnected(int port); } extern "C" void PadPublishInput_nid_postfix(const PadInputState& input); +extern "C" void PadPublishInputForPort_nid_postfix(int port, const PadInputState& input); extern "C" void PadReportInputFailure_nid_postfix(std::exception_ptr error); extern "C" bool PadFetchOutput_nid_postfix(std::uint32_t* seenSequence, PadOutputState* out); +extern "C" bool PadFetchOutputForPort_nid_postfix(int port, std::uint32_t* seenSequence, PadOutputState* out); extern "C" void PadSubmitAudioHaptics_nid_postfix(std::uint8_t large, std::uint8_t small); extern "C" void PadQueueLegacyAudioHaptics_nid_postfix(int source, std::uint64_t begin, std::uint64_t end, std::uint8_t large, std::uint8_t small); extern "C" void PadClearLegacyAudioHaptics_nid_postfix(int source); +extern "C" void PadSubmitAudioHapticsForPort_nid_postfix(int port, std::uint8_t large, std::uint8_t small); +extern "C" void PadSetPortConnected_nid_postfix(int port, bool connected); #endif diff --git a/core/libs/prx/libScePad/src/PadState.cpp b/core/libs/prx/libScePad/src/PadState.cpp index 5a6795f32e..4e1453dbfe 100644 --- a/core/libs/prx/libScePad/src/PadState.cpp +++ b/core/libs/prx/libScePad/src/PadState.cpp @@ -13,47 +13,57 @@ #include namespace { - std::mutex stateMutex; - PadInputState state; - std::uint32_t pressedSinceRead = 0; - std::uint8_t pressedAnalogL2 = 0; - std::uint8_t pressedAnalogR2 = 0; - std::uint64_t pressedAt = 0; + struct Quat { float w = 1.0f, x = 0.0f, y = 0.0f, z = 0.0f; }; constexpr std::uint64_t kLatchExpiry = 100000; - PadOutputState output; - std::chrono::steady_clock::time_point audioHapticEnd; - std::array audioHapticRumble{}; - PadAudioHapticQueue legacyHaptics; - std::uint64_t timestamp = 0; + std::uint64_t injectedTimestamp = 0; struct QueuedInput { PadInputState input; std::uint64_t timestamp = 0; }; constexpr std::size_t kQueueCapacity = 64; - std::deque queue; + + struct PadSlot { + PadInputState state; + PadOutputState output; + std::deque queue; + Quat orientation; + std::uint64_t lastFuseTime = 0; + float biasIntegral[3] = {0.0f, 0.0f, 0.0f}; + bool tiltCorrection = true; + bool angularVelocityDeadband = false; + std::uint8_t nextTouchId = 0; + bool prevTouchActive[2] = {false, false}; + std::uint8_t touchIds[2] = {0, 0}; + std::chrono::steady_clock::time_point audioHapticEnd{}; + std::array audioHapticRumble{}; + PadAudioHapticQueue legacyHaptics; + std::uint64_t timestamp = 0; + std::uint32_t pressedSinceRead = 0; + std::uint8_t pressedAnalogL2 = 0; + std::uint8_t pressedAnalogR2 = 0; + std::uint64_t pressedAt = 0; + bool connected = false; + }; + + std::mutex stateMutex; + std::array slots = [] { + std::array initialSlots{}; + initialSlots[0].connected = true; + return initialSlots; + }(); std::exception_ptr failure; bool initialized = false; - - struct Quat { float w = 1.0f, x = 0.0f, y = 0.0f, z = 0.0f; }; - Quat orientation; - std::uint64_t lastFuseTime = 0; - float biasIntegral[3] = {0.0f, 0.0f, 0.0f}; - bool tiltCorrection = true; - bool angularVelocityDeadband = false; constexpr float kAngularVelocityDeadband = 0.017453292f; - std::uint8_t nextTouchId = 0; - bool prevTouchActive[2] = {false, false}; - std::uint8_t touchIds[2] = {0, 0}; - bool RefreshAudioHaptics() { - if (std::chrono::steady_clock::now() >= audioHapticEnd) audioHapticRumble = {}; - const auto legacy = legacyHaptics.Read(sceKernelGetProcessTime()); - const auto large = std::max(audioHapticRumble[0], legacy[0]); - const auto small = std::max(audioHapticRumble[1], legacy[1]); - if (output.audioVibrationLarge == large && output.audioVibrationSmall == small) return false; - output.audioVibrationLarge = large; - output.audioVibrationSmall = small; + bool RefreshAudioHaptics(PadSlot& slot) { + if (std::chrono::steady_clock::now() >= slot.audioHapticEnd) slot.audioHapticRumble = {}; + const auto legacy = slot.legacyHaptics.Read(sceKernelGetProcessTime()); + const auto large = std::max(slot.audioHapticRumble[0], legacy[0]); + const auto small = std::max(slot.audioHapticRumble[1], legacy[1]); + if (slot.output.audioVibrationLarge == large && slot.output.audioVibrationSmall == small) return false; + slot.output.audioVibrationLarge = large; + slot.output.audioVibrationSmall = small; return true; } @@ -63,33 +73,33 @@ namespace { q.w /= n; q.x /= n; q.y /= n; q.z /= n; } - void fuse(float dt, const std::array& accel, const std::array& gyro) { + void fuse(PadSlot& slot, float dt, const std::array& accel, const std::array& gyro) { float gx = gyro[0], gy = gyro[1], gz = gyro[2]; const float an = std::sqrt(accel[0] * accel[0] + accel[1] * accel[1] + accel[2] * accel[2]); - if (tiltCorrection && an > 1.0f) { + if (slot.tiltCorrection && an > 1.0f) { const float ax = accel[0] / an, ay = accel[1] / an, az = accel[2] / an; - const float vx = 2.0f * (orientation.x * orientation.y + orientation.w * orientation.z); - const float vy = orientation.w * orientation.w - orientation.x * orientation.x + orientation.y * orientation.y - orientation.z * orientation.z; - const float vz = 2.0f * (orientation.y * orientation.z - orientation.w * orientation.x); + const float vx = 2.0f * (slot.orientation.x * slot.orientation.y + slot.orientation.w * slot.orientation.z); + const float vy = slot.orientation.w * slot.orientation.w - slot.orientation.x * slot.orientation.x + slot.orientation.y * slot.orientation.y - slot.orientation.z * slot.orientation.z; + const float vz = 2.0f * (slot.orientation.y * slot.orientation.z - slot.orientation.w * slot.orientation.x); const float ex = ay * vz - az * vy; const float ey = az * vx - ax * vz; const float ez = ax * vy - ay * vx; constexpr float kp = 1.5f; constexpr float ki = 0.02f; - biasIntegral[0] += ki * ex * dt; - biasIntegral[1] += ki * ey * dt; - biasIntegral[2] += ki * ez * dt; - gx += kp * ex + biasIntegral[0]; - gy += kp * ey + biasIntegral[1]; - gz += kp * ez + biasIntegral[2]; + slot.biasIntegral[0] += ki * ex * dt; + slot.biasIntegral[1] += ki * ey * dt; + slot.biasIntegral[2] += ki * ez * dt; + gx += kp * ex + slot.biasIntegral[0]; + gy += kp * ey + slot.biasIntegral[1]; + gz += kp * ez + slot.biasIntegral[2]; } const float hx = 0.5f * gx * dt, hy = 0.5f * gy * dt, hz = 0.5f * gz * dt; - const Quat q = orientation; - orientation.w = q.w - q.x * hx - q.y * hy - q.z * hz; - orientation.x = q.x + q.w * hx + q.y * hz - q.z * hy; - orientation.y = q.y + q.w * hy - q.x * hz + q.z * hx; - orientation.z = q.z + q.w * hz + q.x * hy - q.y * hx; - normalize(orientation); + const Quat q = slot.orientation; + slot.orientation.w = q.w - q.x * hx - q.y * hy - q.z * hz; + slot.orientation.x = q.x + q.w * hx + q.y * hz - q.z * hy; + slot.orientation.y = q.y + q.w * hy - q.x * hz + q.z * hx; + slot.orientation.z = q.z + q.w * hz + q.x * hy - q.y * hx; + normalize(slot.orientation); } void encodeZones(std::uint8_t* dst, std::uint8_t mode, const std::uint8_t* strengths, int count, std::uint8_t frequency) { @@ -124,10 +134,13 @@ void Pad::Initialize() { std::lock_guard lock(stateMutex); if (failure) std::rethrow_exception(failure); if (!initialized) { - timestamp = sceKernelGetProcessTime(); - lastFuseTime = timestamp; - queue.clear(); - pressedSinceRead = 0; + const std::uint64_t now = sceKernelGetProcessTime(); + for (std::size_t i = 0; i < slots.size(); ++i) { + slots[i].timestamp = now; + slots[i].lastFuseTime = now; + slots[i].queue.clear(); + slots[i].pressedSinceRead = 0; + } } initialized = true; } @@ -138,7 +151,7 @@ bool Pad::IsInitialized() { } namespace { -PadData BuildData(const PadInputState& state, std::uint64_t changeTime, std::uint64_t now, const Pad::InputCommandFile::Input& injected) { +PadData BuildData(PadSlot& slot, const PadInputState& state, std::uint64_t changeTime, std::uint64_t now, const Pad::InputCommandFile::Input& injected, int port) { PadData data{}; data.buttons = state.buttons | injected.buttons; const auto& sticks = injected.hasSticks ? injected.sticks : state.sticks; @@ -152,28 +165,28 @@ PadData BuildData(const PadInputState& state, std::uint64_t changeTime, std::uin data.analog_buttons_l2 = trigger(state.analogButtonsL2, 0x100); data.analog_buttons_r2 = trigger(state.analogButtonsR2, 0x200); - const bool live = state.hasMotion && output.motionEnabled; + const bool live = state.hasMotion && slot.output.motionEnabled; const std::array rest{0.0f, 9.80665f, 0.0f}; const std::array& accel = live ? state.accel : rest; std::array gyro = live ? state.gyro : std::array{0.0f, 0.0f, 0.0f}; - if (angularVelocityDeadband) { + if (slot.angularVelocityDeadband) { for (float& axis : gyro) { if (axis < kAngularVelocityDeadband && axis > -kAngularVelocityDeadband) axis = 0.0f; } } if (live) { - float dt = lastFuseTime != 0 && now > lastFuseTime ? static_cast(now - lastFuseTime) * 1e-6f : 0.0f; + float dt = slot.lastFuseTime != 0 && now > slot.lastFuseTime ? static_cast(now - slot.lastFuseTime) * 1e-6f : 0.0f; dt = std::min(dt, 0.1f); - fuse(dt, accel, gyro); + fuse(slot, dt, accel, gyro); } else { - orientation = Quat{}; - biasIntegral[0] = biasIntegral[1] = biasIntegral[2] = 0.0f; + slot.orientation = Quat{}; + slot.biasIntegral[0] = slot.biasIntegral[1] = slot.biasIntegral[2] = 0.0f; } - lastFuseTime = now; - data.orientation_x = orientation.x; - data.orientation_y = orientation.y; - data.orientation_z = orientation.z; - data.orientation_w = orientation.w; + slot.lastFuseTime = now; + data.orientation_x = slot.orientation.x; + data.orientation_y = slot.orientation.y; + data.orientation_z = slot.orientation.z; + data.orientation_w = slot.orientation.w; constexpr float kGravity = 9.80665f; data.acceleration_x = accel[0] / kGravity; data.acceleration_y = accel[1] / kGravity; @@ -182,20 +195,24 @@ PadData BuildData(const PadInputState& state, std::uint64_t changeTime, std::uin data.angular_velocity_y = gyro[1]; data.angular_velocity_z = gyro[2]; - data.connected = true; - data.connected_count = 1; + std::uint8_t connectedCount = 0; + for (const auto& s : slots) { + if (s.connected) ++connectedCount; + } + data.connected = slot.connected; + data.connected_count = connectedCount > 0 ? connectedCount : 1; data.timestamp = changeTime; std::uint8_t touchNum = 0; for (int i = 0; i < 2; ++i) { const PadTouchPoint& tp = state.touch[i]; - if (!tp.active) { prevTouchActive[i] = false; continue; } - if (!prevTouchActive[i]) touchIds[i] = static_cast(nextTouchId++ & 0x7F); - prevTouchActive[i] = true; + if (!tp.active) { slot.prevTouchActive[i] = false; continue; } + if (!slot.prevTouchActive[i]) slot.touchIds[i] = static_cast(slot.nextTouchId++ & 0x7F); + slot.prevTouchActive[i] = true; if (touchNum == 0) { - data.touch_data_touch0_x = tp.x; data.touch_data_touch0_y = tp.y; data.touch_data_touch0_id = touchIds[i]; + data.touch_data_touch0_x = tp.x; data.touch_data_touch0_y = tp.y; data.touch_data_touch0_id = slot.touchIds[i]; } else { - data.touch_data_touch1_x = tp.x; data.touch_data_touch1_y = tp.y; data.touch_data_touch1_id = touchIds[i]; + data.touch_data_touch1_x = tp.x; data.touch_data_touch1_y = tp.y; data.touch_data_touch1_id = slot.touchIds[i]; } ++touchNum; } @@ -223,66 +240,85 @@ Pad::InputCommandFile::Input InjectedInput(std::uint64_t now) { const auto injected = commands.Poll(now); if (injected != lastInjected) { lastInjected = injected; - timestamp = now; + slots[0].timestamp = now; + injectedTimestamp = now; } return injected; } } -PadData Pad::ReadState() { +PadData Pad::ReadState(int port) { + if (port < 0 || port >= PAD_MAX_CONTROLLERS) throw std::invalid_argument("Pad: invalid port"); std::lock_guard lock(stateMutex); if (failure) std::rethrow_exception(failure); if (!initialized) throw std::runtime_error("Pad: read before initialization"); const std::uint64_t now = sceKernelGetProcessTime(); - const auto injected = InjectedInput(now); - PadData data = BuildData(state, timestamp, now, injected); - if (pressedSinceRead != 0 && now - pressedAt <= kLatchExpiry) { - data.buttons |= pressedSinceRead; - if (pressedSinceRead & 0x100u) data.analog_buttons_l2 = std::max(data.analog_buttons_l2, pressedAnalogL2); - if (pressedSinceRead & 0x200u) data.analog_buttons_r2 = std::max(data.analog_buttons_r2, pressedAnalogR2); + const auto injected = port == 0 ? InjectedInput(now) : Pad::InputCommandFile::Input{}; + auto& slot = slots[port]; + PadData data = BuildData(slot, slot.state, slot.timestamp, now, injected, port); + if (slot.pressedSinceRead != 0 && now - slot.pressedAt <= kLatchExpiry) { + data.buttons |= slot.pressedSinceRead; + if (slot.pressedSinceRead & 0x100u) data.analog_buttons_l2 = std::max(data.analog_buttons_l2, slot.pressedAnalogL2); + if (slot.pressedSinceRead & 0x200u) data.analog_buttons_r2 = std::max(data.analog_buttons_r2, slot.pressedAnalogR2); } - pressedSinceRead = 0; + slot.pressedSinceRead = 0; return data; } -int Pad::Read(PadData* data, int num) { +int Pad::Read(PadData* data, int num, int port) { if (data == nullptr || num <= 0) throw std::invalid_argument("Pad: invalid read buffer"); + if (port < 0 || port >= PAD_MAX_CONTROLLERS) throw std::invalid_argument("Pad: invalid port"); std::lock_guard lock(stateMutex); if (failure) std::rethrow_exception(failure); if (!initialized) throw std::runtime_error("Pad: read before initialization"); - pressedSinceRead = 0; + auto& slot = slots[port]; + slot.pressedSinceRead = 0; const std::uint64_t now = sceKernelGetProcessTime(); - const auto injected = InjectedInput(now); - if (queue.empty()) { - data[0] = BuildData(state, timestamp, now, injected); + const auto injected = port == 0 ? InjectedInput(now) : Pad::InputCommandFile::Input{}; + if (slot.queue.empty()) { + data[0] = BuildData(slot, slot.state, slot.timestamp, now, injected, port); return 1; } int count = 0; - while (count < num && !queue.empty()) { - const QueuedInput& entry = queue.front(); - data[count++] = BuildData(entry.input, entry.timestamp, now, injected); - queue.pop_front(); + while (count < num && !slot.queue.empty()) { + const QueuedInput& entry = slot.queue.front(); + data[count++] = BuildData(slot, entry.input, port == 0 ? std::max(entry.timestamp, injectedTimestamp) : entry.timestamp, now, injected, port); + slot.queue.pop_front(); } return count; } -void Pad::SetVibration(std::uint8_t large, std::uint8_t small) { +void Pad::SetVibration(int port, std::uint8_t large, std::uint8_t small) { + if (port < 0 || port >= PAD_MAX_CONTROLLERS) return; std::lock_guard lock(stateMutex); + auto& output = slots[port].output; if (output.vibrationLarge == large && output.vibrationSmall == small) return; output.vibrationLarge = large; output.vibrationSmall = small; ++output.sequence; } -void Pad::SetVibrationMode(int mode) { +void Pad::SetVibration(std::uint8_t large, std::uint8_t small) { + SetVibration(0, large, small); +} + +void Pad::SetVibrationMode(int port, int mode) { + if (port < 0 || port >= PAD_MAX_CONTROLLERS) return; std::lock_guard lock(stateMutex); + auto& output = slots[port].output; if (output.vibrationMode == mode) return; output.vibrationMode = mode; ++output.sequence; } -void Pad::SetLightBar(bool valid, std::uint8_t r, std::uint8_t g, std::uint8_t b) { +void Pad::SetVibrationMode(int mode) { + SetVibrationMode(0, mode); +} + +void Pad::SetLightBar(int port, bool valid, std::uint8_t r, std::uint8_t g, std::uint8_t b) { + if (port < 0 || port >= PAD_MAX_CONTROLLERS) return; std::lock_guard lock(stateMutex); + auto& output = slots[port].output; output.lightBarValid = valid; output.lightBar[0] = r; output.lightBar[1] = g; @@ -290,8 +326,12 @@ void Pad::SetLightBar(bool valid, std::uint8_t r, std::uint8_t g, std::uint8_t b ++output.sequence; } -void Pad::SetTriggerCommand(int trigger, const std::uint8_t* command) { - if (trigger < 0 || trigger > 1 || command == nullptr) return; +void Pad::SetLightBar(bool valid, std::uint8_t r, std::uint8_t g, std::uint8_t b) { + SetLightBar(0, valid, r, g, b); +} + +void Pad::SetTriggerCommand(int port, int trigger, const std::uint8_t* command) { + if (port < 0 || port >= PAD_MAX_CONTROLLERS || trigger < 0 || trigger > 1 || command == nullptr) return; std::uint32_t mode = 0; std::memcpy(&mode, command, 4); const std::uint8_t* p = command + 8; @@ -348,55 +388,95 @@ void Pad::SetTriggerCommand(int trigger, const std::uint8_t* command) { req.valid = req.effect[0] != 0x05; if (!req.valid) req.fallback = 0; std::lock_guard lock(stateMutex); + auto& output = slots[port].output; output.trigger[trigger] = req; output.triggerTouched = true; ++output.sequence; } -void Pad::ResetOrientation() { +void Pad::SetTriggerCommand(int trigger, const std::uint8_t* command) { + SetTriggerCommand(0, trigger, command); +} + +void Pad::ResetOrientation(int port) { + if (port < 0 || port >= PAD_MAX_CONTROLLERS) return; std::lock_guard lock(stateMutex); - orientation = Quat{}; - biasIntegral[0] = biasIntegral[1] = biasIntegral[2] = 0.0f; + slots[port].orientation = Quat{}; + slots[port].biasIntegral[0] = slots[port].biasIntegral[1] = slots[port].biasIntegral[2] = 0.0f; +} + +void Pad::SetTiltCorrection(int port, bool enabled) { + if (port < 0 || port >= PAD_MAX_CONTROLLERS) return; + std::lock_guard lock(stateMutex); + slots[port].tiltCorrection = enabled; + slots[port].biasIntegral[0] = slots[port].biasIntegral[1] = slots[port].biasIntegral[2] = 0.0f; } void Pad::SetTiltCorrection(bool enabled) { + SetTiltCorrection(0, enabled); +} + +void Pad::SetAngularVelocityDeadband(int port, bool enabled) { + if (port < 0 || port >= PAD_MAX_CONTROLLERS) return; std::lock_guard lock(stateMutex); - tiltCorrection = enabled; - biasIntegral[0] = biasIntegral[1] = biasIntegral[2] = 0.0f; + slots[port].angularVelocityDeadband = enabled; } void Pad::SetAngularVelocityDeadband(bool enabled) { - std::lock_guard lock(stateMutex); - angularVelocityDeadband = enabled; + SetAngularVelocityDeadband(0, enabled); } -void Pad::SetMotionEnabled(bool enabled) { +void Pad::SetMotionEnabled(int port, bool enabled) { + if (port < 0 || port >= PAD_MAX_CONTROLLERS) return; std::lock_guard lock(stateMutex); + auto& output = slots[port].output; if (output.motionEnabled == enabled) return; output.motionEnabled = enabled; ++output.sequence; } -extern "C" void PadPublishInput_nid_postfix(const PadInputState& input) { +void Pad::SetMotionEnabled(bool enabled) { + SetMotionEnabled(0, enabled); +} + +void Pad::SetConnected(int port, bool connected) { + if (port < 0 || port >= PAD_MAX_CONTROLLERS) return; + std::lock_guard lock(stateMutex); + slots[port].connected = connected; +} + +bool Pad::IsConnected(int port) { + if (port < 0 || port >= PAD_MAX_CONTROLLERS) return false; + std::lock_guard lock(stateMutex); + return slots[port].connected; +} + +extern "C" void PadPublishInputForPort_nid_postfix(int port, const PadInputState& input) { + if (port < 0 || port >= PAD_MAX_CONTROLLERS) return; std::lock_guard lock(stateMutex); if (failure) std::rethrow_exception(failure); - if (state.buttons == input.buttons && state.sticks == input.sticks && - state.analogButtonsL2 == input.analogButtonsL2 && state.analogButtonsR2 == input.analogButtonsR2 && - state.touchLeft == input.touchLeft && state.touchRight == input.touchRight && - state.hasMotion == input.hasMotion && state.accel == input.accel && state.gyro == input.gyro && - state.touch == input.touch && state.deviceKind == input.deviceKind) return; - const std::uint32_t pressed = input.buttons & ~state.buttons; - timestamp = sceKernelGetProcessTime(); + auto& slot = slots[port]; + if (slot.state.buttons == input.buttons && slot.state.sticks == input.sticks && + slot.state.analogButtonsL2 == input.analogButtonsL2 && slot.state.analogButtonsR2 == input.analogButtonsR2 && + slot.state.touchLeft == input.touchLeft && slot.state.touchRight == input.touchRight && + slot.state.hasMotion == input.hasMotion && slot.state.accel == input.accel && slot.state.gyro == input.gyro && + slot.state.touch == input.touch && slot.state.deviceKind == input.deviceKind) return; + const std::uint32_t pressed = input.buttons & ~slot.state.buttons; + slot.timestamp = sceKernelGetProcessTime(); if (pressed != 0) { - if (pressedSinceRead != 0 && timestamp - pressedAt > kLatchExpiry) pressedSinceRead = 0; - pressedSinceRead |= pressed; - pressedAt = timestamp; - if (pressed & 0x100u) pressedAnalogL2 = input.analogButtonsL2; - if (pressed & 0x200u) pressedAnalogR2 = input.analogButtonsR2; + if (slot.pressedSinceRead != 0 && slot.timestamp - slot.pressedAt > kLatchExpiry) slot.pressedSinceRead = 0; + slot.pressedSinceRead |= pressed; + slot.pressedAt = slot.timestamp; + if (pressed & 0x100u) slot.pressedAnalogL2 = input.analogButtonsL2; + if (pressed & 0x200u) slot.pressedAnalogR2 = input.analogButtonsR2; } - state = input; - if (queue.size() == kQueueCapacity) queue.pop_front(); - queue.push_back({input, timestamp}); + slot.state = input; + if (slot.queue.size() == kQueueCapacity) slot.queue.pop_front(); + slot.queue.push_back({input, slot.timestamp}); +} + +extern "C" void PadPublishInput_nid_postfix(const PadInputState& input) { + PadPublishInputForPort_nid_postfix(0, input); } extern "C" void PadReportInputFailure_nid_postfix(std::exception_ptr error) { @@ -405,35 +485,51 @@ extern "C" void PadReportInputFailure_nid_postfix(std::exception_ptr error) { if (!failure) failure = error; } -extern "C" bool PadFetchOutput_nid_postfix(std::uint32_t* seenSequence, PadOutputState* out) { +extern "C" bool PadFetchOutputForPort_nid_postfix(int port, std::uint32_t* seenSequence, PadOutputState* out) { + if (port < 0 || port >= PAD_MAX_CONTROLLERS) return false; std::lock_guard lock(stateMutex); - if (RefreshAudioHaptics()) ++output.sequence; - if (seenSequence == nullptr || out == nullptr || *seenSequence == output.sequence) return false; - *seenSequence = output.sequence; - *out = output; + auto& slot = slots[port]; + if (RefreshAudioHaptics(slot)) ++slot.output.sequence; + if (seenSequence == nullptr || out == nullptr || *seenSequence == slot.output.sequence) return false; + *seenSequence = slot.output.sequence; + *out = slot.output; return true; } -extern "C" void PadSubmitAudioHaptics_nid_postfix(std::uint8_t large, std::uint8_t small) { +extern "C" bool PadFetchOutput_nid_postfix(std::uint32_t* seenSequence, PadOutputState* out) { + return PadFetchOutputForPort_nid_postfix(0, seenSequence, out); +} + +extern "C" void PadSubmitAudioHapticsForPort_nid_postfix(int port, std::uint8_t large, std::uint8_t small) { + if (port < 0 || port >= PAD_MAX_CONTROLLERS) return; std::lock_guard lock(stateMutex); if (large == 0 && small == 0) return; const auto now = std::chrono::steady_clock::now(); - if (now >= audioHapticEnd) audioHapticRumble = {}; - audioHapticRumble[0] = std::max(audioHapticRumble[0], large); - audioHapticRumble[1] = std::max(audioHapticRumble[1], small); - audioHapticEnd = now + std::chrono::milliseconds(80); - static_cast(RefreshAudioHaptics()); - ++output.sequence; + auto& slot = slots[port]; + if (now >= slot.audioHapticEnd) slot.audioHapticRumble = {}; + slot.audioHapticRumble[0] = std::max(slot.audioHapticRumble[0], large); + slot.audioHapticRumble[1] = std::max(slot.audioHapticRumble[1], small); + slot.audioHapticEnd = now + std::chrono::milliseconds(80); + static_cast(RefreshAudioHaptics(slot)); + ++slot.output.sequence; } extern "C" void PadQueueLegacyAudioHaptics_nid_postfix(int source, std::uint64_t begin, std::uint64_t end, std::uint8_t large, std::uint8_t small) { std::lock_guard lock(stateMutex); - legacyHaptics.Queue(source, begin, end, large, small); - if (RefreshAudioHaptics()) ++output.sequence; + slots[0].legacyHaptics.Queue(source, begin, end, large, small); + if (RefreshAudioHaptics(slots[0])) ++slots[0].output.sequence; } extern "C" void PadClearLegacyAudioHaptics_nid_postfix(int source) { std::lock_guard lock(stateMutex); - legacyHaptics.Clear(source); - if (RefreshAudioHaptics()) ++output.sequence; + slots[0].legacyHaptics.Clear(source); + if (RefreshAudioHaptics(slots[0])) ++slots[0].output.sequence; +} + +extern "C" void PadSubmitAudioHaptics_nid_postfix(std::uint8_t large, std::uint8_t small) { + PadSubmitAudioHapticsForPort_nid_postfix(0, large, small); +} + +extern "C" void PadSetPortConnected_nid_postfix(int port, bool connected) { + Pad::SetConnected(port, connected); } diff --git a/core/libs/prx/libSceSysCore/Export.cpp b/core/libs/prx/libSceSysCore/Export.cpp index f06a188fd8..de8a605acc 100644 --- a/core/libs/prx/libSceSysCore/Export.cpp +++ b/core/libs/prx/libSceSysCore/Export.cpp @@ -16,4 +16,288 @@ int APS5_VABI sceApplicationGetAppInfoByAppId(std::uint32_t applicationId, Appli return 0; } +int APS5_VABI _sceApplicationGetAppId() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationAddProcess2() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationBeginSclkChange() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationBlockingKill2() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationBlockingKill3() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationContinue() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationCrashSyscore() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationCrashSystemProcess() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationEndSclkChange() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationExitSpawn() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationExitSpawn2() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationFinalize() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationGetAppPackageInfoForCoredump() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationGetCompiledSdkVersionByAppId() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationGetCoredumpDirAndFileName() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationGetCoredumpState() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationGetCpuUsage() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationGetCpuUsage2() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationGetDbgExecutablePath() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationGetMode() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationGetProcs() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationGetShellCoreAppId() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationGetStateForDebugger() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationInitialize() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationInitializeForShellCore() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationIsCrashReportBeingProcessed() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationIsPrimaryProcess() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationIsResumable() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationIsSuspendable() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationKickCoredump() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationKickCoredump2() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationKill() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationLocalProcessKill() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationLocalProcessKill2() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationLocalProcessResume() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationLocalProcessSuspend() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationNotifyCoredumpRequestEnd() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationNotifyCoredumpRequestProgress() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationNotifyVshMainOnStandby() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationNotifyVshReady() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationOnAllResourceReleased() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationRaiseException() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationRaiseExceptionToLocalPid() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationRaiseExceptionToPid() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationRequestToChangeRenderingMode() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationRestore() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationResume() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationSave() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationSendDebugSpawnResult2() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationSendResultOfDebuggerKillRequest() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationSendResultOfDebuggerResumeRequest() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationSendResultOfDebuggerSuspendRequest() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationSendResultOfDebuggerTitleIdLaunchRequest() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationSetApplicationFocus() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationSetControllerFocus() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationSetControllerFocusPermissionToSubProcess() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationSetMemoryPstate() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationSignalShellCoreHeartBeat() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationSpawn2() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationSuspend() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationSuspendSystemForDebugger() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationSwitchToBaseMode() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationSwitchToNeoMode() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationSwitchToNeoMode2() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationSystemReboot() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationSystemShutdown2() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationSystemSuspend() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplicationSystemSuspend2() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceApplictionGetStateForDebugger() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceSysCoreGetEventCount() { + NotImplemented_nid_no_patch(__func__); + return 0; +} +int APS5_VABI sceSysCoreReceiveEvent() { + NotImplemented_nid_no_patch(__func__); + return 0; +} } diff --git a/core/libs/prx/libSceSystemService/CMakeLists.txt b/core/libs/prx/libSceSystemService/CMakeLists.txt index 20a06f0cd9..02c5161b32 100644 --- a/core/libs/prx/libSceSystemService/CMakeLists.txt +++ b/core/libs/prx/libSceSystemService/CMakeLists.txt @@ -98,6 +98,29 @@ if(BUILD_TESTING) set_tests_properties(guest_system_service_time_zone PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$") endif() + add_test_executable(guest_system_service_app_title_id_tests ${LIBS_INCLUDE_DIR}/tests/GuestSystemServiceAppTitleId.cpp) + target_include_directories(guest_system_service_app_title_id_tests PRIVATE ${LIBS_INCLUDE_DIR}) + target_link_libraries(guest_system_service_app_title_id_tests PRIVATE libSceSystemService libkernel libc) + configure_windows_unwind(guest_system_service_app_title_id_tests) + foreach(appTitleIdCase IN ITEMS known:PPSA00000 long:PPSA000000 missing:none) + string(REPLACE ":" ";" appTitleIdCase ${appTitleIdCase}) + list(GET appTitleIdCase 0 appTitleIdName) + list(GET appTitleIdCase 1 appTitleIdValue) + set(appTitleIdRun ${CMAKE_CURRENT_BINARY_DIR}/guest_system_service_app_title_id_${appTitleIdName}) + if(appTitleIdName STREQUAL "missing") + file(MAKE_DIRECTORY ${appTitleIdRun}) + else() + file(WRITE ${appTitleIdRun}/app0/sce_sys/param.json + "{\"titleId\":\"${appTitleIdValue}\",\"localizedParameters\":{\"en-US\":{\"titleName\":\"Example\"}}}") + endif() + add_test(NAME guest_system_service_app_title_id_${appTitleIdName} COMMAND guest_system_service_app_title_id_tests ${appTitleIdName} + WORKING_DIRECTORY ${appTitleIdRun}) + set_tests_properties(guest_system_service_app_title_id_${appTitleIdName} PROPERTIES TIMEOUT 20) + if(WIN32) + set_tests_properties(guest_system_service_app_title_id_${appTitleIdName} PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$;PATH=path_list_prepend:$") + endif() + endforeach() add_test_executable(guest_system_kill_app_tests ${LIBS_INCLUDE_DIR}/tests/GuestSystemKillApp.cpp) target_include_directories(guest_system_kill_app_tests PRIVATE ${LIBS_INCLUDE_DIR}) target_link_libraries(guest_system_kill_app_tests PRIVATE libSceSystemService libc) diff --git a/core/libs/prx/libSceSystemService/Export.cpp b/core/libs/prx/libSceSystemService/Export.cpp index ad7c9d9937..64d0bf5dc1 100644 --- a/core/libs/prx/libSceSystemService/Export.cpp +++ b/core/libs/prx/libSceSystemService/Export.cpp @@ -26,6 +26,7 @@ #include "LoadExec.hpp" #include "prx/libc/include/General.hpp" #include "prx/libkernel/KernelErrors.hpp" +#include "prx/libkernel/AppMetadata/include/AppMetadata.hpp" #include "prx/libSceSystemService/SystemService.hpp" #include "prx/libSceAgcDriver/Execution/include/HostHdr.hpp" @@ -243,6 +244,21 @@ HostLaunch PrepareLaunch(const char*, const char* const*) { void StartHostExecutable(HostLaunch&) {} #endif +constexpr std::size_t TitleIdBufferSize = 10; + +std::string RunningTitleId() { + const AppTitleId titleId = GetAppTitleId_nid_postfix(); + return std::string(titleId.value); +} + +void CopyRunningTitleId(const std::string& caller, bool running, char* destination) { + if (!running) NotImplemented_nid_no_patch((caller + ": application other than the running title").c_str()); + if (destination == nullptr) NotImplemented_nid_no_patch((caller + ": null title ID buffer").c_str()); + const std::string titleId = RunningTitleId(); + if (titleId.size() >= TitleIdBufferSize) NotImplemented_nid_no_patch((caller + ": title ID that does not fit 10 bytes").c_str()); + std::memcpy(destination, titleId.c_str(), titleId.size() + 1); +} + } extern "C" { @@ -445,6 +461,26 @@ int APS5_VABI sceSystemServiceGetAppIdOfRunningBigApp(void) { return SYSTEM_SERVICE_RUNNING_APP_ID; } +int APS5_VABI sceSystemServiceGetAppId(const char* titleId) { + if (titleId == nullptr) { + NotImplemented_nid_no_patch("sceSystemServiceGetAppId: null title ID"); + } + if (RunningTitleId() != titleId) { + NotImplemented_nid_no_patch("sceSystemServiceGetAppId: title other than the running title"); + } + return SYSTEM_SERVICE_RUNNING_APP_ID; +} + +int APS5_VABI sceSystemServiceGetAppTitleId(int appId, char* titleId) { + CopyRunningTitleId("sceSystemServiceGetAppTitleId", appId == SYSTEM_SERVICE_RUNNING_APP_ID, titleId); + return SYSTEM_SERVICE_OK; +} + +int APS5_VABI sceLncUtilGetAppTitleId(std::int32_t appId, char* titleId) { + CopyRunningTitleId("sceLncUtilGetAppTitleId", appId == SYSTEM_SERVICE_RUNNING_APP_ID, titleId); + return SYSTEM_SERVICE_OK; +} + int APS5_VABI sceSystemServiceKillApp(int appId, int how, int reason, int coreDump) { if (appId != SYSTEM_SERVICE_RUNNING_APP_ID) { NotImplemented_nid_no_patch("sceSystemServiceKillApp: application other than the running title"); diff --git a/core/libs/prx/libSceSystemService/PendingExports.cpp b/core/libs/prx/libSceSystemService/PendingExports.cpp index 9fbb45de38..85bb9527ea 100644 --- a/core/libs/prx/libSceSystemService/PendingExports.cpp +++ b/core/libs/prx/libSceSystemService/PendingExports.cpp @@ -386,11 +386,6 @@ int APS5_VABI sceLncUtilGetAppStatusListForShellUIReboot(LncShellUiAppStatus* st return 0; } -int APS5_VABI sceLncUtilGetAppTitleId(std::int32_t appId, char* titleId) { - NotImplemented_nid_no_patch(__func__); - return 0; -} - int APS5_VABI sceLncUtilGetAppType(std::int32_t appId, std::uint32_t* type) { NotImplemented_nid_no_patch(__func__); return 0; @@ -1957,11 +1952,6 @@ int APS5_VABI sceSystemServiceGetAppFocusedAppStatus(SystemServiceAppStatus* sta return 0; } -int APS5_VABI sceSystemServiceGetAppId(const char* titleId) { - NotImplemented_nid_no_patch(__func__); - return 0; -} - int APS5_VABI sceSystemServiceGetAppIdListOfBigApp(std::int32_t* appIds, std::uint32_t capacity, std::uint32_t* count) { NotImplemented_nid_no_patch(__func__); return 0; @@ -1977,11 +1967,6 @@ int APS5_VABI sceSystemServiceGetAppStatus(SystemServiceAppStatus* status) { return 0; } -int APS5_VABI sceSystemServiceGetAppTitleId(std::int32_t appId, char* titleId) { - NotImplemented_nid_no_patch(__func__); - return 0; -} - int APS5_VABI sceSystemServiceGetAppType(std::int32_t appId, std::int32_t* type) { NotImplemented_nid_no_patch(__func__); return 0; diff --git a/core/libs/prx/libSceUlt/Export.cpp b/core/libs/prx/libSceUlt/Export.cpp index f876cfba21..cc3c25c60a 100644 --- a/core/libs/prx/libSceUlt/Export.cpp +++ b/core/libs/prx/libSceUlt/Export.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include @@ -818,7 +819,8 @@ int APS5_VABI sceUltUlthreadCreate(void* ulthread, const char* name, UltUlthread std::memset(ulthread, 0, 512); gUlthreads[ulthread] = state; } - const int result = scePthreadCreate(&state->_thread, nullptr, ulthreadRunner, state.get(), name != nullptr ? name : ""); + const std::string threadName = name != nullptr ? std::string(name).substr(0, 31) : std::string(); + const int result = scePthreadCreate(&state->_thread, nullptr, ulthreadRunner, state.get(), threadName.c_str()); if (result != 0) { std::lock_guard lock(gMutex); gUlthreads.erase(ulthread); diff --git a/core/libs/prx/libSceUsbStorage/CMakeLists.txt b/core/libs/prx/libSceUsbStorage/CMakeLists.txt new file mode 100644 index 0000000000..a4280361de --- /dev/null +++ b/core/libs/prx/libSceUsbStorage/CMakeLists.txt @@ -0,0 +1,16 @@ +add_library(libSceUsbStorage SHARED EXCLUDE_FROM_ALL Export.cpp) +target_include_directories(libSceUsbStorage PRIVATE ${LIBS_INCLUDE_DIR}) +target_link_libraries(libSceUsbStorage PRIVATE libc) +configure_windows_unwind(libSceUsbStorage) + +if(BUILD_TESTING) + add_test_executable(guest_usb_storage_tests ${LIBS_INCLUDE_DIR}/tests/GuestUsbStorage.cpp) + target_include_directories(guest_usb_storage_tests PRIVATE ${LIBS_INCLUDE_DIR}) + target_link_libraries(guest_usb_storage_tests PRIVATE libSceUsbStorage libc) + configure_windows_unwind(guest_usb_storage_tests) + add_test(NAME guest_usb_storage COMMAND guest_usb_storage_tests) + if(WIN32) + set_tests_properties(guest_usb_storage PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + endif() +endif() diff --git a/core/libs/prx/libSceUsbStorage/Export.cpp b/core/libs/prx/libSceUsbStorage/Export.cpp index 96c447c943..0a837a06f4 100644 --- a/core/libs/prx/libSceUsbStorage/Export.cpp +++ b/core/libs/prx/libSceUsbStorage/Export.cpp @@ -3,77 +3,150 @@ #include "SceTypes.hpp" #include "prx/libc/include/General.hpp" -struct UsbStorageEvent; -struct UsbStorageDeviceInfo; -using UsbStorageCallback = void (APS5_VABI *)(const UsbStorageEvent* event, void* userData); +#include "prx/libSceUsbStorage/UsbStorage.hpp" extern "C" { -int APS5_VABI sceUsbStorageInit(void* attr) { +std::int32_t APS5_VABI sceUsbStorageInit(void* attr) { + (void)attr; + return 0; +} + +std::int32_t APS5_VABI sceUsbStorageTerm(void) { + return 0; +} + +std::int32_t APS5_VABI sceUsbStorageRegisterCallback(std::int32_t eventType, UsbStorageCallback callback, void* arg) { + (void)arg; + if (callback == nullptr) { + return UsbStorage::InvalidArgument; + } + if (eventType < UsbStorage::CallbackEventTypeFirst || eventType > UsbStorage::CallbackEventTypeLast) { + return UsbStorage::InvalidArgument; + } NotImplemented_nid_no_patch(__func__); return 0; } -int APS5_VABI sceUsbStorageTerm(void) { + +std::int32_t APS5_VABI sceUsbStorageUnregisterCallback(std::int32_t eventType, UsbStorageCallback callback) { + if (callback == nullptr) { + return UsbStorage::InvalidArgument; + } + if (eventType < UsbStorage::CallbackEventTypeFirst || eventType > UsbStorage::CallbackEventTypeLast) { + return UsbStorage::InvalidArgument; + } NotImplemented_nid_no_patch(__func__); return 0; } -int APS5_VABI sceUsbStorageRegisterCallback(int32_t eventType, UsbStorageCallback callback, void* arg) { + +std::int32_t APS5_VABI sceUsbStorageRegisterCallbackForMapAvailable(std::uint32_t device, const char* directory, UsbStorageCallback callback, void* arg) { + (void)device; + (void)arg; + if (directory == nullptr || callback == nullptr) { + return UsbStorage::InvalidArgument; + } NotImplemented_nid_no_patch(__func__); return 0; } -int APS5_VABI sceUsbStorageUnregisterCallback(int32_t eventType, UsbStorageCallback callback) { + +std::int32_t APS5_VABI sceUsbStorageUnregisterCallbackForMapAvailable(std::uint32_t device, const char* directory, UsbStorageCallback callback) { + (void)device; + if (directory == nullptr || callback == nullptr) { + return UsbStorage::InvalidArgument; + } NotImplemented_nid_no_patch(__func__); return 0; } -int APS5_VABI sceUsbStorageRegisterCallbackForMapAvailable(uint32_t device, const char* directory, UsbStorageCallback callback, void* arg) { + +std::int32_t APS5_VABI sceUsbStorageRegisterCallbackForMapUnavailable(std::uint32_t device, const char* directory, UsbStorageCallback callback, void* arg) { + (void)device; + (void)arg; + if (directory == nullptr || callback == nullptr) { + return UsbStorage::InvalidArgument; + } NotImplemented_nid_no_patch(__func__); return 0; } -int APS5_VABI sceUsbStorageUnregisterCallbackForMapAvailable(uint32_t device, const char* directory, UsbStorageCallback callback) { + +std::int32_t APS5_VABI sceUsbStorageUnregisterCallbackForMapUnavailable(std::uint32_t device, const char* directory, UsbStorageCallback callback) { + (void)device; + if (directory == nullptr || callback == nullptr) { + return UsbStorage::InvalidArgument; + } NotImplemented_nid_no_patch(__func__); return 0; } -int APS5_VABI sceUsbStorageRegisterCallbackForMapUnavailable(uint32_t device, const char* directory, UsbStorageCallback callback, void* arg) { - NotImplemented_nid_no_patch(__func__); - return 0; + +std::int32_t APS5_VABI sceUsbStorageGetDeviceInfo(std::uint32_t device, UsbStorageDeviceInfo* info) { + (void)device; + if (info == nullptr) { + return UsbStorage::InvalidArgument; + } + return UsbStorage::DeviceNotFound; } -int APS5_VABI sceUsbStorageUnregisterCallbackForMapUnavailable(uint32_t device, const char* directory, UsbStorageCallback callback) { - NotImplemented_nid_no_patch(__func__); - return 0; -} -int APS5_VABI sceUsbStorageGetDeviceInfo(uint32_t device, UsbStorageDeviceInfo* info) { - NotImplemented_nid_no_patch(__func__); - return 0; -} -int APS5_VABI sceUsbStorageGetDeviceList(uint32_t* devices, int32_t* count) { - NotImplemented_nid_no_patch(__func__); + +std::int32_t APS5_VABI sceUsbStorageGetDeviceList(std::uint32_t* devices, std::int32_t* count) { + if (devices == nullptr || count == nullptr) { + return UsbStorage::InvalidArgument; + } + *count = 0; return 0; } + int APS5_VABI sceUsbStorageGetMountPointOfShellCore(uint32_t device, char* mountPoint) { + (void)device; + (void)mountPoint; NotImplemented_nid_no_patch(__func__); return 0; } -bool APS5_VABI sceUsbStorageIsExist(uint32_t device, const char* directory, std::uint8_t* exists) { + +bool APS5_VABI sceUsbStorageIsExist(std::uint32_t device, const char* directory, std::uint8_t* exists) { + (void)device; + (void)directory; + *exists = 0; + return true; +} + +std::int32_t APS5_VABI sceUsbStorageRequestMap(std::uint32_t device, const char* directory, std::int32_t flags, + std::uint64_t volumeSize, char* mountPoint, std::uint64_t* mountId) { + (void)device; + (void)directory; + (void)flags; + (void)volumeSize; + (void)mountPoint; + (void)mountId; + return UsbStorage::DeviceNotFound; +} + +std::int32_t APS5_VABI sceUsbStorageRequestUnmap(std::uint32_t device, const char* directory) { + (void)device; + if (directory == nullptr) { + return UsbStorage::InvalidArgument; + } NotImplemented_nid_no_patch(__func__); return 0; } -int APS5_VABI sceUsbStorageRequestMap(uint32_t device, const char* directory, int32_t flags, uint64_t volumeSize, char* mountPoint, uint64_t* mountId) { - NotImplemented_nid_no_patch(__func__); - return 0; + +std::int32_t APS5_VABI sceUsbStorageGetdentsOpen(std::uint32_t device, std::int32_t* fd, std::uint64_t* bufferSize) { + (void)device; + if (fd == nullptr || bufferSize == nullptr) { + return UsbStorage::InvalidArgument; + } + return UsbStorage::DeviceNotFound; } -int APS5_VABI sceUsbStorageRequestUnmap(uint32_t device, const char* directory) { - NotImplemented_nid_no_patch(__func__); - return 0; + +std::int32_t APS5_VABI sceUsbStorageGetdentsRead(std::int32_t fd, void* buffer, std::uint64_t bufferSize, std::uint64_t* bytesRead) { + (void)fd; + (void)bufferSize; + if (buffer == nullptr || bytesRead == nullptr) { + return UsbStorage::InvalidArgument; + } + *bytesRead = 0; + return UsbStorage::NotSupported; } -int APS5_VABI sceUsbStorageGetdentsOpen(uint32_t device, int32_t* fd, uint64_t* bufferSize) { - NotImplemented_nid_no_patch(__func__); - return 0; -} -int APS5_VABI sceUsbStorageGetdentsRead(int32_t fd, void* buffer, uint64_t bufferSize, uint64_t* bytesRead) { - NotImplemented_nid_no_patch(__func__); - return 0; -} -int APS5_VABI sceUsbStorageGetdentsClose(int32_t fd) { + +std::int32_t APS5_VABI sceUsbStorageGetdentsClose(std::int32_t fd) { + (void)fd; NotImplemented_nid_no_patch(__func__); return 0; } @@ -83,10 +156,15 @@ int APS5_VABI sceUsbStorageClearFakeWriteProtection() { return 0; } -int APS5_VABI sceUsbStorageRequestMapWSB(uint32_t device_id, const char* subpath, int32_t flags, - int32_t unknown, char* out_mount_path, int32_t* out_status) { - NotImplemented_nid_no_patch(__func__); - return 0; +std::int32_t APS5_VABI sceUsbStorageRequestMapWSB(std::uint32_t device, const char* subpath, std::int32_t flags, + std::int32_t unknown, char* mountPath, std::int32_t* status) { + (void)device; + (void)subpath; + (void)flags; + (void)unknown; + (void)mountPath; + (void)status; + return UsbStorage::DeviceNotFound; } int APS5_VABI sceUsbStorageSetFakeMapLockForDebug() { diff --git a/core/libs/prx/libSceUsbStorage/UsbStorage.hpp b/core/libs/prx/libSceUsbStorage/UsbStorage.hpp new file mode 100644 index 0000000000..b3bc2d24ab --- /dev/null +++ b/core/libs/prx/libSceUsbStorage/UsbStorage.hpp @@ -0,0 +1,50 @@ +#ifndef CORE_LIBS_PRX_LIBSCEUSBSTORAGE_USBSTORAGE_HPP +#define CORE_LIBS_PRX_LIBSCEUSBSTORAGE_USBSTORAGE_HPP + +#include "prx/libc/include/general/VabiMacros.hpp" + +#include +#include + +namespace UsbStorage { + +inline constexpr std::int32_t InvalidArgument = static_cast(0x80f40002); +inline constexpr std::int32_t DeviceNotFound = static_cast(0x80f40004); +inline constexpr std::int32_t NotSupported = static_cast(0x80f40005); + +inline constexpr std::int32_t CallbackEventTypeFirst = 1; +inline constexpr std::int32_t CallbackEventTypeLast = 2; + +} + +struct UsbStorageEvent; +struct UsbStorageDeviceInfo; +using UsbStorageCallback = void (APS5_VABI *)(const UsbStorageEvent* event, void* userData); + +extern "C" { +std::int32_t APS5_VABI sceUsbStorageInit(void* attr); +std::int32_t APS5_VABI sceUsbStorageTerm(void); +std::int32_t APS5_VABI sceUsbStorageRegisterCallback(std::int32_t eventType, UsbStorageCallback callback, void* arg); +std::int32_t APS5_VABI sceUsbStorageUnregisterCallback(std::int32_t eventType, UsbStorageCallback callback); +std::int32_t APS5_VABI sceUsbStorageRegisterCallbackForMapAvailable(std::uint32_t device, const char* directory, UsbStorageCallback callback, void* arg); +std::int32_t APS5_VABI sceUsbStorageUnregisterCallbackForMapAvailable(std::uint32_t device, const char* directory, UsbStorageCallback callback); +std::int32_t APS5_VABI sceUsbStorageRegisterCallbackForMapUnavailable(std::uint32_t device, const char* directory, UsbStorageCallback callback, void* arg); +std::int32_t APS5_VABI sceUsbStorageUnregisterCallbackForMapUnavailable(std::uint32_t device, const char* directory, UsbStorageCallback callback); +std::int32_t APS5_VABI sceUsbStorageGetDeviceInfo(std::uint32_t device, UsbStorageDeviceInfo* info); +std::int32_t APS5_VABI sceUsbStorageGetDeviceList(std::uint32_t* devices, std::int32_t* count); +std::int32_t APS5_VABI sceUsbStorageGetMountPointOfShellCore(std::uint32_t device, char* mountPoint); +bool APS5_VABI sceUsbStorageIsExist(std::uint32_t device, const char* directory, std::uint8_t* exists); +std::int32_t APS5_VABI sceUsbStorageRequestMap(std::uint32_t device, const char* directory, std::int32_t flags, + std::uint64_t volumeSize, char* mountPoint, std::uint64_t* mountId); +std::int32_t APS5_VABI sceUsbStorageRequestUnmap(std::uint32_t device, const char* directory); +std::int32_t APS5_VABI sceUsbStorageGetdentsOpen(std::uint32_t device, std::int32_t* fd, std::uint64_t* bufferSize); +std::int32_t APS5_VABI sceUsbStorageGetdentsRead(std::int32_t fd, void* buffer, std::uint64_t bufferSize, std::uint64_t* bytesRead); +std::int32_t APS5_VABI sceUsbStorageGetdentsClose(std::int32_t fd); +std::int32_t APS5_VABI sceUsbStorageRequestMapWSB(std::uint32_t device, const char* subpath, std::int32_t flags, + std::int32_t unknown, char* mountPath, std::int32_t* status); +std::int32_t APS5_VABI sceUsbStorageClearFakeWriteProtection(); +std::int32_t APS5_VABI sceUsbStorageSetFakeMapLockForDebug(); +std::int32_t APS5_VABI sceUsbStorageSetFakeWriteProtection(); +} + +#endif diff --git a/core/libs/prx/libSceUsbd/Export.cpp b/core/libs/prx/libSceUsbd/Export.cpp index 6d791a3166..2859958e57 100644 --- a/core/libs/prx/libSceUsbd/Export.cpp +++ b/core/libs/prx/libSceUsbd/Export.cpp @@ -28,6 +28,8 @@ constexpr std::uint8_t TRANSFER_TYPE_BULK = 2; constexpr std::uint8_t TRANSFER_TYPE_INTERRUPT = 3; constexpr std::int32_t CONTROL_SETUP_SIZE = 8; constexpr std::uint8_t SUPPORTED_TRANSFER_FLAGS = LIBUSB_TRANSFER_SHORT_NOT_OK | LIBUSB_TRANSFER_ADD_ZERO_PACKET; +constexpr std::uint16_t STRING_DESCRIPTOR_SIZE = 255; +constexpr std::uint32_t STRING_DESCRIPTOR_TIMEOUT = 1000; struct UsbdTimeval { std::int64_t seconds; @@ -141,6 +143,13 @@ void LIBUSB_CALL CompleteTransfer(libusb_transfer* host) { if (guest->callback != nullptr) guest->callback(guest); } +bool RequestStringDescriptor(libusb_device_handle* handle, std::uint8_t descriptorIndex, std::uint16_t languageId, std::uint8_t* descriptor) { + std::memset(descriptor, 0, STRING_DESCRIPTOR_SIZE); + const auto value = static_cast(LIBUSB_DT_STRING << 8 | descriptorIndex); + const int result = libusb_control_transfer(handle, LIBUSB_ENDPOINT_IN, LIBUSB_REQUEST_GET_DESCRIPTOR, value, languageId, descriptor, STRING_DESCRIPTOR_SIZE, STRING_DESCRIPTOR_TIMEOUT); + return result >= 0 && descriptor[1] == LIBUSB_DT_STRING; +} + } extern "C" { @@ -550,18 +559,31 @@ std::int32_t APS5_VABI sceUsbdEventHandlerActive() { } std::int32_t APS5_VABI sceUsbdGetConfigDescriptorByValue(void* device, std::uint8_t configurationValue, void** config) { - NotImplemented_nid_no_patch(__func__); + if (device == nullptr || config == nullptr) return SCE_USBD_ERROR_INVALID_ARG; + libusb_config_descriptor* host = nullptr; + const int result = libusb_get_config_descriptor_by_value(KnownDevice(device, __func__), configurationValue, &host); + *config = nullptr; + if (result != 0) return ToGuestError(result); + auto& state = State(); + std::lock_guard lock(state.mutex); + state.configs.insert(host); + *config = host; return 0; } std::int32_t APS5_VABI sceUsbdGetConfiguration(void* deviceHandle, std::int32_t* configuration) { - NotImplemented_nid_no_patch(__func__); - return 0; + if (deviceHandle == nullptr || configuration == nullptr) return SCE_USBD_ERROR_INVALID_ARG; + int host = 0; + const int result = libusb_get_configuration(KnownHandle(deviceHandle, __func__), &host); + if (result == 0) *configuration = host; + return ToGuestError(result); } std::int32_t APS5_VABI sceUsbdGetDescriptor(void* deviceHandle, std::uint8_t descriptorType, std::uint8_t descriptorIndex, std::uint8_t* data, std::int32_t length) { - NotImplemented_nid_no_patch(__func__); - return 0; + if (deviceHandle == nullptr || data == nullptr || length < 1) return SCE_USBD_ERROR_INVALID_ARG; + auto* handle = KnownHandle(deviceHandle, __func__); + if (length > 65535) length = 65535; + return ToGuestError(libusb_get_descriptor(handle, descriptorType, descriptorIndex, data, length)); } void* APS5_VABI sceUsbdGetDevice(void* deviceHandle) { @@ -585,8 +607,35 @@ std::int32_t APS5_VABI sceUsbdGetMaxPacketSize(void* device, std::uint8_t endpoi } std::int32_t APS5_VABI sceUsbdGetStringDescriptorAscii(void* deviceHandle, std::uint8_t descriptorIndex, std::uint8_t* data, std::int32_t length) { - NotImplemented_nid_no_patch(__func__); - return 0; + if (deviceHandle == nullptr || data == nullptr || length < 1) return SCE_USBD_ERROR_INVALID_ARG; + auto* handle = KnownHandle(deviceHandle, __func__); + if (length > 65535) length = 65535; + data[0] = 0; + std::uint8_t descriptor[STRING_DESCRIPTOR_SIZE]; + if (!RequestStringDescriptor(handle, 0, 0, descriptor)) return SCE_USBD_ERROR_OTHER; + const auto languageId = static_cast(descriptor[2] | descriptor[3] << 8); + if (!RequestStringDescriptor(handle, descriptorIndex, languageId, descriptor) || descriptor[0] < 2) return SCE_USBD_ERROR_OTHER; + std::int32_t units = descriptor[0] / 2 - 1; + if (units > length - 1) units = length - 1; + std::uint8_t byteOrder = 3; + std::int32_t written = 0; + for (std::int32_t unit = 0; unit < units; ++unit) { + const auto code = static_cast(descriptor[2 * unit + 2] | descriptor[2 * unit + 3] << 8); + std::uint8_t character; + if ((code & 0xFF00) == 0 && (byteOrder & 1) != 0) { + character = static_cast(code); + byteOrder = 1; + } else if ((code & 0x00FF) == 0 && (byteOrder & 2) != 0) { + character = static_cast(code >> 8); + byteOrder = 2; + } else { + continue; + } + if (character < 0x20 || character > 0x7E || character == '<' || character == '>') continue; + data[written++] = character; + } + data[written] = 0; + return written; } std::int32_t APS5_VABI sceUsbdHandleEvents() { diff --git a/core/libs/prx/libSceUserService/Export.cpp b/core/libs/prx/libSceUserService/Export.cpp index 513d813d46..e96a561ddf 100644 --- a/core/libs/prx/libSceUserService/Export.cpp +++ b/core/libs/prx/libSceUserService/Export.cpp @@ -3,14 +3,35 @@ #include "SceTypes.hpp" #include "prx/libc/include/General.hpp" #include +#include #include +#include +#include #include "prx/libSceUserService/UserService.hpp" -static std::atomic initialized{false}; -static std::atomic loginReported{false}; +namespace { -static int ReportAccessibilitySetting(int user_id, int32_t* setting, int32_t value) { - if (user_id != USER_SERVICE_INITIAL_USER_ID) { +std::atomic initialized{false}; +std::mutex userMutex; + +struct UserEvent { + std::uint32_t eventType; + int userId; +}; + +std::deque eventQueue; +std::array userConnected{true, false, false, false}; + +int UserIndex(int userId) { + if (userId >= USER_SERVICE_INITIAL_USER_ID && userId < USER_SERVICE_INITIAL_USER_ID + USER_SERVICE_MAX_USERS) { + return userId - USER_SERVICE_INITIAL_USER_ID; + } + return -1; +} + +int ReportAccessibilitySetting(int user_id, int32_t* setting, int32_t value) { + const int index = UserIndex(user_id); + if (index < 0) { return USER_SERVICE_ERROR_NOT_LOGGED_IN; } if (setting == nullptr) { @@ -20,6 +41,17 @@ static int ReportAccessibilitySetting(int user_id, int32_t* setting, int32_t val return USER_SERVICE_OK; } +void PopulateInitialLoginEvents() { + std::lock_guard lock(userMutex); + eventQueue.clear(); + constexpr std::uint32_t EventTypeLogin = 0; + eventQueue.push_back({EventTypeLogin, USER_SERVICE_INITIAL_USER_ID}); + for (int i = 1; i < USER_SERVICE_MAX_USERS; ++i) { + if (userConnected[i]) { + eventQueue.push_back({EventTypeLogin, USER_SERVICE_INITIAL_USER_ID + i}); + } + } +} extern "C" int APS5_VABI sceSystemServiceParamGetInt(int param_id, int* value); @@ -47,15 +79,43 @@ static int ReportKeyboardSetting(int user_id, int32_t* type, int32_t (*value)()) if (type == nullptr) { return USER_SERVICE_ERROR_INVALID_ARGUMENT; } - if (user_id != USER_SERVICE_INITIAL_USER_ID) { + if (UserIndex(user_id) < 0) { return USER_SERVICE_ERROR_UNKNOWN_USER; } *type = value(); return USER_SERVICE_OK; } +} extern "C" { +void UserServiceSetUserConnected_nid_postfix(int userIndex, bool connected) { + if (userIndex <= 0 || userIndex >= USER_SERVICE_MAX_USERS) return; + std::lock_guard lock(userMutex); + if (userConnected[userIndex] != connected) { + userConnected[userIndex] = connected; + if (initialized.load()) { + constexpr std::uint32_t EventTypeLogin = 0; + constexpr std::uint32_t EventTypeLogout = 1; + const std::uint32_t eventType = connected ? EventTypeLogin : EventTypeLogout; + const int userId = USER_SERVICE_INITIAL_USER_ID + userIndex; + bool collapsed = false; + for (auto it = eventQueue.begin(); it != eventQueue.end(); ++it) { + if (it->userId == userId) { + if (it->eventType != eventType) { + eventQueue.erase(it); + } + collapsed = true; + break; + } + } + if (!collapsed) { + eventQueue.push_back({eventType, userId}); + } + } + } +} + int APS5_VABI sceUserServiceGetAccessibilityChatTranscription(int user_id, int32_t* chat_transcription) { return ReportAccessibilitySetting(user_id, chat_transcription, 0); } @@ -91,7 +151,8 @@ int APS5_VABI sceUserServiceGetAccessibilityZoomFollowFocus(int user_id, int32_t } int APS5_VABI sceUserServiceGetAgeLevel(int user_id, uint32_t* age_level) { - if (user_id != USER_SERVICE_INITIAL_USER_ID) { + const int index = UserIndex(user_id); + if (index < 0) { return USER_SERVICE_ERROR_NOT_LOGGED_IN; } if (age_level == nullptr) { @@ -110,12 +171,14 @@ int APS5_VABI sceUserServiceGetEvent(SceUserServiceEvent* event) { if (event == nullptr) { return USER_SERVICE_ERROR_INVALID_ARGUMENT; } - if (loginReported.exchange(true)) { + std::lock_guard lock(userMutex); + if (eventQueue.empty()) { return USER_SERVICE_ERROR_NO_EVENT; } - constexpr std::uint32_t EventTypeLogin = 0; - event->event_type = EventTypeLogin; - event->user_id = USER_SERVICE_INITIAL_USER_ID; + const auto item = eventQueue.front(); + eventQueue.pop_front(); + event->event_type = item.eventType; + event->user_id = item.userId; return USER_SERVICE_OK; } @@ -123,7 +186,8 @@ int APS5_VABI sceUserServiceGetGamePresets(int user_id, UserServiceGamePresets* // System-wide game presets (difficulty, view inversion, subtitles, audio language) of the user's // profile. A fresh console has none set: every field is 0, "not specified", and the title falls // back to its own defaults. this_size is the caller's. - if (presets == nullptr || user_id != USER_SERVICE_INITIAL_USER_ID) { + const int index = UserIndex(user_id); + if (presets == nullptr || index < 0) { return USER_SERVICE_ERROR_INVALID_ARGUMENT; } presets->difficulty = 0; @@ -152,10 +216,14 @@ int APS5_VABI sceUserServiceGetLoginUserIdList(UserServiceLoginUserIdList* user_ if (user_id_list == nullptr) { return USER_SERVICE_ERROR_INVALID_ARGUMENT; } - user_id_list->user_id[0] = USER_SERVICE_INITIAL_USER_ID; - user_id_list->user_id[1] = USER_SERVICE_USER_ID_INVALID; - user_id_list->user_id[2] = USER_SERVICE_USER_ID_INVALID; - user_id_list->user_id[3] = USER_SERVICE_USER_ID_INVALID; + std::lock_guard lock(userMutex); + for (int i = 0; i < 4; ++i) { + if (i < USER_SERVICE_MAX_USERS && userConnected[i]) { + user_id_list->user_id[i] = USER_SERVICE_INITIAL_USER_ID + i; + } else { + user_id_list->user_id[i] = USER_SERVICE_USER_ID_INVALID; + } + } return USER_SERVICE_OK; } @@ -163,22 +231,24 @@ int APS5_VABI sceUserServiceGetUserName(int user_id, char* name, size_t size) { if (name == nullptr) { return USER_SERVICE_ERROR_INVALID_ARGUMENT; } - if (user_id != USER_SERVICE_INITIAL_USER_ID) { + const int index = UserIndex(user_id); + if (index < 0) { return USER_SERVICE_ERROR_UNKNOWN_USER; } if (size < USER_SERVICE_MAX_USER_NAME_LENGTH + 1) { std::memset(name, 0, size); return USER_SERVICE_ERROR_BUFFER_TOO_SHORT; } - std::strncpy(name, USER_SERVICE_INITIAL_USER_NAME, size); + std::strncpy(name, USER_SERVICE_USER_NAMES[index], size); return USER_SERVICE_OK; } int APS5_VABI sceUserServiceGetUserNumber(int user_id, int32_t* number) { - if (number == nullptr || user_id != USER_SERVICE_INITIAL_USER_ID) { + const int index = UserIndex(user_id); + if (number == nullptr || index < 0) { return USER_SERVICE_ERROR_INVALID_ARGUMENT; } - *number = 1; + *number = index + 1; return USER_SERVICE_OK; } @@ -190,7 +260,7 @@ static int MarkInitialized() { if (initialized.exchange(true)) { return USER_SERVICE_ERROR_ALREADY_INITIALIZED; } - loginReported.store(false); + PopulateInitialLoginEvents(); return USER_SERVICE_OK; } @@ -214,6 +284,8 @@ int APS5_VABI sceUserServiceTerminate(void) { if (!initialized.exchange(false)) { return USER_SERVICE_ERROR_NOT_INITIALIZED; } + std::lock_guard lock(userMutex); + eventQueue.clear(); return USER_SERVICE_OK; } @@ -237,18 +309,23 @@ int APS5_VABI sceUserServiceGetRegisteredUserIdList(UserServiceRegisteredUserIdL if (user_id_list == nullptr) { return USER_SERVICE_ERROR_INVALID_ARGUMENT; } - user_id_list->user_id[0] = USER_SERVICE_INITIAL_USER_ID; - for (int i = 1; i < 16; ++i) { - user_id_list->user_id[i] = USER_SERVICE_USER_ID_INVALID; + std::lock_guard lock(userMutex); + for (int i = 0; i < 16; ++i) { + if (i < USER_SERVICE_MAX_USERS && userConnected[i]) { + user_id_list->user_id[i] = USER_SERVICE_INITIAL_USER_ID + i; + } else { + user_id_list->user_id[i] = USER_SERVICE_USER_ID_INVALID; + } } return USER_SERVICE_OK; } int APS5_VABI sceUserServiceGetUserColor(int user_id, int* color) { - if (color == nullptr || user_id != USER_SERVICE_INITIAL_USER_ID) { + const int index = UserIndex(user_id); + if (color == nullptr || index < 0) { return USER_SERVICE_ERROR_INVALID_ARGUMENT; } - *color = 0; + *color = index; return USER_SERVICE_OK; } @@ -265,7 +342,8 @@ int APS5_VABI sceUserServiceGetTraditionalChineseInputType(int user_id, int32_t* } int APS5_VABI sceUserServiceGetNpAccountId(int user_id, uint64_t* account_id) { - if (account_id == nullptr || user_id != USER_SERVICE_INITIAL_USER_ID) { + const int index = UserIndex(user_id); + if (account_id == nullptr || index < 0) { return USER_SERVICE_ERROR_INVALID_ARGUMENT; } *account_id = 0; diff --git a/core/libs/prx/libSceUserService/UserService.hpp b/core/libs/prx/libSceUserService/UserService.hpp index eeb192f8aa..2ef3478df9 100644 --- a/core/libs/prx/libSceUserService/UserService.hpp +++ b/core/libs/prx/libSceUserService/UserService.hpp @@ -24,5 +24,11 @@ constexpr int USER_SERVICE_ERROR_BUFFER_TOO_SHORT = -2137653238; constexpr int USER_SERVICE_ERROR_UNKNOWN_USER = static_cast(0x80960105u); constexpr std::size_t USER_SERVICE_MAX_USER_NAME_LENGTH = 16; constexpr const char* USER_SERVICE_INITIAL_USER_NAME = "Player"; +constexpr int USER_SERVICE_MAX_USERS = 4; +constexpr const char* USER_SERVICE_USER_NAMES[USER_SERVICE_MAX_USERS] = { + "Player", "Player2", "Player3", "Player4" +}; + +extern "C" void UserServiceSetUserConnected_nid_postfix(int userIndex, bool connected); #endif diff --git a/core/libs/prx/libSceVideoOut/CMakeLists.txt b/core/libs/prx/libSceVideoOut/CMakeLists.txt index dee987ce6e..5acccb7a32 100644 --- a/core/libs/prx/libSceVideoOut/CMakeLists.txt +++ b/core/libs/prx/libSceVideoOut/CMakeLists.txt @@ -23,7 +23,7 @@ target_include_directories(libSceVideoOut PRIVATE ${CMAKE_BINARY_DIR}/core/libs/prx/libSceAgcDriver/generated ) add_dependencies(libSceVideoOut agc_trace_revision) -target_link_libraries(libSceVideoOut PRIVATE libSceAgcDriver libScePad libSceMouse libSceKeyboard libSceIme libc libkernel decoder_png SDL2-static) +target_link_libraries(libSceVideoOut PRIVATE libSceAgcDriver libScePad libSceUserService libSceMouse libSceKeyboard libSceIme libc libkernel decoder_png SDL2-static) if(WIN32) target_link_libraries(libSceVideoOut PRIVATE comctl32 dxgi) endif() @@ -102,7 +102,7 @@ if(BUILD_TESTING) endforeach() set_target_properties(video_out_flip_tests PROPERTIES EXCLUDE_FROM_ALL OFF) - foreach(flipMode IN ITEMS lifetime reopen decode controls overlay unopened ps4) + foreach(flipMode IN ITEMS lifetime reopen decode controls overlay unopened ps4 resolution resolution_override) if(flipMode STREQUAL "lifetime") add_test(NAME video_out_flip_${flipMode} COMMAND video_out_flip_tests) else() @@ -145,8 +145,14 @@ endif() if(BUILD_TESTING) add_test_executable(pad_sdl_tests tests/PadSdl.cpp) - target_link_libraries(pad_sdl_tests PRIVATE SDL2-static) + target_include_directories(pad_sdl_tests PRIVATE ${LIBS_INCLUDE_DIR}) + target_link_libraries(pad_sdl_tests PRIVATE libSceVideoOut libScePad libSceUserService libc SDL2-static) + configure_windows_unwind(pad_sdl_tests) add_test(NAME pad_sdl COMMAND pad_sdl_tests) + if(WIN32) + set_tests_properties(pad_sdl PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$;PATH=path_list_prepend:$;PATH=path_list_prepend:$;PATH=path_list_prepend:$") + endif() add_test_executable(pad_input_mapping_tests tests/InputMapping.cpp src/InputMapping.cpp) target_include_directories(pad_input_mapping_tests PRIVATE ${LIBS_INCLUDE_DIR} ${CMAKE_SOURCE_DIR}/3rdparty/SDL2/include) target_link_libraries(pad_input_mapping_tests PRIVATE SDL2-static libc) diff --git a/core/libs/prx/libSceVideoOut/include/PadInput.hpp b/core/libs/prx/libSceVideoOut/include/PadInput.hpp index dfa068d16d..2f5eddb56c 100644 --- a/core/libs/prx/libSceVideoOut/include/PadInput.hpp +++ b/core/libs/prx/libSceVideoOut/include/PadInput.hpp @@ -5,7 +5,9 @@ #include "SDL_gamecontroller.h" #include "prx/libScePad/include/KeyboardMouse.hpp" #include "prx/libScePad/include/PadState.hpp" +#include "prx/libScePad/include/Pad.hpp" #include "prx/libSceVideoOut/include/PadOutputPacing.hpp" +#include #include #include "prx/libSceVideoOut/include/Telemetry.hpp" @@ -18,28 +20,37 @@ public: void HandleEvent(const SDL_Event& event, DisplayWindow& window); void Update(const DisplayWindow& window); bool MouseModeEnabled() const { return keyboardMouse.MouseMode(); } + bool SlotOccupied(int slot) const { + return slot >= 0 && slot < PAD_MAX_CONTROLLERS && slots[slot].controller != nullptr; + } private: + struct ControllerSlot { + SDL_GameController* controller = nullptr; + SDL_JoystickID instanceId = -1; + PadInputState state{}; + std::uint32_t outputSequence = 0; + PadOutputState outputState{}; + bool outputPending = false; + std::chrono::steady_clock::time_point nextRumbleRefresh{}; + LightBarPacer lightBar; + TriggerEffectCache triggerEffect; + }; + explicit PadInput(Pad::InputConfiguration configuration); void publish(std::uint64_t frame); + void publish(std::uint64_t frame, int slot); void setMouseMode(bool enabled); - void openFirstAvailableController(); + void openAvailableControllers(); void openController(int deviceIndex); - void closeController(); - PadInputState sampleController() const; + void closeController(int slot); + PadInputState sampleController(int slot) const; void applyOutput(); - void enableSensors(); + void enableSensors(int slot); Pad::KeyboardMouse keyboardMouse; - SDL_GameController* controller = nullptr; - PadInputState controllerState{}; - std::uint32_t outputSequence = 0; - PadOutputState outputState{}; - bool outputPending = false; + std::array slots{}; VideoOutTelemetry::PadChangeFilter telemetryFilter; - std::chrono::steady_clock::time_point nextRumbleRefresh{}; - LightBarPacer lightBar; - TriggerEffectCache triggerEffect; }; #endif diff --git a/core/libs/prx/libSceVideoOut/include/VideoOutDriver.hpp b/core/libs/prx/libSceVideoOut/include/VideoOutDriver.hpp index 7c868f3ce2..156e03c66d 100644 --- a/core/libs/prx/libSceVideoOut/include/VideoOutDriver.hpp +++ b/core/libs/prx/libSceVideoOut/include/VideoOutDriver.hpp @@ -96,6 +96,9 @@ static constexpr std::uint64_t VIDEO_OUT_OUTPUT_STATUS_FLAG_HDR = 1; static constexpr uint64_t VIDEO_OUT_REFRESH_RATE_59_94HZ = 3; static constexpr uint64_t VIDEO_OUT_REFRESH_RATE_119_88HZ = 13; +static constexpr std::uint32_t VIDEO_OUT_RESOLUTION_1080P = 1; +static constexpr std::uint32_t VIDEO_OUT_RESOLUTION_2160P = 2; + struct VideoOutBuffer { int groupIndex = -1; uint64_t dataAddress = 0; @@ -226,6 +229,7 @@ public: bool HasConfig(int handle); bool SupportsHighFrameRate(); int GetDisplayRefreshRate(); + std::uint32_t GetDisplayResolution(); // 0, or VIDEO_OUT_ERROR_FLIP_QUEUE_FULL when the title has VIDEO_OUT_FLIP_QUEUE_CAPACITY flips pending. int SubmitFlip(int handle, int index, int flipMode, int64_t flipArg); @@ -252,6 +256,7 @@ private: HostHdrProbe hdrProbe; std::uint64_t hdrGeneration = 0; std::atomic displayRefreshRate{0}; + std::atomic displayResolution{VIDEO_OUT_RESOLUTION_1080P}; std::jthread presentThread; std::jthread vblankThread; diff --git a/core/libs/prx/libSceVideoOut/src/Output.cpp b/core/libs/prx/libSceVideoOut/src/Output.cpp index 24f9b6f15c..20fb05c756 100644 --- a/core/libs/prx/libSceVideoOut/src/Output.cpp +++ b/core/libs/prx/libSceVideoOut/src/Output.cpp @@ -19,7 +19,7 @@ static constexpr std::size_t VIDEO_OUT_OUTPUT_OPTIONS_FREE_WORD = 3; static VideoOutOutputStatus outputStatus(const auto& cfg) { VideoOutOutputStatus status{}; - status.resolution = (cfg.width >= 3840 || cfg.height >= 2160) ? 2u : 1u; + status.resolution = VideoOutDriver::Get().GetDisplayResolution(); const bool hdr = AgcDriver::CurrentHostHdr().available; status.dynamicRange = hdr ? VIDEO_OUT_DYNAMIC_RANGE_HDR : VIDEO_OUT_DYNAMIC_RANGE_SDR; status.refreshRate = (cfg.outputMode == VIDEO_OUT_OUTPUT_MODE_119_88HZ) ? VIDEO_OUT_REFRESH_RATE_119_88HZ : VIDEO_OUT_REFRESH_RATE_59_94HZ; diff --git a/core/libs/prx/libSceVideoOut/src/PadInput.cpp b/core/libs/prx/libSceVideoOut/src/PadInput.cpp index d9abeeb048..5e9ac3fcf7 100644 --- a/core/libs/prx/libSceVideoOut/src/PadInput.cpp +++ b/core/libs/prx/libSceVideoOut/src/PadInput.cpp @@ -14,7 +14,9 @@ #include "prx/libSceAgcDriver/Execution/include/Presentation.hpp" #include "prx/libSceAgcDriver/Execution/include/Driver.hpp" #include "prx/libScePad/include/PadState.hpp" +#include "prx/libScePad/include/Pad.hpp" #include "prx/libScePad/include/PadInputTypes.hpp" +#include "prx/libSceUserService/UserService.hpp" #include "prx/libc/include/General.hpp" #include "prx/libSceIme/include/ImeInput.hpp" @@ -22,15 +24,16 @@ PadInput::PadInput() : PadInput(Pad::LoadInputMapping()) { } PadInput::PadInput(Pad::InputConfiguration configuration) : keyboardMouse(std::move(configuration)) { - openFirstAvailableController(); + openAvailableControllers(); } PadInput::~PadInput() { - closeController(); + for (int i = 0; i < PAD_MAX_CONTROLLERS; ++i) { + closeController(i); + } } -void PadInput::openFirstAvailableController() { - if (controller != nullptr) return; +void PadInput::openAvailableControllers() { SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1"); SDL_SetHint(SDL_HINT_JOYSTICK_HIDAPI_PS5_RUMBLE, "1"); if ((SDL_WasInit(SDL_INIT_GAMECONTROLLER) & SDL_INIT_GAMECONTROLLER) == 0) { @@ -42,82 +45,122 @@ void PadInput::openFirstAvailableController() { for (int deviceIndex = 0; deviceIndex < SDL_NumJoysticks(); ++deviceIndex) { if (!SDL_IsGameController(deviceIndex)) continue; openController(deviceIndex); - if (controller != nullptr) return; } } void PadInput::openController(int deviceIndex) { - if (controller != nullptr || !SDL_IsGameController(deviceIndex)) return; - controller = SDL_GameControllerOpen(deviceIndex); - if (controller == nullptr) { + if (!SDL_IsGameController(deviceIndex)) return; + const auto instanceId = SDL_JoystickGetDeviceInstanceID(deviceIndex); + for (int i = 0; i < PAD_MAX_CONTROLLERS; ++i) { + if (slots[i].controller != nullptr && slots[i].instanceId == instanceId) return; + } + int slot = -1; + for (int i = 0; i < PAD_MAX_CONTROLLERS; ++i) { + if (slots[i].controller == nullptr) { + slot = i; + break; + } + } + if (slot == -1) return; + SDL_GameController* gc = SDL_GameControllerOpen(deviceIndex); + if (gc == nullptr) { APS5_LOG_ERR("Pad: could not open game controller %d: %s", deviceIndex, SDL_GetError()); return; } - const char* name = SDL_GameControllerName(controller); - APS5_LOG_OUT("Pad: connected game controller: %s (type %d, sensors accel=%d gyro=%d, touchpads=%d, led=%d, trigger rumble=%d)", - name != nullptr ? name : "unknown", static_cast(SDL_GameControllerGetType(controller)), - SDL_GameControllerHasSensor(controller, SDL_SENSOR_ACCEL) == SDL_TRUE, SDL_GameControllerHasSensor(controller, SDL_SENSOR_GYRO) == SDL_TRUE, - SDL_GameControllerGetNumTouchpads(controller), SDL_GameControllerHasLED(controller) == SDL_TRUE, SDL_GameControllerHasRumbleTriggers(controller) == SDL_TRUE); - enableSensors(); - outputPending = true; - lightBar.Reset(); - triggerEffect.Reset(); + slots[slot].controller = gc; + slots[slot].instanceId = SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(gc)); + slots[slot].state = {}; + slots[slot].outputSequence = 0; + slots[slot].outputState = {}; + slots[slot].outputPending = true; + slots[slot].nextRumbleRefresh = {}; + slots[slot].lightBar.Reset(); + slots[slot].triggerEffect.Reset(); + const char* name = SDL_GameControllerName(gc); + APS5_LOG_OUT("Pad: connected game controller (slot %d): %s (type %d, sensors accel=%d gyro=%d, touchpads=%d, led=%d, trigger rumble=%d)", + slot, name != nullptr ? name : "unknown", static_cast(SDL_GameControllerGetType(gc)), + SDL_GameControllerHasSensor(gc, SDL_SENSOR_ACCEL) == SDL_TRUE, SDL_GameControllerHasSensor(gc, SDL_SENSOR_GYRO) == SDL_TRUE, + SDL_GameControllerGetNumTouchpads(gc), SDL_GameControllerHasLED(gc) == SDL_TRUE, SDL_GameControllerHasRumbleTriggers(gc) == SDL_TRUE); + enableSensors(slot); + PadSetPortConnected_nid_postfix(slot, true); + UserServiceSetUserConnected_nid_postfix(slot, true); } -void PadInput::enableSensors() { +void PadInput::enableSensors(int slot) { + if (slot < 0 || slot >= PAD_MAX_CONTROLLERS) return; + SDL_GameController* controller = slots[slot].controller; if (controller == nullptr) return; - const SDL_bool wanted = outputState.motionEnabled ? SDL_TRUE : SDL_FALSE; + const SDL_bool wanted = slots[slot].outputState.motionEnabled ? SDL_TRUE : SDL_FALSE; if (SDL_GameControllerHasSensor(controller, SDL_SENSOR_ACCEL) == SDL_TRUE) SDL_GameControllerSetSensorEnabled(controller, SDL_SENSOR_ACCEL, wanted); if (SDL_GameControllerHasSensor(controller, SDL_SENSOR_GYRO) == SDL_TRUE) SDL_GameControllerSetSensorEnabled(controller, SDL_SENSOR_GYRO, wanted); } -void PadInput::closeController() { - if (controller == nullptr) return; - SDL_GameControllerClose(controller); - controller = nullptr; - controllerState = {}; +void PadInput::closeController(int slot) { + if (slot < 0 || slot >= PAD_MAX_CONTROLLERS) return; + if (slots[slot].controller == nullptr) return; + SDL_GameControllerClose(slots[slot].controller); + slots[slot].controller = nullptr; + slots[slot].instanceId = -1; + slots[slot].state = {}; + slots[slot].outputState = {}; + slots[slot].outputPending = false; + if (slot > 0) { + PadSetPortConnected_nid_postfix(slot, false); + UserServiceSetUserConnected_nid_postfix(slot, false); + } } void PadInput::applyOutput() { - PadOutputState fetched; - if (PadFetchOutput_nid_postfix(&outputSequence, &fetched)) { - const bool motionChanged = fetched.motionEnabled != outputState.motionEnabled; - outputState = fetched; - outputPending = true; - if (motionChanged) enableSensors(); - } - if (controller == nullptr) return; - const auto now = std::chrono::steady_clock::now(); - const bool isPs5 = SDL_GameControllerGetType(controller) == SDL_CONTROLLER_TYPE_PS5; - const auto large = std::max(outputState.vibrationLarge, outputState.audioVibrationLarge); - const auto small = std::max(outputState.vibrationSmall, outputState.audioVibrationSmall); - const bool rumbling = large != 0 || small != 0; - const bool triggerRumble = outputState.trigger[0].fallback != 0 || outputState.trigger[1].fallback != 0; - if (SDL_GameControllerHasLED(controller) == SDL_TRUE) { - const LightBarPacer::Color color = outputState.lightBarValid - ? LightBarPacer::Color{outputState.lightBar[0], outputState.lightBar[1], outputState.lightBar[2]} - : LightBarPacer::Color{0, 64, 255}; - if (lightBar.Due(color, now)) lightBar.Result(color, now, SDL_GameControllerSetLED(controller, color[0], color[1], color[2]) == 0); - } - if (!outputPending) { - if ((rumbling || (triggerRumble && !isPs5)) && now >= nextRumbleRefresh) outputPending = true; - else return; - } - outputPending = false; - nextRumbleRefresh = now + std::chrono::milliseconds(700); - constexpr Uint32 rumbleMs = 2000; - SDL_GameControllerRumble(controller, static_cast(large * 257), static_cast(small * 257), rumbling ? rumbleMs : 0); - if (outputState.triggerTouched) { - if (isPs5) { - TriggerEffectCache::Effect effect{}; - effect[0] = 0x04 | 0x08; - std::memcpy(effect.data() + 10, outputState.trigger[1].effect, 11); - std::memcpy(effect.data() + 21, outputState.trigger[0].effect, 11); - if (triggerEffect.NeedsSend(effect)) { - triggerEffect.Result(effect, SDL_GameControllerSendEffect(controller, effect.data(), static_cast(effect.size())) == 0); + for (int slot = 0; slot < PAD_MAX_CONTROLLERS; ++slot) { + PadOutputState fetched; + if (PadFetchOutputForPort_nid_postfix(slot, &slots[slot].outputSequence, &fetched)) { + const bool motionChanged = fetched.motionEnabled != slots[slot].outputState.motionEnabled; + slots[slot].outputState = fetched; + slots[slot].outputPending = true; + if (motionChanged) enableSensors(slot); + } + SDL_GameController* controller = slots[slot].controller; + if (controller == nullptr) continue; + const auto now = std::chrono::steady_clock::now(); + const bool isPs5 = SDL_GameControllerGetType(controller) == SDL_CONTROLLER_TYPE_PS5; + const auto large = std::max(slots[slot].outputState.vibrationLarge, slots[slot].outputState.audioVibrationLarge); + const auto small = std::max(slots[slot].outputState.vibrationSmall, slots[slot].outputState.audioVibrationSmall); + const bool rumbling = large != 0 || small != 0; + const bool triggerRumble = slots[slot].outputState.trigger[0].fallback != 0 || slots[slot].outputState.trigger[1].fallback != 0; + if (SDL_GameControllerHasLED(controller) == SDL_TRUE) { + struct PlayerLedColor { Uint8 r, g, b; }; + static constexpr PlayerLedColor defaultColors[4] = { + {0, 64, 255}, + {255, 0, 0}, + {0, 255, 0}, + {255, 0, 128} + }; + const auto& def = defaultColors[slot % 4]; + const LightBarPacer::Color color = slots[slot].outputState.lightBarValid + ? LightBarPacer::Color{slots[slot].outputState.lightBar[0], slots[slot].outputState.lightBar[1], slots[slot].outputState.lightBar[2]} + : LightBarPacer::Color{def.r, def.g, def.b}; + if (slots[slot].lightBar.Due(color, now)) slots[slot].lightBar.Result(color, now, SDL_GameControllerSetLED(controller, color[0], color[1], color[2]) == 0); + } + if (!slots[slot].outputPending) { + if ((rumbling || (triggerRumble && !isPs5)) && now >= slots[slot].nextRumbleRefresh) slots[slot].outputPending = true; + else continue; + } + slots[slot].outputPending = false; + slots[slot].nextRumbleRefresh = now + std::chrono::milliseconds(700); + constexpr Uint32 rumbleMs = 2000; + SDL_GameControllerRumble(controller, static_cast(large * 257), static_cast(small * 257), rumbling ? rumbleMs : 0); + if (slots[slot].outputState.triggerTouched) { + if (isPs5) { + TriggerEffectCache::Effect effect{}; + effect[0] = 0x04 | 0x08; + std::memcpy(effect.data() + 10, slots[slot].outputState.trigger[1].effect, 11); + std::memcpy(effect.data() + 21, slots[slot].outputState.trigger[0].effect, 11); + if (slots[slot].triggerEffect.NeedsSend(effect)) { + slots[slot].triggerEffect.Result(effect, SDL_GameControllerSendEffect(controller, effect.data(), static_cast(effect.size())) == 0); + } + } else if (SDL_GameControllerHasRumbleTriggers(controller) == SDL_TRUE) { + SDL_GameControllerRumbleTriggers(controller, static_cast(slots[slot].outputState.trigger[0].fallback * 257), static_cast(slots[slot].outputState.trigger[1].fallback * 257), triggerRumble ? rumbleMs : 0); } - } else if (SDL_GameControllerHasRumbleTriggers(controller) == SDL_TRUE) { - SDL_GameControllerRumbleTriggers(controller, static_cast(outputState.trigger[0].fallback * 257), static_cast(outputState.trigger[1].fallback * 257), triggerRumble ? rumbleMs : 0); } } } @@ -129,10 +172,12 @@ void PadInput::setMouseMode(bool enabled) { keyboardMouse.SetMouseMode(enabled); } -PadInputState PadInput::sampleController() const { +PadInputState PadInput::sampleController(int slot) const { PadInputState result; + if (slot < 0 || slot >= PAD_MAX_CONTROLLERS) return result; + SDL_GameController* controller = slots[slot].controller; if (controller == nullptr) return result; - const auto readButton = [this](SDL_GameControllerButton button) { + const auto readButton = [controller](SDL_GameControllerButton button) { return SDL_GameControllerGetButton(controller, button) != 0; }; const auto addButton = [&result, &readButton](SDL_GameControllerButton source, Pad::PadButton button) { @@ -156,7 +201,7 @@ PadInputState PadInput::sampleController() const { addButton(SDL_CONTROLLER_BUTTON_DPAD_LEFT, Pad::PadButton::Left); addButton(SDL_CONTROLLER_BUTTON_TOUCHPAD, Pad::PadButton::TouchPad); - const auto triggerValue = [this](SDL_GameControllerAxis axis) { + const auto triggerValue = [controller](SDL_GameControllerAxis axis) { const auto value = std::clamp(SDL_GameControllerGetAxis(controller, axis), 0, 32767); return static_cast((value * 255 + 16383) / 32767); }; @@ -165,7 +210,7 @@ PadInputState PadInput::sampleController() const { if (result.analogButtonsL2 != 0) result.buttons |= static_cast(Pad::PadButton::L2); if (result.analogButtonsR2 != 0) result.buttons |= static_cast(Pad::PadButton::R2); - const auto stick = [this](SDL_GameControllerAxis xAxis, SDL_GameControllerAxis yAxis, int deadzonePercent) { + const auto stick = [this, controller](SDL_GameControllerAxis xAxis, SDL_GameControllerAxis yAxis, int deadzonePercent) { return Pad::StickWithDeadzone(SDL_GameControllerGetAxis(controller, xAxis), SDL_GameControllerGetAxis(controller, yAxis), deadzonePercent); }; const auto left = stick(SDL_CONTROLLER_AXIS_LEFTX, SDL_CONTROLLER_AXIS_LEFTY, keyboardMouse.Configuration().settings.leftStickDeadzonePercent); @@ -207,23 +252,34 @@ void PadInput::HandleEvent(const SDL_Event& event, DisplayWindow& window) { openController(event.cdevice.which); return; } - if (event.type == SDL_CONTROLLERDEVICEREMOVED && controller != nullptr) { - const auto instanceId = SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(controller)); - if (instanceId == event.cdevice.which) { - closeController(); - openFirstAvailableController(); - publish(window.FrameNumber()); + if (event.type == SDL_CONTROLLERDEVICEREMOVED) { + for (int slot = 0; slot < PAD_MAX_CONTROLLERS; ++slot) { + if (slots[slot].controller != nullptr && slots[slot].instanceId == event.cdevice.which) { + closeController(slot); + publish(window.FrameNumber(), slot); + break; + } } return; } if (event.type == SDL_CONTROLLERBUTTONDOWN || event.type == SDL_CONTROLLERBUTTONUP || event.type == SDL_CONTROLLERAXISMOTION || event.type == SDL_CONTROLLERDEVICEREMAPPED) { - publish(window.FrameNumber()); + SDL_JoystickID which = -1; + if (event.type == SDL_CONTROLLERBUTTONDOWN || event.type == SDL_CONTROLLERBUTTONUP) which = event.cbutton.which; + else if (event.type == SDL_CONTROLLERAXISMOTION) which = event.caxis.which; + else if (event.type == SDL_CONTROLLERDEVICEREMAPPED) which = event.cdevice.which; + for (int slot = 0; slot < PAD_MAX_CONTROLLERS; ++slot) { + if (slots[slot].controller != nullptr && slots[slot].instanceId == which) { + slots[slot].state = sampleController(slot); + publish(window.FrameNumber(), slot); + return; + } + } return; } if (event.type == SDL_WINDOWEVENT) { if (keyboardMouse.HandleEvent(event, std::chrono::steady_clock::now()).releaseMouse) setMouseMode(false); - publish(window.FrameNumber()); + publish(window.FrameNumber(), 0); return; } // APS5_NO_PAD_INPUT=1 keeps a measurement run from reacting to keys or mouse buttons that reach @@ -239,25 +295,54 @@ void PadInput::HandleEvent(const SDL_Event& event, DisplayWindow& window) { if (requests.togglePerformanceOverlay && window.Handle() != nullptr && event.key.windowID == SDL_GetWindowID(window.Handle())) AgcDriverTogglePerformanceOverlay_nid_postfix(); if (requests.screenshot && window.Handle() != nullptr && event.key.windowID == SDL_GetWindowID(window.Handle())) AgcDriverRequestScreenshot_nid_postfix(); if (requests.toggleMouse) setMouseMode(!keyboardMouse.MouseMode()); - if (event.type != SDL_MOUSEMOTION) publish(window.FrameNumber()); + if (event.type != SDL_MOUSEMOTION) publish(window.FrameNumber(), 0); } void PadInput::Update(const DisplayWindow& window) { - if (controller != nullptr) SDL_GameControllerUpdate(); + bool anyController = false; + for (int slot = 0; slot < PAD_MAX_CONTROLLERS; ++slot) { + if (slots[slot].controller != nullptr) { + anyController = true; + break; + } + } + if (anyController) SDL_GameControllerUpdate(); applyOutput(); - if (controller != nullptr) controllerState = sampleController(); + for (int slot = 0; slot < PAD_MAX_CONTROLLERS; ++slot) { + if (slots[slot].controller != nullptr) { + slots[slot].state = sampleController(slot); + } + } if (keyboardMouse.MouseMode() && SDL_GetKeyboardFocus() == nullptr) { keyboardMouse.Release(); setMouseMode(false); - publish(window.FrameNumber()); + publish(window.FrameNumber(), 0); return; } const bool changed = keyboardMouse.Advance(std::chrono::steady_clock::now()); - if (controller != nullptr || keyboardMouse.MouseMode() || changed) publish(window.FrameNumber()); + if (slots[0].controller != nullptr || keyboardMouse.MouseMode() || changed) { + publish(window.FrameNumber(), 0); + } + for (int slot = 1; slot < PAD_MAX_CONTROLLERS; ++slot) { + if (slots[slot].controller != nullptr) { + publish(window.FrameNumber(), slot); + } + } } void PadInput::publish(std::uint64_t frame) { - const auto state = keyboardMouse.Compose(controllerState); - PadPublishInput_nid_postfix(state); - if (auto* telemetry = VideoOutTelemetry::Instance(); telemetry != nullptr) VideoOutTelemetry::EmitPadIfChanged(*telemetry, telemetryFilter, frame, state); + for (int slot = 0; slot < PAD_MAX_CONTROLLERS; ++slot) { + publish(frame, slot); + } +} + +void PadInput::publish(std::uint64_t frame, int slot) { + if (slot < 0 || slot >= PAD_MAX_CONTROLLERS) return; + if (slot == 0) { + const auto state = keyboardMouse.Compose(slots[0].state); + PadPublishInput_nid_postfix(state); + if (auto* telemetry = VideoOutTelemetry::Instance(); telemetry != nullptr) VideoOutTelemetry::EmitPadIfChanged(*telemetry, telemetryFilter, frame, state); + } else { + PadPublishInputForPort_nid_postfix(slot, slots[slot].state); + } } diff --git a/core/libs/prx/libSceVideoOut/src/VideoOutDriver.cpp b/core/libs/prx/libSceVideoOut/src/VideoOutDriver.cpp index f308f42739..377fb3548b 100644 --- a/core/libs/prx/libSceVideoOut/src/VideoOutDriver.cpp +++ b/core/libs/prx/libSceVideoOut/src/VideoOutDriver.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -6,6 +7,7 @@ #include #include #include +#include #include #include @@ -41,6 +43,26 @@ void require(bool condition, const char* reason) { if (!condition) throw std::runtime_error(std::string("VideoOut: ") + reason); } +std::uint32_t resolutionIndex(std::uint32_t width, std::uint32_t height) { + return (width >= 3840 || height >= 2160) ? VIDEO_OUT_RESOLUTION_2160P : VIDEO_OUT_RESOLUTION_1080P; +} + +std::optional configuredResolution() { + const char* value = std::getenv("APS5_OUTPUT_RESOLUTION"); + if (value == nullptr || *value == '\0') return std::nullopt; + const std::string text(value); + const char* end = text.data() + text.size(); + std::uint32_t width = 0; + std::uint32_t height = 0; + const auto widthResult = std::from_chars(text.data(), end, width); + const bool separated = widthResult.ec == std::errc{} && widthResult.ptr != end && (*widthResult.ptr == 'x' || *widthResult.ptr == 'X'); + const auto heightResult = separated ? std::from_chars(widthResult.ptr + 1, end, height) : std::from_chars_result{end, std::errc::invalid_argument}; + if (!separated || heightResult.ec != std::errc{} || heightResult.ptr != end || width == 0 || height == 0) { + throw std::runtime_error("VideoOut: APS5_OUTPUT_RESOLUTION must be x with positive integers, got \"" + text + "\""); + } + return resolutionIndex(width, height); +} + std::vector windowExtensions(SDL_Window* window) { unsigned extensionCount = 0; if (!SDL_Vulkan_GetInstanceExtensions(window, &extensionCount, nullptr)) throw std::runtime_error(std::string("SDL_Vulkan_GetInstanceExtensions failed: ") + SDL_GetError()); @@ -489,12 +511,18 @@ int VideoOutDriver::GetDisplayRefreshRate() { return displayRefreshRate.load(); } +std::uint32_t VideoOutDriver::GetDisplayResolution() { + if (const auto configured = configuredResolution()) return *configured; + return displayResolution.load(); +} + void VideoOutDriver::updateDisplayRefreshRate() { const int display = SDL_GetWindowDisplayIndex(window.Handle()); if (display < 0) throw std::runtime_error(std::string("SDL_GetWindowDisplayIndex failed: ") + SDL_GetError()); SDL_DisplayMode mode{}; if (SDL_GetCurrentDisplayMode(display, &mode) != 0) throw std::runtime_error(std::string("SDL_GetCurrentDisplayMode failed: ") + SDL_GetError()); displayRefreshRate.store(mode.refresh_rate); + displayResolution.store(resolutionIndex(mode.w, mode.h)); } void VideoOutDriver::pollHostHdr() { diff --git a/core/libs/prx/libSceVideoOut/tests/Flip.cpp b/core/libs/prx/libSceVideoOut/tests/Flip.cpp index 365eddf412..75870fadad 100644 --- a/core/libs/prx/libSceVideoOut/tests/Flip.cpp +++ b/core/libs/prx/libSceVideoOut/tests/Flip.cpp @@ -1022,6 +1022,81 @@ void testOneDevice() { check(next.Serial() == 2, "the GPU work and the flip used more than one device"); } +void testOutputResolution() { + const int handle = sceVideoOutOpen(255, 0, 0, nullptr); + auto cfg = VideoOutDriver::Get().GetConfig(handle); + SDL_DisplayMode mode{}; + check(SDL_GetCurrentDisplayMode(0, &mode) == 0, "the display mode could not be read"); + const std::uint32_t expected = (mode.w >= 3840 || mode.h >= 2160) ? 2u : 1u; + VideoOutOutputStatus status{}; + check(sceVideoOutGetOutputStatus(handle, &status) == 0 && status.resolution == expected, "a port without buffers did not report the display's resolution"); + const std::uint32_t width = expected == 2u ? 1920u : 3840u; + const std::uint32_t height = expected == 2u ? 1080u : 2160u; + std::vector allocation(static_cast(width) * height * 4 + 65535); + const auto storage = alignedBuffer(allocation); + VideoOutBuffers buffer{storage.data(), nullptr, {nullptr, nullptr}}; + VideoOutBufferAttribute2 attribute{}; + attribute.width = width; + attribute.height = height; + attribute.tiling_mode = 1; + attribute.pitch_in_pixel = width; + attribute.pixel_format = 0x8000000000000000ull; + sceVideoOutRegisterBuffers2(handle, 0, 0, &buffer, 1, &attribute, 0, nullptr); + check(sceVideoOutSubmitFlip(handle, 0, 1, 1) == 0, "the flip was not accepted"); + { + std::unique_lock lock(cfg->mutex); + const bool done = cfg->vblankCond.wait_for(lock, std::chrono::seconds(15), [&] { return cfg->failure || cfg->flipStatus.count == 1; }); + if (cfg->failure) std::rethrow_exception(cfg->failure); + check(done, "the flip did not complete"); + check(cfg->width == width && cfg->height == height, "the port did not record the buffer size"); + } + check(sceVideoOutGetOutputStatus(handle, &status) == 0 && status.resolution == expected, "the flipped buffer's size changed the reported resolution"); + sceVideoOutUnregisterBuffers(handle, 0); + sceVideoOutClose(handle); + LibcRunShutdown_nid_postfix(); +} + +void setOutputResolution(const char* value) { +#ifdef _WIN32 + check(_putenv_s("APS5_OUTPUT_RESOLUTION", value) == 0, "APS5_OUTPUT_RESOLUTION was not set"); +#else + check((*value != '\0' ? setenv("APS5_OUTPUT_RESOLUTION", value, 1) : unsetenv("APS5_OUTPUT_RESOLUTION")) == 0, "APS5_OUTPUT_RESOLUTION was not set"); +#endif +} + +void testOutputResolutionOverride() { + const int handle = sceVideoOutOpen(255, 0, 0, nullptr); + SDL_DisplayMode mode{}; + check(SDL_GetCurrentDisplayMode(0, &mode) == 0, "the display mode could not be read"); + const std::uint32_t following = (mode.w >= 3840 || mode.h >= 2160) ? 2u : 1u; + const auto reported = [&] { + VideoOutOutputStatus status{}; + check(sceVideoOutGetOutputStatus(handle, &status) == 0, "the output status was not returned"); + return status.resolution; + }; + setOutputResolution(""); + check(reported() == following, "an unset override changed the display's resolution"); + const std::pair overrides[] = {{"1920x1080", 1u}, {"1280x720", 1u}, {"3840x2160", 2u}, {"2560x2160", 2u}, {"3840X1", 2u}}; + for (const auto& [value, expected] : overrides) { + setOutputResolution(value); + check(reported() == expected, (std::string("the override ") + value + " did not set the reported resolution").c_str()); + std::array raw{}; + VideoOutOutputStatus withVrr{}; + check(sceVideoOutGetOutputStatusWithVrr(handle, raw.data()) == 0, "the VRR status was not returned"); + std::memcpy(&withVrr, raw.data(), sizeof(withVrr)); + check(withVrr.resolution == expected,(std::string("the override ") + value + " did not reach the VRR status").c_str()); + } + for (const char* value : {"1920", "1920x", "x1080", "0x1080", "1920x0", "1920x1080x2", " 1920x1080", "1920x1080 ", "-1920x1080", "1920*1080", "4294967296x1080", "high"}) { + setOutputResolution(value); + const auto message = expectFailure([&] { reported(); }); + check(message.find("APS5_OUTPUT_RESOLUTION") != std::string::npos && message.find(value) != std::string::npos, (std::string("the malformed value ") + value + " was not rejected by name").c_str()); + } + setOutputResolution(""); + check(reported() == following, "clearing the override did not restore the display's resolution"); + sceVideoOutClose(handle); + LibcRunShutdown_nid_postfix(); +} + } int main(int argc, char** argv) { @@ -1047,6 +1122,8 @@ int main(int argc, char** argv) { else if (argc == 2 && std::string(argv[1]) == "unregistered") testUnregisteredBuffer(); else if (argc == 2 && std::string(argv[1]) == "ps4") testPs4Buffers(); else if (argc == 2 && std::string(argv[1]) == "cpuflip") testCpuFlipOrder(); + else if (argc == 2 && std::string(argv[1]) == "resolution") testOutputResolution(); + else if (argc == 2 && std::string(argv[1]) == "resolution_override") testOutputResolutionOverride(); else testLifetime(argc == 2 && std::string(argv[1]) == "reopen"); std::puts("VideoOut flip tests passed"); return 0; diff --git a/core/libs/prx/libSceVideoOut/tests/PadSdl.cpp b/core/libs/prx/libSceVideoOut/tests/PadSdl.cpp index cec6638fbd..3bdec619d1 100644 --- a/core/libs/prx/libSceVideoOut/tests/PadSdl.cpp +++ b/core/libs/prx/libSceVideoOut/tests/PadSdl.cpp @@ -1,13 +1,99 @@ #include "SDL_config.h" - +#include "SDL.h" +#include "SDL_joystick.h" +#include "SDL_gamecontroller.h" +#include "prx/libSceVideoOut/include/PadInput.hpp" +#include "prx/libSceVideoOut/include/DisplayWindow.hpp" +#include "prx/libScePad/include/PadState.hpp" #include +#include +#undef main + +namespace { + +static void Require(bool condition) { + if (!condition) { + std::abort(); + } +} + +} int main() { #ifdef SDL_JOYSTICK_HIDAPI std::puts("pad_sdl: SDL drives controllers through HIDAPI"); - return 0; #else std::fputs("pad_sdl: SDL is built without HIDAPI, so a DualSense gets no light bar, touchpad or trigger effects\n", stderr); return 1; #endif + + Require(SDL_Init(SDL_INIT_GAMECONTROLLER | SDL_INIT_JOYSTICK) == 0); + + SDL_VirtualJoystickDesc desc{}; + desc.version = SDL_VIRTUAL_JOYSTICK_DESC_VERSION; + desc.type = SDL_JOYSTICK_TYPE_GAMECONTROLLER; + desc.naxes = 6; + desc.nbuttons = 16; + desc.name = "Virtual Controller 0"; + const int dev0 = SDL_JoystickAttachVirtualEx(&desc); + Require(dev0 >= 0); + Require(SDL_IsGameController(dev0)); + + DisplayWindow window; + PadInput padInput; + Require(padInput.SlotOccupied(0)); + Require(!padInput.SlotOccupied(1)); + + SDL_Event add0{}; + add0.type = SDL_CONTROLLERDEVICEADDED; + add0.cdevice.which = dev0; + padInput.HandleEvent(add0, window); + Require(padInput.SlotOccupied(0)); + Require(!padInput.SlotOccupied(1)); + + desc.name = "Virtual Controller 1"; + const int dev1 = SDL_JoystickAttachVirtualEx(&desc); + Require(dev1 >= 0); + Require(SDL_IsGameController(dev1)); + + SDL_Event add1{}; + add1.type = SDL_CONTROLLERDEVICEADDED; + add1.cdevice.which = dev1; + padInput.HandleEvent(add1, window); + Require(padInput.SlotOccupied(0)); + Require(padInput.SlotOccupied(1)); + Require(!padInput.SlotOccupied(2)); + Require(Pad::IsConnected(1)); + + const SDL_JoystickID id0 = SDL_JoystickGetDeviceInstanceID(dev0); + SDL_Event rem0{}; + rem0.type = SDL_CONTROLLERDEVICEREMOVED; + rem0.cdevice.which = id0; + padInput.HandleEvent(rem0, window); + Require(!padInput.SlotOccupied(0)); + Require(padInput.SlotOccupied(1)); + Require(Pad::IsConnected(1)); + + padInput.HandleEvent(add0, window); + Require(padInput.SlotOccupied(0)); + Require(padInput.SlotOccupied(1)); + + const SDL_JoystickID id1 = SDL_JoystickGetDeviceInstanceID(dev1); + SDL_Event rem1{}; + rem1.type = SDL_CONTROLLERDEVICEREMOVED; + rem1.cdevice.which = id1; + padInput.HandleEvent(rem1, window); + Require(padInput.SlotOccupied(0)); + Require(!padInput.SlotOccupied(1)); + Require(!Pad::IsConnected(1)); + + padInput.HandleEvent(rem0, window); + Require(!padInput.SlotOccupied(0)); + Require(!padInput.SlotOccupied(1)); + + Require(SDL_JoystickDetachVirtual(dev1) == 0); + Require(SDL_JoystickDetachVirtual(dev0) == 0); + SDL_QuitSubSystem(SDL_INIT_GAMECONTROLLER | SDL_INIT_JOYSTICK); + + return 0; } diff --git a/core/libs/prx/libc/CMakeLists.txt b/core/libs/prx/libc/CMakeLists.txt index dfbe462ee1..d3e9bed678 100644 --- a/core/libs/prx/libc/CMakeLists.txt +++ b/core/libs/prx/libc/CMakeLists.txt @@ -115,19 +115,56 @@ target_include_directories(libc PRIVATE target_link_libraries(libc PRIVATE zarchive) +if(BUILD_TESTING) + add_test_executable(guest_update_env_tests ${LIBS_INCLUDE_DIR}/tests/GuestUpdateEnv.cpp) + target_include_directories(guest_update_env_tests PRIVATE ${LIBS_INCLUDE_DIR}) + target_link_libraries(guest_update_env_tests PRIVATE libc ${CMAKE_DL_LIBS}) + configure_windows_unwind(guest_update_env_tests) + add_test(NAME guest_update_env COMMAND guest_update_env_tests $) + foreach(bit RANGE 0 5) + foreach(source RANGE 0 2) + add_test(NAME guest_update_env_pending_${bit}_${source} COMMAND guest_update_env_tests $ ${bit} ${source}) + set_tests_properties(guest_update_env_pending_${bit}_${source} PROPERTIES TIMEOUT 10) + if(WIN32) + set_tests_properties(guest_update_env_pending_${bit}_${source} PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + endif() + endforeach() + endforeach() + if(WIN32) + set_tests_properties(guest_update_env PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + endif() +endif() + target_sources(libc PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src/FloatingPoint.cpp) if(BUILD_TESTING) add_test_executable(guest_floating_point_tests ${LIBS_INCLUDE_DIR}/tests/GuestFloatingPoint.cpp) target_include_directories(guest_floating_point_tests PRIVATE ${LIBS_INCLUDE_DIR}) - target_link_libraries(guest_floating_point_tests PRIVATE libc) + target_link_libraries(guest_floating_point_tests PRIVATE libc ${CMAKE_DL_LIBS}) configure_windows_unwind(guest_floating_point_tests) - add_test(NAME guest_floating_point COMMAND guest_floating_point_tests) + add_test(NAME guest_floating_point COMMAND guest_floating_point_tests $) set_tests_properties(guest_floating_point PROPERTIES TIMEOUT 10) if(WIN32) set_tests_properties(guest_floating_point PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$") endif() + if(MINGW OR CMAKE_SYSTEM_NAME STREQUAL "Linux") + enable_language(ASM) + add_test_executable(guest_sce_v2_exceptions_tests + ${LIBS_INCLUDE_DIR}/tests/GuestSceV2Exceptions.cpp + ${LIBS_INCLUDE_DIR}/tests/GuestSceV2Exceptions.S) + target_include_directories(guest_sce_v2_exceptions_tests PRIVATE ${LIBS_INCLUDE_DIR}) + target_link_libraries(guest_sce_v2_exceptions_tests PRIVATE libc) + configure_windows_unwind(guest_sce_v2_exceptions_tests) + add_test(NAME guest_sce_v2_exceptions COMMAND guest_sce_v2_exceptions_tests) + set_tests_properties(guest_sce_v2_exceptions PROPERTIES TIMEOUT 10) + if(WIN32) + set_tests_properties(guest_sce_v2_exceptions PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + endif() + endif() endif() if(WIN32) @@ -364,6 +401,24 @@ if(BUILD_TESTING) set_tests_properties(guest_random_state PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$") endif() + add_test_executable(guest_libc_random_seeding_tests ${LIBS_INCLUDE_DIR}/tests/GuestLibcRandomSeeding.cpp) + target_include_directories(guest_libc_random_seeding_tests PRIVATE ${LIBS_INCLUDE_DIR}) + target_link_libraries(guest_libc_random_seeding_tests PRIVATE libc) + configure_windows_unwind(guest_libc_random_seeding_tests) + add_test(NAME guest_libc-random-seeding COMMAND guest_libc_random_seeding_tests) + if(WIN32) + set_tests_properties(guest_libc-random-seeding PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + endif() + add_test_executable(guest_random_device_seed_tests ${LIBS_INCLUDE_DIR}/tests/GuestRandomDeviceSeed.cpp src/Random.cpp) + target_include_directories(guest_random_device_seed_tests PRIVATE ${LIBS_INCLUDE_DIR}) + target_link_libraries(guest_random_device_seed_tests PRIVATE libc) + configure_windows_unwind(guest_random_device_seed_tests) + add_test(NAME guest_random_device_seed COMMAND guest_random_device_seed_tests) + if(WIN32) + set_tests_properties(guest_random_device_seed PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + endif() add_test_executable(guest_integer_builtins_tests ${LIBS_INCLUDE_DIR}/tests/GuestIntegerBuiltins.cpp) target_include_directories(guest_integer_builtins_tests PRIVATE ${LIBS_INCLUDE_DIR}) target_link_libraries(guest_integer_builtins_tests PRIVATE libc) @@ -451,6 +506,16 @@ if(BUILD_TESTING) set_tests_properties(guest_libc_threads PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$") endif() + add_test_executable(guest_std_thread_hardware_concurrency_tests ${LIBS_INCLUDE_DIR}/tests/GuestStdThreadHardwareConcurrency.cpp) + target_include_directories(guest_std_thread_hardware_concurrency_tests PRIVATE ${LIBS_INCLUDE_DIR}) + target_link_libraries(guest_std_thread_hardware_concurrency_tests PRIVATE libc) + configure_windows_unwind(guest_std_thread_hardware_concurrency_tests) + add_test(NAME guest_std_thread_hardware_concurrency COMMAND guest_std_thread_hardware_concurrency_tests) + set_tests_properties(guest_std_thread_hardware_concurrency PROPERTIES TIMEOUT 20) + if(WIN32) + set_tests_properties(guest_std_thread_hardware_concurrency PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + endif() add_test_executable(guest_libc_static_imports_tests ${LIBS_INCLUDE_DIR}/tests/GuestLibcStaticImports.cpp) target_include_directories(guest_libc_static_imports_tests PRIVATE ${LIBS_INCLUDE_DIR}) target_link_libraries(guest_libc_static_imports_tests PRIVATE libc) @@ -661,6 +726,16 @@ if(BUILD_TESTING) set_tests_properties(guest_standard_streams guest_standard_streams_closed_stdout guest_standard_streams_exclusive_stdout PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$") endif() + add_test_executable(guest_gets_tests ${LIBS_INCLUDE_DIR}/tests/GuestGets.cpp) + target_include_directories(guest_gets_tests PRIVATE ${LIBS_INCLUDE_DIR}) + target_link_libraries(guest_gets_tests PRIVATE libc ${CMAKE_DL_LIBS}) + configure_windows_unwind(guest_gets_tests) + add_test(NAME guest_gets COMMAND guest_gets_tests $ guest_gets_error.tmp) + set_tests_properties(guest_gets PROPERTIES TIMEOUT 10) + if(WIN32) + set_tests_properties(guest_gets PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + endif() add_test_executable(file_streams_tests ${LIBS_INCLUDE_DIR}/tests/FileStreams.cpp) target_include_directories(file_streams_tests PRIVATE ${LIBS_INCLUDE_DIR}) target_link_libraries(file_streams_tests PRIVATE libc) diff --git a/core/libs/prx/libc/include/FileStream.hpp b/core/libs/prx/libc/include/FileStream.hpp index 8ff7bf482d..73140da0c7 100644 --- a/core/libs/prx/libc/include/FileStream.hpp +++ b/core/libs/prx/libc/include/FileStream.hpp @@ -17,6 +17,7 @@ #include "prx/libc/include/FileLock.hpp" extern "C" void* HostStandardHandle_nid_no_patch(int descriptor); extern "C" void KeepStandardHandle_nid_no_patch(int descriptor, void* handle); +extern "C" void PrepareStandardStreams_nid_no_patch(); struct UcrtStreamState { char* pointer; char* base; @@ -73,6 +74,9 @@ class FileStream { public: explicit FileStream(std::FILE* handle, bool dynamic = false) : _handle(handle), _dynamic(dynamic) { if (!_handle) throw std::runtime_error("FileStream: null handle"); +#ifdef _WIN32 + PrepareStandardStreams_nid_no_patch(); +#endif _guest.flags = handle == stdin ? 4 : handle == stdout || handle == stderr ? 8 : 0x10; #ifdef _WIN32 const int descriptor = _fileno(handle); diff --git a/core/libs/prx/libc/include/General.hpp b/core/libs/prx/libc/include/General.hpp index f22c2e8556..b45386ab48 100644 --- a/core/libs/prx/libc/include/General.hpp +++ b/core/libs/prx/libc/include/General.hpp @@ -16,10 +16,10 @@ extern "C" [[noreturn]] void NotImplemented_nid_no_patch(const char* funcName); extern "C" void CxaFinalize_nid_no_patch(void* dsoHandle); extern "C" bool StopAtPrefixLetter_nid_no_patch(const char* str, char** endptr, int base); extern "C" bool RejectIntegerBase_nid_no_patch(const char* str, char** endptr, int base, bool setError); -#ifndef _WIN32 extern "C" int KeepHostStandardError_nid_no_patch(); -extern "C" int HostStandardError_nid_no_patch(); extern "C" int KeepHostStandardOutput_nid_no_patch(); +#ifndef _WIN32 +extern "C" int HostStandardError_nid_no_patch(); #else extern "C" bool AdoptHostStandardStream_nid_no_patch(int standard, int saved); extern "C" std::FILE* HostStandardOutputFile_nid_no_patch(); @@ -70,6 +70,28 @@ struct ThreadForwards_nid_no_patch { void (APS5_VABI* exit)(void*); }; extern "C" void RegisterThreadForwards_nid_no_patch(const ThreadForwards_nid_no_patch* forwards); +struct PthreadSyncForwards_nid_no_patch { + int (APS5_VABI* mutexattrInit)(void**); + int (APS5_VABI* mutexattrSettype)(void**, int); + int (APS5_VABI* mutexattrDestroy)(void**); + int (APS5_VABI* mutexInit)(void**, void* const*, const char*); + int (APS5_VABI* mutexDestroy)(void**); + int (APS5_VABI* mutexLock)(void**); + int (APS5_VABI* mutexTrylock)(void**); + int (APS5_VABI* mutexUnlock)(void**); + int (APS5_VABI* condInit)(void**, void* const*, const char*); + int (APS5_VABI* condDestroy)(void**); + int (APS5_VABI* condWait)(void**, void**); + int (APS5_VABI* condSignal)(void**); + int (APS5_VABI* condBroadcast)(void**); + int (APS5_VABI* detach)(void*); + int (APS5_VABI* join)(void*, void**); + void (APS5_VABI* yield)(); +}; +extern "C" void RegisterPthreadSyncForwards_nid_no_patch(const PthreadSyncForwards_nid_no_patch* forwards); + +using SigactionFn_nid_no_patch = int (APS5_VABI*)(int, const void*, void*); +extern "C" void RegisterSigaction_nid_no_patch(SigactionFn_nid_no_patch fn, std::uint32_t* interruptSet); #define APS5_INVALID_ARG_EX throw std::invalid_argument(std::string(__func__) + ": invalid argument") diff --git a/core/libs/prx/libc/include/GuestArena.hpp b/core/libs/prx/libc/include/GuestArena.hpp index bf0d756d40..49719e365b 100644 --- a/core/libs/prx/libc/include/GuestArena.hpp +++ b/core/libs/prx/libc/include/GuestArena.hpp @@ -55,6 +55,10 @@ void GuestArenaMapFile_nid_postfix(void* pointer, std::size_t bytes, void* secti void* GuestArenaMapAlias_nid_postfix(std::uintptr_t address, std::size_t bytes); void GuestArenaUnmapAlias_nid_postfix(void* alias); bool GuestArenaResidentSpan_nid_postfix(std::uintptr_t address, std::size_t bytes, std::uintptr_t* start, std::uintptr_t* end); +void GuestArenaCompareWhenShared_nid_no_patch(std::uintptr_t address, std::size_t bytes); +void GuestArenaCompareResidentWhenShared_nid_no_patch(std::uintptr_t address, std::size_t bytes); +void GuestArenaComparedPages_nid_no_patch(std::uintptr_t address, std::size_t bytes, void** pages, std::size_t* count); +void GuestArenaTakeUnreportedPages_nid_no_patch(std::uintptr_t address, std::size_t bytes, void** pages, std::size_t* count); void GuestArenaForgetSection_nid_postfix(void* section); bool GuestArenaBeginHostImport_nid_no_patch(void* pointer, std::size_t bytes); void GuestArenaEndHostImport_nid_no_patch(void* pointer, std::size_t bytes); diff --git a/core/libs/prx/libc/include/GuestLocale.hpp b/core/libs/prx/libc/include/GuestLocale.hpp index 45e29df3f1..92ca9f13b8 100644 --- a/core/libs/prx/libc/include/GuestLocale.hpp +++ b/core/libs/prx/libc/include/GuestLocale.hpp @@ -58,6 +58,40 @@ struct CollateVtable { std::int64_t (APS5_VABI *hash)(const CollateFacet* self, const char* first, const char* last); }; +struct CodecvtVtable { + FacetVtable facet; + bool (APS5_VABI *alwaysNoconv)(const Facet* self); + int (APS5_VABI *maxLength)(const Facet* self); + int (APS5_VABI *encoding)(const Facet* self); + int (APS5_VABI *in)(Facet* self, std::uint64_t* state, const char* first1, const char* last1, const char** next1, char* first3, char* last3, char** next3); + int (APS5_VABI *out)(Facet* self, std::uint64_t* state, const char* first1, const char* last1, const char** next1, char* first3, char* last3, char** next3); + int (APS5_VABI *unshift)(Facet* self, std::uint64_t* state, char* first3, char* last3, char** next3); + std::size_t (APS5_VABI *length)(Facet* self, std::uint64_t* state, const char* first1, const char* last1, std::size_t count); +}; + +struct NumpunctFacet { + Facet base; + const char* grouping; + char decimalPoint; + char thousandsSep; + const char* falsename; + const char* truename; +}; + +struct NumpunctVtable { + FacetVtable facet; + char (APS5_VABI *decimalPoint)(const NumpunctFacet* self); + char (APS5_VABI *thousandsSep)(const NumpunctFacet* self); + String* (APS5_VABI *grouping)(String* result, const NumpunctFacet* self); + String* (APS5_VABI *falsename)(String* result, const NumpunctFacet* self); + String* (APS5_VABI *truename)(String* result, const NumpunctFacet* self); +}; + +struct UnsupportedMethodsVtable { + FacetVtable facet; + void (APS5_VABI *methods[8])(); +}; + struct IosBase { const void* vtable; std::int64_t standardStream; @@ -89,6 +123,16 @@ static_assert(offsetof(CollateFacet, collation) == 0x10); static_assert(sizeof(CollateFacet) == 0x20); static_assert(offsetof(CollateVtable, compare) == 0x20); static_assert(offsetof(CollateVtable, hash) == 0x30); +static_assert(offsetof(CodecvtVtable, alwaysNoconv) == 0x20); +static_assert(offsetof(CodecvtVtable, in) == 0x38); +static_assert(offsetof(CodecvtVtable, length) == 0x50); +static_assert(sizeof(NumpunctFacet) == 0x30); +static_assert(offsetof(NumpunctFacet, grouping) == 0x10); +static_assert(offsetof(NumpunctFacet, decimalPoint) == 0x18); +static_assert(offsetof(NumpunctFacet, falsename) == 0x20); +static_assert(offsetof(NumpunctFacet, truename) == 0x28); +static_assert(offsetof(NumpunctVtable, decimalPoint) == 0x20); +static_assert(offsetof(NumpunctVtable, truename) == 0x40); static_assert(alignof(LocinfoStorage) == 8); } diff --git a/core/libs/prx/libc/include/WindowsFormatting.hpp b/core/libs/prx/libc/include/WindowsFormatting.hpp index 0c8dd2861f..f0ee4f771d 100644 --- a/core/libs/prx/libc/include/WindowsFormatting.hpp +++ b/core/libs/prx/libc/include/WindowsFormatting.hpp @@ -15,7 +15,6 @@ #include #include -extern "C" int APS5_VABI strerror_r_nid_postfix(int error, char* buffer, std::size_t length); extern "C" std::FILE* GuestStandardOutputFile_nid_no_patch(); namespace LibcDetail { @@ -161,7 +160,7 @@ inline void AppendUtf8(std::string& utf8, char32_t code) { } } -inline std::string NanSpec(const std::string& spec) { +inline std::string PaddedStringSpec(const std::string& spec) { std::string result = "%"; std::size_t index = 1; for (; index < spec.size() && std::strchr("-+ #0", spec[index]); ++index) @@ -170,6 +169,14 @@ inline std::string NanSpec(const std::string& spec) { return result + 's'; } +inline std::string NanText(const std::string& spec, bool negative, bool upper) { + std::string sign; + if (negative) sign = "-"; + else if (spec.find('+') != std::string::npos) sign = "+"; + else if (spec.find(' ') != std::string::npos) sign = " "; + return sign + (upper ? "NAN" : "nan"); +} + inline std::string FormatPointer(const std::string& spec, const void* pointer) { std::size_t index = 1; bool left = false; @@ -205,7 +212,8 @@ inline std::string FormatPointer(const std::string& spec, const void* pointer) { return std::string(padding, ' ') + digits; } -constexpr size_t maximumArgumentPosition = 99; +inline constexpr char KnownConversions[] = "diouxXeEfFgGaAcCsSpn"; +constexpr size_t maximumArgumentPosition = 65535; inline size_t ArgumentPosition(const char*& format) { const char* end = format; @@ -214,7 +222,8 @@ inline size_t ArgumentPosition(const char*& format) { size_t position = 0; for (const char* digit = format; digit != end && position <= maximumArgumentPosition; ++digit) position = position * 10 + static_cast(*digit - '0'); - if (position == 0 || position > maximumArgumentPosition) throw std::invalid_argument("Format argument position out of range"); + if (position == 0) return 0; + if (position > maximumArgumentPosition) throw std::invalid_argument("Format argument position out of range"); format = end + 1; return position; } @@ -258,7 +267,6 @@ inline std::vector NumberedArgumentClasses(const char* format) { inline int FormatWindows(char* buffer, size_t size, const char* format, const void* source, std::string* complete = nullptr, bool secure = false) { if (!format || !source) throw std::invalid_argument("Null formatting argument"); - const int savedError = errno; const char* const formatStart = format; FormatArguments args(source); args.Number(NumberedArgumentClasses(format)); @@ -279,16 +287,24 @@ inline int FormatWindows(char* buffer, size_t size, const char* format, const vo } if (const size_t position = ArgumentPosition(format)) args.Seek(position); std::string spec = "%"; - for (; *format && std::strchr("-+ #0'", *format); ++format) + bool unmeasuredUnknownFlag = false; + bool zeroFlag = false; + for (; *format && std::strchr("-+ #0'", *format); ++format) { if (*format != '\'') spec += *format; + if (std::strchr("+ '", *format)) unmeasuredUnknownFlag = true; + if (*format == '0') zeroFlag = true; + } + bool widthGiven = false; if (*format == '*') { ++format; const int width = star(); if (width < 0) spec += '-'; spec += std::to_string(width < 0 ? -static_cast(width) : width); + widthGiven = true; } else { - while (*format >= '0' && *format <= '9') spec += *format++; + for (; *format >= '0' && *format <= '9'; widthGiven = true) spec += *format++; } + if (zeroFlag && widthGiven) unmeasuredUnknownFlag = true; size_t precisionLimit = std::numeric_limits::max(); if (*format == '.') { ++format; @@ -319,6 +335,13 @@ inline int FormatWindows(char* buffer, size_t size, const char* format, const vo const char conversion = *format; if (!conversion) throw std::invalid_argument("Incomplete format conversion"); ++format; + if (!std::strchr(KnownConversions, conversion)) { + if (secure || unmeasuredUnknownFlag) + throw std::invalid_argument("Unsupported format conversion '" + spec + length + conversion + "' in \"" + std::string(formatStart).substr(0, 160) + "\""); + const char text[] = {'%', conversion, '\0'}; + output.Value(PaddedStringSpec(spec), conversion == '%' ? "%" : text); + continue; + } const bool integerLength = length.empty() || length == "h" || length == "hh" || length == "l" || length == "ll" || length == "j" || length == "z" || length == "t"; if (conversion == 'd' || conversion == 'i') { @@ -346,17 +369,16 @@ inline int FormatWindows(char* buffer, size_t size, const char* format, const vo spec = spec.substr(0, spec.find('.')) + ".1"; output.Value(spec + "ll" + conversion, value); } else if (std::strchr("aAeEfFgG", conversion)) { - const char* nan = conversion >= 'a' ? "nan" : "NAN"; if (length == "L") { static_assert(sizeof(long double) == 16); static_assert(std::numeric_limits::digits == 64); const long double value = args.Next(); - if (std::isnan(value)) output.Value(NanSpec(spec), nan); + if (std::isnan(value)) output.Value(PaddedStringSpec(spec), NanText(spec, std::signbit(value), conversion < 'a').c_str()); else output.Value(spec + "L" + conversion, value); } else { if (!length.empty() && length != "l") throw std::invalid_argument("Invalid floating length"); const double value = args.Next(); - if (std::isnan(value)) output.Value(NanSpec(spec), nan); + if (std::isnan(value)) output.Value(PaddedStringSpec(spec), NanText(spec, std::signbit(value), conversion < 'a').c_str()); else output.Value(spec + conversion, value); } } else if (conversion == 'c' && (length.empty() || length == "h")) { @@ -377,8 +399,7 @@ inline int FormatWindows(char* buffer, size_t size, const char* format, const vo if (!value) value = u"(null)"; std::string utf8; while (utf8.size() < precisionLimit && *value) { - char32_t code = *value++; - if (code >= 0xd800 && code < 0xdc00 && *value >= 0xdc00 && *value < 0xe000) code = 0x10000 + ((code - 0xd800) << 10) + (*value++ - 0xdc00); + const char32_t code = *value++; const auto previousSize = utf8.size(); AppendUtf8(utf8, code); if (utf8.size() > precisionLimit) { @@ -390,10 +411,6 @@ inline int FormatWindows(char* buffer, size_t size, const char* format, const vo } else if (conversion == 'p' && (length.empty() || length == "l")) { const std::string text = FormatPointer(spec, args.Next()); output.Append(text.data(), text.size()); - } else if (conversion == 'm') { - char message[128]; - strerror_r_nid_postfix(savedError, message, sizeof(message)); - output.Value(spec + 's', static_cast(message)); } else if (conversion == 'n' && secure) { throw FormatConstraintViolation("Format count conversion"); } else if (conversion == 'n' && integerLength && spec == "%") { diff --git a/core/libs/prx/libc/include/WindowsMappings.hpp b/core/libs/prx/libc/include/WindowsMappings.hpp index 624685e8a9..9e348301a8 100644 --- a/core/libs/prx/libc/include/WindowsMappings.hpp +++ b/core/libs/prx/libc/include/WindowsMappings.hpp @@ -5,6 +5,9 @@ #include #include #include +#ifdef APS5_COUNT_MAPPING_QUERIES +#include +#endif #include #include #include @@ -28,18 +31,48 @@ public: return *mappings; } +#ifdef APS5_COUNT_MAPPING_QUERIES + static std::uint64_t MemoryQueries() { + return queries.load(std::memory_order_relaxed); + } +#endif + void* Reserve(void* address, std::size_t bytes) { return allocate(GetCurrentProcess(), address, bytes, MEM_RESERVE | MEM_RESERVE_PLACEHOLDER, PAGE_NOACCESS, nullptr, 0); } std::vector> Commit(void* address, std::size_t bytes, DWORD protection, std::size_t granule, bool watched) { + if (granule == 0) throw std::invalid_argument("guest memory commit granule is zero"); std::vector> created; std::lock_guard lock(mutex); collectableRanges.clear(); + const DWORD flags = MEM_RESERVE | MEM_COMMIT | MEM_REPLACE_PLACEHOLDER | (watched ? MEM_WRITE_WATCH : 0); + const auto cut = [](std::uintptr_t at, std::size_t size, const char* operation) { + if (!VirtualFree(reinterpret_cast(at), size, MEM_RELEASE | MEM_PRESERVE_PLACEHOLDER)) fail(operation); + }; + const auto replace = [&](std::uintptr_t at, std::size_t size) { + if (!allocate(GetCurrentProcess(), reinterpret_cast(at), size, flags, protection, nullptr, 0)) fail("replace guest placeholder with private memory"); + if (!created.empty() && created.back().first + created.back().second == at) created.back().second += size; + else created.emplace_back(at, size); + }; const auto end = reinterpret_cast(address) + bytes; for (auto cursor = reinterpret_cast(address); cursor < end;) { const auto memory = query(cursor); - const auto stop = std::min(end, reinterpret_cast(memory.BaseAddress) + memory.RegionSize); + const auto regionEnd = reinterpret_cast(memory.BaseAddress) + memory.RegionSize; + const auto stop = std::min(end, regionEnd); + if (memory.State == MEM_RESERVE && std::min(end, cursor + granule) <= regionEnd) { + forgetClean(cursor, stop); + const auto holder = reinterpret_cast(memory.AllocationBase); + if (holder != cursor) cut(holder, cursor - holder, "split guest placeholder prefix"); + if (stop != regionEnd) cut(cursor, stop - cursor, "split guest placeholder suffix"); + while (cursor < stop && std::min(end, cursor + granule) <= stop) { + const auto size = std::min(end, cursor + granule) - cursor; + if (cursor + size != stop) cut(cursor, size, "split guest placeholder suffix"); + replace(cursor, size); + cursor += size; + } + continue; + } if (memory.State == MEM_RESERVE) { const auto limit = std::min(end, cursor + granule); auto placeholderEnd = stop; @@ -50,10 +83,7 @@ public: } const auto size = std::min(limit, placeholderEnd) - cursor; reset(cursor, size); - const DWORD flags = MEM_RESERVE | MEM_COMMIT | MEM_REPLACE_PLACEHOLDER | (watched ? MEM_WRITE_WATCH : 0); - if (!allocate(GetCurrentProcess(), reinterpret_cast(cursor), size, flags, protection, nullptr, 0)) fail("replace guest placeholder with private memory"); - if (!created.empty() && created.back().first + created.back().second == cursor) created.back().second += size; - else created.emplace_back(cursor, size); + replace(cursor, size); cursor += size; } else { if (memory.State != MEM_COMMIT) throw std::runtime_error("guest memory is not committed"); @@ -96,6 +126,7 @@ public: auto shared = slot.page.lock(); if (!shared) { shared = std::make_shared(); + shared->comparedWhenShared = slot.comparedWhenShared; slot.page = shared; } return shared; @@ -139,13 +170,15 @@ public: const auto aliases = shared->aliases; for (const auto alias : aliases) { if (views.at(alias).resident) demote(alias); + views.at(alias).aliasDemoted = false; } const auto viewBytes = std::min(pageBytes, sectionBytes - done); split(base, pageBytes); mapShared(base, section, offset + done, protection, viewProtection, viewBytes); slot->stored = true; shared->aliases.push_back(base); - views.emplace(base, View{shared, protection, {}, 0, owned, offset + done, 0, 0, false, slot, hostPages(viewBytes)}); + const auto created = views.emplace(base, View{shared, protection, {}, 0, owned, offset + done, 0, 0, false, slot, hostPages(viewBytes)}).first; + if (shared->comparedWhenShared) created->second.unreported = allSubpages; remember(freshRanges, base, base + pageBytes); invalidate(*shared); done += pageBytes; @@ -186,6 +219,45 @@ public: } } + void CompareWhenShared(std::uintptr_t address, std::size_t bytes, bool residentOnly = false) { + std::lock_guard lock(mutex); + collectableRanges.clear(); + for (auto it = views.lower_bound(address & ~(pageBytes - 1)); it != views.end() && it->first < address + bytes; ++it) { + if (residentOnly && !it->second.resident && !it->second.aliasDemoted) continue; + auto& page = *it->second.page; + const bool marked = page.comparedWhenShared; + page.comparedWhenShared = true; + it->second.slot->comparedWhenShared = true; + for (const auto alias : page.aliases) { + auto& view = views.at(alias); + if (view.resident) continue; + if (!marked) view.unreported = allSubpages; + if (view.armed == 0) continue; + protectSubpages(alias, view.armed, view.protection, "stop arming a compared shared page", view.viewBytes); + view.armed = 0; + } + } + } + + void ComparedPages(std::uintptr_t address, std::size_t bytes, void** pages, std::size_t* count, bool unreported) { + std::lock_guard lock(mutex); + const auto capacity = *count; + *count = 0; + const auto end = address + bytes; + for (auto it = views.lower_bound(address & ~(pageBytes - 1)); it != views.end() && it->first < end; ++it) { + auto& view = it->second; + if (view.resident || !view.page->comparedWhenShared) continue; + const auto listed = static_cast(subpagesOf(it->first, address, end) & (unreported ? view.unreported : allSubpages)); + for (std::size_t sub = 0; sub < subpages; ++sub) { + const auto bit = static_cast(1u << sub); + if ((listed & bit) == 0) continue; + if (*count == capacity) return; + pages[(*count)++] = reinterpret_cast(it->first + sub * subpageBytes); + if (unreported) view.unreported &= static_cast(~bit); + } + } + } + bool HandleWrite(std::uintptr_t address) { std::lock_guard lock(mutex); const auto base = address & ~(pageBytes - 1); @@ -286,7 +358,7 @@ public: } for (std::size_t done = 0; done < bytes; done += pageBytes) { const auto found = views.find(address + done); - if (found != views.end() && found->second.resident) demote(address + done); + if (found != views.end() && found->second.resident) demote(address + done, true); } auto view = views.find(address); if (view == views.end()) throw refuse("the range does not start at a shared view"); @@ -350,6 +422,7 @@ public: if (!watchResident) return false; continue; } + if (it->second.page->comparedWhenShared) return false; if (it->second.protection != PAGE_NOACCESS) continue; for (auto at = std::max(address, it->first); at < std::min(end, it->first + pageBytes); at += 4096) { if ((it->second.collected & (1u << ((at - it->first) / 4096))) == 0) return false; @@ -496,7 +569,7 @@ private: for (auto cursor = address; cursor < end;) { const auto base = cursor & ~(pageBytes - 1); if (const auto found = viewAt(base, position); found != views.end()) { - if (found->second.protection == PAGE_NOACCESS || (found->second.resident && !watchResident)) return false; + if (found->second.protection == PAGE_NOACCESS || (found->second.resident ? !watchResident : found->second.page->comparedWhenShared)) return false; cursor = std::min(end, base + pageBytes); continue; } @@ -522,6 +595,7 @@ private: }(); std::vector aliases; std::uint32_t pins = 0; + bool comparedWhenShared = false; }; struct Section { HANDLE handle; @@ -553,6 +627,7 @@ private: struct Physical { std::weak_ptr page; bool stored = false; + bool comparedWhenShared = false; std::shared_ptr saved; std::size_t savedOffset = 0; }; @@ -569,6 +644,8 @@ private: std::shared_ptr slot; std::size_t viewBytes = pageBytes; std::uint32_t hostImports = 0; + bool aliasDemoted = false; + std::uint8_t unreported = 0; }; struct Run { std::uintptr_t end; @@ -716,7 +793,7 @@ private: return true; } - void demote(std::uintptr_t address) { + void demote(std::uintptr_t address, bool alias = false) { const auto run = std::prev(residentRuns.upper_bound(address)); const auto start = run->first; const auto stop = run->second.end; @@ -734,6 +811,8 @@ private: mapShared(page, view.section->handle, view.offset, view.protection); view.slot->stored = true; view.resident = false; + view.aliasDemoted = alias; + view.unreported = view.page->comparedWhenShared ? allSubpages : std::uint8_t{0}; view.seen.fill(0); view.armed = 0; view.collected = 0; @@ -890,6 +969,9 @@ private: static MEMORY_BASIC_INFORMATION query(std::uintptr_t address) { MEMORY_BASIC_INFORMATION memory{}; +#ifdef APS5_COUNT_MAPPING_QUERIES + queries.fetch_add(1, std::memory_order_relaxed); +#endif if (VirtualQuery(reinterpret_cast(address), &memory, sizeof(memory)) != sizeof(memory)) fail("query guest memory"); return memory; } @@ -1023,6 +1105,9 @@ private: std::map splitAliases; std::size_t granularity = 0x10000; bool watchResident = false; +#ifdef APS5_COUNT_MAPPING_QUERIES + static inline std::atomic queries{0}; +#endif std::mutex mutex; AllocateFunction allocate = nullptr; MapFunction map = nullptr; diff --git a/core/libs/prx/libc/src/DinkumwareAtomics.cpp b/core/libs/prx/libc/src/DinkumwareAtomics.cpp index f8aecb774e..73ee0dbcd1 100644 --- a/core/libs/prx/libc/src/DinkumwareAtomics.cpp +++ b/core/libs/prx/libc/src/DinkumwareAtomics.cpp @@ -172,4 +172,61 @@ int APS5_VABI _Atomic_compare_exchange_weak_nid_postfix(volatile unsigned char* return LockedCompareExchange(flag, size, target, expected, desired); } + +std::uint8_t APS5_VABI __atomic_load_1_nid_postfix(const volatile std::uint8_t* target, int) { return __atomic_load_n(target, __ATOMIC_SEQ_CST); } +std::uint16_t APS5_VABI __atomic_load_2_nid_postfix(const volatile std::uint16_t* target, int) { return __atomic_load_n(target, __ATOMIC_SEQ_CST); } +std::uint32_t APS5_VABI __atomic_load_4_nid_postfix(const volatile std::uint32_t* target, int) { return __atomic_load_n(target, __ATOMIC_SEQ_CST); } +std::uint64_t APS5_VABI __atomic_load_8_nid_postfix(const volatile std::uint64_t* target, int) { return __atomic_load_n(target, __ATOMIC_SEQ_CST); } + +void APS5_VABI __atomic_store_1_nid_postfix(volatile std::uint8_t* target, std::uint8_t value, int) { Store(target, value); } +void APS5_VABI __atomic_store_2_nid_postfix(volatile std::uint16_t* target, std::uint16_t value, int) { Store(target, value); } +void APS5_VABI __atomic_store_4_nid_postfix(volatile std::uint32_t* target, std::uint32_t value, int) { Store(target, value); } +void APS5_VABI __atomic_store_8_nid_postfix(volatile std::uint64_t* target, std::uint64_t value, int) { Store(target, value); } + +std::uint8_t APS5_VABI __atomic_exchange_1_nid_postfix(volatile std::uint8_t* target, std::uint8_t value, int) { return Exchange(target, value); } +std::uint16_t APS5_VABI __atomic_exchange_2_nid_postfix(volatile std::uint16_t* target, std::uint16_t value, int) { return Exchange(target, value); } +std::uint32_t APS5_VABI __atomic_exchange_4_nid_postfix(volatile std::uint32_t* target, std::uint32_t value, int) { return Exchange(target, value); } +std::uint64_t APS5_VABI __atomic_exchange_8_nid_postfix(volatile std::uint64_t* target, std::uint64_t value, int) { return Exchange(target, value); } + +int APS5_VABI __atomic_compare_exchange_1_nid_postfix(volatile std::uint8_t* target, std::uint8_t* expected, std::uint8_t desired, int, int) { + return CompareExchange(target, expected, desired); +} + +int APS5_VABI __atomic_compare_exchange_2_nid_postfix(volatile std::uint16_t* target, std::uint16_t* expected, std::uint16_t desired, int, int) { + return CompareExchange(target, expected, desired); +} + +int APS5_VABI __atomic_compare_exchange_4_nid_postfix(volatile std::uint32_t* target, std::uint32_t* expected, std::uint32_t desired, int, int) { + return CompareExchange(target, expected, desired); +} + +int APS5_VABI __atomic_compare_exchange_8_nid_postfix(volatile std::uint64_t* target, std::uint64_t* expected, std::uint64_t desired, int, int) { + return CompareExchange(target, expected, desired); +} + +std::uint8_t APS5_VABI __atomic_fetch_add_1_nid_postfix(volatile std::uint8_t* target, std::uint8_t value, int) { return __atomic_fetch_add(target, value, __ATOMIC_SEQ_CST); } +std::uint16_t APS5_VABI __atomic_fetch_add_2_nid_postfix(volatile std::uint16_t* target, std::uint16_t value, int) { return __atomic_fetch_add(target, value, __ATOMIC_SEQ_CST); } +std::uint32_t APS5_VABI __atomic_fetch_add_4_nid_postfix(volatile std::uint32_t* target, std::uint32_t value, int) { return __atomic_fetch_add(target, value, __ATOMIC_SEQ_CST); } +std::uint64_t APS5_VABI __atomic_fetch_add_8_nid_postfix(volatile std::uint64_t* target, std::uint64_t value, int) { return __atomic_fetch_add(target, value, __ATOMIC_SEQ_CST); } + +std::uint8_t APS5_VABI __atomic_fetch_sub_1_nid_postfix(volatile std::uint8_t* target, std::uint8_t value, int) { return __atomic_fetch_sub(target, value, __ATOMIC_SEQ_CST); } +std::uint16_t APS5_VABI __atomic_fetch_sub_2_nid_postfix(volatile std::uint16_t* target, std::uint16_t value, int) { return __atomic_fetch_sub(target, value, __ATOMIC_SEQ_CST); } +std::uint32_t APS5_VABI __atomic_fetch_sub_4_nid_postfix(volatile std::uint32_t* target, std::uint32_t value, int) { return __atomic_fetch_sub(target, value, __ATOMIC_SEQ_CST); } +std::uint64_t APS5_VABI __atomic_fetch_sub_8_nid_postfix(volatile std::uint64_t* target, std::uint64_t value, int) { return __atomic_fetch_sub(target, value, __ATOMIC_SEQ_CST); } + +std::uint8_t APS5_VABI __atomic_fetch_and_1_nid_postfix(volatile std::uint8_t* target, std::uint8_t value, int) { return __atomic_fetch_and(target, value, __ATOMIC_SEQ_CST); } +std::uint16_t APS5_VABI __atomic_fetch_and_2_nid_postfix(volatile std::uint16_t* target, std::uint16_t value, int) { return __atomic_fetch_and(target, value, __ATOMIC_SEQ_CST); } +std::uint32_t APS5_VABI __atomic_fetch_and_4_nid_postfix(volatile std::uint32_t* target, std::uint32_t value, int) { return __atomic_fetch_and(target, value, __ATOMIC_SEQ_CST); } +std::uint64_t APS5_VABI __atomic_fetch_and_8_nid_postfix(volatile std::uint64_t* target, std::uint64_t value, int) { return __atomic_fetch_and(target, value, __ATOMIC_SEQ_CST); } + +std::uint8_t APS5_VABI __atomic_fetch_or_1_nid_postfix(volatile std::uint8_t* target, std::uint8_t value, int) { return __atomic_fetch_or(target, value, __ATOMIC_SEQ_CST); } +std::uint16_t APS5_VABI __atomic_fetch_or_2_nid_postfix(volatile std::uint16_t* target, std::uint16_t value, int) { return __atomic_fetch_or(target, value, __ATOMIC_SEQ_CST); } +std::uint32_t APS5_VABI __atomic_fetch_or_4_nid_postfix(volatile std::uint32_t* target, std::uint32_t value, int) { return __atomic_fetch_or(target, value, __ATOMIC_SEQ_CST); } +std::uint64_t APS5_VABI __atomic_fetch_or_8_nid_postfix(volatile std::uint64_t* target, std::uint64_t value, int) { return __atomic_fetch_or(target, value, __ATOMIC_SEQ_CST); } + +std::uint8_t APS5_VABI __atomic_fetch_xor_1_nid_postfix(volatile std::uint8_t* target, std::uint8_t value, int) { return __atomic_fetch_xor(target, value, __ATOMIC_SEQ_CST); } +std::uint16_t APS5_VABI __atomic_fetch_xor_2_nid_postfix(volatile std::uint16_t* target, std::uint16_t value, int) { return __atomic_fetch_xor(target, value, __ATOMIC_SEQ_CST); } +std::uint32_t APS5_VABI __atomic_fetch_xor_4_nid_postfix(volatile std::uint32_t* target, std::uint32_t value, int) { return __atomic_fetch_xor(target, value, __ATOMIC_SEQ_CST); } +std::uint64_t APS5_VABI __atomic_fetch_xor_8_nid_postfix(volatile std::uint64_t* target, std::uint64_t value, int) { return __atomic_fetch_xor(target, value, __ATOMIC_SEQ_CST); } + } diff --git a/core/libs/prx/libc/src/DinkumwareInternals.cpp b/core/libs/prx/libc/src/DinkumwareInternals.cpp index 163a7362fa..ab80ee57d9 100644 --- a/core/libs/prx/libc/src/DinkumwareInternals.cpp +++ b/core/libs/prx/libc/src/DinkumwareInternals.cpp @@ -10,7 +10,10 @@ extern "C" { int* APS5_VABI __error_nid_postfix(); double APS5_VABI strtod_nid_postfix(const char* str, char** endptr); +float APS5_VABI strtof_nid_postfix(const char* str, char** endptr); +long double APS5_VABI strtold_nid_postfix(const char* str, char** endptr); long long APS5_VABI strtoll_nid_postfix(const char* str, char** endptr, int base); +unsigned long long APS5_VABI strtoull_nid_postfix(const char* str, char** endptr, int base); double APS5_VABI sin_nid_postfix(double x); double APS5_VABI cos_nid_postfix(double x); float APS5_VABI sinf_nid_postfix(float x); @@ -28,6 +31,7 @@ float APS5_VABI log2f_nid_postfix(float x); namespace { constexpr long long ExponentLimit = 1000000000; +constexpr int InvalidArgument = 22; constexpr int RangeError = 34; bool Hexadecimal(const char* text) { @@ -36,39 +40,115 @@ bool Hexadecimal(const char* text) { return text[0] == '0' && (text[1] == 'x' || text[1] == 'X'); } +template +TFloat ScaledFloat(const char* text, char** end, std::int64_t pten, int* error, TFloat (APS5_VABI *parse)(const char*, char**), const char* function) { + int& guestErrno = *__error_nid_postfix(); + const int saved = guestErrno; + if (error != nullptr) *error = 0; + guestErrno = 0; + char* stop = nullptr; + TFloat value = parse(text, &stop); + if (end != nullptr) *end = stop; + const bool overflowed = std::isinf(value) && guestErrno == RangeError; + if (pten != 0 && stop != text && (std::isfinite(value) || overflowed)) { + if (Hexadecimal(text)) NotImplemented_nid_no_patch(function); + std::string number(text, static_cast(stop)); + long long exponent = 0; + const auto marker = number.find_first_of("eE"); + if (marker != std::string::npos) { + exponent = std::strtoll(number.c_str() + marker + 1, nullptr, 10); + number.resize(marker); + } + exponent = std::clamp(exponent, -ExponentLimit, ExponentLimit) + std::clamp(pten, -ExponentLimit, ExponentLimit); + number += 'e'; + number += std::to_string(exponent); + guestErrno = 0; + value = parse(number.c_str(), nullptr); + } + if (guestErrno == RangeError && (value == 0 || std::isinf(value))) { + if (error != nullptr) *error = 1; + } else if (guestErrno == 0 || guestErrno == RangeError) { + guestErrno = saved; + } + return value; +} + +bool InvalidBase(const char* text, char** end, int base, int* error) { + if (base >= 0 && base != 1 && base <= 36) return false; + if (end != nullptr) *end = const_cast(text); + if (error != nullptr) *error = InvalidArgument; + return true; +} + +template +TInteger ParsedInteger(const char* text, char** end, int base, int* error, TInteger (APS5_VABI *parse)(const char*, char**, int)) { + if (error != nullptr) *error = 0; + if (InvalidBase(text, end, base, error)) return 0; + int& guestErrno = *__error_nid_postfix(); + const int saved = guestErrno; + guestErrno = 0; + const TInteger value = parse(text, end, base); + if (error != nullptr) *error = guestErrno; + if (guestErrno == 0) guestErrno = saved; + return value; +} + } extern "C" { +double APS5_VABI _Stodx_nid_postfix(const char* text, char** end, std::int64_t pten, int* error) { + return ScaledFloat(text, end, pten, error, strtod_nid_postfix, "_Stodx (hexadecimal text with a power-of-ten scale)"); +} + +float APS5_VABI _Stofx_nid_postfix(const char* text, char** end, std::int64_t pten, int* error) { + return ScaledFloat(text, end, pten, error, strtof_nid_postfix, "_Stofx (hexadecimal text with a power-of-ten scale)"); +} + +long double APS5_VABI _Stoldx_nid_postfix(const char* text, char** end, std::int64_t pten, int* error) { + return ScaledFloat(text, end, pten, error, strtold_nid_postfix, "_Stoldx (hexadecimal text with a power-of-ten scale)"); +} + double APS5_VABI _Stod_nid_postfix(const char* text, char** end, std::int64_t pten) { + return _Stodx_nid_postfix(text, end, pten, nullptr); +} + +float APS5_VABI _Stof_nid_postfix(const char* text, char** end, std::int64_t pten) { + return _Stofx_nid_postfix(text, end, pten, nullptr); +} + +long double APS5_VABI _Stold_nid_postfix(const char* text, char** end, std::int64_t pten) { + return _Stoldx_nid_postfix(text, end, pten, nullptr); +} + +long long APS5_VABI _Stollx_nid_postfix(const char* text, char** end, int base, int* error) { + return ParsedInteger(text, end, base, error, strtoll_nid_postfix); +} + +unsigned long long APS5_VABI _Stoullx_nid_postfix(const char* text, char** end, int base, int* error) { + return ParsedInteger(text, end, base, error, strtoull_nid_postfix); +} + +std::uint64_t APS5_VABI _Stoulx_nid_postfix(const char* text, char** end, int base, int* error) { + return _Stoullx_nid_postfix(text, end, base, error); +} + +std::int64_t APS5_VABI _Stolx_nid_postfix(const char* text, char** end, int base, int* error) { + if (error != nullptr) *error = 0; + if (InvalidBase(text, end, base, error)) return 0; int& guestErrno = *__error_nid_postfix(); const int saved = guestErrno; guestErrno = 0; - char* stop = nullptr; - const double value = strtod_nid_postfix(text, &stop); - if (end != nullptr) *end = stop; - const bool overflowed = std::isinf(value) && guestErrno == RangeError; - if (pten == 0 || stop == text || !(std::isfinite(value) || overflowed)) { - if (guestErrno == 0) guestErrno = saved; - return value; - } - if (Hexadecimal(text)) NotImplemented_nid_no_patch("_Stod (hexadecimal text with a power-of-ten scale)"); - std::string number(text, static_cast(stop)); - long long exponent = 0; - const auto marker = number.find_first_of("eE"); - if (marker != std::string::npos) { - exponent = std::strtoll(number.c_str() + marker + 1, nullptr, 10); - number.resize(marker); - } - exponent = std::clamp(exponent, -ExponentLimit, ExponentLimit) + std::clamp(pten, -ExponentLimit, ExponentLimit); - number += 'e'; - number += std::to_string(exponent); - guestErrno = saved; - return strtod_nid_postfix(number.c_str(), nullptr); + const long long value = strtoll_nid_postfix(text, end, base); + if (error != nullptr) *error = guestErrno; + guestErrno = 0; + static_cast(strtoull_nid_postfix(text, nullptr, base)); + if (guestErrno == 0) guestErrno = saved; + return value; } long long APS5_VABI _Stoll_nid_postfix(const char* text, char** end, int base) { - return strtoll_nid_postfix(text, end, base); + return _Stollx_nid_postfix(text, end, base, nullptr); } double APS5_VABI _Sin_nid_postfix(double x, unsigned int quadrant) { diff --git a/core/libs/prx/libc/src/Errors.cpp b/core/libs/prx/libc/src/Errors.cpp index 4e2d3f08bb..3ca96f18ce 100644 --- a/core/libs/prx/libc/src/Errors.cpp +++ b/core/libs/prx/libc/src/Errors.cpp @@ -18,7 +18,7 @@ constexpr const char* messages[] = { "Is a directory", "Invalid argument", "Too many open files in system", "Too many open files", "Inappropriate ioctl for device", "Text file busy", "File too large", "No space left on device", "Illegal seek", - "Read-only file system", "Too many links", "Broken pipe", + "Read-only filesystem", "Too many links", "Broken pipe", "Numerical argument out of domain", "Result too large", "Resource temporarily unavailable", "Operation now in progress", "Operation already in progress", "Socket operation on non-socket", "Destination address required", "Message too long", "Protocol wrong type for socket", @@ -44,15 +44,15 @@ constexpr const char* messages[] = { }; static_assert(sizeof(messages) / sizeof(messages[0]) == 97); bool Known(int error) { return error >= 0 && error < 97; } +bool NamedByStrerror(int error) { return error > 0 && error < 95; } } extern "C" { char* APS5_VABI strerror_nid_postfix(int error) { thread_local char buffer[128]; - const int saved = *__error_nid_postfix(); - if (Known(error)) std::snprintf(buffer, sizeof(buffer), "%s", messages[error]); - else std::snprintf(buffer, sizeof(buffer), "Unknown error: %d", error); - *__error_nid_postfix() = Known(error) ? saved : 22; + if (error == 0) std::snprintf(buffer, sizeof(buffer), "No error"); + else if (NamedByStrerror(error)) std::snprintf(buffer, sizeof(buffer), "%s", messages[error]); + else std::snprintf(buffer, sizeof(buffer), "error_code=0x%08x", static_cast(error)); return buffer; } diff --git a/core/libs/prx/libc/src/FileAndHeap.cpp b/core/libs/prx/libc/src/FileAndHeap.cpp index 67f4ddd394..c5a44fa29e 100644 --- a/core/libs/prx/libc/src/FileAndHeap.cpp +++ b/core/libs/prx/libc/src/FileAndHeap.cpp @@ -611,6 +611,8 @@ int APS5_VABI fflush_nid_postfix(FileStream* stream) { if (stream && (stream->IsRandomDevice() || stream->IsDirectory())) return 0; #ifdef __linux__ if (native && !__fwriting(native)) return 0; +#elif defined(__APPLE__) + if (native && !(native->_flags & __SWR)) return 0; #endif const int result = std::fflush(native); if (stream) stream->SyncStatus(); diff --git a/core/libs/prx/libc/src/FloatingPoint.cpp b/core/libs/prx/libc/src/FloatingPoint.cpp index e3717ef61b..e63d702609 100644 --- a/core/libs/prx/libc/src/FloatingPoint.cpp +++ b/core/libs/prx/libc/src/FloatingPoint.cpp @@ -119,6 +119,10 @@ int APS5_VABI fegettrapenable_nid_postfix() { return ~control & Exceptions; } +int APS5_VABI fegetexcept_nid_postfix() { + return fegettrapenable_nid_postfix(); +} + int APS5_VABI fesettrapenable_nid_postfix(int exceptions) { const auto mask = static_cast(exceptions); if (mask > Exceptions) return 1; diff --git a/core/libs/prx/libc/src/Formatting.cpp b/core/libs/prx/libc/src/Formatting.cpp index 5a35df68eb..ad2e65acda 100644 --- a/core/libs/prx/libc/src/Formatting.cpp +++ b/core/libs/prx/libc/src/Formatting.cpp @@ -40,13 +40,19 @@ bool RequiresGuestFormatting(const char* format) { ++format; if (*format == '%') { ++format; continue; } while (*format && std::strchr("-+ #0'0123456789.*$", *format)) ++format; - if (*format == 'S' || *format == 'C' || *format == 'm' || *format == 'p' || + if (*format == 'S' || *format == 'C' || *format == 'p' || (*format == 'l' && (format[1] == 's' || format[1] == 'c' || format[1] == 'p'))) return true; + while (*format && std::strchr("hljztL", *format)) ++format; + if (*format && !std::strchr(LibcDetail::KnownConversions, *format)) return true; if (*format) ++format; } return false; } +std::size_t HostFormatSize(std::size_t size) { + return std::min(size, static_cast(INT_MAX) + 1); +} + bool HasGuestWideScan(const char* format) { if (format == nullptr) return false; while ((format = std::strchr(format, '%')) != nullptr) { @@ -361,13 +367,6 @@ int APS5_VABI fprintf_nid_postfix(FileStream* stream, const char* format, ...) { return result; } -static bool SizeOverflowsInt(char* buffer, size_t size) { - if (size == 0 || size - 1 <= static_cast(INT_MAX)) return false; - errno = 84; - if (buffer != nullptr) *buffer = '\0'; - return true; -} - int APS5_VABI fscanf_nid_postfix(FileStream* stream, const char* format, ...) { if (stream && !stream->IsOpen()) { errno = 9; return -1; } auto* native = GetNativeStream(stream); @@ -410,7 +409,6 @@ int APS5_VABI libc_printf_nid_postfix(const char* format, ...) { } int APS5_VABI snprintf_nid_postfix(char* buffer, size_t size, const char* format, ...) { - if (SizeOverflowsInt(buffer, size)) return -1; __builtin_sysv_va_list args; __builtin_sysv_va_start(args, format); const int result = LibcDetail::FormatWindows(buffer, size, format, args); @@ -447,10 +445,9 @@ int APS5_VABI libc_printf_nid_postfix(const char* format, ...) { } int APS5_VABI snprintf_nid_postfix(char* buffer, size_t size, const char* format, ...) { - if (SizeOverflowsInt(buffer, size)) return -1; std::va_list args; va_start(args, format); - const int result = RequiresGuestFormatting(format) ? LibcDetail::FormatWindows(buffer, size, format, args) : std::vsnprintf(buffer, size, format, args); + const int result = RequiresGuestFormatting(format) ? LibcDetail::FormatWindows(buffer, size, format, args) : std::vsnprintf(buffer, HostFormatSize(size), format, args); va_end(args); return result; } @@ -550,14 +547,13 @@ int APS5_VABI vsprintf_nid_postfix(char* str, const char* format, VaList* args) } int APS5_VABI vsnprintf_nid_postfix(char* str, size_t size, const char* format, VaList* c) { - if (SizeOverflowsInt(str, size)) return -1; #ifdef _WIN32 return LibcDetail::FormatWindows(str, size, format, c); #else if (RequiresGuestFormatting(format)) return LibcDetail::FormatWindows(str, size, format, c); std::va_list copy; va_copy(copy, *reinterpret_cast(c)); - const int result = std::vsnprintf(str, size, format, copy); + const int result = std::vsnprintf(str, HostFormatSize(size), format, copy); va_end(copy); return result; #endif diff --git a/core/libs/prx/libc/src/FormattingWide.cpp b/core/libs/prx/libc/src/FormattingWide.cpp index a2b3d0fe7d..ac1f820c1d 100644 --- a/core/libs/prx/libc/src/FormattingWide.cpp +++ b/core/libs/prx/libc/src/FormattingWide.cpp @@ -399,7 +399,8 @@ int APS5_VABI putwchar_nid_postfix(char16_t value) { } char16_t* APS5_VABI fgetws_nid_postfix(char16_t* buffer, int size, FileStream* stream) { - if (buffer == nullptr || stream == nullptr || size <= 0) { + if (size < 2) return nullptr; + if (buffer == nullptr || stream == nullptr) { if (stream != nullptr) stream->SetError(); errno = 22; return nullptr; @@ -407,14 +408,11 @@ char16_t* APS5_VABI fgetws_nid_postfix(char16_t* buffer, int size, FileStream* s int count = 0; while (count < size - 1) { const std::int32_t character = fgetwc_nid_postfix(stream); - if (character == -1) { - if (count == 0) return nullptr; - break; - } + if (character == -1) break; buffer[count++] = static_cast(character); if (character == static_cast(u'\n')) break; } - if (ferror_nid_postfix(stream) != 0) return nullptr; + if (count == 0) return nullptr; buffer[count] = 0; return buffer; } diff --git a/core/libs/prx/libc/src/General.cpp b/core/libs/prx/libc/src/General.cpp index 52df6b5cd9..34c4457efe 100644 --- a/core/libs/prx/libc/src/General.cpp +++ b/core/libs/prx/libc/src/General.cpp @@ -574,6 +574,36 @@ extern "C" std::FILE* HostStandardErrorFile_nid_no_patch() { if (std::FILE* kept = g_hostStandardErrorFile.load(std::memory_order_acquire)) return kept; return __acrt_iob_func(2); } + +namespace { +std::mutex g_hostStandardStreamMutex; + +int keepHostStandardStream(int standard) { + auto& slot = standard == 1 ? g_hostStandardOutputFile : g_hostStandardErrorFile; + if (auto* stream = slot.load(std::memory_order_acquire)) return ::_fileno(stream); + const std::unique_ptr unlock(__acrt_iob_func(standard), ::_unlock_file); + ::_lock_file(unlock.get()); + const std::lock_guard lock(g_hostStandardStreamMutex); + if (auto* stream = slot.load(std::memory_order_acquire)) return ::_fileno(stream); + const int saved = DuplicateHostDescriptor_nid_no_patch(standard); + if (saved < 0) return -1; + if (!AdoptHostStandardStream_nid_no_patch(standard, saved)) { + const int error = errno; + ::_close(saved); + errno = error; + return -1; + } + return saved; +} +} + +extern "C" int KeepHostStandardError_nid_no_patch() { + return keepHostStandardStream(2); +} + +extern "C" int KeepHostStandardOutput_nid_no_patch() { + return keepHostStandardStream(1); +} #endif extern "C" void AddPathAlias_nid_no_patch(const char* guestPrefix, const char* hostPath) { diff --git a/core/libs/prx/libc/src/GuestArena.cpp b/core/libs/prx/libc/src/GuestArena.cpp index 6a3f41cfc3..65f574aded 100644 --- a/core/libs/prx/libc/src/GuestArena.cpp +++ b/core/libs/prx/libc/src/GuestArena.cpp @@ -519,6 +519,22 @@ bool GuestArenaResidentSpan_nid_postfix(std::uintptr_t address, std::size_t byte return WindowsMappings::Get().ResidentSpan(address, bytes, start, end); } +void GuestArenaCompareWhenShared_nid_no_patch(std::uintptr_t address, std::size_t bytes) { + WindowsMappings::Get().CompareWhenShared(address, bytes); +} + +void GuestArenaCompareResidentWhenShared_nid_no_patch(std::uintptr_t address, std::size_t bytes) { + WindowsMappings::Get().CompareWhenShared(address, bytes, true); +} + +void GuestArenaComparedPages_nid_no_patch(std::uintptr_t address, std::size_t bytes, void** pages, std::size_t* count) { + WindowsMappings::Get().ComparedPages(address, bytes, pages, count, false); +} + +void GuestArenaTakeUnreportedPages_nid_no_patch(std::uintptr_t address, std::size_t bytes, void** pages, std::size_t* count) { + WindowsMappings::Get().ComparedPages(address, bytes, pages, count, true); +} + void GuestArenaForgetSection_nid_postfix(void* section) { WindowsMappings::Get().ForgetSection(section); } diff --git a/core/libs/prx/libc/src/HeapDiagnostics.cpp b/core/libs/prx/libc/src/HeapDiagnostics.cpp index 04324b8d39..c8f99ed0d2 100644 --- a/core/libs/prx/libc/src/HeapDiagnostics.cpp +++ b/core/libs/prx/libc/src/HeapDiagnostics.cpp @@ -4,8 +4,21 @@ #include #include #include +#include #include +#ifdef _WIN32 +#ifndef NOMINMAX +#define NOMINMAX +#endif +#include +#else +#include +#include +#endif + +extern "C" std::FILE* GuestStandardOutputFile_nid_no_patch(); + namespace { std::uint64_t mspaceAtomicIdMask = 0; @@ -19,6 +32,26 @@ static_assert(sizeof(LibcHeapInfo) == 32); static_assert(offsetof(LibcHeapInfo, mspace_atomic_id_mask) == 16); static_assert(offsetof(LibcHeapInfo, mstate_table) == 24); +constexpr std::size_t MspaceNameOffset = 0x3B0; +constexpr std::size_t MspaceNumberOffset = 0x3F4; +constexpr std::uint32_t SegmentOutOfRange = 1; + +unsigned long CurrentProcessId() { +#ifdef _WIN32 + return GetCurrentProcessId(); +#else + return static_cast(::getpid()); +#endif +} + +unsigned long CurrentThreadId() { +#ifdef _WIN32 + return GetCurrentThreadId(); +#else + return static_cast(::syscall(SYS_gettid)); +#endif +} + } extern "C" { @@ -39,6 +72,17 @@ void APS5_VABI sceLibcInternalBacktraceForGame_nid_postfix(const char* heapName) } void APS5_VABI sceLibcInternalHeapErrorReportForGame_nid_postfix(void* heap, void* block, std::uint32_t error) { + if (heap == nullptr) throw std::invalid_argument("sceLibcInternalHeapErrorReportForGame: null heap"); + const auto* state = static_cast(heap); + std::uint32_t number = 0; + std::memcpy(&number, state + MspaceNumberOffset, sizeof(number)); + std::FILE* output = GuestStandardOutputFile_nid_no_patch(); + std::fprintf(output, "[SceLibc] A heap error is detected.\n[SceLibc] -- Mspace Name --\n[SceLibc] %s", state + MspaceNameOffset); + if (number != 0) std::fprintf(output, " (%2u)", number); + std::fputc('\n', output); + if (error == SegmentOutOfRange) std::fprintf(output, "[SceLibc] -- Error reason --\n[SceLibc] segment out of range.\n"); + std::fprintf(output, "[SceLibc] pid:<0x%lx> tid:<0x%lx>\n", CurrentProcessId(), CurrentThreadId()); + std::fflush(output); std::fprintf(stderr, "[libc] heap error 0x%x: heap %p, block %p, reported from guest 0x%llx\n", error, heap, block, static_cast(reinterpret_cast(__builtin_return_address(0)))); } diff --git a/core/libs/prx/libc/src/LocaleSupport.cpp b/core/libs/prx/libc/src/LocaleSupport.cpp index fb1f8042ab..9c3138778b 100644 --- a/core/libs/prx/libc/src/LocaleSupport.cpp +++ b/core/libs/prx/libc/src/LocaleSupport.cpp @@ -220,6 +220,83 @@ std::int64_t APS5_VABI WideCollateHash(const GuestLocale::CollateFacet*, const c const GuestLocale::CollateVtable g_wideCollateVtable{{DestroyCollate, DeleteCollate, RetainFacet, ReleaseFacet}, WideCollateCompare, WideCollateTransform, WideCollateHash}; +bool APS5_VABI CodecvtAlwaysNoconv(const GuestLocale::Facet*) { + return true; +} + +int APS5_VABI CodecvtConstantOne(const GuestLocale::Facet*) { + return 1; +} + +int APS5_VABI CodecvtNoConversion(GuestLocale::Facet*, std::uint64_t*, const char* first1, const char*, const char** next1, char* first3, char*, char** next3) { + *next1 = first1; + *next3 = first3; + return 3; +} + +int APS5_VABI CodecvtUnshift(GuestLocale::Facet*, std::uint64_t*, char* first3, char*, char** next3) { + *next3 = first3; + return 0; +} + +std::size_t APS5_VABI CodecvtLength(GuestLocale::Facet*, std::uint64_t*, const char* first1, const char* last1, std::size_t count) { + return std::min(static_cast(last1 - first1), count); +} + +const GuestLocale::CodecvtVtable g_codecvtVtable{{DestroyCollate, DeleteCollate, RetainFacet, ReleaseFacet}, CodecvtAlwaysNoconv, CodecvtConstantOne, CodecvtConstantOne, CodecvtNoConversion, CodecvtNoConversion, CodecvtUnshift, CodecvtLength}; + +GuestLocale::String* APS5_VABI AssignCString(GuestLocale::String* result, const char* source) { + if (result == nullptr || source == nullptr) throw std::invalid_argument("numpunct string: null argument"); + const std::size_t length = std::strlen(source); + char* data = result->buffer; + std::size_t capacity = GuestStringInlineCapacity; + if (length > GuestStringInlineCapacity) { + capacity = length | GuestStringCapacityMask; + data = static_cast(AllocateStringStorage(capacity + 1)); + } + if (length != 0) std::memcpy(data, source, length); + data[length] = 0; + if (data != result->buffer) result->pointer = data; + result->size = length; + result->capacity = capacity; + return result; +} + +char APS5_VABI NumpunctDecimalPoint(const GuestLocale::NumpunctFacet* self) { + return self->decimalPoint; +} + +char APS5_VABI NumpunctThousandsSep(const GuestLocale::NumpunctFacet* self) { + return self->thousandsSep; +} + +GuestLocale::String* APS5_VABI NumpunctGrouping(GuestLocale::String* result, const GuestLocale::NumpunctFacet* self) { + return AssignCString(result, self->grouping); +} + +GuestLocale::String* APS5_VABI NumpunctFalsename(GuestLocale::String* result, const GuestLocale::NumpunctFacet* self) { + return AssignCString(result, self->falsename); +} + +GuestLocale::String* APS5_VABI NumpunctTruename(GuestLocale::String* result, const GuestLocale::NumpunctFacet* self) { + return AssignCString(result, self->truename); +} + +const GuestLocale::NumpunctVtable g_numpunctVtable{{DestroyCollate, DeleteCollate, RetainFacet, ReleaseFacet}, NumpunctDecimalPoint, NumpunctThousandsSep, NumpunctGrouping, NumpunctFalsename, NumpunctTruename}; + +void APS5_VABI NumPutMethodUnsupported() { + NotImplemented_nid_no_patch("num_put::do_put"); +} + +void APS5_VABI TimePutMethodUnsupported() { + NotImplemented_nid_no_patch("time_put::do_put"); +} + +const GuestLocale::UnsupportedMethodsVtable g_numPutVtable{{DestroyCollate, DeleteCollate, RetainFacet, ReleaseFacet}, {NumPutMethodUnsupported, NumPutMethodUnsupported, NumPutMethodUnsupported, NumPutMethodUnsupported, NumPutMethodUnsupported, NumPutMethodUnsupported, NumPutMethodUnsupported, NumPutMethodUnsupported}}; +const GuestLocale::UnsupportedMethodsVtable g_timePutVtable{{DestroyCollate, DeleteCollate, RetainFacet, ReleaseFacet}, {TimePutMethodUnsupported, TimePutMethodUnsupported, TimePutMethodUnsupported, TimePutMethodUnsupported, TimePutMethodUnsupported, TimePutMethodUnsupported, TimePutMethodUnsupported, TimePutMethodUnsupported}}; + +constexpr std::size_t GuestTimePutFacetSize = 0xb8; + constexpr std::array MakeClassificationTable() { std::array table{}; for (unsigned int value = 0; value < 256; ++value) { @@ -287,6 +364,8 @@ int APS5_VABI isprint_nid_postfix(int c) { return ClassifyCharacter(c, std::ctyp int APS5_VABI isgraph_nid_postfix(int c) { return ClassifyCharacter(c, std::ctype_base::graph); } int APS5_VABI ispunct_nid_postfix(int c) { return ClassifyCharacter(c, std::ctype_base::punct); } int APS5_VABI isxdigit_nid_postfix(int c) { return ClassifyCharacter(c, std::ctype_base::xdigit); } +int APS5_VABI isascii_nid_postfix(int c) { return (c & ~0x7F) == 0; } +int APS5_VABI toascii_nid_postfix(int c) { return c & 0x7F; } int APS5_VABI toupper_nid_postfix(int c) { return ConvertCharacter(c, true); } int APS5_VABI tolower_nid_postfix(int c) { return ConvertCharacter(c, false); } @@ -294,13 +373,19 @@ std::uint64_t _ZNSt5ctypeIcE2idE_nid_postfix = 0; std::uint64_t _ZNSt5ctypeIwE2idE_nid_postfix = 0; std::uint64_t _ZNSt7collateIwE2idE_nid_postfix = 0; std::uint64_t _ZNSt7collateIcE2idE_nid_postfix = 0; +std::uintptr_t _ZTVSt7collateIcE_nid_postfix[9] {}; std::uint64_t _ZNSt7codecvtIcc9_MbstatetE2idE_nid_postfix = 0; std::uintptr_t _ZTVSt7codecvtIcc9_MbstatetE_nid_postfix[16] {}; +std::uintptr_t _ZTVSt7codecvtIwc9_MbstatetE_nid_postfix[13] {}; std::uint64_t _ZNSt7num_putIcSt19ostreambuf_iteratorIcSt11char_traitsIcEEE2idE_nid_postfix = 0; std::uintptr_t _ZTVSt7num_putIcSt19ostreambuf_iteratorIcSt11char_traitsIcEEE_nid_postfix[12] {}; std::uint64_t _ZNSt7num_getIcSt19istreambuf_iteratorIcSt11char_traitsIcEEE2idE_nid_postfix = 0; std::uintptr_t _ZTVSt7num_getIcSt19istreambuf_iteratorIcSt11char_traitsIcEEE_nid_postfix[17] {}; std::uint64_t _ZNSt8time_getIcSt19istreambuf_iteratorIcSt11char_traitsIcEEE2idE_nid_postfix = 0; +std::uint64_t _ZNSt7num_putIwSt19ostreambuf_iteratorIwSt11char_traitsIwEEE2idE_nid_postfix = 0; +std::uintptr_t _ZTVSt7num_putIwSt19ostreambuf_iteratorIwSt11char_traitsIwEEE_nid_postfix[14] {}; +std::uint64_t _ZNSt8numpunctIcE2idE_nid_postfix = 0; +std::uint64_t _ZNSt8time_putIcSt19ostreambuf_iteratorIcSt11char_traitsIcEEE2idE_nid_postfix = 0; std::streamoff _ZSt7_BADOFF_nid_postfix = -1; std::fpos_t _ZSt4_Fpz_nid_postfix {}; @@ -400,6 +485,43 @@ std::size_t APS5_VABI _ZNSt7collateIcE7_GetcatEPPKNSt6locale5facetEPKS1__nid_pos return 1; } +std::size_t APS5_VABI _ZNSt7codecvtIcc9_MbstatetE7_GetcatEPPKNSt6locale5facetEPKS2__nid_postfix(GuestLocale::Facet** facet, const GuestLocale::Implementation* const*) { + if (facet != nullptr && *facet == nullptr) { + auto* object = static_cast(_Znwm_nid_postfix(sizeof(GuestLocale::Facet))); + *object = {&g_codecvtVtable.facet, 0, 0}; + *facet = object; + } + return 2; +} + +std::size_t APS5_VABI _ZNSt8numpunctIcE7_GetcatEPPKNSt6locale5facetEPKS1__nid_postfix(GuestLocale::Facet** facet, const GuestLocale::Implementation* const*) { + if (facet != nullptr && *facet == nullptr) { + auto* punct = static_cast(_Znwm_nid_postfix(sizeof(GuestLocale::NumpunctFacet))); + *punct = {{&g_numpunctVtable.facet, 0, 0}, "", '.', ',', "false", "true"}; + *facet = &punct->base; + } + return 4; +} + +std::size_t APS5_VABI _ZNSt7num_putIcSt19ostreambuf_iteratorIcSt11char_traitsIcEEE7_GetcatEPPKNSt6locale5facetEPKS5__nid_postfix(GuestLocale::Facet** facet, const GuestLocale::Implementation* const*) { + if (facet != nullptr && *facet == nullptr) { + auto* object = static_cast(_Znwm_nid_postfix(sizeof(GuestLocale::Facet))); + *object = {&g_numPutVtable.facet, 0, 0}; + *facet = object; + } + return 4; +} + +std::size_t APS5_VABI _ZNSt8time_putIcSt19ostreambuf_iteratorIcSt11char_traitsIcEEE7_GetcatEPPKNSt6locale5facetEPKS5__nid_postfix(GuestLocale::Facet** facet, const GuestLocale::Implementation* const*) { + if (facet != nullptr && *facet == nullptr) { + auto* object = static_cast(_Znwm_nid_postfix(GuestTimePutFacetSize)); + std::memset(object, 0, GuestTimePutFacetSize); + object->vtable = &g_timePutVtable.facet; + *facet = object; + } + return 5; +} + void APS5_VABI _ZNSt8_LocinfoC1EPKc_nid_postfix(GuestLocale::LocinfoStorage* self, const char* localeName) { if (self == nullptr || localeName == nullptr || std::strcmp(localeName, "C") != 0) throw std::invalid_argument("_Locinfo: only the C locale is supported"); new (self) GuestLocale::LocinfoStorage{}; @@ -467,9 +589,11 @@ std::uint32_t APS5_VABI _Towctrans_nid_postfix(std::uint32_t c, std::uint64_t de std::uint64_t _ZNSt7codecvtIwc9_MbstatetE2idE_nid_postfix = 0; alignas(16) unsigned char _ZSt4cout_nid_postfix[0x400] {}; alignas(16) unsigned char _ZSt4cerr_nid_postfix[0x400] {}; +alignas(16) unsigned char _ZSt4clog_nid_postfix[0x400] {}; alignas(16) unsigned char _ZSt3cin_nid_postfix[0x400] {}; alignas(16) unsigned char _ZSt5wcout_nid_postfix[0x400] {}; alignas(16) unsigned char _ZSt5wcerr_nid_postfix[0x400] {}; +alignas(16) unsigned char _ZSt5wclog_nid_postfix[0x400] {}; alignas(16) unsigned char _ZSt4wcin_nid_postfix[0x400] {}; // Facet vectors are read by inlined guest code, so they come from the guest application heap. diff --git a/core/libs/prx/libc/src/Math.cpp b/core/libs/prx/libc/src/Math.cpp index b9e65e787f..84177877d1 100644 --- a/core/libs/prx/libc/src/Math.cpp +++ b/core/libs/prx/libc/src/Math.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -325,7 +326,6 @@ double APS5_VABI hypot_nid_postfix(double x, double y) { } return std::hypot(x, y); } -float APS5_VABI tanf_nid_postfix(float x) { return std::tan(x); } float APS5_VABI log10f_nid_postfix(float x) { return std::log10(x); } float APS5_VABI logbf_nid_postfix(float x) { return std::logb(x); } double APS5_VABI exp2_nid_postfix(double x) { @@ -462,7 +462,105 @@ double APS5_VABI log2_nid_postfix(double x) { return std::log2(x); } double APS5_VABI log_nid_postfix(double x) { return std::log(x); } double APS5_VABI log1p_nid_postfix(double x) { return std::log1p(x); } float APS5_VABI log1pf_nid_postfix(float x) { return std::log1p(x); } -double APS5_VABI expm1_nid_postfix(double x) { return std::expm1(x); } +static double Expm1(double x) { + const double tiny = 1.0e-300; + const double o_threshold = 7.09782712893383973096e+02; + const double ln2_hi = 6.93147180369123816490e-01; + const double ln2_lo = 1.90821492927058770002e-10; + const double invln2 = 1.44269504088896338700e+00; + const double Q1 = -3.33333333333331316428e-02; + const double Q2 = 1.58730158725481460165e-03; + const double Q3 = -7.93650757867487942473e-05; + const double Q4 = 4.00821782732936239552e-06; + const double Q5 = -2.01099218183624371326e-07; + static volatile double huge = 1.0e+300; + const std::uint64_t bits = std::bit_cast(x); + std::uint32_t hx = static_cast(bits >> 32); + const std::uint32_t xsb = hx & 0x80000000u; + hx &= 0x7fffffff; + if (hx >= 0x4043687a) { + if (hx >= 0x40862e42) { + if (hx >= 0x7ff00000) { + if (((hx & 0xfffff) | static_cast(bits)) != 0) return x + x; + return xsb == 0 ? x : -1.0; + } + if (x > o_threshold) return huge * huge; + } + if (xsb != 0 && x + tiny < 0.0) return tiny - 1.0; + } + double c = 0.0; + std::int32_t k; + if (hx > 0x3fd62e42) { + double hi; + double lo; + if (hx < 0x3ff0a2b2) { + if (xsb == 0) { + hi = x - ln2_hi; + lo = ln2_lo; + k = 1; + } else { + hi = x + ln2_hi; + lo = -ln2_lo; + k = -1; + } + } else { + k = static_cast(invln2 * x + (xsb == 0 ? 0.5 : -0.5)); + const double t = k; + hi = x - t * ln2_hi; + lo = t * ln2_lo; + } + x = hi - lo; + c = (hi - x) - lo; + } else if (hx < 0x3c900000) { + const double t = huge + x; + return x - (t - (huge + x)); + } else { + k = 0; + } + const double hfx = 0.5 * x; + const double hxs = x * hfx; + const double r1 = 1.0 + hxs * (Q1 + hxs * (Q2 + hxs * (Q3 + hxs * (Q4 + hxs * Q5)))); + double t = 3.0 - r1 * hfx; + double e = hxs * ((r1 - t) / (6.0 - x * t)); + if (k == 0) return x - (x * e - hxs); + const double twopk = std::bit_cast(static_cast(static_cast(0x3ff + k) << 20) << 32); + e = (x * (e - c) - c); + e -= hxs; + if (k == -1) return 0.5 * (x - e) - 0.5; + if (k == 1) { + if (x < -0.25) return -2.0 * (e - (x + 0.5)); + return 1.0 + 2.0 * (x - e); + } + double y; + if (k <= -2 || k > 56) { + y = 1.0 - (e - x); + if (k == 1024) { + y = y * 2.0 * 0x1p1023; + } else { + y = y * twopk; + } + return y - 1.0; + } + if (k < 20) { + t = std::bit_cast(static_cast(0x3ff00000u - (0x200000u >> k)) << 32); + y = t - (e - x); + y = y * twopk; + } else { + t = std::bit_cast(static_cast(static_cast((0x3ff - k) << 20)) << 32); + y = x - (e + t); + y += 1.0; + y = y * twopk; + } + return y; +} + +double APS5_VABI expm1_nid_postfix(double x) { + const double result = Expm1(x); + if (std::isinf(result) && std::isfinite(x)) { + errno = ERANGE; + } + return result; +} float APS5_VABI expm1f_nid_postfix(float x) { return std::expm1(x); } float APS5_VABI erff_nid_postfix(float x) { return std::erf(x); } double APS5_VABI erf_nid_postfix(double x) { return std::erf(x); } @@ -479,12 +577,282 @@ double APS5_VABI lgamma_nid_postfix(double x) { return std::lgamma(x); } -float APS5_VABI sinf_nid_postfix(float x) { return std::sin(x); } -float APS5_VABI cosf_nid_postfix(float x) { return std::cos(x); } +static const std::int32_t TrigfIpio2[] = { + 0xA2F983, 0x6E4E44, 0x1529FC, 0x2757D1, 0xF534DD, 0xC0DB62, 0x95993C, 0x439041, 0xFE5163, 0xABDEBB, 0xC561B7, 0x246E3A, + 0x424DD2, 0xE00649, 0x2EEA09, 0xD1921C, 0xFE1DEB, 0x1CB129, 0xA73EE8, 0x8235F5, 0x2EBB44, 0x84E99C, 0x7026B4, 0x5F7E41, + 0x3991D6, 0x398353, 0x39F49C, 0x845F8B, 0xBDF928, 0x3B1FF8, 0x97FFDE, 0x05980F, 0xEF2F11, 0x8B5A0A, 0x6D1F6D, 0x367ECF, + 0x27CB09, 0xB74F46, 0x3F669E, 0x5FEA2D, 0x7527BA, 0xC7EBE5, 0xF17B3D, 0x0739F7, 0x8A5292, 0xEA6BFB, 0x5FB11F, 0x8D5D08, + 0x560330, 0x46FC7B, 0x6BABF0, 0xCFBC20, 0x9AF436, 0x1DA9E3, 0x91615E, 0xE61B08, 0x659985, 0x5F14A0, 0x68408D, 0xFFD880, + 0x4D7327, 0x310606, 0x1556CA, 0x73A8C9, 0x60E27B, 0xC08C6B, +}; + +static const double TrigfPio2Parts[] = { + 1.57079625129699707031e+00, 7.54978941586159635335e-08, 5.39030252995776476554e-15, 3.28200341580791294123e-22, + 1.27065575308067607349e-29, 1.22933308981111328932e-36, 2.73370053816464559624e-44, 2.16741683877804819444e-51, +}; + +static int TrigfKernelRemPio2(const double* x, double* y, int e0) { + const double two24 = 1.67772160000000000000e+07, twon24 = 5.96046447753906250000e-08; + std::int32_t jz, jv, jp, jk, carry, n, iq[20], i, j, k, m, q0, ih; + double z, fw, f[20], fq[20], q[20]; + const std::int32_t jx = 0; + jk = 3; + jp = jk; + jv = (e0 - 3) / 24; + if (jv < 0) jv = 0; + q0 = e0 - 24 * (jv + 1); + j = jv - jx; + m = jx + jk; + for (i = 0; i <= m; i++, j++) f[i] = j < 0 ? 0.0 : static_cast(TrigfIpio2[j]); + for (i = 0; i <= jk; i++) { + for (j = 0, fw = 0.0; j <= jx; j++) fw += x[j] * f[jx + i - j]; + q[i] = fw; + } + jz = jk; +recompute: + for (i = 0, j = jz, z = q[jz]; j > 0; i++, j--) { + fw = static_cast(static_cast(twon24 * z)); + iq[i] = static_cast(z - two24 * fw); + z = q[j - 1] + fw; + } + z = std::scalbn(z, q0); + z -= 8.0 * std::floor(z * 0.125); + n = static_cast(z); + z -= static_cast(n); + ih = 0; + if (q0 > 0) { + i = iq[jz - 1] >> (24 - q0); + n += i; + iq[jz - 1] -= i << (24 - q0); + ih = iq[jz - 1] >> (23 - q0); + } else if (q0 == 0) { + ih = iq[jz - 1] >> 23; + } else if (z >= 0.5) { + ih = 2; + } + if (ih > 0) { + n += 1; + carry = 0; + for (i = 0; i < jz; i++) { + j = iq[i]; + if (carry == 0) { + if (j != 0) { + carry = 1; + iq[i] = 0x1000000 - j; + } + } else { + iq[i] = 0xffffff - j; + } + } + if (q0 > 0) { + switch (q0) { + case 1: iq[jz - 1] &= 0x7fffff; break; + case 2: iq[jz - 1] &= 0x3fffff; break; + } + } + if (ih == 2) { + z = 1.0 - z; + if (carry != 0) z -= std::scalbn(1.0, q0); + } + } + if (z == 0.0) { + j = 0; + for (i = jz - 1; i >= jk; i--) j |= iq[i]; + if (j == 0) { + for (k = 1; iq[jk - k] == 0; k++) { + } + for (i = jz + 1; i <= jz + k; i++) { + f[jx + i] = static_cast(TrigfIpio2[jv + i]); + for (j = 0, fw = 0.0; j <= jx; j++) fw += x[j] * f[jx + i - j]; + q[i] = fw; + } + jz += k; + goto recompute; + } + } + if (z == 0.0) { + jz -= 1; + q0 -= 24; + while (iq[jz] == 0) { + jz--; + q0 -= 24; + } + } else { + z = std::scalbn(z, -q0); + if (z >= two24) { + fw = static_cast(static_cast(twon24 * z)); + iq[jz] = static_cast(z - two24 * fw); + jz += 1; + q0 += 24; + iq[jz] = static_cast(fw); + } else { + iq[jz] = static_cast(z); + } + } + fw = std::scalbn(1.0, q0); + for (i = jz; i >= 0; i--) { + q[i] = fw * static_cast(iq[i]); + fw *= twon24; + } + for (i = jz; i >= 0; i--) { + for (fw = 0.0, k = 0; k <= jp && k <= jz - i; k++) fw += TrigfPio2Parts[k] * q[i + k]; + fq[jz - i] = fw; + } + fw = 0.0; + for (i = jz; i >= 0; i--) fw += fq[i]; + y[0] = ih == 0 ? fw : -fw; + return n & 7; +} + +static int TrigfRemPio2(float x, double* y) { + const double invpio2 = 6.36619772367581382433e-01, pio2_1 = 1.57079631090164184570e+00, pio2_1t = 1.58932547735281966916e-08; + const std::int32_t hx = std::bit_cast(x); + const std::int32_t ix = hx & 0x7fffffff; + if (ix < 0x4dc90fdb) { + volatile double rounded = x * invpio2 + 0x1.8p52; + double fn = rounded; + fn = fn - 0x1.8p52; + const std::int32_t n = static_cast(fn); + const double r = x - fn * pio2_1; + const double w = fn * pio2_1t; + *y = r - w; + return n; + } + if (ix >= 0x7f800000) { + *y = x - x; + return 0; + } + const std::int32_t e0 = (ix >> 23) - 150; + const float z = std::bit_cast(ix - static_cast(static_cast(e0) << 23)); + double tx[1] = {z}, ty[1]; + const int n = TrigfKernelRemPio2(tx, ty, e0); + if (hx < 0) { + *y = -ty[0]; + return -n; + } + *y = ty[0]; + return n; +} + +static float TrigfKernelSin(double x) { + const double S1 = -0x15555554cbac77.0p-55, S2 = 0x111110896efbb2.0p-59, S3 = -0x1a00f9e2cae774.0p-65, S4 = 0x16cd878c3b46a7.0p-71; + const double z = x * x, w = z * z, r = S3 + z * S4, s = z * x; + return static_cast((x + s * (S1 + z * S2)) + s * w * r); +} + +static float TrigfKernelCos(double x) { + const double C0 = -0x1ffffffd0c5e81.0p-54, C1 = 0x155553e1053a42.0p-57, C2 = -0x16c087e80f1e27.0p-62, C3 = 0x199342e0ee5069.0p-68; + const double z = x * x, w = z * z, r = C2 + z * C3; + return static_cast(((1.0 + z * C0) + w * C1) + (w * z) * r); +} + +float APS5_VABI sinf_nid_postfix(float x) { + const double s1pio2 = std::bit_cast(0x3FF921FB54442D18ull), s2pio2 = std::bit_cast(0x400921FB54442D18ull); + const double s3pio2 = std::bit_cast(0x4012D97C7F3321D2ull), s4pio2 = std::bit_cast(0x401921FB54442D18ull); + double y; + const std::int32_t hx = std::bit_cast(x); + const std::int32_t ix = hx & 0x7fffffff; + if (ix <= 0x3f490fda) { + if (ix < 0x39800000) + if (static_cast(x) == 0) return x; + return TrigfKernelSin(x); + } + if (ix <= 0x407b53d1) { + if (ix <= 0x4016cbe3) return hx > 0 ? TrigfKernelCos(x - s1pio2) : -TrigfKernelCos(x + s1pio2); + return TrigfKernelSin((hx > 0 ? s2pio2 : -s2pio2) - x); + } + if (ix <= 0x40e231d5) { + if (ix <= 0x40afeddf) return hx > 0 ? -TrigfKernelCos(x - s3pio2) : TrigfKernelCos(x + s3pio2); + return TrigfKernelSin(x + (hx > 0 ? -s4pio2 : s4pio2)); + } + if (ix >= 0x7f800000) { + if (ix == 0x7f800000) errno = EDOM; + return x - x; + } + const int n = TrigfRemPio2(x, &y); + switch (n & 3) { + case 0: return TrigfKernelSin(y); + case 1: return TrigfKernelCos(y); + case 2: return TrigfKernelSin(-y); + default: return -TrigfKernelCos(y); + } +} + +static const double TrigfPio2[5] = {0.0, std::bit_cast(0x3FF921FB54442D18ull), std::bit_cast(0x400921FB54442D18ull), + std::bit_cast(0x4012D97C7F3321D2ull), std::bit_cast(0x401921FB54442D18ull)}; + +float APS5_VABI cosf_nid_postfix(float x) { + double y; + const std::int32_t hx = std::bit_cast(x); + const std::int32_t ix = hx & 0x7fffffff; + if (ix <= 0x3f490fda) { + if (ix < 0x39800000) + if (static_cast(x) == 0) return 1.0f; + return TrigfKernelCos(x); + } + if (ix <= 0x407b53d1) { + if (ix > 0x4016cbe3) return -TrigfKernelCos(x + (hx > 0 ? -TrigfPio2[2] : TrigfPio2[2])); + return hx > 0 ? TrigfKernelSin(TrigfPio2[1] - x) : TrigfKernelSin(x + TrigfPio2[1]); + } + if (ix <= 0x40e231d5) { + if (ix > 0x40afeddf) return TrigfKernelCos(x + (hx > 0 ? -TrigfPio2[4] : TrigfPio2[4])); + return hx > 0 ? TrigfKernelSin(x - TrigfPio2[3]) : TrigfKernelSin(-TrigfPio2[3] - x); + } + if (ix >= 0x7f800000) { + if (ix == 0x7f800000) errno = EDOM; + return x - x; + } + const int n = TrigfRemPio2(x, &y); + switch (n & 3) { + case 0: return TrigfKernelCos(y); + case 1: return TrigfKernelSin(-y); + case 2: return -TrigfKernelCos(y); + default: return TrigfKernelSin(y); + } +} + +static float TrigfKernelTan(double x, int iy) { + static const double T[] = {0x15554d3418c99f.0p-54, 0x1112fd38999f72.0p-55, 0x1b54c91d865afe.0p-57, + 0x191df3908c33ce.0p-58, 0x185dadfcecf44e.0p-61, 0x1362b9bf971bcd.0p-59}; + const double z = x * x; + double r = T[4] + z * T[5]; + const double t = T[2] + z * T[3]; + const double w = z * z; + const double s = z * x; + const double u = T[0] + z * T[1]; + r = (x + s * u) + (s * w) * (t + w * r); + if (iy == 1) return static_cast(r); + return static_cast(-1.0 / r); +} + +float APS5_VABI tanf_nid_postfix(float x) { + double y; + const std::int32_t hx = std::bit_cast(x); + const std::int32_t ix = hx & 0x7fffffff; + if (ix <= 0x3f490fda) { + if (ix < 0x39800000) + if (static_cast(x) == 0) return x; + return TrigfKernelTan(x, 1); + } + if (ix <= 0x407b53d1) { + if (ix <= 0x4016cbe3) return TrigfKernelTan(x + (hx > 0 ? -TrigfPio2[1] : TrigfPio2[1]), -1); + return TrigfKernelTan(x + (hx > 0 ? -TrigfPio2[2] : TrigfPio2[2]), 1); + } + if (ix <= 0x40e231d5) { + if (ix <= 0x40afeddf) return TrigfKernelTan(x + (hx > 0 ? -TrigfPio2[3] : TrigfPio2[3]), -1); + return TrigfKernelTan(x + (hx > 0 ? -TrigfPio2[4] : TrigfPio2[4]), 1); + } + if (ix >= 0x7f800000) { + if (ix == 0x7f800000) errno = EDOM; + return x - x; + } + const int n = TrigfRemPio2(x, &y); + return TrigfKernelTan(y, 1 - ((n & 1) << 1)); +} void APS5_VABI sincosf_nid_postfix(float x, float* sinp, float* cosp) { - *sinp = std::sin(x); - *cosp = std::cos(x); + *sinp = sinf_nid_postfix(x); + *cosp = cosf_nid_postfix(x); } double APS5_VABI sin_nid_postfix(double x) { return std::sin(x); } @@ -538,7 +906,21 @@ float APS5_VABI sinhf_nid_postfix(float x) { double APS5_VABI asinh_nid_postfix(double x) { return std::asinh(x); } float APS5_VABI asinhf_nid_postfix(float x) { return std::asinh(x); } double APS5_VABI acosh_nid_postfix(double x) { return std::acosh(x); } -float APS5_VABI acoshf_nid_postfix(float x) { return std::acosh(x); } +float APS5_VABI acoshf_nid_postfix(float x) { + constexpr float ln2 = 6.9314718246e-01f; + const std::uint32_t bits = std::bit_cast(x); + const std::uint32_t magnitude = bits & 0x7fffffffu; + if ((bits & 0x80000000u) != 0 || magnitude < 0x3f800000u) return (x - x) / (x - x); + if (magnitude >= 0x7f800000u) return x + x; + if (magnitude == 0x3f800000u) return 0.0f; + if (magnitude >= 0x4d800000u) return std::log(x) + ln2; + if (magnitude > 0x40000000u) { + const float square = x * x; + return std::log(2.0f * x - 1.0f / (x + std::sqrt(square - 1.0f))); + } + const float t = x - 1.0f; + return AtanhfLog1p(t + std::sqrt(2.0f * t + t * t)); +} double APS5_VABI atanh_nid_postfix(double x) { return std::atanh(x); } float APS5_VABI atanhf_nid_postfix(float x) { @@ -708,7 +1090,33 @@ double APS5_VABI fmod_nid_postfix(double x, double y) { } float APS5_VABI roundf_nid_postfix(float x) { return std::round(x); } double APS5_VABI round_nid_postfix(double x) { return std::round(x); } -float APS5_VABI cbrtf_nid_postfix(float x) { return std::cbrt(x); } +float APS5_VABI cbrtf_nid_postfix(float x) { + const std::uint32_t B1 = 709958130; + const std::uint32_t B2 = 642849266; + std::uint32_t hx = std::bit_cast(x); + const std::uint32_t sign = hx & 0x80000000u; + hx ^= sign; + if (hx >= 0x7f800000u) { + return x + x; + } + float t; + if (hx < 0x00800000u) { + if (hx == 0) { + return x; + } + t = 0x1p24f * x; + const std::uint32_t high = std::bit_cast(t); + t = std::bit_cast(sign | ((high & 0x7fffffffu) / 3 + B2)); + } else { + t = std::bit_cast(sign | (hx / 3 + B1)); + } + double T = t; + double r = T * T * T; + T = T * (static_cast(x) + x + r) / (x + r + r); + r = T * T * T; + T = T * (static_cast(x) + x + r) / (x + r + r); + return static_cast(T); +} float APS5_VABI remainderf_nid_postfix(float x, float y) { return std::remainder(x, y); } float APS5_VABI nextafterf_nid_postfix(float x, float y) { return std::nextafter(x, y); } double APS5_VABI nextafter_nid_postfix(double x, double y) { return std::nextafter(x, y); } @@ -905,7 +1313,29 @@ struct GuestFloatingPointEnvironment { unsigned char other[16]; std::uint32_t mxcsr; }; -static_assert(sizeof(GuestFloatingPointEnvironment) == 32 && offsetof(GuestFloatingPointEnvironment, mxcsr) == 28); +static_assert(sizeof(GuestFloatingPointEnvironment) == 32 && alignof(GuestFloatingPointEnvironment) == 4 && offsetof(GuestFloatingPointEnvironment, mxcsr) == 28); + +int APS5_VABI feupdateenv_nid_postfix(const GuestFloatingPointEnvironment* environment) { + if (environment == nullptr) throw std::invalid_argument("feupdateenv: null environment"); + const auto saved = *environment; + alignas(16) std::array state; + asm volatile("fxsave %0" : "=m"(state)); + const auto supported = state[7] != 0 ? state[7] : 0xffbfu; + if ((saved.mxcsr & ~supported) != 0) throw std::invalid_argument("feupdateenv: unsupported MXCSR bits"); + std::uint16_t status; + asm volatile("fnstsw %0" : "=am"(status)); + const auto pending = (status | _mm_getcsr()) & 0x3f; + asm volatile("fldenv %0" : : "m"(saved) : "st", "st(1)", "st(2)", "st(3)", "st(4)", "st(5)", "st(6)", "st(7)"); + _mm_setcsr(saved.mxcsr); + GuestFloatingPointEnvironment raised; + asm volatile("fnstenv %0" : "=m"(raised)); + raised.status |= pending; + const auto mxcsr = _mm_getcsr() | pending; + asm volatile("fldenv %0" : : "m"(raised) : "st", "st(1)", "st(2)", "st(3)", "st(4)", "st(5)", "st(6)", "st(7)"); + _mm_setcsr(mxcsr); + asm volatile("fwait"); + return 0; +} int APS5_VABI fegetenv_nid_postfix(GuestFloatingPointEnvironment* environment) { if (environment == nullptr) throw std::invalid_argument("fegetenv: null environment"); @@ -914,6 +1344,15 @@ int APS5_VABI fegetenv_nid_postfix(GuestFloatingPointEnvironment* environment) { return 0; } +int APS5_VABI feholdexcept_nid_postfix(GuestFloatingPointEnvironment* environment) { + if (environment == nullptr) throw std::invalid_argument("feholdexcept: null environment"); + const auto csr = _mm_getcsr(); + asm volatile("fnstenv %0\n\tfnclex" : "=m"(*environment)); + environment->mxcsr = csr; + _mm_setcsr((csr & ~0x3fu) | 0x1f80u); + return 0; +} + union SonyDconst { unsigned short _Word[8]; float _Float; diff --git a/core/libs/prx/libc/src/Process.cpp b/core/libs/prx/libc/src/Process.cpp index 9d2f6909b0..c200af881d 100644 --- a/core/libs/prx/libc/src/Process.cpp +++ b/core/libs/prx/libc/src/Process.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -281,3 +282,42 @@ int APS5_VABI daemon_nid_postfix(int noChdir, int noClose) { } } + +namespace { +constexpr int SaRestart = 0x2; +std::atomic registeredSigaction{nullptr}; +std::atomic registeredInterruptSet{nullptr}; + +struct InterruptSigaction { + std::uintptr_t handler; + int flags; + std::uint32_t mask[4]; +}; +static_assert(sizeof(InterruptSigaction) == 32 && offsetof(InterruptSigaction, flags) == 8 && offsetof(InterruptSigaction, mask) == 12); +} + +extern "C" { + +void RegisterSigaction_nid_no_patch(SigactionFn_nid_no_patch fn, std::uint32_t* interruptSet) { + registeredInterruptSet.store(interruptSet, std::memory_order_release); + registeredSigaction.store(fn, std::memory_order_release); +} + +int APS5_VABI siginterrupt_nid_postfix(int guest, int flag) { + const auto sigaction = registeredSigaction.load(std::memory_order_acquire); + auto* const interruptSet = registeredInterruptSet.load(std::memory_order_acquire); + if (sigaction == nullptr || interruptSet == nullptr) throw std::runtime_error("siginterrupt: libkernel has not registered sigaction"); + InterruptSigaction action{}; + if (sigaction(guest, nullptr, &action) != 0) return -1; + const auto index = static_cast(guest - 1); + if (flag) { + interruptSet[index >> 5] |= 1u << (index & 31); + action.flags &= ~SaRestart; + } else { + interruptSet[index >> 5] &= ~(1u << (index & 31)); + action.flags |= SaRestart; + } + return sigaction(guest, &action, nullptr); +} + +} diff --git a/core/libs/prx/libc/src/Random.cpp b/core/libs/prx/libc/src/Random.cpp index 06cf1d789d..5fee1280e8 100644 --- a/core/libs/prx/libc/src/Random.cpp +++ b/core/libs/prx/libc/src/Random.cpp @@ -1,4 +1,5 @@ #include "prx/libc/include/general/VabiMacros.hpp" +#include "prx/libc/include/HostEntropy.hpp" #include #include #include @@ -17,15 +18,7 @@ constexpr std::array MinimumStateBytes {8, 32, 64, 128, constexpr std::array Degrees {0, 7, 15, 31, 63}; constexpr std::array Separations {0, 3, 1, 3, 1}; -std::uint32_t g_defaultTable[] { - 3, - 0x2cf41758, 0x27bb3711, 0x4916d4d1, 0x7b02f59f, 0x9b8e28eb, 0xc0e80269, - 0x696f5c16, 0x878f1ff5, 0x52d9c07f, 0x916a06cd, 0xb50b3a20, 0x2776970a, - 0xee4eb2a6, 0xe94640ec, 0xb1d65612, 0x9d1ed968, 0x1043f6b7, 0xa3432a76, - 0x17eacbb9, 0x3c09e2eb, 0x04f8c2b3, 0x708a1f57, 0xee341814, 0x95d0e4d2, - 0xb06f216c, 0x8bd2e72e, 0x8f7c38d7, 0xcfc6a8fc, 0x02a59495, 0xa20d2a69, - 0xe29d12d1 -}; +std::uint32_t g_defaultTable[32]{}; struct AdditiveFeedbackState { char* table; @@ -50,12 +43,13 @@ void StoreWord(char* table, int index, std::uint32_t value) { } std::uint32_t GoodRand(std::uint32_t context) { - std::int32_t x = static_cast(context % 0x7ffffffeu) + 1; - const std::int32_t high = x / 127773; - const std::int32_t low = x % 127773; + std::int64_t x = static_cast(context); + if (x == 0) x = 123459876; + const std::int64_t high = x / 127773; + const std::int64_t low = x % 127773; x = 16807 * low - 2836 * high; if (x < 0) x += 0x7fffffff; - return static_cast(x - 1); + return static_cast(x); } std::uint32_t NextLocked() { @@ -95,6 +89,14 @@ void SaveTypeWordLocked() { StoreWord(state.table, 0, static_cast(word)); } +bool SeedDefaultState() { + SeedLocked(1); + SaveTypeWordLocked(); + return true; +} + +[[maybe_unused]] const bool g_defaultStateSeeded = SeedDefaultState(); + std::uint16_t g_rand48Seed[3]{0x330e, 0xabcd, 0x1234}; constexpr std::uint64_t DefaultRand48Multiplier = 0x5deece66dull; constexpr std::uint16_t DefaultRand48Addend = 0xb; @@ -180,6 +182,16 @@ void APS5_VABI srandom_nid_postfix(std::uint64_t seed) { SeedLocked(static_cast(seed)); } +void APS5_VABI srandomdev_nid_postfix() { + std::lock_guard lock(g_randomLock); + const auto words = g_random.type == LinearCongruentialType ? 1 : g_random.degree; + HostEntropy_nid_no_patch(g_random.table + sizeof(std::uint32_t), static_cast(words) * sizeof(std::uint32_t)); + if (g_random.type != LinearCongruentialType) { + g_random.front = g_random.separation; + g_random.rear = 0; + } +} + char* APS5_VABI initstate_nid_postfix(std::uint64_t seed, char* state, std::int64_t size) { if (size < MinimumStateBytes[0]) return nullptr; if (state == nullptr) throw std::invalid_argument("initstate: null state"); diff --git a/core/libs/prx/libc/src/Rename.cpp b/core/libs/prx/libc/src/Rename.cpp index 973c142ff6..9ac9f5ad57 100644 --- a/core/libs/prx/libc/src/Rename.cpp +++ b/core/libs/prx/libc/src/Rename.cpp @@ -71,6 +71,9 @@ static int RenamePath_nid_no_patch(const std::filesystem::path& source, const st error.clear(); sameFile = fs::equivalent(source, destination, error); if (error == std::errc::no_such_file_or_directory) error.clear(); +#ifdef __APPLE__ + if (error == std::errc::not_supported) error.clear(); +#endif if (error) return FilesystemError(error); if (!sameFile && sourceIsDirectory && !destinationIsDirectory) return GuestEnotdir; if (!sameFile && !sourceIsDirectory && destinationIsDirectory) return GuestEisdir; diff --git a/core/libs/prx/libc/src/ScanningWide.cpp b/core/libs/prx/libc/src/ScanningWide.cpp index 759e475c90..aeca7eae6a 100644 --- a/core/libs/prx/libc/src/ScanningWide.cpp +++ b/core/libs/prx/libc/src/ScanningWide.cpp @@ -321,6 +321,8 @@ private: case u's': case u'c': case u'[': return Text(directive); + case u'S': + case u'C': return Outcome::Matching; case u'n': return Count(directive); default: Fail("unsupported conversion " + std::to_string(directive.conversion)); } diff --git a/core/libs/prx/libc/src/StandardStreams.cpp b/core/libs/prx/libc/src/StandardStreams.cpp index 4d8c34a019..05767fd5f4 100644 --- a/core/libs/prx/libc/src/StandardStreams.cpp +++ b/core/libs/prx/libc/src/StandardStreams.cpp @@ -1,18 +1,70 @@ #include "prx/libc/include/FileStream.hpp" +#include "prx/libc/include/General.hpp" #include "prx/libc/include/general/VabiMacros.hpp" #include #include #include #include #ifdef _WIN32 +#include #include +#include #else #include #include #endif +#ifdef _WIN32 +extern "C" _invalid_parameter_handler _set_thread_local_invalid_parameter_handler(_invalid_parameter_handler); +#endif + namespace { -#ifndef _WIN32 +#ifdef _WIN32 +constexpr std::intptr_t NoStreamHandle = -2; + +std::intptr_t StandardHandle(int descriptor) { + const auto previous = _set_thread_local_invalid_parameter_handler([](const wchar_t*, const wchar_t*, const wchar_t*, unsigned int, std::uintptr_t) {}); + const std::intptr_t handle = ::_get_osfhandle(descriptor); + _set_thread_local_invalid_parameter_handler(previous); + return handle; +} + +} + +extern "C" void PrepareStandardStreams_nid_no_patch() { + static const bool ready = [] { + for (int descriptor = 0; descriptor <= 2; ++descriptor) { + const std::intptr_t handle = StandardHandle(descriptor); + if (handle != -1 && handle != NoStreamHandle) continue; + auto* const stream = __acrt_iob_func(descriptor); + const int existing = ::_fileno(stream); + if (existing >= 0) { + const std::intptr_t existingHandle = StandardHandle(existing); + if (existingHandle != -1 && existingHandle != NoStreamHandle) { + if (::_dup2(existing, descriptor) != 0) + throw std::runtime_error("cannot restore a standard descriptor from its stream"); + continue; + } + } + const int opened = ::_open("NUL", (descriptor == 0 ? _O_RDONLY : _O_WRONLY) | _O_BINARY); + if (opened < 0) throw std::runtime_error("cannot open NUL for a missing standard descriptor"); + if (opened > descriptor) { + const int moved = ::_dup2(opened, descriptor); + ::_close(opened); + if (moved != 0) throw std::runtime_error("cannot restore a missing standard descriptor"); + } + if (::_fileno(stream) != descriptor && ::_close(descriptor) != 0) + throw std::runtime_error("cannot release a restored standard descriptor"); + if (::_wfreopen(L"NUL", descriptor == 0 ? L"rb" : L"wb", stream) != stream || ::_fileno(stream) != descriptor) + throw std::runtime_error("cannot attach a standard stream to its NUL descriptor"); + } + return true; + }(); + static_cast(ready); +} + +namespace { +#else const bool standardDescriptorsOpen = [] { for (int descriptor = 0; descriptor <= 2; ++descriptor) { if (::fcntl(descriptor, F_GETFD) != -1 || errno != EBADF) continue; @@ -28,6 +80,8 @@ const bool standardDescriptorsOpen = [] { FileStream input{stdin}; } extern "C" { +void APS5_VABI flockfile_nid_postfix(FileStream*); +void APS5_VABI funlockfile_nid_postfix(FileStream*); FileStream* __stdinp_nid_postfix = &input; FileStream* __stdoutp_nid_postfix = &_Stdout_nid_postfix; FileStream* __stderrp_nid_postfix = &_Stderr_nid_postfix; @@ -100,6 +154,30 @@ int APS5_VABI getc_unlocked_nid_postfix(FileStream* stream) { } int APS5_VABI __srget_nid_postfix(FileStream* stream) { return fgetc_nid_postfix(stream); } int APS5_VABI getchar_nid_postfix() { return fgetc_nid_postfix(__stdinp_nid_postfix); } +char* APS5_VABI gets_nid_postfix(char* buffer) { + if (buffer == nullptr) throw std::invalid_argument("gets: null buffer"); + auto* stream = __stdinp_nid_postfix; + if (stream && !stream->IsOpen()) { errno = 9; return nullptr; } + flockfile_nid_postfix(stream); + struct Unlock { + FileStream* stream; + ~Unlock() { funlockfile_nid_postfix(stream); } + } unlock{stream}; + char* cursor = buffer; + for (;;) { + const int value = fgetc_nid_postfix(stream); + if (value == EOF) { + if (cursor == buffer) return nullptr; + *cursor = 0; + return buffer; + } + if (value == '\n') { + *cursor = 0; + return buffer; + } + *cursor++ = static_cast(value); + } +} std::int32_t APS5_VABI getwc_nid_postfix(FileStream* stream) { return fgetwc_nid_postfix(stream); } std::int32_t APS5_VABI getwchar_nid_postfix() { return fgetwc_nid_postfix(__stdinp_nid_postfix); } int APS5_VABI getchar_unlocked_nid_postfix() { return getc_unlocked_nid_postfix(__stdinp_nid_postfix); } @@ -194,7 +272,12 @@ int APS5_VABI setvbuf_nid_postfix(FileStream* stream, char* buffer, int mode, st buffer = nullptr; size = BUFSIZ; } - return std::setvbuf(GetStreamHandle(stream), buffer, native, size); + auto* handle = GetStreamHandle(stream); +#ifdef _WIN32 + if (handle == __acrt_iob_func(1) && KeepHostStandardOutput_nid_no_patch() < 0) return -1; + if (handle == __acrt_iob_func(2) && KeepHostStandardError_nid_no_patch() < 0) return -1; +#endif + return std::setvbuf(handle, buffer, native, size); } void APS5_VABI setbuf_nid_postfix(FileStream* stream, char* buffer) { setvbuf_nid_postfix(stream, buffer, buffer ? 0 : 2, buffer ? 1024 : 0); diff --git a/core/libs/prx/libc/src/StdioExtras.cpp b/core/libs/prx/libc/src/StdioExtras.cpp index fbbdd1a972..5cc08407b7 100644 --- a/core/libs/prx/libc/src/StdioExtras.cpp +++ b/core/libs/prx/libc/src/StdioExtras.cpp @@ -130,6 +130,10 @@ void APS5_VABI _ZdlPv_nid_postfix(void* pointer) { if (pointer != nullptr) ApplicationHeapFree_nid_no_patch(pointer); } +void APS5_VABI _ZdlPvRKSt9nothrow_t_nid_postfix(void* pointer, const void*) noexcept { + _ZdlPv_nid_postfix(pointer); +} + void APS5_VABI _ZdaPv_nid_postfix(void* pointer) { if (pointer != nullptr) ApplicationHeapFree_nid_no_patch(pointer); } diff --git a/core/libs/prx/libc/src/Strings.cpp b/core/libs/prx/libc/src/Strings.cpp index 5af4b2af6f..721ede0c0a 100644 --- a/core/libs/prx/libc/src/Strings.cpp +++ b/core/libs/prx/libc/src/Strings.cpp @@ -456,7 +456,7 @@ int APS5_VABI strcasecmp_nid_postfix(const char* s1, const char* s2) { for (;; ++s1, ++s2) { const int a = tolower_nid_postfix(static_cast(*s1)); const int b = tolower_nid_postfix(static_cast(*s2)); - if (a != b || *s1 == 0) return a - b; + if (a != b || *s1 == 0) return (a > b) - (a < b); } } @@ -464,7 +464,7 @@ int APS5_VABI strncasecmp_nid_postfix(const char* s1, const char* s2, size_t n) for (; n != 0; ++s1, ++s2, --n) { const int a = tolower_nid_postfix(static_cast(*s1)); const int b = tolower_nid_postfix(static_cast(*s2)); - if (a != b) return a - b; + if (a != b) return a < b ? -1 : 1; if (*s1 == 0) break; } return 0; diff --git a/core/libs/prx/libc/src/Threads.cpp b/core/libs/prx/libc/src/Threads.cpp index 1e7ea35688..ae9bd58fa7 100644 --- a/core/libs/prx/libc/src/Threads.cpp +++ b/core/libs/prx/libc/src/Threads.cpp @@ -350,6 +350,10 @@ int SignalCondition_nid_no_patch(void** handle) { return threadSuccess; } +unsigned APS5_VABI _ZNSt6thread20hardware_concurrencyEv_nid_postfix() { + return std::thread::hardware_concurrency(); +} + int APS5_VABI _Thrd_sleep_nid_postfix(const Xtime* target, Xtime*) { if (!target) APS5_INVALID_ARG_EX; @@ -550,5 +554,123 @@ int APS5_VABI _Mtx_current_owns_nid_postfix(void**) { return 1; } +static std::atomic registeredPthreadSyncForwards{nullptr}; + +void RegisterPthreadSyncForwards_nid_no_patch(const PthreadSyncForwards_nid_no_patch* forwards) { + registeredPthreadSyncForwards.store(forwards, std::memory_order_release); +} + +static const PthreadSyncForwards_nid_no_patch& PthreadSync(const char* function) { + const auto* forwards = registeredPthreadSyncForwards.load(std::memory_order_acquire); + if (!forwards) throw std::runtime_error(std::string(function) + ": libkernel synchronization is not registered"); + return *forwards; +} + +constexpr int KernelDeadlock = static_cast(0x8002000b); +constexpr int KernelNoMemory = static_cast(0x8002000c); +constexpr int KernelBusy = static_cast(0x80020010); +constexpr int RecursiveMutexType = 0x100; +constexpr int KernelRecursiveMutex = 2; + +static int CreateResult(int result) { + if (result == 0) return threadSuccess; + return result == KernelNoMemory ? threadNomem : threadError; +} + +static const char* SyncObjectName(const char* name) { + return name != nullptr && name[0] != '\0' ? name : nullptr; +} + +int APS5_VABI _ZNSt8__sce_v219_Mtx_init_with_nameEPP13pthread_mutexiPKc_nid_postfix(void** mutex, int type, const char* name) { + const auto& sync = PthreadSync(__func__); + *mutex = nullptr; + void* attr = nullptr; + const int created = sync.mutexattrInit(&attr); + if (created != 0) return CreateResult(created); + if ((type & RecursiveMutexType) != 0 && sync.mutexattrSettype(&attr, KernelRecursiveMutex) != 0) { + sync.mutexattrDestroy(&attr); + return threadError; + } + const int initialized = sync.mutexInit(mutex, &attr, SyncObjectName(name)); + const int released = sync.mutexattrDestroy(&attr); + if (initialized != 0) return CreateResult(initialized); + if (released != 0) { + sync.mutexDestroy(mutex); + return threadError; + } + return threadSuccess; +} + +int APS5_VABI _ZNSt8__sce_v29_Mtx_initEPP13pthread_mutexi_nid_postfix(void** mutex, int type) { + return _ZNSt8__sce_v219_Mtx_init_with_nameEPP13pthread_mutexiPKc_nid_postfix(mutex, type, "SceLibcI_Mtx"); +} + +int APS5_VABI _ZNSt8__sce_v212_Mtx_destroyEPP13pthread_mutex_nid_postfix(void** mutex) { + return PthreadSync(__func__).mutexDestroy(mutex); +} + +int APS5_VABI _ZNSt8__sce_v29_Mtx_lockEPP13pthread_mutex_nid_postfix(void** mutex) { + const int result = PthreadSync(__func__).mutexLock(mutex); + if (result == 0) return threadSuccess; + return result == KernelDeadlock ? threadBusy : threadError; +} + +int APS5_VABI _ZNSt8__sce_v212_Mtx_trylockEPP13pthread_mutex_nid_postfix(void** mutex) { + const int result = PthreadSync(__func__).mutexTrylock(mutex); + if (result == 0) return threadSuccess; + return result == KernelBusy ? threadBusy : threadError; +} + +int APS5_VABI _ZNSt8__sce_v211_Mtx_unlockEPP13pthread_mutex_nid_postfix(void** mutex) { + PthreadSync(__func__).mutexUnlock(mutex); + return threadSuccess; +} + +int APS5_VABI _ZNSt8__sce_v219_Cnd_init_with_nameEPP12pthread_condPKc_nid_postfix(void** condition, const char* name) { + return CreateResult(PthreadSync(__func__).condInit(condition, nullptr, SyncObjectName(name))); +} + +int APS5_VABI _ZNSt8__sce_v29_Cnd_initEPP12pthread_cond_nid_postfix(void** condition) { + return CreateResult(PthreadSync(__func__).condInit(condition, nullptr, "SceLibcI_Cnd")); +} + +int APS5_VABI _ZNSt8__sce_v212_Cnd_destroyEPP12pthread_cond_nid_postfix(void** condition) { + return PthreadSync(__func__).condDestroy(condition); +} + +int APS5_VABI _ZNSt8__sce_v29_Cnd_waitEPP12pthread_condPP13pthread_mutex_nid_postfix(void** condition, void** mutex) { + PthreadSync(__func__).condWait(condition, mutex); + return threadSuccess; +} + +int APS5_VABI _ZNSt8__sce_v211_Cnd_signalEPP12pthread_cond_nid_postfix(void** condition) { + PthreadSync(__func__).condSignal(condition); + return threadSuccess; +} + +int APS5_VABI _ZNSt8__sce_v214_Cnd_broadcastEPP12pthread_cond_nid_postfix(void** condition) { + PthreadSync(__func__).condBroadcast(condition); + return threadSuccess; +} + +int APS5_VABI _ZNSt8__sce_v212_Thrd_detachEP7pthread_nid_postfix(void* thread) { + return PthreadSync(__func__).detach(thread) == 0 ? threadSuccess : threadError; +} + +int APS5_VABI _ZNSt8__sce_v210_Thrd_joinEP7pthreadPi_nid_postfix(void* thread, int* code) { + void* result = nullptr; + if (PthreadSync(__func__).join(thread, &result) != 0) return threadError; + if (code) *code = static_cast(reinterpret_cast(result)); + return threadSuccess; +} + +void APS5_VABI _ZNSt8__sce_v211_Thrd_yieldEv_nid_postfix() { + PthreadSync(__func__).yield(); +} + +void* APS5_VABI _ZNSt8__sce_v28_Thrd_idEv_nid_postfix() { + return _Thrd_current_nid_postfix(); +} + } diff --git a/core/libs/prx/libc/src/exception/StandardExceptions.cpp b/core/libs/prx/libc/src/exception/StandardExceptions.cpp index bf6b539f82..003a1af171 100644 --- a/core/libs/prx/libc/src/exception/StandardExceptions.cpp +++ b/core/libs/prx/libc/src/exception/StandardExceptions.cpp @@ -268,6 +268,27 @@ const void* const SystemCategoryVtable[] { reinterpret_cast(CategoryEquivalentCode_nid_no_patch) }; const CategoryObject SystemCategory {SystemCategoryVtable + 2}; +[[noreturn]] void ThrowCppError(int code, const ExceptionVtable& table) { + static constexpr CppError errors[] { + {16, "device or resource busy"}, + {22, "invalid argument"}, + {3, "no such process"}, + {12, "not enough memory"}, + {1, "operation not permitted"}, + {11, "resource deadlock would occur"}, + {35, "resource unavailable try again"} + }; + if (code < 0 || code >= static_cast(std::size(errors))) + throw std::out_of_range("_Throw_Cpp_error: " + std::to_string(code) + " is not a _Cpp_error code"); + const auto& error = errors[code]; + char message[128]; + std::snprintf(message, sizeof(message), "%s: %s", error.message, GenericErrorMessage(error.value)); + auto* object = static_cast(__cxa_allocate_exception_nid_postfix(sizeof(SystemErrorObject))); + Construct(&object->base, table, message); + object->code = {0, error.value, &GenericCategory}; + __cxa_throw_nid_postfix(object, reinterpret_cast(const_cast(table.type)), + reinterpret_cast(DestroyGuestMessage_nid_no_patch)); +} } extern "C" { @@ -311,6 +332,10 @@ void APS5_VABI _ZNSt8bad_castD0Ev_nid_postfix(LibcException::ExceptionObject* se const char* APS5_VABI _ZNKSt8bad_cast4whatEv_nid_postfix(const LibcException::ExceptionObject* self) { return LibcException::PlainWhat(self); } void APS5_VABI _ZNSt8bad_castC1Ev_nid_postfix(LibcException::ExceptionObject* self) { self->vtable = &_ZTVSt8bad_cast_nid_postfix.destroy; } void APS5_VABI _ZNSt8bad_castC2Ev_nid_postfix(LibcException::ExceptionObject* self) { self->vtable = &_ZTVSt8bad_cast_nid_postfix.destroy; } +[[noreturn]] void APS5_VABI _ZNKSt8bad_cast8_DoraiseEv_nid_postfix(const LibcException::ExceptionObject* self) { + if (!self) throw std::invalid_argument("exception object is null"); + LibcException::ThrowPlain(_ZTVSt8bad_cast_nid_postfix); +} LibcException::TypeRecord _ZTISt10bad_typeid_nid_postfix {LibcException::SingleTypeVtable + 2, "St10bad_typeid", &_ZTISt9exception_nid_postfix}; LibcException::ExceptionVtable _ZTVSt10bad_typeid_nid_postfix {0, &_ZTISt10bad_typeid_nid_postfix, LibcException::GuestDestroyPlain_nid_no_patch, LibcException::GuestDeletePlain_nid_no_patch, LibcException::GuestPlainWhat_nid_no_patch}; @@ -500,7 +525,6 @@ const char* APS5_VABI _ZNKSt8ios_base7failure4whatEv_nid_postfix(const LibcExcep [[noreturn]] void APS5_VABI _ZSt15_Xruntime_errorPKc_nid_postfix(const char* message) { LibcException::ThrowMessage(_ZTVSt13runtime_error_nid_postfix, message); } [[noreturn]] void APS5_VABI _ZSt13_Xrange_errorPKc_nid_postfix(const char* message) { LibcException::ThrowMessage(_ZTVSt11range_error_nid_postfix, message); } [[noreturn]] void APS5_VABI _ZSt19_Xbad_function_callv_nid_postfix() { LibcException::ThrowPlain(_ZTVSt17bad_function_call_nid_postfix); } -[[noreturn]] void APS5_VABI _ZNSt8__sce_v219_Xbad_function_callEv_nid_postfix() { LibcException::ThrowPlain(_ZTVSt17bad_function_call_nid_postfix); } [[noreturn]] void APS5_VABI _ZSt19_Throw_bad_weak_ptrv_nid_postfix() { LibcException::ThrowPlain(_ZTVSt12bad_weak_ptr_nid_postfix); } [[noreturn]] void APS5_VABI _ZSt26__throw_bad_variant_accessv_nid_postfix() { LibcException::ThrowPlain(_ZTVSt18bad_variant_access_nid_postfix); } [[noreturn]] void APS5_VABI _ZSt26_Throw_bad_optional_accessv_nid_postfix() { LibcException::ThrowPlain(_ZTVSt19bad_optional_access_nid_postfix); } @@ -513,24 +537,101 @@ const char* APS5_VABI _ZNKSt8ios_base7failure4whatEv_nid_postfix(const LibcExcep reinterpret_cast(LibcException::DestroyGuestMessage_nid_no_patch)); } [[noreturn]] void ThrowCppError_nid_no_patch(int code) { - static constexpr LibcException::CppError errors[] { - {16, "device or resource busy"}, - {22, "invalid argument"}, - {3, "no such process"}, - {12, "not enough memory"}, - {1, "operation not permitted"}, - {11, "resource deadlock would occur"}, - {35, "resource unavailable try again"} - }; - if (code < 0 || code >= static_cast(std::size(errors))) - throw std::out_of_range("_Throw_Cpp_error: " + std::to_string(code) + " is not a _Cpp_error code"); - const auto& error = errors[code]; - char message[128]; - std::snprintf(message, sizeof(message), "%s: %s", error.message, LibcException::GenericErrorMessage(error.value)); - auto* object = static_cast(__cxa_allocate_exception_nid_postfix(sizeof(LibcException::SystemErrorObject))); - LibcException::Construct(&object->base, _ZTVSt12system_error_nid_postfix, message); - object->code = {0, error.value, &LibcException::GenericCategory}; - __cxa_throw_nid_postfix(object, reinterpret_cast(&_ZTISt12system_error_nid_postfix), - reinterpret_cast(LibcException::DestroyGuestMessage_nid_no_patch)); + LibcException::ThrowCppError(code, _ZTVSt12system_error_nid_postfix); +} +} + +namespace LibcException::SceV2 { +static const char* APS5_VABI BadOptionalAccessWhat_nid_no_patch(const ExceptionObject*) { return "Bad optional access"; } +static const char* APS5_VABI BadWeakPtrWhat_nid_no_patch(const ExceptionObject*) { return "bad_weak_ptr"; } +static const char* APS5_VABI BadVariantAccessWhat_nid_no_patch(const ExceptionObject*) { return "bad variant access"; } +static const char* APS5_VABI BadFunctionCallWhat_nid_no_patch(const ExceptionObject*) { return "bad function call"; } +} + +extern "C" { +LibcException::TypeRecord _ZTINSt13__sce_cxxvers16_Error_exceptionE_nid_postfix {LibcException::SingleTypeVtable + 2, "NSt13__sce_cxxvers16_Error_exceptionE", &_ZTISt9exception_nid_postfix}; +} + +namespace LibcException::SceV2 { +constexpr ExceptionVtable MessageTable(const TypeRecord& type) { + return {0, &type, GuestDestroyMessage_nid_no_patch, GuestDeleteMessage_nid_no_patch, GuestMessageWhat_nid_no_patch}; +} +constexpr ExceptionVtable PlainTable(const TypeRecord& type, const char* (APS5_VABI *what)(const ExceptionObject*)) { + return {0, &type, GuestDestroyPlain_nid_no_patch, GuestDeletePlain_nid_no_patch, what}; +} +const ExceptionVtable ErrorException = MessageTable(_ZTINSt13__sce_cxxvers16_Error_exceptionE_nid_postfix); +const TypeRecord LogicErrorType {SingleTypeVtable + 2, "NSt8__sce_v211logic_errorE", &_ZTINSt13__sce_cxxvers16_Error_exceptionE_nid_postfix}; +const TypeRecord LengthErrorType {SingleTypeVtable + 2, "NSt8__sce_v212length_errorE", &LogicErrorType}; +const TypeRecord OutOfRangeType {SingleTypeVtable + 2, "NSt8__sce_v212out_of_rangeE", &LogicErrorType}; +const TypeRecord InvalidArgumentType {SingleTypeVtable + 2, "NSt8__sce_v216invalid_argumentE", &LogicErrorType}; +const TypeRecord RuntimeErrorType {SingleTypeVtable + 2, "NSt8__sce_v213runtime_errorE", &_ZTINSt13__sce_cxxvers16_Error_exceptionE_nid_postfix}; +const TypeRecord OverflowErrorType {SingleTypeVtable + 2, "NSt8__sce_v214overflow_errorE", &RuntimeErrorType}; +const TypeRecord RangeErrorType {SingleTypeVtable + 2, "NSt8__sce_v211range_errorE", &RuntimeErrorType}; +const TypeRecord SystemErrorBaseType {SingleTypeVtable + 2, "NSt8__sce_v213_System_errorE", &RuntimeErrorType}; +const TypeRecord SystemErrorType {SingleTypeVtable + 2, "NSt8__sce_v212system_errorE", &SystemErrorBaseType}; +const TypeRecord IosFailureType {SingleTypeVtable + 2, "NSt8__sce_v28ios_base7failureE", &SystemErrorType}; +const TypeRecord BadOptionalAccessType {SingleTypeVtable + 2, "NSt8__sce_v219bad_optional_accessE", &_ZTISt9exception_nid_postfix}; +const TypeRecord BadWeakPtrType {SingleTypeVtable + 2, "NSt8__sce_v212bad_weak_ptrE", &_ZTISt9exception_nid_postfix}; +const TypeRecord BadVariantAccessType {SingleTypeVtable + 2, "NSt8__sce_v218bad_variant_accessE", &_ZTISt9exception_nid_postfix}; +const TypeRecord BadFunctionCallType {SingleTypeVtable + 2, "NSt8__sce_v217bad_function_callE", &_ZTISt9exception_nid_postfix}; +const ExceptionVtable LengthError = MessageTable(LengthErrorType); +const ExceptionVtable OutOfRange = MessageTable(OutOfRangeType); +const ExceptionVtable InvalidArgument = MessageTable(InvalidArgumentType); +const ExceptionVtable RuntimeError = MessageTable(RuntimeErrorType); +const ExceptionVtable OverflowError = MessageTable(OverflowErrorType); +const ExceptionVtable RangeError = MessageTable(RangeErrorType); +const ExceptionVtable SystemError = MessageTable(SystemErrorType); +const ExceptionVtable IosFailure = MessageTable(IosFailureType); +const ExceptionVtable BadOptionalAccess = PlainTable(BadOptionalAccessType, BadOptionalAccessWhat_nid_no_patch); +const ExceptionVtable BadWeakPtr = PlainTable(BadWeakPtrType, BadWeakPtrWhat_nid_no_patch); +const ExceptionVtable BadVariantAccess = PlainTable(BadVariantAccessType, BadVariantAccessWhat_nid_no_patch); +const ExceptionVtable BadFunctionCall = PlainTable(BadFunctionCallType, BadFunctionCallWhat_nid_no_patch); + +[[noreturn]] void ThrowCopy(const ExceptionObject* source, const ExceptionVtable& table) { + auto* object = static_cast(__cxa_allocate_exception_nid_postfix(sizeof(ExceptionObject))); + Copy(object, source, table); + __cxa_throw_nid_postfix(object, reinterpret_cast(const_cast(table.type)), + reinterpret_cast(DestroyGuestMessage_nid_no_patch)); +} +[[noreturn]] void ThrowSystemCopy(const SystemErrorObject* source, const ExceptionVtable& table) { + auto* object = static_cast(__cxa_allocate_exception_nid_postfix(sizeof(SystemErrorObject))); + Copy(&object->base, &source->base, table); + object->code = source->code; + __cxa_throw_nid_postfix(object, reinterpret_cast(const_cast(table.type)), + reinterpret_cast(DestroyGuestMessage_nid_no_patch)); +} +} + +extern "C" { +void APS5_VABI _ZNSt13__sce_cxxvers16_Error_exceptionC1EPKc_nid_postfix(LibcException::ExceptionObject* self, const char* message) { LibcException::Construct(self, LibcException::SceV2::ErrorException, message); } +void APS5_VABI _ZNSt13__sce_cxxvers16_Error_exceptionC2EPKc_nid_postfix(LibcException::ExceptionObject* self, const char* message) { LibcException::Construct(self, LibcException::SceV2::ErrorException, message); } +void APS5_VABI _ZNSt13__sce_cxxvers16_Error_exceptionC1ERKS0__nid_postfix(LibcException::ExceptionObject* self, const LibcException::ExceptionObject* source) { LibcException::Copy(self, source, LibcException::SceV2::ErrorException); } +void APS5_VABI _ZNSt13__sce_cxxvers16_Error_exceptionC2ERKS0__nid_postfix(LibcException::ExceptionObject* self, const LibcException::ExceptionObject* source) { LibcException::Copy(self, source, LibcException::SceV2::ErrorException); } +void APS5_VABI _ZNSt13__sce_cxxvers16_Error_exceptionD1Ev_nid_postfix(LibcException::ExceptionObject* self) { LibcException::DestroyMessage(self); } +void APS5_VABI _ZNSt13__sce_cxxvers16_Error_exceptionD2Ev_nid_postfix(LibcException::ExceptionObject* self) { LibcException::DestroyMessage(self); } +void APS5_VABI _ZNSt13__sce_cxxvers16_Error_exceptionD0Ev_nid_postfix(LibcException::ExceptionObject* self) { LibcException::DeleteMessage(self); } +const char* APS5_VABI _ZNKSt13__sce_cxxvers16_Error_exception4whatEv_nid_postfix(const LibcException::ExceptionObject* self) { return LibcException::MessageWhat(self); } + +[[noreturn]] void APS5_VABI _ZNSt8__sce_v214_Xlength_errorEPKc_nid_postfix(const char* message) { LibcException::ThrowMessage(LibcException::SceV2::LengthError, message); } +[[noreturn]] void APS5_VABI _ZNSt8__sce_v214_Xout_of_rangeEPKc_nid_postfix(const char* message) { LibcException::ThrowMessage(LibcException::SceV2::OutOfRange, message); } +[[noreturn]] void APS5_VABI _ZNSt8__sce_v218_Xinvalid_argumentEPKc_nid_postfix(const char* message) { LibcException::ThrowMessage(LibcException::SceV2::InvalidArgument, message); } +[[noreturn]] void APS5_VABI _ZNSt8__sce_v215_Xruntime_errorEPKc_nid_postfix(const char* message) { LibcException::ThrowMessage(LibcException::SceV2::RuntimeError, message); } +[[noreturn]] void APS5_VABI _ZNSt8__sce_v216_Xoverflow_errorEPKc_nid_postfix(const char* message) { LibcException::ThrowMessage(LibcException::SceV2::OverflowError, message); } +[[noreturn]] void APS5_VABI _ZNSt8__sce_v211_Xbad_allocEv_nid_postfix() { LibcException::ThrowPlain(_ZTVSt9bad_alloc_nid_postfix); } +[[noreturn]] void APS5_VABI _ZNSt8__sce_v219_Xbad_function_callEv_nid_postfix() { LibcException::ThrowPlain(LibcException::SceV2::BadFunctionCall); } +[[noreturn]] void APS5_VABI _ZNSt8__sce_v26_ThrowERKNS_19bad_optional_accessE_nid_postfix(const LibcException::ExceptionObject*) { LibcException::ThrowPlain(LibcException::SceV2::BadOptionalAccess); } +[[noreturn]] void APS5_VABI _ZNSt8__sce_v26_ThrowERKNS_12bad_weak_ptrE_nid_postfix(const LibcException::ExceptionObject*) { LibcException::ThrowPlain(LibcException::SceV2::BadWeakPtr); } +[[noreturn]] void APS5_VABI _ZNSt8__sce_v26_ThrowERKNS_18bad_variant_accessE_nid_postfix(const LibcException::ExceptionObject*) { LibcException::ThrowPlain(LibcException::SceV2::BadVariantAccess); } +[[noreturn]] void APS5_VABI _ZNSt8__sce_v26_ThrowERKNS_11range_errorE_nid_postfix(const LibcException::ExceptionObject* error) { LibcException::SceV2::ThrowCopy(error, LibcException::SceV2::RangeError); } +[[noreturn]] void APS5_VABI _ZNSt8__sce_v26_ThrowERKNS_12system_errorE_nid_postfix(const LibcException::SystemErrorObject* error) { LibcException::SceV2::ThrowSystemCopy(error, LibcException::SceV2::SystemError); } +[[noreturn]] void APS5_VABI _ZNSt8__sce_v28ios_base6_ThrowERKNS0_7failureE_nid_postfix(const LibcException::SystemErrorObject* failure) { LibcException::SceV2::ThrowSystemCopy(failure, LibcException::SceV2::IosFailure); } +[[noreturn]] void APS5_VABI _ZNSt8__sce_v216_Throw_Cpp_errorEi_nid_postfix(int code) { LibcException::ThrowCppError(code, LibcException::SceV2::SystemError); } + +std::int64_t APS5_VABI _Xtime_get_ticks_nid_postfix(); +std::int64_t APS5_VABI _ZNSt8__sce_v216_Xtime_get_ticksEv_nid_postfix() { return _Xtime_get_ticks_nid_postfix(); } +const char* APS5_VABI _ZNSt8__sce_v213_Syserror_mapEi_nid_postfix(int value) { + if (value == 0) return "success"; + const char* message = LibcException::GenericErrorMessage(value); + return message ? message : "unknown error"; } } diff --git a/core/libs/prx/libkernel/Aio/src/Aio.cpp b/core/libs/prx/libkernel/Aio/src/Aio.cpp index 9926ac1dd6..1e71f9cd7e 100644 --- a/core/libs/prx/libkernel/Aio/src/Aio.cpp +++ b/core/libs/prx/libkernel/Aio/src/Aio.cpp @@ -5,7 +5,6 @@ #include #include #include -#include #include "SceTypes.hpp" #include "prx/libc/include/General.hpp" #include "prx/libc/include/GuestArena.hpp" @@ -25,6 +24,7 @@ namespace { constexpr std::int32_t AioProcessing = 2; constexpr std::int32_t AioCompleted = 3; constexpr std::int32_t AioAborted = 4; +constexpr std::int32_t AioReported = 0x10000; constexpr std::int32_t AioMaxQueues = 512; constexpr std::int32_t AioRequestNumMax = 128; constexpr std::int32_t AioIdNumMax = 128; @@ -34,6 +34,11 @@ constexpr std::int32_t AioSchedulingWindowMax = 128; constexpr std::int32_t AioDelayedCountMax = 128; constexpr std::uint32_t AioSplitSizeMax = 0x1000000; constexpr std::uint32_t AioSplitChunkSizeMax = 0x1000000; +constexpr std::int32_t AioPriorityLow = 1; +constexpr std::int32_t AioPriorityHigh = 3; +constexpr std::int32_t AioDefaultSchedulingWindow = 32; +constexpr std::int32_t AioDefaultDelayedCountLimit = 32; +constexpr std::uint32_t AioDefaultSplitSize = 0x100000; std::mutex g_mutex; std::array g_states{}; @@ -57,6 +62,30 @@ void SetState(std::int32_t id, std::int32_t state) { g_states[id] = state; } +std::int32_t StateLocked(std::int32_t id) { + return ValidId(id) && g_states[id] != 0 ? g_states[id] : SCE_KERNEL_ERROR_ESRCH; +} + +std::int32_t ReportLocked(std::int32_t id) { + const std::int32_t state = StateLocked(id); + if (state != SCE_KERNEL_ERROR_ESRCH && state != AioProcessing) g_states[id] |= AioReported; + return state; +} + +std::int32_t CancelLocked(std::int32_t id) { + const std::int32_t state = StateLocked(id); + if (state == SCE_KERNEL_ERROR_ESRCH) return state; + if (state != AioProcessing) return state & ~AioReported; + g_states[id] = AioAborted; + return AioAborted; +} + +std::int32_t DeleteLocked(std::int32_t id) { + if (StateLocked(id) == SCE_KERNEL_ERROR_ESRCH) return SCE_KERNEL_ERROR_ESRCH; + g_states[id] = 0; + return 0; +} + std::int64_t NativePread(std::int32_t fd, void* buf, std::size_t nbyte, std::int64_t offset) { const GuestArena::HostWrite destination(buf, nbyte); if (!destination.Open()) { @@ -93,7 +122,7 @@ std::int64_t NativePwrite(std::int32_t fd, const void* buf, std::size_t nbyte, s #endif } -bool RunRequest(KernelAioRwRequest& req, bool write) { +void RunRequest(KernelAioRwRequest& req, bool write) { const std::int64_t done = write ? NativePwrite(req.fd, req.buf, req.nbyte, req.offset) : NativePread(req.fd, req.buf, req.nbyte, req.offset); @@ -103,55 +132,47 @@ bool RunRequest(KernelAioRwRequest& req, bool write) { if (error == EBADF) req.result->return_value = static_cast(SCE_KERNEL_ERROR_EBADF); if (error == EFAULT) req.result->return_value = static_cast(SCE_KERNEL_ERROR_EFAULT); if (error == EINVAL) req.result->return_value = static_cast(SCE_KERNEL_ERROR_EINVAL); - req.result->state = AioAborted; - return false; + req.result->state = AioCompleted; + return; } req.result->return_value = done; req.result->state = AioCompleted; - return true; } -int ValidateRequests(KernelAioRwRequest* req, std::int32_t size, const void* id) { +int ValidateRequests(KernelAioRwRequest* req, std::int32_t size, std::int32_t prio, const void* id) { if (req == nullptr || id == nullptr) return SCE_KERNEL_ERROR_EFAULT; - if (size <= 0) return SCE_KERNEL_ERROR_EINVAL; + if (size <= 0 || size > AioRequestNumMax || prio < AioPriorityLow || prio > AioPriorityHigh) return SCE_KERNEL_ERROR_EINVAL; for (std::int32_t i = 0; i < size; ++i) { if (req[i].result == nullptr) return SCE_KERNEL_ERROR_EFAULT; } return 0; } -int SubmitCommands(KernelAioRwRequest* req, std::int32_t size, bool write, std::int32_t* id) { - const int error = ValidateRequests(req, size, id); +int SubmitCommands(KernelAioRwRequest* req, std::int32_t size, std::int32_t prio, bool write, std::int32_t* id) { + const int error = ValidateRequests(req, size, prio, id); if (error != 0) return error; const std::int32_t queue = AllocateId(); - bool aborted = false; - for (std::int32_t i = 0; i < size; ++i) { - if (!RunRequest(req[i], write)) aborted = true; - } - SetState(queue, aborted ? AioAborted : AioCompleted); + for (std::int32_t i = 0; i < size; ++i) RunRequest(req[i], write); + SetState(queue, AioCompleted); *id = queue; return 0; } -int SubmitCommandsMultiple(KernelAioRwRequest* req, std::int32_t size, bool write, std::int32_t* id, const char* name) { - if (req != nullptr && id != nullptr && size > AioRequestNumMax) throw std::runtime_error(std::string(name) + ": more than 128 requests per batch is not modelled"); - const int error = ValidateRequests(req, size, id); +int SubmitCommandsMultiple(KernelAioRwRequest* req, std::int32_t size, std::int32_t prio, bool write, std::int32_t* id) { + const int error = ValidateRequests(req, size, prio, id); if (error != 0) return error; for (std::int32_t i = 0; i < size; ++i) { const std::int32_t queue = AllocateId(); - SetState(queue, RunRequest(req[i], write) ? AioCompleted : AioAborted); + RunRequest(req[i], write); + SetState(queue, AioCompleted); id[i] = queue; } return 0; } -int ValidateIds(const std::int32_t* id, std::int32_t num, const void* out, const char* name) { +int ValidateIds(const std::int32_t* id, std::int32_t num, const void* out) { if (id == nullptr || out == nullptr) return SCE_KERNEL_ERROR_EFAULT; - if (num < 0) return SCE_KERNEL_ERROR_EINVAL; - if (num > AioIdNumMax) throw std::runtime_error(std::string(name) + ": more than 128 ids per batch is not modelled"); - for (std::int32_t i = 0; i < num; ++i) { - if (!ValidId(id[i])) return SCE_KERNEL_ERROR_EINVAL; - } + if (num <= 0 || num > AioIdNumMax) return SCE_KERNEL_ERROR_EINVAL; return 0; } @@ -161,49 +182,31 @@ extern "C" { int APS5_VABI sceKernelAioDeleteRequest(int32_t id, int32_t* ret) { if (ret == nullptr) return SCE_KERNEL_ERROR_EFAULT; - if (!ValidId(id)) return SCE_KERNEL_ERROR_EINVAL; - SetState(id, AioAborted); - *ret = 0; + std::lock_guard lock(g_mutex); + *ret = DeleteLocked(id); return 0; } int APS5_VABI sceKernelAioDeleteRequests(int32_t* id, int32_t num, int32_t* ret) { - const int error = ValidateIds(id, num, ret, "sceKernelAioDeleteRequests"); + const int error = ValidateIds(id, num, ret); if (error != 0) return error; - for (int32_t i = 0; i < num; ++i) { - SetState(id[i], AioAborted); - ret[i] = 0; - } + std::lock_guard lock(g_mutex); + for (int32_t i = 0; i < num; ++i) ret[i] = DeleteLocked(id[i]); return 0; } int APS5_VABI sceKernelAioCancelRequest(int32_t id, int32_t* state) { if (state == nullptr) return SCE_KERNEL_ERROR_EFAULT; - if (id == 0) { - *state = AioProcessing; - return 0; - } - if (!ValidId(id)) return SCE_KERNEL_ERROR_EINVAL; - SetState(id, AioAborted); - *state = AioAborted; + std::lock_guard lock(g_mutex); + *state = CancelLocked(id); return 0; } int APS5_VABI sceKernelAioCancelRequests(int32_t* id, int32_t num, int32_t* state) { - if (id == nullptr || state == nullptr) return SCE_KERNEL_ERROR_EFAULT; - if (num < 0) return SCE_KERNEL_ERROR_EINVAL; - if (num > AioIdNumMax) throw std::runtime_error("sceKernelAioCancelRequests: more than 128 ids per batch is not modelled"); - for (int32_t i = 0; i < num; ++i) { - if (id[i] != 0 && !ValidId(id[i])) return SCE_KERNEL_ERROR_EINVAL; - } - for (int32_t i = 0; i < num; ++i) { - if (id[i] == 0) { - state[i] = AioProcessing; - continue; - } - SetState(id[i], AioAborted); - state[i] = AioAborted; - } + const int error = ValidateIds(id, num, state); + if (error != 0) return error; + std::lock_guard lock(g_mutex); + for (int32_t i = 0; i < num; ++i) state[i] = CancelLocked(id[i]); return 0; } @@ -215,14 +218,18 @@ int APS5_VABI sceKernelAioInitializeImpl(void* param, int32_t size) { void APS5_VABI sceKernelAioInitializeParam(void* param) { if (param == nullptr) throw std::invalid_argument("sceKernelAioInitializeParam: param is null"); + auto* scheduling = static_cast(param); + for (int index = 0; index < 3; ++index) { + const bool split = index < 2; + scheduling[index] = {AioDefaultSchedulingWindow, AioDefaultDelayedCountLimit, split ? 1u : 0u, split ? AioDefaultSplitSize : 0u, split ? AioDefaultSplitSize : 0u}; + } } int APS5_VABI sceKernelAioSetParam(KernelAioSchedulingParam* param, int32_t schedulingWindowSize, int32_t delayedCountLimit, uint32_t enableSplit, uint32_t splitSize, uint32_t splitChunkSize) { if (param == nullptr) throw std::invalid_argument("sceKernelAioSetParam: param is null"); - if (schedulingWindowSize < 0 || schedulingWindowSize > AioSchedulingWindowMax) throw std::runtime_error("sceKernelAioSetParam: a scheduling window outside 0..128 is not modelled"); - if (delayedCountLimit < 0 || delayedCountLimit > AioDelayedCountMax) throw std::runtime_error("sceKernelAioSetParam: a delayed count limit outside 0..128 is not modelled"); - if (enableSplit > 1) throw std::runtime_error("sceKernelAioSetParam: a split flag other than 0 or 1 is not modelled"); - if (splitSize > AioSplitSizeMax || splitChunkSize > AioSplitChunkSizeMax) throw std::runtime_error("sceKernelAioSetParam: a split size above 0x1000000 is not modelled"); + if (schedulingWindowSize < 0 || schedulingWindowSize > AioSchedulingWindowMax || delayedCountLimit < 0 || delayedCountLimit > AioDelayedCountMax || enableSplit > 1) + return SCE_KERNEL_ERROR_EINVAL; + if (enableSplit == 1 && (splitSize == 0 || splitSize > AioSplitSizeMax || splitChunkSize == 0 || splitChunkSize > AioSplitChunkSizeMax)) return SCE_KERNEL_ERROR_EINVAL; param->schedulingWindowSize = schedulingWindowSize; param->delayedCountLimit = delayedCountLimit; param->enableSplit = enableSplit; @@ -232,37 +239,32 @@ int APS5_VABI sceKernelAioSetParam(KernelAioSchedulingParam* param, int32_t sche } int APS5_VABI sceKernelAioSubmitReadCommands(KernelAioRwRequest* req, int32_t size, int32_t prio, int32_t* id) { - (void)prio; - return SubmitCommands(req, size, false, id); + return SubmitCommands(req, size, prio, false, id); } int APS5_VABI sceKernelAioSubmitWriteCommands(KernelAioRwRequest* req, int32_t size, int32_t prio, int32_t* id) { - (void)prio; - return SubmitCommands(req, size, true, id); + return SubmitCommands(req, size, prio, true, id); } int APS5_VABI sceKernelAioSubmitReadCommandsMultiple(KernelAioRwRequest* req, int32_t size, int32_t prio, int32_t* id) { - (void)prio; - return SubmitCommandsMultiple(req, size, false, id, "sceKernelAioSubmitReadCommandsMultiple"); + return SubmitCommandsMultiple(req, size, prio, false, id); } int APS5_VABI sceKernelAioSubmitWriteCommandsMultiple(KernelAioRwRequest* req, int32_t size, int32_t prio, int32_t* id) { - (void)prio; - return SubmitCommandsMultiple(req, size, true, id, "sceKernelAioSubmitWriteCommandsMultiple"); + return SubmitCommandsMultiple(req, size, prio, true, id); } int APS5_VABI sceKernelAioPollRequest(int32_t id, int32_t* state) { if (state == nullptr) return SCE_KERNEL_ERROR_EFAULT; - if (!ValidId(id)) return SCE_KERNEL_ERROR_EINVAL; std::lock_guard lock(g_mutex); - *state = g_states[id]; + *state = ReportLocked(id); return 0; } int APS5_VABI sceKernelAioWaitRequests(int32_t* id, int32_t num, int32_t* state, uint32_t mode, uint32_t* usec) { - const int error = ValidateIds(id, num, state, "sceKernelAioWaitRequests"); + const int error = ValidateIds(id, num, state); if (error != 0) return error; - if (mode != AioWaitAnd && mode != AioWaitOr) throw std::runtime_error("sceKernelAioWaitRequests: unsupported wait mode"); + if (mode != AioWaitAnd && mode != AioWaitOr) return SCE_KERNEL_ERROR_EINVAL; const auto start = std::chrono::steady_clock::now(); for (;;) { bool allDone = true; @@ -270,15 +272,22 @@ int APS5_VABI sceKernelAioWaitRequests(int32_t* id, int32_t num, int32_t* state, { std::lock_guard lock(g_mutex); for (int32_t i = 0; i < num; ++i) { - state[i] = g_states[id[i]]; - if (state[i] == AioProcessing) allDone = false; - if (state[i] == AioCompleted) anyCompleted = true; + const std::int32_t current = StateLocked(id[i]); + if (current == AioProcessing) allDone = false; + if ((current & ~AioReported) == AioCompleted) anyCompleted = true; + } + if (allDone || (mode == AioWaitOr && anyCompleted)) { + for (int32_t i = 0; i < num; ++i) state[i] = ReportLocked(id[i]); + return 0; } } - if (allDone || (mode == AioWaitOr && anyCompleted)) return 0; if (usec != nullptr && *usec != 0) { const auto elapsed = std::chrono::duration_cast(std::chrono::steady_clock::now() - start).count(); - if (elapsed > static_cast(*usec)) return SCE_KERNEL_ERROR_ETIMEDOUT; + if (elapsed > static_cast(*usec)) { + std::lock_guard lock(g_mutex); + for (int32_t i = 0; i < num; ++i) state[i] = ReportLocked(id[i]); + return SCE_KERNEL_ERROR_ETIMEDOUT; + } } PreciseSleepUs(10); } @@ -286,13 +295,12 @@ int APS5_VABI sceKernelAioWaitRequests(int32_t* id, int32_t num, int32_t* state, int APS5_VABI sceKernelAioWaitRequest(int32_t id, int32_t* state, uint32_t* usec) { if (state == nullptr) return SCE_KERNEL_ERROR_EFAULT; - if (!ValidId(id)) return SCE_KERNEL_ERROR_EINVAL; const auto start = std::chrono::steady_clock::now(); for (;;) { std::int32_t current; { std::lock_guard lock(g_mutex); - current = g_states[id]; + current = ReportLocked(id); } if (current != AioProcessing) { *state = current; @@ -310,10 +318,10 @@ int APS5_VABI sceKernelAioWaitRequest(int32_t id, int32_t* state, uint32_t* usec } int APS5_VABI sceKernelAioPollRequests(int32_t* id, int32_t num, int32_t* state) { - const int error = ValidateIds(id, num, state, "sceKernelAioPollRequests"); + const int error = ValidateIds(id, num, state); if (error != 0) return error; std::lock_guard lock(g_mutex); - for (int32_t i = 0; i < num; ++i) state[i] = g_states[id[i]]; + for (int32_t i = 0; i < num; ++i) state[i] = ReportLocked(id[i]); return 0; } diff --git a/core/libs/prx/libkernel/CMakeLists.txt b/core/libs/prx/libkernel/CMakeLists.txt index 5692b19d0e..92cbd44e5e 100644 --- a/core/libs/prx/libkernel/CMakeLists.txt +++ b/core/libs/prx/libkernel/CMakeLists.txt @@ -188,6 +188,16 @@ if(BUILD_TESTING) set_tests_properties(guest_posix_sem_init guest_posix_sem_post PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$") endif() + add_test_executable(guest_posix_named_sem_tests ${LIBS_INCLUDE_DIR}/tests/GuestPosixNamedSem.cpp) + target_include_directories(guest_posix_named_sem_tests PRIVATE ${LIBS_INCLUDE_DIR}) + target_link_libraries(guest_posix_named_sem_tests PRIVATE libkernel libc) + configure_windows_unwind(guest_posix_named_sem_tests) + add_test(NAME guest_posix_named_sem COMMAND guest_posix_named_sem_tests) + set_tests_properties(guest_posix_named_sem PROPERTIES TIMEOUT 20) + if(WIN32) + set_tests_properties(guest_posix_named_sem PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + endif() add_test_executable(guest_process_time_tests ${LIBS_INCLUDE_DIR}/tests/GuestProcessTime.cpp) target_include_directories(guest_process_time_tests PRIVATE ${LIBS_INCLUDE_DIR}) target_link_libraries(guest_process_time_tests PRIVATE libkernel libc) @@ -214,6 +224,17 @@ if(BUILD_TESTING) set_tests_properties(guest_process_arguments PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$") endif() + find_package(Python3 QUIET COMPONENTS Interpreter) + if(CMAKE_SYSTEM_NAME STREQUAL "Linux" AND Python3_Interpreter_FOUND) + add_test_executable(guest_runtime_lifecycle_tests ${LIBS_INCLUDE_DIR}/tests/GuestRuntimeLifecycle.cpp) + target_include_directories(guest_runtime_lifecycle_tests PRIVATE ${LIBS_INCLUDE_DIR}) + target_link_libraries(guest_runtime_lifecycle_tests PRIVATE libkernel libc ${CMAKE_DL_LIBS}) + set_target_properties(guest_runtime_lifecycle_tests PROPERTIES ENABLE_EXPORTS ON) + add_dependencies(guest_runtime_lifecycle_tests relinker) + add_test(NAME guest_runtime_lifecycle COMMAND ${Python3_EXECUTABLE} + ${CMAKE_SOURCE_DIR}/core/relinker/relinker/tests/test_guest_runtime_lifecycle.py + $ $ $) + endif() add_library(guest_module_fixture SHARED ${LIBS_INCLUDE_DIR}/tests/GuestModuleFixture.cpp) target_include_directories(guest_module_fixture PRIVATE ${LIBS_INCLUDE_DIR}) add_custom_command(TARGET guest_module_fixture POST_BUILD @@ -222,11 +243,12 @@ if(BUILD_TESTING) add_dependencies(guest_module_fixture nid_patcher) configure_windows_unwind(guest_module_fixture) set_target_properties(guest_module_fixture PROPERTIES BUILD_WITH_INSTALL_RPATH ON INSTALL_RPATH "$ORIGIN") - find_package(Python3 QUIET COMPONENTS Interpreter) - if(Python3_Interpreter_FOUND AND NOT WIN32) - target_link_options(guest_module_fixture PRIVATE -Wl,--no-as-needed) - add_test(NAME nid_patcher_dynamic_strings COMMAND ${Python3_EXECUTABLE} - ${LIBS_INCLUDE_DIR}/nid/tests/test_dynamic_strings.py $) + if(Python3_Interpreter_FOUND) + if(NOT WIN32) + target_link_options(guest_module_fixture PRIVATE -Wl,--no-as-needed) + add_test(NAME nid_patcher_dynamic_strings COMMAND ${Python3_EXECUTABLE} + ${LIBS_INCLUDE_DIR}/nid/tests/test_dynamic_strings.py $) + endif() add_test(NAME nid_patcher_symbol_versions COMMAND ${Python3_EXECUTABLE} ${LIBS_INCLUDE_DIR}/nid/tests/test_symbol_versions.py $) endif() @@ -359,8 +381,27 @@ if(BUILD_TESTING) target_link_libraries(guest_host_standard_error_windows_tests PRIVATE libkernel libc) configure_windows_unwind(guest_host_standard_error_windows_tests) add_test(NAME guest_host_standard_error_windows COMMAND guest_host_standard_error_windows_tests) - set_tests_properties(guest_host_standard_error_windows PROPERTIES TIMEOUT 10 ENVIRONMENT_MODIFICATION + add_test(NAME guest_host_standard_buffering_windows COMMAND guest_host_standard_error_windows_tests --buffering) + set_tests_properties(guest_host_standard_error_windows guest_host_standard_buffering_windows PROPERTIES TIMEOUT 10 ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$") + add_test_executable(guest_closed_standard_descriptors_windows_tests ${LIBS_INCLUDE_DIR}/tests/GuestClosedStandardDescriptorsWindows.cpp) + target_include_directories(guest_closed_standard_descriptors_windows_tests PRIVATE ${LIBS_INCLUDE_DIR}) + target_link_libraries(guest_closed_standard_descriptors_windows_tests PRIVATE libkernel libc) + configure_windows_unwind(guest_closed_standard_descriptors_windows_tests) + add_test(NAME guest_closed_standard_descriptors_windows COMMAND guest_closed_standard_descriptors_windows_tests) + set_tests_properties(guest_closed_standard_descriptors_windows PROPERTIES TIMEOUT 30 ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + add_test_executable(guest_closed_standard_streams_load_windows_tests ${LIBS_INCLUDE_DIR}/tests/GuestClosedStandardStreamsLoadWindows.cpp) + target_include_directories(guest_closed_standard_streams_load_windows_tests PRIVATE ${LIBS_INCLUDE_DIR}) + target_link_libraries(guest_closed_standard_streams_load_windows_tests PRIVATE shell32) + if(MINGW) + target_compile_options(guest_closed_standard_streams_load_windows_tests PRIVATE -fasynchronous-unwind-tables) + endif() + add_dependencies(guest_closed_standard_streams_load_windows_tests libc) + configure_windows_unwind(guest_closed_standard_streams_load_windows_tests) + add_test(NAME guest_closed_standard_streams_load_windows COMMAND guest_closed_standard_streams_load_windows_tests "$") + set_tests_properties(guest_closed_standard_streams_load_windows PROPERTIES TIMEOUT 10 + ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$") endif() add_test_executable(guest_truncate_tests ${LIBS_INCLUDE_DIR}/tests/GuestTruncate.cpp) target_include_directories(guest_truncate_tests PRIVATE ${LIBS_INCLUDE_DIR}) @@ -1169,6 +1210,16 @@ if(BUILD_TESTING) set_tests_properties(guest_sched_priority PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$") endif() + add_test_executable(guest_rtprio_thread_tests ${LIBS_INCLUDE_DIR}/tests/GuestRtprioThread.cpp) + target_include_directories(guest_rtprio_thread_tests PRIVATE ${LIBS_INCLUDE_DIR}) + target_link_libraries(guest_rtprio_thread_tests PRIVATE libkernel libc) + configure_windows_unwind(guest_rtprio_thread_tests) + add_test(NAME guest_rtprio_thread COMMAND guest_rtprio_thread_tests) + set_tests_properties(guest_rtprio_thread PROPERTIES TIMEOUT 20) + if(WIN32) + set_tests_properties(guest_rtprio_thread PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + endif() add_test_executable(guest_operation_mode_tests ${LIBS_INCLUDE_DIR}/tests/GuestOperationMode.cpp) target_include_directories(guest_operation_mode_tests PRIVATE ${LIBS_INCLUDE_DIR}) target_link_libraries(guest_operation_mode_tests PRIVATE libkernel libc) @@ -1295,6 +1346,19 @@ if(BUILD_TESTING) set_tests_properties(guest_named_shm_concurrency PROPERTIES TIMEOUT 20) endif() if(WIN32) + add_test_executable(guest_named_shm_windows_tests ${LIBS_INCLUDE_DIR}/tests/GuestNamedShmWindows.cpp) + target_include_directories(guest_named_shm_windows_tests PRIVATE ${LIBS_INCLUDE_DIR} ${LIBS_INCLUDE_DIR}/nid/include) + target_link_libraries(guest_named_shm_windows_tests PRIVATE libkernel libc advapi32) + configure_windows_unwind(guest_named_shm_windows_tests) + add_test(NAME guest_named_shm_windows COMMAND guest_named_shm_windows_tests) + set_tests_properties(guest_named_shm_windows PROPERTIES TIMEOUT 20 ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + add_test(NAME guest_named_shm_windows_short_temp COMMAND guest_named_shm_windows_tests --short-temp) + set_tests_properties(guest_named_shm_windows_short_temp PROPERTIES TIMEOUT 20 SKIP_RETURN_CODE 77 ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + add_test(NAME guest_named_shm_windows_long_short_temp COMMAND guest_named_shm_windows_tests --long-short-temp) + set_tests_properties(guest_named_shm_windows_long_short_temp PROPERTIES TIMEOUT 20 SKIP_RETURN_CODE 77 ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") set_tests_properties(guest_shm_open PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$") endif() @@ -1361,9 +1425,10 @@ if(BUILD_TESTING) target_link_libraries(guest_socket_options_tests PRIVATE libkernel libc) configure_windows_unwind(guest_socket_options_tests) add_test(NAME guest_socket_options COMMAND guest_socket_options_tests) - set_tests_properties(guest_socket_options PROPERTIES TIMEOUT 30) + add_test(NAME guest_socket_options_multicast COMMAND guest_socket_options_tests multicast) + set_tests_properties(guest_socket_options guest_socket_options_multicast PROPERTIES TIMEOUT 30) if(WIN32) - set_tests_properties(guest_socket_options PROPERTIES ENVIRONMENT_MODIFICATION + set_tests_properties(guest_socket_options guest_socket_options_multicast PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$") endif() add_test_executable(guest_socket_stream_options_tests ${LIBS_INCLUDE_DIR}/tests/GuestSocketStreamOptions.cpp) @@ -1659,6 +1724,7 @@ if(BUILD_TESTING) target_link_libraries(guest_flexible_memory_budget_tests PRIVATE libkernel libc) configure_windows_unwind(guest_flexible_memory_budget_tests) add_library(guest_flexible_memory_module SHARED ${LIBS_INCLUDE_DIR}/tests/GuestFlexibleMemoryModule.cpp) + target_include_directories(guest_flexible_memory_module PRIVATE ${LIBS_INCLUDE_DIR}) set_target_properties(guest_flexible_memory_module PROPERTIES PREFIX "" SUFFIX ".guest.prx") add_dependencies(guest_flexible_memory_budget_tests guest_flexible_memory_module) target_compile_definitions(guest_flexible_memory_budget_tests PRIVATE GUEST_FLEXIBLE_MEMORY_MODULE="$") diff --git a/core/libs/prx/libkernel/DirectMemory/Export.cpp b/core/libs/prx/libkernel/DirectMemory/Export.cpp index 1afee54e9f..fa42c29e9d 100644 --- a/core/libs/prx/libkernel/DirectMemory/Export.cpp +++ b/core/libs/prx/libkernel/DirectMemory/Export.cpp @@ -218,13 +218,12 @@ int APS5_VABI sceKernelAllocateMainDirectMemory(size_t len, size_t alignment, in } int APS5_VABI sceKernelAvailableDirectMemorySize(int64_t search_start, int64_t search_end, size_t alignment, int64_t* phys_addr_out, size_t* size_out) { - if (!phys_addr_out || !size_out) return SCE_KERNEL_ERROR_EINVAL; int64_t start = 0; size_t size = 0; const int result = DirectMemoryLargestFree(search_start, search_end, alignment, &start, &size); - if (result != 0) { *phys_addr_out = 0; *size_out = 0; return result; } - *phys_addr_out = start; - *size_out = size; + if (result != 0) return result; + if (phys_addr_out) *phys_addr_out = start; + if (size_out) *size_out = size; return 0; } @@ -505,6 +504,12 @@ int APS5_VABI sceKernelGetPageTableStats(int* cpu_total, int* cpu_available, int return 0; } +int APS5_VABI sceKernelGetTraceMemoryStats(std::uint64_t* first, std::uint64_t* second) { + if (first) *first = 0; + if (second) *second = 0; + return 0; +} + int APS5_VABI sceKernelGetPrtAperture(int index, void** addr, size_t* len) { if (index < 0 || index >= PRT_APERTURE_COUNT || !addr || !len) return SCE_KERNEL_ERROR_EINVAL; std::lock_guard lock(g_prtLock); diff --git a/core/libs/prx/libkernel/DirectMemory/MemoryPool.cpp b/core/libs/prx/libkernel/DirectMemory/MemoryPool.cpp index 54cdbaf12d..44a02c7f12 100644 --- a/core/libs/prx/libkernel/DirectMemory/MemoryPool.cpp +++ b/core/libs/prx/libkernel/DirectMemory/MemoryPool.cpp @@ -154,14 +154,10 @@ struct PhysicalMemoryPool { } int LargestFree(int64_t searchStart, int64_t searchEnd, size_t alignment, int64_t* startOut, size_t* sizeOut) { - if (!startOut || !sizeOut) return SCE_KERNEL_ERROR_EINVAL; - if (searchStart < 0 || searchEnd <= searchStart) return SCE_KERNEL_ERROR_EAGAIN; - uint64_t align; - if (!NormalizeAlignment(alignment, align)) return SCE_KERNEL_ERROR_EAGAIN; - - uint64_t cur; - if (!RoundUp(static_cast(searchStart), kPage, cur)) return SCE_KERNEL_ERROR_EAGAIN; - const uint64_t limit = std::min(static_cast(searchEnd), kPoolBytes) & ~(kPage - 1); + if ((alignment & (alignment - 1)) != 0) return SCE_KERNEL_ERROR_EINVAL; + const uint64_t alignMask = std::max(alignment, kPage) - 1; + uint64_t cur = searchStart > 0 ? std::min(static_cast(searchStart) & ~(kPage - 1), kPoolBytes) : 0; + const uint64_t limit = searchEnd > 0 ? std::min((static_cast(searchEnd) + kPage - 1) & ~(kPage - 1), kPoolBytes) : 0; std::lock_guard lock(_mutex); uint64_t bestStart = 0; @@ -175,11 +171,13 @@ struct PhysicalMemoryPool { uint64_t runEnd = cur; while (runEnd < limit && !_used[runEnd / kPage]) runEnd += kPage; - uint64_t alignedStart; - if (!RoundUp(cur, align, alignedStart)) break; - if (alignedStart < runEnd && runEnd - alignedStart > bestSize) { - bestStart = alignedStart; - bestSize = runEnd - alignedStart; + const int64_t low = std::max(static_cast(cur), searchStart); + const int64_t high = std::min(static_cast(runEnd), searchEnd); + const int64_t alignedStart = static_cast((static_cast(low) + alignMask) & ~alignMask); + const uint64_t size = static_cast(high) - static_cast(alignedStart); + if (low < high && alignedStart < high && size > bestSize) { + bestStart = static_cast(alignedStart); + bestSize = size; } cur = runEnd; } @@ -267,7 +265,15 @@ private: bool splitHi = false; try { splitHi = _splitAt(hi); - ForgetDirectMemory(static_cast(lo), static_cast(hi - lo)); + for (size_t page = first; page < first + count;) { + if (!_used[page]) { + ++page; + continue; + } + const size_t runStart = page; + while (page < first + count && _used[page]) ++page; + ForgetDirectMemory(static_cast(runStart * kPage), static_cast((page - runStart) * kPage)); + } } catch (...) { if (splitHi) _unsplitAt(hi); if (splitLo) _unsplitAt(lo); diff --git a/core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp b/core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp index 8f449c1b16..89c8d28df4 100644 --- a/core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp +++ b/core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp @@ -38,6 +38,7 @@ constexpr int GuestNoMemory = 12; constexpr int GuestBadDescriptor = 9; constexpr int GuestAccess = 13; constexpr int GuestFault = 14; +constexpr int GuestBusy = 16; constexpr int GuestIoError = 5; constexpr int GuestNotSupported = 45; constexpr int GuestShared = 0x1; @@ -337,7 +338,7 @@ int APS5_VABI msync_nid_postfix(void* address, std::size_t length, int flags) { if (end < start) return failed(GuestInvalid); if ((flags & (GuestSyncAsync | GuestSyncInvalidate)) == (GuestSyncAsync | GuestSyncInvalidate)) return failed(GuestInvalid); if (start == end ? !Mapped(start) : !GuestRangeMapped(start, end)) return failed(GuestNoMemory); - if ((flags & GuestSyncInvalidate) != 0) throw std::runtime_error("msync: MS_INVALIDATE is not implemented"); + if ((flags & GuestSyncInvalidate) != 0) return failed(GuestBusy); return 0; } diff --git a/core/libs/prx/libkernel/Equeue/Equeue.cpp b/core/libs/prx/libkernel/Equeue/Equeue.cpp index 1256cc9391..a0d5532a03 100644 --- a/core/libs/prx/libkernel/Equeue/Equeue.cpp +++ b/core/libs/prx/libkernel/Equeue/Equeue.cpp @@ -12,6 +12,64 @@ #include #include +extern "C" int APS5_VABI sceKernelFstat(int d, FileStat* sb); + +static constexpr std::uint64_t FileRepollNs = 1000000; + +struct UnreadFile { + bool measured = false; + std::int64_t bytes = 0; +}; + +static UnreadFile UnreadFileBytes(int file) { + FileStat status{}; + if (sceKernelFstat(file, &status) != 0) { + return {}; + } + const std::int64_t offset = sceKernelLseek(file, 0, 1); + if (offset < 0) { + return {}; + } + return {true, status.st_size - offset}; +} + +static bool RegularFile(int descriptor) { + if (File::IsHostStream(descriptor)) { + return false; + } + FileStat status{}; + return sceKernelFstat(descriptor, &status) == 0 && (status.st_mode & 0170000) == 0100000; +} + +static KernelEqueueEvent FileEvent(int descriptor, int16_t filter, uint16_t flags, void* udata) { + KernelEqueueEvent event{}; + event.event.ident = static_cast(descriptor); + event.event.filter = filter; + event.event.flags = flags; + event.event.udata = udata; + event.filter.repoll = true; + if (filter == EVFILT_WRITE) { + event.filter.pollFunc = [](KernelEqueueEvent* e) { + e->event.data = 0; + return true; + }; + } else { + event.filter.owner = std::make_shared(); + event.filter.pollFunc = [](KernelEqueueEvent* e) { + const auto* unread = static_cast(e->filter.owner.get()); + if (!unread->measured || unread->bytes == 0) { + return false; + } + e->event.data = static_cast(unread->bytes); + return true; + }; + } + event.filter.resetFunc = [](KernelEqueueEvent* e) { + e->triggered = false; + }; + return event; +} + static std::unordered_map g_equeues; static std::mutex g_equeueMutex; static uint64_t g_nextEqueue = 1; @@ -21,6 +79,7 @@ struct KernelEqueuePrivate::WaitSet { std::vector> held; std::vector> queues; uint64_t nearestTimerNs = std::numeric_limits::max(); + bool repoll = false; }; extern "C" { @@ -98,6 +157,28 @@ bool KernelEqueuePrivate::PollEvents() { return polled; } +void KernelEqueuePrivate::MeasureFiles(std::unique_lock& lock) { + std::vector>> files; + for (const auto& e : m_events) { + if (e.enabled && e.filter.repoll && e.event.filter == EVFILT_READ) { + files.emplace_back(static_cast(e.event.ident), e.filter.owner); + } + } + if (files.empty()) { + return; + } + lock.unlock(); + std::vector measured; + measured.reserve(files.size()); + for (const auto& [file, owner] : files) { + measured.push_back(UnreadFileBytes(file)); + } + lock.lock(); + for (std::size_t i = 0; i < files.size(); ++i) { + *static_cast(files[i].second.get()) = measured[i]; + } +} + void KernelEqueuePrivate::WakePollers() { for (const auto waker : m_pollers) { GuestSockets::Wake(waker); @@ -117,6 +198,8 @@ void KernelEqueuePrivate::GatherInterests(WaitSet& wait, std::uintptr_t waker, b } if (e.filter.watched) { GatherWatched(wait, e.filter.watched, waker); + } else if (e.filter.repoll) { + wait.repoll = true; } else if (e.filter.waitDescriptor >= 0) { wait.interests.push_back({e.filter.waitDescriptor, false}); wait.held.push_back(e.filter.owner); @@ -151,6 +234,12 @@ void KernelEqueuePrivate::WaitForDescriptors(std::unique_lock& lock, const auto waker = GuestSockets::CurrentWaker(); WaitSet wait; GatherInterests(wait, waker, false); + if (wait.repoll) { + const std::uint64_t soon = TimedWait::NowNanos() + FileRepollNs; + if (deadlineNanos == 0 || deadlineNanos > soon) { + deadlineNanos = soon; + } + } m_pollers.push_back(waker); if (!wait.queues.empty()) { PollEvents(); @@ -187,9 +276,10 @@ int KernelEqueuePrivate::CollectTriggered(KernelEvent* ev, int num) { return CollectOncePerWait(ev, num); } int ret = 0; + std::list requeued; for (auto it = m_events.begin(); it != m_events.end() && ret < num;) { auto& e = *it; - if (!e.enabled) { + if (!e.triggered || !e.enabled) { ++it; continue; } @@ -221,8 +311,15 @@ int KernelEqueuePrivate::CollectTriggered(KernelEvent* ev, int num) { e.pendingEvents.pop_front(); e.triggered = true; } - it = erase ? m_events.erase(it) : std::next(it); + const auto next = std::next(it); + if (erase) { + m_events.erase(it); + } else { + requeued.splice(requeued.end(), m_events, it); + } + it = next; } + m_events.splice(m_events.end(), requeued); return ret; } @@ -271,6 +368,10 @@ int KernelEqueuePrivate::GetTriggeredEvents(KernelEvent* ev, int num) { if (m_closed) { return SCE_KERNEL_ERROR_EBADF; } + MeasureFiles(lock); + if (m_closed) { + return SCE_KERNEL_ERROR_EBADF; + } TriggerExpiredTimers(MonotonicNs()); PollEvents(); return CollectTriggered(ev, num); @@ -285,6 +386,10 @@ std::size_t KernelEqueuePrivate::CountTriggeredEvents() { if (m_closed) { return 0; } + MeasureFiles(lock); + if (m_closed) { + return 0; + } TriggerExpiredTimers(MonotonicNs()); PollEvents(); return static_cast(std::count_if(m_events.begin(), m_events.end(), [](const KernelEqueueEvent& event) { return event.enabled && event.triggered; })); @@ -311,6 +416,10 @@ int KernelEqueuePrivate::WaitForEventsUntil(KernelEvent* ev, int num, std::uint6 return SCE_KERNEL_ERROR_EBADF; } for (;;) { + MeasureFiles(lock); + if (m_closed) { + return SCE_KERNEL_ERROR_EBADF; + } TriggerExpiredTimers(MonotonicNs()); const bool polling = PollEvents(); const int ret = CollectTriggered(ev, num); @@ -414,6 +523,9 @@ int KernelEqueuePrivate::EditEvent(uintptr_t ident, int16_t filter, const std::f if (m_closed) { return SCE_KERNEL_ERROR_EBADF; } + if (filter == EVFILT_TIMER) { + TriggerExpiredTimers(MonotonicNs()); + } auto it = std::find_if(m_events.begin(), m_events.end(), [ident, filter](const auto& e) { return e.event.ident == ident && e.event.filter == filter; @@ -709,15 +821,19 @@ static int AddDescriptorEvent(KernelEqueue eq, int fd, std::size_t size, void* u std::int64_t available = 0; bool eof = false; const bool isPipe = !isSocket && filter == EVFILT_READ && !File::IsHostStream(fd) && GuestSockets::PipeReadState(fd, &available, &eof); - if (!isSocket && !isPipe && DescriptorIsOpen_nid_no_patch(fd)) { - NotImplemented_nid_no_patch(filter == EVFILT_READ ? "sceKernelAddReadEvent: descriptor that is neither a socket nor the read end of a pipe" : "sceKernelAddWriteEvent: non-socket descriptor"); + const bool isFile = !isSocket && !isPipe && RegularFile(fd); + if (!isSocket && !isPipe && !isFile && DescriptorIsOpen_nid_no_patch(fd)) { + NotImplemented_nid_no_patch(filter == EVFILT_READ ? "sceKernelAddReadEvent: descriptor that is neither a socket, the read end of a pipe nor a regular file" : "sceKernelAddWriteEvent: descriptor that is neither a socket nor a regular file"); } if (size > 1 && !isPipe) { NotImplemented_nid_no_patch(filter == EVFILT_READ ? "sceKernelAddReadEvent: low watermark" : "sceKernelAddWriteEvent: low watermark"); } - if (!isSocket && !isPipe) { + if (!isSocket && !isPipe && !isFile) { return SCE_KERNEL_ERROR_EBADF; } + if (isFile) { + return EqueueAddEvent_nid_postfix(eq, FileEvent(fd, filter, EV_ADD, udata)); + } return EqueueAddEvent_nid_postfix(eq, EqueueDescriptorEvent(fd, filter, EV_ADD, udata)); } diff --git a/core/libs/prx/libkernel/Equeue/Equeue.hpp b/core/libs/prx/libkernel/Equeue/Equeue.hpp index ef217632d6..6b9194be56 100644 --- a/core/libs/prx/libkernel/Equeue/Equeue.hpp +++ b/core/libs/prx/libkernel/Equeue/Equeue.hpp @@ -57,6 +57,7 @@ struct KernelFilter { EqueueDeleteFunc deleteEventFunc = nullptr; EqueuePollFunc pollFunc = nullptr; int waitDescriptor = -1; + bool repoll = false; std::shared_ptr watched; }; @@ -104,6 +105,7 @@ private: int CollectTriggered(KernelEvent* ev, int num); int CollectOncePerWait(KernelEvent* ev, int num); bool PollEvents(); + void MeasureFiles(std::unique_lock& lock); void WakePollers(); void WaitForDescriptors(std::unique_lock& lock, std::uint64_t deadlineNanos); void GatherInterests(WaitSet& wait, std::uintptr_t waker, bool nested); diff --git a/core/libs/prx/libkernel/Equeue/src/Kqueue.cpp b/core/libs/prx/libkernel/Equeue/src/Kqueue.cpp index 917bc4a8f8..609da35b20 100644 --- a/core/libs/prx/libkernel/Equeue/src/Kqueue.cpp +++ b/core/libs/prx/libkernel/Equeue/src/Kqueue.cpp @@ -1,8 +1,12 @@ #include "prx/libkernel/Equeue/Equeue.hpp" #include "prx/libkernel/Equeue/include/EdgeWatch.hpp" #include "prx/libc/include/General.hpp" +#include "prx/libkernel/File/include/ArchivedFile.hpp" +#include "prx/libkernel/File/include/DirectoryDescriptor.hpp" #include "prx/libkernel/File/include/File.hpp" #include "prx/libkernel/File/include/HostStreams.hpp" +#include "prx/libkernel/File/include/RandomDevice.hpp" +#include "prx/libkernel/Socket/include/SocketPoll.hpp" #include "prx/libkernel/Socket/include/SocketRuntime.hpp" #include "prx/libkernel/Time/include/TimedWait.hpp" @@ -19,10 +23,12 @@ #include #ifdef _WIN32 +#include "prx/libc/include/WindowsDescriptor.hpp" #include #include #else #include +#include #include #endif @@ -30,6 +36,7 @@ extern "C" int* APS5_VABI __error_nid_postfix(); namespace { +constexpr int GUEST_EPERM = 1; constexpr int GUEST_ENOENT = 2; constexpr int GUEST_EBADF = 9; constexpr int GUEST_EFAULT = 14; @@ -39,6 +46,10 @@ constexpr int GUEST_EMFILE = 24; constexpr int GUEST_EPIPE = 32; constexpr int KQ_NEVENTS = 8; +constexpr std::int16_t EVFILT_AIO = -3; +constexpr std::int16_t EVFILT_PROCDESC = -8; +constexpr std::int16_t EVFILT_LIO = -10; +constexpr std::int16_t EVFILT_SENDFILE = -12; constexpr std::uint32_t NOTE_FFCTRLMASK = 0xc0000000; constexpr std::uint32_t NOTE_FFLAGSMASK = 0x00ffffff; constexpr std::uint32_t NOTE_FFAND = 0x40000000; @@ -128,6 +139,27 @@ int DescriptorFilterError(int kqueueDescriptor, std::uintptr_t ident) { return descriptor == kqueueDescriptor ? GUEST_EINVAL : 0; } +bool MeasuredProcdescDescriptor(int descriptor) { + std::shared_ptr owner; + std::uintptr_t native = 0; + if (GuestSockets::IsOpen(descriptor) || KernelLookupSocketNative_nid_no_patch(descriptor, &owner, &native) || FindKqueue(descriptor)) { + return true; + } + if (descriptor < 3 || File::IsRandomDevice(descriptor)) { + return false; + } + if (File::IsArchivedFile(descriptor) || File::DirectoryDescriptorPath(descriptor)) { + return true; + } +#ifdef _WIN32 + const DWORD type = ::GetFileType(WindowsDescriptorHandle_nid_no_patch(descriptor)); + return type == FILE_TYPE_DISK || type == FILE_TYPE_PIPE; +#else + struct stat status{}; + return ::fstat(descriptor, &status) == 0 && (S_ISREG(status.st_mode) || S_ISDIR(status.st_mode) || S_ISFIFO(status.st_mode) || S_ISSOCK(status.st_mode)); +#endif +} + std::int64_t DescriptorLowWater(const KernelEvent& change) { if ((change.fflags & NOTE_LOWAT) == 0 || !GuestSockets::IsOpen(static_cast(change.ident))) { return 0; @@ -327,6 +359,47 @@ void TouchUserKnote(KernelEqueueEvent& event, KernelEvent& change) { } } +int ModifyTimerKnote(const KernelEqueueRef& queue, KernelEvent& change) { + if ((change.flags & ~(EV_ADD | EV_ONESHOT)) != 0) { + NotImplemented_nid_no_patch("kevent: re-adding an EVFILT_TIMER with flags other than EV_ONESHOT"); + } + if (change.data <= 0 || change.fflags != 0) { + NotImplemented_nid_no_patch("kevent: re-adding an EVFILT_TIMER with nonzero fflags or a period of 0 or less"); + } + const auto periodNs = TimerPeriodNanos(change.data, change.fflags); + bool oneShot = false; + bool disabled = false; + bool overflow = false; + const int error = GuestErrno(queue->EditEvent(change.ident, change.filter, [&change, &oneShot, &disabled, &overflow, periodNs](KernelEqueueEvent& event) { + if (event.intervalNs == 0) { + oneShot = true; + return; + } + if (!event.enabled) { + disabled = true; + return; + } + if (*periodNs > std::numeric_limits::max() - event.deadlineNs) { + overflow = true; + return; + } + event.intervalNs = *periodNs; + event.event.udata = change.udata; + })); + if (oneShot) { + NotImplemented_nid_no_patch("kevent: re-adding a one-shot EVFILT_TIMER"); + } + if (disabled) { + NotImplemented_nid_no_patch("kevent: re-adding a disabled EVFILT_TIMER"); + } + if (overflow) { + NotImplemented_nid_no_patch("kevent: re-adding an EVFILT_TIMER with a period that overflows the armed expiry"); + } + change.fflags = 0; + change.data = 0; + return error; +} + int ModifyKnote(const KernelEqueueRef& queue, KernelEvent& change) { if (change.filter == EVFILT_TIMER) { NotImplemented_nid_no_patch("kevent: changing an existing EVFILT_TIMER"); @@ -369,6 +442,28 @@ int Register(const KernelEqueueRef& queue, int kqueueDescriptor, KernelEvent& ch if (change.filter > 0) { return GUEST_EINVAL; } + if (change.filter == EVFILT_AIO || change.filter == EVFILT_LIO || change.filter == EVFILT_SENDFILE) { + if ((change.flags & EV_ADD) == 0) { + return GUEST_ENOENT; + } + change.fflags = 0; + change.data = 0; + return GUEST_EPERM; + } + if (change.filter == EVFILT_PROCDESC) { + if (const int error = DescriptorFilterError(kqueueDescriptor, change.ident)) { + return error; + } + if ((change.flags & EV_ADD) == 0) { + return GUEST_ENOENT; + } + if (!MeasuredProcdescDescriptor(static_cast(change.ident))) { + NotImplemented_nid_no_patch("kevent: EVFILT_PROCDESC on a kind of descriptor that was not measured"); + } + change.fflags = 0; + change.data = 0; + return GUEST_EINVAL; + } if (change.filter != EVFILT_READ && change.filter != EVFILT_WRITE && change.filter != EVFILT_VNODE && change.filter != EVFILT_TIMER && change.filter != EVFILT_USER) { NotImplemented_nid_no_patch(("kevent: filter " + std::to_string(change.filter)).c_str()); } @@ -403,6 +498,9 @@ int Register(const KernelEqueueRef& queue, int kqueueDescriptor, KernelEvent& ch if ((change.flags & EV_DELETE) != 0) { return GuestErrno(queue->DeleteEvent(change.ident, change.filter)); } + if (change.filter == EVFILT_TIMER && (change.flags & EV_ADD) != 0) { + return ModifyTimerKnote(queue, change); + } return ModifyKnote(queue, change); } diff --git a/core/libs/prx/libkernel/File/include/SharedMemory.hpp b/core/libs/prx/libkernel/File/include/SharedMemory.hpp index 5c9ffc9f47..7d9be97385 100644 --- a/core/libs/prx/libkernel/File/include/SharedMemory.hpp +++ b/core/libs/prx/libkernel/File/include/SharedMemory.hpp @@ -14,6 +14,15 @@ std::int64_t WriteSharedMemory_nid_no_patch(int descriptor, const void* buffer, std::int64_t PwriteSharedMemory_nid_no_patch(int descriptor, const void* buffer, std::size_t size, std::int64_t offset); std::int64_t SeekSharedMemory_nid_no_patch(int descriptor, std::int64_t offset, int whence); int TruncateSharedMemory_nid_no_patch(int descriptor, std::int64_t length); +#ifdef _WIN32 +int SetSharedMemoryMode_nid_no_patch(int descriptor, int mode); +int SharedMemoryDescriptorWritable_nid_no_patch(int descriptor); +void TrackSharedMemoryDescriptor_nid_no_patch(int descriptor, bool writable); +void ForgetSharedMemoryDescriptor_nid_no_patch(int descriptor); +void DuplicateSharedMemoryDescriptor_nid_no_patch(int source, int destination); +int CloseSharedMemoryDescriptor_nid_no_patch(int descriptor); +int ReplaceSharedMemoryDescriptor_nid_no_patch(int source, int destination); +#endif #ifndef _WIN32 std::int64_t WritevSharedMemory_nid_no_patch(int descriptor, const struct iovec* vectors, int count); std::int64_t PwritevSharedMemory_nid_no_patch(int descriptor, const struct iovec* vectors, int count, std::int64_t offset); diff --git a/core/libs/prx/libkernel/File/src/DescriptorControl.cpp b/core/libs/prx/libkernel/File/src/DescriptorControl.cpp index f832da7ae0..8f7dd2c448 100644 --- a/core/libs/prx/libkernel/File/src/DescriptorControl.cpp +++ b/core/libs/prx/libkernel/File/src/DescriptorControl.cpp @@ -22,8 +22,10 @@ #include #ifdef _WIN32 +#include #include #include +#include "prx/libc/include/General.hpp" #include "prx/libc/include/WindowsDescriptor.hpp" #else #include @@ -180,8 +182,33 @@ Entry InitialEntry(int fd) { return {false, std::make_shared(status)}; } +void SetHostAppend(int fd, bool append, const char* function) { + const int translation = ::_setmode(fd, _O_BINARY); + if (translation == -1 || ::_setmode(fd, translation) == -1) + throw std::runtime_error(std::string(function) + ": cannot read the translation mode of descriptor " + std::to_string(fd) + ", errno=" + std::to_string(errno)); + HANDLE copy = nullptr; + if (!::DuplicateHandle(::GetCurrentProcess(), WindowsDescriptorHandle_nid_no_patch(fd), ::GetCurrentProcess(), ©, 0, TRUE, DUPLICATE_SAME_ACCESS)) + throw std::runtime_error(std::string(function) + ": cannot duplicate the handle of descriptor " + std::to_string(fd) + ", error=" + std::to_string(::GetLastError())); + const int replacement = ::_open_osfhandle(reinterpret_cast(copy), (append ? _O_APPEND : 0) | translation); + if (replacement < 0) { + ::CloseHandle(copy); + throw std::runtime_error(std::string(function) + ": cannot open a replacement for descriptor " + std::to_string(fd) + ", errno=" + std::to_string(errno)); + } + auto* const standard = HostStandardHandle_nid_no_patch(fd); + const int moved = ::_dup2(replacement, fd); + const int error = errno; + ::_close(replacement); + KeepStandardHandle_nid_no_patch(fd, standard); + if (moved != 0) + throw std::runtime_error(std::string(function) + ": cannot replace descriptor " + std::to_string(fd) + ", errno=" + std::to_string(error)); +} + void ApplyHostStatus(int fd, Kind kind, int changed, int next, const char* function) { - if ((changed & Append) && kind == Kind::File) Unsupported(function, "changing O_APPEND", kind, fd); + if ((changed & Append) && kind == Kind::File) { + const auto status = g_entries.at(fd).status; + for (const auto& [descriptor, entry] : g_entries) + if (entry.status == status && DescriptorIsOpen_nid_no_patch(descriptor)) SetHostAppend(descriptor, (next & Append) != 0, function); + } if ((changed & Nonblocking) && kind == Kind::Device) Unsupported(function, "changing O_NONBLOCK", kind, fd); if ((changed & Nonblocking) && kind == Kind::Pipe) SetPipeNonblocking(fd, (next & Nonblocking) != 0, function); } diff --git a/core/libs/prx/libkernel/File/src/Open.cpp b/core/libs/prx/libkernel/File/src/Open.cpp index d3fe98dd1f..71668b784b 100644 --- a/core/libs/prx/libkernel/File/src/Open.cpp +++ b/core/libs/prx/libkernel/File/src/Open.cpp @@ -87,6 +87,8 @@ static int NativeWrite(int fd, const void* buf, std::size_t n) { extern "C" _invalid_parameter_handler _set_thread_local_invalid_parameter_handler(_invalid_parameter_handler); static void IgnoreInvalidParameter(const wchar_t*, const wchar_t*, const wchar_t*, unsigned int, std::uintptr_t) {} static int NativeClose(int fd) { + const int shared = File::CloseSharedMemoryDescriptor_nid_no_patch(fd); + if (shared != -2) return shared; const auto previous = _set_thread_local_invalid_parameter_handler(IgnoreInvalidParameter); const int result = ::_close(fd); _set_thread_local_invalid_parameter_handler(previous); @@ -296,10 +298,10 @@ int OpenInDirectory_nid_no_patch(int directory, const char* path, int flags, std int APS5_VABI sceKernelClose(int d) { if (File::IsHostStream(d)) return SCE_KERNEL_ERROR_EPERM; if (GuestSockets::IsOpen(d)) return GuestSockets::Close(d) == 0 ? 0 : SceKernelError(*__error_nid_postfix()); + EqueueDescriptorClosed(d); #ifdef _WIN32 const auto positioned = File::LockPositionedTransfer(d); #endif - EqueueDescriptorClosed(d); File::ForgetRandomDevice(d); File::ForgetDescriptor(d); ForgetJitSharedMemory(d); diff --git a/core/libs/prx/libkernel/File/src/SharedMemory.cpp b/core/libs/prx/libkernel/File/src/SharedMemory.cpp index c5b133d941..9378f47f62 100644 --- a/core/libs/prx/libkernel/File/src/SharedMemory.cpp +++ b/core/libs/prx/libkernel/File/src/SharedMemory.cpp @@ -14,6 +14,8 @@ #include #include #include +#include +#include #ifdef _WIN32 #include @@ -22,6 +24,11 @@ #include #include #include +#include +#include +#include +#include "nid/Sha1.hpp" +extern "C" _invalid_parameter_handler _set_thread_local_invalid_parameter_handler(_invalid_parameter_handler); #else #include "nid/Sha1.hpp" #include @@ -37,6 +44,32 @@ extern "C" int* APS5_VABI __error_nid_postfix(); namespace { +#ifdef _WIN32 +std::mutex sharedDescriptorAccessMutex; +std::unordered_map sharedDescriptorAccess; + +const std::wstring& WindowsNamedRoot() { + static const std::wstring root = [] { + const auto directory = std::filesystem::temp_directory_path(); + std::vector pathBuffer(32768); + const HANDLE handle = ::CreateFileW(directory.c_str(), 0, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, + nullptr, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, nullptr); + if (handle == INVALID_HANDLE_VALUE) + throw std::system_error(static_cast(::GetLastError()), std::system_category(), "opening the named shared-memory temporary root"); + const DWORD length = ::GetFinalPathNameByHandleW(handle, pathBuffer.data(), static_cast(pathBuffer.size()), + FILE_NAME_NORMALIZED | VOLUME_NAME_DOS); + const DWORD error = length == 0 ? ::GetLastError() : length >= pathBuffer.size() ? ERROR_FILENAME_EXCED_RANGE : ERROR_SUCCESS; + ::CloseHandle(handle); + if (error != ERROR_SUCCESS) + throw std::system_error(static_cast(error), std::system_category(), "resolving the named shared-memory temporary root"); + return (std::filesystem::path(std::wstring(pathBuffer.data(), length)) / L"AnyPS5" / L"shm").wstring(); + }(); + return root; +} + +void IgnoreDescriptorParameter(const wchar_t*, const wchar_t*, const wchar_t*, unsigned int, std::uintptr_t) {} +#endif + bool IsHostSharedMemoryDescriptor(int descriptor) { #ifdef __linux__ char descriptorPath[64]; @@ -47,6 +80,44 @@ bool IsHostSharedMemoryDescriptor(int descriptor) { objectPath[length] = '\0'; return std::string_view(objectPath).starts_with("/dev/shm/anyps5-shm-") || std::string_view(objectPath).starts_with("/memfd:anyps5 shm_open"); +#elif defined(_WIN32) + { + std::lock_guard lock(sharedDescriptorAccessMutex); + if (!sharedDescriptorAccess.contains(descriptor)) return false; + } + const auto previous = _set_thread_local_invalid_parameter_handler(IgnoreDescriptorParameter); + const auto handleValue = ::_get_osfhandle(descriptor); + _set_thread_local_invalid_parameter_handler(previous); + if (handleValue == -1) return false; + const auto handle = reinterpret_cast(handleValue); + std::vector pathBuffer(32768); + const DWORD length = ::GetFinalPathNameByHandleW(handle, pathBuffer.data(), static_cast(pathBuffer.size()), FILE_NAME_NORMALIZED | VOLUME_NAME_DOS); + if (length == 0 || length >= pathBuffer.size()) return false; + std::wstring objectPath(pathBuffer.data(), length); + std::wstring_view root; + try { + root = WindowsNamedRoot(); + } catch (...) { + return false; + } + if (objectPath.size() <= root.size() || objectPath[root.size()] != L'\\') return false; + if (::CompareStringOrdinal(objectPath.data(), static_cast(root.size()), root.data(), static_cast(root.size()), TRUE) != CSTR_EQUAL) return false; + const std::wstring_view relative(objectPath.data() + root.size() + 1, objectPath.size() - root.size() - 1); + if (relative.find_first_of(L"\\/") != std::wstring_view::npos) return false; + const auto validHex = [](wchar_t character) { + return (character >= L'0' && character <= L'9') || (character >= L'a' && character <= L'f'); + }; + if (relative.size() < 44 || !std::all_of(relative.begin(), relative.begin() + 40, validHex)) return false; + if (relative.substr(40) == L".dat") return true; + constexpr std::wstring_view marker = L"-unlinked-"; + if (!relative.substr(40).starts_with(marker) || !relative.ends_with(L".dat")) return false; + const auto suffix = relative.substr(40 + marker.size(), relative.size() - 40 - marker.size() - 4); + const auto separator = suffix.find(L'-'); + if (separator == std::wstring_view::npos || separator == 0 || separator + 1 == suffix.size()) return false; + const auto decimal = [](std::wstring_view value) { + return std::all_of(value.begin(), value.end(), [](wchar_t character) { return character >= L'0' && character <= L'9'; }); + }; + return decimal(suffix.substr(0, separator)) && decimal(suffix.substr(separator + 1)); #else (void)descriptor; return false; @@ -107,7 +178,6 @@ int GuestError(int error) { } } -#ifndef _WIN32 int NamedGuestError(int error) { switch (error) { case ENOENT: @@ -133,14 +203,17 @@ int NamedGuestError(int error) { } } -std::string NamedHostPath(const char* path, int& error) { +std::string NamedHostName(const char* path, int& error) { if (path == nullptr) { error = EFAULT; return {}; } std::size_t length = 0; while (length < 1024 && path[length] != '\0') ++length; if (length == 1024) { error = ENAMETOOLONG; return {}; } if (length == 0 || path[0] != '/') { error = EINVAL; return {}; } const auto digest = Nid::Sha1(std::vector(path, path + length)); - std::string hostName = "/anyps5-shm-"; + std::string hostName; +#ifndef _WIN32 + hostName = "/anyps5-shm-"; +#endif for (const auto byte : digest) { hostName += "0123456789abcdef"[byte >> 4]; hostName += "0123456789abcdef"[byte & 15]; @@ -149,9 +222,384 @@ std::string NamedHostPath(const char* path, int& error) { return hostName; } +#ifdef _WIN32 +int WindowsNamedError(DWORD error) { + switch (error) { + case ERROR_FILE_NOT_FOUND: + case ERROR_PATH_NOT_FOUND: return ENOENT; + case ERROR_ACCESS_DENIED: + case ERROR_SHARING_VIOLATION: return EACCES; + case ERROR_FILE_EXISTS: + case ERROR_ALREADY_EXISTS: return EEXIST; + case ERROR_INVALID_NAME: return EINVAL; + case ERROR_TOO_MANY_OPEN_FILES: return EMFILE; + case ERROR_DISK_FULL: return ENOSPC; + case ERROR_NOT_ENOUGH_MEMORY: + case ERROR_OUTOFMEMORY: return ENOMEM; + default: return 0; + } +} + +std::filesystem::path WindowsNamedPath(const std::string& name) { + return std::filesystem::path(WindowsNamedRoot()) / std::filesystem::path(name + ".dat"); +} + +void EnsurePrivateWindowsDirectory(const std::filesystem::path& path, bool objectDirectory = false) { + const std::wstring ownerRights = objectDirectory ? L"0x001701ff" : L"FA"; + const std::wstring policy = L"D:P(A;OICI;FA;;;SY)(A;OICI;FA;;;BA)(A;OICI;" + ownerRights + L";;;OW)"; + PSECURITY_DESCRIPTOR descriptor = nullptr; + if (!::ConvertStringSecurityDescriptorToSecurityDescriptorW( + policy.c_str(), SDDL_REVISION_1, &descriptor, nullptr)) + throw std::system_error(static_cast(::GetLastError()), std::system_category(), "creating the named shared-memory access policy"); + PACL dacl = nullptr; + BOOL present = FALSE; + BOOL defaulted = FALSE; + if (!::GetSecurityDescriptorDacl(descriptor, &present, &dacl, &defaulted) || !present || dacl == nullptr) { + const DWORD error = ::GetLastError(); + ::LocalFree(descriptor); + throw std::system_error(static_cast(error == ERROR_SUCCESS ? ERROR_INVALID_SECURITY_DESCR : error), + std::system_category(), "reading the named shared-memory access policy"); + } + + SECURITY_ATTRIBUTES security{sizeof(SECURITY_ATTRIBUTES), descriptor, FALSE}; + if (!::CreateDirectoryW(path.c_str(), &security) && ::GetLastError() != ERROR_ALREADY_EXISTS) { + const DWORD error = ::GetLastError(); + ::LocalFree(descriptor); + throw std::system_error(static_cast(error), std::system_category(), "creating the named shared-memory directory"); + } + const DWORD attributes = ::GetFileAttributesW(path.c_str()); + if (attributes == INVALID_FILE_ATTRIBUTES || (attributes & FILE_ATTRIBUTE_DIRECTORY) == 0 || + (attributes & FILE_ATTRIBUTE_REPARSE_POINT) != 0) { + const DWORD error = attributes == INVALID_FILE_ATTRIBUTES ? ::GetLastError() : ERROR_ACCESS_DENIED; + ::LocalFree(descriptor); + throw std::system_error(static_cast(error), std::system_category(), "validating the named shared-memory directory"); + } + const DWORD result = ::SetNamedSecurityInfoW(const_cast(path.c_str()), SE_FILE_OBJECT, + DACL_SECURITY_INFORMATION | PROTECTED_DACL_SECURITY_INFORMATION, + nullptr, nullptr, dacl, nullptr); + ::LocalFree(descriptor); + if (result != ERROR_SUCCESS) throw std::system_error(static_cast(result), std::system_category(), "securing the named shared-memory directory"); +} + +DWORD SetWindowsNamedPermissions(HANDLE handle, int mode) { + std::wstring policy = L"D:P(A;OICI;FA;;;SY)(A;;WD;;;OW)"; + if ((mode & 0400) != 0) policy += L"(A;;GRWD;;;OW)"; + if ((mode & 0200) != 0) policy += L"(A;;GWSDWD;;;OW)"; + PSECURITY_DESCRIPTOR descriptor = nullptr; + if (!::ConvertStringSecurityDescriptorToSecurityDescriptorW(policy.c_str(), SDDL_REVISION_1, &descriptor, nullptr)) + return ::GetLastError(); + PACL dacl = nullptr; + BOOL present = FALSE; + BOOL defaulted = FALSE; + if (!::GetSecurityDescriptorDacl(descriptor, &present, &dacl, &defaulted) || !present || dacl == nullptr) { + const DWORD error = ::GetLastError(); + ::LocalFree(descriptor); + return error == ERROR_SUCCESS ? ERROR_INVALID_SECURITY_DESCR : error; + } + const DWORD result = ::SetSecurityInfo(handle, SE_FILE_OBJECT, + DACL_SECURITY_INFORMATION | PROTECTED_DACL_SECURITY_INFORMATION, nullptr, nullptr, dacl, nullptr); + ::LocalFree(descriptor); + return result; +} + +void CleanupUnlinkedWindowsObjects(const std::filesystem::path& objectPath, const std::string& name) { + const auto pattern = objectPath.parent_path() / (std::wstring(name.begin(), name.end()) + L"-unlinked-*.dat"); + WIN32_FIND_DATAW data{}; + const HANDLE search = ::FindFirstFileW(pattern.c_str(), &data); + if (search == INVALID_HANDLE_VALUE) return; + do { + if ((data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) { + const auto orphanPath = objectPath.parent_path() / data.cFileName; + HANDLE orphan = ::CreateFileW(orphanPath.c_str(), DELETE, 0, nullptr, OPEN_EXISTING, + FILE_FLAG_OPEN_REPARSE_POINT, nullptr); + if (orphan != INVALID_HANDLE_VALUE) { + FILE_DISPOSITION_INFO deletion{TRUE}; + ::SetFileInformationByHandle(orphan, FileDispositionInfo, &deletion, sizeof(deletion)); + ::CloseHandle(orphan); + } + } + } while (::FindNextFileW(search, &data)); + ::FindClose(search); +} + +void ReapUnlinkedWindowsObject(const std::filesystem::path& orphanPath) { + HANDLE orphan = ::CreateFileW(orphanPath.c_str(), DELETE, 0, nullptr, OPEN_EXISTING, + FILE_FLAG_OPEN_REPARSE_POINT, nullptr); + if (orphan == INVALID_HANDLE_VALUE) return; + FILE_DISPOSITION_INFO deletion{TRUE}; + ::SetFileInformationByHandle(orphan, FileDispositionInfo, &deletion, sizeof(deletion)); + ::CloseHandle(orphan); +} + +class WindowsNamedLock { +public: + explicit WindowsNamedLock(const std::string& name) { + Initialize(WindowsNamedPath(name).wstring()); + } + + explicit WindowsNamedLock(HANDLE object) { + BY_HANDLE_FILE_INFORMATION status{}; + if (!::GetFileInformationByHandle(object, &status)) { error = ::GetLastError(); return; } + Initialize(L"object-" + std::to_wstring(status.dwVolumeSerialNumber) + L"-" + + std::to_wstring(status.nFileIndexHigh) + L"-" + std::to_wstring(status.nFileIndexLow)); + } + + ~WindowsNamedLock() { + if (locked) ::ReleaseMutex(handle); + if (handle != nullptr) ::CloseHandle(handle); + } + + DWORD Error() const { return error; } + +private: + void Initialize(const std::wstring& identity) { + const auto bytes = std::vector(reinterpret_cast(identity.data()), + reinterpret_cast(identity.data() + identity.size())); + const auto digest = Nid::Sha1(bytes); + std::wstring lockName = L"Global\\AnyPS5NamedShm-"; + for (const auto byte : digest) { + lockName += L"0123456789abcdef"[byte >> 4]; + lockName += L"0123456789abcdef"[byte & 15]; + } + handle = ::CreateMutexW(nullptr, FALSE, lockName.c_str()); + if (handle == nullptr) { error = ::GetLastError(); return; } + const DWORD result = ::WaitForSingleObject(handle, INFINITE); + if (result == WAIT_OBJECT_0 || result == WAIT_ABANDONED) locked = true; + else error = result == WAIT_FAILED ? ::GetLastError() : ERROR_GEN_FAILURE; + } + HANDLE handle = nullptr; + DWORD error = ERROR_SUCCESS; + bool locked = false; +}; + +int OpenNamedFile(const char* path, int flags, int mode) { + const int savedError = errno; + int validationError = 0; + const auto hostName = NamedHostName(path, validationError); + if (validationError != 0) return Fail(NamedGuestError(validationError) ? NamedGuestError(validationError) : GuestInvalid); + + WindowsNamedLock namespaceLock(hostName); + if (namespaceLock.Error() != ERROR_SUCCESS) { + if (const int guest = WindowsNamedError(namespaceLock.Error())) return Fail(NamedGuestError(guest)); + throw std::runtime_error("shm_open: locking the host named-object namespace failed, error=" + std::to_string(namespaceLock.Error())); + } + + std::filesystem::path objectPath; + try { + objectPath = WindowsNamedPath(hostName); + EnsurePrivateWindowsDirectory(objectPath.parent_path().parent_path()); + EnsurePrivateWindowsDirectory(objectPath.parent_path(), true); + CleanupUnlinkedWindowsObjects(objectPath, hostName); + } catch (const std::system_error& error) { + const auto nativeError = static_cast(error.code().value()); + if (const int guest = WindowsNamedError(nativeError)) return Fail(NamedGuestError(guest)); + throw std::runtime_error("shm_open: preparing the host named-object namespace failed: " + std::string(error.what())); + } + + const bool readWrite = (flags & SCE_KERNEL_O_ACCMODE) == SCE_KERNEL_O_RDWR; + const DWORD access = GENERIC_READ | (readWrite ? GENERIC_WRITE : 0) | WRITE_DAC; + const bool create = (flags & SCE_KERNEL_O_CREAT) != 0; + const DWORD creationAccess = access | (create ? WRITE_DAC | DELETE : 0); + ::SetLastError(ERROR_SUCCESS); + HANDLE handle = ::CreateFileW(objectPath.c_str(), creationAccess, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, + nullptr, create ? CREATE_NEW : OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OPEN_REPARSE_POINT, nullptr); + DWORD createResult = ::GetLastError(); + bool created = handle != INVALID_HANDLE_VALUE && create; + if (handle == INVALID_HANDLE_VALUE && create && createResult == ERROR_FILE_EXISTS && !(flags & SCE_KERNEL_O_EXCL)) { + handle = ::CreateFileW(objectPath.c_str(), access, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, + nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OPEN_REPARSE_POINT, nullptr); + createResult = ::GetLastError(); + } + if (handle == INVALID_HANDLE_VALUE) { + const DWORD nativeError = createResult; + if (const int guest = WindowsNamedError(nativeError)) return Fail(NamedGuestError(guest)); + throw std::runtime_error("shm_open: opening the host named object failed, error=" + std::to_string(nativeError)); + } + + BY_HANDLE_FILE_INFORMATION status{}; + if (!::GetFileInformationByHandle(handle, &status) || (status.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) != 0) { + const DWORD lastError = ::GetLastError(); + const DWORD nativeError = lastError == ERROR_SUCCESS ? ERROR_INVALID_NAME : lastError; + if (created) { + FILE_DISPOSITION_INFO deletion{TRUE}; + ::SetFileInformationByHandle(handle, FileDispositionInfo, &deletion, sizeof(deletion)); + } + ::CloseHandle(handle); + if (const int guest = WindowsNamedError(nativeError)) return Fail(NamedGuestError(guest)); + throw std::runtime_error("shm_open: refusing a reparse-point named object, error=" + std::to_string(nativeError)); + } + + if (created) { + const int permissions = (mode & 0777) & ~File::GetCreationMask_nid_no_patch(); + const DWORD nativeError = SetWindowsNamedPermissions(handle, permissions); + if (nativeError != ERROR_SUCCESS) { + FILE_DISPOSITION_INFO deletion{TRUE}; + ::SetFileInformationByHandle(handle, FileDispositionInfo, &deletion, sizeof(deletion)); + ::CloseHandle(handle); + if (const int guest = WindowsNamedError(nativeError)) return Fail(NamedGuestError(guest)); + throw std::runtime_error("shm_open: setting guest creation permissions failed, error=" + std::to_string(nativeError)); + } + } + + if ((flags & SCE_KERNEL_O_TRUNC) && readWrite) { + WindowsNamedLock objectLock(handle); + if (objectLock.Error() != ERROR_SUCCESS) { + const DWORD nativeError = objectLock.Error(); + ::CloseHandle(handle); + if (const int guest = WindowsNamedError(nativeError)) return Fail(NamedGuestError(guest)); + throw std::runtime_error("shm_open: locking the host named object for truncation failed, error=" + std::to_string(nativeError)); + } + LARGE_INTEGER zero{}; + if (!::SetFilePointerEx(handle, zero, nullptr, FILE_BEGIN) || !::SetEndOfFile(handle)) { + const DWORD nativeError = ::GetLastError(); + ::CloseHandle(handle); + if (const int guest = WindowsNamedError(nativeError)) return Fail(NamedGuestError(guest)); + throw std::runtime_error("shm_open: truncating the host named object failed, error=" + std::to_string(nativeError)); + } + } + + const int nativeFlags = _O_BINARY | (readWrite ? _O_RDWR : _O_RDONLY); + const int descriptor = ::_open_osfhandle(reinterpret_cast(handle), nativeFlags); + if (descriptor >= 0) { + File::TrackSharedMemoryDescriptor_nid_no_patch(descriptor, readWrite); + errno = savedError; + return descriptor; + } + const int nativeError = errno; + ::CloseHandle(handle); + if (const int guest = GuestError(nativeError)) return Fail(guest); + throw std::runtime_error("shm_open: converting the host named object to a descriptor failed, errno=" + std::to_string(nativeError)); +} + +int UnlinkNamedFile(const char* path) { + const int savedError = errno; + int validationError = 0; + const auto hostName = NamedHostName(path, validationError); + if (validationError != 0) return Fail(NamedGuestError(validationError) ? NamedGuestError(validationError) : GuestInvalid); + WindowsNamedLock namespaceLock(hostName); + if (namespaceLock.Error() != ERROR_SUCCESS) { + if (const int guest = WindowsNamedError(namespaceLock.Error())) return Fail(NamedGuestError(guest)); + throw std::runtime_error("shm_unlink: locking the host named-object namespace failed, error=" + std::to_string(namespaceLock.Error())); + } + + std::filesystem::path objectPath; + try { + objectPath = WindowsNamedPath(hostName); + } catch (const std::filesystem::filesystem_error& error) { + const auto nativeError = static_cast(error.code().value()); + if (const int guest = WindowsNamedError(nativeError)) return Fail(NamedGuestError(guest)); + throw std::runtime_error("shm_unlink: locating the host named object failed: " + std::string(error.what())); + } + + HANDLE objectHandle = ::CreateFileW(objectPath.c_str(), FILE_READ_ATTRIBUTES | GENERIC_WRITE, + FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nullptr, OPEN_EXISTING, + FILE_FLAG_OPEN_REPARSE_POINT, nullptr); + if (objectHandle == INVALID_HANDLE_VALUE) { + const DWORD nativeError = ::GetLastError(); + if (const int guest = WindowsNamedError(nativeError)) return Fail(NamedGuestError(guest)); + throw std::runtime_error("shm_unlink: opening the host named object failed, error=" + std::to_string(nativeError)); + } + BY_HANDLE_FILE_INFORMATION status{}; + if (!::GetFileInformationByHandle(objectHandle, &status) || + (status.dwFileAttributes & (FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_REPARSE_POINT)) != 0) { + const DWORD lastError = ::GetLastError(); + const DWORD nativeError = lastError == ERROR_SUCCESS ? ERROR_INVALID_NAME : lastError; + ::CloseHandle(objectHandle); + if (const int guest = WindowsNamedError(nativeError)) return Fail(NamedGuestError(guest)); + throw std::runtime_error("shm_unlink: refusing an invalid host named object, error=" + std::to_string(nativeError)); + } + + static std::atomic sequence{0}; + for (unsigned attempt = 0; attempt < 64; ++attempt) { + const auto orphanPath = objectPath.parent_path() / + (objectPath.stem().wstring() + L"-unlinked-" + std::to_wstring(::GetCurrentProcessId()) + + L"-" + std::to_wstring(sequence.fetch_add(1, std::memory_order_relaxed)) + objectPath.extension().wstring()); + if (!::MoveFileExW(objectPath.c_str(), orphanPath.c_str(), MOVEFILE_WRITE_THROUGH)) { + const DWORD nativeError = ::GetLastError(); + if (nativeError == ERROR_ALREADY_EXISTS || nativeError == ERROR_FILE_EXISTS) continue; + ::CloseHandle(objectHandle); + if (const int guest = WindowsNamedError(nativeError)) return Fail(NamedGuestError(guest)); + throw std::runtime_error("shm_unlink: detaching the host named object failed, error=" + std::to_string(nativeError)); + } + ::CloseHandle(objectHandle); + ReapUnlinkedWindowsObject(orphanPath); + errno = savedError; + return 0; + } + ::CloseHandle(objectHandle); + return Fail(NamedGuestError(EEXIST)); +} + +int SetSharedMemoryModeInternal(int descriptor, int mode) { + if (!IsHostSharedMemoryDescriptor(descriptor)) return -2; + const auto rawHandle = ::_get_osfhandle(descriptor); + if (rawHandle == -1) { errno = EBADF; return -1; } + const auto handle = reinterpret_cast(rawHandle); + WindowsNamedLock objectLock(handle); + if (objectLock.Error() != ERROR_SUCCESS) { + const int error = WindowsNamedError(objectLock.Error()); + errno = error ? error : EIO; + return -1; + } + const DWORD nativeError = SetWindowsNamedPermissions(handle, mode & 0777); + if (nativeError == ERROR_SUCCESS) return 0; + const int error = WindowsNamedError(nativeError); + errno = error ? error : EACCES; + return -1; +} + +std::filesystem::path SharedMemoryDescriptorPath(int descriptor) { + const auto rawHandle = ::_get_osfhandle(descriptor); + std::filesystem::path finalPath; + if (rawHandle != -1) { + std::vector path(32768); + const DWORD length = ::GetFinalPathNameByHandleW(reinterpret_cast(rawHandle), path.data(), + static_cast(path.size()), FILE_NAME_NORMALIZED | VOLUME_NAME_DOS); + if (length > 4 && length < path.size()) { + std::wstring value(path.data(), length); + finalPath = std::filesystem::path(std::move(value)); + } + } + return finalPath; +} + +void ReapSharedMemoryDescriptorPath(const std::filesystem::path& path) { + if (!path.empty() && path.filename().wstring().find(L"-unlinked-") != std::wstring::npos) + ReapUnlinkedWindowsObject(path); +} + +int CloseSharedMemoryDescriptorInternal(int descriptor) { + if (!IsHostSharedMemoryDescriptor(descriptor)) return -2; + const int savedError = errno; + const auto finalPath = SharedMemoryDescriptorPath(descriptor); + int result; + { + std::lock_guard lock(sharedDescriptorAccessMutex); + result = ::_close(descriptor); + if (result == 0) sharedDescriptorAccess.erase(descriptor); + } + if (result == 0) ReapSharedMemoryDescriptorPath(finalPath); + if (result == 0) errno = savedError; + return result; +} + +int ReplaceSharedMemoryDescriptorInternal(int source, int destination) { + if (!IsHostSharedMemoryDescriptor(destination)) return -2; + const int savedError = errno; + const auto finalPath = SharedMemoryDescriptorPath(destination); + const auto previous = _set_thread_local_invalid_parameter_handler(IgnoreDescriptorParameter); + const int result = ::_dup2(source, destination); + _set_thread_local_invalid_parameter_handler(previous); + if (result == 0) { + ReapSharedMemoryDescriptorPath(finalPath); + errno = savedError; + } + return result; +} +#else int OpenNamedFile(const char* path, int flags, int mode) { int validationError = 0; - const auto hostName = NamedHostPath(path, validationError); + const auto hostName = NamedHostName(path, validationError); if (validationError != 0) return Fail(NamedGuestError(validationError) ? NamedGuestError(validationError) : GuestInvalid); HostShmLock namespaceLock; if (namespaceLock.Error() != 0) { @@ -222,6 +670,44 @@ bool IsSharedMemoryDescriptor_nid_no_patch(int descriptor) { return IsHostSharedMemoryDescriptor(descriptor); } +#ifdef _WIN32 +int SetSharedMemoryMode_nid_no_patch(int descriptor, int mode) { + return SetSharedMemoryModeInternal(descriptor, mode); +} + +int CloseSharedMemoryDescriptor_nid_no_patch(int descriptor) { + return CloseSharedMemoryDescriptorInternal(descriptor); +} + +int ReplaceSharedMemoryDescriptor_nid_no_patch(int source, int destination) { + return ReplaceSharedMemoryDescriptorInternal(source, destination); +} + +int SharedMemoryDescriptorWritable_nid_no_patch(int descriptor) { + if (!IsHostSharedMemoryDescriptor(descriptor)) return -2; + std::lock_guard lock(sharedDescriptorAccessMutex); + const auto found = sharedDescriptorAccess.find(descriptor); + return found != sharedDescriptorAccess.end() && found->second ? 1 : 0; +} + +void TrackSharedMemoryDescriptor_nid_no_patch(int descriptor, bool writable) { + std::lock_guard lock(sharedDescriptorAccessMutex); + sharedDescriptorAccess[descriptor] = writable; +} + +void ForgetSharedMemoryDescriptor_nid_no_patch(int descriptor) { + std::lock_guard lock(sharedDescriptorAccessMutex); + sharedDescriptorAccess.erase(descriptor); +} + +void DuplicateSharedMemoryDescriptor_nid_no_patch(int source, int destination) { + std::lock_guard lock(sharedDescriptorAccessMutex); + sharedDescriptorAccess.erase(destination); + const auto found = sharedDescriptorAccess.find(source); + if (found != sharedDescriptorAccess.end()) sharedDescriptorAccess[destination] = found->second; +} +#endif + namespace { class SharedMemoryLock { @@ -230,6 +716,18 @@ public: #ifdef __linux__ struct stat originalStatus{}; if (::fstat(value, &originalStatus) != 0) { shared = true; error = errno; return; } +#elif defined(_WIN32) + if (!IsHostSharedMemoryDescriptor(value)) return; + const auto originalValue = ::_get_osfhandle(value); + if (originalValue == -1) return; + BY_HANDLE_FILE_INFORMATION originalStatus{}; + if (!::GetFileInformationByHandle(reinterpret_cast(originalValue), &originalStatus)) { + shared = true; + error = EBADF; + return; + } + windowsObjectLock = std::make_unique(reinterpret_cast(originalValue)); + if (windowsObjectLock->Error() != ERROR_SUCCESS) { shared = true; error = WindowsNamedError(windowsObjectLock->Error()); if (!error) error = EIO; return; } #endif if (!IsHostSharedMemoryDescriptor(value)) { #ifdef __linux__ @@ -247,10 +745,20 @@ public: if (hostLock->Error() != 0) { error = hostLock->Error(); return; } descriptor = Duplicate(value); if (descriptor < 0) { error = errno; return; } +#ifdef _WIN32 + DuplicateSharedMemoryDescriptor_nid_no_patch(value, descriptor); +#endif if (!IsHostSharedMemoryDescriptor(descriptor)) { error = EBADF; return; } if (!Refresh()) { error = errno; return; } #ifdef __linux__ if (status.st_dev != originalStatus.st_dev || status.st_ino != originalStatus.st_ino) error = EBADF; +#elif defined(_WIN32) + BY_HANDLE_FILE_INFORMATION duplicatedStatus{}; + const auto duplicatedValue = ::_get_osfhandle(descriptor); + if (duplicatedValue == -1 || !::GetFileInformationByHandle(reinterpret_cast(duplicatedValue), &duplicatedStatus) || + duplicatedStatus.dwVolumeSerialNumber != originalStatus.dwVolumeSerialNumber || + duplicatedStatus.nFileIndexHigh != originalStatus.nFileIndexHigh || + duplicatedStatus.nFileIndexLow != originalStatus.nFileIndexLow) error = EBADF; #endif } @@ -258,7 +766,7 @@ public: if (descriptor >= 0) { const int savedError = errno; #ifdef _WIN32 - ::_close(descriptor); + CloseSharedMemoryDescriptor_nid_no_patch(descriptor); #else ::close(descriptor); #endif @@ -292,6 +800,9 @@ private: int descriptor = -1; std::optional hostLock; +#ifdef _WIN32 + std::unique_ptr windowsObjectLock; +#endif bool shared = false; int error = 0; }; @@ -303,7 +814,9 @@ std::int64_t WriteSharedMemory_nid_no_patch(int descriptor, const void* buffer, if (guard.Error() != 0) { errno = guard.Error(); return -1; } if (!guard.Refresh()) return -1; const auto fd = guard.Descriptor(); -#ifndef _WIN32 +#ifdef _WIN32 + if (SharedMemoryDescriptorWritable_nid_no_patch(fd) == 0) { errno = EBADF; return -1; } +#else const int access = ::fcntl(fd, F_GETFL); if (access < 0) return -1; if ((access & O_ACCMODE) == O_RDONLY) { errno = EBADF; return -1; } @@ -331,7 +844,9 @@ std::int64_t PwriteSharedMemory_nid_no_patch(int descriptor, const void* buffer, if (guard.Error() != 0) { errno = guard.Error(); return -1; } if (!guard.Refresh()) return -1; const auto fd = guard.Descriptor(); -#ifndef _WIN32 +#ifdef _WIN32 + if (SharedMemoryDescriptorWritable_nid_no_patch(fd) == 0) { errno = EBADF; return -1; } +#else const int access = ::fcntl(fd, F_GETFL); if (access < 0) return -1; if ((access & O_ACCMODE) == O_RDONLY) { errno = EBADF; return -1; } @@ -341,7 +856,19 @@ std::int64_t PwriteSharedMemory_nid_no_patch(int descriptor, const void* buffer, if (offset >= status.st_size) return 0; const auto count = std::min(size, static_cast(status.st_size - offset)); #ifdef _WIN32 - const auto result = NativePositioned_nid_no_patch(fd, const_cast(buffer), static_cast(count), offset, true); + const auto handle = reinterpret_cast(::_get_osfhandle(fd)); + if (handle == INVALID_HANDLE_VALUE || ::GetFileType(handle) != FILE_TYPE_DISK) { errno = EBADF; return -1; } + LARGE_INTEGER previous{}; + if (!::SetFilePointerEx(handle, LARGE_INTEGER{}, &previous, FILE_CURRENT)) { errno = EIO; return -1; } + OVERLAPPED overlapped{}; + overlapped.Offset = static_cast(offset); + overlapped.OffsetHigh = static_cast(static_cast(offset) >> 32u); + DWORD written = 0; + const BOOL succeeded = ::WriteFile(handle, buffer, static_cast(std::min(count, std::numeric_limits::max())), &written, &overlapped); + const DWORD writeError = succeeded ? ERROR_SUCCESS : ::GetLastError(); + if (!::SetFilePointerEx(handle, previous, nullptr, FILE_BEGIN)) throw std::runtime_error("pwrite: restoring the host file position failed"); + if (!succeeded) { errno = writeError == ERROR_ACCESS_DENIED ? EBADF : EIO; return -1; } + const auto result = written; #else const auto result = ::pwrite(fd, buffer, static_cast(count), static_cast(offset)); #endif @@ -436,9 +963,19 @@ std::int64_t PwritevSharedMemory_nid_no_patch(int descriptor, const struct iovec #endif int TruncateSharedMemory_nid_no_patch(int descriptor, std::int64_t length) { +#ifdef _WIN32 + const auto positioned = LockPositionedTransfer(descriptor); +#endif SharedMemoryLock guard(descriptor); if (!guard.IsShared()) return -2; - if (guard.Error() != 0) { errno = guard.Error(); return -1; } + if (guard.Error() != 0) { + errno = guard.Error(); +#ifdef _WIN32 + return errno; +#else + return -1; +#endif + } const auto fd = guard.Descriptor(); #ifdef _WIN32 const int result = static_cast(::_chsize_s(fd, length)); @@ -456,7 +993,7 @@ extern "C" int APS5_VABI shm_open_nid_postfix(const char* path, int flags, int m if ((flags & ~AcceptedFlags) != 0) return Fail(GuestInvalid); if (path != anonymousObject) { #ifdef _WIN32 - NotImplemented_nid_no_patch("shm_open of a named object"); + return OpenNamedFile(path, flags, mode); #else return OpenNamedFile(path, flags, mode); #endif @@ -471,12 +1008,10 @@ extern "C" int APS5_VABI shm_open_nid_postfix(const char* path, int flags, int m extern "C" int APS5_VABI shm_unlink_nid_postfix(const char* path) { #ifdef _WIN32 - (void)path; - NotImplemented_nid_no_patch("shm_unlink of a named object"); - return 0; + return UnlinkNamedFile(path); #else int validationError = 0; - const auto hostName = NamedHostPath(path, validationError); + const auto hostName = NamedHostName(path, validationError); if (validationError != 0) return Fail(NamedGuestError(validationError) ? NamedGuestError(validationError) : GuestInvalid); HostShmLock namespaceLock; if (namespaceLock.Error() != 0) { diff --git a/core/libs/prx/libkernel/File/src/Stdio.cpp b/core/libs/prx/libkernel/File/src/Stdio.cpp index fe106fe2a1..61d1e37967 100644 --- a/core/libs/prx/libkernel/File/src/Stdio.cpp +++ b/core/libs/prx/libkernel/File/src/Stdio.cpp @@ -734,16 +734,18 @@ int APS5_VABI close_nid_postfix(int d) { return -1; } if (GuestSockets::IsOpen(d)) return GuestSockets::Close(d); + EqueueDescriptorClosed(d); #ifdef _WIN32 const auto positioned = File::LockPositionedTransfer(d); #endif - EqueueDescriptorClosed(d); File::ForgetRandomDevice(d); File::ForgetDescriptor(d); ForgetJitSharedMemory(d); #ifdef _WIN32 File::ForgetDirectoryDescriptor(d); File::ForgetFileLock(d); + const int shared = File::CloseSharedMemoryDescriptor_nid_no_patch(d); + if (shared != -2) return shared; return WithoutParameterHandler([d] { return ::_close(d); }); #else return ::close(d); @@ -762,26 +764,9 @@ std::atomic g_hostStandardOutput{-1}; void PreserveHostStream(int standard, std::atomic& slot) { if (slot.load() >= 0) return; -#ifdef _WIN32 - const int saved = WithoutParameterHandler([standard] { return ::_dup(standard); }); -#else const int saved = standard == 1 ? KeepHostStandardOutput_nid_no_patch() : KeepHostStandardError_nid_no_patch(); -#endif if (saved < 0) throw std::runtime_error(standard == 1 ? "dup2: cannot keep the host standard output" : "dup2: cannot keep the host standard error"); - int expected = -1; - if (!slot.compare_exchange_strong(expected, saved)) { -#ifdef _WIN32 - _close(saved); -#endif - return; - } -#ifdef _WIN32 - if (!AdoptHostStandardStream_nid_no_patch(standard, saved)) { - slot.store(-1); - _close(saved); - throw std::runtime_error(standard == 1 ? "dup2: cannot keep the host standard output" : "dup2: cannot keep the host standard error"); - } -#endif + slot.store(saved); } void PreserveHostStandardError() { PreserveHostStream(2, g_hostStandardError); } @@ -801,6 +786,7 @@ int APS5_VABI dup_nid_postfix(int d) { #ifdef _WIN32 const int duplicate = WithoutParameterHandler([d] { return ::_dup(d); }); if (duplicate >= 0) File::DuplicateDirectoryDescriptor(d, duplicate); + if (duplicate >= 0) File::DuplicateSharedMemoryDescriptor_nid_no_patch(d, duplicate); #else const int duplicate = ::dup(d); #endif @@ -833,8 +819,10 @@ int APS5_VABI dup2_nid_postfix(int from, int to) { if (from == to) return to; if (to == 1) PreserveHostStandardOutput(); if (to == 2) PreserveHostStandardError(); + auto positioned = File::LockPositionedTransfer(to); auto* const standard = HostStandardHandle_nid_no_patch(to); - const int duplicated = WithoutParameterHandler([from, to] { return ::_dup2(from, to); }); + int duplicated = File::ReplaceSharedMemoryDescriptor_nid_no_patch(from, to); + if (duplicated == -2) duplicated = WithoutParameterHandler([from, to] { return ::_dup2(from, to); }); KeepStandardHandle_nid_no_patch(to, standard); if (duplicated != 0) return PosixFailure(SceErrorFromErrno(errno) & 0xffff); EqueueDescriptorClosed(to); @@ -843,7 +831,10 @@ int APS5_VABI dup2_nid_postfix(int from, int to) { File::DuplicateDirectoryDescriptor(from, to); File::ForgetFileLock(to); File::ForgetRandomDevice(to); + File::ForgetSharedMemoryDescriptor_nid_no_patch(to); + File::DuplicateSharedMemoryDescriptor_nid_no_patch(from, to); if (sourceIsRandom) File::RememberRandomDevice(to); + positioned.unlock(); File::ShareDescriptor(from, to); DuplicateJitSharedMemory(from, to); return to; @@ -1562,6 +1553,10 @@ int APS5_VABI sceKernelChmod_nid_postfix(const char* path, std::uint16_t mode) { int APS5_VABI sceKernelFchmod(int d, std::uint16_t mode) { if (GuestSockets::IsOpen(d)) return SceErrorFromErrno(GUEST_EINVAL); File::RefuseArchivedFile(d, "fchmod"); +#ifdef _WIN32 + const int sharedMemoryResult = File::SetSharedMemoryMode_nid_no_patch(d, mode); + if (sharedMemoryResult != -2) return sharedMemoryResult == 0 ? 0 : SceErrorFromErrno(errno); +#endif if (NativeFchmod(d, mode & 07777) != 0) return SceErrorFromErrno(errno); return 0; } diff --git a/core/libs/prx/libkernel/Module/src/DynamicLoader.cpp b/core/libs/prx/libkernel/Module/src/DynamicLoader.cpp index 29765cb8e6..64c1b9b553 100644 --- a/core/libs/prx/libkernel/Module/src/DynamicLoader.cpp +++ b/core/libs/prx/libkernel/Module/src/DynamicLoader.cpp @@ -24,6 +24,9 @@ #else #include #include +#ifdef __linux__ +#include +#endif #endif #ifdef _WIN32 @@ -137,6 +140,56 @@ char* APS5_VABI dlerror_nid_postfix() { pendingError = false; return loaderError.data(); } +#ifdef __linux__ +static void InitializeLinuxGuestModule(void* native) { + auto* table = static_cast(::dlsym(native, "__aps5_guest_lifecycle_nid_no_patch")); + if (!table) return; + link_map* requested = nullptr; + link_map* owner = nullptr; + Dl_info info{}; + if (::dlinfo(native, RTLD_DI_LINKMAP, &requested) != 0 || requested == nullptr || + !::dladdr1(table, &info, reinterpret_cast(&owner), RTLD_DL_LINKMAP) || owner == nullptr) + throw std::runtime_error("dlopen: cannot identify guest lifecycle metadata owner"); + if (owner != requested) return; + if (table[0] != 0x415053354c494658ull) throw std::runtime_error("dlopen: unsupported guest lifecycle metadata"); + using Entry = int (APS5_VABI *)(std::size_t, const void*, void*); + using Initializer = void (APS5_VABI *)(int, char**, char**); + static std::mutex initializedLock; + static std::condition_variable initializedChanged; + const auto thread = static_cast(::gettid()); + std::unique_lock lock(initializedLock); + while (table[6] == 1) { + if (table[7] == thread) return; + initializedChanged.wait(lock); + } + if (table[6] == 2) return; + if (table[6] != 0) throw std::runtime_error("dlopen: deferred guest initialization failed or finalized"); + table[6] = 1; + table[7] = thread; + lock.unlock(); + try { + const auto pending = *static_cast(__aps5_get_pending_module_args_nid_no_patch()); + const int result = table[2] == 0 ? 0 : reinterpret_cast(table[2])(pending.args, pending.argp, nullptr); + for (std::uint64_t index = 0; index < table[4]; ++index) { + const auto initializer = *reinterpret_cast(table[8 + index]); + if (initializer) initializer(0, nullptr, nullptr); + } + __aps5_set_module_init_result_nid_no_patch(result); + lock.lock(); + table[6] = 2; + table[7] = 0; + lock.unlock(); + initializedChanged.notify_all(); + } catch (...) { + lock.lock(); + table[6] = 3; + table[7] = 0; + lock.unlock(); + initializedChanged.notify_all(); + throw; + } +} +#endif #ifdef _WIN32 static void InitializeDeferredModule(HMODULE native) { using Entry = int (APS5_VABI *)(std::size_t, const void*, void*); @@ -228,6 +281,9 @@ void* APS5_VABI dlopen_nid_postfix(const char* path, int flags) { if (path) LoadFailure(source, resolved, ::dlerror()); else Error(::dlerror()); return nullptr; } +#ifdef __linux__ + if (path) InitializeLinuxGuestModule(module->native); +#endif #endif std::lock_guard lock(modulesMutex); const auto handle = nextHandle++; diff --git a/core/libs/prx/libkernel/Module/src/Module.cpp b/core/libs/prx/libkernel/Module/src/Module.cpp index 9e71a3183c..44877cf635 100644 --- a/core/libs/prx/libkernel/Module/src/Module.cpp +++ b/core/libs/prx/libkernel/Module/src/Module.cpp @@ -142,7 +142,7 @@ bool IsRelinkedImage(std::uint64_t address) { void FillGuestUnwindInfo(std::uint64_t address, ModuleInfoForUnwind* info) { ModuleInfoEx module{}; module.st_size = sizeof(ModuleInfoEx); - if (sceKernelGetModuleInfoFromAddr(address, 2, &module) != 0) + if (sceKernelGetModuleInfoFromAddr(address, 1, &module) != 0) throw std::runtime_error("sceKernelGetModuleInfoForUnwind: failed to query guest module information"); info->eh_frame_hdr_addr = module.eh_frame_hdr_addr; info->eh_frame_addr = module.eh_frame_addr; diff --git a/core/libs/prx/libkernel/Module/src/ModuleInfo.cpp b/core/libs/prx/libkernel/Module/src/ModuleInfo.cpp index c240b13f9d..f3931ee478 100644 --- a/core/libs/prx/libkernel/Module/src/ModuleInfo.cpp +++ b/core/libs/prx/libkernel/Module/src/ModuleInfo.cpp @@ -394,22 +394,32 @@ int FillModuleList(KernelModule* handles, std::size_t count, std::size_t* actual extern "C" { int APS5_VABI sceKernelGetModuleInfoFromAddr(std::uint64_t address, int flags, ModuleInfoEx* info) { + if (flags != 1 && flags != 2) { + if (!info) throw std::invalid_argument("sceKernelGetModuleInfoFromAddr: null info with flags " + std::to_string(flags)); + *info = {}; + return SCE_KERNEL_ERROR_EINVAL; + } if (!info) return SCE_KERNEL_ERROR_EFAULT; - if (flags != 2) throw std::invalid_argument("sceKernelGetModuleInfoFromAddr: unsupported flags " + std::to_string(flags)); ModuleInfoEx result{}; - result.st_size = sizeof(ModuleInfoEx); #ifdef _WIN32 - if (!Fill(static_cast(address), result)) return SCE_KERNEL_ERROR_ESRCH; + MEMORY_BASIC_INFORMATION memory{}; + const bool found = Fill(static_cast(address), result) && + VirtualQuery(reinterpret_cast(address), &memory, sizeof(memory)) == sizeof(memory); + const GuestImage image{nullptr, found ? reinterpret_cast(memory.AllocationBase) : 0}; #else Dl_info symbol{}; link_map* native = nullptr; - if (!dladdr1(reinterpret_cast(address), &symbol, reinterpret_cast(&native), RTLD_DL_LINKMAP) || !native) - return SCE_KERNEL_ERROR_ESRCH; ImageSearch search{static_cast(address), &result, false}; - dl_iterate_phdr(FindImage, &search); - if (!search.found) return SCE_KERNEL_ERROR_ESRCH; - result.id = ModuleIdForImage_nid_no_patch(native); + if (dladdr1(reinterpret_cast(address), &symbol, reinterpret_cast(&native), RTLD_DL_LINKMAP) && native) + dl_iterate_phdr(FindImage, &search); + const bool found = search.found; + if (found) result.id = ModuleIdForImage_nid_no_patch(native); + const GuestImage image{native, 0}; #endif + if (!found || (flags == 2 && SystemImage(image))) { + *info = {}; + return SCE_KERNEL_ERROR_ESRCH; + } *info = result; return 0; } diff --git a/core/libs/prx/libkernel/Pthread/Posix/Common.hpp b/core/libs/prx/libkernel/Pthread/Posix/Common.hpp index b89859ae7e..334842c761 100644 --- a/core/libs/prx/libkernel/Pthread/Posix/Common.hpp +++ b/core/libs/prx/libkernel/Pthread/Posix/Common.hpp @@ -20,6 +20,10 @@ inline int ToErrno(int sceResult) { return sceResult == 0 ? 0 : static_cast(static_cast(sceResult) & 0xFFFFu); } +inline int ToSce(int error) { + return error == 0 ? 0 : static_cast(0x80020000u | static_cast(error)); +} + // Converts an absolute deadline on the guest clock into a relative wait, clamped to what the sce layer accepts. inline bool RelativeMicroseconds(int clockId, const KernelTimespec* abstime, KernelUseconds* usec) { if (!abstime || abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000) return false; diff --git a/core/libs/prx/libkernel/Pthread/Posix/Cond.cpp b/core/libs/prx/libkernel/Pthread/Posix/Cond.cpp index f357cac53a..324ee35216 100644 --- a/core/libs/prx/libkernel/Pthread/Posix/Cond.cpp +++ b/core/libs/prx/libkernel/Pthread/Posix/Cond.cpp @@ -80,4 +80,16 @@ int APS5_VABI pthread_condattr_getpshared_nid_postfix(const PthreadCondattr* att return 0; } +int APS5_VABI pthread_cond_signalto_np_nid_postfix(PthreadCond* cond, Pthread thread) { + return toPosix(scePthreadCondSignalto(cond, thread)); +} + +int APS5_VABI scePthreadCondattrSetpshared(PthreadCondattr* attr, int pshared) { + return PosixThread::ToSce(pthread_condattr_setpshared_nid_postfix(attr, pshared)); +} + +int APS5_VABI scePthreadCondattrGetpshared(const PthreadCondattr* attr, int* pshared) { + return PosixThread::ToSce(pthread_condattr_getpshared_nid_postfix(attr, pshared)); +} + } diff --git a/core/libs/prx/libkernel/Pthread/Posix/Mutex.cpp b/core/libs/prx/libkernel/Pthread/Posix/Mutex.cpp index 346b447bb8..fbf631b696 100644 --- a/core/libs/prx/libkernel/Pthread/Posix/Mutex.cpp +++ b/core/libs/prx/libkernel/Pthread/Posix/Mutex.cpp @@ -39,6 +39,7 @@ void _initializeStatic(PthreadMutex* mutex, const char* funcName) { extern "C" { int APS5_VABI sched_get_priority_max_nid_postfix(int policy); +int* APS5_VABI __error_nid_postfix(); int APS5_VABI pthread_mutex_destroy_nid_postfix(PthreadMutex* mutex) { if (!mutex) throw std::runtime_error("pthread_mutex_destroy: null mutex"); @@ -169,4 +170,51 @@ int APS5_VABI pthread_mutexattr_gettype_nid_postfix(const PthreadMutexattr* attr return 0; } +int APS5_VABI pthread_mutexattr_setkind_np_nid_postfix(PthreadMutexattr* attr, int kind) { + if (!attr || !*attr) { + *__error_nid_postfix() = POSIX_EINVAL; + return -1; + } + if (kind < static_cast(MutexType::ErrorCheck) || kind > static_cast(MutexType::Adaptive)) + throw std::runtime_error("pthread_mutexattr_setkind_np: kind " + std::to_string(kind) + " is not a mutex type, and pthread_mutex_init does not reject it with EINVAL"); + (*attr)->type = static_cast(kind); + return 0; +} + +int APS5_VABI pthread_mutexattr_getkind_np_nid_postfix(PthreadMutexattr attr) { + if (!attr) { + *__error_nid_postfix() = POSIX_EINVAL; + return -1; + } + return static_cast(attr->type); +} + +int APS5_VABI scePthreadMutexattrGettype(const PthreadMutexattr* attr, int* type) { + return PosixThread::ToSce(pthread_mutexattr_gettype_nid_postfix(attr, type)); +} + +int APS5_VABI scePthreadMutexattrSetprotocol(PthreadMutexattr* attr, int protocol) { + return PosixThread::ToSce(pthread_mutexattr_setprotocol_nid_postfix(attr, protocol)); +} + +int APS5_VABI scePthreadMutexattrGetprotocol(const PthreadMutexattr* attr, int* protocol) { + return PosixThread::ToSce(pthread_mutexattr_getprotocol_nid_postfix(attr, protocol)); +} + +int APS5_VABI scePthreadMutexattrSetprioceiling(PthreadMutexattr* attr, int prioceiling) { + return PosixThread::ToSce(pthread_mutexattr_setprioceiling_nid_postfix(attr, prioceiling)); +} + +int APS5_VABI scePthreadMutexattrGetprioceiling(const PthreadMutexattr* attr, int* prioceiling) { + return PosixThread::ToSce(pthread_mutexattr_getprioceiling_nid_postfix(attr, prioceiling)); +} + +int APS5_VABI scePthreadMutexattrSetpshared(PthreadMutexattr* attr, int pshared) { + return PosixThread::ToSce(pthread_mutexattr_setpshared_nid_postfix(attr, pshared)); +} + +int APS5_VABI scePthreadMutexattrGetpshared(const PthreadMutexattr* attr, int* pshared) { + return PosixThread::ToSce(pthread_mutexattr_getpshared_nid_postfix(attr, pshared)); +} + } diff --git a/core/libs/prx/libkernel/Pthread/Posix/Rwlock.cpp b/core/libs/prx/libkernel/Pthread/Posix/Rwlock.cpp index 203af9f0f4..a15fe61fb0 100644 --- a/core/libs/prx/libkernel/Pthread/Posix/Rwlock.cpp +++ b/core/libs/prx/libkernel/Pthread/Posix/Rwlock.cpp @@ -70,6 +70,25 @@ int APS5_VABI pthread_rwlockattr_setpshared_nid_postfix(PthreadRwlockattr* attr, return 0; } +int APS5_VABI pthread_rwlockattr_gettype_np_nid_postfix(const PthreadRwlockattr* attr, int* type) { + if (!attr || !*attr) throw std::runtime_error("pthread_rwlockattr_gettype_np: null attr"); + if (!type) throw std::runtime_error("pthread_rwlockattr_gettype_np: null type"); + *type = (*attr)->type; + return 0; +} + +int APS5_VABI scePthreadRwlockattrGettype(const PthreadRwlockattr* attr, int* type) { + return PosixThread::ToSce(pthread_rwlockattr_gettype_np_nid_postfix(attr, type)); +} + +int APS5_VABI scePthreadRwlockattrSetpshared(PthreadRwlockattr* attr, int pshared) { + return PosixThread::ToSce(pthread_rwlockattr_setpshared_nid_postfix(attr, pshared)); +} + +int APS5_VABI scePthreadRwlockattrGetpshared(const PthreadRwlockattr* attr, int* pshared) { + return PosixThread::ToSce(pthread_rwlockattr_getpshared_nid_postfix(attr, pshared)); +} + int APS5_VABI pthread_rwlock_rdlock_nid_postfix(PthreadRwlock* rwlock) { initializeStatic(rwlock, __func__); return toPosix(scePthreadRwlockRdlock(rwlock)); diff --git a/core/libs/prx/libkernel/Pthread/Posix/Sem.cpp b/core/libs/prx/libkernel/Pthread/Posix/Sem.cpp index e780d7f59f..70faa0359a 100644 --- a/core/libs/prx/libkernel/Pthread/Posix/Sem.cpp +++ b/core/libs/prx/libkernel/Pthread/Posix/Sem.cpp @@ -7,8 +7,14 @@ #include "prx/libkernel/Time/include/TimedWait.hpp" #include #include +#include #include +#include +#include #include +#include +#include +#include // The guest sem_t is at least 8 bytes; its first word holds a pointer to the host semaphore. struct PosixSemaphore { @@ -17,10 +23,19 @@ struct PosixSemaphore { unsigned int count; }; +static constexpr int GUEST_ENOENT = 2; +static constexpr int GUEST_EEXIST = 17; static constexpr int GUEST_EAGAIN = 35; +static constexpr int GUEST_ENAMETOOLONG = 63; static constexpr int GUEST_EOVERFLOW = 84; static constexpr unsigned int GUEST_SEM_VALUE_MAX = 0x7fffffffu; static constexpr int GUEST_REALTIME_CLOCK = 0; +static constexpr int GUEST_O_CREAT = 0x200; +static constexpr int GUEST_O_EXCL = 0x800; +static constexpr std::size_t GUEST_PATH_MAX = 1024; +static constexpr std::size_t GUEST_NAME_MAX = 255; +static constexpr std::string_view NAMED_SEMAPHORE_PREFIX = "/tmp/SEMD"; +static constexpr std::string_view NAMED_SEMAPHORE_FILE_PREFIX = "SEMD"; static int Fail(int error) { errno = error; @@ -34,6 +49,49 @@ static PosixSemaphore* Get(void* sem) { return semaphore; } +struct NamedSemaphoreMapping { + PosixSemaphore* semaphore; + std::uint64_t reserved; +}; + +struct NamedSemaphore { + std::unique_ptr semaphore; + std::unique_ptr mapping; + int openCount = 0; +}; + +struct NamedSemaphores { + std::mutex lock; + std::map> linked; + std::map> open; +}; + +static NamedSemaphores& Named() { + static NamedSemaphores named; + return named; +} + +static bool IsNamed(const void* sem) { + auto& named = Named(); + std::lock_guard lock(named.lock); + return named.open.count(sem) != 0; +} + +static int NamedPathError(const char* function, const char* name, int notAbsolute) { + if (!name) throw std::invalid_argument(std::string(function) + ": null name"); + if (name[0] != '/') return notAbsolute; + if (NAMED_SEMAPHORE_PREFIX.size() + std::strlen(name + 1) >= GUEST_PATH_MAX) return GUEST_ENAMETOOLONG; + return 0; +} + +static int NamedFileError(const char* function, const char* name) { + const std::string_view file(name + 1); + if (file.find('/') != std::string_view::npos) + throw std::runtime_error(std::string(function) + ": the name " + name + " would resolve below /tmp, which is not modelled"); + if (NAMED_SEMAPHORE_FILE_PREFIX.size() + file.size() > GUEST_NAME_MAX) return GUEST_ENAMETOOLONG; + return 0; +} + static int WaitFor(PosixSemaphore* semaphore, const KernelUseconds* usec) { ThreadCancel::Check(); std::unique_lock lock(semaphore->lock); @@ -60,7 +118,7 @@ int APS5_VABI sem_init_nid_postfix(void* sem, int pshared, unsigned int value) { int APS5_VABI sem_destroy_nid_postfix(void* sem) { auto* semaphore = Get(sem); - if (!semaphore) return Fail(PosixThread::GUEST_EINVAL); + if (!semaphore || IsNamed(sem)) return Fail(PosixThread::GUEST_EINVAL); delete semaphore; std::memset(sem, 0, sizeof(semaphore)); return 0; @@ -125,4 +183,86 @@ int APS5_VABI sem_getvalue_nid_postfix(void* sem, int* value) { return 0; } +void* APS5_VABI sem_open_nid_postfix(const char* name, int flags, ...) { + if (const int error = NamedPathError("sem_open", name, PosixThread::GUEST_EINVAL)) { + Fail(error); + return nullptr; + } + if ((flags & ~(GUEST_O_CREAT | GUEST_O_EXCL)) != 0) { + Fail(PosixThread::GUEST_EINVAL); + return nullptr; + } + unsigned int value = 0; + if ((flags & GUEST_O_CREAT) != 0) { +#ifdef _WIN32 + __builtin_sysv_va_list arguments; + __builtin_sysv_va_start(arguments, flags); + static_cast(__builtin_va_arg(arguments, int)); + value = static_cast(__builtin_va_arg(arguments, int)); + __builtin_sysv_va_end(arguments); +#else + std::va_list arguments; + va_start(arguments, flags); + static_cast(va_arg(arguments, int)); + value = static_cast(va_arg(arguments, int)); + va_end(arguments); +#endif + } + if (const int error = NamedFileError("sem_open", name)) { + Fail(error); + return nullptr; + } + auto& named = Named(); + std::lock_guard lock(named.lock); + const std::string file(name + 1); + std::shared_ptr object; + if (const auto found = named.linked.find(file); found != named.linked.end()) { + if ((flags & (GUEST_O_CREAT | GUEST_O_EXCL)) == (GUEST_O_CREAT | GUEST_O_EXCL)) { + Fail(GUEST_EEXIST); + return nullptr; + } + object = found->second; + } else { + if ((flags & GUEST_O_CREAT) == 0) { + Fail(GUEST_ENOENT); + return nullptr; + } + if (value > GUEST_SEM_VALUE_MAX) + throw std::runtime_error("sem_open: initial value " + std::to_string(value) + " is above SEM_VALUE_MAX, which FreeBSD stores unchecked"); + object = std::make_shared(); + object->semaphore = std::make_unique(); + object->semaphore->count = value; + named.linked.emplace(file, object); + } + if (!object->mapping) { + object->mapping = std::make_unique(NamedSemaphoreMapping{object->semaphore.get(), 0}); + named.open.emplace(object->mapping.get(), object); + } + ++object->openCount; + return object->mapping.get(); +} + +int APS5_VABI sem_close_nid_postfix(void* sem) { + if (!sem) throw std::invalid_argument("sem_close: null semaphore"); + auto& named = Named(); + std::lock_guard lock(named.lock); + const auto found = named.open.find(sem); + if (found == named.open.end()) return Fail(PosixThread::GUEST_EINVAL); + const auto object = found->second; + if (--object->openCount == 0) { + named.open.erase(found); + object->mapping.reset(); + } + return 0; +} + +int APS5_VABI sem_unlink_nid_postfix(const char* name) { + if (const int error = NamedPathError("sem_unlink", name, GUEST_ENOENT)) return Fail(error); + if (const int error = NamedFileError("sem_unlink", name)) return Fail(error); + auto& named = Named(); + std::lock_guard lock(named.lock); + if (named.linked.erase(std::string(name + 1)) == 0) return Fail(GUEST_ENOENT); + return 0; +} + } diff --git a/core/libs/prx/libkernel/Pthread/Posix/Thread.cpp b/core/libs/prx/libkernel/Pthread/Posix/Thread.cpp index 69198777f4..c8671c5c4f 100644 --- a/core/libs/prx/libkernel/Pthread/Posix/Thread.cpp +++ b/core/libs/prx/libkernel/Pthread/Posix/Thread.cpp @@ -28,6 +28,19 @@ int APS5_VABI scePthreadKeyCreate(PthreadKey* key, pthread_key_destructor_func_t int APS5_VABI scePthreadKeyDelete(PthreadKey key); void* APS5_VABI scePthreadGetspecific(PthreadKey key); int APS5_VABI scePthreadSetspecific(PthreadKey key, void* value); +int APS5_VABI scePthreadMutexattrInit(PthreadMutexattr* attr); +int APS5_VABI scePthreadMutexattrSettype(PthreadMutexattr* attr, int type); +int APS5_VABI scePthreadMutexattrDestroy(PthreadMutexattr* attr); +int APS5_VABI scePthreadMutexInit(PthreadMutex* mutex, const PthreadMutexattr* attr, const char* name); +int APS5_VABI scePthreadMutexDestroy(PthreadMutex* mutex); +int APS5_VABI scePthreadMutexLock(PthreadMutex* mutex); +int APS5_VABI scePthreadMutexTrylock(PthreadMutex* mutex); +int APS5_VABI scePthreadMutexUnlock(PthreadMutex* mutex); +int APS5_VABI scePthreadCondInit(PthreadCond* cond, const PthreadCondattr* attr, const char* name); +int APS5_VABI scePthreadCondDestroy(PthreadCond* cond); +int APS5_VABI scePthreadCondWait(PthreadCond* cond, PthreadMutex* mutex); +int APS5_VABI scePthreadCondSignal(PthreadCond* cond); +int APS5_VABI scePthreadCondBroadcast(PthreadCond* cond); int APS5_VABI scePthreadCancel(Pthread thread); int APS5_VABI scePthreadSetcancelstate(int state, int* old_state); int APS5_VABI scePthreadSetcanceltype(int type, int* old_type); @@ -132,26 +145,28 @@ struct GuestRtPrio { int APS5_VABI rtprio_thread_nid_postfix(int function, int lwpid, GuestRtPrio* priority) { constexpr int Lookup = 0, Set = 1; - constexpr int GuestEsrch = 3, GuestEfault = 14, GuestEinval = 22; - constexpr std::uint16_t Normal = 3; + constexpr int GuestEsrch = 3, GuestEfault = 14; + constexpr int GuestSchedFifo = 1, GuestSchedRr = 3; + constexpr std::uint16_t Realtime = 2, Normal = 3, Fifo = 10; const auto fail = [](int error) { *__error_nid_postfix() = error; return -1; }; - if (function != Lookup && function != Set) return fail(GuestEinval); + if (function != Lookup && function != Set) return fail(GuestEsrch); if (priority == nullptr) return fail(GuestEfault); auto* self = scePthreadSelf(); - if (lwpid != 0 && static_cast(lwpid) != static_cast(self->tid)) { - if (!LiveGuestThreadExists(lwpid)) return fail(GuestEsrch); - NotImplemented_nid_no_patch("rtprio_thread: a thread other than the calling one"); - } - const int policy = self->schedPolicy.load(std::memory_order_relaxed); - if (policy == 1 || policy == 3) NotImplemented_nid_no_patch("rtprio_thread: a guest thread's real-time class"); + int policy = self->schedPolicy.load(std::memory_order_relaxed); + int schedPriority = self->priority.load(std::memory_order_relaxed); + const bool other = lwpid != 0 && static_cast(lwpid) != static_cast(self->tid); + if (other && !GetLiveGuestThreadSched_nid_no_patch(lwpid, policy, schedPriority)) return fail(GuestEsrch); + const bool realTime = policy == GuestSchedFifo || policy == GuestSchedRr; if (function == Lookup) { - priority->type = Normal; - priority->prio = 0; + priority->type = realTime ? (policy == GuestSchedFifo ? Fifo : Realtime) : Normal; + priority->prio = realTime ? static_cast(schedPriority) : 0; return 0; } + if (other) NotImplemented_nid_no_patch("rtprio_thread: a thread other than the calling one"); + if (realTime) NotImplemented_nid_no_patch("rtprio_thread: a guest thread's real-time class"); if (priority->type == Normal && priority->prio == 0) return 0; NotImplemented_nid_no_patch("rtprio_thread: a real-time priority change"); } @@ -265,6 +280,25 @@ struct RegisterPthreadOnLoad { scePthreadExit, }; RegisterThreadForwards_nid_no_patch(&forwards); + static const PthreadSyncForwards_nid_no_patch sync{ + reinterpret_cast(scePthreadMutexattrInit), + reinterpret_cast(scePthreadMutexattrSettype), + reinterpret_cast(scePthreadMutexattrDestroy), + reinterpret_cast(scePthreadMutexInit), + reinterpret_cast(scePthreadMutexDestroy), + reinterpret_cast(scePthreadMutexLock), + reinterpret_cast(scePthreadMutexTrylock), + reinterpret_cast(scePthreadMutexUnlock), + reinterpret_cast(scePthreadCondInit), + reinterpret_cast(scePthreadCondDestroy), + reinterpret_cast(scePthreadCondWait), + reinterpret_cast(scePthreadCondSignal), + reinterpret_cast(scePthreadCondBroadcast), + reinterpret_cast(scePthreadDetach), + reinterpret_cast(scePthreadJoin), + scePthreadYield, + }; + RegisterPthreadSyncForwards_nid_no_patch(&sync); } }; const RegisterPthreadOnLoad gRegisterPthread; diff --git a/core/libs/prx/libkernel/Pthread/include/Pthread.hpp b/core/libs/prx/libkernel/Pthread/include/Pthread.hpp index c4442bd188..0e83376197 100644 --- a/core/libs/prx/libkernel/Pthread/include/Pthread.hpp +++ b/core/libs/prx/libkernel/Pthread/include/Pthread.hpp @@ -181,6 +181,7 @@ bool GuestThreadIsLive(const PthreadPrivate* thread); std::unique_lock LockLiveGuestThread(const PthreadPrivate* thread); bool LiveGuestThreadExists(std::int64_t tid); bool GetLiveGuestThreadAffinity_nid_no_patch(std::int64_t tid, KernelCpumask& mask); +bool GetLiveGuestThreadSched_nid_no_patch(std::int64_t tid, int& policy, int& priority); bool SetLiveGuestThreadAffinity_nid_no_patch(std::int64_t tid, KernelCpumask mask); extern "C" std::size_t GetDefaultStackSize_nid_no_patch(); extern "C" bool SetDefaultStackSize_nid_no_patch(std::size_t size); diff --git a/core/libs/prx/libkernel/Pthread/src/Mutex.cpp b/core/libs/prx/libkernel/Pthread/src/Mutex.cpp index 04677c3ba8..6e8cc3df59 100644 --- a/core/libs/prx/libkernel/Pthread/src/Mutex.cpp +++ b/core/libs/prx/libkernel/Pthread/src/Mutex.cpp @@ -149,16 +149,6 @@ int APS5_VABI scePthreadMutexattrSettype(PthreadMutexattr* attr, int type) { return 0; } -int APS5_VABI scePthreadMutexattrSetprotocol(PthreadMutexattr* attr, int protocol) { - if (!attr || !*attr) - return SCE_KERNEL_ERROR_EINVAL; - constexpr int PrioNone = 0; - constexpr int PrioProtect = 2; - if (protocol < PrioNone || protocol > PrioProtect) - return SCE_KERNEL_ERROR_EINVAL; - return 0; -} - int APS5_VABI scePthreadMutexInit(PthreadMutex* mutex, const PthreadMutexattr* attr, const char*) { if (!mutex) throw std::invalid_argument("Mutex pointer is null"); diff --git a/core/libs/prx/libkernel/Pthread/src/Rwlock.cpp b/core/libs/prx/libkernel/Pthread/src/Rwlock.cpp index 35a043a8e9..36d7301077 100644 --- a/core/libs/prx/libkernel/Pthread/src/Rwlock.cpp +++ b/core/libs/prx/libkernel/Pthread/src/Rwlock.cpp @@ -14,6 +14,8 @@ static constexpr int SCE_KERNEL_ERROR_EDEADLK = 0x8002000B; static constexpr int SCE_KERNEL_ERROR_EBUSY = 0x80020010; static constexpr int SCE_KERNEL_ERROR_ETIMEDOUT = 0x8002003C; static constexpr int SCE_KERNEL_ERROR_EINVAL = 0x80020016; +static constexpr int FIRST_STORED_RWLOCK_TYPE = 1; +static constexpr int LAST_STORED_RWLOCK_TYPE = 2; static PthreadRwlockPrivate* const DestroyedRwlock = reinterpret_cast(std::uintptr_t{1}); @@ -174,7 +176,7 @@ int APS5_VABI scePthreadRwlockattrInit(PthreadRwlockattr* attr) { } int APS5_VABI scePthreadRwlockattrSettype(PthreadRwlockattr* attr, int type) { - if (!attr || !*attr) throw std::runtime_error("scePthreadRwlockattrSettype: null attr"); + if (!attr || !*attr || type < FIRST_STORED_RWLOCK_TYPE || type > LAST_STORED_RWLOCK_TYPE) return SCE_KERNEL_ERROR_EINVAL; (*attr)->type = type; return SCE_OK; } diff --git a/core/libs/prx/libkernel/Pthread/src/Thread.cpp b/core/libs/prx/libkernel/Pthread/src/Thread.cpp index 1d2fecb349..3268744271 100644 --- a/core/libs/prx/libkernel/Pthread/src/Thread.cpp +++ b/core/libs/prx/libkernel/Pthread/src/Thread.cpp @@ -35,6 +35,7 @@ #endif static constexpr int SCE_OK = 0; +static constexpr std::size_t GUEST_THREAD_NAME_CAPACITY = 32; static constexpr std::size_t HOST_STACK_MARGIN = 1u << 20; static constexpr int DETACH_DETACHED = 1; @@ -109,14 +110,103 @@ struct RetiredStack { int hostTid; }; +struct FinishedStack { + void* address; + std::size_t bytes; +}; + static std::mutex retiredStacksLock; static std::vector retiredStacks; +static std::vector spareStacks; +static std::size_t spareStackBytes = 0; + +static std::size_t SpareStackLimit() { + static const auto limit = [] { + const auto pages = sysconf(_SC_PHYS_PAGES); + const auto pageSize = sysconf(_SC_PAGESIZE); + if (pages <= 0 || pageSize <= 0) return std::size_t{4u << 20}; + const auto total = static_cast(pages) * static_cast(pageSize); + return std::clamp(static_cast(total / 1024), 4u << 20, 64u << 20); + }(); + return limit; +} + +static void ReleaseFinishedStack(const FinishedStack& stack) { + UnregisterGuestStackMapping(stack.address); + DoMunmap(stack.address, stack.bytes); +} + +static bool DiscardFinishedStackPages(const FinishedStack& stack) { + auto* usable = static_cast(stack.address) + GUEST_STACK_GUARD; + const auto bytes = stack.bytes - GUEST_STACK_GUARD; + return madvise(usable, bytes, MADV_DONTNEED) == 0; +} + +static void CollectFinishedStack(void* address, std::size_t bytes) { + UnregisterGuestStackMapping(address); + const FinishedStack finished{address, bytes}; + if (!DiscardFinishedStackPages(finished)) { + ReleaseFinishedStack(finished); + return; + } + std::vector release; + { + std::lock_guard lock(retiredStacksLock); + spareStacks.push_back({address, bytes}); + spareStackBytes += bytes; + const auto limit = SpareStackLimit(); + while (spareStackBytes > limit && !spareStacks.empty()) { + spareStackBytes -= spareStacks.front().bytes; + release.push_back(spareStacks.front()); + spareStacks.erase(spareStacks.begin()); + } + } + if (!release.empty()) { + static std::atomic_flag logged = ATOMIC_FLAG_INIT; + if (!logged.test_and_set(std::memory_order_relaxed)) + std::fprintf(stderr, "scePthread: spare stack cache reached its host-memory limit; releasing cached mappings\n"); + for (const auto& stack : release) ReleaseFinishedStack(stack); + } +} + +static bool TakeSpareStack(std::size_t bytes, FinishedStack* stack) { + std::lock_guard lock(retiredStacksLock); + const auto found = std::find_if(spareStacks.begin(), spareStacks.end(), [bytes](const FinishedStack& candidate) { return candidate.bytes == bytes; }); + if (found == spareStacks.end()) return false; + *stack = *found; + spareStackBytes -= found->bytes; + spareStacks.erase(found); + return true; +} + +static void ReleaseSpareStacks() { + std::vector release; + { + std::lock_guard lock(retiredStacksLock); + release.swap(spareStacks); + spareStackBytes = 0; + } + for (const auto& stack : release) ReleaseFinishedStack(stack); +} static void AllocateGuestStack(PthreadPrivate* thread, std::size_t usableBytes) { const std::size_t bytes = (usableBytes + PS5_PAGE_SIZE - 1) / PS5_PAGE_SIZE * PS5_PAGE_SIZE + GUEST_STACK_GUARD; + FinishedStack spare{}; + if (TakeSpareStack(bytes, &spare)) { + if (DoMprotect(spare.address, spare.bytes, 3) == 0 && DoMprotect(spare.address, GUEST_STACK_GUARD, 0) == 0) { + thread->guestStack = spare.address; + thread->guestStackBytes = spare.bytes; + return; + } + UnregisterGuestStackMapping(spare.address); + DoMunmap(spare.address, spare.bytes); + } void* stack = nullptr; - if (DoMapAnon(&stack, bytes, 3, 0) != 0) - throw std::runtime_error("scePthreadCreate: cannot map the guest thread stack"); + if (DoMapAnon(&stack, bytes, 3, 0) != 0) { + ReleaseSpareStacks(); + if (DoMapAnon(&stack, bytes, 3, 0) != 0) + throw std::runtime_error("scePthreadCreate: cannot map the guest thread stack"); + } if (DoMprotect(stack, GUEST_STACK_GUARD, 0) != 0) { DoMunmap(stack, bytes); throw std::runtime_error("scePthreadCreate: cannot protect the guest thread stack guard"); @@ -127,9 +217,7 @@ static void AllocateGuestStack(PthreadPrivate* thread, std::size_t usableBytes) static void FreeGuestStack(PthreadPrivate* thread) { if (thread->guestStack == nullptr) return; - void* const mapping = thread->guestStack; - UnregisterGuestStackMapping(mapping); - DoMunmap(thread->guestStack, thread->guestStackBytes); + CollectFinishedStack(thread->guestStack, thread->guestStackBytes); thread->guestStack = nullptr; thread->guestStackBytes = 0; } @@ -154,10 +242,7 @@ static void ReapRetiredStacks() { } } } - for (const auto& stack : ready) { - UnregisterGuestStackMapping(stack.address); - DoMunmap(stack.address, stack.bytes); - } + for (const auto& stack : ready) CollectFinishedStack(stack.address, stack.bytes); } static void RetireGuestStack(PthreadPrivate* thread) { @@ -464,6 +549,18 @@ bool GetLiveGuestThreadAffinity_nid_no_patch(std::int64_t tid, KernelCpumask& ma return false; } +bool GetLiveGuestThreadSched_nid_no_patch(std::int64_t tid, int& policy, int& priority) { + auto& registry = CpuClocks(); + std::lock_guard lock(registry.lock); + for (const auto& [id, thread] : registry.threads) { + if (thread->tid != tid || thread->_finished.load(std::memory_order_acquire)) continue; + policy = thread->schedPolicy.load(std::memory_order_relaxed); + priority = thread->priority.load(std::memory_order_relaxed); + return true; + } + return false; +} + bool SetLiveGuestThreadAffinity_nid_no_patch(std::int64_t tid, KernelCpumask mask) { auto& registry = CpuClocks(); std::lock_guard lock(registry.lock); @@ -820,6 +917,7 @@ extern "C" { int APS5_VABI scePthreadCreate(Pthread* thread, const PthreadAttr* attr, PthreadEntry entry, void* arg, const char* name) { if (!thread || !entry) throw std::runtime_error("scePthreadCreate: null arg"); + if (name && std::strlen(name) >= GUEST_THREAD_NAME_CAPACITY) return SCE_KERNEL_ERROR_ENAMETOOLONG; if (attr && !*attr) attr = nullptr; auto p = std::make_unique(attr ? *attr : nullptr); bool detached = false; @@ -988,7 +1086,8 @@ int APS5_VABI scePthreadDetach(Pthread thread) { if (!thread) return SCE_KERNEL_ERROR_EINVAL; { std::lock_guard lock(thread->_join_mtx); - if (thread->_detached || thread->_joining || thread->_joined.load(std::memory_order_acquire)) return SCE_KERNEL_ERROR_EINVAL; + if (thread->_joined.load(std::memory_order_acquire)) return SCE_KERNEL_ERROR_ESRCH; + if (thread->_detached || thread->_joining) return SCE_KERNEL_ERROR_EINVAL; thread->_detached = true; } #ifdef _WIN32 diff --git a/core/libs/prx/libkernel/Semaphore/src/Semaphore.cpp b/core/libs/prx/libkernel/Semaphore/src/Semaphore.cpp index eab1d9b52d..a1b4e1bd60 100644 --- a/core/libs/prx/libkernel/Semaphore/src/Semaphore.cpp +++ b/core/libs/prx/libkernel/Semaphore/src/Semaphore.cpp @@ -21,9 +21,12 @@ extern "C" { int APS5_VABI sceKernelCreateSema(KernelSema* sem, const char* name, uint32_t attr, int init, int max, void* opt) { (void)opt; - if (sem == nullptr || name == nullptr || attr > 2 || init < 0 || max <= 0 || init > max) { + if (sem == nullptr || attr > 2 || max < 0) { APS5_INVALID_ARG_EX; } + if (name == nullptr || init < 0 || max == 0 || init > max) { + return SCE_KERNEL_ERROR_EINVAL; + } *sem = new KernelSemaPrivate(init, max, std::string(name), attr == 1); if (TraceSync()) std::fprintf(stderr, "[sema] create %p '%s' attr=0x%x init=%d max=%d\n", static_cast(*sem), name, attr, init, max); @@ -34,8 +37,8 @@ int APS5_VABI sceKernelPollSema(KernelSema sem, int need) { if (sem == nullptr) { return SCE_KERNEL_ERROR_ESRCH; } - if (need <= 0) { - APS5_INVALID_ARG_EX; + if (need <= 0 || need > sem->maxCount) { + return SCE_KERNEL_ERROR_EINVAL; } std::lock_guard lock(sem->mutex); @@ -53,6 +56,9 @@ int APS5_VABI sceKernelSignalSema(KernelSema sem, int count) { if (count < 0) { APS5_INVALID_ARG_EX; } + if (count == 0) { + return SCE_KERNEL_ERROR_EINVAL; + } std::lock_guard lock(sem->mutex); if (count > sem->maxCount - sem->tokenCount) { diff --git a/core/libs/prx/libkernel/Socket/include/SocketOptions.hpp b/core/libs/prx/libkernel/Socket/include/SocketOptions.hpp index 56bbe00efe..18a966c5da 100644 --- a/core/libs/prx/libkernel/Socket/include/SocketOptions.hpp +++ b/core/libs/prx/libkernel/Socket/include/SocketOptions.hpp @@ -1,5 +1,6 @@ #pragma once #include +#include namespace GuestSockets { @@ -9,13 +10,30 @@ struct SocketOptionState { bool noSignal = false; std::int64_t sendTimeout = 0; std::int64_t receiveTimeout = 0; + int sendBuffer = 0; + int receiveBuffer = 0; + int sendLowWater = 0; + int receiveLowWater = 0; + int ipTimeToLive = 64; + int ipTypeOfService = 0; + int multicastTimeToLive = 1; + bool multicastLoop = true; + int tcpMaximumSegment = 536; + unsigned int tcpKeepIdle = 0; + unsigned int tcpKeepInterval = 0; + unsigned int tcpKeepCount = 0; + int keptFlags = 0; + std::uint32_t multicastInterface = 0; + std::vector memberships; }; bool NativeListening(std::uintptr_t native, bool& listening); -int SetSocketLevelOption(std::uintptr_t native, SocketOptionState& state, int option, const void* value, std::uint32_t length); -int GetSocketLevelOption(std::uintptr_t native, SocketOptionState& state, int type, int option, void* value, std::uint32_t* length); -int SetTcpLevelOption(std::uintptr_t native, bool tcp, int option, const void* value, std::uint32_t length); -int GetTcpLevelOption(std::uintptr_t native, bool tcp, int option, void* value, std::uint32_t* length); +int SetSocketLevelOption(std::uintptr_t native, SocketOptionState& state, int family, int type, int option, const void* value, std::uint32_t length); +int GetSocketLevelOption(std::uintptr_t native, SocketOptionState& state, int family, int type, int option, void* value, std::uint32_t* length); +int SetTcpLevelOption(std::uintptr_t native, SocketOptionState& state, bool tcp, int option, const void* value, std::uint32_t length); +int GetTcpLevelOption(std::uintptr_t native, SocketOptionState& state, bool tcp, int option, void* value, std::uint32_t* length); +int SetIpLevelOption(std::uintptr_t native, SocketOptionState& state, bool ipv4, bool datagram, int option, const void* value, std::uint32_t length); +int GetIpLevelOption(SocketOptionState& state, bool ipv4, bool datagram, int option, void* value, std::uint32_t* length); int SetIpv6LevelOption(std::uintptr_t native, bool ipv6, int option, const void* value, std::uint32_t length); int GetIpv6LevelOption(std::uintptr_t native, bool ipv6, int option, void* value, std::uint32_t* length); diff --git a/core/libs/prx/libkernel/Socket/src/SocketOptions.cpp b/core/libs/prx/libkernel/Socket/src/SocketOptions.cpp index 37fce2af2e..fd7ea3cfd8 100644 --- a/core/libs/prx/libkernel/Socket/src/SocketOptions.cpp +++ b/core/libs/prx/libkernel/Socket/src/SocketOptions.cpp @@ -16,6 +16,7 @@ #include "prx/libkernel/Socket/include/SocketOptions.hpp" #include #include +#include namespace GuestSockets { namespace { @@ -30,18 +31,48 @@ constexpr int GuestOobInline = 0x100; constexpr int GuestNoSigpipe = 0x800; constexpr int GuestSendBuffer = 0x1001; constexpr int GuestReceiveBuffer = 0x1002; +constexpr int GuestSendLowWater = 0x1003; +constexpr int GuestReceiveLowWater = 0x1004; constexpr int GuestSendTimeout = 0x1005; constexpr int GuestReceiveTimeout = 0x1006; constexpr int GuestError = 0x1007; constexpr int GuestType = 0x1008; constexpr int GuestNonBlocking = 0x1200; +constexpr int GuestNonBlockingState = 0x100; +constexpr int GuestUnix = 1; constexpr int GuestTcpNoDelay = 1; +constexpr int GuestReusePort = 0x200; +constexpr int KeptFlags = 0x1 | GuestReuseAddress | 0x40 | GuestReusePort | 0x400 | 0x2000 | 0x10000; +constexpr int WriteOnlyFlags = 0x4000 | 0x8000; constexpr int GuestTcpNoDelayFlag = 4; +constexpr int GuestTcpMaximumSegment = 2; +constexpr int GuestTcpKeepIdle = 0x100; +constexpr int GuestTcpKeepInterval = 0x200; +constexpr int GuestTcpKeepCount = 0x400; +constexpr int MinimumSegment = 216; +constexpr unsigned int TicksPerSecond = 1000; +constexpr unsigned int DefaultKeepIdle = 7200; +constexpr unsigned int DefaultKeepInterval = 75; +constexpr unsigned int DefaultKeepCount = 8; constexpr int GuestIpv6Only = 27; +constexpr int GuestIpTypeOfService = 3; +constexpr int GuestIpTimeToLive = 4; +constexpr int GuestIpMulticastTimeToLive = 10; +constexpr int GuestIpMulticastLoop = 11; +constexpr int GuestIpMulticastInterface = 9; +constexpr int GuestIpAddMembership = 12; +constexpr int GuestIpDropMembership = 13; +constexpr std::uint32_t MulticastRequestWithIndex = 12; +constexpr int ErrorNotSupported = 45; +constexpr int ErrorAddressInUse = 48; +constexpr int ErrorAddressNotAvailable = 49; constexpr int ErrorInvalid = 22; constexpr int ErrorDomain = 33; constexpr int ErrorNoProtocolOption = 42; +constexpr int ErrorNoBuffers = 55; +constexpr int MaximumReservation = 5592405; +constexpr int MaximumBuffer = 524288; constexpr int MaximumLinger = 65535; constexpr std::int64_t LongestTimeout = 0x7fffffffffffffffLL; @@ -78,7 +109,6 @@ int HostError() { return GuestErrorFromHost(errno); } int NativeBoolean(int option) { switch (option) { - case GuestReuseAddress: return SO_REUSEADDR; case GuestKeepAlive: return SO_KEEPALIVE; case GuestDontRoute: return SO_DONTROUTE; case GuestBroadcast: return SO_BROADCAST; @@ -87,9 +117,21 @@ int NativeBoolean(int option) { } } +void EnsureBuffers(SocketOptionState& state, int family, int type) { + if (state.sendBuffer != 0) return; + if (family == GuestUnix) { + state.sendBuffer = type == 2 ? 2048 : 8192; + state.receiveBuffer = type == 2 ? 4096 : 8192; + } else { + state.sendBuffer = type == 1 ? 32768 : 9216; + state.receiveBuffer = type == 1 ? 65536 : 42080; + } + state.sendLowWater = std::min(2048, state.sendBuffer); + state.receiveLowWater = 1; +} + int Copy(void* output, std::uint32_t* length, const void* value, std::uint32_t size) { - const std::uint32_t count = std::min(*length, size); - if (count != 0 && !output) return ErrorInvalid; + const std::uint32_t count = output ? std::min(*length, size) : 0; if (count != 0) std::memcpy(output, value, count); *length = count; return 0; @@ -97,11 +139,10 @@ int Copy(void* output, std::uint32_t* length, const void* value, std::uint32_t s } -int SetSocketLevelOption(std::uintptr_t native, SocketOptionState& state, int option, const void* value, std::uint32_t length) { +int SetSocketLevelOption(std::uintptr_t native, SocketOptionState& state, int family, int type, int option, const void* value, std::uint32_t length) { const auto handle = static_cast(native); if (option == GuestLinger) { if (length < sizeof(GuestLingerValue)) return ErrorInvalid; - if (!value) return ErrorInvalid; GuestLingerValue guest; std::memcpy(&guest, value, sizeof(guest)); if (guest.seconds < 0 || guest.seconds > MaximumLinger) return ErrorDomain; @@ -119,7 +160,6 @@ int SetSocketLevelOption(std::uintptr_t native, SocketOptionState& state, int op } if (option == GuestSendTimeout || option == GuestReceiveTimeout) { if (length < sizeof(GuestTimeval)) return ErrorInvalid; - if (!value) return ErrorInvalid; GuestTimeval time; std::memcpy(&time, value, sizeof(time)); if (time.seconds < 0 || time.microseconds < 0 || time.microseconds >= 1000000) return ErrorDomain; @@ -128,7 +168,6 @@ int SetSocketLevelOption(std::uintptr_t native, SocketOptionState& state, int op } if (option == GuestNoSigpipe || option == GuestNonBlocking) { if (length < sizeof(int)) return ErrorInvalid; - if (!value) return ErrorInvalid; int number; std::memcpy(&number, value, sizeof(number)); if (option == GuestNoSigpipe) { @@ -146,21 +185,52 @@ int SetSocketLevelOption(std::uintptr_t native, SocketOptionState& state, int op return 0; } const bool native_boolean = NativeBoolean(option) != -1; - if (native_boolean || option == GuestSendBuffer || option == GuestReceiveBuffer) { + const bool buffer = option == GuestSendBuffer || option == GuestReceiveBuffer; + const bool lowWater = option == GuestSendLowWater || option == GuestReceiveLowWater; + if (native_boolean || buffer || lowWater) { if (length < sizeof(int)) return ErrorInvalid; - if (!value) return ErrorInvalid; int number; std::memcpy(&number, value, sizeof(number)); - if (option == GuestSendBuffer || option == GuestReceiveBuffer) { + if (buffer || lowWater) { if (number < 1) return ErrorInvalid; - const int nativeOption = option == GuestSendBuffer ? SO_SNDBUF : SO_RCVBUF; + EnsureBuffers(state, family, type); + const bool send = option == GuestSendBuffer || option == GuestSendLowWater; + int& size = send ? state.sendBuffer : state.receiveBuffer; + int& mark = send ? state.sendLowWater : state.receiveLowWater; + if (lowWater) { + mark = std::min(number, size); + return 0; + } + if (number > MaximumReservation) return ErrorNoBuffers; + const int nativeOption = send ? SO_SNDBUF : SO_RCVBUF; if (::setsockopt(handle, SOL_SOCKET, nativeOption, reinterpret_cast(&number), sizeof(number))) return HostError(); + size = std::min(number, MaximumBuffer); + mark = std::min(mark, size); return 0; } const int enabled = number != 0 ? 1 : 0; if (::setsockopt(handle, SOL_SOCKET, NativeBoolean(option), reinterpret_cast(&enabled), sizeof(enabled))) return HostError(); return 0; } + if ((option & (KeptFlags | WriteOnlyFlags)) != 0 && (option & (option - 1)) == 0) { + if (length < sizeof(int)) return ErrorInvalid; + int number; + std::memcpy(&number, value, sizeof(number)); + if ((option & KeptFlags) == 0) return 0; + const int flags = number != 0 ? state.keptFlags | option : state.keptFlags & ~option; + if (option == GuestReuseAddress || option == GuestReusePort) { +#ifdef _WIN32 + const int reuseAddress = (flags & (GuestReuseAddress | GuestReusePort)) != 0 ? 1 : 0; +#else + const int reuseAddress = (flags & GuestReuseAddress) != 0 ? 1 : 0; + const int reusePort = (flags & GuestReusePort) != 0 ? 1 : 0; + if (::setsockopt(handle, SOL_SOCKET, SO_REUSEPORT, reinterpret_cast(&reusePort), sizeof(reusePort))) return HostError(); +#endif + if (::setsockopt(handle, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast(&reuseAddress), sizeof(reuseAddress))) return HostError(); + } + state.keptFlags = flags; + return 0; + } return ErrorNoProtocolOption; } @@ -178,7 +248,7 @@ bool NativeListening(std::uintptr_t native, bool& listening) { return true; } -int GetSocketLevelOption(std::uintptr_t native, SocketOptionState& state, int type, int option, void* value, std::uint32_t* length) { +int GetSocketLevelOption(std::uintptr_t native, SocketOptionState& state, int family, int type, int option, void* value, std::uint32_t* length) { const auto handle = static_cast(native); if (option == GuestLinger) { linger current{}; @@ -203,10 +273,12 @@ int GetSocketLevelOption(std::uintptr_t native, SocketOptionState& state, int ty number = listening ? GuestAcceptConnection : 0; } else if (option == GuestType) { number = type; + } else if ((option & KeptFlags) != 0 && (option & (option - 1)) == 0) { + number = state.keptFlags & option; } else if (option == GuestNoSigpipe) { number = state.noSignal ? GuestNoSigpipe : 0; } else if (option == GuestNonBlocking) { - number = state.nonblocking ? 1 : 0; + number = state.nonblocking ? GuestNonBlockingState : 0; } else if (option == GuestError) { int pending = 0; socklen_t size = sizeof(pending); @@ -216,29 +288,53 @@ int GetSocketLevelOption(std::uintptr_t native, SocketOptionState& state, int ty else if (pending != 0) state.reportedError = pending; #endif number = pending == 0 ? 0 : GuestErrorFromHost(pending); - } else if (option == GuestSendBuffer || option == GuestReceiveBuffer) { - socklen_t size = sizeof(number); - if (::getsockopt(handle, SOL_SOCKET, option == GuestSendBuffer ? SO_SNDBUF : SO_RCVBUF, reinterpret_cast(&number), &size)) return HostError(); + } else if (option == GuestSendBuffer || option == GuestReceiveBuffer || option == GuestSendLowWater || option == GuestReceiveLowWater) { + EnsureBuffers(state, family, type); + number = option == GuestSendBuffer ? state.sendBuffer : option == GuestReceiveBuffer ? state.receiveBuffer + : option == GuestSendLowWater ? state.sendLowWater : state.receiveLowWater; } else { return ErrorNoProtocolOption; } return Copy(value, length, &number, sizeof(number)); } -int SetTcpLevelOption(std::uintptr_t native, bool tcp, int option, const void* value, std::uint32_t length) { +int SetTcpLevelOption(std::uintptr_t native, SocketOptionState& state, bool tcp, int option, const void* value, std::uint32_t length) { if (!tcp) return ErrorInvalid; - if (option != GuestTcpNoDelay) return ErrorNoProtocolOption; + if (option != GuestTcpNoDelay && option != GuestTcpMaximumSegment && option != GuestTcpKeepIdle && option != GuestTcpKeepInterval && + option != GuestTcpKeepCount) + return ErrorNoProtocolOption; if (length < sizeof(int)) return ErrorInvalid; - if (!value) return ErrorInvalid; int number; std::memcpy(&number, value, sizeof(number)); + const auto unsignedNumber = static_cast(number); + if (option == GuestTcpMaximumSegment) { + if (number <= 0 || number > state.tcpMaximumSegment || number + 40 < MinimumSegment) return ErrorInvalid; + state.tcpMaximumSegment = number; + return 0; + } + if (option == GuestTcpKeepCount) { + state.tcpKeepCount = unsignedNumber; + return 0; + } + if (option == GuestTcpKeepIdle || option == GuestTcpKeepInterval) { + if (unsignedNumber > UINT32_MAX / TicksPerSecond) return ErrorInvalid; + (option == GuestTcpKeepIdle ? state.tcpKeepIdle : state.tcpKeepInterval) = unsignedNumber; + return 0; + } const int enabled = number != 0 ? 1 : 0; if (::setsockopt(static_cast(native), IPPROTO_TCP, TCP_NODELAY, reinterpret_cast(&enabled), sizeof(enabled))) return HostError(); return 0; } -int GetTcpLevelOption(std::uintptr_t native, bool tcp, int option, void* value, std::uint32_t* length) { +int GetTcpLevelOption(std::uintptr_t native, SocketOptionState& state, bool tcp, int option, void* value, std::uint32_t* length) { if (!tcp) return ErrorInvalid; + if (option == GuestTcpMaximumSegment || option == GuestTcpKeepIdle || option == GuestTcpKeepInterval || option == GuestTcpKeepCount) { + const unsigned int number = option == GuestTcpMaximumSegment ? static_cast(state.tcpMaximumSegment) + : option == GuestTcpKeepIdle ? (state.tcpKeepIdle != 0 ? state.tcpKeepIdle : DefaultKeepIdle) + : option == GuestTcpKeepInterval ? (state.tcpKeepInterval != 0 ? state.tcpKeepInterval : DefaultKeepInterval) + : (state.tcpKeepCount != 0 ? state.tcpKeepCount : DefaultKeepCount); + return Copy(value, length, &number, sizeof(number)); + } if (option != GuestTcpNoDelay) return ErrorNoProtocolOption; int enabled = 0; socklen_t size = sizeof(enabled); @@ -247,11 +343,117 @@ int GetTcpLevelOption(std::uintptr_t native, bool tcp, int option, void* value, return Copy(value, length, &number, sizeof(number)); } +bool MulticastOption(int option) { + return option == GuestIpMulticastInterface || option == GuestIpMulticastTimeToLive || option == GuestIpMulticastLoop || + option == GuestIpAddMembership || option == GuestIpDropMembership; +} + +int SetIpLevelOption(std::uintptr_t native, SocketOptionState& state, bool ipv4, bool datagram, int option, const void* value, std::uint32_t length) { + if (!ipv4) return ErrorInvalid; + if (MulticastOption(option) && !datagram) return ErrorNotSupported; + const auto handle = static_cast(native); + if (option == GuestIpAddMembership || option == GuestIpDropMembership) { + if (length < sizeof(ip_mreq)) return ErrorInvalid; + ip_mreq request{}; + std::memcpy(&request, value, sizeof(request)); + const auto* group = reinterpret_cast(&request.imr_multiaddr); + if ((group[0] & 0xf0) != 0xe0) return ErrorInvalid; + std::uint32_t groupBits; + std::uint32_t interfaceBits; + std::memcpy(&groupBits, &request.imr_multiaddr, sizeof(groupBits)); + std::memcpy(&interfaceBits, &request.imr_interface, sizeof(interfaceBits)); + const std::uint64_t key = (std::uint64_t{groupBits} << 32) | interfaceBits; + const bool join = option == GuestIpAddMembership; + const auto found = join || interfaceBits != 0 + ? std::find(state.memberships.begin(), state.memberships.end(), key) + : std::find_if(state.memberships.begin(), state.memberships.end(), [groupBits](std::uint64_t joined) { return joined >> 32 == groupBits; }); + if (join && found != state.memberships.end()) return ErrorAddressInUse; + if (!join && found == state.memberships.end()) return ErrorAddressNotAvailable; + if (!join) { + const auto joinedInterface = static_cast(*found); + std::memcpy(&request.imr_interface, &joinedInterface, sizeof(joinedInterface)); + } + if (::setsockopt(handle, IPPROTO_IP, join ? IP_ADD_MEMBERSHIP : IP_DROP_MEMBERSHIP, reinterpret_cast(&request), sizeof(request))) + return HostError(); + if (join) state.memberships.push_back(key); + else state.memberships.erase(found); + return 0; + } + if (option == GuestIpMulticastInterface) { + if (length < sizeof(in_addr)) return ErrorInvalid; + in_addr address{}; + if (length == MulticastRequestWithIndex) { + std::int32_t index; + std::memcpy(&index, static_cast(value) + 8, sizeof(index)); + if (index != 0) throw std::runtime_error("IP_MULTICAST_IF: an interface index is not supported"); + const auto* addresses = static_cast(value); + if (std::any_of(addresses, addresses + 8, [](unsigned char byte) { return byte != 0; })) + throw std::runtime_error("IP_MULTICAST_IF: an ip_mreqn with a non-zero address is not supported"); + } else { + std::memcpy(&address, value, sizeof(address)); + } + if (::setsockopt(handle, IPPROTO_IP, IP_MULTICAST_IF, reinterpret_cast(&address), sizeof(address))) return HostError(); + std::memcpy(&state.multicastInterface, &address, sizeof(address)); + return 0; + } + if (option == GuestIpTimeToLive || option == GuestIpTypeOfService) { + if (length < sizeof(int)) return ErrorInvalid; + int number; + std::memcpy(&number, value, sizeof(number)); + const int stored = number & 0xff; + if (option == GuestIpTypeOfService) { + state.ipTypeOfService = stored; + return 0; + } + if (stored != 0 && ::setsockopt(handle, IPPROTO_IP, IP_TTL, reinterpret_cast(&stored), sizeof(stored))) return HostError(); + state.ipTimeToLive = stored; + return 0; + } + if (option == GuestIpMulticastTimeToLive || option == GuestIpMulticastLoop) { + unsigned int number; + if (length == 1) { + unsigned char byte; + std::memcpy(&byte, value, 1); + number = byte; + } else { + if (length < sizeof(number)) return ErrorInvalid; + std::memcpy(&number, value, sizeof(number)); + if (option == GuestIpMulticastTimeToLive && number > 255) return ErrorInvalid; + } + const int host = option == GuestIpMulticastLoop ? ((number & 0xff) != 0 ? 1 : 0) : static_cast(number); + const int nativeOption = option == GuestIpMulticastLoop ? IP_MULTICAST_LOOP : IP_MULTICAST_TTL; + if (::setsockopt(handle, IPPROTO_IP, nativeOption, reinterpret_cast(&host), sizeof(host))) return HostError(); + if (option == GuestIpMulticastLoop) state.multicastLoop = host != 0; + else state.multicastTimeToLive = host; + return 0; + } + return ErrorNoProtocolOption; +} + +int GetIpLevelOption(SocketOptionState& state, bool ipv4, bool datagram, int option, void* value, std::uint32_t* length) { + if (!ipv4) return ErrorInvalid; + if (MulticastOption(option) && option != GuestIpAddMembership && option != GuestIpDropMembership && !datagram) return ErrorNotSupported; + if (option == GuestIpMulticastInterface) { + if (*length == MulticastRequestWithIndex) throw std::runtime_error("IP_MULTICAST_IF: reading an ip_mreqn is not supported"); + return Copy(value, length, &state.multicastInterface, sizeof(state.multicastInterface)); + } + int number; + if (option == GuestIpTimeToLive) number = state.ipTimeToLive; + else if (option == GuestIpTypeOfService) number = state.ipTypeOfService; + else if (option == GuestIpMulticastTimeToLive) number = state.multicastTimeToLive; + else if (option == GuestIpMulticastLoop) number = state.multicastLoop ? 1 : 0; + else return ErrorNoProtocolOption; + if ((option == GuestIpMulticastTimeToLive || option == GuestIpMulticastLoop) && *length == 1) { + const auto byte = static_cast(number); + return Copy(value, length, &byte, 1); + } + return Copy(value, length, &number, sizeof(number)); +} + int SetIpv6LevelOption(std::uintptr_t native, bool ipv6, int option, const void* value, std::uint32_t length) { if (!ipv6) return ErrorInvalid; if (option != GuestIpv6Only) return ErrorNoProtocolOption; if (length != sizeof(int)) return ErrorInvalid; - if (!value) return ErrorInvalid; int number; std::memcpy(&number, value, sizeof(number)); const int enabled = number != 0 ? 1 : 0; diff --git a/core/libs/prx/libkernel/Socket/src/Udp.cpp b/core/libs/prx/libkernel/Socket/src/Udp.cpp index 078c6d9aec..61a0d07b10 100644 --- a/core/libs/prx/libkernel/Socket/src/Udp.cpp +++ b/core/libs/prx/libkernel/Socket/src/Udp.cpp @@ -313,16 +313,6 @@ bool Listening(const Socket& socket) { bool listening = false; return GuestSockets::NativeListening(static_cast(socket.value), listening) && listening; } -bool ZeroPortConnect(const Socket& socket, const void* address, std::uint32_t length) { - if (!address || (socket.type != 1 && socket.type != 2) || (socket.family != 2 && socket.family != 28)) return false; - const auto* bytes = static_cast(address); - if (length < (socket.family == 2 ? 16u : 28u) || bytes[1] != socket.family || (bytes[2] | bytes[3]) != 0) return false; - sockaddr_storage peer{}; - socklen_t peerSize = sizeof(peer); - if (::getpeername(socket.value, reinterpret_cast(&peer), &peerSize) == 0) return false; - Fail(49); - return true; -} constexpr std::int64_t NotAnswered = -2; std::int64_t ZeroPortSend(const Socket& socket, const sockaddr_storage& destination, std::uint64_t length) { if (socket.type != 2 || socket.family != 2 || destination.ss_family != AF_INET) return NotAnswered; @@ -364,6 +354,20 @@ int Disconnect(const Socket& socket) { if (::bind(socket.value, reinterpret_cast(&wildcard), localSize) != 0) return Fail(NativeError()); return 0; } +bool ZeroPortConnect(const Socket& socket, const void* address, std::uint32_t length) { + if (!address || (socket.type != 1 && socket.type != 2) || (socket.family != 2 && socket.family != 28)) return false; + const auto* bytes = static_cast(address); + if (length < (socket.family == 2 ? 16u : 28u) || bytes[1] != socket.family || (bytes[2] | bytes[3]) != 0) return false; + if (socket.type == 2) { + if (Disconnect(socket) != 0) return true; + } else { + sockaddr_storage peer{}; + socklen_t peerSize = sizeof(peer); + if (::getpeername(socket.value, reinterpret_cast(&peer), &peerSize) == 0) return false; + } + Fail(49); + return true; +} void GuestAddress(const sockaddr_storage& native, void* output, std::uint32_t* length) { unsigned char bytes[28]{}; bytes[0] = native.ss_family == AF_INET ? 16 : 28; @@ -712,6 +716,17 @@ int SetNonBlocking(Socket& socket) { socket.options.nonblocking = true; return 0; } +bool ReadableNow(const Socket& socket) { + NativePollDescriptor entry{}; + entry.fd = socket.value; +#ifdef _WIN32 + entry.events = POLLRDNORM; + return WSAPoll(&entry, 1, 0) > 0; +#else + entry.events = POLLIN; + return ::poll(&entry, 1, 0) > 0; +#endif +} int ReceiveAnsweredHere(Socket& socket, std::uint64_t length) { #ifdef _WIN32 { @@ -719,7 +734,8 @@ int ReceiveAnsweredHere(Socket& socket, std::uint64_t length) { if (socket.receiveShut) return 0; } #endif - if (length != 0 || socket.type != 1) return -1; + if (length != 0) return -1; + if (socket.type != 1) return ReadableNow(socket) ? -1 : 0; sockaddr_storage peer{}; socklen_t size = sizeof(peer); return ::getpeername(socket.value, reinterpret_cast(&peer), &size) == 0 ? 0 : NativeError(); @@ -1122,31 +1138,35 @@ int APS5_VABI _fcntl_nid_postfix(int descriptor, int command, ...) { } int APS5_VABI setsockopt_nid_postfix(int descriptor, int level, int option, const void* value, std::uint32_t length) { + if (!value && length != 0) return Fail(14); int netError = 0; if (!GuestSockets::IsOpen(descriptor) && KernelSetSocketOption_nid_no_patch(descriptor, level, option, value, length, &netError)) { return netError == 0 ? 0 : Fail(netError); } const auto socket = Lookup(descriptor); if (!socket) return -1; - if (level != 0xffff && level != 6 && level != 41) return Fail(42); + if (level != 0xffff && level != 0 && level != 6 && level != 41) return Fail(22); std::lock_guard lock(socket->modeMutex); const auto native = static_cast(socket->value); - const int error = level == 6 ? GuestSockets::SetTcpLevelOption(native, socket->family != GuestUnix && socket->type == 1, option, value, length) + const int error = level == 0 ? GuestSockets::SetIpLevelOption(native, socket->options, socket->family == 2, socket->type == 2, option, value, length) + : level == 6 ? GuestSockets::SetTcpLevelOption(native, socket->options, socket->family != GuestUnix && socket->type == 1, option, value, length) : level == 41 ? GuestSockets::SetIpv6LevelOption(native, socket->family == 28, option, value, length) - : GuestSockets::SetSocketLevelOption(native, socket->options, option, value, length); + : GuestSockets::SetSocketLevelOption(native, socket->options, socket->family, socket->type, option, value, length); return error == 0 ? 0 : Fail(error); } int APS5_VABI getsockopt_nid_postfix(int descriptor, int level, int option, void* value, std::uint32_t* length) { const auto socket = Lookup(descriptor); if (!socket) return -1; - if (level != 0xffff && level != 6 && level != 41) return Fail(42); + if (level != 0xffff && level != 0 && level != 6 && level != 41) return Fail(22); if (!length) return Fail(22); + if (!value) *length = 0; std::lock_guard lock(socket->modeMutex); const auto native = static_cast(socket->value); - const int error = level == 6 ? GuestSockets::GetTcpLevelOption(native, socket->family != GuestUnix && socket->type == 1, option, value, length) + const int error = level == 0 ? GuestSockets::GetIpLevelOption(socket->options, socket->family == 2, socket->type == 2, option, value, length) + : level == 6 ? GuestSockets::GetTcpLevelOption(native, socket->options, socket->family != GuestUnix && socket->type == 1, option, value, length) : level == 41 ? GuestSockets::GetIpv6LevelOption(native, socket->family == 28, option, value, length) - : GuestSockets::GetSocketLevelOption(native, socket->options, socket->type, option, value, length); + : GuestSockets::GetSocketLevelOption(native, socket->options, socket->family, socket->type, option, value, length); return error == 0 ? 0 : Fail(error); } int APS5_VABI socket_nid_postfix(int family, int type, int protocol) { @@ -1279,8 +1299,18 @@ int APS5_VABI accept_nid_postfix(int descriptor, void* address, std::uint32_t* l std::lock_guard modeLock(listener->modeMutex); inheritNonblocking = listener->options.nonblocking; accepted->options.noSignal = listener->options.noSignal; + accepted->options.keptFlags = listener->options.keptFlags; accepted->options.sendTimeout = listener->options.sendTimeout; accepted->options.receiveTimeout = listener->options.receiveTimeout; + accepted->options.sendBuffer = listener->options.sendBuffer; + accepted->options.receiveBuffer = listener->options.receiveBuffer; + accepted->options.sendLowWater = listener->options.sendLowWater; + accepted->options.receiveLowWater = listener->options.receiveLowWater; + accepted->options.ipTimeToLive = listener->options.ipTimeToLive; + accepted->options.ipTypeOfService = listener->options.ipTypeOfService; + accepted->options.tcpKeepIdle = listener->options.tcpKeepIdle; + accepted->options.tcpKeepInterval = listener->options.tcpKeepInterval; + accepted->options.tcpKeepCount = listener->options.tcpKeepCount; } if (inheritNonblocking && SetNonBlocking(*accepted)) return -1; const int acceptedDescriptor = Install(std::move(accepted)); @@ -1583,22 +1613,26 @@ std::int64_t APS5_VABI recvmsg_nid_postfix(int descriptor, GuestMsghdr* message, return Fail(55); } sockaddr_storage native{}; - socklen_t size = sizeof(native); - if (const int waited = WaitForTransfer(*socket, false, flags)) return Fail(waited); - std::int64_t received = ::recvfrom(socket->value, buffer.data(), static_cast(buffer.size()), - NativeReceiveFlags(flags, socket->type), reinterpret_cast(&native), &size); - if (received < 0) { + socklen_t size = 0; + std::int64_t received = 0; + if (total != 0 || socket->type == 1 || ReadableNow(*socket)) { + size = sizeof(native); + if (const int waited = WaitForTransfer(*socket, false, flags)) return Fail(waited); + received = ::recvfrom(socket->value, buffer.data(), static_cast(buffer.size()), + NativeReceiveFlags(flags, socket->type), reinterpret_cast(&native), &size); + if (received < 0) { #ifdef _WIN32 - if (!records || WSAGetLastError() != WSAEMSGSIZE) return Fail(NativeError()); - received = static_cast(buffer.size()); + if (!records || WSAGetLastError() != WSAEMSGSIZE) return Fail(NativeError()); + received = static_cast(buffer.size()); #else - return Fail(NativeError()); + return Fail(NativeError()); #endif + } } - header.flags = 0; + header.flags = flags; if (received > total) { received = total; - header.flags = 0x10; + header.flags |= 0x10; } std::size_t offset = 0; for (const auto& vector : vectors) { @@ -1711,6 +1745,7 @@ int APS5_VABI poll_nid_postfix(GuestPollDescriptor* descriptors, std::uint32_t c std::vector native; std::vector owners; std::vector pipes; + std::vector kqueues; int ready = 0; for (std::uint32_t i = 0; i < count; ++i) { auto& entry = descriptors[i]; @@ -1729,6 +1764,10 @@ int APS5_VABI poll_nid_postfix(GuestPollDescriptor* descriptors, std::uint32_t c ++ready; continue; } + if (IsKqueueDescriptor(entry.descriptor)) { + kqueues.push_back(i); + continue; + } if (File::IsRandomDevice(entry.descriptor)) { const short readable = static_cast(entry.events & GuestPollReadable); entry.revents = readable != 0 ? readable : entry.events; @@ -1751,7 +1790,7 @@ int APS5_VABI poll_nid_postfix(GuestPollDescriptor* descriptors, std::uint32_t c #else if (!IsPipe(entry.descriptor, entry.events)) #endif - throw std::runtime_error("poll: descriptor " + std::to_string(entry.descriptor) + " is neither a socket, a pipe, a file nor a directory"); + throw std::runtime_error("poll: descriptor " + std::to_string(entry.descriptor) + " is neither a socket, a pipe, a file, a directory nor a kqueue"); pipes.push_back(i); continue; } @@ -1774,7 +1813,7 @@ int APS5_VABI poll_nid_postfix(GuestPollDescriptor* descriptors, std::uint32_t c owners.push_back(index); } #endif - if (native.empty() && pipes.empty()) { + if (native.empty() && pipes.empty() && kqueues.empty()) { if (ready || timeout == 0) return ready; if (timeout < 0) throw std::runtime_error("poll: an infinite wait without sockets or pipes never returns"); PreciseSleepUs(static_cast(timeout) * 1000); @@ -1795,18 +1834,35 @@ int APS5_VABI poll_nid_postfix(GuestPollDescriptor* descriptors, std::uint32_t c entry.revents = static_cast((entry.revents & ~GuestPollWritable) | (entry.events & GuestPollReadable)); if (entry.revents) ++ready; }; -#ifdef _WIN32 const auto deadline = std::chrono::steady_clock::now() + std::chrono::milliseconds(timeout < 0 ? 0 : timeout); +#ifdef _WIN32 + const bool sliced = !pipes.empty() || !kqueues.empty(); +#else + const bool sliced = !kqueues.empty(); +#endif for (;;) { - int pipeReady = 0; + int checkedReady = 0; +#ifdef _WIN32 for (const auto index : pipes) { auto& entry = descriptors[index]; PipeReadiness(entry.descriptor, entry.events, entry.revents); - if (entry.revents) ++pipeReady; + if (entry.revents) ++checkedReady; + } +#endif + for (const auto index : kqueues) { + auto& entry = descriptors[index]; + const auto readable = KqueueReadable(entry.descriptor); + if (!readable && DescriptorIsOpen_nid_no_patch(entry.descriptor)) { + if (timeout < 0) return poll_nid_postfix(descriptors, count, -1); + const auto left = std::chrono::duration_cast(deadline - std::chrono::steady_clock::now()).count(); + return poll_nid_postfix(descriptors, count, static_cast(std::max(0, (left + 999) / 1000))); + } + entry.revents = !readable ? GuestPollInvalid : *readable ? static_cast(entry.events & GuestPollReadable) : short{0}; + if (entry.revents) ++checkedReady; } int slice = 0; - if (!ready && !pipeReady) { - if (pipes.empty()) slice = timeout; + if (!ready && !checkedReady) { + if (!sliced) slice = timeout; else if (timeout < 0) slice = 1; else { const auto left = std::chrono::duration_cast(deadline - std::chrono::steady_clock::now()).count(); @@ -1815,8 +1871,12 @@ int APS5_VABI poll_nid_postfix(GuestPollDescriptor* descriptors, std::uint32_t c } if (!native.empty()) { for (auto& request : native) request.revents = 0; +#ifdef _WIN32 if (WSAPoll(native.data(), static_cast(native.size()), slice) < 0) return Fail(NativeError()); - } else if (!ready && !pipeReady) { +#else + if (::poll(native.data(), static_cast(native.size()), slice) < 0) return Fail(NativeError()); +#endif + } else if (!ready && !checkedReady) { const auto wake = std::chrono::steady_clock::now() + std::chrono::milliseconds(1); PreciseSleepUntil(timeout < 0 ? wake : std::min(deadline, wake)); } @@ -1824,19 +1884,16 @@ int APS5_VABI poll_nid_postfix(GuestPollDescriptor* descriptors, std::uint32_t c for (const auto& request : native) if (request.revents) ++socketReady; const bool expired = timeout >= 0 && std::chrono::steady_clock::now() >= deadline; - if (ready || pipeReady || socketReady || expired || pipes.empty()) { - ready += pipeReady; + if (ready || checkedReady || socketReady || expired || !sliced) { + ready += checkedReady; +#ifdef _WIN32 for (std::size_t i = 0; i < native.size(); ++i) collect(i, false); +#else + for (std::size_t i = 0; i < native.size(); ++i) collect(i, i >= socketCount); +#endif return ready; } } -#else - const int wait = ready ? 0 : timeout; - const int result = ::poll(native.data(), static_cast(native.size()), wait); - if (result < 0) return Fail(NativeError()); - for (std::size_t i = 0; i < native.size(); ++i) collect(i, i >= socketCount); - return ready; -#endif } } diff --git a/core/libs/prx/libkernel/SyncOnAddress/src/SyncOnAddress.cpp b/core/libs/prx/libkernel/SyncOnAddress/src/SyncOnAddress.cpp index 98da8ea9e9..ae7d9a7b13 100644 --- a/core/libs/prx/libkernel/SyncOnAddress/src/SyncOnAddress.cpp +++ b/core/libs/prx/libkernel/SyncOnAddress/src/SyncOnAddress.cpp @@ -56,7 +56,7 @@ struct UserSemaphore { }; struct AddressWaiter { - TimedWait::Condition condition; + TimedWait::SingleWaiterCondition condition; bool woken = false; }; @@ -81,15 +81,11 @@ bool IsAlignedAddress(std::uintptr_t address) { template void DeferringDelivery(TFunction function) { -#ifdef _WIN32 { TimedWait::DeliveryDeferral deferral; function(); } TimedWait::DeliverDeferred(); -#else - function(); -#endif } template diff --git a/core/libs/prx/libkernel/System/src/Exception.cpp b/core/libs/prx/libkernel/System/src/Exception.cpp index 8508d317d0..a6b313d86a 100644 --- a/core/libs/prx/libkernel/System/src/Exception.cpp +++ b/core/libs/prx/libkernel/System/src/Exception.cpp @@ -4,6 +4,7 @@ #include "prx/libc/include/General.hpp" #include "prx/libkernel/KernelErrors.hpp" #include "prx/libkernel/Pthread/include/Pthread.hpp" +#include "prx/libkernel/Time/include/TimedWait.hpp" #include #include #include @@ -527,7 +528,8 @@ void ToHost(const GuestMcontext& m, mcontext_t& host) { if (host.fpregs != nullptr) std::memcpy(host.fpregs, m.fpstate, FxsaveRegisterBytes); } -void Deliver(int, siginfo_t*, void* context) { +void Deliver(int signum, siginfo_t*, void* context) { + if (TimedWait::DeferDelivery(signum)) return; const auto handler = Handler(RaisedSignal); if (handler == nullptr) { static constexpr char message[] = "sceKernelRaiseException: the handler was removed before the signal was delivered\n"; diff --git a/core/libs/prx/libkernel/System/src/Process.cpp b/core/libs/prx/libkernel/System/src/Process.cpp index 61b3fa2bab..397b42409f 100644 --- a/core/libs/prx/libkernel/System/src/Process.cpp +++ b/core/libs/prx/libkernel/System/src/Process.cpp @@ -58,6 +58,7 @@ extern "C" int APS5_VABI sceKernelVirtualQuery(const void* addr, int flags, Virt namespace { constexpr int sceInvalidArgument = static_cast(0x80020016u); +constexpr int errnoOperationNotPermitted = 1; constexpr int errnoNoSuchProcess = 3; constexpr int errnoNoChild = 10; constexpr int errnoBadAddress = 14; @@ -344,6 +345,24 @@ int APS5_VABI getsid_nid_postfix(int pid) { return -1; } +int APS5_VABI setpgid_nid_postfix(int pid, int pgid) { + if (pgid < 0) { + *__error_nid_postfix() = errnoInvalidArgument; + return -1; + } + const int self = getpid_nid_postfix(); + if (pid != 0 && pid != self) { + *__error_nid_postfix() = errnoNoSuchProcess; + return -1; + } + *__error_nid_postfix() = errnoOperationNotPermitted; + return -1; +} + +int APS5_VABI setpgrp_nid_postfix(int pid, int pgrp) { + return setpgid_nid_postfix(pid, pgrp); +} + int APS5_VABI getppid_nid_postfix(void) { #ifdef _WIN32 const DWORD self = GetCurrentProcessId(); diff --git a/core/libs/prx/libkernel/System/src/Signals.cpp b/core/libs/prx/libkernel/System/src/Signals.cpp index 0137030675..1a281703a4 100644 --- a/core/libs/prx/libkernel/System/src/Signals.cpp +++ b/core/libs/prx/libkernel/System/src/Signals.cpp @@ -124,6 +124,10 @@ bool TakePending(int guest) { return (word.fetch_and(~bit, std::memory_order_relaxed) & bit) != 0; } +void DiscardPendingIfIgnored(int guest, const GuestSigaction& action) { + if (action.handler == 1 || (action.handler == 0 && IgnoredByDefault(guest))) TakePending(guest); +} + bool PendingInSet(const GuestSignalSet& set, int* guest) { for (int candidate = 1; candidate <= MaxSignal; ++candidate) { if (!SignalInSet(set, candidate) || !TakePending(candidate)) continue; @@ -489,6 +493,7 @@ GuestHandler APS5_VABI signal_nid_postfix(int guest, GuestHandler handler) { if (!Install(guest, action)) { *__error_nid_postfix() = 22; return invalid; } const auto previous = reinterpret_cast(dispositions[guest].handler); dispositions[guest] = action; + DiscardPendingIfIgnored(guest, action); return previous; } int APS5_VABI sigaction_nid_postfix(int guest, const GuestSigaction* action, GuestSigaction* previous) { @@ -500,6 +505,7 @@ int APS5_VABI sigaction_nid_postfix(int guest, const GuestSigaction* action, Gue if (action) { if (!Install(guest, *action)) { *__error_nid_postfix() = 22; return -1; } dispositions[guest] = *action; + DiscardPendingIfIgnored(guest, *action); } if (previous) *previous = old; return 0; @@ -675,3 +681,12 @@ int APS5_VABI _is_signal_return_nid_postfix(std::uint64_t programCounter) { } } + +namespace { +struct RegisterSignalsOnLoad { + RegisterSignalsOnLoad() { + RegisterSigaction_nid_no_patch(reinterpret_cast(sigaction_nid_postfix), _sigintr_nid_postfix.bits); + } +}; +const RegisterSignalsOnLoad gRegisterSignals; +} diff --git a/core/libs/prx/libkernel/System/src/SystemConfiguration.cpp b/core/libs/prx/libkernel/System/src/SystemConfiguration.cpp index ff20f5f917..2a06853c32 100644 --- a/core/libs/prx/libkernel/System/src/SystemConfiguration.cpp +++ b/core/libs/prx/libkernel/System/src/SystemConfiguration.cpp @@ -3,6 +3,7 @@ #include "prx/libkernel/DirectMemory/DirectMemory.hpp" #include "prx/libkernel/KernelErrors.hpp" #include "prx/libkernel/System/include/HostCpu.hpp" +#include "prx/libkernel/Time/include/Time.hpp" #include "SceTypes.hpp" #include #include @@ -92,7 +93,18 @@ const SystemInteger* FindSystemInteger(const int* name) { } bool IsLeafMib(const int* name) { - return FindSystemInteger(name) != nullptr || (name[0] == 6 && (name[1] == 2 || name[1] == 3 || name[1] == 5 || name[1] == 7)) || (name[0] == 1 && (name[1] == 37 || name[1] == 50)); + return FindSystemInteger(name) != nullptr || (name[0] == 6 && (name[1] == 2 || name[1] == 3 || name[1] == 5 || name[1] == 7)) || (name[0] == 1 && (name[1] == 21 || name[1] == 37 || name[1] == 50)); +} + +KernelTimeval BootTime() { + static const KernelTimeval bootTime = [] { + KernelTimespec now{}; + KernelTimespec uptime{}; + if (clock_gettime_nid_postfix(0, &now) != 0 || clock_gettime_nid_postfix(5, &uptime) != 0) throw std::runtime_error("kern.boottime: reading CLOCK_REALTIME or CLOCK_UPTIME failed"); + const std::int64_t nanos = (now.tv_sec - uptime.tv_sec) * 1000000000LL + now.tv_nsec - uptime.tv_nsec; + return KernelTimeval{nanos / 1000000000LL, nanos % 1000000000LL / 1000LL}; + }(); + return bootTime; } bool IsMeasuredAbsentMib(const int* name, std::uint32_t nameLength) { @@ -354,6 +366,14 @@ int APS5_VABI sysctl_nid_postfix(const int* name, std::uint32_t nameLength, void const auto entries = GuestVmEntries(); return CopyOut(entries.data(), entries.size() * sizeof(GuestVmEntry), oldValue, oldLength); } + if (nameLength == 2 && name[0] == 1 && name[1] == 21) { + if (newValue != nullptr) { + *__error_nid_postfix() = 1; + return -1; + } + const KernelTimeval bootTime = BootTime(); + return CopyOut(&bootTime, sizeof(bootTime), oldValue, oldLength); + } if (nameLength == 2 && name[0] == 1 && name[1] == 37) { if (newValue != nullptr) { *__error_nid_postfix() = 1; @@ -411,6 +431,10 @@ int APS5_VABI sysctlbyname_nid_postfix(const char* name, void* oldValue, std::si static constexpr int mib[] = {1, 14, 12}; return sysctl_nid_postfix(mib, 3, oldValue, oldLength, newValue, newLength); } + if (std::strcmp(name, "kern.boottime") == 0) { + static constexpr int mib[] = {1, 21}; + return sysctl_nid_postfix(mib, 2, oldValue, oldLength, newValue, newLength); + } if (std::strcmp(name, "kern.arandom") == 0) { static constexpr int mib[] = {1, 37}; return sysctl_nid_postfix(mib, 2, oldValue, oldLength, newValue, newLength); @@ -481,6 +505,31 @@ int APS5_VABI sceKernelGetSystemSwVersion_nid_postfix(SceKernelSystemSwVersion* return 0; } +int APS5_VABI sceKernelGetSystemExVersion(char* first, char* second) { + if (first == nullptr || second == nullptr) { + return 22; + } + *first = '\0'; + *second = '\0'; + return 0; +} + +int APS5_VABI sysKernelGetLowerLimitSysexVersion_nid_postfix(char* version) { + if (version == nullptr) { + return SCE_KERNEL_ERROR_EINVAL; + } + *version = '\0'; + return 0; +} + +int APS5_VABI sysKernelGetLowerLimitUpdVersion_nid_postfix(std::uint32_t* version) { + if (version == nullptr) { + return SCE_KERNEL_ERROR_EINVAL; + } + *version = 0; + return 0; +} + int APS5_VABI sysctlnametomib_nid_postfix(const char* name, int* mib, std::size_t* size) { struct KnownName { const char* name; @@ -496,6 +545,7 @@ int APS5_VABI sysctlnametomib_nid_postfix(const char* name, int* mib, std::size_ {"hw.physmem", {6, 5}, 2}, {"hw.realmem", {6, 12}, 2}, {"kern.arandom", {1, 37}, 2}, + {"kern.boottime", {1, 21}, 2}, {"kern.clockrate", {1, 12}, 2}, {"kern.maxfiles", {1, 7}, 2}, {"kern.maxfilesperproc", {1, 27}, 2}, diff --git a/core/libs/prx/libkernel/Time/TimedWait.cpp b/core/libs/prx/libkernel/Time/TimedWait.cpp index 7c171db399..bb5ba2cdca 100644 --- a/core/libs/prx/libkernel/Time/TimedWait.cpp +++ b/core/libs/prx/libkernel/Time/TimedWait.cpp @@ -11,6 +11,9 @@ #include #else #include +#include +#include +#include #include #endif @@ -389,6 +392,91 @@ void Condition::NotifyAll() { coarse.notify_all(); } +namespace { + +constinit thread_local std::atomic deferralDepth{0}; +constinit thread_local std::atomic deferredSignal{0}; + +class DeliveryAllowance { +public: + DeliveryAllowance() : depth(deferralDepth.exchange(0)) { DeliverDeferred(); } + ~DeliveryAllowance() { deferralDepth.store(depth); } + DeliveryAllowance(const DeliveryAllowance&) = delete; + DeliveryAllowance& operator=(const DeliveryAllowance&) = delete; + +private: + int depth; +}; + +} + +DeliveryDeferral::DeliveryDeferral() { + deferralDepth.fetch_add(1); +} + +DeliveryDeferral::~DeliveryDeferral() { + deferralDepth.fetch_sub(1); +} + +void DeliverDeferred() { + if (deferralDepth.load() != 0) return; + const int signum = deferredSignal.exchange(0); + if (signum != 0) pthread_kill(pthread_self(), signum); +} + +bool DeferDelivery(int signum) { + if (deferralDepth.load() == 0) return false; + deferredSignal.store(signum); + return true; +} + +#ifdef __APPLE__ +SingleWaiterCondition::SingleWaiterCondition() : semaphore(dispatch_semaphore_create(0)) { + if (semaphore == nullptr) throw std::runtime_error("TimedWait: cannot create a wait semaphore"); +} + +SingleWaiterCondition::~SingleWaiterCondition() { + dispatch_release(semaphore); +} + +void SingleWaiterCondition::NotifyOne() { + dispatch_semaphore_signal(semaphore); +} + +bool SingleWaiterCondition::waitSignal(const std::uint64_t* deadlineNanos) { + DeliveryAllowance allowance; + if (deadlineNanos == nullptr) return dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER) == 0; + const std::uint64_t now = NowNanos(); + const std::uint64_t remaining = *deadlineNanos > now ? *deadlineNanos - now : 0; + const dispatch_time_t deadline = remaining >= static_cast(std::numeric_limits::max()) ? DISPATCH_TIME_FOREVER : dispatch_time(DISPATCH_TIME_NOW, static_cast(remaining)); + return dispatch_semaphore_wait(semaphore, deadline) == 0; +} +#else +SingleWaiterCondition::SingleWaiterCondition() { + if (sem_init(&semaphore, 0, 0) != 0) throw std::system_error(errno, std::generic_category(), "TimedWait: cannot create a wait semaphore"); +} + +SingleWaiterCondition::~SingleWaiterCondition() { + sem_destroy(&semaphore); +} + +void SingleWaiterCondition::NotifyOne() { + sem_post(&semaphore); +} + +bool SingleWaiterCondition::waitSignal(const std::uint64_t* deadlineNanos) { + DeliveryAllowance allowance; + timespec deadline{}; + if (deadlineNanos != nullptr) deadline = {static_cast(*deadlineNanos / 1000000000ULL), static_cast(*deadlineNanos % 1000000000ULL)}; + for (;;) { + const int result = deadlineNanos == nullptr ? sem_wait(&semaphore) : sem_clockwait(&semaphore, CLOCK_MONOTONIC, &deadline); + if (result == 0) return true; + if (errno == ETIMEDOUT) return false; + if (errno != EINTR) throw std::system_error(errno, std::generic_category(), "TimedWait: cannot wait for a wait semaphore"); + } +} +#endif + #endif void BindThreadWaitState(std::atomic* state) { diff --git a/core/libs/prx/libkernel/Time/include/TimedWait.hpp b/core/libs/prx/libkernel/Time/include/TimedWait.hpp index 2f1725819d..9ad4e09f03 100644 --- a/core/libs/prx/libkernel/Time/include/TimedWait.hpp +++ b/core/libs/prx/libkernel/Time/include/TimedWait.hpp @@ -7,6 +7,12 @@ #include #include +#ifdef __APPLE__ +#include +#elif !defined(_WIN32) +#include +#endif + namespace TimedWait { bool Coarse(); @@ -21,9 +27,6 @@ void WakeChangeSleepers(); struct Waiter; -#ifdef _WIN32 -constexpr unsigned PARK_SPINS = 1000; - class DeliveryDeferral { public: DeliveryDeferral(); @@ -33,6 +36,10 @@ public: }; void DeliverDeferred(); + +#ifdef _WIN32 +constexpr unsigned PARK_SPINS = 1000; + void SpinPause(); Waiter* BeginPark(const void* key); bool FinishPark(const void* key, Waiter* waiter, const std::uint64_t* deadlineNanos); @@ -132,6 +139,51 @@ private: std::condition_variable_any coarse; }; +#ifdef _WIN32 +using SingleWaiterCondition = Condition; +#else +bool DeferDelivery(int signum); + +class SingleWaiterCondition { +public: + SingleWaiterCondition(); + ~SingleWaiterCondition(); + SingleWaiterCondition(const SingleWaiterCondition&) = delete; + SingleWaiterCondition& operator=(const SingleWaiterCondition&) = delete; + + void NotifyOne(); + + template + void Wait(TLock& lock, TPredicate predicate) { + while (!predicate()) { + lock.unlock(); + waitSignal(nullptr); + lock.lock(); + } + } + + template + bool WaitUntil(TLock& lock, std::uint64_t deadlineNanos, TPredicate predicate) { + while (!predicate()) { + lock.unlock(); + const bool signaled = waitSignal(&deadlineNanos); + lock.lock(); + if (!signaled) return predicate(); + } + return true; + } + +private: + bool waitSignal(const std::uint64_t* deadlineNanos); + +#ifdef __APPLE__ + dispatch_semaphore_t semaphore; +#else + sem_t semaphore; +#endif +}; +#endif + } // namespace TimedWait #endif diff --git a/core/libs/prx/libkernel_sys/Export.Discovery.cpp b/core/libs/prx/libkernel_sys/Export.Discovery.cpp index 0d39de44f2..122ab52e24 100644 --- a/core/libs/prx/libkernel_sys/Export.Discovery.cpp +++ b/core/libs/prx/libkernel_sys/Export.Discovery.cpp @@ -1151,11 +1151,6 @@ int APS5_VABI sceKernelGetSubsysSubId(void) { return 0; } -int APS5_VABI sceKernelGetSystemExVersion(void) { - NotImplemented_nid_no_patch(__func__); - return 0; -} - int APS5_VABI sceKernelGetSystemLevelDebuggerModeForRcmgr(void) { NotImplemented_nid_no_patch(__func__); return 0; @@ -1171,11 +1166,6 @@ int APS5_VABI sceKernelGetThreadName(void) { return 0; } -int APS5_VABI sceKernelGetTraceMemoryStats(void) { - NotImplemented_nid_no_patch(__func__); - return 0; -} - int APS5_VABI sceKernelGetUniversalMode(void) { NotImplemented_nid_no_patch(__func__); return 0; @@ -2334,26 +2324,11 @@ int APS5_VABI sched_setscheduler_nid_postfix(void) { return 0; } -int APS5_VABI sem_close_nid_postfix(void) { - NotImplemented_nid_no_patch(__func__); - return 0; -} - -int APS5_VABI sem_open_nid_postfix(void) { - NotImplemented_nid_no_patch(__func__); - return 0; -} - int APS5_VABI sem_setname_nid_postfix(void) { NotImplemented_nid_no_patch(__func__); return 0; } -int APS5_VABI sem_unlink_nid_postfix(void) { - NotImplemented_nid_no_patch(__func__); - return 0; -} - int APS5_VABI set_phys_fmem_limit_nid_postfix(void) { NotImplemented_nid_no_patch(__func__); return 0; @@ -2409,16 +2384,6 @@ int APS5_VABI sysKernelGetIntdevModeForRcmgr_nid_postfix(void) { return 0; } -int APS5_VABI sysKernelGetLowerLimitSysexVersion_nid_postfix(void) { - NotImplemented_nid_no_patch(__func__); - return 0; -} - -int APS5_VABI sysKernelGetLowerLimitUpdVersion_nid_postfix(void) { - NotImplemented_nid_no_patch(__func__); - return 0; -} - int APS5_VABI sysKernelGetManufacturingMode_nid_postfix(void) { NotImplemented_nid_no_patch(__func__); return 0; diff --git a/core/libs/prx/libkernel_sys/Export.cpp b/core/libs/prx/libkernel_sys/Export.cpp index 3a5bda20b0..cd60e58230 100644 --- a/core/libs/prx/libkernel_sys/Export.cpp +++ b/core/libs/prx/libkernel_sys/Export.cpp @@ -581,6 +581,24 @@ int APS5_VABI openat_nid_postfix(int fd, const char* path, int flags, ...) { return result; } +int APS5_VABI _openat_nid_postfix(int fd, const char* path, int flags, ...) { + int mode = 0; + if (flags & SCE_KERNEL_O_CREAT) { +#ifdef _WIN32 + __builtin_sysv_va_list arguments; + __builtin_sysv_va_start(arguments, flags); + mode = __builtin_va_arg(arguments, int); + __builtin_sysv_va_end(arguments); +#else + std::va_list arguments; + va_start(arguments, flags); + mode = va_arg(arguments, int); + va_end(arguments); +#endif + } + return openat_nid_postfix(fd, path, flags, mode); +} + int APS5_VABI mkdir_nid_postfix(const char* path, std::uint16_t mode); int APS5_VABI mkdirat_nid_postfix(int fd, const char* path, std::uint16_t mode) { @@ -600,6 +618,7 @@ int APS5_VABI mkdirat_nid_postfix(int fd, const char* path, std::uint16_t mode) if (code == ERROR_ALREADY_EXISTS || code == ERROR_FILE_EXISTS) return static_cast(Fail(GuestEexist)); if (code == ERROR_ACCESS_DENIED) return static_cast(Fail(GuestEperm)); if (code == ERROR_FILENAME_EXCED_RANGE) return static_cast(Fail(GuestEnametoolong)); + if (code == ERROR_CANT_RESOLVE_FILENAME) return static_cast(Fail(GuestEloop)); if (code == ERROR_PATH_NOT_FOUND) { if (HasNonDirectoryPrefix(native)) return static_cast(Fail(GuestEnotdir)); return static_cast(Fail(GuestEnoent)); diff --git a/core/libs/tests/AppArchiveFixture.hpp b/core/libs/tests/AppArchiveFixture.hpp index b6c5e4529c..234c647e3c 100644 --- a/core/libs/tests/AppArchiveFixture.hpp +++ b/core/libs/tests/AppArchiveFixture.hpp @@ -20,8 +20,11 @@ inline std::vector Pattern(std::size_t size) { } inline std::filesystem::path EnterFreshRoot(const char* name) { - const auto root = std::filesystem::temp_directory_path() / - (std::string(name) + "-" + std::to_string(std::chrono::steady_clock::now().time_since_epoch().count())); + std::filesystem::path root; + do { + root = std::filesystem::temp_directory_path() / + (std::string(name) + "-" + std::to_string(std::chrono::steady_clock::now().time_since_epoch().count())); + } while (!std::filesystem::create_directory(root)); std::filesystem::create_directories(root / "app0" / "sce_sys"); std::filesystem::current_path(root); return std::filesystem::canonical(root); diff --git a/core/libs/tests/ExceptionRuntime.cpp b/core/libs/tests/ExceptionRuntime.cpp index 54111f196c..8f8b1c41fe 100644 --- a/core/libs/tests/ExceptionRuntime.cpp +++ b/core/libs/tests/ExceptionRuntime.cpp @@ -29,11 +29,13 @@ extern "C" void __cxa_rethrow_primary_exception_nid_postfix(void*); extern "C" [[noreturn]] void _ZSt14_Xout_of_rangePKc_nid_postfix(const char*); extern "C" [[noreturn]] void _ZSt13_Xrange_errorPKc_nid_postfix(const char*); extern "C" [[noreturn]] void _ZNSt8__sce_v219_Xbad_function_callEv_nid_postfix(); +extern "C" [[noreturn]] void _ZSt19_Xbad_function_callv_nid_postfix(); extern "C" [[noreturn]] void __cxa_bad_cast_nid_postfix(); extern "C" [[noreturn]] void _ZSt19_Throw_bad_weak_ptrv_nid_postfix(); extern "C" [[noreturn]] void _ZSt26__throw_bad_variant_accessv_nid_postfix(); extern "C" [[noreturn]] void _ZSt26_Throw_bad_optional_accessv_nid_postfix(); extern "C" [[noreturn]] void _ZNKSt9exception6_RaiseEv_nid_postfix(const void*); +extern "C" [[noreturn]] void _ZNKSt8bad_cast8_DoraiseEv_nid_postfix(const void*); extern "C" void* __cxa_vec_new3_nid_postfix(std::size_t, std::size_t, std::size_t, void(*)(void*), void(*)(void*), void*(*)(std::size_t), void(*)(void*, std::size_t)); extern "C" void __cxa_vec_delete3_nid_postfix(void*, std::size_t, std::size_t, void(*)(void*), void(*)(void*, std::size_t)); extern "C" _Unwind_Reason_Code _Unwind_Backtrace_nid_postfix(_Unwind_Trace_Fn, void*); @@ -248,8 +250,17 @@ int main() { catch (const std::range_error&) {} bool badFunctionCall = false; try { _ZNSt8__sce_v219_Xbad_function_callEv_nid_postfix(); } - catch (const std::bad_function_call&) { badFunctionCall = true; } + catch (const std::bad_function_call&) { std::abort(); } + catch (const std::exception& value) { + if (std::strcmp(typeid(value).name(), "NSt8__sce_v217bad_function_callE") != 0) std::abort(); + if (std::strcmp(value.what(), "bad function call") != 0) std::abort(); + badFunctionCall = true; + } if (!badFunctionCall) std::abort(); + bool plainBadFunctionCall = false; + try { _ZSt19_Xbad_function_callv_nid_postfix(); } + catch (const std::bad_function_call&) { plainBadFunctionCall = true; } + if (!plainBadFunctionCall) std::abort(); try { __cxa_bad_cast_nid_postfix(); } catch (const std::exception& value) { assert(value.what() != nullptr); } try { _ZSt19_Throw_bad_weak_ptrv_nid_postfix(); std::abort(); } @@ -262,6 +273,10 @@ int main() { catch (const std::invalid_argument&) {} try { _ZNKSt9exception6_RaiseEv_nid_postfix(&object); assert(false); } catch (const std::exception& value) { assert(std::strcmp(value.what(), "std::exception") == 0); } + try { _ZNKSt8bad_cast8_DoraiseEv_nid_postfix(nullptr); assert(false); } + catch (const std::invalid_argument&) {} + try { _ZNKSt8bad_cast8_DoraiseEv_nid_postfix(&object); assert(false); } + catch (const std::bad_cast&) {} CheckRtti(); CheckForeign(); CheckStaticInitialization(); diff --git a/core/libs/tests/Fiber.cpp b/core/libs/tests/Fiber.cpp index 2267ee2497..f6b532900b 100644 --- a/core/libs/tests/Fiber.cpp +++ b/core/libs/tests/Fiber.cpp @@ -24,6 +24,9 @@ std::int32_t APS5_VABI sceFiberGetThreadFramePointerAddress(std::uint64_t*); std::int32_t APS5_VABI _sceFiberGetThreadFramePointerAddress(std::uint64_t*); std::int32_t APS5_VABI sceFiberStartContextSizeCheck(std::uint32_t); std::int32_t APS5_VABI sceFiberStopContextSizeCheck(void); +std::int32_t APS5_VABI _sceFiberAttachContextAndRun(FiberObject*, void*, std::uint64_t, std::uint64_t, std::uint64_t*); +std::int32_t APS5_VABI _sceFiberAttachContextAndSwitch(FiberObject*, void*, std::uint64_t, std::uint64_t, std::uint64_t*); +std::int32_t APS5_VABI _sceFiberInitializeWithInternalOptionImpl(FiberObject*, const char*, Entry, std::uint64_t, void*, std::uint64_t, const void*, std::uint32_t, std::uint32_t); } static void Require(bool value, const char* what) { @@ -33,6 +36,8 @@ static void Require(bool value, const char* what) { namespace { constexpr std::int32_t FiberErrorNull = static_cast(0x80590001); +constexpr std::int32_t FiberErrorAlignment = static_cast(0x80590002); +constexpr std::int32_t FiberErrorRange = static_cast(0x80590003); constexpr std::int32_t FiberErrorInvalid = static_cast(0x80590004); constexpr std::int32_t FiberErrorPermission = static_cast(0x80590005); constexpr std::int32_t FiberErrorState = static_cast(0x80590006); @@ -235,6 +240,91 @@ void RunOnLargeContext() { Require(sceFiberFinalize(fiber) == 0, "finalize large fiber"); } +alignas(16) std::array g_attachedContext; +alignas(16) std::array g_attachedSmallContext; +FiberStorage g_attachedStorage; +FiberStorage g_attachedTargetStorage; +auto* const g_attached = reinterpret_cast(&g_attachedStorage); +auto* const g_attachedTarget = reinterpret_cast(&g_attachedTargetStorage); + +void APS5_VABI AttachedTargetEntry(std::uint64_t argOnInitialize, std::uint64_t argOnRun) { + Require(argOnInitialize == 44 && argOnRun == 500, "attached switch target arguments"); + CheckActiveContext(g_attachedTarget, g_attachedSmallContext.size()); + std::uint64_t received = 0; + Require(sceFiberSwitch(g_attached, 600, &received) == 0, "switch back from the attached target"); + Require(false, "attached switch target resumed after its last switch"); +} + +void APS5_VABI AttachedEntry(std::uint64_t argOnInitialize, std::uint64_t argOnRun) { + Require(argOnInitialize == 33 && argOnRun == 400, "attached fiber arguments"); + FiberObject* self = nullptr; + Require(sceFiberGetSelf(&self) == 0 && self == g_attached, "attached fiber self"); + CheckActiveContext(g_attached, g_attachedContext.size()); + Require(_sceFiberAttachContextAndRun(g_attachedTarget, g_attachedSmallContext.data(), g_attachedSmallContext.size(), 0, nullptr) == FiberErrorPermission, "attach and run inside a fiber"); + std::uint64_t received = 0; + Require(_sceFiberAttachContextAndSwitch(g_attached, g_attachedContext.data(), g_attachedContext.size(), 0, nullptr) == FiberErrorState, "attach and switch to self"); + Require(_sceFiberAttachContextAndSwitch(g_attachedTarget, g_attachedSmallContext.data() + 8, g_attachedSmallContext.size() - 16, 500, nullptr) == FiberErrorAlignment, "attach and switch with a misaligned context"); + Require(_sceFiberAttachContextAndSwitch(g_attachedTarget, g_attachedSmallContext.data(), g_attachedSmallContext.size(), 500, &received) == 0 && received == 600, "attach and switch"); + Require(sceFiberReturnToThread(700, &received) == 0 && received == 800, "attached fiber resumed"); + for (;;) sceFiberReturnToThread(received + 1, &received); +} + +void APS5_VABI DefaultFpuEntry(std::uint64_t, std::uint64_t) { + std::uint16_t control = 0; + asm volatile("fnstcw %0" : "=m"(control)); + std::uint64_t received = 0; + for (;;) sceFiberReturnToThread((static_cast(control) << 32) | _mm_getcsr(), &received); +} + +void RunAttachedContext() { + Require(_sceFiberInitializeImpl_nid_postfix(g_attached, "attached", AttachedEntry, 33, nullptr, 0, nullptr, 0) == 0, "initialize without a context"); + Require(_sceFiberInitializeImpl_nid_postfix(g_attachedTarget, "attached target", AttachedTargetEntry, 44, nullptr, 0, nullptr, 0) == 0, "initialize switch target without a context"); + FiberInfo info{}; + info.size = sizeof(info); + Require(sceFiberGetInfo(g_attached, &info) == 0 && info.addr_context == nullptr && info.size_context == 0, "fiber without a context reports none"); + Require(_sceFiberAttachContextAndRun(nullptr, g_attachedContext.data(), g_attachedContext.size(), 0, nullptr) == FiberErrorNull, "attach and run a null fiber"); + Require(_sceFiberAttachContextAndRun(g_attached, nullptr, g_attachedContext.size(), 0, nullptr) == FiberErrorInvalid, "attach a null context with a size"); + Require(_sceFiberAttachContextAndRun(g_attached, g_attachedContext.data(), 0, 0, nullptr) == FiberErrorRange, "attach a context without a size"); + Require(_sceFiberAttachContextAndRun(g_attached, g_attachedContext.data(), 256, 0, nullptr) == FiberErrorRange, "attach a context below the minimum size"); + Require(_sceFiberAttachContextAndRun(g_attached, g_attachedContext.data(), 1000, 0, nullptr) == FiberErrorAlignment, "attach a context of an unaligned size"); + Require(_sceFiberAttachContextAndSwitch(g_attached, g_attachedContext.data(), g_attachedContext.size(), 0, nullptr) == FiberErrorPermission, "attach and switch outside a fiber"); + std::uint64_t returned = 0; + Require(_sceFiberAttachContextAndRun(g_attached, g_attachedContext.data(), g_attachedContext.size(), 400, &returned) == 0 && returned == 700, "attach and run"); + Require(_sceFiberAttachContextAndRun(g_attached, g_attachedContext.data(), g_attachedContext.size(), 800, &returned) == FiberErrorInvalid, "attach to a fiber that has a context"); + Require(_sceFiberAttachContextAndRun(g_attached, nullptr, 0, 800, &returned) == 0 && returned == 801, "run an attached fiber without a new context"); + Require(sceFiberRun_nid_postfix(g_attached, 802, &returned) == 0 && returned == 803, "run an attached fiber"); + Require(sceFiberFinalize(g_attachedTarget) == 0, "finalize a suspended attached target"); + Require(sceFiberFinalize(g_attached) == 0, "finalize attached fiber"); + Require(_sceFiberInitializeImpl_nid_postfix(g_attachedTarget, "attached target", AttachedTargetEntry, 44, nullptr, 0, nullptr, 0) == 0, "reinitialize attached target"); + Require(sceFiberFinalize(g_attachedTarget) == 0, "finalize fiber without a context"); +} + +void RunInternalOptions() { + auto* fiber = reinterpret_cast(&g_checkedStorage); + Require(_sceFiberInitializeWithInternalOptionImpl(nullptr, "options", CheckedEntry, 0, g_checkedContext.data(), g_checkedContext.size(), nullptr, 0, 0) == FiberErrorNull, "internal option null fiber"); + Require(_sceFiberInitializeWithInternalOptionImpl(fiber, "options", CheckedEntry, 0, g_checkedContext.data(), 256, nullptr, 0, 0) == FiberErrorRange, "internal option small context"); + Require(_sceFiberInitializeWithInternalOptionImpl(fiber, "options", CheckedEntry, 0, g_checkedContext.data(), g_checkedContext.size(), nullptr, 0x1, 0) == 0, "internal option without object registration"); + FiberInfo info{}; + info.size = sizeof(info); + Require(sceFiberGetInfo(fiber, &info) == 0 && info.size_context_margin == ~0ull, "no margin without the size check flag"); + Require(sceFiberFinalize(fiber) == 0, "finalize internal option fiber"); + Require(_sceFiberInitializeWithInternalOptionImpl(fiber, "options", CheckedEntry, 0, g_checkedContext.data(), g_checkedContext.size(), nullptr, 0x10, 0) == 0, "internal option size check"); + Require(sceFiberGetInfo(fiber, &info) == 0 && info.size_context_margin == g_checkedContext.size(), "size check flag fills the context"); + Require(sceFiberRun_nid_postfix(fiber, 0, nullptr) == 0, "run size checked fiber"); + Require(sceFiberGetInfo(fiber, &info) == 0 && info.size_context_margin < g_checkedContext.size() - 4096, "size check flag measures the margin"); + Require(sceFiberFinalize(fiber) == 0, "finalize size checked fiber"); + + Require(_sceFiberInitializeWithInternalOptionImpl(fiber, "fpu", DefaultFpuEntry, 0, nullptr, 0, nullptr, 0x100, 0) == 0, "internal option fpu defaults without a context"); + const auto threadCsr = _mm_getcsr(); + _mm_setcsr((threadCsr & ~0x8040u) | 0x6000u); + std::uint64_t returned = 0; + Require(_sceFiberAttachContextAndRun(fiber, g_checkedContext.data(), g_checkedContext.size(), 0, &returned) == 0, "run fpu defaults fiber"); + _mm_setcsr(threadCsr); + Require((returned & 0xffffffffu) == 0x9fc0u, "fpu defaults flag starts with the default MXCSR"); + Require((returned >> 32) == 0x037fu, "fpu defaults flag starts with the default x87 control word"); + Require(sceFiberFinalize(fiber) == 0, "finalize fpu defaults fiber"); +} + [[gnu::noinline]] std::int32_t RunFirst(std::uint64_t* returned) { g_threadFramePointer = reinterpret_cast(__builtin_frame_address(0)); const std::int32_t result = sceFiberRun_nid_postfix(g_first, 100, returned); @@ -285,4 +375,6 @@ int main() { RunHostCallOnSmallContext(); RunFrameChain(); RunOnLargeContext(); + RunAttachedContext(); + RunInternalOptions(); } diff --git a/core/libs/tests/Formatting.cpp b/core/libs/tests/Formatting.cpp index 8d80547b6a..ea4b2ecaa8 100644 --- a/core/libs/tests/Formatting.cpp +++ b/core/libs/tests/Formatting.cpp @@ -103,8 +103,8 @@ static void CheckSizeLimits() { *__error_nid_postfix() = 0; const int result = wide ? format(output, oversized, "%ls%n", u"x", &consumed) : format(output, oversized, "x%n", &consumed); - Require(result == -1 && *__error_nid_postfix() == 84 && output[0] == 0 && consumed == -1); - Require(std::all_of(output + 1, output + sizeof(output), [](char value) { return value == 'Z'; })); + Require(result == 1 && *__error_nid_postfix() == 0 && std::strcmp(output, "x") == 0 && consumed == 1); + Require(std::all_of(output + 2, output + sizeof(output), [](char value) { return value == 'Z'; })); } } for (const std::size_t capacity : {std::size_t{0}, std::size_t{1}, static_cast(INT_MAX), static_cast(INT_MAX) + 1}) { @@ -123,8 +123,8 @@ static void CheckSizeLimits() { static bool CheckWidePrecision() { const char16_t input[] = u"A\u00e9\u20ac\U0001f600Z"; const char* expected[] = {"", "A", "A", "A\xc3\xa9", "A\xc3\xa9", "A\xc3\xa9", "A\xc3\xa9\xe2\x82\xac", - "A\xc3\xa9\xe2\x82\xac", "A\xc3\xa9\xe2\x82\xac", "A\xc3\xa9\xe2\x82\xac", - "A\xc3\xa9\xe2\x82\xac\xf0\x9f\x98\x80", "A\xc3\xa9\xe2\x82\xac\xf0\x9f\x98\x80Z"}; + "A\xc3\xa9\xe2\x82\xac", "A\xc3\xa9\xe2\x82\xac", "A\xc3\xa9\xe2\x82\xac\xed\xa0\xbd", + "A\xc3\xa9\xe2\x82\xac\xed\xa0\xbd", "A\xc3\xa9\xe2\x82\xac\xed\xa0\xbd"}; bool correct = true; for (int precision = 0; precision < 12; ++precision) { char output[32]; @@ -139,7 +139,8 @@ static bool CheckWidePrecision() { char output[32]; Require(snprintf_nid_postfix(output, sizeof(output), "[%5.1ls]", u"\u00e9") == 7 && std::strcmp(output, "[ ]") == 0); Require(snprintf_nid_postfix(output, sizeof(output), "[%-5.2ls]", u"\u00e9") == 7 && std::strcmp(output, "[\xc3\xa9 ]") == 0); - Require(FormatList(output, sizeof(output), "%.3ls", u"\U0001f600") == 0 && output[0] == 0); + Require(FormatList(output, sizeof(output), "%.3ls", u"\U0001f600") == 3 && std::strcmp(output, "\xed\xa0\xbd") == 0); + Require(FormatList(output, sizeof(output), "%ls", u"\U0001f600") == 6 && std::strcmp(output, "\xed\xa0\xbd\xed\xb8\x80") == 0); Require(FormatList(output, sizeof(output), "%ls[% u][% 4u][%+ lu][%- 3hhu][% x][%+o]", u"", 5u, 5u, 5ULL, 5u, 255u, 8u) == 25 && std::strcmp(output, "[5][ 5][5][5 ][ff][10]") == 0); Require(FormatList(output, sizeof(output), "%.*ls:%d", -1, u"\u00e9", 7) == 4 && std::strcmp(output, "\xc3\xa9:7") == 0); @@ -152,10 +153,11 @@ static bool CheckWidePrecision() { Require(snprintf_nid_postfix(output, sizeof(output), "[%ls]", static_cast(nullptr)) == 8 && std::strcmp(output, "[(null)]") == 0); Require(snprintf_nid_postfix(output, sizeof(output), "a%lcb%C", 0, 0) == 4 && std::memcmp(output, "a\0b\0", 5) == 0); Require(FormatList(output, sizeof(output), "%%ls:%2$d:%1$d", 3, 7) == 7 && std::strcmp(output, "%ls:7:3") == 0); - Require(FormatList(output, sizeof(output), "%ls[%f][%+f][% F][%5.1e]", u"", -NAN, -NAN, NAN, -NAN) == 22 - && std::strcmp(output, "[nan][nan][NAN][ nan]") == 0); - Require(FormatList(output, sizeof(output), "%ls[%-6g][%06a][%LG][%+f]", u"", NAN, -NAN, -static_cast(NAN), -INFINITY) == 27 - && std::strcmp(output, "[nan ][ nan][NAN][-inf]") == 0); + Require(FormatList(output, sizeof(output), "%ls[%f][%+f][% F][%5.1e]", u"", -NAN, -NAN, NAN, -NAN) == 25 + && std::strcmp(output, "[-nan][-nan][ NAN][ -nan]") == 0); + Require(FormatList(output, sizeof(output), "%ls[%-6g][%06a][%LG][%+f]", u"", NAN, -NAN, -static_cast(NAN), -INFINITY) == 28 + && std::strcmp(output, "[nan ][ -nan][-NAN][-inf]") == 0); + Require(FormatList(output, sizeof(output), "%ls[%+f][%8.3f|]", u"", NAN, -NAN) == 17 && std::strcmp(output, "[+nan][ -nan|]") == 0); for (std::size_t capacity = 0; capacity <= 6; ++capacity) { std::memset(output, '!', sizeof(output)); int count = -1; @@ -238,16 +240,7 @@ __attribute__((noinline)) static void APS5_VABI RunChecks() { Require(FormatList(buffer, sizeof(buffer), "%2$ls|%1$C|%3$d", 0x41, u"\u00e9", 7) == 6 && std::strcmp(buffer, "\xc3\xa9|A|7") == 0); Require(FormatList(buffer, sizeof(buffer), "%2$d %d %ls|%*5$d|%1$d", 1, 2, 3, u"x", 4) == 12 && std::strcmp(buffer, "2 3 x| 4|1") == 0); Require(FormatList(buffer, sizeof(buffer), "[%*3$d]%ls", 5, u"", 3) == 5 && std::strcmp(buffer, "[ 5]") == 0); - const char* const invalidPositions[] = {"%0$d%ls", "%100$d%ls", "%*0$d%ls"}; - for (const char* format : invalidPositions) { - bool threw = false; - try { - FormatList(buffer, sizeof(buffer), format, 1, u""); - } catch (const std::invalid_argument&) { - threw = true; - } - Require(threw); - } + Require(FormatList(buffer, sizeof(buffer), "%100$d %2$d%1$ls", u"", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100) == 4 && std::strcmp(buffer, "99 1") == 0); char expected[1024]; std::snprintf(expected, sizeof(expected), "%#08x %.3e %a %g %llx", 42u, 1.25, 1.25, 1.25, static_cast(reinterpret_cast(buffer))); @@ -364,6 +357,21 @@ __attribute__((noinline)) static void APS5_VABI RunChecks() { } char narrow[8] = {}; Require(sscanf_nid_postfix("ab%", "%7[^%ls]", narrow) == 1 && std::strcmp(narrow, "ab") == 0); +#endif +#ifdef _WIN32 + char freebsd[64]; + Require(snprintf_nid_postfix(freebsd, sizeof(freebsd), "%qd|%qu|%qx|%qo") == 15 && + std::strcmp(freebsd, "%qd|%qu|%qx|%qo") == 0); + Require(snprintf_nid_postfix(freebsd, sizeof(freebsd), "%qd|%d", 7) == 5 && std::strcmp(freebsd, "%qd|7") == 0); + Require(snprintf_nid_postfix(freebsd, sizeof(freebsd), "%D|%hD|%O|%U|%jD") == 14 && + std::strcmp(freebsd, "%D|%D|%O|%U|%D") == 0); + Require(snprintf_nid_postfix(freebsd, sizeof(freebsd), "[%5%][%-5%][%y][%5y][%-3y][%hhz]") == 34 && + std::strcmp(freebsd, "[ %][% ][%y][ %y][%y ][%z]") == 0); + Require(snprintf_nid_postfix(freebsd, sizeof(freebsd), "[%#y][%.2y][%-#4.1y]") == 14 && + std::strcmp(freebsd, "[%y][%y][%y ]") == 0); + bool incomplete = false; + try { snprintf_nid_postfix(freebsd, sizeof(freebsd), "100%"); } catch (const std::invalid_argument&) { incomplete = true; } + Require(incomplete); #endif long long count = -1; int smallCount = -1; @@ -372,11 +380,12 @@ __attribute__((noinline)) static void APS5_VABI RunChecks() { Require(snprintf_nid_postfix(buffer, sizeof(buffer), "a%cb", 0) == 3); Require(buffer[0] == 'a' && buffer[1] == 0 && buffer[2] == 'b' && buffer[3] == 0); *__error_nid_postfix() = 35; - Require(snprintf_nid_postfix(buffer, sizeof(buffer), "[%m][%.8m][%-10.5m][%d]", 7) == 59); - Require(std::strcmp(buffer, "[Resource temporarily unavailable][Resource][Resou ][7]") == 0); + Require(snprintf_nid_postfix(buffer, sizeof(buffer), "[%m][%.8m][%-10.5m][%d]", 7) == 23); + Require(std::strcmp(buffer, "[%m][%m][%m ][7]") == 0); Require(*__error_nid_postfix() == 35); - *__error_nid_postfix() = 63; - Require(FormatList(buffer, sizeof(buffer), "%s: %m", "open") == 24 && std::strcmp(buffer, "open: File name too long") == 0); + Require(FormatList(buffer, sizeof(buffer), "%s: %m", "open") == 8 && std::strcmp(buffer, "open: %m") == 0); + Require(snprintf_nid_postfix(buffer, sizeof(buffer), "[%5k][%-3y][%lk][%#.2k][%y%d][%0$d]", 42) == 31); + Require(std::strcmp(buffer, "[ %k][%y ][%k][%k][%y42][%$d]") == 0); Require(printf_nid_postfix("printf: %d %.1f\n", 7, 2.5) == 14); Require(libc_printf_nid_postfix("libc_printf: %s\n", "OK") == 16); Require(libc_printf_nid_postfix("%d %d %d %d %d %d %d\n", 1, 22, 333, 4444, 55555, 666666, 7777777) == 35); diff --git a/core/libs/tests/GuestAio.cpp b/core/libs/tests/GuestAio.cpp index 161b1e0039..616f141ce4 100644 --- a/core/libs/tests/GuestAio.cpp +++ b/core/libs/tests/GuestAio.cpp @@ -69,9 +69,9 @@ static void CheckBadPositionedDescriptors() { KernelAioResult result{-1, 0}; KernelAioRwRequest request{0, count, &buffer, &result, descriptor}; std::int32_t id = 0; - Check((write ? sceKernelAioSubmitWriteCommands(&request, 1, 0, &id) - : sceKernelAioSubmitReadCommands(&request, 1, 0, &id)) == 0); - Check(result.state == 4 && result.return_value == SCE_KERNEL_ERROR_EBADF); + Check((write ? sceKernelAioSubmitWriteCommands(&request, 1, 1, &id) + : sceKernelAioSubmitReadCommands(&request, 1, 1, &id)) == 0); + Check(result.state == 3 && result.return_value == SCE_KERNEL_ERROR_EBADF); std::int32_t removed = -1; Check(sceKernelAioDeleteRequest(id, &removed) == 0); } @@ -109,26 +109,42 @@ int main(int argc, char** argv) { Check(sceKernelAioSetParam(&schedulingParams[2], 128, 128, 0, 0x1000000, 0x1000000) == 0); Check(schedulingParams[2].schedulingWindowSize == 128 && schedulingParams[2].delayedCountLimit == 128); Check(schedulingParams[2].enableSplit == 0 && schedulingParams[2].splitSize == 0x1000000 && schedulingParams[2].splitChunkSize == 0x1000000); - const auto setParamThrows = [&](std::int32_t window, std::int32_t delayed, std::uint32_t split, std::uint32_t size, std::uint32_t chunk) { + const auto setParamRefuses = [&](std::int32_t window, std::int32_t delayed, std::uint32_t split, std::uint32_t size, std::uint32_t chunk) { KernelAioSchedulingParam untouchedParam{7, 7, 7, 7, 7}; - bool thrown = false; - try { sceKernelAioSetParam(&untouchedParam, window, delayed, split, size, chunk); } catch (const std::exception&) { thrown = true; } - return thrown && untouchedParam.schedulingWindowSize == 7 && untouchedParam.delayedCountLimit == 7 && + return sceKernelAioSetParam(&untouchedParam, window, delayed, split, size, chunk) == SCE_KERNEL_ERROR_EINVAL && + untouchedParam.schedulingWindowSize == 7 && untouchedParam.delayedCountLimit == 7 && untouchedParam.enableSplit == 7 && untouchedParam.splitSize == 7 && untouchedParam.splitChunkSize == 7; }; - Check(setParamThrows(129, 32, 1, 0x100000, 0x100000)); - Check(setParamThrows(-1, 32, 1, 0x100000, 0x100000)); - Check(setParamThrows(32, 129, 1, 0x100000, 0x100000)); - Check(setParamThrows(32, -1, 1, 0x100000, 0x100000)); - Check(setParamThrows(32, 32, 2, 0x100000, 0x100000)); - Check(setParamThrows(32, 32, 1, 0x1000001, 0x100000)); - Check(setParamThrows(32, 32, 1, 0x100000, 0x1000001)); + Check(setParamRefuses(129, 32, 1, 0x100000, 0x100000)); + Check(setParamRefuses(0x7fffffff, 32, 0, 0, 0)); + Check(setParamRefuses(-1, 32, 1, 0x100000, 0x100000)); + Check(setParamRefuses(32, 129, 1, 0x100000, 0x100000)); + Check(setParamRefuses(32, -1, 1, 0x100000, 0x100000)); + Check(setParamRefuses(32, 32, 2, 0x100000, 0x100000)); + Check(setParamRefuses(32, 32, 0xffffffffu, 0x100000, 0x100000)); + Check(setParamRefuses(32, 32, 1, 0x1000001, 0x100000)); + Check(setParamRefuses(32, 32, 1, 0x100000, 0x1000001)); + Check(setParamRefuses(32, 32, 1, 0, 0x100000)); + Check(setParamRefuses(32, 32, 1, 0x100000, 0)); + KernelAioSchedulingParam accepted{7, 7, 7, 7, 7}; + Check(sceKernelAioSetParam(&accepted, 0, 0, 0, 0, 0) == 0 && accepted.schedulingWindowSize == 0 && accepted.splitSize == 0); + Check(sceKernelAioSetParam(&accepted, 1, 1, 0, 0xffffffffu, 0x1000001) == 0 && accepted.splitSize == 0xffffffffu && accepted.splitChunkSize == 0x1000001); + Check(sceKernelAioSetParam(&accepted, 1, 1, 1, 1, 0x20000) == 0 && accepted.splitSize == 1 && accepted.splitChunkSize == 0x20000); + Check(sceKernelAioSetParam(&accepted, 1, 1, 1, 0x10001, 0x1000000) == 0 && accepted.splitSize == 0x10001); bool nullThrew = false; try { sceKernelAioSetParam(nullptr, 32, 32, 1, 0x100000, 0x100000); } catch (const std::invalid_argument&) { nullThrew = true; } Check(nullThrew); Check(sceKernelAioInitializeImpl(schedulingParams.data(), static_cast(sizeof(schedulingParams))) == 0); - int initParam = 0; - sceKernelAioInitializeParam(&initParam); + std::array initParam{}; + initParam.fill({7, 7, 7, 7, 7}); + sceKernelAioInitializeParam(initParam.data()); + for (int index = 0; index < 2; ++index) { + Check(initParam[index].schedulingWindowSize == 32 && initParam[index].delayedCountLimit == 32 && initParam[index].enableSplit == 1 && + initParam[index].splitSize == 0x100000 && initParam[index].splitChunkSize == 0x100000); + } + Check(initParam[2].schedulingWindowSize == 32 && initParam[2].delayedCountLimit == 32 && initParam[2].enableSplit == 0 && + initParam[2].splitSize == 0 && initParam[2].splitChunkSize == 0); + Check(initParam[3].schedulingWindowSize == 7 && initParam[3].splitChunkSize == 7); const int fd = sceKernelOpen(file.string().c_str(), SCE_KERNEL_O_RDONLY, 0); Check(fd >= 0); std::array first{}; @@ -140,7 +156,7 @@ int main(int argc, char** argv) { {32, second.size(), second.data(), &secondResult, fd}, }; std::int32_t id = 0; - Check(sceKernelAioSubmitReadCommands(requests, 2, 0, &id) == 0); + Check(sceKernelAioSubmitReadCommands(requests, 2, 1, &id) == 0); Check(id > 0); std::int32_t state = 0; Check(sceKernelAioWaitRequest(id, &state, nullptr) == 0); @@ -152,11 +168,11 @@ int main(int argc, char** argv) { KernelAioResult invalidReadResult{-1, 0}; KernelAioRwRequest invalidReadRequest{-1, first.size(), first.data(), &invalidReadResult, fd}; std::int32_t invalidReadId = 0; - Check(sceKernelAioSubmitReadCommands(&invalidReadRequest, 1, 0, &invalidReadId) == 0); + Check(sceKernelAioSubmitReadCommands(&invalidReadRequest, 1, 1, &invalidReadId) == 0); std::int32_t invalidReadState = 0; Check(sceKernelAioWaitRequest(invalidReadId, &invalidReadState, nullptr) == 0); - Check(invalidReadState == 4); - Check(invalidReadResult.state == 4 && invalidReadResult.return_value == SCE_KERNEL_ERROR_EINVAL); + Check(invalidReadState == 3); + Check(invalidReadResult.state == 3 && invalidReadResult.return_value == SCE_KERNEL_ERROR_EINVAL); std::array aggregateReadBuffer{}; KernelAioResult aggregateReadSuccessResult{-1, 0}; KernelAioResult aggregateReadErrorResult{-1, 0}; @@ -165,16 +181,26 @@ int main(int argc, char** argv) { {-1, aggregateReadBuffer.size(), aggregateReadBuffer.data(), &aggregateReadErrorResult, fd}, }; std::int32_t aggregateReadId = 0; - Check(sceKernelAioSubmitReadCommands(aggregateReads, 2, 0, &aggregateReadId) == 0); + Check(sceKernelAioSubmitReadCommands(aggregateReads, 2, 1, &aggregateReadId) == 0); std::int32_t aggregateReadState = 0; Check(sceKernelAioWaitRequest(aggregateReadId, &aggregateReadState, nullptr) == 0); - Check(aggregateReadState == 4); + Check(aggregateReadState == 3); Check(aggregateReadSuccessResult.state == 3 && aggregateReadSuccessResult.return_value == 4); - Check(aggregateReadErrorResult.state == 4 && aggregateReadErrorResult.return_value == SCE_KERNEL_ERROR_EINVAL); + Check(aggregateReadErrorResult.state == 3 && aggregateReadErrorResult.return_value == SCE_KERNEL_ERROR_EINVAL); Check(std::memcmp(aggregateReadBuffer.data(), seed.data() + 4, aggregateReadBuffer.size()) == 0); + std::int32_t pollFirstId = 0; + Check(sceKernelAioSubmitReadCommands(requests, 1, 1, &pollFirstId) == 0); + std::int32_t pollFirst = 0; + std::int32_t cancelFirst = 0; + Check(sceKernelAioCancelRequest(pollFirstId, &cancelFirst) == 0 && cancelFirst == 3); + Check(sceKernelAioPollRequest(pollFirstId, &pollFirst) == 0 && pollFirst == 3); + Check(sceKernelAioPollRequest(pollFirstId, &pollFirst) == 0 && pollFirst == 0x10003); + Check(sceKernelAioCancelRequest(pollFirstId, &cancelFirst) == 0 && cancelFirst == 3); + Check(sceKernelAioDeleteRequest(pollFirstId, &cancelFirst) == 0 && cancelFirst == 0); std::int32_t polled = 0; Check(sceKernelAioPollRequest(id, &polled) == 0); - Check(polled == 3); + Check(polled == 0x10003); + Check(sceKernelAioWaitRequest(id, &state, nullptr) == 0 && state == 0x10003); std::int32_t deleted = -1; Check(sceKernelAioDeleteRequest(id, &deleted) == 0); Check(deleted == 0); @@ -185,18 +211,18 @@ int main(int argc, char** argv) { KernelAioResult writeResult{-1, 0}; KernelAioRwRequest writeRequest{0, written.size(), written.data(), &writeResult, writeFd}; std::int32_t writeId = 0; - Check(sceKernelAioSubmitWriteCommands(&writeRequest, 1, 0, &writeId) == 0); + Check(sceKernelAioSubmitWriteCommands(&writeRequest, 1, 1, &writeId) == 0); std::int32_t writeState = 0; Check(sceKernelAioWaitRequest(writeId, &writeState, nullptr) == 0); Check(writeState == 3 && writeResult.state == 3 && writeResult.return_value == 5); KernelAioResult invalidWriteResult{-1, 0}; KernelAioRwRequest invalidWriteRequest{-1, written.size(), written.data(), &invalidWriteResult, writeFd}; std::int32_t invalidWriteId = 0; - Check(sceKernelAioSubmitWriteCommands(&invalidWriteRequest, 1, 0, &invalidWriteId) == 0); + Check(sceKernelAioSubmitWriteCommands(&invalidWriteRequest, 1, 1, &invalidWriteId) == 0); std::int32_t invalidWriteState = 0; Check(sceKernelAioWaitRequest(invalidWriteId, &invalidWriteState, nullptr) == 0); - Check(invalidWriteState == 4); - Check(invalidWriteResult.state == 4 && invalidWriteResult.return_value == SCE_KERNEL_ERROR_EINVAL); + Check(invalidWriteState == 3); + Check(invalidWriteResult.state == 3 && invalidWriteResult.return_value == SCE_KERNEL_ERROR_EINVAL); std::array aggregateWriteBuffer = {'G', 'O', 'O', 'D'}; KernelAioResult aggregateWriteSuccessResult{-1, 0}; KernelAioResult aggregateWriteErrorResult{-1, 0}; @@ -205,12 +231,12 @@ int main(int argc, char** argv) { {-1, aggregateWriteBuffer.size(), aggregateWriteBuffer.data(), &aggregateWriteErrorResult, writeFd}, }; std::int32_t aggregateWriteId = 0; - Check(sceKernelAioSubmitWriteCommands(aggregateWrites, 2, 0, &aggregateWriteId) == 0); + Check(sceKernelAioSubmitWriteCommands(aggregateWrites, 2, 1, &aggregateWriteId) == 0); std::int32_t aggregateWriteState = 0; Check(sceKernelAioWaitRequest(aggregateWriteId, &aggregateWriteState, nullptr) == 0); - Check(aggregateWriteState == 4); + Check(aggregateWriteState == 3); Check(aggregateWriteSuccessResult.state == 3 && aggregateWriteSuccessResult.return_value == 4); - Check(aggregateWriteErrorResult.state == 4 && aggregateWriteErrorResult.return_value == SCE_KERNEL_ERROR_EINVAL); + Check(aggregateWriteErrorResult.state == 3 && aggregateWriteErrorResult.return_value == SCE_KERNEL_ERROR_EINVAL); Check(sceKernelClose(writeFd) == 0); { std::ifstream stream(file, std::ios::binary); std::string contents; std::getline(stream, contents); Check(contents.rfind("HELLO", 0) == 0); } @@ -220,13 +246,17 @@ int main(int argc, char** argv) { Check(contents.size() == seed.size()); Check(contents.substr(8, aggregateWriteBuffer.size()) == "GOOD"); Check(contents.substr(12, 4) == seed.substr(12, 4)); } - Check(sceKernelAioSubmitReadCommands(nullptr, 1, 0, &id) == SCE_KERNEL_ERROR_EFAULT); - Check(sceKernelAioSubmitReadCommands(requests, 0, 0, &id) == SCE_KERNEL_ERROR_EINVAL); - Check(sceKernelAioSubmitReadCommands(requests, 1, 0, nullptr) == SCE_KERNEL_ERROR_EFAULT); - Check(sceKernelAioWaitRequest(9999, &state, nullptr) == SCE_KERNEL_ERROR_EINVAL); + Check(sceKernelAioSubmitReadCommands(nullptr, 1, 1, &id) == SCE_KERNEL_ERROR_EFAULT); + Check(sceKernelAioSubmitReadCommands(requests, 0, 1, &id) == SCE_KERNEL_ERROR_EINVAL); + Check(sceKernelAioSubmitReadCommands(requests, 1, 1, nullptr) == SCE_KERNEL_ERROR_EFAULT); + state = 7; + Check(sceKernelAioWaitRequest(9999, &state, nullptr) == 0 && state == SCE_KERNEL_ERROR_ESRCH); Check(sceKernelAioWaitRequest(id, nullptr, nullptr) == SCE_KERNEL_ERROR_EFAULT); Check(sceKernelAioPollRequest(id, nullptr) == SCE_KERNEL_ERROR_EFAULT); - Check(sceKernelAioPollRequest(0, &polled) == SCE_KERNEL_ERROR_EINVAL); + for (const std::int32_t unknown : {0, -1, 511, 512, 100000}) { + polled = 7; + Check(sceKernelAioPollRequest(unknown, &polled) == 0 && polled == SCE_KERNEL_ERROR_ESRCH); + } Check(sceKernelAioDeleteRequest(id, nullptr) == SCE_KERNEL_ERROR_EFAULT); const int batchFd = sceKernelOpen(file.string().c_str(), SCE_KERNEL_O_RDWR, 0); Check(batchFd >= 0); @@ -239,13 +269,13 @@ int main(int argc, char** argv) { {-1, batchReadBuffer.size(), batchReadBuffer.data(), &batchReadErrorResult, batchFd}, }; std::int32_t batchReadIds[2] = {0, 0}; - Check(sceKernelAioSubmitReadCommandsMultiple(batchReads, 2, 0, batchReadIds) == 0); + Check(sceKernelAioSubmitReadCommandsMultiple(batchReads, 2, 1, batchReadIds) == 0); Check(batchReadIds[0] > 0 && batchReadIds[1] > 0 && batchReadIds[0] != batchReadIds[1]); Check(batchReadSuccessResult.state == 3 && batchReadSuccessResult.return_value == 4); - Check(batchReadErrorResult.state == 4 && batchReadErrorResult.return_value == SCE_KERNEL_ERROR_EINVAL); + Check(batchReadErrorResult.state == 3 && batchReadErrorResult.return_value == SCE_KERNEL_ERROR_EINVAL); std::int32_t batchReadStates[2] = {0, 0}; Check(sceKernelAioWaitRequests(batchReadIds, 2, batchReadStates, 1, nullptr) == 0); - Check(batchReadStates[0] == 3 && batchReadStates[1] == 4); + Check(batchReadStates[0] == 3 && batchReadStates[1] == 3); std::array batchWriteData = {'A', 'B', 'C', 'D'}; KernelAioResult batchWriteSuccessResult{-1, 0}; KernelAioResult batchWriteErrorResult{-1, 0}; @@ -254,13 +284,13 @@ int main(int argc, char** argv) { {-1, batchWriteData.size(), batchWriteData.data(), &batchWriteErrorResult, batchFd}, }; std::int32_t batchWriteIds[2] = {0, 0}; - Check(sceKernelAioSubmitWriteCommandsMultiple(batchWrites, 2, 0, batchWriteIds) == 0); + Check(sceKernelAioSubmitWriteCommandsMultiple(batchWrites, 2, 1, batchWriteIds) == 0); Check(batchWriteIds[0] > 0 && batchWriteIds[1] > 0 && batchWriteIds[0] != batchWriteIds[1]); Check(batchWriteSuccessResult.state == 3 && batchWriteSuccessResult.return_value == 4); - Check(batchWriteErrorResult.state == 4 && batchWriteErrorResult.return_value == SCE_KERNEL_ERROR_EINVAL); + Check(batchWriteErrorResult.state == 3 && batchWriteErrorResult.return_value == SCE_KERNEL_ERROR_EINVAL); std::int32_t batchWriteStates[2] = {0, 0}; Check(sceKernelAioWaitRequests(batchWriteIds, 2, batchWriteStates, 1, nullptr) == 0); - Check(batchWriteStates[0] == 3 && batchWriteStates[1] == 4); + Check(batchWriteStates[0] == 3 && batchWriteStates[1] == 3); std::array patch = {'W', 'X', 'Y', 'Z'}; KernelAioResult patchResult{-1, 0}; KernelAioResult badResult{-1, 0}; @@ -269,17 +299,17 @@ int main(int argc, char** argv) { {0, patch.size(), patch.data(), &badResult, -1}, }; std::int32_t writeIds[2] = {0, 0}; - Check(sceKernelAioSubmitWriteCommandsMultiple(writeBatch, 2, 0, writeIds) == 0); + Check(sceKernelAioSubmitWriteCommandsMultiple(writeBatch, 2, 1, writeIds) == 0); Check(sceKernelLseek(batchFd, 0, 1) == 2); Check(writeIds[0] > 0 && writeIds[1] > 0 && writeIds[0] != writeIds[1]); Check(patchResult.state == 3 && patchResult.return_value == 4); - Check(badResult.state == 4 && badResult.return_value == SCE_KERNEL_ERROR_EBADF); + Check(badResult.state == 3 && badResult.return_value == SCE_KERNEL_ERROR_EBADF); std::int32_t writeStates[2] = {0, 0}; Check(sceKernelAioWaitRequests(writeIds, 2, writeStates, 1, nullptr) == 0); - Check(writeStates[0] == 3 && writeStates[1] == 4); + Check(writeStates[0] == 3 && writeStates[1] == 3); std::int32_t polledStates[2] = {0, 0}; Check(sceKernelAioPollRequests(writeIds, 2, polledStates) == 0); - Check(polledStates[0] == 3 && polledStates[1] == 4); + Check(polledStates[0] == 0x10003 && polledStates[1] == 0x10003); std::array readBack{}; std::array tail{}; KernelAioResult readBackResult{-1, 0}; @@ -289,7 +319,7 @@ int main(int argc, char** argv) { {60, tail.size(), tail.data(), &tailResult, batchFd}, }; std::int32_t readIds[2] = {0, 0}; - Check(sceKernelAioSubmitReadCommandsMultiple(readBatch, 2, 0, readIds) == 0); + Check(sceKernelAioSubmitReadCommandsMultiple(readBatch, 2, 1, readIds) == 0); Check(readIds[0] > 0 && readIds[1] > 0 && readIds[0] != readIds[1]); Check(readBackResult.state == 3 && readBackResult.return_value == 4); Check(std::memcmp(readBack.data(), "WXYZ", 4) == 0); @@ -301,74 +331,87 @@ int main(int argc, char** argv) { Check(readStates[0] == 3 && readStates[1] == 3); std::int32_t cancelled = 0; Check(sceKernelAioCancelRequest(readIds[0], &cancelled) == 0); - Check(cancelled == 4); + Check(cancelled == 3); Check(sceKernelAioPollRequest(readIds[0], &polled) == 0); - Check(polled == 4); + Check(polled == 0x10003); Check(sceKernelAioCancelRequest(0, &cancelled) == 0); - Check(cancelled == 2); + Check(cancelled == SCE_KERNEL_ERROR_ESRCH); std::int32_t cancelIds[2] = {0, readIds[1]}; std::int32_t cancelStates[2] = {0, 0}; Check(sceKernelAioCancelRequests(cancelIds, 2, cancelStates) == 0); - Check(cancelStates[0] == 2 && cancelStates[1] == 4); + Check(cancelStates[0] == SCE_KERNEL_ERROR_ESRCH && cancelStates[1] == 3); Check(sceKernelAioPollRequest(readIds[1], &polled) == 0); - Check(polled == 4); + Check(polled == 0x10003); + Check(sceKernelAioCancelRequest(readIds[1], &cancelled) == 0 && cancelled == 3); Check(sceKernelAioPollRequests(readIds, 2, polledStates) == 0); - Check(polledStates[0] == 4 && polledStates[1] == 4); + Check(polledStates[0] == 0x10003 && polledStates[1] == 0x10003); std::int32_t deletedRets[2] = {-1, -1}; Check(sceKernelAioDeleteRequests(writeIds, 2, deletedRets) == 0); Check(deletedRets[0] == 0 && deletedRets[1] == 0); Check(sceKernelAioPollRequest(writeIds[0], &polled) == 0); - Check(polled == 4); - Check(sceKernelAioDeleteRequests(writeIds, 0, deletedRets) == 0); + Check(polled == SCE_KERNEL_ERROR_ESRCH); + Check(sceKernelAioDeleteRequests(writeIds, 2, deletedRets) == 0); + Check(deletedRets[0] == SCE_KERNEL_ERROR_ESRCH && deletedRets[1] == SCE_KERNEL_ERROR_ESRCH); + std::int32_t deletedRet = 7; + Check(sceKernelAioDeleteRequest(writeIds[0], &deletedRet) == 0 && deletedRet == SCE_KERNEL_ERROR_ESRCH); + std::uint32_t unknownTimeout = 1000; + Check(sceKernelAioWaitRequests(writeIds, 2, polledStates, 1, &unknownTimeout) == 0); + Check(polledStates[0] == SCE_KERNEL_ERROR_ESRCH && polledStates[1] == SCE_KERNEL_ERROR_ESRCH); + Check(sceKernelAioDeleteRequests(writeIds, 0, deletedRets) == SCE_KERNEL_ERROR_EINVAL); Check(sceKernelClose(batchFd) == 0); std::int32_t badIds[2] = {readIds[0], 9999}; std::int32_t untouched[2] = {7, 7}; - Check(sceKernelAioSubmitReadCommandsMultiple(nullptr, 1, 0, readIds) == SCE_KERNEL_ERROR_EFAULT); - Check(sceKernelAioSubmitReadCommandsMultiple(readBatch, 1, 0, nullptr) == SCE_KERNEL_ERROR_EFAULT); - Check(sceKernelAioSubmitWriteCommandsMultiple(writeBatch, 0, 0, writeIds) == SCE_KERNEL_ERROR_EINVAL); + Check(sceKernelAioSubmitReadCommandsMultiple(nullptr, 1, 1, readIds) == SCE_KERNEL_ERROR_EFAULT); + Check(sceKernelAioSubmitReadCommandsMultiple(readBatch, 1, 1, nullptr) == SCE_KERNEL_ERROR_EFAULT); + Check(sceKernelAioSubmitWriteCommandsMultiple(writeBatch, 0, 1, writeIds) == SCE_KERNEL_ERROR_EINVAL); KernelAioRwRequest noResult{0, 1, readBack.data(), nullptr, batchFd}; - Check(sceKernelAioSubmitWriteCommandsMultiple(&noResult, 1, 0, writeIds) == SCE_KERNEL_ERROR_EFAULT); + Check(sceKernelAioSubmitWriteCommandsMultiple(&noResult, 1, 1, writeIds) == SCE_KERNEL_ERROR_EFAULT); std::array unread{}; std::array unreadIds{}; - for (const auto submit : {sceKernelAioSubmitReadCommandsMultiple, sceKernelAioSubmitWriteCommandsMultiple}) { - bool limited = false; - try { submit(unread.data(), 129, 0, unreadIds.data()); } catch (const std::runtime_error&) { limited = true; } - Check(limited); + unreadIds.fill(7); + for (const auto submit : {sceKernelAioSubmitReadCommandsMultiple, sceKernelAioSubmitWriteCommandsMultiple, sceKernelAioSubmitReadCommands, sceKernelAioSubmitWriteCommands}) { + Check(submit(unread.data(), 129, 1, unreadIds.data()) == SCE_KERNEL_ERROR_EINVAL); + for (const std::int32_t priority : {0, 4, -1}) Check(submit(unread.data(), 1, priority, unreadIds.data()) == SCE_KERNEL_ERROR_EINVAL); + Check(unreadIds[0] == 7); } - std::int32_t unreadId = 0; - Check(sceKernelAioSubmitReadCommands(unread.data(), 129, 0, &unreadId) == SCE_KERNEL_ERROR_EFAULT); - Check(sceKernelAioSubmitWriteCommands(unread.data(), 129, 0, &unreadId) == SCE_KERNEL_ERROR_EFAULT); Check(sceKernelAioWaitRequests(readIds, 2, nullptr, 1, nullptr) == SCE_KERNEL_ERROR_EFAULT); Check(sceKernelAioWaitRequests(nullptr, 2, readStates, 1, nullptr) == SCE_KERNEL_ERROR_EFAULT); Check(sceKernelAioWaitRequests(readIds, -1, readStates, 1, nullptr) == SCE_KERNEL_ERROR_EINVAL); - Check(sceKernelAioWaitRequests(badIds, 2, untouched, 1, nullptr) == SCE_KERNEL_ERROR_EINVAL); - Check(untouched[0] == 7 && untouched[1] == 7); + Check(sceKernelAioWaitRequests(badIds, 2, untouched, 1, nullptr) == 0); + Check(untouched[0] == 0x10003 && untouched[1] == SCE_KERNEL_ERROR_ESRCH); + untouched[0] = untouched[1] = 7; Check(sceKernelAioPollRequests(readIds, 2, nullptr) == SCE_KERNEL_ERROR_EFAULT); Check(sceKernelAioPollRequests(nullptr, 2, readStates) == SCE_KERNEL_ERROR_EFAULT); Check(sceKernelAioPollRequests(readIds, -1, readStates) == SCE_KERNEL_ERROR_EINVAL); - Check(sceKernelAioPollRequests(badIds, 2, untouched) == SCE_KERNEL_ERROR_EINVAL); + Check(sceKernelAioPollRequests(badIds, 2, untouched) == 0); + Check(untouched[0] == 0x10003 && untouched[1] == SCE_KERNEL_ERROR_ESRCH); + untouched[0] = untouched[1] = 7; + Check(sceKernelAioPollRequests(readIds, 0, untouched) == SCE_KERNEL_ERROR_EINVAL); + Check(sceKernelAioWaitRequests(readIds, 0, untouched, 1, nullptr) == SCE_KERNEL_ERROR_EINVAL); + Check(sceKernelAioCancelRequests(readIds, 0, untouched) == SCE_KERNEL_ERROR_EINVAL); + Check(sceKernelAioDeleteRequests(readIds, 0, untouched) == SCE_KERNEL_ERROR_EINVAL); Check(untouched[0] == 7 && untouched[1] == 7); - Check(sceKernelAioPollRequests(readIds, 0, untouched) == 0); - Check(untouched[0] == 7 && untouched[1] == 7); - bool threw = false; - try { sceKernelAioWaitRequests(readIds, 2, readStates, 3, nullptr); } catch (const std::runtime_error&) { threw = true; } - Check(threw); - threw = false; + for (const std::uint32_t mode : {0u, 3u, 4u, 0x100u, 0xffffffffu}) { + Check(sceKernelAioWaitRequests(readIds, 2, untouched, mode, nullptr) == SCE_KERNEL_ERROR_EINVAL); + Check(untouched[0] == 7 && untouched[1] == 7); + } std::array manyIds{}; std::array manyStates{}; manyIds.fill(readIds[0]); - try { sceKernelAioDeleteRequests(manyIds.data(), 129, manyStates.data()); } catch (const std::runtime_error&) { threw = true; } - Check(threw); - threw = false; - try { sceKernelAioPollRequests(manyIds.data(), 129, manyStates.data()); } catch (const std::runtime_error&) { threw = true; } - Check(threw); - Check(sceKernelAioCancelRequest(9999, &cancelled) == SCE_KERNEL_ERROR_EINVAL); + manyStates.fill(7); + Check(sceKernelAioDeleteRequests(manyIds.data(), 129, manyStates.data()) == SCE_KERNEL_ERROR_EINVAL); + Check(sceKernelAioPollRequests(manyIds.data(), 129, manyStates.data()) == SCE_KERNEL_ERROR_EINVAL); + Check(sceKernelAioWaitRequests(manyIds.data(), 129, manyStates.data(), 1, nullptr) == SCE_KERNEL_ERROR_EINVAL); + Check(sceKernelAioCancelRequests(manyIds.data(), 129, manyStates.data()) == SCE_KERNEL_ERROR_EINVAL); + Check(manyStates[0] == 7); + Check(sceKernelAioPollRequests(manyIds.data(), 128, manyStates.data()) == 0 && manyStates[127] == manyStates[0]); + Check(sceKernelAioCancelRequest(9999, &cancelled) == 0 && cancelled == SCE_KERNEL_ERROR_ESRCH); Check(sceKernelAioCancelRequest(readIds[0], nullptr) == SCE_KERNEL_ERROR_EFAULT); - Check(sceKernelAioCancelRequests(badIds, 2, untouched) == SCE_KERNEL_ERROR_EINVAL); - Check(untouched[0] == 7 && untouched[1] == 7); + Check(sceKernelAioCancelRequests(badIds, 2, untouched) == 0); + Check(untouched[0] == 3 && untouched[1] == SCE_KERNEL_ERROR_ESRCH); Check(sceKernelAioCancelRequests(cancelIds, 2, nullptr) == SCE_KERNEL_ERROR_EFAULT); - Check(sceKernelAioDeleteRequests(badIds, 2, untouched) == SCE_KERNEL_ERROR_EINVAL); - Check(untouched[0] == 7 && untouched[1] == 7); + Check(sceKernelAioDeleteRequests(badIds, 2, untouched) == 0); + Check(untouched[0] == 0 && untouched[1] == SCE_KERNEL_ERROR_ESRCH); Check(sceKernelAioDeleteRequests(writeIds, 2, nullptr) == SCE_KERNEL_ERROR_EFAULT); const auto shared = root / "shared.bin"; std::string sharedContents; @@ -385,7 +428,7 @@ int main(int argc, char** argv) { KernelAioResult result{-1, 0}; KernelAioRwRequest request{offset, bytes.size(), bytes.data(), &result, sharedFd}; std::int32_t requestId = 0; - if (sceKernelAioSubmitReadCommands(&request, 1, 0, &requestId) != 0 || result.state != 3 || + if (sceKernelAioSubmitReadCommands(&request, 1, 1, &requestId) != 0 || result.state != 3 || result.return_value != 16 || std::memcmp(bytes.data(), sharedContents.data() + offset, 16) != 0) { misread = true; } @@ -403,7 +446,7 @@ int main(int argc, char** argv) { KernelAioResult appendResult{-1, 0}; KernelAioRwRequest appendRequest{0, appendBuffer.size(), appendBuffer.data(), &appendResult, appendFd}; std::int32_t appendId = 0; - Check(sceKernelAioSubmitWriteCommands(&appendRequest, 1, 0, &appendId) == 0); + Check(sceKernelAioSubmitWriteCommands(&appendRequest, 1, 1, &appendId) == 0); std::int32_t appendState = 0; Check(sceKernelAioWaitRequest(appendId, &appendState, nullptr) == 0); Check(appendState == 3 && appendResult.state == 3 && appendResult.return_value == 4); diff --git a/core/libs/tests/GuestAvPlayer.cpp b/core/libs/tests/GuestAvPlayer.cpp index b79a260a1c..29977f4a42 100644 --- a/core/libs/tests/GuestAvPlayer.cpp +++ b/core/libs/tests/GuestAvPlayer.cpp @@ -1621,6 +1621,437 @@ void TestVideoSyncTolerance() { Check(allocations.blocks.empty(), "sync tolerance allocations leaked"); } +void TestAudioClockDriftGating() { + const auto open = [](Events& events) { + AvPlayerInitData init = InitData(&events); + auto* player = sceAvPlayerInit(&init); + Check(player != nullptr, "audio drift gating init failed"); + Check(sceAvPlayerAddSource(player, "/app0/avplayer.mp4") == 0, "audio drift gating source failed"); + Check(WaitFor([&] { return events.Seen(EventReady); }), "audio drift gating ready missing"); + Check(sceAvPlayerEnableStream(player, EnglishAudioStream) == 0, "audio drift gating audio failed"); + Check(sceAvPlayerStart(player) == 0, "audio drift gating start failed"); + Check(WaitFor([&] { return events.Seen(EventPlay); }), "audio drift gating play missing"); + return player; + }; + const auto close = [](AvPlayerInternal* player) { + Check(sceAvPlayerClose(player) == 0, "audio drift gating close failed"); + std::lock_guard lock(allocations.mutex); + Check(allocations.blocks.empty(), "audio drift gating allocations leaked"); + }; + const auto take = [](AvPlayerInternal* player, AvPlayerFrameInfo& sound, const char* missing) { + Check(WaitFor([&] { return sceAvPlayerGetAudioData(player, &sound) != 0; }), missing); + }; + + std::uint64_t secondTs = 0; + std::uint64_t beyondTs = 0; + { + Events events; + auto* player = open(events); + AvPlayerFrameInfo sound{}; + take(player, sound, "probe first audio frame missing"); + Check(sound.timestamp == 0, "first audio frame timestamp unexpected"); + take(player, sound, "probe second audio frame missing"); + Check(sound.timestamp > 0 && sound.timestamp <= 30, "second audio frame timestamp unexpected"); + secondTs = sound.timestamp; + Check(WaitFor([&] { + while (sceAvPlayerGetAudioData(player, &sound)) { + if (sound.timestamp > 50) { + beyondTs = sound.timestamp; + return true; + } + } + return false; + }), "probe audio beyond 50ms missing"); + close(player); + } + Check(beyondTs > 50, "probe timestamps unsuitable for drift gating"); + + { + Events events; + auto* player = open(events); + AvPlayerFrameInfo sound{}; + take(player, sound, "within-threshold first audio missing"); + AvPlayer::SetAvPlayerClockForTest_nid_no_patch(player, 0.0); + std::this_thread::sleep_for(std::chrono::milliseconds(40)); + take(player, sound, "within-threshold second audio missing"); + Check(sound.timestamp == secondTs, "within-threshold second timestamp changed"); + Check(sceAvPlayerCurrentTime(player) == 0, "clock rebased despite audio being within drift threshold"); + close(player); + } + + { + Events events; + auto* player = open(events); + AvPlayerFrameInfo sound{}; + take(player, sound, "clock-lead boundary first audio missing"); + AvPlayer::SetAvPlayerClockForTest_nid_no_patch(player, static_cast(secondTs + 50)); + std::this_thread::sleep_for(std::chrono::milliseconds(40)); + take(player, sound, "clock-lead boundary second audio missing"); + Check(sound.timestamp == secondTs, "clock-lead boundary second timestamp changed"); + Check(sceAvPlayerCurrentTime(player) == secondTs + 50, "exact 50ms clock-lead boundary rebased"); + close(player); + } + + { + Events events; + auto* player = open(events); + AvPlayerFrameInfo sound{}; + take(player, sound, "audio-lead boundary first audio missing"); + AvPlayer::SetAvPlayerClockForTest_nid_no_patch(player, static_cast(beyondTs - 50)); + std::this_thread::sleep_for(std::chrono::milliseconds(40)); + Check(WaitFor([&] { + while (sceAvPlayerGetAudioData(player, &sound)) { + if (sound.timestamp >= beyondTs) return true; + } + return false; + }), "audio-lead boundary frame missing"); + Check(sound.timestamp == beyondTs, "audio-lead boundary timestamp unexpected"); + Check(sceAvPlayerCurrentTime(player) == beyondTs - 50, "exact 50ms audio-lead boundary rebased"); + close(player); + } + + { + Events events; + auto* player = open(events); + AvPlayerFrameInfo sound{}; + take(player, sound, "clock-lead rebase first audio missing"); + AvPlayer::SetAvPlayerClockForTest_nid_no_patch(player, static_cast(secondTs + 51)); + std::this_thread::sleep_for(std::chrono::milliseconds(40)); + take(player, sound, "clock-lead rebase second audio missing"); + Check(sound.timestamp == secondTs, "clock-lead rebase second timestamp changed"); + Check(sceAvPlayerCurrentTime(player) == secondTs, "clock-lead beyond 50ms did not rebase"); + close(player); + } + + { + Events events; + auto* player = open(events); + AvPlayerFrameInfo sound{}; + take(player, sound, "audio-lead rebase first audio missing"); + AvPlayer::SetAvPlayerClockForTest_nid_no_patch(player, 0.0); + std::this_thread::sleep_for(std::chrono::milliseconds(40)); + Check(WaitFor([&] { + while (sceAvPlayerGetAudioData(player, &sound)) { + if (sound.timestamp > 50) return true; + } + return false; + }), "audio-lead rebase frame missing"); + Check(sound.timestamp == beyondTs, "audio-lead rebase timestamp unexpected"); + Check(sceAvPlayerCurrentTime(player) == beyondTs, "audio-lead beyond 50ms did not rebase"); + close(player); + } +} + +constexpr std::array ShortAudioMovie{ + 0x00, 0x00, 0x00, 0x20, 0x66, 0x74, 0x79, 0x70, 0x69, 0x73, 0x6f, 0x6d, 0x00, 0x00, 0x02, 0x00, + 0x69, 0x73, 0x6f, 0x6d, 0x69, 0x73, 0x6f, 0x32, 0x61, 0x76, 0x63, 0x31, 0x6d, 0x70, 0x34, 0x31, + 0x00, 0x00, 0x06, 0x77, 0x6d, 0x6f, 0x6f, 0x76, 0x00, 0x00, 0x00, 0x6c, 0x6d, 0x76, 0x68, 0x64, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xe8, + 0x00, 0x00, 0x03, 0xe8, 0x00, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x02, 0xe8, 0x74, 0x72, 0x61, 0x6b, 0x00, 0x00, 0x00, 0x5c, + 0x74, 0x6b, 0x68, 0x64, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xe8, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, + 0x00, 0x40, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x24, 0x65, 0x64, 0x74, 0x73, + 0x00, 0x00, 0x00, 0x1c, 0x65, 0x6c, 0x73, 0x74, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, + 0x00, 0x00, 0x03, 0xe8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x02, 0x60, + 0x6d, 0x64, 0x69, 0x61, 0x00, 0x00, 0x00, 0x20, 0x6d, 0x64, 0x68, 0x64, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x00, 0x3c, 0x00, + 0x55, 0xc4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2d, 0x68, 0x64, 0x6c, 0x72, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x76, 0x69, 0x64, 0x65, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x56, 0x69, 0x64, 0x65, 0x6f, 0x48, 0x61, 0x6e, 0x64, 0x6c, 0x65, 0x72, + 0x00, 0x00, 0x00, 0x02, 0x0b, 0x6d, 0x69, 0x6e, 0x66, 0x00, 0x00, 0x00, 0x14, 0x76, 0x6d, 0x68, + 0x64, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x24, 0x64, 0x69, 0x6e, 0x66, 0x00, 0x00, 0x00, 0x1c, 0x64, 0x72, 0x65, 0x66, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x0c, 0x75, 0x72, 0x6c, 0x20, 0x00, 0x00, 0x00, + 0x01, 0x00, 0x00, 0x01, 0xcb, 0x73, 0x74, 0x62, 0x6c, 0x00, 0x00, 0x00, 0xb7, 0x73, 0x74, 0x73, + 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0xa7, 0x61, 0x76, 0x63, + 0x31, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x40, 0x00, 0x48, 0x00, + 0x00, 0x00, 0x48, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0xff, 0xff, 0x00, + 0x00, 0x00, 0x2d, 0x61, 0x76, 0x63, 0x43, 0x01, 0x42, 0xc0, 0x0a, 0xff, 0xe1, 0x00, 0x16, 0x67, + 0x42, 0xc0, 0x0a, 0xda, 0x10, 0x9b, 0x01, 0x10, 0x00, 0x00, 0x03, 0x00, 0x10, 0x00, 0x00, 0x03, + 0x03, 0xc0, 0xf1, 0x22, 0x6a, 0x01, 0x00, 0x04, 0x68, 0xce, 0x0f, 0xc8, 0x00, 0x00, 0x00, 0x10, + 0x70, 0x61, 0x73, 0x70, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x14, + 0x62, 0x74, 0x72, 0x74, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1c, 0x20, 0x00, 0x00, 0x1c, 0x20, + 0x00, 0x00, 0x00, 0x18, 0x73, 0x74, 0x74, 0x73, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, + 0x00, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x14, 0x73, 0x74, 0x73, 0x73, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x28, + 0x73, 0x74, 0x73, 0x63, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, + 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x18, + 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x8c, 0x73, 0x74, 0x73, 0x7a, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x02, 0x62, 0x00, 0x00, 0x00, 0x0a, + 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x0a, + 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x0a, + 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x0a, + 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x0a, + 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x0a, + 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x0a, + 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x0a, + 0x00, 0x00, 0x00, 0x2c, 0x73, 0x74, 0x63, 0x6f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, + 0x00, 0x00, 0x06, 0xbf, 0x00, 0x00, 0x09, 0x2d, 0x00, 0x00, 0x09, 0x43, 0x00, 0x00, 0x09, 0x53, + 0x00, 0x00, 0x09, 0x69, 0x00, 0x00, 0x09, 0x79, 0x00, 0x00, 0x09, 0x8f, 0x00, 0x00, 0x02, 0xb9, + 0x74, 0x72, 0x61, 0x6b, 0x00, 0x00, 0x00, 0x5c, 0x74, 0x6b, 0x68, 0x64, 0x00, 0x00, 0x00, 0x03, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0xc8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, + 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x24, 0x65, 0x64, 0x74, 0x73, 0x00, 0x00, 0x00, 0x1c, 0x65, 0x6c, 0x73, 0x74, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0xc8, 0x00, 0x00, 0x04, 0x00, + 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x02, 0x31, 0x6d, 0x64, 0x69, 0x61, 0x00, 0x00, 0x00, 0x20, + 0x6d, 0x64, 0x68, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0xbb, 0x80, 0x00, 0x00, 0x25, 0x80, 0x55, 0xc4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2d, + 0x68, 0x64, 0x6c, 0x72, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x73, 0x6f, 0x75, 0x6e, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x53, 0x6f, 0x75, 0x6e, + 0x64, 0x48, 0x61, 0x6e, 0x64, 0x6c, 0x65, 0x72, 0x00, 0x00, 0x00, 0x01, 0xdc, 0x6d, 0x69, 0x6e, + 0x66, 0x00, 0x00, 0x00, 0x10, 0x73, 0x6d, 0x68, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x24, 0x64, 0x69, 0x6e, 0x66, 0x00, 0x00, 0x00, 0x1c, 0x64, 0x72, 0x65, + 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x0c, 0x75, 0x72, 0x6c, + 0x20, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0xa0, 0x73, 0x74, 0x62, 0x6c, 0x00, 0x00, 0x00, + 0x7e, 0x73, 0x74, 0x73, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, + 0x6e, 0x6d, 0x70, 0x34, 0x61, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0xbb, 0x80, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x36, 0x65, 0x73, 0x64, 0x73, 0x00, 0x00, 0x00, 0x00, 0x03, 0x80, 0x80, + 0x80, 0x25, 0x00, 0x02, 0x00, 0x04, 0x80, 0x80, 0x80, 0x17, 0x40, 0x15, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x7d, 0x00, 0x00, 0x00, 0x0b, 0xdc, 0x05, 0x80, 0x80, 0x80, 0x05, 0x11, 0x90, 0x56, 0xe5, + 0x00, 0x06, 0x80, 0x80, 0x80, 0x01, 0x02, 0x00, 0x00, 0x00, 0x14, 0x62, 0x74, 0x72, 0x74, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x7d, 0x00, 0x00, 0x00, 0x0b, 0xdc, 0x00, 0x00, 0x00, 0x20, 0x73, + 0x74, 0x74, 0x73, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x0a, 0x00, + 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x00, 0x58, 0x73, + 0x74, 0x73, 0x63, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x01, 0x00, + 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, + 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, + 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0x00, + 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x02, 0x00, + 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x40, 0x73, 0x74, 0x73, 0x7a, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x06, 0x00, + 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x06, 0x00, + 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x06, 0x00, + 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x2c, 0x73, 0x74, 0x63, 0x6f, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x07, 0x00, 0x00, 0x06, 0xa7, 0x00, 0x00, 0x09, 0x21, 0x00, 0x00, 0x09, 0x37, 0x00, + 0x00, 0x09, 0x4d, 0x00, 0x00, 0x09, 0x5d, 0x00, 0x00, 0x09, 0x73, 0x00, 0x00, 0x09, 0x83, 0x00, + 0x00, 0x00, 0x1a, 0x73, 0x67, 0x70, 0x64, 0x01, 0x00, 0x00, 0x00, 0x72, 0x6f, 0x6c, 0x6c, 0x00, + 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0xff, 0xff, 0x00, 0x00, 0x00, 0x1c, 0x73, 0x62, 0x67, + 0x70, 0x00, 0x00, 0x00, 0x00, 0x72, 0x6f, 0x6c, 0x6c, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, + 0x0b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x62, 0x75, 0x64, 0x74, 0x61, 0x00, 0x00, 0x00, + 0x5a, 0x6d, 0x65, 0x74, 0x61, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0x68, 0x64, 0x6c, + 0x72, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6d, 0x64, 0x69, 0x72, 0x61, 0x70, 0x70, + 0x6c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2d, 0x69, 0x6c, + 0x73, 0x74, 0x00, 0x00, 0x00, 0x25, 0xa9, 0x74, 0x6f, 0x6f, 0x00, 0x00, 0x00, 0x1d, 0x64, 0x61, + 0x74, 0x61, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x4c, 0x61, 0x76, 0x66, 0x35, 0x38, + 0x2e, 0x37, 0x36, 0x2e, 0x31, 0x30, 0x30, 0x00, 0x00, 0x00, 0x08, 0x66, 0x72, 0x65, 0x65, 0x00, + 0x00, 0x03, 0xe0, 0x6d, 0x64, 0x61, 0x74, 0xde, 0x04, 0x00, 0x4c, 0x61, 0x76, 0x63, 0x35, 0x38, + 0x2e, 0x31, 0x33, 0x34, 0x2e, 0x31, 0x30, 0x30, 0x00, 0x42, 0x20, 0x08, 0xc1, 0x18, 0x38, 0x00, + 0x00, 0x02, 0x44, 0x06, 0x05, 0xff, 0xff, 0x40, 0xdc, 0x45, 0xe9, 0xbd, 0xe6, 0xd9, 0x48, 0xb7, + 0x96, 0x2c, 0xd8, 0x20, 0xd9, 0x23, 0xee, 0xef, 0x78, 0x32, 0x36, 0x34, 0x20, 0x2d, 0x20, 0x63, + 0x6f, 0x72, 0x65, 0x20, 0x31, 0x36, 0x34, 0x20, 0x2d, 0x20, 0x48, 0x2e, 0x32, 0x36, 0x34, 0x2f, + 0x4d, 0x50, 0x45, 0x47, 0x2d, 0x34, 0x20, 0x41, 0x56, 0x43, 0x20, 0x63, 0x6f, 0x64, 0x65, 0x63, + 0x20, 0x2d, 0x20, 0x43, 0x6f, 0x70, 0x79, 0x6c, 0x65, 0x66, 0x74, 0x20, 0x32, 0x30, 0x30, 0x33, + 0x2d, 0x32, 0x30, 0x32, 0x31, 0x20, 0x2d, 0x20, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x77, + 0x77, 0x77, 0x2e, 0x76, 0x69, 0x64, 0x65, 0x6f, 0x6c, 0x61, 0x6e, 0x2e, 0x6f, 0x72, 0x67, 0x2f, + 0x78, 0x32, 0x36, 0x34, 0x2e, 0x68, 0x74, 0x6d, 0x6c, 0x20, 0x2d, 0x20, 0x6f, 0x70, 0x74, 0x69, + 0x6f, 0x6e, 0x73, 0x3a, 0x20, 0x63, 0x61, 0x62, 0x61, 0x63, 0x3d, 0x30, 0x20, 0x72, 0x65, 0x66, + 0x3d, 0x31, 0x20, 0x64, 0x65, 0x62, 0x6c, 0x6f, 0x63, 0x6b, 0x3d, 0x30, 0x3a, 0x30, 0x3a, 0x30, + 0x20, 0x61, 0x6e, 0x61, 0x6c, 0x79, 0x73, 0x65, 0x3d, 0x30, 0x3a, 0x30, 0x20, 0x6d, 0x65, 0x3d, + 0x64, 0x69, 0x61, 0x20, 0x73, 0x75, 0x62, 0x6d, 0x65, 0x3d, 0x30, 0x20, 0x70, 0x73, 0x79, 0x3d, + 0x31, 0x20, 0x70, 0x73, 0x79, 0x5f, 0x72, 0x64, 0x3d, 0x31, 0x2e, 0x30, 0x30, 0x3a, 0x30, 0x2e, + 0x30, 0x30, 0x20, 0x6d, 0x69, 0x78, 0x65, 0x64, 0x5f, 0x72, 0x65, 0x66, 0x3d, 0x30, 0x20, 0x6d, + 0x65, 0x5f, 0x72, 0x61, 0x6e, 0x67, 0x65, 0x3d, 0x31, 0x36, 0x20, 0x63, 0x68, 0x72, 0x6f, 0x6d, + 0x61, 0x5f, 0x6d, 0x65, 0x3d, 0x31, 0x20, 0x74, 0x72, 0x65, 0x6c, 0x6c, 0x69, 0x73, 0x3d, 0x30, + 0x20, 0x38, 0x78, 0x38, 0x64, 0x63, 0x74, 0x3d, 0x30, 0x20, 0x63, 0x71, 0x6d, 0x3d, 0x30, 0x20, + 0x64, 0x65, 0x61, 0x64, 0x7a, 0x6f, 0x6e, 0x65, 0x3d, 0x32, 0x31, 0x2c, 0x31, 0x31, 0x20, 0x66, + 0x61, 0x73, 0x74, 0x5f, 0x70, 0x73, 0x6b, 0x69, 0x70, 0x3d, 0x31, 0x20, 0x63, 0x68, 0x72, 0x6f, + 0x6d, 0x61, 0x5f, 0x71, 0x70, 0x5f, 0x6f, 0x66, 0x66, 0x73, 0x65, 0x74, 0x3d, 0x30, 0x20, 0x74, + 0x68, 0x72, 0x65, 0x61, 0x64, 0x73, 0x3d, 0x32, 0x20, 0x6c, 0x6f, 0x6f, 0x6b, 0x61, 0x68, 0x65, + 0x61, 0x64, 0x5f, 0x74, 0x68, 0x72, 0x65, 0x61, 0x64, 0x73, 0x3d, 0x31, 0x20, 0x73, 0x6c, 0x69, + 0x63, 0x65, 0x64, 0x5f, 0x74, 0x68, 0x72, 0x65, 0x61, 0x64, 0x73, 0x3d, 0x30, 0x20, 0x6e, 0x72, + 0x3d, 0x30, 0x20, 0x64, 0x65, 0x63, 0x69, 0x6d, 0x61, 0x74, 0x65, 0x3d, 0x31, 0x20, 0x69, 0x6e, + 0x74, 0x65, 0x72, 0x6c, 0x61, 0x63, 0x65, 0x64, 0x3d, 0x30, 0x20, 0x62, 0x6c, 0x75, 0x72, 0x61, + 0x79, 0x5f, 0x63, 0x6f, 0x6d, 0x70, 0x61, 0x74, 0x3d, 0x30, 0x20, 0x63, 0x6f, 0x6e, 0x73, 0x74, + 0x72, 0x61, 0x69, 0x6e, 0x65, 0x64, 0x5f, 0x69, 0x6e, 0x74, 0x72, 0x61, 0x3d, 0x30, 0x20, 0x62, + 0x66, 0x72, 0x61, 0x6d, 0x65, 0x73, 0x3d, 0x30, 0x20, 0x77, 0x65, 0x69, 0x67, 0x68, 0x74, 0x70, + 0x3d, 0x30, 0x20, 0x6b, 0x65, 0x79, 0x69, 0x6e, 0x74, 0x3d, 0x33, 0x30, 0x20, 0x6b, 0x65, 0x79, + 0x69, 0x6e, 0x74, 0x5f, 0x6d, 0x69, 0x6e, 0x3d, 0x33, 0x20, 0x73, 0x63, 0x65, 0x6e, 0x65, 0x63, + 0x75, 0x74, 0x3d, 0x30, 0x20, 0x69, 0x6e, 0x74, 0x72, 0x61, 0x5f, 0x72, 0x65, 0x66, 0x72, 0x65, + 0x73, 0x68, 0x3d, 0x30, 0x20, 0x72, 0x63, 0x3d, 0x63, 0x72, 0x66, 0x20, 0x6d, 0x62, 0x74, 0x72, + 0x65, 0x65, 0x3d, 0x30, 0x20, 0x63, 0x72, 0x66, 0x3d, 0x32, 0x33, 0x2e, 0x30, 0x20, 0x71, 0x63, + 0x6f, 0x6d, 0x70, 0x3d, 0x30, 0x2e, 0x36, 0x30, 0x20, 0x71, 0x70, 0x6d, 0x69, 0x6e, 0x3d, 0x30, + 0x20, 0x71, 0x70, 0x6d, 0x61, 0x78, 0x3d, 0x36, 0x39, 0x20, 0x71, 0x70, 0x73, 0x74, 0x65, 0x70, + 0x3d, 0x34, 0x20, 0x69, 0x70, 0x5f, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x3d, 0x31, 0x2e, 0x34, 0x30, + 0x20, 0x61, 0x71, 0x3d, 0x30, 0x00, 0x80, 0x00, 0x00, 0x00, 0x16, 0x65, 0x88, 0x84, 0x3a, 0x26, + 0x28, 0x00, 0x09, 0x02, 0xc9, 0xc9, 0xc9, 0xd7, 0x5d, 0x75, 0xd7, 0x5d, 0x75, 0xd7, 0x5d, 0x75, + 0xe0, 0x21, 0x10, 0x04, 0x60, 0x8c, 0x1c, 0x21, 0x10, 0x04, 0x60, 0x8c, 0x1c, 0x00, 0x00, 0x00, + 0x06, 0x41, 0x9a, 0x20, 0x36, 0x82, 0x30, 0x21, 0x10, 0x04, 0x60, 0x8c, 0x1c, 0x21, 0x10, 0x04, + 0x60, 0x8c, 0x1c, 0x00, 0x00, 0x00, 0x06, 0x41, 0x9a, 0x40, 0x3a, 0x82, 0x30, 0x21, 0x10, 0x04, + 0x60, 0x8c, 0x1c, 0x00, 0x00, 0x00, 0x06, 0x41, 0x9a, 0x60, 0x3a, 0x82, 0x30, 0x21, 0x10, 0x04, + 0x60, 0x8c, 0x1c, 0x21, 0x10, 0x04, 0x60, 0x8c, 0x1c, 0x00, 0x00, 0x00, 0x06, 0x41, 0x9a, 0x80, + 0x3a, 0x82, 0x30, 0x21, 0x10, 0x04, 0x60, 0x8c, 0x1c, 0x00, 0x00, 0x00, 0x06, 0x41, 0x9a, 0xa0, + 0x3a, 0x82, 0x30, 0x21, 0x10, 0x04, 0x60, 0x8c, 0x1c, 0x21, 0x10, 0x04, 0x60, 0x8c, 0x1c, 0x00, + 0x00, 0x00, 0x06, 0x41, 0x9a, 0xc0, 0x3e, 0x82, 0x30, 0x00, 0x00, 0x00, 0x06, 0x41, 0x9a, 0xe0, + 0x3e, 0x82, 0x30, 0x00, 0x00, 0x00, 0x06, 0x41, 0x9b, 0x00, 0x3e, 0x82, 0x30, 0x00, 0x00, 0x00, + 0x06, 0x41, 0x9b, 0x20, 0x3e, 0x82, 0x30, 0x00, 0x00, 0x00, 0x06, 0x41, 0x9b, 0x40, 0x3e, 0x82, + 0x30, 0x00, 0x00, 0x00, 0x06, 0x41, 0x9b, 0x60, 0x3e, 0x82, 0x30, 0x00, 0x00, 0x00, 0x06, 0x41, + 0x9b, 0x80, 0x3e, 0x82, 0x30, 0x00, 0x00, 0x00, 0x06, 0x41, 0x9b, 0xa0, 0x3e, 0x82, 0x30, 0x00, + 0x00, 0x00, 0x06, 0x41, 0x9b, 0xc0, 0x3e, 0x82, 0x30, 0x00, 0x00, 0x00, 0x06, 0x41, 0x9b, 0xe0, + 0x3e, 0x82, 0x30, 0x00, 0x00, 0x00, 0x06, 0x41, 0x9a, 0x00, 0x3e, 0x82, 0x30, 0x00, 0x00, 0x00, + 0x06, 0x41, 0x9a, 0x20, 0x3e, 0x82, 0x30, 0x00, 0x00, 0x00, 0x06, 0x41, 0x9a, 0x40, 0x3e, 0x82, + 0x30, 0x00, 0x00, 0x00, 0x06, 0x41, 0x9a, 0x60, 0x3e, 0x82, 0x30, 0x00, 0x00, 0x00, 0x06, 0x41, + 0x9a, 0x80, 0x3e, 0x82, 0x30, 0x00, 0x00, 0x00, 0x06, 0x41, 0x9a, 0xa0, 0x3e, 0x82, 0x30, 0x00, + 0x00, 0x00, 0x06, 0x41, 0x9a, 0xc0, 0x3e, 0x82, 0x30, 0x00, 0x00, 0x00, 0x06, 0x41, 0x9a, 0xe0, + 0x3e, 0x82, 0x30, 0x00, 0x00, 0x00, 0x06, 0x41, 0x9b, 0x00, 0x3e, 0x82, 0x30, 0x00, 0x00, 0x00, + 0x06, 0x41, 0x9b, 0x20, 0x3e, 0x82, 0x30, 0x00, 0x00, 0x00, 0x06, 0x41, 0x9b, 0x40, 0x3e, 0x82, + 0x30, 0x00, 0x00, 0x00, 0x06, 0x41, 0x9b, 0x60, 0x3e, 0x82, 0x30, 0x00, 0x00, 0x00, 0x06, 0x41, + 0x9b, 0x80, 0x3e, 0x82, 0x30, 0x00, 0x00, 0x00, 0x06, 0x41, 0x9b, 0xa0, 0x3e, 0x82, 0x30 +}; + +void WriteShortAudioMovie() { + std::filesystem::create_directories("app0"); + std::ofstream file("app0/short-audio.mp4", std::ios::binary | std::ios::trunc); + file.write(reinterpret_cast(ShortAudioMovie.data()), static_cast(ShortAudioMovie.size())); + Check(static_cast(file), "cannot write the short audio movie"); +} + +void TestAudioEofClockRebase() { + const auto open = [](Events& events, bool hold) { + AvPlayerInitData init = InitData(&events); + auto* player = sceAvPlayerInit(&init); + Check(player != nullptr, "audio eof clock init failed"); + Check(sceAvPlayerAddSource(player, "/app0/avplayer.mp4") == 0, "audio eof clock source failed"); + Check(WaitFor([&] { return events.Seen(EventReady); }), "audio eof clock ready missing"); + Check(sceAvPlayerEnableStream(player, EnglishAudioStream) == 0, "audio eof clock audio failed"); + if (hold) AvPlayer::HoldAvPlayerAudioEofForTest_nid_no_patch(player, true); + Check(sceAvPlayerStart(player) == 0, "audio eof clock start failed"); + Check(WaitFor([&] { return events.Seen(EventPlay); }), "audio eof clock play missing"); + return player; + }; + const auto close = [](AvPlayerInternal* player) { + AvPlayer::HoldAvPlayerAudioEofForTest_nid_no_patch(player, false); + Check(sceAvPlayerClose(player) == 0, "audio eof clock close failed"); + std::lock_guard lock(allocations.mutex); + Check(allocations.blocks.empty(), "audio eof clock allocations leaked"); + }; + const auto pullQueued = [](AvPlayerInternal* player) { + std::uint64_t timestamp = 0; + Check(AvPlayer::NextQueuedAvPlayerAudioTimestampForTest_nid_no_patch(player, ×tamp), "audio eof clock queue empty"); + AvPlayer::SetAvPlayerClockForTest_nid_no_patch(player, static_cast(timestamp)); + AvPlayerFrameInfo sound{}; + Check(sceAvPlayerGetAudioData(player, &sound) != 0, "audio eof clock drain failed"); + Check(sound.timestamp == timestamp, "audio eof clock drained an unexpected frame"); + }; + const auto leaveFinalFrame = [&](AvPlayerInternal* player, bool afterEof) { + Check(WaitFor([&] { + const auto queued = AvPlayer::QueuedAvPlayerAudioForTest_nid_no_patch(player); + const auto ready = afterEof ? AvPlayer::AvPlayerAudioEndedForTest_nid_no_patch(player) : AvPlayer::AvPlayerAudioEofHeldForTest_nid_no_patch(player); + if (ready && queued >= 1) return true; + if (queued > 1) pullQueued(player); + return false; + }), afterEof ? "audio eof before the final pull missing" : "audio eof hold missing"); + while (AvPlayer::QueuedAvPlayerAudioForTest_nid_no_patch(player) > 1) pullQueued(player); + }; + const auto pullFinal = [](AvPlayerInternal* player, AvPlayerFrameInfo& sound) { + std::uint64_t last = 0; + Check(AvPlayer::NextQueuedAvPlayerAudioTimestampForTest_nid_no_patch(player, &last), "final audio frame was not queued"); + Check(last > 50, "final audio timestamp is inside the drift gate"); + AvPlayer::SetAvPlayerClockForTest_nid_no_patch(player, static_cast(last - 42)); + std::this_thread::sleep_for(std::chrono::milliseconds(40)); + Check(sceAvPlayerGetAudioData(player, &sound) != 0, "final audio frame missing"); + Check(sound.timestamp == last, "final audio timestamp changed"); + return last; + }; + + { + Events events; + auto* player = open(events, true); + leaveFinalFrame(player, false); + AvPlayerFrameInfo sound{}; + const auto last = pullFinal(player, sound); + Check(sceAvPlayerCurrentTime(player) == last - 42, "clock rebased before audio eof was marked"); + AvPlayer::HoldAvPlayerAudioEofForTest_nid_no_patch(player, false); + Check(WaitFor([&] { return AvPlayer::AvPlayerAudioEndedForTest_nid_no_patch(player); }), "audio eof was not marked"); + Check(sceAvPlayerCurrentTime(player) == last, "clock was not reconciled after audio eof"); + close(player); + } + + { + Events events; + auto* player = open(events, false); + leaveFinalFrame(player, true); + AvPlayerFrameInfo sound{}; + const auto last = pullFinal(player, sound); + Check(sceAvPlayerCurrentTime(player) == last, "clock was not reconciled when the final frame followed eof"); + close(player); + } +} + +void TestAudioClockAdvancesAfterEof() { + WriteShortAudioMovie(); + Events events; + AvPlayerInitData init = InitData(&events); + auto* player = sceAvPlayerInit(&init); + Check(player != nullptr, "audio eof advance init failed"); + Check(sceAvPlayerAddSource(player, "/app0/short-audio.mp4") == 0, "audio eof advance source failed"); + Check(WaitFor([&] { return events.Seen(EventReady); }), "audio eof advance ready missing"); + Check(sceAvPlayerEnableStream(player, EnglishAudioStream) == 0, "audio eof advance audio failed"); + Check(sceAvPlayerStart(player) == 0, "audio eof advance start failed"); + Check(WaitFor([&] { return events.Seen(EventPlay); }), "audio eof advance play missing"); + AvPlayerFrameInfo sound{}; + std::uint64_t last = 0; + Check(WaitFor([&] { + while (sceAvPlayerGetAudioData(player, &sound)) last = sound.timestamp; + return AvPlayer::AvPlayerAudioEndedForTest_nid_no_patch(player); + }), "short audio did not end"); + Check(last > 0 && last < 500, "short audio timestamp unexpected"); + const auto before = sceAvPlayerCurrentTime(player); + std::this_thread::sleep_for(std::chrono::milliseconds(80)); + for (int poll = 0; poll < 5; ++poll) sceAvPlayerGetAudioData(player, &sound); + const auto after = sceAvPlayerCurrentTime(player); + Check(after >= before + 40, "clock stopped after audio eof: " + std::to_string(before) + " to " + std::to_string(after)); + Check(sceAvPlayerClose(player) == 0, "audio eof advance close failed"); + std::lock_guard lock(allocations.mutex); + Check(allocations.blocks.empty(), "audio eof advance allocations leaked"); +} + +void TestTrailingVideoAfterShortAudio() { + WriteShortAudioMovie(); + Events events; + AvPlayerInitData init = InitData(&events); + auto* player = sceAvPlayerInit(&init); + Check(player != nullptr, "trailing video init failed"); + Check(sceAvPlayerAddSource(player, "/app0/short-audio.mp4") == 0, "trailing video source failed"); + Check(WaitFor([&] { return events.Seen(EventReady); }), "trailing video ready missing"); + Check(sceAvPlayerEnableStream(player, VideoStream) == 0, "trailing video stream failed"); + Check(sceAvPlayerEnableStream(player, EnglishAudioStream) == 0, "trailing video audio failed"); + Check(sceAvPlayerStart(player) == 0, "trailing video start failed"); + Check(WaitFor([&] { return events.Seen(EventPlay); }), "trailing video play missing"); + AvPlayerFrameInfo sound{}; + std::uint64_t last = 0; + Check(WaitFor([&] { + while (sceAvPlayerGetAudioData(player, &sound)) last = sound.timestamp; + return AvPlayer::AvPlayerAudioEndedForTest_nid_no_patch(player); + }), "trailing video audio did not end"); + Check(last > 0 && last < 500, "trailing video audio timestamp unexpected"); + std::uint64_t latest = 0; + Check(WaitFor([&] { + sceAvPlayerGetAudioData(player, &sound); + sceAvPlayerCurrentTime(player); + AvPlayerFrameInfoEx frame{}; + if (sceAvPlayerGetVideoDataEx(player, &frame)) latest = std::max(latest, frame.timestamp); + return latest > last + 100; + }, 3000), "trailing video was not delivered after audio eof"); + Check(sceAvPlayerClose(player) == 0, "trailing video close failed"); + std::lock_guard lock(allocations.mutex); + Check(allocations.blocks.empty(), "trailing video allocations leaked"); +} + } int main(int argc, char** argv) { @@ -1660,6 +2091,10 @@ int main(int argc, char** argv) { TestLoopWarningWaitsForPlayback(); TestEofStopLatency(); TestVideoSyncTolerance(); + TestAudioClockDriftGating(); + TestAudioEofClockRebase(); + TestAudioClockAdvancesAfterEof(); + TestTrailingVideoAfterShortAudio(); std::puts("AvPlayer tests passed"); return 0; } catch (const std::exception& error) { diff --git a/core/libs/tests/GuestCharacter.cpp b/core/libs/tests/GuestCharacter.cpp index 94fa6665a1..70615e6c3e 100644 --- a/core/libs/tests/GuestCharacter.cpp +++ b/core/libs/tests/GuestCharacter.cpp @@ -18,6 +18,8 @@ int APS5_VABI isprint_nid_postfix(int); int APS5_VABI isgraph_nid_postfix(int); int APS5_VABI ispunct_nid_postfix(int); int APS5_VABI isxdigit_nid_postfix(int); +int APS5_VABI isascii_nid_postfix(int); +int APS5_VABI toascii_nid_postfix(int); int APS5_VABI toupper_nid_postfix(int); int APS5_VABI tolower_nid_postfix(int); const short* APS5_VABI _Getptolower_nid_postfix(); @@ -57,9 +59,16 @@ int main() { Require(bool(isgraph_nid_postfix(c)) == bool(std::isgraph(c))); Require(bool(ispunct_nid_postfix(c)) == bool(std::ispunct(c))); Require(bool(isxdigit_nid_postfix(c)) == bool(std::isxdigit(c))); + Require(bool(isascii_nid_postfix(c)) == (c >= 0 && c <= 127)); + Require(toascii_nid_postfix(c) == (c & 0x7F)); Require(toupper_nid_postfix(c) == std::toupper(c)); Require(tolower_nid_postfix(c) == std::tolower(c)); Require(toupper_nid_postfix(c) == _Getptoupper_nid_postfix()[c]); Require(tolower_nid_postfix(c) == _Getptolower_nid_postfix()[c]); } + Require(!isascii_nid_postfix(-100)); + Require(!isascii_nid_postfix(128)); + Require(!isascii_nid_postfix(256)); + Require(toascii_nid_postfix(258) == 2); + Require(toascii_nid_postfix(-1) == 0x7F); } diff --git a/core/libs/tests/GuestClosedStandardDescriptorsWindows.cpp b/core/libs/tests/GuestClosedStandardDescriptorsWindows.cpp new file mode 100644 index 0000000000..77106d6465 --- /dev/null +++ b/core/libs/tests/GuestClosedStandardDescriptorsWindows.cpp @@ -0,0 +1,97 @@ +#include "prx/libc/include/FileStream.hpp" +#include "prx/libc/include/general/VabiMacros.hpp" +#include +#include +#include +#include +#include + +extern "C" { +int APS5_VABI fcntl_nid_postfix(int, int, ...); +extern FileStream* __stdinp_nid_postfix; +extern FileStream* __stdoutp_nid_postfix; +extern FileStream* __stderrp_nid_postfix; +int APS5_VABI fileno_nid_postfix(FileStream*); +int APS5_VABI fputc_nid_postfix(int, FileStream*); +int APS5_VABI fflush_nid_postfix(FileStream*); +int APS5_VABI fgetc_nid_postfix(FileStream*); +int APS5_VABI feof_nid_postfix(FileStream*); +int APS5_VABI ferror_nid_postfix(FileStream*); +void APS5_VABI clearerr_nid_postfix(FileStream*); +} + +namespace { + +constexpr int GuestGetDescriptorFlags = 1; +constexpr int GuestGetFlags = 3; +constexpr int GuestAccessMode = 3; +constexpr int GuestReadOnly = 0; +constexpr int GuestWriteOnly = 1; + +int Child(const char* closed) { + for (int descriptor = 0; descriptor <= 2; ++descriptor) { + if (closed[descriptor] != '1') continue; + const auto handle = reinterpret_cast(::_get_osfhandle(descriptor)); + if (handle == INVALID_HANDLE_VALUE || handle == reinterpret_cast(-2)) return 70 + descriptor; + if (::GetFileType(handle) != FILE_TYPE_CHAR) return 73 + descriptor; + if (fcntl_nid_postfix(descriptor, GuestGetDescriptorFlags) != 0) return 76 + descriptor; + const int expected = descriptor == 0 ? GuestReadOnly : GuestWriteOnly; + if ((fcntl_nid_postfix(descriptor, GuestGetFlags) & GuestAccessMode) != expected) return 79 + descriptor; + std::FILE* const host = descriptor == 0 ? stdin : descriptor == 1 ? stdout : stderr; + FileStream* const guest = descriptor == 0 ? __stdinp_nid_postfix : descriptor == 1 ? __stdoutp_nid_postfix : __stderrp_nid_postfix; + if (::_fileno(host) != descriptor) return 82 + descriptor; + if (guest->GuestState().descriptor != descriptor) return 96 + descriptor; + if (fileno_nid_postfix(guest) != descriptor) return 85 + descriptor; + if (descriptor == 0) { + if (std::fgetc(host) != EOF || std::feof(host) == 0 || std::ferror(host) != 0) return 88; + std::clearerr(host); + if (fgetc_nid_postfix(guest) != EOF || feof_nid_postfix(guest) == 0 || ferror_nid_postfix(guest) != 0) return 89; + clearerr_nid_postfix(guest); + } else { + if (std::fputc('x', host) == EOF || std::fflush(host) != 0 || std::ferror(host) != 0) return 90 + descriptor; + if (fputc_nid_postfix('x', guest) == EOF || fflush_nid_postfix(guest) != 0 || ferror_nid_postfix(guest) != 0) return 93 + descriptor; + } + } + return 0; +} + +DWORD RunWithout(const wchar_t* self, bool input, bool output, bool error) { + std::wstring command = std::wstring(L"\"") + self + L"\" --closed-child " + (input ? L"1" : L"0") + (output ? L"1" : L"0") + (error ? L"1" : L"0"); + STARTUPINFOW startup{}; + startup.cb = sizeof(startup); + startup.dwFlags = STARTF_USESTDHANDLES; + startup.hStdInput = input ? nullptr : GetStdHandle(STD_INPUT_HANDLE); + startup.hStdOutput = output ? nullptr : GetStdHandle(STD_OUTPUT_HANDLE); + startup.hStdError = error ? nullptr : GetStdHandle(STD_ERROR_HANDLE); + PROCESS_INFORMATION process{}; + if (!CreateProcessW(self, command.data(), nullptr, nullptr, TRUE, DETACHED_PROCESS, nullptr, nullptr, &startup, &process)) return 0xffffffffu; + DWORD code = 0xfffffffeu; + if (WaitForSingleObject(process.hProcess, 10000) == WAIT_OBJECT_0) GetExitCodeProcess(process.hProcess, &code); + else TerminateProcess(process.hProcess, code); + CloseHandle(process.hThread); + CloseHandle(process.hProcess); + return code; +} + +} + +int main(int argc, char** argv) { + if (argc > 2 && std::strcmp(argv[1], "--closed-child") == 0 && std::strlen(argv[2]) == 3) return Child(argv[2]); + wchar_t self[MAX_PATH]; + if (GetModuleFileNameW(nullptr, self, MAX_PATH) == 0) return 2; + const struct { bool input, output, error; const char* name; } cases[] = { + {true, false, false, "stdin"}, + {false, true, false, "stdout"}, + {false, false, true, "stderr"}, + {true, true, true, "all three"}, + }; + for (const auto& test : cases) { + const DWORD result = RunWithout(self, test.input, test.output, test.error); + if (result != 0) { + std::fprintf(stderr, "missing standard handles, %s: child exited with %lu\n", test.name, result); + return 1; + } + } + std::puts("missing standard handles are opened on NUL"); + return 0; +} diff --git a/core/libs/tests/GuestClosedStandardStreamsLoadWindows.cpp b/core/libs/tests/GuestClosedStandardStreamsLoadWindows.cpp new file mode 100644 index 0000000000..1bf5e960fc --- /dev/null +++ b/core/libs/tests/GuestClosedStandardStreamsLoadWindows.cpp @@ -0,0 +1,104 @@ +#include "prx/libc/include/FileStream.hpp" +#include "prx/libc/include/general/VabiMacros.hpp" +#include +#include +#include +#include +#include +#include +#include +#include + +extern "C" { +_invalid_parameter_handler _set_thread_local_invalid_parameter_handler(_invalid_parameter_handler); +int APS5_VABI fputc_nid_postfix(int, FileStream*); +int APS5_VABI fflush_nid_postfix(FileStream*); +} + +namespace { +int invalidCalls = 0; + +void InvalidParameter(const wchar_t*, const wchar_t*, const wchar_t*, unsigned int, std::uintptr_t) { + ++invalidCalls; +} + +DWORD RunPrebound(const std::wstring& library, HANDLE report) { + wchar_t self[MAX_PATH]; + const DWORD length = GetModuleFileNameW(nullptr, self, MAX_PATH); + if (length == 0 || length >= MAX_PATH) return 0xffffffffu; + HANDLE inherited = nullptr; + if (!DuplicateHandle(GetCurrentProcess(), report, GetCurrentProcess(), &inherited, 0, TRUE, DUPLICATE_SAME_ACCESS)) return 0xfffffffeu; + std::wstring command = L"\"" + std::wstring(self) + L"\" \"" + library + L"\" --prebound"; + STARTUPINFOW startup{}; + startup.cb = sizeof(startup); + startup.dwFlags = STARTF_USESTDHANDLES; + startup.hStdError = inherited; + PROCESS_INFORMATION process{}; + const bool created = CreateProcessW(self, command.data(), nullptr, nullptr, TRUE, DETACHED_PROCESS, nullptr, nullptr, &startup, &process) != 0; + CloseHandle(inherited); + if (!created) return 0xfffffffdu; + DWORD code = 0xfffffffcu; + if (WaitForSingleObject(process.hProcess, 5000) == WAIT_OBJECT_0) GetExitCodeProcess(process.hProcess, &code); + else TerminateProcess(process.hProcess, code); + CloseHandle(process.hThread); + CloseHandle(process.hProcess); + return code; +} +} + +int main() { + HANDLE report = nullptr; + if (!DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_ERROR_HANDLE), GetCurrentProcess(), &report, 0, FALSE, DUPLICATE_SAME_ACCESS)) return 1; + const auto finish = [&](int code, const std::string& message) { + DWORD written = 0; + WriteFile(report, message.data(), static_cast(message.size()), &written, nullptr); + CloseHandle(report); + return code; + }; + int count = 0; + auto** const arguments = CommandLineToArgvW(GetCommandLineW(), &count); + if (!arguments || (count != 2 && count != 3) || (count == 3 && std::wcscmp(arguments[2], L"--prebound") != 0)) { + if (arguments) LocalFree(arguments); + return finish(2, "expected one libc path argument\n"); + } + const std::wstring library(arguments[1]); + const bool prebound = count == 3; + LocalFree(arguments); + int outputDescriptor = 1; + if (prebound) { + if (::_fileno(stdout) != -2 || ::_wfreopen(L"NUL", L"wb", stdout) != stdout) + return finish(11, "cannot prepare an already rebound stdout\n"); + outputDescriptor = ::_fileno(stdout); + if (outputDescriptor < 3 || ::_get_osfhandle(1) != -2) + return finish(11, "stdout did not retain its separate descriptor\n"); + } else { + const DWORD child = RunPrebound(library, report); + if (child != 0) return finish(12, "prebound stdout child exited with " + std::to_string(child) + "\n"); + for (int descriptor : {1, 2}) { + if (::_fileno(__acrt_iob_func(descriptor)) != descriptor || ::_close(descriptor) != 0) + return finish(4, "cannot close an inherited standard descriptor\n"); + } + } + const auto previous = _set_thread_local_invalid_parameter_handler(InvalidParameter); + const auto module = LoadLibraryW(library.c_str()); + const DWORD loadError = module == nullptr ? GetLastError() : ERROR_SUCCESS; + _set_thread_local_invalid_parameter_handler(previous); + if (module == nullptr) return finish(5, "cannot load libc: " + std::to_string(loadError) + "\n"); + if (invalidCalls != 0) return finish(6, "restoring bound standard streams invoked the invalid parameter handler " + std::to_string(invalidCalls) + " times\n"); + const auto put = reinterpret_cast(GetProcAddress(module, "fputc_nid_postfix")); + const auto flush = reinterpret_cast(GetProcAddress(module, "fflush_nid_postfix")); + if (!put || !flush) return finish(7, "cannot find guest stream operations\n"); + if (::_write(1, "x", 1) != 1) return finish(13, "the restored stdout descriptor cannot write\n"); + for (int descriptor : {1, 2}) { + const auto pointer = GetProcAddress(module, descriptor == 1 ? "__stdoutp_nid_postfix" : "__stderrp_nid_postfix"); + if (!pointer) return finish(8, "cannot find a guest standard stream\n"); + auto* const guest = *reinterpret_cast(pointer); + auto* const host = __acrt_iob_func(descriptor); + const int expected = descriptor == 1 ? outputDescriptor : descriptor; + if (!guest || guest->GuestState().descriptor != expected || ::_fileno(host) != expected) + return finish(9, "a restored stream has the wrong descriptor\n"); + if (std::fputc('x', host) != 'x' || std::fflush(host) != 0 || put('x', guest) != 'x' || flush(guest) != 0) + return finish(10, "a restored stream cannot write to NUL\n"); + } + return finish(0, "bound standard streams are restored without invalid parameter callbacks\n"); +} diff --git a/core/libs/tests/GuestCondTimedwait.cpp b/core/libs/tests/GuestCondTimedwait.cpp index 434d257d2d..27a91e95e7 100644 --- a/core/libs/tests/GuestCondTimedwait.cpp +++ b/core/libs/tests/GuestCondTimedwait.cpp @@ -20,6 +20,8 @@ int APS5_VABI pthread_cond_init_nid_postfix(PthreadCond* cond, const PthreadCond int APS5_VABI pthread_cond_destroy_nid_postfix(PthreadCond* cond); int APS5_VABI pthread_cond_timedwait_nid_postfix(PthreadCond* cond, PthreadMutex* mutex, const KernelTimespec* abstime); int APS5_VABI pthread_cond_reltimedwait_np_nid_postfix(PthreadCond* cond, PthreadMutex* mutex, unsigned int usec); +int APS5_VABI pthread_cond_signalto_np_nid_postfix(PthreadCond* cond, Pthread thread); +Pthread APS5_VABI scePthreadSelf(); int APS5_VABI __cxa_atexit_nid_postfix(void (APS5_VABI *)(void*), void*, void*); void APS5_VABI __pthread_cxa_finalize_nid_postfix(void*); unsigned int APS5_VABI sceKernelSleep(unsigned int seconds); @@ -55,6 +57,7 @@ struct Signalled { PthreadCond cond = nullptr; bool ready = false; int trylock = SCE_OK; + Pthread target = nullptr; }; static void* APS5_VABI Trylocker(void* arg) { @@ -72,6 +75,15 @@ static void* APS5_VABI Signaller(void* arg) { return nullptr; } +static void* APS5_VABI SignallerTo(void* arg) { + auto& signalled = *static_cast(arg); + Require(scePthreadMutexLock(&signalled.mutex) == SCE_OK); + signalled.ready = true; + Require(pthread_cond_signalto_np_nid_postfix(&signalled.cond, signalled.target) == 0); + Require(scePthreadMutexUnlock(&signalled.mutex) == SCE_OK); + return nullptr; +} + static void RequireHeldByCaller(Signalled& signalled) { Pthread thread = nullptr; Require(scePthreadCreate(&thread, nullptr, Trylocker, &signalled, nullptr) == SCE_OK); @@ -108,8 +120,29 @@ static void RelativeWait() { Require(scePthreadMutexDestroy(&signalled.mutex) == SCE_OK); } +static void SignalTo() { + Signalled signalled; + signalled.target = scePthreadSelf(); + Require(scePthreadMutexInit(&signalled.mutex, nullptr, nullptr) == SCE_OK); + Require(scePthreadCondInit(&signalled.cond, nullptr, nullptr) == SCE_OK); + Require(pthread_cond_signalto_np_nid_postfix(&signalled.cond, signalled.target) == 0); + Require(scePthreadMutexLock(&signalled.mutex) == SCE_OK); + Pthread thread = nullptr; + Require(scePthreadCreate(&thread, nullptr, SignallerTo, &signalled, nullptr) == SCE_OK); + const auto waitStart = std::chrono::steady_clock::now(); + while (!signalled.ready) + Require(pthread_cond_reltimedwait_np_nid_postfix(&signalled.cond, &signalled.mutex, 10000000) == 0); + Require(std::chrono::steady_clock::now() - waitStart < std::chrono::seconds(5)); + Require(scePthreadMutexUnlock(&signalled.mutex) == SCE_OK); + Require(scePthreadJoin(thread, nullptr) == SCE_OK); + Require(scePthreadCondDestroy(&signalled.cond) == SCE_OK); + Require(pthread_cond_signalto_np_nid_postfix(&signalled.cond, signalled.target) == 22); + Require(scePthreadMutexDestroy(&signalled.mutex) == SCE_OK); +} + int main() { RelativeWait(); + SignalTo(); Context context; PthreadCond cond = nullptr; diff --git a/core/libs/tests/GuestDatagramReceive.cpp b/core/libs/tests/GuestDatagramReceive.cpp index 2c7bc9e0c5..d719b7719e 100644 --- a/core/libs/tests/GuestDatagramReceive.cpp +++ b/core/libs/tests/GuestDatagramReceive.cpp @@ -140,6 +140,21 @@ static void Check(bool net, bool from, int family) { std::this_thread::sleep_for(std::chrono::milliseconds(1)); } while (std::chrono::steady_clock::now() < deadline); Require(result == 0 && byte == '!', "empty datagram"); + if (!net) { + send("hello", 5); + const auto ready = std::chrono::steady_clock::now() + std::chrono::seconds(1); + do { + result = receive(&byte, 1, 2, nullptr, nullptr); + if (result >= 0 || *error != 35) break; + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } while (std::chrono::steady_clock::now() < ready); + Require(result == 1, "wait for the datagram"); + Require(receive(&byte, 0, 2, nullptr, nullptr) == 0 && receive(&byte, 1, 2, nullptr, nullptr) == 1, "a zero-length peek keeps the datagram"); + Require(receive(&byte, 0, 0, nullptr, nullptr) == 0, "a zero-length receive returns 0"); + Require(receive(&byte, 1, 0, nullptr, nullptr) < 0 && *error == 35, "a zero-length receive discards the datagram"); + Require(fcntl_nid_postfix(receiver, 4, 2) == 0, "make receiver blocking"); + Require(receive(&byte, 0, 0, nullptr, nullptr) == 0, "a zero-length receive on an empty queue returns 0 without waiting"); + } Require(closeSocket(receiver) == 0, "close receiver"); Require(closeSocket(sender) == 0, "close sender"); } diff --git a/core/libs/tests/GuestDinkumwareAtomics.cpp b/core/libs/tests/GuestDinkumwareAtomics.cpp index 8e45d76902..cda03655f0 100644 --- a/core/libs/tests/GuestDinkumwareAtomics.cpp +++ b/core/libs/tests/GuestDinkumwareAtomics.cpp @@ -29,6 +29,20 @@ void APS5_VABI _Atomic_copy_nid_postfix(volatile unsigned char*, std::size_t, vo void APS5_VABI _Atomic_exchange_nid_postfix(volatile unsigned char*, std::size_t, volatile void*, volatile void*, int); int APS5_VABI _Atomic_compare_exchange_strong_nid_postfix(volatile unsigned char*, std::size_t, volatile void*, void*, const void*, int, int); int APS5_VABI _Atomic_compare_exchange_weak_nid_postfix(volatile unsigned char*, std::size_t, volatile void*, void*, const void*, int, int); +std::uint8_t APS5_VABI __atomic_load_1_nid_postfix(const volatile std::uint8_t*, int); +std::uint64_t APS5_VABI __atomic_load_8_nid_postfix(const volatile std::uint64_t*, int); +void APS5_VABI __atomic_store_2_nid_postfix(volatile std::uint16_t*, std::uint16_t, int); +void APS5_VABI __atomic_store_8_nid_postfix(volatile std::uint64_t*, std::uint64_t, int); +std::uint32_t APS5_VABI __atomic_exchange_4_nid_postfix(volatile std::uint32_t*, std::uint32_t, int); +int APS5_VABI __atomic_compare_exchange_1_nid_postfix(volatile std::uint8_t*, std::uint8_t*, std::uint8_t, int, int); +int APS5_VABI __atomic_compare_exchange_8_nid_postfix(volatile std::uint64_t*, std::uint64_t*, std::uint64_t, int, int); +std::uint16_t APS5_VABI __atomic_fetch_add_2_nid_postfix(volatile std::uint16_t*, std::uint16_t, int); +std::uint64_t APS5_VABI __atomic_exchange_8_nid_postfix(volatile std::uint64_t*, std::uint64_t, int); +std::uint64_t APS5_VABI __atomic_fetch_add_8_nid_postfix(volatile std::uint64_t*, std::uint64_t, int); +std::uint32_t APS5_VABI __atomic_fetch_sub_4_nid_postfix(volatile std::uint32_t*, std::uint32_t, int); +std::uint8_t APS5_VABI __atomic_fetch_and_1_nid_postfix(volatile std::uint8_t*, std::uint8_t, int); +std::uint16_t APS5_VABI __atomic_fetch_or_2_nid_postfix(volatile std::uint16_t*, std::uint16_t, int); +std::uint64_t APS5_VABI __atomic_fetch_xor_8_nid_postfix(volatile std::uint64_t*, std::uint64_t, int); } static void Require(bool value) { if (!value) std::abort(); } @@ -111,8 +125,45 @@ static void CheckConcurrentGeneric() { Require(counter.parts[0] == 2 * Iterations && counter.parts[1] == 4 * Iterations && counter.parts[2] == 6 * Iterations); } +static void CheckSized() { + std::uint8_t byte = 0xa5; + Require(__atomic_load_1_nid_postfix(&byte, Acquire) == 0xa5); + std::uint16_t half = 0; + __atomic_store_2_nid_postfix(&half, 0x1234, Release); + Require(half == 0x1234); + Require(__atomic_fetch_add_2_nid_postfix(&half, 0xffff, Relaxed) == 0x1234 && half == 0x1233); + Require(__atomic_fetch_or_2_nid_postfix(&half, 0x8000, SeqCst) == 0x1233 && half == 0x9233); + std::uint32_t word = 5; + Require(__atomic_exchange_4_nid_postfix(&word, 9, AcqRel) == 5 && word == 9); + Require(__atomic_fetch_sub_4_nid_postfix(&word, 10, SeqCst) == 9 && word == 0xffffffffu); + Require(__atomic_fetch_and_1_nid_postfix(&byte, 0x0f, SeqCst) == 0xa5 && byte == 0x05); + std::uint8_t expectedByte = 4; + Require(__atomic_compare_exchange_1_nid_postfix(&byte, &expectedByte, 7, SeqCst, Relaxed) == 0 && expectedByte == 5 && byte == 5); + Require(__atomic_compare_exchange_1_nid_postfix(&byte, &expectedByte, 7, SeqCst, Relaxed) == 1 && expectedByte == 5 && byte == 7); + std::uint64_t wide = 0; + __atomic_store_8_nid_postfix(&wide, 0x0123456789abcdefull, SeqCst); + Require(__atomic_load_8_nid_postfix(&wide, SeqCst) == 0x0123456789abcdefull); + Require(__atomic_fetch_xor_8_nid_postfix(&wide, 0xffffffff00000000ull, SeqCst) == 0x0123456789abcdefull && wide == 0xfedcba9889abcdefull); + std::uint64_t expectedWide = 0xfedcba9889abcdefull; + Require(__atomic_compare_exchange_8_nid_postfix(&wide, &expectedWide, 1, SeqCst, SeqCst) == 1 && wide == 1); + Require(__atomic_exchange_8_nid_postfix(&wide, 2, 6) == 1 && wide == 2); + Require(__atomic_load_8_nid_postfix(&wide, Release) == 2 && __atomic_load_8_nid_postfix(&wide, AcqRel) == 2 && __atomic_load_8_nid_postfix(&wide, -1) == 2); + Require(__atomic_fetch_add_8_nid_postfix(&wide, 1, 7) == 2 && wide == 3); + + std::uint64_t counter = 0; + std::thread threads[4]; + for (auto& thread : threads) { + thread = std::thread([&counter] { + for (int i = 0; i < 100000; ++i) __atomic_fetch_add_8_nid_postfix(&counter, 1, Relaxed); + }); + } + for (auto& thread : threads) thread.join(); + Require(counter == 400000); +} + int main() { CheckTyped(); + CheckSized(); CheckFlagsAndFences(); CheckGeneric(); CheckConcurrentGeneric(); diff --git a/core/libs/tests/GuestDinkumwareInternals.cpp b/core/libs/tests/GuestDinkumwareInternals.cpp index aa660dabdf..563d08d8cc 100644 --- a/core/libs/tests/GuestDinkumwareInternals.cpp +++ b/core/libs/tests/GuestDinkumwareInternals.cpp @@ -4,11 +4,21 @@ #include #include #include +#include extern "C" { int* APS5_VABI __error_nid_postfix(); double APS5_VABI _Stod_nid_postfix(const char* text, char** end, std::int64_t pten); long long APS5_VABI _Stoll_nid_postfix(const char* text, char** end, int base); +double APS5_VABI _Stodx_nid_postfix(const char* text, char** end, std::int64_t pten, int* error); +float APS5_VABI _Stofx_nid_postfix(const char* text, char** end, std::int64_t pten, int* error); +long double APS5_VABI _Stoldx_nid_postfix(const char* text, char** end, std::int64_t pten, int* error); +float APS5_VABI _Stof_nid_postfix(const char* text, char** end, std::int64_t pten); +long double APS5_VABI _Stold_nid_postfix(const char* text, char** end, std::int64_t pten); +long long APS5_VABI _Stollx_nid_postfix(const char* text, char** end, int base, int* error); +std::int64_t APS5_VABI _Stolx_nid_postfix(const char* text, char** end, int base, int* error); +std::uint64_t APS5_VABI _Stoulx_nid_postfix(const char* text, char** end, int base, int* error); +unsigned long long APS5_VABI _Stoullx_nid_postfix(const char* text, char** end, int base, int* error); double APS5_VABI _Sin_nid_postfix(double x, unsigned int quadrant); float APS5_VABI _FSin_nid_postfix(float x, unsigned int quadrant); long double APS5_VABI _LSin_nid_postfix(long double x, unsigned int quadrant); @@ -19,6 +29,7 @@ float APS5_VABI _FLog_nid_postfix(float x, int base); namespace { constexpr int RangeError = 34; +constexpr int InvalidArgument = 22; void Require(bool condition, const char* message) { if (!condition) { @@ -71,6 +82,101 @@ void TestStoll() { Require(_Stoll_nid_postfix("9223372036854775808", &end, 10) == 9223372036854775807ll && *__error_nid_postfix() == RangeError, "_Stoll saturates"); } +void TestFloatErrors() { + char* end = nullptr; + int error = 99; + *__error_nid_postfix() = 0; + Require(_Stodx_nid_postfix("1.5", &end, 2, &error) == 150.0 && error == 0 && *__error_nid_postfix() == 0, "_Stodx in range"); + error = 99; + Require(std::isinf(_Stodx_nid_postfix("1e400", &end, 0, &error)) && error == 1 && *__error_nid_postfix() == RangeError, "_Stodx overflow"); + error = 99; + *__error_nid_postfix() = 0; + Require(_Stodx_nid_postfix("1e-400", &end, 2, &error) == 0.0 && error == 1 && *__error_nid_postfix() == RangeError, "_Stodx underflow"); + error = 99; + *__error_nid_postfix() = 7; + Require(_Stodx_nid_postfix("1e-400", &end, 300, &error) == 1e-100 && error == 0 && *__error_nid_postfix() == 7, "_Stodx rescued by its scale"); + error = 99; + *__error_nid_postfix() = RangeError; + Require(_Stodx_nid_postfix("2.5", &end, 1, &error) == 25.0 && error == 0 && *__error_nid_postfix() == RangeError, "_Stodx kept a preset errno"); + error = 99; + *__error_nid_postfix() = 0; + Require(std::isinf(_Stofx_nid_postfix("1e39", &end, 0, &error)) && error == 1 && *__error_nid_postfix() == RangeError, "_Stofx overflow"); + error = 99; + *__error_nid_postfix() = 0; + Require(_Stofx_nid_postfix("2.5e-1", &end, 3, &error) == 250.0f && error == 0 && *__error_nid_postfix() == 0, "_Stofx scaled"); + error = 99; + *__error_nid_postfix() = 0; + Require(_Stofx_nid_postfix("1e39", &end, -1, &error) == 1e38f && error == 0 && *__error_nid_postfix() == 0, "_Stofx overflow scaled back into range"); + error = 99; + Require(_Stodx_nid_postfix("1e400", &end, -100, &error) == 1e300 && error == 0 && *__error_nid_postfix() == 0, "_Stodx overflow scaled back into range"); + error = 99; + *__error_nid_postfix() = 0; + Require(_Stoldx_nid_postfix("1e400", &end, 0, &error) == 1e400L && error == 0 && *__error_nid_postfix() == 0, "_Stoldx uses the extended range"); + error = 99; + *__error_nid_postfix() = 0; + Require(_Stodx_nid_postfix("1e-310", &end, 0, &error) == 1e-310 && error == 0 && *__error_nid_postfix() == 0 && end[0] == '\0', "_Stodx subnormal"); + error = 99; + Require(_Stodx_nid_postfix("1e-300", &end, -10, &error) == 1e-310 && error == 0 && *__error_nid_postfix() == 0, "_Stodx scaled into the subnormal range"); + error = 99; + *__error_nid_postfix() = 7; + Require(_Stodx_nid_postfix("1e-310", &end, 0, &error) == 1e-310 && error == 0 && *__error_nid_postfix() == 7, "_Stodx subnormal kept a preset errno"); + error = 99; + *__error_nid_postfix() = 0; + Require(_Stodx_nid_postfix("2e-324", &end, 0, &error) == 0.0 && error == 1 && *__error_nid_postfix() == RangeError, "_Stodx below the smallest subnormal"); + error = 99; + *__error_nid_postfix() = 0; + Require(_Stofx_nid_postfix("1e-45", &end, 0, &error) == 0x1p-149f && error == 0 && *__error_nid_postfix() == 0 && end[0] == '\0', "_Stofx inexact subnormal"); + error = 99; + Require(_Stofx_nid_postfix("1e-35", &end, -10, &error) == 0x1p-149f && error == 0 && *__error_nid_postfix() == 0, "_Stofx scaled into the subnormal range"); + error = 99; + *__error_nid_postfix() = 7; + const float subnormal = _Stofx_nid_postfix("1e-40", &end, 0, &error); + Require(subnormal > 0x1p-134f && subnormal < 0x1p-132f && error == 0 && *__error_nid_postfix() == 7, "_Stofx subnormal kept a preset errno"); + error = 99; + *__error_nid_postfix() = 0; + Require(_Stofx_nid_postfix("1e-46", &end, 0, &error) == 0.0f && error == 1 && *__error_nid_postfix() == RangeError, "_Stofx underflow"); + error = 99; + *__error_nid_postfix() = 0; + Require(_Stofx_nid_postfix("1.17549435e-38", &end, 0, &error) == 0x1p-126f && error == 0 && *__error_nid_postfix() == 0, "_Stofx smallest normal"); + error = 99; + Require(_Stoldx_nid_postfix("1e-4940", &end, 0, &error) == 1e-4940L && error == 0 && *__error_nid_postfix() == 0, "_Stoldx subnormal"); + error = 99; + Require(_Stoldx_nid_postfix("1e-4930", &end, -10, &error) == 1e-4940L && error == 0 && *__error_nid_postfix() == 0, "_Stoldx scaled into the subnormal range"); + error = 99; + Require(_Stoldx_nid_postfix("1e-5000", &end, 0, &error) == 0.0L && error == 1 && *__error_nid_postfix() == RangeError, "_Stoldx underflow"); + Require(_Stof_nid_postfix("1.5", &end, 1) == 15.0f, "_Stof"); + Require(_Stold_nid_postfix("1.5", &end, -1) == 0.15L, "_Stold"); + error = 99; + Require(std::isnan(_Stodx_nid_postfix("nan", &end, 2, &error)) && error == 0 && end[0] == '\0', "_Stodx NaN"); +} + +void TestIntegerErrors() { + char* end = nullptr; + int error = 99; + *__error_nid_postfix() = 5; + Require(_Stollx_nid_postfix("123", &end, 0, &error) == 123 && error == 0 && *__error_nid_postfix() == 5, "_Stollx in range"); + Require(_Stollx_nid_postfix("9223372036854775808", &end, 10, &error) == 9223372036854775807ll && error == RangeError && *__error_nid_postfix() == RangeError, "_Stollx overflow"); + *__error_nid_postfix() = 5; + const char* text = " 12"; + for (int base : {1, -1, 37}) { + error = 99; + Require(_Stollx_nid_postfix(text, &end, base, &error) == 0 && error == InvalidArgument && end == text && *__error_nid_postfix() == 5, "_Stollx invalid base"); + error = 99; + Require(_Stolx_nid_postfix(text, &end, base, &error) == 0 && error == InvalidArgument && end == text && *__error_nid_postfix() == 5, "_Stolx invalid base"); + } + Require(_Stoll_nid_postfix(text, &end, 1) == 0 && end == text && *__error_nid_postfix() == 5, "_Stoll invalid base"); + error = 99; + *__error_nid_postfix() = 0; + Require(_Stolx_nid_postfix("9223372036854775808", &end, 0, &error) == 9223372036854775807ll && error == RangeError && *__error_nid_postfix() == 0, "_Stolx overflow inside the unsigned range"); + Require(_Stolx_nid_postfix("-18446744073709551615", &end, 10, &error) == (-9223372036854775807ll - 1) && error == RangeError && *__error_nid_postfix() == 0, "_Stolx negative overflow inside the unsigned range"); + Require(_Stolx_nid_postfix("18446744073709551616", &end, 10, &error) == 9223372036854775807ll && error == RangeError && *__error_nid_postfix() == RangeError, "_Stolx overflow past the unsigned range"); + error = 99; + *__error_nid_postfix() = 0; + Require(_Stoulx_nid_postfix("-1", &end, 0, &error) == 18446744073709551615ull && error == 0, "_Stoulx wraps a negative value"); + Require(_Stoullx_nid_postfix("-18446744073709551615", &end, 10, &error) == 1 && error == 0 && *__error_nid_postfix() == 0, "_Stoullx wraps a negated maximum"); + Require(_Stoullx_nid_postfix("18446744073709551616", &end, 10, &error) == 18446744073709551615ull && error == RangeError && *__error_nid_postfix() == RangeError, "_Stoullx overflow"); +} + void TestDispatch() { Require(_Sin_nid_postfix(0.5, 0) == std::sin(0.5) && _Sin_nid_postfix(0.5, 1) == std::cos(0.5) && _Sin_nid_postfix(0.5, 7) == std::cos(0.5), "_Sin"); Require(_FSin_nid_postfix(0.5f, 0) == std::sin(0.5f) && _FSin_nid_postfix(0.5f, 2) == std::cos(0.5f), "_FSin"); @@ -84,5 +190,7 @@ void TestDispatch() { int main() { TestStod(); TestStoll(); + TestFloatErrors(); + TestIntegerErrors(); TestDispatch(); } diff --git a/core/libs/tests/GuestDup.cpp b/core/libs/tests/GuestDup.cpp index 0a8016a79b..9d0029e2ca 100644 --- a/core/libs/tests/GuestDup.cpp +++ b/core/libs/tests/GuestDup.cpp @@ -104,6 +104,8 @@ static void BadDescriptors() { #ifdef _WIN32 const auto previous = _set_invalid_parameter_handler(RecordInvalidParameter); #endif + Require(dup2_nid_postfix(pipe[0], closed) == closed); + Require(close_nid_postfix(closed) == 0); RequireFailure(close_nid_postfix(closed), GuestEbadf); RequireFailure(close_nid_postfix(-1), GuestEbadf); #ifdef _WIN32 diff --git a/core/libs/tests/GuestEqueueEvents.cpp b/core/libs/tests/GuestEqueueEvents.cpp index c0858dc39a..9a6a6dee78 100644 --- a/core/libs/tests/GuestEqueueEvents.cpp +++ b/core/libs/tests/GuestEqueueEvents.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include #include @@ -256,12 +257,64 @@ static void VerifyRepeatedAmprCompletionsAreNotLost() { Require(sceKernelDeleteEqueue(eq) == SCE_OK); } +static void RequireReported(KernelEqueue eq, int num, std::initializer_list expected) { + KernelEvent events[4]{}; + int count = 0; + const KernelUseconds poll = 0; + Require(sceKernelWaitEqueue(eq, events, num, &count, &poll) == SCE_OK); + Require(count == static_cast(expected.size())); + int index = 0; + for (const uintptr_t id : expected) { + Require(sceKernelGetEventId(&events[index++]) == id); + } +} + +static void VerifyReportedEventsMoveBehindWaitingOnes() { + KernelEqueue eq = 0; + KernelEvent events[4]{}; + int count = 0; + const KernelUseconds poll = 0; + int payload = 0; + Require(sceKernelCreateEqueue(&eq, "rotate") == SCE_OK); + for (const int id : {1, 2}) { + Require(sceKernelAddUserEvent(eq, id) == SCE_OK); + Require(sceKernelTriggerUserEvent(eq, id, &payload) == SCE_OK); + } + RequireReported(eq, 1, {1}); + RequireReported(eq, 1, {2}); + RequireReported(eq, 1, {1}); + Require(sceKernelDeleteEqueue(eq) == SCE_OK); + + Require(sceKernelCreateEqueue(&eq, "rotate three") == SCE_OK); + for (const int id : {1, 2, 3}) { + Require(sceKernelAddUserEvent(eq, id) == SCE_OK); + Require(sceKernelTriggerUserEvent(eq, id, &payload) == SCE_OK); + } + RequireReported(eq, 2, {1, 2}); + RequireReported(eq, 2, {3, 1}); + RequireReported(eq, 2, {2, 3}); + Require(sceKernelDeleteEqueue(eq) == SCE_OK); + + Require(sceKernelCreateEqueue(&eq, "rotate edge") == SCE_OK); + for (const int id : {7, 8}) { + Require(sceKernelAddUserEventEdge(eq, id) == SCE_OK); + Require(sceKernelTriggerUserEvent(eq, id, &payload) == SCE_OK); + } + RequireReported(eq, 1, {7}); + Require(sceKernelTriggerUserEvent(eq, 7, &payload) == SCE_OK); + RequireReported(eq, 1, {8}); + RequireReported(eq, 1, {7}); + Require(sceKernelWaitEqueue(eq, events, 4, &count, &poll) == SCE_KERNEL_ERROR_ETIMEDOUT); + Require(sceKernelDeleteEqueue(eq) == SCE_OK); +} + int main() { VerifyPeriodicTimer(); VerifyUserEventReportedOncePerWait(); VerifyBlockingWaitReportsLevelEventOnce(); VerifyEdgeUserEventReportedOnce(); VerifyRepeatedAmprCompletionsAreNotLost(); + VerifyReportedEventsMoveBehindWaitingOnes(); KernelEqueue eq = 0; Require(sceKernelCreateEqueue(&eq, "events") == SCE_OK); diff --git a/core/libs/tests/GuestErrors.cpp b/core/libs/tests/GuestErrors.cpp index b66b25b00d..1cf514aaed 100644 --- a/core/libs/tests/GuestErrors.cpp +++ b/core/libs/tests/GuestErrors.cpp @@ -1,5 +1,6 @@ #include "prx/libc/include/general/VabiMacros.hpp" #include +#include #include #include #include @@ -21,7 +22,7 @@ static const char* const freebsd[97] = { "Is a directory", "Invalid argument", "Too many open files in system", "Too many open files", "Inappropriate ioctl for device", "Text file busy", "File too large", "No space left on device", "Illegal seek", - "Read-only file system", "Too many links", "Broken pipe", + "Read-only filesystem", "Too many links", "Broken pipe", "Numerical argument out of domain", "Result too large", "Resource temporarily unavailable", "Operation now in progress", "Operation already in progress", "Socket operation on non-socket", "Destination address required", "Message too long", "Protocol wrong type for socket", @@ -62,7 +63,11 @@ int main() { for (int error = 0; error <= 96; ++error) { Require(strerror_r_nid_postfix(error, buffer, sizeof(buffer)) == 0); Require(std::strcmp(buffer, freebsd[error]) == 0); - Require(std::strcmp(strerror_nid_postfix(error), freebsd[error]) == 0); + char coded[32]; + std::snprintf(coded, sizeof(coded), "error_code=0x%08x", static_cast(error)); + const char* expected = error == 0 ? "No error" : error < 95 ? freebsd[error] : coded; + Require(std::strcmp(strerror_nid_postfix(error), expected) == 0); + Require(*__error_nid_postfix() == 13); } Require(strerror_r_nid_postfix(-1, buffer, sizeof(buffer)) == 22); Require(std::strcmp(buffer, "Unknown error: -1") == 0); @@ -70,15 +75,18 @@ int main() { struct UnknownError { int error; const char* text; + const char* coded; }; - for (const UnknownError& unknown : {UnknownError{97, "Unknown error: 97"}, UnknownError{INT_MAX, "Unknown error: 2147483647"}, - UnknownError{INT_MIN, "Unknown error: -2147483648"}}) { + for (const UnknownError& unknown : {UnknownError{97, "Unknown error: 97", "error_code=0x00000061"}, + UnknownError{9999, "Unknown error: 9999", "error_code=0x0000270f"}, + UnknownError{INT_MAX, "Unknown error: 2147483647", "error_code=0x7fffffff"}, + UnknownError{-1, "Unknown error: -1", "error_code=0xffffffff"}, + UnknownError{INT_MIN, "Unknown error: -2147483648", "error_code=0x80000000"}}) { Require(strerror_r_nid_postfix(unknown.error, buffer, sizeof(buffer)) == 22); Require(std::strcmp(buffer, unknown.text) == 0); Require(*__error_nid_postfix() == 13); - Require(std::strcmp(strerror_nid_postfix(unknown.error), unknown.text) == 0); - Require(*__error_nid_postfix() == 22); - *__error_nid_postfix() = 13; + Require(std::strcmp(strerror_nid_postfix(unknown.error), unknown.coded) == 0); + Require(*__error_nid_postfix() == 13); } char sentinel[] = "xyz"; Require(strerror_r_nid_postfix(22, sentinel, 1) == 34); diff --git a/core/libs/tests/GuestFlexibleMemoryModule.cpp b/core/libs/tests/GuestFlexibleMemoryModule.cpp index 1bf220a810..10f33287fc 100644 --- a/core/libs/tests/GuestFlexibleMemoryModule.cpp +++ b/core/libs/tests/GuestFlexibleMemoryModule.cpp @@ -1,3 +1,37 @@ +#ifdef __APPLE__ +#include "prx/libc/include/specifics/linux/ElfTypes.hpp" +#include +#include +#endif + extern "C" { unsigned char guestFlexibleMemoryModuleData[512u * 1024u * 1024u] = {}; } + +#ifdef __APPLE__ +namespace { + +[[gnu::section("__APS5DATA,__meta"), gnu::used]] alignas(8) std::byte metadata[0x60 + sizeof(Elf64_Phdr)]{}; + +[[maybe_unused]] const bool described = [] { + Dl_info image{}; + if (dladdr(guestFlexibleMemoryModuleData, &image) == 0) return false; + const std::uint64_t loadBias = reinterpret_cast(image.dli_fbase); + Elf64_Phdr header{}; + header.p_type = PT_LOAD; + header.p_flags = PF_R | PF_W; + header.p_offset = reinterpret_cast(guestFlexibleMemoryModuleData) - loadBias; + header.p_vaddr = header.p_offset; + header.p_paddr = header.p_offset; + header.p_memsz = sizeof(guestFlexibleMemoryModuleData); + const std::uint64_t version = 2; + const std::uint64_t count = 1; + std::memcpy(metadata + 0x08, &loadBias, sizeof(loadBias)); + std::memcpy(metadata + 0x58, &count, sizeof(count)); + std::memcpy(metadata + 0x60, &header, sizeof(header)); + std::memcpy(metadata, &version, sizeof(version)); + return true; +}(); + +} +#endif diff --git a/core/libs/tests/GuestFloatingPoint.cpp b/core/libs/tests/GuestFloatingPoint.cpp index 31e3374567..9d0de18775 100644 --- a/core/libs/tests/GuestFloatingPoint.cpp +++ b/core/libs/tests/GuestFloatingPoint.cpp @@ -2,11 +2,20 @@ #include #include #include +#include #include #include +#include #include #include #include +#ifdef _WIN32 +#include +#else +#include +#include +#include +#endif extern "C" { int* APS5_VABI __error_nid_postfix(); @@ -22,6 +31,7 @@ int APS5_VABI fedisableexcept_nid_postfix(int); int APS5_VABI __feenableexcept_nid_postfix(int); int APS5_VABI __fedisableexcept_nid_postfix(int); int APS5_VABI fegettrapenable_nid_postfix(); +int APS5_VABI fegetexcept_nid_postfix(); int APS5_VABI fesettrapenable_nid_postfix(int); } @@ -60,6 +70,28 @@ Environment SeedFlags(unsigned x87, unsigned sse) { return ReadEnvironment(); } +void CheckEnabledExceptions() { + const auto seeded = SeedFlags(0, 0); + for (unsigned x87 = 0; x87 <= 0x3f; ++x87) { + for (unsigned sse = 0; sse <= 0x3f; ++sse) { + auto state = seeded; + state.x87[0] = (state.x87[0] & ~0xc3fu) | 0x800 | x87; + state.x87[1] = (state.x87[1] & ~0x3fu) | (x87 & 0x15u); + state.sse = (state.sse & ~0x7fbfu) | 0x2000 | (sse << 7) | (sse & 0x2au); + WriteEnvironment(state); + *__error_nid_postfix() = 123; + const int enabled = fegetexcept_nid_postfix(); + const int error = *__error_nid_postfix(); + const auto after = ReadEnvironment(); + WriteEnvironment(seeded); + Require(enabled == static_cast(0x3f ^ x87), "enabled exceptions use the x87 masks independently of SSE"); + Require(after.x87[0] == state.x87[0] && after.x87[1] == state.x87[1] && after.sse == state.sse, + "enabled exception query preserves control modes and flags"); + Require(error == 123, "enabled exception query preserves errno"); + } + } +} + void CheckRounding() { const auto seeded = SeedFlags(0x02, 0x10); constexpr int modes[] = {0, 0x400, 0x800, 0xc00}; @@ -222,12 +254,122 @@ void CheckTraps() { WriteEnvironment(seeded); } -int main() { +using HoldExcept = int (APS5_VABI*)(Environment*); + +void CheckHold(HoldExcept hold, Environment* saved) { + const auto before = ReadEnvironment(); + *__error_nid_postfix() = 123; + Require(hold(saved) == 0, "feholdexcept succeeds"); + const auto after = ReadEnvironment(); + Require(std::memcmp(saved, &before, sizeof(before)) == 0, "feholdexcept saves the full original environment"); + Require((after.x87[0] & 0x3f) == 0x3f && (after.sse & 0x1f80) == 0x1f80, "feholdexcept masks both units"); + Require((after.x87[1] & 0x80ff) == 0 && (after.sse & 0x3f) == 0, "feholdexcept clears pending exceptions"); + Require((after.x87[1] & ~0x80ffu) == (before.x87[1] & ~0x80ffu), "feholdexcept preserves other x87 status bits"); + Require((after.x87[0] & ~0x3fu) == (before.x87[0] & ~0x3fu), "feholdexcept preserves x87 control settings"); + Require((after.sse & ~0x1fbfu) == (before.sse & ~0x1fbfu), "feholdexcept preserves SSE control settings"); + Require(after.x87[2] == before.x87[2] && std::memcmp(after.x87.data() + 3, before.x87.data() + 3, 16) == 0, "feholdexcept preserves x87 tags and instruction/data state"); + Require(*__error_nid_postfix() == 123, "feholdexcept preserves errno"); +} + +void CheckHoldExcept(HoldExcept hold) { + Require(hold != nullptr, "feholdexcept export is missing"); + static_assert(sizeof(Environment) == 32 && alignof(Environment) == 4 && offsetof(Environment, sse) == 28); + const auto original = ReadEnvironment(); + struct { + std::array before; + Environment value; + std::array after; + } output; + for (unsigned rounding = 0; rounding < 4; ++rounding) { + for (unsigned modes = 0; modes < 4; ++modes) { + for (unsigned mask = 0; mask < 64; ++mask) { + auto state = original; + const unsigned sseMask = (mask * 13 + 7) & 0x3f; + state.x87[0] = (state.x87[0] & ~0xc3fu) | (rounding << 10) | mask; + state.x87[1] = (state.x87[1] & ~0x80ffu) | (mask & 0x25); + state.sse = (state.sse & ~0xffffu) | (sseMask << 7) | (sseMask & 0x1a) | (((rounding + 1) & 3) << 13) | ((modes & 1) << 6) | ((modes & 2) << 14); + WriteEnvironment(state); + std::memset(&output, 0xa5, sizeof(output)); + CheckHold(hold, &output.value); + for (const auto byte : output.before) Require(byte == 0xa5, "feholdexcept underwrites output"); + for (const auto byte : output.after) Require(byte == 0xa5, "feholdexcept overwrites output"); + } + } + } + WriteEnvironment(original); + bool threw = false; + try { hold(nullptr); } catch (const std::invalid_argument&) { threw = true; } + Require(threw, "feholdexcept rejects null storage"); + const auto afterNull = ReadEnvironment(); + Require(std::memcmp(&original, &afterNull, sizeof(original)) == 0, "null feholdexcept preserves the environment"); +#ifdef _WIN32 + SYSTEM_INFO info{}; + GetSystemInfo(&info); + const std::size_t page = info.dwPageSize; + auto* memory = static_cast(VirtualAlloc(nullptr, page * 2, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)); + Require(memory != nullptr, "allocate feholdexcept guard pages"); + DWORD oldProtection; + Require(VirtualProtect(memory + page, page, PAGE_NOACCESS, &oldProtection) != 0, "protect feholdexcept trailing page"); +#else + const auto page = static_cast(sysconf(_SC_PAGESIZE)); + auto* memory = static_cast(mmap(nullptr, page * 2, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)); + Require(memory != MAP_FAILED, "allocate feholdexcept guard pages"); + Require(mprotect(memory + page, page, PROT_NONE) == 0, "protect feholdexcept trailing page"); +#endif + CheckHold(hold, reinterpret_cast(memory + page - sizeof(Environment))); +#ifdef _WIN32 + Require(VirtualFree(memory, 0, MEM_RELEASE) != 0, "release feholdexcept guard pages"); +#else + Require(munmap(memory, page * 2) == 0, "release feholdexcept guard pages"); +#endif + WriteEnvironment(original); + std::barrier ready(2); + const auto worker = [&](unsigned mode) { + const auto saved = ReadEnvironment(); + auto state = saved; + state.x87[0] = (state.x87[0] & ~0xc00u) | (mode << 10) | 0x3f; + state.x87[1] &= ~0x80ffu; + state.sse |= 0x1f80; + state.sse &= ~0x3fu; + WriteEnvironment(state); + ready.arrive_and_wait(); + Environment output; + CheckHold(hold, &output); + ready.arrive_and_wait(); + Require(fegetround_nid_postfix() == static_cast(mode << 10), "feholdexcept keeps thread-local rounding"); + WriteEnvironment(saved); + }; + std::thread first(worker, 1); + std::thread second(worker, 2); + first.join(); + second.join(); + const auto afterThreads = ReadEnvironment(); + Require(std::memcmp(&original, &afterThreads, sizeof(original)) == 0, "feholdexcept workers preserve the main environment"); +} + +int main(int argc, char** argv) { + Require(argc == 2, "expected libc path"); +#ifdef _WIN32 + const auto library = LoadLibraryA(argv[1]); + Require(library != nullptr, "load libc for feholdexcept"); + const auto hold = reinterpret_cast(GetProcAddress(library, "feholdexcept_nid_postfix")); +#else + const auto library = dlopen(argv[1], RTLD_NOW); + Require(library != nullptr, "load libc for feholdexcept"); + const auto hold = reinterpret_cast(dlsym(library, "feholdexcept_nid_postfix")); +#endif const auto original = ReadEnvironment(); CheckRounding(); + CheckEnabledExceptions(); CheckFlags(); CheckTraps(); CheckThreads(); + CheckHoldExcept(hold); WriteEnvironment(original); +#ifdef _WIN32 + FreeLibrary(library); +#else + dlclose(library); +#endif std::puts("floating-point environment checks passed"); } diff --git a/core/libs/tests/GuestFnmatch.cpp b/core/libs/tests/GuestFnmatch.cpp index fbac3b5ae8..e32ada6dfd 100644 --- a/core/libs/tests/GuestFnmatch.cpp +++ b/core/libs/tests/GuestFnmatch.cpp @@ -32,10 +32,11 @@ static void CheckCaseInsensitiveBytePairs() { char left[] = {static_cast(a), 0}; char right[] = {static_cast(b), 0}; const int expected = FoldCByte(a) - FoldCByte(b); - Require(strcasecmp_nid_postfix(left, right) == expected); + const int sign = (expected > 0) - (expected < 0); + Require(strcasecmp_nid_postfix(left, right) == sign); Require(strncasecmp_nid_postfix(left, right, 0) == 0); - Require(strncasecmp_nid_postfix(left, right, 1) == expected); - Require(strncasecmp_nid_postfix(left, right, 2) == expected); + Require(strncasecmp_nid_postfix(left, right, 1) == sign); + Require(strncasecmp_nid_postfix(left, right, 2) == sign); const char* match = b == 0 || expected == 0 ? left : nullptr; Require(strcasestr_nid_postfix(left, right) == match); const char pattern[] = {'\\', static_cast(a), 0}; @@ -51,9 +52,9 @@ static void CheckCaseInsensitiveBytePairs() { Require(strcasestr_nid_postfix(repeated, "aaac") == repeated + 6); Require(strcasestr_nid_postfix(repeated, "aAa") == repeated); Require(strcasestr_nid_postfix(repeated, "aaad") == nullptr); - Require(strcasecmp_nid_postfix("AbC", "aBcD") == -'d'); + Require(strcasecmp_nid_postfix("AbC", "aBcD") == -1); Require(strncasecmp_nid_postfix("AbC", "aBcD", 3) == 0); - Require(strncasecmp_nid_postfix("AbC", "aBcD", 4) == -'d'); + Require(strncasecmp_nid_postfix("AbC", "aBcD", 4) == -1); } static void CheckCaseInsensitiveHostLocale() { diff --git a/core/libs/tests/GuestForceTlsDestructor.cpp b/core/libs/tests/GuestForceTlsDestructor.cpp index 6f900b9396..2bf6d13807 100644 --- a/core/libs/tests/GuestForceTlsDestructor.cpp +++ b/core/libs/tests/GuestForceTlsDestructor.cpp @@ -57,7 +57,7 @@ void* APS5_VABI Worker(void*) { KernelModule ModuleOf(const void* address) { ModuleInfoEx info{}; info.st_size = sizeof(ModuleInfoEx); - Require(sceKernelGetModuleInfoFromAddr(reinterpret_cast(address), 2, &info) == 0); + Require(sceKernelGetModuleInfoFromAddr(reinterpret_cast(address), 1, &info) == 0); return info.id; } diff --git a/core/libs/tests/GuestFreeType.cpp b/core/libs/tests/GuestFreeType.cpp index 732c2c67a3..422ebe3faf 100644 --- a/core/libs/tests/GuestFreeType.cpp +++ b/core/libs/tests/GuestFreeType.cpp @@ -6,6 +6,8 @@ #include FT_FREETYPE_H #include FT_GASP_H #include FT_OUTLINE_H +#include FT_TRIGONOMETRY_H +#include FT_CID_H #include #include @@ -27,6 +29,11 @@ using namespace FreeType; extern "C" { void APS5_VABI FT_Set_Transform_nid_postfix(GuestFaceRec*, const GuestMatrix*, const GuestVector*); +std::int64_t APS5_VABI FT_Cos_nid_postfix(std::int64_t); +std::int64_t APS5_VABI FT_Sin_nid_postfix(std::int64_t); +std::int64_t APS5_VABI FT_Tan_nid_postfix(std::int64_t); +std::int64_t APS5_VABI FT_Angle_Diff_nid_postfix(std::int64_t, std::int64_t); +void APS5_VABI FT_Vector_Unit_nid_postfix(GuestVector*, std::int64_t); int APS5_VABI FT_Init_FreeType_nid_postfix(void**); int APS5_VABI FT_Done_FreeType_nid_postfix(void*); int APS5_VABI FT_New_Face_nid_postfix(void*, const char*, std::int64_t, GuestFaceRec**); @@ -294,10 +301,359 @@ static void SizeMatches(const GuestFaceRec* face, FT_Face host) { Require(actual.height == expected.height && actual.max_advance == expected.max_advance); } +static void Trigonometry() { + constexpr std::int64_t circle = 360ll << 16; + const std::array, 68> poleTangents{{ + {-17694728ll, 33692373208ll}, + {-17694727ll, 45725363639ll}, + {-17694726ll, 45725363639ll}, + {-17694725ll, 64015509094ll}, + {-17694724ll, 64015509094ll}, + {-17694723ll, 128031018189ll}, + {-17694722ll, 128031018189ll}, + {-17694721ll, 640155090944ll}, + {-17694720ll, -640155090944ll}, + {-17694719ll, -128031018189ll}, + {-17694718ll, -128031018189ll}, + {-17694717ll, -64015509094ll}, + {-17694716ll, -64015509094ll}, + {-17694715ll, -45725363639ll}, + {-17694714ll, -45725363639ll}, + {-17694713ll, -33692373208ll}, + {-17694712ll, -33692373208ll}, + {-5898248ll, 33692373208ll}, + {-5898247ll, 45725363639ll}, + {-5898246ll, 45725363639ll}, + {-5898245ll, 64015509094ll}, + {-5898244ll, 64015509094ll}, + {-5898243ll, 128031018189ll}, + {-5898242ll, 128031018189ll}, + {-5898241ll, 640155090944ll}, + {-5898240ll, -640155090944ll}, + {-5898239ll, -128031018189ll}, + {-5898238ll, -128031018189ll}, + {-5898237ll, -64015509094ll}, + {-5898236ll, -64015509094ll}, + {-5898235ll, -45725363639ll}, + {-5898234ll, -45725363639ll}, + {-5898233ll, -33692373208ll}, + {-5898232ll, -33692373208ll}, + {5898232ll, 33692373208ll}, + {5898233ll, 45725363639ll}, + {5898234ll, 45725363639ll}, + {5898235ll, 64015509094ll}, + {5898236ll, 64015509094ll}, + {5898237ll, 128031018189ll}, + {5898238ll, 128031018189ll}, + {5898239ll, 640155090944ll}, + {5898240ll, -640155090944ll}, + {5898241ll, -128031018189ll}, + {5898242ll, -128031018189ll}, + {5898243ll, -64015509094ll}, + {5898244ll, -64015509094ll}, + {5898245ll, -45725363639ll}, + {5898246ll, -45725363639ll}, + {5898247ll, -33692373208ll}, + {5898248ll, -33692373208ll}, + {17694712ll, 33692373208ll}, + {17694713ll, 45725363639ll}, + {17694714ll, 45725363639ll}, + {17694715ll, 64015509094ll}, + {17694716ll, 64015509094ll}, + {17694717ll, 128031018189ll}, + {17694718ll, 128031018189ll}, + {17694719ll, 640155090944ll}, + {17694720ll, -640155090944ll}, + {17694721ll, -128031018189ll}, + {17694722ll, -128031018189ll}, + {17694723ll, -64015509094ll}, + {17694724ll, -64015509094ll}, + {17694725ll, -45725363639ll}, + {17694726ll, -45725363639ll}, + {17694727ll, -33692373208ll}, + {17694728ll, -33692373208ll}}}; + int scalarCases = 0; + int vectorCases = 0; + int differenceCases = 0; + for (const auto& [angle, tangent] : poleTangents) { + Require(FT_Tan_nid_postfix(angle) == tangent); + if (sizeof(FT_Fixed) == 4) { + Require(static_cast(FT_Tan(static_cast(angle))) != tangent); + } else { + Require(static_cast(FT_Tan(static_cast(angle))) == tangent); + } + } + auto compare = [&](std::int64_t angle, FT_Angle nativeAngle, std::int64_t expectedTangent) { + Require(FT_Cos_nid_postfix(angle) == FT_Cos(nativeAngle)); + Require(FT_Sin_nid_postfix(angle) == FT_Sin(nativeAngle)); + Require(FT_Tan_nid_postfix(angle) == expectedTangent); + FT_Vector expected{}; + FT_Vector_Unit(&expected, nativeAngle); + struct GuardedVector { + std::uint64_t before; + GuestVector vector; + std::uint64_t after; + } actual{0xA55A0FF0FF00AA55ull, {INT64_MAX, INT64_MIN}, 0x55AA00FFF00F5AA5ull}; + FT_Vector_Unit_nid_postfix(&actual.vector, angle); + Require(actual.vector.x == expected.x && actual.vector.y == expected.y); + Require(actual.vector.x == FT_Cos_nid_postfix(angle) && actual.vector.y == FT_Sin_nid_postfix(angle)); + Require(actual.before == 0xA55A0FF0FF00AA55ull && actual.after == 0x55AA00FFF00F5AA5ull); + ++scalarCases; + ++vectorCases; + }; + std::mt19937 random(0x17A693Bu); + std::uniform_int_distribution angles(INT32_MIN, INT32_MAX); + for (unsigned sample = 0; sample < 4096; ++sample) { + const std::int32_t angle = angles(random); + Require(FT_Cos_nid_postfix(angle) == FT_Cos(angle)); + Require(FT_Sin_nid_postfix(angle) == FT_Sin(angle)); + Require(static_cast(FT_Tan_nid_postfix(angle)) == FT_Tan(angle)); + FT_Vector expected{}; + GuestVector actual{}; + FT_Vector_Unit(&expected, angle); + FT_Vector_Unit_nid_postfix(&actual, angle); + Require(actual.x == expected.x && actual.y == expected.y); + ++scalarCases; + ++vectorCases; + } + const std::array bases{-circle + 1, -(180ll << 16), -(90ll << 16) - 1, -(45ll << 16), + -1, 0, 1, 25783, 45ll << 16, (90ll << 16) - 1, 90ll << 16, (90ll << 16) + 1, 180ll << 16, circle - 1}; + for (const std::int64_t turns : {0ll, 1ll, -1ll, 97ll, -97ll, 100000000000ll, -100000000000ll, 390000000000ll, -390000000000ll}) { + for (const std::int64_t base : bases) { + const std::int64_t tangent = FT_Tan_nid_postfix(base); + compare(base + circle * turns, static_cast(base), tangent); + for (const std::int64_t other : bases) { + Require(FT_Angle_Diff_nid_postfix(base + circle * turns, other + circle * turns) == + FT_Angle_Diff(static_cast(base), static_cast(other))); + ++differenceCases; + } + } + } + for (const auto boundary : std::array, 4>{ + std::pair{INT64_MIN, -16252928ll}, std::pair{INT64_MAX, 16252927ll}, + std::pair{INT64_MIN + 1, -16252927ll}, std::pair{INT64_MAX - 1, 16252926ll}}) { + Require(FT_Cos_nid_postfix(boundary.first) == FT_Cos(static_cast(boundary.second))); + Require(FT_Sin_nid_postfix(boundary.first) == FT_Sin(static_cast(boundary.second))); + Require(FT_Tan_nid_postfix(boundary.first) == FT_Tan_nid_postfix(boundary.second)); + FT_Vector expected{}; + GuestVector actual{}; + FT_Vector_Unit(&expected, static_cast(boundary.second)); + FT_Vector_Unit_nid_postfix(&actual, boundary.first); + Require(actual.x == expected.x && actual.y == expected.y); + ++scalarCases; + ++vectorCases; + } + FT_Vector_Unit_nid_postfix(nullptr, INT64_MIN); + Require(FT_Angle_Diff_nid_postfix(INT64_MIN, INT64_MIN + 1) == 1); + Require(FT_Angle_Diff_nid_postfix(INT64_MAX, INT64_MAX - 1) == -1); + Require(FT_Angle_Diff_nid_postfix(INT64_MIN, INT64_MIN) == 0); + Require(FT_Angle_Diff_nid_postfix(INT64_MAX, INT64_MAX) == 0); + for (const auto pair : std::array, 4>{ + std::pair{INT64_MIN, INT64_MAX}, std::pair{INT64_MAX, INT64_MIN}, std::pair{INT64_MIN, 0ll}, std::pair{0ll, INT64_MIN}}) { + if (pair.first == 0) { + Require(FT_Angle_Diff_nid_postfix(pair.first, pair.second) == FT_Angle_Diff_nid_postfix(0, -16252928)); + } else { + Require(ThrowsWith([&] { FT_Angle_Diff_nid_postfix(pair.first, pair.second); }, "FT_Angle_Diff: overflowing signed angle subtraction")); + } + } + const auto font = SquareGlyphFont('A', 700); + void* library = nullptr; + FT_Library oracle = nullptr; + GuestFaceRec* face = nullptr; + FT_Face native = nullptr; + Require(FT_Init_FreeType_nid_postfix(&library) == 0 && FT_Init_FreeType(&oracle) == 0); + Require(FT_New_Memory_Face_nid_postfix(library, font.data(), font.size(), 0, &face) == 0); + Require(FT_New_Memory_Face(oracle, font.data(), static_cast(font.size()), 0, &native) == 0); + int rendered = 0; + for (const unsigned pixels : {32u, 64u}) { + Require(FT_Set_Pixel_Sizes_nid_postfix(face, 0, pixels) == FT_Set_Pixel_Sizes(native, 0, pixels)); + for (const FT_Angle angle : {0L, 30L << 16, 45L << 16, 90L << 16, 135L << 16}) { + Require(FT_Load_Char_nid_postfix(face, 'A', FT_LOAD_DEFAULT) == FT_Load_Char(native, 'A', FT_LOAD_DEFAULT)); + GuestVector unit{}; + FT_Vector_Unit_nid_postfix(&unit, angle + circle * 100000000000ll); + for (unsigned point = 0; point < face->glyph->outline.n_points; ++point) { + auto& value = face->glyph->outline.points[point]; + const auto x = static_cast(value.x); + const auto y = static_cast(value.y); + value = {FT_MulFix(x, static_cast(unit.x)) - FT_MulFix(y, static_cast(unit.y)), + FT_MulFix(x, static_cast(unit.y)) + FT_MulFix(y, static_cast(unit.x))}; + } + FT_Matrix matrix{FT_Cos(angle), -FT_Sin(angle), FT_Sin(angle), FT_Cos(angle)}; + FT_Outline_Transform(&native->glyph->outline, &matrix); + Require(FT_Render_Glyph_nid_postfix(face->glyph, FT_RENDER_MODE_NORMAL) == FT_Render_Glyph(native->glyph, FT_RENDER_MODE_NORMAL)); + SlotMatches(face->glyph, native->glyph); + ++rendered; + } + } + Require(FT_Done_FreeType_nid_postfix(library) == 0 && FT_Done_FreeType(oracle) == 0); + std::printf("FreeType trigonometry: %d scalar cases, %d vectors, %d angle differences, 68 LP64 tangent goldens, %d rendered glyphs\n", + scalarCases, vectorCases, differenceCases, rendered); +} + struct Finalizers { std::vector calls; }; +extern "C" { +int APS5_VABI FT_Get_CID_Registry_Ordering_Supplement_nid_postfix(GuestFaceRec*, const char**, const char**, std::int32_t*); +int APS5_VABI FT_Get_CID_Is_Internally_CID_Keyed_nid_postfix(GuestFaceRec*, std::uint8_t*); +int APS5_VABI FT_Get_CID_From_Glyph_Index_nid_postfix(GuestFaceRec*, std::uint32_t, std::uint32_t*); +} + +static std::vector CidMetadataFont(bool keyed, const std::string& registry, const std::string& ordering, + int supplement, unsigned cid) { + const auto index = [](const std::vector>& objects) { + std::vector result; + Put16(result, objects.size()); + if (objects.empty()) return result; + result.insert(result.end(), {1, 1}); + unsigned offset = 1; + for (const auto& object : objects) { + offset += object.size(); + Require(offset < 256); + result.push_back(static_cast(offset)); + } + for (const auto& object : objects) result.insert(result.end(), object.begin(), object.end()); + return result; + }; + const auto integer = [](std::vector& dict, int value) { + dict.push_back(29); + Put32(dict, static_cast(value)); + }; + const auto bytes = [](const std::string& text) { return std::vector(text.begin(), text.end()); }; + std::array offsets{}; + std::vector font; + for (unsigned pass = 0; pass < 2; ++pass) { + std::vector top; + for (const int value : {0, 0, 700, 800}) integer(top, value); + top.push_back(5); + if (keyed) { + for (const int value : {391, 392, supplement}) integer(top, value); + top.insert(top.end(), {12, 30}); + integer(top, cid + 1); + top.insert(top.end(), {12, 34}); + } + integer(top, offsets[0]); top.push_back(15); + integer(top, offsets[1]); top.push_back(17); + if (keyed) { + integer(top, offsets[3]); top.insert(top.end(), {12, 36}); + integer(top, offsets[2]); top.insert(top.end(), {12, 37}); + } + font = {1, 0, 4, 4}; + const auto append = [&](const std::vector>& objects) { + const auto data = index(objects); + font.insert(font.end(), data.begin(), data.end()); + }; + append({bytes(keyed ? "TestCID" : "TestNamed")}); + append({top}); + append({bytes(registry), bytes(ordering)}); + append({}); + offsets[0] = static_cast(font.size()); + font.push_back(0); Put16(font, cid); + offsets[2] = static_cast(font.size()); + font.insert(font.end(), {0, 0, 0}); + offsets[1] = static_cast(font.size()); + append({{248, 236, 14}, {248, 236, 239, 139, 21, 248, 136, 139, 139, 249, 80, 252, 136, 139, 139, 253, 80, 5, 14}}); + offsets[3] = static_cast(font.size()); + std::vector privateDict; + integer(privateDict, 0); integer(privateDict, offsets[4]); privateDict.push_back(18); + append({privateDict}); + offsets[4] = static_cast(font.size()); + } + return font; +} + +static bool SameCidString(const char* a, const char* b) { return a && b ? std::strcmp(a, b) == 0 : a == b; } + +static unsigned cidQueries = 0; + +static void CheckCidQueries(GuestFaceRec* face, FT_Face native, int kind, unsigned cid, int supplement, + const char* registry, const char* ordering) { + for (unsigned mask = 0; mask < 8; ++mask) { + struct Result { + std::uint64_t before = 0x123456789ABCDEF0; + const char* registry = "registry sentinel"; + const char* ordering = "ordering sentinel"; + std::int32_t supplement = 0x12345678; + std::uint32_t after = 0xFEDCBA98; + } actual, expected; + const int error = FT_Get_CID_Registry_Ordering_Supplement_nid_postfix(face, mask & 1 ? &actual.registry : nullptr, + mask & 2 ? &actual.ordering : nullptr, mask & 4 ? &actual.supplement : nullptr); + Require(error == FT_Get_CID_Registry_Ordering_Supplement(native, mask & 1 ? &expected.registry : nullptr, + mask & 2 ? &expected.ordering : nullptr, mask & 4 ? &expected.supplement : nullptr)); + Require(error == (kind > 0 ? 0 : FT_Err_Invalid_Argument)); + Require(SameCidString(actual.registry, expected.registry) && SameCidString(actual.ordering, expected.ordering)); + Require(actual.supplement == expected.supplement && actual.before == 0x123456789ABCDEF0 && actual.after == 0xFEDCBA98); + if (mask & 1) Require(SameCidString(actual.registry, kind > 0 ? registry : nullptr)); + if (mask & 2) Require(SameCidString(actual.ordering, kind > 0 ? ordering : nullptr)); + if (mask & 4) Require(actual.supplement == (kind > 0 ? supplement : 0)); + ++cidQueries; + } + const char* alias = "sentinel"; + const char* nativeAlias = "sentinel"; + Require(FT_Get_CID_Registry_Ordering_Supplement_nid_postfix(face, &alias, &alias, nullptr) == + FT_Get_CID_Registry_Ordering_Supplement(native, &nativeAlias, &nativeAlias, nullptr)); + Require(SameCidString(alias, nativeAlias) && SameCidString(alias, kind > 0 ? ordering : nullptr)); + ++cidQueries; + for (const bool output : {false, true}) { + struct Result { std::uint8_t before = 0x12; std::uint8_t value = 0xA5; std::array after{0x34, 0x56, 0x78}; } actual, expected; + const int error = FT_Get_CID_Is_Internally_CID_Keyed_nid_postfix(face, output ? &actual.value : nullptr); + Require(error == FT_Get_CID_Is_Internally_CID_Keyed(native, output ? &expected.value : nullptr)); + Require(error == (kind >= 0 ? 0 : FT_Err_Invalid_Argument)); + Require(actual.value == expected.value && (!output || actual.value == (kind > 0 ? 1 : 0))); + Require(actual.before == 0x12 && actual.after == expected.after); + ++cidQueries; + for (const unsigned glyph : {0u, 1u, 2u, 41u, 65535u, UINT32_MAX}) { + struct Mapping { std::uint32_t before = 0x12345678; std::uint32_t value = 0xDEADBEEF; std::uint32_t after = 0x87654321; } actualCid, nativeCid; + const int mappingError = FT_Get_CID_From_Glyph_Index_nid_postfix(face, glyph, output ? &actualCid.value : nullptr); + Require(mappingError == FT_Get_CID_From_Glyph_Index(native, glyph, output ? &nativeCid.value : nullptr)); + Require(mappingError == (kind > 0 && glyph < 2 ? 0 : FT_Err_Invalid_Argument)); + Require(actualCid.value == nativeCid.value && (!output || actualCid.value == (mappingError == 0 && glyph == 1 ? cid : 0))); + Require(actualCid.before == 0x12345678 && actualCid.after == 0x87654321); + ++cidQueries; + } + } +} + +static void CheckCidMetadata() { + CheckCidQueries(nullptr, nullptr, -2, 0, 0, nullptr, nullptr); + struct Fixture { std::vector bytes; int kind; unsigned cid; int supplement; const char* registry; const char* ordering; unsigned renderGlyph; }; + const std::array fixtures{{ + {CidMetadataFont(true, "Adobe", "Identity", 7, 41), 1, 41, 7, "Adobe", "Identity", 41}, + {CidMetadataFont(true, "TestRegistry", "TestOrdering", 9, 65535), 1, 65535, 9, "TestRegistry", "TestOrdering", 65535}, + {CidMetadataFont(false, "Adobe", "Identity", 0, 41), 0, 0, 0, nullptr, nullptr, 1}, + {SquareGlyphFont('A', 700), -1, 0, 0, nullptr, nullptr, 1}}}; + for (const auto& fixture : fixtures) { + void* library = nullptr; + GuestFaceRec* face = nullptr; + FT_Library nativeLibrary = nullptr; + FT_Face native = nullptr; + Require(FT_Init_FreeType_nid_postfix(&library) == 0 && FT_Init_FreeType(&nativeLibrary) == 0); + Require(FT_New_Memory_Face_nid_postfix(library, fixture.bytes.data(), fixture.bytes.size(), 0, &face) == 0); + Require(FT_New_Memory_Face(nativeLibrary, fixture.bytes.data(), static_cast(fixture.bytes.size()), 0, &native) == 0); + CheckCidQueries(face, native, fixture.kind, fixture.cid, fixture.supplement, fixture.registry, fixture.ordering); + const char* registry = nullptr; + const char* ordering = nullptr; + if (fixture.kind > 0) Require(FT_Get_CID_Registry_Ordering_Supplement_nid_postfix(face, ®istry, &ordering, nullptr) == 0); + Require(FT_Set_Pixel_Sizes_nid_postfix(face, 0, 32) == 0 && FT_Set_Pixel_Sizes(native, 0, 32) == 0); + Require(FT_Load_Glyph_nid_postfix(face, fixture.renderGlyph, FT_LOAD_NO_HINTING | FT_LOAD_NO_BITMAP) == 0); + Require(FT_Load_Glyph(native, fixture.renderGlyph, FT_LOAD_NO_HINTING | FT_LOAD_NO_BITMAP) == 0); + Require(FT_Render_Glyph_nid_postfix(face->glyph, FT_RENDER_MODE_NORMAL) == 0 && FT_Render_Glyph(native->glyph, FT_RENDER_MODE_NORMAL) == 0); + SlotMatches(face->glyph, native->glyph); + Require(face->glyph->bitmap.width > 0 && face->glyph->bitmap.rows > 0); + if (fixture.kind > 0) { + const char* againRegistry = nullptr; + const char* againOrdering = nullptr; + Require(FT_Get_CID_Registry_Ordering_Supplement_nid_postfix(face, &againRegistry, &againOrdering, nullptr) == 0); + Require(registry == againRegistry && ordering == againOrdering); + Require(SameCidString(registry, fixture.registry) && SameCidString(ordering, fixture.ordering)); + } + Require(FT_Done_Face_nid_postfix(face) == 0 && FT_Done_Face(native) == 0); + Require(FT_Done_FreeType_nid_postfix(library) == 0 && FT_Done_FreeType(nativeLibrary) == 0); + } + std::printf("FreeType CID metadata: %u query scenarios, 4 fonts and 4 rendered glyphs passed\n", cidQueries); +} + static void NativeSlotFinalizer(void* object) { auto* slot = static_cast(object); Require(slot->face->glyph == nullptr); @@ -437,12 +793,14 @@ int main() { const auto root = std::filesystem::temp_directory_path() / ("anyps5_guest_freetype-" + std::to_string(std::random_device{}())); std::filesystem::create_directories(root / "app0"); CheckGasp(); + Trigonometry(); const std::vector font = SquareGlyphFont('A', 700); { std::ofstream file(root / "app0" / "font.ttf", std::ios::binary); file.write(reinterpret_cast(font.data()), static_cast(font.size())); } const auto previous = std::filesystem::current_path(); + CheckCidMetadata(); std::filesystem::current_path(root); CheckOutlineOrientation(); diff --git a/core/libs/tests/GuestFreeTypeBitmap.cpp b/core/libs/tests/GuestFreeTypeBitmap.cpp new file mode 100644 index 0000000000..a0d5ddb9bc --- /dev/null +++ b/core/libs/tests/GuestFreeTypeBitmap.cpp @@ -0,0 +1,298 @@ +#include "FontFixture.hpp" +#include "prx/libSceFreeType/include/GuestObjects.hpp" +#include "prx/libc/include/general/VabiMacros.hpp" + +#include FT_BITMAP_H +#include FT_MODULE_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace FreeType; + +extern "C" { +int APS5_VABI FT_Init_FreeType_nid_postfix(GuestLibrary**); +int APS5_VABI FT_Load_Glyph_nid_postfix(GuestFaceRec*, std::uint32_t, std::int32_t); +void APS5_VABI FT_Bitmap_New_nid_postfix(GuestBitmap*); +int APS5_VABI FT_Bitmap_Copy_nid_postfix(GuestLibrary*, const GuestBitmap*, GuestBitmap*); +int APS5_VABI FT_Bitmap_Convert_nid_postfix(GuestLibrary*, const GuestBitmap*, GuestBitmap*, std::int32_t); +int APS5_VABI FT_Bitmap_Done_nid_postfix(GuestLibrary*, GuestBitmap*); +} + +static void Check(bool value, int line) { + if (!value) { + std::fprintf(stderr, "FreeType bitmap check failed at line %d\n", line); + std::abort(); + } +} +#define Require(value) Check((value), __LINE__) + +static GuestBitmap Guest(const FT_Bitmap& bitmap) { + return {bitmap.rows, bitmap.width, bitmap.pitch, bitmap.buffer, bitmap.num_grays, bitmap.pixel_mode, bitmap.palette_mode, bitmap.palette}; +} + +static void Matches(const GuestBitmap& guest, const FT_Bitmap& native, bool comparePadding) { + Require(guest.rows == native.rows && guest.width == native.width && guest.pitch == native.pitch); + Require(guest.num_grays == native.num_grays && guest.pixel_mode == native.pixel_mode && guest.palette_mode == native.palette_mode); + Require(guest.palette == native.palette && (guest.buffer == nullptr) == (native.buffer == nullptr)); + if (!guest.buffer) return; + const auto pitch = static_cast(native.pitch < 0 ? -native.pitch : native.pitch); + const std::size_t width = comparePadding ? pitch : native.width; + for (std::uint32_t row = 0; row < native.rows; ++row) { + Require(std::memcmp(guest.buffer + row * pitch, native.buffer + row * pitch, width) == 0); + } +} + +static void TestFormat(GuestLibrary* library, FT_Library oracle, unsigned char mode, int bits) { + constexpr std::uint32_t width = 10; + constexpr std::uint32_t rows = 3; + const int stride = static_cast((width * bits + 7) / 8) + 2; + std::vector data(rows * static_cast(stride)); + for (std::size_t index = 0; index < data.size(); ++index) data[index] = static_cast(index * 37 + 11); + if (mode == FT_PIXEL_MODE_BGRA) { + for (std::uint32_t row = 0; row < rows; ++row) { + for (std::uint32_t col = 0; col < width; ++col) { + unsigned char* pixel = data.data() + row * stride + col * 4; + pixel[0] = static_cast(col * 7); + pixel[1] = static_cast(row * 23); + pixel[2] = static_cast(col * 11); + pixel[3] = col == 0 ? 0 : static_cast(128 + row * 63); + if (col == 0) pixel[0] = pixel[1] = pixel[2] = 0; + } + } + } + const auto original = data; + int palette = 17; + for (const int sign : {-1, 1}) { + const FT_Bitmap source{rows, width, sign * stride, data.data(), static_cast(bits < 8 ? 1 << bits : 256), mode, 7, &palette}; + GuestBitmap guestSource = Guest(source); + for (const int flow : {-1, 0, 1}) { + FT_Bitmap native{}; + native.pitch = flow; + GuestBitmap target{}; + target.pitch = flow; + Require(FT_Bitmap_Copy_nid_postfix(library, &guestSource, &target) == FT_Bitmap_Copy(oracle, &source, &native)); + Require(target.buffer != source.buffer && native.buffer != source.buffer && target.buffer != native.buffer); + Matches(target, native, true); + const GuestBitmap before = target; + Require(FT_Bitmap_Copy_nid_postfix(library, &target, &target) == 0); + Require(target.buffer == before.buffer && target.pitch == before.pitch); + Matches(target, native, true); + data[0] ^= 0xFF; + Require(target.buffer[flow != 0 && flow != sign ? (rows - 1) * stride : 0] == original[0]); + data[0] ^= 0xFF; + for (const int alignment : {0, 1, 2, 4, -4}) { + Require(FT_Bitmap_Convert_nid_postfix(library, &guestSource, &target, alignment) == FT_Bitmap_Convert(oracle, &source, &native, alignment)); + Matches(target, native, false); + Require(target.pixel_mode == FT_PIXEL_MODE_GRAY && target.rows == rows && target.width == width); + Require(target.pitch == (flow < 0 || (flow == 0 && sign < 0) ? -1 : 1) * (alignment == 4 || alignment == -4 ? 12 : 10)); + Require(data == original); + } + Require(FT_Bitmap_Done_nid_postfix(library, &target) == FT_Bitmap_Done(oracle, &native)); + Matches(target, native, true); + Require(target.rows == 0 && target.pitch == 0 && target.buffer == nullptr && target.palette == nullptr); + Require(FT_Bitmap_Done_nid_postfix(library, &target) == 0); + } + } +} + +struct Allocations { + bool fail = false; + std::unordered_set blocks; + + static void* Allocate(FT_Memory memory, long size) { + auto& state = *static_cast(memory->user); + if (state.fail) return nullptr; + void* block = std::malloc(static_cast(size)); + if (block) Require(state.blocks.insert(block).second); + return block; + } + + static void Free(FT_Memory memory, void* block) { + auto& state = *static_cast(memory->user); + Require(state.blocks.erase(block) == 1); + std::free(block); + } + + static void* Reallocate(FT_Memory, long, long, void*) { + return nullptr; + } +}; + +static void TestAllocationFailure() { + Allocations guestAllocations; + Allocations nativeAllocations; + FT_MemoryRec_ guestMemory{&guestAllocations, Allocations::Allocate, Allocations::Free, Allocations::Reallocate}; + FT_MemoryRec_ nativeMemory{&nativeAllocations, Allocations::Allocate, Allocations::Free, Allocations::Reallocate}; + GuestLibrary library{}; + FT_Library oracle = nullptr; + Require(FT_New_Library(&guestMemory, &library.host) == 0); + Require(FT_New_Library(&nativeMemory, &oracle) == 0); + const auto guestBase = guestAllocations.blocks.size(); + const auto nativeBase = nativeAllocations.blocks.size(); + unsigned char pixels[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; + const FT_Bitmap source{2, 5, 5, pixels, 256, FT_PIXEL_MODE_GRAY, 0, nullptr}; + const GuestBitmap guestSource = Guest(source); + GuestBitmap target{}; + FT_Bitmap native{}; + for (const bool convert : {false, true}) { + Require(FT_Bitmap_Copy_nid_postfix(&library, &guestSource, &target) == 0); + Require(FT_Bitmap_Copy(oracle, &source, &native) == 0); + Require(guestAllocations.blocks.size() == guestBase + 1 && nativeAllocations.blocks.size() == nativeBase + 1); + guestAllocations.fail = nativeAllocations.fail = true; + const FT_Error expected = convert ? FT_Bitmap_Convert(oracle, &source, &native, 4) : FT_Bitmap_Copy(oracle, &source, &native); + const int error = convert ? FT_Bitmap_Convert_nid_postfix(&library, &guestSource, &target, 4) : FT_Bitmap_Copy_nid_postfix(&library, &guestSource, &target); + Require(expected == FT_Err_Out_Of_Memory && error == expected); + Matches(target, native, false); + Require(target.buffer == nullptr && guestAllocations.blocks.size() == guestBase && nativeAllocations.blocks.size() == nativeBase); + guestAllocations.fail = nativeAllocations.fail = false; + } + Require(FT_Bitmap_Convert_nid_postfix(&library, &guestSource, &target, 4) == 0); + Require(FT_Bitmap_Convert(oracle, &source, &native, 4) == 0); + Matches(target, native, false); + Require(FT_Bitmap_Done_nid_postfix(&library, &target) == 0 && FT_Bitmap_Done(oracle, &native) == 0); + Require(guestAllocations.blocks.size() == guestBase && nativeAllocations.blocks.size() == nativeBase); + Require(FT_Done_Library(library.host) == 0 && FT_Done_Library(oracle) == 0); + Require(guestAllocations.blocks.empty() && nativeAllocations.blocks.empty()); +} + +struct SlotFinalizer { + GuestFace* face; + GuestLibrary* library; + bool called = false; + + static void Run(void* object) { + auto* slot = static_cast(object); + auto& state = *static_cast(slot->generic.data); + Require(slot->face->glyph == nullptr); + Require(FT_Load_Glyph_nid_postfix(&state.face->rec, 0, FT_LOAD_DEFAULT) == FT_Err_Invalid_Face_Handle); + Require(FT_Bitmap_Done_nid_postfix(state.library, &state.face->slot.bitmap) == 0); + Require(slot->bitmap.buffer == nullptr && slot->bitmap.width == 0); + state.called = true; + } +}; + +static void TestGlyphSlot(GuestLibrary* library, FT_Library oracle) { + const auto bytes = FontFixture::BuildFont(1, {}); + GuestFace face; + face.library = library; + Require(FT_New_Memory_Face(library->host, bytes.data(), static_cast(bytes.size()), 0, &face.host) == 0); + FT_Face nativeFace = nullptr; + Require(FT_New_Memory_Face(oracle, bytes.data(), static_cast(bytes.size()), 0, &nativeFace) == 0); + SyncFace(face); + unsigned char pixels[]{7, 31, 65, 127, 255, 17, 61, 101, 189, 239}; + const FT_Bitmap source{2, 5, 5, pixels, 256, FT_PIXEL_MODE_GRAY, 0, nullptr}; + const GuestBitmap guestSource = Guest(source); + auto& target = face.slot.bitmap; + auto& native = nativeFace->glyph->bitmap; + Require(FT_Bitmap_Copy_nid_postfix(library, &guestSource, &target) == FT_Bitmap_Copy(oracle, &source, &native)); + Matches(target, native, true); + Require(face.host->glyph->bitmap.buffer == target.buffer && face.host->glyph->bitmap.width == 5); + GuestBitmap retained{}; + Require(FT_Bitmap_Copy_nid_postfix(library, &target, &retained) == 0); + Require(retained.buffer != target.buffer && std::memcmp(retained.buffer, target.buffer, 10) == 0); + Require(FT_Bitmap_Done_nid_postfix(library, &target) == FT_Bitmap_Done(oracle, &native)); + Require(face.host->glyph->bitmap.buffer == nullptr && face.host->glyph->bitmap.width == 0); + Require(FT_Bitmap_Convert_nid_postfix(library, &guestSource, &target, 4) == FT_Bitmap_Convert(oracle, &source, &native, 4)); + Matches(target, native, false); + Require(face.host->glyph->bitmap.buffer == target.buffer && face.host->glyph->bitmap.pitch == 8); + Require(FT_Bitmap_Done_nid_postfix(library, &target) == 0 && FT_Bitmap_Done(oracle, &native) == 0); + target.rows = 9; + target.width = 17; + face.host->glyph->bitmap.rows = 9; + face.host->glyph->bitmap.width = 17; + FT_Bitmap_New_nid_postfix(&target); + Require(target.rows == 0 && face.host->glyph->bitmap.rows == 0 && face.host->glyph->bitmap.width == 0); + Require(FT_Bitmap_Done_nid_postfix(library, &retained) == 0); + Require(FT_Bitmap_Copy_nid_postfix(library, &guestSource, &target) == 0); + SlotFinalizer finalizer{&face, library}; + face.host->glyph->generic = {&finalizer, SlotFinalizer::Run}; + Require(FT_Done_Face(face.host) == 0 && FT_Done_Face(nativeFace) == 0); + Require(finalizer.called); +} + +static void TestReusedStorage(GuestLibrary* library) { + const auto bytes = FontFixture::BuildFont(1, {}); + alignas(GuestFace) std::byte storage[sizeof(GuestFace)]{}; + auto* face = new (storage) GuestFace; + face->library = library; + Require(FT_New_Memory_Face(library->host, bytes.data(), static_cast(bytes.size()), 0, &face->host) == 0); + FT_Face native = face->host; + SyncFace(*face); + void* address = &face->slot.bitmap; + face->~GuestFace(); + auto* bitmap = new (address) GuestBitmap{}; + native->glyph->bitmap.width = 73; + FT_Bitmap_New_nid_postfix(bitmap); + Require(bitmap->width == 0 && native->glyph->bitmap.width == 73); + Require(FT_Done_Face(native) == 0); +} + +int main() { + GuestLibrary* library = nullptr; + FT_Library oracle = nullptr; + Require(FT_Init_FreeType_nid_postfix(&library) == 0 && library != nullptr); + Require(FT_Init_FreeType(&oracle) == 0); + + GuestBitmap empty{1, 2, 3, nullptr, 4, 5, 6, nullptr}; + FT_Bitmap_New_nid_postfix(&empty); + FT_Bitmap nativeEmpty{}; + Matches(empty, nativeEmpty, true); + Require(empty.rows == 0 && empty.width == 0 && empty.pitch == 0); + FT_Bitmap_New_nid_postfix(nullptr); + Require(FT_Bitmap_Copy_nid_postfix(nullptr, nullptr, nullptr) == FT_Err_Invalid_Library_Handle); + Require(FT_Bitmap_Convert_nid_postfix(nullptr, nullptr, nullptr, 1) == FT_Err_Invalid_Library_Handle); + Require(FT_Bitmap_Done_nid_postfix(nullptr, nullptr) == FT_Err_Invalid_Library_Handle); + Require(FT_Bitmap_Copy_nid_postfix(library, nullptr, &empty) == FT_Err_Invalid_Argument); + Require(FT_Bitmap_Copy_nid_postfix(library, &empty, nullptr) == FT_Err_Invalid_Argument); + Require(FT_Bitmap_Convert_nid_postfix(library, nullptr, &empty, 1) == FT_Err_Invalid_Argument); + Require(FT_Bitmap_Convert_nid_postfix(library, &empty, nullptr, 1) == FT_Err_Invalid_Argument); + Require(FT_Bitmap_Done_nid_postfix(library, nullptr) == FT_Err_Invalid_Argument); + Require(FT_Bitmap_Copy_nid_postfix(library, &empty, &empty) == 0); + + for (const auto [mode, bits] : {std::pair{FT_PIXEL_MODE_MONO, 1}, {FT_PIXEL_MODE_GRAY2, 2}, {FT_PIXEL_MODE_GRAY4, 4}, + {FT_PIXEL_MODE_GRAY, 8}, {FT_PIXEL_MODE_LCD, 8}, {FT_PIXEL_MODE_LCD_V, 8}, {FT_PIXEL_MODE_BGRA, 32}}) { + TestFormat(library, oracle, static_cast(mode), bits); + } + GuestBitmap target{}; + const GuestBitmap unsupported{0, 0, 0, nullptr, 0, FT_PIXEL_MODE_NONE, 0, nullptr}; + Require(FT_Bitmap_Copy_nid_postfix(library, &unsupported, &target) == 0); + Require(target.buffer == nullptr); + Require(FT_Bitmap_Convert_nid_postfix(library, &unsupported, &target, 1) == FT_Err_Invalid_Argument); + Require(target.buffer == nullptr && target.width == 0); + bool overlapThrew = false; + try { + FT_Bitmap_Convert_nid_postfix(library, &target, &target, 1); + } catch (const std::runtime_error&) { + overlapThrew = true; + } + Require(overlapThrew); + unsigned char sharedPixel = 17; + GuestBitmap shared{1, 1, 1, &sharedPixel, 256, FT_PIXEL_MODE_GRAY, 0, nullptr}; + GuestBitmap alias = shared; + for (const bool convert : {false, true}) { + overlapThrew = false; + try { + if (convert) FT_Bitmap_Convert_nid_postfix(library, &shared, &alias, 1); + else FT_Bitmap_Copy_nid_postfix(library, &shared, &alias); + } catch (const std::runtime_error&) { + overlapThrew = true; + } + Require(overlapThrew && sharedPixel == 17 && alias.buffer == &sharedPixel); + } + Require(FT_Bitmap_Done_nid_postfix(library, &target) == 0); + TestAllocationFailure(); + TestGlyphSlot(library, oracle); + TestReusedStorage(library); + Require(FT_Done_FreeType(library->host) == 0); + delete library; + Require(FT_Done_FreeType(oracle) == 0); +} diff --git a/core/libs/tests/GuestGets.cpp b/core/libs/tests/GuestGets.cpp new file mode 100644 index 0000000000..7b986b9381 --- /dev/null +++ b/core/libs/tests/GuestGets.cpp @@ -0,0 +1,155 @@ +#include "prx/libc/include/FileStream.hpp" +#include "prx/libc/include/general/VabiMacros.hpp" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#ifdef _WIN32 +#include +#else +#include +#endif + +extern "C" { +extern FileStream* __stdinp_nid_postfix; +int* APS5_VABI __error_nid_postfix(); +} + +namespace { + +using Gets = char* (APS5_VABI*)(char*); +Gets readLine; + +void Require(bool value, const char* message) { + if (value) return; + std::fprintf(stderr, "gets: %s\n", message); + std::abort(); +} + +class Input { + FileStream* previous = __stdinp_nid_postfix; + FileStream stream; + +public: + explicit Input(std::FILE* handle) : stream(handle) { __stdinp_nid_postfix = &stream; } + Input(const void* bytes, std::size_t size) : stream(std::tmpfile()) { + Require(std::fwrite(bytes, 1, size, stream.GetHandle()) == size, "seed input"); + std::rewind(stream.GetHandle()); + __stdinp_nid_postfix = &stream; + } + ~Input() { + __stdinp_nid_postfix = previous; + if (stream.IsOpen()) stream.Close(); + } + FileStream& Stream() { return stream; } +}; + +void CheckBytes() { + std::array bytes; + std::size_t size = 0; + for (unsigned value = 0; value < 256; ++value) + if (value != '\n') bytes[size++] = static_cast(value); + bytes[size++] = '\n'; + Input input(bytes.data(), size); + std::array output; + output.fill(0xa5); + *__error_nid_postfix() = 123; + Require(readLine(reinterpret_cast(output.data() + 4)) == reinterpret_cast(output.data() + 4), "byte read pointer"); + const int error = *__error_nid_postfix(); + Require(std::memcmp(output.data() + 4, bytes.data(), 255) == 0 && output[259] == 0, "all byte values and terminator"); + Require(error == 123, "successful read preserves errno"); + for (std::size_t index = 0; index < 4; ++index) Require(output[index] == 0xa5, "prefix guard"); + for (std::size_t index = 260; index < output.size(); ++index) Require(output[index] == 0xa5, "suffix guard"); + output.fill(0xa5); + Require(readLine(reinterpret_cast(output.data() + 4)) == nullptr, "EOF return"); + for (const auto value : output) Require(value == 0xa5, "initial EOF leaves buffer unchanged"); + Require((input.Stream().GuestState().flags & 0x20) != 0, "guest EOF flag"); +} + +void CheckLines() { + const char text[] = "\nfirst\r\nlast"; + Input input(text, sizeof(text) - 1); + char output[16]; + Require(readLine(output) == output && output[0] == 0, "empty line"); + Require(readLine(output) == output && std::strcmp(output, "first\r") == 0, "newline removed without dropping carriage return"); + Require(readLine(output) == output && std::strcmp(output, "last") == 0, "partial last line at EOF"); + output[0] = 'x'; + Require(readLine(output) == nullptr && output[0] == 'x', "repeated EOF preserves destination"); +} + +void CheckErrors() { + Input input("", 0); + input.Stream().Close(); + char output[8] = "keep"; + Require(readLine(output) == nullptr && std::strcmp(output, "keep") == 0 && *__error_nid_postfix() == 9, "closed input"); + bool threw = false; + try { readLine(nullptr); } catch (const std::invalid_argument&) { threw = true; } + Require(threw, "null buffer is explicit unsupported input"); +} + +void CheckReadError(const char* path) { + { + Input input(std::fopen(path, "wb")); + std::array output; + output.fill('x'); + Require(readLine(output.data()) == nullptr, "write-only input read error"); + for (const auto value : output) Require(value == 'x', "initial read error leaves destination unchanged"); + Require((input.Stream().GuestState().flags & 0x40) != 0, "guest error flag"); + } + Require(std::remove(path) == 0, "remove error fixture"); +} + +void CheckThreads() { + constexpr unsigned lines = 2048; + std::array text; + for (unsigned index = 0; index < lines; ++index) + std::snprintf(text.data() + index * 8, 9, "%07u\n", index); + Input input(text.data(), lines * 8); + std::array, lines> seen{}; + std::atomic invalid{false}; + std::vector workers; + for (unsigned thread = 0; thread < 8; ++thread) workers.emplace_back([&] { + char output[lines * 8 + 1]; + while (readLine(output)) { + if (std::strlen(output) != 7) { invalid = true; continue; } + unsigned index = 0; + bool digits = true; + for (unsigned digit = 0; digit < 7; ++digit) { + if (output[digit] < '0' || output[digit] > '9') { digits = false; break; } + index = index * 10 + static_cast(output[digit] - '0'); + } + if (!digits || index >= lines) invalid = true; + else ++seen[index]; + } + }); + for (auto& worker : workers) worker.join(); + Require(!invalid, "concurrent line contents"); + for (auto& count : seen) Require(count == 1, "each line read exactly once"); +} + +} + +int main(int argc, char** argv) { + Require(argc == 3, "expected libc path and scratch file"); +#ifdef _WIN32 + const auto library = LoadLibraryA(argv[1]); + Require(library != nullptr, "load libc"); + readLine = reinterpret_cast(GetProcAddress(library, "gets_nid_postfix")); +#else + const auto library = dlopen(argv[1], RTLD_NOW | RTLD_LOCAL); + Require(library != nullptr, "load libc"); + readLine = reinterpret_cast(dlsym(library, "gets_nid_postfix")); +#endif + Require(readLine != nullptr, "missing gets export"); + CheckBytes(); + CheckLines(); + CheckErrors(); + CheckReadError(argv[2]); + CheckThreads(); + std::puts("gets checks passed"); +} diff --git a/core/libs/tests/GuestGnmNeoEmbedded.cpp b/core/libs/tests/GuestGnmNeoEmbedded.cpp new file mode 100644 index 0000000000..e802446b9f --- /dev/null +++ b/core/libs/tests/GuestGnmNeoEmbedded.cpp @@ -0,0 +1,77 @@ +#include "prx/libc/include/general/VabiMacros.hpp" +#include +#include +#include + +extern "C" { +int APS5_VABI sceGnmSetEmbeddedVsShader(std::uint32_t*, std::uint32_t, std::uint32_t, std::uint32_t); +int APS5_VABI sceGnmSetEmbeddedPsShader(std::uint32_t*, std::uint32_t, std::uint32_t, std::uint32_t); +} + +namespace { + +constexpr int Failure = static_cast(0x8EEE00FFu); +constexpr std::uint32_t Untouched = 0xa5a5a5a5u; +constexpr std::uint32_t BufferWords = 48; + +std::uint32_t g_words[BufferWords]; + +void Require(bool value) { if (!value) std::abort(); } + +std::uint32_t* Fresh() { + for (auto& word : g_words) word = Untouched; + return g_words; +} + +void RequireAt(std::uint32_t at, std::initializer_list expected) { + for (const auto word : expected) Require(g_words[at++] == word); +} + +void RequireUntouchedFrom(std::uint32_t at) { + for (; at < BufferWords; ++at) Require(g_words[at] == Untouched); +} + +const std::uint32_t* ShaderCode() { + const auto address = (static_cast(g_words[3]) << 40) | (static_cast(g_words[2]) << 8); + return reinterpret_cast(address); +} + +void CheckVs() { + Require(sceGnmSetEmbeddedVsShader(Fresh(), 0x1d, 0, 0) == 0); + RequireAt(0, {0xc0027600u, 0x48}); + RequireAt(4, {0xc0027600u, 0x4a, 0xc0000, 4, 0xc0016900u, 0x207, 0, 0xc0016900u, 0x1b1, 0, 0xc0016900u, 0x1c3, 4, + 0xc00a1000u, 0}); + RequireUntouchedFrom(19); + const auto* code = ShaderCode(); + Require(code[0] == 0xbeeb0387u && code[10] == 0xf80008cfu && code[11] == 0x03020001u && code[14] == 0xbf810000u); + + Require(sceGnmSetEmbeddedVsShader(Fresh(), 0x1d, 1, 0) == Failure); + RequireUntouchedFrom(0); +} + +void CheckPs() { + Require(sceGnmSetEmbeddedPsShader(Fresh(), 0x28, 0, 0) == 0); + RequireAt(0, {0xc0027600u, 8}); + RequireAt(4, {0xc0027600u, 10, 0xc0000, 4, 0xc0026900u, 0x1c4, 0, 4, 0xc0026900u, 0x1b3, 2, 2, 0xc0016900u, 0x1b6, 0, + 0xc0016900u, 0x1b8, 0, 0xc0016900u, 0x203, 0x10, 0xc0016900u, 0x8f, 0xf, 0xc00a1000u, 0}); + RequireUntouchedFrom(30); + const auto* black = ShaderCode(); + Require(black[0] == 0xbeeb0383u && black[2] == 0x5e000100u && black[4] == 0xf8001c0fu && black[6] == 0xbf810000u); + + Require(sceGnmSetEmbeddedPsShader(Fresh(), 0x28, 1, 0) == 0); + RequireAt(4, {0xc0027600u, 10, 0x2000, 0, 0xc0026900u, 0x1c4, 0, 2, 0xc0026900u, 0x1b3, 2, 2, 0xc0016900u, 0x1b6, 0, + 0xc0016900u, 0x1b8, 0, 0xc0016900u, 0x203, 0x10, 0xc0016900u, 0x8f, 3, 0xc00a1000u, 0}); + RequireUntouchedFrom(30); + const auto* depthOnly = ShaderCode(); + Require(depthOnly != black && depthOnly[1] == 0x7e040280u && depthOnly[2] == 0xf8001803u && depthOnly[4] == 0xbf810000u); + + Require(sceGnmSetEmbeddedPsShader(Fresh(), 0x28, 2, 0) == Failure); + RequireUntouchedFrom(0); +} + +} // namespace + +int main() { + CheckVs(); + CheckPs(); +} diff --git a/core/libs/tests/GuestGnmNeoShaders.cpp b/core/libs/tests/GuestGnmNeoShaders.cpp new file mode 100644 index 0000000000..14f1f17264 --- /dev/null +++ b/core/libs/tests/GuestGnmNeoShaders.cpp @@ -0,0 +1,142 @@ +#include "prx/libc/include/general/VabiMacros.hpp" +#include +#include +#include + +extern "C" { +int APS5_VABI sceGnmUpdateVsShader(std::uint32_t*, std::uint32_t, const std::uint32_t*, std::uint32_t); +int APS5_VABI sceGnmUpdatePsShader(std::uint32_t*, std::uint32_t, const std::uint32_t*); +int APS5_VABI sceGnmUpdatePsShader350(std::uint32_t*, std::uint32_t, const std::uint32_t*); +int APS5_VABI sceGnmSetPsShader350(std::uint32_t*, std::uint32_t, const std::uint32_t*); +int APS5_VABI sceGnmUpdateGsShader(std::uint32_t*, std::uint32_t, const std::uint32_t*); +int APS5_VABI sceGnmUpdateHsShader(std::uint32_t*, std::uint32_t, const std::uint32_t*, std::uint32_t); +int APS5_VABI sceGnmSetCsShaderWithModifier(std::uint32_t*, std::uint32_t, const std::uint32_t*, std::uint32_t); +int APS5_VABI sceGnmCreateWorkloadStream(std::uint64_t, std::uint32_t*); +int APS5_VABI sceGnmBeginWorkload(std::uint32_t, std::uint64_t*); +int APS5_VABI sceGnmEndWorkload(std::uint64_t); +int APS5_VABI sceGnmGetGpuCoreClockFrequency(void); +int APS5_VABI sceGnmGetOffChipTessellationBufferSize(void); +} + +namespace { + +constexpr std::uint32_t Untouched = 0xa5a5a5a5u; +constexpr std::size_t BufferWords = 48; +constexpr std::uint32_t Regs[12] = {0x1000, 0, 0x2000, 0x3000, 0x4000, 0x5000, 0x6000, 0x7000, 0x8000, 0x9000, 0xa000, 0xb000}; +constexpr std::uint32_t BadRegs[12] = {0x1000, 1}; +constexpr std::uint32_t MaskedRegs[12] = {0x1000, 0, 0x030003c0u, 0x3000, 0x4000, 0x5000, 0x6000}; + +std::uint32_t g_words[BufferWords]; + +void Require(bool value) { if (!value) std::abort(); } + +std::uint32_t* Fresh() { + for (auto& word : g_words) word = Untouched; + return g_words; +} + +void RequireWords(std::initializer_list expected) { + std::size_t at = 0; + for (const auto word : expected) Require(g_words[at++] == word); + for (; at < BufferWords; ++at) Require(g_words[at] == Untouched); +} + +void RequireRefused(int result) { + Require(result == -1); + RequireWords({}); +} + +void CheckVs() { + Require(sceGnmUpdateVsShader(Fresh(), 0x1d, Regs, 0) == 0); + RequireWords({0xc0027600u, 0x48, 0x1000, 0, 0xc0027600u, 0x4a, 0x2000, 0x3000, 0xc0011000u, 0xc01e0207u, 0x6000, + 0xc0011000u, 0xc01e01b1u, 0x4000, 0xc0011000u, 0xc01e01c3u, 0x5000, 0xc00a1000u, 0}); + Require(sceGnmUpdateVsShader(Fresh(), 0x1d, MaskedRegs, 0x40) == 0); + Require(g_words[6] == 0x40); + Require(sceGnmUpdateVsShader(Fresh(), 0x1d, MaskedRegs, 0) == 0); + Require(g_words[6] == 0x030003c0u); + RequireRefused(sceGnmUpdateVsShader(Fresh(), 0x1d, Regs, 0x80000000u)); + RequireRefused(sceGnmUpdateVsShader(Fresh(), 0x1c, Regs, 0)); + RequireRefused(sceGnmUpdateVsShader(Fresh(), 0x1d, BadRegs, 0)); + RequireRefused(sceGnmUpdateVsShader(Fresh(), 0x1d, nullptr, 0)); +} + +void CheckPs() { + Require(sceGnmUpdatePsShader(Fresh(), 0x28, Regs) == 0); + RequireWords({0xc0027600u, 8, 0x1000, 0, 0xc0027600u, 10, 0x2000, 0x3000, 0xc0021000u, 0xc01e01c4u, 0x4000, 0x5000, + 0xc0021000u, 0xc01e01b3u, 0x6000, 0x7000, 0xc0011000u, 0xc01e01b6u, 0x8000, 0xc0011000u, 0xc01e01b8u, 0x9000, + 0xc0011000u, 0xc01e0203u, 0xa000, 0xc0011000u, 0xc01e008fu, 0xb000, 0xc00a1000u, 0}); + Require(sceGnmUpdatePsShader(Fresh(), 0x28, nullptr) == 0); + RequireWords({0xc0027600u, 8, 0, 0, 0xc0011000u, 0xc01e0203u, 0, 0xc01f1000u, 0}); + RequireRefused(sceGnmUpdatePsShader(Fresh(), 0x27, Regs)); + RequireRefused(sceGnmUpdatePsShader(Fresh(), 0x28, BadRegs)); + + Require(sceGnmUpdatePsShader350(Fresh(), 0x28, Regs) == 0); + Require(g_words[26] == 0xc01e008fu && g_words[28] == 0xc00a1000u); + Require(sceGnmUpdatePsShader350(Fresh(), 0x28, nullptr) == 0); + RequireWords({0xc0027600u, 8, 0, 0, 0xc0011000u, 0xc01e0203u, 0, 0xc0016900u, 0x8f, 0xf, 0xc01c1000u, 0}); + RequireRefused(sceGnmUpdatePsShader350(Fresh(), 0x27, nullptr)); + RequireRefused(sceGnmUpdatePsShader350(Fresh(), 0x28, BadRegs)); + + Require(sceGnmSetPsShader350(Fresh(), 0x28, Regs) == 0); + RequireWords({0xc0027600u, 8, 0x1000, 0, 0xc0027600u, 10, 0x2000, 0x3000, 0xc0026900u, 0x1c4, 0x4000, 0x5000, + 0xc0026900u, 0x1b3, 0x6000, 0x7000, 0xc0016900u, 0x1b6, 0x8000, 0xc0016900u, 0x1b8, 0x9000, + 0xc0016900u, 0x203, 0xa000, 0xc0016900u, 0x8f, 0xb000, 0xc00a1000u, 0}); + Require(sceGnmSetPsShader350(Fresh(), 0x28, nullptr) == 0); + RequireWords({0xc0027600u, 8, 0, 0, 0xc0016900u, 0x203, 0, 0xc0016900u, 0x8f, 0xf, 0xc01c1000u, 0}); + RequireRefused(sceGnmSetPsShader350(Fresh(), 0x27, Regs)); + RequireRefused(sceGnmSetPsShader350(Fresh(), 0x28, BadRegs)); +} + +void CheckGsHsCs() { + Require(sceGnmUpdateGsShader(Fresh(), 0x1d, Regs) == 0); + RequireWords({0xc0027600u, 0x88, 0x1000, 0, 0xc0027600u, 0x8a, 0x2000, 0x3000, 0xc0011000u, 0xc01e02e5u, 0x4000, + 0xc0011000u, 0xc01e029bu, 0x5000, 0xc0011000u, 0xc01e02e4u, 0x6000, 0xc00a1000u, 0}); + RequireRefused(sceGnmUpdateGsShader(Fresh(), 0x1c, Regs)); + RequireRefused(sceGnmUpdateGsShader(Fresh(), 0x1d, nullptr)); + RequireRefused(sceGnmUpdateGsShader(Fresh(), 0x1d, BadRegs)); + + Require(sceGnmUpdateHsShader(Fresh(), 0x1d, Regs, 0x77) == 0); + RequireWords({0xc0027600u, 0x108, 0x1000, 0, 0xc0027600u, 0x10a, 0x2000, 0x3000, 0xc0021000u, 0xc01e0286u, 0x5000, 0x6000, + 0xc0011000u, 0xc01e02dbu, 0x4000, 0xc0011000u, 0xc01e02d6u, 0x77, 0xc00a1000u, 0}); + RequireRefused(sceGnmUpdateHsShader(Fresh(), 0x1c, Regs, 0)); + RequireRefused(sceGnmUpdateHsShader(Fresh(), 0x1d, BadRegs, 0)); + + Require(sceGnmSetCsShaderWithModifier(Fresh(), 0x19, Regs, 0x3c0) == 0); + RequireWords({0xc0027600u, 0x20c, 0x1000, 0, 0xc0027600u, 0x212, 0x23c0, 0x3000, 0xc0037600u, 0x207, 0x4000, 0x5000, 0x6000, + 0xc00a1000u, 0}); + Require(sceGnmSetCsShaderWithModifier(Fresh(), 0x19, MaskedRegs, 0x40) == 0); + Require(g_words[6] == 0x03000040u); + Require(sceGnmSetCsShaderWithModifier(Fresh(), 0x19, MaskedRegs, 0) == 0); + Require(g_words[6] == 0x030003c0u); + RequireRefused(sceGnmSetCsShaderWithModifier(Fresh(), 0x19, Regs, 0x400)); + RequireRefused(sceGnmSetCsShaderWithModifier(Fresh(), 0x18, Regs, 0)); + RequireRefused(sceGnmSetCsShaderWithModifier(Fresh(), 0x19, BadRegs, 0)); +} + +void CheckWorkloads() { + std::uint32_t stream = Untouched; + Require(sceGnmCreateWorkloadStream(0, &stream) == 3 && stream == Untouched); + Require(sceGnmCreateWorkloadStream(5, nullptr) == 3); + Require(sceGnmCreateWorkloadStream(5, &stream) == 0 && stream == 1); + + std::uint64_t workload = Untouched; + Require(sceGnmBeginWorkload(3, &workload) == 0 && workload == 1); + Require(sceGnmBeginWorkload(0x10, &workload) == 1 && workload == 0); + Require(sceGnmBeginWorkload(0, nullptr) == 3); + + Require(sceGnmEndWorkload(0) == 2); + Require(sceGnmEndWorkload(1) == 0); + Require(sceGnmEndWorkload(0x0full << 56) == 0); + Require(sceGnmEndWorkload(0x10ull << 56) == 2); +} + +} // namespace + +int main() { + CheckVs(); + CheckPs(); + CheckGsHsCs(); + CheckWorkloads(); + Require(sceGnmGetGpuCoreClockFrequency() == 911000000); + Require(sceGnmGetOffChipTessellationBufferSize() == 0x800000); +} diff --git a/core/libs/tests/GuestHeapReportForGame.cpp b/core/libs/tests/GuestHeapReportForGame.cpp index de3ce98250..6986740948 100644 --- a/core/libs/tests/GuestHeapReportForGame.cpp +++ b/core/libs/tests/GuestHeapReportForGame.cpp @@ -2,6 +2,13 @@ #include #include #include +#include +#include +#ifdef _WIN32 +#include +#else +#include +#endif #include #include #include @@ -12,6 +19,13 @@ void APS5_VABI sceLibcInternalBacktraceForGame_nid_postfix(const char* heapName) void APS5_VABI sceLibcInternalHeapErrorReportForGame_nid_postfix(void* heap, void* block, std::uint32_t error); } +static std::string ReadAll(const std::filesystem::path& path) { + std::ifstream file(path); + std::stringstream text; + text << file.rdbuf(); + return text.str(); +} + static void Require(bool value, const char* message) { if (value) return; std::fprintf(stdout, "%s\n", message); @@ -20,29 +34,58 @@ static void Require(bool value, const char* message) { int main() { const auto path = std::filesystem::temp_directory_path() / ("anyps5-heap-report-" + std::to_string(std::rand()) + ".txt"); + const auto outputPath = std::filesystem::temp_directory_path() / ("anyps5-heap-report-out-" + std::to_string(std::rand()) + ".txt"); #ifdef _WIN32 Require(_wfreopen(path.wstring().c_str(), L"w", stderr) != nullptr, "stderr redirection failed"); + Require(_wfreopen(outputPath.wstring().c_str(), L"w", stdout) != nullptr, "stdout redirection failed"); #else Require(std::freopen(path.c_str(), "w", stderr) != nullptr, "stderr redirection failed"); + Require(std::freopen(outputPath.c_str(), "w", stdout) != nullptr, "stdout redirection failed"); #endif char heapName[] = "SceLibcHeap"; sceLibcInternalBacktraceForGame_nid_postfix(heapName); sceLibcInternalBacktraceForGame_nid_postfix(nullptr); - int heap = 0; + alignas(16) char heap[0x400] = {}; + std::strcpy(heap + 0x3B0, "SceLibcHeap"); + const std::uint32_t number = 3; + std::memcpy(heap + 0x3F4, &number, sizeof(number)); int block = 0; - sceLibcInternalHeapErrorReportForGame_nid_postfix(&heap, &block, 0x1234u); + sceLibcInternalHeapErrorReportForGame_nid_postfix(heap, &block, 1); + std::memset(heap + 0x3F4, 0, sizeof(number)); + sceLibcInternalHeapErrorReportForGame_nid_postfix(heap, &block, 2); + bool threw = false; + try { + sceLibcInternalHeapErrorReportForGame_nid_postfix(nullptr, &block, 1); + } catch (const std::invalid_argument&) { + threw = true; + } + Require(threw, "null heap accepted"); std::fflush(stderr); std::fclose(stderr); + std::fflush(stdout); + std::fclose(stdout); - std::ifstream file(path); - std::stringstream text; - text << file.rdbuf(); - file.close(); + const std::string report = ReadAll(path); + const std::string output = ReadAll(outputPath); std::filesystem::remove(path); - const std::string report = text.str(); + std::filesystem::remove(outputPath); + const std::string first = "[SceLibc] A heap error is detected.\n[SceLibc] -- Mspace Name --\n[SceLibc] SceLibcHeap ( 3)\n" + "[SceLibc] -- Error reason --\n[SceLibc] segment out of range.\n[SceLibc] pid:<0x"; + const std::string second = "[SceLibc] A heap error is detected.\n[SceLibc] -- Mspace Name --\n[SceLibc] SceLibcHeap\n[SceLibc] pid:<0x"; + Require(output.rfind(first, 0) == 0, "segment error report differs"); + Require(output.find(second, first.size()) != std::string::npos, "unnamed reason report differs"); + Require(output.find("reason", output.find(second)) == std::string::npos, "reason printed for error 2"); Require(report.find("[SceLibcHeap] backtrace requested from guest 0x") != std::string::npos, "heap name not reported"); Require(report.find("[] backtrace requested from guest 0x") != std::string::npos, "null heap name not reported"); - Require(report.find("[libc] heap error 0x1234: heap ") != std::string::npos, "heap error not reported"); + Require(report.find("[libc] heap error 0x1: heap ") != std::string::npos, "host diagnostic missing"); +#ifdef _WIN32 + const unsigned long pid = static_cast(_getpid()); +#else + const unsigned long pid = static_cast(getpid()); +#endif + char pidText[32]; + std::snprintf(pidText, sizeof(pidText), "pid:<0x%lx> tid:<0x", pid); + Require(output.find(pidText) != std::string::npos, "pid differs"); Require(std::string(heapName) == "SceLibcHeap", "heap name modified"); } diff --git a/core/libs/tests/GuestHostStandardErrorWindows.cpp b/core/libs/tests/GuestHostStandardErrorWindows.cpp index f91c92d09e..559b90d3f2 100644 --- a/core/libs/tests/GuestHostStandardErrorWindows.cpp +++ b/core/libs/tests/GuestHostStandardErrorWindows.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #ifdef _WIN32 #include @@ -19,6 +20,10 @@ int APS5_VABI printf_nid_postfix(const char*, ...); int APS5_VABI puts_nid_postfix(const char*); int APS5_VABI fprintf_nid_postfix(FileStream*, const char*, ...); int APS5_VABI fflush_nid_postfix(FileStream*); +int APS5_VABI setvbuf_nid_postfix(FileStream*, char*, int, std::size_t); +void APS5_VABI setbuf_nid_postfix(FileStream*, char*); +void APS5_VABI flockfile_nid_postfix(FileStream*); +void APS5_VABI funlockfile_nid_postfix(FileStream*); void APS5_VABI perror_nid_postfix(const char*); int* APS5_VABI __error_nid_postfix(); extern FileStream _Stdout_nid_postfix; @@ -96,7 +101,53 @@ static void GuestText(int standard, const std::string& text) { ::_close(guest[1]); } -int main() { +static void Buffering(int standard) { + int capture[2]{-1, -1}; + Require(::_pipe(capture, 65536, _O_BINARY) == 0); + const int original = ::_dup(standard); + Require(original >= 0 && ::_dup2(capture[1], standard) == 0); + auto* guestStream = standard == 1 ? &_Stdout_nid_postfix : &_Stderr_nid_postfix; + char buffer[4096]{}; + flockfile_nid_postfix(guestStream); + if (standard == 1) setbuf_nid_postfix(guestStream, buffer); + else Require(setvbuf_nid_postfix(guestStream, buffer, 0, sizeof(buffer)) == 0); + funlockfile_nid_postfix(guestStream); + Require(fprintf_nid_postfix(guestStream, "guest-buffered") == 14); + Require(std::fputs("host-immediate", standard == 1 ? stdout : stderr) >= 0); + char bytes[64]{}; + DWORD read = 0; + Require(Available(capture[0], bytes, sizeof(bytes), read) == 14); + Require(read == 14 && std::string(bytes, read) == "host-immediate"); + Require(::_read(capture[0], bytes, 14) == 14); + Require(Available(capture[0], bytes, sizeof(bytes), read) == 0); + Require(fflush_nid_postfix(guestStream) == 0); + Require(Available(capture[0], bytes, sizeof(bytes), read) == 14); + Require(read == 14 && std::string(bytes, read) == "guest-buffered"); + Require(::_read(capture[0], bytes, 14) == 14); + + int guest[2]{-1, -1}; + Require(pipe_nid_postfix(guest) == 0 && dup2_nid_postfix(guest[1], standard) == standard); + Require(fprintf_nid_postfix(guestStream, "guest-redirected") == 16 && fflush_nid_postfix(guestStream) == 0); + Require(std::fputs("host-original", standard == 1 ? stdout : stderr) >= 0); + Require(Available(capture[0], bytes, sizeof(bytes), read) == 13); + Require(read == 13 && std::string(bytes, read) == "host-original"); + Require(Available(guest[0], bytes, sizeof(bytes), read) == 16); + Require(read == 16 && std::string(bytes, read) == "guest-redirected"); + Require(setvbuf_nid_postfix(guestStream, nullptr, 2, 0) == 0); + Require(::_dup2(original, standard) == 0); + ::_close(original); + ::_close(capture[0]); + ::_close(capture[1]); + ::_close(guest[0]); + ::_close(guest[1]); +} + +int main(int argc, char** argv) { + if (argc == 2 && std::strcmp(argv[1], "--buffering") == 0) { + Buffering(2); + Buffering(1); + return 0; + } Split(2); Split(1); GuestText(1, "guest-printf\nguest-puts\n"); diff --git a/core/libs/tests/GuestHttp.cpp b/core/libs/tests/GuestHttp.cpp index 9ecb47dd94..49134461d3 100644 --- a/core/libs/tests/GuestHttp.cpp +++ b/core/libs/tests/GuestHttp.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include @@ -31,6 +32,7 @@ int APS5_VABI sceHttpInit_nid_postfix(int, int, std::uint64_t); int APS5_VABI sceHttpTerm_nid_postfix(int); int APS5_VABI sceHttpAuthCacheFlush(int); int APS5_VABI sceHttpCookieFlush(int); +int APS5_VABI sceHttpGetMemoryPoolStats(int, void*); int APS5_VABI sceHttpReadData(int, void*, std::size_t); int APS5_VABI sceHttpCreateRequest2(int, const char*, const char*, std::uint64_t); int APS5_VABI sceHttpsEnableOption(int, std::uint32_t); @@ -54,6 +56,41 @@ static void Require(bool value) { if (!value) std::abort(); } static bool Equal(const char* left, const char* right) { return std::strcmp(left, right) == 0; } +struct HttpPoolStats { + std::size_t poolSize; + std::size_t maxInuseSize; + std::size_t currentInuseSize; +}; +static_assert(sizeof(HttpPoolStats) == 24); + +template +static bool Throws(TCall call) { + try { + call(); + } catch (const std::exception&) { + return true; + } + return false; +} + +static void CheckPoolStatsRejected(int id, int expected) { + unsigned char buffer[sizeof(HttpPoolStats) + 16]; + std::memset(buffer, 0x5a, sizeof(buffer)); + Require(sceHttpGetMemoryPoolStats(id, buffer) == expected); + for (unsigned char byte : buffer) Require(byte == 0x5a); +} + +static void CheckPoolStats(int id, std::size_t poolSize) { + unsigned char buffer[sizeof(HttpPoolStats) + 16]; + std::memset(buffer, 0x5a, sizeof(buffer)); + Require(sceHttpGetMemoryPoolStats(id, buffer) == 0); + HttpPoolStats stats; + std::memcpy(&stats, buffer, sizeof(stats)); + Require(stats.poolSize == poolSize); + Require(stats.maxInuseSize == 0 && stats.currentInuseSize == 0); + for (std::size_t i = sizeof(stats); i < sizeof(buffer); ++i) Require(buffer[i] == 0x5a); +} + static void CheckUriParseCharacters() { char pool[256]; SceHttpUriElement element{}; @@ -679,6 +716,10 @@ int main() { flushCaches(0, beforeInit); flushCaches(-1, beforeInit); flushCaches(request, beforeInit); + CheckPoolStatsRejected(0, beforeInit); + CheckPoolStatsRejected(-1, beforeInit); + CheckPoolStatsRejected(request, beforeInit); + Require(Throws([] { sceHttpGetMemoryPoolStats(0, nullptr); })); HttpEpollHandle epoll = nullptr; HttpNBEvent events[2]{}; Require(sceHttpCreateEpoll(1, &epoll) == beforeInit); @@ -692,6 +733,14 @@ int main() { flushCaches(-1, invalidId); flushCaches(INT32_MAX, invalidId); flushCaches(request, invalidId); + CheckPoolStats(context, 0x10000); + CheckPoolStats(context, 0x10000); + CheckPoolStatsRejected(0, invalidId); + CheckPoolStatsRejected(-1, invalidId); + CheckPoolStatsRejected(INT32_MAX, invalidId); + CheckPoolStatsRejected(request, invalidId); + Require(Throws([context] { sceHttpGetMemoryPoolStats(context, nullptr); })); + Require(Throws([] { sceHttpGetMemoryPoolStats(INT32_MAX, nullptr); })); Require(sceHttpCreateEpoll(-1, &epoll) == invalidId); Require(sceHttpCreateEpoll(context, nullptr) == invalidValue); Require(sceHttpCreateEpoll(context, &epoll) == 0 && epoll != nullptr); @@ -733,11 +782,15 @@ int main() { Require(waitWokenBy([&] { Require(sceHttpDestroyEpoll(context, epoll) == 0); }) == 0); Require(sceHttpCreateEpoll(context, &epoll) == 0); - const int otherContext = sceHttpInit_nid_postfix(0, 0, 0x10000); + const int otherContext = sceHttpInit_nid_postfix(0, 0, 0x23000); Require(otherContext > 0 && otherContext != context); flushCaches(otherContext, 0); + CheckPoolStats(otherContext, 0x23000); + CheckPoolStats(context, 0x10000); Require(sceHttpTerm_nid_postfix(otherContext) == 0); flushCaches(otherContext, invalidId); + CheckPoolStatsRejected(otherContext, invalidId); + CheckPoolStats(context, 0x10000); flushCaches(context, 0); Require(sceHttpTerm_nid_postfix(otherContext) == invalidId); Require(sceHttpWaitRequest(epoll, events, 2, 0) == 0); @@ -745,13 +798,23 @@ int main() { Require(sceHttpWaitRequest(epoll, events, 2, 0) == beforeInit); Require(sceHttpTerm_nid_postfix(context) == beforeInit); flushCaches(context, beforeInit); - const int newContext = sceHttpInit_nid_postfix(0, 0, 0x10000); + CheckPoolStatsRejected(context, beforeInit); + const int newContext = sceHttpInit_nid_postfix(0, 0, 0x123456000ull); Require(newContext > 0 && newContext != context && newContext != otherContext); flushCaches(context, invalidId); flushCaches(otherContext, invalidId); flushCaches(newContext, 0); + CheckPoolStatsRejected(context, invalidId); + CheckPoolStatsRejected(otherContext, invalidId); + CheckPoolStats(newContext, 0x123456000ull); Require(sceHttpTerm_nid_postfix(newContext) == 0); flushCaches(newContext, beforeInit); + CheckPoolStatsRejected(newContext, beforeInit); + const int emptyPoolContext = sceHttpInit_nid_postfix(0, 0, 0); + Require(emptyPoolContext > 0); + CheckPoolStats(emptyPoolContext, 0); + CheckPoolStatsRejected(newContext, invalidId); + Require(sceHttpTerm_nid_postfix(emptyPoolContext) == 0); char data[16]; Require(sceHttpReadData(1, data, sizeof(data)) == network); diff --git a/core/libs/tests/GuestImeDialog.cpp b/core/libs/tests/GuestImeDialog.cpp index 756a809fd7..3087ec13b5 100644 --- a/core/libs/tests/GuestImeDialog.cpp +++ b/core/libs/tests/GuestImeDialog.cpp @@ -13,6 +13,9 @@ int APS5_VABI sceImeDialogGetStatus(void); int APS5_VABI sceImeDialogGetResult(Result*); int APS5_VABI sceImeDialogAbort(void); int APS5_VABI sceImeDialogTerm(void); +int APS5_VABI sceImeDialogGetPanelPositionAndForm(PositionAndForm*); +int APS5_VABI sceImeDialogGetPanelSize(const Param*, std::uint32_t*, std::uint32_t*); +int APS5_VABI sceImeDialogGetPanelSizeExtended(const Param*, const ExtendedParam*, std::uint32_t*, std::uint32_t*); int APS5_VABI sceImeDialogForceClose(void); int APS5_VABI sceImeDialogGetCurrentStarState(std::int64_t); int APS5_VABI sceImeDialogInitInternal(const Param*, const ExtendedParam*); @@ -181,6 +184,13 @@ int main() { Open(param); Close(); + Require(sceImeDialogGetPanelPositionAndForm(nullptr) == ErrorNotOpened); + std::uint32_t panelWidth = 0; + std::uint32_t panelHeight = 0; + Require(sceImeDialogGetPanelSize(nullptr, &panelWidth, &panelHeight) == 0 && panelWidth == 1920 && panelHeight == 1080); + panelWidth = 0; + panelHeight = 0; + Require(sceImeDialogGetPanelSizeExtended(nullptr, nullptr, &panelWidth, &panelHeight) == 0 && panelWidth == 1920 && panelHeight == 1080); Require(sceImeDialogInit(nullptr, nullptr) == ErrorInvalidAddress); const Param empty = DialogParam(buffer, 0); Require(sceImeDialogInit(&empty, nullptr) == ErrorInvalidMaxTextLength); diff --git a/core/libs/tests/GuestInitOnce.cpp b/core/libs/tests/GuestInitOnce.cpp new file mode 100644 index 0000000000..0a33705835 --- /dev/null +++ b/core/libs/tests/GuestInitOnce.cpp @@ -0,0 +1,53 @@ +#include "prx/libc/include/general/VabiMacros.hpp" +#include +#include +#include +#include +#include + +extern "C" { +int APS5_VABI __std_init_once_begin_initialize_clr_nid_postfix(void** once, unsigned long flags, int* pending, void** context); +int APS5_VABI __std_init_once_complete_clr_nid_postfix(void** once, unsigned long flags, void* context); +} + +namespace { + +constexpr unsigned long InitFailed = 4; + +void Require(bool condition, const char* message) { + if (condition) return; + std::fprintf(stderr, "init once: %s\n", message); + std::abort(); +} + +int Begin(void** once) { + int pending = -1; + Require(__std_init_once_begin_initialize_clr_nid_postfix(once, 0, &pending, nullptr) == 1, "begin returns 1"); + return pending; +} + +} + +int main() { + alignas(8) void* once = nullptr; + Require(Begin(&once) == 1, "the first caller runs the initialization"); + Require(reinterpret_cast(once) == 1, "a running initialization marks the object"); + Require(__std_init_once_complete_clr_nid_postfix(&once, InitFailed, nullptr) == 1, "complete a failed initialization"); + Require(once == nullptr, "a failed initialization resets the object"); + Require(Begin(&once) == 1, "a caller after a failure runs the initialization again"); + + std::atomic waiterDone{false}; + std::atomic waiterPending{-1}; + std::thread waiter([&] { + waiterPending = Begin(&once); + waiterDone = true; + }); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + Require(!waiterDone, "a second caller waits while the initialization runs"); + Require(__std_init_once_complete_clr_nid_postfix(&once, 0, nullptr) == 1, "complete the initialization"); + waiter.join(); + Require(waiterPending == 0, "the waiting caller sees the initialization done"); + Require(reinterpret_cast(once) == 2, "a completed initialization marks the object"); + Require(Begin(&once) == 0, "a later caller does not run the initialization"); + return 0; +} diff --git a/core/libs/tests/GuestJson.cpp b/core/libs/tests/GuestJson.cpp index 9a577f0dd3..89f76dbf29 100644 --- a/core/libs/tests/GuestJson.cpp +++ b/core/libs/tests/GuestJson.cpp @@ -95,6 +95,13 @@ String* APS5_VABI _ZN3sce4Json6StringpLEh(String*, unsigned char); String* APS5_VABI _ZNK3sce4Json6String6substrEmm(String*, const String*, std::size_t, std::size_t); std::size_t APS5_VABI _ZNK3sce4Json6String4findEcm(const String*, char, std::size_t); std::size_t APS5_VABI _ZNK3sce4Json6String5rfindEcm(const String*, char, std::size_t); +char APS5_VABI _ZNK3sce4Json6String2atEm(const String*, std::size_t); +std::size_t APS5_VABI _ZNK3sce4Json6String4findEPKcm(const String*, const char*, std::size_t); +std::size_t APS5_VABI _ZNK3sce4Json6String4findEPKcmm(const String*, const char*, std::size_t, std::size_t); +std::size_t APS5_VABI _ZNK3sce4Json6String4findERKS1_m(const String*, const String*, std::size_t); +std::size_t APS5_VABI _ZNK3sce4Json6String5rfindEPKcm(const String*, const char*, std::size_t); +std::size_t APS5_VABI _ZNK3sce4Json6String5rfindEPKcmm(const String*, const char*, std::size_t, std::size_t); +std::size_t APS5_VABI _ZNK3sce4Json6String5rfindERKS1_m(const String*, const String*, std::size_t); bool APS5_VABI _ZNK3sce4Json6Object8iteratorneERKS2_(const Iterator*, const Iterator*); bool APS5_VABI _ZNK3sce4Json6Object8iteratoreqERKS2_(const Iterator*, const Iterator*); void APS5_VABI _ZN3sce4Json5ValueC1Ev(Value*); @@ -719,6 +726,42 @@ static void StringMethods() { Require(_ZNK3sce4Json6String4findEcm(&base, 'z', 0) == _ZN3sce4Json6String4nposE); Require(_ZNK3sce4Json6String5rfindEcm(&base, '/', _ZN3sce4Json6String4nposE) == 3); Require(_ZNK3sce4Json6String5rfindEcm(&base, '/', 2) == 1); + const std::size_t npos = _ZN3sce4Json6String4nposE; + Require(_ZNK3sce4Json6String2atEm(&base, 0) == 'a'); + Require(_ZNK3sce4Json6String2atEm(&base, 4) == 'c'); + bool atRefused = false; + try { + _ZNK3sce4Json6String2atEm(&base, 5); + } catch (const std::out_of_range&) { + atRefused = true; + } + Require(atRefused); + Require(_ZNK3sce4Json6String4findEPKcm(&base, "/b", 0) == 1); + Require(_ZNK3sce4Json6String4findEPKcm(&base, "/", 2) == 3); + Require(_ZNK3sce4Json6String4findEPKcm(&base, "", 5) == 5); + Require(_ZNK3sce4Json6String4findEPKcm(&base, "", 6) == npos); + Require(_ZNK3sce4Json6String4findEPKcm(&base, "b/d", 0) == npos); + Require(_ZNK3sce4Json6String4findEPKcmm(&base, "b/d", 0, 2) == 2); + Require(_ZNK3sce4Json6String4findEPKcmm(&base, nullptr, 3, 0) == 3); + Require(_ZNK3sce4Json6String5rfindEPKcm(&base, "/", npos) == 3); + Require(_ZNK3sce4Json6String5rfindEPKcm(&base, "/", 2) == 1); + Require(_ZNK3sce4Json6String5rfindEPKcm(&base, "", npos) == 5); + Require(_ZNK3sce4Json6String5rfindEPKcmm(&base, "a/x", npos, 2) == 0); + Require(_ZNK3sce4Json6String5rfindEPKcmm(&base, "zz", npos, 2) == npos); + String needle{}; + _ZN3sce4Json6StringC1EPKc(&needle, "/c"); + Require(_ZNK3sce4Json6String4findERKS1_m(&base, &needle, 0) == 3); + Require(_ZNK3sce4Json6String4findERKS1_m(&base, &needle, 4) == npos); + Require(_ZNK3sce4Json6String5rfindERKS1_m(&base, &needle, npos) == 3); + Require(_ZNK3sce4Json6String5rfindERKS1_m(&base, &needle, 2) == npos); + _ZN3sce4Json6StringD1Ev(&needle); + bool findRefused = false; + try { + _ZNK3sce4Json6String4findEPKcm(&base, nullptr, 0); + } catch (const std::invalid_argument&) { + findRefused = true; + } + Require(findRefused); String part{}; Require(_ZNK3sce4Json6String6substrEmm(&part, &base, 2, 3) == &part); Require(text(part) == "b/c"); diff --git a/core/libs/tests/GuestKernelAppArchive.cpp b/core/libs/tests/GuestKernelAppArchive.cpp index 2eb8aae01f..3fcb0f4094 100644 --- a/core/libs/tests/GuestKernelAppArchive.cpp +++ b/core/libs/tests/GuestKernelAppArchive.cpp @@ -146,7 +146,7 @@ void Reads() { KernelAioRwRequest request{65530, 20, buffer.data(), &result, fd}; std::int32_t id = 0; std::int32_t state = 0; - Check(sceKernelAioSubmitReadCommands(&request, 1, 0, &id) == 0); + Check(sceKernelAioSubmitReadCommands(&request, 1, 1, &id) == 0); Check(sceKernelAioWaitRequest(id, &state, nullptr) == 0); Check(result.return_value == 20 && std::equal(buffer.begin(), buffer.begin() + 20, expected.begin() + 65530)); std::int32_t removed = 0; diff --git a/core/libs/tests/GuestKernelErrors.cpp b/core/libs/tests/GuestKernelErrors.cpp index 8989624672..66b6210e17 100644 --- a/core/libs/tests/GuestKernelErrors.cpp +++ b/core/libs/tests/GuestKernelErrors.cpp @@ -95,9 +95,9 @@ int main() { Require(sceKernelCreateSema(&semaphore, "limit", 1, 1, 3, nullptr) == SCE_OK); Require(sceKernelSignalSema(semaphore, 3) == SCE_KERNEL_ERROR_EINVAL); Require(sceKernelSignalSema(semaphore, 2) == SCE_OK); - Require(sceKernelSignalSema(semaphore, 0) == SCE_OK); + Require(sceKernelSignalSema(semaphore, 0) == SCE_KERNEL_ERROR_EINVAL); Require(sceKernelPollSema(semaphore, 3) == SCE_OK); - Require(sceKernelSignalSema(semaphore, 0) == SCE_OK); + Require(sceKernelSignalSema(semaphore, 0) == SCE_KERNEL_ERROR_EINVAL); Require(sceKernelPollSema(semaphore, 1) == SCE_KERNEL_ERROR_EBUSY); Require(sceKernelDeleteSema(semaphore) == SCE_OK); diff --git a/core/libs/tests/GuestKernelFcntl.cpp b/core/libs/tests/GuestKernelFcntl.cpp index 3220eaa25c..164a289714 100644 --- a/core/libs/tests/GuestKernelFcntl.cpp +++ b/core/libs/tests/GuestKernelFcntl.cpp @@ -199,6 +199,7 @@ static void SocketsKeepTheirPath() { } static void FileFlagsRoundTrip() { + std::remove("kernel_fcntl_probe.bin"); const int file = sceKernelOpen("kernel_fcntl_probe.bin", CreateReadWrite, 0644); Require(file >= 0); Require(sceKernelFcntl(file, GetFlags) == ReadWrite); @@ -275,12 +276,26 @@ static void FileFlagsRoundTrip() { Require(fcntl_nid_postfix(appending, GetFlags) == (WriteOnly | Append)); Require(fcntl_nid_postfix(appending, SetFlags, Append | NonBlocking) == 0); Require(fcntl_nid_postfix(appending, GetFlags) == (WriteOnly | Append | NonBlocking)); -#ifdef _WIN32 - Require(ThrowsWith([appending] { fcntl_nid_postfix(appending, SetFlags, 0); }, "fcntl: changing O_APPEND on a file is not implemented")); -#else + Require(write_nid_postfix(appending, "ab", 2) == 2); Require(fcntl_nid_postfix(appending, SetFlags, 0) == 0); Require(fcntl_nid_postfix(appending, GetFlags) == WriteOnly); -#endif + Require(sceKernelLseek(appending, 0, FromStart) == 0); + Require(write_nid_postfix(appending, "X", 1) == 1); + const int sharing = fcntl_nid_postfix(appending, DuplicateCommand, 0); + Require(sharing >= 0); + Require(fcntl_nid_postfix(sharing, SetFlags, Append) == 0); + Require(fcntl_nid_postfix(appending, GetFlags) == (WriteOnly | Append)); + Require(sceKernelLseek(appending, 0, FromStart) == 0); + Require(write_nid_postfix(appending, "c", 1) == 1); + Require(fcntl_nid_postfix(sharing, SetFlags, 0) == 0); + Require(sceKernelLseek(appending, 0, FromStart) == 0); + Require(write_nid_postfix(appending, "Y", 1) == 1); + Require(close_nid_postfix(sharing) == 0); + const int reading = sceKernelOpen("kernel_fcntl_probe.bin", ReadOnly, 0); + Require(reading >= 0); + char written[16]{}; + Require(sceKernelRead(reading, written, sizeof(written)) == 8 && std::memcmp(written, "Ybcdeabc", 8) == 0); + Require(sceKernelClose(reading) == 0); Require(ThrowsWith([appending] { fcntl_nid_postfix(appending, SetFlags, Append | 0x80); }, "fcntl: changing O_FSYNC on a file is not implemented")); Require(sceKernelClose(appending) == 0); std::remove("kernel_fcntl_probe.bin"); diff --git a/core/libs/tests/GuestKernelSys.cpp b/core/libs/tests/GuestKernelSys.cpp index 43657d062c..1650d050d4 100644 --- a/core/libs/tests/GuestKernelSys.cpp +++ b/core/libs/tests/GuestKernelSys.cpp @@ -62,6 +62,7 @@ int APS5_VABI setsid_nid_postfix(void); int APS5_VABI link_nid_postfix(const char*, const char*); int APS5_VABI symlink_nid_postfix(const char*, const char*); int APS5_VABI openat_nid_postfix(int, const char*, int, ...); +int APS5_VABI _openat_nid_postfix(int, const char*, int, ...); int APS5_VABI unlinkat_nid_postfix(int, const char*, int); std::int64_t APS5_VABI readlinkat_nid_postfix(int, const char*, char*, std::size_t); int APS5_VABI mkdirat_nid_postfix(int, const char*, std::uint16_t); @@ -791,6 +792,22 @@ int main() { const int plain = sceKernelOpen(file.c_str(), SCE_KERNEL_O_RDONLY, 0); Require(plain >= 0); Require(openat_nid_postfix(plain, "file.txt", SCE_KERNEL_O_RDONLY) == -1 && Error() == 20); + const int aliased = _openat_nid_postfix(base, "file.txt", SCE_KERNEL_O_RDONLY); + byte = 0; + Require(aliased >= 0 && sceKernelRead(aliased, &byte, 1) == 1 && byte == 'x'); + Require(sceKernelClose(aliased) == 0); + const int aliasMade = _openat_nid_postfix(base, "alias.bin", SCE_KERNEL_O_WRONLY | SCE_KERNEL_O_CREAT | SCE_KERNEL_O_EXCL, 0600); + Require(aliasMade >= 0 && sceKernelClose(aliasMade) == 0 && std::filesystem::is_regular_file(root / "alias.bin")); +#ifndef _WIN32 + Require((std::filesystem::status(root / "alias.bin").permissions() & std::filesystem::perms::all) == + (std::filesystem::perms::owner_read | std::filesystem::perms::owner_write)); +#endif + const int aliasWritten = _openat_nid_postfix(base, "alias.bin", SCE_KERNEL_O_WRONLY); + Require(aliasWritten >= 0 && sceKernelClose(aliasWritten) == 0); + Require(_openat_nid_postfix(base, "alias.bin", SCE_KERNEL_O_WRONLY | SCE_KERNEL_O_CREAT | SCE_KERNEL_O_EXCL, 0600) == -1 && Error() == 17); + Require(_openat_nid_postfix(base, "file.txt", SCE_KERNEL_O_ACCMODE) == -1 && Error() == 22); + Require(_openat_nid_postfix(-1, "file.txt", SCE_KERNEL_O_RDONLY) == -1 && Error() == 9); + Require(_openat_nid_postfix(plain, "file.txt", SCE_KERNEL_O_RDONLY) == -1 && Error() == 20); Require(readlinkat_nid_postfix(base, "file.txt", buffer, sizeof(buffer)) == -1 && Error() == 22); Require(readlinkat_nid_postfix(base, "missing.bin", buffer, sizeof(buffer)) == -1 && Error() == 2); Require(readlinkat_nid_postfix(base, "file.txt/child", buffer, sizeof(buffer)) == -1 && Error() == 20); @@ -817,6 +834,19 @@ int main() { const std::string longName(256, 'a'); Require(mkdirat_nid_postfix(base, longName.c_str(), 0700) == -1 && Error() == 63); Require(mkdirat_nid_postfix(base, "dir/../dotdot_dir", 0700) == 0 && std::filesystem::is_directory(root / "dotdot_dir")); +#ifdef _WIN32 + for (const DWORD linkFlags : {DWORD(0), DWORD(SYMBOLIC_LINK_FLAG_DIRECTORY)}) { + const auto loopLink = root / "mkdirat_loop"; + if (CreateSymbolicLinkW(loopLink.c_str(), L"mkdirat_loop", linkFlags | SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE) == 0) { + Require(GetLastError() == ERROR_PRIVILEGE_NOT_HELD); + std::fprintf(stderr, "symbolic links unavailable: the mkdirat loop case not tested\n"); + break; + } + Require(mkdirat_nid_postfix(base, "mkdirat_loop/child", 0700) == -1 && Error() == 62); + Require(mkdirat_nid_postfix(base, "mkdirat_loop", 0700) == -1 && Error() == 17); + Require((linkFlags != 0 ? RemoveDirectoryW(loopLink.c_str()) : DeleteFileW(loopLink.c_str())) != 0); + } +#endif Require(unlinkat_nid_postfix(plain, "file.txt", 0) == -1 && Error() == 20); Require(unlinkat_nid_postfix(-1, "file.txt", 0) == -1 && Error() == 9); Require(sceKernelClose(plain) == 0); diff --git a/core/libs/tests/GuestKernelSysModuleInternal.cpp b/core/libs/tests/GuestKernelSysModuleInternal.cpp index 0d477ebd9f..0785445b57 100644 --- a/core/libs/tests/GuestKernelSysModuleInternal.cpp +++ b/core/libs/tests/GuestKernelSysModuleInternal.cpp @@ -57,11 +57,12 @@ static void CheckEachModule() { static void CheckMatchesAddressLookup() { ModuleInfoEx byAddress{}; byAddress.st_size = sizeof(ModuleInfoEx); - Require(sceKernelGetModuleInfoFromAddr(reinterpret_cast(&sceKernelGetModuleInfo), 2, &byAddress) == 0); + Require(sceKernelGetModuleInfoFromAddr(reinterpret_cast(&sceKernelGetModuleInfo), 1, &byAddress) == 0); ModuleInfoEx byHandle{}; byHandle.st_size = sizeof(ModuleInfoEx); Require(sceKernelGetModuleInfoInternal(byAddress.id, &byHandle) == 0); - Require(std::memcmp(&byHandle, &byAddress, sizeof(ModuleInfoEx)) == 0); + Require(byAddress.st_size == 0 && byHandle.st_size == sizeof(ModuleInfoEx)); + Require(std::memcmp(byHandle.name, byAddress.name, sizeof(ModuleInfoEx) - offsetof(ModuleInfoEx, name)) == 0); } static void CheckRefusals() { diff --git a/core/libs/tests/GuestKqueue.cpp b/core/libs/tests/GuestKqueue.cpp index fd0acdad4e..3289a4276a 100644 --- a/core/libs/tests/GuestKqueue.cpp +++ b/core/libs/tests/GuestKqueue.cpp @@ -39,6 +39,7 @@ int APS5_VABI rmdir_nid_postfix(const char*); std::int64_t APS5_VABI write_nid_postfix(int, const char*, std::int64_t); int APS5_VABI ftruncate_nid_postfix(int, std::int64_t); int APS5_VABI unlink_nid_postfix(const char*); +int APS5_VABI getpid_nid_postfix(void); int APS5_VABI sceKernelRename(const char*, const char*); int APS5_VABI sceKernelCreateEqueue(KernelEqueue* eq, const char* name); int APS5_VABI sceKernelDeleteEqueue(KernelEqueue eq); @@ -48,6 +49,7 @@ int APS5_VABI sceKernelDeleteFileEvent(KernelEqueue eq, int fd); int* APS5_VABI __error_nid_postfix(); } +static constexpr int GuestEperm = 1; static constexpr int GuestEnoent = 2; static constexpr int GuestEbadf = 9; static constexpr int GuestEinval = 22; @@ -62,9 +64,13 @@ static constexpr int FirstSocket = 0x10000000; static constexpr std::int16_t EvfiltRead = -1; static constexpr std::int16_t EvfiltWrite = -2; static constexpr std::int16_t EvfiltVnode = -4; +static constexpr std::int16_t EvfiltAio = -3; static constexpr std::int16_t EvfiltProc = -5; static constexpr std::int16_t EvfiltTimer = -7; +static constexpr std::int16_t EvfiltProcdesc = -8; +static constexpr std::int16_t EvfiltLio = -10; static constexpr std::int16_t EvfiltUser = -11; +static constexpr std::int16_t EvfiltSendfile = -12; static constexpr std::uint16_t EvAdd = 0x0001; static constexpr std::uint16_t EvDelete = 0x0002; static constexpr std::uint16_t EvEnable = 0x0004; @@ -192,7 +198,94 @@ static void Timers() { RequireFailure(Apply(kq, Change(3, EvfiltTimer, EvAdd, 0x10, 5)), GuestEinval); RequireFailure(Apply(kq, Change(3, EvfiltTimer, EvAdd, NoteUseconds | 0x1, 5)), GuestEinval); Require(Apply(kq, Change(4, EvfiltTimer, EvAdd, 0, 100000)) == 0); - Require(Throws([kq] { Apply(kq, Change(4, EvfiltTimer, EvAdd, 0, 5)); })); + Require(Apply(kq, Change(4, EvfiltTimer, EvAdd, 0, 5)) == 0); + const KernelTimespec fiftyMillis{0, 50000000}; + Require(kevent_nid_postfix(kq, nullptr, 0, events, 4, &fiftyMillis) == 0); + Require(close_nid_postfix(kq) == 0); +} + +static void TimerReadd() { + const int kq = kqueue_nid_postfix(); + int first = 0; + int second = 0; + KernelEvent events[4]{}; + Require(Apply(kq, Change(6, EvfiltTimer, EvAdd, 0, 10, &first)) == 0); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + Require(Apply(kq, Change(6, EvfiltTimer, EvAdd, 0, 1000, &second)) == 0); + Require(Poll(kq, events, 4) == 1 && events[0].ident == 6 && events[0].data >= 6 && events[0].udata == &second); + Require(Apply(kq, Change(6, EvfiltTimer, EvDelete)) == 0); + + const auto armed = std::chrono::steady_clock::now(); + Require(Apply(kq, Change(7, EvfiltTimer, EvAdd, 0, 50)) == 0); + Require(Apply(kq, Change(7, EvfiltTimer, EvAdd, 0, 10)) == 0); + Require(Wait(kq, events, 4) == 1 && events[0].ident == 7); + Require(std::chrono::steady_clock::now() - armed >= std::chrono::milliseconds(49)); + std::this_thread::sleep_for(std::chrono::milliseconds(120)); + Require(Poll(kq, events, 4) == 1 && events[0].ident == 7 && events[0].data >= 6); + Require(Apply(kq, Change(7, EvfiltTimer, EvDelete)) == 0); + + Require(Apply(kq, Change(8, EvfiltTimer, EvAdd, 0, 10)) == 0); + Require(Apply(kq, Change(8, EvfiltTimer, EvAdd, 0, 100)) == 0); + Require(Wait(kq, events, 4) == 1 && events[0].ident == 8); + std::this_thread::sleep_for(std::chrono::milliseconds(120)); + Require(Poll(kq, events, 4) == 1 && events[0].ident == 8 && events[0].data >= 1 && events[0].data <= 4); + Require(Apply(kq, Change(8, EvfiltTimer, EvDelete)) == 0); + + Require(Apply(kq, Change(9, EvfiltTimer, EvAdd, 0, 20)) == 0); + Require(Apply(kq, Change(9, EvfiltTimer, EvAdd | EvOneshot, 0, 20)) == 0); + Require(Wait(kq, events, 4) == 1 && events[0].ident == 9); + Require(Wait(kq, events, 4) == 1 && events[0].ident == 9); + Require(Apply(kq, Change(9, EvfiltTimer, EvDelete)) == 0); + + Require(Apply(kq, Change(10, EvfiltTimer, EvAdd | EvOneshot, 0, 100000)) == 0); + Require(Throws([kq] { Apply(kq, Change(10, EvfiltTimer, EvAdd, 0, 5)); })); + Require(Apply(kq, Change(11, EvfiltTimer, EvAdd, 0, 100000)) == 0); + Require(Throws([kq] { Apply(kq, Change(11, EvfiltTimer, EvAdd, NoteUseconds, 5)); })); + Require(Throws([kq] { Apply(kq, Change(11, EvfiltTimer, EvAdd, 0, 0)); })); + Require(Throws([kq] { Apply(kq, Change(11, EvfiltTimer, EvAdd, 0, INTPTR_MAX)); })); + Require(Throws([kq] { Apply(kq, Change(11, EvfiltTimer, EvAdd | EvClear, 0, 5)); })); + Require(Throws([kq] { Apply(kq, Change(11, EvfiltTimer, EvEnable, 0, 5)); })); + Require(Apply(kq, Change(12, EvfiltTimer, EvAdd | EvDisable, 0, 100000)) == 0); + Require(Throws([kq] { Apply(kq, Change(12, EvfiltTimer, EvAdd, 0, 5)); })); + Require(close_nid_postfix(kq) == 0); +} + +static void UnsupportedFilters() { + const int kq = kqueue_nid_postfix(); + for (const std::int16_t filter : {EvfiltAio, EvfiltLio, EvfiltSendfile}) { + RequireFailure(Apply(kq, Change(0, filter, EvAdd)), GuestEperm); + RequireFailure(Apply(kq, Change(5, filter, EvAdd)), GuestEperm); + RequireFailure(Apply(kq, Change(5, filter, EvAdd | EvClear)), GuestEperm); + RequireFailure(Apply(kq, Change(5, filter, EvDelete)), GuestEnoent); + RequireFailure(Apply(kq, Change(5, filter, EvEnable)), GuestEnoent); + const KernelEvent add = Change(5, filter, EvAdd, 7, 9); + KernelEvent failed[1]{}; + Require(kevent_nid_postfix(kq, &add, 1, failed, 1, nullptr) == 1); + Require(failed[0].flags == EvError && failed[0].data == GuestEperm && failed[0].fflags == 0); + } + int pipe[2]; + Require(pipe_nid_postfix(pipe) == 0); + int pair[2]; + Require(socketpair_nid_postfix(Unix, Stream, 0, pair) == 0); + RequireFailure(Apply(kq, Change(static_cast(pipe[0]), EvfiltProcdesc, EvAdd)), GuestEinval); + RequireFailure(Apply(kq, Change(static_cast(pipe[1]), EvfiltProcdesc, EvAdd)), GuestEinval); + RequireFailure(Apply(kq, Change(static_cast(pair[0]), EvfiltProcdesc, EvAdd)), GuestEinval); + RequireFailure(Apply(kq, Change(static_cast(pair[0]), EvfiltProcdesc, EvDelete)), GuestEnoent); + RequireFailure(Apply(kq, Change(static_cast(kq), EvfiltProcdesc, EvAdd)), GuestEinval); + RequireFailure(Apply(kq, Change(std::uintptr_t{1} << 40, EvfiltProcdesc, EvAdd)), GuestEbadf); + const KernelEvent attach = Change(static_cast(pipe[0]), EvfiltProcdesc, EvAdd, 7, 9); + KernelEvent failed[1]{}; + Require(kevent_nid_postfix(kq, &attach, 1, failed, 1, nullptr) == 1); + Require(failed[0].flags == EvError && failed[0].data == GuestEinval && failed[0].fflags == 0); + const int random = open_nid_postfix("/dev/urandom", 0, 0); + Require(random >= 0); + Require(Throws([kq, random] { Apply(kq, Change(static_cast(random), EvfiltProcdesc, EvAdd)); })); + Require(Throws([kq] { Apply(kq, Change(2, EvfiltProcdesc, EvAdd)); })); + RequireFailure(Apply(kq, Change(static_cast(random), EvfiltProcdesc, EvDelete)), GuestEnoent); + Require(close_nid_postfix(random) == 0); + Require(close_nid_postfix(pipe[0]) == 0 && close_nid_postfix(pipe[1]) == 0); + Require(close_nid_postfix(pair[0]) == 0 && close_nid_postfix(pair[1]) == 0); + RequireFailure(Apply(kq, Change(static_cast(pipe[0]), EvfiltProcdesc, EvAdd)), GuestEbadf); Require(close_nid_postfix(kq) == 0); } @@ -834,7 +927,28 @@ static void Errors() { } static std::string TemporaryName(const char* stem) { - return std::string("anyps5-kqueue-") + stem + "-" + std::to_string(std::chrono::steady_clock::now().time_since_epoch().count()); + return std::string("anyps5-kqueue-") + stem + "-" + std::to_string(getpid_nid_postfix()) + "-" + std::to_string(std::chrono::steady_clock::now().time_since_epoch().count()); +} + +static void ProcdescOnFiles() { + const int kq = kqueue_nid_postfix(); + const int other = kqueue_nid_postfix(); + RequireFailure(Apply(kq, Change(static_cast(other), EvfiltProcdesc, EvAdd)), GuestEinval); + const auto path = TemporaryName("procdesc-file"); + const int file = open_nid_postfix(path.c_str(), ReadWrite | Create, 0644); + Require(file >= 0); + RequireFailure(Apply(kq, Change(static_cast(file), EvfiltProcdesc, EvAdd)), GuestEinval); + Require(close_nid_postfix(file) == 0); + Require(std::filesystem::remove(path)); + const auto folder = TemporaryName("procdesc-directory"); + Require(std::filesystem::create_directory(folder)); + const int directory = open_nid_postfix(folder.c_str(), Directory, 0); + Require(directory >= 0); + RequireFailure(Apply(kq, Change(static_cast(directory), EvfiltProcdesc, EvAdd)), GuestEinval); + Require(close_nid_postfix(directory) == 0); + Require(std::filesystem::remove(folder)); + Require(close_nid_postfix(other) == 0); + Require(close_nid_postfix(kq) == 0); } static void FileWatch() { @@ -1185,6 +1299,9 @@ int main() { StandardInput(); CreateAndClose(); Timers(); + TimerReadd(); + UnsupportedFilters(); + ProcdescOnFiles(); PipeRead(); #ifndef __APPLE__ PipeWrite(); diff --git a/core/libs/tests/GuestLibcFilesystem.cpp b/core/libs/tests/GuestLibcFilesystem.cpp new file mode 100644 index 0000000000..903fc8b603 --- /dev/null +++ b/core/libs/tests/GuestLibcFilesystem.cpp @@ -0,0 +1,136 @@ +#include "SceTypes.hpp" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +extern "C" { +int APS5_VABI _ZNSt10filesystem5_StatEPKcPNS_5permsEPi_nid_postfix(const char* path, int* perms, int* error); +int APS5_VABI _ZNSt10filesystem6_LstatEPKcPNS_5permsEPi_nid_postfix(const char* path, int* perms, int* error); +std::uint64_t APS5_VABI _ZNSt10filesystem10_File_sizeEPKcPi_nid_postfix(const char* path, int* error); +int APS5_VABI _ZNSt10filesystem16_Last_write_timeEPKcPlS2__nid_postfix(const char* path, std::int64_t* seconds, std::int64_t* nanoseconds); +int APS5_VABI _ZNSt10filesystem9_Make_dirEPKcS1__nid_postfix(const char* path, const char* existing); +bool APS5_VABI _ZNSt10filesystem11_Remove_dirEPKc_nid_postfix(const char* path); +int APS5_VABI _ZNSt10filesystem7_UnlinkEPKc_nid_postfix(const char* path); +int APS5_VABI _ZNSt10filesystem6_ChmodEPKcNS_5permsE_nid_postfix(const char* path, int perms); +bool APS5_VABI _ZNSt10filesystem12_Current_getERA260_c_nid_postfix(char* target); +void* APS5_VABI _ZNSt10filesystem9_Open_dirERA260_cPKcRiRNS_9file_typeE_nid_postfix(char* target, const char* path, int* error, int* type); +char* APS5_VABI _ZNSt10filesystem9_Read_dirERA260_cPvRNS_9file_typeE_nid_postfix(char* target, void* handle, int* type); +int APS5_VABI _ZNSt10filesystem10_Close_dirEPv_nid_postfix(void* handle); +int APS5_VABI stat_nid_postfix(const char* path, FileStat* sb); +char* APS5_VABI getcwd_nid_postfix(char* buffer, std::size_t size); +} + +namespace { + +constexpr int NoSuchFile = 2; +constexpr int TypeNotFound = -1; +constexpr int TypeRegular = 1; +constexpr int TypeDirectory = 2; +constexpr int TypeUnknown = 8; + +void Require(bool condition, const char* message) { + if (condition) return; + std::fprintf(stderr, "filesystem: %s\n", message); + std::abort(); +} + +std::string root; + +std::string At(const char* name) { + return root + "/" + name; +} + +void Write(const std::string& path, std::size_t bytes) { + std::ofstream file(path, std::ios::binary | std::ios::trunc); + file << std::string(bytes, 'x'); +} + +void TestStat() { + Write(At("file"), 7); + int perms = -1; + int error = -1; + Require(_ZNSt10filesystem5_StatEPKcPNS_5permsEPi_nid_postfix(At("file").c_str(), &perms, &error) == TypeRegular && error == 0, "regular file"); + FileStat status{}; + Require(stat_nid_postfix(At("file").c_str(), &status) == 0 && perms == (status.st_mode & 07777), "permission bits"); + Require(_ZNSt10filesystem5_StatEPKcPNS_5permsEPi_nid_postfix(root.c_str(), nullptr, &error) == TypeDirectory && error == 0, "directory"); + perms = 1234; + Require(_ZNSt10filesystem5_StatEPKcPNS_5permsEPi_nid_postfix(At("missing").c_str(), &perms, &error) == TypeNotFound && error == NoSuchFile && perms == 1234, "missing path"); + Require(_ZNSt10filesystem5_StatEPKcPNS_5permsEPi_nid_postfix(At("file/below").c_str(), nullptr, &error) == TypeNotFound, "path below a file"); + Require(_ZNSt10filesystem6_LstatEPKcPNS_5permsEPi_nid_postfix(At("file").c_str(), nullptr, &error) == TypeRegular && error == 0, "lstat of a regular file"); + std::error_code link; + std::filesystem::create_symlink("file", At("link"), link); + if (!link) Require(_ZNSt10filesystem6_LstatEPKcPNS_5permsEPi_nid_postfix(At("link").c_str(), nullptr, &error) == TypeRegular, "lstat follows a link as the console does"); +} + +void TestSizeAndTime() { + int error = 0; + Require(_ZNSt10filesystem10_File_sizeEPKcPi_nid_postfix(At("file").c_str(), &error) == 7 && error == 0, "file size"); + error = 0; + Require(_ZNSt10filesystem10_File_sizeEPKcPi_nid_postfix(At("missing").c_str(), &error) == UINT64_MAX && error == NoSuchFile, "size of a missing file"); + std::int64_t seconds = 0; + std::int64_t nanoseconds = -1; + FileStat status{}; + Require(stat_nid_postfix(At("file").c_str(), &status) == 0, "stat for time"); + Require(_ZNSt10filesystem16_Last_write_timeEPKcPlS2__nid_postfix(At("file").c_str(), &seconds, &nanoseconds) == 0, "last write time"); + Require(seconds == status.st_mtim.tv_sec && nanoseconds == status.st_mtim.tv_nsec, "last write time matches stat"); + seconds = 5; + Require(_ZNSt10filesystem16_Last_write_timeEPKcPlS2__nid_postfix(At("missing").c_str(), &seconds, &nanoseconds) == NoSuchFile && seconds == 5, "time of a missing file"); +} + +void TestDirectories() { + Require(_ZNSt10filesystem9_Make_dirEPKcS1__nid_postfix(At("made").c_str(), nullptr) == 1, "make a directory"); + Require(_ZNSt10filesystem9_Make_dirEPKcS1__nid_postfix(At("made").c_str(), nullptr) == 0, "make an existing directory"); + Require(_ZNSt10filesystem9_Make_dirEPKcS1__nid_postfix(At("absent/made").c_str(), nullptr) == -NoSuchFile, "make below a missing directory"); + Require(_ZNSt10filesystem9_Make_dirEPKcS1__nid_postfix(At("copy").c_str(), At("missing").c_str()) == -NoSuchFile, "copy the mode of a missing directory"); + Require(_ZNSt10filesystem9_Make_dirEPKcS1__nid_postfix(At("copy").c_str(), At("made").c_str()) == 1, "copy the mode of a directory"); + Require(_ZNSt10filesystem11_Remove_dirEPKc_nid_postfix(At("copy").c_str()), "remove a directory"); + Require(!_ZNSt10filesystem11_Remove_dirEPKc_nid_postfix(At("copy").c_str()), "remove a missing directory"); + char name[260]; + int error = -1; + int type = -1; + Require(_ZNSt10filesystem9_Open_dirERA260_cPKcRiRNS_9file_typeE_nid_postfix(name, At("made").c_str(), &error, &type) == nullptr && error == 0 && name[0] == '\0', "an empty directory gives no handle"); + Require(_ZNSt10filesystem9_Open_dirERA260_cPKcRiRNS_9file_typeE_nid_postfix(name, At("missing").c_str(), &error, &type) == nullptr && error == NoSuchFile, "a missing directory"); + Write(At("made/one"), 1); + Write(At("made/two"), 2); + void* handle = _ZNSt10filesystem9_Open_dirERA260_cPKcRiRNS_9file_typeE_nid_postfix(name, At("made").c_str(), &error, &type); + Require(handle != nullptr && error == 0 && type == TypeUnknown, "open a directory"); + std::set names{name}; + type = 0; + Require(_ZNSt10filesystem9_Read_dirERA260_cPvRNS_9file_typeE_nid_postfix(name, handle, &type) == name && type == TypeUnknown, "read a directory"); + names.insert(name); + Require(names == std::set{"one", "two"}, "entries without dot entries"); + Require(_ZNSt10filesystem9_Read_dirERA260_cPvRNS_9file_typeE_nid_postfix(name, handle, &type)[0] == '\0' && type == TypeUnknown, "end of the directory"); + Require(_ZNSt10filesystem10_Close_dirEPv_nid_postfix(handle) == 0, "close a directory"); +} + +void TestFiles() { + Require(_ZNSt10filesystem6_ChmodEPKcNS_5permsE_nid_postfix(At("file").c_str(), 0600) == 0, "chmod"); + Require(_ZNSt10filesystem6_ChmodEPKcNS_5permsE_nid_postfix(At("missing").c_str(), 0600) == -1, "chmod of a missing file"); + Require(_ZNSt10filesystem7_UnlinkEPKc_nid_postfix(At("made/one").c_str()) == 0, "unlink"); + Require(_ZNSt10filesystem7_UnlinkEPKc_nid_postfix(At("made/one").c_str()) == NoSuchFile, "unlink a missing file"); + char current[260]; + char expected[256]; + Require(getcwd_nid_postfix(expected, sizeof(expected)) != nullptr, "getcwd"); + Require(_ZNSt10filesystem12_Current_getERA260_c_nid_postfix(current) && std::strcmp(current, expected) == 0, "current directory"); +} + +} + +int main() { + root = "anyps5-libc-filesystem-" + std::to_string(std::random_device{}()); + std::filesystem::create_directory(root); + TestStat(); + TestSizeAndTime(); + TestDirectories(); + TestFiles(); + std::error_code ignored; + std::filesystem::remove_all(root, ignored); + return 0; +} diff --git a/core/libs/tests/GuestLibcRandomSeeding.cpp b/core/libs/tests/GuestLibcRandomSeeding.cpp new file mode 100644 index 0000000000..0e1ed63a76 --- /dev/null +++ b/core/libs/tests/GuestLibcRandomSeeding.cpp @@ -0,0 +1,118 @@ +#include "prx/libc/include/general/VabiMacros.hpp" +#include +#include +#include +#include +#include +#include +#include + +extern "C" { +std::int64_t APS5_VABI random_nid_postfix(); +void APS5_VABI srandom_nid_postfix(std::uint64_t); +char* APS5_VABI initstate_nid_postfix(std::uint64_t, char*, std::int64_t); +char* APS5_VABI setstate_nid_postfix(char*); +} + +static int failures = 0; + +static void Require(bool condition, const char* message) { + if (!condition) { + std::fprintf(stderr, "%s\n", message); + ++failures; + } +} + +static std::vector Draw(std::size_t count) { + std::vector values; + values.reserve(count); + for (std::size_t index = 0; index < count; ++index) values.push_back(random_nid_postfix()); + return values; +} + +static void RequireSequence(const std::vector& actual, const std::vector& expected, const char* message) { + Require(actual == expected, message); +} + +static std::string FirstStateWord(const char* table) { + static const char* digits = "0123456789abcdef"; + std::string text; + const unsigned char* bytes = reinterpret_cast(table); + for (std::size_t index = 0; index < 8; ++index) { + text.push_back(digits[bytes[index] >> 4]); + text.push_back(digits[bytes[index] & 0x0f]); + } + return text; +} + +struct StateCase { + std::int64_t size; + std::vector values; + const char* firstStateWord; +}; + +int main() { + RequireSequence(Draw(5), {1804289383, 846930886, 1681692777, 1714636915, 1957747793}, + "a fresh process must draw the measured first five values"); + + srandom_nid_postfix(1); + RequireSequence(Draw(6), {1804289383, 846930886, 1681692777, 1714636915, 1957747793, 424238335}, + "srandom(1) must match the measured sequence"); + + srandom_nid_postfix(0); + RequireSequence(Draw(6), {577655601, 1248161417, 192959563, 143633695, 541917479, 1903093295}, + "srandom(0) must match the measured sequence"); + + srandom_nid_postfix(2); + RequireSequence(Draw(6), {1505335290, 1738766719, 190686788, 260874575, 747983061, 906156498}, + "srandom(2) must match the measured sequence"); + + srandom_nid_postfix(12345); + RequireSequence(Draw(6), {383100999, 858300821, 357768173, 455528251, 133005921, 116285904}, + "srandom(12345) must match the measured sequence"); + + srandom_nid_postfix(0xffffffffull); + RequireSequence(Draw(6), {254925627, 1205188300, 366127624, 1401405153, 76053476, 1604170158}, + "srandom(0xffffffff) must match the measured sequence"); + + srandom_nid_postfix(0x80000000ull); + RequireSequence(Draw(6), {1336741213, 1210407648, 1447044896, 337392383, 82502902, 538660432}, + "srandom(0x80000000) must match the measured sequence"); + + std::vector tiny(16); + Require(initstate_nid_postfix(1, tiny.data(), 0) == nullptr, "initstate of 0 bytes must return null"); + Require(initstate_nid_postfix(1, tiny.data(), 7) == nullptr, "initstate of 7 bytes must return null"); + + const std::array states {{ + {8, {1646035001, 1025921153, 510616708, 590357944, 771515668}, "000000001465fc2d"}, + {15, {1646035001, 1025921153, 510616708, 590357944, 771515668}, "000000001465fc2d"}, + {31, {1646035001, 1025921153, 510616708, 590357944, 771515668}, "000000001465fc2d"}, + {32, {964237963, 406111040, 156505215, 1274863108, 1882652865}, "0100000082016ee0"}, + {63, {964237963, 406111040, 156505215, 1274863108, 1882652865}, "0100000082016ee0"}, + {64, {1894937090, 1645272306, 2143216519, 1889283008, 669383071}, "02000000fea8f5e2"}, + {127, {1894937090, 1645272306, 2143216519, 1889283008, 669383071}, "02000000fea8f5e2"}, + {128, {1804289383, 846930886, 1681692777, 1714636915, 1957747793}, "03000000b1391599"}, + {129, {1804289383, 846930886, 1681692777, 1714636915, 1957747793}, "03000000b1391599"}, + {255, {1804289383, 846930886, 1681692777, 1714636915, 1957747793}, "03000000b1391599"}, + {256, {510644794, 625058908, 1816371419, 326864818, 1257431873}, "0400000032ba371f"}, + {257, {510644794, 625058908, 1816371419, 326864818, 1257431873}, "0400000032ba371f"}, + {1000, {510644794, 625058908, 1816371419, 326864818, 1257431873}, "0400000032ba371f"}, + }}; + + std::vector> buffers; + for (const auto& state : states) { + buffers.emplace_back(static_cast(state.size) + 16, static_cast(0xa5)); + char* const buffer = buffers.back().data(); + char* const returned = initstate_nid_postfix(1, buffer, state.size); + Require(returned != nullptr, "initstate must not return null from 8 bytes up"); + RequireSequence(Draw(5), state.values, "initstate must draw the measured values"); + Require(FirstStateWord(buffer) == state.firstStateWord, "initstate must save the measured type word and first state word"); + } + + std::vector last(128); + char* const replaced = initstate_nid_postfix(5, last.data(), 128); + Require(replaced != nullptr, "initstate must not return null from 8 bytes up"); + Require(setstate_nid_postfix(replaced) != nullptr, "setstate of the state initstate returned must not return null"); + + return failures == 0 ? 0 : 1; +} diff --git a/core/libs/tests/GuestLibcThreads.cpp b/core/libs/tests/GuestLibcThreads.cpp index e811e6f77d..7536f444aa 100644 --- a/core/libs/tests/GuestLibcThreads.cpp +++ b/core/libs/tests/GuestLibcThreads.cpp @@ -54,6 +54,25 @@ int APS5_VABI _Tss_set_nid_postfix(int key, void* value); int APS5_VABI _Thrd_equal_nid_postfix(void* first, void* second); void* APS5_VABI _Thrd_current_nid_postfix(void); int APS5_VABI _Mtx_current_owns_nid_postfix(void** handle); +int APS5_VABI _ZNSt8__sce_v29_Mtx_initEPP13pthread_mutexi_nid_postfix(void** mutex, int type); +int APS5_VABI _ZNSt8__sce_v219_Mtx_init_with_nameEPP13pthread_mutexiPKc_nid_postfix(void** mutex, int type, const char* name); +int APS5_VABI _ZNSt8__sce_v212_Mtx_destroyEPP13pthread_mutex_nid_postfix(void** mutex); +int APS5_VABI _ZNSt8__sce_v29_Mtx_lockEPP13pthread_mutex_nid_postfix(void** mutex); +int APS5_VABI _ZNSt8__sce_v212_Mtx_trylockEPP13pthread_mutex_nid_postfix(void** mutex); +int APS5_VABI _ZNSt8__sce_v211_Mtx_unlockEPP13pthread_mutex_nid_postfix(void** mutex); +int APS5_VABI _ZNSt8__sce_v29_Cnd_initEPP12pthread_cond_nid_postfix(void** condition); +int APS5_VABI _ZNSt8__sce_v219_Cnd_init_with_nameEPP12pthread_condPKc_nid_postfix(void** condition, const char* name); +int APS5_VABI _ZNSt8__sce_v212_Cnd_destroyEPP12pthread_cond_nid_postfix(void** condition); +int APS5_VABI _ZNSt8__sce_v29_Cnd_waitEPP12pthread_condPP13pthread_mutex_nid_postfix(void** condition, void** mutex); +int APS5_VABI _ZNSt8__sce_v211_Cnd_signalEPP12pthread_cond_nid_postfix(void** condition); +int APS5_VABI _ZNSt8__sce_v214_Cnd_broadcastEPP12pthread_cond_nid_postfix(void** condition); +int APS5_VABI _ZNSt8__sce_v212_Thrd_detachEP7pthread_nid_postfix(void* thread); +int APS5_VABI _ZNSt8__sce_v210_Thrd_joinEP7pthreadPi_nid_postfix(void* thread, int* code); +void APS5_VABI _ZNSt8__sce_v211_Thrd_yieldEv_nid_postfix(); +void* APS5_VABI _ZNSt8__sce_v28_Thrd_idEv_nid_postfix(); +int APS5_VABI scePthreadCreate(void** thread, const void* attr, void* (APS5_VABI* entry)(void*), void* argument, const char* name); +int APS5_VABI scePthreadMutexUnlock(void** mutex); +int APS5_VABI scePthreadCondWait(void** condition, void** mutex); void* APS5_VABI scePthreadSelf(); int APS5_VABI _Thrd_yield_nid_postfix(); } @@ -375,6 +394,78 @@ static void ForwardedCalls() { RequireThrows([] { _Tss_get_nid_postfix(0); }); } +struct VersionedShared { + void* mutex = nullptr; + void* condition = nullptr; + std::atomic stage{0}; + int tryResult = -1; +}; + +static void* APS5_VABI VersionedWorker(void* argument) { + auto* shared = static_cast(argument); + shared->tryResult = _ZNSt8__sce_v212_Mtx_trylockEPP13pthread_mutex_nid_postfix(&shared->mutex); + shared->stage = 1; + Require(_ZNSt8__sce_v29_Mtx_lockEPP13pthread_mutex_nid_postfix(&shared->mutex) == success); + shared->stage = 2; + Require(_ZNSt8__sce_v211_Cnd_signalEPP12pthread_cond_nid_postfix(&shared->condition) == success); + Require(_ZNSt8__sce_v211_Mtx_unlockEPP13pthread_mutex_nid_postfix(&shared->mutex) == success); + return reinterpret_cast(std::intptr_t{42}); +} + +static void* APS5_VABI DetachedWorker(void*) { return nullptr; } + +static void VersionedSync() { + void* recursiveMutex = reinterpret_cast(std::uintptr_t{1}); + Require(_ZNSt8__sce_v29_Mtx_initEPP13pthread_mutexi_nid_postfix(&recursiveMutex, plain | recursive) == success && recursiveMutex != nullptr); + Require(_ZNSt8__sce_v29_Mtx_lockEPP13pthread_mutex_nid_postfix(&recursiveMutex) == success); + Require(_ZNSt8__sce_v29_Mtx_lockEPP13pthread_mutex_nid_postfix(&recursiveMutex) == success); + Require(_ZNSt8__sce_v212_Mtx_trylockEPP13pthread_mutex_nid_postfix(&recursiveMutex) == success); + for (int unlock = 0; unlock < 3; ++unlock) Require(_ZNSt8__sce_v211_Mtx_unlockEPP13pthread_mutex_nid_postfix(&recursiveMutex) == success); + Require(_ZNSt8__sce_v212_Mtx_destroyEPP13pthread_mutex_nid_postfix(&recursiveMutex) == success); + + void* plainMutex = nullptr; + Require(_ZNSt8__sce_v219_Mtx_init_with_nameEPP13pthread_mutexiPKc_nid_postfix(&plainMutex, plain, "") == success); + Require(_ZNSt8__sce_v29_Mtx_lockEPP13pthread_mutex_nid_postfix(&plainMutex) == success); + Require(_ZNSt8__sce_v29_Mtx_lockEPP13pthread_mutex_nid_postfix(&plainMutex) == busy); + Require(_ZNSt8__sce_v211_Mtx_unlockEPP13pthread_mutex_nid_postfix(&plainMutex) == success); + void* unownedCondition = nullptr; + Require(_ZNSt8__sce_v29_Cnd_initEPP12pthread_cond_nid_postfix(&unownedCondition) == success); + Require(scePthreadMutexUnlock(&plainMutex) != 0); + Require(scePthreadCondWait(&unownedCondition, &plainMutex) != 0); + Require(_ZNSt8__sce_v211_Mtx_unlockEPP13pthread_mutex_nid_postfix(&plainMutex) == success); + Require(_ZNSt8__sce_v29_Cnd_waitEPP12pthread_condPP13pthread_mutex_nid_postfix(&unownedCondition, &plainMutex) == success); + Require(_ZNSt8__sce_v212_Cnd_destroyEPP12pthread_cond_nid_postfix(&unownedCondition) == success); + Require(_ZNSt8__sce_v212_Mtx_destroyEPP13pthread_mutex_nid_postfix(&plainMutex) == success); + + VersionedShared shared; + Require(_ZNSt8__sce_v29_Mtx_initEPP13pthread_mutexi_nid_postfix(&shared.mutex, plain) == success); + Require(_ZNSt8__sce_v219_Cnd_init_with_nameEPP12pthread_condPKc_nid_postfix(&shared.condition, "versioned") == success); + Require(_ZNSt8__sce_v29_Mtx_lockEPP13pthread_mutex_nid_postfix(&shared.mutex) == success); + void* thread = nullptr; + Require(scePthreadCreate(&thread, nullptr, VersionedWorker, &shared, "versioned") == 0); + while (shared.stage.load() == 0) std::this_thread::yield(); + while (shared.stage.load() == 1) Require(_ZNSt8__sce_v29_Cnd_waitEPP12pthread_condPP13pthread_mutex_nid_postfix(&shared.condition, &shared.mutex) == success); + Require(shared.tryResult == busy); + Require(_ZNSt8__sce_v211_Mtx_unlockEPP13pthread_mutex_nid_postfix(&shared.mutex) == success); + int code = 0; + Require(_ZNSt8__sce_v210_Thrd_joinEP7pthreadPi_nid_postfix(thread, &code) == success && code == 42); + Require(_ZNSt8__sce_v210_Thrd_joinEP7pthreadPi_nid_postfix(thread, &code) == 4); + Require(_ZNSt8__sce_v214_Cnd_broadcastEPP12pthread_cond_nid_postfix(&shared.condition) == success); + Require(_ZNSt8__sce_v212_Cnd_destroyEPP12pthread_cond_nid_postfix(&shared.condition) == success); + Require(_ZNSt8__sce_v212_Mtx_destroyEPP13pthread_mutex_nid_postfix(&shared.mutex) == success); + + void* condition = nullptr; + Require(_ZNSt8__sce_v29_Cnd_initEPP12pthread_cond_nid_postfix(&condition) == success); + Require(_ZNSt8__sce_v212_Cnd_destroyEPP12pthread_cond_nid_postfix(&condition) == success); + + void* detached = nullptr; + Require(scePthreadCreate(&detached, nullptr, DetachedWorker, nullptr, "detached") == 0); + Require(_ZNSt8__sce_v212_Thrd_detachEP7pthread_nid_postfix(detached) == success); + Require(_ZNSt8__sce_v212_Thrd_detachEP7pthread_nid_postfix(nullptr) == 4); + _ZNSt8__sce_v211_Thrd_yieldEv_nid_postfix(); + Require(_ZNSt8__sce_v28_Thrd_idEv_nid_postfix() != nullptr); +} + static void ThreadHelpers() { Require(_Thrd_yield_nid_postfix() == success); std::thread other([&] { @@ -385,6 +476,7 @@ static void ThreadHelpers() { int main() { KernelKeys(); + VersionedSync(); ForwardedCalls(); MutexBasics(); RecursiveMutex(); diff --git a/core/libs/tests/GuestLibreSslAes.cpp b/core/libs/tests/GuestLibreSslAes.cpp new file mode 100644 index 0000000000..4146b14b36 --- /dev/null +++ b/core/libs/tests/GuestLibreSslAes.cpp @@ -0,0 +1,220 @@ +#include "prx/libc/include/general/VabiMacros.hpp" +#include +#include +#include +#include +#include +#include +#include +#include + +struct AesKey { + std::uint32_t roundKeys[60]; + std::int32_t rounds; +}; + +extern "C" { +int APS5_VABI AES_set_encrypt_key_nid_postfix(const unsigned char*, int, AesKey*); +int APS5_VABI AES_set_decrypt_key_nid_postfix(const unsigned char*, int, AesKey*); +void APS5_VABI AES_encrypt_nid_postfix(const unsigned char*, unsigned char*, const AesKey*); +void APS5_VABI AES_decrypt_nid_postfix(const unsigned char*, unsigned char*, const AesKey*); +void APS5_VABI AES_ecb_encrypt_nid_postfix(const unsigned char*, unsigned char*, const AesKey*, int); +void APS5_VABI AES_cbc_encrypt_nid_postfix(const unsigned char*, unsigned char*, std::size_t, const AesKey*, unsigned char*, int); +void APS5_VABI AES_cfb128_encrypt_nid_postfix(const unsigned char*, unsigned char*, std::size_t, const AesKey*, unsigned char*, int*, int); +void APS5_VABI AES_ctr128_encrypt_nid_postfix(const unsigned char*, unsigned char*, std::size_t, const AesKey*, unsigned char*, unsigned char*, unsigned*); +} + +static void Require(bool value, const char* what) { + if (value) return; + std::fprintf(stderr, "failed: %s\n", what); + std::abort(); +} + +template static void RequireThrows(TAction action, const char* what) { + try { action(); } catch (const std::exception&) { return; } + Require(false, what); +} + +static std::vector Bytes(const char* hex) { + std::vector bytes; + for (; hex[0] && hex[1]; hex += 2) bytes.push_back(static_cast(std::stoi(std::string(hex, 2), nullptr, 16))); + return bytes; +} + +static std::string Hex(const unsigned char* data, std::size_t size) { + static constexpr char digits[] = "0123456789abcdef"; + std::string text; + for (std::size_t i = 0; i < size; ++i) { + text += digits[data[i] >> 4]; + text += digits[data[i] & 15]; + } + return text; +} + +static std::uint32_t Word(const std::vector& bytes, std::size_t index) { + return std::uint32_t(bytes[4 * index]) << 24 | std::uint32_t(bytes[4 * index + 1]) << 16 | std::uint32_t(bytes[4 * index + 2]) << 8 | bytes[4 * index + 3]; +} + +static AesKey Sentinel() { + AesKey key; + for (std::uint32_t i = 0; i < 60; ++i) key.roundKeys[i] = 0xa5a50000u | i; + key.rounds = 0x5a5a; + return key; +} + +template static void RequireSchedule(TSetup setup, const char* keyHex, const char* scheduleHex, const char* what) { + const auto userKey = Bytes(keyHex); + const auto schedule = Bytes(scheduleHex); + const std::size_t used = schedule.size() / 4; + const AesKey before = Sentinel(); + AesKey key = before; + Require(setup(userKey.data(), static_cast(userKey.size() * 8), &key) == 0 && key.rounds == static_cast(used / 4 - 1), what); + for (std::size_t i = 0; i < 60; ++i) Require(key.roundKeys[i] == (i < used ? Word(schedule, i) : before.roundKeys[i]), what); +} + +template static void RequireRefusalsKeepKey(TSetup setup, const char* what) { + const unsigned char userKey[32] {}; + const AesKey before = Sentinel(); + for (const int bits : {0, 64, 127, 129, 160, 224, 255, 384, 512, -128}) { + AesKey key = before; + Require(setup(userKey, bits, &key) == -2 && !std::memcmp(&key, &before, sizeof(key)), what); + } + for (const int bits : {128, 64}) { + AesKey key = before; + Require(setup(nullptr, bits, &key) == -1 && !std::memcmp(&key, &before, sizeof(key)), what); + } + Require(setup(userKey, 128, nullptr) == -1, what); +} + +int main() { + const auto plain = Bytes("00112233445566778899aabbccddeeff"); + const char* keys[3] { + "000102030405060708090a0b0c0d0e0f", + "000102030405060708090a0b0c0d0e0f1011121314151617", + "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f", + }; + const char* ciphers[3] {"69c4e0d86a7b0430d8cdb78070b4c55a", "dda97ca4864cdfe06eaf70a0ec0d7191", "8ea2b7ca516745bfeafc49904b496089"}; + for (int i = 0; i < 3; ++i) { + const auto key = Bytes(keys[i]); + const int bits = static_cast(key.size() * 8); + AesKey encrypt {}, decrypt {}; + unsigned char out[16] {}, back[16] {}; + Require(AES_set_encrypt_key_nid_postfix(key.data(), bits, &encrypt) == 0 && encrypt.rounds == 10 + 2 * i, "encrypt key"); + Require(AES_set_decrypt_key_nid_postfix(key.data(), bits, &decrypt) == 0 && decrypt.rounds == encrypt.rounds, "decrypt key"); + const int last = 4 * encrypt.rounds; + Require(!std::memcmp(decrypt.roundKeys, encrypt.roundKeys + last, 16) && !std::memcmp(decrypt.roundKeys + last, encrypt.roundKeys, 16), "decrypt key reverses the round keys"); + AES_encrypt_nid_postfix(plain.data(), out, &encrypt); + Require(Hex(out, 16) == ciphers[i], "FIPS-197 appendix C cipher"); + AES_decrypt_nid_postfix(out, back, &decrypt); + Require(!std::memcmp(back, plain.data(), 16), "FIPS-197 appendix C inverse cipher"); + AES_ecb_encrypt_nid_postfix(plain.data(), out, &encrypt, 1); + Require(Hex(out, 16) == ciphers[i], "ECB encrypt"); + AES_ecb_encrypt_nid_postfix(out, out, &decrypt, 0); + Require(!std::memcmp(out, plain.data(), 16), "ECB decrypt in place"); + } + + AesKey fipsKey {}; + Require(AES_set_encrypt_key_nid_postfix(Bytes("2b7e151628aed2a6abf7158809cf4f3c").data(), 128, &fipsKey) == 0, "FIPS-197 appendix A key"); + Require(fipsKey.roundKeys[4] == 0xa0fafe17 && fipsKey.roundKeys[43] == 0xb6630ca6, "FIPS-197 appendix A.1 expansion"); + AesKey bad {}; + bad.rounds = 7; + bad.roundKeys[0] = 1; + const unsigned char zero[32] {}; + Require(AES_set_encrypt_key_nid_postfix(zero, 64, &bad) == -2 && bad.rounds == 7 && bad.roundKeys[0] == 1, "unsupported key length keeps the key"); + Require(AES_set_decrypt_key_nid_postfix(zero, 129, &bad) == -2 && bad.rounds == 7 && bad.roundKeys[0] == 1, "unsupported decrypt key length keeps the key"); + Require(AES_set_encrypt_key_nid_postfix(nullptr, 128, &bad) == -1 && AES_set_encrypt_key_nid_postfix(zero, 128, nullptr) == -1, "null key arguments"); + unsigned char block[16] {}; + RequireThrows([&] { AES_encrypt_nid_postfix(zero, block, &bad); }, "key schedule with an invalid round count"); + RequireRefusalsKeepKey(&AES_set_encrypt_key_nid_postfix, "refused encrypt key setup leaves the key unchanged"); + RequireRefusalsKeepKey(&AES_set_decrypt_key_nid_postfix, "refused decrypt key setup leaves the key unchanged"); + RequireSchedule(&AES_set_encrypt_key_nid_postfix, "2b7e151628aed2a6abf7158809cf4f3c", + "2b7e151628aed2a6abf7158809cf4f3ca0fafe1788542cb123a339392a6c7605f2c295f27a96b9435935807a7359f67f3d80477d4716fe3e1e237e446d7a883b" + "ef44a541a8525b7fb671253bdb0bad00d4d1c6f87c839d87caf2b8bc11f915bc6d88a37a110b3efddbf98641ca0093fd4e54f70e5f5fc9f384a64fb24ea6dc4f" + "ead27321b58dbad2312bf5607f8d292fac7766f319fadc2128d12941575c006ed014f9a8c9ee2589e13f0cc8b6630ca6", + "FIPS-197 appendix A.1 AES-128 encrypt schedule"); + RequireSchedule(&AES_set_encrypt_key_nid_postfix, "8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b", + "8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7bfe0c91f72402f5a5ec12068e6c827f6b0e7a95b95c56fec24db7b4bd69b5411885a74796e92538fd" + "e75fad44bb095386485af05721efb14fa448f6d94d6dce24aa326360113b30e6a25e7ed583b1cf9a27f939436a94f767c0a69407d19da4e1ec1786eb6fa64971" + "485f703222cb8755e26d135233f0b7b340beeb282f18a2596747d26b458c553ea7e1466c9411f1df821f750aad07d753ca4005388fcc5006282d166abc3ce7b5" + "e98ba06f448c773c8ecc720401002202", + "FIPS-197 appendix A.2 AES-192 encrypt schedule"); + RequireSchedule(&AES_set_encrypt_key_nid_postfix, "603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", + "603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff49ba354118e6925afa51a8b5f2067fcdea8b09c1a93d194cdbe49846eb75d5b9a" + "d59aecb85bf3c917fee94248de8ebe96b5a9328a2678a647983122292f6c79b3812c81addadf48ba24360af2fab8b46498c5bfc9bebd198e268c3ba709e04214" + "68007bacb2df331696e939e46c518d80c814e20476a9fb8a5025c02d59c58239de1369676ccc5a71fa2563959674ee155886ca5d2e2f31d77e0af1fa27cf73c3" + "749c47ab18501ddae2757e4f7401905acafaaae3e4d59b349adf6acebd10190dfe4890d1e6188d0b046df344706c631e", + "FIPS-197 appendix A.3 AES-256 encrypt schedule"); + RequireSchedule(&AES_set_decrypt_key_nid_postfix, "2b7e151628aed2a6abf7158809cf4f3c", + "d014f9a8c9ee2589e13f0cc8b6630ca60c7b5a631319eafeb0398890664cfbb4df7d925a1f62b09da320626ed675732412c07647c01f22c7bc42d2f37555114a" + "6efcd876d2df54807c5df034c917c3b96ea30afcbc238cf6ae82a4b4b54a338d90884413d280860a12a128421bc897397c1f13f74208c219c021ae480969bf7b" + "cc7505eb3e17d1ee82296c51c94811332b3708a7f262d405bc3ebdbf4b617d622b7e151628aed2a6abf7158809cf4f3c", + "AES-128 decrypt schedule"); + RequireSchedule(&AES_set_decrypt_key_nid_postfix, "8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b", + "e98ba06f448c773c8ecc720401002202ac491644e55710b746c08a75c89b2cada3979ac28e5ba6d8e12cc9e654b272baf3b42258b59ebb5cf8fb64fe491e06f3" + "4d65dfa2b1e5620dea899c312dcc3c1a5b6cfe3cc745a02bf8b9a572462a9904c5ddb7f8be933c760b4f46a6fc80bdafb5dc7ad0f7cffb09a7ec43939c295e17" + "5023b89a3bc51d84d04b19377b4e8b8e41b34544ab0592b9ce92f15e421381d9659763e78c817087123039436be6a51e9eb149c479d69c5dfeb4a27ceab6d7fd" + "8e73b0f7da0e6452c810f32b809079e5", + "AES-192 decrypt schedule"); + RequireSchedule(&AES_set_decrypt_key_nid_postfix, "603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", + "fe4890d1e6188d0b046df344706c631eada23f4963e23b2455427c8a5c70910457c96cf6074f07c0706abb07137f9241b668b621ce40046d36a047ae0932ed8e" + "34ad1e4450866b367725bcc76315294632526c367828b24cf8e043c33f92aa20c440b289642b757227a3d7f114309581d669a7334a7ade7a80c8f18fc772e9e3" + "25ba3c22a06bc7fb4388a2833393427054fb808b9c137949cab22ff547ba186c6c3d632985d1fbd9e3e36578701be0f34a7459f9c8e8f9c256a156bc8d083799" + "42107758e9ec98f066329ea193f8858b8ec6bff6829ca03b9e49af7edba96125603deb1015ca71be2b73aef0857d7781", + "AES-256 decrypt schedule"); + + const auto key = Bytes("2b7e151628aed2a6abf7158809cf4f3c"); + const auto message = Bytes("6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710"); + AesKey encrypt {}, decrypt {}; + AES_set_encrypt_key_nid_postfix(key.data(), 128, &encrypt); + AES_set_decrypt_key_nid_postfix(key.data(), 128, &decrypt); + std::vector out(message.size() + 16), back(message.size() + 16); + + auto iv = Bytes("000102030405060708090a0b0c0d0e0f"); + AES_cbc_encrypt_nid_postfix(message.data(), out.data(), 64, &encrypt, iv.data(), 1); + Require(Hex(out.data(), 64) == "7649abac8119b246cee98e9b12e9197d5086cb9b507219ee95db113a917678b273bed6b8e3c1743b7116e69e222295163ff1caa1681fac09120eca307586e1a7", "SP 800-38A F.2.1 CBC encrypt"); + Require(Hex(iv.data(), 16) == "3ff1caa1681fac09120eca307586e1a7", "CBC encrypt returns the last cipher block as iv"); + iv = Bytes("000102030405060708090a0b0c0d0e0f"); + std::memcpy(back.data(), out.data(), 64); + AES_cbc_encrypt_nid_postfix(back.data(), back.data(), 32, &decrypt, iv.data(), 0); + AES_cbc_encrypt_nid_postfix(back.data() + 32, back.data() + 32, 32, &decrypt, iv.data(), 0); + Require(!std::memcmp(back.data(), message.data(), 64) && Hex(iv.data(), 16) == "3ff1caa1681fac09120eca307586e1a7", "SP 800-38A F.2.2 CBC decrypt in place across calls"); + iv = Bytes("000102030405060708090a0b0c0d0e0f"); + std::vector partial(32, 0xee); + AES_cbc_encrypt_nid_postfix(message.data(), partial.data(), 20, &encrypt, iv.data(), 1); + Require(Hex(partial.data(), 16) == "7649abac8119b246cee98e9b12e9197d" && partial[31] != 0xee && Hex(iv.data(), 16) == Hex(partial.data() + 16, 16), "CBC encrypt pads a partial block with the iv and writes the whole block"); + iv = Bytes("000102030405060708090a0b0c0d0e0f"); + std::vector plainPartial(32, 0xee); + AES_cbc_encrypt_nid_postfix(partial.data(), plainPartial.data(), 20, &decrypt, iv.data(), 0); + Require(!std::memcmp(plainPartial.data(), message.data(), 20) && plainPartial[20] == 0xee && Hex(iv.data(), 16) == Hex(partial.data() + 16, 16), "CBC decrypt writes only the requested length"); + + iv = Bytes("000102030405060708090a0b0c0d0e0f"); + int num = 0; + AES_cfb128_encrypt_nid_postfix(message.data(), out.data(), 5, &encrypt, iv.data(), &num, 1); + AES_cfb128_encrypt_nid_postfix(message.data() + 5, out.data() + 5, 32, &encrypt, iv.data(), &num, 1); + Require(num == 5, "CFB128 keeps the block position"); + AES_cfb128_encrypt_nid_postfix(message.data() + 37, out.data() + 37, 27, &encrypt, iv.data(), &num, 1); + Require(Hex(out.data(), 64) == "3b3fd92eb72dad20333449f8e83cfb4ac8a64537a0b3a93fcde3cdad9f1ce58b26751f67a3cbb140b1808cf187a4f4dfc04b05357c5d1c0eeac4c66f9ff7f2e6" && num == 0, "SP 800-38A F.3.13 CFB128 encrypt in pieces"); + iv = Bytes("000102030405060708090a0b0c0d0e0f"); + num = 0; + AES_cfb128_encrypt_nid_postfix(out.data(), back.data(), 64, &encrypt, iv.data(), &num, 0); + Require(!std::memcmp(back.data(), message.data(), 64), "SP 800-38A F.3.14 CFB128 decrypt"); + num = 16; + RequireThrows([&] { AES_cfb128_encrypt_nid_postfix(message.data(), out.data(), 1, &encrypt, iv.data(), &num, 1); }, "CFB128 position out of range"); + + auto counter = Bytes("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff"); + unsigned char keystream[16] {}; + unsigned position = 0; + AES_ctr128_encrypt_nid_postfix(message.data(), out.data(), 7, &encrypt, counter.data(), keystream, &position); + AES_ctr128_encrypt_nid_postfix(message.data() + 7, out.data() + 7, 57, &encrypt, counter.data(), keystream, &position); + Require(Hex(out.data(), 64) == "874d6191b620e3261bef6864990db6ce9806f66b7970fdff8617187bb9fffdff5ae4df3edbd5d35e5b4f09020db03eab1e031dda2fbe03d1792170a0f3009cee", "SP 800-38A F.5.1 CTR encrypt in pieces"); + Require(position == 0 && Hex(counter.data(), 16) == "f0f1f2f3f4f5f6f7f8f9fafbfcfdff03", "CTR advances the counter per block"); + counter = Bytes("ffffffffffffffffffffffffffffffff"); + position = 0; + AES_ctr128_encrypt_nid_postfix(message.data(), out.data(), 37, &encrypt, counter.data(), keystream, &position); + Require(Hex(out.data(), 37) == "e13338e36cb71962e00d020b4cedbd86d3dae15b04bb352fa0f59febfcb4da3e67da610697", "CTR counter wraps across all 128 bits"); + Require(position == 5 && Hex(counter.data(), 16) == "00000000000000000000000000000002", "CTR position and counter after a partial block"); + position = 16; + RequireThrows([&] { AES_ctr128_encrypt_nid_postfix(message.data(), out.data(), 1, &encrypt, counter.data(), keystream, &position); }, "CTR position out of range"); + + std::puts("libSceLibreSsl AES tests passed"); +} diff --git a/core/libs/tests/GuestLocale.cpp b/core/libs/tests/GuestLocale.cpp index 78aaee27be..506939f2da 100644 --- a/core/libs/tests/GuestLocale.cpp +++ b/core/libs/tests/GuestLocale.cpp @@ -1,6 +1,8 @@ +#include "prx/libc/include/ApplicationHeap.hpp" #include "prx/libc/include/GuestLocale.hpp" #include #include +#include #include #include #include @@ -36,7 +38,15 @@ std::uint32_t APS5_VABI _Towctrans_nid_postfix(std::uint32_t wc, std::uint64_t d int* APS5_VABI __error_nid_postfix(); extern std::uint64_t _ZNSt7num_getIcSt19istreambuf_iteratorIcSt11char_traitsIcEEE2idE_nid_postfix; extern std::uint64_t _ZNSt8time_getIcSt19istreambuf_iteratorIcSt11char_traitsIcEEE2idE_nid_postfix; +extern std::uint64_t _ZNSt8time_putIcSt19ostreambuf_iteratorIcSt11char_traitsIcEEE2idE_nid_postfix; extern std::uintptr_t _ZTVSt7num_getIcSt19istreambuf_iteratorIcSt11char_traitsIcEEE_nid_postfix[17]; +extern std::uintptr_t _ZTVSt7codecvtIwc9_MbstatetE_nid_postfix[13]; +extern std::uintptr_t _ZTVSt7num_putIwSt19ostreambuf_iteratorIwSt11char_traitsIwEEE_nid_postfix[14]; +std::size_t APS5_VABI _ZNSt7codecvtIcc9_MbstatetE7_GetcatEPPKNSt6locale5facetEPKS2__nid_postfix(GuestLocale::Facet** facet, const GuestLocale::Implementation* const* locale); +std::size_t APS5_VABI _ZNSt8numpunctIcE7_GetcatEPPKNSt6locale5facetEPKS1__nid_postfix(GuestLocale::Facet** facet, const GuestLocale::Implementation* const* locale); +std::size_t APS5_VABI _ZNSt7num_putIcSt19ostreambuf_iteratorIcSt11char_traitsIcEEE7_GetcatEPPKNSt6locale5facetEPKS5__nid_postfix(GuestLocale::Facet** facet, const GuestLocale::Implementation* const* locale); +std::size_t APS5_VABI _ZNSt8time_putIcSt19ostreambuf_iteratorIcSt11char_traitsIcEEE7_GetcatEPPKNSt6locale5facetEPKS5__nid_postfix(GuestLocale::Facet** facet, const GuestLocale::Implementation* const* locale); +extern std::uintptr_t _ZTVSt7collateIcE_nid_postfix[9]; } @@ -44,6 +54,17 @@ static void Require(bool condition) { if (!condition) throw std::runtime_error("Guest locale check failed"); } +void* APS5_VABI TestHeapAllocate(std::size_t bytes) { return std::malloc(bytes); } +void APS5_VABI TestHeapFree(void* pointer) { std::free(pointer); } +void* APS5_VABI TestHeapCalloc(std::size_t count, std::size_t bytes) { return std::calloc(count, bytes); } +void* APS5_VABI TestHeapReallocate(void* pointer, std::size_t bytes) { return std::realloc(pointer, bytes); } +void* APS5_VABI TestHeapAlign(std::size_t, std::size_t bytes) { return std::malloc(bytes); } +void* APS5_VABI TestHeapRealign(void* pointer, std::size_t bytes, std::size_t) { return std::realloc(pointer, bytes); } +int APS5_VABI TestHeapPosixAlign(void** pointer, std::size_t, std::size_t bytes) { + *pointer = std::malloc(bytes); + return *pointer == nullptr ? 12 : 0; +} + template static void RequireException(TAction action) { bool caught = false; @@ -65,6 +86,24 @@ static void CheckInputFacetObjects() { for (const auto slot : _ZTVSt7num_getIcSt19istreambuf_iteratorIcSt11char_traitsIcEEE_nid_postfix) Require(slot == 0); } +static void CheckCodecvtWideVtableObject() { + for (std::uintptr_t& slot : _ZTVSt7codecvtIwc9_MbstatetE_nid_postfix) { + Require(slot == 0); + slot = 0x1111; + Require(slot == 0x1111); + slot = 0; + } +} + +static void CheckCollateVtableObject() { + for (std::uintptr_t& slot : _ZTVSt7collateIcE_nid_postfix) { + Require(slot == 0); + slot = 0x1111; + Require(slot == 0x1111); + slot = 0; + } +} + static void CheckGuestCalls() { auto* locale = _ZSt21_sceLibcClassicLocale_nid_postfix; Require(locale != nullptr && locale == _ZNSt6locale16_GetgloballocaleEv_nid_postfix()); @@ -253,7 +292,69 @@ static void CheckWideCaseTranslation() { } } +static void CheckWideNumPutVtableObject() { + for (std::uintptr_t& slot : _ZTVSt7num_putIwSt19ostreambuf_iteratorIwSt11char_traitsIwEEE_nid_postfix) { + Require(slot == 0); + slot = 0x1111; + Require(slot == 0x1111); + slot = 0; + } +} + +static void CheckFacetGetcat() { + const GuestLocale::Implementation* locale = _ZSt21_sceLibcClassicLocale_nid_postfix; + GuestLocale::Facet* codecvt = nullptr; + Require(_ZNSt7codecvtIcc9_MbstatetE7_GetcatEPPKNSt6locale5facetEPKS2__nid_postfix(&codecvt, &locale) == 2 && codecvt != nullptr); + auto* codecvtVtable = reinterpret_cast(codecvt->vtable); + Require(codecvtVtable->alwaysNoconv(codecvt) && codecvtVtable->encoding(codecvt) == 1 && codecvtVtable->maxLength(codecvt) == 1); + const char* nextFrom = nullptr; + char target[4]{}; + char* nextTo = nullptr; + Require(codecvtVtable->out(codecvt, nullptr, "ab", "ab" + 2, &nextFrom, target, target + 4, &nextTo) == 3); + Require(nextFrom == "ab" && nextTo == target); + Require(codecvtVtable->in(codecvt, nullptr, "ab", "ab" + 2, &nextFrom, target, target + 4, &nextTo) == 3); + nextTo = reinterpret_cast(0x10); + Require(codecvtVtable->unshift(codecvt, nullptr, nullptr, nullptr, &nextTo) == 0 && nextTo == nullptr); + Require(codecvtVtable->length(codecvt, nullptr, "ab", "ab" + 2, 5) == 2); + Require(codecvtVtable->length(codecvt, nullptr, "ab", "ab" + 2, 1) == 1); + GuestLocale::Facet* punct = nullptr; + Require(_ZNSt8numpunctIcE7_GetcatEPPKNSt6locale5facetEPKS1__nid_postfix(&punct, &locale) == 4 && punct != nullptr); + auto* punctVtable = reinterpret_cast(punct->vtable); + auto* punctFacet = reinterpret_cast(punct); + Require(punctVtable->decimalPoint(punctFacet) == '.' && punctVtable->thousandsSep(punctFacet) == ','); + GuestLocale::String name{}; + Require(punctVtable->grouping(&name, punctFacet) == &name && name.size == 0); + const char* contents = name.size <= 15 ? name.buffer : name.pointer; + Require(punctVtable->falsename(&name, punctFacet)->size == 5 && std::memcmp(contents, "false", 5) == 0); + contents = name.size <= 15 ? name.buffer : name.pointer; + Require(punctVtable->truename(&name, punctFacet)->size == 4 && std::memcmp(contents, "true", 4) == 0); + GuestLocale::Facet* numPut = nullptr; + Require(_ZNSt7num_putIcSt19ostreambuf_iteratorIcSt11char_traitsIcEEE7_GetcatEPPKNSt6locale5facetEPKS5__nid_postfix(&numPut, &locale) == 4 && numPut != nullptr); + GuestLocale::Facet* timePut = nullptr; + Require(_ZNSt8time_putIcSt19ostreambuf_iteratorIcSt11char_traitsIcEEE7_GetcatEPPKNSt6locale5facetEPKS5__nid_postfix(&timePut, &locale) == 5 && timePut != nullptr); + _ZNSt8time_putIcSt19ostreambuf_iteratorIcSt11char_traitsIcEEE2idE_nid_postfix = 11; + Require(_ZNSt8time_putIcSt19ostreambuf_iteratorIcSt11char_traitsIcEEE2idE_nid_postfix == 11); + _ZNSt8time_putIcSt19ostreambuf_iteratorIcSt11char_traitsIcEEE2idE_nid_postfix = 0; + for (GuestLocale::Facet* facet : {codecvt, punct, numPut, timePut}) { + facet->vtable->retain(facet); + facet->vtable->retain(facet); + Require(facet->references == 2); + facet->vtable->deleteObject(facet); + } + Require(_ZNSt7codecvtIcc9_MbstatetE7_GetcatEPPKNSt6locale5facetEPKS2__nid_postfix(nullptr, &locale) == 2); + Require(_ZNSt8numpunctIcE7_GetcatEPPKNSt6locale5facetEPKS1__nid_postfix(&punct, &locale) == 4); +} + int main() { + std::array heapApi{}; + heapApi[0] = reinterpret_cast(&TestHeapAllocate); + heapApi[1] = reinterpret_cast(&TestHeapFree); + heapApi[2] = reinterpret_cast(&TestHeapCalloc); + heapApi[3] = reinterpret_cast(&TestHeapReallocate); + heapApi[4] = reinterpret_cast(&TestHeapAlign); + heapApi[5] = reinterpret_cast(&TestHeapRealign); + heapApi[6] = reinterpret_cast(&TestHeapPosixAlign); + ApplicationHeapRegister_nid_no_patch(heapApi.data()); CheckGuestCalls(); CheckLocinfoAlignment(); CheckCharacterTables(); @@ -263,6 +364,10 @@ int main() { CheckStandardStreams(); CheckWideCaseTranslation(); CheckInputFacetObjects(); + CheckCodecvtWideVtableObject(); + CheckWideNumPutVtableObject(); + CheckFacetGetcat(); + CheckCollateVtableObject(); _Locksyslock_nid_postfix(); _Locksyslock_nid_postfix(); _Unlocksyslock_nid_postfix(); diff --git a/core/libs/tests/GuestMath.cpp b/core/libs/tests/GuestMath.cpp index a6eb081146..c82fef2a8a 100644 --- a/core/libs/tests/GuestMath.cpp +++ b/core/libs/tests/GuestMath.cpp @@ -97,7 +97,11 @@ float APS5_VABI remquof_nid_postfix(float, float, int*); float APS5_VABI fmaf_nid_postfix(float, float, float); long double APS5_VABI ceill_nid_postfix(long double); long double APS5_VABI floorl_nid_postfix(long double); +float APS5_VABI cbrtf_nid_postfix(float); float APS5_VABI tanf_nid_postfix(float); +float APS5_VABI sinf_nid_postfix(float); +float APS5_VABI cosf_nid_postfix(float); +void APS5_VABI sincosf_nid_postfix(float, float*, float*); int APS5_VABI rand_nid_postfix(); void APS5_VABI srand_nid_postfix(unsigned int); int APS5_VABI rand_r_nid_postfix(unsigned int*); @@ -682,6 +686,35 @@ static long double MakeExtended(std::uint64_t mantissa, std::uint16_t signExpone return value; } +static void CheckExpm1ConsoleBits() { + constexpr struct { + std::uint64_t input; + std::uint64_t expected; + int error; + } cases[] = { + {0x0000000000000000ull, 0x0000000000000000ull, 0}, {0x8000000000000000ull, 0x8000000000000000ull, 0}, + {0x3c8fffffffffffffull, 0x3c8fffffffffffffull, 0}, {0x3c90000000000000ull, 0x3c90000000000000ull, 0}, + {0x3fd62e42fefa39efull, 0x3fda827999fcef32ull, 0}, {0x3fd62e4300000000ull, 0x3fda82799b6f2366ull, 0}, + {0x3ff0a2b1ffffffffull, 0x3ffd413c1a24d411ull, 0}, {0x3ff0a2b200000000ull, 0x3ffd413c1a24d414ull, 0}, + {0xbfd0000000000000ull, 0xbfcc5041854df7d4ull, 0}, {0xbfe0000000000000ull, 0xbfd92e9a0720d3ecull, 0}, + {0x3ff0000000000000ull, 0x3ffb7e151628aed2ull, 0}, {0xbff0000000000000ull, 0xbfe43a54e4e98864ull, 0}, + {0x4043687a00000000ull, 0x436fffd8395c0c41ull, 0}, {0xc043687a00000000ull, 0xbff0000000000000ull, 0}, + {0x4049000000000000ull, 0x44719103e4080b45ull, 0}, {0x40862e42fefa39efull, 0x7fefffffffffff2aull, 0}, + {0x40862e42fefa39f0ull, 0x7ff0000000000000ull, ERANGE}, {0xc08f400000000000ull, 0xbff0000000000000ull, 0}, + {0x7fefffffffffffffull, 0x7ff0000000000000ull, ERANGE}, {0xffefffffffffffffull, 0xbff0000000000000ull, 0}, + {0x7ff0000000000000ull, 0x7ff0000000000000ull, 0}, {0xfff0000000000000ull, 0xbff0000000000000ull, 0}, + {0x7ff8000000000001ull, 0x7ff8000000000001ull, 0}, {0xfff8000000000002ull, 0xfff8000000000002ull, 0}, + {0x3fc78b9e474afed8ull, 0x3fc9d99e60806b35ull, 0}, {0xbf887a99bea50fb1ull, 0xbf88554c5b9d0cbbull, 0}, + {0x3ff790fb0ae85390ull, 0x400ae5103402894bull, 0}, {0xbf840fe48e07e54dull, 0xbf83f6d1bcda4943ull, 0}, + {0xbfbb1276066a31c7ull, 0xbfb9b098289dab68ull, 0}, {0x3fc07bc20a0c887eull, 0x3fc197828317c86cull, 0}, + }; + for (const auto& item : cases) { + errno = 1234; + Require(std::bit_cast(expm1_nid_postfix(std::bit_cast(item.input))) == item.expected); + Require(errno == (item.error != 0 ? item.error : 1234)); + } +} + static void CheckAsinl() { const struct { std::uint64_t inputMantissa; std::uint16_t inputSignExponent; std::uint64_t outputMantissa; std::uint16_t outputSignExponent; } cases[] = { {0, 0x0000, 0, 0x0000}, {0, 0x8000, 0, 0x8000}, @@ -716,6 +749,21 @@ static void CheckAsinl() { *__error_nid_postfix() = 0; } +static void CheckCbrtfConsoleBits() { + constexpr std::uint32_t cases[][2] = { + {0x00000123u, 0x28059605u}, {0x00400327u, 0x2a4b334bu}, {0x3f000555u, 0x3f4b32c7u}, {0x3fc00778u, 0x3f9287e5u}, + {0x42000999u, 0x404b3509u}, {0x7e000bbcu, 0x544b362au}, {0xbf400dddu, 0xbf689d01u}, {0x80201002u, 0xaa215ff8u}, + {0x00000000u, 0x00000000u}, {0x80000000u, 0x80000000u}, {0x00000001u, 0x26a14518u}, {0x007fffffu, 0x2a7fffffu}, + {0x00800000u, 0x2a800000u}, {0x3f800000u, 0x3f800000u}, {0x41000000u, 0x40000000u}, {0xc1000000u, 0xc0000000u}, + {0x7f7fffffu, 0x54cb2ff5u}, {0x7f800000u, 0x7f800000u}, {0xff800000u, 0xff800000u}, {0x7fc00001u, 0x7fc00001u}, + {0xffc00002u, 0xffc00002u}, + }; + for (const auto& [input, expected] : cases) { + errno = 1234; + Require(std::bit_cast(cbrtf_nid_postfix(std::bit_cast(input))) == expected && errno == 1234); + } +} + static void CheckIntegerConversions() { for (const long long numerator : {4294967301LL, -4294967301LL}) { for (const long long denominator : {3LL, -3LL}) { @@ -897,6 +945,42 @@ static void CheckAsin() { Require(!std::fetestexcept(FE_INVALID)); } +static void CheckTrigfConsoleBits() { + constexpr struct { + std::uint32_t input; + std::uint32_t sine; + std::uint32_t cosine; + std::uint32_t tangent; + int error; + } cases[] = { + {0x3e801d5au, 0x3e7d9057u, 0x3f780703u, 0x3e82db72u, 0}, {0x3fc00321u, 0x3f7f5c46u, 0x3d90acbbu, 0x4161edb0u, 0}, + {0x40400556u, 0x3e102d3eu, 0xbf7d7328u, 0xbe11a09du, 0}, {0x40c00781u, 0xbe8e9c41u, 0x3f75de78u, 0xbe947c92u, 0}, + {0x412009acu, 0xbf0bc6b7u, 0xbf56790du, 0x3f26d71eu, 0}, {0x47000bbbu, 0x3eb0df2fu, 0x3f703d5au, 0x3ebc799eu, 0}, + {0x4e000de0u, 0xbf664722u, 0x3edfade4u, 0xc003c6abu, 0}, {0x5f001006u, 0xbe0ecf11u, 0x3f7d7f8cu, 0xbe1037deu, 0}, + {0x7e001112u, 0x3eabd0cbu, 0x3f7127beu, 0x3eb6645du, 0}, {0xc0400556u, 0xbe102d3eu, 0xbf7d7328u, 0x3e11a09du, 0}, + {0x00000000u, 0x00000000u, 0x3f800000u, 0x00000000u, 0}, {0x80000000u, 0x80000000u, 0x3f800000u, 0x80000000u, 0}, + {0x39000000u, 0x39000000u, 0x3f800000u, 0x39000000u, 0}, {0x3f490fdbu, 0x3f3504f3u, 0x3f3504f3u, 0x3f800000u, 0}, + {0x40490fdbu, 0xb3bbbd2eu, 0xbf800000u, 0x33bbbd2fu, 0}, {0x4dc90fdbu, 0xbf3d5f98u, 0x3f2c42b6u, 0xbf8cb742u, 0}, + {0x7f7fffffu, 0xbf0599b3u, 0x3f5a5f96u, 0xbf1c9ecau, 0}, {0xff7fffffu, 0x3f0599b3u, 0x3f5a5f96u, 0x3f1c9ecau, 0}, + {0x7f800000u, 0xffc00000u, 0xffc00000u, 0xffc00000u, EDOM}, {0xff800000u, 0xffc00000u, 0xffc00000u, 0xffc00000u, EDOM}, + {0x7fc00001u, 0x7fc00001u, 0x7fc00001u, 0x7fc00001u, 0}, + }; + for (const auto& item : cases) { + const float x = std::bit_cast(item.input); + const int expected = item.error != 0 ? item.error : 1234; + errno = 1234; + Require(std::bit_cast(sinf_nid_postfix(x)) == item.sine && errno == expected); + errno = 1234; + Require(std::bit_cast(cosf_nid_postfix(x)) == item.cosine && errno == expected); + errno = 1234; + Require(std::bit_cast(tanf_nid_postfix(x)) == item.tangent && errno == expected); + float sine = 0.0f; + float cosine = 0.0f; + sincosf_nid_postfix(x, &sine, &cosine); + Require(std::bit_cast(sine) == item.sine && std::bit_cast(cosine) == item.cosine); + } +} + static void CheckExtendedMath() { Require(ExtendedBits(atanl_nid_postfix(INFINITY), 0xc90fdaa22168c235ull, 0x3fff)); Require(std::fabs(atanl_nid_postfix(1.0L) - 0.785398163397448309616L) < 1e-18L); @@ -961,6 +1045,21 @@ static void CheckHyperbolic() { Require(Near(acosh_nid_postfix(1e300), 691.4686750787736, 1e-15)); } +static void CheckAcoshfMsunBoundaries() { + Require(acoshf_nid_postfix(1.0f) == 0.0f && !std::signbit(acoshf_nid_postfix(1.0f))); + errno = EINTR; + std::feclearexcept(FE_ALL_EXCEPT); + Require(std::isnan(acoshf_nid_postfix(0.5f)) && errno == EINTR && (std::fetestexcept(FE_INVALID) & FE_INVALID) != 0); + for (const std::uint32_t bits : {0x3f800001u, 0x3fffffffu, 0x40000000u, 0x40000001u, + 0x4d7fffffu, 0x4d800000u, 0x4d800001u, 0x7f7fffffu}) { + const float x = std::bit_cast(bits); + Require(Near(acoshf_nid_postfix(x), std::acosh(static_cast(x)), 1e-6)); + } + Require(std::isinf(acoshf_nid_postfix(INFINITY)) && acoshf_nid_postfix(INFINITY) > 0.0f); + Require(std::isnan(acoshf_nid_postfix(-INFINITY))); + Require(std::isnan(acoshf_nid_postfix(std::numeric_limits::quiet_NaN()))); +} + static void CheckCoshfMsunBoundaries() { static constexpr struct { std::uint32_t input; @@ -1467,7 +1566,9 @@ int main() { CheckMissingClassificationExports(); CheckFloatMathExports(); CheckIntegerConversions(); + CheckCbrtfConsoleBits(); CheckExtendedMath(); + CheckTrigfConsoleBits(); Require(atof_nid_postfix(" -12.5tail") == -12.5); char* end = nullptr; const char input[] = "0x1.8p+2 remainder"; @@ -1622,6 +1723,7 @@ int main() { CheckFegetenv(); CheckLongDouble(); CheckNearbyintl(); + CheckExpm1ConsoleBits(); CheckAsinl(); Require(scalbn_nid_postfix(0.75, -2) == 0.1875); Require(scalbnf_nid_postfix(0.75f, 4) == 12.f); @@ -1744,6 +1846,7 @@ int main() { Require(std::isinf(__powidf2_nid_postfix(-0.0, -3)) && std::signbit(__powidf2_nid_postfix(-0.0, -3))); Require(__powidf2_nid_postfix(2.0, -127) == 0x1p-127); CheckHyperbolic(); + CheckAcoshfMsunBoundaries(); CheckCoshfMsunBoundaries(); CheckScaledHyperbolic(); CheckSinhf(); diff --git a/core/libs/tests/GuestMemory.cpp b/core/libs/tests/GuestMemory.cpp index 2a6571e721..bb7246740c 100644 --- a/core/libs/tests/GuestMemory.cpp +++ b/core/libs/tests/GuestMemory.cpp @@ -500,6 +500,23 @@ static void CheckReleaseFindsEveryDirectMapping() { for (std::size_t index = 0; index < pieces; ++index) Require(sceKernelVirtualQuery(mapped[index], 0, &info, sizeof(info)) != 0); } +static void CheckReleaseSkipsFreePages() { + constexpr std::size_t page = 0x4000; + std::int64_t phys = 0; + Require(sceKernelAllocateDirectMemory(0, 0x7fffffffffll, page * 4, 0, 0, &phys) == 0); + void* mapped = nullptr; + Require(sceKernelMapDirectMemory(&mapped, page * 4, 3, 0, phys, 0) == 0); + Require(sceKernelReleaseDirectMemory(phys + static_cast(page), page) == 0); + Require(sceKernelReleaseDirectMemory(phys + static_cast(page), page) == 0); + Require(sceKernelReleaseDirectMemory(phys, page * 3) == 0); + VirtualQueryInfo info{}; + Require(sceKernelVirtualQuery(mapped, 0, &info, sizeof(info)) != 0); + Require(sceKernelVirtualQuery(static_cast(mapped) + page * 3, 0, &info, sizeof(info)) == 0); + Require(sceKernelCheckedReleaseDirectMemory(phys, page) == SCE_KERNEL_ERROR_ENOENT); + Require(sceKernelCheckedReleaseDirectMemory(phys + static_cast(page * 3), page) == 0); + Require(sceKernelReleaseDirectMemory(phys, page * 4) == 0); +} + static void CheckCheckedReleaseDirectMemory() { constexpr std::size_t page = 0x4000; std::int64_t phys = 0; @@ -2488,7 +2505,7 @@ static void CheckReadsIntoSharedWriteTracking() { KernelAioRwRequest request{static_cast(page), page, bytes, &result, fd}; std::int32_t id = 0; std::int32_t state = 0; - Require(sceKernelAioSubmitReadCommands(&request, 1, 0, &id) == 0); + Require(sceKernelAioSubmitReadCommands(&request, 1, 1, &id) == 0); Require(sceKernelAioWaitRequest(id, &state, nullptr) == 0); Require(result.state == 3 && result.return_value == static_cast(page)); Require(std::memcmp(bytes, seed.data() + page, page) == 0); @@ -2499,7 +2516,7 @@ static void CheckReadsIntoSharedWriteTracking() { Require(sceKernelPread(fd, mapped, page, 0) == static_cast(SCE_KERNEL_ERROR_EFAULT)); KernelAioResult refusedResult{-1, 0}; KernelAioRwRequest refusedRequest{0, page, bytes, &refusedResult, fd}; - Require(sceKernelAioSubmitReadCommands(&refusedRequest, 1, 0, &id) == 0); + Require(sceKernelAioSubmitReadCommands(&refusedRequest, 1, 1, &id) == 0); Require(sceKernelAioWaitRequest(id, &state, nullptr) == 0); Require(refusedResult.return_value == static_cast(SCE_KERNEL_ERROR_EFAULT)); Require(sceKernelAioDeleteRequest(id, &deleted) == 0); @@ -3264,12 +3281,43 @@ static void CheckAvailableDirectMemoryIsLargestFreeBlock() { Require(start == top + 0x200000 && size == 0x400000); Require(sceKernelAvailableDirectMemorySize(top, top + window, 0x1000000, &start, &size) == 0); Require(start == top + 0x1000000 && size == static_cast(window - 0x1000000)); - Require(sceKernelAvailableDirectMemorySize(top, top + 0x200000, 0, &start, &size) == SCE_KERNEL_ERROR_ENOMEM); - Require(start == 0 && size == 0); - Require(sceKernelAvailableDirectMemorySize(top + window, top, 0, &start, &size) == SCE_KERNEL_ERROR_EAGAIN); - Require(sceKernelAvailableDirectMemorySize(-1, top + window, 0, &start, &size) == SCE_KERNEL_ERROR_EAGAIN); - Require(sceKernelAvailableDirectMemorySize(top, top + window, std::numeric_limits::max(), &start, &size) == SCE_KERNEL_ERROR_EAGAIN); - Require(sceKernelAvailableDirectMemorySize(top, top + window, 0, nullptr, &size) == SCE_KERNEL_ERROR_EINVAL); + const std::int64_t run = top + 0x800000; + const auto untouched = [&](std::int64_t searchStart, std::int64_t searchEnd, std::size_t alignment, int expected) { + start = -7; + size = 7; + return sceKernelAvailableDirectMemorySize(searchStart, searchEnd, alignment, &start, &size) == expected && start == -7 && size == 7; + }; + Require(untouched(top, top + 0x200000, 0, SCE_KERNEL_ERROR_ENOMEM)); + Require(untouched(top + window, top, 0, SCE_KERNEL_ERROR_ENOMEM)); + Require(untouched(run, run, 0, SCE_KERNEL_ERROR_ENOMEM)); + Require(untouched(0, -1, 0, SCE_KERNEL_ERROR_ENOMEM)); + Require(untouched(run + 0x1000, run + 0x3000, 0, SCE_KERNEL_ERROR_ENOMEM)); + Require(untouched(static_cast(DIRECT_MEMORY_SIZE), std::numeric_limits::max(), 0, SCE_KERNEL_ERROR_ENOMEM)); + Require(untouched(std::numeric_limits::max() - 0x3ffe, std::numeric_limits::max(), 0, SCE_KERNEL_ERROR_ENOMEM)); + Require(untouched(std::numeric_limits::max(), run + 0x100000, 0, SCE_KERNEL_ERROR_ENOMEM)); + Require(untouched(run, std::numeric_limits::max(), std::size_t{1} << 62, SCE_KERNEL_ERROR_ENOMEM)); + for (const std::size_t alignment : {std::size_t{3}, std::size_t{0x6000}, std::size_t{0xc000}, std::size_t{0x14000}, ~std::size_t{0x3fff}, std::numeric_limits::max()}) { + Require(untouched(top, top + window, alignment, SCE_KERNEL_ERROR_EINVAL)); + Require(untouched(top + window, top, alignment, SCE_KERNEL_ERROR_EINVAL)); + } + Require(sceKernelAvailableDirectMemorySize(top, top + window, 0, nullptr, &size) == 0 && size == static_cast(window - 0x800000)); + Require(sceKernelAvailableDirectMemorySize(top, top + window, 0, &start, nullptr) == 0 && start == run); + Require(sceKernelAvailableDirectMemorySize(top, top + window, 0, nullptr, nullptr) == 0); + Require(sceKernelAvailableDirectMemorySize(top, top + window, 3, nullptr, nullptr) == SCE_KERNEL_ERROR_EINVAL); + Require(sceKernelAvailableDirectMemorySize(-1, top + window, 0, &start, &size) == 0 && start >= 0 && static_cast(size) <= top + window - start); + Require(sceKernelAvailableDirectMemorySize(std::numeric_limits::min(), top + window, 0, &start, &size) == 0 && start >= 0); + Require(sceKernelAvailableDirectMemorySize(run, run + 0xfffff, 0, &start, &size) == 0 && start == run && size == 0xfffff); + Require(sceKernelAvailableDirectMemorySize(run, run + 0x3fff, 0, &start, &size) == 0 && start == run && size == 0x3fff); + Require(sceKernelAvailableDirectMemorySize(run + 1, run + 0x100000, 0, &start, &size) == 0 && start == run + 0x4000 && size == 0xfc000); + Require(sceKernelAvailableDirectMemorySize(run + 0x2000, run + 0x6000, 0, &start, &size) == 0 && start == run + 0x4000 && size == 0x2000); + Require(sceKernelAvailableDirectMemorySize(run + 0x2000, run + 0x10e000, 1, &start, &size) == 0 && start == run + 0x4000 && size == 0x10a000); + Require(sceKernelAvailableDirectMemorySize(run + 0x4000, run + 0x100000, 0x8000, &start, &size) == 0 && start == run + 0x8000 && size == 0xf8000); + Require(sceKernelAvailableDirectMemorySize(top, top + window, std::size_t{1} << 63, &start, &size) == 0); + Require(start == std::numeric_limits::min() && size == static_cast(top + window) - (std::size_t{1} << 63)); + std::int64_t divider = -1; + Require(sceKernelAllocateDirectMemory(run + 0x40000, run + 0x44000, 0x4000, 0, 0, ÷r) == 0 && divider == run + 0x40000); + Require(sceKernelAvailableDirectMemorySize(run, run + 0x84000, 0, &start, &size) == 0 && start == run && size == 0x40000); + Require(sceKernelReleaseDirectMemory(divider, 0x4000) == 0); Require(sceKernelReleaseDirectMemory(first, 0x200000) == 0); Require(sceKernelReleaseDirectMemory(last, 0x200000) == 0); @@ -3330,6 +3378,7 @@ int main(int argc, char** argv) { CheckGuestModuleUsesFlexibleMemory(); CheckBatchMapStopsAtInvalidEntry(); CheckCheckedReleaseDirectMemory(); + CheckReleaseSkipsFreePages(); CheckAudioCoprocessorProtection(); CheckMprotectIgnoresUndefinedBits(); CheckStrictMemoryProtectionBits(); diff --git a/core/libs/tests/GuestModuleInfo.cpp b/core/libs/tests/GuestModuleInfo.cpp index d596bf0280..a9419b03f3 100644 --- a/core/libs/tests/GuestModuleInfo.cpp +++ b/core/libs/tests/GuestModuleInfo.cpp @@ -41,7 +41,7 @@ static bool ThrowsInvalidArgument(TFunction function) { static ModuleInfoEx Query(const void* address, int expected) { ModuleInfoEx info{}; info.st_size = sizeof(ModuleInfoEx); - Require(sceKernelGetModuleInfoFromAddr(reinterpret_cast(address), 2, &info) == expected); + Require(sceKernelGetModuleInfoFromAddr(reinterpret_cast(address), 1, &info) == expected); return info; } int main(int argc, char** argv) { @@ -51,7 +51,7 @@ int main(int argc, char** argv) { const void* add = dlsym_nid_postfix(module, "GuestModuleAdd"); Require(add != nullptr); const auto info = Query(add, 0); - Require(info.st_size == sizeof(ModuleInfoEx)); + Require(info.st_size == 0); Require(info.id == static_cast(reinterpret_cast(module))); Require(std::string(info.name) == std::string(argv[1]).substr(std::string(argv[1]).find_last_of("/\\") + 1)); Require(info.segment_count > 0 && info.segment_count <= 4 && info.ref_count == 1); @@ -169,10 +169,34 @@ int main(int argc, char** argv) { Require(sceKernelGetModuleInfoFromAddr(reinterpret_cast(add), 2, nullptr) == SCE_KERNEL_ERROR_EFAULT); ModuleInfoEx invalid{}; invalid.st_size = sizeof(ModuleInfoEx); - Require(ThrowsInvalidArgument([&] { sceKernelGetModuleInfoFromAddr(reinterpret_cast(add), 1, &invalid); })); + for (const int flags : {-1, 0, 3, 4, 8}) { + std::memset(&invalid, 0xbb, sizeof(invalid)); + Require(sceKernelGetModuleInfoFromAddr(reinterpret_cast(add), flags, &invalid) == SCE_KERNEL_ERROR_EINVAL); + Require(std::all_of(reinterpret_cast(&invalid), reinterpret_cast(&invalid + 1), [](unsigned char value) { return value == 0; })); + } + Require(ThrowsInvalidArgument([&] { sceKernelGetModuleInfoFromAddr(reinterpret_cast(add), 0, nullptr); })); + std::vector everyModule(64); + std::vector titleOnlyModules(64); + std::size_t everyCount = 0; + std::size_t titleOnlyCount = 0; + Require(sceKernelGetModuleList(everyModule.data(), everyModule.size(), &everyCount) == 0); + Require(sceKernelGetModuleList2(titleOnlyModules.data(), titleOnlyModules.size(), &titleOnlyCount) == 0); + const auto systemModule = std::find_if(everyModule.begin(), everyModule.begin() + static_cast(everyCount), + [&](KernelModule handle) { return std::find(titleOnlyModules.begin(), titleOnlyModules.begin() + static_cast(titleOnlyCount), handle) == titleOnlyModules.begin() + static_cast(titleOnlyCount); }); + Require(systemModule != everyModule.begin() + static_cast(everyCount)); + ModuleInfo systemInfo{}; + systemInfo.st_size = sizeof(ModuleInfo); + Require(sceKernelGetModuleInfo(*systemModule, &systemInfo) == 0 && systemInfo.segment_count > 0); + const auto systemAddress = static_cast(systemInfo.segments[0].address); + ModuleInfoEx anyModule{}; + Require(sceKernelGetModuleInfoFromAddr(reinterpret_cast(&Query), 2, &anyModule) == 0 && anyModule.segment_count > 0 && anyModule.st_size == 0); + Require(sceKernelGetModuleInfoFromAddr(systemAddress, 1, &anyModule) == 0 && anyModule.id == *systemModule); + std::memset(&anyModule, 0xbb, sizeof(anyModule)); + Require(sceKernelGetModuleInfoFromAddr(systemAddress, 2, &anyModule) == SCE_KERNEL_ERROR_ESRCH && anyModule.segment_count == 0 && anyModule.name[0] == 0); + Require(sceKernelGetModuleInfoFromAddr(reinterpret_cast(add), 1, &anyModule) == 0 && anyModule.id == info.id); ModuleInfoEx unset{}; unset.st_size = 0xf0000; - Require(sceKernelGetModuleInfoFromAddr(reinterpret_cast(add), 2, &unset) == 0 && unset.st_size == sizeof(ModuleInfoEx)); + Require(sceKernelGetModuleInfoFromAddr(reinterpret_cast(add), 1, &unset) == 0 && unset.st_size == 0); const auto again = Query(add, 0); Require(again.eh_frame_hdr_addr == info.eh_frame_hdr_addr && again.eh_frame_hdr_size == info.eh_frame_hdr_size); Require(again.eh_frame_addr == info.eh_frame_addr && again.eh_frame_size == info.eh_frame_size); diff --git a/core/libs/tests/GuestMsync.cpp b/core/libs/tests/GuestMsync.cpp index 451dd44583..1f23ef02ad 100644 --- a/core/libs/tests/GuestMsync.cpp +++ b/core/libs/tests/GuestMsync.cpp @@ -4,7 +4,6 @@ #include #include #include -#include extern "C" { void* APS5_VABI mmap_nid_postfix(void*, std::size_t, int, int, int, std::int64_t) noexcept; @@ -23,6 +22,7 @@ constexpr int Async = 0x1; constexpr int Invalidate = 0x2; constexpr int Invalid = 22; constexpr int NoMemory = 12; +constexpr int Busy = 16; void Require(bool condition, std::source_location location = std::source_location::current()) { if (condition) return; @@ -40,15 +40,6 @@ bool Fails(void* address, std::size_t length, int flags, int error) { return msync_nid_postfix(address, length, flags) == -1 && *__error_nid_postfix() == error; } -bool Throws(void* address, std::size_t length, int flags) { - try { - msync_nid_postfix(address, length, flags); - } catch (const std::runtime_error&) { - return true; - } - return false; -} - char* Map(std::size_t pages) { void* mapped = mmap_nid_postfix(nullptr, pages * Page, ReadWrite, PrivateAnonymous, -1, 0); Require(mapped != reinterpret_cast(static_cast(-1))); @@ -69,7 +60,11 @@ int main() { Require(Fails(mapped, Page, Async | Invalidate, Invalid)); const auto top = std::numeric_limits::max() & ~(Page - 1); Require(Fails(reinterpret_cast(top), 2 * Page, Sync, Invalid)); - Require(Throws(mapped, Page, Invalidate)); + Require(Fails(mapped, Page, Invalidate, Busy)); + Require(Fails(mapped, 3 * Page, Sync | Invalidate, Busy)); + Require(Fails(mapped + 1, 16, Invalidate, Busy)); + Require(Fails(mapped, 0, Invalidate, Busy)); + Require(Succeeds(mapped, Page, 0x8)); Require(munmap_nid_postfix(mapped, 3 * Page) == 0); Require(Fails(mapped, Page, Sync, NoMemory)); diff --git a/core/libs/tests/GuestNamedShmIoDescriptorReuse.cpp b/core/libs/tests/GuestNamedShmIoDescriptorReuse.cpp index 368fb3cc60..8de714aa88 100644 --- a/core/libs/tests/GuestNamedShmIoDescriptorReuse.cpp +++ b/core/libs/tests/GuestNamedShmIoDescriptorReuse.cpp @@ -115,6 +115,9 @@ extern "C" ssize_t readlink(const char* path, char* buffer, std::size_t length) } return result; } +extern "C" ssize_t __readlink_chk(const char* path, char* buffer, std::size_t length, std::size_t) { + return readlink(path, buffer, length); +} extern "C" int flock(int descriptor, int operation) { const int result = static_cast(::syscall(SYS_flock, descriptor, operation)); if (result == 0 && operation == LOCK_EX && armed && !replaceAtProbe && !replaceBeforeProbe && !swapped) { diff --git a/core/libs/tests/GuestNamedShmWindows.cpp b/core/libs/tests/GuestNamedShmWindows.cpp new file mode 100644 index 0000000000..f159ac1ee8 --- /dev/null +++ b/core/libs/tests/GuestNamedShmWindows.cpp @@ -0,0 +1,625 @@ +#include "prx/libc/include/general/VabiMacros.hpp" +#include "prx/libkernel/File/include/SharedMemory.hpp" +#include "nid/Sha1.hpp" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +extern "C" { +int APS5_VABI shm_open_nid_postfix(const char*, int, int); +int APS5_VABI shm_unlink_nid_postfix(const char*); +int APS5_VABI close_nid_postfix(int); +int APS5_VABI dup_nid_postfix(int); +int APS5_VABI dup2_nid_postfix(int, int); +std::int64_t APS5_VABI lseek_nid_postfix(int, std::int64_t, int); +int APS5_VABI ftruncate_nid_postfix(int, std::int64_t); +int APS5_VABI fchmod_nid_postfix(int, int); +std::int64_t APS5_VABI pread_nid_postfix(int, void*, std::size_t, std::int64_t); +std::int64_t APS5_VABI pwrite_nid_disambig1_nid_postfix(int, const void*, std::size_t, std::int64_t); +int* APS5_VABI __error_nid_postfix(); +} + +namespace { +constexpr int ReadOnly = 0; +constexpr int ReadWrite = 2; +constexpr int Create = 0x200; +constexpr int Exclusive = 0x800; +constexpr std::int64_t RaceSize = 4096; + +void Require(bool condition, const char* message) { + if (!condition) throw std::runtime_error(std::string(message) + " (guest errno=" + std::to_string(*__error_nid_postfix()) + ")"); +} + +void RequireError(int descriptor, int expected, const char* message) { + Require(descriptor == -1 && *__error_nid_postfix() == expected, message); +} + +void RequireNoAdministratorsAccess(HANDLE handle) { + PACL dacl = nullptr; + PSECURITY_DESCRIPTOR descriptor = nullptr; + const DWORD result = ::GetSecurityInfo(handle, SE_FILE_OBJECT, DACL_SECURITY_INFORMATION, + nullptr, nullptr, &dacl, nullptr, &descriptor); + if (result != ERROR_SUCCESS || dacl == nullptr) { + if (descriptor != nullptr) ::LocalFree(descriptor); + Require(false, "reading named shared-memory object permissions failed"); + } + SID_IDENTIFIER_AUTHORITY authority = SECURITY_NT_AUTHORITY; + PSID administrators = nullptr; + if (::AllocateAndInitializeSid(&authority, 2, SECURITY_BUILTIN_DOMAIN_RID, + DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &administrators) == FALSE) { + ::LocalFree(descriptor); + Require(false, "building the Administrators SID failed"); + } + ACL_SIZE_INFORMATION information{}; + const BOOL gotInformation = ::GetAclInformation(dacl, &information, sizeof(information), AclSizeInformation); + bool grantsAdministrators = false; + if (gotInformation) { + for (DWORD index = 0; index < information.AceCount; ++index) { + void* rawAce = nullptr; + if (!::GetAce(dacl, index, &rawAce)) continue; + const auto* header = static_cast(rawAce); + if (header->AceType != ACCESS_ALLOWED_ACE_TYPE) continue; + const auto* ace = static_cast(rawAce); + if (::EqualSid(const_cast(&ace->SidStart), administrators) != FALSE) { + grantsAdministrators = true; + break; + } + } + } + ::FreeSid(administrators); + ::LocalFree(descriptor); + Require(gotInformation && !grantsAdministrators, + "named shared-memory object grants access to the Administrators group"); +} + +void RequireNoUnlinkedFiles(const std::string& name) { + const auto digest = Nid::Sha1(std::vector(name.begin(), name.end())); + std::string prefix; + for (const auto byte : digest) { + prefix += "0123456789abcdef"[byte >> 4]; + prefix += "0123456789abcdef"[byte & 15]; + } + const auto directory = std::filesystem::temp_directory_path() / L"AnyPS5" / L"shm"; + if (!std::filesystem::exists(directory)) return; + for (const auto& entry : std::filesystem::directory_iterator(directory)) { + const auto filename = entry.path().filename().string(); + Require(!filename.starts_with(prefix + "-unlinked-"), + "closing the last descriptor did not reap its unlinked host object"); + } +} + +void VerifyUntrackedDescriptor(int descriptor) { + Require(File::IsSharedMemoryDescriptor_nid_no_patch(descriptor), "named descriptor was not tracked"); + std::vector path(32768); + const DWORD length = ::GetFinalPathNameByHandleW(reinterpret_cast(::_get_osfhandle(descriptor)), + path.data(), static_cast(path.size()), FILE_NAME_NORMALIZED | VOLUME_NAME_DOS); + Require(length > 0 && length < path.size(), "locating the tracked file failed"); + const HANDLE handle = ::CreateFileW(path.data(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, + nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr); + Require(handle != INVALID_HANDLE_VALUE, "opening an untracked handle to the named file failed"); + const int untracked = ::_open_osfhandle(reinterpret_cast(handle), _O_RDONLY | _O_BINARY); + if (untracked < 0) ::CloseHandle(handle); + Require(untracked >= 0, "creating an untracked descriptor failed"); + const bool classified = File::IsSharedMemoryDescriptor_nid_no_patch(untracked); + Require(::_close(untracked) == 0, "closing the untracked descriptor failed"); + Require(!classified, "an untracked descriptor entered the named shared-memory path"); + Require(!File::IsSharedMemoryDescriptor_nid_no_patch(untracked), "a closed untracked descriptor was classified as shared memory"); +} + +void VerifyConcurrentDescriptorReuse(const std::string& name) { + std::barrier start(3); + std::vector okay(2, 1); + std::vector workers; + for (int index = 0; index < 2; ++index) { + workers.emplace_back([&, index] { + start.arrive_and_wait(); + for (int iteration = 0; iteration < 64; ++iteration) { + const int descriptor = shm_open_nid_postfix(name.c_str(), ReadWrite, 0); + if (descriptor < 0) { okay[index] = 0; break; } + if (!File::IsSharedMemoryDescriptor_nid_no_patch(descriptor) || + pwrite_nid_disambig1_nid_postfix(descriptor, "x", 1, 64) != 0) okay[index] = 0; + if (close_nid_postfix(descriptor) != 0) okay[index] = 0; + } + }); + } + start.arrive_and_wait(); + for (auto& worker : workers) worker.join(); + Require(okay[0] && okay[1], "concurrent close/open lost named-descriptor tracking or bounded writes"); +} + +bool RunResizeWriteRace(const std::string& name) { + const int descriptor = shm_open_nid_postfix(name.c_str(), ReadWrite, 0); + if (descriptor < 0) return false; + bool okay = true; + for (int iteration = 0; iteration < 300; ++iteration) { + const auto size = (iteration & 1) ? 0 : RaceSize; + if (ftruncate_nid_postfix(descriptor, size) != 0) { okay = false; break; } + if (pwrite_nid_disambig1_nid_postfix(descriptor, "x", 1, RaceSize / 2) < 0) { okay = false; break; } + struct _stat64 status{}; + if (::_fstat64(descriptor, &status) != 0 || (status.st_size != 0 && status.st_size != RaceSize)) { + okay = false; + break; + } + } + close_nid_postfix(descriptor); + return okay; +} + +void VerifyCrossProcessResizeWrite(const std::string& name, int descriptor) { + wchar_t executable[32768]{}; + const DWORD length = ::GetModuleFileNameW(nullptr, executable, static_cast(std::size(executable))); + Require(length > 0 && length < std::size(executable), "locating the Windows shared-memory test executable failed"); + std::vector workers; + for (int index = 0; index < 2; ++index) { + std::wstring command = L"\"" + std::wstring(executable, length) + L"\" --race " + std::wstring(name.begin(), name.end()); + STARTUPINFOW startup{}; + startup.cb = sizeof(startup); + PROCESS_INFORMATION process{}; + Require(::CreateProcessW(nullptr, command.data(), nullptr, nullptr, FALSE, CREATE_NO_WINDOW, + nullptr, nullptr, &startup, &process) != FALSE, + "starting a cross-process shared-memory race worker failed"); + workers.push_back(process); + } + const bool localSucceeded = RunResizeWriteRace(name); + bool childrenSucceeded = true; + for (const auto& process : workers) { + childrenSucceeded = childrenSucceeded && ::WaitForSingleObject(process.hProcess, 10000) == WAIT_OBJECT_0; + DWORD exitCode = 1; + childrenSucceeded = childrenSucceeded && ::GetExitCodeProcess(process.hProcess, &exitCode) && exitCode == 0; + ::CloseHandle(process.hThread); + ::CloseHandle(process.hProcess); + } + Require(localSucceeded && childrenSucceeded, + "cross-process truncate/write race produced an invalid shared-object size"); + struct _stat64 status{}; + Require(::_fstat64(descriptor, &status) == 0 && (status.st_size == 0 || status.st_size == RaceSize), + "cross-process truncate/write race left an invalid final size"); + Require(shm_unlink_nid_postfix(name.c_str()) == 0, "cross-process race cleanup failed"); +} + +void VerifyResizeReadPosition(const std::string& name) { + constexpr std::size_t payloadSize = 2u << 20; + const int descriptor = shm_open_nid_postfix(name.c_str(), ReadWrite | Create | Exclusive, 0600); + Require(descriptor >= 0 && ftruncate_nid_postfix(descriptor, payloadSize) == 0, + "creating the resize/read object failed"); + const int resizer = dup_nid_postfix(descriptor); + Require(resizer >= 0, "duplicating the resize/read descriptor failed"); + std::vector payload(payloadSize, 'r'); + Require(pwrite_nid_disambig1_nid_postfix(descriptor, payload.data(), payload.size(), 0) == payloadSize && + lseek_nid_postfix(descriptor, 137, 0) == 137, "preparing the resize/read object failed"); + std::barrier start(2); + bool resized = true; + std::thread resizing([&] { + start.arrive_and_wait(); + for (int iteration = 0; iteration < 256; ++iteration) + if (ftruncate_nid_postfix(resizer, payloadSize + ((iteration & 1) ? 4096 : 0)) != 0) resized = false; + }); + start.arrive_and_wait(); + std::vector output(payloadSize); + bool read = true; + for (int iteration = 0; iteration < 32; ++iteration) + if (pread_nid_postfix(descriptor, output.data(), output.size(), 0) != payloadSize || output != payload) read = false; + resizing.join(); + Require(resized && read && lseek_nid_postfix(descriptor, 0, 1) == 137, + "concurrent resize/read changed the shared file position or contents"); + Require(shm_unlink_nid_postfix(name.c_str()) == 0 && close_nid_postfix(descriptor) == 0 && close_nid_postfix(resizer) == 0, + "resize/read cleanup failed"); + RequireNoUnlinkedFiles(name); +} + +void Run() { + const std::string base = "/anyps5-windows-named-" + std::to_string(::GetCurrentProcessId()) + "-" + std::to_string(::GetTickCount64()); + const std::string name = base + "-lifecycle"; + Require(shm_unlink_nid_postfix(name.c_str()) == -1 && *__error_nid_postfix() == 2, + "new test name unexpectedly existed"); + + *__error_nid_postfix() = 73; + const int original = shm_open_nid_postfix(name.c_str(), ReadWrite | Create | Exclusive, 0600); + Require(original >= 0 && *__error_nid_postfix() == 73, "exclusive creation failed or changed guest errno"); + VerifyUntrackedDescriptor(original); + Require(ftruncate_nid_postfix(original, 64) == 0, "sizing the named object failed"); + VerifyConcurrentDescriptorReuse(name); + const char originalBytes[] = "original"; + Require(pwrite_nid_disambig1_nid_postfix(original, originalBytes, sizeof(originalBytes), 0) == sizeof(originalBytes), + "write to named object failed"); + + *__error_nid_postfix() = 0; + RequireError(shm_open_nid_postfix(name.c_str(), ReadWrite | Create | Exclusive, 0600), 17, + "exclusive creation replaced an existing named object"); + const int reader = shm_open_nid_postfix(name.c_str(), ReadOnly, 0); + char contents[sizeof(originalBytes)]{}; + Require(reader >= 0 && pread_nid_postfix(reader, contents, sizeof(contents), 0) == sizeof(contents) && + std::memcmp(contents, originalBytes, sizeof(contents)) == 0, "reopen did not share the original contents"); + *__error_nid_postfix() = 0; + Require(pwrite_nid_disambig1_nid_postfix(reader, "x", 1, 0) == -1 && *__error_nid_postfix() == 9, + "read-only descriptor accepted a write"); + *__error_nid_postfix() = 0; + Require(pwrite_nid_disambig1_nid_postfix(reader, "x", 1, 64) == -1 && *__error_nid_postfix() == 9, + "read-only descriptor accepted a write at end of file"); + *__error_nid_postfix() = 0; + Require(pwrite_nid_disambig1_nid_postfix(reader, "", 0, 64) == -1 && *__error_nid_postfix() == 9, + "read-only descriptor accepted a zero-length write"); + + Require(shm_unlink_nid_postfix(name.c_str()) == 0, "unlink of named object failed"); + *__error_nid_postfix() = 0; + RequireError(shm_open_nid_postfix(name.c_str(), ReadOnly, 0), 2, "unlinked name remained openable"); + std::memset(contents, 0, sizeof(contents)); + Require(pread_nid_postfix(original, contents, sizeof(contents), 0) == sizeof(contents) && + std::memcmp(contents, originalBytes, sizeof(contents)) == 0, "open descriptor stopped working after unlink"); + const char updatedByte = 'X'; + Require(pwrite_nid_disambig1_nid_postfix(original, &updatedByte, 1, 0) == 1, + "open descriptor stopped accepting writes after unlink"); + + const int replacement = shm_open_nid_postfix(name.c_str(), ReadWrite | Create | Exclusive, 0600); + Require(replacement >= 0, "name could not be recreated while old descriptors remained open"); + Require(ftruncate_nid_postfix(replacement, 64) == 0, "sizing the replacement object failed"); + Require(fchmod_nid_postfix(original, 0400) == 0, + "fchmod failed on the old open object after unlink and name recreation"); + Require(fchmod_nid_postfix(original, 0600) == 0, + "restoring mode on the old open object after name recreation failed"); + std::memset(contents, 0x7f, sizeof(contents)); + Require(pread_nid_postfix(replacement, contents, sizeof(contents), 0) == sizeof(contents) && + std::memcmp(contents, "\0\0\0\0\0\0\0\0", sizeof(contents)) == 0, + "recreated name still referred to the unlinked object"); + Require(pwrite_nid_disambig1_nid_postfix(replacement, "y", 1, 0) == 1, + "fchmod on the old descriptor changed the replacement object's permissions"); + const char replacementBytes[] = "replacement"; + Require(pwrite_nid_disambig1_nid_postfix(replacement, replacementBytes, sizeof(replacementBytes), 0) == sizeof(replacementBytes), + "write to replacement object failed"); + std::memset(contents, 0, sizeof(contents)); + Require(pread_nid_postfix(original, contents, sizeof(contents), 0) == sizeof(contents) && + contents[0] == updatedByte && std::memcmp(contents + 1, originalBytes + 1, sizeof(originalBytes) - 2) == 0, + "replacement write changed the old open object"); + Require(shm_unlink_nid_postfix(name.c_str()) == 0, "replacement unlink failed"); + Require(close_nid_postfix(reader) == 0 && close_nid_postfix(original) == 0 && close_nid_postfix(replacement) == 0, + "closing lifecycle descriptors failed"); + RequireNoUnlinkedFiles(name); + + const std::string duplicateName = base + "-duplicate"; + const int duplicateOriginal = shm_open_nid_postfix(duplicateName.c_str(), ReadWrite | Create | Exclusive, 0600); + Require(duplicateOriginal >= 0 && ftruncate_nid_postfix(duplicateOriginal, 64) == 0, + "creating the duplicate test object failed"); + const int duplicate = dup_nid_postfix(duplicateOriginal); + Require(duplicate >= 0 && pwrite_nid_disambig1_nid_postfix(duplicate, "d", 1, 0) == 1, + "dup lost shared-memory write access"); + const int duplicateReader = shm_open_nid_postfix(duplicateName.c_str(), ReadOnly, 0); + Require(duplicateReader >= 0 && shm_unlink_nid_postfix(duplicateName.c_str()) == 0, + "unlinking the duplicate test object failed"); + Require(close_nid_postfix(duplicateOriginal) == 0, "closing the duplicated original failed"); + Require(dup2_nid_postfix(-1, duplicate) == -1, + "dup2 accepted an invalid source"); + Require(pwrite_nid_disambig1_nid_postfix(duplicate, "e", 1, 0) == 1, + "failed dup2 destroyed its shared-memory target"); + Require(dup2_nid_postfix(duplicateReader, duplicate) == duplicate, + "dup2 could not replace a shared-memory target"); + Require(pwrite_nid_disambig1_nid_postfix(duplicate, "f", 1, 0) == -1 && *__error_nid_postfix() == 9, + "dup2 lost shared-memory read-only access"); + Require(close_nid_postfix(duplicateReader) == 0, "closing the duplicate reader failed"); + const std::string replacementName = base + "-duplicate-replacement"; + const int duplicateReplacement = shm_open_nid_postfix(replacementName.c_str(), ReadWrite | Create | Exclusive, 0600); + Require(duplicateReplacement >= 0 && dup2_nid_postfix(duplicateReplacement, duplicate) == duplicate, + "dup2 could not replace the last unlinked descriptor"); + RequireNoUnlinkedFiles(duplicateName); + Require(shm_unlink_nid_postfix(replacementName.c_str()) == 0 && + close_nid_postfix(duplicateReplacement) == 0 && close_nid_postfix(duplicate) == 0, + "cleaning up dup2 replacement descriptors failed"); + RequireNoUnlinkedFiles(replacementName); + VerifyResizeReadPosition(base + "-resize-read"); + + const std::string concurrentName = base + "-exclusive-race"; + constexpr int contenderCount = 12; + std::vector descriptors(contenderCount, -2); + std::vector errors(contenderCount, 0); + std::vector contenders; + contenders.reserve(contenderCount); + for (int index = 0; index < contenderCount; ++index) { + contenders.emplace_back([&, index] { + descriptors[index] = shm_open_nid_postfix(concurrentName.c_str(), ReadWrite | Create | Exclusive, 0600); + errors[index] = *__error_nid_postfix(); + }); + } + for (auto& contender : contenders) contender.join(); + int created = 0; + for (int index = 0; index < contenderCount; ++index) { + if (descriptors[index] >= 0) { + ++created; + Require(close_nid_postfix(descriptors[index]) == 0, "closing exclusive-race descriptor failed"); + } else { + Require(errors[index] == 17, "exclusive-race loser did not receive EEXIST"); + } + } + Require(created == 1, "exclusive create race produced more or fewer than one winner"); + Require(shm_unlink_nid_postfix(concurrentName.c_str()) == 0, "exclusive-race cleanup failed"); + + const std::string raceName = base + "-cross-process-race"; + const int raceDescriptor = shm_open_nid_postfix(raceName.c_str(), ReadWrite | Create | Exclusive, 0600); + Require(raceDescriptor >= 0 && ftruncate_nid_postfix(raceDescriptor, RaceSize) == 0, + "cross-process race object creation failed"); + VerifyCrossProcessResizeWrite(raceName, raceDescriptor); + Require(close_nid_postfix(raceDescriptor) == 0, "closing cross-process race descriptor failed"); + + const std::string permissionsName = base + "-permissions"; + const int restricted = shm_open_nid_postfix(permissionsName.c_str(), ReadWrite | Create | Exclusive, 0400); + Require(restricted >= 0, "read-only mode creation failed"); + RequireNoAdministratorsAccess(reinterpret_cast(::_get_osfhandle(restricted))); + Require(close_nid_postfix(restricted) == 0, "closing read-only mode object failed"); + *__error_nid_postfix() = 0; + RequireError(shm_open_nid_postfix(permissionsName.c_str(), ReadWrite, 0), 13, + "mode 0400 allowed a later read-write open"); + const int restrictedReader = shm_open_nid_postfix(permissionsName.c_str(), ReadOnly, 0); + Require(restrictedReader >= 0, "mode 0400 rejected a read-only open"); + Require(fchmod_nid_postfix(restrictedReader, 0600) == 0, + "fchmod could not restore write permission on a named object"); + const int restoredWriter = shm_open_nid_postfix(permissionsName.c_str(), ReadWrite, 0); + Require(restoredWriter >= 0, "mode 0600 after fchmod rejected read-write open"); + Require(fchmod_nid_postfix(restoredWriter, 0400) == 0, + "fchmod could not remove write permission on a named object"); + *__error_nid_postfix() = 0; + Require(shm_unlink_nid_postfix(permissionsName.c_str()) == -1 && *__error_nid_postfix() == 13, + "unlink of a read-only named object did not return EACCES"); + Require(fchmod_nid_postfix(restoredWriter, 0600) == 0, + "fchmod could not restore permission before unlink"); + Require(shm_unlink_nid_postfix(permissionsName.c_str()) == 0, + "owner could not unlink a writable named object"); + Require(close_nid_postfix(restoredWriter) == 0, "closing restored writer failed"); + Require(close_nid_postfix(restrictedReader) == 0, "closing restricted reader failed"); + + const std::string noPermissionsName = base + "-mode-zero"; + const int noPermissions = shm_open_nid_postfix(noPermissionsName.c_str(), ReadWrite | Create | Exclusive, 0000); + Require(noPermissions >= 0 && fchmod_nid_postfix(noPermissions, 0600) == 0, + "fchmod could not restore access to a mode 0000 named object"); + Require(shm_unlink_nid_postfix(noPermissionsName.c_str()) == 0 && close_nid_postfix(noPermissions) == 0, + "mode 0000 object cleanup failed"); + + *__error_nid_postfix() = 0; + RequireError(shm_open_nid_postfix(nullptr, ReadWrite, 0), 14, "null name did not return EFAULT"); + *__error_nid_postfix() = 0; + RequireError(shm_open_nid_postfix("relative", ReadWrite, 0), 22, "relative name did not return EINVAL"); + std::puts("Windows named shared-memory lifecycle checks passed"); +} + +bool EnvironmentEntryHasKey(const std::wstring& entry, const wchar_t* key) { + const auto separator = entry.find(L'='); + return separator != std::wstring::npos && separator == std::wcslen(key) && + ::CompareStringOrdinal(entry.data(), static_cast(separator), key, static_cast(separator), TRUE) == CSTR_EQUAL; +} + +std::vector EnvironmentWithTempPath(const std::wstring& path) { + std::vector entries; + LPWCH environment = ::GetEnvironmentStringsW(); + if (environment == nullptr) throw std::runtime_error("reading the Windows environment failed"); + try { + for (const wchar_t* current = environment; *current != L'\0'; current += std::wcslen(current) + 1) { + std::wstring entry(current); + if (!EnvironmentEntryHasKey(entry, L"TMP") && !EnvironmentEntryHasKey(entry, L"TEMP")) + entries.push_back(std::move(entry)); + } + } catch (...) { + ::FreeEnvironmentStringsW(environment); + throw; + } + ::FreeEnvironmentStringsW(environment); + entries.push_back(L"TMP=" + path); + entries.push_back(L"TEMP=" + path); + std::sort(entries.begin(), entries.end(), [](const auto& left, const auto& right) { + return ::CompareStringOrdinal(left.c_str(), -1, right.c_str(), -1, TRUE) == CSTR_LESS_THAN; + }); + std::vector block; + for (const auto& entry : entries) { + block.insert(block.end(), entry.begin(), entry.end()); + block.push_back(L'\0'); + } + block.push_back(L'\0'); + return block; +} + +bool SameDirectory(HANDLE left, HANDLE right) { + BY_HANDLE_FILE_INFORMATION leftInfo{}, rightInfo{}; + return ::GetFileInformationByHandle(left, &leftInfo) && ::GetFileInformationByHandle(right, &rightInfo) && + leftInfo.dwVolumeSerialNumber == rightInfo.dwVolumeSerialNumber && + leftInfo.nFileIndexHigh == rightInfo.nFileIndexHigh && leftInfo.nFileIndexLow == rightInfo.nFileIndexLow; +} + +std::filesystem::path ExtendedPath(const std::filesystem::path& path) { + wchar_t fullPath[32768]{}; + const DWORD length = ::GetFullPathNameW(path.c_str(), static_cast(std::size(fullPath)), fullPath, nullptr); + if (length == 0 || length >= std::size(fullPath)) + throw std::runtime_error("getting the full private temporary path failed"); + std::wstring full(fullPath, length); + if (full.starts_with(L"\\\\?\\")) return full; + if (full.starts_with(L"\\\\")) return L"\\\\?\\UNC\\" + full.substr(2); + return L"\\\\?\\" + full; +} + +std::filesystem::path DosPath(const std::filesystem::path& path) { + const auto& native = path.native(); + if (native.starts_with(L"\\\\?\\UNC\\")) return L"\\\\" + native.substr(8); + if (native.starts_with(L"\\\\?\\")) return native.substr(4); + return path; +} + +void RemovePrivateDirectory(const std::filesystem::path& directory) { + WIN32_FIND_DATAW entry{}; + const HANDLE search = ::FindFirstFileW((directory / L"*").c_str(), &entry); + if (search == INVALID_HANDLE_VALUE) { + if (::GetLastError() != ERROR_FILE_NOT_FOUND) + throw std::runtime_error("enumerating the private temporary directory for cleanup failed"); + } else { + try { + do { + if (std::wcscmp(entry.cFileName, L".") == 0 || std::wcscmp(entry.cFileName, L"..") == 0) continue; + if (entry.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) + throw std::runtime_error("the private temporary directory contains a reparse point"); + const auto child = directory / entry.cFileName; + if (entry.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) RemovePrivateDirectory(child); + else if (!::DeleteFileW(child.c_str())) + throw std::runtime_error("removing a private temporary file failed"); + } while (::FindNextFileW(search, &entry)); + if (::GetLastError() != ERROR_NO_MORE_FILES) + throw std::runtime_error("enumerating the private temporary directory for cleanup failed"); + } catch (...) { + ::FindClose(search); + throw; + } + ::FindClose(search); + } + if (!::RemoveDirectoryW(directory.c_str())) + throw std::runtime_error("removing the private temporary directory failed"); +} + +int RunWithShortTempPath(bool longPath) { + wchar_t tempBuffer[32768]{}; + const DWORD tempLength = ::GetTempPathW(static_cast(std::size(tempBuffer)), tempBuffer); + if (tempLength == 0 || tempLength >= std::size(tempBuffer)) + throw std::runtime_error("locating the Windows temporary directory failed"); + const std::filesystem::path parent(std::wstring(tempBuffer, tempLength)); + std::filesystem::path baseRoot; + bool created = false; + for (unsigned attempt = 0; attempt < 16; ++attempt) { + baseRoot = parent / (L"AnyPS5-Temp-Regression-" + std::to_wstring(::GetCurrentProcessId()) + L"-" + + std::to_wstring(::GetTickCount64()) + L"-" + std::to_wstring(attempt)); + if (::CreateDirectoryW(baseRoot.c_str(), nullptr)) { + created = true; + break; + } + if (::GetLastError() != ERROR_ALREADY_EXISTS) throw std::runtime_error("creating a private temporary directory failed"); + } + if (!created) throw std::runtime_error("creating a unique private temporary directory failed"); + const auto cleanupRoot = ExtendedPath(baseRoot); + std::filesystem::path longRoot = cleanupRoot; + const auto cleanup = [&] { + RemovePrivateDirectory(cleanupRoot); + }; + + try { + std::vector shortBuffer(32768); + DWORD shortLength = ::GetShortPathNameW(longRoot.c_str(), shortBuffer.data(), static_cast(shortBuffer.size())); + if (shortLength == 0) + throw std::runtime_error("GetShortPathNameW failed (Win32 error " + std::to_string(::GetLastError()) + ")"); + if (shortLength >= shortBuffer.size()) + throw std::runtime_error("GetShortPathNameW required a buffer of " + std::to_string(shortLength) + " characters"); + std::filesystem::path shortRoot(std::wstring(shortBuffer.data(), shortLength)); + if (longPath) { + bool aliasAvailable = true; + bool longAliasReady = false; + for (unsigned level = 0; level < 80; ++level) { + longRoot /= L"LongSharedMemoryTempDirectoryRegressionSegment"; + if (!::CreateDirectoryW(longRoot.c_str(), nullptr)) + throw std::runtime_error("creating the nested long temporary directory failed (Win32 error " + std::to_string(::GetLastError()) + ")"); + shortLength = ::GetShortPathNameW(longRoot.c_str(), shortBuffer.data(), static_cast(shortBuffer.size())); + if (shortLength == 0) + throw std::runtime_error("GetShortPathNameW failed for nested path (Win32 error " + std::to_string(::GetLastError()) + ")"); + if (shortLength >= shortBuffer.size()) + throw std::runtime_error("GetShortPathNameW required a buffer of " + std::to_string(shortLength) + " characters"); + shortRoot = std::filesystem::path(std::wstring(shortBuffer.data(), shortLength)); + if (::CompareStringOrdinal(longRoot.filename().c_str(), -1, shortRoot.filename().c_str(), -1, TRUE) == CSTR_EQUAL) { + aliasAvailable = false; + break; + } + if (DosPath(longRoot).native().size() > MAX_PATH + 64 && DosPath(shortRoot).native().size() < MAX_PATH - 64) { + longAliasReady = true; + break; + } + } + if (!aliasAvailable) { + std::puts("long-short-temp regression skipped: this volume did not create an 8.3 alias for the nested directory"); + cleanup(); + return 77; + } + if (!longAliasReady) + throw std::runtime_error("nested private path did not exceed MAX_PATH with a usable short alias"); + } + if (::CompareStringOrdinal(longRoot.filename().c_str(), -1, shortRoot.filename().c_str(), -1, TRUE) == CSTR_EQUAL) { + std::puts("short-temp regression skipped: this volume did not create an 8.3 alias for the private directory"); + cleanup(); + return 77; + } + const HANDLE longHandle = ::CreateFileW(longRoot.c_str(), FILE_READ_ATTRIBUTES, + FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nullptr, OPEN_EXISTING, + FILE_FLAG_BACKUP_SEMANTICS, nullptr); + if (longHandle == INVALID_HANDLE_VALUE) throw std::runtime_error("opening the long temporary path failed"); + const HANDLE shortHandle = ::CreateFileW(shortRoot.c_str(), FILE_READ_ATTRIBUTES, + FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nullptr, OPEN_EXISTING, + FILE_FLAG_BACKUP_SEMANTICS, nullptr); + if (shortHandle == INVALID_HANDLE_VALUE) { + ::CloseHandle(longHandle); + throw std::runtime_error("opening the 8.3 temporary alias failed"); + } + const bool isSameDirectory = SameDirectory(longHandle, shortHandle); + ::CloseHandle(shortHandle); + ::CloseHandle(longHandle); + if (!isSameDirectory) throw std::runtime_error("the short temporary path did not identify the created directory"); + std::wprintf(L"%ls regression verified 8.3 directory alias (%llu/%llu characters):\n%ls\n%ls\n", + longPath ? L"long-short-temp" : L"short-temp", + static_cast(longRoot.native().size()), + static_cast(shortRoot.native().size()), longRoot.c_str(), shortRoot.c_str()); + std::fflush(stdout); + + wchar_t executable[32768]{}; + const DWORD executableLength = ::GetModuleFileNameW(nullptr, executable, static_cast(std::size(executable))); + if (executableLength == 0 || executableLength >= std::size(executable)) + throw std::runtime_error("locating the Windows shared-memory test executable failed"); + std::wstring command = L"\"" + std::wstring(executable, executableLength) + L"\" --short-temp-child"; + auto environment = EnvironmentWithTempPath(DosPath(shortRoot).native()); + STARTUPINFOW startup{}; + startup.cb = sizeof(startup); + PROCESS_INFORMATION process{}; + if (!::CreateProcessW(nullptr, command.data(), nullptr, nullptr, FALSE, + CREATE_NO_WINDOW | CREATE_UNICODE_ENVIRONMENT, environment.data(), nullptr, &startup, &process)) + throw std::runtime_error("starting the short temporary path child failed"); + const DWORD wait = ::WaitForSingleObject(process.hProcess, 16000); + DWORD exitCode = 1; + const bool childSucceeded = wait == WAIT_OBJECT_0 && ::GetExitCodeProcess(process.hProcess, &exitCode) && exitCode == 0; + if (wait != WAIT_OBJECT_0) { + ::TerminateProcess(process.hProcess, 1); + ::WaitForSingleObject(process.hProcess, 1000); + } + if (!childSucceeded) + std::fprintf(stderr, "%s child failed: wait=%lu exit=%lu\n", longPath ? "long-short-temp" : "short-temp", + static_cast(wait), static_cast(exitCode)); + ::CloseHandle(process.hThread); + ::CloseHandle(process.hProcess); + cleanup(); + return childSucceeded ? 0 : 1; + } catch (...) { + cleanup(); + throw; + } +} +} + +int main(int argumentCount, char** arguments) { + try { + if (argumentCount == 3 && std::string_view(arguments[1]) == "--race") + return RunResizeWriteRace(arguments[2]) ? 0 : 1; + if (argumentCount == 2 && std::string_view(arguments[1]) == "--short-temp") + return RunWithShortTempPath(false); + if (argumentCount == 2 && std::string_view(arguments[1]) == "--long-short-temp") + return RunWithShortTempPath(true); + if (argumentCount == 2 && std::string_view(arguments[1]) == "--short-temp-child") { + Run(); + return 0; + } + Run(); + return 0; + } catch (const std::exception& error) { + std::fprintf(stderr, "%s\n", error.what()); + return 1; + } +} diff --git a/core/libs/tests/GuestNetCtl.cpp b/core/libs/tests/GuestNetCtl.cpp index f45a66dc77..9b900f4bd4 100644 --- a/core/libs/tests/GuestNetCtl.cpp +++ b/core/libs/tests/GuestNetCtl.cpp @@ -51,6 +51,9 @@ int APS5_VABI sceNetCtlRegisterCallbackV6(NetCtlCallback, void*, int*); void APS5_VABI sceNetCtlTerm(void); int APS5_VABI sceNetCtlUnregisterCallback(int); int APS5_VABI sceNetCtlUnregisterCallbackV6(int); +int APS5_VABI sceNetCtlRegisterCallbackForNpToolkit(NetCtlCallback, void*, int*); +int APS5_VABI sceNetCtlUnregisterCallbackForNpToolkit(int); +int APS5_VABI sceNetCtlCheckCallbackForNpToolkit(void); } namespace { @@ -283,6 +286,9 @@ int main() { Require(sceNetCtlRegisterCallbackV6(nullptr, nullptr, nullptr) == kErrInvalidAddr); Require(sceNetCtlUnregisterCallback(-1) == invalidId); Require(sceNetCtlUnregisterCallbackV6(-1) == invalidId); + Require(sceNetCtlRegisterCallbackForNpToolkit(nullptr, nullptr, nullptr) == kErrInvalidAddr); + Require(sceNetCtlUnregisterCallbackForNpToolkit(-1) == invalidId); + Require(sceNetCtlCheckCallbackForNpToolkit() == 0); CheckCallbacks(); Require(sceNetCtlGetStateV6(nullptr) == kErrInvalidAddr); @@ -290,25 +296,16 @@ int main() { Require(sceNetCtlGetStateV6(&stateV6) == 0); Require(stateV6 == 0 || stateV6 == kStateIpObtained); Require(sceNetCtlGetResult(0, nullptr) == kErrInvalidAddr); - int errorCode = -1; - bool resultUnsupported = false; - try { - sceNetCtlGetResult(0, &errorCode); - } catch (const std::logic_error&) { - resultUnsupported = true; + Require(sceNetCtlGetResult(kEventDisconnected, nullptr) == kErrInvalidAddr); + for (const int eventType : {kEventDisconnected, 2, kEventIpObtained}) { + int errorCode = -1; + Require(sceNetCtlGetResult(eventType, &errorCode) == 0 && errorCode == 0); } - Require(resultUnsupported && errorCode == -1); Require(sceNetCtlGetResultV6(kEventIpObtained, nullptr) == kErrInvalidAddr); for (const int eventType : {kEventDisconnected, 2, kEventIpObtained}) { int errorCodeV6 = -1; Require(sceNetCtlGetResultV6(eventType, &errorCodeV6) == 0 && errorCodeV6 == 0); } - for (const int eventType : {0, 4, -1}) { - int errorCodeV6 = -1; - bool threw = false; - try { sceNetCtlGetResultV6(eventType, &errorCodeV6); } catch (const std::runtime_error&) { threw = true; } - Require(threw && errorCodeV6 == -1); - } NetCtlInfo info{}; Require(sceNetCtlGetInfo(0, &info) == kErrInvalidCode); Require(sceNetCtlGetInfo(22, &info) == kErrInvalidCode); diff --git a/core/libs/tests/GuestNetCtlAp.cpp b/core/libs/tests/GuestNetCtlAp.cpp index 12f8799e79..cc7dfda6a9 100644 --- a/core/libs/tests/GuestNetCtlAp.cpp +++ b/core/libs/tests/GuestNetCtlAp.cpp @@ -108,7 +108,7 @@ int main() { cids[i] = -1; Require(sceNetCtlApRegisterCallback(Callback, &cids[i], &cids[i]) == 0 && cids[i] == i); } - int extra = -1; + int extra = 0x5a5a; Require(sceNetCtlApRegisterCallback(Callback, nullptr, &extra) == kErrCallbackMax && extra == -1); Require(sceNetCtlApRegisterCallback(nullptr, nullptr, &extra) == kErrInvalidAddr); Require(sceNetCtlApRegisterCallback(Callback, nullptr, nullptr) == kErrInvalidAddr); diff --git a/core/libs/tests/GuestNetThread.cpp b/core/libs/tests/GuestNetThread.cpp new file mode 100644 index 0000000000..27f3b16a11 --- /dev/null +++ b/core/libs/tests/GuestNetThread.cpp @@ -0,0 +1,86 @@ +#include "SceTypes.hpp" +#include +#include +#include +#include +#include +#include + +extern "C" { +int APS5_VABI sceNetThreadCreate(Pthread*, void*, const char*); +void APS5_VABI sceNetThreadExit(); +int APS5_VABI sceNetThreadJoin(Pthread); +int APS5_VABI sceNetEmulationGet(); +int APS5_VABI sceNetEmulationSet(); +int* APS5_VABI sceNetErrnoLoc(); +} + +namespace { + +constexpr int Einval = 22; +constexpr int Eopnotsupp = 45; +constexpr int NetError = static_cast(0x80410100u); + +struct ThreadParam { + void (APS5_VABI *entry)(void*); + void* arg; + std::uint64_t reserved; + std::uint32_t flags; +}; + +struct Run { + std::atomic started{0}; + std::atomic pastExit{false}; +}; + +Run run; + +void Require(bool value, const char* what) { + if (value) return; + std::fprintf(stderr, "%s\n", what); + std::abort(); +} + +void APS5_VABI Count(void* arg) { + ++static_cast(arg)->started; +} + +void APS5_VABI ExitEarly(void* arg) { + auto* run = static_cast(arg); + ++run->started; + sceNetThreadExit(); + run->pastExit = true; +} + +} + +int main() { + ThreadParam counting{Count, &run, 0, 0}; + Pthread thread = nullptr; + Require(sceNetThreadCreate(&thread, &counting, "anyps5-net-thread") == 0 && thread != nullptr, "sceNetThreadCreate failed"); + Require(sceNetThreadJoin(thread) == 0, "sceNetThreadJoin failed"); + Require(run.started == 1, "the thread did not run its entry with its argument"); + + Pthread untouched = reinterpret_cast(&run); + *sceNetErrnoLoc() = 0; + Require(sceNetThreadCreate(&untouched, nullptr, "anyps5-net-thread") == (NetError | Einval), "a null parameter was not refused with EINVAL"); + Require(*sceNetErrnoLoc() == Einval && untouched == reinterpret_cast(&run), "a refused create set no errno or wrote the thread"); + + ThreadParam exiting{ExitEarly, &run, 0, 1}; + Require(sceNetThreadCreate(&thread, &exiting, nullptr) == 0, "sceNetThreadCreate failed without a name"); + Require(sceNetThreadJoin(thread) == 0, "joining an exited thread failed"); + Require(run.started == 2 && !run.pastExit, "sceNetThreadExit did not end the thread"); + + static ThreadParam detached{Count, &run, 0, 0}; + Require(sceNetThreadCreate(nullptr, &detached, "anyps5-net-thread") == 0, "a null thread output was refused"); + const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(5); + while (run.started != 3 && std::chrono::steady_clock::now() < deadline) std::this_thread::sleep_for(std::chrono::milliseconds(1)); + Require(run.started == 3, "the thread created without an output did not run"); + + *sceNetErrnoLoc() = 0; + Require(sceNetEmulationGet() == (NetError | Eopnotsupp) && *sceNetErrnoLoc() == Eopnotsupp, "sceNetEmulationGet did not return EOPNOTSUPP"); + *sceNetErrnoLoc() = 0; + Require(sceNetEmulationSet() == (NetError | Eopnotsupp) && *sceNetErrnoLoc() == Eopnotsupp, "sceNetEmulationSet did not return EOPNOTSUPP"); + std::puts("net thread tests passed"); + return 0; +} diff --git a/core/libs/tests/GuestNewHandler.cpp b/core/libs/tests/GuestNewHandler.cpp index 04a5d9a8a1..6229ada261 100644 --- a/core/libs/tests/GuestNewHandler.cpp +++ b/core/libs/tests/GuestNewHandler.cpp @@ -19,6 +19,7 @@ void* APS5_VABI _ZnwmSt11align_val_t_nid_postfix(std::size_t, std::size_t); void* APS5_VABI _ZnwmRKSt9nothrow_t_nid_postfix(std::size_t, const void*) noexcept; void* APS5_VABI _ZnamRKSt9nothrow_t_nid_postfix(std::size_t, const void*) noexcept; void APS5_VABI _ZdlPv_nid_postfix(void*); +void APS5_VABI _ZdlPvRKSt9nothrow_t_nid_postfix(void*, const void*) noexcept; void APS5_VABI _ZdlPvSt11align_val_t_nid_postfix(void*, std::size_t); } @@ -27,6 +28,7 @@ namespace { alignas(16) std::array storage{}; unsigned failuresLeft = 0; unsigned allocations = 0; +unsigned releases = 0; unsigned handlerCalls = 0; std::size_t lastSize = 0; std::size_t lastAlignment = 0; @@ -50,7 +52,7 @@ void* APS5_VABI allocate(std::size_t bytes) { return storage.data(); } -void APS5_VABI release(void*) {} +void APS5_VABI release(void*) { ++releases; } void* APS5_VABI allocateZeroed(std::size_t count, std::size_t bytes) { return allocate(count * bytes); } void* APS5_VABI reallocate(void*, std::size_t bytes) { return allocate(bytes); } void* APS5_VABI align(std::size_t alignment, std::size_t bytes) { @@ -80,6 +82,7 @@ void APS5_VABI allocatingHandler() { void Reset(unsigned failures) { failuresLeft = failures; allocations = 0; + releases = 0; handlerCalls = 0; } @@ -138,6 +141,15 @@ int main(int argc, char** argv) { REQUIRE(_ZSt15get_new_handlerv_nid_postfix() == nullptr); + Reset(0); + void* nothrowPointer = _Znwm_nid_postfix(8); + const int nothrowTag = 0; + REQUIRE(nothrowPointer == storage.data() && releases == 0); + _ZdlPvRKSt9nothrow_t_nid_postfix(nothrowPointer, ¬hrowTag); + REQUIRE(releases == 1); + _ZdlPvRKSt9nothrow_t_nid_postfix(nullptr, ¬hrowTag); + REQUIRE(releases == 1); + Reset(1); REQUIRE(Throws([] { _Znwm_nid_postfix(8); }) && handlerCalls == 0 && allocations == 1); Reset(1); diff --git a/core/libs/tests/GuestNpCommon.cpp b/core/libs/tests/GuestNpCommon.cpp index d605767148..c3d964f7ea 100644 --- a/core/libs/tests/GuestNpCommon.cpp +++ b/core/libs/tests/GuestNpCommon.cpp @@ -24,6 +24,7 @@ int APS5_VABI sceNpLwMutexInit(PthreadMutex* mutex, const char* name, std::uint6 int APS5_VABI sceNpLwMutexTryLock(PthreadMutex* mutex); int APS5_VABI sceNpLwMutexUnlock(PthreadMutex* mutex); int APS5_VABI sceNpLwMutexDestroy(PthreadMutex* mutex); +int APS5_VABI sceNpLwMutexLock(PthreadMutex* mutex); int APS5_VABI sceNpCondInit(PthreadCond* cond, const char* name, std::uint64_t flags); int APS5_VABI sceNpCondDestroy(PthreadCond* cond); int APS5_VABI sceNpCondSignal(PthreadCond* cond); @@ -127,6 +128,8 @@ static int testSync() { ok &= expect(sceNpMutexUnlock(&plain) == 0, "the plain mutex did not unlock"); ok &= expect(sceNpLwMutexTryLock(&recursive) == 0 && sceNpLwMutexTryLock(&recursive) == 0, "flag 1 did not make the mutex recursive"); ok &= expect(sceNpLwMutexUnlock(&recursive) == 0 && sceNpLwMutexUnlock(&recursive) == 0, "the recursive mutex did not unlock twice"); + ok &= expect(sceNpLwMutexLock(&recursive) == 0, "the recursive mutex did not lock via sceNpLwMutexLock"); + ok &= expect(sceNpLwMutexUnlock(&recursive) == 0, "the recursive mutex did not unlock"); ok &= expect(sceNpLwMutexDestroy(&recursive) == 0, "the recursive mutex was not destroyed"); PthreadCond cond = nullptr; diff --git a/core/libs/tests/GuestNpCppWebApiIntrusivePtr.cpp b/core/libs/tests/GuestNpCppWebApiIntrusivePtr.cpp index f9a03f0d52..93ee41cd7a 100644 --- a/core/libs/tests/GuestNpCppWebApiIntrusivePtr.cpp +++ b/core/libs/tests/GuestNpCppWebApiIntrusivePtr.cpp @@ -79,6 +79,28 @@ void* APS5_VABI _ZNK3sce2Np9CppWebApi6Common12IntrusivePtrINS2_6VectorINS3_INS1_ void* APS5_VABI _ZNK3sce2Np9CppWebApi6Common12IntrusivePtrINS2_6VectorINS3_INS1_11UserProfile2V113BasicPresenceEEEEEEptEv(const IntrusivePtr*); void* APS5_VABI _ZNK3sce2Np9CppWebApi6Common12IntrusivePtrINS2_6VectorImEEEdeEv(const IntrusivePtr*); void* APS5_VABI _ZNK3sce2Np9CppWebApi6Common12IntrusivePtrINS2_6VectorImEEEptEv(const IntrusivePtr*); +void APS5_VABI _ZN3sce2Np9CppWebApi6Common12IntrusivePtrINS1_17TitleCloudStorage2V132GetMultiDataStatusesResponseBodyEEC1Ev(IntrusivePtr*); +void APS5_VABI _ZN3sce2Np9CppWebApi6Common12IntrusivePtrINS2_6VectorINS3_INS1_13InGameCatalog2V59ContainerEEEEEEC1Ev(IntrusivePtr*); +void* APS5_VABI _ZNK3sce2Np9CppWebApi6Common12IntrusivePtrINS1_13InGameCatalog2V57ProductEEptEv(const IntrusivePtr*); +void* APS5_VABI _ZNK3sce2Np9CppWebApi6Common12IntrusivePtrINS1_13InGameCatalog2V59ContainerEEptEv(const IntrusivePtr*); +void* APS5_VABI _ZNK3sce2Np9CppWebApi6Common12IntrusivePtrINS1_17TitleCloudStorage2V110DataStatusEEptEv(const IntrusivePtr*); +void* APS5_VABI _ZNK3sce2Np9CppWebApi6Common12IntrusivePtrINS1_17TitleCloudStorage2V132GetMultiDataStatusesResponseBodyEEptEv(const IntrusivePtr*); +void* APS5_VABI _ZNK3sce2Np9CppWebApi6Common12IntrusivePtrINS1_17TitleCloudStorage2V15OwnerEEptEv(const IntrusivePtr*); +void* APS5_VABI _ZNK3sce2Np9CppWebApi6Common12IntrusivePtrINS2_6VectorINS3_INS1_13InGameCatalog2V53SkuEEEEEEptEv(const IntrusivePtr*); +void* APS5_VABI _ZNK3sce2Np9CppWebApi6Common12IntrusivePtrINS2_6VectorINS3_INS1_13InGameCatalog2V57ProductEEEEEEptEv(const IntrusivePtr*); +void* APS5_VABI _ZNK3sce2Np9CppWebApi6Common12IntrusivePtrINS2_6VectorINS3_INS1_13InGameCatalog2V59ContainerEEEEEEdeEv(const IntrusivePtr*); +void* APS5_VABI _ZNK3sce2Np9CppWebApi6Common12IntrusivePtrINS2_6VectorINS3_INS1_13InGameCatalog2V59ContainerEEEEEEptEv(const IntrusivePtr*); +void* APS5_VABI _ZNK3sce2Np9CppWebApi6Common12IntrusivePtrINS2_6VectorINS3_INS1_17TitleCloudStorage2V110DataStatusEEEEEEdeEv(const IntrusivePtr*); +void APS5_VABI _ZN3sce2Np9CppWebApi6Common12IntrusivePtrINS1_13InGameCatalog2V57ProductEEC1ERS7_(IntrusivePtr*, const IntrusivePtr*); +void APS5_VABI _ZN3sce2Np9CppWebApi6Common12IntrusivePtrINS1_13InGameCatalog2V59ContainerEEC1ERS7_(IntrusivePtr*, const IntrusivePtr*); +void APS5_VABI _ZN3sce2Np9CppWebApi6Common12IntrusivePtrINS1_13InGameCatalog2V57ProductEED1Ev(IntrusivePtr*); +void APS5_VABI _ZN3sce2Np9CppWebApi6Common12IntrusivePtrINS1_13InGameCatalog2V59ContainerEED1Ev(IntrusivePtr*); +void APS5_VABI _ZN3sce2Np9CppWebApi6Common12IntrusivePtrINS1_17TitleCloudStorage2V132GetMultiDataStatusesResponseBodyEED1Ev(IntrusivePtr*); +void APS5_VABI _ZN3sce2Np9CppWebApi6Common12IntrusivePtrINS1_17TitleCloudStorage2V15OwnerEED1Ev(IntrusivePtr*); +void APS5_VABI _ZN3sce2Np9CppWebApi6Common12IntrusivePtrINS2_6VectorINS3_INS1_13InGameCatalog2V53SkuEEEEEED1Ev(IntrusivePtr*); +void APS5_VABI _ZN3sce2Np9CppWebApi6Common12IntrusivePtrINS2_6VectorINS3_INS1_13InGameCatalog2V57ProductEEEEEED1Ev(IntrusivePtr*); +void APS5_VABI _ZN3sce2Np9CppWebApi6Common12IntrusivePtrINS2_6VectorINS3_INS1_13InGameCatalog2V59ContainerEEEEEED1Ev(IntrusivePtr*); +void APS5_VABI _ZN3sce2Np9CppWebApi6Common12IntrusivePtrINS2_6VectorINS3_INS1_17TitleCloudStorage2V110DataStatusEEEEEED1Ev(IntrusivePtr*); } static void Require(bool value) { if (!value) std::abort(); } @@ -305,6 +327,8 @@ int main() { _ZN3sce2Np9CppWebApi6Common12IntrusivePtrINS1_15ProfanityFilter2V219WebApiFilterRequestEEC1Ev, _ZN3sce2Np9CppWebApi6Common12IntrusivePtrINS2_18ResponseHeaderBaseEEC1Ev, _ZN3sce2Np9CppWebApi6Common12IntrusivePtrINS2_6StringEEC1Ev, + _ZN3sce2Np9CppWebApi6Common12IntrusivePtrINS1_17TitleCloudStorage2V132GetMultiDataStatusesResponseBodyEEC1Ev, + _ZN3sce2Np9CppWebApi6Common12IntrusivePtrINS2_6VectorINS3_INS1_13InGameCatalog2V59ContainerEEEEEEC1Ev, }; for (const auto construct : constructors) TestConstruct(construct); @@ -333,6 +357,16 @@ int main() { _ZNK3sce2Np9CppWebApi6Common12IntrusivePtrINS2_6VectorINS3_INS1_11UserProfile2V113BasicPresenceEEEEEEptEv, _ZNK3sce2Np9CppWebApi6Common12IntrusivePtrINS2_6VectorImEEEdeEv, _ZNK3sce2Np9CppWebApi6Common12IntrusivePtrINS2_6VectorImEEEptEv, + _ZNK3sce2Np9CppWebApi6Common12IntrusivePtrINS1_13InGameCatalog2V57ProductEEptEv, + _ZNK3sce2Np9CppWebApi6Common12IntrusivePtrINS1_13InGameCatalog2V59ContainerEEptEv, + _ZNK3sce2Np9CppWebApi6Common12IntrusivePtrINS1_17TitleCloudStorage2V110DataStatusEEptEv, + _ZNK3sce2Np9CppWebApi6Common12IntrusivePtrINS1_17TitleCloudStorage2V132GetMultiDataStatusesResponseBodyEEptEv, + _ZNK3sce2Np9CppWebApi6Common12IntrusivePtrINS1_17TitleCloudStorage2V15OwnerEEptEv, + _ZNK3sce2Np9CppWebApi6Common12IntrusivePtrINS2_6VectorINS3_INS1_13InGameCatalog2V53SkuEEEEEEptEv, + _ZNK3sce2Np9CppWebApi6Common12IntrusivePtrINS2_6VectorINS3_INS1_13InGameCatalog2V57ProductEEEEEEptEv, + _ZNK3sce2Np9CppWebApi6Common12IntrusivePtrINS2_6VectorINS3_INS1_13InGameCatalog2V59ContainerEEEEEEdeEv, + _ZNK3sce2Np9CppWebApi6Common12IntrusivePtrINS2_6VectorINS3_INS1_13InGameCatalog2V59ContainerEEEEEEptEv, + _ZNK3sce2Np9CppWebApi6Common12IntrusivePtrINS2_6VectorINS3_INS1_17TitleCloudStorage2V110DataStatusEEEEEEdeEv, }; for (const auto access : accessors) TestAccess(access); @@ -340,6 +374,8 @@ int main() { _ZN3sce2Np9CppWebApi6Common12IntrusivePtrINS1_12Leaderboards2V121GetRankingRequestBodyEEC1ERS7_, _ZN3sce2Np9CppWebApi6Common12IntrusivePtrINS1_12Leaderboards2V122RecordScoreRequestBodyEEC1ERS7_, _ZN3sce2Np9CppWebApi6Common12IntrusivePtrINS1_15ProfanityFilter2V219WebApiFilterRequestEEC1ERS7_, + _ZN3sce2Np9CppWebApi6Common12IntrusivePtrINS1_13InGameCatalog2V57ProductEEC1ERS7_, + _ZN3sce2Np9CppWebApi6Common12IntrusivePtrINS1_13InGameCatalog2V59ContainerEEC1ERS7_, }; for (const auto copy : copies) TestCopy(copy); @@ -356,6 +392,14 @@ int main() { {_ZN3sce2Np9CppWebApi6Common12IntrusivePtrINS2_18ResponseHeaderBaseEED1Ev, 0}, {_ZN3sce2Np9CppWebApi6Common12IntrusivePtrINS2_6StringEED1Ev, 0}, {_ZN3sce2Np9CppWebApi6Common12IntrusivePtrINS2_6VectorINS3_INS1_11UserProfile2V113BasicPresenceEEEEEED1Ev, VectorReferenceCountOffset}, + {_ZN3sce2Np9CppWebApi6Common12IntrusivePtrINS1_13InGameCatalog2V57ProductEED1Ev, 0}, + {_ZN3sce2Np9CppWebApi6Common12IntrusivePtrINS1_13InGameCatalog2V59ContainerEED1Ev, 0}, + {_ZN3sce2Np9CppWebApi6Common12IntrusivePtrINS1_17TitleCloudStorage2V132GetMultiDataStatusesResponseBodyEED1Ev, 0}, + {_ZN3sce2Np9CppWebApi6Common12IntrusivePtrINS1_17TitleCloudStorage2V15OwnerEED1Ev, 0}, + {_ZN3sce2Np9CppWebApi6Common12IntrusivePtrINS2_6VectorINS3_INS1_13InGameCatalog2V53SkuEEEEEED1Ev, VectorReferenceCountOffset}, + {_ZN3sce2Np9CppWebApi6Common12IntrusivePtrINS2_6VectorINS3_INS1_13InGameCatalog2V57ProductEEEEEED1Ev, VectorReferenceCountOffset}, + {_ZN3sce2Np9CppWebApi6Common12IntrusivePtrINS2_6VectorINS3_INS1_13InGameCatalog2V59ContainerEEEEEED1Ev, VectorReferenceCountOffset}, + {_ZN3sce2Np9CppWebApi6Common12IntrusivePtrINS2_6VectorINS3_INS1_17TitleCloudStorage2V110DataStatusEEEEEED1Ev, VectorReferenceCountOffset}, }; for (const auto& testCase : destructors) TestDestroy(testCase); diff --git a/core/libs/tests/GuestNpCppWebApiOffline.cpp b/core/libs/tests/GuestNpCppWebApiOffline.cpp new file mode 100644 index 0000000000..32a7b48145 --- /dev/null +++ b/core/libs/tests/GuestNpCppWebApiOffline.cpp @@ -0,0 +1,34 @@ +#include "prx/libc/include/general/VabiMacros.hpp" +#include +#include +#include + +using SelfCall = int (APS5_VABI*)(void*); + +extern "C" { +int APS5_VABI _ZN3sce2Np9CppWebApi6Common13ConstIteratorINS2_12IntrusivePtrINS1_13InGameCatalog2V57ProductEEEED2Ev(void*); +int APS5_VABI _ZN3sce2Np9CppWebApi6Common13ConstIteratorINS2_12IntrusivePtrINS1_17TitleCloudStorage2V110DataStatusEEEED2Ev(void*); +} + +static void Require(bool value, const char* what) { + if (value) return; + std::fprintf(stderr, "%s\n", what); + std::abort(); +} + +static void TestIteratorDestructors() { + unsigned char iterator[0x20]; + std::memset(iterator, 0x5a, sizeof(iterator)); + const SelfCall destructors[] = { + _ZN3sce2Np9CppWebApi6Common13ConstIteratorINS2_12IntrusivePtrINS1_13InGameCatalog2V57ProductEEEED2Ev, + _ZN3sce2Np9CppWebApi6Common13ConstIteratorINS2_12IntrusivePtrINS1_17TitleCloudStorage2V110DataStatusEEEED2Ev, + }; + for (const auto call : destructors) Require(call(iterator) == 0, "an iterator destructor failed"); + for (const auto byte : iterator) Require(byte == 0x5a, "an iterator destructor wrote to the iterator"); +} + +int main() { + TestIteratorDestructors(); + std::puts("NpCppWebApi offline tests passed"); + return 0; +} diff --git a/core/libs/tests/GuestNpMatching2Offline.cpp b/core/libs/tests/GuestNpMatching2Offline.cpp new file mode 100644 index 0000000000..7435757a21 --- /dev/null +++ b/core/libs/tests/GuestNpMatching2Offline.cpp @@ -0,0 +1,119 @@ +#include "SceTypes.hpp" +#include +#include + +struct InitializeParameter { + std::uint64_t poolSize; + std::uint64_t reserved; + std::int32_t priority; + std::uint8_t padding[4]; + std::uint64_t stackSize; + std::uint64_t size; + std::uint64_t sslPoolSize; +}; + +struct CreateContextParameter { + const void* npId; + const void* npCommunicationId; + const void* npPassphrase; + std::uint32_t serviceLabel; + std::uint64_t size; +}; + +extern "C" { +int APS5_VABI sceNpMatching2Initialize(const InitializeParameter* param); +int APS5_VABI sceNpMatching2Terminate(); +int APS5_VABI sceNpMatching2CreateContext(const CreateContextParameter* param, std::uint16_t* ctxId); +int APS5_VABI sceNpMatching2DestroyContext(std::uint16_t ctxId); +int APS5_VABI sceNpMatching2ContextStart(std::uint16_t ctxId, std::uint64_t timeout); +int APS5_VABI sceNpMatching2ContextStop(std::uint16_t ctxId); +int APS5_VABI sceNpMatching2RegisterContextCallback(void* callback, void* userdata); +int APS5_VABI sceNpMatching2RegisterRoomEventCallback(std::uint16_t ctxId, void* callback, void* userdata); +int APS5_VABI sceNpMatching2RegisterRoomMessageCallback(std::uint16_t ctxId, void* callback, void* userdata); +int APS5_VABI sceNpMatching2RegisterSignalingCallback(std::uint16_t ctxId, void* callback, void* userdata); +int APS5_VABI sceNpMatching2SetDefaultRequestOptParam(std::uint16_t ctxId, const void* requestOpt); +} + +namespace { + +constexpr int ResolverTimedOut = static_cast(0x804101E2u); +constexpr int NotInitialized = static_cast(0x80550C03u); +constexpr int ContextMax = static_cast(0x80550C04u); +constexpr int ContextNotStarted = static_cast(0x80550C08u); +constexpr int InvalidArgument = static_cast(0x80550C0Au); +constexpr int InvalidContextId = static_cast(0x80550C0Bu); + +int failures = 0; + +void Require(bool value, const char* name, const char* what) { + if (value) return; + std::fprintf(stderr, "%s: %s\n", name, what); + ++failures; +} + +} + +int main() { + InitializeParameter init{}; + CreateContextParameter create{}; + create.size = 0x28; + unsigned char requestOpt[24] = {}; + std::uint16_t context = 77; + + Require(sceNpMatching2CreateContext(nullptr, nullptr) == NotInitialized, "sceNpMatching2CreateContext", "a call before initialization did not return NOT_INITIALIZED first"); + Require(sceNpMatching2DestroyContext(1) == NotInitialized, "sceNpMatching2DestroyContext", "a call before initialization did not return NOT_INITIALIZED"); + Require(sceNpMatching2ContextStart(1, 0) == NotInitialized, "sceNpMatching2ContextStart", "a call before initialization did not return NOT_INITIALIZED"); + Require(sceNpMatching2ContextStop(1) == NotInitialized, "sceNpMatching2ContextStop", "a call before initialization did not return NOT_INITIALIZED"); + Require(sceNpMatching2RegisterContextCallback(nullptr, nullptr) == NotInitialized, "sceNpMatching2RegisterContextCallback", "a call before initialization did not return NOT_INITIALIZED"); + Require(sceNpMatching2RegisterRoomEventCallback(1, nullptr, nullptr) == NotInitialized, "sceNpMatching2RegisterRoomEventCallback", "a call before initialization did not return NOT_INITIALIZED"); + Require(sceNpMatching2RegisterRoomMessageCallback(1, nullptr, nullptr) == NotInitialized, "sceNpMatching2RegisterRoomMessageCallback", "a call before initialization did not return NOT_INITIALIZED"); + Require(sceNpMatching2RegisterSignalingCallback(1, nullptr, nullptr) == NotInitialized, "sceNpMatching2RegisterSignalingCallback", "a call before initialization did not return NOT_INITIALIZED"); + Require(sceNpMatching2SetDefaultRequestOptParam(1, nullptr) == NotInitialized, "sceNpMatching2SetDefaultRequestOptParam", "a call before initialization did not return NOT_INITIALIZED first"); + + init.size = 0x30; + Require(sceNpMatching2Initialize(&init) == 0, "sceNpMatching2Initialize", "initialization failed"); + + Require(sceNpMatching2CreateContext(nullptr, &context) == InvalidArgument, "sceNpMatching2CreateContext", "a null parameter was taken"); + create.size = 0x30; + Require(sceNpMatching2CreateContext(&create, &context) == InvalidArgument, "sceNpMatching2CreateContext", "parameter size 0x30 was taken"); + create.size = 0x28; + Require(sceNpMatching2CreateContext(&create, nullptr) == InvalidArgument, "sceNpMatching2CreateContext", "a null output was taken"); + Require(context == 77, "sceNpMatching2CreateContext", "a refused call wrote the context id"); + Require(sceNpMatching2CreateContext(&create, &context) == 0 && context == 1, "sceNpMatching2CreateContext", "the first context is not id 1"); + Require(sceNpMatching2DestroyContext(1) == 0, "sceNpMatching2DestroyContext", "destroying context 1 failed"); + std::uint16_t created = 0; + for (std::uint16_t expected = 2; expected <= 10; ++expected) { + Require(sceNpMatching2CreateContext(&create, &created) == 0 && created == expected, "sceNpMatching2CreateContext", "a context id was reused before the ids up to 10 were handed out"); + } + Require(sceNpMatching2CreateContext(&create, &created) == 0 && created == 1, "sceNpMatching2CreateContext", "after id 10 the allocation did not wrap to the free id 1"); + created = 77; + Require(sceNpMatching2CreateContext(&create, &created) == ContextMax && created == 77, "sceNpMatching2CreateContext", "an eleventh context did not return CONTEXT_MAX"); + Require(sceNpMatching2DestroyContext(11) == InvalidContextId, "sceNpMatching2DestroyContext", "an unknown context was destroyed"); + + Require(sceNpMatching2ContextStart(1, 0) == ResolverTimedOut, "sceNpMatching2ContextStart", "starting a context did not return the offline resolver timeout"); + Require(sceNpMatching2ContextStart(99, 0) == ResolverTimedOut, "sceNpMatching2ContextStart", "the offline answer did not come before the context check"); + Require(sceNpMatching2ContextStop(1) == ContextNotStarted, "sceNpMatching2ContextStop", "a context that never started did not return CONTEXT_NOT_STARTED"); + Require(sceNpMatching2ContextStop(99) == InvalidContextId, "sceNpMatching2ContextStop", "an unknown context was taken"); + + Require(sceNpMatching2RegisterContextCallback(nullptr, nullptr) == 0, "sceNpMatching2RegisterContextCallback", "registering the context callback failed"); + Require(sceNpMatching2RegisterRoomEventCallback(99, nullptr, nullptr) == InvalidContextId, "sceNpMatching2RegisterRoomEventCallback", "an unknown context was taken"); + Require(sceNpMatching2RegisterRoomEventCallback(1, nullptr, nullptr) == 0, "sceNpMatching2RegisterRoomEventCallback", "registering failed"); + Require(sceNpMatching2RegisterRoomMessageCallback(99, nullptr, nullptr) == InvalidContextId, "sceNpMatching2RegisterRoomMessageCallback", "an unknown context was taken"); + Require(sceNpMatching2RegisterRoomMessageCallback(1, nullptr, nullptr) == 0, "sceNpMatching2RegisterRoomMessageCallback", "registering failed"); + Require(sceNpMatching2RegisterSignalingCallback(99, nullptr, nullptr) == InvalidContextId, "sceNpMatching2RegisterSignalingCallback", "an unknown context was taken"); + Require(sceNpMatching2RegisterSignalingCallback(1, nullptr, nullptr) == 0, "sceNpMatching2RegisterSignalingCallback", "registering failed"); + Require(sceNpMatching2SetDefaultRequestOptParam(99, nullptr) == InvalidArgument, "sceNpMatching2SetDefaultRequestOptParam", "a null option was not refused before the context"); + Require(sceNpMatching2SetDefaultRequestOptParam(99, requestOpt) == InvalidContextId, "sceNpMatching2SetDefaultRequestOptParam", "an unknown context was taken"); + Require(sceNpMatching2SetDefaultRequestOptParam(1, requestOpt) == 0, "sceNpMatching2SetDefaultRequestOptParam", "setting the default option failed"); + + Require(sceNpMatching2Terminate() == 0, "sceNpMatching2Terminate", "termination failed"); + Require(sceNpMatching2DestroyContext(1) == NotInitialized, "sceNpMatching2DestroyContext", "a context was destroyed after termination"); + Require(sceNpMatching2Initialize(&init) == 0, "sceNpMatching2Initialize", "initializing again failed"); + Require(sceNpMatching2RegisterRoomEventCallback(5, nullptr, nullptr) == InvalidContextId, "sceNpMatching2Terminate", "a context created before termination is still found"); + Require(sceNpMatching2CreateContext(&create, &created) == 0 && created == 1, "sceNpMatching2Terminate", "termination did not restart the context ids at 1"); + Require(sceNpMatching2Terminate() == 0, "sceNpMatching2Terminate", "the second termination failed"); + + if (failures != 0) return 1; + std::puts("NpMatching2 offline tests passed"); + return 0; +} diff --git a/core/libs/tests/GuestNpTrophy2.cpp b/core/libs/tests/GuestNpTrophy2.cpp index 165d4347f1..27e80b54a4 100644 --- a/core/libs/tests/GuestNpTrophy2.cpp +++ b/core/libs/tests/GuestNpTrophy2.cpp @@ -1,5 +1,7 @@ #include "prx/libc/include/general/VabiMacros.hpp" +#include "prx/libSceNpTrophy2/include/NpTrophy2Constants.hpp" #include "prx/libSceNpTrophy2/include/NpTrophy2Types.hpp" +#include #include #include @@ -10,6 +12,7 @@ int APS5_VABI sceNpTrophy2GetGameInfo(int, int, NpTrophy2GameDetails*, NpTrophy2 int APS5_VABI sceNpTrophy2GetGroupInfo(int, int, int, NpTrophy2GroupDetails*, NpTrophy2GroupData*); int APS5_VABI sceNpTrophy2GetTrophyInfo(int, int, int, NpTrophy2Details*, NpTrophy2Data*); int APS5_VABI sceNpTrophy2ShowTrophyList(int); +int APS5_VABI sceNpTrophy2GetRewardIcon(int, int, int, void*, std::size_t*); } static void Require(bool value) { if (!value) std::abort(); } @@ -57,4 +60,8 @@ int main() { Require(sceNpTrophy2GetTrophyInfo(1, 1, 3, nullptr, &trophyData) == 0); Require(trophyData.trophy_id == 3); Require(ThrowsInvalidArgument([] { sceNpTrophy2GetTrophyInfo(1, 1, 3, nullptr, nullptr); })); + std::size_t iconSize = 1234; + Require(sceNpTrophy2GetRewardIcon(1, 1, 1, nullptr, &iconSize) == SCE_NP_TROPHY2_ERROR_ICON_FILE_NOT_FOUND); + Require(iconSize == 0); + Require(sceNpTrophy2GetRewardIcon(1, 1, 1, nullptr, nullptr) == SCE_NP_TROPHY2_ERROR_ICON_FILE_NOT_FOUND); } diff --git a/core/libs/tests/GuestPad.cpp b/core/libs/tests/GuestPad.cpp index 35abd4888c..36e3e9e0ab 100644 --- a/core/libs/tests/GuestPad.cpp +++ b/core/libs/tests/GuestPad.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -41,6 +42,8 @@ int APS5_VABI scePadGetExtControllerInformation(int, void*); int APS5_VABI scePadResetLightBar(int); int APS5_VABI scePadSetLightBar(int, const PadLightBarParam*); int APS5_VABI scePadSetMotionSensorState(int, bool); +int APS5_VABI scePadGetControllerType(int, std::int32_t*); +int APS5_VABI scePadGetInfo_nid_postfix(PadInfo*); } static void Require(bool value) { if (!value) std::abort(); } @@ -420,6 +423,40 @@ static void CheckTrackerDevices(int handle) { } } +static void CheckSystemRemote(int handle) { + constexpr int noHandle = static_cast(0x80920008); + Require(scePadGetHandle(PAD_USER_ID_SYSTEM, PAD_PORT_TYPE_REMOTE, 0) == noHandle); + const int remote = scePadOpen_nid_postfix(PAD_USER_ID_SYSTEM, PAD_PORT_TYPE_REMOTE, 0, nullptr); + Require(remote > 0 && remote != handle); + Require(scePadGetHandle(PAD_USER_ID_SYSTEM, PAD_PORT_TYPE_REMOTE, 0) == remote); + Require(scePadGetHandle(PAD_USER_ID_SYSTEM, PAD_PORT_TYPE_REMOTE, 1) == noHandle); + PadData drained[64]{}; + scePadRead_nid_postfix(handle, drained, 64); + PadInputState pressed; + pressed.buttons = static_cast(Pad::PadButton::Cross); + PadPublishInput_nid_postfix(pressed); + PadData data{}; + data.buttons = ~0u; + data.connected = true; + Require(scePadReadState(remote, &data) == PAD_OK); + Require(data.buttons == 0 && !data.connected); + Require(scePadReadState(remote, nullptr) == PAD_ERROR_INVALID_ARG); + PadData queued[4]{}; + queued[0].connected = true; + Require(scePadRead_nid_postfix(remote, queued, 4) == 1); + Require(queued[0].buttons == 0 && !queued[0].connected); + PadLightBarParam lightBar{1, 2, 3}; + Require(scePadSetLightBar(remote, &lightBar) == PAD_ERROR_INVALID_HANDLE); + Require(scePadReadState(handle, &data) == PAD_OK); + Require((data.buttons & pressed.buttons) != 0 && data.connected); + PadPublishInput_nid_postfix(PadInputState{}); + scePadRead_nid_postfix(handle, drained, 64); + Require(scePadClose_nid_postfix(remote) == PAD_OK); + Require(scePadGetHandle(PAD_USER_ID_SYSTEM, PAD_PORT_TYPE_REMOTE, 0) == noHandle); + Require(scePadReadState(remote, &data) == PAD_ERROR_INVALID_HANDLE); + Require(scePadReadState(handle, &data) == PAD_OK); +} + static void CheckAccessibilityControllerState(int handle) { std::uint32_t state = 7; Require(scePadGetAccessibilityControllerState(handle + 1, nullptr) == PAD_ERROR_INVALID_ARG); @@ -429,6 +466,15 @@ static void CheckAccessibilityControllerState(int handle) { Require(state == 0); } +static void CheckControllerType(int handle) { + std::int32_t type = -1; + Require(scePadGetControllerType(handle + 1, nullptr) == PAD_ERROR_INVALID_ARG); + Require(scePadGetControllerType(handle + 1, &type) == PAD_ERROR_INVALID_HANDLE); + Require(type == -1); + Require(scePadGetControllerType(handle, &type) == PAD_OK); + Require(type == PAD_CONTROLLER_TYPE_DUAL_SENSE); +} + static void CheckInjectedState(const std::filesystem::path& path) { PadInputState physical; physical.buttons = 0x2000; @@ -487,12 +533,53 @@ static void CheckInjectedState(const std::filesystem::path& path) { PadPublishInput_nid_postfix(PadInputState{}); } +static void CheckInjectedQueueTimes(const std::filesystem::path& path) { + PadData drained[64]{}; + Pad::Read(drained, 64); + PadInputState physical; + physical.buttons = 0x2000; + PadPublishInput_nid_postfix(physical); + const auto before = Pad::ReadState(); + std::ofstream(path) << "5 16384 300\n"; + PadData pressed{}; + const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(2); + do { + pressed = Pad::ReadState(); + if ((pressed.buttons & 0x4000) != 0) break; + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + } while (std::chrono::steady_clock::now() < deadline); + Require(pressed.buttons == 0x6000 && pressed.timestamp > before.timestamp); + PadData queued{}; + Require(Pad::Read(&queued, 1) == 1); + Require(queued.buttons == pressed.buttons); + if (queued.timestamp < pressed.timestamp) { + std::fputs("queued input predates its injected button press\n", stderr); + std::abort(); + } + physical.sticks[0] = 130; + PadPublishInput_nid_postfix(physical); + std::this_thread::sleep_for(std::chrono::milliseconds(310)); + const auto released = Pad::ReadState(); + Require(released.buttons == 0x2000 && released.timestamp > pressed.timestamp); + Require(Pad::Read(&queued, 1) == 1); + Require(queued.buttons == released.buttons && queued.left_stick_x == 130); + if (queued.timestamp < released.timestamp) { + std::fputs("queued input predates its injected button release\n", stderr); + std::abort(); + } + PadPublishInput_nid_postfix(PadInputState{}); +} + int main() { CheckInputCommands(); std::uint32_t accessibilityState = 7; Require(scePadGetAccessibilityControllerState(PAD_HANDLE, nullptr) == PAD_ERROR_NOT_INITIALIZED); Require(scePadGetAccessibilityControllerState(PAD_HANDLE, &accessibilityState) == PAD_ERROR_NOT_INITIALIZED); Require(accessibilityState == 7); + std::int32_t controllerType = -1; + Require(scePadGetControllerType(PAD_HANDLE, nullptr) == PAD_ERROR_NOT_INITIALIZED); + Require(scePadGetControllerType(PAD_HANDLE, &controllerType) == PAD_ERROR_NOT_INITIALIZED); + Require(controllerType == -1); Require(scePadUnknown_n3kSX62fgNo() == static_cast(0x80920005)); const auto path = std::filesystem::temp_directory_path() / ("anyps5-pad-state-" + std::to_string(std::chrono::steady_clock::now().time_since_epoch().count())); struct Cleanup { @@ -547,7 +634,14 @@ int main() { Require(scePadGetHandle(user, PAD_PORT_TYPE_SPECIAL, 0) == earlySpecial); Require(scePadClose_nid_postfix(earlySpecial) == PAD_OK); Require(scePadGetHandle(user, PAD_PORT_TYPE_SPECIAL, 0) == noHandle); + PadSetPortConnected_nid_postfix(1, true); Require(scePadInit_nid_postfix() == 0); + PadInfo initInfo{}; + Require(scePadGetInfo_nid_postfix(&initInfo) == PAD_OK); + Require(initInfo.connectedCount[0] == 2); + Require(initInfo.padTypeInfo[0].connectPort == 0); + Require(initInfo.padTypeInfo[1].connectPort == 1); + PadSetPortConnected_nid_postfix(1, false); Require(scePadUnknown_n3kSX62fgNo() == PAD_OK); const int handle = scePadOpen_nid_postfix(user, 0, 0, nullptr); Require(handle > 0); @@ -568,7 +662,9 @@ int main() { CheckRemoteController(handle); CheckTrackerDevices(handle); CheckAccessibilityControllerState(handle); + CheckControllerType(handle); CheckInjectedState(commandPath); + CheckInjectedQueueTimes(commandPath); PadLightBarParam lightBar{255, 128, 64}; Require(scePadSetLightBar(handle, &lightBar) == PAD_OK); Require(scePadSetLightBar(handle, nullptr) == PAD_ERROR_INVALID_ARG); @@ -585,7 +681,7 @@ int main() { Require(scePadSetVibrationMode(handle, 5) == 0); Require(scePadSetVibrationMode(handle, 6) == PAD_ERROR_INVALID_ARG); Require(scePadSetVibrationMode(handle, -1) == PAD_ERROR_INVALID_ARG); - Require(scePadGetHandle(0xff, 16, 0) == handle); + CheckSystemRemote(handle); Require(scePadGetHandle(user, 16, 0) == noHandle); Require(scePadGetHandle(user, 0, 1) == noHandle); Require(scePadClose_nid_postfix(handle) == 0); @@ -597,4 +693,42 @@ int main() { Require(scePadSetAngularVelocityDeadbandState(handle + 1, true) == PAD_ERROR_INVALID_HANDLE); Require(scePadSetAngularVelocityBiasCorrectionState(handle, false) == PAD_OK); Require(scePadSetAngularVelocityBiasCorrectionState(handle + 1, false) == PAD_ERROR_INVALID_HANDLE); + PadInfo info{}; + Require(scePadGetInfo_nid_postfix(&info) == PAD_OK); + Require(info.maxConnectCount == PAD_MAX_CONTROLLERS); + Require(info.connectedCount[0] == 1); + Require(info.padTypeInfo[0].connectPort == 0); + + const int user2 = 0x10000001; + Require(scePadGetHandle(user2, 0, 0) == noHandle); + const int handle2 = scePadOpen_nid_postfix(user2, 0, 0, nullptr); + Require(handle2 == PAD_HANDLE_BASE + 1); + Require(scePadGetHandle(user2, 0, 0) == handle2); + + PadInputState p2Input{}; + p2Input.buttons = static_cast(Pad::PadButton::Triangle); + PadPublishInputForPort_nid_postfix(1, p2Input); + PadData p2Data{}; + Require(scePadReadState(handle2, &p2Data) == 0); + Require((p2Data.buttons & static_cast(Pad::PadButton::Triangle)) != 0); + + Require(scePadSetVibrationMode(handle2, 1) == 0); + std::uint32_t p2AccessState = 7; + Require(scePadGetAccessibilityControllerState(handle2, &p2AccessState) == PAD_OK); + Require(p2AccessState == 0); + + PadSetPortConnected_nid_postfix(1, true); + Require(scePadGetInfo_nid_postfix(&info) == PAD_OK); + Require(info.connectedCount[0] == 2); + Require(info.padTypeInfo[0].connectPort == 0); + Require(info.padTypeInfo[1].connectPort == 1); + PadSetPortConnected_nid_postfix(1, false); + Require(scePadGetInfo_nid_postfix(&info) == PAD_OK); + Require(info.connectedCount[0] == 1); + Require(info.padTypeInfo[0].connectPort == 0); + + Require(scePadClose_nid_postfix(handle2) == 0); + Require(scePadGetHandle(user2, 0, 0) == noHandle); + Require(scePadSetAngularVelocityDeadbandState(handle2, false) == PAD_OK); + Require(scePadSetAngularVelocityDeadbandState(handle2 + 1, false) == PAD_ERROR_INVALID_HANDLE); } diff --git a/core/libs/tests/GuestPageTableStats.cpp b/core/libs/tests/GuestPageTableStats.cpp index eba38dd12c..2ef3d599be 100644 --- a/core/libs/tests/GuestPageTableStats.cpp +++ b/core/libs/tests/GuestPageTableStats.cpp @@ -9,6 +9,7 @@ extern "C" { int APS5_VABI sceKernelGetPageTableStats(int*, int*, int*, int*); +int APS5_VABI sceKernelGetTraceMemoryStats(std::uint64_t*, std::uint64_t*); int APS5_VABI sceKernelMapFlexibleMemory(void**, std::size_t, int, int); int APS5_VABI sceKernelMapDirectMemory(void**, std::size_t, int, int, std::int64_t, std::size_t); int APS5_VABI sceKernelAllocateDirectMemory(std::int64_t, std::int64_t, std::size_t, std::size_t, int, std::int64_t*); @@ -111,8 +112,21 @@ static void CheckGpuDirectMapping() { RequireTaken(before, 0, 0); } +static void CheckTraceMemoryStats() { + std::uint64_t first[2] = {0xaaaaaaaaaaaaaaaaull, 0xaaaaaaaaaaaaaaaaull}; + std::uint64_t second[2] = {0xaaaaaaaaaaaaaaaaull, 0xaaaaaaaaaaaaaaaaull}; + Require(sceKernelGetTraceMemoryStats(first, second) == 0); + Require(first[0] == 0 && first[1] == 0xaaaaaaaaaaaaaaaaull && second[0] == 0 && second[1] == 0xaaaaaaaaaaaaaaaaull); + first[0] = 1; + Require(sceKernelGetTraceMemoryStats(first, nullptr) == 0 && first[0] == 0); + second[0] = 1; + Require(sceKernelGetTraceMemoryStats(nullptr, second) == 0 && second[0] == 0); + Require(sceKernelGetTraceMemoryStats(nullptr, nullptr) == 0); +} + int main() { CheckRejectedArguments(); + CheckTraceMemoryStats(); CheckCpuGranules(); CheckGpuEntries(); CheckGpuDirectMapping(); diff --git a/core/libs/tests/GuestParentProcess.cpp b/core/libs/tests/GuestParentProcess.cpp index 203cfe3a00..3465efc6aa 100644 --- a/core/libs/tests/GuestParentProcess.cpp +++ b/core/libs/tests/GuestParentProcess.cpp @@ -5,10 +5,13 @@ #else #include #endif + extern "C" { int APS5_VABI getpid_nid_postfix(void); int APS5_VABI getppid_nid_postfix(void); int APS5_VABI getsid_nid_postfix(int); +int APS5_VABI setpgid_nid_postfix(int, int); +int APS5_VABI setpgrp_nid_postfix(int, int); int* APS5_VABI __error_nid_postfix(); } static void Require(bool value) { if (!value) std::abort(); } @@ -21,6 +24,72 @@ int main() { Require(*__error_nid_postfix() == 3); Require(getsid_nid_postfix(-1) == -1); Require(*__error_nid_postfix() == 3); + *__error_nid_postfix() = 0; + Require(setpgid_nid_postfix(0, 0) == -1); + Require(*__error_nid_postfix() == 1); + *__error_nid_postfix() = 0; + Require(setpgid_nid_postfix(self, 0) == -1); + Require(*__error_nid_postfix() == 1); + *__error_nid_postfix() = 0; + Require(setpgid_nid_postfix(0, self) == -1); + Require(*__error_nid_postfix() == 1); + *__error_nid_postfix() = 0; + Require(setpgid_nid_postfix(self, self) == -1); + Require(*__error_nid_postfix() == 1); + *__error_nid_postfix() = 0; + Require(setpgid_nid_postfix(0, 123456) == -1); + Require(*__error_nid_postfix() == 1); + *__error_nid_postfix() = 0; + Require(setpgid_nid_postfix(self, 123456) == -1); + Require(*__error_nid_postfix() == 1); + *__error_nid_postfix() = 0; + Require(setpgid_nid_postfix(-1, -1) == -1); + Require(*__error_nid_postfix() == 22); + *__error_nid_postfix() = 0; + Require(setpgid_nid_postfix(0, -1) == -1); + Require(*__error_nid_postfix() == 22); + *__error_nid_postfix() = 0; + Require(setpgid_nid_postfix(self, -1) == -1); + Require(*__error_nid_postfix() == 22); + *__error_nid_postfix() = 0; + Require(setpgid_nid_postfix(-1, 0) == -1); + Require(*__error_nid_postfix() == 3); + *__error_nid_postfix() = 0; + Require(setpgid_nid_postfix(123456, 0) == -1); + Require(*__error_nid_postfix() == 3); + *__error_nid_postfix() = 0; + Require(setpgrp_nid_postfix(0, 0) == -1); + Require(*__error_nid_postfix() == 1); + *__error_nid_postfix() = 0; + Require(setpgrp_nid_postfix(self, 0) == -1); + Require(*__error_nid_postfix() == 1); + *__error_nid_postfix() = 0; + Require(setpgrp_nid_postfix(0, self) == -1); + Require(*__error_nid_postfix() == 1); + *__error_nid_postfix() = 0; + Require(setpgrp_nid_postfix(self, self) == -1); + Require(*__error_nid_postfix() == 1); + *__error_nid_postfix() = 0; + Require(setpgrp_nid_postfix(0, 123456) == -1); + Require(*__error_nid_postfix() == 1); + *__error_nid_postfix() = 0; + Require(setpgrp_nid_postfix(self, 123456) == -1); + Require(*__error_nid_postfix() == 1); + *__error_nid_postfix() = 0; + Require(setpgrp_nid_postfix(-1, -1) == -1); + Require(*__error_nid_postfix() == 22); + *__error_nid_postfix() = 0; + Require(setpgrp_nid_postfix(0, -1) == -1); + Require(*__error_nid_postfix() == 22); + *__error_nid_postfix() = 0; + Require(setpgrp_nid_postfix(self, -1) == -1); + Require(*__error_nid_postfix() == 22); + *__error_nid_postfix() = 0; + Require(setpgrp_nid_postfix(-1, 0) == -1); + Require(*__error_nid_postfix() == 3); + *__error_nid_postfix() = 0; + Require(setpgrp_nid_postfix(123456, 0) == -1); + Require(*__error_nid_postfix() == 3); const int parent = getppid_nid_postfix(); Require(parent > 0 && parent != getpid_nid_postfix()); Require(getppid_nid_postfix() == parent); diff --git a/core/libs/tests/GuestPoll.cpp b/core/libs/tests/GuestPoll.cpp index 7be6d0675c..328a7e722e 100644 --- a/core/libs/tests/GuestPoll.cpp +++ b/core/libs/tests/GuestPoll.cpp @@ -7,6 +7,7 @@ #include #endif #include "prx/libc/include/general/VabiMacros.hpp" +#include "SceTypes.hpp" #include #include #include @@ -39,6 +40,7 @@ int APS5_VABI unlink_nid_postfix(const char*); int APS5_VABI poll_nid_postfix(PollDescriptor*, std::uint32_t, int); int APS5_VABI pipe_nid_postfix(int*); int APS5_VABI kqueue_nid_postfix(); +int APS5_VABI kevent_nid_postfix(int, const KernelEvent*, int, KernelEvent*, int, const KernelTimespec*); std::int64_t APS5_VABI read_nid_postfix(int, void*, std::uint64_t); std::int64_t APS5_VABI write_nid_postfix(int, const char*, std::int64_t); int APS5_VABI fcntl_nid_postfix(int, int, ...); @@ -62,6 +64,46 @@ constexpr short Hup = 0x10; constexpr short Invalid = 0x20; constexpr short ReadNormal = 0x40; constexpr short WriteBand = 0x100; +constexpr std::int16_t EvfiltUser = -11; +constexpr std::uint16_t EvAdd = 0x0001; +constexpr std::uint16_t EvClear = 0x0020; +constexpr std::uint32_t NoteTrigger = 0x01000000; +static KernelEvent UserEvent(std::uint16_t flags, std::uint32_t fflags) { + KernelEvent change{}; + change.ident = 7; + change.filter = EvfiltUser; + change.flags = flags; + change.fflags = fflags; + return change; +} +static void PollsKqueues(int socket) { + const int queue = kqueue_nid_postfix(); + Require(queue >= 0); + PollDescriptor kq{queue, static_cast(In | ReadNormal | Out | Priority | ReadBand | WriteBand), -1}; + Require(poll_nid_postfix(&kq, 1, 0) == 0 && kq.revents == 0); + const auto added = UserEvent(EvAdd | EvClear, 0); + Require(kevent_nid_postfix(queue, &added, 1, nullptr, 0, nullptr) == 0); + Require(poll_nid_postfix(&kq, 1, 0) == 0 && kq.revents == 0); + const auto start = std::chrono::steady_clock::now(); + Require(poll_nid_postfix(&kq, 1, 30) == 0 && kq.revents == 0); + Require(std::chrono::steady_clock::now() - start >= std::chrono::milliseconds(25)); + std::thread trigger([queue] { + std::this_thread::sleep_for(std::chrono::milliseconds(20)); + const auto triggered = UserEvent(0, NoteTrigger); + Require(kevent_nid_postfix(queue, &triggered, 1, nullptr, 0, nullptr) == 0); + }); + PollDescriptor mixed[2]{{socket, In, -1}, {queue, static_cast(In | Out), -1}}; + Require(poll_nid_postfix(mixed, 2, -1) == 1); + trigger.join(); + Require(mixed[0].revents == 0 && mixed[1].revents == In); + Require(poll_nid_postfix(&kq, 1, 0) == 1 && kq.revents == (In | ReadNormal)); + KernelEvent events[1]{}; + const KernelTimespec zero{0, 0}; + Require(kevent_nid_postfix(queue, nullptr, 0, events, 1, &zero) == 1); + Require(poll_nid_postfix(&kq, 1, 0) == 0 && kq.revents == 0); + Require(close_nid_postfix(queue) == 0); + Require(poll_nid_postfix(&kq, 1, 0) == 1 && kq.revents == Invalid); +} static void PollsFilesAndDirectories(int socket) { constexpr int ReadWrite = 0x0002, Create = 0x0200, DirectoryFlag = 0x00020000; const auto stamp = std::to_string(std::chrono::steady_clock::now().time_since_epoch().count()); @@ -309,16 +351,6 @@ int main() { const auto inputStart = std::chrono::steady_clock::now(); Require(poll_nid_postfix(&input, 1, 30) == 0 && input.revents == 0); Require(std::chrono::steady_clock::now() - inputStart >= std::chrono::milliseconds(25)); - const int queue = kqueue_nid_postfix(); - Require(queue >= 0); - PollDescriptor unknown[2]{{listener, In, 0}, {queue, In, 0}}; - bool unsupported = false; - try { - poll_nid_postfix(unknown, 2, 0); - } catch (const std::runtime_error&) { - unsupported = true; - } - Require(unsupported); - Require(close_nid_postfix(queue) == 0); + PollsKqueues(listener); Require(close_nid_postfix(listener) == 0); } diff --git a/core/libs/tests/GuestPosixNamedSem.cpp b/core/libs/tests/GuestPosixNamedSem.cpp new file mode 100644 index 0000000000..0a7de1f321 --- /dev/null +++ b/core/libs/tests/GuestPosixNamedSem.cpp @@ -0,0 +1,136 @@ +#include "SceTypes.hpp" +#include "prx/libc/include/general/VabiMacros.hpp" +#include +#include +#include +#include +#include +#include + +extern "C" { +void* APS5_VABI sem_open_nid_postfix(const char* name, int flags, ...); +int APS5_VABI sem_close_nid_postfix(void* sem); +int APS5_VABI sem_unlink_nid_postfix(const char* name); +int APS5_VABI sem_init_nid_postfix(void* sem, int pshared, unsigned int value); +int APS5_VABI sem_destroy_nid_postfix(void* sem); +int APS5_VABI sem_wait_nid_postfix(void* sem); +int APS5_VABI sem_trywait_nid_postfix(void* sem); +int APS5_VABI sem_post_nid_postfix(void* sem); +int APS5_VABI sem_getvalue_nid_postfix(void* sem, int* value); +int* APS5_VABI __error_nid_postfix(); +} + +static constexpr int GUEST_ENOENT = 2; +static constexpr int GUEST_EEXIST = 17; +static constexpr int GUEST_EINVAL = 22; +static constexpr int GUEST_EAGAIN = 35; +static constexpr int GUEST_ENAMETOOLONG = 63; +static constexpr int O_CREAT_FLAG = 0x200; +static constexpr int O_EXCL_FLAG = 0x800; + +static void Require(bool condition, int line) { + if (!condition) { + std::fprintf(stderr, "line %d failed, errno=%d\n", line, *__error_nid_postfix()); + std::abort(); + } +} +#define REQUIRE(condition) Require(condition, __LINE__) + +template +static bool Throws(Call call) { + try { + call(); + } catch (const std::exception&) { + return true; + } + return false; +} + +static bool OpenFails(const char* name, int flags, int error) { + *__error_nid_postfix() = 0; + return sem_open_nid_postfix(name, flags, 0600, 1) == nullptr && *__error_nid_postfix() == error; +} + +static bool UnlinkFails(const char* name, int error) { + *__error_nid_postfix() = 0; + return sem_unlink_nid_postfix(name) == -1 && *__error_nid_postfix() == error; +} + +static int Value(void* sem) { + int value = -1; + REQUIRE(sem_getvalue_nid_postfix(sem, &value) == 0); + return value; +} + +static void CheckNames() { + REQUIRE(OpenFails("relative", O_CREAT_FLAG, GUEST_EINVAL)); + REQUIRE(OpenFails("/anyps5-named-flags", 0x1, GUEST_EINVAL)); + REQUIRE(OpenFails("/anyps5-named-flags", O_CREAT_FLAG | 0x2, GUEST_EINVAL)); + REQUIRE(OpenFails("/anyps5-named-missing", 0, GUEST_ENOENT)); + REQUIRE(OpenFails("/anyps5-named-missing", O_EXCL_FLAG, GUEST_ENOENT)); + const std::string longPath = "/" + std::string(1015, 'p'); + REQUIRE(OpenFails(longPath.c_str(), O_CREAT_FLAG, GUEST_ENAMETOOLONG)); + REQUIRE(UnlinkFails(longPath.c_str(), GUEST_ENAMETOOLONG)); + const std::string longFile = "/" + std::string(252, 'f'); + REQUIRE(OpenFails(longFile.c_str(), O_CREAT_FLAG, GUEST_ENAMETOOLONG)); + REQUIRE(OpenFails(longFile.c_str(), 0x1, GUEST_EINVAL)); + REQUIRE(UnlinkFails(longFile.c_str(), GUEST_ENAMETOOLONG)); + const std::string longestFile = "/" + std::string(251, 'f'); + void* longest = sem_open_nid_postfix(longestFile.c_str(), O_CREAT_FLAG, 0600, 0); + REQUIRE(longest != nullptr && sem_close_nid_postfix(longest) == 0 && sem_unlink_nid_postfix(longestFile.c_str()) == 0); + REQUIRE(UnlinkFails("relative", GUEST_ENOENT)); + REQUIRE(UnlinkFails("/anyps5-named-missing", GUEST_ENOENT)); + REQUIRE(Throws([] { sem_open_nid_postfix(nullptr, 0); })); + REQUIRE(Throws([] { sem_unlink_nid_postfix(nullptr); })); +} + +static void CheckLifetime() { + const char* name = "/anyps5-named-lifetime"; + void* sem = sem_open_nid_postfix(name, O_CREAT_FLAG | O_EXCL_FLAG, 0600, 2); + REQUIRE(sem != nullptr && Value(sem) == 2); + REQUIRE(sem_open_nid_postfix(name, 0) == sem); + REQUIRE(sem_open_nid_postfix(name, O_CREAT_FLAG, 0600, 9) == sem && Value(sem) == 2); + REQUIRE(OpenFails(name, O_CREAT_FLAG | O_EXCL_FLAG, GUEST_EEXIST)); + REQUIRE(sem_trywait_nid_postfix(sem) == 0 && sem_trywait_nid_postfix(sem) == 0); + *__error_nid_postfix() = 0; + REQUIRE(sem_trywait_nid_postfix(sem) == -1 && *__error_nid_postfix() == GUEST_EAGAIN); + std::thread poster([name] { + void* other = sem_open_nid_postfix(name, 0); + REQUIRE(other != nullptr && sem_post_nid_postfix(other) == 0 && sem_close_nid_postfix(other) == 0); + }); + REQUIRE(sem_wait_nid_postfix(sem) == 0); + poster.join(); + REQUIRE(sem_post_nid_postfix(sem) == 0 && sem_post_nid_postfix(sem) == 0 && Value(sem) == 2); + *__error_nid_postfix() = 0; + REQUIRE(sem_destroy_nid_postfix(sem) == -1 && *__error_nid_postfix() == GUEST_EINVAL && Value(sem) == 2); + for (int open = 0; open < 3; ++open) REQUIRE(sem_close_nid_postfix(sem) == 0); + *__error_nid_postfix() = 0; + REQUIRE(sem_close_nid_postfix(sem) == -1 && *__error_nid_postfix() == GUEST_EINVAL); + + void* reopened = sem_open_nid_postfix(name, 0); + REQUIRE(reopened != nullptr && Value(reopened) == 2); + REQUIRE(sem_unlink_nid_postfix(name) == 0); + REQUIRE(UnlinkFails(name, GUEST_ENOENT)); + REQUIRE(OpenFails(name, 0, GUEST_ENOENT)); + REQUIRE(sem_post_nid_postfix(reopened) == 0 && Value(reopened) == 3); + void* recreated = sem_open_nid_postfix(name, O_CREAT_FLAG | O_EXCL_FLAG, 0600, 5); + REQUIRE(recreated != nullptr && recreated != reopened && Value(recreated) == 5 && Value(reopened) == 3); + REQUIRE(sem_close_nid_postfix(reopened) == 0); + REQUIRE(sem_close_nid_postfix(recreated) == 0 && sem_unlink_nid_postfix(name) == 0); +} + +static void CheckUnnamed() { + std::uintptr_t storage[2] = {}; + REQUIRE(sem_init_nid_postfix(storage, 0, 1) == 0); + *__error_nid_postfix() = 0; + REQUIRE(sem_close_nid_postfix(storage) == -1 && *__error_nid_postfix() == GUEST_EINVAL); + REQUIRE(Value(storage) == 1 && sem_destroy_nid_postfix(storage) == 0); + REQUIRE(Throws([] { sem_close_nid_postfix(nullptr); })); +} + +int main() { + CheckNames(); + CheckLifetime(); + CheckUnnamed(); + return 0; +} diff --git a/core/libs/tests/GuestPthreadCondattr.cpp b/core/libs/tests/GuestPthreadCondattr.cpp index 237ac4374e..a05d017c80 100644 --- a/core/libs/tests/GuestPthreadCondattr.cpp +++ b/core/libs/tests/GuestPthreadCondattr.cpp @@ -10,6 +10,8 @@ int APS5_VABI scePthreadCondattrSetclock(PthreadCondattr* attr, KernelClockid cl int APS5_VABI pthread_condattr_setclock_nid_postfix(PthreadCondattr* attr, KernelClockid clockId); int APS5_VABI pthread_condattr_setpshared_nid_postfix(PthreadCondattr* attr, int pshared); int APS5_VABI pthread_condattr_getpshared_nid_postfix(const PthreadCondattr* attr, int* pshared); +int APS5_VABI scePthreadCondattrSetpshared(PthreadCondattr* attr, int pshared); +int APS5_VABI scePthreadCondattrGetpshared(const PthreadCondattr* attr, int* pshared); } static constexpr int SCE_OK = 0; @@ -31,6 +33,8 @@ int main() { Require(pthread_condattr_setpshared_nid_postfix(&unset, 0) == GuestEinval); Require(pthread_condattr_getpshared_nid_postfix(nullptr, &sharing) == GuestEinval && sharing == -1); Require(pthread_condattr_getpshared_nid_postfix(&unset, &sharing) == GuestEinval && sharing == -1); + Require(scePthreadCondattrSetpshared(&unset, 0) == SceEinval); + Require(scePthreadCondattrGetpshared(nullptr, &sharing) == SceEinval && sharing == -1); PthreadCondattr attr = nullptr; Require(scePthreadCondattrInit(&attr) == SCE_OK); @@ -38,6 +42,9 @@ int main() { Require(scePthreadCondattrSetclock(&attr, clock) == SCE_OK); Require(pthread_condattr_setclock_nid_postfix(&attr, clock) == 0); } + Require(scePthreadCondattrSetpshared(&attr, 1) == SceEinval); + Require(scePthreadCondattrSetpshared(&attr, 0) == SCE_OK); + Require(scePthreadCondattrGetpshared(&attr, &sharing) == SCE_OK && sharing == 0); Require(pthread_condattr_getpshared_nid_postfix(&attr, &sharing) == 0 && sharing == 0); for (const int invalid : {1, 2, -1}) { Require(pthread_condattr_setpshared_nid_postfix(&attr, invalid) == GuestEinval); diff --git a/core/libs/tests/GuestPthreadMutexattr.cpp b/core/libs/tests/GuestPthreadMutexattr.cpp index 48dd291a5b..859dc770e1 100644 --- a/core/libs/tests/GuestPthreadMutexattr.cpp +++ b/core/libs/tests/GuestPthreadMutexattr.cpp @@ -15,6 +15,19 @@ int APS5_VABI pthread_mutexattr_setprioceiling_nid_postfix(PthreadMutexattr* att int APS5_VABI pthread_mutexattr_getprioceiling_nid_postfix(const PthreadMutexattr* attr, int* prioceiling); int APS5_VABI pthread_mutexattr_settype_nid_postfix(PthreadMutexattr* attr, int type); int APS5_VABI pthread_mutexattr_gettype_nid_postfix(const PthreadMutexattr* attr, int* type); +int APS5_VABI pthread_mutexattr_setkind_np_nid_postfix(PthreadMutexattr* attr, int kind); +int APS5_VABI pthread_mutexattr_getkind_np_nid_postfix(PthreadMutexattr attr); +int APS5_VABI scePthreadMutexattrInit(PthreadMutexattr* attr); +int APS5_VABI scePthreadMutexattrDestroy(PthreadMutexattr* attr); +int APS5_VABI scePthreadMutexattrSettype(PthreadMutexattr* attr, int type); +int APS5_VABI scePthreadMutexattrGettype(const PthreadMutexattr* attr, int* type); +int APS5_VABI scePthreadMutexattrSetprotocol(PthreadMutexattr* attr, int protocol); +int APS5_VABI scePthreadMutexattrGetprotocol(const PthreadMutexattr* attr, int* protocol); +int APS5_VABI scePthreadMutexattrSetprioceiling(PthreadMutexattr* attr, int prioceiling); +int APS5_VABI scePthreadMutexattrGetprioceiling(const PthreadMutexattr* attr, int* prioceiling); +int APS5_VABI scePthreadMutexattrSetpshared(PthreadMutexattr* attr, int pshared); +int APS5_VABI scePthreadMutexattrGetpshared(const PthreadMutexattr* attr, int* pshared); +int* APS5_VABI __error_nid_postfix(); int APS5_VABI pthread_mutex_init_nid_postfix(PthreadMutex* mutex, const PthreadMutexattr* attr); int APS5_VABI pthread_mutex_lock_nid_postfix(PthreadMutex* mutex); int APS5_VABI pthread_mutex_unlock_nid_postfix(PthreadMutex* mutex); @@ -26,6 +39,8 @@ int APS5_VABI sched_get_priority_max_nid_postfix(int policy); } static constexpr int GUEST_EINVAL = 22; +static constexpr int SCE_OK = 0; +static constexpr int SCE_KERNEL_ERROR_EINVAL = static_cast(0x80020016u); static constexpr int PRIO_NONE = 0; static constexpr int PRIO_INHERIT = 1; static constexpr int PRIO_PROTECT = 2; @@ -80,6 +95,78 @@ static void CheckType() { Require(pthread_mutexattr_gettype_nid_postfix(nullptr, &type) == GUEST_EINVAL && type == -1); } +static void CheckKind() { + PthreadMutexattr attr = nullptr; + *__error_nid_postfix() = 0; + Require(pthread_mutexattr_getkind_np_nid_postfix(nullptr) == -1 && *__error_nid_postfix() == GUEST_EINVAL); + *__error_nid_postfix() = 0; + Require(pthread_mutexattr_setkind_np_nid_postfix(nullptr, 2) == -1 && *__error_nid_postfix() == GUEST_EINVAL); + *__error_nid_postfix() = 0; + Require(pthread_mutexattr_setkind_np_nid_postfix(&attr, 2) == -1 && *__error_nid_postfix() == GUEST_EINVAL); + Require(pthread_mutexattr_init_nid_postfix(&attr) == 0); + Require(pthread_mutexattr_getkind_np_nid_postfix(attr) == 1); + int type = -1; + for (const int kind : {2, 3, 4, 1}) { + Require(pthread_mutexattr_setkind_np_nid_postfix(&attr, kind) == 0); + Require(pthread_mutexattr_getkind_np_nid_postfix(attr) == kind); + Require(pthread_mutexattr_gettype_nid_postfix(&attr, &type) == 0 && type == kind); + } + Require(pthread_mutexattr_settype_nid_postfix(&attr, 2) == 0); + Require(pthread_mutexattr_getkind_np_nid_postfix(attr) == 2); + for (const int invalid : {0, 5, -1}) + Require(Throws([&] { pthread_mutexattr_setkind_np_nid_postfix(&attr, invalid); })); + Require(pthread_mutexattr_getkind_np_nid_postfix(attr) == 2); + PthreadMutex mutex = nullptr; + Require(pthread_mutex_init_nid_postfix(&mutex, &attr) == 0); + Require(pthread_mutex_lock_nid_postfix(&mutex) == 0); + Require(pthread_mutex_lock_nid_postfix(&mutex) == 0); + Require(pthread_mutex_unlock_nid_postfix(&mutex) == 0); + Require(pthread_mutex_unlock_nid_postfix(&mutex) == 0); + Require(pthread_mutex_destroy_nid_postfix(&mutex) == 0); + Require(pthread_mutexattr_destroy_nid_postfix(&attr) == 0); + *__error_nid_postfix() = 0; + Require(pthread_mutexattr_setkind_np_nid_postfix(&attr, 2) == -1 && *__error_nid_postfix() == GUEST_EINVAL); +} + +static void CheckSceAccessors() { + PthreadMutexattr attr = nullptr; + int value = -1; + Require(scePthreadMutexattrGettype(nullptr, &value) == SCE_KERNEL_ERROR_EINVAL); + Require(scePthreadMutexattrGettype(&attr, &value) == SCE_KERNEL_ERROR_EINVAL); + Require(scePthreadMutexattrSetprotocol(&attr, PRIO_NONE) == SCE_KERNEL_ERROR_EINVAL); + Require(scePthreadMutexattrGetprotocol(&attr, &value) == SCE_KERNEL_ERROR_EINVAL); + Require(scePthreadMutexattrSetprioceiling(&attr, 300) == SCE_KERNEL_ERROR_EINVAL); + Require(scePthreadMutexattrGetprioceiling(&attr, &value) == SCE_KERNEL_ERROR_EINVAL); + Require(scePthreadMutexattrSetpshared(nullptr, 0) == SCE_KERNEL_ERROR_EINVAL); + Require(scePthreadMutexattrGetpshared(&attr, &value) == SCE_KERNEL_ERROR_EINVAL); + Require(value == -1); + Require(scePthreadMutexattrInit(&attr) == SCE_OK); + Require(scePthreadMutexattrGettype(&attr, &value) == SCE_OK && value == 1); + Require(scePthreadMutexattrSettype(&attr, 3) == SCE_OK); + Require(scePthreadMutexattrGettype(&attr, &value) == SCE_OK && value == 3); + Require(Throws([&] { scePthreadMutexattrGettype(&attr, nullptr); })); + Require(scePthreadMutexattrGetprotocol(&attr, &value) == SCE_OK && value == PRIO_NONE); + for (const int invalid : {-1, 3}) + Require(scePthreadMutexattrSetprotocol(&attr, invalid) == SCE_KERNEL_ERROR_EINVAL); + Require(scePthreadMutexattrGetprotocol(&attr, &value) == SCE_OK && value == PRIO_NONE); + Require(scePthreadMutexattrSetprotocol(&attr, PRIO_INHERIT) == SCE_OK); + Require(scePthreadMutexattrGetprotocol(&attr, &value) == SCE_OK && value == PRIO_INHERIT); + Require(scePthreadMutexattrSetprioceiling(&attr, 300) == SCE_KERNEL_ERROR_EINVAL); + Require(scePthreadMutexattrGetprioceiling(&attr, &value) == SCE_KERNEL_ERROR_EINVAL); + Require(scePthreadMutexattrSetprotocol(&attr, PRIO_PROTECT) == SCE_OK); + Require(scePthreadMutexattrGetprioceiling(&attr, &value) == SCE_OK && value == sched_get_priority_max_nid_postfix(SCHED_RR)); + Require(scePthreadMutexattrSetprioceiling(&attr, 300) == SCE_OK); + Require(scePthreadMutexattrGetprioceiling(&attr, &value) == SCE_OK && value == 300); + int protocol = -1; + Require(pthread_mutexattr_getprotocol_nid_postfix(&attr, &protocol) == 0 && protocol == PRIO_PROTECT); + Require(scePthreadMutexattrGetpshared(&attr, &value) == SCE_OK && value == 0); + Require(scePthreadMutexattrSetpshared(&attr, 1) == SCE_KERNEL_ERROR_EINVAL); + Require(scePthreadMutexattrSetpshared(&attr, 0) == SCE_OK); + Require(scePthreadMutexattrGetpshared(&attr, &value) == SCE_OK && value == 0); + Require(scePthreadMutexattrDestroy(&attr) == SCE_OK); + Require(scePthreadMutexattrGettype(&attr, &value) == SCE_KERNEL_ERROR_EINVAL); +} + static void CheckProcessSharing() { PthreadMutexattr attr = nullptr; int sharing = -1; @@ -127,6 +214,8 @@ static void CheckInvalidPriorityAttribute(PthreadMutexattr* attr) { int main() { CheckType(); + CheckKind(); + CheckSceAccessors(); CheckProcessSharing(); CheckTimedlockInvalidDeadline(); PthreadMutexattr attr = nullptr; diff --git a/core/libs/tests/GuestPthreadRwlock.cpp b/core/libs/tests/GuestPthreadRwlock.cpp index 91c219a1cb..9c8c6d557c 100644 --- a/core/libs/tests/GuestPthreadRwlock.cpp +++ b/core/libs/tests/GuestPthreadRwlock.cpp @@ -16,6 +16,11 @@ int APS5_VABI pthread_rwlockattr_init_nid_postfix(PthreadRwlockattr* attr); int APS5_VABI pthread_rwlockattr_destroy_nid_postfix(PthreadRwlockattr* attr); int APS5_VABI pthread_rwlockattr_getpshared_nid_postfix(const PthreadRwlockattr* attr, int* pshared); int APS5_VABI pthread_rwlockattr_setpshared_nid_postfix(PthreadRwlockattr* attr, int pshared); +int APS5_VABI pthread_rwlockattr_gettype_np_nid_postfix(const PthreadRwlockattr* attr, int* type); +int APS5_VABI scePthreadRwlockattrSettype(PthreadRwlockattr* attr, int type); +int APS5_VABI scePthreadRwlockattrGettype(const PthreadRwlockattr* attr, int* type); +int APS5_VABI scePthreadRwlockattrSetpshared(PthreadRwlockattr* attr, int pshared); +int APS5_VABI scePthreadRwlockattrGetpshared(const PthreadRwlockattr* attr, int* pshared); int APS5_VABI pthread_rwlock_rdlock_nid_postfix(PthreadRwlock* rwlock); int APS5_VABI pthread_rwlock_wrlock_nid_postfix(PthreadRwlock* rwlock); int APS5_VABI pthread_rwlock_unlock_nid_postfix(PthreadRwlock* rwlock); @@ -31,6 +36,7 @@ int APS5_VABI scePthreadRwlockRdlock(PthreadRwlock* rwlock); int APS5_VABI scePthreadRwlockWrlock(PthreadRwlock* rwlock); int APS5_VABI scePthreadRwlockTryrdlock(PthreadRwlock* rwlock); int APS5_VABI scePthreadRwlockUnlock(PthreadRwlock* rwlock); +int APS5_VABI scePthreadRwlockattrSettype(PthreadRwlockattr* attr, int type); } using TimedLock = int (APS5_VABI *)(PthreadRwlock*, const KernelTimespec*); @@ -397,8 +403,49 @@ static void CheckAttrProcessSharing() { Require(pthread_rwlockattr_destroy_nid_postfix(&attr) == 0); } +static void CheckAttrSettype() { + PthreadRwlockattr attr = nullptr; + Require(pthread_rwlockattr_init_nid_postfix(&attr) == 0); + for (const int type : {1, 2}) { + Require(scePthreadRwlockattrSettype(&attr, type) == SCE_OK); + PthreadRwlock rwlock = nullptr; + Require(pthread_rwlock_init_nid_postfix(&rwlock, &attr) == 0); + Require(pthread_rwlock_wrlock_nid_postfix(&rwlock) == 0 && pthread_rwlock_unlock_nid_postfix(&rwlock) == 0); + Require(pthread_rwlock_destroy_nid_postfix(&rwlock) == 0); + } + for (const int type : {0, 3, -1, static_cast(0x80000000u)}) Require(scePthreadRwlockattrSettype(&attr, type) == SCE_KERNEL_ERROR_EINVAL); + Require(pthread_rwlockattr_destroy_nid_postfix(&attr) == 0); + Require(scePthreadRwlockattrSettype(&attr, 1) == SCE_KERNEL_ERROR_EINVAL); + Require(scePthreadRwlockattrSettype(nullptr, 1) == SCE_KERNEL_ERROR_EINVAL); +} + +static void CheckAttrType() { + PthreadRwlockattr attr = nullptr; + Require(pthread_rwlockattr_init_nid_postfix(&attr) == 0); + int type = -1; + Require(pthread_rwlockattr_gettype_np_nid_postfix(&attr, &type) == 0 && type == 0); + Require(scePthreadRwlockattrGettype(&attr, &type) == SCE_OK && type == 0); + for (const int set : {1, 2}) { + Require(scePthreadRwlockattrSettype(&attr, set) == SCE_OK); + Require(pthread_rwlockattr_gettype_np_nid_postfix(&attr, &type) == 0 && type == set); + Require(scePthreadRwlockattrGettype(&attr, &type) == SCE_OK && type == set); + } + Require(Throws([&] { pthread_rwlockattr_gettype_np_nid_postfix(&attr, nullptr); })); + int pshared = -1; + Require(scePthreadRwlockattrGetpshared(&attr, &pshared) == SCE_OK && pshared == PROCESS_PRIVATE); + Require(scePthreadRwlockattrSetpshared(&attr, 1) == SCE_KERNEL_ERROR_EINVAL); + Require(scePthreadRwlockattrSetpshared(&attr, PROCESS_PRIVATE) == SCE_OK); + Require(pthread_rwlockattr_destroy_nid_postfix(&attr) == 0); + PthreadRwlockattr unset = nullptr; + Require(Throws([&] { pthread_rwlockattr_gettype_np_nid_postfix(nullptr, &type); })); + Require(Throws([&] { pthread_rwlockattr_gettype_np_nid_postfix(&unset, &type); })); + Require(Throws([&] { scePthreadRwlockattrGettype(&unset, &type); })); +} + int main() { + CheckAttrSettype(); CheckAttrProcessSharing(); + CheckAttrType(); CheckWaitingWriterHoldsBackNewReaders(); CheckConcurrentFirstWrlockExcludes(); CheckDeadlinesBeyondTheClockRange(); diff --git a/core/libs/tests/GuestPthreadSecondJoin.cpp b/core/libs/tests/GuestPthreadSecondJoin.cpp index 4db4d57034..0173a7bd2c 100644 --- a/core/libs/tests/GuestPthreadSecondJoin.cpp +++ b/core/libs/tests/GuestPthreadSecondJoin.cpp @@ -3,18 +3,21 @@ #include #include #include +#include #include extern "C" { int APS5_VABI scePthreadCreate(Pthread* thread, const PthreadAttr* attr, PthreadEntry entry, void* arg, const char* name); int APS5_VABI scePthreadJoin(Pthread thread, void** retval); int APS5_VABI scePthreadDetach(Pthread thread); +int APS5_VABI scePthreadGetname(Pthread thread, char* name); int APS5_VABI pthread_join_nid_postfix(Pthread thread, void** retval); } static constexpr int SCE_OK = 0; static constexpr int SCE_KERNEL_ERROR_ESRCH = static_cast(0x80020003u); static constexpr int SCE_KERNEL_ERROR_EINVAL = static_cast(0x80020016u); +static constexpr int SCE_KERNEL_ERROR_ENAMETOOLONG = static_cast(0x8002003fu); static constexpr int GUEST_ESRCH = 3; static constexpr std::size_t REUSED_SLOT_ROUNDS = 64; @@ -40,7 +43,7 @@ int main() { result = reinterpret_cast(std::uintptr_t{0x55}); Require(scePthreadJoin(thread, &result) == SCE_KERNEL_ERROR_ESRCH); Require(result == reinterpret_cast(std::uintptr_t{0x55})); - Require(scePthreadDetach(thread) == SCE_KERNEL_ERROR_EINVAL); + Require(scePthreadDetach(thread) == SCE_KERNEL_ERROR_ESRCH); Require(pthread_join_nid_postfix(thread, &result) == GUEST_ESRCH); Require(result == reinterpret_cast(std::uintptr_t{0x55})); @@ -62,4 +65,18 @@ int main() { Require(result == reinterpret_cast(std::uintptr_t{0x55})); Require(scePthreadDetach(detached) == SCE_KERNEL_ERROR_EINVAL); release = true; + + Pthread named = nullptr; + char name[64] = {}; + Require(scePthreadCreate(&named, nullptr, Finish, nullptr, "0123456789abcdefghijklmnopqrstu") == SCE_OK); + Require(scePthreadGetname(named, name) == SCE_OK && std::strcmp(name, "0123456789abcdefghijklmnopqrstu") == 0); + Require(scePthreadJoin(named, nullptr) == SCE_OK); + Pthread untouched = reinterpret_cast(std::uintptr_t{0x77}); + Require(scePthreadCreate(&untouched, nullptr, Finish, nullptr, "0123456789abcdefghijklmnopqrstuv") == SCE_KERNEL_ERROR_ENAMETOOLONG); + Require(untouched == reinterpret_cast(std::uintptr_t{0x77})); + Pthread unnamed = nullptr; + std::memset(name, 'x', sizeof(name)); + Require(scePthreadCreate(&unnamed, nullptr, Finish, nullptr, nullptr) == SCE_OK); + Require(scePthreadGetname(unnamed, name) == SCE_OK && name[0] == 0); + Require(scePthreadJoin(unnamed, nullptr) == SCE_OK); } diff --git a/core/libs/tests/GuestPthreadStack.cpp b/core/libs/tests/GuestPthreadStack.cpp index 5fcbe7ac52..a00fdd183e 100644 --- a/core/libs/tests/GuestPthreadStack.cpp +++ b/core/libs/tests/GuestPthreadStack.cpp @@ -1,11 +1,14 @@ #include "SceTypes.hpp" #ifdef __linux__ #include "prx/libc/include/GuestAllocations.hpp" +#include #include #include #include #include #include +#include +#include #include #include #endif @@ -32,6 +35,20 @@ static constexpr std::size_t FRAME_SIZE = 4096; static constexpr std::size_t FRAME_MARGIN = 2 * FRAME_SIZE; static constexpr std::size_t HOST_CALL_STACK = 256 * 1024; static constexpr std::size_t STACK_SIZES[] = {16384, 65536, 1u << 20, 16u << 20}; +#ifdef __linux__ +static void* lastGuestStack = nullptr; +static std::size_t lastGuestStackSize = 0; +#endif + +#ifdef __linux__ +extern bool GuestThreadStackHostRangeOverlaps(std::uintptr_t start, std::uintptr_t end); +static std::atomic observedUnmaps{0}; + +static void ObserveUnmap(std::uintptr_t address, std::size_t bytes) { + if (GuestThreadStackHostRangeOverlaps(address, address + bytes)) std::abort(); + observedUnmaps.fetch_add(1, std::memory_order_relaxed); +} +#endif thread_local unsigned char threadLocalBlock[512 * 1024]; @@ -61,6 +78,10 @@ static void* APS5_VABI Worker(void* arg) { void* address = nullptr; std::size_t size = 0; Require(scePthreadAttrGetstack(&attr, &address, &size) == SCE_OK); +#ifdef __linux__ + lastGuestStack = address; + lastGuestStackSize = size; +#endif Require(scePthreadAttrDestroy(&attr) == SCE_OK); Require(size == requested); @@ -78,33 +99,56 @@ static void* APS5_VABI Worker(void* arg) { } #ifdef __linux__ -bool GuestThreadStackHostRangeOverlaps(std::uintptr_t start, std::uintptr_t end); -static std::atomic observedUnmaps{0}; -static std::atomic retiredStackWord{0}; -static std::atomic retiredUnmapped{false}; -static std::atomic detachedDone{false}; -static int detachedTid = 0; +struct StackProbe { + std::size_t requested = 0; + std::uintptr_t stack = 0; + std::uintptr_t markerAddress = 0; + std::uintptr_t marker = 0; + std::atomic done{false}; + int hostTid = 0; +}; -static void* APS5_VABI DetachedWorker(void*) { - int local = 0; - detachedTid = static_cast(syscall(SYS_gettid)); - retiredStackWord.store(reinterpret_cast(&local), std::memory_order_relaxed); - detachedDone.store(true, std::memory_order_release); +static void* APS5_VABI RetainedStackWorker(void* opaque) { + auto& probe = *static_cast(opaque); + PthreadAttr attr = nullptr; + Require(scePthreadAttrInit(&attr) == SCE_OK); + Require(scePthreadAttrGet(scePthreadSelf(), &attr) == SCE_OK); + void* address = nullptr; + std::size_t size = 0; + Require(scePthreadAttrGetstack(&attr, &address, &size) == SCE_OK); + Require(scePthreadAttrDestroy(&attr) == SCE_OK); + Require(size == probe.requested); + probe.stack = reinterpret_cast(address); + probe.markerAddress = probe.stack + size - 4096; + *reinterpret_cast(probe.markerAddress) = probe.marker; + Require(*reinterpret_cast(probe.markerAddress) == probe.marker); + probe.hostTid = static_cast(syscall(SYS_gettid)); + probe.done.store(true, std::memory_order_release); return nullptr; } -static void* APS5_VABI ReaperWorker(void*) { - return nullptr; -} - -static void ObserveUnmap(std::uintptr_t address, std::size_t bytes) { - if (GuestThreadStackHostRangeOverlaps(address, address + bytes)) { - std::fprintf(stderr, "Retired stack still owns host-only pages after unmap\n"); - std::abort(); +static void WaitForProbe(StackProbe& probe) { + const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(2); + while (!probe.done.load(std::memory_order_acquire)) { + Require(std::chrono::steady_clock::now() < deadline); + std::this_thread::sleep_for(std::chrono::milliseconds(1)); } - observedUnmaps.fetch_add(1, std::memory_order_relaxed); - const auto retired = retiredStackWord.load(std::memory_order_relaxed); - if (retired >= address && retired - address < bytes) retiredUnmapped.store(true, std::memory_order_relaxed); + const auto task = "/proc/self/task/" + std::to_string(probe.hostTid); + while (access(task.c_str(), F_OK) == 0) { + Require(std::chrono::steady_clock::now() < deadline); + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } +} + +static void RequireCachedStackDiscarded(const StackProbe& probe) { + const auto range = GuestAllocations::GuestAllocationsLookup_nid_postfix(probe.markerAddress, false); + Require(range != nullptr && range->readable && range->writable); + Require(*reinterpret_cast(probe.markerAddress) == 0); +} + +static bool ReusesOneOf(const StackProbe& probe, const StackProbe* previous, std::size_t count) { + for (std::size_t i = 0; i < count; ++i) if (probe.stack == previous[i].stack) return true; + return false; } #endif @@ -122,26 +166,91 @@ int main() { void* result = nullptr; Require(scePthreadJoin(thread, &result) == SCE_OK); Require(result == &stackSize); +#ifdef __linux__ + if (stackSize == STACK_SIZES[sizeof(STACK_SIZES) / sizeof(STACK_SIZES[0]) - 1]) { + const auto pageSize = static_cast(sysconf(_SC_PAGESIZE)); + const auto stackAddress = reinterpret_cast(lastGuestStack); + const auto firstPageAddress = stackAddress - stackAddress % pageSize; + const auto leadingBytes = stackAddress - firstPageAddress; + const auto pageCount = (leadingBytes + lastGuestStackSize + pageSize - 1) / pageSize; + std::vector residency(pageCount); + if (mincore(reinterpret_cast(firstPageAddress), pageCount * pageSize, residency.data()) == 0) { + for (std::size_t page = 0; page < pageCount; ++page) + Require((residency[page] & 1u) == 0); + } else { + Require(errno == ENOMEM); + } + } +#endif } #ifdef __linux__ - Pthread detached = nullptr; - Require(scePthreadCreate(&detached, nullptr, DetachedWorker, nullptr, nullptr) == SCE_OK); - Require(scePthreadDetach(detached) == SCE_OK); - const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(2); - while (!detachedDone.load(std::memory_order_acquire)) { - Require(std::chrono::steady_clock::now() < deadline); - std::this_thread::sleep_for(std::chrono::milliseconds(1)); + constexpr std::size_t JoinedStackSize = 81920; + StackProbe joined[6]; + for (std::size_t i = 0; i < 6; ++i) { + joined[i].requested = JoinedStackSize; + joined[i].marker = 0x77000000u + i; + PthreadAttr attr = nullptr; + Require(scePthreadAttrInit(&attr) == SCE_OK); + Require(scePthreadAttrSetstacksize(&attr, JoinedStackSize) == SCE_OK); + Pthread thread = nullptr; + Require(scePthreadCreate(&thread, &attr, RetainedStackWorker, &joined[i], nullptr) == SCE_OK); + Require(scePthreadAttrDestroy(&attr) == SCE_OK); + Require(scePthreadJoin(thread, nullptr) == SCE_OK); + RequireCachedStackDiscarded(joined[i]); } - const auto task = "/proc/self/task/" + std::to_string(detachedTid); - while (access(task.c_str(), F_OK) == 0) { - Require(std::chrono::steady_clock::now() < deadline); - std::this_thread::sleep_for(std::chrono::milliseconds(1)); + StackProbe joinedReuse; + joinedReuse.requested = JoinedStackSize; + joinedReuse.marker = 0x7700ffffu; + PthreadAttr joinedAttr = nullptr; + Require(scePthreadAttrInit(&joinedAttr) == SCE_OK); + Require(scePthreadAttrSetstacksize(&joinedAttr, JoinedStackSize) == SCE_OK); + Pthread joinedThread = nullptr; + Require(scePthreadCreate(&joinedThread, &joinedAttr, RetainedStackWorker, &joinedReuse, nullptr) == SCE_OK); + Require(scePthreadAttrDestroy(&joinedAttr) == SCE_OK); + Require(scePthreadJoin(joinedThread, nullptr) == SCE_OK); + Require(ReusesOneOf(joinedReuse, joined, 6)); + RequireCachedStackDiscarded(joinedReuse); + + constexpr std::size_t DetachedStackSize = 114688; + StackProbe detached[5]; + for (std::size_t i = 0; i < 5; ++i) { + detached[i].requested = DetachedStackSize; + detached[i].marker = 0x77300000u + i; + PthreadAttr attr = nullptr; + Require(scePthreadAttrInit(&attr) == SCE_OK); + Require(scePthreadAttrSetstacksize(&attr, DetachedStackSize) == SCE_OK); + Pthread thread = nullptr; + Require(scePthreadCreate(&thread, &attr, RetainedStackWorker, &detached[i], nullptr) == SCE_OK); + Require(scePthreadAttrDestroy(&attr) == SCE_OK); + Require(scePthreadDetach(thread) == SCE_OK); + WaitForProbe(detached[i]); + } + StackProbe detachedReuse; + detachedReuse.requested = DetachedStackSize; + detachedReuse.marker = 0x7730ffffu; + PthreadAttr detachedAttr = nullptr; + Require(scePthreadAttrInit(&detachedAttr) == SCE_OK); + Require(scePthreadAttrSetstacksize(&detachedAttr, DetachedStackSize) == SCE_OK); + Pthread detachedThread = nullptr; + Require(scePthreadCreate(&detachedThread, &detachedAttr, RetainedStackWorker, &detachedReuse, nullptr) == SCE_OK); + Require(scePthreadAttrDestroy(&detachedAttr) == SCE_OK); + Require(scePthreadJoin(detachedThread, nullptr) == SCE_OK); + for (const auto& probe : detached) RequireCachedStackDiscarded(probe); + Require(ReusesOneOf(detachedReuse, detached, 5)); + RequireCachedStackDiscarded(detachedReuse); + + for (std::size_t stackSize = 4u << 20; stackSize <= 20u << 20; stackSize += 1u << 20) { + PthreadAttr attr = nullptr; + Require(scePthreadAttrInit(&attr) == SCE_OK); + Require(scePthreadAttrSetstacksize(&attr, stackSize) == SCE_OK); + Pthread thread = nullptr; + Require(scePthreadCreate(&thread, &attr, Worker, &stackSize, nullptr) == SCE_OK); + Require(scePthreadAttrDestroy(&attr) == SCE_OK); + void* result = nullptr; + Require(scePthreadJoin(thread, &result) == SCE_OK); + Require(result == &stackSize); } - Pthread reaper = nullptr; - Require(scePthreadCreate(&reaper, nullptr, ReaperWorker, nullptr, nullptr) == SCE_OK); - Require(scePthreadJoin(reaper, nullptr) == SCE_OK); - Require(retiredUnmapped.load(std::memory_order_relaxed)); - GuestAllocations::GuestAllocationsSetUnmapObserver_nid_postfix(nullptr); Require(observedUnmaps.load(std::memory_order_relaxed) != 0); + GuestAllocations::GuestAllocationsSetUnmapObserver_nid_postfix(nullptr); #endif } diff --git a/core/libs/tests/GuestRaiseException.cpp b/core/libs/tests/GuestRaiseException.cpp index fd74e36771..6c2fabea02 100644 --- a/core/libs/tests/GuestRaiseException.cpp +++ b/core/libs/tests/GuestRaiseException.cpp @@ -404,7 +404,6 @@ static void NestedWaitKeepsOuterWake() { Require(sceKernelDeleteSema(sem) == 0); } -#ifdef _WIN32 static constexpr int AddressRounds = 50; static std::uint32_t addressWord = 0; static std::uint32_t otherAddressWord = 0; @@ -483,7 +482,36 @@ static void HandlerLeavesAddressLock(PthreadEntry entry, const char* name) { worker.stop.store(true); Require(scePthreadJoin(thread, nullptr) == 0); } -#endif + +static std::uint32_t idleWord = 0; + +static void* APS5_VABI AddressIdle(void* arg) { + auto& worker = *static_cast(arg); + worker.id = std::this_thread::get_id(); + worker.started.store(true); + worker.waitResult = sceKernelSyncOnAddressWait(&idleWord, 0, nullptr, "address idle"); + return nullptr; +} + +static void HandlerRunsInUnboundedAddressWait() { + Worker worker; + Pthread thread = nullptr; + Require(scePthreadCreate(&thread, nullptr, AddressIdle, &worker, "address idle") == 0); + while (!worker.started.load()) std::this_thread::yield(); + std::this_thread::sleep_for(std::chrono::milliseconds(50)); + holdingEntered.store(false); + holdingLeft.store(false); + holdingRelease.store(false); + Require(sceKernelRaiseException(thread, SIGUSR1) == 0); + RequireInRound(AwaitWithin(holdingEntered, std::chrono::seconds(5)), "address idle", 0, "the handler did not run while the thread waited without a timeout"); + RequireInRound(holdingThread.load() == worker.id, "address idle", 0, "the handler ran on another thread"); + holdingRelease.store(true); + RequireInRound(AwaitWithin(holdingLeft, std::chrono::seconds(5)), "address idle", 0, "the handler did not return within 5 s of its release"); + std::atomic_ref(idleWord).store(1); + Require(sceKernelSyncOnAddressWake(&idleWord, 1) >= 0); + Require(scePthreadJoin(thread, nullptr) == 0); + Require(worker.waitResult == 0); +} static void ExpectDelivery(int before, std::thread::id thread) { for (int attempt = 0; attempt < 5000 && calls.load() == before; ++attempt) std::this_thread::sleep_for(std::chrono::milliseconds(1)); @@ -745,10 +773,9 @@ int main() { NestedWaitKeepsOuterWake(); Require(sceKernelRemoveExceptionHandler(SIGUSR1) == 0); -#ifdef _WIN32 Require(sceKernelInstallExceptionHandler(SIGUSR1, reinterpret_cast(&HoldingHandler)) == 0); HandlerLeavesAddressLock(AddressWaking, "address waking"); HandlerLeavesAddressLock(AddressWaiting, "address waiting"); + HandlerRunsInUnboundedAddressWait(); Require(sceKernelRemoveExceptionHandler(SIGUSR1) == 0); -#endif } diff --git a/core/libs/tests/GuestRandomDeviceSeed.cpp b/core/libs/tests/GuestRandomDeviceSeed.cpp new file mode 100644 index 0000000000..0aaee61657 --- /dev/null +++ b/core/libs/tests/GuestRandomDeviceSeed.cpp @@ -0,0 +1,97 @@ +#include "prx/libc/include/HostEntropy.hpp" +#include "prx/libc/include/general/VabiMacros.hpp" +#include +#include +#include +#include +#include +#include +#include +#include + +extern "C" { +std::int64_t APS5_VABI random_nid_postfix(); +void APS5_VABI srandomdev_nid_postfix(); +char* APS5_VABI initstate_nid_postfix(std::uint64_t, char*, std::int64_t); +char* APS5_VABI setstate_nid_postfix(char*); +} + +static void* entropyBuffer; +static std::size_t entropySize; +static bool failEntropy; + +void HostEntropy_nid_no_patch(void* buffer, std::size_t size) { + entropyBuffer = buffer; + entropySize = size; + if (failEntropy) throw std::runtime_error("entropy unavailable"); + auto* bytes = static_cast(buffer); + for (std::size_t i = 0; i < size; ++i) bytes[i] = static_cast(i * 37 + 11); +} + +static void Require(bool condition, const char* message) { + if (!condition) { + std::fprintf(stderr, "%s\n", message); + std::abort(); + } +} + +static std::uint32_t Word(const char* buffer, std::size_t index) { + std::uint32_t value; + std::memcpy(&value, buffer + sizeof(value) * index, sizeof(value)); + return value; +} + +int main() { + constexpr std::array sizes {8, 32, 64, 128, 256}; + constexpr std::array degrees {1, 7, 15, 31, 63}; + constexpr std::array separations {0, 3, 1, 3, 1}; + std::array state{}; + for (std::size_t type = 0; type < sizes.size(); ++type) { + state.fill(static_cast(0xa5)); + char* table = state.data() + 1; + char* previous = initstate_nid_postfix(42, table, static_cast(sizes[type])); + for (int i = 0; i < 5; ++i) random_nid_postfix(); + const std::uint32_t header = Word(table, 0); + srandomdev_nid_postfix(); + Require(entropyBuffer == table + 4, "srandomdev fills the active state after its header"); + Require(entropySize == degrees[type] * 4, "srandomdev fills every generator word"); + Require(Word(table, 0) == header, "srandomdev leaves the saved header untouched"); + for (std::size_t i = 0; i < entropySize; ++i) + Require(static_cast(table[4 + i]) == static_cast(i * 37 + 11), + "srandomdev neither derives words from one seed nor advances the generator"); + Require(state[0] == static_cast(0xa5) && + std::all_of(state.begin() + 1 + sizes[type], state.end(), [](char c) { return c == static_cast(0xa5); }), + "srandomdev preserves the buffer guards"); + const auto seeded = state; + if (type != 0) { + const std::uint32_t sum = Word(table, 1) + Word(table, 1 + separations[type]); + Require(random_nid_postfix() == sum / 2, "srandomdev resets the front and rear positions"); + Require(Word(table, 1 + separations[type]) == sum, "random advances the newly seeded table"); + } else { + std::int64_t x = static_cast(Word(table, 1)); + if (x == 0) x = 123459876; + x = 16807 * (x % 127773) - 2836 * (x / 127773); + if (x < 0) x += 0x7fffffff; + const auto expected = x; + Require(random_nid_postfix() == static_cast(expected), "the linear generator uses the entropy word"); + } + setstate_nid_postfix(previous); + Require(Word(table, 0) == type + (type == 0 ? 0 : 5), "the reseeded state saves its new position"); + state = seeded; + const std::uint32_t resetHeader = static_cast(type); + std::memcpy(table, &resetHeader, sizeof(resetHeader)); + setstate_nid_postfix(table); + const auto beforeFailure = state; + failEntropy = true; + bool threw = false; + try { + srandomdev_nid_postfix(); + } catch (const std::runtime_error&) { + threw = true; + } + failEntropy = false; + Require(threw, "srandomdev propagates entropy failures"); + Require(state == beforeFailure, "an entropy failure before writing keeps the state"); + setstate_nid_postfix(previous); + } +} diff --git a/core/libs/tests/GuestRandomState.cpp b/core/libs/tests/GuestRandomState.cpp index 71511241be..dc4259eb5e 100644 --- a/core/libs/tests/GuestRandomState.cpp +++ b/core/libs/tests/GuestRandomState.cpp @@ -13,6 +13,7 @@ extern "C" { std::int64_t APS5_VABI random_nid_postfix(); void APS5_VABI srandom_nid_postfix(std::uint64_t); +void APS5_VABI srandomdev_nid_postfix(); char* APS5_VABI initstate_nid_postfix(std::uint64_t, char*, std::int64_t); char* APS5_VABI setstate_nid_postfix(char*); int* APS5_VABI __error_nid_postfix(); @@ -123,16 +124,16 @@ static void CheckRand48() { int main() { CheckRand48(); - constexpr Sequence seedOne {1408992891, 1638182910, 83848093, 145962118, 627851256}; - constexpr Sequence seed12345 {1052984060, 1639322404, 1971525204, 2097956667, 2012147915}; + constexpr Sequence seedOne {1804289383, 846930886, 1681692777, 1714636915, 1957747793}; + constexpr Sequence seed12345 {383100999, 858300821, 357768173, 455528251, 133005921}; constexpr std::array seed42ByType {{ - {2060028338, 1164936638, 481681973, 1763071474, 962919018}, - {660273387, 1356916325, 287587791, 1132382070, 1497594867}, - {1015209508, 12952757, 1418769128, 2005808989, 1212998434}, - {745791173, 418148062, 949332769, 1480536337, 177711995}, - {996638292, 1209745284, 1148231710, 400392963, 1149215164}, + {413993338, 139015486, 2118548913, 1172713531, 191403351}, + {769798547, 2024571666, 1204852799, 931293870, 1762463907}, + {2051258974, 339992574, 1379825892, 1298392284, 825292997}, + {71876166, 708592740, 1483128881, 907283241, 442951012}, + {472624893, 994493761, 100792968, 176611971, 1804504504}, }}; - constexpr std::array seed7Type1 {1271472800, 27686551, 1252069488, 1609001632, 28873941, 508017694}; + constexpr std::array seed7Type1 {1380991591, 1769076016, 21842418, 334161865, 1367472909, 1249858043}; Require(Draw() == seedOne, "unseeded random must match srandom(1)"); srandom_nid_postfix(1); @@ -167,7 +168,7 @@ int main() { } Require(defaultTable != nullptr && Word(defaultTable, 0) == 5 * 5 + 3, "default table must hold its rear position"); Require(setstate_nid_postfix(defaultTable) == active, "setstate must return the previous state"); - Require(random_nid_postfix() == 1533042628 && random_nid_postfix() == 1225903521, + Require(random_nid_postfix() == 116285904 && random_nid_postfix() == 591987137, "setstate on the default table must continue the srandom(12345) sequence"); alignas(4) std::array small{}; @@ -232,5 +233,25 @@ int main() { std::sort(concurrent.begin(), concurrent.end()); Require(serial == concurrent, "concurrent calls must draw the serial sequence"); + for (const std::int64_t size : {8, 32, 64, 128, 256}) { + std::array entropyState{}; + char* previous = initstate_nid_postfix(42, entropyState.data(), size); + srandomdev_nid_postfix(); + const std::size_t separation = size == 32 || size == 128 ? 3 : 1; + std::int64_t expected; + if (size == 8) { + std::int64_t x = static_cast(Word(entropyState.data(), 1)); + if (x == 0) x = 123459876; + x = 16807 * (x % 127773) - 2836 * (x / 127773); + if (x < 0) x += 0x7fffffff; + expected = x; + } else { + const std::uint32_t sum = Word(entropyState.data(), 1) + Word(entropyState.data(), 1 + separation); + expected = sum >> 1; + } + Require(random_nid_postfix() == expected, "srandomdev seeds the shared library's active state from host entropy"); + setstate_nid_postfix(previous); + } + return failures == 0 ? 0 : 1; } diff --git a/core/libs/tests/GuestRtprioThread.cpp b/core/libs/tests/GuestRtprioThread.cpp new file mode 100644 index 0000000000..4d7aa6cc78 --- /dev/null +++ b/core/libs/tests/GuestRtprioThread.cpp @@ -0,0 +1,117 @@ +#include "SceTypes.hpp" +#include +#include +#include +#include +#include +#include + +extern "C" { +int APS5_VABI scePthreadCreate(Pthread* thread, const PthreadAttr* attr, PthreadEntry entry, void* arg, const char* name); +int APS5_VABI scePthreadJoin(Pthread thread, void** retval); +Pthread APS5_VABI scePthreadSelf(void); +int APS5_VABI scePthreadGetthreadid(void); +int APS5_VABI scePthreadGetschedparam(Pthread thread, int* policy, KernelSchedParam* param); +int APS5_VABI scePthreadAttrInit(PthreadAttr* attr); +int APS5_VABI scePthreadAttrDestroy(PthreadAttr* attr); +int APS5_VABI scePthreadAttrSetinheritsched(PthreadAttr* attr, int inheritSched); +int APS5_VABI scePthreadAttrSetschedpolicy(PthreadAttr* attr, int policy); +int APS5_VABI scePthreadAttrSetschedparam(PthreadAttr* attr, const KernelSchedParam* param); +struct GuestRtPrio { + std::uint16_t type; + std::uint16_t prio; +}; +int APS5_VABI rtprio_thread_nid_postfix(int function, int lwpid, GuestRtPrio* priority); +int* APS5_VABI __error_nid_postfix(); +} + +static constexpr int SCE_OK = 0; +static constexpr int GUEST_SCHED_FIFO = 1; +static constexpr int GUEST_SCHED_RR = 3; +static constexpr int EXPLICIT_SCHED = 0; +static constexpr int RTP_LOOKUP = 0; +static constexpr int RTP_SET = 1; +static constexpr std::uint16_t RTP_PRIO_REALTIME = 2; +static constexpr std::uint16_t RTP_PRIO_FIFO = 10; +static constexpr int GUEST_ESRCH = 3; +static constexpr int GUEST_EFAULT = 14; + +static void Require(bool value) { if (!value) std::abort(); } + +struct Report { + int policy; + int priority; + std::atomic tid{0}; + GuestRtPrio lookup{7, 7}; + int lookupResult = -1; + std::atomic release{false}; +}; + +static void* APS5_VABI Entry(void* arg) { + auto* report = static_cast(arg); + report->lookupResult = rtprio_thread_nid_postfix(RTP_LOOKUP, 0, &report->lookup); + report->tid.store(scePthreadGetthreadid(), std::memory_order_release); + const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(10); + while (!report->release.load(std::memory_order_acquire)) { + Require(std::chrono::steady_clock::now() < deadline); + std::this_thread::yield(); + } + return nullptr; +} + +static int LookupError(int function, int lwpid, GuestRtPrio* priority) { + *__error_nid_postfix() = 0; + Require(rtprio_thread_nid_postfix(function, lwpid, priority) == -1); + return *__error_nid_postfix(); +} + +static void CheckThread(int policy, int priority, std::uint16_t expectedType) { + Report report{policy, priority}; + PthreadAttr attr = nullptr; + KernelSchedParam param{priority}; + Require(scePthreadAttrInit(&attr) == SCE_OK); + Require(scePthreadAttrSetinheritsched(&attr, EXPLICIT_SCHED) == SCE_OK); + Require(scePthreadAttrSetschedpolicy(&attr, policy) == SCE_OK); + Require(scePthreadAttrSetschedparam(&attr, ¶m) == SCE_OK); + Pthread thread = nullptr; + Require(scePthreadCreate(&thread, &attr, Entry, &report, "rtprio") == SCE_OK); + Require(scePthreadAttrDestroy(&attr) == SCE_OK); + const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(10); + while (report.tid.load(std::memory_order_acquire) == 0) { + Require(std::chrono::steady_clock::now() < deadline); + std::this_thread::yield(); + } + Require(report.lookupResult == 0 && report.lookup.type == expectedType && report.lookup.prio == priority); + GuestRtPrio fromCreator{7, 7}; + Require(rtprio_thread_nid_postfix(RTP_LOOKUP, report.tid.load(), &fromCreator) == 0); + Require(fromCreator.type == expectedType && fromCreator.prio == priority); + bool threw = false; + try { rtprio_thread_nid_postfix(RTP_SET, report.tid.load(), &fromCreator); } catch (const std::exception&) { threw = true; } + Require(threw); + report.release.store(true, std::memory_order_release); + Require(scePthreadJoin(thread, nullptr) == SCE_OK); + Require(LookupError(RTP_LOOKUP, report.tid.load(), &fromCreator) == GUEST_ESRCH); +} + +int main() { + int policy = -1; + KernelSchedParam param{}; + Require(scePthreadGetschedparam(scePthreadSelf(), &policy, ¶m) == SCE_OK); + Require(policy == GUEST_SCHED_FIFO || policy == GUEST_SCHED_RR); + GuestRtPrio self{7, 7}; + Require(rtprio_thread_nid_postfix(RTP_LOOKUP, 0, &self) == 0); + Require(self.type == (policy == GUEST_SCHED_FIFO ? RTP_PRIO_FIFO : RTP_PRIO_REALTIME) && self.prio == param.sched_priority); + GuestRtPrio byId{7, 7}; + Require(rtprio_thread_nid_postfix(RTP_LOOKUP, scePthreadGetthreadid(), &byId) == 0); + Require(byId.type == self.type && byId.prio == self.prio); + CheckThread(GUEST_SCHED_FIFO, 512, RTP_PRIO_FIFO); + CheckThread(GUEST_SCHED_RR, 600, RTP_PRIO_REALTIME); + CheckThread(GUEST_SCHED_FIFO, 256, RTP_PRIO_FIFO); + Require(LookupError(2, 0, &self) == GUEST_ESRCH); + Require(LookupError(RTP_LOOKUP, 0, nullptr) == GUEST_EFAULT); + Require(LookupError(RTP_LOOKUP, 1, &self) == GUEST_ESRCH); + Require(LookupError(RTP_LOOKUP, 999999, &self) == GUEST_ESRCH); + bool threw = false; + try { rtprio_thread_nid_postfix(RTP_SET, 0, &self); } catch (const std::exception&) { threw = true; } + Require(threw); +} diff --git a/core/libs/tests/GuestRuntimeLifecycle.cpp b/core/libs/tests/GuestRuntimeLifecycle.cpp new file mode 100644 index 0000000000..e424940b2a --- /dev/null +++ b/core/libs/tests/GuestRuntimeLifecycle.cpp @@ -0,0 +1,140 @@ +#include "prx/libc/include/general/VabiMacros.hpp" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +extern "C" { +char* APS5_VABI dlerror_nid_postfix(); +std::int32_t APS5_VABI sceKernelLoadStartModule(const char*, std::size_t, const void*, std::uint32_t, const void*, int*); +int APS5_VABI sceKernelStopUnloadModule(std::int32_t, std::size_t, const void*, std::uint32_t, const void*, int*); +} + +namespace { +struct Event { std::size_t args; const void* argp; }; +std::array events{}; +std::atomic count{0}; +std::atomic entered{false}, releaseEntry{false}; +bool concurrent = false, reentrant = false; +thread_local bool isLifecycleWaiter = false; +std::atomic waiterObserved{false}; +std::atomic waiterReturns{0}; +const char* modulePath = nullptr; +std::int32_t reentrantHandle = -1; +void Require(bool ok, const std::source_location& where = std::source_location::current()) { + if (ok) return; + std::fprintf(stderr, "guest_runtime_lifecycle: failed at %u\n", where.line()); + std::abort(); +} +} + +extern "C" int pthread_cond_wait(pthread_cond_t* condition, pthread_mutex_t* mutex) { + using Wait = int (*)(pthread_cond_t*, pthread_mutex_t*); + static const auto actual = reinterpret_cast(::dlsym(RTLD_NEXT, "pthread_cond_wait")); + Require(actual != nullptr); + if (isLifecycleWaiter) waiterObserved.store(true, std::memory_order_release); + const int result = actual(condition, mutex); + if (isLifecycleWaiter) waiterReturns.fetch_add(1, std::memory_order_relaxed); + return result; +} + +extern "C" void APS5_VABI RecordArgs(std::size_t args, const void* argp) { + const auto index = count.fetch_add(1); + Require(index < events.size()); + events[index] = {args, argp}; + if (index == 0 && reentrant) { + reentrantHandle = sceKernelLoadStartModule(modulePath, 0, nullptr, 0, nullptr, nullptr); + Require(reentrantHandle > 0 && count.load() == 1); + } + if (index == 0 && concurrent) { + entered.store(true, std::memory_order_release); + const auto limit = std::chrono::steady_clock::now() + std::chrono::seconds(5); + while (!releaseEntry.load(std::memory_order_acquire)) { + Require(std::chrono::steady_clock::now() < limit); + std::this_thread::yield(); + } + } +} + +int main(int argc, char** argv) { + Require(argc == 3); + concurrent = std::strcmp(argv[2], "concurrent") == 0; + reentrant = std::strcmp(argv[2], "reentrant") == 0; + modulePath = argv[1]; + void* preload = ::dlopen(argv[1], RTLD_NOW | RTLD_LOCAL); + if (!preload) std::fprintf(stderr, "%s\n", ::dlerror()); + Require(preload && count.load() == 0); + if (std::strcmp(argv[2], "never-start") == 0) { + Require(::dlclose(preload) == 0 && count.load() == 0); + return 0; + } + const std::uint64_t payload = 0xfeed0123; + const bool zeroStart = std::strcmp(argv[2], "zero-start") == 0; + const std::size_t requestedSize = zeroStart ? 0 : sizeof(payload); + const void* requestedPointer = zeroStart ? nullptr : &payload; + int result = -1; + std::int32_t first = -1, second = -1; + if (concurrent) { + std::thread one([&] { first = sceKernelLoadStartModule(argv[1], requestedSize, requestedPointer, 0, nullptr, &result); }); + const auto limit = std::chrono::steady_clock::now() + std::chrono::seconds(5); + while (!entered.load(std::memory_order_acquire)) { + Require(std::chrono::steady_clock::now() < limit); + std::this_thread::yield(); + } + std::thread two([&] { + isLifecycleWaiter = true; + second = sceKernelLoadStartModule(argv[1], 0, nullptr, 0, nullptr, nullptr); + isLifecycleWaiter = false; + }); + while (!waiterObserved.load(std::memory_order_acquire)) { + Require(std::chrono::steady_clock::now() < limit); + std::this_thread::yield(); + } + Require(count.load() == 1); + releaseEntry.store(true, std::memory_order_release); + one.join(); + two.join(); + Require(waiterReturns.load() > 0); + std::fprintf(stderr, "Observed lifecycle waiter before initializer release; actual condition-wait returns=%u\n", waiterReturns.load()); + } else { + first = sceKernelLoadStartModule(argv[1], requestedSize, requestedPointer, 0, nullptr, &result); + second = sceKernelLoadStartModule(argv[1], 0, nullptr, 0, nullptr, nullptr); + } + std::fprintf(stderr, "first=%d second=%d result=%x count=%u\n", first, second, result, count.load()); + if (first < 0) std::fprintf(stderr, "loader error: %s\n", dlerror_nid_postfix()); + Require(first > 0 && second > 0 && first != second && result == 0x5a17); + Require(count.load() == 2 && events[0].args == requestedSize && events[0].argp == requestedPointer); + Require(events[1].args == 0xa1 && events[1].argp == nullptr); + Require(sceKernelStopUnloadModule(first, 0, nullptr, 0, nullptr, nullptr) == 0 && count.load() == 2); + Require(sceKernelStopUnloadModule(second, 0, nullptr, 0, nullptr, nullptr) == 0 && count.load() == 2); + if (reentrant) { + Require(reentrantHandle != first && reentrantHandle != second); + Require(::dlclose(preload) == 0 && count.load() == 2); + Require(sceKernelStopUnloadModule(reentrantHandle, 0, nullptr, 0, nullptr, nullptr) == 0 && count.load() == 4); + } else { + Require(::dlclose(preload) == 0 && count.load() == 4); + } + Require(events[2].args == 0xf1 && events[2].argp == nullptr); + Require(events[3].args == 0xfe && events[3].argp == nullptr); + if (std::strcmp(argv[2], "reload") == 0) { + void* resident = ::dlopen(argv[1], RTLD_NOW | RTLD_NOLOAD); + Require(resident == nullptr); + result = -1; + const auto reload = sceKernelLoadStartModule(argv[1], sizeof(payload), &payload, 0, nullptr, &result); + Require(reload > 0 && reload != first && reload != second && result == 0x5a17); + Require(count.load() == 6 && events[4].args == sizeof(payload) && events[4].argp == &payload); + Require(events[5].args == 0xa1 && events[5].argp == nullptr); + Require(sceKernelStopUnloadModule(reload, 0, nullptr, 0, nullptr, nullptr) == 0 && count.load() == 8); + Require(events[6].args == 0xf1 && events[6].argp == nullptr); + Require(events[7].args == 0xfe && events[7].argp == nullptr); + Require(::dlopen(argv[1], RTLD_NOW | RTLD_NOLOAD) == nullptr); + } +} diff --git a/core/libs/tests/GuestSceNet.cpp b/core/libs/tests/GuestSceNet.cpp index ad4e23494a..4e5f27cab2 100644 --- a/core/libs/tests/GuestSceNet.cpp +++ b/core/libs/tests/GuestSceNet.cpp @@ -34,8 +34,11 @@ extern const std::uint8_t in6addr_any_nid_postfix[16]; extern const std::uint8_t in6addr_loopback_nid_postfix[16]; int APS5_VABI sceNetInit_nid_postfix(void); int APS5_VABI sceNetShowNetstatWithMemory(int); +int APS5_VABI sceNetShowRouteWithMemory(int); int APS5_VABI sceNetShowNetstat(void); +int APS5_VABI sceNetShowNetstatEx(std::uint32_t); int APS5_VABI sceNetShowIfconfig(void); +int APS5_VABI sceNetShowPolicy(void); int APS5_VABI sceNetShowRoute(void); int APS5_VABI sceNetShowRoute6(void); int APS5_VABI sceNetSocket(const char*, int, int, int); @@ -44,6 +47,7 @@ int APS5_VABI sceNetListen(int, int); int APS5_VABI sceNetGetsockname(int, void*, std::uint32_t*); int APS5_VABI sceNetGetpeername(int, void*, std::uint32_t*); int APS5_VABI sceNetGetSockInfo(int, void*, int, int); +int APS5_VABI sceNetGetSockInfo6(int, void*, int, int); int APS5_VABI sceNetConnect(int, const void*, std::uint32_t); int APS5_VABI sceNetAccept(int, void*, std::uint32_t*); std::int64_t APS5_VABI sceNetSend(int, const void*, std::size_t, int); @@ -87,6 +91,10 @@ int APS5_VABI sceNetCtlRegisterCallback(NetCtlCallback, void*, int*); int APS5_VABI sceNetCtlUnregisterCallback(int); int APS5_VABI sceNetCtlRegisterCallbackV6(NetCtlCallback, void*, int*); int APS5_VABI sceNetCtlUnregisterCallbackV6(int); +int APS5_VABI sceNetCtlCheckCallback(void); +int APS5_VABI sceNetCtlRegisterCallbackForNpToolkit(NetCtlCallback, void*, int*); +int APS5_VABI sceNetCtlUnregisterCallbackForNpToolkit(int); +int APS5_VABI sceNetCtlCheckCallbackForNpToolkit(void); std::uint64_t NetCtlReadByteCounter_nid_no_patch(const char*); int APS5_VABI select_nid_postfix(int, void*, void*, void*, const void*); int APS5_VABI open_nid_postfix(const char*, int, int); @@ -167,6 +175,19 @@ public: static void APS5_VABI IgnoreNetCtlEvent(int, void*) {} +struct NetCtlEventRecord { + int calls = 0; + int event = 0; + void* arg = nullptr; +}; + +static void APS5_VABI RecordNetCtlEvent(int event, void* arg) { + auto* record = static_cast(arg); + ++record->calls; + record->event = event; + record->arg = arg; +} + static bool Failed(std::int64_t result, int error) { return result == static_cast(0x80410100u | static_cast(error)) && *sceNetErrnoLoc() == error; } @@ -270,11 +291,15 @@ static void CheckNetstat() { } Require(unsupported); } + Require(sceNetShowPolicy() == 0); + Require(sceNetShowNetstatEx(0) == 0); Require(sceNetShowRoute() == 0); Require(sceNetShowRoute6() == 0); Require(Failed(sceNetShowNetstatWithMemory(-1), 9)); + Require(Failed(sceNetShowRouteWithMemory(-1), 9)); const int pool = sceNetPoolCreate("netstat", 4096, 0); Require(pool > 0); + Require(sceNetShowRouteWithMemory(pool) == 0); const auto empty = CaptureNetstat(pool); Require(empty.find("bytes=4096 sockets=0") != std::string::npos); const int socket = sceNetSocket("netstat", 2, 1, 0); @@ -295,6 +320,7 @@ static void CheckNetstat() { Require(CaptureNetstat(pool).find("sockets=0") != std::string::npos); Require(sceNetPoolDestroy(pool) == 0); Require(Failed(sceNetShowNetstatWithMemory(pool), 9)); + Require(Failed(sceNetShowRouteWithMemory(pool), 9)); } static void CheckPoolStats() { @@ -1623,6 +1649,185 @@ static void CheckSockInfo() { Require(RequireSockInfoList(0) == earlier); } +static std::array RequireSockInfo6(int socket, int type, bool ipv6, bool nonblock, const std::array& local, + const std::array& remote, int requestFlags) { + std::array records; + records.fill(0xa5); + Require(sceNetGetSockInfo6(socket, records.data(), 2, requestFlags) == 1); + std::array expected{}; + std::memcpy(expected.data() + 0x24, &socket, sizeof(socket)); + expected[0x28] = static_cast(type); + if (ipv6) { + std::memcpy(expected.data() + 0x6c, local.data() + 8, 16); + std::memcpy(expected.data() + 0x7c, remote.data() + 8, 16); + } else { + std::memcpy(expected.data() + 0x34, local.data() + 4, 4); + std::memcpy(expected.data() + 0x38, remote.data() + 4, 4); + } + std::memcpy(expected.data() + 0x3c, local.data() + 2, 2); + std::memcpy(expected.data() + 0x3e, remote.data() + 2, 2); + const int flags = 1 | (nonblock ? 0x200000 : 0) | (ipv6 ? 0x400000 : 0); + std::memcpy(expected.data() + 0x48, &flags, sizeof(flags)); + for (const auto [offset, option] : {std::array{0x64, 0x1002}, std::array{0x68, 0x1001}}) { + int size = 0; + std::uint32_t length = sizeof(size); + Require(sceNetGetsockopt(socket, 0xffff, option, &size, &length) == 0 && size > 0); + std::memcpy(expected.data() + offset, &size, sizeof(size)); + } + Require(std::equal(expected.begin(), expected.end(), records.begin())); + Require(std::all_of(records.begin() + 0xa0, records.end(), [](auto byte) { return byte == 0xa5; })); + return expected; +} + +static std::vector RequireSockInfo6List(int flags) { + const int count = sceNetGetSockInfo6(-1, nullptr, 0, flags); + Require(count >= 0); + std::vector records(static_cast(count + 1) * 0xa0, 0xa5); + Require(sceNetGetSockInfo6(-1, records.data(), count + 1, flags) == count); + Require(std::all_of(records.end() - 0xa0, records.end(), [](auto byte) { return byte == 0xa5; })); + std::vector unbounded(records.size(), 0xa5); + Require(sceNetGetSockInfo6(-1, unbounded.data(), INT32_MAX, flags) == count && unbounded == records); + std::vector sockets; + for (int index = 0; index < count; ++index) { + const auto listed = records.begin() + index * 0xa0; + int socket = 0; + int status = 0; + std::memcpy(&socket, &listed[0x24], sizeof(socket)); + std::memcpy(&status, &listed[0x48], sizeof(status)); + Require(sockets.empty() || socket > sockets.back()); + Require((flags & 0x1000) != 0 || (status & 0x400000) != 0); + std::array single; + Require(sceNetGetSockInfo6(socket, single.data(), 1, flags) == 1 && std::equal(single.begin(), single.end(), listed)); + sockets.push_back(socket); + } + for (const int limit : {1, 2}) { + const int written = std::min(count, limit); + std::vector limited(records.size(), 0xa5); + Require(sceNetGetSockInfo6(-1, limited.data(), limit, flags) == written); + Require(std::equal(limited.begin(), limited.begin() + written * 0xa0, records.begin())); + Require(std::all_of(limited.begin() + written * 0xa0, limited.end(), [](auto byte) { return byte == 0xa5; })); + } + return sockets; +} + +static std::vector WithSockets(std::vector sockets, std::initializer_list added) { + sockets.insert(sockets.end(), added); + std::sort(sockets.begin(), sockets.end()); + return sockets; +} + +static void CheckSockInfo6() { + const auto earlier = RequireSockInfo6List(0); + const auto earlierAll = RequireSockInfo6List(0x1000); + Require(RequireSockInfo6List(0x40) == earlier && RequireSockInfo6List(0x1040) == earlierAll); + Require(std::includes(earlierAll.begin(), earlierAll.end(), earlier.begin(), earlier.end())); + + const std::array unnamed{}; + const int datagram = sceNetSocket("sockinfo6-inet", 2, 2, 17); + Require(datagram >= 0); + std::array inetAddress{16, 2, 0, 0, 127, 0, 0, 1}; + Require(sceNetBind_nid_postfix(datagram, inetAddress.data(), 16) == 0); + std::uint32_t size = 16; + Require(sceNetGetsockname(datagram, inetAddress.data(), &size) == 0 && size == 16); + const int nonblocking = 1; + Require(sceNetSetsockopt(datagram, 0xffff, 0x1200, &nonblocking, sizeof(nonblocking)) == 0); + RequireSockInfo6(datagram, 2, false, true, inetAddress, unnamed, 0x1000); + RequireSockInfo6(datagram, 2, false, true, inetAddress, unnamed, 0x1040); + + std::array record; + record.fill(0xa5); + const auto untouched = [&record] { return std::all_of(record.begin(), record.end(), [](auto byte) { return byte == 0xa5; }); }; + Require(Failed(sceNetGetSockInfo6(datagram, nullptr, 1, 0x1000), 22)); + Require(Failed(sceNetGetSockInfo6(datagram, record.data(), 0, 0x1000), 22)); + Require(Failed(sceNetGetSockInfo6(datagram, record.data(), -1, 0x1000), 22)); + Require(Failed(sceNetGetSockInfo6(-1, record.data(), 0, 0), 22)); + Require(Failed(sceNetGetSockInfo6(-1, record.data(), -5, 0x1000), 22)); + for (const int flags : {1, 0x41, 0x80, 0x1001, 0x2000, 0x400000, 0x1048}) { + Require(Failed(sceNetGetSockInfo6(datagram, record.data(), 1, flags), 22)); + Require(Failed(sceNetGetSockInfo6(-1, nullptr, 0, flags), 22)); + Require(Failed(sceNetGetSockInfo6(-1, record.data(), 1, flags), 22)); + } + Require(Failed(sceNetGetSockInfo6(datagram, record.data(), 1, 0), 47)); + Require(Failed(sceNetGetSockInfo6(datagram, record.data(), 1, 0x40), 47)); + Require(untouched()); + const int closed = sceNetSocket("sockinfo6-closed", 2, 2, 17); + Require(closed >= 0 && sceNetSocketClose(closed) == 0); + for (const int flags : {0, 0x40, 0x1000, 0x1040}) { + Require(Failed(sceNetGetSockInfo6(closed, record.data(), 1, flags), 9)); + Require(Failed(sceNetGetSockInfo6(INT32_MAX, record.data(), 1, flags), 9)); + } + Require(untouched()); + Require(RequireSockInfo6List(0) == earlier); + Require(RequireSockInfo6List(0x1000) == WithSockets(earlierAll, {datagram})); + + if (ipv6Unavailable) { + Require(sceNetSocketClose(datagram) == 0); + Require(Failed(sceNetGetSockInfo6(datagram, record.data(), 1, 0x1000), 9)); + Require(RequireSockInfo6List(0x1000) == earlierAll); + return; + } + + const int udp = sceNetSocket("sockinfo6-datagram", 28, 2, 17); + Require(udp >= 0); + RequireSockInfo6(udp, 2, true, false, unnamed, unnamed, 0); + std::array udpAddress{28, 28}; + udpAddress[23] = 1; + Require(sceNetBind_nid_postfix(udp, udpAddress.data(), udpAddress.size()) == 0); + size = udpAddress.size(); + Require(sceNetGetsockname(udp, udpAddress.data(), &size) == 0 && size == udpAddress.size()); + Require(udpAddress[2] != 0 || udpAddress[3] != 0); + RequireSockInfo6(udp, 2, true, false, udpAddress, unnamed, 0); + RequireSockInfo6(udp, 2, true, false, udpAddress, unnamed, 0x40); + RequireSockInfo6(udp, 2, true, false, udpAddress, unnamed, 0x1000); + RequireSockInfo6(udp, 2, true, false, udpAddress, unnamed, 0x1040); + + const int listener = sceNetSocket("sockinfo6-listener", 28, 1, 6); + std::array listening{28, 28}; + listening[23] = 1; + Require(listener >= 0 && sceNetBind_nid_postfix(listener, listening.data(), listening.size()) == 0 && sceNetListen(listener, 1) == 0); + size = listening.size(); + Require(sceNetGetsockname(listener, listening.data(), &size) == 0 && size == listening.size()); + RequireSockInfo6(listener, 1, true, false, listening, unnamed, 0); + const int client = sceNetSocket("sockinfo6-client", 28, 1, 6); + Require(client >= 0 && sceNetConnect(client, listening.data(), listening.size()) == 0); + const int accepted = sceNetAccept(listener, nullptr, nullptr); + Require(accepted >= 0); + std::array clientAddress{}; + size = clientAddress.size(); + Require(sceNetGetsockname(client, clientAddress.data(), &size) == 0 && size == clientAddress.size()); + Require(clientAddress[2] != 0 || clientAddress[3] != 0); + RequireSockInfo6(client, 1, true, false, clientAddress, listening, 0); + RequireSockInfo6(accepted, 1, true, false, listening, clientAddress, 0); + + const int peer = sceNetSocket("sockinfo6-peer", 28, 2, 17); + std::array peerAddress{28, 28}; + peerAddress[23] = 1; + Require(peer >= 0 && sceNetBind_nid_postfix(peer, peerAddress.data(), peerAddress.size()) == 0); + size = peerAddress.size(); + Require(sceNetGetsockname(peer, peerAddress.data(), &size) == 0 && size == peerAddress.size()); + Require(sceNetConnect(peer, udpAddress.data(), udpAddress.size()) == 0); + RequireSockInfo6(peer, 2, true, false, peerAddress, udpAddress, 0); + const std::array buffers{65536, 32768}; + Require(sceNetSetsockopt(peer, 0xffff, 0x1200, &nonblocking, sizeof(nonblocking)) == 0); + Require(sceNetSetsockopt(peer, 0xffff, 0x1002, &buffers[0], sizeof(buffers[0])) == 0); + Require(sceNetSetsockopt(peer, 0xffff, 0x1001, &buffers[1], sizeof(buffers[1])) == 0); + const auto configured = RequireSockInfo6(peer, 2, true, true, peerAddress, udpAddress, 0); + Require(std::memcmp(configured.data() + 0x64, buffers.data(), sizeof(buffers)) == 0); + Require(std::all_of(configured.begin() + 0x34, configured.begin() + 0x3c, [](auto byte) { return byte == 0; })); + Require(sceNetGetSockInfo6(peer, record.data(), 1, 0x1040) == 1 && std::equal(configured.begin(), configured.end(), record.begin())); + + Require(RequireSockInfo6List(0) == WithSockets(earlier, {udp, listener, client, accepted, peer})); + Require(RequireSockInfo6List(0x40) == WithSockets(earlier, {udp, listener, client, accepted, peer})); + Require(RequireSockInfo6List(0x1000) == WithSockets(earlierAll, {datagram, udp, listener, client, accepted, peer})); + Require(Failed(sceNetGetSockInfo6(datagram, record.data(), 1, 0), 47)); + + for (const int descriptor : {peer, accepted, client, listener, udp}) Require(sceNetSocketClose(descriptor) == 0); + Require(Failed(sceNetGetSockInfo6(udp, record.data(), 1, 0), 9)); + Require(RequireSockInfo6List(0) == earlier); + Require(sceNetSocketClose(datagram) == 0); + Require(RequireSockInfo6List(0x1000) == earlierAll); +} + static void CheckDatagramDisconnect() { const int peer = sceNetSocket("disconnect-peer", 2, 2, 17); const int other = sceNetSocket("disconnect-other", 2, 2, 17); @@ -1722,6 +1927,7 @@ int main() { CheckDatagramDisconnect(); CheckCloseKernelSocket(); CheckSockInfo(); + CheckSockInfo6(); const int closed = sceNetSocket("closed-by-kernel", 2, 2, 17); Require(closed >= 0); @@ -2211,6 +2417,46 @@ int main() { } Require(sceNetCtlUnregisterCallbackV6(0) == 0); } + { + int cid = -1; + Require(sceNetCtlRegisterCallbackForNpToolkit(nullptr, nullptr, &cid) == static_cast(0x80412107)); + Require(sceNetCtlRegisterCallbackForNpToolkit(IgnoreNetCtlEvent, nullptr, nullptr) == static_cast(0x80412107)); + int cids[8] = {}; + for (int i = 0; i < 8; ++i) { + Require(sceNetCtlRegisterCallbackForNpToolkit(IgnoreNetCtlEvent, nullptr, &cids[i]) == 0); + Require(cids[i] == i); + } + Require(sceNetCtlRegisterCallbackForNpToolkit(IgnoreNetCtlEvent, nullptr, &cid) == static_cast(0x80412103)); + Require(sceNetCtlRegisterCallback(IgnoreNetCtlEvent, nullptr, &cid) == 0 && cid == 0); + Require(sceNetCtlUnregisterCallback(0) == 0); + Require(sceNetCtlUnregisterCallbackForNpToolkit(-1) == static_cast(0x80412105)); + Require(sceNetCtlUnregisterCallbackForNpToolkit(8) == static_cast(0x80412105)); + Require(sceNetCtlUnregisterCallbackForNpToolkit(3) == 0); + Require(sceNetCtlRegisterCallbackForNpToolkit(IgnoreNetCtlEvent, nullptr, &cid) == 0); + Require(cid == 3); + for (int i = 0; i < 8; ++i) { + Require(sceNetCtlUnregisterCallbackForNpToolkit(i) == 0); + } + + NetCtlEventRecord toolkit; + NetCtlEventRecord plain; + Require(sceNetCtlRegisterCallbackForNpToolkit(RecordNetCtlEvent, &toolkit, &cid) == 0 && cid == 0); + Require(sceNetCtlCheckCallback() == 0 && toolkit.calls == 0); + Require(sceNetCtlCheckCallbackForNpToolkit() == 0); + Require(toolkit.calls == 1 && toolkit.arg == &toolkit && (toolkit.event == 1 || toolkit.event == 3)); + int current = -1; + Require(sceNetCtlGetState(¤t) == 0 && toolkit.event == (current == 3 ? 3 : 1)); + Require(sceNetCtlCheckCallbackForNpToolkit() == 0 && toolkit.calls == 1); + int plainCid = -1; + Require(sceNetCtlRegisterCallback(RecordNetCtlEvent, &plain, &plainCid) == 0); + Require(sceNetCtlCheckCallbackForNpToolkit() == 0 && plain.calls == 0); + Require(sceNetCtlCheckCallback() == 0 && plain.calls == 1 && toolkit.calls == 1); + Require(sceNetCtlUnregisterCallback(plainCid) == 0); + Require(sceNetCtlUnregisterCallbackForNpToolkit(cid) == 0); + Require(sceNetCtlRegisterCallbackForNpToolkit(RecordNetCtlEvent, &toolkit, &cid) == 0); + Require(sceNetCtlCheckCallbackForNpToolkit() == 0 && toolkit.calls == 2); + Require(sceNetCtlUnregisterCallbackForNpToolkit(cid) == 0); + } if (ipv6Unavailable) { std::puts("skipped the IPv6 socket checks, the host has no usable IPv6 loopback"); return 77; diff --git a/core/libs/tests/GuestSceV2Exceptions.S b/core/libs/tests/GuestSceV2Exceptions.S new file mode 100644 index 0000000000..8b076dc726 --- /dev/null +++ b/core/libs/tests/GuestSceV2Exceptions.S @@ -0,0 +1,72 @@ +.cfi_sections .eh_frame +.text +.globl SceV2Catch +SceV2Catch: + .cfi_startproc +#ifdef _WIN32 + .cfi_personality 0x00, __gxx_personality_v0_nid_postfix + .cfi_lsda 0x00, .Lcatch_lsda +#else + .cfi_personality 0x9b, .Lcatch_personality + .cfi_lsda 0x1b, .Lcatch_lsda +#endif + pushq %rbp + .cfi_def_cfa_offset 16 + .cfi_offset %rbp, -16 + movq %rsp, %rbp + .cfi_def_cfa_register %rbp + subq $16, %rsp + movq %rdi, %rax + movq %rsi, %rdi +.Lcatch_call: + call *%rax +.Lcatch_call_end: + ud2 +.Lcatch_landing: + movl %edx, SceV2Selector(%rip) + movq %rax, %rdi + call __cxa_begin_catch_nid_postfix + movq %rax, %rdi + call SceV2Inspect + call __cxa_end_catch_nid_postfix + leave + .cfi_def_cfa %rsp, 8 + ret + .cfi_endproc +#ifdef _WIN32 +.section .rdata,"dr" +#else +.section .data.rel.ro,"aw",@progbits +.p2align 3 +.Lcatch_personality: + .quad __gxx_personality_v0_nid_postfix +.section .rodata +#endif +.Lcatch_lsda: +#ifdef _WIN32 + .byte 0xff, 0x80 +#else + .byte 0xff, 0x9b +#endif + .uleb128 .Lcatch_types_end-.Lcatch_types_offset +.Lcatch_types_offset: + .byte 0x01 + .uleb128 .Lcatch_calls_end-.Lcatch_calls +.Lcatch_calls: + .uleb128 .Lcatch_call-SceV2Catch + .uleb128 .Lcatch_call_end-.Lcatch_call + .uleb128 .Lcatch_landing-SceV2Catch + .uleb128 1 +.Lcatch_calls_end: + .byte 1, 1, 2, 0 +#ifdef _WIN32 + .quad SceV2CatchType + .quad SceV2OtherType +#else + .long SceV2CatchType-. + .long SceV2OtherType-. +#endif +.Lcatch_types_end: +#ifndef _WIN32 +.section .note.GNU-stack,"",@progbits +#endif diff --git a/core/libs/tests/GuestSceV2Exceptions.cpp b/core/libs/tests/GuestSceV2Exceptions.cpp new file mode 100644 index 0000000000..b7f21db666 --- /dev/null +++ b/core/libs/tests/GuestSceV2Exceptions.cpp @@ -0,0 +1,217 @@ +#include "prx/libc/include/general/VabiMacros.hpp" +#include +#include +#include +#include +#include + +struct TypeRecord { const void* vtable; const char* name; const TypeRecord* base; }; +struct Category { const void* const* vtable; }; +struct ErrorCode { int reserved; int value; const Category* category; }; +struct Exception { const void* const* vtable; const char* message; }; +struct SystemError { Exception base; ErrorCode code; }; +static_assert(sizeof(Exception) == 16 && sizeof(SystemError) == 32); + +using GuestWhat = const char* (APS5_VABI *)(const Exception*); +using Thrower = void (APS5_VABI *)(const void*); + +extern "C" { +extern const TypeRecord _ZTISt9exception_nid_postfix; +extern const TypeRecord _ZTISt9bad_alloc_nid_postfix; +extern const TypeRecord _ZTISt12length_error_nid_postfix; +extern const TypeRecord _ZTISt12system_error_nid_postfix; +extern const TypeRecord _ZTINSt13__sce_cxxvers16_Error_exceptionE_nid_postfix; +const Category* APS5_VABI _ZSt16generic_categoryv_nid_postfix(); +void APS5_VABI _ZNSt13__sce_cxxvers16_Error_exceptionC1EPKc_nid_postfix(Exception* self, const char* message); +void APS5_VABI _ZNSt13__sce_cxxvers16_Error_exceptionC2EPKc_nid_postfix(Exception* self, const char* message); +void APS5_VABI _ZNSt13__sce_cxxvers16_Error_exceptionC1ERKS0__nid_postfix(Exception* self, const Exception* source); +void APS5_VABI _ZNSt13__sce_cxxvers16_Error_exceptionD1Ev_nid_postfix(Exception* self); +void APS5_VABI _ZNSt13__sce_cxxvers16_Error_exceptionD2Ev_nid_postfix(Exception* self); +const char* APS5_VABI _ZNKSt13__sce_cxxvers16_Error_exception4whatEv_nid_postfix(const Exception* self); +void APS5_VABI _ZNSt8__sce_v214_Xlength_errorEPKc_nid_postfix(const char* message); +void APS5_VABI _ZNSt8__sce_v214_Xout_of_rangeEPKc_nid_postfix(const char* message); +void APS5_VABI _ZNSt8__sce_v218_Xinvalid_argumentEPKc_nid_postfix(const char* message); +void APS5_VABI _ZNSt8__sce_v215_Xruntime_errorEPKc_nid_postfix(const char* message); +void APS5_VABI _ZNSt8__sce_v216_Xoverflow_errorEPKc_nid_postfix(const char* message); +void APS5_VABI _ZNSt8__sce_v211_Xbad_allocEv_nid_postfix(const void*); +void APS5_VABI _ZNSt8__sce_v219_Xbad_function_callEv_nid_postfix(const void*); +void APS5_VABI _ZNSt8__sce_v26_ThrowERKNS_19bad_optional_accessE_nid_postfix(const void*); +void APS5_VABI _ZNSt8__sce_v26_ThrowERKNS_12bad_weak_ptrE_nid_postfix(const void*); +void APS5_VABI _ZNSt8__sce_v26_ThrowERKNS_18bad_variant_accessE_nid_postfix(const void*); +void APS5_VABI _ZNSt8__sce_v26_ThrowERKNS_11range_errorE_nid_postfix(const void* error); +void APS5_VABI _ZNSt8__sce_v26_ThrowERKNS_12system_errorE_nid_postfix(const void* error); +void APS5_VABI _ZNSt8__sce_v28ios_base6_ThrowERKNS0_7failureE_nid_postfix(const void* failure); +void APS5_VABI _ZNSt8__sce_v216_Throw_Cpp_errorEi_nid_postfix(std::intptr_t code); +const char* APS5_VABI _ZNSt8__sce_v213_Syserror_mapEi_nid_postfix(int value); +const char* APS5_VABI _ZSt13_Syserror_mapi_nid_postfix(int value); +std::int64_t APS5_VABI _ZNSt8__sce_v216_Xtime_get_ticksEv_nid_postfix(); +std::int64_t APS5_VABI _Xtime_get_ticks_nid_postfix(); +void APS5_VABI SceV2Catch(Thrower thrower, const void* argument); + +const void* SceV2CatchType = nullptr; +const void* SceV2OtherType = nullptr; +int SceV2Selector = 0; +const void* SceV2Caught = nullptr; +} + +static unsigned failures = 0; + +static void Require(bool value, const char* what) { + if (value) return; + std::fprintf(stderr, "FAIL: %s\n", what); + ++failures; +} + +static const TypeRecord* TypeOf(const Exception* object) { + return static_cast(object->vtable[-1]); +} + +static const char* What(const Exception* object) { + return reinterpret_cast(const_cast(object->vtable[2]))(object); +} + +static TypeRecord Named(const char* name) { + return {_ZTISt9exception_nid_postfix.vtable, name, nullptr}; +} + +static char inspected[256]; +static std::size_t inspectBytes = sizeof(Exception); +static SystemError inspectedSystem {}; + +extern "C" void APS5_VABI SceV2Inspect(void* object) { + const auto* exception = static_cast(object); + std::snprintf(inspected, sizeof(inspected), "%s|%s", TypeOf(exception)->name, What(exception)); + std::memcpy(&inspectedSystem, object, inspectBytes); +} + +struct Case { const char* label; Thrower thrower; const void* argument; const void* catchType; const void* otherType; int selector; const char* expected; }; + +static void Run(const Case& test) { + SceV2CatchType = test.catchType; + SceV2OtherType = test.otherType; + SceV2Selector = 0; + inspected[0] = 0; + SceV2Catch(test.thrower, test.argument); + Require(SceV2Selector == test.selector, test.label); + if (std::strcmp(inspected, test.expected) != 0) { + std::fprintf(stderr, " %s: got '%s', want '%s'\n", test.label, inspected, test.expected); + ++failures; + } +} + +template +static Thrower AsThrower(TFunction function) { + return reinterpret_cast(function); +} + +static void CheckThrowers() { + const TypeRecord logicError = Named("NSt8__sce_v211logic_errorE"); + const TypeRecord runtimeError = Named("NSt8__sce_v213runtime_errorE"); + const TypeRecord lengthError = Named("NSt8__sce_v212length_errorE"); + const TypeRecord systemError = Named("NSt8__sce_v212system_errorE"); + const TypeRecord unrelated = Named("NSt8__sce_v211range_errorE"); + const void* exception = &_ZTISt9exception_nid_postfix; + const void* errorException = &_ZTINSt13__sce_cxxvers16_Error_exceptionE_nid_postfix; + const Case cases[] { + {"_Xlength_error", AsThrower(_ZNSt8__sce_v214_Xlength_errorEPKc_nid_postfix), "length", &logicError, &unrelated, 2, + "NSt8__sce_v212length_errorE|length"}, + {"_Xlength_error is not std::length_error", AsThrower(_ZNSt8__sce_v214_Xlength_errorEPKc_nid_postfix), "length", &lengthError, &_ZTISt12length_error_nid_postfix, 2, + "NSt8__sce_v212length_errorE|length"}, + {"_Xout_of_range", AsThrower(_ZNSt8__sce_v214_Xout_of_rangeEPKc_nid_postfix), "range", errorException, &runtimeError, 2, + "NSt8__sce_v212out_of_rangeE|range"}, + {"_Xinvalid_argument", AsThrower(_ZNSt8__sce_v218_Xinvalid_argumentEPKc_nid_postfix), "argument", &logicError, &runtimeError, 2, + "NSt8__sce_v216invalid_argumentE|argument"}, + {"_Xruntime_error", AsThrower(_ZNSt8__sce_v215_Xruntime_errorEPKc_nid_postfix), "runtime", &runtimeError, &logicError, 2, + "NSt8__sce_v213runtime_errorE|runtime"}, + {"_Xoverflow_error", AsThrower(_ZNSt8__sce_v216_Xoverflow_errorEPKc_nid_postfix), nullptr, exception, &logicError, 2, + "NSt8__sce_v214overflow_errorE|"}, + {"_Xbad_alloc", AsThrower(_ZNSt8__sce_v211_Xbad_allocEv_nid_postfix), nullptr, &_ZTISt9bad_alloc_nid_postfix, errorException, 2, + "St9bad_alloc|std::bad_alloc"}, + {"_Xbad_function_call", AsThrower(_ZNSt8__sce_v219_Xbad_function_callEv_nid_postfix), nullptr, exception, errorException, 2, + "NSt8__sce_v217bad_function_callE|bad function call"}, + {"_Throw(bad_optional_access)", AsThrower(_ZNSt8__sce_v26_ThrowERKNS_19bad_optional_accessE_nid_postfix), nullptr, exception, errorException, 2, + "NSt8__sce_v219bad_optional_accessE|Bad optional access"}, + {"_Throw(bad_weak_ptr)", AsThrower(_ZNSt8__sce_v26_ThrowERKNS_12bad_weak_ptrE_nid_postfix), nullptr, exception, errorException, 2, + "NSt8__sce_v212bad_weak_ptrE|bad_weak_ptr"}, + {"_Throw(bad_variant_access)", AsThrower(_ZNSt8__sce_v26_ThrowERKNS_18bad_variant_accessE_nid_postfix), nullptr, exception, errorException, 2, + "NSt8__sce_v218bad_variant_accessE|bad variant access"}, + {"_Throw_Cpp_error", AsThrower(_ZNSt8__sce_v216_Throw_Cpp_errorEi_nid_postfix), reinterpret_cast(1), &systemError, &_ZTISt12system_error_nid_postfix, 2, + "NSt8__sce_v212system_errorE|invalid argument: invalid argument"}, + }; + for (const auto& test : cases) Run(test); +} + +static void CheckCopies() { + const TypeRecord runtimeError = Named("NSt8__sce_v213runtime_errorE"); + const TypeRecord systemError = Named("NSt8__sce_v212system_errorE"); + const TypeRecord logicError = Named("NSt8__sce_v211logic_errorE"); + + Exception range {}; + _ZNSt13__sce_cxxvers16_Error_exceptionC2EPKc_nid_postfix(&range, "range copy"); + Run({"_Throw(range_error)", AsThrower(_ZNSt8__sce_v26_ThrowERKNS_11range_errorE_nid_postfix), &range, &runtimeError, &logicError, 2, + "NSt8__sce_v211range_errorE|range copy"}); + Require(inspectedSystem.base.message == range.message, "_Throw(range_error) shares the message"); + _ZNSt13__sce_cxxvers16_Error_exceptionD2Ev_nid_postfix(&range); + + SystemError error {}; + _ZNSt13__sce_cxxvers16_Error_exceptionC2EPKc_nid_postfix(&error.base, "system copy"); + error.code = {0, 5, _ZSt16generic_categoryv_nid_postfix()}; + inspectBytes = sizeof(SystemError); + SceV2CatchType = &runtimeError; + SceV2OtherType = &logicError; + SceV2Catch(AsThrower(_ZNSt8__sce_v26_ThrowERKNS_12system_errorE_nid_postfix), &error); + Require(std::strcmp(inspected, "NSt8__sce_v212system_errorE|system copy") == 0, "_Throw(system_error) type and message"); + Require(inspectedSystem.code.value == 5, "_Throw(system_error) copies the code value"); + Require(inspectedSystem.code.category == error.code.category, "_Throw(system_error) copies the category"); + + SceV2CatchType = &systemError; + SceV2Catch(AsThrower(_ZNSt8__sce_v28ios_base6_ThrowERKNS0_7failureE_nid_postfix), &error); + Require(std::strcmp(inspected, "NSt8__sce_v28ios_base7failureE|system copy") == 0, "ios_base::_Throw(failure) type and message"); + Require(inspectedSystem.code.value == 5, "ios_base::_Throw(failure) copies the code"); + inspectBytes = sizeof(Exception); + _ZNSt13__sce_cxxvers16_Error_exceptionD2Ev_nid_postfix(&error.base); +} + +static void CheckErrorException() { + Exception empty {}; + _ZNSt13__sce_cxxvers16_Error_exceptionC1EPKc_nid_postfix(&empty, nullptr); + Require(std::strcmp(_ZNKSt13__sce_cxxvers16_Error_exception4whatEv_nid_postfix(&empty), "") == 0, "_Error_exception(nullptr) has an empty message"); + Require(std::strcmp(TypeOf(&empty)->name, "NSt13__sce_cxxvers16_Error_exceptionE") == 0, "_Error_exception type name"); + Require(TypeOf(&empty)->base == &_ZTISt9exception_nid_postfix, "_Error_exception derives from std::exception"); + + Exception text {}; + _ZNSt13__sce_cxxvers16_Error_exceptionC2EPKc_nid_postfix(&text, "text"); + Exception copy {}; + _ZNSt13__sce_cxxvers16_Error_exceptionC1ERKS0__nid_postfix(©, &text); + Require(std::strcmp(_ZNKSt13__sce_cxxvers16_Error_exception4whatEv_nid_postfix(©), "text") == 0, "_Error_exception copy keeps the message"); + _ZNSt13__sce_cxxvers16_Error_exceptionD2Ev_nid_postfix(&text); + Require(std::strcmp(_ZNKSt13__sce_cxxvers16_Error_exception4whatEv_nid_postfix(©), "text") == 0, "_Error_exception copy outlives the source"); + _ZNSt13__sce_cxxvers16_Error_exceptionD1Ev_nid_postfix(©); + _ZNSt13__sce_cxxvers16_Error_exceptionD1Ev_nid_postfix(&empty); +} + +static void CheckSyserrorMap() { + Require(std::strcmp(_ZNSt8__sce_v213_Syserror_mapEi_nid_postfix(0), "success") == 0, "_Syserror_map(0)"); + Require(_ZSt13_Syserror_mapi_nid_postfix(0) == nullptr, "unversioned _Syserror_map(0) stays null"); + Require(std::strcmp(_ZNSt8__sce_v213_Syserror_mapEi_nid_postfix(2), _ZSt13_Syserror_mapi_nid_postfix(2)) == 0, "_Syserror_map(2) matches the unversioned text"); + Require(std::strcmp(_ZNSt8__sce_v213_Syserror_mapEi_nid_postfix(99999), "unknown error") == 0, "_Syserror_map(unknown)"); + Require(_ZSt13_Syserror_mapi_nid_postfix(99999) == nullptr, "unversioned _Syserror_map(unknown) stays null"); +} + +static void CheckTicks() { + const auto before = _Xtime_get_ticks_nid_postfix(); + const auto ticks = _ZNSt8__sce_v216_Xtime_get_ticksEv_nid_postfix(); + const auto after = _Xtime_get_ticks_nid_postfix(); + Require(before <= ticks && ticks <= after, "_Xtime_get_ticks is the unversioned clock"); +} + +int main() { + CheckErrorException(); + CheckThrowers(); + CheckCopies(); + CheckSyserrorMap(); + CheckTicks(); + if (failures) return 1; + std::puts("guest std::__sce_v2 exceptions: PASS"); + return 0; +} diff --git a/core/libs/tests/GuestSchedPriority.cpp b/core/libs/tests/GuestSchedPriority.cpp index 0b15d2becd..f83d5a751e 100644 --- a/core/libs/tests/GuestSchedPriority.cpp +++ b/core/libs/tests/GuestSchedPriority.cpp @@ -100,14 +100,12 @@ int main() { Require(setpriority_nid_postfix(PrioProcess, 0, -99) == 0 && getpriority_nid_postfix(PrioProcess, 0) == -20); Require(setpriority_nid_postfix(PrioProcess, 0, 0) == 0 && getpriority_nid_postfix(PrioProcess, 0) == 0); GuestRtPrio rtprio{7, 7}; + Require(rtprio_thread_nid_postfix(0, 0, &rtprio) == 0 && rtprio.type == 10 && rtprio.prio == 700); bool rtprioThrew = false; - try { rtprio_thread_nid_postfix(0, 0, &rtprio); } catch (const std::exception&) { rtprioThrew = true; } - Require(rtprioThrew); - rtprioThrew = false; try { rtprio_thread_nid_postfix(1, 0, &rtprio); } catch (const std::exception&) { rtprioThrew = true; } Require(rtprioThrew); *__error_nid_postfix() = 0; - Require(rtprio_thread_nid_postfix(2, 0, &rtprio) == -1 && *__error_nid_postfix() == 22); + Require(rtprio_thread_nid_postfix(2, 0, &rtprio) == -1 && *__error_nid_postfix() == 3); *__error_nid_postfix() = 0; Require(rtprio_thread_nid_postfix(0, 0, nullptr) == -1 && *__error_nid_postfix() == 14); *__error_nid_postfix() = 0; diff --git a/core/libs/tests/GuestSemaphore.cpp b/core/libs/tests/GuestSemaphore.cpp index 0ba58d4520..4ee50ec2ea 100644 --- a/core/libs/tests/GuestSemaphore.cpp +++ b/core/libs/tests/GuestSemaphore.cpp @@ -36,6 +36,28 @@ static void* APS5_VABI Wait(void* arg) { return nullptr; } +static void RequireInvalidArguments() { + KernelSema rejected = nullptr; + Require(sceKernelCreateSema(&rejected, "rejected", 0, 5, 3, nullptr) == SCE_KERNEL_ERROR_EINVAL); + Require(sceKernelCreateSema(&rejected, "rejected", 0, 0, 0, nullptr) == SCE_KERNEL_ERROR_EINVAL); + Require(sceKernelCreateSema(&rejected, "rejected", 0, -1, 3, nullptr) == SCE_KERNEL_ERROR_EINVAL); + Require(sceKernelCreateSema(&rejected, nullptr, 0, 0, 1, nullptr) == SCE_KERNEL_ERROR_EINVAL); + Require(rejected == nullptr); + + KernelSema sem = nullptr; + Require(sceKernelCreateSema(&sem, "limits", 0, 2, 3, nullptr) == SCE_OK); + Require(sceKernelPollSema(sem, 0) == SCE_KERNEL_ERROR_EINVAL); + Require(sceKernelPollSema(sem, -1) == SCE_KERNEL_ERROR_EINVAL); + Require(sceKernelPollSema(sem, 4) == SCE_KERNEL_ERROR_EINVAL); + Require(sceKernelPollSema(sem, 100) == SCE_KERNEL_ERROR_EINVAL); + Require(sceKernelPollSema(sem, 3) == SCE_KERNEL_ERROR_EBUSY); + Require(sceKernelSignalSema(sem, 0) == SCE_KERNEL_ERROR_EINVAL); + Require(sceKernelPollSema(sem, 2) == SCE_OK); + Require(sceKernelSignalSema(sem, 3) == SCE_OK); + Require(sceKernelPollSema(sem, 3) == SCE_OK); + Require(sceKernelDeleteSema(sem) == SCE_OK); +} + int main() { KernelUseconds nullTimeout = 1000; int nullWaiters = -1; @@ -94,4 +116,6 @@ int main() { Require(sceKernelSignalSema(sem, 1) == SCE_OK); Require(sceKernelWaitSema(sem, 1, nullptr) == SCE_OK); Require(sceKernelDeleteSema(sem) == SCE_OK); + + RequireInvalidArguments(); } diff --git a/core/libs/tests/GuestShmOpen.cpp b/core/libs/tests/GuestShmOpen.cpp index ff4f8fb16c..c81cfc704d 100644 --- a/core/libs/tests/GuestShmOpen.cpp +++ b/core/libs/tests/GuestShmOpen.cpp @@ -68,13 +68,7 @@ int main() { RequireInvalid("/anyps5-shm", 0x1); RequireInvalid("/anyps5-shm", 0x202 | 0x8); #ifdef _WIN32 - bool threw = false; - try { - shm_open_nid_postfix("/anyps5-shm", 0x202, 0600); - } catch (const std::runtime_error&) { - threw = true; - } - Require(threw); + RequireInvalid("", 0x202); #else RequireInvalid("", 0x202); #endif diff --git a/core/libs/tests/GuestSignals.cpp b/core/libs/tests/GuestSignals.cpp index a5ca92484b..9879994a1a 100644 --- a/core/libs/tests/GuestSignals.cpp +++ b/core/libs/tests/GuestSignals.cpp @@ -29,6 +29,7 @@ int APS5_VABI sigsuspend_nid_postfix(const GuestSignalSet*); int APS5_VABI _is_signal_return_nid_postfix(std::uint64_t); unsigned int APS5_VABI GuestAlarm_nid_no_patch(unsigned int); int APS5_VABI sigpending_nid_postfix(GuestSignalSet*); +int APS5_VABI siginterrupt_nid_postfix(int, int); } struct GuestSigaction { std::uintptr_t handler; @@ -399,6 +400,54 @@ int main() { Require(sigpending_nid_postfix(&pendingIgnored) == 0 && pendingIgnored.bits[0] == 0); Require(sigprocmask_nid_postfix(3, &noneBlocked, nullptr) == 0); Require(sigaction_nid_postfix(30, &restored, nullptr) == 0); + + const GuestSigaction caught{callback, 0, {}}; + received = 0; + Require(sigaction_nid_postfix(30, &caught, nullptr) == 0); + Require(sigprocmask_nid_postfix(3, &userSignal, nullptr) == 0); + Require(raise_nid_postfix(30) == 0 && received == 0); + Require(sigpending_nid_postfix(&pendingIgnored) == 0 && pendingIgnored.bits[0] == userSignal.bits[0]); + Require(sigaction_nid_postfix(30, &restored, nullptr) == 0); + Require(sigpending_nid_postfix(&pendingIgnored) == 0 && pendingIgnored.bits[0] == userSignal.bits[0]); + Require(sigaction_nid_postfix(30, &ignored, nullptr) == 0); + Require(sigpending_nid_postfix(&pendingIgnored) == 0 && pendingIgnored.bits[0] == 0); + Require(sigaction_nid_postfix(30, &caught, nullptr) == 0); + Require(sigprocmask_nid_postfix(3, &noneBlocked, nullptr) == 0 && received == 0); + Require(sigprocmask_nid_postfix(3, &userSignal, nullptr) == 0); + Require(raise_nid_postfix(30) == 0 && received == 0); + Require(signal_nid_postfix(30, ignore) == Callback); + Require(sigpending_nid_postfix(&pendingIgnored) == 0 && pendingIgnored.bits[0] == 0); + Require(signal_nid_postfix(30, Callback) == ignore); + Require(sigprocmask_nid_postfix(3, &noneBlocked, nullptr) == 0 && received == 0); + Require(signal_nid_postfix(30, nullptr) == Callback); + + const GuestSignalSet windowSignal{{1u << 27, 0, 0, 0}}; + Require(sigaction_nid_postfix(28, &caught, nullptr) == 0); + Require(sigprocmask_nid_postfix(3, &windowSignal, nullptr) == 0); + Require(raise_nid_postfix(28) == 0 && received == 0); + Require(sigpending_nid_postfix(&pendingIgnored) == 0 && pendingIgnored.bits[0] == windowSignal.bits[0]); + Require(sigaction_nid_postfix(28, &restored, nullptr) == 0); + Require(sigpending_nid_postfix(&pendingIgnored) == 0 && pendingIgnored.bits[0] == 0); + Require(sigaction_nid_postfix(28, &caught, nullptr) == 0); + Require(sigprocmask_nid_postfix(3, &noneBlocked, nullptr) == 0 && received == 0); + Require(sigaction_nid_postfix(28, &restored, nullptr) == 0); + + const GuestSignalSet continueSignal{{1u << 18, 0, 0, 0}}; + Require(sigprocmask_nid_postfix(3, &continueSignal, nullptr) == 0); + Require(raise_nid_postfix(19) == 0); + Require(sigpending_nid_postfix(&pendingIgnored) == 0 && pendingIgnored.bits[0] == continueSignal.bits[0]); + Require(signal_nid_postfix(19, nullptr) == nullptr); + Require(sigpending_nid_postfix(&pendingIgnored) == 0 && pendingIgnored.bits[0] == 0); + Require(sigprocmask_nid_postfix(3, &noneBlocked, nullptr) == 0 && received == 0); + + const GuestSignalSet highSignal{{0, 1u << 7, 0, 0}}; + Require(sigprocmask_nid_postfix(3, &highSignal, nullptr) == 0); + Require(raise_nid_postfix(40) == 0); + Require(sigpending_nid_postfix(&pendingIgnored) == 0 && pendingIgnored.bits[1] == highSignal.bits[1]); + Require(sigaction_nid_postfix(40, &ignored, nullptr) == 0); + Require(sigpending_nid_postfix(&pendingIgnored) == 0 && pendingIgnored.bits[0] == 0 && pendingIgnored.bits[1] == 0); + Require(sigaction_nid_postfix(40, &restored, nullptr) == 0); + Require(sigprocmask_nid_postfix(3, &noneBlocked, nullptr) == 0); Require(_sigintr_nid_postfix.bits[0] == 0 && _sigintr_nid_postfix.bits[1] == 0 && _sigintr_nid_postfix.bits[2] == 0 && _sigintr_nid_postfix.bits[3] == 0); GuestSigaction interruptible{}; @@ -419,4 +468,24 @@ int main() { Require(sigaction_nid_postfix(30, nullptr, &interruptible) == 0 && interruptible.flags == 0x2); Require(signal_nid_postfix(30, nullptr) == Callback && signal_nid_postfix(31, nullptr) == Callback && signal_nid_postfix(128, nullptr) == Callback); + Require(signal_nid_postfix(30, Callback) == nullptr); + Require(siginterrupt_nid_postfix(30, 1) == 0 && _sigintr_nid_postfix.bits[0] == (1u << 29)); + Require(sigaction_nid_postfix(30, nullptr, &interruptible) == 0 && + interruptible.handler == reinterpret_cast(Callback) && interruptible.flags == 0); + Require(signal_nid_postfix(30, Callback) == Callback); + Require(sigaction_nid_postfix(30, nullptr, &interruptible) == 0 && interruptible.flags == 0); + Require(siginterrupt_nid_postfix(30, 0) == 0 && _sigintr_nid_postfix.bits[0] == 0); + Require(sigaction_nid_postfix(30, nullptr, &interruptible) == 0 && + interruptible.handler == reinterpret_cast(Callback) && interruptible.flags == 0x2); + Require(siginterrupt_nid_postfix(128, 1) == 0 && _sigintr_nid_postfix.bits[3] == (1u << 31)); + Require(sigaction_nid_postfix(128, nullptr, &interruptible) == 0 && interruptible.handler == 0 && interruptible.flags == 0); + Require(siginterrupt_nid_postfix(128, 0) == 0 && _sigintr_nid_postfix.bits[3] == 0); + Require(sigaction_nid_postfix(128, nullptr, &interruptible) == 0 && interruptible.flags == 0x2); + for (const int badSignal : {0, -1, 129}) { + *__error_nid_postfix() = 0; + Require(siginterrupt_nid_postfix(badSignal, 1) == -1 && *__error_nid_postfix() == 22); + } + Require(_sigintr_nid_postfix.bits[0] == 0 && _sigintr_nid_postfix.bits[3] == 0); + Require(signal_nid_postfix(30, nullptr) == Callback); + Require(sigaction_nid_postfix(128, &restored, nullptr) == 0); } diff --git a/core/libs/tests/GuestSocketEvents.cpp b/core/libs/tests/GuestSocketEvents.cpp index 1ef4eea859..20ecb18f99 100644 --- a/core/libs/tests/GuestSocketEvents.cpp +++ b/core/libs/tests/GuestSocketEvents.cpp @@ -1,9 +1,12 @@ #include "SceTypes.hpp" #include +#include #include #include #include +#include #include +#include #include extern "C" { @@ -19,6 +22,8 @@ int APS5_VABI close_nid_postfix(int); int APS5_VABI pipe_nid_postfix(int*); std::int64_t APS5_VABI read_nid_postfix(int, void*, std::uint64_t); std::int64_t APS5_VABI write_nid_postfix(int, const char*, std::int64_t); +int APS5_VABI open_nid_postfix(const char*, int, int); +std::int64_t APS5_VABI lseek_nid_postfix(int, std::int64_t, int); int APS5_VABI sceKernelCreateEqueue(KernelEqueue* eq, const char* name); int APS5_VABI sceKernelDeleteEqueue(KernelEqueue eq); int APS5_VABI sceKernelWaitEqueue(KernelEqueue eq, KernelEvent* ev, int num, int* out, const KernelUseconds* timo); @@ -96,8 +101,83 @@ static void PipeReadEvents() { Require(sceKernelDeleteEqueue(eq) == SCE_OK); } +static void RegularFileEvents() { + const std::string path = "anyps5-equeue-file-" + std::to_string(std::chrono::steady_clock::now().time_since_epoch().count()); + const int file = open_nid_postfix(path.c_str(), 0x0002 | 0x0200, 0644); + Require(file >= 0); + Require(write_nid_postfix(file, "0123456789", 10) == 10); + Require(lseek_nid_postfix(file, 2, 0) == 2); + KernelEqueue eq = 0; + Require(sceKernelCreateEqueue(&eq, "file") == SCE_OK); + const KernelUseconds shortTimeout = 20000; + const KernelUseconds timeout = 1000000; + int marker = 0; + int count = 0; + std::array events{}; + Require(sceKernelAddReadEvent(eq, file, 1, &marker) == SCE_OK); + Require(sceKernelWaitEqueue(eq, events.data(), events.size(), &count, &timeout) == SCE_OK); + Require(count == 1 && events[0].ident == static_cast(file) && events[0].filter == EVFILT_READ); + Require(events[0].data == 8 && events[0].udata == &marker); + Require(lseek_nid_postfix(file, 0, 2) == 10); + Require(sceKernelWaitEqueue(eq, events.data(), events.size(), &count, &shortTimeout) == SCE_KERNEL_ERROR_ETIMEDOUT); + const int appender = open_nid_postfix(path.c_str(), 0x0002, 0); + Require(appender >= 0); + std::thread writer([appender] { + std::this_thread::sleep_for(std::chrono::milliseconds(50)); + Require(lseek_nid_postfix(appender, 0, 2) == 10); + Require(write_nid_postfix(appender, "abc", 3) == 3); + }); + Require(sceKernelWaitEqueue(eq, events.data(), events.size(), &count, nullptr) == SCE_OK); + writer.join(); + Require(count == 1 && events[0].data == 3); + Require(sceKernelDeleteReadEvent(eq, file) == SCE_OK); + Require(sceKernelAddWriteEvent(eq, file, 0, nullptr) == SCE_OK); + Require(sceKernelWaitEqueue(eq, events.data(), events.size(), &count, &timeout) == SCE_OK); + Require(count == 1 && events[0].filter == EVFILT_WRITE && events[0].data == 0); + Require(sceKernelDeleteWriteEvent(eq, file) == SCE_OK); + Require(Rejects(sceKernelAddReadEvent, eq, file, 64)); + Require(Rejects(sceKernelAddWriteEvent, eq, file, 64)); + Require(sceKernelAddReadEvent(eq, file, 0, nullptr) == SCE_OK); + Require(close_nid_postfix(file) == 0); + Require(sceKernelDeleteReadEvent(eq, file) == SCE_KERNEL_ERROR_ENOENT); + Require(sceKernelAddReadEvent(eq, file, 0, nullptr) == SCE_KERNEL_ERROR_EBADF); + Require(close_nid_postfix(appender) == 0); + Require(sceKernelDeleteEqueue(eq) == SCE_OK); + Require(std::filesystem::remove(path)); +} + +static void CloseWhileWaitingOnFile() { + const std::string path = "anyps5-equeue-close-" + std::to_string(std::chrono::steady_clock::now().time_since_epoch().count()); + const int created = open_nid_postfix(path.c_str(), 0x0002 | 0x0200, 0644); + Require(created >= 0); + Require(close_nid_postfix(created) == 0); + KernelEqueue eq = 0; + Require(sceKernelCreateEqueue(&eq, "close") == SCE_OK); + std::atomic stop{false}; + std::thread waiter([eq, &stop] { + const KernelUseconds timeout = 1000; + std::array events{}; + int count = 0; + while (!stop.load()) { + Require(sceKernelWaitEqueue(eq, events.data(), events.size(), &count, &timeout) == SCE_KERNEL_ERROR_ETIMEDOUT); + } + }); + for (int i = 0; i < 2000; ++i) { + const int file = open_nid_postfix(path.c_str(), 0x0002, 0); + Require(file >= 0); + Require(sceKernelAddReadEvent(eq, file, 0, nullptr) == SCE_OK); + Require(close_nid_postfix(file) == 0); + } + stop.store(true); + waiter.join(); + Require(sceKernelDeleteEqueue(eq) == SCE_OK); + Require(std::filesystem::remove(path)); +} + int main() { PipeReadEvents(); + RegularFileEvents(); + CloseWhileWaitingOnFile(); const int listener = socket_nid_postfix(2, 1, 6); Require(listener >= 0); std::array address{16, 2, 0, 0, 127, 0, 0, 1}; diff --git a/core/libs/tests/GuestSocketOptions.cpp b/core/libs/tests/GuestSocketOptions.cpp index 7ab20ba2f4..f3d2decabc 100644 --- a/core/libs/tests/GuestSocketOptions.cpp +++ b/core/libs/tests/GuestSocketOptions.cpp @@ -14,6 +14,7 @@ int APS5_VABI connect_nid_postfix(int, const void*, std::uint32_t); int APS5_VABI accept_nid_postfix(int, void*, std::uint32_t*); int APS5_VABI setsockopt_nid_postfix(int, int, int, const void*, std::uint32_t); int APS5_VABI getsockopt_nid_postfix(int, int, int, void*, std::uint32_t*); +int APS5_VABI socketpair_nid_postfix(int, int, int, int*); int APS5_VABI fcntl_nid_postfix(int, int, ...); int APS5_VABI poll_nid_postfix(void*, std::uint32_t, int); int APS5_VABI close_nid_postfix(int); @@ -35,6 +36,9 @@ constexpr int SoReusePort = 0x200; constexpr int SoTimestamp = 0x400; constexpr int SoNoSigpipe = 0x800; constexpr int SoSendBuffer = 0x1001; +constexpr int SoReceiveBuffer = 0x1002; +constexpr int SoSendLowWater = 0x1003; +constexpr int SoReceiveLowWater = 0x1004; constexpr int SoError = 0x1007; constexpr int SoType = 0x1008; constexpr int ErrorInvalid = 22; @@ -154,16 +158,41 @@ void BooleanOptionsReportTheirBit() { Require(GetInt(datagram, SoBroadcast) == SoBroadcast, "SO_BROADCAST reads back as its bit"); SetInt(stream, SoNoSigpipe, 1); Require(GetInt(stream, SoNoSigpipe) == SoNoSigpipe, "SO_NOSIGPIPE reads back as its bit"); - for (const int option : {SoReusePort, SoDebug, SoUseLoopback, SoTimestamp}) { - const int one = 1; - Require(SetFails(stream, SolSocket, option, &one, sizeof(one), ErrorNoProtocolOption), "an option no title needs must fail with ENOPROTOOPT (42)"); + for (const int option : {SoReusePort, SoDebug, SoUseLoopback, SoTimestamp, 0x2000, 0x10000}) { + Require(GetInt(stream, option) == 0, "a kept flag starts off"); + SetInt(stream, option, 5); + Require(GetInt(stream, option) == option, "a kept flag reads back as its bit"); + SetInt(stream, option, 0); + Require(GetInt(stream, option) == 0, "a kept flag turns off"); + } + for (const int option : {0x4000, 0x8000}) { + SetInt(stream, option, 1); int value = 0; std::uint32_t size = sizeof(value); - Require(getsockopt_nid_postfix(stream, SolSocket, option, &value, &size) == -1 && *__error_nid_postfix() == ErrorNoProtocolOption, "getsockopt of an option no title needs must fail with ENOPROTOOPT (42)"); + Require(getsockopt_nid_postfix(stream, SolSocket, option, &value, &size) == -1 && *__error_nid_postfix() == ErrorNoProtocolOption, "SO_NO_OFFLOAD and SO_NO_DDP are accepted but cannot be read"); } + const unsigned char narrowFlag = 1; + Require(SetFails(stream, SolSocket, SoDebug, &narrowFlag, 1, ErrorInvalid), "a flag shorter than an int must fail with EINVAL (22)"); Require(close_nid_postfix(stream) == 0 && close_nid_postfix(datagram) == 0, "close failed"); } +void ReusePortSharesAPort() { + const int first = socket_nid_postfix(2, Datagram, 0); + const int second = socket_nid_postfix(2, Datagram, 0); + const int third = socket_nid_postfix(2, Datagram, 0); + Require(first >= 0 && second >= 0 && third >= 0, "socket failed"); + SetInt(first, SoReusePort, 1); + SetInt(second, SoReusePort, 1); + Require(GetInt(first, SoReusePort) == SoReusePort && GetInt(first, 0x4) == 0, "SO_REUSEPORT does not read back as SO_REUSEADDR"); + auto address = Loopback(0); + Require(bind_nid_postfix(first, address.data(), address.size()) == 0, "bind failed"); + std::uint32_t size = address.size(); + Require(getsockname_nid_postfix(first, address.data(), &size) == 0, "getsockname failed"); + Require(bind_nid_postfix(second, address.data(), address.size()) == 0, "a second SO_REUSEPORT socket must bind the same port"); + Require(bind_nid_postfix(third, address.data(), address.size()) == -1 && *__error_nid_postfix() == 48, "a socket without SO_REUSEPORT must fail with EADDRINUSE (48)"); + Require(close_nid_postfix(first) == 0 && close_nid_postfix(second) == 0 && close_nid_postfix(third) == 0, "close failed"); +} + void LingerFollowsFreeBsd() { const int stream = socket_nid_postfix(2, Stream, 0); Require(stream >= 0, "socket failed"); @@ -204,6 +233,171 @@ void LengthsFollowFreeBsd() { Require(close_nid_postfix(stream) == 0, "close failed"); } +void BuffersFollowThePs5() { + const int stream = socket_nid_postfix(2, Stream, 0); + const int datagram = socket_nid_postfix(28, Datagram, 0); + Require(stream >= 0 && datagram >= 0, "socket failed"); + Require(GetInt(stream, SoSendBuffer) == 32768 && GetInt(stream, SoReceiveBuffer) == 65536, "a stream socket starts with 32768 and 65536 bytes"); + Require(GetInt(stream, SoSendLowWater) == 2048 && GetInt(stream, SoReceiveLowWater) == 1, "the low-water marks start at 2048 and 1"); + Require(GetInt(datagram, SoSendBuffer) == 9216 && GetInt(datagram, SoReceiveBuffer) == 42080, "a datagram socket starts with 9216 and 42080 bytes"); + SetInt(stream, SoSendBuffer, 1000); + Require(GetInt(stream, SoSendBuffer) == 1000 && GetInt(stream, SoSendLowWater) == 1000, "a smaller buffer is kept and lowers its low-water mark"); + SetInt(stream, SoReceiveBuffer, 524289); + Require(GetInt(stream, SoReceiveBuffer) == 524288, "a buffer above 512 KiB is limited to 524288 bytes"); + SetInt(stream, SoSendBuffer, 5592405); + Require(GetInt(stream, SoSendBuffer) == 524288, "the largest reservation is accepted and limited"); + const int tooLarge = 5592406; + Require(SetFails(stream, SolSocket, SoSendBuffer, &tooLarge, sizeof(tooLarge), 55), "a reservation above sb_max must fail with ENOBUFS (55)"); + Require(GetInt(stream, SoSendBuffer) == 524288, "a failed reservation keeps the buffer"); + SetInt(stream, SoReceiveLowWater, 600000); + Require(GetInt(stream, SoReceiveLowWater) == 524288, "a low-water mark above the buffer is limited to it"); + SetInt(stream, SoReceiveBuffer, 50); + Require(GetInt(stream, SoReceiveLowWater) == 50, "a smaller buffer lowers the low-water mark"); + const int zero = 0; + Require(SetFails(stream, SolSocket, SoReceiveLowWater, &zero, sizeof(zero), ErrorInvalid), "a low-water mark below 1 must fail with EINVAL (22)"); + int pair[2] = {-1, -1}; + Require(socketpair_nid_postfix(1, Stream, 0, pair) == 0, "socketpair failed"); + Require(GetInt(pair[0], SoSendBuffer) == 8192 && GetInt(pair[0], SoReceiveBuffer) == 8192, "an AF_UNIX stream socket starts with 8192 bytes each way"); + Require(close_nid_postfix(pair[0]) == 0 && close_nid_postfix(pair[1]) == 0, "close failed"); + Require(socketpair_nid_postfix(1, Datagram, 0, pair) == 0, "socketpair failed"); + Require(GetInt(pair[0], SoSendBuffer) == 2048 && GetInt(pair[0], SoReceiveBuffer) == 4096, "an AF_UNIX datagram socket starts with 2048 and 4096 bytes"); + Require(close_nid_postfix(pair[0]) == 0 && close_nid_postfix(pair[1]) == 0, "close failed"); + Require(close_nid_postfix(stream) == 0 && close_nid_postfix(datagram) == 0, "close failed"); + const int listener = socket_nid_postfix(2, Stream, 0); + Require(listener >= 0, "socket failed"); + auto address = Loopback(0); + Require(bind_nid_postfix(listener, address.data(), address.size()) == 0 && listen_nid_postfix(listener, 1) == 0, "listen failed"); + std::uint32_t size = address.size(); + Require(getsockname_nid_postfix(listener, address.data(), &size) == 0, "getsockname failed"); + SetInt(listener, SoReceiveBuffer, 3000); + SetInt(listener, SoSendLowWater, 700); + const int client = socket_nid_postfix(2, Stream, 0); + Require(client >= 0 && connect_nid_postfix(client, address.data(), address.size()) == 0, "connect failed"); + const int accepted = accept_nid_postfix(listener, nullptr, nullptr); + Require(accepted >= 0, "accept failed"); + Require(GetInt(accepted, SoReceiveBuffer) == 3000 && GetInt(accepted, SoSendLowWater) == 700 && GetInt(accepted, SoSendBuffer) == 32768, "an accepted socket takes the listener's buffers"); + Require(close_nid_postfix(accepted) == 0 && close_nid_postfix(client) == 0 && close_nid_postfix(listener) == 0, "close failed"); +} + +void NullValues() { + const int stream = socket_nid_postfix(2, Stream, 0); + Require(stream >= 0, "socket failed"); + Require(SetFails(stream, SolSocket, SoKeepAlive, nullptr, sizeof(int), 14), "a null value with a length must fail with EFAULT (14)"); + Require(SetFails(-1, SolSocket, SoKeepAlive, nullptr, sizeof(int), 14), "the null value is checked before the descriptor"); + Require(SetFails(stream, SolSocket, 0x7fff, nullptr, sizeof(int), 14), "the null value is checked before the option"); + Require(SetFails(stream, SolSocket, SoKeepAlive, nullptr, 0, ErrorInvalid), "a null value without a length must fail with EINVAL (22)"); + std::uint32_t size = sizeof(int); + Require(getsockopt_nid_postfix(stream, SolSocket, SoSendBuffer, nullptr, &size) == 0 && size == 0, "getsockopt without a value succeeds with length 0"); + Require(close_nid_postfix(stream) == 0, "close failed"); +} + +int GetIp(int descriptor, int option) { + int value = -12345; + std::uint32_t size = sizeof(value); + Require(getsockopt_nid_postfix(descriptor, 0, option, &value, &size) == 0 && size == sizeof(value), "getsockopt of an IPPROTO_IP option failed"); + return value; +} + +bool SetIp(int descriptor, int option, int value) { + return setsockopt_nid_postfix(descriptor, 0, option, &value, sizeof(value)) == 0; +} + +void IpLevelFollowsThePs5() { + constexpr int IpTypeOfService = 3; + constexpr int IpTimeToLive = 4; + constexpr int IpMulticastTimeToLive = 10; + constexpr int IpMulticastLoop = 11; + const int datagram = socket_nid_postfix(2, Datagram, 0); + const int datagram6 = socket_nid_postfix(28, Datagram, 0); + Require(datagram >= 0 && datagram6 >= 0, "socket failed"); + Require(GetIp(datagram, IpTimeToLive) == 64 && GetIp(datagram, IpTypeOfService) == 0, "IP_TTL starts at 64 and IP_TOS at 0"); + Require(GetIp(datagram, IpMulticastTimeToLive) == 1 && GetIp(datagram, IpMulticastLoop) == 1, "IP_MULTICAST_TTL and IP_MULTICAST_LOOP start at 1"); + Require(SetIp(datagram, IpTimeToLive, 32) && GetIp(datagram, IpTimeToLive) == 32, "IP_TTL reads back"); + Require(SetIp(datagram, IpTimeToLive, 300) && GetIp(datagram, IpTimeToLive) == 44, "IP_TTL keeps the low byte"); + Require(SetIp(datagram, IpTimeToLive, -1) && GetIp(datagram, IpTimeToLive) == 255, "IP_TTL of -1 reads back as 255"); + Require(SetIp(datagram, IpTimeToLive, 0) && GetIp(datagram, IpTimeToLive) == 0, "IP_TTL of 0 is kept"); + const unsigned char byte = 9; + Require(SetFails(datagram, 0, IpTimeToLive, &byte, 1, ErrorInvalid), "a one-byte IP_TTL must fail with EINVAL (22)"); + Require(SetIp(datagram, IpTypeOfService, 0x1ff) && GetIp(datagram, IpTypeOfService) == 0xff, "IP_TOS keeps the low byte"); + const unsigned char seven = 7; + Require(setsockopt_nid_postfix(datagram, 0, IpMulticastTimeToLive, &seven, 1) == 0 && GetIp(datagram, IpMulticastTimeToLive) == 7, "a one-byte IP_MULTICAST_TTL is accepted"); + const int tooLarge = 300; + Require(SetFails(datagram, 0, IpMulticastTimeToLive, &tooLarge, sizeof(tooLarge), ErrorInvalid) && GetIp(datagram, IpMulticastTimeToLive) == 7, "IP_MULTICAST_TTL above 255 must fail with EINVAL (22)"); + unsigned char narrow = 0xaa; + std::uint32_t size = 1; + Require(getsockopt_nid_postfix(datagram, 0, IpMulticastTimeToLive, &narrow, &size) == 0 && size == 1 && narrow == 7, "a one-byte read gets the IP_MULTICAST_TTL byte"); + Require(SetIp(datagram, IpMulticastLoop, 5) && GetIp(datagram, IpMulticastLoop) == 1, "IP_MULTICAST_LOOP reads back as 1"); + Require(SetIp(datagram, IpMulticastLoop, 0) && GetIp(datagram, IpMulticastLoop) == 0, "IP_MULTICAST_LOOP can be turned off"); + Require(SetIp(datagram, IpMulticastLoop, 1) && SetIp(datagram, IpMulticastLoop, 0x100) && GetIp(datagram, IpMulticastLoop) == 0, "an int IP_MULTICAST_LOOP keeps only its low byte"); + int value = 0; + size = sizeof(value); + Require(getsockopt_nid_postfix(datagram6, 0, IpTimeToLive, &value, &size) == -1 && *__error_nid_postfix() == ErrorInvalid, "IPPROTO_IP on an AF_INET6 socket must fail with EINVAL (22)"); + size = sizeof(value); + Require(getsockopt_nid_postfix(datagram, 0, 0x7777, &value, &size) == -1 && *__error_nid_postfix() == ErrorNoProtocolOption, "an unknown IPPROTO_IP option must fail with ENOPROTOOPT (42)"); + size = sizeof(value); + Require(getsockopt_nid_postfix(datagram, 99, 1, &value, &size) == -1 && *__error_nid_postfix() == ErrorInvalid, "an unknown level must fail with EINVAL (22)"); + Require(SetFails(datagram, 99, 1, &value, sizeof(value), ErrorInvalid), "setsockopt at an unknown level must fail with EINVAL (22)"); + Require(close_nid_postfix(datagram) == 0 && close_nid_postfix(datagram6) == 0, "close failed"); +} + +void MulticastMembership() { + constexpr int IpMulticastInterface = 9; + constexpr int IpAddMembership = 12; + constexpr int IpDropMembership = 13; + constexpr int ErrorNotSupported = 45; + constexpr int ErrorAddressNotAvailable = 49; + const int datagram = socket_nid_postfix(2, Datagram, 0); + const int stream = socket_nid_postfix(2, Stream, 0); + Require(datagram >= 0 && stream >= 0, "socket failed"); + const std::array group{224, 0, 0, 251, 0, 0, 0, 0}; + const std::array unicast{10, 0, 0, 1, 0, 0, 0, 0}; + const std::array other{224, 0, 0, 253, 0, 0, 0, 0}; + Require(SetFails(stream, 0, IpAddMembership, group.data(), group.size(), ErrorNotSupported), "a stream socket must fail a membership with EOPNOTSUPP (45)"); + Require(SetFails(datagram, 0, IpAddMembership, group.data(), 7, ErrorInvalid), "a short membership must fail with EINVAL (22)"); + Require(SetFails(datagram, 0, IpAddMembership, unicast.data(), unicast.size(), ErrorInvalid), "a group that is not multicast must fail with EINVAL (22)"); + Require(SetFails(datagram, 0, IpDropMembership, other.data(), other.size(), ErrorAddressNotAvailable), "dropping a group never joined must fail with EADDRNOTAVAIL (49)"); + std::array address{0xaa, 0xaa, 0xaa, 0xaa}; + std::uint32_t size = address.size(); + Require(getsockopt_nid_postfix(datagram, 0, IpMulticastInterface, address.data(), &size) == 0 && size == 4 && address == std::array{}, "IP_MULTICAST_IF starts at INADDR_ANY"); + const std::array loopback{127, 0, 0, 1}; + const std::array anyIndex{}; + Require(setsockopt_nid_postfix(datagram, 0, IpMulticastInterface, anyIndex.data(), anyIndex.size()) == 0, "an ip_mreqn without an index selects the default interface"); + size = address.size(); + Require(getsockopt_nid_postfix(datagram, 0, IpMulticastInterface, address.data(), &size) == 0 && address == std::array{}, "the default interface reads back as INADDR_ANY"); + Require(SetFails(datagram, 0, IpMulticastInterface, loopback.data(), 2, ErrorInvalid), "a short IP_MULTICAST_IF must fail with EINVAL (22)"); + Require(close_nid_postfix(datagram) == 0 && close_nid_postfix(stream) == 0, "close failed"); +} + +void MulticastOnTheHost() { + constexpr int IpMulticastInterface = 9; + constexpr int IpAddMembership = 12; + constexpr int IpDropMembership = 13; + constexpr int ErrorAddressInUse = 48; + constexpr int ErrorAddressNotAvailable = 49; + const int datagram = socket_nid_postfix(2, Datagram, 0); + Require(datagram >= 0, "socket failed"); + const std::array group{224, 0, 0, 251, 0, 0, 0, 0}; + Require(setsockopt_nid_postfix(datagram, 0, IpAddMembership, group.data(), group.size()) == 0, "joining 224.0.0.251 through INADDR_ANY failed"); + Require(SetFails(datagram, 0, IpAddMembership, group.data(), group.size(), ErrorAddressInUse), "joining twice must fail with EADDRINUSE (48)"); + Require(setsockopt_nid_postfix(datagram, 0, IpDropMembership, group.data(), group.size()) == 0, "drop failed"); + Require(SetFails(datagram, 0, IpDropMembership, group.data(), group.size(), ErrorAddressNotAvailable), "dropping twice must fail with EADDRNOTAVAIL (49)"); + const std::array throughLoopback{224, 0, 0, 252, 127, 0, 0, 1}; + const std::array anyInterface{224, 0, 0, 252, 0, 0, 0, 0}; + Require(setsockopt_nid_postfix(datagram, 0, IpAddMembership, throughLoopback.data(), throughLoopback.size()) == 0, "joining 224.0.0.252 through 127.0.0.1 failed"); + Require(setsockopt_nid_postfix(datagram, 0, IpDropMembership, anyInterface.data(), anyInterface.size()) == 0, "a drop through INADDR_ANY leaves the group on any interface"); + Require(SetFails(datagram, 0, IpDropMembership, anyInterface.data(), anyInterface.size(), ErrorAddressNotAvailable), "dropping it again must fail with EADDRNOTAVAIL (49)"); + const std::array loopback{127, 0, 0, 1}; + Require(setsockopt_nid_postfix(datagram, 0, IpMulticastInterface, loopback.data(), loopback.size()) == 0, "selecting the loopback interface for IP_MULTICAST_IF failed"); + std::array address{0xaa, 0xaa, 0xaa, 0xaa}; + std::uint32_t size = address.size(); + Require(getsockopt_nid_postfix(datagram, 0, IpMulticastInterface, address.data(), &size) == 0 && size == 4 && address == loopback, "IP_MULTICAST_IF reads back"); + const std::array anyIndex{}; + Require(setsockopt_nid_postfix(datagram, 0, IpMulticastInterface, anyIndex.data(), anyIndex.size()) == 0, "an ip_mreqn without an index selects the default interface"); + size = address.size(); + Require(getsockopt_nid_postfix(datagram, 0, IpMulticastInterface, address.data(), &size) == 0 && address == std::array{}, "the default interface reads back as INADDR_ANY"); + Require(close_nid_postfix(datagram) == 0, "close failed"); +} + void UnknownRequestsFail() { const int stream = socket_nid_postfix(2, Stream, 0); Require(stream >= 0, "socket failed"); @@ -218,12 +412,21 @@ void UnknownRequestsFail() { } -int main() { +int main(int argc, char** argv) { + if (argc > 1 && std::strcmp(argv[1], "multicast") == 0) { + MulticastOnTheHost(); + return 0; + } ReportsTypeAndListening(); ReportsAndClearsTheConnectError(); BooleanOptionsReportTheirBit(); + ReusePortSharesAPort(); LingerFollowsFreeBsd(); LengthsFollowFreeBsd(); + BuffersFollowThePs5(); + NullValues(); + IpLevelFollowsThePs5(); + MulticastMembership(); UnknownRequestsFail(); return 0; } diff --git a/core/libs/tests/GuestSocketPortZero.cpp b/core/libs/tests/GuestSocketPortZero.cpp index 070b9c8982..36857ee65e 100644 --- a/core/libs/tests/GuestSocketPortZero.cpp +++ b/core/libs/tests/GuestSocketPortZero.cpp @@ -11,6 +11,7 @@ int APS5_VABI bind_nid_postfix(int, const void*, std::uint32_t); int APS5_VABI listen_nid_postfix(int, int); int APS5_VABI getsockname_nid_postfix(int, void*, std::uint32_t*); int APS5_VABI getpeername_nid_postfix(int, void*, std::uint32_t*); +std::int64_t APS5_VABI send_nid_postfix(int, const void*, std::uint64_t, int); std::int64_t APS5_VABI sendto_nid_postfix(int, const void*, std::uint64_t, int, const void*, std::uint32_t); int APS5_VABI close_nid_postfix(int); int* APS5_VABI __error_nid_postfix(); @@ -45,7 +46,28 @@ static void CheckDatagram(int family) { Require(connect_nid_postfix(probe, address.data(), size) == -1 && *__error_nid_postfix() == 49, "connect to port 0 on an unconnected datagram socket must fail with EADDRNOTAVAIL (49)"); Require(getpeername_nid_postfix(probe, name.data(), &length) == -1 && *__error_nid_postfix() == 57, "still unconnected"); - Require(close_nid_postfix(probe) == 0, "close"); + const int peer = socket_nid_postfix(family, 2, 0); + Require(peer >= 0, "peer"); + const int bound = bind_nid_postfix(peer, address.data(), size); + if (bound < 0 && family == 28 && *__error_nid_postfix() == 49) { + Require(close_nid_postfix(peer) == 0 && close_nid_postfix(probe) == 0, "close"); + return; + } + Require(bound == 0, "bind peer"); + Address target{}; + length = target.size(); + Require(getsockname_nid_postfix(peer, target.data(), &length) == 0, "peer name"); + Require(connect_nid_postfix(probe, target.data(), size) == 0, "connect to the peer"); + length = name.size(); + Require(getpeername_nid_postfix(probe, name.data(), &length) == 0, "connected"); + Require(connect_nid_postfix(probe, address.data(), size) == -1 && *__error_nid_postfix() == 49, + "connect to port 0 on a connected datagram socket must fail with EADDRNOTAVAIL (49)"); + length = name.size(); + Require(getpeername_nid_postfix(probe, name.data(), &length) == -1 && *__error_nid_postfix() == 57, + "connect to port 0 must disconnect a connected datagram socket"); + Require(send_nid_postfix(probe, "x", 1, 0) == -1 && *__error_nid_postfix() == 39, + "send without an address after the disconnect must fail with EDESTADDRREQ (39)"); + Require(close_nid_postfix(peer) == 0 && close_nid_postfix(probe) == 0, "close"); } static void CheckStream(int family) { diff --git a/core/libs/tests/GuestSocketStreamOptions.cpp b/core/libs/tests/GuestSocketStreamOptions.cpp index f85a693f17..58c21ea728 100644 --- a/core/libs/tests/GuestSocketStreamOptions.cpp +++ b/core/libs/tests/GuestSocketStreamOptions.cpp @@ -91,12 +91,13 @@ int main() { Require(client >= 0 && connect_nid_postfix(client, address.data(), address.size()) == 0); const int server = accept_nid_postfix(listener, nullptr, nullptr); Require(server >= 0); - Require(Set(listener, SocketLevel, NoSigpipe, 1) && Set(listener, TcpLevel, TcpNoDelay, 1)); + Require(Set(listener, SocketLevel, NoSigpipe, 1) && Set(listener, TcpLevel, TcpNoDelay, 1) && Set(listener, TcpLevel, 0x100, 60)); const int secondClient = socket_nid_postfix(2, 1, 0); Require(secondClient >= 0 && connect_nid_postfix(secondClient, address.data(), address.size()) == 0); const int inheriting = accept_nid_postfix(listener, nullptr, nullptr); Require(inheriting >= 0); Require(Get(inheriting, SocketLevel, NoSigpipe) == NoSigpipe && Get(inheriting, TcpLevel, TcpNoDelay) == 4); + Require(Get(inheriting, TcpLevel, 0x100) == 60 && Get(server, TcpLevel, 0x100) == 7200); Require(Get(server, SocketLevel, NoSigpipe) == 0 && Get(server, TcpLevel, TcpNoDelay) == 0); Require(close_nid_postfix(inheriting) == 0 && close_nid_postfix(secondClient) == 0); const int datagram = socket_nid_postfix(2, 2, 0); @@ -114,7 +115,21 @@ int main() { int value = 0; Require(Set(client, TcpLevel, TcpNoDelay, 0) && Get(client, TcpLevel, TcpNoDelay) == 0); Require(Fails(setsockopt_nid_postfix(client, TcpLevel, TcpNoDelay, &one, sizeof(one) - 1), Invalid)); - Require(Fails(setsockopt_nid_postfix(client, TcpLevel, TcpMaximumSegment, &one, sizeof(one)), NoProtocolOption)); + Require(Fails(setsockopt_nid_postfix(client, TcpLevel, TcpMaximumSegment, &one, sizeof(one)), Invalid)); + const int fresh = socket_nid_postfix(2, 1, 0); + Require(fresh >= 0 && Get(fresh, TcpLevel, TcpMaximumSegment) == 536); + for (const int segment : {1000, 175, 0, -1}) { + Require(Fails(setsockopt_nid_postfix(fresh, TcpLevel, TcpMaximumSegment, &segment, sizeof(segment)), Invalid)); + } + Require(Set(fresh, TcpLevel, TcpMaximumSegment, 300) && Get(fresh, TcpLevel, TcpMaximumSegment) == 300); + Require(Get(fresh, TcpLevel, 0x100) == 7200 && Get(fresh, TcpLevel, 0x200) == 75 && Get(fresh, TcpLevel, 0x400) == 8); + Require(Set(fresh, TcpLevel, 0x100, 60) && Get(fresh, TcpLevel, 0x100) == 60); + Require(Set(fresh, TcpLevel, 0x100, 0) && Get(fresh, TcpLevel, 0x100) == 7200); + Require(Set(fresh, TcpLevel, 0x200, 10) && Get(fresh, TcpLevel, 0x200) == 10); + Require(Set(fresh, TcpLevel, 0x400, 3) && Get(fresh, TcpLevel, 0x400) == 3); + Require(Set(fresh, TcpLevel, 0x400, -1) && Get(fresh, TcpLevel, 0x400) == -1); + Require(Fails(setsockopt_nid_postfix(fresh, TcpLevel, 0x7777, &one, sizeof(one)), NoProtocolOption)); + Require(close_nid_postfix(fresh) == 0); length = sizeof(value); Require(Fails(setsockopt_nid_postfix(datagram, TcpLevel, TcpNoDelay, &one, sizeof(one)), Invalid)); Require(Fails(getsockopt_nid_postfix(datagram, TcpLevel, TcpNoDelay, &value, &length), Invalid)); @@ -124,13 +139,13 @@ int main() { char received = 0; Require(Get(server, SocketLevel, NonBlockingOption) == 0); Require((fcntl_nid_postfix(server, GetStatusFlags) & NonBlockingFlag) == 0); - Require(Set(server, SocketLevel, NonBlockingOption, 5) && Get(server, SocketLevel, NonBlockingOption) == 1); + Require(Set(server, SocketLevel, NonBlockingOption, 5) && Get(server, SocketLevel, NonBlockingOption) == 0x100); Require((fcntl_nid_postfix(server, GetStatusFlags) & NonBlockingFlag) != 0); Require(Fails(recv_nid_postfix(server, &received, sizeof(received), 0), Again)); Require(Set(server, SocketLevel, NonBlockingOption, 0) && Get(server, SocketLevel, NonBlockingOption) == 0); Require((fcntl_nid_postfix(server, GetStatusFlags) & NonBlockingFlag) == 0); Require(Fails(setsockopt_nid_postfix(server, SocketLevel, NonBlockingOption, &one, sizeof(one) - 1), Invalid)); - Require(Set(datagram, SocketLevel, NonBlockingOption, 1) && Get(datagram, SocketLevel, NonBlockingOption) == 1); + Require(Set(datagram, SocketLevel, NonBlockingOption, 1) && Get(datagram, SocketLevel, NonBlockingOption) == 0x100); Require(Fails(recv_nid_postfix(datagram, &received, sizeof(received), 0), Again)); #ifndef _WIN32 diff --git a/core/libs/tests/GuestStandardStreams.cpp b/core/libs/tests/GuestStandardStreams.cpp index 842964c4ca..70fd7600b2 100644 --- a/core/libs/tests/GuestStandardStreams.cpp +++ b/core/libs/tests/GuestStandardStreams.cpp @@ -745,9 +745,11 @@ int main(int argc, char** argv) { Require(wideBuf[0] == u'E' && wideBuf[1] == u'n' && wideBuf[2] == u'd' && wideBuf[3] == 0); Require(fgetws_nid_postfix(wideBuf, 16, &wideIo) == nullptr); Require(feof_nid_postfix(&wideIo)); - Require(fgetws_nid_postfix(wideBuf, 0, &wideIo) == nullptr && *__error_nid_postfix() == 22); - Require(ferror_nid_postfix(&wideIo)); clearerr_nid_postfix(&wideIo); + *__error_nid_postfix() = 0; + Require(fgetws_nid_postfix(wideBuf, 0, &wideIo) == nullptr && *__error_nid_postfix() == 0); + Require(fgetws_nid_postfix(wideBuf, -1, &wideIo) == nullptr && *__error_nid_postfix() == 0); + Require(!ferror_nid_postfix(&wideIo)); Require(fgetws_nid_postfix(nullptr, 16, &wideIo) == nullptr && *__error_nid_postfix() == 22); Require(ferror_nid_postfix(&wideIo)); clearerr_nid_postfix(&wideIo); @@ -755,7 +757,7 @@ int main(int argc, char** argv) { wideBuf[0] = u'X'; wideBuf[1] = u'Y'; std::rewind(wideIo.GetHandle()); - Require(fgetws_nid_postfix(wideBuf, 1, &wideIo) == wideBuf && wideBuf[0] == 0 && wideBuf[1] == u'Y'); + Require(fgetws_nid_postfix(wideBuf, 1, &wideIo) == nullptr && wideBuf[0] == u'X' && wideBuf[1] == u'Y'); Require(fgetc_nid_postfix(&wideIo) == 'H'); wideIo.Close(); @@ -779,7 +781,7 @@ int main(int argc, char** argv) { Require(fputc_nid_postfix(0xff, &midErr) == 0xff); std::rewind(midErr.GetHandle()); char16_t midBuf[16]{}; - Require(fgetws_nid_postfix(midBuf, 16, &midErr) == nullptr); + Require(fgetws_nid_postfix(midBuf, 16, &midErr) == midBuf && midBuf[0] == u'A' && midBuf[1] == 0); Require(ferror_nid_postfix(&midErr)); midErr.Close(); diff --git a/core/libs/tests/GuestStdThreadHardwareConcurrency.cpp b/core/libs/tests/GuestStdThreadHardwareConcurrency.cpp new file mode 100644 index 0000000000..6b53d172cf --- /dev/null +++ b/core/libs/tests/GuestStdThreadHardwareConcurrency.cpp @@ -0,0 +1,15 @@ +#include "prx/libc/include/general/VabiMacros.hpp" +#include +#include + +extern "C" unsigned APS5_VABI _ZNSt6thread20hardware_concurrencyEv_nid_postfix(); + +int main() { + const unsigned result = _ZNSt6thread20hardware_concurrencyEv_nid_postfix(); + if (result != std::thread::hardware_concurrency()) { + std::fprintf(stderr, "guest processor hint %u differs from std::thread::hardware_concurrency %u\n", + result, std::thread::hardware_concurrency()); + return 1; + } + return 0; +} diff --git a/core/libs/tests/GuestStrings.cpp b/core/libs/tests/GuestStrings.cpp index a30f57eb0a..89aec886ab 100644 --- a/core/libs/tests/GuestStrings.cpp +++ b/core/libs/tests/GuestStrings.cpp @@ -181,18 +181,17 @@ static void CheckSecureStringSizeLimits() { static void CheckCaseInsensitiveComparison() { Require(strcasecmp_nid_postfix("", "") == 0 && strcasecmp_nid_postfix("AbC", "aBc") == 0); - Require(strcasecmp_nid_postfix("A", "b") == 'a' - 'b' && strcasecmp_nid_postfix("b", "A") == 'b' - 'a'); - Require(strcasecmp_nid_postfix("ab", "A") == 'b' && strcasecmp_nid_postfix("AB", "a") == 'b'); - Require(strcasecmp_nid_postfix("A", "aB") == -'b' && strcasecmp_nid_postfix("a", "AB") == -'b'); - Require(strcasecmp_nid_postfix("", "Z") == -'z' && strcasecmp_nid_postfix("Z", "") == 'z'); - Require(strncasecmp_nid_postfix("abc", "ABD", 2) == 0 && strncasecmp_nid_postfix("abc", "ABD", 3) == 'c' - 'd'); - Require(strncasecmp_nid_postfix("Ab", "a", 2) == 'b' && strncasecmp_nid_postfix("a", "AB", 5) == -'b'); - Require(strncasecmp_nid_postfix("Q", "", 1) == 'q' && strncasecmp_nid_postfix("", "Q", 1) == -'q'); + Require(strcasecmp_nid_postfix("A", "b") == -1 && strcasecmp_nid_postfix("b", "A") == 1); + Require(strcasecmp_nid_postfix("ab", "A") == 1 && strcasecmp_nid_postfix("AB", "a") == 1); + Require(strcasecmp_nid_postfix("A", "aB") == -1 && strcasecmp_nid_postfix("a", "AB") == -1); + Require(strcasecmp_nid_postfix("", "Z") == -1 && strcasecmp_nid_postfix("Z", "") == 1); + Require(strncasecmp_nid_postfix("abc", "ABD", 2) == 0 && strncasecmp_nid_postfix("abc", "ABD", 3) == -1); + Require(strncasecmp_nid_postfix("Ab", "a", 2) == 1 && strncasecmp_nid_postfix("a", "AB", 5) == -1); + Require(strncasecmp_nid_postfix("Q", "", 1) == 1 && strncasecmp_nid_postfix("", "Q", 1) == -1); Require(strncasecmp_nid_postfix("x", "y", 0) == 0 && strncasecmp_nid_postfix("same", "SAME", 99) == 0); } static void CheckSscanfS() { -#ifndef _WIN32 int number = 0; char word[4] = "zz"; char letter = 0; @@ -209,7 +208,6 @@ static void CheckSscanfS() { Require(sscanf_s_nid_postfix("7 %", "%d %%%n", &number, &position) == 1 && position == 3); Require(sscanf_s_nid_postfix(" ", "%d", &number) == EOF); Require(sscanf_s_nid_postfix("x", "%d", &number) == 0); -#endif } static bool CheckDirnameLengths() { diff --git a/core/libs/tests/GuestSystemConfiguration.cpp b/core/libs/tests/GuestSystemConfiguration.cpp index a4d441e2e0..e1d7bc3d57 100644 --- a/core/libs/tests/GuestSystemConfiguration.cpp +++ b/core/libs/tests/GuestSystemConfiguration.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include struct SceKernelSystemSwVersion { @@ -24,6 +25,9 @@ int APS5_VABI sysctl_nid_postfix(const int*, std::uint32_t, void*, std::size_t*, int APS5_VABI sysctlbyname_nid_postfix(const char*, void*, std::size_t*, const void*, std::size_t); int APS5_VABI sysctlnametomib_nid_postfix(const char*, int*, std::size_t*); int APS5_VABI sceKernelGetSystemSwVersion_nid_postfix(SceKernelSystemSwVersion*); +int APS5_VABI sceKernelGetSystemExVersion(char*, char*); +int APS5_VABI sysKernelGetLowerLimitSysexVersion_nid_postfix(char*); +int APS5_VABI sysKernelGetLowerLimitUpdVersion_nid_postfix(std::uint32_t*); int APS5_VABI getpid_nid_postfix(); void* APS5_VABI mmap_nid_postfix(void*, std::size_t, int, int, int, std::int64_t); int APS5_VABI munmap_nid_postfix(void*, std::size_t); @@ -406,8 +410,64 @@ static void CheckOsReleaseDateSysctl() { length = sizeof(value); Require(sysctl_nid_postfix(deeper, 3, &value, &length, nullptr, 0) == -1 && *__error_nid_postfix() == 20); } +struct BootTimeval { + std::int64_t tv_sec; + std::int64_t tv_usec; +}; +static_assert(sizeof(BootTimeval) == 16); +struct BootTimespec { + std::int64_t tv_sec; + std::int64_t tv_nsec; +}; +extern "C" int APS5_VABI clock_gettime_nid_postfix(int, BootTimespec*); +static void CheckBootTimeSysctl() { + const int mib[] = {1, 21}; + *__error_nid_postfix() = 13; + std::size_t length = 0; + Require(sysctl_nid_postfix(mib, 2, nullptr, &length, nullptr, 0) == 0 && length == sizeof(BootTimeval)); + length = 0; + Require(sysctlbyname_nid_postfix("kern.boottime", nullptr, &length, nullptr, 0) == 0 && length == sizeof(BootTimeval)); + BootTimeval first{}; + length = sizeof(first); + Require(sysctl_nid_postfix(mib, 2, &first, &length, nullptr, 0) == 0 && length == sizeof(first)); + Require(first.tv_sec > 0 && first.tv_sec <= static_cast(std::time(nullptr))); + Require(first.tv_usec >= 0 && first.tv_usec < 1000000); + BootTimespec now{}; + BootTimespec uptime{}; + Require(clock_gettime_nid_postfix(5, &uptime) == 0 && clock_gettime_nid_postfix(0, &now) == 0); + const std::int64_t drift = (first.tv_sec + uptime.tv_sec - now.tv_sec) * 1000000 + first.tv_usec + uptime.tv_nsec / 1000 - now.tv_nsec / 1000; + Require(drift > -1000000 && drift < 1000000); + BootTimeval second{}; + length = sizeof(second); + Require(sysctl_nid_postfix(mib, 2, &second, &length, nullptr, 0) == 0 && length == sizeof(second)); + Require(second.tv_sec == first.tv_sec && second.tv_usec == first.tv_usec); + BootTimeval named{}; + length = 32; + Require(sysctlbyname_nid_postfix("kern.boottime", &named, &length, nullptr, 0) == 0 && length == sizeof(named)); + Require(named.tv_sec == first.tv_sec && named.tv_usec == first.tv_usec); + Require(*__error_nid_postfix() == 13); + + int namedMib[3] = {0x5a5a5a5a, 0x5a5a5a5a, 0x5a5a5a5a}; + std::size_t mibLength = 3; + Require(sysctlnametomib_nid_postfix("kern.boottime", namedMib, &mibLength) == 0 && mibLength == 2); + Require(namedMib[0] == 1 && namedMib[1] == 21 && namedMib[2] == 0x5a5a5a5a); + + unsigned char truncated[16]; + std::memset(truncated, 0xaa, sizeof(truncated)); + length = 8; + Require(sysctl_nid_postfix(mib, 2, truncated, &length, nullptr, 0) == -1 && *__error_nid_postfix() == 12); + Require(length == 8 && std::memcmp(truncated, &first.tv_sec, 8) == 0 && truncated[8] == 0xaa); + std::memset(truncated, 0xaa, sizeof(truncated)); + length = 8; + Require(sysctlbyname_nid_postfix("kern.boottime", truncated, &length, nullptr, 0) == -1 && *__error_nid_postfix() == 12); + Require(length == 8 && truncated[8] == 0xaa); + + length = sizeof(first); + Require(sysctl_nid_postfix(mib, 2, &first, &length, &first, sizeof(first)) == -1 && *__error_nid_postfix() == 1); + Require(sysctlbyname_nid_postfix("kern.boottime", &first, &length, &first, sizeof(first)) == -1 && *__error_nid_postfix() == 1); +} static void CheckLeafWithTrailingComponent() { - const int leaves[][2] = {{6, 3}, {6, 2}, {6, 5}, {6, 7}, {1, 24}, {1, 37}, {1, 50}}; + const int leaves[][2] = {{6, 3}, {6, 2}, {6, 5}, {6, 7}, {1, 21}, {1, 24}, {1, 37}, {1, 50}}; for (const auto& leaf : leaves) { const int mib[] = {leaf[0], leaf[1], 0}; char buffer[8] = {}; @@ -461,6 +521,31 @@ static void CheckMeasuredAbsentSysctlMibs() { Require(threw); } } +static void CheckVersionLimits() { + unsigned char first[4]; + unsigned char second[4]; + std::memset(first, 0xaa, sizeof(first)); + std::memset(second, 0xaa, sizeof(second)); + *__error_nid_postfix() = 13; + Require(sceKernelGetSystemExVersion(reinterpret_cast(first + 1), reinterpret_cast(second + 1)) == 0 && *__error_nid_postfix() == 13); + Require(first[0] == 0xaa && first[1] == 0 && first[2] == 0xaa && second[0] == 0xaa && second[1] == 0 && second[2] == 0xaa); + std::memset(first, 0xaa, sizeof(first)); + Require(sceKernelGetSystemExVersion(reinterpret_cast(first), nullptr) == 22 && first[0] == 0xaa); + Require(sceKernelGetSystemExVersion(nullptr, reinterpret_cast(first)) == 22 && first[0] == 0xaa); + Require(sceKernelGetSystemExVersion(nullptr, nullptr) == 22 && *__error_nid_postfix() == 13); + + std::memset(first, 0xaa, sizeof(first)); + Require(sysKernelGetLowerLimitSysexVersion_nid_postfix(reinterpret_cast(first + 1)) == 0); + Require(first[0] == 0xaa && first[1] == 0 && first[2] == 0xaa); + Require(sysKernelGetLowerLimitSysexVersion_nid_postfix(nullptr) == SCE_KERNEL_ERROR_EINVAL); + + struct { + std::uint32_t version; + std::uint32_t after; + } update{0xaaaaaaaau, 0xaaaaaaaau}; + Require(sysKernelGetLowerLimitUpdVersion_nid_postfix(&update.version) == 0 && update.version == 0 && update.after == 0xaaaaaaaau); + Require(sysKernelGetLowerLimitUpdVersion_nid_postfix(nullptr) == SCE_KERNEL_ERROR_EINVAL && *__error_nid_postfix() == 13); +} static void CheckSystemSwVersion() { *__error_nid_postfix() = 13; Require(sceKernelGetSystemSwVersion_nid_postfix(nullptr) == SCE_KERNEL_ERROR_EFAULT && *__error_nid_postfix() == 13); @@ -732,6 +817,7 @@ static void CheckNameToMib() { {"hw.physmem", {6, 5}, 2}, {"hw.realmem", {6, 12}, 2}, {"kern.arandom", {1, 37}, 2}, + {"kern.boottime", {1, 21}, 2}, {"kern.rng_pseudo", {1, 50}, 2}, {"kern.proc.pathname", {1, 14, 12}, 3}, }; @@ -833,9 +919,11 @@ int main() { CheckSystemStringSysctl(); CheckSystemStringNames(); CheckOsReleaseDateSysctl(); + CheckBootTimeSysctl(); CheckLeafWithTrailingComponent(); CheckMeasuredAbsentSysctlMibs(); CheckSystemSwVersion(); + CheckVersionLimits(); CheckPseudoRandomSysctl(); CheckSdkVersionSysctl(); CheckOvercommitSysctl(); diff --git a/core/libs/tests/GuestSystemServiceAppTitleId.cpp b/core/libs/tests/GuestSystemServiceAppTitleId.cpp new file mode 100644 index 0000000000..2331f59411 --- /dev/null +++ b/core/libs/tests/GuestSystemServiceAppTitleId.cpp @@ -0,0 +1,136 @@ +#include "SceTypes.hpp" +#include +#include +#include +#include +#include +#include +#include + +extern "C" int APS5_VABI sceSystemServiceGetAppIdOfRunningBigApp(void); +extern "C" int APS5_VABI sceSystemServiceGetAppId(const char* titleId); +extern "C" int APS5_VABI sceSystemServiceGetAppTitleId(int appId, char* titleId); +extern "C" int APS5_VABI sceLncUtilGetAppTitleId(std::int32_t appId, char* titleId); + +namespace { + +constexpr char Sentinel = 0x5A; + +struct GuardedBuffer { + char bytes[10 + 6]; + GuardedBuffer() { std::memset(bytes, Sentinel, sizeof(bytes)); } + bool Untouched() const { + for (const char byte : bytes) { + if (byte != Sentinel) return false; + } + return true; + } + bool GuardIntact() const { + for (std::size_t index = 10; index < sizeof(bytes); ++index) { + if (bytes[index] != Sentinel) return false; + } + return true; + } +}; + +void Require(bool value) { if (!value) std::abort(); } + +template +bool Throws(TCall call) { + try { + call(); + } catch (const std::exception&) { + return true; + } + return false; +} + +bool GetAppIdThrows(const char* titleId) { + return Throws([&] { static_cast(sceSystemServiceGetAppId(titleId)); }); +} + +bool GetAppTitleIdThrows(int appId, GuardedBuffer& buffer) { + return Throws([&] { static_cast(sceSystemServiceGetAppTitleId(appId, buffer.bytes)); }) && buffer.Untouched(); +} + +bool LncGetAppTitleIdThrows(std::uint32_t appId, GuardedBuffer& buffer) { + return Throws([&] { static_cast(sceLncUtilGetAppTitleId(static_cast(appId), buffer.bytes)); }) && buffer.Untouched(); +} + +void CheckOtherApplications(int runningAppId) { + const int otherAppIds[] = {runningAppId + 1, runningAppId - 1, 0, -1, std::numeric_limits::min(), std::numeric_limits::max()}; + for (const int appId : otherAppIds) { + GuardedBuffer buffer; + Require(GetAppTitleIdThrows(appId, buffer)); + Require(LncGetAppTitleIdThrows(static_cast(appId), buffer)); + } + GuardedBuffer buffer; + Require(LncGetAppTitleIdThrows(static_cast(runningAppId) | 0x80000000u, buffer)); + Require(LncGetAppTitleIdThrows(std::numeric_limits::max(), buffer)); +} + +void CheckNullBuffers(int runningAppId) { + Require(Throws([&] { static_cast(sceSystemServiceGetAppTitleId(runningAppId, nullptr)); })); + Require(Throws([&] { static_cast(sceLncUtilGetAppTitleId(runningAppId, nullptr)); })); + Require(Throws([&] { static_cast(sceSystemServiceGetAppTitleId(runningAppId + 1, nullptr)); })); + Require(GetAppIdThrows(nullptr)); +} + +void CheckKnownTitle(int runningAppId) { + constexpr std::string_view titleId = "PPSA00000"; + Require(runningAppId >= 0); + Require(sceSystemServiceGetAppId("PPSA00000") == runningAppId); + Require(sceSystemServiceGetAppId("PPSA00000") == runningAppId); + const char* const otherTitles[] = {"PPSA00001", "PPSA00010", "ppsa00000", "PPSA0000", "PPSA000000", "PPSA00000 ", " PPSA00000", "CUSA00000", ""}; + for (const char* other : otherTitles) { + Require(GetAppIdThrows(other)); + } + + GuardedBuffer buffer; + Require(sceSystemServiceGetAppTitleId(runningAppId, buffer.bytes) == 0); + Require(std::memcmp(buffer.bytes, "PPSA00000", titleId.size() + 1) == 0); + Require(buffer.GuardIntact()); + Require(sceSystemServiceGetAppId(buffer.bytes) == runningAppId); + + GuardedBuffer lncBuffer; + Require(sceLncUtilGetAppTitleId(runningAppId, lncBuffer.bytes) == 0); + Require(std::memcmp(lncBuffer.bytes, "PPSA00000", titleId.size() + 1) == 0); + Require(lncBuffer.GuardIntact()); + + GuardedBuffer dirty; + std::memset(dirty.bytes, 'x', 10); + Require(sceSystemServiceGetAppTitleId(runningAppId, dirty.bytes) == 0); + Require(std::strcmp(dirty.bytes, "PPSA00000") == 0 && dirty.GuardIntact()); + + CheckOtherApplications(runningAppId); + CheckNullBuffers(runningAppId); +} + +void CheckLongTitle(int runningAppId) { + Require(sceSystemServiceGetAppId("PPSA000000") == runningAppId); + Require(GetAppIdThrows("PPSA00000")); + Require(GetAppIdThrows("PPSA0000000")); + GuardedBuffer buffer; + Require(GetAppTitleIdThrows(runningAppId, buffer)); + Require(LncGetAppTitleIdThrows(static_cast(runningAppId), buffer)); +} + +void CheckMissingMetadata(int runningAppId) { + Require(GetAppIdThrows("PPSA00000")); + Require(GetAppIdThrows(nullptr)); + GuardedBuffer buffer; + Require(GetAppTitleIdThrows(runningAppId, buffer)); + Require(LncGetAppTitleIdThrows(static_cast(runningAppId), buffer)); +} + +} + +int main(int argc, char** argv) { + Require(argc == 2); + const std::string_view mode = argv[1]; + const int runningAppId = sceSystemServiceGetAppIdOfRunningBigApp(); + if (mode == "known") CheckKnownTitle(runningAppId); + else if (mode == "long") CheckLongTitle(runningAppId); + else if (mode == "missing") CheckMissingMetadata(runningAppId); + else Require(false); +} diff --git a/core/libs/tests/GuestTimeConversion.cpp b/core/libs/tests/GuestTimeConversion.cpp index 035e0bce3b..57e14fece0 100644 --- a/core/libs/tests/GuestTimeConversion.cpp +++ b/core/libs/tests/GuestTimeConversion.cpp @@ -302,13 +302,70 @@ static void CheckLocalRange() { Require(localtime_s_nid_postfix(&overflow, &scratch) == nullptr, "Unrepresentable negative local time was accepted"); } -#ifndef _WIN32 +#ifdef _WIN32 +extern "C" long* __cdecl __dstbias(void); + +struct DaylightBiasFixture { + long saved = *__dstbias(); + ~DaylightBiasFixture() { *__dstbias() = saved; } + void Apply() const { *__dstbias() = -3600; } +}; + +struct ZoneOffset { + const char* zone; + std::int64_t timer; + long offset; + int isdst; +}; + +static void CheckPosixFormZoneOffsets() { + DaylightBiasFixture bias; + constexpr std::array expectations{{ + {"PST8PDT", 979560000, -28800, 0}, + {"PST8PDT", 995198400, -25200, 1}, + {"EST5EDT", 979560000, -18000, 0}, + {"EST5EDT", 995198400, -14400, 1}, + }}; + for (const ZoneOffset& expected : expectations) { + Require(setenv_nid_postfix("TZ", expected.zone, 1) == 0, "Could not set the POSIX-form time zone"); + tzset_nid_postfix(); + bias.Apply(); + GuestTm local{}; + Require(localtime_s_nid_postfix(&expected.timer, &local) == &local, "Local conversion in a POSIX-form zone failed"); + if (local.tm_gmtoff != expected.offset || local.tm_isdst != expected.isdst) { + long hostTimezone = -1; + long hostDstBias = -1; + _get_timezone(&hostTimezone); + _get_dstbias(&hostDstBias); + std::tm hostLocal{}; + const __time64_t hostTimer = expected.timer; + _localtime64_s(&hostLocal, &hostTimer); + std::fprintf(stderr, "%s at %lld: offset %ld daylight %d, expected %ld daylight %d (host timezone %ld dstbias %ld daylight %d)\n", expected.zone, static_cast(expected.timer), static_cast(local.tm_gmtoff), local.tm_isdst, expected.offset, expected.isdst, hostTimezone, hostDstBias, hostLocal.tm_isdst); + std::abort(); + } + } + Require(setenv_nid_postfix("TZ", "UTC-2", 1) == 0, "Could not restore the guest time zone"); + tzset_nid_postfix(); +} +#endif + static void CheckRegionZones() { +#ifdef _WIN32 + constexpr std::array zones{"PST8PDT", "EST5EDT"}; + constexpr std::array timers{-62135553600, -12609691200, -5016340800, -5000011200, -2732356800, -2209032000, -2208988800, -2208945600, -631108800, 979560000, 995198400}; +#else constexpr std::array zones{"America/Los_Angeles", "Europe/Berlin", "Asia/Kolkata", "America/Argentina/Buenos_Aires"}; constexpr std::array timers{-62135553600, -12609691200, -5016340800, -5000011200, -2732356800, -2209032000, -2208988800, -2208945600, -631108800}; +#endif +#ifdef _WIN32 + DaylightBiasFixture bias; +#endif for (const char* zone : zones) { Require(setenv_nid_postfix("TZ", zone, 1) == 0, "Could not set the region time zone"); tzset_nid_postfix(); +#ifdef _WIN32 + bias.Apply(); +#endif for (const std::int64_t timer : timers) { GuestTm local{}; Require(localtime_s_nid_postfix(&timer, &local) == &local, "Local conversion in a region zone failed"); @@ -327,7 +384,6 @@ static void CheckRegionZones() { Require(setenv_nid_postfix("TZ", "UTC-2", 1) == 0, "Could not restore the guest time zone"); tzset_nid_postfix(); } -#endif static void CheckGuestZone() { const std::int64_t moment = 172800; @@ -512,8 +568,9 @@ int main() { CheckFreeBsdConversions(); CheckLocalRange(); -#ifndef _WIN32 CheckRegionZones(); +#ifdef _WIN32 + CheckPosixFormZoneOffsets(); #endif CheckLiteralConversions(); diff --git a/core/libs/tests/GuestUdp.cpp b/core/libs/tests/GuestUdp.cpp index 7efa4731f4..a4e6abd15c 100644 --- a/core/libs/tests/GuestUdp.cpp +++ b/core/libs/tests/GuestUdp.cpp @@ -254,6 +254,9 @@ int main() { source.fill(0); unsigned char control[8]{}; Msghdr incoming{source.data(), static_cast(source.size()), pieces, 2, control, sizeof(control), -1}; + Require(recvmsg_nid_postfix(receiver, &incoming, 2) == 14 && incoming.flags == 2); + incoming.nameLength = static_cast(source.size()); + incoming.controlLength = sizeof(control); Require(recvmsg_nid_postfix(receiver, &incoming, 0) == 14); Require(std::memcmp(first, "scat", 4) == 0 && std::memcmp(second, "ter gather", 10) == 0); Require(incoming.flags == 0 && incoming.controlLength == 0 && incoming.nameLength == 16 && source[1] == 2 && source[4] == 127); @@ -262,9 +265,15 @@ int main() { char shortBuffer[5]{}; Iovec single{shortBuffer, sizeof(shortBuffer)}; Msghdr truncated{nullptr, 0, &single, 1, nullptr, 0, 0}; - Require(recvmsg_nid_postfix(receiver, &truncated, 0) == 5); - Require(std::memcmp(shortBuffer, "scatt", 5) == 0 && truncated.flags == 0x10); + Require(recvmsg_nid_postfix(receiver, &truncated, 2) == 5 && truncated.flags == 0x12); + Require(recvmsg_nid_postfix(receiver, &truncated, 0x80) == 5); + Require(std::memcmp(shortBuffer, "scatt", 5) == 0 && truncated.flags == 0x90); Require(recvmsg_nid_postfix(receiver, &truncated, 0) == -1 && *__error_nid_postfix() == 35); + Iovec none{shortBuffer, 0}; + Msghdr empty{nullptr, 0, &none, 1, nullptr, 0, -1}; + Require(fcntl_nid_postfix(receiver, 4, 2) == 0); + Require(recvmsg_nid_postfix(receiver, &empty, 0) == 0 && empty.flags == 0); + Require(fcntl_nid_postfix(receiver, 4, 6) == 0); Msghdr oversized{nullptr, 0, parts, 1025, nullptr, 0, 0}; Require(sendmsg_nid_postfix(sender, &oversized, 0) == -1 && *__error_nid_postfix() == 40); Require(recvmsg_nid_postfix(receiver, nullptr, 0) == -1 && *__error_nid_postfix() == 14); diff --git a/core/libs/tests/GuestUnderscoreEntries.cpp b/core/libs/tests/GuestUnderscoreEntries.cpp index edb44de05b..a7e94b5e01 100644 --- a/core/libs/tests/GuestUnderscoreEntries.cpp +++ b/core/libs/tests/GuestUnderscoreEntries.cpp @@ -77,7 +77,7 @@ constexpr int Stream = 1; constexpr int Datagram = 2; constexpr int TcpLevel = 6; constexpr int TcpNoDelay = 1; -constexpr int TcpMaximumSegment = 2; +constexpr int UnknownTcpOption = 0x7777; constexpr int OutOfBand = 0x1; constexpr int Truncated = 0x10; constexpr int Interrupted = 4; @@ -134,7 +134,7 @@ static void StreamSockets() { Require(_setsockopt_nid_postfix(client, TcpLevel, TcpNoDelay, &enabled, sizeof(enabled)) == 0); valueSize = sizeof(value); Require(_getsockopt_nid_postfix(client, TcpLevel, TcpNoDelay, &value, &valueSize) == 0 && value == 4 && valueSize == sizeof(value)); - Require(Fails(_setsockopt_nid_postfix(client, TcpLevel, TcpMaximumSegment, &enabled, sizeof(enabled)), NoProtocolOption)); + Require(Fails(_setsockopt_nid_postfix(client, TcpLevel, UnknownTcpOption, &enabled, sizeof(enabled)), NoProtocolOption)); const char text[] = "underscore stream"; Require(_sendto_nid_postfix(client, text, sizeof(text), 0, nullptr, 0) == sizeof(text)); diff --git a/core/libs/tests/GuestUnwindModuleInfoFailureLinux.cpp b/core/libs/tests/GuestUnwindModuleInfoFailureLinux.cpp index 222fa17660..f164ab2bc5 100644 --- a/core/libs/tests/GuestUnwindModuleInfoFailureLinux.cpp +++ b/core/libs/tests/GuestUnwindModuleInfoFailureLinux.cpp @@ -16,7 +16,7 @@ static void Require(bool value, const char* message) { extern "C" int APS5_VABI sceKernelGetModuleInfoFromAddr(std::uint64_t address, int flags, ModuleInfoEx* info) { Require(address == expectedAddress, "module lookup received the wrong address"); - Require(flags == 2, "module lookup received the wrong flags"); + Require(flags == 1, "module lookup received the wrong flags"); Require(info != nullptr && info->st_size == sizeof(ModuleInfoEx), "module lookup received the wrong structure size"); ++calls; return SCE_KERNEL_ERROR_ESRCH; diff --git a/core/libs/tests/GuestUpdateEnv.cpp b/core/libs/tests/GuestUpdateEnv.cpp new file mode 100644 index 0000000000..2b25a307da --- /dev/null +++ b/core/libs/tests/GuestUpdateEnv.cpp @@ -0,0 +1,274 @@ +#include "prx/libc/include/general/VabiMacros.hpp" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#ifdef _WIN32 +#include +#else +#include +#include +#include +#endif + +struct Environment { + std::array x87; + std::uint32_t mxcsr; +}; +static_assert(sizeof(Environment)==32 && offsetof(Environment,mxcsr)==28); +using UpdateEnvironment=int (APS5_VABI*)(const Environment*); +extern "C" int* APS5_VABI __error_nid_postfix(); +extern "C" int APS5_VABI fegetenv_nid_postfix(Environment*); +UpdateEnvironment updateEnvironment; + +void Require(bool condition,const char* message) { + if(!condition) { std::fprintf(stderr,"feupdateenv: %s\n",message); std::exit(1); } +} +Environment Read() { + Environment result; + asm volatile("fnstenv %0\n\tfldcw %0" : "=m"(result.x87)); + result.mxcsr=_mm_getcsr(); + return result; +} +void Write(const Environment& value) { + asm volatile("fldenv %0" : : "m"(value.x87) : "st","st(1)","st(2)","st(3)","st(4)","st(5)","st(6)","st(7)"); + _mm_setcsr(value.mxcsr); +} +bool Same(const Environment& a,const Environment& b){return a.x87==b.x87 && a.mxcsr==b.mxcsr;} +void Matrix() { + const auto original=Read(); + auto clean=original; + clean.x87[0]|=0x3f; + clean.x87[1]&=~0xffu; + clean.mxcsr=(clean.mxcsr&~0x3fu)|(0x3f<<7); + unsigned cases=0; + for(unsigned x87=0;x87<64;++x87) + for(unsigned sse=0;sse<64;++sse) + for(unsigned pattern=0;pattern<4;++pattern) + for(unsigned rounding=0;rounding<16;++rounding) { + auto saved=clean; + constexpr unsigned precisions[]{0u,0x200u,0x300u,0u}; + saved.x87[0]=(saved.x87[0]&~0x300u)|precisions[pattern]; + saved.x87[0]=(saved.x87[0]&~0xc00u)|((rounding&3)<<10); + saved.mxcsr=(saved.mxcsr&~0x6000u)|((rounding>>2)<<13); + const unsigned savedX87=pattern==0?0:pattern==1?0x15:pattern==2?0x2a:0x3f; + const unsigned savedSse=pattern==0?0x3f:pattern==1?0x2a:pattern==2?0x15:0; + saved.x87[1]|=savedX87; + saved.mxcsr|=savedSse; + Write(saved); + saved=Read(); + auto pending=clean; + pending.x87[1]|=x87; + pending.mxcsr|=sse; + Write(pending); + const auto input=saved; + *__error_nid_postfix()=123; + const int result=updateEnvironment(&saved); + const auto after=Read(); + const int error=*__error_nid_postfix(); + Write(clean); + auto expected=input; + expected.x87[1]|=x87|sse; + expected.mxcsr|=x87|sse; + Require(result==0 && error==123,"return/errno"); + Require(after.x87==expected.x87 && after.mxcsr==expected.mxcsr,"restore saved controls and raise old union in both units"); + Require(std::memcmp(&input,&saved,sizeof(input))==0,"input unchanged"); + ++cases; + } + Write(original); + std::printf("feupdateenv: %u flag/rounding combinations passed\n",cases); +} + +void TagsAndPointers() { + const auto entry=Read(); + auto clean=entry; + clean.x87[0]|=0x3f; clean.x87[1]&=~0xffu; + clean.mxcsr=(clean.mxcsr&~0x3fu)|(0x3f<<7); + Write(clean); + const auto original=Read(); + asm volatile("fninit\n\tfld1\n\tfldpi" : : : "st","st(1)"); + const auto desired=Read(); + asm volatile("fninit" : : : "st","st(1)","st(2)","st(3)","st(4)","st(5)","st(6)","st(7)"); + const int result=updateEnvironment(&desired); + const auto after=Read(); + Write(original); + Require(result==0 && Same(after,desired),"nonempty x87 tag/TOP and instruction pointer restored"); + Environment saved; + Require(fegetenv_nid_postfix(&saved)==0,"save using existing guest fegetenv"); + asm volatile("fninit" : : : "st","st(1)","st(2)","st(3)","st(4)","st(5)","st(6)","st(7)"); + Require(updateEnvironment(&saved)==0,"restore guest fegetenv output"); + const auto restored=Read(); + Write(original); + Require(Same(saved,restored),"guest fegetenv round trip"); + Write(entry); +} + +void BoundsAndErrors() { + const auto entry=Read(); + auto clean=entry; + clean.x87[0]|=0x3f; clean.x87[1]&=~0xffu; + clean.mxcsr=(clean.mxcsr&~0x3fu)|(0x3f<<7); + Write(clean); + const auto original=Read(); +#ifdef _WIN32 + SYSTEM_INFO info{}; + GetSystemInfo(&info); + const auto page=static_cast(info.dwPageSize); + auto* memory=static_cast(VirtualAlloc(nullptr,2*page,MEM_RESERVE|MEM_COMMIT,PAGE_READWRITE)); + Require(memory!=nullptr,"allocate guard pages"); + DWORD previous; + Require(VirtualProtect(memory+page,page,PAGE_NOACCESS,&previous)!=0,"protect trailing page"); +#else + const auto page=static_cast(sysconf(_SC_PAGESIZE)); + auto* memory=static_cast(mmap(nullptr,2*page,PROT_READ|PROT_WRITE,MAP_PRIVATE|MAP_ANONYMOUS,-1,0)); + Require(memory!=MAP_FAILED,"allocate guard pages"); + Require(mprotect(memory+page,page,PROT_NONE)==0,"protect trailing page"); +#endif + auto* end=reinterpret_cast(memory+page-sizeof(Environment)); + std::memcpy(end,&original,sizeof(original)); +#ifdef _WIN32 + Require(VirtualProtect(memory,page,PAGE_READONLY,&previous)!=0,"input read-only"); +#else + Require(mprotect(memory,page,PROT_READ)==0,"input read-only"); +#endif + const int result=updateEnvironment(end); + const auto after=Read(); + Require(result==0 && Same(after,original),"exact32-byte read at guard-page boundary"); +#ifdef _WIN32 + Require(VirtualFree(memory,0,MEM_RELEASE)!=0,"release guard pages"); +#else + Require(munmap(memory,2*page)==0,"release guard pages"); +#endif + bool rejected=false; + *__error_nid_postfix()=123; + try { updateEnvironment(nullptr); } catch(const std::invalid_argument&) { rejected=true; } + Require(rejected && Same(Read(),original) && *__error_nid_postfix()==123,"null rejected without state or errno change"); + alignas(16) std::array extended; + asm volatile("fxsave %0" : "=m"(extended)); + const auto supported=extended[7] ? extended[7] : 0xffbfu; + for(unsigned bit=0;bit<32;++bit) { + const auto reserved=1u< state; + asm volatile("fxsave %0" : "=m"(state)); + const auto supported=state[7]?state[7]:0xffbfu; + for(unsigned mode:{0u,0x40u,0x8000u,0x8040u}) { + if(mode&~supported) continue; + auto desired=clean; desired.mxcsr=(desired.mxcsr&~0x8040u)|mode; + Require(updateEnvironment(&desired)==0,"supported DAZ/FTZ return"); + const auto actual=Read(); Write(clean); + Require(Same(actual,desired),"supported DAZ/FTZ restored"); + } + std::barrier ready(2); + auto worker=[&](unsigned rounding,unsigned flags) { + const auto entry=Read(); + auto desired=clean; + desired.x87[0]=(desired.x87[0]&~0xc00u)|(rounding<<10); + desired.mxcsr=(desired.mxcsr&~0x6000u)|(rounding<<13); + auto pending=clean; pending.mxcsr|=flags; + ready.arrive_and_wait(); + Write(pending); + *__error_nid_postfix()=static_cast(flags); + const int result=updateEnvironment(&desired); + const int error=*__error_nid_postfix(); + const auto after=Read(); + ready.arrive_and_wait(); + Write(entry); + desired.x87[1]|=flags; desired.mxcsr|=flags; + Require(result==0 && error==static_cast(flags),"thread return/errno"); + Require(Same(after,desired),"thread environment and flag union"); + }; + std::thread a(worker,1,4),b(worker,2,8);a.join();b.join(); + Write(original); +} +volatile std::sig_atomic_t entered=0; +#ifdef _WIN32 +unsigned expectedFlag; +DWORD expectedException; +LONG CALLBACK Trap(EXCEPTION_POINTERS* exception) { + const auto& state=exception->ContextRecord->FltSave; + const auto active=state.StatusWord&~state.ControlWord&0x3f; + const auto code=exception->ExceptionRecord->ExceptionCode; + const bool denormal=expectedFlag==2 && code==EXCEPTION_FLT_INVALID_OPERATION; + if(entered && active==expectedFlag && (code==expectedException || denormal)) std::_Exit(0); + return EXCEPTION_CONTINUE_SEARCH; +} +#else +void Trap(int){std::_Exit(entered?0:1);} +#endif +void Pending(unsigned bit,unsigned source) { + Require(bit<6 && source<3,"trap arguments"); +#ifdef _WIN32 + constexpr DWORD codes[]{EXCEPTION_FLT_INVALID_OPERATION,EXCEPTION_FLT_DENORMAL_OPERAND,EXCEPTION_FLT_DIVIDE_BY_ZERO,EXCEPTION_FLT_OVERFLOW,EXCEPTION_FLT_UNDERFLOW,EXCEPTION_FLT_INEXACT_RESULT}; + expectedFlag=1u<(GetProcAddress(library,"feupdateenv_nid_postfix")); +#else + const auto library=dlopen(argv[1],RTLD_NOW); + Require(library!=nullptr,"load libc"); + updateEnvironment=reinterpret_cast(dlsym(library,"feupdateenv_nid_postfix")); +#endif + Require(updateEnvironment!=nullptr,"export is missing"); + if(argc==4) Pending(static_cast(std::atoi(argv[2])),static_cast(std::atoi(argv[3]))); + Matrix(); TagsAndPointers(); BoundsAndErrors(); MasksAndThreads(); +#ifdef _WIN32 + Require(FreeLibrary(library)!=0,"release library lookup handle"); +#else + Require(dlclose(library)==0,"release library lookup handle"); +#endif +} diff --git a/core/libs/tests/GuestUsbStorage.cpp b/core/libs/tests/GuestUsbStorage.cpp new file mode 100644 index 0000000000..60254a0076 --- /dev/null +++ b/core/libs/tests/GuestUsbStorage.cpp @@ -0,0 +1,249 @@ +#include "prx/libSceUsbStorage/UsbStorage.hpp" + +#include +#include +#include +#include +#include +#include + +extern "C" { +int APS5_VABI sceUsbStorageGetMountPointOfShellCore(unsigned int device, char* mountPoint); +int APS5_VABI sceUsbStorageClearFakeWriteProtection(); +int APS5_VABI sceUsbStorageSetFakeMapLockForDebug(); +int APS5_VABI sceUsbStorageSetFakeWriteProtection(); +} + +namespace { + +int g_attr = 0; + +void APS5_VABI CallbackStub(const UsbStorageEvent*, void*) {} + +UsbStorageCallback Callback() { + return &CallbackStub; +} + +void Require(bool condition, const char* message) { + if (!condition) { + std::fprintf(stderr, "USBSTORAGE: %s\n", message); + std::abort(); + } +} + +void TestInitializeAndTerminate() { + Require(sceUsbStorageInit(nullptr) == 0, "initialize with a null attribute"); + Require(sceUsbStorageInit(&g_attr) == 0, "initialize with an attribute"); + Require(sceUsbStorageTerm() == 0, "terminate"); + Require(sceUsbStorageInit(nullptr) == 0, "initialize after terminate"); + Require(sceUsbStorageTerm() == 0, "terminate again"); +} + +void TestRegisterCallbacks() { + bool threw = false; + try { + sceUsbStorageRegisterCallback(1, Callback(), nullptr); + } catch (const std::runtime_error& error) { + threw = std::string_view(error.what()) == "sceUsbStorageRegisterCallback not implemented"; + } + Require(threw, "register callback of event type 1 did not throw its marker"); + threw = false; + try { + sceUsbStorageRegisterCallback(2, Callback(), nullptr); + } catch (const std::runtime_error& error) { + threw = std::string_view(error.what()) == "sceUsbStorageRegisterCallback not implemented"; + } + Require(threw, "register callback of event type 2 did not throw its marker"); + Require(sceUsbStorageRegisterCallback(1, nullptr, nullptr) == UsbStorage::InvalidArgument, "register a null callback"); + Require(sceUsbStorageRegisterCallback(0, Callback(), nullptr) == UsbStorage::InvalidArgument, "register callback of event type 0"); + Require(sceUsbStorageRegisterCallback(3, Callback(), nullptr) == UsbStorage::InvalidArgument, "register callback of event type 3"); + Require(sceUsbStorageRegisterCallback(-1, Callback(), nullptr) == UsbStorage::InvalidArgument, "register callback of a negative event type"); + threw = false; + try { + sceUsbStorageUnregisterCallback(1, Callback()); + } catch (const std::runtime_error& error) { + threw = std::string_view(error.what()) == "sceUsbStorageUnregisterCallback not implemented"; + } + Require(threw, "unregister callback of event type 1 did not throw its marker"); + threw = false; + try { + sceUsbStorageUnregisterCallback(2, Callback()); + } catch (const std::runtime_error& error) { + threw = std::string_view(error.what()) == "sceUsbStorageUnregisterCallback not implemented"; + } + Require(threw, "unregister callback of event type 2 did not throw its marker"); + Require(sceUsbStorageUnregisterCallback(1, nullptr) == UsbStorage::InvalidArgument, "unregister a null callback"); + Require(sceUsbStorageUnregisterCallback(0, Callback()) == UsbStorage::InvalidArgument, "unregister callback of event type 0"); +} + +void TestMapCallbacks() { + bool threw = false; + try { + sceUsbStorageRegisterCallbackForMapAvailable(0, "/data", Callback(), nullptr); + } catch (const std::runtime_error& error) { + threw = std::string_view(error.what()) == "sceUsbStorageRegisterCallbackForMapAvailable not implemented"; + } + Require(threw, "register map-available callback did not throw its marker"); + Require(sceUsbStorageRegisterCallbackForMapAvailable(0, nullptr, Callback(), nullptr) == UsbStorage::InvalidArgument, "register map-available callback with a null directory"); + Require(sceUsbStorageRegisterCallbackForMapAvailable(0, "/data", nullptr, nullptr) == UsbStorage::InvalidArgument, "register a null map-available callback"); + threw = false; + try { + sceUsbStorageUnregisterCallbackForMapAvailable(0, "/data", Callback()); + } catch (const std::runtime_error& error) { + threw = std::string_view(error.what()) == "sceUsbStorageUnregisterCallbackForMapAvailable not implemented"; + } + Require(threw, "unregister map-available callback did not throw its marker"); + Require(sceUsbStorageUnregisterCallbackForMapAvailable(0, nullptr, Callback()) == UsbStorage::InvalidArgument, "unregister map-available callback with a null directory"); + Require(sceUsbStorageUnregisterCallbackForMapAvailable(0, "/data", nullptr) == UsbStorage::InvalidArgument, "unregister a null map-available callback"); + threw = false; + try { + sceUsbStorageRegisterCallbackForMapUnavailable(0, "/data", Callback(), nullptr); + } catch (const std::runtime_error& error) { + threw = std::string_view(error.what()) == "sceUsbStorageRegisterCallbackForMapUnavailable not implemented"; + } + Require(threw, "register map-unavailable callback did not throw its marker"); + Require(sceUsbStorageRegisterCallbackForMapUnavailable(0, nullptr, Callback(), nullptr) == UsbStorage::InvalidArgument, "register map-unavailable callback with a null directory"); + Require(sceUsbStorageRegisterCallbackForMapUnavailable(0, "/data", nullptr, nullptr) == UsbStorage::InvalidArgument, "register a null map-unavailable callback"); + threw = false; + try { + sceUsbStorageUnregisterCallbackForMapUnavailable(0, "/data", Callback()); + } catch (const std::runtime_error& error) { + threw = std::string_view(error.what()) == "sceUsbStorageUnregisterCallbackForMapUnavailable not implemented"; + } + Require(threw, "unregister map-unavailable callback did not throw its marker"); + Require(sceUsbStorageUnregisterCallbackForMapUnavailable(0, nullptr, Callback()) == UsbStorage::InvalidArgument, "unregister map-unavailable callback with a null directory"); + Require(sceUsbStorageUnregisterCallbackForMapUnavailable(0, "/data", nullptr) == UsbStorage::InvalidArgument, "unregister a null map-unavailable callback"); +} + +void TestNoDevice() { + std::uint32_t devices[4]{0x5a5a5a5a, 0x5a5a5a5a, 0x5a5a5a5a, 0x5a5a5a5a}; + std::int32_t count = -1; + Require(sceUsbStorageGetDeviceList(devices, &count) == 0, "get the device list"); + Require(count == 0, "no usb storage device is attached"); + Require(devices[0] == 0x5a5a5a5a && devices[3] == 0x5a5a5a5a, "the device list was written"); + Require(sceUsbStorageGetDeviceList(nullptr, &count) == UsbStorage::InvalidArgument, "get the device list without a device array"); + Require(sceUsbStorageGetDeviceList(devices, nullptr) == UsbStorage::InvalidArgument, "get the device list without a count"); + + std::uint8_t info[16]; + std::memset(info, 0x5a, sizeof(info)); + auto* const infoRecord = reinterpret_cast(info); + Require(sceUsbStorageGetDeviceInfo(0, infoRecord) == UsbStorage::DeviceNotFound, "get device info of device 0"); + Require(sceUsbStorageGetDeviceInfo(1, infoRecord) == UsbStorage::DeviceNotFound, "get device info of device 1"); + Require(info[0] == 0x5a && info[15] == 0x5a, "device info was written"); + Require(sceUsbStorageGetDeviceInfo(0, nullptr) == UsbStorage::InvalidArgument, "get device info without an info buffer"); + + std::uint8_t exists = 1; + Require(sceUsbStorageIsExist(0, "/mnt/usb0", &exists) == true && exists == 0, "device 0 does not exist"); + exists = 1; + Require(sceUsbStorageIsExist(1, nullptr, &exists) == true && exists == 0, "device 1 does not exist"); + exists = 1; + Require(sceUsbStorageIsExist(0xffffffffu, "/mnt/usb0", &exists) == true && exists == 0, "no device id exists"); +} + +void TestMapRequests() { + char mountPoint[16]; + std::uint64_t mountId = 0x5a5a5a5a5a5a5a5aull; + std::int32_t status = 0x5a5a5a5a; + std::memset(mountPoint, 0x5a, sizeof(mountPoint)); + Require(sceUsbStorageRequestMap(0, "/data", 0, 0, mountPoint, &mountId) == UsbStorage::DeviceNotFound, "request a map of device 0"); + Require(sceUsbStorageRequestMap(1, nullptr, 0, 0, nullptr, nullptr) == UsbStorage::DeviceNotFound, "request a map of device 1"); + Require(mountId == 0x5a5a5a5a5a5a5a5aull && mountPoint[0] == static_cast(0x5a), "the map request wrote its outputs"); + std::memset(mountPoint, 0x5a, sizeof(mountPoint)); + Require(sceUsbStorageRequestMapWSB(0, "/data", 0, 0, mountPoint, &status) == UsbStorage::DeviceNotFound, "request a WSB map of device 0"); + Require(sceUsbStorageRequestMapWSB(0, nullptr, 0, 0, nullptr, nullptr) == UsbStorage::DeviceNotFound, "request a WSB map without a subpath"); + Require(mountPoint[0] == static_cast(0x5a) && status == 0x5a5a5a5a, "the WSB map request wrote its outputs"); + Require(sceUsbStorageRequestUnmap(0, nullptr) == UsbStorage::InvalidArgument, "unmap a null directory"); + bool threw = false; + try { + sceUsbStorageRequestUnmap(0, "/mnt/usb0"); + } catch (const std::runtime_error& error) { + threw = std::string_view(error.what()) == "sceUsbStorageRequestUnmap not implemented"; + } + Require(threw, "unmap a directory did not throw its marker"); + threw = false; + try { + sceUsbStorageRequestUnmap(7, "/mnt/usb7"); + } catch (const std::runtime_error& error) { + threw = std::string_view(error.what()) == "sceUsbStorageRequestUnmap not implemented"; + } + Require(threw, "unmap a directory of another device did not throw its marker"); +} + +void TestGetdents() { + std::int32_t fd = -1; + std::uint64_t bufferSize = 0x5a5a5a5a5a5a5a5aull; + Require(sceUsbStorageGetdentsOpen(0, &fd, &bufferSize) == UsbStorage::DeviceNotFound, "open a getdents scan of device 0"); + Require(sceUsbStorageGetdentsOpen(3, &fd, &bufferSize) == UsbStorage::DeviceNotFound, "open a getdents scan of device 3"); + Require(fd == -1 && bufferSize == 0x5a5a5a5a5a5a5a5aull, "the getdents open wrote its outputs"); + Require(sceUsbStorageGetdentsOpen(0, nullptr, &bufferSize) == UsbStorage::InvalidArgument, "open a getdents scan without a descriptor"); + Require(sceUsbStorageGetdentsOpen(0, &fd, nullptr) == UsbStorage::InvalidArgument, "open a getdents scan without a buffer size"); + + std::uint8_t buffer[64]; + std::uint64_t bytesRead = 0x5a5a5a5a5a5a5a5aull; + std::memset(buffer, 0x5a, sizeof(buffer)); + Require(sceUsbStorageGetdentsRead(1, buffer, sizeof(buffer), &bytesRead) == UsbStorage::NotSupported, "read a getdents scan that was never opened"); + Require(bytesRead == 0, "the getdents read did not answer zero bytes"); + Require(buffer[0] == 0x5a, "the getdents read wrote the buffer"); + Require(sceUsbStorageGetdentsRead(1, nullptr, sizeof(buffer), &bytesRead) == UsbStorage::InvalidArgument, "read a getdents scan without a buffer"); + Require(sceUsbStorageGetdentsRead(1, buffer, sizeof(buffer), nullptr) == UsbStorage::InvalidArgument, "read a getdents scan without a byte count"); + + bool threw = false; + try { + sceUsbStorageGetdentsClose(1); + } catch (const std::runtime_error& error) { + threw = std::string_view(error.what()) == "sceUsbStorageGetdentsClose not implemented"; + } + Require(threw, "close a getdents scan that was never opened did not throw its marker"); + threw = false; + try { + sceUsbStorageGetdentsClose(0); + } catch (const std::runtime_error& error) { + threw = std::string_view(error.what()) == "sceUsbStorageGetdentsClose not implemented"; + } + Require(threw, "close descriptor 0 did not throw its marker"); +} + +void TestThrowingExports() { + char mountPoint[8]{}; + bool threw = false; + try { + sceUsbStorageGetMountPointOfShellCore(0, mountPoint); + } catch (const std::runtime_error& error) { + threw = std::string_view(error.what()) == "sceUsbStorageGetMountPointOfShellCore not implemented"; + } + Require(threw, "get the mount point of shell core did not throw its marker"); + threw = false; + try { + sceUsbStorageClearFakeWriteProtection(); + } catch (const std::runtime_error& error) { + threw = std::string_view(error.what()) == "sceUsbStorageClearFakeWriteProtection not implemented"; + } + Require(threw, "clear fake write protection did not throw its marker"); + threw = false; + try { + sceUsbStorageSetFakeMapLockForDebug(); + } catch (const std::runtime_error& error) { + threw = std::string_view(error.what()) == "sceUsbStorageSetFakeMapLockForDebug not implemented"; + } + Require(threw, "set fake map lock for debug did not throw its marker"); + threw = false; + try { + sceUsbStorageSetFakeWriteProtection(); + } catch (const std::runtime_error& error) { + threw = std::string_view(error.what()) == "sceUsbStorageSetFakeWriteProtection not implemented"; + } + Require(threw, "set fake write protection did not throw its marker"); +} + +} + +int main() { + TestInitializeAndTerminate(); + TestRegisterCallbacks(); + TestMapCallbacks(); + TestNoDevice(); + TestMapRequests(); + TestGetdents(); + TestThrowingExports(); + return 0; +} diff --git a/core/libs/tests/GuestUsbd.cpp b/core/libs/tests/GuestUsbd.cpp index 358e369eea..1cbfa48e1e 100644 --- a/core/libs/tests/GuestUsbd.cpp +++ b/core/libs/tests/GuestUsbd.cpp @@ -43,6 +43,10 @@ std::int32_t APS5_VABI sceUsbdKernelDriverActive(void* deviceHandle, std::int32_ std::int32_t APS5_VABI sceUsbdAttachKernelDriver(void* deviceHandle, std::int32_t interfaceNumber); std::int32_t APS5_VABI sceUsbdControlTransfer(void* deviceHandle, std::uint8_t requestType, std::uint8_t request, std::uint16_t value, std::uint16_t index, std::uint8_t* data, std::uint16_t length, std::uint32_t timeout); std::int32_t APS5_VABI sceUsbdGetStringDescriptor(void* deviceHandle, std::uint8_t descriptorIndex, std::uint16_t languageId, std::uint8_t* data, std::int32_t length); +std::int32_t APS5_VABI sceUsbdGetStringDescriptorAscii(void* deviceHandle, std::uint8_t descriptorIndex, std::uint8_t* data, std::int32_t length); +std::int32_t APS5_VABI sceUsbdGetDescriptor(void* deviceHandle, std::uint8_t descriptorType, std::uint8_t descriptorIndex, std::uint8_t* data, std::int32_t length); +std::int32_t APS5_VABI sceUsbdGetConfiguration(void* deviceHandle, std::int32_t* configuration); +std::int32_t APS5_VABI sceUsbdGetConfigDescriptorByValue(void* device, std::uint8_t configurationValue, void** config); std::int32_t APS5_VABI sceUsbdSubmitTransfer(void* transfer); void* APS5_VABI sceUsbdAllocTransfer(std::int32_t isoPackets); void APS5_VABI sceUsbdFreeTransfer(void* transfer); @@ -126,13 +130,20 @@ void Device() { const auto* raw = static_cast(config); Require(raw[1] == 2 && raw[4] > 0, "active config descriptor is not a configuration with interfaces"); const std::uint8_t interfaces = raw[4]; + const std::uint8_t activeValue = raw[5]; sceUsbdFreeConfigDescriptor(config); Require(Throws([&] { sceUsbdFreeConfigDescriptor(config); }), "a config descriptor was freed twice"); Require(sceUsbdGetConfigDescriptor(device, 0, &config) == 0, "config descriptor 0 failed"); sceUsbdFreeConfigDescriptor(config); + Require(sceUsbdGetConfigDescriptorByValue(device, activeValue, &config) == 0 && config != nullptr, "config descriptor by value failed"); + Require(static_cast(config)[5] == activeValue, "config descriptor by value has another configuration value"); + sceUsbdFreeConfigDescriptor(config); + Require(sceUsbdGetConfigDescriptorByValue(device, 0, &config) == static_cast(0x80240005) && config == nullptr, "configuration value 0 was found"); void* handle = nullptr; Require(sceUsbdOpen(device, &handle) == 0 && handle != nullptr, "opening the device failed"); Require(sceUsbdCheckConnected(handle) == 0, "an open device was reported disconnected"); + std::int32_t configuration = -1; + Require(sceUsbdGetConfiguration(handle, &configuration) == 0 && configuration == activeValue, "configuration differs from the active config descriptor"); std::uint8_t text[256] = {}; if (descriptor[15] != 0) { const std::int32_t length = sceUsbdGetStringDescriptor(handle, descriptor[15], 0x409, text, sizeof(text)); @@ -140,10 +151,31 @@ void Device() { std::string product_name; for (std::int32_t index = 2; index + 1 < length; index += 2) product_name += static_cast(text[index]); std::printf("product \"%s\"\n", product_name.c_str()); + char ascii[256] = {}; + const std::int32_t asciiLength = sceUsbdGetStringDescriptorAscii(handle, descriptor[15], reinterpret_cast(ascii), sizeof(ascii)); + Require(asciiLength >= 0 && std::strlen(ascii) == static_cast(asciiLength), "ASCII product string length differs from its terminated length"); + std::printf("ASCII product \"%s\"\n", ascii); + std::uint8_t languages[4] = {}; + Require(sceUsbdGetStringDescriptor(handle, 0, 0, languages, sizeof(languages)) == 4, "language ID string descriptor failed"); + std::uint8_t first[256] = {}; + const std::int32_t firstLength = sceUsbdGetStringDescriptor(handle, descriptor[15], static_cast(languages[2] | languages[3] << 8), first, sizeof(first)); + Require(firstLength >= 2 && first[1] == 3, "product string descriptor in the first language failed"); + const std::int32_t end = first[0] < firstLength ? first[0] : firstLength; + std::string printable; + for (std::int32_t index = 2; index + 1 < end; index += 2) { + if (first[index + 1] == 0 && first[index] >= 0x20 && first[index] <= 0x7E && first[index] != '<' && first[index] != '>') printable += static_cast(first[index]); + } + Require(printable == ascii, "ASCII product string differs from the printable characters of the product string"); + char shortAscii[4] = {'x', 'x', 'x', 'x'}; + const std::int32_t shortLength = sceUsbdGetStringDescriptorAscii(handle, descriptor[15], reinterpret_cast(shortAscii), sizeof(shortAscii)); + Require(shortLength >= 0 && shortLength <= 3 && shortAscii[shortLength] == 0 && std::strncmp(shortAscii, ascii, static_cast(shortLength)) == 0, "a short ASCII buffer was not filled with a terminated prefix"); } std::uint8_t again[18] = {}; Require(sceUsbdControlTransfer(handle, 0x80, 6, 0x0100, 0, again, sizeof(again), 1000) == 18, "synchronous device descriptor request failed"); Require(std::memcmp(again, descriptor, sizeof(again)) == 0, "device descriptor read over the wire differs from the cached one"); + std::uint8_t wire[18] = {}; + Require(sceUsbdGetDescriptor(handle, 1, 0, wire, sizeof(wire)) == 18, "device descriptor request through sceUsbdGetDescriptor failed"); + Require(std::memcmp(wire, descriptor, sizeof(wire)) == 0, "device descriptor read through sceUsbdGetDescriptor differs from the cached one"); for (std::int32_t interfaceNumber = 0; interfaceNumber < interfaces; ++interfaceNumber) { Require(sceUsbdClaimInterface(handle, interfaceNumber) == 0, "claiming an interface failed"); Require(sceUsbdReleaseInterface(handle, interfaceNumber) == 0, "releasing an interface failed"); @@ -274,6 +306,20 @@ int main(int argc, char** argv) { Require(Throws([&] { sceUsbdControlTransfer(&foreign, 0x80, 6, 0x100, 0, descriptor, 18, 1000); }), "control transfer on a handle that was never opened did not throw"); Require(sceUsbdGetStringDescriptor(nullptr, 1, 0x409, descriptor, 18) == invalidArgument, "null handle string descriptor accepted"); Require(sceUsbdGetStringDescriptor(&foreign, 1, 0x409, descriptor, 0) == invalidArgument, "empty string descriptor buffer accepted"); + descriptor[0] = 0x55; + Require(sceUsbdGetStringDescriptorAscii(nullptr, 1, descriptor, 18) == invalidArgument, "null handle ASCII string descriptor accepted"); + Require(sceUsbdGetStringDescriptorAscii(&foreign, 1, nullptr, 18) == invalidArgument && sceUsbdGetStringDescriptorAscii(&foreign, 1, descriptor, 0) == invalidArgument, "missing ASCII string buffer accepted"); + Require(descriptor[0] == 0x55, "a rejected ASCII string request wrote the buffer"); + Require(Throws([&] { sceUsbdGetStringDescriptorAscii(&foreign, 1, descriptor, 18); }), "ASCII string request on a handle that was never opened did not throw"); + Require(sceUsbdGetDescriptor(nullptr, 1, 0, descriptor, 18) == invalidArgument, "null handle descriptor request accepted"); + Require(sceUsbdGetDescriptor(&foreign, 1, 0, nullptr, 18) == invalidArgument && sceUsbdGetDescriptor(&foreign, 1, 0, descriptor, 0) == invalidArgument, "missing descriptor buffer accepted"); + Require(Throws([&] { sceUsbdGetDescriptor(&foreign, 1, 0, descriptor, 18); }), "descriptor request on a handle that was never opened did not throw"); + std::int32_t configuration = -1; + Require(sceUsbdGetConfiguration(nullptr, &configuration) == invalidArgument && sceUsbdGetConfiguration(&foreign, nullptr) == invalidArgument, "null configuration query accepted"); + Require(configuration == -1, "a rejected configuration query wrote the output"); + Require(Throws([&] { sceUsbdGetConfiguration(&foreign, &configuration); }), "configuration of a handle that was never opened did not throw"); + Require(sceUsbdGetConfigDescriptorByValue(nullptr, 1, &config) == invalidArgument && sceUsbdGetConfigDescriptorByValue(&foreign, 1, nullptr) == invalidArgument, "null config by value argument accepted"); + Require(Throws([&] { sceUsbdGetConfigDescriptorByValue(&foreign, 1, &config); }), "config by value of a device that was never listed did not throw"); Require(Throws([&] { sceUsbdClaimInterface(&foreign, 0); }), "claiming on a handle that was never opened did not throw"); auto* transfer = static_cast(sceUsbdAllocTransfer(2)); Require(transfer != nullptr, "transfer allocation failed"); diff --git a/core/libs/tests/GuestUserService.cpp b/core/libs/tests/GuestUserService.cpp index 83e78239ca..88116c03d8 100644 --- a/core/libs/tests/GuestUserService.cpp +++ b/core/libs/tests/GuestUserService.cpp @@ -2,6 +2,7 @@ #include "prx/libSceUserService/UserService.hpp" #include #include +#include #include #include #include @@ -27,6 +28,8 @@ int APS5_VABI sceUserServiceGetAccessibilityVibration(int user_id, std::int32_t* int APS5_VABI sceUserServiceGetAccessibilityZoom(); int APS5_VABI sceUserServiceGetAccessibilityZoomEnabled(int user_id, std::int32_t* value); int APS5_VABI sceUserServiceGetAccessibilityZoomFollowFocus(int user_id, std::int32_t* value); +int APS5_VABI sceUserServiceGetUserName(int user_id, char* name, std::size_t size); +int APS5_VABI sceUserServiceGetUserNumber(int user_id, std::int32_t* number); } namespace { @@ -179,4 +182,38 @@ int main() { threwZoom = true; } Require(threwZoom); + + char name[32]{}; + Require(sceUserServiceGetUserName(USER_SERVICE_INITIAL_USER_ID, nullptr, sizeof(name)) == USER_SERVICE_ERROR_INVALID_ARGUMENT); + Require(sceUserServiceGetUserName(123, name, sizeof(name)) == USER_SERVICE_ERROR_UNKNOWN_USER); + Require(sceUserServiceGetUserName(USER_SERVICE_INITIAL_USER_ID, name, 5) == USER_SERVICE_ERROR_BUFFER_TOO_SHORT); + Require(sceUserServiceGetUserName(USER_SERVICE_INITIAL_USER_ID, name, sizeof(name)) == USER_SERVICE_OK); + Require(std::strcmp(name, "Player") == 0); + Require(sceUserServiceGetUserName(USER_SERVICE_INITIAL_USER_ID + 1, name, sizeof(name)) == USER_SERVICE_OK); + Require(std::strcmp(name, "Player2") == 0); + + std::int32_t number = -1; + Require(sceUserServiceGetUserNumber(USER_SERVICE_INITIAL_USER_ID, nullptr) == USER_SERVICE_ERROR_INVALID_ARGUMENT); + Require(sceUserServiceGetUserNumber(123, &number) == USER_SERVICE_ERROR_INVALID_ARGUMENT); + Require(sceUserServiceGetUserNumber(USER_SERVICE_INITIAL_USER_ID, &number) == USER_SERVICE_OK); + Require(number == 1); + Require(sceUserServiceGetUserNumber(USER_SERVICE_INITIAL_USER_ID + 1, &number) == USER_SERVICE_OK); + Require(number == 2); + + Require(sceUserServiceGetUserColor(USER_SERVICE_INITIAL_USER_ID + 1, &color) == USER_SERVICE_OK); + Require(color == 1); + + UserServiceSetUserConnected_nid_postfix(1, true); + Require(sceUserServiceGetRegisteredUserIdList(®istered) == USER_SERVICE_OK); + Require(registered.user_id[0] == USER_SERVICE_INITIAL_USER_ID); + Require(registered.user_id[1] == USER_SERVICE_INITIAL_USER_ID + 1); + for (int i = 2; i < 16; ++i) { + Require(registered.user_id[i] == USER_SERVICE_USER_ID_INVALID); + } + UserServiceSetUserConnected_nid_postfix(1, false); + Require(sceUserServiceGetRegisteredUserIdList(®istered) == USER_SERVICE_OK); + Require(registered.user_id[0] == USER_SERVICE_INITIAL_USER_ID); + for (int i = 1; i < 16; ++i) { + Require(registered.user_id[i] == USER_SERVICE_USER_ID_INVALID); + } } diff --git a/core/libs/tests/GuestUserServiceLifecycle.cpp b/core/libs/tests/GuestUserServiceLifecycle.cpp index 550acb5353..b1346120f8 100644 --- a/core/libs/tests/GuestUserServiceLifecycle.cpp +++ b/core/libs/tests/GuestUserServiceLifecycle.cpp @@ -82,6 +82,42 @@ int main() { Require(sceUserServiceInitialize(nullptr) == USER_SERVICE_OK); RequireLoginOnce(); + + UserServiceSetUserConnected_nid_postfix(1, true); + SceUserServiceEvent event{}; + Require(sceUserServiceGetEvent(&event) == USER_SERVICE_OK); + Require(event.event_type == 0 && event.user_id == USER_SERVICE_INITIAL_USER_ID + 1); + Require(sceUserServiceGetLoginUserIdList(&list) == USER_SERVICE_OK); + Require(list.user_id[0] == USER_SERVICE_INITIAL_USER_ID); + Require(list.user_id[1] == USER_SERVICE_INITIAL_USER_ID + 1); + + UserServiceSetUserConnected_nid_postfix(1, false); + Require(sceUserServiceGetEvent(&event) == USER_SERVICE_OK); + Require(event.event_type == 1 && event.user_id == USER_SERVICE_INITIAL_USER_ID + 1); + Require(sceUserServiceGetLoginUserIdList(&list) == USER_SERVICE_OK); + Require(list.user_id[0] == USER_SERVICE_INITIAL_USER_ID); + Require(list.user_id[1] == USER_SERVICE_USER_ID_INVALID); + + for (int i = 0; i < 50; ++i) { + UserServiceSetUserConnected_nid_postfix(1, true); + UserServiceSetUserConnected_nid_postfix(1, false); + } + Require(sceUserServiceGetEvent(&event) == USER_SERVICE_ERROR_NO_EVENT); + + UserServiceSetUserConnected_nid_postfix(2, true); + UserServiceSetUserConnected_nid_postfix(2, false); + Require(sceUserServiceGetEvent(&event) == USER_SERVICE_ERROR_NO_EVENT); + + UserServiceSetUserConnected_nid_postfix(2, true); + Require(sceUserServiceGetEvent(&event) == USER_SERVICE_OK); + Require(event.event_type == 0 && event.user_id == USER_SERVICE_INITIAL_USER_ID + 2); + UserServiceSetUserConnected_nid_postfix(2, false); + UserServiceSetUserConnected_nid_postfix(2, true); + Require(sceUserServiceGetEvent(&event) == USER_SERVICE_ERROR_NO_EVENT); + UserServiceSetUserConnected_nid_postfix(2, false); + Require(sceUserServiceGetEvent(&event) == USER_SERVICE_OK); + Require(event.event_type == 1 && event.user_id == USER_SERVICE_INITIAL_USER_ID + 2); + Require(sceUserServiceTerminate() == USER_SERVICE_OK); TestThreadPriority(); diff --git a/core/libs/tests/GuestVectorAlgorithms.cpp b/core/libs/tests/GuestVectorAlgorithms.cpp index c3b858bac6..8fd443df2e 100644 --- a/core/libs/tests/GuestVectorAlgorithms.cpp +++ b/core/libs/tests/GuestVectorAlgorithms.cpp @@ -12,7 +12,9 @@ struct MinMaxPointers { extern "C" { const void* APS5_VABI __std_find_trivial_2_nid_postfix(const void*, const void*, std::uint16_t); const void* APS5_VABI __std_find_trivial_unsized_1_nid_postfix(const void*, std::uint8_t); +const void* APS5_VABI __std_find_trivial_unsized_2_nid_postfix(const void*, std::uint16_t); const void* APS5_VABI __std_find_trivial_unsized_4_nid_postfix(const void*, std::uint32_t); +const void* APS5_VABI __std_find_trivial_unsized_8_nid_postfix(const void*, std::uint64_t); std::size_t APS5_VABI __std_count_trivial_8_nid_postfix(const void*, const void*, std::uint64_t); const void* APS5_VABI __std_min_element_1_nid_postfix(const void*, const void*, bool); const void* APS5_VABI __std_max_element_4_nid_postfix(const void*, const void*, bool); @@ -48,6 +50,36 @@ static void CheckUnsizedRescan() { bytes[3] = 0; bytes[20] = 0; Require(__std_find_trivial_unsized_1_nid_postfix(bytes + 8, 'x') == bytes + 70); + + alignas(32) std::uint16_t halves[32] = {}; + halves[0] = 17; + halves[4] = 17; + halves[20] = 17; + Require(__std_find_trivial_unsized_2_nid_postfix(halves + 1, 17) == halves + 4); + halves[4] = 0; + Require(__std_find_trivial_unsized_2_nid_postfix(halves + 1, 17) == halves); + halves[0] = 0; + Require(__std_find_trivial_unsized_2_nid_postfix(halves + 1, 17) == halves + 20); + + alignas(32) std::uint32_t quads[16] = {}; + quads[0] = 42; + quads[5] = 42; + quads[12] = 42; + Require(__std_find_trivial_unsized_4_nid_postfix(quads + 1, 42) == quads + 5); + quads[5] = 0; + Require(__std_find_trivial_unsized_4_nid_postfix(quads + 1, 42) == quads); + quads[0] = 0; + Require(__std_find_trivial_unsized_4_nid_postfix(quads + 1, 42) == quads + 12); + + alignas(32) std::uint64_t wides[8] = {}; + wides[0] = 99; + wides[2] = 99; + wides[6] = 99; + Require(__std_find_trivial_unsized_8_nid_postfix(wides + 1, 99) == wides + 2); + wides[2] = 0; + Require(__std_find_trivial_unsized_8_nid_postfix(wides + 1, 99) == wides); + wides[0] = 0; + Require(__std_find_trivial_unsized_8_nid_postfix(wides + 1, 99) == wides + 6); } static void CheckMinMax() { diff --git a/core/libs/tests/GuestVideoRecording.cpp b/core/libs/tests/GuestVideoRecording.cpp index 3338094cac..537811d685 100644 --- a/core/libs/tests/GuestVideoRecording.cpp +++ b/core/libs/tests/GuestVideoRecording.cpp @@ -14,6 +14,7 @@ int APS5_VABI sceVideoRecordingOpen(const char* path, const void* param, void* h int APS5_VABI sceVideoRecordingStart(void); int APS5_VABI sceVideoRecordingStop(void); int APS5_VABI sceVideoRecordingClose(int discard); +int APS5_VABI sceVideoRecordingGetInfo(std::int32_t info, void* data, std::size_t size); } namespace { @@ -62,6 +63,9 @@ int main() { for (const std::uint8_t value : heap) Require(value == Sentinel, "the heap was written"); Require(sceVideoRecordingGetStatus() == StatusNone, "a failed open changed the status"); + Require(sceVideoRecordingGetInfo(0xa001, nullptr, 0) == static_cast(0x80a80006), "info 0xa001 validation"); + Require(sceVideoRecordingGetInfo(0xa005, nullptr, 0) == static_cast(0x80a80006), "info 0xa005 validation"); + Require(sceVideoRecordingGetInfo(0, nullptr, 0) == static_cast(0x80a80003), "unknown info validation"); std::puts("VideoRecording tests passed"); return 0; } diff --git a/core/libs/tests/GuestWideConversion.cpp b/core/libs/tests/GuestWideConversion.cpp index b56da38d8d..808a23b90f 100644 --- a/core/libs/tests/GuestWideConversion.cpp +++ b/core/libs/tests/GuestWideConversion.cpp @@ -134,6 +134,11 @@ static void CheckScan() { Require(swscanf_s_nid_postfix(u"x", u"%2lc", pair, std::size_t{2}) == 0, "%2lc short input"); Require(swscanf_s_nid_postfix(u"cab12", u"%l[abc]%d", text, std::size_t{6}, &first) == 2 && std::u16string(text) == u"cab" && first == 12, "%l["); Require(swscanf_s_nid_postfix(u"été!", u"%l[^!]", text, std::size_t{6}) == 1 && std::u16string(text) == u"été", "%l[^"); + char16_t untouched[4] = {u'?', u'?', u'?', u'?'}; + Require(swscanf_s_nid_postfix(u"Āb cd", u"%S", untouched, std::size_t{4}) == 0 && untouched[0] == u'?', "%S is an invalid conversion"); + Require(swscanf_s_nid_postfix(u"7 Āb", u"%d%lS", &first, untouched, std::size_t{4}) == 1 && first == 7 && untouched[0] == u'?', "%lS stops after the earlier assignment"); + Require(swscanf_s_nid_postfix(u"Āyz", u"%2C", untouched, std::size_t{4}) == 0 && untouched[0] == u'?', "%C is an invalid conversion"); + Require(swscanf_s_nid_postfix(u"5 6", u"%hC%d", &first) == 0 && first == 7, "%hC stops before later conversions"); std::int64_t count = -1; Require(swscanf_s_nid_postfix(u" 12 ab", u"%d %ln", &first, &count) == 1 && count == 4, "%ln"); @@ -195,6 +200,11 @@ static void CheckUnsizedScan() { Require(swscanf_nid_postfix(u" q7", u"%c%d", &letter, &first) == 1 && letter == ' ', "swscanf %c does not skip white space"); Require(swscanf_nid_postfix(u"cab12", u"%l[abc]%d", text, &first) == 2 && std::u16string(text) == u"cab" && first == 12, "swscanf %l["); Require(swscanf_nid_postfix(u"été!", u"%l[^!]", text) == 1 && std::u16string(text) == u"été", "swscanf %l[^"); + char16_t untouched[4] = {u'?', u'?', u'?', u'?'}; + Require(swscanf_nid_postfix(u"Āb cd", u"%S", untouched) == 0 && untouched[0] == u'?', "swscanf %S is an invalid conversion"); + Require(swscanf_nid_postfix(u"7 Āb 9", u"%d%3S%d", &first, untouched, &second) == 1 && first == 7 && untouched[0] == u'?', "swscanf stops at %S"); + Require(swscanf_nid_postfix(u"Āyz", u"%lC", untouched) == 0 && untouched[0] == u'?', "swscanf %lC is an invalid conversion"); + Require(swscanf_nid_postfix(u"abc", u"%*S") == 0, "swscanf suppressed %S is an invalid conversion"); Require(swscanf_nid_postfix(u"key=val", u"%[^=]=%s", narrow, narrow + 4) == 2 && std::strcmp(narrow, "key") == 0 && std::strcmp(narrow + 4, "val") == 0, "swscanf %[^"); std::int64_t count = -1; Require(swscanf_nid_postfix(u" 12 ab", u"%d %ln", &first, &count) == 1 && count == 4, "swscanf %ln"); diff --git a/core/libs/tests/IrDumpMemory.cpp b/core/libs/tests/IrDumpMemory.cpp new file mode 100644 index 0000000000..e68b9567f0 --- /dev/null +++ b/core/libs/tests/IrDumpMemory.cpp @@ -0,0 +1,96 @@ +#include "Translation/TranslationContext.hpp" +#include "RdnaDecoder/RdnaMemoryOpDecoder.hpp" +#include +#include +#include +#include +#include +#include +#include + +using namespace ShaderRecompiler; + +static void Require(bool value, const std::string& message) { + if (!value) throw std::runtime_error(message); +} + +static RdnaInstruction Smem(std::uint32_t pc, std::uint32_t opcode, std::uint32_t destination, std::uint32_t base, std::uint32_t offset) { + const std::array code{(0x3du << 26u) | (opcode << 18u) | (destination << 6u) | (base / 2u), (125u << 25u) | offset}; + return DecodeRdnaSmem(pc, code, 0u); +} + +static RdnaInstruction Ds(std::uint32_t pc, std::uint32_t opcode, std::uint32_t offset0, std::uint32_t offset1, std::uint32_t address, std::uint32_t destination) { + const std::array code{(0x36u << 26u) | (opcode << 18u) | (offset1 << 8u) | offset0, (destination << 24u) | address}; + return DecodeRdnaDs(pc, code, 0u); +} + +static void Translate(IrProgram& program, std::initializer_list instructions) { + auto& block = program.CreateBlock(); + program.SetEntryBlock(block); + program.BlockOrder() = {&block}; + program.Metadata().blockInfo.resize(1); + TranslationContext context(program, block, 256); + for (const auto& instruction : instructions) { + context.TranslateInstruction(instruction); + } +} + +static std::vector Lines(const std::string& dump, const std::string& opcode) { + std::vector lines; + std::istringstream stream(dump); + for (std::string line; std::getline(stream, line);) { + if (line.find(" " + opcode + " ") != std::string::npos) lines.push_back(line); + } + return lines; +} + +static void RequireLines(const std::string& dump, const std::string& opcode, std::initializer_list suffixes) { + const auto lines = Lines(dump, opcode); + Require(lines.size() == suffixes.size(), dump + "\nexpected " + std::to_string(suffixes.size()) + " " + opcode + " lines, found " + std::to_string(lines.size())); + std::size_t index = 0; + for (const std::string suffix : suffixes) { + const auto& line = lines[index++]; + Require(line.size() >= suffix.size() && line.compare(line.size() - suffix.size(), suffix.size(), suffix) == 0, dump + "\nexpected \"" + line + "\" to end with \"" + suffix + "\""); + } +} + +static void CheckOffsets() { + IrProgram program; + Translate(program, { + Smem(0x12de0u, 0x00u, 4u, 0u, 0x54u), + Smem(0x12de8u, 0x0au, 12u, 8u, 0x60u), + Ds(0x12df0u, 0x37u, 3u, 9u, 1u, 2u), + }); + const auto dump = ProgramToString(program); + RequireLines(dump, "LoadAddressU32", {" [pc=0x00012de0 off=0x54]"}); + RequireLines(dump, "ReadConstBuffer", {" [pc=0x00012de8 off=0x60]", " [pc=0x00012de8 off=0x64]", " [pc=0x00012de8 off=0x68]", " [pc=0x00012de8 off=0x6c]"}); + RequireLines(dump, "LoadSharedU32", {" [pc=0x00012df0 off=0xc]", " [pc=0x00012df0 off=0x24]"}); + Require(dump.find(" %1 = GetScalarRegister s0 (U32; uses=1)\n") != std::string::npos, dump + "\nexpected register reads without memory fields"); +} + +static void CheckMissingMemoryInfo() { + IrProgram program; + Translate(program, {Smem(0x40u, 0x00u, 4u, 0u, 0x54u)}); + program.Resources().memoryInfo.clear(); + const auto dump = ProgramToString(program); + RequireLines(dump, "LoadAddressU32", {" [pc=0x00000040 mem=?]"}); + bool rejected = false; + try { + ValidateProgram(program, false); + } catch (const std::runtime_error& error) { + rejected = std::string(error.what()) == "LoadAddressU32 has an invalid memory-info index"; + Require(rejected, std::string("unexpected validation failure: ") + error.what()); + } + Require(rejected, "validation accepted a memory instruction without memory info"); +} + +int main() { + try { + CheckOffsets(); + CheckMissingMemoryInfo(); + } catch (const std::exception& error) { + std::fprintf(stderr, "%s\n", error.what()); + return 1; + } + return 0; +} diff --git a/core/libs/tests/NetEmptyStream.cpp b/core/libs/tests/NetEmptyStream.cpp new file mode 100644 index 0000000000..af88d91872 --- /dev/null +++ b/core/libs/tests/NetEmptyStream.cpp @@ -0,0 +1,177 @@ +#include "SceTypes.hpp" +#include +#include +#include +#include +#include +#include +#include +#include + +struct NetIovec { + void* base; + std::uint64_t length; +}; + +struct NetMsghdr { + void* name; + std::uint32_t nameLength; + NetIovec* iov; + int iovLength; + void* control; + std::uint32_t controlLength; + int flags; +}; + +extern "C" { +int APS5_VABI sceNetSocket(const char*, int, int, int); +int APS5_VABI sceNetSocketClose(int); +int APS5_VABI sceNetBind_nid_postfix(int, const void*, std::uint32_t); +int APS5_VABI sceNetGetsockname(int, void*, std::uint32_t*); +int APS5_VABI sceNetListen(int, int); +int APS5_VABI sceNetConnect(int, const void*, std::uint32_t); +int APS5_VABI sceNetAccept(int, void*, std::uint32_t*); +int APS5_VABI sceNetSetsockopt(int, int, int, const void*, std::uint32_t); +std::int64_t APS5_VABI sceNetSend(int, const void*, std::size_t, int); +std::int64_t APS5_VABI sceNetSendto(int, const void*, std::size_t, int, const void*, std::uint32_t); +std::int64_t APS5_VABI sceNetRecv(int, void*, std::size_t, int); +std::int64_t APS5_VABI sceNetRecvfrom(int, void*, std::size_t, int, void*, std::uint32_t*); +std::int64_t APS5_VABI sceNetRecvmsg(int, NetMsghdr*, int); +int* APS5_VABI sceNetErrnoLoc(); +} + +namespace { + +void Require(bool value, const char* message) { + if (value) return; + std::fprintf(stderr, "empty stream: %s\n", message); + std::exit(1); +} + +void SetOption(int descriptor, int option, int value) { + Require(sceNetSetsockopt(descriptor, 0xffff, option, &value, sizeof(value)) == 0, "set option"); +} + +struct Pair { + int sender; + int receiver; + + explicit Pair(bool datagram) { + const int listener = sceNetSocket("empty-listener", 2, datagram ? 2 : 1, 0); + sender = sceNetSocket("empty-sender", 2, datagram ? 2 : 1, 0); + Require(listener >= 0 && sender >= 0, "create sockets"); + std::array address{16, 2, 0, 0, 127, 0, 0, 1}; + Require(sceNetBind_nid_postfix(listener, address.data(), address.size()) == 0, "bind receiver"); + std::uint32_t size = address.size(); + Require(sceNetGetsockname(listener, address.data(), &size) == 0, "get receiver name"); + if (!datagram) Require(sceNetListen(listener, 1) == 0, "listen"); + Require(sceNetConnect(sender, address.data(), size) == 0, "connect sender"); + if (datagram) receiver = listener; + else { + receiver = sceNetAccept(listener, nullptr, nullptr); + Require(receiver >= 0, "accept"); + Require(sceNetSocketClose(listener) == 0, "close listener"); + } + SetOption(receiver, 0x1106, 2000000); + } + + ~Pair() { + Require(sceNetSocketClose(receiver) == 0 && sceNetSocketClose(sender) == 0, "close pair"); + } + + void Send() { + Require(sceNetSend(sender, "x", 1, 0) == 1, "send byte"); + } +}; + +struct Result { + std::int64_t count; + int error; +}; + +Result Receive(std::string_view api, int descriptor, int flags, bool nullBuffer, bool datagram) { + char payload = 'p'; + char control = 'c'; + std::array address; + address.fill(0xaa); + std::uint32_t addressLength = address.size(); + std::int64_t count; + if (api == "recv") count = sceNetRecv(descriptor, nullBuffer ? nullptr : &payload, 0, flags); + else if (api == "recvfrom") + count = sceNetRecvfrom(descriptor, nullBuffer ? nullptr : &payload, 0, flags, address.data(), &addressLength); + else { + NetIovec vector{&payload, 0}; + NetMsghdr message{address.data(), addressLength, nullBuffer ? nullptr : &vector, nullBuffer ? 0 : 1, &control, 1, 0x4000}; + count = sceNetRecvmsg(descriptor, &message, flags); + if (count >= 0) { + Require(message.controlLength == 0, "control length is not zero"); + Require(message.flags == (datagram ? 0x10 : 0), "unexpected output flags"); + addressLength = message.nameLength; + } + } + const int error = *sceNetErrnoLoc(); + Require(payload == 'p' && control == 'c', "empty receive modified payload or control storage"); + if (count >= 0 && api != "recv") { + Require(addressLength == (datagram ? 16u : 0u), "wrong sender length for an empty receive"); + if (!datagram) Require(std::all_of(address.begin(), address.end(), [](unsigned char byte) { return byte == 0xaa; }), + "empty stream receive wrote a sender address"); + } + return {count, error}; +} + +void Check(std::string_view api, std::string_view mode) { + const bool datagram = mode == "datagram"; + Pair pair(datagram); + if (mode == "errors") { + auto result = Receive(api, -1, 0, false, false); + Require(result.count < 0 && result.error == 9, "invalid descriptor became an empty success"); + result = Receive(api, pair.receiver, 1, false, false); + Require(result.count < 0 && result.error == 45, "unsupported flags became an empty success"); + const int fresh = sceNetSocket("empty-unconnected", 2, 1, 0); + Require(fresh >= 0, "create unconnected socket"); + result = Receive(api, fresh, 0, false, false); + std::fprintf(stderr, "unconnected result=%lld errno=%d\n", static_cast(result.count), result.error); + Require(result.count < 0 && result.error == 57, "fresh stream socket did not report ENOTCONN"); + std::array address{16, 2, 0, 0, 127, 0, 0, 1}; + Require(sceNetBind_nid_postfix(fresh, address.data(), address.size()) == 0, "bind unconnected socket"); + result = Receive(api, fresh, 0, false, false); + Require(result.count < 0 && result.error == 57, "bound stream socket did not report ENOTCONN"); + Require(sceNetListen(fresh, 1) == 0, "listen on unconnected socket"); + result = Receive(api, fresh, 0, false, false); + Require(result.count < 0 && result.error == 57, "listening socket did not report ENOTCONN"); + Require(sceNetSocketClose(fresh) == 0, "close unconnected socket"); + return; + } + if (mode == "queued" || datagram) pair.Send(); + if (mode == "nonblocking") SetOption(pair.receiver, 0x1200, 1); + const int flags = mode == "dontwait" ? 0x80 : mode == "peek" ? 0x82 : 0; + auto pending = std::async(std::launch::async, [&] { return Receive(api, pair.receiver, flags, mode == "nullbuf", datagram); }); + const bool ready = pending.wait_for(std::chrono::milliseconds(500)) == std::future_status::ready; + if (!ready) { + pair.Send(); + Require(pending.wait_for(std::chrono::seconds(2)) == std::future_status::ready, "cleanup receive did not finish"); + } + const auto result = pending.get(); + Require(ready, "zero-length receive waited for peer data"); + Require(result.count == 0, "zero-length receive did not return zero"); + if (mode == "queued") { + char byte = 0; + Require(sceNetRecv(pair.receiver, &byte, 1, 0) == 1 && byte == 'x', "empty stream receive consumed data"); + } + if (datagram) { + char byte = 0; + Require(sceNetRecv(pair.receiver, &byte, 1, 0x80) < 0 && *sceNetErrnoLoc() == 35, + "zero-length datagram receive did not consume its packet"); + } + std::printf("%.*s/%.*s: zero return, output sentinels and transport semantics preserved\n", + static_cast(api.size()), api.data(), static_cast(mode.size()), mode.data()); +} + +} + +int main(int argc, char** argv) { + Require(argc == 3, "expected API and mode"); + const std::string_view api(argv[1]); + Require(api == "recv" || api == "recvfrom" || api == "recvmsg", "unknown API"); + Check(api, argv[2]); +} diff --git a/core/libs/tests/WindowsWriteWatch.cpp b/core/libs/tests/WindowsWriteWatch.cpp index b44c48af61..7a42cdea83 100644 --- a/core/libs/tests/WindowsWriteWatch.cpp +++ b/core/libs/tests/WindowsWriteWatch.cpp @@ -172,6 +172,149 @@ void CheckPrivate() { VirtualFree(memory, 0, MEM_RELEASE); } +MEMORY_BASIC_INFORMATION RegionAt(const void* address) { + MEMORY_BASIC_INFORMATION info{}; + Require(VirtualQuery(address, &info, sizeof(info)) == sizeof(info), "private commit query failed"); + return info; +} + +void CheckPrivateCommitCarvesPlaceholders() { + auto& mappings = Mappings::Get(); + constexpr std::size_t Page = 16384; + constexpr std::size_t Pages = 256; + constexpr std::size_t Span = Pages * Page; + constexpr std::size_t Reserved = std::size_t{1} << 30; + auto* holder = static_cast(mappings.Reserve(nullptr, Reserved)); + Require(holder != nullptr, "cannot reserve a placeholder for private commits"); + auto* memory = holder + Page; + const auto page = [&](std::size_t index) { return memory + index * Page; }; + const auto at = [](const unsigned char* pointer) { return reinterpret_cast(pointer); }; + auto before = Mappings::MemoryQueries(); + auto created = mappings.Commit(memory, Span, PAGE_READWRITE, Page, true); + const auto queries = Mappings::MemoryQueries() - before; + Require(queries == 1, ("committing 256 fresh pages queried the address space " + std::to_string(queries) + " times").c_str()); + Require(created.size() == 1 && created[0].first == at(memory) && created[0].second == Span, "a fresh commit did not report its range as one created range"); + for (std::size_t index = 0; index < Pages; ++index) { + const auto info = RegionAt(page(index)); + Require(info.State == MEM_COMMIT && info.Type == MEM_PRIVATE && info.AllocationBase == page(index) && info.RegionSize == Page && info.Protect == PAGE_READWRITE, "a committed page is not a read-write allocation of its own"); + } + const auto prefix = RegionAt(holder); + Require(prefix.State == MEM_RESERVE && prefix.AllocationBase == holder && prefix.RegionSize == Page, "the placeholder before the commit changed"); + const auto suffix = RegionAt(page(Pages)); + Require(suffix.State == MEM_RESERVE && suffix.AllocationBase == page(Pages) && suffix.RegionSize == Reserved - Page - Span, "the placeholder after the commit changed"); + for (std::size_t index = 0; index < Pages; ++index) page(index)[8] = static_cast(index + 1); + mappings.Reset(page(11), Page); + const auto released = RegionAt(page(11)); + Require(released.State == MEM_RESERVE && released.AllocationBase == page(11) && released.RegionSize == Page, "a released page did not become a placeholder of its own"); + before = Mappings::MemoryQueries(); + created = mappings.Commit(page(11), Page, PAGE_READWRITE, Page, true); + Require(Mappings::MemoryQueries() - before <= 1 && created.size() == 1 && created[0].first == at(page(11)) && created[0].second == Page && page(11)[8] == 0, "recommitting a released page was not one fresh allocation"); + for (std::size_t index = 0; index < Pages; ++index) { + if (index != 11) Require(page(index)[8] == static_cast(index + 1), "a release or commit changed a neighbouring page"); + } + created = mappings.Commit(memory, Span, PAGE_READONLY, Page, true); + Require(created.empty() && RegionAt(page(0)).Protect == PAGE_READONLY && RegionAt(page(Pages - 1)).Protect == PAGE_READONLY, "a protection change reported fresh memory or missed a page"); + mappings.Commit(memory, Span, PAGE_READWRITE, Page, true); + std::array written{}; + std::size_t count = written.size(); + mappings.Collect(at(page(40)), 4 * Page, written.data(), &count, true); + page(41)[4096 + 3] = 1; + count = written.size(); + Require(mappings.Collect(at(page(40)), 4 * Page, written.data(), &count, true, Collection::Armed) && count == 1 && written[0] == page(41) + 4096, "a carved page lost its write watch"); + mappings.Reset(page(20), 2 * Page); + mappings.Reset(page(22), 2 * Page); + created = mappings.Commit(page(20), 4 * Page, PAGE_READWRITE, 4 * Page, true); + Require(created.size() == 1 && created[0].second == 4 * Page && RegionAt(page(23)).AllocationBase == page(20), "a commit over two placeholders was not one allocation"); + mappings.Reset(memory, Span); + Require(VirtualFree(holder, Reserved, MEM_RELEASE | MEM_COALESCE_PLACEHOLDERS) && VirtualFree(holder, 0, MEM_RELEASE), "cannot release the private commit placeholder"); +} + +void CheckPrivateCommitEdgeLayouts() { + auto& mappings = Mappings::Get(); + constexpr std::size_t Page = 16384; + constexpr std::size_t Pages = 32; + auto* memory = static_cast(mappings.Reserve(nullptr, Pages * Page)); + Require(memory != nullptr, "cannot reserve a placeholder for private commit edges"); + const auto page = [&](std::size_t index) { return memory + index * Page; }; + const auto range = [&](std::size_t first, std::size_t count) { return std::make_pair(reinterpret_cast(page(first)), count * Page); }; + const auto region = [&](std::size_t first, std::size_t count, DWORD state) { + const auto info = RegionAt(page(first)); + return info.State == state && info.AllocationBase == page(first) && info.RegionSize == count * Page; + }; + const auto ownPages = [&](std::size_t first, std::size_t count) { + for (auto index = first; index < first + count; ++index) { + if (!region(index, 1, MEM_COMMIT)) return false; + } + return true; + }; + auto before = Mappings::MemoryQueries(); + auto created = mappings.Commit(page(2), 4 * Page, PAGE_READWRITE, 4 * Page, true); + Require(Mappings::MemoryQueries() - before <= 1 && created.size() == 1 && created[0] == range(2, 4), "a whole-range granule inside a placeholder was not one created range from one query"); + Require(region(0, 2, MEM_RESERVE) && region(2, 4, MEM_COMMIT) && region(6, Pages - 6, MEM_RESERVE), "a whole-range granule inside a placeholder was not one allocation between two placeholders"); + page(2)[8] = 0x5a; + before = Mappings::MemoryQueries(); + created = mappings.Commit(page(0), 8 * Page, PAGE_READWRITE, Page, true); + Require(Mappings::MemoryQueries() - before <= 3 && created.size() == 2 && created[0] == range(0, 2) && created[1] == range(6, 2), "a commit around a committed piece did not create exactly the placeholders around it"); + Require(ownPages(0, 2) && region(2, 4, MEM_COMMIT) && page(2)[8] == 0x5a && ownPages(6, 2) && region(8, Pages - 8, MEM_RESERVE), "a commit around a committed piece changed the piece or the layout"); + for (std::size_t index = 0; index < 8; ++index) page(index)[8] = static_cast(index + 1); + mappings.Reset(page(6), 2 * Page); + Require(region(6, 2, MEM_RESERVE) && ownPages(0, 2) && region(2, 4, MEM_COMMIT) && page(1)[8] == 2 && page(5)[8] == 6 && region(8, Pages - 8, MEM_RESERVE), "a partial release of carved pages changed a neighbour"); + mappings.Reset(page(2), 4 * Page); + before = Mappings::MemoryQueries(); + created = mappings.Commit(page(2), 4 * Page, PAGE_READWRITE, Page, true); + Require(Mappings::MemoryQueries() - before <= 1 && created.size() == 1 && created[0] == range(2, 4) && ownPages(2, 4) && page(2)[8] == 0 && page(5)[8] == 0, "a fixed remap was not carved into fresh pages"); + Require(page(1)[8] == 2 && region(6, 2, MEM_RESERVE), "a fixed remap changed a neighbour"); + before = Mappings::MemoryQueries(); + created = mappings.Commit(page(6), 4 * Page, PAGE_READWRITE, Page, true); + Require(Mappings::MemoryQueries() - before <= 2 && created.size() == 1 && created[0] == range(6, 4) && ownPages(6, 4) && region(10, Pages - 10, MEM_RESERVE), "a commit over adjacent placeholders was not carved page by page"); + bool refused = false; + try { + mappings.Commit(page(12), Page, PAGE_READWRITE, 0, true); + } catch (const std::invalid_argument&) { + refused = true; + } + Require(refused && region(10, Pages - 10, MEM_RESERVE), "a zero commit granule was accepted or changed the placeholder"); + mappings.Commit(page(10), 3 * Page, PAGE_READWRITE, 3 * Page, true); + mappings.Reset(page(10), 3 * Page); + created = mappings.Commit(page(10), 4 * Page, PAGE_READWRITE, 2 * Page, true); + Require(created.size() == 1 && created[0] == range(10, 4) && region(10, 2, MEM_COMMIT) && region(12, 2, MEM_COMMIT) && region(14, Pages - 14, MEM_RESERVE), "a granule crossing the end of a carved placeholder did not take the coalescing path"); + mappings.Reset(memory, 14 * Page); + Require(VirtualFree(memory, Pages * Page, MEM_RELEASE | MEM_COALESCE_PLACEHOLDERS) && VirtualFree(memory, 0, MEM_RELEASE), "cannot release the private commit edge placeholder"); +} + +void CheckPrivateCommitTails() { + auto& mappings = Mappings::Get(); + constexpr std::size_t Page = 16384; + constexpr std::size_t Pages = 8; + auto* memory = static_cast(mappings.Reserve(nullptr, Pages * Page)); + Require(memory != nullptr, "cannot reserve a placeholder for private commit tails"); + const auto page = [&](std::size_t index) { return memory + index * Page; }; + const auto at = [](const unsigned char* pointer) { return reinterpret_cast(pointer); }; + const auto allocation = [](const unsigned char* base, std::size_t bytes, DWORD state) { + const auto info = RegionAt(base); + return info.State == state && info.AllocationBase == base && info.RegionSize == bytes; + }; + const auto unwatched = [](unsigned char* base) { + std::array written{}; + ULONG_PTR count = written.size(); + DWORD granularity = 0; + return GetWriteWatch(0, base, Small, written.data(), &count, &granularity) != 0; + }; + auto before = Mappings::MemoryQueries(); + auto created = mappings.Commit(page(1), 2 * Page + Small, PAGE_READWRITE, Page, false); + Require(Mappings::MemoryQueries() - before == 1 && created.size() == 1 && created[0].first == at(page(1)) && created[0].second == 2 * Page + Small, "a commit with a short last piece was not one created range from one query"); + Require(allocation(page(0), Page, MEM_RESERVE) && allocation(page(1), Page, MEM_COMMIT) && allocation(page(2), Page, MEM_COMMIT) && allocation(page(3), Small, MEM_COMMIT) && allocation(page(3) + Small, (Pages - 3) * Page - Small, MEM_RESERVE), "a short last piece was not its own allocation before the rest of the placeholder"); + page(3)[Small - 1] = 7; + Require(unwatched(page(1)) && unwatched(page(3)), "an unwatched commit carved write-watched pages"); + before = Mappings::MemoryQueries(); + created = mappings.Commit(page(5), 3 * Page, PAGE_READWRITE, Page, true); + Require(Mappings::MemoryQueries() - before == 1 && created.size() == 1 && created[0].first == at(page(5)) && created[0].second == 3 * Page, "a commit to the end of a placeholder was not one created range from one query"); + Require(allocation(page(3) + Small, 2 * Page - Small, MEM_RESERVE) && allocation(page(5), Page, MEM_COMMIT) && allocation(page(6), Page, MEM_COMMIT) && allocation(page(7), Page, MEM_COMMIT) && page(3)[Small - 1] == 7, "a commit to the end of a placeholder changed the placeholder before it or a neighbour"); + mappings.Reset(page(1), 2 * Page + Small); + mappings.Reset(page(5), 3 * Page); + Require(VirtualFree(memory, Pages * Page, MEM_RELEASE | MEM_COALESCE_PLACEHOLDERS) && VirtualFree(memory, 0, MEM_RELEASE), "cannot release the private commit tail placeholder"); +} + void CheckForgottenSectionKeepsNoPages() { auto& mappings = Mappings::Get(); HANDLE section = CreateFileMappingW(INVALID_HANDLE_VALUE, nullptr, PAGE_EXECUTE_READWRITE | SEC_RESERVE, 0, Bytes, nullptr); @@ -216,6 +359,180 @@ void CheckConcurrentWrites() { region.Collect(Collection::Armed); Require(region.Collect(Collection::Armed).empty(), "concurrent writes did not settle"); } + +void CheckComparedWhenShared() { + auto& mappings = Mappings::Get(); + HANDLE section = CreateFileMappingW(INVALID_HANDLE_VALUE, nullptr, PAGE_EXECUTE_READWRITE | SEC_RESERVE, 0, 2 * Bytes, nullptr); + auto* first = static_cast(mappings.Reserve(nullptr, 2 * Bytes)); + auto* second = static_cast(mappings.Reserve(nullptr, 2 * Bytes)); + Require(section && first && second, "cannot allocate compared sharing test memory"); + mappings.ForgetSection(section); + const auto collect = [&](unsigned char* address, std::size_t bytes) { + std::array pages{}; + std::size_t count = pages.size(); + return mappings.Collect(reinterpret_cast(address), bytes, pages.data(), &count, true); + }; + const auto protection = [](unsigned char* address) { + MEMORY_BASIC_INFORMATION info{}; + Require(VirtualQuery(address, &info, sizeof(info)) == sizeof(info), "compared sharing protection query failed"); + return info; + }; + mappings.Map(first, 2 * Bytes, section, 0, PAGE_READWRITE, true, true); + Require(protection(first).Type == MEM_PRIVATE, "the compared sharing test memory is not resident"); + mappings.CompareWhenShared(reinterpret_cast(first), Bytes); + Require(collect(first, 2 * Bytes), "a resident page to be compared when shared stopped being collected"); + mappings.Map(second, 2 * Bytes, section, 0, PAGE_READWRITE, true, true); + Require(protection(first).Type == MEM_MAPPED && protection(second).Type == MEM_MAPPED, "a second mapping kept the first resident"); + Require(!collect(first, Bytes) && !collect(second, Bytes), "a page to be compared when shared was collected once shared"); + Require(!collect(first, 2 * Bytes), "a collect over a page to be compared when shared succeeded once shared"); + Require(protection(first).Protect == PAGE_READWRITE && protection(second).Protect == PAGE_READWRITE, "a page to be compared when shared was armed once shared"); + const auto before = faults.load(); + first[8] = 0x5a; + second[Small + 8] = 0xa5; + Require(faults.load() == before && second[8] == 0x5a && first[Small + 8] == 0xa5, "a store into a page to be compared when shared faulted once shared"); + Require(collect(first + Bytes, Bytes), "a shared page that was never to be compared stopped being collected"); + mappings.Reset(first, 2 * Bytes); + mappings.Reset(second, 2 * Bytes); + for (auto* reserved : {first, second}) { + VirtualFree(reserved, 2 * Bytes, MEM_RELEASE | MEM_COALESCE_PLACEHOLDERS); + VirtualFree(reserved, 0, MEM_RELEASE); + } + mappings.ForgetSection(section); + CloseHandle(section); +} + +void CheckResidentOnlyComparedWhenShared() { + auto& mappings = Mappings::Get(); + HANDLE section = CreateFileMappingW(INVALID_HANDLE_VALUE, nullptr, PAGE_EXECUTE_READWRITE | SEC_RESERVE, 0, Bytes, nullptr); + auto* first = static_cast(mappings.Reserve(nullptr, Bytes)); + auto* second = static_cast(mappings.Reserve(nullptr, Bytes)); + Require(section && first && second, "cannot allocate resident-only sharing test memory"); + mappings.ForgetSection(section); + mappings.Map(first, Bytes, section, 0, PAGE_READWRITE, true, true); + MEMORY_BASIC_INFORMATION info{}; + Require(VirtualQuery(first, &info, sizeof(info)) == sizeof(info) && info.Type == MEM_PRIVATE, "the resident-only sharing test memory is not resident"); + mappings.CompareWhenShared(reinterpret_cast(first), Bytes, true); + mappings.Map(second, Bytes, section, 0, PAGE_READWRITE, true, true); + std::array pages{}; + std::size_t count = pages.size(); + Require(!mappings.Collect(reinterpret_cast(first), Bytes, pages.data(), &count, true), "a resident page marked to be compared was collected once shared"); + const auto before = faults.load(); + first[8] = 0x3c; + Require(faults.load() == before && second[8] == 0x3c, "a store into a resident page marked to be compared faulted once shared"); + mappings.Reset(first, Bytes); + mappings.Reset(second, Bytes); + for (auto* reserved : {first, second}) { + VirtualFree(reserved, Bytes, MEM_RELEASE | MEM_COALESCE_PLACEHOLDERS); + VirtualFree(reserved, 0, MEM_RELEASE); + } + mappings.ForgetSection(section); + CloseHandle(section); + Region region; + mappings.CompareWhenShared(reinterpret_cast(region.first), Bytes, true); + Require(region.Collect(Collection::All, 0, 1) == Pages(0, 1), "marking only resident pages changed a shared view"); + Require(region.Protection(0) == PAGE_READONLY, "a shared view outside a resident-only mark was not armed"); +} + +void CheckComparedWhenSharedRemapped() { + auto& mappings = Mappings::Get(); + constexpr std::size_t Page = 16384; + HANDLE section = CreateFileMappingW(INVALID_HANDLE_VALUE, nullptr, PAGE_EXECUTE_READWRITE | SEC_RESERVE, 0, Bytes, nullptr); + std::array reserved{}; + for (auto& address : reserved) address = static_cast(mappings.Reserve(nullptr, Bytes)); + Require(section && reserved[0] && reserved[1] && reserved[2], "cannot allocate remapped compared sharing test memory"); + mappings.ForgetSection(section); + const auto collect = [&](unsigned char* address) { + std::array pages{}; + std::size_t count = pages.size(); + return mappings.Collect(reinterpret_cast(address), Page, pages.data(), &count, true); + }; + const auto memory = [](unsigned char* address) { + MEMORY_BASIC_INFORMATION info{}; + Require(VirtualQuery(address, &info, sizeof(info)) == sizeof(info), "remapped compared sharing query failed"); + return info; + }; + const auto share = [&] { + mappings.Map(reserved[1], Page, section, 0, PAGE_READWRITE, true, true); + mappings.Map(reserved[2], Page, section, 0, PAGE_READWRITE, true, true); + Require(memory(reserved[1]).Type == MEM_MAPPED && memory(reserved[2]).Type == MEM_MAPPED, "the remapped page is not shared"); + }; + const auto unmapShared = [&] { + mappings.Reset(reserved[1], Page); + mappings.Reset(reserved[2], Page); + }; + mappings.Map(reserved[0], Page, section, 0, PAGE_READWRITE, true, true); + Require(memory(reserved[0]).Type == MEM_PRIVATE, "the remapped compared sharing test memory is not resident"); + mappings.CompareWhenShared(reinterpret_cast(reserved[0]), Page); + mappings.Reset(reserved[0], Page); + share(); + Require(!collect(reserved[1]) && !collect(reserved[2]), "a page to be compared when shared was collected after every view was unmapped and it was mapped again"); + Require(memory(reserved[1]).Protect == PAGE_READWRITE && memory(reserved[2]).Protect == PAGE_READWRITE, "a page to be compared when shared was armed after every view was unmapped and it was mapped again"); + const auto before = faults.load(); + reserved[1][8] = 0x6b; + Require(faults.load() == before && reserved[2][8] == 0x6b, "a store into a remapped page to be compared faulted once shared"); + unmapShared(); + mappings.ForgetSection(section); + share(); + Require(collect(reserved[1]) && memory(reserved[2]).Protect == PAGE_READONLY, "a forgotten section's handle value kept its pages compared when shared"); + unmapShared(); + for (auto* address : reserved) { + VirtualFree(address, Bytes, MEM_RELEASE | MEM_COALESCE_PLACEHOLDERS); + VirtualFree(address, 0, MEM_RELEASE); + } + mappings.ForgetSection(section); + CloseHandle(section); +} + +void CheckComparedPagesReportedOnce() { + auto& mappings = Mappings::Get(); + HANDLE section = CreateFileMappingW(INVALID_HANDLE_VALUE, nullptr, PAGE_EXECUTE_READWRITE | SEC_RESERVE, 0, 2 * Bytes, nullptr); + std::array reserved{}; + for (auto& address : reserved) address = static_cast(mappings.Reserve(nullptr, 2 * Bytes)); + Require(section && reserved[0] && reserved[1] && reserved[2], "cannot allocate compared page report test memory"); + mappings.ForgetSection(section); + const auto listed = [&](unsigned char* base, std::size_t begin, std::size_t count, bool unreported) { + const auto address = reinterpret_cast(base); + auto cursor = address + begin * Small; + const auto end = cursor + count * Small; + std::set result; + while (cursor < end) { + std::array pages{}; + std::size_t available = pages.size(); + mappings.ComparedPages(cursor, end - cursor, pages.data(), &available, unreported); + for (std::size_t i = 0; i < available; ++i) result.insert((reinterpret_cast(pages[i]) - address) / Small); + if (available < pages.size()) break; + cursor = reinterpret_cast(pages[available - 1]) + Small; + } + return result; + }; + auto* first = reserved[0]; + auto* second = reserved[1]; + mappings.Map(first, 2 * Bytes, section, 0, PAGE_READWRITE, true, true); + mappings.CompareWhenShared(reinterpret_cast(first), Bytes); + Require(listed(first, 0, 32, false).empty() && listed(first, 0, 32, true).empty(), "a resident page to be compared was listed before it was shared"); + mappings.Map(second, 2 * Bytes, section, 0, PAGE_READWRITE, true, true); + Require(listed(first, 0, 32, false) == Pages(0, 16) && listed(second, 0, 32, false) == Pages(0, 16), "the compared pages of both mappings were not listed once shared"); + Require(listed(first, 2, 4, true) == Pages(2, 6), "a partial take did not report exactly its compared pages"); + std::set rest = Pages(0, 2); + rest.insert(6); + for (std::size_t page = 7; page < 16; ++page) rest.insert(page); + Require(listed(first, 0, 32, true) == rest, "a compared page was reported twice or not at all"); + Require(listed(first, 0, 32, true).empty() && listed(first, 0, 32, false) == Pages(0, 16), "a compared page was reported again, or a listing consumed its mark"); + Require(listed(second, 0, 32, true) == Pages(0, 16) && listed(second, 0, 32, true).empty(), "the second mapping's compared pages were not reported exactly once"); + mappings.CompareWhenShared(reinterpret_cast(first), Bytes); + Require(listed(first, 0, 32, true).empty() && listed(second, 0, 32, true).empty(), "marking compared pages again reported them again"); + mappings.CompareWhenShared(reinterpret_cast(first + Bytes), Bytes); + Require(listed(first, 16, 16, true) == Pages(16, 32) && listed(second, 16, 16, true) == Pages(16, 32), "shared pages newly marked to be compared were not reported in every mapping"); + mappings.Map(reserved[2], 2 * Bytes, section, 0, PAGE_READWRITE, true, true); + Require(listed(reserved[2], 0, 32, true) == Pages(0, 32) && listed(first, 0, 32, true).empty(), "a new mapping of compared pages was not reported, or reported the others again"); + for (auto* address : reserved) mappings.Reset(address, 2 * Bytes); + for (auto* address : reserved) { + VirtualFree(address, 2 * Bytes, MEM_RELEASE | MEM_COALESCE_PLACEHOLDERS); + VirtualFree(address, 0, MEM_RELEASE); + } + mappings.ForgetSection(section); + CloseHandle(section); +} } int main() { @@ -230,6 +547,13 @@ int main() { CheckPrivate(); CheckConcurrentWrites(); CheckForgottenSectionKeepsNoPages(); + CheckComparedWhenShared(); + CheckResidentOnlyComparedWhenShared(); + CheckComparedWhenSharedRemapped(); + CheckComparedPagesReportedOnce(); + CheckPrivateCommitCarvesPlaceholders(); + CheckPrivateCommitEdgeLayouts(); + CheckPrivateCommitTails(); std::cout << "Windows deferred write-watch tests passed\n"; } catch (const std::exception& error) { std::cerr << error.what() << '\n'; diff --git a/core/relinker/CMakeLists.txt b/core/relinker/CMakeLists.txt index db46fa7af8..55968ce9c2 100644 --- a/core/relinker/CMakeLists.txt +++ b/core/relinker/CMakeLists.txt @@ -33,6 +33,7 @@ add_library(relinker_core STATIC ${CMAKE_CURRENT_SOURCE_DIR}/io/src/NativePath.cpp ${CMAKE_CURRENT_SOURCE_DIR}/io/src/FileIdentity.cpp ${CMAKE_CURRENT_SOURCE_DIR}/codegen/src/Amd64OnlyConverter.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/codegen/src/x86/RedZoneFramer.cpp ${CMAKE_CURRENT_SOURCE_DIR}/codegen/src/InstructionScanner.cpp ${CMAKE_CURRENT_SOURCE_DIR}/codegen/src/x86/X64InstructionDecoder.cpp ${CMAKE_CURRENT_SOURCE_DIR}/codegen/src/x86/X64InstructionRewriter.cpp @@ -52,6 +53,7 @@ add_library(relinker_core STATIC ${CMAKE_CURRENT_SOURCE_DIR}/codegen/src/x86/WideStoreOperands.cpp ${CMAKE_CURRENT_SOURCE_DIR}/codegen/src/x86/WideStoreLowering.cpp ${CMAKE_CURRENT_SOURCE_DIR}/codegen/src/x86/ScalarCompareOperands.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/codegen/src/x86/MaskedStoreLowering.cpp ${CMAKE_CURRENT_SOURCE_DIR}/codegen/src/x86/AndnLowering.cpp ${CMAKE_CURRENT_SOURCE_DIR}/relinker/src/parsing/ElfReader.cpp ${CMAKE_CURRENT_SOURCE_DIR}/relinker/src/analysis/SyscallScanner.cpp @@ -98,6 +100,11 @@ if(BUILD_TESTING) add_test(NAME andn_lowering COMMAND andn_lowering_tests) set_tests_properties(andn_lowering PROPERTIES TIMEOUT 20) + add_test_executable(masked_store_lowering_tests ${CMAKE_CURRENT_SOURCE_DIR}/codegen/tests/MaskedStoreLoweringTests.cpp) + target_link_libraries(masked_store_lowering_tests PRIVATE relinker_core) + add_test(NAME masked_store_lowering COMMAND masked_store_lowering_tests) + set_tests_properties(masked_store_lowering PROPERTIES TIMEOUT 20) + add_test_executable(relinker_conversion_tests ${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/ConversionTests.cpp) target_link_libraries(relinker_conversion_tests PRIVATE relinker_core) add_test(NAME relinker_conversion COMMAND relinker_conversion_tests) @@ -106,6 +113,11 @@ if(BUILD_TESTING) target_link_libraries(relinker_game_patch_tests PRIVATE relinker_core) add_test(NAME relinker_game_patch COMMAND relinker_game_patch_tests) set_tests_properties(relinker_game_patch PROPERTIES TIMEOUT 30) + + add_test_executable(call_registry_writer_tests ${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/CallRegistryWriterTests.cpp) + target_link_libraries(call_registry_writer_tests PRIVATE relinker_core) + add_test(NAME call_registry_writer COMMAND call_registry_writer_tests) + set_tests_properties(call_registry_writer PROPERTIES TIMEOUT 20) endif() if(ANYPS5_BUILD_WASM) @@ -198,6 +210,7 @@ if(WIN32) ${CMAKE_CURRENT_SOURCE_DIR}/codegen/src/x86/WideStoreOperands.cpp ${CMAKE_CURRENT_SOURCE_DIR}/codegen/src/x86/WideStoreLowering.cpp ${CMAKE_CURRENT_SOURCE_DIR}/codegen/src/x86/ScalarCompareOperands.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/codegen/src/x86/MaskedStoreLowering.cpp ${CMAKE_CURRENT_SOURCE_DIR}/codegen/src/x86/AndnLowering.cpp ) target_include_directories(amd64_only_windows_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/elfpatcher/include ${CMAKE_CURRENT_SOURCE_DIR}/domain/include ${CMAKE_CURRENT_SOURCE_DIR}/io/include ${CMAKE_CURRENT_SOURCE_DIR}/codegen/include ${CMAKE_CURRENT_SOURCE_DIR}/../libs/prx/libc/src/specifics/windows) @@ -248,6 +261,19 @@ if(BUILD_TESTING) add_test(NAME sdk_mman_syscall COMMAND sdk_mman_syscall_tests) set_tests_properties(sdk_mman_syscall PROPERTIES TIMEOUT 20) + add_test_executable(code_data_scanning_tests + ${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/CodeDataScanningTests.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/relinker/src/analysis/SyscallScanner.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/codegen/src/InstructionScanner.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/codegen/src/x86/X64InstructionDecoder.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/codegen/src/x86/DecodedInstruction.cpp) + target_include_directories(code_data_scanning_tests PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/domain/include + ${CMAKE_CURRENT_SOURCE_DIR}/relinker/include + ${CMAKE_CURRENT_SOURCE_DIR}/codegen/include) + add_test(NAME code_data_scanning COMMAND code_data_scanning_tests) + set_tests_properties(code_data_scanning PROPERTIES TIMEOUT 20) + add_test_executable(runtime_layout_tests ${CMAKE_CURRENT_SOURCE_DIR}/cli/tests/RuntimeLayoutTests.cpp) target_include_directories(runtime_layout_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/cli/include) add_test(NAME runtime_layout COMMAND runtime_layout_tests) @@ -276,6 +302,16 @@ if(BUILD_TESTING) add_test(NAME rewrite_boundaries COMMAND rewrite_boundaries_tests) set_tests_properties(rewrite_boundaries PROPERTIES TIMEOUT 20) + add_test_executable(red_zone_framer_tests ${CMAKE_CURRENT_SOURCE_DIR}/codegen/tests/RedZoneFramerTests.cpp) + target_link_libraries(red_zone_framer_tests PRIVATE relinker_core) + add_test(NAME red_zone_framer COMMAND red_zone_framer_tests) + if(CMAKE_SYSTEM_NAME STREQUAL "Linux" AND CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|AMD64") + add_test_executable(red_zone_framer_execution_tests ${CMAKE_CURRENT_SOURCE_DIR}/codegen/tests/RedZoneFramerExecutionTests.cpp) + target_link_libraries(red_zone_framer_execution_tests PRIVATE relinker_core) + add_test(NAME red_zone_framer_execution COMMAND red_zone_framer_execution_tests) + set_tests_properties(red_zone_framer_execution PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 60) + endif() + add_test_executable(cli_args_tests ${CMAKE_CURRENT_SOURCE_DIR}/cli/tests/CliArgsTests.cpp ${CMAKE_CURRENT_SOURCE_DIR}/cli/src/CliArgs.cpp @@ -414,6 +450,7 @@ if(BUILD_TESTING) ${CMAKE_CURRENT_SOURCE_DIR}/codegen/src/x86/WideStoreOperands.cpp ${CMAKE_CURRENT_SOURCE_DIR}/codegen/src/x86/WideStoreLowering.cpp ${CMAKE_CURRENT_SOURCE_DIR}/codegen/src/x86/ScalarCompareOperands.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/codegen/src/x86/MaskedStoreLowering.cpp ${CMAKE_CURRENT_SOURCE_DIR}/codegen/src/x86/AndnLowering.cpp ${CMAKE_CURRENT_SOURCE_DIR}/elfpatcher/src/linux/LinuxElfPatcher.cpp ${CMAKE_CURRENT_SOURCE_DIR}/elfpatcher/src/general/SegmentFilter.cpp @@ -589,6 +626,13 @@ if(BUILD_TESTING) ${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_executables.py $) add_test(NAME game_patches COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_game_patches.py $) + if(Python3_VERSION VERSION_GREATER_EQUAL 3.11) + add_test(NAME patch_advisor_unit COMMAND ${Python3_EXECUTABLE} + ${CMAKE_SOURCE_DIR}/tools/tests/test_suggest_patches.py) + add_test(NAME patch_advisor_integration COMMAND ${Python3_EXECUTABLE} + ${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_patch_advisor.py $) + set_tests_properties(patch_advisor_integration PROPERTIES TIMEOUT 40) + endif() add_test(NAME sce_module_path COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_sce_module_path.py $) add_test(NAME linux_guest_stack COMMAND ${Python3_EXECUTABLE} @@ -634,6 +678,7 @@ if(BUILD_TESTING) ${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_guest_unused_import_dependencies.py $ $) set_property(TEST guest_unused_import_dependencies APPEND PROPERTY LABELS relinker) + add_asan_test_options(guest_unused_import_dependencies verify_asan_link_order=0) add_test(NAME guest_export_lookup COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_guest_export_lookup.py $) set_tests_properties(guest_export_lookup PROPERTIES TIMEOUT 30) @@ -643,6 +688,7 @@ if(BUILD_TESTING) add_asan_test_options(guest_module_args verify_asan_link_order=0) add_asan_test_options(guest_deferred_dependencies verify_asan_link_order=0) add_asan_test_options(guest_module_fini verify_asan_link_order=0) + add_asan_test_options(guest_unused_import_dependencies verify_asan_link_order=0) add_test(NAME guest_needed_nested_modules COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_guest_needed_nested_modules.py $) add_test(NAME guest_zero_initializer COMMAND ${Python3_EXECUTABLE} @@ -650,6 +696,10 @@ if(BUILD_TESTING) $ $) set_tests_properties(guest_zero_initializer PROPERTIES TIMEOUT 30) add_asan_test_options(guest_zero_initializer verify_asan_link_order=0) + add_test(NAME linux_guest_named_imports COMMAND ${Python3_EXECUTABLE} + ${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_linux_guest_named_imports.py + $ $) + add_asan_test_options(linux_guest_named_imports verify_asan_link_order=0) set(windows_stdio_host_arguments) if(WIN32) if(MINGW) diff --git a/core/relinker/cli/include/Cli.hpp b/core/relinker/cli/include/Cli.hpp index cb379408f0..2625b44aac 100644 --- a/core/relinker/cli/include/Cli.hpp +++ b/core/relinker/cli/include/Cli.hpp @@ -16,6 +16,7 @@ struct Args { bool skipSceModule = false; bool toIntel = false; bool toRosetta = false; + bool frameRedZone = false; bool writeRegistry = false; bool toWindows = false; bool toMacos = false; diff --git a/core/relinker/cli/src/CliArgs.cpp b/core/relinker/cli/src/CliArgs.cpp index 3fa915c6d2..47166d9972 100644 --- a/core/relinker/cli/src/CliArgs.cpp +++ b/core/relinker/cli/src/CliArgs.cpp @@ -13,7 +13,7 @@ namespace Cli { const char* Usage() { - return "Usage: relinker [--help] [--windows | --macos] [--windows-diagnostics] [--windows-gui] [--trace-imports] [--windows-fixed-base
] [--linux-fixed-base
] [--macos-fixed-base
] [--skip-syscall-check | --trap-sdk-syscall-stubs] [--skip-sce-module | --sce-module-path ] [--exclude-sce-module ]... [--module-dir ]... [--executable ]... [--to-intel] [--to-rosetta] [unused-filter=0|1|2] [--registry] [--rpath ] [--lazy-binding] [--autorun] [--patches | --no-patches] \n" + return "Usage: relinker [--help] [--windows | --macos] [--windows-diagnostics] [--windows-gui] [--trace-imports] [--windows-fixed-base
] [--linux-fixed-base
] [--macos-fixed-base
] [--skip-syscall-check | --trap-sdk-syscall-stubs] [--skip-sce-module | --sce-module-path ] [--exclude-sce-module ]... [--module-dir ]... [--executable ]... [--to-intel] [--to-rosetta] [--frame-red-zone] [unused-filter=0|1|2] [--registry] [--rpath ] [--lazy-binding] [--autorun] [--patches | --no-patches] \n" " relinker --verify-patches [--verify-output] [--skip-sce-module | --sce-module-path ] [--exclude-sce-module ]... [--module-dir ]... [--executable ]... \n" "Example: relinker input.elf output.elf"; } @@ -105,6 +105,8 @@ Args ParseArgs(int argc, char* argv[]) { args.executables.push_back(argv[++i]); } else if (arg == "--to-intel") { args.toIntel = true; + } else if (arg == "--frame-red-zone") { + args.frameRedZone = true; } else if (arg == "--to-rosetta") { args.toRosetta = true; args.toIntel = true; diff --git a/core/relinker/cli/tests/AutorunExitTests.cpp b/core/relinker/cli/tests/AutorunExitTests.cpp index c4883221a5..c773826ee9 100644 --- a/core/relinker/cli/tests/AutorunExitTests.cpp +++ b/core/relinker/cli/tests/AutorunExitTests.cpp @@ -9,9 +9,28 @@ #include #include #include +#ifdef _WIN32 +#include +#endif + +namespace { + +std::filesystem::path ExecutablePath(const char* argument) { +#ifdef _WIN32 + static_cast(argument); + std::vector filename(32768); + const auto size = GetModuleFileNameW(nullptr, filename.data(), static_cast(filename.size())); + if (size == 0 || size >= filename.size()) throw std::runtime_error("Cannot locate the autorun test executable"); + return std::filesystem::path(filename.data()); +#else + return std::filesystem::absolute(argument); +#endif +} + +} int main(int, char* argv[]) { - const auto executable = std::filesystem::absolute(argv[0]); + const auto executable = ExecutablePath(argv[0]); const auto name = executable.stem().string(); if (name.starts_with("anyps5-autorun-child-")) { if (name.find("signal") != std::string::npos) std::raise(SIGILL); diff --git a/core/relinker/codegen/include/codegen/CodegenTypes.hpp b/core/relinker/codegen/include/codegen/CodegenTypes.hpp index 021f2bf059..9b116f3657 100644 --- a/core/relinker/codegen/include/codegen/CodegenTypes.hpp +++ b/core/relinker/codegen/include/codegen/CodegenTypes.hpp @@ -76,6 +76,7 @@ struct DecodedInstructionInfo { bool IsTwoByteOpcode; std::uint8_t Opcode; std::uint8_t ModRmRegField; + std::size_t ModRmOffset = 0; }; } diff --git a/core/relinker/codegen/include/codegen/x86/Amd64OnlySubstitutionTable.hpp b/core/relinker/codegen/include/codegen/x86/Amd64OnlySubstitutionTable.hpp index c3df3388cc..8f71e35555 100644 --- a/core/relinker/codegen/include/codegen/x86/Amd64OnlySubstitutionTable.hpp +++ b/core/relinker/codegen/include/codegen/x86/Amd64OnlySubstitutionTable.hpp @@ -42,6 +42,7 @@ inline constexpr Entry kRdseed = {"RDSEED", nullptr, 0}; inline constexpr Entry kRdpid = {"RDPID", nullptr, 0}; inline constexpr Entry kRdtscp = {"RDTSCP", nullptr, 0}; inline constexpr Entry kClwb = {"CLWB", nullptr, 0}; +inline constexpr Entry kXsaveopt = {"XSAVEOPT", nullptr, 0}; inline constexpr Entry kWideStore = {"256-bit store", nullptr, 0}; inline constexpr Entry kScalarCompare = {"scalar compare", nullptr, 0}; inline constexpr Entry kExtrq = {"EXTRQ", nullptr, 0}; diff --git a/core/relinker/codegen/include/codegen/x86/DecodedInstruction.hpp b/core/relinker/codegen/include/codegen/x86/DecodedInstruction.hpp index 4176cc4cff..d1c86aff41 100644 --- a/core/relinker/codegen/include/codegen/x86/DecodedInstruction.hpp +++ b/core/relinker/codegen/include/codegen/x86/DecodedInstruction.hpp @@ -27,6 +27,7 @@ struct DecodedInstruction { [[nodiscard]] bool IsRdpid() const; [[nodiscard]] bool IsRdtscp() const; [[nodiscard]] bool IsClwb() const; + [[nodiscard]] bool IsXsaveopt() const; private: [[nodiscard]] std::size_t _skipPrefixesAndRex(bool* outHasOperandSizePrefix, bool* outHasRepnePrefix, bool* outHasRepPrefix) const; diff --git a/core/relinker/codegen/include/codegen/x86/IAmd64OnlyInstructionMatcher.hpp b/core/relinker/codegen/include/codegen/x86/IAmd64OnlyInstructionMatcher.hpp index 9986e79286..998c321b29 100644 --- a/core/relinker/codegen/include/codegen/x86/IAmd64OnlyInstructionMatcher.hpp +++ b/core/relinker/codegen/include/codegen/x86/IAmd64OnlyInstructionMatcher.hpp @@ -27,7 +27,8 @@ public: virtual void EmitOutOfLine( StubBodyBuilder& body, - std::span instruction + std::span instruction, + bool allowInPlace = false ) const = 0; }; diff --git a/core/relinker/codegen/include/codegen/x86/MaskedStoreLowering.hpp b/core/relinker/codegen/include/codegen/x86/MaskedStoreLowering.hpp new file mode 100644 index 0000000000..d03957a542 --- /dev/null +++ b/core/relinker/codegen/include/codegen/x86/MaskedStoreLowering.hpp @@ -0,0 +1,27 @@ +#ifndef CODEGEN_X86_MASKEDSTORELOWERING_HPP +#define CODEGEN_X86_MASKEDSTORELOWERING_HPP + +#include +#include +#include +#include +#include + +namespace Codegen { + +struct MaskedStoreOperands { + const char* Name; + std::uint8_t ElementSize; + bool Wide; + std::uint8_t Mask; + MemoryOperand Memory; + std::vector Instruction; + std::size_t DisplacementOffset; +}; + +[[nodiscard]] std::optional DecodeMaskedStore(const std::uint8_t* data, std::size_t length); +void EmitMaskedStore(StubBodyBuilder& body, const MaskedStoreOperands& operands); + +} + +#endif diff --git a/core/relinker/codegen/include/codegen/x86/RedZoneFramer.hpp b/core/relinker/codegen/include/codegen/x86/RedZoneFramer.hpp new file mode 100644 index 0000000000..a23fa23206 --- /dev/null +++ b/core/relinker/codegen/include/codegen/x86/RedZoneFramer.hpp @@ -0,0 +1,47 @@ +#ifndef CODEGEN_X86_REDZONEFRAMER_HPP +#define CODEGEN_X86_REDZONEFRAMER_HPP + +#include +#include +#include +#include +#include +#include +#include + +namespace Codegen { + +struct RedZoneFunction { + std::uint64_t Begin = 0; + std::uint64_t End = 0; +}; + +struct RedZoneFrameReport { + std::size_t Functions = 0; + std::size_t RedZoneFunctions = 0; + std::size_t Framed = 0; + std::size_t FramedWithoutFramePointer = 0; + std::size_t Exits = 0; + std::size_t MovedOperands = 0; + std::size_t OperandStubs = 0; + std::size_t StubbedOperands = 0; + std::map Skipped; +}; + +struct RedZoneDisplacement { + Domain::FileByteOffset Offset; + std::vector Bytes; +}; + +std::vector FrameRedZoneFunctions( + const std::vector& fileBytes, + const std::vector& codeSegments, + const std::vector& functions, + const std::vector& existing, + RedZoneFrameReport& report, + std::vector& displacements +); + +} + +#endif diff --git a/core/relinker/codegen/src/Amd64OnlyConverter.cpp b/core/relinker/codegen/src/Amd64OnlyConverter.cpp index 8f312031fe..de510c4224 100644 --- a/core/relinker/codegen/src/Amd64OnlyConverter.cpp +++ b/core/relinker/codegen/src/Amd64OnlyConverter.cpp @@ -56,6 +56,7 @@ public: ) : _seg(seg), _matches(matches), _ph(ph), _branchTargets(branchTargets), _pending(pending), _claimed(claimed), _matcher(matcher) {} [[nodiscard]] std::optional Window(const std::size_t index) const { + std::optional fallback; for (const bool avoidSegment : {true, false}) { if (index + 1 < _matches.size() && (_pending.contains(index + 1) || _movable(index + 1, true, avoidSegment))) { if (auto placement = _relocate(index, index, index + 1)) @@ -65,8 +66,10 @@ public: if (auto placement = _relocate(index, index - 1, index)) return placement; } + if (!fallback && index + 1 < _matches.size() && _pending.contains(index + 1)) + fallback = _relocate(index, index, index + 1, true); } - return std::nullopt; + return fallback; } [[nodiscard]] std::optional Island(const std::size_t index) { @@ -130,7 +133,7 @@ private: }; } - [[nodiscard]] std::optional _relocate(const std::size_t index, const std::size_t first, const std::size_t last) const { + [[nodiscard]] std::optional _relocate(const std::size_t index, const std::size_t first, const std::size_t last, const bool allowInPlace = false) const { if (_entered(_address(first), _end(last))) return std::nullopt; StubBodyBuilder body; @@ -138,7 +141,7 @@ private: try { for (auto k = first; k <= last; ++k) { if (_pending.contains(k)) { - _matcher.EmitOutOfLine(body, _bytes(k)); + _matcher.EmitOutOfLine(body, _bytes(k), allowInPlace); if (k != index) placement.Consumed.push_back(k); } else { diff --git a/core/relinker/codegen/src/x86/Amd64OnlyInstructionMatcher.cpp b/core/relinker/codegen/src/x86/Amd64OnlyInstructionMatcher.cpp index 739a3aea7f..065e2ad80e 100644 --- a/core/relinker/codegen/src/x86/Amd64OnlyInstructionMatcher.cpp +++ b/core/relinker/codegen/src/x86/Amd64OnlyInstructionMatcher.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -172,7 +173,8 @@ public: void EmitOutOfLine( StubBodyBuilder& body, - std::span instruction + std::span instruction, + bool allowInPlace = false ) const override; private: @@ -258,6 +260,11 @@ std::optional Amd64OnlyInstructionMatcher::_matchRosetta(const D } if (instr.IsClwb()) return _inPlace(kClwb, instr.Length, {}); + if (instr.IsXsaveopt()) { + std::vector replacement(instr.Data, instr.Data + instr.Length); + replacement[instr.OpcodeOffset() + 2] &= static_cast(~(2u << X64OpcodeConstants::ModRmRegShift)); + return Amd64OnlyMatch{kXsaveopt.Name, instr.Length, Amd64OnlyLowering::InPlace, std::move(replacement), {}, 0}; + } if (instr.IsRdpid()) return _trampoline(kRdpid.Name, instr.Length, _outOfLine(instr.Length, trailing, [&](StubBodyBuilder& body) { body.Raw(_rdpidReplacement(instr)); }), true); if (instr.IsRdtscp()) @@ -266,6 +273,8 @@ std::optional Amd64OnlyInstructionMatcher::_matchRosetta(const D return _trampoline(kWideStore.Name, instr.Length, _outOfLine(instr.Length, trailing, [&](StubBodyBuilder& body) { _wideStoreLowering.EmitOutOfLine(body, *wide); }), false, true); if (const auto compare = DecodeScalarCompare(instr.Data, instr.Length)) return _trampoline(kScalarCompare.Name, instr.Length, _outOfLine(instr.Length, trailing, [&](StubBodyBuilder& body) { _emitScalarCompare(body, instr, *compare); }), true); + if (const auto masked = DecodeMaskedStore(instr.Data, instr.Length)) + return _trampoline(masked->Name, instr.Length, _outOfLine(instr.Length, trailing, [&](StubBodyBuilder& body) { EmitMaskedStore(body, *masked); })); return std::nullopt; } @@ -325,6 +334,10 @@ const char* Amd64OnlyInstructionMatcher::_emitOutOfLine(StubBodyBuilder& body, c _emitScalarCompare(body, instr, *compare); return kScalarCompare.Name; } + if (const auto masked = _target == Amd64OnlyTarget::Rosetta ? DecodeMaskedStore(instr.Data, instr.Length) : std::nullopt) { + EmitMaskedStore(body, *masked); + return masked->Name; + } return nullptr; } @@ -355,8 +368,12 @@ const char* Amd64OnlyInstructionMatcher::_emitInPlace(StubBodyBuilder& body, con return nullptr; } -void Amd64OnlyInstructionMatcher::EmitOutOfLine(StubBodyBuilder& body, const std::span instruction) const { - if (_emitOutOfLine(body, DecodedInstruction{instruction.data(), instruction.size()}) == nullptr) +void Amd64OnlyInstructionMatcher::EmitOutOfLine(StubBodyBuilder& body, const std::span instruction, const bool allowInPlace) const { + const DecodedInstruction instr{instruction.data(), instruction.size()}; + const auto* emitted = _emitOutOfLine(body, instr); + if (emitted == nullptr && allowInPlace) + emitted = _emitInPlace(body, instr); + if (emitted == nullptr) throw CodegenException("AMD-only instruction has no out-of-line lowering"); body.Advance(instruction.size()); } diff --git a/core/relinker/codegen/src/x86/DecodedInstruction.cpp b/core/relinker/codegen/src/x86/DecodedInstruction.cpp index d54ce541bc..fa32c87812 100644 --- a/core/relinker/codegen/src/x86/DecodedInstruction.cpp +++ b/core/relinker/codegen/src/x86/DecodedInstruction.cpp @@ -324,6 +324,21 @@ bool DecodedInstruction::IsClwb() const { ((modrm >> ModRmModShift) & ModRmModMask) != ModRmModRegister && ((modrm >> ModRmRegShift) & ModRmRegMask) == 6; } +bool DecodedInstruction::IsXsaveopt() const { + bool operandSizeOverride = false; + bool repnePrefix = false; + bool repPrefix = false; + const std::size_t pos = _skipPrefixesAndRex(&operandSizeOverride, &repnePrefix, &repPrefix); + + if (operandSizeOverride || repnePrefix || repPrefix || pos + 2 >= Length) { + return false; + } + + const auto modrm = Data[pos + 2]; + return Data[pos] == TwoByteOpcodeEscape && Data[pos + 1] == TwoByteGrp15 && + ((modrm >> ModRmModShift) & ModRmModMask) != ModRmModRegister && ((modrm >> ModRmRegShift) & ModRmRegMask) == 6; +} + bool DecodedInstruction::IsMovntss() const { bool operandSizeOverride = false; bool repnePrefix = false; diff --git a/core/relinker/codegen/src/x86/MaskedStoreLowering.cpp b/core/relinker/codegen/src/x86/MaskedStoreLowering.cpp new file mode 100644 index 0000000000..642cee81ff --- /dev/null +++ b/core/relinker/codegen/src/x86/MaskedStoreLowering.cpp @@ -0,0 +1,102 @@ +#include +#include +#include +#include +#include + +namespace Codegen { + +namespace { + +constexpr std::int32_t kFrameSize = 128 + 3 * 8; +constexpr std::uint8_t kAddress = 0; +constexpr std::uint8_t kVexMap0F38 = 0x02; +constexpr std::uint8_t kVexPp66 = 0x01; + +bool _prefix(std::uint8_t value) { + return value == 0x2E || value == 0x36 || value == 0x3E || value == 0x26 || value == 0x64 || value == 0x65 || value == 0x67; +} + +void _append32(std::vector& bytes, std::int32_t value) { + for (unsigned shift = 0; shift < 32; shift += 8) + bytes.push_back(static_cast(static_cast(value) >> shift)); +} + +void _address(StubBodyBuilder& body, const MemoryOperand& memory) { + auto displacement = static_cast(memory.Displacement); + if (memory.StackBase) displacement += kFrameSize; + const bool address32 = std::find(memory.Prefixes.begin(), memory.Prefixes.end(), 0x67) != memory.Prefixes.end(); + if (!address32 && displacement > std::numeric_limits::max()) + throw CodegenException("Masked store stack displacement does not fit after spilling"); + const auto mod = memory.StackBase ? 2 : memory.Mod; + auto bytes = memory.Prefixes; + bytes.push_back(static_cast(0x48 | memory.RexIndexBase)); + bytes.push_back(0x8D); + bytes.push_back(static_cast((mod << 6) | (kAddress << 3) | memory.Rm)); + if (memory.Rm == 4) bytes.push_back(memory.Sib); + const auto offset = bytes.size(); + if (mod == 1) bytes.push_back(static_cast(displacement)); + if (mod == 2 || (mod == 0 && (memory.RipRelative || (memory.Rm == 4 && (memory.Sib & 7) == 5)))) + _append32(bytes, static_cast(displacement)); + if (memory.RipRelative) + body.RipRelative(bytes, offset, static_cast(memory.InstructionLength) + memory.Displacement); + else + body.Raw(bytes); +} + +} + +std::optional DecodeMaskedStore(const std::uint8_t* data, const std::size_t length) { + std::size_t vex = 0; + while (vex < length && _prefix(data[vex])) ++vex; + if (vex + 5 > length || data[vex] != 0xC4 || (data[vex + 1] & 0x1F) != kVexMap0F38 || (data[vex + 2] & 3) != kVexPp66 || (data[vex + 4] >> 6) == 3) + return std::nullopt; + const bool w = (data[vex + 2] & 0x80) != 0; + const char* name = nullptr; + std::uint8_t elementSize = 4; + if (data[vex + 3] == 0x2E && !w) { + name = "VMASKMOVPS"; + } else if (data[vex + 3] == 0x2F && !w) { + name = "VMASKMOVPD"; + elementSize = 8; + } else if (data[vex + 3] == 0x8E) { + name = w ? "VPMASKMOVQ" : "VPMASKMOVD"; + elementSize = w ? 8 : 4; + } else { + return std::nullopt; + } + const auto extensions = static_cast(~data[vex + 1]); + const auto rex = static_cast(((extensions & 0x20) >> 5) | ((extensions & 0x40) >> 5)); + auto memory = DecodeMemoryOperand(data, length, vex + 4, rex, {data, data + vex}); + if (vex + 4 + memory.EncodedSize != length) + throw CodegenException("Masked store memory operand has trailing bytes"); + const auto displacementOffset = vex + 5 + (memory.Rm == 4 ? 1 : 0); + return MaskedStoreOperands{name, elementSize, (data[vex + 2] & 4) != 0, static_cast((~data[vex + 2] >> 3) & 15), + std::move(memory), std::vector(data, data + length), displacementOffset}; +} + +void EmitMaskedStore(StubBodyBuilder& body, const MaskedStoreOperands& operands) { + const auto& memory = operands.Memory; + body.Raw(std::array{0x48, 0x8D, 0x64, 0x24, 0x80, 0x9C, 0x50, 0x51}); + _address(body, memory); + body.Raw(std::array{0xC4, static_cast(operands.Mask >= 8 ? 0xC1 : 0xE1), + static_cast(0x78 | (operands.Wide ? 4 : 0) | (operands.ElementSize == 8 ? 1 : 0)), 0x50, + static_cast(0xC8 | (operands.Mask & 7))}); + const auto elements = (operands.Wide ? 32u : 16u) / operands.ElementSize; + for (unsigned element = 0; element < elements; ++element) { + std::vector probes; + for (const auto offset : {element * operands.ElementSize, element * operands.ElementSize + operands.ElementSize - 1u}) { + probes.insert(probes.end(), memory.Prefixes.begin(), memory.Prefixes.end()); + probes.insert(probes.end(), {0xF0, 0x80, 0x48, static_cast(offset), 0x00}); + } + body.Raw(std::array{0xF6, 0xC1, static_cast(1u << element), 0x74, static_cast(probes.size())}); + body.Raw(probes); + } + body.Raw(std::array{0x59, 0x58, 0x9D, 0x48, 0x8D, 0xA4, 0x24, 0x80, 0x00, 0x00, 0x00}); + if (memory.RipRelative) + body.RipRelative(operands.Instruction, operands.DisplacementOffset, static_cast(memory.InstructionLength) + memory.Displacement); + else + body.Raw(operands.Instruction); +} + +} diff --git a/core/relinker/codegen/src/x86/RedZoneFramer.cpp b/core/relinker/codegen/src/x86/RedZoneFramer.cpp new file mode 100644 index 0000000000..cfe714faef --- /dev/null +++ b/core/relinker/codegen/src/x86/RedZoneFramer.cpp @@ -0,0 +1,441 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace Codegen { + +namespace { + +constexpr std::uint8_t kRegRsp = 4; +constexpr std::uint8_t kRegRbp = 5; +constexpr std::size_t kJumpBytes = 5; +constexpr std::int64_t kRedZoneBytes = 0x80; +constexpr std::int64_t kStackAlignment = 16; +constexpr std::uint8_t kJumpBack[] = {0xe9, 0x00, 0x00, 0x00, 0x00}; + +std::vector lowerRsp(const std::int64_t shift) { + return {0x48, 0x8d, 0x64, 0x24, static_cast(-shift)}; +} + +std::vector raiseRsp(const std::int64_t shift) { + const auto value = static_cast(shift); + return {0x48, 0x8d, 0xa4, 0x24, static_cast(value), static_cast(value >> 8), static_cast(value >> 16), static_cast(value >> 24)}; +} + +struct Insn { + std::size_t Offset; + std::size_t Length; + DecodedInstructionInfo Info; +}; + +std::optional pushedRegister(const std::uint8_t* code, const Insn& insn) { + if (insn.Length == 1 && code[0] >= 0x50 && code[0] <= 0x57 && code[0] != 0x50 + kRegRsp) return static_cast(code[0] - 0x50); + if (insn.Length == 2 && code[0] == 0x41 && code[1] >= 0x50 && code[1] <= 0x57) return static_cast(code[1] - 0x50 + 8); + return std::nullopt; +} + +std::optional poppedRegister(const std::uint8_t* code, const Insn& insn) { + if (insn.Length == 1 && code[0] >= 0x58 && code[0] <= 0x5f && code[0] != 0x58 + kRegRsp) return static_cast(code[0] - 0x58); + if (insn.Length == 2 && code[0] == 0x41 && code[1] >= 0x58 && code[1] <= 0x5f) return static_cast(code[1] - 0x58 + 8); + return std::nullopt; +} + +std::optional rspAdjustment(const std::uint8_t* code, const Insn& insn) { + std::int32_t value = 0; + if (insn.Length == 4 && code[0] == 0x48 && code[1] == 0x83 && (code[2] == 0xec || code[2] == 0xc4)) { + const std::int64_t amount = static_cast(code[3]); + return code[2] == 0xec ? -amount : amount; + } + if (insn.Length == 7 && code[0] == 0x48 && code[1] == 0x81 && (code[2] == 0xec || code[2] == 0xc4)) { + std::memcpy(&value, code + 3, 4); + return code[2] == 0xec ? -std::int64_t{value} : std::int64_t{value}; + } + if (insn.Length == 5 && std::memcmp(code, "\x48\x8d\x64\x24", 4) == 0) return static_cast(code[4]); + if (insn.Length == 8 && std::memcmp(code, "\x48\x8d\xa4\x24", 4) == 0) { + std::memcpy(&value, code + 4, 4); + return value; + } + return std::nullopt; +} + +struct MemoryUse { + bool UsesRsp = false; + bool RegisterRsp = false; + bool RbpBased = false; + std::int64_t RbpDisp = 0; + bool RspBased = false; + std::int64_t RspDisp = 0; + std::uint8_t Mod = 0; + std::size_t DispOffset = 0; + bool Indexed = false; + bool Address = false; +}; + +void extensionBits(const std::uint8_t* code, const Insn& insn, bool& r, bool& x, bool& b) { + const auto& info = insn.Info; + r = x = b = false; + if (info.RexPrefix != 0) { + r = (info.RexPrefix & 4) != 0; + x = (info.RexPrefix & 2) != 0; + b = (info.RexPrefix & 1) != 0; + return; + } + const auto lead = code[info.OpcodeOffset]; + if ((lead == 0xc4 || lead == 0x8f || lead == 0x62) && info.OpcodeOffset + 1 < insn.Length) { + r = (code[info.OpcodeOffset + 1] & 0x80) == 0; + x = (code[info.OpcodeOffset + 1] & 0x40) == 0; + b = (code[info.OpcodeOffset + 1] & 0x20) == 0; + } else if (lead == 0xc5 && info.OpcodeOffset + 1 < insn.Length) { + r = (code[info.OpcodeOffset + 1] & 0x80) == 0; + } +} + +bool twoByteRegisterIsGeneral(const std::uint8_t op) { + return (op >= 0x40 && op <= 0x4f) || op == 0xa3 || op == 0xa4 || op == 0xa5 || op == 0xab || op == 0xac || op == 0xad || op == 0xaf || op == 0xb0 || op == 0xb1 || op == 0xb3 || op == 0xb6 || op == 0xb7 || op == 0xb8 || (op >= 0xbb && op <= 0xbf) || op == 0xc0 || op == 0xc1 || op == 0xc3; +} + +bool twoByteRmIsGeneral(const std::uint8_t op) { + return twoByteRegisterIsGeneral(op) || op == 0x6e || op == 0x7e || op == 0xba; +} + +bool vexGeneralUsesRsp(const std::uint8_t* code, const Insn& insn, const std::uint8_t mod, const std::uint8_t reg, const std::uint8_t rm, const bool r, const bool b) { + const auto at = insn.Info.OpcodeOffset; + const auto lead = code[at]; + if ((lead != 0xc4 && lead != 0xc5) || at + 3 >= insn.Length) return false; + const auto map = lead == 0xc5 ? std::uint8_t{1} : static_cast(code[at + 1] & 0x1f); + const auto vvvv = static_cast((~code[lead == 0xc5 ? at + 1 : at + 2] >> 3) & 0xf); + const auto op = code[lead == 0xc5 ? at + 2 : at + 3]; + const bool general = (map == 2 && (op == 0xf2 || op == 0xf3 || op == 0xf5 || op == 0xf6 || op == 0xf7)) || (map == 3 && op == 0xf0); + return general && ((reg == kRegRsp && !r) || (mod == 3 && rm == kRegRsp && !b) || vvvv == kRegRsp); +} + +MemoryUse memoryUse(const std::uint8_t* code, const Insn& insn) { + MemoryUse use; + const auto& info = insn.Info; + if (!info.HasModRm || info.ModRmOffset == 0 || info.ModRmOffset >= insn.Length) return use; + bool r = false; + bool x = false; + bool b = false; + extensionBits(code, insn, r, x, b); + const auto modrm = info.ModRmByte; + const auto mod = static_cast(modrm >> 6); + const auto reg = static_cast((modrm >> 3) & 7); + const auto rm = static_cast(modrm & 7); + const bool vex = info.RexPrefix == 0 && (code[info.OpcodeOffset] == 0xc4 || code[info.OpcodeOffset] == 0xc5 || code[info.OpcodeOffset] == 0x62 || code[info.OpcodeOffset] == 0x8f); + if (!vex && !info.IsTwoByteOpcode && reg == kRegRsp && !r) { + const auto op = info.Opcode; + if ((op <= 0x3f && (op & 7) <= 3) || (op >= 0x84 && op <= 0x8b) || op == 0x8d) use.UsesRsp = use.RegisterRsp = true; + } + if (!vex && info.IsTwoByteOpcode && ((reg == kRegRsp && !r && twoByteRegisterIsGeneral(info.Opcode)) || (mod == 3 && rm == kRegRsp && !b && twoByteRmIsGeneral(info.Opcode)))) use.UsesRsp = use.RegisterRsp = true; + if (vex && vexGeneralUsesRsp(code, insn, mod, reg, rm, r, b)) use.UsesRsp = use.RegisterRsp = true; + if (mod == 3) { + const bool generalPurpose = !vex && !info.IsTwoByteOpcode && !(info.Opcode >= 0xd8 && info.Opcode <= 0xdf); + if (generalPurpose && rm == kRegRsp && !b) use.UsesRsp = use.RegisterRsp = true; + return use; + } + std::size_t at = info.ModRmOffset + 1; + std::uint8_t base = rm; + bool baseExtended = b; + bool indexed = false; + if (rm == 4) { + if (at >= insn.Length) return use; + const auto sib = code[at++]; + base = sib & 7; + indexed = ((sib >> 3) & 7) != 4 || x; + if (mod == 0 && base == 5) return use; + } else if (mod == 0 && rm == 5) { + return use; + } + std::int64_t disp = 0; + if (mod == 1 && at < insn.Length) disp = static_cast(code[at]); + if (mod == 2 && at + 4 <= insn.Length) { + std::int32_t value = 0; + std::memcpy(&value, code + at, 4); + disp = value; + } + if (base == kRegRsp && !baseExtended) { + use.UsesRsp = true; + use.RspBased = true; + use.RspDisp = disp; + use.Mod = mod; + use.DispOffset = at; + use.Indexed = indexed; + use.Address = !vex && !info.IsTwoByteOpcode && info.Opcode == 0x8d; + } + if (base == kRegRbp && !baseExtended && mod != 0) { + use.RbpBased = true; + use.RbpDisp = disp; + } + return use; +} + +std::optional> movedDisplacement(const MemoryUse& use, const std::int64_t shift) { + const auto moved = use.RspDisp + shift; + if (use.Mod == 1 && moved <= std::numeric_limits::max()) return std::vector{static_cast(moved)}; + if (use.Mod == 2 && moved <= std::numeric_limits::max()) { + const auto value = static_cast(static_cast(moved)); + return std::vector{static_cast(value), static_cast(value >> 8), static_cast(value >> 16), static_cast(value >> 24)}; + } + return std::nullopt; +} + +std::optional> withDisplacement(const std::uint8_t* code, const Insn& insn, const MemoryUse& use, const std::int64_t disp) { + if (disp < std::numeric_limits::min() || disp > std::numeric_limits::max()) return std::nullopt; + const std::size_t oldBytes = use.Mod == 1 ? 1 : use.Mod == 2 ? 4 : 0; + if (use.DispOffset + oldBytes > insn.Length) return std::nullopt; + const bool narrow = disp >= std::numeric_limits::min() && disp <= std::numeric_limits::max(); + std::vector bytes(code, code + use.DispOffset); + bytes[insn.Info.ModRmOffset] = static_cast((bytes[insn.Info.ModRmOffset] & 0x3f) | (narrow ? 0x40 : 0x80)); + const auto value = static_cast(static_cast(disp)); + for (std::size_t index = 0; index < (narrow ? 1u : 4u); ++index) bytes.push_back(static_cast(value >> (8 * index))); + bytes.insert(bytes.end(), code + use.DispOffset + oldBytes, code + insn.Length); + return bytes; +} + +struct OperandStub { + std::size_t First; + std::size_t Last; + std::size_t Begin; + std::size_t End; + std::vector Body; + std::size_t Operands = 0; +}; + +void frameSegment( + const std::vector& fileBytes, + const Domain::ProgramHeader& ph, + const std::vector& functions, + const std::vector& existing, + std::vector& sites, + std::vector& displacements, + RedZoneFrameReport& report +) { + const auto segOffset = static_cast(ph.Offset); + const auto segSize = static_cast(ph.FileSize); + if (segOffset > fileBytes.size() || segSize > fileBytes.size() - segOffset) return; + const std::vector seg(fileBytes.begin() + static_cast(segOffset), fileBytes.begin() + static_cast(segOffset + segSize)); + const auto scanner = MakeInstructionScanner(); + const X64InstructionDecoder decoder; + + std::set targets; + for (const auto& match : scanner->ScanCodeSection(seg, 0, seg.size())) { + const auto info = decoder.DecodeInstruction(seg.data() + match.Offset, match.Length); + if (!info.HasBranchTarget || info.HasRipRelativeDisp || info.FlowKind == ControlFlowKind::Call) continue; + const auto target = static_cast(match.Offset + match.Length) + info.BranchDisp; + if (target >= 0 && static_cast(target) < seg.size()) targets.insert(static_cast(target)); + } + std::vector> taken; + for (const auto& site : existing) { + if (site.Address < ph.MappedAddress || site.Address >= ph.MappedAddress + segSize) continue; + const auto at = static_cast(site.Address - ph.MappedAddress); + taken.emplace_back(at, at + site.Length); + } + const auto overlapsExisting = [&](const std::size_t begin, const std::size_t end) { + return std::any_of(taken.begin(), taken.end(), [&](const auto& range) { return begin < range.second && range.first < end; }); + }; + const auto targetInside = [&](const std::size_t begin, const std::size_t end) { + const auto hit = targets.upper_bound(begin); + return hit != targets.end() && *hit < end; + }; + for (const auto& function : functions) { + if (function.Begin < ph.MappedAddress || function.End <= function.Begin || function.End - ph.MappedAddress > segSize) continue; + const auto start = static_cast(function.Begin - ph.MappedAddress); + const auto limit = static_cast(function.End - ph.MappedAddress); + ++report.Functions; + const auto skip = [&](const char* reason) { ++report.Skipped[reason]; }; + + const std::vector code(seg.begin() + static_cast(start), seg.begin() + static_cast(limit)); + std::vector body; + for (const auto& match : scanner->ScanCodeSection(code, 0, code.size())) { + if (code[match.Offset] == 0xcc && match.Length == 1) break; + body.push_back({start + static_cast(match.Offset), match.Length, decoder.DecodeInstruction(code.data() + match.Offset, match.Length)}); + } + if (body.size() < 3) continue; + const auto at = [&](const std::size_t k) { return seg.data() + body[k].Offset; }; + const bool rbpPrologue = body[0].Length == 1 && at(0)[0] == 0x55 && body[1].Length == 3 && std::memcmp(at(1), "\x48\x89\xe5", 3) == 0; + std::vector pushed; + std::size_t p = rbpPrologue ? 2 : 0; + while (p < body.size()) + if (const auto reg = pushedRegister(at(p), body[p])) { pushed.push_back(*reg); ++p; } else break; + std::int64_t allocation = 0; + if (!rbpPrologue && p < body.size()) + if (const auto adjustment = rspAdjustment(at(p), body[p]); adjustment && *adjustment < 0) { allocation = -*adjustment; ++p; } + std::vector expected(pushed.begin(), pushed.end()); + if (rbpPrologue) expected.insert(expected.begin(), kRegRbp); + const auto popsBefore = [&](const std::size_t k) -> std::optional { + std::size_t q = k; + std::vector popped; + while (q > p && popped.size() < expected.size()) + if (const auto reg = poppedRegister(at(q - 1), body[q - 1])) { popped.push_back(*reg); --q; } else break; + if (popped != expected) return std::nullopt; + return q; + }; + std::set frees; + if (allocation > 0) + for (std::size_t k = p; k < body.size(); ++k) + if (body[k].Info.FlowKind == ControlFlowKind::Return) + if (const auto q = popsBefore(k); q && *q > p && rspAdjustment(at(*q - 1), body[*q - 1]) == allocation) frees.insert(*q - 1); + + bool leaf = true; + bool redZone = false; + const char* reason = nullptr; + std::vector> exits; + std::map>> moves; + std::vector stubbed; + const auto functionEnd = limit; + std::int64_t depth = 0; + bool wholeRedZone = rbpPrologue; + for (std::size_t k = p; k < body.size(); ++k) { + const auto use = memoryUse(at(k), body[k]); + if (!use.RspBased) continue; + if (use.RspDisp < 0) depth = std::max(depth, -use.RspDisp); + if (use.Indexed || (use.Address && use.RspDisp < 0)) wholeRedZone = true; + } + const auto shift = wholeRedZone ? kRedZoneBytes : std::min(kRedZoneBytes, (depth + kStackAlignment - 1) / kStackAlignment * kStackAlignment); + for (std::size_t k = p; k < body.size(); ++k) { + const auto& insn = body[k]; + const auto* code = at(k); + const auto flow = insn.Info.FlowKind; + if (flow == ControlFlowKind::Call || flow == ControlFlowKind::IndirectCall) { leaf = false; break; } + if (frees.contains(k)) continue; + const auto use = memoryUse(code, insn); + if (use.RbpBased && rbpPrologue && use.RbpDisp < -static_cast(8 * pushed.size())) redZone = true; + if (use.RspBased && use.RspDisp < 0) redZone = true; + if (reason != nullptr) continue; + if (code[insn.Info.OpcodeOffset] == 0xc9 || code[insn.Info.OpcodeOffset] == 0xc8) { reason = "leave or enter"; continue; } + if (rbpPrologue) { + if (use.UsesRsp) { reason = "other use of rsp"; continue; } + } else { + if (use.RegisterRsp) { reason = "other use of rsp"; continue; } + if (use.RspBased) { + if (code[insn.Info.OpcodeOffset] == 0x62) { reason = "rsp operand that cannot move with rsp"; continue; } + if (overlapsExisting(insn.Offset, insn.Offset + insn.Length)) { reason = "overlaps another stub"; continue; } + if (auto moved = movedDisplacement(use, shift)) moves.emplace(k, std::pair{insn.Offset + use.DispOffset, std::move(*moved)}); + else stubbed.push_back(k); + } + } + if (flow == ControlFlowKind::IndirectJump) { reason = "indirect jump"; continue; } + if ((flow == ControlFlowKind::UnconditionalJump || flow == ControlFlowKind::ConditionalBranch) && insn.Info.HasBranchTarget) { + const auto target = static_cast(insn.Offset + insn.Length) + insn.Info.BranchDisp; + if (insn.Info.HasRipRelativeDisp || target < static_cast(start) || target >= static_cast(functionEnd)) { reason = "branch leaves the function"; continue; } + } + if (flow == ControlFlowKind::Return) { + if (insn.Length != 1 || code[0] != 0xc3) { reason = "ret with an immediate"; continue; } + const auto pops = popsBefore(k); + if (!pops) { reason = "exit does not pop the prologue's registers"; continue; } + auto q = *pops; + if (allocation > 0) { + if (q == p || !frees.contains(q - 1)) { reason = "exit does not free the prologue's allocation"; continue; } + --q; + } + const auto siteStart = body[q].Offset; + const auto siteEnd = insn.Offset + 1; + if (siteEnd - siteStart < kJumpBytes) { reason = "exit shorter than a jump"; continue; } + if (targetInside(siteStart, siteEnd)) { reason = "branch into an exit"; continue; } + exits.emplace_back(siteStart, siteEnd); + } + } + if (!leaf || !redZone) continue; + ++report.RedZoneFunctions; + const auto prologueEnd = p > 0 ? body[p - 1].Offset + body[p - 1].Length : start; + if (reason == nullptr && ((pushed.empty() && allocation == 0) || prologueEnd - start < kJumpBytes)) reason = "prologue shorter than a jump"; + if (reason == nullptr && targetInside(start, prologueEnd)) reason = "branch into the prologue"; + if (reason == nullptr && exits.empty()) reason = "no ret exit"; + if (reason == nullptr && (overlapsExisting(start, prologueEnd) || std::any_of(exits.begin(), exits.end(), [&](const auto& exit) { return overlapsExisting(exit.first, exit.second); }))) reason = "overlaps another stub"; + std::vector stubs; + const auto overlapsSite = [&](const std::size_t begin, const std::size_t end) { + if (begin < prologueEnd && start < end) return true; + if (std::any_of(exits.begin(), exits.end(), [&](const auto& exit) { return begin < exit.second && exit.first < end; })) return true; + return std::any_of(stubs.begin(), stubs.end(), [&](const OperandStub& stub) { return begin < stub.End && stub.Begin < end; }); + }; + for (const auto k : stubbed) { + if (reason != nullptr) break; + if (!stubs.empty() && k < stubs.back().Last) continue; + OperandStub stub{k, k, body[k].Offset, body[k].Offset, {}}; + while (stub.Last < body.size() && stub.End - stub.Begin < kJumpBytes) { + const auto& covered = body[stub.Last]; + if (covered.Offset != stub.End || covered.Info.FlowKind != ControlFlowKind::Sequential || covered.Info.HasRipRelativeDisp || frees.contains(stub.Last)) break; + const auto use = memoryUse(at(stub.Last), covered); + const auto bytes = use.RspBased ? withDisplacement(at(stub.Last), covered, use, use.RspDisp + shift) : std::vector(at(stub.Last), at(stub.Last) + covered.Length); + if (!bytes) break; + stub.Body.insert(stub.Body.end(), bytes->begin(), bytes->end()); + if (use.RspBased) ++stub.Operands; + stub.End += covered.Length; + ++stub.Last; + } + while (stub.End - stub.Begin < kJumpBytes && stub.First > p) { + const auto j = stub.First - 1; + const auto& prior = body[j]; + if (prior.Offset + prior.Length != stub.Begin || prior.Info.FlowKind != ControlFlowKind::Sequential || prior.Info.HasRipRelativeDisp || frees.contains(j)) break; + const auto use = memoryUse(at(j), prior); + const auto bytes = use.RspBased ? withDisplacement(at(j), prior, use, use.RspDisp + shift) : std::vector(at(j), at(j) + prior.Length); + if (!bytes) break; + stub.Body.insert(stub.Body.begin(), bytes->begin(), bytes->end()); + if (use.RspBased) ++stub.Operands; + stub.Begin = prior.Offset; + stub.First = j; + } + if (stub.End - stub.Begin < kJumpBytes || targetInside(stub.Begin, stub.End) || overlapsSite(stub.Begin, stub.End) || overlapsExisting(stub.Begin, stub.End)) reason = "rsp operand that cannot move and has no room for a stub"; + else stubs.push_back(std::move(stub)); + } + if (reason != nullptr) { skip(reason); continue; } + for (const auto& stub : stubs) + for (auto k = stub.First; k < stub.Last; ++k) moves.erase(k); + + const auto addSite = [&](const std::size_t begin, const std::size_t end, std::vector code) { + TrampolineSite site; + site.Offset = ph.Offset + begin; + site.Address = ph.MappedAddress + begin; + site.Length = end - begin; + site.OriginalBytes.assign(seg.begin() + static_cast(begin), seg.begin() + static_cast(end)); + site.ReturnBranchOffset = code.size(); + code.insert(code.end(), std::begin(kJumpBack), std::end(kJumpBack)); + site.Body = std::move(code); + taken.emplace_back(begin, end); + sites.push_back(std::move(site)); + }; + const auto original = [&](const std::size_t begin, const std::size_t end) { return std::vector(seg.begin() + static_cast(begin), seg.begin() + static_cast(end)); }; + auto entry = original(start, prologueEnd); + const auto lower = lowerRsp(shift); + entry.insert(entry.end(), lower.begin(), lower.end()); + addSite(start, prologueEnd, std::move(entry)); + for (const auto& exit : exits) { + auto code = raiseRsp(shift); + const auto tail = original(exit.first, exit.second); + code.insert(code.end(), tail.begin(), tail.end()); + addSite(exit.first, exit.second, std::move(code)); + } + for (auto& stub : stubs) addSite(stub.Begin, stub.End, std::move(stub.Body)); + for (auto& [k, move] : moves) displacements.push_back({ph.Offset + move.first, std::move(move.second)}); + ++report.Framed; + if (!rbpPrologue) ++report.FramedWithoutFramePointer; + report.Exits += exits.size(); + report.MovedOperands += moves.size(); + report.OperandStubs += stubs.size(); + for (const auto& stub : stubs) report.StubbedOperands += stub.Operands; + } +} + +} + +std::vector FrameRedZoneFunctions( + const std::vector& fileBytes, + const std::vector& codeSegments, + const std::vector& functions, + const std::vector& existing, + RedZoneFrameReport& report, + std::vector& displacements +) { + std::vector sites; + for (const auto& ph : codeSegments) frameSegment(fileBytes, ph, functions, existing, sites, displacements, report); + return sites; +} + +} diff --git a/core/relinker/codegen/src/x86/X64InstructionDecoder.cpp b/core/relinker/codegen/src/x86/X64InstructionDecoder.cpp index b22c3f8726..0c2f5571c9 100644 --- a/core/relinker/codegen/src/x86/X64InstructionDecoder.cpp +++ b/core/relinker/codegen/src/x86/X64InstructionDecoder.cpp @@ -393,6 +393,7 @@ DecodedInstructionInfo X64InstructionDecoder::DecodeInstruction( const std::uint8_t modrm = data[modrmPos]; info.HasModRm = true; info.ModRmByte = modrm; + info.ModRmOffset = modrmPos; info.ModRmRegField = (modrm >> ModRmRegShift) & ModRmRegMask; const std::uint8_t mod = registerOnly ? ModRmModRegister : (modrm >> ModRmModShift) & ModRmModMask; const std::uint8_t rm = modrm & ModRmRmMask; @@ -489,6 +490,7 @@ DecodedInstructionInfo X64InstructionDecoder::DecodeInstruction( std::uint8_t modrm = data[pos]; info.HasModRm = true; info.ModRmByte = modrm; + info.ModRmOffset = pos; std::uint8_t reg = (modrm >> ModRmRegShift) & ModRmRegMask; info.ModRmRegField = reg; std::uint8_t mod = (modrm >> ModRmModShift) & ModRmModMask; diff --git a/core/relinker/codegen/src/x86/X64InstructionRewriter.cpp b/core/relinker/codegen/src/x86/X64InstructionRewriter.cpp index 212bdaee22..82990dd9ac 100644 --- a/core/relinker/codegen/src/x86/X64InstructionRewriter.cpp +++ b/core/relinker/codegen/src/x86/X64InstructionRewriter.cpp @@ -96,6 +96,66 @@ RewriteResult X64InstructionRewriter::Rewrite( const auto cutStart = static_cast(offset); const std::int64_t cutEnd = cutStart + oldLength; + if (delta != 0) { + const auto sectionSize = static_cast(codeSection.size()); + + for (const auto& match : matches) { + const auto matchOffset = static_cast(match.Offset); + if (matchOffset >= cutStart && matchOffset < cutEnd) + continue; + + const auto info = decoder.DecodeInstruction( + codeSection.data() + match.Offset, match.Length); + + if (!info.TruncatesBranchTarget) + continue; + + const auto opcodeLength = info.IsTwoByteOpcode ? 2u : 1u; + const auto displacementSize = + info.Length - info.OpcodeOffset - opcodeLength; + const auto displacementOffset = + match.Offset + info.Length - displacementSize; + + std::int64_t branchDisplacement = 0; + + if (displacementSize == 1) { + branchDisplacement = + static_cast(codeSection[displacementOffset]); + } else if (displacementSize == 2) { + const auto raw = static_cast( + static_cast(codeSection[displacementOffset]) | + (static_cast( + codeSection[displacementOffset + 1]) << 8)); + branchDisplacement = static_cast(raw); + } else { + throw CodegenException( + "Unexpected displacement size for truncated relative branch", + match.Offset); + } + + const auto rawTargetPosition = + matchOffset + static_cast(info.Length) + + branchDisplacement; + if (rawTargetPosition < 0 || rawTargetPosition > 0xFFFF) { + throw CodegenException( + "Cannot change code length while a truncated relative branch target wraps at 16 bits", + match.Offset); + } + const auto targetPosition = rawTargetPosition; + + const bool instrShifts = + matchOffset >= cutEnd && matchOffset < sectionSize; + const bool targetShifts = + targetPosition >= cutEnd && targetPosition < sectionSize; + + if (instrShifts != targetShifts) { + throw CodegenException( + "Cannot change code length while a truncated relative branch moves", + match.Offset); + } + } + } + std::vector rewritten; rewritten.reserve(codeSection.size() + static_cast(delta > 0 ? delta : 0)); rewritten.insert(rewritten.end(), codeSection.begin(), codeSection.begin() + offset); diff --git a/core/relinker/codegen/tests/Amd64OnlyConverterTests.cpp b/core/relinker/codegen/tests/Amd64OnlyConverterTests.cpp index 8f26dba82d..ca2cf46715 100644 --- a/core/relinker/codegen/tests/Amd64OnlyConverterTests.cpp +++ b/core/relinker/codegen/tests/Amd64OnlyConverterTests.cpp @@ -690,8 +690,8 @@ void rosettaSubstitutions() { const auto intel = Codegen::MakeAmd64OnlyInstructionMatcher(); const auto rosetta = Codegen::MakeAmd64OnlyInstructionMatcher(Codegen::Amd64OnlyTarget::Rosetta); const auto match = [](const auto& matcher, const Bytes& bytes) { return matcher->Match(bytes.data(), bytes.size()); }; - for (const auto& bytes : {Bytes{0x0F, 0xC7, 0xF8}, Bytes{0xF3, 0x0F, 0xC7, 0xF8}, Bytes{0x66, 0x0F, 0xAE, 0x30}, Bytes{0x0F, 0x01, 0xF9}}) - require(!match(intel, bytes).has_value(), "RDSEED, RDPID, CLWB or RDTSCP was converted for an Intel host"); + for (const auto& bytes : {Bytes{0x0F, 0xC7, 0xF8}, Bytes{0xF3, 0x0F, 0xC7, 0xF8}, Bytes{0x66, 0x0F, 0xAE, 0x30}, Bytes{0x0F, 0x01, 0xF9}, Bytes{0x0F, 0xAE, 0x37}}) + require(!match(intel, bytes).has_value(), "RDSEED, RDPID, CLWB, RDTSCP or XSAVEOPT was converted for an Intel host"); const auto rdseed = match(rosetta, {0x48, 0x0F, 0xC7, 0xF9}); require(rdseed && rdseed->Lowering == Amd64OnlyLowering::InPlace && rdseed->ReplacementBytes == Bytes{0x48, 0x0F, 0xC7, 0xF1} && rdseed->InstructionName == "RDSEED", "RDSEED was not rewritten to RDRAND"); const auto rdseed16 = match(rosetta, {0x66, 0x41, 0x0F, 0xC7, 0xFA}); @@ -701,6 +701,18 @@ void rosettaSubstitutions() { require(clwb && clwb->Lowering == Amd64OnlyLowering::InPlace && clwb->ReplacementBytes == Bytes{0x66, 0x0F, 0x1F, 0x44, 0x00, 0x00} && clwb->InstructionName == "CLWB", "CLWB was not replaced by a NOP"); require(!match(rosetta, {0x66, 0x0F, 0xAE, 0xF1}).has_value(), "TPAUSE (the register form of 66 0F AE /6) was taken for CLWB"); require(!match(rosetta, {0x66, 0x0F, 0xAE, 0x38}).has_value(), "CLFLUSHOPT was taken for CLWB"); + const auto xsaveopt = match(rosetta, {0x0F, 0xAE, 0x37}); + require(xsaveopt && xsaveopt->Lowering == Amd64OnlyLowering::InPlace && xsaveopt->ReplacementBytes == Bytes{0x0F, 0xAE, 0x27} && xsaveopt->InstructionName == "XSAVEOPT", "XSAVEOPT was not rewritten to XSAVE"); + const auto xsaveopt64 = match(rosetta, {0x48, 0x0F, 0xAE, 0x74, 0x24, 0x40}); + require(xsaveopt64 && xsaveopt64->ReplacementBytes == Bytes{0x48, 0x0F, 0xAE, 0x64, 0x24, 0x40}, "XSAVEOPT64 with an RSP-relative operand was not rewritten to XSAVE64"); + const auto xsaveoptR12 = match(rosetta, {0x41, 0x0F, 0xAE, 0x34, 0x24}); + require(xsaveoptR12 && xsaveoptR12->ReplacementBytes == Bytes{0x41, 0x0F, 0xAE, 0x24, 0x24}, "XSAVEOPT with an extended base register was not rewritten to XSAVE"); + const auto xsaveoptRip = match(rosetta, {0x0F, 0xAE, 0x35, 0x00, 0x01, 0x00, 0x00}); + require(xsaveoptRip && xsaveoptRip->ReplacementBytes == Bytes{0x0F, 0xAE, 0x25, 0x00, 0x01, 0x00, 0x00}, "RIP-relative XSAVEOPT was not rewritten to XSAVE with the same displacement"); + const auto xsaveoptFs = match(rosetta, {0x64, 0x0F, 0xAE, 0x34, 0x25, 0x40, 0x00, 0x00, 0x00}); + require(xsaveoptFs && xsaveoptFs->ReplacementBytes == Bytes{0x64, 0x0F, 0xAE, 0x24, 0x25, 0x40, 0x00, 0x00, 0x00}, "FS-relative XSAVEOPT was not rewritten to XSAVE"); + for (const auto& other : {Bytes{0x0F, 0xAE, 0x27}, Bytes{0x0F, 0xAE, 0x2F}, Bytes{0x0F, 0xAE, 0x3F}, Bytes{0x0F, 0xAE, 0xF0}, Bytes{0xF3, 0x0F, 0xAE, 0xF0}, Bytes{0xF2, 0x0F, 0xAE, 0xF1}, Bytes{0xF3, 0x0F, 0xAE, 0x37}}) + require(!match(rosetta, other).has_value(), "XSAVE, XRSTOR, CLFLUSH, MFENCE, UMONITOR, UMWAIT or a prefixed 0F AE /6 was taken for XSAVEOPT"); const auto rdpid = match(rosetta, {0xF3, 0x41, 0x0F, 0xC7, 0xFB}); require(rdpid && rdpid->Lowering == Amd64OnlyLowering::Trampoline && rdpid->Optional && rdpid->InstructionName == "RDPID", "RDPID was not lowered through an optional stub"); require(rdpid->ReturnBranchOffset == 6 && Bytes(rdpid->StubBody.begin(), rdpid->StubBody.begin() + 7) == Bytes{0x41, 0xBB, 0, 0, 0, 0, 0xE9}, "RDPID r11 stub does not load 0 before returning"); @@ -837,6 +849,21 @@ void converterWideStoreWindows() { expected.insert(expected.end(), {0xC5, 0xF8, 0x11, 0x0E, 0xC4, 0xE3, 0x7D, 0x19, 0x4E, 0x10, 0x01}); require(paired.Trampolines.size() == 1 && paired.Trampolines[0].Length == 8 && body(paired.Trampolines[0]) == expected && paired.Reports.size() == 1, "A 256-bit store followed by another was not lowered with it in one stub"); + const auto withRdseed = convert(withStore({}, {0x0F, 0xC7, 0xF8, 0xC3})); + expected = halves; + expected.insert(expected.end(), {0x0F, 0xC7, 0xF0}); + require(withRdseed.Trampolines.size() == 1 && withRdseed.Trampolines[0].Length == 7 && body(withRdseed.Trampolines[0]) == expected && withRdseed.Reports.size() == 1, "A 256-bit store followed by an in-place site did not move its replacement into the stub"); + + const auto withClwb = convert(withStore({}, {0x66, 0x0F, 0xAE, 0x37, 0xC3})); + expected = halves; + expected.insert(expected.end(), {0x0F, 0x1F, 0x40, 0x00}); + require(withClwb.Trampolines.size() == 1 && withClwb.Trampolines[0].Length == 8 && body(withClwb.Trampolines[0]) == expected, "A 256-bit store followed by CLWB did not move its replacement into the stub"); + + const auto withRipStore = convert(withStore({}, {0xF3, 0x0F, 0x2B, 0x05, 0x40, 0x00, 0x00, 0x00, 0xC3})); + expected = halves; + expected.insert(expected.end(), {0xF3, 0x0F, 0x11, 0x05, 0x00, 0x00, 0x00, 0x00}); + require(withRipStore.Trampolines.size() == 1 && withRipStore.Trampolines[0].Length == 12 && body(withRipStore.Trampolines[0]) == expected && withRipStore.Trampolines[0].Relocations.size() == 1 && withRipStore.Trampolines[0].Relocations[0].SiteTarget == 0x4C && withRipStore.Trampolines[0].Relocations[0].InstructionEnd == 19, "A 256-bit store followed by a RIP-relative in-place site did not relocate its replacement"); + const Bytes push = {0x48, 0x8D, 0x64, 0x24, 0xF8, 0x50, 0x48, 0x8D, 0x05, 0, 0, 0, 0, 0x48, 0x89, 0x44, 0x24, 0x08, 0x58}; const auto ripCalled = convert(withStore({}, {0xFF, 0x15, 0x10, 0x00, 0x00, 0x00})); expected = halves; @@ -1993,6 +2020,45 @@ void wideStoreExecution() { munmap(pages, 8192); munmap(code, 4096); } + +void xsaveoptExecution() { + const Bytes site = {0x48, 0x0F, 0xAE, 0x37}; + const auto match = Codegen::MakeAmd64OnlyInstructionMatcher(Codegen::Amd64OnlyTarget::Rosetta)->Match(site.data(), site.size()); + require(match && match->Lowering == Codegen::Amd64OnlyLowering::InPlace, "XSAVEOPT64 was not rewritten in place"); + auto body = match->ReplacementBytes; + body.push_back(0xC3); + void* code = mmap(nullptr, 4096, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + require(code != MAP_FAILED, "cannot map executable memory for the XSAVEOPT replacement"); + std::memcpy(code, body.data(), body.size()); + alignas(64) std::uint8_t area[1024] = {}; + alignas(32) std::uint8_t pattern[32]; + alignas(32) std::uint8_t restored[32] = {}; + for (std::size_t i = 0; i < sizeof(pattern); ++i) + pattern[i] = static_cast(0xA0 + i); + asm volatile( + "vmovdqu (%[pattern]), %%ymm3\n\t" + "mov %[area], %%rdi\n\t" + "mov $7, %%eax\n\t" + "xor %%edx, %%edx\n\t" + "sub $128, %%rsp\n\t" + "call *%[code]\n\t" + "add $128, %%rsp\n\t" + "vpxor %%ymm3, %%ymm3, %%ymm3\n\t" + "mov $7, %%eax\n\t" + "xor %%edx, %%edx\n\t" + "xrstor64 (%%rdi)\n\t" + "vmovdqu %%ymm3, (%[restored])\n\t" + "vzeroupper\n\t" + : + : [pattern] "r"(pattern), [area] "r"(area), [code] "r"(code), [restored] "r"(restored) + : "rax", "rdx", "rdi", "xmm3", "memory"); + munmap(code, 4096); + std::uint64_t components = 0; + std::memcpy(&components, area + 512, sizeof(components)); + require((components & 6) == 6, "XSAVE did not mark the SSE and AVX state as saved"); + require(std::memcmp(area + 160 + 3 * 16, pattern, 16) == 0 && std::memcmp(area + 576 + 3 * 16, pattern + 16, 16) == 0, "XSAVE did not store YMM3 in the standard format"); + require(std::memcmp(restored, pattern, sizeof(pattern)) == 0, "XRSTOR did not restore YMM3 from the area XSAVE wrote"); +} #else void reciprocalExecution() {} void registerFormExecution() {} @@ -2003,6 +2069,7 @@ void immediateFormExecution() {} void ripRelativeExecution() {} void rdpruExecution() {} void mcommitExecution() {} +void xsaveoptExecution() {} #endif #if defined(__x86_64__) && (defined(__linux__) || defined(__APPLE__)) @@ -2091,6 +2158,7 @@ int main() { converterInPlaceFollower(); converterClzero(); rosettaSubstitutions(); + xsaveoptExecution(); converterRosetta(); rosettaScalarCompares(); converterRosettaScalarCompare(); diff --git a/core/relinker/codegen/tests/MaskedStoreLoweringTests.cpp b/core/relinker/codegen/tests/MaskedStoreLoweringTests.cpp new file mode 100644 index 0000000000..5517011feb --- /dev/null +++ b/core/relinker/codegen/tests/MaskedStoreLoweringTests.cpp @@ -0,0 +1,368 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#if (defined(__linux__) || defined(__APPLE__)) && defined(__x86_64__) +#include +#include +#include +#endif + +namespace { + +using Bytes = std::vector; + +void require(bool condition, const char* message) { + if (!condition) throw std::runtime_error(message); +} + +struct Form { + std::uint8_t Opcode; + bool W; + const char* Name; + std::uint8_t ElementSize; +}; + +constexpr std::array kForms{ + Form{0x2E, false, "VMASKMOVPS", 4}, + Form{0x2F, false, "VMASKMOVPD", 8}, + Form{0x8E, false, "VPMASKMOVD", 4}, + Form{0x8E, true, "VPMASKMOVQ", 8}, +}; + +Bytes maskedStore(const Form& form, bool wide, std::uint8_t mask, std::uint8_t source, std::uint8_t rexIndexBase, Bytes memory, const Bytes& prefixes = {}) { + Bytes bytes = prefixes; + bytes.insert(bytes.end(), {0xC4, + static_cast(((source & 8) != 0 ? 0 : 0x80) | ((rexIndexBase & 2) != 0 ? 0 : 0x40) | ((rexIndexBase & 1) != 0 ? 0 : 0x20) | 0x02), + static_cast((form.W ? 0x80 : 0) | ((~mask & 15) << 3) | (wide ? 4 : 0) | 0x01), form.Opcode}); + memory.at(0) = static_cast(memory[0] | ((source & 7) << 3)); + bytes.insert(bytes.end(), memory.begin(), memory.end()); + return bytes; +} + +bool rejected(const Bytes& bytes) { + try { + (void)Codegen::DecodeMaskedStore(bytes.data(), bytes.size()); + } catch (const Codegen::CodegenException&) { + return true; + } + return false; +} + +void decoding() { + for (const auto& form : kForms) { + for (const bool wide : {false, true}) { + for (std::uint8_t mask = 0; mask < 16; ++mask) { + for (std::uint8_t source = 0; source < 16; ++source) { + const auto bytes = maskedStore(form, wide, mask, source, 0, {0x07}); + const auto operands = Codegen::DecodeMaskedStore(bytes.data(), bytes.size()); + require(operands && std::string(operands->Name) == form.Name && operands->ElementSize == form.ElementSize && operands->Wide == wide && operands->Mask == mask, "Masked store decoding changed the form, width or mask register"); + require(operands->Memory.Rm == 7 && operands->Memory.Mod == 0 && operands->Memory.RexIndexBase == 0 && operands->Instruction == bytes, "Masked store decoding changed the memory operand"); + } + } + } + } + const auto extended = maskedStore(kForms[0], true, 3, 4, 3, {0x84, 0xD1, 0x78, 0x56, 0x34, 0x12}, {0x64, 0x67}); + const auto operands = Codegen::DecodeMaskedStore(extended.data(), extended.size()); + require(operands && operands->Memory.RexIndexBase == 3 && operands->Memory.Sib == 0xD1 && operands->Memory.Displacement == 0x12345678 && operands->Memory.Prefixes == Bytes{0x64, 0x67}, "Masked store extensions, SIB or prefixes were lost"); + const auto rip = maskedStore(kForms[1], false, 1, 2, 0, {0x05, 0x10, 0x00, 0x00, 0x00}); + const auto ripOperands = Codegen::DecodeMaskedStore(rip.data(), rip.size()); + require(ripOperands && ripOperands->Memory.RipRelative && ripOperands->DisplacementOffset == 5 && ripOperands->Memory.Displacement == 0x10, "RIP-relative masked store was not recognised"); + const auto stack = maskedStore(kForms[2], false, 1, 2, 0, {0x44, 0x24, 0x08}); + const auto stackOperands = Codegen::DecodeMaskedStore(stack.data(), stack.size()); + require(stackOperands && stackOperands->Memory.StackBase && stackOperands->DisplacementOffset == 6, "RSP-based masked store was not recognised"); + for (const auto& bytes : { + Bytes{0xC4, 0xE2, 0x75, 0x2C, 0x07}, Bytes{0xC4, 0xE2, 0x75, 0x2D, 0x07}, Bytes{0xC4, 0xE2, 0x75, 0x8C, 0x07}, + Bytes{0xC4, 0xE2, 0xF5, 0x2E, 0x07}, Bytes{0xC4, 0xE2, 0xF5, 0x2F, 0x07}, Bytes{0xC4, 0xE2, 0x75, 0x2E, 0xC7}, + Bytes{0xC4, 0xE1, 0x75, 0x2E, 0x07}, Bytes{0xC4, 0xE2, 0x74, 0x2E, 0x07}, Bytes{0xC5, 0xF5, 0x2E, 0x07}, + Bytes{0xC4, 0xE2, 0x75, 0x2E}, Bytes{0x66, 0xC4, 0xE2, 0x75, 0x2E, 0x07}}) + require(!Codegen::DecodeMaskedStore(bytes.data(), bytes.size()), "A load, register form, other map or prefix matched a masked store"); + require(rejected(Bytes{0xC4, 0xE2, 0x75, 0x2E, 0x07, 0x00}), "A masked store with trailing bytes was accepted"); + require(rejected(Bytes{0xC4, 0xE2, 0x75, 0x2E, 0x47}), "A truncated masked store was accepted"); +} + +void body() { + const auto site = maskedStore(kForms[1], false, 1, 2, 0, {0x07}); + Codegen::StubBodyBuilder builder; + Codegen::EmitMaskedStore(builder, *Codegen::DecodeMaskedStore(site.data(), site.size())); + const auto lowered = builder.Finish(); + const Bytes expected{ + 0x48, 0x8D, 0x64, 0x24, 0x80, 0x9C, 0x50, 0x51, 0x48, 0x8D, 0x07, 0xC4, 0xE1, 0x79, 0x50, 0xC9, + 0xF6, 0xC1, 0x01, 0x74, 0x0A, 0xF0, 0x80, 0x48, 0x00, 0x00, 0xF0, 0x80, 0x48, 0x07, 0x00, + 0xF6, 0xC1, 0x02, 0x74, 0x0A, 0xF0, 0x80, 0x48, 0x08, 0x00, 0xF0, 0x80, 0x48, 0x0F, 0x00, + 0x59, 0x58, 0x9D, 0x48, 0x8D, 0xA4, 0x24, 0x80, 0x00, 0x00, 0x00, 0xC4, 0xE2, 0x71, 0x2F, 0x17}; + require(Bytes(lowered.Bytes.begin(), lowered.Bytes.begin() + static_cast(lowered.ReturnBranchOffset)) == expected, "VMASKMOVPD stub does not probe its elements before the store"); + const auto segment = maskedStore(kForms[0], true, 9, 2, 0, {0x44, 0x24, 0x08}, {0x64}); + Codegen::StubBodyBuilder segmentBuilder; + Codegen::EmitMaskedStore(segmentBuilder, *Codegen::DecodeMaskedStore(segment.data(), segment.size())); + const auto segmentBody = segmentBuilder.Finish().Bytes; + const Bytes lea{0x64, 0x48, 0x8D, 0x84, 0x24, 0xA0, 0x00, 0x00, 0x00, 0xC4, 0xC1, 0x7C, 0x50, 0xC9}; + require(std::search(segmentBody.begin(), segmentBody.end(), lea.begin(), lea.end()) == segmentBody.begin() + 8, "RSP-based masked store address does not skip the spill frame"); + const Bytes probe{0x64, 0xF0, 0x80, 0x48, 0x1F, 0x00}; + require(std::search(segmentBody.begin(), segmentBody.end(), probe.begin(), probe.end()) != segmentBody.end(), "Masked store probes lost the segment prefix"); + const auto overflow = maskedStore(kForms[0], false, 1, 2, 0, {0x84, 0x24, 0xFF, 0xFF, 0xFF, 0x7F}); + bool thrown = false; + try { + Codegen::StubBodyBuilder overflowBuilder; + Codegen::EmitMaskedStore(overflowBuilder, *Codegen::DecodeMaskedStore(overflow.data(), overflow.size())); + } catch (const Codegen::CodegenException&) { + thrown = true; + } + require(thrown, "An overflowing stack displacement was silently wrapped"); +} + +void matching() { + const auto intel = Codegen::MakeAmd64OnlyInstructionMatcher(); + const auto rosetta = Codegen::MakeAmd64OnlyInstructionMatcher(Codegen::Amd64OnlyTarget::Rosetta); + for (const auto& form : kForms) { + const auto bytes = maskedStore(form, true, 1, 0, 0, {0x00}); + require(!intel->Match(bytes.data(), bytes.size()), "Intel mode lowered a masked store"); + const auto match = rosetta->Match(bytes.data(), bytes.size()); + require(match && match->InstructionName == form.Name && match->Lowering == Codegen::Amd64OnlyLowering::Trampoline && !match->Optional, "Rosetta mode did not require a masked store trampoline"); + const std::array, 2> sequence{bytes, bytes}; + require(rosetta->MatchSequence(sequence, {}).has_value(), "A masked store sequence could not move out of line"); + } + const Bytes load{0xC4, 0xE2, 0x75, 0x2C, 0x00}; + require(!rosetta->Match(load.data(), load.size()), "Rosetta mode lowered a masked load"); +} + +#if (defined(__linux__) || defined(__APPLE__)) && defined(__x86_64__) + +constexpr std::size_t kPage = 4096; +constexpr std::size_t kDataPages = 16; +constexpr std::uint8_t kFill = 0xA5; +constexpr std::uint8_t kSentinel = 0x5A; + +std::uint8_t* protectedPage = nullptr; +volatile sig_atomic_t faults = 0; + +void onFault(int number, siginfo_t* info, void*) { + const auto* address = static_cast(info->si_addr); + if (address < protectedPage || address >= protectedPage + kPage) { + signal(number, SIG_DFL); + return; + } + faults = faults + 1; + mprotect(protectedPage, kPage, PROT_READ | PROT_WRITE); +} + +struct alignas(32) Machine { + std::uint8_t Ymm[16][32]; + std::uint64_t Gpr[7]; + std::uint64_t Flags; + std::uint64_t Stack; + std::uint64_t Code; +}; + +static_assert(offsetof(Machine, Gpr) == 512 && offsetof(Machine, Flags) == 568 && offsetof(Machine, Stack) == 576 && offsetof(Machine, Code) == 584); + +void run(Machine& machine) { + asm volatile( + "mov %%rsp, %%r12\n\t" + "mov %[machine], %%r13\n\t" + "vmovdqu 0(%%r13), %%ymm0\n\t" + "vmovdqu 32(%%r13), %%ymm1\n\t" + "vmovdqu 64(%%r13), %%ymm2\n\t" + "vmovdqu 96(%%r13), %%ymm3\n\t" + "vmovdqu 128(%%r13), %%ymm4\n\t" + "vmovdqu 160(%%r13), %%ymm5\n\t" + "vmovdqu 192(%%r13), %%ymm6\n\t" + "vmovdqu 224(%%r13), %%ymm7\n\t" + "vmovdqu 256(%%r13), %%ymm8\n\t" + "vmovdqu 288(%%r13), %%ymm9\n\t" + "vmovdqu 320(%%r13), %%ymm10\n\t" + "vmovdqu 352(%%r13), %%ymm11\n\t" + "vmovdqu 384(%%r13), %%ymm12\n\t" + "vmovdqu 416(%%r13), %%ymm13\n\t" + "vmovdqu 448(%%r13), %%ymm14\n\t" + "vmovdqu 480(%%r13), %%ymm15\n\t" + "mov 512(%%r13), %%rax\n\t" + "mov 520(%%r13), %%rcx\n\t" + "mov 528(%%r13), %%rbx\n\t" + "mov 536(%%r13), %%rsi\n\t" + "mov 544(%%r13), %%rdi\n\t" + "mov 552(%%r13), %%r9\n\t" + "mov 560(%%r13), %%r10\n\t" + "mov 576(%%r13), %%rsp\n\t" + "pushq 568(%%r13)\n\t" + "popfq\n\t" + "call *584(%%r13)\n\t" + "pushfq\n\t" + "popq 568(%%r13)\n\t" + "mov %%rsp, 576(%%r13)\n\t" + "mov %%r12, %%rsp\n\t" + "mov %%rax, 512(%%r13)\n\t" + "mov %%rcx, 520(%%r13)\n\t" + "mov %%rbx, 528(%%r13)\n\t" + "mov %%rsi, 536(%%r13)\n\t" + "mov %%rdi, 544(%%r13)\n\t" + "mov %%r9, 552(%%r13)\n\t" + "mov %%r10, 560(%%r13)\n\t" + "vmovdqu %%ymm0, 0(%%r13)\n\t" + "vmovdqu %%ymm1, 32(%%r13)\n\t" + "vmovdqu %%ymm2, 64(%%r13)\n\t" + "vmovdqu %%ymm3, 96(%%r13)\n\t" + "vmovdqu %%ymm4, 128(%%r13)\n\t" + "vmovdqu %%ymm5, 160(%%r13)\n\t" + "vmovdqu %%ymm6, 192(%%r13)\n\t" + "vmovdqu %%ymm7, 224(%%r13)\n\t" + "vmovdqu %%ymm8, 256(%%r13)\n\t" + "vmovdqu %%ymm9, 288(%%r13)\n\t" + "vmovdqu %%ymm10, 320(%%r13)\n\t" + "vmovdqu %%ymm11, 352(%%r13)\n\t" + "vmovdqu %%ymm12, 384(%%r13)\n\t" + "vmovdqu %%ymm13, 416(%%r13)\n\t" + "vmovdqu %%ymm14, 448(%%r13)\n\t" + "vmovdqu %%ymm15, 480(%%r13)\n\t" + "vzeroupper\n\t" + : + : [machine] "r"(&machine) + : "rax", "rcx", "rbx", "rsi", "rdi", "r9", "r10", "r12", "r13", + "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7", + "xmm8", "xmm9", "xmm10", "xmm11", "xmm12", "xmm13", "xmm14", "xmm15", "memory", "cc"); +} + +struct Addressing { + Bytes Memory; + std::uint8_t RexIndexBase; + std::uint8_t Mask; + std::uint8_t Source; +}; + +enum Register { Rax, Rcx, Rbx, Rsi, Rdi, R9, R10 }; + +const std::array kAddressings{ + Addressing{{0x07}, 0, 1, 2}, + Addressing{{0x44, 0x5E, 0x7F}, 0, 9, 14}, + Addressing{{0x84, 0x88, 0x00, 0xF0, 0xFF, 0xFF}, 0, 15, 0}, + Addressing{{0x04, 0xD1}, 3, 3, 8}, + Addressing{{0x44, 0x24, 0x40}, 0, 12, 5}, + Addressing{{0x05, 0x00, 0x00, 0x00, 0x00}, 0, 6, 11}, +}; + +void execution() { + const auto region = static_cast(mmap(nullptr, (kDataPages + 2) * kPage, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)); + require(region != MAP_FAILED, "Cannot map masked store test pages"); + auto* const code = region; + auto* const data = region + kPage; + protectedPage = data + kDataPages * kPage; + struct sigaction action {}; + action.sa_sigaction = onFault; + action.sa_flags = SA_SIGINFO; + sigemptyset(&action.sa_mask); + struct sigaction previousSegv {}; + struct sigaction previousBus {}; + sigaction(SIGSEGV, &action, &previousSegv); + sigaction(SIGBUS, &action, &previousBus); + const auto matcher = Codegen::MakeAmd64OnlyInstructionMatcher(Codegen::Amd64OnlyTarget::Rosetta); + const std::array patterns{0xFF, 0x00, 0x01, 0x80, 0x55, 0xFE}; + std::uint64_t runs = 0; + for (const auto& form : kForms) { + for (const bool wide : {false, true}) { + const auto elements = (wide ? 32u : 16u) / form.ElementSize; + for (std::size_t mode = 0; mode < kAddressings.size(); ++mode) { + const auto& addressing = kAddressings[mode]; + const auto site = maskedStore(form, wide, addressing.Mask, addressing.Source, addressing.RexIndexBase, addressing.Memory); + const auto match = matcher->Match(site.data(), site.size()); + require(match && match->Lowering == Codegen::Amd64OnlyLowering::Trampoline, "Masked store stub was not produced"); + for (const int offset : {-40, -16, -4, -2, 0, 8}) { + auto* const target = protectedPage + offset; + const bool stackBased = mode == 4; + auto* const stack = stackBased ? target + 8 - 0x40 : data + kDataPages / 2 * kPage; + auto body = match->StubBody; + const auto siteAddress = mode == 5 ? reinterpret_cast(target) - site.size() : reinterpret_cast(code); + Codegen::ApplyStubRelocations(body, match->Relocations, siteAddress, reinterpret_cast(code), 0); + body.at(match->ReturnBranchOffset) = 0xC3; + require(body.size() <= kPage, "Masked store stub exceeds its code page"); + std::memcpy(code, body.data(), body.size()); + for (const auto pattern : patterns) { + Machine machine{}; + for (unsigned reg = 0; reg < 16; ++reg) + for (unsigned byte = 0; byte < 32; ++byte) + machine.Ymm[reg][byte] = static_cast(reg * 37 + byte * 11 + 1); + for (unsigned element = 0; element < 32u / form.ElementSize; ++element) { + const bool active = element >= elements || (pattern >> element & 1) != 0; + auto* lane = machine.Ymm[addressing.Mask] + element * form.ElementSize; + std::memset(lane, active ? 0x00 : 0xFF, form.ElementSize); + lane[0] = 0x01; + lane[form.ElementSize - 1] = active ? 0x80 : 0x7F; + } + machine.Gpr[Rax] = 0x1111111111111111ull; + machine.Gpr[Rcx] = 0x2222222222222222ull; + machine.Gpr[Rbx] = 8; + machine.Gpr[Rsi] = reinterpret_cast(target) - 0x7F - 16; + machine.Gpr[Rdi] = reinterpret_cast(target); + machine.Gpr[R9] = reinterpret_cast(target) - 5 * 8; + machine.Gpr[R10] = 5; + if (mode == 2) { + machine.Gpr[Rcx] = 3; + machine.Gpr[Rax] = reinterpret_cast(target) + 0x1000 - 12; + } + machine.Flags = (runs & 1) != 0 ? 0x8D7 : 0x002; + machine.Stack = reinterpret_cast(stack); + machine.Code = reinterpret_cast(code); + const auto input = machine; + require(mprotect(protectedPage, kPage, PROT_READ | PROT_WRITE) == 0, "Cannot unprotect the watched page"); + std::memset(target - 16, kFill, 64); + std::memset(stack - 8 - 128, kSentinel, 128); + require(mprotect(protectedPage, kPage, PROT_READ) == 0, "Cannot protect the watched page"); + faults = 0; + run(machine); + ++runs; + bool touchesProtected = false; + for (unsigned element = 0; element < elements; ++element) { + const bool active = (pattern >> element & 1) != 0; + for (unsigned byte = 0; byte < form.ElementSize; ++byte) { + const auto at = element * form.ElementSize + byte; + require(target[at] == (active ? input.Ymm[addressing.Source][at] : kFill), "Masked store wrote an inactive element or lost an active one"); + touchesProtected = touchesProtected || (active && target + at >= protectedPage); + } + } + for (int at = -16; at < 0; ++at) + require(target[at] == kFill, "Masked store wrote before its operand"); + for (unsigned at = elements * form.ElementSize; at < 48; ++at) + require(target[at] == kFill, "Masked store wrote past its operand"); + require(faults == (touchesProtected ? 1 : 0), "Masked store stub did not fault exactly once on the protected page"); + for (unsigned at = 0; at < 128; ++at) + require(stack[-8 - 128 + static_cast(at)] == kSentinel, "Masked store stub wrote into the red zone"); + require(std::memcmp(machine.Ymm, input.Ymm, sizeof(input.Ymm)) == 0, "Masked store stub changed a vector register"); + require(std::memcmp(machine.Gpr, input.Gpr, sizeof(input.Gpr)) == 0, "Masked store stub changed a general register"); + require((machine.Flags & 0x8D5) == (input.Flags & 0x8D5), "Masked store stub changed the flags"); + require(machine.Stack == input.Stack, "Masked store stub did not restore RSP"); + } + } + } + } + } + sigaction(SIGSEGV, &previousSegv, nullptr); + sigaction(SIGBUS, &previousBus, nullptr); + munmap(region, (kDataPages + 2) * kPage); + std::cout << runs << " masked store executions\n"; +} +#else +void execution() { std::cout << "Masked store execution checks require a Linux/macOS x86-64 build\n"; } +#endif + +} + +int main() { + try { + decoding(); + body(); + matching(); + execution(); + std::cout << "Masked store lowering checks passed\n"; + } catch (const std::exception& error) { + std::cerr << error.what() << '\n'; + return 1; + } +} diff --git a/core/relinker/codegen/tests/RedZoneFramerExecutionTests.cpp b/core/relinker/codegen/tests/RedZoneFramerExecutionTests.cpp new file mode 100644 index 0000000000..7d940524b9 --- /dev/null +++ b/core/relinker/codegen/tests/RedZoneFramerExecutionTests.cpp @@ -0,0 +1,286 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#if defined(__linux__) && defined(__x86_64__) +#include +#include +#include +#endif + +#if defined(__linux__) && defined(__x86_64__) + +namespace { + +void require(const bool condition, const std::string& message) { + if (!condition) throw std::runtime_error(message); +} + +constexpr std::size_t kArenaBytes = 0x10000; +constexpr std::size_t kStubBase = 0x4000; +constexpr std::size_t kRunnerBase = 0xc000; +constexpr std::uint8_t kClobber = 0xa5; +constexpr std::uint64_t kA = 0x1111, kB = 0x2222, kC = 0x3333, kD = 0x4444; + +std::uint8_t* arena = nullptr; +volatile sig_atomic_t steps = 0; + +void clobberBelowRsp(int, siginfo_t*, void* context) { + auto& registers = static_cast(context)->uc_mcontext.gregs; + const auto rip = static_cast(registers[REG_RIP]); + const auto base = reinterpret_cast(arena); + if (rip < base || rip >= base + kArenaBytes) { + registers[REG_EFL] &= ~static_cast(0x100); + return; + } + std::memset(reinterpret_cast(static_cast(registers[REG_RSP]) - 0x80), kClobber, 0x80); + steps = steps + 1; +} + +const std::vector Runner{ + 0x53, 0x4c, 0x89, 0xc3, 0x48, 0x89, 0xf8, 0x48, 0x89, 0xf7, 0x48, 0x89, 0xd6, 0x48, 0x89, 0xca, + 0x4d, 0x85, 0xc9, 0x74, 0x0a, 0x9c, 0x48, 0x81, 0x0c, 0x24, 0x00, 0x01, 0x00, 0x00, 0x9d, + 0xff, 0xd0, 0x9c, 0x48, 0x81, 0x24, 0x24, 0xff, 0xfe, 0xff, 0xff, 0x9d, 0x5b, 0xc3, +}; + +struct Leaf { + const char* Name; + std::vector Code; + std::uint64_t Expected; +}; + +const std::vector FramelessPushes{0x53, 0x41, 0x56, 0x41, 0x57}; +const std::vector FramelessPops{0x41, 0x5f, 0x41, 0x5e, 0x5b, 0xc3}; + +std::vector frameless(const std::vector& body) { + std::vector bytes = FramelessPushes; + bytes.insert(bytes.end(), body.begin(), body.end()); + bytes.insert(bytes.end(), FramelessPops.begin(), FramelessPops.end()); + return bytes; +} + +std::vector framedLeaves() { + return { + {"frameless leaf spilling 0x10 below rsp", frameless({ + 0x48, 0x89, 0x7c, 0x24, 0xf0, 0x48, 0x89, 0x74, 0x24, 0xf8, 0x31, 0xc0, 0xb9, 0x08, 0x00, 0x00, 0x00, + 0x48, 0x03, 0x44, 0x24, 0xf0, 0x48, 0x03, 0x44, 0x24, 0xf8, 0xff, 0xc9, 0x75, 0xf2, + }), 8 * (kA + kB)}, + {"leaf reaching 0x28 below rsp", frameless({ + 0x48, 0x89, 0x7c, 0x24, 0xd8, 0x48, 0x89, 0x74, 0x24, 0xe8, 0x48, 0x89, 0x54, 0x24, 0xf0, 0x31, 0xc0, 0xb9, 0x08, 0x00, 0x00, 0x00, + 0x48, 0x03, 0x44, 0x24, 0xd8, 0x48, 0x03, 0x44, 0x24, 0xe8, 0x48, 0x03, 0x44, 0x24, 0xf0, 0xff, 0xc9, 0x75, 0xed, + }), 8 * (kA + kB + kC)}, + {"leaf reading its saved rbx at +0x10 moved in place", frameless({ + 0x48, 0x89, 0x7c, 0x24, 0xf0, 0x31, 0xc0, 0xb9, 0x08, 0x00, 0x00, 0x00, + 0x48, 0x03, 0x44, 0x24, 0xf0, 0x48, 0x03, 0x44, 0x24, 0x10, 0xff, 0xc9, 0x75, 0xf2, + }), 8 * (kA + kD)}, + {"leaf taking the address of a slot 0x20 below rsp", frameless({ + 0x4c, 0x8d, 0x74, 0x24, 0xe0, 0x49, 0x89, 0x3e, 0x49, 0x89, 0x76, 0x08, 0x31, 0xc0, 0xb9, 0x08, 0x00, 0x00, 0x00, + 0x49, 0x03, 0x06, 0x49, 0x03, 0x46, 0x08, 0xff, 0xc9, 0x75, 0xf5, + }), 8 * (kA + kB)}, + {"inflate_fast-shaped leaf allocating 0x40 after six pushes", { + 0x55, 0x41, 0x57, 0x41, 0x56, 0x41, 0x55, 0x41, 0x54, 0x53, 0x48, 0x83, 0xec, 0x40, + 0x48, 0x89, 0x7c, 0x24, 0xe0, 0x48, 0x89, 0x7c, 0x24, 0x00, 0x48, 0x89, 0x74, 0x24, 0x08, 0x48, 0x89, 0x54, 0x24, 0x38, + 0xb9, 0x04, 0x00, 0x00, 0x00, 0x31, 0xc0, 0x48, 0x03, 0x44, 0x24, 0x08, 0x48, 0x03, 0x44, 0x24, 0xe0, 0x48, 0x03, 0x44, 0x24, 0x00, + 0xff, 0xc9, 0x75, 0xed, 0x48, 0x8b, 0x54, 0x24, 0x38, + 0x48, 0x83, 0xc4, 0x40, 0x5b, 0x41, 0x5c, 0x41, 0x5d, 0x41, 0x5e, 0x41, 0x5f, 0x5d, 0xc3, + }, 4 * (kB + 2 * kA)}, + }; +} + +std::vector stubbedLeaves() { + return { + {"inflate_fast-shaped leaf with operands at rsp and at 0x60 shifted by 0x20", { + 0x55, 0x41, 0x57, 0x41, 0x56, 0x41, 0x55, 0x41, 0x54, 0x53, 0x48, 0x83, 0xec, 0x70, + 0x48, 0x89, 0x7c, 0x24, 0xe0, 0x48, 0x89, 0x34, 0x24, 0x48, 0x89, 0x54, 0x24, 0x60, 0xb9, 0x04, 0x00, 0x00, 0x00, 0x31, 0xc0, + 0x48, 0x03, 0x44, 0x24, 0xe0, 0x48, 0x03, 0x04, 0x24, 0x48, 0x03, 0x44, 0x24, 0x60, 0xff, 0xc9, 0x75, 0xee, + 0x48, 0x83, 0xc4, 0x70, 0x5b, 0x41, 0x5c, 0x41, 0x5d, 0x41, 0x5e, 0x41, 0x5f, 0x5d, 0xc3, + }, 4 * (kA + kB + kC)}, + {"leaf shifted by the whole red zone reading its saved rbx at +0x8", { + 0x41, 0x57, 0x53, 0x41, 0x56, 0x4c, 0x8d, 0x74, 0x24, 0xe0, 0x49, 0x89, 0x3e, 0x31, 0xc0, 0xb9, 0x08, 0x00, 0x00, 0x00, + 0x49, 0x03, 0x06, 0x48, 0x03, 0x44, 0x24, 0x08, 0xff, 0xc9, 0x75, 0xf4, 0x41, 0x5e, 0x5b, 0x41, 0x5f, 0xc3, + }, 8 * (kA + kD)}, + }; +} + +std::vector backwardStubbedLeaves() { + return { + {"inflate_fast-shaped leaf comparing at rsp between an add and a jne", { + 0x55, 0x41, 0x57, 0x41, 0x56, 0x41, 0x55, 0x41, 0x54, 0x53, 0x48, 0x83, 0xec, 0x40, + 0x48, 0x89, 0x7c, 0x24, 0xe0, 0x48, 0x89, 0x7c, 0x24, 0x00, 0xb9, 0x04, 0x00, 0x00, 0x00, 0x31, 0xc0, + 0x48, 0x03, 0x44, 0x24, 0xe0, 0x49, 0x01, 0xf8, 0x48, 0x39, 0x3c, 0x24, 0x75, 0x03, 0x48, 0x01, 0xf0, 0xff, 0xc9, 0x75, 0xeb, + 0x48, 0x83, 0xc4, 0x40, 0x5b, 0x41, 0x5c, 0x41, 0x5d, 0x41, 0x5e, 0x41, 0x5f, 0x5d, 0xc3, + }, 4 * (kA + kB)}, + {"leaf comparing at rsp after a load below rsp that heads its loop", { + 0x55, 0x41, 0x57, 0x41, 0x56, 0x41, 0x55, 0x41, 0x54, 0x53, 0x48, 0x83, 0xec, 0x40, + 0x48, 0x89, 0x7c, 0x24, 0xe0, 0x48, 0x89, 0x7c, 0x24, 0x00, 0xb9, 0x04, 0x00, 0x00, 0x00, 0x31, 0xc0, + 0x48, 0x03, 0x44, 0x24, 0xe0, 0x48, 0x39, 0x3c, 0x24, 0x75, 0x03, 0x48, 0x01, 0xf0, 0xff, 0xc9, 0x75, 0xee, + 0x48, 0x83, 0xc4, 0x40, 0x5b, 0x41, 0x5c, 0x41, 0x5d, 0x41, 0x5e, 0x41, 0x5f, 0x5d, 0xc3, + }, 4 * (kA + kB)}, + }; +} + +void writeRel32(std::uint8_t* at, const std::uint64_t target, const std::uint64_t next) { + const auto displacement = static_cast(target) - static_cast(next); + require(displacement >= std::numeric_limits::min() && displacement <= std::numeric_limits::max(), "a stub jump is out of rel32 range"); + const auto value = static_cast(static_cast(displacement)); + for (std::size_t index = 0; index < 4; ++index) at[index] = static_cast(value >> (index * 8)); +} + +std::vector callTo(const std::size_t from, const std::size_t target) { + std::vector bytes{0xe8, 0, 0, 0, 0}; + const auto displacement = static_cast(static_cast(target) - static_cast(from + 5)); + for (std::size_t index = 0; index < 4; ++index) bytes[1 + index] = static_cast(displacement >> (index * 8)); + return bytes; +} + +struct Image { + std::vector Bytes; + std::vector Starts; + std::vector Ends; +}; + +Image place(const std::vector& leaves) { + Image image; + std::size_t at = 0x40; + for (const auto& leaf : leaves) { + image.Starts.push_back(at); + at += (leaf.Code.size() + 15) / 16 * 16 + 16; + } + for (const auto start : image.Starts) { + const auto call = callTo(image.Bytes.size(), start); + image.Bytes.insert(image.Bytes.end(), call.begin(), call.end()); + } + image.Bytes.push_back(0xc3); + for (std::size_t index = 0; index < leaves.size(); ++index) { + image.Bytes.resize(image.Starts[index], 0xcc); + image.Bytes.insert(image.Bytes.end(), leaves[index].Code.begin(), leaves[index].Code.end()); + image.Ends.push_back(image.Bytes.size()); + } + image.Bytes.resize((image.Bytes.size() + 15) / 16 * 16 + 16, 0xcc); + require(image.Bytes.size() <= kStubBase, "the leaves do not fit before the stubs"); + return image; +} + +struct Framing { + Codegen::RedZoneFrameReport Report; + std::vector Sites; + std::vector Displacements; +}; + +Framing load(const Image& image, const bool frame) { + std::fill_n(arena, kArenaBytes, 0xcc); + std::copy(image.Bytes.begin(), image.Bytes.end(), arena); + std::copy(Runner.begin(), Runner.end(), arena + kRunnerBase); + Framing framing; + if (!frame) return framing; + const auto base = reinterpret_cast(arena); + const std::vector segments{{1, 5, 0, base, base, image.Bytes.size(), image.Bytes.size(), 16}}; + std::vector functions; + for (std::size_t index = 0; index < image.Starts.size(); ++index) functions.push_back({base + image.Starts[index], base + image.Ends[index]}); + framing.Sites = Codegen::FrameRedZoneFunctions(image.Bytes, segments, functions, {}, framing.Report, framing.Displacements); + for (const auto& moved : framing.Displacements) std::copy(moved.Bytes.begin(), moved.Bytes.end(), arena + moved.Offset); + std::size_t cursor = kStubBase; + for (const auto& site : framing.Sites) { + const auto at = static_cast(site.Address - base); + require(std::equal(site.OriginalBytes.begin(), site.OriginalBytes.end(), arena + at), "a framed site's bytes changed before it was patched"); + cursor = (cursor + 15) / 16 * 16; + require(cursor + site.Body.size() <= kRunnerBase, "the stubs do not fit before the runner"); + auto* stub = arena + cursor; + std::copy(site.Body.begin(), site.Body.end(), stub); + Codegen::ApplyStubRelocations(std::span(stub, site.ReturnBranchOffset), site.Relocations, site.Address, base + cursor, site.Offset); + writeRel32(stub + site.ReturnBranchOffset + 1, site.Address + site.Length, base + cursor + site.ReturnBranchOffset + 5); + std::fill_n(arena + at, site.Length, 0x90); + arena[at] = 0xe9; + writeRel32(arena + at + 1, base + cursor, site.Address + 5); + cursor += site.Body.size(); + } + return framing; +} + +struct Run { + std::uint64_t Result; + int Steps; +}; + +Run run(const std::size_t start, const bool clobber) { + using Entry = std::uint64_t (*)(std::uint64_t, std::uint64_t, std::uint64_t, std::uint64_t, std::uint64_t, std::uint64_t); + const auto entry = reinterpret_cast(arena + kRunnerBase); + steps = 0; + const auto result = entry(reinterpret_cast(arena + start), kA, kB, kC, kD, clobber ? 1 : 0); + return {result, static_cast(steps)}; +} + +std::string hex(const std::uint64_t value) { + std::ostringstream text; + text << "0x" << std::hex << value; + return text.str(); +} + +Codegen::RedZoneFrameReport runLeaves(const std::vector& leaves) { + const auto image = place(leaves); + load(image, false); + std::vector plain, clobbered; + for (std::size_t index = 0; index < leaves.size(); ++index) { + plain.push_back(run(image.Starts[index], false)); + clobbered.push_back(run(image.Starts[index], true)); + require(plain.back().Result == leaves[index].Expected, std::string(leaves[index].Name) + ": the unframed leaf returned " + hex(plain.back().Result) + " without the clobber, not " + hex(leaves[index].Expected)); + require(clobbered.back().Steps > 0, std::string(leaves[index].Name) + ": the clobber did not run"); + require(clobbered.back().Result != leaves[index].Expected, std::string(leaves[index].Name) + ": the unframed leaf survived writes below rsp, so the case proves nothing"); + } + const auto framing = load(image, true); + require(framing.Report.Framed == leaves.size(), "only " + std::to_string(framing.Report.Framed) + " of " + std::to_string(leaves.size()) + " leaves were framed"); + for (std::size_t index = 0; index < leaves.size(); ++index) { + const auto calm = run(image.Starts[index], false); + const auto framed = run(image.Starts[index], true); + require(calm.Result == leaves[index].Expected, std::string(leaves[index].Name) + ": the framed leaf returned " + hex(calm.Result) + " without the clobber, not " + hex(leaves[index].Expected)); + require(framed.Steps > 0, std::string(leaves[index].Name) + ": the clobber did not run over the framed leaf"); + require(framed.Result == leaves[index].Expected, std::string(leaves[index].Name) + ": the framed leaf returned " + hex(framed.Result) + " under writes below rsp, not " + hex(leaves[index].Expected)); + std::cout << leaves[index].Name << ": unframed " << hex(plain[index].Result) << " alone and " << hex(clobbered[index].Result) << " under the clobber (" << clobbered[index].Steps << " steps); framed " << hex(framed.Result) << " under the clobber (" << framed.Steps << " steps)\n"; + } + return framing.Report; +} + +} + +int main() { + try { + struct sigaction action {}; + action.sa_sigaction = clobberBelowRsp; + action.sa_flags = SA_SIGINFO; + sigemptyset(&action.sa_mask); + require(sigaction(SIGTRAP, &action, nullptr) == 0, "cannot install the SIGTRAP handler"); + void* mapped = mmap(nullptr, kArenaBytes, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + if (mapped == MAP_FAILED) { + std::cout << "skipped: no writable and executable memory\n"; + return 77; + } + arena = static_cast(mapped); + runLeaves(framedLeaves()); + const auto stubbed = runLeaves(stubbedLeaves()); + require(stubbed.OperandStubs == 3 && stubbed.StubbedOperands == 5, "the leaves were framed with " + std::to_string(stubbed.OperandStubs) + " operand stubs carrying " + std::to_string(stubbed.StubbedOperands) + " rsp operands, not 3 carrying 5"); + const auto backward = runLeaves(backwardStubbedLeaves()); + require(backward.OperandStubs == 2 && backward.StubbedOperands == 3 && backward.MovedOperands == 5, "the leaves comparing at rsp before a branch were framed with " + std::to_string(backward.OperandStubs) + " operand stubs carrying " + std::to_string(backward.StubbedOperands) + " rsp operands and " + std::to_string(backward.MovedOperands) + " moved in place, not 2 carrying 3 and 5"); + std::cout << "red-zone framer execution tests passed\n"; + return 0; + } catch (const std::exception& error) { + std::cerr << "red-zone framer execution tests failed: " << error.what() << '\n'; + return 1; + } +} + +#else + +int main() { + std::cout << "skipped: the red-zone framer execution tests single-step x86-64 code on Linux\n"; + return 77; +} + +#endif diff --git a/core/relinker/codegen/tests/RedZoneFramerTests.cpp b/core/relinker/codegen/tests/RedZoneFramerTests.cpp new file mode 100644 index 0000000000..31466fcfdb --- /dev/null +++ b/core/relinker/codegen/tests/RedZoneFramerTests.cpp @@ -0,0 +1,625 @@ +#include +#include +#include +#include +#include +#include + +namespace { + +void require(const bool condition, const std::string& message) { + if (!condition) throw std::runtime_error(message); +} + +constexpr std::uint64_t Base = 0x1000; + +std::vector padTo(std::vector bytes, const std::size_t size) { + bytes.resize(size, 0xcc); + return bytes; +} + +void append(std::vector& bytes, const std::vector& more) { + bytes.insert(bytes.end(), more.begin(), more.end()); +} + +std::vector callTo(const std::size_t from, const std::size_t target) { + const auto displacement = static_cast(target) - static_cast(from + 5); + return {0xe8, static_cast(displacement), static_cast(displacement >> 8), static_cast(displacement >> 16), static_cast(displacement >> 24)}; +} + +const std::vector Prologue{0x55, 0x48, 0x89, 0xe5, 0x41, 0x57, 0x53}; +const std::vector Exit{0x5b, 0x41, 0x5f, 0x5d, 0xc3}; +const std::vector StoreBelowPushes{0x48, 0x89, 0x7d, 0xe8}; +const std::vector LoadBelowPushes{0x48, 0x8b, 0x45, 0xe8}; +const std::vector LoadAbovePushes{0x48, 0x8b, 0x45, 0x08}; + +std::vector leaf(const std::vector& body) { + std::vector bytes = Prologue; + append(bytes, body); + append(bytes, Exit); + return bytes; +} + +struct Image { + std::vector bytes; + std::vector segments; + std::vector functions; +}; + +Image image(const std::vector>& functions) { + Image result; + std::vector starts; + std::size_t at = 0x40; + for (const auto& function : functions) { + starts.push_back(at); + at += (function.size() + 15) / 16 * 16 + 16; + } + std::vector caller; + for (const auto start : starts) append(caller, callTo(caller.size(), start)); + caller.push_back(0xc3); + result.bytes = padTo(caller, 0x40); + for (std::size_t index = 0; index < functions.size(); ++index) { + result.bytes = padTo(result.bytes, starts[index]); + append(result.bytes, functions[index]); + result.functions.push_back({Base + starts[index], Base + starts[index] + functions[index].size()}); + } + result.bytes = padTo(result.bytes, (result.bytes.size() + 15) / 16 * 16 + 16); + result.segments.push_back({1, 5, 0, Base, Base, result.bytes.size(), result.bytes.size(), 16}); + return result; +} + +void framesRedZoneLeaf() { + std::vector body = StoreBelowPushes; + append(body, LoadBelowPushes); + const auto input = image({leaf(body)}); + Codegen::RedZoneFrameReport report; + std::vector displacements; + const auto sites = Codegen::FrameRedZoneFunctions(input.bytes, input.segments, input.functions, {}, report, displacements); + require(report.RedZoneFunctions == 1 && report.Framed == 1 && report.Exits == 1, "a red-zone leaf was not framed"); + require(sites.size() == 2, "a framed leaf needs one prologue and one exit site"); + require(displacements.empty() && report.FramedWithoutFramePointer == 0, "a leaf with a frame pointer had rsp operands moved"); + + const auto& entry = sites[0]; + require(entry.Address == Base + 0x40 && entry.Length == Prologue.size(), "the prologue site does not cover the pushes"); + require(entry.OriginalBytes == Prologue, "the prologue site lost its bytes"); + std::vector entryBody = Prologue; + append(entryBody, {0x48, 0x8d, 0x64, 0x24, 0x80, 0xe9, 0, 0, 0, 0}); + require(entry.Body == entryBody && entry.ReturnBranchOffset == Prologue.size() + 5, "the prologue stub does not lower rsp after the pushes"); + + const auto& exit = sites[1]; + require(exit.Address == Base + 0x40 + Prologue.size() + body.size() && exit.Length == Exit.size(), "the exit site does not cover the pops and ret"); + std::vector exitBody{0x48, 0x8d, 0xa4, 0x24, 0x80, 0, 0, 0}; + append(exitBody, Exit); + append(exitBody, {0xe9, 0, 0, 0, 0}); + require(exit.Body == exitBody && exit.ReturnBranchOffset == 8 + Exit.size(), "the exit stub does not raise rsp before the pops"); +} + +const std::vector FramelessPrologue{0x53, 0x41, 0x56, 0x41, 0x57}; +const std::vector FramelessExit{0x41, 0x5f, 0x41, 0x5e, 0x5b, 0xc3}; +const std::vector StoreBelowRsp{0x48, 0x89, 0x7c, 0x24, 0xf0}; +const std::vector LoadBelowRsp{0x48, 0x8b, 0x44, 0x24, 0xf0}; +const std::vector LoadAboveRsp{0x48, 0x8b, 0x44, 0x24, 0x08}; +const std::vector LoadFarAboveRsp{0x48, 0x8b, 0x44, 0x24, 0x78}; +const std::vector LoadAtRsp{0x48, 0x8b, 0x04, 0x24}; +const std::vector StoreDeepBelowRsp{0x48, 0x89, 0x74, 0x24, 0xd8}; +const std::vector LoadDeepBelowRsp{0x48, 0x8b, 0x44, 0x24, 0xd8}; +const std::vector AddressBelowRsp{0x48, 0x8d, 0x44, 0x24, 0xe0}; +const std::vector AddressAboveRsp{0x48, 0x8d, 0x44, 0x24, 0x08}; +const std::vector StoreIndexedBelowRsp{0x48, 0x89, 0x7c, 0xcc, 0xf0}; +const std::vector StoreImmediateFarAboveRsp{0xc7, 0x44, 0x24, 0x7c, 0x78, 0x56, 0x34, 0x12}; +const std::vector LoadIndexedAtRsp{0x48, 0x8b, 0x04, 0xcc}; +const std::vector AddRdxToRax{0x48, 0x01, 0xd0}; +const std::vector VexStoreAtRsp{0xc5, 0xf8, 0x11, 0x04, 0x24}; +const std::vector EvexStoreBelowRsp{0x62, 0xf1, 0x7c, 0x48, 0x11, 0x44, 0x24, 0xff}; +const std::vector LoadRipRelative{0x48, 0x8b, 0x05, 0x00, 0x00, 0x00, 0x00}; + +std::vector stubBody(std::vector code) { + append(code, {0xe9, 0, 0, 0, 0}); + return code; +} + +std::vector framelessLeaf(const std::vector& prologue, const std::vector& body, const std::vector& exit) { + std::vector bytes = prologue; + append(bytes, body); + append(bytes, exit); + return bytes; +} + +void framesLeafWithoutFramePointer() { + std::vector body = StoreBelowRsp; + append(body, LoadBelowRsp); + const auto input = image({framelessLeaf(FramelessPrologue, body, FramelessExit)}); + Codegen::RedZoneFrameReport report; + std::vector displacements; + const auto sites = Codegen::FrameRedZoneFunctions(input.bytes, input.segments, input.functions, {}, report, displacements); + require(report.RedZoneFunctions == 1 && report.Framed == 1 && report.FramedWithoutFramePointer == 1 && report.MovedOperands == 2, "a leaf that only pushes was not framed"); + require(sites.size() == 2, "a framed leaf needs one prologue and one exit site"); + const auto& entry = sites[0]; + require(entry.Address == Base + 0x40 && entry.Length == FramelessPrologue.size(), "the prologue site does not cover the pushes"); + std::vector entryBody = FramelessPrologue; + append(entryBody, {0x48, 0x8d, 0x64, 0x24, 0xf0, 0xe9, 0, 0, 0, 0}); + require(entry.Body == entryBody, "the prologue stub does not lower rsp by the leaf's depth below rsp after the pushes"); + const auto& exit = sites[1]; + require(exit.Address == Base + 0x40 + FramelessPrologue.size() + body.size() && exit.OriginalBytes == FramelessExit, "the exit site does not cover the pops and ret"); + std::vector exitBody{0x48, 0x8d, 0xa4, 0x24, 0x10, 0, 0, 0}; + append(exitBody, FramelessExit); + append(exitBody, {0xe9, 0, 0, 0, 0}); + require(exit.Body == exitBody, "the exit stub does not raise rsp by the leaf's depth below rsp before the pops"); + const auto storeDisplacement = 0x40 + FramelessPrologue.size() + 4; + const auto loadDisplacement = storeDisplacement + StoreBelowRsp.size(); + require(displacements.size() == 2, "both rsp operands must move"); + require(displacements[0].Offset == storeDisplacement && displacements[0].Bytes == std::vector{0x00}, "the store below rsp was not moved to the lowered rsp"); + require(displacements[1].Offset == loadDisplacement && displacements[1].Bytes == std::vector{0x00}, "the load below rsp was not moved to the lowered rsp"); +} + +void shiftsByTheDeepestOperandBelowRsp() { + std::vector body = StoreBelowRsp; + append(body, StoreDeepBelowRsp); + append(body, LoadDeepBelowRsp); + const auto input = image({framelessLeaf(FramelessPrologue, body, FramelessExit)}); + Codegen::RedZoneFrameReport report; + std::vector displacements; + const auto sites = Codegen::FrameRedZoneFunctions(input.bytes, input.segments, input.functions, {}, report, displacements); + require(report.Framed == 1 && sites.size() == 2 && displacements.size() == 3, "a leaf reaching 0x28 bytes below rsp was not framed"); + std::vector entryBody = FramelessPrologue; + append(entryBody, {0x48, 0x8d, 0x64, 0x24, 0xd0, 0xe9, 0, 0, 0, 0}); + require(sites[0].Body == entryBody, "rsp is not lowered by the deepest displacement rounded up to 16"); + require(std::vector(sites[1].Body.begin(), sites[1].Body.begin() + 8) == std::vector{0x48, 0x8d, 0xa4, 0x24, 0x30, 0, 0, 0}, "rsp is not raised by the shift it was lowered by"); + require(displacements[0].Bytes == std::vector{0x20} && displacements[1].Bytes == std::vector{0x08} && displacements[2].Bytes == std::vector{0x08}, "the operands below rsp were not moved by the shift"); +} + +void movesPositiveOperandsThatFitAfterTheShift() { + std::vector body = StoreBelowRsp; + append(body, LoadAboveRsp); + const auto input = image({framelessLeaf(FramelessPrologue, body, FramelessExit)}); + Codegen::RedZoneFrameReport report; + std::vector displacements; + const auto sites = Codegen::FrameRedZoneFunctions(input.bytes, input.segments, input.functions, {}, report, displacements); + require(report.Framed == 1 && report.MovedOperands == 2 && sites.size() == 2, "a leaf with an 8-bit displacement above rsp that fits after the shift was not framed"); + const auto loadDisplacement = 0x40 + FramelessPrologue.size() + StoreBelowRsp.size() + 4; + require(displacements.size() == 2 && displacements[1].Offset == loadDisplacement && displacements[1].Bytes == std::vector{0x18}, "the load above rsp was not moved in place by the shift"); +} + +void shiftsByTheWholeRedZoneForAddressesBelowRsp() { + std::vector below = StoreBelowRsp; + append(below, AddressBelowRsp); + std::vector above = StoreBelowRsp; + append(above, AddressAboveRsp); + const auto input = image({framelessLeaf(FramelessPrologue, below, FramelessExit), framelessLeaf(FramelessPrologue, above, FramelessExit)}); + Codegen::RedZoneFrameReport report; + std::vector displacements; + const auto sites = Codegen::FrameRedZoneFunctions(input.bytes, input.segments, input.functions, {}, report, displacements); + require(report.Framed == 2 && sites.size() == 4 && displacements.size() == 4, "leaves that take the address of a stack slot were not framed"); + require(sites[0].Body[FramelessPrologue.size() + 4] == 0x80 && displacements[0].Bytes == std::vector{0x70} && displacements[1].Bytes == std::vector{0x60}, "a leaf that takes the address of a slot below rsp was not shifted by the whole red zone"); + require(sites[2].Body[FramelessPrologue.size() + 4] == 0xf0 && displacements[2].Bytes == std::vector{0x00} && displacements[3].Bytes == std::vector{0x18}, "a leaf that only takes the address of a slot above rsp was shifted by more than its depth"); +} + +void shiftsByTheWholeRedZoneForIndexedOperands() { + std::vector body = StoreIndexedBelowRsp; + append(body, LoadBelowRsp); + const auto input = image({framelessLeaf(FramelessPrologue, body, FramelessExit)}); + Codegen::RedZoneFrameReport report; + std::vector displacements; + const auto sites = Codegen::FrameRedZoneFunctions(input.bytes, input.segments, input.functions, {}, report, displacements); + require(report.Framed == 1 && sites.size() == 2 && displacements.size() == 2, "a leaf with an indexed operand below rsp was not framed"); + require(sites[0].Body[FramelessPrologue.size() + 4] == 0x80 && displacements[0].Bytes == std::vector{0x70} && displacements[1].Bytes == std::vector{0x70}, "a leaf whose indexed operand can reach deeper than its displacement was not shifted by the whole red zone"); +} + +void stubsOperandsWhoseDisplacementOverflows() { + std::vector body = StoreBelowRsp; + append(body, LoadFarAboveRsp); + append(body, StoreImmediateFarAboveRsp); + const auto input = image({framelessLeaf(FramelessPrologue, body, FramelessExit)}); + Codegen::RedZoneFrameReport report; + std::vector displacements; + const auto sites = Codegen::FrameRedZoneFunctions(input.bytes, input.segments, input.functions, {}, report, displacements); + require(report.Framed == 1 && report.MovedOperands == 1 && report.OperandStubs == 2 && report.StubbedOperands == 2 && sites.size() == 4, "a leaf whose 8-bit displacements above rsp overflow after the shift was not framed with operand stubs"); + const auto load = Base + 0x40 + FramelessPrologue.size() + StoreBelowRsp.size(); + require(sites[2].Address == load && sites[2].Length == LoadFarAboveRsp.size() && sites[2].OriginalBytes == LoadFarAboveRsp, "the stub site does not cover the load that cannot move in place"); + require(sites[2].Body == stubBody({0x48, 0x8b, 0x84, 0x24, 0x88, 0x00, 0x00, 0x00}) && sites[2].ReturnBranchOffset == 8, "the load was not re-encoded with a 32-bit displacement moved by the shift"); + require(sites[3].Address == load + LoadFarAboveRsp.size() && sites[3].OriginalBytes == StoreImmediateFarAboveRsp, "the stub site does not cover the store of an immediate that cannot move in place"); + require(sites[3].Body == stubBody({0xc7, 0x84, 0x24, 0x8c, 0x00, 0x00, 0x00, 0x78, 0x56, 0x34, 0x12}), "the store lost its immediate when its displacement was widened"); + require(displacements.size() == 1 && displacements[0].Bytes == std::vector{0x00}, "the store below rsp was not moved in place"); +} + +void stubsOperandsAtRspWithTheInstructionsAfterThem() { + std::vector atRsp = StoreBelowRsp; + append(atRsp, LoadAtRsp); + append(atRsp, LoadAboveRsp); + std::vector indexed = StoreBelowRsp; + append(indexed, LoadIndexedAtRsp); + append(indexed, AddRdxToRax); + const auto input = image({framelessLeaf(FramelessPrologue, atRsp, FramelessExit), framelessLeaf(FramelessPrologue, indexed, FramelessExit)}); + Codegen::RedZoneFrameReport report; + std::vector displacements; + const auto sites = Codegen::FrameRedZoneFunctions(input.bytes, input.segments, input.functions, {}, report, displacements); + require(report.Framed == 2 && report.MovedOperands == 2 && report.OperandStubs == 2 && report.StubbedOperands == 3 && sites.size() == 6, "leaves with operands at rsp were not framed with operand stubs"); + const auto first = 0x40 + FramelessPrologue.size() + StoreBelowRsp.size(); + require(sites[2].Address == Base + first && sites[2].Length == LoadAtRsp.size() + LoadAboveRsp.size(), "the stub site does not run on to the next instruction to make room for its jump"); + require(sites[2].Body == stubBody({0x48, 0x8b, 0x44, 0x24, 0x10, 0x48, 0x8b, 0x44, 0x24, 0x18}), "the load at rsp and the load the stub covers after it were not both moved by the shift"); + for (const auto& displacement : displacements) require(displacement.Offset < first || displacement.Offset >= first + sites[2].Length, "an rsp operand inside a stub site was also moved in place"); + require(sites[3].Body[FramelessPrologue.size() + 4] == 0x80, "a leaf with an indexed operand at rsp was not shifted by the whole red zone"); + require(sites[5].OriginalBytes.size() == LoadIndexedAtRsp.size() + AddRdxToRax.size() && sites[5].Body == stubBody({0x48, 0x8b, 0x84, 0xcc, 0x80, 0x00, 0x00, 0x00, 0x48, 0x01, 0xd0}), "the indexed load at rsp was not re-encoded with a 32-bit displacement of 0x80 ahead of the add it covers"); +} + +void stubsVexOperandsAtRsp() { + std::vector body = StoreBelowRsp; + append(body, VexStoreAtRsp); + const auto input = image({framelessLeaf(FramelessPrologue, body, FramelessExit)}); + Codegen::RedZoneFrameReport report; + std::vector displacements; + const auto sites = Codegen::FrameRedZoneFunctions(input.bytes, input.segments, input.functions, {}, report, displacements); + require(report.Framed == 1 && report.OperandStubs == 1 && report.StubbedOperands == 1 && sites.size() == 3, "a leaf with a VEX store at rsp was not framed with an operand stub"); + require(sites[2].OriginalBytes == VexStoreAtRsp && sites[2].Body == stubBody({0xc5, 0xf8, 0x11, 0x44, 0x24, 0x10}), "the VEX store at rsp was not re-encoded with an 8-bit displacement moved by the shift"); +} + +void stubsOperandsAtRspWithTheInstructionsBeforeThem() { + std::vector body = StoreBelowRsp; + append(body, LoadAtRsp); + append(body, LoadRipRelative); + const auto input = image({framelessLeaf(FramelessPrologue, body, FramelessExit)}); + Codegen::RedZoneFrameReport report; + std::vector displacements; + const auto sites = Codegen::FrameRedZoneFunctions(input.bytes, input.segments, input.functions, {}, report, displacements); + require(report.Framed == 1 && report.MovedOperands == 0 && report.OperandStubs == 1 && report.StubbedOperands == 2 && sites.size() == 3 && displacements.empty(), "a leaf whose short operand at rsp sits before a rip-relative instruction was not framed with a stub over the instruction before it"); + const auto store = Base + 0x40 + FramelessPrologue.size(); + require(sites[2].Address == store && sites[2].Length == StoreBelowRsp.size() + LoadAtRsp.size(), "the stub site does not start at the store before the operand at rsp"); + require(sites[2].Body == stubBody({0x48, 0x89, 0x7c, 0x24, 0x00, 0x48, 0x8b, 0x44, 0x24, 0x10}), "the store before the operand and the operand at rsp were not both moved by the shift"); +} + +void skipsLeavesWithOperandsThatCannotMove() { + std::vector evex = StoreBelowRsp; + append(evex, EvexStoreBelowRsp); + std::vector beforeExit = StoreBelowRsp; + append(beforeExit, LoadAtRsp); + std::vector beforeTarget = StoreBelowRsp; + append(beforeTarget, LoadAtRsp); + append(beforeTarget, {0xff, 0xc0, 0x3b, 0x44, 0x24, 0xf0, 0x72, 0xf8}); + const auto input = image({framelessLeaf(FramelessPrologue, evex, FramelessExit), framelessLeaf(FramelessPrologue, beforeExit, FramelessExit), framelessLeaf(FramelessPrologue, beforeTarget, FramelessExit), framelessLeaf({0x41, 0x56, 0x41, 0x57}, StoreBelowRsp, {0x41, 0x5f, 0x41, 0x5e, 0xc3})}); + Codegen::RedZoneFrameReport report; + std::vector displacements; + const auto sites = Codegen::FrameRedZoneFunctions(input.bytes, input.segments, input.functions, {}, report, displacements); + require(sites.empty() && displacements.empty() && report.RedZoneFunctions == 4, "a leaf whose rsp operands can neither move nor be stubbed, or whose prologue is too short, was framed"); + require(report.Skipped.at("rsp operand that cannot move with rsp") == 1, "an EVEX operand, whose 8-bit displacement is scaled, was not refused"); + require(report.Skipped.at("rsp operand that cannot move and has no room for a stub") == 2, "an operand at rsp followed by an exit or a branch target was not refused for want of room"); + require(report.Skipped.at("prologue shorter than a jump") == 1, "the leaf with a short prologue was not reported with its reason"); +} + +void skipsLeavesThatCopyRsp() { + std::vector withCmov = StoreBelowRsp; + append(withCmov, {0x48, 0x0f, 0x45, 0xc4}); + std::vector withShlx = StoreBelowRsp; + append(withShlx, {0xc4, 0xe2, 0xf1, 0xf7, 0xc4}); + const auto input = image({framelessLeaf(FramelessPrologue, withCmov, FramelessExit), framelessLeaf(FramelessPrologue, withShlx, FramelessExit)}); + Codegen::RedZoneFrameReport report; + std::vector displacements; + const auto sites = Codegen::FrameRedZoneFunctions(input.bytes, input.segments, input.functions, {}, report, displacements); + require(sites.empty() && displacements.empty() && report.Skipped.at("other use of rsp") == 2, "a leaf that copies rsp into another register was framed"); +} + +const std::vector InflatePrologue{0x55, 0x41, 0x57, 0x41, 0x56, 0x41, 0x55, 0x41, 0x54, 0x53, 0x48, 0x83, 0xec, 0x40}; +const std::vector InflatePops{0x5b, 0x41, 0x5c, 0x41, 0x5d, 0x41, 0x5e, 0x41, 0x5f, 0x5d, 0xc3}; +const std::vector InflateFree{0x48, 0x83, 0xc4, 0x40}; + +std::vector inflateExit() { + std::vector bytes = InflateFree; + append(bytes, InflatePops); + return bytes; +} + +std::vector inflateBody() { + return { + 0x48, 0x89, 0x7c, 0x24, 0xe0, + 0x48, 0x89, 0x7c, 0x24, 0x00, + 0x48, 0x89, 0x74, 0x24, 0x08, + 0x48, 0x89, 0x54, 0x24, 0x38, + 0xb9, 0x04, 0x00, 0x00, 0x00, + 0x31, 0xc0, + 0x48, 0x03, 0x44, 0x24, 0x08, + 0x48, 0x03, 0x44, 0x24, 0xe0, + 0x48, 0x03, 0x44, 0x24, 0x00, + 0xff, 0xc9, + 0x75, 0xed, + 0x48, 0x8b, 0x54, 0x24, 0x38, + }; +} + +std::vector inflateCompareBody(const std::vector& prior, const std::uint8_t back) { + std::vector bytes{ + 0x48, 0x89, 0x7c, 0x24, 0xe0, + 0x48, 0x89, 0x7c, 0x24, 0x00, + 0xb9, 0x04, 0x00, 0x00, 0x00, + 0x31, 0xc0, + 0x48, 0x03, 0x44, 0x24, 0xe0, + }; + append(bytes, prior); + append(bytes, {0x48, 0x39, 0x3c, 0x24, 0x75, 0x03, 0x48, 0x01, 0xf0, 0xff, 0xc9, 0x75, back}); + return bytes; +} + +const std::vector AddRdiToR8{0x49, 0x01, 0xf8}; +const std::vector CompareRdiAtRspShifted{0x48, 0x39, 0x7c, 0x24, 0x20}; + +void stubsShortOperandsAtRspBeforeABranchWithTheInstructionsBeforeThem() { + const auto withAdd = inflateCompareBody(AddRdiToR8, 0xeb); + std::vector withLoad{ + 0x48, 0x89, 0x7c, 0x24, 0xe0, + 0x48, 0x89, 0x7c, 0x24, 0x00, + 0xb9, 0x04, 0x00, 0x00, 0x00, + 0x31, 0xc0, + 0x48, 0x03, 0x44, 0x24, 0xe0, + 0x48, 0x39, 0x3c, 0x24, 0x75, 0x03, 0x48, 0x01, 0xf0, 0xff, 0xc9, 0x75, 0xee, + }; + const auto input = image({framelessLeaf(InflatePrologue, withAdd, inflateExit()), framelessLeaf(InflatePrologue, withLoad, inflateExit())}); + Codegen::RedZoneFrameReport report; + std::vector displacements; + const auto sites = Codegen::FrameRedZoneFunctions(input.bytes, input.segments, input.functions, {}, report, displacements); + require(report.Framed == 2 && report.OperandStubs == 2 && report.StubbedOperands == 3 && report.MovedOperands == 5 && sites.size() == 6 && report.Skipped.empty(), "inflate_fast's 4-byte cmp at rsp between an instruction and a branch was not framed with a stub over the instruction before it"); + const auto add = input.functions[0].Begin + InflatePrologue.size() + 22; + require(sites[2].Address == add && sites[2].Length == AddRdiToR8.size() + 4, "the stub site does not start at the add before the cmp at rsp"); + std::vector addBody = AddRdiToR8; + append(addBody, CompareRdiAtRspShifted); + require(sites[2].Body == stubBody(addBody), "the add was not copied ahead of the cmp re-encoded with a displacement of the shift"); + const auto load = input.functions[1].Begin + InflatePrologue.size() + 17; + require(sites[5].Address == load && sites[5].Length == 9, "the stub site does not start at the load before the cmp at rsp"); + std::vector loadBody{0x48, 0x03, 0x44, 0x24, 0x00}; + append(loadBody, CompareRdiAtRspShifted); + require(sites[5].Body == stubBody(loadBody), "the load before the cmp, itself an rsp operand, was not moved by the shift in the stub"); + for (const auto& displacement : displacements) { + const auto at = Base + displacement.Offset; + require((at < sites[2].Address || at >= sites[2].Address + sites[2].Length) && (at < sites[5].Address || at >= sites[5].Address + sites[5].Length), "an rsp operand inside a stub site was also moved in place"); + } +} + +void skipsShortOperandsAtRspWhoseWidenedStubCoversABranchTarget() { + std::vector body{ + 0x48, 0x89, 0x7c, 0x24, 0xe0, + 0x48, 0x89, 0x7c, 0x24, 0x00, + 0xb9, 0x04, 0x00, 0x00, 0x00, + 0x31, 0xc0, + }; + append(body, AddRdiToR8); + append(body, {0x48, 0x39, 0x3c, 0x24, 0x75, 0x03, 0x48, 0x01, 0xf0, 0xff, 0xc9, 0x75, 0xf3}); + const auto input = image({framelessLeaf(InflatePrologue, body, inflateExit())}); + Codegen::RedZoneFrameReport report; + std::vector displacements; + const auto sites = Codegen::FrameRedZoneFunctions(input.bytes, input.segments, input.functions, {}, report, displacements); + require(sites.empty() && displacements.empty() && report.RedZoneFunctions == 1 && report.Skipped.at("rsp operand that cannot move and has no room for a stub") == 1, "a cmp at rsp whose loop branches back to it was framed with a stub that starts at the add before it"); +} + +void framesLeavesThatAllocateBelowTheirPushes() { + const auto body = inflateBody(); + const auto input = image({framelessLeaf(InflatePrologue, body, inflateExit())}); + Codegen::RedZoneFrameReport report; + std::vector displacements; + const auto sites = Codegen::FrameRedZoneFunctions(input.bytes, input.segments, input.functions, {}, report, displacements); + require(report.RedZoneFunctions == 1 && report.Framed == 1 && report.FramedWithoutFramePointer == 1 && report.Exits == 1 && sites.size() == 2, "a leaf that allocates with sub after its pushes and spills below its allocation was not framed"); + const auto& entry = sites[0]; + require(entry.Address == Base + 0x40 && entry.OriginalBytes == InflatePrologue, "the prologue site does not cover the pushes and the sub"); + std::vector entryBody = InflatePrologue; + append(entryBody, {0x48, 0x8d, 0x64, 0x24, 0xe0, 0xe9, 0, 0, 0, 0}); + require(entry.Body == entryBody, "the prologue stub does not lower rsp by 0x20 after the sub"); + const auto& exit = sites[1]; + require(exit.Address == Base + 0x40 + InflatePrologue.size() + body.size() && exit.OriginalBytes == inflateExit(), "the exit site does not cover the add, the pops and the ret"); + std::vector exitBody{0x48, 0x8d, 0xa4, 0x24, 0x20, 0, 0, 0}; + append(exitBody, inflateExit()); + append(exitBody, {0xe9, 0, 0, 0, 0}); + require(exit.Body == exitBody, "the exit stub does not raise rsp by 0x20 before the add"); + const std::vector moved{0x00, 0x20, 0x28, 0x58, 0x28, 0x00, 0x20, 0x58}; + require(displacements.size() == moved.size() && report.MovedOperands == moved.size(), "not every rsp operand of the allocating leaf was moved"); + for (std::size_t index = 0; index < moved.size(); ++index) + require(displacements[index].Bytes == std::vector{moved[index]}, "an rsp operand of the allocating leaf was not moved by 0x20 from the post-sub rsp"); + require(displacements[0].Offset == 0x40 + InflatePrologue.size() + 4, "the spill below the allocation was not the first operand moved"); +} + +void framesLeavesThatAllocateWithLeaOrALargeSub() { + const std::vector leaPrologue{0x53, 0x41, 0x56, 0x48, 0x8d, 0x64, 0x24, 0xc0}; + const std::vector leaExit{0x48, 0x8d, 0x64, 0x24, 0x40, 0x41, 0x5e, 0x5b, 0xc3}; + const std::vector largePrologue{0x53, 0x48, 0x81, 0xec, 0x00, 0x01, 0x00, 0x00}; + const std::vector largeExit{0x48, 0x81, 0xc4, 0x00, 0x01, 0x00, 0x00, 0x5b, 0xc3}; + std::vector largeBody = StoreBelowRsp; + append(largeBody, {0x48, 0x8b, 0x84, 0x24, 0x80, 0x00, 0x00, 0x00}); + const auto input = image({framelessLeaf(leaPrologue, StoreBelowRsp, leaExit), framelessLeaf(largePrologue, largeBody, largeExit)}); + Codegen::RedZoneFrameReport report; + std::vector displacements; + const auto sites = Codegen::FrameRedZoneFunctions(input.bytes, input.segments, input.functions, {}, report, displacements); + require(report.Framed == 2 && sites.size() == 4 && displacements.size() == 3, "leaves that allocate with lea or a 32-bit sub were not framed"); + require(sites[0].OriginalBytes == leaPrologue && sites[1].OriginalBytes == leaExit, "the lea allocation and free are not part of the prologue and exit sites"); + require(sites[2].OriginalBytes == largePrologue && sites[3].OriginalBytes == largeExit, "the 32-bit sub and add are not part of the prologue and exit sites"); + require(displacements[2].Bytes == std::vector{0x90, 0x00, 0x00, 0x00}, "the 32-bit displacement into the allocation was not moved by the shift"); +} + +void skipsAllocatingLeavesWithoutAMatchingFree() { + const std::vector otherFree{0x48, 0x83, 0xc4, 0x30}; + std::vector wrongFree = otherFree; + append(wrongFree, InflatePops); + std::vector moreRsp = StoreBelowRsp; + append(moreRsp, {0x48, 0x83, 0xec, 0x08}); + const auto input = image({framelessLeaf(InflatePrologue, StoreBelowRsp, InflatePops), framelessLeaf(InflatePrologue, StoreBelowRsp, wrongFree), framelessLeaf(InflatePrologue, moreRsp, inflateExit())}); + Codegen::RedZoneFrameReport report; + std::vector displacements; + const auto sites = Codegen::FrameRedZoneFunctions(input.bytes, input.segments, input.functions, {}, report, displacements); + require(sites.empty() && displacements.empty() && report.RedZoneFunctions == 3, "an allocating leaf without a matching free, or with more rsp arithmetic, was framed"); + require(report.Skipped.at("exit does not free the prologue's allocation") == 1, "an exit that does not free the allocation was not reported with its own reason"); + require(report.Skipped.at("other use of rsp") == 2, "an add of another size before the pops, or more rsp arithmetic in the body, was not reported as another use of rsp"); +} + +Image calledThenUncalled(const std::vector& called, const std::vector& uncalled) { + Image result; + result.bytes = callTo(0, 0x40); + result.bytes.push_back(0xc3); + result.bytes = padTo(result.bytes, 0x40); + append(result.bytes, called); + result.bytes.resize(0x60, 0x90); + append(result.bytes, uncalled); + result.bytes = padTo(result.bytes, 0x80); + result.segments.push_back({1, 5, 0, Base, Base, result.bytes.size(), result.bytes.size(), 16}); + result.functions.push_back({Base + 0x40, Base + 0x40 + called.size()}); + result.functions.push_back({Base + 0x60, Base + 0x60 + uncalled.size()}); + return result; +} + +void framesEachFunctionFromItsOwnFde() { + std::vector body = StoreBelowRsp; + append(body, LoadBelowRsp); + const auto input = calledThenUncalled(framelessLeaf(FramelessPrologue, body, FramelessExit), framelessLeaf(FramelessPrologue, StoreBelowRsp, FramelessExit)); + Codegen::RedZoneFrameReport report; + std::vector displacements; + const auto sites = Codegen::FrameRedZoneFunctions(input.bytes, input.segments, input.functions, {}, report, displacements); + require(report.Functions == 2 && report.Framed == 2 && report.Exits == 2 && sites.size() == 4 && displacements.size() == 3, "two adjacent functions were not framed each by its own FDE"); + require(sites[0].Address < Base + 0x60 && sites[1].Address + sites[1].Length <= Base + 0x60 && displacements[0].Offset < 0x60 && displacements[1].Offset < 0x60, "the first function's frame runs past its FDE end"); + require(sites[2].Address == Base + 0x60 && sites[2].OriginalBytes == FramelessPrologue && displacements[2].Offset > 0x60, "the function reached without a call was not framed from its own FDE start"); +} + +void treatsATailCallPastTheFdeEndAsLeaving() { + std::vector called = FramelessPrologue; + append(called, StoreBelowRsp); + append(called, {0x85, 0xff, 0x75, 0x06}); + append(called, FramelessExit); + append(called, {0x41, 0x5f, 0x41, 0x5e, 0x5b, 0xe9, 0x02, 0x00, 0x00, 0x00}); + const auto input = calledThenUncalled(called, framelessLeaf(FramelessPrologue, StoreBelowRsp, FramelessExit)); + Codegen::RedZoneFrameReport report; + std::vector displacements; + const auto sites = Codegen::FrameRedZoneFunctions(input.bytes, input.segments, input.functions, {}, report, displacements); + require(report.Skipped.at("branch leaves the function") == 1, "the tail call past the FDE end was not reported as leaving the function"); + require(report.Framed == 1 && sites.size() == 2 && sites[0].Address == Base + 0x60, "the tail-called function was not framed from its own FDE start"); +} + +void framesALeafAfterAJumpWithoutInt3FromItsOwnFde() { + std::vector first = FramelessPrologue; + append(first, StoreBelowRsp); + append(first, {0x31, 0xc0, 0xeb, 0x06}); + append(first, FramelessExit); + append(first, {0x48, 0x3b, 0x44, 0x24, 0xf0, 0x73, 0xf3, 0x48, 0xff, 0xc0, 0xeb, 0xf4}); + require(first.size() == 0x20, "the first leaf must end where the second starts"); + std::vector second = StoreBelowRsp; + append(second, LoadBelowRsp); + const auto input = calledThenUncalled(first, framelessLeaf(FramelessPrologue, second, FramelessExit)); + Codegen::RedZoneFrameReport report; + std::vector displacements; + const auto sites = Codegen::FrameRedZoneFunctions(input.bytes, input.segments, input.functions, {}, report, displacements); + require(report.Framed == 2 && report.Exits == 2 && sites.size() == 4 && displacements.size() == 4, "a leaf right after a jmp without int3 padding was not framed on its own"); + require(sites[1].Address + sites[1].Length <= Base + 0x60 && displacements[1].Offset < 0x60, "the leaf ending in a jmp took the next leaf into its body"); + require(sites[2].Address == Base + 0x60 && sites[2].OriginalBytes == FramelessPrologue && displacements[2].Offset > 0x60, "the leaf after the jmp was not framed from its own FDE start"); +} + +void keepsColdBlocksAfterARetInsideTheFde() { + std::vector leaf = FramelessPrologue; + append(leaf, StoreBelowRsp); + append(leaf, {0x85, 0xff, 0x74, 0x12}); + append(leaf, LoadBelowRsp); + append(leaf, FramelessExit); + append(leaf, {0x0f, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00}); + append(leaf, {0x48, 0x8b, 0x54, 0x24, 0xf0}); + append(leaf, FramelessExit); + const auto input = image({leaf}); + Codegen::RedZoneFrameReport report; + std::vector displacements; + const auto sites = Codegen::FrameRedZoneFunctions(input.bytes, input.segments, input.functions, {}, report, displacements); + require(report.Framed == 1 && report.Exits == 2 && sites.size() == 3, "the exit of a cold block after the ret inside the FDE was not framed"); + require(displacements.size() == 3 && displacements[2].Offset == 0x40 + 0x20 + 4 && displacements[2].Bytes == std::vector{0x00}, "the rsp operand of a cold block after the ret inside the FDE was not moved"); +} + +void keepsLoopsAlignedAfterAJump() { + std::vector loop = FramelessPrologue; + append(loop, StoreBelowRsp); + append(loop, {0x31, 0xc0, 0xeb, 0x04, 0x66, 0x90, 0xff, 0xc0, 0x3b, 0x44, 0x24, 0xf0, 0x72, 0xf8}); + append(loop, FramelessExit); + const auto input = image({loop}); + Codegen::RedZoneFrameReport report; + std::vector displacements; + const auto sites = Codegen::FrameRedZoneFunctions(input.bytes, input.segments, input.functions, {}, report, displacements); + require(report.Framed == 1 && sites.size() == 2 && displacements.size() == 2, "a leaf whose loop header is aligned after a jump was not framed"); +} + +void leavesOtherFunctions() { + std::vector nonLeaf = Prologue; + append(nonLeaf, StoreBelowPushes); + append(nonLeaf, {0xff, 0xd0}); + append(nonLeaf, Exit); + const auto input = image({leaf(LoadAbovePushes), nonLeaf}); + Codegen::RedZoneFrameReport report; + std::vector displacements; + const auto sites = Codegen::FrameRedZoneFunctions(input.bytes, input.segments, input.functions, {}, report, displacements); + require(sites.empty() && report.RedZoneFunctions == 0, "a leaf without red-zone use or a function that calls was framed"); +} + +void skipsSitesTakenByOtherStubs() { + const auto input = image({leaf(StoreBelowPushes)}); + Codegen::TrampolineSite other{}; + other.Address = Base + 0x40; + other.Length = 5; + Codegen::RedZoneFrameReport report; + std::vector displacements; + const auto sites = Codegen::FrameRedZoneFunctions(input.bytes, input.segments, input.functions, {other}, report, displacements); + require(sites.empty() && report.Skipped.at("overlaps another stub") == 1, "a site over another stub was framed"); +} + +void framesAPointerOnlyFunctionByItsOwnBounds() { + const auto first = leaf(StoreBelowPushes); + std::vector secondBody = StoreBelowPushes; + append(secondBody, LoadBelowPushes); + const auto second = leaf(secondBody); + std::vector bytes = callTo(0, 0x40); + bytes.push_back(0xc3); + bytes = padTo(bytes, 0x40); + append(bytes, first); + const auto secondStart = bytes.size(); + append(bytes, second); + bytes = padTo(bytes, (bytes.size() + 15) / 16 * 16 + 16); + const std::vector segments{{1, 5, 0, Base, Base, bytes.size(), bytes.size(), 16}}; + const std::vector functions{{Base + 0x40, Base + 0x40 + first.size()}, {Base + secondStart, Base + secondStart + second.size()}}; + Codegen::RedZoneFrameReport report; + std::vector displacements; + const auto sites = Codegen::FrameRedZoneFunctions(bytes, segments, functions, {}, report, displacements); + require(report.Functions == 2 && report.Framed == 2 && report.Exits == 2, "a function reached only through a pointer was not framed by its own bounds"); + require(sites.size() == 4, "two framed leaves need two prologue and two exit sites"); + require(sites[2].Address == Base + secondStart && sites[2].OriginalBytes == Prologue, "the second function's prologue site is not at its own start"); + require(sites[1].Address + sites[1].Length == Base + 0x40 + first.size(), "the first function's exit runs past its end"); +} + +void leavesCodeWithoutAFunctionRange() { + const auto input = image({leaf(StoreBelowPushes)}); + Codegen::RedZoneFrameReport report; + std::vector displacements; + const auto sites = Codegen::FrameRedZoneFunctions(input.bytes, input.segments, {}, {}, report, displacements); + require(sites.empty() && report.Functions == 0, "code without a function range was framed"); +} + +} + +int main() { + try { + framesRedZoneLeaf(); + leavesOtherFunctions(); + skipsSitesTakenByOtherStubs(); + framesLeafWithoutFramePointer(); + shiftsByTheDeepestOperandBelowRsp(); + movesPositiveOperandsThatFitAfterTheShift(); + shiftsByTheWholeRedZoneForAddressesBelowRsp(); + shiftsByTheWholeRedZoneForIndexedOperands(); + framesLeavesThatAllocateBelowTheirPushes(); + framesLeavesThatAllocateWithLeaOrALargeSub(); + skipsAllocatingLeavesWithoutAMatchingFree(); + stubsOperandsWhoseDisplacementOverflows(); + stubsOperandsAtRspWithTheInstructionsAfterThem(); + stubsVexOperandsAtRsp(); + stubsOperandsAtRspWithTheInstructionsBeforeThem(); + stubsShortOperandsAtRspBeforeABranchWithTheInstructionsBeforeThem(); + skipsShortOperandsAtRspWhoseWidenedStubCoversABranchTarget(); + skipsLeavesWithOperandsThatCannotMove(); + skipsLeavesThatCopyRsp(); + framesEachFunctionFromItsOwnFde(); + treatsATailCallPastTheFdeEndAsLeaving(); + framesALeafAfterAJumpWithoutInt3FromItsOwnFde(); + keepsColdBlocksAfterARetInsideTheFde(); + keepsLoopsAlignedAfterAJump(); + framesAPointerOnlyFunctionByItsOwnBounds(); + leavesCodeWithoutAFunctionRange(); + std::cout << "red-zone framer tests passed\n"; + return 0; + } catch (const std::exception& error) { + std::cerr << "red-zone framer tests failed: " << error.what() << '\n'; + return 1; + } +} diff --git a/core/relinker/codegen/tests/RewriteBoundaryTests.cpp b/core/relinker/codegen/tests/RewriteBoundaryTests.cpp index fa9ee00553..dd1bd9a185 100644 --- a/core/relinker/codegen/tests/RewriteBoundaryTests.cpp +++ b/core/relinker/codegen/tests/RewriteBoundaryTests.cpp @@ -2,26 +2,114 @@ #include #include -static void Require(bool ok) { if (!ok) throw std::runtime_error("Invalid rewrite boundary accepted"); } +static void Require(bool ok) { + if (!ok) + throw std::runtime_error("Invalid rewrite boundary accepted"); +} + +static void RequireTruncatedBranchRejected(bool ok) { + if (!ok) + throw std::runtime_error( + "Length-changing rewrite accepted a section with a truncated relative branch"); +} int main() { try { const Codegen::X64InstructionRewriter rewriter; - for (const auto& code : {std::vector{0xB8, 0x90, 0x90, 0x90, 0x90, 0xC3}, - std::vector{0x48, 0x8B, 0x05, 0x90, 0x90, 0x90, 0x90, 0xC3}, - std::vector{0xE8, 0x90, 0x90, 0x90, 0x90, 0xC3}}) { + + for (const auto& code : { + std::vector{ + 0xB8, 0x90, 0x90, 0x90, 0x90, 0xC3}, + std::vector{ + 0x48, 0x8B, 0x05, 0x90, 0x90, 0x90, 0x90, 0xC3}, + std::vector{ + 0xE8, 0x90, 0x90, 0x90, 0x90, 0xC3} + }) { const auto original = code; - for (std::size_t offset = 1; offset + 1 < code.size(); ++offset) { + + for (std::size_t offset = 1; + offset + 1 < code.size(); + ++offset) { bool rejected = false; - try { (void)rewriter.Rewrite(code, {offset, {0x90}}); } - catch (const std::exception&) { rejected = true; } + + try { + (void)rewriter.Rewrite(code, {offset, {0x90}}); + } catch (const std::exception&) { + rejected = true; + } + Require(rejected && code == original); } + const auto result = rewriter.Rewrite(code, {0, {0x90}}); - Require(result.Bytes == std::vector{0x90, 0xC3}); + + Require(result.Bytes == std::vector{ + 0x90, 0xC3}); } - const auto result = rewriter.Rewrite({0x90, 0x90, 0xC3}, {1, {0x90, 0x90}}); - Require(result.Bytes == std::vector{0x90, 0x90, 0x90, 0xC3}); - std::cout << "18 rewrite boundary cases passed\n"; - } catch (const std::exception& error) { std::cerr << error.what() << '\n'; return 1; } + + const auto result = rewriter.Rewrite( + {0x90, 0x90, 0xC3}, + {1, {0x90, 0x90}}); + + Require(result.Bytes == std::vector{ + 0x90, 0x90, 0x90, 0xC3}); + + for (const auto& code : { + std::vector{ + 0x66, 0xEB, 0x01, 0x90, 0xC3}, + std::vector{ + 0x90, 0x66, 0xEB, 0xFC, 0xC3} + }) { + bool rejected = false; + + try { + const auto offset = + code[0] == 0x66 ? std::size_t{3} : std::size_t{0}; + + (void)rewriter.Rewrite(code, {offset, {0x66, 0x90}}); + } catch (const std::exception&) { + rejected = true; + } + + RequireTruncatedBranchRejected(rejected); + } + + const auto unaffected = rewriter.Rewrite( + {0x66, 0xEB, 0x00, 0x90, 0x90, 0xC3}, + {4, {0x66, 0x90}}); + + Require(unaffected.Bytes == std::vector{ + 0x66, 0xEB, 0x00, 0x90, 0x66, 0x90, 0xC3}); + + std::vector forwardWraparound(0x10004, 0x90); + forwardWraparound[0xFFFA] = 0x66; + forwardWraparound[0xFFFB] = 0xEB; + forwardWraparound[0xFFFC] = 0x03; + + bool forwardWraparoundRejected = false; + try { + (void)rewriter.Rewrite(forwardWraparound, {10, {0x66, 0x90}}); + } catch (const std::exception&) { + forwardWraparoundRejected = true; + } + RequireTruncatedBranchRejected(forwardWraparoundRejected); + + std::vector backwardWraparound(0x10004, 0x90); + backwardWraparound[0] = 0x66; + backwardWraparound[1] = 0xEB; + backwardWraparound[2] = 0xFC; + + bool backwardWraparoundRejected = false; + try { + (void)rewriter.Rewrite(backwardWraparound, {0xFFFE, {0x66, 0x90}}); + } catch (const std::exception&) { + backwardWraparoundRejected = true; + } + RequireTruncatedBranchRejected(backwardWraparoundRejected); + + std::cout << "21 rewrite boundary cases passed\n"; + } catch (const std::exception& error) { + std::cerr << error.what() << '\n'; + return 1; + } } diff --git a/core/relinker/elfpatcher/include/elfpatcher/general/ElfConstants.hpp b/core/relinker/elfpatcher/include/elfpatcher/general/ElfConstants.hpp index 78ed999eab..b8b6b853b6 100644 --- a/core/relinker/elfpatcher/include/elfpatcher/general/ElfConstants.hpp +++ b/core/relinker/elfpatcher/include/elfpatcher/general/ElfConstants.hpp @@ -139,20 +139,14 @@ inline constexpr std::int64_t DT_OS_SYMTABSZ = 0x6100003f; inline constexpr std::uint32_t R_X86_64_NONE = 0; inline constexpr std::uint32_t R_X86_64_64 = 1; inline constexpr std::uint32_t R_X86_64_PC32 = 2; -inline constexpr std::uint32_t R_X86_64_GOT32 = 3; -inline constexpr std::uint32_t R_X86_64_PLT32 = 4; inline constexpr std::uint32_t R_X86_64_COPY = 5; inline constexpr std::uint32_t R_X86_64_GLOB_DAT = 6; inline constexpr std::uint32_t R_X86_64_JUMP_SLOT = 7; inline constexpr std::uint32_t R_X86_64_RELATIVE = 8; -inline constexpr std::uint32_t R_X86_64_GOTPCREL = 9; inline constexpr std::uint32_t R_X86_64_32 = 10; -inline constexpr std::uint32_t R_X86_64_32S = 11; inline constexpr std::uint32_t R_X86_64_DTPMOD64 = 16; inline constexpr std::uint32_t R_X86_64_DTPOFF64 = 17; inline constexpr std::uint32_t R_X86_64_TPOFF64 = 18; -inline constexpr std::uint32_t R_X86_64_GOTPCRELX = 41; -inline constexpr std::uint32_t R_X86_64_REX_GOTPCRELX = 42; inline constexpr std::uint32_t R_X86_64_IRELATIVE = 37; inline constexpr std::uint8_t STB_GLOBAL = 1; diff --git a/core/relinker/elfpatcher/include/elfpatcher/general/GuestModuleWriter.hpp b/core/relinker/elfpatcher/include/elfpatcher/general/GuestModuleWriter.hpp index 5ca491e9bf..1dcc28bbd0 100644 --- a/core/relinker/elfpatcher/include/elfpatcher/general/GuestModuleWriter.hpp +++ b/core/relinker/elfpatcher/include/elfpatcher/general/GuestModuleWriter.hpp @@ -9,7 +9,7 @@ namespace Elfpatcher { class GuestModuleWriter { public: - std::vector WriteLinux(const Relinker::GuestImage& image, const std::vector& dependencies, const std::string& runPath) const; + std::vector WriteLinux(const Relinker::GuestImage& image, const std::vector& dependencies, const std::string& runPath, bool deferInitialization = false) const; std::vector WriteWindows(const Relinker::GuestImage& image, Domain::GuestRuntime& runtime, const std::function& message = {}) const; std::vector WriteMacOs(const Relinker::GuestImage& image, const std::vector& dependencies, const std::string& runPath, const std::function& message = {}) const; }; diff --git a/core/relinker/elfpatcher/src/general/ProgramHeaderLayoutBuilder.cpp b/core/relinker/elfpatcher/src/general/ProgramHeaderLayoutBuilder.cpp index fc23e28dea..f53da5a100 100644 --- a/core/relinker/elfpatcher/src/general/ProgramHeaderLayoutBuilder.cpp +++ b/core/relinker/elfpatcher/src/general/ProgramHeaderLayoutBuilder.cpp @@ -13,6 +13,8 @@ namespace Elfpatcher { namespace { +constexpr std::size_t kExtendedProgramHeaderCount = 0xffff; + bool hasWritableLoadCoverage( const std::vector& headers, const Domain::ProgramHeader& relro @@ -152,6 +154,8 @@ std::vector ProgramHeaderLayoutBuilder::_outputHeaders(co auto kept = _keptHeaders(originalHeaders); if (std::none_of(kept.begin(), kept.end(), [](const Domain::ProgramHeader& ph) { return ph.Type == PT_GNU_STACK; })) kept.push_back(Domain::ProgramHeader{PT_GNU_STACK, PF_R | PF_W, 0, 0, 0, 0, 0, 16}); + if (kept.size() >= kExtendedProgramHeaderCount - kSyntheticProgramHeaderCount) + throw Domain::RelinkerException("Extended program header counts are not supported"); return kept; } diff --git a/core/relinker/elfpatcher/src/linux/LinuxElfPatcher.cpp b/core/relinker/elfpatcher/src/linux/LinuxElfPatcher.cpp index 7453d82aca..b2f84ae52d 100644 --- a/core/relinker/elfpatcher/src/linux/LinuxElfPatcher.cpp +++ b/core/relinker/elfpatcher/src/linux/LinuxElfPatcher.cpp @@ -85,7 +85,6 @@ std::vector RebasedRelocations(const std::vector& ta case R_X86_64_GLOB_DAT: case R_X86_64_JUMP_SLOT: case R_X86_64_32: - case R_X86_64_32S: case R_X86_64_DTPMOD64: case R_X86_64_DTPOFF64: case R_X86_64_TPOFF64: diff --git a/core/relinker/elfpatcher/src/linux/LinuxGuestModuleWriter.cpp b/core/relinker/elfpatcher/src/linux/LinuxGuestModuleWriter.cpp index 76b91f97dc..316a1cc318 100644 --- a/core/relinker/elfpatcher/src/linux/LinuxGuestModuleWriter.cpp +++ b/core/relinker/elfpatcher/src/linux/LinuxGuestModuleWriter.cpp @@ -9,7 +9,7 @@ namespace Elfpatcher { -std::vector GuestModuleWriter::WriteLinux(const Relinker::GuestImage& image, const std::vector& dependencies, const std::string& runPath) const { +std::vector GuestModuleWriter::WriteLinux(const Relinker::GuestImage& image, const std::vector& dependencies, const std::string& runPath, const bool deferInitialization) const { auto bytes = image.Bytes; std::vector headers; std::uint64_t end = 0; @@ -35,7 +35,7 @@ std::vector GuestModuleWriter::WriteLinux(const Relinker::GuestIma const auto extraOffset = bytes.size(); const auto extraAddress = Io::AlignUp64(end, 0x4000); const auto address = [&] { return extraAddress + bytes.size() - extraOffset; }; - const bool passesModuleArgs = image.HasInit; + const bool passesModuleArgs = image.HasInit && !deferInitialization; std::uint64_t argsSlot = 0; std::uint64_t resultSlot = 0; if (passesModuleArgs) { @@ -83,6 +83,39 @@ std::vector GuestModuleWriter::WriteLinux(const Relinker::GuestIma Io::WriteU64(relaData, entry, slot); Io::WriteU64(relaData, entry + 8, (symbol << 32) | kGlobDat); }; + std::uint64_t lifecycleTable = 0; + if (deferInitialization) { + constexpr char exportName[] = "__aps5_guest_lifecycle_nid_no_patch"; + if (std::any_of(image.Symbols.begin(), image.Symbols.end(), [&](const auto& symbol) { return symbol.Name == exportName; })) + throw Domain::RelinkerException("Reserved guest lifecycle export is already present"); + Io::AlignBuffer(bytes, 8); + lifecycleTable = address(); + const auto pointer = [&](std::uint64_t target, bool relocate = true) { + const auto slot = address(); + Io::AppendU64(bytes, 0); + if (!relocate) return; + Io::AppendU64(relaData, slot); + Io::AppendU64(relaData, 8); + Io::AppendU64(relaData, target); + }; + Io::AppendU64(bytes, 0x415053354c494658ull); + pointer(0); + pointer(image.Init, image.HasInit); + pointer(image.Fini, image.Fini != 0); + Io::AppendU64(bytes, image.InitArray.size()); + Io::AppendU64(bytes, image.FiniArray.size()); + Io::AppendU64(bytes, 0); + Io::AppendU64(bytes, 0); + for (const auto slot : image.InitArray) pointer(slot); + for (const auto slot : image.FiniArray) pointer(slot); + const auto entry = symbols.size(); + symbols.resize(entry + 24, 0); + Io::WriteU32(symbols, entry, addString(exportName)); + symbols[entry + 4] = 0x11; + Io::WriteU16(symbols, entry + 6, 1); + Io::WriteU64(symbols, entry + 8, lifecycleTable); + Io::WriteU64(symbols, entry + 16, address() - lifecycleTable); + } if (passesModuleArgs) { argsImport = addUndefinedImport("__aps5_get_pending_module_args_nid_no_patch"); resultImport = addUndefinedImport("__aps5_set_module_init_result_nid_no_patch"); @@ -156,8 +189,33 @@ std::vector GuestModuleWriter::WriteLinux(const Relinker::GuestIma Io::AppendU32(bytes, static_cast(distance)); return start; }; - const auto init = lifecycle(image.Init); - const auto fini = finalizer(image.Fini); + const auto init = deferInitialization ? 0 : lifecycle(image.Init); + auto fini = deferInitialization ? 0 : finalizer(image.Fini); + if (deferInitialization && (image.Fini != 0 || !image.FiniArray.empty())) { + fini = address(); + bytes.insert(bytes.end(), {0x48, 0x83, 0x3d}); + Io::AppendU32(bytes, stubDisplacement(lifecycleTable + 48, address() + 5)); + bytes.push_back(2); + bytes.insert(bytes.end(), {0x0f, 0x85}); + const auto skip = bytes.size(); + Io::AppendU32(bytes, 0); + bytes.insert(bytes.end(), {0x48, 0xc7, 0x05}); + Io::AppendU32(bytes, stubDisplacement(lifecycleTable + 48, address() + 8)); + Io::AppendU32(bytes, 4); + bytes.insert(bytes.end(), {0x48, 0x83, 0xec, 8}); + for (auto slot = image.FiniArray.rbegin(); slot != image.FiniArray.rend(); ++slot) { + bytes.insert(bytes.end(), {0x48, 0x8b, 0x05}); + Io::AppendU32(bytes, stubDisplacement(*slot, address() + 4)); + bytes.insert(bytes.end(), {0x48, 0x85, 0xc0, 0x74, 8, 0x31, 0xff, 0x31, 0xf6, 0x31, 0xd2, 0xff, 0xd0}); + } + if (image.Fini != 0) { + bytes.insert(bytes.end(), {0x31, 0xff, 0x31, 0xf6, 0x31, 0xd2, 0xe8}); + Io::AppendU32(bytes, stubDisplacement(image.Fini, address() + 4)); + } + bytes.insert(bytes.end(), {0x48, 0x83, 0xc4, 8}); + Io::WriteU32(bytes, skip, static_cast(bytes.size() - skip - 4)); + bytes.push_back(0xc3); + } Io::AlignBuffer(bytes, 8); const auto dynamicOffset = bytes.size(); const auto dynamicAddress = address(); @@ -181,8 +239,8 @@ std::vector GuestModuleWriter::WriteLinux(const Relinker::GuestIma tag(20, 7); tag(3, image.Got); } - if (!image.InitArray.empty()) { tag(25, image.InitArray.front()); tag(27, image.InitArray.size() * 8); } - if (!image.FiniArray.empty()) { tag(26, image.FiniArray.front()); tag(28, image.FiniArray.size() * 8); } + if (!deferInitialization && !image.InitArray.empty()) { tag(25, image.InitArray.front()); tag(27, image.InitArray.size() * 8); } + if (!deferInitialization && !image.FiniArray.empty()) { tag(26, image.FiniArray.front()); tag(28, image.FiniArray.size() * 8); } if (init != 0) tag(12, init); if (fini != 0) tag(13, fini); tag(30, DF_BIND_NOW | (image.Symbolic ? DF_SYMBOLIC : 0)); diff --git a/core/relinker/elfpatcher/src/macos/MacOsImage.cpp b/core/relinker/elfpatcher/src/macos/MacOsImage.cpp index 43a00cd7af..b864c5640e 100644 --- a/core/relinker/elfpatcher/src/macos/MacOsImage.cpp +++ b/core/relinker/elfpatcher/src/macos/MacOsImage.cpp @@ -87,11 +87,13 @@ constexpr std::uint8_t EntryStub[] = { 0x4c, 0xc4, 0x08, 0x49, 0xff, 0xc0, 0xeb, 0xed, 0x48, 0xc7, 0x44, 0xfc, 0x08, 0x00, 0x00, 0x00, 0x00, 0x4c, 0x8d, 0x54, 0xfc, 0x10, 0x45, 0x31, 0xc0, 0x49, 0x39, 0xc8, 0x74, 0x0d, 0x4e, 0x8b, 0x0c, 0xc2, 0x4f, 0x89, 0x0c, 0xc2, 0x49, 0xff, 0xc0, 0xeb, 0xee, 0x49, 0xc7, 0x04, 0xca, 0x00, - 0x00, 0x00, 0x00, 0x48, 0x89, 0xe6, 0x48, 0x8d, 0x3d, 0x00, 0x00, 0x00, 0x00, 0xff, 0x15, 0x00, - 0x00, 0x00, 0x00, 0x0f, 0x0b, + 0x00, 0x00, 0x00, 0x48, 0x89, 0xe6, 0x48, 0x8d, 0x3d, 0x00, 0x00, 0x00, 0x00, 0x48, 0x83, 0xec, + 0x10, 0x0f, 0xae, 0x1c, 0x24, 0xc7, 0x44, 0x24, 0x04, 0xc0, 0x9f, 0x00, 0x00, 0x0f, 0xae, 0x54, + 0x24, 0x04, 0xff, 0x15, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xae, 0x14, 0x24, 0x48, 0x83, 0xc4, 0x10, + 0x0f, 0x0b, }; constexpr std::size_t EntryStubEntryDisplacement = 0x69; -constexpr std::size_t EntryStubStartDisplacement = 0x6f; +constexpr std::size_t EntryStubStartDisplacement = 0x84; struct Section { std::string Name; @@ -423,6 +425,9 @@ std::vector WriteMacOsImage(MacOsImageInput& input) { const auto guestSegments = segments.size(); auto sections = image.BuildSections(); for (const auto& trap : tls.GsTraps) { + const auto target = tls.BranchTargets.upper_bound(trap.Rva); + if (target != tls.BranchTargets.end() && *target < trap.Rva + trap.Length) + throw Domain::RelinkerException("Branch enters a guest GS instruction", *target); if (input.Trampolines != nullptr) { for (const auto& site : *input.Trampolines) { const auto siteRva = image.GetRva(site.Address, site.Length); diff --git a/core/relinker/elfpatcher/src/windows/WindowsEntryStubBuilder.cpp b/core/relinker/elfpatcher/src/windows/WindowsEntryStubBuilder.cpp index dbcca232a2..2be086cc21 100644 --- a/core/relinker/elfpatcher/src/windows/WindowsEntryStubBuilder.cpp +++ b/core/relinker/elfpatcher/src/windows/WindowsEntryStubBuilder.cpp @@ -157,6 +157,7 @@ WindowsEntryStub WindowsEntryStubBuilder::Build(const std::uint32_t dataRva, con std::vector searchCounts; std::vector platformTlsResolvers; std::vector imported(libraries.size()); + std::vector executableImports(guestModules.size()); const bool unscopedImports = std::any_of(imports.begin(), imports.end(), [](const auto& import) { return import.Library.empty(); }); for (const auto& import : imports) { const auto library = Domain::NeededLibraryName(import.Library); @@ -167,6 +168,7 @@ WindowsEntryStub WindowsEntryStubBuilder::Build(const std::uint32_t dataRva, con const bool guestName = index < guestModules.size() && std::find(guestModules[index].Names.begin(), guestModules[index].Names.end(), import.Library) != guestModules[index].Names.end(); if (import.Library.empty() || name == library || guestName) order.push_back(CheckedRva(index)); + if (import.TargetModule < 0 && !import.Library.empty() && index < guestModules.size() && (name == library || guestName)) executableImports[index] = true; } if (order.empty()) throw Domain::RelinkerException("Import module is not a dependency: " + import.Library); const bool hostModule = !import.Library.empty() && @@ -249,6 +251,11 @@ WindowsEntryStub WindowsEntryStubBuilder::Build(const std::uint32_t dataRva, con const auto searched = addString("Searched libraries:\n"); const auto indent = addString(" "); const auto enteringElf = addString("Transferring control to ELF entry point\n"); + std::vector deferredImports; + for (std::size_t index = 0; index < guestModules.size(); ++index) { + if (guestModules[index].DeferInitialization && executableImports[index]) + deferredImports.push_back(addString("Deferred module " + guestModules[index].Path + ": the executable imports from it, but it starts only when the title loads it\n")); + } std::vector resolvedPaths; for (std::size_t index = 0; index < libraries.size(); ++index) resolvedPaths.push_back(reserve(PathCapacity)); @@ -717,6 +724,7 @@ WindowsEntryStub WindowsEntryStubBuilder::Build(const std::uint32_t dataRva, con code.Emit({0x85, 0xc0}); unresolvedBranches.push_back(code.Branch({0x0f, 0x85})); } + for (const auto message : deferredImports) writeString(message); guestStartup.Initialize(code, guestModules, handles); writeString(enteringElf); code.Rip({0x48, 0x8d, 0x0d}, shellLibrary); diff --git a/core/relinker/elfpatcher/tests/ProgramLayoutRangeTests.cpp b/core/relinker/elfpatcher/tests/ProgramLayoutRangeTests.cpp index 18748830de..c86b61da23 100644 --- a/core/relinker/elfpatcher/tests/ProgramLayoutRangeTests.cpp +++ b/core/relinker/elfpatcher/tests/ProgramLayoutRangeTests.cpp @@ -149,6 +149,27 @@ int main() { bool stackSlotRejected = false; try { (void)writeSingle(6); } catch (const std::exception&) { stackSlotRejected = true; } Require(stackSlotRejected, "layout must refuse to drop the synthetic stack when its slot is missing"); - std::cout << "21 program layout cases passed\n"; + constexpr std::uint16_t extendedCount = 0xffff; + std::vector manyHeaders(extendedCount - Elfpatcher::kSyntheticProgramHeaderCount - 2); + manyHeaders.front() = singleLoad.front(); + Require(builder.RequiredProgramHeaderCount(manyHeaders) == extendedCount - 1, "largest ordinary count changed"); + const auto writeMany = [&builder](const std::vector& headers, std::vector& bytes, std::uint16_t count) { + const std::uint64_t extraOffset = 0x1000; + const std::uint64_t extraSize = static_cast(count) * 56; + Elfpatcher::ProgramHeaderLayoutRequest request{extraOffset, 56, count, headers, extraOffset, + builder.ComputeExtraBlockVaddr(headers, extraOffset), extraSize, extraOffset, 16, extraOffset + 16, 8}; + return builder.WriteLayout(bytes, request); + }; + std::vector largeOutput(0x1000 + static_cast(extendedCount) * 56, 0xA5); + Require(writeMany(manyHeaders, largeOutput, extendedCount - 1) == extendedCount - 1, "largest ordinary layout rejected"); + manyHeaders.push_back({}); + rejected = false; + try { builder.RequiredProgramHeaderCount(manyHeaders); } catch (const Domain::RelinkerException&) { rejected = true; } + Require(rejected, "extended program header count accepted"); + const auto beforeLarge = largeOutput; + rejected = false; + try { writeMany(manyHeaders, largeOutput, extendedCount); } catch (const Domain::RelinkerException&) { rejected = true; } + Require(rejected && largeOutput == beforeLarge, "extended count written without section-zero encoding"); + std::cout << "25 program layout cases passed\n"; } catch (const std::exception& error) { std::cerr << error.what() << '\n'; return 1; } } diff --git a/core/relinker/elfpatcher/tests/WindowsDependencyDiagnosticsTests.cpp b/core/relinker/elfpatcher/tests/WindowsDependencyDiagnosticsTests.cpp index 15197aa868..45ce2bf954 100644 --- a/core/relinker/elfpatcher/tests/WindowsDependencyDiagnosticsTests.cpp +++ b/core/relinker/elfpatcher/tests/WindowsDependencyDiagnosticsTests.cpp @@ -242,8 +242,8 @@ void expectDiagnostic(const Fs::path& runner, const std::vector& ex int main() { try { checkRuntimeDependencies(); - std::vector filename(32768); - const auto size = GetModuleFileNameA(nullptr, filename.data(), static_cast(filename.size())); + std::vector filename(32768); + const auto size = GetModuleFileNameW(nullptr, filename.data(), static_cast(filename.size())); if (size == 0 || size >= filename.size()) throw std::runtime_error("Cannot locate diagnostic tests"); const auto directory = createFixtureDirectory(Fs::path(filename.data()).parent_path()); diff --git a/core/relinker/io/src/FileIdentity.cpp b/core/relinker/io/src/FileIdentity.cpp index 351058935a..5f2b5cc967 100644 --- a/core/relinker/io/src/FileIdentity.cpp +++ b/core/relinker/io/src/FileIdentity.cpp @@ -47,6 +47,18 @@ bool SameFile(const std::filesystem::path& first, const std::filesystem::path& s #ifdef _WIN32 return FileIdentity(first) == FileIdentity(second); #else + for (const auto* path : {&first, &second}) { + std::error_code error; + const auto status = std::filesystem::status(*path, error); + if (error) throw std::filesystem::filesystem_error("Cannot determine file identity", *path, error); + if (!std::filesystem::exists(status)) { + throw std::filesystem::filesystem_error( + "Cannot determine file identity", + *path, + std::make_error_code(std::errc::no_such_file_or_directory) + ); + } + } return std::filesystem::equivalent(first, second); #endif } diff --git a/core/relinker/io/tests/FileIdentityTests.cpp b/core/relinker/io/tests/FileIdentityTests.cpp index 41e6bdc5e7..763b71af1c 100644 --- a/core/relinker/io/tests/FileIdentityTests.cpp +++ b/core/relinker/io/tests/FileIdentityTests.cpp @@ -17,10 +17,16 @@ void write(const std::filesystem::path& path, const std::string& text) { require(std::filesystem::is_regular_file(path), "Cannot create " + path.string()); } -void requireMissing(const std::filesystem::path& first, const std::filesystem::path& second) { +void requireMissing( + const std::filesystem::path& first, + const std::filesystem::path& second, + const std::filesystem::path& missing +) { try { Io::SameFile(first, second); - } catch (const std::filesystem::filesystem_error&) { + } catch (const std::filesystem::filesystem_error& error) { + require(error.code() == std::errc::no_such_file_or_directory, "SameFile reported the wrong error for a missing path"); + require(error.path1() == missing, "SameFile reported the wrong missing path"); return; } throw std::runtime_error("SameFile accepted a missing file"); @@ -41,8 +47,9 @@ void run(const std::filesystem::path& directory) { require(!Io::SameFile(first, copy), "A copy with the same contents is the same file"); require(Io::SameFile(folder, directory / "folder" / "."), "Two spellings of one directory differ"); require(!Io::SameFile(first, folder), "A file is the same as a directory"); - requireMissing(directory / "missing.bin", first); - requireMissing(first, directory / "missing.bin"); + const auto missing = directory / "missing.bin"; + requireMissing(missing, first, missing); + requireMissing(first, missing, missing); } } diff --git a/core/relinker/main.cpp b/core/relinker/main.cpp index 91c5b09ee7..6f2a15fe2e 100644 --- a/core/relinker/main.cpp +++ b/core/relinker/main.cpp @@ -57,6 +57,7 @@ ConvertedExecutable Convert(const Cli::Args& args, const std::string& inputPath, options.Target = args.toWindows ? Relinker::TargetSystem::Windows : args.toMacos ? Relinker::TargetSystem::MacOs : Relinker::TargetSystem::Linux; options.ToIntel = args.toIntel; options.ToRosetta = args.toRosetta; + options.FrameRedZone = args.frameRedZone; options.SkipSyscallCheck = args.skipSyscallCheck; options.TrapSdkSyscallStubs = args.trapSdkSyscallStubs; options.UnusedFilterLevel = args.unusedFilterLevel; diff --git a/core/relinker/relinker/include/relinker/guest/GuestImage.hpp b/core/relinker/relinker/include/relinker/guest/GuestImage.hpp index 6be45157af..8fe4507a89 100644 --- a/core/relinker/relinker/include/relinker/guest/GuestImage.hpp +++ b/core/relinker/relinker/include/relinker/guest/GuestImage.hpp @@ -57,6 +57,7 @@ struct GuestImage { std::uint64_t Got = 0; bool UsePlatformTlsResolver = true; bool Symbolic = false; + std::vector NamedImports; }; class GuestImageReader { diff --git a/core/relinker/relinker/include/relinker/patching/GamePatch.hpp b/core/relinker/relinker/include/relinker/patching/GamePatch.hpp index 6b76048dfb..e04f9605e3 100644 --- a/core/relinker/relinker/include/relinker/patching/GamePatch.hpp +++ b/core/relinker/relinker/include/relinker/patching/GamePatch.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include #include @@ -83,6 +84,8 @@ struct WriterSites { std::span Traps; std::span RelaData; std::span RelaPltData; + std::span RedZoneSites = {}; + std::span RedZoneDisplacements = {}; }; enum class VerifyStatus { Applicable, Applied, NotChecked, Mismatch }; diff --git a/core/relinker/relinker/include/relinker/pipeline/Conversion.hpp b/core/relinker/relinker/include/relinker/pipeline/Conversion.hpp index 6998e95873..3a45a1ea10 100644 --- a/core/relinker/relinker/include/relinker/pipeline/Conversion.hpp +++ b/core/relinker/relinker/include/relinker/pipeline/Conversion.hpp @@ -15,6 +15,7 @@ struct ConversionOptions { std::uint32_t UnusedFilterLevel = 0; bool ToIntel = false; bool ToRosetta = false; + bool FrameRedZone = false; bool SkipSyscallCheck = false; bool TrapSdkSyscallStubs = false; bool LazyBinding = false; diff --git a/core/relinker/relinker/src/analysis/CallSiteResolver.cpp b/core/relinker/relinker/src/analysis/CallSiteResolver.cpp index 91fe531f9c..b615250ae8 100644 --- a/core/relinker/relinker/src/analysis/CallSiteResolver.cpp +++ b/core/relinker/relinker/src/analysis/CallSiteResolver.cpp @@ -26,7 +26,7 @@ std::optional ripRelativeDispOffset(const std::vector while (cur < end) { std::uint8_t b = text[cur]; if (b >= RexMin && b <= RexMax) { - if (b & RexWBit) hasRexW = true; + hasRexW = (b & RexWBit) != 0; ++cur; continue; } diff --git a/core/relinker/relinker/src/analysis/UnusedNidFilter/ControlFlowGraph.cpp b/core/relinker/relinker/src/analysis/UnusedNidFilter/ControlFlowGraph.cpp index da812ba58e..d8c89bcffb 100644 --- a/core/relinker/relinker/src/analysis/UnusedNidFilter/ControlFlowGraph.cpp +++ b/core/relinker/relinker/src/analysis/UnusedNidFilter/ControlFlowGraph.cpp @@ -103,13 +103,16 @@ void buildGraph(std::span text, VirtualAddress textVaddr, Vi if (followCodeAddresses && info.HasRipRelativeDisp && text[bufOff + info.OpcodeOffset] == 0x8d && (info.RexPrefix & 8)) { std::int32_t displacement = 0; std::memcpy(&displacement, text.data() + bufOff + info.RipRelativeDispOffset, 4); - enqueue(nextVaddr + static_cast(static_cast(displacement))); + auto target = nextVaddr + static_cast(static_cast(displacement)); + if (info.AddressSize32) target = static_cast(target); + enqueue(target); } if (data != nullptr && info.HasRipRelativeDisp && !info.IsTwoByteOpcode && info.Opcode == 0x8D) { std::int32_t displacement = 0; std::memcpy(&displacement, text.data() + bufOff + info.RipRelativeDispOffset, 4); - const auto base = nextVaddr + static_cast(static_cast(displacement)); + auto base = nextVaddr + static_cast(static_cast(displacement)); + if (info.AddressSize32) base = static_cast(base); takeAddress(base); for (VirtualAddress target : ReadRelativeTableTargets(*data, base, textVaddr, text.size())) takeAddress(target); } @@ -129,6 +132,7 @@ void buildGraph(std::span text, VirtualAddress textVaddr, Vi std::int32_t disp = 0; std::memcpy(&disp, text.data() + bufOff + info.RipRelativeDispOffset, 4); VirtualAddress slotVaddr = nextVaddr + static_cast(static_cast(disp)); + if (info.AddressSize32) slotVaddr = static_cast(slotVaddr); auto resolved = relativeRelocations.TargetOfSlot(slotVaddr); if (resolved.has_value()) enqueue(*resolved); @@ -141,6 +145,7 @@ void buildGraph(std::span text, VirtualAddress textVaddr, Vi std::int32_t disp = 0; std::memcpy(&disp, text.data() + bufOff + info.RipRelativeDispOffset, 4); VirtualAddress slotVaddr = nextVaddr + static_cast(static_cast(disp)); + if (info.AddressSize32) slotVaddr = static_cast(slotVaddr); auto resolved = relativeRelocations.TargetOfSlot(slotVaddr); if (resolved.has_value()) enqueue(*resolved); diff --git a/core/relinker/relinker/src/analysis/UnusedNidFilter/GotAccessIndex.cpp b/core/relinker/relinker/src/analysis/UnusedNidFilter/GotAccessIndex.cpp index 9d1e5a3c2e..18dc887f88 100644 --- a/core/relinker/relinker/src/analysis/UnusedNidFilter/GotAccessIndex.cpp +++ b/core/relinker/relinker/src/analysis/UnusedNidFilter/GotAccessIndex.cpp @@ -43,8 +43,10 @@ std::unique_ptr BuildGotAccessIndex( VirtualAddress nextVaddr = va + static_cast(info.Length); std::int32_t disp = 0; std::memcpy(&disp, text.data() + bufOff + info.RipRelativeDispOffset, 4); - if (disp < 0 && nextVaddr < static_cast(-static_cast(disp))) continue; - if (disp > 0 && std::numeric_limits::max() - nextVaddr < static_cast(disp)) continue; + if (!info.AddressSize32) { + if (disp < 0 && nextVaddr < static_cast(-static_cast(disp))) continue; + if (disp > 0 && std::numeric_limits::max() - nextVaddr < static_cast(disp)) continue; + } VirtualAddress target = nextVaddr + static_cast(static_cast(disp)); if (info.AddressSize32) target = static_cast(target); accessed.insert(target); diff --git a/core/relinker/relinker/src/guest/GuestImageReader.cpp b/core/relinker/relinker/src/guest/GuestImageReader.cpp index a4e2d5308a..6010508de0 100644 --- a/core/relinker/relinker/src/guest/GuestImageReader.cpp +++ b/core/relinker/relinker/src/guest/GuestImageReader.cpp @@ -162,6 +162,8 @@ GuestImage GuestImageReader::Read(const std::filesystem::path& path, std::vector if (!importModules.emplace(value >> 48, string(value & 0xffffffffu)).second) fail("Duplicate import module ID"); std::vector neededLibraries; for (const auto offset : needed) neededLibraries.push_back(string(offset)); + const bool consoleFileNames = std::any_of(neededLibraries.begin(), neededLibraries.end(), + [](const std::string& name) { return Domain::NeededLibraryName(name) != name; }); std::map exports; for (std::uint64_t offset = 0; offset < symSize; offset += 24) { auto name = string(Io::ReadU32(bytes, symOffset + offset)); @@ -195,6 +197,7 @@ GuestImage GuestImageReader::Read(const std::filesystem::path& path, std::vector if (image.Dynamic.DynStrData.size() > std::numeric_limits::max()) fail("String table too large"); Io::WriteU32(image.Dynamic.DynSymData, offset, static_cast(image.Dynamic.DynStrData.size())); Io::AppendString(image.Dynamic.DynStrData, name); + if (consoleFileNames && section == 0 && !name.empty() && name == qualifiedName) image.NamedImports.push_back(image.Symbols.size()); image.Symbols.push_back(symbol); } if (tags.contains(14)) { diff --git a/core/relinker/relinker/src/guest/GuestModuleBuilder.cpp b/core/relinker/relinker/src/guest/GuestModuleBuilder.cpp index 1ffd34e19d..31dc35aa0d 100644 --- a/core/relinker/relinker/src/guest/GuestModuleBuilder.cpp +++ b/core/relinker/relinker/src/guest/GuestModuleBuilder.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -42,6 +43,37 @@ std::vector ReadNeededNames(const Domain::SysVDynamicSection& dynam return names; } +std::string SymbolName(const Domain::SysVDynamicSection& dynamic, const std::size_t index) { + if (dynamic.DynSymData.size() % 24 != 0 || index >= dynamic.DynSymData.size() / 24) throw Domain::RelinkerException("Invalid symbol index"); + const auto nameOffset = Io::ReadU32(dynamic.DynSymData, index * 24); + if (nameOffset >= dynamic.DynStrData.size()) throw Domain::RelinkerException("Invalid symbol name offset"); + const auto start = dynamic.DynStrData.begin() + nameOffset; + const auto end = std::find(start, dynamic.DynStrData.end(), 0); + if (end == dynamic.DynStrData.end()) throw Domain::RelinkerException("Unterminated symbol name"); + return std::string(start, end); +} + +std::set ExportedNames(const Domain::SysVDynamicSection& dynamic) { + std::set names; + for (std::size_t index = 1; index < dynamic.DynSymData.size() / 24; ++index) { + const auto info = dynamic.DynSymData[index * 24 + 4]; + const auto visibility = dynamic.DynSymData[index * 24 + 5] & 3; + if (Io::ReadU16(dynamic.DynSymData, index * 24 + 6) != 0 && (info >> 4) != 0 && visibility != 1 && visibility != 2) + names.insert(SymbolName(dynamic, index)); + } + return names; +} + +void BindNamedImportsByNid(GuestImage& image, const std::set& kept) { + for (const auto index : image.NamedImports) { + const auto name = SymbolName(image.Dynamic, index); + if (name.ends_with(GuestSymbolSuffix) || kept.contains(name)) continue; + if (image.Dynamic.DynStrData.size() > std::numeric_limits::max()) throw Domain::RelinkerException("Guest string table too large"); + Io::WriteU32(image.Dynamic.DynSymData, index * 24, static_cast(image.Dynamic.DynStrData.size())); + Io::AppendString(image.Dynamic.DynStrData, Nid::ComputeNid(name, "")); + } +} + std::string FoldFilename(std::string name) { for (auto& character : name) if (character >= 'A' && character <= 'Z') character = static_cast(character + ('a' - 'A')); return name; @@ -292,12 +324,14 @@ std::vector GuestModuleBuilder::Build(const std::filesystem::path return directory == "." ? std::string("app0") : "app0/" + directory; }; if (!windows) { + const auto executableExports = macos ? std::set{} : ExportedNames(dynamic); for (auto& image : images) { for (std::size_t index = 1; index < image.Symbols.size(); ++index) { const auto& symbol = image.Symbols[index]; const bool exported = symbol.Section != 0 && (symbol.Info >> 4) != 0 && symbol.Visibility != 1 && symbol.Visibility != 2; if (((symbol.Section == 0 && !declaresHost(symbol.Library)) || exported) && exports.contains(symbol.Name)) rename(image.Dynamic.DynSymData, image.Dynamic.DynStrData, index, symbol.Name); } + if (!macos) BindNamedImportsByNid(image, executableExports); } } std::vector order; @@ -314,17 +348,18 @@ std::vector GuestModuleBuilder::Build(const std::filesystem::path std::vector hostLibraries; std::vector> moduleHostLibraries(images.size()); std::set uniqueHosts; + const auto hostLibrary = [&](const std::string& name) { return windows || macos ? name : Domain::NeededLibraryName(name); }; const auto addHost = [&](const std::string& name, const bool startup) { if (findGuest(name) != guestNames.end()) return; if (name.empty() || name.find_first_of("/\\:$") != std::string::npos) throw Domain::RelinkerException("Invalid host dependency: " + name); - if (startup && uniqueHosts.insert(name).second) hostLibraries.push_back(name); + if (const auto host = hostLibrary(name); startup && uniqueHosts.insert(host).second) hostLibraries.push_back(host); }; for (const auto& name : neededNames) addHost(name, true); for (std::size_t index = 0; index < images.size(); ++index) { auto& hosts = moduleHostLibraries[index]; for (const auto& dependency : images[index].Dependencies) { addHost(dependency, windows || !deferred(index)); - if (findGuest(dependency) == guestNames.end()) hosts.push_back(dependency); + if (findGuest(dependency) == guestNames.end()) hosts.push_back(hostLibrary(dependency)); } if (!windows && !macos && images[index].HasInit) { if (std::find(hosts.begin(), hosts.end(), "libkernel.prx") == hosts.end()) hosts.push_back("libkernel.prx"); @@ -404,7 +439,10 @@ std::vector GuestModuleBuilder::Build(const std::filesystem::path needed.push_back("$ORIGIN/" + dependencyPath.lexically_relative(image.SourcePath.parent_path()).generic_string()); } needed.insert(needed.end(), moduleHostLibraries[index].begin(), moduleHostLibraries[index].end()); - output = macos ? Elfpatcher::GuestModuleWriter().WriteMacOs(image, needed, guestRunPath, message) : Elfpatcher::GuestModuleWriter().WriteLinux(image, needed, guestRunPath); + const auto directory = image.SourcePath.parent_path(); + const bool deferLinuxStart = isRuntime(index) && directory != root / "sce_module" && + directory != root / "sce_modules" && directory != root / "prx"; + output = macos ? Elfpatcher::GuestModuleWriter().WriteMacOs(image, needed, guestRunPath, message) : Elfpatcher::GuestModuleWriter().WriteLinux(image, needed, guestRunPath, deferLinuxStart); } if (windows) { for (const auto& [name, provider] : guestNames) { @@ -425,7 +463,7 @@ std::vector GuestModuleBuilder::Build(const std::filesystem::path dynamic.GuestModules.push_back(std::move(runtime)); std::vector neededLibraries; for (const auto& name : image.Dependencies) - if (findGuest(name) == guestNames.end()) neededLibraries.push_back(name); + if (findGuest(name) == guestNames.end()) neededLibraries.push_back(hostLibrary(name)); artifacts.push_back({target, std::move(output), std::move(imports), image.SourcePath, std::move(neededLibraries)}); progress.Report({Domain::ProgressStage::Modules, Io::Utf8Path(image.SourcePath), artifacts.size(), order.size()}); } diff --git a/core/relinker/relinker/src/output/CallRegistryWriter.cpp b/core/relinker/relinker/src/output/CallRegistryWriter.cpp index 8878c6496c..f79f7fa68b 100644 --- a/core/relinker/relinker/src/output/CallRegistryWriter.cpp +++ b/core/relinker/relinker/src/output/CallRegistryWriter.cpp @@ -1,5 +1,6 @@ #include #include +#include #include namespace Relinker { @@ -66,6 +67,7 @@ static std::string _jsonString(const std::string& s) { static std::string _hexOffset(FileByteOffset v) { std::ostringstream oss; + oss.imbue(std::locale::classic()); oss << "\"0x" << std::hex << std::uppercase << v << "\""; return oss.str(); } diff --git a/core/relinker/relinker/src/patching/GamePatch.cpp b/core/relinker/relinker/src/patching/GamePatch.cpp index 0d278b51f4..12f6451372 100644 --- a/core/relinker/relinker/src/patching/GamePatch.cpp +++ b/core/relinker/relinker/src/patching/GamePatch.cpp @@ -481,6 +481,8 @@ void CheckOverlaps(const std::string& target, const std::vector& p for (const auto& site : writers.Trampolines) sites.push_back({site.Offset, site.Length, "Intel trampoline site"}); for (const auto& patch : writers.RelinkPatches) sites.push_back({patch.Offset, patch.Bytes.size(), "relinker patch"}); sites.insert(sites.end(), writers.Traps.begin(), writers.Traps.end()); + for (const auto& site : writers.RedZoneSites) sites.push_back({site.Offset, site.Length, "red-zone frame site"}); + for (const auto& moved : writers.RedZoneDisplacements) sites.push_back({moved.Offset, moved.Bytes.size(), "red-zone moved displacement"}); const auto relaViolation = [&](const std::span table, const char* name, const PatchedRange& range) { for (std::size_t position = 0; position + 24 <= table.size(); position += 24) { std::uint64_t relocation = 0; diff --git a/core/relinker/relinker/src/pipeline/Conversion.cpp b/core/relinker/relinker/src/pipeline/Conversion.cpp index 1cf1879466..1f371ecbca 100644 --- a/core/relinker/relinker/src/pipeline/Conversion.cpp +++ b/core/relinker/relinker/src/pipeline/Conversion.cpp @@ -18,6 +18,8 @@ #include #include #include +#include +#include #include #include #include @@ -41,6 +43,7 @@ ConversionOutput Conversion::Convert(ConversionInput input, const ConversionOpti if (options.Target != TargetSystem::Linux && options.Target != TargetSystem::Windows && options.Target != TargetSystem::MacOs) throw Domain::RelinkerException("Unknown target system"); if (options.ToRosetta && (!options.ToIntel || options.Target == TargetSystem::Windows)) throw Domain::RelinkerException("Rosetta conversion requires Intel lowering and a non-Windows target"); if (options.UnusedFilterLevel > 2) throw Domain::RelinkerException("Unused NID filter level must be 0, 1 or 2"); + if (options.FrameRedZone && options.Target != TargetSystem::Windows) throw Domain::RelinkerException("Red-zone framing requires a Windows target: only Windows exception dispatch writes below RSP"); if (input.InputPath.empty() || input.OutputPath.empty()) throw Domain::RelinkerException("Input and output names are required"); if (input.InputPath.lexically_normal() == input.OutputPath.lexically_normal()) throw Domain::RelinkerException("Executable output would overwrite the input executable"); if (!input.Modules.empty() && input.ModuleRoot.empty()) throw Domain::RelinkerException("Module root is required"); @@ -105,7 +108,27 @@ ConversionOutput Conversion::Convert(ConversionInput input, const ConversionOpti throw Domain::RelinkerException("Relinker patch exceeds source image", patch.Offset); for (std::size_t index = 0; index < patch.Bytes.size(); ++index) sourceBytes[patch.Offset + index] = patch.Bytes[index]; } - GamePatch::CheckOverlaps(options.TargetName, patchedRanges, {.RelinkPatches = result.Patches, .RelaData = result.DynamicSection.RelaData, .RelaPltData = result.DynamicSection.RelaPltData}); + const auto intelSites = trampolines.size(); + std::vector displacements; + if (options.FrameRedZone) { + const auto headers = Relinker::ElfReader(sourceBytes).ReadProgramHeaders(); + std::vector codeSegments; + for (const auto& header : headers) if (header.Type == 1 && (header.Flags & 1) != 0) codeSegments.push_back(header); + std::vector functions; + for (const auto& region : Relinker::UnusedNidFilter::ReadExceptionFunctions(sourceBytes, headers, {}, {})) functions.push_back({region.Begin, region.End}); + if (functions.empty()) throw Domain::RelinkerException("Red-zone framing needs function bounds from the executable's .eh_frame, and it has none"); + Codegen::RedZoneFrameReport report; + auto framed = Codegen::FrameRedZoneFunctions(sourceBytes, codeSegments, functions, trampolines, report, displacements); + for (const auto& moved : displacements) { + if (moved.Offset > sourceBytes.size() || moved.Bytes.size() > sourceBytes.size() - moved.Offset) + throw Domain::RelinkerException("Red-zone displacement exceeds source image", moved.Offset); + std::copy(moved.Bytes.begin(), moved.Bytes.end(), sourceBytes.begin() + static_cast(moved.Offset)); + } + { std::ostringstream message; message << "Red-zone framing: " << report.Functions << " functions, " << report.RedZoneFunctions << " leaf functions keep values below RSP, " << report.Framed << " framed (" << report.FramedWithoutFramePointer << " without a frame pointer, " << report.MovedOperands << " rsp operands moved, " << report.StubbedOperands << " in " << report.OperandStubs << " operand stubs; " << report.Exits << " exits)\n"; inputProgress.Message(message.str()); } + for (const auto& [reason, count] : report.Skipped) { std::ostringstream message; message << "Red-zone framing: skipped " << count << ": " << reason << '\n'; inputProgress.Message(message.str()); } + trampolines.insert(trampolines.end(), std::make_move_iterator(framed.begin()), std::make_move_iterator(framed.end())); + } + GamePatch::CheckOverlaps(options.TargetName, patchedRanges, {.RelinkPatches = result.Patches, .RelaData = result.DynamicSection.RelaData, .RelaPltData = result.DynamicSection.RelaPltData, .RedZoneSites = std::span(trampolines).subspan(intelSites), .RedZoneDisplacements = displacements}); if (!options.Patches.empty()) { for (auto& module : input.Modules) { diff --git a/core/relinker/relinker/tests/CallRegistryWriterTests.cpp b/core/relinker/relinker/tests/CallRegistryWriterTests.cpp new file mode 100644 index 0000000000..542f386b46 --- /dev/null +++ b/core/relinker/relinker/tests/CallRegistryWriterTests.cpp @@ -0,0 +1,81 @@ +#include +#include +#include +#include +#include +#include + +namespace { + +void Require(bool condition, const std::string& message) { + if (!condition) throw std::runtime_error(message); +} + +class GroupedPunctuation : public std::numpunct { +public: + explicit GroupedPunctuation(char separator) : separator(separator) {} + +protected: + char do_thousands_sep() const override { return separator; } + std::string do_grouping() const override { return "\3"; } + +private: + char separator; +}; + +class ScopedLocale { +public: + explicit ScopedLocale(const std::locale& locale) : previous(std::locale::global(locale)) {} + ~ScopedLocale() { std::locale::global(previous); } + +private: + std::locale previous; +}; + +void RegistryOffsets() { + Relinker::CallRegistryWriter writer; + Relinker::CallRegistryEntry entry; + entry.Nid = "testNid"; + entry.Library = "test.prx"; + entry.RelocationTypeString = "R_X86_64_JUMP_SLOT"; + entry.RelocationOffset = 0x12345678; + entry.TargetSection = ".text"; + entry.TargetOffset = 0xabcdef0123456789ULL; + entry.CallSites = {0, 0xfff, 0x1000, 0xffffffffffffffffULL}; + entry.CallSitesResolved = true; + entry.Weak = true; + std::string registry; + std::string imports; + std::string libraries; + { + const ScopedLocale classic(std::locale::classic()); + registry = writer.WriteCallRegistry({entry}); + imports = writer.WriteModuleImports({entry}); + libraries = writer.WriteNeededLibraries({entry.Library}); + } + Require(registry.find("\"relocationOffset\": \"0x12345678\"") != std::string::npos, "relocation offset is not canonical hexadecimal"); + Require(registry.find("\"targetOffset\": \"0xABCDEF0123456789\"") != std::string::npos, "target offset is not canonical hexadecimal"); + Require(registry.find("\"callSites\": [\"0x0\", \"0xFFF\", \"0x1000\", \"0xFFFFFFFFFFFFFFFF\"]") != std::string::npos, "call sites are not canonical hexadecimal"); + Require(imports.find("\"targetOffset\": \"0xABCDEF0123456789\"") != std::string::npos, "module target offset is not canonical hexadecimal"); + for (const char separator : {',', '.'}) { + const ScopedLocale grouped(std::locale(std::locale::classic(), new GroupedPunctuation(separator))); + std::ostringstream control; + control << std::hex << 0x12345678; + Require(control.str().find(separator) != std::string::npos, "grouping locale is not active"); + Require(writer.WriteCallRegistry({entry}) == registry, "call registry depends on the host locale"); + Require(writer.WriteModuleImports({entry}) == imports, "module imports depend on the host locale"); + Require(writer.WriteNeededLibraries({entry.Library}) == libraries, "needed libraries depend on the host locale"); + Require(std::use_facet>(std::locale()).thousands_sep() == separator, "registry writer changed the global locale"); + } +} + +} + +int main() { + try { + RegistryOffsets(); + } catch (const std::exception& error) { + std::cerr << error.what() << '\n'; + return 1; + } +} diff --git a/core/relinker/relinker/tests/CallSiteResolverTests.cpp b/core/relinker/relinker/tests/CallSiteResolverTests.cpp index f06fab07fa..6dd410e448 100644 --- a/core/relinker/relinker/tests/CallSiteResolverTests.cpp +++ b/core/relinker/relinker/tests/CallSiteResolverTests.cpp @@ -31,6 +31,8 @@ int main() { check("RIP-relative function pointer load", {0x48, 0x8b, 0x05, 0xf9, 0, 0, 0}, true); check("REX-prefixed indirect call", {0x48, 0xff, 0x15, 0xf9, 0, 0, 0}, true); check("multiple REX prefixes on pointer load", {0x40, 0x48, 0x8b, 0x05, 0xf8, 0, 0, 0}, true); + check("earlier REX.W does not widen a pointer load", {0x48, 0x40, 0x8b, 0x05, 0xf8, 0, 0, 0}, false); + check("final REX.W widens a pointer load", {0x48, 0x40, 0x48, 0x8b, 0x05, 0xf7, 0, 0, 0}, true); check("pointer load without REX.W", {0x40, 0x8b, 0x05, 0xf9, 0, 0, 0}, false); check("register pointer load", {0x48, 0x8b, 0x00}, false); check("increment memory", {0xff, 0x05, 0xfa, 0, 0, 0}, false); diff --git a/core/relinker/relinker/tests/CodeDataScanningTests.cpp b/core/relinker/relinker/tests/CodeDataScanningTests.cpp new file mode 100644 index 0000000000..0b2e06c47c --- /dev/null +++ b/core/relinker/relinker/tests/CodeDataScanningTests.cpp @@ -0,0 +1,86 @@ +#include "TestProgress.hpp" +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace { + +using Bytes = std::vector; + +constexpr std::size_t DataOffset = 6; +constexpr std::size_t RoutineOffset = 14; +constexpr std::array UndecodableData{0x0f, 0x78, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55}; + +void Require(bool condition, const std::string& message) { + if (!condition) throw std::runtime_error(message); +} + +Bytes Fixture(bool syscall) { + constexpr std::array entry{0xe8, 0x09, 0x00, 0x00, 0x00, 0xc3}; + Bytes code(RoutineOffset, 0x90); + std::copy(entry.begin(), entry.end(), code.begin()); + if (syscall) code.insert(code.end(), {0xb8, 0x09, 0x00, 0x00, 0x00, 0x0f, 0x05}); + code.push_back(0xc3); + return code; +} + +void CheckUndecodableData(Domain::FileByteOffset origin) { + TestProgress progress; + const auto scanner = Relinker::MakeSyscallScanner(progress); + auto code = Fixture(false); + Require(scanner->ScanCodeSectionForSyscalls(code, origin, code.size()).empty(), "Valid code across a padding gap was rejected"); + std::copy(UndecodableData.begin(), UndecodableData.end(), code.begin() + DataOffset); + try { + scanner->ScanCodeSectionForSyscalls(code, origin, code.size()); + } catch (const Codegen::CodegenException& error) { + Require(error.FailureOffset == origin + DataOffset, "Decode failure did not identify the embedded data"); + Require(std::string(error.what()).find("Cannot decode instruction: Unsupported 0F 78 opcode without SSE4A prefix") != std::string::npos, "Unexpected embedded-data decode failure"); + std::cout << "Embedded-data decode rejection reproduced at " << error.FailureOffset << '\n'; + return; + } + throw std::runtime_error("Embedded-data scanning changed; update the known-gap fixture and debt entry"); +} + +void CheckForbiddenInstruction(const Bytes& code, Domain::FileByteOffset origin, std::size_t offset, const std::string& number) { + for (const bool trap : {false, true}) { + TestProgress progress; + try { + Relinker::MakeSyscallScanner(progress, trap)->ScanCodeSectionForSyscalls(code, origin, code.size()); + } catch (const Domain::RelinkerException& error) { + Require(error.FailureOffset == origin + offset, "Forbidden instruction reported at the wrong offset"); + Require(std::string(error.what()).find("Forbidden syscall instruction at code offset 0x") != std::string::npos, "Unexpected syscall rejection"); + Require(number.empty() || std::string(error.what()).find(number) != std::string::npos, "Lost the reachable routine's syscall number"); + continue; + } + throw std::runtime_error("Forbidden instruction was not reported"); + } +} + +void CheckGapAndReachableRoutine(Domain::FileByteOffset origin) { + auto code = Fixture(false); + code[DataOffset] = 0x0f; + code[DataOffset + 1] = 0x05; + CheckForbiddenInstruction(code, origin, DataOffset, {}); + CheckForbiddenInstruction(Fixture(true), origin, RoutineOffset + 5, "syscall number 9"); +} + +} + +int main() { + try { + for (const auto origin : {std::uint64_t{0}, std::uint64_t{0x123400}, std::uint64_t{0x100000000}}) { + CheckUndecodableData(origin); + CheckGapAndReachableRoutine(origin); + } + std::cout << "Embedded-code-data known-gap and reachable-code controls passed\n"; + } catch (const std::exception& error) { + std::cerr << error.what() << '\n'; + return 1; + } +} diff --git a/core/relinker/relinker/tests/ControlFlowGraphTests.cpp b/core/relinker/relinker/tests/ControlFlowGraphTests.cpp index 94a34990c1..092f5fa398 100644 --- a/core/relinker/relinker/tests/ControlFlowGraphTests.cpp +++ b/core/relinker/relinker/tests/ControlFlowGraphTests.cpp @@ -142,6 +142,26 @@ void CheckRelocatedTransfers() { RequireOffsets(*unresolved, {0, 6, 7, 8}); } +void CheckAddressSizeOverride() { + constexpr auto slot = TextAddress + 0x1000; + Bytes text{0x67, 0xff, 0x15, 0, 0, 0, 0, 0x90, 0xc3, 0x67, 0xff, 0x25, 0, 0, 0, 0, 0x90, 0x90, 0xc3}; + SetDisplacement(text, 3, 7, slot); + SetDisplacement(text, 12, 16, slot + 8); + Pointers pointers; + pointers.Slots = {{static_cast(slot), TextAddress + 9}, {static_cast(slot + 8), TextAddress + 18}}; + RequireOffsets(*BuildControlFlowGraph(text, TextAddress, TextAddress, {}, pointers), {0, 7, 8, 9, 18}); + RequireOffsets(*BuildControlFlowGraph(text, TextAddress, TextAddress + 9, {}, pointers), {9, 18}); + + Bytes lea(17, 0x90); + const Bytes opcode{0x67, 0x48, 0x8d, 0x05}; + std::copy(opcode.begin(), opcode.end(), lea.begin()); + SetDisplacement(lea, 4, 8, TextAddress + 16); + lea[8] = lea[16] = 0xc3; + const Pointers missing; + RequireOffsets(*BuildControlFlowGraph(lea, TextAddress, TextAddress, {}, missing, true), {0, 8}); + RequireOffsets(*BuildAddressTakenControlFlowGraph(lea, TextAddress, TextAddress, {}, missing, {}), {0, 8}); +} + void CheckCodeAddresses() { Bytes text(17, 0x90); text[0] = 0x48; @@ -354,6 +374,7 @@ int main() { CheckCallsBeforeZeroPadding(); CheckCallsThatStillFallThrough(); CheckRelocatedTransfers(); + CheckAddressSizeOverride(); CheckCodeAddresses(); CheckSpeculativeRoots(); CheckTrailingZeroRoots(); diff --git a/core/relinker/relinker/tests/ConversionTests.cpp b/core/relinker/relinker/tests/ConversionTests.cpp index 175199158a..4b27916736 100644 --- a/core/relinker/relinker/tests/ConversionTests.cpp +++ b/core/relinker/relinker/tests/ConversionTests.cpp @@ -127,6 +127,35 @@ void convert() { require(progress.Events.back().Completed != 1 || progress.Events.back().Stage != Domain::ProgressStage::Executable, "Failed conversion reported completion"); } +void runtimeLifecyclePolicy() { + TestProgress progress; + const Relinker::Conversion converter(progress); + for (const auto* directory : {"sce_module", "sce_modules", "prx", "Media/Plugins"}) { + auto source = input(); + const auto parent = source.ModuleRoot / directory; + auto module = fixture(); + Io::WriteU64(module, 0x2080, 12); + Io::WriteU64(module, 0x2088, 0x1000); + Io::WriteU64(module, 208, 160); + Io::WriteU64(module, 216, 160); + source.Modules.push_back({parent / "plugin.prx", std::move(module)}); + source.RuntimeDirectories.push_back(parent); + const auto output = converter.Convert(std::move(source), {}); + const auto& bytes = output.Modules.at(0).Bytes; + const auto headers = Io::ReadU64(bytes, 32); + bool initializesOnMapping = false; + for (std::uint16_t index = 0; index < Io::ReadU16(bytes, 56); ++index) { + const auto header = headers + index * 56; + if (Io::ReadU32(bytes, header) != 2) continue; + const auto dynamic = Io::ReadU64(bytes, header + 8); + const auto end = dynamic + Io::ReadU64(bytes, header + 32); + for (auto offset = dynamic; offset < end; offset += 16) + if (Io::ReadU64(bytes, offset) == 12) initializesOnMapping = Io::ReadU64(bytes, offset + 8) != 0; + } + require(initializesOnMapping == (std::string_view(directory) != "Media/Plugins"), "Default SDK directories lost their mapping initializer"); + } +} + void fixedBase() { TestProgress progress; const Relinker::Conversion converter(progress); @@ -348,6 +377,7 @@ void relocationValidation() { int main() { try { convert(); + runtimeLifecyclePolicy(); fixedBase(); scanning(); diagnosticContext(); diff --git a/core/relinker/relinker/tests/GamePatchTests.cpp b/core/relinker/relinker/tests/GamePatchTests.cpp index 641b830206..0b6ab99a13 100644 --- a/core/relinker/relinker/tests/GamePatchTests.cpp +++ b/core/relinker/relinker/tests/GamePatchTests.cpp @@ -353,6 +353,14 @@ void overlapWithWriters() { CheckOverlaps("t", patched, {.Traps = trapBefore}); const std::vector window{{0x100f, 4, "window"}}; reject({.Traps = window}, "window"); + const std::vector frame{{0x1011, 0x401011, 5, {}, {}, 0}}; + reject({.RedZoneSites = frame}, "red-zone frame site"); + const std::vector frameBefore{{0x100b, 0x40100b, 5, {}, {}, 0}}; + CheckOverlaps("t", patched, {.RedZoneSites = frameBefore}); + const std::vector moved{{0x100f, {0x10, 0, 0, 0}}}; + reject({.RedZoneDisplacements = moved}, "red-zone moved displacement"); + const std::vector movedBefore{{0x100f, {0x10}}}; + CheckOverlaps("t", patched, {.RedZoneDisplacements = movedBefore}); } void conversion() { @@ -400,6 +408,56 @@ void conversion() { require(repeated.Executable == baseline.Executable && repeated.Modules.front().Bytes == baseline.Modules.front().Bytes, "no entries changed the output"); } +std::vector framedLeafFixture() { + auto bytes = conversionFixture(); + Io::WriteU64(bytes, 64 + 32, 0x80); + Io::WriteU64(bytes, 64 + 40, 0x80); + std::fill(bytes.begin() + 0x1000, bytes.begin() + 0x1080, 0xcc); + const std::vector caller{0xe8, 0x3b, 0x00, 0x00, 0x00, 0xc3}; + const std::vector leaf{0x53, 0x41, 0x56, 0x41, 0x57, 0x48, 0x89, 0x7c, 0x24, 0xf0, 0x48, 0x8b, 0x04, 0x24, 0x48, 0x8b, 0x44, 0x24, 0x08, 0x41, 0x5f, 0x41, 0x5e, 0x5b, 0xc3}; + std::copy(caller.begin(), caller.end(), bytes.begin() + 0x1000); + std::copy(leaf.begin(), leaf.end(), bytes.begin() + 0x1040); + const auto ehFrameHeader = 64 + 7 * 56; + Io::WriteU32(bytes, ehFrameHeader, 0x6474e550); + Io::WriteU32(bytes, ehFrameHeader + 4, 4); + for (const auto field : {8, 16, 24}) Io::WriteU64(bytes, ehFrameHeader + field, 0x2900); + Io::WriteU64(bytes, ehFrameHeader + 32, 32); + Io::WriteU64(bytes, ehFrameHeader + 40, 32); + Io::WriteU64(bytes, ehFrameHeader + 48, 4); + Io::WriteU32(bytes, 0x2800, 9); + const std::vector cie{1, 0, 1, 0x78, 16}; + std::copy(cie.begin(), cie.end(), bytes.begin() + 0x2808); + Io::WriteU32(bytes, 0x2840, 20); + Io::WriteU32(bytes, 0x2844, 0x44); + Io::WriteU64(bytes, 0x2848, 0x1040); + Io::WriteU64(bytes, 0x2850, leaf.size()); + bytes[0x2900] = 1; + bytes[0x2902] = 3; + Io::WriteU64(bytes, 0x2904, 0x2800); + Io::WriteU32(bytes, 0x290c, 1); + Io::WriteU64(bytes, 0x2910, 0x1040); + Io::WriteU64(bytes, 0x2918, 0x2840); + return bytes; +} + +void framedLeafPatches() { + TestProgress progress; + const Conversion converter(progress); + const auto executable = framedLeafFixture(); + ConversionOptions options; + options.TargetName = "custom-name.bin"; + options.Target = TargetSystem::Windows; + options.FrameRedZone = true; + converter.Convert(conversionInput(executable), options); + require(progress.Messages.find("1 framed (1 without a frame pointer, 1 rsp operands moved, 2 in 1 operand stubs; 1 exits)") != std::string::npos, "the fixture's leaf was not framed with a moved displacement and an operand stub: " + progress.Messages); + options.Patches = {entry("custom-name.bin", 0x1010, {0xcc}, {0x90})}; + require(converter.Convert(conversionInput(executable), options).AppliedPatches.size() == 1, "a patch outside the framed leaf was not applied"); + options.Patches = {entry("custom-name.bin", 0x1049, {0xf0}, {0xe8})}; + rejects([&] { converter.Convert(conversionInput(executable), options); }, "overlaps red-zone moved displacement at off=0x1049 length 1"); + options.Patches = {entry("custom-name.bin", 0x104c, {0x04}, {0x0c})}; + rejects([&] { converter.Convert(conversionInput(executable), options); }, "overlaps red-zone frame site at off=0x104a length 9"); +} + void targets() { const std::vector entries{entry("a.elf", 1, {0}, {1}), entry("sce_module/libc.prx", 1, {0}, {1})}; MatchTargets(entries, {"a.elf", "b.elf", "sce_module/libc.prx"}); @@ -588,6 +646,7 @@ int main() { applicationRangeWidths(); overlapWithWriters(); conversion(); + framedLeafPatches(); targets(); verify(); rangeFailureInVerify(); diff --git a/core/relinker/relinker/tests/test_guest_deferred_dependencies.py b/core/relinker/relinker/tests/test_guest_deferred_dependencies.py index 7d27de5530..1a155bc31e 100644 --- a/core/relinker/relinker/tests/test_guest_deferred_dependencies.py +++ b/core/relinker/relinker/tests/test_guest_deferred_dependencies.py @@ -124,8 +124,8 @@ def main(): env={**os.environ, "ANYPS5_GUEST_MODULE_ARGS": str(events)}) assert run.returncode == 42, (root, run.returncode, run.stdout, run.stderr) recorded = events.read_text() if events.exists() else "" - assert recorded.count("args=") == (1 if root in ("executable", "bundled", "imported") else 0), (root, recorded) - load_and_record(case, libraries / "libc.prx", runtime / "plugin.prx.guest.prx") + assert recorded.count("args=") == 0, (root, recorded) + load_and_record(case, libraries / "libc.prx", runtime / "provider.prx.guest.prx") print("Guest deferred dependency tests passed") diff --git a/core/relinker/relinker/tests/test_guest_module_args.py b/core/relinker/relinker/tests/test_guest_module_args.py index ea9ecf0f18..0ee80ca6f3 100644 --- a/core/relinker/relinker/tests/test_guest_module_args.py +++ b/core/relinker/relinker/tests/test_guest_module_args.py @@ -105,6 +105,11 @@ def check_lifecycle_host(relinker, host, work, linux, deferred): env={**os.environ, 'ANYPS5_GUEST_MODULE_ARGS': str(events)}, capture_output=True, text=True, timeout=30) assert run.returncode == 0, (run.returncode, run.stdout, run.stderr) + assert not events.exists(), events.read_text() + run = subprocess.run([sys.executable, __file__, '--load', str(libraries / 'libkernel.prx'), str(initializer)], + env={**os.environ, 'ANYPS5_GUEST_MODULE_ARGS': str(events)}, + capture_output=True, text=True, timeout=30) + assert run.returncode == 0, (run.returncode, run.stdout, run.stderr) assert events.read_text() == 'result=0\n', events.read_text() @@ -161,7 +166,20 @@ def main(): host = ctypes.CDLL(sys.argv[2], mode=ctypes.RTLD_GLOBAL if sys.platform == 'darwin' else ctypes.DEFAULT_MODE) host.SetPendingArgs.argtypes = [ctypes.c_size_t, ctypes.c_void_p] host.SetPendingArgs(ARGS, ARGP) - ctypes.CDLL(sys.argv[3]) + module = ctypes.CDLL(sys.argv[3]) + try: + table = (ctypes.c_uint64 * 8).in_dll(module, '__aps5_guest_lifecycle_nid_no_patch') + except ValueError: + return + assert table[0] == 0x415053354c494658 and table[6] == 0 + table[6] = 1 + returned = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_size_t, ctypes.c_void_p, ctypes.c_void_p)(table[2])(ARGS, ARGP, None) if table[2] else 0 + for index in range(table[4]): + callback = ctypes.c_void_p.from_address(ctypes.c_uint64.from_address(ctypes.addressof(table) + 64 + index * 8).value).value + if callback: + ctypes.CFUNCTYPE(None, ctypes.c_int, ctypes.c_void_p, ctypes.c_void_p)(callback)(0, None, None) + host.__aps5_set_module_init_result_nid_no_patch(returned) + table[6] = 2 return relinker = Path(sys.argv[1]).resolve() host = Path(sys.argv[2]).resolve() diff --git a/core/relinker/relinker/tests/test_guest_module_dir.py b/core/relinker/relinker/tests/test_guest_module_dir.py index 7f2022a918..95e4f087ac 100644 --- a/core/relinker/relinker/tests/test_guest_module_dir.py +++ b/core/relinker/relinker/tests/test_guest_module_dir.py @@ -10,6 +10,7 @@ from test_guest_intel_trampolines import main_fixture, pe_sections from test_guest_module_directories import module_with_symbol, needed_libraries from test_guest_needed_modules import executable_with_needed, module_with_needed from test_macos_output import MachO +from test_windows_import_modules import executable, provider def pe_export_names(data): @@ -199,6 +200,21 @@ def main(): else: assert "$ORIGIN/app0/Media/Modules/needed.prx.guest.prx" in needed_libraries(output.read_bytes()), needed_libraries(output.read_bytes()) + for directory, deferred in (("Media/Modules", True), ("sce_module", False)): + case = work / f"{windows}-imported-{deferred}" + (case / directory).mkdir(parents=True) + (case / directory / "needed.prx").write_bytes(provider(42)) + result, output = convert(case, windows, ["--module-dir", directory] if deferred else [], executable("needed.prx")) + assert result.returncode == 0, (result.stdout, result.stderr) + line = f"Deferred module app0/{directory}/needed.prx.guest.prx: the executable imports from it, but it starts only when the title loads it\n" + if windows: + assert (line.encode() in output.read_bytes()) == deferred, directory + if os.name == "nt": + run = subprocess.run([str(output)], capture_output=True, text=True, timeout=30) + assert run.returncode == 42 and (line in run.stdout) == deferred, (directory, run.returncode, run.stdout, run.stderr) + else: + assert f"$ORIGIN/app0/{directory}/needed.prx.guest.prx" in needed_libraries(output.read_bytes()), needed_libraries(output.read_bytes()) + case = work / f"{windows}-root" case.mkdir() (case / "root.prx").write_bytes(module_with_symbol(True)) diff --git a/core/relinker/relinker/tests/test_guest_runtime_lifecycle.py b/core/relinker/relinker/tests/test_guest_runtime_lifecycle.py new file mode 100644 index 0000000000..d7bd720160 --- /dev/null +++ b/core/relinker/relinker/tests/test_guest_runtime_lifecycle.py @@ -0,0 +1,57 @@ +from pathlib import Path +import os +import struct +import subprocess +import sys +import tempfile + +from test_guest_module_args import args_module + + +def lifecycle_module(): + image = args_module() + tags = [] + for offset in range(0x2000, 0x2300, 16): + tag, value = struct.unpack_from("= len(image), "The grown program header table must be moved past the input" assert elf[64 + 5 * 56:64 + 6 * 56] == image[64 + 5 * 56:64 + 6 * 56], "Bytes after the input program header table changed" - image = fixture(5, with_frame=False) - headers = image[64:64 + 4 * 56] - headers += struct.pack("= len(setup), "entry stub does not initialize guest MXCSR before its start call") + check(stub[call - len(setup):call] == setup, "entry stub does not save host MXCSR and load guest MXCSR 0x9fc0") + entry = stub.find(bytes.fromhex("488d3d")) + entry_target = IMAGE_BASE + macho.entryoff + entry + 7 + struct.unpack_from(" #include #include +#include #include #include @@ -275,13 +277,35 @@ private: append(key, value.maxEvaluationOutputComponents); } + static void appendFeatureDigest(std::vector& key, std::span capabilities, std::span extensions) { + std::uint64_t low = 0xcbf29ce484222325ull; + std::uint64_t high = 0x6a09e667f3bcc909ull; + const auto mix = [&](std::uint64_t value) { + low = (low ^ value) * 0x100000001b3ull; + high = (high ^ (value * 0x9e3779b97f4a7c15ull)) * 0xff51afd7ed558ccdull; + high ^= high >> 29u; + }; + mix(capabilities.size()); + for (const auto capability : capabilities) mix(capability); + mix(extensions.size()); + for (const auto extension : extensions) { + mix(extension.size()); + for (std::size_t offset = 0; offset < extension.size(); offset += sizeof(std::uint64_t)) { + std::uint64_t chunk = 0; + std::memcpy(&chunk, extension.data() + offset, std::min(sizeof(std::uint64_t), extension.size() - offset)); + mix(chunk); + } + } + key.push_back(low); + key.push_back(high); + } + static void append(std::vector& key, const SpirvTarget& value) { append(key, value.vulkanVersion); append(key, value.spirvVersion); append(key, value.subgroupSize); append(key, value.bdaAbiVersion); - append(key, value.supportedCapabilities); - append(key, value.supportedExtensions); + appendFeatureDigest(key, value.supportedCapabilities, value.supportedExtensions); append(key, value.fragmentShaderBarycentricEnabled); append(key, value.maxWorkgroupSize); append(key, value.maxWorkgroupInvocations); diff --git a/core/shader/recompiler/ControlFlow/src/Structurizer.cpp b/core/shader/recompiler/ControlFlow/src/Structurizer.cpp index 3267461138..8684767d4a 100644 --- a/core/shader/recompiler/ControlFlow/src/Structurizer.cpp +++ b/core/shader/recompiler/ControlFlow/src/Structurizer.cpp @@ -2335,7 +2335,8 @@ void Structurizer::canonicalizeNaturalLoops(ControlFlowGraph& graph, bool routeL }); const auto& header = graph.FindBlock(loop.headerBlock); const bool semanticSelfLoop = latches.size() == 1u && latches.front() == header.id && header.instructionBegin != header.instructionEnd; - if (latches.size() <= 1u && !latchInInnerLoop && !semanticSelfLoop) { + const bool semanticLatch = latches.size() == 1u && graph.FindBlock(latches.front()).instructionBegin != graph.FindBlock(latches.front()).instructionEnd && graph.FindBlock(latches.front()).predecessors.size() == 1u; + if (latches.size() <= 1u && !latchInInnerLoop && !semanticSelfLoop && !semanticLatch) { continue; } diff --git a/core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/Resources.hpp b/core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/Resources.hpp index 0b6a5ceb0d..e639277ea3 100644 --- a/core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/Resources.hpp +++ b/core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/Resources.hpp @@ -96,6 +96,7 @@ struct ImageResource { bool volumeArraySampleCompatible = false; bool flatLineCompatible = true; bool flatVolumeLineCompatible = true; + bool dimensionQueryOnly = true; bool foldTexelOffsetsOnly = false; bool convertedSamples = false; bool remappedDepth1D = false; diff --git a/core/shader/recompiler/IntermediateRepresentation/src/IrProgram.cpp b/core/shader/recompiler/IntermediateRepresentation/src/IrProgram.cpp index 08dba6b1d9..4278d1a771 100644 --- a/core/shader/recompiler/IntermediateRepresentation/src/IrProgram.cpp +++ b/core/shader/recompiler/IntermediateRepresentation/src/IrProgram.cpp @@ -173,6 +173,21 @@ std::string formatHex64(std::uint64_t value) { return stream.str(); } +bool carriesMemoryInfo(IrOpcode opcode) { + return opcode == IrOpcode::ReadConstBuffer || AddressOpcodeInfoOf(opcode).access != AddressAccess::None || BufferComponentCount(opcode) != 0u || SharedComponentCount(opcode) != 0u || ImageOpcodeInfoOf(opcode).access != ImageAccess::None; +} + +std::string memoryToString(const IrProgram& program, const IrValue& inst) { + const auto flags = inst.Flags(); + const auto& memoryInfo = program.Resources().memoryInfo; + if (flags.index >= memoryInfo.size()) { + return " [pc=" + formatHex32(flags.pc) + " mem=?]"; + } + std::ostringstream stream; + stream << " [pc=" << formatHex32(flags.pc) << " off=0x" << std::hex << memoryInfo[flags.index].offset << "]"; + return stream.str(); +} + std::string valueToString(const std::map& ids, const IrValue* value) { if (value == nullptr) { return ""; @@ -404,7 +419,11 @@ std::string ProgramToString(const IrProgram& program) { text += valueToString(ids, inst->Argument(index)); } } - text += " (" + TypeToString(inst->Type()) + "; uses=" + std::to_string(inst->UseCount()) + ")\n"; + text += " (" + TypeToString(inst->Type()) + "; uses=" + std::to_string(inst->UseCount()) + ")"; + if (carriesMemoryInfo(inst->Opcode())) { + text += memoryToString(program, *inst); + } + text += "\n"; } } return text; @@ -586,11 +605,11 @@ void ValidateProgram(const IrProgram& program, bool requireSsa) { if (fixedSignature && inst->ArgumentCount() != IrOpcodeOperandCount(inst->Opcode())) { fail(std::string(IrOpcodeName(inst->Opcode())) + " has " + std::to_string(inst->ArgumentCount()) + " arguments, expected " + std::to_string(IrOpcodeOperandCount(inst->Opcode()))); } + if (carriesMemoryInfo(inst->Opcode()) && inst->Flags().index >= program.Resources().memoryInfo.size()) { + fail(std::string(IrOpcodeName(inst->Opcode())) + " has an invalid memory-info index"); + } if (inst->Opcode() == IrOpcode::ReadConstBuffer) { const auto memoryIndex = inst->Flags().index; - if (memoryIndex >= program.Resources().memoryInfo.size()) { - fail(std::string(IrOpcodeName(inst->Opcode())) + " has an invalid memory-info index"); - } const auto& memory = program.Resources().memoryInfo[memoryIndex]; if (memory.kind != ResourceKind::ScalarBuffer) { fail(std::string(IrOpcodeName(inst->Opcode())) + " has an invalid scalar-memory resource kind"); @@ -603,9 +622,6 @@ void ValidateProgram(const IrProgram& program, bool requireSsa) { const auto addressInfo = AddressOpcodeInfoOf(inst->Opcode()); if (addressInfo.access != AddressAccess::None) { const auto memoryIndex = inst->Flags().index; - if (memoryIndex >= program.Resources().memoryInfo.size()) { - fail(std::string(IrOpcodeName(inst->Opcode())) + " has an invalid memory-info index"); - } const auto& memory = program.Resources().memoryInfo[memoryIndex]; if (!IsAddressResourceKind(memory.kind) || (memory.kind == ResourceKind::ScalarAddress && inst->Opcode() != IrOpcode::LoadAddressU32)) { fail(std::string(IrOpcodeName(inst->Opcode())) + " has an invalid address resource kind"); @@ -620,9 +636,6 @@ void ValidateProgram(const IrProgram& program, bool requireSsa) { const auto bufferComponents = BufferComponentCount(inst->Opcode()); if (bufferComponents != 0u) { const auto memoryIndex = inst->Flags().index; - if (memoryIndex >= program.Resources().memoryInfo.size()) { - fail(std::string(IrOpcodeName(inst->Opcode())) + " has an invalid memory-info index"); - } const auto& memory = program.Resources().memoryInfo[memoryIndex]; if (memory.kind != ResourceKind::Buffer && memory.kind != ResourceKind::ScalarBuffer) { fail(std::string(IrOpcodeName(inst->Opcode())) + " has a non-buffer resource kind"); @@ -637,9 +650,6 @@ void ValidateProgram(const IrProgram& program, bool requireSsa) { const auto sharedComponents = SharedComponentCount(inst->Opcode()); if (sharedComponents != 0u) { const auto memoryIndex = inst->Flags().index; - if (memoryIndex >= program.Resources().memoryInfo.size()) { - fail(std::string(IrOpcodeName(inst->Opcode())) + " has an invalid memory-info index"); - } const auto& memory = program.Resources().memoryInfo[memoryIndex]; if ((memory.kind != ResourceKind::Lds && memory.kind != ResourceKind::Gds) || memory.resource != 0u || memory.sampler != 0u || memory.componentCount == 0u || memory.componentIndex >= memory.componentCount) { fail(std::string(IrOpcodeName(inst->Opcode())) + " has invalid shared-memory metadata"); @@ -657,9 +667,6 @@ void ValidateProgram(const IrProgram& program, bool requireSsa) { const auto imageInfo = ImageOpcodeInfoOf(inst->Opcode()); if (imageInfo.access != ImageAccess::None) { const auto memoryIndex = inst->Flags().index; - if (memoryIndex >= program.Resources().memoryInfo.size()) { - fail(std::string(IrOpcodeName(inst->Opcode())) + " has an invalid memory-info index"); - } const auto& memory = program.Resources().memoryInfo[memoryIndex]; if (memory.kind != ResourceKind::Image || imageInfo.resourceClass == ImageResourceClass::None) { fail(std::string(IrOpcodeName(inst->Opcode())) + " has invalid image-memory metadata"); diff --git a/core/shader/recompiler/Optimization/src/DescriptorBindingBuilder.cpp b/core/shader/recompiler/Optimization/src/DescriptorBindingBuilder.cpp index b3c679dc2e..f11023d4b4 100644 --- a/core/shader/recompiler/Optimization/src/DescriptorBindingBuilder.cpp +++ b/core/shader/recompiler/Optimization/src/DescriptorBindingBuilder.cpp @@ -139,6 +139,7 @@ std::vector GuestBuffersDescriptor(const std::vector GuestSamplersDescriptor(const std::vector& resources, const ResourceSnapshot& snapshot) { std::vector result; + result.reserve(resources.size() * 4u); std::uint32_t dwordCount = 0; for (std::size_t i = 0; i < resources.size(); i++) { const std::uint32_t r = resources[i]; @@ -567,6 +568,7 @@ void DescriptorBindingBuilder::Populate(BindingAllocationResult& allocation, con auto shaderData = needsShaderData ? ShaderDataDwordsFor(layout, userDataBase, snapshot, partialThreads, plan.imageMetadata, plan.runtimeState) : std::vector{}; allocation.shaderDataUserWords.clear(); if (needsShaderData) { + allocation.shaderDataUserWords.reserve(layout.userDataRegisters.size()); for (const auto reg : layout.userDataRegisters) allocation.shaderDataUserWords.push_back(reg - userDataBase); } std::vector bindings; diff --git a/core/shader/recompiler/Optimization/src/RedundantValueEliminator.cpp b/core/shader/recompiler/Optimization/src/RedundantValueEliminator.cpp index 5244bf70f9..df61fa400d 100644 --- a/core/shader/recompiler/Optimization/src/RedundantValueEliminator.cpp +++ b/core/shader/recompiler/Optimization/src/RedundantValueEliminator.cpp @@ -328,6 +328,9 @@ RedundantValueEliminationStats RedundantValueEliminator::Eliminate(IrProgram& pr replaced(info.indirectTarget); } for (auto*& read : program.Metadata().dynamicReads) replaced(read); + for (auto& descriptor : program.Resources().descriptorSources) { + for (auto*& dword : descriptor.dwords) replaced(dword); + } return stats; } diff --git a/core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp b/core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp index aba7fa1a55..921b9bb653 100644 --- a/core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp +++ b/core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp @@ -207,7 +207,8 @@ ImageDecode inspectImageDescriptor(const DescriptorValue& descriptor, const Imag if (!validImageDescriptor(descriptor, base.r128)) { return rejectImage(invalidImageReason(descriptor), "image descriptor is invalid"); } - decoded.dimension = descriptorDimension(descriptor, base.dimension); + const bool queryLayers = base.dimension == RdnaImageDimension::Dim1D && base.dimensionQueryOnly && rawImageType(descriptor) == ImageType::Color2DArray; + decoded.dimension = descriptorDimension(descriptor, queryLayers ? RdnaImageDimension::Dim2DArray : base.dimension); if (base.depthCompare) { if (decoded.dimension == RdnaImageDimension::Dim1D) { decoded.dimension = RdnaImageDimension::Dim2D; @@ -1838,7 +1839,12 @@ ResolvedImageTables ResourceMaterializer::ResolveImageTables(const ShaderInfo& i } std::shared_ptr ResourceMaterializer::ResolveImageTablesShared(const ShaderInfo& info, const ResourceSnapshot& snapshot, std::uint64_t memoKey) { - if (memoKey == 0u || snapshot.tables.tables.empty() || !TableResolutionReuse()) return std::make_shared(ResolveImageTables(info, snapshot, memoKey)); + if (snapshot.tables.tables.empty()) { + static const auto none = std::make_shared(); + static_cast(ResolveImageTables(info, snapshot, memoKey)); + return none; + } + if (memoKey == 0u || !TableResolutionReuse()) return std::make_shared(ResolveImageTables(info, snapshot, memoKey)); auto reducing = reducingSamplers(info, snapshot); auto& memo = resolutionMemo(); auto& totals = tableReuseTotals(); @@ -1940,11 +1946,24 @@ std::vector ResourceMaterializer::RuntimeImageModes(const ImageRe line.dimension = RdnaImageDimension::Dim1D; modes.push_back(line); } - if (image.dimension == RdnaImageDimension::Dim1D && !storage && !depth && conversion == IrBufferFormat::Invalid && packed == IrBufferFormat::Invalid) { + const bool planeAdded = image.dimension == RdnaImageDimension::Dim1D && !storage && !depth && conversion == IrBufferFormat::Invalid && packed == IrBufferFormat::Invalid; + if (planeAdded) { auto plane = mode; plane.dimension = RdnaImageDimension::Dim2D; modes.push_back(plane); } + if (image.dimension == RdnaImageDimension::Dim1D && image.dimensionQueryOnly && !storage) { + for (const auto dimension : {RdnaImageDimension::Dim2D, RdnaImageDimension::Dim2DArray, RdnaImageDimension::Dim3D}) { + if (dimension == RdnaImageDimension::Dim2D && planeAdded) continue; + auto view = mode; + view.dimension = dimension; + modes.push_back(view); + } + auto cube = mode; + cube.dimension = RdnaImageDimension::Dim2DArray; + cube.cube = true; + modes.push_back(cube); + } if (image.dimension == RdnaImageDimension::Dim1DArray || image.dimension == RdnaImageDimension::Dim2DArray || image.dimension == RdnaImageDimension::Dim2DMsaaArray) { auto plain = mode; plain.dimension = image.dimension == RdnaImageDimension::Dim1DArray ? RdnaImageDimension::Dim1D : image.dimension == RdnaImageDimension::Dim2DArray ? RdnaImageDimension::Dim2D : RdnaImageDimension::Dim2DMsaa; diff --git a/core/shader/recompiler/Optimization/src/ResourceTracker.cpp b/core/shader/recompiler/Optimization/src/ResourceTracker.cpp index 51b118329e..ee6873ef41 100644 --- a/core/shader/recompiler/Optimization/src/ResourceTracker.cpp +++ b/core/shader/recompiler/Optimization/src/ResourceTracker.cpp @@ -1947,6 +1947,7 @@ private: const bool flatLineAccess = image.dimension == RdnaImageDimension::Dim2DArray ? flatLineLevelZero || flatLineExplicitLod : flatLineLoad || flatLineSample || flatLineExplicitLod; image.flatLineCompatible = image.flatLineCompatible && flatLineAccess && !memory.imagePacked && memory.imageByElements == 0u; image.flatVolumeLineCompatible = image.flatVolumeLineCompatible && (flatLineLoad || flatLineSample) && !memory.imagePacked && memory.imageByElements == 0u; + image.dimensionQueryOnly = image.dimensionQueryOnly && op == IrOpcode::ImageQueryDimensions; if ((memory.imageSampleFlags & RdnaImageSampleFlagCompare) != 0u) { constexpr auto unsupported = RdnaImageSampleFlagLod | RdnaImageSampleFlagDerivative; if (op == IrOpcode::ImageGatherRaw || (memory.imageSampleFlags & unsupported) != 0u) image.emulatedCompare |= EmulatedCompare::Unsupported; diff --git a/core/shader/recompiler/Optimization/src/ShaderInfoCollector.cpp b/core/shader/recompiler/Optimization/src/ShaderInfoCollector.cpp index 1ab728cc97..472ade83d6 100644 --- a/core/shader/recompiler/Optimization/src/ShaderInfoCollector.cpp +++ b/core/shader/recompiler/Optimization/src/ShaderInfoCollector.cpp @@ -623,7 +623,7 @@ void ShaderInfoCollector::Collect(IrProgram& program, const ShaderStageInputInfo return SharedAccessOf(inst->Opcode()) == SharedAccess::Atomic && inst->Type() == IrType::U64; }); }); - next.usesFaultBuffer = next.usesFaultBuffer || (next.sharedMemoryBytes != 0u && ldsAtomic64); + next.usesFaultBuffer = next.usesFaultBuffer || ldsAtomic64; switch (program.Resources().stage) { case IrShaderStage::Vertex: case IrShaderStage::Local: diff --git a/core/shader/recompiler/Optimization/src/SrtWalker/SrtEvaluator.cpp b/core/shader/recompiler/Optimization/src/SrtWalker/SrtEvaluator.cpp index 7b8536caac..75d62f0546 100644 --- a/core/shader/recompiler/Optimization/src/SrtWalker/SrtEvaluator.cpp +++ b/core/shader/recompiler/Optimization/src/SrtWalker/SrtEvaluator.cpp @@ -459,6 +459,8 @@ bool BasicEvaluator::MayDecide(const IrResourcePlan& program, IrValue* va case IrOpcode::LogicalNot: return value->ArgumentCount() == 1u && decides(0); case IrOpcode::LogicalAnd: case IrOpcode::LogicalOr: return value->ArgumentCount() == 2u && (decides(0) || decides(1)); + case IrOpcode::ULessThan32: + case IrOpcode::UGreaterThan32: return value->ArgumentCount() == 2u && (MayEvaluate(program, value->Argument(0)) || MayEvaluate(program, value->Argument(1))); default: return false; } } @@ -819,17 +821,24 @@ bool BasicEvaluator::EvaluateInst(const TNode& inst, std::uint64_t& resul } return false; case IrOpcode::ULessThan32: - if (binary()) { - result = static_cast(a) < static_cast(b) ? 1u : 0u; + case IrOpcode::UGreaterThan32: { + const bool greater = inst.Opcode() == IrOpcode::UGreaterThan32; + const bool first = Arg(inst, 0, a); + if (first && static_cast(a) == (greater ? 0u : 0xffffffffu)) { + result = 0u; return true; } - return false; - case IrOpcode::UGreaterThan32: - if (binary()) { - result = static_cast(a) > static_cast(b) ? 1u : 0u; + const bool second = Arg(inst, 1, b); + if (second && static_cast(b) == (greater ? 0xffffffffu : 0u)) { + result = 0u; return true; } - return false; + if (!first || !second) return false; + const auto lhs = static_cast(greater ? b : a); + const auto rhs = static_cast(greater ? a : b); + result = lhs < rhs ? 1u : 0u; + return true; + } case IrOpcode::LogicalAnd: case IrOpcode::LogicalOr: { const bool decisive = inst.Opcode() == IrOpcode::LogicalOr; diff --git a/core/shader/recompiler/Optimization/src/SrtWalker/SrtTape.cpp b/core/shader/recompiler/Optimization/src/SrtWalker/SrtTape.cpp index 9ed10832e1..208b02d774 100644 --- a/core/shader/recompiler/Optimization/src/SrtWalker/SrtTape.cpp +++ b/core/shader/recompiler/Optimization/src/SrtWalker/SrtTape.cpp @@ -363,6 +363,26 @@ bool SrtTape::compute(Frame& frame, const Node& node, std::uint64_t& result) con result = decisive ? 0u : 1u; return true; } + if (node.op == Op::ULessThan32 || node.op == Op::UGreaterThan32) { + const bool greater = node.op == Op::UGreaterThan32; + std::uint64_t first = 0; + std::uint64_t second = 0; + const bool firstEvaluated = evaluate(frame, node.arguments[0], first); + if (firstEvaluated && static_cast(first) == (greater ? 0u : 0xffffffffu)) { + result = 0u; + return true; + } + const bool secondEvaluated = evaluate(frame, node.arguments[1], second); + if (secondEvaluated && static_cast(second) == (greater ? 0xffffffffu : 0u)) { + result = 0u; + return true; + } + if (!firstEvaluated || !secondEvaluated) return false; + const auto lhs = static_cast(greater ? second : first); + const auto rhs = static_cast(greater ? first : second); + result = lhs < rhs ? 1u : 0u; + return true; + } std::array operand{}; std::int32_t origin = -1; for (std::uint8_t index = 0; index < node.argumentCount; ++index) { @@ -505,8 +525,8 @@ bool SrtTape::compute(Frame& frame, const Node& node, std::uint64_t& result) con case Op::IEqual32: result = static_cast(a) == static_cast(b) ? 1u : 0u; return true; case Op::INotEqual32: result = static_cast(a) != static_cast(b) ? 1u : 0u; return true; case Op::SLessThan32: result = std::bit_cast(static_cast(a)) < std::bit_cast(static_cast(b)) ? 1u : 0u; return true; - case Op::ULessThan32: result = static_cast(a) < static_cast(b) ? 1u : 0u; return true; - case Op::UGreaterThan32: result = static_cast(a) > static_cast(b) ? 1u : 0u; return true; + case Op::ULessThan32: + case Op::UGreaterThan32: return false; case Op::LogicalAnd: case Op::LogicalOr: return false; case Op::LogicalXor: result = (a != 0u) != (b != 0u) ? 1u : 0u; return true; diff --git a/core/shader/recompiler/Optimization/src/TessellationLowering.cpp b/core/shader/recompiler/Optimization/src/TessellationLowering.cpp index 79a616db94..690d3bde1e 100644 --- a/core/shader/recompiler/Optimization/src/TessellationLowering.cpp +++ b/core/shader/recompiler/Optimization/src/TessellationLowering.cpp @@ -253,6 +253,82 @@ bool Implies(IrValue* rawPredicate, IrValue* rawCondition) { return predicate->Opcode() == IrOpcode::LogicalAnd && (Implies(predicate->Argument(0), condition) || Implies(predicate->Argument(1), condition)); } +bool ImpliesNot(IrValue* rawPredicate, IrValue* rawCondition) { + IrValue* predicate = rawPredicate->Resolve(); + IrValue* condition = rawCondition->Resolve(); + if (condition->HasImmediate() && condition->Type() == IrType::Bool && !condition->ImmediateBool()) return true; + if (condition->Opcode() == IrOpcode::LogicalNot) return Implies(predicate, condition->Argument(0)); + if (predicate->Opcode() == IrOpcode::LogicalNot) return Implies(condition, predicate->Argument(0)); + return predicate->Opcode() == IrOpcode::LogicalAnd && (ImpliesNot(predicate->Argument(0), condition) || ImpliesNot(predicate->Argument(1), condition)); +} + +bool CarriesRelativeBits(IrOpcode opcode) { + switch (opcode) { + case IrOpcode::Identity: + case IrOpcode::Phi: + case IrOpcode::IAdd32: + case IrOpcode::ISub32: + case IrOpcode::IMul32: + case IrOpcode::ShiftLeftLogical32: + case IrOpcode::ShiftRightLogical32: + case IrOpcode::ShiftRightArithmetic32: + case IrOpcode::BitwiseAnd32: + case IrOpcode::BitwiseOr32: + case IrOpcode::BitwiseXor32: + case IrOpcode::BitwiseNot32: + case IrOpcode::BitFieldUExtract: + case IrOpcode::BitFieldSExtract: + case IrOpcode::BitFieldInsert: + case IrOpcode::Select: + case IrOpcode::SelectU1: + case IrOpcode::SelectU32: + case IrOpcode::SelectF32: + case IrOpcode::BitCastU32F32: + case IrOpcode::BitCastF32U32: + case IrOpcode::BitCastU16F16: + case IrOpcode::BitCastF16U16: + case IrOpcode::ConvertF32U32: + case IrOpcode::ConvertU32F32: + case IrOpcode::ConvertF32S32: + case IrOpcode::ConvertS32F32: + case IrOpcode::ConvertFToU32: + case IrOpcode::ConvertUToF32: + case IrOpcode::ConvertFToI32: + case IrOpcode::ConvertIToF32: + case IrOpcode::ConvertF32F16: + case IrOpcode::ConvertF16F32: + case IrOpcode::ConvertU16U32: + case IrOpcode::ConvertU32U16: + case IrOpcode::ConvertU8U32: + case IrOpcode::ConvertU32U8: + case IrOpcode::FPAdd32: + case IrOpcode::FPSub32: + case IrOpcode::FPMul32: + case IrOpcode::FPMad32: + case IrOpcode::FPFma32: + case IrOpcode::FFma32: + case IrOpcode::FPMin32: + case IrOpcode::FPMax32: + case IrOpcode::FPNeg32: + case IrOpcode::FPAbs32: + case IrOpcode::CompositeConstructU64: + case IrOpcode::CompositeConstructU32x2: + case IrOpcode::CompositeConstructU32x3: + case IrOpcode::CompositeConstructU32x4: + case IrOpcode::CompositeExtractU64: + case IrOpcode::CompositeExtractU32x2: + case IrOpcode::CompositeExtractU32x3: + case IrOpcode::CompositeExtractU32x4: + return true; + default: + return false; + } +} + +bool IsRelativeSelect(IrOpcode opcode) { + return opcode == IrOpcode::Select || opcode == IrOpcode::SelectU1 || opcode == IrOpcode::SelectU32 || opcode == IrOpcode::SelectF32; +} + class AddressEvaluator { public: AddressEvaluator(std::uint32_t vertexCount, std::int64_t relativeCount) : vertexCount(vertexCount), relativeCount(relativeCount) {} @@ -784,33 +860,86 @@ private: return address && address->dynamic && address->relative == stride; } + bool dependsOnRelative(IrValue* raw, IrValue* rawPredicate) { + if (raw == nullptr) return false; + IrValue* value = raw->Resolve(); + IrValue* predicate = rawPredicate == nullptr ? nullptr : rawPredicate->Resolve(); + if (value->HasImmediate()) return false; + const auto key = std::pair{value, predicate}; + if (const auto found = dependence.find(key); found != dependence.end()) return found->second == Dependence::Yes; + const auto inserted = dependence.emplace(key, Dependence::Visiting); + if (!inserted.second) return false; + const bool result = [&] { + if (const auto form = evaluator.Evaluate(value, predicate)) return form->relative != 0; + if (IsRelativeSelect(value->Opcode()) && predicate != nullptr) { + IrValue* condition = value->Argument(0); + if (Implies(predicate, condition)) return dependsOnRelative(value->Argument(1), predicate); + if (ImpliesNot(predicate, condition)) return dependsOnRelative(value->Argument(2), predicate); + } + for (IrValue* argument : value->Arguments()) { + if (dependsOnRelative(argument, predicate)) return true; + } + return false; + }(); + inserted.first->second = result ? Dependence::Yes : Dependence::No; + return result; + } + + bool windowLoadObserves(IrValue* store) { + IrBlock* start = store->Parent(); + if (start == nullptr) return true; + std::unordered_set seen; + std::vector pending = start->Successors(); + bool loops = false; + while (!pending.empty()) { + IrBlock* block = pending.back(); + pending.pop_back(); + if (block == nullptr) continue; + if (block == start) { + loops = true; + continue; + } + if (!seen.insert(block).second) continue; + for (IrValue* inst : block->Instructions()) { + if (inst->Opcode() == IrOpcode::LoadVectorWindow) return true; + } + for (IrBlock* next : block->Successors()) pending.push_back(next); + } + bool after = false; + for (IrValue* inst : start->Instructions()) { + if (inst == store) { + after = true; + continue; + } + if (inst->Opcode() == IrOpcode::LoadVectorWindow && (after || loops)) return true; + } + return false; + } + + IrValue* effectPredicate(IrValue* user) const { + if (!user->MayHaveSideEffects() || user->ArgumentCount() == 0u) return nullptr; + IrValue* last = user->Argument(user->ArgumentCount() - 1u); + if (last == nullptr || last->Resolve()->Type() != IrType::Bool) return nullptr; + return last; + } + bool usedAsData(const std::vector& bases) { + dependence.clear(); std::vector pending(bases.begin(), bases.end()); std::unordered_set visited(bases.begin(), bases.end()); while (!pending.empty()) { IrValue* value = pending.back(); pending.pop_back(); for (IrValue* user : value->Uses()) { - switch (user->Opcode()) { - case IrOpcode::Identity: - case IrOpcode::IAdd32: - case IrOpcode::ISub32: - case IrOpcode::IMul32: - case IrOpcode::ShiftLeftLogical32: - case IrOpcode::ShiftRightLogical32: - case IrOpcode::BitwiseAnd32: - case IrOpcode::BitwiseOr32: - case IrOpcode::BitFieldUExtract: - case IrOpcode::SelectU32: - case IrOpcode::Phi: + if (user == nullptr) continue; + if (CarriesRelativeBits(user->Opcode())) { if (visited.insert(user).second) pending.push_back(user); continue; - default: - break; } if (dynamicInputAddress(user, value)) continue; - const auto address = evaluator.Evaluate(value, nullptr); - if (!address || address->relative != 0) return true; + if (!dependsOnRelative(value, effectPredicate(user))) continue; + if (user->Opcode() == IrOpcode::StoreVectorWindow && !windowLoadObserves(user)) continue; + return true; } } return false; @@ -1075,10 +1204,13 @@ private: bool merged; AddressEvaluator evaluator; std::vector accesses; + enum class Dependence : std::uint8_t { Visiting, No, Yes }; + std::uint32_t localStride; std::uint32_t controlStride = 0; std::uint32_t patchStride = 0; std::uint32_t sharedStride = 0; + std::map, Dependence> dependence; }; } diff --git a/core/shader/recompiler/Recompiler.cpp b/core/shader/recompiler/Recompiler.cpp index a149307af2..fad7606ef9 100644 --- a/core/shader/recompiler/Recompiler.cpp +++ b/core/shader/recompiler/Recompiler.cpp @@ -246,6 +246,7 @@ IrProgram PrepareResourceProgram(const RecompileRequest& request, ShaderPreparat translateOptions.floatMode = request.context.floatMode; translateOptions.float32FlushToZero = HardwareFloat32Flush(request); translateOptions.inputInfo = inputInfo; + translateOptions.float32DenormPreserve = request.target.float32DenormPreserve; constexpr InstructionTranslator translator; @@ -499,11 +500,7 @@ std::shared_ptr getSource(const RecompileRequest& request, ShaderPr std::vector planKey; if (disk) { ShaderDiskCache::BuildPlanKey(request, HostSubgroupSize(request), planKey); - IrResourcePlan loaded; - if (ShaderDiskCache::LoadPlan(planKey, loaded)) { - loaded.shaderHash = request.shader.codeAddress; - source->plan = std::make_shared(std::move(loaded)); - } + source->plan = ShaderDiskCache::LoadSharedPlan(planKey, request.shader.codeAddress); } if (source->plan == nullptr) { try { @@ -1091,7 +1088,13 @@ RecompileResult materializeResult(const CompiledVariant& variant, const Recompil formatComponents = 1u; for (std::uint32_t component = 0; component < 4u; ++component) { const auto selector = (input.outputMask & (1u << component)) != 0u ? (resource.fields[3] >> (component * 3u)) & 7u : 0u; - if (selector == 2u || selector == 3u) throw std::runtime_error("reserved prepared vertex component selector"); + if (selector == 2u || selector == 3u) { + const auto& fields = resource.fields; + char message[192]; + std::snprintf(message, sizeof(message), "reserved prepared vertex component selector %u at location %u component %u (V#=[%08x %08x %08x %08x])", + selector, input.location, component, fields[0], fields[1], fields[2], fields[3]); + throw std::runtime_error(message); + } if (selector >= 4u) formatComponents = std::max(formatComponents, selector - 3u); selectors |= selector << (component * 3u); } diff --git a/core/shader/recompiler/ShaderCacheVersion.cmake b/core/shader/recompiler/ShaderCacheVersion.cmake index 847a513496..f4477ed1e1 100644 --- a/core/shader/recompiler/ShaderCacheVersion.cmake +++ b/core/shader/recompiler/ShaderCacheVersion.cmake @@ -26,6 +26,7 @@ endforeach() list(SORT sources) list(SORT guard) get_filename_component(rootReal "${ROOT}" REALPATH) +get_filename_component(recompilerReal "${RECOMPILER_DIR}" REALPATH) set(sourcesReal) foreach(source IN LISTS sources) get_filename_component(real "${source}" REALPATH) @@ -39,8 +40,11 @@ foreach(source IN LISTS sources) if(NOT EXISTS "${source}") continue() endif() - file(SHA256 "${source}" digest) - file(RELATIVE_PATH relative "${ROOT}" "${source}") + get_filename_component(sourceReal "${source}" REALPATH) + file(READ "${source}" text) + string(REPLACE "\r\n" "\n" text "${text}") + string(SHA256 digest "${text}") + file(RELATIVE_PATH relative "${rootReal}" "${sourceReal}") string(APPEND manifest "${relative} ${digest}\n") if(NOT source MATCHES "\\.(cpp|hpp|h)$") continue() @@ -50,11 +54,11 @@ foreach(source IN LISTS sources) string(REGEX MATCHALL "\"APS5_[A-Z0-9_]+\"" names "${line}") list(APPEND switches ${names}) endforeach() - string(FIND "${source}" "${RECOMPILER_DIR}/" inRecompiler) + string(FIND "${sourceReal}" "${recompilerReal}/" inRecompiler) if(NOT inRecompiler EQUAL 0) continue() endif() - get_filename_component(sourceDirectory "${source}" DIRECTORY) + get_filename_component(sourceDirectory "${sourceReal}" DIRECTORY) file(STRINGS "${source}" lines ENCODING UTF-8 REGEX "^[ \t]*#[ \t]*include[ \t]*\"[^\"]+\"") foreach(line IN LISTS lines) string(REGEX REPLACE "^[ \t]*#[ \t]*include[ \t]*\"([^\"]+)\".*$" "\\1" included "${line}") @@ -65,7 +69,7 @@ foreach(source IN LISTS sources) break() endif() endforeach() - file(RELATIVE_PATH relativeSource "${ROOT}" "${source}") + file(RELATIVE_PATH relativeSource "${rootReal}" "${sourceReal}") if(NOT resolved) continue() endif() diff --git a/core/shader/recompiler/ShaderDiskCache.cpp b/core/shader/recompiler/ShaderDiskCache.cpp index 225392cdd8..aca1cb4eea 100644 --- a/core/shader/recompiler/ShaderDiskCache.cpp +++ b/core/shader/recompiler/ShaderDiskCache.cpp @@ -95,7 +95,7 @@ static_assert(sizeof(FragmentParameter) == 16, "FragmentParameter changed: updat static_assert(sizeof(CompiledShaderInfo) == 352, "CompiledShaderInfo changed: update the info encoder"); static_assert(sizeof(ShaderInfo) == 232, "ShaderInfo changed: update the info encoder"); static_assert(sizeof(BufferResource) == 32, "BufferResource changed: update the info encoder"); -static_assert(sizeof(ImageResource) == 92, "ImageResource changed: update the info encoder"); +static_assert(sizeof(ImageResource) == 96, "ImageResource changed: update the info encoder"); static_assert(sizeof(SamplerResource) == 24, "SamplerResource changed: update the info encoder"); static_assert(sizeof(SampledResourcePair) == 12, "SampledResourcePair changed: update the info encoder"); static_assert(sizeof(StageInput) == 56, "StageInput changed: update the info encoder"); @@ -141,7 +141,7 @@ constexpr std::size_t fieldCount() { static_assert(fieldCount() == 24, "ShaderInfo changed: update the info encoder"); static_assert(fieldCount() == 12, "BufferResource changed: update the info encoder"); -static_assert(fieldCount() == 42, "ImageResource changed: update the info encoder"); +static_assert(fieldCount() == 43, "ImageResource changed: update the info encoder"); static_assert(fieldCount() == 8, "SamplerResource changed: update the info encoder"); static_assert(fieldCount() == 3, "SampledResourcePair changed: update the info encoder"); static_assert(fieldCount() == 5, "StageInput changed: update the info encoder"); @@ -174,6 +174,49 @@ std::size_t PayloadOffset(std::size_t keyBytes) { return (sizeof(FileHeader) + keyBytes + alignment - 1) / alignment * alignment; } +class SharedPlanTable { +public: + std::shared_ptr Get(std::span payload, std::uint64_t shaderHash) { + const Key key{HashBytes(payload), HashBytes(payload, 0x5eed0003ull), payload.size(), shaderHash}; + { + std::lock_guard lock(mutex); + if (const auto found = plans.find(key); found != plans.end()) { + if (auto plan = found->second.lock()) return plan; + } + } + IrResourcePlan decoded; + if (!DecodePlan(payload, decoded)) return nullptr; + decoded.shaderHash = shaderHash; + auto plan = std::make_shared(std::move(decoded)); + std::lock_guard lock(mutex); + auto& slot = plans[key]; + if (auto existing = slot.lock()) return existing; + slot = plan; + if (++inserts % 1024u == 0u) std::erase_if(plans, [](const auto& entry) { return entry.second.expired(); }); + return plan; + } + +private: + struct Key { + std::uint64_t low; + std::uint64_t high; + std::uint64_t bytes; + std::uint64_t shaderHash; + bool operator==(const Key&) const = default; + }; + struct KeyHash { + std::size_t operator()(const Key& key) const { return static_cast(key.low ^ (key.high * 0x9e3779b97f4a7c15ull) ^ (key.shaderHash * 0xff51afd7ed558ccdull)); } + }; + std::mutex mutex; + std::unordered_map, KeyHash> plans; + std::uint64_t inserts = 0; +}; + +SharedPlanTable& sharedPlans() { + static SharedPlanTable table; + return table; +} + class MappedFile { public: MappedFile(const void* data, std::size_t size) : data(data), size(size) {} @@ -756,6 +799,7 @@ void encodeShaderInfo(Writer& writer, const ShaderInfo& info) { out.Value(image.volumeArraySampleCompatible); out.Value(image.flatLineCompatible); out.Value(image.flatVolumeLineCompatible); + out.Value(image.dimensionQueryOnly); out.Value(image.foldTexelOffsetsOnly); out.Value(image.convertedSamples); out.Value(image.remappedDepth1D); @@ -863,6 +907,7 @@ void decodeShaderInfo(Reader& reader, ShaderInfo& info) { in.Value(image.volumeArraySampleCompatible); in.Value(image.flatLineCompatible); in.Value(image.flatVolumeLineCompatible); + in.Value(image.dimensionQueryOnly); in.Value(image.foldTexelOffsetsOnly); in.Value(image.convertedSamples); in.Value(image.remappedDepth1D); @@ -1416,6 +1461,18 @@ public: return load(key, [&](std::span file, std::shared_ptr) { return DecodePlanEntry(file, key, plan); }, planHits, planMisses, planLoadFailures); } + std::shared_ptr LoadSharedPlan(std::span key, std::uint64_t shaderHash) { + std::shared_ptr plan; + load(key, [&](std::span file, std::shared_ptr) { + std::span payload; + const auto status = unwrapEntry(file, key, payload); + if (status != LoadStatus::Loaded) return status; + plan = sharedPlans().Get(payload, shaderHash); + return plan != nullptr ? LoadStatus::Loaded : LoadStatus::Rejected; + }, planHits, planMisses, planLoadFailures); + return plan; + } + void Store(std::vector key, std::shared_ptr variant) { enqueue({std::move(key), std::move(variant), nullptr}); } @@ -1925,6 +1982,11 @@ bool LoadPlan(std::span key, IrResourcePlan& plan) { return instance.Enabled() && instance.LoadPlan(key, plan); } +std::shared_ptr LoadSharedPlan(std::span key, std::uint64_t shaderHash) { + auto& instance = store(); + return instance.Enabled() ? instance.LoadSharedPlan(key, shaderHash) : nullptr; +} + void StorePlan(std::vector key, std::shared_ptr plan) { auto& instance = store(); if (instance.Enabled() && plan != nullptr) instance.StorePlan(std::move(key), std::move(plan)); diff --git a/core/shader/recompiler/ShaderDiskCache.hpp b/core/shader/recompiler/ShaderDiskCache.hpp index d007fc126a..d4cf9e2f30 100644 --- a/core/shader/recompiler/ShaderDiskCache.hpp +++ b/core/shader/recompiler/ShaderDiskCache.hpp @@ -13,7 +13,7 @@ namespace ShaderRecompiler::ShaderDiskCache { -inline constexpr std::uint32_t FormatVersion = 46; +inline constexpr std::uint32_t FormatVersion = 47; enum class LoadStatus { Loaded, @@ -71,6 +71,7 @@ void BuildPlanKey(const RecompileRequest& request, std::uint32_t hostSubgroupSiz void Store(std::vector key, std::shared_ptr variant); [[nodiscard]] std::shared_ptr Persist(std::span key, const CompiledVariant& variant); [[nodiscard]] bool LoadPlan(std::span key, IrResourcePlan& plan); +[[nodiscard]] std::shared_ptr LoadSharedPlan(std::span key, std::uint64_t shaderHash); void StorePlan(std::vector key, std::shared_ptr plan); void Flush(); void Shutdown(); diff --git a/core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvOptimizer.hpp b/core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvOptimizer.hpp index 661caa053c..f50d5c6f37 100644 --- a/core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvOptimizer.hpp +++ b/core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvOptimizer.hpp @@ -8,6 +8,7 @@ namespace ShaderRecompiler { [[nodiscard]] std::vector ValidateAndOptimizeSpirv(std::span spirv, std::uint32_t vulkanVersion, std::uint32_t spirvVersion, bool allowOffsetTextureOperand = false, bool optimize = true, bool specialize = false); +[[nodiscard]] std::uint64_t OptimizerHostThreads(); } diff --git a/core/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cpp b/core/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cpp index 07ad9585b8..601bed36a2 100644 --- a/core/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cpp +++ b/core/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cpp @@ -453,6 +453,10 @@ std::uint32_t QueryDimensions(SpirvValueEmitContext& ctx, const ImageEmitAccess& if (access.mem.imageDimension == RdnaImageDimension::Dim3D && dimension == RdnaImageDimension::Dim2D) { result[2] = ConstantU32(state, 1); } + const auto requested = access.mem.imageDimension; + if ((requested == RdnaImageDimension::Dim1D || requested == RdnaImageDimension::Dim2D || requested == RdnaImageDimension::Dim2DMsaa) && dimension != RdnaImageDimension::Dim3D) { + result[2] = ConstantU32(state, 0); + } if (info.multisampled == 0u) { result[3] = state.module.AllocateId(); state.module.AddFunction(spv::OpImageQueryLevels, TypeU32(state), result[3], image); diff --git a/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvMemoryInstructions.cpp b/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvMemoryInstructions.cpp index a6e4c11678..78314c2247 100644 --- a/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvMemoryInstructions.cpp +++ b/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvMemoryInstructions.cpp @@ -1441,31 +1441,31 @@ std::uint32_t SharedAtomic64(SpirvValueEmitContext& ctx, const IrValue& inst, TR const auto low = EmitMemoryElementIndex(state, resource, rawIndex); const auto high = EmitMemoryElementIndex(state, resource, Binary(state, spv::OpIAdd, TypeU32(state), rawIndex, ConstantU32(state, 1u))); return EmitValueOrDefaultIfCondition(state, EmitMemoryElementInBounds(state, resource, high), TypeU64(state), ConstantU64(state, 0u), [&]() { - if (state.requirements.ldsBufferInt64Atomics) { - const auto unaligned = Binary(state, spv::OpINotEqual, TypeBool(state), Binary(state, spv::OpBitwiseAnd, TypeU32(state), low, ConstantU32(state, 1u)), ConstantU32(state, 0u)); - EmitIfCondition(state, unaligned, [&] { RecordBdaFaultWords(state, Binary(state, spv::OpShiftLeftLogical, TypeU32(state), rawIndex, ConstantU32(state, 2u)), ConstantU32(state, 0u), ConstantU32(state, 8u), ConstantU32(state, inst.Flags().pc), BdaAbi::FaultReason::Unaligned); }); - const auto aligned = Unary(state, spv::OpLogicalNot, TypeBool(state), unaligned); - return EmitValueOrDefaultIfCondition(state, aligned, TypeU64(state), ConstantU64(state, 0u), [&]() { return LdsBufferAtomic64(state, low, [&](std::uint32_t old) { return replacement(state, old); }); }); - } - const auto update = [&]() { - const auto lowPointer = EmitMemoryElementPointer(state, resource, low); - const auto highPointer = EmitMemoryElementPointer(state, resource, high); - const auto lowValue = state.module.AllocateId(); - const auto highValue = state.module.AllocateId(); - state.module.AddFunction(spv::OpLoad, TypeU32(state), lowValue, lowPointer); - state.module.AddFunction(spv::OpLoad, TypeU32(state), highValue, highPointer); - const auto old = state.module.AllocateId(); - state.module.AddFunction(spv::OpCompositeConstruct, TypeU64(state), old, lowValue, highValue); - const auto next = Unary(state, spv::OpBitcast, TypeU64(state), replacement(state, Unary(state, spv::OpBitcast, TypeScalarU64(state), old))); - const auto nextLow = state.module.AllocateId(); - const auto nextHigh = state.module.AllocateId(); - state.module.AddFunction(spv::OpCompositeExtract, TypeU32(state), nextLow, next, 0u); - state.module.AddFunction(spv::OpCompositeExtract, TypeU32(state), nextHigh, next, 1u); - state.module.AddFunction(spv::OpStore, lowPointer, nextLow); - state.module.AddFunction(spv::OpStore, highPointer, nextHigh); - return old; - }; - return state.requirements.ldsLock ? LockedLdsUpdate(state, TypeU64(state), ConstantU64(state, 0u), update) : update(); + const auto unaligned = Binary(state, spv::OpINotEqual, TypeBool(state), Binary(state, spv::OpBitwiseAnd, TypeU32(state), rawIndex, ConstantU32(state, 1u)), ConstantU32(state, 0u)); + EmitIfCondition(state, unaligned, [&] { RecordBdaFaultWords(state, Binary(state, spv::OpShiftLeftLogical, TypeU32(state), rawIndex, ConstantU32(state, 2u)), ConstantU32(state, 0u), ConstantU32(state, 8u), ConstantU32(state, inst.Flags().pc), BdaAbi::FaultReason::Unaligned); }); + const auto aligned = Unary(state, spv::OpLogicalNot, TypeBool(state), unaligned); + return EmitValueOrDefaultIfCondition(state, aligned, TypeU64(state), ConstantU64(state, 0u), [&]() { + if (state.requirements.ldsBufferInt64Atomics) return LdsBufferAtomic64(state, low, [&](std::uint32_t old) { return replacement(state, old); }); + const auto update = [&]() { + const auto lowPointer = EmitMemoryElementPointer(state, resource, low); + const auto highPointer = EmitMemoryElementPointer(state, resource, high); + const auto lowValue = state.module.AllocateId(); + const auto highValue = state.module.AllocateId(); + state.module.AddFunction(spv::OpLoad, TypeU32(state), lowValue, lowPointer); + state.module.AddFunction(spv::OpLoad, TypeU32(state), highValue, highPointer); + const auto old = state.module.AllocateId(); + state.module.AddFunction(spv::OpCompositeConstruct, TypeU64(state), old, lowValue, highValue); + const auto next = Unary(state, spv::OpBitcast, TypeU64(state), replacement(state, Unary(state, spv::OpBitcast, TypeScalarU64(state), old))); + const auto nextLow = state.module.AllocateId(); + const auto nextHigh = state.module.AllocateId(); + state.module.AddFunction(spv::OpCompositeExtract, TypeU32(state), nextLow, next, 0u); + state.module.AddFunction(spv::OpCompositeExtract, TypeU32(state), nextHigh, next, 1u); + state.module.AddFunction(spv::OpStore, lowPointer, nextLow); + state.module.AddFunction(spv::OpStore, highPointer, nextHigh); + return old; + }; + return state.requirements.ldsLock ? LockedLdsUpdate(state, TypeU64(state), ConstantU64(state, 0u), update) : update(); + }); }); }); } diff --git a/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvModuleSetup.cpp b/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvModuleSetup.cpp index 27b431282b..cab4d0b74c 100644 --- a/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvModuleSetup.cpp +++ b/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvModuleSetup.cpp @@ -192,19 +192,24 @@ private: std::unordered_set active; }; -std::uint32_t IndexedWindowSlots(const IrProgram& program) { +constexpr std::uint64_t MaxNarrowedSwitchCases = 4096; + +std::uint32_t IndexedWindowSlots(const IrProgram& program, std::uint32_t laneCount) { const auto window = program.Metadata().vectorWindowSize; WindowIndexBounds bounds; std::uint64_t reached = 0; + std::uint64_t dynamicAccesses = 0; for (const auto& block : program.Blocks()) { for (const IrValue* inst : block->Instructions()) { if (inst->Opcode() != IrOpcode::LoadVectorWindow && inst->Opcode() != IrOpcode::StoreVectorWindow) continue; const IrValue* index = inst->Argument(0)->Resolve(); if (index->HasImmediate()) continue; + ++dynamicAccesses; const auto bound = bounds.Of(*index); if (bound && *bound < window / 2u) reached = std::max(reached, *bound + 1u); } } + if (reached == 0u || dynamicAccesses * (window - reached) * laneCount > MaxNarrowedSwitchCases) return 0u; return static_cast(reached); } @@ -245,7 +250,7 @@ void DefineModule(SpirvEmitterState& state) { } if (const auto window = state.program.Metadata().vectorWindowSize; window != 0u) { static const bool narrowWindow = std::getenv("APS5_NO_WINDOW_NARROWING") == nullptr; - state.vectorWindowIndexed = narrowWindow ? IndexedWindowSlots(state.program) : 0u; + state.vectorWindowIndexed = narrowWindow ? IndexedWindowSlots(state.program, state.laneCount) : 0u; for (std::uint32_t half = 0; half < state.laneCount; half++) { state.vectorWindowVariable.at(half) = state.module.AllocateId(); if (state.vectorWindowIndexed == 0u) continue; diff --git a/core/shader/recompiler/SpirvBackend/src/SpirvOptimizer.cpp b/core/shader/recompiler/SpirvBackend/src/SpirvOptimizer.cpp index 4a319c4f65..2b089a3e8f 100644 --- a/core/shader/recompiler/SpirvBackend/src/SpirvOptimizer.cpp +++ b/core/shader/recompiler/SpirvBackend/src/SpirvOptimizer.cpp @@ -3,16 +3,59 @@ #include #include #include +#include #include +#include #include #include #include +#include +#ifdef _WIN32 +#ifndef NOMINMAX +#define NOMINMAX +#endif +#include +#else +#include +#endif namespace ShaderRecompiler { namespace { constexpr std::uint32_t ScalarReplacementLimit = 64; +constexpr std::uintptr_t InlineOptimizerStackBytes = 256 * 1024; + +std::atomic optimizerHostThreads{0}; + +bool StackHoldsOptimizer() { +#ifdef _WIN32 + ULONG_PTR low = 0; + ULONG_PTR high = 0; + GetCurrentThreadStackLimits(&low, &high); +#else + struct Bounds { + std::uintptr_t low = 0; + std::uintptr_t high = 0; + Bounds() { + pthread_attr_t attributes; + if (pthread_getattr_np(pthread_self(), &attributes) != 0) return; + void* base = nullptr; + std::size_t size = 0; + if (pthread_attr_getstack(&attributes, &base, &size) == 0 && base != nullptr) { + low = reinterpret_cast(base); + high = low + size; + } + pthread_attr_destroy(&attributes); + } + }; + thread_local const Bounds bounds; + const auto low = bounds.low; + const auto high = bounds.high; +#endif + const auto frame = reinterpret_cast(__builtin_frame_address(0)); + return frame >= low && frame < high && frame - low >= InlineOptimizerStackBytes; +} bool HasLimitedUseTypes(std::span spirv) { if (spirv.size() < 5) throw std::runtime_error("SPIRV-Tools: incomplete module header"); @@ -101,9 +144,7 @@ std::vector CollapseBlockBallots(std::span s return collapsed; } -} - -std::vector ValidateAndOptimizeSpirv(std::span spirv, std::uint32_t vulkanVersion, std::uint32_t spirvVersion, bool allowOffsetTextureOperand, bool optimize, bool specialize) { +std::vector ValidateAndOptimizeSpirvInline(std::span spirv, std::uint32_t vulkanVersion, std::uint32_t spirvVersion, bool allowOffsetTextureOperand, bool optimize, bool specialize) { spv_target_env environment; const auto apiVersion = vulkanVersion & ~0xfffu; std::uint32_t maxSpirvVersion = 0; @@ -222,3 +263,26 @@ std::vector ValidateAndOptimizeSpirv(std::span ValidateAndOptimizeSpirv(std::span spirv, std::uint32_t vulkanVersion, std::uint32_t spirvVersion, bool allowOffsetTextureOperand, bool optimize, bool specialize) { + if (StackHoldsOptimizer()) return ValidateAndOptimizeSpirvInline(spirv, vulkanVersion, spirvVersion, allowOffsetTextureOperand, optimize, specialize); + std::vector result; + std::exception_ptr failure; + std::thread host([&] { + try { + result = ValidateAndOptimizeSpirvInline(spirv, vulkanVersion, spirvVersion, allowOffsetTextureOperand, optimize, specialize); + } catch (...) { + failure = std::current_exception(); + } + }); + optimizerHostThreads.fetch_add(1, std::memory_order_relaxed); + host.join(); + if (failure) std::rethrow_exception(failure); + return result; +} + +std::uint64_t OptimizerHostThreads() { + return optimizerHostThreads.load(std::memory_order_relaxed); +} + +} diff --git a/core/shader/recompiler/Translation/include/Translation/InstructionTranslator.hpp b/core/shader/recompiler/Translation/include/Translation/InstructionTranslator.hpp index 2c190b4239..21f6bc7e0e 100644 --- a/core/shader/recompiler/Translation/include/Translation/InstructionTranslator.hpp +++ b/core/shader/recompiler/Translation/include/Translation/InstructionTranslator.hpp @@ -36,6 +36,7 @@ struct TranslateOptions { bool interpolationFunctions = false; bool nativeFloat16 = false; bool float32FlushToZero = false; + bool float32DenormPreserve = false; bool gcnLegacyVertexInputs = false; std::optional floatMode; ShaderStageInputInfo inputInfo; diff --git a/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp b/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp index d856fad819..00da5a7db5 100644 --- a/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp +++ b/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp @@ -25,6 +25,7 @@ public: } void SetEntryRounding(const RoundingState& state) { setRounding(state); } void SetNativeFloat16(bool enabled) { nativeFloat16 = enabled; } + void SetFloat32DenormPreserve(bool enabled) { float32DenormPreserve = enabled; } void SetFloat32FlushToZero(bool enabled) { float32FlushToZero = enabled; } void AddBranchCondition(const BasicBlock& source, BlockInfo& info); void TranslateCodeTableLoad(const RdnaInstruction& instruction, const ControlFlowGraph::CodeTableLoad& table); @@ -81,7 +82,7 @@ private: IrU32 flushTinyF16Conversion(IrF32 value, IrU32 bits); void writeF16Conversion(const RdnaOperand& operand, IrF32 value, std::initializer_list sources); IrU32 readU32(const RdnaOperand& operand); - IrU32 flushF32Denormal(IrU32 bits); + IrU32 flushF32Denormal(IrU32 bits, std::uint32_t flushBit = 1u); IrF32 flushTinyProduct(IrValue* lhs, IrValue* rhs, IrValue* product, IrValue* addend = nullptr, bool afterRounding = true); IrU32 quietNan32(IrU32 bits); IrU32 quietNan16(IrU32 bits); @@ -387,6 +388,7 @@ private: RdnaOpcode currentOpcode = RdnaOpcode::Unknown; std::uint32_t f32DenormalFlush = 0u; bool float32FlushToZero = false; + bool float32DenormPreserve = false; std::uint32_t currentProgramCounter = 0; std::uint32_t currentVectorLimit = 1; }; diff --git a/core/shader/recompiler/Translation/src/InstructionTranslator.cpp b/core/shader/recompiler/Translation/src/InstructionTranslator.cpp index b15814b78e..08393a3942 100644 --- a/core/shader/recompiler/Translation/src/InstructionTranslator.cpp +++ b/core/shader/recompiler/Translation/src/InstructionTranslator.cpp @@ -599,6 +599,7 @@ IrProgram InstructionTranslator::Translate(const RdnaProgram& decoded, const Con context.SetPixelInput(options.inputInfo.pixel, options.fragmentShaderBarycentricEnabled); context.SetFloatMode(options.floatMode); context.SetNativeFloat16(options.nativeFloat16); + context.SetFloat32DenormPreserve(options.float32DenormPreserve); context.SetFloat32FlushToZero(options.float32FlushToZero); context.SetEntryRounding(entryRounding.at(cfgBlock.id)); for (std::uint32_t index = cfgBlock.instructionBegin; index < cfgBlock.instructionEnd; index++) { diff --git a/core/shader/recompiler/Translation/src/TranslationContext/OperandAccess.cpp b/core/shader/recompiler/Translation/src/TranslationContext/OperandAccess.cpp index d54715a372..ebec2355da 100644 --- a/core/shader/recompiler/Translation/src/TranslationContext/OperandAccess.cpp +++ b/core/shader/recompiler/Translation/src/TranslationContext/OperandAccess.cpp @@ -145,8 +145,8 @@ RdnaOperand TranslationContext::plainOperand(const RdnaOperand& operand) { return result; } -IrU32 TranslationContext::flushF32Denormal(IrU32 bits) { - if ((f32DenormalFlush & 1u) == 0u) return bits; +IrU32 TranslationContext::flushF32Denormal(IrU32 bits, std::uint32_t flushBit) { + if ((f32DenormalFlush & flushBit) == 0u) return bits; if (bits.Value().HasImmediate()) { const std::uint32_t value = bits.Value().ImmediateU32(); return (value & 0x7f800000u) == 0u ? IrU32(ir.Constant(value & 0x80000000u)) : bits; @@ -156,7 +156,7 @@ IrU32 TranslationContext::flushF32Denormal(IrU32 bits) { } IrF32 TranslationContext::flushTinyProduct(IrValue* lhs, IrValue* rhs, IrValue* product, IrValue* addend, bool afterRounding) { - if ((f32DenormalFlush & 2u) == 0u || keepsProductOutOfTinyRange(*lhs) || keepsProductOutOfTinyRange(*rhs)) return IrF32(*product); + if ((f32DenormalFlush & 2u) == 0u || ((f32DenormalFlush & 1u) != 0u && (keepsProductOutOfTinyRange(*lhs) || keepsProductOutOfTinyRange(*rhs)))) return IrF32(*product); IrValue& lhsBits = ir.BitCastU32(*lhs); IrValue& rhsBits = ir.BitCastU32(*rhs); const std::uint32_t roundMode = floatMode.has_value() ? floatMode->floatMode & 3u : 0u; @@ -249,7 +249,7 @@ void TranslationContext::writeOperand(const RdnaOperand& operand, IrValue* value } IrType type = value->Type(); if (type == IrType::F32) { - if ((f32DenormalFlush & 2u) != 0u) value = &ir.BitCastF32(flushF32Denormal(IrU32(ir.BitCastU32(*value))).Value()); + if ((f32DenormalFlush & 2u) != 0u) value = &ir.BitCastF32(flushF32Denormal(IrU32(ir.BitCastU32(*value)), 2u).Value()); value = &applyF32ResultModifiers(operand, IrF32(*value)).Value(); type = IrType::F32; } diff --git a/core/shader/recompiler/Translation/src/VectorInstructions.cpp b/core/shader/recompiler/Translation/src/VectorInstructions.cpp index c02cdba51e..f94148689f 100644 --- a/core/shader/recompiler/Translation/src/VectorInstructions.cpp +++ b/core/shader/recompiler/Translation/src/VectorInstructions.cpp @@ -49,12 +49,30 @@ std::uint32_t TranslationContext::f32DenormalFlushFor(const RdnaInstruction& ins case RdnaOpcode::VSubrevF32: case RdnaOpcode::VMulF32: case RdnaOpcode::VMulLegacyF32: + if (floatMode.has_value() && (floatMode->floatMode & 3u) == 0u) { + const std::uint32_t denormals = (floatMode->floatMode >> 4u) & 3u; + if (denormals == 1u || denormals == 2u) { + if (!float32DenormPreserve || float32FlushToZero) { + throw std::runtime_error("f32 denormal mode " + std::to_string(denormals) + " at pc " + std::to_string(inst.programCounter) + " needs DenormPreserve 32, which the device does not offer for this shader"); + } + return denormals == 1u ? 2u : 1u; + } + } + flush = 3u; + break; case RdnaOpcode::VMullitF32: + flush = 3u; + break; case RdnaOpcode::VMinF32: case RdnaOpcode::VMaxF32: case RdnaOpcode::VMin3F32: case RdnaOpcode::VMax3F32: case RdnaOpcode::VMed3F32: + if (!floatMode.has_value() || ((floatMode->floatMode >> 4u) & 1u) == 0u) return 3u; + if (((floatMode->floatMode >> 4u) & 3u) == 1u && (!float32DenormPreserve || float32FlushToZero)) { + throw std::runtime_error("f32 denormal mode 1 at pc " + std::to_string(inst.programCounter) + " needs DenormPreserve 32, which the device does not offer for this shader"); + } + return 0u; case RdnaOpcode::VFractF32: case RdnaOpcode::VFloorF32: case RdnaOpcode::VCeilF32: @@ -119,6 +137,12 @@ std::uint32_t TranslationContext::f32DenormalFlushFor(const RdnaInstruction& ins } const std::uint32_t denormals = floatMode.has_value() ? (floatMode->floatMode >> 4u) & 3u : 0u; if (denormals == 1u || denormals == 2u) { + if (inst.op == RdnaOpcode::VMullitF32 && (denormals == 2u || (floatMode->floatMode & 3u) == 0u)) { + if (!float32DenormPreserve || float32FlushToZero) { + throw std::runtime_error("f32 denormal mode " + std::to_string(denormals) + " at pc " + std::to_string(inst.programCounter) + " needs DenormPreserve 32, which the device does not offer for this shader"); + } + return denormals == 1u ? 2u : 1u; + } throw std::runtime_error("f32 denormal mode " + std::to_string(denormals) + " at pc " + std::to_string(inst.programCounter) + " is not implemented"); } if (denormals != 0u) return 0u; diff --git a/core/shader/recompiler/tests/test_shader_cache_native_paths.py b/core/shader/recompiler/tests/test_shader_cache_native_paths.py index b2f1e8c57f..9a28aab1e8 100644 --- a/core/shader/recompiler/tests/test_shader_cache_native_paths.py +++ b/core/shader/recompiler/tests/test_shader_cache_native_paths.py @@ -51,6 +51,24 @@ class ShaderCachePathTests(unittest.TestCase): def test_cjk(self): self.check_source_change('\u6e2c\u8a66 path') + def test_line_endings_keep_version(self): + with tempfile.TemporaryDirectory() as temporary: + root = Path(temporary) / 'line endings' + source, listing, output = self.fixture(root) + text = '#include \nconst char* mode = "APS5_TEST_PATH";\nint value = 1;\n' + source.write_bytes(text.encode('utf-8')) + result = self.run_script(root, listing, output) + self.assertEqual(result.returncode, 0, result.stdout + result.stderr) + unix = output.read_bytes() + source.write_bytes(text.replace('\n', '\r\n').encode('utf-8')) + result = self.run_script(root, listing, output) + self.assertEqual(result.returncode, 0, result.stdout + result.stderr) + self.assertEqual(unix, output.read_bytes()) + source.write_bytes(text.replace('= 1;', '= 2;').replace('\n', '\r\n').encode('utf-8')) + result = self.run_script(root, listing, output) + self.assertEqual(result.returncode, 0, result.stdout + result.stderr) + self.assertNotEqual(unix, output.read_bytes()) + def test_unicode_guard_rejects_unhashed_include(self): with tempfile.TemporaryDirectory() as temporary: root = Path(temporary) / '\u0441\u0431\u043e\u0440\u043a\u0430' @@ -74,5 +92,34 @@ class ShaderCachePathTests(unittest.TestCase): self.assertIn('does not hash', result.stdout + result.stderr) self.assertFalse(output.exists()) + @unittest.skipUnless(sys.platform == 'win32', 'Windows path separator semantics require Windows host') + def test_windows_path_separators(self): + with tempfile.TemporaryDirectory() as temporary: + root = Path(temporary) / 'windows separators' + source, listing, output = self.fixture(root) + windows_path = str(source).replace('/', '\\') + listing.write_text(f'sources {windows_path}\n', encoding='utf-8') + result = self.run_script(root, listing, output) + self.assertEqual(result.returncode, 0, result.stdout + result.stderr) + windows_version = output.read_bytes() + listing.write_text(f'sources {source.as_posix()}\n', encoding='utf-8') + result = self.run_script(root, listing, output) + self.assertEqual(result.returncode, 0, result.stdout + result.stderr) + self.assertEqual(windows_version, output.read_bytes()) + + @unittest.skipUnless(sys.platform == 'win32', 'Windows path separator semantics require Windows host') + def test_windows_path_separators_reject_unhashed_include(self): + with tempfile.TemporaryDirectory() as temporary: + root = Path(temporary) / 'windows separators' + source, listing, output = self.fixture(root) + windows_path = str(source).replace('/', '\\') + listing.write_text(f'sources {windows_path}\n', encoding='utf-8') + (root / 'shared.hpp').write_text('int example();\n', encoding='utf-8') + source.write_text('#include "../shared.hpp"\n', encoding='utf-8') + result = self.run_script(root, listing, output) + self.assertNotEqual(result.returncode, 0) + self.assertIn('does not hash', result.stdout + result.stderr) + self.assertFalse(output.exists()) + unittest.main() diff --git a/docs/dev/ARCHITECTURE.md b/docs/dev/ARCHITECTURE.md index 387d084365..6c47cfa197 100644 --- a/docs/dev/ARCHITECTURE.md +++ b/docs/dev/ARCHITECTURE.md @@ -124,7 +124,7 @@ flowchart LR - [`Recompiler.cpp`](../../core/shader/recompiler/Recompiler.cpp) runs the stages in this order. With `ANYPS5_ENABLE_SPIRV_TOOLS`, the SPIR-V is also validated and optimized with SPIRV-Tools. - `ShaderRecompiler::Recompile` keeps compiled variants in memory, and `ShaderDiskCache` stores them on disk so later runs reuse them. Before the writable cache, a miss is also looked up in read-only prebuilt packs and loose entry folders under `shader_cache/prebuilt`; pack entries are ordinary encoded cache entries behind a sorted key-hash index, validated by magic, compiler source version and index checksum when mapped, and a miss or rejected entry falls through to the writable cache and the compiler. Cache format 35 also stores the finite stencil-reference set, its replay-safety proof and reserved push-data word in the artifact and binding layouts. Entries from formats 31 to 34 are rejected. -- The Vulkan pipelines are journaled as well ([`PipelineJournal.cpp`](../../core/libs/prx/libSceAgcDriver/Graphics/src/PipelineJournal.cpp)): a graphics pipeline the store builds (`CachedPipeline`) or a compute pipeline a dispatch builds is appended to `pipelines.journal` next to the shader cache entries, as the materialized result of each stage (the specialized SPIR-V, bindings and push constants, through `ShaderDiskCache::EncodeResult`) plus the recipe the pipeline was built from (`GraphicsPipelineRecipe`: the stages' identities, vertex input layout, descriptor set layout keys and the state fields the `Pipeline` constructor reads, `CanonicalPipelineState`). When a `VulkanDevice` is created, the journal is loaded and its pipelines are built on a pool of `hardware_concurrency() / 2` threads (`APS5_PIPELINE_JOURNAL_THREADS`) while the title loads, so the first draw or dispatch of a configuration seen on an earlier run finds its pipeline resident. The pipeline stores key on `RecompileResult::Identity`, a hash of the module words, specialization constants, subgroup width and bindings that is the same on every run (`SharedSpirv::ContentHash`, computed once per shared module), rather than on the process-lifetime variant ids, and a graphics pipeline is built outside the store's mutex, so the replay threads and the queue workers build in parallel. A draw that arrives before its replayed entry builds the pipeline itself; the first inserted wins. A replayed entry enters the store at its least recently used end and a full store takes none, so the title's own pipelines are never evicted for a replayed one, and the optimized relink of a replayed pipeline (`OptimizedPipeline::Schedule`) waits for the first draw that binds it, so the relink thread serves the title's pipelines first. A device created without a window starts its replay at its first draw or dispatch rather than at creation, since `Driver::AttachWindow` usually replaces it before the title draws. The journal records a pipeline once (modules and recipes are deduplicated by content), keeps at most `APS5_PIPELINE_JOURNAL_MAX_MIB` (1024), replays the first `APS5_PIPELINE_JOURNAL_ENTRIES` recorded (1024, the pipeline store's bound, `APS5_PIPELINE_STORE_ENTRIES`; a run reaches those first), and is disabled with `APS5_NO_PIPELINE_JOURNAL=1` or without a shader cache directory. The file header names the device (`PipelineJournalDeviceFingerprint`, the properties the driver's pipeline cache file is named by), so a journal written for another GPU or driver is started over rather than replayed; appends and reads hold `pipelines.journal.lock` against another process sharing the directory, and a device cancels its replay before it is replaced (`PrepareForReplacement`) or destroyed, so a replaced headless device never inserts entries the window's device then sweeps. `APS5_PROFILE_DRAW=1` prints a `[pipeline-journal]` summary when the replay finishes and at teardown. +- The Vulkan pipelines are journaled as well ([`PipelineJournal.cpp`](../../core/libs/prx/libSceAgcDriver/Graphics/src/PipelineJournal.cpp)): a graphics pipeline the store builds (`CachedPipeline`) or a compute pipeline a dispatch builds is appended to `pipelines.journal` next to the shader cache entries, as the materialized result of each stage (the specialized SPIR-V, bindings and push constants, through `ShaderDiskCache::EncodeResult`) plus the recipe the pipeline was built from (`GraphicsPipelineRecipe`: the stages' identities, vertex input layout, descriptor set layout keys and the state fields the `Pipeline` constructor reads, `CanonicalPipelineState`). When a `VulkanDevice` is created, the journal is loaded and its pipelines are built on a pool of `hardware_concurrency() / 2` threads (`APS5_PIPELINE_JOURNAL_THREADS`) while the title loads. The load parses the file a record at a time and keeps it open, and a replay thread reads an entry's modules from it when it builds that entry (a record that no longer reads back as loaded rejects the entry), so the replay holds the modules of the entries being built rather than the file, so the first draw or dispatch of a configuration seen on an earlier run finds its pipeline resident. The pipeline stores key on `RecompileResult::Identity`, a hash of the module words, specialization constants, subgroup width and bindings that is the same on every run (`SharedSpirv::ContentHash`, computed once per shared module), rather than on the process-lifetime variant ids, and a graphics pipeline is built outside the store's mutex, so the replay threads and the queue workers build in parallel. A draw that arrives before its replayed entry builds the pipeline itself; the first inserted wins. A replayed entry enters the store at its least recently used end and a full store takes none, so the title's own pipelines are never evicted for a replayed one, and the optimized relink of a replayed pipeline (`OptimizedPipeline::Schedule`) waits for the first draw that binds it, so the relink thread serves the title's pipelines first. A device created without a window starts its replay at its first draw or dispatch rather than at creation, since `Driver::AttachWindow` usually replaces it before the title draws. The journal records a pipeline once (modules and recipes are deduplicated by content), keeps at most `APS5_PIPELINE_JOURNAL_MAX_MIB` (1024), replays the first `APS5_PIPELINE_JOURNAL_ENTRIES` recorded (1024, the pipeline store's bound, `APS5_PIPELINE_STORE_ENTRIES`; a run reaches those first), and is disabled with `APS5_NO_PIPELINE_JOURNAL=1` or without a shader cache directory. The file header names the device (`PipelineJournalDeviceFingerprint`, the properties the driver's pipeline cache file is named by), so a journal written for another GPU or driver is started over rather than replayed; appends and reads hold `pipelines.journal.lock` against another process sharing the directory, and a device cancels its replay before it is replaced (`PrepareForReplacement`) or destroyed, so a replaced headless device never inserts entries the window's device then sweeps. `APS5_PROFILE_DRAW=1` prints a `[pipeline-journal]` summary when the replay finishes and at teardown. - A compute shader whose LDS plus its lock dword exceeds the host's `maxComputeSharedMemorySize` is not given a `Workgroup` array. `Recompile` sets `ShaderInfo::sharedMemoryBytes` for it and the SPIR-V backend addresses the LDS in a coherent storage buffer at descriptor set 1 (`WorkgroupMemoryDescriptorSet`), with a slice of `RecompileResult::workgroupMemoryDwords` per workgroup, indexed by `WorkgroupID` and `NumWorkGroups`. `VulkanDevice` keeps one such buffer, sized from the dispatch dimensions, and binds it after the resource set; programs that fit keep using shared memory. - `Driver::RegisterShader` splits shader preparation in two. `PlanRegistered` runs on the registering guest thread: it validates the registers and entry point, decodes the code and builds the recompile request, so invalid registrations still fail there. The RDNA to IR translation and resource plan (`PrepareShader`) run on a background pool of `hardware_concurrency() / 2` detached threads (at least 2, 64 MiB stacks). - A thread that needs the GPU mutex, the committer's next item or the end of the committer's queue spins before it blocks (`HandoffSpin.hpp`): `GpuMutexType::lock` retries `try_lock` for the window, the committer polls its item count, and `DrawPipeline::Drain` polls it for twice the window, so a hand-off that completes within the window costs no kernel wake-up. The window is 150 us when the process may run on more than 8 logical processors (its affinity mask, via `CpuTopology`) and 0 otherwise, where the spinning threads would compete with the holder; `APS5_HANDOFF_SPIN_US=` sets it (0 turns it off, at most 10000). Ordering and the lock's semantics are unchanged. diff --git a/docs/dev/BUILD.md b/docs/dev/BUILD.md index 08190a503e..3693189557 100644 --- a/docs/dev/BUILD.md +++ b/docs/dev/BUILD.md @@ -31,9 +31,14 @@ git submodule update --init --recursive - Linux: GCC, G++, binutils. SDL's X11 backend requires X11 and Xext development headers (`libx11-dev` and `libxext-dev` on Debian/Ubuntu). SDL's Wayland backend requires the Wayland, xkbcommon and EGL development files (`libwayland-dev`, `libxkbcommon-dev` and `libegl-dev` on Debian/Ubuntu; `wayland-devel`, `libxkbcommon-devel` and `mesa-libEGL-devel` on Fedora), and `libdecor-0-dev` (`libdecor-devel`) for window decorations on GNOME. SDL loads both backends at run time and prefers Wayland when `WAYLAND_DISPLAY` is set; `SDL_VIDEODRIVER=x11` selects X11 (XWayland). Titles that bundle their own SDL read `SDL_VIDEODRIVER` too, and some of them fail to start with `x11`. Without the Wayland files configuration still succeeds, and the build uses X11 only. Audio output needs SDL's PipeWire, PulseAudio or ALSA backend, built from their development files (`libpipewire-0.3-dev`, `libpulse-dev` and `libasound2-dev` on Debian/Ubuntu; `pipewire-devel`, `pulseaudio-libs-devel` and `alsa-lib-devel` on Fedora). SDL loads them at run time. Without any of them SDL keeps only its silent dummy and disk drivers, so configuration fails. - Fedora: if CMake reports `Could NOT find Threads`, install `glibc-static` and `libstdc++-static` with `sudo dnf install glibc-static libstdc++-static`, then rerun configuration. The project enables static executable linking, and FFmpeg's thread check needs these libraries. - Windows: x86-64 MinGW-w64 GCC with POSIX threads. The compiler must provide DWARF exceptions with `-fno-asynchronous-unwind-tables`, or SEH exceptions for System V ABI functions; CMake checks these capabilities. WinLibs GCC 15.2.0 (`x86_64-ucrt-posix-seh`, release [`15.2.0posix-14.0.0-ucrt-r7`](https://github.com/brechtsanders/winlibs_mingw/releases/tag/15.2.0posix-14.0.0-ucrt-r7)) uses DWARF. [MSYS2 UCRT64 GCC](https://packages.msys2.org/packages/mingw-w64-ucrt-x86_64-gcc) 16.2.0-6 uses SEH. Add the selected toolchain's `bin` directory to `PATH` before configuring. -- Windows antivirus software can quarantine the executables that the relinker tests create in the temp directory, which fails `optional_plt`, `empty_tls`, `windows_address_space` and `windows_icon`. Set `TEMP` and `TMP` to a directory the scanner excludes before running `ctest`. +- Windows antivirus software can block or quarantine the executables that the relinker tests create and run, which fails `optional_plt`, `empty_tls`, `windows_address_space`, `windows_bss`, `windows_fixed_base` and `windows_icon`. CTest sets `TMPDIR`, `TEMP` and `TMP` of the relinker tests to `core/relinker/test-temp` in the build directory, so setting them before running `ctest` does not move these files; add that directory to the scanner's exclusions instead. - Bitdefender Advanced Threat Defense hooks Windows calls that `sceKernelRaiseException` makes while its target thread is suspended. When the target is suspended inside ntdll's exception dispatch while it holds an ntdll lock, the hook waits for that lock and `guest_raise_exception` hangs until its timeout. A scanning exception for the build directory does not prevent it; add `guest_raise_exception_tests.exe` to the Advanced Threat Defense exceptions. - FFmpeg binaries are downloaded during configuration unless `FFMPEG_PREBUILT_DIR` is set. With the WinLibs CMake, the download fails with status 60 (`SSL peer certificate or SSH remote key was not OK`) unless `SSL_CERT_FILE` names a CA bundle, for example `C:\Program Files\Git\ucrt64\etc\ssl\certs\ca-bundle.crt` from Git for Windows 2.56 and later (`mingw64` instead of `ucrt64` before 2.56), as in CI. +- Windows: libusb's `version_describe.h` is generated by `build-aux/gen-describe.ps1`, which CMake runs with `-ExecutionPolicy Bypass`. An execution policy set by Group Policy (`MachinePolicy` or `UserPolicy` in `Get-ExecutionPolicy -List`) takes precedence over that switch ([about_Execution_Policies](https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_execution_policies?view=powershell-5.1)); with `Restricted` or `AllSigned` the build stops at `version_describe.h` with `running scripts is disabled on this system`. The script only starts `build-aux/gen-describe.sh` with the `bash.exe` of Git for Windows, so a `.cmd` file that runs the shell script directly can replace PowerShell for this step without changing the policy. CMake passes it the `.ps1` path as the fifth argument and the source and output paths as the sixth and seventh; configure with `-DLIBUSB_POWERSHELL_EXECUTABLE=`: + + ```bat + @"C:\Program Files\Git\bin\bash.exe" "%~dpn5.sh" %6 %7 + ``` ## Web converter releases @@ -82,6 +87,8 @@ If configuration reports `Could NOT find Python3` or selects an unusable interpr cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++ -DPython3_EXECUTABLE="C:/path/to/python.exe" ``` +The documentation runs the Python tools as `python3`. With the python.org installer for Windows, which provides `python.exe` and the `py` launcher but no `python3.exe`, `python3` resolves to the Microsoft Store alias in `WindowsApps`, which prints `Python was not found` and exits with code 9009 (49 in Git Bash). Installs that provide `python3.exe`, such as the Python Install Manager, are not affected. In that case run these commands with `python` or `py -3`, for example `py -3 tools/check_conventions.py --base origin/main`. + If `gen-describe.ps1` fails while generating libusb's `version_describe.h`, check the Bash path printed in the build output. A WindowsApps or System32 `bash.exe` can launch WSL and fail with `WSL_E_DEFAULT_DISTRO_NOT_FOUND` when no distribution is installed. Install Git for Windows with Bash, or use Portable Git, and put its `cmd` and `usr\bin` directories on `PATH` before retrying the build. Use Bash and `cygpath.exe` from the same Git installation. After `cmake --build build --target libs --parallel` succeeds, copy the toolchain's runtime DLLs beside the patched libraries: @@ -186,6 +193,19 @@ build/core/libs/prx/libSceAgcDriver/agc_pm4_replay submits On Windows use `agc_pm4_replay.exe` and prepend `build/core/libs/libs/unpatched` to the process `PATH`. With `BUILD_TESTING=ON`, `agc_driver_pm4_replay` captures submissions made with the libSceAgc command builders through the driver and checks the index, the held waits, the conditional ranges, the dwords, the decode and the outcome of each wait, then checks hand-made captures: each release route; a store of another queue to the awaited bytes that entered before or after the store of the waiting queue (unknown); the label history limit, with the stores after the label on the waiting queue or on another queue; a fill the history does not keep; a store in an earlier submission overwritten later; later stores, rejected values and a missing rewind tail (unreleased); the six captures of the reviews (a two-queue deadlock, a store of 0 behind a released wait on another queue, with and without that wait held at submit, and labels pushed out of the history by stores of another queue), none of whose waits may be released except the one held at submit; five groups of waits in eight submission orders with identical outcomes; an index without the held and conditional columns; corrupt captures; then a failed write. `agc_driver_pm4_replay_tool` runs the tool on the driver capture and checks its exit codes and report. +## Frame capture and replay + +Set `APS5_FRAME_CAPTURE` to a directory to record a few frames of a title at the AGC driver boundary, then replay them with `agc_frame_replay` through any build of the driver with no title running. From the start of the process the driver keeps every shader registration and ABI resolution, video output registration and EOP flip reservation. At the frame boundary picked by `APS5_FRAME_CAPTURE_AT_FLIP` (default 1), or by the appearance of the file `APS5_FRAME_CAPTURE_TRIGGER` names (a number in it replaces the frame count), it waits for the driver to go idle and writes every committed, readable allocation of the guest allocation registry, the title's image included, as zstd chunks without their all-zero 64 KiB blocks. A frame ends at a CPU flip or at a submission that flips (a flip packet, or the EOP release of a reserved flip token); the reservation itself only records its token. For the next `APS5_FRAME_CAPTURE_FRAMES` (10) frames it records each submission (queue, command buffer address and size), flip and suspend point with the thread that made it and the accepted and completed serials, each preceded by the guest pages the write tracker saw the CPU write since the previous call that handed work to the GPU (`GuestMemory::CollectWrittenBlocks`, per 4 KiB where the tracker kept page stamps); an allocation the registry gained after the snapshot is recorded there as a new range with its nonzero 64 KiB blocks. A submission ends a frame only when it flips itself, not when another thread's submission flipped while it was being submitted. Shader registrations and ABI resolutions only collect the blocks of the shader header and the arrays it points to, and the pages of the title's image only when their content changed. At the end it stores each display buffer the title presented, decoded, as a reference image. A capture is `calls.bin`, `memory.bin` and `capture.json` (commit, switches, sizes, warnings), and the recorder stops with a warning when its volume has less than 4 GiB free. A 20-frame capture in the Demon's Souls Nexus is 5.3 GiB of snapshot and 11.5 GiB of deltas before compression, 3.9 GiB on disk. + +`agc_frame_replay [--no-pace] [--no-release] [--release-after-ms ] [--no-synthesize] [--strict] [--dump
::]... [--frames ]` maps the captured ranges at their addresses and registers them with their access (a range the replaying process already maps writable, such as the driver's fixed memory, is only refilled, and a non-GPU range it uses itself is skipped and counted), repeats the registrations, then replays the calls of each captured thread on a thread of its own, started in captured order. Before a call that hands work to the GPU it waits until no more submissions are in flight than when the title made it (`--no-pace` turns this off). Titles release GPU waits from threads that never call the driver, so when the driver completes nothing for 3 ms the replay reads the waits its queues are blocked on (`AgcDriverAwaitedWaits`) and writes the next recorded CPU value of the awaited dword that satisfies the compare, else the last earlier one, else a value derived from the reference (`--no-synthesize` turns this last step off). It prints one JSON line with the frame count and times, the submissions, the pacing wait, the released waits and how many were derived, the submissions that never completed and, for captures with reference images, the share of identical pixels and the largest channel difference of the replayed display buffers (`--frames` writes both images). Effects that depend on timing (temporal antialiasing, particles) make pixels differ, so expect a visual match rather than identical frames. Replay times follow the GPU clock state: lock the clocks (`nvidia-smi -lgc`, `-lmc`) when comparing two drivers. + +```sh +cmake --build build --target agc_frame_replay +build/core/libs/prx/libSceAgcDriver/agc_frame_replay capture +``` + +On Windows use `agc_frame_replay.exe` and prepend `build/core/libs/libs/unpatched` to the process `PATH`. With `BUILD_TESTING=ON`, `agc_frame_capture` checks the file format (round trip, zero-block elision, truncated and corrupt files), and `agc_driver_frame_capture_eop` reserves and submits six EOP flips with `APS5_FRAME_CAPTURE_FRAMES=3` and checks that the capture starts after the first flipping submission and holds three frames of one reservation and one submission each, then that a flipping submission on another thread leaves this thread's flip count unchanged, and `agc_driver_frame_replay` captures two frames of CPU writes followed by DMA copies, the second from a block committed during the capture, replays them in `agc_frame_replay` and compares the copies. + ## Pipeline statistics Set `APS5_PIPELINE_STATS=1` to capture and print driver statistics for each newly created graphics or compute pipeline. This requires `VK_KHR_pipeline_executable_properties` and `pipelineExecutableInfo`; an unsupported device fails with an error. Statistic names and units are driver-specific. Capturing statistics can increase pipeline compilation cost. The setting is disabled by default. diff --git a/docs/dev/ENVIRONMENT.md b/docs/dev/ENVIRONMENT.md index 3de2a085ee..3a728911cd 100644 --- a/docs/dev/ENVIRONMENT.md +++ b/docs/dev/ENVIRONMENT.md @@ -95,6 +95,7 @@ The code reads environment names through `getenv`, `IsEnvironmentSet`, `kibSetti | `APS5_CAPTURE_GPU_COPIES` | presence | `core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp` | | records GPU buffer copies in the capture trace (requires APS5_CAPTURE_TRACE) | | `APS5_CAPTURE_INPUTS` | presence | `core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp` | | records each draw's inputs in the capture trace (requires APS5_CAPTURE_TRACE) | | `APS5_CAPTURE_TRACE` | presence | `core/libs/prx/libSceAgcDriver/Execution/include/CaptureTrace.hpp` | | writes capture events to capture-trace.log | +| `APS5_CAPTURE_TRACE_PATH` | string | `core/libs/prx/libSceAgcDriver/Execution/include/CaptureTrace.hpp` | capture-trace.log | file path for the capture events log | | `APS5_CHECK_INDIRECT_ARGS` | presence | `core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp` | | reads GPU-side indirect draw records back on the CPU and prints the first ones | | `APS5_COMPLETION_STORE_SYNC` | presence | `core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp` | | makes a store made by a completion action wait for later batches over its range | | `APS5_COPY_DESTINATION_STRICT` | presence | `core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp` | | refuses a CPU copy whose destination has any pending writer, not only an unsettled one | @@ -111,6 +112,7 @@ The code reads environment names through `getenv`, `IsEnvironmentSet`, `kibSetti | `APS5_CPU_STORES` | presence | `core/libs/prx/libSceAgcDriver/Execution/src/Driver/Packets/PacketMemory.cpp` | | writes packet stores on the CPU instead of recording them on the GPU | | `APS5_DCC_KEYS_EACH` | presence | `core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp` | | records every DCC key store at once instead of batching them | | `APS5_DEPTH_COPY_MIN_KIB` | integer | `core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp` | 1024 | minimum surface size in KiB for a depth target to take the depth-copy path | +| `APS5_DEPTH_PLANE_COPY_MIB` | integer | `core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp` | 256 | budget in MiB of the sampled depth-plane copies kept for reuse (least recently used copies leave first); `0` keeps every copy | | `APS5_DEPTH_PLANE_WHOLE_STORE` | presence | `core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp` | | stores a stale depth plane whole when memory over it is read, instead of only the 64 KiB units the read overlaps | | `APS5_DISPATCH_CACHE_BYTES` | integer | `core/libs/prx/libSceAgcDriver/Execution/src/Driver/Dispatch/DispatchCache.cpp` | 268435456 | size of the dispatch cache in bytes (0 keeps the default of 256 MiB) | | `APS5_DISPATCH_CACHE_ENTRIES` | integer | `core/libs/prx/libSceAgcDriver/Execution/src/Driver/Dispatch/DispatchCache.cpp` | 8192 | number of dispatch cache entries; 0 picks a default from the variant count | @@ -150,6 +152,10 @@ The code reads environment names through `getenv`, `IsEnvironmentSet`, `kibSetti | `APS5_FLIP_READ_CHECK` | presence | `core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp` | | records the in-place reads of completed batches and checks them before the present blit's fence | | `APS5_FORCE_SDR` | presence | `core/libs/prx/libSceAgcDriver/Execution/src/HostHdr.cpp` | unset | suppresses advertised HDR availability while retaining detected host capabilities; uses the existing SDR presentation path | | `APS5_FORCE_STRUCTURIZER_FALLBACK` | presence | `core/libs/prx/libSceAgcDriver/tests/ControlFlow.cpp` | | makes the control-flow test expect the reach-flag fallback instead of no cloning | +| `APS5_FRAME_CAPTURE` | string | `core/libs/prx/libSceAgcDriver/Execution/src/FrameRecorder.cpp` | | directory in which the driver records a few frames of the title's driver calls and guest memory for `agc_frame_replay` | +| `APS5_FRAME_CAPTURE_AT_FLIP` | integer | `core/libs/prx/libSceAgcDriver/Execution/src/FrameRecorder.cpp` | 1 | frame boundary (CPU flip or submission that flips) at which the frame capture starts when no trigger file is set | +| `APS5_FRAME_CAPTURE_FRAMES` | integer | `core/libs/prx/libSceAgcDriver/Execution/src/FrameRecorder.cpp` | 10 | number of frame boundaries the frame capture records | +| `APS5_FRAME_CAPTURE_TRIGGER` | string | `core/libs/prx/libSceAgcDriver/Execution/src/FrameRecorder.cpp` | | file whose appearance starts the frame capture at the next frame boundary; a number in it replaces the frame count | | `APS5_FULL_BARRIERS` | presence | `core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp` | | records every barrier instead of merging redundant ones | | `APS5_GPU_CHECKPOINTS` | presence | `core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp` | | attaches device diagnostic checkpoints through VK_NV_device_diagnostic_checkpoints, or buffer markers through VK_AMD_buffer_marker on a device without it | | `APS5_GPU_COPY_MAX_KIB` | integer | `core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp` | 1024 | size cap in KiB on a descriptor sub-range copied through the GPU path, larger ones staying on the CPU | @@ -169,7 +175,7 @@ The code reads environment names through `getenv`, `IsEnvironmentSet`, `kibSetti | `APS5_KEPT_DESCRIPTOR_SETS` | integer | `core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp` | 8 | number of freed dedicated image table descriptor sets kept for a later build with the same layout and sizes; 0 keeps none | | `APS5_KEYFLIP_DROP` | presence | `core/libs/prx/libSceAgcDriver/Graphics/src/Texture.cpp` | | drops a pending unit's results on a clear-code to uncompressed key flip, as before | | `APS5_KEYS_FILL_CLEAR` | presence | `core/libs/prx/libSceAgcDriver/Graphics/src/Texture.cpp` | | clears an image from DCC keys whose fill pattern it matches (only when set to 1) | -| `APS5_LABEL_FLUSH_US` | integer | `core/libs/prx/libSceAgcDriver/Execution/src/Driver/Synchronization/DeferredLabels.cpp` | 1000 | age in microseconds at which a pending label batch is recorded or submitted | +| `APS5_LABEL_FLUSH_US` | integer | `core/libs/prx/libSceAgcDriver/Execution/src/Driver/Synchronization/DeferredLabels.cpp` | | age in microseconds at which a pending label batch is recorded or submitted; unset is 1000 with batched label submission and 250 when `APS5_NO_LABEL_BATCH_SUBMIT` or `APS5_NO_LABEL_BATCHING` turns it off | | `APS5_LABEL_GATE_ALL` | presence | `core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp` | | gates a label store on any pending completion, not only one overlapping the range | | `APS5_LABEL_LOCK_EACH` | presence | `core/libs/prx/libSceAgcDriver/Execution/src/Driver/Synchronization/DeferredLabels.cpp` | | records each label under the GpuMutex instead of deferring it | | `APS5_LABEL_OWN_LOCK` | presence | `core/libs/prx/libSceAgcDriver/Execution/src/Driver/Packets/PacketFlush.cpp` | | makes a label flush take the GpuMutex itself instead of relying on a self-locking packet | @@ -337,6 +343,7 @@ The code reads environment names through `getenv`, `IsEnvironmentSet`, `kibSetti | `APS5_NO_WRITE_EVIDENCE` | presence | `core/libs/prx/libSceAgcDriver/Execution/src/Driver/Memory/WriteEvidence.cpp` | | disables the written-element evidence that lets a read skip the flush hook | | `APS5_NO_WRITE_ONLY_PASSES` | presence | `core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp` | | counts a buffer a draw only stores to as read and written, so the next draw storing to it begins a new render pass | | `APS5_NO_WRITTEN_SHADOW` | presence | `core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp` | | binds written staging elements in place instead of through the written shadow | +| `APS5_NV_DIAGNOSTICS` | presence | `core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp` | | creates the device with VK_NV_device_diagnostics_config and VK_NV_device_diagnostic_checkpoints where supported, for Nsight Aftermath crash dumps | | `APS5_PACKET_EPOCH` | presence | `core/libs/prx/libSceAgcDriver/Execution/src/Driver/Synchronization/WaitStatistics.cpp` | | makes queue workers bump the collect epoch before every packet | | `APS5_PIPELINED_DRAWS` | integer | `core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawPipeline.cpp` | 8 | depth of the queue-0 draw pipeline: 0 records draws inline, 1 selects the default depth of 8, and any other value is the depth capped at 256 | | `APS5_PIPELINE_DISPATCH` | presence | `core/libs/prx/libSceAgcDriver/Execution/src/Driver/Dispatch/Dispatch.cpp` | | records an eligible dispatch as an item in the queue-0 pipeline instead of running it inline | @@ -344,8 +351,8 @@ The code reads environment names through `getenv`, `IsEnvironmentSet`, `kibSetti | `APS5_PIPELINE_DRAIN_FILLS` | presence | `core/libs/prx/libSceAgcDriver/Execution/src/Driver/Memory/BufferFill.cpp` | | runs queue-0 buffer fills through the draw pipeline to keep their order; any value stores them in place instead | | `APS5_PIPELINE_DRAIN_LABELS` | presence | `core/libs/prx/libSceAgcDriver/Execution/src/Driver/Packets/PacketExecution.cpp` | | answers a queue-0 wait from the draw pipeline's own pending labels while it is busy; any value drains the pipeline at the wait | | `APS5_PIPELINE_DRAIN_SUBMISSIONS` | presence | `core/libs/prx/libSceAgcDriver/Execution/src/Driver/Packets/PacketExecution.cpp` | | lets a submission end without submitting its recorded work, so it joins a later submit with the pipeline's queue; any value drains the pipeline and submits at the end of the submission | -| `APS5_PIPELINE_JOURNAL_ENTRIES` | integer | `core/libs/prx/libSceAgcDriver/Graphics/src/PipelineJournal.cpp` | 1024 | number of journal entries loaded at most; a longer journal is truncated to this count | -| `APS5_PIPELINE_JOURNAL_MAX_MIB` | integer | `core/libs/prx/libSceAgcDriver/Graphics/src/PipelineJournal.cpp` | 1024 | size cap in MiB on the pipeline journal; later pipelines are not recorded once it is reached | +| `APS5_PIPELINE_JOURNAL_ENTRIES` | integer | `core/libs/prx/libSceAgcDriver/Graphics/src/PipelineJournal.cpp` | 1024 | number of journal entries loaded at most; a longer journal keeps its last entries, the ones recorded last | +| `APS5_PIPELINE_JOURNAL_MAX_MIB` | integer | `core/libs/prx/libSceAgcDriver/Graphics/src/PipelineJournal.cpp` | 1024 | size cap in MiB on the pipeline journal; the file is rewritten down to three quarters of the cap when it is reached, keeping the newest pipelines and the modules they name and leaving room for later records | | `APS5_PIPELINE_JOURNAL_THREADS` | integer | `core/libs/prx/libSceAgcDriver/Graphics/src/PipelineJournal.cpp` | | threads that replay the journal's pipelines, 0 disables the replay; unset uses half the hardware threads, at least one | | `APS5_PIPELINE_STATS` | string | `core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp` | | captures and prints driver statistics for each new pipeline, requiring `VK_KHR_pipeline_executable_properties`; `1` enables | | `APS5_PIPELINE_STORE_ENTRIES` | integer | `core/libs/prx/libSceAgcDriver/Graphics/src/Pipeline.cpp` | 1024 | LRU bound on the entries of the pipeline store; an entry is evicted only once no recorded draw holds it | @@ -385,6 +392,7 @@ The code reads environment names through `getenv`, `IsEnvironmentSet`, `kibSetti | `APS5_STORE_AT_LABEL` | presence | `core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp` | | flushes every pending storage-image result when a label is written on the GPU | | `APS5_SUBMISSION_EPOCH_EACH` | presence | `core/libs/prx/libSceAgcDriver/Execution/src/Driver/Packets/PacketExecution.cpp` | | bumps the guest-memory collect epoch on every submission instead of only when the submission is newer than the last bump | | `APS5_SUBMIT_AT_END` | presence | `core/libs/prx/libSceAgcDriver/Execution/src/Driver/Packets/PacketExecution.cpp` | | submits recorded work at the end of a submission even when a worker is queued | +| `APS5_SUBMIT_PROGRESS` | presence | `core/libs/prx/libSceAgcDriver/Execution/src/Driver/Queues/Submission.cpp` | | enables opt-in pacing for eligible queue-zero submission prefixes within a shared nominal two-millisecond acknowledgement/timeline budget; timeout retains asynchronous acceptance | | `APS5_SUSPEND_DRAIN` | presence | `core/libs/prx/libSceAgcDriver/Execution/src/Driver/Packets/PacketExecution.cpp` | | drains the device at a suspend point | | `APS5_SYNC_COMPLETION_DRAWS` | presence | `core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp` | | keeps every draw with completion work synchronous | | `APS5_SYNC_DISPATCH` | presence | `core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp` | | waits for every dispatch instead of batching it | @@ -425,6 +433,7 @@ The code reads environment names through `getenv`, `IsEnvironmentSet`, `kibSetti | `APS5_TRACE_QUERY` | presence | `core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp` | | prints every 16th page query with its cost | | `APS5_TRACE_RESOURCE_KEY_MISSES` | presence | `core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp` | | reports shader-resource cache key misses | | `APS5_TRACE_SECONDS` | string | `core/libs/prx/libSceAgcDriver/Execution/include/PerformanceTrace.hpp` | | records from the first present `` seconds after the first present for `` seconds (whole numbers, `start:length`) | +| `APS5_TRACE_SHADER_CORRELATION` | presence | `core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/Draw.cpp` | | traces shader selection and preparation failure tokens through draw batches | | `APS5_TRACE_SHADER_REGS` | string | `core/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cpp` | | prints a shader's registers for one hex program address, or `all` | | `APS5_TRACE_SHADOW` | integer | `core/libs/prx/libSceAgcDriver/Graphics/src/UnitShadow.cpp` | | prints the first n shadow publishes, the first 64 seeds, every evict and retire | | `APS5_TRACE_SKIPS` | presence | `core/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderDiagnostics.cpp` | | reports each distinct shader skip once | @@ -521,7 +530,7 @@ The code reads environment names through `getenv`, `IsEnvironmentSet`, `kibSetti | Name | Kind | Source | Default | Description | |------|------|--------|---------|-------------| -| `ANYPS5_LANGUAGE` | integer | `core/libs/prx/libSceSystemService/Export.cpp` | 1 | console language number returned by sceSystemServiceParamGetInt, defaulting to English US | +| `ANYPS5_LANGUAGE` | integer | `core/libs/prx/libSceSystemService/SystemService.hpp` | 1 | console language number returned by sceSystemServiceParamGetInt, defaulting to English US | ### libSceVideoOut @@ -530,6 +539,8 @@ The code reads environment names through `getenv`, `IsEnvironmentSet`, `kibSetti | `ANYPS5_INPUT_CONFIG` | string | `core/libs/prx/libSceVideoOut/src/InputMapping.cpp` | | path of the input mapping file that binds host keys and mouse to guest pad actions | | `ANYPS5_REQUIRE_DISPLAY` | presence | `core/libs/prx/libSceVideoOut/tests/Flip.cpp` | | makes video-out tests fail instead of skipping when no display or Vulkan device is available | | `APS5_NO_PAD_INPUT` | presence | `core/libs/prx/libSceVideoOut/src/PadInput.cpp` | | ignores keyboard and mouse input so a measurement run is not advanced by a stray press | +| `APS5_NO_WGI` | presence | `core/libs/prx/libSceVideoOut/src/VideoOutDriver.cpp` | | turns off SDL's Windows.Gaming.Input joystick backend at default hint priority, on Windows hosts where it faults a few seconds into every run | +| `APS5_OUTPUT_RESOLUTION` | string | `core/libs/prx/libSceVideoOut/src/VideoOutDriver.cpp` | | `x` in pixels that replaces the host display's size when `sceVideoOutGetOutputStatus` and `sceVideoOutGetOutputStatusWithVrr` report the output resolution: 1 for a size below 3840 wide and 2160 high, 2 otherwise, as for the display; unset or empty follows the display, and any other value makes those calls throw | | `APS5_VIDEOOUT_TELEMETRY_DIR` | string | `core/libs/prx/libSceVideoOut/src/Telemetry.cpp` | | directory where videoout.jsonl is appended with the pad and present telemetry lines; nothing is opened when unset or empty | ### libSceVideodec2 @@ -542,12 +553,12 @@ The code reads environment names through `getenv`, `IsEnvironmentSet`, `kibSetti | Name | Kind | Source | Default | Description | |------|------|--------|---------|-------------| -| `APS5_CRASH_INDIRECT_MEMORY` | presence | `core/libs/prx/libc/src/specifics/windows/CrashReport.cpp` | | dumps the words the faulting registers point at in the Windows crash report | +| `APS5_CRASH_INDIRECT_MEMORY` | presence | `core/libs/prx/libc/src/specifics/windows/CrashReport.cpp` | | adds to the Windows crash report up to 64 bytes from each pointer in the dump of the words the registers point at, as `[[reg+off]] @addr` lines; off when unset or empty | | `APS5_CRASH_STACK_BYTES` | integer | `core/libs/prx/libc/src/specifics/windows/CrashReport.cpp` | 0 | bytes of the faulting thread's stack from rsp, decimal or 0x hexadecimal and at most 1048576, that the Windows crash report prints with the data at the stack words pointing outside the stack; 0 prints none | | `APS5_HANG_DUMP_SECS` | integer | `core/libs/prx/libc/src/specifics/windows/CrashReport.cpp` | | seconds after which a watchdog dumps all threads and aborts | | `APS5_HEAP_CACHE_MIB` | integer | `core/libs/prx/libc/src/GuestHeap.cpp` | 4096 | size in MiB of the cache of freed large blocks before the oldest are decommitted | | `APS5_HELPER_AFFINITY_MASK` | integer | `core/libs/prx/libc/include/CpuTopology.hpp` | | hex core mask that helper threads (deferred release, mirror refresh) are pinned to | -| `APS5_LOG_TIMESTAMPS` | presence | `core/libs/prx/libc/include/general/LogMacros.hpp` | | prefixes each log line with the milliseconds since the first log | +| `APS5_LOG_TIMESTAMPS` | presence | `core/libs/prx/libc/include/general/LogMacros.hpp` | | prefixes each log line with `[+S.UUUUUU s]`, the seconds with microsecond digits since the clock started at the first stamped line; on Windows each library has its own clock, started at that library's first stamped line | | `APS5_NO_PROCMAP_QUERY` | presence | `core/libs/prx/libc/include/specifics/linux/HostMapping.hpp` | | skips the /proc/self/maps PROCMAP_QUERY ioctl and scans the maps file instead | | `APS5_NO_SSE4A_EMULATION` | presence | `core/libs/prx/libc/src/specifics/windows/CrashReport.cpp` | | disables emulation of AMD-only SSE4a instructions, making such a fault fatal | | `APS5_NO_WRITE_WATCH` | presence | `core/libs/prx/libc/src/GuestArena.cpp` | | disables write watching so guest buffers are fully re-read on every use | diff --git a/docs/dev/PROGRESS.md b/docs/dev/PROGRESS.md index a1257f85a9..4e451630a3 100644 --- a/docs/dev/PROGRESS.md +++ b/docs/dev/PROGRESS.md @@ -22,6 +22,10 @@ Comparisons identify functions by library and name. When a head group becomes a Real removals and transitions back to stubs remain visible. Percentages retain the original snapshot totals, so eliminating duplicate accounting can change a percentage without changing available functionality. Older JSON snapshots without `shared_sources` are accepted. +PR reports include a table of changed libraries with implemented export counts, declared totals, percentages before and after, and changes in percentage points (`pp`). The table uses the same two JSON snapshots as the global report. A library appears when its counts, export names or shared-source owner change, including when its counts stay equal. Unchanged libraries are omitted, and the table lists at most 100 libraries in name order with a count of omitted rows. + +New or removed libraries show `Not present` on the missing side. Libraries whose snapshots contain both counts and report zero exports show `No exports`; if either count is absent, the table shows `Counts unavailable`. Wrappers show `Shared sources` with the source library's name. These rows show `n/a` for the percentage change. The table retains the scanner's counting rules; it does not measure compatibility. + Implemented definitions are also classified as behaviourally empty when their body holds only `(void)name;` discards and at most one constant return, bare `return;` or `throw`, including a return of a local `static` wrapper that is itself behaviourally empty; wrapper calls whose arguments contain calls do not qualify. Comparisons list newly implemented behaviourally empty functions without changing any count. Older JSON snapshots without `inert_names` are accepted. ## One library diff --git a/docs/dev/debt/libSceAgc.md b/docs/dev/debt/libSceAgc.md index b6719b8790..aa30dca0b0 100644 --- a/docs/dev/debt/libSceAgc.md +++ b/docs/dev/debt/libSceAgc.md @@ -27,7 +27,7 @@ - [sceAgcAcbMemSemaphore, sceAgcDcbMemSemaphore](../../../core/libs/prx/libSceAgc/Acb/src/Sync.cpp) (libSceAgc) - signature (cb, semaphoreAddr, waitForMailbox, signalType, operation) inferred from PPSA03396, whose wrapper passes the address second and operation 6 (signal) or 7 (wait) last; the three trailing arguments go to the packet as-is - [sceAgcCbMemSemaphore](../../../core/libs/prx/libSceAgc/Cb/src/Sync.cpp) (libSceAgc) - signature (cb, semaphoreAddr, operation, modifier, value) follows SharpProspero; writes 4-dword PM4 0x39 packet with control word 0x80100000 following console oracle measurements in prosper (cases agcbuild_CbMemSemaphore_a/b/c); operation, modifier and value are unused - [sceAgcAsyncCondExecPatchSetCommandAddress, sceAgcAsyncCondExecPatchSetEnd](../../../core/libs/prx/libSceAgc/Patch/src/CondExec.cpp) (libSceAgc) - no reference has a body for them; they are assumed to patch the COND_EXEC packet like `sceAgcCondExecPatchSetCommandAddress` and `sceAgcCondExecPatchSetEnd`, since `sceAgcAcbCondExec` writes the same packet as `sceAgcDcbCondExec`, as `sceAgcAsyncRewindPatchSetRewindState` does for REWIND -- [sceAgcAcbAtomicGds_0900](../../../core/libs/prx/libSceAgc/Acb/src/Control.cpp) (libSceAgc) - name from its NID (`gQkqkLttcpw`), signature unknown +- [sceAgcAcbAtomicGds_0900](../../../core/libs/prx/libSceAgc/Acb/src/Control.cpp) (libSceAgc) - name from its NID (`gQkqkLttcpw`) - [sceAgcDcbPrimeUtcl2](../../../core/libs/prx/libSceAgc/DcbState/src/Display.cpp) (libSceAgc) - assumed to write the packet `sceAgcAcbPrimeUtcl2` writes: PRIME_UTCL2 has the same 5 dwords on the graphics and compute rings. Not confirmed on a title. Both write a NOP of that size, as there is no TLB to prime - [sceAgcDcbQueueEndOfShaderActionGetSize](../../../core/libs/prx/libSceAgc/DcbState/src/ContextState.cpp) (libSceAgc) - the builder is inline in the SDK and not exported; the size is assumed to be that of the RELEASE_MEM packet `sceAgcCbReleaseMem` writes (32 bytes), the same as `sceAgcAcbQueueEndOfShaderActionGetSize`, as in [OpenAGC](https://github.com/OpenAGC/OpenAGC/blob/main/src/game_compat.c) and [REmu](https://github.com/xnetcat/REmu/blob/main/crates/remu-runtime/src/agc_dcb.rs) - [sceAgcDcbSetZPassPredicationEnableGetSize, sceAgcDcbSetPredicationDisableGetSize, sceAgcDcbSetBoolPredicationEnableGetSize](../../../core/libs/prx/libSceAgc/DcbState/src/Predication.cpp) (libSceAgc) - the builders are inline in the SDK and not exported; the size is assumed to be that of the SET_PREDICATION packet `sceAgcDcbSetPredication` writes (16 bytes) @@ -40,9 +40,11 @@ ### Functional - [sceAgcDebugRaiseException](../../../core/libs/prx/libSceAgc/Misc/src/Debug.cpp) (libSceAgc) - logs the exception id and returns 0, as retail hardware does nothing per [OpenAGC](https://github.com/OpenAGC/OpenAGC/blob/3c7e8072f0d7684e930454794fd5c02056946d73/src/game_compat.c); the trap (`ud2`) OpenAGC describes for development hardware is not reproduced. Neither is verified on a console. The `uint32` exception id is [KytyPS5](https://github.com/KytyPS5/KytyPS5/blob/6f0cddd4404ec5203e71fea77e75fc373299ba3f/src/libs/agc.cpp)'s claim; OpenAGC declares no arguments, so the logged id may be leftover register contents -- [sceAgcSetAmmSemaphoreMemory](../../../core/libs/prx/libSceAgc/Misc/src/Semaphore.cpp) - native registration first invokes a kernel ioctl and driver registration; host registration does not reproduce their memory-access checks or error propagation. Null, empty and address-wrapping ranges throw. The full range is cleared after successful registration; duplicate registration, alignment and label-range errors match the native wrapper. +- [sceAgcSetAmmSemaphoreMemory, sceAgcSetSemaphoreMemory, sceAgcGetSemaphoreLabel](../../../core/libs/prx/libSceAgc/Misc/src/Semaphore.cpp) (libSceAgc) - the checks, error codes and clearing are the firmware's, but two firmware steps are not reproduced. After its checks, the firmware validates the memory with libkernel `qSleeVC6lwI`: an ioctl `0xc018ac15` taking `{memory, size, 1}`. The AMM call always does this; the non-AMM call only for memory from 8 TiB up that ends below 15 TiB. The firmware then sends `{7: memory, 8: size, 0xb: 0 for AMM, 1 otherwise}` through sceAgcDriverPassInfoDownward. Neither step happens here, so a registration that the kernel or driver would refuse succeeds. A null memory with a non-zero size and a range that wraps the address space throw where the firmware would fault - [sceAgcDcbSetWorkloadsActive and sceAgcDcbSetWorkloadComplete](../../../core/libs/prx/libSceAgc/DcbState/src/Workload.cpp) (libSceAgc) - the DCB calls write the packets [KytyPS5](https://github.com/KytyPS5/KytyPS5/blob/main/src/libs/agc.cpp) writes: an 18-dword (active) or 12-dword (complete) `IT_NOP` packet with the stream id, and for active the 64-bit mask of the workload ids, for complete the workload id and the 64-bit clear mask of that id. Both return null for a null command buffer, an unregistered or out-of-range stream, a workload id above 63, and for active a duplicate id or a count of 0 or above 63. The layouts and checks come from KytyPS5 alone; the console oracle in [prosper](https://github.com/mattias800/prosper/blob/main/prosper/tests/data/console_oracle/agcbuild.golden.tsv) measures the null return only for unregistered streams, not the packet contents of a registered one. PPSA29866 calls sceAgcDcbSetWorkloadsActive while building its command buffers (sceAgcDcbSetWorkloadComplete was not observed); nothing else was observed. - [sceAgcDcbSetWorkloadStreamInactive](../../../core/libs/prx/libSceAgc/DcbState/src/Workload.cpp) and [sceAgcAcbSetWorkloadsActive, sceAgcAcbSetWorkloadComplete, sceAgcAcbSetWorkloadStreamInactive](../../../core/libs/prx/libSceAgc/Acb/src/Control.cpp) (libSceAgc) - return null without writing to the buffer for a null command buffer or a stream that is not registered (out-of-range ids included), and throw for a registered stream. The console oracle in prosper measured only that null return, with no stream registered, three argument patterns each and nothing written ([ACB](https://github.com/mattias800/prosper/blob/df074498e56c0df22d74f9c6622e4709bc1c9a19/prosper/tests/data/console_oracle/agcbuild.golden.tsv#L48-L56), [DCB](https://github.com/mattias800/prosper/blob/df074498e56c0df22d74f9c6622e4709bc1c9a19/prosper/tests/data/console_oracle/agcbuild.golden.tsv#L196-L204)); the packet a registered stream gets is not measured. KytyPS5 has no body for these four, and its `IT_NOP` layout for the DCB active and complete calls differs from the SET_UCONFIG_REG, WRITE_DATA and SET_WORKLOAD sequence [OpenAGC](https://github.com/OpenAGC/OpenAGC/blob/3c7e8072f0d7684e930454794fd5c02056946d73/src/agc_workload_packet.c) derives from the driver, so neither is taken. The `(buf, streamId)`, `(buf, streamId, workloadId)` and `(buf, streamId, workloadIds, workloadCount)` signatures are those of the DCB calls in [OpenAGC](https://github.com/OpenAGC/OpenAGC/blob/3c7e8072f0d7684e930454794fd5c02056946d73/src/game_compat.c#L2317-L2384), which says the ACB calls use the same driver builders; no ACB declaration was found. `sceAgcDcbSetWorkloadsActive` checks the stream before it reads the workload ids, as OpenAGC does: the console returned null for stream 1 with the ids at address 2 ([measured](https://github.com/mattias800/prosper/blob/df074498e56c0df22d74f9c6622e4709bc1c9a19/prosper/tests/data/console_oracle/agcbuild.golden.tsv#L203)) without faulting - [sceAgcDcbSetWorkload*](../../../core/libs/prx/libSceAgc/DcbState/src/Workload.cpp) and [sceAgcAcbSetWorkload*](../../../core/libs/prx/libSceAgc/Acb/src/Control.cpp) (libSceAgc) - the console's packets are not written: the executor has no ATOMIC_MEM or mask table. In `libSceAgc.sprx` (firmware 12.00, read from disassembly) all six write a SET_UCONFIG_REG to 0x342 (tag `0xCC000000 | stream`, or `0xCD000000 + workload * 32 + stream` for complete) and a WRITE_DATA to 0xC343, and active and complete add an ATOMIC_MEM (header `0xC0071E00`) on the stream's 64-bit mask. [Measured on a PS5](https://gist.github.com/orezraey/befe7632b827421988b47a6c04e89e1d) (firmware 13.60): WRITE_DATA dwords 5 to 8 are `0x06010000`, `0xC343`, 0 and the mask's high half (0 for inactive); the header low byte is 0x04 on the DCB SET_UCONFIG_REG and WRITE_DATA and 0x00 on the ACB ones; the table was at `0xFE003A000 + 8 * stream`; the pointer passed to `sceAgcDriverRegisterWorkloadStream` is never written - [sceAgcCreatePrimState](../../../core/libs/prx/libSceAgc/Shader/src/PrimState.cpp) (libSceAgc) - the `ge_user_vgpr_en` special is passed to the user configuration registers at whatever offset the shader header stores, as the SPRX does according to [OpenAGC](https://github.com/OpenAGC/OpenAGC/blob/3c7e8072f0d7684e930454794fd5c02056946d73/src/game_compat.c#L1181-L1252), which copies all four specials verbatim (KytyPS5 stops on any other offset). PPSA99039, PPSA99505, PPSA99515 and PPSA99997 build their headers by hand with it at 0x25c (`GE_USER_VGPR1`) instead of 0x262 (`GE_USER_VGPR_EN`), value 0, and run on the console; it is not moved to 0x262, and the driver reads neither register. `ge_cntl`, `vgt_shader_stages_en` and a used `vgt_gs_out_prim_type` must still be at their own offsets, since the driver reads those registers there, so a header that stores one elsewhere throws naming the offset, although by OpenAGC the SPRX would copy it as well - [sceAgcDcbEventWrite](../../../core/libs/prx/libSceAgc/DcbFlow/src/Sync.cpp) (libSceAgc) - non-sample-dump event types (any event other than 0x38 and 0x39) emit a 2-dword PM4 packet 0x46 without address dwords; the address argument is ignored for these events. Void Terrarium (PPSA03061, #5055) passes a non-null address buffer (0x00000002d2a10000) with event type 0x16 (cache flush). + +- This pending declaration has an unverified signature and behavior; it is a parameterless import-resolution declaration that throws through `NotImplemented_nid_no_patch`: [`sceAgcAcbInitializeDefaultHardwareState`](../../../core/libs/prx/libSceAgc/Unimplemented.cpp). diff --git a/docs/dev/debt/libSceAgcDriver.md b/docs/dev/debt/libSceAgcDriver.md index 7a8f6ad52d..36535fa9b4 100644 --- a/docs/dev/debt/libSceAgcDriver.md +++ b/docs/dev/debt/libSceAgcDriver.md @@ -23,6 +23,7 @@ - [Inactive vertex point size](../../../core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp) - `PA_CL_VS_OUT_CNTL.USE_VTX_POINT_SIZE` is accepted for lines, triangles and rectangles with `GS_EN` and `HS_EN` clear, when polygon mode is disabled or both faces are filled. Patches and absent routing or rasterization registers still throw; point lists on that routing use it (next entry). Bit 16 selects the misc export's X component as PointSize ([AMD CIK register reference](https://www.amd.com/content/dam/amd/en/documents/radeon-tech-docs/programmer-references/CIK_3D_registers_v2.pdf), PA_CL_VS_OUT_CNTL); ignoring it for non-point rasterization follows [Vulkan PointSize](https://docs.vulkan.org/refpages/latest/refpages/source/PointSize.html). This is inferred, not measured on console hardware. XashPS5 (PPSA19111 v1.3) sets `0x01210000` on a filled triangle draw. - [Vertex-path point lists](../../../core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp) (AGC driver) - throws for a register size of 0, a clamp maximum of 0 with `USE_VTX_POINT_SIZE`, unequal register width and height, a clamp minimum above its maximum, `USE_VTX_POINT_SIZE` without `VS_OUT_MISC_VEC_ENA`, and point sprite coordinates; sizes above one pixel need `largePoints`. Inferred, not measured: that `PA_SU_POINT_MINMAX` clamps the exported size and not the register size ([radeonsi](https://gitlab.freedesktop.org/mesa/mesa/-/blob/mesa-24.0.0/src/gallium/drivers/radeonsi/si_state.c#L1047-1064)), and that an exported size of 0 or less covers nothing (here: the smallest point). A point list with `GS_EN` and `HS_EN` clear draws as Vulkan points, and the vertex program [writes](../../../core/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cpp) `PointSize`: the misc export's X, clamped, under `USE_VTX_POINT_SIZE`, else the `PA_SU_POINT_SIZE` height (12.4 fixed-point half sizes). +- [MEM_SEMAPHORE](../../../core/libs/prx/libSceAgcDriver/Execution/src/Pm4.cpp) (AGC driver) - the semaphore is taken as a 64-bit word, as the 8-byte address alignment `sceAgcAcbMemSemaphore` checks suggests: a signal (operation 6) writes 1 with signal type 1 and adds 1 with signal type 0, and a wait (operation 7) polls until the word is non-zero and then subtracts 1. Signals and the decrement of a wait are atomic 64-bit operations on the guest word, so CPU threads and other queues updating it concurrently are not lost. Mailbox waits throw. The width and both signal types are inferred from the AMD PM4 field names, not measured on hardware; PPSA03396 signals with type 1 after compute dispatches. - [RELEASE_MEM of the system counter](../../../core/libs/prx/libSceAgcDriver/Execution/src/Pm4.cpp) (AGC driver) - a `RELEASE_MEM` with `DATA_SEL = 4` throws: the frequency and epoch of the console's 64-bit system counter are not known. [amdgpu's GFX10 packet header](https://github.com/torvalds/linux/blob/master/drivers/gpu/drm/amd/amdgpu/nvd.h) names the value "send 64bit sys counter value", beside 3, the GPU counter, which the driver writes as a 100 MHz count of the host's steady clock. An older KFD header names 4 a CP performance counter instead. A PS4 title through #6977 writes it at the end of each frame. - [Point and line polygon modes](../../../core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp) (AGC driver) - front and back polygon types that differ while both faces are drawn throw, as Vulkan has one `polygonMode` per draw. Points are drawn only one pixel wide (`PA_SU_POINT_SIZE = 0x00080008` inside `PA_SU_POINT_MINMAX`, the `VK_KHR_maintenance5` size of a point whose shader does not write `PointSize`); other sizes, a per-vertex size and devices without `maintenance5` throw. Lines must be one pixel wide (`PA_SU_LINE_CNTL`) and use Vulkan's default line rasterization. Depth bias with a point or line polygon mode, and such a mode on a rectangle list, throw. The fields follow the [AMD CIK register reference](https://www.amd.com/content/dam/amd/en/documents/radeon-tech-docs/programmer-references/CIK_3D_registers_v2.pdf); the console's rasterization of these points and lines is not measured. - [Native multisampled depth clears](../../../core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp) accept the recognized HTILE mask kernel's direct full-range dispatch after its GPU writes complete. Every metadata byte needs tracked driver-store coverage since notification, no intervening CPU or depth write, and HTILE words proving the current clear value of zero or one. Partial, overlapping, indirect and unproven producers remain unsupported. Existing sampled views retain the native image; guest depth publication and metadata-fill transitions remain unimplemented. Windows shared imports retain write tracking through separate aliases; private host imports that lose tracking and copied or mirrored HTILE uploads remain unsupported. Clear-word encoding and console equivalence remain unmeasured on PS5 hardware. @@ -36,15 +37,16 @@ - [GS_FAST_LAUNCH geometry](../../../core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp) (AGC driver) - `VGT_SHADER_STAGES_EN.GS_FAST_LAUNCH = 1` with a point list and `GE_CNTL` groups of one vertex and one primitive decodes as one point per subgroup, with the primitive limit taken from `GE_NGG_SUBGRP_CNTL.PRIM_AMP_FACTOR` and the GS/ES VGPR counts not required. Follows how RADV drives mesh shaders on gfx10.3 (`ac_nir_lower_ngg.c`, the workgroup index comes from the vertex id of the first thread), not measured on the console. Fast launch 2, other group sizes or input primitives and indexed fast-launch draws still throw. PPSA03671's fast-launch draws decode, but the title stops earlier in a wave64 vertex program, so they have not reached the GPU yet. - [Point geometry output](../../../core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp) (AGC driver) - a geometry program with `VGT_GS_OUT_PRIM_TYPE = 0` emits mesh points with `OutputPoints` and scalar `PrimitivePointIndicesEXT` as required by [SPV_EXT_mesh_shader](https://github.khronos.org/SPIRV-Registry/extensions/EXT/SPV_EXT_mesh_shader.html). Each packed primitive export contributes its first 9-bit vertex index and its null bit, using the same NGG primitive packing as line and triangle outputs. The subgroup reserves at least `GE_MAX_OUTPUT_PER_SUBGROUP` point slots, as the line path does; the console budget is not measured. Points use the register size or the misc-X point-size export and clamp interpreted by the vertex-point path; those register, clamp and rasterization inferences remain unmeasured on the console. Point sprites remain unsupported. Point output from a vertex workgroup still throws. Cocoon (PPSA08766) reaches this path with triangle-strip input. - [Line-strip geometry output](../../../core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp) (AGC driver) - a geometry program with `VGT_GS_OUT_PRIM_TYPE = 1` (line strip) runs as a mesh shader with `OutputLinesEXT`: each primitive export is one line whose two vertex indices are packed like the three of a triangle, as Mesa's NGG lowering exports line strips (`ac_nir_lower_ngg_gs.c`), and each input primitive gets `VGT_GS_MAX_VERT_OUT - 1` lines where a triangle strip gets `- 2` triangles. A subgroup gets at least `GE_MAX_OUTPUT_PER_SUBGROUP` line slots; whether the console does the same is unmeasured. A fast-launch subgroup takes its line count from `GE_NGG_SUBGRP_CNTL.PRIM_AMP_FACTOR`. Line output counts as lines for polygon offset decode. The lines are drawn 1 pixel wide with Vulkan default rasterization; `PA_SU_LINE_CNTL` is not read, and console line rules are not measured. -- [Alpha-to-coverage](../../../core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp) (AGC driver) - only `DB_ALPHA_TO_MASK = 0xaa01` is accepted: the enable bit with the four quad offsets at 2 and no `OFFSET_ROUND`, the undithered setting [radeonsi](https://gitlab.freedesktop.org/mesa/mesa/-/blob/bffd91947efc1932da99aca69fc540d6d4201b0c/src/gallium/drivers/radeonsi/si_state.c#L371-L381) programs. Draws are single-sampled, so the pixel shader [writes](../../../core/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cpp) `SampleMask` 0 when its MRT0 alpha is below one half or NaN and all ones otherwise, and runs with late fragment tests. That rule was measured on a Radeon 660M (gfx1035, RADV with `RADV_DEBUG=noatocdithering`, which programs the same register) for 0, -0, 0.25, the two floats below one half, one half, the two above, 0.75, 1, 2, -1, both infinities and both NaNs, and is pinned in [AlphaToCoverage.cpp](../../../core/libs/prx/libSceAgcDriver/tests/execution/AlphaToCoverage.cpp); it is not measured on gfx1013. Vulkan's `alphaToCoverageEnable` is not used: the NVIDIA driver 615.78.08 dithers single-sample coverage (an alpha of 0.25 writes half the pixels of an RTX 3050 Laptop target). Other offsets, `OFFSET_ROUND`, a sample-mask export, and an MRT0 that is missing, swizzled, packed or exported without a floating-point alpha throw. A draw without a color target on MRT0 keeps alpha-to-coverage from its MRT0 export, as [radeonsi exports MRT0 as `32_AR` for alpha-to-coverage without a color buffer](https://gitlab.freedesktop.org/mesa/mesa/-/blob/mesa-24.0.0/src/gallium/drivers/radeonsi/si_state_shaders.cpp#L2425-L2431). A draw that enables alpha-to-coverage with no MRT0 export still throws: what the console covers then is not measured. Default dithered `DB_ALPHA_TO_MASK = 0x18701` additionally uses a per-quad sample-mask table [measured on gfx1036](https://github.com/boykopovar/AnyPS5/pull/4451#issuecomment-6124848921) for eight standard samples with FP16_ABGR or 32_ABGR exports; other sample counts, custom locations, EQAA combinations and dithered UNORM16/SNORM16 exports throw. The dithered rule is not measured on gfx1013. -- [A one-sample target under a multisampled rasterizer without MSAA_ENABLE](../../../core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp) (AGC driver) - `PA_SC_AA_CONFIG.MSAA_NUM_SAMPLES` > 0 with `PA_SC_MODE_CNTL_0.MSAA_ENABLE` clear, one-sample color targets and no depth target, or a one-sample depth target with no color target or one-sample color targets, is drawn as a one-sample draw at the pixel center. Without the bit every sample is taken to be at the pixel center (see the entry for a multisampled target rasterized without MSAA_ENABLE below), and a one-sample target is taken to keep the coverage of sample 0, so a one-sample depth target is tested and written once per pixel with the depth at the pixel center. That is the rule [`VK_AMD_mixed_attachment_samples`](https://github.com/KhronosGroup/Vulkan-Docs/blob/193ebe195f47ffc5004683d1659e71c725658c4d/chapters/fragops.adoc#L2510-L2516) gives a color target with fewer samples than the rasterizer, but Vulkan reaches it only beside a depth or stencil target with the rasterizer's sample count ([VUID-VkGraphicsPipelineCreateInfo-subpass-01505](https://github.com/KhronosGroup/Vulkan-Docs/blob/193ebe195f47ffc5004683d1659e71c725658c4d/chapters/pipelines.adoc#L3033-L3041)), so here it is an analogy, and for a one-sample depth target an extension of it. [shadPS4](https://github.com/shadps4-emu/shadPS4/blob/15abf68c9b52c4b04d094f2837719fdb871021f3/src/video_core/renderer_vulkan/vk_graphics_pipeline.cpp#L127-L128) also rasterizes with the largest sample count of the bound depth and color targets ([L416-L417](https://github.com/shadps4-emu/shadPS4/blob/15abf68c9b52c4b04d094f2837719fdb871021f3/src/video_core/renderer_vulkan/vk_pipeline_cache.cpp#L416-L417), [L477-L480](https://github.com/shadps4-emu/shadPS4/blob/15abf68c9b52c4b04d094f2837719fdb871021f3/src/video_core/renderer_vulkan/vk_pipeline_cache.cpp#L477-L480)), and reads `PA_SC_AA_CONFIG` only for a `SAMPLE_COVERAGE` input ([L174-L177](https://github.com/shadps4-emu/shadPS4/blob/15abf68c9b52c4b04d094f2837719fdb871021f3/src/video_core/renderer_vulkan/vk_pipeline_cache.cpp#L174-L177)). This is not measured on hardware. An `ANCILLARY` or `SAMPLE_COVERAGE` pixel input and a sample-mask export throw in a pixel program that runs, and so does `PS_ITER_SAMPLE` with one-sample color targets and no depth target, because what the console gives them under a multisampled configuration is not known; a depth-only draw that skips its pixel program is drawn with the null program, and its `PS_ITER_SAMPLE` and pixel input bits are ignored. With a one-sample depth target, a pixel program with `PS_ITER_SAMPLE` and neither input runs once per pixel; if the console runs it once per sample, its atomics and append or consume counters count differently. Points and alpha-to-coverage throw as for a multisampled target. With a one-sample depth target, `DB_EQAA.INTERPOLATE_COMP_Z` is accepted: with every sample at the pixel center, Z interpolated to any sample position is the center's Z, which the one-sample draw tests. That rests on the same inference; neither [PAL](https://github.com/GPUOpen-Drivers/pal/blob/c5e800072a32f68b6ccc4422936d96167c6e0728/src/core/hw/gfxip/gfx9/gfx9MsaaState.cpp#L117-L118) nor [radeonsi](https://gitlab.freedesktop.org/mesa/mesa/-/blob/761d5c8a54f93b3d43608f718910ed742983dd0b/src/gallium/drivers/radeonsi/si_state.c#L3314-L3316) sets the bit, and its meaning is not documented. Line and point lists (`VGT_PRIMITIVE_TYPE` 1-3, even when a geometry program turns them into triangles), `INTERPOLATE_SRC_Z` and overrasterization (`OVERRASTERIZATION_AMOUNT`, `ENABLE_POSTZ_OVERRASTERIZATION`) still throw. METAL GEAR SOLID: Peace Walker (PPSA18216) draws a one-sample color target under a 4x configuration this way; the first draw of its opening movie binds a one-sample depth target and no color target under the same configuration. Gran Turismo 7 (PPSA01317) draws its main pass with a one-sample depth target, `DB_EQAA` 0x00050000 and a 4x configuration. +- [Alpha-to-coverage](../../../core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp) (AGC driver) - `DB_ALPHA_TO_MASK = 0xaa01` is accepted: the enable bit with the four quad offsets at 2 and no `OFFSET_ROUND`, the undithered setting [radeonsi](https://gitlab.freedesktop.org/mesa/mesa/-/blob/bffd91947efc1932da99aca69fc540d6d4201b0c/src/gallium/drivers/radeonsi/si_state.c#L371-L381) programs. Draws are single-sampled, so the pixel shader [writes](../../../core/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cpp) `SampleMask` 0 when its MRT0 alpha is below one half or NaN and all ones otherwise, and runs with late fragment tests. That rule was measured on a Radeon 660M (gfx1035, RADV with `RADV_DEBUG=noatocdithering`, which programs the same register) for 0, -0, 0.25, the two floats below one half, one half, the two above, 0.75, 1, 2, -1, both infinities and both NaNs, and is pinned in [AlphaToCoverage.cpp](../../../core/libs/prx/libSceAgcDriver/tests/execution/AlphaToCoverage.cpp); it is not measured on gfx1013. Vulkan's `alphaToCoverageEnable` is not used: the NVIDIA driver 615.78.08 dithers single-sample coverage (an alpha of 0.25 writes half the pixels of an RTX 3050 Laptop target). Other offsets, `OFFSET_ROUND`, a sample-mask export, and an MRT0 that is missing, swizzled, packed or exported without a floating-point alpha throw. A draw without a color target on MRT0 keeps alpha-to-coverage from its MRT0 export, as [radeonsi exports MRT0 as `32_AR` for alpha-to-coverage without a color buffer](https://gitlab.freedesktop.org/mesa/mesa/-/blob/mesa-24.0.0/src/gallium/drivers/radeonsi/si_state_shaders.cpp#L2425-L2431). A draw that enables alpha-to-coverage with no MRT0 export still throws: what the console covers then is not measured. Default dithered `DB_ALPHA_TO_MASK = 0x18701` additionally uses a per-quad sample-mask table [measured on gfx1036](https://github.com/boykopovar/AnyPS5/pull/4451#issuecomment-6124848921) for eight standard samples with FP16_ABGR or 32_ABGR exports; other sample counts, custom locations, EQAA combinations and dithered UNORM16/SNORM16 exports throw. The dithered rule is not measured on gfx1013. +- [A one-sample target under a multisampled rasterizer without MSAA_ENABLE](../../../core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp) (AGC driver) - `PA_SC_AA_CONFIG.MSAA_NUM_SAMPLES` > 0 with `PA_SC_MODE_CNTL_0.MSAA_ENABLE` clear, one-sample color targets and no depth target, or a one-sample depth target with no color target or one-sample color targets, is drawn as a one-sample draw at the pixel center. Without the bit every sample is taken to be at the pixel center (see the entry for a multisampled target rasterized without MSAA_ENABLE below), and a one-sample target is taken to keep the coverage of sample 0, so a one-sample depth target is tested and written once per pixel with the depth at the pixel center. That is the rule [`VK_AMD_mixed_attachment_samples`](https://github.com/KhronosGroup/Vulkan-Docs/blob/193ebe195f47ffc5004683d1659e71c725658c4d/chapters/fragops.adoc#L2510-L2516) gives a color target with fewer samples than the rasterizer, but Vulkan reaches it only beside a depth or stencil target with the rasterizer's sample count ([VUID-VkGraphicsPipelineCreateInfo-subpass-01505](https://github.com/KhronosGroup/Vulkan-Docs/blob/193ebe195f47ffc5004683d1659e71c725658c4d/chapters/pipelines.adoc#L3033-L3041)), so here it is an analogy, and for a one-sample depth target an extension of it. [shadPS4](https://github.com/shadps4-emu/shadPS4/blob/15abf68c9b52c4b04d094f2837719fdb871021f3/src/video_core/renderer_vulkan/vk_graphics_pipeline.cpp#L127-L128) also rasterizes with the largest sample count of the bound depth and color targets ([L416-L417](https://github.com/shadps4-emu/shadPS4/blob/15abf68c9b52c4b04d094f2837719fdb871021f3/src/video_core/renderer_vulkan/vk_pipeline_cache.cpp#L416-L417), [L477-L480](https://github.com/shadps4-emu/shadPS4/blob/15abf68c9b52c4b04d094f2837719fdb871021f3/src/video_core/renderer_vulkan/vk_pipeline_cache.cpp#L477-L480)), and reads `PA_SC_AA_CONFIG` only for a `SAMPLE_COVERAGE` input ([L174-L177](https://github.com/shadps4-emu/shadPS4/blob/15abf68c9b52c4b04d094f2837719fdb871021f3/src/video_core/renderer_vulkan/vk_pipeline_cache.cpp#L174-L177)). This is not measured on hardware. An `ANCILLARY` or `SAMPLE_COVERAGE` pixel input and a sample-mask export throw in a pixel program that runs (with a one-sample depth target, `ANCILLARY` only under `PS_ITER_SAMPLE`), because what the console gives them under a multisampled configuration is not known; a depth-only draw that skips its pixel program is drawn with the null program, and its `PS_ITER_SAMPLE` and pixel input bits are ignored. A pixel program with `PS_ITER_SAMPLE` and neither input runs once per pixel; if the console runs it once per sample, its atomics and append or consume counters count differently. With a one-sample depth target and without `PS_ITER_SAMPLE`, the [packed ancillary word](../../../core/shader/recompiler/Optimization/src/ConstantFolder.cpp) gives a program that reads `ANCILLARY` sample index 0, inferred for a pixel the guest does not iterate, and that is the sample the one-sample draw keeps. Points and alpha-to-coverage throw as for a multisampled target. With a one-sample depth target, `DB_EQAA.INTERPOLATE_COMP_Z` is accepted: with every sample at the pixel center, Z interpolated to any sample position is the center's Z, which the one-sample draw tests. That rests on the same inference; neither [PAL](https://github.com/GPUOpen-Drivers/pal/blob/c5e800072a32f68b6ccc4422936d96167c6e0728/src/core/hw/gfxip/gfx9/gfx9MsaaState.cpp#L117-L118) nor [radeonsi](https://gitlab.freedesktop.org/mesa/mesa/-/blob/761d5c8a54f93b3d43608f718910ed742983dd0b/src/gallium/drivers/radeonsi/si_state.c#L3314-L3316) sets the bit, and its meaning is not documented. Line and point lists (`VGT_PRIMITIVE_TYPE` 1-3, even when a geometry program turns them into triangles), `INTERPOLATE_SRC_Z` and overrasterization (`OVERRASTERIZATION_AMOUNT`, `ENABLE_POSTZ_OVERRASTERIZATION`) still throw. METAL GEAR SOLID: Peace Walker (PPSA18216) draws a one-sample color target under a 4x configuration this way; the first draw of its opening movie binds a one-sample depth target and no color target under the same configuration. Gran Turismo 7 (PPSA01317) draws its main pass with a one-sample depth target, `DB_EQAA` 0x00050000 and a 4x configuration. +- [One-fragment multisampled color target](../../../core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp) (AGC driver) - a color target with `CB_COLOR_ATTRIB` 4 samples and 1 fragment is decoded as one sample that keeps its coverage sample count and draws sample 0's coverage. Measured only on radeonsi GL (Radeon 610M and 680M); console behaviour is not measured. It requires static `DB_EQAA` anchors at the depth target's sample count and CMASK code 0 or 0xc-0xf. More than one but fewer fragments than samples, more fragments than samples, other CMASK codes and any guest read of the FMASK of a target the host drew still throw. - [MSAA_ENABLE on a single-sample target](../../../core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp) (AGC driver) - `PA_SC_MODE_CNTL_0.MSAA_ENABLE` with `PA_SC_AA_CONFIG.MSAA_NUM_SAMPLES = 0` is drawn like the same state without the bit when sample 0 of the four `PA_SC_AA_SAMPLE_LOCS_PIXEL_*_0` registers is at the pixel center and the draw rasterizes filled triangles or rectangles (`VGT_PRIMITIVE_TYPE` 7 or 17 as well as the triangle types) with alpha-to-coverage and conservative rasterization off. [RADV](https://gitlab.freedesktop.org/mesa/mesa/-/blob/745709a000482a4a24c91faef5c5a40bd653a863/src/amd/vulkan/radv_cmd_buffer.c#L13606-13609) programs this state for one-sample custom sample locations on GFX10 and later, where the bit makes the rasterizer use the programmed location of sample 0 ([061bc615](https://gitlab.freedesktop.org/mesa/mesa/-/commit/061bc6151a11597f720535f00e79a5752e24aff6)); that the bit changes nothing else for such a draw is inferred from that and is not measured on hardware. EA Sports UFC 5 (PPSA03541) reaches a rect-list draw in this state. Other locations of sample 0, absent location registers, points, lines, alpha-to-coverage and conservative rasterization throw - [A multisampled target rasterized without MSAA_ENABLE](../../../core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp) (AGC driver) - not measured on hardware: `PA_SC_AA_CONFIG.MSAA_NUM_SAMPLES` > 0 with `PA_SC_MODE_CNTL_0.MSAA_ENABLE` clear puts every color, depth and stencil sample at the pixel center, as in GL without multisampling ([ARB_multisample](https://github.com/KhronosGroup/OpenGL-Registry/blob/6af574a14089/extensions/ARB/ARB_multisample.txt#L185), [RADV 061bc615](https://gitlab.freedesktop.org/mesa/mesa/-/commit/061bc6151a11)). Lines cover whole pixels by the host's line rule, not the console's aliased one ([D3D11.3](https://github.com/microsoft/DirectX-Specs/blob/f37b0d6033a1/d3d/archive/D3D11_3_FunctionalSpec.htm#L13705)). Depth sample tests and occlusion counts are unknown. Rasterized points, alpha-to-coverage, and Z interpolation, overrasterization or a sample-mask export with depth throw. PPSA06888 draws a 2x target, PPSA18216 a 4x depth and color pair and 4x lines. - [sceAgcDriverGetEqContextId](../../../core/libs/prx/libSceAgcDriver/Eq/src/Query.cpp) (libSceAgcDriver) - returns `ident`: an end-of-pipe interrupt is delivered only to the registration whose id equals the raising queue, so `ident` is that context's id (titles register 0 or 1 for graphics and `0x20 + 8 * pipe + queue` for compute). The console's value is not known; KytyPS5 returns the event's `data`, which it fills with the interrupt's context id, whereas `data` here is the EV_CLEAR trigger count. Other event filters throw - [User data registers](../../../core/libs/prx/libSceAgcDriver/Execution/src/Driver/Diagnostics.cpp) (AGC driver) - a `SPI_SHADER_USER_DATA_*` or `COMPUTE_USER_DATA_*` register the title never wrote reads as 0 when a stage's RSRC2 count covers it, as Kyty (zeroed `UserSgprInfo`) and SharpEmu (missing registers read 0) do, and as AMD PAL and Mesa initialize them; the console's value is not confirmed - [SET_PREDICATION](../../../core/libs/prx/libSceAgcDriver/Execution/src/Pm4.cpp) (AGC driver) - boolean predication follows the AMD PM4 encoding [Mesa](https://gitlab.freedesktop.org/mesa/mesa/-/blob/main/src/amd/common/sid.h) uses: operation 3 reads 64 bits and 4 reads 32 bits, and bit 8 runs predicated packets when the value is non-zero (clear: when it is zero); the bit 12 hint is ignored. The address must have its low 4 bits clear, as `sceAgcDcbSetPredication` writes it. Query predication (Z-pass, primitive count), a predicated COND_EXEC, predicated command buffer chains and flips in predicated command buffers throw - [Barycentrics without mesh shaders](../../../core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/Draw.cpp) (AGC driver) - a pixel shader that reads barycentrics on the mesh path is emulated only on a device with `VK_EXT_mesh_shader`; a device with neither it nor `VK_KHR_fragment_shader_barycentric` (reported on Linux RADV with a GCN 5 GPU) throws that the device has neither the barycentric extension nor mesh shaders, because a geometry stage cannot follow a mesh stage and no other route is implemented. `VK_AMD_shader_explicit_vertex_parameter`, which a GCN 5 device on RADV exposes, is an unexplored option. -- [NGG vertex programs on the mesh path](../../../core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp) (AGC driver) - an unregistered vertex program with `PRIMGEN_EN` that uses LDS or `s_barrier` runs on the geometry (mesh) path. Each input primitive is assumed to yield at most one output primitive, the subgroup is bounded by `GE_MAX_OUTPUT_PER_SUBGROUP` as a vertex budget, and the GS/ES VGPR count checks are skipped, because the merged inputs keep their positions. Taken from the code Mesa's ACO emits for NGG culling and from PPSA59322 and PPSA99010 draws, not measured on hardware. Registered programs keep the vertex path +- [NGG vertex programs on the mesh path](../../../core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp) (AGC driver) - a vertex program with `PRIMGEN_EN` that uses LDS or `s_barrier` runs on the geometry (mesh) path. Each input primitive is assumed to yield at most one output primitive, the subgroup is bounded by `GE_MAX_OUTPUT_PER_SUBGROUP` as a vertex budget, and the GS/ES VGPR count checks are skipped, because the merged inputs keep their positions. Taken from the code Mesa's ACO emits for NGG culling and from PPSA59322 and PPSA99010 draws, not measured on hardware. - [VGT_SHADER_STAGES_EN 0x02002008](../../../core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp) (AGC driver) - this one value, `ES_EN` = 1 without `HS_EN` on a non-patch draw, is drawn as the vertex path. AMD's CIK register reference calls `ES_EN` = 1 `ES_STAGE_DS` (the ES stage runs the tessellation evaluation shader), which does not explain it on a non-patch draw. Seen only on PPSA29413 and not verified on the console; other vertex-path values with `ES_EN` set still throw. - [Strided buffer overhang](../../../core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp) (libSceAgcDriver) - a strided buffer descriptor that ends less than one stride past the end of the readable registered allocation holding its base is bound only up to that allocation's end, even when another readable allocation starts right there. This assumes the bytes past the end are the rounded-up `num_records` of the last record (RADV rounds it up), which no fetch inside the buffer reads; a guest that really keeps a vertex split across two adjacent allocations would read zeros for the cut tail. The same holds for a read-only strided buffer that starts below the render target and runs less than one stride into it: it is bound only up to the target, as a vertex fetch whose attribute descriptor starts at the attribute's offset reaches that far past the attribute's last bytes. Taken from the RetroArch Dolphin core case in #2912, not measured on hardware. - [Buffer descriptor CACHE_SWIZZLE hint](../../../core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp) (libSceAgcDriver) - bit 30 of word 1 in a buffer resource descriptor (`words[1] & 0x40000000u`) is normalized to zero when binding guest buffers in `ShaderResources::addGuestBuffer`. On GFX10 (gfx1013), bit 30 of V# word 1 is `CACHE_SWIZZLE` (Mesa's `gfx10-rsrc.json`: STRIDE [29:16], CACHE_SWIZZLE [30], SWIZZLE_ENABLE [31]), a cache-bank hint with no effect on the address or data, which is dropped here. @@ -68,10 +70,13 @@ ### Functional - [Written staging window on discrete GPUs](../../../core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp) (libSceAgcDriver) - a written buffer element of a dispatch that is not swept is staged device-local up to 8 MiB on a discrete GPU (`Context::discreteGpu`) and up to 2 MiB otherwise; `APS5_WRITTEN_SHADOW_MAX_KIB` replaces either bound. The 8 MiB figure comes from one title on one NVIDIA Windows system (PPSA09911: a 2.5 and a 3.3 MiB skin-cache output written in place cost 55 ms of GPU time per frame, 1.8 ms of extra staging copies once staged); the point where the two copies of a larger element cost more than its in-place stores, and whether AMD or Linux drivers move it, has not been measured. The window is per element and per use, so a kernel that streams once through an element inside the window pays the copy-in and the copy-back on every dispatch. +- [Frame capture](../../../core/libs/prx/libSceAgcDriver/Execution/src/FrameRecorder.cpp) (libSceAgcDriver) records guest memory on Windows only (a VirtualQuery walk of the registry allocations and the Windows write tracker); elsewhere a capture throws when it starts. Ranges are captured by allocation and protection, not by mapping: section views, aliases of one direct memory block and file views are snapshotted and replayed as separate private memory, so a store through one alias is not seen through another, and CPU stores to them during the captured frames are not recorded (only write-watched private memory is). Pages of the title's image are recorded during the frames only when a shader header or an array it points to lies in them and their content changed. A capture starts only at a frame boundary where the driver goes idle within 10 s, and gives up after five boundaries that do not. +- [Frame replay](../../../core/libs/prx/libSceAgcDriver/tools/FrameReplay.cpp) (libSceAgcDriver) orders the calls of different captured threads only by their start (a call starts once every earlier call has started), so the driver sees a schedule the title could have produced, not necessarily the one it did. CPU stores a title made from threads that never call the driver are known only by the next recorded call, so the replay applies them early or late when a GPU wait needs them and derives a value from the wait's reference when no recorded store satisfies it (`of_them_synthesized`). Pacing compares the number of submissions in flight, not what they hold. Prologue registrations of shaders whose memory the title reused after registering them fail and are counted (`stale_registrations`). The title's image range must be free in the replaying process, so the tool is linked at a high image base on Windows. - [Workgroup count split](../../../core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp) (libSceAgcDriver) - a direct dispatch over `maxComputeWorkGroupCount` runs as `vkCmdDispatch` chunks, each bound to a pipeline variant whose workgroup offset specialization constants add the chunk's base to `WorkgroupId` and `GlobalInvocationId`. A variant is built when its chunk base is first recorded, not when the dispatch is prepared. A split of a shader with device-memory LDS throws: its slot base reads the chunk's `NumWorkgroups`, not the dispatch's. Chunks are ordered by a compute barrier, which the console does not have between workgroups of one dispatch. +- [Experimental submission progress](../../../core/libs/prx/libSceAgcDriver/Execution/include/Driver/Queues/SubmissionProgress.hpp) (AGC driver) - the opt-in two-millisecond pacing budget is not derived from console hardware. Cross-title performance remains unvalidated; host scheduling can overshoot the timeout and pacing can reduce CPU/GPU overlap. - [GPU allocation recovery](../../../core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp) (libSceAgcDriver) - recovery may free too little and still throw: caller-held sampled images, pending storage results and recorder references stay alive. Budgets remain floors rather than hard device limits. Image and buffer allocation failures trim idle buffers, then sampled and clean storage cache entries. A held cache lock is reused only for a new image's allocation after lookup iterators are no longer used; hit refreshes and flushes skip that cache. Other cache, resource and device locks are tried without waiting. - [Scan-resistant sampled texture admission](../../../core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp) (libSceAgcDriver) - while the sampled texture cache is over its budget but within a quarter over it, the first use of a texture it does not hold in a present is refused (used for its draw, then released, never cached) when its least recently used entry was used in the current or previous present, and a later use in the same present is refused when that entry was used in the current present. So a working set larger than the budget keeps a stable subset: a texture used more than once in a present still evicts an entry not yet used in that present, so a working set with interleaved multi-use textures rotates as on main, and the textures that arrive after the cache fills are created again at each use for as long as the least recently used entry is from the current present, including in the transition present after a scene change. Past a quarter over the budget the entries are evicted as before, so after the budget falls the cache can sit at up to 125% of the reduced budget until entries stop being used. Entries unused for two presents are evicted as before. A later use can evict an entry from the previous present and then be refused when the next least recently used entry is from the current present, so that entry is created again in the next present. The "current or previous present" window is an unmeasured threshold: it was chosen, not derived. Measured once, as one A-B-A series on one title (loading phase about 33000 textures per frame against a 5.4 GiB budget) on one RTX 4090, 3600 s each: 932 / 968 / 941 frames, evictions after frame 620 2.72M -> 1.17M, texture creations unchanged (about 5.8M). Nothing is measured on other titles or GPUs, and a title whose per-present working set only slightly exceeds the budget now re-creates the excess textures at every use. -- [Texture cache budgets](../../../core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp) (libSceAgcDriver) - the storage image cache's budget is what the reported heap budget leaves after the heap's use other than the cached storage images (at least the sampled textures alive) and an eighth of the budget, capped at a quarter of the budget and at `TextureCacheBudget` (a quarter of the heap's size, at least 2 GiB), never below 512 MiB; the sampled cache's budget reserves `TextureCacheBudget` for the storage cache and never falls below 2 GiB. The storage budget keeps no room for the part of that 2 GiB floor the sampled textures alive do not use: it gives room back as they grow, at its next read. As the heap's other use grows the sampled budget reaches its floor first, then the storage budget falls. Each cache trims only at its next insert, a storage image is allocated before its insert reads the budget, and both budgets read the heap at most once a second, so a burst between two reads, sampled textures included, can still exceed the reported budget. The eighth is the sampled formula's value reused and the 512 MiB floor and the caps the earlier ones; none was measured beyond PPSA11386 on one 10 GB GPU and PPSA16064 on one 8 GB GPU, and a title whose storage working set exceeds the shrunk budget re-creates storage images at each use. +- [Texture cache budgets](../../../core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp) (libSceAgcDriver) - the storage image cache gets what the reported heap budget leaves after the heap's other use, the live sampled textures and an eighth of the budget, capped at half of the budget and of the heap, at least 512 MiB; the sampled cache reserves the larger of `TextureCacheBudget` and the storage images held, and keeps at least 2 GiB; above the floors neither budget plus the other cache's bytes exceeds the budget less its eighth. Both read the heap at most once a second and trim at their next insert, so a burst can exceed the budget. The storage budget gives back the part of the sampled floor not in use. The eighth and the floors were checked only on PPSA11386 (10 GB GPU) and PPSA16064 (8 GB GPU); the halves come from PPSA25636 on one 8 GB GPU (a 3.5-4 GiB storage working set). A title whose storage working set exceeds its budget re-creates storage images at each use. - [Polygon primitive type](../../../core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp) (AGC driver) - `VGT_PRIMITIVE_TYPE` 21 (`PrimitiveType::Polygon`) decodes to `VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN` and is treated as inactive for vertex point size, like triangle fans and strips. PPSA01317 (Gran Turismo 7) submits polygon draws; whether the console performs polygon-mode decomposition or custom vertex routing is unverified. - [Native multisampled DCC fast clears](../../../core/libs/prx/libSceAgcDriver/Graphics/src/MultisampleTarget.cpp) import uniform 0000/0001/1110/1111 keys into a single-layer, level-zero SW_64KB_R_X color image with 2, 4 or 8 fragments. Color and DCC/CMASK ranges must stay write-watched and unchanged; expanded or mixed initial DCC, register CMASK clears, compressed payloads, color transforms, lossy modes, other metadata changes, guest publication and writes over a live owner throw. A whole-range DCC fill with one of those keys, landed or still recorded, is taken as its fast clear; a partial fill, or a CMASK changed without a whole fill, throws. Driver-mediated reads of the color and metadata stay guarded. The metadata extent follows [AMD Addrlib](https://github.com/GPUOpen-Drivers/pal/blob/c5e800072a32f68b6ccc4422936d96167c6e0728/src/core/imported/addrlib/src/gfx10/gfx10addrlib.cpp#L347-L454) with the [PS5 Mesa model](https://github.com/mihawk-99/PS5_Mesa/blob/0b2d6d1a61d9bbf89cf8beb88a696144f67c61f8/src/amd/vulkan/winsys/ps5/radv_ps5_winsys.c): GFX10.1, 16 pipes, 256-byte interleave, two maximum compressed fragments. Console geometry, concurrent guest mutation and transitions outside this subset are unverified. @@ -83,10 +88,12 @@ - [Stencil reference export replay](../../../core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp) on devices without `VK_EXT_shader_stencil_export` replays the low eight bits of the values proved from constant, select and phi exports, with an export proved on every kept path. An export the prover cannot bound replays all 256 eight-bit values, one pass per value per triangle (logged once per process). It records instances, then their triangles, then the reference classes of each triangle. One pipeline filters fragments at shader termination against a dynamic push value before late stencil and depth tests. The [Vulkan stencil-reference replacement rule](https://docs.vulkan.org/refpages/latest/refpages/source/FragStencilRefEXT.html) discards bits above the attachment's stencil width; supported stencil attachments have eight bits. Numeric subgroup observations throw except bounded own-lane mask plumbing and group control with kept-lane and derivative-helper coverage proved. WQM inputs must exclude helper contributions (a ballot of the active lanes, as in an EXEC copy, covers whole quads and is accepted), since [non-quad subgroup operations may treat helpers as inactive](https://docs.vulkan.org/glslext/latest/glslext/khr/GL_KHR_shader_subgroup.html). Readonly guarded SRT reads may report internal host faults, and read-only BDA reads may report faults; these draws wait and check faults before publishing results, and loops throw. Indirect draws, restart, strips, fans, tessellation, mesh, multisampling, early tests, ordered pixel execution, guest shader memory writes, other fault-producing memory access, clocks, primitive ID inputs, attachment feedback and pipeline face/channel splits throw. An exhausted stage push range throws. Equivalence to console ordering has not been measured on console hardware. - [32-bit depth-bits reads of guest memory](../../../core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp) (libSceAgcDriver): a sampled 32_UINT or 32_SINT T# with a depth swizzle mode that no live depth surface matches reads the guest bytes through an R32_SFLOAT image, as the recompiler's depth-bits read expects, and the shader casts the fetched value back to the stored dword. On RX 6700 XT (gfx1031, AMD Windows driver 32.0.21045.5002) the read keeps float denormals, the sign bit alone and quiet and signaling NaN payloads bit for bit. A host that canonicalizes NaNs or flushes denormals on a 32-bit float fetch would change those dwords, and other drivers are not measured. A live depth surface whose extent or base level differs from the T# is not used; the read takes the guest bytes, as the 32-bit float and 16-bit views already do. -- [Reused depth storage](../../../core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp): a storage view with another extent or a format that cannot hold the cached depth plane uses guest memory. Depth rendering is not written back to those guest bytes; this admission rule does not prove allocation reuse or preserve a depth writer through an incompatible view. A 2D storage view, or a one-layer 2D array view of layer 0, of level 0 with the plane's extent and format (32-bit float or the 32-bit depth-bits layout for the depth plane, 16-bit unorm for D16, 8-bit uint for the stencil plane) is a storage image that is [filled](../../../core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp) from the depth image before every use and, when the shader writes it, copied back after the work: a whole-plane copy through a staging buffer each way. A dispatch of the 15-word 8x8x1 image clear kernel ([`MatchStorageImageClear`](../../../core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp): one colour read from the V# in `user[8..11]` stored to every texel of the T# in `user[0..7]`) that covers the whole plane and stores 0 or 1 through a 16-bit unorm view, 0 or 0xffff through a 16-bit uint view, or 0 or a normal float in (0, 1] through a 32-bit float or integer view leaves out the copy-in and replaces the copy-out by a clear of the depth image to that value; the dispatch still writes the storage image. The colour is read again when the copy-out is replaced, and a changed value keeps the copy-out. Indirect dispatches, partial last groups, dispatches that queue on the draw pipeline, other values and formats, a plane the kernel covers only partly and stencil planes keep both copies. A written view is also left pending like any other storage image, so the plane's content, drawn depth included, reaches guest memory on the next flush of that range. A 2D array view of more than one slice of the depth plane, or of a later slice, is a storage image over the slices from 0 to its last (`LayeredDepthStorage`); only the slices from its base slice to its last are copied: a slice that has a depth surface at the plane's address plus a whole depth slice per layer (`DepthSliceBytes`, as for the depth plane copies below) is copied from that surface before every use and back to it after the work, every bound slice each time, through a staging buffer; the other slices hold guest memory like any storage image. These images are kept per address, extent, slice count, base slice, format and tile mode until the device goes away, outside the storage cache's budget. Such a view needs a layer pitch equal to that slice size, and another view of the same memory (the 2D view of its first slice, a view from another base slice, or a sampled copy) is a separate image, copied in before and out after each use, so views that are written before a flush and share a slice do not merge: the later copy-out wins, so their results are not merged. A matching view still throws when it is a cube, a 2D array of more than one layer of the stencil plane or from a later slice, or another level, when the surface is multisampled, when it names the depth plane of a 16-bit depth with stencil surface the device stores in a wider format, and when it was written after another view of the same plane (another format, tile mode, address or dimension) replaced it, as two such views bound in one dispatch are. Stored depth values outside 0..1 are not clamped, and Vulkan leaves that copy undefined without `VK_EXT_depth_range_unrestricted`. `agc_driver_depth_storage_dispatch` pins these paths; none is measured on the console. +- [Reused depth storage](../../../core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp): a storage view with another extent or a format that cannot hold the cached depth plane uses guest memory. Depth rendering is not written back to those guest bytes; this admission rule does not prove allocation reuse or preserve a depth writer through an incompatible view. A 2D storage view, or a one-layer 2D array view of layer 0, of level 0 with the plane's extent and format (32-bit float or the 32-bit depth-bits layout for the depth plane, 16-bit unorm for D16, 8-bit uint for the stencil plane) is a storage image that is [filled](../../../core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp) from the depth image before every use and, when the shader writes it, copied back after the work: a whole-plane copy through a staging buffer each way. A dispatch of the 15-word 8x8x1 image clear kernel ([`MatchStorageImageClear`](../../../core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp): one colour read from the V# in `user[8..11]` stored to every texel of the T# in `user[0..7]`) that covers the whole plane and stores 0 or 1 through a 16-bit unorm view, 0 or 0xffff through a 16-bit uint view, or 0 or a normal float in (0, 1] through a 32-bit float or integer view leaves out the copy-in and replaces the copy-out by a clear of the depth image to that value; the dispatch still writes the storage image. The colour is read again when the copy-out is replaced, and a changed value keeps the copy-out. Indirect dispatches, partial last groups, dispatches that queue on the draw pipeline, other values and formats, a plane the kernel covers only partly and stencil planes keep both copies. A written view is also left pending like any other storage image, so the plane's content, drawn depth included, reaches guest memory on the next flush of that range. A 2D array view of more than one slice of the depth plane, or of a later slice, is a storage image over the slices from 0 to its last (`LayeredDepthStorage`); only the slices from its base slice to its last are copied: a slice that has a depth surface at the plane's address plus a whole depth slice per layer (`DepthSliceBytes`, as for the depth plane copies below) is copied from that surface before every use and back to it after the work, every bound slice each time, through a staging buffer; the other slices hold guest memory like any storage image. These images are kept per address, extent, slice count, base slice, format and tile mode until the device goes away, outside the storage cache's budget. Such a view needs a layer pitch equal to that slice size, and another view of the same memory (the 2D view of its first slice, a view from another base slice, or a sampled copy) is a separate image, copied in before and out after each use, so views that are written before a flush and share a slice do not merge: the later copy-out wins, so their results are not merged. A matching view still throws when it is a cube or a 2D array of more than one layer of the stencil plane or from a later slice, when the surface is multisampled, when it names the depth plane of a 16-bit depth with stencil surface the device stores in a wider format, and when it was written after another view of the same plane (another format, tile mode, address or dimension) replaced it, as two such views bound in one dispatch are. Stored depth values outside 0..1 are not clamped, and Vulkan leaves that copy undefined without `VK_EXT_depth_range_unrestricted`. `agc_driver_depth_storage_dispatch` pins these paths; none is measured on the console. - [Collapsed color array views](../../../core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp) preserve the selected slice in the XOR address equation when the view is represented as a shifted 2D surface. Native Vulkan coverage checks R_X draws, array reads, publication and re-upload for slices 1-3; Lost Soul Aside PPSA26358 validates the RGBA16F cube mip chain on Windows/NVIDIA. Other devices and console comparisons remain unverified. Full-array views retain their layer addressing. -- A [depth surface](../../../core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp) (libSceAgcDriver) is retired when a color target or a written storage image it does not serve overlaps it, or when it is sampled through a view it cannot serve and guest memory over it was written since it was last bound; lookups then skip it, so sampling reads the memory like any texture. Bound again, it keeps its own depth instead of the bytes written over it, except for the depth planes the next entry keeps in guest memory. Without write watching, a view it cannot serve still throws. A retired surface also gives up its depth and stencil planes, so another surface sharing them no longer copies from it. +- A [depth surface](../../../core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp) (libSceAgcDriver) is retired when a color target overlaps it, an unserved storage view writes a whole plane, or it is sampled through a view it cannot serve and guest memory over it was written since it was last bound; lookups then skip it, so sampling reads the memory like any texture. Bound again, it keeps its own depth instead of the bytes written over it, except for the depth planes the next entry keeps in guest memory. Without write watching, a view it cannot serve still throws. A retired surface also gives up its depth and stencil planes, so another surface sharing them no longer copies from it. - [Depth planes in guest memory](../../../core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp) (libSceAgcDriver): only single-sample `D32_SFLOAT` and `D32_SFLOAT_S8_UINT` planes with the 64 KiB Z swizzle (`SW_MODE` 24) reach guest memory; D16, other swizzles, stencil and multisampled planes keep depth in the host image only. A plane drawn since its last store is stored, uncompressed, in 64 KiB units (128x128 tiles): a buffer upload, a `GuestBuffers` V#, an image lookup or a pending-store flush stores the units it overlaps, a color target or written storage image over the plane all of them. CPU reads and reads through address-based heaps are not caught. Guest writes are ordered when collected (bind, flush, collect epoch), not when made: a collected write reloads the units it touched unless an HTILE clear is pending. A store outside a recorder waits for the GPU. The console keeps HTILE-compressed tiles compressed in memory until a decompress (Mesa `ac_descriptors.h`). +- [Depth surfaces over compared shared memory](../../../core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp) (libSceAgcDriver): a depth surface over Windows direct memory that is compared once it is shared sees CPU writes made after the sharing no more than one over unwatched memory does. Its generations come from refused collects (0) or `TrackerGeneration()`, and nothing stamps compared memory after the one report at the sharing. +- [Unmapped depth planes](../../../core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp) (libSceAgcDriver): a drawn depth plane whose guest memory the title unmapped before it was stored is not stored; its pending depth is dropped with a one-time warning. What the console does with such a store is not known. Readable planes without write permission retain the unsupported-store failure and their pending results. - [Depth plane copies](../../../core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp) (libSceAgcDriver): a depth plane sampled as a 2D array or a cube, or a D16 plane read as `R16_UINT`, is copied into an image of its own (`DepthPlaneCopy`), kept per base address, extent, format and slice count; a slice comes from the live depth surface when one is bound at the slice's address and from guest memory (detiled) otherwise. A draw or dispatch that binds the plane copies again only the slices that changed and records nothing when none did: a surface slice when its image or write serial differs from the one copied (the serial is bumped when the surface is created, when a draw binds it as its depth target, when a pending fast clear is applied and when a written storage view of the plane is copied back), a guest-memory slice when its bytes were written since they were last read (`GuestMemory::UnchangedSince`), a snapshot write overlaps them, or its address, size or tile mode changed. The surface of the latest depth draw is copied at every use until another depth target is bound, since that draw may still be recorded after the copy. A slice's address is the base plus a whole 64 KiB-block depth slice per layer (`DepthSliceBytes`), and the cube face order is the array order; both are inferred from the titles' use, not measured on hardware. A 2D-array T# whose `BASE_ARRAY` is past slice 0 reads slices `BASE_ARRAY` to `LAST_ARRAY` of the copy, which holds slices 0 to `LAST_ARRAY`, and a 2D binding of it reads slice `BASE_ARRAY`; that follows the colour 2D-array semantics and the titles that hit it (PPSA01556, PPSA03912), and was not measured on hardware for depth surfaces. Planes with mips, a cube or a 2D T# with a base slice other than 0, a `BASE_ARRAY` past `LAST_ARRAY`, a cube with other than 1 or 6 slices, and other views of the planes still throw, and so do these copies of a 16-bit plane with stencil that the device stores as D24 or D32 (16-bit depth with stencil, below). - [Repacked format-43 copies](../../../core/libs/prx/libSceAgcDriver/Graphics/src/Texture.cpp) (libSceAgcDriver): a sampled texture of format 43 (10-bit R, 11-bit G and B unsigned floats from bit 0) is uploaded into a `B10G11R11_UFLOAT` image with its R and B fields swapped (`RepackTextureWord`) and viewed with R and B swapped back. A texture whose results a storage image still holds is copied from it again, whole, whenever the image changed (`RefreshDepthCopy`); a 3D one is flushed to guest memory and uploaded instead. Storage reads and writes of format 43 keep the packed `R32_UINT` path. - [SW_64KB_Z_X color targets](../../../core/libs/prx/libSceAgcDriver/Graphics/src/ColorTargetLayout.cpp) (libSceAgcDriver) use the texture equations and 64 KiB block geometry of `SW_64KB_Z_X`, whose addresses are checked against [AMD addrlib GFX10](https://github.com/GPUOpen-Drivers/pal/blob/c5e800072a32f68b6ccc4422936d96167c6e0728/src/core/imported/addrlib/src/gfx10/gfx10addrlib.cpp) in the texture tiling tests. Any 256-byte pipe/bank XOR below the block size is accepted, as for `SW_64KB_R_X` targets and Z_X textures; which of those bits the console honours, and the render results, are not measured. @@ -97,20 +104,21 @@ - [8_8 UINT color targets](../../../core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp) (libSceAgcDriver) decode as `R8G8_UINT` with the same component-swap mapping as 8_8 UNORM/SNORM (`0xe4` / `0xec`). Follows the existing 8 and 8_8_8_8 UINT paths; not measured on the console. RetroArch (PPSA99169) uses it during menu startup. - [Quad-list primitives](../../../core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp) (libSceAgcDriver): primitive type 19 (DI_PT_QUADLIST, 0x13) translates to Vulkan triangle lists by generating 6 indices per 4-vertex quad in a synthetic 32-bit index buffer during draw input preparation (triangles (0, 1, 2) and (0, 2, 3) for first provoking vertex; (0, 1, 3) and (1, 2, 3) for last provoking vertex). Incomplete trailing vertices (< 4) are discarded. Indirect draws, primitive restart and draw recipes are unsupported for quad-list primitives. Console behavior for incomplete quads has not been measured on hardware. - [Separate alpha blend into 8_8 SWAP_ALT targets](../../../core/libs/prx/libSceAgcDriver/Graphics/src/Pipeline.cpp) (libSceAgcDriver): the second channel of a two-channel target with `COMP_SWAP` 1 holds the alpha export, but Vulkan blends it with the color equation of the R8G8 attachment, so a draw whose alpha equation differs from its color equation is recorded twice, the first channel with the color equation and the second with the alpha factors and operation moved into the color equation. The vertex stages run twice, so a vertex shader that writes memory writes twice, and occlusion counts include both passes. Draws that write depth or stencil, pixel shaders that write memory and factors of the destination alpha throw. That the hardware blends the second channel with the alpha equation is taken from Mesa's `si_translate_colorswap` and radeonsi's use of it for luminance-alpha formats, not measured on hardware. +- [Color logic operations](../../../core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp) (libSceAgcDriver): `CB_COLOR_CONTROL.ROP3` accepts the 28 codes of the source and destination form (`pqrspqrs`) or the brush and destination form (`pqpqrsrs`, the brush is the shader color), as the R6xx 3D register reference describes the field and Mesa's `gfx10.json` `ROP3` enum lists. A code that combines source and brush, which the console emulates in software, throws. - [Unexported pixel inputs](../../../core/libs/prx/libSceAgcDriver/Graphics/src/ShaderInputState.cpp) (libSceAgcDriver): a pixel input whose `SPI_PS_INPUT_CNTL` parameter the previous stage does not export is programmed as a default-value input (`OFFSET` bit 5, `DEFAULT_VAL` 0, FP16 high half too) and reads (0, 0, 0, 0), as radeonsi's `SI_PS_INPUT_CNTL_UNUSED` does and as rect-list draws already do. The console's value for an unwritten parameter cache entry is not measured on hardware. - [Per-sample pixel shading](../../../core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp) (libSceAgcDriver): `PA_SC_MODE_CNTL_1.PS_ITER_SAMPLE` (bit 16, [PS5_Mesa GFX10.3 registers](https://github.com/mihawk-99/PS5_Mesa/blob/0b2d6d1a61d9bbf89cf8beb88a696144f67c61f8/src/amd/registers/gfx103.json)) on a multisampled draw enables Vulkan sample shading with a minimum rate of 1.0, so the pixel program runs once per sample. The draw throws when the device lacks `sampleRateShading`. On a single-sample draw the bit has no effect and is dropped. That the console runs the pixel program once per covered sample, and what it does with the other multisample registers in that mode, is taken from the register name and Vulkan's definition, not measured on hardware. Without the bit the pixel program runs once per pixel: the recompile request carries the mode (`iteratesSamples`), the ancillary's sample number reads 0 instead of `SampleId`, whose use would make Vulkan shade per sample, and the sample barycentrics and positions are evaluated at sample 0, whose locations the request carries (`sampleLocations`, every sample's in an iterated draw). `DB_EQAA.PS_ITER_SAMPLES`, the number of samples an iterated shader covers, is not read: an iterated draw runs once per sample. - [Unused pixel programs](../../../core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp) (libSceAgcDriver): a bound pixel program is skipped when color masks have no intersection and DB export and execution enables are clear, regardless of retained SPI_SHADER_Z_FORMAT. This follows [AMD PAL DbShaderControl](https://github.com/GPUOpen-Drivers/pal/blob/c5e800072a32f68b6ccc4422936d96167c6e0728/src/core/hw/gfxip/gfx9/gfx9AbiToPipelineRegisters.h) and its [GFX10 register definitions](https://github.com/GPUOpen-Drivers/pal/blob/c5e800072a32f68b6ccc4422936d96167c6e0728/src/core/hw/gfxip/gfx9/chip/gfx9_plus_merged_registers.h), not a hardware measurement. Enabled DB_ALPHA_TO_MASK conservatively retains the program even with ALPHA_TO_MASK_DISABLE, because DecodeState currently enables Vulkan alpha-to-coverage without applying that override. Console behavior for these combinations has not been measured. - [Sample masks](../../../core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp) (libSceAgcDriver) - `PA_SC_AA_MASK_X0Y0_X1Y0` and `PA_SC_AA_MASK_X0Y1_X1Y1` hold one 16-bit sample mask for each pixel of the aligned 2x2 quad ([Mesa gfx10.json](https://gitlab.freedesktop.org/mesa/mesa/-/blob/7b87f8ad226e/src/amd/registers/gfx10.json#L13512)); only the bits of the `MSAA_NUM_SAMPLES` rasterized samples count, and at one sample bit 0 of a pixel's field covers every sample of the target the draw is raised to. A mask equal for the four pixels is the Vulkan `pSampleMask`, the case [radv](https://gitlab.freedesktop.org/mesa/mesa/-/blob/7b87f8ad226e/src/amd/vulkan/radv_cmd_buffer.c#L13633) and [radeonsi](https://gitlab.freedesktop.org/mesa/mesa/-/blob/7b87f8ad226e/src/gallium/drivers/radeonsi/si_state.c#L4218) program; with early fragment tests it needs `earlyFragmentSampleMaskTestBeforeSampleCounting` ([radv](https://gitlab.freedesktop.org/mesa/mesa/-/blob/7b87f8ad226e/src/amd/vulkan/radv_physical_device.c#L2079) reports it). A mask that differs between the pixels is applied in the pixel shader without early fragment tests: a pixel whose mask covers all or none of its samples is demoted to a helper invocation by `gl_FragCoord` parity and killed when the program returns (the Radeon 860M's Windows driver 32.0.13058.8001 writes undefined colour for a demoted pixel whose export leaves its output unwritten), and otherwise the pixel's mask is ANDed into `gl_SampleMask` (with alpha-to-coverage and the mask export) after demoting invocations whose `SampleMask` input it leaves empty. A pixel program with early Z that writes memory under depth, stencil or depth-bounds tests is therefore refused. That XnYm is the pixel at (x mod 2, y mod 2) = (n, m) is read from the field names; that the mask removes samples before the depth and stencil tests and occlusion counting is inferred from radv implementing the Vulkan sample mask test with it; and that it takes no part in the centroid (`AA_MASK_CENTROID_DTMN` 0) is inferred from radv setting that bit only for conservative rasterization. None of it is measured on gfx1013. A mask equal for the four pixels that removes some but not all samples of a pixel is refused with active centroid inputs, since where Vulkan puts the centroid relative to `pSampleMask` is implementation-defined, and any partial mask is refused with conservative rasterization (radv sets `PREZ_AA_MASK_ENABLE` and `POSTZ_AA_MASK_ENABLE` [there](https://gitlab.freedesktop.org/mesa/mesa/-/blob/7b87f8ad226e/src/amd/vulkan/radv_cmd_buffer.c#L13653), which 0x6001 leaves clear). - [Covered-centroid raster state](../../../core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp) (libSceAgcDriver) accepts PA_SC_AA_CONFIG.COVERED_CENTROID_IS_CENTER only when the pixel program is skipped or SPI_PS_INPUT_ENA and SPI_PS_INPUT_ADDR have no active perspective or linear centroid input. Bit 29 follows [PS5_Mesa GFX10.3 registers](https://github.com/mihawk-99/PS5_Mesa/blob/0b2d6d1a61d9bbf89cf8beb88a696144f67c61f8/src/amd/registers/gfx103.json); [RADV](https://github.com/mihawk-99/PS5_Mesa/blob/0b2d6d1a61d9bbf89cf8beb88a696144f67c61f8/src/amd/vulkan/radv_cmd_buffer.c) sets it on GFX10.3 and later. Active centroid interpolation and unspecified input registers still throw. Console covered-pixel centroid results were not measured. - [Compute LDS beyond the device's shared-memory limit](../../../core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp) (libSceAgcDriver) lives in a coherent storage buffer with one slice of `LDS bytes / 4 + 1` dwords, rounded up to an even count so that 64-bit atomics stay 8-byte aligned, per workgroup (the dword after the LDS is the atomic lock on a device without `shaderBufferInt64Atomics`), shared by all such dispatches and grown in 1 MiB steps up to `maxStorageBufferRange`; a dispatch whose slices do not fit throws. It is slower than shared memory and its cost was not measured. An indirect dispatch of such a program always reads its group counts on the CPU. Its results are pinned against the in-limit semantics in `agc_driver_lds_beyond_device_limit`, not measured on hardware. -- [AGC buffer allocations](../../../core/libs/prx/libSceAgcDriver/Graphics/src/BufferPool.cpp) retry host/device out-of-memory once after reclaiming idle pool buffers and fully unused slab blocks. Live buffers and blocks with live slots are retained. Live images and other retained resources have no global memory bound; allocation failure still throws when reclaim cannot satisfy the request. +- [AGC buffer allocations](../../../core/libs/prx/libSceAgcDriver/Graphics/src/BufferPool.cpp) retry host/device out-of-memory once after reclaiming idle pool buffers and fully unused slab blocks. Live buffers and blocks with live slots are retained. Live images and other retained resources have no global memory bound; allocation failure still throws when reclaim cannot satisfy the request, except for the device buffers and images that the "Host placement after a device-memory refusal" entry places in host memory. - [Storage-cache pressure and idle device buffers](../../../core/libs/prx/libSceAgcDriver/Graphics/src/BufferPool.cpp): the one-second idle observation interval is a retention choice, not a console rule; buffers reused less often can be recreated under pressure. Before a storage-cache eviction, independently allocated device-tier buffers unused throughout that interval can be released, oldest first, for the cache's shortfall below its existing ceiling. Live buffers, host tiers and slab allocations are not reclaimed this way. Reclamation requires exactly one device-local heap because retained buffers do not record their heap. The heap budget is read again after an actual release; a delayed driver report can still cause an eviction. This is not a shared budget for all retained GPU resources, and its effect on titles with intermittent large buffer use is unmeasured. - [Host placement after a device-memory refusal](../../../core/libs/prx/libSceAgcDriver/Graphics/src/BufferPool.cpp) (libSceAgcDriver): when the driver refuses a device-local allocation with `VK_ERROR_OUT_OF_DEVICE_MEMORY` after the pool's trim and the texture-cache release, device buffers and images (textures, storage textures, block, repacked, depth, depth plane and stencil copies, render, depth and multisampled targets and attachment proxies) are allocated in a host memory type their requirements allow. A host-placed device buffer leaves the pool on release. A host-placed image keeps that placement until it is destroyed and is never moved back to device memory, so every access to it crosses PCIe for its lifetime. The placements are counted in a rate-limited `[bufferpool]` line with the cumulative MiB, not the resident bytes. The 1x1 denormal clear probe still allocates device memory directly and throws on a refusal. -- [Pipeline journal](../../../core/libs/prx/libSceAgcDriver/Graphics/src/PipelineJournal.cpp) (libSceAgcDriver): the journal is append-only and never compacted, so a module a later run no longer builds pipelines from stays in the file until it is deleted, and the whole file is read into memory while the replay runs (its modules reference that buffer until the last replayed pipeline was built). Only the first `APS5_PIPELINE_JOURNAL_ENTRIES` pipelines recorded are replayed (a record is never rewritten, so a pipeline past the bound stays past it), and the pipeline store evicts the least recently used entry past `APS5_PIPELINE_STORE_ENTRIES` (1024 each): a title with more configurations rebuilds the rest at the draw as before. A compute pipeline is replayed over the set layout `DescribeSetLayouts` derives from the result's bindings, which a dispatch that finds it resident checks against its `ShaderResources` layout only under `APS5_VERIFY_SET_LAYOUTS=1`. Recording and replay are best effort: a record whose write fails is lost, an entry whose build fails is skipped, and both are counted in the `[pipeline-journal]` line rather than stopping the run. A replayed pipeline that the title never asks for again costs its build time and its driver memory (its optimized relink is deferred to its first draw, so a run whose first frames bind many replayed pipelines queues their relinks then); nothing ages recipes out of the file. Two runs sharing the file take turns under its lock file, but each only knows the modules it loaded or wrote itself, so a module the other run appended meanwhile may be appended again (the loader keeps the first copy). Records a run appends after another run's load are replayed by that run only after its next start. The file header's format number is the only guard against a change of the recipe codec, `CanonicalPipelineState` or the store key that keeps the structures' sizes (the recompiler's source version does not cover the driver's sources). +- [Pipeline journal](../../../core/libs/prx/libSceAgcDriver/Graphics/src/PipelineJournal.cpp) (libSceAgcDriver): the journal is append-only until it reaches `APS5_PIPELINE_JOURNAL_MAX_MIB`, when it is rewritten to three quarters of the cap with the newest pipelines and their modules; the rewrite streams kept records; a replay thread reads an entry's modules when it builds that entry. Only the last `APS5_PIPELINE_JOURNAL_ENTRIES` pipelines are replayed, and the store evicts the least recently used entry past `APS5_PIPELINE_STORE_ENTRIES`: a title with more configurations rebuilds the rest at the draw. A compute pipeline is replayed over the set layout `DescribeSetLayouts` derives from the result's bindings, checked against its `ShaderResources` layout only under `APS5_VERIFY_SET_LAYOUTS=1`. Recording and replay are best effort: a record whose write fails is lost and an entry whose build fails is skipped, both counted in the `[pipeline-journal]` line. The file header's format number guards the recipe codec and the store key. - [Graphics pipeline libraries](../../../core/libs/prx/libSceAgcDriver/Graphics/src/PipelineLibrary.cpp) (libSceAgcDriver): a pipeline is fast-linked at the draw and relinked with `VK_PIPELINE_CREATE_LINK_TIME_OPTIMIZATION_BIT_EXT` on a background thread, and binds the optimized one from the next draw on. Draws recorded before that use the fast link, whose vertex stage keeps the outputs the fragment stage does not read: on RADV (Radeon 660M, Mesa 26.2.4) 346 of 483 Astro's Playroom vertex shaders paired with a fragment stage that reads none of them used more VGPRs (median 8 more, up to 2.5 times), and on an RTX 4090 13 of 15 PPSA36082 vertex shaders up to 68 instead of 38 (#2242). The optimized link compiles to the same code as a whole pipeline +- [Fragment shader interlock pipelines](../../../core/libs/prx/libSceAgcDriver/Graphics/src/Pipeline.cpp) (libSceAgcDriver): a pipeline whose fragment shader declares a fragment shader interlock capability is never built from graphics pipeline libraries, on every driver, so it is compiled whole at the draw with no fast link to bridge the compile. RADV's fast link ([mesa#16653](https://gitlab.freedesktop.org/mesa/mesa/-/work_items/16653), Mesa 26.2.5, RX 7900 GRE) jumps to the PS epilog without waiting for the shader's stores, and the epilog's `done` export ends the GFX11 primitive-ordered section, so the next overlapping wave reads the old value: `agc_driver_pixel_interlock` lost read-modify-writes in 108 of 120 loaded runs under `APS5_NO_PIPELINE_LTO=1`. On an RTX 4090 (NVIDIA 617.42, Windows) the fast link kept the ordering in 240 of 240 loaded runs; other drivers are not measured. - [Prebuilt pipeline libraries](../../../core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp) (libSceAgcDriver): each registered vertex or pixel shader in registration form gets its pre-rasterization or fragment-shader library built on one background thread, with a fixed state the draw may not use: triangle list, counter-clockwise front face, no depth clamp, depth bias or conservative rasterization, first provoking vertex, one sample. Libraries are kept until the device is torn down; on Astro's Playroom 1948 of them added 0.44 GB of peak memory on RADV. - [`APS5_THREAD_PRIORITY=rt`](../../../core/libs/prx/libSceAgcDriver/Execution/src/ThreadPriority.cpp) (libSceAgcDriver) lowers the process's hard `RLIMIT_RTTIME` when it is above `APS5_THREAD_RTTIME_US` or rtkit's `RTTimeUSecMax`, because [rtkit](https://github.com/heftig/rtkit/blob/c295fa849f52b487be6433e69e08b46251950399/rtkit-daemon.c#L592-L637) (`verify_process_rttime`) reads the hard limit from `/proc//limits` and refuses real-time scheduling when it is higher. An unprivileged process cannot raise the hard limit again, so it stays lowered for the process and its threads even when rtkit then refuses the request (burst limit, user or start-time checks) and the thread throws. rtkit's `MaxRealtimePriority` and `RTTimeUSecMax` are read first, so an unreachable rtkit or a priority above its maximum leaves the limit untouched. The lowered limit only matters for threads that later run under a real-time policy -- AMD image descriptors with `TYPE=0` are treated as null for read-only image slots even when the base and other words are nonzero: `image_load` on a gfx1035 with such a T# returns 0,0,0,0, and the same descriptors were seen in direct reads in PPSA01341 on an RTX 4070. Writable and atomic image slots and entries of an image table keep faulting because there is no hardware measurement for them, and the reserved types 2-7 also fault. `TYPE=1` is treated as null in the same read-only slots: gfx1035 reads it as zeros too, and gfx1010 returns zeros for `image_sample_lz`, `image_sample_b`, `image_load` and `image_get_resinfo` with format 22 (#5828); it is not measured on RDNA2, with other formats or with depth-compare reads (`image_sample_c*`). gfx1013 behavior is unverified - On a device without VK_EXT_robustness2's `nullDescriptor`, such as MoltenVK, a null image descriptor in a typed heap is a [placeholder image](../../../core/libs/prx/libSceAgcDriver/Graphics/src/PlaceholderImages.cpp) of that heap's type, 1×1 and cleared to zero, which every null descriptor of the heap shares. Reads give zeros, as `nullDescriptor` allows, but stores and atomics to it are kept, so a later read of a null storage image can return them, and a depth comparison compares against depth 0. A heap whose placeholder the device cannot create, for example 1D or 3D depth, 64-bit atomics without 64-bit image atomics, or multisampled storage without `shaderStorageImageMultisample`, throws when a null descriptor reaches it. - [Draw input snapshots](../../../core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp) (libSceAgcDriver): `PrepareDrawBindings` caps read-only input buffer snapshots at 64 MiB (overridable via `APS5_DRAW_SNAPSHOT_MAX_MIB`). Larger inputs (streaming heaps, bindless pools) or draws exceeding the cumulative budget skip the per-draw snapshot and bind in place, avoiding multi-gigabyte CPU copy stalls (such as Ratchet & Clank: Rift Apart PPSA01474 stalling at frame ~380 copying 36 GB every 5 s). If the title rewrites those large ranges on the CPU before GPU completion, the in-place binding observes the modification, as verified by `drawSnapshotCapTests`. - [Pending storage-image results](../../../core/libs/prx/libSceAgcDriver/Graphics/src/Texture.cpp) (libSceAgcDriver) are stored when something reads their memory, not when the GPU work that wrote them completes. A title that waits for that work, frees the image and reuses its memory can have the new data overwritten by the late store (#1970). A title's own CPU reads are not seen either, so a title that reads the results after waiting for a label reads stale memory. Explicit-size file writes (`write`, `pwrite`, `writev`, `pwritev`, AIO and `fwrite`) synchronize their source ranges through the [host-read hook](../../../core/libs/prx/libc/src/GuestArena.cpp); other host readers are not covered. `APS5_STORE_AT_FLIP=1` stores every pending result at each flip and `APS5_STORE_AT_LABEL=1` before each label the GPU writes; both store every pending image, not only the ones the waited-for work wrote, and neither is on by default. @@ -139,7 +147,7 @@ - [WRITE_DATA to thread-trace user-data registers](../../../core/libs/prx/libSceAgcDriver/Execution/src/Pm4.cpp) (libSceAgcDriver) updates the queue's UCONFIG values for register destinations `0xc340..0xc347`, including repeated writes to one address; these are not guest-memory labels. The range follows AMD's GFX10 `SQ_THREAD_TRACE_USERDATA_0..7` definitions in [the Linux register header](https://github.com/torvalds/linux/blob/master/drivers/gpu/drm/amd/include/asic_reg/gc/gc_10_1_0_offset.h) and the packets emitted by PPSA21564's bundled AGC driver before its EOP flip. Other register destinations throw. GPU thread-trace capture and its user-data stream are not emitted; only the final register state is retained. Console trace behavior is unverified. - [WRITE_DATA with DST_SEL 3](../../../core/libs/prx/libSceAgcDriver/Execution/src/Pm4.cpp) (libSceAgcDriver) writes guest memory like DST_SEL 5, which a PS5 does not: on firmware 13.40 (measured 2026-10-19 with buffers submitted through `sceAgcDriverSubmitDcb` inside a retail title) a DST_SEL 3 `WRITE_DATA` (control 0x00100300, or 0x06100300 with cache policy 3) left its label in direct memory unwritten, also after a `RELEASE_MEM` with GL2 write-back and invalidate (0x00300514), and a GDS `COPY_DATA` (SRC_SEL 3) at that address read 0. The console's own `sceAgcDcbWriteData(dst 6)` builds DST_SEL 3 (0x00110300) as here, and Killzone: Liberation (PPSA11142) fences its frame with it and waits on that label with `WAIT_REG_MEM`, so the write is kept until the path that lands the label is known. Closes when: that path is measured - [SET_*_REG_INDIRECT](../../../core/libs/prx/libSceAgcDriver/Execution/src/Driver/Queues/Submission.cpp) (libSceAgcDriver) reads its register list when the command buffer is submitted, with the packets. The console's graphics PFP reads it when it reaches the packet, so a list written after the submission (by the CPU, or by GPU work ahead of the packet behind PFP_SYNC_ME) is read here before that write. When the list read at submission holds an offset dword that is not a register offset (bits 16-27 set, or the 0xffffffff sentinel), the worker reads the list again where the packet executes and writes that copy. PPSA23566 (UFC 6) intermittently submits a SET_CONTEXT_REG_INDIRECT whose first entry is 0x038f038f or 0x03900390 in both copies; what the PFP does with such an entry is unknown, so a copy that still holds one throws unless `APS5_SKIP_INVALID_INDIRECT_REGISTERS=1`, which skips its invalid entries and writes its valid ones. -- The [shader float mode](../../../core/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cpp) (`FLOAT_MODE`, `DX10_CLAMP`, `IEEE_MODE` and `FP16_OVFL` of `COMPUTE_PGM_RSRC1` and `SPI_SHADER_PGM_RSRC1_PS/GS/HS`, bit positions from Mesa's `gfx10.json` and `gfx103.json`) is decoded into `GuestContext::floatMode` and is part of the shader cache keys and the serialized request (#981). `IEEE_MODE` is read by `v_min_f32`, `v_max_f32`, `v_min3_f32`, `v_max3_f32` and `v_med3_f32`, their f16 forms, `v_min_f64` and `v_max_f64`, `IEEE_MODE` and the `FLOAT_MODE` output denormal bits by the output modifier, `DX10_CLAMP` by the clamp modifier, `FP16_OVFL` by every instruction with an f16 result (see its entry), and the whole mode by the 16-bit interpolation instructions (see their entries). With IEEE=1 a signaling NaN operand returns that NaN quieted (the first operand checked first, min3/max3 as two nested pairs, med3 as min3 when any operand is NaN), measured on RDNA2 (gfx1035) over every combination of ±0, ±1, ±inf, denormals, quiet and signaling NaNs with f32 denormals kept and flushed. IEEE=0 and unknown modes keep the previous f32 lowering, which matches IEEE=0. The f16 forms choose their NaN results on the bits: a NaN operand returns the other operand unchanged (src0 when both are NaN), and with IEEE=1 a signaling NaN operand returns that NaN quieted, in the same order as f32. `v_min_f64` and `v_max_f64` quiet a signaling NaN operand the same way with IEEE=1; with IEEE=0 they return src0 unchanged when src1 is NaN, otherwise src1 when src0 is NaN. Both were measured on RDNA2 (gfx1036) at IEEE=0 and IEEE=1 with f32 denormals kept and flushed and f16/f64 denormals kept; RDNA2 has no f64 min3, max3 or med3. Unknown modes follow IEEE=0. `v_min_f16` and `v_max_f16` flush each denormal input to its signed zero when bit 0 of the f16/f64 denormal field is clear (0 and 2); fields 1 and 3 preserve inputs, even when output flushing is enabled. Measured on RDNA2 (gfx1036) at IEEE=0 and IEEE=1, including signed zeros, NaNs and neg/abs modifiers. Unknown modes keep f16 denormals. `v_min_f64` and `v_max_f64` flush subnormal inputs to signed zero when bit 0 of the f16/f64 denormal field is clear (0 and 2); fields 1 and 3, and an unknown mode, preserve inputs. The fully flushed and preserved f64 behavior was [measured on gfx1036](https://github.com/boykopovar/AnyPS5/pull/2036); split-mode selection follows [LLVM's denormal-mode enumeration](https://llvm.org/docs/AMDGPUUsage.html#floating-point-denorm-mode-enumeration-values). Other f64 arithmetic does not model these denormal controls. NaN quieting in f16 arithmetic (the unary instructions and the f32 arithmetic follow it, see below) and `FLOAT_MODE` (f32 denormals flushed before the min/max compare in Astro's Playroom's 0xc0) still follow a single mode, as the entries below record. The mode comes from the registered shader header's `sh_registers`, the same source at registration and at draw/dispatch so the prepared artifact keys match. A title that rewrites RSRC1 at run time, or a raw compute program with no header, compiles with that mode or none. +- The [shader float mode](../../../core/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cpp) (`FLOAT_MODE`, `DX10_CLAMP`, `IEEE_MODE` and `FP16_OVFL` of `COMPUTE_PGM_RSRC1` and `SPI_SHADER_PGM_RSRC1_PS/GS/HS`, bit positions from Mesa's `gfx10.json` and `gfx103.json`) is decoded into `GuestContext::floatMode` and is part of the shader cache keys and the serialized request (#981). `IEEE_MODE` is read by `v_min_f32`, `v_max_f32`, `v_min3_f32`, `v_max3_f32` and `v_med3_f32`, their f16 forms, `v_min_f64` and `v_max_f64`, `IEEE_MODE` and the `FLOAT_MODE` output denormal bits by the output modifier, `DX10_CLAMP` by the clamp modifier, `FP16_OVFL` by every instruction with an f16 result (see its entry), and the whole mode by the 16-bit interpolation instructions (see their entries). With IEEE=1 a signaling NaN operand returns that NaN quieted (the first operand checked first, min3/max3 as two nested pairs, med3 as min3 when any operand is NaN), measured on RDNA2 (gfx1035) over every combination of ±0, ±1, ±inf, denormals, quiet and signaling NaNs with f32 denormals kept and flushed. IEEE=0 and unknown modes keep the previous f32 lowering, which matches IEEE=0. The f16 forms choose their NaN results on the bits: a NaN operand returns the other operand unchanged (src0 when both are NaN), and with IEEE=1 a signaling NaN operand returns that NaN quieted, in the same order as f32. `v_min_f64` and `v_max_f64` quiet a signaling NaN operand the same way with IEEE=1; with IEEE=0 they return src0 unchanged when src1 is NaN, otherwise src1 when src0 is NaN. Both were measured on RDNA2 (gfx1036) at IEEE=0 and IEEE=1 with f32 denormals kept and flushed and f16/f64 denormals kept; RDNA2 has no f64 min3, max3 or med3. Unknown modes follow IEEE=0. `v_min_f16` and `v_max_f16` flush each denormal input to its signed zero when bit 0 of the f16/f64 denormal field is clear (0 and 2); fields 1 and 3 preserve inputs, even when output flushing is enabled. Measured on RDNA2 (gfx1036) at IEEE=0 and IEEE=1, including signed zeros, NaNs and neg/abs modifiers. Unknown modes keep f16 denormals. `v_min_f64` and `v_max_f64` flush subnormal inputs to signed zero when bit 0 of the f16/f64 denormal field is clear (0 and 2); fields 1 and 3, and an unknown mode, preserve inputs. The fully flushed and preserved f64 behavior was [measured on gfx1036](https://github.com/boykopovar/AnyPS5/pull/2036); split-mode selection follows [LLVM's denormal-mode enumeration](https://llvm.org/docs/AMDGPUUsage.html#floating-point-denorm-mode-enumeration-values). `v_fma_f64`, `v_div_fmas_f64`, `v_div_scale_f64`, `v_ldexp_f64`, `v_rcp_f64`, `v_rsq_f64`, `v_sqrt_f64` and `v_trig_preop_f64` do not model these denormal controls. NaN quieting in f16 arithmetic (the unary instructions and the f32 arithmetic follow it, see below) and `FLOAT_MODE` (f32 denormals flushed before the min/max compare in Astro's Playroom's 0xc0) still follow a single mode, as the entries below record. The mode comes from the registered shader header's `sh_registers`, the same source at registration and at draw/dispatch so the prepared artifact keys match. A registered shader keeps that mode when a title rewrites RSRC1; a raw compute program takes the live RSRC1. - A [WAIT_REG_MEM](../../../core/libs/prx/libSceAgcDriver/Execution/src/Driver/Synchronization/WaitMemory.cpp) (libSceAgcDriver) that a compute queue passes on another queue's recorded label store, before the GPU has executed it, is landed before the queue's next dispatch, draw, COND_EXEC or predicated packet reads guest memory: the worker polls until memory satisfies the wait or no unfinished batch writes the label. The graphics queue (queue 0) is exempt, as landing its waits costs PPSA01341 about a quarter of its frame rate; a title that fills memory a graphics command buffer reads after it submits the buffer behind such a wait reads it early there. `APS5_LAND_WAITS_ALL_QUEUES=1` lands the graphics queue's waits too. - [CPU reads behind an address-based build](../../../core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/Draw.cpp) (libSceAgcDriver) - an address-based store (FLAT/GLOBAL, or through a descriptor the recompiler cannot resolve to a static buffer) is not recorded as a pending write: its batch holds a lease on the registered allocations instead, and the flush hook does not wait for it. A DISPATCH_INDIRECT whose arguments are read on the CPU first finishes the batches recorded with a lease (`Graphics::FinishLeaseWork`). These reads do not, so they can see guest memory from before such a build ran: the indirect draw arguments and draw count of a draw resolved on the CPU (`Pm4::ReadDrawArguments`, `Pm4::ReadDrawCount`), the count record of the [copy kernel](../../../core/libs/prx/libSceAgcDriver/Execution/src/Driver/Memory/BufferCopy.cpp) HLE (guarded only by its shape check), and the checks in [Submission.cpp](../../../core/libs/prx/libSceAgcDriver/Execution/src/Driver/Queues/Submission.cpp) that decide whether a queue awaits a label only the title writes (`awaitsTitle`, `orderReleased`). No test covers them and no title has been seen to read such memory there. - The [dispatches that follow a compute queue's wait](../../../core/libs/prx/libSceAgcDriver/Execution/src/Driver/Dispatch/DispatchAhead.cpp) (libSceAgcDriver), up to the next wait that memory does not satisfy, a packet that writes guest memory other than a label, or a conditional packet, are resolved (memory captured, shader recompiled) when the wait passes, as the CP reads them right after it; the graphics queue is exempt (`APS5_GROUP_CAPTURE_ALL_QUEUES=1` includes it). The device still reads guest memory when the dispatch is issued; when that memory no longer matches the capture, the dispatch is resolved again from current memory. @@ -157,23 +165,22 @@ - [PA_SC_CONSERVATIVE_RASTERIZATION_CNTL](../../../core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp) (libSceAgcDriver) - 0x6001 uses Vulkan overestimation on devices that add at most 1/256 pixel and keep degenerate triangles; the console's uncertainty region and degenerate handling are unmeasured. A value with `OVER_RAST_ENABLE` and `UNDER_RAST_ENABLE` clear is decoded as disabled even when `UNCERTAINTY_REGION_MODE` or the `OUTER_`/`INNER_UNCERTAINTY_EDGERULE_OVERRIDE` bits (19:16) are set, as PPSA01474 writes 0xe6000; that these fields have no effect without over- or underestimation is assumed from their names, not measured. With `PA_SC_AA_CONFIG.MSAA_NUM_SAMPLES` = 0 the `PERSP_CENTROID` and `LINEAR_CENTROID` inputs get the pixel-centre barycentrics, as without conservative rasterization ([shader debt](shader.md)): the one rasterized sample sits at the pixel centre, so every centroid the rasterizer could pick for an overestimated pixel is that point; that it does not move the centroid towards the primitive when the sample is uncovered is inferred, not measured. With more rasterized samples, and when a single-sample draw is raised to a multisampled color target, centroid inputs throw. - [Descriptor pool chain](../../../core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp) (libSceAgcDriver) - each pool holds two storage buffer descriptors more than a set can take, as a workaround: RADV (Mesa 26.0 and later) lets a `FREE_DESCRIPTOR_SET` pool place a set up to 32 bytes past the pool's buffer ([`util_vma_heap_init`](https://gitlab.freedesktop.org/mesa/mesa/-/blob/main/src/amd/vulkan/radv_descriptor_pool.c) is given `bo_size + RADV_POOL_HEAP_OFFSET` as a size), and with a page-multiple pool size those bytes are an unmapped page - [CMASK fast clears](../../../core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp) (libSceAgcDriver) - each fast-cleared CMASK tile (code 0) is stored as `CB_COLOR_CLEAR_WORD` into its 8x8 texels at the first draw into the target or at its fast-clear eliminate or DCC decompress pass, not as the color block writes the tile, and the CMASK is then stored as expanded (0xF): a texture or copy read of the target after that draw and before the title's eliminate sees the clear value in tiles no draw wrote, where the console keeps their previous bytes. Tile addresses follow the pipe-aligned SW_64KB_Z_X CMASK equation for 16 pipes; other CMASK swizzle modes, a cleared tile of a DCC target and a fast clear of a 3D or array color target throw; base mip 0 of a 2D mipmapped color target is supported (offset follows AMD Addrlib `Gfx10Lib::HwlComputeCmaskInfo` reverse mip-chain ordering preceding base mip 0, unverified on PS5 hardware; mipmapped targets with 8 or 16 bpp throw until `dsMipmapHtileFix` mip-tail shrinkage is modelled). A tile with a code other than 0 and 0xF keeps its texels like an expanded tile: PPSA18391 (engine: Clickteam Fusion) fast clears a target whose CMASK it never initialized, so some of its nibbles hold leftover values such as 1 and 10; what the console does with such codes on a single-sample target is unverified on hardware. Single-sample views with `viewMip > 0` skip CMASK fast-clear decoding since CMASK metadata only covers base mip 0 on AMD hardware (Mesa `si_state.c` similarly clears `FAST_CLEAR` for level > 0). -- [CMASK clear pass on AMD without pipeline libraries](../../../core/libs/prx/libSceAgcDriver/Graphics/src/TextureDetiler.cpp) (libSceAgcDriver) - the compute pass that stores a fast-cleared CMASK into a resident render target (`DispatchCmaskClear`) is unverified on AMD's Windows driver with `APS5_NO_GPL=1`: on a Radeon 860M (driver 32.0.13058.8001, Windows 11) `agc_driver_cmask_fast_clear` keeps the stale 0x5A5A5A5A in its `SW_64KB_R_X` pass instead of the clear value, while it passes with pipeline libraries on and on an NVIDIA RTX 4080 SUPER in both modes. An RX 6400 (driver 32.0.21033.3005) lost the device (read invalid at 0xbeefdeadc000) in an earlier build tree, but clean rebuilds of four main commits of 19 October pass there (three of three runs each), so that failure is not reproduced. The cause is not found (#5150). Closes when: the cause is found, or `ctest -R '^agc_driver_cmask_fast_clear$' --repeat until-fail:5` passes with `ANYPS5_GPU=Radeon` and `APS5_NO_GPL=1` on AMD. -- [HTILE depth fast clears](../../../core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp) (libSceAgcDriver) - HTILE is not modeled. Only RADV's `clear_htile_mask` compute kernel (17 GFX10 words, 64x1x1 groups) is recognized, and only when it leaves ZMASK at 0 (`user[4]` and `user[5]` low nibbles both 0); the dispatch still runs, and the host depth image is cleared to `DB_DEPTH_CLEAR` the next time the surface with that `DB_HTILE_DATA_BASE` address is bound. The address match is exact, so a clear of a different HTILE range does not hit. HTILE initialization, the stencil-only update, and fast clears made by the SDK's own paths, CP DMA fills or other kernels are not covered and leave the depth image as it was. The kernel words are the ones the PR author saw RADV submit; they are not compared with Mesa's source. A second kernel, the 9-word dword fill PPSA01802 (Kena: Bridge of Spirits) clears its depth HTILE with (64x1x1 groups, one `buffer_store_format_x` per thread through the stride-4 V# in `user[0..3]`, the value read by `s_buffer_load_dword` through the V# in `user[4..7]`), is taken as a fill of that range when the store format is 32_UINT, 32_SINT or 32_FLOAT and the value V# covers the dword (NUM_RECORDS times its stride, or NUM_RECORDS bytes when the stride is 0) ([`MatchMemoryValueFill`](../../../core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp), then the whole-HTILE and ZMask rules of `NoteDepthMetadataFill`). The value is read from guest memory when the dispatch is decoded, so a value that earlier GPU work in the same submission has not written back yet is not seen. When the [fill HLE](../../../core/libs/prx/libSceAgcDriver/Execution/src/Driver/Memory/BufferFill.cpp) takes the dispatch instead (an output descriptor of one 32-bit value per element, a value descriptor that covers the loaded dword, a 16-byte aligned range), the value is read again when the fill is recorded, after the labels queued before it, and the HTILE fill is noted with that value. +- [HTILE depth fast clears](../../../core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp) (libSceAgcDriver) - HTILE is not modeled. Only RADV's `clear_htile_mask` compute kernel (17 GFX10 words, 64x1x1 groups) is recognized, and only when it leaves ZMASK at 0 (`user[4]` and `user[5]` low nibbles both 0); the dispatch still runs, and the host depth image is cleared to `DB_DEPTH_CLEAR` the next time the surface with that `DB_HTILE_DATA_BASE` address is bound. A clear hits a surface only when its range covers that surface's whole HTILE, which need not start where the clear does. Fast clears that other writers leave in HTILE are taken at bind as the next entry describes. The kernel words are the ones the PR author saw RADV submit; they are not compared with Mesa's source. A second kernel, the 9-word dword fill PPSA01802 (Kena: Bridge of Spirits) clears its depth HTILE with (64x1x1 groups, one `buffer_store_format_x` per thread through the stride-4 V# in `user[0..3]`, the value read by `s_buffer_load_dword` through the V# in `user[4..7]`), is taken as a fill of that range when the store format is 32_UINT, 32_SINT or 32_FLOAT and the value V# covers the dword (NUM_RECORDS times its stride, or NUM_RECORDS bytes when the stride is 0) ([`MatchMemoryValueFill`](../../../core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp), then the whole-HTILE and ZMask rules of `NoteDepthMetadataFill`). The value is read from guest memory when the dispatch is decoded, so a value that earlier GPU work in the same submission has not written back yet is not seen. When the [fill HLE](../../../core/libs/prx/libSceAgcDriver/Execution/src/Driver/Memory/BufferFill.cpp) takes the dispatch instead (an output descriptor of one 32-bit value per element, a value descriptor that covers the loaded dword, a 16-byte aligned range), the value is read again when the fill is recorded, after the labels queued before it, and the HTILE fill is noted with that value. - [HTILE written by other kernels](../../../core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp) (libSceAgcDriver) - when a depth target is bound and its HTILE was written since it was last read, the words over its whole pipe-aligned metadata (`HtileMetadataBytes`, the extent `HtileFillCovers` takes) are read, outside the surfaces mutex since the read waits for recorded work: when every one marks the depth (ZMask 0) or the stencil (SMem 0) cleared, the surface takes that fast clear and the words are stored back as expanded (depth only: ZMask 15 over the full Z range; with stencil: ZMask 15, Z range and SMem, SR1 and SR0 3), so only the title's next clear clears it again. PPSA29141 clears its depth every frame with a compute kernel the fill hooks do not recognize (`InvokeHTileClearComputeShader` in its `ClearMRT` pass); without this its depth kept every earlier frame and moving objects left black trails. PPSA25344 (Unreal Engine 5) reads its HTILE after the depth prepass in its Nanite depth export, which takes a tile with ZMask 0 as holding the clear value; without the expanded words the export put Nanite depth over the prepass and the Nanite material pass painted over the meshes in front. The word layouts are Mesa radv's for GFX10. Metadata with some tiles cleared is not modelled, and the compression state draws leave in HTILE is not tracked, so a title reading its HTILE after a draw sees the stored expanded words. A recognized HTILE fill or clear makes the next bind read the words again. A slice other than 0 of a depth array is read at its own HTILE (`DB_HTILE_DATA_BASE` plus the slice times `HtileMetadataBytes`) but the HTILE word layout of slices 1 and up is not modelled (PPSA05684's tile masks need X8 and Y8 to take Z0 and Z1 as well, slices 4 and up unmeasured), which neither the check that every word is cleared nor the store-back of each word in place needs, so such a slice takes the clear like slice 0. A depth-only HTILE whose tiles all have ZMask 0 but different Min/Max Z (per-tile clear values, PPSA05684's cascades) is taken as a whole-surface clear to `DB_DEPTH_CLEAR`, the value the DB's depth test reads for a cleared tile (`HTILE_Z_CLEAR_REG` in Mesa's `src/amd/common/ac_descriptors.h`: the clear word "only clears HiZ", "the DB register contain[s] the full clear values"); the HiZ test before it, which rejects against each tile's own Min/Max Z range (the HTILE definitions in the same file), is not modelled, so a draw that HiZ would reject over a tile cleared with Min/Max Z 0 is tested against `DB_DEPTH_CLEAR` instead. A multisampled depth target, and any target whose HTILE overlaps a resident native depth guard, skips this read and store-back and goes to the native multisample depth owner, which keeps its own refusal for a fast-clear transition. - [RADV fill-memory kernel](../../../core/libs/prx/libSceAgcDriver/Execution/src/Driver/Memory/BufferFill.cpp) (libSceAgcDriver) - the [fill HLE](../../../core/libs/prx/libSceAgcDriver/Execution/src/Driver/Memory/BufferFill.cpp) takes RADV's 16-byte `fill_memory` compute kernel (`MatchClampedGlobalFill`: 11 GFX10.3 words, 64x1x1 groups, six user SGPRs with the address in `user[2..3]`, the largest store offset in `user[4]` and the value in `user[5]`; each thread stores 16 bytes at `min(thread * 16, user[4])`) as a fill of `min(threads * 16, user[4] + 16)` bytes. RADV initializes and fast-clears DCC keys and other metadata with it, so the key and metadata tracking sees such a fill where it is in the command stream; run as a dispatch, its global stores name no range, and a key fill was only seen once it had landed, after later writes to the surface. Only a 16-byte aligned address and largest offset are taken. RADV's 4-byte variant (fills below 16 bytes), other Mesa versions' encodings and other compilers still run as dispatches. The kernel words are the ones vkQuake's PS5 RADV build (Mesa 25.2) submitted; they are not compared with the NIR builder's output for other targets. -- [Host imports](../../../core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp) (libSceAgcDriver) on Linux skip ranges that are not mapped read-write, which are copied instead. The NVIDIA driver (595.91.07) refuses `VK_EXT_external_memory_host` imports of read-only or executable mappings (and shared mappings of regular files) with `VK_ERROR_OUT_OF_DEVICE_MEMORY`, after which every `vkAllocateMemory` on the device fails the same way. Read-only ranges are never imported, even on drivers that accept them. Shared mappings of regular files are not checked for. On Windows, an alias of adjacent registered ranges is refused if any page is not a shared view; a range made entirely of shared views is aliased run by run, including across sections or nonconsecutive section offsets. Adjacent registered ranges are joined into one import: the largest aligned power-of-two block, from twice the import alignment up to the 8 MiB window, that holds the requested address and that they fill exactly, so a block only grows by doubling as its window fills. A block that would exceed the budget, would replace an import a build holds, or fails (a refused Windows alias, `vkAllocateMemory`) steps down to the next smaller block; a failed block is not tried again until one of its ranges changes. Ranges larger than the window are imported one at a time. A buffer bound across adjacent ranges that are each larger than the window, or bound exactly over adjacent ranges of any size (from the first one's start to the last one's end), joins them into one import when it is bound, and their own imports are retired; a buffer that crosses any other two imports is copied. A heap grown in pieces re-imports its joined run only when a buffer is bound exactly over all of its pieces, or when a sparse descriptor heap over it is bound: a heap a shader binds whole while only the pieces from its base are mapped is bound in place from the import of that run, its views ending at the import's end as the copy's do, once the previous bind of that address saw the same last mapped byte (a heap that grows between every two of its binds stays copied; one bound more often than it grows is joined again after each growth) and at most 64 times per joined run base; past that, with a message, it is not joined again: it is bound in place while an existing import covers its run and copied otherwise, until no registered range starts at that base or the device changes. Ranges larger than the window are taken to be registered whole and never grown in pieces: a title that grows a heap in pieces larger than 8 MiB re-imports the joined run each time a bound buffer crosses into a new piece. The 8 MiB window is a choice: unbounded runs were re-imported whole on every growth of a heap mapped in small pieces, and the NVIDIA driver (610.57.04, 615.78.08) then refused imports or lost the device. Not measured on RADV. +- [Host imports](../../../core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp) (libSceAgcDriver) on Linux skip ranges that are not mapped read-write, which are copied instead. The NVIDIA driver (595.91.07) refuses `VK_EXT_external_memory_host` imports of read-only or executable mappings (and shared mappings of regular files) with `VK_ERROR_OUT_OF_DEVICE_MEMORY`, after which every `vkAllocateMemory` on the device fails the same way. Read-only ranges are never imported, even on drivers that accept them. Shared mappings of regular files are not checked for. On Windows, an alias of adjacent registered ranges is refused if any page is not a shared view; a range made entirely of shared views is aliased run by run, including across sections or nonconsecutive section offsets. Adjacent registered ranges are joined into one import: the largest aligned power-of-two block, from twice the import alignment up to the 8 MiB window, that holds the requested address and that they fill exactly, so a block only grows by doubling as its window fills. A block that would exceed the budget, would replace an import a build holds, or fails (a refused Windows alias, `vkAllocateMemory`) steps down to the next smaller block; a failed block is not tried again until one of its ranges changes. Ranges larger than the window are imported one at a time. A buffer bound across adjacent ranges that are each larger than the window, or bound over adjacent ranges of any size from the first one's start (ending anywhere in the last range it crosses), joins the ranges it crosses into one import when it is bound, and their own imports are retired; a buffer that crosses any other two imports is copied. A heap grown in pieces re-imports its joined run only when a buffer bound from its first piece's start crosses into a piece the joined import does not cover, or when a sparse descriptor heap over it is bound: a heap a shader binds whole while only the pieces from its base are mapped is bound in place from the import of that run, its views ending at the import's end as the copy's do, once the previous bind of that address saw the same last mapped byte (a heap that grows between every two of its binds stays copied; one bound more often than it grows is joined again after each growth) and at most 64 times per joined run base; past that, with a message, it is not joined again: it is bound in place while an existing import covers its run and copied otherwise, until no registered range starts at that base or the device changes. Ranges larger than the window are taken to be registered whole and never grown in pieces: a title that grows a heap in pieces larger than 8 MiB re-imports the joined run each time a bound buffer crosses into a new piece. The 8 MiB window is a choice: unbounded runs were re-imported whole on every growth of a heap mapped in small pieces, and the NVIDIA driver (610.57.04, 615.78.08) then refused imports or lost the device. Not measured on RADV. - [Cube texture descriptors](../../../core/libs/prx/libSceAgcDriver/Graphics/src/GuestTextureResource.cpp) with equal `BASE_ARRAY` and `DEPTH` expose six faces starting at `BASE_ARRAY`. Measured with `image_load`, `image_sample` and `image_get_resinfo` on RX 6600 (gfx1032), using 64x64 R32_UINT faces and base slices 0, 5 and 6. Other ranges containing fewer than six faces still throw: hardware load bounds and filtered-sample addressing differ. A range of six faces or more that does not end on a whole cube keeps every slice up to `DEPTH`, the bound loads use on hardware; sampling the cube it cuts short is out-of-range cube-array sampling. Out-of-range cube-array sampling remains unimplemented. Samplers with `DISABLE_CUBE_WRAP` are created with `VK_SAMPLER_CREATE_NON_SEAMLESS_CUBE_MAP_BIT_EXT`, the flag RADV maps to that bit (Mesa `src/amd/vulkan/radv_sampler.c`, `src/amd/common/ac_descriptors.c`); without `VK_EXT_non_seamless_cube_map` a cube texture sampled through such a sampler throws. PS5 equivalence has not been measured on console hardware. - [Array layer views](../../../core/libs/prx/libSceAgcDriver/Graphics/src/GuestTextureResource.cpp) (libSceAgcDriver) - `ArrayLayersServeView` lets a 2D texture at a 2D array image's base address, or a 2D array texture over its first layers, view that image (`Texture::CanCopyFrom`). Mesa-built homebrew (ps5-opengl) writes the view's last slice plus one to `CB_COLOR_ATTRIB3.MIP0_DEPTH`, so a slice-0 render target is a two-layer array image, and the title samples it as a plain 2D texture (SuperTuxKart PPSA69421 drew a black 3D scene and minimap without this). The per-layer layout of a mipmapped array is assumed equal to a standalone surface's; it is not measured on the console. A 2D texture at a later slice's address still gets an image of its own. - [Multisampled CMASK](../../../core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp) (libSceAgcDriver) - a multisampled color target's CMASK is decoded under CB_COLOR_INFO FAST_CLEAR (bit 13, as for a single-sample target) or COMPRESSION (bit 14). Which bit a multisampled target sets is inferred: its fast clears were seen on GTA V's multisampled targets through their CMASK fills, not in a register dump. A COMPRESSION-only target's zero CMASK is not read as cleared (inferred: zero means cleared only under FAST_CLEAR), so only a fill after its first use clears it. A slice of a multisampled array is its own image at its own address with its own CB_COLOR0_CMASK (inferred from the register layout). Its CMASK is main's whole-metablock size. - [Compressed FMASK reads](../../../core/libs/prx/libSceAgcDriver/Graphics/src/MultisampleTarget.cpp) without an exact resident native owner throw. Native reads require eight fragments, one slice and mip, pipe-aligned DCC/CMASK, Z_64KB_X FMASK and unchanged, disjoint watched planes. The host stores independent samples and exposes their expanded indices (`0x76543210`, AMD PAL `c5e80007`, `Gfx9Fmask::GetPackedExpandedValue`); console equivalence is unmeasured. Other layouts, standalone compressed decoding, guest publication and recipe reuse remain unsupported. Metadata writes invalidate the native view, including same-value stores. -- [Multisampled CMASK state](../../../core/libs/prx/libSceAgcDriver/Graphics/src/MultisampleTarget.cpp) (libSceAgcDriver) - a multisampled target has no guest texels, so its CMASK state comes from fills rather than from a read at each draw as a single-sample target's does. A fill of the whole CMASK with 0 clears the host image at the target's next use, one with 0xffffffff marks it expanded, and any other pattern, or a fill that covers only part of the CMASK, throws at the next use (inferred: main's rule that a mixed CMASK is unmodeled, applied to the one host image). A FAST_CLEAR target starts from its CMASK keys at its first use, and a COMPRESSION-only target starts expanded. Copies by DMA into the CMASK are not seen, and a later use with another CMASK address throws. The clear does not rewrite the CMASK bytes as expanded, so a new target over the same CMASK (a different extent, so a different image) seeds from them again and clears once more. +- [Multisampled CMASK state](../../../core/libs/prx/libSceAgcDriver/Graphics/src/MultisampleTarget.cpp) (libSceAgcDriver) - a multisampled target has no guest texels, so its CMASK state comes from fills rather than from a read at each draw as a single-sample target's does. A fill of the whole CMASK with 0 clears the host image at the target's next use, one with 0xcccccccc, 0xdddddddd, 0xeeeeeeee or 0xffffffff marks it expanded, and any other pattern, or a fill that covers only part of the CMASK, throws at the next use (inferred: main's rule that a mixed CMASK is unmodeled, applied to the one host image). A FAST_CLEAR target starts from its CMASK keys at its first use, and a COMPRESSION-only target starts expanded. Copies by DMA into the CMASK are not seen, and a later use with another CMASK address throws. The clear does not rewrite the CMASK bytes as expanded, so a new target over the same CMASK (a different extent, so a different image) seeds from them again and clears once more. - [Multisampled DCC CMASK without fast clears](../../../core/libs/prx/libSceAgcDriver/Graphics/src/MultisampleTarget.cpp) (libSceAgcDriver) - the initial CMASK of a multisampled DCC target is not checked when CB_COLOR_INFO enables COMPRESSION but not FAST_CLEAR: it then holds only FMASK compression, and a uniform DCC clear sets every fragment, so the target imports as that clear. What the console reads where such an FMASK names no fragment, as an uninitialized CMASK can, is unverified. - [Multisampled color fills](../../../core/libs/prx/libSceAgcDriver/Graphics/src/MultisampleTarget.cpp) (libSceAgcDriver) - a multisampled target's samples are on the host, so a fill of its color memory is applied at its next use: a uniform fill over the whole of it clears the host image to that value, and a pending CMASK fast clear takes precedence over the fill and over a fill that would throw. Its color memory is a single mip and slice of addrlib's block dimensions for its sample count (ComputeThinBlockDimension). A fill over part of it, a fill reaching the start of a target whose footprint is not computed (linear), and a fill value with no exact clear value in the format (sRGB channels other than 0 and 1) throw at the next use. A fill before the target's first use is not seen (the host image starts at 0), nor are copies or shader writes into its color memory. A uniform compute fill over exactly the whole color memory with an exact clear value is not stored into guest memory, since only the host image holds the samples. A target is retired only when its whole guarded range is unmapped or its allocation removed, so if a title reuses that memory for other data while it stays mapped, a read the guard does not check (a buffer read through a V# or a pointer) sees the bytes from before the fill. -- [Multisampled texture no draw rendered to](../../../core/libs/prx/libSceAgcDriver/Graphics/src/MultisampleTarget.cpp) (libSceAgcDriver) - sampling a multisampled texture whose surface no draw rendered to creates the host target from the T# and clears it to the color a single 32-bit value repeated over the whole footprint encodes, padding up to whole tile blocks included. Inferred, not measured on the console: that the samples of such a surface are the guest color memory, as a title that clears its canvas on the CPU (ps5-opengl `ps5_clear_msaa4_color`) relies on. Memory that is not one repeated value, tile modes other than Standard4KB, Standard64KB and RenderTarget, unmapped memory and values with no exact clear color throw. CPU writes to the surface after its host target exists are not seen by the host. +- [Multisampled texture no draw rendered to](../../../core/libs/prx/libSceAgcDriver/Graphics/src/MultisampleTarget.cpp) (libSceAgcDriver) - sampling a multisampled texture whose surface no draw rendered to creates the host target from the T# and clears it to the color a single 32-bit value repeated over the whole footprint encodes, padding up to whole tile blocks included. Inferred, not measured on the console: that the samples of such a surface are the guest color memory, as a title that clears its canvas on the CPU (ps5-opengl `ps5_clear_msaa4_color`) relies on. Memory that is not one repeated value, tile modes other than Standard4KB, Standard64KB, Z64KBX, D64KBX and RenderTarget, unmapped memory and values with no exact clear color throw. CPU writes to the surface after its host target exists are not seen by the host. - [Sample 0 of a multisampled color target](../../../core/libs/prx/libSceAgcDriver/Graphics/src/FirstSampleCopy.cpp) (libSceAgcDriver) - a 2D image read of a multisampled color target returns its float, UNORM, SNORM or sRGB sample 0 exactly only where the driver keeps those values through a shader and a color attachment, which Vulkan does not require, and one of a multisampled depth surface throws. The read binds a one-sample copy that a fragment pass (`texelFetch` of sample 0) draws before every such binding; integer formats copy exactly. - [Multisampled targets in CB passes](../../../core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp) (libSceAgcDriver) - a CB eliminate or DCC pass over a multisampled target throws, since its texels are on the host and only a CB resolve and an FMASK decompress (`CB_COLOR_CONTROL` mode 5) are modeled. The FMASK decompress only applies the pending CMASK fast clear to the host image; it requires a CMASK address, and the target's DCC and FMASK contents are not interpreted. DCC over a multisampled target is accepted only in that pass and throws everywhere else. A CB resolve into a fast-clear destination throws: the resolve writes the destination's texels but not its CMASK, which would still read as cleared. -- [Multisampled target guest access](../../../core/libs/prx/libSceAgcDriver/Graphics/src/MultisampleTarget.cpp) (libSceAgcDriver) - the memory a multisampled color target throws on (CPU read, index buffer read, fixed-function vertex buffer read, DMA copy, texture or storage image read) uses fragment-aware 64 KiB render-target blocks. [AMD Addrlib's GFX10 thin-block rule](https://github.com/GPUOpen-Drivers/pal/blob/c5e800072a32f68b6ccc4422936d96167c6e0728/src/core/imported/addrlib/src/core/addrlib2.cpp#L1680-L1712) gives the block width and height from element bytes and fragment count; its [surface-size calculation](https://github.com/GPUOpen-Drivers/pal/blob/c5e800072a32f68b6ccc4422936d96167c6e0728/src/core/imported/addrlib/src/gfx10/gfx10addrlib.cpp#L4000-L4005) rounds both extents to those blocks and multiplies by element bytes and fragments. Thus a 1920x1080 4x RGBA8 surface occupies 0x1fe0000 bytes, leaving the next allocation at +0x2000000 outside the guard. The 2x and 8x block orientations differ from treating the fragments as a larger element. Console address-layout equivalence has not been measured. Other tile modes retain the conservative `max(CB bytes * samples, width * height * element bytes * samples)` bound, which may still reject a distinct allocation. Live targets remain cached per device, surface address, format, extent and sample count. Retired cache owners are removed on the next target, sampled or storage lookup; outstanding recorded work and sampled views retain their images. Ordinary sampled and storage bindings check the color-target guard before resource reuse, including precollected sampled lookups. Reused image tables do not invalidate on multisample color-target registration yet; their per-element fault handling and native multisample sampling require a separate reuse proof. -- [DB depth copies](../../../core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp) (libSceAgcDriver) - a draw with `DB_RENDER_CONTROL.DEPTH_COPY` is run as a compute pass that copies one sample of the `Z_32_FLOAT` depth surface into a single-sample R32 float color target over the whole surface. With `COPY_CENTROID` the sample is the first one the draw covers, which is sample 0 under the full `PA_SC_AA_MASK` the driver requires: [radeonsi copies one sample per draw with `COPY_CENTROID` and a one-sample mask](https://gitlab.freedesktop.org/mesa/mesa/-/blob/mesa-24.0.0/src/gallium/drivers/radeonsi/si_state.c#L1679-1681); the console is not measured. Stencil copies, depth tests, other depth formats, multisampled or other-format targets and copies over part of the surface throw. +- [Multisampled target guest access](../../../core/libs/prx/libSceAgcDriver/Graphics/src/MultisampleTarget.cpp) (libSceAgcDriver) - the memory a multisampled color target throws on (CPU read, index buffer read, fixed-function vertex buffer read, DMA copy, texture or storage image read) uses fragment-aware 64 KiB blocks for the RenderTarget, Z64KBX and D64KBX tile modes. [AMD Addrlib's GFX10 thin-block rule](https://github.com/GPUOpen-Drivers/pal/blob/c5e800072a32f68b6ccc4422936d96167c6e0728/src/core/imported/addrlib/src/core/addrlib2.cpp#L1680-L1712) gives the block width and height from element bytes and fragment count; its [surface-size calculation](https://github.com/GPUOpen-Drivers/pal/blob/c5e800072a32f68b6ccc4422936d96167c6e0728/src/core/imported/addrlib/src/gfx10/gfx10addrlib.cpp#L4000-L4005) rounds both extents to those blocks and multiplies by element bytes and fragments. Thus a 1920x1080 4x RGBA8 surface occupies 0x1fe0000 bytes, leaving the next allocation at +0x2000000 outside the guard. The 2x and 8x block orientations differ from treating the fragments as a larger element. Console address-layout equivalence has not been measured. Other tile modes retain the conservative `max(CB bytes * samples, width * height * element bytes * samples)` bound, which may still reject a distinct allocation. Live targets remain cached per device, surface address, format, extent and sample count. Retired cache owners are removed on the next target, sampled or storage lookup; outstanding recorded work and sampled views retain their images. Ordinary sampled and storage bindings check the color-target guard before resource reuse, including precollected sampled lookups. Reused image tables do not invalidate on multisample color-target registration yet; their per-element fault handling and native multisample sampling require a separate reuse proof. +- [DB depth copies](../../../core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawPrecheck.cpp) (libSceAgcDriver) - with `COPY_CENTROID` the copied sample is sample 0, as [radeonsi](https://gitlab.freedesktop.org/mesa/mesa/-/blob/mesa-24.0.0/src/gallium/drivers/radeonsi/si_state.c#L1679-1681) programs it under a full sample mask, so an edge pixel whose sample 0 the draw doesn't cover can differ from the console's first covered sample; not measured on the console. The copy runs as the title's draw with a driver-authored guest pixel program (hand-encoded RDNA2) that loads the sample through a driver-built T# and exports it as `32_R`. A multisampled plane is read without the native multisample import checks. Stencil copies, depth tests, planes other than `Z_32_FLOAT`, mips, a partial sample mask, blending, indirect draws and other targets throw. - [CB resolve sample count](../../../core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp) (libSceAgcDriver) - a draw with CB_COLOR_CONTROL in resolve mode is a CB resolve when either PA_SC_AA_CONFIG or CB_COLOR0_ATTRIB.NUM_SAMPLES is multisampled, and takes its sample count from CB0. Inferred, not from documentation: PPSA36082 resolves its 4x targets with no sample count in PA_SC_AA_CONFIG, and radeonsi sets PA_SC_AA_CONFIG to 0 for a multisampled framebuffer when rasterizer multisampling and line smoothing are off. PA_SC_AA_CONFIG is only compared with CB0 while PA_SC_MODE_CNTL_0.MSAA_ENABLE is set: PPSA08270 resolves a 4x 512x512 target with MSAA_ENABLE off and PA_SC_AA_CONFIG still at 2x (0x00108001) from earlier draws. - [Custom MSAA sample locations](../../../core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp) (libSceAgcDriver) support a uniform 1x1 pattern for 2, 4 or 8 samples through VK_EXT_sample_locations. Patterns varying across the guest's 2x2 pixel grid use a 2x2 sample location grid, anchored at the framebuffer origin like the guest's while the window offset is zero, on devices whose `maxSampleLocationGridSize` for the sample count is a multiple of 2x2. On other devices every pixel takes the pattern's pixel (0,0) locations (`PA_SC_AA_SAMPLE_LOCS_PIXEL_X0Y0_*`, its first registers and the corner the grid is anchored at), and the first such pipeline of each sample count logs `[gpu] no 2x2 sample location grid`. The RTX 5070 reports 2x4 for 2 samples, 2x2 for 4 and 1x2 for 8; an AMD Radeon integrated GPU reports 2x2 for each count, per count and for every count; an RTX 3070 Laptop GPU (driver 617.42) reports `maxSampleLocationGridSize` 1x1 for every count (`VkPhysicalDeviceSampleLocationsPropertiesEXT`), but per count (`VkMultisamplePropertiesEXT`, which the device setup reads and logs) a 2x4 grid at 2 samples, 2x2 at 4 and 1x2 at 8. So the 3070's 2x patterns, the 2x checkerboard among them, take the native grid, and the stand-in plus the exact path serve its 8-sample patterns and devices without a grid. A pattern that only reorders one set of positions between pixels, as a 2x checkerboard pattern puts its samples at x = +4/16 and -4/16 and swaps them on odd rows, is drawn exactly through that stand-in: before every render pass that draws with it each pixel's samples are put in the stand-in's order and after it back in the guest's, through a storage view of a multisampled color target and through a copy and a per-sample pass for depth (writing stencil through VK_EXT_shader_stencil_export, or one pass per bit without it), while the pixel shader reads SampleID, applies the sample mask and exports coverage in the guest's order, and the second log line names the reordering. A uniform partial sample mask that the order moves is applied in the pixel shader. With early Z, a depth or stencil test and memory writes in the pixel program, the draw throws, where #6451's stand-in alone ran it with odd rows' sample indices swapped. A fix would choose the stand-in after compilation. That costs two passes over each multisampled attachment per render pass, and on those devices every multisampled color target is created with a mutable format and storage usage for its view, which a driver may answer by not compressing it. It needs shaderStorageImageMultisample with a storage view of the color format at the sample count, sampleRateShading, and a depth format a FragDepth write gives back exactly (16-bit, or 32-bit float; not 24-bit depth, the stand-in for 16-bit depth with stencil where the device has neither); without them, and for patterns whose pixels hold different positions, the stand-in alone is used, and the log line says why. A device without shaderStorageImageMultisample takes the stand-in alone for every pass, depth-only passes included, so a depth target that a depth-only pass and a color pass both draw keeps one order. On a device that has it, a pass is still refused for a color format without a storage view at its sample count, while a depth-only pass on the same depth target is reordered, so that target's samples change order between those passes; a fix would decide the order per target. Then the top-left pixel of every 2x2 quad matches the console and the other three use its positions. A same-set pattern keeps every position and the number of samples a primitive covers in each pixel, so a resolve that averages a pixel's samples, or takes their minimum or maximum, sees the console's values, but sample i of an odd row holds what the console stores in sample 1 - i: a pass that reads the samples by index, as checkerboard reconstruction does, exchanges the two half-pixels of odd rows, which can show as combing along vertical edges, and per-sample shading, sample-mask exports and coverage inputs see the swapped indices too. A pattern whose pixels hold different positions draws pixel (0,0)'s positions in each pixel whose own differ. A device's pipelines and render pass begins take the same stand-in for a given pattern, so a sample-location-compatible depth image is not drawn with one set of locations and transitioned or tested with another. A multisampled rasterizer drawing into one-sample color targets, and a one-sample depth target if any, with MSAA_ENABLE set, no alpha-to-coverage and no coverage pixel input is drawn with one sample at sample 0's location in each pixel of the grid, as the targets keep only sample 0's coverage. Without PS_ITER_SAMPLE such a draw also refuses centroid inputs, and keeps PERSP_SAMPLE, LINEAR_SAMPLE and ANCILLARY: a pixel shaded once is taken to get sample 0's barycentrics and sample index 0. With PS_ITER_SAMPLE only sample 0's invocation reaches the targets, so its sample and centroid barycentrics are taken to be sample 0's and the draw is shaded once. Both are inferred, not measured. The pixel shader runs once per pixel with sample number 0, so ANCILLARY reads sample 0 and POS_X_FLOAT and POS_Y_FLOAT keep the position `SPI_BARYC_CNTL` names. PERSP_CENTER and LINEAR_CENTER inputs on this path are interpolated at sample 0's location on the host, where the console uses the pixel center; one-sample depth images are created with `SAMPLE_LOCATIONS_COMPATIBLE_DEPTH` when the device supports it, as multisampled ones already were; this is how VK_AMD_mixed_attachment_samples defines the case, and it is not measured on PS5 hardware. Other mixed sample counts throw. Devices without the required sample count, four subpixel bits or the full guest coordinate range refuse custom patterns. Depth images remain in GENERAL between draws, and render passes cannot continue across pattern changes. A pass that draws depth at custom sample locations ends with a transition to TRANSFER_SRC_OPTIMAL and back that names them. Guest offsets retain the existing decoder's signed four-bit, sixteenth-pixel interpretation; PS5 coverage and interpolation equivalence have not been measured on hardware. - [Single-sample scan into a multisampled depth target](../../../core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp) (libSceAgcDriver) - a draw with `PA_SC_AA_CONFIG.MSAA_NUM_SAMPLES` = 0 and `PA_SC_MODE_CNTL_0.MSAA_ENABLE` clear into a multisampled depth target rasterizes the depth target's sample count with every sample at the pixel center, as a single-sample scan into a multisampled color target already does. radeonsi programs this `MSAA_ENABLE` and `PA_SC_AA_CONFIG` with the depth buffer's own sample count when GL multisampling is disabled on a multisampled framebuffer ([L1066-L1068](https://github.com/openbsd/xenocara/blob/1b39033df8d928ec247ca64448c4a63c72302db7/lib/mesa/src/gallium/drivers/radeonsi/si_state.c#L1066-L1068), [L3485-L3512](https://github.com/openbsd/xenocara/blob/1b39033df8d928ec247ca64448c4a63c72302db7/lib/mesa/src/gallium/drivers/radeonsi/si_state.c#L3485-L3512)), although its comment lists Z samples as at most the coverage samples ([L3439](https://github.com/openbsd/xenocara/blob/1b39033df8d928ec247ca64448c4a63c72302db7/lib/mesa/src/gallium/drivers/radeonsi/si_state.c#L3439)) and it sets `DB_EQAA.MAX_ANCHOR_SAMPLES` to the Z sample count, where PPSA18216 sets `DB_EQAA` to 0. GL then lets every sample take the single-sample depth ([L185-L190](https://github.com/KhronosGroup/OpenGL-Registry/blob/6af574a14089ccfee87efe230ebcdd8742859813/extensions/ARB/ARB_multisample.txt#L185-L190)) and the fragment be treated as in multisampling with full coverage ([L422-L425](https://github.com/KhronosGroup/OpenGL-Registry/blob/6af574a14089ccfee87efe230ebcdd8742859813/extensions/ARB/ARB_multisample.txt#L422-L425)), which is what this draws: each depth and stencil sample is tested and written with the depth at the pixel center. GL also lets an implementation test only a centermost sample and set every sample from it ([L425-L434](https://github.com/KhronosGroup/OpenGL-Registry/blob/6af574a14089ccfee87efe230ebcdd8742859813/extensions/ARB/ARB_multisample.txt#L425-L434)), which differs when a pixel's stored samples differ; which one the console does, whether it instead evaluates the depth plane at each sample position, and how its occlusion queries count such a draw, is not measured on hardware. Points, lines, alpha-to-coverage, `DB_EQAA` Z interpolation or overrasterization, a pixel program reading the sample index and a sample-mask export throw @@ -198,6 +205,7 @@ - [COPY_DATA from a register](../../../core/libs/prx/libSceAgcDriver/Execution/src/Pm4.cpp) (libSceAgcDriver, `SRC_SEL` 0) copies the value the queue holds for a shader, context or user config register (`COUNT_SEL` two consecutive ones) and throws for a register with no value. A dispatch records `COMPUTE_DISPATCH_INITIATOR` and, for `DISPATCH_DIRECT`, `COMPUTE_DIM_X/Y/Z` in the shader registers; `DISPATCH_INDIRECT` clears the three counts. Neither the register values the CP writes nor the copy's ordering against in-flight dispatches have been measured on hardware. - [EVENT_WRITE stream-out statistics events](../../../core/libs/prx/libSceAgcDriver/Execution/src/Pm4.cpp) (libSceAgcDriver, opcode 0x46) - `SAMPLE_STREAMOUTSTATS1`-`3` and `SAMPLE_STREAMOUTSTATS` (event types 0x01-0x03 and 0x20 in [navi10_enum.h](https://github.com/torvalds/linux/blob/0fcb05b72344f40806cbe810de8872f1698bf053/drivers/gpu/drm/amd/include/navi10_enum.h#L14155-L14188)) are accepted on the graphics queue as a 2-dword packet with event index 0 and do nothing. [nvd.h](https://github.com/torvalds/linux/blob/0fcb05b72344f40806cbe810de8872f1698bf053/drivers/gpu/drm/amd/amdgpu/nvd.h#L326-L347) documents these events only with the sampling index (3, or `EVENT_INDEX__SAMPLE_STREAMOUTSTATS*` 8-11 on gfx10); the index-0 packet has no destination dwords, and what the CP does with it is neither documented nor measured on hardware. AnyPS5 does not emulate the VGT stream-out counters, so there is nothing to sample. The sampling indices throw that sampling to memory is not implemented, and other indices, other sizes and the compute queue throw. [sceAgcDcbEventWrite](../../../core/libs/prx/libSceAgc/DcbFlow/src/Sync.cpp) ignores the address for every event type except 0x38 and 0x39, so a call with one of these event types and an address builds the index-0 packet: nothing is written and nothing throws. Whether the console's builder emits a sampling packet for an address is not known. - [INDEX_ATTRIBUTES_INDIRECT](../../../core/libs/prx/libSceAgcDriver/Execution/src/Pm4.cpp) (libSceAgcDriver, opcode 0x91) loads INDEX_BASE, INDEX_BUFFER_SIZE (in indices) and INDEX_TYPE from the 16-byte record at `attribute_base + attribute_index * 16`, read when the packet runs. The record layout (address low, address high, size, type) is PAL's [`IndexAttribIndirect`](https://github.com/GPUOpen-Drivers/pal/blob/c5e800072a32f68b6ccc4422936d96167c6e0728/src/core/hw/gfxip/gfx9/gfx9Chip.h#L60-L67) and the packet is the one [`BuildIndexAttributesIndirect`](https://github.com/GPUOpen-Drivers/pal/blob/c5e800072a32f68b6ccc4422936d96167c6e0728/src/core/hw/gfxip/gfx9/gfx9CmdUtil.cpp#L2586-L2613) writes. The console's `sceAgcDcbSetIndexIndirectArgs` writes this packet, with `indirect_mode` 0, the attribute base in the address dwords and the attribute index in bits 0-15 ([measured](https://github.com/mattias800/prosper/blob/3ea5cb9a846b6dcd00f5073360462bbda4f72288/prosper/tests/data/console_oracle/agcbuild.golden.tsv#L172-L174) by the prosper console oracle); the record layout is not measured on a PS5, and neither is seen in a title. `indirect_mode` 1 (`addr_offset`, which only PAL's ExecuteIndirect path emits) throws, and so does a record whose index type is above 3, as INDEX_TYPE does. +- [Indexed indirect draws without an index limit](../../../core/libs/prx/libSceAgcDriver/Execution/src/Pm4.cpp) (libSceAgcDriver, opcodes 0x25 and 0x38) - on a queue that has received neither INDEX_BUFFER_SIZE (0x13) nor INDEX_ATTRIBUTES_INDIRECT (0x91) since its last reset, the draw has no index limit: it takes the CPU record path ([IndirectDraw.cpp](../../../core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/IndirectDraw.cpp)), and [Draw.cpp](../../../core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/Draw.cpp) reads each record's firstIndex and indexCount range as written, without clamping, and refuses a range that overflows or is not readable. An INDEX_BUFFER_SIZE of 0 that was written still throws. The console's initial INDEX_BUFFER_SIZE and whether DRAW_INDEX_2 or DRAW_INDEX_OFFSET_2 MAX_SIZE changes it are not measured. The suspend-point queue reset also clears a written size. PPSA08338 issues DRAW_INDEX_INDIRECT_MULTI this way without ever emitting either packet. - [16-bit RGBA color targets](../../../core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp) (libSceAgcDriver) support the standard and the alternate (`COMP_SWAP` 1) component order. Alternate swaps red and blue in shader exports, write masks and constant-color blend factors, following `ac_translate_colorswap` in [Mesa 26.2.2 ac_formats.c](https://gitlab.freedesktop.org/mesa/mesa/-/blob/mesa-26.2.2/src/amd/common/ac_formats.c). Blending into the reverse orders (`COMP_SWAP` 2 and 3) is covered by the reversed-order blending entry, and simultaneous targets requiring conflicting blend constants throw. Console equivalence has not been measured on hardware. - [UINT16_ABGR color exports](../../../core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp) (libSceAgcDriver) - export format 7 is accepted only into an unsigned integer target, and each 16-bit value reaches the target unchanged; a value beyond a narrower target's range (`R8_UINT`) is undefined in Vulkan, and whether the console clamps or truncates it is not confirmed. SINT16_ABGR (8) throws - [Layered color targets](../../../core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp) (libSceAgcDriver) - 3D color targets with 64 KiB render-target or 4 KiB standard tiling render several slices, and so does a `CB_COLOR_VIEW` slice range on a 2D array target through the array's resident image, or on a surface of one slice as an array of the view's last slice + 1 slices, one slice pitch apart: a layered draw into an array without its resident image, a layered draw with a depth target, and color targets of different slice counts throw. The vertex layer export (`PA_CL_VS_OUT_CNTL` USE_VTX_RENDER_TARGET_INDX) reaches the shader only for a layered draw and is otherwise ignored, as before; a layer beyond the view's last slice is undefined in Vulkan, while the console's handling (clamping to `SLICE_MAX`) is not modeled, and a view whose last slice lies past the surface is clamped to its last slice. DCC keys of a 3D color target are taken as one key per 256 bytes over the whole surface, every slice included, and only uniform keys (all uncompressed, or one fast-clear code) are interpreted, as for 2D targets; mixed keys leave the stored texels as they are. The console's DCC key layout for 3D surfaces is not checked (LEGO Horizon Adventures' (PPSA14632) lighting volumes are DCC) @@ -225,7 +233,7 @@ - [DB_RENDER_CONTROL resummarize draws](../../../core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp) (libSceAgcDriver) - HTILE is not modeled, so a RESUMMARIZE_ENABLE draw without depth or stencil tests and without color writes changes no modeled state and draws nothing. A pure resummarize with pass-through D16/D32 depth, no depth writes or bounds test, no stencil plane, no color writes and no running pixel stage also draws nothing; other resummarize, copy or decompress draws throw. - [Comparison-sampled R32 float and R16 unorm textures](../../../core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp) (libSceAgcDriver) whose surface a storage image holds are sampled from a D32_SFLOAT or D16_UNORM image that the GPU refills from the storage image whenever its version moves, because Vulkan compares only depth formats. Only surfaces of at least 1 MiB (`APS5_DEPTH_COPY_MIN_KIB`) that are not 3D take the copy; smaller ones stay CPU snapshots compared against guest memory. With copies of small surfaces, one title hit an intermittent boot-time crash that was not explained. - [Null vertex buffer V#](../../../core/libs/prx/libSceAgcDriver/Graphics/include/VertexInput.hpp) (libSceAgcDriver) - a V# whose four dwords are all zero is bound to a zeroed vertex buffer of the attribute's size with stride 0, so every component of its attribute reads 0. This follows the V# fields in the [AMD RDNA2 ISA](https://gpuopen.com/download/rdna2-shader-instruction-set-architecture.pdf) (`NUM_RECORDS` 0, `DST_SEL` 0 selects 0), not measured on hardware. -- [Descriptor pool recycling](../../../core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp) avoids individually freed sets: [RADV 26.2.2](https://gitlab.freedesktop.org/mesa/mesa/-/blob/mesa-26.2.2/src/amd/vulkan/radv_descriptor_pool.c) initializes its free-set heap with 32 bytes beyond the backing allocation. An allocation-only RX 6600 test reproduces acceptance of a 4,128-byte set in a 4,096-byte pool; the path without individual frees rejects it. Pools reset after their last set is released. A cached set can retain unused slots in an exhausted pool until eviction; peak gameplay pool memory remains unmeasured. +- [Descriptor pool recycling](../../../core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp) frees sets individually only on lavapipe: [RADV 26.2.2](https://gitlab.freedesktop.org/mesa/mesa/-/blob/mesa-26.2.2/src/amd/vulkan/radv_descriptor_pool.c) initializes its free-set heap with 32 bytes beyond the backing allocation. An allocation-only RX 6600 test reproduces acceptance of a 4,128-byte set in a 4,096-byte pool; the reset-only path rejects it. Pools reset after their last set is released. A cached set can retain unused slots in an exhausted pool until eviction; peak gameplay pool memory remains unmeasured. - [16-bit depth with stencil](../../../core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp) (libSceAgcDriver) is stored as `D24_UNORM_S8_UINT`, or `D32_SFLOAT_S8_UINT`, on a device without `D16_UNORM_S8_UINT` as an attachment. Depth comparisons, clears and the depth image see the wider format's precision, so results at the 16-bit quantization boundary can differ from the console; the constant depth bias is scaled by 256 for D24, and a non-zero constant on the D32 float fallback throws. Not measured on console hardware. - [Depth surfaces created over storage writes](../../../core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp) (libSceAgcDriver): a depth surface created without HTILE copies its depth plane from the storage image at the plane's address, a pending one first, when that image has the plane's extent, one level, a 2D view or a 2D array view (its layer 0 is copied) and a 16-bit format for a 16-bit plane or a 32-bit format for a 32-bit plane. A multisampled surface, a surface the device stores in another format, a surface with HTILE and a surface without such an image start from `DB_DEPTH_CLEAR`, and the stencil plane from `DB_STENCIL_CLEAR`; CPU writes to the plane's memory are not read. Not measured on the console, where the plane is that memory. - [Flip packets raise the graphics end-of-pipe event](../../../core/libs/prx/libSceAgcDriver/Execution/src/Driver/Packets/PacketExecution.cpp) (libSceAgcDriver) - a flip delivers the queue's end-of-pipe event (`sceAgcDriverAddEqEvent`) once the work recorded before it completes, as a `RELEASE_MEM` with `INT_SEL` does, but only on a queue that has never delivered an interrupt from such a release: the queue is latched the first time a release interrupt reaches the event queue, and flips on that queue deliver nothing while its own release interrupts keep arriving. The latch is cleared when the queue has flipped five times without another interrupt from its own releases: the fifth flip delivers the event and unlatched the queue, so a title that waits on the flip event after latching recovers only if it submits further flips. The bound cannot be by time: the next release interrupt can arrive seconds later than a suppressed flip across loading stalls, and delivering an unexpected flip event on a queue that pairs flips with releases can crash; no fixed window short enough to unblock a waiting title keeps such a title alive. Whether the console raises the event from the flip itself or from an interrupt its flip sequence contains is not measured; nor is how the console behaves for a title that waits on the flip event and also raises release interrupts on the same queue. diff --git a/docs/dev/debt/libSceAjm.native.md b/docs/dev/debt/libSceAjm.native.md index 5fd76482c4..fa1c55df74 100644 --- a/docs/dev/debt/libSceAjm.native.md +++ b/docs/dev/debt/libSceAjm.native.md @@ -3,7 +3,7 @@ ### Unknown function info - [sceAjmBatchGetLog, sceAjmInstanceAvailable](../../../core/libs/prx/libSceAjm.native/Export.cpp) (libSceAjm.native) - names and NIDs are present in the [PS5 3.20 export inventory](https://github.com/DNNDHH/PS5-3.20_Libs/blob/b2a77c09cd71c01f34c790cfbe98c751a13262ad/libSceAjm.c), but no credible prototypes were found. They are declared without parameters solely to resolve known imports; this is not an ABI claim. -- [sceAjmInstanceExtend, sceAjmInstanceSwitch](../../../core/libs/prx/libSceAjm.native/Export.cpp) (libSceAjm.native) - their signatures follow [shadPS4](https://github.com/shadps4-emu/shadPS4/blob/f68923fe807f657348e6719ce658bcbd0a2b1c72/src/core/libraries/ajm/ajm.h#L19-L20) and [SharpProspero](https://github.com/SvenGDK/SharpProspero/blob/5dfdb4b0b166e83dc9ae275001afb695f12c7174/src/SharpProspero/Interop/Audio/Ajm.cs#L100-L104), not measured on a console; these throwing declarations only resolve imports. +- [sceAjmInstanceExtend, sceAjmInstanceSwitch](../../../core/libs/prx/libSceAjm.native/Export.cpp) (libSceAjm.native) - their four-argument signatures come only from [SharpProspero](https://github.com/SvenGDK/SharpProspero/blob/5dfdb4b0b166e83dc9ae275001afb695f12c7174/src/SharpProspero/Interop/Audio/Ajm.cs#L98-L104), whose comments do not describe them; shadPS4 [declares both without parameters](https://github.com/shadps4-emu/shadPS4/blob/15abf68c9b52c4b04d094f2837719fdb871021f3/src/core/libraries/ajm/ajm.h#L222-L223). Neither is measured on a console; these throwing declarations only resolve imports. - [sceAjmInstanceCreate](../../../core/libs/prx/libSceAjm.native/src/Ajm.cpp) (libSceAjm.native) - a full 14-bit instance slot namespace returns `0x80930007` (`OUT_OF_RESOURCES`), as [shadPS4's InstanceCreate](https://github.com/shadps4-emu/shadPS4/blob/15abf68c9b52c4b04d094f2837719fdb871021f3/src/core/libraries/ajm/ajm_context.cpp) does for its own instance pool (PS4). The 16384 slots per codec prefix are this runtime's existing ID layout, not a measured console capacity; the PS5 capacity and exhaustion result are unverified. - [ATRAC9 vibration decoding](../../../core/libs/prx/libSceAjm.native/src/Ajm.cpp) (libSceAjm.native): channel configurations 6 (mono) and 7 (dual mono) use the pinned [vgmstream decoder](https://github.com/vgmstream/vgmstream/blob/7c520e8f9b078dfde2f84f1b5bcbad0c6c415806/src/coding/libs/atrac9_dec.c), including its 12/24/48 kHz profile limits. Synthetic fixtures cover interleaving, reset after malformed input, partial superframes and PCM16/PCM32/float output; console PCM rounding and physical haptic output are unverified. Ordinary channel configurations and discrete multichannel streams still use LibAtrac9. Decoder creation and decoding run under the existing AJM instance lock because the dependency rebuilds shared Huffman tables during initialization. - [sceAjmBatchJobGetInfo](../../../core/libs/prx/libSceAjm.native/src/Ajm.cpp) (libSceAjm.native) - writes the result and format sideband of a run job without buffers with the `SIDEBAND_FORMAT` flag (0x400000000000), as [KytyPS5](https://github.com/KytyPS5/KytyPS5) does, not confirmed on a PS5 title; the format reports bitrate 0 for ATRAC9 as in KytyPS5, while [shadPS4](https://github.com/shadps4-emu/shadPS4) derives it from the superframe size; before the instance is initialized it returns the not-initialized result (1) and no format, like the other run jobs here, while KytyPS5 returns result 0 with the format of the uninitialized decoder diff --git a/docs/dev/debt/libSceAppInstUtil.md b/docs/dev/debt/libSceAppInstUtil.md index 92d88fb4a6..28ed171a11 100644 --- a/docs/dev/debt/libSceAppInstUtil.md +++ b/docs/dev/debt/libSceAppInstUtil.md @@ -5,3 +5,101 @@ - [sceAppInstUtilAppInstallPkg](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) - the package path is followed by an opaque 64-bit value sent to the install service; its meaning and pointer-versus-integer type remain unresolved. - [sceAppInstUtilGetInstallProgressInfo, sceAppInstUtilGetInstallProgress and sceAppInstUtilGetPrimaryAppSlot](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) - take a title-id string and an output pointer. The IPC replies contain 48, 4 and 4 bytes respectively; callers and reply producers are needed to establish the field types. - [sceAppInstUtilAppPrepareOverwritePkg, sceAppInstUtilAppRecoverApp and sceAppInstUtilAppUnInstallTheme](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) - absent from the supplied system module and from the supplied decompiled callers; the old export inventory establishes names but not prototypes. +- [sceAppInstUtilAppGetAppInstallStatusByAppInfo](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilAppUnInstall2](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilAppUnInstallForRemote](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilAppUnInstallForRemote2](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilAppGetAddcontListTotalSize](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilAppGetAddcontInfo](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilAppGetAddcontInfoByContentId](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilGetAddcontInstalledStatus](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilAppGetInsertedDiscTitleId](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilAppUpdateStartTime](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilAppInstallSharePlayLink](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilAppInstallSharePlayLinkByUser](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilAppGetAddcontNum](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilAppCancelUnInstall](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilAppDestroyMetadata](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilCheckAppSystemVer](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilAppConvertAppDownloadToDisc](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilGetMainAppTitleIdForSP](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilGetSubAppTitleIdForSP](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilGetMainAppTitleId](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilGetSubAppTitleId](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilAppGetAppInstallStatus](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilAppDestroyPkg](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilJson](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilGetInsertedDiscRoleType](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilRequestInstallDiscGame](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilRequestDataDiscCopy](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilCancelDataDiscCopy](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilGetDataDiscCopyProgress](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilSetPrimaryAppSlot](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilGetAppSlotInfoList](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilGetAppEmptySlot](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilAppInstallGetLinkState](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilAppUnInstallForMultiInstall](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilAppInstallPs4CloudGame](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilAppStoreCompilationDiscInfo](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilAppGetCompilationDiscInfo](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilAppGetStoreCompilationDiscInfo](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilAppGetInsertedDiscTitleIdList](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilAppInstallRequestCompilationDiscApp](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilAppGetNoRelatedAddcontTitleList](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilAppGetIrremovableAppList](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilAppRequestMoveApps](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilAppProhibitDownloadInstall](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilAppAllowDownloadInstall](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilGetAppMoveProgressInfo](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilAppCancelMoveApps](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilAppGetMoveErrorAppList](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilAppGetAllAppSize](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilAppCancelGetAllAppSize](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilAppGetInsertedDiscMasterDataId](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilAppGetAppOtherSize](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilAppCancelGetAppOtherSize](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilGetInsertedDiscTotal](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilAppSetRemasterInstallType](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilGetInstallProgressInfoForMultiDisc](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilMountBdEmu](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilUnmountBdEmu](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilGetInstallStatus](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilResumeInstall](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilPauseInstall](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilRegisterDownload](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilInstall](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilStartInstall](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilInstallPatch](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilGetPatchInstallStatus](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilResumePatchInstall](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilPausePatchInstall](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilCancelInstallPatch](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilCancelInstallAddcont](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilCancelInstall](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilCancelForMultiInstall](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilInstallByPackage](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilIsRestoreState](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilGetRestoreState](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilGetAddons](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilGetInstallOptions](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilGetContentSize](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilCancelGetInformation](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilGetWorkspaceStatus](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilDeleteWorkspace](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilGetParentalControlLevel](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilCheckLatestPatch](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilCancelCheckPatch](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilAppInstallPsNowIcon](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilGetAddcontInfoList2](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilGetAddcontInfoList3](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilClearAddcontInfoCache](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilGetContentSize2](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilAppInstallContentPush](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilGetAvailableStorage](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilSwitchInstallLocation](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilGetConfigList](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilSwitchConfig](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilRestoreDataSource](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilGetNeededStorageSize](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilAppGetStorageDestType](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature +- [sceAppInstUtilInstallAddons](../../../core/libs/prx/libSceAppInstUtil/Export.cpp) (libSceAppInstUtil) - unknown signature diff --git a/docs/dev/debt/libSceAudioOut.md b/docs/dev/debt/libSceAudioOut.md index 83e83145fa..edab686a12 100644 --- a/docs/dev/debt/libSceAudioOut.md +++ b/docs/dev/debt/libSceAudioOut.md @@ -3,6 +3,7 @@ ### Silent stubs - [sceAudioOut2LoTerminate](../../../core/libs/prx/libSceAudioOut/src/AudioOut2Low.cpp) (libSceAudioOut) - returns 0, as the PS5 12.00 module (0x248d0) does whether or not the low-latency service runs. The module disconnects from the audio server and clears the service tables; here `sceAudioOut2LoInit` starts no service and open contexts and ports are left as they are +- [sceAudioOutMasteringTerm](../../../core/libs/prx/libSceAudioOut/src/AudioOut2Mastering.cpp) (libSceAudioOut) - returns 0 without doing anything: there is no mastering stage to tear down, and `sceAudioOutMasteringInit` keeps no state. Whether the console fails a Term without a matching Init is unknown ### Unknown function info @@ -12,7 +13,7 @@ - [sceAudioOutExOpen, sceAudioOutExConfigureOutput, sceAudioOutExClose](../../../core/libs/prx/libSceAudioOut/src/AudioOut.cpp) (libSceAudioOut) - the HDMI bitstream calls follow [PS5-Audio-Passthrough-Research](https://github.com/sainsaji/PS5-Audio-Passthrough-Research/blob/cf3c921d79bf00db4847f31b68a7bd95a8037cd7/docs/FINDINGS.md#L31-L80), which disassembles firmware 12.70's libSceAudioOut and logs the calls on a console. `sceAudioOutExOpen(user, mode)` returns `0x80260015` for a mode above 10 or outside mask `0x61F`; otherwise it opens a port of internal type 6 in S16 stereo, 256 frames at 48000 Hz for modes 0, 1, 2 and 9 and 1024 frames at 192000 Hz for modes 3, 4 and 10. `sceAudioOutExConfigureOutput(control, flags, mode, target, option)` returns `0x80268009` when `control` is not 0, takes modes 0 to 10 and 0xFF and targets 1 to 3 and 0xFF, and returned 0 on the console; `sceAudioOutExClose` returned 0. The argument registers match [Jelly5](https://github.com/02dnot/Jelly5/blob/552b866d89b2a58b27f7f7b0484dc743ce235526/app/src/jelly5_bitstream.cpp#L26-L28) and PPSA99051's [bitstream_out.cpp](https://github.com/sainsaji/PS5-Audio-Passthrough-Research/blob/cf3c921d79bf00db4847f31b68a7bd95a8037cd7/sample-app/src/platform/ps5/bitstream_out.cpp#L29-L32). Audio reaches the host through SDL as PCM and the host has no HDMI bitstream output, so an Ex port has no host device: `sceAudioOutOutput` paces it at its own rate and drops its IEC 61937 bursts, and `sceAudioOutExConfigureOutput` changes nothing. Nonzero `flags` or `option`, other modes or targets, `sceAudioOutExClose` of a handle that is not an Ex port, and `sceAudioOutGetPortState` on an Ex port throw, since the console's answers are unknown - [sceAudioOutExGetMonitorInfo](../../../core/libs/prx/libSceAudioOut/src/AudioOut.cpp) - native validation requires first argument zero, a non-null buffer and exactly 1024 bytes, in that order (errors 0x80260003/04/06); the second argument is ignored. The host still reports no audio descriptors by zeroing the record. Native content comes from monitor/device services and system audio configuration, which the SDL backend does not reproduce. - [sceAudioOut2Set3DLatency](../../../core/libs/prx/libSceAudioOut/src/AudioOut2System.cpp) (libSceAudioOut) - the two 32-bit arguments and selector validation match the system library: selector 0 accepts outputs 0 through 2; selector 255 accepts outputs 1 and 2. No third latency argument exists. The host has no system 3D rendering stage, so the call still does not apply latency and does not reproduce the system initialization and output-availability checks. -- [sceAudioOut2MasteringInit and sceAudioOut2MasteringInitEx](../../../core/libs/prx/libSceAudioOut/src/AudioOut2Mastering.cpp) (libSceAudioOut) - the initialized flag and the flag checks follow the PS5 12.00 module (0x22360): a repeated initialization returns 0, other flags return `0x80268201`, and InitEx also accepts bit 31, which the console passes to the audio server as a mastering mode. Nothing is sent to an audio server, and the `0x80268006` the console returns before `sceAudioOut2Initialize` is not returned, since that initialization is not tracked. PPSA01289 calls Init twice before Term twice; by the module its second Term gets `0x80268203`, and the title only logs a non-zero result +- [sceAudioOut2MasteringInit and sceAudioOut2MasteringInitEx](../../../core/libs/prx/libSceAudioOut/src/AudioOut2Mastering.cpp) (libSceAudioOut) - the initialized flag and the flag checks follow the PS5 12.00 module (0x22360): a repeated initialization returns 0, other flags return `0x80268201`, and InitEx also accepts bit 31, which the console passes to the audio server as a mastering mode. Nothing is sent to an audio server, and `sceAudioOut2MasteringInit` does not return the `0x80268006` the console returns before `sceAudioOut2Initialize`, which is a missing state check of its own. PPSA01289 calls Init twice before Term twice; by the module its second Term gets `0x80268203`, and the title only logs a non-zero result - [sceAudioOut2MasteringGetState and sceAudioOut2MasteringSetParam](../../../core/libs/prx/libSceAudioOut/src/AudioOut2Mastering.cpp) (libSceAudioOut) - the state layout, the states ids 1 (default) and 2 (V2), the descriptor ids `0x01010001`, `0x01020001` and `0x01010003` and the error `0x80268204` follow [KytyPS5](https://github.com/KytyPS5/KytyPS5/blob/df5b9b92be7dfd463b47756b707d6d356a9f0550/src/libs/libAudio2.cpp), not a console measurement. GetState zeroes the states and writes the two descriptors; with no mastering stage the compressor and limiter values stay zero. SetParam accepts any parameter set and records nothing, as KytyPS5 does. The checks before them follow the PS5 12.00 module: both return `0x80268203` before mastering is initialized, GetState returns `0x8026800C` for a null state, SetParam returns `0x80268201` for a null parameter and `0x80268205` for flags above 1. The console's checks of `output` and `user` are not reproduced; both are ignored - [sceAudioOut2SetSystemDebugState](../../../core/libs/prx/libSceAudioOut/src/AudioOut2System.cpp) (libSceAudioOut) - records the debug state id in the trace and returns 0, as [KytyPS5](https://github.com/KytyPS5/KytyPS5/blob/df5b9b92be7dfd463b47756b707d6d356a9f0550/src/libs/libAudio2.cpp) does; the host has no system mix to apply it to. Id `0x20` with a 4-byte param throws, and the not-initialized error is not returned. Not measured on a console - [sceAudioOut2GetSpeakerArrayCoefficients](../../../core/libs/prx/libSceAudioOut/src/AudioOut2SpeakerArray.cpp) (libSceAudioOut) - validates the handle and the coefficient count like the ambisonics call, then writes the front pair at full gain and silence for every other speaker, ignoring the position, spread, height awareness and downmix spread radius: [KytyPS5](https://github.com/KytyPS5/KytyPS5/blob/df5b9b92be7dfd463b47756b707d6d356a9f0550/src/libs/libAudio2.cpp) and [prosper](https://github.com/mattias800/prosper/blob/main/prosper/src/hle/audio/hle_audio.cpp) both return that pair. The console's VBAP gains over the array's directions are not measured @@ -27,7 +28,10 @@ - [Legacy vibration ports](../../../core/libs/prx/libSceAudioOut/src/AudioOut.cpp): PPSA99902's [public input wrapper](https://github.com/Nazky/UnleashedRecomp-Prospero/blob/2ce067a1d51bb58670c7ee4a3aed893c504da805/UnleashedRecomp/hid/driver/sdl_hid.cpp#L316) opens type 10 with 256 stereo S16 or F32 frames at 48000 Hz. Mono duplicates its channel as AudioOut2 does; wider vibration layouts throw. Native controller output uses AudioOut2's four-channel layout, or its six-channel PulseAudio mapping. Without a controller sound card the existing RMS motor transform is applied to segments of at most 256 frames, scheduled by the legacy queue's frame count and rate; drain and close remove only that port's segments. Concurrent sources combine by the maximum per motor, and the pad consumer uses this approximation only for controllers other than DualSense. This host timing and motor model is not a console measurement or waveform reproduction; legacy native output is unmeasured on hardware, and user routing retains the single-controller limitation. AudioOut2 retains its separate 80 ms peak hold. - [sceAudioOutDeviceIdOpen](../../../core/libs/prx/libSceAudioOut/src/AudioOut.cpp) (libSceAudioOut) - the native wrapper consumes a 64-bit device ID, port type, sample count, frequency and format from argument slots 1, 2, 4, 5 and 6. Slot 3 is discarded, with the common open API index position preserved. Device enumeration and mapping to host outputs remain unimplemented; the throwing body is unchanged. -- [sceAudioOut2PortGetState](../../../core/libs/prx/libSceAudioOut/src/AudioOut2Port.cpp) and [sceAudioOut2GetSpeakerInfo](../../../core/libs/prx/libSceAudioOut/src/AudioOut2SpeakerArray.cpp) (libSceAudioOut) - the port state reports the channel count of the port's data format, output main, volume 127 and a zero reroute counter and flags. Taking the channel count from the data format follows [SharpEmu](https://github.com/sharpemu/sharpemu/blob/main/src/SharpEmu.Libs/Audio/AudioOut2Exports.cs), another emulator. A payload process on a PS5 (firmware 13.60), which is not the foreground application, read output 0, volume -1 and reroute counter 5 for its main ports, and 6 channels for an 8-channel port on a 5.1 system, so a title's values are unverified, including the output of a pad port, the volume after `sceAudioOut2PortSetAttributes` and the reroute counter on a device change. The speaker info reports type 0 with available bits `0x3`, the front left speaker at +30 degrees and the front right one at -30, and -360 for the azimuth and elevation of every other slot. On that console the type followed the HDMI device type and the available bits its channels: type 0 with the device set to TV, 1 to AV amplifier and 2 to sound bar, which [SharpProspero](https://github.com/SvenGDK/SharpProspero/blob/5dfdb4b0b166e83dc9ae275001afb695f12c7174/src/SharpProspero/Interop/Audio/AudioOut2.cs) names television, AV receiver and sound bar (3 is headphones there), and available bits `0x3` for 2 channels and `0x3F` for 5.1 (centre at 0, surround pair at +110 and -110, -360 for the LFE). The front pair and the empty slots read the same in every setting; 7.1 on the AV amplifier reported 2 channels on that console's HDMI chain. The host output is always reported as a stereo television here. The console also returns `0x80268006` before `sceAudioOut2Initialize`, `0x80268001` for flags above 1 and `0x8026800C` for a null pointer, which are not modelled. ASTRO BOT and ASTRO's PLAYROOM reportedly stop on an assertion that depends on these values (#4317), but which field matters has not been isolated +- [sceAudioOut2PortGetState](../../../core/libs/prx/libSceAudioOut/src/AudioOut2Port.cpp) (libSceAudioOut) - the port state reports the channel count of the port's data format, output main, volume 127 and a zero reroute counter and flags. Taking the channel count from the data format follows [SharpEmu](https://github.com/sharpemu/sharpemu/blob/main/src/SharpEmu.Libs/Audio/AudioOut2Exports.cs), another emulator. A payload process on a PS5 (firmware 13.60), which is not the foreground application, read output 0, volume -1 and reroute counter 5 for its main ports, and 6 channels for an 8-channel port on a 5.1 system, so a title's values are unverified, including the output of a pad port, the volume after `sceAudioOut2PortSetAttributes` and the reroute counter on a device change. +- [sceAudioOut2GetSpeakerInfo](../../../core/libs/prx/libSceAudioOut/src/AudioOut2SpeakerArray.cpp) (libSceAudioOut) - The speaker info reports type 0 with available bits `0x3`, the front left speaker at +30 degrees and the front right one at -30, and -360 for the azimuth and elevation of every other slot. On that console the type followed the HDMI device type and the available bits its channels: type 0 with the device set to TV, 1 to AV amplifier and 2 to sound bar, which [SharpProspero](https://github.com/SvenGDK/SharpProspero/blob/5dfdb4b0b166e83dc9ae275001afb695f12c7174/src/SharpProspero/Interop/Audio/AudioOut2.cs) names television, AV receiver and sound bar (3 is headphones there), and available bits `0x3` for 2 channels and `0x3F` for 5.1 (centre at 0, surround pair at +110 and -110, -360 for the LFE). +- [sceAudioOut2GetSpeakerInfo](../../../core/libs/prx/libSceAudioOut/src/AudioOut2SpeakerArray.cpp) (libSceAudioOut) - The front pair and the empty slots read the same in every setting; 7.1 on the AV amplifier reported 2 channels on that console's HDMI chain. The host output is always reported as a stereo television here. The console also refuses flag 1 below a per-process libkernel value whose source is unidentified; BunnyFl measured the boundary between `0x14FFFFF` and `0x1500000`, and it is not modelled here. ASTRO BOT and ASTRO's PLAYROOM reportedly stop on an assertion that depends on these values (#4317), but which field matters has not been isolated +- [sceAudioOutInit and sceAudioOut2Initialize repeats](../../../core/libs/prx/libSceAudioOut/src/AudioOut.cpp) (libSceAudioOut) - a repeated `sceAudioOutInit` returns 0 here, while the firmware returns `0x8026000e` on the second call; whether a repeated `sceAudioOut2Initialize` returns 0 is unmeasured. That `sceAudioOutInit` initializes AudioOut2, and that `sceAudioOut2GetSpeakerInfo` then succeeds, follow the firmware and are covered by tests. - [sceAudioOut2UserCreateEx](../../../core/libs/prx/libSceAudioOut/src/Unimplemented.cpp) (libSceAudioOut) - unknown signature - [sceAudioOutOpenEx](../../../core/libs/prx/libSceAudioOut/src/Unimplemented.cpp) (libSceAudioOut) - unknown signature - [sceAudioOutSetSystemDebugState](../../../core/libs/prx/libSceAudioOut/src/Unimplemented.cpp) (libSceAudioOut) - unknown signature @@ -47,10 +51,6 @@ - [sceAudioOutExGetLastOutputTime](../../../core/libs/prx/libSceAudioOut/src/Unimplemented.cpp) (libSceAudioOut) - unknown signature - [sceAudioOutGetInfoOpenNum](../../../core/libs/prx/libSceAudioOut/src/Unimplemented.cpp) (libSceAudioOut) - unknown signature - [sceAudioOutGetInfo](../../../core/libs/prx/libSceAudioOut/src/Unimplemented.cpp) (libSceAudioOut) - unknown signature -- [sceAudioOutMasteringInit](../../../core/libs/prx/libSceAudioOut/src/Unimplemented.cpp) (libSceAudioOut) - unknown signature -- [sceAudioOutMasteringSetParam](../../../core/libs/prx/libSceAudioOut/src/Unimplemented.cpp) (libSceAudioOut) - unknown signature -- [sceAudioOutMasteringGetState](../../../core/libs/prx/libSceAudioOut/src/Unimplemented.cpp) (libSceAudioOut) - unknown signature -- [sceAudioOutMasteringTerm](../../../core/libs/prx/libSceAudioOut/src/Unimplemented.cpp) (libSceAudioOut) - unknown signature - [sceAudioOutSetPadSpkVolume](../../../core/libs/prx/libSceAudioOut/src/Unimplemented.cpp) (libSceAudioOut) - unknown signature - [sceAudioOutSetPadJackVolume](../../../core/libs/prx/libSceAudioOut/src/Unimplemented.cpp) (libSceAudioOut) - unknown signature - [sceAudioOutSetUsbVolume](../../../core/libs/prx/libSceAudioOut/src/Unimplemented.cpp) (libSceAudioOut) - unknown signature @@ -113,6 +113,8 @@ ### Functional +- [sceAudioOutMasteringInit, sceAudioOutMasteringTerm and sceAudioOutMasteringSetParam](../../../core/libs/prx/libSceAudioOut/src/AudioOut2Mastering.cpp) (libSceAudioOut) - non-zero flags, a null pointer and a params id other than 1 throw; a repeated Init, a Term without Init and a SetParam or GetState before Init are unknown. There is no mastering stage, so Init and Term keep no state and SetParam reads only the id. The signatures and the default params id 1 follow [SharpProspero](https://github.com/SvenGDK/SharpProspero/blob/5dfdb4b0b166e83dc9ae275001afb695f12c7174/src/SharpProspero/Interop/Audio/AudioOut.cs#L312-L344), not a console measurement. SharpProspero documents flag bit 0 as `SwapEqCompressor`, and shadPS4's PS4 stub [rejects any non-zero Init flags](https://github.com/shadps4-emu/shadPS4/blob/8d50471b7dc141dd700b51276b8edd1f293bce26/src/core/libraries/audio/audioout.cpp#L956-L962) with 0x80260201 +- [sceAudioOutMasteringGetState](../../../core/libs/prx/libSceAudioOut/src/AudioOut2Mastering.cpp) (libSceAudioOut) - the states id 1 and the meter offsets 32, 128, 240, 272 and 304 in a 336-byte struct follow [SharpProspero](https://github.com/SvenGDK/SharpProspero/blob/5dfdb4b0b166e83dc9ae275001afb695f12c7174/src/SharpProspero/Audio/AudioOutMastering.cs#L97-L110), not a console measurement. With no mastering stage it zeroes the compressor input RMS and gain, and the limiter input, output and gain peaks, and leaves the other bytes, including the descriptors the AudioOut2 call writes, untouched; zero is not known to be what the console reports for an idle chain. A null pointer and a states id other than 1 throw; the console error codes are unknown - [sceAudioOut2EnableChat and sceAudioOut2DisableChat](../../../core/libs/prx/libSceAudioOut/src/AudioOut2System.cpp) - both PS5 exports take no arguments, return 0x80268006 before AudioOut2 initialization, and invoke the system chat service with selector 3. A negative service result maps to 0x80268000, otherwise they return zero. The host only records a local enabled flag, without initialization validation or a chat service; repeated-call and no-session behavior therefore still depends on the missing service. - [sceAudioOut2Set3DLatency](../../../core/libs/prx/libSceAudioOut/src/AudioOut2System.cpp) (libSceAudioOut) - the two 32-bit arguments and selector validation match the system library: selector 0 accepts outputs 0 through 2; selector 255 accepts outputs 1 and 2. No third latency argument exists. The host has no system 3D rendering stage, so the call still does not apply latency and does not reproduce the system initialization and output-availability checks. - [AudioOut2 contexts](../../../core/libs/prx/libSceAudioOut/src/AudioOut2Context.cpp) (libSceAudioOut) with an SDL device and no `sceAudioOut2ContextAdvance` calls read a grain's port buffers at the next push, at least 100 us after the grain was pushed, and play the mix behind a 40 ms cushion that the queue level leaves out. When the hardware reads the buffers is not known. The timing follows a title whose mixer fills a buffer after pushing it, reuses it two pushes later, and may still be writing it when the next push follows within microseconds. The last grain pushed before a title stops pushing is never read or played. diff --git a/docs/dev/debt/libSceBgft.md b/docs/dev/debt/libSceBgft.md index 385f701d0f..2240209a4d 100644 --- a/docs/dev/debt/libSceBgft.md +++ b/docs/dev/debt/libSceBgft.md @@ -5,3 +5,13 @@ - [sceBgftServiceDownloadGetProgress](../../../core/libs/prx/libSceBgft/Export.cpp), `sceBgftServiceIntDownloadGetProgress`, `sceBgftServiceIntDownloadGetGameAndGameAcProgress` and `sceBgftServiceIntDownloadGetPlayGoProgress`: signatures and the shared 88-byte output are confirmed. The library forwards the output through IPC without accessing individual fields; their types remain unresolved. - [sceBgftServiceIntDownloadRegisterTaskByStorageEx](../../../core/libs/prx/libSceBgft/Export.cpp): confirmed parameter pointer and 32-bit task ID output. The input contains 32-bit scalars at offsets 0, 4, 56 and 104, strings at offsets 8, 16, 32, 40, 48, 64, 72, 80 and 88, and a 64-bit scalar at offset 96. The typed layout preserves the unused eight bytes at offset 24; scalar meanings and several string roles remain unresolved. - [sceBgftServiceIntDownloadGetPatchProgress](../../../core/libs/prx/libSceBgft/Export.cpp) and `sceBgftServiceIntDownloadGetTaskInfo`: neither export nor a caller was found in the supplied decompiled library corpus. Their signatures remain unresolved. + +- [sceBgftServiceIntDebugPlayGoClearSetFreeZone](../../../core/libs/prx/libSceBgft/Export.cpp) (libSceBgft) - It is in the intersection of the export table of the console's libSceBgft (function symbols only), the pinned aerolib names and the functions not yet defined anywhere in the repo; names and NIDs are present in the [PS5 3.20 export inventory](https://github.com/DNNDHH/PS5-3.20_Libs/blob/b2a77c09cd71c01f34c790cfbe98c751a13262ad/libSceBgft.c) and the [verified NID name database](https://github.com/zecoxao/sce_symbols/blob/2883963a0a514ba6e77407a08ef4730a65e88254/aerolib.csv), but no credible prototypes were found. It is declared without parameters solely to resolve known imports; this is not an ABI claim and callers must not call them until signatures are verified. +- [sceBgftServiceIntDebugPlayGoIsSetFreeZone](../../../core/libs/prx/libSceBgft/Export.cpp) (libSceBgft) - It is in the intersection of the export table of the console's libSceBgft (function symbols only), the pinned aerolib names and the functions not yet defined anywhere in the repo; names and NIDs are present in the [PS5 3.20 export inventory](https://github.com/DNNDHH/PS5-3.20_Libs/blob/b2a77c09cd71c01f34c790cfbe98c751a13262ad/libSceBgft.c) and the [verified NID name database](https://github.com/zecoxao/sce_symbols/blob/2883963a0a514ba6e77407a08ef4730a65e88254/aerolib.csv), but no credible prototypes were found. It is declared without parameters solely to resolve known imports; this is not an ABI claim and callers must not call them until signatures are verified. +- [sceBgftServiceIntDownloadCheckPatchUpdateState and sceBgftServiceIntDownloadClearStats](../../../core/libs/prx/libSceBgft/Export.cpp) (libSceBgft) - The list is the intersection of the export table of the console's libSceBgft (function symbols only), the pinned aerolib names and the functions not yet defined anywhere in the repo; names and NIDs are present in the [PS5 3.20 export inventory](https://github.com/DNNDHH/PS5-3.20_Libs/blob/b2a77c09cd71c01f34c790cfbe98c751a13262ad/libSceBgft.c) and the [verified NID name database](https://github.com/zecoxao/sce_symbols/blob/2883963a0a514ba6e77407a08ef4730a65e88254/aerolib.csv), but no credible prototypes were found. They are declared without parameters solely to resolve known imports; this is not an ABI claim and callers must not call them until signatures are verified. +- [sceBgftServiceIntDownloadGetApplicationDataSize](../../../core/libs/prx/libSceBgft/Export.cpp) (libSceBgft) - It is in the intersection of the export table of the console's libSceBgft (function symbols only), the pinned aerolib names and the functions not yet defined anywhere in the repo; names and NIDs are present in the [PS5 3.20 export inventory](https://github.com/DNNDHH/PS5-3.20_Libs/blob/b2a77c09cd71c01f34c790cfbe98c751a13262ad/libSceBgft.c) and the [verified NID name database](https://github.com/zecoxao/sce_symbols/blob/2883963a0a514ba6e77407a08ef4730a65e88254/aerolib.csv), but no credible prototypes were found. It is declared without parameters solely to resolve known imports; this is not an ABI claim and callers must not call them until signatures are verified. +- [sceBgftServiceIntDownloadGetPatchGoProgress and sceBgftServiceIntDownloadHideTask](../../../core/libs/prx/libSceBgft/Export.cpp) (libSceBgft) - The list is the intersection of the export table of the console's libSceBgft (function symbols only), the pinned aerolib names and the functions not yet defined anywhere in the repo; names and NIDs are present in the [PS5 3.20 export inventory](https://github.com/DNNDHH/PS5-3.20_Libs/blob/b2a77c09cd71c01f34c790cfbe98c751a13262ad/libSceBgft.c) and the [verified NID name database](https://github.com/zecoxao/sce_symbols/blob/2883963a0a514ba6e77407a08ef4730a65e88254/aerolib.csv), but no credible prototypes were found. They are declared without parameters solely to resolve known imports; this is not an ABI claim and callers must not call them until signatures are verified. +- [sceBgftServiceIntDownloadSetResumeState and sceBgftServiceIntDownloadSetStartState](../../../core/libs/prx/libSceBgft/Export.cpp) (libSceBgft) - The list is the intersection of the export table of the console's libSceBgft (function symbols only), the pinned aerolib names and the functions not yet defined anywhere in the repo; names and NIDs are present in the [PS5 3.20 export inventory](https://github.com/DNNDHH/PS5-3.20_Libs/blob/b2a77c09cd71c01f34c790cfbe98c751a13262ad/libSceBgft.c) and the [verified NID name database](https://github.com/zecoxao/sce_symbols/blob/2883963a0a514ba6e77407a08ef4730a65e88254/aerolib.csv), but no credible prototypes were found. They are declared without parameters solely to resolve known imports; this is not an ABI claim and callers must not call them until signatures are verified. +- [sceBgftServiceIntPlayGoCheckDiscIniChunksInstall and sceBgftServiceIntPlayGoStartDataDiscInstall](../../../core/libs/prx/libSceBgft/Export.cpp) (libSceBgft) - The list is the intersection of the export table of the console's libSceBgft (function symbols only), the pinned aerolib names and the functions not yet defined anywhere in the repo; names and NIDs are present in the [PS5 3.20 export inventory](https://github.com/DNNDHH/PS5-3.20_Libs/blob/b2a77c09cd71c01f34c790cfbe98c751a13262ad/libSceBgft.c) and the [verified NID name database](https://github.com/zecoxao/sce_symbols/blob/2883963a0a514ba6e77407a08ef4730a65e88254/aerolib.csv), but no credible prototypes were found. They are declared without parameters solely to resolve known imports; this is not an ABI claim and callers must not call them until signatures are verified. +- [sceBgftServiceIntSetTaskVisible, sceBgftServiceIntUploadGetAvailableTaskCount, sceBgftServiceIntUploadGetTaskInfo and sceBgftServiceIntUploadHideTask](../../../core/libs/prx/libSceBgft/Export.cpp) (libSceBgft) - The list is the intersection of the export table of the console's libSceBgft (function symbols only), the pinned aerolib names and the functions not yet defined anywhere in the repo; names and NIDs are present in the [PS5 3.20 export inventory](https://github.com/DNNDHH/PS5-3.20_Libs/blob/b2a77c09cd71c01f34c790cfbe98c751a13262ad/libSceBgft.c) and the [verified NID name database](https://github.com/zecoxao/sce_symbols/blob/2883963a0a514ba6e77407a08ef4730a65e88254/aerolib.csv), but no credible prototypes were found. They are declared without parameters solely to resolve known imports; this is not an ABI claim and callers must not call them until signatures are verified. +- [sceBgftServiceIntUploadSetStartState and sceBgftServiceIntUploadSetUploadError](../../../core/libs/prx/libSceBgft/Export.cpp) (libSceBgft) - The list is the intersection of the export table of the console's libSceBgft (function symbols only), the pinned aerolib names and the functions not yet defined anywhere in the repo; names and NIDs are present in the [PS5 3.20 export inventory](https://github.com/DNNDHH/PS5-3.20_Libs/blob/b2a77c09cd71c01f34c790cfbe98c751a13262ad/libSceBgft.c) and the [verified NID name database](https://github.com/zecoxao/sce_symbols/blob/2883963a0a514ba6e77407a08ef4730a65e88254/aerolib.csv), but no credible prototypes were found. They are declared without parameters solely to resolve known imports; this is not an ABI claim and callers must not call them until signatures are verified. diff --git a/docs/dev/debt/libSceFiber.md b/docs/dev/debt/libSceFiber.md index 9ad010851f..f1c33e85c3 100644 --- a/docs/dev/debt/libSceFiber.md +++ b/docs/dev/debt/libSceFiber.md @@ -6,7 +6,8 @@ ### Unknown function info -- [_sceFiberInitializeWithInternalOptionImpl](../../../core/libs/prx/libSceFiber/Export.cpp) (libSceFiber) - its signature follows shadPS4's [initialize implementations](https://github.com/shadps4-emu/shadPS4/blob/f68923fe807f657348e6719ce658bcbd0a2b1c72/src/core/libraries/fiber/fiber.cpp#L150-L153); its export alias is mapped at [RegisterLib](https://github.com/shadps4-emu/shadPS4/blob/f68923fe807f657348e6719ce658bcbd0a2b1c72/src/core/libraries/fiber/fiber.cpp#L548-L551). Not measured on a console; the throwing declaration only resolves the import. -- [_sceFiberAttachContextAndRun, _sceFiberAttachContextAndSwitch](../../../core/libs/prx/libSceFiber/Export.cpp) (libSceFiber) - signatures follow shadPS4's [run implementation](https://github.com/shadps4-emu/shadPS4/blob/f68923fe807f657348e6719ce658bcbd0a2b1c72/src/core/libraries/fiber/fiber.cpp#L260-L261) and [switch implementation](https://github.com/shadps4-emu/shadPS4/blob/f68923fe807f657348e6719ce658bcbd0a2b1c72/src/core/libraries/fiber/fiber.cpp#L336-L337); their export aliases are mapped at [RegisterLib](https://github.com/shadps4-emu/shadPS4/blob/f68923fe807f657348e6719ce658bcbd0a2b1c72/src/core/libraries/fiber/fiber.cpp#L555-L563). Not measured on a console; the throwing declarations only resolve imports. +- [_sceFiberInitializeWithInternalOptionImpl](../../../core/libs/prx/libSceFiber/Export.cpp) (libSceFiber) - its signature and the flag values `0x1` (no ulobjmgr registration), `0x10` (context size check) and `0x100` (start with MXCSR `0x9fc0` and x87 control word `0x037f`) follow shadPS4's [initialize implementation](https://github.com/shadps4-emu/shadPS4/blob/f68923fe807f657348e6719ce658bcbd0a2b1c72/src/core/libraries/fiber/fiber.cpp#L150-L153) and [flags](https://github.com/shadps4-emu/shadPS4/blob/f68923fe807f657348e6719ce658bcbd0a2b1c72/src/core/libraries/fiber/fiber.h#L27-L32). Not measured on a console. `0x1` has no effect because no ulobjmgr exists; other flag bits throw. shadPS4 also sets `0x100` for `build_version` 3.50 and later in every initialize; `opt_param` and `build_version` are ignored. +- [_sceFiberAttachContextAndRun, _sceFiberAttachContextAndSwitch](../../../core/libs/prx/libSceFiber/Export.cpp) (libSceFiber) - signatures and behaviour follow shadPS4's [run](https://github.com/shadps4-emu/shadPS4/blob/f68923fe807f657348e6719ce658bcbd0a2b1c72/src/core/libraries/fiber/fiber.cpp#L260-L261), [switch](https://github.com/shadps4-emu/shadPS4/blob/f68923fe807f657348e6719ce658bcbd0a2b1c72/src/core/libraries/fiber/fiber.cpp#L336-L337) and [attach](https://github.com/shadps4-emu/shadPS4/blob/f68923fe807f657348e6719ce658bcbd0a2b1c72/src/core/libraries/fiber/fiber.cpp#L46-L79) implementations, not measured on a console. A context is attached only to an idle fiber initialized without one; a fiber that already has a context, or is not idle, returns `SCE_FIBER_ERROR_INVALID`. A size that is not a multiple of 16 returns `SCE_FIBER_ERROR_ALIGNMENT` as in initialize, where shadPS4 returns `SCE_FIBER_ERROR_INVALID`. +- [Fibers without a context](../../../core/libs/prx/libSceFiber/Export.cpp) (libSceFiber) - running or switching to a fiber initialized without a context, before one is attached, throws. On the console such a fiber runs on the stack of its caller, per shadPS4's [run implementation](https://github.com/shadps4-emu/shadPS4/blob/f68923fe807f657348e6719ce658bcbd0a2b1c72/src/core/libraries/fiber/fiber.cpp#L297-L313). - [_sceFiberGetThreadFramePointerAddress](../../../core/libs/prx/libSceFiber/Export.cpp) (libSceFiber, NID `0dy4JtMUcMQ`) forwards to `sceFiberGetThreadFramePointerAddress`; that the argument list matches is inferred from the export without the underscore, as for the underscore Ult exports, not measured - [Fiber stacks](../../../core/libs/prx/libSceFiber/Export.cpp) (libSceFiber) - a fiber whose context is smaller than 64 KiB runs on an mmap'd host stack of its context size plus 1 MiB, so host calls made from it (21 KiB deep when PPSA01341 reaches SPIRV-Tools from a 16 KiB context) do not overwrite the contexts packed next to it; a larger context is the stack, as on the console. PPSA05684 needs the second: its job fibers have 64 KiB and 512 KiB contexts, the 512 KiB ones at 1 MiB-aligned addresses, and find their job record by masking the stack pointer (`~0xfffff` for the 512 KiB ones), which a host stack breaks (the game waits forever in GameInit, or faults later in a 64 KiB fiber). The 64 KiB limit is chosen between these two titles, not measured: a title with small contexts that also derives state from the stack pointer, or one whose host calls exceed what is left of a large context, is not covered diff --git a/docs/dev/debt/libSceFreeType.md b/docs/dev/debt/libSceFreeType.md index b74efa0b9d..5fd44fd1d1 100644 --- a/docs/dev/debt/libSceFreeType.md +++ b/docs/dev/debt/libSceFreeType.md @@ -6,8 +6,12 @@ - [FT_Select_Charmap, FT_Set_Charmap, FT_Get_First_Char, FT_Get_Next_Char](../../../core/libs/prx/libSceFreeType/Export.cpp), [FT_Get_Glyph_Name, FT_Get_Name_Index, FT_Get_Postscript_Name](../../../core/libs/prx/libSceFreeType/src/Names.cpp) (libSceFreeType) - console signatures, naming rules and errors are unverified; the LP64 adapter follows bundled [FreeType 2.13.3](https://github.com/freetype/freetype/blob/42608f77f20749dd6ddc9e0536788eaad70ea4b5/src/base/ftobjs.c). Iteration uses the low 32 input bits and updates only the selected guest charmap; charmap fields are read-only copies. Naming preserves LP64 invalid-index errors on Windows. PostScript names borrow native face storage and remain valid only while the face is alive +- [FT_Cos, FT_Sin, FT_Tan, FT_Vector_Unit and FT_Angle_Diff](../../../core/libs/prx/libSceFreeType/src/Trigonometry.cpp) (libSceFreeType) - console ABI/version and CORDIC equivalence are unverified; the LP64 adapter follows bundled [FreeType 2.13.3](https://github.com/freetype/freetype/blob/42608f77f20749dd6ddc9e0536788eaad70ea4b5/src/base/fttrigon.c). Tangent retains the 64-bit quotient near poles instead of Windows FT_Long truncation. Angle differences whose signed 64-bit subtraction overflows throw; the bundled source's subtraction is undefined there. + - [FT_Init_FreeType, FT_New_Face, FT_Set_Pixel_Sizes, FT_Get_Char_Index, FT_Load_Glyph, FT_Render_Glyph](../../../core/libs/prx/libSceFreeType/Export.cpp) (libSceFreeType) - the PS4 library is taken to be FreeType 2 built for LP64: the face, size, charmap, glyph slot, bitmap and outline layouts follow the public headers of the bundled FreeType 2.13.3 with a 64-bit `long`, which matches the offsets the OpenOrbis samples read (`face->glyph` at 0x98, `slot->advance` at 0x80, `slot->bitmap` at 0x98, `slot->bitmap_left` at 0xc0). Which FreeType version and options the console library is built with is unknown, so the behaviour and error codes are those of FreeType 2.13.3. The OpenOrbis font, keyboard, threading, system and SDL2 samples import these six functions +- [FT_Bitmap_New, FT_Bitmap_Copy, FT_Bitmap_Convert, FT_Bitmap_Done](../../../core/libs/prx/libSceFreeType/src/Bitmap.cpp) (libSceFreeType) - console behaviour and error codes are unmeasured; these follow bundled FreeType 2.13.3. Copy and Convert throw when source and target share a non-null buffer; Convert also throws when they are the same object. Other overlapping storage remains forbidden by the native API contract + - [FT_Request_Size](../../../core/libs/prx/libSceFreeType/src/SizeRequest.cpp) (libSceFreeType) - console signature, request layout and errors are unverified; the guest request follows the LP64 public headers of the bundled [FreeType 2.13.3](https://github.com/freetype/freetype/blob/42608f77f20749dd6ddc9e0536788eaad70ea4b5/include/freetype/freetype.h). Host overflow behavior remains that of the bundled build. On Windows, native `FT_Long` is 32 bits; valid requests whose width or height exceeds that range throw before changing the size. - [FT_New_Memory_Face, FT_Done_Face and FT_Done_FreeType](../../../core/libs/prx/libSceFreeType/Export.cpp) (libSceFreeType) - console signatures, errors and generic-finalizer ordering are unverified; they follow the bundled [FreeType 2.13.3](https://github.com/freetype/freetype/blob/42608f77f20749dd6ddc9e0536788eaad70ea4b5/include/freetype/freetype.h) with LP64 guest arguments. Caller-owned font bytes must remain valid until the face is freed. @@ -35,3 +39,5 @@ - [FT_Set_Char_Size](../../../core/libs/prx/libSceFreeType/Export.cpp) (libSceFreeType) - console ABI, FreeType version and error compatibility are unverified. The name and NID `xuY99Qcb108` are in the [PS5 3.20 inventory](https://github.com/DNNDHH/PS5-3.20_Libs/blob/b2a77c09cd71c01f34c790cfbe98c751a13262ad/libSceFreeTypeOt.c); the LP64 signature and behavior follow bundled [FreeType 2.13.3](https://github.com/freetype/freetype/blob/42608f77f20749dd6ddc9e0536788eaad70ea4b5/src/base/ftobjs.c), not a console measurement. After applying its defaults and minimum-size clamp, requests whose `dimension * dpi + 36` exceeds the host's signed `FT_Long` range throw before the native call. This is a 32-bit limit on Windows; larger console requests remain unsupported. - [FT_Get_Gasp](../../../core/libs/prx/libSceFreeType/Export.cpp) (libSceFreeType) - console FreeType version and query-result compatibility are unverified. The guest face adapter follows bundled [FreeType 2.13.3](https://github.com/freetype/freetype/blob/42608f77f20749dd6ddc9e0536788eaad70ea4b5/src/base/ftgasp.c) and its existing LP64 face bridge. + +- [FT_Get_CID_Registry_Ordering_Supplement, FT_Get_CID_Is_Internally_CID_Keyed and FT_Get_CID_From_Glyph_Index](../../../core/libs/prx/libSceFreeType/Export.cpp) (libSceFreeType) - console ABI/version and CID metadata equivalence are unverified. The adapters follow bundled [FreeType 2.13.3](https://github.com/freetype/freetype/blob/42608f77f20749dd6ddc9e0536788eaad70ea4b5/src/base/ftcid.c) through the existing face bridge; returned registry/ordering strings have the native face's lifetime. diff --git a/docs/dev/debt/libSceGnmDriverForNeoMode.md b/docs/dev/debt/libSceGnmDriverForNeoMode.md index 4d80911108..507eeee80f 100644 --- a/docs/dev/debt/libSceGnmDriverForNeoMode.md +++ b/docs/dev/debt/libSceGnmDriverForNeoMode.md @@ -2,10 +2,14 @@ ### Unknown function info -- [libSceGnmDriverForNeoMode pending exports](../../../core/libs/prx/libSceGnmDriverForNeoMode/Export.cpp) - 48 named callable exports are listed in the [PS5 3.20 export table](https://github.com/DNNDHH/PS5-3.20_Libs/blob/b2a77c09cd71c01f34c790cfbe98c751a13262ad/libSceGnmDriverForNeoMode.c) and were cross-checked against the same module in PS5 firmware 13.60. Their signatures and behaviour are unverified; the declarations use the no-parameter unknown-signature convention and throw through `NotImplemented_nid_no_patch`. +- [libSceGnmDriverForNeoMode pending exports](../../../core/libs/prx/libSceGnmDriverForNeoMode/Export.cpp) - 34 named callable exports are listed in the [PS5 3.20 export table](https://github.com/DNNDHH/PS5-3.20_Libs/blob/b2a77c09cd71c01f34c790cfbe98c751a13262ad/libSceGnmDriverForNeoMode.c) and were cross-checked against the same module in PS5 firmware 13.60. Their signatures and behaviour are unverified; the declarations use the no-parameter unknown-signature convention and throw through `NotImplemented_nid_no_patch`. ### Functional - [retail-only answers](../../../core/libs/prx/libSceGnmDriverForNeoMode/Export.cpp) (libSceGnmDriverForNeoMode) - the debug resets, the resource and owner registration calls, the SDMA calls and the SQ counter calls return `0x8EEE00FF`, the five `sceGnmDriverInternalRetrieveGnmInterface*` calls return `0x80000000`, `sceGnmGetDbgGcHandle` returns -1, `sceGnmDriverTraceInProgress` and `sceGnmValidateOnSubmitEnabled` return 0 and the other validation calls return `0x80D13FFF`. These are the values [shadPS4](https://github.com/shadps4-emu/shadPS4/blob/a808e76/src/core/libraries/gnmdriver/gnmdriver.cpp) returns for calls it marks as not available in retail firmware; they are not measured on a console, and the arguments are not read - [default state packets](../../../core/libs/prx/libSceGnmDriverForNeoMode/Export.cpp) (libSceGnmDriverForNeoMode) - `sceGnmDrawInitDefaultHardwareState`, `sceGnmDrawInitDefaultHardwareState200`, `sceGnmDrawInitDefaultHardwareState350`, `sceGnmDrawInitToDefaultContextState` and `sceGnmDrawInitToDefaultContextState400` write the sequences in [InitSequences.hpp](../../../core/libs/prx/libSceGnmDriverForNeoMode/include/InitSequences.hpp), taken from [shadPS4's gnmdriver_init.h](https://github.com/shadps4-emu/shadPS4/blob/a808e76/src/core/libraries/gnmdriver/gnmdriver_init.h): its Neo sequences, except the plain hardware state, which shadPS4 also writes unchanged in Neo mode. The size checks and returned sizes are shadPS4's. Not compared with console output, and nothing in AnyPS5 consumes a GNM command buffer yet - [draw, dispatch, wait and VGT packets](../../../core/libs/prx/libSceGnmDriverForNeoMode/Export.cpp) (libSceGnmDriverForNeoMode) - the signatures, size checks, PM4 words and refusals (-1) of the 17 command writers follow [shadPS4](https://github.com/shadps4-emu/shadPS4/blob/a808e76/src/core/libraries/gnmdriver/gnmdriver.cpp) on its Neo-mode path, since this module is the Neo-mode driver: draw initiators keep flag bits 29 to 31, and `sceGnmSetVgtControl` and `sceGnmResetVgtControl` write `IA_MULTI_VGT_PARAM` through `SET_UCONFIG_REG` (shadPS4's `UseNeoCompatSequences` is false). The packets are not compared with console output, and nothing in AnyPS5 consumes a GNM command buffer yet +- [embedded shaders](../../../core/libs/prx/libSceGnmDriverForNeoMode/Export.cpp) (libSceGnmDriverForNeoMode) - `sceGnmSetEmbeddedVsShader` (id 0, a fullscreen triangle) and `sceGnmSetEmbeddedPsShader` (id 0 writes black, id 1 exports depth only) write the register packets of [shadPS4](https://github.com/shadps4-emu/shadPS4/blob/a808e76/src/core/libraries/gnmdriver/gnmdriver.cpp), pointing at the shader code in [EmbeddedShaders.hpp](../../../core/libs/prx/libSceGnmDriverForNeoMode/include/EmbeddedShaders.hpp) in this module's memory instead of the console's `0xfe000f00` area. That code is shadPS4's instruction listing assembled with LLVM 19 `llvm-mc -arch=amdgcn -mcpu=bonaire`, without the binary header and register block that follow it on the console, since nothing here reads them; another id returns `0x8EEE00FF`. The size and modifier arguments are not checked, as in shadPS4. Not compared with console output, and nothing in AnyPS5 consumes a GNM command buffer yet +- [shader register packets](../../../core/libs/prx/libSceGnmDriverForNeoMode/Export.cpp) (libSceGnmDriverForNeoMode) - `sceGnmUpdateVsShader`, `sceGnmUpdatePsShader`, `sceGnmUpdatePsShader350`, `sceGnmSetPsShader350`, `sceGnmUpdateGsShader`, `sceGnmUpdateHsShader` and `sceGnmSetCsShaderWithModifier` write the packets, size checks and -1 refusals of [shadPS4](https://github.com/shadps4-emu/shadPS4/blob/a808e76/src/core/libraries/gnmdriver/gnmdriver.cpp); the update forms carry context registers inside NOP packets as there. Not compared with console output, and nothing in AnyPS5 consumes a GNM command buffer yet +- [workloads](../../../core/libs/prx/libSceGnmDriverForNeoMode/Export.cpp) (libSceGnmDriverForNeoMode) - `sceGnmCreateWorkloadStream`, `sceGnmBeginWorkload` and `sceGnmEndWorkload` return shadPS4's values (3 for a null output or name, 1 for a stream above 15, 2 for workload 0 or one with a top byte above 15) and keep no state; not measured on a console +- [sceGnmGetGpuCoreClockFrequency](../../../core/libs/prx/libSceGnmDriverForNeoMode/Export.cpp) (libSceGnmDriverForNeoMode) - returns 911 MHz, shadPS4's Neo-mode value, which it derives from Neo mode instead of the console's ioctl. `sceGnmGetOffChipTessellationBufferSize` returns shadPS4's `0x800000` diff --git a/docs/dev/debt/libSceHttp.md b/docs/dev/debt/libSceHttp.md index 7d65b883c1..e58f4e6694 100644 --- a/docs/dev/debt/libSceHttp.md +++ b/docs/dev/debt/libSceHttp.md @@ -29,6 +29,7 @@ - [sceHttpDbgShowConnectionStat](../../../core/libs/prx/libSceHttp/Export.cpp) (libSceHttp) - the native diagnostic consumes no arguments; it prints library state. The throwing implementation remains unchanged. - [sceHttpDbgShowRequestStat](../../../core/libs/prx/libSceHttp/Export.cpp) (libSceHttp) - the native diagnostic consumes no arguments; it prints library state. The throwing implementation remains unchanged. - [sceHttpDbgShowMemoryPoolStat](../../../core/libs/prx/libSceHttp/Export.cpp) (libSceHttp) - the native diagnostic consumes no arguments; it prints library state. The throwing implementation remains unchanged. +- [sceHttpGetMemoryPoolStats](../../../core/libs/prx/libSceHttp/Export.cpp) (libSceHttp) - not measured on a console. The library takes nothing from the pool given to `sceHttpInit`, so it reports that size with peak and current use 0, where a console already holds bytes for a live context (0x1640 for Http2, see libSceNet). No live context returns `BEFORE_INIT` (0x80431001) and an unknown or terminated id `INVALID_ID` (0x80431100), as in `sceHttpCookieFlush`, with the output untouched; both codes are unmeasured. A null output throws, as its error code is unknown. Only the 24 bytes of the declared layout are written; [SharpProspero's struct](https://github.com/SvenGDK/SharpProspero/blob/5dfdb4b0b166e83dc9ae275001afb695f12c7174/src/SharpProspero/Interop/Net/Http.cs#L398-L413) adds a 32-bit reserved field after them ### Functional diff --git a/docs/dev/debt/libSceJobManager.md b/docs/dev/debt/libSceJobManager.md index d0330291ce..fbd9771780 100644 --- a/docs/dev/debt/libSceJobManager.md +++ b/docs/dev/debt/libSceJobManager.md @@ -3,6 +3,6 @@ ### Functional - [sce::Job::JobManager](../../../core/libs/prx/libSceJobManager/Export.cpp) submits commands as work becomes runnable rather than encoding ahead with the rewind protocol recovered from PPSA06888's module (offsets 0x2210 and 0x2650). Commands are recycled after a completed wait. Command capacity is a separate bound from the dispatch count: the native size query measures one register and a 31-character marker, then its exhaustion callback at 0x29d0 traps instead of growing storage. The fallback throws. Original-module execution with the host AGC builders accepts two dispatches with 255 registers and 65,535-character markers, then traps on a third. Console timing equivalence remains unverified. Compute dispatch on a CPU sequence throws. Shutdown with outstanding compute work throws. -- [Scheduling](../../../core/libs/prx/libSceJobManager/Export.cpp) uses host mutexes and condition variables instead of native atomic queues and semaphores; spin intervals deliberately use unscaled host TSC. Blocks below 0x2d8 bytes, factory priorities above one, use of destroyed sequences and shutdown from a worker throw. New calls concurrent with shutdown are unverified. +- [Scheduling](../../../core/libs/prx/libSceJobManager/Export.cpp) uses host mutexes and condition variables instead of native atomic queues and semaphores; spin intervals deliberately use unscaled host TSC, while the original module reads scaled guest TSC under the host-only `APS5_TIME_SCALE` diagnostic. Blocks below 0x2d8 bytes, factory priorities above one, use of destroyed sequences and shutdown from a worker throw. New calls concurrent with shutdown are unverified. - [Pool allocation](../../../core/libs/prx/libSceJobManager/Export.cpp) reuses freed nodes in a different order; profiler dispatch IDs can differ from native allocation. The fallback Wait deliberately allocates no pool node; native Wait can stall on global pool exhaustion. Resource-creation failures throw after cleanup, where the original does not check every return. Vtable order was checked, but no calling title or PS5 timing equivalence has been tested. -- Console behavior for [worker waits](../../../core/libs/prx/libSceJobManager/Export.cpp) is unverified. The owned original module stalls on the zero-spin, zero-persistence `worker_wait` fixture with native Windows and Linux host kernel services; the fallback wakes a spare worker and completes the child. +- Console behavior for [worker waits](../../../core/libs/prx/libSceJobManager/Export.cpp) is unverified. The owned original module stalls on the zero-spin, zero-persistence `worker_wait` fixture with native Windows and Linux host kernel services; the fallback wakes a spare worker and completes the child. The [reference runner](../../../tools/run_job_manager_reference.py) on this branch supports Windows only; failures remain failures. diff --git a/docs/dev/debt/libSceJson2.md b/docs/dev/debt/libSceJson2.md index 527684f6a3..bb2d163782 100644 --- a/docs/dev/debt/libSceJson2.md +++ b/docs/dev/debt/libSceJson2.md @@ -10,6 +10,7 @@ - [sce::Json::Value::setTypeMismatchHandler, setElementAccessFailureHandler and setSpecialFloatHandler](../../../core/libs/prx/libSceJson2/Export.cpp) (libSceJson2) - members of `Value` that return an int, inferred from a caller that registers them on its root `Value` before parsing into it, with the handler and its context, and accepts 0 or 0x80848104 as success; they return 0. The PS Vita library's `Value::setNullAccessCallback` is such a member too ([vitasdk](https://github.com/vitasdk/vita-headers/blob/15605617cbbb23d266f88af28899fdb573e2355e/include/psp2/json.h#L638)). The handler signatures follow the mangled names. A handler applies to the `Value` it is set on and to the values that `Value` holds then, and `Parser::parse` gives the values it creates the handlers of the `Value` it parses into, since the title reads the errors its handlers record after accessing parsed members; copies, `push_back` and `Object::operator[]` do not carry them, and how the console finds the handler of an accessed value is unknown. A typed getter on a non-null value of another type calls the type mismatch handler with the requested type, the value, null and the context, and returns the value the handler returns if it has the requested type, else the getter's default; passing null as the third handler argument (the second `Value` pointer), and the order of the two `Value` pointers, remain unverified against the console. The element access failure handler is not called: a failed `operator[]` or `getValue` on a value carrying it throws, as the error code the console passes, which the title records as its error, is unknown; without one the access still reaches the null-access callback. The special float handler is not called: serializing a non-finite real throws with or without it, as the `FunctionType` values and the `bool*` output are unknown, and the title's handler returns a different replacement for each of its two types - [sce::Json::MemAllocator::notifyError](../../../core/libs/prx/libSceJson2/Export.cpp) (libSceJson2) - the base class's virtual does nothing. A title's allocator that does not override it calls it through its own vtable; the library never calls the title's allocator, so it never reports an allocation failure. Whether the console's base implementation logs anything is unknown - [sce::Json::String size, capacity, resize, append(const char*, size_t), operator+=](../../../core/libs/prx/libSceJson2/Export.cpp) (libSceJson2) - storage uses `std::string`; capacity and allocation growth do not match the system 32-bit capacity and allocator settings. Appending null text with a nonzero length throws instead of dereferencing it. +- [sce::Json::String at, find and rfind](../../../core/libs/prx/libSceJson2/Export.cpp) (libSceJson2) - the return types follow the PS Vita library's `String` ([vitasdk](https://github.com/vitasdk/vita-headers/blob/15605617cbbb23d266f88af28899fdb573e2355e/include/psp2/json.h#L338)) and the search follows `std::string`. `at` past the end and a null text or `String` throw; what the console returns or does there is unverified. - [sce::Json::Array::iterator::advance and Object::iterator members](../../../core/libs/prx/libSceJson2/Export.cpp) (libSceJson2) - the return type of `advance` remains unverified. Object iterator advancement past the end throws instead of asserting. Dereferencing an empty array iterator throws instead of returning the system null-value pointer or an invalid address. - [sce::Json::Value::operator bool](../../../core/libs/prx/libSceJson2/Export.cpp) (libSceJson2) - callback dispatch before conversion is not implemented. The system library invokes the applicable type mismatch handler before converting a non-boolean value, with SDK-dependent global callback handling. @@ -81,10 +82,3 @@ - [_ZNK3sce4Json14InitParameter217getFileBufferSizeEv](../../../core/libs/prx/libSceJson2/InventoryExports.cpp) (libSceJson2) - NID `Pcg6xQUhq9E` from the PS5 3.20 export inventory; prototype and behavior are unverified. Declaration is for import resolution only. - [_ZNK3sce4Json14InitParameter225getSpecialFloatFormatTypeEv](../../../core/libs/prx/libSceJson2/InventoryExports.cpp) (libSceJson2) - NID `PDkib0zyABg` from the PS5 3.20 export inventory; prototype and behavior are unverified. Declaration is for import resolution only. - [_ZNK3sce4Json5ValueixERKNS0_6StringE](../../../core/libs/prx/libSceJson2/InventoryExports.cpp) (libSceJson2) - NID `clF7J7N9xXE` from the PS5 3.20 export inventory; prototype and behavior are unverified. Declaration is for import resolution only. -- [_ZNK3sce4Json6String2atEm](../../../core/libs/prx/libSceJson2/InventoryExports.cpp) (libSceJson2) - NID `KrKM-5SdQrA` from the PS5 3.20 export inventory; prototype and behavior are unverified. Declaration is for import resolution only. -- [_ZNK3sce4Json6String4findEPKcm](../../../core/libs/prx/libSceJson2/InventoryExports.cpp) (libSceJson2) - NID `yjsf6SvXA08` from the PS5 3.20 export inventory; prototype and behavior are unverified. Declaration is for import resolution only. -- [_ZNK3sce4Json6String4findEPKcmm](../../../core/libs/prx/libSceJson2/InventoryExports.cpp) (libSceJson2) - NID `wwDnDDsQVc0` from the PS5 3.20 export inventory; prototype and behavior are unverified. Declaration is for import resolution only. -- [_ZNK3sce4Json6String4findERKS1_m](../../../core/libs/prx/libSceJson2/InventoryExports.cpp) (libSceJson2) - NID `awkeNR6Ukp8` from the PS5 3.20 export inventory; prototype and behavior are unverified. Declaration is for import resolution only. -- [_ZNK3sce4Json6String5rfindEPKcm](../../../core/libs/prx/libSceJson2/InventoryExports.cpp) (libSceJson2) - NID `VpQBXbaHXOo` from the PS5 3.20 export inventory; prototype and behavior are unverified. Declaration is for import resolution only. -- [_ZNK3sce4Json6String5rfindEPKcmm](../../../core/libs/prx/libSceJson2/InventoryExports.cpp) (libSceJson2) - NID `A29RHrlfTM0` from the PS5 3.20 export inventory; prototype and behavior are unverified. Declaration is for import resolution only. -- [_ZNK3sce4Json6String5rfindERKS1_m](../../../core/libs/prx/libSceJson2/InventoryExports.cpp) (libSceJson2) - NID `-R9mdXBspeQ` from the PS5 3.20 export inventory; prototype and behavior are unverified. Declaration is for import resolution only. diff --git a/docs/dev/debt/libSceLibcInternal.md b/docs/dev/debt/libSceLibcInternal.md index 0339d848d4..b1cb33da7a 100644 --- a/docs/dev/debt/libSceLibcInternal.md +++ b/docs/dev/debt/libSceLibcInternal.md @@ -7,6 +7,7 @@ ### Functional +- [std::filesystem::_Xfilesystem_error](../../../core/libs/prx/libSceLibcInternal/Filesystem.cpp) (libSceLibcInternal, NIDs `B0CeIhQty7Y` and `C6-7Mo5WbwU`) - both overloads still throw `NotImplemented`: on the console they throw `std::filesystem::filesystem_error`, whose guest type information and layout this library does not provide yet. The filesystem primitives beside them follow the PS5 13.60 module - [makecontext](../../../core/libs/prx/libSceLibcInternal/UserContext.cpp) (libSceLibcInternal) follows FreeBSD 11's `lib/libc/amd64/gen/makecontext.c`: at most six arguments, a stack of at least `MINSIGSTKSZ` (2048) bytes, and `exit(0)` when the function returns with no `uc_link`. The console's version is not verified - [makecontext](../../../core/libs/prx/libSceLibcInternal/UserContext.cpp) (libSceLibcInternal) on Windows pins its stack writable until a later `makecontext` stack overlaps it, since nothing tells the library when the guest frees the stack - [Thread exit destructors](../../../core/libs/prx/libSceLibcInternal/Export.cpp) (libSceLibcInternal) run when a libkernel thread finishes; on the main or a host thread only `_sceLibcInternalThreadDtors` runs them, as `exit` does, else they are dropped diff --git a/docs/dev/debt/libSceLibreSsl.md b/docs/dev/debt/libSceLibreSsl.md index 29a95f22e3..f0d6d1c3bb 100644 --- a/docs/dev/debt/libSceLibreSsl.md +++ b/docs/dev/debt/libSceLibreSsl.md @@ -2,8 +2,9 @@ ### Unknown function info -- [libSceLibreSsl pending exports](../../../core/libs/prx/libSceLibreSsl/Export.cpp) - 365 named callable exports are listed in the [PS5 3.20 export table](https://github.com/DNNDHH/PS5-3.20_Libs/blob/b2a77c09cd71c01f34c790cfbe98c751a13262ad/libSceLibreSsl.c) and were cross-checked against the same module in PS5 firmware 13.60. Their signatures and behaviour are unverified; the declarations use the no-parameter unknown-signature convention and throw through `NotImplemented_nid_no_patch`. +- [libSceLibreSsl pending exports](../../../core/libs/prx/libSceLibreSsl/Export.cpp) - 357 named callable exports are listed in the [PS5 3.20 export table](https://github.com/DNNDHH/PS5-3.20_Libs/blob/b2a77c09cd71c01f34c790cfbe98c751a13262ad/libSceLibreSsl.c) and were cross-checked against the same module in PS5 firmware 13.60. Their signatures and behaviour are unverified; the declarations use the no-parameter unknown-signature convention and throw through `NotImplemented_nid_no_patch`. - [MD4, MD5, SHA1, SHA224, SHA256, SHA384 and SHA512 Init/Update/Final and SHA256](../../../core/libs/prx/libSceLibreSsl/src/Digest.cpp) (libSceLibreSsl) - signatures and context layouts follow the [LibreSSL headers](https://github.com/libressl/openbsd/blob/bc9483725c5815efb64b6d9acc52fc5fba139212/src/lib/libcrypto/sha/sha.h), and buffering and `Final` follow its [sources](https://github.com/libressl/openbsd/tree/bc9483725c5815efb64b6d9acc52fc5fba139212/src/lib/libcrypto/sha); the module's LibreSSL version is unknown and nothing is checked on the console. Behaviour that differs between LibreSSL versions throws: `SHA256` with a null digest buffer (older versions return a static buffer) and `SHA512_Final` with the SHA-512/224 or SHA-512/256 digest length. Null contexts, null input with a nonzero length, null digest buffers in the 32-bit `Final` functions and a buffered byte count that is not below the block size, which LibreSSL does not check, throw - [DES_set_key, DES_ecb_encrypt, DES_set_odd_parity](../../../core/libs/prx/libSceLibreSsl/Export.cpp) (libSceLibreSsl) - the 128-byte `DES_key_schedule` holds the 8-byte key followed by zeros instead of LibreSSL's precomputed subkeys, so a title reading its contents sees different bytes; `DES_ecb_encrypt` derives the subkeys through mbedtls on each call. Not compared with the console. Results follow LibreSSL [des_key.c](https://github.com/libressl/openbsd/blob/bc9483725c5815efb64b6d9acc52fc5fba139212/src/lib/libcrypto/des/des_key.c) and [des.c](https://github.com/libressl/openbsd/blob/bc9483725c5815efb64b6d9acc52fc5fba139212/src/lib/libcrypto/des/des.c): `DES_set_key` skips the parity and weak-key checks and returns 0, as it does while `DES_check_key` is 0 (that variable is not in the PS5 export inventory), a nonzero `enc` encrypts, and `DES_set_odd_parity` sets each byte's low bit for odd parity. Null pointers throw where LibreSSL faults - [X509_verify_cert_error_string](../../../core/libs/prx/libSceLibreSsl/Export.cpp) - no console call is verified. The 64-bit argument, low-32-bit lookup, messages and shared numeric fallback follow [LibreSSL 2.5.4](https://github.com/libressl/openbsd/blob/2e2c612066aa993717528a0249c15c51128d61cb/src/lib/libcrypto/x509/x509_txt.c), the version identified in [published firmware 13.40 measurements](https://github.com/boykopovar/AnyPS5/discussions/8981#discussioncomment-19111214). Console modifications and other firmware versions remain unverified. Unknown-code results share an overwritten static buffer and are not safe for concurrent use, as in that release; code 1 and codes above 54 use the numeric fallback. - [RAND_add, RAND_bytes, RAND_status, RAND_load_file, RAND_file_name, CRYPTO_malloc, CRYPTO_free, CRYPTO_set_mem_functions](../../../core/libs/prx/libSceLibreSsl/src/Runtime.cpp) (libSceLibreSsl) - follow LibreSSL's [rand_lib.c, randfile.c](https://github.com/libressl/openbsd/tree/0f0b16dc977f6ca499f0f4540078d992c96da597/src/lib/libcrypto/rand) and [malloc-wrapper.c](https://github.com/libressl/openbsd/blob/0f0b16dc977f6ca499f0f4540078d992c96da597/src/lib/libcrypto/malloc-wrapper.c), which are the same in 2.6.5 (3.4.3 linked); nothing is checked on the console. `RAND_bytes` reads host entropy and `CRYPTO_malloc` and `CRYPTO_free` use the guest heap. `CRYPTO_malloc` returns null for a size of zero or less, as LibreSSL 2.5.4 to 3.4 do; later versions take a `size_t` size and call `malloc`. A null buffer with a positive length throws +- [AES_set_encrypt_key, AES_set_decrypt_key, AES_encrypt, AES_decrypt, AES_ecb_encrypt, AES_cbc_encrypt, AES_cfb128_encrypt, AES_ctr128_encrypt](../../../core/libs/prx/libSceLibreSsl/src/Aes.cpp) (libSceLibreSsl) - follow LibreSSL's [aes.h](https://github.com/libressl/openbsd/blob/bc9483725c5815efb64b6d9acc52fc5fba139212/src/lib/libcrypto/aes/aes.h) and [mode code](https://github.com/libressl/openbsd/tree/bc9483725c5815efb64b6d9acc52fc5fba139212/src/lib/libcrypto/modes). Nothing is checked on the console. Key setup follows [LibreSSL 2.5.4's aes_core.c](https://github.com/libressl/openbsd/blob/2e2c612066aa993717528a0249c15c51128d61cb/src/lib/libcrypto/aes/aes_core.c), the version the `X509_verify_cert_error_string` entry names: a null pointer returns -1 and a key length other than 128, 192 or 256 bits returns -2 before the key is written, and only the 44, 52 or 60 words the key length needs are written. The `AES_KEY` schedule uses that file's generic C layout (big-endian words, `rounds` 10, 12 or 14). The 2.5.4 amd64 build uses [aes-x86_64](https://github.com/libressl/openbsd/blob/2e2c612066aa993717528a0249c15c51128d61cb/src/lib/libcrypto/aes/asm/aes-x86_64.pl) instead, which copies the key bytes without swapping, so each word is byte-reversed compared with the C layout (OpenSSL 3.0.13's x86-64 code gives the same schedules byte-reversed); the console's build options are unknown, so a schedule the title reads or fills is unverified. CTR leaves the last keystream block in `ecount_buf` even at a block boundary. Null pointers in the cipher and mode functions, other `rounds` values and a CFB or CTR position outside 0-15 throw diff --git a/docs/dev/debt/libSceNet.md b/docs/dev/debt/libSceNet.md index b9ad83d736..611efe6936 100644 --- a/docs/dev/debt/libSceNet.md +++ b/docs/dev/debt/libSceNet.md @@ -2,14 +2,18 @@ ### Silent stubs +- [sceNetShowRouteWithMemory](../../../core/libs/prx/libSceNet/Export.cpp) (libSceNet) - validates memory pool ID and returns success without printing anything: the `(poolId)` signature and pool validation follow [shadPS4](https://github.com/shadps4-emu/shadPS4/blob/main/src/core/libraries/net/net.cpp) and `sceNetShowNetstatWithMemory`. What the console prints and where is unverified - [sceNetShowIfconfig, sceNetShowNetstat](../../../core/libs/prx/libSceNet/Export.cpp) return the native not-initialized error and set errno before initialization. Initialized calls throw because native diagnostic collection and formatting are not implemented. -- [sceNetShowRoute, sceNetShowRoute6](../../../core/libs/prx/libSceNet/Export.cpp) (libSceNet) - return success without printing anything: the `()` signatures and the success return follow [shadPS4](https://github.com/shadps4-emu/shadPS4/blob/main/src/core/libraries/network/net.cpp), which stubs each the same way. PPSA01530 fails to load without the `sceNetShowIfconfig` export (#3177); PPSA11386 imports `sceNetShowNetstat` but does not call it during start-up. What the console prints and where is unverified +- [sceNetShowPolicy, sceNetShowNetstatEx, sceNetShowRoute, sceNetShowRoute6](../../../core/libs/prx/libSceNet/Export.cpp) (libSceNet) - return success without printing anything: the `()` signatures and the success return follow [shadPS4](https://github.com/shadps4-emu/shadPS4/blob/main/src/core/libraries/network/net.cpp), which stubs each the same way. PPSA01530 fails to load without the `sceNetShowIfconfig` export (#3177); PPSA11386 imports `sceNetShowNetstat` but does not call it during start-up. What the console prints and where is unverified ### Unknown function info +- [sceNetRecv, sceNetRecvfrom and sceNetRecvmsg](../../../core/libs/prx/libSceNet/Export.cpp) (libSceNet) - zero-length TCP receives while connecting, after disconnect, or with a pending host error still use the host receive path. Error and output precedence in those states is unmeasured on a console. The established, error-free case follows FreeBSD 11.4's [`soreceive_generic`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_socket.c#L1568-L1583). + - [sceNetConfigAddIfaddr](../../../core/libs/prx/libSceNet/Export.cpp) (libSceNet) - disassembly confirms the same interface/address/broadcast/netmask arguments as SetIfaddr, entering its shared handler with add mode selected. GetIfaddr copies up to two 16-byte interface-address records. GetRouteInfo uses a family, record buffer, signed capacity and byte flags; internal statistics output is 16 bytes. The address-record and internal-statistics fields remain incomplete. -- [Resolver-connect, thread creation, interface and diagnostic queries, WLAN configuration and DHCP queries](../../../core/libs/prx/libSceNet/Export.cpp) retain unsupported implementations. Their restored argument lists still reference incomplete connection, thread, interface, route, WLAN and statistics types. Kernel ioctl payloads need reconstruction from their handlers or consumers; buffer lengths alone do not establish field types. +- [Resolver-connect, interface and diagnostic queries, WLAN configuration and DHCP queries](../../../core/libs/prx/libSceNet/Export.cpp) retain unsupported implementations. Their restored argument lists still reference incomplete connection, interface, route, WLAN and statistics types. Kernel ioctl payloads need reconstruction from their handlers or consumers; buffer lengths alone do not establish field types. +- [sceNetThreadCreate, sceNetThreadExit, sceNetThreadJoin, sceNetEmulationGet and sceNetEmulationSet](../../../core/libs/prx/libSceNet/Export.cpp) (libSceNet) - the signatures, the `{ entry, arg, reserved, flags }` parameter block and the `EOPNOTSUPP` of the two emulation calls ("not available on retail units") follow [shadPS4](https://github.com/shadps4-emu/shadPS4/blob/a808e76/src/core/libraries/net/net.cpp) (PS4), not a PS5 measurement. The block is read on the new thread, so it must stay valid until the thread starts, `flags` is ignored (shadPS4 ends the thread with `pthread_exit` when bit 0 is set, which ends it the same way), and a failing `scePthreadCreate` or `scePthreadJoin` result is returned as it is, where shadPS4 also sets the net errno from it - [sceHttp2GetMemoryPoolStats, sceSslGetMemoryPoolStats, sceNetGetMemoryPoolStats](../../../core/libs/prx/libSceNet/Export.cpp) (libSceHttp2, libSceSsl, libSceNet) - the signatures `(int id, stats*)`, the layouts (pool size, peak and current use, and for Http2 and Ssl a 32-bit reserved field) and the error codes were measured on a PS5 (firmware 13.60). The pool size is the one given at init or pool creation. Http2 and Net allocate nothing from these pools. Ssl counts its certificate backend allocations and returned data against its pool; C++ bookkeeping is outside that budget. All three initially report zero use, where the console already reports 0x15C0 bytes for Ssl, 0x1640 for Http2 and 4 for a Net pool. Ssl's allocation sizes and peak usage do not reproduce the console allocator - [sceNetClearDnsCache](../../../core/libs/prx/libSceNet/Export.cpp) (libSceNet) - the library has no DNS cache to clear because lookups use the host resolver. The host resolver cache is left unchanged. - [sceNetEtherStrton](../../../core/libs/prx/libSceNet/Export.cpp) (libSceNet) - strings shorter than 17 bytes return EINVAL without changing the output, rather than reading past the terminating NUL as in the [firmware 13.60 probe](https://gist.github.com/orezraey/789290577123af3c45b340d6db6f395c/71e0a6215c7ee86f49fdb31de989dd2b50720b7a). Non-ASCII digit bytes throw because their decoding is unmeasured; other firmware versions remain unverified. Fixed character pairs, ignored separators, alphabetic values beyond f, non-alphanumeric ASCII values of 0x7f, and null-pointer EINVAL follow those measurements, not the strict [shadPS4 parser](https://github.com/shadps4-emu/shadPS4/blob/d615b3887ba29025a59242e1a26f5b68bb176158/src/core/libraries/net/net.cpp#L1274-L1297) @@ -17,7 +21,6 @@ - [sceNetGetRandom](../../../core/libs/prx/libSceNet/Export.cpp) (libSceNet) - generates values with the host random device; the system library maintains its own seeded PRNG state. - [sceNetInetPtonEx](../../../core/libs/prx/libSceNet/Export.cpp) (libSceNet) - for IPv6 a null `dst` returns `NET_EINVAL` here, where it crashed a payload process on a PS5 (firmware 13.60). The `(int af, const char* src, void* dst, int flags)` signature follows [shadPS4](https://github.com/shadps4-emu/shadPS4/blob/main/src/core/libraries/net/net.cpp). On PS4 firmware 10.71 and on that PS5, IPv4 text is read as `inet_aton` reads it (`a`, `a.b`, `a.b.c`, hex and octal parts, values wrapping at 32 bits, the text ending at a space, tab or VT), except that a part starting with `09` is decimal (`"0989"` is 989) and an octal part with `8` or `9` fails; flags were ignored (PS4: 0, single bits up to 0x100, 0x10000, 0x80000000, -1; PS5: 1, 2, 4, 0x100, 0x10000, -1). On the PS5 only: LF, FF and CR also end the text, a null `src` returns `NET_EINVAL`, and a null `dst` returns 1 or 0 without writing or setting errno. IPv6 converts as `sceNetInetPton` does - [sceNetMemoryAllocate and sceNetMemoryFree](../../../core/libs/prx/libSceNet/Export.cpp) (libSceNet) - allocations use the guest heap instead of the system network pool. Pool limits, its allocation accounting and free-time fill are not reproduced: a reused block allocated without flag 2 holds 0xdd bytes on the console and its previous contents here. Flag 4 bypasses the system pool reserve threshold; with no pool there is no threshold, so it allocates as other flags do. What the console returns once the pool is exhausted is unmeasured; AnyPS5 returns null without setting an errno. Measured on a PS5 (firmware 13.60): a size of 0 or less returns null and leaves `sceNetErrnoLoc` unchanged, flag 2 zero-fills, flags 1, 4, 8, 0x10, 0x100 and 0x80000000 had no visible effect, and every block of 1 to 65536 bytes was 64-byte aligned -- [sceNetThreadExit](../../../core/libs/prx/libSceNet/Export.cpp) is a zero-argument, non-returning void export in the native module. The protected host stub still declares int and contains `return 0`; changing it to void requires changing that protected body, so the return-type mismatch is retained. - [sceNetConfigCleanUpAllInterfaces](../../../core/libs/prx/libSceNet/Export.cpp) (libSceNet) - the native routine consumes no arguments; the throwing implementation remains unchanged. - [sceNetDhcpdStart](../../../core/libs/prx/libSceNet/Export.cpp): six arguments and the three-argument address callback are confirmed. The 24-byte parameter contains five network-order addresses and a client limit. Individual address roles and the two unused thread-option meanings remain unresolved. - [sceNetPppoeStart](../../../core/libs/prx/libSceNet/Export.cpp): seven arguments are confirmed, including the parameter pointer on the stack. The 272-byte parameter contains two DNS addresses, two 128-byte credential arrays, MTU and flags. Callback arguments are user data, event and error; the two unused thread-option meanings remain unresolved. @@ -26,6 +29,7 @@ ### Functional +- [sceNetRecv/sceNetRecvfrom](../../../core/libs/prx/libSceNet/Export.cpp) (libSceNet) translate Windows WSAEMSGSIZE into the copied byte count for datagram sockets, including MSG_PEEK, and retain the sender address. This follows FreeBSD 11.4 recv(2) and Winsock's documented truncated-datagram behavior; the console measurement covered only libkernel_sys `recv` and `recvfrom`, not libSceNet. - [sceNetUsleep](../../../core/libs/prx/libSceNet/Export.cpp) (libSceNet) prints nothing for a negative count, where the console prints `[LIBNET] usleep ` to standard output before failing with `EINVAL`. What an interrupted sleep returns is unmeasured: AnyPS5 returns the result of libkernel's `sceKernelUsleep`, a cancellation point. The signed 32-bit count (the upper half of the register is ignored) and the 0 result were measured on a PS5 (firmware 13.60) - [sceNetIoctl](../../../core/libs/prx/libSceNet/Export.cpp) (libSceNet) - on a `sceNetSocket` socket only `FIONBIO` and `FIONREAD` are handled and other requests fail with `ENOTTY`, as in libkernel's `ioctl`, though FreeBSD 11.4's [`soo_ioctl`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/kern/sys_socket.c#L156-L262) handles more; not measured on a console. [shadPS4](https://github.com/shadps4-emu/shadPS4/blob/60c42b925c3d1e00008621b8587819999260ec31/src/core/libraries/net/net.cpp#L1839) (PS4) returns `EINVAL` for other requests. `FIONBIO` sets `SO_NBIO`, assumed to be `soo_ioctl`'s `SS_NBIO`. `FIONREAD` asks the host, which on Linux counts only the next datagram where `soo_ioctl` counts all. Libkernel's own sockets go to its `ioctl`; other descriptors get `EBADF`. - [sceNetResolverStartAton6](../../../core/libs/prx/libSceNet/Export.cpp) (libSceNet) reverse-resolves a 16-byte IPv6 address through the host resolver (`getnameinfo` with `NI_NAMEREQD`) into the caller's buffer of `len` bytes, as `sceNetResolverStartAton` does for IPv4. The `(rid, addr, hostname, len, timeout, retry, flags)` signature comes from [SharpProspero](https://github.com/SvenGDK/SharpProspero/blob/5dfdb4b0b166e83dc9ae275001afb695f12c7174/src/SharpProspero/Interop/Net/Socket.cs#L992-L997). An address with no name, or a name longer than `len - 1` bytes, fails with `RESOLVER_ENODNS` (`0x804101E1`) and leaves the buffer untouched; whether the console truncates instead is not verified. Abort flag 2 (ATON preservation) fails the next call with `EINTR` as it does `sceNetResolverStartAton`. Timeout and retry are ignored, and any nonzero flags throw because their meaning is unknown @@ -40,6 +44,7 @@ - [sceNetGetSockInfo](../../../core/libs/prx/libSceNet/Export.cpp) (libSceNet) reports 0 for the name, pid, policy, priority, queue lengths, virtual ports, state, bandwidth, drops and waits, sets no status flag except `SELF` (0x1) and `NONBLOCK` (0x200000), and reports the host's buffer sizes where none was set. An IPv6 socket fails with `EAFNOSUPPORT` and is not counted or listed; shadPS4 lists it with zero addresses. `flags` bits other than 0x40 fail with `EINVAL`, where shadPS4 ignores `flags`; 0x40 adds sockets in TIME_WAIT ([SharpProspero](https://github.com/SvenGDK/SharpProspero/blob/5dfdb4b0b166e83dc9ae275001afb695f12c7174/src/SharpProspero/Interop/Net/Socket.cs)), but closed sockets are not kept. Otherwise the `s` below 0 count and list, the return value and `EINVAL` for a null `info` or an `n` below 1 follow [shadPS4](https://github.com/shadps4-emu/shadPS4/blob/06e813ff8c8e2a6d81b02cabfcc9543c3debf78e/src/core/libraries/net/net.cpp#L1498-L1522). Not measured on a console. - [SO_SNDBUF and SO_RCVBUF validation](../../../core/libs/prx/libSceNet/Export.cpp) remains unverified on PS5, and positive sizes remain subject to host buffer limits. Nonpositive sizes fail with `EINVAL` before changing the native socket, following FreeBSD 11.4 [sosetopt](https://github.com/freebsd/freebsd-src/blob/releng/11.4/sys/kern/uipc_socket.c#L2494-L2509). - [sceNetSendmsg and sceNetRecvmsg](../../../core/libs/prx/libSceNet/Export.cpp) (libSceNet) copy the scatter/gather list through one host buffer and accept only the flags `sceNetSend`/`sceNetRecv` accept. Sending control data throws; a receive never returns control data (`msg_controllen` 0), since no socket option that queues it is supported +- [sceNetGetSockInfo6](../../../core/libs/prx/libSceNet/Export.cpp) (libSceNet) is not measured on a console; shadPS4 stubs it. It lists only IPv6 sockets and `flags` 0x1000 adds IPv4 sockets, reported with the IPv4 addresses, all-zero IPv6 addresses and no `F_IPV6` (0x400000), which every IPv6 socket sets ([SharpProspero](https://github.com/SvenGDK/SharpProspero/blob/5dfdb4b0b166e83dc9ae275001afb695f12c7174/src/SharpProspero/Interop/Net/Socket.cs#L251-L343), [flags](https://github.com/SvenGDK/SharpProspero/blob/5dfdb4b0b166e83dc9ae275001afb695f12c7174/src/SharpProspero/Interop/Net/Socket.cs#L698-L705), [docs](https://github.com/SvenGDK/SharpProspero/blob/5dfdb4b0b166e83dc9ae275001afb695f12c7174/docs/net.md#L301-L304)). The default of leaving IPv4 sockets out, the zero IPv6 addresses of an IPv4 socket and accepting 0x40 are inferred from those names, not documented. An IPv4 socket without 0x1000 fails with `EAFNOSUPPORT`. The other gaps are those of `sceNetGetSockInfo`. - [sceNetSocketClose, sceNetEpollControl and sceNetEpollWait](../../../core/libs/prx/libSceNet/Export.cpp) (libSceNet) - close and concurrent registration ordering are unmeasured on a console. Closed descriptors retire their registrations, and a wait discards snapshots invalidated by close, DEL or MOD; descriptor identity follows [Linux epoll(7)](https://man7.org/linux/man-pages/man7/epoll.7.html). ADD, MOD and DEL reject closed or invalid socket descriptors with `EBADF`, as [Linux epoll_ctl(2)](https://man7.org/linux/man-pages/man2/epoll_ctl.2.html) does; this error ordering is unmeasured on a console. Duplicate-descriptor alias lifetime is not modeled by this epoll backend. - [MSG_DONTWAIT](../../../core/libs/prx/libSceNet/Export.cpp) (libSceNet) - console ordering between competing readers and writers is unmeasured. The per-call behavior follows FreeBSD 11.4's `soreceive_generic` and `sosend_generic` (`sys/kern/uipc_socket.c`); `0x80` is FreeBSD's flag value (`sys/sys/socket.h`). On Windows native sockets stay non-blocking. `MSG_DONTWAIT` and guest `SO_NBIO` return one host transfer's result. Blocking stream sends queue the whole buffer with one deadline and return progress after a timeout or host error. SSL uses this path through the native owner, retaining the socket, its mode and timeouts. Readiness waits poll at most every 10 ms for local close, so idle blocked operations wake periodically. - [sceNetRecv, sceNetRecvfrom and sceNetRecvmsg](../../../core/libs/prx/libSceNet/Export.cpp) (libSceNet) on a datagram socket with no local address wait on Windows and macOS until a bind, connect or send gives it one, checking the host socket every 10 ms. Winsock fails such a receive with `WSAEINVAL`, where FreeBSD 11.4's `soreceive_dgram` waits for a datagram (`sys/kern/uipc_socket.c`). On macOS the host receive waits, but a receive-side `shutdown` does not wake it. A non-blocking socket fails with `EAGAIN` at once, and the receive timeout ends the wait with `EAGAIN`; when the address comes first, the call waits for a datagram only for what is left of the timeout. A `sceNetShutdown` of the receive side ends the wait and the receive returns 0, and a zero-length receive returns 0 without waiting, as `soreceive_dgram` does when the socket can't receive more or nothing was asked for. An abort or close ends the wait, and the host receive then fails as it did before diff --git a/docs/dev/debt/libSceNetCtl.md b/docs/dev/debt/libSceNetCtl.md index aaa9cf2f9d..add05f78df 100644 --- a/docs/dev/debt/libSceNetCtl.md +++ b/docs/dev/debt/libSceNetCtl.md @@ -2,7 +2,7 @@ ### Functional -- [sceNetCtlGetNatInfo and sceNetCtlGetResult](../../../core/libs/prx/libSceNetCtl/Export.cpp) - NAT discovery and service event results are not implemented and throw after native initialization and argument checks. NAT information is 16 bytes with a 32-bit size prefix; the remaining fields are unresolved. +- [sceNetCtlGetNatInfo](../../../core/libs/prx/libSceNetCtl/Export.cpp) - NAT discovery is not implemented and throws after native initialization and argument checks. NAT information is 16 bytes with a 32-bit size prefix; the remaining fields are unresolved. ### Unknown function info @@ -10,7 +10,9 @@ - [sceNetCtlGetInfo](../../../core/libs/prx/libSceNetCtl/Export.cpp) (libSceNetCtl) - answers from the host interface that holds an IPv4 address, preferring the one that carries the default route: its address, netmask, gateway, MTU and first two IPv4 DNS servers. The device is reported as wired, the link as connected, the IP configuration as DHCP and the HTTP proxy as off, whatever the host uses, and the Ethernet address is the one `sceNetGetMacAddress` reports. A gateway or DNS server the interface does not have is reported as `0.0.0.0`; what a console reports then is unknown. The DHCP host name (12), the PPPoE user name (13), the HTTP proxy server (20) and the Wi-Fi BSSID and SSID (5, 6) are empty strings, as that configuration sets none of them; the remaining Wi-Fi codes (7-10) and the HTTP proxy port (21) report 0; this follows [KytyPS5](https://github.com/KytyPS5/KytyPS5/blob/main/src/libs/network.cpp) and is not measured on hardware - [sceNetCtlGetInfoV6](../../../core/libs/prx/libSceNetCtl/Export.cpp) (libSceNetCtl, NID `Jy1EO5GdlcM`) - imported by PPSA08587. `(int code, SceNetCtlInfoV6* info)` with codes 1-5 (IP address, link-local address, default route, primary and secondary DNS) comes only from [SharpProspero](https://github.com/SvenGDK/SharpProspero), whose source is unknown; shadPS4 and fpPS4 only stub it. A null `info` answers `INVALID_ADDR`, a code outside 1-5 `INVALID_CODE`, and a host without an IPv6 address `NOT_CONNECTED`, as `sceNetCtlGetStateV6` reports it. The layout of each answer is unverified, so every code throws on a host with an IPv6 address +- [sceNetCtlGetResult](../../../core/libs/prx/libSceNetCtl/Export.cpp) (libSceNetCtl) - `(int eventType, int* errorCode)` reports no error: event types 1-3 (disconnected to IP obtained, the numbering `sceNetCtlCheckCallback` delivers) write 0 to `errorCode`, as `sceNetCtlGetResultV6` does. The error code a console reports for each event, a disconnection's cause in particular, is unconfirmed. Callers found: PPSA20052 (Worms Armageddon: Anniversary Edition 01.003.000) calls it 2 s into boot, and PPSA24191 (Monster Hunter Wilds) without a network, with event 1 (disconnected) as its first call. A null `errorCode` answers `INVALID_ADDR` and other event types throw, as their error code on a console is unknown - [sceNetCtlGetResultV6](../../../core/libs/prx/libSceNetCtl/Export.cpp) (libSceNetCtl, NID `H5yARg37U5g`) - `(int eventType, int* errorCode)` comes only from [SharpProspero](https://github.com/SvenGDK/SharpProspero/blob/5dfdb4b0b166e83dc9ae275001afb695f12c7174/src/SharpProspero/Interop/Net/NetCtl.cs#L246-L251), whose source is unknown; shadPS4 only stubs it. Callbacks registered with `sceNetCtlRegisterCallbackV6` receive no event, so event types 1-3 write 0 to `errorCode`; what a console writes before the first event is unmeasured. A null `errorCode` answers `INVALID_ADDR`, as `sceNetCtlGetResult` does, and other event types throw, as their error code on a console is unknown +- [sceNetCtlRegisterCallbackForNpToolkit, sceNetCtlUnregisterCallbackForNpToolkit, sceNetCtlCheckCallbackForNpToolkit](../../../core/libs/prx/libSceNetCtl/Export.cpp) (libSceNetCtl) - a table of eight callbacks apart from the IPv4 and IPv6 ones, with the `(int eventType, void* arg)` callback, follows [shadPS4](https://github.com/shadps4-emu/shadPS4/blob/main/src/core/libraries/network/net_ctl_obj.h), whose NpToolkit check delivers the same events to that table. The three share the register, unregister and dispatch of `sceNetCtlRegisterCallback`, `sceNetCtlUnregisterCallback` and `sceNetCtlCheckCallback`, so they return the same initialization, address, slot-limit and ID errors, and the check reports the state `sceNetCtlCheckCallback` does. Whether the console keeps this table apart, and what `sceNetCtlCheckCallback` does with it, is unverified on hardware - [ApRp exports](../../../core/libs/prx/libSceNetCtl/Export.cpp) are constant-success functions in the supplied native module and consume no arguments. No callers were found in the supplied system-library decompilations, so unused callback, output and configuration argument types remain unresolved. Protected stub bodies are unchanged. ### Functional diff --git a/docs/dev/debt/libSceNetCtlAp.md b/docs/dev/debt/libSceNetCtlAp.md index 94b54af330..59dab4dfd7 100644 --- a/docs/dev/debt/libSceNetCtlAp.md +++ b/docs/dev/debt/libSceNetCtlAp.md @@ -6,6 +6,8 @@ ### Unknown function info +- [sceNetCtlApRegisterCallback](../../../core/libs/prx/libSceNetCtlAp/Export.cpp) (libSceNetCtlAp) - writing `InvalidCid` (`-1`) to `cid` when all eight slots are taken (`CALLBACK_MAX`) is unmeasured and interprets [SharpProspero's comment](https://github.com/SvenGDK/SharpProspero/blob/5dfdb4b0b166e83dc9ae275001afb695f12c7174/src/SharpProspero/Interop/Net/NetCtlAp.cs#L117-L118) that it is the value written into an unclaimed callback id, which does not name this path. [`libSceNetCtl`](https://github.com/boykopovar/AnyPS5/blob/aeeaefad98613c63a49ea2daae4d4558304b14cd/core/libs/prx/libSceNetCtl/Export.cpp#L103-L115) and [shadPS4](https://github.com/shadps4-emu/shadPS4/blob/396c2a09a38d22eaa02aab32c1967263dceab95f/src/core/libraries/network/netctl.cpp#L381-L390) leave `cid` unchanged on a full table + - [sceNetCtlApInit, sceNetCtlApTerm](../../../core/libs/prx/libSceNetCtlAp/Export.cpp) (libSceNetCtlAp) - every other call returns `NOT_INITIALIZED` (`0x80412994`) before `sceNetCtlApInit` and after `sceNetCtlApTerm`, which also drops the registered callbacks. A second `sceNetCtlApInit` throws, as no error for it is documented - [sceNetCtlApGetInfo, sceNetCtlApGetConnectInfo, sceNetCtlApStop](../../../core/libs/prx/libSceNetCtlAp/Export.cpp) - stopped-state results come from a service on the console and remain unverified. Wrapper validation is recovered: initialization precedes arguments, the optional connection string is at most eight bytes, then output pointers and exact 152/184-byte sizes are checked. The service interpretation of the option string remains unresolved. - [sceNetCtlApGetResult](../../../core/libs/prx/libSceNetCtlAp/Export.cpp) - with no event delivered, the host writes zero; the service result before the first event remains unknown. Wrapper validation checks initialization, output pointer and event type in that order. Event types 1 through 4 and lowest-free callback IDs 0 through 7 are confirmed. diff --git a/docs/dev/debt/libSceNgs2.native.md b/docs/dev/debt/libSceNgs2.native.md index a49957e009..5997a09d48 100644 --- a/docs/dev/debt/libSceNgs2.native.md +++ b/docs/dev/debt/libSceNgs2.native.md @@ -21,12 +21,14 @@ - [NGS2 streaming PCM waveforms](../../../core/libs/prx/libSceNgs2.native/src/Voice.cpp) (libSceNgs2.native) - PPSA21402 streams its music by adding one block whose `num_samples` is the whole track and whose `data_size` is a 16 KiB first piece, with `CONTINUE`, and then adds each following piece with flag `0x2` (called `APPEND` here) and the same `num_samples`. No reference describes flag `0x2`. A 16-bit PCM block without skipped samples or repeats whose `num_samples` exceed its data is assumed to start such a stream; flag `0x2` is assumed to append the block's data to the last streaming block, ignoring its `num_samples`; the voice is assumed to output silence without advancing while it has played all appended data and `CONTINUE` is set; and the waveform is assumed to end at `num_samples` or, once `CONTINUE` is cleared, at the end of the appended data. `sceNgs2VoiceGetState` reports the bytes played as `decoded_data_size`, which the title uses to release its queued data, and the read position as `waveform_data`. Appending to a block that is not a stream, or other formats, skipped samples, repeats and partial frames, throw - [NGS2 matrix levels](../../../core/libs/prx/libSceNgs2.native/src/Voice.cpp) (libSceNgs2.native) - levels are clamped to -4..4 and NaN is kept, as the PS4 firmware 11.00 `libSceNgs2.sprx` stores them; not yet confirmed on the PS5 library. That library rejects a level count of 0 and keeps the matrix; the PS5 error code is unknown, so a count of 0 throws, except on a sampler voice without a format, where it clears the matrix (see NGS2 voices without a format). `sceNgs2VoiceRunCommands` is not in that library, so its matrix levels command is assumed to clamp the same way - [NGS2 sampler voice state flag EMPTY](../../../core/libs/prx/libSceNgs2.native/src/Render.cpp) (libSceNgs2.native) - a playing sampler voice that still accepts blocks (`CONTINUE`) and has played all queued blocks reports `INUSE|PLAYING|EMPTY` (0x20) and keeps playing, so blocks added later play on. PPSA18391 (engine: Clickteam Fusion) reads `state_flags & 0x22 != 2` from `sceNgs2VoiceGetState` as the end of a sound and only then reuses the voice. When the console sets and clears EMPTY, and whether it also reports it for other voice types, is unverified on hardware. +- [NGS2 reverb channel conversion](../../../core/libs/prx/libSceNgs2.native/src/Reverb.cpp) (libSceNgs2.native) - a reverb voice whose `num_input_channels` differs from `num_output_channels` (PPSA03747 sets up 2 inputs and 8 outputs) mixes its ports in the input layout and folds that to mono with the input count's gains, as an equal-count voice does, and runs the comb and allpass stages per output channel. The PS4 library's handling of the dry signal in this case is not checked: here it reaches the output layout through the default channel map, so a stereo input keeps its dry signal on the front left and right and the other outputs carry only the reverb - [NGS2 port matrix](../../../core/libs/prx/libSceNgs2.native/src/Render.cpp) (libSceNgs2.native) - a port without a matrix, or with a selected matrix whose levels were never set, mixes through a default channel map: mono reaches both stereo channels at -3 dB and the 5.1 and 7.1 centre, stereo folds to mono at -3 dB per side, and wider sources fold down with 0.5 and -3 dB gains. The map and the equivalence of an unset matrix with no matrix were measured from the PS4 firmware 11.00 `libSceNgs2.sprx`, not yet from the PS5 library. That library only accepts submixer and mastering voices with 1, 2, 6 or 8 channels, so the map into 3, 4, 5 or 7 channels throws. A matrix set with fewer levels than the destination needs still mixes only the rows it has - [NGS2 custom submixer rack](../../../core/libs/prx/libSceNgs2.native/src/Custom.cpp) (libSceNgs2.native, rack 0x4002) - UserFx2 process flags and setup/process/cleanup order follow only [KytyPS5](https://github.com/KytyPS5/KytyPS5), not a console measurement; layouts follow [shadPS4](https://github.com/shadps4-emu/shadPS4)'s `ngs2_custom.h`. A module control handler, unequal input and output channels, setup flags and default rack options throw. Module state is a separate buffer, not the `state_offset` slice of voice state; `sceNgs2VoiceGetState` throws for this rack. UserFx2 runs only on grains with input samples, so an effect tail stops with the input - [NGS2 custom UserFx](../../../core/libs/prx/libSceNgs2.native/src/Custom.cpp) (libSceNgs2.native) - the 48-byte process context, process flags, callback error handling and null-handler behavior retain the existing submixer callback model and are not console-verified. Non-null module options throw because their console error is unknown. UserFx on custom sampler rack 0x4001 throws: its parameter indexing and routing are not implemented. Module id 0x19, the 40-byte voice parameter (handler and three user-data words), parameter id `0x40001900 | index`, and lack of option and module state follow [fma-rc's PS5 12.70 module reading](https://github.com/boykopovar/AnyPS5/issues/8039#issuecomment-6319332943), including parameter dispatch at 0x25ec4. - [NGS2 custom Delay](../../../core/libs/prx/libSceNgs2.native/src/Custom.cpp) (libSceNgs2.native, 0x1d) - processing and defaults remain unverified on PS5. The [agreed compatibility path](https://github.com/boykopovar/AnyPS5/pull/9337#issuecomment-6351205140) preserves main's PCM pass-through for Void Terrarium (PPSA03061) until any Delay parameter is set; every `0x40001dxx` parameter throws. Only one-buffer custom submixers with no module state and a 20-byte option are accepted. Option type 0/1, taps 1..8, length 1..20000 and reserved 0 follow [fma-rc's 12.70 module reading](https://github.com/boykopovar/AnyPS5/issues/8039#issuecomment-6319332943). Other shapes and values throw because their console errors are unknown. This preserves host behavior, not console-equivalent Delay processing -- [NGS2 custom module ids](../../../core/libs/prx/libSceNgs2.native/src/Custom.cpp) (libSceNgs2.native) - Reverb (0x1b), PitchShift (0x20) and Convolution (0x23) are not processed: a rack that passes their creation checks throws. Gain (0x15) on custom sampler racks also throws. Per [fma-rc's 12.70 module reading](https://github.com/boykopovar/AnyPS5/issues/8039#issuecomment-6294323150), creation returns `0x804A8302` for a wrong non-zero state size, and `0x804A8013`, `0x804A8099` and `0x804A8098` for a bad Reverb option size or word and PitchShift word. Unknown: check order, errors for a Gain option, missing Reverb or PitchShift options, other PitchShift and Convolution option sizes, and Convolution's state (64 bytes from SDK 3.0). PPSA04540 uses 0x15, 0x19, 0x1b and 0x23 (#8039), PPSA35169 0x20 (#4246) +- [NGS2 custom module ids](../../../core/libs/prx/libSceNgs2.native/src/Custom.cpp) (libSceNgs2.native) - Reverb (0x1b), PitchShift (0x20) and Convolution (0x23) are not processed: a rack that passes their creation checks throws. Gain (0x15) on custom sampler racks also throws. Per [fma-rc's 12.70 module reading](https://github.com/boykopovar/AnyPS5/issues/8039#issuecomment-6294323150), creation returns `0x804A8302` for a wrong non-zero state size, and `0x804A8013`, `0x804A8099` and `0x804A8098` for a bad Reverb option size or word and PitchShift word. Unknown: check order, errors for a Gain option, missing Reverb or PitchShift options, other PitchShift and Convolution option sizes, and Convolution's state (64 bytes from SDK 3.0). PPSA04540 uses 0x15, 0x19, 0x1b and 0x23 (#8039), PPSA35169 0x20 (#4246) and PPSA33655 0x15, 0x19 and 0x1b - [NGS2 custom Gain module](../../../core/libs/prx/libSceNgs2.native/src/Custom.cpp) (libSceNgs2.native, rack 0x4002) - follows [fma-rc's reading of the 12.70 module](https://github.com/boykopovar/AnyPS5/issues/8039#issuecomment-6319332943): parameter `0x40001500 | module index` holds 37 linear levels, one per channel, default 0, and the module writes source × level into dest, ramping linearly over the grain from the previous level. Unverified: where the ramp starts and ends (here the grain's first sample is one step past the previous level and its last sample is at the new level), whether stopping or setting up the voice again resets the levels, and what the library does with a level outside -32 to 32 (here it throws). On a custom sampler rack, whose parameter index is one higher per fma-rc, Gain throws. A Gain module with an option throws: fma-rc reads Gain as taking none, and the error the library returns for one is unknown. The extra buffer is ignored, as fma-rc reads it +- [NGS2 custom mastering rack](../../../core/libs/prx/libSceNgs2.native/src/Rack.cpp) (libSceNgs2.native, rack 0x4003) - follows [fma-rc's reading of the 12.70 module](https://github.com/boykopovar/AnyPS5/issues/8039#issuecomment-6319332943): option size is 0x508, matching the custom submixer rack layout. Creation returns `0x804A8052` above 37 channels; channels 9 to 37 throw because voices hold at most 8 channels. Mastering racks with modules throw: attached modules and non-default option fields are unverified on PS5. Voice control parameters 0x40030000 (setup), 0x40030004 (gain) and 0x40030005 (output) configure channels, levels and destination buffer. `sceNgs2VoiceGetState` fills common voice state flags. PPSA10278 and PPSA04540 create rack 0x4003 without modules (#8039) - [NGS2 custom rack buffers and channels](../../../core/libs/prx/libSceNgs2.native/src/Custom.cpp) (libSceNgs2.native, racks 0x4001 and 0x4002) - racks with 2 to 4 buffers, custom submixers with 9 to 37 channels and submixer `max_inputs` other than 1 or 2 throw: how modules and ports route several buffers is unknown, and voices hold at most 8 channels. Per [fma-rc's reading of the 12.70 module](https://github.com/boykopovar/AnyPS5/issues/8039#issuecomment-6294323150), creation returns `0x804A830B` for a buffer count outside 1 to 4 or below the submixer's `max_inputs`, `0x804A8306` for a module source, extra or dest buffer id at or above the count, and `0x804A8052` for more than 37 submixer channels; the check order is not known. `sceNgs2RackQueryBufferSize` throws for these options, as no source says whether it checks them. A port buffer id other than 0 throws. PPSA04540 creates racks with up to 4 buffers and a 16-channel custom submixer (#8039) - [NGS2 reverb rack](../../../core/libs/prx/libSceNgs2.native/src/Reverb.cpp) (libSceNgs2.native, rack 0x2001) - follows the PS4 firmware 11.00 `libSceNgs2.sprx`, read and checked against its output at 11.025 to 48 kHz, including parameter changes during playback (differences at float rounding level); not yet compared with the PS5 library. Structure layouts come from [shadPS4](https://github.com/shadps4-emu/shadPS4)'s `ngs2_reverb.h` and the param ids 0x20010000 (setup) and 0x20010001 (I3DL2) from PPSA22520. Only the default `reverb_size` of 1 is handled, with its 14400-sample reflection buffer; other sizes and the library's half-rate mode throw. System sample rates below about 7.5 kHz, where an allpass delay would have no samples, throw. Setup only accepts equal input and output channel counts - [sceNgs2ReportRegisterHandler, sceNgs2ReportUnregisterHandler](../../../core/libs/prx/libSceNgs2.native/src/System.cpp) (libSceNgs2.native, NIDs `uBIN24Tv2MI`, `nPzb7Ly-VjE`) keep a registry of report handlers. Register rejects a null handler with an invalid argument and returns `INVALID_OUT_ADDRESS` for a null out handle, then stores the report type, handler and user data under a freshly allocated opaque handle; unregister removes and frees the matching record and throws for a handle that is not registered. The signatures (report type, handler, user data, out handle) and (report handle) follow [SharpProspero](https://github.com/SvenGDK/SharpProspero) and [shadPS4](https://github.com/shadps4-emu/shadPS4)'s `ngs2_report.h`, and the 24-byte `Ngs2ReportDataHeader` passed to a handler is its `OrbisNgs2ReportDataHeader`. No report is produced yet, so no registered handler is ever invoked and PPSA04540, which imports both and registers a CPU load handler (type 5), still receives none. diff --git a/docs/dev/debt/libSceNpMatching2.md b/docs/dev/debt/libSceNpMatching2.md index 15a5abb1e0..edbc279625 100644 --- a/docs/dev/debt/libSceNpMatching2.md +++ b/docs/dev/debt/libSceNpMatching2.md @@ -3,3 +3,4 @@ ### Functional - [sceNpMatching2Initialize, sceNpMatching2Terminate](../../../core/libs/prx/libSceNpMatching2/Export.cpp) (libSceNpMatching2) - only the library state is kept: initialization checks the parameter's `size` field (0x28, or 0x30 with an SSL pool) as [shadPS4](https://github.com/shadps4-emu/shadPS4/blob/81be949167d0060b08f26dc3e055a0b640d129c8/src/core/libraries/np/np_matching2/np_matching2.cpp) does and starts no thread; a second initialization returns `ALREADY_INITIALIZED` and a termination without one `NOT_INITIALIZED`. The pool, thread and SSL settings are not used: no matching server is reached. Not measured on a console +- [sceNpMatching2CreateContext and the context calls](../../../core/libs/prx/libSceNpMatching2/Export.cpp) (libSceNpMatching2) - offline only, not measured on a PS5: the checks follow [shadPS4](https://github.com/shadps4-emu/shadPS4/blob/91e6afa69b0a64b0afaa0281b80f8fbb0cd88136/src/core/libraries/np/np_matching2/np_matching2.cpp#L26-L349) and its [context manager](https://github.com/shadps4-emu/shadPS4/blob/91e6afa69b0a64b0afaa0281b80f8fbb0cd88136/src/core/libraries/np/np_matching2/np_matching2_internal.cpp#L1348-L1457) (a `0x28`-byte parameter, ten ids handed out round-robin). `sceNpMatching2ContextStart` returns `0x804101E2` (`SCE_NET_ERROR_RESOLVER_ETIMEDOUT`), which shadPS4 says was confirmed on a console disconnected from the internet, so no context starts and `sceNpMatching2ContextStop` answers `CONTEXT_NOT_STARTED`. Callbacks and the default request option are checked but never used. The room, user, world, server-id and signaling-info calls still throw diff --git a/docs/dev/debt/libSceNpTrophy2.md b/docs/dev/debt/libSceNpTrophy2.md index e6cb5cd197..189c214f1d 100644 --- a/docs/dev/debt/libSceNpTrophy2.md +++ b/docs/dev/debt/libSceNpTrophy2.md @@ -2,16 +2,15 @@ ### Silent stubs +- [sceNpTrophy2GetRewardIcon](../../../core/libs/prx/libSceNpTrophy2/src/Context.cpp) (libSceNpTrophy2) - zeroes size and returns `SCE_NP_TROPHY2_ERROR_ICON_FILE_NOT_FOUND` (-2141898479): the `(int context, int handle, int reward_id, void* buffer, size_t* size)` signature and two-stage query pattern follow the bundled `Media/Plugins/PSNCore.prx` of PPSA06888 and SharpProspero; returning -2141898479 (0x80553911) when icons are unavailable is inferred from sibling calls (sceNpTrophy2GetGameIcon, sceNpTrophy2GetGroupIcon, sceNpTrophy2GetTrophyIcon) in KytyPS5 (libTrophy.cpp:464/483/502) - [libSceNpTrophy2](../../../core/libs/prx/libSceNpTrophy2/src/GameInfo.cpp): `sceNpTrophy2RegisterUnlockCallback` accepts a callback that is never called, as the callback's signature and when the console calls it are not known, `sceNpTrophy2UnregisterUnlockCallback` only returns success, and `sceNpTrophy2ShowTrophyList` returns success without showing a list (there is no system trophy UI; seen on PPSA15552). ### Unknown function info - [Query structures](../../../core/libs/prx/libSceNpTrophy2/include/NpTrophy2Types.hpp): a progress value is a 16-byte `{int32 type, 4 reserved bytes, uint64 value}` and the game, group and trophy structures are 152, 24, 152, 32, 1312 and 32 bytes, as [KytyPS5](https://github.com/KytyPS5/KytyPS5/blob/9e41d8320a2774b303fbad344c2347e8d0292e83/src/libs/libTrophy.cpp) asserts; no other public source confirms the layout. Progress type 1 marks a progressive trophy's target and progress, as in KytyPS5. Trophies without a `groupId` belong to group -1, KytyPS5's base game group. -- [sceNpTrophy2GetRewardIcon](../../../core/libs/prx/libSceNpTrophy2/src/Context.cpp) (libSceNpTrophy2) takes `(int context, int handle, int reward_id, void* buffer, size_t* size)`, from the call sites in the bundled `Media/Plugins/PSNCore.prx` of PPSA06888, the same shape as `sceNpTrophy2GetTrophyIcon`. The title calls it twice: first with a null buffer to read the size, then with a buffer of that size allocated with `malloc`. The meaning of the third argument is unverified, and the reward icon data is unknown, so every call throws - - - [libSceNpTrophy2 pending exports](../../../core/libs/prx/libSceNpTrophy2/Export.cpp) - 23 named callable exports were identified in the PS5 3.20 export table and cross-checked against firmware 13.60. Signatures and behavior remain unverified; declarations use the unknown-signature convention and throw through `NotImplemented_nid_no_patch`. Evidence: [3.20 export list](https://github.com/DNNDHH/PS5-3.20_Libs/blob/b2a77c09cd71c01f34c790cfbe98c751a13262ad/libSceNpTrophy2.c). + ### Functional - [Trophy queries](../../../core/libs/prx/libSceNpTrophy2/src/TrophyInfo.cpp) - not confirmed on the console: `sceNpTrophy2GetTrophyInfo` of a trophy id that is not in the title's [trophy data](../../../core/libs/prx/libSceNpTrophy2/src/TrophyPackage.cpp) (`sce_sys/trophy2/trophyNN.ucp`) returns `0x8055390A` (KytyPS5's `NP_TROPHY2_ERROR_INVALID_TROPHY_ID`) and writes nothing; a context not created or destroyed reports placeholders and no unlocks and returns success; hidden trophies report their names and descriptions before the unlock; `progress_percentage` weighs gold 6, silver 2 and bronze 1 and leaves the platinum out, as KytyPS5 does (the ratio of the published PSN trophy points). A group that is not in the data throws. Without the file, or when it is malformed, the queries report the old placeholders and `sceNpTrophy2GetTrophyInfoArray` lists only trophy 0. Icons are not read. Unlocks come from the [trophy store](../../../core/libs/prx/libSceNpTrophy2/src/TrophyStore.cpp). diff --git a/docs/dev/debt/libSceNpUniversalDataSystem.md b/docs/dev/debt/libSceNpUniversalDataSystem.md index 6c8a996278..2e07cb9901 100644 --- a/docs/dev/debt/libSceNpUniversalDataSystem.md +++ b/docs/dev/debt/libSceNpUniversalDataSystem.md @@ -4,6 +4,19 @@ - [Events](../../../core/libs/prx/libSceNpUniversalDataSystem/src/Event.cpp): `sceNpUniversalDataSystemCreateEvent` copies `prop` and returns the event's own property object through `prop_ptr`, as [KytyPS5](https://github.com/KytyPS5/KytyPS5/blob/9e41d8320a2774b303fbad344c2347e8d0292e83/src/libs/libTrophy.cpp) does; `sceNpUniversalDataSystemEventPropertyObjectSetObject`, `SetArray` and the array setters for objects and arrays store a copy of `value` and return the stored copy through `value_ptr`. Destroying a property object or array that its Create function did not return (an event's property object or a stored copy) does nothing, as its owner frees it. These ownership rules are not verified on a console, nor are the output of `sceNpUniversalDataSystemEventToString` (still `{}`) and `sceNpUniversalDataSystemEventEstimateSize` (still 3). +- [sceNpUniversalDataSystemIntCreateRecordArray, sceNpUniversalDataSystemIntCreateRecordData and sceNpUniversalDataSystemIntCreateRecordObject](../../../core/libs/prx/libSceNpUniversalDataSystem/PendingExports.cpp) (libSceNpUniversalDataSystem) - The list is the intersection of the export table of the console's libSceNpUniversalDataSystem (function symbols only), the pinned aerolib names and the functions not yet defined anywhere in the repo; names and NIDs are present in the [PS5 3.20 export inventory](https://github.com/DNNDHH/PS5-3.20_Libs/blob/b2a77c09cd71c01f34c790cfbe98c751a13262ad/libSceNpUniversalDataSystem.c) and the [verified NID name database](https://github.com/zecoxao/sce_symbols/blob/2883963a0a514ba6e77407a08ef4730a65e88254/aerolib.csv), but no credible prototypes were found. They are declared without parameters solely to resolve known imports; this is not an ABI claim and callers must not call them until signatures are verified. +- [sceNpUniversalDataSystemIntDeleteAllRecord](../../../core/libs/prx/libSceNpUniversalDataSystem/PendingExports.cpp) (libSceNpUniversalDataSystem) - It is in the intersection of the export table of the console's libSceNpUniversalDataSystem (function symbols only), the pinned aerolib names and the functions not yet defined anywhere in the repo; names and NIDs are present in the [PS5 3.20 export inventory](https://github.com/DNNDHH/PS5-3.20_Libs/blob/b2a77c09cd71c01f34c790cfbe98c751a13262ad/libSceNpUniversalDataSystem.c) and the [verified NID name database](https://github.com/zecoxao/sce_symbols/blob/2883963a0a514ba6e77407a08ef4730a65e88254/aerolib.csv), but no credible prototypes were found. It is declared without parameters solely to resolve known imports; this is not an ABI claim and callers must not call them until signatures are verified. +- [sceNpUniversalDataSystemIntDestroyRecordArray and sceNpUniversalDataSystemIntDestroyRecordData](../../../core/libs/prx/libSceNpUniversalDataSystem/PendingExports.cpp) (libSceNpUniversalDataSystem) - The list is the intersection of the export table of the console's libSceNpUniversalDataSystem (function symbols only), the pinned aerolib names and the functions not yet defined anywhere in the repo; names and NIDs are present in the [PS5 3.20 export inventory](https://github.com/DNNDHH/PS5-3.20_Libs/blob/b2a77c09cd71c01f34c790cfbe98c751a13262ad/libSceNpUniversalDataSystem.c) and the [verified NID name database](https://github.com/zecoxao/sce_symbols/blob/2883963a0a514ba6e77407a08ef4730a65e88254/aerolib.csv), but no credible prototypes were found. They are declared without parameters solely to resolve known imports; this is not an ABI claim and callers must not call them until signatures are verified. +- [sceNpUniversalDataSystemIntDestroyRecordObject and sceNpUniversalDataSystemIntGetMemoryStat](../../../core/libs/prx/libSceNpUniversalDataSystem/PendingExports.cpp) (libSceNpUniversalDataSystem) - The list is the intersection of the export table of the console's libSceNpUniversalDataSystem (function symbols only), the pinned aerolib names and the functions not yet defined anywhere in the repo; names and NIDs are present in the [PS5 3.20 export inventory](https://github.com/DNNDHH/PS5-3.20_Libs/blob/b2a77c09cd71c01f34c790cfbe98c751a13262ad/libSceNpUniversalDataSystem.c) and the [verified NID name database](https://github.com/zecoxao/sce_symbols/blob/2883963a0a514ba6e77407a08ef4730a65e88254/aerolib.csv), but no credible prototypes were found. They are declared without parameters solely to resolve known imports; this is not an ABI claim and callers must not call them until signatures are verified. +- [sceNpUniversalDataSystemIntPostRecordData, sceNpUniversalDataSystemIntRecordArraySetArray and sceNpUniversalDataSystemIntRecordArraySetBinary](../../../core/libs/prx/libSceNpUniversalDataSystem/PendingExports.cpp) (libSceNpUniversalDataSystem) - The list is the intersection of the export table of the console's libSceNpUniversalDataSystem (function symbols only), the pinned aerolib names and the functions not yet defined anywhere in the repo; names and NIDs are present in the [PS5 3.20 export inventory](https://github.com/DNNDHH/PS5-3.20_Libs/blob/b2a77c09cd71c01f34c790cfbe98c751a13262ad/libSceNpUniversalDataSystem.c) and the [verified NID name database](https://github.com/zecoxao/sce_symbols/blob/2883963a0a514ba6e77407a08ef4730a65e88254/aerolib.csv), but no credible prototypes were found. They are declared without parameters solely to resolve known imports; this is not an ABI claim and callers must not call them until signatures are verified. +- [sceNpUniversalDataSystemIntRecordArraySetBool, sceNpUniversalDataSystemIntRecordArraySetFloat32 and sceNpUniversalDataSystemIntRecordArraySetFloat64](../../../core/libs/prx/libSceNpUniversalDataSystem/PendingExports.cpp) (libSceNpUniversalDataSystem) - The list is the intersection of the export table of the console's libSceNpUniversalDataSystem (function symbols only), the pinned aerolib names and the functions not yet defined anywhere in the repo; names and NIDs are present in the [PS5 3.20 export inventory](https://github.com/DNNDHH/PS5-3.20_Libs/blob/b2a77c09cd71c01f34c790cfbe98c751a13262ad/libSceNpUniversalDataSystem.c) and the [verified NID name database](https://github.com/zecoxao/sce_symbols/blob/2883963a0a514ba6e77407a08ef4730a65e88254/aerolib.csv), but no credible prototypes were found. They are declared without parameters solely to resolve known imports; this is not an ABI claim and callers must not call them until signatures are verified. +- [sceNpUniversalDataSystemIntRecordArraySetInt32, sceNpUniversalDataSystemIntRecordArraySetInt64 and sceNpUniversalDataSystemIntRecordArraySetObject](../../../core/libs/prx/libSceNpUniversalDataSystem/PendingExports.cpp) (libSceNpUniversalDataSystem) - The list is the intersection of the export table of the console's libSceNpUniversalDataSystem (function symbols only), the pinned aerolib names and the functions not yet defined anywhere in the repo; names and NIDs are present in the [PS5 3.20 export inventory](https://github.com/DNNDHH/PS5-3.20_Libs/blob/b2a77c09cd71c01f34c790cfbe98c751a13262ad/libSceNpUniversalDataSystem.c) and the [verified NID name database](https://github.com/zecoxao/sce_symbols/blob/2883963a0a514ba6e77407a08ef4730a65e88254/aerolib.csv), but no credible prototypes were found. They are declared without parameters solely to resolve known imports; this is not an ABI claim and callers must not call them until signatures are verified. +- [sceNpUniversalDataSystemIntRecordArraySetString, sceNpUniversalDataSystemIntRecordArraySetUInt32 and sceNpUniversalDataSystemIntRecordArraySetUInt64](../../../core/libs/prx/libSceNpUniversalDataSystem/PendingExports.cpp) (libSceNpUniversalDataSystem) - The list is the intersection of the export table of the console's libSceNpUniversalDataSystem (function symbols only), the pinned aerolib names and the functions not yet defined anywhere in the repo; names and NIDs are present in the [PS5 3.20 export inventory](https://github.com/DNNDHH/PS5-3.20_Libs/blob/b2a77c09cd71c01f34c790cfbe98c751a13262ad/libSceNpUniversalDataSystem.c) and the [verified NID name database](https://github.com/zecoxao/sce_symbols/blob/2883963a0a514ba6e77407a08ef4730a65e88254/aerolib.csv), but no credible prototypes were found. They are declared without parameters solely to resolve known imports; this is not an ABI claim and callers must not call them until signatures are verified. +- [sceNpUniversalDataSystemIntRecordDataEstimateSize, sceNpUniversalDataSystemIntRecordObjectSetArray and sceNpUniversalDataSystemIntRecordObjectSetBinary](../../../core/libs/prx/libSceNpUniversalDataSystem/PendingExports.cpp) (libSceNpUniversalDataSystem) - The list is the intersection of the export table of the console's libSceNpUniversalDataSystem (function symbols only), the pinned aerolib names and the functions not yet defined anywhere in the repo; names and NIDs are present in the [PS5 3.20 export inventory](https://github.com/DNNDHH/PS5-3.20_Libs/blob/b2a77c09cd71c01f34c790cfbe98c751a13262ad/libSceNpUniversalDataSystem.c) and the [verified NID name database](https://github.com/zecoxao/sce_symbols/blob/2883963a0a514ba6e77407a08ef4730a65e88254/aerolib.csv), but no credible prototypes were found. They are declared without parameters solely to resolve known imports; this is not an ABI claim and callers must not call them until signatures are verified. +- [sceNpUniversalDataSystemIntRecordObjectSetBool, sceNpUniversalDataSystemIntRecordObjectSetFloat32 and sceNpUniversalDataSystemIntRecordObjectSetFloat64](../../../core/libs/prx/libSceNpUniversalDataSystem/PendingExports.cpp) (libSceNpUniversalDataSystem) - The list is the intersection of the export table of the console's libSceNpUniversalDataSystem (function symbols only), the pinned aerolib names and the functions not yet defined anywhere in the repo; names and NIDs are present in the [PS5 3.20 export inventory](https://github.com/DNNDHH/PS5-3.20_Libs/blob/b2a77c09cd71c01f34c790cfbe98c751a13262ad/libSceNpUniversalDataSystem.c) and the [verified NID name database](https://github.com/zecoxao/sce_symbols/blob/2883963a0a514ba6e77407a08ef4730a65e88254/aerolib.csv), but no credible prototypes were found. They are declared without parameters solely to resolve known imports; this is not an ABI claim and callers must not call them until signatures are verified. +- [sceNpUniversalDataSystemIntRecordObjectSetInt32, sceNpUniversalDataSystemIntRecordObjectSetInt64 and sceNpUniversalDataSystemIntRecordObjectSetObject](../../../core/libs/prx/libSceNpUniversalDataSystem/PendingExports.cpp) (libSceNpUniversalDataSystem) - The list is the intersection of the export table of the console's libSceNpUniversalDataSystem (function symbols only), the pinned aerolib names and the functions not yet defined anywhere in the repo; names and NIDs are present in the [PS5 3.20 export inventory](https://github.com/DNNDHH/PS5-3.20_Libs/blob/b2a77c09cd71c01f34c790cfbe98c751a13262ad/libSceNpUniversalDataSystem.c) and the [verified NID name database](https://github.com/zecoxao/sce_symbols/blob/2883963a0a514ba6e77407a08ef4730a65e88254/aerolib.csv), but no credible prototypes were found. They are declared without parameters solely to resolve known imports; this is not an ABI claim and callers must not call them until signatures are verified. +- [sceNpUniversalDataSystemIntRecordObjectSetString, sceNpUniversalDataSystemIntRecordObjectSetUInt32 and sceNpUniversalDataSystemIntRecordObjectSetUInt64](../../../core/libs/prx/libSceNpUniversalDataSystem/PendingExports.cpp) (libSceNpUniversalDataSystem) - The list is the intersection of the export table of the console's libSceNpUniversalDataSystem (function symbols only), the pinned aerolib names and the functions not yet defined anywhere in the repo; names and NIDs are present in the [PS5 3.20 export inventory](https://github.com/DNNDHH/PS5-3.20_Libs/blob/b2a77c09cd71c01f34c790cfbe98c751a13262ad/libSceNpUniversalDataSystem.c) and the [verified NID name database](https://github.com/zecoxao/sce_symbols/blob/2883963a0a514ba6e77407a08ef4730a65e88254/aerolib.csv), but no credible prototypes were found. They are declared without parameters solely to resolve known imports; this is not an ABI claim and callers must not call them until signatures are verified. + ### Functional - [Trophy events](../../../core/libs/prx/libSceNpUniversalDataSystem/src/Event.cpp): `sceNpUniversalDataSystemPostEvent` stores an `_UnlockTrophy` event with an int32 `_trophy_id` of 0 or more as an unlock for the user and service label of its context, as KytyPS5 does, except an id that [libSceNpTrophy2](../../../core/libs/prx/libSceNpTrophy2/src/TrophyStore.cpp) refuses when the title's trophy data is loaded (one not in the data, a platinum or a progressive trophy). `_UpdateTrophyProgress`, named only by a third-party unlocker, is logged once per trophy and not interpreted. A context need not be registered, and every post of an event that `sceNpUniversalDataSystemCreateEvent` returned and that is not yet destroyed returns success (a null or any other event pointer stops the game): KytyPS5's errors for an unknown or unregistered context and for a malformed unlock are not verified. The storage statistics stay zero. diff --git a/docs/dev/debt/libSceNpUtility.md b/docs/dev/debt/libSceNpUtility.md index b1cd969543..642d6ab2bd 100644 --- a/docs/dev/debt/libSceNpUtility.md +++ b/docs/dev/debt/libSceNpUtility.md @@ -1,5 +1,22 @@ # libSceNpUtility technical debt +### Unknown function info + +- [sceNpAppInfoIntAbortRequest, sceNpAppInfoIntCheckAvailability, sceNpAppInfoIntCheckAvailabilityA, sceNpAppInfoIntCheckAvailabilityAll, sceNpAppInfoIntCheckAvailabilityAllA and sceNpAppInfoIntCheckServiceAvailability](../../../core/libs/prx/libSceNpUtility/Export.cpp) (libSceNpUtility) - The list is the intersection of the export table of the console's libSceNpUtility (function symbols only), the pinned aerolib names and the functions not yet defined anywhere in the repo; names and NIDs are present in the [PS5 3.20 export inventory](https://github.com/DNNDHH/PS5-3.20_Libs/blob/b2a77c09cd71c01f34c790cfbe98c751a13262ad/libSceNpUtility.c) and the [verified NID name database](https://github.com/zecoxao/sce_symbols/blob/2883963a0a514ba6e77407a08ef4730a65e88254/aerolib.csv), but no credible prototypes were found. They are declared without parameters solely to resolve known imports; this is not an ABI claim and callers must not call them until signatures are verified. +- [sceNpAppInfoIntCheckServiceAvailabilityA, sceNpAppInfoIntCheckServiceAvailabilityAll, sceNpAppInfoIntCheckServiceAvailabilityAllA, sceNpAppInfoIntCreateRequest, sceNpAppInfoIntDestroyRequest and sceNpAppInfoIntFinalize](../../../core/libs/prx/libSceNpUtility/Export.cpp) (libSceNpUtility) - The list is the intersection of the export table of the console's libSceNpUtility (function symbols only), the pinned aerolib names and the functions not yet defined anywhere in the repo; names and NIDs are present in the [PS5 3.20 export inventory](https://github.com/DNNDHH/PS5-3.20_Libs/blob/b2a77c09cd71c01f34c790cfbe98c751a13262ad/libSceNpUtility.c) and the [verified NID name database](https://github.com/zecoxao/sce_symbols/blob/2883963a0a514ba6e77407a08ef4730a65e88254/aerolib.csv), but no credible prototypes were found. They are declared without parameters solely to resolve known imports; this is not an ABI claim and callers must not call them until signatures are verified. +- [sceNpAppInfoIntInitialize, sceNpAppLaunchLink2IntAbortRequest, sceNpAppLaunchLink2IntCreateRequest, sceNpAppLaunchLink2IntDestroyRequest, sceNpAppLaunchLink2IntFinalize and sceNpAppLaunchLink2IntGetCompatibleTitleIdList](../../../core/libs/prx/libSceNpUtility/Export.cpp) (libSceNpUtility) - The list is the intersection of the export table of the console's libSceNpUtility (function symbols only), the pinned aerolib names and the functions not yet defined anywhere in the repo; names and NIDs are present in the [PS5 3.20 export inventory](https://github.com/DNNDHH/PS5-3.20_Libs/blob/b2a77c09cd71c01f34c790cfbe98c751a13262ad/libSceNpUtility.c) and the [verified NID name database](https://github.com/zecoxao/sce_symbols/blob/2883963a0a514ba6e77407a08ef4730a65e88254/aerolib.csv), but no credible prototypes were found. They are declared without parameters solely to resolve known imports; this is not an ABI claim and callers must not call them until signatures are verified. +- [sceNpAppLaunchLink2IntGetCompatibleTitleIdNum, sceNpAppLaunchLink2IntInitialize, sceNpAppLaunchLinkIntAbortRequest, sceNpAppLaunchLinkIntCreateRequest, sceNpAppLaunchLinkIntDestroyRequest and sceNpAppLaunchLinkIntFinalize](../../../core/libs/prx/libSceNpUtility/Export.cpp) (libSceNpUtility) - The list is the intersection of the export table of the console's libSceNpUtility (function symbols only), the pinned aerolib names and the functions not yet defined anywhere in the repo; names and NIDs are present in the [PS5 3.20 export inventory](https://github.com/DNNDHH/PS5-3.20_Libs/blob/b2a77c09cd71c01f34c790cfbe98c751a13262ad/libSceNpUtility.c) and the [verified NID name database](https://github.com/zecoxao/sce_symbols/blob/2883963a0a514ba6e77407a08ef4730a65e88254/aerolib.csv), but no credible prototypes were found. They are declared without parameters solely to resolve known imports; this is not an ABI claim and callers must not call them until signatures are verified. +- [sceNpAppLaunchLinkIntGetCompatibleTitleIdList, sceNpAppLaunchLinkIntGetCompatibleTitleIdNum, sceNpAppLaunchLinkIntInitialize, sceNpBandwidthTestShutdownWithDetailedInfo and sceNpLookupAbortRequest](../../../core/libs/prx/libSceNpUtility/Export.cpp) (libSceNpUtility) - The list is the intersection of the export table of the console's libSceNpUtility (function symbols only), the pinned aerolib names and the functions not yet defined anywhere in the repo; names and NIDs are present in the [PS5 3.20 export inventory](https://github.com/DNNDHH/PS5-3.20_Libs/blob/b2a77c09cd71c01f34c790cfbe98c751a13262ad/libSceNpUtility.c) and the [verified NID name database](https://github.com/zecoxao/sce_symbols/blob/2883963a0a514ba6e77407a08ef4730a65e88254/aerolib.csv), but no credible prototypes were found. They are declared without parameters solely to resolve known imports; this is not an ABI claim and callers must not call them until signatures are verified. +- [sceNpLookupCreateAsyncRequest, sceNpLookupCreateTitleCtxA, sceNpLookupNetAbortRequest and sceNpLookupNetCensorComment](../../../core/libs/prx/libSceNpUtility/Export.cpp) (libSceNpUtility) - The list is the intersection of the export table of the console's libSceNpUtility (function symbols only), the pinned aerolib names and the functions not yet defined anywhere in the repo; names and NIDs are present in the [PS5 3.20 export inventory](https://github.com/DNNDHH/PS5-3.20_Libs/blob/b2a77c09cd71c01f34c790cfbe98c751a13262ad/libSceNpUtility.c) and the [verified NID name database](https://github.com/zecoxao/sce_symbols/blob/2883963a0a514ba6e77407a08ef4730a65e88254/aerolib.csv), but no credible prototypes were found. They are declared without parameters solely to resolve known imports; this is not an ABI claim and callers must not call them until signatures are verified. +- [sceNpLookupNetConvertJidToNpId, sceNpLookupNetConvertNpIdToJid, sceNpLookupNetCreateRequest, sceNpLookupNetCreateTitleCtx, sceNpLookupNetDeleteRequest, sceNpLookupNetDeleteTitleCtx and sceNpLookupNetInit](../../../core/libs/prx/libSceNpUtility/Export.cpp) (libSceNpUtility) - The list is the intersection of the export table of the console's libSceNpUtility (function symbols only), the pinned aerolib names and the functions not yet defined anywhere in the repo; names and NIDs are present in the [PS5 3.20 export inventory](https://github.com/DNNDHH/PS5-3.20_Libs/blob/b2a77c09cd71c01f34c790cfbe98c751a13262ad/libSceNpUtility.c) and the [verified NID name database](https://github.com/zecoxao/sce_symbols/blob/2883963a0a514ba6e77407a08ef4730a65e88254/aerolib.csv), but no credible prototypes were found. They are declared without parameters solely to resolve known imports; this is not an ABI claim and callers must not call them until signatures are verified. +- [sceNpLookupNetInitWithFunctionPointer, sceNpLookupNetInitWithMemoryPool, sceNpLookupNetIsInit, sceNpLookupNetNpId, sceNpLookupNetSanitizeComment, sceNpLookupNetSetTimeout, sceNpLookupNetTerm and sceNpLookupPollAsync](../../../core/libs/prx/libSceNpUtility/Export.cpp) (libSceNpUtility) - The list is the intersection of the export table of the console's libSceNpUtility (function symbols only), the pinned aerolib names and the functions not yet defined anywhere in the repo; names and NIDs are present in the [PS5 3.20 export inventory](https://github.com/DNNDHH/PS5-3.20_Libs/blob/b2a77c09cd71c01f34c790cfbe98c751a13262ad/libSceNpUtility.c) and the [verified NID name database](https://github.com/zecoxao/sce_symbols/blob/2883963a0a514ba6e77407a08ef4730a65e88254/aerolib.csv), but no credible prototypes were found. They are declared without parameters solely to resolve known imports; this is not an ABI claim and callers must not call them until signatures are verified. +- [sceNpLookupSetTimeout, sceNpLookupWaitAsync, sceNpServiceChecker2IntAbortRequest, sceNpServiceChecker2IntCreateRequest, sceNpServiceChecker2IntDestroyRequest and sceNpServiceChecker2IntFinalize](../../../core/libs/prx/libSceNpUtility/Export.cpp) (libSceNpUtility) - The list is the intersection of the export table of the console's libSceNpUtility (function symbols only), the pinned aerolib names and the functions not yet defined anywhere in the repo; names and NIDs are present in the [PS5 3.20 export inventory](https://github.com/DNNDHH/PS5-3.20_Libs/blob/b2a77c09cd71c01f34c790cfbe98c751a13262ad/libSceNpUtility.c) and the [verified NID name database](https://github.com/zecoxao/sce_symbols/blob/2883963a0a514ba6e77407a08ef4730a65e88254/aerolib.csv), but no credible prototypes were found. They are declared without parameters solely to resolve known imports; this is not an ABI claim and callers must not call them until signatures are verified. +- [sceNpServiceChecker2IntGetServiceAvailability, sceNpServiceChecker2IntGetServiceAvailabilityA, sceNpServiceChecker2IntInitialize, sceNpServiceChecker2IntIsSetServiceType and sceNpServiceCheckerIntAbortRequest](../../../core/libs/prx/libSceNpUtility/Export.cpp) (libSceNpUtility) - The list is the intersection of the export table of the console's libSceNpUtility (function symbols only), the pinned aerolib names and the functions not yet defined anywhere in the repo; names and NIDs are present in the [PS5 3.20 export inventory](https://github.com/DNNDHH/PS5-3.20_Libs/blob/b2a77c09cd71c01f34c790cfbe98c751a13262ad/libSceNpUtility.c) and the [verified NID name database](https://github.com/zecoxao/sce_symbols/blob/2883963a0a514ba6e77407a08ef4730a65e88254/aerolib.csv), but no credible prototypes were found. They are declared without parameters solely to resolve known imports; this is not an ABI claim and callers must not call them until signatures are verified. +- [sceNpServiceCheckerIntCreateRequest, sceNpServiceCheckerIntDestroyRequest, sceNpServiceCheckerIntFinalize, sceNpServiceCheckerIntGetAvailability, sceNpServiceCheckerIntGetAvailabilityList and sceNpServiceCheckerIntInitialize](../../../core/libs/prx/libSceNpUtility/Export.cpp) (libSceNpUtility) - The list is the intersection of the export table of the console's libSceNpUtility (function symbols only), the pinned aerolib names and the functions not yet defined anywhere in the repo; names and NIDs are present in the [PS5 3.20 export inventory](https://github.com/DNNDHH/PS5-3.20_Libs/blob/b2a77c09cd71c01f34c790cfbe98c751a13262ad/libSceNpUtility.c) and the [verified NID name database](https://github.com/zecoxao/sce_symbols/blob/2883963a0a514ba6e77407a08ef4730a65e88254/aerolib.csv), but no credible prototypes were found. They are declared without parameters solely to resolve known imports; this is not an ABI claim and callers must not call them until signatures are verified. +- [sceNpServiceCheckerIntIsCached, sceNpTitleMetadataIntAbortRequest, sceNpTitleMetadataIntCreateRequest, sceNpTitleMetadataIntDeleteRequest, sceNpTitleMetadataIntGetInfo, sceNpTitleMetadataIntGetNpTitleId and sceNpUtilityInit](../../../core/libs/prx/libSceNpUtility/Export.cpp) (libSceNpUtility) - The list is the intersection of the export table of the console's libSceNpUtility (function symbols only), the pinned aerolib names and the functions not yet defined anywhere in the repo; names and NIDs are present in the [PS5 3.20 export inventory](https://github.com/DNNDHH/PS5-3.20_Libs/blob/b2a77c09cd71c01f34c790cfbe98c751a13262ad/libSceNpUtility.c) and the [verified NID name database](https://github.com/zecoxao/sce_symbols/blob/2883963a0a514ba6e77407a08ef4730a65e88254/aerolib.csv), but no credible prototypes were found. They are declared without parameters solely to resolve known imports; this is not an ABI claim and callers must not call them until signatures are verified. +- [sceNpUtilityTerm and sceNpWordFilterCreateTitleCtxA](../../../core/libs/prx/libSceNpUtility/Export.cpp) (libSceNpUtility) - The list is the intersection of the export table of the console's libSceNpUtility (function symbols only), the pinned aerolib names and the functions not yet defined anywhere in the repo; names and NIDs are present in the [PS5 3.20 export inventory](https://github.com/DNNDHH/PS5-3.20_Libs/blob/b2a77c09cd71c01f34c790cfbe98c751a13262ad/libSceNpUtility.c) and the [verified NID name database](https://github.com/zecoxao/sce_symbols/blob/2883963a0a514ba6e77407a08ef4730a65e88254/aerolib.csv), but no credible prototypes were found. They are declared without parameters solely to resolve known imports; this is not an ABI claim and callers must not call them until signatures are verified. +- [sceNpWordFilterSetTimeout and sceNpWordFilterWaitAsync](../../../core/libs/prx/libSceNpUtility/Export.cpp) (libSceNpUtility) - The list is the intersection of the export table of the console's libSceNpUtility (function symbols only), the pinned aerolib names and the functions not yet defined anywhere in the repo; names and NIDs are present in the [PS5 3.20 export inventory](https://github.com/DNNDHH/PS5-3.20_Libs/blob/b2a77c09cd71c01f34c790cfbe98c751a13262ad/libSceNpUtility.c) and the [verified NID name database](https://github.com/zecoxao/sce_symbols/blob/2883963a0a514ba6e77407a08ef4730a65e88254/aerolib.csv), but no credible prototypes were found. They are declared without parameters solely to resolve known imports; this is not an ABI claim and callers must not call them until signatures are verified. + ### Functional - [NpLookup and NpWordFilter](../../../core/libs/prx/libSceNpUtility/Export.cpp) (libSceNpUtility) - offline only, not measured on a PS5: the argument checks and their order, the limit of 32 title contexts and 32 requests, the word filter id bases `0x01000001` and `0x02000001`, the 0x400-byte comment limit and single-use requests (`INVALID_TYPE`) follow shadPS4's [lookup](https://github.com/shadps4-emu/shadPS4/blob/91e6afa69b0a64b0afaa0281b80f8fbb0cd88136/src/core/libraries/np/np_utility/np_utility.cpp#L41-L222) and [word filter](https://github.com/shadps4-emu/shadPS4/blob/91e6afa69b0a64b0afaa0281b80f8fbb0cd88136/src/core/libraries/np/np_utility/np_utility.cpp#L705-L909) calls. `sceNpLookupNpId`, `sceNpWordFilterCensorComment` and `sceNpWordFilterSanitizeComment` then finish with `SCE_NP_ERROR_SIGNED_OUT` and write no NpId or comment; shadPS4's word filter accepts every comment as a stand-in for the server, so its result is not used diff --git a/docs/dev/debt/libScePad.md b/docs/dev/debt/libScePad.md index dc6579857e..000b382ce7 100644 --- a/docs/dev/debt/libScePad.md +++ b/docs/dev/debt/libScePad.md @@ -11,8 +11,11 @@ - [libScePad report and tracking exports](../../../core/libs/prx/libScePad/Export.cpp) - read, extended-read, tracker, UI, VR-controller and report argument registers are recovered. Feature reports use a byte report ID and 64-bit byte count, capped at 64. VR trigger effects consume a 56-byte record; the dual-controller call uses two handles and a mask with records at offsets 8 and 64. Remaining payload field layouts use incomplete declarations, including extended samples and kernel-owned extension reports. - [scePadUnknown_n3kSX62fgNo](../../../core/libs/prx/libScePad/Export.cpp) (libScePad, NID `n3kSX62fgNo`) - the name is unknown, and the 0 returned after `scePadInit` is inferred, not measured. In the OS 10.01 system module the function takes no arguments, returns `0x80920005` before `scePadInit`, and otherwise sends a per-process value of 1 to ShellCore over Mbus, returning `0x809200ff` if that call fails and 0 if it succeeds, with or without an open handle. The #3177 probe measured `0x809200ff` in a process that is not a game; a game process is assumed to get 0. The ShellCore setting it changes is unknown and not modelled -- [scePadSetAngularVelocityDeadbandState](../../../core/libs/prx/libScePad/src/PadState.cpp) (libScePad) - with the deadband on, each angular velocity axis whose magnitude is below 0.017453292 rad/s (1 degree per second) reads as 0, before the orientation is integrated; it starts off. Threshold, per-axis test and default follow [duaLib](https://github.com/WujekFoliarz/duaLib/blob/03bad1bea2a36561b520846776f2a07ced6773e0/src/source/duaLib.cpp#L44), which reimplements the PC `libScePad.dll`; the console's threshold is not measured, and the deadband is kept per library rather than per handle since one pad handle exists +- [scePadSetAngularVelocityDeadbandState](../../../core/libs/prx/libScePad/src/PadState.cpp) (libScePad) - with the deadband on, each angular velocity axis whose magnitude is below 0.017453292 rad/s (1 degree per second) reads as 0, before the orientation is integrated; it starts off. Threshold, per-axis test and default follow [duaLib](https://github.com/WujekFoliarz/duaLib/blob/03bad1bea2a36561b520846776f2a07ced6773e0/src/source/duaLib.cpp#L44), which reimplements the PC `libScePad.dll`; the console's threshold is not measured, and the deadband is kept per port slot +- [PadQueueLegacyAudioHaptics, PadClearLegacyAudioHaptics](../../../core/libs/prx/libScePad/src/PadState.cpp) (libScePad) - PadQueueLegacyAudioHaptics and PadClearLegacyAudioHaptics route legacy audio haptics only to port 0; multi-port queueing is not modelled +- [scePadGetControllerInformation](../../../core/libs/prx/libScePad/Export.cpp) (libScePad) - scePadGetControllerInformation always reports connectedCount = 1 and connected = true; behaviour across multiple open controller handles is not measured on hardware - [scePadOpen and scePadOpenExt](../../../core/libs/prx/libScePad/Export.cpp) (libScePad) - the special port (type 2) opens a handle per user (`PAD_SPECIAL_HANDLE` + slot, 16 slots for the console's 16 accounts, a 17th user gets the unknown-user code 0x809b0081) that every pad call accepts: reads give the rest record (sticks 0x80, orientation w 1, acceleration y 1, `connected` false), information is zeroed, extended information has device class -1, setters succeed without effect. Not measured: the console's own handle values (fresh per open), a PS5, and the calls the PS4 Pro survey (firmware 10.71, no special controller) did not cover; it measured open, `ALREADY_OPENED`, `scePadGetHandle`, both reads, both informations, the five setters above, close, and that two logged-in users get independent handles. `scePadOpenExt` returns `DEVICE_NOT_CONNECTED` (0x80920007) for special ports, unmeasured; PPSA01317 skips a user on any negative result. Other port types throw +- [scePadOpen, scePadGetHandle, scePadRead and scePadReadState for the remote control port](../../../core/libs/prx/libScePad/Export.cpp) (libScePad) - whether a console without a media remote opens the system user's remote control port (type 16), returns `DEVICE_NOT_CONNECTED` or fills other fields is not measured. The host has no remote, so the port opens its own handle (`PAD_REMOTE_HANDLE`) that reads one zeroed entry with no buttons and `connected` false; the other pad calls reject it with `INVALID_HANDLE`. SDL's PS5 video driver maps this port's buttons to keys, so a remote that read the DualSense turned Cross into Return (Raptor PPSA94002). - [scePadVrControllerReadState](../../../core/libs/prx/libScePad/Export.cpp) - the 104-byte output layout is recovered, including buttons, stick, triggers, touch fields, motion, connection state and timestamp. Touch-value semantics and kernel report production remain incomplete; the export still throws. - [scePadVrControllerReadCombinedState](../../../core/libs/prx/libScePad/Export.cpp) - the two handle arguments and 128-byte output are recovered; output field semantics remain incomplete. - [scePadSetVibrationMode](../../../core/libs/prx/libScePad/Export.cpp) (libScePad) - modes 1 to 3 and 5 are accepted and stored. On a PS5 (firmware 13.60) the call returns `0x80920005` before `scePadInit`, then rejects a mode of 0 or below with `INVALID_ARG` before it checks the handle, and returns `INVALID_HANDLE` for a bad handle with any higher mode; whether an open handle accepts modes above 3 was not measured, since the payload could not open a pad; 5 is accepted because PPSA25286 passes it on its open handle at start-up; the meanings of modes 3 and 5 are unknown (PPSA21480 calls it with 3 right after `scePadOpen` and aborts on failure; [SharpProspero](https://github.com/SvenGDK/SharpProspero/blob/main/src/SharpProspero/Interop/Pad/Pad.cs) names only 1 and 2). The stored mode has no other effect @@ -20,9 +23,8 @@ - [scePadIsDS4Connected, scePadIsBlasterConnected and scePadIsMoveConnected](../../../core/libs/prx/libScePad/Export.cpp) (libScePad) - the `bool (int handle)` signatures are recovered; the library reports one DualSense to every title (see Touchpad resolution below), so no DualShock 4, Blaster or Move controller is ever behind a handle and each call answers false for any handle. The console's answer for an invalid handle is not measured - [scePadSetAngularVelocityBiasCorrectionState](../../../core/libs/prx/libScePad/Export.cpp) (libScePad) - inferred signature `(int handle, bool enabled)` follows `scePadSetTiltCorrectionState` and `scePadSetAngularVelocityDeadbandState`; an invalid handle returns `PAD_ERROR_INVALID_HANDLE`, disabling is a no-op, and enabling throws because bias correction is not modelled - [scePadGetAccessibilityControllerState](../../../core/libs/prx/libScePad/Export.cpp) (libScePad) - the value a console reports for a DualSense is not measured; the emulated pad, which is not an Access controller, reports 0. The name hashes to NID `zBsgF0q8DIM`. In the OS 10.01 system module the `(int handle, uint32_t* state)` function returns `0x80920005` before `scePadInit`, then `INVALID_ARG` for a NULL `state` and `INVALID_HANDLE` for a bad handle, and otherwise asks the pad driver for a per-device value of the device behind the handle: 0 when no device is behind it, 1 when the driver has no answer, else the driver's value. What the values mean is unknown - +- [scePadGetControllerType](../../../core/libs/prx/libScePad/Export.cpp) (libScePad) - the value a console reports for a DualSense is not measured; the emulated pad reports `PAD_CONTROLLER_TYPE_DUAL_SENSE` (2), following [duaLib](https://github.com/WujekFoliarz/duaLib/blob/master/src/include/duaLib.h)'s `s_SceControllerType` enum where `DUALSENSE = 2`. The signature `(int handle, int32_t* type)` follows duaLib, while the validation order (checking initialization, null pointer, then handle) follows the library's other pad queries; duaLib does not validate null pointers and returns `PAD_ERROR_DEVICE_NOT_CONNECTED` for an invalid pad. - [libScePad internal data and device control](../../../core/libs/prx/libScePad/Export.cpp) - kernel-owned version, telemetry, Lotus-button and update payload fields remain unresolved. Lotus-button parameter units and the three blinking byte meanings are unknown. Port information occupies 152 bytes; history records occupy 72 bytes; Leonardo unique data occupies 24 bytes. Their complete field semantics remain unresolved. - - [libScePad profile writes and virtual devices](../../../core/libs/prx/libScePad/Export.cpp) - profile writes take a handle, profile ID, profile pointer and a 32-bit result pointer; the profile payload direction and fields remain unresolved. Virtual-device creation takes an input record and an output ID pointer, whose width is not exposed by the wrapper. Component information copies 16 bytes; field meanings remain unknown. Virtual trigger state contains two 32-bit states and three control bytes, but the control semantics remain incomplete. ### Functional diff --git a/docs/dev/debt/libSceSysCore.md b/docs/dev/debt/libSceSysCore.md index 969d61b1fe..386b64d98d 100644 --- a/docs/dev/debt/libSceSysCore.md +++ b/docs/dev/debt/libSceSysCore.md @@ -3,3 +3,15 @@ ### Unknown function info - [sceApplicationGetAppInfoByAppId](../../../core/libs/prx/libSceSysCore/Export.cpp) takes a 32-bit application ID and a writable 152-byte record. The service fills the record through IPC; its complete field layout remains unresolved. The SystemService caller consumes a subrecord beginning at byte 48. + +- [_sceApplicationGetAppId, sceApplicationAddProcess2, sceApplicationBeginSclkChange, sceApplicationBlockingKill2, sceApplicationBlockingKill3, sceApplicationContinue, sceApplicationCrashSyscore, sceApplicationCrashSystemProcess](../../../core/libs/prx/libSceSysCore/Export.cpp) (libSceSysCore) - The list is the intersection of the export table of the console's libSceSysCore (function symbols only), the pinned aerolib names and the functions not yet defined anywhere in the repo. Names and NIDs are present in the [PS5 3.20 export inventory](https://github.com/DNNDHH/PS5-3.20_Libs/blob/b2a77c09cd71c01f34c790cfbe98c751a13262ad/libSceSysCore.c) and the [verified NID name database](https://github.com/zecoxao/sce_symbols/blob/2883963a0a514ba6e77407a08ef4730a65e88254/aerolib.csv), but no credible prototypes were found. They are declared without parameters solely to resolve known imports; this is not an ABI claim and callers must not call them until signatures are verified. +- [sceApplicationEndSclkChange, sceApplicationExitSpawn, sceApplicationExitSpawn2, sceApplicationFinalize, sceApplicationGetAppPackageInfoForCoredump, sceApplicationGetCompiledSdkVersionByAppId and sceApplicationGetCoredumpDirAndFileName](../../../core/libs/prx/libSceSysCore/Export.cpp) (libSceSysCore) - The list is the intersection of the export table of the console's libSceSysCore (function symbols only), the pinned aerolib names and the functions not yet defined anywhere in the repo. Names and NIDs are present in the [PS5 3.20 export inventory](https://github.com/DNNDHH/PS5-3.20_Libs/blob/b2a77c09cd71c01f34c790cfbe98c751a13262ad/libSceSysCore.c) and the [verified NID name database](https://github.com/zecoxao/sce_symbols/blob/2883963a0a514ba6e77407a08ef4730a65e88254/aerolib.csv), but no credible prototypes were found. They are declared without parameters solely to resolve known imports; this is not an ABI claim and callers must not call them until signatures are verified. +- [sceApplicationGetCoredumpState, sceApplicationGetCpuUsage, sceApplicationGetCpuUsage2, sceApplicationGetDbgExecutablePath, sceApplicationGetMode, sceApplicationGetProcs and sceApplicationGetShellCoreAppId](../../../core/libs/prx/libSceSysCore/Export.cpp) (libSceSysCore) - The list is the intersection of the export table of the console's libSceSysCore (function symbols only), the pinned aerolib names and the functions not yet defined anywhere in the repo. Names and NIDs are present in the [PS5 3.20 export inventory](https://github.com/DNNDHH/PS5-3.20_Libs/blob/b2a77c09cd71c01f34c790cfbe98c751a13262ad/libSceSysCore.c) and the [verified NID name database](https://github.com/zecoxao/sce_symbols/blob/2883963a0a514ba6e77407a08ef4730a65e88254/aerolib.csv), but no credible prototypes were found. They are declared without parameters solely to resolve known imports; this is not an ABI claim and callers must not call them until signatures are verified. +- [sceApplicationGetStateForDebugger, sceApplicationInitialize, sceApplicationInitializeForShellCore, sceApplicationIsCrashReportBeingProcessed, sceApplicationIsPrimaryProcess, sceApplicationIsResumable and sceApplicationIsSuspendable](../../../core/libs/prx/libSceSysCore/Export.cpp) (libSceSysCore) - The list is the intersection of the export table of the console's libSceSysCore (function symbols only), the pinned aerolib names and the functions not yet defined anywhere in the repo. Names and NIDs are present in the [PS5 3.20 export inventory](https://github.com/DNNDHH/PS5-3.20_Libs/blob/b2a77c09cd71c01f34c790cfbe98c751a13262ad/libSceSysCore.c) and the [verified NID name database](https://github.com/zecoxao/sce_symbols/blob/2883963a0a514ba6e77407a08ef4730a65e88254/aerolib.csv), but no credible prototypes were found. They are declared without parameters solely to resolve known imports; this is not an ABI claim and callers must not call them until signatures are verified. +- [sceApplicationKickCoredump, sceApplicationKickCoredump2, sceApplicationKill, sceApplicationLocalProcessKill, sceApplicationLocalProcessKill2, sceApplicationLocalProcessResume and sceApplicationLocalProcessSuspend](../../../core/libs/prx/libSceSysCore/Export.cpp) (libSceSysCore) - The list is the intersection of the export table of the console's libSceSysCore (function symbols only), the pinned aerolib names and the functions not yet defined anywhere in the repo. Names and NIDs are present in the [PS5 3.20 export inventory](https://github.com/DNNDHH/PS5-3.20_Libs/blob/b2a77c09cd71c01f34c790cfbe98c751a13262ad/libSceSysCore.c) and the [verified NID name database](https://github.com/zecoxao/sce_symbols/blob/2883963a0a514ba6e77407a08ef4730a65e88254/aerolib.csv), but no credible prototypes were found. They are declared without parameters solely to resolve known imports; this is not an ABI claim and callers must not call them until signatures are verified. +- [sceApplicationNotifyCoredumpRequestEnd, sceApplicationNotifyCoredumpRequestProgress, sceApplicationNotifyVshMainOnStandby, sceApplicationNotifyVshReady, sceApplicationOnAllResourceReleased and sceApplicationRaiseException](../../../core/libs/prx/libSceSysCore/Export.cpp) (libSceSysCore) - The list is the intersection of the export table of the console's libSceSysCore (function symbols only), the pinned aerolib names and the functions not yet defined anywhere in the repo. Names and NIDs are present in the [PS5 3.20 export inventory](https://github.com/DNNDHH/PS5-3.20_Libs/blob/b2a77c09cd71c01f34c790cfbe98c751a13262ad/libSceSysCore.c) and the [verified NID name database](https://github.com/zecoxao/sce_symbols/blob/2883963a0a514ba6e77407a08ef4730a65e88254/aerolib.csv), but no credible prototypes were found. They are declared without parameters solely to resolve known imports; this is not an ABI claim and callers must not call them until signatures are verified. +- [sceApplicationRaiseExceptionToLocalPid, sceApplicationRaiseExceptionToPid, sceApplicationRequestToChangeRenderingMode, sceApplicationRestore, sceApplicationResume, sceApplicationSave and sceApplicationSendDebugSpawnResult2](../../../core/libs/prx/libSceSysCore/Export.cpp) (libSceSysCore) - The list is the intersection of the export table of the console's libSceSysCore (function symbols only), the pinned aerolib names and the functions not yet defined anywhere in the repo. Names and NIDs are present in the [PS5 3.20 export inventory](https://github.com/DNNDHH/PS5-3.20_Libs/blob/b2a77c09cd71c01f34c790cfbe98c751a13262ad/libSceSysCore.c) and the [verified NID name database](https://github.com/zecoxao/sce_symbols/blob/2883963a0a514ba6e77407a08ef4730a65e88254/aerolib.csv), but no credible prototypes were found. They are declared without parameters solely to resolve known imports; this is not an ABI claim and callers must not call them until signatures are verified. +- [sceApplicationSendResultOfDebuggerKillRequest, sceApplicationSendResultOfDebuggerResumeRequest, sceApplicationSendResultOfDebuggerSuspendRequest, sceApplicationSendResultOfDebuggerTitleIdLaunchRequest and sceApplicationSetApplicationFocus](../../../core/libs/prx/libSceSysCore/Export.cpp) (libSceSysCore) - The list is the intersection of the export table of the console's libSceSysCore (function symbols only), the pinned aerolib names and the functions not yet defined anywhere in the repo. Names and NIDs are present in the [PS5 3.20 export inventory](https://github.com/DNNDHH/PS5-3.20_Libs/blob/b2a77c09cd71c01f34c790cfbe98c751a13262ad/libSceSysCore.c) and the [verified NID name database](https://github.com/zecoxao/sce_symbols/blob/2883963a0a514ba6e77407a08ef4730a65e88254/aerolib.csv), but no credible prototypes were found. They are declared without parameters solely to resolve known imports; this is not an ABI claim and callers must not call them until signatures are verified. +- [sceApplicationSetControllerFocus, sceApplicationSetControllerFocusPermissionToSubProcess, sceApplicationSetMemoryPstate, sceApplicationSignalShellCoreHeartBeat, sceApplicationSpawn2 and sceApplicationSuspend](../../../core/libs/prx/libSceSysCore/Export.cpp) (libSceSysCore) - The list is the intersection of the export table of the console's libSceSysCore (function symbols only), the pinned aerolib names and the functions not yet defined anywhere in the repo. Names and NIDs are present in the [PS5 3.20 export inventory](https://github.com/DNNDHH/PS5-3.20_Libs/blob/b2a77c09cd71c01f34c790cfbe98c751a13262ad/libSceSysCore.c) and the [verified NID name database](https://github.com/zecoxao/sce_symbols/blob/2883963a0a514ba6e77407a08ef4730a65e88254/aerolib.csv), but no credible prototypes were found. They are declared without parameters solely to resolve known imports; this is not an ABI claim and callers must not call them until signatures are verified. +- [sceApplicationSuspendSystemForDebugger, sceApplicationSwitchToBaseMode, sceApplicationSwitchToNeoMode, sceApplicationSwitchToNeoMode2, sceApplicationSystemReboot and sceApplicationSystemShutdown2](../../../core/libs/prx/libSceSysCore/Export.cpp) (libSceSysCore) - The list is the intersection of the export table of the console's libSceSysCore (function symbols only), the pinned aerolib names and the functions not yet defined anywhere in the repo. Names and NIDs are present in the [PS5 3.20 export inventory](https://github.com/DNNDHH/PS5-3.20_Libs/blob/b2a77c09cd71c01f34c790cfbe98c751a13262ad/libSceSysCore.c) and the [verified NID name database](https://github.com/zecoxao/sce_symbols/blob/2883963a0a514ba6e77407a08ef4730a65e88254/aerolib.csv), but no credible prototypes were found. They are declared without parameters solely to resolve known imports; this is not an ABI claim and callers must not call them until signatures are verified. +- [sceApplicationSystemSuspend, sceApplicationSystemSuspend2, sceApplictionGetStateForDebugger, sceSysCoreGetEventCount and sceSysCoreReceiveEvent](../../../core/libs/prx/libSceSysCore/Export.cpp) (libSceSysCore) - The list is the intersection of the export table of the console's libSceSysCore (function symbols only), the pinned aerolib names and the functions not yet defined anywhere in the repo. Names and NIDs are present in the [PS5 3.20 export inventory](https://github.com/DNNDHH/PS5-3.20_Libs/blob/b2a77c09cd71c01f34c790cfbe98c751a13262ad/libSceSysCore.c) and the [verified NID name database](https://github.com/zecoxao/sce_symbols/blob/2883963a0a514ba6e77407a08ef4730a65e88254/aerolib.csv), but no credible prototypes were found. They are declared without parameters solely to resolve known imports; this is not an ABI claim and callers must not call them until signatures are verified. diff --git a/docs/dev/debt/libSceSystemService.md b/docs/dev/debt/libSceSystemService.md index c11d910836..40dfa92cfa 100644 --- a/docs/dev/debt/libSceSystemService.md +++ b/docs/dev/debt/libSceSystemService.md @@ -14,6 +14,7 @@ ### Unknown function info - [sceSystemServiceAddLocalProcessForPsmKit, sceSystemServiceKillLocalProcessForPsmKit](../../../core/libs/prx/libSceSystemService/Export.cpp) (libSceSystemService) - AnyPS5 runs one process and starts or stops no other, so both throw without reading their arguments. The parameters follow Forever Launcher's own firmware 5.50 probes (`ps5/probes/localproc` in [bizkut/ps5-forever](https://github.com/bizkut/ps5-forever)): a path, a null-terminated argv and a 0x90-byte parameter block in, a handle out; the kill takes that handle. Their results on the console are unknown. Forever Launcher (PPSA99030) imports both and stops at load without them. +- [sceSystemServiceGetAppId, sceSystemServiceGetAppTitleId, sceLncUtilGetAppTitleId](../../../core/libs/prx/libSceSystemService/Export.cpp) (libSceSystemService) - the console's results for a title ID or app ID other than the running title are unknown, and so are its errors for null pointers, so those calls throw. `sceSystemServiceGetAppId` compares the title ID exactly with the one in `param.json` or `param.sfo`. The title ID buffer is 10 bytes, so a running title ID of 10 or more characters throws too. The signatures follow [SharpProspero](https://github.com/SvenGDK/SharpProspero/blob/5dfdb4b0b166e83dc9ae275001afb695f12c7174/src/SharpProspero/Interop/SystemService/SystemService.cs#L236-L260), except that `sceLncUtilGetAppTitleId` returns an `int`, as declared in the pending exports, and 0 on success like `sceSystemServiceGetAppTitleId`; SharpProspero names `ErrorNoAppInfo` (0x80A10009) only for `sceSystemServiceGetAppFocusedAppStatus`. - [sceSystemServiceLaunchApp](../../../core/libs/prx/libSceSystemService/Export.cpp) (libSceSystemService) - AnyPS5 runs one title and cannot start another, so every call throws without reading its arguments. The (title ID, null-terminated argv, launch parameters) signature follows [SharpProspero](https://github.com/SvenGDK/SharpProspero/blob/5dfdb4b0b166e83dc9ae275001afb695f12c7174/src/SharpProspero/Interop/SystemService/SystemService.cs#L141-L148), whose `AppLauncher` Prospero Explorer (PPSA99109) and Prospero Multi Tools (PPSA99110) import it through and which passes a 0x20-byte launch context or null; the console's argument checks and errors are unknown - [sceSystemServiceLaunchWebBrowser](../../../core/libs/prx/libSceSystemService/Export.cpp): the native export takes one URI argument. Null input, the 6015-byte length limit and supported scheme checks are recovered. The host still returns kernel `EOPNOTSUPP` for valid input; a missing browser service's native error is unresolved. - [sceSystemServiceParamGetString](../../../core/libs/prx/libSceSystemService/Export.cpp) (libSceSystemService) - the system name (parameter 6) is the fixed `PS5`, not the console's name. As in a payload process on a PS5 (firmware 13.60), the name is written NUL-padded to the whole buffer, and a buffer under 65 bytes, a null buffer or any other parameter returns `SYSTEM_SERVICE_ERROR_PARAMETER` without writing. Parameters 0 to 40, 100 to 110, 1000 to 1010, -1 and `INT_MAX` were tried there; the others are assumed to fail the same way. PPSA26344 at `0xA87935` passes parameter 6 with a 65-byte buffer diff --git a/docs/dev/debt/libSceUsbStorage.md b/docs/dev/debt/libSceUsbStorage.md index e1bc7fc86b..c901faca35 100644 --- a/docs/dev/debt/libSceUsbStorage.md +++ b/docs/dev/debt/libSceUsbStorage.md @@ -1,6 +1,19 @@ # libSceUsbStorage technical debt +### Silent stubs + +- [sceUsbStorageInit, sceUsbStorageTerm](../../../core/libs/prx/libSceUsbStorage/Export.cpp) (libSceUsbStorage) - no USB mass-storage device is ever attached, so there is nothing to initialize or tear down: both accept every call and keep no state, as [shadPS4](https://github.com/shadps4-emu/shadPS4/blob/f68923fe807f657348e6719ce658bcbd0a2b1c72/src/core/libraries/usbd/usbstorage.cpp#L91-L99) does with its plain ORBIS_OK returns. A passthrough that lists devices must build its list in `Init` and free it in `Term`. + ### Unknown function info +- [sceUsbStorageRegisterCallback, sceUsbStorageUnregisterCallback, sceUsbStorageRegisterCallbackForMapAvailable, sceUsbStorageUnregisterCallbackForMapAvailable, sceUsbStorageRegisterCallbackForMapUnavailable, sceUsbStorageUnregisterCallbackForMapUnavailable](../../../core/libs/prx/libSceUsbStorage/Export.cpp) (libSceUsbStorage) - what a successful registration stores, what unregistering a never-registered callback answers, and when any callback fires is unverified: no device is ever attached, and [shadPS4](https://github.com/shadps4-emu/shadPS4/blob/f68923fe807f657348e6719ce658bcbd0a2b1c72/src/core/libraries/usbd/usbstorage.cpp#L110-L174) logs all six as `(STUBBED)` and returns 0 without storing anything, the success-without-a-reference pattern #6288 was closed for. The argument validation is implemented (0x80f40002 for a null callback, an event type outside 1 and 2, or a null directory); past it they keep the marker until a reference or a measurement says what success does. +- [sceUsbStorageRequestUnmap, sceUsbStorageGetdentsClose](../../../core/libs/prx/libSceUsbStorage/Export.cpp) (libSceUsbStorage) - what the console answers for a directory that was never mapped and a getdents descriptor that was never opened is unknown: nothing can be mapped or opened in this model, shadPS4's [unmap](https://github.com/shadps4-emu/shadPS4/blob/f68923fe807f657348e6719ce658bcbd0a2b1c72/src/core/libraries/usbd/usbstorage.cpp#L281-L288) only logs without keeping state and its [close](https://github.com/shadps4-emu/shadPS4/blob/f68923fe807f657348e6719ce658bcbd0a2b1c72/src/core/libraries/usbd/usbstorage.cpp#L378-L383) succeeds only because `erase` ignores a missing key, and the no-device libraries throw on this path (`sceMoveClose` for a handle that was never opened, `sceContentSearchCloseMetadata` because no metadata id is ever handed out). Both keep the marker after their argument validation. - [sceUsbStorageClearFakeWriteProtection, sceUsbStorageSetFakeMapLockForDebug, sceUsbStorageSetFakeWriteProtection](../../../core/libs/prx/libSceUsbStorage/Export.cpp) (libSceUsbStorage) - names and NIDs are present in the [PS5 3.20 export inventory](https://github.com/DNNDHH/PS5-3.20_Libs/blob/b2a77c09cd71c01f34c790cfbe98c751a13262ad/libSceUsbStorage.c), but no credible prototypes were found. They are declared without parameters solely to resolve known imports; this is not an ABI claim. -- [sceUsbStorageRequestMapWSB](../../../core/libs/prx/libSceUsbStorage/Export.cpp) (libSceUsbStorage) - its prototype follows [shadPS4](https://github.com/shadps4-emu/shadPS4/blob/f68923fe807f657348e6719ce658bcbd0a2b1c72/src/core/libraries/usbd/usbstorage.cpp#L245-L246), not measured on a console; the throwing declaration only resolves imports. +- [sceUsbStorageGetMountPointOfShellCore](../../../core/libs/prx/libSceUsbStorage/Export.cpp) (libSceUsbStorage) - the prototype is known, but no reference says what a console without a mapped device writes into the mount-point buffer or returns; it keeps the marker until a reference or a console measurement answers it. + +### Functional + +- [the argument validation](../../../core/libs/prx/libSceUsbStorage/Export.cpp) (libSceUsbStorage) - the null-pointer, null-callback, event-type and null-directory refusals with 0x80f40002, and the getdents read of a never-opened descriptor answering 0x80f40005 with zero bytes, follow [shadPS4](https://github.com/shadps4-emu/shadPS4/blob/f68923fe807f657348e6719ce658bcbd0a2b1c72/src/core/libraries/usbd/usbstorage.cpp#L176-L383); the validation runs before the markers the register, unregister, unmap and getdents-close exports keep above. None of this is measured on a console. +- [the no-device answers](../../../core/libs/prx/libSceUsbStorage/Export.cpp) (libSceUsbStorage) - AnyPS5 has no USB mass-storage passthrough, so no device is ever attached: the device list is empty, `sceUsbStorageIsExist` succeeds and reports non-existence for every device id and directory, and the device-info query and the map and getdents-open requests answer 0x80f40004 for every device id. A passthrough that lists devices follows shadPS4's virtual-device model at the same commit for the filled paths, including its packed dirent. +- [the device-info record](../../../core/libs/prx/libSceUsbStorage/Export.cpp) (libSceUsbStorage) - the 0x338-byte record a passthrough fills for `sceUsbStorageGetDeviceInfo` is unverified on hardware; its layout follows [SharpProspero](https://github.com/SvenGDK/SharpProspero/blob/5dfdb4b0b166e83dc9ae275001afb695f12c7174/src/SharpProspero/Platform/UsbStorage.cs#L34-L45), which recovered it from the module's own export and was checked against #6288's "volume size at 0x0": the u64 volume size at offset 0, the vendor, product and serial strings at 0x16, 0x120 and 0x228, and the flags field the service masks with 0xd0 at 0x330. shadPS4's record starts with a u32 device id at offset 0; filling it that way puts a device id where a title reads the volume size. +- [the prototypes of sceUsbStorageRequestMap and sceUsbStorageGetDeviceList](../../../core/libs/prx/libSceUsbStorage/Export.cpp) (libSceUsbStorage) - the signatures follow the ABI restored on `main` (the firmware-database sweep of 2026-10-21), which matches [shadPS4](https://github.com/shadps4-emu/shadPS4/blob/f68923fe807f657348e6719ce658bcbd0a2b1c72/src/core/libraries/usbd/usbstorage.cpp#L176-L383): `RequestMap`'s six parameters, the typed `UsbStorageCallback` of the register/unregister family, `IsExist`'s three-argument bool form and `GetDeviceList`'s `std::int32_t*` count (the count also follows [SharpProspero](https://github.com/SvenGDK/SharpProspero/blob/5dfdb4b0b166e83dc9ae275001afb695f12c7174/src/SharpProspero/Platform/UsbStorage.cs#L233-L235), whose earlier eight-parameter `RequestMap` reading carried the trailing reserved pair the restored ABI drops). diff --git a/docs/dev/debt/libSceUsbd.md b/docs/dev/debt/libSceUsbd.md index f23c18fd72..3f234ef7b4 100644 --- a/docs/dev/debt/libSceUsbd.md +++ b/docs/dev/debt/libSceUsbd.md @@ -6,4 +6,8 @@ ### Unknown function info -- [sceUsbdClearHalt, sceUsbdDetachKernelDriver, sceUsbdEventHandlerActive, sceUsbdGetConfigDescriptorByValue, sceUsbdGetConfiguration, sceUsbdGetDescriptor, sceUsbdGetDevice, sceUsbdGetDeviceSpeed, sceUsbdGetMaxIsoPacketSize, sceUsbdGetMaxPacketSize, sceUsbdGetStringDescriptorAscii, sceUsbdHandleEvents, sceUsbdHandleEventsLocked, sceUsbdLockEventWaiters, sceUsbdLockEvents, sceUsbdOpenDeviceWithVidPid, sceUsbdSetInterfaceAltSetting, sceUsbdTryLockEvents, sceUsbdUnlockEventWaiters, sceUsbdUnlockEvents, sceUsbdWaitForEvent](../../../core/libs/prx/libSceUsbd/Export.cpp) (libSceUsbd) - signatures follow the console type database in [orbital](https://github.com/AlexAltea/orbital/blob/cc408367da6a6ebbf5ac1ac559fefeadc73b2831/tools/ida/db_types.json#L2268-L2322), but their behavior is unknown and each call throws; not measured on a console +- [sceUsbdClearHalt, sceUsbdDetachKernelDriver, sceUsbdEventHandlerActive, sceUsbdGetDevice, sceUsbdGetDeviceSpeed, sceUsbdGetMaxIsoPacketSize, sceUsbdGetMaxPacketSize, sceUsbdHandleEvents, sceUsbdHandleEventsLocked, sceUsbdLockEventWaiters, sceUsbdLockEvents, sceUsbdOpenDeviceWithVidPid, sceUsbdSetInterfaceAltSetting, sceUsbdTryLockEvents, sceUsbdUnlockEventWaiters, sceUsbdUnlockEvents, sceUsbdWaitForEvent](../../../core/libs/prx/libSceUsbd/Export.cpp) (libSceUsbd) - signatures follow the console type database in [orbital](https://github.com/AlexAltea/orbital/blob/cc408367da6a6ebbf5ac1ac559fefeadc73b2831/tools/ida/db_types.json#L2268-L2322), but their behavior is unknown and each call throws; not measured on a console + +### Functional + +- [sceUsbdGetConfiguration, sceUsbdGetConfigDescriptorByValue, sceUsbdGetDescriptor, sceUsbdGetStringDescriptorAscii](../../../core/libs/prx/libSceUsbd/Export.cpp) (libSceUsbd) - not measured on a console; they follow FreeBSD 11.4 ([libusb10_desc.c](https://github.com/freebsd/freebsd-src/blob/d106002967eff712894ab8b5337bbbff106ce5e0/lib/libusb/libusb10_desc.c), [libusb20.c](https://github.com/freebsd/freebsd-src/blob/d106002967eff712894ab8b5337bbbff106ce5e0/lib/libusb/libusb20.c)). The configuration and descriptor queries return host libusb errors. `sceUsbdGetStringDescriptorAscii` rebuilds `libusb20_dev_req_string_simple_sync` with only its 255-byte read: libusb's Windows HID backend reports the requested length as `bLength`, cutting a 4-byte header read to one character. Unlike the host `libusb_get_string_descriptor_ascii` it accepts index 0, drops non-printable characters and `<>` instead of writing `?`, and fails with `0x802400FF`. Closes when: measured on a PS5 diff --git a/docs/dev/debt/libSceUserService.md b/docs/dev/debt/libSceUserService.md index cd811dc408..f1cb7b914e 100644 --- a/docs/dev/debt/libSceUserService.md +++ b/docs/dev/debt/libSceUserService.md @@ -4,8 +4,9 @@ - [sceUserServiceGetNpAccountId](../../../core/libs/prx/libSceUserService/Export.cpp) (libSceUserService) - the account id for a user with no linked PSN account is assumed to be 0 with success; libSceNpManager's `sceNpGetAccountIdA` answers the same question with `SCE_NP_ERROR_SIGNED_OUT` - [sceUserServiceInitialize and sceUserServiceInitialize2](../../../core/libs/prx/libSceUserService/Export.cpp) (libSceUserService) - event delivery has no background service thread, so the confirmed priority and Initialize2 CPU affinity mask do not affect host scheduling. -- [sceUserServiceGetUserColor](../../../core/libs/prx/libSceUserService/Export.cpp) (libSceUserService) - the default profile colour is assumed to be blue (0) -- [sceUserServiceGetUserName](../../../core/libs/prx/libSceUserService/Export.cpp) (libSceUserService) - `0x80960105`, which a PS5 (firmware 13.60) returns for a user id it does not know, has no known name; it is `USER_SERVICE_ERROR_UNKNOWN_USER` here. On the console a registered user who is not logged in still gets their name; here only the initial user exists +- [sceUserServiceGetUserColor](../../../core/libs/prx/libSceUserService/Export.cpp) (libSceUserService) - the profile colour corresponds to user index (0: blue, 1: red, 2: green, 3: pink); colours 1, 2 and 3 are assumed, not measured +- [sceUserServiceGetUserName](../../../core/libs/prx/libSceUserService/Export.cpp) (libSceUserService) - `0x80960105`, which a PS5 (firmware 13.60) returns for a user id it does not know, has no known name; it is `USER_SERVICE_ERROR_UNKNOWN_USER` here. On the console a registered user who is not logged in still gets their name; here up to 4 users ("Player", "Player2", "Player3", "Player4") are supported; user names "Player2", "Player3" and "Player4" are assumed, not measured +- [sceUserServiceGetEvent](../../../core/libs/prx/libSceUserService/Export.cpp) (libSceUserService) - login and logout events queued dynamically on controller hotplug have no source and are assumed, not measured - [sceUserServiceGetAgeLevel](../../../core/libs/prx/libSceUserService/Export.cpp) (libSceUserService) - on a PS5 (firmware 13.60) the value is the age set in the user's parental restriction (0, 7, 12 and 15 were measured) and `0xFFFFFFFF` without one; a user who is not logged in returns `0x80960009` with or without an output. Parental restrictions are not modelled, so the initial user is always reported as unrestricted. PPSA12544 (Unity) only stores the value for its user profile - [sceUserServiceGetAccessibilityChatTranscription, PressAndHoldDelay, TriggerEffect, Vibration, ZoomEnabled, ZoomFollowFocus](../../../core/libs/prx/libSceUserService/Export.cpp) (libSceUserService) - report 0, 0, 1, 1, 0 and 1, what a PS5 (firmware 13.60) reports for a user whose accessibility settings are all at their defaults; the console reports the user's own settings (a changed vibration intensity read 3), which are not modelled. What each number means is unknown. A user who is not logged in returns `0x80960009` with or without an output - [sceUserServiceGetKeyboardType, sceUserServiceGetJapaneseInputType, sceUserServiceGetTraditionalChineseInputType](../../../core/libs/prx/libSceUserService/Export.cpp) (libSceUserService) - AnyPS5 stores no per-user keyboard setting, so the initial user reports the keyboard type of the configured system language (`ANYPS5_LANGUAGE`, English US by default) and 0 for the two input variants, and any other user id returns `USER_SERVICE_ERROR_UNKNOWN_USER`. The language-to-keyboard-type table is the repository owner's (languages 0 to 29); a valid language outside it, such as Ukrainian (30), reports 20 (US English). A value that is not a console language number is rejected by `sceSystemServiceParamGetInt` with an error naming `ANYPS5_LANGUAGE`; the call throws it and leaves the output untouched. The input variant is 0 because the owner's table gives 0 for Japanese and Traditional Chinese, the only languages needing one. `sceConvertKeycodeGetImeKeyboardType` consumes these values. diff --git a/docs/dev/debt/libSceVideoOut.md b/docs/dev/debt/libSceVideoOut.md index 5fdf3bd52f..48a3bade68 100644 --- a/docs/dev/debt/libSceVideoOut.md +++ b/docs/dev/debt/libSceVideoOut.md @@ -18,8 +18,9 @@ - [sceVideoOutRegisterBuffers2](../../../core/libs/prx/libSceVideoOut/src/Buffer.cpp) (libSceVideoOut) - DCC buffers (category 1): `dcc_control` is taken as the CB_DCC_CONTROL block layout, only bits 0x10026c are accepted; `dcc_cb_register_clear_color` as the register-clear texel, only 32-bit values are presented - [sceVideoOutIsOutputSupported](../../../core/libs/prx/libSceVideoOut/src/Output.cpp) (libSceVideoOut) - mode 1 is supported; mode 15 is supported when the current host display runs at 120 Hz or above. Configuring mode 15 requires attribute3 bit 0x40; this single-bit mask is inferred from the measured full value 0x80040, not isolated by a console measurement. Vblanks run at 119.88 Hz until mode 1 restores 59.94 Hz. Display modes are read through SDL; a move to a slower display falls back the configured output mode to mode 1, fires the output-mode event and continues at 59.94 Hz. The console's exact fallback behaviour when a 120 Hz display is lost has not been measured; this fallback avoids the abort in #7877 and matches the HDMI output mode renegotiation that a display change requires. Selecting another host display mode and VRR are not implemented. The console answers per display and per title: with a 120 Hz TV it also answers 1 for mode 15 (119.88 Hz), and [PS5_Vulkan](https://github.com/mihawk-99/PS5_Vulkan/blob/5b5e4fc2d80fdb67a4f61ba9e6a8a424026bb8a5/docs/HARDWARE_FINDINGS.md) measured that `sceVideoOutConfigureOutput` accepts 15 only from a title whose param.json declares high-frame-rate output (attribute3 0x80040). Modes 4, 7, 8, 12-14, 16-17 and 19 return `VIDEO_OUT_ERROR_UNSUPPORTED_OUTPUT_MODE` here, as they did on the measured display. Mode 18 throws, because one console answered 0x80290016 and two others 0x8029001E, the last a PS5 on 13.40 with a 120 Hz TV (measured 2026-10-19 inside a retail title), whose answers for every other mode match the ones here. The name `VIDEO_OUT_ERROR_UNKNOWN_OUTPUT_MODE` for 0x8029001E is ours. Word 3 of the output options is accepted with any value, as on the console, and its meaning is unknown. PPSA12544 probes `0xd000000a`, which the console answers with 0x8029001E, so the title selects 3840 pixels; PPSA26344 at `0xCD0946` passes `(handle, 0xf, nullptr, nullptr, 0)` and branches on the result equalling 1 - [sceVideoOutGetResolutionStatus](../../../core/libs/prx/libSceVideoOut/src/Output.cpp) (libSceVideoOut) - `screenSizeInInch` is 0, the value EDID uses for an undefined size, since the host display's size is not read; a PS5 (firmware 13.40, measured 2026-10-19 inside a retail title) reported its TV's 60.0f. The 48-byte layout, from the declarations in ps5-opengl and EVO-PLAYER-PS5 (#1578), is confirmed there: full size and pane 3840 x 2160, `refreshRate` 3, `flags` 0 and the rest 0 on the title's handle, 0x8029000B for handles 0 to 8. `flags` is 0 here too; its bits are unknown. The pane equals the full size because `sceVideoOutSetWindowModeMargins` applies no margins +- [sceVideoOutGetOutputStatus](../../../core/libs/prx/libSceVideoOut/src/Output.cpp) (libSceVideoOut) - the status after a buffer is registered, a 1440p output and a display below 4K with the console's resolution on Automatic were not measured. A payload process on a PS5 (firmware 12.70) with no buffer registered reported `resolution` 2 right after `sceVideoOutOpen(0xff, 0, 0, NULL)` with the console's resolution on Automatic on a 4K display and 1 with it set to 1080p, and `dynamicRange` 2 with `flags` 0 on an HDR display with HDR on, while this code sets flag bit 0 whenever it reports dynamic range 2. `resolution` is 2 when the current display mode SDL reports for the window's display is at least 3840 pixels wide or 2160 high and 1 otherwise, read with the refresh rate and independent of the registered buffers; on Wayland SDL's mode is the output's logical size (from SDL's source, not run), so a scaled 3840x2160 output reports 1. `APS5_OUTPUT_RESOLUTION` replaces that size - [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. PPSA26344's only call at `0xCCF5EA` passes `(0xFF, 0, 0, nullptr)`. Whether a non-null param continues past offset 24 is also unknown -- [sceVideoOutGetBufferLabelAddress](../../../core/libs/prx/libSceVideoOut/src/Buffer.cpp) exposes a port-owned contiguous array of 16 aligned 64-bit labels and returns its count. Astro Bot PPSA21564's bundled AGC driver passes a pointer output and addresses each label at `base + index * 8`. The count, zero initialization on registration, and release of the previous displayed buffer when its replacement completes follow [shadPS4](https://github.com/shadps4-emu/shadPS4/blob/23c41333822fe8eca4c609b43119180af63ae4ac/src/core/libraries/videoout/video_out.cpp) and its driver; PS5 console equivalence has not been measured. Labels live until the port closes, and reopening creates a new array. Error precedence follows the reference for null output and the existing open-port policy for handles. Exact scanout release timing, repeated same-buffer flips and concurrent guest label stores remain unverified. EOP-flip registration and interrupt tokens remain unimplemented; exposing these labels does not implement that separate API. +- [sceVideoOutGetBufferLabelAddress](../../../core/libs/prx/libSceVideoOut/src/Buffer.cpp) exposes a port-owned contiguous array of 16 aligned 64-bit labels and returns its count. Astro Bot PPSA21564's bundled AGC driver passes a pointer output and addresses each label at `base + index * 8`. The count, zero initialization on registration, and release of the previous displayed buffer when its replacement completes follow [shadPS4](https://github.com/shadps4-emu/shadPS4/blob/23c41333822fe8eca4c609b43119180af63ae4ac/src/core/libraries/videoout/video_out.cpp) and its driver; PS5 console equivalence has not been measured. Labels live until the port closes, and reopening creates a new array. Error precedence follows the reference for null output and the existing open-port policy for handles. Exact scanout release timing, repeated same-buffer flips and concurrent guest label stores remain unverified. EOP-flip registration and interrupt tokens are the separate `sceVideoOutSubmitEopFlip` API. - [GPU flips](../../../core/libs/prx/libSceVideoOut/src/VideoOutDriver.cpp) (libSceVideoOut) - a command-buffer flip of a buffer index with no registered buffer when the GPU reaches it presents black and completes like any flip (flip event, count, `flipPendingNum`). The console's behaviour is unknown; PPSA05684 requires that it completes: its renderer submits a flip of buffer 0 while another thread waits for `sceVideoOutIsFlipPending` to reach 0 before registering the buffers, and `flipPendingNum` counts that flip from its submission as in shadPS4 - [sceVideoOutSubmitFlip](../../../core/libs/prx/libSceVideoOut/src/VideoOutDriver.cpp) (libSceVideoOut, libSceAgcDriver) - in the [AGC driver](../../../core/libs/prx/libSceAgcDriver/Execution/src/Driver/Queues/Submission.cpp), a CPU flip waits for the graphics-queue submissions ahead of it when the calling thread has submitted graphics-queue work itself: a title that submits a frame and flips it without waiting relies on that order, since emulated GPU work finishes later than the console's. A flip from a thread that has never submitted graphics-queue work is presented without waiting for queued submissions, unless a command-buffer or EOP flip ahead of it has not been reached. Such a thread can only know its frame is finished by waiting for it, as the PS5 RADV WSI's flip thread waits on the frame's fence. Whether the console ever orders a CPU flip behind the graphics queue is not measured; `APS5_CPU_FLIP_IN_ORDER=1` orders every CPU flip - [sceVideoOutGetVblankStatus](../../../core/libs/prx/libSceVideoOut/src/Output.cpp) (libSceVideoOut) - the third 64-bit word of the PS5 status (`reserved` in [SceTypes.hpp](../../../core/libs/SceTypes.hpp)) is written 0. On a PS5 (firmware 13.40, measured 2026-10-19) a retail title's own call, caught inside it, got count 0xb16b, `processTime` 0x381a8790 (941 s), third word 0x1c485ffaadc, `processTimeCounter` 0x15dd63c364b, flags 0x04 and phase 0: the third word is a running counter, about 970 s at ~2 GHz, consistent with a TSC, not zero. Its clock and what it counts from are not established. Closes when: the third word's source is measured and written diff --git a/docs/dev/debt/libc.md b/docs/dev/debt/libc.md index 1dd26aff01..2d4e0fc01d 100644 --- a/docs/dev/debt/libc.md +++ b/docs/dev/debt/libc.md @@ -3,6 +3,7 @@ ### Unknown function info - [strtonum](../../../core/libs/prx/libc/src/StringExtensions.cpp) (libc) is unverified on the console. Its signature, decimal parsing, inclusive bounds, error strings, error precedence and errno preservation follow [FreeBSD 11.4 strtonum.c](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/lib/libc/stdlib/strtonum.c). Parsing uses the host `strtoll` with base 10. +- [fegetexcept](../../../core/libs/prx/libc/src/FloatingPoint.cpp) (libSceLibcInternal, implemented in libc) has not been read from the PS5 library or measured on a console. It follows FreeBSD 11.4 amd64 [fenv.h](https://github.com/freebsd/freebsd-src/blob/4554bee35405b36ea19e9e60d6abe6ea534e36e0/lib/msun/x86/fenv.h#L121-L133), reporting the six unmasked x87 exceptions independently of MXCSR; whether the PS5 uses the same rule when the units disagree is unverified. - [sceLibcBacktraceGetBufferSize, sceLibcBacktraceSelf](../../../core/libs/prx/libc/src/Backtrace.cpp) (libc) - signatures and frame record layout inferred from a title (next pointer, u32 module offset, module name at +0x10); the u32 at +0xC is unknown and written as 0. Module names are host file names without the `.guest.prx` suffix, so their case may differ from the PS5 module name. On Windows, unwinding uses `_Unwind_Backtrace` and stops at a frame without `.ehmeta` or `.ehfram` tables whose PE unwind data has an exception or termination handler, leaving the trace empty or cut short while still returning 0. Implemented only on Linux and Windows; other platforms throw - [ApplicationGuestHeapApi](../../../core/libs/prx/libc/src/ApplicationProcessParameters.cpp) (libc) - on Linux, HLE application allocation uses a loaded guest SDK's ten allocator exports when the title's replacement table has an allocator entry and the exports belong to one native module. With an empty table it keeps the default heap, since a bundled `libc.prx` may export the allocator NIDs while forwarding them to HLE. Its CRT owns application allocator initialization and finalization; HLE retains the module until process exit. Partial or mixed providers throw. This avoids reinitializing an allocator already used by SDK startup; console behavior and other host platforms remain unverified. @@ -18,9 +19,11 @@ - [fegetround, fesetround, fetestexcept, feclearexcept, fegetexceptflag and fesetexceptflag](../../../core/libs/prx/libc/src/FloatingPoint.cpp) (libSceLibcInternal, implemented in libc) follow FreeBSD 11.4 amd64 [fenv.h](https://github.com/freebsd/freebsd-src/blob/4554bee35405b36ea19e9e60d6abe6ea534e36e0/lib/msun/x86/fenv.h): rounding modes use x87 control bits 10-11, exception flags include denormals (`0x3f`), and `fexcept_t` is 16 bits. They access the current host thread's x87 and MXCSR state. Under Rosetta 2 the six MXCSR exception masks always read back set, so an SSE exception the guest unmasks stays masked; the x87 masks and the other MXCSR bits are kept. This console ABI is assumed, as for `fegetenv`, not measured on hardware. Null flag pointers throw instead of faulting. - [feenableexcept, fedisableexcept, __feenableexcept, __fedisableexcept, fegettrapenable and fesettrapenable](../../../core/libs/prx/libc/src/FloatingPoint.cpp) (libc) change only the x87 masks under Rosetta 2: MXCSR keeps its six exception masks set there, so no SSE trap is enabled, and the enable and disable results, which report the traps enabled on both units, report none - [fegetenv](../../../core/libs/prx/libc/src/Math.cpp) (libSceLibcInternal, implemented in libc, NID `psx0YiAKm7k`) follows FreeBSD 11 amd64 [fenv.c](https://github.com/freebsd/freebsd-src/blob/4554bee35405b36ea19e9e60d6abe6ea534e36e0/lib/msun/amd64/fenv.c) with the 32-byte `fenv_t` of [x86/fenv.h](https://github.com/freebsd/freebsd-src/blob/4554bee35405b36ea19e9e60d6abe6ea534e36e0/lib/msun/x86/fenv.h): `fnstenv` fills the 28-byte x87 part, MXCSR follows at 0x1c, and the x87 control word is reloaded because `fnstenv` masks every x87 exception. That the console's `libSceLibcInternal` has the same code and layout is assumed, not read from its binary. A null pointer throws instead of faulting +- [feholdexcept](../../../core/libs/prx/libc/src/Math.cpp) (libSceLibcInternal, implemented in libc, NID `1kduKXMqx7k`) - console equivalence and module/library versions are unverified. The signature and 32-byte amd64 environment layout follow the public SDK [fenv.h](https://github.com/ps5-payload-dev/sdk/blob/a7685ce8e97ce1f8a4f38253a41d54241c9124f1/include/freebsd/fenv.h); saving both units, masking exceptions and clearing pending flags follows FreeBSD 11 amd64 [fenv.c](https://github.com/freebsd/freebsd-src/blob/4554bee35405b36ea19e9e60d6abe6ea534e36e0/lib/msun/amd64/fenv.c). The NID comes from the public 3.20 stub inventory, not a module examined here. Null storage throws before modifying the environment, where the reference dereferences it +- [feupdateenv](../../../core/libs/prx/libc/src/Math.cpp) (libSceLibcInternal, implemented in libc, NID `YXQ4gXamCrY`) follows FreeBSD 11.4 amd64 [fenv.c](https://github.com/freebsd/freebsd-src/blob/d106002967eff712894ab8b5337bbbff106ce5e0/lib/msun/amd64/fenv.c): capture the old x87/MXCSR exception union (`0x3f`), restore the saved 32-byte environment, raise those flags in both units, then `fwait`. The console's layout, behavior and module versions are unmeasured; state belongs to the current host thread. A null input or MXCSR bits unsupported by the host's `FXSAVE` mask throw before changing floating-point state; the mask's architectural zero fallback is `0xffbf`. Console fault behavior for malformed environments is unknown. The separate `__fe_dfl_env` object remains unimplemented; no host `FE_DFL_ENV` sentinel is substituted. - [Guest arena](../../../core/libs/prx/libc/src/GuestArena.cpp) (libc) - the application map area is assumed to end at 0xFC_0000_0000, as `SCE_KERNEL_APP_MAP_AREA_END_ADDR` does on the PS4. On Windows, host allocations made in the arena before libc loads stay, and a fixed guest mapping over one throws. The console's low-address layout is not measured. Linux and Windows reserve free addresses from 64 KiB, except host mappings and low chunks the Linux address policy refuses; null hints still allocate from 8 GiB. macOS and ASan builds still start the arena at 8 GiB and cannot supply low 32-bit addresses. Guest thread creation ignores a stack address set in its attributes. A host-loaded executable is not moved, so a PIE above the arena gets no memory within 2 GiB of its code ([#5217](https://github.com/boykopovar/AnyPS5/issues/5217)) - [sceLibcInternalBacktraceForGame](../../../core/libs/prx/libc/src/HeapDiagnostics.cpp) (libSceLibcInternal, implemented in libc) - `(const char* heapName)` and its return follow the `libc.prx` ASTRO BOT (PPSA21564 01.007.000) bundles: its four calls (0x6801, 0x7F46, 0xE75E, 0xEE02) are in the `malloc_stats` and `sceLibcMspaceMallocStats` paths, each after a `[%s] ...` line printed with the same heap name buffer, and the caller ignores the result and continues. The console's backtrace output is not reproduced: the heap name and the guest return address are written to the host standard error -- [sceLibcInternalHeapErrorReportForGame](../../../core/libs/prx/libc/src/HeapDiagnostics.cpp) (libSceLibcInternal, implemented in libc) - `(void* heap, void* block, uint32 error)` follows the same `libc.prx` (call at 0xF634): it passes the heap, the block and the nonzero result of its block check, and then calls a function that never returns, so the report only has to return. The meaning of the error values is unknown; the three values and the guest return address are written to the host standard error +- [sceLibcInternalHeapErrorReportForGame](../../../core/libs/prx/libc/src/HeapDiagnostics.cpp) (libSceLibcInternal, implemented in libc) - `(void* heap, void* block, uint32 error)` writes to the guest's standard output what a payload process on a PS5 (firmware 13.60) wrote: `[SceLibc] A heap error is detected.`, `[SceLibc] -- Mspace Name --`, then the C string at heap+0x3B0 followed by ` (%2u)` of the 32-bit value at heap+0x3F4 when it is not 0, the reason `segment out of range.` for error 1 only (38 other measured values print none), and `pid:<0x..> tid:<0x..>`, and returns. The pid and tid are the host's, where the console prints its own process and kernel thread ids. The block's contents did not change the console's output and it is not read here; the values and the guest return address also go to the host's standard error. A null heap faulted on the console and throws here. What the other heap fields and error values mean is unknown - [sceLibcPafMspaceCreate, Destroy, Malloc, Memalign, PosixMemalign, Free, Calloc, Realloc, Reallocalign, MallocStats and MallocStatsFast](../../../core/libs/prx/libc/src/Mspace.cpp) - PAF exports delegate to `sceLibcMspace` without malloc hooks; `Memalign` and `PosixMemalign` forward to their `Mspace` counterparts with owner ignored and are unverified on console. `Create`'s options block (version, type 1 or 2 at +2, field at +0x10) throws when non-null as its meaning is unknown. Block footers are not modelled, so owners are ignored: on PS5 firmware 13.60 mismatched owners in `Reallocalign` or in-place `Realloc` kill the process and `MallocStats` returns 1 if non-heap-owned blocks exist, which is not reproduced. `MallocUsableSize`, `Trim`, `IsHeapEmpty`, `CheckMemoryBounds`, `ReportMemoryBlocks` and `GetFooterValue` are not implemented. - [malloc_usable_size](../../../core/libs/prx/libc/src/ApplicationHeap.cpp) (libc) - with the default allocator it returns the size the block was requested with (the range registered for it), not the rounded-up chunk size Sony's allocator reports; both satisfy "at least the requested size", and bytes past the request would not be write-watched. With an application allocator it calls entry 9 of the replacement table (`malloc_usable_size` in KytyPS5's `MallocReplace` order) and throws when the table leaves it null, like `malloc_stats_fast` - [malloc_stats_fast](../../../core/libs/prx/libc/src/GuestHeap.cpp) (libc) - with the default allocator it fills the 0x28-byte managed-size block as `sceLibcMspaceMallocStatsFast` does, from the default heap's own accounting, not Sony's allocator: the system sizes count the 1 MiB spans of its small-block classes and its committed large blocks, cached free ones included, and the in-use sizes count each handed-out block at its size-class or 64 KiB-granule size, header included. A block size below 0x28 or a null pointer returns 22, as in the mspace version; the version field is not checked. Ship of Harkinian (PPSA99320) calls it about 10 s after start-up. @@ -32,15 +35,18 @@ - [Dinkumware atomics](../../../core/libs/prx/libc/src/DinkumwareAtomics.cpp) (libc) - every operation is sequentially consistent whatever memory order it is given, which is never weaker than the console. Matching the `libc.prx` bundled with PPSA08427 (that binary run side by side on every order from 0 to 7), `_Atomic_load_8` returns 0 for a release, acq_rel or invalid order and the 8-byte exchange and fetch operations return 0 without touching memory for an order above `memory_order_seq_cst`; the generic `_Atomic_copy`, `_Atomic_exchange` and `_Atomic_compare_exchange_*` lock the byte flag passed first, as the console does - [std_execute_once](../../../core/libs/prx/libc/Export.cpp) (libc) - assumed to be the plain-named twin of `std::_Execute_once` (`_ZSt13_Execute_onceRSt9once_flagPFiPvS1_PS1_ES1_`, the callee of Dinkumware `call_once`): it delegates there and gets the [thread.once.callonce] semantics already verified for that symbol (one invocation per flag, concurrent callers block until it completes, and a failing or throwing invocation leaves the flag unready so the next caller runs it). The name equivalence is not confirmed on a title, and the callback convention (called with (null, arg, null), non-zero return meaning success) is the twin's, unverified for this symbol - [__atomic_load, __atomic_store, __atomic_exchange and __atomic_compare_exchange](../../../core/libs/prx/libc/src/AtomicBuiltins.cpp) (libc) - the generic, size-parameterised libatomic entry points are a single `ud2` in the `libc.prx` bundled with PPSA08427 (disassembly), so the console traps on any call; these throw instead +- [Sized __atomic calls](../../../core/libs/prx/libc/src/DinkumwareAtomics.cpp) (libc) - the 36 `__atomic_load_N`, `__atomic_store_N`, `__atomic_exchange_N`, `__atomic_compare_exchange_N` and `__atomic_fetch_{add,sub,and,or,xor}_N` calls (N = 1, 2, 4, 8) are sequentially consistent whatever order they are given. The `libc.prx` bundled with PPSA08427, run side by side on random values and on every order from -1 to 7, switches on the order but always uses a locked instruction or CAS loop, except that a store with an order other than `memory_order_seq_cst` is a plain store, so these are never weaker than the console. Unlike `_Atomic_load_8` and the 8-byte `_Atomic_*` exchange and fetch calls above, the console accepts every order here, including release, acq_rel and invalid ones, and these do the same - [Dinkumware mutexes and conditions](../../../core/libs/prx/libc/src/Threads.cpp) (libc) use pointer-to-handle arguments, matching the console aerolib signatures reported in [#3079](https://github.com/boykopovar/AnyPS5/pull/3079). The title's `_Mtx_lock` maps `0` to `_Thrd_success` (0), `0x8002000b` to `_Thrd_busy` (3), and other failures to `_Thrd_error` (4); its internal mutex init recognizes recursive flag `0x100`, while every other type uses default attributes. The observed relock failure for a plain mutex suggests those defaults are error-checking, but that has not been confirmed on hardware. The title's `_Mtx_unlock` and `_Cnd_wait` return success regardless of their internal result. The host implementation treats invalid ownership preconditions as no-ops, and throws for invalid handles, host wait errors and destroying a locked mutex as debug guards; those cases are not confirmed against the console. - [Floating-point compiler-rt builtins](../../../core/libs/prx/libc/src/FloatBuiltins.cpp) (libc) follow the `libc.prx` bundled with PPSA08427 (disassembly, and that binary run side by side on random inputs) where it differs from current compiler-rt: the soft-float `__add`, `__sub`, `__mul` and `__div` (`sf3`, `df3`) round to nearest, raise no exception flags and ignore MXCSR, here done with SSE under a default MXCSR that is restored afterwards; division flushes a quotient below the smallest normal to a signed zero; `__extendsfdf2` and `__truncdfsf2` copy NaN payloads unshifted; float-to-integer conversions do not saturate but shift the significand by a count masked to the shift width. `__mulsc3`, `__muldc3`, `__divsc3` and `__divdc3` fuse the same products as the console with `fma` and scale through the x87 like its `scalbn`, so their values match, but NaN payloads and exception flags can differ, as the host `fma` and `logb` are not the console's VEX FMA code and libm -- [random, srandom, initstate, setstate](../../../core/libs/prx/libc/src/Random.cpp) (libc) follow FreeBSD 11 [random.c](https://github.com/freebsd/freebsd-src/blob/4ab2e064d7950be84256d671a7ae93f87cc6aa36/lib/libc/stdlib/random.c): the additive feedback generator on a default 128-byte `TYPE_3` table, `good_rand` seeding, state sizes rounded down to 8, 32, 64, 128 or 256 bytes, and `setstate` returning the previous state or null for a rear position past the degree. `initstate` below 8 bytes returns null without setting errno, as that file and its manual do (glibc sets `EINVAL`). FreeBSD does not lock the generator; here every call is serialised by one lock, so concurrent callers draw the serial sequence between them. A null state throws, where FreeBSD faults. That the console's `libSceLibcInternal` uses this FreeBSD code and not another generator is unverified +- [random, srandom, initstate, setstate](../../../core/libs/prx/libc/src/Random.cpp) (libc) - not measured: the state `setstate` returns and its null return for a rear position past the degree, `errno` after `initstate` below 8 bytes, a null state (throws here), an `initstate` seed other than 1, concurrent callers (one lock serialises every call here) and a title's own libc. Measured in a payload process on a PS5 (firmware 12.70): a fresh process draws the `srandom(1)` sequence; `srandom` seeds an additive feedback generator through a `good_rand` that reads its argument as an int32, turns 0 into 123459876 and applies `x = 16807 * (x % 127773) - 2836 * (x / 127773)`, adding 0x7fffffff to a negative result, which reproduces seeds 0, 1, 2, 12345, 0xffffffff and 0x80000000; `initstate` returns null at 0 and 7 bytes and selects types 0 to 4 by size as measured at 8, 15, 31, 32, 63, 64, 127, 128, 129, 255, 256, 257 and 1000 bytes. +- [srandomdev](../../../core/libs/prx/libc/src/Random.cpp) (libSceLibcInternal, implemented in libc) is not verified against the console. The firmware 3.20 [export inventory](https://github.com/DNNDHH/PS5-3.20_Libs/blob/b2a77c09cd71c01f34c790cfbe98c751a13262ad/libSceLibcInternal.c) names NID `a2MOZf++Wjg`; it follows FreeBSD 11 [random.c](https://github.com/freebsd/freebsd-src/blob/4ab2e064d7950be84256d671a7ae93f87cc6aa36/lib/libc/stdlib/random.c), filling every active generator word from host OS entropy instead of `KERN_ARND`, then resetting additive feedback positions without warming up. An entropy failure throws rather than leaving `sysctl`'s failure unchecked. Calls share the generator lock. - [Integer compiler-rt builtins](../../../core/libs/prx/libc/src/IntegerBuiltins.cpp) (libc) - the console traps in `__divsi3`, `__modsi3` and the 64- and 128-bit divisions on a zero divisor (`div`) and in `__negv*`, `__absv*` and `__mulv*` on overflow (an abort helper, then `ud2`); these throw instead. As in the `libc.prx` bundled with PPSA08427 (disassembly), `__addv*` and `__subv*` wrap without an overflow check, and `__udivsi3`, `__umodsi3` and `__udivmodsi4` divide in software, giving a quotient of 0 and the dividend as remainder for a zero divisor; `__clzdi2` and `__ctzdi2` of 0 return 32 and `__clzti2` and `__ctzti2` of 0 return 64 - [qsort](../../../core/libs/prx/libc/src/FileAndHeap.cpp) (libc) follows FreeBSD 11 [qsort.c](https://github.com/freebsd/freebsd-src/blob/5ed7eb0d97ba4436218e810f61bd059acba984c2/lib/libc/stdlib/qsort.c): Bentley and McIlroy's quicksort, with insertion sort below 7 elements and after a partition pass that swaps nothing, so elements that compare equal end in FreeBSD's order and the comparator is called in the same sequence. That the console's libc uses this FreeBSD code is unverified - [memcpy_s, memmove_s, strcpy_s, strncpy_s, strcat_s, strncat_s, wmemcpy_s, wmemmove_s, wcsncpy_s, wcscat_s, wcsncat_s](../../../core/libs/prx/libc/src/Strings.cpp) (libc) use the C11 Annex K destination size constraints, with RSIZE_MAX taken as (SIZE_MAX >> 1) / sizeof(char16_t) for wide operations and SIZE_MAX >> 1 for byte operations. An oversized destination size returns ERANGE (34) without touching the destination; the console's RSIZE_MAX and exact error value are unverified. Constraint violations return an error without invoking the registered constraint handler, as before. - [wcsncat_s and wcscat_s](../../../core/libs/prx/libc/src/Strings.cpp) (libc) refuse a copy as overlapping only when the written range `[dest + len1, dest + len1 + len2)` meets the read range `[src, src + len2)`. The rule comes from a static reading of the console's `wcsncat_s` in `libc.prx` (not run on a console); `wcscat_s` gets it through `wcsncat_s`, and its own console code was not read. The narrow `strncat_s` keeps a wider rule, `[dest, dest + used + length + 1)` against `[src, src + length + 1)`, whose console code was not read either, so it refuses some copies whose wide equivalent proceeds. - [snwprintf_s](../../../core/libs/prx/libc/src/FormattingWide.cpp) (libc) - follows C11 K.3.9.1.3 with `RSIZE_MAX` assumed to be `SIZE_MAX >> 1`. A runtime-constraint violation only returns a negative value: the handler installed with `set_constraint_handler_s` is not invoked, and the console's default handler is unknown. An invalid multibyte `%s` argument is copied byte by byte instead of being reported as an encoding error, as in `vswprintf` - [swprintf_s and vswprintf_s](../../../core/libs/prx/libc/src/FormattingWide.cpp) (libc) - follow C11 K.3.9.1.4 with `RSIZE_MAX` assumed to be `SIZE_MAX >> 1`: the complete output, including the terminating null, must fit in the buffer, otherwise it is a runtime-constraint violation that sets `buffer[0]` to zero and returns a negative value, and the buffer is not partially written. Argument and encoding checks match `snwprintf_s` +- [strerror](../../../core/libs/prx/libc/src/Errors.cpp) (libc) - follows the `libc.prx` that CUSA04865 and CUSA12435 ship, loaded on a PS4 Pro (firmware 10.71): 0 gives "No error", 1 to 94 the FreeBSD texts, any other number "error_code=0x%08x", and errno is left alone. The console's system libc, which homebrew without a bundled `libc.prx` uses, answers differently ("No error information" for 0 and unknown numbers, 95 and 96 named). `strerror_r` and `perror` keep "No error: 0" and "Unknown error: %d"; they are not measured. - [strerror_s and strerrorlen_s](../../../core/libs/prx/libc/src/WideConversion.cpp) (libc) follow [C11 K.3.7.4.2 and K.3.7.4.3](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf), using the existing FreeBSD error messages and unknown-error formatting. Truncation writes an ellipsis when capacity exceeds 3; constraint failures leave the destination untouched and invoke the registered guest-ABI handler. The console's `RSIZE_MAX` (`SIZE_MAX >> 1` here), nonzero returns (`EINVAL` 22 for constraints, `ERANGE` 34 for truncation), handler arguments and default abort behavior are unverified. Messages remain in the C locale, and guest errno is preserved. - [getw and putw](../../../core/libs/prx/libc/src/BinaryWordIo.cpp) (libc) follow FreeBSD 11.4 [getw.c](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/lib/libc/stdio/getw.c) and [putw.c](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/lib/libc/stdio/putw.c): one 32-bit little-endian word, `getw` returns -1 for EOF or an error, and `putw` returns 0 on success or -1 on error while holding the stream lock. They use the existing host-backed streams, so buffering and error timing follow the host CRT. Null streams throw and closed streams return -1 with `EBADF`, as existing byte I/O does. Console behavior is unverified. - [vsnwprintf_s](../../../core/libs/prx/libc/src/FormattingWide.cpp) (libc) - `va_list` counterpart of `snwprintf_s` (C11 K.3.9.1.3): truncates to the buffer and returns the full length, with the same constraints and argument checks @@ -55,11 +61,12 @@ - [__umodti3](../../../core/libs/prx/libc/src/RuntimeSupport.cpp) (libc) - the hardware traps on a zero divisor; this throws instead - [__modti3](../../../core/libs/prx/libc/src/RuntimeSupport.cpp) (libc) - the hardware traps on a zero divisor; this throws instead -- [snprintf and vsnprintf](../../../core/libs/prx/libc/src/Formatting.cpp) (libc) reject sizes above `INT_MAX + 1` with `EOVERFLOW` (84), a terminating NUL and return value -1, following FreeBSD 11.4 [snprintf.c](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/lib/libc/stdio/snprintf.c) and [vsnprintf.c](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/lib/libc/stdio/vsnprintf.c). This limit is unverified on the console. A null buffer at an oversized nonzero size returns the error without writing, where FreeBSD dereferences it. +- [snprintf and vsnprintf](../../../core/libs/prx/libc/src/Formatting.cpp) (libc) accept any size up to `SIZE_MAX`, as a payload process on a PS5 (firmware 13.60) does. A null buffer with a nonzero size throws here, where the console writes through it, and a result longer than `INT_MAX` throws, which was not measured ### Functional - [_Cosh](../../../core/libs/prx/libc/src/Math.cpp) (libc) follows the two-argument Dinkumware CRT helper used by `std::complex`, including a scaled large-finite-input path; the signature, exact result and floating-point exception behavior are not verified against the console. +- [std::thread::hardware_concurrency](../../../core/libs/prx/libc/src/Threads.cpp) (libc, NID `CHrhwd8QSBs`) returns the host's `std::thread::hardware_concurrency()` (which may be 0 when unknown) instead of the console's processor count; no reference implements it and the value a title expects (PPSA34015 imports it) is unverified. - [fmodf](../../../core/libs/prx/libc/src/Math.cpp) (libc) - console `errno`, exception flags and NaN payloads remain unverified. The wrapper preserves `errno` like the existing double `fmod`, following FreeBSD 11.4 [`e_fmodf.c`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/msun/src/e_fmodf.c); host `std::fmod` still supplies the result and exception flags. - [_FSinh and _FCosh](../../../core/libs/prx/libc/src/Math.cpp) (libc) finite results are not verified bit for bit against the console: the scaled product uses the host's binary64 hyperbolic function before rounding to binary32, rather than the float rational and `_FExp` sequence in PRAGMATA's bundled `libc.prx`. NaN preference and payloads, signed zeros, infinite operands and the range-error thresholds follow that binary's instructions; the system `libSceLibcInternal` implementation and nondefault flush-to-zero modes are unverified. @@ -69,25 +76,28 @@ - [strtof and wcstof](../../../core/libs/prx/libc/src/Strings.cpp) (libc) - `ERANGE` follows the `libc.prx` bundled with PPSA08427 (J03547016) and PPSA03671 (J03848509), both run side by side with this libc on a Linux host: a decimal input whose result is a nonzero subnormal sets it only when inexact, a hexadecimal one only when it lies below the smallest subnormal and rounds up to it, and underflow to zero and overflow set it. Both builds also take a `NAN(...)` payload in `strtof` from the sequence's leading digits (`nan(0x123)` is `0x7fc00123`, `-nan(5)` is `0xffc00005`); the sign is kept here, the payload is not. `libSceLibcInternal`'s `_Stof` differs: no `ERANGE` for hexadecimal input, a positive `nan`, and after a minus sign a failed parse is `-0.0` for a lone sign and `+0.0` before an `inf`/`nan` spelling, the reverse of `libc.prx`. Decimal `strtod` sets `ERANGE` for a nonzero subnormal in J03547016 but not in J03848509 or `libSceLibcInternal`, whose result is kept. - [Guest time conversions](../../../core/libs/prx/libc/src/Time.cpp) rewrite the host process `TZ` when a guest changes its own `TZ`, so other host consumers of that variable (including libSceRtc local time) shift too; code outside libc does not take the zone lock. Zone syntax follows the host C runtime, not FreeBSD: Windows only accepts the `std offset [dst]` form, treats an empty guest `TZ` as `UTC0` and reads `:Europe/Berlin` as an abbreviation (for example `rop`); only Linux honours zone file names. If the guest accepts a `TZ` value that the host rejects (on Windows, for example, one longer than the host environment limit), conversions throw `std::runtime_error`. A `tm_zone` saved before a `TZ` change can refer to storage now holding the new zone name, changing `%Z` output. +- [cbrtf](../../../core/libs/prx/libc/src/Math.cpp) (libc) is FreeBSD msun's [`s_cbrtf.c`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/msun/src/s_cbrtf.c): a 5-bit first guess from the exponent bits, then two Newton steps in double precision. It sets no `errno`, as the `libc.prx` that titles ship does not. Floating-point exceptions are not compared. It gives the same bits as the `cbrtf` of the `libc.prx` bundled with PPSA07923, PPSA05916 and PPSA13934 for all 2^32 arguments, run on a Windows host. - [__isfinitel](../../../core/libs/prx/libc/src/Math.cpp) - classifies the x87 80-bit exponent without narrowing to double or performing floating-point comparisons, following [FreeBSD msun](https://github.com/freebsd/freebsd-src/blob/main/lib/msun/src/s_isfinite.c). The signature and classification follow FreeBSD; console behaviour is unmeasured. Requires little-endian x87 long double stored in 16 bytes, as the existing guest long-double ABI does. - [logf](../../../core/libs/prx/libc/src/Math.cpp) (libc) is FreeBSD msun's [`e_logf.c`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/msun/src/e_logf.c), with `vzero` read at run time as msun's `volatile` read does. As in the `libc.prx` that titles ship, `errno` is set to `ERANGE` for ±0 and to `EDOM` for every argument with the sign bit set, negative NaNs and -inf included; positive NaNs and +inf leave it unchanged. Floating-point exceptions are not compared. It gives the same bits and `errno` as the `logf` of the `libc.prx` bundled with PPSA07923 and PPSA05916 for all 2^32 arguments, run on a Windows host. The FMA-contracted build bundled with PPSA13934 differs from it by 1 ulp on 961,881 arguments, so no single port matches every title's last bit. - Windows direct-memory backing reserves a paging-file section. A [direct memory page](../../../core/libs/prx/libc/include/WindowsMappings.hpp) mapped at one guest address is private write-watched memory at that address and is not committed in the section; a page mapped at a second address moves its whole resident run into the section (committed there) and every address becomes a view of it, tracked by write protection. An unmapped resident page that is not all zeros is kept in a private reservation until it is mapped again. Committed section contents remain across unmap/remap and are released when the backing and its remaining views are destroyed. A host import alias over part of a resident run is refused. Linux retains its existing backing. Peak gameplay memory and Windows versions other than the tested host remain unverified. - Stable Windows resident runs of at least 256 KiB use temporary read-only protection for clean write-watch proofs. The first CPU write restores the logical protection of the whole run; that run remains on native write watching until it is remapped, so repeatedly rewritten runs stay writable. Native kernel writes into guest buffers must hold [GuestArena::HostWrite](../../../core/libs/prx/libc/include/GuestArena.hpp), as shared views already require: an unbracketed WinAPI ReadFile can fail with ERROR_NOACCESS while the resident proof is armed. Guest read, pread and socket receive paths already bracket their destinations. GPU host-import access suppresses resident rearming without inventing shared-page stores. This is a host integration requirement, not a change to guest logical protection. - [getc_unlocked, putc_unlocked, getchar_unlocked, putchar_unlocked](../../../core/libs/prx/libc/src/StandardStreams.cpp) (libc) - byte I/O uses the host unlocked operations; guest EOF/error flags and macro counters follow `FileStream`. Linux status queries use `feof_unlocked`/`ferror_unlocked` because [glibc `feof`](https://github.com/bminor/glibc/blob/d2097651cc57834dbfcaa102ddfacae0d86cfb66/libio/feof.c) and [`ferror`](https://github.com/bminor/glibc/blob/d2097651cc57834dbfcaa102ddfacae0d86cfb66/libio/ferror.c) can acquire the stream lock. Windows uses [the CRT no-lock byte operations](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/fgetc-nolock-fgetwc-nolock) and `feof`/`ferror`, verified under a held stream lock on UCRT. Console behavior and other host CRTs remain unverified. -- [strcasecmp and strncasecmp](../../../core/libs/prx/libc/src/Strings.cpp) (libc) - console locale behavior is unmeasured. They use the guest's C-locale case conversion and the folded unsigned-byte difference of FreeBSD 11.4's [strcasecmp.c](https://github.com/freebsd/freebsd-src/blob/releng/11.4/lib/libc/string/strcasecmp.c); other guest locales are not implemented. +- [strcasecmp and strncasecmp](../../../core/libs/prx/libc/src/Strings.cpp) (libc) - console locale behavior is unmeasured. They use the guest's C-locale case conversion and return -1, 0 or 1, the sign of the folded unsigned-byte difference, as the `libc.prx` bundled with eleven titles does (FreeBSD 11.4's [strcasecmp.c](https://github.com/freebsd/freebsd-src/blob/releng/11.4/lib/libc/string/strcasecmp.c) returns the difference itself); other guest locales are not implemented. - [feof_unlocked and ferror_unlocked](../../../core/libs/prx/libc/src/StandardStreams.cpp) (libSceLibcInternal, implemented in libc) have no console measurement. The firmware 3.20 [export inventory](https://github.com/DNNDHH/PS5-3.20_Libs/blob/b2a77c09cd71c01f34c790cfbe98c751a13262ad/libSceLibcInternal.c) names NIDs `NuydofHcR1w` and `yxbGzBQC5xA`. They follow FreeBSD 11 [feof.c](https://github.com/freebsd/freebsd-src/blob/release/11.0.0/lib/libc/stdio/feof.c) and [ferror.c](https://github.com/freebsd/freebsd-src/blob/release/11.0.0/lib/libc/stdio/ferror.c), returning 0 or 1 from the stream status without acquiring the stream lock. They inherit the host CRT limitations of the unlocked byte operations above. -- [fgetws, getwc, getwchar, putwc, putwchar](../../../core/libs/prx/libc/src/FormattingWide.cpp) (libc) - wide stream I/O encodes and decodes wide characters as UTF-8 on the underlying byte stream; `fgetws` reads characters into the destination buffer until newline or EOF and null-terminates, returning null on stream error. Console wide stream buffering and encoding details remain unverified. -- [Closed standard descriptors](../../../core/libs/prx/libc/src/StandardStreams.cpp) (libc) - on Linux and macOS, a descriptor 0, 1 or 2 that is closed when libc loads is opened on `/dev/null` (read-only for 0, write-only for 1 and 2) by a static initializer of libc, which runs before the libraries that depend on libc initialize and before the title starts. Within libc this relies on no static initializer opening a descriptor before it, which `guest_closed_standard_descriptors` checks. glibc does the same for set-user-ID programs ([`check_fds.c`](https://sourceware.org/git/?p=glibc.git;a=blob;f=csu/check_fds.c)). Without it the first descriptor a library opens, the write watch's `userfaultfd` for one, becomes the guest's standard input or output. What the console gives a title as its standard descriptors is not measured, and on Windows a process started without standard handles is not handled. +- [gets](../../../core/libs/prx/libc/src/StandardStreams.cpp) (libSceLibcInternal, implemented in libc, NID `Ok+SYcoL19Y`) follows [FreeBSD 11.4 gets.c](https://github.com/freebsd/freebsd-src/blob/d106002967eff712894ab8b5337bbbff106ce5e0/lib/libc/stdio/gets.c) for byte input, newline removal and EOF, without PS5 verification or known module/library versions. It reads the guest stdin under its stream lock and preserves the destination when EOF or a read error precedes any byte; a partial line is terminated and returned. A null destination throws; its console behavior is unverified. It emits no diagnostic; FreeBSD writes a warning once, and the console's diagnostic behavior and stream orientation are unverified. +- [fgetws, getwc, getwchar, putwc, putwchar](../../../core/libs/prx/libc/src/FormattingWide.cpp) (libc) - wide stream I/O encodes and decodes wide characters as UTF-8 on the underlying byte stream; `fgetws` follows the `libc.prx` that titles ship (read from disassembly of three titles' copies, PPSA13934, PPSA07923 and PPSA05916, not measured on a console): it returns null for a size below 2 without writing the buffer, `errno` or the stream, stores each `fgetwc` result as its low 16 bits until a newline (compared on all 32 bits), `-1` or a full buffer, and null-terminates and returns the buffer whenever it stored a character, also after a read error. A null buffer or stream, which the console dereferences, sets `errno` to 22 here. The calls the console makes before and after reading, and the one it makes when bits 0xC of the stream's second flag byte are set, are not reproduced. Console wide stream buffering and `fgetwc`'s decoding remain unverified. +- [Closed standard descriptors](../../../core/libs/prx/libc/src/StandardStreams.cpp) (libc) - console startup descriptors remain unmeasured. [Halo: Combat Evolved's PS5 host](https://github.com/thelinkin3000/halo-ce-universal/blob/201e0f18011a30ea6d098ad249bf17eba005d653/port/ps5/host/host_main.c#L407-L429) expects 0 to 2 closed and opens its eboot on them; here it leaves them open. Missing descriptors are opened on `/dev/null` on Linux/macOS or `NUL` on Windows, read-only for 0 and write-only for 1 and 2. Windows also reattaches the UCRT streams before any guest `FileStream` captures their descriptor. Initialization runs before libraries depending on libc; no earlier libc initializer may consume a missing standard descriptor. The `guest_closed_standard_descriptors` and `guest_closed_standard_descriptors_windows` tests cover startup. - [Guest path names](../../../core/libs/prx/libc/src/General.cpp) (libc) are checked against FreeBSD 11.4's `NAME_MAX` (255 bytes per component) and `MAXPATHLEN` (1024 bytes with the terminating NUL) before they are resolved, and a longer name fails with `ENAMETOOLONG`, as `namei` does. The host's own limit still applies below that: on Windows a path whose host form exceeds `MAX_PATH` fails with `ENOENT`. A backslash counts as a path separator for the component length, because the guest path resolver turns it into `/` before the host call, while FreeBSD treats it as an ordinary name character, so a 301-byte name that contains one is accepted here. `sceNgs2ParseWaveformFile` resolves its path without a try block, so a long component ends in an uncaught `filesystem_error` there where a missing file ended in a `runtime_error` (both end the title). A path through a regular file (`file/x`, `file/`) is not always `ENOTDIR` on the calls that resolve it. None of this was measured on a console. - [Guest path names](../../../core/libs/prx/libc/src/General.cpp) (libc) fail on macOS with `ENAMETOOLONG` once their host form, the root directory followed by the guest path, reaches 1024 bytes, so a guest path of 1023 bytes, which FreeBSD accepts, fails there under any root directory - [ftrylockfile](../../../core/libs/prx/libc/src/RuntimeSupport.cpp) (libc) - tries the host CRT stream lock that `flockfile`, `funlockfile` and each locking host stdio call on the stream take, so it returns -1 while another thread holds the stream through `flockfile` or is inside such a call, and 0 when it acquires the lock or already owns it (recursively, released by as many `funlockfile` calls), as FreeBSD 11 [`_ftrylockfile`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/lib/libc/stdio/_flock_stub.c#L89-L104) does. A libc export holds the lock only for each host call it makes, not across several host calls or while it updates the guest `FILE` prefix. Linux uses glibc `ftrylockfile`. UCRT has no try variant of `_lock_file`, so Windows calls `TryEnterCriticalSection` on the `CRITICAL_SECTION` that `_lock_file` enters, found through the private UCRT stream layout (`__crt_stdio_stream_data` in `ucrt/inc/corecrt_internal_stdio.h`, unchanged from Windows SDK 10.0.19041 to 10.0.26100); another CRT or a change of that layout breaks it. A null or closed stream throws where FreeBSD dereferences it +- [sinf, cosf, tanf and sincosf](../../../core/libs/prx/libc/src/Math.cpp) (libc) are FreeBSD msun's [`s_sinf.c`, `s_cosf.c` and `s_tanf.c`](https://github.com/freebsd/freebsd-src/tree/release/11.4.0/lib/msun/src) over its double-precision kernels and `e_rem_pio2f.c`, with `k_rem_pio2.c` (for `|x| >= 2^28 * pi/2`) ported only for one input word and `prec` 0; `sincosf` returns the two. As in the `libc.prx` that titles ship, only an infinite argument sets `errno` (`EDOM`); NaN leaves it unchanged. Floating-point exceptions are not compared. All four give the same bits and `errno` as the copies bundled with PPSA07923, PPSA05916 and PPSA13934 for all 2^32 arguments, run on a Windows host. - [fgetln](../../../core/libs/prx/libc/src/StandardStreams.cpp) (libSceLibcInternal, implemented in libc) throws: in the console's `libSceLibcInternal.sprx` (SDK 4.5x build of `J03950001`, 0x5FE80) the export is a `hlt` instruction, which faults in user mode and ends the process - [Wide-character conversions](../../../core/libs/prx/libc/src/Multibyte.cpp) (libc) - The wide `wcsto*` functions parse the ASCII prefix of the guest string, as the C locale accepts only ASCII white space and digits. The multibyte conversions follow the C locale of the console's `libSceLibcInternal.sprx`: UTF-8 of at most three bytes, so `_Getmbcurmax` returns 3, overlong forms and encoded surrogates decode, and four-byte forms are invalid. `__mb_cur_max` stays 1 like the console's variable; `___mb_cur_max` is not in that module and also stays 1, unverified. Other locales are not supported. With a null destination, `wcstombs` returns the byte count; the console's count is wrong when the capacity argument equals the running count, because it reads past the terminator - [realpath](../../../core/libs/prx/libc/src/RealPath.cpp) (libc) follows FreeBSD 11's [`realpath.c`](https://github.com/freebsd/freebsd-src/blob/stable/11/lib/libc/stdlib/realpath.c) except for symlinks. The console's guest filesystem has none, so a host symlink under the guest root is followed for existence and type but not expanded, and `..` drops the previous component lexically, as `Resolve` does for every guest path. Through a host link `linkdir -> sub/deep`, `linkdir/../x` gives `x` beside the link, not beside its target. - [strtod, strtof, strtold](../../../core/libs/prx/libc/src/Strings.cpp) (libc) - `strtod` and `wcstod` (and `atof`, which calls `strtod` here) read a `NAN(...)` sequence as FreeBSD 11's gdtoa [`hexnan`](https://github.com/freebsd/freebsd-src/blob/release/11.0.0/contrib/gdtoa/hexnan.c) does: hexadecimal digits with an optional `0x`, blanks starting a new 32-bit word, the first 16 digits kept and the payload cut to 52 bits, a zero payload made 1, any other character giving the default NaN but consuming up to the closing parenthesis, and an unterminated sequence keeping its payload while consuming only `nan`. The payload goes under the quiet bit with the sign, as in gdtoa's [`strtod`](https://github.com/freebsd/freebsd-src/blob/release/11.0.0/contrib/gdtoa/strtod.c#L316-L328). `strtof`, `wcstof`, `strtold` and `wcstold` return the positive default quiet NaN (`0x7fc00000`; exponent `0x7fff`, significand `0xc000000000000000`) whatever the sign and the sequence, and consume a sequence of letters, digits and underscores up to its closing parenthesis. The console's `libSceLibcInternal.sprx`, called from a payload process on a PS5 (firmware 12.70), gives these results for the 64 NaN inputs tried through `strtod` and `strtof`, 50 of them also through `strtold` and `wcstod`, and 18 through `wcstof` and `wcstold`, and, through `strtod`, for `nan(1 2)` and for `nan( ` followed by NUL and `)`; that a title's own `libc.prx` does the same is assumed. In `hexnan`, a blank after a payload digit, while a 32-bit word of the payload is still free, starts a blank skip that has no NUL check, so when only blanks follow up to the terminating NUL the console's `strtod` reads past it: `nan(1 ` followed by NUL and `)` consumes the `)` after the NUL, so the result depends on the bytes after the string, and here that case throws. A blank right after `nan(` is skipped with a NUL check, and the console stopped at the NUL there - [remquol](../../../core/libs/prx/libc/src/MathExact.cpp) (libc) leaves `*quo` unchanged when the result is NaN; the `libc.prx` bundled with PPSA08427 writes a value that depends on earlier x87 state (`remquol(1, 0)` stored 0 on a first call and 4 on later ones) -- [_Stod](../../../core/libs/prx/libc/src/DinkumwareInternals.cpp) (libSceLibcInternal, implemented in libc) throws for hexadecimal text with a non-zero power-of-ten scale. The console's result there does not follow a simple rule: `0x3` with a scale of -1 gives 0.29999999999999999 +- [_Stod, _Stodx, _Stofx, _Stoldx, _Stof and _Stold](../../../core/libs/prx/libc/src/DinkumwareInternals.cpp) (libSceLibcInternal, implemented in libc) throw for hexadecimal text with a non-zero power-of-ten scale. The console's result there does not follow a simple rule: `0x3` with a scale of -1 gives 0.29999999999999999. A nonzero subnormal result sets neither `errno` nor the error argument, as measured in firmware 13.60's `libSceLibcInternal.sprx` run in a Linux host process, not on a console; there about a fifth of decimal inputs with a subnormal result come out one unit apart in the last place (`_Stodx("2.2250738585072014e-308", 0)` is the largest subnormal), which is not reproduced - [Long double math in MathExact.cpp](../../../core/libs/prx/libc/src/MathExact.cpp) (libc) was compared with the `libc.prx` bundled with PPSA08427 only for canonical 80-bit encodings; unnormals, pseudo-denormals and pseudo-infinities passed to arithmetic functions (not the classifiers) follow the host x87 instead of the console - [atanf](../../../core/libs/prx/libc/src/Math.cpp) (libc) is FreeBSD msun's [`s_atanf.c`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/msun/src/s_atanf.c), with `atanlo[3]` read at run time as msun's `volatile` does. Tiny arguments return `x` without raising `FE_INEXACT`. It gives the same bits as the `atanf` of the `libc.prx` bundled with PPSA07923, PPSA05916 and PPSA13934 for all 2^32 arguments, run on a Windows host, and none of those sets `errno`. Whether other titles' builds differ is unverified. - [rename, sceKernelRename](../../../core/libs/prx/libc/include/RenameDot.hpp) (libc, libkernel) fail a last path component of `.` or `..` with `EINVAL`, after FreeBSD 11.4's [`lookup`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/vfs_lookup.c), which refuses `.` and, through its `ISDOTDOT` and `ISLASTCN` flags, `..` as the last component for the `DELETE` and `RENAME` operations. `rmdir("dir/.")`, `unlink("dir/.")` and the same calls with `..` follow the same lookup rule in FreeBSD but are not changed here: the guest still resolves the dot component away and acts on `dir` or its parent. Both arguments are checked before any host access, so `rename` of a missing source onto `dir/.` answers `EINVAL`, where FreeBSD's [`kern_renameat`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/vfs_syscalls.c) looks the source up first and answers `ENOENT`. A backslash counts as a path separator here (the guest path resolver turns it into `/`); FreeBSD treats it as an ordinary name character. None of this was measured on a console. @@ -96,7 +106,9 @@ - [seed48 and lcong48](../../../core/libs/prx/libc/src/Random.cpp) (libSceLibcInternal, implemented in libc) follow the console's `libSceLibcInternal.sprx` (SDK 4.5x build of `J03950001`, 0x6F460 and 0x6F420): the multiplier and addend are shared by the whole `rand48` family, including `erand48`, `nrand48` and `jrand48` on a caller's seed; `lcong48` sets them with the seed, and `srand48` and `seed48` restore 0x5DEECE66D and 0xB. Unlike FreeBSD, `seed48` returns a static copy of the new seed rather than the previous one. A null argument, which the console dereferences, throws - [lgamma and signgam](../../../core/libs/prx/libc/src/Math.cpp) (libc) - `lgamma` returns the host's `lgamma` and stores the sign of Γ(x) in the exported `signgam` (NID `5gOOC0kzW0c`, starting at 0 as in FreeBSD 11's [`s_signgam.c`](https://github.com/freebsd/freebsd-src/blob/stable/11/lib/msun/src/s_signgam.c)) by the rules of [`e_lgamma_r.c`](https://github.com/freebsd/freebsd-src/blob/stable/11/lib/msun/src/e_lgamma_r.c): -1 for -0 and for a negative non-integer whose floor is odd, 1 otherwise, including poles, infinities and NaN. Taken from the FreeBSD source, not measured on the console. The NID is in the aerolib symbol list; no title is known to read `signgam`. PPSA99004 imports `lgamma` - [coshf](../../../core/libs/prx/libc/src/Math.cpp) (libc) follows FreeBSD msun's [`e_coshf.c`](https://github.com/freebsd/freebsd-src/blob/5aa554c7e5b6fd9042986df07f12ac5687e41392/lib/msun/src/e_coshf.c), using the `expm1f` and `expf` paths already inlined for `sinhf` below; the Windows UCRT differed from the `libc.prx` copies in PPSA07923 and PPSA13934 on 500,554 and 500,614 of 20 million inputs in the #9707 survey. The exact system `libSceLibcInternal` behavior and the FMA-contracted `libc.prx` results remain unverified +- [expm1](../../../core/libs/prx/libc/src/Math.cpp) (libc) does not compare floating-point exceptions, and its double inputs are checked by sampling, not exhaustively. It is FreeBSD msun's [`s_expm1.c`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/msun/src/s_expm1.c) with `huge` read at run time, and sets `ERANGE` only when a finite argument overflows to infinity, as the titles' `libc.prx` does. It gives the same bits and `errno` as the `expm1` bundled with PPSA07923 and PPSA05916 on 320 million random and 47,222 structured arguments on a Windows host; the FMA-contracted build bundled with PPSA13934 differs on 43,433 of the random ones. - [sinhf](../../../core/libs/prx/libc/src/Math.cpp) (libc) follows the `sinhf` of the `libc.prx` bundled with PPSA28319 (disassembly): FreeBSD msun's [`e_sinhf.c`](https://github.com/freebsd/freebsd-src/blob/5aa554c7e5b6fd9042986df07f12ac5687e41392/lib/msun/src/e_sinhf.c) from before `__ldexp_expf`, which squares `expf(|x| / 2)` between `logf(FLT_MAX)` and the overflow threshold, calling msun's [`expm1f`](https://github.com/freebsd/freebsd-src/blob/59bc2f095289a10b8cfa55cbb2b45c3bd6c2d4ae/lib/msun/src/s_expm1f.c) and [`expf`](https://github.com/freebsd/freebsd-src/blob/59bc2f095289a10b8cfa55cbb2b45c3bd6c2d4ae/lib/msun/src/e_expf.c), whose paths for the arguments it passes are inlined here. Past the threshold Sony's error helper sets `errno` to `ERANGE` before returning `x * 1e37`; msun itself sets no `errno`. That code, run on a Windows host, gives the same bits as this implementation for every finite argument up to the threshold, of both signs; it was not run on Linux. That the system `libSceLibcInternal`, which Ruffle (PPSA68091) and RetroArch (PPSA99169) import while bundling a zero-return `libc.prx`, has the same code is assumed +- [acoshf](../../../core/libs/prx/libc/src/Math.cpp) (libc) uses FreeBSD msun's [`e_acoshf.c`](https://github.com/freebsd/freebsd-src/blob/5aa554c7e5b6fd9042986df07f12ac5687e41392/lib/msun/src/e_acoshf.c) range handling; the Windows UCRT differed from the `libc.prx` copies in PPSA07923 and PPSA13934 on 216,017 and 216,024 of 20 million inputs in the #9707 survey. The invalid-domain path raises `FE_INVALID` without setting `errno` as the source does. The exact system `libSceLibcInternal` behavior and FMA-contracted results remain unverified - [acoshl, asinhl, atanhl, atan2l, cbrtl, erfl, erfcl, expm1l, hypotl, log1pl, log2l, lgammal, tgammal, j0f, j1f, jnf, y0f, y1f and ynf](../../../core/libs/prx/libc/src/Math.cpp) (libc) return the host C library's results (the float Bessel functions through its double ones), not those of the console's own code, so the last bits can differ (`y0f`, `y1f`, `ynf` follow the console at `+-0`). As in the console's `libSceLibcInternal.sprx` (SDK 4.5x build of `J03950001`, 0x56410), `lgammal` does not store the sign in `signgam`; `hypotl` returns infinity for an infinite argument even when the other is a NaN, as C11 F.10.4.3 requires. [hypot3, hypot3f and hypot3l](../../../core/libs/prx/libc/src/Math.cpp) follow that module (0x51D30, 0x520D0, 0x52420): the first NaN of x, y and z is returned unchanged, any infinity gives positive infinity, a zero argument gives the two-argument hypot of the other two, and otherwise the largest magnitude is combined with the hypot of the other two - [asinl](../../../core/libs/prx/libc/src/Math.cpp) (libc) follows the `asinl` of the `libc.prx` bundled with PPSA28319 (disassembly), FreeBSD msun's [`e_asinl.c`](https://github.com/freebsd/freebsd-src/blob/9f0432041b8fb69832f10c32da50a67f34feeb61/lib/msun/src/e_asinl.c) with the ld80 [`invtrig.c`](https://github.com/freebsd/freebsd-src/blob/9f0432041b8fb69832f10c32da50a67f34feeb61/lib/msun/ld80/invtrig.c) coefficients, which the binary holds as the same 80-bit constants. For |x| > 1, the infinities and NaN, Sony's error helper raises `FE_INVALID` and sets `errno` to `EDOM` before returning `(x - x) / (x - x)`, so a NaN argument also sets `EDOM`; msun itself sets no `errno`. 20 million random arguments in (-1, 1) give the same bits as that code run on a Windows host. The result needs the x87 precision control at 64 bits, as FreeBSD amd64 starts threads (control word `0x37f`), but guest threads do not set it: on Windows a thread started by `_beginthreadex` runs at 53 bits (`0x27f`), where `asinl(0.75)` returns mantissa `0xd91a98ae3406e000` instead of `0xd91a98ae3406e041`. The system `libSceLibcInternal` that RetroArch (PPSA99169) imports is assumed to have the same code - [atanhf](../../../core/libs/prx/libc/src/Math.cpp) (libc) follows the `atanhf` of the `libc.prx` bundled with PPSA28319 (disassembly): FreeBSD msun's [`e_atanhf.c`](https://github.com/freebsd/freebsd-src/blob/59bc2f095289a10b8cfa55cbb2b45c3bd6c2d4ae/lib/msun/src/e_atanhf.c) calling msun's [`log1pf`](https://github.com/freebsd/freebsd-src/blob/59bc2f095289a10b8cfa55cbb2b45c3bd6c2d4ae/lib/msun/src/s_log1pf.c), whose paths for the positive arguments it passes are inlined here; the exported `log1pf` is still the host's. For |x| > 1, the infinities and NaN, Sony's error helper raises `FE_INVALID` and sets `errno` to `EDOM` before returning `(x - x) / (x - x)`, and for |x| = 1 it raises `FE_DIVBYZERO` and sets `errno` to `ERANGE` before returning `x / 0`; msun itself sets no `errno`. That code, run on a Windows host, gives the same bits, `errno` and floating-point exceptions as this implementation for all 2^32 arguments; it was not run on Linux. That the system `libSceLibcInternal`, which Dusklight (PPSA99305) imports while bundling a zero-return `libc.prx`, has the same code is assumed @@ -123,9 +135,10 @@ - [wctomb_s, wcrtomb_s, mbstowcs_s and mbsrtowcs_s](../../../core/libs/prx/libc/src/BoundsChecking.cpp) (libc) follow the console's `libc.prx`, but convert in the single-byte C locale of `wctomb`, `wcrtomb` and `mbrtowc` (`MB_CUR_MAX` 1): a character above 0xFF is `EILSEQ` in `wctomb_s` and `wcrtomb_s`, and each byte is one wide character in `mbstowcs_s` and `mbsrtowcs_s`. The locale of a title's `libc.prx` was not measured. The `wcrtomb_s` path where a character does not fit `smax` and the `EILSEQ` paths of `mbstowcs_s` and `mbsrtowcs_s` are not reached - [wcsrtombs_s](../../../core/libs/prx/libc/src/Multibyte.cpp) (libc) follows the console's `libc.prx`, but a runtime-constraint violation only returns its error: the default handler there writes the message and a newline to `stderr` before returning, which is not done here. Conversion uses the C locale of `wcrtomb` (`MB_CUR_MAX` 1), so the path where a multibyte character does not fit the remaining space is not reached - [c16rtomb, c32rtomb, mbrtoc16 and mbrtoc32](../../../core/libs/prx/libc/src/Multibyte.cpp) (libSceLibcInternal, implemented in libc) - a null state, which the console dereferences, throws. Otherwise they follow the console's `libSceLibcInternal.sprx` (0x7D480 to 0x7D920), read from disassembly: UTF-8 whatever the locale, up to six bytes per sequence, with no surrogate or 0x10FFFF check when encoding. The state holds the value in its first eight bytes and the expected byte count (0x10 for a pending low surrogate, 1 for a stored high one) as 16 bits at 0xA. `mbrtoc16` builds a low surrogate from the value the state last held, `c16rtomb` returns -1 without `errno` for an unpaired high surrogate, and `EILSEQ` is 86. On a PS5 (13.60) the system library's default locale converts UTF-8 in `wcrtomb`, `wctomb`, `mbrtowc` and `mblen`; a title's bundled `libc.prx` does too until `setlocale` installs a table ([disassembly](https://github.com/boykopovar/AnyPS5/pull/8855#issuecomment-6335267758)) -- [The narrow printf family](../../../core/libs/prx/libc/src/Formatting.cpp) (libc) converts 16-bit guest wide arguments to UTF-8 on both platforms, independently of the host locale. It does not go through the guest's `wcrtomb`; the console's treatment of invalid UTF-16 and locale-specific encoding remains unverified. Linux retains host formatting for formats without wide arguments, `%m` or `%p`, so a NaN there still follows the host (glibc prints its sign, `-nan`, and honors the `+` and space flags, where FreeBSD prints `nan`); argument positions above 99 (FreeBSD's `NL_ARGMAX`) throw when a format requires the guest formatter +- [The narrow printf family](../../../core/libs/prx/libc/src/Formatting.cpp) (libc) follows a payload process on a PS5 (firmware 13.60): each 16-bit unit of a `%ls` or `%lc` argument is encoded alone, so a surrogate pair gives two 3-byte sequences; a NaN keeps its sign and the `+` and space flags, and `0` pads it with spaces; argument positions above 99 work (up to 400 measured; above 65535 throw here); an unknown conversion such as `%m` or `%k` prints `%` and its letter, padded to the width, without taking an argument. The guest's `wcrtomb` and locales are not used. On Linux the formats without those conversions go to glibc, which prints NaN the same way. The `_s` forms throw for an unknown conversion, `%m` included, which was not measured - [%p and %lp](../../../core/libs/prx/libc/include/WindowsFormatting.hpp) (libc) follow the console's `libSceLibcInternal.sprx` measured from a homebrew payload on firmware 12.70 ([#7549](https://github.com/boykopovar/AnyPS5/issues/7549)), not FreeBSD: bare lowercase hex with no `0x` prefix even under `#`, `0` for a null pointer, `+` and the space flag ignored, the width counting only the digits, `-` padding on the right, `0` padding with zeros when there is no precision, the precision as the minimum number of digits, nothing for a null pointer under `%.0p`, and `l` accepted. Only `snprintf` and `swprintf` of that module were measured, from a payload process rather than a title process, so a width beside `%.0p`, the `0` flag beside a precision (which C17 ignores) and `%.0p` of a non-null pointer follow this formatter's own rules and are not measured; `swprintf` was not run with `%lp`, so the wide family still refuses that conversion and returns -1. Linux routes `%p` and `%lp` to this formatter because glibc prints the FreeBSD form and `(nil)` for a null pointer, which means a format that combines `%p` with a conversion this formatter refuses, such as an argument position above 99, throws there instead of reaching glibc - [collate and collate _Getcat](../../../core/libs/prx/libc/src/LocaleSupport.cpp) (libc) build the facet for the C locale only and throw for any other locale name, the wide entry point following the narrow one it sits next to. The wide facet carries its own vtable, whose `compare`, `transform` and `hash` take the guest's 16-bit characters, so the length of a range is a count of characters: `compare` orders by 16-bit value (the C locale, as Dinkumware's `char_traits::compare` does), `transform` copies the characters into the result and records size and capacity in 16-bit elements (inline capacity 7, heap capacity `length | 7`, `(capacity + 1) * 2` bytes including the terminator), following Dinkumware's [VC12 `basic_string`](https://github.com/ganboing/crt.vc12/blob/f1d47126bf13d1c9dccaa6c53b72217fd0474968/src/xstring#L500-L531), and `hash` mixes one whole character per round, so wide text below 0x100 hashes like its narrow spelling and a character above 0xff hashes as one value instead of the two bytes it holds. The console's `libc.prx` exports both `_Getcat` (NIDs BSVJqITGCyI and 2wz4rthdiy8) with identical 0x19d-byte bodies, believed to build the same 0x20-byte facet; the console bodies themselves are unreadable (obfuscated dynsym, and the text of the relinked `libc.prx` is zeroed), so the wide entries follow the narrow algorithm and the C locale instead of the console's wide code, and no console run checks the two. The console's `libc.prx` stores pointers to its collation state tables at offsets 0x10 and 0x18 of the facet; ours leaves them null, as only the facet's own functions read them. `do_transform` throws for input with embedded null characters, which the console drops depending on the output buffer size. The narrow result capacity (`length | 15`) and the over-aligned storage of results of 4080 characters or more follow the console's `libc.prx` code and are not checked on hardware. No module of the PPSA59322, PPSA76677 or PPSA97358 dumps in this tree imports either `_Getcat`, so neither has been exercised by a title. +- [codecvt, numpunct, num_put and time_put _Getcat](../../../core/libs/prx/libc/src/LocaleSupport.cpp) (libc) build facets matching a bundled console `libc.prx` (dynsym decoded by NID, vtables disassembled): categories 2, 4, 4 and 5, sizes 0x10, 0x30, 0x10 and 0xb8. `codecvt` reproduces the console virtuals (`noconv` conversions, `length` the shorter range); `numpunct` copies its C-locale table (`""`, `.`, `,`, `false`, `true`). `num_put`/`time_put` formatting virtuals throw rather than fabricate output, and `time_put`'s `_Locinfo` table is unpopulated — the console's `do_put` bodies were not mirrored. The facets share the no-op `destroy`/`deleteObject`/`retain`/`release` base used for `collate`, so `numpunct`'s static C-locale strings are never freed. Unlike `collate`, these `_Getcat` entries ignore the locale argument and always build the C-locale facet; the console's behaviour for a non-C name is unverified. `std::clog` and `std::wclog` are 0x400-byte zeroed objects like the rest of the `NSTDSTR` stream set (the console's `libc.prx` sizes `std::clog` at 96 bytes, measured from its dynsym `st_size`), and the `numpunct::id` and `time_put::id` objects are 8-byte zeros like the other facet ids. - [std::__throw_bad_variant_access](../../../core/libs/prx/libc/src/exception/StandardExceptions.cpp) (libc) throws `std::bad_variant_access` with the `what()` string `bad_variant_access`, the one [LLVM libc++](https://github.com/llvm/llvm-project/blob/main/libcxx/src/variant.cpp) returns, since the export carries libc++'s `__throw_bad_variant_access` name; the console's string is unverified. Onimusha: Way of the Sword (PPSA27836 01.000.001) imports it from `libc`, and its bundled `libc.prx` does not export it. - [std::bad_optional_access and std::_Throw_bad_optional_access](../../../core/libs/prx/libc/src/exception/StandardExceptions.cpp) (libc) provide the exception type, constructor, destructor, and `what()` exports alongside `_Throw_bad_optional_access`, returning the `what()` string `bad_optional_access` matching Dinkumware and libc++ conventions; the console libc.prx string is unverified. PPSA26578 (issue #7624) and PPSA34015 import `_ZSt26_Throw_bad_optional_accessv` (`KwzEip7m6TM`) from `libc`, and fail to load on main with unresolved import (0xC0000139). - [The scanf family](../../../core/libs/prx/libc/src/Formatting.cpp) (libc), the `_s` forms included, decodes the input of the 16-bit wide conversions (`%ls`, `%lc`, `%l[`, `%S`, `%C`) on Linux as UTF-8, mirroring the narrow printf family, where FreeBSD uses the locale's `mbrtowc` (one byte per character in the C locale); the console's locale encoding is unverified. Input that is not UTF-8, a character outside the BMP, a scanset member outside ASCII, positional arguments next to a wide conversion and wide conversions in `fscanf` throw. Each character is stored as one 16-bit unit, widths and `_s` sizes count characters, and a `%lc` field cut short by the end of the input keeps what it read, as FreeBSD's `convert_wchar` does. Windows hands `%ls`, `%lc` and `%l[` to the host, whose `wchar_t` is 16 bits @@ -139,13 +152,18 @@ - [syslog](../../../core/libs/prx/libc/src/Syslog.cpp) (libc) writes each message to `stderr` as `[syslog:] `, with a newline added when missing, instead of sending it to the console's system log. Priority and facility handling follow FreeBSD's `vsyslog`: `%m` expands to the guest errno's message, unknown bits are reported (as priority 0x23, since FreeBSD passes `LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID` as the priority) and masked, a missing facility becomes `LOG_USER` or the facility set by `openlog`, and errno is preserved. `openlog` applies its identity and `LOG_PID`; a null identity retains the previous one, and a valid nonzero facility replaces the default. `closelog` clears the identity but retains the options and facility, as in FreeBSD 11; when no identity is set, FreeBSD falls back to the program name, which this backend does not emit. `LOG_PERROR` has no extra effect because stderr is the emulated log sink; `LOG_NDELAY`/`LOG_ODELAY`, `LOG_NOWAIT` and `LOG_CONS` have no daemon, child or console backend to control. `setlogmask` follows [FreeBSD 11](https://github.com/freebsd/freebsd-src/blob/8d2708de5d6112a492744d367cfdfb8fd219227a/lib/libc/gen/syslog.c): the process-wide mask defaults to 0xff, zero queries it, and nonzero values replace it; masked messages are skipped before formatting, including the unknown-priority diagnostic. The console's mask behaviour is unverified - [fwscanf, vfwscanf, wscanf, vwscanf and their `_s` forms](../../../core/libs/prx/libc/src/ScanningWide.cpp) (libc) run the `swscanf` scanner on characters read with `fgetwc`, so everything listed for `swscanf` in this file applies to them. As in the console's `libc.prx`, a field takes characters while they can still begin a valid item, so a failed field such as `0x` for `%x` stays consumed, and only the character that ended the scan goes back to the stream. That character is pushed back as its UTF-8 bytes with `ungetc`, because `ungetwc` takes single bytes only; the Windows C runtime guarantees one byte of push-back, so a non-ASCII lookahead can be lost there. A null stream or format returns `EOF`; the console checks them only in the `_s` forms. - [daemon](../../../core/libs/prx/libc/src/Process.cpp) (libc) throws: forking into the background is not supported. PPSA85300 imports it and fails to load without it. VLC 3.0 calls it only when its `daemon` option is set ([libvlc.c](https://github.com/videolan/vlc/blob/6de05adcbaf2e8b85fe86aad4169393098628119/src/libvlc.c#L190-L197)), and the title starts libVLC with `--ignore-config` and without `--daemon` ([player.cc](https://github.com/ZiZc3/VLC/blob/f3253020a64368153233ede29183014c0b6c8979/src/player.cc#L960-L981)) -- [_WStoul](../../../core/libs/prx/libc/src/Strings.cpp), [_Iswctype](../../../core/libs/prx/libc/src/RuneLocale.cpp), [swscanf_s and swscanf](../../../core/libs/prx/libc/src/ScanningWide.cpp) (libc) - `_WStoul` is `wcstoul` and throws for a base other than 0 or 2 to 36. `_Iswctype` takes the class numbers of Dinkumware's [xwcc.h](https://github.com/superna9999/ti-cgt6x/blob/cgt6x_7_3_4/include/xwcc.h) (1 `alnum` to 11 `xdigit`, 12 `blank`) and throws for any other value, including the FreeBSD masks `wctype` returns; the numbering is not confirmed on the console's `libc.prx`. `swscanf_s` follows C11 7.29.2.2 and K.3.9.1.5, but reads the size argument as `unsigned int` like `sscanf_s` where K.3.9.1.5 specifies `rsize_t`; the console's width is unverified. A runtime-constraint violation (null buffer, format or receiving pointer) only returns `EOF`, and a too small array gets a null first element as in `sscanf_s`. A literal at end of input returns the number of assignments, including zero, rather than `EOF` as FreeBSD does, as reported from the console scanner in #8479; `%%` at end of input still returns `EOF`. A literal mismatch is a matching failure, where FreeBSD's `vfwscanf` reports an input failure. An incomplete floating exponent such as `1e+` converts its mantissa, as reported from the console scanner in #8479; other incomplete exponent forms are not separately verified. A short `%c` field is currently a matching failure without storing its available prefix, while #9321 reports that the console stores a short `%3lc` field; its return count, partial-output extent and precedence are unverified. An integer field that is only a prefix, such as `0x`, does not convert; FreeBSD's `vfwscanf` pushes back the `x` and converts the `0`. Integers out of range are truncated from `strtoll`/`strtoull`. `%p` follows FreeBSD 11's [vfwscanf.c](https://github.com/freebsd/freebsd-src/blob/4ab2e064d7950be84256d671a7ae93f87cc6aa36/lib/libc/stdio/vfwscanf.c#L610-L615): a hexadecimal integer with an optional `0x`, converted as unsigned and stored as a pointer; a length modifier on it throws, where FreeBSD ignores it. It throws for `%S`, `%C`, unknown length modifiers, scanset ranges, `%n` with suppression or width, and narrow `%s`, `%c` or `%[` input that the C-locale `wcrtomb` cannot convert. `swscanf` runs the same scanner without the size arguments of `%s`, `%c` and `%[`, as FreeBSD's [swscanf.c](https://github.com/freebsd/freebsd-src/blob/d106002967eff712894ab8b5337bbbff106ce5e0/lib/libc/stdio/swscanf.c) passes its arguments to `vswscanf`, so everything above except the sizes applies to it; it also keeps the null buffer, format and receiving pointer checks returning `EOF`, where C leaves those undefined. Neither variant is confirmed on a PS5 title +- [_WStoul](../../../core/libs/prx/libc/src/Strings.cpp), [_Iswctype](../../../core/libs/prx/libc/src/RuneLocale.cpp), [swscanf_s and swscanf](../../../core/libs/prx/libc/src/ScanningWide.cpp) (libc) - `_WStoul` is `wcstoul` and throws for a base other than 0 or 2 to 36. `_Iswctype` takes the class numbers of Dinkumware's [xwcc.h](https://github.com/superna9999/ti-cgt6x/blob/cgt6x_7_3_4/include/xwcc.h) (1 `alnum` to 11 `xdigit`, 12 `blank`) and throws for any other value, including the FreeBSD masks `wctype` returns; the numbering is not confirmed on the console's `libc.prx`. `swscanf_s` follows C11 7.29.2.2 and K.3.9.1.5, but reads the size argument as `unsigned int` like `sscanf_s` where K.3.9.1.5 specifies `rsize_t`; the console's width is unverified. A runtime-constraint violation (null buffer, format or receiving pointer) only returns `EOF`, and a too small array gets a null first element as in `sscanf_s`. A literal at end of input returns the number of assignments, including zero, rather than `EOF` as FreeBSD does, as reported from the console scanner in #8479; `%%` at end of input still returns `EOF`. A literal mismatch is a matching failure, where FreeBSD's `vfwscanf` reports an input failure. An incomplete floating exponent such as `1e+` converts its mantissa, as reported from the console scanner in #8479; other incomplete exponent forms are not separately verified. A short `%c` field is currently a matching failure without storing its available prefix, while #9321 reports that the console stores a short `%3lc` field; its return count, partial-output extent and precedence are unverified. An integer field that is only a prefix, such as `0x`, does not convert; FreeBSD's `vfwscanf` pushes back the `x` and converts the `0`. Integers out of range are truncated from `strtoll`/`strtoull`. `%p` follows FreeBSD 11's [vfwscanf.c](https://github.com/freebsd/freebsd-src/blob/4ab2e064d7950be84256d671a7ae93f87cc6aa36/lib/libc/stdio/vfwscanf.c#L610-L615): a hexadecimal integer with an optional `0x`, converted as unsigned and stored as a pointer; a length modifier on it throws, where FreeBSD ignores it. `%S` and `%C`, with any length modifier or `*`, end the scan as a matching failure, as the console's Dinkumware scanner in PPSA21564's `libc.prx` treats them as invalid conversions; this was read from the module, not run on a console, and whether input ending before them is an input failure instead is unverified. It throws for unknown length modifiers, scanset ranges, `%n` with suppression or width, and narrow `%s`, `%c` or `%[` input that the C-locale `wcrtomb` cannot convert. `swscanf` runs the same scanner without the size arguments of `%s`, `%c` and `%[`, as FreeBSD's [swscanf.c](https://github.com/freebsd/freebsd-src/blob/d106002967eff712894ab8b5337bbbff106ce5e0/lib/libc/stdio/swscanf.c) passes its arguments to `vswscanf`, so everything above except the sizes applies to it; it also keeps the null buffer, format and receiving pointer checks returning `EOF`, where C leaves those undefined. Neither variant is confirmed on a PS5 title - [flock](../../../core/libs/prx/libc/src/FileLock.cpp) (libkernel) on Windows shares one lock state between duplicated descriptors, following FreeBSD 11.4's [flock(2)](https://github.com/freebsd/freebsd-src/blob/releng/11.4/lib/libc/sys/flock.2). Independent opens have separate states; closing a descriptor or reopening a stream drops only its reference. Lock transitions on one open file description are serialized. The lock is a host [LockFileEx](https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-lockfileex) lock on one byte at offset 2^63 - 2, which no file reaches, so it excludes other `flock` calls and leaves reads, writes and mapped views alone, like FreeBSD's advisory locks. Console behavior, signal interruption and concurrent closure of a descriptor while its lock call is pending are unverified. - [std::_Throw_Cpp_error](../../../core/libs/prx/libc/src/exception/StandardExceptions.cpp) (libc) follows Dinkumware `thread0.cpp` ([VC12](https://github.com/ganboing/crt.vc12/blob/f1d47126/src/thread0.cpp), [STL](https://github.com/microsoft/STL/blob/da52dc0f/stl/src/thread0.cpp)), without console verification. Codes outside 0..6 throw `std::out_of_range`, the deleting destructor throws, and STREAMS errnos absent from FreeBSD 11.4 [`errno.h`](https://github.com/freebsd/freebsd-src/blob/releng/11.4/sys/sys/errno.h) (`ENODATA`, `ENOSR`, `ENOSTR`, `ETIME`) are omitted. Codes 0..6 throw a 32-byte [VC12 `system_error`](https://github.com/ganboing/crt.vc12/blob/f1d47126/src/system_error) with FreeBSD errnos (16, 22, 3, 12, 1, 11, 35), a static `generic_category` (not `_Generic_object`), and unverified 40-byte [GuestLocale::String](../../../core/libs/prx/libc/include/GuestLocale.hpp) messages from [`syserror.cpp`](https://github.com/ganboing/crt.vc12/blob/f1d47126/src/syserror.cpp) - [Call tracing](../../../core/libs/prx/libc/src/specifics/x86_64/CallTrace.cpp) (`--trace-imports`, `APS5_TRACE_CALLS`) is missing on macOS and records only the entry of a call, not its arguments or result. One process-wide ring keeps the last 65536 calls, so a busy thread pushes the others out; the reporting thread's own calls are listed only while they are still in that ring. Calls are counted per traced target; an import bound to a target already traced under another name is counted under the first name. The thunk saves the integer argument registers and `xmm0` to `xmm7`; wider vector arguments survive only while the recording code avoids AVX, which is untested. Linux has no crash report, so it prints the trace only on `SIGABRT` (guest or host `abort()`), not on `SIGSEGV`; a caller in a module loaded after the last traced import prints without a module name, and a thread aborting while another writes the report waits for it at most 2 seconds - [_Dtest](../../../core/libs/prx/libc/src/Math.cpp) (libc) follows Dinkumware double classification matching _FDtest: finite values return -1, subnormals -2, zeros 0, infinities 1, and NaNs 2. - [app0.zar](../../../core/libs/prx/libc/src/AppArchive.cpp) (libc) - a [ZArchive](https://github.com/Exzap/ZArchive) beside `app0/` is a read-only layer under it: a path missing from `app0/` is looked up in the archive, case-insensitively. It is not a console format; titles ship their files in a PFS image instead. Resolving an archive-only path through any call other than the archive-aware libkernel ones throws, so `fopen`, `stat`, `access`, `opendir` and the rest do not read the archive yet, and `opendir` of a directory that exists in both places, `app0/` itself included, and `fdopen` of an archived descriptor throw instead of missing its entries or reading the null device. A damaged compressed block throws instead of returning `EIO` - [fopen_s](../../../core/libs/prx/libc/src/FileAndHeap.cpp) (libc) - returns the errno of the failed open, as C11 K.3.5.2.1 leaves the nonzero value open and Microsoft's CRT returns errno; the console's Dinkumware `fopen_s` is not verified +- [codecvt vtable](../../../core/libs/prx/libc/src/LocaleSupport.cpp) (libc, NID `H-TDszhsYuY`) is thirteen zeroed `uintptr_t` slots matching `st_size` 104 on title `libc.prx` from PPSA26344 and on `libSceLibcInternal.sprx` (offset-to-top, typeinfo, two destructors, `_Incref`/`_Decref`, and seven conversion virtuals). Facet calls through the object fault until those methods are wired. +- [num_put vtable](../../../core/libs/prx/libc/src/LocaleSupport.cpp) (libc, NID `59oywaaZbJk`) is fourteen zeroed `uintptr_t` slots matching `st_size` 112 on the title `libc.prx` from PPSA16833 (offset-to-top, typeinfo, two destructors, `_Incref`/`_Decref`, and eight `do_put` virtuals). Facet calls through the object fault until those methods are wired. - [ios_base standard streams](../../../core/libs/prx/libc/src/LocaleSupport.cpp) (libc) `_Addstd` queues the stream in the standard stream table of Dinkumware [ios.cpp](https://github.com/ganboing/crt.vc12/blob/master/src/ios.cpp) (`stdstr`, `stdopens`, `_Addstd`, `_Ios_base_dtor`): it takes the first of slots 1 to 7 that is free or already holds the stream, and slot 8 (`NSTDSTR`) when none is, so slot 8 is shared by every stream past the first seven and registration never fails. It stores the 1-based slot index in the guest `IosBase::standardStream` (Dinkumware's `_Stdstr`, where 0 means not a standard stream) and counts the stream objects registered per slot; registering the same stream again reuses its slot and raises its count. Measured on a PS5 (firmware 13.60) from a homebrew payload calling the system `libSceLibcInternal` export: in a process whose own standard streams already hold slots 1 to 7, twelve distinct streams and a repeated one all get 8, written as a 64-bit value at offset 0x8 with no other byte of the object touched; the assignment of slots 1 to 7 to fresh streams is not observable there and follows VC12. The counts are `int`, not VC12's `char`, and the registration and the destructor lock the table where VC12 locks only `_Addstd`; a slot index past the table throws in the destructor instead of indexing out of bounds. The destructor decrements its slot's count and suppresses the rest of the destruction while the count stays positive, as `_Ios_base_dtor` does, and the slots of destroyed streams keep their pointer and stay reserved for that stream, as `stdstr` does. The streams themselves are destroyed by the title's own code as it registers them through `__cxa_atexit`; when the last reference closes, the unsupported stream storage and callback cleanup is still the throw it was and the locale is released as for a non-standard stream. - [std::_Pad, _Thrd_join, _Thrd_detach, _Thrd_id](../../../core/libs/prx/libc/src/Threads.cpp) (libc) follow Dinkumware V6.00 `thr/xthread` and `thr/threadcall.cpp` ([crt.vc12 `f1d47126`](https://github.com/ganboing/crt.vc12/tree/f1d47126/src/thr)), without console verification. `_Thrd_id` throws because whether the console returns `pthread_t` or a kernel thread ID and at what width is unknown; `_Launch` omits `_Cnd_do_broadcast_at_thread_exit` and calls `pthread_create` registered by libkernel with default attributes. The 32-byte pad layout (`_Go()` vtable at 0x0, `_Cnd_t` at 0x8, `_Mtx_t` at 0x10, `bool _Started` at 0x18) is assumed from VC12, unverified. Failing primitives or thread start throw the guest `std::system_error`; null arguments throw. `_Thrd_join` returns `_Thrd_error` (4) on `pthread_join` failure and truncates the exit code to `int`; `_Thrd_detach` returns `_Thrd_error` (4) on `pthread_detach` failure and throws when none is registered +- [siginterrupt](../../../core/libs/prx/libc/src/Process.cpp) (libc) runs FreeBSD 11.4's `siginterrupt.c` sequence through the `sigaction` and the `_sigintr` set that libkernel registers at load: it reads the signal's disposition, sets or clears the signal's bit and `SA_RESTART`, and installs the disposition again. The set is written outside libkernel's registration lock, as FreeBSD's libc writes it without one; a call before libkernel has registered throws. +- [collate vtable](../../../core/libs/prx/libc/src/LocaleSupport.cpp) (libc, NID `ruZtIwbCFjk`) is nine zeroed `uintptr_t` slots matching `st_size` 72 on the title `libc.prx` from PPSA16833 (offset-to-top, typeinfo, two destructors, `_Incref`/`_Decref`, and three `do_*` virtuals). Facet calls through the object fault until those methods are wired; `_Getcat` still installs the host `g_collateVtable` for facets it builds. - [num_get::id, time_get::id and the num_get vtable](../../../core/libs/prx/libc/src/LocaleSupport.cpp) (libc) are data objects, as the public SDK's libSceLibcInternal table declares them and as the other facet ids and the `num_put` and `codecvt` vtables are kept: the ids start at zero and the vtable is 17 zeroed slots, the 136-byte size of `KfcTPbeaOqg` in the console's libSceLibcInternal, whose relocations fill the offset-to-top and typeinfo entries, the two destructors, `locale::facet::_Incref` and `_Decref`, and the eleven `do_get` of Dinkumware's crt.vc12 `xlocnum`, so a guest resolves the symbols and reads data, and a facet call through the vtable faults. libSceAmpr exported the three as throwing functions, which a data consumer reads as code; its qualified imports fall back to libc. +- [printf family conversions](../../../core/libs/prx/libc/include/WindowsFormatting.hpp) (libc) - Windows `FormatWindows` follows the title `libc.prx` `_Printf` measured from PPSA13934, PPSA07923, PPSA05916 and PPSA04158: `%q`, `%D`, `%O` and `%U` are unknown conversions; an unknown character is printed with its leading `%`, and width, `-` and discarded length modifiers behave as measured (`%5y` is ` %y`, `%hhz` is `%z`). `%%` takes a width. A trailing `%` reads past the format's NUL and is not reproduced; it throws. `#`, precision and a `0` without a width are ignored, as a payload process on a PS5 (firmware 13.60) printed `%#k` and `%.2k` as `%k`; `+`, space, `'` and `0` with a width on an unknown conversion are unverified and throw. On Linux a format with an unknown conversion also goes to this formatter diff --git a/docs/dev/debt/libkernel.md b/docs/dev/debt/libkernel.md index 67bf4903a9..ead756f295 100644 --- a/docs/dev/debt/libkernel.md +++ b/docs/dev/debt/libkernel.md @@ -2,8 +2,9 @@ ### Silent stubs +- [SO_TIMESTAMP, SO_BINTIME](../../../core/libs/prx/libkernel/Socket/src/SocketOptions.cpp) (libkernel) - set and read back as their bits, as a payload process on a PS5 (firmware 13.60) answered them, but `recvmsg` adds no `SCM_TIMESTAMP` or `SCM_BINTIME` control message, which FreeBSD 11.4's `ip_savecontrol` and `ip6_savecontrol` add to a received datagram while the option is set; what the console's `recvmsg` returns was not measured - [sceKernelGetSocPowerConsumption](../../../core/libs/prx/libkernel/System/src/ThermalSensors.cpp) (libkernel) - returns `EINVAL` and writes nothing, as `sceKernelGetSocSensorTemperature` does when the host has no sensor: the host's package power is not read (Linux RAPL `energy_uj` is readable by root only, and Windows has no general source). The prototype is not documented. PS5LM Plus (PPSA99581) passes a zeroed 128-byte buffer and, on success, reads its first 32-bit word as milliwatts, accepting 1000 to 350000; on failure it shows 0 W -- [scePthreadMutexattrSetprotocol](../../../core/libs/prx/libkernel/Pthread/src/Mutex.cpp) (libkernel) - accepts 0 to 2 as a PS5 (firmware 13.60) does and returns `EINVAL` otherwise, but the protocol is not stored or applied: priority inheritance and protection are not modelled. On the console a null or uninitialized mutex attribute returns `EINVAL` from the SCE attribute calls, `scePthreadMutexInit` takes an uninitialized attribute as the default one, and a destroyed mutex returns `EINVAL` from `scePthreadMutexDestroy`; a null mutex pointer crashed the payload and still throws +- [scePthreadMutexattrSetprotocol, scePthreadMutexattrGetprotocol, scePthreadMutexattrSetprioceiling, scePthreadMutexattrGetprioceiling](../../../core/libs/prx/libkernel/Pthread/Posix/Mutex.cpp) (libkernel) - `scePthreadMutexattrSetprotocol` accepts 0 to 2 as a PS5 (firmware 13.60) does and returns `EINVAL` otherwise, but the protocol is not applied: priority inheritance and protection are not modelled. On the console a null or uninitialized mutex attribute returns `EINVAL` from the SCE attribute calls, `scePthreadMutexInit` takes an uninitialized attribute as the default one, and a destroyed mutex returns `EINVAL` from `scePthreadMutexDestroy`; a null mutex pointer crashed the payload and still throws - [pthread_mutexattr_setprotocol, pthread_mutexattr_getprotocol, pthread_mutexattr_setprioceiling, pthread_mutexattr_getprioceiling](../../../core/libs/prx/libkernel/Pthread/Posix/Mutex.cpp) (libkernel) - the protocol and the priority ceiling are stored and reported as in [FreeBSD 11 libthr](https://github.com/freebsd/freebsd-src/blob/releng/11.0/lib/libthr/thread/thr_mutexattr.c#L192-L245) (the ceiling is set or read only under `PTHREAD_PRIO_PROTECT`, and setting a protocol resets it to the highest `SCHED_RR` priority). A null, uninitialized or destroyed attribute returns `EINVAL` before the other arguments are checked; this error behavior is unverified on the console. Mutexes do not apply the protocol or ceiling: a priority-inherit or priority-protect mutex locks like a normal one and does not change its owner's priority - [sceCoredumpSetUserDataType](../../../core/libs/prx/libkernel/System/src/Coredump.cpp) (libkernel, NID `Uxqkdta7wEg`) - `(int type)` follows the PS5 12.00 `libkernel.sprx` at 0x138B0: the sign-extended `type` goes to syscall 601 (command 29, third argument 0), and the raw result comes back with no SCE code mapping, so a kernel error is -1 with `errno` set. Which types the kernel accepts is not measured. No dump is produced here, so every type returns 0. Palworld (PPSA23126, #2598) and Hello Neighbor (PPSA35154, discussion #6991) import it; #4144 left it throwing because no signature was known - [sceKernelGetMainSocId and sceKernelGetCpumode](../../../core/libs/prx/libkernel/System/src/Process.cpp) (libkernel) both return 0, and [sceKernelGetCpuFrequency](../../../core/libs/prx/libkernel/Time/Time.cpp) returns the host TSC frequency measured for `sceKernelGetTscFrequency`, not the console CPU clock. The PS5's SoC id and CPU mode are unknown; shadPS4's PS4 values are [0x710f10/0x740f30 and 0, 2 or 5 from the param.sfo CPU-mode flags](https://github.com/shadps4-emu/shadPS4/blob/e0a466753a1a/src/core/libraries/kernel/process.cpp). PPSA99203 only logs them (`[console] main SoC id: result=%#x out=%#x`, `CPU: %llu Hz, cpumode %d`), passing a pointer that `sceKernelGetMainSocId` leaves untouched @@ -26,6 +27,7 @@ - [kern.sdk_version](../../../core/libs/prx/libkernel/System/src/SystemConfiguration.cpp) (libkernel) - `sysctlbyname` reports the system software version as the 4-byte value `0x01000000` (1.00), the value `sceKernelGetSystemSwVersion` reports; a write returns -1 with `EPERM`. The major version is byte 3 and the minor version byte 2, in hex digits that read as decimal (13.60 is `0x1360xxxx`), as [Capy Mobile](https://github.com/khalifa007/CapyMobile/blob/2758c016/src/ps5/platform.c#L64-L71) (PPSA92280), [remote_lua_loader](https://github.com/n0llptr/remote_lua_loader/blob/7c2840e475/savedata/misc.lua#L195-L209) and the [lapse payload](https://github.com/NetflixNHack/Netflix-N-Hack/blob/605eb93ba5/PS4/payloads/lapse.js#L436-L450) read it. Capy Mobile only displays it. The console's firmware version is not modeled and the low two bytes are unknown - [kern.ostype, kern.osrelease, kern.version, kern.hostname, hw.machine](../../../core/libs/prx/libkernel/System/src/SystemConfiguration.cpp) (libkernel) - `sysctl` (1.1, 1.2, 1.4, 1.10, 6.1), `sysctlbyname` and `sysctlnametomib` serve the names `uname` reads as read-only strings: "FreeBSD", "11.4-RELEASE" and "FreeBSD 11.4-RELEASE\n", the values FreeBSD 11.4-RELEASE builds without source control information ([newvers.sh](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/conf/newvers.sh#L45-L52), [L293-L294](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/conf/newvers.sh#L293-L294)), the host name `gethostname` reports ("AnyPS5") and "amd64"; a short buffer returns `ENOMEM` and a write `EPERM`. The console's strings are not measured - [kern.osreldate](../../../core/libs/prx/libkernel/System/src/SystemConfiguration.cpp) (libkernel) - reports MIB `{CTL_KERN, KERN_OSRELDATE}` as `1104001`, FreeBSD 11.4's `__FreeBSD_version` in `sys/sys/param.h`; this is consistent with `kern.osrelease` and `kern.version` above, but the value reported by a PS5 title has not been measured. +- [kern.boottime](../../../core/libs/prx/libkernel/System/src/SystemConfiguration.cpp) (libkernel) - `sysctl` {`CTL_KERN`, `KERN_BOOTTIME`} and `sysctlbyname` report a read-only 16-byte `struct timeval`, as FreeBSD 11.4's [`sysctl_kern_boottime`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/kern_tc.c#L144-L163) does: the guest's `CLOCK_REALTIME` minus its `CLOCK_UPTIME`, taken at the first read and kept, so a later step of the host clock does not move it as FreeBSD's `tc_setclock` would. The value a PS5 title gets has not been measured. - [sceKernelGetProsperoSystemSwVersion](../../../core/libs/prx/libkernel/System/src/SoftwareVersion.cpp) (libkernel) reports system software version `0x01000000` ("01.000.000"); the console's firmware version is not modeled. The 0x28-byte block (a 64-bit size at 0, the version string at 8 and the packed version at 0x24) follows [SharpProspero](https://github.com/SvenGDK/SharpProspero/blob/main/src/SharpProspero/Interop/Kernel/KernelSystem.cs), which Prospero Multi Tools (PPSA99110) and Prospero Explorer (PPSA99109) are built with and which sets the size to 40, and the same fields in [etaHEN](https://github.com/etaHEN/etaHEN/blob/main/Source%20Code/daemon/include/ipc.hpp) and [shsrv](https://github.com/ps5-payload-dev/shsrv/blob/master/sh.c). A size other than 0x28 returns `SCE_KERNEL_ERROR_EINVAL` without writing; whether the console checks the size is unverified, since etaHEN and shsrv declare a 0x30-byte block and do not set it - [sceKernelGetAllowedSdkVersionOnSystem](../../../core/libs/prx/libkernel/System/src/SoftwareVersion.cpp) (libkernel) reports the system software version with the 12 patch bits set, `0x01000fff`, as [shadPS4](https://github.com/shadps4-emu/shadPS4/blob/98fd0689/src/core/libraries/kernel/kernel.cpp) does on the PS4 and as [SharpProspero](https://github.com/SvenGDK/SharpProspero/blob/main/src/SharpProspero/Interop/Kernel/KernelSystem.cs) describes it, which Prospero Explorer (PPSA99109) reads it through; a null pointer returns `SCE_KERNEL_ERROR_EINVAL`. The console's value and its errors are unverified - [sceKernelConvertUtcToLocaltime, sceKernelConvertLocaltimeToUtc](../../../core/libs/prx/libkernel/Time/Time.cpp) (libkernel) convert with the host time zone at the given instant, as libc `localtime` does, instead of the console's time zone setting. `west_sec` holds the standard-time offset in seconds east of UTC, despite its name, and `dst_sec` the daylight saving seconds in effect. This follows PPSA99203 ([PS5SX2](https://github.com/Swordpdf/PS5SX2/blob/b4cf55e87c34e8a2a1cd9302df4373e5a94d28f4/ps5/frontend/fe_ps5.cpp#L54-L63)), whose author saw a console write 7200 and 3600 into the 16-byte block; which zone that console was set to is not recorded, so whether `west_sec` includes daylight saving is not settled. [shadPS4](https://github.com/shadps4-emu/shadPS4/blob/23c41333/src/core/libraries/kernel/time.cpp) also stores seconds east. PPSA99203 declares the last argument of `sceKernelConvertUtcToLocaltime` as `uint64_t*`; 32 bits are written. On Windows the standard offset and the daylight saving amount are the C runtime's current `_get_timezone` and `_get_dstbias` for every instant, as libc `localtime_s` uses them, so instants under a zone's earlier rules get today's offsets; a `TZ` variable does not set `_dstbias`, which keeps the value an earlier `_tzset` read from the system (0 when the system zone has no daylight saving), and summer time then adds nothing. In a repeated or skipped local hour, `sceKernelConvertLocaltimeToUtc` takes the offset in effect one standard offset earlier. Its last argument receives the zone's daylight saving amount whether or not it is in effect, found by sampling the host zone over the year around the result, while `dst_sec` is the amount in effect: the libc `mktime` that PPSA99109 bundles (text 0x70ebb-0x70ee5) subtracts that amount when `tm_isdst` is set outside summer time. That `mktime` passes `tm_isdst` as the second argument, which is ignored, so it does not choose the offset in a repeated hour. `sceKernelGettimezone` and the `gettimeofday` time zone still report no offset and no daylight saving. A host time the C runtime cannot convert throws @@ -36,36 +38,38 @@ - [APR reads past the end of a file](../../../core/libs/prx/libkernel/Apr/src/Apr.cpp) (libkernel) - a read that reaches the current end of the file on disk copies the bytes the file has and fills the rest of the destination with zeros; a read that comes back short before that end still throws. PPSA18605 resolves its settings file once, rewrites it one byte shorter and reads it again with the size that resolve returned. The console's behaviour for a read past the end of a file is unverified: it may zero-fill, leave the destination untouched or fail the command - [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 256 GiB range (PPSA02015 places its heaps 64 GiB above the start of the range, so 32 GiB is too small, and reached 84 GiB above it in its first 159 s; KytyPS5 reports 512 GiB, but every 64 GiB-aligned 544 GiB window in the guest arena covers 448 to 608 GiB, and on Windows one host thread stack there is enough to block it) and a 32 GiB multimap range reserved on first use at the top of the guest arena with the start on a 64 GiB boundary (PPSA02015 tells its pool pointers from its heap pointers by address bits 28 to 35, which needs such a start for its layout), above the addresses that maps without `MAP_FIXED` are given lowest first and that titles derive their next hints from; the console's addresses are not known (ampr_emu takes them from `sceKernelMapperGetParam`), and a reservation that cannot be made there throws. 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. A map returns its target to the reservation before releasing the old pages, so pool exhaustion leaves that target reserved; mapping failure also restores the reservation before returning newly taken pages to the pool. The console's map failure and rollback behavior is unverified. `sceAmprAmmGetVirtualAddressRanges` throws for a null output +- [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 256 GiB range (PPSA02015 places its heaps 64 GiB above the start of the range, so 32 GiB is too small, and reached 84 GiB above it in its first 159 s; KytyPS5 reports 512 GiB, but every 64 GiB-aligned 544 GiB window in the guest arena covers 448 to 608 GiB, and on Windows one host thread stack there is enough to block it) and a 32 GiB multimap range reserved on first use at the top of the guest arena with the start on a 64 GiB boundary (PPSA02015 tells its pool pointers from its heap pointers by address bits 28 to 35, which needs such a start for its layout), above the addresses that maps without `MAP_FIXED` are given lowest first and that titles derive their next hints from; the console's addresses are not known (ampr_emu takes them from `sceKernelMapperGetParam`), and a reservation that cannot be made there throws. 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. A buffer may run after submission returns, the submit result is always 0 and a failing command throws; the AMM measures return errors sign-extended, as ampr_emu does. A map returns its target to the reservation before releasing the old pages, so pool exhaustion leaves that target reserved; mapping failure also restores the reservation before returning newly taken pages to the pool. The console's map failure and rollback behavior is unverified. `sceAmprAmmGetVirtualAddressRanges` throws for a null output - [AMM remap, multimap and protection changes](../../../core/libs/prx/libkernel/Apr/src/Apr.cpp) (libSceAmpr) - signatures and the protection and mask checks taken from ampr_emu; the effects are inferred from the names, not confirmed on a title. A remap moves every page of a fully mapped range to the new address with the given protection and leaves the old range reserved; a multimap maps the same pages at a second address, anywhere in the AMM ranges, and a pool page goes back to the pool once its last mapping is unmapped; a multimap onto its own source range throws. A protection change sets the masked bits on every page of a fully mapped range; the memory type is recorded and ignored. A command on a range that is not fully mapped throws - [AMM PRT ranges](../../../core/libs/prx/libkernel/Apr/src/Apr.cpp) (libSceAmpr) - signatures, checks and their order taken from ampr_emu, where `MapAsPrt` is a map with the PRT flag and protection 0 and `AllocatePaForPrt` a memory type and protection change with mask 1019, both checking the buffer memory after its size; the effects are inferred from the names, not confirmed on a title. Unbacked PRT pages are zero-filled pages with GPU read access, so CPU reads see zeros instead of faulting and GPU writes fault instead of being dropped. `AllocatePaForPrt` backs each unbacked page with a pool page and sets the type and protection of backed ones; `RemapIntoPrt` moves a fully mapped range into a PRT range, its opcode argument (1011 when 0, as in ampr_emu) is recorded and ignored; `UnmapToPrt` returns the pages to the pool and zero-fills them again. A plain map, unmap or remap over a PRT range ends it; `AllocatePaForPrt`, `RemapIntoPrt` or `UnmapToPrt` outside a PRT range throws - [AMM kernel write commands](../../../core/libs/prx/libkernel/Apr/src/Apr.cpp) (libkernel) - signatures of `sceKernelWriteModifyMtypeProtectCommand` and `sceKernelWriteRemapIntoPrtCommand` taken from [ampr_emu's libkernel hook targets](https://github.com/drakmor/ampr_emu/blob/cfa85df379f6eeeb165d7badf9b648e266fe77b7/include/ampr_libkernel_hook_targets.h), which forward to the real kernel; the measure mode (`dst` null reports the size, `outSize` null skips the report) follows ampr_emu's measure-then-record calls, not confirmed on a title. The range, protection and mask checks mirror the `libSceAmpr` siblings, and the recorded bytes match them exactly so the submitted buffer executes on the existing paths - [sceCoredumpWriteUserString](../../../core/libs/prx/libkernel/System/src/Coredump.cpp) (libkernel, NID `32KQRUK13kI`) - `(uint32_t, const char* text, int)` follows the console's `libkernel.sprx` (SDK 4.5x build of `J03950001`) at 0x13050: a null `text` returns `0x81180000`; otherwise the three arguments go to syscall 601 (command 10), whose `EPERM` becomes `SCE_COREDUMP_ERROR_NOT_IN_COREDUMP_HANDLER` (0x81180003), `EINVAL` `0x81180000`, and a success returns the value the kernel wrote back. Measured on a PS5 (firmware 13.60) from a homebrew payload with no core dump handler: the call succeeds with 0, the kernel rejects a first argument above 15 or a third outside 0 to 256 with `0x81180000`, and a 2047-character text is accepted. Those checks are reproduced and anything else returns 0; nothing is recorded, as there is no dump, and what the two numbers mean is unknown. PPSA16064 imports it - [sceCoredumpConfigDumpMode](../../../core/libs/prx/libkernel/System/src/Coredump.cpp) (libkernel, NID `gzLt9Qrauk0`) - `(int mode)` follows the same `libkernel.sprx` at 0x134D0: a mode other than 1 returns `0x81180000`; mode 1 issues syscall 601 (command 16, arguments 0 and 1) and returns 0 whether or not it fails, since only a raw return of 22 would map to `0x81180000`. No dump is produced here, so mode 1 returns 0 without the syscall. PPSA16833 imports it - [sceCoredumpGetStopInfoGpu_Agc](../../../core/libs/prx/libkernel/System/src/Coredump.cpp) (libkernel, NID `Jrs7UUkGOFo`) - `(void* info, size_t size)` follows the same `libkernel.sprx` at 0x137E0: a null `info` returns `0x81180000`; syscall 601 (command 21) failing with `EPERM` returns `NOT_IN_COREDUMP_HANDLER`; otherwise two `libSceAgcDriver` imports (NIDs `+NIAY3DWpOw` and `LgctzJSGy7s`, names unknown) decide between `0x81180005` and, for a `size` of 8, storing the second one's 64-bit result and returning 0, with any other size returning `0x81180000`. The handler is never invoked here, so a non-null `info` returns `NOT_IN_COREDUMP_HANDLER` without writing, assuming `EPERM` outside the handler as above; the AGC branch is not reached -- [sceKernelInternalMemoryGetModuleSegmentInfo](../../../core/libs/prx/libkernel/Module/src/ModuleInfo.cpp) (libkernel) - the `(InternalSegmentInfo* result)` signature, the 16-byte `{address, size}` result and the choice of the main program's first segment follow [fpPS4](https://github.com/red-prig/fpPS4/blob/04cefd43e6fddd1ab033e7980cd356d14c964905/kernel/ps4_libkernel.pas#L343-L355) only, which fills it through its `sceKernelGetLibkernelTextLocation` from the first segment of `eboot.bin`; whether the console reports that segment, the text of libkernel itself or the calling module's is unknown. The segment is the first one `sceKernelGetModuleInfo` reports for the main program, and 0 is returned. A null result throws. 44 audited titles that bundle their own `libc.prx` import it; no call has been observed +- [sceKernelInternalMemoryGetModuleSegmentInfo](../../../core/libs/prx/libkernel/Module/src/ModuleInfo.cpp) (libkernel) - returns the main program's first segment, as [fpPS4](https://github.com/red-prig/fpPS4/blob/04cefd43e6fddd1ab033e7980cd356d14c964905/kernel/ps4_libkernel.pas#L343-L355) does through its `sceKernelGetLibkernelTextLocation`. In a payload process on a PS5 (firmware 13.60) both returned 0 with address and size 0, and a null result faulted (here it throws); what a title process gets is not known. 44 audited titles that bundle their own `libc.prx` import it; no call has been observed - [sceKernelGetModuleInfoForUnwind](../../../core/libs/prx/libkernel/Module/src/Module.cpp) (libkernel) - on Linux the tables come from `sceKernelGetModuleInfoFromAddr`, whose `eh_frame_size` includes the zero terminator, as measured on firmware 13.60 (reported in [#2419](https://github.com/boykopovar/AnyPS5/issues/2419), not reproduced here); on Windows it is assumed to stop before the terminator. LLVM libunwind's `CFI_Parser::findFDE` stops at the terminator or the length, whichever comes first, so it finds the same FDEs either way. Only the main executable and `.guest.prx` images report tables; other host images report none, as on Windows - [sceKernelGetModuleInfoForUnwind](../../../core/libs/prx/libkernel/Module/src/Module.cpp) (libkernel) - on Windows the `.ehmeta` tables and the image file name are cached per allocation base and dropped after a loader free (`dlclose`, `sceKernelStopUnloadModule`, the thread atexit release) when the module is no longer loaded. A table changed in place, or an image freed by anything other than the loader, is not noticed. An address inside an image of the Windows loader's module list whose tables are cached is resolved with `RtlPcToFileHeader` and no `VirtualQuery`; other addresses, and images without tables, still use `VirtualQuery`. The `.eh_frame` walk that finds its end is done once per image instead of once per call -- [sceKernelGetModuleInfoFromAddr](../../../core/libs/prx/libkernel/Module/src/ModuleInfo.cpp) (libkernel) - the 0x1A8-byte info layout follows the [shadPS4](https://github.com/shadps4-emu/shadPS4) reimplementation; a title's `libc.prx` reads only `id` (PPSA14632). The meaning of `flags` is unknown; only 2 is accepted. The input `st_size` is ignored, because PPSA21703's `libc.prx` passes an uninitialized 0x1A8-byte buffer; the full size is written back. The id is the `sceKernelLoadStartModule` handle of an image opened through the loader and otherwise a stable id from the same range. The name is the host file name without `.guest.prx`. An image with more than 4 load segments (the converted executable has 7) reports its first 4. `tls_offset` is 0 and `ref_count` is 1, as the host loader exposes neither. On Windows the module is the PE image holding the address: a relinked image reports its `.elf` sections as segments and the eh_frame_hdr recorded in `.ehmeta`, any other image its first 4 sections with memory access and the `.ehfram` section without an eh_frame_hdr; the TLS fields and `init_proc_addr`/`fini_proc_addr` are 0 +- [sceKernelGetModuleInfoFromAddr](../../../core/libs/prx/libkernel/Module/src/ModuleInfo.cpp) (libkernel) - as in a payload process on a PS5 (firmware 13.60), flags 1 finds any module and flags 2 refuses the system ones (`ESRCH`), here the host libraries; other flags clear the 0x1A8 bytes and return `EINVAL`, any failure clears them, `st_size` is left 0, and a null info returns `EFAULT` (with other flags the console faulted; here it throws). Flags 2 finding a title module was not measured. The layout follows [shadPS4](https://github.com/shadps4-emu/shadPS4). The id is the loader handle or a stable id from that range, and the name is the host file name without `.guest.prx`, where the console keeps `.sprx`. Only the first 4 load segments are reported. `tls_offset` is 0 and `ref_count` 1 (the console gave 5 for system modules). On Windows a relinked image reports its `.elf` sections and the `.ehmeta` eh_frame_hdr; the TLS fields and init/fini are 0 - [sceKernelGetModuleList](../../../core/libs/prx/libkernel/Module/src/ModuleInfo.cpp) (libkernel) - lists the main program and every loaded `.prx` image in load order; host runtime libraries are left out. The handles are the ids `sceKernelGetModuleInfoFromAddr` reports, not the console's. The `(int32_t* handles, size_t count, size_t* actual)` signature and returning `ENOMEM` without writing `actual` after filling the first `count` handles when more images are loaded follow [shadPS4](https://github.com/shadps4-emu/shadPS4/blob/0fe263a4760dfbfa973366890061749b4af0de97/src/core/libraries/kernel/process.cpp#L244-L264); whether the console writes any handles in that case is unknown - [sceKernelGetModuleInfo](../../../core/libs/prx/libkernel/Module/src/ModuleInfo.cpp) (libkernel) - the 0x160-byte layout (`st_size`, `name[256]`, 4 segments, `segment_count`, `fingerprint[20]`) follows [fpPS4](https://github.com/red-prig/fpPS4/blob/04cefd43e6fddd1ab033e7980cd356d14c964905/sys/sys_types.pas#L319-L337) and [shadPS4](https://github.com/shadps4-emu/shadPS4/blob/0fe263a4760dfbfa973366890061749b4af0de97/src/core/module.h#L15-L31). Only that `st_size` is accepted, as in shadPS4; other sizes return `EINVAL`, a null pointer `EFAULT` and a handle `sceKernelGetModuleList` does not list `ESRCH`. Name and segments are the ones `sceKernelGetModuleInfoFromAddr` reports, on Windows from the same PE sections, so `prot` uses the read/write/execute bits 1/2/4, while fpPS4 and shadPS4 store the ELF `p_flags`. The fingerprint is left zero, as the host image does not carry it - [sceKernelGetModuleList2 and sceKernelGetModuleInfo2](../../../core/libs/prx/libkernel/Module/src/ModuleInfo.cpp) (libkernel) - the title-module variants follow [shadPS4](https://github.com/shadps4-emu/shadPS4/blob/main/src/core/libraries/kernel/process.cpp): `sceKernelGetModuleList2` lists what `sceKernelGetModuleList` lists without system libraries, and `sceKernelGetModuleInfo2` answers `EPERM` for a system library after the `EFAULT`, `EINVAL` and `ESRCH` checks of `sceKernelGetModuleInfo`. A system library here is a loaded `.prx` image that is not a relinked title module (`.guest.prx`); the main program and the title's own modules are listed. Not verified on the console. PPSA34063 imports both and fails to load without them - [sceKernelGetLibkernelTextLocation](../../../core/libs/prx/libkernel/Module/src/ModuleInfo.cpp) (libkernel) - the `(void** address, std::size_t* size)` signature and the choice of the main program's first segment follow [fpPS4](https://github.com/red-prig/fpPS4/blob/04cefd43e6fddd1ab033e7980cd356d14c964905/kernel/ps4_libkernel.pas#L338-L348); whether the console reports that segment, the text of libkernel itself or the calling module's is unverified. The segment is the first one `sceKernelGetModuleInfo` reports for the main program, and 0 is returned. A null argument throws. - [sceKernelReleaseFlexibleMemory](../../../core/libs/prx/libkernel/DirectMemory/Export.cpp) (libkernel) - on a PS5 (firmware 13.60) the released range stays mapped, with its protection and contents (64 MiB checked after 0.5 s), and a fixed no-overwrite map over it fails with `ENOMEM`, as here. What it does to the flexible budget was not measured, because a payload process has none (`sceKernelAvailableFlexibleMemorySize` did not change on map or release); here the released bytes return to the budget while the pages stay mapped, and a later map over them charges them again. The host pages behind a released range stay committed until `sceKernelMunmap`, as `madvise` keeps them for every advice; freeing them would lose the contents the console still returns. - [sceKernelAvailableFlexibleMemorySize](../../../core/libs/prx/libkernel/DirectMemory/Export.cpp) (libkernel) - the segments of the title's executable and of its `.guest.prx` modules count against the configured flexible-memory budget (448 MiB by default) while loaded, for this size and for flexible maps. A title may declare a larger budget through `kernel.flexibleMemorySize` in `param.json`; this does not reduce the 13.5 GiB direct-memory size. Rejection of zero or a value not aligned to 16 KiB has not been measured on a console. This segment-accounting model follows [shadPS4](https://github.com/shadps4-emu/shadPS4/blob/3a9f57265d28975658f1ca941daa5c879c7a74bd/src/core/module.cpp#L165), which maps every segment of a module outside its system module directory as flexible memory on the PS4. [Counted](../../../core/libs/prx/libkernel/Module/src/ModuleInfo.cpp) are the segments the title's ELF gives permissions, each rounded to 16 KiB pages; not counted are segments without permissions, the relinker's own blocks (on Linux both are the read/write/execute `PT_LOAD`s, plus the header block at file offset 0; on Windows everything outside the `.elf` sections) and the host system libraries. The Linux image carries no marker for the relinker's blocks, so they are told apart by those flags and that offset only: a title segment that is writable and executable, or starts at file offset 0, is not counted. A module whose segments exceed what is left still loads, and the available size then reads 0; shadPS4 fails the segment map instead ([memory.cpp](https://github.com/shadps4-emu/shadPS4/blob/3a9f57265d28975658f1ca941daa5c879c7a74bd/src/core/memory.cpp#L519-L524) returns `EINVAL`, [module.cpp](https://github.com/shadps4-emu/shadPS4/blob/3a9f57265d28975658f1ca941daa5c879c7a74bd/src/core/module.cpp#L168-L169) asserts). Which segments the PS5 charges, its rounding, whether system modules use the title's budget and when an unload releases the charge are not measured. PPSA15508 subtracts the reported size and a 10 MiB module allowance from its own 448 MiB budget at start-up and stops when the result exceeds its 146 MiB code and data limit ([#2883](https://github.com/boykopovar/AnyPS5/issues/2883)) -- [sceKernelAvailableDirectMemorySize](../../../core/libs/prx/libkernel/DirectMemory/Export.cpp) (libkernel) - how the console handles invalid arguments is not measured. A negative `search_start`, a `search_end` at or below `search_start` and an alignment that overflows when rounded up to 16 KiB return `EAGAIN` (`0x80020023`); bounds that are not multiples of 16 KiB are rounded inward, the alignment is raised to a multiple of 16 KiB and need not be a power of two, and a `search_end` past the pool is cut to it. fpPS4's HLE wrapper on its trunk branch refuses all of these with `EINVAL` ([ps4_map_mm.pas](https://github.com/red-prig/fpPS4/blob/04cefd43e6fddd1ab033e7980cd356d14c964905/kernel/ps4_map_mm.pas#L558-L570)) +- [sceKernelAvailableDirectMemorySize](../../../core/libs/prx/libkernel/DirectMemory/Export.cpp) (libkernel) - its arguments follow a payload process on a PS5 (firmware 13.60) ([probes](https://gist.github.com/orezraey/42a06a7ea0d9fb57ac9c06eaf9e6756c)). One answer is not modelled: in a process that had not yet called `sceKernelGetDirectMemorySize` or otherwise touched direct memory, the console returned `EBADF` (`0x80020009`); here the call always searches the pool. Titles open the direct-memory device during start-up, so they are not expected to see it +- [sceKernelReleaseDirectMemory](../../../core/libs/prx/libkernel/DirectMemory/MemoryPool.cpp) (libkernel) - releases the allocated pages of the range and skips pages that are not allocated, returning 0, while `sceKernelCheckedReleaseDirectMemory` returns `ENOENT` for them, as [shadPS4](https://github.com/shadps4-emu/shadPS4/blob/c206f35a546230d9c9808b823c0f36f5850cd4af/src/core/memory.cpp#L295-L313) does; not measured on a console. Absolum (PPSA28311) releases such a range when it disposes of its video logos - [Memory protection bits 0x100 and 0x200](../../../core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp) (libkernel) - taken as audio coprocessor (ACP) read and write, as in the [KytyPS5](https://github.com/KytyPS5/KytyPS5) and [etaHEN](https://github.com/etaHEN/etaHEN) headers; the coprocessor work runs on the host CPU, so they grant host read and read/write access like the GPU bits - [Protecting a host thread's stack](../../../core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp) (libkernel) - on Windows `sceKernelIsStack` reports a host thread's whole stack reservation, which Windows commits on demand from the top down behind a guard page. A protect over that range changes only the pages above the guard; the guard page and the reserved pages below it are left as they are, so the stack still grows and an overflow still raises a stack overflow. A protect that starts at the bottom of the reservation and stops short of the guard page, as HotSpot makes its stack guard zones, is committed and protected. Windows moves the guard page down by committing the pages below it as read/write guard pages, whether they were reserved or committed, so those pages come in read/write as the stack grows to within one guard region of them (three pages on GitHub's runners). A protection other than read/write on them, `PROT_NONE` included, does not hold once the stack is that close, where on the console every page of the reported range takes the protection. Holding it would mean removing the thread's guard page and keeping the stack limit in its TEB by hand. On Linux the reported range of the main thread is mostly unmapped; the unmapped pages from the bottom of a protect up to the top of the stack are mapped read/write in place, then the protect is applied, so the stack never has to grow down to the protected pages, which Linux refuses within its stack guard gap (1 MiB by default) above a readable mapping. The pages below a protect stay unmapped and out of the stack's reach, where on the console the stack goes on below them - [Foreign guest module image protection on Windows](../../../core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp) (libkernel) - `sceKernelMprotect` on Windows allows protection mutations on mapped PE images containing a `.elf0` section (as relinked guest modules produce, such as NVIDIA Real-time Denoisers `libNRD.prx` bundled in Dying Light 2: Stay Human PPSA02262), registering their sections into `GuestAllocations`. Host libraries and system DLLs (such as `libkernel.prx` and `kernel32.dll`) without a `.elf0` section continue to be rejected - [_umtx_op](../../../core/libs/prx/libkernel/SyncOnAddress/src/SyncOnAddress.cpp) (libkernel) - the mutex, condition variable, read-write lock, legacy lock and `SEM2` operations throw, and so does a misaligned object. Implemented as FreeBSD 11's [kern_umtx.c](https://github.com/freebsd/freebsd-src/blob/stable/11/sys/kern/kern_umtx.c): `UMTX_OP_WAIT`, `UMTX_OP_WAKE`, `UMTX_OP_WAIT_UINT`, `UMTX_OP_WAIT_UINT_PRIVATE`, `UMTX_OP_WAKE_PRIVATE`, `UMTX_OP_NWAKE_PRIVATE`, `UMTX_OP_SEM_WAIT` and `UMTX_OP_SEM_WAKE` (the last two built under `COMPAT_FREEBSD9`/`10`). Operations 0 and 1 fail with `EOPNOTSUPP`, as measured on a PS5 (firmware 12.70). The `SEM2` operations, 23 and 24, were measured on the console and returned `0` for a count of 0 where FreeBSD would time out, so they stay unimplemented. +- [sceKernelCreateSema, sceKernelPollSema, sceKernelSignalSema, sceKernelDeleteSema](../../../core/libs/prx/libkernel/Semaphore/src/Semaphore.cpp) (libkernel) - a poll, a signal or a second delete of a deleted semaphore reads freed memory here, where the console returns `0x80020003` (ESRCH) ([measured](https://github.com/mattias800/prosper/blob/3ea5cb9a846b6dcd00f5073360462bbda4f72288/prosper/tests/data/console_oracle/kernel.golden.tsv#L17-L19)). A null output pointer to `sceKernelCreateSema`, an attribute above 2, a negative maximum and a negative signal count throw, as they were not measured. `SCE_KERNEL_ERROR_EINVAL` for an initial count above the maximum or negative, a maximum of 0, a null name, a poll of 0 or above the maximum and a signal of 0 is [measured](https://github.com/mattias800/prosper/blob/3ea5cb9a846b6dcd00f5073360462bbda4f72288/prosper/tests/data/console_oracle/kernel.golden.tsv#L10-L23). - [sceKernelSyncOnAddressWait](../../../core/libs/prx/libkernel/SyncOnAddress/src/SyncOnAddress.cpp) (libkernel) - the only known caller passes a null timeout and a name string as the fourth argument; the timeout is assumed to point to microseconds like the other kernel waits, and the name is ignored - [sceKernelSyncOnAddressWait8 / sceKernelSyncOnAddressWait16 / sceKernelSyncOnAddressWait32 / sceKernelSyncOnAddressWait64](../../../core/libs/prx/libkernel/SyncOnAddress/src/SyncOnAddress.cpp) (libkernel) - libc's `__std_atomic_wait_direct_8/16/32/64` pass a microsecond timeout or null and ignore the result; the by-value comparand, return values and the 2- and 8-byte alignment of Wait16 and Wait64 are assumed - [scePthreadRwlockDestroy, pthread_rwlock_destroy](../../../core/libs/prx/libkernel/Pthread/src/Rwlock.cpp) (libkernel) - measured on a PS5 (firmware 13.60): destroying a rwlock returns 0 whether it is free or held for reading or writing, and leaves 1 in the handle; every call on that handle then returns `EINVAL` until it is initialized again, and a zero handle is initialized on first use by the `scePthreadRwlock` calls as by the POSIX ones. A rwlock destroyed while held is not freed, so its memory leaks. Destroying a rwlock while another thread is acquiring it is not guarded - [scePthreadCondWait / scePthreadCondTimedwait](../../../core/libs/prx/libkernel/Pthread/src/Cond.cpp) (libkernel) - before waiting, FreeBSD libthr's [`cond_wait_common`](https://github.com/freebsd/freebsd-src/blob/main/lib/libthr/thread/thr_cond.c#L1992-L2029) checks ownership through [`_mutex_owned`](https://github.com/freebsd/freebsd-src/blob/main/lib/libthr/thread/thr_mutex.c#L1166-L1178): an unowned mutex returns `EPERM`, while its destroyed marker returns `EINVAL`. An untouched adaptive static initializer (slot value 1) therefore returns `EPERM`; the test covers this, an uninitialized slot, a mutex owned by another thread, and the destroyed marker for both timed and untimed sce and POSIX waits. A recursive mutex locked several times is released completely for the wait and relocked to the same depth afterwards, as FreeBSD libthr does (`_mutex_cv_unlock`, `_mutex_cv_lock`). These FreeBSD-derived behaviors remain unverified on console hardware. Cancellation follows the [POSIX condition wait contract](https://pubs.opengroup.org/onlinepubs/9799919799/functions/pthread_cond_clockwait.html): the original mutex and recursive depth are restored before cleanup, and a canceled waiter forwards a wake to the remaining waiters. The host condition state is retained across mutex reacquisition so the guest can destroy and free its condition after notification; cancellation ordering and signal replacement are not measured on a console - [sceKernelOpenEventFlag](../../../core/libs/prx/libkernel/EventFlag/src/EventFlag.cpp) (libkernel) - opens only flags this process created with attribute 0x100. On the console, other processes' 0x100 flags, such as the shell's `SceShellCoreUtilAppFocus`, can be opened too. They do not exist here, so opening them returns `SCE_KERNEL_ERROR_ESRCH`. A null output with the name of an openable flag returns `SCE_KERNEL_ERROR_EFAULT`, the host's answer to the console's fault there, not a measured code. With a plain flag's name it returns `SCE_KERNEL_ERROR_ESRCH`, as measured; with a missing name it also returns `SCE_KERNEL_ERROR_ESRCH`, which was not measured. - [sceKernelCreateEventFlag](../../../core/libs/prx/libkernel/EventFlag/src/EventFlag.cpp) (libkernel) - when a call has both an invalid attribute and a name of 32 bytes or more, `SCE_KERNEL_ERROR_EINVAL` is returned, because the attribute checks run before the name length check. The console was only measured with long names and valid attributes (0x20 and 0x120), so which error it returns first is not known. Event flag handles are host addresses here, while the console returns small IDs. +- [sceKernelAioSubmitReadCommandsMultiple / sceKernelAioSubmitWriteCommandsMultiple / sceKernelAioWaitRequests / sceKernelAioPollRequests / sceKernelAioCancelRequest / sceKernelAioCancelRequests / sceKernelAioDeleteRequests](../../../core/libs/prx/libkernel/Aio/src/Aio.cpp) (libkernel) - one id per request for the Multiple submits and wait mode 2 returning once one request completed follow shadPS4. Requests run synchronously on submit, so cancel never finds one processing. The console's ids are not the small numbers used here (it returned 33, 0x2021, 0x4021) - [sceKernelWaitSema, sceKernelPollSema, sceKernelSignalSema, sceKernelCancelSema, sceKernelDeleteSema](../../../core/libs/prx/libkernel/Semaphore/src/Semaphore.cpp) (libkernel) - `SCE_KERNEL_ERROR_ESRCH` for a null handle follows [shadPS4](https://github.com/shadps4-emu/shadPS4/blob/60c42b925c3d1e00008621b8587819999260ec31/src/core/libraries/kernel/threads/semaphore.cpp#L329-L366) (PS4), unverified on PS5. Handles are pointers, so a non-null handle that is not a live semaphore is not detected; shadPS4 returns `ESRCH` for any unallocated handle. -- [sceKernelAioSubmitReadCommandsMultiple / sceKernelAioSubmitWriteCommandsMultiple / sceKernelAioWaitRequests / sceKernelAioPollRequests / sceKernelAioCancelRequest / sceKernelAioCancelRequests / sceKernelAioDeleteRequests](../../../core/libs/prx/libkernel/Aio/src/Aio.cpp) (libkernel) - behaviour from shadPS4 (one id per request for the Multiple submits, cancel turns any request aborted and id 0 reports processing, wait mode 2 returns once one request completed, poll copies every state without waiting); the 128 requests or ids per batch limit is from SharpProspero, with an unknown error, so larger batches throw; wait modes other than 1 (and) and 2 (or) throw; null pointers, negative counts and invalid ids return the codes the single-request siblings use, checked before any state is written - [scePthreadAttr setters and getters](../../../core/libs/prx/libkernel/Pthread/src/Attr.cpp) (libkernel) - measured on a PS5 (firmware 13.60): a null or uninitialized attribute returns `EINVAL`, as does a null output of the stack size, detach state and scheduling parameter getters; a policy other than 1 and 3, an inheritance other than 0 and 4, or a priority outside 256 to 767 (or a null parameter) returns `ENOTSUP` (`0x8002002D`); a detach state other than 0 and 1 and a stack size below 16384 return `EINVAL`; any affinity mask is accepted. A null attribute pointer to `scePthreadAttrInit` and a null output of `scePthreadAttrGetaffinity` crashed the payload, so they still throw. A new attribute holds a 64 KiB stack size on the console and 1 MiB here, which the host code that runs on guest threads needs. `scePthreadAttrGetschedpolicy` and `scePthreadAttrGetinheritsched` were not measured and follow [FreeBSD 11.4 libthr](https://github.com/freebsd/freebsd-src/blob/releng/11.4/lib/libthr/thread/thr_attr.c#L216-L262); a null output of `scePthreadAttrGetinheritsched` throws, since libthr writes through it unchecked - [scePthreadAttrGet](../../../core/libs/prx/libkernel/Pthread/src/Attr.cpp) and [pthread_attr_get_np](../../../core/libs/prx/libkernel/Pthread/Posix/Attr.cpp) (libkernel) - the returned scheduling policy is unmeasured on a console. Both report the recorded guest policy, following [FreeBSD 11.4 libthr](https://github.com/freebsd/freebsd-src/blob/releng/11.4/lib/libthr/thread/thr_attr.c#L134-L164) - [pthread_getprio](../../../core/libs/prx/libkernel/Pthread/Posix/Thread.cpp) (libkernel) is unmeasured on the console. NID `aO+WS2xWuo4` is named in the firmware 3.20 [export inventory](https://github.com/DNNDHH/PS5-3.20_Libs/blob/b2a77c09cd71c01f34c790cfbe98c751a13262ad/libkernel.c). Its return and errno contract follows FreeBSD 11 [thr_getprio.c](https://github.com/freebsd/freebsd-src/blob/671fb7df97631020c42ce55527dc49fffe0656b7/lib/libthr/thread/thr_getprio.c): return the recorded guest priority, or -1 with `ESRCH` for an unknown or finished thread. The live-thread registry lock protects the read from a concurrent join. This reports guest scheduler state, not the host scheduling priority. @@ -94,7 +98,7 @@ - [sceCoredumpWriteUserData, sceCoredumpDebugTextOut](../../../core/libs/prx/libkernel/System/src/Coredump.cpp) have no active coredump report. WriteUserData validates a nonnull buffer and size at most `INT32_MAX`, including zero; accepted requests return the sign-extended outside-handler error. DebugTextOut still prints to stderr instead of a report; its validation and report integration remain unverified. - [sceCoredumpAttachUserFile, sceCoredumpAttachMemoryRegionAsUserFile](../../../core/libs/prx/libkernel/System/src/Coredump.cpp) cannot write files to an active coredump. Native null-name checks, the signed 32-bit memory length and SDK 6.50 flag validation are recovered. Nonnegative requests return the outside-handler error; negative memory lengths are ignored after name/flag validation. Kernel-side path handling and capture limits remain unimplemented. - [sceKernelAddTimerEvent](../../../core/libs/prx/libkernel/Equeue/Equeue.cpp) (libkernel) - `usec` is taken as the period in microseconds; re-adding an existing timer keeps its pending expiration count and restarts the period from the time of the call, where FreeBSD 12+ `filt_timertouch` clears pending expirations on `EV_ADD`; `usec == 0` fires on every wait. None of this is confirmed on a PS5 title -- [sceKernelWaitEqueue](../../../core/libs/prx/libkernel/Equeue/Equeue.cpp) (libkernel) - a level-triggered user event (`sceKernelAddUserEvent`) is reported at most once per call, as in FreeBSD `kqueue_scan` (`sys/kern/kern_event.c`), which re-queues a reported knote without `EV_CLEAR` or `EV_DISPATCH` behind the scan marker, and kevent(2), where several triggers do not produce several kevents. Not measured on a console. Events are scanned in list order and the list is not rotated, so when `num` is smaller than the number of ready level events the later ones can starve, where FreeBSD moves a reported level knote to the tail. Several completions of one AMPR or VideoOut event within one call are returned as separate entries, where FreeBSD would aggregate them; the Orbis behaviour is unconfirmed +- [sceKernelWaitEqueue](../../../core/libs/prx/libkernel/Equeue/Equeue.cpp) (libkernel) - a level-triggered user event (`sceKernelAddUserEvent`) is reported at most once per call, as in FreeBSD `kqueue_scan` (`sys/kern/kern_event.c`), which re-queues a reported knote without `EV_CLEAR` or `EV_DISPATCH` behind the scan marker, and kevent(2), where several triggers do not produce several kevents. Not measured on a console. A reported event moves to the end of the list, so events are reported in the order they were last reported, and those never reported come first in registration order. FreeBSD `knote_enqueue` queues them in the order they fire. Several completions of one AMPR or VideoOut event within one call are returned as separate entries, where FreeBSD would aggregate them; the Orbis behaviour is unconfirmed - [AC1FtjqMCL0](../../../core/libs/prx/libkernel/Unimplemented.cpp) (libkernel) - unknown name, signature - [0iEKNAvT600](../../../core/libs/prx/libkernel/Unimplemented.cpp) (libkernel) - unknown name, signature - [2cELEBPdYQ0](../../../core/libs/prx/libkernel/Unimplemented.cpp) (libkernel) - unknown name, signature @@ -124,13 +128,17 @@ - [scePthreadYield](../../../core/libs/prx/libkernel/Pthread/src/Thread.cpp) (libkernel) on Windows calls Sleep(1) after 512 consecutive yields that find no other thread ready on the current processor and arrive less than ~10,000 TSC ticks apart (so the span varies with host CPU TSC rate); FreeBSD sched_yield never sleeps. - [Guest thread scheduling](../../../core/libs/prx/libkernel/Pthread/src/Thread.cpp) (libkernel) - the policies and priorities that `sched_get_priority_max`, `sched_get_priority_min`, `pthread_setschedparam` and `scePthreadSetschedparam` accept, and those a created thread takes, follow a PS5 (firmware 13.60), but they are only recorded and reported: they are not applied to host threads, so a title's priority order has no effect on host scheduling +- [Linux runtime module initialization](../../../core/libs/prx/libkernel/Module/src/DynamicLoader.cpp) (libkernel) - modules in explicit runtime directories can be mapped for bindings before an explicit guest load runs their entry and init array once with the caller arguments. Finalizers run only after initialization completed. Console dependency-start and finalization ordering are unmeasured. Windows and macOS use their existing lifecycle paths. + - [sceKernelMprotect, sceKernelMtypeprotect](../../../core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp) (libkernel) over a range with a gap that is not registered in the allocation tracker protect the registered parts and skip the gap, and return 0, as FreeBSD 11.4's [`vm_map_protect`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/vm/vm_map.c) does (it has no check for holes); a range whose start is unmapped (also when later pages are mapped) returns `SCE_KERNEL_ERROR_EINVAL` (`mprotect`: `EINVAL`) because an existing test pins that for an unmapped start; FreeBSD answers 0 there too (`vm_map_protect` takes the next entry), so this errno is not taken from the source and was not changed. `sceKernelMprotect` threw for a gap before, which ended the title. Every part is checked for a GPU or image pin before the first one is protected; the parts are then protected one after the other, so a host protection failure in a later part leaves the earlier parts changed (FreeBSD's `vm_map_protect` cannot fail in its second pass). The behaviour on the console is not known. None of this was measured on a console. - [sceKernelMprotect protection bits](../../../core/libs/prx/libkernel/DirectMemory/Export.cpp) (libkernel) - bits outside `0x3F7` are dropped instead of refused, in the POSIX `mprotect` as well (FreeBSD 11.4 masks the argument, `sys/vm/vm_mmap.c:613`; the console was measured only through `sceKernelMprotect`), as a PS5 (firmware 13.60) drops them: Onimusha: Way of the Sword (PPSA27836 01.000.001) maps direct memory with `0xf2` and then protects it with `0xfffffff2`, which the console reports as `0xf3` and then `0x3f3`. In a payload process the console also drops the CPU execute bit (`0x4`) of flexible, direct and JIT memory, and POSIX `mprotect` with write and execute reports write only (`0x2`). A title process may keep execute, so it is kept here. The map calls and `sceKernelMtypeprotect` refuse undefined bits on the console with `EINVAL` (`0x80020016`), and the direct map calls refuse execute; here both throw for undefined bits, and the direct map calls map execute - [JIT shared memory on macOS](../../../core/libs/prx/libkernel/DirectMemory/Jit.cpp) (libkernel) - the object is an unlinked `mkstemp` file in the temporary directory, as `shm_open` makes its anonymous objects. macOS refuses an executable shared file mapping (`EPERM`), so an executable view is mapped without execute and then given it with `mprotect`. Under Rosetta 2 (Apple M2, macOS 26), code rewritten through one view after it ran from another keeps its old translation until the executable view's protection changes; first writes and a single read-write-execute view, as XPSemu PPSA97358 and VLC PPSA85300 map, are seen. `mmap` of a JIT descriptor is not supported, as file-backed guest `mmap` throws on macOS. - [JIT shared memory](../../../core/libs/prx/libkernel/DirectMemory/Jit.cpp) (libkernel) - `sceKernelJitCreateSharedMemory`, `sceKernelJitCreateAliasOfSharedMemory` and `sceKernelJitMapSharedMemory` take the signatures of [Orbital](https://github.com/AlexAltea/orbital)'s `tools/ida/db_types.json` (name, length, maximum protection, descriptor out; descriptor, maximum protection, descriptor out; descriptor, protection, address in and out), which ps4sdk, ps5-jar-loader and the call sites in RetroArch PPSA99169, XPSemu PPSA97358 and VLC PPSA85300 match; SharpProspero declares the alias as returning the descriptor. The object is a host anonymous file shared by its descriptors (a `memfd` on Linux, a delete-on-close temporary file mapped as an executable section on Windows), the alias is a duplicate descriptor, and every map maps the whole object, so writes through one view show through the others; a non-null address is a hint, as in `mmap` without `MAP_FIXED`. Not measured on the console: a length that is zero or not a multiple of 16 KiB and protection bits other than CPU read, write and execute are `EINVAL`; an alias or a map asking for more than the descriptor's maximum protection (write and execute counting as read) is `EACCES`, as FreeBSD 11's `vm_map_protect` treats a protection past `max_protection`; a descriptor that is not a JIT object is `EBADF`. The name is ignored, the objects do not count against the flexible memory budget, `mprotect` of a view is not limited to the maximum protection, and `mmap` of the descriptors maps the object as a [regular file](../../../core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp), so its views and the JIT views see each other's writes. On Linux the `memfd` is created without `MFD_EXEC`, which Linux 6.3 added and older kernels reject with `EINVAL`, and relies on the protection of the mapping: the flag only sets the file's execute permission bits, and the direct memory `memfd` is mapped executable the same way; a host with `vm.memfd_noexec` set to 2 refuses the creation and the call throws -- [kqueue and kevent](../../../core/libs/prx/libkernel/Equeue/src/Kqueue.cpp) (libkernel) follow FreeBSD 11's [`kern_event.c`](https://github.com/freebsd/freebsd-src/blob/34bd34750187bae02a0086402c78c031c59dd8b3/sys/kern/kern_event.c) (`kqueue_kevent`, `kqueue_register`, `kqueue_scan`, the timer and user filters) over the event queue; nothing is measured on a console. The descriptor is a host null device registered as the kqueue: `close` and `sceKernelClose` close the queue and `dup`/`dup2` of it throw, but `read`, `write` and `fstat` act on the null device instead of failing with `EOPNOTSUPP` or reporting `S_IFIFO`. Read and write events are supported on guest sockets only, plus `EVFILT_READ` on the read end and `EVFILT_WRITE` on the write end of a pipe (see the kevent on a pipe entry) and the timer, user and [vnode](../../../core/libs/prx/libkernel/Equeue/src/FileEvent.cpp) filters and a read event on the host stream on descriptor 0, which never triggers, as `select` never reports descriptor 0 readable; PPSA34015 registers descriptor 0 for reading in a new kqueue at start-up and does not check the result; other filters, `EV_CLEAR` on a socket or a pipe off Linux (see the kevent EV_CLEAR entry), a `NOTE_NSECONDS` period above one second with a fraction (11.0 converts the fraction with the microsecond macro) and any change to an existing timer (11.0 keeps the armed expiry and applies the new period after it, 11.4's `filt_timertouch` restarts it) throw. `EV_DISABLE`, `EV_DISPATCH` and `EV_FORCEONESHOT` follow kern_event.c: a disabled knote stays registered, stops polling or firing and is not reported, a trigger raised while it is disabled is kept and reported after `EV_ENABLE`, where `knote` would drop it, `EV_DISPATCH` reports once and then disables the knote, and `EV_FORCEONESHOT` is ignored on a new knote while on an existing one it sets `EV_ONESHOT` and fires the knote; a negative `nevents` fails with `EINVAL`. A new user event keeps its `fflags` unmasked and is not triggered by `NOTE_TRIGGER`, because `filt_userattach` reads the already cleared `kn_fflags`. The timer limit `kq_calloutmax` (`ENOMEM`) is not enforced, and closing a kqueue wakes a thread waiting on it in `kevent` with `EBADF`, where `kqueue_drain` makes the close wait for that thread. A level-triggered event is reported once per call and moved behind the other events, as in `kqueue_scan`; the `sceKernel*Equeue` calls keep their own collection loop. `NOTE_LOWAT` on a socket sets the low-water mark as FreeBSD 11's [`filt_soread` and `filt_sowrite`](https://github.com/freebsd/freebsd-src/blob/release/11.0.0/sys/kern/uipc_socket.c) do, and a later change of the knote replaces it as `kqueue_register` replaces `kn_sdata`: a read event fires once the buffered bytes reach the mark, or at end of file or on a listening socket. A read mark above 1 is taken on stream sockets only: on a datagram socket Linux's `FIONREAD` reports the next datagram where FreeBSD's `sbavail` counts every queued byte, so it throws. A write event fires once the send buffer's free space reaches the mark, as `filt_sowrite` does with `sbspace`; the host reports that space as `SO_SNDBUF` less `SIOCOUTQ` on Linux and as `SO_SNDBUF` alone elsewhere, so off Linux it does not shrink while data is queued. Marks below 1, which make FreeBSD's read filter always fire, throw +- [kqueue and kevent](../../../core/libs/prx/libkernel/Equeue/src/Kqueue.cpp) (libkernel) follow FreeBSD 11's [`kern_event.c`](https://github.com/freebsd/freebsd-src/blob/34bd34750187bae02a0086402c78c031c59dd8b3/sys/kern/kern_event.c) (`kqueue_kevent`, `kqueue_register`, `kqueue_scan`, the timer and user filters) over the event queue; nothing here is taken from a console except what the kevent measurements entry lists. The descriptor is a host null device registered as the kqueue: `close` and `sceKernelClose` close the queue and `dup`/`dup2` of it throw, but `read`, `write` and `fstat` act on the null device instead of failing with `EOPNOTSUPP` or reporting `S_IFIFO`. Read and write events are supported on guest sockets only, plus `EVFILT_READ` on the read end and `EVFILT_WRITE` on the write end of a pipe (see the kevent on a pipe entry) and the timer, user and [vnode](../../../core/libs/prx/libkernel/Equeue/src/FileEvent.cpp) filters and a read event on the host stream on descriptor 0, which never triggers, as `select` never reports descriptor 0 readable; PPSA34015 registers descriptor 0 for reading in a new kqueue at start-up and does not check the result; other filters except `EVFILT_AIO`, `EVFILT_LIO`, `EVFILT_SENDFILE` and `EVFILT_PROCDESC` (see the kevent measurements entry), `EV_CLEAR` on a socket or a pipe off Linux (see the kevent EV_CLEAR entry) and every change to an existing timer except `EV_DELETE` and the `EV_ADD` re-adds that the kevent measurements entry describes throw. A `NOTE_NSECONDS` period above one second with a fraction is kept to the nanosecond, where 11.0 converts the fraction with the microsecond macro. `EV_DISABLE`, `EV_DISPATCH` and `EV_FORCEONESHOT` follow kern_event.c: a disabled knote stays registered, stops polling or firing and is not reported, a trigger raised while it is disabled is kept and reported after `EV_ENABLE`, where `knote` would drop it, `EV_DISPATCH` reports once and then disables the knote, and `EV_FORCEONESHOT` is ignored on a new knote while on an existing one it sets `EV_ONESHOT` and fires the knote; a negative `nevents` fails with `EINVAL`. A new user event keeps its `fflags` unmasked and is not triggered by `NOTE_TRIGGER`, because `filt_userattach` reads the already cleared `kn_fflags`. The timer limit `kq_calloutmax` (`ENOMEM`) is not enforced, and closing a kqueue wakes a thread waiting on it in `kevent` with `EBADF`, where `kqueue_drain` makes the close wait for that thread. A level-triggered event is reported once per call and moved behind the other events, as in `kqueue_scan`; the `sceKernel*Equeue` calls keep their own collection loop. `NOTE_LOWAT` on a socket sets the low-water mark as FreeBSD 11's [`filt_soread` and `filt_sowrite`](https://github.com/freebsd/freebsd-src/blob/release/11.0.0/sys/kern/uipc_socket.c) do, and a later change of the knote replaces it as `kqueue_register` replaces `kn_sdata`: a read event fires once the buffered bytes reach the mark, or at end of file or on a listening socket. A read mark above 1 is taken on stream sockets only: on a datagram socket Linux's `FIONREAD` reports the next datagram where FreeBSD's `sbavail` counts every queued byte, so it throws. A write event fires once the send buffer's free space reaches the mark, as `filt_sowrite` does with `sbspace`; the host reports that space as `SO_SNDBUF` less `SIOCOUTQ` on Linux and as `SO_SNDBUF` alone elsewhere, so off Linux it does not shrink while data is queued. Marks below 1, which make FreeBSD's read filter always fire, throw - [EVFILT_VNODE, sceKernelAddFileEvent and sceKernelDeleteFileEvent](../../../core/libs/prx/libkernel/Equeue/src/FileEvent.cpp) (libkernel) watch the host file or directory behind the descriptor and turn host changes into FreeBSD 11 vnode hints, which [`filt_vfsvnode`](https://github.com/freebsd/freebsd-src/blob/34bd34750187bae02a0086402c78c031c59dd8b3/sys/kern/vfs_subr.c#L5032) adds whole when they share a bit with the requested `fflags`: a write that grows a file is `NOTE_WRITE | NOTE_EXTEND`, another write `NOTE_WRITE`, a shrinking truncate or an attribute change `NOTE_ATTRIB`, a higher link count `NOTE_LINK`, a lower one or a pending delete `NOTE_DELETE`, a new path `NOTE_RENAME`; in a directory, an entry created, removed or renamed is `NOTE_WRITE`, with `NOTE_LINK` for a subdirectory, and on Linux an entry moved from or to another directory adds `NOTE_EXTEND`; inotify queues the two halves of a rename one after the other, so a watcher woken by the first can read it alone, and a half read without the other is classified only after a `getdents64` on the watched directory, which takes the directory lock that `rename` holds until both halves are queued ([`do_renameat2`](https://github.com/torvalds/linux/blob/v6.8/fs/namei.c#L4975) around `vfs_rename` and its `fsnotify_move`, [`iterate_dir`](https://github.com/torvalds/linux/blob/v6.8/fs/readdir.c#L103)), an ordering taken from the kernel source, not from inotify(7), which promises none; a directory registered with `NOTE_EXTEND` on Windows is accepted, as `vfs_kqfilter` does not check it, but never receives it, because the directory handle cannot tell a move between directories from a removal plus an addition. FreeBSD's hints depend on the filesystem and the console's filesystems are not known: a growing `ftruncate` reports `NOTE_WRITE | NOTE_EXTEND` where `vop_setattr_post` reports `NOTE_ATTRIB`, and on Windows `utimes` on a file reports `NOTE_WRITE`. The three Windows notifications are told apart in the error, which names the `parent`, `entries` or `subdirectories` watch, the path it was given and `GetLastError`, but a `FindFirstChangeNotification` that fails still throws rather than returning an error to the guest: the EVFILT_VNODE attach path is [`vfs_kqfilter`](https://github.com/freebsd/freebsd-src/blob/34bd34750187bae02a0086402c78c031c59dd8b3/sys/kern/vfs_subr.c#L4936), which returns `EINVAL`, `ENOMEM` or 0 after `fget` has turned a descriptor that is not open into `EBADF`, and consults no directory permission, so no FreeBSD errno stands for a host that refuses to notify, and what the console does on such a host is not measured. Only the `parent` watch can fail this way in practice: `entries` and `subdirectories` need the `FILE_LIST_DIRECTORY` that opening the watched directory already took. The parent handle produces no hint of its own, as `RunHostWatch` only calls `FindNextChangeNotification` on it, so a later change may leave the watch on its 50 ms poll instead of throwing; that is not done here. Windows uses `FindFirstChangeNotification` on the parent directory (and on a watched directory itself) and compares `GetFileInformationByHandleEx` snapshots at least every 50 ms, because a write through an open handle is reported only when the handle is flushed or closed, so file hints can arrive 50 ms late, and a file deleted while watched, which the POSIX delete of Windows 10 1903 moves into `$Extend\$Deleted` until its last handle closes, reports `NOTE_DELETE` without `NOTE_RENAME` and keeps its handle without a parent directory watch, as that directory cannot be watched; Linux uses inotify on the file and is not run. `NOTE_REVOKE`, `NOTE_OPEN`, `NOTE_CLOSE`, `NOTE_CLOSE_WRITE`, `NOTE_READ`, unknown bits and descriptors other than files, directories, pipes and sockets throw. `sceKernelAddFileEvent(eq, fd, watch, udata)` and `sceKernelDeleteFileEvent(eq, fd)` take the [OpenOrbis](https://github.com/OpenOrbis/OpenOrbis-PS4-Toolchain/blob/0a1aaf9d/include/orbis/libkernel.h#L35) and [SharpProspero](https://github.com/SvenGDK/SharpProspero/blob/5dfdb4b0/src/SharpProspero/Interop/Kernel/KernelEqueue.cs#L164) signatures; `watch` is taken as the `fflags` and the event is added with `EV_CLEAR`, neither confirmed on a title (feat(libkernel): implement kevent EV_DISABLE, EV_DISPATCH and EV_FORCEONESHOT) (fix(libkernel): follow kqueue_register for EV_FORCEONESHOT and modify flags) +- [sem_open, sem_close, sem_unlink](../../../core/libs/prx/libkernel/Pthread/Posix/Sem.cpp) (libkernel) keep named semaphores in a table inside the process; FreeBSD 11.4's [`sem_new.c`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/lib/libc/gen/sem_new.c#L138-L308) backs each with a file `/tmp/SEMD`. A semaphore lasts until `sem_unlink` and its last `sem_close` but is not shared with other processes, and the mode is ignored (no permission errors, as for a root caller). Whether a title can create files under `/tmp` on the console is unverified. A name with a further `/` throws, as it would resolve below `/tmp`, and so does an initial value above `SEM_VALUE_MAX`, which FreeBSD stores unchecked. - [sem_timedwait](../../../core/libs/prx/libkernel/Pthread/Posix/Sem.cpp) (libkernel) consumes an available count before checking the timeout and waits indefinitely for a null timeout, following FreeBSD 11's `sem_timedwait`. Console behavior for null and invalid timeouts when a count is available is unverified. +- [kevent measurements](../../../core/libs/prx/libkernel/Equeue/src/Kqueue.cpp) (libkernel) - in payload processes on a PS5 (firmware 12.70), re-adding an existing periodic `EVFILT_TIMER` with `EV_ADD`, `fflags` 0 and a positive period kept the armed expiry and the expirations already counted and applied the new period after that expiry, as FreeBSD 11.0 does, where 11.4's `filt_timertouch` restarts the timer: a 50 ms timer re-added with 10 ms fired first 46 ms after the re-add and then every 10 ms, a 10 ms timer re-added with 100 ms fired first 5 ms after the re-add and then every 100 ms, the 12 expirations counted before a re-add were reported after it, and `EV_ONESHOT` in the re-add left the timer periodic. Here such a re-add does the same; re-adding a one-shot or disabled timer, a re-add with nonzero `fflags` (`NOTE_MSECONDS` included), a period of 0 or less, a period too large to add to the armed expiry or flags other than `EV_ONESHOT` were not measured and throw here, and the other changes to an existing timer except `EV_DELETE` throw here too. There `EV_ADD` failed with `EPERM` for `EVFILT_AIO`, `EVFILT_LIO` and `EVFILT_SENDFILE` at every ident tried (0, 5, 999, 2^40 and an open pipe) and, at ident 5, also with `EV_CLEAR`, `EV_ONESHOT` or `EV_DISABLE` added, and `EV_DELETE` and `EV_ENABLE` without `EV_ADD` failed with `ENOENT`. `EVFILT_PROCDESC` failed with `EBADF` on a descriptor that is not open and on an ident above `INT_MAX`, and with `EINVAL` on every open descriptor tried: Unix-domain stream and datagram sockets, descriptor 0 (a socket), an `AF_INET` UDP socket, both ends of a pipe, a regular file, a directory, another kqueue and the kqueue itself; `EV_DELETE` on a socket failed with `ENOENT`. Here these four filters answer the same way. `EV_ADD` of `EVFILT_PROCDESC` fails with `EINVAL` on the kinds of descriptor tried, since there are no process descriptors here, and also on shared memory and JIT shared memory, which are host files here: FreeBSD 11.0 gives `shm_open` objects [`invfo_kqfilter`](https://github.com/freebsd/freebsd-src/blob/release/11.0.0/sys/kern/uipc_shm.c#L136), which answers `EINVAL`, but neither was measured. It throws on any other kind, such as `/dev/random` and `/dev/urandom`, and on descriptors 0 to 2 that are not sockets or kqueues, whatever they hold: the host's standard streams, a pipe or file that `dup2` put on one of them, or a descriptor opened on a number that was free when libkernel loaded. A change of any of the four without `EV_ADD` fails with `ENOENT`, for `EVFILT_PROCDESC` after the descriptor check, as `kqueue_register` finds no knote. When `EV_ADD` of one of the four fails with `EPERM`, or with `EINVAL` on a descriptor other than the kqueue itself, the error event has `fflags` 0 and the errno in `data`, as FreeBSD 11.0's [`kqueue_register`](https://github.com/freebsd/freebsd-src/blob/release/11.0.0/sys/kern/kern_event.c#L1302-L1316) clears `fflags` and `data` before the attach and [`kqueue_kevent`](https://github.com/freebsd/freebsd-src/blob/release/11.0.0/sys/kern/kern_event.c#L1005-L1006) then stores the error in `data`; a descriptor that is not open, the kqueue itself and `EV_ENABLE` with `EV_DISABLE` fail before that and keep their `fflags`. That error event was not measured. `EVFILT_PROC` on the process itself with `NOTE_EXIT`, `EVFILT_SIGNAL` for `SIGUSR1` and `EVFILT_FS` at ident 0 were accepted there and still throw here. Also measured there on Unix-domain socketpairs, and still throwing here: a `NOTE_LOWAT` mark of 0 on an empty stream socket gave a read event at once (`data` 0), and so did re-adding that knote with a mark of -1 once the socket was drained; a write mark of 2 on the other end of that stream socketpair gave an event (`data` 8192), and re-adding that knote with 2^30 gave none; a mark of 5 on a datagram socket gave no read event after one 1-byte datagram and a read event (`data` 11) after a second, 10-byte datagram. `EVFILT_READ` on another kqueue reported the number of events pending in it there (1, then 2) and throws here; `EVFILT_WRITE` on a kqueue and a kqueue registered on itself failed with `EINVAL` there, as they do here - [tcsendbreak, tcdrain, tcflush, tcflow, tcgetpgrp, tcsetpgrp](../../../core/libs/prx/libkernel/File/src/Terminal.cpp) (libkernel) follow FreeBSD 11.4's [`termios.c`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/lib/libc/gen/termios.c): `tcflush` answers `EINVAL` for a queue other than `TCIFLUSH`, `TCOFLUSH` or `TCIOFLUSH` and `tcflow` for an action outside `TCOOFF`..`TCION` before the descriptor is looked at, and every other call fails as its terminal ioctl does on a descriptor that is not a terminal, with the errors `tcgetattr` gives, so `tcsendbreak` never reaches its 0.4 s pause. FreeBSD's `pipe_ioctl` answers `TIOCGPGRP` and `TIOCSPGRP` on a pipe or FIFO with the descriptor's SIGIO owner, which is not modelled, so `tcgetpgrp` and `tcsetpgrp` throw on a host pipe or FIFO above descriptor 2; descriptors 0 to 2 are taken as non-terminals that are not pipes, whatever the host attaches to them, so a pipe the guest creates on a freed descriptor 0 to 2 gets `ENOTTY` there. The PS5 `libkernel` is assumed to use this FreeBSD code, and `tcdrain` is not a cancellation point here - [Windows file timestamps](../../../core/libs/prx/libkernel/File/include/WindowsFileTime.hpp) use the host filesystem's timestamp range and resolution. `utimes`, `futimes`, and `lutimes` reject timestamps outside nonnegative signed 64-bit Windows file-time ticks with `EINVAL`, including exactly 1601-01-01 00:00:00 UTC, whose zero value means "leave unchanged" to `SetFileTime`. Directory timestamp updates resolve the current path of the opened handle. Filesystems with coarser timestamp resolution cannot retain every guest microsecond. Birth-time clamping is not atomic with concurrent host timestamp updates and has not been measured on a console - [EVFILT_VNODE on macOS](../../../core/libs/prx/libkernel/Equeue/src/FileEvent.cpp) (libkernel) registers `EVFILT_VNODE` on a duplicate of the descriptor in a host kqueue, whose notes have FreeBSD's values, and drops the `NOTE_LINK` that XNU adds to `NOTE_DELETE` when the watched file or directory is removed, since FreeBSD's `vop_remove_post` and `vop_rmdir_post` report `NOTE_DELETE` alone. As on Windows, a directory registered with `NOTE_EXTEND` is accepted but never receives it: XNU reports a move between directories as `NOTE_WRITE` alone, with `NOTE_LINK` for a subdirectory @@ -147,13 +155,15 @@ - [readv, writev, preadv, pwritev and their sceKernel forms](../../../core/libs/prx/libkernel/File/src/Stdio.cpp) (libkernel) - unreadable iovec-table errors follow FreeBSD 11.4's [`copyinuio`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/subr_uio.c#L401-L434), not console measurements. The table is copied before inspecting its entries; payload-buffer validation is unchanged. A Linux `process_vm_readv` failure other than `EFAULT` throws, including when the host denies that API. - [/dev/random and /dev/urandom](../../../core/libs/prx/libkernel/File/src/RandomDevice.cpp) (libkernel) opened for reading return a descriptor backed by the host null device whose reads are filled with host OS entropy (`getentropy`, `rand_s` on Windows); both paths behave the same, never block, and positioned reads ignore the offset (FreeBSD `random(4)`). Only the exact paths are recognised, `fstat`, `lseek` and `mmap` see the null device, and opening them for writing throws - [pthread_attr_getinheritsched, pthread_attr_setinheritsched, pthread_attr_getscope, pthread_attr_setscope, pthread_attr_getstackaddr](../../../core/libs/prx/libkernel/Pthread/Posix/Attr.cpp) (libkernel) follow FreeBSD 11 libthr ([thr_attr.c](https://github.com/freebsd/freebsd-src/blob/5ed7eb0d97ba4436218e810f61bd059acba984c2/lib/libthr/thread/thr_attr.c), [thr_create.c](https://github.com/freebsd/freebsd-src/blob/5ed7eb0d97ba4436218e810f61bd059acba984c2/lib/libthr/thread/thr_create.c)), not measured on the console: the scope defaults to `PTHREAD_SCOPE_SYSTEM` (2), only it and `PTHREAD_SCOPE_PROCESS` (0) are accepted and other values return `EINVAL`, and an inherit value other than `PTHREAD_INHERIT_SCHED` (4) or `PTHREAD_EXPLICIT_SCHED` (0) returns `ENOTSUP`. `pthread_attr_getstackaddr` returns the configured stack address pointer (null by default). A null output of `pthread_attr_getinheritsched` throws, since libthr writes through it unchecked. A thread keeps the scope it was created with, taken from the creating thread under `PTHREAD_INHERIT_SCHED`, and `pthread_attr_get_np` and `scePthreadAttrGet` report it, but every guest thread is a host thread at system scope, so process scope changes nothing -- [Shared memory](../../../core/libs/prx/libkernel/File/src/SharedMemory.cpp) (libkernel) on Linux implements anonymous and named objects, bounded descriptor writes and seeks, and `shm_unlink` with effective read/write permission checks from FreeBSD 11. Named paths up to 1023 bytes use `/anyps5-shm-` plus SHA-1; collisions are not detected, and the namespace is shared across processes in the host mount namespace. Named creation applies the guest umask without changing the host mask. Linux procfs and `/dev/shm` are required. Operations use an advisory directory lock; external host operations bypass it, unrelated objects serialize, and descriptor operations require two spare host descriptors and compare device/inode with the initial descriptor identity on both the pinned-object and ordinary-file fallback paths. [Guest mappings](../../../core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp) support shared and private mappings of these descriptors through the allocation tracker and pin a duplicate descriptor during validation and mapping; mapping requires one spare host descriptor; file-backed mappings use the write-watch scan fallback because aliases and descriptor writes bypass per-mapping tracking. On Windows, anonymous objects are delete-on-close temporary files and `mmap` maps them as [regular files](../../../core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp); named objects remain unsupported. Invalid non-null name pointers, console behaviour and title compatibility remain unverified. +- [Shared memory](../../../core/libs/prx/libkernel/File/src/SharedMemory.cpp) (libkernel) on Linux implements anonymous and named objects, bounded descriptor writes and seeks, and `shm_unlink` with effective read/write permission checks from FreeBSD 11. Named paths up to 1023 bytes use `/anyps5-shm-` plus SHA-1; collisions are not detected, and the namespace is shared across processes in the host mount namespace. Named creation applies the guest umask without changing the host mask. Linux procfs and `/dev/shm` are required. Operations use an advisory directory lock; external host operations bypass it, unrelated objects serialize, and descriptor operations require two spare host descriptors and compare device/inode with the initial descriptor identity on both the pinned-object and ordinary-file fallback paths. [Guest mappings](../../../core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp) support shared and private mappings of these descriptors through the allocation tracker and pin a duplicate descriptor during validation and mapping; mapping requires one spare host descriptor; file-backed mappings use the write-watch scan fallback because aliases and descriptor writes bypass per-mapping tracking. On Windows, anonymous objects are delete-on-close temporary files and `mmap` maps them as [regular files](../../../core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp); named objects are described in the next entry. Invalid non-null name pointers, console behaviour and title compatibility remain unverified. +- [shm_open, shm_unlink](../../../core/libs/prx/libkernel/File/src/SharedMemory.cpp) (libkernel, Windows) - named objects are regular files in the current user's temporary directory under `AnyPS5/shm`, identified by SHA-1 of the exact guest name. The namespace is per host user and shared by every AnyPS5 process using that temporary root, so two titles using the same name share the object. Namespace operations use a cross-session named mutex, while file-size changes and writes use a mutex keyed by the host file identity; unlink renames the visible object to a unique internal name so already-open handles remain usable while the guest name can be recreated. Orphaned files are reaped when the last tracked guest descriptor closes or opportunistically during a later open. Closing through host stdio wrappers does not update this bookkeeping or reap immediately. The namespace directory has a protected ACL for the owner, SYSTEM and Administrators; newly created objects map the guest owner read/write bits after applying the guest creation mask. `fchmod` updates those owner permissions on the descriptor's file object. Guest group/other permission classes are not represented independently on Windows. Mapping a named object uses the same Windows file mapper as anonymous objects and no test covers it, and no behavior here has been measured on PS5. Lifecycle, cross-process resize/write, and mode behavior are exercised by Windows tests. See Microsoft's [file sharing modes](https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilea), [DeleteFile](https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-deletefilew) and [mutex objects](https://learn.microsoft.com/en-us/windows/win32/sync/mutex-objects). - [sceKernelAddReadEvent and sceKernelAddWriteEvent](../../../core/libs/prx/libkernel/Equeue/Equeue.cpp) (libkernel): a read event on a listening socket, here and in `kevent`, reports the number of completed connections waiting to be accepted in `data`, as FreeBSD 11.4's [`filt_solisten`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_socket.c#L3384-L3392) reports `so_qlen`: on Linux a TCP listener's accept queue from `TCP_INFO` (`tcpi_unacked`, which Linux fills with the accept backlog for a listening socket), and elsewhere, or for a listener that is not TCP, 1 while a connection is waiting, as no host call reports that count. A write event reports the free space of the socket's send buffer in `data`, as FreeBSD 11.4's [`filt_sowrite`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_socket.c#L3358-L3382) reports `sbspace`: on Linux the host's `SO_SNDBUF` less the bytes `SIOCOUTQ` counts as queued, which include the host's per-packet overhead, so the space shrinks by somewhat more than the bytes sent. Windows has no query for that space and macOS is not wired up, so both report the whole `SO_SNDBUF`, which does not shrink as data is queued. Only guest sockets are supported; other descriptors and a low watermark above 1 throw -- [sceKernelAddReadEvent on pipes](../../../core/libs/prx/libkernel/Equeue/Equeue.cpp) (libkernel) is the one exception to the sockets-only rule above: it also takes the read end of a pipe, as `kevent` does through FreeBSD's `filt_piperead`, and ignores the size argument there. Host streams, other descriptors and a write event on a pipe throw +- [sceKernelAddReadEvent on pipes](../../../core/libs/prx/libkernel/Equeue/Equeue.cpp) (libkernel) is an exception to the sockets-only rule above: it also takes the read end of a pipe, as `kevent` does through FreeBSD's `filt_piperead`, and ignores the size argument there. Host streams, other descriptors and a write event on a pipe throw +- [sceKernelAddReadEvent and sceKernelAddWriteEvent on regular files](../../../core/libs/prx/libkernel/Equeue/Equeue.cpp) (libkernel) are the other exception: a size argument above 1, directories and other vnode types throw, and whether the console's file systems answer as `vfs_kqfilter` does was not measured. A wait holding one of these events polls it every millisecond, as no host call reports every file change, so a write through another descriptor shows up to 1 ms late. Unlike FreeBSD, a seek can make a read event fire again: FreeBSD 11.4's [`kqueue_scan`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/kern_event.c) deactivates a level-triggered read knote once `filt_vfsread` reports `data` 0, and only a vnode `KNOTE` (a write, an extend or a truncate) activates it again; `lseek` posts none, so after a file read to its end is seeked back, the 1 ms poll here reports the new `data` (10 for a 10-byte file seeked to 0) where FreeBSD keeps waiting until the file changes. Otherwise they follow FreeBSD 11.4's [`filt_vfsread` and `filt_vfswrite`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/vfs_subr.c#L5122-L5168): a read event reports the size less the descriptor's offset in `data` and fires while that is not 0; a write event always fires with `data` 0 - [close](../../../core/libs/prx/libkernel/File/src/Stdio.cpp) and [setsockopt](../../../core/libs/prx/libkernel/Socket/src/Udp.cpp) (libkernel) reach a [libSceNet](../../../core/libs/prx/libSceNet/Export.cpp) socket through hooks libSceNet registers, as `select` polls them; the other libkernel socket calls do not. libSceNet numbers its sockets from 32 in its own table, so a CRT file descriptor and a socket can share a number; `close` then closes the socket. On the console both are one kernel descriptor space - [sceKernelPread, sceKernelPwrite, sceKernelPreadv, sceKernelPwritev](../../../core/libs/prx/libkernel/File/src/Stdio.cpp) (libkernel) on Windows transfer with an overlapped `ReadFile`/`WriteFile` and restore the descriptor's file pointer afterwards. Guest scalar transfers, positioned transfers, vectored transfers and seeks share a position lock across duplicated descriptors; a vector holds it for the whole operation. Independently opened descriptors and other processes do not share that lock, so vector transfers over their overlapping file ranges are not coordinated. Handles that are not disk files return `ESPIPE`. A pipe or console `readv` can return a short read when no more input is available; each iovec is transferred separately, so a failure after the first transfer returns the bytes already done - [sceKernelAioSubmitReadCommands, sceKernelAioSubmitWriteCommands and their Multiple variants](../../../core/libs/prx/libkernel/Aio/src/Aio.cpp) (libkernel) on Windows transfer through the same positioned path as `sceKernelPread`/`sceKernelPwrite` and restore the descriptor's file pointer afterwards; positioned transfers, plain transfers and seeks of one descriptor are serialized per descriptor (shared across duplicated descriptors), so a concurrent transfer observes a stable file pointer -- [sceKernelAioSetParam](../../../core/libs/prx/libkernel/Aio/src/Aio.cpp) (libkernel) writes the five values into the 20-byte scheduling struct the first argument points to, following the declarations in [Orbital](https://github.com/AlexAltea/orbital/blob/cc408367da6a6ebbf5ac1ac559fefeadc73b2831/tools/ida/db_types.json), [SharpProspero](https://github.com/SvenGDK/SharpProspero/blob/5dfdb4b0b166e83dc9ae275001afb695f12c7174/src/SharpProspero/Interop/Kernel/KernelAio.cs) and [ampr_emu](https://github.com/drakmor/ampr_emu/blob/cfa85df379f6eeeb165d7badf9b648e266fe77b7/ps5/include/sce/_fs.h). Whether the console checks the values is unknown, so a null struct, a scheduling window or delayed count limit outside 0..128, a split flag other than 0 or 1 and a split size or chunk size above 0x1000000 (SharpProspero's maximums) throw. The scheduling settings are not used: `sceKernelAioInitializeImpl` ignores the struct and requests run synchronously on submit +- [sceKernelAioSetParam](../../../core/libs/prx/libkernel/Aio/src/Aio.cpp) (libkernel) - the limits and `sceKernelAioInitializeParam`'s defaults (three 20-byte scheduling structs, the last without split) are measured on a PS5 (firmware 13.60), where a null struct crashes both calls; here both throw. The scheduling settings are not used: `sceKernelAioInitializeImpl` ignores the struct and requests run synchronously on submit - [sceKernelGetFsSandboxRandomWord](../../../core/libs/prx/libkernel/System/src/Sandbox.cpp) (libkernel) returns a fixed directory name, `gkbHtXc3Wq`. On the console the name is the process's randomized path (syscall 602, `randomized_path`), and the sandbox exposes system files under it, such as `common/lib` and `common/cert`. Nothing is mounted under the name here, so `/gkbHtXc3Wq/common/...` resolves under the guest root like any other path and does not exist, except `/gkbHtXc3Wq/common/cert/CA_LIST.cer`, which [SandboxCertificates.cpp](../../../core/libs/prx/libkernel/File/src/SandboxCertificates.cpp) serves from the host trust store - [ktimer_create, ktimer_settime, ktimer_gettime, ktimer_getoverrun, ktimer_delete](../../../core/libs/prx/libkernel/Time/Ktimer.cpp) (libkernel) - a `SIGEV_SIGNAL` or `SIGEV_THREAD_ID` timer, and one created with a null `sigevent` (FreeBSD defaults it to `SIGEV_SIGNAL`), throws, because delivering a guest signal from a timer is not modelled. A non-zero value under one tick rounds up to 1 ms, as FreeBSD 11 does at hz 1000; the console's hz is not measured (`kern.clockrate` is unanswered). `SIGEV_NONE` timers on `CLOCK_REALTIME` and `CLOCK_MONOTONIC` follow `kern_time.c`: ids 3 to 31 (`EAGAIN` when full), `EINVAL` for another clock, a bad notify type or signal, an id that is not live or a bad `itimerspec` field, and `TIMER_ABSTIME` (1). Expiry is worked out from the guest clock when the timer is read, and `ktimer_getoverrun` returns 0, as FreeBSD only updates it when a signal is accepted - [The sandbox CA list](../../../core/libs/prx/libkernel/File/src/SandboxCertificates.cpp) (libkernel), `//common/cert/CA_LIST.cer`, is a PEM bundle of the host's trust anchors, written once per process to `cert_cache/CA_LIST.cer` in the run directory with the owner write bit cleared: on Windows the current user's `ROOT` store, filtered through Disallowed and the combined EKU extension/property for TLS server authentication (Windows fetches missing roots on demand, so a root that was never fetched is absent), elsewhere `SSL_CERT_FILE` or the first of `/etc/ssl/certs/ca-certificates.crt`, `/etc/pki/tls/certs/ca-bundle.crt`, `/etc/ssl/ca-bundle.pem` and `/etc/ssl/cert.pem` that exists. On macOS, `/etc/ssl/cert.pem` is a static snapshot and does not reflect roots added to or distrusted in the Keychain. Existing guest files, explicit aliases and blocked mounts take precedence over the provider. The PEM snapshot does not carry Windows CTL restrictions beyond EKU, native revocation policy or later trust-store changes. Without any certificate the path does not exist. The console file's format is unverified: only that bundled OpenSSL accepts a PEM bundle there is shown (ProsperoRadio PPSA99001 and SwanStation PPSA98510, #5001). The other entries of the console's `common/cert` directory, the directory itself (it does not exist here) and the error a write open gets (`EACCES` from the host here) are unverified @@ -164,7 +174,7 @@ - [scePthreadAttrGet](../../../core/libs/prx/libkernel/Pthread/src/Attr.cpp) (libkernel) - the answer for a thread that has ended is not measured on the console, and [`pthread_attr_get_np`](../../../core/libs/prx/libkernel/Pthread/Posix/Attr.cpp) still returns 0 for such a thread, reading a detached one's object after it is freed. `scePthreadAttrGet` returns `ESRCH` (`0x80020003`) for a thread that has ended, joined or not and detached or not, as FreeBSD 11.4's [`_pthread_attr_get_np`](https://github.com/freebsd/freebsd-src/blob/releng/11.4/lib/libthr/thread/thr_attr.c#L139-L172) does through `_thr_find_thread`; a thread counts as ended once its thread-specific data destructors have run, where FreeBSD marks it `PS_DEAD`. On Windows a host thread adopted by `scePthreadSelf` that ends without `scePthreadExit` keeps answering 0. Monster Hunter Wilds (PPSA24191) reuses a slot of its thread table only when `scePthreadAttrGet` returns `0x80020003` - [scePthreadAttrGet, pthread_attr_get_np and sceKernelIsStack](../../../core/libs/prx/libkernel/Pthread/src/Thread.cpp) (libkernel) report the stack of a host thread the library did not create, on Windows, from one allocation granule (64 KiB) above the bottom of its reservation when everything above that is committed read-write, as in a relinked executable's main thread and its default-size host threads (0x200000 committed of 0x210000 reserved), so the guest can protect the lowest reported pages, as HotSpot does for its stack guard pages in Java 25 LAN / DNS (PPSA99869). Any other such thread, and every such thread on Linux, gets its whole host stack: on Windows `mprotect` commits its reserved lower pages before protecting them and drops the guard flag of Windows' guard pages, and on Linux it cannot change the unmapped lower pages - [sceKernelInstallExceptionHandler](../../../core/libs/prx/libkernel/System/src/Exception.cpp) records the handler, but a host fault still ends the process: only signals raised with `sceKernelRaiseException` are delivered. Unity's PS5Util module installs a SIGUSR1 handler that reads `uc_mcontext.mc_rsp` (offset 0xf8 of the context) and raises SIGUSR1 on other threads to stop them for garbage collection -- [sceKernelRaiseException](../../../core/libs/prx/libkernel/System/src/Exception.cpp) on Linux sends the host SIGUSR1, which the library takes over, to the target thread, so the handler runs wherever the thread was interrupted, emulator code included. On the console kernel objects are system calls and libthr defers signals while a thread holds its internal locks; here a handler that needs a host lock the interrupted code holds deadlocks, and a thread that the handler keeps waiting keeps its host locks. Host waits resume after the handler. The host calls Linux does not restart end early: sleeps that do not retry return before their time, and the poll of `sceNetEpollWait` and socket calls with a send or receive timeout return `EINTR` to the guest +- [sceKernelRaiseException](../../../core/libs/prx/libkernel/System/src/Exception.cpp) on Linux sends the host SIGUSR1, which the library takes over, to the target thread, so the handler runs wherever the thread was interrupted, emulator code included. On the console kernel objects are system calls and libthr defers signals while a thread holds its internal locks; here a handler that needs a host lock the interrupted code holds deadlocks, and a thread that the handler keeps waiting keeps its host locks. A thread holding the address-wait lock (`sceKernelSyncOnAddressWait`, `sceKernelSyncOnAddressWake`, `_umtx_op`) records the signal and runs the handler once it releases the lock or while it waits; other emulator locks are not deferred. Host waits resume after the handler. The host calls Linux does not restart end early: sleeps that do not retry return before their time, and the poll of `sceNetEpollWait` and socket calls with a send or receive timeout return `EINTR` to the guest - [sceKernelRaiseException](../../../core/libs/prx/libkernel/System/src/Exception.cpp) on Windows suspends the target and redirects it to the handler. A target running host code (any module other than the executable and the `*.guest.prx` modules, so libkernel, libc and the other HLE modules count too) is resumed and sampled again until it is back in guest code, since a handler that waits there would keep the host locks that code holds. The raising thread spins for 20 µs before each new host-code sample: suspended again at once, the target could stay at the same instruction for every sample until the 50 ms below ran out (on a 16-core Ryzen 9 5950X, `guest_raise_in_host_code` then raised into `fwrite` and deadlocked on its stream lock), and the 20 µs is a measured choice, not a console value; a target blocked in an ntdll wait is redirected in place, as are NtContinue and winpthreads after their retry limit. A target that stays in host code without waiting for 50 ms after the raise began, such as a long HLE or driver call, is then redirected in host code anyway, with the same hazard. A target seen right after a `syscall` instruction in ntdll, inside a system call that is not a wait such as the `NtWriteFile` of an `fwrite` holding its stream lock, restarts those 50 ms, so a raise can cost the raising thread up to the 1 s retry limit. While the target is suspended only `VirtualQuery` and a lock-free cache of image bases are consulted; an image not yet in the cache is classified with `GetModuleFileNameW` after the target is resumed. An APC queued at the first host-code sample, which an alertable guest wait would run sooner, is not implemented - [sceKernelRaiseException](../../../core/libs/prx/libkernel/System/src/Exception.cpp) builds the handler's `ucontext` with FreeBSD's amd64 layout (16-byte sigmask, `mcontext` at 0x40, `mc_rsp` at 0xf8); the console's layout is not verified beyond the fields PS5Util reads - [sceKernelRaiseException](../../../core/libs/prx/libkernel/System/src/Exception.cpp) does not consult the target thread's signal mask @@ -172,13 +182,14 @@ - [sceKernelRaiseException](../../../core/libs/prx/libkernel/System/src/Exception.cpp) reaches a thread that waits in an alertable `TimedWait` wait through an APC. A thread blocked anywhere else, including all waits under `APS5_COARSE_TIMED_WAITS` and host waits inside the emulator, is redirected and runs the handler only when that wait returns - [sceKernelRaiseException](../../../core/libs/prx/libkernel/System/src/Exception.cpp) on Windows runs the handler wherever the target thread was interrupted. It waits only while the thread is inside winpthreads or restoring a context through `NtContinue`, and throws if that lasts 1 s, so a handler that calls into other host code the thread was interrupted in, such as the C runtime's heap or a lock of the emulator, can deadlock on it or reenter it. A thread that holds the address-wait lock of `sceKernelSyncOnAddressWait`, `sceKernelSyncOnAddressWake` and `_umtx_op`, or the park queue lock of the guest mutexes, gets the raise as an APC instead, which runs once it has released the lock or while it waits; the other locks of the emulator are not deferred this way - [sigaltstack](../../../core/libs/prx/libkernel/System/src/Signals.cpp) (libkernel) records each thread's alternate signal stack and reports it back, following FreeBSD's `kern_sigaltstack` (`EINVAL` for flags other than `SS_DISABLE`, `ENOMEM` below `MINSIGSTKSZ` 2048), but handlers never run on it, so `SS_ONSTACK` and `EPERM` are never reported -- [sigpending](../../../core/libs/prx/libkernel/System/src/Signals.cpp) (libkernel) reports the intersection of the process-wide pending set of `sigtimedwait` and the blocked mask of `_sigprocmask`, as FreeBSD's `sys_sigpending` intersects the pending list with `td_sigmask`, and returns -1 with `EFAULT` for a null set. Because `_sigprocmask` keeps only the first 32 signals, signals 33 to 128 are never blocked and never appear in the set, and setting `SIG_IGN` (or `SIG_DFL` on a signal whose default action is to ignore) does not clear a signal that was already marked pending. PPSA99420 imports it -- [scePthreadJoin, scePthreadCreate](../../../core/libs/prx/libkernel/Pthread/src/Thread.cpp) (libkernel) - on a PS5 (firmware 13.60) joining a thread a second time returns `ESRCH` (`0x80020003`), as it does here, but a joined thread is kept until `scePthreadCreate` publishes over the same handle storage, so one host object and its handle are retained per distinct guest handle storage location for the lifetime of the process; a guest that joins into a fresh storage location every time keeps every one of them. A null entry crashed the payload and still throws. Joining the calling thread returns `EDEADLK` and a zeroed attribute creates a default thread, as on the console. `scePthreadDetach` on a joined thread answers `EINVAL`, as FreeBSD does on a thread that is not joinable; the console answer for a detach after a join is unverified. Detaching a thread while another thread waits to join it returns `EINVAL`, and a second concurrent join returns `EOPNOTSUPP`, as FreeBSD 11.4's [`thr_detach.c`](https://github.com/freebsd/freebsd-src/blob/releng/11.4/lib/libthr/thread/thr_detach.c) and [`thr_join.c`](https://github.com/freebsd/freebsd-src/blob/releng/11.4/lib/libthr/thread/thr_join.c) check `joiner`; before this PPSA34015 stopped once with `Joining guest thread: Access is denied` when a thread it was joining was released under the join, and which of the two calls released it was not captured. A detached thread is still released when it ends, so joining it after it ended reads freed memory. A handle value copied to a second storage and joined there retains the object under the original storage key, so a later `scePthreadCreate` over that original storage releases an object the copy still points at, and the copy reads freed memory +- [sigpending](../../../core/libs/prx/libkernel/System/src/Signals.cpp) (libkernel) reports the intersection of the process-wide pending set of `sigtimedwait` and the blocked mask of `_sigprocmask`, as FreeBSD's `sys_sigpending` intersects the pending list with `td_sigmask`, and returns -1 with `EFAULT` for a null set. PPSA99420 imports it +- [scePthreadJoin, scePthreadCreate](../../../core/libs/prx/libkernel/Pthread/src/Thread.cpp) (libkernel) - a joined thread is kept until `scePthreadCreate` publishes over the same handle storage, so one host object and its handle are retained per distinct guest storage location for the life of the process; a guest that joins into a fresh location every time keeps every one. A handle copied to a second storage and joined there retains the object under the original storage key, so a later `scePthreadCreate` over the original storage releases an object the copy still points at, and the copy reads freed memory. A detached thread is released when it ends, so joining it after it ended reads freed memory. A null entry crashed the payload and throws. Joining the calling thread returns `EDEADLK` and a zeroed attribute creates a default thread, as on the console. PPSA34015 once stopped with `Joining guest thread: Access is denied` before the join and detach checks followed the console +- [scePthreadCreate and scePthreadDetach, measured](../../../core/libs/prx/libkernel/Pthread/src/Thread.cpp) (libkernel) - as in a payload process on a PS5 (firmware 13.60): a name of 32 bytes or more returns `ENAMETOOLONG` (`0x8002003f`) without creating a thread, 31 is kept whole, and a null name gives an empty one; detaching a joined thread returns `ESRCH`; detaching a thread another thread is joining, or a detached one, returns `EINVAL`, as does joining a detached one; a second concurrent join returns `EOPNOTSUPP`; a null thread returns `EINVAL` from both; a null handle pointer to `scePthreadCreate` faulted and throws here. Whether the handle is written before the name check was not measured; here it is left alone - [Thread cancellation](../../../core/libs/prx/libkernel/Pthread/src/Thread.cpp) (libkernel) follows FreeBSD libthr (states and types 0/1 and 0/2, `PTHREAD_CANCELED` 1, the `_pthread_cleanup_info` node layout), not measured on the console. A request is acted on only in `scePthreadTestcancel`, the condition variable, `sem` and `scePthreadSem` waits, `scePthreadJoin` and the sleeps, and their POSIX names; file and socket calls are not cancellation points. An enabled cancellation request wakes POSIX `sleep` and exits the thread. `usleep`, `sceKernelUsleep`, `nanosleep`, `_nanosleep`, `sceKernelNanosleep` and `sceKernelSleep` check cancellation only before and after the host sleep, so an enabled request during those waits is delivered when the sleep ends. The asynchronous type acts like the deferred one, except at the two set calls. - [cpuset_setaffinity](../../../core/libs/prx/libkernel/Pthread/Posix/Thread.cpp) (libkernel) - the calling thread, another live guest thread selected by its guest TID, or the calling process is handled; other levels (ROOT/CPUSET) and other scopes (CPUSET/JAIL) throw as unmodelled, invalid levels or scopes return `EINVAL` and unknown thread or process IDs return `ESRCH`, following [FreeBSD 11.4](https://github.com/freebsd/freebsd-src/blob/d106002967eff712894ab8b5337bbbff106ce5e0/sys/kern/kern_cpuset.c#L1281-L1286) thread selection. Bits outside `sceKernelGetAvailableCpumask` are dropped, and a mask with none of those bits fails with `EDEADLK`, as FreeBSD does when the new set has no CPU in common with the parent set. Guest-TID lookup and the update hold the thread registry lock so a join cannot release the target during access. Affinity is recorded for guest CPU reporting and child inheritance, not applied to host scheduling; PS5 behaviour has not been measured - [cpuset_getaffinity](../../../core/libs/prx/libkernel/Pthread/Posix/Thread.cpp) (libkernel) - the calling thread, another live guest thread selected by its guest TID, or the calling process is handled; other levels (ROOT/CPUSET) and other scopes (CPUSET/JAIL) throw as unmodelled, invalid levels or scopes return `EINVAL` and unknown thread or process IDs return `ESRCH`, following [FreeBSD 11.4](https://github.com/freebsd/freebsd-src/blob/d106002967eff712894ab8b5337bbbff106ce5e0/sys/kern/kern_cpuset.c#L426-L437) guest-TID selection, not measured on PS5. For the process the mask is the union of the affinities of the live guest threads, as [FreeBSD 11.4](https://github.com/freebsd/freebsd-src/blob/d106002967eff712894ab8b5337bbbff106ce5e0/sys/kern/kern_cpuset.c#L1150-L1156) ORs the mask of every thread in the process; threads the guest did not create through libkernel are not included, and the console result is unverified - [pthread_setaffinity_np](../../../core/libs/prx/libkernel/Pthread/Posix/Thread.cpp) (libkernel) - follows [FreeBSD 11.4](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/lib/libthr/thread/thr_affinity.c#L41-L62) libthr over `kern_cpuset_setaffinity` for the calling thread and any live guest thread: a size other than the 32-byte `cpuset_t` returns `ERANGE`, an unknown or finished thread `ESRCH`. Bits outside `sceKernelGetAvailableCpumask` are dropped and a mask with none of those bits returns `EDEADLK`, as `cpuset_setaffinity` does; the affinity is recorded on the guest thread and not applied to the host thread, and the console result is unverified -- [dup and dup2](../../../core/libs/prx/libkernel/File/src/Stdio.cpp) (libkernel) throw for the combinations they do not model: `dup2` of a guest socket onto descriptors 0 to 2, whose host streams libc writes without the socket table, and on Windows duplicating a directory descriptor. Guest sockets share one socket object between the duplicates; the numbers come from the host descriptor table like those of files. The errno values for descriptor exhaustion are not verified against the console. On Windows a guest `dup2` onto descriptor 1 or 2 keeps host logging on an `_fdopen` of a duplicate, and the guest `FILE` stays on the descriptor that was replaced; opening that duplicate throws (#7791) +- [dup and dup2](../../../core/libs/prx/libkernel/File/src/Stdio.cpp) (libkernel) throw for the combinations they do not model: `dup2` of a guest socket onto descriptors 0 to 2, whose host streams libc writes without the socket table. Guest sockets share one socket object between the duplicates; the numbers come from the host descriptor table like those of files. The errno values for descriptor exhaustion are not verified against the console. On Windows a guest `dup2` onto descriptor 1 or 2 keeps host logging on an `_fdopen` of a duplicate, and the guest `FILE` stays on the descriptor that was replaced; opening that duplicate throws (#7791) - [waitpid](../../../core/libs/prx/libkernel/System/src/Process.cpp) (libkernel) returns -1 with `ECHILD` (`EINVAL` for unknown options): nothing here creates a child process; child processes would need a real `waitpid`. - [execv](../../../core/libs/prx/libkernel/System/src/Process.cpp) (libkernel) throws: replacing the process image is not supported, the same as `execvp`. PPSA97358 imports it and fails to load without it. - [execve](../../../core/libs/prx/libkernel/System/src/Process.cpp) (libkernel) throws like `execvp`: replacing the process image is not supported. The console behavior for invalid arguments and executable path lookups is not modeled. @@ -191,6 +202,7 @@ - [hw.pagesize](../../../core/libs/prx/libkernel/System/src/SystemConfiguration.cpp) (libkernel) reports the system page size (16384 bytes, PS5_PAGE_SIZE) as an integer through sysctl MIB {6, 7} (CTL_HW, HW_PAGESIZE) and the "hw.pagesize" sysctlbyname lookup, following FreeBSD's [`hw.pagesize`](https://github.com/freebsd/freebsd-src/blob/main/sys/kern/kern_mib.c#L177), a read-only `SYSCTL_INT` of `PAGE_SIZE`. The value on a PS5 follows the 16 KiB page size the rest of libkernel uses and is unmeasured. A write returns -1 with EPERM. - [hw.model](../../../core/libs/prx/libkernel/System/src/SystemConfiguration.cpp) (libkernel) reports the [host CPU's name](../../../core/libs/prx/libkernel/System/src/HostCpu.cpp), built as FreeBSD 11 builds `hw.model`: the CPUID brand string (leaves 0x80000002 to 0x80000004) without leading spaces ([identcpu.c](https://github.com/freebsd/freebsd-src/blob/release/11.0.0/sys/x86/x86/identcpu.c#L683-L691)). Trailing padding stays, as it does there, so an AMD host's name ends in spaces. The name a PS5 reports is unknown. A write returns -1 with `EPERM`. A host without an x86 CPUID brand string, such as an Apple Silicon Mac, throws. PPSA99203 only prints it (`[console] hw.model: %s`) - [Host CPU check](../../../core/libs/prx/libkernel/System/src/HostCpu.cpp) (libkernel) - when libkernel loads on a Linux or Windows x86-64 host, it logs which of the PS5 CPU's user-mode extensions CPUID reports and throws, naming the host CPU, if any extension is missing that LLVM selects from plain C/C++ for the PS5's Zen 2 (`-march=znver2`, [feature list](https://github.com/llvm/llvm-project/blob/llvmorg-19.1.7/llvm/lib/Target/X86/X86.td#L1459-L1513)): SSE to SSE4.2, SSSE3, POPCNT, LZCNT, BMI1, BMI2, MOVBE, CMPXCHG16B, LAHF/SAHF, AVX, AVX2, FMA, F16C, and the operating system's AVX state in XCR0 (#3719). SSE4A, CLZERO and SHA, which `--to-intel` converts, and AES, PCLMULQDQ, RDRAND, RDSEED, ADX, CLFLUSHOPT, CLWB, RDPID, XSAVEOPT, XSAVEC and PREFETCHW, which compilers emit only for intrinsics or explicit prefetches, are logged but not required: a title that executes one of them on a host without it still stops with an illegal instruction. macOS hosts are not checked: some Rosetta versions execute extensions without reporting them through CPUID, but support differs by host. ANDN and RDRAND fault on an M1 with macOS 15.3.1; see the [Rosetta conversion limits](relinker.md) +- [sceKernelGetSystemExVersion, sysKernelGetLowerLimitSysexVersion, sysKernelGetLowerLimitUpdVersion](../../../core/libs/prx/libkernel/System/src/SystemConfiguration.cpp) (libkernel) and [sceKernelGetTraceMemoryStats](../../../core/libs/prx/libkernel/DirectMemory/Export.cpp) (libkernel) - follow libkernel.sprx, libkernel_sys.sprx and libkernel_web.sprx of firmware 13.42, read from disassembly and not measured on a console. None of them asks the kernel: the first writes an empty string to both outputs and returns a plain 22, not `SCE_KERNEL_ERROR_EINVAL`, when either is null; the lower limits write an empty string and a 32-bit 0; the trace memory statistics write 0 to each output that is not null. The meaning of the outputs is not known - [machdep.tsc_freq](../../../core/libs/prx/libkernel/System/src/SystemConfiguration.cpp) (libkernel) reports the host TSC frequency that `sceKernelGetTscFrequency` measures, as a 64-bit value like FreeBSD 11's [`sysctl_machdep_tsc_freq`](https://github.com/freebsd/freebsd-src/blob/release/11.0.0/sys/x86/x86/tsc.c#L658-L676). FreeBSD lets root write it to change the TSC timecounter's frequency; a write here returns -1 with `EPERM`. PPSA99203 reads it at start-up and measures the TSC itself when the value is 100 MHz or less - [getrlimit](../../../core/libs/prx/libkernel/System/src/Process.cpp) (libkernel) - `RLIMIT_CPU`, `RLIMIT_FSIZE`, `RLIMIT_CORE`, `RLIMIT_SBSIZE`, `RLIMIT_VMEM`/`AS`, `RLIMIT_NPTS`, `RLIMIT_SWAP`, `RLIMIT_KQUEUES` and `RLIMIT_UMTXP` report infinity as in FreeBSD's process-0 defaults, and `RLIMIT_DATA` mirrors the host limit; a payload process on a PS5 (firmware 12.70) reads 0x800000000 for both limits. `RLIMIT_NOFILE` reports, as both soft and hard limit, the number of descriptors after which AnyPS5 itself fails with `EMFILE`: on Windows the C runtime's 8192 low-level descriptors, which guest files, pipes and sockets all take, and as many stdio streams, which libkernel raises from 512 at load with [`_setmaxstdio`](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/setmaxstdio), on Linux the host's `RLIMIT_NOFILE`, whose soft limit libkernel raises to the hard limit at load, and on macOS the smaller of the host's hard limit and `kern.maxfilesperproc`, which libkernel sets as both limits at load, since macOS fails with `EMFILE` at `kern.maxfilesperproc` whatever the soft limit. A payload process on a PS5 (firmware 12.70) reads 13952 for both limits, while `getdtablesize` returns 12555 and the process can open descriptors up to 12554. XPSemu (PPSA97358) reads `RLIMIT_NOFILE` in QEMU's `os_setup_limits` and raises the soft limit to the hard one when they differ. `RLIMIT_STACK` (0x200000), `RLIMIT_RSS` and `RLIMIT_MEMLOCK` (infinity) and `RLIMIT_NPROC` (1604) report the soft and hard limits a payload process reads on a PS5 (firmware 12.70). Guest locking still follows the host: `mlock` and `sceKernelMlock` fail at the host's hard `RLIMIT_MEMLOCK` on Linux, and on Windows when the process working set cannot grow, although `RLIMIT_MEMLOCK` reports infinity. There the main thread's stack is 0x200000 as well (`pthread_attr_get_np`); here the main thread runs on the host thread's stack, whose size can differ. Whether a title process reads the same limits is unverified. - [setrlimit](../../../core/libs/prx/libkernel/System/src/Process.cpp) (libkernel) validates the resource and input in FreeBSD order, maps negative limits to infinity, and lowers a soft limit above the hard limit. A request other than `RLIMIT_NPROC` succeeds when its normalized values match `getrlimit`, or, for `RLIMIT_NOFILE`, when `rlim_max` does not exceed the current hard limit and `rlim_cur` does not exceed `rlim_max`, storing the updated limits for subsequent `getrlimit` queries; descriptor allocation enforcement of the lowered soft limit is not implemented, and changing other limits throws because guest limit state is not stored. In a payload process on a PS5 (firmware 12.70), `setrlimit(RLIMIT_NOFILE)` lowers a requested 13952 to 12555, as the hard and as the soft limit; here a request within the current hard limit is stored as given and one above it throws; setting `RLIMIT_STACK`, `RLIMIT_RSS` and `RLIMIT_MEMLOCK` to their current values succeeds and changes nothing, as here; setting `RLIMIT_NPROC` to its current 1604 also succeeds but leaves 1443. `RLIMIT_NPROC` follows FreeBSD 11 by clamping both values to 1 through `maxprocperuid`, stores the result and returns it from `getrlimit`. The initial 1604 and `maxprocperuid` 1443 follow that measurement: process 0's limit starts at `maxproc` ([`init_main.c`](https://github.com/freebsd/freebsd-src/blob/release/11.0.0/sys/kern/init_main.c#L550-L551)), `maxprocperuid` starts at nine tenths of it ([`subr_param.c`](https://github.com/freebsd/freebsd-src/blob/release/11.0.0/sys/kern/subr_param.c#L258)), and `setrlimit` applies that bound ([`kern_resource.c`](https://github.com/freebsd/freebsd-src/blob/release/11.0.0/sys/kern/kern_resource.c#L714-L718)). `kern.maxproc` fails with `ENOENT` in the payload process, so the values cannot be queried independently, and process creation does not enforce the stored limit. Java 25 LAN / DNS (PPSA99869) sets `rlim_cur = MIN(0x100000, rlim_max)` during VM initialization @@ -198,39 +210,48 @@ - [nmount](../../../core/libs/prx/libkernel/File/src/Stdio.cpp) (libkernel) throws: mounting file systems is not supported. Prospero Themes (PPSA90210) imports it and fails to load without it; it opens its theme library without calling it, and its strings (`/dev/ssd0.system_ex`, `/system_ex`, `exfatfs`, `fstype`, `fspath`, `from`, `ignoreacl`) point to remounting the console's `/system_ex` partition to write theme files there. - [socketpair](../../../core/libs/prx/libkernel/Socket/src/Udp.cpp) (libkernel) - the pair is the host's `AF_UNIX` pair on Linux and macOS and, on Windows, a loopback TCP connection for `SOCK_STREAM` or two connected UDP sockets for `SOCK_DGRAM`; `SOCK_SEQPACKET` throws on Windows and macOS, which has no such `AF_UNIX` pair. A datagram sent to a full receiver gets `ENOBUFS` on the console, waits or gets `EAGAIN` on Linux and is dropped on Windows, and on Windows a datagram `recv` after the peer closed reports `ECONNRESET` instead of the console's `ENOTCONN`, and the first stream `send` after the peer closed succeeds on the loopback connection, where the console returns `EPIPE`; the sends after it report `EPIPE`. `read` and `write` on a pair end go to the host like on every guest socket. `getpeername` on a pair end succeeds after the peer closed, where the console returns `ENOTCONN`, and `connect`, `bind` and `sendto` with an address on a pair end report an address-family error instead of the console's `EISCONN`. On Windows `poll` on a stream end reports `POLLIN` after the peer's `shutdown(SHUT_WR)` or `close`, as the console does after a `shutdown(SHUT_WR)`; after the peer's `close` the console reports `POLLIN` with `POLLHUP`, because `unp_drop` disconnects the other end, and the loopback connection cannot tell that from a shutdown, so no `POLLHUP` is reported - Guest sockets on macOS ([the equeue wait, kevent, fstat and ioctl](../../../core/libs/prx/libkernel/Socket/src/Udp.cpp), [gethostbyname](../../../core/libs/prx/libScePosixForWebKit/Resolver.cpp)): macOS has no `ppoll`, so the wait uses `poll` with the timeout rounded up to a millisecond; it has no `POLLRDHUP`, so a stream that the peer closed is found by peeking one byte when it is readable; `FIONREAD` on a datagram socket, and the byte count that `fstat` and `kevent` report for it, is `SO_NREAD`, the size of the next datagram, because macOS adds the source address to `FIONREAD`; and `gethostbyname` replaces `gethostbyname_r`, which macOS lacks. -- [UDP recv/recvfrom](../../../core/libs/prx/libkernel/Socket/src/Udp.cpp) and [sceNetRecv/sceNetRecvfrom](../../../core/libs/prx/libSceNet/Export.cpp) translate Windows WSAEMSGSIZE into the copied byte count for datagram sockets, including MSG_PEEK, and retain the sender address. This follows FreeBSD 11.4 recv(2) and Winsock's documented truncated-datagram behavior; console behavior and existing title impact are unverified. - [recvmsg](../../../core/libs/prx/libkernel/Socket/src/Udp.cpp) (libkernel) loses received stream data on an output copy fault: outputs are checked after the host receive. FreeBSD 11.4 leaves unread stream data queued, though it also drops a datagram on a copy fault ([`soreceive_dgram`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_socket.c#L2308-L2312), [`soreceive_generic`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_socket.c#L1758-L1774)). A Linux `process_vm_readv` or `process_vm_writev` failure other than `EFAULT` throws - [MSG_DONTWAIT](../../../core/libs/prx/libkernel/Socket/src/Udp.cpp) (libkernel) in `send`, `sendto`, `sendmsg`, `recv`, `recvfrom` and `recvmsg` makes that one call non-blocking through the host's `MSG_DONTWAIT`, as FreeBSD 11.4's `soreceive_generic` returns `EWOULDBLOCK` for it (`sys/kern/uipc_socket.c`). Winsock has no per-call non-blocking flag, so on Windows the call first asks `WSAPoll` with a zero timeout whether the socket is ready and fails with `EAGAIN` when it is not. The check and the transfer are two host calls: a second thread that takes the data in between can leave a `recv` blocked, and a `send` of more bytes than the socket buffer holds can still block until it has sent them, where FreeBSD's `sosend_generic` returns a partial count - [MSG_WAITALL](../../../core/libs/prx/libkernel/Socket/src/Udp.cpp) (libkernel) in `recv`, `recvfrom` and `recvmsg` on a stream socket is passed to the host, which returns when the buffer is full, the peer closes or an error occurs, as FreeBSD 11.4's [`soreceive_generic`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_socket.c#L1855-L1860) does; a datagram socket ignores it. It is not measured on a console, a nonblocking socket and a call that also passes `MSG_PEEK` follow the host's rules, and `SO_RCVTIMEO` bounds only the wait for the first data. Any other flag still fails with `EOPNOTSUPP`. -- [recv and recvfrom](../../../core/libs/prx/libkernel/Socket/src/Udp.cpp) (libkernel) answer without the host a zero-length receive on a stream socket (0, or `ENOTCONN` when it is not connected, [L1566-L1578](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_socket.c#L1566-L1578)) and, on Windows only, a receive on a socket whose read side `shutdown` closed (0, as [`soreceive_generic`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_socket.c#L1556-L1562) returns at `SBS_CANTRCVMORE`); Winsock fails the second with `EPIPE` and blocks on the first. Linux and macOS still return data buffered before the `shutdown`, where FreeBSD discards it. `recvmsg` still asks the host in both cases. Not measured on a console. +- [recv and recvfrom](../../../core/libs/prx/libkernel/Socket/src/Udp.cpp) (libkernel) answer some cases without the host. A zero-length receive on a stream socket returns 0, or `ENOTCONN` when it is not connected ([L1566-L1578](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_socket.c#L1566-L1578)), as on a PS5 (firmware 13.60). On an empty datagram socket it returns 0 at once; the console was measured only with `MSG_DONTWAIT`, and the blocking case follows FreeBSD 11.4's `soreceive_dgram`, which returns 0 before waiting. `recvmsg` answers the empty datagram socket the same way but asks the host in the stream cases. A datagram another thread takes after the check makes a zero-length receive wait +- [recv and recvfrom after `shutdown(SHUT_RD)`](../../../core/libs/prx/libkernel/Socket/src/Udp.cpp) (libkernel) return 0 on Windows ([`soreceive_generic`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_socket.c#L1556-L1562)) where Winsock fails with `EPIPE`; Linux and macOS still return data buffered before it, which the console discards. - [send, sendto](../../../core/libs/prx/libkernel/Socket/src/Udp.cpp) (libkernel) - send and sendto without an address on an unconnected datagram socket map Windows `WSAENOTCONN` to FreeBSD's `EDESTADDRREQ` (39). - [bind, connect, sendto](../../../core/libs/prx/libkernel/Socket/src/Udp.cpp) (libkernel) check a socket address length as FreeBSD 11.4's [`getsockaddr`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_syscalls.c) and the `sa_len` test of `tcp_usr_connect` and `in_pcbconnect` do: below 2 or an `AF_INET` address below 16 or an `AF_INET6` address below 28 bytes fails with `EINVAL`, above 255 with `ENAMETOOLONG`. `sendto` does not check an address longer than `sockaddr_in` on an `AF_INET` datagram socket, where `udp_output` answers `EINVAL` unless the socket has a local address and port and the destination is neither `INADDR_ANY` nor `INADDR_BROADCAST`, nor on an unconnected stream socket, which FreeBSD connects implicitly through `tcp_connect` and its length test. An address of another family still fails with `EAFNOSUPPORT`, as the console's behaviour for it is not known. None of this was measured on a console. - [read, write and vector I/O](../../../core/libs/prx/libkernel/File/src/Stdio.cpp) (libkernel) on a guest socket use `recv` and `send` without flags; `sceKernelReadv` and `sceKernelWritev` gather or scatter through one socket operation, preserving stream short reads and datagram boundaries. The positioned `pread`, `pwrite`, `preadv` and `pwritev` forms, and their `sceKernel` forms, return `ESPIPE` for an open guest socket and `EBADF` for a closed one. `EPIPE` from a write raises the host's `SIGPIPE` on Linux unless the socket has `SO_NOSIGPIPE` set, and the guest cannot ignore it: `signal(SIGPIPE, SIG_IGN)` and `sigaction` record the disposition, as they do for every number from 1 to 128, but only the six signals with host equivalents (2, 4, 6, 8, 11 and 15) take effect on the host or are delivered, so the host `SIGPIPE` is not stopped; nothing is raised on Windows; a `write` longer than `INT_MAX` bytes is `EMSGSIZE` where the console sends it. AIO on a guest socket still goes to the host, which has no such descriptor - [Socket descriptor numbers](../../../core/libs/prx/libkernel/Socket/src/Udp.cpp) (libkernel, libSceNet) - `socket`, `accept`, `socketpair`, `dup`, `sceNetSocket` and `sceNetAccept` take the lowest free number of the host descriptor table that files use, as FreeBSD 11.4 `fdalloc` takes the lowest free entry of the one process table (`kern_descrip.c`), so every number stays below `FD_SETSIZE` (1024) while fewer than 1024 descriptors are open. The number holds a zero-access `NUL` handle on Windows and a duplicate of the host socket on Linux, and the socket tables map it to the socket. The host table also holds the runtime's own files, so the numbers differ from the console's; the first number `socket` and `sceNetSocket` return on a console and its `getdtablesize` are not measured. Paths that bypass the socket tables see the placeholder: libc `fdopen` on a socket opens it, and libkernel `close` of a libSceNet socket releases the number but leaves the libSceNet socket open. Palworld (PPSA23126), The Mound (PPSA26223) and Dying Light 2 (PPSA02262) set socket bits in a 1024-bit `fd_set` - [fstat on a guest socket](../../../core/libs/prx/libkernel/Socket/src/Udp.cpp) (libkernel) fills the fields FreeBSD 11's soo_stat and pru_sense_null fill and zeroes the rest: S_IFSOCK, the read bits while the receive side is open or data is queued, the write bits while the send side is open, st_size and st_blksize. Only this process's shutdown closes a side here; a peer's shutdown, close or reset does not clear the bits as FreeBSD's socantrcvmore and socantsendmore do. st_size is the host's FIONREAD, which for a Linux datagram socket is the size of the next datagram where FreeBSD counts every queued byte, and st_blksize is the host's SO_SNDBUF, which Linux reports as twice the size that was set. A socketpair end reports st_dev and st_ino 0, where FreeBSD's uipc_sense reports NODEV and a per-socket number -- [poll](../../../core/libs/prx/libkernel/Socket/src/Udp.cpp) (libkernel) waits on guest sockets and pipes, through `WSAPoll` on Windows and `poll` on Linux; the `pollfd` layout and flag values are FreeBSD's. A libSceNet socket joins the same host wait through the native socket libSceNet lends to libkernel. `WSAPoll` rejects `POLLPRI`, so on Windows a socket's `POLLPRI` is asked as `POLLRDBAND` and `POLLIN` as `POLLRDNORM`; Winsock reports urgent data until it is read with `MSG_OOB`, where FreeBSD 11.4's `sopoll_generic` stops once the normal stream passes the mark. Guest `send` and `recv` refuse `MSG_OOB` with `EOPNOTSUPP`, so urgent data can only come from an outside peer and Windows keeps reporting `POLLPRI` after the stream passes the mark. A pipe follows FreeBSD's `pipe_poll`: the read end is readable when data is buffered, either end reports `POLLHUP` once the other end is closed, with the requested `POLLIN` and `POLLRDNORM` bits and without `POLLOUT`, which `pollscan` clears, and the write end is writable when `PIPE_BUF` (512) bytes are free on Windows, or when the host kernel says so on Linux. `POLLOUT` on a read end throws: FreeBSD opens both ends for reading and writing and reports it writable, and host pipes are one-way. Windows pipes cannot be waited on, so while a pipe is polled `poll` checks it with `NtQueryInformationFile` and waits on the sockets in slices of 1 ms. A regular file, a directory or a file read from app0.zar is always ready for `POLLIN`, `POLLRDNORM` and `POLLOUT` and never for `POLLPRI`, `POLLRDBAND` or `POLLWRBAND`, as FreeBSD 11.4's [`vop_stdpoll`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/vfs_default.c#L563-L574) and `vop_nopoll` answer every event `poll` accepts; which of the two the console's filesystems use is not known, and they agree on these events. `/dev/random` and `/dev/urandom` follow FreeBSD 11.4's [`randomdev_poll`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/dev/random/randomdev.c#L274-L285) for a seeded device: a request with `POLLIN` or `POLLRDNORM` gets only those bits back, and any other request gets every requested bit. Open descriptors 0 to 2 follow the console model `select` uses: descriptor 0 never reports an event and descriptors 1 and 2 report `POLLOUT`, whatever the host attaches to them. A closed descriptor reports `POLLNVAL`, and an open descriptor that is neither a guest socket, a libSceNet socket, a pipe, a file, a directory, the random device nor descriptor 0 to 2 (other devices, a kqueue) throws, as do event bits other than `POLLIN`, `POLLPRI`, `POLLOUT`, `POLLRDNORM`, `POLLRDBAND` and `POLLWRBAND` (the `POLLERR`, `POLLHUP` and `POLLNVAL` bits are ignored in `events`), an infinite timeout with no socket or pipe to wait on. A closed socket descriptor reports `POLLNVAL`; the console's limit on the descriptor count is unknown and not checked +- [poll](../../../core/libs/prx/libkernel/Socket/src/Udp.cpp) (libkernel) waits on guest sockets and pipes, through `WSAPoll` on Windows and `poll` on Linux; the `pollfd` layout and flag values are FreeBSD's. A libSceNet socket joins the same host wait through the native socket libSceNet lends to libkernel. `WSAPoll` rejects `POLLPRI`, so on Windows a socket's `POLLPRI` is asked as `POLLRDBAND` and `POLLIN` as `POLLRDNORM`; Winsock reports urgent data until it is read with `MSG_OOB`, where FreeBSD 11.4's `sopoll_generic` stops once the normal stream passes the mark. Guest `send` and `recv` refuse `MSG_OOB` with `EOPNOTSUPP`, so urgent data can only come from an outside peer and Windows keeps reporting `POLLPRI` after the stream passes the mark. A pipe follows FreeBSD's `pipe_poll`: the read end is readable when data is buffered, either end reports `POLLHUP` once the other end is closed, with the requested `POLLIN` and `POLLRDNORM` bits and without `POLLOUT`, which `pollscan` clears, and the write end is writable when `PIPE_BUF` (512) bytes are free on Windows, or when the host kernel says so on Linux. `POLLOUT` on a read end throws: FreeBSD opens both ends for reading and writing and reports it writable, and host pipes are one-way. Windows pipes cannot be waited on, so while a pipe is polled `poll` checks it with `NtQueryInformationFile` and waits on the sockets in slices of 1 ms. A kqueue is ready for `POLLIN` and `POLLRDNORM` while one of its events is triggered and for no other event, as FreeBSD 11.4's [`kqueue_poll`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/kern_event.c#L1900-L1924) answers. A kqueue cannot be waited on either, so on every host `poll` checks it every millisecond and waits on the other descriptors in slices of 1 ms. A kqueue closed during the wait reports `POLLNVAL`, and one whose number was reused is polled again with the rest of the timeout, as FreeBSD 11.4's [`pollrescan`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/sys_generic.c#L1499-L1519) looks the descriptor up again. A close that has already removed the kqueue but not yet closed its host descriptor is seen as a descriptor of unknown kind, which throws. A regular file, a directory or a file read from app0.zar is always ready for `POLLIN`, `POLLRDNORM` and `POLLOUT` and never for `POLLPRI`, `POLLRDBAND` or `POLLWRBAND`, as FreeBSD 11.4's [`vop_stdpoll`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/vfs_default.c#L563-L574) and `vop_nopoll` answer every event `poll` accepts; which of the two the console's filesystems use is not known, and they agree on these events. `/dev/random` and `/dev/urandom` follow FreeBSD 11.4's [`randomdev_poll`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/dev/random/randomdev.c#L274-L285) for a seeded device: a request with `POLLIN` or `POLLRDNORM` gets only those bits back, and any other request gets every requested bit. Open descriptors 0 to 2 follow the console model `select` uses: descriptor 0 never reports an event and descriptors 1 and 2 report `POLLOUT`, whatever the host attaches to them. A closed descriptor reports `POLLNVAL`, and an open descriptor that is neither a guest socket, a libSceNet socket, a pipe, a file, a directory, the random device nor descriptor 0 to 2 (other devices) throws, as do event bits other than `POLLIN`, `POLLPRI`, `POLLOUT`, `POLLRDNORM`, `POLLRDBAND` and `POLLWRBAND` (the `POLLERR`, `POLLHUP` and `POLLNVAL` bits are ignored in `events`), an infinite timeout with no socket, pipe or kqueue to wait on. A closed socket descriptor reports `POLLNVAL`; the console's limit on the descriptor count is unknown and not checked - [poll](../../../core/libs/prx/libkernel/Socket/src/Udp.cpp) (libkernel) on Windows reports a guest stream socket whose peer closed as readable and adds `POLLHUP` only once the socket has shut its own sending side, as FreeBSD 11.4's [`sopoll_generic`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_socket.c#L3080-L3086) does; `WSAPoll` reports `POLLHUP` alone in both cases. A reset or refused connection (`POLLHUP` with `POLLERR`) keeps `WSAPoll`'s flags, a libSceNet socket keeps them too, and a call that asks for no event still returns early on the peer's close with nothing reported. Not measured on a console. - [kevent EV_CLEAR on a socket or a pipe](../../../core/libs/prx/libkernel/Equeue/src/EdgeWatch.cpp) (libkernel) - FreeBSD 11 activates an `EV_CLEAR` knote when `kqueue_register` finds it ready, which every `EV_ADD` or `EV_ENABLE` of an enabled knote does again, and on each `KNOTE` the socket or pipe raises (`sorwakeup`, `sowwakeup`, `pipeselwakeup`); [`kqueue_scan`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/kern_event.c) checks the filter again before reporting, drops a knote that is no longer ready and deactivates a reported one until the next activation. On Linux each such knote registers the host socket or pipe with its own epoll instance in edge-triggered mode (`EPOLLIN | EPOLLRDHUP` for `EVFILT_READ`, `EPOLLOUT` for `EVFILT_WRITE`), and a wakeup of the host descriptor with that mask stands for a `KNOTE`: the knote is then checked with the same readiness test as a level-triggered one, `NOTE_LOWAT` mark included, and a wait blocks on the epoll descriptor instead of the socket, so a socket that stays readable does not wake it. The host's wakeups are not FreeBSD's: FreeBSD raises `sowwakeup` whenever send buffer space is freed, while Linux TCP wakes `EPOLLOUT` waiters only once the socket is writable again after a write found it full (`SOCK_NOSPACE`, [`tcp_check_space`](https://github.com/torvalds/linux/blob/v6.8/net/ipv4/tcp_input.c#L5639)), so a TCP writer that never fills its buffer sees fewer `EVFILT_WRITE` events than on FreeBSD; a host stack may also coalesce data wakeups, so the number of times a busy socket fires can differ. Windows and macOS throw, as the host there has no edge-triggered wait over these descriptors that is wired up; `EV_CLEAR` on the timer, user and vnode filters does not use this and works everywhere. - [kevent on a pipe](../../../core/libs/prx/libkernel/Equeue/src/Kqueue.cpp) (libkernel) - host pipes are one-way, where FreeBSD's [`kern_pipe`](https://github.com/freebsd/freebsd-src/blob/release/11.0.0/sys/kern/sys_pipe.c#L422) opens both ends for reading and writing, so `EVFILT_READ` on the write end and `EVFILT_WRITE` on the read end throw; on macOS `EVFILT_WRITE` on a pipe throws, as the host has no call for a pipe's capacity. `EVFILT_WRITE` reports the free space of the host buffer, not of FreeBSD's 16 KiB one, and on Linux fires when the host says the pipe is writable rather than at `PIPE_BUF` (512) free bytes. Windows pipes cannot be waited on, so a kqueue waiting on one checks it every 1 ms. Otherwise [`filt_piperead`](https://github.com/freebsd/freebsd-src/blob/release/11.0.0/sys/kern/sys_pipe.c#L1787-L1828) and `filt_pipewrite` are followed: `EV_EOF` once the other end closes, `EPIPE` for a write event added after the read end closed, and `NOTE_LOWAT` ignored. On Windows `fcntl` cannot make a pipe write end non-blocking, so the full-pipe and drain case of `EVFILT_WRITE` is tested on Linux only and the `NtQueryInformationFile` write quota is checked there only for the free space, `EV_EOF` and `EPIPE`. - [kevent on a kqueue](../../../core/libs/prx/libkernel/Equeue/src/Kqueue.cpp) (libkernel) - `EVFILT_READ` on a kqueue counts the watched queue's enabled notes that are ready when it is evaluated, where [`filt_kqueue`](https://github.com/freebsd/freebsd-src/blob/5ed7eb0d97ba4436218e810f61bd059acba984c2/sys/kern/kern_event.c#L363-L370) reports `kq_count`, the queued notes: a queued note counts until that kqueue is scanned, even once disabled or no longer ready (a socket or pipe read empty, a user note cleared, a watched kqueue drained), so FreeBSD can report it readable where this reports nothing. A queued `EV_ONESHOT` note is reported there with its last count and deleted; here it is evaluated again and, if the watched queue is empty by then, neither reported nor deleted. `EV_CLEAR` throws, as FreeBSD re-arms it only when the watched kqueue queues a note. A cycle of watches throws; a note on the queue itself is `EINVAL`. Not measured on a console - [Socket error numbers](../../../core/libs/prx/libkernel/Socket/include/SocketError.hpp) (libkernel) - a host socket error becomes the FreeBSD 11 `errno.h` number of the same name; the numbers are not checked on the console. Winsock `WSAESHUTDOWN` becomes `EPIPE`, as a send after `shutdown` does on FreeBSD, and a host error with no FreeBSD socket counterpart becomes `EIO`. On Windows a refused nonblocking `connect` is never reported (`SO_ERROR` is unsupported), so a repeated one keeps reporting `EALREADY` where FreeBSD reports `ECONNREFUSED`. A receive after `shutdown(SHUT_RD)` reports `EPIPE` on Windows where FreeBSD returns 0 -- [getsockopt, setsockopt](../../../core/libs/prx/libkernel/Socket/src/SocketOptions.cpp) (libkernel) on a guest socket handle the SOL_SOCKET level, `TCP_NODELAY` and `IPV6_V6ONLY`, after FreeBSD 11.4's [`sosetopt` and `sogetopt`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_socket.c): `SO_REUSEADDR`, `SO_KEEPALIVE`, `SO_DONTROUTE`, `SO_BROADCAST`, `SO_OOBINLINE` and `SO_LINGER` act on the host socket, and `SO_ERROR`, `SO_TYPE`, `SO_ACCEPTCONN`, `SO_SNDBUF` and `SO_RCVBUF` are read. A boolean option reads back as its own bit, as FreeBSD returns `so_options & option`. Winsock does not clear a socket's pending error when it is read, so on Windows the library reports a pending error once and shows it again only after a new `connect`; a second failure with the same code on the same socket without a `connect` in between would be hidden. `SO_SNDBUF` and `SO_RCVBUF` read the host's value, which is not the size FreeBSD reports (Linux doubles it). `SO_NOSIGPIPE` is kept per socket and reads back as its bit. On Linux a `send`, `sendto`, `sendmsg` or `write` on that socket passes `MSG_NOSIGNAL` to the host, as [`kern_sendit`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/kern/uipc_syscalls.c#L890-L895) and [`soo_write`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/kern/sys_socket.c#L147-L151) leave out the signal. An accepted socket takes it from its listener, as [`sonewconn`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/kern/uipc_socket.c#L603) copies the options. `SO_NBIO` (`0x1200`) is the console's own option, not FreeBSD's. It switches the socket between blocking and non-blocking as `FIONBIO` does and reads back as 0 or 1, as `sceNetSetsockopt` and `sceNetGetsockopt` treat it here; what the console reads back for it is not measured. `TCP_NODELAY` (`IPPROTO_TCP`, 1) is set on the host socket and reads back as `TF_NODELAY` (4), as [`tcp_default_ctloutput`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/netinet/tcp_usrreq.c#L1860-L1861) returns it. The `IPPROTO_TCP` level on a datagram or `AF_UNIX` socket fails with `EINVAL`, as [`ip_ctloutput`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/netinet/ip_output.c#L943-L944) and [`uipc_ctloutput`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/kern/uipc_usrreq.c#L1228-L1229) answer it, and an accepted socket takes `TCP_NODELAY` through the host. `SO_SNDTIMEO` and `SO_RCVTIMEO` follow [`sosetopt`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/kern/uipc_socket.c#L2657-L2689) and [`sogetopt`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/kern/uipc_socket.c#L2846-L2850). A 16-byte `timeval` with a negative field or 1000000 microseconds or more fails with `EDOM`, and more than `INT32_MAX` seconds becomes the longest wait. The value is kept and read back through FreeBSD's 32.32 fixed-point conversion ([`tvtosbt`, `sbttotv`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/sys/time.h#L178-L286)), so half a second reads back as 499999 microseconds and one microsecond as 0. A `recv`, `recvfrom`, `recvmsg`, `read` or `readv`, and a `send`, `sendto`, `sendmsg`, `write` or `writev`, on a blocking socket then waits that long for the host to report the socket readable or writable, and fails with `EWOULDBLOCK`; a call with `MSG_DONTWAIT` does not wait. An accepted socket takes both from its listener ([`sonewconn`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/kern/uipc_socket.c#L629-L630)). The wait covers only the first transfer: once the socket is writable the host send runs without a limit, where FreeBSD stops a send that cannot finish and returns the bytes sent so far, and a receive whose data another thread takes between the wait and the host call blocks again. `sendfile` ignores the send timeout. The host wait has millisecond steps, so a shorter timeout lasts up to a millisecond longer. A null value fails with `EINVAL`, where FreeBSD's [`kern_setsockopt`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/kern/uipc_syscalls.c#L1414-L1415) returns `EFAULT`. `IPV6_V6ONLY` (`IPPROTO_IPV6`, 27) on an `AF_INET6` socket is set on the host socket and reads back as 0 or 1, after FreeBSD's [`ip6_ctloutput`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/netinet6/ip6_output.c#L1716-L1732): a value that is not exactly an `int` fails with `EINVAL` ([L1572](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/netinet6/ip6_output.c#L1572)), as does setting it on a bound socket, which the host refuses too. `socket` turns it on for every `AF_INET6` socket, as FreeBSD's default `net.inet6.ip6.v6only` does, and Java turns it off on the sockets it opens: Java 25 Advanced Network (PPSA99871) stops at `java.net.SocketException: Protocol not available` without it. The `IPPROTO_IPV6` level on an `AF_INET` or `AF_UNIX` socket fails with `EINVAL`, as `ip_ctloutput` and `uipc_ctloutput` answer it. Every other SOL_SOCKET option (`SO_DEBUG`, `SO_USELOOPBACK`, `SO_REUSEPORT` and `SO_TIMESTAMP`, which FreeBSD stores, the low-water marks among them; PPSA99871 sets `SO_REUSEPORT` and carries on when it fails), every other `IPPROTO_TCP` option (`TCP_MAXSEG`, `TCP_KEEPIDLE`, `TCP_KEEPINTVL` and `TCP_KEEPCNT` among them), every other `IPPROTO_IPV6` option ([L1928-L1929](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/netinet6/ip6_output.c#L1928-L1929), [L2158-L2159](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/netinet6/ip6_output.c#L2158-L2159)) and the `IPPROTO_IP` level fail with `ENOPROTOOPT`. None of this was measured on a console. -- [connect to AF_UNSPEC](../../../core/libs/prx/libkernel/Socket/src/Udp.cpp) (libkernel) dissolves a datagram association as FreeBSD 11.4's `udp_disconnect()` does, which resets the local address to the wildcard address and keeps the port. On Linux a socket bound to an explicit port keeps its local address as well (`127.0.0.1:` stays `127.0.0.1:`), because a bound socket cannot be bound again; one bound to port 0 is bound again to the wildcard address with the port it had -- [connect](../../../core/libs/prx/libkernel/Socket/src/Udp.cpp) (libkernel) - `connect` to port 0 on an unconnected `AF_INET` or `AF_INET6` stream or datagram socket fails with `EADDRNOTAVAIL` (49), as FreeBSD 11.4's `in_pcbconnect_setup()` and `in6_pcbconnect()` do. A connected stream socket keeps its host `EISCONN` answer. Known gaps: on an already connected datagram socket `connect` to port 0 keeps the old peer, where FreeBSD's `soconnect()` disconnects first and then fails with 49; `sendto` and `sendmsg` to port 0 on an `AF_INET` datagram socket fail with 49 unless the socket is bound to a specific address and port and the destination is neither `INADDR_ANY` nor `INADDR_BROADCAST`, which is the path where `udp_output()` skips `in_pcbconnect_setup()` and FreeBSD sends to port 0. There nothing can receive on port 0, so the call returns the length without the host, because Linux's `udp_sendmsg` refuses port 0 with `EINVAL`; an `AF_INET6` socket still goes to the host. The answers are taken from the FreeBSD source and are not measured on a console. [sceNetConnect](../../../core/libs/prx/libSceNet/Export.cpp) is not changed -- [msync](../../../core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp) (libkernel) follows FreeBSD 11's `kern_msync` and `vm_map_sync`, not measured on the console: the address is rounded down to the page, a range that wraps is `EINVAL`, as is `MS_ASYNC` with `MS_INVALIDATE`, and a range whose start or any page is not mapped (as `sceKernelVirtualQuery` reports it, reservations included) is `ENOMEM`; a page unmapped from inside a larger mapping still counts as mapped there, since `sceKernelVirtualQuery` keeps reporting the whole mapping. The guest has no file mappings, so a successful call writes nothing back. `MS_INVALIDATE` on a mapped range throws: FreeBSD returns `EBUSY` when the range holds `mlock`ed pages, and locked pages are not tracked +- [getsockopt, setsockopt](../../../core/libs/prx/libkernel/Socket/src/SocketOptions.cpp) (libkernel) - a guest socket has the SOL_SOCKET level, five `IPPROTO_TCP` options, `IPV6_V6ONLY` and seven `IPPROTO_IP` options. `TCP_NOPUSH`, `TCP_NOOPT` and `TCP_KEEPINIT` fail with `ENOPROTOOPT` (42), where a payload process on a PS5 (firmware 13.60) read them as 0, and so does `SO_ACCEPTFILTER` (0x1000), which the console fails with `EINVAL` for a set and a get on a non-listening socket, as FreeBSD 11.4 does; the other options of these levels fail with `ENOPROTOOPT` and were not all measured. `SO_KEEPALIVE`, `SO_DONTROUTE`, `SO_BROADCAST`, `SO_OOBINLINE` and `SO_LINGER` act on the host socket, and a boolean one reads back as its own bit, as there. Winsock does not clear a pending error when `SO_ERROR` reads it, so on Windows a pending error is reported once and again only after a new `connect`; a second failure with the same code without a `connect` in between is hidden +- [SO_DEBUG, SO_REUSEADDR, SO_USELOOPBACK, SO_REUSEPORT, SO_TIMESTAMP, SO_BINTIME and option 0x10000](../../../core/libs/prx/libkernel/Socket/src/SocketOptions.cpp) (libkernel) - kept as bits that read back as on a PS5 (firmware 13.60, payload process), and options 0x4000 and 0x8000 are accepted and cannot be read, as there; only `SO_REUSEADDR` and `SO_REUSEPORT` change what the socket does. Both are set on the host, `SO_REUSEPORT` as `SO_REUSEADDR` on Windows, which has no `SO_REUSEPORT` and there also lets the socket bind over one that did not set it; Linux spreads unicast datagrams across the sockets, where FreeBSD gives each to one. Option 0x10000 is only kept: if the console treats it as FreeBSD 12's `SO_REUSEPORT_LB`, two sockets that set it share a port there, where the second `bind` here fails with `EADDRINUSE`. An accepted socket takes the bits from its listener, as FreeBSD 11.4's `sonewconn` copies `so_options`; that was not measured +- [IP_ADD_MEMBERSHIP, IP_DROP_MEMBERSHIP, IP_MULTICAST_IF](../../../core/libs/prx/libkernel/Socket/src/SocketOptions.cpp) (libkernel) - set on the host socket after the checks a PS5 (firmware 13.60, payload process) showed: `EOPNOTSUPP` on a stream socket, `EINVAL` for a short value or a group outside 224.0.0.0/4, `EADDRINUSE` for a group joined twice and `EADDRNOTAVAIL` for one not joined, tracked per socket. An interface the host lacks fails with the host's error (`EADDRNOTAVAIL` on the console). A group joined through `INADDR_ANY` and through that interface's address counts as two here. Not measured, after FreeBSD 11.4: a drop through `INADDR_ANY` leaves the first such group on any interface (`inp_leave_group`), and the other multicast options fail with `EOPNOTSUPP` on a stream socket (`inp_setmoptions`). An `ip_mreqn` with a non-zero index or address throws, as only a zeroed one was measured, and so does reading `IP_MULTICAST_IF` into 12 bytes, where FreeBSD returns an `ip_mreqn` +- [TCP_MAXSEG](../../../core/libs/prx/libkernel/Socket/src/SocketOptions.cpp) (libkernel) - kept per stream socket, read as 536 at first and failing with `EINVAL` for 1000, 70000 and 100, as a payload process on a PS5 (firmware 13.60) did. No segment set succeeded there, so the rule here, `EINVAL` above the current segment or below 176 and any segment between accepted, follows FreeBSD 11.4's `tcp_default_ctloutput` (`optval + 40 >= V_tcp_minmss`) and is not measured. An `AF_INET6` socket also starts at 536 here, where FreeBSD's `tcp_newtcpcb` uses 1220; not measured. The segment is not set on the host socket, so the host's segment size is its own, and a connected socket still reads the segment it was given instead of the negotiated one +- [TCP_KEEPIDLE, TCP_KEEPINTVL, TCP_KEEPCNT](../../../core/libs/prx/libkernel/Socket/src/SocketOptions.cpp) (libkernel) - kept per stream socket and read back as a payload process on a PS5 (firmware 13.60) did: 7200, 75 and 8 at first, 0 restoring a keepalive default, and a count stored as an unsigned value. They are not set on the host socket, so the host's keepalive timers are its own while `SO_KEEPALIVE` is applied there. An interval above 4294967 seconds fails with `EINVAL`, after FreeBSD 11.4's `tcp_default_ctloutput` with `hz` taken as 1000, and an accepted socket takes the keepalive values from its listener as `syncache_socket` copies them; neither was measured +- [IP_TTL, IP_TOS, IP_MULTICAST_TTL, IP_MULTICAST_LOOP](../../../core/libs/prx/libkernel/Socket/src/SocketOptions.cpp) (libkernel) - kept per `AF_INET` socket with the defaults (64, 0, 1, 1), the low-byte storage of `IP_TTL` and `IP_TOS`, the one-byte forms and the `EINVAL` above 255 of the multicast options that a payload process on a PS5 (firmware 13.60) showed. `IP_TTL` and the multicast options are also set on the host socket, except a time to live of 0, which hosts refuse; `IP_TOS` is only kept, so the host's packets carry its own type of service, and the host's default time to live (128 on Windows) is not changed to 64. An accepted socket takes `IP_TTL` and `IP_TOS` from its listener, as FreeBSD's `syncache_socket` does, and an `int` `IP_MULTICAST_LOOP` keeps only its low byte, as FreeBSD 11.4's `inp_setmoptions` does; neither was measured. Winsock applies `IP_MULTICAST_LOOP` on the receiving socket, not the sender, so on Windows the receiver's setting decides loopback +- [SO_SNDBUF, SO_RCVBUF, SO_SNDLOWAT, SO_RCVLOWAT](../../../core/libs/prx/libkernel/Socket/src/SocketOptions.cpp) (libkernel) - kept per socket with the defaults, the 524288-byte limit, the `ENOBUFS` above 5592405 bytes and the low-water clipping of a payload process on a PS5 (firmware 13.60); the host socket gets the size asked for, so a request above the limit still enlarges the host's buffer. The host's real buffering, FreeBSD's automatic growth of a connected TCP socket's buffers, and a low-water mark's effect on `select`, `poll` and a receive are not reproduced. An accepted socket takes all four from its listener, as FreeBSD's [`sonewconn`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/kern/uipc_socket.c#L603) does; that was not measured +- [SO_NOSIGPIPE](../../../core/libs/prx/libkernel/Socket/src/SocketOptions.cpp) (libkernel) - kept per socket and read back as its bit. On Linux a `send`, `sendto`, `sendmsg` or `write` on that socket passes `MSG_NOSIGNAL` to the host, as [`kern_sendit`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/kern/uipc_syscalls.c#L890-L895) and [`soo_write`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/kern/sys_socket.c#L147-L151) leave out the signal, and an accepted socket takes it from its listener, as [`sonewconn`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/kern/uipc_socket.c#L603) copies the options. Not measured on a console +- [SO_SNDTIMEO, SO_RCVTIMEO](../../../core/libs/prx/libkernel/Socket/src/SocketOptions.cpp) (libkernel) - the value checks and the 32.32 fixed-point read-back follow [`sosetopt`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/kern/uipc_socket.c#L2657-L2689), as a payload process on a PS5 (firmware 13.60) answered them. A receive or send on a blocking socket waits that long for the host to report the socket readable or writable and fails with `EWOULDBLOCK`, but only before the first transfer: once writable the host send has no limit, where FreeBSD returns the bytes sent so far, and a receive whose data another thread takes after the wait blocks again. `sendfile` ignores the send timeout, the host wait has millisecond steps, and an accepted socket takes both from its listener +- [IPV6_V6ONLY](../../../core/libs/prx/libkernel/Socket/src/SocketOptions.cpp) (libkernel) - the `IPPROTO_IPV6` option 27 is set on the host socket and reads back as 0 or 1, after FreeBSD's [`ip6_ctloutput`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/netinet6/ip6_output.c#L1716-L1732): a value that is not exactly an `int` fails with `EINVAL`, as does setting it on a bound socket, which the host refuses too. `socket` turns it on for every `AF_INET6` socket, as a payload process on a PS5 (firmware 13.60) reported, and Java turns it off on the sockets it opens: Java 25 Advanced Network (PPSA99871) stops at `java.net.SocketException: Protocol not available` without it. The value checks and the bound-socket answer were not measured +- [connect to AF_UNSPEC](../../../core/libs/prx/libkernel/Socket/src/Udp.cpp) (libkernel) dissolves a datagram association as FreeBSD 11.4's `udp_disconnect()` does, which resets the local address to the wildcard address and keeps the port. On Linux and on Windows a socket bound to an explicit port keeps its local address as well (`127.0.0.1:` stays `127.0.0.1:`), because a bound socket cannot be bound again, and `getsockname` reports that host address. On Windows a `sendto` to a non-loopback address from such a socket then fails with `ENETUNREACH` (Winsock 10051, measured on Windows 10), where FreeBSD sends it from the wildcard address. One bound to port 0 is bound again to the wildcard address with the port it had +- [connect](../../../core/libs/prx/libkernel/Socket/src/Udp.cpp) (libkernel) - `connect` to port 0 on an unconnected `AF_INET` or `AF_INET6` stream or datagram socket fails with `EADDRNOTAVAIL` (49), as FreeBSD 11.4's `in_pcbconnect_setup()` and `in6_pcbconnect()` do; a connected datagram socket is disconnected first, as `soconnect()` does, and then fails with 49. A connected stream socket keeps its host `EISCONN` answer. Known gap: `sendto` and `sendmsg` to port 0 on an `AF_INET` datagram socket fail with 49 unless the socket is bound to a specific address and port and the destination is neither `INADDR_ANY` nor `INADDR_BROADCAST`, which is the path where `udp_output()` skips `in_pcbconnect_setup()` and FreeBSD sends to port 0. There nothing can receive on port 0, so the call returns the length without the host, because Linux's `udp_sendmsg` refuses port 0 with `EINVAL`; an `AF_INET6` socket still goes to the host. The answers are taken from the FreeBSD source and are not measured on a console. [sceNetConnect](../../../core/libs/prx/libSceNet/Export.cpp) is not changed +- [msync](../../../core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp) (libkernel) follows FreeBSD 11's `kern_msync` and `vm_map_sync`: the address is rounded down to the page, a range that wraps is `EINVAL`, as is `MS_ASYNC` with `MS_INVALIDATE`, and a range whose start or any page is not mapped (as `sceKernelVirtualQuery` reports it, reservations included) is `ENOMEM`; a page unmapped from inside a larger mapping still counts as mapped there, since `sceKernelVirtualQuery` keeps reporting the whole mapping. The guest has no file mappings, so a successful call writes nothing back. `MS_INVALIDATE` on a mapped range fails with `EBUSY`, as it did in a payload process on a PS5 (firmware 13.60) for anonymous memory, locked or not, also with a hole unmapped inside it; a title process, reservations, direct memory and flexible memory were not measured - [madvise](../../../core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp) (libkernel) accepts `MADV_PROTECT` (10) as a no-op hint without modifying process protection. FreeBSD 11's `kern_madvise` (`sys/vm/vm_mmap.c`) handles it before the address check, ignores the address and length, and calls `kern_procctl(..., PROC_SPROTECT, PPROT_SET)`, which requires `PRIV_VM_MADV_PROTECT` and fails with `EPERM` without it. Returning 0 unconditionally assumes the calling process is privileged. Console behaviour is unverified - [mincore](../../../core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp) (libkernel) follows FreeBSD 11's `sys_mincore` (`sys/vm/vm_mmap.c`), not measured on the console: the address is rounded down to the page, length 0 returns 0, a null vector over a mapped range returns `EFAULT`, an overflowing range or any unmapped page in the range (as reported by `sceKernelVirtualQuery`) returns `ENOMEM`. For valid ranges, a page reports `MINCORE_INCORE` (0x1) when `sceKernelVirtualQuery` reports it committed and 0 when it is only reserved; actual residency, referenced and modified bits are not modelled. Length 0 returns 0 when the start page is mapped and `ENOMEM` otherwise, and the range and mapping checks come before the null vector `EFAULT`, as in `kern_mincore` Java 25 LAN / DNS (PPSA99869) imports it - [mlockall, munlockall](../../../core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp) (libkernel) - `mlockall` returns `EINVAL` for zero or unknown flags. Valid `MCL_CURRENT` throws because it would lock host stacks, heaps, modules and driver mappings along with guest memory; `MCL_FUTURE` throws because later mappings are not tracked for locking. FreeBSD 11.4's `sys_munlockall` (`sys/vm/vm_mmap.c`:1133-1165) checks `priv_check(td, PRIV_VM_MUNLOCK)` first (returning `EPERM` without it) and unwires the user map (`vm_map_unwire` over `vm_map_min`...`vm_map_max` with `VM_MAP_WIRE_HOLESOK`). Returning 0 unconditionally assumes the calling process is privileged. `munlockall` unlocks memory locked by `mlock` (`VirtualUnlock` on Windows, tolerating `ERROR_NOT_LOCKED` for pages never locked, and `munlockall` on Linux). On main, XPSemu (PPSA97358) fails to load at these imports (mlockall #4386, munlockall); their calls in QEMU `os-posix.c` execute only when `-overcommit mem-lock=on` is enabled. The call has not been observed in a run with this implementation. - [socket](../../../core/libs/prx/libkernel/Socket/src/Udp.cpp) (libkernel) creates only `SOCK_STREAM` and `SOCK_DGRAM` sockets in `AF_INET` and `AF_INET6`. A type that FreeBSD 11.4's inet domain has no protocol for in any configuration ([`in_proto.c`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/netinet/in_proto.c) has no `SOCK_RDM` entry and no unknown type) fails with `EPROTOTYPE` when the protocol is 0 and `EPROTONOSUPPORT` otherwise, as [`socreate`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_socket.c) does. `SOCK_RAW`, which `in_proto.c` always has (`socreate` creates it for a privileged process), and `SOCK_SEQPACKET`, which it has only in a kernel built with `SCTP`, fail with `EPROTONOSUPPORT`; whether the console allows either is not known. `SOCK_DGRAM` with `IPPROTO_UDPLITE` and `AF_UNIX` (`socketpair` supports it) fail with `EPROTONOSUPPORT` and `EAFNOSUPPORT` where FreeBSD creates the socket - [sigtimedwait](../../../core/libs/prx/libkernel/System/src/Signals.cpp) (libkernel) waits on a process-wide pending set instead of throwing: a signal raised while blocked stays pending and is taken by `sigwait`, `sigwaitinfo` or `sigtimedwait`, delivered to the handler when `_sigprocmask` unblocks it, or delivered by `pause`. The set covers all 128 signals and the lowest number that is pending and in the set is selected (FreeBSD specifies the lowest number only among real-time signals). Selection, `EAGAIN` on timeout, `EINVAL` for a `tv_nsec` outside 0 to 1000000000 while nothing is pending and `NULL` timeout blocking indefinitely follow FreeBSD's [sigtimedwait(2)](https://man.freebsd.org/cgi/man.cgi?query=sigtimedwait). `sigwait` returns 0 with the number in its output, as POSIX specifies, and `sigwaitinfo` and `sigtimedwait` return the number, as FreeBSD does. The guest `siginfo_t` follows FreeBSD 11's `sys/sys/siginfo.h`: 80 bytes, `si_signo` at 0, `si_code` at 8, `si_pid` at 12, `si_uid` at 16, `si_addr` at 24, `si_value` at 32 and `si_band` at 40; only `si_signo`, `si_code` (`SI_USER`) and `si_pid` (the guest pid) are written, `si_uid` stays 0 because no uid is modelled, and the console's layout is not verified. PPSA85300 imports `sigtimedwait` and fails to load without it; its only call site is VLC's `vlc_writev` ([filesystem.c](https://github.com/videolan/vlc/blob/6de05adcbaf2e8b85fe86aad4169393098628119/src/posix/filesystem.c#L243-L278)), which polls with a zero timeout to drain a `SIGPIPE` only after `writev` fails with `EPIPE`. The waits are not cancellation points and an unblocked caught signal does not interrupt them with `EINTR`, where FreeBSD's `sigwaitinfo` and `sigtimedwait` may return it. `SIGPIPE` has no host equivalent and is never raised by a host write here (see `read, write and vector I/O`), so VLC's drain finds nothing - [pause, sigsuspend, sigwait, sigwaitinfo](../../../core/libs/prx/libkernel/System/src/Signals.cpp) (libkernel) take the pending set of `sigtimedwait` instead of throwing. `pause` waits for a pending signal that is not blocked and whose disposition can be delivered, runs its handler on the calling thread and returns -1 with `EINTR`; an ignored signal is discarded and the wait continues, and it never returns for a signal whose handler cannot run. A signal whose registered handler has `SA_SIGINFO` still throws, and one whose disposition is the default action and that has no host equivalent (every signal except 2, 4, 6, 8, 11 and 15) is neither delivered nor discarded: it stays pending until a wait takes it, where FreeBSD runs the default action, which terminates the process for most of them. A pending `SIGALRM` with the default disposition exits the process with status 142 once it is unblocked, as FreeBSD's default action does. PPSA68091, PPSA99169, PPSA99420 and PPSA97358 import them and fail to load without them. PPSA68091 calls `pause` at one site (0x8F7266), in a loop that waits while a global holds another thread's `errno` address. PPSA99169 only stores the address of `pause` in a table of symbol addresses (0x30DB1). PPSA99420 calls `pause` at 0x3BD80F6 and 0x3C9E220, `sigwait` at 0x3C9E4C7 and `sigwaitinfo` at 0x3C9E588. PPSA97358 calls `sigwait` from QEMU's `sigwait_compat` thread ([compatfd.c](https://github.com/ZiZc3/XPSemu/blob/8eec07b4e31f66dce8e0b05c1ca1281af303e933/util/compatfd.c#L28-L53)), which `qemu_signal_init` ([main-loop.c](https://github.com/ZiZc3/XPSemu/blob/8eec07b4e31f66dce8e0b05c1ca1281af303e933/util/main-loop.c#L110-L147)) starts for `SIGIO`, `SIGALRM` and `SIGBUS` when the main loop is created, so it reaches the wait at start-up, and from the vCPU thread of the dummy accelerator ([dummy-cpus.c](https://github.com/ZiZc3/XPSemu/blob/8eec07b4e31f66dce8e0b05c1ca1281af303e933/accel/dummy-cpus.c#L48-L56)). `sigsuspend` swaps the process-wide mask of `_sigprocmask` for its argument, waits as `pause` does through the same helper, then restores the mask through `_sigprocmask`, and returns -1 with `EINTR`; a null set throws, as the console faults in libthr instead of returning `EFAULT`. All four words of the argument replace the mask and are restored afterwards, as in `_sigprocmask`. Java 25 LAN / DNS (PPSA99869, #5501) imports it and fails to load without it, after `sysctlnametomib`, `fegetenv` and `mincore`. None of the five waits is a cancellation point -- [getsid](../../../core/libs/prx/libkernel/System/src/Process.cpp) (libkernel) returns the calling process ID for pid 0 and the calling process, and `ESRCH` for other process IDs, assuming the single guest process is its own session leader. PPSA74126 imports it and fails to load without it +- [getsid, setpgid, setpgrp](../../../core/libs/prx/libkernel/System/src/Process.cpp) (libkernel) - getsid returns the calling process ID for pid 0 and the calling process, and `ESRCH` for other process IDs, assuming the single guest process is its own session leader. setpgid and setpgrp follow FreeBSD 11.4's `sys_setpgid` for the only guest process: a negative group ID fails with `EINVAL`, a process ID other than 0 or the caller's PID (including a negative PID) fails with `ESRCH`, and because the single guest process is modeled as its own session leader, `SESS_LEADER(targp)` fails with `EPERM` before normalizing the process group or reaching the same-process-group no-op. PPSA74126 imports `getsid` and fails to load without it - [sigaction](../../../core/libs/prx/libkernel/System/src/Signals.cpp) (libkernel) records the handler, flags and mask of every signal from 1 to 128 and reports them back, following FreeBSD's `sigaction` (`EINVAL` for other signals, and for a handler on `SIGKILL` or `SIGSTOP`); `signal` records its handler with `SA_RESTART`, as FreeBSD's libc does. Only handlers without `SA_SIGINFO` on the signals `signal` already delivers (`SIGINT`, `SIGILL`, `SIGABRT`, `SIGFPE`, `SIGSEGV`, `SIGTERM`) run, the same way `signal` handlers do; their mask and flags are not applied. `SA_SIGINFO` handlers are recorded but never called, since that needs FreeBSD's `siginfo_t` and `ucontext_t`, and neither are the handlers of other signals (`SIGBUS`, `SIGHUP`, ...), whether they are set by `sigaction` or by `signal`, which records them too (every number from 1 to 128, with `SA_RESTART` and an empty mask replacing what `sigaction` stored; `SIGKILL` and `SIGSTOP` accept only the default handler in both calls, as in FreeBSD's `kern_sigaction`). `raise` and `kill` accept every number from 1 to 128 now: a blocked one stays pending on the set of `sigtimedwait`, an unblocked one whose handler is registered runs it on the calling thread, and an unblocked one that has no host equivalent and no handler still fails with `EINVAL`, where FreeBSD runs its default action. `raise` of an `SA_SIGINFO` handler still throws instead of terminating -- [signal](../../../core/libs/prx/libkernel/System/src/Signals.cpp) (libkernel) records its handler with `SA_RESTART` unless the signal is in the exported `_sigintr` set, as FreeBSD's libc does (`lib/libc/gen/signal.c`). `siginterrupt`, which fills that set, is a libSceLibcInternal export and is not provided: libc does not link libkernel and cannot reach the dispositions, so only a guest that writes the set itself clears `SA_RESTART` this way. +- [signal](../../../core/libs/prx/libkernel/System/src/Signals.cpp) (libkernel) records its handler with `SA_RESTART` unless the signal is in the exported `_sigintr` set, as FreeBSD's libc does (`lib/libc/gen/signal.c`). `siginterrupt`, which fills that set, is a libSceLibcInternal export that libc serves through the `sigaction` and the set libkernel registers at load (see libc). - [kill, killpg](../../../core/libs/prx/libkernel/System/src/Signals.cpp) (libkernel) follow FreeBSD's `kern_kill` and `killpg1` for the only guest process: `EINVAL` for signals outside 0 to 128 and for negative `killpg` process groups, signal 0 only checks that the target exists, and its own pid, 0 and minus its own pid (the guest is taken to lead its own process group) signal it through `raise`, so they share `raise`'s limits above. Any other pid, and -1, which skips the caller, report `ESRCH`, so `EPERM` is never reported - [sceKernelFcntl](../../../core/libs/prx/libkernel/File/src/Open.cpp) (libkernel) runs the POSIX `fcntl` below and returns its guest errno as `SCE_KERNEL_ERROR_*`, the form the other `sceKernel` file exports use. The return values and error codes are unverified on a console - [Creating through a trailing slash](../../../core/libs/prx/libkernel/File/src/Open.cpp) (libkernel) - `open` and `sceKernelOpen` with `O_CREAT` on a missing name followed by a slash (`new/`) fail with `EINVAL` on Windows and with the host's `EISDIR` on Linux, where FreeBSD 11.4's `lookup()` (sys/kern/vfs_lookup.c) fails with `ENOENT` because only a directory may be created through a trailing slash -- [fcntl](../../../core/libs/prx/libkernel/File/src/DescriptorControl.cpp) (libkernel) follows FreeBSD 11.4's [`kern_fcntl`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/kern/kern_descrip.c#L479-L799) and [`kern_dup`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/kern/kern_descrip.c#L812) on file, directory, pipe and character device descriptors; a socket keeps the libSceNet path (`F_DUPFD`, `F_DUP2FD`, `F_DUPFD_CLOEXEC`, `F_DUP2FD_CLOEXEC`, `F_GETFL`, `F_SETFL` with `O_NONBLOCK`, `F_GETFD` and `F_SETFD`, `EINVAL` for every other command, see the next entry). On Linux and macOS a host socket that is not a guest socket, such as a standard descriptor a service manager connected to its journal, takes the flag handling of a pipe end and `FIONREAD` asks the host for its queued bytes (for a datagram socket Linux counts only the next datagram, where FreeBSD's [`soo_ioctl`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/sys_socket.c#L202-L205) counts every buffered byte); its record locks and read-ahead hints fail with `EBADF`, as FreeBSD fails them on a descriptor without a vnode, and `O_ASYNC` on it throws. The close-on-exec flag and the status flags live in a guest table, the status flags shared by `dup`, `dup2` and `F_DUPFD` as in an open file description; a descriptor the table has not seen, such as 0 to 2, starts from the host's flags, since the console's are not known. `F_DUPFD` and `F_DUPFD_CLOEXEC` return the lowest free host descriptor at or above the start, so the bound is the host table (`EMFILE` on Windows, `EINVAL` past `RLIMIT_NOFILE` on Linux), not the console's unknown `RLIMIT_NOFILE` and `kern.maxfilesperproc`. `O_NONBLOCK` switches a host pipe (`PIPE_NOWAIT` on Windows, where an empty read and a full write then report `EAGAIN`) and is only recorded on files, directories and the random device, as their reads never block; on a Windows console or other character device it throws. Changing `O_APPEND` on a Windows file, changing `O_FSYNC` on a file, `O_ASYNC` on a pipe or character device (`SIGIO` is not delivered), `F_GETOWN`, `F_SETOWN` and `F_SETLK_REMOTE` throw; `O_DIRECT`, `F_RDAHEAD` and `F_READAHEAD` are recorded as the hints they are. Record locks (`F_GETLK`, `F_SETLK`, `F_SETLKW` and their forms without `l_sysid`) use the host's POSIX locks on Linux and macOS. `F_GETLK` with `F_UNLCK` asks the host with `F_RDLCK`: as in [`lf_blocks`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/kern/kern_lockf.c#L866) only another owner's overlapping write lock conflicts with either request. On Windows they follow [`kern_fcntl`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/kern/kern_descrip.c#L607-L752) and [`lf_advlockasync`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/kern/kern_lockf.c#L433-L487) for the one guest process, which owns every lock: `EOVERFLOW` for an `SEEK_CUR` start past `OFF_MAX`, then `EBADF` for `F_RDLCK` without read access or `F_WRLCK` without write access and `EINVAL` for another type, then `EINVAL` and `EOVERFLOW` for the range; as [`lf_blocks`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/kern/kern_lockf.c#L866) never lets locks of one owner conflict, whichever descriptor or thread asks, `F_SETLK` and `F_SETLKW` then succeed without waiting and `F_GETLK` reports `F_UNLCK`, so no lock list is kept and merging, splitting and the release on close have nothing to change. Nothing is held on the host, since Windows byte-range locks are mandatory and would fail the guest's own reads and writes through other descriptors, so other host processes neither see these locks nor are seen; a lock from the end of a directory throws. The return values and error codes are unverified on a console +- [fcntl](../../../core/libs/prx/libkernel/File/src/DescriptorControl.cpp) (libkernel) follows FreeBSD 11.4's [`kern_fcntl`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/kern/kern_descrip.c#L479-L799) and [`kern_dup`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/kern/kern_descrip.c#L812) on file, directory, pipe and character device descriptors; a socket keeps the libSceNet path (`F_DUPFD`, `F_DUP2FD`, `F_DUPFD_CLOEXEC`, `F_DUP2FD_CLOEXEC`, `F_GETFL`, `F_SETFL` with `O_NONBLOCK`, `F_GETFD` and `F_SETFD`, `EINVAL` for every other command, see the next entry). On Linux and macOS a host socket that is not a guest socket, such as a standard descriptor a service manager connected to its journal, takes the flag handling of a pipe end and `FIONREAD` asks the host for its queued bytes (for a datagram socket Linux counts only the next datagram, where FreeBSD's [`soo_ioctl`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/sys_socket.c#L202-L205) counts every buffered byte); its record locks and read-ahead hints fail with `EBADF`, as FreeBSD fails them on a descriptor without a vnode, and `O_ASYNC` on it throws. The close-on-exec flag and the status flags live in a guest table, the status flags shared by `dup`, `dup2` and `F_DUPFD` as in an open file description; a descriptor the table has not seen, such as 0 to 2, starts from the host's flags, since the console's are not known. `F_DUPFD` and `F_DUPFD_CLOEXEC` return the lowest free host descriptor at or above the start, so the bound is the host table (`EMFILE` on Windows, `EINVAL` past `RLIMIT_NOFILE` on Linux), not the console's unknown `RLIMIT_NOFILE` and `kern.maxfilesperproc`. `O_NONBLOCK` switches a host pipe (`PIPE_NOWAIT` on Windows, where an empty read and a full write then report `EAGAIN`) and is only recorded on files, directories and the random device, as their reads never block; on a Windows console or other character device it throws. Changing `O_FSYNC` on a file, `O_ASYNC` on a pipe or character device (`SIGIO` is not delivered), `F_GETOWN`, `F_SETOWN` and `F_SETLK_REMOTE` throw; `O_DIRECT`, `F_RDAHEAD` and `F_READAHEAD` are recorded as the hints they are. Record locks (`F_GETLK`, `F_SETLK`, `F_SETLKW` and their forms without `l_sysid`) use the host's POSIX locks on Linux and macOS. `F_GETLK` with `F_UNLCK` asks the host with `F_RDLCK`: as in [`lf_blocks`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/kern/kern_lockf.c#L866) only another owner's overlapping write lock conflicts with either request. On Windows they follow [`kern_fcntl`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/kern/kern_descrip.c#L607-L752) and [`lf_advlockasync`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/kern/kern_lockf.c#L433-L487) for the one guest process, which owns every lock: `EOVERFLOW` for an `SEEK_CUR` start past `OFF_MAX`, then `EBADF` for `F_RDLCK` without read access or `F_WRLCK` without write access and `EINVAL` for another type, then `EINVAL` and `EOVERFLOW` for the range; as [`lf_blocks`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/kern/kern_lockf.c#L866) never lets locks of one owner conflict, whichever descriptor or thread asks, `F_SETLK` and `F_SETLKW` then succeed without waiting and `F_GETLK` reports `F_UNLCK`, so no lock list is kept and merging, splitting and the release on close have nothing to change. Nothing is held on the host, since Windows byte-range locks are mandatory and would fail the guest's own reads and writes through other descriptors, so other host processes neither see these locks nor are seen; a lock from the end of a directory throws. The return values and error codes are unverified on a console - [fcntl on a guest socket](../../../core/libs/prx/libkernel/Socket/src/Udp.cpp) (libkernel) answers `F_DUPFD`, `F_DUP2FD`, `F_DUPFD_CLOEXEC`, `F_DUP2FD_CLOEXEC`, `F_GETFD`, `F_SETFD`, `F_GETFL` and `F_SETFL` and fails every other command with `EINVAL`; FreeBSD 11.4's `kern_fcntl` also handles `F_GETOWN` and `F_SETOWN` on a socket. `FD_CLOEXEC` is only recorded (there is no `exec`): `dup`, `dup2`, `F_DUPFD`, `F_DUP2FD` onto another descriptor and an accepted socket start without it, `F_DUP2FD` onto the same descriptor keeps it, and `SOCK_CLOEXEC`, `F_DUPFD_CLOEXEC`, `F_DUP2FD_CLOEXEC` and `F_SETFD` set it. None of this was measured on a console. - [ioctl, _ioctl](../../../core/libs/prx/libkernel/Socket/src/Udp.cpp) (libkernel) handle only `FIONBIO` and `FIONREAD` on a socket, and any other request there fails with `ENOTTY`. On the other descriptors they follow FreeBSD's [`kern_ioctl`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/kern/sys_generic.c#L758), [`vn_ioctl`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/kern/vfs_vnops.c#L1488) and [`pipe_ioctl`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/kern/sys_pipe.c#L1346): `FIONBIO` sets `O_NONBLOCK` as `fcntl` does, `FIOCLEX` and `FIONCLEX` set and clear the close-on-exec flag `F_GETFD` reports, and `FIONREAD` reports the size past the offset of a file, the queued bytes of a pipe's read end, 0 on its write end and `ENOTTY` on the random device, while it throws on a directory or another character device. Every termios request (group `t`, `TIOCGWINSZ` included) fails with `ENOTTY`, since those descriptors are taken as not a terminal, as in `tcgetattr`, and any other well-formed request throws. An input request with length 0 is not rejected as malformed: [`sys_ioctl`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/kern/sys_generic.c#L703-L718) rejects it only in a kernel built without `COMPAT_FREEBSD4`, `COMPAT_FREEBSD5` and `COMPAT_43`, and which of these the PS5 kernel has is unknown. Stremio Plus (PPSA74126) imports `_ioctl` for its statically linked `tc*` functions, which send `TIOCGETA`, `TIOCSETA`, `TIOCSPGRP` and the other termios requests - [ioctl, _ioctl](../../../core/libs/prx/libkernel/Socket/src/Udp.cpp) (libkernel) do not pass a `sceNetSocket` socket to libSceNet as `fcntl` does. On Linux its number duplicates the host socket, so `FIONBIO` switches the host `O_NONBLOCK` but not the library's `SO_NBIO` state; on Windows it is a `NUL` placeholder, so `FIONREAD` and enabling `FIONBIO` throw as on a character device. `sceNetIoctl` handles both requests on such a socket. @@ -245,6 +266,9 @@ - [pthread_spin_lock](../../../core/libs/prx/libkernel/Pthread/Posix/Spinlock.cpp) (libkernel) - a lock taken again by the thread that holds it throws here. On a PS5 (firmware 12.70) that call had not returned after 2 s, as FreeBSD 11.4 libthr [`thr_pspinlock.c`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/lib/libthr/thread/thr_pspinlock.c) spins forever - [pthread_mutex_getspinloops_np, pthread_mutex_setspinloops_np, pthread_mutex_getyieldloops_np, pthread_mutex_setyieldloops_np and their scePthreadMutex counterparts](../../../core/libs/prx/libkernel/Pthread/src/Mutex.cpp) (libkernel) store and report the counts, but host mutexes do not spin or yield by them. The defaults follow FreeBSD 11.4's [`mutex_init_body`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/lib/libthr/thread/thr_mutex.c#L236-L272) without `LIBPTHREAD_SPINLOOPS` and `LIBPTHREAD_YIELDLOOPS`: 2000 spins for an adaptive mutex, 0 otherwise; the console's defaults are unverified. Setting the yield count of a destroyed mutex returns 0 as FreeBSD's [`__pthread_mutex_setyieldloops_np`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/lib/libthr/thread/thr_mutex.c#L1125-L1135) does; the console's result is unverified - [pthread_rwlockattr_getpshared, pthread_rwlockattr_setpshared](../../../core/libs/prx/libkernel/Pthread/Posix/Rwlock.cpp) (libkernel) - `pthread_rwlockattr_destroy` clears the attribute here, so a later get, or a set of 0, throws. On a PS5 (firmware 12.70) destroy leaves the attribute set; a get afterwards returned 0 with a stale value and a set of 0 returned 0 +- [pthread_mutexattr_setkind_np](../../../core/libs/prx/libkernel/Pthread/Posix/Mutex.cpp) (libkernel) throws for a kind outside 1 to 4. FreeBSD 11.4's [`_pthread_mutexattr_setkind_np`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/lib/libthr/thread/thr_mutexattr.c#L108-L120) stores any kind and [`mutex_check_attr`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/lib/libthr/thread/thr_mutex.c#L207-L218) then makes `pthread_mutex_init` fail with `EINVAL`, but `pthread_mutex_init` here does not check the type +- [pthread_cond_signalto_np, scePthreadCondSignalto](../../../core/libs/prx/libkernel/Pthread/src/Cond.cpp) (libkernel) wake every waiter of the condition variable, not only the given thread, and do not check the thread; the other waiters see a spurious wakeup. Which waiters the console wakes, and its result for a thread that is not waiting, are unverified +- [pthread_rwlockattr_gettype_np, scePthreadRwlockattrGettype](../../../core/libs/prx/libkernel/Pthread/Posix/Rwlock.cpp) (libkernel) report the type stored by `scePthreadRwlockattrSettype`, which accepts any value and which read-write locks do not apply; the console's valid types and their effect are unverified. A null or uninitialized attribute throws, as for `pthread_rwlockattr_getpshared` - [sceKernelGetEventError](../../../core/libs/prx/libkernel/Equeue/src/EventAccessors.cpp) (libkernel) throws for `fflags` above 0xFFFF, where OR and addition of 0x80020000 differ and the console was not measured. Otherwise it returns 0 for `fflags` 0 and `0x80020000 | fflags`, whatever `flags` and `data` hold, as [fpPS4](https://github.com/red-prig/fpPS4/blob/04cefd43e6fddd1ab033e7980cd356d14c964905/kernel/ps4_queue.pas#L643) does and a PS5 (firmware 13.40, measured 2026-10-19 in a payload process) answered: `{flags 0x4000, fflags 0, data 22}`, `{0x4000, 0, -1}`, `{0x4000, 0, 0x80020016}` and `{0, 0, 0}` gave 0, `{0x4000, 5, 0}` and `{0x4000, 5, 22}` 0x80020005, `{0, 7, 22}` 0x80020007, `{0x8000, 3, 9}` 0x80020003. Video-out events carry their count in `fflags`, so they get 0x80020001 and up; only events built by the caller were measured. Closes when: `fflags` above 0xFFFF is measured - [pthread_cond_reltimedwait_np](../../../core/libs/prx/libkernel/Pthread/Posix/Cond.cpp) (libkernel) takes `(cond, mutex, usec)`, a relative timeout in microseconds, and is `scePthreadCondTimedwait` with POSIX error numbers. FreeBSD 11.4 has no such function; the signature comes only from shadPS4, which registers one function for this NID and for `scePthreadCondTimedwait` ([`condvar.cpp`](https://github.com/shadps4-emu/shadPS4/blob/3a9f57265d28975658f1ca941daa5c879c7a74bd/src/core/libraries/kernel/threads/condvar.cpp#L229-L234)). shadPS4 reads a 64-bit count; here it is the 32-bit `SceKernelUseconds` of the SCE twin, as for `sem_reltimedwait_np`. Unverified on the console - [pthread_mutex_reltimedlock_np](../../../core/libs/prx/libkernel/Pthread/Posix/Mutex.cpp) (libkernel) takes `(mutex, usec)`, a relative timeout in microseconds, and is `scePthreadMutexTimedlock` with POSIX error numbers. FreeBSD 11.4 has no such function; the signature comes only from shadPS4, which registers one function for this NID and for `scePthreadMutexTimedlock` ([`mutex.cpp`](https://github.com/shadps4-emu/shadPS4/blob/7c960eae136b956cf4c1d72aacd2426b650330df/src/core/libraries/kernel/threads/mutex.cpp#L284-L287)). shadPS4 reads a 64-bit count; here it is the 32-bit `SceKernelUseconds` of the SCE twin, as for `pthread_cond_reltimedwait_np`, so a count of 2^32 microseconds or more would be truncated if the console reads 64 bits. Unverified on the console @@ -256,7 +280,7 @@ - [chflags](../../../core/libs/prx/libkernel/File/src/Stdio.cpp) (libkernel) follows FreeBSD 11.4's [`kern_chflagsat`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/kern/vfs_syscalls.c#L2366-L2383) and UFS's [`ufs_setattr`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/ufs/ufs/ufs_vnops.c#L509-L556) for a root caller: the path lookup reports `EFAULT`, `ENOENT` and `ENOTDIR`, a flag outside the 17 UFS stores (`VNOVAL` included) `EOPNOTSUPP`, and `SF_SNAPSHOT` `EPERM`. Neither host keeps BSD file flags, so every file is taken to have none (`stat` reports `st_flags` 0): flags 0 succeed without touching the file, and any other flag throws, naming the flags. `UF_READONLY`, `UF_HIDDEN`, `UF_SYSTEM` and `UF_ARCHIVE` are FreeBSD's names for the Windows attributes but are not mapped to them: UFS stores them without enforcing them, `FILE_ATTRIBUTE_READONLY` already carries the owner write bit of `chmod` on Windows, and Linux has no equivalent a process can set without `CAP_LINUX_IMMUTABLE`. The console's filesystem types, `EROFS` on read-only mounts and the change-time update are not modelled - [fchflags](../../../core/libs/prx/libkernel/File/src/Stdio.cpp) (libkernel) has the gaps of `chflags`: a valid non-zero flag throws, and the console's filesystem types, `EROFS` and the change-time update are not modelled. Character devices, the random device, other host descriptors and files read from app0.zar throw, since their console filesystems and the privilege check for devices are not modelled. On Windows, anonymous shared memory objects are temporary files that are not told apart from regular files, so they are treated as files instead of reporting `EINVAL` as FreeBSD 11.4's [`getvnode`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/kern/vfs_syscalls.c#L3850-L3877) does. - [getpriority](../../../core/libs/prx/libkernel/System/src/Process.cpp) (libkernel) follows FreeBSD 11.4's [`sys_getpriority`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/kern/kern_resource.c#L93-L165) for the only guest process, which is taken to run at nice 0: `PRIO_PROCESS` and `PRIO_PGRP` with 0 or its own pid (the guest is taken to lead its own process group, see `kill`) and `PRIO_USER` with 0 or its user id 0 return 0, any other id fails with `ESRCH` and any other `which` with `EINVAL`. As in FreeBSD's libc, where `getpriority` is the bare system call, errno is left unchanged on success. The nice value of a title process on the console is unverified, and the host nice of the process or of the threads `APS5_THREAD_PRIORITY=high` raises is not reported. `setpriority` accepts the same target ids (any other id fails with `ESRCH`, any other `which` with `EINVAL`), clamps the value to FreeBSD's `PRIO_MIN`-`PRIO_MAX` (-20 to 20) and stores it as the guest process's nice value, which `getpriority` returns; the host scheduling class is not changed, and whether a title process may lower the value without privilege is unverified -- [rtprio_thread, pthread_setconcurrency, pthread_getconcurrency](../../../core/libs/prx/libkernel/Pthread/Posix/Thread.cpp) (libkernel) - `rtprio_thread` follows FreeBSD 11.4's `rtprio_thread(2)`. Every guest thread here runs under `SCHED_FIFO` or `SCHED_RR` (priority 256-767), whose rtprio class is real-time; the console's mapping from that priority to the rtprio `prio` range (0-31) is unmeasured, so `RTP_LOOKUP` and `RTP_SET` throw for such a thread rather than report an impossible priority. A thread that is not real-time reports `RTP_PRIO_NORMAL` (3) with 0, and `RTP_SET` accepts only that value. Another function fails with `EINVAL`, a null `rtprio` with `EFAULT`, a thread other than the calling one throws, and an id that is not a live guest thread with `ESRCH`. `pthread_setconcurrency` stores 0 or more and returns `EINVAL` for a negative level (the error itself, as FreeBSD's libthr does, with errno untouched); `pthread_getconcurrency` returns the level, which is a hint and changes nothing about the host's concurrency. Morrowind (`mshivam019/morrowind-ps5`) imports all three at start-up +- [rtprio_thread, pthread_setconcurrency, pthread_getconcurrency](../../../core/libs/prx/libkernel/Pthread/Posix/Thread.cpp) (libkernel) - `RTP_SET` throws for a real-time thread (sce attributes allow only `SCHED_FIFO`/`SCHED_RR`) and for another thread: a payload process on a PS5 (firmware 12.70) answered type 2, prio 0/15/31 on the calling thread with `EINVAL`; no accepted value or set by another thread's id measured. A non-real-time thread (POSIX attributes store `SCHED_OTHER`), unmeasured, reports 3 with 0, and `RTP_SET` accepts only that. `RTP_LOOKUP` reports the policy's type (10 or 2) and the scheduling priority, as measured, not FreeBSD's 0-31; another function fails with `ESRCH` (2 measured), a null `rtprio` with `EFAULT`, an id of no live thread with `ESRCH` (unknown ids measured). `pthread_setconcurrency` stores 0 or more, returns `EINVAL` (not errno) below 0, as libthr does; `pthread_getconcurrency` returns it, a hint. Morrowind (`mshivam019/morrowind-ps5`) imports them - [sceKernelVirtualQuery](../../../core/libs/prx/libkernel/DirectMemory/Export.cpp) returns [VirtualQueryInfo](../../../core/libs/SceTypes.hpp) with a one-byte flag allocation unit so Windows and SysV hosts return the same 72-byte representation, with the name at byte 33 and GPU mask at byte 65. This preserves the existing SysV representation; the console offsets beyond the address, protection and size fields observed in Astro Bot's AGC calls are not hardware-measured. Size and offset assertions plus a raw-byte named-mapping regression guard the host ABI. - [app0.zar files](../../../core/libs/prx/libkernel/File/src/ArchivedFile.cpp) (libkernel) - `sceKernelOpen`, `open`, `sceKernelStat` and `sceKernelCheckReachability` resolve a path missing from `app0/` in `app0.zar`. An archived file is a read-only host null device whose reads, positioned and vectored reads, AIO reads, seeks, `fstat` and `FIONREAD` are served from the archive, with the position shared by `dup`, `dup2` and `F_DUPFD`; writes fail with `EBADF` as on a read-only descriptor, opening one for writing or truncation fails with `EROFS` and `O_CREAT | O_EXCL` with `EEXIST`. `fstat` and `sceKernelStat` report mode `0444` (`0555` for a directory), the node index as inode and the archive file's device and times. Opening an archived directory or a directory present both on disk and in the archive throws, as `getdents` would miss entries. On an archived descriptor `mmap`, `flock`, record locks, `fstatfs`, `ftruncate`, `fsync`, `fdatasync`, `getdirentries`, `fchmod` and `futimes` throw instead of acting on the null device; `mmap` is `noexcept`, so its refusal terminates the process. `lstat` and the other path calls on archived files are not implemented - [sceKernelLoadStartModule](../../../core/libs/prx/libkernel/Module/src/Module.cpp) (libkernel) loads a system library named by its console path, `/system/common/lib/.sprx` or the sandboxed `//common/lib/.sprx` that `sceKernelGetFsSandboxRandomWord` names, from the host `libs/.prx`, as libSceSysmodule loads modules by ID; a host library has no `module_start`, so `res` is 0. Other system directories, such as `/system/priv/lib`, are not mapped. The OpenOrbis piglet sample loads `libScePigletv2VSH.sprx` and `libScePrecompiledShaders.sprx` this way diff --git a/docs/dev/debt/libkernel_sys.md b/docs/dev/debt/libkernel_sys.md index 02c6230564..5f015be839 100644 --- a/docs/dev/debt/libkernel_sys.md +++ b/docs/dev/debt/libkernel_sys.md @@ -12,7 +12,7 @@ - [sched_rr_get_interval](../../../core/libs/prx/libkernel_sys/Export.cpp) (libkernel_sys) accepts pid 0 or the host-backed guest pid and reports the FreeBSD 11 ULE default time slice for the usual AMD64 clock rates (94 ms); other pids return `ESRCH`, and a null interval returns `EFAULT`. AnyPS5 does not run a guest ULE scheduler or track a per-process configured quantum, so this reports the modeled default and does not reflect runtime scheduling. Console-specific clock rates and quantum configuration are unverified - [fchdir](../../../core/libs/prx/libkernel_sys/Export.cpp) (libkernel_sys) changes the guest working directory to the descriptor's directory: on Windows through the path recorded when the descriptor was opened, on Linux through the `/proc/self/fd` link target. It returns `EBADF` for a descriptor that is not open, `EINVAL` for a pipe or socket, as FreeBSD `getvnode` does for a descriptor without a vnode, and `ENOTDIR` for any other descriptor that is not a directory. Outside Windows it checks search permission on the directory with `faccessat`, which fails with `EACCES` as FreeBSD `change_dir` does; on Windows that check is not modelled. A directory renamed after it was opened is still reached through the recorded path. Console behaviour is unverified -- [openat](../../../core/libs/prx/libkernel_sys/Export.cpp) (libkernel_sys) passes `AT_FDCWD` (-100) and absolute paths to `open`. Any other relative path is joined to the descriptor's host directory, found the same way as for `fchdir`, so guest path aliases and case-insensitive name matching do not apply to it, and a `..` component is not kept inside the guest root. It returns `EBADF` for a descriptor that is not open and `ENOTDIR` for one that is not a directory. As with `open`, `O_NOFOLLOW` and `O_NONBLOCK` are ignored. PPSA99003 opens directory entries this way. Console behaviour is unverified +- [openat, _openat](../../../core/libs/prx/libkernel_sys/Export.cpp) (libkernel_sys) pass `AT_FDCWD` (-100) and absolute paths to `open`. Any other relative path is joined to the descriptor's host directory, found the same way as for `fchdir`, so guest path aliases and case-insensitive name matching do not apply to it, and a `..` component is not kept inside the guest root. They return `EBADF` for a descriptor that is not open and `ENOTDIR` for one that is not a directory. As with `open`, `O_NONBLOCK` is ignored. PPSA99003 opens directory entries through `openat`. Console behaviour is unverified - [fchmodat](../../../core/libs/prx/libkernel_sys/Export.cpp) (libkernel_sys) passes `AT_FDCWD` (-100) and absolute paths to `chmod`; any other relative path is joined to the descriptor's host directory the same way as for `openat`, so guest path aliases and case-insensitive name matching do not apply to it and a `..` component is not kept inside the guest root. A flag other than `0` or `AT_SYMLINK_NOFOLLOW` (0x200) returns `EINVAL`; the mode is masked to `07777`. With `AT_SYMLINK_NOFOLLOW` a symbolic link returns success silently on Windows and Linux, since the host cannot change a link's own mode, while a regular file is changed either way. The mode maps to the host file mode, which on Windows is only the CRT's read-only bit. PPSA99621 calls it through `std::filesystem::permissions`. Console behaviour is unverified - [lchmod](../../../core/libs/prx/libkernel_sys/Export.cpp) (libkernel_sys) throws for a symbolic link on Windows and Linux, whose hosts cannot change a link's own mode; on macOS the link's mode is changed. Anything else goes to `fchmodat(AT_FDCWD, path, mode, AT_SYMLINK_NOFOLLOW)`, as FreeBSD 11.4's [`sys_lchmod`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/kern/vfs_syscalls.c#L2490-L2496) does, so a file or directory is changed as by `chmod`, with `fchmodat`'s path lookup and Windows mode mapping. On Windows any reparse point counts as a link. Console behaviour is unverified - [pathconf, fpathconf, _fpathconf](../../../core/libs/prx/libkernel_sys/Export.cpp) (libkernel_sys) report FreeBSD 11's `ufs_pathconf` constants for files and directories alike (`_PC_LINK_MAX` 32767, `_PC_NAME_MAX` 255, `_PC_PATH_MAX` and `_PC_SYMLINK_MAX` 1024, `_PC_PIPE_BUF` 512, `_PC_CHOWN_RESTRICTED` and `_PC_NO_TRUNC` 1, `_PC_FILESIZEBITS` 64, `_PC_REC_MAX_XFER_SIZE` -1, `_PC_PRIO_IO` and `_PC_SYNC_IO` 0); whether the console's filesystems report the same is not verified. They throw for names whose UFS value depends on the mount, the kernel options or the page size, and where UFS and `vop_stdpathconf` disagree (`_PC_MAX_CANON`, `_PC_MAX_INPUT`, `_PC_VDISABLE`, and `_PC_ASYNC_IO` in `pathconf`); other names fail with `EINVAL`. `fpathconf` and `_fpathconf` answer `_PC_ASYNC_IO` 200112 and, for a pipe, `_PC_PIPE_BUF` 512 and `EINVAL` otherwise, as FreeBSD 11's `kern_fpathconf` does; a guest socket is not a host descriptor, so the `fstat` check reports it as `EBADF`, as in `fchown` diff --git a/docs/dev/debt/relinker.md b/docs/dev/debt/relinker.md index f4e849cbda..241d10f16f 100644 --- a/docs/dev/debt/relinker.md +++ b/docs/dev/debt/relinker.md @@ -2,20 +2,26 @@ ### Functional +- The [syscall scanner](../../../core/relinker/relinker/src/analysis/SyscallScanner.cpp) decodes executable ranges linearly without code/data boundaries. Embedded data between routines can fail decoding or be reported as a forbidden syscall ([#7547](https://github.com/boykopovar/AnyPS5/issues/7547)). Unwind ranges alone omit real code. The [synthetic fixture](../../../core/relinker/relinker/tests/CodeDataScanningTests.cpp) characterizes these rejections and checks a reachable routine beyond the gap; it does not resolve the boundary problem. + - [Executable guest imports](../../../core/relinker/relinker/src/guest/GuestModuleBuilder.cpp) - on Linux and macOS, a relocated executable import resolved to one guest provider requires that module and its dependencies at startup, even when it was discovered through `--module-dir` without a DT_NEEDED entry. Unreferenced symbols and no-op relocations do not require an otherwise deferred module. Console module-start ordering is unmeasured. -- [Runtime-loaded modules](../../../core/relinker/relinker/src/guest/GuestModuleBuilder.cpp) (`--module-dir`) that another module needs are initialized at startup with no start arguments, before the module that needs them is loaded; the console is assumed to start them when that module is loaded. -- [Runtime-loaded modules the executable needs](../../../core/relinker/relinker/src/guest/GuestModuleBuilder.cpp) (`--module-dir`) are mapped at startup on Windows and started when the title loads them with `sceKernelLoadStartModule`, with their start arguments, as a Unity player loads `mono-ps4.sprx`. A title that calls such a module before loading it finds its initializers not run; whether the console starts it earlier is not measured. A module the executable only imports from and the title never loads itself gets no `module_start` and no initializers at all under this deferral, and nothing reports it. Linux and macOS still start it at startup. +- [Runtime-loaded modules](../../../core/relinker/relinker/src/guest/GuestModuleBuilder.cpp) (`--module-dir`) can be mapped early for bindings. Linux defers their initialization to an explicit load of each module; standard SDK folders keep their mapping initializer, including through the browser conversion API. On Windows and macOS, a module another module needs is initialized at startup without start arguments; console dependency-start ordering is unmeasured. +- [Runtime-loaded modules the executable needs](../../../core/relinker/relinker/src/guest/GuestModuleBuilder.cpp) (`--module-dir`) are mapped at startup on Windows and started when the title loads them with `sceKernelLoadStartModule`, with their start arguments, as a Unity player loads `mono-ps4.sprx`. A title that calls such a module before loading it finds its initializers not run; whether the console starts it earlier is not measured. A module the executable only imports from and the title never loads itself gets no `module_start` and no initializers at all under this deferral. The [entry stub](../../../core/relinker/elfpatcher/src/windows/WindowsEntryStubBuilder.cpp) prints `Deferred module ` at startup for each such module that an import of the executable names, whether or not the title loads it later; an import without a module name is not reported. macOS still starts it at startup. - The executable file that [relinker](../../../core/relinker/elfpatcher/src/windows/WindowsPeWriter.cpp) generates opens a console when launched, unless it is relinked with `--windows-gui`, which selects the GUI subsystem. A GUI build started from Explorer has no standard output or error handle; the [entry stub](../../../core/relinker/elfpatcher/src/windows/WindowsEntryStubBuilder.cpp) gives each missing one `anyps5.log` beside the executable, replacing the previous run's file (#3282). Errors about the executable path or about waiting for the previous LoadExec process are written before the log is opened and are lost when the standard error handle is missing. If the log cannot be created, output to the missing handles is lost. - [Guest import registries](../../../core/relinker/relinker/src/guest/GuestModuleBuilder.cpp) - declared host imports of bundled modules are recorded with their module name and relocation target, even when another guest module exports the same stripped NID. No-op relocations are omitted. Registries describe import bindings, not implementation correctness or runtime reachability. - Converted Windows main executables [omit `DYNAMIC_BASE`](../../../core/relinker/elfpatcher/src/windows/WindowsPeWriter.cpp) by default, not only with `--windows-fixed-base`: on the console the executable lies inside the 40-bit window that the title's own `sceLibcMspaceCreate` checks (#3043). Relocations are kept, so if `0x140000000` is taken Windows relocates the image, possibly above that window. Verified only on Windows 10. - [SDK memory-management syscall traps](../../../core/relinker/relinker/src/analysis/SyscallScanner.cpp) under `--trap-sdk-syscall-stubs` recognize three exact instruction sequences from [ps5-payload-dev SDK libc/mman.c](https://github.com/ps5-payload-dev/sdk/blob/12cf3dda40/libc/mman.c), observed in Nuvio Maxd 0.5.15 (PPSA99288): syscall 74 and the two syscall 477 paths, with their argument preparation and carry-flag error handling. Matching requires a decoded instruction boundary and the complete sequence inside the scanned code range. Other SDK/compiler output remains rejected. The matched syscall becomes `UD2`; this does not implement mmap or mprotect and a call still faults. - The [instruction decoder](../../../core/relinker/codegen/src/x86/X64InstructionDecoder.cpp) reads a 16-bit displacement for `66 E8`, `66 E9` and `66 0F 80`-`8F` without REX.W, as AMD processors, the console included, do (binutils `objdump -M amd64`; REX.W selects the 32-bit form). AMD also truncates the target of every near relative branch with that prefix, rel8, LOOP and JRCXZ forms included, to 16 bits. Instruction metadata marks these branches as truncated and reports no branch target for them, and stubs refuse to move them. Compilers do not emit these forms. - Converted Linux main executables are [placed at `0x400000`](../../../core/relinker/relinker/src/pipeline/Conversion.cpp) by default, as `ET_EXEC` without ASLR, not only with `--linux-fixed-base`: the console executable lies inside the 40-bit window that the title's own `sceLibcMspaceCreate` checks (#3043). Where the console loads an executable that does not require an address is not measured; `0x400000` is the base that executables reading their GOT through `app0/imports.txt` assume (#4013). The relinker refuses an image that spans 2 GiB or more, so the executable ends below the guest arena at `0x200000000`. With `--lazy-binding` the executable stays position independent. +- `--frame-red-zone` frames only leaf functions that start with a chain of pushes of at least five bytes (`push rbp; mov rbp, rsp; push ...`, or pushes alone, which may be followed by `sub $n, %rsp` or `lea -n(%rsp), %rsp`) and leave through exits of at least five bytes that undo that allocation with `add` or `lea`, pop the same registers and `ret` ([framer](../../../core/relinker/codegen/src/x86/RedZoneFramer.cpp)). Leaves with a frame pointer that allocate, leaves with neither pushes nor an allocation, shorter prologues and exits, exits that free another size, and functions with indirect jumps or other RSP arithmetic keep their red zone. +- In a red-zone leaf without a frame pointer, `--frame-red-zone` lowers RSP after the prologue by the deepest displacement below RSP, rounded up to 16, and moves every RSP operand by the same amount ([framer](../../../core/relinker/codegen/src/x86/RedZoneFramer.cpp)). A leaf that takes the address of a slot below RSP, or uses an RSP operand with an index register, is lowered by the whole 128 bytes, since a pointer or an index can reach deeper than any displacement; values below RSP reached through any other register are not seen. Leaves with a frame pointer are lowered by 128 bytes. Inside a moved frame the CFA rules of the guest's unwind tables are off by the shift. +- An RSP operand that `--frame-red-zone` cannot move in place, an 8-bit displacement the shift takes past 0x7f or one without a displacement, moves to a stub with the instructions after it up to five bytes and is re-encoded there with the moved displacement ([framer](../../../core/relinker/codegen/src/x86/RedZoneFramer.cpp)). The leaf is skipped when that site would take in a branch target, a branch, ret or RIP-relative instruction, or another site, and when the operand is EVEX, whose 8-bit displacement is scaled. A game patch over a framed site or a moved displacement is refused. +- In `--frame-red-zone`, function bounds come from the executable's `.eh_frame` FDEs: each function is decoded from its FDE start to its FDE end, or to int3 padding before it, and the relinker stops with an error when the executable has none. Code outside every FDE, such as a function without unwind information, is not framed ([framer](../../../core/relinker/codegen/src/x86/RedZoneFramer.cpp)). - `--to-intel` lowers MCOMMIT to STC plus NOP padding and RDPRU through a runtime-checked stub in the [matcher](../../../core/relinker/codegen/src/x86/Amd64OnlyInstructionMatcher.cpp): AMD APM Vol. 3 defines RDPRU ECX 0 as MPERF and ECX 1 as APERF, neither readable on Intel hosts, so both selectors return the host TSC as a disclosed monotonic-counter approximation (their ratio always reads 1.0) while other selectors yield zero EDX:EAX. STC asserts CF=1 because under x86-TSO prior stores are already ordered and guest memory is host-DRAM-backed, leaving the commit-success case as the only observable one. Non-zero RDPRU selectors, the TSC-for-MPERF/APERF substitution, and any console path reporting MCOMMIT CF=0 are unverified. - [Unscoped imports on Windows](../../../core/relinker/elfpatcher/src/windows/WindowsEntryStubBuilder.cpp) - an import without a module that no searched library exports under its name is searched again under the NID of that name, because homebrew built with the [ps5-payload-dev SDK](https://github.com/ps5-payload-dev/sdk) imports by symbol name (Quake, from the websrv homebrew launcher; Mari0, #6239) while the libraries in `libs/` export NIDs. A dependency named with `.sprx`, `.suprx` or `.debug_prx` loads the `.prx` library of the same stem from `libs/`, and `libkernel_web` also loads `libkernel.prx`, as `libSceLibcInternal` already loads `libc.prx`: AnyPS5's `libkernel_web.prx` exports only two functions, where the console's `libkernel_web.sprx` is a libkernel variant. A name that matches a library's export as written still binds to that export first. macOS resolves these imports through the host loader by name and is unchanged. -- [Unscoped imports on Linux](../../../core/relinker/elfpatcher/src/linux/LinuxElfPatcher.cpp) - in an executable that names a dependency with `.sprx`, `.suprx` or `.debug_prx`, as homebrew built with the [ps5-payload-dev SDK](https://github.com/ps5-payload-dev/sdk) does, an import without a module that is named by symbol is bound to the NID of that name only. Unlike Windows, the name as written is not tried first, because the host loader also searches the host C library, which exports `malloc` or `pthread_create` under their names. A name that a bundled module exports still binds to that module, and such a dependency loads the `.prx` library of the same stem. Unqualified imports of other executables, and imports of bundled modules, are still bound by name. -- The length-changing path of the [instruction rewriter](../../../core/relinker/codegen/src/x86/X64InstructionRewriter.cpp) is not used by the [converter](../../../core/relinker/codegen/src/Amd64OnlyConverter.cpp): it does not adjust data-to-code references (relocations, FDEs, jump tables) or segment sizes, so every substitution keeps the instruction length. +- [Unscoped imports on Linux](../../../core/relinker/elfpatcher/src/linux/LinuxElfPatcher.cpp) - in an executable or bundled module that names a dependency with `.sprx`, `.suprx` or `.debug_prx`, as homebrew built with the [ps5-payload-dev SDK](https://github.com/ps5-payload-dev/sdk) does, an import without a module that is named by symbol is bound to the NID of that name only. Unlike Windows, the name as written is not tried first, because the host loader also searches the host C library, which exports `malloc` or `pthread_create` under their names. A name that a bundled module exports still binds to that module, as does a bundled module's import of a name the executable exports, and such a dependency loads the `.prx` library of the same stem. Unqualified imports of other executables and bundled modules are still bound by name. +- The length-changing path of the [instruction rewriter](../../../core/relinker/codegen/src/x86/X64InstructionRewriter.cpp) is not used by the [converter](../../../core/relinker/codegen/src/Amd64OnlyConverter.cpp): it does not adjust data-to-code references (relocations, FDEs, jump tables) or segment sizes, so every substitution keeps the instruction length. The rewriter conservatively rejects length-changing edits when a surviving truncated relative branch would move relative to its target (only one of the two lies after the cut) or when its section-relative target falls outside the 16-bit range, because the runtime address base needed to resolve a wrapped destination is unavailable; truncated branches that keep their distance and stay in range are accepted. - The [converter](../../../core/relinker/codegen/src/Amd64OnlyConverter.cpp) takes the branch targets that keep an instruction in place from a linear scan of the code segment, so data in that segment decodes as branches too: OpenSSL's x86-64 assembly keeps strings such as "SHA1 block transform for x86_64, CRYPTOGAMS by " in its code, and in SuperTuxKart (PPSA69421) and Mortalfin (PPSA99141) the `jae` read from "sf" points into the VEX prefix of a 256-bit store. A direct branch target inside a scanned instruction counts only where every byte before it is a legacy prefix or REX, which a real branch can skip (a `lock` prefix, for example); a target in a VEX prefix, opcode or operand is ignored, as a jump-table entry that starts no instruction already is. A real branch to such a target, overlapping instructions as hand-written code can, is not seen, and the instruction it enters can be moved. - A `PT_SCE_RELRO` segment that no `PT_LOAD` covers, the PS4 layout, is [loaded](../../../core/relinker/relinker/src/parsing/ElfReader.cpp) as a read-write `PT_LOAD` and stays writable after relocation; FreeBSD rtld makes `PT_GNU_RELRO` read-only once relocation is done, and what the console does with `PT_SCE_RELRO` is not checked. - In Windows output, a guest instruction that addresses memory through GS becomes `UD2` and NOP padding of its own length ([TLS builder](../../../core/relinker/elfpatcher/src/windows/WindowsTlsBuilder.cpp)): the host owns GS, and the guest GS base that FreeBSD's `sysarch(AMD64_SET_GSBASE)` would set is not emulated. A converted module runs until such an instruction is reached, which then faults. Prospero Win (PPSA99995) has six in its Wine `ntdll`. @@ -25,11 +31,14 @@ - Read-only data can also sit between the unwind functions of such images. There, a `syscall`, `sysenter` or `int 0x80` pattern fails the [syscall scan](../../../core/relinker/relinker/src/analysis/SyscallScanner.cpp) only when a constant syscall number is set before it; the others, and every `sysret`, are skipped and counted in one message. The number is looked for in the decoded instructions and, whatever alignment the decoding took, in the bytes before the pattern: a `mov eax, imm32`, `mov rax, imm32`, `mov rax, imm64` or `xor eax, eax`, optionally followed by `mov al, imm8` or `mov ax, imm16`, then by any number of register moves or zeroings that leave `rax` alone. A real syscall without an unwind entry whose number is set otherwise is not refused and runs on the host: from memory or another register (`mov eax, ecx`), by `push`/`pop` or arithmetic (`or eax, imm32`), or with any other instruction between the load and the pattern. - In the [code analysis](../../../core/relinker/relinker/src/analysis/CodeInstructionCollector.cpp) of the Windows and macOS outputs for such images, a relocation or `lea` target outside every unwind function is followed as code only when its linear decoding, up to the first jump, return or trap, the next unwind function or 256 instructions, holds an `fs:[disp32]` access, or when it is 16-byte aligned, starts less than 256 bytes after an unwind function and ends in a jump, return or trap. Every other such target is taken as data and counted in one message, and the Windows TLS rewrite fails instead of overwriting one. A TLS access of another form, such as `mov rax, fs:[rax]`, or a `gs` instruction in code reached only through such a target is therefore not rewritten or trapped. Other fs and gs instructions do not tell code from data there: string bytes decode as these prefixes, and in two measured titles about 2,500 of about 5,500 such targets decode to one before their first jump. - In executables and guest modules with a `PT_SCE_DYNLIBDATA` segment, the [code analysis](../../../core/relinker/relinker/src/analysis/CodeInstructionCollector.cpp) of the Windows and macOS outputs follows code only from the end of `PT_INTERP`, or of the `/libexec/ld-elf.so.1` string that begins the segment when no `PT_INTERP` covers it, as in OpenOrbis modules, to the end of the PLT after the last function of the unwind table, or to the end of that function when the PLT comes before the code, as in the `libSceFios2.prx` and `libc.prx` that CUSA02636 bundles (PLT at 0xc8); the pipeline's scans of executables above stop at this same end, also when the PLT comes before the code. A guest module with PLT relocations and no PLT before or after its unwind functions, or with a PLT that overlaps one, fails as the executable does. Code after that end is not analyzed, such as the `DT_FINI` routine without an unwind entry that follows the last unwind function in both modules and that the Windows and macOS outputs call when the program exits: a TLS access in it is not rewritten, the ELF entry point, `DT_INIT`, `DT_FINI`, a branch or a pointer into it is ignored, and a function symbol or init/fini array entry pointing into it fails the conversion. +- Under Rosetta (M5, macOS 27.0.1), a VMASKMOVPS, VMASKMOVPD, VPMASKMOVD or VPMASKMOVQ store or load that faults on an element other than an active first one ends the process (`unexpectedly need to EmulateForward on a synchronous exception`) instead of raising SIGSEGV or SIGBUS, so a masked store into a page the macOS write watch keeps read-only ends the title. `--to-rosetta` moves each masked store to a [stub](../../../core/relinker/codegen/src/x86/MaskedStoreLowering.cpp) that first writes to the first and last byte of every active element with `LOCK OR` of 0, which faults normally and changes nothing; each store then costs up to 16 locked byte writes. A page made read-only again by another thread between those writes and the store still ends the process. A stack-relative displacement that exceeds signed disp32 after the spill frame throws. Masked loads are not converted. - A relocation against a dynamic symbol entry that is all zero, like the null symbol, takes the value 0 plus its addend, as the gABI defines for `STN_UNDEF` ([pipeline](../../../core/relinker/relinker/src/pipeline/RelinkerPipeline.cpp)). The Linux output imports such an entry as an unnamed weak symbol, which the host loader resolves to 0; the Windows and macOS outputs write the value into the slot ([relocation builder](../../../core/relinker/elfpatcher/src/windows/WindowsRelocationBuilder.cpp)). What the console's loader does with such an entry is not measured. OliSe Player (PPSA01153) has seven of them, and its code calls through those slots only when they are not 0 ([#4437](https://github.com/boykopovar/AnyPS5/issues/4437)). - In macOS output, a guest instruction that addresses memory through GS becomes `UD2` and NOP padding of its own length ([macOS image writer](../../../core/relinker/elfpatcher/src/macos/MacOsImage.cpp)): the host owns GS, which holds the pthread TSD base the guest TLS stubs read, and the guest GS base that FreeBSD's `sysarch(AMD64_SET_GSBASE)` would set is not emulated. A converted module runs until such an instruction is reached, which then faults. Prospero Win (PPSA99995) has six in its Wine `ntdll`. - The Linux placement of the `--to-intel` stubs in the [ELF patcher](../../../core/relinker/elfpatcher/src/linux/LinuxElfPatcher.cpp) is covered only by a synthetic test. - `--to-rosetta` lowers what Rosetta lacks on top of `--to-intel` ([matcher](../../../core/relinker/codegen/src/x86/Amd64OnlyInstructionMatcher.cpp)). AVX2, FMA, BMI1/2, F16C and MOVBE were observed to execute on an M5 Max without being reported through CPUID; this is not a guarantee for other Rosetta hosts. ANDN and RDRAND fault on an M1 running macOS 15.3.1 and execute on an M1 running macOS 26.6.2, so on the M1 the difference follows the macOS version; on that host BEXTR, BLSI, BLSMSK, BLSR, TZCNT, LZCNT, POPCNT, BZHI, MULX, PDEP, PEXT, RORX, SARX, SHLX, SHRX, MOVBE, FMA, AVX2 and F16C execute as well, its CPUID leaf 1 reports none of MOVBE, FMA, AVX or F16C, and RDSEED, RDPID, CLWB, ADCX and ADOX raise SIGILL. They also execute on an M5 running macOS 26.4, where the rest of BMI1 and BMI2, LZCNT, PREFETCHW, CLFLUSHOPT, AVX, AVX2, FMA, F16C and MOVBE execute as well although CPUID leaf 7 reads zero and leaves 1 and 0x80000001 report none of them, and where RDSEED, CLWB, RDPID, ADCX and ADOX raise an illegal instruction. ANDN is lowered, but RDRAND and other BMI instructions still depend on the host. RDSEED becomes RDRAND and CLWB a NOP. RDPID returns processor 0, and RDTSCP runs unchanged with ECX set to 0 after it, because Rosetta's TSC_AUX is the constant 0xAAAAAAAB (also measured on an M5, macOS 27.0.1). An RDPID or RDTSCP site with no movable instruction after it for the jump is kept; a kept RDTSCP reports 0xAAAAAAAB. ADCX/ADOX fault under Rosetta and are not converted: compilers do not emit them, and code that checks CPUID sees no ADX. The conversions are covered by the converter tests. ANDN lowering is also executed by an x86-64 test build under Rosetta on the M1; the other conversions were originally checked on an M5 Max, outside this repository's CI. +- `--to-rosetta` output faults on an M2 running macOS 14.8.9 when it reaches AVX, AVX2, FMA, F16C, RDRAND or a BMI1/BMI2 instruction other than ANDN, TZCNT and LZCNT: Rosetta on that host raises SIGILL for them and for RDSEED, RDPID, CLWB, ADCX and ADOX ([matcher](../../../core/relinker/codegen/src/x86/Amd64OnlyInstructionMatcher.cpp)). Only TZCNT, LZCNT, POPCNT, MOVBE, CLFLUSHOPT, PREFETCHW and RDTSCP execute, and RDTSCP sets ECX to 0xAAAAAAAB. CPUID leaf 1 reports POPCNT and none of MOVBE, AVX, FMA, F16C or RDRAND, leaf 7 reads zero, and leaf 0x80000001 reports RDTSCP but neither LZCNT nor PREFETCHW. An M2 Max on macOS 27.2 reports the same CPUID but executes everything except RDSEED, RDPID, CLWB, ADCX and ADOX, so the faults follow the macOS version, not the chip. - `--to-intel` replaces the approximate legacy SSE and VEX.128 register forms of RSQRTPS/RCPPS and VRSQRTPS/VRCPPS with [correctly rounded](../../../core/relinker/codegen/src/x86/ReciprocalLowering.cpp) 1/sqrt(x) and 1/x, not with the AMD approximation: its tables are not public. Intel's approximation of 1/sqrt(1.0) is 0x3F7FF000, and two Newton-Raphson steps on it settle on 0.99999994 instead of 1.0; a renormalised identity quaternion then turns into a NaN axis in PPSA21564, which the console does not show. Refined results now reach the exact value; unrefined ones can still differ from the console in the low bits. Each site costs an out-of-line stub (two jumps, a spill below the red zone, SQRTPS/DIVPS). Legacy SSE forms preserve the destination's upper YMM bits; VEX forms clear them. The 256-bit, scalar and memory forms keep the native instruction, as does a site shorter than 5 bytes with no movable instruction after it for the jump (5 sites in PPSA21564, counted before a following RIP-relative instruction could move into the stub; not recounted). +- XSAVEC raises an illegal instruction under Rosetta and is not converted, because it writes the compacted format. XSAVEOPT, which also faults there, is rewritten to XSAVE in place by the [matcher](../../../core/relinker/codegen/src/x86/Amd64OnlyInstructionMatcher.cpp) under `--to-rosetta` (`0F AE /6` becomes `/4`; prefixes and operand are kept). The two differ only in the init and modified optimizations, which let XSAVEOPT skip writing unchanged or initial state ([Intel SDM volume 1](https://cdrdv2-public.intel.com/812382/253665-sdm-vol-1.pdf), chapter 13), so XRSTOR restores the same state from either area. Measured on one M5 with macOS 27.0.1: XSAVE and XRSTOR handle x87, SSE and AVX state in the standard format, although CPUID leaf 1 reports neither XSAVE nor OSXSAVE and leaf 0xD sub-leaf 1 reports neither XSAVEOPT nor XSAVEC. The converter tests' x86-64 build executes the replacement XSAVE under Rosetta on that host. - `--trace-imports` ([Windows entry stub](../../../core/relinker/elfpatcher/src/windows/WindowsEntryStubBuilder.cpp), [Linux entry stub](../../../core/relinker/elfpatcher/src/linux/LinuxElfPatcher.cpp)) exists for Windows and Linux output, not macOS. Only imports bound through a PLT slot, or through a GOT slot whose symbol is a function, are traced; a function reached through any other relocation, or looked up at run time with `sceKernelDlsym`, is not, and neither are weak imports or the imports of modules loaded at run time. On Linux the entry stub makes the page of every traced slot readable, writable and executable with `mprotect` before it stores the thunk, so a slot inside `PT_GNU_RELRO` can be replaced; those pages stay writable, and data pages become executable, while tracing is on - [ANDN lowering](../../../core/relinker/codegen/src/x86/AndnLowering.cpp) uses scalar NOT and AND for 32- and 64-bit register and memory operands under `--to-rosetta`, following the ANDN entry in [Intel SDM volume 2A](https://cdrdv2-public.intel.com/812383/253666-sdm-vol-2a.pdf). SF and ZF follow the result, CF and OF clear, and AF and PF are undefined. The scratch register is saved below the 128-byte red zone. A 64-bit stack-relative displacement that exceeds signed disp32 after reserving the spill frame throws; other BMI instructions are not lowered. The M1 test covers register overlap, RSP operands, memory addressing, RIP relocation, flags and red-zone preservation; precise fault delivery inside a spill sequence has not been verified. diff --git a/docs/dev/debt/shader.md b/docs/dev/debt/shader.md index cd3bca4fdb..6919cefeed 100644 --- a/docs/dev/debt/shader.md +++ b/docs/dev/debt/shader.md @@ -14,7 +14,7 @@ - [MSAA-address image loads on 2D textures](../../../core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp) - 32-bit `image_load` with DIM 2D_MSAA reads a non-multisampled 2D T# as the same DIM 2D load, ignoring the sample index. Measured on RX 6650 XT (gfx1032), wave32 and wave64, for R32_UINT, R32_FLOAT, RGBA8_UNORM and RGBA8_SRGB, sample indices 0/1/2/7/31/0xffffffff and in-range/out-of-range coordinates; IEEE 0, DX10 clamp 1, denorm32 0, denorm16 3, round32/16 0, fp16 overflow 0. Pinned in ImageMsaaAddressOnPlane.cpp. Not measured on gfx1013. Stores, atomics, mip-bearing or A16 addresses, packed accesses, converted formats, depth-bit views, array-MSAA addresses and image-table accesses are not enabled by this compatibility mode. - [MUBUF/MTBUF descriptors ending in VCC](../../../core/shader/recompiler/RdnaDecoder/src/RdnaMemoryOpDecoder.cpp): the four dword descriptor at s104 is s104, s105, vcc_lo and vcc_hi, as LLVM's [VCCTuple/SReg_128](https://github.com/llvm/llvm-project/blob/d8219b47422f7322db252ad29ca8ea87993fb433/llvm/lib/Target/AMDGPU/SIRegisterInfo.td#L422) and [buffer resource operands](https://github.com/llvm/llvm-project/blob/d8219b47422f7322db252ad29ca8ea87993fb433/llvm/lib/Target/AMDGPU/BUFInstructions.td) describe. Existing translation maps scalar codes106/107 to the raw VCC words. Recompiled loads/stores, both words, bounds, formats and wave32/wave64 execute on NVIDIA and AMD Vulkan; raw gfx10/gfx1013 hardware is not measured. Higher descriptor bases remain refused; the normal SGPR bound is unchanged. -- [Packed pixel ancillary word](../../../core/shader/recompiler/Optimization/src/ConstantFolder.cpp) (shader recompiler) - the sample index at bits 8-11 and the render target index at bits 16-28 follow #295. The sample index is the SampleId builtin in a pixel shader the guest iterates per sample (`PS_ITER_SAMPLE` on a multisampled draw), an equality inferred, not measured. In any other pixel shader it is 0, the iterated sample number of a pixel the hardware does not iterate (also inferred), so reading it does not make the host shade per sample. The render target index is the Layer builtin when the draw exports it (a layered draw, `PA_CL_VS_OUT_CNTL` bit 18; the pipeline specialization constant `LayerExported`) and 0 otherwise, the first layer Vulkan renders when no pre-rasterization stage writes Layer. Reading the unwritten Layer builtin instead makes AMD's Windows driver drop the fragment's writes (#4800); with the constant at 0 the load is folded away. Mesa reads both fields from those bits, with the layer 14 bits wide only from GFX12 on ([ac_nir_lower_intrinsics_to_args.c](https://gitlab.freedesktop.org/mesa/mesa/-/blob/bffd91947efc1932da99aca69fc540d6d4201b0c/src/amd/common/nir/ac_nir_lower_intrinsics_to_args.c#L342-L368)). A value passed through a select, move or phi whose every read is an extract inside one of those fields is rebuilt from the two fields alone. Any other read the masked-select pass cannot drop gets the whole word: the primitive class at bits 0-1 (0 point, 1 line, 2 triangle, 3 rectangle), which the draw sets as a pipeline specialization constant ([RasterPrimitiveOf](../../../core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp)) in the shaders that read it, plus the sample field, with every other bit zero. That layout comes from the RDNA 3 and 3.5 ISA guides (prim type 1:0, VRS rates 3:2 and 5:4, eye id 7, sample 11:8, render target index 28:16). The GCN register guides give the same prim type and sample fields and put the render target index at 26:16, so the two sources agree for layers below 2048. AMD's LLPC reads the primitive class from bits 1:0 with the same encoding ([LowerGlCompatibility.cpp](https://github.com/GPUOpen-Drivers/llpc/blob/40cb8d95ad8d6f7f1652e3fd47d39667594cce08/llpc/lowering/LowerGlCompatibility.cpp#L830-L836)) and without a GFX-level check ([getPrimType](https://github.com/GPUOpen-Drivers/llpc/blob/40cb8d95ad8d6f7f1652e3fd47d39667594cce08/lgc/lowering/LowerInOut.cpp#L5307-L5315)). Nobody has checked it on gfx1013. In the whole word only bits 0-1 and 8-11 have a source. Bits 2-7, 12-15 and 29-31 are assumed zero, not measured: the VRS rates (2-5) and the eye id (7) because the driver supports neither variable-rate shading nor multiview, so a title that enables either would read other bits on the console, and bits 6, 12-15 and 29-31 because no source gives them a meaning. The sample field reads SampleId only in iterated shaders. The whole word has the same 0 at bits 16-28. A change that makes draws export a layer must restore the Layer read in both places +- [Packed pixel ancillary word](../../../core/shader/recompiler/Optimization/src/ConstantFolder.cpp) (shader recompiler) - the sample index at bits 8-11 and the render target index at bits 16-28 follow #295. The sample index is the SampleId builtin in a pixel shader the guest iterates per sample (`PS_ITER_SAMPLE` on a multisampled draw), an equality inferred, not measured. In any other pixel shader it is 0, the iterated sample number of a pixel the hardware does not iterate (also inferred), so reading it does not make the host shade per sample. The render target index is the Layer builtin when the draw exports it (a layered draw, `PA_CL_VS_OUT_CNTL` bit 18; the pipeline specialization constant `LayerExported`) and 0 otherwise, the first layer Vulkan renders when no pre-rasterization stage writes Layer. Reading the unwritten Layer builtin instead makes AMD's Windows driver drop the fragment's writes (#4800); with the constant at 0 the load is folded away. Mesa reads both fields from those bits, with the layer 14 bits wide only from GFX12 on ([ac_nir_lower_intrinsics_to_args.c](https://gitlab.freedesktop.org/mesa/mesa/-/blob/bffd91947efc1932da99aca69fc540d6d4201b0c/src/amd/common/nir/ac_nir_lower_intrinsics_to_args.c#L342-L368)). A value passed through a select, move or phi whose every read is an extract inside one of those fields is rebuilt from the two fields alone. Any other read the masked-select pass cannot drop gets the whole word: the primitive class at bits 0-1 (0 point, 1 line, 2 triangle, 3 rectangle), which the draw sets as a pipeline specialization constant ([RasterPrimitiveOf](../../../core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp)) in the shaders that read it, plus the two fields, with every other bit zero. That layout comes from the RDNA 3 and 3.5 ISA guides (prim type 1:0, VRS rates 3:2 and 5:4, eye id 7, sample 11:8, render target index 28:16). The GCN register guides give the same prim type and sample fields and put the render target index at 26:16, so the two sources agree for layers below 2048. AMD's LLPC reads the primitive class from bits 1:0 with the same encoding ([LowerGlCompatibility.cpp](https://github.com/GPUOpen-Drivers/llpc/blob/40cb8d95ad8d6f7f1652e3fd47d39667594cce08/llpc/lowering/LowerGlCompatibility.cpp#L830-L836)) and without a GFX-level check ([getPrimType](https://github.com/GPUOpen-Drivers/llpc/blob/40cb8d95ad8d6f7f1652e3fd47d39667594cce08/lgc/lowering/LowerInOut.cpp#L5307-L5315)). Nobody has checked it on gfx1013. In the whole word only bits 0-1, 8-11 and 16-28 have a source. Bits 2-7, 12-15 and 29-31 are assumed zero, not measured: the VRS rates (2-5) and the eye id (7) because the driver supports neither variable-rate shading nor multiview, so a title that enables either would read other bits on the console, and bits 6, 12-15 and 29-31 because no source gives them a meaning. The sample field reads SampleId only in iterated shaders. The whole word has the same render target index at bits 16-28 - [Front-face input with `FRONT_FACE_ALL_BITS`](../../../core/shader/recompiler/Translation/src/InstructionTranslator.cpp) (shader recompiler) - with `SPI_BARYC_CNTL.FRONT_FACE_ALL_BITS` set, a back face reads 0 and a front face 0xffffffff. Mesa 22.0 RADV sets the bit ([radv_pipeline.c](https://gitlab.freedesktop.org/mesa/mesa/-/blob/mesa-22.0.0/src/amd/vulkan/radv_pipeline.c#L5953)) and ACO tests the input with `v_cmp_lg_u32 0` ([aco_instruction_selection.cpp](https://gitlab.freedesktop.org/mesa/mesa/-/blob/mesa-22.0.0/src/amd/compiler/aco_instruction_selection.cpp#L8123-L8128)), so only back = 0 and front ≠ 0 are sourced; PPSA05684's two-sided cloth shaders test it with `v_cmp_lt_u32 0`. The exact front value is not measured. Without the bit the input is ±1.0, as current Mesa reads it (`fgt 0`, `fsign`; [ac_nir_lower_intrinsics_to_args.c](https://gitlab.freedesktop.org/mesa/mesa/-/blob/bffd91947efc1932da99aca69fc540d6d4201b0c/src/amd/common/nir/ac_nir_lower_intrinsics_to_args.c)) - [Color comparison descriptor forms](../../../core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp): for formats AnyPS5 can sample as floating-point from GFX10 codes 1–77, `image_sample_c*` returns 0 for every comparison function except codes 7 (`16_UNORM`) and 22 (`32_FLOAT`), which use the native comparison path. The split was measured with point sampling on RDNA2 (gfx1031 and gfx1036) and with point and bilinear sampling on RDNA3 (gfx1100); the PS5's gfx1013 and 2D-array/cube comparisons across these formats remain unverified. Codes 128–130 (8-bit sRGB) also return 0, measured on gfx1035 over references -0.5..1.5 and texel codes 0..255. Scaled, integer and formats requiring converted sampling remain unsupported, although the tested hardware formats also return 0 for them. Anisotropic comparison of the remaining formats has not been measured. - [s_setkill](../../../core/shader/recompiler/RdnaDecoder/src/RdnaScalarOpDecoder.cpp) (core/shader/recompiler) - decoded as `s_endpgm` for any immediate: measured on RDNA2 hardware (gfx1035), a store before it is visible and nothing after it runs ([#771](https://github.com/boykopovar/AnyPS5/pull/771)); whether the KILL status bit it sets is observable differently from `s_endpgm` is unverified @@ -35,6 +35,7 @@ - [Windows cache housekeeping](../../../core/shader/recompiler/ShaderDiskCache.cpp): eviction of old versions and temporary files is skipped for `\\?\` and `\\.\` cache roots because the supported WinLibs directory iterator can enumerate the process working directory for long prefixed paths. The active version's `last-used` stamp update still runs; it fails for long `\\.\` aliases with the supported toolchain. Ordinary Windows directory enumeration remains subject to MAX_PATH. - [f64 compares](../../../core/shader/recompiler/Translation/src/CompareInstructions.cpp) flush denormal inputs to signed zero when bit 0 of the `FLOAT_MODE` f16/f64 denormal field is clear (modes 0 and 2); modes 1 and 3 and an unknown mode keep them. This follows the RDNA2 ISA section 6.4 and LLVM's sign-preserving AMDGPU denormal modes. The gfx1036 audit in [#981](https://github.com/boykopovar/AnyPS5/issues/981#issuecomment-6070046381) reports mode-sensitive `VopcCompareF64` results. Class compares still inspect the original bits. The full four-mode matrix has not been newly measured on AMD hardware or the PS5's gfx1013. +- [Shader disk cache source version](../../../core/shader/recompiler/ShaderCacheVersion.cmake) - the version that names `shader_cache/` and stamps `pipelines.journal` hashes every file under `core/shader/recompiler` (line endings normalized), `HostThreadLocal.hpp`, the SPIR-V and glslang submodule commits, and the compiler, build type and flags. Any edit there changes it and empties the cache, including edits that cannot change a translation (logging, the disk cache's own I/O, the recompiler's tests), and so does a rebuild with another compiler or flags. Driver sources outside the recompiler are not hashed: their recompiler calls are only checked against `shaderCacheReviewedNames`, so a driver change that alters a `RecompileRequest` field's meaning without changing its value keeps a stale cache. - [Unlinked calls](../../../core/shader/recompiler/ControlFlow/src/GraphBuilder.cpp) - the `s_swappc_b64` a table call's compare chain falls back to, when its target matches no linked copy, records the `UnlinkedCall` fault with the target and the program counter and stops the invocation, or continues after the call in programs whose faults do not stop invocations (see `bdaStopsInvocations`). The driver lists those calls in the request (`linkedCallFallbacks`), and it reports the fault like a BDA fault, naming the target: a dispatch that checks its faults when it completes throws, one whose check runs in a deferred write-back logs it and continues. The hardware would jump to the target, which is not modelled. Any other `s_swappc_b64` whose target is not resolved still throws when the shader is compiled. - [Cached source IR](../../../core/shader/recompiler/Recompiler.cpp) releases lazily allocated SSA construction tables after resource preparation and reclaims values unreachable from instructions, metadata and resource plans when retaining a pending source program. Immediate shader preparation skips reclamation because compilation consumes the program. Reclamation must account for every new IR pointer owner; a remaining external use throws. Live IDs are retained. Source and variant entries remain cached without an eviction bound. A pending source program is released after its first variant is obtained; memory beyond the measured title checkpoint and Linux performance remain unmeasured. @@ -68,10 +69,12 @@ - [MIMG BY2/BY4 loads](../../../core/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cpp) (`image_load[_mip]_by2/by4`, opcodes 0x42, 0x43, 0x4a and 0x4b) were measured on RDNA2 (gfx1035) with the [hardware oracle](../HW_ORACLE.md) for 2D images with identity swizzle. They read N texels (BY2: 2, BY4: 4) from `x & ~(N - 1)` on row y, texel by texel and component by component within each texel, with each texel converted like `image_load`. Every register is zero when the mip level is past the last level, `x + N - 1` is outside the level's width (unaligned x, unsigned), or y is outside its height. Data only comes back when the N texels fit in one dword: R8 and R16 (DMASK 0x3) or RG8 (0xf) for BY2, and R8 (0xf) for BY4. With 32-bit texels (R32, RG32, RG16, RGBA8) the hardware returns zeros. Other formats, masks, dimensions, swizzles, D16/A16 and indirect descriptors throw. The stores (0x52, 0x53, 0x5a and 0x5b) decode but throw at translation. Measured on hardware, they write from the unaligned x up to the end of its dword, which is not implemented. - [2D stores to 1D storage images](../../../core/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cpp): an `image_store` with DIM 2D through a 1D T# writes texel x and ignores y, as measured on RDNA2 (gfx1035) for y from 0 to 4 and 0xffffffff, single- and four-component formats and a 1x1 8_8_8_8_UNORM T#: the T# type decides the addressing, DIM only how many address VGPRs are read. Storage loads, atomics and `image_store_mip` through such a pair still throw - [MIMG PCK2/PCK4 loads](../../../core/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cpp) (`image_load[_mip]_pck2/pck4`, opcodes 0x70, 0x71, 0x73 and 0x74) were measured on RDNA2 (gfx1035) with the [hardware oracle](../HW_ORACLE.md) for 2D images with identity swizzle. With data mask 0x1, they write one dword: the raw bits of the N texels (PCK2: 2, PCK4: 4) from `x & ~(N - 1)` on row y, concatenated (element k at bit `k * texel bits`) and zero-extended. The dword is zero when the mip level is past the last level, `x + N - 1` is outside the level's width (unaligned x, unsigned), or y is outside its height. Data only comes back when N texels fit in 32 bits: R8, R16 or RG8 for PCK2, R8 for PCK4. Wider texels (R32, RG16, RGBA8) and masks other than 0x1 write zeros on hardware. Those masks throw at decode, and formats outside UNORM/UINT/SINT R8, R16 and RG8 throw. #1314's earlier measurement saw only zeros because it used masks 0x3 and 0xf. The stores (0x76, 0x77, 0x79 and 0x7a) decode but throw at translation. They are not measured, and opcode 0x76 on 32_32_32_32_FLOAT hung the GPU (#1314). +- [`image_get_resinfo` through DIM 1D on a cube, 2D-array or 3D T#](../../../core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp) follows thuanquang's measurement on RDNA2 (RX 6700 XT, gfx1031, #7550), not the PS5's gfx1013: width, height and the level count come from the T#, and the third component is the 3D T#'s depth, or 0 for a cube or 2D array, as for every DIM without layers (1D, 2D, 2D MSAA). Such a query on a 1D-array T# still reads it as its first layer, whose height is not measured. On a 2D MSAA or 2D MSAA-array T# it throws: gfx1031 was measured only with one fragment, which the driver cannot bind, and the level component for 2, 4 or 8 fragments is not measured. - [3D image accesses bound to a 2D or 2D-array T#](../../../core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp) follow CouchRiv's [hardware oracle](../HW_ORACLE.md) measurement on RDNA2 (gfx1036, #2023) and adriadam10's (gfx1035, #2140), not the PS5's gfx1013. On a 2D T#, `image_sample_lz` ignores r and reads the (u,v) texel, `image_load` and `image_load_mip` read the (x,y) texel when z is 0 and return zero otherwise, and `image_get_resinfo` returns (width, height, 1, levels). On a 2D-array T#, these accesses behave like dim 2D-array: r or z is the layer, and `image_get_resinfo` returns the layer count as its third component. Every other access of such an image, including samples with an LOD, bias, derivatives, offset or comparison, gathers, `image_get_lod` and stores, keeps throwing because it is not measured. -- [2D-array samples through a 3D T#](../../../core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp) (`image_sample`, `image_sample_l` and `image_sample_lz` with DIM 2D_ARRAY) sample the volume as DIM 3D does: the third address VGPR is the normalized r coordinate, addressed by the sampler's Z mode (only wrap was measured), not a layer index, and a plain sample in a pixel shader takes its LOD from the derivatives of all three coordinates. Measured on RDNA2 (RX 6700 XT, gfx1031, driver 32.0.21045.5002, Windows 11) with the [hardware oracle](../HW_ORACLE.md) template run through the driver's comgr and HIP runtime, not on the PS5's gfx1013: on 16x16x4 R32_FLOAT 3D T#s with one and three levels, the three instructions with DIM 2D_ARRAY returned the same value as with DIM 3D in all 256 lanes of 66 cases (point, bilinear and z-linear filters, wrap addressing, r from -0.125 to 3, quad steps of 1 to 4 texels in u and v and of 0 to 4 slices in r, LOD 1). Other samples of such a pair (bias, LOD clamp, derivatives, offsets, comparison, 16-bit addresses), gathers, `image_get_lod` and a sample mixed with a load of the same image keep throwing because they are not measured. The host sampler ignores `Z_FILTER`, as for every 3D sample, so r is filtered with the XY filter. +- [2D-array samples through a 3D T#](../../../core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp) (`image_sample`, `image_sample_l` and `image_sample_lz` with DIM 2D_ARRAY) are measured only on RDNA2 (gfx1031, [hardware oracle](../HW_ORACLE.md)), not on the PS5's gfx1013, and only with wrap addressing. They sample the volume as DIM 3D does: the third address VGPR is the normalized r coordinate, addressed by the S#'s `CLAMP_Z`, not a layer index. The host sampler ignores `Z_FILTER`, as for every 3D sample, so r is filtered with the XY filter. Other samples through such a pair (bias, LOD clamp, derivatives, offsets, comparison, `_a`, 16-bit addresses), gathers, `image_get_lod` and `image_get_resinfo` throw, as do the three samples of a T# selected from an image table, of a T# the program also loads with DIM 2D_ARRAY, and of an 11_11_10_UNORM, 11_11_10_UINT or depth-bits T#. +- AMD image descriptors with `TYPE=0` are treated as null for [read-only image slots](../../../core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp) even when the base and other words are nonzero: `image_load` on a gfx1035 with such a T# returns 0,0,0,0, and the same descriptors were seen in direct reads in PPSA01341 on an RTX 4070. Writable and atomic image slots and entries of an image table keep faulting because there is no hardware measurement for them, and the reserved types 2-7 also fault. `TYPE=1` is treated as null in the same read-only slots: gfx1035 reads it as zeros too, and gfx1010 returns zeros for `image_sample_lz`, `image_sample_b`, `image_load` and `image_get_resinfo` with format 22 (#5828); it is not measured on RDNA2, with other formats or with depth-compare reads (`image_sample_c*`). gfx1013 behavior is unverified - [2D accesses through a cube T#](../../../core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp) (#4139) read the T#'s `BASE_ARRAY` face as a cube access with face coordinate 0 does, measured on RDNA1 (gfx1010, RX 5700 XT) with the [hardware oracle](../HW_ORACLE.md) template run through the Windows driver's comgr and HIP runtime on 8x8 and 32x32 32_FLOAT cubes (`DEPTH` 5 and 11, `BASE_ARRAY` 0 and 6): `image_sample_lz`, plain `image_sample` (one level, and six levels in 2x2 compute quads) and `image_sample_l` (the third address VGPR is the LOD, integer and fractional) with DIM 2D equal DIM CUBE with face 0 on every row, with point and bilinear filters, wrap, clamp and mirror addressing and `DISABLE_CUBE_WRAP` 0 and 1, and the VGPR after the two coordinates is not read; `image_load` with DIM 2D reads texel (x, y) of the `BASE_ARRAY` face. So the face coordinates are cube coordinates in [1, 2]: the texel is `floor((u - 1) * width)`. They are translated through the cube mode DIM CUBE accesses use, which samples the face of the 2D-array view at (u - 1, v - 1) with the S# addressing. That matches the hardware inside the face, except within half a texel of its edges with a bilinear filter and `DISABLE_CUBE_WRAP` 0, where the hardware blends texels of the neighbouring faces (also measured on RDNA2, gfx1035, 8x8 cubes with `BASE_ARRAY` 0 and 6). Outside it the hardware clamps a coordinate below 1 to the first texel under every filter, wrap mode and `DISABLE_CUBE_WRAP` setting measured, clamps a coordinate of 2 or more to the last texel when `DISABLE_CUBE_WRAP` is 0 (point filter) and applies the S# addressing to it when it is 1; the translation does none of this, as for DIM CUBE. Only `image_sample_lz`, plain `image_sample`, `image_sample_l`, mip-less `image_load`, and mip-less `image_store` to an image the program only stores to take this mode. Such a store writes texel (x, y) of the `BASE_ARRAY` face, measured on RDNA2 (gfx1036, [by simonbbby](https://github.com/boykopovar/AnyPS5/pull/6799#issuecomment-6217609589), not the PS5's gfx1013) with the [hardware oracle](../HW_ORACLE.md) on 8x8 mip-less cube arrays (`DEPTH` 11) of 32_UINT, 32_FLOAT and 16_16_16_16_FLOAT ([the last also by simonbbby](https://github.com/boykopovar/AnyPS5/pull/6799#issuecomment-6224023050), with exactly representable half values) with `BASE_ARRAY` 0 and 6, wave32 and wave64: the 64 stored texels land on that face and every other byte of the 64 KiB backing is unchanged. Other formats, and Crimson Desert's own 256x256 tiled descriptor, are not measured. Other DIM 2D accesses through a cube T# (gathers, bias, comparison, offsets, derivatives, atomics, stores to an image the program also reads, stores of formats written through a conversion such as 32_SINT, mip loads and stores) still throw. -- [Unnormalized S#s](../../../core/shader/recompiler/Optimization/src/DescriptorBindingBuilder.cpp) are accepted only when direct and used only by explicit-LOD `image_sample` without comparison, derivatives or `_a`, on a single-level, single-layer 1D or 2D view of mip 0, even when the image is also sampled through a normalized S#. +- [Unnormalized S#s](../../../core/shader/recompiler/Optimization/src/DescriptorBindingBuilder.cpp) are accepted only when direct and used only by `image_sample` without comparison or `_a`, on a single-layer 1D or 2D view that is one level at mip 0 (even when the image is also sampled through a normalized S#), or that only unnormalized S#s sample, each without a mip filter or only by `image_sample_lz` without minimum LOD or bias. - [Unnormalized texel offsets](../../../core/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cpp) are added to the f32 coordinates: exact on RDNA2 (gfx1035), which applies the offset before the clamp ([measured by adriadam10](https://github.com/boykopovar/AnyPS5/pull/822#issuecomment-6033192725)). - [Cube image gradients](../../../core/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cpp) (shader recompiler) - `image_sample_d` and `image_sample_cd` on a cube pass their gradients to the host sample without the -1.0 that moves the face coordinates from [1, 2] to [0, 1], as Mesa's AMD lowering builds them (`src/amd/common/nir/ac_nir_lower_tex_coords.c`, `prepare_cube_coords`: face-space derivatives with no offset, coordinates offset by 1.5). Measured on RDNA2 (gfx1035) with the [hardware oracle](../HW_ORACLE.md): on a 16x16 32_FLOAT cube with five levels, point mip filter, `image_sample_d` reads the level of `round(log2(16 * max(|ddx|, |ddy|)))` for gradients of 2^(level ± 0.3) texels on each of the four components, both signs, mixed components, every face and two coordinates (1032 rows). `image_sample_cd` takes one level per quad of lanes, from the gradients of its first lane; the host sample computes it per lane. - [Image atomics](../../../core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp) on 32_SINT and 32_FLOAT surfaces act on the raw dword through an R32_UINT storage view. On gfx1100 (RDNA3, [hardware oracle](../HW_ORACLE.md), wave32 and wave64), all 13 integer image atomics, `swap` to `dec`, return the old dword and store the raw-dword result identically on 32_UINT, 32_SINT and 32_FLOAT T#s, over 121 old/data pairs per op, among them 0x80000000, 0xffffffff, 1.0, -1.0 and NaN: `add` adds the bits and `umin` is unsigned on every format. A 16_UINT control differs, so the format is read. gfx1031 (RDNA2) gives the same rows, and its `fmin`, `fmax` and `fcmpswap`, which gfx1100 lacks, also match on the three formats ([measured by thuanquang](https://github.com/boykopovar/AnyPS5/pull/7939#issuecomment-6275099482)). The console's gfx1013 is unmeasured. Other formats throw. @@ -105,16 +108,16 @@ - [Mesh shader grid size](../../../core/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cpp) (shader recompiler) - every mesh shader loads `NumWorkgroups.x` and declares no vertices when it is 0, which no dispatch produces. The read is a workaround for RADV (Mesa 26.2.4, seen on an RX 9070 XT, GFX12): binding a mesh pipeline whose shader reads neither `NumWorkgroups` nor `DrawIndex` keeps the user SGPR count and draw-id flag of the vertex pipeline bound before it in the same command buffer (`radv_bind_pre_rast_shader` updates them only when the shader has the `AC_UD_VS_BASE_VERTEX_START_INSTANCE` slot), so `radv_emit_userdata_mesh` opens a 3-register `SET_SH_REG` and writes one value, the CP takes the following `DISPATCH_MESH_DIRECT` as register data and the GPU hangs (gfx ring timeout, MODE1 reset). Kena: Bridge of Spirits (PPSA01802) hits it on its layered volume draws. Reading `NumWorkgroups` gives every mesh shader the grid-size slot, so the count and the values match; drop the read once a fixed Mesa is the minimum - [Mesh layer output](../../../core/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cpp) (shader recompiler) - a mesh shader writes each primitive's layer through a one-member `Block` struct array whose member is decorated `BuiltIn Layer` and `PerPrimitiveEXT`, as glslang declares `gl_Layer` inside `gl_MeshPerPrimitiveEXT`, not through a standalone `PerPrimitiveEXT` `Layer` array. Both forms pass `spirv-val`, but AMD's Windows driver (32.0.21045.5002, RX 6700 XT) crashes in `amdvlk64.dll` creating a pipeline whose mesh shader writes the standalone form, whether the layer is signed or unsigned and whatever the target, while the block form draws, also with a pixel shader that reads the layer. - [Shader recompilation](../../../core/shader/recompiler/Recompiler.cpp) currently occurs right before it was transferred to Vulkan with caching, but should be moved to the [relinker](../../../core/relinker/main.cpp) stage. For this purpose, [shader/recompiler](../../../core/shader/recompiler) was written completely independently from [libs/prx](../../../core/libs/prx). -- [v_interp_p1ll_f16, v_interp_p1lv_f16 and v_interp_p2_f16](../../../core/shader/recompiler/Translation/src/AttributeInstructions.cpp) read the 16-bit halves of each vertex's parameter through `VK_KHR_fragment_shader_barycentric`, as fixed-function interpolation cannot split a 32-bit attribute. Without the extension the pixel shader takes the [barycentric emulation](../../../core/shader/recompiler/SpirvBackend/src/BarycentricGeometryShader.cpp), whose generated geometry stage passes each vertex's parameter to it as a flat input, so the halves arrive bit for bit; a device without geometry shaders takes the tessellation stages of the same emulation instead, which no 16-bit interpolation test has run through yet. P10 and P20 are the vertex differences rounded to f16 to nearest even, which is not confirmed on hardware. v_interp_p1ll_f16 and v_interp_p1lv_f16 round the exact `P10 * I + P0` once to f32 and v_interp_p2_f16 rounds the exact `P20 * J + S2` once to f16, as measured on RDNA2 (#697); the exact sums are computed in f64 or, on a device without the Float64 capability, in 64-bit integers: the exact product is added with a sticky bit and rounded once. They follow the shader's float mode, measured on RDNA2 (gfx1035) in all eight combinations of `IEEE_MODE` and kept/flushed f32 and f16 denormals over 768 special and 1,024 random rows. With IEEE=1 a NaN operand is returned quieted; with IEEE=0 it is returned unquieted, except that v_interp_p2_f16's f16 result is always quiet. Flushed f32 denormals flush I/J, v_interp_p2_f16's f32 S2 and the f32 result; flushed f16 denormals flush P0, P10/P20, v_interp_p1lv_f16's f16 S2 and the f16 result. Denormal modes 1 and 2 (flush only inputs or only outputs) and rounding other than to nearest even are not measured and throw. An unknown mode follows IEEE=0 with denormals kept. Lavapipe has no barycentrics, so the draw test is skipped in CI; `agc_driver_interpolation_modes` checks the integer arithmetic on every device, and the f64 arithmetic on devices with Float64. It checks both against the hardware rows in every mode, and against a CPU reference on random rows in every mode and f16 rounding. +- [v_interp_p1ll_f16, v_interp_p1lv_f16 and v_interp_p2_f16](../../../core/shader/recompiler/Translation/src/AttributeInstructions.cpp) read the 16-bit halves of each vertex's parameter through `VK_KHR_fragment_shader_barycentric`, as fixed-function interpolation cannot split a 32-bit attribute. Without the extension the pixel shader takes the [barycentric emulation](../../../core/shader/recompiler/SpirvBackend/src/BarycentricGeometryShader.cpp), whose generated geometry stage passes each vertex's parameter to it as a flat input, so the halves arrive bit for bit; a device without geometry shaders takes the tessellation stages of the same emulation instead, which no 16-bit interpolation test has run through yet. P10 and P20 are the vertex differences rounded to f16 to nearest even, which is not confirmed on hardware. v_interp_p1ll_f16 and v_interp_p1lv_f16 round the exact `P10 * I + P0` once to f32 and v_interp_p2_f16 rounds the exact `P20 * J + S2` once to f16, as measured on RDNA2 (#697); the exact sums are computed in f64 or, on a device without the Float64 capability, in 64-bit integers: the exact product is added with a sticky bit and rounded once. They follow the shader's float mode, measured on RDNA2 (gfx1035) in all eight combinations of `IEEE_MODE` and kept/flushed f32 and f16 denormals over 768 special and 1,024 random rows. With IEEE=1 a NaN operand is returned quieted; with IEEE=0 it is returned unquieted, except that v_interp_p2_f16's f16 result is always quiet. Flushed f32 denormals flush I/J, v_interp_p2_f16's f32 S2 and the f32 result; flushed f16 denormals flush P0, P10/P20, v_interp_p1lv_f16's f16 S2 and the f16 result. Denormal modes 1 and 2 (flush only inputs or only outputs) and rounding other than to nearest even are not measured and throw. An unknown mode follows IEEE=0 with denormals kept. `agc_driver_interpolation_modes` checks the integer arithmetic on every device, and the f64 arithmetic on devices with Float64. It checks both against the hardware rows in every mode, and against a CPU reference on random rows in every mode and f16 rounding. - Without `VK_KHR_fragment_shader_barycentric`, a pixel shader whose I/J VGPRs [only feed `v_interp_p1_f32`/`v_interp_p2_f32` of their own pair](../../../core/shader/recompiler/Optimization/src/HostInterpolationChecker.cpp) reads the attribute Vulkan interpolated. Any other read of them or of a raw vertex parameter takes the weights and the three vertex values from a [generated geometry stage](../../../core/shader/recompiler/SpirvBackend/src/BarycentricGeometryShader.cpp), which needs `geometryShader` and a vertex shader before it. On a device with `tessellationShader` but no `geometryShader` (MoltenVK), a triangle list without primitive restart takes them from [generated tessellation stages](../../../core/shader/recompiler/SpirvBackend/src/BarycentricGeometryShader.cpp) instead: each triangle is one three-point patch at tessellation level 1, the evaluation stage writes the corner `gl_TessCoord` points at as the weights, and the vertex clip distances are forwarded: the control stage reads them as the separate `ClipDistance` array the vertex shader writes, so the vertex-to-control interfaces match, and passes them on inside its per-vertex block, which the evaluation stage reads and writes. MoltenVK 1.4.2 (SPIRV-Cross) cannot translate a separate `ClipDistance` array in a tessellation evaluation stage (`no member named 'gl_ClipDistance' in 'main0_out'`), so the two generated stages keep it in their blocks. A flat parameter takes the provoking vertex's value there, the first or with `PROVOKING_VTX_LAST` the last, and every corner of the patch writes it, so the host's own provoking vertex does not matter and `VK_EXT_provoking_vertex` is not needed; a flat per-vertex parameter with the last provoking vertex still throws. The patch keeps the triangle's facing with `VertexOrderCw`, checked with face culling on MoltenVK 1.4.2 (Apple M2) only. A rect-list draw takes them from its own generated tessellation stages; its test passes on MoltenVK 1.4.2 (Apple M2), RADV (Radeon RX 7900 XT, Mesa 25.2.8), lavapipe, and on Windows on an RTX 3080 (NVIDIA 617.42) and Radeon RDNA 2 iGPUs (AMD 32.0.21045 and 32.0.21030); no title was run through it except Ruffle PPSA68091 on the M2. Tessellation and mesh pipelines, primitives other than triangles, triangle fans and primitive restart without `geometryShader`, more than 32 fragment input locations and interfaces beyond the device's geometry or tessellation limits throw. -- [v_fma_f64 and v_div_fmas_f64](../../../core/shader/recompiler/SpirvBackend/src/SpirvAlu/SpirvAluEmitterFloat64.cpp) with a subnormal operand or result round once, as IEEE 754 fusedMultiplyAdd does with f64 denormals kept, measured on RDNA2 (gfx1035) at IEEE=0 and IEEE=1, `v_div_fmas_f64` with VCC clear and set. The f16/f64 denormal field of `FLOAT_MODE` (bits 7:6) is not read: when it flushes, the hardware flushes the operands and the result of `v_fma_f64` (tininess before rounding) and only the result of `v_div_fmas_f64`, while these keep them. Shaders with f64 instructions declare `DenormPreserve` for 64-bit floats only when `shaderDenormPreserveFloat64` is true and Vulkan reports `denormBehaviorIndependence` as `ALL`; other independence modes are not handled by the current backend because they require coupled execution modes. See [VkShaderFloatControlsIndependence](https://docs.vulkan.org/refpages/latest/refpages/source/VkShaderFloatControlsIndependence.html). Elsewhere (including lavapipe), the driver may flush f64 subnormals, and `agc_driver_f64_fma_subnormals` is skipped. -- [v_floor_f64, v_ceil_f64, v_fract_f64, v_frexp_mant_f64 and v_frexp_exp_i32_f64](../../../core/shader/recompiler/Translation/src/Float64Instructions.cpp) follow the `FLOAT_MODE` f16/f64 denormal field: a denormal input is flushed to a zero of its sign when bit 0 is clear (0 and 2), and `v_fract_f64` flushes a denormal result, the fraction of a positive denormal input, to +0 when bit 1 is clear (0 and 1). `v_trunc_f64` and `v_rndne_f64` give the same results either way and are unchanged. Measured on RDNA2 (gfx1031, Radeon RX 6700 XT, AMD Windows driver 32.0.21045.5002 through HIP) at IEEE=0 and IEEE=1 with denorm16 0..3 over 23 f64 values (zeros, denormals, minimum normals, ±0.5, ±1, values next to 1, the largest finite value, infinities and quiet and signaling NaNs) and 16 of them with neg and abs modifiers. Shaders without a known float mode keep the previous lowering. The PS5's gfx1013 is not measured. +- [v_fma_f64 and v_div_fmas_f64](../../../core/shader/recompiler/SpirvBackend/src/SpirvAlu/SpirvAluEmitterFloat64.cpp) with a subnormal operand or result round once, as IEEE 754 fusedMultiplyAdd does with f64 denormals kept, measured on RDNA2 (gfx1035) at IEEE=0 and IEEE=1, `v_div_fmas_f64` with VCC clear and set. The f16/f64 denormal field of `FLOAT_MODE` (bits 7:6) is not read: when it flushes, the hardware flushes the operands and the result of `v_fma_f64` (tininess before rounding) and only the result of `v_div_fmas_f64`, while these keep them. Shaders with f64 instructions declare `DenormPreserve` for 64-bit floats only when `shaderDenormPreserveFloat64` is true and Vulkan reports `denormBehaviorIndependence` as `ALL`; other independence modes are not handled by the current backend because they require coupled execution modes. See [VkShaderFloatControlsIndependence](https://docs.vulkan.org/refpages/latest/refpages/source/VkShaderFloatControlsIndependence.html). Elsewhere the driver may flush f64 subnormals, and `agc_driver_f64_fma_subnormals` skips if it does. +- [v_floor_f64, v_ceil_f64, v_fract_f64, v_frexp_mant_f64 and v_frexp_exp_i32_f64](../../../core/shader/recompiler/Translation/src/Float64Instructions.cpp) follow the `FLOAT_MODE` f16/f64 denormal field: a denormal input is flushed to a zero of its sign when bit 0 is clear (0 and 2), and `v_fract_f64` flushes a denormal result, the fraction of a positive denormal input, to +0 when bit 1 is clear (0 and 1). `v_trunc_f64` and `v_rndne_f64` give the same results either way and are unchanged. Measured on RDNA2 (gfx1031, Radeon RX 6700 XT, AMD Windows driver 32.0.21045.5002 through HIP) at IEEE=0 and IEEE=1 with denorm16 0..3 over 23 f64 values (zeros, denormals, minimum normals, ±0.5, ±1, values next to 1, the largest finite value, infinities and quiet and signaling NaNs), and with neg and abs modifiers over 16 values (13 of those, -1.5, 2.75 and 1 - 2^-53). Shaders without a known float mode keep the previous lowering. The PS5's gfx1013 is not measured. - [v_add_f64 and v_mul_f64 denormals](../../../core/shader/recompiler/Translation/src/Float64Instructions.cpp) follow the `FLOAT_MODE` f16/f64 denormal field; the PS5's gfx1013 is not measured. Inputs flush to a zero of their sign when bit 0 is clear (0 and 2). When bit 1 is clear (0 and 1), a sum below 2^-1022, or a product whose exact value rounded to 53 significant bits with an unbounded exponent is below 2^-1022, flushes to a zero of its sign, so `0x2000000000000001 * 0x1ffffffffffffffe` (exactly 2^-1022 - 2^-1126) rounds to 2^-1022 and is kept. Measured on RDNA2 (gfx1031, RX 6700 XT, AMD Windows driver 32.0.21045.5002, HIP) at IEEE=0 and 1, denorm16 0..3, over 576 pairs of 24 f64 values (zeros, denormals, minimum normals, values next to 1 and to 2^-1022, products just below 2^-1022, the largest finite value, infinities), with neg, abs and clamp. `v_fma_f64`, `v_div_fmas_f64` and shaders without a known float mode keep the previous lowering. - [SINT storage images](../../../core/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cpp) of 8- and 16-bit components saturate each stored 32-bit value to the component range, as adriadam10 measured for image stores on RDNA2 (gfx1035) in #944 (448 rows match); the PS5's gfx1013 is not measured. A D16 store sign-extends each 16-bit half before that saturation, as measured on RDNA2 (gfx1035) for `image_store d16` to all eight SINT formats with DMASK 0xf, 0x7, 0x5, 0x3, 0x2, 0xa and 0x1 (1792 rows match). - [`StoreTexel`](../../../core/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cpp) writes channel 1 of a two-channel format from the data slot whose DST_SEL is Y, for SINT and UINT alike. With DST_SEL X,Y,Z,W, gfx1035 writes it from the W slot, as adriadam10 measured in #944. - [v_ldexp_f16](../../../core/shader/recompiler/Translation/src/FloatInstructions.cpp) follows the `FLOAT_MODE` f16 denormal field: bit 0 preserves the half input and bit 1 the half result. Otherwise a denormal input is flushed to its signed zero, and a result whose exact scaled value is below the minimum normal (2^-14) is flushed to its signed zero before rounding, so `v_ldexp_f16 0x07ff, -1` gives +0 instead of 0x0400. Measured on RDNA2 (gfx1036) with round to nearest, IEEE 0 and 1 and denorm16 0..3, over every half magnitude 0x0000..0x0fff of both signs scaled by 2^-1 and 2^0 (16384 rows per mode): no mismatch. NaN and infinity handling and destination high-half preservation are unchanged. Shaders without a known float mode keep the previous lowering. - [v_div_fixup_f64](../../../core/shader/recompiler/Translation/src/Float64Instructions.cpp) flushes its src1 and src2 to a zero of the same sign when bit 0 of the `FLOAT_MODE` f16/f64 denormal field is clear (0 and 2) and keeps a denormal src0 and result in every mode, as `v_div_fixup_f32` does with the f32 field. Measured on RDNA2 (gfx1031, Radeon RX 6700 XT, AMD Windows driver 32.0.21045.5002 through HIP) over 1728 triples of 12 f64 values (zeros, denormals, the minimum normal, ±1, the largest finite value, infinity and quiet and signaling NaNs) at IEEE=0 and IEEE=1 with denorm16 0..3. Shaders without a known float mode keep the previous lowering. The PS5's gfx1013 is not measured. -- [v_mullit_f32](../../../core/shader/recompiler/Translation/src/FloatInstructions.cpp) follows the hardware measured with the MODE register at IEEE=1 and denormals kept (a signaling NaN src0 is quieted only with IEEE=1, as measured at IEEE=0 too; a positive denormal src2 counts as positive); with f32 denormals flushed it follows the f32 denormals entry below, and denormal modes 1 and 2 throw. +- [v_mullit_f32](../../../core/shader/recompiler/Translation/src/FloatInstructions.cpp) follows the hardware measured with the MODE register at IEEE=1 and denormals kept (a signaling NaN src0 is quieted only with IEEE=1, as measured at IEEE=0 too; a positive denormal src2 counts as positive); with f32 denormals flushed it follows the f32 denormals entry below. Denormal mode 2 flushes only the sources, and mode 1 only the result: a result below 2^-126 after rounding becomes a zero of its sign, also when a preserved denormal source produced it. Measured on RDNA2 (gfx1030) over 13,824 operand sets, and 1,632 with an inline or literal constant factor, in all four modes at IEEE=0 and IEEE=1 with f32 round to nearest even. Modes 1 and 2 throw when the module cannot declare `DenormPreserve 32` (no `shaderDenormPreserveFloat32`, or the hardware f32 flush in use), and mode 1 also with another f32 rounding. - [v_mad_legacy_f32 and v_mac_legacy_f32](../../../core/shader/recompiler/Translation/src/FloatInstructions.cpp) (VOP3 0x140, VOP2 0x06) round the product to f32 before the add, give a +0 product for a zero factor, and flush their f32 denormal sources, their product and their result to a zero of the same sign in every f32 denormal mode, an exception to the `FLOAT_MODE` model in [f32DenormalFlushFor](../../../core/shader/recompiler/Translation/src/VectorInstructions.cpp). Measured on gfx1010 (RX 5700 XT) with `tools/hw-oracle/template.s`, assembled by the AMD Windows driver's `amd_comgr_2.dll` and launched through its `amdhip64_6.dll` instead of `oracle.c` and ROCr, over the 32 rows of the five `FmaLegacy.cpp` and the four `MacLegacy.cpp` encodings: identical in wave32 and wave64 at IEEE 0 and 1 with f32 denormal modes 0 to 3, except that the two signaling NaN rows are quieted only with IEEE=1 (#1281, #1290). A sum of two zeros takes its sign on the bits (+0 unless both are -0), as lavapipe returned -0 for +0 + -0. The PS5's gfx1013 is not measured; LLVM decodes the encoding as `v_mad_legacy_f32` there, as on gfx1010. RDNA2 differs: on gfx1031 (RX 6700 XT, measured by pandather in #1281) the encoding is `v_fma_legacy_f32` and fused, with the same zero rule and denormal-source flush, so an RDNA2 measurement is not evidence for the console. - [v_mad_f32, v_mac_f32, v_madak_f32 and v_madmk_f32](../../../core/shader/recompiler/Translation/src/FloatInstructions.cpp) (gfx10.1 only) leave f32 denormals and NaN bits to the driver, unlike the console. Measured on gfx1010 (RX 5700 XT, #5782): the product is rounded before the add; f32 sources, product and result are flushed to a zero of the same sign in every f32 denormal mode; the product is flushed when tiny after rounding to 24 bits with an unbounded exponent; NaN results follow the fma rule, an invalid product beating a NaN src2; in round mode 2 a sum of zeros of opposite signs is -0. Translating this cost 15× `main` on a `v_mac_f32` chain on the RX 5700 XT and 23× on an RTX 3070, and a never-taken branch around it 6×, so it waits for the SPIR-V float controls of #981. - [v_fma_mixlo_f16 and v_fma_mixhi_f16](../../../core/shader/recompiler/Translation/src/FloatInstructions.cpp) round the exact product-sum once to f16 with the arithmetic of v_interp_p2_f16, in f64 or, without the Float64 capability, in 64-bit integers. On RDNA2 (gfx1036) they follow the `FLOAT_MODE` f16 round and denormal fields, not the f32 round field: -1.5 * 2^-24 + 2^-126 from f32 sources (0xbfc00000, 0x33800000, 0x00800000) gives 0x8001 with f16 denormals kept and round to nearest even, 0x8000 with f16 denormals flushed and 0x8002 with f16 round toward -infinity. With f16 denormals kept, every f16 rounding mode is translated: the exact product-sum is rounded once in the mode's direction. All four modes match RDNA2 (gfx1036) on the 2,233 rows of the #2631 sweep, including `FLOAT_MODE` 0xcc (toward zero, used by ps5-opengl) and the exact cancellations toward -infinity; the PS5's gfx1013 is not measured. A result beyond 65504 becomes infinity or 65504 as IEEE 754 gives for the mode. Flushed f16 denormals still throw, and packed f16 throws with any f16 rounding but nearest even. `FP16_OVFL` set is not measured. @@ -123,12 +126,14 @@ - [f16 unary, frexp and conversion denormals](../../../core/shader/recompiler/Translation/src/FloatInstructions.cpp) follow the `FLOAT_MODE` f16 denormal field: `v_ceil_f16`, `v_floor_f16`, `v_fract_f16`, `v_rcp_f16`, `v_rsq_f16`, `v_sqrt_f16`, `v_log_f16`, `v_exp_f16`, `v_sin_f16`, `v_frexp_mant_f16`, `v_frexp_exp_i16_f16`, `v_cvt_norm_i16_f16`, `v_cvt_norm_u16_f16` and `v_cvt_f32_f16` flush a denormal half input to its signed zero when bit 0 is clear (0 and 2), and the f16-result ones flush a denormal half result to its signed zero when bit 1 is clear (0 and 1), after the host rounds it to half. Measured on RDNA2 (gfx1036) at IEEE=0 and IEEE=1 with denorm16 0..3 over every non-NaN f16 input (63,490 rows per mode and instruction): the only mismatches are the 1-ULP driver differences of `v_exp_f16` (2 inputs) and `v_sin_f16` (14), which are the same with denormals kept. NaN inputs are not compared. `v_ldexp_f16` shares the input rule. `v_cvt_pknorm_i16_f16` and `v_cvt_pknorm_u16_f16` flush both half inputs by the same bit, measured on RDNA2 (gfx1030) over every f16 input (each half also negated in the other source) at IEEE=0 and IEEE=1 with denorm16 0..3 and at f16 rounding 1..3: every row matches. Shaders without a known float mode keep the previous lowering. - [v_div_fixup_f16](../../../core/shader/recompiler/Translation/src/FloatInstructions.cpp) flushes its src1 and src2 to a zero of the same sign when bit 0 of the `FLOAT_MODE` f16 denormal field is clear (0 and 2) and keeps a denormal src0 and result in every mode, as `v_div_fixup_f32` does with the f32 field. Measured on RDNA2 (gfx1031, Radeon RX 6700 XT, AMD Windows driver 32.0.21045.5002 through HIP) over 6859 triples of 19 halves (zeros, denormals, minimum normals, ±0.5, ±1, the largest finite value, infinities and quiet and signaling NaNs) at IEEE=0 and IEEE=1 with denorm16 0..3 and FP16_OVFL 0 and 1, also with neg and abs source modifiers and with op_sel high halves. Shaders without a known float mode keep the previous lowering. The PS5's gfx1013 is not measured. - [Packed f16 NaN results](../../../core/shader/recompiler/Translation/src/FloatInstructions.cpp) (`v_pk_add/mul/fma/min/max_f16`) follow the hardware measured on RDNA2 (gfx1035) at IEEE=0 and IEEE=1 with f16 round-to-nearest-even and f16 denormals kept: with IEEE=1 a signaling NaN input is quieted and min/max return the other operand only for a quiet NaN; with IEEE=0 NaN inputs pass through unquieted and min/max ignore signaling. In `v_pk_fma_f16` an invalid product (inf * 0) gives the default NaN even when src2 is a NaN. A shader without a known float mode is translated as IEEE=0, the mode titles use; a mode with another f16 rounding throws. In the f16 denormal modes that flush, each denormal input lane is flushed to a zero of its sign when bit 0 of the field is clear (0 and 2), so `inf * 0x0001` is an invalid product there, and when bit 1 is clear (0 and 1) a `v_pk_add_f16` result below 2^-14, or a `v_pk_mul_f16`, `v_pk_fma_f16` or `v_pk_fmac_f16` result whose exact value rounded to 11 significant bits with an unbounded exponent is below 2^-14, is flushed to a zero of its sign; `v_pk_min/max_f16` flush only their inputs. The native f16x2 add and multiply is not used while results are flushed. Measured on RDNA2 (gfx1031, Radeon RX 6700 XT, AMD Windows driver 32.0.21045.5002 through HIP) at IEEE=0 and IEEE=1 with denorm16 0..3, over 19,192 to 162,204 lanes per instruction with op_sel, op_sel_hi, neg_lo, neg_hi and clamp variants and products and sums at the 2^-14 boundary. -- [f32 denormals](../../../core/shader/recompiler/Translation/src/TranslationContext/OperandAccess.cpp) follow the shader's `FLOAT_MODE` f32 denormal field (bits 5:4) when it flushes them (0, and shaders without a known float mode, as in the 0xc0 most titles use), measured on RDNA2 (gfx1035) over 1444 rows per instruction at IEEE=0 and IEEE=1. `v_add/sub/subrev/mul/mul_legacy/mullit/fma/fmac/fmamk/fmaak/min/max/min3/max3/med3/fract/floor/ceil/frexp_mant/sin/cos_f32` flush their f32 inputs and their result to a zero of the same sign (`v_mullit_f32` measured on RDNA2 (gfx1036) over 6907 rows per mode at IEEE=0 and IEEE=1, so a positive denormal src2 counts as zero); the f32 compares, `v_cvt_i32/u32/rpi_i32/flr_i32_f32`, `v_frexp_exp_i32_f32`, `v_cvt_f64_f32`, `v_cvt_f16_f32`, `v_cvt_pkrtz_f16_f32`, `v_cvt_pknorm_i16/u16_f32` and `v_cvt_pk_u8_f32` flush their f32 inputs. `v_div_scale_f32` flushes all three inputs to signed zeros, measured on RDNA2 (gfx1036) over 2744 operand triples per mode at IEEE=0 and IEEE=1 with f32 denormals flushed and kept; its scale-down behaviour and host denormal preservation remain incomplete. `v_div_fixup_f32` flushes its src1 and src2 and keeps a denormal src0 and result, measured on RDNA2 (gfx1036) over 6859 rows per mode at IEEE=0 and IEEE=1. Tininess is detected after rounding, measured on RDNA2 (gfx1036) for `v_mul_f32` and `v_mullit_f32` at IEEE=0 and IEEE=1: a product is flushed when its value rounded to 24 bits with an unbounded exponent is below 2^-126, so `0x3f7ffffe * 0x00800001` (2^-126 * (1 - 2^-46)) gives `0x00800000` and `0x3f7fffff * 0x00800000` (2^-126 * (1 - 2^-24)) gives zero. The check rounds in the f32 round mode of `FLOAT_MODE` (bits 1:0), measured in all four modes, while the products themselves are still rounded to nearest even. This applies to `v_mul_f32`, `v_mul_legacy_f32`, `v_mullit_f32`, and to `v_fma_f32`, `v_fmac_f32`, `v_fmamk_f32`, `v_fmaak_f32` and `v_fma_mix_f32` when the addend is zero; with a nonzero addend the fused result is kept, so an exact result just below 2^-126 that rounds to it is not flushed. In the three directed modes the check is computed exactly from the significands. To nearest even (0, the mode of shaders without a known float mode) one factor is multiplied by 2^64, which is exact below 2^64, and the product with the other factor is compared with 2^-62. That multiply is a plain `OpFMul` like the product, and no f32 rounding mode is requested in the SPIR-V, so the check relies on the driver rounding f32 to nearest even by default, as the products do, and a product that rounds up to 2^-126 is the emitted product. `v_fma_mix_f32` follows this rule with mixed f16/f32 sources too, measured on RDNA2 (gfx1036) with f16 in either multiplicand slot, low and high half selection, and an f32 or f16 zero addend: f32 `0x00801002` times f16 `0x3bff` gives `0x00800000` to nearest at IEEE=0 and IEEE=1, while the neighbours `0x00801001` and `0x00801003` give zero and `0x00800001`. The tiny-product boundary was also measured for both signs in all four f32 round modes at IEEE=0; Its f16 sources are flushed to a zero of their sign when bit 0 of the f16/f64 denormal field is clear (0 and 2), whatever the f32 field, measured on RDNA2 (gfx1031, Radeon RX 6700 XT, AMD Windows driver 32.0.21045.5002 through HIP) at IEEE=0 and IEEE=1 with f32 denormal modes 0 and 3 and f16 denormal modes 0 to 3, over 2744 triples of 14 halves with all three sources f16 and 1960 rows with an f32 multiplicand. `v_fma_mixlo_f16` and `v_fma_mixhi_f16` flush their f32 sources too and keep a product below 2^-126, measured on RDNA2 (gfx1036). `v_mad_legacy_f32` and `v_mac_legacy_f32` flush their inputs, their product and their result in every mode, see its entry; their product is still checked for tininess on its exact value, so one below 2^-126 gives zero even when it rounds to 2^-126, as the console's separately rounded encoding is not measured at this boundary. Denormal modes 1 and 2 throw for the other instructions. Not covered, and not measurable on RDNA2 (no such opcodes) or not measured: `v_mac_f32`, `v_mad_f32`, `v_madak_f32` and `v_madmk_f32` (rounded separately), `v_dot2_f32_f16` and `v_div_fmas_f32` (on hardware `v_div_fmas_f32` flushes only its result), f32 interpolation and `ds_add_f32` keep the previous lowering; `ds_min/max/cmpst_f32` and the image, buffer, global and flat `fmin/fmax/fcmpswap` flush both operands by the field, see the float atomics entries. With f32 denormals kept (3) the results depend on the driver's default denormal behaviour, as no `DenormPreserve` execution mode is requested; `v_ldexp_f32` and the f32 compares are the exceptions. `v_cmp_X_f32` and `v_cmpx_X_f32` compare the operands as sign-magnitude integers with the NaN cases handled apart, so a denormal is not read as zero, measured on RDNA2 (gfx1036) over 256 operand pairs per opcode at IEEE=0 and IEEE=1 with f32 denormals kept and flushed. `v_ldexp_f32` preserves subnormal inputs and rounds a subnormal result in the f32 round mode of `FLOAT_MODE`, measured on RDNA2 (gfx1036) over 2163 rows in each of the four round modes at IEEE=0 and IEEE=1. An overflow rounds like any other result (infinity, or the largest finite value when the mode rounds that sign toward zero). With f32 denormals flushed it flushes a subnormal input and result, measured over the same rows in each round mode at IEEE=0 only. A device whose driver splits `Fma` into a multiply and an add flushes a product below 2^-126 before adding a non-zero addend in `v_fma_f32` and `v_fmac_f32`: `0x1fffffff * 0x1fffffff + 0x00800000` gives `0x00800000` instead of the fused `0x00ffffff` on RADV RENOIR (Mesa 26.2.4), whose compiled shader has `v_mad_f32` and no `v_fma_f32`. [F32DenormalFlush](../../../core/libs/prx/libSceAgcDriver/tests/execution/F32DenormalFlush.cpp) accepts that sum on such a device. +- [f32 denormals](../../../core/shader/recompiler/Translation/src/TranslationContext/OperandAccess.cpp) follow the shader's `FLOAT_MODE` f32 denormal field (bits 5:4) when it flushes them (0, and shaders without a known float mode, as in the 0xc0 most titles use), measured on RDNA2 (gfx1035) over 1444 rows per instruction at IEEE=0 and IEEE=1. `v_add/sub/subrev/mul/mul_legacy/mullit/fma/fmac/fmamk/fmaak/min/max/min3/max3/med3/fract/floor/ceil/frexp_mant/sin/cos_f32` flush their f32 inputs and their result to a zero of the same sign (`v_mullit_f32` measured on RDNA2 (gfx1036) over 6907 rows per mode at IEEE=0 and IEEE=1, so a positive denormal src2 counts as zero); the f32 compares, `v_cvt_i32/u32/rpi_i32/flr_i32_f32`, `v_frexp_exp_i32_f32`, `v_cvt_f64_f32`, `v_cvt_f16_f32`, `v_cvt_pkrtz_f16_f32`, `v_cvt_pknorm_i16/u16_f32` and `v_cvt_pk_u8_f32` flush their f32 inputs. `v_div_scale_f32` flushes all three inputs to signed zeros, measured on RDNA2 (gfx1036) over 2744 operand triples per mode at IEEE=0 and IEEE=1 with f32 denormals flushed and kept; its scale-down behaviour and host denormal preservation remain incomplete. `v_div_fixup_f32` flushes its src1 and src2 and keeps a denormal src0 and result, measured on RDNA2 (gfx1036) over 6859 rows per mode at IEEE=0 and IEEE=1. Tininess is detected after rounding, measured on RDNA2 (gfx1036) for `v_mul_f32` and `v_mullit_f32` at IEEE=0 and IEEE=1: a product is flushed when its value rounded to 24 bits with an unbounded exponent is below 2^-126, so `0x3f7ffffe * 0x00800001` (2^-126 * (1 - 2^-46)) gives `0x00800000` and `0x3f7fffff * 0x00800000` (2^-126 * (1 - 2^-24)) gives zero. The check rounds in the f32 round mode of `FLOAT_MODE` (bits 1:0), measured in all four modes, while the products themselves are still rounded to nearest even. This applies to `v_mul_f32`, `v_mul_legacy_f32`, `v_mullit_f32`, and to `v_fma_f32`, `v_fmac_f32`, `v_fmamk_f32`, `v_fmaak_f32` and `v_fma_mix_f32` when the addend is zero; with a nonzero addend the fused result is kept, so an exact result just below 2^-126 that rounds to it is not flushed. In the three directed modes the check is computed exactly from the significands. To nearest even (0, the mode of shaders without a known float mode) one factor is multiplied by 2^64, which is exact below 2^64, and the product with the other factor is compared with 2^-62. That multiply is a plain `OpFMul` like the product, and no f32 rounding mode is requested in the SPIR-V, so the check relies on the driver rounding f32 to nearest even by default, as the products do, and a product that rounds up to 2^-126 is the emitted product. `v_fma_mix_f32` follows this rule with mixed f16/f32 sources too, measured on RDNA2 (gfx1036) with f16 in either multiplicand slot, low and high half selection, and an f32 or f16 zero addend: f32 `0x00801002` times f16 `0x3bff` gives `0x00800000` to nearest at IEEE=0 and IEEE=1, while the neighbours `0x00801001` and `0x00801003` give zero and `0x00800001`. The tiny-product boundary was also measured for both signs in all four f32 round modes at IEEE=0; Its f16 sources are flushed to a zero of their sign when bit 0 of the f16/f64 denormal field is clear (0 and 2), whatever the f32 field, measured on RDNA2 (gfx1031, Radeon RX 6700 XT, AMD Windows driver 32.0.21045.5002 through HIP) at IEEE=0 and IEEE=1 with f32 denormal modes 0 and 3 and f16 denormal modes 0 to 3, over 2744 triples of 14 halves with all three sources f16 and 1960 rows with an f32 multiplicand. `v_fma_mixlo_f16` and `v_fma_mixhi_f16` flush their f32 sources too and keep a product below 2^-126, measured on RDNA2 (gfx1036). `v_mad_legacy_f32` and `v_mac_legacy_f32` flush their inputs, their product and their result in every mode, see its entry; their product is still checked for tininess on its exact value, so one below 2^-126 gives zero even when it rounds to 2^-126, as the console's separately rounded encoding is not measured at this boundary. Denormal modes 1 and 2 throw for the other instructions except the f32 compares. Not covered, and not measurable on RDNA2 (no such opcodes) or not measured: `v_mac_f32`, `v_mad_f32`, `v_madak_f32` and `v_madmk_f32` (rounded separately), `v_dot2_f32_f16` and `v_div_fmas_f32` (on hardware `v_div_fmas_f32` flushes only its result), f32 interpolation and `ds_add_f32` keep the previous lowering; `ds_min/max/cmpst_f32` and the image, buffer, global and flat `fmin/fmax/fcmpswap` flush both operands by the field, see the float atomics entries. With f32 denormals kept (3) the results depend on the driver's default denormal behaviour, as no `DenormPreserve` execution mode is requested; `v_ldexp_f32` and the f32 compares are the exceptions. `v_cmp_X_f32` and `v_cmpx_X_f32` compare the operands as sign-magnitude integers with the NaN cases handled apart, so a denormal is not read as zero, measured on RDNA2 (gfx1036) over 256 operand pairs per opcode at IEEE=0 and IEEE=1 with f32 denormals kept and flushed. `v_ldexp_f32` preserves subnormal inputs and rounds a subnormal result in the f32 round mode of `FLOAT_MODE`, measured on RDNA2 (gfx1036) over 2163 rows in each of the four round modes at IEEE=0 and IEEE=1. An overflow rounds like any other result (infinity, or the largest finite value when the mode rounds that sign toward zero). With f32 denormals flushed it flushes a subnormal input and result, measured over the same rows in each round mode at IEEE=0 only. A device whose driver splits `Fma` into a multiply and an add flushes a product below 2^-126 before adding a non-zero addend in `v_fma_f32` and `v_fmac_f32`: `0x1fffffff * 0x1fffffff + 0x00800000` gives `0x00800000` instead of the fused `0x00ffffff` on RADV RENOIR (Mesa 26.2.4), whose compiled shader has `v_mad_f32` and no `v_fma_f32`. [F32DenormalFlush](../../../core/libs/prx/libSceAgcDriver/tests/execution/F32DenormalFlush.cpp) accepts that sum on such a device. - [v_rcp, v_rsq, v_sqrt, v_log, v_exp, v_sin and v_cos](../../../core/shader/recompiler/Translation/src/FloatInstructions.cpp) (f32 and f16) use the driver's approximation for finite results, not the console's: the low bits can differ. Only NaN, invalid, zero and infinite results and the exact sin/cos zeros are chosen on the bits. Measured on RDNA2 (gfx1036) over every f16 input and 32,832 f32 inputs: RADV returns the hardware's bits for every input of rcp, rsq, sqrt and log and all but one of exp (f16 0x11c5, 1 ULP); NVIDIA (615.71.09) stays within 2 ULP except `v_log_f32` near 1.0 (next entry). [v_sin and v_cos](../../../core/shader/recompiler/SpirvBackend/src/SpirvAlu/SpirvAluEmitterMath.cpp) take the driver's sine or cosine of the radians of the exact cycle, except within 2^-12 of a turn from a zero of the function, where the result is the linear term of the exact distance to the zero: the hardware's constant 6.2831826 (0x40c90fd5) at the integers, which the hardware applies exactly to inputs below 2^-12 of a turn, and the f32 2π at the half and quarter turns, where the hardware is within 7 ULP of it. Outside those bands, a driver result more than 4 ULP from a Taylor series in turns of the exact cycle is replaced by the series, which is within 2 ULP of the true sine and cosine; the hardware is up to 9 ULP from them (gfx1035, 243,092 f32 inputs), so results stay within 13 ULP of the hardware on any driver. RADV returns the hardware's bits for every f16 input and for 32,422 sine and 32,486 cosine results of the 32,832 f32 inputs; the rest are 1 ULP off, except up to 14 ULP on either side of non-zero integers and up to 15 ULP beyond ±256 cycles, where the hardware's own reduction differs from the exact fraction, and up to 6 ULP inside the half- and quarter-turn bands. NVIDIA's own sine is within 1 ULP on 93% of the f32 inputs and up to 1,166 ULP off just outside the bands, its cosine within 163 ULP; the series replaces those results. A denormal f32 input with f32 denormals kept gives the hardware the input times 6.2831826; both hosts flush it. - [v_rcp_f32 and v_rcp_iflag_f32 with `APS5_EXACT_RCP`](../../../core/shader/recompiler/SpirvBackend/src/SpirvAlu/SpirvAluEmitterReciprocal.cpp) return the hardware reciprocal for normal inputs with normal results: a cubic over 32 mantissa segments, the linear term rounded to nearest even, plus 2,761 one-ULP corrections. It matches all 2^23 mantissas of an RDNA2 (gfx1031) sweep; inputs or results that are not normal keep the driver's 1/x. It is off by default because the coefficient and correction tables add about 13k SPIR-V words to each shader that uses it. The switch also reaches v_rcp_f16 (rounded from the f32 reciprocal) and the pixel shader's position W input. - [v_ldexp_f32 in denormal modes 1 and 2](../../../core/shader/recompiler/Translation/src/FloatInstructions.cpp) follows the `FLOAT_MODE` f32 denormal field: mode 1 keeps a subnormal input and flushes a result below 2^-126 to a zero of its sign in every f32 round mode, also one that rounds to 2^-126 at subnormal precision (`0x3fffffff` scaled by 2^-127); mode 2 flushes a subnormal input to a zero of its sign and rounds a subnormal result in the f32 round mode of `FLOAT_MODE`, as mode 3 does. Measured on RDNA2 (gfx1030, Radeon RX 6800, AMD Windows driver 32.0.21045.5002 through HIP) over 10,260 rows (54 inputs of both signs: zeros, subnormals, normals near 2^-126 and 1.0, the largest finite value, infinities and NaNs, each scaled by 190 exponents from -2^31 to 2^31-1) at IEEE=0 and IEEE=1 with each f32 denormal mode 0 to 3 and f32 round mode 0 to 3, DX10_CLAMP=1, f16 denormals kept, f16 round to nearest even and FP16_OVFL=0; with the `clamp` modifier the result is clamped as in the other modes. The PS5's gfx1013 is not measured. - [32-bit denormal execution mode](../../../core/shader/recompiler/Recompiler.cpp) (shader recompiler) - the hardware f32 flush that the startup probe chooses (`DenormFlushToZero 32`) is declared only in shaders whose `FLOAT_MODE` flushes f32 denormals (field 0, or no known float mode). A shader that keeps them on either side (field 1, 2 or 3) declares `DenormPreserve 32` when the device has `shaderDenormPreserveFloat32` with a `denormBehaviorIndependence` other than NONE; on other devices it gets the driver's default f32 denormal handling, which flushes the denormals it keeps on MoltenVK. - [v_log_f32 and v_log_f16](../../../core/shader/recompiler/SpirvBackend/src/SpirvAlu/SpirvAluEmitterMath.cpp) keep the driver's log2 unless, for inputs within 1/8 of 1.0, it is more than 4 ULP from an eight-term series in x - 1, which then replaces it. `v_log_f16` goes through the same f32 lowering and is rounded to half. The series is evaluated for every `v_log` and chosen with `OpSelect` rather than a branch, so its seven fused multiply-adds and two multiplies are an unconditional cost. Measured on RDNA2 over all 3,145,728 f32 inputs within 1/8 of 1.0 (gfx1035, IEEE 0, f32 denormals flushed), the hardware is within 1 ULP of the correctly rounded log2 and the series within 2 ULP of it and within 3 ULP of the hardware (2 except at 0x3f752e47, 0x3f7baab0 and 0x3f7bc4fb), so a kept driver value is within 7 ULP of the hardware and a driver that returns the hardware's bits never triggers the gate; RADV (Mesa 26.2.4) returns the hardware's bits on the 9,280 such inputs measured on gfx1036; NVIDIA (615.71.09) was up to 23,947,836 ULP off at 1 - 2^-24 and is within 6 ULP inside the band after the change; outside it its own log2 stays up to 9 ULP off (measured up to 1.375). Over every f16 input, the change moves one `v_log_f16` result on NVIDIA (0x3bed, 1 ULP, onto the hardware's bits) and none on RADV. +- [v_add_f32, v_sub_f32, v_subrev_f32, v_mul_f32 and v_mul_legacy_f32 in denormal modes 1 and 2](../../../core/shader/recompiler/Translation/src/VectorInstructions.cpp) throw with a directed f32 round mode, as the host rounds these sums and products to nearest even (a difference that modes 0 and 3 also have then), and when the module cannot declare `DenormPreserve 32` (no `shaderDenormPreserveFloat32`, or the hardware f32 flush in use); the PS5's gfx1013 is not measured. Otherwise mode 1 keeps denormal inputs and flushes a result below 2^-126 to a zero of its sign (a product only when it stays below 2^-126 after rounding to 24 bits with an unbounded exponent), and mode 2 flushes denormal inputs and keeps a denormal result. Measured on RDNA2 (gfx1030, RX 6800) over 1,296 operand pairs, 896 products around 2^-126 and 1,632 with a constant factor, at IEEE 0 and 1 in every f32 denormal mode, with DX10_CLAMP=1, f16 denormals kept and FP16_OVFL=0. +- [f32 min and max in denormal modes 1 and 2](../../../core/shader/recompiler/Translation/src/VectorInstructions.cpp) (`v_min_f32`, `v_max_f32`, `v_min3_f32`, `v_max3_f32`, `v_med3_f32`) throw in mode 1 when the module cannot declare `DenormPreserve 32` (no `shaderDenormPreserveFloat32`, or the hardware f32 flush in use), and mode 3 keeps depending on the driver there; the PS5's gfx1013 is not measured. They follow only bit 0 of the `FLOAT_MODE` f32 denormal field: mode 2 flushes denormal inputs to zeros of their sign as mode 0 does, and mode 1 keeps them and returns a selected denormal unflushed as mode 3 does. Measured on RDNA2 (gfx1030, RX 6800) over the 2,744 operand triples of ±0, ±2^-149, ±0x007fffff, ±2^-126, ±1, ±infinity, a quiet and a signaling NaN at IEEE 0 and 1 in each f32 denormal mode, round to nearest even, DX10_CLAMP=1, f16 denormals kept, FP16_OVFL=0: modes 1 and 3 give identical results, and so do modes 2 and 0. - [Ordered not-equal, less-than and greater-than compares](../../../core/shader/recompiler/SpirvBackend/src/SpirvAlu/SpirvAluEmitterMath.cpp) (shader recompiler) - `FPOrdNotEqual32` is emitted as `OpFUnordNotEqual`, and `FPOrdLessThan32` and `FPOrdGreaterThan32` as their ordered forms, each with both operands checked for NaN on their bits; the results are the same for every input. The AMD Windows driver 32.0.13058.8001 (Radeon 860M, RDNA 3.5) returns 1 for a NaN operand when any float form of not-equal feeds a ballot on a 32-wide subgroup (#825), and so does a less-than or greater-than against an infinity the driver sees as a constant (#3536), presumably rewritten into a not-equal by its compiler - [16-bit integer multiplies](../../../core/shader/recompiler/Translation/src/IntegerInstructions.cpp) (`v_mul_lo_u16`, `v_pk_mul_lo_u16`) (shader recompiler) - a factor taken from the low half of its source is read as the low 24 bits of the register, not the low 16; the low 16 bits of the product are the same for every input. The AMD Windows driver 32.0.21045.5002 (RX 6700 XT, RDNA 2) stores the full 32-bit product when two values it knows fit in 16 bits are multiplied, the product is reduced to 16 bits and nothing else is merged into the stored value: `v_mul_lo_u16` into a register whose high half is zero, also with factors masked to 16 bits by the program, and `v_pk_mul_lo_u16` whose high lanes are zero wrote 0xfffe0001 for 0xffff * 0xffff where the GPU writes 0x00000001. Reducing the product with `v_and_b32`, `v_bfe_u32` or a shift pair loses the same bits; factors read as 24-bit fields do not. The [constant folder](../../../core/shader/recompiler/Optimization/src/ConstantFolder.cpp) keeps a field read that a multiply uses, even when the program already masked the factor to 16 bits, since folding it away brings the full product back (#8271). Measured against the same instructions run through the [hardware oracle](../HW_ORACLE.md) on that GPU; other drivers are not checked - [f32 compares in denormal modes 1 and 2](../../../core/shader/recompiler/Translation/src/VectorInstructions.cpp) (`v_cmp_X_f32` and `v_cmpx_X_f32` with X = eq, ge, gt, le, lg, lt, neq, nge, ngt, nle, nlg, nlt) flush their f32 denormal inputs by bit 0 of the `FLOAT_MODE` f32 denormal field: mode 1 keeps them as mode 3 does, and mode 2 flushes them as mode 0 does. Measured on RDNA2 (gfx1030, Radeon RX 6800, AMD Windows driver 32.0.21045.5002 through HIP) over 484 operand pairs of zeros, denormals, minimum normals, ones, the largest finite value, infinities and quiet and signaling NaNs of both signs, in VOPC form, at IEEE=0 and IEEE=1 with each f32 denormal mode 0 to 3, f32 round to nearest even, DX10_CLAMP=1, f16 denormals kept, f16 round to nearest even and FP16_OVFL=0. The PS5's gfx1013 is not measured. @@ -154,8 +159,7 @@ - [Comparison sampling of a color texture](../../../core/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cpp) (`image_sample_c`/`image_sample_c_lz` on a texture that is not R32 float or R16 unorm) returns 0 for every comparison function on formats AnyPS5 can sample as floating-point measured on GFX10 (codes 1–77 except native comparison codes 7 and 22) and on the corresponding GFX11 formats measured on gfx1100. The current Vulkan path still lowers these formats through the shader; the compare result is constant zero. Scaled, integer and formats requiring converted sampling remain refused. Only 2D, 2D-array and cube views are accepted; image tables, min/max reduction, unnormalized coordinates and unsupported addressing remain refused. Point and bilinear results match on gfx1100. The PS5's gfx1013, array/cube behavior for the measured formats, and anisotropic comparisons remain unverified. Bilinear weights are full float, not the hardware's fixed-point subtexel weights - [Negative color-texture comparison wrap indices](../../../core/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cpp) (libSceAgcDriver) use explicit `OpSRem` normalization to implement modulo; the negative-offset point-wrap regression fails with direct `OpSMod` and passes with normalization on Mesa 22.3.6 llvmpipe (LLVM 15.0.6), but negative-index wrap has not been measured on PS5 hardware. - [Converted unorm images](../../../core/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cpp) (10_11_11_UNORM, read and written through an R32_UINT view) are sampled in the shader, the way color comparisons are emulated: `image_sample` of a 2D or 2D array view fetches the one or four texels at LOD 0, decodes them and blends them, for point or bilinear filtering with equal minification and magnification filters and wrap or clamp-to-edge addressing. A shader gets these sample modes only once a converted descriptor reaches one of its sampled images: that capture throws `ConvertedSampleRequired`, the driver records the program and prepares it again with `GuestContext::convertedSamples`, so such a shader is prepared twice per run and its first capture is repeated. The record lives with the registered program and is not kept across runs. Bilinear weights are rounded to the hardware's 8-bit grid (`roundEven(fraction * 256) / 256`, ties to even), as measured with `image_sample_lz` on RDNA2 (gfx1035) for 10_11_11 UNORM texels by adriadam10 in #5958; point sampling, decoding, swizzles and wrap and clamp-to-edge addressing matched the same measurement. A sample that is not `_lz` needs an image with one mip level; gathers, LOD queries, clamp-to-border and mirrored addressing, other filters, min/max reduction, unnormalized coordinates, images selected at run time, 16-bit data and a DST_SEL of the missing W channel throw. -- [Converted unorm image loads](../../../core/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cpp) divide by 2^n - 1 with the buffer format path's `OpFDiv`, which Vulkan allows to be up to 2.5 ULP off, so some drivers are not bit-exact. -- [64-bit LDS atomics](../../../core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvMemoryInstructions.cpp) (`ds_*_u64`, `ds_*_b64`, `ds_*_f64`) read and write the two dwords under a workgroup lock stored after the guest LDS, as LDS is a 32-bit array without 64-bit atomics. On RDNA2 (gfx1036) the LDS serialises a 32-bit atomic on a slot's high dword and a 64-bit atomic on that slot: 128 lanes of `ds_add_u32` and 128 lanes of `ds_add_u64` on one slot sum exactly. Only that pair was measured. Every 32-bit LDS atomic of a program that contains a 64-bit LDS atomic therefore takes the same lock, including a 32-bit `flat_atomic_*` whose address lands in the LDS aperture (a 64-bit one takes it already); the flat forms were not measured against a 64-bit atomic either (the other 32-bit LDS atomics, `ds_min/max`, `ds_cmpst`, the float forms, `ds_inc/dec`, `ds_mskor` and `ds_wrxchg`, are extrapolated from `ds_add_u32` and were not measured against a 64-bit atomic); a program without a 64-bit LDS atomic keeps its native 32-bit atomics. `ds_condxchg32_rtn_b64` is two `ds_mskor` halves, each under the lock but not atomic as a pair, so a 64-bit atomic between the two halves is not excluded. Sub-dword writes (`ds_write_b8`, `ds_write_b16` and their `_d16_hi` forms) and plain `ds_read_*`, `ds_write_*`, `ds_append_u32` and `ds_consume_u32` are not taken under the lock, so a plain access racing a 64-bit atomic on the same dwords can see or overwrite one half of it; whether the hardware orders them was not measured. The lock serialises the workgroup's LDS atomics of such a program; the throughput cost was not measured. `VK_KHR_workgroup_memory_explicit_layout` with `shaderSharedInt64Atomics` would make both widths native and is not used. A 64-bit atomic at an address that is 4-byte but not 8-byte aligned faults the wave on gfx1036, as on gfx1035. When the LDS lives in device memory (compute LDS beyond the device's shared-memory limit) and the device has `shaderBufferInt64Atomics`, no lock is used: a 64-bit atomic is a compare-exchange loop on a 64-bit view of the storage buffer and a 32-bit atomic is a native buffer atomic, a misaligned 64-bit atomic records the `Unaligned` shader fault with the LDS byte address instead of updating, so a mixed-width pair on one slot relies on the device ordering both widths on one buffer address; only `ds_add_u32` against `ds_add_u64` was measured, on gfx1036 (AMD Windows driver 32.0.21042.62) and an RTX 4070 SUPER. The lock is not used there because AMD's Windows driver compiles an acquire or release on a storage buffer, at workgroup scope or wider, to `s_barrier`, and those barriers inside the lock's divergent loop pair with the program's own `s_barrier`s. The fence after each LDS atomic compiles to `s_barrier` on that driver as well, so a wave that skips an LDS atomic that other waves of its workgroup execute misses a barrier they reach; its effect was not measured. On that path a 64-bit atomic at an address that is 4-byte but not 8-byte aligned is skipped and returns 0. +- [64-bit LDS atomics](../../../core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvMemoryInstructions.cpp) (`ds_*_u64`, `ds_*_b64`, `ds_*_f64`) read and write the two dwords under a workgroup lock stored after the guest LDS, as LDS is a 32-bit array without 64-bit atomics. On RDNA2 (gfx1036) the LDS serialises a 32-bit atomic on a slot's high dword and a 64-bit atomic on that slot: 128 lanes of `ds_add_u32` and 128 lanes of `ds_add_u64` on one slot sum exactly. Only that pair was measured. Every 32-bit LDS atomic of a program that contains a 64-bit LDS atomic therefore takes the same lock, including a 32-bit `flat_atomic_*` whose address lands in the LDS aperture (a 64-bit one takes it already); the flat forms were not measured against a 64-bit atomic either (the other 32-bit LDS atomics, `ds_min/max`, `ds_cmpst`, the float forms, `ds_inc/dec`, `ds_mskor` and `ds_wrxchg`, are extrapolated from `ds_add_u32` and were not measured against a 64-bit atomic); a program without a 64-bit LDS atomic keeps its native 32-bit atomics. `ds_condxchg32_rtn_b64` is two `ds_mskor` halves, each under the lock but not atomic as a pair, so a 64-bit atomic between the two halves is not excluded. Sub-dword writes (`ds_write_b8`, `ds_write_b16` and their `_d16_hi` forms) and plain `ds_read_*`, `ds_write_*`, `ds_append_u32` and `ds_consume_u32` are not taken under the lock, so a plain access racing a 64-bit atomic on the same dwords can see or overwrite one half of it; whether the hardware orders them was not measured. The lock serialises the workgroup's LDS atomics of such a program; the throughput cost was not measured. `VK_KHR_workgroup_memory_explicit_layout` with `shaderSharedInt64Atomics` would make both widths native and is not used. A 64-bit atomic at an address that is 4-byte but not 8-byte aligned faults the wave on gfx1036, as on gfx1035; with the workgroup lock, in device memory and in the invocation-private LDS of other stages alike, the recompiler records the `Unaligned` shader fault with the LDS byte address, leaves both dwords unchanged and returns 0, but the lane and the rest of its wave keep running. When the LDS lives in device memory (compute LDS beyond the device's shared-memory limit) and the device has `shaderBufferInt64Atomics`, no lock is used: a 64-bit atomic is a compare-exchange loop on a 64-bit view of the storage buffer and a 32-bit atomic is a native buffer atomic, so a mixed-width pair on one slot relies on the device ordering both widths on one buffer address; only `ds_add_u32` against `ds_add_u64` was measured, on gfx1036 (AMD Windows driver 32.0.21042.62) and an RTX 4070 SUPER. The lock is not used there because AMD's Windows driver compiles an acquire or release on a storage buffer, at workgroup scope or wider, to `s_barrier`, and those barriers inside the lock's divergent loop pair with the program's own `s_barrier`s. The fence after each LDS atomic compiles to `s_barrier` on that driver as well, so a wave that skips an LDS atomic that other waves of its workgroup execute misses a barrier they reach; its effect was not measured. - [Image, buffer and global float atomics](../../../core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvMemoryInstructions.cpp) (`image_atomic_fmin/fmax/fcmpswap`, the same three `buffer_atomic_*` and `global_atomic_*` forms and their `_x2` forms) follow the rule of the LDS float atomics below, including its operand flush: the 32-bit forms flush both operands to a signed zero when bit 0 of the f32 denormal field is clear, the `_x2` forms by the f16/f64 field, and `fcmpswap` stores a denormal source as given. The `flat_atomic_*` forms take the same path and are not measured. Measured on RDNA2 (gfx1031, RX 6700 XT) with the [hardware oracle](../HW_ORACLE.md) over 20 x 20 f32 and 18 x 18 f64 operand pairs in every value of both fields, at IEEE 0 and 1, in wave32 and wave64, and for the image forms on 32_UINT, 32_SINT and 32_FLOAT surfaces. The console's gfx1013 is not measured. - [LDS float atomics](../../../core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvMemoryInstructions.cpp) (`ds_min/max/cmpst_f32`, `ds_min/max/cmpst_f64` and their `_rtn` forms) follow the hardware measured on RDNA2 (gfx1036) over 14 x 14 operand sets (zeros, ones, infinities, denormals, quiet and signalling NaNs of both signs and with payloads) in IEEE 0 and 1 with every value 0-3 of the type's denormal field: both operands are flushed to a signed zero when bit 0 of the field is clear (0 and 2; 1 and 3 keep them; the f32 field for the f32 forms, the f16/f64 field for the f64 forms; a shader without a known float mode flushes f32 and keeps f64), then a signalling-NaN data operand is stored quieted, else a signalling-NaN old value is stored quieted, else a quiet-NaN old value is replaced by the data, else a quiet-NaN data operand loses, else the ordered compare with -0 below +0 decides, and the flushed value is what is written, also when the old value wins. `cmpst` compares the flushed bits (both zeros equal, NaN never equal) and leaves memory unchanged when they differ. The returned value is always the old value as it was in memory. `IEEE_MODE` has no effect. Not measured: a denormal or signalling-NaN `data1` of `cmpst` (stored as given). `ds_add_f32` and `ds_add_rtn_f32` follow RDNA1 (gfx1010, RX 5700 XT), measured with the [hardware oracle](../HW_ORACLE.md) template run through the Windows driver's comgr and HIP runtime over 1574 operand pairs (signed zeros, denormals, the smallest normals, infinities, quiet and signalling NaNs with payloads, the largest normals, random pairs near the denormal range and pairs that nearly cancel) in all 16 combinations of the f32 denormal field and the f32 round mode, and at IEEE 1 and DX10 clamp 0: both operands are flushed to a signed zero when bit 0 of the field is clear and the result when bit 1 is clear, the sum is rounded to nearest even whatever the round mode, a NaN data operand is stored quieted, else a NaN old value is stored quieted, `+inf + -inf` stores 0xffc00000, and `IEEE_MODE` and `DX10_CLAMP` have no effect. The sum is the host's `OpFAdd` between those flushes, so where the field keeps f32 denormals the result also depends on the host preserving them, which the module does not request. thuanquang measured the same rule on RDNA2 (gfx1031, RX 6700 XT) in #5492, with the same operand classes and 18 modes in wave32 and wave64; the PS5's gfx1013 is not measured. - [LDS in pixel, vertex-path and fetch programs](../../../core/shader/recompiler/SpirvBackend/src/SpirvAnalysis.cpp) (shader recompiler) - console waves share one LDS region; the host uses a private `Function` array (8192 dwords when unbounded). On NVIDIA proprietary, past about 4 GiB of private memory over every resident invocation that array fails (4090 on 617.42: 5456 ok, 5472 fault at 128×48×32 from `VK_NV_shader_sm_builtins`; 5090: 4096 ok, 6144 timeout). Above `min(4096, 4 GiB / (SMs × warps/SM × subgroup × 4))` it is a storage buffer per lane; other drivers keep `Function`. Only the 4090 sizes are measured. The NVIDIA-only pool fits subgroups in min(2 GiB, storage range) until teardown; excess slots sticky-flag and `WaitIdle` throws (no recycle). Header mapped host-visible/coherent (prefer device-local). Sub-dword writes, 32-bit atomics, `ds_append` and `ds_consume` are non-atomic on the lane's copy; append/consume update only the first active lane. Not measured on hardware. @@ -187,6 +191,7 @@ - [GFX10 scratch address operands](../../../core/shader/recompiler/RdnaDecoder/src/RdnaMemoryOpDecoder.cpp) (shader recompiler) - `core/shader/recompiler/RdnaDecoder/src/RdnaMemoryOpDecoder.cpp` and `core/shader/recompiler/Translation/src/MemoryInstructions/MemoryResourceHelpers.cpp` use the SV/SS/ST scratch operand widths described by LLVM FLATInstructions.td at 3eb867f9b63ae2881bbe3c25d3c12842f2bdc55e (operand pseudos L557-L630, GFX10 encoding L2973-L3038). Decoding and IR tests cover the one-dword VGPR offset; PS5 hardware address-result validation remains outstanding. Unsupported scalar registers still throw. - [SDWA source selects on `v_cvt_f32_ubyte1`, `v_cvt_f32_ubyte2` and `v_cvt_f32_ubyte3`](../../../core/shader/recompiler/RdnaDecoder/src/RdnaVectorOpDecoder.cpp) (shader recompiler) - byte N is taken from source 0 after `SRC0_SEL` and `SRC0_SEXT` select and extend it, since SDWA supplies source 0 (RDNA 2 ISA guide 6.8 and 13.3.7) and the opcode reads `S0.u[8N+7:8N]` (12.8). Not measured on hardware. - [Flag structurization](../../../core/shader/recompiler/ControlFlow/src/Structurizer.cpp) (shader recompiler) - a graph that every structurizer attempt refuses, irreducible and non-terminating graphs included, is rebuilt with one reach flag per block and one again flag per loop (`structurizeWithFlags`): each block keeps one copy and runs behind a guard on its flag, and each strongly connected component becomes a loop whose single latch repeats it while a back edge set its flag. The output has about 8 times the input's blocks and one route variable per block, and ends in an empty Return that never runs. Only blocks that start the top-level order unconditionally run unguarded, so resource planning also sees the static paths that skip a guarded block and can plan extra descriptor candidates for them. The attempts still run first. Shared merge splitting refuses an attempt once the graph has grown past 16 times its block count at the start of splitting (at least 64 blocks); succeeding attempts grew at most 10.6 times on random graphs of up to 128 blocks, and an attempt that would succeed only beyond the budget leaves the graph to a later attempt or to this construction. Splitting that adds one block per round stays within that budget until its 4 rounds per block run out, so the attempts can still take more than 10 s (9 of 2,000 random graphs of up to 128 blocks, 63 s at most). `APS5_FORCE_STRUCTURIZER_FALLBACK=1` builds every graph this way, for tests +- [Shared loop latches](../../../core/shader/recompiler/ControlFlow/src/Structurizer.cpp) (shader recompiler) - a loop whose single latch holds instructions and is reached by one edge gets an empty continue block, so its continue construct is that one block. A latch that several `continue` paths share keeps its instructions in the continue construct, since isolating it would clone them into each path. When such a latch emits more than one SPIR-V block, as a memory or image access does, SPIRV-Cross (MoltenVK) folds the construct into a `for` increment and drops the header's `OpPhi` updates, so the loop never advances on Metal. No title is known to reach this case. - [Scalar buffer descriptors](../../../core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp) use only the base address, stride and record count for resources accessed exclusively by scalar loads; their fourth word is normalized to zero, following AMD RDNA2 ISA section 7.2.2. Vector and mixed accesses retain descriptor validation. Native Vulkan readbacks cover ignored fourth-word fields on Windows/NVIDIA; their console behavior has not been measured. - [Spanned wave64 compute](../../../core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvSubgroup.cpp) (shader recompiler) - `APS5_SPANNED_WAVE=1`, off by default, runs a single-wave wave64 compute program at one lane per invocation on a 32-lane host subgroup and exchanges ballots, shuffles and reductions between the two host subgroups through workgroup memory and barriers. The driver turns it on only where compute subgroups can be pinned at 32 full lanes (`subgroupSizeControl` and `computeFullSubgroups`), passes it as `SpirvTarget::spannedWaves` (part of the cache key and the serialized request), and creates the pipelines of spanned programs with `REQUIRE_FULL_SUBGROUPS`, since the lane number `SubgroupId * 32 + SubgroupLocalInvocationId` needs two full subgroups. Those barriers are correct only where every invocation of the workgroup reaches them, which holds for the guest's wave-uniform control flow. The emulated 64-bit LDS atomics ([LockedLdsUpdate](../../../core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvMemoryInstructions.cpp)) run only on the lanes the exec mask keeps, so their lock loop ballots within its own host subgroup, as its elect already does. Any other cross-lane exchange emitted inside per-lane control flow would hang a spanned wave in the same way, and the emitter does not reject one: there is no uniformity check. The only check so far is manual: disassemble the module (`spirv-dis`), list the selection and loop constructs that enclose each `OpControlBarrier`, and read the condition of each enclosing selection. In Ghost of Yōtei's program 0x80003af600 (PPSA26344), 108 of 111 barriers sat only in loops on wave-uniform conditions and 3 inside per-invocation conditionals, all in the lock loop. Other programs and titles have not been scanned. An earlier, cheaper ballot (one barrier instead of three) also lost the device for a reason that is not understood, which may be another barrier placement problem. Measured on an RTX 4090 (Windows): an offline replay of that program and two Yōtei runs that previously lost the device (NVIDIA TDR) now run past it, and [LdsMaskWaterfall.cpp](../../../core/libs/prx/libSceAgcDriver/tests/execution/LdsMaskWaterfall.cpp) runs both modes and covers a half-inactive wave. Stray (PPSA02101) on Intel ANV and a Demon's Souls (PPSA01342) request replay showed no fault, before this fix - [Guest memory load guards](../../../core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvMemoryInstructions.cpp) (shader recompiler) - a guest load whose lane is inactive or whose address is out of bounds is still performed and its result replaced by 0 with `OpSelect`, instead of a selection per guard, when the load can be pointed at a location nothing writes: scratch reads its own element 0; storage buffers (direct buffers, GDS) read element 0xfffffffe, which `robustBufferAccess2` turns into a zero load with no memory access, so the extra read cannot race with a write the guarded program would not have raced with ([Vulkan memory model](https://docs.vulkan.org/spec/latest/appendices/memorymodel.html#data-race)). Without `robustBufferAccess2` ([VulkanDevice.cpp](../../../core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp) enables it when the device has it) those loads keep their selections; LDS loads, stores, atomics, formatted and physical-address (BDA) accesses always do. LDS keeps the branch because NVIDIA's compiler predicates a branched LDS load at no cost, while the select form costs about 2.5 instructions per dword and splits a `ds_read_b128` into four loads (RTX 4060 measurements by @karanvora2599 in #4048); Intel's static counts favour the select only for wide reads, and run time was not measured on either. The aligned high word of a misaligned-capable buffer read re-reads the low word's own element. A selection guard costs four blocks per lane half and the host compiler's time grows with the block count: PPSA11142's merged PSP vertex program (3176 ISA words, wave64) emitted 16,983 blocks and 1,710 conditional branches, 5,907 and 868 with the selects. `agc_guarded_memory_loads` traces the emitted loads in both device modes. diff --git a/docs/user/INPUT_MAPPING.md b/docs/user/INPUT_MAPPING.md index 7fd7a8d16d..98970b5163 100644 --- a/docs/user/INPUT_MAPPING.md +++ b/docs/user/INPUT_MAPPING.md @@ -1,6 +1,6 @@ # Controller, keyboard and mouse input -AnyPS5 accepts SDL-mapped game controllers as well as its built-in keyboard and mouse bindings. Xbox-style controllers and PlayStation controllers recognized by SDL use the standard PS button layout; sticks and analog triggers are passed through, and controllers can be connected or disconnected while the game is running. The first recognized controller is used. AnyPS5 uses its built-in keyboard and mouse bindings when no configuration file is present. To change selected keyboard and mouse bindings, create `anyps5-input.ini` beside the generated game executable. Set `ANYPS5_INPUT_CONFIG` to use a file at another path. Both the game directory and that path may contain any characters, such as `ü` or `日本`. +AnyPS5 accepts SDL-mapped game controllers as well as its built-in keyboard and mouse bindings. Up to 4 controllers are supported simultaneously. The first connected controller is assigned to Player 1 and composed with keyboard and mouse input; additional controllers are sequentially assigned to Players 2, 3, and 4 (matching `libSceUserService` users). Xbox-style controllers and PlayStation controllers recognized by SDL use the standard PS button layout; sticks and analog triggers are passed through, and controllers can be connected or disconnected dynamically while the game is running. AnyPS5 uses its built-in keyboard and mouse bindings when no configuration file is present. To change selected keyboard and mouse bindings, create `anyps5-input.ini` beside the generated game executable. Set `ANYPS5_INPUT_CONFIG` to use a file at another path. Both the game directory and that path may contain any characters, such as `ü` or `日本`. For an Xbox-layout controller, A/B/X/Y map to Cross/Circle/Square/Triangle, LB/RB map to L1/R1, and LT/RT map to analog L2/R2. Start maps to Options. D-pad and stick-click buttons are supported. On a PlayStation controller, touchpad clicks and the positions of up to two fingers on the touchpad are forwarded. On other controllers, View (Back) presses the touchpad at its center. diff --git a/docs/user/USAGE.md b/docs/user/USAGE.md index 029554069d..3a4dc187ee 100644 --- a/docs/user/USAGE.md +++ b/docs/user/USAGE.md @@ -69,7 +69,7 @@ All switches are disabled by default. `unused-filter` defaults to `0`; `--rpath` | `--linux-fixed-base
` | Load the Linux executable at this fixed address instead of the default `0x400000`, given in decimal or hexadecimal (`0x`): every virtual address of the image is offset by it, so virtual address 0 is placed at the address, as the console places an executable. Requires a 64 KiB-aligned address from `0x10000` through `0x7fffffff0000` that is a multiple of every `PT_LOAD` alignment, and an image that ends in the user address space. The executable is written as `ET_EXEC`, without ASLR, as it is at the default address. Bundled modules keep their normal placement. Conflicts with `--windows`, `--macos` and `--lazy-binding`. | | `--macos-fixed-base
` | Map the lowest ELF load page of the macOS executable at this fixed address, given in decimal or hexadecimal (`0x`). Requires `--macos`, a 64 KiB-aligned address from `0x20000` through `0x7fffffff0000`, and an image that ends in the macOS user address space. `__PAGEZERO` ends 64 KiB below the address and the Mach-O header occupies those 64 KiB. The executable has no ASLR; an address the process cannot map fails to load. Bundled modules keep their normal placement. | | `--to-intel` | Convert supported AMD-only instructions in the executable and bundled modules. Unsupported instructions or unreachable conversion stubs cause an error. | -| `--to-rosetta` | Convert as `--to-intel` does and also lower what Rosetta lacks: ANDN becomes scalar NOT/AND, RDSEED becomes RDRAND, CLWB a NOP, and RDPID and the ECX of RDTSCP return processor 0. Implies `--to-intel`. Independent of `--macos`: `--macos` alone leaves these instructions as they are. Conflicts with `--windows`. | +| `--to-rosetta` | Convert as `--to-intel` does and also lower what Rosetta lacks: ANDN becomes scalar NOT/AND, RDSEED becomes RDRAND, CLWB a NOP, XSAVEOPT becomes XSAVE, and RDPID and the ECX of RDTSCP return processor 0. Implies `--to-intel`. Independent of `--macos`: `--macos` alone leaves these instructions as they are. Conflicts with `--windows`. | | `unused-filter=0` | Keep all imported NID references. | | `unused-filter=1` | Filter unused non-PLT imports using control-flow and GOT access analysis, treating address-taken code as reachable; preserve PLT imports. | | `unused-filter=2` | Apply strict unused-import analysis and compact the PLT. Unsupported analysis cases cause an error. | @@ -77,7 +77,7 @@ All switches are disabled by default. `unused-filter` defaults to `0`; `--rpath` | `--rpath ` | Set the system library search path. Quote `$ORIGIN` to prevent shell expansion, for example `--rpath '$ORIGIN/libs'` in Bash or PowerShell. Linux guest modules require an absolute path or a path beginning with `$ORIGIN`. Windows requires a nonempty ASCII path and supports `$ORIGIN` as the executable directory. | | `--autorun` | Run the output after conversion, print its exit code, and wait for Enter. Adds executable permissions for Linux output. Requires the target OS and prepared runtime layout. | | `--sce-module-path ` | Parent of the module directories, not the module directory itself. Default: input ELF parent; relative input paths resolve from the process working directory. Explicit absolute path: used directly. Explicit relative path (including `.` and `..`): relative to the process working directory. Output path has no effect. Requires one nonempty value; repeats, nonexistent or nondirectory paths, access failures, and combination with `--skip-sce-module` are errors. | -| `--module-dir ` | Relink the ELF modules of a directory under the module parent directory, besides `sce_module`/`sce_modules`/`prx`; outputs keep the relative path. A module another module needs is initialized at startup, and on Linux and macOS also one the executable needs; any other when the title loads it with `sceKernelLoadStartModule`, with its start arguments (for example Unity plugins in `Media/Plugins`, or `mono-ps4.sprx` on Windows). Repeatable; `.` is the parent itself, without the input ELF. Conflicts with `--skip-sce-module`. | +| `--module-dir ` | Relink the ELF modules of a directory under the module parent directory, besides `sce_module`/`sce_modules`/`prx`; outputs keep the relative path. Needed modules are mapped at startup. On Linux, modules in these extra directories initialize only when explicitly loaded, with the arguments from `sceKernelLoadStartModule` (for example Unity plugins in `Media/Plugins`). A module another module needs initializes at startup on Windows and macOS; one only the executable needs initializes when explicitly loaded on Windows and at startup on macOS. Other modules initialize when loaded. Repeatable; `.` is the parent itself, without the input ELF. Conflicts with `--skip-sce-module`. | | `--executable ` | Also convert another executable of the title, which it starts with `sceSystemServiceLoadExec`; the path is relative to the module parent directory, for example `picker/picker.bin`. It is written beside the output, named after the path with `/` replaced by `.` and given the output's extension (`picker.picker.bin.exe`), and uses the same `libs/`, `app0/` and guest modules. It is an error if a guest module converts differently for it. Repeatable. Conflicts with `--skip-sce-module`. | | `--patches ` | Apply the byte patches of this file during conversion, see [game patches](#game-patches). Overrides `relinker-patches.toml` discovery. Conflicts with `--no-patches`. | | `--no-patches` | Do not apply patches, and do not load `relinker-patches.toml` from the input directory. | @@ -87,6 +87,7 @@ All switches are disabled by default. `unused-filter` defaults to `0`; `--rpath` | `--exclude-sce-module ` | Deprecated. Exclude a bundled module by exact filename, not path. Repeat for multiple files; a missing filename is an error. Conflicts with `--skip-sce-module`. | | `--skip-syscall-check` | Deprecated. Disable syscall scanning in the executable and bundled modules. | | `--trap-sdk-syscall-stubs` | Replace the exact ps5-payload-dev generic and numbered syscall wrapper stubs and the recognized inline mmap/mprotect sequences from SDK libc/mman.c with `UD2`, so unused wrappers do not block relinking. Other syscall instructions still cause an error. If a trapped wrapper runs, it raises an illegal-instruction exception. For debugging only. | +| `--frame-red-zone` | Windows only: give leaf functions that keep values below RSP (the System V red zone) a frame, so an exception Windows delivers on a guest thread cannot overwrite them. Covers leaves with a `push rbp; mov rbp, rsp; push ...` prologue and leaves that only push registers, with or without a `sub` allocation after the pushes; in those, RSP is lowered only as far as the leaf reaches below it and their RSP operands are moved with it, in place or, when their encoding cannot hold the moved displacement, in a stub. Function bounds come from the executable's `.eh_frame`, and the relinker stops with an error when it has none. The relinker prints how many it framed and why it skipped the others, and refuses a game patch over a framed site. | | `--lazy-binding` | Deprecated. Enable lazy symbol binding instead of eager binding. Incompatible with bundled ELF modules. The Linux executable stays position independent (`ET_DYN`) instead of being placed at `0x400000`. | Specify `unused-filter=0|1|2` without `--`, at most once. Unknown options and extra positional arguments are errors. Use `relinker --help` to print the usage syntax to stdout and exit with code `0` without converting files. Invoking `relinker` without arguments prints the usage syntax to stderr and exits with code `1`. @@ -99,6 +100,32 @@ The `--skip-sce-module`, `--exclude-sce-module `, `--skip-syscall-check`, A patch file changes bytes of the input executable or of a bundled module before conversion, for a title that hard-codes a value the host cannot meet. The relinker checks the bytes it replaces, so a patch for another version of the title fails instead of corrupting it. With no patch file the output is unchanged. +The advisor requires Python 3.11 or newer. To check a local patch catalog, run: + +```sh +python3 tools/suggest_patches.py --catalog --param-json --input --relinker [--sce-module-path ] [--module-dir ]... [--executable ]... [--skip-sce-module] [--timeout-seconds ] +``` + +The advisor reads `titleId` and `contentVersion` from the specified `param.json`, checks the SHA-256 of every file targeted by a candidate patch, then calls the relinker specified by `--relinker` to run `--verify-patches`. A relative path such as `--relinker ./relinker` resolves to that local file. The explicit metadata path also works when `param.json` and the unwrapped ELF files are stored separately. The catalog directory must contain an `index.json` with `format: 1` and an `entries` array. Each entry has `titleId`, `contentVersion`, a relative `patch` path, and `targets` mapping every patch target to its SHA-256. Hexadecimal digests are case-insensitive. For example: + +```json +{ + "format": 1, + "entries": [{ + "titleId": "TEST00001", + "contentVersion": "01.000.000", + "patch": "patch.toml", + "targets": {"Media/Modules/module.prx": ""} + }] +} +``` + +The advisor validates patch files only for entries matching the requested title and version. It reports and skips a malformed matching entry or a relinker verification error, so another valid entry can still be recommended. Verification times out after 120 seconds per entry by default; use `--timeout-seconds ` to change the limit. For synthetic inputs without system modules, use `--skip-sce-module` if conversion also uses it; this option conflicts with `--sce-module-path`. No matching entry or no verified patch exits `1`; invalid input or no verified patch after a relinker error exits `2`. If multiple entries verify, the advisor recommends none because it cannot choose or combine them. The tool does not fetch or apply patches. A recommendation prints `--patches` and the module and executable options to use when converting. Keep catalog patch files outside the input executable's directory to avoid automatic `relinker-patches.toml` discovery. + +The target hashes identify the exact unwrapped files changed by a patch, which might not include the main executable. Different dump tools could produce different bytes for the same title version; the advisor then rejects a patch even if it would work. It has not been confirmed that all dump tools produce identical files. The hashes do not authenticate a catalog or establish that a patch is safe or that the title is fully playable. Use catalogs from sources you trust and review the patch before applying it. + +If the patch targets another title executable, pass `--executable ` to the advisor for each additional executable, using paths relative to the module parent directory, such as `picker/picker.bin`. The advisor hashes those target files and forwards the same options to `relinker --verify-patches`. Use the same `--executable` options when converting with `--patches`. + `relinker-patches.toml` in the directory of the input executable is loaded automatically and announced as `Patches: `. `--patches ` names another file, and `--no-patches` loads none. Each applied patch is reported: ``` @@ -176,7 +203,7 @@ Resources can also be packed into `app0.zar` beside `app0/`, a [ZArchive](https: Use the generated files printed as `Guest module:` for bundled title modules. `libs/` is for AnyPS5 system libraries, not the original PS5 `.prx` files. Placing an original PS5 module in `libs/` on Windows makes Windows try to load it as a DLL and can fail with error 193 (not a valid Win32 application). A module the title loads while it runs that was not relinked, such as one in a directory not given to `--module-dir`, is not loaded either: the title gets an error and continues, and the runtime prints `[module] dlopen: was not relinked: ...` once for each file. Relink that module's directory with `--module-dir `. -On Windows, a self-built `libs/` also needs `libgcc_s_seh-1.dll`, `libstdc++-6.dll` and `libwinpthread-1.dll` from the `mingw64/bin` directory of the toolchain that built the libraries; the release archives already contain them. The libraries are loaded without searching `PATH`, so a copy elsewhere on the system is not used, and a library that needs one of them fails with error 126 (the specified module could not be found) although its `.prx` file is present. +On Windows, a self-built `libs/` also needs `libgcc_s_seh-1.dll`, `libstdc++-6.dll` and `libwinpthread-1.dll` from the `mingw64/bin` directory of the toolchain that built the libraries; the release archives already contain them. The libraries are loaded without searching `PATH`, so a copy elsewhere on the system is not used, and a library that needs one of them fails with error 126 (the specified module could not be found) although its `.prx` file is present. With `APS5_MIMALLOC` set, the title loads `mimalloc.dll` and `mimalloc-redirect.dll` from `libs/` and allocates through mimalloc. Copy both from `build/core/libs/libs`; the release archives contain them. Without them the title keeps the C runtime's heap and prints nothing. On Windows, direct memory (`sceKernelAllocateDirectMemory`, up to 13824 MiB per title) is committed when the title maps it, not when the title allocates it or first uses its pages. The system commit limit (installed memory plus page file size, the second value of Committed in Task Manager) must cover the mapped direct memory together with all other committed memory. Otherwise `sceKernelMapDirectMemory` throws `replace guest placeholder with resident direct memory` with the Windows error (`commit direct memory pages` when a page is mapped at a second address, `keep unmapped direct memory` when it is unmapped); enlarge the page file or close other applications. diff --git a/tools/configure_seed.py b/tools/configure_seed.py index 4e0a82372c..1dd9e52f29 100644 --- a/tools/configure_seed.py +++ b/tools/configure_seed.py @@ -4,7 +4,7 @@ import re import sys from pathlib import Path -CHECK = re.compile(r"^//(Have (include|includes|function|symbol|library) |Test |Result of (TRY_COMPILE|TRY_RUN|TEST_BIG_ENDIAN|check_))") +CHECK = re.compile(r"^//(Have (include|includes|function|symbol|library) |Test |Result of (TRY_COMPILE|TRY_RUN|TEST_BIG_ENDIAN|check_)|CHECK_TYPE_SIZE: )") ENTRY = re.compile(r"^([^#/:][^:]*):([A-Z]+)=(.*)$") diff --git a/tools/env_reference.py b/tools/env_reference.py index 8641a3f60b..2cfe032b71 100644 --- a/tools/env_reference.py +++ b/tools/env_reference.py @@ -163,6 +163,9 @@ def check(found, doc, prefixes, out): def main(argv=None): + for stream in (sys.stdout, sys.stderr): + if hasattr(stream, "reconfigure"): + stream.reconfigure(encoding="utf-8", errors="backslashreplace") parser = argparse.ArgumentParser( description="list the environment names the code reads, or check them against a reference document") parser.add_argument("--check", action="store_true", diff --git a/tools/import_audit.py b/tools/import_audit.py index 1e6e096acf..5d43653497 100644 --- a/tools/import_audit.py +++ b/tools/import_audit.py @@ -86,14 +86,18 @@ def elf_program_dynamic_symbols(path, data): raise AuditError(f"{path}: dynamic segment size {dynamic_size} is not a multiple of 16") dynamic = {} needed_offsets = [] + terminated = False for position in range(0, dynamic_size, 16): tag, value = unpack(" 3 and parts[:3] == ("core", "libs", "prx") else "" -def same_exports(pa, pb, shared): - common = (export_ids(pa) & export_ids(pb)).difference(*(export_ids(pr) for pr in shared)) - named = {name for name in pa["exports"] | pb["exports"] if compute_nid(name) in common} - return named | common.difference(compute_nid(name) for name in named) +def source_exports(text, library=""): + found = {} + for match in nid_names.source_definitions(text): + name = nid_names.strip_nid_postfix(match.group(1)) + if not name.endswith(("_nid_no_patch", "_nid_no_patch_cut")): + found.setdefault((library, nid_names.compute_nid(name)), set()).add(name) + for name in nid_names.source_assembly_definitions(text): + name = nid_names.strip_nid_postfix(name) + if not name.endswith(("_nid_no_patch", "_nid_no_patch_cut")): + found.setdefault((library, nid_names.compute_nid(name)), set()).add(name) + for nid, name in nid_names.source_aliases(text): + found.setdefault((library, nid), set()).add(name) + return found + + +def source_changed_exports(diff, prefix): + found = {} + for path, text in changed_source(diff, prefix): + library = library_for_path(path) + for key, names in source_exports(text, library).items(): + found.setdefault(key, set()).update(names) + file_diff = "\n".join(prefix + line for line in text.splitlines()) + for name in exports(file_diff, prefix): + canonical = nid_names.strip_nid_postfix(name) + found.setdefault((library, nid_names.compute_nid(canonical)), set()).add(canonical) + return found + + +def base_exports(ref): + root = git("rev-parse", "--show-toplevel").strip() + archive = subprocess.run( + ["git", "-C", root, "archive", "--format=tar", ref, "core/libs"], capture_output=True, check=True + ).stdout + found = {} + with tarfile.open(fileobj=io.BytesIO(archive), mode="r:") as source: + for entry in source: + path = Path(entry.name) + if not entry.isfile() or path.suffix != ".cpp" or any( + part.lower() in {"test", "tests"} for part in path.parts[2:-1]): + continue + text = source.extractfile(entry).read().decode("utf-8", errors="replace") + library = library_for_path(entry.name) + for key, names in source_exports(text, library).items(): + found.setdefault(key, set()).update(names) + return found def scan(pr): @@ -151,7 +237,8 @@ def scan(pr): pr["added"].add(path) diff = git("diff", "-U0", "--no-color", old, new, "--", "core/libs/prx/*.cpp") pr["exports"] = exports(diff) - pr["aliases"] = aliases(diff) + pr["added_exports"] = source_changed_exports(diff, "+") + pr["removed_exports"] = source_changed_exports(diff, "-") def scan_all(prs, jobs): @@ -206,7 +293,7 @@ def overlaps(prs, a, b): found = {"added": (pa["added"] & pb["added"]).difference(*(pr["added"] for pr in shared))} if pa["author"] != pb["author"]: found["conflicts"] = conflicts(pa, pb) - found["exports"] = same_exports(pa, pb, shared) + found["exports"] = (pa["exports"] & pb["exports"]).difference(*(pr["exports"] for pr in shared)) if not (pa["tree"] and pb["tree"]): uncompared = pa.get("files", set()) & pb.get("files", set()) if uncompared: @@ -227,9 +314,10 @@ def pull(blob, number): return f'[PR {number}]({blob.split("/blob/", 1)[0].replace("https://github.com/", "https://www.github.com/", 1)}/pull/{number})' -def report(rows, blob): +def report(rows, blob, main_exports=None): if not rows: - return "" + if not main_exports: + return "" confirmed = {number: found for number, found in rows.items() if found.get("conflicts") or found.get("exports") or found["added"]} uncompared = {number: found["uncompared"] for number, found in rows.items() if found.get("uncompared")} @@ -244,6 +332,12 @@ def report(rows, blob): ]) for number, found in sorted(confirmed.items()): lines.append(f'| {pull(blob, number)} | {spans(found.get("conflicts"), blob)} | {cell(found.get("exports"))} | {cell(found["added"])} |') + if main_exports: + if confirmed: + lines.append("") + lines.append("Exports introduced here that are already present in `main`:") + lines.append("") + lines.append(f"- {cell(main_exports)}") if uncompared: if confirmed: lines.append("") @@ -259,10 +353,24 @@ def report(rows, blob): return "\n".join(lines) + "\n" -def write_reports(out, rows, blob): +def write_reports(out, rows, blob, main_exports=None): + main_exports = main_exports or {} out.mkdir(parents=True, exist_ok=True) for number, found in rows.items(): - (out / f"{number}.md").write_text(report(found, blob), encoding="utf-8") + (out / f"{number}.md").write_text(report(found, blob, main_exports.get(number)), encoding="utf-8") + + +def main_export_findings(prs, current_exports): + findings = {} + for number, pr in prs.items(): + candidates = set(pr.get("added_exports", {})) - set(pr.get("removed_exports", {})) + names = set() + for key in candidates & set(current_exports): + names.update(pr["added_exports"][key]) + names.update(current_exports[key]) + if names: + findings[number] = names + return findings def describe_failure(error): @@ -288,10 +396,7 @@ def main(argv=None): if args.jobs < 1: parser.error("--jobs must be at least 1") global REPO - if args.out: - args.out = args.out.resolve() try: - os.chdir(git("rev-parse", "--show-toplevel").strip()) REPO = args.repo or upstream() prs = open_prs(args.base) fetch(args.base, prs) @@ -300,7 +405,9 @@ def main(argv=None): prs = own(prs, sha) prs[0] = {"ref": sha, "author": None, "depends": set()} scan_all(prs, args.jobs) + current_exports = base_exports("refs/pr/base") rows = {n: {} for n in prs} + main_exports = main_export_findings(prs, current_exports) pairs = [(0, n) for n in prs if n] if args.branch else combinations(prs, 2) for a, b in pairs: if found := overlaps(prs, a, b): @@ -309,9 +416,9 @@ def main(argv=None): except subprocess.CalledProcessError as error: raise SystemExit(describe_failure(error)) if args.branch: - print(report(rows[0], blob) or "No overlap with open pull requests.", end="") + print(report(rows[0], blob, main_exports.get(0)) or "No overlap with open pull requests.", end="") return 0 - write_reports(args.out, rows, blob) + write_reports(args.out, rows, blob, main_exports) return 0 diff --git a/tools/progress.py b/tools/progress.py index 153975e4d5..a3c38e1e83 100644 --- a/tools/progress.py +++ b/tools/progress.py @@ -441,8 +441,46 @@ def compare(title, column, unit, base, head): return lines + [""] +def library_progress(group): + if group is None: + return "Not present", None + if "shared_sources" in group: + return f'Shared sources: `{group["shared_sources"]}`', None + if "done" not in group or "todo" not in group: + return "Counts unavailable", None + total = group["done"] + group["todo"] + if not total: + return "No exports", None + percent = 100 * group["done"] / total + return f'{group["done"]}/{total} ({percent:.2f}%)', percent + + +def library_report(base, head): + before, after = ({group["name"]: group for group in data["groups"]} for data in (base, head)) + fields = ("done", "todo", "done_names", "todo_names", "shared_sources") + rows = [] + for name in sorted(before.keys() | after.keys(), key=str.casefold): + old, new = before.get(name), after.get(name) + if old is not None and new is not None and all(old.get(field) == new.get(field) for field in fields): + continue + old_text, old_percent = library_progress(old) + new_text, new_percent = library_progress(new) + delta = "n/a" + if old_percent is not None and new_percent is not None: + delta = f'{round(new_percent - old_percent, 2) or 0:+.2f} pp' + rows.append(f"| {name} | {old_text} | {new_text} | {delta} |") + if not rows: + return [] + lines = ["#### Changed libraries (implemented exports)", "", + "| Library | Before | After | Change |", "| - | - | - | - |"] + rows[:REPORT_ROWS] + if len(rows) > REPORT_ROWS: + lines.append(f"| ... | {len(rows) - REPORT_ROWS} more libraries | | |") + return lines + [""] + + def report(base, head): lines = compare("System libraries", "Library", "functions", base["libraries"], head["libraries"]) + lines += library_report(base["libraries"], head["libraries"]) lines += compare("GPU shader instructions", "Encoding", "instructions", base["shaders"], head["shaders"]) return "\n".join(lines) diff --git a/tools/select_tests.py b/tools/select_tests.py index 436c30441d..02b15cfbbe 100644 --- a/tools/select_tests.py +++ b/tools/select_tests.py @@ -89,6 +89,10 @@ class Build: leaves.add(name) return sorted(leaves) + def outputs(self, paths): + built = {line.rpartition(": ")[0] for line in self.run("-t", "targets", "all").splitlines()} + return [path for path in paths if path in built] + def tests(self, *arguments): listing = json.loads(subprocess.run(["ctest", "--test-dir", self.path, "--show-only=json-v1", *arguments], capture_output=True, text=True, check=True).stdout) @@ -143,6 +147,13 @@ def relative(path, root): return path[root_len + 1:].replace(os.sep, "/") +def paths(arguments): + for argument in arguments: + value = argument.partition("=")[2] if argument.startswith("-D") else argument + if os.path.isabs(value): + yield value + + def chunks(names): batch, size = [], 0 for name in names: @@ -193,7 +204,7 @@ def dump(build, out): registered.add(build.repo(graph["files"][graph["nodes"][node]["file"]])) node = graph["nodes"][node].get("parent") props = {p["name"]: p["value"] for p in test.get("properties", [])} - arguments = sorted({build.repo(a) for a in (test.get("command") or [])[1:] if os.path.isabs(a)} - {None}) + arguments = sorted({build.repo(a) for a in paths((test.get("command") or [])[1:])} - {None}) tests[test["name"]] = {"registered": sorted(registered - {None}), "arguments": arguments, "requires": props.get("FIXTURES_REQUIRED", []), "sets": props.get("FIXTURES_SETUP", []), "depends": props.get("DEPENDS", []), "target": build.target(test)} @@ -254,9 +265,10 @@ def plan(build, graph, picked): run = [i for i, t in enumerate(listed, 1) if t["name"] in names] if any(not t.get("command") for t in listed if t["name"] in names): return run, build.leaves(), len(listed) - keep = {build.output(a) for t in listed if t["name"] in names for a in t.get("command") or [] if os.path.isabs(a)} + keep = {build.output(a) for t in listed if t["name"] in names for a in paths(t.get("command") or [])} skip = {build.target(t) for t in listed if t["name"] not in names} - keep - {None} - return run, [t for t in build.leaves() if t not in skip], len(listed) + targets = [t for t in build.leaves() if t not in skip] + return run, targets + build.outputs(sorted(keep - set(targets) - {None})), len(listed) def escape(name): diff --git a/tools/suggest_patches.py b/tools/suggest_patches.py new file mode 100644 index 0000000000..51d91d85a1 --- /dev/null +++ b/tools/suggest_patches.py @@ -0,0 +1,190 @@ +import argparse +import hashlib +import json +import os +import re +import shlex +import subprocess +import sys +import tomllib +from pathlib import Path, PurePosixPath + + +SHA256 = re.compile(r"[0-9a-fA-F]{64}\Z") +VERIFY_SUMMARY = re.compile(r"^Verified (\d+) patches: (\d+) ok, (\d+) not-checked, (\d+) mismatch$", re.MULTILINE) + + +def confined(root, name): + if not isinstance(name, str) or not name or "\\" in name: + raise ValueError(f"invalid relative path: {name!r}") + path = PurePosixPath(name) + if path.is_absolute() or any(part in (".", "..") for part in name.split("/")): + raise ValueError(f"invalid relative path: {name!r}") + resolved = (root / Path(*path.parts)).resolve() + if not resolved.is_relative_to(root.resolve()) or not resolved.is_file(): + raise ValueError(f"file missing or outside directory: {name}") + return resolved + + +def sha256(path): + digest = hashlib.sha256() + with path.open("rb") as source: + for chunk in iter(lambda: source.read(1024 * 1024), b""): + digest.update(chunk) + return digest.hexdigest() + + +def command_line(arguments): + return subprocess.list2cmdline(arguments) if os.name == "nt" else shlex.join(arguments) + + +def load_catalog(directory, title, version): + data = json.loads(confined(directory, "index.json").read_text(encoding="utf-8")) + if not isinstance(data, dict) or set(data) != {"format", "entries"} or type(data["format"]) is not int or data["format"] != 1 or not isinstance(data["entries"], list): + raise ValueError("catalog must have format 1 and an entries array") + entries = [] + for number, entry in enumerate(data["entries"], 1): + if not isinstance(entry, dict) or (entry.get("titleId"), entry.get("contentVersion")) != (title, version): + continue + entries.append((number, entry)) + return entries + + +def validate_entry(directory, entry): + if set(entry) != {"titleId", "contentVersion", "patch", "targets"}: + raise ValueError("invalid fields") + patch = confined(directory, entry["patch"]) + targets = entry["targets"] + if not isinstance(targets, dict) or not targets: + raise ValueError("targets must be a nonempty object") + if any(not isinstance(name, str) or not isinstance(digest, str) or not SHA256.fullmatch(digest) for name, digest in targets.items()): + raise ValueError("invalid target digest") + parsed = tomllib.loads(patch.read_text(encoding="utf-8")) + patches = parsed.get("patch") + if not isinstance(patches, list) or not patches or any(not isinstance(item, dict) or not isinstance(item.get("target"), str) for item in patches): + raise ValueError("patch file has no valid targets") + if set(targets) != {item["target"] for item in patches}: + raise ValueError("target hashes must cover exactly the patch targets") + return patch, {name: digest.lower() for name, digest in targets.items()} + + +def main(argv=None): + parser = argparse.ArgumentParser(description="Recommend a local patch only after metadata, hashes and relinker verification match") + parser.add_argument("--catalog", required=True, type=Path, help="directory containing index.json and patch files") + parser.add_argument("--param-json", required=True, type=Path, help="title metadata file") + parser.add_argument("--input", required=True, type=Path, help="unwrapped main ELF") + parser.add_argument("--relinker", required=True, type=Path, help="relinker executable to run") + parser.add_argument("--sce-module-path", type=Path, help="parent directory of bundled modules") + parser.add_argument("--module-dir", action="append", default=[], help="module directory relative to the module parent; repeatable") + parser.add_argument("--executable", action="append", default=[], help="additional title executable relative to the module parent; repeatable") + parser.add_argument("--skip-sce-module", action="store_true", help="skip bundled modules during verification") + parser.add_argument("--timeout-seconds", type=int, default=120, help="maximum seconds per relinker verification (default: 120)") + args = parser.parse_args(argv) + try: + metadata = json.loads(args.param_json.read_text(encoding="utf-8")) + if not isinstance(metadata, dict): + raise ValueError("param.json must be an object") + title = metadata.get("titleId") + version = metadata.get("contentVersion") + if not isinstance(title, str) or not title or not isinstance(version, str) or not version: + raise ValueError("param.json needs nonempty titleId and contentVersion") + if not args.input.is_file() or not args.relinker.is_file(): + raise ValueError("input and relinker must be files") + if args.skip_sce_module and args.sce_module_path: + raise ValueError("--skip-sce-module conflicts with --sce-module-path") + if args.timeout_seconds <= 0: + raise ValueError("timeout-seconds must be positive") + root = (args.sce_module_path or args.input.parent).resolve() + if not root.is_dir(): + raise ValueError("module parent must be a directory") + for executable in args.executable: + confined(root, executable) + matches = [] + entries = load_catalog(args.catalog.resolve(), title, version) + if not entries: + print(f"No recommendation: no catalog entry for {title} {version}.") + return 1 + relinker_errors = False + for number, entry in entries: + label = f"Catalog entry {number} ({entry.get('patch')!r})" + try: + patch, targets = validate_entry(args.catalog.resolve(), entry) + except (OSError, ValueError, tomllib.TOMLDecodeError) as error: + print(f"{label}: rejected: {error}") + continue + errors = [] + for name, expected in targets.items(): + try: + source = args.input.resolve() if name == args.input.name else confined(root, name) + actual = sha256(source) + if actual != expected: + errors.append(f"{name}: SHA-256 mismatch ({actual})") + except (OSError, ValueError) as error: + errors.append(f"{name}: {error}") + if errors: + print(f"{label}: rejected: {'; '.join(errors)}") + continue + command = [str(args.relinker.resolve()), "--verify-patches", str(patch)] + if args.skip_sce_module: + command.append("--skip-sce-module") + if args.sce_module_path: + command.extend(("--sce-module-path", str(args.sce_module_path))) + for module_dir in args.module_dir: + command.extend(("--module-dir", module_dir)) + for executable in args.executable: + command.extend(("--executable", executable)) + command.append(str(args.input)) + try: + result = subprocess.run(command, capture_output=True, text=True, timeout=args.timeout_seconds) + except subprocess.TimeoutExpired: + print(f"{label}: rejected: relinker verification exceeded {args.timeout_seconds} seconds") + continue + summary = VERIFY_SUMMARY.search(result.stdout) + verified = tuple(map(int, summary.groups())) if summary else None + if result.returncode: + detail = (result.stdout + result.stderr).strip() + if result.returncode == 1 and verified and verified[3] > 0: + print(f"{label}: rejected by relinker: {detail}") + continue + if result.returncode == 2: + print(f"{label}: relinker error (exit 2): {detail}") + relinker_errors = True + continue + raise ValueError(f"{label}: relinker error (exit {result.returncode}): {detail}") + if not verified: + raise ValueError(f"{label}: relinker produced no verification summary") + if verified[0] == 0 or verified[1] != verified[0] or verified[2] or verified[3]: + print(f"{label}: rejected: relinker did not confirm every patch") + continue + matches.append((number, entry, patch, targets)) + if len(matches) == 1: + number, entry, patch, targets = matches[0] + print(f"Recommended: {patch} (catalog entry {number})") + for name, digest in targets.items(): + print(f" {name} SHA-256 {digest}") + conversion = ["--patches", str(patch)] + if args.skip_sce_module: + conversion.append("--skip-sce-module") + if args.sce_module_path: + conversion.extend(("--sce-module-path", str(args.sce_module_path))) + for module_dir in args.module_dir: + conversion.extend(("--module-dir", module_dir)) + for executable in args.executable: + conversion.extend(("--executable", executable)) + print(f"Use these options when converting:\n {command_line(conversion)}") + return 0 + if len(matches) > 1: + print("No recommendation: multiple verified catalog entries match.") + return 1 + if relinker_errors: + print("No recommendation: relinker errors occurred; see catalog entries above.", file=sys.stderr) + return 2 + print("No recommendation: no catalog entry passed all checks.") + return 1 + except (OSError, ValueError, json.JSONDecodeError, tomllib.TOMLDecodeError) as error: + print(f"Patch advisor: {error}", file=sys.stderr) + return 2 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/tools/tests/test_configure_seed.py b/tools/tests/test_configure_seed.py index ec7253a1fe..36d6c6a66d 100644 --- a/tools/tests/test_configure_seed.py +++ b/tools/tests/test_configure_seed.py @@ -1,3 +1,4 @@ +import ctypes import os import shutil import subprocess @@ -17,11 +18,20 @@ project(mini C) include(CheckIncludeFile) include(CheckSymbolExists) include(CheckCSourceCompiles) +include(CheckTypeSize) check_include_file(stdio.h MINI_HAVE_STDIO_H) check_include_file(mini_missing_header.h MINI_HAVE_MISSING_H) check_symbol_exists(printf stdio.h MINI_HAVE_PRINTF) check_c_source_compiles("int main(void) { return 0; }" MINI_COMPILES) check_c_source_compiles("int main(void) { return undefined_name; }" MINI_FAILS) +check_type_size(int MINI_SIZEOF_INT) +check_type_size(mini_no_such_type_t MINI_SIZEOF_NONE) +if(NOT HAVE_MINI_SIZEOF_INT OR NOT MINI_SIZEOF_INT) + message(FATAL_ERROR "Unsupported integer size: int") +endif() +if(HAVE_MINI_SIZEOF_NONE OR NOT "${MINI_SIZEOF_NONE}" STREQUAL "") + message(FATAL_ERROR "A missing type has a size") +endif() set(MINI_OPTION "value" CACHE STRING "An option, not a check result") """ @@ -158,8 +168,9 @@ class ConfigureSeedProject(unittest.TestCase): class ConfigureSeedTests(ConfigureSeedProject): def test_only_check_results_are_seeded(self): found = configure_seed.checks(self.root / "fresh" / "CMakeCache.txt") - self.assertEqual({n for n in found if n.startswith("MINI_")}, - {"MINI_HAVE_STDIO_H", "MINI_HAVE_MISSING_H", "MINI_HAVE_PRINTF", "MINI_COMPILES", "MINI_FAILS"}) + self.assertEqual({n for n in found if "MINI_" in n}, + {"MINI_HAVE_STDIO_H", "MINI_HAVE_MISSING_H", "MINI_HAVE_PRINTF", "MINI_COMPILES", "MINI_FAILS", + "HAVE_MINI_SIZEOF_INT", "MINI_SIZEOF_INT", "HAVE_MINI_SIZEOF_NONE", "MINI_SIZEOF_NONE"}) self.assertNotIn("CMAKE_C_COMPILER", found) def test_cache_without_checks_writes_an_empty_seed(self): @@ -202,13 +213,17 @@ class ConfigureSeedRoundTripTests(ConfigureSeedProject): self.assertNotIn("HAVE:INTERNAL=", seeded.read_text(encoding="utf-8")) def test_seeded_configure_runs_no_check_and_gets_the_same_results(self): - self.assertRegex(self.fresh, r"Looking for|Performing Test") + self.assertRegex(self.fresh, r"Looking for|Performing Test|Check size of") output = self.configure("seeded", "-C", str(self.seed(self.root / "fresh" / "CMakeCache.txt", "seed.cmake"))) - self.assertNotRegex(output, r"Looking for|Performing Test") + self.assertNotRegex(output, r"Looking for|Performing Test|Check size of") fresh = configure_seed.checks(self.root / "fresh" / "CMakeCache.txt") self.assertEqual(configure_seed.checks(self.root / "seeded" / "CMakeCache.txt"), fresh) self.assertEqual(fresh["MINI_HAVE_STDIO_H"][0], "1") self.assertEqual(fresh["MINI_FAILS"][0], "") + self.assertEqual(fresh["HAVE_MINI_SIZEOF_INT"][0], "TRUE") + self.assertEqual(fresh["MINI_SIZEOF_INT"][0], str(ctypes.sizeof(ctypes.c_int))) + self.assertEqual(fresh["HAVE_MINI_SIZEOF_NONE"][0], "FALSE") + self.assertEqual(fresh["MINI_SIZEOF_NONE"][0], "") def test_values_with_cmake_syntax_round_trip(self): cache = self.root / "odd" / "CMakeCache.txt" diff --git a/tools/tests/test_env_reference.py b/tools/tests/test_env_reference.py index 0be6fc1f8e..c19bb714c9 100644 --- a/tools/tests/test_env_reference.py +++ b/tools/tests/test_env_reference.py @@ -3,7 +3,9 @@ import io import sys import tempfile import unittest +from unittest import mock from pathlib import Path +from unittest.mock import patch sys.path.insert(0, str(Path(__file__).resolve().parents[1])) import env_reference @@ -213,6 +215,64 @@ class EnvReferenceTests(unittest.TestCase): (core / "3rdparty" / "Ignored.cpp").write_text('static bool b = IsEnvironmentSet("APS5_IGNORED");\n', encoding="utf-8") self.assertEqual(set(env_reference.reads([core], BOTH)), {"APS5_TRACE_GPU"}) + def test_main_reconfigures_streams_for_unicode_report_paths(self): + class Stream: + def __init__(self): + self.encoding = "ascii" + self.errors = "strict" + self.text = "" + + def reconfigure(self, encoding=None, errors=None): + self.encoding = encoding + self.errors = errors + + def write(self, text): + text.encode(self.encoding, self.errors) + self.text += text + return len(text) + + def flush(self): + pass + + root = self.fixture() / "source-Ж" + root.mkdir() + core = root / "core" + core.mkdir() + (core / "Sample.cpp").write_text('static bool a = std::getenv("APS5_TRACE_GPU") != nullptr;\n', encoding="utf-8") + out, err = Stream(), Stream() + with mock.patch.object(env_reference.sys, "stdout", out), mock.patch.object(env_reference.sys, "stderr", err): + self.assertEqual(env_reference.main(["--root", str(core)]), 0) + self.assertEqual((out.encoding, out.errors), ("utf-8", "backslashreplace")) + self.assertEqual((err.encoding, err.errors), ("utf-8", "backslashreplace")) + self.assertIn((core / "Sample.cpp").as_posix(), out.text) + + def test_main_reports_unicode_scan_root_with_non_utf8_stderr(self): + class Stream: + def __init__(self): + self.encoding = "ascii" + self.errors = "strict" + self.text = "" + + def reconfigure(self, encoding=None, errors=None): + self.encoding = encoding + self.errors = errors + + def write(self, text): + text.encode(self.encoding, self.errors) + self.text += text + return len(text) + + def flush(self): + pass + + root = Path(tempfile.gettempdir()) / "missing-source-Ж" + out, err = Stream(), Stream() + with mock.patch.object(env_reference.sys, "stdout", out), mock.patch.object(env_reference.sys, "stderr", err): + self.assertEqual(env_reference.main(["--root", str(root)]), 1) + self.assertEqual((out.encoding, out.errors), ("utf-8", "backslashreplace")) + self.assertEqual((err.encoding, err.errors), ("utf-8", "backslashreplace")) + self.assertIn(str(root), err.text) + def test_main_rejects_a_scan_root_that_is_not_a_directory(self): root = self.fixture() for name in ("missing", "core/Sample.cpp"): @@ -225,5 +285,33 @@ class EnvReferenceTests(unittest.TestCase): self.assertNotIn("no longer reads", out.getvalue()) + def test_main_reconfigures_streams(self): + class Stream: + def __init__(self): + self.encoding = None + self.errors = None + + def reconfigure(self, encoding=None, errors=None): + self.encoding = encoding + self.errors = errors + + def write(self, text): + pass + + def flush(self): + pass + + out, err = Stream(), Stream() + with patch.object(env_reference.sys, "stdout", out), patch.object(env_reference.sys, "stderr", err): + try: + env_reference.main(["--root", "nonexistent"]) + except SystemExit: + pass + self.assertEqual(out.encoding, "utf-8") + self.assertEqual(out.errors, "backslashreplace") + self.assertEqual(err.encoding, "utf-8") + self.assertEqual(err.errors, "backslashreplace") + + if __name__ == "__main__": unittest.main() diff --git a/tools/tests/test_import_audit.py b/tools/tests/test_import_audit.py index 2c4ac001ee..c1acc164c6 100644 --- a/tools/tests/test_import_audit.py +++ b/tools/tests/test_import_audit.py @@ -1603,6 +1603,58 @@ class ImportAuditTests(unittest.TestCase): struct.pack_into("" + " -P ${CMAKE_SOURCE_DIR}/data/run.cmake)\n", encoding="utf-8") + subprocess.run(["cmake", "-S", str(self.source), "-B", str(self.build)], check=True, capture_output=True) + subprocess.run(["ninja", "-C", str(self.build), "-t", "clean"], check=True, capture_output=True) + graph = self.build / "run-graph.json" + self.tool("dump", self.build, graph) + self.assertEqual(self.select("beta/main.c", graph=graph)[1], {"alpha_run", "beta", "beta_setup", "script"}) + subprocess.run(["ninja", "-C", str(self.build), "-f", "select.ninja", "selected"], check=True, capture_output=True) + output = self.tool("ctest", self.build, "-R", "alpha_run", "--output-on-failure") + self.assertIn("100% tests passed", output) + finally: + cmake.write_text(original, encoding="utf-8") + script.unlink(missing_ok=True) + subprocess.run(["cmake", "-S", str(self.source), "-B", str(self.build)], check=True, capture_output=True) + subprocess.run(["cmake", "--build", str(self.build)], check=True, capture_output=True) + + def test_script_test_builds_an_excluded_executable_it_gets_as_a_definition(self): + cmake, script = self.source / "alpha/CMakeLists.txt", self.source / "data/run.cmake" + original = cmake.read_text(encoding="utf-8") + try: + script.write_text('execute_process(COMMAND "${TOOL}" RESULT_VARIABLE result)\n' + 'if(NOT result STREQUAL "0")\n message(FATAL_ERROR "tool: ${result}")\nendif()\n', + encoding="utf-8") + cmake.write_text(original + "add_executable(alpha_tool EXCLUDE_FROM_ALL main.c)\n" + "add_test(NAME alpha_run COMMAND ${CMAKE_COMMAND} -DTOOL=$" + " -P ${CMAKE_SOURCE_DIR}/data/run.cmake)\n", encoding="utf-8") + subprocess.run(["cmake", "-S", str(self.source), "-B", str(self.build)], check=True, capture_output=True) + subprocess.run(["ninja", "-C", str(self.build), "-t", "clean"], check=True, capture_output=True) + graph = self.build / "run-graph.json" + self.tool("dump", self.build, graph) + self.assertEqual(self.select("beta/main.c", graph=graph)[1], {"alpha_run", "beta", "beta_setup", "script"}) + subprocess.run(["ninja", "-C", str(self.build), "-f", "select.ninja", "selected"], check=True, capture_output=True) + output = self.tool("ctest", self.build, "-R", "alpha_run", "--output-on-failure") + self.assertIn("100% tests passed", output) + finally: + cmake.write_text(original, encoding="utf-8") + script.unlink(missing_ok=True) + subprocess.run(["cmake", "-S", str(self.source), "-B", str(self.build)], check=True, capture_output=True) + subprocess.run(["cmake", "--build", str(self.build)], check=True, capture_output=True) + def test_retargeted_test_does_not_prune_its_previous_executable(self): cmake = self.source / "alpha/CMakeLists.txt" original = cmake.read_text(encoding="utf-8") diff --git a/tools/tests/test_shader_cache_runtime_sources.py b/tools/tests/test_shader_cache_runtime_sources.py index 6aebbb7529..a0442a6d52 100644 --- a/tools/tests/test_shader_cache_runtime_sources.py +++ b/tools/tests/test_shader_cache_runtime_sources.py @@ -78,7 +78,7 @@ file(WRITE "${{CMAKE_CURRENT_BINARY_DIR}}/runtime-sources.txt" "${{runtimeSource def configure(self, flags=''): result = subprocess.run([CMAKE, '-S', str(self.root), '-B', str(self.build), '-G', 'Ninja', f'-DCMAKE_CXX_FLAGS={flags}'], - capture_output=True, encoding='utf-8', errors='replace', timeout=15) + capture_output=True, encoding='utf-8', errors='replace', timeout=120) if result.returncode: raise AssertionError(result.stdout + result.stderr) if (self.root / 'compiler-invoked').exists(): @@ -101,7 +101,7 @@ file(WRITE "${{CMAKE_CURRENT_BINARY_DIR}}/runtime-sources.txt" "${{runtimeSource f'-DRECOMPILER_DIR={self.recompiler.as_posix()}', f'-DLIST_FILE={self.listing.as_posix()}', f'-DOUTPUT={output.as_posix()}', f'-DEXTRA={self.extra}', '-P', str(self.recompiler / 'ShaderCacheVersion.cmake')], - capture_output=True, encoding='utf-8', errors='replace', timeout=10) + capture_output=True, encoding='utf-8', errors='replace', timeout=60) if not success: if result.returncode == 0 or output.exists(): raise AssertionError('The generator accepted an unkeyed dependency') @@ -147,7 +147,7 @@ class ShaderCacheRuntimeSourceTests(unittest.TestCase): 'if(NOT result EQUAL 99)', 'message(FATAL_ERROR "Unexpected compiler arguments were accepted")', 'endif()'] script.write_text('\n'.join(lines) + '\n', encoding='utf-8') result = subprocess.run([CMAKE, '-P', str(script)], capture_output=True, - encoding='utf-8', errors='replace', timeout=10) + encoding='utf-8', errors='replace', timeout=60) self.assertEqual(result.returncode, 0, result.stdout + result.stderr) self.assertEqual(len(marker.read_text(encoding='utf-8').splitlines()), len(rejected)) diff --git a/tools/tests/test_suggest_patches.py b/tools/tests/test_suggest_patches.py new file mode 100644 index 0000000000..0da25c6f5d --- /dev/null +++ b/tools/tests/test_suggest_patches.py @@ -0,0 +1,250 @@ +import contextlib +import hashlib +import io +import json +import subprocess +import sys +import tempfile +import unittest +from pathlib import Path +from unittest.mock import patch + +sys.path.insert(0, str(Path(__file__).resolve().parents[1])) +import suggest_patches + +VERIFIED = "Verified 1 patches: 1 ok, 0 not-checked, 0 mismatch\n" + + +class PatchAdvisorTests(unittest.TestCase): + def setUp(self): + self.directory = tempfile.TemporaryDirectory() + self.addCleanup(self.directory.cleanup) + self.root = Path(self.directory.name) + self.catalog = self.root / "catalog" + self.catalog.mkdir() + self.input = self.root / "eboot.bin" + self.input.write_bytes(b"main executable") + self.module = self.root / "Media" / "Modules" / "module.prx" + self.module.parent.mkdir(parents=True) + self.module.write_bytes(b"module version one") + self.param = self.root / "param.json" + self.param.write_text(json.dumps({"titleId": "TEST00001", "contentVersion": "01.000.000"})) + self.relinker = self.root / "relinker" + self.relinker.write_bytes(b"fixture") + (self.catalog / "patch.toml").write_text('format = 1\n[[patch]]\ntarget = "Media/Modules/module.prx"\nva = 0x1000\nexpected = "00"\nvalue = "01"\n') + self.entry = {"titleId": "TEST00001", "contentVersion": "01.000.000", "patch": "patch.toml", + "targets": {"Media/Modules/module.prx": hashlib.sha256(self.module.read_bytes()).hexdigest()}} + self.write_index() + + def write_index(self, entries=None): + (self.catalog / "index.json").write_text(json.dumps({"format": 1, "entries": entries if entries is not None else [self.entry]})) + + def advise(self, *extra): + return suggest_patches.main(["--catalog", str(self.catalog), "--param-json", str(self.param), + "--input", str(self.input), "--relinker", str(self.relinker), + "--module-dir", "Media/Modules", *extra]) + + def additional_executable(self): + executable = self.root / "picker" / "picker.bin" + executable.parent.mkdir() + executable.write_bytes(b"secondary executable") + (self.catalog / "patch.toml").write_text('format = 1\n[[patch]]\ntarget = "picker/picker.bin"\nva = 0x1000\nexpected = "00"\nvalue = "01"\n') + self.entry["targets"] = {"picker/picker.bin": hashlib.sha256(executable.read_bytes()).hexdigest()} + self.write_index() + return executable + + @patch.object(suggest_patches.subprocess, "run") + def test_verified_match_requires_explicit_application(self, run): + run.return_value.returncode = 0 + run.return_value.stdout = VERIFIED + self.assertEqual(self.advise(), 0) + self.assertEqual(run.call_args.args[0], [str(self.relinker), "--verify-patches", + str(self.catalog / "patch.toml"), "--module-dir", "Media/Modules", + str(self.input)]) + self.assertFalse((self.root / "relinker-patches.toml").exists()) + + @patch.object(suggest_patches.subprocess, "run") + def test_changed_module_is_not_sent_to_relinker(self, run): + self.module.write_bytes(b"module version two") + self.assertEqual(self.advise(), 1) + run.assert_not_called() + + @patch.object(suggest_patches.subprocess, "run") + def test_relinker_rejection_is_not_a_recommendation(self, run): + run.return_value.returncode = 1 + run.return_value.stdout = "Mismatch: patch\nVerified 1 patches: 0 ok, 0 not-checked, 1 mismatch\n" + run.return_value.stderr = "" + self.assertEqual(self.advise(), 1) + + @patch.object(suggest_patches.subprocess, "run") + def test_catalog_cannot_escape_directory(self, run): + self.entry["patch"] = "../outside.toml" + self.write_index() + self.assertEqual(self.advise(), 1) + run.assert_not_called() + + @patch.object(suggest_patches.subprocess, "run") + def test_hashes_cover_all_patch_targets(self, run): + self.entry["targets"] = {"eboot.bin": hashlib.sha256(self.input.read_bytes()).hexdigest()} + self.write_index() + self.assertEqual(self.advise(), 1) + run.assert_not_called() + + @patch.object(suggest_patches.subprocess, "run") + def test_additional_executable_is_hashed_and_verified(self, run): + self.additional_executable() + run.return_value.returncode = 0 + run.return_value.stdout = VERIFIED + self.assertEqual(self.advise("--executable", "picker/picker.bin"), 0) + self.assertEqual(run.call_args.args[0], [str(self.relinker), "--verify-patches", + str(self.catalog / "patch.toml"), "--module-dir", "Media/Modules", + "--executable", "picker/picker.bin", str(self.input)]) + + @patch.object(suggest_patches.subprocess, "run") + def test_changed_additional_executable_is_not_verified(self, run): + executable = self.additional_executable() + executable.write_bytes(b"different executable") + self.assertEqual(self.advise("--executable", "picker/picker.bin"), 1) + run.assert_not_called() + + @patch.object(suggest_patches.subprocess, "run") + def test_additional_executable_cannot_escape_module_parent(self, run): + self.assertEqual(self.advise("--executable", "../outside.bin"), 2) + run.assert_not_called() + + @patch.object(suggest_patches.subprocess, "run") + def test_unrelated_malformed_patch_does_not_block_recommendation(self, run): + bad = {"titleId": "OTHER0001", "contentVersion": "01.000.000", "patch": "missing.toml", "targets": {}} + (self.catalog / "index.json").write_text(json.dumps({"format": 1, "entries": [bad, self.entry]})) + run.return_value.returncode = 0 + run.return_value.stdout = VERIFIED + self.assertEqual(self.advise(), 0) + run.assert_called_once() + + @patch.object(suggest_patches.subprocess, "run") + def test_matching_malformed_patch_is_rejected(self, run): + self.entry["patch"] = "missing.toml" + self.write_index() + self.assertEqual(self.advise(), 1) + run.assert_not_called() + + @patch.object(suggest_patches.subprocess, "run") + def test_relinker_timeout_is_not_a_recommendation(self, run): + run.side_effect = subprocess.TimeoutExpired([str(self.relinker)], 7) + self.assertEqual(self.advise("--timeout-seconds", "7"), 1) + self.assertEqual(run.call_args.kwargs["timeout"], 7) + + @patch.object(suggest_patches.subprocess, "run") + def test_broken_matching_entry_does_not_block_valid_entry(self, run): + bad = dict(self.entry, patch="broken.toml") + (self.catalog / "broken.toml").write_text("format = @") + self.write_index([bad, self.entry]) + run.return_value.returncode = 0 + run.return_value.stdout = VERIFIED + output = io.StringIO() + with contextlib.redirect_stdout(output): + self.assertEqual(self.advise(), 0) + self.assertIn("Catalog entry 1 ('broken.toml'): rejected", output.getvalue()) + self.assertIn("Recommended:", output.getvalue()) + run.assert_called_once() + + @patch.object(suggest_patches.subprocess, "run") + def test_timeout_does_not_block_valid_entry(self, run): + self.write_index([self.entry, self.entry]) + run.side_effect = [subprocess.TimeoutExpired([str(self.relinker)], 7), subprocess.CompletedProcess([], 0, VERIFIED, "")] + self.assertEqual(self.advise("--timeout-seconds", "7"), 0) + self.assertEqual(run.call_count, 2) + + @patch.object(suggest_patches.subprocess, "run") + def test_relinker_error_without_verified_entries_exits_two(self, run): + run.return_value.returncode = 2 + run.return_value.stdout = "" + run.return_value.stderr = "FAIL: broken input" + self.assertEqual(self.advise(), 2) + + @patch.object(suggest_patches.subprocess, "run") + def test_relinker_error_does_not_block_verified_entry(self, run): + self.write_index([self.entry, self.entry]) + run.side_effect = [subprocess.CompletedProcess([], 2, "", "FAIL: invalid patch"), + subprocess.CompletedProcess([], 0, VERIFIED, "")] + output = io.StringIO() + with contextlib.redirect_stdout(output): + self.assertEqual(self.advise(), 0) + self.assertIn("Catalog entry 1 ('patch.toml'): relinker error (exit 2)", output.getvalue()) + self.assertIn("Recommended:", output.getvalue()) + self.assertEqual(run.call_count, 2) + + @patch.object(suggest_patches.subprocess, "run") + def test_relinker_error_and_mismatch_exit_two(self, run): + self.write_index([self.entry, self.entry]) + run.side_effect = [subprocess.CompletedProcess([], 2, "", "FAIL: invalid patch"), + subprocess.CompletedProcess([], 1, "Verified 1 patches: 0 ok, 0 not-checked, 1 mismatch\n", "")] + self.assertEqual(self.advise(), 2) + + @patch.object(suggest_patches.subprocess, "run") + def test_multiple_verified_entries_are_ambiguous(self, run): + self.write_index([self.entry, self.entry]) + run.return_value.returncode = 0 + run.return_value.stdout = VERIFIED + output = io.StringIO() + with contextlib.redirect_stdout(output): + self.assertEqual(self.advise(), 1) + self.assertIn("multiple verified catalog entries", output.getvalue()) + self.assertEqual(run.call_count, 2) + + @patch.object(suggest_patches.subprocess, "run") + def test_not_checked_patch_is_not_recommended(self, run): + run.return_value.returncode = 0 + run.return_value.stdout = "Verified 1 patches: 0 ok, 1 not-checked, 0 mismatch\n" + self.assertEqual(self.advise(), 1) + + @patch.object(suggest_patches.subprocess, "run") + def test_success_without_verification_summary_is_an_error(self, run): + run.return_value.returncode = 0 + run.return_value.stdout = "unrecognized verifier" + self.assertEqual(self.advise(), 2) + + @patch.object(suggest_patches.subprocess, "run") + def test_relinker_argument_error_is_not_a_patch_rejection(self, run): + run.return_value.returncode = 1 + run.return_value.stdout = "" + run.return_value.stderr = "FAIL: invalid option" + self.assertEqual(self.advise(), 2) + + @patch.object(suggest_patches.subprocess, "run") + def test_conflicting_module_options_are_rejected_before_relinker(self, run): + self.assertEqual(self.advise("--skip-sce-module", "--sce-module-path", str(self.root)), 2) + run.assert_not_called() + + @patch.object(suggest_patches.subprocess, "run") + def test_uppercase_digest_and_conversion_options(self, run): + self.additional_executable() + self.entry["targets"]["picker/picker.bin"] = self.entry["targets"]["picker/picker.bin"].upper() + self.write_index() + run.return_value.returncode = 0 + run.return_value.stdout = VERIFIED + output = io.StringIO() + with contextlib.redirect_stdout(output): + self.assertEqual(self.advise("--executable", "picker/picker.bin", "--sce-module-path", str(self.root)), 0) + self.assertIn("--patches", output.getvalue()) + self.assertIn("--module-dir Media/Modules", output.getvalue()) + self.assertIn("--executable picker/picker.bin", output.getvalue()) + self.assertIn(f"--sce-module-path {self.root}", output.getvalue()) + + @patch.object(suggest_patches.subprocess, "run") + def test_no_entries_for_title_is_distinct(self, run): + self.write_index([]) + output = io.StringIO() + with contextlib.redirect_stdout(output): + self.assertEqual(self.advise(), 1) + self.assertIn("no catalog entry for TEST00001 01.000.000", output.getvalue()) + run.assert_not_called() + + @patch.object(suggest_patches.subprocess, "run") + def test_invalid_timeout_is_rejected_before_relinker(self, run): + self.assertEqual(self.advise("--timeout-seconds", "0"), 2) + run.assert_not_called() + + +if __name__ == "__main__": + unittest.main()