From 773ec47629ccb46681d789667ddca8aea1f2537f Mon Sep 17 00:00:00 2001 From: Weiyi Wang Date: Thu, 25 Oct 2018 10:51:00 -0400 Subject: [PATCH] Kernel: make config_mem and MapSharedPages members of KernelSystem --- src/core/hle/kernel/config_mem.cpp | 8 +++++--- src/core/hle/kernel/config_mem.h | 9 +++++++-- src/core/hle/kernel/kernel.cpp | 4 +--- src/core/hle/kernel/kernel.h | 11 +++++++++++ src/core/hle/kernel/memory.cpp | 18 ++++++++++-------- src/core/hle/kernel/memory.h | 2 -- src/core/hle/kernel/process.cpp | 2 +- src/tests/core/memory/memory.cpp | 4 ++-- 8 files changed, 37 insertions(+), 21 deletions(-) diff --git a/src/core/hle/kernel/config_mem.cpp b/src/core/hle/kernel/config_mem.cpp index 91c044192..58bef4110 100644 --- a/src/core/hle/kernel/config_mem.cpp +++ b/src/core/hle/kernel/config_mem.cpp @@ -9,9 +9,7 @@ namespace ConfigMem { -ConfigMemDef config_mem; - -void Init() { +Handler::Handler() { std::memset(&config_mem, 0, sizeof(config_mem)); // Values extracted from firmware 11.2.0-35E @@ -28,4 +26,8 @@ void Init() { config_mem.firm_ctr_sdk_ver = 0x0000F297; } +ConfigMemDef& Handler::GetConfigMem() { + return config_mem; +} + } // namespace ConfigMem diff --git a/src/core/hle/kernel/config_mem.h b/src/core/hle/kernel/config_mem.h index 1840d1760..ecb97c6bd 100644 --- a/src/core/hle/kernel/config_mem.h +++ b/src/core/hle/kernel/config_mem.h @@ -49,8 +49,13 @@ struct ConfigMemDef { static_assert(sizeof(ConfigMemDef) == Memory::CONFIG_MEMORY_SIZE, "Config Memory structure size is wrong"); -extern ConfigMemDef config_mem; +class Handler { +public: + Handler(); + ConfigMemDef& GetConfigMem(); -void Init(); +private: + ConfigMemDef config_mem; +}; } // namespace ConfigMem diff --git a/src/core/hle/kernel/kernel.cpp b/src/core/hle/kernel/kernel.cpp index 74b1f596a..e491dfa50 100644 --- a/src/core/hle/kernel/kernel.cpp +++ b/src/core/hle/kernel/kernel.cpp @@ -16,9 +16,7 @@ namespace Kernel { /// Initialize the kernel KernelSystem::KernelSystem(u32 system_mode) { - ConfigMem::Init(); - - Kernel::MemoryInit(system_mode); + MemoryInit(system_mode); resource_limits = std::make_unique(*this); thread_manager = std::make_unique(); diff --git a/src/core/hle/kernel/kernel.h b/src/core/hle/kernel/kernel.h index 5d27b2df7..e001fa8b7 100644 --- a/src/core/hle/kernel/kernel.h +++ b/src/core/hle/kernel/kernel.h @@ -12,6 +12,10 @@ #include "common/common_types.h" #include "core/hle/result.h" +namespace ConfigMem { +class Handler; +} + namespace Kernel { class AddressArbiter; @@ -30,6 +34,7 @@ class ResourceLimitList; class SharedMemory; class ThreadManager; class TimerManager; +class VMManager; enum class ResetType { OneShot, @@ -195,7 +200,11 @@ public: TimerManager& GetTimerManager(); const TimerManager& GetTimerManager() const; + void MapSharedPages(VMManager& address_space); + private: + void MemoryInit(u32 mem_type); + std::unique_ptr resource_limits; std::atomic next_object_id{0}; @@ -210,6 +219,8 @@ private: std::unique_ptr thread_manager; std::unique_ptr timer_manager; + + std::unique_ptr config_mem_handler; }; } // namespace Kernel diff --git a/src/core/hle/kernel/memory.cpp b/src/core/hle/kernel/memory.cpp index ff4c06cab..e8c326569 100644 --- a/src/core/hle/kernel/memory.cpp +++ b/src/core/hle/kernel/memory.cpp @@ -41,7 +41,7 @@ static const u32 memory_region_sizes[8][3] = { {0x0B200000, 0x02E00000, 0x02000000}, // 7 }; -void MemoryInit(u32 mem_type) { +void KernelSystem::MemoryInit(u32 mem_type) { // TODO(yuriks): On the n3DS, all o3DS configurations (<=5) are forced to 6 instead. ASSERT_MSG(mem_type <= 5, "New 3DS memory configuration aren't supported yet!"); ASSERT(mem_type != 1); @@ -64,7 +64,8 @@ void MemoryInit(u32 mem_type) { // We must've allocated the entire FCRAM by the end ASSERT(base == Memory::FCRAM_SIZE); - using ConfigMem::config_mem; + config_mem_handler = std::make_unique(); + auto& config_mem = config_mem_handler->GetConfigMem(); config_mem.app_mem_type = mem_type; // app_mem_malloc does not always match the configured size for memory_region[0]: in case the // n3DS type override is in effect it reports the size the game expects, not the real one. @@ -152,12 +153,13 @@ void HandleSpecialMapping(VMManager& address_space, const AddressMapping& mappin mapping.read_only ? VMAPermission::Read : VMAPermission::ReadWrite); } -void MapSharedPages(VMManager& address_space) { - auto cfg_mem_vma = address_space - .MapBackingMemory(Memory::CONFIG_MEMORY_VADDR, - reinterpret_cast(&ConfigMem::config_mem), - Memory::CONFIG_MEMORY_SIZE, MemoryState::Shared) - .Unwrap(); +void KernelSystem::MapSharedPages(VMManager& address_space) { + auto cfg_mem_vma = + address_space + .MapBackingMemory(Memory::CONFIG_MEMORY_VADDR, + reinterpret_cast(&config_mem_handler->GetConfigMem()), + Memory::CONFIG_MEMORY_SIZE, MemoryState::Shared) + .Unwrap(); address_space.Reprotect(cfg_mem_vma, VMAPermission::Read); auto shared_page_vma = diff --git a/src/core/hle/kernel/memory.h b/src/core/hle/kernel/memory.h index da6bb3563..a39a53977 100644 --- a/src/core/hle/kernel/memory.h +++ b/src/core/hle/kernel/memory.h @@ -20,12 +20,10 @@ struct MemoryRegionInfo { std::shared_ptr> linear_heap_memory; }; -void MemoryInit(u32 mem_type); void MemoryShutdown(); MemoryRegionInfo* GetMemoryRegion(MemoryRegion region); void HandleSpecialMapping(VMManager& address_space, const AddressMapping& mapping); -void MapSharedPages(VMManager& address_space); extern MemoryRegionInfo memory_regions[3]; } // namespace Kernel diff --git a/src/core/hle/kernel/process.cpp b/src/core/hle/kernel/process.cpp index 21204eeb8..9b45c77f1 100644 --- a/src/core/hle/kernel/process.cpp +++ b/src/core/hle/kernel/process.cpp @@ -143,7 +143,7 @@ void Process::Run(s32 main_thread_priority, u32 stack_size) { memory_region->used += stack_size; // Map special address mappings - MapSharedPages(vm_manager); + kernel.MapSharedPages(vm_manager); for (const auto& mapping : address_mappings) { HandleSpecialMapping(vm_manager, mapping); } diff --git a/src/tests/core/memory/memory.cpp b/src/tests/core/memory/memory.cpp index d5f96e1f2..de1848007 100644 --- a/src/tests/core/memory/memory.cpp +++ b/src/tests/core/memory/memory.cpp @@ -25,7 +25,7 @@ TEST_CASE("Memory::IsValidVirtualAddress", "[core][memory]") { SECTION("CONFIG_MEMORY_VADDR and SHARED_PAGE_VADDR should be valid after mapping them") { auto process = kernel.CreateProcess(kernel.CreateCodeSet("", 0)); - Kernel::MapSharedPages(process->vm_manager); + kernel.MapSharedPages(process->vm_manager); CHECK(Memory::IsValidVirtualAddress(*process, Memory::CONFIG_MEMORY_VADDR) == true); CHECK(Memory::IsValidVirtualAddress(*process, Memory::SHARED_PAGE_VADDR) == true); } @@ -47,7 +47,7 @@ TEST_CASE("Memory::IsValidVirtualAddress", "[core][memory]") { SECTION("Unmapping a VAddr should make it invalid") { auto process = kernel.CreateProcess(kernel.CreateCodeSet("", 0)); - Kernel::MapSharedPages(process->vm_manager); + kernel.MapSharedPages(process->vm_manager); process->vm_manager.UnmapRange(Memory::CONFIG_MEMORY_VADDR, Memory::CONFIG_MEMORY_SIZE); CHECK(Memory::IsValidVirtualAddress(*process, Memory::CONFIG_MEMORY_VADDR) == false); }