Rewrite resource subsystem
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
#include <amd64/gdt.h>
|
||||
#include <amd64/proc.h>
|
||||
#include <aux/elf.h>
|
||||
#include <libk/list.h>
|
||||
#include <libk/rbtree.h>
|
||||
@@ -9,13 +10,16 @@
|
||||
#include <mm/pmm.h>
|
||||
#include <proc/proc.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;
|
||||
int rid;
|
||||
|
||||
struct proc* proc = malloc (sizeof (*proc));
|
||||
if (proc == NULL)
|
||||
@@ -27,51 +31,59 @@ struct proc* proc_from_elf (uint8_t* elf_contents) {
|
||||
atomic_store (&proc->state, PROC_READY);
|
||||
proc->pid = atomic_fetch_add (&pids, 1);
|
||||
|
||||
proc->resources = malloc (sizeof (*proc->resources));
|
||||
if (proc->resources == NULL) {
|
||||
free (proc);
|
||||
return NULL;
|
||||
}
|
||||
proc->resources->tree = NULL;
|
||||
proc->resources->lock = RW_SPIN_LOCK_INIT;
|
||||
proc->resources->refs = 1;
|
||||
proc->resources->sys_rids = 0;
|
||||
|
||||
proc->pd = malloc (sizeof (*proc->pd));
|
||||
if (proc->pd == NULL) {
|
||||
free (proc->resources);
|
||||
free (proc);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
proc->sys_rids = malloc (sizeof (*proc->sys_rids));
|
||||
if (proc->sys_rids == NULL) {
|
||||
free (proc);
|
||||
return NULL;
|
||||
}
|
||||
proc->sys_rids->counter = 0;
|
||||
proc->sys_rids->refs = 1;
|
||||
|
||||
proc->pd->lock = SPIN_LOCK_INIT;
|
||||
proc->pd->refs = 1;
|
||||
proc->pd->cr3_paddr = mm_alloc_user_pd_phys ();
|
||||
if (proc->pd->cr3_paddr == 0) {
|
||||
free (proc->pd);
|
||||
free (proc->resources);
|
||||
free (proc);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int kstk_rid = atomic_fetch_add (&proc->sys_rids->counter, 1);
|
||||
struct proc_resource_mem_init kstk_mem_init = {.pages = KSTACK_SIZE / PAGE_SIZE};
|
||||
struct proc_resource_mem_init kstk_mem_init = {.pages = KSTACK_SIZE / PAGE_SIZE,
|
||||
.managed = false};
|
||||
rid = atomic_fetch_add (&proc->resources->sys_rids, 1);
|
||||
struct proc_resource* kstk_r =
|
||||
proc_create_resource (proc, kstk_rid, PR_MEM, RV_PRIVATE, (void*)&kstk_mem_init);
|
||||
proc_create_resource (proc, rid, PR_MEM, RV_PRIVATE, (void*)&kstk_mem_init);
|
||||
if (kstk_r == NULL) {
|
||||
pmm_free (proc->pd->cr3_paddr, 1);
|
||||
free (proc->pd);
|
||||
free (proc->resources);
|
||||
free (proc);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
proc->pdata.kernel_stack = kstk_r->u.mem.paddr + (uintptr_t)hhdm->offset + KSTACK_SIZE;
|
||||
|
||||
int ustk_rid = atomic_fetch_add (&proc->sys_rids->counter, 1);
|
||||
struct proc_resource_mem_init ustk_mem_init = {.pages = USTACK_SIZE / PAGE_SIZE};
|
||||
struct proc_resource_mem_init ustk_mem_init = {.pages = USTACK_SIZE / PAGE_SIZE,
|
||||
.managed = false};
|
||||
rid = atomic_fetch_add (&proc->resources->sys_rids, 1);
|
||||
struct proc_resource* ustk_r =
|
||||
proc_create_resource (proc, ustk_rid, PR_MEM, RV_PRIVATE, (void*)&ustk_mem_init);
|
||||
proc_create_resource (proc, rid, PR_MEM, RV_PRIVATE, (void*)&ustk_mem_init);
|
||||
if (ustk_r == NULL) {
|
||||
kstk_r->ops.cleanup (proc, kstk_r);
|
||||
free (kstk_r);
|
||||
pmm_free (proc->pd->cr3_paddr, 1);
|
||||
free (proc->pd);
|
||||
free (proc->resources);
|
||||
free (proc);
|
||||
return NULL;
|
||||
}
|
||||
@@ -97,7 +109,8 @@ struct proc* proc_from_elf (uint8_t* elf_contents) {
|
||||
struct proc* proc_spawn_thread (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;
|
||||
spin_lock_ctx_t ctxprt, ctxrs;
|
||||
int rid;
|
||||
|
||||
struct proc* proc = malloc (sizeof (*proc));
|
||||
if (proc == NULL)
|
||||
@@ -115,9 +128,26 @@ struct proc* proc_spawn_thread (struct proc* proto, uintptr_t vstack_top, size_t
|
||||
proc->mappings = proto->mappings;
|
||||
atomic_fetch_add (&proto->pd->refs, 1);
|
||||
|
||||
proc->resource_tree = proto->resource_tree;
|
||||
proc->sys_rids = proto->sys_rids;
|
||||
atomic_fetch_add (&proc->sys_rids->refs, 1);
|
||||
proc->resources = proto->resources;
|
||||
|
||||
rw_spin_write_lock (&proc->resources->lock, &ctxrs);
|
||||
|
||||
atomic_fetch_add (&proc->resources->refs, 1);
|
||||
|
||||
struct rb_node_link* rnode;
|
||||
rbtree_first (&proc->resources->tree, rnode);
|
||||
while (rnode) {
|
||||
struct rb_node_link* next;
|
||||
rbtree_next (rnode, next);
|
||||
|
||||
struct proc_resource* resource =
|
||||
rbtree_entry (rnode, struct proc_resource, local_resource_tree_link);
|
||||
atomic_fetch_add (&resource->refs, 1);
|
||||
|
||||
rnode = next;
|
||||
}
|
||||
|
||||
rw_spin_write_unlock (&proc->resources->lock, &ctxrs);
|
||||
|
||||
spin_unlock (&proto->lock, &ctxprt);
|
||||
|
||||
@@ -129,10 +159,11 @@ struct proc* proc_spawn_thread (struct proc* proto, uintptr_t vstack_top, size_t
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int kstk_rid = atomic_fetch_add (&proc->sys_rids->counter, 1);
|
||||
struct proc_resource_mem_init kstk_mem_init = {.pages = KSTACK_SIZE / PAGE_SIZE};
|
||||
struct proc_resource_mem_init kstk_mem_init = {.pages = KSTACK_SIZE / PAGE_SIZE,
|
||||
.managed = false};
|
||||
rid = atomic_fetch_add (&proc->resources->sys_rids, 1);
|
||||
struct proc_resource* kstk_r =
|
||||
proc_create_resource (proc, kstk_rid, PR_MEM, RV_PRIVATE, (void*)&kstk_mem_init);
|
||||
proc_create_resource (proc, rid, PR_MEM, RV_PRIVATE, (void*)&kstk_mem_init);
|
||||
if (kstk_r == NULL) {
|
||||
free (proc);
|
||||
return NULL;
|
||||
|
||||
Reference in New Issue
Block a user