Skip to content

Commit

Permalink
Adjusts hypervisor errors on mac to use NonZero (#852)
Browse files Browse the repository at this point in the history
Co-authored-by: tompro <[email protected]>
  • Loading branch information
SuchAFuriousDeath and tompro committed Jun 22, 2024
1 parent c216dc7 commit d594e0c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
31 changes: 19 additions & 12 deletions src/kernel/src/hv/mac.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,37 @@
use std::ffi::{c_int, c_void};
use std::num::NonZero;
use std::ptr::null_mut;

/// RAII struct for `hv_vm_create` and `hv_vm_destroy`.
pub struct Vm(());

impl Vm {
pub fn new() -> Result<Self, c_int> {
match unsafe { hv_vm_create(null_mut()) } {
0 => Ok(Self(())),
v => Err(v),
pub fn new() -> Result<Self, NonZero<c_int>> {
let ret = unsafe { hv_vm_create(null_mut()) };

match NonZero::new(ret) {
Some(ret) => Err(ret),
None => Ok(Self(())),
}
}

pub fn capability(&self, id: u64) -> Result<u64, c_int> {
pub fn capability(&self, id: u64) -> Result<u64, NonZero<c_int>> {
let mut value = 0;

match unsafe { hv_capability(id, &mut value) } {
0 => Ok(value),
v => Err(v),
let ret = unsafe { hv_capability(id, &mut value) };

match NonZero::new(ret) {
Some(ret) => Err(ret),
None => Ok(value),
}
}

pub fn vm_map(&self, host: *mut c_void, guest: u64, len: usize) -> Result<(), c_int> {
match unsafe { hv_vm_map(host, guest, len, 1 | 2 | 4) } {
0 => Ok(()),
v => Err(v),
pub fn vm_map(&self, host: *mut c_void, guest: u64, len: usize) -> Result<(), NonZero<c_int>> {
let ret = unsafe { hv_vm_map(host, guest, len, 1 | 2 | 4) };

match NonZero::new(ret) {
Some(ret) => Err(ret),
None => Ok(()),
}
}
}
Expand Down
14 changes: 10 additions & 4 deletions src/kernel/src/hv/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
pub use self::ram::*;
#[cfg(target_os = "macos")]
use std::num::NonZero;
use thiserror::Error;

pub use self::ram::*;

#[cfg(target_os = "linux")]
mod linux;

#[cfg(target_os = "macos")]
mod mac;

mod ram;

#[cfg(target_os = "windows")]
mod win32;

Expand Down Expand Up @@ -190,13 +196,13 @@ pub enum HypervisorError {

#[cfg(target_os = "macos")]
#[error("couldn't create a VM ({0:#x})")]
CreateVmFailed(std::ffi::c_int),
CreateVmFailed(NonZero<std::ffi::c_int>),

#[cfg(target_os = "macos")]
#[error("couldn't get maximum number of CPU for a VM")]
GetMaxCpuFailed(std::ffi::c_int),
GetMaxCpuFailed(NonZero<std::ffi::c_int>),

#[cfg(target_os = "macos")]
#[error("couldn't map memory to the VM")]
MapRamFailed(std::ffi::c_int),
MapRamFailed(NonZero<std::ffi::c_int>),
}

0 comments on commit d594e0c

Please sign in to comment.