kernel: Move copy_string_vector_to_userspace to ThreadImage

This commit is contained in:
apio 2023-04-07 14:36:24 +02:00
parent b22bea84ec
commit 3a70accdeb
Signed by: apio
GPG Key ID: B8A7D06E42258954
3 changed files with 18 additions and 16 deletions

View File

@ -31,21 +31,6 @@ static Result<Vector<String>> copy_string_vector_from_userspace(u64 address)
return result;
}
static Result<u64> copy_string_vector_to_userspace(const Vector<String>& vec, ThreadImage& image)
{
Vector<u64> user_vec;
for (const auto& item : vec)
{
// Copy each individual string and retrieve a userspace pointer to said copy
u64 addr = TRY(image.push_mem_on_stack((const u8*)item.chars(), item.length() + 1));
TRY(user_vec.try_append(addr));
}
TRY(user_vec.try_append((u64) nullptr));
// Copy the actual vector of userspace pointers to the stack
return TRY(image.push_mem_on_stack((u8*)user_vec.data(), user_vec.size() * sizeof(u64)));
}
Result<u64> sys_exec(Registers* regs, SyscallArgs args)
{
auto path = TRY(MemoryManager::strdup_from_user(args[0]));
@ -68,7 +53,7 @@ Result<u64> sys_exec(Registers* regs, SyscallArgs args)
kdbgln("exec: copying argv to image memory (argc = %zu)", argv.size());
u64 user_argv = TRY(copy_string_vector_to_userspace(argv, *image));
u64 user_argv = TRY(image->push_string_vector_on_stack(argv));
usize user_argc = argv.size();
// From now on, nothing should fail.

View File

@ -92,6 +92,21 @@ Result<u64> ThreadImage::push_mem_on_stack(const u8* mem, usize size)
return m_sp;
}
Result<u64> ThreadImage::push_string_vector_on_stack(const Vector<String>& vec)
{
Vector<u64> user_vec;
for (const auto& item : vec)
{
// Copy each individual string and retrieve a userspace pointer to said copy
u64 addr = TRY(push_mem_on_stack((const u8*)item.chars(), item.length() + 1));
TRY(user_vec.try_append(addr));
}
TRY(user_vec.try_append((u64) nullptr));
// Copy the actual vector of userspace pointers to the stack
return TRY(push_mem_on_stack((u8*)user_vec.data(), user_vec.size() * sizeof(u64)));
}
void ThreadImage::apply(Thread* thread)
{
thread->init_regs_user();

View File

@ -10,6 +10,7 @@
#include <luna/OwnedPtr.h>
#include <luna/Result.h>
#include <luna/Stack.h>
#include <luna/Vector.h>
class Thread;
@ -21,6 +22,7 @@ class ThreadImage
static Result<OwnedPtr<ThreadImage>> clone_from_thread(Thread* parent);
Result<u64> push_mem_on_stack(const u8* mem, usize size);
Result<u64> push_string_vector_on_stack(const Vector<String>& vec);
void apply(Thread* thread);