在噩梦中惊醒,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中继续写点代码了。

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