11-Memory Management Examples
快速鏈接:
.
👉👉👉 個人博客筆記導讀目錄(全部) 👈👈👈
相關鏈接: (專題:《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、簡介
- 術語介紹
- Translation regimes
- 2、構建頁表示例 -- 一個簡單場景Single-level table at EL3
- (1)、設置頁表基地址VBAR_EL3
- (2)、初始化MAIR_EL3
- (3)、配置TCR_EL3 (Translation Control Register)
- (4)、創(chuàng)建頁表
- (5)、Enable the MMU
- 3、構建頁表示例 -- 稍微復雜一點場景Multiple levels of table at EL3
- 4、構建頁表示例 -- 繼續(xù)復雜一點場景Single-level table at EL1
- (1)、Configure SCR_EL3
- (2)、Configure HCR_EL2
今天我們進行幾個示例,怎樣去配置MMU并建立頁表,加深對VMSA/MMU/CACHE等的理解。
1、簡介
在學習之前我們先回顧一些概念
術語介紹
- (1)、flat map : 一一映射,也就是虛擬地址=物理地址,官方說法是This means that the input virtual address and output physical address are the same for all translations,此時MMU作用其實就是控制內存屬性和權限
(2)、full level 1 table : 一個完整的 L1 頁表
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、構建頁表示例 – 一個簡單場景Single-level table at EL3
構建頁表都需要做哪些事情呢?
- 設置頁表基地址VBAR_EL3 (Specify the location of the translation table)
- 初始化MAIR_EL3 (Memory Attribute Indirection Register)
- 配置TCR_EL3 (Configure the translation regime)
- 創(chuàng)建頁表 (Generate the translation tables)
- Enable the MMU
(1)、設置頁表基地址VBAR_EL3
設置 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必需是對齊的, 如我們示例中,有一個完整的L1頁表,4KB的granule,一個完整的L1頁表包含512個entries,每個entries是8bytes,所以一個完整的L1頁表就是4KB, 所以tt_l1_base必需4KB對齊的
tt_l1_base 可以是定義在section段的一個全局變量,初始化全0,注意全0的entry恰好fault entry
section TT,"ax" .align 12 .global tt_l1_base tt_l1_base: .fill 4096 , 1 , 0那么fault entry又是什么呢?
請參考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 又是什么呢??沒完沒了了是不,descriptor 可以理解成就是 entry.
(2)、初始化MAIR_EL3
我們先看以下MAIR_EL3寄存器的用法,64位的MAIR_EL3寄存器被分成了8個Attr,每一個Attr可以表示一類內存屬性
每一個Attr的配置如下:
對于Device Memory的配置,dd是啥含義呢?
對于Normal Memory的配置,oooo和iiii是啥含義呢?
那么RW又是啥含義呢?
好了,寄存器知識復習完了,接下來我們來看實列
如下所示,設置了3個index(3個Attr),分別是:
- index0 (Attr0) : 0x01000100 = Normal, Inner/Outer Non-Cacheable
- index1 (Attr1) : 0x11111111 = Normal, Inner/Outer WB/WA/RA
- index2 (Attr2) : 0x00000000 = Device-nGnRnE
設置完后,留著,后有大用!!
(3)、配置TCR_EL3 (Translation Control Register)
非常復雜的一個系統(tǒng)寄存器,不過還好,哥已經(jīng)給你整理出了一篇文檔,專門介紹TCR,參見《https://blog.csdn.net/weixin_42135087/article/details/112462416》, 本文就不再詳細介紹這些比特位了。
然后我們就可以看示例代碼了,牛批不是吹,你看這一個小小寄存器干了了多少事情:
- 配置了虛擬地址有效位是 39 ,也就意味著translation starts at L1, Why ???
- 頁表所在內存的內存屬性,Inner WB/WA、Outer WB/WA、Inner Shareable
- ignore top bytes
- 4KB granule
- 輸出32位物理地址
我們再來看看,為什么 配置了虛擬地址有效位是 39 ,就是意味著translation starts at L1 ?
請參考ARMV8的官方文檔,這是address translation的格式要求,虛擬地址有效位是39,也就是BIT[38:0], 所以也就不存在Level 0 table了,TTBR_EL3直接執(zhí)行Level 1 Table。
然后我們再來看每一個Table或每一個entry能夠cover住的范圍:
- L0 table: 512GB per entry – 注意,本示例中,沒有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相關的寄存器后,一定要記得 Invalidate TLBs, 這里操作的是Secure EL3 translation regime, 所以只要invalid EL3的TLB即可:
// Invalidate TLBs // ---------------- TLBI ALLE3 DSB SY ISB(4)、創(chuàng)建頁表
我們創(chuàng)建一個啥樣子的頁表好呢?
TTBR0_EL3 指向 Level 1 Table, L1 Table中只有3個entry,每個entry管理者1G內存,也就是一共管理了3G內存的頁表,一一映射。如下圖所示:就是一張這樣的表
示例代碼如下:(吊不吊?,反正不是我寫的)
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)或軟件的實現(xiàn)本例這樣的場景時,會在運行時給L1 Table的其它entry賦0,而不像本例在編譯的時候,就定義了初始化位0的數(shù)組。
那么上述示例中的TT_S1_DEVICE_nGnRnE、TT_S1_NORMAL_WBWA、TT_S1_DEVICE_nGnRnE又是什么鬼? 其實就是定義了3個內存屬性模板,每個模板都是Upper Attributes + Lower Attributes的集合
內存屬性相關比特位的解釋如下:
- 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個步驟,是不是覺得已經(jīng)大功告成了? 沒錯,基本完事了,還差最后一步,打開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就這樣被打開,至此一個構建開啟MMU構建頁表的完整示例就介紹完了,是不是覺得非常遺憾? 感覺學到了什么,又好像沒學到什么。沒事慢慢來,后面還有。
3、構建頁表示例 – 稍微復雜一點場景Multiple levels of table at EL3
由于很多步驟在上一節(jié)都已經(jīng)講述過了,所以本節(jié)講述的僅僅是創(chuàng)建頁表 (Generate the translation tables)
- 設置頁表基地址VBAR_EL3 (Specify the location of the translation table)
- 初始化MAIR_EL3 (Memory Attribute Indirection Register)
- 配置TCR_EL3 (Configure the translation regime)
- 創(chuàng)建頁表 (Generate the translation tables)
- Enable the MMU
先構建L1 table,在L1 table中只讓 3個entries有效。其中:
- entry0 : 指向block,即0x0000,0000 - 0x3FFF,FFFF
- entry1 : 指向block,即0x4000,0000 - 0x7FFF,FFFF
- entry2 : 指向L2 table,控制著0x8000,0000 - 0xBFFF,FFFF地址空間
其實我們就是想建立一張下圖所示的表,entry0 和entry1還是依然指向block內存,entry2則指向了二級頁表
如下示例代碼,便是L1 Table的實現(xiàn)
如下示例代碼,便是L2 Table的實現(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的實現(xiàn)4、構建頁表示例 – 繼續(xù)復雜一點場景Single-level table at EL1
在上面兩個小節(jié)已經(jīng)講述了Single-level table 、Multi-level table 的配置和映射場景,當然都是發(fā)生在EL3的。 本節(jié)再來繼續(xù)看一下如果是EL1呢?
配置和建立頁表需要做哪些事情,由于在前面都已經(jīng)講述過了,本節(jié)就不在重復描述了,我們來看看EL1和EL3不一樣的地方。
- 設置頁表基地址VBAR_EL3 (Specify the location of the translation table)
- 初始化MAIR_EL3 (Memory Attribute Indirection Register)
- 配置TCR_EL3 (Configure the translation regime)
- 創(chuàng)建頁表 (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總結
以上是生活随笔為你收集整理的11-Memory Management Examples的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 编写TA链接静态库的方法
- 下一篇: 12-Generic Timer