+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, PAGE_SIZE) };
+ 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(®ion->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);