From 12779cdef8e394b74ba2249b2a5706b31a8239fe Mon Sep 17 00:00:00 2001 From: Bananymous Date: Wed, 12 Apr 2023 00:22:08 +0300 Subject: [PATCH] Kernel: Threads now use only 4 KiB stack :) --- kernel/include/kernel/Thread.h | 4 ++++ kernel/kernel/Process.cpp | 5 +++-- kernel/kernel/Thread.cpp | 5 ++--- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/kernel/include/kernel/Thread.h b/kernel/include/kernel/Thread.h index 6e68fe81..e2bc306c 100644 --- a/kernel/include/kernel/Thread.h +++ b/kernel/include/kernel/Thread.h @@ -40,6 +40,9 @@ namespace Kernel State state() const { return m_state; } void terminate() { m_state = State::Terminating; } + uintptr_t stack_base() const { return (uintptr_t)m_stack_base; } + size_t stack_size() const { return m_stack_size; } + static Thread& current() ; BAN::RefPtr process(); @@ -50,6 +53,7 @@ namespace Kernel 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; diff --git a/kernel/kernel/Process.cpp b/kernel/kernel/Process.cpp index 1236fb2f..cd7e09d5 100644 --- a/kernel/kernel/Process.cpp +++ b/kernel/kernel/Process.cpp @@ -9,10 +9,11 @@ namespace Kernel { + static pid_t s_next_pid = 1; + BAN::ErrorOr> Process::create_kernel(entry_t entry, void* data) { - static pid_t next_pid = 1; - auto process = TRY(BAN::RefPtr::create(next_pid++)); + auto process = TRY(BAN::RefPtr::create(s_next_pid++)); TRY(process->m_working_directory.push_back('/')); TRY(process->add_thread(entry, data)); return process; diff --git a/kernel/kernel/Thread.cpp b/kernel/kernel/Thread.cpp index db7cd34c..29502507 100644 --- a/kernel/kernel/Thread.cpp +++ b/kernel/kernel/Thread.cpp @@ -10,7 +10,6 @@ namespace Kernel { - static constexpr size_t thread_stack_size = 16384; template static void write_to_stack(uintptr_t& rsp, const T& value) @@ -45,10 +44,10 @@ namespace Kernel BAN::ErrorOr Thread::initialize(entry_t entry, void* data) { - m_stack_base = kmalloc(thread_stack_size, PAGE_SIZE); + m_stack_base = kmalloc(m_stack_size, PAGE_SIZE); if (m_stack_base == nullptr) return BAN::Error::from_errno(ENOMEM); - m_rsp = (uintptr_t)m_stack_base + thread_stack_size; + m_rsp = (uintptr_t)m_stack_base + m_stack_size; m_rip = (uintptr_t)entry; write_to_stack(m_rsp, this);