Something went wrong. Try again.
[WIP] A toy kernel written in Zig
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556const std = @import("std");
const kernel = @import("root.zig");
const Cause = packed struct { code: Code, is_interrupt: bool,
pub const Code = enum(u63) { syscall = 8, _ };
fn get() Cause { return asm ( \\csrrs %[ret], scause, x0 : [ret] "=r" (-> Cause), ); }};
export fn _trap() align(4) callconv(.c) noreturn { var buffer: [32]u8 = undefined; var alloc = std.heap.FixedBufferAllocator.init(&buffer); const cause = Cause.get();
if (cause.is_interrupt) { kernel.uart.putStr("INTERRUPT"); } else if (cause.code == .syscall) { kernel.uart.putStr("SYSCALL :)\n"); kernel.syscon.action(.poweroff); } else { kernel.uart.putStr("EXCEPTION"); }
const code_msg = std.fmt.allocPrint( alloc.allocator(), "{d}", .{cause.code}, ) catch "???";
kernel.uart.putStr(" with code "); kernel.uart.putStr(code_msg);
kernel.uart.putStr(" :(\n"); kernel.syscon.action(.poweroff);}
pub fn setup() void { const trap_addr = &_trap;
// Set trap handler address asm volatile ( \\csrw stvec, %[trap_addr] : : [trap_addr] "r" (trap_addr), );}