Salta al contenuto
Note per Studenti Virtual Memory Report

Virtual Memory

In questa pagina 6
In questa pagina 5

Each process sees its own virtual address space, as large as the addresses allow (32 bits means 4 GB), even if physical memory is smaller or shared with other processes. The hardware (MMU, Memory Management Unit) translates every virtual address into a physical address; the parts not present in memory are on disk.

It is the level of the memory hierarchyCharacteristics of memories (location, capacity, transfer unit, access method, performance, volatility); the cost-capacity-speed trade-off and the levels of the hierarchy; temporal and spatial locality; hit, miss and average access time with worked calculations.Memory Hierarchy and Locality → between main memory and disk, with the same logic as the Cache MemoryBlocks, lines and tags; address decomposition; direct, fully associative and set-associative mapping with field calculation; replacement policies (LRU, FIFO, random); write policies (write-through, write-back with dirty bit, write-allocate); block size; multilevel and split caches; reducing misses; typical address-field questions.Cache Memory → but different terms: page (block), page fault (miss).

Pages and frames

Definition (pages, frames and virtual address)

  • The virtual space is divided into fixed-size pages (typically 4 KB); physical memory into frames of the same size.
  • Virtual address = virtual page number followed by offset; the offset has log⁡2(page size)\log_2(\text{page size}) bits and does not change in the translation.

Example. With 44 KB pages the offset has log⁡24096=12\log_24096=12 bits; a 3232-bit virtual address has 32−12=2032-12=20 bits of page number.

Example: 32-bit virtual addresses, 30-bit physical addresses (1 GB), 4 KB pages.

  • offset: log⁡24096=12\log_2 4096 = 12 bits;
  • virtual page number: 32−12=2032 - 12 = 20 bits, so 2202^{20} pages;
  • frame number: 30−12=1830 - 12 = 18 bits, so 2182^{18} frames.

Page table

Definition (page table). For each process a page table in memory, indexed by the virtual page number. Each entry contains: valid bit (page in memory or not), frame number, protection bits (read, write, execute, user/supervisor), reference bit and dirty bit.

Example. If the entry of page 0x00403 indicates frame 0x1F2, the virtual address 0x00403A7C becomes the physical address 0x1F2A7C.

Translation of 0x00403A7C with 4 KB pages: virtual page 0x00403, offset 0xA7C. If entry 0x00403 says frame 0x1F2, the physical address is 0x1F2A7C.

Table size in the example: 2202^{20} entries of 4 bytes = 4 MB per process. For this reason multi-level tables are used (only the used parts are allocated).

Page fault

If the entry is not valid, the MMU raises an exception and the operating system:

  1. chooses a free frame, or a page to replace (approximations of LRU with the reference bit); if the replaced page is dirty it writes it to disk;
  2. reads the page from disk (millions of cycles: meanwhile it runs another process);
  3. updates the table and restarts the instruction that caused the fault.

Since a fault costs millions of cycles: large pages, fully associative placement (any frame), always write-back, software management. The exception mechanism is described in Interrupts and ExceptionsClasses of interrupts; interrupt-driven I/O; hardware and software handling sequence with context saving on the stack; identifying the source (multiple lines, polling, daisy chain, vector); priorities and nested interrupts; exceptions and modes in ARM.Interrupts and Exceptions →.

TLB

Definition (TLB). Every access would require an extra access to the table in memory. The TLB (Translation Lookaside Buffer) is a small associative cache of recent translations (tens to hundreds of entries). TLB hit: translation in one cycle; TLB miss: the table is read and the TLB updated.

Example. Memory access 100 ns, TLB 1 ns, TLB hit rate 98% (no page fault):

T=0,98⋅(1+100)+0,02⋅(1+100+100)=98,98+4,02=103 nsT = 0{,}98 \cdot (1 + 100) + 0{,}02 \cdot (1 + 100 + 100) = 98{,}98 + 4{,}02 = 103 \text{ ns}

against 200 ns without a TLB.

Protection and sharing

Typical mistakes

  • Translating the offset too: only the page number changes.
  • Confusing the size of the page table (it depends on the virtual space) with the physical memory.

Versione ripasso

Each process has a virtual space (32 bits means 4 GB); the MMU translates virtual to physical addresses; absent parts are on disk. Same logic as the Cache MemoryBlocks, lines and tags; address decomposition; direct, fully associative and set-associative mapping with field calculation; replacement policies (LRU, FIFO, random); write policies (write-through, write-back with dirty bit, write-allocate); block size; multilevel and split caches; reducing misses; typical address-field questions.Cache Memory →: page is the block, page fault is the miss.

Pages

Pages and frames of equal size (typically 4 KB). Virtual address = page number + offset; offset =log⁡2(page size)= \log_2(\text{page size}) bits and unchanged by translation. Example (32-bit virtual, 30-bit physical, 4 KB): offset 12 bits, 20-bit page number (2202^{20} pages), 18-bit frame number (2182^{18} frames).

Page table

One per process, indexed by virtual page number; entries: valid, frame number, protection bits, reference bit, dirty. 0x00403A7C with entry 0x00403 →\to frame 0x1F2 gives 0x1F2A7C. Size 220⋅42^{20} \cdot 4 B =4= 4 MB per process, hence multi-level tables.

Page fault

Invalid entry: exception; the OS chooses a frame (LRU approximation, writes back a dirty page), reads the page from disk (millions of cycles, runs another process meanwhile), updates the table, restarts the instruction. Hence large pages, fully associative placement, write-back, software handling (Interrupts and ExceptionsClasses of interrupts; interrupt-driven I/O; hardware and software handling sequence with context saving on the stack; identifying the source (multiple lines, polling, daisy chain, vector); priorities and nested interrupts; exceptions and modes in ARM.Interrupts and Exceptions →).

TLB

Associative cache of recent translations (tens to hundreds of entries). Hit: 1 cycle. Example: 0,98⋅101+0,02⋅201=1030{,}98 \cdot 101 + 0{,}02 \cdot 201 = 103 ns against 200 ns without TLB.

Protection

Own table per process; protection bits per page, violation means segmentation fault; pages shared by two tables pointing to the same frame; changing tables is privileged.

Typical mistakes: translating the offset (only the page number changes); table size depends on the virtual space, not on physical memory.

Teoria collegata