diff --git a/src/core/arm/dynarmic/arm_dynarmic.cpp b/src/core/arm/dynarmic/arm_dynarmic.cpp index 7be5a38de5..928bbc0969 100644 --- a/src/core/arm/dynarmic/arm_dynarmic.cpp +++ b/src/core/arm/dynarmic/arm_dynarmic.cpp @@ -129,7 +129,8 @@ public: }; std::unique_ptr ARM_Dynarmic::MakeJit() const { - auto** const page_table = Core::CurrentProcess()->vm_manager.page_table.pointers.data(); + auto& current_process = Core::CurrentProcess(); + auto** const page_table = current_process->vm_manager.page_table.pointers.data(); Dynarmic::A64::UserConfig config; @@ -138,7 +139,7 @@ std::unique_ptr ARM_Dynarmic::MakeJit() const { // Memory config.page_table = reinterpret_cast(page_table); - config.page_table_address_space_bits = Memory::ADDRESS_SPACE_BITS; + config.page_table_address_space_bits = current_process->vm_manager.GetAddressSpaceWidth(); config.silently_mirror_page_table = false; // Multi-process state diff --git a/src/core/hle/kernel/vm_manager.cpp b/src/core/hle/kernel/vm_manager.cpp index 337f17b7bf..20d06f0007 100644 --- a/src/core/hle/kernel/vm_manager.cpp +++ b/src/core/hle/kernel/vm_manager.cpp @@ -66,18 +66,21 @@ VMManager::~VMManager() { void VMManager::Reset(FileSys::ProgramAddressSpaceType type) { Clear(); + InitializeMemoryRegionRanges(type); + page_table.Resize(address_space_width); + // Initialize the map with a single free region covering the entire managed space. VirtualMemoryArea initial_vma; - initial_vma.size = MAX_ADDRESS; + initial_vma.size = address_space_end; vma_map.emplace(initial_vma.base, initial_vma); UpdatePageTableForVMA(initial_vma); } VMManager::VMAHandle VMManager::FindVMA(VAddr target) const { - if (target >= MAX_ADDRESS) { + if (target >= address_space_end) { return vma_map.end(); } else { return std::prev(vma_map.upper_bound(target)); @@ -291,7 +294,7 @@ ResultVal VMManager::CarveVMARange(VAddr target, u64 size) { const VAddr target_end = target + size; ASSERT(target_end >= target); - ASSERT(target_end <= MAX_ADDRESS); + ASSERT(target_end <= address_space_end); ASSERT(size > 0); VMAIter begin_vma = StripIterConstness(FindVMA(target)); @@ -455,9 +458,10 @@ void VMManager::ClearVMAMap() { } void VMManager::ClearPageTable() { - page_table.pointers.fill(nullptr); + std::fill(page_table.pointers.begin(), page_table.pointers.end(), nullptr); page_table.special_regions.clear(); - page_table.attributes.fill(Memory::PageType::Unmapped); + std::fill(page_table.attributes.begin(), page_table.attributes.end(), + Memory::PageType::Unmapped); } u64 VMManager::GetTotalMemoryUsage() const { @@ -480,6 +484,10 @@ u64 VMManager::GetAddressSpaceSize() const { return MAX_ADDRESS; } +u64 VMManager::GetAddressSpaceWidth() const { + return address_space_width; +} + VAddr VMManager::GetCodeRegionBaseAddress() const { return code_region_base; } diff --git a/src/core/hle/kernel/vm_manager.h b/src/core/hle/kernel/vm_manager.h index 0ce240126d..581bf3d009 100644 --- a/src/core/hle/kernel/vm_manager.h +++ b/src/core/hle/kernel/vm_manager.h @@ -205,6 +205,9 @@ public: /// Gets the total address space address size, used by svcGetInfo u64 GetAddressSpaceSize() const; + /// Gets the address space width in bits. + u64 GetAddressSpaceWidth() const; + /// Gets the base address of the code region. VAddr GetCodeRegionBaseAddress() const; diff --git a/src/core/memory.cpp b/src/core/memory.cpp index 316b468201..674ef08291 100644 --- a/src/core/memory.cpp +++ b/src/core/memory.cpp @@ -3,7 +3,6 @@ // Refer to the license.txt file included. #include -#include #include #include @@ -41,6 +40,21 @@ PageTable* GetCurrentPageTable() { return current_page_table; } +PageTable::PageTable() = default; + +PageTable::PageTable(std::size_t address_space_width_in_bits) { + Resize(address_space_width_in_bits); +} + +PageTable::~PageTable() = default; + +void PageTable::Resize(std::size_t address_space_width_in_bits) { + const std::size_t num_page_table_entries = 1ULL << (address_space_width_in_bits - PAGE_BITS); + + pointers.resize(num_page_table_entries); + attributes.resize(num_page_table_entries); +} + static void MapPages(PageTable& page_table, VAddr base, u64 size, u8* memory, PageType type) { LOG_DEBUG(HW_Memory, "Mapping {} onto {:016X}-{:016X}", fmt::ptr(memory), base * PAGE_SIZE, (base + size) * PAGE_SIZE); @@ -50,7 +64,7 @@ static void MapPages(PageTable& page_table, VAddr base, u64 size, u8* memory, Pa VAddr end = base + size; while (base != end) { - ASSERT_MSG(base < PAGE_TABLE_NUM_ENTRIES, "out of range mapping at {:016X}", base); + ASSERT_MSG(base < page_table.pointers.size(), "out of range mapping at {:016X}", base); page_table.attributes[base] = type; page_table.pointers[base] = memory; diff --git a/src/core/memory.h b/src/core/memory.h index 2a27c02518..739e5be94d 100644 --- a/src/core/memory.h +++ b/src/core/memory.h @@ -4,10 +4,10 @@ #pragma once -#include #include #include #include +#include #include #include "common/common_types.h" #include "core/memory_hook.h" @@ -23,10 +23,8 @@ namespace Memory { * be mapped. */ constexpr std::size_t PAGE_BITS = 12; -constexpr u64 PAGE_SIZE = 1 << PAGE_BITS; +constexpr u64 PAGE_SIZE = 1ULL << PAGE_BITS; constexpr u64 PAGE_MASK = PAGE_SIZE - 1; -constexpr std::size_t ADDRESS_SPACE_BITS = 36; -constexpr std::size_t PAGE_TABLE_NUM_ENTRIES = 1ULL << (ADDRESS_SPACE_BITS - PAGE_BITS); enum class PageType : u8 { /// Page is unmapped and should cause an access error. @@ -62,23 +60,35 @@ struct SpecialRegion { * mimics the way a real CPU page table works. */ struct PageTable { - /** - * Array of memory pointers backing each page. An entry can only be non-null if the - * corresponding entry in the `attributes` array is of type `Memory`. - */ - std::array pointers; + explicit PageTable(); + explicit PageTable(std::size_t address_space_width_in_bits); + ~PageTable(); /** - * Contains MMIO handlers that back memory regions whose entries in the `attribute` array is of - * type `Special`. + * Resizes the page table to be able to accomodate enough pages within + * a given address space. + * + * @param address_space_width_in_bits The address size width in bits. + */ + void Resize(std::size_t address_space_width_in_bits); + + /** + * Vector of memory pointers backing each page. An entry can only be non-null if the + * corresponding entry in the `attributes` vector is of type `Memory`. + */ + std::vector pointers; + + /** + * Contains MMIO handlers that back memory regions whose entries in the `attribute` vector is + * of type `Special`. */ boost::icl::interval_map> special_regions; /** - * Array of fine grained page attributes. If it is set to any value other than `Memory`, then + * Vector of fine grained page attributes. If it is set to any value other than `Memory`, then * the corresponding entry in `pointers` MUST be set to null. */ - std::array attributes; + std::vector attributes; }; /// Virtual user-space memory regions diff --git a/src/tests/core/arm/arm_test_common.cpp b/src/tests/core/arm/arm_test_common.cpp index 7c69fc26e7..c17a122cd1 100644 --- a/src/tests/core/arm/arm_test_common.cpp +++ b/src/tests/core/arm/arm_test_common.cpp @@ -2,6 +2,8 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. +#include + #include "core/core.h" #include "core/hle/kernel/process.h" #include "core/memory.h" @@ -16,9 +18,10 @@ TestEnvironment::TestEnvironment(bool mutable_memory_) Core::CurrentProcess() = Kernel::Process::Create(kernel, ""); page_table = &Core::CurrentProcess()->vm_manager.page_table; - page_table->pointers.fill(nullptr); + std::fill(page_table->pointers.begin(), page_table->pointers.end(), nullptr); page_table->special_regions.clear(); - page_table->attributes.fill(Memory::PageType::Unmapped); + std::fill(page_table->attributes.begin(), page_table->attributes.end(), + Memory::PageType::Unmapped); Memory::MapIoRegion(*page_table, 0x00000000, 0x80000000, test_memory); Memory::MapIoRegion(*page_table, 0x80000000, 0x80000000, test_memory);