aboutsummaryrefslogtreecommitdiff
path: root/src/main.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.zig')
-rw-r--r--src/main.zig51
1 files changed, 26 insertions, 25 deletions
diff --git a/src/main.zig b/src/main.zig
index d727d6f..540e8f8 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -4,7 +4,7 @@
const std = @import("std");
-const debug_console = @import("sbi/debug_console.zig");
+const Console = @import("Console.zig");
const fdt = @import("fdt.zig");
const instructions = @import("instructions.zig");
const interrupts = @import("interrupts.zig");
@@ -25,7 +25,8 @@ pub fn panic(msg: []const u8, error_return_trace: ?*std.builtin.StackTrace, ret_
@setCold(true);
- const w = debug_console.writer() catch while (true) asm volatile ("wfi");
+ const console = Console.autoChoose() orelse while (true) asm volatile ("wfi");
+ const w = console.writer();
w.print("\r\n", .{}) catch while (true) asm volatile ("wfi");
w.print("= !! ========== !! =\r\n", .{}) catch while (true) asm volatile ("wfi");
@@ -49,20 +50,11 @@ export fn _start() callconv(.Naked) noreturn {
}
fn kmain(hart_id: usize, fdt_blob: *fdt.RawHeader) noreturn {
- const w = debug_console.writer() catch while (true) {};
- w.print("Kernel init\r\n", .{}) catch while (true) {};
-
- run(hart_id, fdt_blob, w) catch |err| std.debug.panic("Hart {d}: {any}", .{ hart_id, err });
+ run(hart_id, fdt_blob) catch |err| std.debug.panic("Hart {d}: {any}", .{ hart_id, err });
}
-fn run(hart_id: usize, fdt_blob: *fdt.RawHeader, w: debug_console.Writer) !noreturn {
- try w.print("\r\n", .{});
-
- try w.print("Hart : {d}\r\n", .{hart_id});
- try w.print("FDT address : 0x{x:0>8}\r\n", .{@intFromPtr(fdt_blob)});
-
+fn run(hart_id: usize, fdt_blob: *fdt.RawHeader) !noreturn {
if (hart_id > ~@as(u16, 0)) return Error.HartIdOutOfRange;
- if (@intFromPtr(fdt_blob) < 0xf000000) return Error.SuspiciousFdtAddr;
const dt_header = try fdt.Header.parse(fdt_blob);
@@ -70,6 +62,26 @@ fn run(hart_id: usize, fdt_blob: *fdt.RawHeader, w: debug_console.Writer) !noret
const kmem: *paging.Table = @alignCast(@ptrCast(try paging.zeroedAlloc(1)));
+ try kmem.mapKernel();
+ try kmem.mapFdt(&dt_header);
+
+ var chunk_allocator = try mem.ChunkAllocator(.{ .auto_merge_free = true }).init(128);
+ const allocator = chunk_allocator.allocator();
+
+ fdt.default = try dt_header.parseTree(allocator);
+
+ try Console.init(kmem, &fdt.default, allocator);
+
+ const console = Console.autoChoose() orelse while (true) asm volatile ("wfi");
+ const w = console.writer();
+
+ try w.print("\r\n", .{});
+ try w.print("Console init\r\n", .{});
+ try w.print("\r\n", .{});
+
+ try w.print("Hart : {d}\r\n", .{hart_id});
+ try w.print("FDT address : 0x{x:0>8}\r\n", .{@intFromPtr(fdt_blob)});
+
try w.print("\r\n", .{});
try w.print("===================== Kernel Page Table =====================\r\n", .{});
try w.print("MMIO: 0x{x:0>8} - 0x{x:0>8} -> identity mapped (rw-)\r\n", .{ paging.mmio_start, paging.mmio_end });
@@ -87,10 +99,8 @@ fn run(hart_id: usize, fdt_blob: *fdt.RawHeader, w: debug_console.Writer) !noret
try w.print("=============================================================\r\n", .{});
try w.print("\r\n", .{});
- try kmem.mapKernel();
- try kmem.mapFdt(&dt_header);
-
instructions.setSatp(kmem.satp(0));
+
try w.print("Paging : Sv39\r\n", .{});
interrupts.init();
@@ -99,15 +109,6 @@ fn run(hart_id: usize, fdt_blob: *fdt.RawHeader, w: debug_console.Writer) !noret
try w.print("\r\n", .{});
- var chunk_allocator = try mem.ChunkAllocator(.{ .auto_merge_free = true }).init(64);
- const allocator = chunk_allocator.allocator();
-
- try w.print("Parse FDT\r\n", .{});
-
- fdt.default = try dt_header.parseTree(allocator);
-
- try w.print("\r\n", .{});
-
try w.print("Timer : {d} Hz\r\n", .{1 / (@as(f64, process.schedule_interval_millis) / 1000)});
try plic.init(&fdt.default, allocator);