Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
hellos
hellos.o
zig-cache
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Bare bones "hello world" i386 kernel written in [Zig](https://ziglang.org/).
## Building

```
zig build-exe hellos.zig -target i386-freestanding -T linker.ld
zig build-exe hellos.zig -target x86-freestanding -T linker.ld -O ReleaseSmall
```

## Testing with qemu
Expand Down
28 changes: 19 additions & 9 deletions hellos.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const std = @import("std");
const builtin = @import("builtin");

const MultiBoot = packed struct {
const StackTrace = std.builtin.StackTrace;

const MultiBoot = extern struct {
magic: i32,
flags: i32,
checksum: i32,
Expand All @@ -21,21 +24,28 @@ export var stack_bytes: [16 * 1024]u8 align(16) linksection(".bss") = undefined;
const stack_bytes_slice = stack_bytes[0..];

export fn _start() callconv(.Naked) noreturn {
@call(.{ .stack = stack_bytes_slice }, kmain, .{});

const stack = stack_bytes_slice;
asm volatile (
\\ movl %[stk], %esp
\\ movl %esp, %ebp
\\ jmp kmain
:
: [stk] "{ecx}" (@intFromPtr(&stack) + @sizeOf(@TypeOf(stack))),
);
while (true) {}
}

pub fn panic(msg: []const u8, error_return_trace: ?*builtin.StackTrace) noreturn {
@setCold(true);
pub fn panic(msg: []const u8, error_return_trace: ?*StackTrace) noreturn {
@branchHint(.cold);
_ = error_return_trace;
terminal.write("KERNEL PANIC: ");
terminal.write(msg);
while (true) {}
}

fn kmain() void {
export fn kmain() callconv(.C) void {
terminal.initialize();
terminal.write("Hello, Kernel World from Zig 0.7.1!");
terminal.write("Hello, Kernel World from Zig 0.14.0!");
}

// Hardware text mode color constants
Expand All @@ -62,7 +72,7 @@ fn vga_entry_color(fg: VgaColor, bg: VgaColor) u8 {
}

fn vga_entry(uc: u8, color: u8) u16 {
var c: u16 = color;
const c: u16 = color;

return uc | (c << 8);
}
Expand All @@ -76,7 +86,7 @@ const terminal = struct {

var color = vga_entry_color(VGA_COLOR_LIGHT_GREY, VGA_COLOR_BLACK);

const buffer = @intToPtr([*]volatile u16, 0xB8000);
const buffer: [*]volatile u16 = @ptrFromInt(0xB8000);

fn initialize() void {
var y: usize = 0;
Expand Down