Kernel: Move ring0 stacks out of kmalloc_eternal

This allows us to use all the same fun memory protection features as the
rest of the system for ring0 processes. Previously a ring0 process could
over- or underrun its stack and nobody cared, since kmalloc_eternal is the
wild west of memory.
This commit is contained in:
Conrad Pankoff 2019-12-24 11:16:50 +11:00 committed by Andreas Kling
parent 3aaeff483b
commit b557aab884
Notes: sideshowbarker 2024-07-19 10:44:29 +09:00

View file

@ -84,20 +84,21 @@ Thread::Thread(Process& process)
m_tss.cr3 = m_process.page_directory().cr3(); m_tss.cr3 = m_process.page_directory().cr3();
if (m_process.is_ring0()) { if (m_process.is_ring0()) {
// FIXME: This memory is leaked. m_kernel_stack_region = MM.allocate_kernel_region(default_kernel_stack_size, String::format("Kernel Stack (Thread %d; Ring0)", m_tid), false, true);
// But uh, there's also no kernel process termination, so I guess it's not technically leaked... m_kernel_stack_base = m_kernel_stack_region->vaddr().get();
m_kernel_stack_base = (u32)kmalloc_eternal(default_kernel_stack_size); m_kernel_stack_top = m_kernel_stack_region->vaddr().offset(default_kernel_stack_size).get() & 0xfffffff8u;
m_kernel_stack_top = (m_kernel_stack_base + default_kernel_stack_size) & 0xfffffff8u;
m_tss.esp = m_kernel_stack_top; m_tss.esp = m_kernel_stack_top;
kprintf("Allocated ring0 stack @ %p - %p\n", m_kernel_stack_base, m_kernel_stack_top);
} else { } else {
// Ring3 processes need a separate stack for Ring0. // Ring3 processes need a separate stack for Ring0.
m_kernel_stack_region = MM.allocate_kernel_region(default_kernel_stack_size, String::format("Kernel Stack (Thread %d)", m_tid)); m_kernel_stack_region = MM.allocate_kernel_region(default_kernel_stack_size, String::format("Kernel Stack (Thread %d; Ring3)", m_tid), false, true);
m_kernel_stack_base = m_kernel_stack_region->vaddr().get(); m_kernel_stack_base = m_kernel_stack_region->vaddr().get();
m_kernel_stack_top = m_kernel_stack_region->vaddr().offset(default_kernel_stack_size).get() & 0xfffffff8u; m_kernel_stack_top = m_kernel_stack_region->vaddr().offset(default_kernel_stack_size).get() & 0xfffffff8u;
m_tss.ss0 = 0x10; m_tss.ss0 = 0x10;
m_tss.esp0 = m_kernel_stack_top; m_tss.esp0 = m_kernel_stack_top;
kprintf("Allocated ring3 stack @ %p - %p\n", m_kernel_stack_base, m_kernel_stack_top);
} }
m_process.page_directory().update_kernel_mappings();
// HACK: Ring2 SS in the TSS is the current PID. // HACK: Ring2 SS in the TSS is the current PID.
m_tss.ss2 = m_process.pid(); m_tss.ss2 = m_process.pid();