31k · Boot¶
Released Thu Oct 8, in class
This is the README OSlings gives every student for 31k_boot (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. After this exercise, your kernel boots on (virtual) hardware and prints to a real serial port.
Learn¶
In exercise 30k the crate compiled, but nothing ran. Now we make it boot — that is, we get our code running on the (virtual) machine from the very first instruction.
The memory map: every address has a meaning¶
On this machine, RAM and hardware devices live in the same address
space. Some address ranges are real memory; others are wired to a device,
and reading or writing them talks to the device instead. Which address means
what is fixed by the board's design — for us, QEMU's virt machine — and this
is its datasheet:
| Address | What lives there | We use it for |
|---|---|---|
0x0010_0000 |
"test finisher" device | power off when done |
0x1000_0000 |
UART (serial port) registers | printing text |
0x8000_0000 |
the start of normal RAM | our kernel + its stack |
So when you see a "magic" address later, it isn't magic — it's an entry in
this map. 0x8000_0000 is where usable RAM begins on this board, and
0x1000_0000 is where the serial port is wired.
What "boot" means here¶
When a real computer powers on, it runs firmware first (BIOS/UEFI on a
PC; SBI on RISC-V), which loads a bootloader, which finally loads the
operating system. We skip all of that: QEMU is launched with -bios none, so
our kernel is the first software that runs — QEMU loads it into RAM and
jumps straight to it. Four terms you will meet:
- ELF — the file format of a compiled program, with a map of where each piece goes in memory.
- Linker script (
kernel.ld) — tells the linker where to place our code and data, and names the entry point. - Entry point — the instruction the machine jumps to first; ours is
_entry. - Section — a named chunk of a program;
_entrygoes in a section called.entry, which the linker script places first, at0x8000_0000.
Put together: QEMU jumps to 0x8000_0000; our linker script guarantees
_entry is sitting there; so _entry is the very first thing that runs.
The stack problem (and what a "trampoline" is)¶
There's a catch. At reset most registers hold garbage — including the stack
pointer (sp), which holds the address of the top of the stack, the
memory that running code uses for local variables, return addresses, and
saved registers. Rust (like C) generates code that constantly uses sp, so
jumping straight into a Rust function would crash. We have to fix sp
before any Rust code runs — and the only thing that can run before Rust is a
few hand-written assembly instructions.
That stub is called a trampoline: a tiny piece of code whose only purpose
is to bounce execution from one environment (bare CPU at reset) into another
(Rust with a working stack). It lives in entry.rs:
#[no_mangle]
#[link_section = ".entry"]
pub unsafe extern "C" fn _entry() -> ! {
asm!(
"la sp, {stack}",
"li t0, {size}",
"add sp, sp, t0",
"call kmain",
stack = sym STACK0,
size = const STACK_SIZE,
options(noreturn),
);
}
The four instructions, in plain English:
la sp, {stack}— load address: put the address of our reserved stack memory (STACK0) intosp.li t0, {size}— load immediate: put the constant stack size into the scratch registert0.add sp, sp, t0—sp = sp + t0, movingspfrom the bottom of our stack memory to the top, because the stack grows downward:spmoves to lower addresses as code runs, so it must start at the highest address.call kmain— jump into our first real Rust function. From here on, Rust takes over.
New Rust pieces used above:
asm!— inline assembly.{stack}/{size}are named operands:sym STACK0substitutes a symbol's address,const STACK_SIZEa compile-time constant.options(noreturn)— control never falls out the bottom of the block; required because the function returns!(from exercise 30k).#[link_section = ".entry"]— puts this function in the.entrysection, whichkernel.ldplaces first.static mut STACK0— the stack memory itself, a large byte array in the kernel;spis pointed at its top.
Talking to hardware: MMIO¶
Once we're in kmain, we want to print — and there is no println!, because
that depends on an operating system, and we are the operating system.
Printing is one memory-mapped I/O store, exactly the one you wrote in
exercise 21r: the UART's first register at 0x1000_0000 is the transmit
register, and a byte written there goes out the serial line — which, under
QEMU's -nographic -serial mon:stdio flags, is your terminal. Look at
uart.rs: the whole driver is one write_volatile, volatile for the reason
21r gave.
Ending the test¶
How does the program stop? testdev.rs writes a special value to the test
finisher device at 0x10_0000 (another entry in the memory map), which
tells QEMU to power off. That is how the OSlings harness knows the run is
over, so it can read the serial output it captured and decide whether you
passed.
Understand¶
Read, in order: rv6/src/entry.rs, rv6/src/uart.rs, rv6/src/testdev.rs,
rv6/src/main.rs. Trace the control flow:
Implement¶
- In
rv6/src/entry.rs, fill in the four assembly instructions in theasm!block, and uncomment the two operand lines (stack = sym STACK0,andsize = const STACK_SIZE,). - In
rv6/src/main.rs, makekmainprint the lineOSLINGS:PASS.
Check your work:
This exercise boots the kernel in QEMU and passes when it sees OSLINGS:PASS
on the serial console. If the trampoline is wrong, the kernel typically
faults silently and the run times out — that is your signal that sp never
got set up correctly.
Stuck? oslings hint.