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/host_memory.cpp') 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 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 65 insertions(+), 29 deletions(-) (limited to 'src/common/host_memory.cpp') 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 -- 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 +++++++++++++++++++++++++++++++++------------ 1 file changed, 33 insertions(+), 12 deletions(-) (limited to 'src/common/host_memory.cpp') 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() { -- 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 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src/common/host_memory.cpp') 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 -- 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 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/common/host_memory.cpp') 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); } -- 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 +++ 1 file changed, 3 insertions(+) (limited to 'src/common/host_memory.cpp') 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 { -- 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/host_memory.cpp') 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/host_memory.cpp') 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 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/host_memory.cpp') 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