From 79e18e209ccd21f4976e8b795cbbffbfbde3edc1 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Thu, 27 Mar 2025 02:36:17 -0400 Subject: kernel: vm: Add munmap() impl Signed-off-by: Ian Moffett --- sys/include/sys/mman.h | 4 +++ sys/vm/vm_map.c | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+) diff --git a/sys/include/sys/mman.h b/sys/include/sys/mman.h index 98ff8fe..4ead9ba 100644 --- a/sys/include/sys/mman.h +++ b/sys/include/sys/mman.h @@ -80,6 +80,9 @@ struct mmap_lgdr { size_t nbytes; }; +/* Kernel munmap() routine */ +int munmap_at(void *addr, size_t len); + /* Kernel mmap() routine */ void *mmap_at(void *addr, size_t len, int prot, int flags, int fildes, off_t off); @@ -90,5 +93,6 @@ RBT_PROTOTYPE(lgdr_entries, mmap_entry, hd, mmap_entrycmp) /* Syscall layer */ scret_t mmap(struct syscall_args *scargs); +scret_t munmap(struct syscall_args *scargs); #endif /* !_SYS_MMAN_H_ */ diff --git a/sys/vm/vm_map.c b/sys/vm/vm_map.c index 3dd4d01..b56e896 100644 --- a/sys/vm/vm_map.c +++ b/sys/vm/vm_map.c @@ -31,6 +31,7 @@ #include #include #include +#include #include #include #include @@ -76,6 +77,22 @@ mmap_add(struct proc *td, struct mmap_entry *ep) return 0; } +/* + * Remove memory mapping from mmap ledger + * + * @td: Process to remove mapping from. + * @ep: Memory map entry to remove. + */ +static inline void +mmap_remove(struct proc *td, struct mmap_entry *ep) +{ + struct mmap_lgdr *lp = td->mlgdr; + + RBT_REMOVE(lgdr_entries, &lp->hd, ep); + lp->nbytes -= ep->size; + dynfree(ep); +} + /* * Create/destroy virtual memory mappings in a specific * address space. @@ -216,6 +233,61 @@ mmap_at(void *addr, size_t len, int prot, int flags, int fildes, off_t off) return addr; } +/* + * Remove mappings for entire pages that + * belong to the current process. + * + * XXX: POSIX munmap(3) requires `addr' to be page-aligned + * and will return -EINVAL if otherwise. However, with + * OUSI munmap(3), `addr' is rounded down to the nearest + * multiple of the machine page size. + */ +int +munmap_at(void *addr, size_t len) +{ + int pgno; + vaddr_t va; + struct proc *td; + struct mmap_lgdr *lp; + struct mmap_entry find, *res; + struct vas vas; + + if (addr == NULL || len == 0) { + return -EINVAL; + } + + /* Apply machine specific addr/len adjustments */ + va = ALIGN_DOWN((vaddr_t)addr, DEFAULT_PAGESIZE); + len = ALIGN_UP(len, DEFAULT_PAGESIZE); + pgno = va >> 12; + + td = this_td(); + __assert(td != NULL && "no pid 1"); + vas = pmap_read_vas(); + + /* + * Try to get the mmap ledger, should not run into + * any issues as long as the PCB isn't borked. However, + * if it somehow is, just segfault ourselves. + */ + if ((lp = td->mlgdr) == NULL) { + __sigraise(SIGSEGV); + return -EFAULT; /* Unreachable */ + } + + /* Lookup entry in ledger with virtual address */ + find.va_start = va; + res = RBT_FIND(lgdr_entries, &lp->hd, &find); + if (res == NULL) { + pr_error("munmap: page %d not in ledger\n", pgno); + return -EINVAL; + } + + vm_unmap(vas, va, len); + mmap_remove(td, res); + return 0; +} + /* * mmap() syscall * @@ -243,6 +315,23 @@ mmap(struct syscall_args *scargs) return (scret_t)mmap_at(addr, len, prot, flags, fildes, off); } +/* + * munmap() syscall + * + * arg0 -> addr + * arg1 -> len + */ +scret_t +munmap(struct syscall_args *scargs) +{ + void *addr; + size_t len; + + addr = (void *)scargs->arg0; + len = scargs->arg1; + return (scret_t)munmap_at(addr, len); +} + /* * Create a virtual memory mapping in a specific * address space. -- cgit v1.2.3