Virtual Memory: Pages and Frames
Two programs can both use address 403c and never meet. Each has a table that swaps the front of the address for a place of its own in memory.
You Will Be Able To
- Explain why programs are given virtual addresses.
- Split an address into a page number and an offset, and say which part a translation changes.
- Translate an address by hand with a page table, and say when the access faults.
- Lesson
- S3.7
- Time
- 24 min
- Runs on
- Your browser
- Check
- 6 questions
A program has a bug. It writes to address 403c, where it should have written to 503c. On a small chip that byte may belong to another program. That program now fails, hours later, for no reason anyone can find.
Before You Start
You need S0.2: addresses in hex, and one address for each byte.
You also need a valid bit, as in S0.7. This page starts a new topic: you do not need the cache lessons for it.
| Question | |
|---|---|
| 1 | Why do programs not use the real addresses of memory? |
| 2 | What are a page and a frame? |
| 3 | Which part of an address does a translation change, and which part does it keep? |
| 4 | What is in an entry of a page table? |
| 5 | What happens when a page has no valid entry? |
The Idea: Every Program Gets Its Own Addresses
On the chip of this course, a program uses real addresses. Address 8000_0040 is the same byte of the SRAM for everyone. One wrong write can damage anything.
A larger system runs many programs at once, and they must not be able to hurt each other. So a program is not given real addresses at all. It uses virtual addresses. On the way to memory, hardware turns each one into a real one, a physical address.
A table does the turning. Each program has its own table, and trusted software writes it. A program can reach only what its table lists. The memory of another program is not in the table, so no address leads there.
A table with an entry for every byte would be larger than the memory itself. So memory is cut into equal pieces. A piece of virtual addresses is a page. A piece of physical memory is a frame. A page and a frame have the same size.
The table has one entry for each page. It says which frame holds that page. An address inside the page keeps its place inside the frame.
This gives three things at once. Programs are kept apart. Each program can be built for the same addresses, wherever its frames really lie. And the frames of a program can be scattered, because the table hides that.
New Words
| Word | Meaning |
|---|---|
| Virtual address | An address as a program uses it. It is translated before it reaches memory. |
| Physical address | A real address: the one that memory itself sees. |
| Page of memory | One of the equal pieces that the virtual addresses of a program are cut into. Here a page has 4096 bytes. Page for short. |
| Frame | One of the equal pieces that physical memory is cut into. It has the size of a page. Also called a physical page. |
| Page number | The front part of a virtual address. It says which page. |
| Frame number | The front part of a physical address. It says which frame. |
| Offset in a page | The back part of an address. It says where a byte lies inside its page. Here it is the last three hex digits. |
| Page table | The table of one program. It has an entry for each page. |
| Entry of a page table | A valid bit and a frame number, for one page. |
| Translate | To swap the page number of an address for the frame number in the table. |
| Address translation | The result of that swap, and the act of doing it. Translation for short. |
| Page fault | What happens when a program uses a page that has no valid entry. The access is stopped. |
| Operating system | The trusted software that starts the programs, writes their page tables and handles their faults. |
One Picture: The Same Address Twice
- Processor
- Control / FSM
- Memory
- A program uses virtual addresses
Program 0 reads address 403c. That number is not sent to memory as it is.
- The table swaps the page number
The first hex digit, 4, is the page. The table of program 0 says that page 4 lies in frame 9.
- The offset stays
The last three digits, 03c, say where in the page. They are kept. Memory sees 903c.
- Another program, another table
Program 1 uses the same number, 403c. Its own table sends page 4 to frame 6. Memory sees 603c.
Read the diagram as text
- Program 0: uses virtual address 403c
- Program 1: uses virtual address 403c too
- Table of program 0: page 4 is in frame 9
- Table of program 1: page 4 is in frame 6
- Frame 9: physical 9000 to 9fff
- Frame 6: physical 6000 to 6fff
- Program 0 → Table of program 0: page 4
- Program 1 → Table of program 1: page 4
- Table of program 0 → Frame 9: 903c
- Table of program 1 → Frame 6: 603c
The same virtual address, two physical addresses. Neither program can even name the frame of the other.
The Smallest Example, Worked by Hand
Take addresses of 16 bits, which are four hex digits. A page has 4096 bytes. In hex that is 1000, so the last three digits are the offset. The first digit is the page number. There are 16 pages and 16 frames.
Program 0 has a table with two valid entries: page 4 is in frame 9, and page 5 is in frame 2. Translate the address 403c.
| Step | Value |
|---|---|
| The virtual address | 403c |
| Its page number | 4 |
| Its offset in the page | 03c |
| Entry 4 of the table | valid, frame 9 |
| The physical address | 903c |
The page number is swapped for the frame number. The offset is copied.
Now the address 5ffc: page 5, offset ffc. Entry 5 says frame 2, so memory sees 2ffc.
And the address 7000: page 7. Entry 7 is not valid. There is no frame to go to. The access is stopped, and the operating system is told. That is a page fault.
The Rules
Rule 1: A program uses virtual addresses only.
Every one of them is translated before it reaches memory.
Rule 2: An address is a page number and an offset.
The size of a page fixes how many bits the offset has.
Rule 3: Translation swaps the page number for a frame number.
The offset is never changed.
Rule 4: Each program has its own page table.
The operating system writes it. The program cannot.
Rule 5: An entry that is not valid means a page fault.
The access does not happen, and the operating system decides what comes next.
Rule 6: A frame belongs to one page table at a time, unless sharing is intended.
Two tables that name one frame share every byte of it.
Sources for These Rules
The textbook "Computer Organization and Design" by Patterson and Hennessy, section 5.7, "Virtual Memory": the virtual page number and the page offset, the page table, and its valid bit.
The textbook "Digital Design and Computer Architecture" by Harris and Harris, sections 8.4.1 and 8.4.2: address translation and the page table.
The addresses of 16 bits and the tables of 16 entries are our own teaching example. Real pages of 4096 bytes are common practice.
The chip of this course has no translation. Its processor uses physical addresses.
Now Run It
The lab at the end of this page is test code. It has a physical memory of 16 frames and two programs. Each program has a page table of 16 entries.
First the operating system writes three entries. Then both programs store a byte at virtual address 403c, and both read it back.
program 0 store 403c: page 4 offset 03c | frame 9 | physical 903c | data aa
program 1 store 403c: page 4 offset 03c | frame 6 | physical 603c | data bb
program 0 load 403c: page 4 offset 03c | frame 9 | physical 903c | data aa
program 1 load 403c: page 4 offset 03c | frame 6 | physical 603c | data bb
program 0 load 5ffc: page 5 offset ffc | frame 2 | physical 2ffc | data 00
program 1 load 5ffc: page 5 offset ffc | no valid entry: page faultRules 2 and 3 at work: the first row. The address 403c splits into page 4 and offset 03c. Frame 9 is swapped in, and memory sees 903c.
Rule 4 at work: the second row. Program 1 uses the same virtual address. Its table says frame 6, so its byte goes to 603c. The third and fourth rows show that each program reads back its own byte.
Rule 5 at work: the last two rows. Page 5 is valid for program 0 and leads to frame 2. For program 1 it has no entry, and the load ends in a page fault.
This lab leaves out who may read or write a page, and where the table itself is stored. It also leaves out the time that a translation takes. The next pages add those.
Why It Is Built This Way
| Design choice | The alternative | What the choice buys, and costs |
|---|---|---|
| Virtual addresses | Real addresses for every program | Programs cannot reach each other. Every access needs a translation. |
| Pages of one fixed size | Pieces of any size | Any free frame fits any page. A program that needs one byte still gets a whole page. |
| Translate the page number only | Translate every byte address | One entry serves 4096 bytes. The offset passes through with no logic at all. |
| A table written by the operating system | A table that the program writes | The program cannot give itself more memory. Every change of the table costs a call. |
The Trap: One Frame in Two Tables
The operating system keeps a list of free frames. Here it has a bug: it hands out frame 9 twice. Both tables now send page 4 to frame 9.
program 0 store 403c: page 4 offset 03c | frame 9 | physical 903c | data aa
program 1 store 403c: page 4 offset 03c | frame 9 | physical 903c | data bb
program 0 load 403c: page 4 offset 03c | frame 9 | physical 903c | data bbThe third row is the failure. Program 0 stored aa and reads back bb. It did nothing wrong. Program 1 did nothing wrong either: it wrote to its own page 4.
The translation worked exactly as built. The wall between programs is only as good as the tables. Rule 6 is the check: every frame in one table, unless two programs are meant to share it.
At Scale
This section looks ahead. Real systems use the same split, with far more pages.
A table too large to keep
A million entries for each program is too much. The next lesson builds the table in two levels.
More pages than frames
A page may also be kept on a disk until it is used. The fault brings it in. That is common practice in large systems.
Sharing on purpose
Two programs that run the same code can share its frames. Both tables name them, and neither may write.
Devices too
A large chip also translates the addresses that devices use: I/O Memory Management and Virtualisation.
Try It
Run the lab. Check the first row against the hand table.
Add map(1, 4'h5, 4'h3) to the table of program 1. Predict the last row, and run it.
Make program 0 store a byte at 4fff and at 5000. Which frames do the two bytes land in?
program 0 store 403c: page 4 offset 03c | frame 9 | physical 903c | data aa program 1 store 403c: page 4 offset 03c | frame 6 | physical 603c | data bb program 0 load 403c: page 4 offset 03c | frame 9 | physical 903c | data aa program 1 load 403c: page 4 offset 03c | frame 6 | physical 603c | data bb program 0 load 5ffc: page 5 offset ffc | frame 2 | physical 2ffc | data 00 program 1 load 5ffc: page 5 offset ffc | no valid entry: page faultCheck Yourself
Common Mistakes
Wrong Translate the whole address.
Only the page number goes through the table.
Fix Split the address first. Swap the page number, and copy the offset.
Wrong Think that two programs with the same address share a byte.
Each program has its own table, and the tables name different frames.
Fix Ask which table the address goes through.
Wrong Treat a page fault as a crash of the chip.
It is a controlled stop that hands the case to the operating system.
Fix The operating system may fix the table and let the program go on, or end the program.
Remember
- A program uses virtual addresses. Memory sees physical addresses.
- An address is a page number and an offset. Translation swaps the page number for a frame number.
- Each program has its own page table, written by the operating system.
- A page with no valid entry gives a page fault.