Introduce concept of Process Resources (PR_MEM), implement necessary syscalls
All checks were successful
Build documentation / build-and-deploy (push) Successful in 42s

This commit is contained in:
2026-01-07 22:47:30 +01:00
parent 28aef30f77
commit d7b734306f
16 changed files with 451 additions and 50 deletions

View File

@@ -10,6 +10,7 @@
#include <mm/liballoc.h>
#include <mm/pmm.h>
#include <proc/proc.h>
#include <proc/resource.h>
#include <rd/rd.h>
#include <sync/spin_lock.h>
#include <sys/debug.h>
@@ -40,9 +41,6 @@ static bool proc_check_elf (uint8_t* elf) {
bool proc_map (struct proc* proc, uintptr_t start_paddr, uintptr_t start_vaddr, size_t pages,
uint32_t flags) {
DEBUG ("start_vaddr=%p, start_paddr=%p, pages=%zu, flags=%x\n", start_vaddr, start_paddr, pages,
flags);
struct proc_mapping* mapping = malloc (sizeof (*mapping));
if (mapping == NULL)
@@ -74,8 +72,6 @@ bool proc_unmap (struct proc* proc, uintptr_t start_vaddr, size_t pages) {
struct list_node_link *mapping_link, *mapping_link_tmp;
bool used_tail_mapping = false;
DEBUG ("start_vaddr=%p, pages=%zu\n", start_vaddr, pages);
struct proc_mapping* tail_mapping = malloc (sizeof (*tail_mapping));
if (tail_mapping == NULL)
return false;
@@ -154,9 +150,15 @@ struct elf_aux proc_load_segments (struct proc* proc, uint8_t* elf) {
size_t blks = div_align_up (phdr->p_memsz + off, PAGE_SIZE);
uintptr_t p_addr = pmm_alloc (blks);
if (p_addr == PMM_ALLOC_ERR)
int rid = atomic_fetch_add (&proc->rids, 1);
struct proc_resource_mem_init mem_init = {.pages = blks};
struct proc_resource* r =
proc_create_resource (proc, rid, PR_MEM, RV_PRIVATE, (void*)&mem_init);
if (r == NULL) {
DEBUG ("pmm oom error while loading ELF segments! (tried to alloc %zu blks)\n", blks);
}
uintptr_t p_addr = r->u.mem.paddr;
memset ((void*)((uintptr_t)hhdm->offset + p_addr), 0, blks * PAGE_SIZE);
memcpy ((void*)((uintptr_t)hhdm->offset + p_addr + off),
@@ -213,8 +215,11 @@ static struct proc* proc_find_sched (void) {
if (!node)
rbtree_first (&thiscpu->proc_run_q, node);
if (!node)
return NULL;
struct rb_node_link* first = node;
while (node) {
do {
proc = rbtree_entry (node, struct proc, cpu_run_q_link);
if (atomic_load (&proc->state) == PROC_READY)
@@ -228,7 +233,7 @@ static struct proc* proc_find_sched (void) {
if (node == first)
break;
}
} while (node != first);
return NULL;
}

View File

@@ -6,6 +6,7 @@
#include <libk/list.h>
#include <libk/rbtree.h>
#include <libk/std.h>
#include <proc/resource.h>
#include <sync/spin_lock.h>
#include <sys/mm.h>
@@ -19,6 +20,8 @@
/// Process marked garbage collection
#define PROC_DEAD 1
#define PROC_RESOURCES_MAX 1024
struct cpu;
struct proc_mapping {
@@ -40,6 +43,8 @@ struct proc {
spin_lock_t lock;
struct cpu* cpu;
atomic_int state;
struct rb_node_link* resource_tree;
atomic_int rids;
};
void proc_sched (void);

100
kernel/proc/resource.c Normal file
View File

@@ -0,0 +1,100 @@
#include <libk/assert.h>
#include <libk/rbtree.h>
#include <libk/std.h>
#include <libk/string.h>
#include <mm/liballoc.h>
#include <mm/pmm.h>
#include <proc/proc.h>
#include <sync/spin_lock.h>
#include <sys/debug.h>
void proc_cleanup_resources (struct proc* proc) {
struct proc_resource* resource = NULL;
struct rb_node_link* rnode;
rbtree_first (&proc->resource_tree, rnode);
while (rnode) {
struct rb_node_link* next;
rbtree_next (rnode, next);
resource = rbtree_entry (rnode, struct proc_resource, proc_resource_tree_link);
rbtree_delete (&proc->resource_tree, rnode);
resource->ops.cleanup (resource);
free (resource);
rnode = next;
}
assert (proc->resource_tree == NULL);
}
void proc_drop_resource (struct proc* proc, struct proc_resource* resource) {
if (atomic_fetch_sub (&resource->refs, 1) == 1) {
spin_lock (&proc->lock);
rbtree_delete (&proc->resource_tree, &resource->proc_resource_tree_link);
spin_unlock (&proc->lock);
resource->ops.cleanup (resource);
free (resource);
}
}
static bool proc_create_resource_mem (struct proc_resource_mem* mem,
struct proc_resource_mem_init* init) {
if (init->pages == 0)
return false;
uintptr_t paddr = pmm_alloc (init->pages);
if (paddr == PMM_ALLOC_ERR)
return false;
mem->paddr = paddr;
mem->pages = init->pages;
DEBUG ("paddr=%p, pages=%zu\n", mem->paddr, mem->pages);
return true;
}
static void proc_cleanup_resource_mem (struct proc_resource* resource) {
pmm_free (resource->u.mem.paddr, resource->u.mem.pages);
}
struct proc_resource* proc_create_resource (struct proc* proc, int rid, int type, int vis,
void* data) {
/* Check if resource RID already exists */
struct proc_resource* resource_check;
rbtree_find (struct proc_resource, &proc->resource_tree, rid, resource_check,
proc_resource_tree_link, rid);
if (resource_check != NULL)
return NULL;
struct proc_resource* resource = malloc (sizeof (*resource));
if (resource == NULL)
return NULL;
memset (resource, 0, sizeof (*resource));
resource->lock = SPIN_LOCK_INIT;
resource->type = type;
resource->refs = 1;
resource->rid = rid;
resource->visibility = vis;
switch (resource->type) {
case PR_MEM: {
struct proc_resource_mem_init* mem_init = data;
proc_create_resource_mem (&resource->u.mem, mem_init);
resource->ops.cleanup = &proc_cleanup_resource_mem;
} break;
default: {
free (resource);
return NULL;
} break;
}
spin_lock (&proc->lock);
rbtree_insert (struct proc_resource, &proc->resource_tree, &resource->proc_resource_tree_link,
proc_resource_tree_link, rid);
spin_unlock (&proc->lock);
return resource;
}

44
kernel/proc/resource.h Normal file
View File

@@ -0,0 +1,44 @@
#ifndef _KERNEL_PROC_RESOURCE_H
#define _KERNEL_PROC_RESOURCE_H
#include <libk/rbtree.h>
#include <libk/std.h>
#include <sync/spin_lock.h>
#define PR_MEM 0
#define RV_PRIVATE 0
#define RV_PUBLIC 1
struct proc;
struct proc_resource_mem {
uintptr_t paddr;
size_t pages;
};
struct proc_resource_mem_init {
size_t pages;
};
struct proc_resource {
int type;
int rid;
int visibility;
spin_lock_t lock;
atomic_int refs;
struct rb_node_link proc_resource_tree_link;
union {
struct proc_resource_mem mem;
} u;
struct {
void (*cleanup) (struct proc_resource* resource);
} ops;
};
struct proc_resource* proc_create_resource (struct proc* proc, int rid, int type, int vis,
void* data);
void proc_drop_resource (struct proc* proc, struct proc_resource* resource);
void proc_cleanup_resources (struct proc* proc);
#endif // _KERNEL_PROC_RESOURCE_H

View File

@@ -1,3 +1,5 @@
c += proc/proc.c
c += proc/resource.c
o += proc/proc.o
o += proc/resource.o