Move platform-specific code for process loading/init for AMD64 to amd64/
All checks were successful
Build documentation / build-and-deploy (push) Successful in 49s

This commit is contained in:
2026-01-01 20:08:37 +01:00
parent 5e6bdcc52d
commit 121fb3b33c
7 changed files with 92 additions and 64 deletions

55
kernel/amd64/proc.c Normal file
View File

@@ -0,0 +1,55 @@
#include <libk/std.h>
#include <libk/string.h>
#include <sync/spin_lock.h>
#include <mm/pmm.h>
#include <mm/liballoc.h>
#include <proc/proc.h>
#include <aux/elf.h>
struct proc* proc_from_elf (uint8_t* elf_contents) {
struct proc* proc = malloc (sizeof (*proc));
if (proc == NULL)
return NULL;
memset (proc, 0, sizeof (*proc));
proc->pd.lock = SPIN_LOCK_INIT;
proc->pd.cr3_paddr = mm_alloc_user_pd_phys ();
if (proc->pd.cr3_paddr == 0) {
free (proc);
return NULL;
}
proc->pdata.syscall_stack = pmm_alloc (KSTACK_SIZE / PAGE_SIZE);
if (proc->pdata.syscall_stack == PMM_ALLOC_ERR) {
free (proc);
return NULL;
}
proc->pdata.user_stack = pmm_alloc (USTACK_SIZE / PAGE_SIZE);
if (proc->pdata.user_stack == PMM_ALLOC_ERR) {
free (proc);
pmm_free (proc->pdata.syscall_stack, USTACK_SIZE / PAGE_SIZE);
return NULL;
}
uintptr_t user_stack = proc->pdata.user_stack;
proc->pdata.syscall_stack += KSTACK_SIZE;
proc->pdata.user_stack += USTACK_SIZE;
proc_map (proc, user_stack, PROC_USTACK_TOP - USTACK_SIZE, USTACK_SIZE / PAGE_SIZE,
MM_PG_USER | MM_PG_PRESENT | MM_PG_RW);
struct elf_aux aux = proc_load_segments (proc, elf_contents);
proc->pdata.regs.ss = 0x20 | 0x03;
proc->pdata.regs.rsp = (uint64_t)PROC_USTACK_TOP;
proc->pdata.regs.rflags = 0x202;
proc->pdata.regs.cs = 0x18 | 0x03;
proc->pdata.regs.rip = aux.entry;
proc->lock = SPIN_LOCK_INIT;
atomic_store (&proc->state, PROC_READY);
return proc;
}

View File

@@ -10,7 +10,8 @@ c += amd64/bootmain.c \
amd64/mm.c \
amd64/time.c \
amd64/smp.c \
amd64/sched1.c
amd64/sched1.c \
amd64/proc.c
S += amd64/intr_stub.S \
amd64/spin.S \
@@ -31,4 +32,5 @@ o += amd64/bootmain.o \
amd64/time.o \
amd64/smp.o \
amd64/sched.o \
amd64/sched1.o
amd64/sched1.o \
amd64/proc.o