12 ms·
Minimal x86 Kernel Zig
- ajxs 8mo agoSee also: https://wiki.osdev.org/Zig_Bare_Bones https://wiki.osdev.org/Zig_Bare_Bones
- csense 8mo agoVery neat. To clarify, Qemu can boot it, but I'm pretty sure you need some bootloader (e.g. Grub) to boot it on a physical system.
- toast0 8mo agoLooks like it's multiboot compliant, so you can pick your favorite multiboot loader. ipxe, grub, pretty sure there's some other ones out there. As it's multiboot, it should likely run on v86 too. It's always fun to have an in browser demo of a little OS like this.
- flopsamjetsam 8mo agoFrom the GitHub page: > It boots on an x86 (i386) machine via the Multiboot 1 protocol Yes, it does need a compliant bootloader on virtual or physical hardware.
- anta40 8mo agoIf you want to use Grub, this tutorial works (see "Booting the kernel"): https://wiki.osdev.org/Zig_Bare_Bones https://wiki.osdev.org/Zig_Bare_Bones Yes, just tried it.
- throwaway27448 8mo agoWhy choose intel? Let's build bootable software in 2026
- xx__yy 8mo agoI'm not that cluey, but from the README it sounds like it can be compiled for a bunch of arches
- asQuirreL 8mo agoIt can be (cross-)compiled on whatever architectures the Zig compiler is available for, but the source contains inline x86 assembly, so you're not going to be able to build this for ARM or RISC-V.
- mghackerlady 7mo agoFor how short it is, it would be trivially easy to translate the instructions into whatever flavor of CPU made in the past 25 years or so
- xx__yy 8mo agoGold! I see Zig, I upvote!
- smallstepforman 8mo agoWell, in the real world we need at least polymorphism and operator overloading, but that is against the core Zig philosophy, so serious GameDev ignores it (which ironically one would think is the biggest core market for low level systems programming). Hence why new GameDev development still chooses C++, and Andrew’s project fails to gain a significant boost in users.
- flohofwoe 8mo ago> in the real world we need at least polymorphism and operator overloading Maybe in your real-world ;) Building your game code around classes with virtual methods has been a bad idea since at least the early 2000s (but both static and dynamic polymorphism is something that Zig can do just fine when needed), and the only important use case for operator overloading in game-dev is vector/matrix math, where Zig is going down a different road (using builting vector types, which maybe one day will be extended with a builtin matrix type - there is some interest in using Zig for GPU code, and at least this use cases will require proper vector/matrix primitives - but not operator overloading).
- mghackerlady 7mo ago>polymorphism and operator overloading Gee, this new C language and UNIX operating system sure seem cool but someone ought to tell those poor people at bell labs that they'll never catch on without them
- Perenti 7mo agoSo use Zig to implement Smalltalk. All problems solved.
- xkriva11 7mo agoOr use existing one: https://github.com/Zag-Research/Zag-Smalltalk https://github.com/Zag-Research/Zag-Smalltalk
- drnick1 8mo agoWhat's the point of doing this in "Zig" instead of C, the traditional choice for this kind of thing?
- mitchellh 8mo agoFun?
- nickmonad 7mo agoNot allowed.
- deleted 8mo ago[deleted]
- toast0 8mo agoBecause you can is a pretty traditional reason. Zig is supposed to be an improvement upon C, so doing C things with it seems reasonable. Kind of neat that there's no need for a separate assembly file although there is inline assembly. Might get better (or worse) syntax support for separate assembly files? But it doesn't make a big difference until there's more features that need it (interrupts, threads/processes and maintaining their stacks, syscalls, starting other processors, etc)
- 6r17 8mo agoWhat's the point writing another kernel in C ???
- lelanthran 8mo agoConsidering that we are talking about experimental toys which have lower odds of seeing production than of you winning a national lottery jackpot, the point of writing it in C would be the same as the point of writing it in anything else - IOW, the kernel is the objective, not the language used to write it. .
- mghackerlady 7mo ago
- 6r17 8mo agoI'm very surprised it's *that* short - handling one in rust i'm surprised by the very low amount of code to get that up. Thanks or sharing that was a first time reading some Zig for me !
- pmarreck 8mo agowhat you’re experiencing is more or less why I am building some stuff in Zig instead of Rust
- simonask 8mo agoLooking at the code, I'm not really sure what part of this would be more verbose in Rust. This kernel does close to nothing, not even page table setup. Granted, the code writing to the VGA buffer will need to be in `unsafe` blocks, but yeah.
- karlosvomacka 7mo agoit doesn't have a bootloader.
- aryalaadi 8mo agothats not a kernel
- benatkin 8mo agoIndeed, it's freestanding, the repo name doesn't have the correct term, but the source file does near the end: https://github.com/lopespm/zig-minimal-kernel-x86/blob/main/src/main.zig#L98 https://github.com/lopespm/zig-minimal-kernel-x86/blob/main/... Also baremetal where the metal is virtual. LLVM uses this term for when an OS isn't available https://github.com/llvm/llvm-project/tree/main/libc/src/stdlib/baremetal https://github.com/llvm/llvm-project/tree/main/libc/src/stdl...
- aryalaadi 8mo agoI think calling baremetal software a kernel keeps a bad impression on people just starting out, you can do alot with baremetal and it does not have to be a kernel
- messe 8mo agoMinimal slop.
- WD-42 8mo agoHere's one for Risc-V that's a little more fleshed out, also in Zig: https://github.com/Fingel/aeros-v/blob/main/src/kernel.zig https://github.com/Fingel/aeros-v/blob/main/src/kernel.zig
- feb 7mo agoThat one actually looks more like a real kernel with services like memory management, syscalls, processes and context switching. Nice work.
- gethly 8mo agowtf? 10 lines of hello world code is not a kernel.
- kunley 8mo agoWhy to spread confusion and call it bare metal when it's run under QEMU? Then it's not bare metal at all. In order to be run on bare metal it's needing another bootloader which the documentation only barely mentions. More on the naming: why to call it kernel?
- cies 8mo agoI agree, I'd not call this a kernel. It does not allow any software to be run on top of it. It just prints text to screen and halts. Even saying it "runs" on QEMU is a far stretch: it "halts", that's all it does. :) (it does run on hardware as per other commenters in this HN convo)
- kunley 8mo agoOk, I am not saying it doesn't run on hardware, but the primary example runs (for the somehow stretched definition of "run", as you say) on QEMU but displays a message that it's bare metal. Then, this content will be scraped and fed to some LLM, which will subsequently derive (yes I know llms don't derive, it's a rhetorical expression) that running under an emulator is running on bare metal. Confusion for the masses! (Not to mention confusion for a reader already now)
- cies 8mo agoIt does not "run" anything: it halts. :)
- lelanthran 8mo ago> In order to be run on bare metal it's needing another bootloader which the documentation only barely mentions. Maybe it's an in-group vs out-group thing: those in the group (i.e. have attempted this in the past) don't care about what the first stage bootloader is; you'll just use some existing bootloader (I used grub). If you're in the out-group, you feel cheated that you still need a bootloader.
- eddd-ddde 7mo agoYou still need a bootloader to run the Linux kernel.
- boricj 8mo agoI wrote something similar a while ago: https://github.com/boricj/hang-os https://github.com/boricj/hang-os It handles interrupts/traps and targets the aarch64 QEMU virt platform. It also features a HAL.
- lopespm 7mo agoOP here, thanks for all the comments! In the meanwhile added the capability to build a bootable ISO with a GRUB menu https://github.com/lopespm/zig-minimal-kernel-x86 https://github.com/lopespm/zig-minimal-kernel-x86
- qaqqqqaq 7mo ago[dead]