A Quick Guide to Page Faults in Modern SoC Architectures

This series is a quick reference booklet on the core areas of knowledge in modern SoC architecture.

Chapter 5Page Faults(Page Faults)

Section 1Types of Page Faults

Section 2Interrupt Service Routines

Section 3Interrupt and Exception Handlers

Section 4What is the Role of Page Fault Handlers?

Chapter 5Page Faults(Page Faults)

When the MMU detects an attempt to access an invalid or unauthorized page table entry, the processor automatically generates a page fault. This exception triggers the execution of a function called Interrupt Service Routine (ISR), which is specifically designed to handle page fault events.

The goal of the page fault service routine is to locate the missing page in auxiliary storage (if possible), bring it into main memory, and continue executing the active process as if nothing had happened.

Section 1Types of Page Faults

There are two types of page faults: Minor (also known as soft page faults) and Major (also known as hard page faults).

A minor page fault occurs when the data being accessed is already in main memory but has not yet been mapped to the process’s virtual memory space. This typically happens in cases involving shared memory, such as shared libraries. For example, when a program loads a shared library, it is mapped into that program’s virtual memory. However, when other programs access the same library, the data is already in main memory but has not been mapped to their virtual address space. In this case, the operating system simply maps the existing memory to the requesting process. Minor page faults are usually handled quickly and have little impact on system performance.

A major page fault occurs when the requested data is not in main memory and must be loaded from auxiliary storage to continue processing. This is typically due to demand paging, where program pages are loaded into main memory only when accessed, or swapping, where pages are temporarily moved from main memory to a swap file on auxiliary storage. Major page faults take longer to resolve, and if they occur frequently in a short period, they can significantly degrade system performance..

Section 2Interrupt Service Routines

Every operating system implements a set of interrupt service routines, which are simple functions that handle important events. These functions are an integral part of the operating system and are registered early in the operating system’s boot process.

When the operating system starts, the code for each ISR is dynamically placed in memory. Since the memory addresses of ISRs are not constant, a data structure called the Interrupt Vector Table (IVT) stores these memory addresses for easy lookup of ISRs. The IVT itself is also stored in main memory. In modern x64 architectures, this table is referred to as the Interrupt Descriptor Table (IDT).

The CPU has a special register called the Interrupt Descriptor Table Register (IDTR), which contains the starting address and size of the Interrupt Descriptor Table in main memory.

Note:IDTR stores virtual addresses, not physical addresses. As with every memory access, the addresses in IDTR must be translated from virtual to physical addresses by the MMU.

Each interrupt or exception is identified by a unique number called the Interrupt Vector Number (IVN). When an interrupt or exception/fault occurs, the processor uses this number as an index into the interrupt descriptor table to find the correct handler to call.

A Quick Guide to Page Faults in Modern SoC Architectures

Figure 17: Processor Calls Page Fault ISR (0x0E)

Section 3Interrupt and Exception Handlers

When an interrupt/exception signal is issued, the processor must perform several steps. This applies not only to page faults but also to general interrupts and exceptions:

1. Save Current State: The processor stores key information about the currently running process in main memory so that it can return after the interrupt/exception is handled.

2. Call Interrupt Service Routine: It finds the ISR responsible for handling the specific interrupt/exception and begins execution.

3. Restore Previous State: When the routine completes, the processor pops the stored information from main memory, restores the registers of the running process, and continues normal execution (restarting the last instruction if necessary).

Section 4What is the Role of Page Fault Handlers?

The page fault handler may perform the following actions:

1.Locate Faulting Page Table: The handler knows which process was running when the fault occurred. It looks up the process’s page table to perform several checks.

2.Check for Access Violations or Unauthorized Access: If the address is not mapped to any valid area (the program is trying to access memory not allocated to it), or if the access violates permissions, the handler kills the faulting process with a Segmentation Fault (known as “Access Violation” on Windows).

3.Locate Physical Frame:

a.Minor Page Fault: If the page is already loaded into main memory (the frame is allocated), this may mean the program is trying to access a shared page. In this case, no changes to the frame are needed.

b.Major Page Fault: If the page is not in memory (swapped-out or demand-paged), the operating system allocates a free physical frame and loads the corresponding data from auxiliary storage.

4.Update Page Table Entry: Update the running process’s page table using the existing shared page or newly allocated frame and define access permissions.

5.Additional Operations: Perform modifications to shared pages (Copy-on-Write).

Leave a Comment