+#include <lunaix/mm/page.h>
#include <lunaix/mm/region.h>
-#include <lunaix/mm/kalloc.h>
-#include <lunaix/process.h>
+#include <lunaix/mm/valloc.h>
+#include <lunaix/spike.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 <klibc/string.h>
- *region = (struct mm_region) {
- .attr = attr,
- .end = end,
- .start = start
- };
+struct mm_region*
+region_create(ptr_t start, ptr_t end, u32_t attr)
+{
+ assert_msg(PG_ALIGNED(start), "not page aligned");
+ assert_msg(PG_ALIGNED(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;
+}
- if (!proc->mm.regions) {
- llist_init_head(®ion->head);
- proc->mm.regions = region;
- }
- else {
- llist_append(&proc->mm.regions->head, ®ion->head);
+struct mm_region*
+region_create_range(ptr_t start, size_t length, u32_t attr)
+{
+ assert_msg(PG_ALIGNED(start), "not page aligned");
+ assert_msg(PG_ALIGNED(length), "not page aligned");
+ struct mm_region* region = valloc(sizeof(struct mm_region));
+ *region = (struct mm_region){ .attr = attr,
+ .start = start,
+ .end = start + length - 1 };
+ return region;
+}
+
+void
+region_add(vm_regions_t* lead, struct mm_region* vmregion)
+{
+ if (llist_empty(lead)) {
+ llist_append(lead, &vmregion->head);
+ return;
}
+
+ 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) {
+ break;
+ }
+ cur_end = n->end;
+ pos = n;
+ n = list_entry(n->head.next, struct mm_region, head);
+ } while ((ptr_t)&pos->head != (ptr_t)lead);
+
+ // XXX caution. require mm_region::head to be the lead of struct
+ llist_insert_after(&pos->head, &vmregion->head);
}
-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)
+ {
+ vfree(pos);
+ }
+}
+
+void
+region_copy(vm_regions_t* src, vm_regions_t* dest)
+{
+ if (!src) {
+ return;
}
- proc->mm.regions = NULL;
+ struct mm_region *pos, *n, *dup;
+
+ llist_for_each(pos, n, src, head)
+ {
+ dup = valloc(sizeof(struct mm_region));
+ memcpy(dup, pos, sizeof(*pos));
+ region_add(dest, dup);
+ }
}
-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) {
+ llist_for_each(pos, n, lead, head)
+ {
+ if (pos->start <= vaddr && vaddr < pos->end) {
return pos;
}
}