53k · Ship Your Commands¶
Released Fri Dec 4, in class: posted here early
This is the README OSlings will give every student for 53k_ship_your_commands (oslings
lesson shows it) when the exercise is released on Fri Dec 4. 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. The payoff. The
echo,cat,wc, andgrepyou wrote in 10c–13c (andhead, if you did the extra credit) — the same source files, not ported, not rewritten — compiled for RISC-V and running on the operating system you built.
Learn¶
Back in 10c–14c you wrote your Unix commands against a small I/O façade called
ulib. They ran on your laptop under cargo test, and at the time the façade
probably looked like unnecessary ceremony: why call ulib::write_all when
println! was right there?
This is why.
ulib has two backends, chosen by the target you compile for:
#[cfg(not(target_os = "none"))] mod host; // std: files, process exit
#[cfg(target_os = "none")] mod rv6; // ecall into YOUR kernel
Your command source has no cfg in it at all. It calls ulib::write_all, and
on your laptop that becomes a write(2) to a real file descriptor, while on
riscv64gc-unknown-none-elf it becomes:
That ecall traps into the trampoline you wrote in exercise 48k, is dispatched
by the syscall table you wrote in that same exercise, and reaches the file
descriptor you implemented in exercise 50k. Every layer between your program
and the screen is yours.
How a program gets into the kernel¶
oslings ship does four things:
- Builds the command for
riscv64gc-unknown-none-elfin release mode. - Reads the resulting ELF file, checks it really is a RISC-V executable, and
flattens its loadable segments into a raw image — the same shape as the
hand-written assembly programs in
exec.rs, because rv6 has no ELF loader. (Writing one is exercise 54k, the extra-credit ELF loader.) - Writes that image to
rv6/src/userbin/<name>.bin. - Regenerates
rv6/src/userbin.rs, a small module whoseshipped()returns every embedded image as a(name, bytes)pair, usinginclude_bytes!.
Two details worth knowing.
The images are tiny. With no standard library, no allocator, and no
formatting machinery, the whole of grep is under three kilobytes (these are
the reference solutions, built with Rust 1.89.0; yours will differ a little,
and a newer Rust moves each of these by up to about 80 bytes):
| Command | Image | Fraction of the 64 KiB budget |
|---|---|---|
echo |
332 bytes | 0.5% |
cat |
1,202 bytes | 1.8% |
wc |
1,749 bytes | 2.7% |
head |
2,839 bytes | 4.3% |
grep |
2,990 bytes | 4.6% |
They are named my<command> inside the kernel. rv6 already ships its own
hand-written assembly demos called echo and cat. Without the prefix, run
echo would quietly launch the demo rather than your program, and you would
have no way to tell from the output which one you got. So yours are myecho,
mycat, mygrep, and so on.
Understand¶
Read these, in order (line numbers are approximate; about 150 lines in all):
rv6/src/userbin.rs— all of it, 12 lines. Generated byoslings ship. Before you ship,shipped()returns an empty slice, which is why the kernel still compiles on a fresh clone; after you ship, it is oneinclude_bytes!line per command.rv6/src/exec.rsProgram(lines 563-567): two fields,nameandimage, both'static.programs()(lines 573-592): the table of built-in demos.lookup(lines 593-607): yours. It searches the table and returns early on a hit; the fallback below that is the marker.
rv6/src/main.rs:SHIPPED_CHECKSandship_self_check(lines 288-402). Each[fail]line names what went wrong; find the check your marker satisfies.ulib/src/sys/rv6.rs(in the course repo, not the kernel): the header comment andecall3(lines 1-31), the backend that turnsulib::writeinto anecall. Compare its register use (a7,a0-a2) to the syscall convention you implemented in exercise 48k.
Control flow when you type run mygrep cat notes.txt:
shell → exec::lookup("mygrep")
→ built-in table? no
→ userbin::shipped()? yes ← the line you write
→ build_addrspace: load the image at USER_CODE, map a stack, push argv
→ sret into user mode at address 0
→ your grep runs, calls ulib::read/write → ecall → your syscall table
→ exit → the shell reaps it
Implement¶
Order matters here. Ship first, then write the code — otherwise there is nothing for your code to find.
-
Ship your commands:
You should see each command's name and image size. If a command fails to build, the error names it; the usual cause is a
Vec,String, orformat!that slipped in — none of those exist on the kernel. -
In
rv6/src/exec.rs, fill in theIMPLEMENTmarker inlookupso that when the built-in table has no match, it falls back tocrate::userbin::shipped().
Check your work:
Then do the thing this whole course was building toward:
rv6$ echo the cat sat > notes.txt
rv6$ run mygrep cat notes.txt
the cat sat
rv6$ run mywc notes.txt
1 3 12 notes.txt
rv6$ run myecho hello from my own operating system
hello from my own operating system
Every layer under those lines is yours: the shell that read the command, the
fork and exec that started the program, the page table that mapped it, the
allocator that provided the page, the trap handler that caught its ecall, the
file descriptor that named the file, the driver that put the characters on the
screen, and the boot code that started the machine.
Sit with that for a second. It is a real operating system, and you wrote it.
Stuck? oslings hint.