Compare commits

..

No commits in common. "779fda307a73e4047eee32e672c7fd3a451c356c" and "6de7753b4c66c42c0b759541ce22892ee643157d" have entirely different histories.

3 changed files with 5 additions and 44 deletions

View File

@ -7,7 +7,6 @@
#include <luna/Alignment.h> #include <luna/Alignment.h>
#include <luna/LinkedList.h> #include <luna/LinkedList.h>
#include <luna/SafeArithmetic.h> #include <luna/SafeArithmetic.h>
#include <luna/ScopeGuard.h>
#include <luna/String.h> #include <luna/String.h>
#include <luna/SystemError.h> #include <luna/SystemError.h>
@ -37,9 +36,7 @@ static DoublyLinkedList<HeapBlock> heap;
static Result<HeapBlock*> allocate_pages(usize count) static Result<HeapBlock*> allocate_pages(usize count)
{ {
u64 virt = TRY(KernelVM::alloc_several_pages(count)); u64 virt = TRY(KernelVM::alloc_several_pages(count));
auto vm_guard = make_scope_guard([&] { KernelVM::free_several_pages(virt, count).value(); });
void* const ptr = (void*)TRY(MemoryManager::alloc_at(virt, count, MMU::ReadWrite | MMU::NoExecute)); void* const ptr = (void*)TRY(MemoryManager::alloc_at(virt, count, MMU::ReadWrite | MMU::NoExecute));
vm_guard.deactivate();
return (HeapBlock*)ptr; return (HeapBlock*)ptr;
} }

View File

@ -4,7 +4,6 @@
#include "arch/MMU.h" #include "arch/MMU.h"
#include "memory/KernelVM.h" #include "memory/KernelVM.h"
#include "memory/MemoryManager.h" #include "memory/MemoryManager.h"
#include <luna/ScopeGuard.h>
#include <luna/Stack.h> #include <luna/Stack.h>
static Thread g_idle; static Thread g_idle;
@ -45,19 +44,12 @@ namespace Scheduler
return &g_idle; return &g_idle;
} }
Result<void> new_kernel_thread_impl(Thread* thread) Result<void> kernel_thread_alloc_stack_and_append_impl(Thread* thread)
{ {
// If anything fails, make sure to clean up. // FIXME: We will leak the thread if VM allocation or alloc_at fail.
auto thread_guard = make_scope_guard([&] { delete thread; });
u64 thread_stack_vm = TRY(KernelVM::alloc_several_pages(4)); u64 thread_stack_vm = TRY(KernelVM::alloc_several_pages(4));
auto vm_guard = make_scope_guard([&] { KernelVM::free_several_pages(thread_stack_vm, 4).value(); });
TRY(MemoryManager::alloc_at(thread_stack_vm, 4, MMU::NoExecute | MMU::ReadWrite)); TRY(MemoryManager::alloc_at(thread_stack_vm, 4, MMU::NoExecute | MMU::ReadWrite));
thread_guard.deactivate();
vm_guard.deactivate();
Stack thread_stack{thread_stack_vm, ARCH_PAGE_SIZE * 4}; Stack thread_stack{thread_stack_vm, ARCH_PAGE_SIZE * 4};
thread->set_sp(thread_stack.top()); thread->set_sp(thread_stack.top());
@ -74,7 +66,7 @@ namespace Scheduler
thread->init_regs_kernel(); thread->init_regs_kernel();
thread->set_ip(address); thread->set_ip(address);
return new_kernel_thread_impl(thread); return kernel_thread_alloc_stack_and_append_impl(thread);
} }
Result<void> new_kernel_thread(void (*func)(void)) Result<void> new_kernel_thread(void (*func)(void))
@ -83,7 +75,7 @@ namespace Scheduler
thread->init_regs_kernel(); thread->init_regs_kernel();
thread->set_ip((u64)func); thread->set_ip((u64)func);
return new_kernel_thread_impl(thread); return kernel_thread_alloc_stack_and_append_impl(thread);
} }
Result<void> new_kernel_thread(void (*func)(void*), void* arg) Result<void> new_kernel_thread(void (*func)(void*), void* arg)
@ -93,7 +85,7 @@ namespace Scheduler
thread->set_ip((u64)func); thread->set_ip((u64)func);
thread->set_arguments((u64)arg, 0, 0, 0); thread->set_arguments((u64)arg, 0, 0, 0);
return new_kernel_thread_impl(thread); return kernel_thread_alloc_stack_and_append_impl(thread);
} }
Thread* pick_task() Thread* pick_task()

View File

@ -1,28 +0,0 @@
#pragma once
template <typename Callback> class ScopeGuard
{
public:
ScopeGuard(const Callback& callback) : m_callback(callback)
{
}
void deactivate()
{
m_activated = false;
}
~ScopeGuard()
{
if (m_activated) m_callback();
}
private:
bool m_activated{true};
Callback m_callback;
};
template <typename Callback> [[nodiscard]] ScopeGuard<Callback> make_scope_guard(const Callback& callback)
{
return {callback};
}