Rewrite resource subsystem

This commit is contained in:
2026-01-18 20:50:45 +01:00
parent 4f7077d458
commit ddafc4eb19
20 changed files with 453 additions and 225 deletions

20
kernel/proc/kpproc_fb.h Normal file
View File

@@ -0,0 +1,20 @@
#ifndef _KERNEL_PROC_KPPROC_FB_H
#define _KERNEL_PROC_KPPROC_FB_H
#include <aux/compiler.h>
#include <libk/std.h>
/* data to expose as a kpproc resource */
struct kpproc_fb {
uintptr_t paddr;
uint64_t w, h, pitch;
uint16_t bpp;
uint8_t red_mask_size;
uint8_t red_mask_shift;
uint8_t green_mask_size;
uint8_t green_mask_shift;
uint8_t blue_mask_size;
uint8_t blue_mask_shift;
};
#endif // _KERNEL_PROC_KPPROC_FB_H

View File

@@ -9,11 +9,17 @@ bool proc_create_resource_mem (struct proc_resource_mem* mem, struct proc_resour
if (init->pages == 0)
return false;
uintptr_t paddr = pmm_alloc (init->pages);
if (paddr == PMM_ALLOC_ERR)
return false;
if (init->managed) {
mem->paddr = init->paddr;
mem->managed = true;
} else {
uintptr_t paddr = pmm_alloc (init->pages);
if (paddr == PMM_ALLOC_ERR)
return false;
mem->paddr = paddr;
mem->managed = false;
}
mem->paddr = paddr;
mem->pages = mem->alive_pages = init->pages;
return true;
@@ -21,17 +27,7 @@ bool proc_create_resource_mem (struct proc_resource_mem* mem, struct proc_resour
void proc_cleanup_resource_mem (struct proc* proc, struct proc_resource* resource) {
(void)proc;
pmm_free (resource->u.mem.paddr, resource->u.mem.pages);
}
void proc_mem_unref (struct proc* proc, struct proc_resource_mem* mem, size_t pages) {
spin_lock_ctx_t ctxrs;
spin_lock (&mem->resource->lock, &ctxrs);
mem->alive_pages -= pages;
ptrdiff_t current_pages = mem->alive_pages;
spin_unlock (&mem->resource->lock, &ctxrs);
if (current_pages <= 0)
proc_drop_resource (proc, mem->resource);
if (!resource->u.mem.managed)
pmm_free (resource->u.mem.paddr, resource->u.mem.pages);
}

View File

@@ -12,14 +12,16 @@ struct proc_resource_mem {
uintptr_t paddr;
size_t pages;
ptrdiff_t alive_pages;
bool managed;
};
struct proc_resource_mem_init {
uintptr_t paddr;
size_t pages;
bool managed;
};
bool proc_create_resource_mem (struct proc_resource_mem* mem, struct proc_resource_mem_init* init);
void proc_cleanup_resource_mem (struct proc* proc, struct proc_resource* resource);
void proc_mem_unref (struct proc* proc, struct proc_resource_mem* mem, size_t pages);
#endif // _KERNEL_PROC_MEM_H

View File

@@ -9,6 +9,7 @@
#include <limine/requests.h>
#include <mm/liballoc.h>
#include <mm/pmm.h>
#include <proc/kpproc_fb.h>
#include <proc/proc.h>
#include <proc/resource.h>
#include <rd/rd.h>
@@ -25,7 +26,7 @@
#include <amd64/intr_defs.h>
#endif
#define SCHED_REAP_FREQ 200
#define SCHED_REAP_FREQ 10
/*
* Lock hierachy:
@@ -40,6 +41,9 @@ static rw_spin_lock_t proc_tree_lock = RW_SPIN_LOCK_INIT;
static atomic_int sched_cycles = 0;
/* kernel pseudo process */
static struct proc kpproc;
static bool proc_check_elf (uint8_t* elf) {
if (!((elf[0] == 0x7F) && (elf[1] == 'E') && (elf[2] == 'L') && (elf[3] == 'F')))
return false;
@@ -144,7 +148,6 @@ struct elf_aux proc_load_segments (struct proc* proc, uint8_t* elf) {
aux.phent = ehdr->e_phentsize;
struct limine_hhdm_response* hhdm = limine_hhdm_request.response;
int rid_counter = 10;
for (uint64_t segment = 0; segment < ehdr->e_phnum; segment++) {
Elf64_Phdr* phdr =
@@ -161,8 +164,9 @@ struct elf_aux proc_load_segments (struct proc* proc, uint8_t* elf) {
size_t blks = div_align_up (phdr->p_memsz + off, PAGE_SIZE);
struct proc_resource_mem_init mem_init = {.pages = blks};
int rid = atomic_fetch_add (&proc->resources->sys_rids, 1);
struct proc_resource* r =
proc_create_resource (proc, rid_counter++, PR_MEM, RV_PRIVATE, (void*)&mem_init);
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);
}
@@ -197,6 +201,17 @@ static struct proc* proc_spawn_rd (char* name) {
return proc_from_elf (rd_file->content);
}
struct proc* proc_find_pid (int pid) {
spin_lock_ctx_t ctxprtr;
struct proc* proc = NULL;
rw_spin_read_lock (&proc_tree_lock, &ctxprtr);
rbtree_find (struct proc, &proc_tree, pid, proc, proc_tree_link, pid);
rw_spin_read_unlock (&proc_tree_lock, &ctxprtr);
return proc;
}
void proc_register (struct proc* proc, struct cpu* cpu) {
spin_lock_ctx_t ctxcpu, ctxprtr;
@@ -402,7 +417,61 @@ static void proc_irq_sched (void* arg, void* regs) {
#endif
}
static void proc_kpproc_init (void) {
struct limine_hhdm_response* hhdm = limine_hhdm_request.response;
memset (&kpproc, 0, sizeof (kpproc));
kpproc.lock = SPIN_LOCK_INIT;
kpproc.state = PROC_PSEUDO;
kpproc.pid = 0;
kpproc.resources = malloc (sizeof (*kpproc.resources));
kpproc.resources->tree = NULL;
kpproc.resources->lock = RW_SPIN_LOCK_INIT;
kpproc.resources->refs = 1;
kpproc.resources->sys_rids = 0;
kpproc.pd = mm_get_kernel_pd ();
kpproc.cpu = thiscpu;
rbtree_insert (struct proc, &proc_tree, &kpproc.proc_tree_link, proc_tree_link, pid);
/* prepare kernel resources */
{
/* frame buffer */
struct limine_framebuffer_response* fb = limine_framebuffer_request.response;
struct kpproc_fb fb_info = {
.paddr = (uintptr_t)fb->framebuffers[0]->address - (uintptr_t)hhdm->offset,
.w = fb->framebuffers[0]->width,
.h = fb->framebuffers[0]->height,
.pitch = fb->framebuffers[0]->pitch,
.bpp = fb->framebuffers[0]->bpp,
.red_mask_size = fb->framebuffers[0]->red_mask_size,
.red_mask_shift = fb->framebuffers[0]->red_mask_shift,
.green_mask_size = fb->framebuffers[0]->green_mask_size,
.green_mask_shift = fb->framebuffers[0]->green_mask_shift,
.blue_mask_size = fb->framebuffers[0]->blue_mask_size,
.blue_mask_shift = fb->framebuffers[0]->blue_mask_shift,
};
DEBUG ("Framebuffer address %p\n", fb_info.paddr);
size_t pages = align_up (sizeof (fb_info), PAGE_SIZE) / PAGE_SIZE;
uintptr_t fb_info_memblk_paddr = pmm_alloc (pages);
memcpy ((struct kpproc_fb*)((uintptr_t)hhdm->offset + fb_info_memblk_paddr), &fb_info,
sizeof (fb_info));
struct proc_resource_mem_init mem_init = {
.pages = pages, .paddr = fb_info_memblk_paddr, .managed = true};
proc_create_resource (&kpproc, 0, PR_MEM, RV_PUBLIC, &mem_init);
}
}
void proc_init (void) {
proc_kpproc_init ();
struct proc* init = proc_spawn_rd ("init.exe");
proc_register (init, thiscpu);

View File

@@ -8,6 +8,7 @@
#include <libk/std.h>
#include <proc/resource.h>
#include <proc/suspension_q.h>
#include <sync/rw_spin_lock.h>
#include <sync/spin_lock.h>
#include <sys/mm.h>
@@ -16,14 +17,11 @@
#include <amd64/proc.h> /* USTACK_SIZE */
#endif
/* Process is ready to run */
#define PROC_READY 0
/* Process marked garbage collection */
#define PROC_DEAD 1
/* Process is suspended */
/* process states */
#define PROC_READY 0
#define PROC_DEAD 1
#define PROC_SUSPENDED 2
#define PROC_RESOURCES_MAX 1024
#define PROC_PSEUDO 3
#define PROC_USTK_PREALLOC (1 << 0)
@@ -37,9 +35,11 @@ struct proc_mapping {
size_t size;
};
struct proc_sys_rids {
struct proc_resources {
atomic_int refs;
atomic_int counter;
atomic_int sys_rids;
struct rb_node_link* tree;
rw_spin_lock_t lock;
};
struct proc {
@@ -56,9 +56,8 @@ struct proc {
spin_lock_t lock;
struct cpu* cpu;
atomic_int state;
struct rb_node_link* resource_tree;
struct proc_sys_rids* sys_rids;
struct proc_suspension_q* suspension_q;
struct proc_resources* resources;
};
void proc_suspend (struct proc* proc, struct proc_suspension_q* sq);
@@ -70,6 +69,7 @@ bool proc_map (struct proc* proc, uintptr_t start_paddr, uintptr_t start_vaddr,
bool proc_unmap (struct proc* proc, uintptr_t start_vaddr, size_t pages);
struct elf_aux proc_load_segments (struct proc* proc, uint8_t* elf);
void proc_register (struct proc* proc, struct cpu* cpu);
struct proc* proc_find_pid (int pid);
void proc_init (void);
#endif // _KERNEL_PROC_PROC_H

View File

@@ -1,4 +1,5 @@
#include <libk/assert.h>
#include <libk/list.h>
#include <libk/rbtree.h>
#include <libk/std.h>
#include <libk/string.h>
@@ -10,48 +11,112 @@
#include <sync/spin_lock.h>
#include <sys/debug.h>
static struct rb_node_link* resource_tree = NULL;
static rw_spin_lock_t resource_tree_lock = RW_SPIN_LOCK_INIT;
void proc_cleanup_resources (struct proc* proc) {
struct proc_resource* resource = NULL;
spin_lock_ctx_t ctxrs;
rw_spin_write_lock (&proc->resources->lock, &ctxrs);
struct rb_node_link* rnode;
rbtree_first (&proc->resource_tree, rnode);
rbtree_first (&proc->resources->tree, rnode);
while (rnode) {
struct rb_node_link* next;
rbtree_next (rnode, next);
resource = rbtree_entry (rnode, struct proc_resource, proc_resource_tree_link);
proc_drop_resource (proc, resource);
struct proc_resource* resource =
rbtree_entry (rnode, struct proc_resource, local_resource_tree_link);
rnode = next;
proc_drop_resource (proc, resource, false);
}
assert (proc->resource_tree == NULL);
rw_spin_write_unlock (&proc->resources->lock, &ctxrs);
if (atomic_fetch_sub (&proc->resources->refs, 1) == 1) {
free (proc->resources);
}
}
void proc_drop_resource (struct proc* proc, struct proc_resource* resource) {
spin_lock_ctx_t ctxpr;
void proc_drop_resource (struct proc* proc, struct proc_resource* resource, bool lock) {
spin_lock_ctx_t ctxrs;
DEBUG ("resource=%p, type=%d, rid=%d\n", resource, resource->type, resource->rid);
DEBUG ("resource=%p created_by=%d vis=%d type=%d rid=%d refs=%d\n", resource,
resource->created_by_pid, resource->visibility, resource->type, resource->rid,
atomic_load (&resource->refs));
if (atomic_fetch_sub (&resource->refs, 1) == 1) {
spin_lock (&proc->lock, &ctxpr);
rbtree_delete (&proc->resource_tree, &resource->proc_resource_tree_link);
spin_unlock (&proc->lock, &ctxpr);
switch (resource->visibility) {
case RV_PRIVATE: {
if (lock)
rw_spin_write_lock (&proc->resources->lock, &ctxrs);
rbtree_delete (&proc->resources->tree, &resource->local_resource_tree_link);
if (lock)
rw_spin_write_unlock (&proc->resources->lock, &ctxrs);
} break;
case RV_PUBLIC: {
if (lock)
rw_spin_write_lock (&resource_tree_lock, &ctxrs);
rbtree_delete (&resource_tree, &resource->global_resource_tree_link);
if (lock)
rw_spin_write_unlock (&resource_tree_lock, &ctxrs);
} break;
default: {
assert (0);
} break;
}
resource->ops.cleanup (proc, resource);
free (resource);
}
}
struct proc_resource* proc_find_resource (struct proc* proc, int rid, int vis) {
struct proc_resource* resource = NULL;
spin_lock_ctx_t ctxrs;
switch (vis) {
case RV_PRIVATE: {
/* User wants to create a private resource, so search locally */
rw_spin_read_lock (&proc->resources->lock, &ctxrs);
rbtree_find (struct proc_resource, &proc->resources->tree, rid, resource,
local_resource_tree_link, rid);
rw_spin_read_unlock (&proc->resources->lock, &ctxrs);
} break;
case RV_PUBLIC: {
/* User wants to create a public resource, so search globally */
rw_spin_read_lock (&resource_tree_lock, &ctxrs);
rbtree_find (struct proc_resource, &resource_tree, rid, resource, global_resource_tree_link,
rid);
rw_spin_read_unlock (&resource_tree_lock, &ctxrs);
} break;
default: {
assert (0);
} break;
}
return resource;
}
struct proc_resource* proc_create_resource (struct proc* proc, int rid, int type, int vis,
void* data) {
spin_lock_ctx_t ctxpr;
spin_lock_ctx_t ctxrs;
/* 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);
struct proc_resource* resource_check = proc_find_resource (proc, rid, vis);
/* Resource was found either way, so it already exists */
if (resource_check != NULL)
return NULL;
/* create the resource */
struct proc_resource* resource = malloc (sizeof (*resource));
if (resource == NULL)
return NULL;
@@ -63,6 +128,7 @@ struct proc_resource* proc_create_resource (struct proc* proc, int rid, int type
resource->refs = 1;
resource->rid = rid;
resource->visibility = vis;
resource->created_by_pid = proc->pid;
switch (resource->type) {
case PR_MEM: {
@@ -70,14 +136,16 @@ struct proc_resource* proc_create_resource (struct proc* proc, int rid, int type
proc_create_resource_mem (&resource->u.mem, mem_init);
resource->ops.cleanup = &proc_cleanup_resource_mem;
resource->u.mem.resource = resource;
DEBUG ("PR_MEM resource=%p type=%d rid=%d paddr=%p, pages=%zu\n", resource, resource->type,
resource->rid, resource->u.mem.paddr, resource->u.mem.pages);
DEBUG ("PR_MEM resource=%p created_by=%d, type=%d rid=%d paddr=%p, pages=%zu\n", resource,
resource->created_by_pid, resource->type, resource->rid, resource->u.mem.paddr,
resource->u.mem.pages);
} break;
case PR_MUTEX: {
proc_create_resource_mutex (&resource->u.mutex);
resource->ops.cleanup = &proc_cleanup_resource_mutex;
resource->u.mutex.resource = resource;
DEBUG ("PR_MUTEX resource=%p, type=%d rid=%d\n", resource, resource->type, resource->rid);
DEBUG ("PR_MUTEX resource=%p created_by=%d type=%d rid=%d\n", resource,
resource->created_by_pid, resource->type, resource->rid);
} break;
default: {
free (resource);
@@ -85,10 +153,23 @@ struct proc_resource* proc_create_resource (struct proc* proc, int rid, int type
} break;
}
spin_lock (&proc->lock, &ctxpr);
rbtree_insert (struct proc_resource, &proc->resource_tree, &resource->proc_resource_tree_link,
proc_resource_tree_link, rid);
spin_unlock (&proc->lock, &ctxpr);
switch (resource->visibility) {
case RV_PRIVATE: {
rw_spin_write_lock (&proc->resources->lock, &ctxrs);
rbtree_insert (struct proc_resource, &proc->resources->tree,
&resource->local_resource_tree_link, local_resource_tree_link, rid);
rw_spin_write_unlock (&proc->resources->lock, &ctxrs);
} break;
case RV_PUBLIC: {
rw_spin_write_lock (&resource_tree_lock, &ctxrs);
rbtree_insert (struct proc_resource, &resource_tree, &resource->global_resource_tree_link,
global_resource_tree_link, rid);
rw_spin_write_unlock (&resource_tree_lock, &ctxrs);
} break;
default: {
assert (0);
} break;
}
return resource;
}

View File

@@ -1,6 +1,7 @@
#ifndef _KERNEL_PROC_RESOURCE_H
#define _KERNEL_PROC_RESOURCE_H
#include <libk/list.h>
#include <libk/rbtree.h>
#include <libk/std.h>
#include <proc/mem.h>
@@ -14,7 +15,6 @@
#define RV_PUBLIC 1
struct proc;
struct proc_resource;
struct proc_resource {
int type;
@@ -22,7 +22,8 @@ struct proc_resource {
int visibility;
spin_lock_t lock;
atomic_int refs;
struct rb_node_link proc_resource_tree_link;
struct rb_node_link global_resource_tree_link;
struct rb_node_link local_resource_tree_link;
union {
struct proc_resource_mem mem;
struct proc_mutex mutex;
@@ -30,11 +31,13 @@ struct proc_resource {
struct {
void (*cleanup) (struct proc* proc, struct proc_resource* resource);
} ops;
int created_by_pid;
};
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);
struct proc_resource* proc_find_resource (struct proc* proc, int rid, int vis);
void proc_drop_resource (struct proc* proc, struct proc_resource* resource, bool lock);
void proc_cleanup_resources (struct proc* proc);
#endif // _KERNEL_PROC_RESOURCE_H