diff options
Diffstat (limited to 'src/lib/paging.zig')
-rw-r--r-- | src/lib/paging.zig | 21 |
1 files changed, 8 insertions, 13 deletions
diff --git a/src/lib/paging.zig b/src/lib/paging.zig index 7915b9b..dfe44cc 100644 --- a/src/lib/paging.zig +++ b/src/lib/paging.zig @@ -30,10 +30,11 @@ inline fn heapSize() usize { } pub const page_size: usize = 0x1000; // 4096 bytes -pub const log2_page_size: u8 = @intCast(std.math.log2(page_size)); -pub var next_mmio_vaddr: usize = 0xff000000; +var num_pages: usize = undefined; +var next_mmio_vaddr: usize = 0xff000000; +pub var alloc_start: usize = undefined; pub var kmem: *Table = undefined; pub const Error = error{ @@ -480,13 +481,17 @@ pub const Table = struct { }; pub fn init() !void { - const num_pages = heapSize() / page_size; + num_pages = heapSize() / page_size; const pages: [*]Page = @ptrCast(heap_start); for (0..num_pages) |i| { pages[i].flags = Page.Flags.clear; } + // Start allocating beyond page descriptors. + const descriptors_end = @intFromPtr(heap_start) + num_pages * @sizeOf(Page); + alloc_start = std.mem.alignForward(usize, descriptors_end, page_size); + kmem = @ptrCast(try zeroedAlloc(1)); try kmem.mapKernel(); } @@ -495,11 +500,6 @@ pub fn init() !void { pub fn alloc(n: usize) ![]align(page_size) u8 { if (n <= 0) return Error.ZeroSize; - const num_pages = heapSize() / page_size; - // Start allocating beyond page descriptors. - const pages = @intFromPtr(heap_start) + num_pages * @sizeOf(Page); - const alloc_start = std.mem.alignForwardLog2(pages, log2_page_size); - const descriptors: [*]Page = @ptrCast(heap_start); // Iterate over potential starting points. @@ -545,11 +545,6 @@ pub fn free(memory: anytype) void { const bytes_len = bytes.len + if (Slice.sentinel != null) @sizeOf(Slice.child) else 0; if (bytes_len == 0) return; - const num_pages = heapSize() / page_size; - // Start allocating beyond page descriptors. - const pages = @intFromPtr(heap_start) + num_pages * @sizeOf(Page); - const alloc_start = std.mem.alignForwardLog2(pages, log2_page_size); - // Restore the address to the page descriptor flags from the address of its contents // by restoring the descriptor number and indexing the descriptor table // at the start of the heap using it. |