From 6b180da4e86410878df34577f36bf6c659ae4aed Mon Sep 17 00:00:00 2001 From: Bananymous Date: Fri, 13 Oct 2023 14:14:05 +0300 Subject: [PATCH] Kernel: Separate scheduler execution and stack loading Not sure if this is actually needed, but this allows actual executing function to be in clean environment --- kernel/include/kernel/Scheduler.h | 1 + kernel/kernel/Scheduler.cpp | 20 +++++++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/kernel/include/kernel/Scheduler.h b/kernel/include/kernel/Scheduler.h index be813856..e03cdd02 100644 --- a/kernel/include/kernel/Scheduler.h +++ b/kernel/include/kernel/Scheduler.h @@ -31,6 +31,7 @@ namespace Kernel static bool is_valid_tid(pid_t tid); [[noreturn]] void execute_current_thread(); + [[noreturn]] void _execute_current_thread(); [[noreturn]] void delete_current_process_and_thread(); private: diff --git a/kernel/kernel/Scheduler.cpp b/kernel/kernel/Scheduler.cpp index c33fda57..22ba59c2 100644 --- a/kernel/kernel/Scheduler.cpp +++ b/kernel/kernel/Scheduler.cpp @@ -7,7 +7,10 @@ #include #include -#if 1 +#define SCHEDULER_VERIFY_STACK 1 +#define SCHEDULER_VERIFY_INTERRUPT_STATE 1 + +#if SCHEDULER_VERIFY_INTERRUPT_STATE #define VERIFY_STI() ASSERT(interrupts_enabled()) #define VERIFY_CLI() ASSERT(!interrupts_enabled()) #else @@ -202,6 +205,7 @@ namespace Kernel delete thread; execute_current_thread(); + ASSERT_NOT_REACHED(); } void Scheduler::execute_current_thread() @@ -210,6 +214,20 @@ namespace Kernel load_temp_stack(); PageTable::kernel().load(); + _execute_current_thread(); + ASSERT_NOT_REACHED(); + } + + NEVER_INLINE void Scheduler::_execute_current_thread() + { + VERIFY_CLI(); + +#if SCHEDULER_VERIFY_STACK + vaddr_t rsp; + read_rsp(rsp); + ASSERT((vaddr_t)s_temp_stack <= rsp && rsp <= (vaddr_t)s_temp_stack + sizeof(s_temp_stack)); + ASSERT(&PageTable::current() == &PageTable::kernel()); +#endif Thread* current = ¤t_thread();