Appendix A: ARM64 Page Tables

This appendix collects the details of the ARM64 page table format, and of what the hardware does with your page tables. You won’t need all of it at once. Here is what each quest relies on:

Everything here describes the configuration our OS uses: 4 KiB pages, 48-bit virtual addresses, and only one of the two top-level page table registers. ARM64 also supports 16 KiB and 64 KiB pages, 52-bit addresses, and other variations, and the ARM manual (like many web pages) describes all of them at once. When you read other sources, check that what you are reading applies to a 4 KiB granule (ARM’s name for the page size).

A note on naming. We call the four page table levels the top-level, 2nd-level, 3rd-level, and last-level page tables. Other sources number them instead, and not all in the same direction: CS 300 counted from L4 (the top) down to L1 (the bottom), while the ARM manual counts from level 0 (the top) to level 3 (the bottom). So ARM’s level 1 is our 2nd level, and ARM’s level 3 is our last level. Linux and many other sources use yet another set of names (PGD, PUD, PMD, PTE). The table in the next section lines them all up. (In the code, we refer to a page table by its top-level table: kernel_pagetable, for example, points to the kernel’s top-level table.)


The big picture

ARM64 page tables work much like the x86-64 page tables you may know from CS 300:

PropertyValue
Page size4 KiB = 212 bytes
Descriptor (page table entry) size8 bytes
Page table sizeone page, so 4096 / 8 = 512 descriptors
Levels4
Virtual address bits used per level9 (because 29 = 512)
Virtual address size48 bits: 4 × 9 index bits + 12 offset bits
Physical address in a descriptorbits 47:12 (the low 12 bits are always 0)

Each level covers 512 times as much memory as the level below it:

Our nameARM nameCS 300 nameLinux nameIndexed by VA bitsOne descriptor coversOne table covers
top-levellevel 0L4PGD47:39512 GiB256 TiB (all 48 bits)
2nd-levellevel 1L3PUD38:301 GiB512 GiB
3rd-levellevel 2L2PMD29:212 MiB1 GiB
last-levellevel 3L1PTE20:124 KiB2 MiB

Translation regimes and the two TTBRs

An ARM64 CPU has a separate translation setup (a translation regime) for each of EL3, EL2, and EL1. The EL1 regime translates the addresses used at both EL1 and EL0, which is why it is sometimes called “EL1&0”. It is the only regime we use, and this is why all of the paging registers in this project end in _EL1.

The EL1&0 regime has two top-level page tables, one pointed to by TTBR0_EL1 and one by TTBR1_EL1. The top 16 bits of a virtual address decide which one translates it:

VA bits 63:48Address rangeTranslated through
all 00x0000'0000'0000'0000 to 0x0000'FFFF'FFFF'FFFFTTBR0_EL1
all 10xFFFF'0000'0000'0000 to 0xFFFF'FFFF'FFFF'FFFFTTBR1_EL1
anything elsenothing: the access takes a translation fault

Our OS turns off translation through TTBR1_EL1 (by setting the EPD1 bit in TCR_EL1), so every address that your OS uses must have bits 63:48 all zero. An access to any other address takes a translation fault at ARM level 0.

What “high” means in our OS. Linux and most other ARM64 kernels put the kernel in the TTBR1 range (addresses that start with 0xFFFF) and processes in the TTBR0 range, so that switching processes only needs to change TTBR0_EL1. Our OS is simpler: everything, including the kernel’s “high map” at KERNEL_VIRTUAL_BASE (0x8000'0000'0000), lives in the TTBR0 range. KERNEL_VIRTUAL_BASE is the address with only bit 47 set: the exact middle of the TTBR0 range, and far above any physical address. Its top-level index is 256, so the high map uses only the upper half of the top-level table (descriptors 256 to 511), well away from the low addresses that processes use. Because there is only one page table for both, every process’s page table must contain the kernel’s mappings, too (mmu_map_kernel() in Quest 4). The kernel is linked at its high-map addresses (Quest 3), so it runs there from the moment it turns on the MMU.

Registers

These are the system registers that control translation. You read and write them with mrs and msr, as in earlier projects.

RegisterMeaning
TTBR0_EL1Translation Table Base Register 0: the physical address of the top-level page table in its low bits (it must be page-aligned), and the address space ID (ASID) in bits 63:48 (see nG and ASIDs). mmu_boot_enable() sets it to the boot page table, and the provided mmu_switch_pagetable() changes it: to kernel_pagetable (Quest 3), and to the next process’s page table on each context switch (Quest 4).
TCR_EL1Translation Control Register: the shape of translation. It sets the virtual address size (T0SZ = 16, for 64 − 16 = 48 bits), the page size (TG0, 4 KiB), how the hardware’s own reads of page tables are cached (IRGN0, ORGN0, SH0), whether TTBR1 is used at all (EPD1), the physical address size (IPS), and how ASIDs work (A1, AS). We provide its value as TCR_VALUE in kernel/vm/mmu_regs.h.
MAIR_EL1Memory Attribute Indirection Register: eight memory types that descriptors refer to by number (see memory attributes). We provide its value as MAIR_VALUE in kernel/vm/mmu_regs.h, and you decode it in Quest 3.
SCTLR_EL1System Control Register: bit 0 (M) turns the MMU on. Bit 19 (WXN) makes every writable page non-executable. Bits 2 (C) and 12 (I) turn on the data and instruction caches.
ESR_EL1, FAR_EL1After a page fault: why it happened, and the virtual address that caused it (see page faults).
ID_AA64MMFR0_EL1Read-only: what this CPU’s MMU supports. Its PARange field gives the physical address size, which mmu_boot_enable() copies into TCR_EL1.

The virtual address format

With 4 KiB pages and 48-bit virtual addresses, a virtual address splits into four 9-bit indices and a 12-bit offset:

A virtual address split into four 9-bit page table indices and a 12-bit offset, and the four-level walk from TTBR0_EL1 to a physical page

VA bits63:4847:3938:3029:2120:1211:0
Fieldunused (all 0 for TTBR0)top-level index2nd-level index3rd-level indexlast-level indexoffset in the page
ARM namelevel 0level 1level 2level 3
Each entry containspointer to a 2nd-level table (no block entry)pointer to a 3rd-level table, or base address of a 1 GiB blockpointer to a last-level table, or base address of a 2 MiB blockbase address of a 4 KiB pagePA bits 11:0

In other words, each index is 9 bits wide: the last-level index starts at bit 12, the 3rd-level index at bit 21, the 2nd-level index at bit 30, and the top-level index at bit 39. To extract an index, shift the VA right by its start bit and keep the low 9 bits (a mask of 0x1FF). The offset is the low 12 bits (a mask of 0xFFF). Since every table holds 512 descriptors of 8 bytes each, the descriptor for index i sits at byte 8 × i of its table.

Example. Here is the virtual address 0x8000'0008'1234, a byte of kernel code in the kernel’s high map (KERNEL_VIRTUAL_BASE + 0x8'1234), split into its fields:

VA = 0x0000'8000'0008'1234
   = 0000 0000 0000 0000  1000 0000 0000 0000  0000 0000 0000 1000  0001 0010 0011 0100
     (bits 63:48)         (bits 47:32)         (bits 31:16)         (bits 15:0)

bits 63:48                    = all 0                            (as required for TTBR0)
bits 47:39 (top-level index)  = 1 0000 0000      = 0x100 = 256
bits 38:30 (2nd-level index)  = 0 0000 0000      =           0
bits 29:21 (3rd-level index)  = 0 0000 0000      =           0
bits 20:12 (last-level index) = 0 1000 0001      = 0x081 = 129
bits 11:0  (offset)           = 0010 0011 0100   = 0x234

To translate this address, the hardware reads descriptor 256 of the top-level table (at byte 8 × 256 = 0x800 of that table), descriptor 0 of the 2nd-level table, descriptor 0 of the 3rd-level table, and descriptor 129 of the last-level table (at byte 0x408). The physical address is the page address in that last-level descriptor plus 0x234. With the high map from Quest 3 in place, that is physical address 0x8'1234. (The 2nd-level and 3rd-level indices are 0 only because this physical address is below 2 MiB. The high-map address of a larger physical address has nonzero indices there, too.)

Open this example in the page table calculator to see the split bit by bit, and change the address to try your own.


How the hardware walks the page tables

When the CPU accesses a virtual address that is not in its TLB, a part of the MMU called the table walker translates it, following the arrows in the figure above:

  1. Pick the top-level table. If bits 63:48 of the VA are not all zero, stop with a translation fault at level 0 (see above). Otherwise, read the physical address of the top-level table from TTBR0_EL1.
  2. Top level. Read the 8-byte descriptor at (top-level table address) + 8 × (top-level index). If it is not a valid table descriptor, stop with a translation fault at ARM level 0. Otherwise, bits 47:12 of the descriptor are the physical address of the 2nd-level table.
  3. 2nd and 3rd level. The same, with the 2nd-level and 3rd-level indices. At these two levels, a valid descriptor can also be a block descriptor, which ends the walk early.
  4. Last level. Read the descriptor at (last-level table address) + 8 × (last-level index). If it is not a valid page descriptor, stop with a translation fault at ARM level 3. Otherwise, the physical address is bits 47:12 of the descriptor plus the 12-bit offset from the VA.
  5. Check the access. If the descriptor’s access flag is 0, the access takes an access flag fault. If its permission bits do not allow the access (for example, a write to a read-only page), the access takes a permission fault (see page faults).
  6. Remember the result. The CPU stores the translation in the TLB, so that the next access to the same page needs no walk at all.

A few consequences are of this design:

  • The table walker only uses physical addresses. TTBR0_EL1 and every descriptor hold physical addresses, and the walker reads them without translating them. Your C code is in the opposite situation: once the MMU is on, it can only use virtual addresses. This is why your software walk (Quest 2) must convert each table’s physical address into a pointer with mmu_pa_to_ptr() before it reads the table, and convert pointers to physical addresses with mmu_ptr_to_pa() before it writes them into a descriptor.
  • Missing a mapping at a high level unmaps a lot. An invalid descriptor in the top-level table makes 512 GiB of the address space inaccessible. This is also why a page table that maps a few pages needs only a few table pages: tables that would contain only invalid descriptors don’t need to exist.
  • A TLB miss costs four memory reads (one per level). This is why the TLB matters so much for performance, and why block descriptors help: they end the walk earlier, and one TLB entry then covers a whole block.
  • The walker does not write to your page tables. Some newer ARM CPUs (ARMv8.1 and later) can set the access flag and track dirty pages in hardware, but the Raspberry Pi 3’s Cortex-A53 cores cannot, and our OS does not turn this feature on. Your page tables change only when your kernel changes them.

Your software walk in Quest 2 follows the same steps. The difference is that where the hardware would fault, your walk returns “not mapped”.


Descriptor formats

Every descriptor is 64 bits wide. Bits 1:0, together with the level of the table it sits in, decide what kind of descriptor it is:

Bits 1:0In the top-level tableIn a 2nd-level or 3rd-level tableIn a last-level table
0b00 or 0b10 (bit 0 = 0)invalidinvalidinvalid
0b01invalidblock descriptorinvalid
0b11table descriptortable descriptorpage descriptor

Notice that table descriptors and page descriptors have the same type bits: the hardware tells them apart only by the level of the table it finds them in. If your code writes a page descriptor into a 3rd-level table by mistake (for example, because it stopped walking one level too early), the hardware will treat the page as if it were a last-level table, and interpret whatever data the page holds as descriptors.

Invalid descriptors

If bit 0 of a descriptor is 0, the descriptor is invalid, and any access through it takes a translation fault. The hardware ignores the other 63 bits completely, so the OS can store whatever it likes in them. (Linux, for example, records where on disk a swapped-out page lives.) A zeroed page, which is what kalloc() returns, is a page table with 512 invalid descriptors: it maps nothing. The TLB never holds an entry for an invalid descriptor.

Bit 0 is necessary for a valid descriptor, but it is not enough: as the table above shows, a descriptor whose bits 1:0 are 0b01 is invalid, too, if it sits in the top-level table or in a last-level table. The hardware takes a translation fault on it, but a software walk that only checks bit 0 (DESC_VALID) would follow it.

Page descriptors (last level)

A page descriptor maps one 4 KiB virtual page to one 4 KiB physical page. Its bits are:

The 64 bits of a last-level page descriptor, from the ignored bits 63:59 on the left to the valid bit 0 on the right

BitsNamemmu_regs.hMeaning
0ValidDESC_VALIDMust be 1 for any valid descriptor. If 0, any access through the descriptor faults.
1TypeDESC_PAGEMust be 1 in a last-level descriptor. (0b01 in bits 1:0 is not a valid last-level descriptor.)
4:2AttrIndx (MAIR index)DESC_ATTRINDX(i)Which slot of MAIR_EL1 gives the memory type of this page. See memory attributes.
5NS: non-secureOnly matters for software in ARM’s “Secure state”. We always leave it 0.
6AP[1]: EL0 accessDESC_AP_*1 for a page that user mode (EL0) and kernel mode (EL1) can access, and 0 for a page that only kernel mode can access. See access permissions.
7AP[2]: read-onlyDESC_AP_*1 for a read-only page and 0 for a writable page.
9:8SH: shareabilityDESC_SH_*Which CPU cores’ caches are kept coherent for this page. We use inner shareable (0b11). See memory attributes.
10AF: access flagDESC_AFA fault is triggered on the first access through this descriptor unless this bit is 1. We always set it. See the access flag.
11nG: not global0 = the translation is valid in every address space. We leave it 0. See nG and ASIDs.
47:12Output addressDESC_ADDR_MASKThe physical address of the page. Its low 12 bits are always 0, so they are not stored.
51:48Reserved, must be 0. (On CPUs with physical addresses longer than 48 bits, these bits can hold more address bits.)
52ContiguousA hint that this descriptor is one of 16 adjacent descriptors that map one contiguous, aligned 64 KiB region with identical attributes, so the TLB can cache them as one entry. We leave it 0.
53PXN: kernel-mode no executeDESC_NX_KERNELIf this bit is 1, this page cannot be executed in kernel mode (EL1).
54UXN: user-mode no executeDESC_NX_USERIf this bit is 1, this page cannot be executed in user mode (EL0).
58:55Reserved for softwareIgnored by the hardware. The OS can use them for its own purposes. See software bits.
63:59Ignored by the hardware on our CPU. Newer CPUs give some of them a meaning, so leave them 0 and use bits 58:55 instead.

ARM calls bits 11:2 the lower attributes and bits 63:52 the upper attributes of the descriptor. The sections below describe the fields that need more than one line.

Access permissions: AP, UXN, and PXN

The two AP bits decide who may read and write the page:

AP[2:1] (bits 7:6)mmu_regs.hKernel (EL1)User (EL0)
0b00DESC_AP_EL1_RWread/writeno access
0b01DESC_AP_ALL_RWread/writeread/write
0b10DESC_AP_EL1_ROread-onlyno access
0b11DESC_AP_ALL_ROread-onlyread-only

There is no combination that gives user mode more access than kernel mode: the kernel can read and write every page that a process can. (Newer ARM CPUs have a feature called PAN that lets the kernel lock itself out of user pages, but the Raspberry Pi 3 does not.) This is what lets your kernel copy to and from user memory through user virtual addresses in Quest 4.

Whether a page can be executed is controlled separately, by the two XN (“execute never”) bits:

  • User mode (EL0) may execute a page if and only if UXN (bit 54) is 0. This does not depend on the AP bits! A page with AP = 0b00 (no EL0 access) and UXN = 0 is execute-only for user mode: a process cannot read or write it, but it can jump into it and run the instructions there. If you don’t want user mode to execute a page, you must set its UXN bit.
  • Kernel mode (EL1) may execute a page if PXN (bit 53) is 0, and user mode cannot write the page. The hardware treats every page with AP = 0b01 (user-writable) as if its PXN bit were set, so the kernel can never be tricked into executing instructions that a process wrote.
  • If WXN (bit 19 of SCTLR_EL1) is 1, every writable page is also non-executable. Our OS leaves WXN at 0, so keeping code and writable data apart is up to the attributes you choose.

An access that the permissions do not allow takes a permission fault: a data abort for a read or write, and an instruction abort for an attempt to execute.

Memory attributes: AttrIndx and SH

The memory type of a page (normal memory or device memory, cached or not) is not stored in the descriptor itself. Instead, AttrIndx (bits 4:2) holds a slot number from 0 to 7, and slot i of MAIR_EL1 holds the actual 8-bit memory type. This indirection saves descriptor bits, and it lets the OS change what a memory type means in a single register. In our OS, slot 0 (MAIR_IDX_NORMAL) holds normal memory, for RAM, and slot 1 (MAIR_IDX_DEVICE) holds device memory, for the peripherals. The background on MAIR in Quest 5 explains the encoding of a slot.

The difference matters because the CPU is allowed to cache normal memory, read it speculatively, reorder accesses to it, and combine several accesses into one. All of this is harmless for RAM, but device registers can have side effects when you read or write them, so device memory forbids these optimizations. Unaligned accesses to device memory also fault. While the MMU is off, the CPU treats all data accesses as device memory accesses, which is why the compiler-generated unaligned accesses that work fine with the MMU on can fault before it is on.

SH (bits 9:8) says which CPU cores must see a consistent view of the page through their caches:

  • 0b00: non-shareable. The memory can only be safely accessed by one CPU core. Reads and writes can stay in that core’s cache and are not necessarily visible to other cores.
  • 0b11: inner shareable. 0b10: outer shareable. When a core writes to a shareable page, the change is visible to all other cores in the same inner or outer shareability domain. The exact definition of a domain is up to the hardware, but the ARM documentation says that all the cores used by one OS should be in the same inner shareability domain.

We use inner shareable for everything. (For device memory, the hardware ignores SH and always treats the page as shareable.)

The access flag

If AF (bit 10) is 0, the first access through the descriptor takes an access flag fault. OSes use this to find out which pages are in use: an OS that needs to evict a page to disk can clear the access flags of a few candidate pages, and see which of them fault again soon. (This is how Linux approximates “least recently used”.) Our OS does not swap pages to disk, so we always set AF. An access flag fault in your OS means that you built a descriptor without DESC_AF.

nG and ASIDs

The TLB caches translations by virtual address. Two processes use the same virtual addresses for different physical pages, so in our OS the kernel flushes the whole TLB on every context switch (Quest 4). The nG bit (bit 11) offers an alternative:

  • nG = 0 (global): the TLB entry for this page matches in every address space. This is right for kernel mappings, which are the same in every page table.
  • nG = 1 (not global): the TLB entry is tagged with the address space ID (ASID) from TTBR0_EL1 bits 63:48 at the time of the walk, and it only matches while the same ASID is in TTBR0_EL1.

With different ASIDs per process and nG = 1 on user pages, the kernel no longer needs to flush the TLB when it switches processes. The background on ASIDs in Quest 4 has more details, and extra credit Quest 4-E1 asks you to implement it. Unless you do, leave nG at 0.

Software bits

The hardware ignores bits 58:55 of a valid page (or block) descriptor, so the OS can use them to keep a few bits of information about each page right in the descriptor. Linux, for example, uses one of them to remember whether a page is dirty on CPUs that cannot track this in hardware.

For example, an OS that implements copy-on-write fork(), where the parent and the child share their pages until one of them writes to a page, can use a software bit to mark copy-on-write pages. To the hardware, a copy-on-write page is just a read-only page, so a write to it takes a permission fault. The software bit lets the OS’s fault handler tell such a page apart from a page that is meant to be read-only, such as a program’s code.

Table descriptors (top, 2nd, and 3rd level)

A table descriptor points to the next table down. Its format is simpler than that of a page descriptor:

BitsNameMeaning
0Valid1
1Type1: this is a table descriptor (0 would make it a block descriptor)
11:2Ignored by the hardware
47:12Next-level table addressThe physical address of the next table. Tables are one page, so the low 12 bits are 0.
51:48Reserved, must be 0
58:52Ignored by the hardware
59PXNTable1: nothing mapped through this descriptor can be executed in kernel mode
60UXNTable1: nothing mapped through this descriptor can be executed in user mode
62:61APTableBit 61 = 1: nothing mapped through this descriptor can be accessed from user mode. Bit 62 = 1: nothing mapped through this descriptor can be written.
63NSTableOnly matters in the Secure state. Leave it 0.

Bits 63:59 are hierarchical attributes. They apply to everything that the next tables down map, and they can only take permissions away, never add them: the effective permission of a page is the most restrictive combination of all the levels on its walk. They let an OS make a whole 2 MiB, 1 GiB, or 512 GiB region kernel-only or non-executable by setting one bit. Our OS sets them all to 0 (“no extra restrictions”), so that the last-level descriptor alone decides a page’s permissions.

Notice that the bits that hold the AP, SH, AF, and AttrIndx fields in a page descriptor (bits 11:2), and the PXN and UXN bit positions of a page descriptor (bits 54:53), are ignored in a table descriptor. Copying page attributes into a table descriptor therefore does no harm, but it also has no effect: you cannot make a page read-only by changing the descriptor in its 3rd-level table.

Block descriptors (2nd and 3rd level)

In a 2nd-level or 3rd-level table (but not in the top-level table), a descriptor whose bits 1:0 are 0b01 is a block descriptor. It maps a whole aligned block of physical memory directly, without any tables below it. Blocks are also called “huge pages” or “superpages”.

LevelBlock sizeOutput address bitsLow VA bits used as the offset
2nd level1 GiB47:3029:0
3rd level2 MiB47:2120:0

A block descriptor has the same upper and lower attributes as a page descriptor (bits 63:52 and 11:2), and they apply to the whole block. The bits between the output address and bit 12 (bits 29:12 at the 2nd level, 20:12 at the 3rd level) are reserved and must be 0. So the physical address of a block must be aligned to the block size, and so must its virtual address, since the block replaces a whole table’s worth of the address space.

Block mappings use fewer table pages, make page table walks shorter, and let one TLB entry cover a whole block. The price is that the whole block must be physically contiguous and has one set of permissions. The boot page table that we provide (kernel/vm/mmu_boot.S, Quest 3) maps the Pi’s memory with 2 MiB blocks, in four tables. Your own page tables only use page descriptors, unless you do extra credit Quest 3-E1. If you do, every function that visits your page tables (walking, destroying, copying, printing) must recognize block descriptors (bit 1 = 0), and must not follow them as if they pointed to a table.


Worked example: decoding a descriptor

Suppose that you find the value 0x0060'0000'1000'3743 in a last-level table. Split it into its fields, from the top bit down:

0x0060'0000'1000'3743

bits 63:55  = 0                       no software bits set
bit  54     = 1   UXN                 user mode cannot execute
bit  53     = 1   PXN                 kernel mode cannot execute
bit  52     = 0   Contiguous          no hint
bits 51:48  = 0                       reserved
bits 47:12  = 0x10003                 output address 0x1000'3000

low 12 bits = 0x743 = 0111 0100 0011
bit  11     = 0   nG                  global
bit  10     = 1   AF                  access flag set
bits 9:8    = 11  SH                  inner shareable
bits 7:6    = 01  AP                  read/write for EL1 and EL0
bit  5      = 0   NS
bits 4:2    = 000 AttrIndx            MAIR slot 0: normal memory
bits 1:0    = 11                      valid page descriptor

So this descriptor maps a page of normal memory at physical address 0x1000'3000 (inside a kalloc() pool that starts at 256 MiB, as in our reference), which both the kernel and the user can read and write, but neither can execute: a page of a process’s data or stack.

Open this descriptor in the page table calculator to see it decoded, and toggle its bits to see what each one changes.

Looking at page tables while debugging. Once the MMU is on, GDB reads memory at virtual addresses, through whichever page table is on the CPU. To look at a page table, either use the table’s high-map address (KERNEL_VIRTUAL_BASE plus its physical address) in GDB, or read physical memory directly from QEMU’s monitor (Ctrl-a c to reach it, and again to get back): in the QEMU monitor, typing xp /8xg 0x10003000 prints the eight 8-byte values at physical address 0x1000'3000.

Debugging with a kernel linked at high addresses. Since Quest 3, every symbol in kernel/kernel8.elf is a high address, so GDB’s breakpoints and symbols work once the MMU is on, from kernel_main onward. Before that (in boot.S, until it jumps to kernel_main), the kernel runs at its physical addresses. To step through that code with symbols, load them a second time, KERNEL_VIRTUAL_BASE lower: add-symbol-file kernel/kernel8.elf -o -0x800000000000.


Page faults

The page fault decoder that you add to your kernel in Quest 3 (fault_decode() in kernel/vm/fault.c) does all of the decoding in this section for you, and fault_print() prints the result on one line. This section explains what that line means.

When a translation fails, or the descriptor forbids an access, the CPU takes a synchronous exception: an instruction abort if it was fetching an instruction, or a data abort for a load or store. The exception class in ESR_EL1 (bits 31:26) tells you which one, and where it came from:

Exception classkernel/aarch64.hMeaning
0x20ESR_EL1_EC_IABT_LOWERInstruction abort from EL0 (a process)
0x21ESR_EL1_EC_IABT_SAMEInstruction abort from EL1 (the kernel)
0x24ESR_EL1_EC_DABT_LOWERData abort from EL0
0x25ESR_EL1_EC_DABT_SAMEData abort from EL1

For both kinds of abort, the low six bits of ESR_EL1 hold the fault status code (FSC, ESR_EL1_FSC_MASK). Bits 5:2 of the FSC say what kind of fault it was (ESR_EL1_FSC_TYPE), and bits 1:0 say at which ARM level the walk was when it happened (ESR_EL1_FSC_LEVEL). These are the codes you are likely to meet:

FSCFaultWhat it usually means in our OS
0x00–0x03Address size fault, ARM level 0–3TTBR0_EL1 or a descriptor contains a physical address with bits set above what the CPU supports. Usually, the walker followed a descriptor that was built from garbage.
0x04–0x07Translation fault, ARM level 0–3There is no valid descriptor at that level for this address. At level 0, also: the address has bits 63:48 set.
0x09–0x0BAccess flag fault, ARM level 1–3The final descriptor has AF = 0.
0x0D–0x0FPermission fault, ARM level 1–3The descriptor is valid, but its permission bits forbid this access.
0x10Synchronous external abortThe memory system rejected the access: for example, the address is mapped, but there is no RAM or device at the physical address.
0x21Alignment faultAn unaligned access to device memory (or to any memory, while the MMU is off).
0x30TLB conflict abortThe TLB held two different translations for the same address. See changing page tables safely.

The level tells you how far the walk got, and so it narrows down which table is missing a descriptor. Remember that ARM counts the levels from 0 at the top: ARM level 0 is our top level, ARM levels 1 and 2 are our 2nd and 3rd levels, and ARM level 3 is our last level. For example, a translation fault at ARM level 3 means that the top-level, 2nd-level, and 3rd-level descriptors on the walk were all valid, and only the last-level descriptor for the page was invalid. For a permission fault, the level is that of the descriptor that holds the permissions: ARM level 3 for a page, or level 1 or 2 for a block.

Two more registers complete the picture:

  • FAR_EL1 holds the virtual address that the CPU tried to access. For an instruction abort, that is the address of the instruction it tried to fetch.
  • For a data abort, bit 6 of ESR_EL1 (WnR, ESR_EL1_ISS_WNR) is 1 if the access was a write, and 0 if it was a read.

As usual, ELR_EL1 holds the address of the instruction that faulted. To retry the instruction after fixing the page table (as a copy-on-write fault handler would), return to that same address.


Changing page tables safely

The table walker reads your page tables from memory, and the TLB keeps the translations it found. Neither of them knows when your kernel changes a descriptor. So every time you change a page table that the CPU might use, you must think about two things:

  1. Make the change visible to the walker. A store from your C code may still be on its way to memory when the walker reads the descriptor. A dsb barrier waits until earlier stores are done, and a following isb makes sure that instructions after it use the new state.
  2. Remove stale translations from the TLB. The TLB may still hold the old translation.

What you need to do depends on the change:

ChangeWhat to do
Invalid descriptor → valid descriptor (a new mapping)Barriers only. The TLB never holds entries for invalid descriptors, so there is nothing stale to remove.
Valid descriptor → invalid descriptor, or a valid descriptor with different permissions or a different output addressBarriers, then invalidate the TLB entry for that page (tlb_flush_va()), or the whole TLB (tlb_flush_all()).
Switch TTBR0_EL1 to a different top-level tableFlush the whole TLB, unless you use ASIDs (this is what mmu_switch_pagetable does in Quest 4).
Free a page table pageRemove the descriptor that points to it, and flush the TLB, before you reuse the page. The CPU may cache table descriptors as well as final translations.

We provide tlb_flush_va() and tlb_flush_all(), including their barriers, in kernel/vm/tlb.S. The background on barriers in Quest 5 explains what their barriers do.

Break-before-make. Strictly speaking, the ARM architecture asks for a break-before-make sequence whenever the OS changes the output address, the memory type, or the size of a valid mapping that the CPU may be using: first write an invalid descriptor, then invalidate the TLB entry (with barriers), and only then write the new descriptor. This makes sure that the TLB never holds two different translations for the same address at once, which on some CPUs causes a TLB conflict abort (FSC 0x30). Linux follows this rule, for example, when it replaces a copy-on-write page with a private copy. If you ever see a TLB conflict abort on the Raspberry Pi, you may need to do this.


Further reading

  • The page table calculator splits a virtual address into its indices and offset, and decodes a last-level descriptor, using the same configuration as this appendix. (It shows ARM’s level numbers, L0 to L3, next to our names for the levels.)

  • The translation worksheet from lecture walks you through translating addresses by hand.

  • The Arm Architecture Reference Manual for A-profile is the authority on everything in this appendix. The relevant chapter is “The AArch64 Virtual Memory System Architecture”, and the register descriptions (TTBR0_EL1, TCR_EL1, MAIR_EL1, SCTLR_EL1, ESR_EL1) are in the “AArch64 System Register Descriptions” part. Remember to look for the descriptions of the 4 KiB granule, and to read ARM’s level numbers as ours in reverse.

  • ARM’s Learn the architecture: AArch64 memory management guide is a much shorter and gentler introduction to the same material, and a good first read before you dive into the reference manual.

  • Jon Palmisciano’s ARM reference presents the system register descriptions from the manual in a compact, searchable form.