diff options
author | Ian Moffett <ian@osmora.org> | 2025-09-19 23:29:18 -0400 |
---|---|---|
committer | Ian Moffett <ian@osmora.org> | 2025-09-19 23:29:18 -0400 |
commit | 50e1b103669a334d31bb27d7d858400c7a12e29e (patch) | |
tree | 6459d4bbfd32157be8b62ad64bd01a2a2916c395 /src/sys/os | |
parent | 69933fc75a701502f06fc30680fa6fa1f13b5ebb (diff) |
kern: proc: Keep track of mapped areas with ranges
This commit adds a virtual memory range queue to the process descriptor
in order to keep track of mapped pages and free their respective frames
upon program exit.
Signed-off-by: Ian Moffett <ian@osmora.org>
Diffstat (limited to 'src/sys/os')
-rw-r--r-- | src/sys/os/os_proc.c | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/src/sys/os/os_proc.c b/src/sys/os/os_proc.c index 329a3ea..3a42496 100644 --- a/src/sys/os/os_proc.c +++ b/src/sys/os/os_proc.c @@ -30,12 +30,39 @@ #include <sys/types.h> #include <sys/atomic.h> #include <sys/errno.h> +#include <sys/cdefs.h> +#include <sys/queue.h> #include <sys/proc.h> +#include <vm/vm.h> +#include <vm/physseg.h> +#include <os/kalloc.h> #include <string.h> static pid_t next_pid = 0; /* + * Deallocate saved memory ranges + * + * @proc: Process to target + */ +static void +proc_clear_ranges(struct proc *proc) +{ + const size_t PSIZE = DEFAULT_PAGESIZE; + struct vm_range *range; + size_t n_pages; + + TAILQ_FOREACH(range, &proc->maplist, link) { + if (range == NULL) { + continue; + } + + n_pages = ALIGN_UP(range->len, PSIZE) / PSIZE; + vm_free_frame(range->pa_base, n_pages); + } +} + +/* * MI proc init code */ int @@ -52,6 +79,7 @@ proc_init(struct proc *procp, int flags) /* Put the process in a known state */ scdp = &procp->scdom; memset(procp, 0, sizeof(*procp)); + TAILQ_INIT(&procp->maplist); /* * Initialize each platform latch @@ -73,6 +101,31 @@ proc_init(struct proc *procp, int flags) } /* + * Add range to process + */ +int +proc_add_range(struct proc *procp, vaddr_t va, paddr_t pa, size_t len) +{ + const size_t PSIZE = DEFAULT_PAGESIZE; + struct vm_range *range; + + if (procp == NULL) { + return -EINVAL; + } + + range = kalloc(sizeof(*range)); + if (range == NULL) { + return -ENOMEM; + } + + range->pa_base = pa; + range->va_base = va; + range->len = ALIGN_UP(len, PSIZE); + TAILQ_INSERT_TAIL(&procp->maplist, range, link); + return 0; +} + +/* * Kill a specific process */ int @@ -83,5 +136,6 @@ proc_kill(struct proc *procp, int status) } procp->flags |= PROC_EXITING; + proc_clear_ranges(procp); return md_proc_kill(procp, 0); } |