Compare commits

...

2 Commits

Author SHA1 Message Date
3032415bc0
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
2024-11-23 20:03:04 +01:00
7b2977a036
kernel: Use a mutex to allocate new posix timers for a thread 2024-11-23 20:02:34 +01:00
9 changed files with 46 additions and 32 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));
}
// FIXME: Move this function to a common location (also used in ThreadImage)
Result<u64> 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<u64> 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)

View File

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

View File

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

View File

@ -641,4 +641,30 @@ namespace MemoryManager
{
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
#include "arch/MMU.h"
#include <luna/Result.h>
#include <luna/Stack.h>
#include <luna/String.h>
#include <luna/Types.h>
@ -88,4 +89,7 @@ namespace MemoryManager
usize used();
usize reserved();
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

@ -118,7 +118,6 @@ Result<u64> sys_timer_create(Registers*, SyscallArgs args)
if (ksevp.sigev_signo <= 0 || ksevp.sigev_signo > NSIG) return err(EINVAL);
int id = TRY(current->allocate_timerid());
current->posix_timers[id] = Timer {};
Timer* timer = current->posix_timers[id].value_ptr();
timer->signo = ksevp.sigev_signo;

View File

@ -61,10 +61,15 @@ Result<FileDescriptor*> Thread::resolve_fd(int fd)
Result<int> Thread::allocate_timerid()
{
ScopedMutexLock lock(posix_timer_mutex);
for (int i = 0; i < MAX_POSIX_TIMERS; i++)
{
// FIXME: Possible race condition, this should be used alongside a mutex.
if (!posix_timers[i].has_value()) { return i; }
if (!posix_timers[i].has_value())
{
posix_timers[i] = Timer {};
return i;
}
}
return err(EMFILE);

View File

@ -3,6 +3,7 @@
#include "arch/MMU.h"
#include "fs/OpenFileDescription.h"
#include "fs/VFS.h"
#include "lib/Mutex.h"
#include "memory/AddressSpace.h"
#include <bits/signal.h>
#include <luna/Bitset.h>
@ -109,6 +110,7 @@ struct Thread : public LinkedListNode<Thread>
Clock profiling_clock;
Option<Timer> posix_timers[MAX_POSIX_TIMERS];
Mutex posix_timer_mutex;
Result<int> allocate_timerid();
Result<Timer*> resolve_timerid(int id);

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)
{
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<u64> ThreadImage::push_string_vector_on_stack(const Vector<String>& vec)