From 0cbcd6ec9aeeafc298fe2e6e4ac10d68bb7267c5 Mon Sep 17 00:00:00 2001 From: Lioncash <mathew1800@gmail.com> Date: Tue, 28 Aug 2018 12:30:33 -0400 Subject: kernel: Eliminate kernel global state As means to pave the way for getting rid of global state within core, This eliminates kernel global state by removing all globals. Instead this introduces a KernelCore class which acts as a kernel instance. This instance lives in the System class, which keeps its lifetime contained to the lifetime of the System class. This also forces the kernel types to actually interact with the main kernel instance itself instead of having transient kernel state placed all over several translation units, keeping everything together. It also has a nice consequence of making dependencies much more explicit. This also makes our initialization a tad bit more correct. Previously we were creating a kernel process before the actual kernel was initialized, which doesn't really make much sense. The KernelCore class itself follows the PImpl idiom, which allows keeping all the implementation details sealed away from everything else, which forces the use of the exposed API and allows us to avoid any unnecessary inclusions within the main kernel header. --- src/core/hle/kernel/process.cpp | 40 +++++++++++----------------------------- 1 file changed, 11 insertions(+), 29 deletions(-) (limited to 'src/core/hle/kernel/process.cpp') diff --git a/src/core/hle/kernel/process.cpp b/src/core/hle/kernel/process.cpp index edf34c5a3b..b025e323ff 100644 --- a/src/core/hle/kernel/process.cpp +++ b/src/core/hle/kernel/process.cpp @@ -8,6 +8,7 @@ #include "common/common_funcs.h" #include "common/logging/log.h" #include "core/hle/kernel/errors.h" +#include "core/hle/kernel/kernel.h" #include "core/hle/kernel/process.h" #include "core/hle/kernel/resource_limit.h" #include "core/hle/kernel/thread.h" @@ -16,30 +17,26 @@ namespace Kernel { -// Lists all processes that exist in the current session. -static std::vector<SharedPtr<Process>> process_list; - -SharedPtr<CodeSet> CodeSet::Create(std::string name) { - SharedPtr<CodeSet> codeset(new CodeSet); +SharedPtr<CodeSet> CodeSet::Create(KernelCore& kernel, std::string name) { + SharedPtr<CodeSet> codeset(new CodeSet(kernel)); codeset->name = std::move(name); return codeset; } -CodeSet::CodeSet() {} -CodeSet::~CodeSet() {} - -u32 Process::next_process_id; +CodeSet::CodeSet(KernelCore& kernel) : Object{kernel} {} +CodeSet::~CodeSet() = default; -SharedPtr<Process> Process::Create(std::string&& name) { - SharedPtr<Process> process(new Process); +SharedPtr<Process> Process::Create(KernelCore& kernel, std::string&& name) { + SharedPtr<Process> process(new Process(kernel)); process->name = std::move(name); process->flags.raw = 0; process->flags.memory_region.Assign(MemoryRegion::APPLICATION); process->status = ProcessStatus::Created; process->program_id = 0; + process->process_id = kernel.CreateNewProcessID(); - process_list.push_back(process); + kernel.AppendNewProcess(process); return process; } @@ -128,7 +125,7 @@ void Process::Run(VAddr entry_point, s32 main_thread_priority, u32 stack_size) { vm_manager.LogLayout(); status = ProcessStatus::Running; - Kernel::SetupMainThread(entry_point, main_thread_priority, this); + Kernel::SetupMainThread(kernel, entry_point, main_thread_priority, this); } void Process::LoadModule(SharedPtr<CodeSet> module_, VAddr base_addr) { @@ -231,22 +228,7 @@ ResultCode Process::UnmapMemory(VAddr dst_addr, VAddr /*src_addr*/, u64 size) { return vm_manager.UnmapRange(dst_addr, size); } -Kernel::Process::Process() {} +Kernel::Process::Process(KernelCore& kernel) : Object{kernel} {} Kernel::Process::~Process() {} -void ClearProcessList() { - process_list.clear(); -} - -SharedPtr<Process> GetProcessById(u32 process_id) { - auto itr = std::find_if( - process_list.begin(), process_list.end(), - [&](const SharedPtr<Process>& process) { return process->process_id == process_id; }); - - if (itr == process_list.end()) - return nullptr; - - return *itr; -} - } // namespace Kernel -- cgit v1.2.3-70-g09d2