From 3032415bc0f399e82b3e58072fc04b877ce54ad2 Mon Sep 17 00:00:00 2001 From: apio Date: Sat, 23 Nov 2024 20:03:04 +0100 Subject: [PATCH] kernel: Move "push_mem_on_stack" and "pop_mem_from_stack" to MemoryManager --- kernel/src/arch/x86_64/Thread.cpp | 21 ++------------------- kernel/src/arch/x86_64/disk/ATA.h | 1 + kernel/src/lib/Mutex.h | 4 +++- kernel/src/memory/MemoryManager.cpp | 26 ++++++++++++++++++++++++++ kernel/src/memory/MemoryManager.h | 4 ++++ kernel/src/thread/ThreadImage.cpp | 10 +--------- 6 files changed, 37 insertions(+), 29 deletions(-) diff --git a/kernel/src/arch/x86_64/Thread.cpp b/kernel/src/arch/x86_64/Thread.cpp index 3cf6913b..09a5793f 100644 --- a/kernel/src/arch/x86_64/Thread.cpp +++ b/kernel/src/arch/x86_64/Thread.cpp @@ -71,31 +71,14 @@ void switch_context(Thread* old_thread, Thread* new_thread, Registers* regs) memcpy(regs, &new_thread->regs, sizeof(Registers)); } -// FIXME: Move this function to a common location (also used in ThreadImage) Result Thread::push_mem_on_stack(const u8* mem, usize size) { - if ((regs.rsp - size) < stack.bottom()) return err(E2BIG); - - 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; + return MemoryManager::push_mem_on_stack(mem, size, stack, regs.rsp); } Result Thread::pop_mem_from_stack(u8* mem, usize size) { - if ((regs.rsp + size) > stack.top()) return err(E2BIG); - - if (!MemoryManager::validate_user_read((void*)regs.rsp, size)) return err(EFAULT); - - memcpy(mem, (void*)regs.rsp, size); - - regs.rsp += size; - - return regs.rsp; + return MemoryManager::pop_mem_from_stack(mem, size, stack, regs.rsp); } bool Thread::deliver_signal(int signo, Registers* current_regs) diff --git a/kernel/src/arch/x86_64/disk/ATA.h b/kernel/src/arch/x86_64/disk/ATA.h index 7ebe1acd..941a8adc 100644 --- a/kernel/src/arch/x86_64/disk/ATA.h +++ b/kernel/src/arch/x86_64/disk/ATA.h @@ -3,6 +3,7 @@ #include "fs/devices/BlockDevice.h" #include "fs/devices/DeviceRegistry.h" #include "lib/Mutex.h" +#include "thread/Thread.h" #include #include #include diff --git a/kernel/src/lib/Mutex.h b/kernel/src/lib/Mutex.h index bf36e992..57f46368 100644 --- a/kernel/src/lib/Mutex.h +++ b/kernel/src/lib/Mutex.h @@ -1,7 +1,9 @@ #pragma once -#include "thread/Thread.h" #include #include +#include + +class Thread; class Mutex { diff --git a/kernel/src/memory/MemoryManager.cpp b/kernel/src/memory/MemoryManager.cpp index 248d1b82..16169cea 100644 --- a/kernel/src/memory/MemoryManager.cpp +++ b/kernel/src/memory/MemoryManager.cpp @@ -641,4 +641,30 @@ namespace MemoryManager { return free_mem + used_mem + reserved_mem; } + + Result 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 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; + } } diff --git a/kernel/src/memory/MemoryManager.h b/kernel/src/memory/MemoryManager.h index 23884712..89920675 100644 --- a/kernel/src/memory/MemoryManager.h +++ b/kernel/src/memory/MemoryManager.h @@ -1,6 +1,7 @@ #pragma once #include "arch/MMU.h" #include +#include #include #include @@ -88,4 +89,7 @@ namespace MemoryManager usize used(); usize reserved(); usize total(); + + Result push_mem_on_stack(const u8* mem, usize size, const Stack& stack, u64& sp); + Result pop_mem_from_stack(u8* mem, usize size, const Stack& stack, u64& sp); } diff --git a/kernel/src/thread/ThreadImage.cpp b/kernel/src/thread/ThreadImage.cpp index b1d3bda5..85323724 100644 --- a/kernel/src/thread/ThreadImage.cpp +++ b/kernel/src/thread/ThreadImage.cpp @@ -67,15 +67,7 @@ Result> ThreadImage::clone_from_thread(Thread* parent) Result ThreadImage::push_mem_on_stack(const u8* mem, usize size) { - if ((m_sp - size) < m_user_stack.bottom()) return err(E2BIG); - - 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; + return MemoryManager::push_mem_on_stack(mem, size, m_user_stack, m_sp); } Result ThreadImage::push_string_vector_on_stack(const Vector& vec)