Support to multi-threading and pthread interface (POSIX.1-2008) (#23)
[lunaix-os.git] / lunaix-os / kernel / mm / region.c
index 6340cfbb9d1768632abda81f34352f62898b4d26..a2bf82e3f6f877891b74cd6d676fa01cc9d03a53 100644 (file)
@@ -2,6 +2,9 @@
 #include <lunaix/mm/region.h>
 #include <lunaix/mm/valloc.h>
 #include <lunaix/spike.h>
+#include <lunaix/process.h>
+
+#include <sys/mm/mempart.h>
 
 #include <klibc/string.h>
 
@@ -24,7 +27,21 @@ region_create_range(ptr_t start, size_t length, u32_t attr)
     struct mm_region* region = valloc(sizeof(struct mm_region));
     *region = (struct mm_region){ .attr = attr,
                                   .start = start,
-                                  .end = PG_ALIGN(start + length - 1) };
+                                  .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;
 }
 
@@ -36,20 +53,19 @@ region_add(vm_regions_t* lead, struct mm_region* vmregion)
         return;
     }
 
+    struct mm_region *pos, *n;
     ptr_t cur_end = 0;
-    struct mm_region *pos = (struct mm_region*)lead,
-                     *n = list_entry(lead->next, struct mm_region, head);
-    do {
-        if (vmregion->start > cur_end && vmregion->end < n->start) {
+
+    llist_for_each(pos, n, lead, head)
+    {
+        if (vmregion->start >= cur_end && vmregion->end <= pos->start) {
             break;
         }
-        cur_end = n->end;
-        pos = n;
-        n = list_entry(n->head.next, struct mm_region, head);
-    } while ((ptr_t)&n->head != (ptr_t)lead);
+        cur_end = pos->end;
+    }
 
     // XXX caution. require mm_region::head to be the lead of struct
-    llist_insert_after(&pos->head, &vmregion->head);
+    llist_append(&pos->head, &vmregion->head);
 }
 
 void
@@ -60,7 +76,8 @@ region_release(struct mm_region* region)
     }
 
     if (region->mfile) {
-        vfs_pclose(region->mfile, region->proc_vms->pid);
+        struct proc_mm* mm = region->proc_vms;
+        vfs_pclose(region->mfile, mm->proc->pid);
     }
 
     if (region->index) {
@@ -82,7 +99,7 @@ region_release_all(vm_regions_t* lead)
 }
 
 void
-region_copy(struct proc_mm* src, struct proc_mm* dest)
+region_copy_mm(struct proc_mm* src, struct proc_mm* dest)
 {
     struct mm_region *pos, *n, *dup;
 
@@ -101,7 +118,7 @@ region_copy(struct proc_mm* src, struct proc_mm* dest)
             dup->region_copied(dup);
         }
 
-        region_add(&dest->regions, dup);
+        llist_append(&dest->regions, &dup->head);
     }
 }
 
@@ -118,7 +135,7 @@ region_get(vm_regions_t* lead, unsigned long vaddr)
 
     llist_for_each(pos, n, lead, head)
     {
-        if (pos->start <= vaddr && vaddr <= pos->end) {
+        if (pos->start <= vaddr && vaddr < pos->end) {
             return pos;
         }
     }