WEBVTT

00:00.000 --> 00:05.334
Week 15: Four Registers Start It, for exercise 49k_exec.

00:05.934 --> 00:12.349
A script steps this explainer, it was built with Claude, and its numbers are examples, not the lecture's.

00:12.880 --> 00:15.684
This follows the November 24 lecture.

00:16.284 --> 00:23.127
On Tuesday, December 1, you write build_process, which leaves a new process ready to start its program.

00:23.727 --> 00:26.894
No solution code here: only the machine's state.

00:27.400 --> 00:33.501
The example command, typed at the prompt: run big lanterns whistle softly before daybreak.

00:34.101 --> 00:38.485
It asks for big, a given program, with five words after its name.

00:39.085 --> 00:46.093
big's image is a few instructions, then padding. big exits 99, whatever words it is given.

00:46.600 --> 00:52.196
exec, the call that starts a program, gives the new process an address space of its own.

00:52.796 --> 00:56.389
The figure draws it as big's, high addresses at the top.

00:56.989 --> 01:01.467
Unmapped means no leaf there; a leaf is the entry naming a page.

01:02.000 --> 01:09.259
At the top sit the trampoline, given kernel code, and big's trapframe, a page that holds its registers.

01:09.859 --> 01:16.727
The stack is filled down from 0x1_1000, with the command's strings and their pointers at its top.

01:17.240 --> 01:20.209
An unmapped band, then big's image, at 0.

01:20.809 --> 01:30.314
Its first page holds big's code, then .skip's padding, part of big itself; the second, more padding, then the given loader's zero fill.

01:30.914 --> 01:36.041
Physical pages are examples, like 0x87B6_7000.

01:36.560 --> 01:42.672
The stepper follows that command from the moment exec claims a process to big's first instruction.

01:43.272 --> 01:45.399
The build is one step, step 2.

01:45.999 --> 01:53.093
Then the given return path, usertrapret and then userret, ends with sret, the return from a trap.

01:53.600 --> 01:58.374
Step 1 of the reference, the finished kernel: exec has claimed slot 0.

01:58.974 --> 02:04.943
It holds a root for big's table, with no leaf yet, a trapframe page and a kernel stack.

02:05.543 --> 02:10.300
The hart, the processor, is in supervisor mode, S, the kernel's mode.

02:11.800 --> 02:14.433
Step 2: big's table holds its leaves.

02:15.033 --> 02:20.362
In big's trapframe, the epc slot, the program's pc, holds 0.

02:20.880 --> 02:26.211
In the same trapframe, its sp slot holds 0x1_0F80;

02:26.811 --> 02:33.015
its a0 slot holds 6, and its a1 slot 0x1_0F80.

02:33.520 --> 02:42.410
The hart is still in supervisor mode, and satp, which names the page table the hart translates through, still names the kernel's table.

02:43.010 --> 02:48.055
Which page table translates the fetch of big's first instruction, at 0?

02:51.080 --> 02:55.943
Step 3: the given return path has run, and the pc is in userret.

02:56.543 --> 03:00.105
The slot is Running, and satp now names big's table.

03:00.705 --> 03:02.965
The hart is still in supervisor mode.

03:03.480 --> 03:07.251
stvec, the trap vector, holds the address TRAMPOLINE.

03:07.851 --> 03:15.811
sepc, the saved pc, holds 0, and sscratch, a scratch register, holds the address TRAPFRAME.

03:17.820 --> 03:25.599
Step 4: the hart is in user mode, U, the program's mode, and pc is 0, the value sepc held.

03:26.199 --> 03:28.693
The fetch at 0 went through big's table.

03:29.293 --> 03:31.863
The kernel's table has no leaf at 0.

03:32.360 --> 03:35.623
big's general registers, at its first instruction:

03:36.223 --> 03:39.282
sp holds 0x1_0F80;

03:39.882 --> 03:44.891
a0 holds 6, and a1 holds 0x1_0F80.

03:45.491 --> 03:50.547
These keys were typed into cargo run: big exited with status 99.

03:51.080 --> 03:53.845
Now the skeleton, the code as you receive it.

03:54.845 --> 03:59.902
Step 2: big's table, its trapframe page and its kernel stack are given back,

04:00.502 --> 04:02.689
and slot 0 is Unused again.

04:03.200 --> 04:05.909
There is no first fetch: big never ran.

04:06.509 --> 04:12.525
These keys were typed into cargo run, and the kernel printed: run: could not start the program

04:13.040 --> 04:15.578
Now kernel A, another broken kernel.

04:16.578 --> 04:23.331
Step 2: its epc slot holds 0, its sp slot 0x1_0F80;

04:23.931 --> 04:27.471
its a0 slot 0x1_0F80;

04:28.071 --> 04:30.075
its a1 slot holds 6.

04:31.600 --> 04:33.424
Step 3: the return path.

04:34.424 --> 04:40.418
Step 4, big's first instruction: a0 holds 0x1_0F80,

04:41.018 --> 04:42.616
and a1 holds 6.

04:43.216 --> 04:48.273
These keys were typed into cargo run: big exited with status 99.

04:48.800 --> 04:57.921
Four kernels were booted under cargo run: the reference, kernel A, and kernels B and C, two more broken build_process bodies.

04:58.521 --> 05:03.697
args is another given program. These keys were typed, with args in big's place:

05:04.297 --> 05:07.581
run args lanterns whistle softly before daybreak.

05:08.080 --> 05:10.938
Here is what each kernel printed after that line.

05:11.538 --> 05:13.399
The reference: status 6.

05:14.899 --> 05:18.663
Kernel A: status 69,504.

05:19.263 --> 05:23.237
Kernels B and C: status 6, the same as the reference.

05:23.760 --> 05:34.913
The course's test passes kernels B and C: for each, it printed the reference's console, byte for byte. That console is in the last section, under the reference.

05:35.513 --> 05:37.289
They pass, and they are wrong.

05:38.800 --> 05:40.483
You write build_process.

05:41.083 --> 05:44.059
When it returns Ok, the process must be ready:

05:44.659 --> 05:54.521
its first instruction will run in user mode at 0, on its own stack page, with its arguments where the calling convention, the given rule, puts them.

05:55.040 --> 06:02.677
The test runs programs of its own and stops at the first check that fails: one [fail] line, then OSLINGS:FAIL.

06:03.677 --> 06:08.612
With the skeleton, whose build_process returns an error before anything is built,

06:09.212 --> 06:12.201
the test prints: [fail] exec("args") failed

06:12.720 --> 06:22.915
The test fails kernel A with a line this page does not draw; its row says where to look: its trapframe at step 2, its registers at step 4.

06:23.915 --> 06:28.736
Kernel D is another broken build_process, with no case in the stepper.

06:29.240 --> 06:37.492
For kernel D, the test prints [ok] the program saw argc = 3, then [fail] echo did not run to completion

06:38.492 --> 06:47.068
The reference passes every check; its last check line is [ok] exec loads programs of any size and passes them arguments

06:47.600 --> 06:53.392
exec leaves big's table built, and its trapframe holding the values big starts from.

06:53.992 --> 06:58.105
Next: the explainer A Slot in Your Table, then Tuesday's session.
