summaryrefslogtreecommitdiff
path: root/x86-bare-metal/mbr-com-serial/mbr.zig
blob: 6c6fb770d8870b5963bbe54b7f0f6f7e8cd07931 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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");
    }
}