Skip to content

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, and grep you wrote in 10c–13c (and head, 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:

li   a7, 16          # SYS_WRITE — the number your syscall.rs gave it in exercise 48k
ecall

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:

  1. Builds the command for riscv64gc-unknown-none-elf in release mode.
  2. 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.)
  3. Writes that image to rv6/src/userbin/<name>.bin.
  4. Regenerates rv6/src/userbin.rs, a small module whose shipped() returns every embedded image as a (name, bytes) pair, using include_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):

  1. rv6/src/userbin.rs — all of it, 12 lines. Generated by oslings 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 one include_bytes! line per command.
  2. rv6/src/exec.rs
    • Program (lines 563-567): two fields, name and image, 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.
  3. rv6/src/main.rs: SHIPPED_CHECKS and ship_self_check (lines 288-402). Each [fail] line names what went wrong; find the check your marker satisfies.
  4. ulib/src/sys/rv6.rs (in the course repo, not the kernel): the header comment and ecall3 (lines 1-31), the backend that turns ulib::write into an ecall. 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.

  1. Ship your commands:

    oslings ship
    

    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, or format! that slipped in — none of those exist on the kernel.

  2. In rv6/src/exec.rs, fill in the IMPLEMENT marker in lookup so that when the built-in table has no match, it falls back to crate::userbin::shipped().

Check your work:

oslings run 53k_ship_your_commands

Then do the thing this whole course was building toward:

cd rv6 && cargo run
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.