Extra Credit¶
Up to +3% on the final grade from the exercises below, and up to +2% more from the OSlings practice problems. None of this is required, and none of it is graded on a curve — if you do the work, you get the points. The work usually outweighs the points; do it because you want the thing to exist.
Extra credit is released on ordinary exercise days, alongside the core
exercise, and is worked the same way as everything else: in class once the
day's core exercise passes, or in office hours with the TA or instructor. It
is never scheduled for a session of its own, and nothing later in the course
depends on it — skipping every item here costs nothing but the points. Submit
it with oslings submit like anything else; it is graded from your repo at
the end of the semester.
| Exercise | Released | Points | What you build |
|---|---|---|---|
14c_head |
Fri Sep 25, with 12c/13c |
+0.5 | head -n: stop early on a line-oriented stream |
41k_devices |
Thu Nov 5, with 40k |
+0.5 | The polled NS16550A UART driver |
47k_file_commands |
Thu Nov 12, with 45k/46k |
+0.5 | touch, cat, echo >, rm, rmdir over your filesystem |
54k_elf_loader |
Fri Dec 4, with 53k |
+1.0 | Teach the kernel to read a real ELF executable |
60k_preemption |
Fri Dec 4, with 53k |
+0.5 | Let the timer take the CPU away from a user process |
55k_pipes |
design-only, no starter | +1.0 | A bounded ring buffer behind two file descriptors, so a \| b works |
4.0 points are available; the total is capped at 3.
OSlings practice problems¶
The short paper problem sets on the exercises are extra credit too: up to +2% on the final grade in all, on top of the +3% above.
- Credit is for trying. A set earns its credit if you make a real try at half or more of its parts, right or wrong.
- Every set is an equal share of the +2%. There's no drop rule.
- You get your paper back on Canvas, marked up in red to show what to fix.
- Each set's answer key is posted on OSlings Practice after the set is reviewed, some with a video for each problem.
A set you didn't hand in stays blank in Canvas during the term, and blanks become zeros after the last set. So your bonus is 2% times the share of sets you got credit for. Until then Canvas counts only the sets you handed in, so the number you see can run a little high.
14c_head — stop early¶
head prints the first n lines of its input and then stops reading — and
the stopping is the exercise. cat and wc drain a stream to the end;
grep matches against every line; head is the one command whose correctness
depends on not consuming input it does not need. You parse the -n count with a
parse_usize you write yourself, iterate with ulib::Lines over a fixed
buffer, and return the moment the count is reached.
It is the same shape as 13c_grep and about the same length. It is extra
credit rather than core because the calendar has one Friday for wc and
grep together and no room for a third. Doing it makes
53k_ship_your_commands more fun in December, because there is more of your
own software to run.
41k_devices — the polled UART driver¶
The NS16550A is the template for every driver you will write: a handful of
memory-mapped registers, a status byte with flag masks, and a poll-then-transfer
loop. You write uart::getc and uart::putc against LSR.DR and LSR.THRE
with read_volatile/write_volatile, and the kernel reads its first keystroke.
It is introduced in Week 11's lecture rather than assigned because 45k_console replaces
it with an interrupt-driven version a week later, and 42k_boot_to_life
already carries the finished driver. It is a clean, satisfying exercise if you
want more practice with MMIO before the console.
47k_file_commands — the four verbs¶
Exercise 46k_shell gives rv6 a shell that moves around a namespace: pwd,
ls, cd, mkdir. This exercise teaches it to work on the things inside it —
touch, cat, echo >, rm, rmdir — each three or four lines over the
filesystem API you wrote in 40k_filesystem. The thinness is the lesson: a
good API makes its clients boring. The lecture behind it is
Week 12's, on November 10; the exercise is released with
45k/46k on November 12, two days later. 48k_user_mode carries the finished
commands, so nothing depends on your having written them.
54k_elf_loader — reading a real executable¶
Right now exec loads a flat image: the kernel copies bytes to virtual
address 0 and jumps to the first one. That works, but it is a lie of
convenience, and it is the last piece of hand-waving left in the kernel.
A flat image has no entry point, no per-segment permissions, and no .bss.
The consequences are real: your program's _start must be physically first in
the file, so adding a function above it silently breaks the program; every
page gets the same mapping, read-execute, because there is nothing to say
otherwise, so constants are executable and variables cannot be written; and
you cannot have a mutable global, because there is nowhere to put one.
An ELF file answers all three. You implement elf::load:
- Parse the ELF64 header — check the magic, the class, and that
e_machinereally is RISC-V. - Walk the program headers and map each
PT_LOADsegment at its ownp_vaddr, with the permissions itsp_flagsasks for. - Zero the tail where
p_memsz > p_filesz— that is.bss, and the C and Rust runtimes both assume it arrives zeroed. - Return
e_entrysoexeccan start the program wherever the linker put it.
About eighty lines. It is exactly what xv6's exec does, and after it, the
kernel loads a real executable format rather than a blob. It is released on
Friday, December 4 with 53k and builds on 53k's finished kernel, so start
it only once 53k, the day's core exercise, passes. It is not on the final.
One detail that trips everyone:
include_bytes!gives you a byte slice with alignment 1, so you cannot cast it to a header struct and read fields. Read each field withu32::from_le_byteson a subslice. That is both correct and the better lesson — the on-disk format is a byte layout, not a Rust struct.
Before you start, the explainer Headers Say Where steps through an example ELF file's loadable headers, each beside the page it fills, and its video walks through the explainer. Neither shows solution code.
60k_preemption — the timer takes the CPU back¶
Since 51k, rv6 runs several processes, but only cooperatively: a process
keeps the CPU until it gives it back, by waiting or by exiting. A program that
spins in a loop and never makes a system call never gives it back, and at the
rv6$ prompt it freezes the machine. The odd part is that the kernel already
gets control: the timer from 44k_interrupts still ticks while user code
runs, and every tick traps into the kernel, which hands the CPU straight back
to the same process.
Preemption is the kernel taking the CPU away from a process that did not
offer it. You give the tick that use: each process gets a time slice of a
few ticks, and after its turn it stays Runnable while round robin, your
36k policy, runs the next one. Then three programs that never stop share the
CPU in turns, and one stuck program can no longer freeze the machine.
You write preempt and its call; everything else is given. It is released on
Friday, December 4 with 53k, and its prerequisite is 51k_fork_wait:
do 53k_ship_your_commands first if you want your own commands to keep
working. Nothing later depends on it. Like 54k, start it only once 53k,
the day's core exercise, passes, and like 54k it is not on the final.
Before you start, the explainer The Timer Takes It Back steps through four spinning processes tick by tick, and its video walks through the explainer. Neither shows solution code.
55k_pipes — a | b¶
Design-only
Unlike the other extra-credit exercises, this one has no OSlings starter — the specification below is complete, but you build it against your own kernel, in office hours, rather than receiving a skeleton. Talk to the instructor if you want to take it on; it is the most substantial thing on this page. Pipes are not on the final exam, so this is entirely for its own sake.
A pipe is a bounded ring buffer with a reader end and a writer end, each
holding a file descriptor. Once it exists, the shell's | operator falls out
of machinery you already have: fork twice, dup the ends onto stdin and
stdout, exec both children.
You implement:
struct Pipe— a fixed[u8; 512]buffer with read and write cursors, and a flag for each end being open. No allocation; it lives in a fixed table behind aSpinLock, in the same style as the process table.- Blocking semantics: a reader on an empty pipe yields until a writer writes
or closes; a writer on a full pipe yields until a reader drains it. This is
where
38k_semaphorespays off. - End-of-file:
readreturns 0 when the buffer is empty and the last writer has closed. Getting this wrong is the classic pipe bug — either a reader that hangs forever or one that sees EOF too early. SYS_PIPEandSYS_DUP, using xv6's numbers so the syscall table stays honest.
The payoff¶
Not extra credit — core, and the best moment in the course. On Friday,
December 4, 53k_ship_your_commands compiles the echo, cat, wc, and
grep you wrote in September (and head, if you did it) for RISC-V and runs
them, unchanged, on the kernel you built. For reference, here is what those
commands compile to against a 64 KiB image budget (the reference solutions;
yours will differ a little):
| Command | Image | % of 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% |
That a working grep fits in under 3 KB with no allocator, no standard
library, and no runtime is worth sitting with for a moment.