diff --git a/kernel/src/thread/ThreadImage.cpp b/kernel/src/thread/ThreadImage.cpp index 2e9ca57c..c96c66b4 100644 --- a/kernel/src/thread/ThreadImage.cpp +++ b/kernel/src/thread/ThreadImage.cpp @@ -6,24 +6,20 @@ static constexpr usize DEFAULT_USER_STACK_PAGES = 6; static constexpr usize DEFAULT_USER_STACK_SIZE = DEFAULT_USER_STACK_PAGES * ARCH_PAGE_SIZE; +static constexpr u64 THREAD_STACK_BASE = 0x10000; -static Result create_stacks(Stack& user_stack, Stack& kernel_stack, AddressSpace* space) +static Result create_user_stack(Stack& user_stack, AddressSpace* space) { - const u64 THREAD_STACK_BASE = 0x10000; - - if (!TRY(space->test_and_alloc_region(THREAD_STACK_BASE, DEFAULT_USER_STACK_PAGES, true))) return err(ENOMEM); - TRY(MemoryManager::alloc_at_zeroed(THREAD_STACK_BASE, DEFAULT_USER_STACK_PAGES, MMU::ReadWrite | MMU::NoExecute | MMU::User)); - auto guard = make_scope_guard([&] { MemoryManager::unmap_owned(THREAD_STACK_BASE, 4); }); + auto guard = make_scope_guard([] { MemoryManager::unmap_owned(THREAD_STACK_BASE, DEFAULT_USER_STACK_PAGES); }); - const u64 kernel_stack_base = TRY(MemoryManager::alloc_for_kernel(4, MMU::ReadWrite | MMU::NoExecute)); + if (!TRY(space->test_and_alloc_region(THREAD_STACK_BASE, DEFAULT_USER_STACK_PAGES, true))) return err(ENOMEM); guard.deactivate(); user_stack = { THREAD_STACK_BASE, DEFAULT_USER_STACK_SIZE }; - kernel_stack = { kernel_stack_base, 4 * ARCH_PAGE_SIZE }; return {}; } @@ -43,12 +39,10 @@ Result> ThreadImage::try_load_from_elf(SharedPtrm_kernel_stack = kernel_stack; image->m_user_stack = user_stack; image->m_loaded_image_data = data; image->m_address_space = move(address_space); @@ -111,7 +105,7 @@ void ThreadImage::apply(Thread* thread) thread->set_ip(m_loaded_image_data.entry); - thread->kernel_stack = m_kernel_stack; + if (m_kernel_stack.bottom()) thread->kernel_stack = m_kernel_stack; thread->stack = m_user_stack; thread->set_sp(align_down<16>(m_sp)); diff --git a/libluna/include/luna/Stack.h b/libluna/include/luna/Stack.h index 30d2ad79..0ce476fd 100644 --- a/libluna/include/luna/Stack.h +++ b/libluna/include/luna/Stack.h @@ -19,6 +19,6 @@ struct Stack } private: - u64 m_base; - usize m_bytes; + u64 m_base { 0 }; + usize m_bytes { 0 }; };