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
+79
View File
@@ -0,0 +1,79 @@
// SPDX-License-Identifier: ISC
#pragma once
#include <zinnia/atomic.h>
#include <zinnia/errno.h>
#include <zinnia/hashmap.h>
#include <zinnia/ident.h>
#include <zinnia/mutex.h>
#include <zinnia/refcount.h>
#include <zinnia/spin.h>
#include <zinnia/tailq.h>
#include <zinnia/types.h>
#include <zinnia/vfs.h>
#include <stdint.h>
#include <uapi/resource.h>
constexpr size_t PROCESS_DEFAULT_FDS = 1024;
constexpr size_t PROCESS_MAX_FDS = 1 << 20;
struct task;
struct vma;
struct process_rlimit {
_Atomic(rlim_t) cur;
rlim_t max;
};
struct fd_entry {
struct vfs_file* file;
bool cloexec;
};
struct process {
struct refcount rc;
pid_t pid;
struct spinlock tasks_lock;
tailq_head(struct task) tasks;
size_t thread_count;
mutex_t lock;
struct vma* space;
struct vfs_path root;
struct vfs_path cwd;
struct ident identity;
mode_t umask;
int8_t nice;
struct process_rlimit limit_nofile;
mutex_t files_lock;
hashmap(int, struct fd_entry) files;
};
struct process* process_kernel();
void process_ref(struct process* proc);
void process_unref(struct process* proc);
uint8_t process_priority(struct process* proc);
void process_attach_task(struct process* proc, struct task* task);
void process_detach_task(struct process* proc, struct task* task);
errno_t process_spawn_init(const char* path, const char** argv, const char** envp);
size_t process_fd_limit(struct process* proc);
// Allocate a file drescriptor for `file` on `proc`.
errno_t process_fd_alloc(struct process* proc, struct vfs_file* file, bool cloexec, int* out_fd);
// Allocate a file descriptor for `file` on `proc`, starting at at least `min_fd`.
errno_t process_fd_alloc_min(struct process* proc, struct vfs_file* file, int min_fd, bool cloexec, int* out_fd);
// Replaces the current file indentified by `fd` in `proc` with a new `file`.
errno_t process_fd_replace(struct process* proc, int fd, struct vfs_file* file, bool cloexec);
errno_t process_fd_get_cloexec(struct process* proc, int fd, bool* out_cloexec);
errno_t process_fd_set_cloexec(struct process* proc, int fd, bool cloexec);
struct vfs_file* process_fd_get(struct process* proc, int fd);
errno_t process_fd_close(struct process* proc, int fd);
+6 -1
View File
@@ -40,6 +40,7 @@ constexpr size_t SCHED_TIME_SLICE_NS = 10'000'000;
constexpr uint8_t SCHED_FEEDBACK_DEPTH = 8;
constexpr uint64_t SCHED_BOOST_INTERVAL_NS = 1'000'000'000;
struct process;
struct turnstile;
constexpr size_t TASK_NAME_MAX = 128;
@@ -49,6 +50,8 @@ struct task {
size_t id;
char name[TASK_NAME_MAX];
struct vma* space;
struct process* process;
tailq_link(struct task) process_link;
enum task_state state;
struct arch_task_context context;
struct usercopy_region* usercopy_region;
@@ -105,7 +108,7 @@ struct scheduler {
typedef void (*task_fn_t)(uintptr_t arg0);
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);
void task_destroy(struct task* task);
void task_lock(struct task* task);
void task_unlock(struct task* task);
@@ -127,6 +130,8 @@ void sched_wake(struct task* task);
// Same as sched_wake(), but the task must be locked with task_lock().
void sched_wake_locked(struct task* task);
void sched_notify(struct task* task);
[[noreturn]]
void sched_exit();