banan-os/kernel/kernel/Thread.cpp

114 lines
2.8 KiB
C++

#include <BAN/Errors.h>
#include <kernel/InterruptController.h>
#include <kernel/Memory/kmalloc.h>
#include <kernel/Process.h>
#include <kernel/Scheduler.h>
#include <kernel/Thread.h>
#define PAGE_SIZE 4096
namespace Kernel
{
extern "C" void thread_jump_userspace(uintptr_t rsp, uintptr_t rip);
template<size_t size, typename T>
static void write_to_stack(uintptr_t& rsp, const T& value)
{
rsp -= size;
memcpy((void*)rsp, (void*)&value, size);
}
BAN::ErrorOr<Thread*> Thread::create(entry_t entry, void* data, Process* process)
{
static pid_t next_tid = 1;
auto* thread = new Thread(next_tid++, process);
if (thread == nullptr)
return BAN::Error::from_errno(ENOMEM);
TRY(thread->initialize(entry, data));
return thread;
}
BAN::ErrorOr<Thread*> Thread::create_userspace(uintptr_t entry, Process* process)
{
Thread* thread = TRY(Thread::create(
[](void* entry)
{
Thread::current().jump_userspace((uintptr_t)entry);
ASSERT_NOT_REACHED();
}, (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().identity_map_range(thread->stack_base(), thread->stack_size(), MMU::Flags::UserSupervisor | MMU::Flags::ReadWrite | MMU::Flags::Present);
return thread;
}
Thread::Thread(pid_t tid, Process* process)
: m_tid(tid), m_process(process)
{}
Thread& Thread::current()
{
return Scheduler::get().current_thread();
}
Process& Thread::process()
{
ASSERT(m_process);
return *m_process;
}
BAN::ErrorOr<void> Thread::initialize(entry_t entry, void* data)
{
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 + m_stack_size;
m_rip = (uintptr_t)entry;
write_to_stack<sizeof(void*)>(m_rsp, this);
write_to_stack<sizeof(void*)>(m_rsp, &Thread::on_exit);
write_to_stack<sizeof(void*)>(m_rsp, data);
return {};
}
Thread::~Thread()
{
dprintln("thread {} ({}) exit", tid(), m_process->pid());
if (m_interrupt_stack)
kfree(m_interrupt_stack);
kfree(m_stack_base);
}
void Thread::jump_userspace(uintptr_t 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()
{
if (m_process)
m_process->on_thread_exit(*this);
Scheduler::get().set_current_thread_done();
ASSERT_NOT_REACHED();
}
}