feat(hvf): create and destroy guests

Signed-off-by: Changyuan Lyu <changyuanl@google.com>
This commit is contained in:
Changyuan Lyu 2024-06-23 12:36:41 -07:00 committed by Lencerf
parent cd95d29164
commit ebeff5f872
3 changed files with 67 additions and 2 deletions

View file

@ -0,0 +1,19 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#[link(name = "Hypervisor", kind = "framework")]
extern "C" {
pub fn hv_vm_create(config: *mut i32) -> i32;
pub fn hv_vm_destroy() -> i32;
}

View file

@ -12,20 +12,55 @@
// See the License for the specific language governing permissions and
// limitations under the License.
mod bindings;
#[path = "vcpu/vcpu.rs"]
mod vcpu;
mod vm;
use crate::hv::{Hypervisor, Result, VmConfig};
use std::fmt::{Display, Formatter};
use std::ptr::null_mut;
use bindings::hv_vm_create;
use snafu::ResultExt;
use crate::hv::{error, Hypervisor, Result, VmConfig};
use vm::HvfVm;
#[derive(Debug, Clone, Copy)]
#[repr(transparent)]
pub struct HvReturn(i32);
impl Display for HvReturn {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
write!(f, "{:#x}", self.0 as u32)
}
}
impl std::error::Error for HvReturn {}
fn check_ret(ret: i32) -> std::io::Result<()> {
if ret == 0 {
return Ok(());
}
let kind = match (ret as u32) & 0xff {
3 => std::io::ErrorKind::InvalidInput,
5 => std::io::ErrorKind::NotFound,
7 => std::io::ErrorKind::PermissionDenied,
0xf => std::io::ErrorKind::Unsupported,
_ => std::io::ErrorKind::Other,
};
Err(std::io::Error::new(kind, HvReturn(ret)))
}
#[derive(Debug)]
pub struct Hvf {}
impl Hypervisor for Hvf {
type Vm = HvfVm;
fn create_vm(&self, _config: &VmConfig) -> Result<Self::Vm> {
unimplemented!()
let ret = unsafe { hv_vm_create(null_mut()) };
check_ret(ret).context(error::CreateVm)?;
Ok(HvfVm {})
}
}

View file

@ -15,6 +15,8 @@
use std::os::fd::{AsFd, BorrowedFd};
use std::thread::JoinHandle;
use crate::hv::hvf::bindings::hv_vm_destroy;
use crate::hv::hvf::check_ret;
use crate::hv::hvf::vcpu::HvfVcpu;
use crate::hv::{
GicV2, IoeventFd, IoeventFdRegistry, IrqFd, IrqSender, MemMapOption, MsiSender, Result, Vm,
@ -173,6 +175,15 @@ impl GicV2 for HvfGicV2 {
#[derive(Debug)]
pub struct HvfVm {}
impl Drop for HvfVm {
fn drop(&mut self) {
let ret = unsafe { hv_vm_destroy() };
if let Err(e) = check_ret(ret) {
log::error!("hv_vm_destroy: {e:?}");
}
}
}
impl Vm for HvfVm {
type Vcpu = HvfVcpu;
type Memory = HvfMemory;