summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--sys/include/sys/mman.h4
-rw-r--r--sys/vm/vm_map.c89
2 files changed, 93 insertions, 0 deletions
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 <sys/types.h>
#include <sys/errno.h>
#include <sys/proc.h>
+#include <sys/systm.h>
#include <sys/syscall.h>
#include <sys/syslog.h>
#include <sys/mman.h>
@@ -77,6 +78,22 @@ mmap_add(struct proc *td, struct mmap_entry *ep)
}
/*
+ * 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.
*
@@ -217,6 +234,61 @@ mmap_at(void *addr, size_t len, int prot, int flags, int fildes, off_t off)
}
/*
+ * 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
*
* arg0 -> addr
@@ -244,6 +316,23 @@ mmap(struct syscall_args *scargs)
}
/*
+ * 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.
*