sylveos

Toy Operating System
Log | Files | Refs

commit 0114e36802535d7acfd9e7f98e1810defe4d6d5b
parent e69b68e0abbb1777e8d83267057d60861049c7c5
Author: Sylvia Ivory <git@sivory.net>
Date:   Sat, 14 Mar 2026 16:28:43 -0700

Add page requests

Diffstat:
Msylveos/memory.zig | 12++++++++++++
Msylveos/pages.zig | 9+++++++++
2 files changed, 21 insertions(+), 0 deletions(-)

diff --git a/sylveos/memory.zig b/sylveos/memory.zig @@ -7,6 +7,8 @@ const pinned = pi.pinned; const mmu = pi.mmu; pub const MB: u32 = 1024 * 1024; +pub const KB64: u32 = 1024 * 32; +pub const KB4: u32 = 1024 * 4; extern const __program_start__: u32; extern const __program_end__: u32; @@ -196,3 +198,13 @@ pub fn translate(va: u32) !u32 { return physical_base | offset; } + +pub fn request_1mb(alloc: std.mem.Allocator) ![]u8 { + return try alloc.alignedAlloc(u8, std.mem.Alignment.fromByteUnits(MB), MB); +} +pub fn request_64kb(alloc: std.mem.Allocator) ![]u8 { + return try alloc.alignedAlloc(u8, std.mem.Alignment.fromByteUnits(KB64), KB64); +} +pub fn request_4kb(alloc: std.mem.Allocator) ![]u8 { + return try alloc.alignedAlloc(u8, std.mem.Alignment.fromByteUnits(KB4), KB4); +} diff --git a/sylveos/pages.zig b/sylveos/pages.zig @@ -1,6 +1,8 @@ const std = @import("std"); const pi = @import("pi"); +const memory = @import("./memory.zig"); + const mmu = pi.mmu; const pt = pi.pt; @@ -35,6 +37,7 @@ pub const Error = error{ TossingSection, Map64To4, Map4To64, + Unaligned, } || std.mem.Allocator.Error; // XKCD 927 @@ -65,6 +68,8 @@ fn va_to_index(va: u32) usize { } pub fn map_1mb(self: *Self, va: u32, pa: u32, attr: Attributes) !void { + if (std.mem.isAligned(pa, memory.MB)) return Error.Unaligned; + // Whole section, yippee // Just check we aren't chucking a coarse page const idx = va_to_index(va); @@ -92,6 +97,8 @@ pub fn map_1mb(self: *Self, va: u32, pa: u32, attr: Attributes) !void { } // Assume user isn't stupid and tries to map 4kb then 64kb pub fn map_64kb(self: *Self, va: u32, pa: u32, attr: Attributes) !void { + if (std.mem.isAligned(pa, memory.KB64)) return Error.Unaligned; + const idx = va_to_index(va); var current = &self.sectors[idx]; @@ -133,6 +140,8 @@ pub fn map_64kb(self: *Self, va: u32, pa: u32, attr: Attributes) !void { mmu.sync_pte(); } pub fn map_4kb(self: *Self, va: u32, pa: u32, attr: Attributes) !void { + if (std.mem.isAligned(pa, memory.KB4)) return Error.Unaligned; + const idx = va_to_index(va); var current = &self.sectors[idx];