2015-05-21 03:37:07 +00:00
|
|
|
// Copyright 2015 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2018-08-02 16:45:56 +00:00
|
|
|
#include <algorithm>
|
2015-06-21 14:11:32 +00:00
|
|
|
#include <iterator>
|
2018-07-18 23:02:47 +00:00
|
|
|
#include <utility>
|
2015-05-21 03:37:07 +00:00
|
|
|
#include "common/assert.h"
|
2018-01-01 20:59:31 +00:00
|
|
|
#include "common/logging/log.h"
|
2017-10-10 03:56:20 +00:00
|
|
|
#include "core/arm/arm_interface.h"
|
2018-01-16 18:05:21 +00:00
|
|
|
#include "core/core.h"
|
2018-09-23 00:09:32 +00:00
|
|
|
#include "core/file_sys/program_metadata.h"
|
2017-05-21 07:11:36 +00:00
|
|
|
#include "core/hle/kernel/errors.h"
|
2016-09-21 06:52:38 +00:00
|
|
|
#include "core/hle/kernel/vm_manager.h"
|
2016-05-26 17:53:30 +00:00
|
|
|
#include "core/memory.h"
|
2018-01-27 15:16:39 +00:00
|
|
|
#include "core/memory_hook.h"
|
2015-05-21 03:37:07 +00:00
|
|
|
#include "core/memory_setup.h"
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2015-07-18 00:55:48 +00:00
|
|
|
static const char* GetMemoryStateName(MemoryState state) {
|
2018-07-18 22:40:35 +00:00
|
|
|
static constexpr const char* names[] = {
|
|
|
|
"Unmapped", "Io",
|
|
|
|
"Normal", "CodeStatic",
|
|
|
|
"CodeMutable", "Heap",
|
|
|
|
"Shared", "Unknown1",
|
|
|
|
"ModuleCodeStatic", "ModuleCodeMutable",
|
|
|
|
"IpcBuffer0", "Mapped",
|
|
|
|
"ThreadLocal", "TransferMemoryIsolated",
|
|
|
|
"TransferMemory", "ProcessMemory",
|
|
|
|
"Unknown2", "IpcBuffer1",
|
|
|
|
"IpcBuffer3", "KernelStack",
|
2015-07-18 00:55:48 +00:00
|
|
|
};
|
|
|
|
|
2018-07-18 22:40:35 +00:00
|
|
|
return names[static_cast<int>(state)];
|
2015-07-18 00:55:48 +00:00
|
|
|
}
|
|
|
|
|
2015-05-21 03:37:07 +00:00
|
|
|
bool VirtualMemoryArea::CanBeMergedWith(const VirtualMemoryArea& next) const {
|
|
|
|
ASSERT(base + size == next.base);
|
2016-09-18 00:38:01 +00:00
|
|
|
if (permissions != next.permissions || meminfo_state != next.meminfo_state ||
|
|
|
|
type != next.type) {
|
2015-05-21 03:37:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (type == VMAType::AllocatedMemoryBlock &&
|
2016-09-18 00:38:01 +00:00
|
|
|
(backing_block != next.backing_block || offset + size != next.offset)) {
|
2015-05-21 03:37:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (type == VMAType::BackingMemory && backing_memory + size != next.backing_memory) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (type == VMAType::MMIO && paddr + size != next.paddr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
VMManager::VMManager() {
|
2018-09-23 00:09:32 +00:00
|
|
|
// Default to assuming a 39-bit address space. This way we have a sane
|
|
|
|
// starting point with executables that don't provide metadata.
|
|
|
|
Reset(FileSys::ProgramAddressSpaceType::Is39Bit);
|
2015-05-21 03:37:07 +00:00
|
|
|
}
|
|
|
|
|
2015-07-10 01:52:15 +00:00
|
|
|
VMManager::~VMManager() {
|
2018-09-23 00:09:32 +00:00
|
|
|
Reset(FileSys::ProgramAddressSpaceType::Is39Bit);
|
2015-07-10 01:52:15 +00:00
|
|
|
}
|
|
|
|
|
2018-09-23 00:09:32 +00:00
|
|
|
void VMManager::Reset(FileSys::ProgramAddressSpaceType type) {
|
|
|
|
Clear();
|
2018-09-24 14:29:56 +00:00
|
|
|
|
2018-09-23 00:09:32 +00:00
|
|
|
InitializeMemoryRegionRanges(type);
|
2015-05-21 03:37:07 +00:00
|
|
|
|
2018-09-24 14:29:56 +00:00
|
|
|
page_table.Resize(address_space_width);
|
|
|
|
|
2015-05-21 03:37:07 +00:00
|
|
|
// Initialize the map with a single free region covering the entire managed space.
|
|
|
|
VirtualMemoryArea initial_vma;
|
2018-09-24 14:29:56 +00:00
|
|
|
initial_vma.size = address_space_end;
|
2015-05-21 03:37:07 +00:00
|
|
|
vma_map.emplace(initial_vma.base, initial_vma);
|
|
|
|
|
2018-01-01 20:59:31 +00:00
|
|
|
UpdatePageTableForVMA(initial_vma);
|
2015-05-21 03:37:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VMManager::VMAHandle VMManager::FindVMA(VAddr target) const {
|
2018-09-24 14:29:56 +00:00
|
|
|
if (target >= address_space_end) {
|
2015-07-18 02:19:16 +00:00
|
|
|
return vma_map.end();
|
|
|
|
} else {
|
|
|
|
return std::prev(vma_map.upper_bound(target));
|
|
|
|
}
|
2015-05-21 03:37:07 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 15:59:22 +00:00
|
|
|
bool VMManager::IsValidHandle(VMAHandle handle) const {
|
|
|
|
return handle != vma_map.cend();
|
|
|
|
}
|
|
|
|
|
2015-05-21 03:37:07 +00:00
|
|
|
ResultVal<VMManager::VMAHandle> VMManager::MapMemoryBlock(VAddr target,
|
2016-09-18 00:38:01 +00:00
|
|
|
std::shared_ptr<std::vector<u8>> block,
|
2018-09-15 13:21:06 +00:00
|
|
|
std::size_t offset, u64 size,
|
2016-09-18 00:38:01 +00:00
|
|
|
MemoryState state) {
|
2015-05-21 03:37:07 +00:00
|
|
|
ASSERT(block != nullptr);
|
|
|
|
ASSERT(offset + size <= block->size());
|
|
|
|
|
|
|
|
// This is the appropriately sized VMA that will turn into our allocation.
|
|
|
|
CASCADE_RESULT(VMAIter vma_handle, CarveVMA(target, size));
|
|
|
|
VirtualMemoryArea& final_vma = vma_handle->second;
|
|
|
|
ASSERT(final_vma.size == size);
|
|
|
|
|
2018-05-03 02:36:51 +00:00
|
|
|
auto& system = Core::System::GetInstance();
|
|
|
|
system.ArmInterface(0).MapBackingMemory(target, size, block->data() + offset,
|
|
|
|
VMAPermission::ReadWriteExecute);
|
|
|
|
system.ArmInterface(1).MapBackingMemory(target, size, block->data() + offset,
|
|
|
|
VMAPermission::ReadWriteExecute);
|
|
|
|
system.ArmInterface(2).MapBackingMemory(target, size, block->data() + offset,
|
|
|
|
VMAPermission::ReadWriteExecute);
|
|
|
|
system.ArmInterface(3).MapBackingMemory(target, size, block->data() + offset,
|
|
|
|
VMAPermission::ReadWriteExecute);
|
2017-10-10 03:56:20 +00:00
|
|
|
|
2015-05-21 03:37:07 +00:00
|
|
|
final_vma.type = VMAType::AllocatedMemoryBlock;
|
|
|
|
final_vma.permissions = VMAPermission::ReadWrite;
|
|
|
|
final_vma.meminfo_state = state;
|
2018-07-18 23:02:47 +00:00
|
|
|
final_vma.backing_block = std::move(block);
|
2015-05-21 03:37:07 +00:00
|
|
|
final_vma.offset = offset;
|
|
|
|
UpdatePageTableForVMA(final_vma);
|
|
|
|
|
|
|
|
return MakeResult<VMAHandle>(MergeAdjacent(vma_handle));
|
|
|
|
}
|
|
|
|
|
2017-09-02 03:10:03 +00:00
|
|
|
ResultVal<VMManager::VMAHandle> VMManager::MapBackingMemory(VAddr target, u8* memory, u64 size,
|
2016-09-18 00:38:01 +00:00
|
|
|
MemoryState state) {
|
2015-05-21 03:37:07 +00:00
|
|
|
ASSERT(memory != nullptr);
|
|
|
|
|
|
|
|
// This is the appropriately sized VMA that will turn into our allocation.
|
|
|
|
CASCADE_RESULT(VMAIter vma_handle, CarveVMA(target, size));
|
|
|
|
VirtualMemoryArea& final_vma = vma_handle->second;
|
|
|
|
ASSERT(final_vma.size == size);
|
|
|
|
|
2018-05-03 02:36:51 +00:00
|
|
|
auto& system = Core::System::GetInstance();
|
|
|
|
system.ArmInterface(0).MapBackingMemory(target, size, memory, VMAPermission::ReadWriteExecute);
|
|
|
|
system.ArmInterface(1).MapBackingMemory(target, size, memory, VMAPermission::ReadWriteExecute);
|
|
|
|
system.ArmInterface(2).MapBackingMemory(target, size, memory, VMAPermission::ReadWriteExecute);
|
|
|
|
system.ArmInterface(3).MapBackingMemory(target, size, memory, VMAPermission::ReadWriteExecute);
|
2017-10-10 03:56:20 +00:00
|
|
|
|
2015-05-21 03:37:07 +00:00
|
|
|
final_vma.type = VMAType::BackingMemory;
|
|
|
|
final_vma.permissions = VMAPermission::ReadWrite;
|
|
|
|
final_vma.meminfo_state = state;
|
|
|
|
final_vma.backing_memory = memory;
|
|
|
|
UpdatePageTableForVMA(final_vma);
|
|
|
|
|
|
|
|
return MakeResult<VMAHandle>(MergeAdjacent(vma_handle));
|
|
|
|
}
|
|
|
|
|
2018-10-23 22:39:10 +00:00
|
|
|
ResultVal<VAddr> 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<VAddr>(target);
|
|
|
|
}
|
|
|
|
|
2017-09-02 03:10:03 +00:00
|
|
|
ResultVal<VMManager::VMAHandle> VMManager::MapMMIO(VAddr target, PAddr paddr, u64 size,
|
2016-09-18 00:38:01 +00:00
|
|
|
MemoryState state,
|
2018-01-27 15:16:39 +00:00
|
|
|
Memory::MemoryHookPointer mmio_handler) {
|
2015-05-21 03:37:07 +00:00
|
|
|
// This is the appropriately sized VMA that will turn into our allocation.
|
|
|
|
CASCADE_RESULT(VMAIter vma_handle, CarveVMA(target, size));
|
|
|
|
VirtualMemoryArea& final_vma = vma_handle->second;
|
|
|
|
ASSERT(final_vma.size == size);
|
|
|
|
|
|
|
|
final_vma.type = VMAType::MMIO;
|
|
|
|
final_vma.permissions = VMAPermission::ReadWrite;
|
|
|
|
final_vma.meminfo_state = state;
|
|
|
|
final_vma.paddr = paddr;
|
2018-07-18 23:02:47 +00:00
|
|
|
final_vma.mmio_handler = std::move(mmio_handler);
|
2015-05-21 03:37:07 +00:00
|
|
|
UpdatePageTableForVMA(final_vma);
|
|
|
|
|
|
|
|
return MakeResult<VMAHandle>(MergeAdjacent(vma_handle));
|
|
|
|
}
|
|
|
|
|
2015-07-18 02:19:16 +00:00
|
|
|
VMManager::VMAIter VMManager::Unmap(VMAIter vma_handle) {
|
|
|
|
VirtualMemoryArea& vma = vma_handle->second;
|
2015-05-21 03:37:07 +00:00
|
|
|
vma.type = VMAType::Free;
|
|
|
|
vma.permissions = VMAPermission::None;
|
2018-03-10 22:46:23 +00:00
|
|
|
vma.meminfo_state = MemoryState::Unmapped;
|
2015-05-21 03:37:07 +00:00
|
|
|
|
|
|
|
vma.backing_block = nullptr;
|
|
|
|
vma.offset = 0;
|
|
|
|
vma.backing_memory = nullptr;
|
|
|
|
vma.paddr = 0;
|
|
|
|
|
|
|
|
UpdatePageTableForVMA(vma);
|
|
|
|
|
2015-07-18 02:19:16 +00:00
|
|
|
return MergeAdjacent(vma_handle);
|
|
|
|
}
|
|
|
|
|
2017-09-02 03:10:03 +00:00
|
|
|
ResultCode VMManager::UnmapRange(VAddr target, u64 size) {
|
2015-07-18 02:19:16 +00:00
|
|
|
CASCADE_RESULT(VMAIter vma, CarveVMARange(target, size));
|
2018-08-02 16:19:05 +00:00
|
|
|
const VAddr target_end = target + size;
|
2015-07-18 02:19:16 +00:00
|
|
|
|
2018-08-02 16:19:05 +00:00
|
|
|
const VMAIter end = vma_map.end();
|
2015-07-18 02:19:16 +00:00
|
|
|
// The comparison against the end of the range must be done using addresses since VMAs can be
|
|
|
|
// merged during this process, causing invalidation of the iterators.
|
|
|
|
while (vma != end && vma->second.base < target_end) {
|
|
|
|
vma = std::next(Unmap(vma));
|
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT(FindVMA(target)->second.size >= size);
|
2018-03-16 22:22:14 +00:00
|
|
|
|
2018-05-03 02:36:51 +00:00
|
|
|
auto& system = Core::System::GetInstance();
|
|
|
|
system.ArmInterface(0).UnmapMemory(target, size);
|
|
|
|
system.ArmInterface(1).UnmapMemory(target, size);
|
|
|
|
system.ArmInterface(2).UnmapMemory(target, size);
|
|
|
|
system.ArmInterface(3).UnmapMemory(target, size);
|
2018-03-16 22:22:14 +00:00
|
|
|
|
2015-07-18 02:19:16 +00:00
|
|
|
return RESULT_SUCCESS;
|
2015-05-21 03:37:07 +00:00
|
|
|
}
|
|
|
|
|
2015-07-18 02:19:16 +00:00
|
|
|
VMManager::VMAHandle VMManager::Reprotect(VMAHandle vma_handle, VMAPermission new_perms) {
|
2015-05-21 03:37:07 +00:00
|
|
|
VMAIter iter = StripIterConstness(vma_handle);
|
|
|
|
|
|
|
|
VirtualMemoryArea& vma = iter->second;
|
|
|
|
vma.permissions = new_perms;
|
|
|
|
UpdatePageTableForVMA(vma);
|
|
|
|
|
2015-07-18 02:19:16 +00:00
|
|
|
return MergeAdjacent(iter);
|
|
|
|
}
|
|
|
|
|
2017-09-02 03:10:03 +00:00
|
|
|
ResultCode VMManager::ReprotectRange(VAddr target, u64 size, VMAPermission new_perms) {
|
2015-07-18 02:19:16 +00:00
|
|
|
CASCADE_RESULT(VMAIter vma, CarveVMARange(target, size));
|
2018-08-02 16:19:05 +00:00
|
|
|
const VAddr target_end = target + size;
|
2015-07-18 02:19:16 +00:00
|
|
|
|
2018-08-02 16:19:05 +00:00
|
|
|
const VMAIter end = vma_map.end();
|
2015-07-18 02:19:16 +00:00
|
|
|
// The comparison against the end of the range must be done using addresses since VMAs can be
|
|
|
|
// merged during this process, causing invalidation of the iterators.
|
|
|
|
while (vma != end && vma->second.base < target_end) {
|
|
|
|
vma = std::next(StripIterConstness(Reprotect(vma, new_perms)));
|
|
|
|
}
|
|
|
|
|
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2018-11-13 16:06:33 +00:00
|
|
|
ResultVal<VAddr> VMManager::HeapAllocate(VAddr target, u64 size, VMAPermission perms) {
|
|
|
|
if (target < GetHeapRegionBaseAddress() || target + size > GetHeapRegionEndAddress() ||
|
|
|
|
target + size < target) {
|
|
|
|
return ERR_INVALID_ADDRESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (heap_memory == nullptr) {
|
|
|
|
// Initialize heap
|
|
|
|
heap_memory = std::make_shared<std::vector<u8>>();
|
|
|
|
heap_start = heap_end = target;
|
|
|
|
} else {
|
|
|
|
UnmapRange(heap_start, heap_end - heap_start);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If necessary, expand backing vector to cover new heap extents.
|
|
|
|
if (target < heap_start) {
|
|
|
|
heap_memory->insert(begin(*heap_memory), heap_start - target, 0);
|
|
|
|
heap_start = target;
|
|
|
|
RefreshMemoryBlockMappings(heap_memory.get());
|
|
|
|
}
|
|
|
|
if (target + size > heap_end) {
|
|
|
|
heap_memory->insert(end(*heap_memory), (target + size) - heap_end, 0);
|
|
|
|
heap_end = target + size;
|
|
|
|
RefreshMemoryBlockMappings(heap_memory.get());
|
|
|
|
}
|
|
|
|
ASSERT(heap_end - heap_start == heap_memory->size());
|
|
|
|
|
|
|
|
CASCADE_RESULT(auto vma, MapMemoryBlock(target, heap_memory, target - heap_start, size,
|
|
|
|
MemoryState::Heap));
|
|
|
|
Reprotect(vma, perms);
|
|
|
|
|
|
|
|
heap_used = size;
|
|
|
|
|
|
|
|
return MakeResult<VAddr>(heap_end - size);
|
|
|
|
}
|
|
|
|
|
|
|
|
ResultCode VMManager::HeapFree(VAddr target, u64 size) {
|
|
|
|
if (target < GetHeapRegionBaseAddress() || target + size > GetHeapRegionEndAddress() ||
|
|
|
|
target + size < target) {
|
|
|
|
return ERR_INVALID_ADDRESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size == 0) {
|
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ResultCode result = UnmapRange(target, size);
|
|
|
|
if (result.IsError()) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
heap_used -= size;
|
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2018-11-18 02:40:17 +00:00
|
|
|
ResultCode VMManager::MirrorMemory(VAddr dst_addr, VAddr src_addr, u64 size, MemoryState state) {
|
2018-11-13 16:06:33 +00:00
|
|
|
const auto vma = FindVMA(src_addr);
|
|
|
|
|
|
|
|
ASSERT_MSG(vma != vma_map.end(), "Invalid memory address");
|
|
|
|
ASSERT_MSG(vma->second.backing_block, "Backing block doesn't exist for address");
|
|
|
|
|
|
|
|
// The returned VMA might be a bigger one encompassing the desired address.
|
|
|
|
const auto vma_offset = src_addr - vma->first;
|
|
|
|
ASSERT_MSG(vma_offset + size <= vma->second.size,
|
|
|
|
"Shared memory exceeds bounds of mapped block");
|
|
|
|
|
|
|
|
const std::shared_ptr<std::vector<u8>>& backing_block = vma->second.backing_block;
|
|
|
|
const std::size_t backing_block_offset = vma->second.offset + vma_offset;
|
|
|
|
|
2018-11-18 02:40:17 +00:00
|
|
|
CASCADE_RESULT(auto new_vma,
|
|
|
|
MapMemoryBlock(dst_addr, backing_block, backing_block_offset, size, state));
|
2018-11-13 16:06:33 +00:00
|
|
|
// Protect mirror with permissions from old region
|
|
|
|
Reprotect(new_vma, vma->second.permissions);
|
|
|
|
// Remove permissions from old region
|
|
|
|
Reprotect(vma, VMAPermission::None);
|
|
|
|
|
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2015-07-18 02:19:16 +00:00
|
|
|
void VMManager::RefreshMemoryBlockMappings(const std::vector<u8>* block) {
|
|
|
|
// If this ever proves to have a noticeable performance impact, allow users of the function to
|
|
|
|
// specify a specific range of addresses to limit the scan to.
|
|
|
|
for (const auto& p : vma_map) {
|
|
|
|
const VirtualMemoryArea& vma = p.second;
|
|
|
|
if (block == vma.backing_block.get()) {
|
|
|
|
UpdatePageTableForVMA(vma);
|
|
|
|
}
|
|
|
|
}
|
2015-05-21 03:37:07 +00:00
|
|
|
}
|
|
|
|
|
2018-04-27 15:49:18 +00:00
|
|
|
void VMManager::LogLayout() const {
|
2015-07-10 01:52:15 +00:00
|
|
|
for (const auto& p : vma_map) {
|
|
|
|
const VirtualMemoryArea& vma = p.second;
|
2018-07-02 16:13:26 +00:00
|
|
|
LOG_DEBUG(Kernel, "{:016X} - {:016X} size: {:016X} {}{}{} {}", vma.base,
|
2018-07-02 16:20:50 +00:00
|
|
|
vma.base + vma.size, vma.size,
|
|
|
|
(u8)vma.permissions & (u8)VMAPermission::Read ? 'R' : '-',
|
|
|
|
(u8)vma.permissions & (u8)VMAPermission::Write ? 'W' : '-',
|
|
|
|
(u8)vma.permissions & (u8)VMAPermission::Execute ? 'X' : '-',
|
|
|
|
GetMemoryStateName(vma.meminfo_state));
|
2015-07-10 01:52:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
VMManager::VMAIter VMManager::StripIterConstness(const VMAHandle& iter) {
|
2015-05-21 03:37:07 +00:00
|
|
|
// This uses a neat C++ trick to convert a const_iterator to a regular iterator, given
|
|
|
|
// non-const access to its container.
|
|
|
|
return vma_map.erase(iter, iter); // Erases an empty range of elements
|
|
|
|
}
|
|
|
|
|
2017-09-02 03:10:03 +00:00
|
|
|
ResultVal<VMManager::VMAIter> VMManager::CarveVMA(VAddr base, u64 size) {
|
2018-05-02 13:14:28 +00:00
|
|
|
ASSERT_MSG((size & Memory::PAGE_MASK) == 0, "non-page aligned size: 0x{:016X}", size);
|
|
|
|
ASSERT_MSG((base & Memory::PAGE_MASK) == 0, "non-page aligned base: 0x{:016X}", base);
|
2015-05-21 03:37:07 +00:00
|
|
|
|
|
|
|
VMAIter vma_handle = StripIterConstness(FindVMA(base));
|
|
|
|
if (vma_handle == vma_map.end()) {
|
|
|
|
// Target address is outside the range managed by the kernel
|
2015-07-18 01:34:50 +00:00
|
|
|
return ERR_INVALID_ADDRESS;
|
2015-05-21 03:37:07 +00:00
|
|
|
}
|
|
|
|
|
2018-08-02 16:19:05 +00:00
|
|
|
const VirtualMemoryArea& vma = vma_handle->second;
|
2015-05-21 03:37:07 +00:00
|
|
|
if (vma.type != VMAType::Free) {
|
|
|
|
// Region is already allocated
|
2015-07-18 01:34:50 +00:00
|
|
|
return ERR_INVALID_ADDRESS_STATE;
|
2015-05-21 03:37:07 +00:00
|
|
|
}
|
|
|
|
|
2018-08-02 16:19:05 +00:00
|
|
|
const VAddr start_in_vma = base - vma.base;
|
|
|
|
const VAddr end_in_vma = start_in_vma + size;
|
2015-05-21 03:37:07 +00:00
|
|
|
|
|
|
|
if (end_in_vma > vma.size) {
|
|
|
|
// Requested allocation doesn't fit inside VMA
|
2015-07-18 01:34:50 +00:00
|
|
|
return ERR_INVALID_ADDRESS_STATE;
|
2015-05-21 03:37:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (end_in_vma != vma.size) {
|
|
|
|
// Split VMA at the end of the allocated region
|
|
|
|
SplitVMA(vma_handle, end_in_vma);
|
|
|
|
}
|
|
|
|
if (start_in_vma != 0) {
|
|
|
|
// Split VMA at the start of the allocated region
|
|
|
|
vma_handle = SplitVMA(vma_handle, start_in_vma);
|
|
|
|
}
|
|
|
|
|
|
|
|
return MakeResult<VMAIter>(vma_handle);
|
|
|
|
}
|
|
|
|
|
2017-09-02 03:10:03 +00:00
|
|
|
ResultVal<VMManager::VMAIter> VMManager::CarveVMARange(VAddr target, u64 size) {
|
2018-05-02 13:14:28 +00:00
|
|
|
ASSERT_MSG((size & Memory::PAGE_MASK) == 0, "non-page aligned size: 0x{:016X}", size);
|
|
|
|
ASSERT_MSG((target & Memory::PAGE_MASK) == 0, "non-page aligned base: 0x{:016X}", target);
|
2015-07-18 02:19:16 +00:00
|
|
|
|
2018-08-02 16:19:05 +00:00
|
|
|
const VAddr target_end = target + size;
|
2015-07-18 02:19:16 +00:00
|
|
|
ASSERT(target_end >= target);
|
2018-09-24 14:29:56 +00:00
|
|
|
ASSERT(target_end <= address_space_end);
|
2015-07-18 02:19:16 +00:00
|
|
|
ASSERT(size > 0);
|
|
|
|
|
|
|
|
VMAIter begin_vma = StripIterConstness(FindVMA(target));
|
2018-08-02 16:19:05 +00:00
|
|
|
const VMAIter i_end = vma_map.lower_bound(target_end);
|
2018-08-02 16:45:56 +00:00
|
|
|
if (std::any_of(begin_vma, i_end,
|
|
|
|
[](const auto& entry) { return entry.second.type == VMAType::Free; })) {
|
|
|
|
return ERR_INVALID_ADDRESS_STATE;
|
2015-07-18 02:19:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (target != begin_vma->second.base) {
|
|
|
|
begin_vma = SplitVMA(begin_vma, target - begin_vma->second.base);
|
|
|
|
}
|
|
|
|
|
|
|
|
VMAIter end_vma = StripIterConstness(FindVMA(target_end));
|
|
|
|
if (end_vma != vma_map.end() && target_end != end_vma->second.base) {
|
|
|
|
end_vma = SplitVMA(end_vma, target_end - end_vma->second.base);
|
|
|
|
}
|
|
|
|
|
|
|
|
return MakeResult<VMAIter>(begin_vma);
|
|
|
|
}
|
|
|
|
|
2017-09-02 03:10:03 +00:00
|
|
|
VMManager::VMAIter VMManager::SplitVMA(VMAIter vma_handle, u64 offset_in_vma) {
|
2015-05-21 03:37:07 +00:00
|
|
|
VirtualMemoryArea& old_vma = vma_handle->second;
|
|
|
|
VirtualMemoryArea new_vma = old_vma; // Make a copy of the VMA
|
|
|
|
|
|
|
|
// For now, don't allow no-op VMA splits (trying to split at a boundary) because it's probably
|
|
|
|
// a bug. This restriction might be removed later.
|
|
|
|
ASSERT(offset_in_vma < old_vma.size);
|
|
|
|
ASSERT(offset_in_vma > 0);
|
|
|
|
|
|
|
|
old_vma.size = offset_in_vma;
|
|
|
|
new_vma.base += offset_in_vma;
|
|
|
|
new_vma.size -= offset_in_vma;
|
|
|
|
|
|
|
|
switch (new_vma.type) {
|
|
|
|
case VMAType::Free:
|
|
|
|
break;
|
|
|
|
case VMAType::AllocatedMemoryBlock:
|
|
|
|
new_vma.offset += offset_in_vma;
|
|
|
|
break;
|
|
|
|
case VMAType::BackingMemory:
|
|
|
|
new_vma.backing_memory += offset_in_vma;
|
|
|
|
break;
|
|
|
|
case VMAType::MMIO:
|
|
|
|
new_vma.paddr += offset_in_vma;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT(old_vma.CanBeMergedWith(new_vma));
|
|
|
|
|
|
|
|
return vma_map.emplace_hint(std::next(vma_handle), new_vma.base, new_vma);
|
|
|
|
}
|
|
|
|
|
|
|
|
VMManager::VMAIter VMManager::MergeAdjacent(VMAIter iter) {
|
2018-08-02 16:19:05 +00:00
|
|
|
const VMAIter next_vma = std::next(iter);
|
2015-05-21 03:37:07 +00:00
|
|
|
if (next_vma != vma_map.end() && iter->second.CanBeMergedWith(next_vma->second)) {
|
|
|
|
iter->second.size += next_vma->second.size;
|
|
|
|
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;
|
|
|
|
vma_map.erase(iter);
|
|
|
|
iter = prev_vma;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return iter;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VMManager::UpdatePageTableForVMA(const VirtualMemoryArea& vma) {
|
|
|
|
switch (vma.type) {
|
|
|
|
case VMAType::Free:
|
2017-07-22 02:17:57 +00:00
|
|
|
Memory::UnmapRegion(page_table, vma.base, vma.size);
|
2015-05-21 03:37:07 +00:00
|
|
|
break;
|
|
|
|
case VMAType::AllocatedMemoryBlock:
|
2017-07-22 02:17:57 +00:00
|
|
|
Memory::MapMemoryRegion(page_table, vma.base, vma.size,
|
|
|
|
vma.backing_block->data() + vma.offset);
|
2015-05-21 03:37:07 +00:00
|
|
|
break;
|
|
|
|
case VMAType::BackingMemory:
|
2017-07-22 02:17:57 +00:00
|
|
|
Memory::MapMemoryRegion(page_table, vma.base, vma.size, vma.backing_memory);
|
2015-05-21 03:37:07 +00:00
|
|
|
break;
|
|
|
|
case VMAType::MMIO:
|
2017-07-22 02:17:57 +00:00
|
|
|
Memory::MapIoRegion(page_table, vma.base, vma.size, vma.mmio_handler);
|
2015-05-21 03:37:07 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-01-01 20:59:31 +00:00
|
|
|
|
2018-09-23 00:09:32 +00:00
|
|
|
void VMManager::InitializeMemoryRegionRanges(FileSys::ProgramAddressSpaceType type) {
|
|
|
|
u64 map_region_size = 0;
|
|
|
|
u64 heap_region_size = 0;
|
|
|
|
u64 new_map_region_size = 0;
|
|
|
|
u64 tls_io_region_size = 0;
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case FileSys::ProgramAddressSpaceType::Is32Bit:
|
svc: Clarify enum values for AddressSpaceBaseAddr and AddressSpaceSize in svcGetInfo()
So, one thing that's puzzled me is why the kernel seemed to *not* use
the direct code address ranges in some cases for some service functions.
For example, in svcMapMemory, the full address space width is compared
against for validity, but for svcMapSharedMemory, it compares against
0xFFE00000, 0xFF8000000, and 0x7FF8000000 as upper bounds, and uses
either 0x200000 or 0x8000000 as the lower-bounds as the beginning of the
compared range. Coincidentally, these exact same values are also used in
svcGetInfo, and also when initializing the user address space, so this
is actually retrieving the ASLR extents, not the extents of the address
space in general.
2018-10-14 18:44:38 +00:00
|
|
|
case FileSys::ProgramAddressSpaceType::Is32BitNoMap:
|
2018-09-23 00:09:32 +00:00
|
|
|
address_space_width = 32;
|
|
|
|
code_region_base = 0x200000;
|
|
|
|
code_region_end = code_region_base + 0x3FE00000;
|
svc: Clarify enum values for AddressSpaceBaseAddr and AddressSpaceSize in svcGetInfo()
So, one thing that's puzzled me is why the kernel seemed to *not* use
the direct code address ranges in some cases for some service functions.
For example, in svcMapMemory, the full address space width is compared
against for validity, but for svcMapSharedMemory, it compares against
0xFFE00000, 0xFF8000000, and 0x7FF8000000 as upper bounds, and uses
either 0x200000 or 0x8000000 as the lower-bounds as the beginning of the
compared range. Coincidentally, these exact same values are also used in
svcGetInfo, and also when initializing the user address space, so this
is actually retrieving the ASLR extents, not the extents of the address
space in general.
2018-10-14 18:44:38 +00:00
|
|
|
aslr_region_base = 0x200000;
|
|
|
|
aslr_region_end = aslr_region_base + 0xFFE00000;
|
|
|
|
if (type == FileSys::ProgramAddressSpaceType::Is32Bit) {
|
|
|
|
map_region_size = 0x40000000;
|
|
|
|
heap_region_size = 0x40000000;
|
|
|
|
} else {
|
|
|
|
map_region_size = 0;
|
|
|
|
heap_region_size = 0x80000000;
|
|
|
|
}
|
2018-09-23 00:09:32 +00:00
|
|
|
break;
|
|
|
|
case FileSys::ProgramAddressSpaceType::Is36Bit:
|
|
|
|
address_space_width = 36;
|
|
|
|
code_region_base = 0x8000000;
|
|
|
|
code_region_end = code_region_base + 0x78000000;
|
svc: Clarify enum values for AddressSpaceBaseAddr and AddressSpaceSize in svcGetInfo()
So, one thing that's puzzled me is why the kernel seemed to *not* use
the direct code address ranges in some cases for some service functions.
For example, in svcMapMemory, the full address space width is compared
against for validity, but for svcMapSharedMemory, it compares against
0xFFE00000, 0xFF8000000, and 0x7FF8000000 as upper bounds, and uses
either 0x200000 or 0x8000000 as the lower-bounds as the beginning of the
compared range. Coincidentally, these exact same values are also used in
svcGetInfo, and also when initializing the user address space, so this
is actually retrieving the ASLR extents, not the extents of the address
space in general.
2018-10-14 18:44:38 +00:00
|
|
|
aslr_region_base = 0x8000000;
|
|
|
|
aslr_region_end = aslr_region_base + 0xFF8000000;
|
2018-09-23 00:09:32 +00:00
|
|
|
map_region_size = 0x180000000;
|
|
|
|
heap_region_size = 0x180000000;
|
|
|
|
break;
|
|
|
|
case FileSys::ProgramAddressSpaceType::Is39Bit:
|
|
|
|
address_space_width = 39;
|
|
|
|
code_region_base = 0x8000000;
|
|
|
|
code_region_end = code_region_base + 0x80000000;
|
svc: Clarify enum values for AddressSpaceBaseAddr and AddressSpaceSize in svcGetInfo()
So, one thing that's puzzled me is why the kernel seemed to *not* use
the direct code address ranges in some cases for some service functions.
For example, in svcMapMemory, the full address space width is compared
against for validity, but for svcMapSharedMemory, it compares against
0xFFE00000, 0xFF8000000, and 0x7FF8000000 as upper bounds, and uses
either 0x200000 or 0x8000000 as the lower-bounds as the beginning of the
compared range. Coincidentally, these exact same values are also used in
svcGetInfo, and also when initializing the user address space, so this
is actually retrieving the ASLR extents, not the extents of the address
space in general.
2018-10-14 18:44:38 +00:00
|
|
|
aslr_region_base = 0x8000000;
|
|
|
|
aslr_region_end = aslr_region_base + 0x7FF8000000;
|
2018-09-23 00:09:32 +00:00
|
|
|
map_region_size = 0x1000000000;
|
|
|
|
heap_region_size = 0x180000000;
|
|
|
|
new_map_region_size = 0x80000000;
|
|
|
|
tls_io_region_size = 0x1000000000;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
UNREACHABLE_MSG("Invalid address space type specified: {}", static_cast<u32>(type));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
address_space_base = 0;
|
|
|
|
address_space_end = 1ULL << address_space_width;
|
|
|
|
|
|
|
|
map_region_base = code_region_end;
|
|
|
|
map_region_end = map_region_base + map_region_size;
|
|
|
|
|
|
|
|
heap_region_base = map_region_end;
|
|
|
|
heap_region_end = heap_region_base + heap_region_size;
|
|
|
|
|
|
|
|
new_map_region_base = heap_region_end;
|
|
|
|
new_map_region_end = new_map_region_base + new_map_region_size;
|
|
|
|
|
|
|
|
tls_io_region_base = new_map_region_end;
|
|
|
|
tls_io_region_end = tls_io_region_base + tls_io_region_size;
|
|
|
|
|
|
|
|
if (new_map_region_size == 0) {
|
|
|
|
new_map_region_base = address_space_base;
|
|
|
|
new_map_region_end = address_space_end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VMManager::Clear() {
|
|
|
|
ClearVMAMap();
|
|
|
|
ClearPageTable();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VMManager::ClearVMAMap() {
|
|
|
|
vma_map.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VMManager::ClearPageTable() {
|
2018-09-24 14:29:56 +00:00
|
|
|
std::fill(page_table.pointers.begin(), page_table.pointers.end(), nullptr);
|
2018-09-23 00:09:32 +00:00
|
|
|
page_table.special_regions.clear();
|
2018-09-24 14:29:56 +00:00
|
|
|
std::fill(page_table.attributes.begin(), page_table.attributes.end(),
|
|
|
|
Memory::PageType::Unmapped);
|
2018-09-23 00:09:32 +00:00
|
|
|
}
|
|
|
|
|
2018-08-02 16:19:05 +00:00
|
|
|
u64 VMManager::GetTotalMemoryUsage() const {
|
2018-07-02 16:13:26 +00:00
|
|
|
LOG_WARNING(Kernel, "(STUBBED) called");
|
2018-04-15 02:04:10 +00:00
|
|
|
return 0xF8000000;
|
2018-01-01 20:59:31 +00:00
|
|
|
}
|
|
|
|
|
2018-08-02 16:19:05 +00:00
|
|
|
u64 VMManager::GetTotalHeapUsage() const {
|
2018-11-13 16:08:59 +00:00
|
|
|
return heap_used;
|
2018-01-01 20:59:31 +00:00
|
|
|
}
|
|
|
|
|
2018-09-24 15:16:17 +00:00
|
|
|
VAddr VMManager::GetAddressSpaceBaseAddress() const {
|
|
|
|
return address_space_base;
|
|
|
|
}
|
|
|
|
|
|
|
|
VAddr VMManager::GetAddressSpaceEndAddress() const {
|
|
|
|
return address_space_end;
|
2018-01-01 20:59:31 +00:00
|
|
|
}
|
|
|
|
|
2018-08-02 16:19:05 +00:00
|
|
|
u64 VMManager::GetAddressSpaceSize() const {
|
2018-09-24 15:16:17 +00:00
|
|
|
return address_space_end - address_space_base;
|
2018-01-01 20:59:31 +00:00
|
|
|
}
|
|
|
|
|
2018-09-24 14:29:56 +00:00
|
|
|
u64 VMManager::GetAddressSpaceWidth() const {
|
|
|
|
return address_space_width;
|
|
|
|
}
|
|
|
|
|
svc: Clarify enum values for AddressSpaceBaseAddr and AddressSpaceSize in svcGetInfo()
So, one thing that's puzzled me is why the kernel seemed to *not* use
the direct code address ranges in some cases for some service functions.
For example, in svcMapMemory, the full address space width is compared
against for validity, but for svcMapSharedMemory, it compares against
0xFFE00000, 0xFF8000000, and 0x7FF8000000 as upper bounds, and uses
either 0x200000 or 0x8000000 as the lower-bounds as the beginning of the
compared range. Coincidentally, these exact same values are also used in
svcGetInfo, and also when initializing the user address space, so this
is actually retrieving the ASLR extents, not the extents of the address
space in general.
2018-10-14 18:44:38 +00:00
|
|
|
VAddr VMManager::GetASLRRegionBaseAddress() const {
|
|
|
|
return aslr_region_base;
|
|
|
|
}
|
|
|
|
|
|
|
|
VAddr VMManager::GetASLRRegionEndAddress() const {
|
|
|
|
return aslr_region_end;
|
|
|
|
}
|
|
|
|
|
|
|
|
u64 VMManager::GetASLRRegionSize() const {
|
|
|
|
return aslr_region_end - aslr_region_base;
|
|
|
|
}
|
|
|
|
|
2018-10-18 02:39:21 +00:00
|
|
|
bool VMManager::IsWithinASLRRegion(VAddr begin, u64 size) const {
|
|
|
|
const VAddr range_end = begin + size;
|
|
|
|
const VAddr aslr_start = GetASLRRegionBaseAddress();
|
|
|
|
const VAddr aslr_end = GetASLRRegionEndAddress();
|
|
|
|
|
|
|
|
if (aslr_start > begin || begin > range_end || range_end - 1 > aslr_end - 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (range_end > heap_region_base && heap_region_end > begin) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (range_end > map_region_base && map_region_end > begin) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-09-23 00:09:32 +00:00
|
|
|
VAddr VMManager::GetCodeRegionBaseAddress() const {
|
|
|
|
return code_region_base;
|
|
|
|
}
|
|
|
|
|
|
|
|
VAddr VMManager::GetCodeRegionEndAddress() const {
|
|
|
|
return code_region_end;
|
|
|
|
}
|
|
|
|
|
|
|
|
u64 VMManager::GetCodeRegionSize() const {
|
|
|
|
return code_region_end - code_region_base;
|
|
|
|
}
|
|
|
|
|
|
|
|
VAddr VMManager::GetHeapRegionBaseAddress() const {
|
|
|
|
return heap_region_base;
|
|
|
|
}
|
|
|
|
|
|
|
|
VAddr VMManager::GetHeapRegionEndAddress() const {
|
|
|
|
return heap_region_end;
|
|
|
|
}
|
|
|
|
|
|
|
|
u64 VMManager::GetHeapRegionSize() const {
|
|
|
|
return heap_region_end - heap_region_base;
|
|
|
|
}
|
|
|
|
|
|
|
|
VAddr VMManager::GetMapRegionBaseAddress() const {
|
|
|
|
return map_region_base;
|
|
|
|
}
|
|
|
|
|
|
|
|
VAddr VMManager::GetMapRegionEndAddress() const {
|
|
|
|
return map_region_end;
|
|
|
|
}
|
|
|
|
|
|
|
|
u64 VMManager::GetMapRegionSize() const {
|
|
|
|
return map_region_end - map_region_base;
|
|
|
|
}
|
|
|
|
|
|
|
|
VAddr VMManager::GetNewMapRegionBaseAddress() const {
|
|
|
|
return new_map_region_base;
|
|
|
|
}
|
|
|
|
|
|
|
|
VAddr VMManager::GetNewMapRegionEndAddress() const {
|
|
|
|
return new_map_region_end;
|
|
|
|
}
|
|
|
|
|
|
|
|
u64 VMManager::GetNewMapRegionSize() const {
|
|
|
|
return new_map_region_end - new_map_region_base;
|
|
|
|
}
|
|
|
|
|
|
|
|
VAddr VMManager::GetTLSIORegionBaseAddress() const {
|
|
|
|
return tls_io_region_base;
|
|
|
|
}
|
|
|
|
|
|
|
|
VAddr VMManager::GetTLSIORegionEndAddress() const {
|
|
|
|
return tls_io_region_end;
|
|
|
|
}
|
|
|
|
|
|
|
|
u64 VMManager::GetTLSIORegionSize() const {
|
|
|
|
return tls_io_region_end - tls_io_region_base;
|
|
|
|
}
|
|
|
|
|
2018-01-01 20:59:31 +00:00
|
|
|
} // namespace Kernel
|