WEBVTT

00:00.000 --> 00:05.552
Midterm 1, Section 01, Problem 5: The Free List and a Page Table.

00:06.152 --> 00:11.590
We'll work it out on the exam's own pages, as the answer key's handwriting appears.

00:12.190 --> 00:16.082
A script reveals it, and this explainer was built with Claude.

00:16.600 --> 00:22.589
For part (a): 32k_physical_memory, from week 7, on Thursday, October 8.

00:23.189 --> 00:25.095
You wrote kfree and kalloc:

00:25.695 --> 00:28.712
push a page onto the free list, and pop one off.

00:29.240 --> 00:36.043
For parts (b), (c) and (d): 33k_paging, from week 7, on Friday, October 9.

00:36.643 --> 00:39.947
You wrote Pte::new, Pte::pa and walk,

00:40.547 --> 00:46.731
which goes down the tables and, when asked to, makes a new, zeroed table wherever one is missing.

00:47.840 --> 00:57.077
Problem 5 opens with exercise 32k's page allocator, and the page-table entries of exercise 33k.

00:58.077 --> 01:03.956
A page is PGSIZE bytes: 4096, or 0x1000.

01:04.556 --> 01:09.377
And the four flags, V, R, W and X, are 1, 2, 4 and 8.

01:09.880 --> 01:11.894
A Run is a single pointer, next:

01:12.494 --> 01:14.593
a free page's first 8 bytes.

01:15.193 --> 01:21.057
The variable FREELIST is the head of the list: the first free page, or null when there is none.

01:21.560 --> 01:24.620
kfree pushes a page onto the front of the list.

01:25.220 --> 01:27.809
It writes the current head into the page's next,

01:28.409 --> 01:30.136
then makes that page the head.

01:30.640 --> 01:32.815
kalloc pops one off the front.

01:33.415 --> 01:38.543
It takes the head, and if that is not null, the head moves on to that page's next.

01:39.143 --> 01:42.471
It returns the page, or null when the list is empty.

01:43.000 --> 01:47.030
A Pte is one page-table entry, a single number.

01:47.630 --> 01:50.251
Pte::new packs an address and its flags:

01:50.851 --> 01:55.059
shift right by 12, then left by 10, and OR in the flags.

01:55.560 --> 02:02.386
Pte::pa undoes the shifts: right by 10, which drops the flags, then left by 12.

02:02.986 --> 02:05.657
And a call to is_valid tests the V bit.

02:06.160 --> 02:12.805
An Sv39 virtual address is three 9-bit indices over a 12-bit offset.

02:13.405 --> 02:18.139
And a PTE packs a physical page number above ten bits of flags.

02:18.739 --> 02:23.717
The lowest four are X, W, R and V, worth 8, 4, 2 and 1.

02:25.240 --> 02:33.814
A page table is one 4096-byte page, holding 512 PTEs of 8 bytes each.

02:34.414 --> 02:37.591
VPN[2] picks an entry of the root table,

02:38.191 --> 02:45.719
VPN[1] an entry of a level-1 table, and VPN[0] an entry of a level-0 table.

02:46.240 --> 02:48.309
Two kinds of valid PTE.

02:48.909 --> 02:54.433
With R, W and X all clear, it is a branch: its address is the next table.

02:55.033 --> 02:59.402
With any of them set, it is a leaf: its address is the mapped page.

02:59.920 --> 03:03.746
For a page-aligned pa, the low 12 bits are zeros,

03:04.346 --> 03:08.825
so the two shifts of Pte::new are just a divide by 4.

03:09.425 --> 03:16.413
For example, 0x8000_4000 divided by 4 is 0x2000_1000.

03:16.920 --> 03:22.176
map_page maps one virtual page, as 33k's mappages does.

03:22.776 --> 03:24.472
It starts at the root table.

03:25.072 --> 03:30.371
At level 2, and then at level 1, it looks at the entry that level's index picks.

03:30.880 --> 03:36.915
If that entry is not valid, it takes a page with kalloc, and fills it with zeros.

03:37.515 --> 03:40.875
Then it writes a branch into the entry, with only V.

03:41.475 --> 03:44.909
Either way, it follows the entry down to the next table.

03:45.440 --> 03:52.933
At level 0, it writes the leaf, Pte::new of pa, with perm and V, into entry VPN[0].

03:53.533 --> 03:58.610
In 33k, walk went down the tables and mappages wrote the leaf.

03:59.210 --> 04:00.734
map_page does both.

04:02.240 --> 04:05.953
Part (a). The free list starts with four pages, then null.

04:06.553 --> 04:09.400
The kernel runs the four lines below, in order.

04:10.000 --> 04:15.290
For each: what it returns, and FREELIST afterwards. The first line is done for you.

04:15.800 --> 04:17.271
kalloc takes the head:

04:17.871 --> 04:21.976
the first line returns 0x8040_8000.

04:22.576 --> 04:30.179
And FREELIST moves on to that page's next, 0x8040_4000, the second page on the list.

04:30.680 --> 04:33.113
The second line runs kalloc again.

04:34.113 --> 04:36.659
It returns the head, page 4000.

04:37.659 --> 04:42.139
And FREELIST moves on to the page after it on the list: C000.

04:42.640 --> 04:45.779
The third line, kfree(a), returns nothing.

04:46.379 --> 04:50.431
It gives back page 8000, the page the first line took.

04:51.031 --> 04:54.816
kfree writes the head, C000, into that page's next,

04:55.816 --> 05:00.901
then makes it the head: FREELIST is 0x8040_8000.

05:01.400 --> 05:04.086
The fourth line runs kalloc once more.

05:05.086 --> 05:07.548
It pops the head, page 8000 again:

05:08.148 --> 05:11.356
the page freed last is the first one handed out.

05:12.356 --> 05:18.317
And FREELIST moves on to that page's next: C000, the link kfree saved there.

05:19.240 --> 05:24.049
Right after kfree(a), what do the first 8 bytes of page a hold?

05:25.549 --> 05:28.130
0x8040_C000.

05:28.730 --> 05:33.475
Those 8 bytes are the page's next, where kfree stored the old FREELIST:

05:34.075 --> 05:39.439
the head before page 8000 went back on, not page 8000 itself.

05:41.160 --> 05:43.560
Part (b) continues from part (a).

05:44.160 --> 05:48.617
The kernel fills page b with zeros, and makes it the root table.

05:49.217 --> 05:55.242
Page b is 0x8040_4000, the page the second line returned.

05:55.760 --> 05:58.110
And the free list is as part (a) left it.

05:58.710 --> 06:00.330
FREELIST is C000,

06:00.930 --> 06:09.003
and the next page after C000 is still 0x8041_0000, as at the start. Then null.

06:09.520 --> 06:11.321
Then the kernel calls map_page,

06:11.921 --> 06:21.210
to map the virtual page 0x0040_3000 to the physical page 0x8020_0000, readable and writable.

06:21.810 --> 06:29.333
Its indices are given: VPN[2] is 0, VPN[1] is 2, and VPN[0] is 3.

06:30.440 --> 06:35.484
Fill in the three PTEs the call writes. The first row is done for you.

06:36.084 --> 06:43.448
Each row gives the table the entry is in, its index, the value written, branch or leaf, and what it points at.

06:44.560 --> 06:50.957
The first row is level 2: the root, at 0x8040_4000, index 0.

06:51.557 --> 06:55.258
The root was just zeroed, so that entry is not valid.

06:55.858 --> 07:01.185
map_page takes a page with kalloc, the head of the list: page C000.

07:01.720 --> 07:05.877
It fills that page with zeros, and writes a branch into the entry:

07:06.477 --> 07:09.271
Pte::new of the page, with only V.

07:09.871 --> 07:18.370
The page's address divided by 4, with V ORed in: the 0x2010_3001 printed in the row.

07:18.880 --> 07:21.687
Either way, map_page follows the entry down.

07:22.287 --> 07:27.023
So the second row is at level 1, in the table the root entry points at:

07:28.023 --> 07:31.781
0x8040_C000, at index 2.

07:32.280 --> 07:36.620
That table was just zeroed too, so its entry 2 is not valid.

07:37.220 --> 07:39.661
kalloc takes the next page on the list:

07:40.661 --> 07:43.526
0x8041_0000.

07:44.126 --> 07:48.382
Not C000 again: the root's kalloc already took that one.

07:48.880 --> 07:52.613
The entry gets Pte::new of that page, with only V:

07:53.613 --> 07:56.151
0x2010_4001,

07:56.751 --> 08:00.411
which is that address divided by 4, with V ORed in.

08:01.411 --> 08:04.951
A branch: V is set, and R, W and X are clear.

08:05.880 --> 08:09.377
Then level 0, in the table the second row points at:

08:10.377 --> 08:14.307
0x8041_0000, at index 3.

08:15.307 --> 08:21.140
At level 0, map_page writes the leaf: Pte::new of pa, with perm and V.

08:21.640 --> 08:24.391
A leaf points at the mapped page itself, pa:

08:25.391 --> 08:28.108
0x8020_0000.

08:28.708 --> 08:34.174
Its value is that address divided by 4, with R, W and V ORed in:

08:35.174 --> 08:38.524
0x2008_0007.

08:39.124 --> 08:41.837
And it is a leaf: R and W are set.

08:42.360 --> 08:44.311
After the call, what is FREELIST?

08:45.811 --> 08:46.579
null.

08:48.079 --> 08:55.937
map_page called kalloc twice on the way down, for C000 and then the page after it: the rest of the list.

08:57.040 --> 08:59.898
The key's note also works out the leaf's value.

09:00.898 --> 09:07.657
0x8020_0000 divided by 4 is 0x2008_0000.

09:08.257 --> 09:11.937
Then OR in R plus W plus V, which is 7.

09:13.640 --> 09:16.391
Part (c) uses the tables of part (b).

09:16.991 --> 09:22.148
(i): where does the virtual address 0x0040_3ABC translate to?

09:22.680 --> 09:25.720
Its low 12 bits are the offset: ABC.

09:26.320 --> 09:33.610
The bits above them name the page that part (b) mapped, 0x0040_3000, with the same three indices.

09:34.210 --> 09:36.267
So the walk ends at the same leaf.

09:38.300 --> 09:41.393
0x8020_0ABC.

09:42.393 --> 09:44.356
The leaf's page, plus the offset.

09:44.956 --> 09:51.078
The 3 before ABC is part of VPN[0]: it is translated, not copied.

09:51.600 --> 09:59.181
(ii): a program uses the virtual address 0x0040_4000. Its indices are 0, 2 and 4.

09:59.781 --> 10:01.487
What happens? Circle one.

10:02.000 --> 10:04.995
The walk follows the same two branches as before,

10:05.595 --> 10:09.039
to the same level-0 table, and looks at entry 4.

10:09.639 --> 10:14.183
map_page filled that table with zeros, and wrote only entry 3.

10:14.680 --> 10:18.273
So entry 4 is still 0, and its V bit is clear.

10:19.273 --> 10:24.515
(A): a page fault. The entry is not valid, so there is nothing to translate.

10:25.440 --> 10:31.720
Not (B) or (C): entry 4 is an entry of its own, and nothing maps it to any page.

10:32.320 --> 10:35.848
And not (D): the MMU never calls kalloc.

10:36.448 --> 10:39.231
Only the kernel does, as map_page did.

10:40.760 --> 10:42.941
Part (d): work these by hand.

10:43.541 --> 10:50.447
(i): a PTE holds 0x2000_C00B. Circle every flag that is set.

10:50.960 --> 10:52.942
The flags are the low 10 bits.

10:53.542 --> 10:57.565
Here, only the last hex digit, B, has any of them set.

10:58.165 --> 11:00.607
The C sits higher, in the page number.

11:01.120 --> 11:02.678
So which flags are set?

11:04.178 --> 11:05.477
V, R and X.

11:06.477 --> 11:10.303
0xB is 8 plus 2 plus 1: X, R and V.

11:10.903 --> 11:13.100
W, which is 4, is clear.

11:13.600 --> 11:16.029
(ii): that PTE's address, pa.

11:16.629 --> 11:23.641
pa shifts right by 10, which drops the flags, then left by 12, which makes it an address again.

11:25.141 --> 11:27.678
0x8003_0000.

11:28.278 --> 11:33.012
Its low 12 bits are zero: pa always gives the start of a page.

11:33.520 --> 11:35.585
(iii): is it a branch or a leaf?

11:36.585 --> 11:38.471
A leaf: R and X are set.

11:39.000 --> 11:47.108
(iv): a table is at 0x8040_2000, and each PTE is 8 bytes. Where is entry 3?

11:48.608 --> 11:51.615
0x8040_2018.

11:52.615 --> 11:57.785
Entry 3 is 3 times 8, 0x18 bytes, past the start of the table.

11:58.880 --> 12:00.435
That's Problem 5.

12:01.035 --> 12:04.766
(a): kfree pushes, kalloc pops, both at the front.

12:05.366 --> 12:08.863
(b): a missing table is a fresh page from kalloc.

12:09.463 --> 12:11.944
(c): a zeroed entry is not valid.

12:12.544 --> 12:15.977
(d): a PTE is a page number above its flags.
