All checks were successful
Build documentation / build-and-deploy (push) Successful in 44s
127 lines
3.4 KiB
C
127 lines
3.4 KiB
C
#include <amd64/gdt.h>
|
|
#include <amd64/proc.h>
|
|
#include <aux/elf.h>
|
|
#include <libk/list.h>
|
|
#include <libk/rbtree.h>
|
|
#include <libk/std.h>
|
|
#include <libk/string.h>
|
|
#include <limine/requests.h>
|
|
#include <mm/liballoc.h>
|
|
#include <mm/pmm.h>
|
|
#include <proc/proc.h>
|
|
#include <proc/procgroup.h>
|
|
#include <proc/resource.h>
|
|
#include <sync/rw_spin_lock.h>
|
|
#include <sync/spin_lock.h>
|
|
#include <sys/debug.h>
|
|
|
|
/* 0 is kpproc */
|
|
static atomic_int pids = 1;
|
|
|
|
struct proc* proc_from_elf (uint8_t* elf_contents) {
|
|
struct limine_hhdm_response* hhdm = limine_hhdm_request.response;
|
|
|
|
struct proc* proc = malloc (sizeof (*proc));
|
|
if (proc == NULL)
|
|
return NULL;
|
|
|
|
memset (proc, 0, sizeof (*proc));
|
|
|
|
proc->lock = SPIN_LOCK_INIT;
|
|
atomic_store (&proc->state, PROC_READY);
|
|
proc->pid = atomic_fetch_add (&pids, 1);
|
|
|
|
proc->procgroup = procgroup_create ();
|
|
if (proc->procgroup == NULL) {
|
|
free (proc);
|
|
return NULL;
|
|
}
|
|
procgroup_attach (proc->procgroup, proc);
|
|
|
|
uintptr_t kstack_paddr = pmm_alloc (KSTACK_SIZE / PAGE_SIZE);
|
|
proc->pdata.kernel_stack = kstack_paddr + (uintptr_t)hhdm->offset + KSTACK_SIZE;
|
|
|
|
procgroup_map (proc->procgroup, PROC_USTACK_TOP - USTACK_SIZE, USTACK_SIZE / PAGE_SIZE,
|
|
MM_PG_USER | MM_PG_PRESENT | MM_PG_RW, NULL);
|
|
|
|
proc->flags |= PROC_USTK_PREALLOC;
|
|
|
|
struct elf_aux aux = proc_load_segments (proc, elf_contents);
|
|
|
|
proc->pdata.regs.ss = GDT_UDATA | 0x03;
|
|
proc->pdata.regs.rsp = (uint64_t)PROC_USTACK_TOP;
|
|
proc->pdata.regs.rflags = 0x202;
|
|
proc->pdata.regs.cs = GDT_UCODE | 0x03;
|
|
proc->pdata.regs.rip = aux.entry;
|
|
|
|
return proc;
|
|
}
|
|
|
|
struct proc* proc_clone (struct proc* proto, uintptr_t vstack_top, size_t stack_size,
|
|
uintptr_t entry) {
|
|
struct limine_hhdm_response* hhdm = limine_hhdm_request.response;
|
|
spin_lock_ctx_t ctxprt;
|
|
|
|
struct proc* proc = malloc (sizeof (*proc));
|
|
if (proc == NULL)
|
|
return NULL;
|
|
|
|
memset (proc, 0, sizeof (*proc));
|
|
|
|
proc->lock = SPIN_LOCK_INIT;
|
|
atomic_store (&proc->state, PROC_READY);
|
|
proc->pid = atomic_fetch_add (&pids, 1);
|
|
|
|
spin_lock (&proto->lock, &ctxprt);
|
|
|
|
proc->procgroup = proto->procgroup;
|
|
procgroup_attach (proc->procgroup, proc);
|
|
|
|
spin_unlock (&proto->lock, &ctxprt);
|
|
|
|
uintptr_t kstack_paddr = pmm_alloc (KSTACK_SIZE / PAGE_SIZE);
|
|
proc->pdata.kernel_stack = kstack_paddr + (uintptr_t)hhdm->offset + KSTACK_SIZE;
|
|
|
|
proc->pdata.regs.ss = GDT_UDATA | 0x03;
|
|
proc->pdata.regs.rsp = (uint64_t)vstack_top;
|
|
proc->pdata.regs.rflags = 0x202;
|
|
proc->pdata.regs.cs = GDT_UCODE | 0x03;
|
|
proc->pdata.regs.rip = (uint64_t)entry;
|
|
|
|
return proc;
|
|
}
|
|
|
|
void proc_cleanup (struct proc* proc) {
|
|
spin_lock_ctx_t ctxsq, ctxpr;
|
|
|
|
spin_lock (&proc->lock, &ctxpr);
|
|
|
|
/* clean suspension queue entries */
|
|
struct list_node_link *sq_link, *sq_link_tmp;
|
|
list_foreach (proc->sq_entries, sq_link, sq_link_tmp) {
|
|
struct proc_sq_entry* sq_entry = list_entry (sq_link, struct proc_sq_entry, proc_link);
|
|
struct proc_suspension_q* sq = sq_entry->sq;
|
|
|
|
spin_lock (&sq->lock, &ctxsq);
|
|
|
|
/* remove from sq's list */
|
|
list_remove (sq->proc_list, &sq_entry->sq_link);
|
|
|
|
/* remove from proc's list */
|
|
list_remove (proc->sq_entries, &sq_entry->proc_link);
|
|
|
|
spin_unlock (&sq->lock, &ctxsq);
|
|
|
|
free (sq_entry);
|
|
}
|
|
|
|
spin_unlock (&proc->lock, &ctxpr);
|
|
|
|
procgroup_detach (proc->procgroup, proc);
|
|
|
|
pmm_free (proc->pdata.kernel_stack, KSTACK_SIZE / PAGE_SIZE);
|
|
|
|
/* clean the process */
|
|
free (proc);
|
|
}
|