Qemu-KVM:将来宾物理地址转换为主机虚拟/主机物理地址 [英] Qemu-KVM: Translation of guest physical address to host virtual/host physical address

查看:96
本文介绍了Qemu-KVM:将来宾物理地址转换为主机虚拟/主机物理地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个项目中,我需要转换 qemu-guest 物理地址以托管虚拟/物理地址.

I am working on a project where I need to translate qemu-guest physical addresses to host virtual/physical addresses.

我正在使用VMI(虚拟机自检)自检qemu进程(KVM VM)并读取存储在virtio环形缓冲区描述符中的来宾物理地址.因此,我正在寻找一种简单的方法来将qemu物理地址转换为主机端的主机虚拟地址.(即从qemu流程中提取尽可能少的信息).

I am using VMI (virtual machine introspection) to introspect into the qemu process (the KVM VM) and to read guest physical addresses stored in virtio ring buffer descriptors. Therefore, I am looking for a simple way to translate the qemu physical addresses to host virtual addresses at the host side. (i.e., to extract as less info as possible from the qemu process).

我在线阅读了以前的版本,qemu将物理RAM库存储在变量 phys_ram_base 中,以便可以按以下方式获取主机虚拟地址:

I read online that in previous versions, qemu stored the physical RAM base in the variable phys_ram_base, so that the host virtual address could be obtained as follows:

host_virtual = phys_ram_base + guest_physical_address

在较新版本的qemu中是否可能发生这种情况(例如,如何获取qemu物理基址-以前的 phys_ram_base ?)

Is something like this possible in newer versions of qemu (e.g., how could I obtain the qemu-physical base address -- the former phys_ram_base?)

推荐答案

我必须解决相同的问题,并提出以下解决方案.

I had to solve the same problem and I come up with the following solution.

将QEMU与 -enable-kvm 选项一起使用时,将通过 KVM_SET_USER_MEMORY_REGION ioctl将内存分配给来宾.基本上,QEMU准备一个 kvm_userspace_memory_region 结构,其中将来宾的物理地址与主机虚拟地址相关联,然后发出ioctl.现在,事实证明,与KVM API提供的结构相比, KVMSlot 结构(几乎)是1:1.QEMU存储所有信息,以执行从访客物理地址到主机虚拟地址的转换.

When using QEMU with the -enable-kvm option, memory is allocated to the guest through the KVM_SET_USER_MEMORY_REGION ioctl. Basically, QEMU prepares a kvm_userspace_memory_regionstruct, where the physical addresses of the guest are associated to host virtual addresses, and then the ioctl is issued. Now, it turned out that the KVMSlot struct is (almost) 1:1 with the struct offered by the KVM API. QEMU stores all the information to perform the translation from guest physical to host virtual addresses there.

KVMSlot结构的定义如下:

The KVMSlot struct is defined like this:

    typedef struct KVMSlot
    {
        hwaddr start_addr;
        ram_addr_t memory_size;
        void *ram;
        int slot;
        int flags;
        int old_flags;
        /* Dirty bitmap cache for the slot */
        unsigned long *dirty_bmap;
    } KVMSlot;

start_addr 是与所考虑的插槽的begininnig对应的物理地址, ram 是其对应的主机虚拟地址,然后 memory_size 是插槽的大小.

start_addr is the physical address corresponding to the begininnig of the considered slot, ram is its corresponding host virtual address and then memory_size is the size of the slot.

现在要执行翻译,您必须:

Now to perform the translation you have to:

  1. 找到正确的插槽.可以分配许多插槽,并将它们保存在 KVMSlot 元素列表中.列表的开头存储在 KVMMemoryListener 中.要找到它,您可以检查来宾的物理地址是否在 start_addr start_addr + memory_size 之间.
  2. 计算您的来宾物理地址在插槽中的偏移量( offset = gpa-start_addr )
  3. 计算转换后的主机虚拟地址,例如 hva = ram + offset .来宾物理地址和主机虚拟地址中的偏移量当然都是相同的,这就是为什么可以使用它.
  1. Find the right slot. Many slots can be allocated and they are kept in a list of KVMSlot elements. The head of the list is stored in KVMMemoryListener. To find it, you can check if the guest physical address is in the range between start_addr and start_addr + memory_size.
  2. Compute the offset of your guest physical address in the slot (offset = gpa - start_addr)
  3. Compute the translated host virtual address like hva = ram + offset. The offset of course is the same both in the guest physical addresses and in the host virtual addresses, that's why you can use it.

最后,您可以使用QEMU Monitor的功能 gpa2hva 来检查翻译是否正确.

Finally you can check that the translation was right using the function gpa2hva of the QEMU Monitor.

这篇关于Qemu-KVM:将来宾物理地址转换为主机虚拟/主机物理地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆