kernel: Move "push_mem_on_stack" and "pop_mem_from_stack" to MemoryManager
All checks were successful
Build and test / build (push) Successful in 1m52s

This commit is contained in:
apio 2024-11-23 20:03:04 +01:00
parent 7b2977a036
commit 3032415bc0
Signed by: apio
GPG Key ID: B8A7D06E42258954
6 changed files with 37 additions and 29 deletions

View File

@ -71,31 +71,14 @@ void switch_context(Thread* old_thread, Thread* new_thread, Registers* regs)
memcpy(regs, &new_thread->regs, sizeof(Registers)); memcpy(regs, &new_thread->regs, sizeof(Registers));
} }
// FIXME: Move this function to a common location (also used in ThreadImage)
Result<u64> Thread::push_mem_on_stack(const u8* mem, usize size) Result<u64> Thread::push_mem_on_stack(const u8* mem, usize size)
{ {
if ((regs.rsp - size) < stack.bottom()) return err(E2BIG); return MemoryManager::push_mem_on_stack(mem, size, stack, regs.rsp);
if (!MemoryManager::validate_user_write((void*)(regs.rsp - size), size)) return err(EFAULT);
regs.rsp -= size;
memcpy((void*)regs.rsp, mem, size);
return regs.rsp;
} }
Result<u64> Thread::pop_mem_from_stack(u8* mem, usize size) Result<u64> Thread::pop_mem_from_stack(u8* mem, usize size)
{ {
if ((regs.rsp + size) > stack.top()) return err(E2BIG); return MemoryManager::pop_mem_from_stack(mem, size, stack, regs.rsp);
if (!MemoryManager::validate_user_read((void*)regs.rsp, size)) return err(EFAULT);
memcpy(mem, (void*)regs.rsp, size);
regs.rsp += size;
return regs.rsp;
} }
bool Thread::deliver_signal(int signo, Registers* current_regs) bool Thread::deliver_signal(int signo, Registers* current_regs)

View File

@ -3,6 +3,7 @@
#include "fs/devices/BlockDevice.h" #include "fs/devices/BlockDevice.h"
#include "fs/devices/DeviceRegistry.h" #include "fs/devices/DeviceRegistry.h"
#include "lib/Mutex.h" #include "lib/Mutex.h"
#include "thread/Thread.h"
#include <luna/Atomic.h> #include <luna/Atomic.h>
#include <luna/SharedPtr.h> #include <luna/SharedPtr.h>
#include <luna/StaticString.h> #include <luna/StaticString.h>

View File

@ -1,7 +1,9 @@
#pragma once #pragma once
#include "thread/Thread.h"
#include <luna/CircularQueue.h> #include <luna/CircularQueue.h>
#include <luna/Spinlock.h> #include <luna/Spinlock.h>
#include <sys/types.h>
class Thread;
class Mutex class Mutex
{ {

View File

@ -641,4 +641,30 @@ namespace MemoryManager
{ {
return free_mem + used_mem + reserved_mem; return free_mem + used_mem + reserved_mem;
} }
Result<u64> push_mem_on_stack(const u8* mem, usize size, const Stack& stack, u64& sp)
{
if ((sp - size) < stack.bottom()) return err(E2BIG);
if (!MemoryManager::validate_user_write((void*)(sp - size), size)) return err(EFAULT);
sp -= size;
memcpy((void*)sp, mem, size);
return sp;
}
Result<u64> pop_mem_from_stack(u8* mem, usize size, const Stack& stack, u64& sp)
{
if ((sp + size) > stack.top()) return err(E2BIG);
if (!MemoryManager::validate_user_read((void*)sp, size)) return err(EFAULT);
memcpy(mem, (void*)sp, size);
sp += size;
return sp;
}
} }

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include "arch/MMU.h" #include "arch/MMU.h"
#include <luna/Result.h> #include <luna/Result.h>
#include <luna/Stack.h>
#include <luna/String.h> #include <luna/String.h>
#include <luna/Types.h> #include <luna/Types.h>
@ -88,4 +89,7 @@ namespace MemoryManager
usize used(); usize used();
usize reserved(); usize reserved();
usize total(); usize total();
Result<u64> push_mem_on_stack(const u8* mem, usize size, const Stack& stack, u64& sp);
Result<u64> pop_mem_from_stack(u8* mem, usize size, const Stack& stack, u64& sp);
} }

View File

@ -67,15 +67,7 @@ Result<OwnedPtr<ThreadImage>> ThreadImage::clone_from_thread(Thread* parent)
Result<u64> ThreadImage::push_mem_on_stack(const u8* mem, usize size) Result<u64> ThreadImage::push_mem_on_stack(const u8* mem, usize size)
{ {
if ((m_sp - size) < m_user_stack.bottom()) return err(E2BIG); return MemoryManager::push_mem_on_stack(mem, size, m_user_stack, m_sp);
if (!MemoryManager::validate_user_write((void*)(m_sp - size), size)) return err(EFAULT);
m_sp -= size;
memcpy((void*)m_sp, mem, size);
return m_sp;
} }
Result<u64> ThreadImage::push_string_vector_on_stack(const Vector<String>& vec) Result<u64> ThreadImage::push_string_vector_on_stack(const Vector<String>& vec)