From 13a8fde3ad2a4a37cf1bb8dcb367b4c8fc8b4d9b Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Sun, 7 Jul 2019 09:42:54 -0700 Subject: Implement MapPhysicalMemory/UnmapPhysicalMemory This implements svcMapPhysicalMemory/svcUnmapPhysicalMemory for Yuzu, which can be used to map memory at a desired address by games since 3.0.0. It also properly parses SystemResourceSize from NPDM, and makes information available via svcGetInfo. This is needed for games like Super Smash Bros. and Diablo 3 -- this PR's implementation does not run into the "ASCII reads" issue mentioned in the comments of #2626, which was caused by the following bugs in Yuzu's memory management that this PR also addresses: * Yuzu's memory coalescing does not properly merge blocks. This results in a polluted address space/svcQueryMemory results that would be impossible to replicate on hardware, which can lead to game code making the wrong assumptions about memory layout. * This implements better merging for AllocatedMemoryBlocks. * Yuzu's implementation of svcMirrorMemory unprotected the entire virtual memory range containing the range being mirrored. This could lead to games attempting to map data at that unprotected range/attempting to access that range after yuzu improperly unmapped it. * This PR fixes it by simply calling ReprotectRange instead of Reprotect. --- src/core/hle/kernel/vm_manager.cpp | 320 ++++++++++++++++++++++++++++++++++++- 1 file changed, 312 insertions(+), 8 deletions(-) (limited to 'src/core/hle/kernel/vm_manager.cpp') diff --git a/src/core/hle/kernel/vm_manager.cpp b/src/core/hle/kernel/vm_manager.cpp index 501544090..9385a8697 100644 --- a/src/core/hle/kernel/vm_manager.cpp +++ b/src/core/hle/kernel/vm_manager.cpp @@ -12,6 +12,8 @@ #include "core/core.h" #include "core/file_sys/program_metadata.h" #include "core/hle/kernel/errors.h" +#include "core/hle/kernel/process.h" +#include "core/hle/kernel/resource_limit.h" #include "core/hle/kernel/vm_manager.h" #include "core/memory.h" #include "core/memory_setup.h" @@ -49,9 +51,8 @@ bool VirtualMemoryArea::CanBeMergedWith(const VirtualMemoryArea& next) const { type != next.type) { return false; } - if (type == VMAType::AllocatedMemoryBlock && - (backing_block != next.backing_block || offset + size != next.offset)) { - return false; + if (type == VMAType::AllocatedMemoryBlock) { + return true; } if (type == VMAType::BackingMemory && backing_memory + size != next.backing_memory) { return false; @@ -100,7 +101,7 @@ bool VMManager::IsValidHandle(VMAHandle handle) const { ResultVal VMManager::MapMemoryBlock(VAddr target, std::shared_ptr> block, std::size_t offset, u64 size, - MemoryState state) { + MemoryState state, VMAPermission perm) { ASSERT(block != nullptr); ASSERT(offset + size <= block->size()); @@ -119,7 +120,7 @@ ResultVal VMManager::MapMemoryBlock(VAddr target, VMAPermission::ReadWriteExecute); final_vma.type = VMAType::AllocatedMemoryBlock; - final_vma.permissions = VMAPermission::ReadWrite; + final_vma.permissions = perm; final_vma.state = state; final_vma.backing_block = std::move(block); final_vma.offset = offset; @@ -308,6 +309,258 @@ ResultVal VMManager::SetHeapSize(u64 size) { return MakeResult(heap_region_base); } +ResultCode VMManager::MapPhysicalMemory(VAddr target, u64 size) { + const auto last_addr = target + size - 1; + VAddr cur_addr = target; + std::size_t mapped_size = 0; + + ResultCode result = RESULT_SUCCESS; + + // Check whether we've already mapped the desired memory. + { + auto vma = FindVMA(target); + ASSERT_MSG(vma != vma_map.end(), "MapPhysicalMemory vma != end"); + + while (true) { + const auto vma_start = vma->second.base; + const auto vma_size = vma->second.size; + const auto state = vma->second.state; + + // Handle last block. + if (last_addr <= (vma_start + vma_size - 1)) { + if (state != MemoryState::Unmapped) { + mapped_size += last_addr - cur_addr + 1; + } + break; + } + + if (state != MemoryState::Unmapped) { + mapped_size += vma_start + vma_size - cur_addr; + } + cur_addr = vma_start + vma_size; + vma++; + ASSERT_MSG(vma != vma_map.end(), "MapPhysicalMemory vma != end"); + } + + // If we already have the desired amount mapped, we're done. + if (mapped_size == size) { + return RESULT_SUCCESS; + } + } + + // Check that we can map the memory we want. + const auto res_limit = Core::CurrentProcess()->GetResourceLimit(); + const u64 physmem_remaining = res_limit->GetMaxResourceValue(ResourceType::PhysicalMemory) - + res_limit->GetCurrentResourceValue(ResourceType::PhysicalMemory); + if (physmem_remaining < (size - mapped_size)) { + return ERR_RESOURCE_LIMIT_EXCEEDED; + } + + // Keep track of the memory regions we unmap. + std::vector> mapped_regions; + + // Iterate, trying to map memory. + // Map initially with VMAPermission::None. + { + cur_addr = target; + + auto vma = FindVMA(target); + ASSERT_MSG(vma != vma_map.end(), "MapPhysicalMemory vma != end"); + + while (true) { + const auto vma_start = vma->second.base; + const auto vma_size = vma->second.size; + const auto state = vma->second.state; + + // Handle last block. + if (last_addr <= (vma_start + vma_size - 1)) { + if (state == MemoryState::Unmapped) { + const auto map_res = MapMemoryBlock( + cur_addr, std::make_shared>(last_addr - cur_addr + 1, 0), 0, + last_addr - cur_addr + 1, MemoryState::Heap, VMAPermission::None); + result = map_res.Code(); + if (result.IsSuccess()) { + mapped_regions.push_back( + std::make_pair(cur_addr, last_addr - cur_addr + 1)); + } + } + break; + } + + if (state == MemoryState::Unmapped) { + const auto map_res = MapMemoryBlock( + cur_addr, std::make_shared>(vma_start + vma_size - cur_addr, 0), + 0, vma_start + vma_size - cur_addr, MemoryState::Heap, VMAPermission::None); + result = map_res.Code(); + if (result.IsSuccess()) { + mapped_regions.push_back( + std::make_pair(cur_addr, vma_start + vma_size - cur_addr)); + } else { + break; + } + } + cur_addr = vma_start + vma_size; + vma = FindVMA(cur_addr); + ASSERT_MSG(vma != vma_map.end(), "MapPhysicalMemory vma != end"); + } + } + + // If we failed, unmap memory. + if (result.IsError()) { + for (const auto& it : mapped_regions) { + const auto unmap_res = UnmapRange(it.first, it.second); + ASSERT_MSG(unmap_res.IsSuccess(), "MapPhysicalMemory un-map on error"); + } + + return result; + } + + // We didn't fail, so reprotect all the memory to ReadWrite. + { + cur_addr = target; + + auto vma = FindVMA(target); + ASSERT_MSG(vma != vma_map.end(), "MapPhysicalMemory vma != end"); + + while (true) { + const auto vma_start = vma->second.base; + const auto vma_size = vma->second.size; + const auto state = vma->second.state; + const auto perm = vma->second.permissions; + + // Handle last block. + if (last_addr <= (vma_start + vma_size - 1)) { + if (state == MemoryState::Heap && perm == VMAPermission::None) { + ASSERT_MSG( + ReprotectRange(cur_addr, last_addr - cur_addr + 1, VMAPermission::ReadWrite) + .IsSuccess(), + "MapPhysicalMemory reprotect"); + } + break; + } + + if (state == MemoryState::Heap && perm == VMAPermission::None) { + ASSERT_MSG(ReprotectRange(cur_addr, vma_start + vma_size - cur_addr, + VMAPermission::ReadWrite) + .IsSuccess(), + "MapPhysicalMemory reprotect"); + } + cur_addr = vma_start + vma_size; + vma = FindVMA(cur_addr); + ASSERT_MSG(vma != vma_map.end(), "MapPhysicalMemory vma != end"); + } + } + + // Update amount of mapped physical memory. + physical_memory_mapped += size - mapped_size; + + return RESULT_SUCCESS; +} + +ResultCode VMManager::UnmapPhysicalMemory(VAddr target, u64 size) { + auto last_addr = target + size - 1; + VAddr cur_addr = target; + std::size_t mapped_size = 0; + + ResultCode result = RESULT_SUCCESS; + + // Check how much of the memory is currently mapped. + { + auto vma = FindVMA(target); + ASSERT_MSG(vma != vma_map.end(), "UnmapPhysicalMemory vma != end"); + + while (true) { + const auto vma_start = vma->second.base; + const auto vma_size = vma->second.size; + const auto state = vma->second.state; + const auto attr = vma->second.attribute; + + // Memory within region must be free or mapped heap. + if (!((state == MemoryState::Heap && attr == MemoryAttribute::None) || + (state == MemoryState::Unmapped))) { + return ERR_INVALID_ADDRESS_STATE; + } + + // If this is the last block and it's mapped, update mapped size. + if (last_addr <= (vma_start + vma_size - 1)) { + if (state == MemoryState::Heap) { + mapped_size += last_addr - cur_addr + 1; + } + break; + } + + if (state == MemoryState::Heap) { + mapped_size += vma_start + vma_size - cur_addr; + } + cur_addr = vma_start + vma_size; + vma++; + ASSERT_MSG(vma != vma_map.end(), "UnmapPhysicalMemory vma != end"); + } + + // If memory is already unmapped, we're done. + if (mapped_size == 0) { + return RESULT_SUCCESS; + } + } + + // Keep track of the memory regions we unmap. + std::vector> unmapped_regions; + + // Try to unmap regions. + { + cur_addr = target; + + auto vma = FindVMA(target); + ASSERT_MSG(vma != vma_map.end(), "UnmapPhysicalMemory vma != end"); + + while (true) { + const auto vma_start = vma->second.base; + const auto vma_size = vma->second.size; + const auto state = vma->second.state; + const auto perm = vma->second.permissions; + + // Handle last block. + if (last_addr <= (vma_start + vma_size - 1)) { + if (state == MemoryState::Heap) { + result = UnmapRange(cur_addr, last_addr - cur_addr + 1); + if (result.IsSuccess()) { + unmapped_regions.push_back( + std::make_pair(cur_addr, last_addr - cur_addr + 1)); + } + } + break; + } + + if (state == MemoryState::Heap) { + result = UnmapRange(cur_addr, vma_start + vma_size - cur_addr); + if (result.IsSuccess()) { + unmapped_regions.push_back( + std::make_pair(cur_addr, vma_start + vma_size - cur_addr)); + } else { + break; + } + } + + cur_addr = vma_start + vma_size; + vma = FindVMA(cur_addr); + ASSERT_MSG(vma != vma_map.end(), "UnmapPhysicalMemory vma != end"); + } + } + + // If we failed, re-map regions. + // TODO: Preserve memory contents? + if (result.IsError()) { + for (const auto& it : unmapped_regions) { + const auto remap_res = + MapMemoryBlock(it.first, std::make_shared>(it.second, 0), 0, + it.second, MemoryState::Heap, VMAPermission::None); + ASSERT_MSG(remap_res.Succeeded(), "UnmapPhysicalMemory re-map on error"); + } + } + + return RESULT_SUCCESS; +} + ResultCode VMManager::MapCodeMemory(VAddr dst_address, VAddr src_address, u64 size) { constexpr auto ignore_attribute = MemoryAttribute::LockedForIPC | MemoryAttribute::DeviceMapped; const auto src_check_result = CheckRangeState( @@ -455,7 +708,7 @@ ResultCode VMManager::MirrorMemory(VAddr dst_addr, VAddr src_addr, u64 size, Mem // Protect mirror with permissions from old region Reprotect(new_vma, vma->second.permissions); // Remove permissions from old region - Reprotect(vma, VMAPermission::None); + ReprotectRange(src_addr, size, VMAPermission::None); return RESULT_SUCCESS; } @@ -588,14 +841,14 @@ VMManager::VMAIter VMManager::SplitVMA(VMAIter vma_handle, u64 offset_in_vma) { VMManager::VMAIter VMManager::MergeAdjacent(VMAIter iter) { const VMAIter next_vma = std::next(iter); if (next_vma != vma_map.end() && iter->second.CanBeMergedWith(next_vma->second)) { - iter->second.size += next_vma->second.size; + MergeAdjacentVMA(iter->second, next_vma->second); vma_map.erase(next_vma); } if (iter != vma_map.begin()) { VMAIter prev_vma = std::prev(iter); if (prev_vma->second.CanBeMergedWith(iter->second)) { - prev_vma->second.size += iter->second.size; + MergeAdjacentVMA(prev_vma->second, iter->second); vma_map.erase(iter); iter = prev_vma; } @@ -604,6 +857,57 @@ VMManager::VMAIter VMManager::MergeAdjacent(VMAIter iter) { return iter; } +void VMManager::MergeAdjacentVMA(VirtualMemoryArea& left, const VirtualMemoryArea& right) { + ASSERT(left.CanBeMergedWith(right)); + + // Always merge allocated memory blocks, even when they don't share the same backing block. + if (left.type == VMAType::AllocatedMemoryBlock && + (left.backing_block != right.backing_block || left.offset + left.size != right.offset)) { + // Check if we can save work. + if (left.offset == 0 && left.size == left.backing_block->size()) { + // Fast case: left is an entire backing block. + left.backing_block->insert(left.backing_block->end(), + right.backing_block->begin() + right.offset, + right.backing_block->begin() + right.offset + right.size); + } else { + // Slow case: make a new memory block for left and right. + auto new_memory = std::make_shared>(); + new_memory->insert(new_memory->end(), left.backing_block->begin() + left.offset, + left.backing_block->begin() + left.offset + left.size); + new_memory->insert(new_memory->end(), right.backing_block->begin() + right.offset, + right.backing_block->begin() + right.offset + right.size); + left.backing_block = new_memory; + left.offset = 0; + } + + // Page table update is needed, because backing memory changed. + left.size += right.size; + UpdatePageTableForVMA(left); + + // Update mappings for unicorn. + system.ArmInterface(0).UnmapMemory(left.base, left.size); + system.ArmInterface(1).UnmapMemory(left.base, left.size); + system.ArmInterface(2).UnmapMemory(left.base, left.size); + system.ArmInterface(3).UnmapMemory(left.base, left.size); + + system.ArmInterface(0).MapBackingMemory(left.base, left.size, + left.backing_block->data() + left.offset, + VMAPermission::ReadWriteExecute); + system.ArmInterface(1).MapBackingMemory(left.base, left.size, + left.backing_block->data() + left.offset, + VMAPermission::ReadWriteExecute); + system.ArmInterface(2).MapBackingMemory(left.base, left.size, + left.backing_block->data() + left.offset, + VMAPermission::ReadWriteExecute); + system.ArmInterface(3).MapBackingMemory(left.base, left.size, + left.backing_block->data() + left.offset, + VMAPermission::ReadWriteExecute); + } else { + // Just update the size. + left.size += right.size; + } +} + void VMManager::UpdatePageTableForVMA(const VirtualMemoryArea& vma) { switch (vma.type) { case VMAType::Free: -- cgit v1.2.3 From 1689784c198f6a7f3c389d4cd5edeccc74c1d9f3 Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Sun, 7 Jul 2019 11:48:11 -0700 Subject: address review commentary --- src/core/hle/kernel/vm_manager.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'src/core/hle/kernel/vm_manager.cpp') diff --git a/src/core/hle/kernel/vm_manager.cpp b/src/core/hle/kernel/vm_manager.cpp index 9385a8697..bda325e87 100644 --- a/src/core/hle/kernel/vm_manager.cpp +++ b/src/core/hle/kernel/vm_manager.cpp @@ -349,7 +349,7 @@ ResultCode VMManager::MapPhysicalMemory(VAddr target, u64 size) { } // Check that we can map the memory we want. - const auto res_limit = Core::CurrentProcess()->GetResourceLimit(); + const auto res_limit = system.CurrentProcess()->GetResourceLimit(); const u64 physmem_remaining = res_limit->GetMaxResourceValue(ResourceType::PhysicalMemory) - res_limit->GetCurrentResourceValue(ResourceType::PhysicalMemory); if (physmem_remaining < (size - mapped_size)) { @@ -558,6 +558,9 @@ ResultCode VMManager::UnmapPhysicalMemory(VAddr target, u64 size) { } } + // Update mapped amount + physical_memory_mapped -= mapped_size; + return RESULT_SUCCESS; } -- cgit v1.2.3 From ce64a9fab9e6f015a4d2b332abcb7043914549d4 Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Sun, 7 Jul 2019 12:55:30 -0700 Subject: physmem: add helpers, cleanup logic. --- src/core/hle/kernel/vm_manager.cpp | 325 ++++++++++++++++++------------------- 1 file changed, 154 insertions(+), 171 deletions(-) (limited to 'src/core/hle/kernel/vm_manager.cpp') diff --git a/src/core/hle/kernel/vm_manager.cpp b/src/core/hle/kernel/vm_manager.cpp index bda325e87..775d170bf 100644 --- a/src/core/hle/kernel/vm_manager.cpp +++ b/src/core/hle/kernel/vm_manager.cpp @@ -310,42 +310,22 @@ ResultVal VMManager::SetHeapSize(u64 size) { } ResultCode VMManager::MapPhysicalMemory(VAddr target, u64 size) { - const auto last_addr = target + size - 1; + const auto end_addr = target + size; + const auto last_addr = end_addr - 1; VAddr cur_addr = target; - std::size_t mapped_size = 0; ResultCode result = RESULT_SUCCESS; - // Check whether we've already mapped the desired memory. - { - auto vma = FindVMA(target); - ASSERT_MSG(vma != vma_map.end(), "MapPhysicalMemory vma != end"); - - while (true) { - const auto vma_start = vma->second.base; - const auto vma_size = vma->second.size; - const auto state = vma->second.state; - - // Handle last block. - if (last_addr <= (vma_start + vma_size - 1)) { - if (state != MemoryState::Unmapped) { - mapped_size += last_addr - cur_addr + 1; - } - break; - } - - if (state != MemoryState::Unmapped) { - mapped_size += vma_start + vma_size - cur_addr; - } - cur_addr = vma_start + vma_size; - vma++; - ASSERT_MSG(vma != vma_map.end(), "MapPhysicalMemory vma != end"); - } + // Check how much memory we've already mapped. + const auto mapped_size_result = SizeOfAllocatedVMAsInRange(target, size); + if (mapped_size_result.Failed()) { + return mapped_size_result.Code(); + } - // If we already have the desired amount mapped, we're done. - if (mapped_size == size) { - return RESULT_SUCCESS; - } + // If we've already mapped the desired amount, return early. + const std::size_t mapped_size = *mapped_size_result; + if (mapped_size == size) { + return RESULT_SUCCESS; } // Check that we can map the memory we want. @@ -360,97 +340,54 @@ ResultCode VMManager::MapPhysicalMemory(VAddr target, u64 size) { std::vector> mapped_regions; // Iterate, trying to map memory. - // Map initially with VMAPermission::None. { cur_addr = target; - auto vma = FindVMA(target); - ASSERT_MSG(vma != vma_map.end(), "MapPhysicalMemory vma != end"); + auto iter = FindVMA(target); + ASSERT_MSG(iter != vma_map.end(), "MapPhysicalMemory iter != end"); while (true) { - const auto vma_start = vma->second.base; - const auto vma_size = vma->second.size; - const auto state = vma->second.state; - - // Handle last block. - if (last_addr <= (vma_start + vma_size - 1)) { - if (state == MemoryState::Unmapped) { - const auto map_res = MapMemoryBlock( - cur_addr, std::make_shared>(last_addr - cur_addr + 1, 0), 0, - last_addr - cur_addr + 1, MemoryState::Heap, VMAPermission::None); - result = map_res.Code(); - if (result.IsSuccess()) { - mapped_regions.push_back( - std::make_pair(cur_addr, last_addr - cur_addr + 1)); - } - } - break; - } - - if (state == MemoryState::Unmapped) { - const auto map_res = MapMemoryBlock( - cur_addr, std::make_shared>(vma_start + vma_size - cur_addr, 0), - 0, vma_start + vma_size - cur_addr, MemoryState::Heap, VMAPermission::None); + const auto& vma = iter->second; + const auto vma_start = vma.base; + const auto vma_end = vma_start + vma.size; + const auto vma_last = vma_end - 1; + + // Map the memory block + const auto map_size = std::min(end_addr - cur_addr, vma_end - cur_addr); + if (vma.state == MemoryState::Unmapped) { + const auto map_res = + MapMemoryBlock(cur_addr, std::make_shared>(map_size, 0), 0, + map_size, MemoryState::Heap, VMAPermission::ReadWrite); result = map_res.Code(); - if (result.IsSuccess()) { - mapped_regions.push_back( - std::make_pair(cur_addr, vma_start + vma_size - cur_addr)); - } else { + if (result.IsError()) { break; } + + mapped_regions.emplace_back(cur_addr, map_size); } - cur_addr = vma_start + vma_size; - vma = FindVMA(cur_addr); - ASSERT_MSG(vma != vma_map.end(), "MapPhysicalMemory vma != end"); + + // Break once we hit the end of the range. + if (last_addr <= vma_last) { + break; + } + + // Advance to the next block. + cur_addr = vma_end; + iter = FindVMA(cur_addr); + ASSERT_MSG(iter != vma_map.end(), "MapPhysicalMemory iter != end"); } } // If we failed, unmap memory. if (result.IsError()) { - for (const auto& it : mapped_regions) { - const auto unmap_res = UnmapRange(it.first, it.second); - ASSERT_MSG(unmap_res.IsSuccess(), "MapPhysicalMemory un-map on error"); + for (const auto [unmap_address, unmap_size] : mapped_regions) { + ASSERT_MSG(UnmapRange(unmap_address, unmap_size).IsSuccess(), + "MapPhysicalMemory un-map on error"); } return result; } - // We didn't fail, so reprotect all the memory to ReadWrite. - { - cur_addr = target; - - auto vma = FindVMA(target); - ASSERT_MSG(vma != vma_map.end(), "MapPhysicalMemory vma != end"); - - while (true) { - const auto vma_start = vma->second.base; - const auto vma_size = vma->second.size; - const auto state = vma->second.state; - const auto perm = vma->second.permissions; - - // Handle last block. - if (last_addr <= (vma_start + vma_size - 1)) { - if (state == MemoryState::Heap && perm == VMAPermission::None) { - ASSERT_MSG( - ReprotectRange(cur_addr, last_addr - cur_addr + 1, VMAPermission::ReadWrite) - .IsSuccess(), - "MapPhysicalMemory reprotect"); - } - break; - } - - if (state == MemoryState::Heap && perm == VMAPermission::None) { - ASSERT_MSG(ReprotectRange(cur_addr, vma_start + vma_size - cur_addr, - VMAPermission::ReadWrite) - .IsSuccess(), - "MapPhysicalMemory reprotect"); - } - cur_addr = vma_start + vma_size; - vma = FindVMA(cur_addr); - ASSERT_MSG(vma != vma_map.end(), "MapPhysicalMemory vma != end"); - } - } - // Update amount of mapped physical memory. physical_memory_mapped += size - mapped_size; @@ -458,49 +395,22 @@ ResultCode VMManager::MapPhysicalMemory(VAddr target, u64 size) { } ResultCode VMManager::UnmapPhysicalMemory(VAddr target, u64 size) { - auto last_addr = target + size - 1; + const auto end_addr = target + size; + const auto last_addr = end_addr - 1; VAddr cur_addr = target; - std::size_t mapped_size = 0; ResultCode result = RESULT_SUCCESS; - // Check how much of the memory is currently mapped. - { - auto vma = FindVMA(target); - ASSERT_MSG(vma != vma_map.end(), "UnmapPhysicalMemory vma != end"); - - while (true) { - const auto vma_start = vma->second.base; - const auto vma_size = vma->second.size; - const auto state = vma->second.state; - const auto attr = vma->second.attribute; - - // Memory within region must be free or mapped heap. - if (!((state == MemoryState::Heap && attr == MemoryAttribute::None) || - (state == MemoryState::Unmapped))) { - return ERR_INVALID_ADDRESS_STATE; - } - - // If this is the last block and it's mapped, update mapped size. - if (last_addr <= (vma_start + vma_size - 1)) { - if (state == MemoryState::Heap) { - mapped_size += last_addr - cur_addr + 1; - } - break; - } - - if (state == MemoryState::Heap) { - mapped_size += vma_start + vma_size - cur_addr; - } - cur_addr = vma_start + vma_size; - vma++; - ASSERT_MSG(vma != vma_map.end(), "UnmapPhysicalMemory vma != end"); - } + // Check how much memory is currently mapped. + const auto mapped_size_result = SizeOfUnmappablePhysicalMemoryInRange(target, size); + if (mapped_size_result.Failed()) { + return mapped_size_result.Code(); + } - // If memory is already unmapped, we're done. - if (mapped_size == 0) { - return RESULT_SUCCESS; - } + // If we've already unmapped all the memory, return early. + const std::size_t mapped_size = *mapped_size_result; + if (mapped_size == 0) { + return RESULT_SUCCESS; } // Keep track of the memory regions we unmap. @@ -510,50 +420,45 @@ ResultCode VMManager::UnmapPhysicalMemory(VAddr target, u64 size) { { cur_addr = target; - auto vma = FindVMA(target); - ASSERT_MSG(vma != vma_map.end(), "UnmapPhysicalMemory vma != end"); + auto iter = FindVMA(target); + ASSERT_MSG(iter != vma_map.end(), "UnmapPhysicalMemory iter != end"); while (true) { - const auto vma_start = vma->second.base; - const auto vma_size = vma->second.size; - const auto state = vma->second.state; - const auto perm = vma->second.permissions; - - // Handle last block. - if (last_addr <= (vma_start + vma_size - 1)) { - if (state == MemoryState::Heap) { - result = UnmapRange(cur_addr, last_addr - cur_addr + 1); - if (result.IsSuccess()) { - unmapped_regions.push_back( - std::make_pair(cur_addr, last_addr - cur_addr + 1)); - } + const auto& vma = iter->second; + const auto vma_start = vma.base; + const auto vma_end = vma_start + vma.size; + const auto vma_last = vma_end - 1; + + // Unmap the memory block + const auto unmap_size = std::min(end_addr - cur_addr, vma_end - cur_addr); + if (vma.state == MemoryState::Heap) { + result = UnmapRange(cur_addr, unmap_size); + if (result.IsError()) { + break; } - break; + + unmapped_regions.emplace_back(cur_addr, unmap_size); } - if (state == MemoryState::Heap) { - result = UnmapRange(cur_addr, vma_start + vma_size - cur_addr); - if (result.IsSuccess()) { - unmapped_regions.push_back( - std::make_pair(cur_addr, vma_start + vma_size - cur_addr)); - } else { - break; - } + // Break once we hit the end of the range. + if (last_addr <= vma_last) { + break; } - cur_addr = vma_start + vma_size; - vma = FindVMA(cur_addr); - ASSERT_MSG(vma != vma_map.end(), "UnmapPhysicalMemory vma != end"); + // Advance to the next block. + cur_addr = vma_end; + iter = FindVMA(cur_addr); + ASSERT_MSG(iter != vma_map.end(), "UnmapPhysicalMemory iter != end"); } } // If we failed, re-map regions. // TODO: Preserve memory contents? if (result.IsError()) { - for (const auto& it : unmapped_regions) { + for (const auto [map_address, map_size] : unmapped_regions) { const auto remap_res = - MapMemoryBlock(it.first, std::make_shared>(it.second, 0), 0, - it.second, MemoryState::Heap, VMAPermission::None); + MapMemoryBlock(map_address, std::make_shared>(map_size, 0), 0, + map_size, MemoryState::Heap, VMAPermission::None); ASSERT_MSG(remap_res.Succeeded(), "UnmapPhysicalMemory re-map on error"); } } @@ -1085,6 +990,84 @@ VMManager::CheckResults VMManager::CheckRangeState(VAddr address, u64 size, Memo std::make_tuple(initial_state, initial_permissions, initial_attributes & ~ignore_mask)); } +ResultVal VMManager::SizeOfAllocatedVMAsInRange(VAddr address, + std::size_t size) const { + const VAddr end_addr = address + size; + const VAddr last_addr = end_addr - 1; + std::size_t mapped_size = 0; + + VAddr cur_addr = address; + auto iter = FindVMA(cur_addr); + ASSERT_MSG(iter != vma_map.end(), "SizeOfAllocatedVMAsInRange iter != end"); + + while (true) { + const auto& vma = iter->second; + const VAddr vma_start = vma.base; + const VAddr vma_end = vma_start + vma.size; + const VAddr vma_last = vma_end - 1; + + // Add size if relevant. + if (vma.state != MemoryState::Unmapped) { + mapped_size += std::min(end_addr - cur_addr, vma_end - cur_addr); + } + + // Break once we hit the end of the range. + if (last_addr <= vma_last) { + break; + } + + // Advance to the next block. + cur_addr = vma_end; + iter = std::next(iter); + ASSERT_MSG(iter != vma_map.end(), "SizeOfAllocatedVMAsInRange iter != end"); + } + + return MakeResult(mapped_size); +} + +ResultVal VMManager::SizeOfUnmappablePhysicalMemoryInRange(VAddr address, + std::size_t size) const { + const VAddr end_addr = address + size; + const VAddr last_addr = end_addr - 1; + std::size_t mapped_size = 0; + + VAddr cur_addr = address; + auto iter = FindVMA(cur_addr); + ASSERT_MSG(iter != vma_map.end(), "SizeOfUnmappablePhysicalMemoryInRange iter != end"); + + while (true) { + const auto& vma = iter->second; + const auto vma_start = vma.base; + const auto vma_end = vma_start + vma.size; + const auto vma_last = vma_end - 1; + const auto state = vma.state; + const auto attr = vma.attribute; + + // Memory within region must be free or mapped heap. + if (!((state == MemoryState::Heap && attr == MemoryAttribute::None) || + (state == MemoryState::Unmapped))) { + return ERR_INVALID_ADDRESS_STATE; + } + + // Add size if relevant. + if (state != MemoryState::Unmapped) { + mapped_size += std::min(end_addr - cur_addr, vma_end - cur_addr); + } + + // Break once we hit the end of the range. + if (last_addr <= vma_last) { + break; + } + + // Advance to the next block. + cur_addr = vma_end; + iter = std::next(iter); + ASSERT_MSG(iter != vma_map.end(), "SizeOfUnmappablePhysicalMemoryInRange iter != end"); + } + + return MakeResult(mapped_size); +} + u64 VMManager::GetTotalPhysicalMemoryAvailable() const { LOG_WARNING(Kernel, "(STUBBED) called"); return 0xF8000000; -- cgit v1.2.3 From 697206092e8ac28c7dfe83eff0eea6613082740c Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Mon, 8 Jul 2019 22:19:27 -0700 Subject: Prevent merging of device mapped memory blocks. This sets the DeviceMapped attribute for GPU-mapped memory blocks, and prevents merging device mapped blocks. This prevents memory mapped from the gpu from having its backing address changed by block coalesce. --- src/core/hle/kernel/vm_manager.cpp | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'src/core/hle/kernel/vm_manager.cpp') diff --git a/src/core/hle/kernel/vm_manager.cpp b/src/core/hle/kernel/vm_manager.cpp index 775d170bf..72a9d7717 100644 --- a/src/core/hle/kernel/vm_manager.cpp +++ b/src/core/hle/kernel/vm_manager.cpp @@ -51,6 +51,11 @@ bool VirtualMemoryArea::CanBeMergedWith(const VirtualMemoryArea& next) const { type != next.type) { return false; } + if ((attribute & MemoryAttribute::DeviceMapped) == MemoryAttribute::DeviceMapped) { + // TODO: Can device mapped memory be merged sanely? + // Not merging it may cause inaccuracies versus hardware when memory layout is queried. + return false; + } if (type == VMAType::AllocatedMemoryBlock) { return true; } -- cgit v1.2.3 From d4fc560c0539e5ba1d5cfcd03c92658699f20e5b Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Thu, 11 Jul 2019 15:12:33 -0700 Subject: Remove unicorn mappings/unmappings --- src/core/hle/kernel/vm_manager.cpp | 19 ------------------- 1 file changed, 19 deletions(-) (limited to 'src/core/hle/kernel/vm_manager.cpp') diff --git a/src/core/hle/kernel/vm_manager.cpp b/src/core/hle/kernel/vm_manager.cpp index 72a9d7717..02170d548 100644 --- a/src/core/hle/kernel/vm_manager.cpp +++ b/src/core/hle/kernel/vm_manager.cpp @@ -796,25 +796,6 @@ void VMManager::MergeAdjacentVMA(VirtualMemoryArea& left, const VirtualMemoryAre // Page table update is needed, because backing memory changed. left.size += right.size; UpdatePageTableForVMA(left); - - // Update mappings for unicorn. - system.ArmInterface(0).UnmapMemory(left.base, left.size); - system.ArmInterface(1).UnmapMemory(left.base, left.size); - system.ArmInterface(2).UnmapMemory(left.base, left.size); - system.ArmInterface(3).UnmapMemory(left.base, left.size); - - system.ArmInterface(0).MapBackingMemory(left.base, left.size, - left.backing_block->data() + left.offset, - VMAPermission::ReadWriteExecute); - system.ArmInterface(1).MapBackingMemory(left.base, left.size, - left.backing_block->data() + left.offset, - VMAPermission::ReadWriteExecute); - system.ArmInterface(2).MapBackingMemory(left.base, left.size, - left.backing_block->data() + left.offset, - VMAPermission::ReadWriteExecute); - system.ArmInterface(3).MapBackingMemory(left.base, left.size, - left.backing_block->data() + left.offset, - VMAPermission::ReadWriteExecute); } else { // Just update the size. left.size += right.size; -- cgit v1.2.3