From 6c8749adf29601e8fe25e84dc1cd039f6c88d515 Mon Sep 17 00:00:00 2001 From: Michael Zhao Date: Fri, 17 Jul 2020 14:10:54 +0800 Subject: [PATCH] arch: Switch to hypervisor crate Removed the dependency on kvm-bindings and kvm-ioctls, use hypervisor crate instead. Signed-off-by: Michael Zhao --- Cargo.lock | 2 -- arch/Cargo.toml | 2 -- arch/src/aarch64/gic.rs | 28 ++++++++++++++++------------ arch/src/aarch64/gicv2.rs | 23 +++++++++++++---------- arch/src/aarch64/gicv3.rs | 21 ++++++++++++--------- arch/src/aarch64/gicv3_its.rs | 15 +++++++++------ arch/src/aarch64/mod.rs | 1 + arch/src/aarch64/regs.rs | 2 +- arch/src/lib.rs | 2 -- arch/src/x86_64/regs.rs | 1 - 10 files changed, 52 insertions(+), 45 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8332dd157..aa10397a8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -61,8 +61,6 @@ dependencies = [ "arch_gen", "byteorder", "hypervisor", - "kvm-bindings", - "kvm-ioctls", "libc", "linux-loader", "log 0.4.11", diff --git a/arch/Cargo.toml b/arch/Cargo.toml index 13fcaca20..c0fd6a0f8 100644 --- a/arch/Cargo.toml +++ b/arch/Cargo.toml @@ -11,8 +11,6 @@ acpi = ["acpi_tables"] anyhow = "1.0" byteorder = "1.3.4" hypervisor = { path = "../hypervisor" } -kvm-bindings = { git = "https://github.com/cloud-hypervisor/kvm-bindings", branch = "ch" } -kvm-ioctls = { git = "https://github.com/cloud-hypervisor/kvm-ioctls", branch = "ch" } libc = "0.2.73" log = "0.4.11" vm-memory = { version = "0.2.1", features = ["backend-mmap"] } diff --git a/arch/src/aarch64/gic.rs b/arch/src/aarch64/gic.rs index 5ebd1e581..93ad1f4bf 100644 --- a/arch/src/aarch64/gic.rs +++ b/arch/src/aarch64/gic.rs @@ -1,8 +1,8 @@ // Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 -use kvm_ioctls::DeviceFd; use std::result; +use std::sync::Arc; /// Errors thrown while setting up the GIC. #[derive(Debug)] @@ -10,13 +10,13 @@ pub enum Error { /// Error while calling KVM ioctl for setting up the global interrupt controller. CreateGIC(hypervisor::HypervisorVmError), /// Error while setting device attributes for the GIC. - SetDeviceAttribute(hypervisor::kvm_ioctls::Error), + SetDeviceAttribute(hypervisor::HypervisorDeviceError), } type Result = result::Result; pub trait GICDevice { - /// Returns the file descriptor of the GIC device - fn device_fd(&self) -> &DeviceFd; + /// Returns the hypervisor agnostic Device of the GIC device + fn device(&self) -> &Arc; /// Returns the fdt compatibility property of the device fn fdt_compatibility(&self) -> &str; @@ -50,10 +50,10 @@ pub mod kvm { use super::super::gicv2::kvm::KvmGICv2; use super::super::gicv3::kvm::KvmGICv3; use super::super::gicv3_its::kvm::KvmGICv3ITS; - use super::super::layout; use super::GICDevice; use super::Result; - use kvm_ioctls::DeviceFd; + use crate::layout; + use hypervisor::kvm::kvm_bindings; use std::boxed::Box; use std::sync::Arc; @@ -65,7 +65,10 @@ pub mod kvm { Self: Sized; /// Create the GIC device object - fn create_device(fd: DeviceFd, vcpu_count: u64) -> Box + fn create_device( + device: Arc, + vcpu_count: u64, + ) -> Box where Self: Sized; @@ -78,7 +81,7 @@ pub mod kvm { Self: Sized; /// Initialize a GIC device - fn init_device(vm: &Arc) -> Result + fn init_device(vm: &Arc) -> Result> where Self: Sized, { @@ -94,7 +97,7 @@ pub mod kvm { /// Set a GIC device attribute fn set_device_attribute( - fd: &DeviceFd, + device: &Arc, group: u32, attr: u64, addr: u64, @@ -109,7 +112,8 @@ pub mod kvm { addr: addr, flags: flags, }; - fd.set_device_attr(&attr) + device + .set_device_attr(&attr) .map_err(super::Error::SetDeviceAttribute)?; Ok(()) @@ -126,7 +130,7 @@ pub mod kvm { let nr_irqs: u32 = layout::IRQ_MAX - layout::IRQ_BASE + 1; let nr_irqs_ptr = &nr_irqs as *const u32; Self::set_device_attribute( - gic_device.device_fd(), + gic_device.device(), kvm_bindings::KVM_DEV_ARM_VGIC_GRP_NR_IRQS, 0, nr_irqs_ptr as u64, @@ -137,7 +141,7 @@ pub mod kvm { * See https://code.woboq.org/linux/linux/virt/kvm/arm/vgic/vgic-kvm-device.c.html#211. */ Self::set_device_attribute( - gic_device.device_fd(), + gic_device.device(), kvm_bindings::KVM_DEV_ARM_VGIC_GRP_CTRL, u64::from(kvm_bindings::KVM_DEV_ARM_VGIC_CTRL_INIT), 0, diff --git a/arch/src/aarch64/gicv2.rs b/arch/src/aarch64/gicv2.rs index 44b90afd6..612e87d79 100644 --- a/arch/src/aarch64/gicv2.rs +++ b/arch/src/aarch64/gicv2.rs @@ -2,18 +2,18 @@ // SPDX-License-Identifier: Apache-2.0 pub mod kvm { + use super::super::gic::kvm::KvmGICDevice; use super::super::gic::{Error, GICDevice}; use std::{boxed::Box, result}; type Result = result::Result; - use super::super::gic::kvm::KvmGICDevice; - use super::super::layout; - use kvm_ioctls::DeviceFd; + use crate::layout; + use hypervisor::kvm::kvm_bindings; use std::sync::Arc; /// Represent a GIC v2 device pub struct KvmGICv2 { /// The file descriptor for the KVM device - fd: DeviceFd, + device: Arc, /// GIC device properties, to be used for setting up the fdt entry properties: [u64; 4], @@ -53,8 +53,8 @@ pub mod kvm { } impl GICDevice for KvmGICv2 { - fn device_fd(&self) -> &DeviceFd { - &self.fd + fn device(&self) -> &Arc { + &self.device } fn device_properties(&self) -> &[u64] { @@ -79,9 +79,12 @@ pub mod kvm { kvm_bindings::kvm_device_type_KVM_DEV_TYPE_ARM_VGIC_V2 } - fn create_device(fd: DeviceFd, vcpu_count: u64) -> Box { + fn create_device( + device: Arc, + vcpu_count: u64, + ) -> Box { Box::new(KvmGICv2 { - fd: fd, + device: device, properties: [ KvmGICv2::get_dist_addr(), KvmGICv2::get_dist_size(), @@ -99,7 +102,7 @@ pub mod kvm { /* Setting up the distributor attribute. We are placing the GIC below 1GB so we need to substract the size of the distributor. */ Self::set_device_attribute( - &gic_device.device_fd(), + gic_device.device(), kvm_bindings::KVM_DEV_ARM_VGIC_GRP_ADDR, u64::from(kvm_bindings::KVM_VGIC_V2_ADDR_TYPE_DIST), &KvmGICv2::get_dist_addr() as *const u64 as u64, @@ -108,7 +111,7 @@ pub mod kvm { /* Setting up the CPU attribute. */ Self::set_device_attribute( - &gic_device.device_fd(), + gic_device.device(), kvm_bindings::KVM_DEV_ARM_VGIC_GRP_ADDR, u64::from(kvm_bindings::KVM_VGIC_V2_ADDR_TYPE_CPU), &KvmGICv2::get_cpu_addr() as *const u64 as u64, diff --git a/arch/src/aarch64/gicv3.rs b/arch/src/aarch64/gicv3.rs index 04ca58f8a..bfb49f737 100644 --- a/arch/src/aarch64/gicv3.rs +++ b/arch/src/aarch64/gicv3.rs @@ -4,15 +4,15 @@ pub mod kvm { use super::super::gic::kvm::KvmGICDevice; use super::super::gic::{Error, GICDevice}; - use super::super::layout; - use kvm_ioctls::DeviceFd; + use crate::layout; + use hypervisor::kvm::kvm_bindings; use std::sync::Arc; use std::{boxed::Box, result}; type Result = result::Result; pub struct KvmGICv3 { /// The file descriptor for the KVM device - fd: DeviceFd, + device: Arc, /// GIC device properties, to be used for setting up the fdt entry properties: [u64; 4], @@ -53,8 +53,8 @@ pub mod kvm { } impl GICDevice for KvmGICv3 { - fn device_fd(&self) -> &DeviceFd { - &self.fd + fn device(&self) -> &Arc { + &self.device } fn fdt_compatibility(&self) -> &str { @@ -79,9 +79,12 @@ pub mod kvm { kvm_bindings::kvm_device_type_KVM_DEV_TYPE_ARM_VGIC_V3 } - fn create_device(fd: DeviceFd, vcpu_count: u64) -> Box { + fn create_device( + device: Arc, + vcpu_count: u64, + ) -> Box { Box::new(KvmGICv3 { - fd: fd, + device: device, properties: [ KvmGICv3::get_dist_addr(), KvmGICv3::get_dist_size(), @@ -100,7 +103,7 @@ pub mod kvm { We are placing the GIC below 1GB so we need to substract the size of the distributor. */ Self::set_device_attribute( - &gic_device.device_fd(), + gic_device.device(), kvm_bindings::KVM_DEV_ARM_VGIC_GRP_ADDR, u64::from(kvm_bindings::KVM_VGIC_V3_ADDR_TYPE_DIST), &KvmGICv3::get_dist_addr() as *const u64 as u64, @@ -111,7 +114,7 @@ pub mod kvm { We are calculating here the start of the redistributors address. We have one per CPU. */ Self::set_device_attribute( - &gic_device.device_fd(), + gic_device.device(), kvm_bindings::KVM_DEV_ARM_VGIC_GRP_ADDR, u64::from(kvm_bindings::KVM_VGIC_V3_ADDR_TYPE_REDIST), &KvmGICv3::get_redists_addr(u64::from(gic_device.vcpu_count())) as *const u64 diff --git a/arch/src/aarch64/gicv3_its.rs b/arch/src/aarch64/gicv3_its.rs index 964834030..e66248028 100644 --- a/arch/src/aarch64/gicv3_its.rs +++ b/arch/src/aarch64/gicv3_its.rs @@ -8,11 +8,11 @@ pub mod kvm { use super::super::gic::kvm::KvmGICDevice; use super::super::gic::{Error, GICDevice}; use super::super::gicv3::kvm::KvmGICv3; - use kvm_ioctls::DeviceFd; + use hypervisor::kvm::kvm_bindings; pub struct KvmGICv3ITS { /// The file descriptor for the KVM device - fd: DeviceFd, + device: Arc, /// GIC device properties, to be used for setting up the fdt entry gic_properties: [u64; 4], @@ -37,8 +37,8 @@ pub mod kvm { } impl GICDevice for KvmGICv3ITS { - fn device_fd(&self) -> &DeviceFd { - &self.fd + fn device(&self) -> &Arc { + &self.device } fn fdt_compatibility(&self) -> &str { @@ -75,9 +75,12 @@ pub mod kvm { KvmGICv3::version() } - fn create_device(fd: DeviceFd, vcpu_count: u64) -> Box { + fn create_device( + device: Arc, + vcpu_count: u64, + ) -> Box { Box::new(KvmGICv3ITS { - fd: fd, + device: device, gic_properties: [ KvmGICv3::get_dist_addr(), KvmGICv3::get_dist_size(), diff --git a/arch/src/aarch64/mod.rs b/arch/src/aarch64/mod.rs index 64d6c7a61..8d42cdc38 100644 --- a/arch/src/aarch64/mod.rs +++ b/arch/src/aarch64/mod.rs @@ -17,6 +17,7 @@ pub mod regs; pub use self::fdt::DeviceInfoForFDT; use crate::DeviceType; use crate::RegionType; +use hypervisor::kvm::kvm_bindings; use std::collections::HashMap; use std::ffi::CStr; use std::fmt::Debug; diff --git a/arch/src/aarch64/regs.rs b/arch/src/aarch64/regs.rs index 001b67941..2abfada06 100644 --- a/arch/src/aarch64/regs.rs +++ b/arch/src/aarch64/regs.rs @@ -6,7 +6,7 @@ // found in the THIRD-PARTY file. use super::get_fdt_addr; -use kvm_bindings::{ +use hypervisor::kvm::kvm_bindings::{ user_pt_regs, KVM_REG_ARM64, KVM_REG_ARM64_SYSREG, KVM_REG_ARM64_SYSREG_CRM_MASK, KVM_REG_ARM64_SYSREG_CRM_SHIFT, KVM_REG_ARM64_SYSREG_CRN_MASK, KVM_REG_ARM64_SYSREG_CRN_SHIFT, KVM_REG_ARM64_SYSREG_OP0_MASK, KVM_REG_ARM64_SYSREG_OP0_SHIFT, KVM_REG_ARM64_SYSREG_OP1_MASK, diff --git a/arch/src/lib.rs b/arch/src/lib.rs index 2b3baf43d..2c6f7a417 100644 --- a/arch/src/lib.rs +++ b/arch/src/lib.rs @@ -16,8 +16,6 @@ extern crate byteorder; extern crate hypervisor; -extern crate kvm_bindings; -extern crate kvm_ioctls; extern crate libc; #[macro_use] extern crate log; diff --git a/arch/src/x86_64/regs.rs b/arch/src/x86_64/regs.rs index ea0b748eb..aae9b445f 100644 --- a/arch/src/x86_64/regs.rs +++ b/arch/src/x86_64/regs.rs @@ -258,7 +258,6 @@ pub fn setup_page_tables(mem: &GuestMemoryMmap, sregs: &mut SpecialRegisters) -> #[cfg(test)] mod tests { - extern crate kvm_ioctls; extern crate vm_memory; use super::*;