Page 60 - 《软件学报》2025年第9期
P. 60
李传东 等: RISC-V 架构下的懒惰影子页表模型 3971
4
(Michigan Technological University, Houghton 49931-1295, USA)
Abstract: Memory virtualization, a core component of virtualization technology, directly impacts the overall performance of virtual
machines. Current memory virtualization approaches often involve a tradeoff between the overhead of two-dimensional address translation
and page table synchronization. Traditional shadow paging employs an additional software-maintained page table to achieve address
translation performance comparable to native systems. However, synchronization of shadow page tables relies on write protection,
frequently causing VM-exits that significantly degrade system performance. In contrast, the nested paging approach leverages hardware-
assisted virtualization, allowing the guest page table and nested page table to be directly loaded into the MMU. While this eliminates page
table synchronization, the two-dimensional page table traversal will seriously degrade the address translation performance. Two-dimensional
page table traversal incurs substantial performance penalties for address translation due to privilege overhead. This study proposes lazy
shadow paging (LSP), which reduces page table synchronization overhead while retaining the high efficiency of shadow page tables.
Leveraging the privilege model and hardware features of the RISC-V architecture, LSP analyzes the access patterns of guest OS page
tables and binds synchronization with translation lookaside buffer (TLB) flushes, reducing the software overhead associated with page table
updates by deferring costs until the first access to a relevant page to minimize VM-exits. In addition, it introduces a fast path for handling
VM-exits, exploiting the fine-grained TLB interception and privilege-level features of RISC-V to further optimize performance.
Experimental results demonstrate that under the baseline RISC-V architecture, LSP reduces VM-exits by up to 50% compared to traditional
shadow paging in micro-benchmark tests. For typical applications in the SPEC2006 benchmark suite, LSP reduces VM-exits by up to 25%
compared to traditional shadow paging and decreases memory accesses per TLB miss by 12 compared to nested paging.
Key words: RISC-V; hypervisor-extension; memory virtualization; shadow page table; nested page table
虚拟化技术作为云计算的核心支柱, 被广泛应用于云服务提供商, 用于管理云资源, 确保资源的共享、安全性
和隔离性 [1−3] , 为服务器无感知计算 (serverless) [4,5] 等新兴应用提供了基础设施支持和安全保证. 在虚拟化技术中,
内存虚拟化是最为关键的组成部分之一, 直接影响着虚拟机的访存性能和安全性. 一种高效的内存虚拟化方法, 能
够提供与原生 (native) 环境相媲美的内存访问性能, 并且不产生额外的虚拟化开销, 最终提高虚拟机的性能. 然而,
尽管存在各种软件和硬件加速方案 [6−8] , 内存虚拟化的开销仍然不可忽视 [9] .
内存虚拟化技术使用两维地址映射为虚拟机提供隔离和完备的内存空间: 1) 从客户机虚拟地址 (guest virtual
address, GVA) 到客户机物理地址 (guest physical address, GPA) 的映射; 2) 从 GPA 到宿主机物理地址 (host
physical address, HPA) 的映射. 通常第 1 维地址映射由进程页表完成, 而对第 2 维地址的映射管理各不相同. 当前
主流的内存虚拟化方法分为硬件辅助虚拟化和软件虚拟化. 首先, 最常用的硬件辅助虚拟化方法被称为嵌套页表
模型 (nested paging) , 其直接使用页表结构维护第 2 维地址映射, 称为嵌套页表 (nested page table, NPT). 在嵌套
[2]
页表模型下, 进程页表和 NPT 能够直接被装载到内存管理单元 (memory management unit, MMU) 中. 当发生转址
旁路缓存 (translation lookaside buffer, TLB) 缺失时, MMU 将在这两套页表中进行高延迟两维页表遍历. 而在没有
硬件辅助虚拟化的场景下, 最常用的内存虚拟化方法被称为影子页表模型 (shadow paging) [10] . 该方法采用物理地
址翻译表维护第 2 维地址映射, 并且从进程页表和物理地址翻译表中构建一个映射 GVA 和 HPA 的影子页表
(shadow page table). MMU 直接装载影子页表进行地址翻译, 因此 TLB 缺失后将进行与原生场景相同的一维页表
遍历.
嵌套页表模型和影子页表模型需要在地址翻译开销和页表同步开销中进行权衡, 但它们总是无法在两者间达
到最优 [11] . 如图 1 所示, 当启用嵌套页表模型时, 在 TLB 缺失后, GVA 需要首先通过遍历进程页表转化为 GPA,
然后再遍历嵌套页表转化为 HPA 进行访存. 虽然该二维页表遍历由硬件实现, 但是其仍然会造成不可忽视的开
销. 例如, 在 RISC-V 架构中最常用的 Sv48 页表模型和 Sv39 页表模型下, TLB 缺失导致的页表遍历将分别造成
24 次和 15 次访存, 而在原生场景仅需要 4 次和 3 次访存. 不过, 嵌套页表模型受益于进程页表的直接更新, 无需
虚拟机监视器 (hypervisor 或者 virtual machine monitor, VMM) 的介入. 具体而言, 虚拟机操作系统完全持有被装载
到页表基地址中的进程页表的所有权, 对于页表页的修改和页表基地址的装载都与原生环境一致. 而第 2 维页表,
即嵌套页表, 保证了虚拟机的任意地址访存不会在宿主机物理地址空间中越界, 从而维护了安全性 [3] . 当使用影子
页表模型时, GVA 能够直接通过影子页表转化为 HPA, 产生的地址翻译开销和原生环境一致. 然而, 为了保证影
子页表与进程页表和嵌套页表映射的一致性, 当虚拟机更新进程页表时需要 hypervisor 介入以进行复杂的页表同

