Lab5: Identity Paging
Goals of this lab
- Understand ARMv8-A virtual memory system architecture.
- Understand how to design paging bookkeeping.
- Understand how to design multitasking with virtual memory.
- Understand how user programs loaded.
- Understand how to prevent invalid memory access.
- Understand how demand paging works.
- Understand how copy-on-write works.
Settings
Source: Armv8-A Address Translation
TCR_EL1, Translation Control Register(EL1)

IPS
maximum output address size
- 000=32 bits
- 101=48 bits
TG0/TG1
the smallest block of memory that can be independently mapped in the translation tables.
- 其實就是 page 大小
- 00=4KB, 01=16KB, 11=64KB
T0SZ
The size offset of the memory region addressed by TTBR0_EL1. The region size is 2(64-T1SZ) bytes.
T1SZ
The size offset of the memory region addressed by TTBR1_EL1. The region size is 2(64-T1SZ) bytes.
1 | /* Translation Control Register (TCR) */ |
TTBRn_EL1, Translation Table Base Register 0 (EL1)
TTBR0_EL1 和 TTBR1_EL1 分別對應高位和低位的 virtual address,例如
TTBR0_EL1對應的記憶體範圍0x00000000_00000000~`0x0000FFFF_FFFFFFFF大小為2^48 bytes,T0SZ`=64-48=12TTBR1_EL1對應的記憶體範圍0xFFFF0000_00000000~`0xFFFFFFFF_FFFFFFFF大小為2^48 bytes,T1SZ`=64-48=12
MAIR_EL1, Memory Attribute Indirection Register (EL1)

MMU 對不同的 memory block 用不同屬性描述,不同屬性對應著不同行為。
deivce memory 的屬性不多,又附加上幾種特性:
- Gathering
- Re-ording
- Early Write Acknowledgement
lab 使用到兩種 memory attribute
- Device memory nGnRnE:
- Peripheral access.
- The most restricted memory access.
- Normal memory without cache:
- Normal RAM access.
- Memory gathering, reordering, and speculative execution are possible but without cache.
Reference: arm64 memory 属性 Device-nGnRnE
1 | /* Memory attribute indirection register (MAIR) */ |
Translation Virtual Address To A Physical Adress
礙於架構因素,ARMv8-A 並不支援 64-bit virtual address,bits [63:48] 必須一致。
virtual address 0x000…使用 TTBR0_EL1,0xFFF…使用 TTBR1_EL1,T0SZ/T1SZ 限制 TTBR0_EL1/TTBR1_EL1 的範圍。
4KB granule

64KB granule

Example
- 64KB granule
- 42-bit virtual address
- 48-bit physical address
- VA[64:42]=1→使用 TTBR1 指向的 page table,VA [64:42]=0→使用 TTBR0 指向的 page table
- L2 page table 包含 2^13=8192 page table entry,並透過 VA[41:29] 進行索引進行查找
- MMU 檢查 page table entry 正確性及權限
- 該 page table entry 指向下一級 page table (table descriptor),以 VA[28:16] 作為索引進行查找
- MMU 檢查 page table entry 正確性及權限
- 該 page table entry 指向下一級 page (page descriptor),VA[47:16] 作為 PA[47:16],VA[15:0] 作為 PA[15:0]
- 返回 PA[47:0]

Identity Paging
將 virtual address 映射到相同的 physical address 為 identity paging。
kernel/start.S

映射 4GB memory,使用 Two-level translation
1 | // load exception_table to VBAR_EL1 |

Map kernel to the upper address space
kernel/start.S
1 | ldr x2, = BOOT_PUD_ATTR |
kernel/linker.ld
kernel 現在被映射到 0xFFFF000000000000 上方,因此必須對 linker.ld 進行修改,以讓 program counter 及 stack pointer 使用正確的值。
1 | SECTIONS |
KERNEL_VIRT_BASE
連同使用記憶體位置的 marco 都需要加上 offset。
include/mm.h
1 |
include/base.h
1 |
Indirect Branch
啟用 mmu 後,得將 stack pointer 加上 offset。進到 main 前再使用 indirect branch 調整 program counter,adr 等 pc-relative 的指令才會使用到 virtual address。
kernel/start.S
1 | + // mov sp to virtual address |
Demo
因為尚未切一塊 page 給 user,執行 user task 時會進到 exception handler。
