hachem.wtf
aboutsummaryrefslogtreecommitdiff
path: root/boot/vmm.zig
diff options
context:
space:
mode:
Diffstat (limited to 'boot/vmm.zig')
-rw-r--r--boot/vmm.zig117
1 files changed, 112 insertions, 5 deletions
diff --git a/boot/vmm.zig b/boot/vmm.zig
index 03d89ef..c84b624 100644
--- a/boot/vmm.zig
+++ b/boot/vmm.zig
@@ -1,9 +1,9 @@
-// our own 4 level (x86_64) page tables, so we can build
+// our own 4 level (x86_64) page tables so we can build
// per process address spaces and mark pages user accessible
//
-// physical frames come from the pmm, so we reach any frame through the hhdm
-// (pmm.phystovirt) to read/write table entries. the privileged bits (reading
-// cr3, invlpg) are the only asm here
+// physical frames come from the pmm so we reach any frame through the hhdm
+// to read/write table entries. the privileged bits (reading cr3, invlpg) are the
+// only thing written in asm here
//
// see: https://wiki.osdev.org/Paging
@@ -15,6 +15,10 @@ pub const WRITE: u64 = 1 << 1;
pub const USER: u64 = 1 << 2; // 1 = ring 3 accessible
pub const NX: u64 = 1 << 63; // WARNING: NEEDS EFER.NXE DO NOT SET UNLESS ENABLED
+// a free software bit (the cpu ignores bits 9..11). we flag a page copy on write
+// here: the page is mapped read only, and a write fault copies it, see handleCow
+pub const COW: u64 = 1 << 9;
+
// bits 12..51 of an entry hold the physical frame address
const ADDR_MASK: u64 = 0x000F_FFFF_FFFF_F000;
@@ -58,7 +62,7 @@ fn index(virt: u64, level: u6) usize {
}
// return the physical base of the next level table under entry i
-// if missing, create one
+// if missing -> create one
fn nextTable(parent_phys: u64, i: usize, create: bool) ?u64 {
const entries = table(parent_phys);
const entry = entries[i];
@@ -100,6 +104,109 @@ pub fn createAddressSpace() ?u64 {
return pml4;
}
+// deeop copy one lower level table (level 0=pt, level 1=pd, level 2=pdpt)
+// subtables get fresh frames but leaf data pages are shared with the parent and
+// flipped to copy on write on both sides.
+fn copyTable(src_phys: u64, level: u6) ?u64 {
+ const new = pmm.alloc() orelse return null;
+ zeroFrame(new);
+ const src = table(src_phys);
+ const dst = table(new);
+
+ var i: usize = 0;
+ while (i < 512) : (i += 1) {
+ if (src[i] & PRESENT == 0) continue;
+ if (level == 0) {
+ var entry = src[i];
+ if (entry & WRITE != 0) {
+ entry = (entry & ~WRITE) | COW; // demote: writable -> copy on write
+ src[i] = entry; // the parent shares the same arr
+ }
+ dst[i] = entry;
+ pmm.incref(entry & ADDR_MASK);
+ } else {
+ const sub = copyTable(src[i] & ADDR_MASK, level - 1) orelse {
+ freeTable(new, level);
+ return null;
+ };
+ dst[i] = sub | (src[i] & ~ADDR_MASK);
+ }
+ }
+ return new;
+}
+
+fn freeTable(phys: u64, level: u6) void {
+ const t = table(phys);
+ var i: usize = 0;
+ while (i < 512) : (i += 1) {
+ if (t[i] & PRESENT == 0) continue;
+ if (level == 0) {
+ pmm.free(t[i] & ADDR_MASK); // decref the leaf data page
+ } else {
+ freeTable(t[i] & ADDR_MASK, level - 1);
+ }
+ }
+ pmm.free(phys); // the table frame itself is never shared
+}
+
+pub fn forkAddressSpace(parent_pml4: u64) ?u64 {
+ const child = pmm.alloc() orelse return null;
+ zeroFrame(child);
+ const src = table(parent_pml4);
+ const dst = table(child);
+
+ // kernel higher half
+ var i: usize = 256;
+ while (i < 512) : (i += 1) dst[i] = src[i];
+
+ // user half
+ i = 0;
+ while (i < 256) : (i += 1) {
+ if (src[i] & PRESENT == 0) continue;
+ const sub = copyTable(src[i] & ADDR_MASK, 2) orelse {
+ freeAddressSpace(child);
+ return null;
+ };
+ dst[i] = sub | (src[i] & ~ADDR_MASK);
+ }
+
+ loadPml4(activePml4());
+ return child;
+}
+
+pub fn freeAddressSpace(pml4: u64) void {
+ const t = table(pml4);
+ var i: usize = 0;
+ while (i < 256) : (i += 1) {
+ if (t[i] & PRESENT != 0) freeTable(t[i] & ADDR_MASK, 2);
+ }
+ pmm.free(pml4);
+}
+
+pub fn handleCow(virt: u64) bool {
+ const pml4 = activePml4();
+ const pdpt = nextTable(pml4, index(virt, 3), false) orelse return false;
+ const pd = nextTable(pdpt, index(virt, 2), false) orelse return false;
+ const pt = nextTable(pd, index(virt, 1), false) orelse return false;
+ const pte = &table(pt)[index(virt, 0)];
+ const entry = pte.*;
+ if (entry & PRESENT == 0 or entry & COW == 0) return false;
+
+ const old_phys = entry & ADDR_MASK;
+ if (pmm.refcount(old_phys) > 1) {
+ const new_phys = pmm.alloc() orelse return false;
+ const from: [*]const u8 = @ptrFromInt(pmm.physToVirt(old_phys));
+ const to: [*]u8 = @ptrFromInt(pmm.physToVirt(new_phys));
+ @memcpy(to[0..pmm.PAGE_SIZE], from[0..pmm.PAGE_SIZE]);
+ pte.* = (new_phys & ADDR_MASK) | (entry & ~ADDR_MASK & ~COW) | WRITE;
+ pmm.free(old_phys); // drop our share of the old frame
+ } else {
+ pte.* = (entry & ~COW) | WRITE; // js take write back
+ }
+ invlpg(virt);
+ return true;
+}
+
// walk the tables and return the physical address a virtual address maps to
// (page base | offset), or null if unmapped
// note: mostly for debugging