2023-03-16 21:44:58 +00:00
|
|
|
#include "ELF.h"
|
|
|
|
#include "Log.h"
|
|
|
|
#include "fs/VFS.h"
|
|
|
|
#include "memory/MemoryManager.h"
|
|
|
|
#include "sys/Syscall.h"
|
|
|
|
#include "thread/Scheduler.h"
|
|
|
|
#include "thread/ThreadImage.h"
|
|
|
|
#include <bits/modes.h>
|
2023-03-24 20:19:24 +00:00
|
|
|
#include <bits/open-flags.h>
|
2023-03-16 21:44:58 +00:00
|
|
|
#include <luna/CString.h>
|
|
|
|
#include <luna/ScopeGuard.h>
|
2023-03-18 20:55:16 +00:00
|
|
|
#include <luna/Vector.h>
|
|
|
|
|
2023-03-29 15:28:22 +00:00
|
|
|
static Result<Vector<String>> copy_string_vector_from_userspace(u64 address)
|
2023-03-18 20:55:16 +00:00
|
|
|
{
|
2023-03-29 15:28:22 +00:00
|
|
|
Vector<String> result;
|
2023-03-18 20:55:16 +00:00
|
|
|
|
|
|
|
const u64* user_vector = (const u64*)address;
|
|
|
|
|
2023-03-18 20:56:34 +00:00
|
|
|
u64 string_addr;
|
2023-03-18 20:55:16 +00:00
|
|
|
while (true)
|
|
|
|
{
|
2023-03-18 20:56:34 +00:00
|
|
|
if (!MemoryManager::copy_from_user_typed(user_vector, &string_addr)) return err(EFAULT);
|
|
|
|
if (!string_addr) break;
|
2023-03-18 20:55:16 +00:00
|
|
|
|
2023-03-18 20:56:34 +00:00
|
|
|
auto string = TRY(MemoryManager::strdup_from_user(string_addr));
|
2023-03-18 20:55:16 +00:00
|
|
|
TRY(result.try_append(move(string)));
|
|
|
|
user_vector++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2023-03-16 21:44:58 +00:00
|
|
|
|
2023-04-07 13:03:38 +00:00
|
|
|
Result<u64> sys_execve(Registers* regs, SyscallArgs args)
|
2023-03-16 21:44:58 +00:00
|
|
|
{
|
|
|
|
auto path = TRY(MemoryManager::strdup_from_user(args[0]));
|
2023-03-18 20:55:16 +00:00
|
|
|
auto argv = TRY(copy_string_vector_from_userspace(args[1]));
|
2023-04-07 13:03:38 +00:00
|
|
|
auto envp = TRY(copy_string_vector_from_userspace(args[2]));
|
2023-03-18 20:55:16 +00:00
|
|
|
|
2023-04-08 11:12:49 +00:00
|
|
|
auto current = Scheduler::current();
|
|
|
|
|
2023-04-07 13:03:38 +00:00
|
|
|
// FIXME: Make sure argv & envp are not too big.
|
2023-03-16 21:44:58 +00:00
|
|
|
|
2023-04-08 11:12:49 +00:00
|
|
|
auto inode = TRY(VFS::resolve_path(path.chars(), current->auth));
|
2023-03-16 21:44:58 +00:00
|
|
|
|
2023-04-08 11:12:49 +00:00
|
|
|
if (!VFS::can_execute(inode, current->auth)) return err(EACCES);
|
2023-03-16 21:44:58 +00:00
|
|
|
|
|
|
|
kinfoln("exec: attempting to replace current image with %s", path.chars());
|
|
|
|
|
2023-03-18 21:25:19 +00:00
|
|
|
auto guard = make_scope_guard([current] { MMU::switch_page_directory(current->directory); });
|
|
|
|
|
2023-03-16 21:44:58 +00:00
|
|
|
auto image = TRY(ThreadImage::try_load_from_elf(inode));
|
|
|
|
|
2023-03-18 21:25:19 +00:00
|
|
|
kdbgln("exec: copying argv to image memory (argc = %zu)", argv.size());
|
|
|
|
|
2023-04-07 12:36:24 +00:00
|
|
|
u64 user_argv = TRY(image->push_string_vector_on_stack(argv));
|
2023-03-18 21:25:19 +00:00
|
|
|
usize user_argc = argv.size();
|
|
|
|
|
2023-04-07 13:03:38 +00:00
|
|
|
kdbgln("exec: copying envp to image memory (envc = %zu)", envp.size());
|
|
|
|
|
|
|
|
u64 user_envp = TRY(image->push_string_vector_on_stack(envp));
|
|
|
|
usize user_envc = envp.size();
|
|
|
|
|
2023-03-16 21:44:58 +00:00
|
|
|
// From now on, nothing should fail.
|
|
|
|
|
|
|
|
kinfoln("exec: image load ok, will now replace existing process image");
|
|
|
|
|
2023-03-18 21:25:19 +00:00
|
|
|
guard.deactivate();
|
|
|
|
|
2023-03-24 20:19:24 +00:00
|
|
|
for (int i = 0; i < FD_MAX; i++)
|
|
|
|
{
|
|
|
|
auto& descriptor = current->fd_table[i];
|
2023-03-24 20:21:13 +00:00
|
|
|
if (!descriptor.has_value()) continue;
|
2023-03-24 20:19:24 +00:00
|
|
|
if (descriptor->flags & O_CLOEXEC) descriptor = {};
|
|
|
|
}
|
2023-03-16 21:44:58 +00:00
|
|
|
|
|
|
|
MMU::delete_userspace_page_directory(current->directory);
|
|
|
|
|
2023-03-24 20:26:45 +00:00
|
|
|
current->name = path.chars();
|
2023-03-24 20:05:38 +00:00
|
|
|
|
2023-03-16 21:44:58 +00:00
|
|
|
image->apply(current);
|
|
|
|
|
|
|
|
MMU::switch_page_directory(current->directory);
|
|
|
|
|
2023-04-07 13:03:38 +00:00
|
|
|
current->set_arguments(user_argc, user_argv, user_envc, user_envp);
|
2023-03-18 21:25:19 +00:00
|
|
|
|
2023-03-16 21:44:58 +00:00
|
|
|
memcpy(regs, ¤t->regs, sizeof(*regs));
|
|
|
|
|
|
|
|
kinfoln("exec: done");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2023-03-18 22:45:48 +00:00
|
|
|
|
|
|
|
Result<u64> sys_fork(Registers* regs, SyscallArgs)
|
|
|
|
{
|
|
|
|
auto current = Scheduler::current();
|
|
|
|
|
|
|
|
auto guard = make_scope_guard([current] { MMU::switch_page_directory(current->directory); });
|
|
|
|
|
|
|
|
kinfoln("fork: trying to duplicate process %lu", current->id);
|
|
|
|
|
|
|
|
memcpy(¤t->regs, regs, sizeof(*regs));
|
|
|
|
|
|
|
|
auto image = TRY(ThreadImage::clone_from_thread(current));
|
|
|
|
|
|
|
|
auto thread = TRY(new_thread());
|
|
|
|
|
|
|
|
thread->state = ThreadState::Runnable;
|
|
|
|
thread->is_kernel = false;
|
2023-03-24 16:37:04 +00:00
|
|
|
thread->parent_id = current->id;
|
2023-03-18 22:45:48 +00:00
|
|
|
thread->fp_data.save();
|
2023-04-07 12:02:36 +00:00
|
|
|
thread->name = current->name;
|
2023-04-08 11:12:49 +00:00
|
|
|
thread->auth = current->auth;
|
2023-03-18 22:45:48 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < FD_MAX; i++) { thread->fd_table[i] = current->fd_table[i]; }
|
|
|
|
|
|
|
|
image->apply(thread);
|
|
|
|
|
|
|
|
memcpy(&thread->regs, regs, sizeof(*regs));
|
|
|
|
|
|
|
|
thread->set_return(0);
|
|
|
|
|
|
|
|
Scheduler::add_thread(thread);
|
|
|
|
|
|
|
|
return thread->id;
|
|
|
|
}
|