diff options
Diffstat (limited to 'hypervisor')
-rw-r--r-- | hypervisor/Cargo.toml | 10 | ||||
-rw-r--r-- | hypervisor/src/caps.rs | 6 | ||||
-rw-r--r-- | hypervisor/src/kvm/mod.rs | 62 | ||||
-rw-r--r-- | hypervisor/src/lib.rs | 25 | ||||
-rw-r--r-- | hypervisor/src/types/mod.rs | 9 | ||||
-rw-r--r-- | hypervisor/src/types/x86.rs | 10 |
6 files changed, 122 insertions, 0 deletions
diff --git a/hypervisor/Cargo.toml b/hypervisor/Cargo.toml new file mode 100644 index 0000000..de82105 --- /dev/null +++ b/hypervisor/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "hypervisor" +version = "0.1.0" +authors = ["The Chromium OS Authors"] +edition = "2018" + +[dependencies] +libc = "*" +kvm_sys = { path = "../kvm_sys" } +sys_util = { path = "../sys_util" } \ No newline at end of file diff --git a/hypervisor/src/caps.rs b/hypervisor/src/caps.rs new file mode 100644 index 0000000..d088ca7 --- /dev/null +++ b/hypervisor/src/caps.rs @@ -0,0 +1,6 @@ +// Copyright 2020 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +/// An enumeration of different hypervisor capabilities. +pub enum HypervisorCap {} diff --git a/hypervisor/src/kvm/mod.rs b/hypervisor/src/kvm/mod.rs new file mode 100644 index 0000000..7058921 --- /dev/null +++ b/hypervisor/src/kvm/mod.rs @@ -0,0 +1,62 @@ +// Copyright 2020 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +use super::{CpuId, Hypervisor, HypervisorCap}; +use libc::{open, O_CLOEXEC, O_RDWR}; +use std::os::raw::c_char; +use sys_util::{ + errno_result, AsRawDescriptor, FromRawDescriptor, RawDescriptor, Result, SafeDescriptor, +}; + +pub struct Kvm { + kvm: SafeDescriptor, +} + +impl Kvm { + /// Opens `/dev/kvm/` and returns a Kvm object on success. + pub fn new() -> Result<Kvm> { + // Open calls are safe because we give a constant nul-terminated string and verify the + // result. + let ret = unsafe { open("/dev/kvm\0".as_ptr() as *const c_char, O_RDWR | O_CLOEXEC) }; + if ret < 0 { + return errno_result(); + } + // Safe because we verify that ret is valid and we own the fd. + Ok(Kvm { + kvm: unsafe { SafeDescriptor::from_raw_descriptor(ret) }, + }) + } +} + +impl AsRawDescriptor for Kvm { + fn as_raw_descriptor(&self) -> RawDescriptor { + self.kvm.as_raw_descriptor() + } +} + +impl Hypervisor for Kvm { + fn check_capability(&self, _cap: &HypervisorCap) -> bool { + unimplemented!("check_capability for Kvm is not yet implemented"); + } + + #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] + fn get_supported_cpuid(&self) -> Result<CpuId> { + unimplemented!("get_supported_cpuid for Kvm is not yet implemented"); + } + + #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] + fn get_emulated_cpuid(&self) -> Result<CpuId> { + unimplemented!("get_emulated_cpuid for Kvm is not yet implemented"); + } +} + +#[cfg(test)] +mod tests { + use super::Kvm; + + #[test] + fn new() { + Kvm::new().unwrap(); + } +} diff --git a/hypervisor/src/lib.rs b/hypervisor/src/lib.rs new file mode 100644 index 0000000..056070b --- /dev/null +++ b/hypervisor/src/lib.rs @@ -0,0 +1,25 @@ +// Copyright 2020 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +//! A crate for abstracting the underlying kernel hypervisor used in crosvm. +pub mod caps; +pub mod kvm; +pub mod types; + +use sys_util::Result; + +pub use crate::caps::*; +pub use crate::types::*; + +/// A trait for managing the underlying cpu information for the hypervisor and to check its capabilities. +trait Hypervisor { + // Checks if a particular `HypervisorCap` is available. + fn check_capability(&self, cap: &HypervisorCap) -> bool; + // Get the system supported CPUID values. + #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] + fn get_supported_cpuid(&self) -> Result<CpuId>; + // Get the system emulated CPUID values. + #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] + fn get_emulated_cpuid(&self) -> Result<CpuId>; +} diff --git a/hypervisor/src/types/mod.rs b/hypervisor/src/types/mod.rs new file mode 100644 index 0000000..69fa9e4 --- /dev/null +++ b/hypervisor/src/types/mod.rs @@ -0,0 +1,9 @@ +// Copyright 2020 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#[cfg(any(target_arch = "x86", target_arch = "x86_64"))] +pub mod x86; + +#[cfg(any(target_arch = "x86", target_arch = "x86_64"))] +pub use self::x86::*; diff --git a/hypervisor/src/types/x86.rs b/hypervisor/src/types/x86.rs new file mode 100644 index 0000000..cd5236a --- /dev/null +++ b/hypervisor/src/types/x86.rs @@ -0,0 +1,10 @@ +// Copyright 2020 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +use kvm_sys::kvm_cpuid_entry2; + +pub type CpuIdEntry = kvm_cpuid_entry2; +pub struct CpuId { + _cpu_id_entries: Vec<CpuIdEntry>, +} |