diff options
author | Johannes Stoelp <johannes.stoelp@gmail.com> | 2025-04-21 22:32:52 +0200 |
---|---|---|
committer | Johannes Stoelp <johannes.stoelp@gmail.com> | 2025-04-21 22:32:52 +0200 |
commit | 20a854354918735c3289c5576a28fad18ca21757 (patch) | |
tree | 7ea3a785b1fd2dffd6b04ff800abc81016387dc8 /x86-bare-metal/mbr-com-serial/mbr.zig | |
parent | a115fc9ad01a605aaad6886cd0fa8d05c328d403 (diff) | |
download | zig-playground-20a854354918735c3289c5576a28fad18ca21757.tar.gz zig-playground-20a854354918735c3289c5576a28fad18ca21757.zip |
mbr: add serial port (com) example
Diffstat (limited to 'x86-bare-metal/mbr-com-serial/mbr.zig')
-rw-r--r-- | x86-bare-metal/mbr-com-serial/mbr.zig | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/x86-bare-metal/mbr-com-serial/mbr.zig b/x86-bare-metal/mbr-com-serial/mbr.zig new file mode 100644 index 0000000..6c6fb77 --- /dev/null +++ b/x86-bare-metal/mbr-com-serial/mbr.zig @@ -0,0 +1,50 @@ +const Com = @import("com.zig").Com; + +// -- ENTRY POINT --------------------------------------------------------------- + +export fn _entry() linksection(".boot") callconv(.naked) noreturn { + asm volatile ( + // Disable interrupts. + \\cli + // Clear segment selectors. + \\xor %%ax, %%ax + \\mov %%ax, %%ds + \\mov %%ax, %%es + \\mov %%ax, %%ss + \\mov %%ax, %%fs + \\mov %%ax, %%gs + \\mov $0x7c00, %sp + // Long jump to set cs to 0x0000, as some BIOSes load the MBR + // to either 07c0:0000 or 0000:7c000. + \\ljmp $0x0, $main + ); +} + +// -- MAIN ---------------------------------------------------------------------- + +// Global access to COM0. +var com0: ?Com = null; + +// main should be "callconv(.naked)", once issue is fixed. +// https://github.com/ziglang/zig/issues/18183 +export fn main() noreturn { + com0 = com0 orelse Com.init(Com.COM1); + com0.?.puts("\n\nBooted into mbr.zig\n"); + + @panic("end reached, crashing kernel"); +} + +pub const panic = @import("std").debug.FullPanic(panicHandler); + +fn panicHandler(msg: []const u8, first_trace_addr: ?usize) noreturn { + _ = first_trace_addr; + + com0 = com0 orelse Com.init(Com.COM1); + com0.?.puts("\nPANIC: "); + com0.?.puts(msg); + com0.?.puts("\n"); + + while (true) { + asm volatile ("hlt"); + } +} |