diff options
-rw-r--r-- | x86-bare-metal/mbr-e820/.gitignore | 1 | ||||
-rw-r--r-- | x86-bare-metal/mbr-e820/Makefile | 29 | ||||
-rw-r--r-- | x86-bare-metal/mbr-e820/bios.zig | 77 | ||||
-rw-r--r-- | x86-bare-metal/mbr-e820/mbr.ld | 24 | ||||
-rw-r--r-- | x86-bare-metal/mbr-e820/mbr.zig | 97 |
5 files changed, 228 insertions, 0 deletions
diff --git a/x86-bare-metal/mbr-e820/.gitignore b/x86-bare-metal/mbr-e820/.gitignore new file mode 100644 index 0000000..a92a67f --- /dev/null +++ b/x86-bare-metal/mbr-e820/.gitignore @@ -0,0 +1 @@ +BUILD/
\ No newline at end of file diff --git a/x86-bare-metal/mbr-e820/Makefile b/x86-bare-metal/mbr-e820/Makefile new file mode 100644 index 0000000..5dc465f --- /dev/null +++ b/x86-bare-metal/mbr-e820/Makefile @@ -0,0 +1,29 @@ +O := BUILD + +COMMON_FLAGS := -fno-unwind-tables -fno-strip -target x86-freestanding-code16 -O ReleaseSmall -mcpu=i386 + +$(O)/boot: $(O)/boot.elf | check_ep dump_info + objcopy -O binary $< $@ + +$(O)/boot.elf: mbr.ld mbr.zig | $(O) + zig build-exe -femit-bin=$@ $(COMMON_FLAGS) -fno-entry --script $^ + +clean: + $(RM) -r $(O) + +$(O): + mkdir -p $(O) + +# Check entry point is exactly at 0x7c00. +check_ep: $(O)/boot.elf + readelf -h $< | awk '/Entry point address:/ { print $$4 }' | grep 0x7c00 >& /dev/null + +# Dump some debug info (disasm, elf load segments, code size). +dump_info: $(O)/boot.elf + objdump -Mintel -m i8086 --disassemble --visualize-jumps=extended-color $< + readelf -W -l $< + size $< + size $< | awk '/$(notdir $<)/ { print "MBR utilization " $$1/512 "%" }' + +run: $(O)/boot + qemu-system-i386 -hda $< $(QEMU_ARGS) diff --git a/x86-bare-metal/mbr-e820/bios.zig b/x86-bare-metal/mbr-e820/bios.zig new file mode 100644 index 0000000..4479c82 --- /dev/null +++ b/x86-bare-metal/mbr-e820/bios.zig @@ -0,0 +1,77 @@ +/// BIOS starts with video mode 0h. +/// * 80x25 text mode +/// * 16 colors (4bit) +/// http://www.ctyme.com/intr/rb-0069.htm +pub const COLS = 80; +pub const ROWS = 25; + +/// BIOS CALL - video teletype output. +/// http://www.ctyme.com/intr/rb-0106.htm +pub fn putc(c: u8) void { + // ah=0eh (call nr) + // al=c (ascii char to write) + const ax = (@as(u16, 0xe) << 8) | c; + + _ = asm volatile ( + \\int $0x10 + : // No outputs. + : [a] "{ax}" (ax), // BIOS call number + char to write. + : "cc" + ); + + // On linefeed also write a carriage return to move to column 0. + if (c == '\n') { + putc('\r'); + } +} + +const E820 = extern struct { + base: u64 = 0, + size: u64 = 0, + kind: u32 = 0, + attr: u32 = 1, // ACPI 3.0 compat +}; + +/// BIOS CALL - get system memory map +/// http://www.ctyme.com/intr/rb-1741.htm +/// https://wiki.osdev.org/Detecting_Memory_(x86)#BIOS_Function:_INT_0x15,_EAX_=_0xE820 +pub fn e820(next_ebx: u32) ?struct { e820: E820, ebx: u32 } { + var res = E820{}; + + // Setup call number. + var eax: u32 = 0xe820; + // Setup size of entry, bios call will return actual bytes written. + var ecx: u32 = @sizeOf(@TypeOf(res)); + // Setup continuation code, next one will be returned from the bios call. + var ebx: u32 = next_ebx; + + const carry = asm volatile ( + \\int $0x15 + \\setc %[c] + : // Outputs. + [n] "+{eax}" (eax), + [l] "+{ecx}" (ecx), + [o] "+{ebx}" (ebx), + [c] "={dl}" (-> u8), // Returns carry bit, recycle dl from dx. + : // Inputs. + [p] "{edi}" (&res), // Pointer to E820 entry. + [m] "{edx}" (0x534D4150), // Magic number (b"SMAP"). + : // Clobbers. + "cc" + ); + + // Call failed if carry is set. + if (carry == 1) { + return null; + } + // Call failed if magic number is not returned in eax. + if (eax != 0x534D4150) { + return null; + } + // Call failed if partial entry is written. + if (ecx < 20) { + return null; + } + + return .{ .e820 = res, .ebx = ebx }; +} diff --git a/x86-bare-metal/mbr-e820/mbr.ld b/x86-bare-metal/mbr-e820/mbr.ld new file mode 100644 index 0000000..9ada8c4 --- /dev/null +++ b/x86-bare-metal/mbr-e820/mbr.ld @@ -0,0 +1,24 @@ +/*OUTPUT_FORMAT(binary)*/ +OUTPUT_FORMAT(elf32-i386) +OUTPUT_ARCH(i386) +ENTRY(_entry) + +SECTIONS { + . = 0x7c00; + .boot : { *(.boot) } + .text : { *(.text) } + .data : { *(.data) } + .rodata : { *(.rodata) *(.rodata.str*) } + _boot_end = .; + + . = 0x7c00 + 510; + .mbr.magic : { + BYTE(0x55); + BYTE(0xaa); + } + + /*/DISCARD/ : { *(.*) }*/ + /*rest : { *(.*) }*/ + + ASSERT(_boot_end - 0x7c00 < 510, "boot sector must fit in 510 bytes") +} diff --git a/x86-bare-metal/mbr-e820/mbr.zig b/x86-bare-metal/mbr-e820/mbr.zig new file mode 100644 index 0000000..3b6f71d --- /dev/null +++ b/x86-bare-metal/mbr-e820/mbr.zig @@ -0,0 +1,97 @@ +const bios = @import("bios.zig"); + +// -- 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 ---------------------------------------------------------------------- + +// main should be "callconv(.naked)", once issue is fixed. +// https://github.com/ziglang/zig/issues/18183 +export fn main() noreturn { + dump_memmap(); + + while (true) { + asm volatile ("hlt"); + } +} + +// -- UTILS --------------------------------------------------------------------- + +fn dump_memmap() void { + // Keep track of continuatation code. + var off: u32 = 0; + // Accumulated free/usable memory. + var mem: u64 = 0; + + // Iterate over E820 entries. + while (bios.e820(off)) |entry| : (off = entry.ebx) { + puts_hex(entry.e820.base); + bios.putc('-'); + puts_hex(entry.e820.size); + bios.putc('-'); + puts_hex(entry.e820.kind); + bios.putc('-'); + puts_hex(entry.e820.attr); + bios.putc('\n'); + + // Accumulate free, useable memory. + if (entry.e820.kind == 1 and entry.e820.attr == 1) { + mem += entry.e820.size; + } + + // All E820 entries retrieved, continuing would start from the beginning. + if (entry.ebx == 0) { + break; + } + } + + // Report total free memory in MBs. + bios.putc('m'); + bios.putc('='); + puts_hex(mem / 1024 / 1024); +} + +fn puts(str: []const u8) void { + for (str) |c| { + bios.putc(c); + } +} + +fn puts_hex(init_val: u64) void { + if (init_val == 0) { + bios.putc('0'); + return; + } + + var val = init_val; + var buf: [32]u8 = undefined; + var idx = buf.len; + + while (val > 0 and idx != 0) : (val /= 16) { + idx -= 1; + const digit = switch (@as(u8, @truncate(val % 16))) { + 0...9 => |d| '0' + d, + 10...15 => |d| 'a' + d - 10, + else => '?', + }; + buf[idx] = digit; + } + puts(buf[idx..]); +} |