From 213b259cf18396c3bd80ba167f0bace0c128e2e8 Mon Sep 17 00:00:00 2001 From: Weiyi Wang Date: Fri, 12 Oct 2018 15:21:32 -0400 Subject: [PATCH] kernel: pass ref in CodeSet --- src/core/hle/kernel/kernel.h | 3 ++ src/core/hle/kernel/process.cpp | 6 ++-- src/core/hle/kernel/process.h | 6 ++-- src/core/loader/3dsx.cpp | 2 +- src/core/loader/elf.cpp | 3 +- src/core/loader/ncch.cpp | 3 +- src/tests/core/arm/arm_test_common.cpp | 8 +++++- src/tests/core/arm/arm_test_common.h | 4 ++- src/tests/core/hle/kernel/hle_ipc.cpp | 38 ++++++++++++++++---------- src/tests/core/memory/memory.cpp | 13 ++++++--- 10 files changed, 56 insertions(+), 30 deletions(-) diff --git a/src/core/hle/kernel/kernel.h b/src/core/hle/kernel/kernel.h index 28bad6efa..0717eb225 100644 --- a/src/core/hle/kernel/kernel.h +++ b/src/core/hle/kernel/kernel.h @@ -13,6 +13,7 @@ namespace Kernel { class AddressArbiter; class Event; class Mutex; +class CodeSet; enum class ResetType { OneShot, @@ -50,6 +51,8 @@ public: * @return Pointer to new Mutex object */ SharedPtr CreateMutex(bool initial_locked, std::string name = "Unknown"); + + SharedPtr CreateCodeSet(std::string name, u64 program_id); }; } // namespace Kernel diff --git a/src/core/hle/kernel/process.cpp b/src/core/hle/kernel/process.cpp index 8f3efe5d9..ac0908913 100644 --- a/src/core/hle/kernel/process.cpp +++ b/src/core/hle/kernel/process.cpp @@ -20,8 +20,8 @@ namespace Kernel { // Lists all processes that exist in the current session. static std::vector> process_list; -SharedPtr CodeSet::Create(std::string name, u64 program_id) { - SharedPtr codeset(new CodeSet); +SharedPtr KernelSystem::CreateCodeSet(std::string name, u64 program_id) { + SharedPtr codeset(new CodeSet(*this)); codeset->name = std::move(name); codeset->program_id = program_id; @@ -29,7 +29,7 @@ SharedPtr CodeSet::Create(std::string name, u64 program_id) { return codeset; } -CodeSet::CodeSet() {} +CodeSet::CodeSet(KernelSystem& system) {} CodeSet::~CodeSet() {} u32 Process::next_process_id; diff --git a/src/core/hle/kernel/process.h b/src/core/hle/kernel/process.h index 1081be063..3d6979598 100644 --- a/src/core/hle/kernel/process.h +++ b/src/core/hle/kernel/process.h @@ -62,8 +62,6 @@ struct CodeSet final : public Object { u32 size = 0; }; - static SharedPtr Create(std::string name, u64 program_id); - std::string GetTypeName() const override { return "CodeSet"; } @@ -111,8 +109,10 @@ struct CodeSet final : public Object { u64 program_id; private: - CodeSet(); + explicit CodeSet(KernelSystem& kernel); ~CodeSet() override; + + friend class KernelSystem; }; class Process final : public Object { diff --git a/src/core/loader/3dsx.cpp b/src/core/loader/3dsx.cpp index d791c085c..20adb1ac4 100644 --- a/src/core/loader/3dsx.cpp +++ b/src/core/loader/3dsx.cpp @@ -217,7 +217,7 @@ static THREEDSX_Error Load3DSXFile(FileUtil::IOFile& file, u32 base_addr, } // Create the CodeSet - SharedPtr code_set = CodeSet::Create("", 0); + SharedPtr code_set = Core::System::GetInstance().Kernel().CreateCodeSet("", 0); code_set->CodeSegment().offset = loadinfo.seg_ptrs[0] - program_image.data(); code_set->CodeSegment().addr = loadinfo.seg_addrs[0]; diff --git a/src/core/loader/elf.cpp b/src/core/loader/elf.cpp index fd2bb23b3..9064abff1 100644 --- a/src/core/loader/elf.cpp +++ b/src/core/loader/elf.cpp @@ -8,6 +8,7 @@ #include "common/common_types.h" #include "common/file_util.h" #include "common/logging/log.h" +#include "core/core.h" #include "core/hle/kernel/process.h" #include "core/hle/kernel/resource_limit.h" #include "core/loader/elf.h" @@ -299,7 +300,7 @@ SharedPtr ElfReader::LoadInto(u32 vaddr) { std::vector program_image(total_image_size); std::size_t current_image_position = 0; - SharedPtr codeset = CodeSet::Create("", 0); + SharedPtr codeset = Core::System::GetInstance().Kernel().CreateCodeSet("", 0); for (unsigned int i = 0; i < header->e_phnum; ++i) { Elf32_Phdr* p = &segments[i]; diff --git a/src/core/loader/ncch.cpp b/src/core/loader/ncch.cpp index 9fa1d05f1..c609eb9bd 100644 --- a/src/core/loader/ncch.cpp +++ b/src/core/loader/ncch.cpp @@ -75,7 +75,8 @@ ResultStatus AppLoader_NCCH::LoadExec(Kernel::SharedPtr& proces std::string process_name = Common::StringFromFixedZeroTerminatedBuffer( (const char*)overlay_ncch->exheader_header.codeset_info.name, 8); - SharedPtr codeset = CodeSet::Create(process_name, program_id); + SharedPtr codeset = + Core::System::GetInstance().Kernel().CreateCodeSet(process_name, program_id); codeset->CodeSegment().offset = 0; codeset->CodeSegment().addr = overlay_ncch->exheader_header.codeset_info.text.address; diff --git a/src/tests/core/arm/arm_test_common.cpp b/src/tests/core/arm/arm_test_common.cpp index 23c0411e7..fd6f43aff 100644 --- a/src/tests/core/arm/arm_test_common.cpp +++ b/src/tests/core/arm/arm_test_common.cpp @@ -3,6 +3,7 @@ // Refer to the license.txt file included. #include "core/core.h" +#include "core/core_timing.h" #include "core/hle/kernel/process.h" #include "core/memory.h" #include "core/memory_setup.h" @@ -15,7 +16,10 @@ static Memory::PageTable* page_table = nullptr; TestEnvironment::TestEnvironment(bool mutable_memory_) : mutable_memory(mutable_memory_), test_memory(std::make_shared(this)) { - Kernel::g_current_process = Kernel::Process::Create(Kernel::CodeSet::Create("", 0)); + CoreTiming::Init(); + kernel = std::make_unique(0); + + Kernel::g_current_process = Kernel::Process::Create(kernel->CreateCodeSet("", 0)); page_table = &Kernel::g_current_process->vm_manager.page_table; page_table->pointers.fill(nullptr); @@ -30,6 +34,8 @@ TestEnvironment::TestEnvironment(bool mutable_memory_) TestEnvironment::~TestEnvironment() { Memory::UnmapRegion(*page_table, 0x80000000, 0x80000000); Memory::UnmapRegion(*page_table, 0x00000000, 0x80000000); + + CoreTiming::Shutdown(); } void TestEnvironment::SetMemory64(VAddr vaddr, u64 value) { diff --git a/src/tests/core/arm/arm_test_common.h b/src/tests/core/arm/arm_test_common.h index 44c63818a..4f396416f 100644 --- a/src/tests/core/arm/arm_test_common.h +++ b/src/tests/core/arm/arm_test_common.h @@ -5,8 +5,8 @@ #include #include #include - #include "common/common_types.h" +#include "core/hle/kernel/kernel.h" #include "core/mmio.h" namespace ArmTests { @@ -79,6 +79,8 @@ private: bool mutable_memory; std::shared_ptr test_memory; std::vector write_records; + + std::unique_ptr kernel; }; } // namespace ArmTests diff --git a/src/tests/core/hle/kernel/hle_ipc.cpp b/src/tests/core/hle/kernel/hle_ipc.cpp index f9b705f20..d9596c397 100644 --- a/src/tests/core/hle/kernel/hle_ipc.cpp +++ b/src/tests/core/hle/kernel/hle_ipc.cpp @@ -3,6 +3,7 @@ // Refer to the license.txt file included. #include +#include "core/core_timing.h" #include "core/hle/ipc.h" #include "core/hle/kernel/client_port.h" #include "core/hle/kernel/client_session.h" @@ -14,16 +15,17 @@ namespace Kernel { -static SharedPtr MakeObject() { - static Kernel::KernelSystem kernel(0); +static SharedPtr MakeObject(Kernel::KernelSystem& kernel) { return kernel.CreateEvent(ResetType::OneShot); } TEST_CASE("HLERequestContext::PopulateFromIncomingCommandBuffer", "[core][kernel]") { + CoreTiming::Init(); + Kernel::KernelSystem kernel(0); auto session = std::get>(ServerSession::CreateSessionPair()); HLERequestContext context(std::move(session)); - auto process = Process::Create(CodeSet::Create("", 0)); + auto process = Process::Create(kernel.CreateCodeSet("", 0)); HandleTable handle_table; SECTION("works with empty cmdbuf") { @@ -53,7 +55,7 @@ TEST_CASE("HLERequestContext::PopulateFromIncomingCommandBuffer", "[core][kernel } SECTION("translates move handles") { - auto a = MakeObject(); + auto a = MakeObject(kernel); Handle a_handle = handle_table.Create(a).Unwrap(); const u32_le input[]{ IPC::MakeHeader(0, 0, 2), @@ -69,7 +71,7 @@ TEST_CASE("HLERequestContext::PopulateFromIncomingCommandBuffer", "[core][kernel } SECTION("translates copy handles") { - auto a = MakeObject(); + auto a = MakeObject(kernel); Handle a_handle = handle_table.Create(a).Unwrap(); const u32_le input[]{ IPC::MakeHeader(0, 0, 2), @@ -85,9 +87,9 @@ TEST_CASE("HLERequestContext::PopulateFromIncomingCommandBuffer", "[core][kernel } SECTION("translates multi-handle descriptors") { - auto a = MakeObject(); - auto b = MakeObject(); - auto c = MakeObject(); + auto a = MakeObject(kernel); + auto b = MakeObject(kernel); + auto c = MakeObject(kernel); const u32_le input[]{ IPC::MakeHeader(0, 0, 5), IPC::MoveHandleDesc(2), handle_table.Create(a).Unwrap(), handle_table.Create(b).Unwrap(), @@ -191,7 +193,7 @@ TEST_CASE("HLERequestContext::PopulateFromIncomingCommandBuffer", "[core][kernel buffer_mapped->size(), MemoryState::Private); REQUIRE(result.Code() == RESULT_SUCCESS); - auto a = MakeObject(); + auto a = MakeObject(kernel); const u32_le input[]{ IPC::MakeHeader(0, 2, 8), 0x12345678, @@ -223,13 +225,17 @@ TEST_CASE("HLERequestContext::PopulateFromIncomingCommandBuffer", "[core][kernel REQUIRE(process->vm_manager.UnmapRange(target_address_mapped, buffer_mapped->size()) == RESULT_SUCCESS); } + + CoreTiming::Shutdown(); } TEST_CASE("HLERequestContext::WriteToOutgoingCommandBuffer", "[core][kernel]") { + CoreTiming::Init(); + Kernel::KernelSystem kernel(0); auto session = std::get>(ServerSession::CreateSessionPair()); HLERequestContext context(std::move(session)); - auto process = Process::Create(CodeSet::Create("", 0)); + auto process = Process::Create(kernel.CreateCodeSet("", 0)); HandleTable handle_table; auto* input = context.CommandBuffer(); u32_le output[IPC::COMMAND_BUFFER_LENGTH]; @@ -256,8 +262,8 @@ TEST_CASE("HLERequestContext::WriteToOutgoingCommandBuffer", "[core][kernel]") { } SECTION("translates move/copy handles") { - auto a = MakeObject(); - auto b = MakeObject(); + auto a = MakeObject(kernel); + auto b = MakeObject(kernel); input[0] = IPC::MakeHeader(0, 0, 4); input[1] = IPC::MoveHandleDesc(1); input[2] = context.AddOutgoingHandle(a); @@ -282,9 +288,9 @@ TEST_CASE("HLERequestContext::WriteToOutgoingCommandBuffer", "[core][kernel]") { } SECTION("translates multi-handle descriptors") { - auto a = MakeObject(); - auto b = MakeObject(); - auto c = MakeObject(); + auto a = MakeObject(kernel); + auto b = MakeObject(kernel); + auto c = MakeObject(kernel); input[0] = IPC::MakeHeader(0, 0, 5); input[1] = IPC::MoveHandleDesc(2); input[2] = context.AddOutgoingHandle(a); @@ -362,6 +368,8 @@ TEST_CASE("HLERequestContext::WriteToOutgoingCommandBuffer", "[core][kernel]") { REQUIRE(process->vm_manager.UnmapRange(target_address, output_buffer->size()) == RESULT_SUCCESS); } + + CoreTiming::Shutdown(); } } // namespace Kernel diff --git a/src/tests/core/memory/memory.cpp b/src/tests/core/memory/memory.cpp index c69340865..89dcd9c9a 100644 --- a/src/tests/core/memory/memory.cpp +++ b/src/tests/core/memory/memory.cpp @@ -3,14 +3,17 @@ // Refer to the license.txt file included. #include +#include "core/core_timing.h" #include "core/hle/kernel/memory.h" #include "core/hle/kernel/process.h" #include "core/hle/shared_page.h" #include "core/memory.h" TEST_CASE("Memory::IsValidVirtualAddress", "[core][memory]") { + CoreTiming::Init(); + Kernel::KernelSystem kernel(0); SECTION("these regions should not be mapped on an empty process") { - auto process = Kernel::Process::Create(Kernel::CodeSet::Create("", 0)); + auto process = Kernel::Process::Create(kernel.CreateCodeSet("", 0)); CHECK(Memory::IsValidVirtualAddress(*process, Memory::PROCESS_IMAGE_VADDR) == false); CHECK(Memory::IsValidVirtualAddress(*process, Memory::HEAP_VADDR) == false); CHECK(Memory::IsValidVirtualAddress(*process, Memory::LINEAR_HEAP_VADDR) == false); @@ -21,14 +24,14 @@ TEST_CASE("Memory::IsValidVirtualAddress", "[core][memory]") { } SECTION("CONFIG_MEMORY_VADDR and SHARED_PAGE_VADDR should be valid after mapping them") { - auto process = Kernel::Process::Create(Kernel::CodeSet::Create("", 0)); + auto process = Kernel::Process::Create(kernel.CreateCodeSet("", 0)); Kernel::MapSharedPages(process->vm_manager); CHECK(Memory::IsValidVirtualAddress(*process, Memory::CONFIG_MEMORY_VADDR) == true); CHECK(Memory::IsValidVirtualAddress(*process, Memory::SHARED_PAGE_VADDR) == true); } SECTION("special regions should be valid after mapping them") { - auto process = Kernel::Process::Create(Kernel::CodeSet::Create("", 0)); + auto process = Kernel::Process::Create(kernel.CreateCodeSet("", 0)); SECTION("VRAM") { Kernel::HandleSpecialMapping(process->vm_manager, {Memory::VRAM_VADDR, Memory::VRAM_SIZE, false, false}); @@ -43,9 +46,11 @@ TEST_CASE("Memory::IsValidVirtualAddress", "[core][memory]") { } SECTION("Unmapping a VAddr should make it invalid") { - auto process = Kernel::Process::Create(Kernel::CodeSet::Create("", 0)); + auto process = Kernel::Process::Create(kernel.CreateCodeSet("", 0)); 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); } + + CoreTiming::Shutdown(); }