host_memory: Switch to FreeRegionManager
This commit is contained in:
		| @@ -32,6 +32,7 @@ | |||||||
|  |  | ||||||
| #include "common/alignment.h" | #include "common/alignment.h" | ||||||
| #include "common/assert.h" | #include "common/assert.h" | ||||||
|  | #include "common/free_region_manager.h" | ||||||
| #include "common/host_memory.h" | #include "common/host_memory.h" | ||||||
| #include "common/logging/log.h" | #include "common/logging/log.h" | ||||||
|  |  | ||||||
| @@ -339,6 +340,11 @@ private: | |||||||
|         return false; |         return false; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     void EnableDirectMappedAddress() { | ||||||
|  |         // TODO | ||||||
|  |         UNREACHABLE(); | ||||||
|  |     } | ||||||
|  |  | ||||||
|     HANDLE process{};        ///< Current process handle |     HANDLE process{};        ///< Current process handle | ||||||
|     HANDLE backing_handle{}; ///< File based backing memory |     HANDLE backing_handle{}; ///< File based backing memory | ||||||
|  |  | ||||||
| @@ -472,7 +478,7 @@ public: | |||||||
|             } |             } | ||||||
|         } |         } | ||||||
| #else | #else | ||||||
|         virtual_base = static_cast<u8*>(ChooseVirtualBase(virtual_size)); |         virtual_base = virtual_map_base = static_cast<u8*>(ChooseVirtualBase(virtual_size)); | ||||||
|         if (virtual_base == MAP_FAILED) { |         if (virtual_base == MAP_FAILED) { | ||||||
|             LOG_CRITICAL(HW_Memory, "mmap failed: {}", strerror(errno)); |             LOG_CRITICAL(HW_Memory, "mmap failed: {}", strerror(errno)); | ||||||
|             throw std::bad_alloc{}; |             throw std::bad_alloc{}; | ||||||
| @@ -480,7 +486,7 @@ public: | |||||||
|         madvise(virtual_base, virtual_size, MADV_HUGEPAGE); |         madvise(virtual_base, virtual_size, MADV_HUGEPAGE); | ||||||
| #endif | #endif | ||||||
|  |  | ||||||
|         placeholders.add({0, virtual_size}); |         free_manager.SetAddressSpace(virtual_base, virtual_size); | ||||||
|         good = true; |         good = true; | ||||||
|     } |     } | ||||||
|  |  | ||||||
| @@ -489,10 +495,11 @@ public: | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     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) { | ||||||
|         { |         // Intersect the range with our address space. | ||||||
|             std::scoped_lock lock{placeholder_mutex}; |         AdjustMap(&virtual_offset, &length); | ||||||
|             placeholders.subtract({virtual_offset, 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, |         void* ret = mmap(virtual_base + virtual_offset, length, PROT_READ | PROT_WRITE, | ||||||
|                          MAP_SHARED | MAP_FIXED, fd, host_offset); |                          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. |         // 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. |         // We don't want to unmap, we want to reserve this memory. | ||||||
|  |  | ||||||
|         { |         // Intersect the range with our address space. | ||||||
|             std::scoped_lock lock{placeholder_mutex}; |         AdjustMap(&virtual_offset, &length); | ||||||
|             auto it = placeholders.find({virtual_offset - 1, virtual_offset + length + 1}); |  | ||||||
|  |  | ||||||
|             if (it != placeholders.end()) { |         // Merge with any adjacent placeholder mappings. | ||||||
|                 size_t prev_upper = virtual_offset + length; |         auto [merged_pointer, merged_size] = | ||||||
|                 virtual_offset = std::min(virtual_offset, it->lower()); |             free_manager.FreeBlock(virtual_base + virtual_offset, length); | ||||||
|                 length = std::max(it->upper(), prev_upper) - virtual_offset; |  | ||||||
|             } |  | ||||||
|  |  | ||||||
|             placeholders.add({virtual_offset, virtual_offset + length}); |         void* ret = mmap(merged_pointer, merged_size, PROT_NONE, | ||||||
|         } |  | ||||||
|  |  | ||||||
|         void* ret = mmap(virtual_base + virtual_offset, length, PROT_NONE, |  | ||||||
|                          MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0); |                          MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0); | ||||||
|         ASSERT_MSG(ret != MAP_FAILED, "mmap failed: {}", strerror(errno)); |         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) { | ||||||
|         int flags = 0; |         // Intersect the range with our address space. | ||||||
|  |         AdjustMap(&virtual_offset, &length); | ||||||
|  |  | ||||||
|  |         int flags = PROT_NONE; | ||||||
|         if (read) { |         if (read) { | ||||||
|             flags |= PROT_READ; |             flags |= PROT_READ; | ||||||
|         } |         } | ||||||
| @@ -533,17 +537,22 @@ public: | |||||||
|         ASSERT_MSG(ret == 0, "mprotect failed: {}", strerror(errno)); |         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 backing_size; ///< Size of the backing memory in bytes | ||||||
|     const size_t virtual_size; ///< Size of the virtual address placeholder in bytes |     const size_t virtual_size; ///< Size of the virtual address placeholder in bytes | ||||||
|  |  | ||||||
|     u8* backing_base{reinterpret_cast<u8*>(MAP_FAILED)}; |     u8* backing_base{reinterpret_cast<u8*>(MAP_FAILED)}; | ||||||
|     u8* virtual_base{reinterpret_cast<u8*>(MAP_FAILED)}; |     u8* virtual_base{reinterpret_cast<u8*>(MAP_FAILED)}; | ||||||
|  |     u8* virtual_map_base{reinterpret_cast<u8*>(MAP_FAILED)}; | ||||||
|  |  | ||||||
| private: | private: | ||||||
|     /// Release all resources in the object |     /// Release all resources in the object | ||||||
|     void Release() { |     void Release() { | ||||||
|         if (virtual_base != MAP_FAILED) { |         if (virtual_map_base != MAP_FAILED) { | ||||||
|             int ret = munmap(virtual_base, virtual_size); |             int ret = munmap(virtual_map_base, virtual_size); | ||||||
|             ASSERT_MSG(ret == 0, "munmap failed: {}", strerror(errno)); |             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; | ||||||
|  |         } | ||||||
|  |  | ||||||
|     boost::icl::interval_set<size_t> placeholders; ///< Mapped placeholders |         // If we are direct mapped, we want to make sure we are operating on a region | ||||||
|     std::mutex placeholder_mutex;                  ///< Mutex for placeholders |         // 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<size_t>(virtual_map_base); | ||||||
|  |         size_t address_space_end = address_space_start + virtual_size; | ||||||
|  |  | ||||||
|  |         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 | #else // ^^^ Linux ^^^ vvv Generic vvv | ||||||
| @@ -591,15 +619,16 @@ HostMemory::HostMemory(size_t backing_size_, size_t virtual_size_) | |||||||
|     try { |     try { | ||||||
|         // Try to allocate a fastmem arena. |         // Try to allocate a fastmem arena. | ||||||
|         // The implementation will fail with std::bad_alloc on errors. |         // The implementation will fail with std::bad_alloc on errors. | ||||||
|         impl = std::make_unique<HostMemory::Impl>(AlignUp(backing_size, PageAlignment), |         impl = | ||||||
|                                                   AlignUp(virtual_size, PageAlignment) + |             std::make_unique<HostMemory::Impl>(AlignUp(backing_size, PageAlignment), | ||||||
|                                                       3 * HugePageSize); |                                                AlignUp(virtual_size, PageAlignment) + HugePageSize); | ||||||
|         backing_base = impl->backing_base; |         backing_base = impl->backing_base; | ||||||
|         virtual_base = impl->virtual_base; |         virtual_base = impl->virtual_base; | ||||||
|  |  | ||||||
|         if (virtual_base) { |         if (virtual_base) { | ||||||
|             virtual_base += 2 * HugePageSize - 1; |             // Ensure the virtual base is aligned to the L2 block size. | ||||||
|             virtual_base -= reinterpret_cast<size_t>(virtual_base) & (HugePageSize - 1); |             virtual_base = reinterpret_cast<u8*>( | ||||||
|  |                 Common::AlignUp(reinterpret_cast<uintptr_t>(virtual_base), HugePageSize)); | ||||||
|             virtual_base_offset = virtual_base - impl->virtual_base; |             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); |     impl->Protect(virtual_offset + virtual_base_offset, length, read, write); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | void HostMemory::EnableDirectMappedAddress() { | ||||||
|  |     if (impl) { | ||||||
|  |         impl->EnableDirectMappedAddress(); | ||||||
|  |         virtual_size += reinterpret_cast<uintptr_t>(virtual_base); | ||||||
|  |     } | ||||||
|  | } | ||||||
|  |  | ||||||
| } // namespace Common | } // namespace Common | ||||||
|   | |||||||
| @@ -37,6 +37,8 @@ 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); | ||||||
|  |  | ||||||
|  |     void EnableDirectMappedAddress(); | ||||||
|  |  | ||||||
|     [[nodiscard]] u8* BackingBasePointer() noexcept { |     [[nodiscard]] u8* BackingBasePointer() noexcept { | ||||||
|         return backing_base; |         return backing_base; | ||||||
|     } |     } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user