From b78596dcf4d765145cedd64c5453e2e9e8f15f00 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Fri, 21 Jul 2023 19:27:10 +0300 Subject: [PATCH] Kernel: Scheduler now sends queued signals --- kernel/include/kernel/Thread.h | 1 + kernel/kernel/Process.cpp | 2 ++ kernel/kernel/Scheduler.cpp | 2 ++ kernel/kernel/Thread.cpp | 17 +++++++++++++---- 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/kernel/include/kernel/Thread.h b/kernel/include/kernel/Thread.h index 8e5dc5c10b..5d4605cfce 100644 --- a/kernel/include/kernel/Thread.h +++ b/kernel/include/kernel/Thread.h @@ -37,6 +37,7 @@ namespace Kernel BAN::ErrorOr clone(Process*, uintptr_t rsp, uintptr_t rip); void setup_exec(); + void handle_next_signal(); void handle_signal(int signal, uintptr_t& return_rsp, uintptr_t& return_rip); pid_t tid() const { return m_tid; } diff --git a/kernel/kernel/Process.cpp b/kernel/kernel/Process.cpp index 0c7617149f..ace5a20c46 100644 --- a/kernel/kernel/Process.cpp +++ b/kernel/kernel/Process.cpp @@ -794,6 +794,8 @@ namespace Kernel if (signal < _SIGMIN || signal > _SIGMAX) return BAN::Error::from_errno(EINVAL); ASSERT(m_threads.size() == 1); + CriticalScope _; + Thread::current().m_signal_queue.push(signal); Thread::current().handle_signal(signal, return_rsp, return_rip); return 0; } diff --git a/kernel/kernel/Scheduler.cpp b/kernel/kernel/Scheduler.cpp index ca8f3c0595..eb5b209249 100644 --- a/kernel/kernel/Scheduler.cpp +++ b/kernel/kernel/Scheduler.cpp @@ -212,6 +212,8 @@ namespace Kernel current.set_started(); start_thread(current.rsp(), current.rip()); case Thread::State::Executing: + while (!current.m_signal_queue.empty()) + current.handle_next_signal(); continue_thread(current.rsp(), current.rip()); case Thread::State::Terminating: ENABLE_INTERRUPTS(); diff --git a/kernel/kernel/Thread.cpp b/kernel/kernel/Thread.cpp index 571692df1d..858d486e28 100644 --- a/kernel/kernel/Thread.cpp +++ b/kernel/kernel/Thread.cpp @@ -143,10 +143,20 @@ namespace Kernel } } + void Thread::handle_next_signal() + { + ASSERT(!interrupts_enabled()); + ASSERT(!m_signal_queue.empty()); + handle_signal(m_signal_queue.front(), m_rsp, m_rip); + } + void Thread::handle_signal(int signal, uintptr_t& return_rsp, uintptr_t& return_rip) { + ASSERT(!interrupts_enabled()); ASSERT(signal >= _SIGMIN && signal <= _SIGMAX); ASSERT(&Thread::current() == this); + ASSERT(!m_signal_queue.empty()); + ASSERT(m_signal_queue.front() == signal); // Skip masked (ignored) signals if (m_signal_mask & (1ull << signal)) @@ -154,7 +164,6 @@ namespace Kernel if (m_signal_handlers[signal]) { - asm volatile("cli"); write_to_stack(return_rsp, return_rip); write_to_stack(return_rsp, signal); write_to_stack(return_rsp, m_signal_handlers[signal]); @@ -193,6 +202,8 @@ namespace Kernel { auto message = BAN::String::formatted("killed by signal {}\n", signal); (void)process().tty().write(0, message.data(), message.size()); + + ENABLE_INTERRUPTS(); process().exit(128 + signal); ASSERT_NOT_REACHED(); } @@ -214,9 +225,7 @@ namespace Kernel } } - asm volatile("cli"); - if (!m_signal_queue.empty() && m_signal_queue.front() == signal) - m_signal_queue.pop(); + m_signal_queue.pop(); } void Thread::validate_stack() const