Skip to content

Commit

Permalink
implement panic handler (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
saza-ku authored Dec 20, 2024
1 parent a8d13f6 commit 78ec0ee
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const log = @import("log.zig");
const fs = @import("fs.zig");
const heap = @import("heap.zig");
const mem = @import("mem.zig");
const mewz_panic = @import("panic.zig");
const uart = @import("uart.zig");
const param = @import("param.zig");
const pci = @import("pci.zig");
Expand All @@ -23,6 +24,8 @@ const wasi = @import("wasi.zig");

extern fn wasker_main() void;

pub const panic = mewz_panic.panic;

export fn bspEarlyInit(boot_magic: u32, boot_params: u32) align(16) callconv(.C) void {
const bootinfo = @as(*multiboot.BootInfo, @ptrFromInt(boot_params));
const cmdline = util.getString(bootinfo.cmdline);
Expand All @@ -43,6 +46,7 @@ export fn bspEarlyInit(boot_magic: u32, boot_params: u32) align(16) callconv(.C)
if (param.params.isNetworkEnabled()) {
virtio_net.init();
}

mem.init2();
if (param.params.isNetworkEnabled()) {
tcpip.init(param.params.addr.?, param.params.subnetmask.?, param.params.gateway.?, &virtio_net.virtio_net.mac_addr);
Expand Down
27 changes: 27 additions & 0 deletions src/panic.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const std = @import("std");
const log = @import("log.zig");

var panicked = false;

pub fn panic(msg: []const u8, _: ?*std.builtin.StackTrace, _: ?usize) noreturn {
log.fatal.printf("=== PANIC ===\n", .{});
log.fatal.printf("Message: {s}\n", .{msg});
asm volatile ("cli");

if (panicked) {
log.fatal.print("Double Panic\n");
asm volatile ("hlt");
}
panicked = true;

var it = std.debug.StackIterator.init(@returnAddress(), null);
var ix: usize = 0;
log.fatal.printf("Stack Trace:\n", .{});
while (it.next()) |frame| : (ix += 1) {
log.fatal.printf("#{d:0>2}: 0x{X:0>16}\n", .{ ix, frame });
}

asm volatile ("hlt");

unreachable;
}

0 comments on commit 78ec0ee

Please sign in to comment.