sylveos

Toy Operating System
Log | Files | Refs

root.zig (2211B)


      1 const std = @import("std");
      2 const pi = @import("pi");
      3 
      4 const make = @import("boot").make;
      5 
      6 const uart = pi.devices.mini_uart;
      7 
      8 fn initialize_interrupts() void {
      9     uart.write_slice("  Disabling interrupts\n");
     10     _ = pi.interrupts.disable_interrupts();
     11     pi.mem.barrier(.Write);
     12 
     13     uart.write_slice("  Clearing interrupt flags\n");
     14     pi.interrupts.clear_interrupt_flags();
     15     pi.mem.barrier(.Write);
     16 
     17     uart.write_slice("  Setting exception vector\n");
     18     pi.interrupts.setup_exception_vector(0);
     19 }
     20 
     21 export const _start = make(kmain, abort)._start;
     22 
     23 export fn kmain() void {
     24     // We don't return so we can modify SP
     25     pi.set_sp(@ptrFromInt(pi.get_stack_address()));
     26 
     27     uart.initialize(921600, .Gpio14, .Gpio15) catch {};
     28 
     29     uart.write_slice("Initializing interrupts\n");
     30     initialize_interrupts();
     31 
     32     // uart.write_slice("Setting up stack\n");
     33     // pi.setup_stacks();
     34 
     35     uart.write_slice("Setting Cycle Counter\n");
     36     pi.cycle_counter_init();
     37 
     38     uart.write_slice("Exception Vector:\n");
     39     for (0..16) |i| {
     40         const dst: *allowzero u32 = @ptrFromInt(i * @sizeOf(*u32));
     41         uart.print("  0x{X} = 0x{X}\n", .{ @intFromPtr(dst), dst.* });
     42     }
     43 
     44     // uart.write_slice("Enabling Interrupts\n");
     45     // uart.set_tx_interrupts(true);
     46     // uart.set_rx_interrupts(true) catch {};
     47     pi.interrupts.enable_interrupts();
     48 
     49     uart.write_slice("Entering program\n");
     50     uart.flush();
     51 
     52     @import("program").main() catch |e| {
     53         uart.print("program returned error: {t}\n", .{e});
     54     };
     55 
     56     uart.flush();
     57     pi.reboot();
     58 }
     59 
     60 export fn abort() noreturn {
     61     @branchHint(.cold);
     62     pi.reboot();
     63 }
     64 
     65 fn panic_handler(msg: []const u8, trace_addr: ?usize) noreturn {
     66     @branchHint(.cold);
     67 
     68     if (uart.is_initialized()) {
     69         uart.print("kernel panic: {s} @ 0x{X}\n", .{ msg, trace_addr orelse 0 });
     70         uart.flush();
     71 
     72         // var it = std.debug.StackIterator.init(trace_addr, null);
     73         // var ix: usize = 0;
     74 
     75         // while (it.next()) |frame| : (ix += 1) {
     76         //     uart.print("| #{d:0>2}: 0x{X:0>16}\n", .{ ix, frame });
     77         //     uart.flush();
     78         // }
     79     }
     80 
     81     abort();
     82 }
     83 
     84 pub const panic = std.debug.FullPanic(panic_handler);