The Two-Level Page Table
A flat page table for 32-bit addresses needs a million entries, used or not. Cut the table in two levels, and the parts that nobody uses need not exist.
You Will Be Able To
- Work out the size of a flat page table, and say why it is too large.
- Split a 32-bit address into two indexes and an offset, and walk two tables by hand.
- Explain what the two levels save, and what they cost.
- Lesson
- S3.21
- Time
- 26 min
- Runs on
- Your browser
- Check
- 6 questions
A small program uses three pages of memory. Its page table, built as on the last page, fills four million bytes. A hundred such programs spend four hundred million bytes on tables that are almost all empty.
Before You Start
You need S3.7: page, frame, offset, the page table and the page fault.
You also need hex and binary side by side: one hex digit is four bits.
| Question | |
|---|---|
| 1 | Why is one flat page table too large for addresses of 32 bits? |
| 2 | How is a 32-bit address cut for a table of two levels? |
| 3 | What does an entry of the root table hold? |
| 4 | How many reads of memory does one translation need? |
| 5 | What do the two levels save, and when? |
The Idea: A Table of Tables
The table of the last page had 16 entries, one for each page. Real addresses have 32 bits. With pages of 4096 bytes that makes 1,048,576 pages. A flat table needs an entry for every one of them.
An entry has 4 bytes. MiB means 1024 times 1024 bytes. So the flat table of one program fills 4 MiB, whether the program uses a million pages or three. Most programs use a few pages near the bottom of their addresses and a few near the top. The rest of the table says "not valid", a million times.
The cure is to cut the table into pieces of 1024 entries, and to keep a short list of the pieces. That list is the root table. It has 1024 entries too. Each one says where one piece is stored, or that the piece does not exist.
A piece that would hold only "not valid" is simply left out. One entry of the root table, marked not valid, stands for all 1024 pages of it.
A translation now takes two steps. The first 10 bits of the address pick an entry of the root table. It leads to a second table. The next 10 bits pick an entry there. It holds the frame of the page. Going through the tables like this is called a table walk.
Each table has 1024 entries of 4 bytes. That is 4096 bytes: exactly one frame. So a table is stored in a frame like any page, and an entry can point to it with a frame number.
New Words
| Word | Meaning |
|---|---|
| Flat page table | A page table in one piece, with an entry for every page. The table of the last page. |
| Two-level page table | A page table built as a root table and second tables. |
| Root table | The first table of a walk. Each valid entry holds the frame number of a second table. |
| Second table | A table of the last level. Each valid entry holds the frame number of a page. |
| Upper index and lower index | Our names for the two parts of the page number. The upper index picks an entry of the root table. The lower index picks an entry of the second table. |
| Table walk | Reading one entry at each level, to get from a virtual address to a frame. Walk for short. |
| KiB and MiB | Sizes of memory. A KiB is 1024 bytes, and a MiB is 1024 KiB. |
| Sv32 | The name that RISC-V gives its translation scheme for addresses of 32 bits, with two levels. |
One Picture: Two Indexes, Two Tables
- Processor
- Control / FSM
- Memory
- Bus
- Three parts
The address is cut into three parts: two indexes of 10 bits and an offset of 12 bits.
- The first index
The upper index picks one of 1024 entries of the root table. The entry holds the frame number of a second table.
- The second index
The lower index picks one of 1024 entries of that second table. This entry holds the frame number of the page.
- Where nothing is mapped
An entry of the root table covers 1024 pages. If the program uses none of them, the entry is not valid, and no second table is stored.
Read the diagram as text
- Virtual address: upper index, lower index, offset
- Root table: one frame, 1024 entries
- Second table: one frame, 1024 entries
- Frame of the page: 4096 bytes of the program
- No second table: the entry is not valid: nothing is stored
- Virtual address → Root table: the upper index picks an entry
- Root table → Second table: frame of the next table
- Second table → Frame of the page: lower index, then the frame of the page
- Root table → No second table: most entries
Two reads of memory for one translation. In return, tables exist only where the program has pages.
The Smallest Example, Worked by Hand
Translate the address 00403abc. The root table is in frame 10 (hex), so it starts at physical address 10000.
First cut the address. In binary it is 0000000001, then 0000000011, then 101010111100. So the upper index is 1, the lower index is 3, and the offset is abc. The lab prints them as index1 and index0.
| Step | What is done | Result |
|---|---|---|
| 1 | Find the entry of the root table: 10000 + 4 x 1 | address 10004 |
| 2 | Read it | 00004401 |
| 3 | Bit 0 is 1, so it is valid. Take bits 31 to 10 | frame 11: a second table, at 11000 |
| 4 | Find the entry of the second table: 11000 + 4 x 3 | address 1100c |
| 5 | Read it | 0000245b |
| 6 | Bit 0 is 1, so it is valid. Take bits 31 to 10 | frame 09: the page |
| 7 | Put the offset behind the frame number | physical address 09abc |
The index is multiplied by 4 because an entry has 4 bytes. All numbers are hex, except the indexes.
How do bits 31 to 10 of 00004401 give 11? Drop the last 10 bits. In hex, 4401 is 0100 0100 0000 0001. Without its last ten bits, 010001 is left: 11 in hex.
How does the walk know that the first entry points to a table, and the second to a page? Three more bits of the entry say so. On this page it is enough that the tables are built correctly. The next page reads those bits.
The Rules
Rule 1: A 32-bit address is cut into 10, 10 and 12 bits.
The upper index, the lower index and the offset. Together the two indexes are the page number.
Rule 2: An entry lies at the start of its table plus 4 times its index.
An entry has 4 bytes.
Rule 3: A valid entry of the root table holds the frame of a second table.
A valid entry of a second table holds the frame of the page.
Rule 4: An entry that is not valid ends the walk with a page fault.
At the root this rules out 1024 pages with one read.
Rule 5: A table is one frame.
So tables are placed and found like pages, by frame number.
Rule 6: Every level is one read of memory.
A translation with two levels costs two reads before the access itself.
Sources for These Rules
The textbook "Computer Organization and Design" by Patterson and Hennessy, section 5.7. See "Virtual Memory for Large Virtual Addresses": the size of a flat table, and multiple levels of page tables as one cure.
The textbook "Digital Design and Computer Architecture" by Harris and Harris, section 8.4.6: multilevel page tables, with a table of two levels.
The exact layout is that of the Sv32 scheme of RISC-V: two indexes of 10 bits, entries of 4 bytes, the frame number in bits 31 to 10. We state it from common knowledge. The RISC-V privileged specification is not in our library, so these facts are not checked against it.
The names upper index and lower index are ours. RISC-V documents number the two parts of the page number 1 and 0.
Now Run It
The lab at the end of this page is test code. Its memory has 32 frames. The operating system of the lab builds a root table in frame 10, and second tables in frames 11 and 12. Three pages are mapped.
Then it translates five addresses. Each walk prints its reads.
load 00403abc: index1=1 index0=3 offset=abc
level 1: entry 1 of the table in frame 10, at 10004 = 00004401 | valid, points to the table in frame 11
level 0: entry 3 of that table, at 1100c = 0000245b | valid, the page is in frame 09
physical 09abc | data da7a9abc
load 00404010: index1=1 index0=4 offset=010
level 1: entry 1 of the table in frame 10, at 10004 = 00004401 | valid, points to the table in frame 11
level 0: entry 4 of that table, at 11010 = 000018d7 | valid, the page is in frame 06
physical 06010 | data da7a6010
load 00000ffc: index1=0 index0=0 offset=ffc
level 1: entry 0 of the table in frame 10, at 10000 = 00004801 | valid, points to the table in frame 12
level 0: entry 0 of that table, at 12000 = 000014d7 | valid, the page is in frame 05
physical 05ffc | data da7a5ffc
load 00405000: index1=1 index0=5 offset=000
level 1: entry 1 of the table in frame 10, at 10004 = 00004401 | valid, points to the table in frame 11
level 0: entry 5 of that table, at 11014 = 00000000 | not valid
page fault
load 00800000: index1=2 index0=0 offset=000
level 1: entry 2 of the table in frame 10, at 10008 = 00000000 | not valid
page faultRules 1 to 3 at work: the first four rows are the hand example. The entry at 10004 leads to the table in frame 11. The entry at 1100c leads to frame 09.
Rule 2 at work: the second load is one page further on. It uses the same second table and the next entry, at 11010.
Rule 4 at work: the last two loads. For 00405000 the second table has no valid entry. For 00800000 the root table has none. That one read settles 1024 pages, which are 4 MiB of addresses.
Now the saving. This program has three tables of one frame each: 12 KiB. A flat table would be 4 MiB. The price is rule 6: two reads of memory before every access. A later page removes most of them again.
This lab leaves out who may read or write a page. It also leaves out the hardware that does the walk.
Why It Is Built This Way
| Design choice | The alternative | What the choice buys, and costs |
|---|---|---|
| Two levels | One flat table | Tables only where pages are in use. Two reads for a translation where one would do. |
| A table the size of a frame | Tables of any size | The operating system places tables like pages. The index widths are fixed by it. |
| Frame numbers in the entries | Full addresses in the entries | Twelve bits of each entry stay free for other uses. Tables must start at the start of a frame. |
| Not valid at the root | Second tables for every part | One bit covers 4 MiB of unused addresses. Nothing: it is free. |
The Trap: An Index Without Its Factor of Four
A team writes the walk, and adds the index to the start of the table as it is. They forget that an entry has 4 bytes. Here is that walk in the lab, for the address of the hand example:
load 00403abc: index1=1 index0=3 offset=abc
level 1: entry 1 of the table in frame 10, at 10001 = 00004801 | valid, points to the table in frame 12
level 0: entry 3 of that table, at 12003 = 000014d7 | valid, the page is in frame 05
physical 05abc | data da7a5abcNothing here looks like a failure. The walk reads at 10001, which lies inside entry 0 of the root table. It follows that entry to the table in frame 12. There it lands inside entry 0 again, which is valid.
The result is physical address 05abc. The right answer was 09abc. The program now reads a frame that belongs to another page, and no fault is raised.
Rule 2 is the check. Test a walk with indexes other than 0: with an index of 0, the wrong sum and the right sum are the same.
At Scale
This section looks ahead. Longer addresses need more levels, and the same idea carries on.
More levels for longer addresses
RISC-V names schemes with three and four levels Sv39 and Sv48. Each level takes 9 bits of the address there. This is not checked against the specification.
Large pages
An entry of the root table may hold a page itself, of 4 MiB. One entry then serves what would need 1024. Not checked.
The walk in hardware
A small state machine does these reads. It has a lesson of its own.
Skipping the walk
A small cache can remember recent translations. That is the subject of a later lesson.
Try It
Run the lab. Check the first four rows against the hand table.
Map the page at 00405000 to frame 07 with one more put. Which table gets the entry? Run the fourth load again.
Map the page at 00800000 to frame 03. How many puts do you need, and how many frames of tables exist then?
load 00403abc: index1=1 index0=3 offset=abc level 1: entry 1 of the table in frame 10, at 10004 = 00004401 | valid, points to the table in frame 11 level 0: entry 3 of that table, at 1100c = 0000245b | valid, the page is in frame 09 physical 09abc | data da7a9abc load 00404010: index1=1 index0=4 offset=010 level 1: entry 1 of the table in frame 10, at 10004 = 00004401 | valid, points to the table in frame 11 level 0: entry 4 of that table, at 11010 = 000018d7 | valid, the page is in frame 06 physical 06010 | data da7a6010 load 00000ffc: index1=0 index0=0 offset=ffc level 1: entry 0 of the table in frame 10, at 10000 = 00004801 | valid, points to the table in frame 12 level 0: entry 0 of that table, at 12000 = 000014d7 | valid, the page is in frame 05 physical 05ffc | data da7a5ffc load 00405000: index1=1 index0=5 offset=000 level 1: entry 1 of the table in frame 10, at 10004 = 00004401 | valid, points to the table in frame 11 level 0: entry 5 of that table, at 11014 = 00000000 | not valid page fault load 00800000: index1=2 index0=0 offset=000 level 1: entry 2 of the table in frame 10, at 10008 = 00000000 | not valid page faultCheck Yourself
Common Mistakes
Wrong Add the index to the start of the table as it is.
An entry has 4 bytes, so entry n starts 4 times n bytes in.
Fix Multiply the index by the size of an entry.
Wrong Cut a 32-bit address at hex digits.
The indexes have 10 bits, and a hex digit has 4.
Fix Write the address in binary, and cut after 10 and after 20 bits.
Wrong Think that two levels make translation faster.
They add a read. What they save is memory for tables.
Fix Count the reads of a walk as a cost, and remove them with a cache of translations.
Remember
- A flat table for 32-bit addresses has a million entries: 4 MiB for each program.
- Two levels: the upper index picks a root entry, the lower index an entry of a second table.
- An entry lies at the start of its table plus 4 times its index.
- Unused parts cost one root entry. A walk costs two reads.