From f617842dae7b8c9d6a0f1c503299ecfdd1ef5503 Mon Sep 17 00:00:00 2001 From: chloe caruso Date: Sun, 4 May 2025 08:42:28 -0700 Subject: [PATCH] initial commit --- .gitignore | 2 ++ boot.s | 28 +++++++++++++++++++++ build.zig | 32 ++++++++++++++++++++++++ grub.cfg | 3 +++ kernel.zig | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ linker.ld | 23 ++++++++++++++++++ 6 files changed, 159 insertions(+) create mode 100644 .gitignore create mode 100644 boot.s create mode 100644 build.zig create mode 100644 grub.cfg create mode 100644 kernel.zig create mode 100644 linker.ld diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d8c8979 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.zig-cache +zig-out diff --git a/boot.s b/boot.s new file mode 100644 index 0000000..ea9f682 --- /dev/null +++ b/boot.s @@ -0,0 +1,28 @@ +.section .multiboot + .align 4 + .set ALIGN, 1<<0 + .set MEMINFO, 1<<1 + .set MAGIC, 0x1BADB002 + .set FLAGS, ALIGN|MEMINFO + .long MAGIC + .long FLAGS + .long -(MAGIC + FLAGS) + +.section .bss + .align 16 +stack_bottom: + .skip 16384 +stack_top: + +.section .text +.global _start +.type _start, @function +_start: + mov $stack_top, %esp + + call kernel_main + + cli +1: hlt + jmp 1b +.size _start, . - _start \ No newline at end of file diff --git a/build.zig b/build.zig new file mode 100644 index 0000000..e33626d --- /dev/null +++ b/build.zig @@ -0,0 +1,32 @@ +pub fn build(b: *Build) !void { + const optimize = std.builtin.OptimizeMode.Debug; + const target = b.resolveTargetQuery(.{ + .cpu_arch = .x86, + .os_tag = .freestanding, + .abi = .none, + }); + const exe = b.addExecutable(.{ + .root_source_file = b.path("kernel.zig"), + .name = "multiboot", + .target = target, + .optimize = optimize, + }); + exe.addCSourceFile(.{ + .file = b.path("boot.s"), + }); + exe.setLinkerScript(b.path("linker.ld")); + + const iso_dir = b.addWriteFiles(); + _ = iso_dir.addCopyFile(exe.getEmittedBin(), "boot/os.bin"); + _ = iso_dir.addCopyFile(b.path("grub.cfg"), "boot/grub/grub.cfg"); + const make_iso = b.addSystemCommand(&.{"i686-elf-grub-mkrescue"}); + make_iso.addArg("-o"); + const iso = make_iso.addOutputFileArg("lab.iso"); + make_iso.addDirectoryArg(iso_dir.getDirectory()); + + b.getInstallStep() + .dependOn(&b.addInstallFile(iso, "lab.iso").step); +} + +const std = @import("std"); +const Build = std.Build; diff --git a/grub.cfg b/grub.cfg new file mode 100644 index 0000000..72c25b9 --- /dev/null +++ b/grub.cfg @@ -0,0 +1,3 @@ +menuentry "experiment" { + multiboot /boot/os.bin +} diff --git a/kernel.zig b/kernel.zig new file mode 100644 index 0000000..cc45e68 --- /dev/null +++ b/kernel.zig @@ -0,0 +1,71 @@ +const vga = struct { + const width = 80; + const height = 25; + const screen: *[width * height]Cell = @ptrFromInt(0xB8000); + + const Cell = packed struct(u16) { + char: u8, + style: Style = .default, + + const blank: Cell = .{ .char = ' ' }; + }; + + const Style = packed struct(u8) { + fg: Color, + bg: Color, + + pub const default: Style = .{ + .fg = .black, + .bg = .light_cyan, + }; + }; + + const Color = enum(u4) { + black = 0, + blue = 1, + green = 2, + cyan = 3, + red = 4, + magenta = 5, + brown = 6, + light_grey = 7, + dark_grey = 8, + light_blue = 9, + light_green = 10, + light_cyan = 11, + light_red = 12, + light_magenta = 13, + light_brown = 14, + white = 15, + }; + + fn init() void { + @memset(screen, .blank); + } + + fn set(x: u32, y: u32, cell: Cell) void { + screen[x + y * width] = cell; + } +}; + +var current_x: u32 = 0; +var current_y: u32 = 0; + +fn writeString(slice: []const u8, style: vga.Style) void { + for (slice) |char| { + vga.set(current_x, current_y, .{ .char = char, .style = style }); + current_x += 1; + if (current_x >= vga.width) { + current_x = 0; + current_y += 1; + if (current_y >= vga.height) { + current_y = 0; + } + } + } +} + +export fn kernel_main() callconv(.c) void { + vga.init(); + writeString("Hello Kernel", .default); +} diff --git a/linker.ld b/linker.ld new file mode 100644 index 0000000..7593d7f --- /dev/null +++ b/linker.ld @@ -0,0 +1,23 @@ +ENTRY(_start) +SECTIONS +{ + . = 2M; + .text : ALIGN(4K) + { + *(.multiboot) + *(.text) + } + .rodata : ALIGN(4K) + { + *(.rodata) + } + .data : ALIGN(4K) + { + *(data) + } + .bss : ALIGN(4K) + { + *(COMMON) + *(.bss) + } +}