Skip to main content

arcbox_hypervisor/linux/vm/
drop.rs

1use crate::traits::VirtualMachine;
2
3use super::KvmVm;
4use super::virtio::VIRTIO_MMIO_QUEUE_NOTIFY;
5
6impl Drop for KvmVm {
7    fn drop(&mut self) {
8        // Stop VM if running.
9        if self.is_running() {
10            let _ = self.stop();
11        }
12
13        // Clean up VirtIO device resources.
14        if let Ok(devices) = self.virtio_devices.read() {
15            for device in devices.iter() {
16                // Unregister IRQFD.
17                let _ = self.unregister_irqfd(device.irq_fd, device.irq);
18
19                // Unregister IOEVENTFD.
20                let notify_addr = device.mmio_base + VIRTIO_MMIO_QUEUE_NOTIFY;
21                let _ = self
22                    .vm_fd
23                    .unregister_ioeventfd(notify_addr, 4, device.notify_fd);
24
25                // Close eventfds.
26                unsafe {
27                    libc::close(device.irq_fd);
28                    libc::close(device.notify_fd);
29                }
30            }
31        }
32
33        tracing::debug!("Dropped VM {}", self.id);
34    }
35}