00r · Hello, Rust¶
Released Thu Aug 27, in class
This is the README OSlings gives every student for 00r_hello_rust (oslings
lesson shows it). The starter code is not published here: it reaches you
through OSlings, in class on the class network. Back to
All Exercises.
Learn → Understand → Implement. Your first Rust, and the number formats a kernel is written in. By the end you'll be reading
0x8000_0000as fluently as you read42.
Learn¶
Welcome to CS 326. Over this semester you will write an operating system — a real one, that boots on a real (emulated) RISC-V machine and runs programs you also wrote. Today we start at the other end: a single Rust function.
The first three weeks are about the language, because the kernel is
unforgiving and you should not be fighting the compiler and the hardware at
the same time. Everything you do in this part runs on your own laptop with
plain cargo test. No QEMU, no kernel, nothing to boot.
Values and bindings¶
A binding gives a name to a value:
Bindings are immutable by default — page_size = 8192 afterwards is a
compile error. That is not an inconvenience; it is the first hint of Rust's
whole personality. If a value is not supposed to change, saying so lets the
compiler catch the moment you forget. When you do want to change something,
you say so:
Types, and why a kernel cares about their size¶
Rust's integers name their size exactly: u8, u16, u32, u64, usize
(unsigned), and i8…i64, isize (signed). In application code you often
ignore this. In a kernel you cannot, because you are describing hardware that
has opinions:
- A page table entry on RISC-V is exactly 64 bits. It must be a
u64. - A UART register is exactly 8 bits. It must be a
u8. - A memory address is
usize— "big enough to hold a pointer on this machine", which on our 64-bit target is 64 bits.
Get one of these wrong and the hardware quietly does something else.
The type usually comes from context, but you can always be explicit, and in kernel code you often should:
Hex, and the underscore¶
Kernel code is full of numbers like this:
Two things are going on.
0x means hexadecimal — base 16, digits 0–9 then a–f. Hardware
addresses are always written in hex because hex digits line up with bits:
one hex digit is exactly 4 bits, so 0xFF is exactly 8 bits, all ones. Once
you are thinking about which bits are set — and once you reach the kernel
you will be, constantly — decimal becomes useless and hex becomes obvious.
The underscore is nothing. Rust lets you put _ anywhere inside a number
purely to help your eyes: 0x8000_0000 and 0x80000000 are the identical
value. The convention in this course, and in most kernels, is to group hex
in fours. Compare:
0x80000000 // how many zeros is that? count them.
0x8000_0000 // oh, it's 8 followed by seven zeros.
That number, by the way, is where our kernel will live in memory. You will type it a lot.
You can write the same value in several bases, and they are all just notation for the same bits:
| Literal | Base | Value |
|---|---|---|
4096 |
decimal | 4096 |
0x1000 |
hex | 4096 |
0o10000 |
octal | 4096 |
0b1_0000_0000_0000 |
binary | 4096 |
Functions¶
Parameter types and the return type are always written out. The body's last
expression is the return value — note there is no semicolon on it. That
is not a typo; it is how Rust distinguishes "this expression is the value of
the block" from "this is a statement, discard it". return a + b; also
works, but the bare-expression form is what you will see in real code.
This trips up everyone once. If you write a + b; with a semicolon, the
function returns () (nothing) and the compiler complains about a type
mismatch. When that happens, look for a stray semicolon.
Understand¶
Open warmup/src/lib.rs. That one file is your whole workspace for this
exercise.
At the bottom is a block that looks like this:
That is the test harness, and it is given to you — you never edit it. It
is the exact contract your code has to satisfy. #[cfg(test)] means "only
compile this when running tests", mod tests groups them, and use super::*
pulls in everything you wrote above so the tests can call it.
Read the tests before you write anything. They are the clearest statement of what each function is supposed to do — clearer than prose, because they are executable.
Implement¶
Fill in the four IMPLEMENT markers in warmup/src/lib.rs:
KERNEL_BASE— the constant0x8000_0000, written with an underscore.PAGE_SIZE— 4096, written as a hex literal.add— return the sum of its two arguments.describe_page— given an address, return which page it falls in (address divided by page size, discarding the remainder).
Check your work:
This runs cargo test on your laptop. No QEMU, no kernel — if the tests are
green, you're done.
Stuck? oslings hint.