hachem.wtf
aboutsummaryrefslogtreecommitdiff
path: root/boot/pmm.zig
diff options
context:
space:
mode:
authorhachem <im@hachem.wtf>2026-10-17 12:24:17 +0200
committerhachem <im@hachem.wtf>2026-10-17 12:24:17 +0200
commit20a921db8bd8d5e99c0e9dcc72f22ac00a5e5222 (patch)
tree3454816f3642fe468f806aac495c04e55993b102 /boot/pmm.zig
parent6a5543bb8f1f535ca49737bcd7be0cafca3b2a08 (diff)
feat: fork/exec & wait
Diffstat (limited to 'boot/pmm.zig')
-rw-r--r--boot/pmm.zig43
1 files changed, 34 insertions, 9 deletions
diff --git a/boot/pmm.zig b/boot/pmm.zig
index 1112134..3e580df 100644
--- a/boot/pmm.zig
+++ b/boot/pmm.zig
@@ -20,6 +20,9 @@ var hhdm_offset: u64 = 0;
// reached through the hhdm, so its just a normal slice we write through
var bitmap: []u8 = &[_]u8{};
+// one reference count per frame
+var refcounts: []u8 = &[_]u8{};
+
var total_frames: usize = 0; // frames the bitmap can index (up to highest usable end)
var usable_frames: usize = 0; // frames that started free
var free_frames: usize = 0; // frames currently free
@@ -67,20 +70,23 @@ pub fn init(hhdm: *const limine.HhdmResponse, memmap: *const limine.MemoryMapRes
}
total_frames = highest / PAGE_SIZE;
const bitmap_bytes = (total_frames + 7) / 8;
+ const refcount_bytes = total_frames; // one u8 per frame
+ const storage_bytes = bitmap_bytes + refcount_bytes;
- // park the bitmap in the first usable region big enough to hold it
var storage_base: u64 = 0;
for (entries) |entry| {
- if (entry.type == .usable and entry.length >= bitmap_bytes) {
+ if (entry.type == .usable and entry.length >= storage_bytes) {
storage_base = entry.base;
break;
}
}
- if (storage_base == 0 and bitmap_bytes > 0) @panic("pmm: no room for the frame bitmap");
+ if (storage_base == 0 and storage_bytes > 0) @panic("pmm: no room for the frame bookkeeping");
const ptr: [*]u8 = @ptrFromInt(physToVirt(storage_base));
bitmap = ptr[0..bitmap_bytes];
+ refcounts = ptr[bitmap_bytes..storage_bytes];
+ @memset(refcounts, 0);
- // start with everything used, then punch out the usable regions
+ // start with everything used then punch out the usable regions
@memset(bitmap, 0xFF);
for (entries) |entry| {
if (entry.type == .usable) {
@@ -99,7 +105,7 @@ pub fn init(hhdm: *const limine.HhdmResponse, memmap: *const limine.MemoryMapRes
// last frame the bitmap spills into still gets reserved
{
const start = storage_base / PAGE_SIZE;
- const end = (storage_base + bitmap_bytes + PAGE_SIZE - 1) / PAGE_SIZE;
+ const end = (storage_base + storage_bytes + PAGE_SIZE - 1) / PAGE_SIZE;
var i = start;
while (i < end) : (i += 1) {
if (i < total_frames) reserve(i);
@@ -109,9 +115,11 @@ pub fn init(hhdm: *const limine.HhdmResponse, memmap: *const limine.MemoryMapRes
}
// reserve = mark used
+// thatsa it
fn reserve(i: usize) void {
if (!frameUsed(i)) {
markUsed(i);
+ refcounts[i] = 1;
free_frames -= 1;
}
}
@@ -125,6 +133,7 @@ pub fn alloc() ?u64 {
if (i >= total_frames) i = 0;
if (!frameUsed(i)) {
markUsed(i);
+ refcounts[i] = 1;
free_frames -= 1;
next_hint = i + 1;
return @as(u64, i) * PAGE_SIZE;
@@ -134,6 +143,16 @@ pub fn alloc() ?u64 {
return null;
}
+pub fn incref(phys: u64) void {
+ const i = phys / PAGE_SIZE;
+ if (i < total_frames) refcounts[i] += 1;
+}
+
+pub fn refcount(phys: u64) u8 {
+ const i = phys / PAGE_SIZE;
+ return if (i < total_frames) refcounts[i] else 0;
+}
+
// hand out count contiguous physical frames cause the heap needs one flat buffer and a
// single usable region is contiguous in physical space, so this just finds a run of count free bits
// note: returns the base physical address of the run
@@ -147,6 +166,7 @@ pub fn allocContig(count: usize) ?u64 {
var j: usize = 0;
while (j < count) : (j += 1) {
markUsed(i + j);
+ refcounts[i + j] = 1;
free_frames -= 1;
}
return @as(u64, i) * PAGE_SIZE;
@@ -157,13 +177,18 @@ pub fn allocContig(count: usize) ?u64 {
return null;
}
+// drop one reference
pub fn free(phys: u64) void {
const i = phys / PAGE_SIZE;
- if (i < total_frames and frameUsed(i)) {
- markFree(i);
- free_frames += 1;
- if (i < next_hint) next_hint = i;
+ if (i >= total_frames or !frameUsed(i)) return;
+ if (refcounts[i] > 1) {
+ refcounts[i] -= 1;
+ return;
}
+ refcounts[i] = 0;
+ markFree(i);
+ free_frames += 1;
+ if (i < next_hint) next_hint = i;
}
pub fn freeBytes() u64 {