← Week 15 page
# Week 15 · Ship Your Commands ## The Friday talk · `53k`, then extra credit CS 326 Operating Systems · Friday, December 4 **Today** [`53k_ship_your_commands`](../assignments/exercises/53k_ship_your_commands/) **Extra credit, once `53k` passes** [`54k_elf_loader`](../assignments/exercises/54k_elf_loader/) (+1.0) · [`60k_preemption`](../assignments/exercises/60k_preemption/) (+0.5) [Prep for Dec 4](../prep/15-cs326-2026-12-04-prep-fork-userland-and-ship/) · [Week 15 videos](../videos/week15/) (8 to 11 are today's) · [Reading: Friday · `53k`](../lectures/15-cs326-2026-12-01-fork-wait-and-the-user-shell/#fri-53k)
Talk, then the exercises. Before you leave:
oslings submit
.
Notes: **0:00–1:00** (1 min). Today is the payoff: the `echo`, `cat`, `wc` and `grep` you wrote in `10c`–`13c` (and `head`, if you did `14c`), the same source files, not ported, not rewritten, compiled for RISC-V and running on the kernel you built. Fifteen minutes of talk, then OSlings on the class network. **Minute plan** (15 minutes) | Min | Slide | |---|---| | 0:00–1:00 | title | | 1:00–3:30 | 53k-backends | | 3:30–6:00 | 53k-ship | | 6:00–8:30 | 53k-lookup | | 8:30–11:00 | 53k-write | | 11:00–12:30 | 54k-elf | | 12:30–14:00 | 60k-preempt | | 14:00–15:00 | final | **Cut order** (when behind): 1. 54k-elf and 60k-preempt, one sentence each: "extra credit, once `53k` passes; its README has the rest". 2. 53k-ship: read only the four verbs. Never cut 53k-lookup, 53k-write or final. - **Q:** "Do I have to do the extra credit?" No. `53k` is today's exercise; `54k` (+1.0) and `60k` (+0.5) are released today too, to start only once `53k` passes. --- ## `53k` · One source, two backends Why call `ulib::write_all` when `println!` was right there? This is why. `ulib` has two backends, chosen by the target you compile for: ```rust #[cfg(not(target_os = "none"))] mod host; // std: files, process exit #[cfg(target_os = "none")] mod rv6; // ecall into YOUR kernel ``` Your command's code has no `cfg`. On `riscv64gc-unknown-none-elf`, `ulib::write_all` becomes: ```text li a7, 16 # SYS_WRITE — the number your syscall.rs gave it in exercise 48k ecall ```
Every layer between your program and the screen is yours.
Notes: **1:00–3:30** (2½ min). - On your laptop `ulib::write_all` becomes a `write(2)` to a real file descriptor; on the kernel target, these two instructions. That `ecall` traps into the trampoline you wrote in `48k`, is dispatched by the syscall table you wrote there, and reaches the file descriptor you built in `50k`. - Read `target_os = "none"` as "no operating system underneath": it follows from the target, so it cannot disagree with the build. Strictly, one `cfg_attr` line turns on `no_std` for the kernel target; the command's code never picks a backend. - Today's reading includes `ulib/src/sys/rv6.rs`, in the course repo: the header comment and `ecall3`. Compare its registers, `a7` for the number and `a0`–`a2` for the arguments, to the convention you built in `48k`. - **Q:** "So what is wrong with `println!`?" It belongs to `std`, and the kernel target has none: the crate is `no_std`, with no allocator. --- ## What `oslings ship` does rv6 has no ELF loader, so `oslings ship`: 1. **builds** each command for `riscv64gc-unknown-none-elf` 2. **flattens** the ELF's loadable segments into a raw image 3. **writes** it to `rv6/src/userbin/
.bin` 4. **regenerates** `rv6/src/userbin.rs`, whose `shipped()` returns every `(name, bytes)` pair Tiny images: `echo` is 332 bytes; `grep`, under three kilobytes of a 64 KiB budget. Notes: **3:30–6:00** (2½ min). - Build (in release mode), flatten, write, embed: the generated `userbin.rs` pulls each image into the kernel with `include_bytes!`. Before flattening, it checks the ELF really is a RISC-V executable. A flat image is bytes copied to address 0 and run from the first one, the same shape as the hand-written assembly programs in `exec.rs`. Flattening keeps the bytes and forgets the rest of what the ELF file knew; a loader that reads the ELF itself is extra credit `54k`, two slides on. - Tiny because nothing came along: no standard library, no allocator, no formatting machinery. The sizes are the README's reference solutions, and yours will differ a little: `grep` is under 5% of the budget. When you run `oslings ship`, it prints each command's name and image size. - **Q:** "Does `oslings ship` change my source?" No. It builds the same files you wrote in September; only the target changes. --- ## `lookup`: the built-ins first, then yours `run mygrep cat notes.txt`, as the README traces it: ```text 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 ``` The table already holds rv6's own `echo` and `cat` demos; without a prefix, `run echo` would quietly launch the demo. So yours are `myecho`, `mycat`, `mywc`, `mygrep`. Notes: **6:00–8:30** (2½ min). - Walk the arrows once. Only the `shipped()` line is new today; below it, the address space is `49k`'s pieces in `52k`'s `build_addrspace`, and the `sret` and `ecall` are `48k`'s. At `52k`'s `$ ` prompt, `exec` finds the shipped image too, so `mycat notes.txt` runs your September `cat`. - The README's reading, about 150 lines in order: `rv6/src/userbin.rs` (12 lines, generated; before you ship, `shipped()` returns an empty slice, which is why the kernel compiles on a fresh clone); in `exec.rs`, `Program` (two `'static` fields, `name` and `image`), `programs()` (the demo table) and `lookup` (it returns early on a hit; the fallback below is yours); then `SHIPPED_CHECKS` and `ship_self_check` in `main.rs`, where each `[fail]` line names what went wrong. - **Q:** "Without the prefix, how would I know which `echo` ran?" You would not: the output gives no way to tell. That is why the prefix exists. --- ## What you write today · `53k` **Order matters: ship first**, or there is nothing for your code to find. 1. `oslings ship`. If a command fails to build, the error names it; usually a `Vec`, `String` or `format!` slipped in. 2. In `rv6/src/exec.rs`, the `IMPLEMENT` marker in `lookup`: when the built-in table has no match, fall back to `crate::userbin::shipped()`. ```text oslings run 53k_ship_your_commands cd rv6 && cargo run rv6$ run myecho hello from my own operating system ``` Notes: **8:30–11:00** (2½ min). - The README's finish, at `rv6$`: `echo the cat sat > notes.txt`, then `run mygrep cat notes.txt` prints `the cat sat`, and `run mywc notes.txt` prints `1 3 12 notes.txt`. 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. - Done: `oslings run` is green, then `oslings submit` before you leave. Not green? Submit anyway: the tests that pass earn their share. - **Q:** "It passed `cargo test` in September. Why does `oslings ship` stop with ``cannot find macro `format` ``?" On your laptop the `std` prelude supplied it; the kernel target is `no_std`, with no allocator, so `format!`, `println!`, `Vec` and `String` do not exist there. --- ## Extra credit · `54k_elf_loader` A flat image has no entry point, no permissions, no `.bss`. The probe's ELF headers carry all three: ```text entry 0x1080 <- not the first byte! PT_LOAD vaddr 0x1000 filesz 382 memsz 382 R+X .text PT_LOAD vaddr 0x2000 filesz 89 memsz 89 R .rodata PT_LOAD vaddr 0x3000 filesz 8 memsz 16 R+W .data + 8 bytes of .bss ``` `p_memsz` beyond `p_filesz` is `.bss`: zeros you supply. **You write** `elf::load`: validate the header; map each `PT_LOAD` at its `p_vaddr`, a page at a time, with its `p_flags`, zero before copying; return `e_entry`.
+1.0 · once
53k
passes
Notes: **11:00–12:30** (1½ min). - What the flat image costs: its first byte is the entry point, so `_start` must be physically first; every page gets the same mapping, read-execute, so constants are executable and variables cannot be written; and an uninitialized global has nowhere to go, though C and Rust assume `.bss` exists and arrives zeroed. - Read each field out of the byte slice with the given `u16le`, `u32le` and `u64le`, never by casting to a `#[repr(C)]` struct: `include_bytes!` has alignment 1, and an under-aligned cast is undefined behavior. A file format is a byte layout. Validation means a 64-bit, little-endian RISC-V executable; flat programs still run, since `fill_addrspace` picks a loader by the first four bytes. Not on the exam. Video: Headers Say Where. - **Q:** "Why validate a file our own compiler made?" A kernel that trusts a file it was handed is a kernel with a security hole. --- ## Extra credit · `60k_preemption` A process keeps the CPU until it waits or exits. With **preemption**, the timer takes it back after a **time slice**, `QUANTUM` = 2 ticks: ```text time → | A A | B B | C C | A A | B B | C C | A A | ... one time slice = 2 ticks; each | is a preemption ``` **You write** `preempt` and its call in `usertrap`'s timer branch: count the tick in `(*p).ticks`; at `QUANTUM`, set it back to 0 and call `proc_yield(p)`.
+0.5 · once
53k
passes
Notes: **12:30–14:00** (1½ min). - Today `loop {}` at the `rv6$` prompt never returns, and the only way out is Ctrl-A X: the timer traps ten times a second, and the kernel hands the CPU straight back. With `preempt`, a used-up slice calls `proc_yield`, the same path `wait` has used since `51k`: the process stays `Runnable`, round robin runs the next one, and later it continues where the tick stopped it. Only who decided differs. In `usertrap`'s timer branch, the given lines clear the pending bit; your call goes after them. - Stopping anywhere is safe: `uservec` saves all 31 registers and `usertrap` saves `sepc`, the kernel side waits on the process's own kernel stack, and its page table is its own. Only user code is preempted, because rv6's kernel data has no locks. A slice trades switching cost against waiting; two ticks, about 0.2 s, is long enough to see: `run rr` prints a run of A, B or C per slice. Video: The Timer Takes It Back. - **Q:** "Why does the trapframe save every register, not just the ones a call preserves?" A tick is not a call: the program had no chance to save anything. - Not on the final. --- ## Next: the final, Tuesday, Dec 8 - [The final](../assignments/final/): the regular Tuesday room and slot; closed book, the printed Cheatsheet, no devices - A third each: Midterm 1, Midterm 2, and `49k`–`52k`; one long question: one command, keypress to next prompt - Rehearse it with today's `mycat`: [one layer per line](../lectures/15-cs326-2026-12-01-fork-wait-and-the-user-shell/#53k-layers) - [Practice Set 3](../assignments/practice-set-03/): out today, solutions Mon Dec 7; on paper first Notes: **14:00–15:00** (1 min). - There is no exam in finals week: Dec 8 is the last thing the course asks. At every layer of the long question, name the component that acts, the CSR or data structure it uses, and what the alternative would have cost. Once `53k` passes, tell the `$ mycat notes.txt` walk aloud, on the command you just shipped; the final's question walks a single operation through every layer, so practice the method, not the lines. - Then open OSlings: `53k` first, and the extra credit only after it passes. - **Q:** "Are `53k`, `54k` and `60k` on the final?" No. A third of the final is Midterm 1, a third Midterm 2, and a third `49k`–`52k` with the lectures since. The long question walks a command like today's `mycat`, but what `oslings ship` does is not asked.