From 20011dfeb8d1fa00a862e9b31ce10ceca8015fa2 Mon Sep 17 00:00:00 2001 From: GPUCode Date: Fri, 17 Nov 2023 20:22:38 +0200 Subject: common: Add libc sigaction hook --- src/common/CMakeLists.txt | 7 +++++++ src/common/signal_chain.cpp | 42 ++++++++++++++++++++++++++++++++++++++++++ src/common/signal_chain.h | 19 +++++++++++++++++++ 3 files changed, 68 insertions(+) create mode 100644 src/common/signal_chain.cpp create mode 100644 src/common/signal_chain.h (limited to 'src/common') diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index e216eb3de..7107f4f78 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -166,6 +166,13 @@ if (WIN32) target_link_libraries(common PRIVATE ntdll) endif() +if (NOT WIN32) + target_sources(common PRIVATE + signal_chain.cpp + signal_chain.h + ) +endif() + if(ANDROID) target_sources(common PRIVATE diff --git a/src/common/signal_chain.cpp b/src/common/signal_chain.cpp new file mode 100644 index 000000000..e0c6b9d4e --- /dev/null +++ b/src/common/signal_chain.cpp @@ -0,0 +1,42 @@ +// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#include + +#include "common/assert.h" +#include "common/dynamic_library.h" +#include "common/scope_exit.h" +#include "common/signal_chain.h" + +namespace Common { + +template +T* LookupLibcSymbol(const char* name) { +#if defined(__BIONIC__) + Common::DynamicLibrary provider("libc.so"); + if (!provider.IsOpen()) { + UNREACHABLE_MSG("Failed to open libc!"); + } +#else + // For other operating environments, we assume the symbol is not overriden. + const char* base = nullptr; + Common::DynamicLibrary provider(base); +#endif + + void* sym = provider.GetSymbolAddress(name); + if (sym == nullptr) { + sym = dlsym(RTLD_DEFAULT, name); + } + if (sym == nullptr) { + UNREACHABLE_MSG("Unable to find symbol {}!", name); + } + + return reinterpret_cast(sym); +} + +int SigAction(int signum, const struct sigaction* act, struct sigaction* oldact) { + static auto libc_sigaction = LookupLibcSymbol("sigaction"); + return libc_sigaction(signum, act, oldact); +} + +} // namespace Common diff --git a/src/common/signal_chain.h b/src/common/signal_chain.h new file mode 100644 index 000000000..e3bfe6882 --- /dev/null +++ b/src/common/signal_chain.h @@ -0,0 +1,19 @@ +// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#ifndef _WIN32 + +#include + +namespace Common { + +// Android's ART overrides sigaction with its own wrapper. This is problematic for SIGSEGV +// in particular, because ARTs handler access TPIDR_EL0, so this extracts the libc version +// and calls it directly. +int SigAction(int signum, const struct sigaction* act, struct sigaction* oldact); + +} // namespace Common + +#endif -- cgit v1.2.3 From 29e7d79a869090a39127442cb9553bf46a8cd009 Mon Sep 17 00:00:00 2001 From: --author=Liam Date: Fri, 17 Nov 2023 20:25:23 +0200 Subject: common: Add free region manager * Abstraction for placeholder region tracking in host_memory --- src/common/CMakeLists.txt | 1 + src/common/free_region_manager.h | 55 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 src/common/free_region_manager.h (limited to 'src/common') diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 7107f4f78..98fd5f1e4 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -52,6 +52,7 @@ add_library(common STATIC fiber.cpp fiber.h fixed_point.h + free_region_manager.h fs/file.cpp fs/file.h fs/fs.cpp diff --git a/src/common/free_region_manager.h b/src/common/free_region_manager.h new file mode 100644 index 000000000..2e590d609 --- /dev/null +++ b/src/common/free_region_manager.h @@ -0,0 +1,55 @@ +// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include +#include + +namespace Common { + +class FreeRegionManager { +public: + explicit FreeRegionManager() = default; + ~FreeRegionManager() = default; + + void SetAddressSpace(void* start, size_t size) { + this->FreeBlock(start, size); + } + + std::pair FreeBlock(void* block_ptr, size_t size) { + std::scoped_lock lk(m_mutex); + + // Check to see if we are adjacent to any regions. + auto start_address = reinterpret_cast(block_ptr); + auto end_address = start_address + size; + auto it = m_free_regions.find({start_address - 1, end_address + 1}); + + // If we are, join with them, ensuring we stay in bounds. + if (it != m_free_regions.end()) { + start_address = std::min(start_address, it->lower()); + end_address = std::max(end_address, it->upper()); + } + + // Free the relevant region. + m_free_regions.insert({start_address, end_address}); + + // Return the adjusted pointers. + block_ptr = reinterpret_cast(start_address); + size = end_address - start_address; + return {block_ptr, size}; + } + + void AllocateBlock(void* block_ptr, size_t size) { + std::scoped_lock lk(m_mutex); + + auto address = reinterpret_cast(block_ptr); + m_free_regions.subtract({address, address + size}); + } + +private: + std::mutex m_mutex; + boost::icl::interval_set m_free_regions; +}; + +} // namespace Common -- cgit v1.2.3 From 448d4815decea4b3b29c768f3507c240932d1999 Mon Sep 17 00:00:00 2001 From: Liam Date: Fri, 17 Nov 2023 20:33:43 +0200 Subject: host_memory: ensure map base is between 36 and 39 bits --- src/common/host_memory.cpp | 60 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 58 insertions(+), 2 deletions(-) (limited to 'src/common') diff --git a/src/common/host_memory.cpp b/src/common/host_memory.cpp index ba22595e0..41ca12ab0 100644 --- a/src/common/host_memory.cpp +++ b/src/common/host_memory.cpp @@ -21,12 +21,14 @@ #include #include #include +#include #include #include "common/scope_exit.h" #endif // ^^^ Linux ^^^ #include +#include #include "common/alignment.h" #include "common/assert.h" @@ -353,6 +355,61 @@ private: #elif defined(__linux__) || defined(__FreeBSD__) // ^^^ Windows ^^^ vvv Linux vvv +#ifdef ARCHITECTURE_arm64 + +uint64_t GetRandomU64() { + uint64_t ret; + ASSERT(getrandom(&ret, sizeof(ret), 0) == 0); + return ret; +} + +void* ChooseVirtualBase(size_t virtual_size) { + constexpr uintptr_t Map39BitSize = (1ULL << 39); + constexpr uintptr_t Map36BitSize = (1ULL << 36); + + // Seed the MT with some initial strong randomness. + // + // This is not a cryptographic application, we just want something more + // random than the current time. + std::mt19937_64 rng(GetRandomU64()); + + // We want to ensure we are allocating at an address aligned to the L2 block size. + // For Qualcomm devices, we must also allocate memory above 36 bits. + const size_t lower = Map36BitSize / HugePageSize; + const size_t upper = (Map39BitSize - virtual_size) / HugePageSize; + const size_t range = upper - lower; + + // Try up to 64 times to allocate memory at random addresses in the range. + for (int i = 0; i < 64; i++) { + // Calculate a possible location. + uintptr_t hint_address = ((rng() % range) + lower) * HugePageSize; + + // Try to map. + // Note: we may be able to take advantage of MAP_FIXED_NOREPLACE here. + void* map_pointer = + mmap(reinterpret_cast(hint_address), virtual_size, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0); + + // If we successfully mapped, we're done. + if (reinterpret_cast(map_pointer) == hint_address) { + return map_pointer; + } + + // Unmap if necessary, and try again. + if (map_pointer != MAP_FAILED) { + munmap(map_pointer, virtual_size); + } + } + + return MAP_FAILED; +} +#else +void* ChooseVirtualBase(size_t virtual_size) { + return mmap(nullptr, virtual_size, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0); +} +#endif + class HostMemory::Impl { public: explicit Impl(size_t backing_size_, size_t virtual_size_) @@ -415,8 +472,7 @@ public: } } #else - virtual_base = static_cast(mmap(nullptr, virtual_size, PROT_NONE, - MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0)); + virtual_base = static_cast(ChooseVirtualBase(virtual_size)); if (virtual_base == MAP_FAILED) { LOG_CRITICAL(HW_Memory, "mmap failed: {}", strerror(errno)); throw std::bad_alloc{}; -- cgit v1.2.3 From 4766baddf3501695b6048ed78f251f4ec28ae0aa Mon Sep 17 00:00:00 2001 From: Liam Date: Fri, 17 Nov 2023 20:57:39 +0200 Subject: host_memory: Switch to FreeRegionManager --- src/common/host_memory.cpp | 94 ++++++++++++++++++++++++++++++++-------------- src/common/host_memory.h | 2 + 2 files changed, 67 insertions(+), 29 deletions(-) (limited to 'src/common') diff --git a/src/common/host_memory.cpp b/src/common/host_memory.cpp index 41ca12ab0..a66fc49e2 100644 --- a/src/common/host_memory.cpp +++ b/src/common/host_memory.cpp @@ -32,6 +32,7 @@ #include "common/alignment.h" #include "common/assert.h" +#include "common/free_region_manager.h" #include "common/host_memory.h" #include "common/logging/log.h" @@ -339,6 +340,11 @@ private: return false; } + void EnableDirectMappedAddress() { + // TODO + UNREACHABLE(); + } + HANDLE process{}; ///< Current process handle HANDLE backing_handle{}; ///< File based backing memory @@ -472,7 +478,7 @@ public: } } #else - virtual_base = static_cast(ChooseVirtualBase(virtual_size)); + virtual_base = virtual_map_base = static_cast(ChooseVirtualBase(virtual_size)); if (virtual_base == MAP_FAILED) { LOG_CRITICAL(HW_Memory, "mmap failed: {}", strerror(errno)); throw std::bad_alloc{}; @@ -480,7 +486,7 @@ public: madvise(virtual_base, virtual_size, MADV_HUGEPAGE); #endif - placeholders.add({0, virtual_size}); + free_manager.SetAddressSpace(virtual_base, virtual_size); good = true; } @@ -489,10 +495,11 @@ public: } void Map(size_t virtual_offset, size_t host_offset, size_t length) { - { - std::scoped_lock lock{placeholder_mutex}; - placeholders.subtract({virtual_offset, virtual_offset + length}); - } + // Intersect the range with our address space. + AdjustMap(&virtual_offset, &length); + + // We are removing a placeholder. + free_manager.AllocateBlock(virtual_base + virtual_offset, length); void* ret = mmap(virtual_base + virtual_offset, length, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, fd, host_offset); @@ -503,26 +510,23 @@ public: // The method name is wrong. We're still talking about the virtual range. // We don't want to unmap, we want to reserve this memory. - { - std::scoped_lock lock{placeholder_mutex}; - auto it = placeholders.find({virtual_offset - 1, virtual_offset + length + 1}); + // Intersect the range with our address space. + AdjustMap(&virtual_offset, &length); - if (it != placeholders.end()) { - size_t prev_upper = virtual_offset + length; - virtual_offset = std::min(virtual_offset, it->lower()); - length = std::max(it->upper(), prev_upper) - virtual_offset; - } + // Merge with any adjacent placeholder mappings. + auto [merged_pointer, merged_size] = + free_manager.FreeBlock(virtual_base + virtual_offset, length); - placeholders.add({virtual_offset, virtual_offset + length}); - } - - void* ret = mmap(virtual_base + virtual_offset, length, PROT_NONE, + void* ret = mmap(merged_pointer, merged_size, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0); ASSERT_MSG(ret != MAP_FAILED, "mmap failed: {}", strerror(errno)); } void Protect(size_t virtual_offset, size_t length, bool read, bool write) { - int flags = 0; + // Intersect the range with our address space. + AdjustMap(&virtual_offset, &length); + + int flags = PROT_NONE; if (read) { flags |= PROT_READ; } @@ -533,17 +537,22 @@ public: ASSERT_MSG(ret == 0, "mprotect failed: {}", strerror(errno)); } + void EnableDirectMappedAddress() { + virtual_base = nullptr; + } + const size_t backing_size; ///< Size of the backing memory in bytes const size_t virtual_size; ///< Size of the virtual address placeholder in bytes u8* backing_base{reinterpret_cast(MAP_FAILED)}; u8* virtual_base{reinterpret_cast(MAP_FAILED)}; + u8* virtual_map_base{reinterpret_cast(MAP_FAILED)}; private: /// Release all resources in the object void Release() { - if (virtual_base != MAP_FAILED) { - int ret = munmap(virtual_base, virtual_size); + if (virtual_map_base != MAP_FAILED) { + int ret = munmap(virtual_map_base, virtual_size); ASSERT_MSG(ret == 0, "munmap failed: {}", strerror(errno)); } @@ -558,10 +567,29 @@ private: } } - int fd{-1}; // memfd file descriptor, -1 is the error value of memfd_create + void AdjustMap(size_t* virtual_offset, size_t* length) { + if (virtual_base != nullptr) { + return; + } + + // If we are direct mapped, we want to make sure we are operating on a region + // that is in range of our virtual mapping. + size_t intended_start = *virtual_offset; + size_t intended_end = intended_start + *length; + size_t address_space_start = reinterpret_cast(virtual_map_base); + size_t address_space_end = address_space_start + virtual_size; - boost::icl::interval_set placeholders; ///< Mapped placeholders - std::mutex placeholder_mutex; ///< Mutex for placeholders + if (address_space_start > intended_end || intended_start > address_space_end) { + *virtual_offset = 0; + *length = 0; + } else { + *virtual_offset = std::max(intended_start, address_space_start); + *length = std::min(intended_end, address_space_end) - *virtual_offset; + } + } + + int fd{-1}; // memfd file descriptor, -1 is the error value of memfd_create + FreeRegionManager free_manager{}; }; #else // ^^^ Linux ^^^ vvv Generic vvv @@ -591,15 +619,16 @@ HostMemory::HostMemory(size_t backing_size_, size_t virtual_size_) try { // Try to allocate a fastmem arena. // The implementation will fail with std::bad_alloc on errors. - impl = std::make_unique(AlignUp(backing_size, PageAlignment), - AlignUp(virtual_size, PageAlignment) + - 3 * HugePageSize); + impl = + std::make_unique(AlignUp(backing_size, PageAlignment), + AlignUp(virtual_size, PageAlignment) + HugePageSize); backing_base = impl->backing_base; virtual_base = impl->virtual_base; if (virtual_base) { - virtual_base += 2 * HugePageSize - 1; - virtual_base -= reinterpret_cast(virtual_base) & (HugePageSize - 1); + // Ensure the virtual base is aligned to the L2 block size. + virtual_base = reinterpret_cast( + Common::AlignUp(reinterpret_cast(virtual_base), HugePageSize)); virtual_base_offset = virtual_base - impl->virtual_base; } @@ -650,4 +679,11 @@ void HostMemory::Protect(size_t virtual_offset, size_t length, bool read, bool w impl->Protect(virtual_offset + virtual_base_offset, length, read, write); } +void HostMemory::EnableDirectMappedAddress() { + if (impl) { + impl->EnableDirectMappedAddress(); + virtual_size += reinterpret_cast(virtual_base); + } +} + } // namespace Common diff --git a/src/common/host_memory.h b/src/common/host_memory.h index 447975ded..4014a1962 100644 --- a/src/common/host_memory.h +++ b/src/common/host_memory.h @@ -37,6 +37,8 @@ public: void Protect(size_t virtual_offset, size_t length, bool read, bool write); + void EnableDirectMappedAddress(); + [[nodiscard]] u8* BackingBasePointer() noexcept { return backing_base; } -- cgit v1.2.3 From 5938a9582a238d7679eb15f9812f8a85bfdb1cc3 Mon Sep 17 00:00:00 2001 From: GPUCode Date: Fri, 17 Nov 2023 21:43:15 +0200 Subject: core: Respect memory permissions in Map --- src/common/host_memory.cpp | 45 +++++++++++++++++++++++++++++++++------------ src/common/host_memory.h | 13 +++++++++++-- 2 files changed, 44 insertions(+), 14 deletions(-) (limited to 'src/common') diff --git a/src/common/host_memory.cpp b/src/common/host_memory.cpp index a66fc49e2..3e4b34de6 100644 --- a/src/common/host_memory.cpp +++ b/src/common/host_memory.cpp @@ -144,7 +144,7 @@ public: Release(); } - void Map(size_t virtual_offset, size_t host_offset, size_t length) { + void Map(size_t virtual_offset, size_t host_offset, size_t length, MemoryPermission perms) { std::unique_lock lock{placeholder_mutex}; if (!IsNiechePlaceholder(virtual_offset, length)) { Split(virtual_offset, length); @@ -163,7 +163,7 @@ public: } } - void Protect(size_t virtual_offset, size_t length, bool read, bool write) { + void Protect(size_t virtual_offset, size_t length, bool read, bool write, bool execute) { DWORD new_flags{}; if (read && write) { new_flags = PAGE_READWRITE; @@ -494,15 +494,29 @@ public: Release(); } - void Map(size_t virtual_offset, size_t host_offset, size_t length) { + void Map(size_t virtual_offset, size_t host_offset, size_t length, MemoryPermission perms) { // Intersect the range with our address space. AdjustMap(&virtual_offset, &length); // We are removing a placeholder. free_manager.AllocateBlock(virtual_base + virtual_offset, length); - void* ret = mmap(virtual_base + virtual_offset, length, PROT_READ | PROT_WRITE, - MAP_SHARED | MAP_FIXED, fd, host_offset); + // Deduce mapping protection flags. + int flags = PROT_NONE; + if (True(perms & MemoryPermission::Read)) { + flags |= PROT_READ; + } + if (True(perms & MemoryPermission::Write)) { + flags |= PROT_WRITE; + } +#ifdef ARCHITECTURE_arm64 + if (True(perms & MemoryPermission::Execute)) { + flags |= PROT_EXEC; + } +#endif + + void* ret = mmap(virtual_base + virtual_offset, length, flags, MAP_SHARED | MAP_FIXED, fd, + host_offset); ASSERT_MSG(ret != MAP_FAILED, "mmap failed: {}", strerror(errno)); } @@ -522,7 +536,7 @@ public: ASSERT_MSG(ret != MAP_FAILED, "mmap failed: {}", strerror(errno)); } - void Protect(size_t virtual_offset, size_t length, bool read, bool write) { + void Protect(size_t virtual_offset, size_t length, bool read, bool write, bool execute) { // Intersect the range with our address space. AdjustMap(&virtual_offset, &length); @@ -533,6 +547,11 @@ public: if (write) { flags |= PROT_WRITE; } +#ifdef ARCHITECTURE_arm64 + if (execute) { + flags |= PROT_EXEC; + } +#endif int ret = mprotect(virtual_base + virtual_offset, length, flags); ASSERT_MSG(ret == 0, "mprotect failed: {}", strerror(errno)); } @@ -602,11 +621,11 @@ public: throw std::bad_alloc{}; } - void Map(size_t virtual_offset, size_t host_offset, size_t length) {} + void Map(size_t virtual_offset, size_t host_offset, size_t length, MemoryPermission perm) {} void Unmap(size_t virtual_offset, size_t length) {} - void Protect(size_t virtual_offset, size_t length, bool read, bool write) {} + void Protect(size_t virtual_offset, size_t length, bool read, bool write, bool execute) {} u8* backing_base{nullptr}; u8* virtual_base{nullptr}; @@ -647,7 +666,8 @@ HostMemory::HostMemory(HostMemory&&) noexcept = default; HostMemory& HostMemory::operator=(HostMemory&&) noexcept = default; -void HostMemory::Map(size_t virtual_offset, size_t host_offset, size_t length) { +void HostMemory::Map(size_t virtual_offset, size_t host_offset, size_t length, + MemoryPermission perms) { ASSERT(virtual_offset % PageAlignment == 0); ASSERT(host_offset % PageAlignment == 0); ASSERT(length % PageAlignment == 0); @@ -656,7 +676,7 @@ void HostMemory::Map(size_t virtual_offset, size_t host_offset, size_t length) { if (length == 0 || !virtual_base || !impl) { return; } - impl->Map(virtual_offset + virtual_base_offset, host_offset, length); + impl->Map(virtual_offset + virtual_base_offset, host_offset, length, perms); } void HostMemory::Unmap(size_t virtual_offset, size_t length) { @@ -669,14 +689,15 @@ void HostMemory::Unmap(size_t virtual_offset, size_t length) { impl->Unmap(virtual_offset + virtual_base_offset, length); } -void HostMemory::Protect(size_t virtual_offset, size_t length, bool read, bool write) { +void HostMemory::Protect(size_t virtual_offset, size_t length, bool read, bool write, + bool execute) { ASSERT(virtual_offset % PageAlignment == 0); ASSERT(length % PageAlignment == 0); ASSERT(virtual_offset + length <= virtual_size); if (length == 0 || !virtual_base || !impl) { return; } - impl->Protect(virtual_offset + virtual_base_offset, length, read, write); + impl->Protect(virtual_offset + virtual_base_offset, length, read, write, execute); } void HostMemory::EnableDirectMappedAddress() { diff --git a/src/common/host_memory.h b/src/common/host_memory.h index 4014a1962..cebfacab2 100644 --- a/src/common/host_memory.h +++ b/src/common/host_memory.h @@ -4,11 +4,20 @@ #pragma once #include +#include "common/common_funcs.h" #include "common/common_types.h" #include "common/virtual_buffer.h" namespace Common { +enum class MemoryPermission : u32 { + Read = 1 << 0, + Write = 1 << 1, + ReadWrite = Read | Write, + Execute = 1 << 2, +}; +DECLARE_ENUM_FLAG_OPERATORS(MemoryPermission) + /** * A low level linear memory buffer, which supports multiple mappings * Its purpose is to rebuild a given sparse memory layout, including mirrors. @@ -31,11 +40,11 @@ public: HostMemory(HostMemory&& other) noexcept; HostMemory& operator=(HostMemory&& other) noexcept; - void Map(size_t virtual_offset, size_t host_offset, size_t length); + void Map(size_t virtual_offset, size_t host_offset, size_t length, MemoryPermission perms); void Unmap(size_t virtual_offset, size_t length); - void Protect(size_t virtual_offset, size_t length, bool read, bool write); + void Protect(size_t virtual_offset, size_t length, bool read, bool write, bool execute = false); void EnableDirectMappedAddress(); -- cgit v1.2.3 From 15331c2a6007656171275fcbb41f912c1297c358 Mon Sep 17 00:00:00 2001 From: GPUCode Date: Fri, 17 Nov 2023 22:03:42 +0200 Subject: settings: Add cpu backend setting --- src/common/settings.cpp | 5 +++++ src/common/settings.h | 10 ++++++++++ src/common/settings_enums.h | 2 ++ 3 files changed, 17 insertions(+) (limited to 'src/common') diff --git a/src/common/settings.cpp b/src/common/settings.cpp index a10131eb2..19dfe08da 100644 --- a/src/common/settings.cpp +++ b/src/common/settings.cpp @@ -41,6 +41,7 @@ SWITCHABLE(AspectRatio, true); SWITCHABLE(AstcDecodeMode, true); SWITCHABLE(AstcRecompression, true); SWITCHABLE(AudioMode, true); +SWITCHABLE(CpuBackend, true); SWITCHABLE(CpuAccuracy, true); SWITCHABLE(FullscreenMode, true); SWITCHABLE(GpuAccuracy, true); @@ -155,6 +156,10 @@ bool IsFastmemEnabled() { return true; } +bool IsNceEnabled(bool is_64bit) { + return values.cpu_backend.GetValue() == CpuBackend::Nce && is_64bit; +} + bool IsDockedMode() { return values.use_docked_mode.GetValue() == Settings::ConsoleMode::Docked; } diff --git a/src/common/settings.h b/src/common/settings.h index e75099b89..389c747cb 100644 --- a/src/common/settings.h +++ b/src/common/settings.h @@ -63,6 +63,7 @@ SWITCHABLE(AspectRatio, true); SWITCHABLE(AstcDecodeMode, true); SWITCHABLE(AstcRecompression, true); SWITCHABLE(AudioMode, true); +SWITCHABLE(CpuBackend, true); SWITCHABLE(CpuAccuracy, true); SWITCHABLE(FullscreenMode, true); SWITCHABLE(GpuAccuracy, true); @@ -179,6 +180,14 @@ struct Values { &use_speed_limit}; // Cpu + SwitchableSetting cpu_backend{ + linkage, CpuBackend::Dynarmic, CpuBackend::Dynarmic, +#ifdef ARCHITECTURE_arm64 + CpuBackend::Nce, +#else + CpuBackend::Dynarmic, +#endif + "cpu_backend", Category::Cpu}; SwitchableSetting cpu_accuracy{linkage, CpuAccuracy::Auto, CpuAccuracy::Auto, CpuAccuracy::Paranoid, "cpu_accuracy", Category::Cpu}; @@ -564,6 +573,7 @@ bool IsGPULevelExtreme(); bool IsGPULevelHigh(); bool IsFastmemEnabled(); +bool IsNceEnabled(bool is_64bit = true); bool IsDockedMode(); diff --git a/src/common/settings_enums.h b/src/common/settings_enums.h index 11429d7a8..d6351e57e 100644 --- a/src/common/settings_enums.h +++ b/src/common/settings_enums.h @@ -129,6 +129,8 @@ ENUM(ShaderBackend, Glsl, Glasm, SpirV); ENUM(GpuAccuracy, Normal, High, Extreme); +ENUM(CpuBackend, Dynarmic, Nce); + ENUM(CpuAccuracy, Auto, Accurate, Unsafe, Paranoid); ENUM(MemoryLayout, Memory_4Gb, Memory_6Gb, Memory_8Gb); -- cgit v1.2.3 From 48388376206aaa7d887b41030019035a06203867 Mon Sep 17 00:00:00 2001 From: GPUCode Date: Fri, 17 Nov 2023 22:23:48 +0200 Subject: device_memory: Enable direct mapped addresses for nce --- src/common/settings.cpp | 10 ++++++++-- src/common/settings.h | 3 ++- 2 files changed, 10 insertions(+), 3 deletions(-) (limited to 'src/common') diff --git a/src/common/settings.cpp b/src/common/settings.cpp index 19dfe08da..167e984a6 100644 --- a/src/common/settings.cpp +++ b/src/common/settings.cpp @@ -156,8 +156,14 @@ bool IsFastmemEnabled() { return true; } -bool IsNceEnabled(bool is_64bit) { - return values.cpu_backend.GetValue() == CpuBackend::Nce && is_64bit; +static bool is_nce_enabled = false; + +void SetNceEnabled(bool is_64bit) { + is_nce_enabled = values.cpu_backend.GetValue() == CpuBackend::Nce && is_64bit; +} + +bool IsNceEnabled() { + return is_nce_enabled; } bool IsDockedMode() { diff --git a/src/common/settings.h b/src/common/settings.h index 389c747cb..fea639ee3 100644 --- a/src/common/settings.h +++ b/src/common/settings.h @@ -573,7 +573,8 @@ bool IsGPULevelExtreme(); bool IsGPULevelHigh(); bool IsFastmemEnabled(); -bool IsNceEnabled(bool is_64bit = true); +void SetNceEnabled(bool is_64bit); +bool IsNceEnabled(); bool IsDockedMode(); -- cgit v1.2.3 From 9f91ba1f7357c61dd2c7c3b437ea203d467fd400 Mon Sep 17 00:00:00 2001 From: Liam Date: Fri, 17 Nov 2023 23:44:53 +0200 Subject: arm: Implement native code execution backend --- src/common/host_memory.cpp | 10 +++++----- src/common/settings.cpp | 4 ++-- src/common/settings.h | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) (limited to 'src/common') diff --git a/src/common/host_memory.cpp b/src/common/host_memory.cpp index 3e4b34de6..38d7b29f7 100644 --- a/src/common/host_memory.cpp +++ b/src/common/host_memory.cpp @@ -189,6 +189,11 @@ public: } } + void EnableDirectMappedAddress() { + // TODO + UNREACHABLE(); + } + const size_t backing_size; ///< Size of the backing memory in bytes const size_t virtual_size; ///< Size of the virtual address placeholder in bytes @@ -340,11 +345,6 @@ private: return false; } - void EnableDirectMappedAddress() { - // TODO - UNREACHABLE(); - } - HANDLE process{}; ///< Current process handle HANDLE backing_handle{}; ///< File based backing memory diff --git a/src/common/settings.cpp b/src/common/settings.cpp index 167e984a6..81a036ef0 100644 --- a/src/common/settings.cpp +++ b/src/common/settings.cpp @@ -158,8 +158,8 @@ bool IsFastmemEnabled() { static bool is_nce_enabled = false; -void SetNceEnabled(bool is_64bit) { - is_nce_enabled = values.cpu_backend.GetValue() == CpuBackend::Nce && is_64bit; +void SetNceEnabled(bool is_39bit) { + is_nce_enabled = values.cpu_backend.GetValue() == CpuBackend::Nce && is_39bit; } bool IsNceEnabled() { diff --git a/src/common/settings.h b/src/common/settings.h index fea639ee3..648e0be0d 100644 --- a/src/common/settings.h +++ b/src/common/settings.h @@ -181,7 +181,7 @@ struct Values { // Cpu SwitchableSetting cpu_backend{ - linkage, CpuBackend::Dynarmic, CpuBackend::Dynarmic, + linkage, CpuBackend::Nce, CpuBackend::Dynarmic, #ifdef ARCHITECTURE_arm64 CpuBackend::Nce, #else -- cgit v1.2.3 From 8fab363237083a8130a7b2a023cd9c5dd83f8f4f Mon Sep 17 00:00:00 2001 From: GPUCode Date: Sat, 18 Nov 2023 16:10:39 +0200 Subject: android: Add cpu bakend gui toggle --- src/common/host_memory.cpp | 6 +++--- src/common/settings.cpp | 8 +++++++- src/common/signal_chain.cpp | 2 +- 3 files changed, 11 insertions(+), 5 deletions(-) (limited to 'src/common') diff --git a/src/common/host_memory.cpp b/src/common/host_memory.cpp index 38d7b29f7..8a869e558 100644 --- a/src/common/host_memory.cpp +++ b/src/common/host_memory.cpp @@ -363,13 +363,13 @@ private: #ifdef ARCHITECTURE_arm64 -uint64_t GetRandomU64() { +static uint64_t GetRandomU64() { uint64_t ret; ASSERT(getrandom(&ret, sizeof(ret), 0) == 0); return ret; } -void* ChooseVirtualBase(size_t virtual_size) { +static void* ChooseVirtualBase(size_t virtual_size) { constexpr uintptr_t Map39BitSize = (1ULL << 39); constexpr uintptr_t Map36BitSize = (1ULL << 36); @@ -410,7 +410,7 @@ void* ChooseVirtualBase(size_t virtual_size) { return MAP_FAILED; } #else -void* ChooseVirtualBase(size_t virtual_size) { +static void* ChooseVirtualBase(size_t virtual_size) { return mmap(nullptr, virtual_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0); } diff --git a/src/common/settings.cpp b/src/common/settings.cpp index 81a036ef0..90e7475d7 100644 --- a/src/common/settings.cpp +++ b/src/common/settings.cpp @@ -159,7 +159,13 @@ bool IsFastmemEnabled() { static bool is_nce_enabled = false; void SetNceEnabled(bool is_39bit) { - is_nce_enabled = values.cpu_backend.GetValue() == CpuBackend::Nce && is_39bit; + const bool is_nce_selected = values.cpu_backend.GetValue() == CpuBackend::Nce; + is_nce_enabled = is_nce_selected && is_39bit; + if (is_nce_selected && !is_nce_enabled) { + LOG_WARNING( + Common, + "Program does not utilize 39-bit address space, unable to natively execute code"); + } } bool IsNceEnabled() { diff --git a/src/common/signal_chain.cpp b/src/common/signal_chain.cpp index e0c6b9d4e..2e4fecc48 100644 --- a/src/common/signal_chain.cpp +++ b/src/common/signal_chain.cpp @@ -18,7 +18,7 @@ T* LookupLibcSymbol(const char* name) { UNREACHABLE_MSG("Failed to open libc!"); } #else - // For other operating environments, we assume the symbol is not overriden. + // For other operating environments, we assume the symbol is not overridden. const char* base = nullptr; Common::DynamicLibrary provider(base); #endif -- cgit v1.2.3 From 6de2edcca1624982e99a72741d4fa289dc9d7551 Mon Sep 17 00:00:00 2001 From: GPUCode Date: Sun, 19 Nov 2023 11:21:53 +0200 Subject: Address some review comments --- src/common/host_memory.cpp | 3 +++ src/common/settings.h | 2 +- src/common/signal_chain.h | 4 ++-- 3 files changed, 6 insertions(+), 3 deletions(-) (limited to 'src/common') diff --git a/src/common/host_memory.cpp b/src/common/host_memory.cpp index 8a869e558..f14077750 100644 --- a/src/common/host_memory.cpp +++ b/src/common/host_memory.cpp @@ -409,11 +409,14 @@ static void* ChooseVirtualBase(size_t virtual_size) { return MAP_FAILED; } + #else + static void* ChooseVirtualBase(size_t virtual_size) { return mmap(nullptr, virtual_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0); } + #endif class HostMemory::Impl { diff --git a/src/common/settings.h b/src/common/settings.h index 648e0be0d..fea639ee3 100644 --- a/src/common/settings.h +++ b/src/common/settings.h @@ -181,7 +181,7 @@ struct Values { // Cpu SwitchableSetting cpu_backend{ - linkage, CpuBackend::Nce, CpuBackend::Dynarmic, + linkage, CpuBackend::Dynarmic, CpuBackend::Dynarmic, #ifdef ARCHITECTURE_arm64 CpuBackend::Nce, #else diff --git a/src/common/signal_chain.h b/src/common/signal_chain.h index e3bfe6882..8d06a1bd1 100644 --- a/src/common/signal_chain.h +++ b/src/common/signal_chain.h @@ -10,8 +10,8 @@ namespace Common { // Android's ART overrides sigaction with its own wrapper. This is problematic for SIGSEGV -// in particular, because ARTs handler access TPIDR_EL0, so this extracts the libc version -// and calls it directly. +// in particular, because ART's handler accesses tpidr_el0, which conflicts with NCE. +// This extracts the libc symbol and calls it directly. int SigAction(int signum, const struct sigaction* act, struct sigaction* oldact); } // namespace Common -- cgit v1.2.3 From c37b5f431fcbffac173fb70ef0a0d7fb6cde7f2d Mon Sep 17 00:00:00 2001 From: GPUCode Date: Mon, 20 Nov 2023 15:33:44 +0200 Subject: common: Enforce fastmem for nce usage --- src/common/settings.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/common') diff --git a/src/common/settings.cpp b/src/common/settings.cpp index 90e7475d7..16c2feeab 100644 --- a/src/common/settings.cpp +++ b/src/common/settings.cpp @@ -160,7 +160,7 @@ static bool is_nce_enabled = false; void SetNceEnabled(bool is_39bit) { const bool is_nce_selected = values.cpu_backend.GetValue() == CpuBackend::Nce; - is_nce_enabled = is_nce_selected && is_39bit; + is_nce_enabled = IsFastmemEnabled() && is_nce_selected && is_39bit; if (is_nce_selected && !is_nce_enabled) { LOG_WARNING( Common, -- cgit v1.2.3 From 5a9ffa81a6452f9f0af6f5b288f302cbaf0475b2 Mon Sep 17 00:00:00 2001 From: GPUCode Date: Thu, 23 Nov 2023 16:55:56 +0200 Subject: host_memory: Simplify randomness generation --- src/common/host_memory.cpp | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) (limited to 'src/common') diff --git a/src/common/host_memory.cpp b/src/common/host_memory.cpp index f14077750..4cbc22b85 100644 --- a/src/common/host_memory.cpp +++ b/src/common/host_memory.cpp @@ -363,21 +363,12 @@ private: #ifdef ARCHITECTURE_arm64 -static uint64_t GetRandomU64() { - uint64_t ret; - ASSERT(getrandom(&ret, sizeof(ret), 0) == 0); - return ret; -} - static void* ChooseVirtualBase(size_t virtual_size) { constexpr uintptr_t Map39BitSize = (1ULL << 39); constexpr uintptr_t Map36BitSize = (1ULL << 36); - // Seed the MT with some initial strong randomness. - // - // This is not a cryptographic application, we just want something more - // random than the current time. - std::mt19937_64 rng(GetRandomU64()); + // This is not a cryptographic application, we just want something random. + std::mt19937_64 rng; // We want to ensure we are allocating at an address aligned to the L2 block size. // For Qualcomm devices, we must also allocate memory above 36 bits. -- cgit v1.2.3 From 15f35b8657ef863ea3df0d141521b72c61f0069f Mon Sep 17 00:00:00 2001 From: Liam Date: Sun, 26 Nov 2023 19:50:10 -0500 Subject: general: fix mac compile --- src/common/host_memory.cpp | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/common') diff --git a/src/common/host_memory.cpp b/src/common/host_memory.cpp index 4cbc22b85..413dd3e98 100644 --- a/src/common/host_memory.cpp +++ b/src/common/host_memory.cpp @@ -621,6 +621,8 @@ public: void Protect(size_t virtual_offset, size_t length, bool read, bool write, bool execute) {} + void EnableDirectMappedAddress() {} + u8* backing_base{nullptr}; u8* virtual_base{nullptr}; }; -- cgit v1.2.3 From a76a8fb5fecb4731a9a9231f3e06d5743c34e090 Mon Sep 17 00:00:00 2001 From: amazingfate Date: Sun, 26 Nov 2023 20:25:18 -0500 Subject: qt: add cpu_backend configuration --- src/common/CMakeLists.txt | 2 +- src/common/settings.h | 2 +- src/common/wall_clock.cpp | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) (limited to 'src/common') diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 98fd5f1e4..d38d5c6d1 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -199,7 +199,7 @@ if(ARCHITECTURE_x86_64) target_link_libraries(common PRIVATE xbyak::xbyak) endif() -if (ARCHITECTURE_arm64 AND (ANDROID OR LINUX)) +if (HAS_NCE) target_sources(common PRIVATE arm64/native_clock.cpp diff --git a/src/common/settings.h b/src/common/settings.h index fea639ee3..508615011 100644 --- a/src/common/settings.h +++ b/src/common/settings.h @@ -182,7 +182,7 @@ struct Values { // Cpu SwitchableSetting cpu_backend{ linkage, CpuBackend::Dynarmic, CpuBackend::Dynarmic, -#ifdef ARCHITECTURE_arm64 +#ifdef HAS_NCE CpuBackend::Nce, #else CpuBackend::Dynarmic, diff --git a/src/common/wall_clock.cpp b/src/common/wall_clock.cpp index caca9a123..012fdc1e0 100644 --- a/src/common/wall_clock.cpp +++ b/src/common/wall_clock.cpp @@ -10,7 +10,7 @@ #include "common/x64/rdtsc.h" #endif -#if defined(ARCHITECTURE_arm64) && defined(__linux__) +#ifdef HAS_NCE #include "common/arm64/native_clock.h" #endif @@ -68,7 +68,7 @@ std::unique_ptr CreateOptimalClock() { // - Is not more precise than 1 GHz (1ns resolution) return std::make_unique(); } -#elif defined(ARCHITECTURE_arm64) && defined(__linux__) +#elif defined(HAS_NCE) return std::make_unique(); #else return std::make_unique(); -- cgit v1.2.3 From 340548aba798ca2d0accf3bc3f0f787d15484946 Mon Sep 17 00:00:00 2001 From: GPUCode Date: Wed, 29 Nov 2023 01:33:47 +0200 Subject: cmake: Move HAS_NCE to root cmake * So we can use it in common --- src/common/host_memory.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/common') diff --git a/src/common/host_memory.cpp b/src/common/host_memory.cpp index 413dd3e98..3a9ea6eb4 100644 --- a/src/common/host_memory.cpp +++ b/src/common/host_memory.cpp @@ -541,7 +541,7 @@ public: if (write) { flags |= PROT_WRITE; } -#ifdef ARCHITECTURE_arm64 +#ifdef HAS_NCE if (execute) { flags |= PROT_EXEC; } -- cgit v1.2.3