Page-Table Entry Bits and Page Faults
An entry of a page table holds more than a frame number. Seven small bits say who may do what with the page, and what has been done with it so far.
You Will Be Able To
- Decode the bits V, R, W, X, U, A and D of an entry.
- Decide whether an access is allowed, and name the bit that refuses it.
- Describe what happens at a page fault, and three ways it can end.
- Lesson
- S3.22
- Time
- 26 min
- Runs on
- Your browser
- Check
- 6 questions
A program has a loop that runs one step too far. It writes one word past the end of its data, into its own code. Without a guard, the program now runs an instruction that nobody wrote.
Before You Start
You need S3.21: an entry of 32 bits. Its valid bit is bit 0, and its frame number is bits 31 to 10.
You also need the page fault from S3.7, and fetch, load and store from S0.6.
| Question | |
|---|---|
| 1 | Which bits of an entry say what a page may be used for? |
| 2 | What does the U bit decide? |
| 3 | What do the A bit and the D bit record, and who needs them? |
| 4 | What does the processor do at a page fault? |
| 5 | How can a page fault end? |
The Idea: The Entry Is Also a Guard
So far an entry answered one question: where is the page? But every access goes through the entry. That makes it the right place for a second question: is this access allowed?
Three bits say what a page may be used for. R allows a load. W allows a store. X allows the fetch of an instruction. Code gets R and X, and no W. Data gets R and W, and no X. The loop from the top of the page is then stopped at its first wrong store.
One bit says whose page it is. U marks a page of a user program: an ordinary program, with fewer rights than the operating system. The pages of the operating system have U at 0, and a user program cannot touch them.
Two bits are notes. A, for accessed, says that the page has been used. D, for dirty, says that it has been written. The operating system reads them when it must take a frame away. A page that was never written need not be saved first.
When a check fails, the access does not happen. The processor stops the instruction and hands control to the operating system. That is the page fault of the earlier pages, now with many possible causes.
A fault is a question to the operating system, and it has three kinds of answer. Fix the entry and run the instruction again. Fetch the page from somewhere, and then run it again. Or decide that the program is wrong, and end it.
New Words
| Word | Meaning |
|---|---|
| Permission bits | Bits 1, 2 and 3 of an entry, named R, W and X. The page may be read, written, and fetched from as instructions. |
| User bit | Bit 4 of an entry, named U. The page belongs to a user program. |
| Accessed bit | Bit 6 of an entry, named A. The page has been used since the bit was last cleared. |
| Dirty bit of a page | Bit 7 of an entry, named D. The page has been written since the bit was last cleared. |
| User program | An ordinary program. It runs with fewer rights than the operating system. |
| Fault handler | The code of the operating system that runs when a page fault happens. |
| Faulting address | The virtual address whose access was refused. The processor keeps it in a register for the handler. |
One Picture: Four Checks, One Refusal
- Processor
- Control / FSM
- Memory
- Bus
- Is there a frame?
V is the valid bit. With V at 0 there is nothing to check. The access faults.
- Is this kind of access allowed?
A load needs R. A store needs W. A fetch of an instruction needs X. A store into a page without W faults here.
- Is the asker allowed?
U marks a page of a user program. A user program is refused on a page without U.
- Has the use been noted?
A says that the page was used. D says that it was written. If the bit is still 0, this lab faults, so that the operating system can note it.
- All checks passed
Only now does the access reach the frame.
Read the diagram as text
- An access: fetch, load or store; user or system
- V: valid?: is there a frame at all
- R, W, X: this kind?: R for a load, W for a store, X for a fetch
- U: for this asker?: user pages for user programs
- A, D: noted?: used before, written before
- Allowed: the access goes to the frame
- Page fault: the operating system takes over
- An access → V: valid?: entry found
- V: valid? → R, W, X: this kind?: V is 1
- R, W, X: this kind? → U: for this asker?: bit is 1
- U: for this asker? → A, D: noted?: U fits
- A, D: noted? → Allowed: A, D set
- V: valid? → Page fault: V is 0
- R, W, X: this kind? → Page fault: bit is 0
- U: for this asker? → Page fault: wrong asker
- A, D: noted? → Page fault: not noted yet
Seven bits, four questions. A page fault always names the access, and the entry shows which bit said no.
The Smallest Example, Worked by Hand
Take the entry 0000245b. Its frame number is bits 31 to 10: frame 09. Its last two hex digits, 5b, hold the bits of this page. In binary, 5b is 0101 1011.
| Bit | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|---|---|---|---|---|---|---|---|---|
| Name | D | A | (not used here) | U | X | W | R | V |
| Value | 0 | 1 | 0 | 1 | 1 | 0 | 1 | 1 |
Valid, readable, executable, a user page, used before, never written. W is 0.
This is a page of code. Now three accesses by a user program:
| Access | Needs | The entry has | Result |
|---|---|---|---|
| fetch an instruction | V, X, U, A | all four | allowed |
| load a word | V, R, U, A | all four | allowed |
| store a word | V, W, U, A, D | W is 0 | page fault |
The store is refused by one bit. The frame is never touched.
The handler now looks at the faulting address and at the entry. W is 0 on purpose: nobody may write code. So this fault ends the program.
The Rules
Rule 1: A load needs R, a store needs W, and a fetch needs X.
One bit for each kind of access.
Rule 2: A user program needs U at 1.
In this scheme the operating system is refused on such a page too, unless it sets a switch of its own.
Rule 3: V at 0 refuses everything.
The other bits of the entry then mean nothing.
Rule 4: A is set at the first use of a page, and D at its first store.
This lab faults when the bit is still 0, and the handler sets it. Hardware may also set it by itself.
Rule 5: A refused access does not happen.
No byte of the frame is read or written.
Rule 6: The handler gets the faulting address and the kind of access.
It fixes the entry and runs the instruction again, or it ends the program.
Sources for These Rules
The textbook "Computer Organization and Design" by Patterson and Hennessy, section 5.7. See "Page Faults" and "Implementing Protection with Virtual Memory". They cover the valid bit, the reference and dirty bits, and the work of the operating system on a fault.
The textbook "Digital Design and Computer Architecture" by Harris and Harris, section 8.4.4: memory protection with bits in the page table.
The names and places of the seven bits follow the Sv32 scheme of RISC-V as commonly described. So does the refusal of the operating system on a user page. The RISC-V privileged specification is not in our library, so these facts are not checked against it.
Rule 4 has two forms in practice: a fault that lets software set the bit, or hardware that sets it. This lab uses the first. That is a choice.
The order of the checks in the lab is a choice too. Each access of the lab fails at most one check.
Now Run It
The lab at the end of this page is test code. It uses the tables of the last page. A user program gets three pages: code, data, and data that was never used. A fourth page belongs to the operating system.
Each row is one access. It shows the entry, its bits as letters, and the verdict. A dash stands for a bit at 0.
user fetch 00403000 | entry 0000245b -A-UX-RV | allowed, physical 09000
user store 00403abc | entry 0000245b -A-UX-RV | page fault: W is 0, and this is a store
user load 00404010 | entry 000018d7 DA-U-WRV | allowed, physical 06010
user store 00404010 | entry 000018d7 DA-U-WRV | allowed, physical 06010
user load 00406000 | entry 000020c7 DA---WRV | page fault: U is 0, and a user program asks
os load 00406000 | entry 000020c7 DA---WRV | allowed, physical 08000
user load 00407000 | no valid entry | page fault: V is 0
user load 00405000 | entry 00001c17 ---U-WRV | page fault: A is 0, the first use of this page
user load 00405000 | entry 00001c57 -A-U-WRV | allowed, physical 07000
user store 00405000 | entry 00001c57 -A-U-WRV | page fault: D is 0, the first store to this page
user store 00405000 | entry 00001cd7 DA-U-WRV | allowed, physical 07000Rule 1 at work: the first two rows. The fetch from the code page is allowed. The store into it is refused, because W is 0. That is the loop from the top of the page, stopped.
Rule 2 at work: rows 5 and 6. The user program is refused on the page of the operating system. The operating system itself may read it.
Rule 3 at work: row 7. The page at 00407000 has no entry.
Rules 4 and 6 at work: the last four rows. The first load of a fresh page faults on A. The handler sets A, and the same load is allowed. The first store faults on D. The handler sets D, and the store is allowed.
Look at the entry in those four rows: 1c17, then 1c57, then 1cd7. The page did not move. Only its notes changed.
This lab leaves out the handler itself: one line of test code stands for it. It also leaves out the time that each check takes.
Why It Is Built This Way
| Design choice | The alternative | What the choice buys, and costs |
|---|---|---|
| Permission bits in the entry | A separate table of rights | The check costs no extra read: the entry is already there. Rights exist only for whole pages. |
| Separate R, W and X | One bit for "allowed" | Code cannot be overwritten, and data cannot be run. Three bits in every entry. |
| A fault, with software deciding | Hardware that decides | The operating system can use any policy. A fault costs far more time than a check. |
| A and D as notes | No record of use | Clean pages need not be saved, and idle pages can be found. Someone must set the bits. |
The Trap: A Page That Is Writable and Executable
A team gives the data page all three bits, R, W and X, "to be safe". A user program has a bug that lets an attacker choose the bytes it stores. Here is what the entry allows:
user store 00404010 | entry 000018df DA-UXWRV | allowed, physical 06010
user fetch 00404010 | entry 000018df DA-UXWRV | allowed, physical 06010Neither row is a fault, and that is the failure. The first row stores bytes that the attacker chose. The second row runs them as instructions. Every check passed.
With W and no X on data, the second row would have been a page fault. The checks can refuse only what the entry forbids. Rule 1 is the test: for every page, ask which of the three bits it truly needs.
At Scale
This section looks ahead. A real operating system builds most of its memory tricks on these bits and on the fault.
Pages on demand
A system can leave V at 0 until a page is first used. The fault then supplies a frame. That is common practice.
Copy when written
Two programs can share a frame with W at 0. The first store faults, and the handler gives the writer a copy. Common practice.
The stack guard
A page with V at 0 below the stack turns a stack that grows too far into a clean fault.
The cost of a walk
Every access needs its entry. Reading two tables each time is too slow. The next lesson removes most of those reads.
Try It
Run the lab. For each fault, find the bit in the letters that caused it.
Make the code page readable only by removing X in its put. Predict the first row, and run it.
Add access(0, "load", 32'h0040_4010): the operating system reads a user page. Predict the verdict.
user fetch 00403000 | entry 0000245b -A-UX-RV | allowed, physical 09000 user store 00403abc | entry 0000245b -A-UX-RV | page fault: W is 0, and this is a store user load 00404010 | entry 000018d7 DA-U-WRV | allowed, physical 06010 user store 00404010 | entry 000018d7 DA-U-WRV | allowed, physical 06010 user load 00406000 | entry 000020c7 DA---WRV | page fault: U is 0, and a user program asks os load 00406000 | entry 000020c7 DA---WRV | allowed, physical 08000 user load 00407000 | no valid entry | page fault: V is 0 user load 00405000 | entry 00001c17 ---U-WRV | page fault: A is 0, the first use of this page user load 00405000 | entry 00001c57 -A-U-WRV | allowed, physical 07000 user store 00405000 | entry 00001c57 -A-U-WRV | page fault: D is 0, the first store to this page user store 00405000 | entry 00001cd7 DA-U-WRV | allowed, physical 07000Check Yourself
Common Mistakes
Wrong Give every page R, W and X.
Then no access is ever refused, and bytes that were stored can be run.
Fix Give each page only the bits it needs: code R and X, data R and W.
Wrong Read the frame number without checking V.
With V at 0 the other bits are junk.
Fix Check V first. An entry that is not valid gives a fault and nothing else.
Wrong Treat every page fault as an error.
Many faults are planned: a first use, a first store, a page that is supplied late.
Fix Let the handler decide from the address, the access and the entry.
Remember
- R, W and X allow a load, a store and a fetch. U marks a page of a user program.
- A and D record that a page was used, and that it was written.
- A refused access does not happen. The processor raises a page fault.
- The handler fixes the entry and repeats the instruction, or ends the program.