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.

Five questions this page answers
Question
1Why do programs not use the real addresses of memory?
2What are a page and a frame?
3Which part of an address does a translation change, and which part does it keep?
4What is in an entry of a page table?
5What 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

New words on this page
WordMeaning
Virtual addressAn address as a program uses it. It is translated before it reaches memory.
Physical addressA real address: the one that memory itself sees.
Page of memoryOne of the equal pieces that the virtual addresses of a program are cut into. Here a page has 4096 bytes. Page for short.
FrameOne of the equal pieces that physical memory is cut into. It has the size of a page. Also called a physical page.
Page numberThe front part of a virtual address. It says which page.
Frame numberThe front part of a physical address. It says which frame.
Offset in a pageThe back part of an address. It says where a byte lies inside its page. Here it is the last three hex digits.
Page tableThe table of one program. It has an entry for each page.
Entry of a page tableA valid bit and a frame number, for one page.
TranslateTo swap the page number of an address for the frame number in the table.
Address translationThe result of that swap, and the act of doing it. Translation for short.
Page faultWhat happens when a program uses a page that has no valid entry. The access is stopped.
Operating systemThe trusted software that starts the programs, writes their page tables and handles their faults.

One Picture: The Same Address Twice

Two Programs, Two Tables, One MemoryStep through the picture. Two programs use the same address, and each reaches a frame of its own.
  • Processor
  • Control / FSM
  • Memory
Two Programs, Two Tables, One MemoryStep through the picture. Two programs use the same address, and each reaches a frame of its own. component Program 0 uses virtual address403c component Program 1 uses virtual address403c too component Table of program0 page 4 is in frame 9 component Table of program1 page 4 is in frame 6 component Frame 9 physical 9000 to9fff component Frame 6 physical 6000 to6fff page 4page 4903c603c
  1. component Program 0uses virtual address 403c
    • page 4 → Table of program 0
  2. component Table of program 0page 4 is in frame 9
    • 903c → Frame 9
  3. component Frame 9physical 9000 to 9fff
  4. component Program 1uses virtual address 403c too
    • page 4 → Table of program 1
  5. component Table of program 1page 4 is in frame 6
    • 603c → Frame 6
  6. component Frame 6physical 6000 to 6fff
  1. A program uses virtual addresses

    Program 0 reads address 403c. That number is not sent to memory as it is.

  2. 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.

  3. The offset stays

    The last three digits, 03c, say where in the page. They are kept. Memory sees 903c.

  4. 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.

One address, translated by hand
StepValue
The virtual address403c
Its page number4
Its offset in the page03c
Entry 4 of the tablevalid, frame 9
The physical address903c

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

  1. Rule 1: A program uses virtual addresses only.

    Every one of them is translated before it reaches memory.

  2. Rule 2: An address is a page number and an offset.

    The size of a page fixes how many bits the offset has.

  3. Rule 3: Translation swaps the page number for a frame number.

    The offset is never changed.

  4. Rule 4: Each program has its own page table.

    The operating system writes it. The program cannot.

  5. 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.

  6. 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.

Printed by the lab on Icarus. Each row is one access: the split of the address, the frame from the table, and the physical address.
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 fault

Rules 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 choiceThe alternativeWhat the choice buys, and costs
Virtual addressesReal addresses for every programPrograms cannot reach each other. Every access needs a translation.
Pages of one fixed sizePieces of any sizeAny free frame fits any page. A program that needs one byte still gets a whole page.
Translate the page number onlyTranslate every byte addressOne entry serves 4096 bytes. The offset passes through with no logic at all.
A table written by the operating systemA table that the program writesThe 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.

The same lab, with one frame in the tables of both programs.
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 bb

The 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 page16 pages of 4096 bytes
A 32-bit systemabout a million pages for each program

This section looks ahead. Real systems use the same split, with far more pages.

  1. A table too large to keep

    A million entries for each program is too much. The next lesson builds the table in two levels.

  2. 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.

  3. Sharing on purpose

    Two programs that run the same code can share its frames. Both tables name them, and neither may write.

  4. 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?

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: 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 fault

Check Yourself

Predict the outputProgram 1 has page 2 in frame 7 and page 3 in frame 1. It stores 55 at 2a10, loads 3a10 and then loads 2a10. What does the last row say?
// Two programs, each with a page table of its own. Addresses have 16 bits. A page has 4096 bytes.
module tb;
  logic [7:0] ram [0:65535];          // physical memory: 16 frames of 4096 bytes
  // One page table for each program: 16 entries. An entry is a valid bit and a frame number.
  logic       valid [0:1][0:15];
  logic [3:0] frame [0:1][0:15];
  int who = 0;                        // the program that runs now

  task automatic map(input int prog, input logic [3:0] page, input logic [3:0] to_frame);   // the operating system writes an entry
    valid[prog][page] = 1;  frame[prog][page] = to_frame;
  endtask

  // Translate one virtual address with the table of the running program. ok is 0 for a page fault.
  task automatic translate(input string what, input logic [15:0] virt, output bit ok, output logic [15:0] phys);
    logic [3:0]  page;
    logic [11:0] offset;
    page = virt[15:12];  offset = virt[11:0];                 // the split: one hex digit, three hex digits
    if (!valid[who][page]) begin
      $display("program %0d %s %04h: page %h offset %03h | no valid entry: page fault", who, what, virt, page, offset);
      ok = 0;
    end else begin
      phys = {frame[who][page], offset};                      // the frame number is swapped in. The offset stays
      $write("program %0d %s %04h: page %h offset %03h | frame %h | physical %04h", who, what, virt, page, offset, frame[who][page], phys);
      ok = 1;
    end
  endtask

  task automatic store(input logic [15:0] virt, input logic [7:0] value);
    logic [15:0] phys;
    bit ok;
    translate("store", virt, ok, phys);
    if (ok) begin ram[phys] = value;  $display(" | data %02h", value); end
  endtask
  task automatic load(input logic [15:0] virt);
    logic [15:0] phys;
    bit ok;
    translate("load ", virt, ok, phys);
    if (ok) $display(" | data %02h", ram[phys]);
  endtask

  initial begin
    for (int p = 0; p < 2; p++) for (int i = 0; i < 16; i++) valid[p][i] = 0;
    for (int a = 0; a < 65536; a++) ram[a] = 8'h00;
    map(1, 4'h2, 4'h7);  map(1, 4'h3, 4'h1);
    who = 1;  store(16'h2a10, 8'h55);
    load(16'h3a10);
    load(16'h2a10);
  end
endmodule
ConceptWhy are programs given virtual addresses?
ConceptA page has 4096 bytes. Which part of the address 8f24 is the offset?
ConceptWhat does a translation do to the offset?
ConceptWhat does an entry of the page table on this page hold?
ConceptA program reads a page whose entry is not valid. What happens?

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.

Practice

Report a problem with this page