From 09c1aa44d8719d8423c22a0c66e67f609b7d0b41 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Wed, 27 Sep 2023 00:29:45 +0300 Subject: [PATCH] Kernel: Allow creationg of empty processes and manual registration You can now create kernel processes without any threads, add the needed threads and only then register the process and its threads to the scheduler. --- kernel/include/kernel/Process.h | 3 ++- kernel/kernel/Process.cpp | 20 +++++++++++++------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/kernel/include/kernel/Process.h b/kernel/include/kernel/Process.h index e53c2e1c..532e9613 100644 --- a/kernel/include/kernel/Process.h +++ b/kernel/include/kernel/Process.h @@ -38,11 +38,13 @@ namespace Kernel }; public: + static Process* create_kernel(); static Process* create_kernel(entry_t, void*); static BAN::ErrorOr create_userspace(const Credentials&, BAN::StringView); ~Process(); void cleanup_function(); + void register_to_scheduler(); void exit(int status, int signal); static void for_each_process(const BAN::Function& callback); @@ -138,7 +140,6 @@ namespace Kernel private: Process(const Credentials&, pid_t pid, pid_t parent, pid_t sid, pid_t pgrp); static Process* create_process(const Credentials&, pid_t parent, pid_t sid = 0, pid_t pgrp = 0); - static void register_process(Process*); // Load an elf file to virtual address space of the current page table static BAN::ErrorOr> load_elf_for_exec(const Credentials&, BAN::StringView file_path, const BAN::String& cwd); diff --git a/kernel/kernel/Process.cpp b/kernel/kernel/Process.cpp index 35474a7c..64334ffb 100644 --- a/kernel/kernel/Process.cpp +++ b/kernel/kernel/Process.cpp @@ -78,22 +78,29 @@ namespace Kernel return process; } - void Process::register_process(Process* process) + void Process::register_to_scheduler() { s_process_lock.lock(); - MUST(s_processes.push_back(process)); + MUST(s_processes.push_back(this)); s_process_lock.unlock(); - for (auto* thread : process->m_threads) + for (auto* thread : m_threads) MUST(Scheduler::get().add_thread(thread)); } + Process* Process::create_kernel() + { + auto* process = create_process({ 0, 0, 0, 0 }, 0); + MUST(process->m_working_directory.push_back('/')); + return process; + } + Process* Process::create_kernel(entry_t entry, void* data) { auto* process = create_process({ 0, 0, 0, 0 }, 0); MUST(process->m_working_directory.push_back('/')); auto* thread = MUST(Thread::create_kernel(entry, data, process)); process->add_thread(thread); - register_process(process); + process->register_to_scheduler(); return process; } @@ -146,7 +153,7 @@ namespace Kernel auto* thread = MUST(Thread::create_userspace(process)); process->add_thread(thread); - register_process(process); + process->register_to_scheduler(); return process; } @@ -353,8 +360,7 @@ namespace Kernel // FIXME: this should be able to fail Thread* thread = MUST(Thread::current().clone(forked, rsp, rip)); forked->add_thread(thread); - - register_process(forked); + forked->register_to_scheduler(); return forked->pid(); }