ARMv7 KVM 在 linux中的实现 3 内存角度
生活随笔
收集整理的這篇文章主要介紹了
ARMv7 KVM 在 linux中的实现 3 内存角度
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
- x86的內存虛擬化支持
ARM內存地址轉換
為什么需要內存地址轉換實體機是這樣子跑的host_VA -> host_PA虛擬機是這樣跑guest_VA -> guest_PA //guest_PA 在 arm 手冊中被稱為 IPA ,在 kvm 代碼中也被稱為fault_ipa // guest_PA 與 host_VA 一一對應guest_PA -> host_PAARM提供了對虛擬物理內存的硬件支持。通過 二級地址轉換(遍歷二級頁表) 提供的 // 二級轉換可以在HYP模式中被禁止或使能。二級地址機制 的原理 (以虛擬機運行時為例)虛擬機管理的物理地址實際是中間物理地址(IPA),也被稱作客戶物理地址。// 客戶物理地址 需要被轉換成實際物理地址,即主機物理地址。ARM提供了 二級頁表,根據客戶物理地址將IPA轉換成PA二級頁表機制的限制與特點二級頁表使用ARM的新的LPAE頁表格式,與內核模式使用的頁表格式略有不同二級地址轉換只能在HYP模式下進行配置,它的使用對虛擬機時透明的。二級地址機制 的應用當實體機運行時二級地址轉換 是關閉的 // vm exit 時 關閉當虛擬機運行時二級地址轉換 是開啟的 // vm entry 時 開啟二級頁表的地址被寫入 基寄存器(VTTBR) // vm entry 時 寫入highvisor管理二級轉換頁表,只允許訪問專門為一個虛擬機配置的內存;其他的訪問操作將導致二級頁錯誤,該錯誤將被hypervisor捕獲。這種機制確保一個虛擬機不能訪問屬于hypervisor或其他虛擬機的內存和任何敏感數據。內存異常
TODO 缺頁異常 MMU異常 內存不夠怎么向host索取 虛擬機是一個進程,那么虛擬機內存的物理內存也是 malloc 申請出來的, 用到了 才會分配給虛擬機內存分配
KVM/ARM 利用現有的內核內存分配、頁引用計數和頁表操作的代碼。KVM/ARM通過判定IPA的錯誤來處理二級頁錯誤,如果出錯的地址屬于虛擬機內存映射中的正常內存,KVM/ARM只需調用現有的內核函數,如get_user_pages來為虛擬機分配一頁,同時將該分配的頁映射到虛擬機的二級頁表中。ARMv7 手冊
DDI0406C_D_armv7_AR_architecture_reference_manual.pdf 中 P1346 Stage 1 translationsNon-secure PL1&0 stage 1 translation // host linux 用戶態和 內核態(不包括hyp mode) 下的內存訪問// guest linux 用戶態 和 內核態的內存訪問(不包括hyp mode) // 現在不考慮嵌套虛擬化The stage 1 translation for memory accesses from Non-secure modes other than Hypmode. In an implementation that includes the Virtualization Extensions, this translatesa VA to an IPA, otherwise it translates a VA to a PA. For this translation:? Non-secure TTBR0 or TTBR1 holds the translation table base address.? Non-secure TTBCR determines which TTBR is used.? The input address range is up to 32 bits, as determined by either:— TTBCR.T0SZ or TTBCR.T1SZ, for a PL1&0 stage 1 translation.— HTCR.T0SZ, for a PL2 stage 1 translation.? The output address range is 40 bits.Non-secure PL2 stage 1 translation// host linux hyp mode 下的內存訪問The stage 1 translation for memory accesses from Hyp mode. Supported only if theimplementation includes the Virtualization Extensions, and translates a VA to a PA. Forthis translation, HTTBR holds the translation table base address.? The input address range is up to 32 bits, as determined by either:— TTBCR.T0SZ or TTBCR.T1SZ, for a PL1&0 stage 1 translation.— HTCR.T0SZ, for a PL2 stage 1 translation.? The output address range is 40 bits.Stage 2 translationNon-secure PL1&0 stage 2 translation // guest linux 用戶態 和 內核態的內存訪問The stage 2 translation for memory accesses from Non-secure modes other than Hypmode. Supported only if the implementation includes the Virtualization Extensions, andtranslates an IPA to a PA. For this translation:? The input address range is 40 bits, as determined by VTCR.T0SZ.? The output address range depends on the implemented memory system, and is upto 40 bits.? VTTBR holds the translation table base address.? VTCR specifies the required input address range, and whether the first lookup isat the first level or at the second level. 綜上host 下的內存訪問 有兩個頁表基址TTBR0或TTBR1 用于 內核態與用戶態下的內存訪問第一步,共一步 , 里面有 HVA -> HPA 的 映射關系HTTBR 用于 hyp mode 下的 內存訪問guest 下的內存訪問 有 2個 頁表基址TTBR0或TTBR1 用于 內核態與用戶態下的內存訪問第一步,共兩步 , 里面有 GVA -> GPA 的 映射關系VTTBR 用于 內核態與用戶態下的內存訪問第二步,共兩步 , 里面有 GPA -> HPA 的 映射關系---host linux 本身要維護TTBR0或TTBR1 及 內核 及各個進程的頁目錄表(頁表)如果添上虛擬機,則需要添加1. host 設置 虛擬機進程的 TTBR0或TTBR1 及 其中的 頁目錄表基址 和 相應的頁表 // 對應B2. host 設置 HTTBR 及 HTTBR 中的頁目錄表基址 和 相應的頁表 // 對應A3. host 設置 虛擬機進程所需要的 VTTBR 及 其中的 頁目錄表基址 和 相應的頁表 // 對應D4. guest 設置 自身用到 的 TTBR0或TTBR1 及 其中的 頁目錄表基址 和 相應的頁表 // 對應C- 內存訪存流程偽代碼
總結
以上是生活随笔為你收集整理的ARMv7 KVM 在 linux中的实现 3 内存角度的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 电饭锅鸿蒙系统,有了美的轻食电饭煲,人们
- 下一篇: 【Linux】一步一步学Linux——V