Hypervisor這個層次對TLB的使用與EL0/1比有限制嗎

我在利用虛擬化這篇文章中出現了一個錯誤,這是一個不起眼,但是細思恐極,再細思安心的錯誤,我有這樣一句話:

對於在EL2中,這條尤其重要,很多cortex都會有IPA-PA相對於VA-PA的縮水,所以EL2中的代碼要怎麼做也是需要思考的問題。

當然,現在我已經在這段話後面添加了一些解釋,表述了自己的錯誤,我之前想當然的認爲了:1階段頁表的虛擬地址是VA,對應EL1/0所用的地址,2階段頁表的虛擬地址是IPA,對應EL1看到的物理地址和EL2所使用的地址;這種理解是錯誤的。官方文檔中的這樣一段話拯救了我:

The hypervisor EL2 and Secure monitor EL3 have their own level 1 tables, which map directly from virtual to physical address space. The table base address is specified in TTBR0_EL2 and TTBR0_EL3 respectively, enabling a single contiguous address space of variable size at the bottom of memory. The TG field specifies the granule size and the SL0 field controls the first level of table lookup. Any access outside the defined address range causes a translation fault.

The Secure monitor EL3 also has its own dedicated translation tables. The table base address is specified in TTBR0_EL3 and configured via TCR_EL3. Translation tables are capable of accessing both Secure and Non-secure physical addresses. TTBR0_EL3 is used only in Secure monitor EL3 mode, not by the trusted kernel itself.

When the transition to Secure world has completed, the trusted kernel uses the EL1 translations, that is, the translation tables pointed to by TTBR0_EL1 and TTBR1_EL1. As these registers are not banked in AArch64, Secure monitor code must configure new tables for the Secure world and save and restore copies of TTBR0_EL1 or TTBR1_EL1.

The EL1 translation regime behaves differently in Secure state, compared to its normal operation in Non-secure state. The second stage of translation is disabled and the EL1 translation regime now points to both Secure and Non-secure physical addresses. There is no virtualization in the Secure world, so the IPA is always the same as the final PA.

Entries in the TLB are tagged as Secure or Non-secure, so that no TLB maintenance is ever required when moving between Secure and Normal worlds.

其實這段話論述的內容不是很稀奇,主要是說EL2和EL3都有其自己的頁表來映射虛擬地址空間到物理地址。然後頁表基地址放在TTBR0_EL2/3,使用虛擬地址低地址空間段。其他的我就不更深入翻譯了,因爲對於本文命題並無太多價值,僅提出一點就是:VA,也就是虛擬地址,是表示需要兩階段頁表翻譯才能得到PA的地址,還是表示程序代碼中使用的需要經過翻譯的地址?就是沒有分辨清楚這個問題,使我誤以爲EL2中使用的是IPA,而這一誤解,使我認爲TLB可能無法爲EL2所用,或是僅有少量可用。

這一理解是錯誤的,根本就在於,VA,虛擬地址,指的是程序代碼中需要經過翻譯使用的地址,重點在於是程序代碼中使用的地址,也就是說,在EL1/0中,這個虛擬地址需要兩階段頁表翻譯;而在EL2中的程序使用的也是虛擬地址,不過這個虛擬地址只需要1個階段的頁表翻譯,而這個階段的頁表翻譯與EL1/0的第2階段頁表翻譯在同一層次,但這並不妨礙這個虛擬地址應該稱作VA而不是IPA,雖然在同一層次

那麼IPA這個概念是怎麼搞得,怎麼搞出來的,IPA是爲了兩階段頁表翻譯的第2階段的地址而存在的,也就是它出現是表示一箇中間狀態,可以說EL1使用AT指令轉出一個“物理地址”其實是IPA,也可以說EL1/0的VA轉換到PA會存在中間過程的IPA,也可以說EL1向EL2發了hypercall給了一個IPA,但是我們不能說在EL2中運行的代碼在使用IPA,就算隨着hypercall發下來了IPA,並且這個IPA的物理地址在EL2的代碼所在的地址空間中已經被映射,並且在這個EL2地址空間中映射後的VA與這個IPA相同,但是不能說EL2用這個IPA幹活兒了。EL2能訪問這個IPA的地址是因爲EL2映射了這個IPA所在的地址空間下指向的物理,並且恰巧映射進來的VA值恰好與此IPA相同。

所以,在查完cortex-a55、cortex-a65、cortex-a75、cortext-a76、cortext-a77、neoverse_n1的手冊後不斷得到:

差點被這一句“Only Non-secure EL1 and EL0 stage 2 translations are cached.”嚇尿的我,終於安心了,可以安心地在EL2中繼續寫點代碼了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章