rust-vmm 学习(二)

eventfd

virtio中,guest和vhost通过evnetfd通知对方,见(Virtio ring and virtio-net)。

REF:

Qemu-kvm的ioeventfd创建与触发的大致流程

virtio的eventfd机制浅析 ( ioctl(KVM_IOEVENTFD, struct kvm_ioeventfd)  )

QEMU下的EVENTFD机制及源代码分析 (huatong写的,还写了很多不错的文章

代码分析(Rng为例)

1. Rng的activate会启动一个thread,run RngEpollHandler 

queue_evts 会通过activate 传给 RngEpollHandler

2. RngEpollHandler run, 实现了epool QUEUE_AVAIL_EVENT 和 KILL_EVENT, 进行处理。

queue处理完成后,还会通知signal_used_queue

3. write_bar的时候会 activate

4. rust-vmm/kvm-ioctls 的VmFd通过register_ioevent注册event

/// Registers an event to be signaled whenever a certain address is written to.
///
/// See the documentation for `KVM_IOEVENTFD`.

5.  ioeventfds 会遍历queue_evts

6. vm初始化,add_virtio_pci_device register_ioevent

irqfd

VHOST通过irqfd通知guest。irqfd需要绑定一个eventfd.

rust-vmm/kvm-ioctls 的VmFd通过register_irqfd注册irqfd

REF:

KVM VHOST中irqfd的使用

关于Linux虚拟化技术KVM的科普 科普二(KVM虚拟机代码揭秘)  (一共写了5个科普)

KVM中断虚拟化主要依赖于VT-x技术,VT-x主要提供了两种中断事件机制,分别是中断退出>
和中断注入。

中断退出:指虚拟机发生中断时,主动式的客户机发生VM-Exit,这样能够在主机中实现对>
客户机中断的注入。

中断注入:是指将中断写入VMCS对应的中断信息位,来实现中断的注入,当中断完成后通过
读取中断的返回信息来分析中断是否正确。

中断注入的标志性函数kvm_set_irq,是中断注入的最开始。

Qemu-kvm模拟APIC Timer中断

qemu-kvm 中斷虛擬化代碼分析

qemu-kvm 中断虚拟化 (qemu-kvm 中断虚拟化)

KVM中断虚拟化浅析

kvm 中断以及io虚拟化

KVM 中断系统结构关系

qemu-kvm 中断虚拟化代码分析  

代码分析(Rng为例子)  

1. VirtioPciDevice 有个 interrupt_cb   field

2. assign_pin_irq 和 assign_msix 用来设置interrupt_cb 

assign_msix 注入中断 (msi_cb)(InterruptParameters { msix: Some(entry) })

// In case the vector control register associated with the entry
// has its first bit set, this means the vector is masked and the
// device should not inject the interrupt.
// Instead, the Pending Bit Array table is updated to reflect there
// is a pending interrupt for this specific vector.

3. vm初始化,add_virtio_pci_device 是会 assign_pin_irq 和 assign_msix ,register_irqfd

4. msi_cb 和 irq_cb 的参数都是 InterruptParameters 类型,就是一个MsixTableEntry

signal_msi返回msi_cb

        irq_cb会调用service_irq来注入中断

service_irq 调用rust-vmm/kvm-ioctls 的VmFd的signal_msi方法注入中断

5. MsixConfig 实现了 read_table 和 write_table

MsixConfig包括table_entries, pba_entries, interrupt_cb和masked字段。

write_table会调用inject_msix_and_clear_pba注入中断

5.1 write_config_register 也会调用 set_msg_ctl 注入中断

6.  VirtioPciDevice new的时候会new msix_config 

7.  read_bar的时候会read_table, write_bar的时候会write_table

8. 对于VFIO来说,会设置中断路由, update_msi_interrupt_routes(调用了set_kvm_routes)

Interrupt.update_msi -> VfioMsix.update  -> set_msg_ctl -> inject_msix_and_clear_pba

vmm-sys-util   

rust-vmm 的 vmm-sys-util 实现了很多utilities, 包括eventfd,ioctrlpollsignal.

实现了eventfd的new, write, read, clone等基本操作。

https://github.com/rust-vmm/vmm-sys-util/tree/master/src

FYI:

PBA (Pending Bit Array)

MSI-HOWTO.txt

原文地址:https://www.cnblogs.com/shaohef/p/11266448.html