Skip to content

Commit abc536f

Browse files
committed
Define portable KVM API
Encapsulate platform specific creation of KVM interrupt controller Fixes: #440 Signed-off-by: Pepper Gray <[email protected]>
1 parent 246b3f7 commit abc536f

File tree

4 files changed

+60
-13
lines changed

4 files changed

+60
-13
lines changed

src/devices/src/legacy/kvmapi.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use kvm_ioctls::{Error, VmFd};
2+
3+
#[cfg(all(target_os = "linux", target_arch = "riscv64"))]
4+
use crate::legacy::KvmAia;
5+
#[cfg(all(target_os = "linux", target_arch = "x86_64"))]
6+
use crate::legacy::KvmIoapic;
7+
#[cfg(all(target_os = "linux", target_arch = "aarch64"))]
8+
use crate::legacy::{KvmGicV2, KvmGicV3};
9+
10+
pub enum KvmApi {
11+
#[cfg(all(target_os = "linux", target_arch = "x86_64"))]
12+
KvmIoapic(KvmIoapic),
13+
#[cfg(all(target_os = "linux", target_arch = "aarch64"))]
14+
KvmGicV2(KvmGicV2),
15+
#[cfg(all(target_os = "linux", target_arch = "aarch64"))]
16+
KvmGicV3(KvmGicV3),
17+
#[cfg(all(target_os = "linux", target_arch = "riscv64"))]
18+
KvmAia(KvmAia),
19+
}
20+
21+
impl KvmApi {
22+
#[cfg(all(target_os = "linux", target_arch = "riscv64"))]
23+
pub fn new(vm: &VmFd, vcpu_count: u64) -> Result<Self, Error> {
24+
let kvmaia = KvmAia::new(vm, vcpu_count as _).unwrap();
25+
Ok(Self::KvmAia(kvmaia))
26+
}
27+
28+
#[cfg(all(target_os = "linux", target_arch = "x86_64"))]
29+
pub fn new(vm: &VmFd, _vcpu_count: u64) -> Result<Self, Error> {
30+
let kvmioapic = KvmIoapic::new(vm).unwrap();
31+
Ok(Self::KvmIoapic(kvmioapic))
32+
}
33+
34+
#[cfg(all(target_os = "linux", target_arch = "aarch64"))]
35+
pub fn new(vm: &VmFd, vcpu_count: u64) -> Result<Self, Error> {
36+
if let Ok(v3) = KvmGicV3::new(vm, vcpu_count.into()) {
37+
Ok(Self::KvmGicV3(v3))
38+
} else {
39+
log::warn!("GICv3 creation failed, falling back to GICv2");
40+
Ok(Self::KvmGicV2(KvmGicV2::new(vm, vcpu_count.into())))
41+
}
42+
}
43+
}

src/devices/src/legacy/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ mod ioapic;
1717
mod irqchip;
1818
#[cfg(all(target_os = "linux", target_arch = "riscv64"))]
1919
mod kvmaia;
20+
mod kvmapi;
2021
#[cfg(all(target_os = "linux", target_arch = "aarch64"))]
2122
mod kvmgicv2;
2223
#[cfg(all(target_os = "linux", target_arch = "aarch64"))]
@@ -57,6 +58,7 @@ pub use self::irqchip::test_utils::DummyIrqChip;
5758
pub use self::irqchip::{IrqChip, IrqChipDevice, IrqChipT};
5859
#[cfg(all(target_os = "linux", target_arch = "riscv64"))]
5960
pub use self::kvmaia::KvmAia;
61+
pub use self::kvmapi::KvmApi;
6062
#[cfg(all(target_os = "linux", target_arch = "aarch64"))]
6163
pub use self::kvmgicv2::KvmGicV2;
6264
#[cfg(all(target_os = "linux", target_arch = "aarch64"))]

src/vmm/src/device_manager/kvm/mmio.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -315,11 +315,10 @@ mod tests {
315315
use super::*;
316316
use arch;
317317
use devices::legacy::DummyIrqChip;
318+
use devices::legacy::KvmApi;
318319
use devices::virtio::InterruptTransport;
319-
use devices::{
320-
legacy::KvmIoapic,
321-
virtio::{ActivateResult, Queue, VirtioDevice},
322-
};
320+
321+
use devices::virtio::{ActivateResult, Queue, VirtioDevice};
323322
use std::sync::Arc;
324323
use utils::errno;
325324
use utils::eventfd::EventFd;
@@ -333,7 +332,7 @@ mod tests {
333332
vm: &VmFd,
334333
guest_mem: GuestMemoryMmap,
335334
device: Arc<Mutex<dyn devices::virtio::VirtioDevice>>,
336-
cmdline: &mut kernel_cmdline::Cmdline,
335+
_cmdline: &mut kernel_cmdline::Cmdline,
337336
type_id: u32,
338337
device_id: &str,
339338
) -> Result<u64> {
@@ -343,7 +342,7 @@ mod tests {
343342
let (mmio_base, _irq) =
344343
self.register_mmio_device(vm, mmio_device, type_id, device_id.to_string())?;
345344
#[cfg(target_arch = "x86_64")]
346-
self.add_device_to_cmdline(cmdline, mmio_base, _irq)?;
345+
self.add_device_to_cmdline(_cmdline, mmio_base, _irq)?;
347346
Ok(mmio_base)
348347
}
349348
}
@@ -435,7 +434,7 @@ mod tests {
435434
let vm = builder::setup_vm(&guest_mem, false).unwrap();
436435
let mut device_manager =
437436
MMIODeviceManager::new(&mut 0xd000_0000, (arch::IRQ_BASE, arch::IRQ_MAX));
438-
let _kvmioapic = KvmIoapic::new(vm.fd()).unwrap();
437+
let _kvmapi = KvmApi::new(vm.fd(), vcpu_count.into()).unwrap();
439438

440439
let mut cmdline = kernel_cmdline::Cmdline::new(4096);
441440
let dummy = Arc::new(Mutex::new(DummyDevice::new()));
@@ -455,7 +454,7 @@ mod tests {
455454
let vm = builder::setup_vm(&guest_mem, false).unwrap();
456455
let mut device_manager =
457456
MMIODeviceManager::new(&mut 0xd000_0000, (arch::IRQ_BASE, arch::IRQ_MAX));
458-
let _kvmioapic = KvmIoapic::new(vm.fd()).unwrap();
457+
let _kvmapi = KvmApi::new(vm.fd(), vcpu_count.into()).unwrap();
459458

460459
let mut cmdline = kernel_cmdline::Cmdline::new(4096);
461460

@@ -552,7 +551,7 @@ mod tests {
552551
let start_addr2 = GuestAddress(0x1000);
553552
let guest_mem =
554553
GuestMemoryMmap::from_ranges(&[(start_addr1, 0x1000), (start_addr2, 0x1000)]).unwrap();
555-
let vcpu_count = 1;
554+
let _vcpu_count = 1;
556555
let vm = builder::setup_vm(&guest_mem, false).unwrap();
557556
let mut device_manager =
558557
MMIODeviceManager::new(&mut 0xd000_0000, (arch::IRQ_BASE, arch::IRQ_MAX));

src/vmm/src/linux/vstate.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1773,7 +1773,8 @@ mod tests {
17731773

17741774
use super::*;
17751775
use devices;
1776-
use devices::legacy::KvmIoapic;
1776+
#[cfg(all(target_os = "linux", target_arch = "x86_64"))]
1777+
use devices::legacy::KvmApi;
17771778

17781779
use utils::signal::validate_signal_num;
17791780

@@ -1795,7 +1796,9 @@ mod tests {
17951796
let kvm = KvmContext::new().unwrap();
17961797
let gm = GuestMemoryMmap::from_ranges(&[(GuestAddress(0), mem_size)]).unwrap();
17971798
let mut vm = Vm::new(kvm.fd()).expect("Cannot create new vm");
1798-
let _kvmioapic = KvmIoapic::new(&vm.fd()).unwrap();
1799+
let vcpu_count = 1;
1800+
#[cfg(all(target_os = "linux", target_arch = "x86_64"))]
1801+
let _kvmapi = KvmApi::new(vm.fd(), vcpu_count.into()).unwrap();
17991802
assert!(vm.memory_init(&gm, kvm.max_memslots()).is_ok());
18001803

18011804
let exit_evt = EventFd::new(utils::eventfd::EFD_NONBLOCK).unwrap();
@@ -1804,7 +1807,7 @@ mod tests {
18041807
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
18051808
{
18061809
vcpu = Vcpu::new_x86_64(
1807-
1,
1810+
vcpu_count,
18081811
vm.fd(),
18091812
vm.supported_cpuid().clone(),
18101813
vm.supported_msrs().clone(),
@@ -1815,7 +1818,7 @@ mod tests {
18151818
}
18161819
#[cfg(target_arch = "aarch64")]
18171820
{
1818-
vcpu = Vcpu::new_aarch64(1, vm.fd(), exit_evt).unwrap();
1821+
vcpu = Vcpu::new_aarch64(vcpu_count, vm.fd(), exit_evt).unwrap();
18191822
}
18201823

18211824
(vm, vcpu, gm)

0 commit comments

Comments
 (0)