2020-04-30 20:46:40 +00:00
|
|
|
// 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.
|
2020-05-02 00:30:24 +00:00
|
|
|
#[cfg(any(target_arch = "arm", target_arch = "aarch64"))]
|
|
|
|
pub mod aarch64;
|
2020-04-30 20:46:40 +00:00
|
|
|
pub mod caps;
|
|
|
|
pub mod kvm;
|
2020-05-02 00:30:24 +00:00
|
|
|
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
|
|
|
|
pub mod x86_64;
|
2020-04-30 20:46:40 +00:00
|
|
|
|
2020-05-02 00:30:24 +00:00
|
|
|
use std::ops::{Deref, DerefMut};
|
2020-04-30 20:46:40 +00:00
|
|
|
|
2020-05-02 00:30:24 +00:00
|
|
|
use sys_util::{GuestMemory, Result};
|
|
|
|
|
|
|
|
#[cfg(any(target_arch = "arm", target_arch = "aarch64"))]
|
|
|
|
pub use crate::aarch64::*;
|
2020-04-30 20:46:40 +00:00
|
|
|
pub use crate::caps::*;
|
2020-05-02 00:30:24 +00:00
|
|
|
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
|
|
|
|
pub use crate::x86_64::*;
|
2020-04-30 20:46:40 +00:00
|
|
|
|
2020-05-02 00:30:24 +00:00
|
|
|
/// A trait for checking hypervisor capabilities.
|
|
|
|
pub trait Hypervisor {
|
|
|
|
/// Checks if a particular `HypervisorCap` is available.
|
2020-04-30 20:46:40 +00:00
|
|
|
fn check_capability(&self, cap: &HypervisorCap) -> bool;
|
2020-05-02 00:30:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// A wrapper for using a VM and getting/setting its state.
|
2020-05-11 20:27:03 +00:00
|
|
|
pub trait Vm: Send + Sized {
|
|
|
|
/// Makes a shallow clone this `Vm`.
|
|
|
|
fn try_clone(&self) -> Result<Self>;
|
|
|
|
|
|
|
|
/// Gets the guest-mapped memory for the Vm.
|
|
|
|
fn get_memory(&self) -> &GuestMemory;
|
2020-05-02 00:30:24 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 20:27:03 +00:00
|
|
|
/// A wrapper around using a VCPU.
|
2020-05-02 00:30:24 +00:00
|
|
|
/// `Vcpu` provides all functionality except for running. To run, `to_runnable` must be called to
|
|
|
|
/// lock the vcpu to a thread. Then the returned `RunnableVcpu` can be used for running.
|
2020-05-11 20:27:03 +00:00
|
|
|
pub trait Vcpu: Send + Sized {
|
2020-05-02 00:30:24 +00:00
|
|
|
type Runnable: RunnableVcpu;
|
|
|
|
|
|
|
|
/// Consumes `self` and returns a `RunnableVcpu`. A `RunnableVcpu` is required to run the guest.
|
|
|
|
fn to_runnable(self) -> Result<Self::Runnable>;
|
|
|
|
|
|
|
|
/// Request the Vcpu to exit the next time it can accept an interrupt.
|
|
|
|
fn request_interrupt_window(&self) -> Result<()>;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A Vcpu that has a thread and can be run. Created by calling `to_runnable` on a `Vcpu`.
|
|
|
|
/// Implements `Deref` to a `Vcpu` so all `Vcpu` methods are usable, with the addition of the `run`
|
|
|
|
/// function to execute the guest.
|
|
|
|
pub trait RunnableVcpu: Deref<Target = <Self as RunnableVcpu>::Vcpu> + DerefMut {
|
|
|
|
type Vcpu: Vcpu;
|
|
|
|
|
|
|
|
/// Runs the VCPU until it exits, returning the reason for the exit.
|
|
|
|
///
|
|
|
|
/// Note that the state of the VCPU and associated VM must be setup first for this to do
|
|
|
|
/// anything useful.
|
|
|
|
fn run(&self) -> Result<VcpuExit>;
|
|
|
|
}
|
|
|
|
|
2020-05-11 20:27:03 +00:00
|
|
|
/// A memory region in the current process that can be mapped into the guest's memory.
|
|
|
|
///
|
|
|
|
/// Safe when implementers guarantee `ptr`..`ptr+size` is an mmaped region owned by this object that
|
|
|
|
/// can't be unmapped during the `MappedRegion`'s lifetime.
|
|
|
|
pub unsafe trait MappedRegion: Send + Sync {
|
|
|
|
/// Returns a pointer to the beginning of the memory region. Should only be
|
|
|
|
/// used for passing this region to ioctls for setting guest memory.
|
|
|
|
fn as_ptr(&self) -> *mut u8;
|
|
|
|
|
|
|
|
/// Returns the size of the memory region in bytes.
|
|
|
|
fn size(&self) -> usize;
|
|
|
|
|
|
|
|
/// Flushes changes to this memory region to the backing file.
|
|
|
|
fn msync(&self) -> Result<()>;
|
|
|
|
}
|
|
|
|
|
2020-05-02 00:30:24 +00:00
|
|
|
/// A reason why a VCPU exited. One of these returns every time `Vcpu::run` is called.
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum VcpuExit {
|
|
|
|
Unknown,
|
2020-04-30 20:46:40 +00:00
|
|
|
}
|
2020-04-30 22:48:47 +00:00
|
|
|
|
|
|
|
pub struct IrqRoute {}
|