深度学习arm MMU一篇就够了
快速鏈接:
.
👉👉👉 個人博客筆記導讀目錄(全部) 👈👈👈
目錄
- 1、MMU概念介紹
- 2、虛擬地址空間和物理地址空間
- 2.1、(虛擬/物理)地址空間的范圍
- 2.2、物理地址空間有效位(范圍)
- 2.2.1、頁表翻譯相關寄存器的配置(TCR)
- 3、Translation regimes
- 4、地址翻譯/幾級頁表?
- 4.1、思考:頁表到底有幾級?
- 4.2、以4KB granule為例,頁表的組成方式
- 4.3、optee實際使用的示例
- 5、頁表格式(Descriptor format)
- 5.1、ARMV8支持的3種頁表格式
- 5.2、AArch64 Long Descriptor支持的四種entry
- 5.3、頁表的屬性位介紹( Block Descriptor/Page Descriptor )
- 5.4、mair寄存器的定義
- 6、TCR寄存器介紹
- 7、地址翻譯指令介紹
- 8、地址翻譯相關的系統寄存器總結
思考
為什么要用虛擬地址?為什么要用MMU?
MMU硬件完成了地址翻譯,我們軟件還需要做什么?
MMU在哪里?MMU和SMMU是什么關系?
1、MMU概念介紹
MMU分為兩個部分: TLB maintenance 和 address translation
MMU的作用,主要是完成地址的翻譯,即虛擬地址到物理地址的轉換,無論是main-memory地址(DDR地址),還是IO地址(設備device地址),在開啟了MMU的系統中,CPU發起的指令讀取、數據讀寫都是虛擬地址,在ARM Core內部,會先經過MMU將該虛擬地址自動轉換成物理地址,然后在將物理地址發送到AXI總線上,完成真正的物理內存、物理設備的讀寫訪問.
那么為什么要用MMU?為什么要用虛擬地址? 以下總結了三點:
- 多個程序獨立執行 — 不需要知道具體物理地址
- 虛擬地址是連續的 — 程序可以在多個分段的物理內存運行
- 允許操作系統管理內存 — 哪些是可見的,哪些是允許讀寫的,哪些是cacheable的……
既然MMU開啟后,硬件會自動的將虛擬地址轉換成物理地址,那么還需要我們軟件做什么事情呢? 即創建一個頁表翻譯都需要做哪些事情呢? 或者說啟用一個MMU需要軟件做什么事情呢?
- 設置頁表基地址VBAR_EL3 (Specify the location of the translation table)
- 初始化MAIR_EL3 (Memory Attribute Indirection Register)
- 配置TCR_EL3 (Configure the translation regime)
- 創建頁表 (Generate the translation tables)
- Enable the MMU
2、虛擬地址空間和物理地址空間
2.1、(虛擬/物理)地址空間的范圍
內核虛擬地址空間的范圍是什么?應用程序的虛擬地址空間的范圍是什么?
以前我們在學習操作系統時,最常看到的一句話是:內核的虛擬地址空間范圍是3G-4G地址空間,應用程序的虛擬地址空間的范圍是0-3G地址空間; 到了aarch64上,則為 : 內核的虛擬地址空間是0xffff_0000_0000_0000 - 0xffff_ffff_ffff_ffff , 應用程序的虛擬地址空間是: 0x0000_0000_0000_0000 - 0x0000_ffff_ffff_ffff.
做為一名杠精,必需告訴你這句話是錯誤的。錯誤主要有兩點:
(1) arm處理器,并沒有規定你的內核必需要使用哪套地址空間,以上這是Linux Kernel自己的設計,它設計了讓Linux Kernel使用0xffff_0000_0000_0000 - 0xffff_ffff_ffff_ffff地址區間,這里正好可以舉一個反例,比如optee os,它的kernel mode和user mode使用的都是高位的虛擬地址空間。
(2) 高位是有幾個F(幾個1)是根據你操作系統使用的有效虛擬地址位來決定的,也并非固定的。比如optee中的mode和user mode的虛擬地址空間范圍都是: 0x0000_0000_0000_0000 - 0x0000_0000_ffff_ffff
其實arm文檔中有一句標準的描述 :
高位是1的虛擬地址空間,使用TTBR1_ELx基地址寄存器進行頁表翻譯;高位是0的虛擬地址空間,使用TTBR0_ELx基地址寄存器頁表翻譯。 所以不應該說,因為你使用了哪個寄存器(TTBR0/TTBR1),然后決定了你使用的哪套虛擬地址空間;應該說,你操作系統(或軟件)使用了哪套虛擬地址空間,決定了使用哪個哪個基地址寄存器(TTBR0/TTBR1)進行翻譯。
As Figure shows, for 48-bit VAs:
? The address range translated using TTBR0_ELx is 0x0000000000000000 to 0x0000FFFFFFFFFFFF.
? The address range translated using TTBR1_ELx is 0xFFFF000000000000 to 0xFFFFFFFFFFFFFFFF.
In an implementation that includes ARMv8.2-LVA and is using Secure EL3 the 64KB translation granule, for 52-bit VAs:
? The address range translated using TTBR0_ELx is 0x0000000000000000 to 0x000FFFFFFFFFFFFF.
? The address range translated using TTBR1_ELx is 0xFFF0000000000000 to 0xFFFFFFFFFFFFFFFF.
Which TTBR_ELx is used depends only on the VA presented for translation. The most significant bits of the VA must all be the same value and:
? If the most significant bits of the VA are zero, then TTBR0_ELx is used.
? If the most significant bits of the VA are one, then TTBR1_ELx is used.
2.2、物理地址空間有效位(范圍)
具體每一個core的物理地址是多少位,其實都是定死的,虛擬地址是多少位,是編譯或開發的時候根據自己的需要自己配置的。如下表格摘出了部分arm core的物理地址有效位,所以你具體使用多少有效位的物理地址,可以查詢core TRM手冊。
2.2.1、頁表翻譯相關寄存器的配置(TCR)
- ID_AA64MMFR0_EL1.PARange : Physical address size : 讀取arm寄存器,得到當前系統支持的有效物理地址是多少位
- TCR_EL1.IPS : Output address size : 告訴mmu,你需要給我輸出多少位的物理地址
- TCR_EL1.T0SZ和TCR_EL1.T1SZ : Input address size : 告訴mmu,我輸入的是多數為的虛擬地址
3、Translation regimes
內存管理單元 (MMU) 執行地址翻譯。MMU 包含以下內容:
- The table walk unit : 它從內存中讀取頁表,并完成地址轉換
- Translation Lookaside Buffers (TLBs) : 緩存,相當于cache
軟件看到的所有內存地址都是虛擬的。 這些內存地址被傳遞到 MMU,它檢查最近使用的緩存轉換的 TLB。 如果 MMU 沒有找到最近緩存的翻譯,表遍歷單元從內存中讀取適當的一個或多個表條目,如下所示:
Translation tables 的工作原理是將虛擬地址空間劃分為大小相等的塊,并在表中為每個塊提供一個entry。
Translation tables 中的entry 0 提供block 0 的映射,entry 1 提供block 1 的映射,依此類推。 每個條目都包含相應物理內存塊的地址以及訪問物理地址時要使用的屬性。
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
Secure and Non-secure addresses
在REE(linux)和TEE(optee)雙系統的環境下,可同時開啟兩個系統的MMU.
在secure和non-secure中使用不同的頁表.secure的頁表可以映射non-secure的內存,而non-secure的頁表不能去映射secure的內存,否則在轉換時會發生錯誤
Two Stage Translations
4、地址翻譯/幾級頁表?
4.1、思考:頁表到底有幾級?
4.2、以4KB granule為例,頁表的組成方式
- 除了第一級index(這里是leve 0 table中的index),每一個查找table/page的index都是9個bit,也就是說除了第一級頁表,后面的每一級table都是有512個offset
- 如果VA_BIT = 39,那么leve 0 table用BIT[38:39]表示,只有1個offset
- 如果VA_BIT = 48,那么leve 0 table用BIT[47:39]表示,有512個offset
- 如果VA_BIT > 48,那是不存在的,因為arm規定,大于48的,只有一個,那就是VA_BIT=52,并且規定該情況下的最小granue size=64KB,而我們這里講述的是granue size=4KB的情況
- 如果VA_BIT = 32,那么leve 0 table就不用了,TTBR_ELx指向Level 1 table
- 另外我們還需注意一點,在Level 0 table中,他只能指向D_Table,不能指向D_Block
4.3、optee實際使用的示例
32位有效虛擬地址、32位有效物理地址,3級頁表查詢(L1、L2、L3),顆粒的位4KB
5、頁表格式(Descriptor format)
5.1、ARMV8支持的3種頁表格式
- AArch64 Long Descriptor : 我們只學習這個
- Armv7-A Long Descriptor : for Large Physical Address Extension (LPAE)
- Armv7-A Short Descriptor
Armv8-A supports three different sets of translation table format:
? The Armv8-A AArch64 Long Descriptor format.
? The Armv7-A Long Descriptor format such as the Large Physical Address Extension (LPAE) to the Armv7-A architecture, for example, the Arm Cortex-A15 processor.
? The Armv7-A Short Descriptor format.
5.2、AArch64 Long Descriptor支持的四種entry
對于AArch64 Long Descriptor,又分為下面四種entry:
- 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
注意:entry[1:0] 表示該entry屬于哪類entry, Block Descriptor和Page Descriptor是一個意思。在當前架構中,reserved也是invalid。
5.3、頁表的屬性位介紹( Block Descriptor/Page Descriptor )
內存屬性相關比特位的解釋如下:
- 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.4、mair寄存器的定義
6、TCR寄存器介紹
在ARM Core中(aarch64),有三個Translation Control Register 寄存器:
- TCR_EL1
- TCR_EL2
- TCR_EL3
| ORGN1、IRGN1、ORGN0、IRGN0 | cacheable屬性 | outer/inner cableability的屬性(如直寫模式、回寫模式) |
| SH1、SH0 | shareable屬性 | cache的共享屬性配置(如non-shareable, outer/inner shareable) |
| TG0/TG1 | Granule size | Granule size(其實就是頁面的大小,4k/16k/64k) |
| IPS | 物理地址size | 物理地址size,如32bit/36bit/40bit |
| EPD1、EPD0 | - | TTBR_EL1/TTBR_EL0的enable和disable |
| TBI1、TBI0 | - | top addr是ignore,還是用于MTE的計算 |
| A1 | - | ASID的選擇,是使用TTBR_EL1中的,還是使用TTBR_EL0中的 |
| AS | - | ASID是使用8bit,還是使用16bit |
7、地址翻譯指令介紹
address translation的指令大約14個:
總結一下:
8、地址翻譯相關的系統寄存器總結
地址轉換由系統寄存器的組合控制:
(1)、SCTLR_ELx
- M - Enable Memory Management Unit (MMU).
- C - Enable for data and unified caches.
- EE - Endianness of translation table walks.
(2)、TTBR0_ELx and TTBR1_ELx
- BADDR - Physical address (PA) (or intermediate physical address, IPA, for EL0/EL1) of start of translation table.
- ASID - The Address Space Identifier for Non-Global translations.
(3)、TCR_ELx
- PS/IPS - Size of PA or IPA space, the maximum output addresssize.
- TnSZ - Size of address space covered by table.
- TGn - Granule size.
- SH/IRGN/ORGN - Cacheability and shareability to be used by MMU table walks.
- TBIn - Disabling of table walks to a specific table.
(4)、MAIR_ELx
- Attr - Controls the Type and cacheability in Stage 1 tables.
總結
以上是生活随笔為你收集整理的深度学习arm MMU一篇就够了的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: csdn自定义模块backup
- 下一篇: optee3.16.0 qemu_v8的