A Total Overhaul on the Lunaix's Virtual Memory Model (#26)
[lunaix-os.git] / lunaix-os / kernel / mm / dmm.c
index dee5d28636fa30f5ff1fc3438762b1b5c1644662..f665e9d023863dbe0c709873edfe52c27a04683c 100644 (file)
@@ -1,89 +1,62 @@
-/**
- * @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. 
- * 
- * @version 0.2
- * @date 2022-03-3
- *
- * @copyright Copyright (c) Lunaixsky 2022
- *
- */
-
-#include <lunaix/mm/dmm.h>
+#include <lunaix/mm/mmap.h>
 #include <lunaix/mm/vmm.h>
 #include <lunaix/mm/vmm.h>
-#include <lunaix/mm/page.h>
+#include <lunaix/process.h>
 #include <lunaix/status.h>
 
 #include <lunaix/spike.h>
 #include <lunaix/status.h>
 
 #include <lunaix/spike.h>
+#include <lunaix/syscall.h>
+#include <lunaix/syscall_utils.h>
 
 
-
-int _syscall_sbrk(void* addr) {
-    heap_context_t* uheap = &__current->mm.u_heap;
-    mutex_lock(&uheap->lock);
-    int r = lxsbrk(uheap, addr);
-    mutex_unlock(&uheap->lock);
-    return r;
-}
-
-void* _syscall_brk(size_t size) {
-    heap_context_t* uheap = &__current->mm.u_heap;
-    mutex_lock(&uheap->lock);
-    void* r = lxbrk(uheap, size);
-    mutex_unlock(&uheap->lock);
-    return r;
+void
+__heap_copied(struct mm_region* region)
+{
+    mm_index((void**)&region->proc_vms->heap, region);
 }
 
 int
 }
 
 int
-dmm_init(heap_context_t* heap)
+create_heap(struct proc_mm* pvms, ptr_t addr)
 {
 {
-    assert((uintptr_t)heap->start % BOUNDARY == 0);
+    struct mmap_param map_param = { .pvms = pvms,
+                                    .vms_mnt = VMS_SELF,
+                                    .flags = MAP_ANON | MAP_PRIVATE,
+                                    .type = REGION_TYPE_HEAP,
+                                    .proct = PROT_READ | PROT_WRITE,
+                                    .mlen = PAGE_SIZE };
+    int status = 0;
+    struct mm_region* heap;
+    if ((status = mmap_user(NULL, &heap, addr, NULL, &map_param))) {
+        return status;
+    }
 
 
-    heap->brk = heap->start;
-    mutex_init(&heap->lock);
+    heap->region_copied = __heap_copied;
+    mm_index((void**)&pvms->heap, heap);
 
 
-    return vmm_alloc_page(__current->pid, heap->brk, NULL, PG_PREM_RW, 0) != NULL;
+    return status;
 }
 
 }
 
-int
-lxsbrk(heap_context_t* heap, void* addr)
+__DEFINE_LXSYSCALL1(void*, sbrk, ssize_t, incr)
 {
 {
-    return lxbrk(heap, addr - heap->brk) != NULL;
-}
+    struct proc_mm* pvms = vmspace(__current);
+    struct mm_region* heap = pvms->heap;
 
 
-void*
-lxbrk(heap_context_t* heap, size_t size)
-{
-    if (size == 0) {
-        return heap->brk;
+    assert(heap);
+    int err = mem_adjust_inplace(&pvms->regions, heap, heap->end + incr);
+    if (err) {
+        return (void*)DO_STATUS(err);
     }
     }
+    return (void*)heap->end;
+}
 
 
-    void* current_brk = heap->brk;
-
-    // The upper bound of our next brk of heap given the size.
-    // This will be used to calculate the page we need to allocate.
-    void* next = current_brk + ROUNDUP(size, BOUNDARY);
-
-    // any invalid situations
-    if (next >= heap->max_addr || next < current_brk) {
-        __current->k_status = LXINVLDPTR;
-    }
+__DEFINE_LXSYSCALL1(int, brk, void*, addr)
+{
+    struct proc_mm* pvms = vmspace(__current);
+    struct mm_region* heap = pvms->heap;
 
 
-    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(__current->pid, (void*)(PG_ALIGN(current_brk) + PG_SIZE),
-                             diff,
-                             PG_PREM_RW, 0)) {
-            __current->k_status = LXHEAPFULL;
-            return NULL;
-        }
+    if (!heap) {
+        return DO_STATUS(create_heap(pvms, (ptr_t)addr));
     }
 
     }
 
-    heap->brk += size;
-    return current_brk;
+    assert(heap);
+    int err = mem_adjust_inplace(&pvms->regions, heap, (ptr_t)addr);
+    return DO_STATUS(err);
 }
\ No newline at end of file
 }
\ No newline at end of file