Programs usually consists of many loops and the program runs a long time in one loop.
While the program is inside one loop, it only need instructions that comprises that loop and do not need other instructions.
Hence, only a few program pages need to be present in the memory while the program is executing in some loop.
When the program leaves that loop and executes inside another loop, the computer will fetch other pages from disk and replace those pages in memory from the previous loop
In order to stop a program so that it can be restarted later from the point where it is stopped, you need to save the context information (see: click here)
Afterwards, another (ready) program is run (while the DMA performs the IO transfer on behave of the stopped program).
That page table will translate program address into memory addresses and these memory address will be value in the memory frames that are occupied by pages of the current program.
That's the magic that makes paging work !
If a page fault occurs, the page must be read from disk by the DMA.
While the DMA is busy transfering the page for the current running program, the current running program must be stop (save context !) and moved to the DMA queue - just like in the case of a IO operation, in fact, it is doing an IO operation...
Give the above statement a bit of thought:
If you don't recognize this problem, see this syllabus webpage: click here
The MMU uses the Interrupt mechanism to make an unplanned page fetch operation !!!
(Note that while the CPU is executing an instruction, it very hard to abort (need lots of extra circuitry). But it is very simple to abort while it has not done fetching).
I will - again - only give you the psuedo code for the page fault handler. You need to take the graduate course in "Operating System" to get all the details.
The psuedo code for the page fault handler is as follows: (very similar to the handler for the disk IO; but need to update the page tabel in the MMU !!!)
(The page fault interrupt is generated by the MMU)
0. Acknowledge Interrupt (CPU will receive vector from the MMU and call the Page fault interrupt handler): ============================================================ 1. Save the context from the CPU into the head of the Ready Queue // This is needed because the current program is no longer // ready to run 2. Perform a disk read operation using the location on disk information in the page table. 3. Remove the head of the Ready Queue and insert it into the DMAQ queue 4. DMAQ's PCB.pageFault = TRUE; // PCB must contain // a variable to indicate // if request was a pagefault 5a. Update current page table register of MMU to point to: the page table of the program that at the head of the Ready Queue 5b. Restore the context using the head of the Ready Queue |
Complete example:
|
Notice that the current page table pointer has changed !!!
Therefore:
This program has its own page table and the current page table register is pointing to the page table of the new process !!!
We have seen a similar story when we discuss the IO communication.
The solution is of course: use interrupt.
However: we must distinguish the case of a page fault from an "ordinary" disk read (file access) operation
(Because in a page fault the OS must update some page table entries, and in an "ordinary" disk read (file access), the page table does not change.)
As explained in the Interrupt handling syllabus pages (see: click here), the CPU will suspend the current running program and make an "unforseen" subroutine call to the DMA interrupt handler.
The DMA interrupt handler will execute and detects (from the newly introduced variable in the PCB) that the program that was waiting on the DMA had a page fault.
We need a new (= updated) DMA handler that can distiguish a normal Disk IO from a disk IO caused by a page fault.
Here is the updated DMA interrupt handler:
Updated DMA interrupt handler: =========================== (modified to differentiate between: A. normal disk read B. disk read due to a page fault ------------------------------------------------------------ 0. Acknowledge Interrupt (CPU will receive vector from DMA) and call the DMA interrupt handler ============================================================ 1. Save context from CPU into the PCB at the head of the Ready Queue 2. Remove the PCB from the DMA Queue 3. Insert this PCB at the head of the Ready Queue (This process is the NEW current running process !) Make the page table of this process the current page table by updating the current page table register in MMU ****** (added for paging) 4. If ( PCB.pageFault == TRUE (i.e., program had a page fault) ) { Update page table entries in MMU Specifically, you must: 1. Set the valid bit of the fetched page to VALID 2. Enter the page number for the fetched program page into the page entry 3. If you replaced an existing page, you must also invalid the corresponding page entry (Usually, you replace one of YOUR own pages and does not "steal" a frame from another process) } 5a. Update current page table register of MMU to point to: the page table of the program that at the head of the Ready Queue 5b. Restore the context using the head of the Ready Queue |
That's why I said very early on that the memory mapping used in paging is dynamic.... it changes from time to time.
How the mapping change depends on
|