From 078f945812435967b0eb19408856ca9caf2b1246 Mon Sep 17 00:00:00 2001
From: Ian Moffett <ian@osmora.org>
Date: Fri, 1 Mar 2024 20:46:14 -0500
Subject: kernel: vm_map: Add vm_map_destroy()

Signed-off-by: Ian Moffett <ian@osmora.org>
---
 sys/include/vm/map.h |  1 +
 sys/vm/vm_map.c      | 31 +++++++++++++++++++++++++++++++
 2 files changed, 32 insertions(+)

(limited to 'sys')

diff --git a/sys/include/vm/map.h b/sys/include/vm/map.h
index 603da92..1cce8b8 100644
--- a/sys/include/vm/map.h
+++ b/sys/include/vm/map.h
@@ -35,5 +35,6 @@
 #include <vm/pmap.h>
 
 int vm_map_create(vaddr_t va, paddr_t pa, vm_prot_t prot, size_t bytes);
+int vm_map_destroy(vaddr_t va, size_t bytes);
 
 #endif  /* !_VM_MMAP_H_ */
diff --git a/sys/vm/vm_map.c b/sys/vm/vm_map.c
index aec4c39..ae93c38 100644
--- a/sys/vm/vm_map.c
+++ b/sys/vm/vm_map.c
@@ -105,3 +105,34 @@ vm_map_create(vaddr_t va, paddr_t pa, vm_prot_t prot, size_t bytes)
 
     return 0;
 }
+
+/*
+ * Destroy a virtual memory mapping in the current
+ * address space.
+ */
+int
+vm_map_destroy(vaddr_t va, size_t bytes)
+{
+    struct vm_ctx *ctx = vm_get_ctx();
+    size_t granule = vm_get_page_size();
+    int s;
+
+    /* We want bytes to be aligned by the granule */
+    bytes = __ALIGN_UP(bytes, granule);
+
+    /* Align VA by granule */
+    va = __ALIGN_DOWN(va, granule);
+
+    if (bytes == 0) {
+        return -1;
+    }
+
+    for (uintptr_t i = 0; i < bytes; i += granule) {
+        s = pmap_unmap(ctx, va + i);
+        if (s != 0) {
+            return -1;
+        }
+    }
+
+    return 0;
+}
-- 
cgit v1.2.3