点击左上方蓝色“Arm精选”,选择“设为星标”
1、简介
术语介绍
Translation regimes
2、构建页表示例 – 一个简单场景Single-level table at EL3
(1)、设置页表基地址VBAR_EL3
// Set the Base address
// ---------------------
LDR x0, =tt_l1_base // Get address of level 1 for TTBR0_EL3
MSR TTBR0_EL3, x0 // Set TTBR0_EL3 (NOTE: There is no TTBR1 at EL3)
section TT,"ax"
.align 12
.global tt_l1_base
tt_l1_base:
.fill 4096 , 1 , 0
(2)、初始化MAIR_EL3
// Set up memory attributes
// -------------------------
// This equates to:
// 0 = b01000100 = Normal, Inner/Outer Non-Cacheable
// 1 = b11111111 = Normal, Inner/Outer WB/WA/RA
// 2 = b00000000 = Device-nGnRnE
MOV x0, #0x000000000000FF44
MSR MAIR_EL3, x0
(3)、配置TCR_EL3 (Translation Control Register)
// Set up TCR_EL3
// ---------------
MOV x0, #0x19 // T0SZ=0b011001 Limits VA space to 39 bits,
// translation starts @ l1
ORR x0, x0, #(0x1 << 8) // IGRN0=0b01 Walks to TTBR0 are Inner WB/WA
ORR x0, x0, #(0x1 << 10) // OGRN0=0b01 Walks to TTBR0 are Outer WB/WA
ORR x0, x0, #(0x3 << 12) // SH0=0b11 Inner Shareable
// TBI0=0b0 Setting this bit causes the top 8 bits of the virtual address to be ignored
// TG0=0b00 4KB granule
// IPS=0 32-bit PA space
MSR TCR_EL3, x0
// Invalidate TLBs
// ----------------
TLBI ALLE3
DSB SY
ISB
(4)、创建页表
LDR x1, =tt_l1_base // Address of L1 table
// [0]: 0x0000,0000 - 0x3FFF,FFFF
LDR x0, =TT_S1_DEVICE_nGnRnE // Entry template
// AP=0, RW
// Don't need to OR in address, as it is 0
STR x0, [x1]
// [1]: 0x4000,0000 - 0x7FFF,FFFF
LDR x0, =TT_S1_DEVICE_nGnRnE // Entry template
// AP=0, RW
ORR x0, x0, #0x40000000 // 'OR' template with base physical address
STR x0, [x1, #8]
// [2]: 0x8000,0000 - 0xBFFF,FFFF (DRAM on the VE and Base Platform)
LDR x0, =TT_S1_NORMAL_WBWA // Entry template
ORR x0, x0, #TT_S1_INNER_SHARED // ‘OR’ with inner-shareable attribute
// AP=0, RW
ORR x0, x0, #0x80000000 // 'OR' template with base physical address
STR x0, [x1, #16]
DSB SY
(5)、Enable the MMU
// Enable MMU
// -----------
MOV x0, #(1 << 0) // M=1 Enable the stage 1 MMU
ORR x0, x0, #(1 << 2) // C=1 Enable data and unified caches
ORR x0, x0, #(1 << 12) // I=1 Enable instruction fetches to allocate
// into unified caches
// A=0 Strict alignment checking disabled
// SA=0 Stack alignment checking disabled
// WXN=0 Write permission does not imply XN
// EE=0 EL3 data accesses are little endian
MSR SCTLR_EL3, x0
ISB
3、构建页表示例 – 稍微复杂一点场景Multiple levels of table at EL3
//
// Generate L1 table
//
LDR x1, =tt_l1_base // Address of L1 table
// [0]: 0x0000,0000 - 0x3FFF,FFFF
LDR x0, =TT_S1_DEVICE_nGnRnE // Entry template
// AP=0, RW
// Don't need to OR in address, as it is 0
STR x0, [x1]
// [1]: 0x4000,0000 - 0x7FFF,FFFF
LDR x0, =TT_S1_DEVICE_nGnRnE // Entry template
// AP=0, RW
ORR x0, x0, #0x40000000 // 'OR' template with base physical address
STR x0, [x1, #8]
// [2]: 0x8000,0000 - 0xBFFF,FFFF (DRAM on the VE and Base Platform)
LDR x2, =tt_l2_base // Get address of L2 table
LDR x0, =TT_S1_TABLE // Entry template for pointer to next level table
ORR x0, x0, x2 // Combine template with L2 table Base address
STR x0, [x1, #16] // Write template into entry table[2]
//
// Generate L2 table
//
…
LDR x0, =tt_l2_base // Address of first L2 table
// The L2 table covers the address range:
// 0x8000_0000 - 0xBFFF_FFFF
//
// This example only populates entry 0, which covers:
// 0x8000_0000 - 0x801F_FFFF
LDR x1, =tt_l2_base // Address of L1 table
LDR x0, =TT_S1_NORMAL_WBWA // Entry template
ORR x0, x0, #TT_S1_INNER_SHARED // 'OR' with inner-shareable attribute
// AP=0, RW
ORR x0, x0, #0x80000000 // 'OR' template with base physical address
STR x0, [x1]
DSB SY
如下示例代码,便是L1 Table的实现
4、构建页表示例 – 继续复杂一点场景Single-level table at EL1
(1)、Configure SCR_EL3
// Configure SCR_EL3
// ------------------
MOV x0, #1 // NS=1
ORR x0, x0, #(1 << 1) // IRQ=1 IRQs routed to EL3
ORR x0, x0, #(1 << 2) // FIQ=1 FIQs routed to EL3
ORR x0, x0, #(1 << 3) // EA=1 SError routed to EL3
ORR x0, x0, #(1 << 8) // HCE=1 HVC instructions are enabled
ORR x0, x0, #(1 << 10) // RW=1 Next EL down uses AArch64
ORR x0, x0, #(1 << 11) // ST=1 Secure EL1 can access timers
MSR SCR_EL3, x0
(2)、Configure HCR_EL2
// Configure HCR_EL2
// ------------------
ORR w0, wzr, #(1 << 3) // FMO=1
ORR x0, x0, #(1 << 4) // IMO=1
ORR x0, x0, #(1 << 31) // RW=1 NS.EL1 is AArch64
// TGE=0 Entry to NS.EL1 is possible
// VM=0 Stage 2 MMU disabled
MSR HCR_EL2, x0
🌍咨询vx : sami01_2023