The Adventures of OS

RISC-V OS using Rust

Support me on Patreon! OS Blog RSS Feed Github EECS site

This is chapter 0, which is presented as a means of establishing a proper build system, of a multi-part series on writing a RISC-V OS in Rust.

Table of Contents → (Chapter 0) → Chapter 1

I Need Your Support

Writing these posts is a past-time right now as my full time job is educating (mostly) undergraduate college students. I will always deliver content, but I could really use your help. If you're willing, please support me at Patreon (sgmarz)

I've just started and there is much to do! So, please join me!

Setup and prerequisites

27 September 2019

Updated 2020

Rust now supports RISC-V out-of-the-box! This makes it much easier for us to build without needing a toolchain. You won't need a toolchain, but you will still need QEMU (the emulator) from the old tutorial at ch0.old.html

.

Rust will need a few things, which you can add using the rustup command. If you don't have rustup, you can download it at https://www.rust-lang.org/tools/install.

  • rustup default nightly
  • rustup target add riscv64gc-unknown-none-elf
  • cargo install cargo-binutils

Since I use language features (denoted by #![features]), we must use the nightly builds even though RISC-V is on the stable build.

Building

Take a look at the blog git repository at: Github. You will see a file called .cargo/config. You can edit this to match your system.


[build]
target = "riscv64gc-unknown-none-elf"
rustflags = ['-Clink-arg=-Tsrc/lds/virt.lds']

[target.riscv64gc-unknown-none-elf]
runner = "qemu-system-riscv64 -machine virt -cpu rv64 -smp 4 -m 128M -drive if=none,format=raw,file=hdd.dsk,id=foo -device virtio-blk-device,scsi=off,drive=foo -nographic -serial mon:stdio -bios none -device virtio-rng-device -device virtio-gpu-device -device virtio-net-device -device virtio-tablet-device -device virtio-keyboard-device -kernel "	

The configuration file shows what target we're building for, which is the riscv64gc target. We also need to specify our linker script so that we put things in the right location, which is in src/lds/virt.lds. Finally, a "runner" is invoked when we type cargo run, which will run the riscv64 qemu. Also, notice that there is a space after -kernel. This is because cargo will automatically specify the executable, whose name is configured through Cargo.toml.

Finished!

That's the boring part, and assuming everything went well (it does on ArchLinux!), now to the fun parts!

(Chapter 0) → Chapter 1

Stephen Marz (c) 2019
If you have a second :) ... I could surely use your support over at Patreon