I wanted to learn Rust, and I know how to work the Pi from C and have a bunch of code that does that, so I thought it would be fun to try and build an interactive computer out of the Raspberry Pi from scratch, using all Rust code.

What it does

This is a bare-metal program that runs on the Raspberry Pi. It's like an operating system.

You copy it to a SD card, put it in the Pi, and turn the thing on, and plug it into a monitor and a keyboard. It gives you a console. It talks to the Pi's video card, to the serial device on the board, and to a PS/2 keyboard. You can type and see things on the screen (I bundled a font).

The console has one actual command so far: reset (which I implemented so that I could recompile and update it fast!)

All the code running on the computer (except some standard library functions that format strings and stuff) is Rust code that I wrote this weekend.

How I built it

I mostly worked off of C code I had lying around. I wrote drivers in Rust for part of the GPU, for the serial peripheral, and to turn pins on and off. I made a basic console and found a bitmap font online to display letters. I wrote a PS/2 client that uses the serial line to read scancodes from a PS/2 keyboard, so you can type.

Challenges I ran into

The hardest part was getting it to build; that took like most of a day, when I started actually trying to use the Rust standard library and it started complaining. The key insight there was using gc-sections in the linker and then stubbing out the remaining symbols.

Rust is also missing a couple important things from C: I had a ton of trouble aligning things in memory and getting data from the linker into the program. Volatile is kind of a pain.

The code is probably really un-idiomatic Rust. Tons of unsafe blocks. The I/O stuff, talking to hardware, is messier than in C, but the programming on top of the I/O layer is nicer in Rust.

Accomplishments that I'm proud of

Figuring out how to get interrupts to work, and how to pass linker information into prologue functions in Rust.

The PS/2 driver I wrote is a ton nicer in Rust than it is in C, because it's basically a state machine, and Rust has way more expressive types.

It's cool that Rust catches exceptions, and I set up a way for them to get reported to me, too. Surprisingly civilized.

What I learned

Rust is pretty neat. It's not that hard to code in it. And I like that I know what's unsafe so I can clean it up later if I want.

What's next for kernel

I was working on connecting to an ESP8266 but the power draw is too much, I think. That would let you do web browsing and wireless connection to a real computer. You could update it over the air, maybe.

Onboard programming language -- some kind of Lisp variant? This would really test whether Rust is better for general programming than C.

GUI? PS/2 mouse support would be a natural next step.

Improve style, use the concurrency stuff in Rust and make things safer. Figure out where unsafe really belongs. Get better types on things.

Some kind of persistent storage, like on the SD card? The main thing MS-DOS has that I don't right now.

Built With

Share this project: