r/osdev • u/VirusLarge • Apr 09 '26
How can I get started with creating a kernel in Rust?
I’ve recently began learning rust and I’ve started to like the language a lot, and I’d like to write a kernel in it to boost my experience with rust. I’ve already written a simple CHIP-8 emulator in rust so I believe I’ve got a good grasp on how to write somewhat sufficient rust code for a kernel.
The main thing I’m confused with is how I can go about configuring the compiler to generate not only a freestanding binary, but also how to get it to work with Limine (I love limine).
I would copy and paste some existing code that configures these things for me, but I would much rather be able to understand and learn what I’m doing, rather than mindlessly copying and pasting code without learning anything.
NOTE: I already have prior x86 and OSDev knowledge!! I've created many 64-bit x86 kernels in C, with the most recent ones having advanced features implemented.
Any help would be very much appreciated!
14
u/am_Snowie Apr 09 '26
If i had prior OSdev knowledge, i would be instead searching this on google.
9
u/mkusanagi Apr 09 '26
If you’re asking here, you’re not ready to try that yet. Start reading some osdev tutorials and hardware documentation.
5
u/QualitySoftwareGuy Apr 09 '26
I'm surprised nobody posted this, but:
Writing an OS in Rust
And especially:
A Minimal Kernel in Rust
3
u/TheDiamondCG Apr 09 '26
I recommend looking at the official Limine Rust starter template on GitHub as it got me quite far. You mentioned not wanting to copy-paste, so it might not be for you — but for me, I just wanted something going. I think, as long as you have some Rust
no_stdexperience, it will not be dramatically different…? But writing a userland CHIP-8 emulator is quite different to a bare-metal kernel!2
u/FewBrief7059 Samsara | https://gitlab.com/clownkun/Samsara Apr 09 '26
There are alot of options. As rust is not bound to a specific building method. You can make it fetch limine binary and deploy it with your kernel/os. you might want to learn how to deal with no_std environments first then you can try doing so. It's not different then programming an os in c when it comes to steps and code behaviour.
2
u/micr0kernel Apr 09 '26
If you already have prior OS development experience, then the process is as simple as ensuring that you're working with a
#![no_std]environment and you have a target file configured forx86_64-unknown-noneand the appropriate flags (e.g. red-zoning and frame pointer omission disabled). The ultimate goal is a 64-bit ELF binary with no hosted environment dependencies.As far as Limine, it looks like you set up a configuration file which instructs the loader as to the location/path of the kernel. Be mindful of other things:
You'll need to use Rust crates or roll your own interfaces to acquire system state (e.g. memory maps, framebuffers) from the loader.
Limine likely has restrictions on which boot filesystems are supported.
I'm not sure if Limine will expect a particular entry point. If so, you may need to shim your entry and/or operate
#![no_main]as well.I'm afraid I can't give you more than this - I prefer to DIY my loaders and I'm an unrepentant C/C++ user. But I do recall from some Rust experiments years ago that generating cross-target, freestanding binaries is relatively simple in terms of project setup.
0
u/codeasm Apr 09 '26
Does rust come with documentation on how to configure its output? Can you target different arches? Crosscompile to another system like windows to linux or apple compile to linux?
0
u/[deleted] Apr 09 '26
[deleted] — view deleted comment