Skip to content

Commit

Permalink
mem: Implement CoW
Browse files Browse the repository at this point in the history
  • Loading branch information
Qwinci committed Oct 20, 2023
1 parent ef5c84c commit 0252f50
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 13 deletions.
66 changes: 66 additions & 0 deletions src/mem/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "sched/process.h"
#include "vmem.h"
#include "string.h"
#include "utils.h"

static VMem kernel_vmem = {};
static Mutex KERNEL_VM_LOCK = {};
Expand Down Expand Up @@ -164,6 +165,47 @@ void* vm_user_alloc_backed(Process* process, void* at, usize count, PageFlags fl
return vm;
}

void* vm_user_create_cow(Process* process, void* original_map, Mapping* original, void* at) {
usize pages = ALIGNUP(original->size, PAGE_SIZE) / PAGE_SIZE;
void* vm = vm_user_alloc(process, at, pages);
if (!vm) {
return NULL;
}

PageFlags page_flags = PF_USER;
if ((original->flags & MAPPING_FLAG_R) || (original->flags & MAPPING_FLAG_W)) {
page_flags |= PF_READ;
}
if (original->flags & MAPPING_FLAG_X) {
page_flags |= PF_EXEC;
}

for (usize i = 0; i < pages; ++i) {
usize phys = arch_virt_to_phys(original_map, (usize) original->base + i * PAGE_SIZE);
if (phys) {
Page* page = page_from_addr(phys);
page->refs += 1;
arch_map_page(process->map, (usize) vm + i * PAGE_SIZE, phys, page_flags);
}
}

MappingFlags flags = original->flags | MAPPING_FLAG_COW;

mutex_lock(&process->mapping_lock);
if (!process_add_mapping(process, (usize) vm, original->size, flags)) {
mutex_unlock(&process->mapping_lock);

for (usize i = 0; i < pages; ++i) {
arch_unmap_page(process->map, (usize) vm + i * PAGE_SIZE, true);
}

vm_user_dealloc(process, vm, pages);
return NULL;
}
mutex_unlock(&process->mapping_lock);
return vm;
}

void* vm_user_alloc_on_demand(Process* process, void* at, usize count, MappingFlags flags, void** kernel_mapping) {
void* vm = vm_user_alloc(process, at, count);
if (!vm) {
Expand Down Expand Up @@ -247,3 +289,27 @@ bool vm_user_dealloc_on_demand(Process* process, void* ptr, usize count, void* k
}
return true;
}

bool vm_user_dealloc_cow(Process* process, void* ptr, usize count) {
if (!process_remove_mapping(process, (usize) ptr)) {
return false;
}
for (usize i = 0; i < count; ++i) {
usize virt = (usize) ptr + i * PAGE_SIZE;
usize phys = arch_virt_to_phys(process->map, virt);
if (!phys) {
continue;
}
arch_user_unmap_page(process, virt, true);

Page* page = page_from_addr(phys);
if (page->refs == 0) {
pfree(page, 1);
}
else {
page->refs -= 1;
}
}
vm_user_dealloc(process, ptr, count);
return true;
}
3 changes: 3 additions & 0 deletions src/mem/vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@ void* vm_user_alloc_on_demand(Process* process, void* at, usize count, MappingFl
void vm_user_dealloc_kernel(void* kernel_mapping, usize count);
bool vm_user_dealloc_backed(Process* process, void* ptr, usize count, void* kernel_mapping);
bool vm_user_dealloc_on_demand(Process* process, void* ptr, usize count, void* kernel_mapping);

void* vm_user_create_cow(Process* process, void* original_map, Mapping* original, void* at);
bool vm_user_dealloc_cow(Process* process, void* ptr, usize count);
32 changes: 19 additions & 13 deletions src/sched/process.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "mem/vm.h"
#include "task.h"
#include "mem/pmalloc.h"
#include "mem/utils.h"

Process* ACTIVE_INPUT_PROCESS = NULL;

Expand Down Expand Up @@ -172,23 +173,28 @@ bool process_handle_fault(Process* process, usize addr) {
if (!mapping || !((mapping->flags & MAPPING_FLAG_COW) | (mapping->flags & MAPPING_FLAG_ON_DEMAND))) {
return false;
}
if (!(mapping->flags & MAPPING_FLAG_R)) {
return false;
}

assert(addr >= mapping->base);
if (mapping->flags & MAPPING_FLAG_ON_DEMAND) {
Page* page = pmalloc(1);
if (!page) {
// todo oom
return false;
}

PageFlags flags = flags_to_pf(mapping->flags);

arch_user_map_page(process, addr & ~(PAGE_SIZE - 1), page->phys, flags);
arch_invalidate_mapping(process);
Page* page = pmalloc(1);
if (!page) {
// todo oom
return false;
}
else {
assert(!"cow is not implemented");

if (mapping->flags & MAPPING_FLAG_COW) {
memcpy(to_virt(page->phys), (void*) ALIGNDOWN(addr, PAGE_SIZE), PAGE_SIZE);
usize orig_addr = arch_virt_to_phys(process->map, ALIGNDOWN(addr, PAGE_SIZE));
Page* orig_page = page_from_addr(orig_addr);
assert(orig_page->refs > 0);
orig_page->refs -= 1;
}

PageFlags flags = flags_to_pf(mapping->flags);
arch_user_map_page(process, ALIGNDOWN(addr, PAGE_SIZE), page->phys, flags);
arch_invalidate_mapping(process);

return true;
}

0 comments on commit 0252f50

Please sign in to comment.