Bare bones "hello world" i386 kernel written in Zig.
zig build-exe hellos.zig -target i386-freestanding -T linker.ld
qemu-system-i386 -kernel hellos
"hello world" x86 kernel example
Bare bones "hello world" i386 kernel written in Zig.
zig build-exe hellos.zig -target i386-freestanding -T linker.ld
qemu-system-i386 -kernel hellos
As title, I got this:
error: UnknownArchitecture
I found this neat little project today, and thought I should look into making it compile using Zig 0.6.0
(Mainly as a fun challenge, since I do not yet know much about Zig)
zig build-exe hellos.zig -target i386-freestanding --linker-script linker.ld
diff --git a/hellos.zig b/hellos.zig
index 8563852..506f2c9 100644
--- a/hellos.zig
+++ b/hellos.zig
@@ -20,8 +20,9 @@ export var multiboot align(4) linksection(".multiboot") = MultiBoot{
export var stack_bytes: [16 * 1024]u8 align(16) linksection(".bss") = undefined;
const stack_bytes_slice = stack_bytes[0..];
-export nakedcc fn _start() noreturn {
- @newStackCall(stack_bytes_slice, kmain);
+export fn _start() callconv(.Naked) noreturn {
+ @call(.{ .stack = stack_bytes_slice }, kmain, .{});
+
while (true) {}
}
@@ -34,7 +35,7 @@ pub fn panic(msg: []const u8, error_return_trace: ?*builtin.StackTrace) noreturn
fn kmain() void {
terminal.initialize();
- terminal.write("Hello, kernel World!");
+ terminal.write("Hello, Kernel World from Zig 0.6.0!");
}
// Hardware text mode color constants
@@ -61,23 +62,26 @@ fn vga_entry_color(fg: VgaColor, bg: VgaColor) u8 {
}
fn vga_entry(uc: u8, color: u8) u16 {
- return u16(uc) | (u16(color) << 8);
+ var c: u16 = color;
+
+ return uc | (c << 8);
}
const VGA_WIDTH = 80;
const VGA_HEIGHT = 25;
const terminal = struct {
- var row = usize(0);
- var column = usize(0);
+ var row: usize = 0;
+ var column: usize = 0;
+
var color = vga_entry_color(VGA_COLOR_LIGHT_GREY, VGA_COLOR_BLACK);
const buffer = @intToPtr([*]volatile u16, 0xB8000);
fn initialize() void {
- var y = usize(0);
+ var y: usize = 0;
while (y < VGA_HEIGHT) : (y += 1) {
- var x = usize(0);
+ var x: usize = 0;
while (x < VGA_WIDTH) : (x += 1) {
putCharAt(' ', color, x, y);
}
https://gist.github.com/peterhellberg/e81128eae5adc6293d5c1bbeef3a3380
The OS currently fails to build with a proper multiboot section on ReleaseFast and Small builds.
For the header to be valid, the entire multiboot header must apparently be in the first 8KB of the OS image, and also be 32-bit aligned. (And ideally should be included as early as possible)
As of 0.8.0-dev.2193+fe14270f4
, building in Debug and Safe seems to put the header in the 4096th byte. Building as Fast, Small, and Safe without the panic
function seems to place the header at the 8192th byte.
It seems like removing the ALIGN(4K)
for .text
from the linker.ld
makes the image valid on Fast, Small, and Safe + panic
.
diff --git a/linker.ld b/linker.ld
index 035bae7..c203a5b 100644
--- a/linker.ld
+++ b/linker.ld
@@ -3,7 +3,7 @@ ENTRY(_start)
SECTIONS {
. = 1M;
- .text : ALIGN(4K) {
+ .text : {
KEEP(*(.multiboot))
*(.text)
}
This changes the location of the header for the following build modes:
0x1000
(or 4096)0x1030
0x1024
panic
function: 0x1030
Since I don't really get the reason behind this - and hence I'm not sure what the best way to deal with the problem - I'm not going to make a PR. Well, at least for now.
I'm still a beginner in low level programming, I tried to compile but got an error,
command to compile
zig build-exe hellos.zig -target x86_64-freestanding -T linker.ld
ellos.zig:14:1: error: unable to export type 'hellos.MultiBoot'
export var multiboot align(4) linksection(".multiboot") = MultiBoot{
^~~~~~
hellos.zig:14:1: note: only extern structs and ABI sized packed structs are extern compatible
hellos.zig:3:26: note: struct declared here
const MultiBoot = packed struct {
~~~~~~~^~~~~~
/home/hexaminate/development/zig/lib/std/builtin.zig:752:9: error: expected type 'fn([]const u8, ?*builtin.StackTrace, ?usize) noreturn', found 'fn([]const u8, ?*builtin.StackTrace) noreturn'
root.panic
~~~~^~~~~~
/home/hexaminate/development/zig/lib/std/builtin.zig:752:9: note: function with 2 parameters cannot cast into a function with 3 parameters
hellos.zig:24:15: error: TODO: implement @call with stack
@call(.{ .stack = stack_bytes_slice }, kmain, .{});
my zig version:
0.11.0-dev.229+adc3fafbc
Please update the code I want to learn how to build os with zig code language
From the Multiboot Specification:
If bit 0 in the ‘flags’ word is set, then all boot modules loaded along with the operating system must be aligned on page (4KB) boundaries. Some operating systems expect to be able to map the pages containing boot modules directly into a paged address space during startup, and thus need the boot modules to be page-aligned.
If bit 1 in the ‘flags’ word is set, then information on available memory via at least the ‘mem_*’ fields of the Multiboot information structure (see Boot information format) must be included. If the boot loader is capable of passing a memory map (the ‘mmap_*’ fields) and one exists, then it may be included as well.
However, this OS does not at any point use the multiboot info structure, ever! Is this because it's meant to be used in the future?
Zig stage2 built from HEAD fails with the following error:
~/HellOS$ ../zig/build/stage2/bin/zig build-exe hellos.zig --target-os freestanding --target-arch i386 --static --linker-script linker.ld
Build 1 compile errors:
hellos.zig:14:31: error: Expected '=' or ';', found Keyword_section
export var multiboot align(4) section(".multiboot") = MultiBoot.{
~~~~~~~
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.