Sub topic 4
Paging and Address Translation
Prerequisites
You should know binary and hexadecimal numbers, powers of two, and the difference between a process and main memory. You should also be comfortable reading short C or Python programs.
The Paging Model
Paging divides a process's virtual address space into fixed-size blocks called pages. Physical memory is divided into blocks of the same size called frames. A page can be stored in any available frame.
The operating system maintains a page table for each process. Each page-table entry maps one virtual page number to a physical frame number. The entry also commonly contains control bits, such as:
- A valid bit showing whether the page can currently be used.
- A protection bit showing whether the page is readable, writable, or executable.
- A dirty bit showing whether the page has been changed.
- A reference bit used by page-replacement algorithms.
Paging avoids the need to place an entire process in one contiguous region of physical memory.
Splitting a Virtual Address
For a page size of \(2^k\) bytes, the lowest \(k\) bits of a virtual address are the offset. The remaining high-order bits form the page number.
If the virtual address has \(m\) bits:
- Number of virtual pages = \(2^{m-k}\).
- Page offset = the lowest \(k\) bits.
- Page number = the remaining \(m-k\) bits.
For example, a 32-bit virtual address with 4 KiB pages uses \(2^{12}\) bytes per page. Therefore, 12 bits identify the byte within a page, and 20 bits identify the virtual page.
#include <stdint.h>
#include <stdio.h>
int main(void) {
uint32_t va = 0x12345678;
uint32_t page = va >> 12;
uint32_t offset = va & 0xFFF;
printf("page=0x%x offset=0x%x\n",
(unsigned)page, (unsigned)offset);
return 0;
}The program prints:
page=0x12345 offset=0x678The offset does not change during translation. Only the page number is replaced by a frame number.
Worked Translation
Assume:
- Virtual address size: 16 bits.
- Physical address size: 16 bits.
- Page size: 1 KiB \(=2^{10}\) bytes.
- Page-table entry for virtual page 5: frame 12.
- Virtual address:
0x1634.
A 1 KiB page has a 10-bit offset. Split the address as follows:
- Page number:
0x1634 >> 10 = 5. - Offset:
0x1634 & 0x3FF = 0x234.
The page table maps page 5 to frame 12. The physical address is therefore:
\[ (12 \times 1024) + 0x234 = 0x3234 \]
The page number and frame number are block identifiers. The offset identifies the exact byte inside that block.
Page-Table Size
If a process has \(2^{m-k}\) virtual pages and each page-table entry occupies \(e\) bytes, then:
\[ \text{page-table size} = 2^{m-k} \times e \]
For a 32-bit address, 4 KiB pages, and 4-byte entries:
\[ 2^{32-12} \times 4 = 2^{20} \times 4 = 4\text{ MiB} \]
This large table is one reason operating systems use multilevel page tables, which are covered later.
| Quantity | Formula for a 32-bit address and 4 KiB pages | Value |
|---|---|---|
| Offset bits | \(\log_2(4096)\) | 12 |
| Virtual page number bits | \(32-12\) | 20 |
| Virtual pages | \(2^{20}\) | 1,048,576 |
| Table size with 4-byte entries | \(2^{20}\times4\) | 4 MiB |
Common Errors
- Forgetting that page numbers and frame numbers start at zero.
- Changing the offset during translation.
- Using decimal page size without converting it to a power of two.
- Counting only frame-number bits when calculating a complete page-table-entry size.
- Confusing the number of pages with the number of bytes in a page.
Exercise
A 20-bit virtual address uses pages of 256 bytes. Page 13 maps to frame 41. What is the physical address of virtual address 0x0D9A?
Show answer
A 256-byte page has an 8-bit offset. The page number is 0x0D9A >> 8 = 0x0D, or 13. The offset is 0x9A. The physical address is (41 × 256) + 0x9A = 0x299A.