Chapter3:MemoryManagement
the kernel must reserve the last 128 MiB of its address space for other purposes which I explain
shortly. Adding these 128 MiB to the 896 MiB of direct RAM mapping results in a total virtual kernel
address space of 1,024 MiB=1 GiB. The kernel uses the two frequently employed abbreviations
‘‘normal‘‘ and ‘‘highmem‘‘to distinguish between pages that can be mapped directly and those
than cannot.
The kernel port must provide two macros for each architecture to translate between physical and virtual
addresses in the identity-mapped part of virtual kernel memory (ultimately this is a platform-dependent
task).^14
❑ __pa(vaddr)returns the physical address associated with the virtual addressvaddr.
❑ __va(paddr)yields the virtual address corresponding to the physical addresspaddr.
Both functions operate withvoidpointers and withunsigned longs because both data types are equally
valid for the representation of memory addresses.
Caution: The functions arenotvalid to deal with arbitrary addresses from the virtual address space, but
onlywork for the identity-mapped part! This is why they can usually be implemented with simple linear
transformations and do not require a detour over the page tables.
IA-32 maps the page frames into the virtual address space starting fromPAGE_OFFSET, and correspond-
ingly the following simple transformation is sufficient:
include/asm-x86/page_32.h
#define __pa(x) ((unsigned long)(x)-PAGE_OFFSET)
#define __va(x) ((void *)((unsigned long)(x)+PAGE_OFFSET))
For what purpose does the kernel use the last 128 MiB of its address space? As Figure 3-15 shows, it is
put to three uses:
- Virtually contiguous memory areas that arenotcontiguous in physical memory can be
reserved in thevmallocarea. While this mechanism is commonly used with user processes,
the kernel itself tries to avoid non-contiguous physical addresses as best it can. It usually
succeeds because most of the large memory blocks are allocated for the kernel at boot time
when RAM is not yet fragmented. However, on systems that have been running for longer
periods, situations can arise in which the kernel requires physical memory but the space
available is not contiguous. A prime example of such a situation is when modules are loaded
dynamically. - Persistent mappingsare used to map non-persistent pages from the highmem area into the
kernel. Section 3.5.8 takes a close look at this topic. - Fixmapsare virtual address space entries associated with a fixed but freely selectable page
in physical address space. In contrast to directly mapped pages that are associated with
RAM memory by means of a fixed formula, the association between a virtual fixmap address
and the position in RAM memory can be freely defined and is then always observed by the
kernel.
(^14) The kernel places only two conditions on the functions that must remain as invariants;x 1 <x 2 ⇒va(x 1 )<va(x 2 )must be
valid (for any physical addressesxi), andva(pa(x))=xmust be valid for any addressesxwithin the direct mapping.