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 bits and does not change in the translation.
Example. With KB pages the offset has bits; a -bit virtual address has bits of page number.
Example: 32-bit virtual addresses, 30-bit physical addresses (1 GB), 4 KB pages.
- offset: bits;
- virtual page number: bits, so pages;
- frame number: bits, so 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: 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:
- 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;
- reads the page from disk (millions of cycles: meanwhile it runs another process);
- 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):
against 200 ns without a TLB.
Protection and sharing
- Each process has its own table: it cannot generate physical addresses of another process.
- Protection bits per page; violations raise an exception (segmentation fault).
- Shared pages: two tables can point to the same frame (dynamic libraries, see From a C Program to an ARM ExecutableThe translation chain compiler, assembler, linker and loader; object files, symbol table and relocation; static and dynamic linking; memory layout of a process; calling assembly functions from C and vice versa, with an example.From a C Program to an ARM Executable →).
- Modifying the tables is a privileged operation.
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 bits and unchanged by translation. Example (32-bit virtual, 30-bit physical, 4 KB): offset 12 bits, 20-bit page number ( pages), 18-bit frame number ( frames).
Page table
One per process, indexed by virtual page number; entries: valid, frame number, protection bits, reference bit, dirty. 0x00403A7C with entry 0x00403 frame 0x1F2 gives 0x1F2A7C. Size B 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: 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.