From a609b6907a67e927d1dd0ade0135c004c9507fad Mon Sep 17 00:00:00 2001 From: bunnei Date: Tue, 23 Oct 2018 18:39:10 -0400 Subject: Kernel/Memory: Added a function to first a suitable guest address at which to allocate a region of a given size. --- src/core/hle/kernel/vm_manager.cpp | 20 ++++++++++++++++++++ src/core/hle/kernel/vm_manager.h | 8 ++++++++ 2 files changed, 28 insertions(+) diff --git a/src/core/hle/kernel/vm_manager.cpp b/src/core/hle/kernel/vm_manager.cpp index e1a34eef1..1a92c8f70 100644 --- a/src/core/hle/kernel/vm_manager.cpp +++ b/src/core/hle/kernel/vm_manager.cpp @@ -143,6 +143,26 @@ ResultVal VMManager::MapBackingMemory(VAddr target, u8* me return MakeResult(MergeAdjacent(vma_handle)); } +ResultVal VMManager::FindFreeRegion(u64 size) const { + // Find the first Free VMA. + const VAddr base = GetASLRRegionBaseAddress(); + const VMAHandle vma_handle = std::find_if(vma_map.begin(), vma_map.end(), [&](const auto& vma) { + if (vma.second.type != VMAType::Free) + return false; + + const VAddr vma_end = vma.second.base + vma.second.size; + return vma_end > base && vma_end >= base + size; + }); + + if (vma_handle == vma_map.end()) { + // TODO(Subv): Find the correct error code here. + return ResultCode(-1); + } + + const VAddr target = std::max(base, vma_handle->second.base); + return MakeResult(target); +} + ResultVal VMManager::MapMMIO(VAddr target, PAddr paddr, u64 size, MemoryState state, Memory::MemoryHookPointer mmio_handler) { diff --git a/src/core/hle/kernel/vm_manager.h b/src/core/hle/kernel/vm_manager.h index 84c890224..2447cbb8f 100644 --- a/src/core/hle/kernel/vm_manager.h +++ b/src/core/hle/kernel/vm_manager.h @@ -157,6 +157,14 @@ public: */ ResultVal MapBackingMemory(VAddr target, u8* memory, u64 size, MemoryState state); + /** + * Finds the first free address that can hold a region of the desired size. + * + * @param size Size of the desired region. + * @return The found free address. + */ + ResultVal FindFreeRegion(u64 size) const; + /** * Maps a memory-mapped IO region at a given address. * -- cgit v1.2.3