Make page table entries that are invalid, but the disk image of each
page is the corresponding portion of the file.
Can effectively perform I/O by memory reference.
Cleaning Policy
Instead of running the replacement algorithm when a fault occurs,
most OS's keep a pool of available pages.
A paging daemon runs periodically and checks if there are
enough pages in the pool. If not, it runs the replacement alg. and
to add more.
When a page enters the available pool, it is written back if modified,
but its contents remain in memory. If that page is needed again
before it is actually replaced, it just be reclaimed from the pool
without any I/O cost.
User Interface. Primarily, VM operation is transparent to the programmer.
But there may be some operations:
Ask for a shared memory region. Allows fast communication.
Ask to memory-map a file.
Configure a distributed shared memory.
Pages on different system can map to the same data.
Fault handling requires a network operation.
OS Involvement: What the OS has to take care of to make VM work.
On process creation, set up paging.
Allocate space for page table. Initial
size usually based on size of the executable program.
Swap area on disk must be set up so data can be accessed on a fault. More on this later.
Information about the page table and swap area are recorded in the
process table entry.
When a process changes state to running.
Reset the MMU to clear out information from the previous
virtual space. The TLB will be flushed.
Make the new page table current by placing a pointer in some
particular register, or possibly copying the whole table. On a Pentium, the page table address goes in a
register is called cr3
Possibly bring in some pages to prevent later faults.
When a fault occurs.
Compute which page is needed and find it on disk.
Find a frame, evicting a current one if needed.
Move the needed page into the frame.
Restart the instruction.
When a process exits, clean up.
Free the page table space, and mark its real pages available. Modified pages backed by a file may need to be written to disk.
But only for the pages no other process is using.
Page fault detail
A trap saves the PC, and usually some other information, and
transfers to the OS.
An assembly-language trap handler saves the general registers,
then calls a handler in the OS, usually written in C or a
higher-level language.
Determine the virtual address which caused the fault
Convenient hardware has saved this in a register.
Inconvenient hardware requires the OS to fetch the instruction
pointed to by the saved PC, and figure out what it was doing.
If the address is invalid, or the fault represents a protection
violation, the program is generally terminated.
Find a free frame. Either one is not in use, or from
a page pool, or by running the replacement algorithm.
If the chosen frame is modified, schedule a write and suspend the
faulting process. The frame is marked busy so it isn't
accidentally reused while waiting.
When the page is clean (after write-back or immediately),
find the needed page on disk and schedule a read to place it into
memory. The process remains suspended until this finishes.
When the disk interrupts to indicate read completion, the page table
is updated and marked for normal use (remove busy mark).
Depending on the architecture, the PC value saved at the fault may
need to be adjust to point to the start of the faulting
instruction. On some architectures, partial results may need to
be reversed.
Eventually, the faulting process is scheduled to run again.
The assembly-level fault handler resumes. This reloads the state
information (including the PC), returns to user space, and the
program retries the instruction which faulted.
Instruction Backup
On some architectures leave a mess after a page fault.
The hardware may interpret the instruction in steps, and be part
way through when the fault occurs.
An instruction may have multiple arguments in memory, any one of
which could fault.
Some architectures auto-increment registers.
The OS will need to figure out which address faulted, which PC
value is the start of the instruction, and reverse any auto-increments.
Hardware may give more or less help.
Newer architectures are simpler to deal with than this.
Locked Pages.
If an DMA operation is moving information to or from a
some page, it must not be paged out.
The OS marks the page as locked so the replacement algorithm
will ignore it.
Backing Store
Each page must have a backing location on disk.
A swap area
A partition or file holding the on-disk copies.
Disk locations computed by adding the virtual page number to
the start of the disk area.
Information may need to be copied to the swap area first.
Dynamic location.
Place pages wherever there's room.
Requires a directory in memory to map from virtual page number
to disk location.
Memory-mapped files, including running executables and shared
libraries, are treated as small swap areas.
Separation of Policy and Mechanism
Move replacement policy and backing store management to user space.
Faults reach the kernel, which requests pages from paging server.
User can control paging policy.
Different processes can use different approaches as desired.