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

View File

@@ -43,6 +43,8 @@ void amd64_load_kernel_cr3 (void) {
}
}
struct pd* mm_get_kernel_pd (void) { return &kernel_pd; }
/* Extract PML info from virtual address */
static struct pg_index amd64_mm_page_index (uint64_t vaddr) {
struct pg_index ret;

View File

@@ -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;

View File

@@ -8,6 +8,7 @@
if (!(x)) { \
DEBUG ("%s ssertion failed\n", #x); \
spin (); \
__builtin_unreachable (); \
} \
} while (0)

View File

@@ -20,3 +20,4 @@ DECL_REQ (memmap, MEMMAP);
DECL_REQ (rsdp, RSDP);
DECL_REQ (mp, MP);
DECL_REQ (module, MODULE);
DECL_REQ (framebuffer, FRAMEBUFFER);

View File

@@ -10,5 +10,6 @@ EXTERN_REQ (memmap);
EXTERN_REQ (rsdp);
EXTERN_REQ (mp);
EXTERN_REQ (module);
EXTERN_REQ (framebuffer);
#endif // _KERNEL_LIMINE_REQUESTS_H

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

View File

@@ -25,6 +25,7 @@ bool mm_validate (struct pd* pd, uintptr_t vaddr, uint32_t flags);
bool mm_validate_buffer (struct pd* pd, uintptr_t vaddr, size_t size, uint32_t flags);
uintptr_t mm_p2v (struct pd* pd, uintptr_t paddr, uint32_t flags);
uintptr_t mm_v2p (struct pd* pd, uintptr_t vaddr, uint32_t flags);
struct pd* mm_get_kernel_pd (void);
void mm_init (void);
#endif // _KERNEL_SYS_MM_H

View File

@@ -1,6 +1,8 @@
#include <aux/compiler.h>
#include <libk/assert.h>
#include <libk/std.h>
#include <limine/requests.h>
#include <m/resource_buffer.h>
#include <m/status.h>
#include <m/syscall_defs.h>
#include <mm/pmm.h>
@@ -15,9 +17,29 @@
#include <syscall/syscall.h>
#define DEFINE_SYSCALL(name) \
int name (struct proc* proc, void* UNUSED regs, uintptr_t UNUSED a1, uintptr_t UNUSED a2, \
int name (struct proc* UNUSED proc, void* UNUSED regs, uintptr_t UNUSED a1, uintptr_t UNUSED a2, \
uintptr_t UNUSED a3, uintptr_t UNUSED a4, uintptr_t UNUSED a5, uintptr_t UNUSED a6)
static void* sys_get_user_buffer (struct proc* proc, uintptr_t uvaddr, size_t size) {
struct limine_hhdm_response* hhdm = limine_hhdm_request.response;
spin_lock_ctx_t ctxprpd;
spin_lock (&proc->pd->lock, &ctxprpd);
if (!mm_validate_buffer (proc->pd, (uintptr_t)uvaddr, size, 0)) {
spin_unlock (&proc->pd->lock, &ctxprpd);
return NULL;
}
uintptr_t out_paddr = mm_v2p (proc->pd, uvaddr, 0);
spin_unlock (&proc->pd->lock, &ctxprpd);
uintptr_t out_kvaddr = (uintptr_t)hhdm->offset + out_paddr;
return (void*)out_kvaddr;
}
/* int proc_quit (void) */
DEFINE_SYSCALL (sys_proc_quit) {
proc_kill (proc, regs);
@@ -60,68 +82,83 @@ DEFINE_SYSCALL (sys_proc_unmap) {
return ok ? ST_OK : -ST_OOM_ERROR;
}
/* int proc_create_resource_mem (int rid, size_t pages, int vis, uintptr_t* out_paddr) */
DEFINE_SYSCALL (sys_proc_create_resource_mem) {
struct limine_hhdm_response* hhdm = limine_hhdm_request.response;
spin_lock_ctx_t ctxprpd;
/* int proc_create_resource (int rid, int type, int vis, void* buffer) */
DEFINE_SYSCALL (sys_proc_create_resource) {
int rid = (int)a1;
size_t pages = (size_t)a2;
int type = (int)a2;
int vis = (int)a3;
uintptr_t* out_paddr_buf = (uintptr_t*)a4;
uintptr_t buffer_ptr = a4;
if (rid < 0)
return -ST_BAD_RESOURCE;
spin_lock (&proc->pd->lock, &ctxprpd);
if (!(type == PR_MEM || type == PR_MUTEX))
return -ST_BAD_RESOURCE;
uintptr_t out_paddr_buf_paddr = mm_v2p (proc->pd, (uintptr_t)out_paddr_buf, 0);
if (!(vis == RV_PRIVATE || vis == RV_PUBLIC))
return -ST_BAD_RESOURCE;
if (!mm_validate_buffer (proc->pd, (uintptr_t)out_paddr_buf, sizeof (uintptr_t), 0)) {
spin_unlock (&proc->pd->lock, &ctxprpd);
return -ST_BAD_ADDRESS_SPACE;
struct resource_buffer* rbuf = NULL;
if (buffer_ptr != 0) {
rbuf = sys_get_user_buffer (proc, buffer_ptr, sizeof (struct resource_buffer));
if (rbuf == NULL)
return -ST_BAD_ADDRESS_SPACE;
}
spin_unlock (&proc->pd->lock, &ctxprpd);
/* confusing data is invalid */
if ((rbuf != NULL) && (type != rbuf->type))
return -ST_BAD_RESOURCE;
uintptr_t* out_paddr_buf_vaddr = (uintptr_t*)((uintptr_t)hhdm->offset + out_paddr_buf_paddr);
switch (type) {
case PR_MEM: {
/* need rbuf to construct */
if (rbuf == NULL)
return -ST_BAD_RESOURCE;
struct proc_resource_mem_init mem_init = {.pages = pages};
struct proc_resource* r = proc_create_resource (proc, rid, PR_MEM, vis, &mem_init);
struct proc_resource_mem_init mem_init = {
.managed = false,
.pages = rbuf->u.mem.pages,
};
if (r != NULL) {
*out_paddr_buf_vaddr = r->u.mem.paddr;
return r->rid;
} else {
return -ST_OOM_ERROR;
struct proc_resource* resource = proc_create_resource (proc, rid, type, vis, &mem_init);
if (resource == NULL) {
return -ST_OOM_ERROR;
}
rbuf->u.mem.pages = resource->u.mem.pages;
rbuf->u.mem.paddr = resource->u.mem.paddr;
return resource->rid;
} break;
case PR_MUTEX: {
/* no rbuf is fine */
struct proc_resource* resource = proc_create_resource (proc, rid, type, vis, NULL);
if (resource == NULL) {
return -ST_OOM_ERROR;
}
return resource->rid;
} break;
}
assert (0);
}
/* int proc_create_resource_mutex (int rid, int vis) */
DEFINE_SYSCALL (sys_proc_create_resource_mutex) {
/* int proc_mutex_lock (int mutex_rid, int vis) */
DEFINE_SYSCALL (sys_proc_mutex_lock) {
int rid = (int)a1;
int vis = (int)a2;
if (rid < 0)
return -ST_BAD_RESOURCE;
struct proc_resource* r = proc_create_resource (proc, rid, PR_MUTEX, vis, NULL);
if (r != NULL)
return r->rid;
else
return -ST_OOM_ERROR;
}
if (!(vis == RV_PUBLIC || vis == RV_PRIVATE))
return -ST_BAD_RESOURCE;
/* int proc_mutex_lock (int mutex_rid) */
DEFINE_SYSCALL (sys_proc_mutex_lock) {
spin_lock_ctx_t ctxpr;
int rid = (int)a1;
struct proc_resource* resource;
spin_lock (&proc->lock, &ctxpr);
rbtree_find (struct proc_resource, &proc->resource_tree, rid, resource, proc_resource_tree_link,
rid);
spin_unlock (&proc->lock, &ctxpr);
struct proc_resource* resource = proc_find_resource (proc, rid, vis);
if (resource == NULL)
return -ST_NOT_FOUND;
@@ -131,16 +168,18 @@ DEFINE_SYSCALL (sys_proc_mutex_lock) {
return ST_OK;
}
/* int proc_mutex_unlock (int mutex_rid) */
/* int proc_mutex_unlock (int mutex_rid, int vis) */
DEFINE_SYSCALL (sys_proc_mutex_unlock) {
spin_lock_ctx_t ctxpr;
int rid = (int)a1;
int vis = (int)a2;
struct proc_resource* resource;
spin_lock (&proc->lock, &ctxpr);
rbtree_find (struct proc_resource, &proc->resource_tree, rid, resource, proc_resource_tree_link,
rid);
spin_unlock (&proc->lock, &ctxpr);
if (rid < 0)
return -ST_BAD_RESOURCE;
if (!(vis == RV_PUBLIC || vis == RV_PRIVATE))
return -ST_BAD_RESOURCE;
struct proc_resource* resource = proc_find_resource (proc, rid, vis);
if (resource == NULL)
return -ST_NOT_FOUND;
@@ -154,21 +193,23 @@ DEFINE_SYSCALL (sys_proc_mutex_unlock) {
return ST_OK;
}
/* int proc_drop_resource (int rid) */
/* int proc_drop_resource (int rid, int vis) */
DEFINE_SYSCALL (sys_proc_drop_resource) {
spin_lock_ctx_t ctxpr;
int rid = (int)a1;
int vis = (int)a2;
struct proc_resource* resource;
spin_lock (&proc->lock, &ctxpr);
rbtree_find (struct proc_resource, &proc->resource_tree, rid, resource, proc_resource_tree_link,
rid);
spin_unlock (&proc->lock, &ctxpr);
if (rid < 0)
return -ST_BAD_RESOURCE;
if (!(vis == RV_PUBLIC || vis == RV_PRIVATE))
return -ST_BAD_RESOURCE;
struct proc_resource* resource = proc_find_resource (proc, rid, vis);
if (resource == NULL)
return -ST_NOT_FOUND;
proc_drop_resource (proc, resource);
proc_drop_resource (proc, resource, true);
return ST_OK;
}
@@ -202,26 +243,18 @@ DEFINE_SYSCALL (sys_proc_sched) {
return ST_OK;
}
/* int proc_translate_v2p (uintptr_t vaddr, uintptr_t* out_paddr) */
DEFINE_SYSCALL (sys_proc_translate_v2p) {
struct limine_hhdm_response* hhdm = limine_hhdm_request.response;
spin_lock_ctx_t ctxprpd;
int result = ST_OK;
uintptr_t vaddr = a1;
uintptr_t* out_paddr_buf = (uintptr_t*)a2;
uintptr_t out_paddr_buf = a2;
spin_lock (&proc->pd->lock, &ctxprpd);
uintptr_t out_paddr_buf_paddr = mm_v2p (proc->pd, (uintptr_t)out_paddr_buf, 0);
if (!mm_validate_buffer (proc->pd, (uintptr_t)out_paddr_buf, sizeof (uintptr_t), 0)) {
spin_unlock (&proc->pd->lock, &ctxprpd);
uintptr_t* out_paddr_buf_vaddr = sys_get_user_buffer (proc, out_paddr_buf, sizeof (uintptr_t));
if (out_paddr_buf_vaddr == NULL)
return -ST_BAD_ADDRESS_SPACE;
}
uintptr_t* out_paddr_buf_vaddr = (uintptr_t*)((uintptr_t)hhdm->offset + out_paddr_buf_paddr);
uintptr_t translated_addr = mm_v2p (proc->pd, vaddr, 0);
uintptr_t translated_addr = mm_v2p (proc->pd, vaddr, MM_PD_LOCK);
if (translated_addr == 0) {
result = -ST_BAD_ADDRESS_SPACE;
goto done;
@@ -230,44 +263,21 @@ DEFINE_SYSCALL (sys_proc_translate_v2p) {
*out_paddr_buf_vaddr = translated_addr;
done:
spin_unlock (&proc->pd->lock, &ctxprpd);
return result;
}
/* int proc_resource_mem_unref (int mem_rid, size_t pages) */
DEFINE_SYSCALL (sys_proc_mem_unref) {
spin_lock_ctx_t ctxpr;
int rid = (int)a1;
size_t pages = (size_t)a2;
struct proc_resource* resource;
spin_lock (&proc->lock, &ctxpr);
rbtree_find (struct proc_resource, &proc->resource_tree, rid, resource, proc_resource_tree_link,
rid);
spin_unlock (&proc->lock, &ctxpr);
if (resource == NULL)
return -ST_NOT_FOUND;
proc_mem_unref (proc, &resource->u.mem, pages);
return ST_OK;
}
static syscall_handler_func_t handler_table[] = {
[SYS_PROC_QUIT] = &sys_proc_quit,
[SYS_PROC_TEST] = &sys_proc_test,
[SYS_PROC_MAP] = &sys_proc_map,
[SYS_PROC_UNMAP] = &sys_proc_unmap,
[SYS_PROC_CREATE_RESOURCE_MEM] = &sys_proc_create_resource_mem,
[SYS_PROC_CREATE_RESOURCE] = &sys_proc_create_resource,
[SYS_PROC_DROP_RESOURCE] = &sys_proc_drop_resource,
[SYS_PROC_CREATE_RESOURCE_MUTEX] = &sys_proc_create_resource_mutex,
[SYS_PROC_MUTEX_LOCK] = &sys_proc_mutex_lock,
[SYS_PROC_MUTEX_UNLOCK] = &sys_proc_mutex_unlock,
[SYS_PROC_SPAWN_THREAD] = &sys_proc_spawn_thread,
[SYS_PROC_SCHED] = &sys_proc_sched,
[SYS_PROC_TRANSLATE_V2P] = &sys_proc_translate_v2p,
[SYS_PROC_MEM_UNREF] = &sys_proc_mem_unref,
};
syscall_handler_func_t syscall_find_handler (int syscall_num) {