Linux Kernel Architecture

(Jacob Rumans) #1

Chapter3:MemoryManagement


The size of the address area that can be addressed with pointers ofn-bit length is 2nbytes. The kernel
defines additional macro variables to hold the values calculated so that it is unnecessary to repeat the
calculations time and time again. The variables are defined as follows:

#define PAGE_SIZE (1UL << PAGE_SHIFT)
#define PUD_SIZE (1UL << PUD_SHIFT)
#define PMD_SIZE (1UL << PMD_SHIFT)
#define PGDIR_SIZE (1UL << PGDIR_SHIFT)

The value 2nis easily calculated in the binary system by shifting a bitnpositions to the left starting from
position 0. The kernel uses this ‘‘trick‘‘ at many places. Those of you unfamiliar with bit arithmetic will
find relevant explanations in Appendix C.

include/asm-x86/pgtable_64.h
#define PGDIR_SHIFT 39
#define PTRS_PER_PGD 512

#define PUD_SHIFT 30
#define PTRS_PER_PUD 512

#define PMD_SHIFT 21
#define PTRS_PER_PMD 512

The macrosPTRS_PER_XXXspecify how many pointers (i.e., different values) a given directory entry can
represent. Since AMD64 employs 9 bits for each directory, 2^9 =512 pointers fit into each.

The kernel also needs a means of extracting the individual components from a given address. The kernel
uses the bitmasks defined below to do this.

#define PAGE_MASK (~(PAGE_SIZE-1))
#define PUD_MASK (~(PUD_SIZE-1))
#define PMD_MASK (~(PMD_SIZE-1))
#define PGDIR_MASK (~(PGDIR_SIZE-1))

The masks are applied on a given address by simple bitwise addition.

Formatof Page Tables


The size of the entries in the page tables has been established by the above definitions but not
their structure. The kernel provides four data structures (defined inpage.h) to represent the entry
structures.

❑ pgd_tfor entries of the global directory.
❑ pud_tfor entries of the page upper directory.
❑ pmd_tfor entries of the page middle directory.
❑ pte_tfor direct page table entries.

The standard functions to analyze page table entries are listed in Table 3-2. (Depending on architecture,
some functions are implemented as macros and others as inline functions; I make no distinction between
the two below.)
Free download pdf