refactor: simplify the vmm design, single responsibility. But using it should with...
[lunaix-os.git] / lunaix-os / kernel / mm / dmm.c
index ae899086d79fcd00f91bb1eb6faca4b8aa04dc04..cb87202e82c33c18a373dcffb5f7def06bbca515 100644 (file)
@@ -2,11 +2,13 @@
  * @file dmm.c
  * @author Lunaixsky
  * @brief Dynamic memory manager for heap. This design do not incorporate any\
- * specific implementation of malloc family. The main purpose of this routines is to
- * provide handy method to initialize & grow the heap as needed by upstream implementation.
- * 
- * This is designed to be portable, so it can serve as syscalls to malloc/free in the c std lib. 
- * 
+ * specific implementation of malloc family. The main purpose of this routines
+ * is to provide handy method to initialize & grow the heap as needed by
+ * upstream implementation.
+ *
+ * This is designed to be portable, so it can serve as syscalls to malloc/free
+ * in the c std lib.
+ *
  * @version 0.2
  * @date 2022-03-3
  *
  */
 
 #include <lunaix/mm/dmm.h>
-#include <lunaix/mm/vmm.h>
 #include <lunaix/mm/page.h>
+#include <lunaix/mm/vmm.h>
+#include <lunaix/status.h>
 
 #include <lunaix/spike.h>
+#include <lunaix/syscall.h>
+
+extern void __kernel_heap_start;
+
+__DEFINE_LXSYSCALL1(int, sbrk, size_t, size)
+{
+    heap_context_t* uheap = &__current->mm.u_heap;
+    mutex_lock(&uheap->lock);
+    void* r = lxsbrk(uheap, size, PG_ALLOW_USER);
+    mutex_unlock(&uheap->lock);
+    return r;
+}
+
+__DEFINE_LXSYSCALL1(void*, brk, void*, addr)
+{
+    heap_context_t* uheap = &__current->mm.u_heap;
+    mutex_lock(&uheap->lock);
+    int r = lxbrk(uheap, addr, PG_ALLOW_USER);
+    mutex_unlock(&uheap->lock);
+    return r;
+}
 
 int
 dmm_init(heap_context_t* heap)
@@ -26,18 +50,25 @@ dmm_init(heap_context_t* heap)
     assert((uintptr_t)heap->start % BOUNDARY == 0);
 
     heap->brk = heap->start;
+    mutex_init(&heap->lock);
+
+    int perm = PG_ALLOW_USER;
+    if (heap->brk >= &__kernel_heap_start) {
+        perm = 0;
+    }
 
-    return vmm_alloc_page(heap->brk, PG_PREM_RW) != NULL;
+    return vmm_set_mapping(PD_REFERENCED, heap->brk, 0, PG_WRITE | perm) !=
+           NULL;
 }
 
 int
-lxsbrk(heap_context_t* heap, void* addr)
+lxbrk(heap_context_t* heap, void* addr, int user)
 {
-    return lxbrk(heap, addr - heap->brk) != NULL;
+    return -(lxsbrk(heap, addr - heap->brk, user) == (void*)-1);
 }
 
 void*
-lxbrk(heap_context_t* heap, size_t size)
+lxsbrk(heap_context_t* heap, size_t size, int user)
 {
     if (size == 0) {
         return heap->brk;
@@ -51,18 +82,18 @@ lxbrk(heap_context_t* heap, size_t size)
 
     // any invalid situations
     if (next >= heap->max_addr || next < current_brk) {
-        return NULL;
+        __current->k_status = LXINVLDPTR;
+        return (void*)-1;
     }
 
     uintptr_t diff = PG_ALIGN(next) - PG_ALIGN(current_brk);
     if (diff) {
-        // if next do require new pages to be allocated
-        if (!vmm_alloc_pages((void*)(PG_ALIGN(current_brk) + PG_SIZE),
-                             diff,
-                             PG_PREM_RW)) {
-            // for debugging
-            assert_msg(0, "unable to brk");
-            return NULL;
+        // if next do require new pages to be mapped
+        for (size_t i = 0; i < diff; i += PG_SIZE) {
+            vmm_set_mapping(PD_REFERENCED,
+                            PG_ALIGN(current_brk) + PG_SIZE + i,
+                            0,
+                            PG_WRITE | user);
         }
     }