原創翻譯,轉載請註明出處。linux
頁錶轉換
arm64在硬件體系結構上支持4級的每頁大小爲4K的頁錶轉換,也支持3級的頁大小64KB的頁錶轉換。
在linux arm64中,若是頁的大小爲4KB,使用3級頁錶轉換或者4級頁錶轉換,用戶空間和內核空間都支持有39bit(512GB)或者48bit(256TB)大小的虛擬地址空間。
若是頁的大小爲64KB,就只有2級頁錶轉換,支持42bit(4TB)大小的虛擬地址,用戶空間和內核空間也大小同樣。
地址分佈
虛擬地址都用64位無符號數表示。
用戶虛擬地址從高位的63到低位的48都是0,內核虛擬地址則相反,63~48都是1. TTBR x(Translation table base register)的選擇是經過虛擬地址的第63 bit位來肯定的。swapper_pg_dir只包含內核地址映射,而用戶
pgd 只包含用戶地址映射,swapper_pg_dir 指向的地址只會寫入到 TTBR1,毫不會寫入到TTBR0。app
AArch64 Linux memory layout with 4KB pages + 3 levels: Start End Size Use ----------------------------------------------------------------------- 0000000000000000 0000007fffffffff 512GB user ffffff8000000000 ffffffffffffffff 512GB kernel
AArch64 Linux memory layout with 4KB pages + 4 levels: Start End Size Use ----------------------------------------------------------------------- 0000000000000000 0000ffffffffffff 256TB user ffff000000000000 ffffffffffffffff 256TB kernel
AArch64 Linux memory layout with 64KB pages + 2 levels: Start End Size Use ----------------------------------------------------------------------- 0000000000000000 000003ffffffffff 4TB user fffffc0000000000 ffffffffffffffff 4TB kernel
AArch64 Linux memory layout with 64KB pages + 3 levels: Start End Size Use ----------------------------------------------------------------------- 0000000000000000 0000ffffffffffff 256TB user ffff000000000000 ffffffffffffffff 256TB kernel
想看內核虛擬地址佈局的詳細狀況,能夠經過觀察內核boot日誌。佈局
Translation table lookup with 4KB pages: +--------+--------+--------+--------+--------+--------+--------+--------+ |63 56|55 48|47 40|39 32|31 24|23 16|15 8|7 0| +--------+--------+--------+--------+--------+--------+--------+--------+ | | | | | | | | | | | v | | | | | [11:0] in-page offset | | | | +-> [20:12] L3 index | | | +-----------> [29:21] L2 index | | +---------------------> [38:30] L1 index | +-------------------------------> [47:39] L0 index +-------------------------------------------------> [63] TTBR0/1
Translation table lookup with 64KB pages: +--------+--------+--------+--------+--------+--------+--------+--------+ |63 56|55 48|47 40|39 32|31 24|23 16|15 8|7 0| +--------+--------+--------+--------+--------+--------+--------+--------+ | | | | | | | | | v | | | | [15:0] in-page offset | | | +----------> [28:16] L3 index | | +--------------------------> [41:29] L2 index | +-------------------------------> [47:42] L1 index +-------------------------------------------------> [63] TTBR0/1
當使用kvm時,虛擬機管理器(hypervisor)內核頁表映射在EL2(CPU異常級別),經過對內核虛擬地址固定的偏移來肯定(高24位內核虛擬地址設置爲0):翻譯
Start End Size Use ----------------------------------------------------------------------- 0000004000000000 0000007fffffffff 256GB kernel objects mapped in HYP