Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 4: Virtual Process Memory


4.10 Handling of Page Faults


The association between virtual and physical memory is not established until the data of an area are
actually needed. If a process accesses a part of virtual address space not yet associated with a page in
memory, the processor automatically raises apage faultthat must be handled by the kernel. This is one
of the most important and complex aspects of memory management simply because a myriad of details
must be taken into account. For example, the kernel must ascertain the following:

❑ Was the page fault caused by access to a valid address from the user address space, or did the
application try to access the protected area of the kernel?
❑ Does a mapping exist for the desired address?
❑ Which mechanism must be used to obtain the data for the area?

Figure 4-17 shows an initial overview of the potential paths the kernel may follow when handling page
faults.

Kernel or User-
space address?

Kernel mode?

Synchronize with
reference page table

Segmentation Fault

Yes

No

Sufficient privileges?

Handle request
Demand Paging/Allocation,
Swapping or COW

Mapping exists?

Segmentation Fault

Segmentation Fault

Yes No

Yes No

Kernel User

Figure 4-17: Potential options for handling page faults.

As demonstrated below, the individual actions are much more complicated because the kernel must not
only guard against malicious access from userspace but must also take note of many minor details; on
top of this, it must not allow the page handling operations to degrade system performance unnecessarily.

The implementation of page fault handling varies from processor to processor. Because the CPUs employ
different memory management concepts, the details of page fault generation also differ. Consequently,
the handler routines in the kernel are located in the architecture-specific source code segments.

We confine ourselves below to a detailed description of the approach adopted on the IA-32 architecture.
Implementation on most other CPUs is at least similar.

An assembler routine inarch/x86/kernel/entry_32.Sserves as the entry point for page faults but
immediately invokes the C routinedo_page_faultfromarch/x86/mm/fault_32.c. (A routine of the
Free download pdf