Specifications

This is the Title of the Book, eMatter Edition
Copyright © 2005 O’Reilly & Associates, Inc. All rights reserved.
Memory Management in Linux
|
415
Different kernel functions require different types of addresses. It would be nice if
there were different C types defined, so that the required address types were explicit,
but we have no such luck. In this chapter, we try to be clear on which types of
addresses are used where.
Physical Addresses and Pages
Physical memory is divided into discrete units called pages. Much of the system’s
internal handling of memory is done on a per-page basis. Page size varies from one
architecture to the next, although most systems currently use 4096-byte pages. The
constant
PAGE_SIZE (defined in <asm/page.h>) gives the page size on any given
architecture.
If you look at a memory address—virtual or physical—it is divisible into a page num-
ber and an offset within the page. If 4096-byte pages are being used, for example, the
12 least-significant bits are the offset, and the remaining, higher bits indicate the
page number. If you discard the offset and shift the rest of an offset to the right, the
result is called a page frame number (PFN). Shifting bits to convert between page
frame numbers and addresses is a fairly common operation; the macro
PAGE_SHIFT
tells how many bits must be shifted to make this conversion.
High and Low Memory
The difference between logical and kernel virtual addresses is highlighted on 32-bit
systems that are equipped with large amounts of memory. With 32 bits, it is possible
to address 4 GB of memory. Linux on 32-bit systems has, until recently, been lim-
ited to substantially less memory than that, however, because of the way it sets up
the virtual address space.
The kernel (on the x86 architecture, in the default configuration) splits the 4-GB vir-
tual address space between user-space and the kernel; the same set of mappings is
used in both contexts. A typical split dedicates 3 GB to user space, and 1 GB for ker-
nel space.
*
The kernel’s code and data structures must fit into that space, but the big-
gest consumer of kernel address space is virtual mappings for physical memory. The
kernel cannot directly manipulate memory that is not mapped into the kernel’s
address space. The kernel, in other words, needs its own virtual address for any
memory it must touch directly. Thus, for many years, the maximum amount of phys-
ical memory that could be handled by the kernel was the amount that could be
mapped into the kernel’s portion of the virtual address space, minus the space
* Many non-x86 architectures are able to efficiently do without the kernel/user-space split described here, so
they can work with up to a 4-GB kernel address space on 32-bit systems. The constraints described in this
section still apply to such systems when more than 4 GB of memory are installed, however.
,ch15.13676 Page 415 Friday, January 21, 2005 11:04 AM