Virtual Memory Addressing

Virtual memory addressing introduces a layer of abstraction between program code and physical memory.
It allows program code to be compiled as though each process will enjoy exclusive access to the entire memory address space.
However, in practice the virtual memory address space of each process is dynamically mapped
to arbitrary physical memory pages during execution.
All memory references are then dynamically translated from virtual memory addresses
to the correct arbitrary physical memory addresses just before each instruction is executed.

The operating system maintains data structures, called page tables, to support virtual-to-physical memory address translation.
The most recently used page table entries are cached in each CPU to optimize address translation.
This cache is commonly called a translation lookaside buffer or TLB.
To further optimize address translation, TLB lookups are performed in hardware.
A TLB miss must be resolved by reference to the page tables in main memory.
This operation is also performed by hardware in some cases.
If hardware address translation fails, the CPU switches into a special execution context (virtual memory context)
to ensure that a physical memory page is allocated for the virtual page, and the page is then refreshed from disk if necessary.
The page table entry is also copied into the TLB.
Such hardware address translation failures are called page faults.
If a page has to be read from disk, it is called a hard or major page fault; otherwise it is a soft or minor page fault.
After a minor page fault has been resolved, the CPU switches back into user context and restarts the current instruction.
However, while I/O is being performed to resolve a major page fault, the CPU will in general be used to service other processes.

Virtual memory addressing enables programs to run when not all of their program code or data is currently in physical memory.
This means that relatively inactive virtual pages can be temporarily removed from physical memory if necessary.
If those pages are later referenced, then a page fault will occur, and the pages will be mapped and read into physical memory again.
This is called paging.
It is effective because programs typically use only a small proportion of their virtual memory pages actively at any one a time.

Virtual memory addressing also enables programs to run from almost any location in physical memory.
This means that it is possible to have many programs and their data in memory at the same time, and to switch between them very quickly.
CPU time is not wasted while a process performs disk I/O, or waits for user input, or waits to resolve a page fault.