Zig Bare Bones: Difference between revisions

m
fix syntax highlighting error
[unchecked revision][unchecked revision]
No edit summary
m (fix syntax highlighting error)
 
(13 intermediate revisions by 4 users not shown)
Line 1:
{{Stub}}
{{TutorialExplain}}
{{Sole Editor}}
In this tutorial, we'll make a simple hello world kernel in Zig.
 
== Prerequisites ==
First off, you'll need:
* The [[Zig]] compiler, at least version 0.712.0
* [[GRUB]] as our bootloader to boot the kernel
 
Line 13 ⟶ 14:
 
=== build.zig ===
<sourcesyntaxhighlight lang="czig">
const std = @import("std");
const Builder = @import("std").build.Builder;
Line 41 ⟶ 42:
};
 
const modeoptimize = b.standardReleaseOptionsstandardOptimizeOption(.{});
 
const kernel = b.addExecutable("kernel.elf", "src/main.zig");{
.name = "kernel.setTarget(target);elf",
.root_source_file = b.path("src/main.zig"),
kernel.setBuildMode(mode);
.target = target,
kernel.setLinkerScriptPath(.{ .path = "src/linker.ld" });
kernel .code_modeloptimize = .kernel;optimize,
.code_model = .kernel,
kernel.install();
});
kernel.setLinkerScriptPathsetLinkerScript(.{ .path = "src/linker.ld" });
b.installArtifact(kernel);
 
const kernel_step = b.step("kernel", "Build the kernel");
Line 90 ⟶ 94:
run_step.dependOn(&run_cmd.step);
}
</syntaxhighlight>
</source>
 
=== src/main.zig ===
<sourcesyntaxhighlight lang="czig">
const console = @import("console.zig");
 
Line 125 ⟶ 129:
console.puts("Hello world!");
}
</syntaxhighlight>
</source>
 
=== src/console.zig ===
<sourcesyntaxhighlight lang="czig">
const fmt = @import("std").fmt;
const memWriter = @import("std").memio.Writer;
 
const VGA_WIDTH = 80;
Line 158 ⟶ 162:
var column: usize = 0;
var color = vgaEntryColor(ConsoleColors.LightGray, ConsoleColors.Black);
var buffer = @intToPtras([*]volatile u16, @ptrFromInt(0xB8000));
 
fn vgaEntryColor(fg: ConsoleColors, bg: ConsoleColors) u8 {
Line 179 ⟶ 183:
 
pub fn clear() void {
mem.set@memset(u16, buffer[0..VGA_SIZE], vgaEntry(' ', color));
}
 
Line 213 ⟶ 217:
fmt.format(writer, format, args) catch unreachable;
}
</syntaxhighlight>
</source>
 
=== src/linker.ld ===
A [[linker script]] is also needed. This file tells the linker to place our code at the base address of 1M. In general user-space programming, code are placed at much higher areas, but that requires [[virtual memory]] to be available, or else only few computers could provide such a large physical memory space.
<source lang="asm">
 
We also asks the linker to place <code>.multiboot</code> section at first, because the Multiboot specification says that the Multiboot header must be at the first 8KiB of the kernel file.
 
<pre>
ENTRY(_start)
Line 222 ⟶ 230:
. = 1M;
*(.multiboot) {
KEEP(*(.multiboot))
}
 
.text : ALIGN(4K) {
*(.multiboot)
*(.text)
}
Line 240 ⟶ 251:
}
}
</sourcepre>
 
=== src/grub.cfg ===
Finally, the last thing you need is a GRUB configuration, which tells the GRUB bootloader how to boot our kernel.
<source lang="c">
 
<code>menuentry</code> adds a menu entry to the screen. When you press ENTER in the GRUB menu, GRUB will run the commands in the menu block.
 
<code>multiboot</code> asks GRUB to load our kernel from <code>/boot/kernel.elf</code>, and GRUB automatically runs <code>boot</code> after the menu block, which will fire the boot.
 
<syntaxhighlight lang="unixconfig">
menuentry "Zig Bare Bones" {
multiboot /boot/kernel.elf
}
</syntaxhighlight>
</source>
 
== Build ==
Now that our kernel code is done, we'll now build our kernel by running the command below:
the command below:
 
<sourcesyntaxhighlight lang="bash">
$ zig build
</syntaxhighlight>
</source>
 
To boot our kernel, simply run this command:
 
<sourcesyntaxhighlight lang="bash">
$ zig build run
</syntaxhighlight>
</source>
 
[[Category: Bare bones tutorials]] [[Category:Zig]]