Computer System with paging

  • A computer system that implements paging will:

    • Assign each running program is a number of memory frames that it can use to store its program pages

  • Important fact:

    • The number of memory frames assigned to a running program can be much smaller than the number of pages in the program

  • The computer system with paging will make sure that:

    • All pages that are needed to execute the current instruction of the program are present in memory

    This will require that:

    • Some pages stored in the memory be swapped out and replaced with new pages in the program !!

How a program will run in a computer system with paging

  • Fact:

    • Programs usually consists of many loops

    • A program runs for a long time in a loop.

  • How a program run in a computer system with paging:

    • While the program is executing instructions in the current loop:

      • The memory (frames) will contain all the pages of the current loop

        (The program can run without a page fault for the entire loop

    • When the program exits the current loop and possibly enters a new loop:

      • The pages in the memory (frames) will replaced with the pages of the new loop

Computer System with paging

  • Graphical depiction of a computer system with paging:

  • A running program is stored in its entirety in disk

  • A few memory frames is allocated (= reserved) for the running program.

Computer System with paging

  • At one point in time, some subset of program pages will be present in memory frames:

  • The memory will contain all the program pages necessary to execute the current loop

Computer System with paging

  • At a later point in time, another subset of program pages will be present in memory:

  • The memory will contain all the program pages necessary to execute the new loop

  • The paging system will detect page faults and fetch the missing pages from disk

Detecting a page hit

  • Page hit:

    • A page hit is detected by Present Bit = 1 for the page number requested

    Example:

Detecting a page fault

  • Page fault:

    • A page fault is detected by Present Bit = 0 for the page number

    Example:

What to do when there is a page fault

  • Consequence of a Page fault:

    • The execution of the (running) program cannot continue

  • What must be done to allow the (running) program to continue execution:

    • The program page (stored of disk) that contains the required program item must be placed in a frame in memory

      (Reason: the CPU can only fetch data from memory (and not from a disk)

  • The paging system defines a page fault handling mechanism to:

    • fetch the page that is needed (from disk) to complete the execution of the current instruction

  • The page fault handling mechanism consists of:

    1. A procedure (= function) to fetch the missing page from disk and place it in some frame
    2. Update the page table to adjust the dynamic address mapping function !!

Where is the missing program page found on the disk ??

  • Suppose the CPU requested a program address that results in a page fault....

    Example:


    Question:   how can the paging system find the required program page on disk ?

Where is the missing program page found on the disk ??

  • The location of a program page is found in the page location field in the page table entry

    Example:


    Next question:   what is the most efficient way to fetch a program page from disk ?

Background info on page fault handling

  • Page fault handling requires:

    • Fetching a program page from disk (= an IO device !!!)

  • IO devices are slooooooooow....

  • Note that a page fault is similar to:

    • Performing an IO operation on the disk !!!

  • Effect of a page fault is the same as performing an IO operation:

    • The running program cannot continue because it must wait for the (very slow) IO transfer operation

  • We also have learned how to handle the IO operation efficiently before:

    • Use the DMA to perform the IO operation

    • Use context switching to make the current process (with page fault) wait

Background info on page fault handling

  • Each process (= "running program") has its own page table

  • There can be multiple processes in a computer system

    However:

    • Only one of the processes is actively executing instructions

      (In a single core CPU)

  • The complete structure of the MMU is as follows:

    The current page table register points to the page table of the actively running process

Computer system with page fault handling

  • A computer system with page fault handling looks as follows:

    • The MMU uses the entries in the current page table to perform address mapping

    • The OS (Operating System) contains a page fault (interrupt) handler which gets invoked when a page fault is detected

Handling a page fault

  • Suppose the execution of the current instruction results in a page fault:
     

  • The page fault is detected by the present bit = 0 in the page table entry
     

Handling a page fault

  • When the MMU detect a page fault, it will emits an interrupt (INT) signal:
     

  • The CPU will detect by the INT signal and initialize the vector interrupt handling protocol (see: click here) to identify the interrupt handler

Handling a page fault

  • The vector interrupt handling mechanism will cause the CPU to perform a "unplanned" subroutine call to the page fault (interrupt) handler:

  • The page fault (interrupt) handler subroutine will be discussed next...
     

Pseudo code of the Page Fault (interrupt) handler

  • 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 entry.
    
         3. Remove the head of the Ready Queue (= active program)
            and insert it into the DMAQ queue
         
         4. Set DMAQ's PCB.pageFault = TRUE;  // The PCB must contain
                                              // a flag to indicate
    			                  // if request was due to 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 PCB at the head of the Ready Queue 
    
         6. mov pc, lr
    

    The PCB contains a pageFault variable to indicate that the program had a page fault

Summary: handling a page fault

  • Situation before the execution of the page fault handler:
     

  • The current program has a page fault and cannot continue
     

Summary: handling a page fault

  • Situation after the execution of the page fault handler:
     

  • A "new" current program is running (i.e.: CPU is not wasted)
    The DMA is fetching the page from disk for the program that had a page fault

The program that had a page fault will resume execution when the DMA is finished    (how ???: interrupt !!)

The Interrupt Handling Routine for the DMA device without support for paging fault handling

  • The DMA interrupt handler needs to be modified with page fault support

  • The DMA interrupt handler that was discussed previously is as follows: ( click here)

       The Interrupt Service Routine for the DMA device:
       				      
        (1) Acknowledge the Interrupt
       				      
        (2) Save context from CPU into the PCB
       	at the head of the Ready Queue
     
        // The main task consists of these 2 actions
        (3) Remove the PCB from the DMA device queue
       				      
        (4) Insert this PCB at the head of the Read Queue
      			      
        (5) Restore the context (register values) using
       	the PCB at the head of the Ready Queue
    


  • In a page fault situation, the DMA interrupt handler must also:

    • Update the page table entry in the MMU (adjust the address mapping) !!!

Updated Interrupt Handling Routine for the DMA device

  • The DMA interrupt handler with support for page fault handling:

       The Interrupt Service Routine for the DMA device:
       				      
        (1) Acknowledge the Interrupt
       				      
        (2) Save context from CPU into the PCB
       	at the head of the Ready Queue
     
        // The main task consists of these 2 actions
        (3) Remove the PCB from the DMA device queue
       				      
        (4) Insert this PCB at the head of the Read Queue
    
        (5) If ( PCB.pageFault == TRUE ) // program had a page fault
            {
                Update page table entry in MMU (adjust address mapping)
            }	
    
        (6a) Update current page table register of MMU to point to
             the page table of the program that at the
             head of the Ready Queue
    
        (6b) Restore the context (register values) using
       	 the PCB at the head of the Ready Queue
    

Elaboration on step 5 of the Interrupt Handling Routine for the DMA device

  • Step 5:

        (5) If ( PCB.pageFault == TRUE ) // program had a page fault
            {
                Update page table entry in MMU (adjust address mapping)
            }	
    

    If a page fault triggered the disk read operation from the disk:

Elaboration on step 5 of the Interrupt Handling Routine for the DMA device

  • Step 5:

        (5) If ( PCB.pageFault == TRUE ) // program had a page fault
            {
                Update page table entry in MMU (adjust address mapping)
            }	
    

    Suppose the program page was placed in frame 2:

Elaboration on step 5 of the Interrupt Handling Routine for the DMA device

  • Step 5:

        (5) If ( PCB.pageFault == TRUE ) // program had a page fault
            {
                Update page table entry in MMU (adjust address mapping)
            }	
    

    The page table entry must be updated with new address mapping information:

Example of the DMA interrupt handler for a page fault

  • Initial situation:   this is the situation when the DMA is fetching the missing program page...

  • Another process is running (so that CPU cycles are not wasted)
    The DMA is fetching the page from disk for the program that had a page fault

The program that had a page fault will resume execution when the DMA is finished    (let's see: how !!)

Example of the DMA interrupt handler for a page fault

  • Notice the current process is using the current page table to perform address mapping through the MMU:

  • When the DMA finishes, the waiting program will become the new current process
    Therefore:   we must update the current page table (modified DMA int handler !!)

Let's see what happens when the DMA finishes...

Example of the DMA interrupt handler for a page fault

  • When the DMA is finished, it will raise the interrupt signal:
     

  • The CPU will detect by the INT signal and initialize the vector interrupt handling protocol (see: click here) to identify the interrupt handler

The CPU will execute an updated DMA interrupt handler that we discussed previously

Example of the DMA interrupt handler for a page fault

  • The waiting program will become the new current process     and
    The missing page is now in memory (i.e.: no page fault)

  • The CPU will repeat the same instruction
    (Because the program counter (PC) has not changed)

Example of the DMA interrupt handler for a page fault

  • When the CPU repeats the same instruction, the result will now be different:
     

  • Because the missing page has been read from disk
    (And the page table has been updated with the frame number to perform the mapping)

Why is paging successful ?

  • Recall:

    • Computer programs exhibits a execution locality behavior due to loops:

      • Programs spend a lot of time in one loop before moving on to the next loop

    • This execution locality behavior was exploited in caching:

      • The cache memory is much smaller than the main memory and caching has a very high hit ratio


  • The execution locality behavior is also the reason why paging is successful:

    • Programs spend a lot of time in one loop before moving on to the next loop

      • If the memory contains all the pages of the current loop, the program will run as if the whole program is in memory !!