Alioth Code Coverage

hvf.rs0.00%

1// Copyright 2024 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#[path = "vcpu/vcpu.rs"]
16mod vcpu;
17mod vm;
18
19use std::fmt::{Display, Formatter};
20use std::io::ErrorKind;
21use std::os::raw::c_void;
22
23use crate::hv::{Hypervisor, Result, VmConfig};
24use crate::sys::os::os_release;
25
26use self::vm::HvfVm;
27
28#[derive(Debug, Clone, Copy)]
29#[repr(transparent)]
30pub struct HvReturn(i32);
31
32impl Display for HvReturn {
33 fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
34 let msg = match self.0 & 0xff {
35 0x01 => "Error",
36 0x02 => "Busy",
37 0x03 => "Bad argument",
38 0x04 => "Illegal guest state",
39 0x05 => "No resources",
40 0x06 => "No device",
41 0x07 => "Denied",
42 0x0f => "Unsupported",
43 _ => "Unknown",
44 };
45 write!(f, "{msg} {:#x}", self.0 as u32)
46 }
47}
48
49impl std::error::Error for HvReturn {}
50
51fn check_ret(ret: i32) -> std::io::Result<()> {
52 if ret == 0 {
53 return Ok(());
54 }
55 let kind = match (ret as u32) & 0xff {
56 0x02 => ErrorKind::ResourceBusy,
57 0x03 => ErrorKind::InvalidInput,
58 0x05 => ErrorKind::NotFound,
59 0x07 => ErrorKind::PermissionDenied,
60 0x0f => ErrorKind::Unsupported,
61 _ => ErrorKind::Other,
62 };
63 Err(std::io::Error::new(kind, HvReturn(ret)))
64}
65
66#[derive(Debug)]
67struct OsObject {
68 addr: usize,
69}
70
71impl Drop for OsObject {
72 fn drop(&mut self) {
73 let ptr = self.addr as *mut c_void;
74 unsafe { os_release(ptr) };
75 }
76}
77
78#[derive(Debug)]
79pub struct Hvf {}
80
81impl Hypervisor for Hvf {
82 type Vm = HvfVm;
83
84 fn create_vm(&self, _config: &VmConfig) -> Result<Self::Vm> {
85 HvfVm::new()
86 }
87}
88