VirtualMachine 的第一條指令是如何獲取的(在 KVM-QEMU 中) (How is the first instruction of VirtualMachine is fetched (in KVM-QEMU))


問題描述

VirtualMachine 的第一條指令是如何獲取的(在 KVM‑QEMU 中) (How is the first instruction of VirtualMachine is fetched (in KVM‑QEMU))

我是 SO 和 X86 VMX 的新手。我在 X86 上學習 KVM‑QEMU,我想知道如何獲取 VM 的第一條指令的詳細信息,以便 VM 可以開始運行。有 KVM API 可以配置和註冊一組內存作為 VM 的物理內存,然後將 guest_RIP 設置為 AAA(例如)。我不知道何時調用 VMLaunch(使用正確配置的 VMCS),CPU 如何從 VMCS 中的 RIP 獲取指令,是否通過一些地址轉換過程,因此應正確設置 guest_CR3 以指向主機為客人分配的內存?謝謝


參考解法

方法 1:

I will explain this in the context of QEMU and how QEMU operates when the KVM accelerator is enabled.

As you might be knowing, under kvm, virtual machines are created by opening a device node /dev/kvm. A guest will have its own memory and is usually separate from the userspace process that created it. So basically kvm is structured as a fairly typical Linux character device ‑ you use ioctl()s to create, run, modify parameters, allocate memory and read and write to the VCPU registers of the virtual machines. Thus, the initial setup will be done via various ioctl()s that will setup KVM for further use.

In terms of the QEMU code, all execution(whether KVM or non‑KVM) starts from :

vl.c start of everything

The initialization of the KVM architecture happens via the below function ‑‑ (collecting CPU flags from CPUID and setting up frequencies etc.)

kvm_arch_init_vcpu

Once all the initialization functions are done, the function do_kvm_cpu_synchronize_post_init will try to synchronize the initial values of the VCPU registers based on the host CPU state. It calls another function, kvm_arch_put_registers and sets the VCPU to be dirty. Why is the VCPU set to dirty ? Only then will the subsequent functions actually initialize the values of the VCPU registers.

This function kvm_arch_put_registers is the key to obtaining all the initial values of the VMCS registers. If you see its body, you will realize what is happening :‑

kvm_arch_put_registers

Specifically focus on the functions, kvm_getput_regs and kvm_put_sregs ‑ the first function will set up the initial values of the GPRs and the EFLAGS as well as the EIP/RIP register, while the second function will set up the initial segment register values.

The guest page table will be rooted to the CR3 register. How does this page table work ?

For this, you need to remember that the mmu in KVM only accounts for one level of virtualization ( guest virtual ‑> guest physical ) but does not account for the second level ( guest physical ‑> host physical ). The initial RIP will account for virtual addresses ‑ it will be appropriately translated to physical addresses in the guest. However, to convert this guest physical address to the host physical address, you need to have a separate page table. This is a shadow page table that will be used in conjunction with the original page table (that converts guest virtual ‑> guest physical) to perform the entire translation.

There is a need for synchronizing the state of the guest page table with the shadow page table and this sometimes tends to be a problem. Whenever the guest will write to its page table, the corresponding changes need to be performed on the shadow page tables as well.

(by wangt13Arnabjyoti Kalita)

參考文件

  1. How is the first instruction of VirtualMachine is fetched (in KVM‑QEMU) (CC BY‑SA 2.5/3.0/4.0)

#virtualization #x86






相關問題

需要一個好的 Internet Explorer 6、7、8 獨立版 (Need a good Internet Explorer 6, 7, 8 standalone)

英特爾虛擬化問題 (Android Studio) (Intel Virtualization Problems (Android Studio))

VirtualMachine 的第一條指令是如何獲取的(在 KVM-QEMU 中) (How is the first instruction of VirtualMachine is fetched (in KVM-QEMU))

如何使用 SharePoint、K2 和域控制器複製虛擬 PC (How to duplicate a virtual PC with SharePoint, K2 and domain controller)

WPF數據虛擬化ListView (WPF Data virtualizing ListView)

製作linux系統的“副本” (Make a "copy" of linux system)

在虛擬機中模擬 Windows 筆記本電腦? (Simulating windows laptop in virtual machine?)

為“假 Mac OS X”虛擬化構建 Windows XP 或 7 (Skeletoning Windows XP or 7 for "fake Mac OS X" virtualization)

在虛擬機中開發 Java 有什麼好處嗎? (Are there any benefits to developing Java in a virtual machine?)

Xen ABI 是真正的 ABI 嗎? (is the Xen ABI a true ABI?)

在使用 qemu kvm 安裝 VM 之前更改 VM 的磁盤大小 (Changing Disk Size of VM before VM installation using qemu kvm)

Qemu 在 Windows 上運行時無法加載 bios-256k.bin (Qemu can't load bios-256k.bin when running on Windows)







留言討論