Loading lessons...
Virtual Memory: Paging, Page Tables, TLB, Segmentation & Memory Protection
Virtual Memory: Paging, Page Tables, TLB, Segmentation & Memory Protection
Virtual memory lets a program use a larger memory than the computer physically has. The program is written for a big address space; only the parts in use are kept in the small memory space (main memory), and the rest stays on disk.
Hardware translates every address the program uses into a real memory address while it runs. This lesson covers paging, page tables, the TLB, segmentation and protection.
1. Address Space and Memory Space
- A virtual (logical) address is an address used by the program. The set of all of them is the address space, of size N.
- A physical address is a location in main memory. The set of all of them is the memory space, of size M.
- Usually N is much larger than M. A memory mapping table translates each virtual address into a physical address.
A computer has an address space of 1024K words and a main memory of 32K words. How many bits are in a virtual address and in a physical address?
- Address space: 1024K = 220, so 20 bits.
- Memory space: 32K = 215, so 15 bits.
2. Paging
The address space is split into equal pages, and main memory into blocks (page frames) of the same size. A page is moved into any free block, and a page table records which block holds each page.
A virtual address therefore has two parts: a page number (the high bits) and a line (word) number inside the page (the low bits). Translation replaces the page number with the block number; the line number is copied unchanged.
Mano's example: an 8K address space and a 4K memory with 1K pages and blocks.
- Pages = 8K / 1K = 8, so the page number has 3 bits; the line number has 10 bits; a virtual address has 13 bits.
- Blocks = 4K / 1K = 4, so the block number has 2 bits; a physical address has 12 bits.
- Pages 1, 2, 5 and 6 are in memory, in blocks 3, 0, 1 and 2.
Paging: pages mapped to blocks by the page table
The page table has one entry per page. Each entry holds a block number and a presence bit: 1 if the page is in main memory, 0 if it is only on disk.
Using the page table above, translate the virtual addresses 1124 and 6161.
- 1124 = 1 × 1024 + 100: page 1, line 100. Page 1 is in block 3, so the physical address is 3 × 1024 + 100 = 3172.
- 6161 = 6 × 1024 + 17: page 6, line 17. Page 6 is in block 2: 2 × 1024 + 17 = 2065.
- 3072 = 3 × 1024 + 0: page 3 has presence bit 0, so this is a page fault.
Associative Memory Page Table
A page table with one entry per page wastes space: in Mano's 8-page example only 4 entries are ever in use, and with 1024K words and 1K pages the table would have 1024 entries but only 32 blocks. An associative page table instead has one entry per block: each word stores a page number and its block number.
The page number of the virtual address is the argument, and an associative search finds the block. The table has only M / (page size) entries.
Page Fault
If the presence bit is 0, the page is not in memory, and the hardware raises a page fault (an internal interrupt). The operating system then:
- suspends the program and finds the page on disk,
- chooses a free block, or a page to remove from memory with a page replacement algorithm (lesson 46); if that page was modified it is written back to disk,
- reads the page into the block and updates the page table,
- restarts the instruction that faulted.
Disk access is very slow, so page faults must be rare. The program's CPU is given to another program while the page is being loaded.
3. Translation Lookaside Buffer (TLB)
The page table is in main memory, so a plain paged access needs two memory accesses: one for the page table entry and one for the data. A TLB is a small, fast associative memory that holds the most recently used page-to-block translations. It is searched first:
- TLB hit: the block number comes straight from the TLB; one memory access.
- TLB miss: the page table in memory is read, and the translation is loaded into the TLB.
- Page fault: the page is not in memory at all.
Address translation with a TLB and a page table
The memory management unit below has a 2-entry TLB and Mano's page table. Page 2 is used twice, so the second time it is a TLB hit; page 6 then replaces it in the TLB:
// Virtual address translation (Mano's sizes): 8K address space, 4K memory,
// 1K pages, so a 13-bit virtual address = 3-bit page + 10-bit line, and a
// 12-bit physical address = 2-bit block + 10-bit line. A 2-entry TLB
// (associative) is searched first, then the page table.
module mmu (
input clk,
input req,
input [12:0] va,
output reg [11:0] pa,
output reg tlb_hit,
output reg fault // page not in main memory
);
reg [1:0] block [0:7]; // page table: block number
reg present [0:7]; // presence bit
reg [2:0] tlb_page [0:1];
reg [1:0] tlb_block [0:1];
reg tlb_valid [0:1];
reg next; // TLB entry to replace (FIFO)
wire [2:0] page = va[12:10];
wire [9:0] line = va[9:0];
integer i;
initial begin
for (i = 0; i < 8; i = i + 1)
present[i] = 0;
// pages 1, 2, 5 and 6 are in blocks 3, 0, 1 and 2 (Mano)
present[1] = 1; block[1] = 2'd3;
present[2] = 1; block[2] = 2'd0;
present[5] = 1; block[5] = 2'd1;
present[6] = 1; block[6] = 2'd2;
tlb_valid[0] = 0;
tlb_valid[1] = 0;
next = 0;
end
always @(posedge clk)
if (req) begin
tlb_hit = 0;
fault = 0;
for (i = 0; i < 2; i = i + 1)
if (tlb_valid[i] && tlb_page[i] == page) begin
tlb_hit = 1;
pa = {tlb_block[i], line};
end
if (!tlb_hit) begin
if (present[page]) begin
pa = {block[page], line};
tlb_page[next] = page; // load the TLB
tlb_block[next] = block[page];
tlb_valid[next] = 1;
next = ~next;
end else
fault = 1; // OS must load the page
end
end
endmodule
module tb;
reg clk = 0, req = 0;
reg [12:0] va;
wire [11:0] pa;
wire tlb_hit, fault;
mmu u (clk, req, va, pa, tlb_hit, fault);
always #5 clk = ~clk;
reg [12:0] trace [0:5];
integer k;
initial begin
trace[0] = 13'd2048 + 13'd5; // page 2, line 5
trace[1] = 13'd1024 + 13'd100; // page 1, line 100
trace[2] = 13'd2048 + 13'd900; // page 2 again
trace[3] = 13'd3072; // page 3: not in memory
trace[4] = 13'd6144 + 13'd17; // page 6, line 17
trace[5] = 13'd2048 + 13'd1; // page 2: evicted from the TLB
$display("virtual page line -> result");
for (k = 0; k < 6; k = k + 1) begin
va = trace[k];
req = 1;
@(posedge clk);
#1;
if (fault)
$display("%5d %0d %3d -> page fault", va, va[12:10], va[9:0]);
else if (tlb_hit)
$display("%5d %0d %3d -> block %0d, physical %0d (TLB hit)",
va, va[12:10], va[9:0], pa[11:10], pa);
else
$display("%5d %0d %3d -> block %0d, physical %0d (page table)",
va, va[12:10], va[9:0], pa[11:10], pa);
end
$finish;
end
endmoduleThe physical addresses match Example 2, and the last access to page 2 has to go back to the page table:
virtual page line -> result
2053 2 5 -> block 0, physical 5 (page table)
1124 1 100 -> block 3, physical 3172 (page table)
2948 2 900 -> block 0, physical 900 (TLB hit)
3072 3 0 -> page fault
6161 6 17 -> block 2, physical 2065 (page table)
2049 2 1 -> block 0, physical 1 (page table)A TLB search takes 20 ns and a memory access 100 ns. Find the effective access time for TLB hit ratios of 90% and 98%.
- TLB hit: 20 + 100 = 120 ns. TLB miss: 20 + 100 (page table) + 100 (data) = 220 ns.
- 90%: 0.9 × 120 + 0.1 × 220 = 108 + 22 = 130 ns.
- 98%: 0.98 × 120 + 0.02 × 220 = 117.6 + 4.4 = 122 ns.
A 32-bit virtual address, 4 KB pages and 4-byte page table entries. How many entries does the page table have, and how big is it?
- Offset = log2 4096 = 12 bits, so the page number has 32 − 12 = 20 bits.
- Entries = 220 = 1M.
- Size = 220 × 4 B = 4 MB per process.
A memory access takes 100 ns, and servicing a page fault takes 10 ms. If one access in 10000 faults, what is the effective access time?
- p = 0.0001; 10 ms = 10,000,000 ns.
- EAT = (1 - p) × 100 + p × 10{,}000{,}000 = 99.99 + 1000 ≈ 1100 ns.
4. Segmentation
Pages have a fixed size that has nothing to do with the program's structure. Segmentation divides the address space into segments: logical units of variable length such as a program, a subroutine, a stack or an array.
A logical address is (segment number, offset), and a segment table gives each segment's base address and length. An offset larger than the length is an addressing error.
Segmented-Page Mapping
Segments can themselves be split into pages, combining the two ideas. Mano's logical address has 20 bits: a 4-bit segment, an 8-bit page and an 8-bit word.
- The segment number indexes the segment table, which gives the start of that segment's page table.
- That value plus the page number indexes the page table, which gives the block number.
- The block number followed by the word gives the physical address.
Segmented-page mapping
The logical address is 6027E (hex). Segment table entry 6 holds 35; page table entry 37 holds 019. Find the physical address.
- Split 6027E: segment = 6, page = 02, word = 7E.
- Segment table entry 6 = 35.
- Page table index = 35 + 02 = 37; entry 37 = block 019.
- Physical address = block 019 followed by word 7E = 0197E.
5. Memory Protection
With many programs in memory, each must be kept out of the others' areas. The segment table entry (the segment descriptor) carries a base address, a length and protection bits:
| Access right | Meaning |
|---|---|
| Full read and write | the program may read and change the segment (its own data) |
| Read only | may be read but not changed (constants, shared tables) |
| Execute only | may be run but not read as data (programs) |
| System only | only the operating system may use it |
An access beyond the length, or against the protection bits, causes an internal interrupt.
| Paging | Segmentation | |
|---|---|---|
| Unit size | fixed (page) | variable (segment) |
| Visible to the programmer | no | yes: matches program structure |
| Wasted space | internal fragmentation (last page partly used) | external fragmentation (gaps between segments) |
| Address | page + line | segment + offset (checked against the length) |
| Protection and sharing | per page | per logical unit: easier |
Key points to remember
- Virtual address = page number + line; physical address = block number + line.
- Page table entry: block number and presence bit. Presence 0 means a page fault.
- Associative page table has one entry per block, not per page.
- TLB: associative cache of translations; EAT = h(t + m) + (1 − h)(t + 2m).
- Segmented-page: segment table gives page table start; + page number gives block; block & word = physical.
📝 IPU End-Term Exam Corner (5-10 Marks)
- Q1. What is virtual memory? Explain address space and memory space with an example. (5 Marks)
- Q2. Explain address mapping using pages, with a memory page table, for an 8K address space and 4K memory with 1K pages. (10 Marks)
- Write: the diagram, presence bits, and a translation such as 1124 → 3172.
- Q3. What is a TLB? With a 20 ns TLB, 100 ns memory and 90% hit ratio, find the effective access time. (5 Marks)
- Q4. Explain segmented-page mapping with an example. (10 Marks)
- Q5. Compare paging and segmentation. How does segmentation support memory protection? (5 Marks)