Kernel: Usespace threads now have an interrupt stack

This commit is contained in:
Bananymous 2023-04-25 14:48:58 +03:00
parent 6ecc8cac0e
commit 58ce907327
2 changed files with 34 additions and 14 deletions

View File

@ -46,6 +46,9 @@ namespace Kernel
uintptr_t stack_base() const { return (uintptr_t)m_stack_base; } uintptr_t stack_base() const { return (uintptr_t)m_stack_base; }
size_t stack_size() const { return m_stack_size; } size_t stack_size() const { return m_stack_size; }
uintptr_t interrupt_stack_base() const { return (uintptr_t)m_interrupt_stack; }
uintptr_t interrupt_stack_size() const { return m_interrupt_stack_size; }
static Thread& current() ; static Thread& current() ;
Process& process(); Process& process();
bool has_process() const { return m_process; } bool has_process() const { return m_process; }
@ -55,18 +58,15 @@ namespace Kernel
private: private:
Thread(pid_t tid, Process*); Thread(pid_t tid, Process*);
void validate_stack() const void validate_stack() const;
{
if (!m_in_syscall)
if (!(stack_base() <= m_rsp && m_rsp <= stack_base() + stack_size()))
Kernel::panic("rsp {8H}, stack {8H}->{8H}", m_rsp, stack_base(), stack_base() + stack_size());
}
BAN::ErrorOr<void> initialize(entry_t, void*); BAN::ErrorOr<void> initialize(entry_t, void*);
void on_exit(); void on_exit();
private: private:
static constexpr size_t m_stack_size = 4096 * 1; static constexpr size_t m_stack_size = 4096 * 1;
static constexpr size_t m_interrupt_stack_size = 4096;
void* m_interrupt_stack { nullptr };
void* m_stack_base { nullptr }; void* m_stack_base { nullptr };
uintptr_t m_rip { 0 }; uintptr_t m_rip { 0 };
uintptr_t m_rsp { 0 }; uintptr_t m_rsp { 0 };

View File

@ -38,6 +38,12 @@ namespace Kernel
ASSERT_NOT_REACHED(); ASSERT_NOT_REACHED();
}, (void*)entry, process }, (void*)entry, process
)); ));
thread->m_interrupt_stack = kmalloc(m_interrupt_stack_size, PAGE_SIZE);
if (thread->m_interrupt_stack == nullptr)
{
delete thread;
return BAN::Error::from_errno(ENOMEM);
}
process->mmu().map_range(thread->stack_base(), thread->stack_size(), MMU::Flags::UserSupervisor | MMU::Flags::ReadWrite | MMU::Flags::Present); process->mmu().map_range(thread->stack_base(), thread->stack_size(), MMU::Flags::UserSupervisor | MMU::Flags::ReadWrite | MMU::Flags::Present);
return thread; return thread;
} }
@ -75,6 +81,8 @@ namespace Kernel
Thread::~Thread() Thread::~Thread()
{ {
dprintln("thread {} ({}) exit", tid(), m_process->pid()); dprintln("thread {} ({}) exit", tid(), m_process->pid());
if (m_interrupt_stack)
kfree(m_interrupt_stack);
kfree(m_stack_base); kfree(m_stack_base);
} }
@ -83,6 +91,18 @@ namespace Kernel
thread_jump_userspace(rsp(), rip); thread_jump_userspace(rsp(), rip);
} }
void Thread::validate_stack() const
{
if (stack_base() <= m_rsp && m_rsp <= stack_base() + stack_size())
return;
if (interrupt_stack_base() <= m_rsp && m_rsp <= interrupt_stack_base() + interrupt_stack_size())
return;
Kernel::panic("rsp {8H}, stack {8H}->{8H}, interrupt_stack {8H}->{8H}", m_rsp,
stack_base(), stack_base() + stack_size(),
interrupt_stack_base(), interrupt_stack_base() + interrupt_stack_size()
);
}
void Thread::on_exit() void Thread::on_exit()
{ {
if (m_process) if (m_process)