From d63716db96e896e53156ca9f82f934c60fef882f Mon Sep 17 00:00:00 2001 From: Bananymous Date: Wed, 19 Apr 2023 00:34:18 +0300 Subject: [PATCH] Kernel: Process is not reference counted any more This was not necessary and it made things needlessly complicated --- LibELF/LibELF/ELF.cpp | 8 +++--- kernel/include/kernel/Process.h | 11 ++++---- kernel/include/kernel/Thread.h | 18 ++++++------- kernel/kernel/Font.cpp | 8 +++--- kernel/kernel/Process.cpp | 24 +++++++++++++---- kernel/kernel/Scheduler.cpp | 10 +++---- kernel/kernel/Shell.cpp | 48 ++++++++++++++++----------------- kernel/kernel/Syscall.cpp | 6 ++--- kernel/kernel/Terminal/TTY.cpp | 4 +-- kernel/kernel/Thread.cpp | 9 ++++--- kernel/kernel/kernel.cpp | 14 +++++++--- 11 files changed, 91 insertions(+), 69 deletions(-) diff --git a/LibELF/LibELF/ELF.cpp b/LibELF/LibELF/ELF.cpp index 390e8bbdb9..f2c7141755 100644 --- a/LibELF/LibELF/ELF.cpp +++ b/LibELF/LibELF/ELF.cpp @@ -15,15 +15,15 @@ namespace LibELF { BAN::Vector data; - int fd = TRY(Kernel::Process::current()->open(file_path, O_RDONLY)); - BAN::ScopeGuard _([fd] { MUST(Kernel::Process::current()->close(fd)); }); + int fd = TRY(Kernel::Process::current().open(file_path, O_RDONLY)); + BAN::ScopeGuard _([fd] { MUST(Kernel::Process::current().close(fd)); }); stat st; - TRY(Kernel::Process::current()->fstat(fd, &st)); + TRY(Kernel::Process::current().fstat(fd, &st)); TRY(data.resize(st.st_size)); - TRY(Kernel::Process::current()->read(fd, data.data(), data.size())); + TRY(Kernel::Process::current().read(fd, data.data(), data.size())); elf = new ELF(BAN::move(data)); ASSERT(elf); diff --git a/kernel/include/kernel/Process.h b/kernel/include/kernel/Process.h index f7bb34862f..c5934856d2 100644 --- a/kernel/include/kernel/Process.h +++ b/kernel/include/kernel/Process.h @@ -13,7 +13,7 @@ namespace Kernel { - class Process : BAN::RefCounted + class Process { BAN_NON_COPYABLE(Process); BAN_NON_MOVABLE(Process); @@ -22,8 +22,8 @@ namespace Kernel using entry_t = Thread::entry_t; public: - static BAN::ErrorOr> create_kernel(entry_t, void*); - static BAN::ErrorOr> create_userspace(void(*)()); + static BAN::ErrorOr create_kernel(entry_t, void*); + static BAN::ErrorOr create_userspace(void(*)()); ~Process(); [[noreturn]] void exit(); @@ -52,10 +52,11 @@ namespace Kernel BAN::ErrorOr working_directory() const; BAN::ErrorOr set_working_directory(BAN::StringView); - static BAN::RefPtr current() { return Thread::current().process(); } + static Process& current() { return Thread::current().process(); } private: Process(pid_t); + static Process* create_process(); BAN::ErrorOr absolute_path_of(BAN::StringView) const; @@ -81,8 +82,6 @@ namespace Kernel BAN::Vector m_threads; TTY* m_tty { nullptr }; - - friend class BAN::RefPtr; }; } \ No newline at end of file diff --git a/kernel/include/kernel/Thread.h b/kernel/include/kernel/Thread.h index dc002135f3..14204206a4 100644 --- a/kernel/include/kernel/Thread.h +++ b/kernel/include/kernel/Thread.h @@ -26,7 +26,7 @@ namespace Kernel }; public: - static BAN::ErrorOr create(entry_t, void* = nullptr, BAN::RefPtr = nullptr); + static BAN::ErrorOr create(entry_t, void*, Process*); ~Thread(); void jump_userspace(uintptr_t rip); @@ -46,22 +46,22 @@ namespace Kernel size_t stack_size() const { return m_stack_size; } static Thread& current() ; - BAN::RefPtr process(); + Process& process(); private: - Thread(pid_t tid, BAN::RefPtr); + Thread(pid_t tid, Process*); BAN::ErrorOr initialize(entry_t, void*); void on_exit(); private: static constexpr size_t m_stack_size = 4096 * 1; - void* m_stack_base = nullptr; - uintptr_t m_rip = 0; - uintptr_t m_rsp = 0; - const pid_t m_tid = 0; - State m_state { State::NotStarted }; - BAN::RefPtr m_process; + void* m_stack_base { nullptr }; + uintptr_t m_rip { 0 }; + uintptr_t m_rsp { 0 }; + const pid_t m_tid { 0 }; + State m_state { State::NotStarted }; + Process* m_process { nullptr }; friend class Scheduler; }; diff --git a/kernel/kernel/Font.cpp b/kernel/kernel/Font.cpp index 8f2fff09a8..160e5aa76c 100644 --- a/kernel/kernel/Font.cpp +++ b/kernel/kernel/Font.cpp @@ -37,15 +37,15 @@ namespace Kernel BAN::ErrorOr Font::load(BAN::StringView path) { - int fd = TRY(Process::current()->open(path, O_RDONLY)); - BAN::ScopeGuard _([fd] { MUST(Process::current()->close(fd)); }); + int fd = TRY(Process::current().open(path, O_RDONLY)); + BAN::ScopeGuard _([fd] { MUST(Process::current().close(fd)); }); stat st; - TRY(Process::current()->fstat(fd, &st)); + TRY(Process::current().fstat(fd, &st)); BAN::Vector file_data; TRY(file_data.resize(st.st_size)); - TRY(Process::current()->read(fd, file_data.data(), st.st_size)); + TRY(Process::current().read(fd, file_data.data(), st.st_size)); if (file_data.size() < 4) return BAN::Error::from_error_code(ErrorCode::Font_FileTooSmall); diff --git a/kernel/kernel/Process.cpp b/kernel/kernel/Process.cpp index 0731a9cd08..ad63d8ae63 100644 --- a/kernel/kernel/Process.cpp +++ b/kernel/kernel/Process.cpp @@ -11,19 +11,33 @@ namespace Kernel { - static pid_t s_next_pid = 1; + static BAN::Vector s_processes; + static SpinLock s_process_lock; - BAN::ErrorOr> Process::create_kernel(entry_t entry, void* data) + Process* Process::create_process() { - auto process = TRY(BAN::RefPtr::create(s_next_pid++)); + static pid_t s_next_pid = 1; + auto* process = new Process(s_next_pid++); + ASSERT(process); + + s_process_lock.lock(); + MUST(s_processes.push_back(process)); + s_process_lock.unlock(); + + return process; + } + + BAN::ErrorOr Process::create_kernel(entry_t entry, void* data) + { + auto* process = create_process(); TRY(process->m_working_directory.push_back('/')); TRY(process->add_thread(entry, data)); return process; } - BAN::ErrorOr> Process::create_userspace(void(*entry)()) + BAN::ErrorOr Process::create_userspace(void(*entry)()) { - auto process = TRY(BAN::RefPtr::create(s_next_pid++)); + auto* process = create_process(); TRY(process->m_working_directory.push_back('/')); TRY(process->init_stdio()); diff --git a/kernel/kernel/Scheduler.cpp b/kernel/kernel/Scheduler.cpp index 3c9af72226..f5dee5c24b 100644 --- a/kernel/kernel/Scheduler.cpp +++ b/kernel/kernel/Scheduler.cpp @@ -27,7 +27,7 @@ namespace Kernel ASSERT(s_instance == nullptr); s_instance = new Scheduler(); ASSERT(s_instance); - s_instance->m_idle_thread = TRY(Thread::create([](void*) { for (;;) asm volatile("hlt"); })); + s_instance->m_idle_thread = TRY(Thread::create([](void*) { for (;;) asm volatile("hlt"); }, nullptr, nullptr)); return {}; } @@ -247,11 +247,11 @@ namespace Kernel { VERIFY_CLI(); - pid_t pid = m_current_thread->thread->process()->pid(); + pid_t pid = m_current_thread->thread->process().pid(); - remove_threads(m_blocking_threads, it->thread->process()->pid() == pid); - remove_threads(m_sleeping_threads, it->thread->process()->pid() == pid); - remove_threads(m_active_threads, it != m_current_thread && it->thread->process()->pid() == pid); + remove_threads(m_blocking_threads, it->thread->process().pid() == pid); + remove_threads(m_sleeping_threads, it->thread->process().pid() == pid); + remove_threads(m_active_threads, it != m_current_thread && it->thread->process().pid() == pid); delete m_current_thread->thread; remove_and_advance_current_thread(); diff --git a/kernel/kernel/Shell.cpp b/kernel/kernel/Shell.cpp index 44faaaf4e0..9cec5f548e 100644 --- a/kernel/kernel/Shell.cpp +++ b/kernel/kernel/Shell.cpp @@ -22,14 +22,14 @@ namespace Kernel static void TTY_PRINT(Args&&... args) { BAN::String message = BAN::String::formatted(BAN::forward(args)...); - MUST(Process::current()->write(STDOUT_FILENO, message.data(), message.size())); + MUST(Process::current().write(STDOUT_FILENO, message.data(), message.size())); } template static void TTY_PRINTLN(Args&&... args) { TTY_PRINT(BAN::forward(args)...); - MUST(Process::current()->write(STDOUT_FILENO, "\n", 1)); + MUST(Process::current().write(STDOUT_FILENO, "\n", 1)); } static auto s_default_prompt = "\\[\e[32m\\]user\\[\e[m\\]:\\[\e[34m\\]\\w\\[\e[m\\]# "sv; @@ -59,7 +59,7 @@ namespace Kernel { MUST(set_prompt(s_default_prompt)); MUST(m_buffer.push_back(""sv)); - MUST(Process::current()->set_termios(termios { .canonical = false, .echo = false })); + MUST(Process::current().set_termios(termios { .canonical = false, .echo = false })); } BAN::ErrorOr Shell::set_prompt(BAN::StringView prompt) @@ -89,7 +89,7 @@ namespace Kernel break; case 'w': { - auto working_directory = TRY(Process::current()->working_directory()); + auto working_directory = TRY(Process::current().working_directory()); TRY(m_prompt.append(working_directory)); m_prompt_length += working_directory.size(); break; @@ -112,7 +112,7 @@ namespace Kernel void Shell::run() { - auto getch = [this] { uint8_t ch; MUST(Process::current()->read(STDIN_FILENO, &ch, 1)); return ch; }; + auto getch = [this] { uint8_t ch; MUST(Process::current().read(STDIN_FILENO, &ch, 1)); return ch; }; MUST(m_buffer.push_back(""sv)); @@ -130,7 +130,7 @@ namespace Kernel while ((current.back() & 0xC0) == 0x80) current.pop_back(); current.pop_back(); - MUST(Process::current()->write(STDOUT_FILENO, "\b \b", 3)); + MUST(Process::current().write(STDOUT_FILENO, "\b \b", 3)); } continue; } @@ -171,7 +171,7 @@ namespace Kernel continue; } - MUST(Process::current()->write(STDOUT_FILENO, &ch, 1)); + MUST(Process::current().write(STDOUT_FILENO, &ch, 1)); if (ch != '\n') { @@ -334,7 +334,7 @@ argument_done: for (int i = 0; i < 100; i++) { lock.lock(); - MUST(Process::create_kernel([](void*) { MUST(Process::current()->init_stdio()); TTY_PRINTLN("####"); Process::current()->exit(); }, nullptr)); + MUST(Process::create_kernel([](void*) { MUST(Process::current().init_stdio()); TTY_PRINTLN("####"); Process::current().exit(); }, nullptr)); PIT::sleep(5); kmalloc_dump_info(); lock.unlock(); @@ -406,7 +406,7 @@ argument_done: SpinLock spinlock; thread_data_t thread_data = { this, spinlock, arguments }; spinlock.lock(); - TRY(Process::current()->add_thread(function, &thread_data)); + TRY(Process::current().add_thread(function, &thread_data)); while (spinlock.is_locked()); } else if (arguments.front() == "memory") @@ -508,15 +508,15 @@ argument_done: if (arguments.size() == 2) TRY(path.append(arguments[1])); else - TRY(path.append(TRY(Process::current()->working_directory()))); + TRY(path.append(TRY(Process::current().working_directory()))); - int fd = TRY(Process::current()->open(path, O_RDONLY)); - BAN::ScopeGuard _([fd] { MUST(Process::current()->close(fd)); }); + int fd = TRY(Process::current().open(path, O_RDONLY)); + BAN::ScopeGuard _([fd] { MUST(Process::current().close(fd)); }); BAN::Vector all_entries; BAN::Vector entries; - while (!(entries = TRY(Process::current()->read_directory_entries(fd))).empty()) + while (!(entries = TRY(Process::current().read_directory_entries(fd))).empty()) { TRY(all_entries.reserve(all_entries.size() + entries.size())); for (auto& entry : entries) @@ -533,7 +533,7 @@ argument_done: BAN::String entry_path; TRY(entry_path.append(entry_prefix)); TRY(entry_path.append(entry)); - TRY(Process::current()->stat(entry_path, &st)); + TRY(Process::current().stat(entry_path, &st)); Inode::Mode mode { st.st_mode }; @@ -554,14 +554,14 @@ argument_done: return {}; } - int fd = TRY(Process::current()->open(arguments[1], O_RDONLY)); - BAN::ScopeGuard _([fd] { MUST(Process::current()->close(fd)); }); + int fd = TRY(Process::current().open(arguments[1], O_RDONLY)); + BAN::ScopeGuard _([fd] { MUST(Process::current().close(fd)); }); char* buffer = new char[1024]; BAN::ScopeGuard buffer_guard([buffer] { delete[] buffer; }); ASSERT(buffer); - while (size_t n_read = TRY(Process::current()->read(fd, buffer, sizeof(buffer)))) + while (size_t n_read = TRY(Process::current().read(fd, buffer, sizeof(buffer)))) TTY_PRINT("{}", BAN::StringView(buffer, n_read)); TTY_PRINTLN(""); } @@ -574,7 +574,7 @@ argument_done: } stat st; - TRY(Process::current()->stat(arguments[1], &st)); + TRY(Process::current().stat(arguments[1], &st)); Inode::Mode mode { st.st_mode }; @@ -604,7 +604,7 @@ argument_done: return {}; } BAN::StringView path = arguments.size() == 2 ? arguments[1].sv() : "/"sv; - TRY(Process::current()->set_working_directory(path)); + TRY(Process::current().set_working_directory(path)); TRY(update_prompt()); } else if (arguments.front() == "touch") @@ -614,7 +614,7 @@ argument_done: TTY_PRINTLN("usage 'touch path'"); return {}; } - TRY(Process::current()->creat(arguments[1], 0)); + TRY(Process::current().creat(arguments[1], 0)); } else if (arguments.front() == "cksum") { @@ -630,15 +630,15 @@ argument_done: for (size_t i = 1; i < arguments.size(); i++) { - int fd = TRY(Process::current()->open(arguments[i], O_RDONLY)); - BAN::ScopeGuard _([fd] { MUST(Process::current()->close(fd)); }); + int fd = TRY(Process::current().open(arguments[i], O_RDONLY)); + BAN::ScopeGuard _([fd] { MUST(Process::current().close(fd)); }); uint32_t crc32 = 0; uint32_t total_read = 0; while (true) { - size_t n_read = TRY(Process::current()->read(fd, buffer, sizeof(buffer))); + size_t n_read = TRY(Process::current().read(fd, buffer, sizeof(buffer))); if (n_read == 0) break; for (size_t j = 0; j < n_read; j++) @@ -660,7 +660,7 @@ argument_done: TTY_PRINTLN("usage: 'mount partition directory'"); return {}; } - TRY(Process::current()->mount(arguments[1], arguments[2])); + TRY(Process::current().mount(arguments[1], arguments[2])); } else if (arguments.front() == "loadfont") { diff --git a/kernel/kernel/Syscall.cpp b/kernel/kernel/Syscall.cpp index 842e41cb42..9d51146e19 100644 --- a/kernel/kernel/Syscall.cpp +++ b/kernel/kernel/Syscall.cpp @@ -7,12 +7,12 @@ namespace Kernel void sys_exit() { - Process::current()->exit(); + Process::current().exit(); } int sys_read(int fd, void* buffer, size_t size) { - auto res = Process::current()->read(fd, buffer, size); + auto res = Process::current().read(fd, buffer, size); if (res.is_error()) return res.error().get_error_code(); return 0; @@ -20,7 +20,7 @@ namespace Kernel int sys_write(int fd, const void* buffer, size_t size) { - auto res = Process::current()->write(fd, buffer, size); + auto res = Process::current().write(fd, buffer, size); if (res.is_error()) return res.error().get_error_code(); return 0; diff --git a/kernel/kernel/Terminal/TTY.cpp b/kernel/kernel/Terminal/TTY.cpp index caa61d436a..dd70c74d58 100644 --- a/kernel/kernel/Terminal/TTY.cpp +++ b/kernel/kernel/Terminal/TTY.cpp @@ -59,11 +59,11 @@ namespace Kernel [](void* tty_) { TTY* tty = (TTY*)tty_; - int fd = MUST(Process::current()->open("/dev/input0"sv, O_RDONLY)); + int fd = MUST(Process::current().open("/dev/input0"sv, O_RDONLY)); while (true) { Input::KeyEvent event; - MUST(Process::current()->read(fd, &event, sizeof(event))); + MUST(Process::current().read(fd, &event, sizeof(event))); tty->on_key(event); } }, this) diff --git a/kernel/kernel/Thread.cpp b/kernel/kernel/Thread.cpp index f6106aa927..7287daf4d6 100644 --- a/kernel/kernel/Thread.cpp +++ b/kernel/kernel/Thread.cpp @@ -19,7 +19,7 @@ namespace Kernel memcpy((void*)rsp, (void*)&value, size); } - BAN::ErrorOr Thread::create(entry_t entry, void* data, BAN::RefPtr process) + BAN::ErrorOr Thread::create(entry_t entry, void* data, Process* process) { static pid_t next_tid = 1; auto* thread = new Thread(next_tid++, process); @@ -29,7 +29,7 @@ namespace Kernel return thread; } - Thread::Thread(pid_t tid, BAN::RefPtr process) + Thread::Thread(pid_t tid, Process* process) : m_tid(tid), m_process(process) {} @@ -38,9 +38,10 @@ namespace Kernel return Scheduler::get().current_thread(); } - BAN::RefPtr Thread::process() + Process& Thread::process() { - return m_process; + ASSERT(m_process); + return *m_process; } BAN::ErrorOr Thread::initialize(entry_t entry, void* data) diff --git a/kernel/kernel/kernel.cpp b/kernel/kernel/kernel.cpp index dedca7996e..ca47facea2 100644 --- a/kernel/kernel/kernel.cpp +++ b/kernel/kernel/kernel.cpp @@ -143,6 +143,7 @@ extern "C" void kernel_main() TTY* tty1 = new TTY(terminal_driver); ASSERT(tty1); + dprintln("TTY initialized"); Memory::Heap::initialize(); dprintln("Heap initialzed"); @@ -164,7 +165,7 @@ extern "C" void kernel_main() MUST(Scheduler::initialize()); Scheduler& scheduler = Scheduler::get(); - MUST(scheduler.add_thread(MUST(Thread::create(init2, tty1)))); + MUST(scheduler.add_thread(MUST(Thread::create(init2, tty1, nullptr)))); scheduler.start(); ASSERT_NOT_REACHED(); @@ -189,18 +190,25 @@ static void init2(void* tty1) [](void*) { MUST(LibELF::ELF::load_from_file("/bin/test"sv)); - Process::current()->exit(); + Process::current().exit(); }, nullptr )); return; + MUST(Process::create_kernel( + [](void*) + { + + }, nullptr + )); + jump_userspace(); return; MUST(Process::create_kernel( [](void*) { - MUST(Process::current()->init_stdio()); + MUST(Process::current().init_stdio()); Shell* shell = new Shell(); ASSERT(shell); shell->run();