process: Rewrite ELF loading, processes

This commit is contained in:
2026-10-25 19:12:24 +01:00
parent 9991c1ddcb
commit 0e13d1c0b4
11 changed files with 697 additions and 117 deletions
+21 -9
View File
@@ -4,6 +4,7 @@
#include <zinnia/assert.h>
#include <zinnia/atomic.h>
#include <zinnia/errno.h>
#include <zinnia/process.h>
#include <zinnia/sched.h>
#include <zinnia/spin.h>
#include <zinnia/stack.h>
@@ -25,7 +26,9 @@ void sched_to_user(uintptr_t info_ptr) {
}
void sched_to_user_context(uintptr_t ctx) {
arch_sched_jump_to_context((struct arch_context*)ctx);
struct arch_context context = *(struct arch_context*)ctx;
mem_free((void*)ctx);
arch_sched_jump_to_context(&context);
}
static struct spinlock task_cache_lock;
@@ -54,20 +57,25 @@ static void task_release(struct task* task) {
spin_unlock(&task_cache_lock);
}
static void task_init_common(struct task* task, const char* name, struct vma* space) {
task->space = space;
static void task_init_common(struct task* task, const char* name, struct process* proc) {
task->process = proc;
task->space = proc->space;
process_ref(proc);
if (task->space != &kernel_vma)
vma_ref(task->space);
spin_init(&task->lock);
atomic_store(&task->lockp, &task->lock, ATOMIC_RELAXED);
strncpy(task->name, name, sizeof(task->name));
task->base_prio = SCHED_PRIO_DEFAULT;
task->base_prio = process_priority(proc);
task->time_slice = SCHED_TIME_SLICE_NS;
task->penalty = 0;
task->feedback_epoch = 0;
atomic_store(&task->priority, SCHED_PRIO_DEFAULT, ATOMIC_RELAXED);
atomic_store(&task->priority, task->base_prio, ATOMIC_RELAXED);
tailq_init(&task->pi_lenders);
process_attach_task(proc, task);
}
errno_t task_create_current(const char* name, struct vma* space, struct task** out) {
errno_t task_create_current(const char* name, struct process* proc, struct task** out) {
struct task* task = task_alloc();
if (task == nullptr)
return ENOMEM;
@@ -78,14 +86,14 @@ errno_t task_create_current(const char* name, struct vma* space, struct task** o
return ENOMEM;
}
task_init_common(task, name, space);
task_init_common(task, name, proc);
*out = task;
return 0;
}
constexpr size_t KERNEL_STACK_SIZE = 16 * 1024;
errno_t task_create(const char* name, struct vma* space, task_fn_t entry, uintptr_t arg0, struct task** out) {
errno_t task_create(const char* name, struct process* proc, task_fn_t entry, uintptr_t arg0, struct task** out) {
if (!out)
return EINVAL;
@@ -119,7 +127,7 @@ errno_t task_create(const char* name, struct vma* space, task_fn_t entry, uintpt
return s;
}
task_init_common(new_task, name, space);
task_init_common(new_task, name, proc);
*out = new_task;
return 0;
}
@@ -129,6 +137,10 @@ void task_destroy(struct task* task) {
turnstile_free(task->turnstile);
arch_task_fini(&task->context);
stack_destroy(&task->kernel_stack);
process_detach_task(task->process, task);
if (task->space != &kernel_vma)
vma_unref(task->space);
process_unref(task->process);
task_release(task);
}
+1 -1
View File
@@ -4,6 +4,6 @@
#include <zinnia/sched.h>
errno_t task_create_current(const char* name, struct vma* space, struct task** out);
errno_t task_create_current(const char* name, struct process* proc, struct task** out);
bool sched_update_priority_locked(struct task* task);
+15 -2
View File
@@ -8,6 +8,7 @@
#include <zinnia/percpu.h>
#include <zinnia/pmap.h>
#include <zinnia/print.h>
#include <zinnia/process.h>
#include <zinnia/sched.h>
#include <zinnia/spin.h>
#include <zinnia/stack.h>
@@ -57,12 +58,12 @@ void sched_init(struct scheduler* sched) {
tailq_init(&sched->run_queue[i]);
struct task* idle_task;
assert(task_create("idle", &kernel_vma, idle_func, 0, &idle_task) == 0, "Unable to create idle task!");
assert(task_create("idle", process_kernel(), idle_func, 0, &idle_task) == 0, "Unable to create idle task!");
idle_task->last_rq = sched;
sched->idle_task = idle_task;
struct task* boot_task;
assert(task_create_current("boot", &kernel_vma, &boot_task) == 0, "Unable to create boot task!");
assert(task_create_current("boot", process_kernel(), &boot_task) == 0, "Unable to create boot task!");
boot_task->state = TASK_STATE_RUNNING;
boot_task->last_rq = sched;
atomic_store(&boot_task->on_cpu, true, ATOMIC_RELAXED);
@@ -326,6 +327,18 @@ void sched_wake(struct task* task) {
task_unlock(task);
}
void sched_notify(struct task* task) {
task_lock(task);
if (task->state == TASK_STATE_BLOCKED) {
enqueue(select_cpu(task), task);
} else {
atomic_store(&task->wake_pending, true, ATOMIC_RELEASE);
if (task->state == TASK_STATE_RUNNING && task->last_rq && task->last_rq != &percpu_get()->sched)
kick(task->last_rq);
}
task_unlock(task);
}
bool sched_update_priority_locked(struct task* task) {
struct scheduler* sched = task->last_rq;
if (sched == nullptr) {