commit 4710c97f45222dbe59c29f380461ce882093ea8e
parent 9bcb202f74089cceae8aef091d85d71405b8a0f4
Author: Sylvia Ivory <git@sivory.net>
Date: Thu, 12 Mar 2026 22:11:58 -0700
Add memory allocator
Diffstat:
3 files changed, 75 insertions(+), 54 deletions(-)
diff --git a/sylveos/loader.zig b/sylveos/loader.zig
@@ -9,8 +9,6 @@ const uart = pi.devices.mini_uart;
const mmu = pi.mmu;
const pt = pi.pt;
-const MB: u32 = 1024 * 1024;
-
// Kernel: 0x20FF_FFFF -> 0xFFFF_FFFF
// IO : 0x2000_0000 -> 0x20FF_FFFF
// User : 0x0000_0000 -> 0x2000_0000 (512mb, fine enough)
@@ -23,53 +21,3 @@ pub const Program = struct {
};
// We map kernel into high addresses
-extern const __program_start__: u32;
-extern const __program_end__: u32;
-extern const __heap_start__: u32;
-
-pub fn map_kernel() void {
- const program_start = std.mem.alignBackward(u32, @intFromPtr(&__program_start__), MB);
- const program_end = std.mem.alignForward(u32, @intFromPtr(&__program_end__), MB);
- const program_size = program_end - program_start;
-
- const stack_end = pi.get_stack_address();
- const stack_start = stack_end - MB;
- const stack_size = stack_end - stack_start;
-
- const int_stack_end = pi.get_interrupt_stack_address();
- const int_stack_start = int_stack_end - MB;
- const int_stack_size = int_stack_end - int_stack_start;
-
- const io_start = std.mem.alignBackward(u32, 0x2000_0000, MB);
- const io_end = std.mem.alignForward(u32, 0x20FF_FFFF, MB);
- const io_size = io_end - io_start;
-
- const memory_info = mailbox.get_arm_memory_info() catch unreachable;
- const memory_start = memory_info.base_address;
- const memory_end = memory_info.base_address + memory_info.memory_size;
- const memory_size = memory_end - memory_start;
-
- const heap_start = @max(
- std.mem.alignBackward(u32, @intFromPtr(&__heap_start__), MB),
- int_stack_end,
- );
- const heap_end = std.mem.alignBackward(u32, memory_end, MB);
- const heap_size = heap_end - heap_start;
-
- uart.print(
- \\Physical Memory Layout: 0x{X:0>8} - 0x{X:0>8} ({Bi})
- \\ KERNEL: 0x{X:0>8} - 0x{X:0>8} ({Bi})
- \\ KERNEL STACK: 0x{X:0>8} - 0x{X:0>8} ({Bi})
- \\KERNEL INTERRUPT STACK: 0x{X:0>8} - 0x{X:0>8} ({Bi})
- \\ HEAP: 0x{X:0>8} - 0x{X:0>8} ({Bi})
- \\ BCM2835 IO: 0x{X:0>8} - 0x{X:0>8} ({Bi})
- \\
- , .{
- memory_start, memory_end, memory_size,
- program_start, program_end, program_size,
- stack_start, stack_end, stack_size,
- int_stack_start, int_stack_end, int_stack_size,
- heap_start, heap_end, heap_size,
- io_start, io_end, io_size,
- });
-}
diff --git a/sylveos/memory.zig b/sylveos/memory.zig
@@ -0,0 +1,71 @@
+const std = @import("std");
+const pi = @import("pi");
+
+const mailbox = pi.devices.mailbox;
+const uart = pi.devices.mini_uart;
+
+const MB: u32 = 1024 * 1024;
+
+extern const __program_start__: u32;
+extern const __program_end__: u32;
+extern const __heap_start__: u32;
+
+pub fn print_regions() void {
+ const program_start = std.mem.alignBackward(u32, @intFromPtr(&__program_start__), MB);
+ const program_end = std.mem.alignForward(u32, @intFromPtr(&__program_end__), MB);
+ const program_size = program_end - program_start;
+
+ const stack_end = pi.get_stack_address();
+ const stack_start = stack_end - MB;
+ const stack_size = stack_end - stack_start;
+
+ const int_stack_end = pi.get_interrupt_stack_address();
+ const int_stack_start = int_stack_end - MB;
+ const int_stack_size = int_stack_end - int_stack_start;
+
+ const io_start = std.mem.alignBackward(u32, 0x2000_0000, MB);
+ const io_end = std.mem.alignForward(u32, 0x20FF_FFFF, MB);
+ const io_size = io_end - io_start;
+
+ const memory_info = mailbox.get_arm_memory_info() catch unreachable;
+ const memory_start = memory_info.base_address;
+ const memory_end = memory_info.base_address + memory_info.memory_size;
+ const memory_size = memory_end - memory_start;
+
+ const heap_start = std.mem.alignBackward(u32, @intFromPtr(&__heap_start__), MB);
+ const heap_end = std.mem.alignBackward(u32, memory_end, MB);
+ const heap_size = heap_end - heap_start;
+
+ uart.print(
+ \\Physical Memory Layout: 0x{X:0>8} - 0x{X:0>8} ({Bi})
+ \\ KERNEL: 0x{X:0>8} - 0x{X:0>8} ({Bi})
+ \\ KERNEL STACK: 0x{X:0>8} - 0x{X:0>8} ({Bi})
+ \\KERNEL INTERRUPT STACK: 0x{X:0>8} - 0x{X:0>8} ({Bi})
+ \\ HEAP: 0x{X:0>8} - 0x{X:0>8} ({Bi})
+ \\ BCM2835 IO: 0x{X:0>8} - 0x{X:0>8} ({Bi})
+ \\
+ , .{
+ memory_start, memory_end, memory_size,
+ program_start, program_end, program_size,
+ stack_start, stack_end, stack_size,
+ int_stack_start, int_stack_end, int_stack_size,
+ heap_start, heap_end, heap_size,
+ io_start, io_end, io_size,
+ });
+}
+
+pub fn get_allocator() std.mem.Allocator {
+ const memory_info = mailbox.get_arm_memory_info() catch unreachable;
+ const memory_start = memory_info.base_address;
+ const memory_end = memory_start + memory_info.memory_size;
+
+ const heap_start = std.mem.alignBackward(u32, @intFromPtr(&__heap_start__), MB);
+ const heap_end = std.mem.alignBackward(u32, memory_end, MB);
+ const heap_size = heap_end - heap_start;
+
+ var buffer_raw: [*]u8 = @ptrFromInt(heap_start);
+
+ var fba: std.heap.FixedBufferAllocator = .init(buffer_raw[0..heap_size]);
+
+ return fba.allocator();
+}
diff --git a/sylveos/root.zig b/sylveos/root.zig
@@ -1,6 +1,8 @@
const pi = @import("pi");
-const loader = @import("./loader.zig");
+const memory = @import("./memory.zig");
pub fn main() !void {
- loader.map_kernel();
+ const alloc = memory.get_allocator();
+ memory.print_regions();
+ _ = alloc;
}