2022-10-12 15:42:01 +00:00
|
|
|
#define MODULE "exec"
|
|
|
|
|
|
|
|
#include "errno.h"
|
|
|
|
#include "interrupts/Interrupts.h"
|
2022-10-15 10:56:48 +00:00
|
|
|
#include "kassert.h"
|
2022-10-16 16:23:33 +00:00
|
|
|
#include "log/Log.h"
|
2022-10-12 15:42:01 +00:00
|
|
|
#include "memory/MemoryManager.h"
|
2022-10-12 17:22:08 +00:00
|
|
|
#include "memory/PMM.h"
|
2022-10-13 20:13:04 +00:00
|
|
|
#include "memory/VMM.h"
|
2022-10-12 16:04:20 +00:00
|
|
|
#include "std/stdlib.h"
|
|
|
|
#include "std/string.h"
|
2022-10-13 20:13:04 +00:00
|
|
|
#include "sys/Syscall.h"
|
2022-10-12 15:42:01 +00:00
|
|
|
#include "sys/elf/ELFLoader.h"
|
|
|
|
#include "thread/Scheduler.h"
|
|
|
|
|
2022-10-17 16:43:35 +00:00
|
|
|
void sys_fork(Context* context)
|
2022-10-12 15:42:01 +00:00
|
|
|
{
|
2022-10-17 16:43:35 +00:00
|
|
|
kinfoln("fork(): attempting fork");
|
|
|
|
|
|
|
|
Task* parent = Scheduler::current_task();
|
|
|
|
|
|
|
|
Task* child = Scheduler::create_user_task();
|
|
|
|
if (!child)
|
|
|
|
{
|
|
|
|
context->rax = -ENOMEM;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!child->allocator.inherit(parent->allocator))
|
|
|
|
{
|
|
|
|
child->state = child->Exited;
|
|
|
|
child->exit_status = -127; // so the reaper reaps it on next reaping
|
|
|
|
context->rax = -ENOMEM;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
child->save_context(context);
|
|
|
|
child->save_floating();
|
2022-10-14 14:46:00 +00:00
|
|
|
|
2022-10-17 16:43:35 +00:00
|
|
|
for (int i = 0; i < TASK_MAX_FDS; i++) { child->files[i] = parent->files[i]; }
|
|
|
|
|
|
|
|
child->address_space = parent->address_space.clone();
|
|
|
|
|
|
|
|
child->regs.rax = 0;
|
|
|
|
context->rax = child->id;
|
|
|
|
|
2022-10-17 17:12:47 +00:00
|
|
|
strlcpy(child->name, parent->name, sizeof(child->name));
|
|
|
|
|
2022-10-17 16:43:35 +00:00
|
|
|
child->state = child->Running;
|
|
|
|
|
|
|
|
kinfoln("fork(): forked parent %ld into child %ld", parent->id, child->id);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void sys_exec(Context* context, const char* pathname)
|
|
|
|
{
|
2022-10-14 16:00:33 +00:00
|
|
|
char* kpathname = Syscall::strdup_from_user(pathname);
|
2022-10-13 20:13:04 +00:00
|
|
|
if (!kpathname)
|
2022-10-12 15:42:01 +00:00
|
|
|
{
|
2022-10-12 17:25:35 +00:00
|
|
|
context->rax = -EFAULT;
|
2022-10-12 15:42:01 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-13 20:13:04 +00:00
|
|
|
kinfoln("exec(): executing %s", kpathname);
|
2022-10-12 15:42:01 +00:00
|
|
|
|
2022-10-13 20:13:04 +00:00
|
|
|
VFS::Node* program = VFS::resolve_path(kpathname);
|
2022-10-12 15:42:01 +00:00
|
|
|
if (!program)
|
|
|
|
{
|
2022-10-13 20:13:04 +00:00
|
|
|
kfree(kpathname);
|
2022-10-12 15:42:01 +00:00
|
|
|
context->rax = -ENOENT;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-12 16:43:48 +00:00
|
|
|
if (program->type == VFS_DIRECTORY)
|
2022-10-12 15:42:01 +00:00
|
|
|
{
|
2022-10-13 20:13:04 +00:00
|
|
|
kfree(kpathname);
|
2022-10-12 16:43:48 +00:00
|
|
|
context->rax = -EISDIR;
|
2022-10-12 15:42:01 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-12 17:22:08 +00:00
|
|
|
long memusage;
|
|
|
|
if ((memusage = ELFLoader::check_elf_image(program)) < 0)
|
2022-10-12 15:42:01 +00:00
|
|
|
{
|
2022-10-13 20:13:04 +00:00
|
|
|
kfree(kpathname);
|
2022-10-12 17:15:44 +00:00
|
|
|
context->rax = -ENOEXEC;
|
2022-10-12 16:43:48 +00:00
|
|
|
return;
|
2022-10-12 15:42:01 +00:00
|
|
|
}
|
|
|
|
|
2022-10-12 18:02:25 +00:00
|
|
|
if ((uint64_t)memusage > PMM::get_free())
|
2022-10-12 17:22:08 +00:00
|
|
|
{
|
2022-10-13 20:13:04 +00:00
|
|
|
kfree(kpathname);
|
2022-10-12 17:22:08 +00:00
|
|
|
context->rax = -ENOMEM;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-12 15:42:01 +00:00
|
|
|
Interrupts::disable();
|
|
|
|
ASSERT(!Interrupts::are_enabled()); // This part is pretty sensitive.
|
|
|
|
|
|
|
|
Task* task = Scheduler::current_task();
|
|
|
|
ASSERT(task);
|
|
|
|
|
|
|
|
// At this point, pretty much nothing can fail.
|
|
|
|
|
2022-10-17 16:43:35 +00:00
|
|
|
task->allocator.free();
|
|
|
|
task->allocator
|
|
|
|
.init(); // If we had enough space for the old bitmap, we should have enough space for the new bitmap.
|
|
|
|
|
|
|
|
task->address_space.clear();
|
|
|
|
task->allocated_stack = (uint64_t)MemoryManager::get_pages_at(
|
|
|
|
0x100000, TASK_PAGES_IN_STACK,
|
|
|
|
MAP_USER | MAP_READ_WRITE); // If we had enough space for the old stack, there should be enough space for the
|
|
|
|
// new stack.
|
|
|
|
|
2022-10-12 16:43:48 +00:00
|
|
|
ELFImage* image = ELFLoader::load_elf_from_vfs(program);
|
|
|
|
ASSERT(image); // If check_elf_image succeeded, load_elf_from_vfs MUST succeed, unless something has gone terribly
|
|
|
|
// wrong.
|
|
|
|
|
2022-10-17 17:12:47 +00:00
|
|
|
strlcpy(task->name, kpathname, sizeof(task->name));
|
|
|
|
|
2022-10-12 15:42:01 +00:00
|
|
|
Scheduler::reset_task(task, image);
|
|
|
|
|
2022-10-17 15:07:25 +00:00
|
|
|
task->restore_context(context);
|
2022-10-12 15:42:01 +00:00
|
|
|
|
2022-10-13 20:13:04 +00:00
|
|
|
kfree(kpathname);
|
|
|
|
|
2022-10-14 16:00:33 +00:00
|
|
|
return;
|
2022-10-12 15:42:01 +00:00
|
|
|
}
|