Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement panic handler #70

Merged
merged 1 commit into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
Loading