A Total Overhaul on the Lunaix's Virtual Memory Model (#26)
[lunaix-os.git] / lunaix-os / kernel / mm / region.c
index 19355832762035b8e94a34c87d487ac5510e1528..7dead6da5de68826c9bfcddb7a4ad28773ecf91a 100644 (file)
 #include <lunaix/mm/region.h>
-#include <lunaix/mm/kalloc.h>
+#include <lunaix/mm/valloc.h>
+#include <lunaix/spike.h>
 #include <lunaix/process.h>
 
-void region_add(struct proc_info* proc,unsigned long start, unsigned long end, unsigned int attr) {
-    struct mm_region* region = lxmalloc(sizeof(struct mm_region));
+#include <sys/mm/mempart.h>
 
-    *region = (struct mm_region) {
-        .attr = attr,
-        .end = end,
-        .start = start
-    };
+#include <klibc/string.h>
 
-    if (!proc->mm.regions) {
-        llist_init_head(&region->head);
-        proc->mm.regions = region;
+struct mm_region*
+region_create(ptr_t start, ptr_t end, u32_t attr)
+{
+    assert_msg(!va_offset(start), "not page aligned");
+    assert_msg(!va_offset(end), "not page aligned");
+    struct mm_region* region = valloc(sizeof(struct mm_region));
+    *region =
+      (struct mm_region){ .attr = attr, .start = start, .end = end - 1 };
+    return region;
+}
+
+struct mm_region*
+region_create_range(ptr_t start, size_t length, u32_t attr)
+{
+    assert_msg(!va_offset(start), "not page aligned");
+    assert_msg(!va_offset(length), "not page aligned");
+    struct mm_region* region = valloc(sizeof(struct mm_region));
+    *region = (struct mm_region){ .attr = attr,
+                                  .start = start,
+                                  .end = ROUNDUP(start + length, MEM_PAGE) };
+    return region;
+}
+
+struct mm_region*
+region_dup(struct mm_region* origin)
+{
+    struct mm_region* region = valloc(sizeof(struct mm_region));
+    *region = *origin;
+
+    if (region->mfile) {
+        vfs_ref_file(region->mfile);
+    }
+
+    llist_init_head(&region->head);
+    return region;
+}
+
+void
+region_add(vm_regions_t* lead, struct mm_region* vmregion)
+{
+    if (llist_empty(lead)) {
+        llist_append(lead, &vmregion->head);
+        return;
+    }
+
+    struct mm_region *pos, *n;
+    ptr_t cur_end = 0;
+
+    llist_for_each(pos, n, lead, head)
+    {
+        if (vmregion->start >= cur_end && vmregion->end <= pos->start) {
+            break;
+        }
+        cur_end = pos->end;
+    }
+
+    // XXX caution. require mm_region::head to be the lead of struct
+    llist_append(&pos->head, &vmregion->head);
+}
+
+void
+region_release(struct mm_region* region)
+{
+    if (region->destruct_region) {
+        region->destruct_region(region);
+    }
+
+    if (region->mfile) {
+        struct proc_mm* mm = region->proc_vms;
+        vfs_pclose(region->mfile, mm->proc->pid);
     }
-    else {
-        llist_append(&proc->mm.regions->head, &region->head);
+
+    if (region->index) {
+        *region->index = NULL;
     }
+
+    vfree(region);
 }
 
-void region_release_all(struct proc_info* proc) {
-    struct mm_region* head = proc->mm.regions;
+void
+region_release_all(vm_regions_t* lead)
+{
     struct mm_region *pos, *n;
 
-    llist_for_each(pos, n, &head->head, head) {
-        lxfree(pos);
+    llist_for_each(pos, n, lead, head)
+    {
+        region_release(pos);
     }
+}
+
+void
+region_copy_mm(struct proc_mm* src, struct proc_mm* dest)
+{
+    struct mm_region *pos, *n, *dup;
 
-    proc->mm.regions = NULL;
+    llist_for_each(pos, n, &src->regions, head)
+    {
+        dup = valloc(sizeof(struct mm_region));
+        memcpy(dup, pos, sizeof(*pos));
+
+        dup->proc_vms = dest;
+
+        if (dup->mfile) {
+            vfs_ref_file(dup->mfile);
+        }
+
+        if (dup->region_copied) {
+            dup->region_copied(dup);
+        }
+
+        llist_append(&dest->regions, &dup->head);
+    }
 }
 
-struct mm_region* region_get(struct proc_info* proc, unsigned long vaddr) {
-    struct mm_region* head = proc->mm.regions;
-    
-    if (!head) {
+struct mm_region*
+region_get(vm_regions_t* lead, unsigned long vaddr)
+{
+    if (llist_empty(lead)) {
         return NULL;
     }
 
     struct mm_region *pos, *n;
 
-    llist_for_each(pos, n, &head->head, head) {
-        if (vaddr >= pos->start && vaddr < pos->end) {
+    vaddr = va_align(vaddr);
+
+    llist_for_each(pos, n, lead, head)
+    {
+        if (pos->start <= vaddr && vaddr < pos->end) {
             return pos;
         }
     }