Kernel: Clean up thread stacks when a thread dies

We were forgetting where we put the userspace thread stacks, so added a
member called Thread::m_userspace_thread_stack to keep track of it.

Then, in ~Thread(), we now deallocate the userspace, kernel and signal
stacks (if present.)

Out of curiosity, the "init_stage2" process doesn't have a kernel stack
which I found surprising. :^)
This commit is contained in:
Andreas Kling 2019-08-01 20:17:12 +02:00
parent 3ad6ae1842
commit 5e01ebfc56
2 changed files with 13 additions and 3 deletions

View file

@ -92,6 +92,15 @@ Thread::~Thread()
if (selector())
gdt_free_entry(selector());
if (m_userspace_stack_region)
m_process.deallocate_region(*m_userspace_stack_region);
if (m_kernel_stack_region)
m_process.deallocate_region(*m_kernel_stack_region);
if (m_kernel_stack_for_signal_handler_region)
m_process.deallocate_region(*m_kernel_stack_for_signal_handler_region);
}
void Thread::unblock()
@ -503,9 +512,9 @@ void Thread::make_userspace_stack_for_main_thread(Vector<String> arguments, Vect
void Thread::make_userspace_stack_for_secondary_thread(void* argument)
{
auto* region = m_process.allocate_region(VirtualAddress(), default_userspace_stack_size, String::format("Stack (Thread %d)", tid()));
ASSERT(region);
m_tss.esp = region->vaddr().offset(default_userspace_stack_size).get();
m_userspace_stack_region = m_process.allocate_region(VirtualAddress(), default_userspace_stack_size, String::format("Stack (Thread %d)", tid()));
ASSERT(m_userspace_stack_region);
m_tss.esp = m_userspace_stack_region->vaddr().offset(default_userspace_stack_size).get();
// NOTE: The stack needs to be 16-byte aligned.
push_value_on_stack((u32)argument);

View file

@ -326,6 +326,7 @@ private:
u32 m_pending_signals { 0 };
u32 m_signal_mask { 0 };
u32 m_kernel_stack_base { 0 };
RefPtr<Region> m_userspace_stack_region;
RefPtr<Region> m_kernel_stack_region;
RefPtr<Region> m_kernel_stack_for_signal_handler_region;
SignalActionData m_signal_action_data[32];