11-Memory Management Examples
快速鏈接:
.
👉👉👉 個(gè)人博客筆記導(dǎo)讀目錄(全部) 👈👈👈
相關(guān)鏈接: (專題:《learn-the-architecture系列》)
- 01-Introducing the Arm architecture
- 02-Armv8-A Instruction Set Architecture
- 03_Introduction_to_AMBA_AXI
- 04-TrustZone for Armv8-A
- 05-Exception model
- 06-GICv3_v4_overview
- 07-Armv8-A virtualization
- 08-Isolation using virtualization in the Secure World_Whitepaper
- 09-LearnTheArchitecture-MemoryManagement
- 10-Armv8-A memory model guide–ongoing
- 11-Memory Management Examples
- 12-Generic Timer
- 13-Introduction to security
- 14-Providing protection for complex software
- 15-Arm-Confidential-Compute-Software-Stack
- 16-Understanding the Armv8.x extensions
目錄
- 1、簡(jiǎn)介
- 術(shù)語(yǔ)介紹
- Translation regimes
- 2、構(gòu)建頁(yè)表示例 -- 一個(gè)簡(jiǎn)單場(chǎng)景Single-level table at EL3
- (1)、設(shè)置頁(yè)表基地址VBAR_EL3
- (2)、初始化MAIR_EL3
- (3)、配置TCR_EL3 (Translation Control Register)
- (4)、創(chuàng)建頁(yè)表
- (5)、Enable the MMU
- 3、構(gòu)建頁(yè)表示例 -- 稍微復(fù)雜一點(diǎn)場(chǎng)景Multiple levels of table at EL3
- 4、構(gòu)建頁(yè)表示例 -- 繼續(xù)復(fù)雜一點(diǎn)場(chǎng)景Single-level table at EL1
- (1)、Configure SCR_EL3
- (2)、Configure HCR_EL2
今天我們進(jìn)行幾個(gè)示例,怎樣去配置MMU并建立頁(yè)表,加深對(duì)VMSA/MMU/CACHE等的理解。
1、簡(jiǎn)介
在學(xué)習(xí)之前我們先回顧一些概念
術(shù)語(yǔ)介紹
- (1)、flat map : 一一映射,也就是虛擬地址=物理地址,官方說(shuō)法是This means that the input virtual address and output physical address are the same for all translations,此時(shí)MMU作用其實(shí)就是控制內(nèi)存屬性和權(quán)限
(2)、full level 1 table : 一個(gè)完整的 L1 頁(yè)表
Translation regimes
在ARMV8-aarch64的系統(tǒng)中,有如下Translation regimes,而我們今天就以Secure EL3 translation regime為例
Secure EL1&0 translation regime, when EL2 is disabled
Non-secure EL1&0 translation regime, when EL2 is disabled
Secure EL1&0 translation regime, when EL2 is enabled
Non-secure EL1&0 translation regime, when EL2 is enabled
Secure EL2&0 translation regime
Non-secure EL2&0 translation regime
Secure EL2 translation regime
Non-secure EL2 translation regime
Secure EL3 translation regime
2、構(gòu)建頁(yè)表示例 – 一個(gè)簡(jiǎn)單場(chǎng)景Single-level table at EL3
構(gòu)建頁(yè)表都需要做哪些事情呢?
- 設(shè)置頁(yè)表基地址VBAR_EL3 (Specify the location of the translation table)
- 初始化MAIR_EL3 (Memory Attribute Indirection Register)
- 配置TCR_EL3 (Configure the translation regime)
- 創(chuàng)建頁(yè)表 (Generate the translation tables)
- Enable the MMU
(1)、設(shè)置頁(yè)表基地址VBAR_EL3
設(shè)置 TTBR0_EL3 = tt_l1_base
// 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)思考: 那么 tt_l1_base 到底在哪里呢?
注意,Translation tables必需是對(duì)齊的, 如我們示例中,有一個(gè)完整的L1頁(yè)表,4KB的granule,一個(gè)完整的L1頁(yè)表包含512個(gè)entries,每個(gè)entries是8bytes,所以一個(gè)完整的L1頁(yè)表就是4KB, 所以tt_l1_base必需4KB對(duì)齊的
tt_l1_base 可以是定義在section段的一個(gè)全局變量,初始化全0,注意全0的entry恰好fault entry
section TT,"ax" .align 12 .global tt_l1_base tt_l1_base: .fill 4096 , 1 , 0那么fault entry又是什么呢?
請(qǐng)參考ARMV8官方文檔
In general, a descriptor is one of:
? An invalid or fault entry.
? A table entry, that points to the next-level translation table.
? A block entry, that defines the memory properties for the access.
? A reserved format.
那么 descriptor 又是什么呢??沒(méi)完沒(méi)了了是不,descriptor 可以理解成就是 entry.
(2)、初始化MAIR_EL3
我們先看以下MAIR_EL3寄存器的用法,64位的MAIR_EL3寄存器被分成了8個(gè)Attr,每一個(gè)Attr可以表示一類內(nèi)存屬性
每一個(gè)Attr的配置如下:
對(duì)于Device Memory的配置,dd是啥含義呢?
對(duì)于Normal Memory的配置,oooo和iiii是啥含義呢?
那么RW又是啥含義呢?
好了,寄存器知識(shí)復(fù)習(xí)完了,接下來(lái)我們來(lái)看實(shí)列
如下所示,設(shè)置了3個(gè)index(3個(gè)Attr),分別是:
- index0 (Attr0) : 0x01000100 = Normal, Inner/Outer Non-Cacheable
- index1 (Attr1) : 0x11111111 = Normal, Inner/Outer WB/WA/RA
- index2 (Attr2) : 0x00000000 = Device-nGnRnE
設(shè)置完后,留著,后有大用!!
(3)、配置TCR_EL3 (Translation Control Register)
非常復(fù)雜的一個(gè)系統(tǒng)寄存器,不過(guò)還好,哥已經(jīng)給你整理出了一篇文檔,專門介紹TCR,參見(jiàn)《https://blog.csdn.net/weixin_42135087/article/details/112462416》, 本文就不再詳細(xì)介紹這些比特位了。
然后我們就可以看示例代碼了,牛批不是吹,你看這一個(gè)小小寄存器干了了多少事情:
- 配置了虛擬地址有效位是 39 ,也就意味著translation starts at L1, Why ???
- 頁(yè)表所在內(nèi)存的內(nèi)存屬性,Inner WB/WA、Outer WB/WA、Inner Shareable
- ignore top bytes
- 4KB granule
- 輸出32位物理地址
我們?cè)賮?lái)看看,為什么 配置了虛擬地址有效位是 39 ,就是意味著translation starts at L1 ?
請(qǐng)參考ARMV8的官方文檔,這是address translation的格式要求,虛擬地址有效位是39,也就是BIT[38:0], 所以也就不存在Level 0 table了,TTBR_EL3直接執(zhí)行Level 1 Table。
然后我們?cè)賮?lái)看每一個(gè)Table或每一個(gè)entry能夠cover住的范圍:
- L0 table: 512GB per entry – 注意,本示例中,沒(méi)有L 0 Table
- L1 tables: Each table covers 512GB, 1GB per entry
- L2 tables: Each table covers 1GB, 2MB per entry
- L3 tables: Each table covers 2MB, 4KB per entry
再配置好MMU相關(guān)的寄存器后,一定要記得 Invalidate TLBs, 這里操作的是Secure EL3 translation regime, 所以只要invalid EL3的TLB即可:
// Invalidate TLBs // ---------------- TLBI ALLE3 DSB SY ISB(4)、創(chuàng)建頁(yè)表
我們創(chuàng)建一個(gè)啥樣子的頁(yè)表好呢?
TTBR0_EL3 指向 Level 1 Table, L1 Table中只有3個(gè)entry,每個(gè)entry管理者1G內(nèi)存,也就是一共管理了3G內(nèi)存的頁(yè)表,一一映射。如下圖所示:就是一張這樣的表
示例代碼如下:(吊不吊?,反正不是我寫的)
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注意:一般操作系統(tǒng)或軟件的實(shí)現(xiàn)本例這樣的場(chǎng)景時(shí),會(huì)在運(yùn)行時(shí)給L1 Table的其它entry賦0,而不像本例在編譯的時(shí)候,就定義了初始化位0的數(shù)組。
那么上述示例中的TT_S1_DEVICE_nGnRnE、TT_S1_NORMAL_WBWA、TT_S1_DEVICE_nGnRnE又是什么鬼? 其實(shí)就是定義了3個(gè)內(nèi)存屬性模板,每個(gè)模板都是Upper Attributes + Lower Attributes的集合
內(nèi)存屬性相關(guān)比特位的解釋如下:
- Indx = b01, take Type information from entry [1] in the MAIR
- NS = b0, output physical addresses are Secure
- AP = b00, address is readable and writeable
- SH = b00, Non-shareable
- AF = b1, Access Flag is pre-set. No Access Flag Fault is generated on access
- nG = Not used at EL3
- Contig = b0, the entry is not part of a contiguous block
- PXN = b0, block is executable. This attribute is called XN at EL3.
- UXN = Not used at EL3
(5)、Enable the MMU
經(jīng)歷前面的4個(gè)步驟,是不是覺(jué)得已經(jīng)大功告成了? 沒(méi)錯(cuò),基本完事了,還差最后一步,打開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 ISBMMU就這樣被打開,至此一個(gè)構(gòu)建開啟MMU構(gòu)建頁(yè)表的完整示例就介紹完了,是不是覺(jué)得非常遺憾? 感覺(jué)學(xué)到了什么,又好像沒(méi)學(xué)到什么。沒(méi)事慢慢來(lái),后面還有。
3、構(gòu)建頁(yè)表示例 – 稍微復(fù)雜一點(diǎn)場(chǎng)景Multiple levels of table at EL3
由于很多步驟在上一節(jié)都已經(jīng)講述過(guò)了,所以本節(jié)講述的僅僅是創(chuàng)建頁(yè)表 (Generate the translation tables)
- 設(shè)置頁(yè)表基地址VBAR_EL3 (Specify the location of the translation table)
- 初始化MAIR_EL3 (Memory Attribute Indirection Register)
- 配置TCR_EL3 (Configure the translation regime)
- 創(chuàng)建頁(yè)表 (Generate the translation tables)
- Enable the MMU
先構(gòu)建L1 table,在L1 table中只讓 3個(gè)entries有效。其中:
- entry0 : 指向block,即0x0000,0000 - 0x3FFF,FFFF
- entry1 : 指向block,即0x4000,0000 - 0x7FFF,FFFF
- entry2 : 指向L2 table,控制著0x8000,0000 - 0xBFFF,FFFF地址空間
其實(shí)我們就是想建立一張下圖所示的表,entry0 和entry1還是依然指向block內(nèi)存,entry2則指向了二級(jí)頁(yè)表
如下示例代碼,便是L1 Table的實(shí)現(xiàn)
如下示例代碼,便是L2 Table的實(shí)現(xiàn)
// // 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的實(shí)現(xiàn)4、構(gòu)建頁(yè)表示例 – 繼續(xù)復(fù)雜一點(diǎn)場(chǎng)景Single-level table at EL1
在上面兩個(gè)小節(jié)已經(jīng)講述了Single-level table 、Multi-level table 的配置和映射場(chǎng)景,當(dāng)然都是發(fā)生在EL3的。 本節(jié)再來(lái)繼續(xù)看一下如果是EL1呢?
配置和建立頁(yè)表需要做哪些事情,由于在前面都已經(jīng)講述過(guò)了,本節(jié)就不在重復(fù)描述了,我們來(lái)看看EL1和EL3不一樣的地方。
- 設(shè)置頁(yè)表基地址VBAR_EL3 (Specify the location of the translation table)
- 初始化MAIR_EL3 (Memory Attribute Indirection Register)
- 配置TCR_EL3 (Configure the translation regime)
- 創(chuàng)建頁(yè)表 (Generate the translation tables)
- Enable the MMU
For NS.EL1:
- Configure SCR_EL3
- Configure HCR_EL2
(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總結(jié)
以上是生活随笔為你收集整理的11-Memory Management Examples的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 编写TA链接静态库的方法
- 下一篇: 12-Generic Timer