Code-base clean-up and refactoring (#47)
[lunaix-os.git] / lunaix-os / kernel / mm / dmm.c
index 188bb0635eebf9a5771d979c55d3e04f642a74e0..50804dbf47ee46523e89e3003d2f1e6a532248fe 100644 (file)
@@ -1,18 +1,62 @@
-#include <lunaix/mm/page.h>
+#include <lunaix/mm/mmap.h>
 #include <lunaix/mm/vmm.h>
+#include <lunaix/process.h>
 #include <lunaix/status.h>
 
 #include <lunaix/spike.h>
 #include <lunaix/syscall.h>
+#include <lunaix/syscall_utils.h>
 
-__DEFINE_LXSYSCALL1(int, sbrk, size_t, size)
+void
+__heap_copied(struct mm_region* region)
 {
-    // TODO mem_remap to expand heap region
-    return 0;
+    mm_index((void**)&region->proc_vms->heap, region);
 }
 
-__DEFINE_LXSYSCALL1(void*, brk, void*, addr)
+int
+create_heap(struct proc_mm* pvms, ptr_t addr)
 {
-    // TODO mem_remap to expand heap region
-    return 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->region_copied = __heap_copied;
+    mm_index((void**)&pvms->heap, heap);
+
+    return status;
+}
+
+__DEFINE_LXSYSCALL1(void*, sbrk, ssize_t, incr)
+{
+    struct proc_mm* pvms = vmspace(__current);
+    struct mm_region* heap = pvms->heap;
+
+    assert(heap);
+    int err = mem_adjust_inplace(&pvms->regions, heap, heap->end + incr);
+    if (err) {
+        return (void*)__ptr(DO_STATUS(err));
+    }
+    return (void*)heap->end;
+}
+
+__DEFINE_LXSYSCALL1(int, brk, void*, addr)
+{
+    struct proc_mm* pvms = vmspace(__current);
+    struct mm_region* heap = pvms->heap;
+
+    if (!heap) {
+        return DO_STATUS(create_heap(pvms, (ptr_t)addr));
+    }
+
+    assert(heap);
+    int err = mem_adjust_inplace(&pvms->regions, heap, (ptr_t)addr);
+    return DO_STATUS(err);
 }
\ No newline at end of file