applevisor/
lib.rs

1#![doc = include_str!("../README.md")]
2#![cfg_attr(feature = "simd-nightly", feature(portable_simd), feature(simd_ffi))]
3
4pub mod error;
5#[cfg(feature = "macos-15-0")]
6pub mod gic;
7pub mod memory;
8pub mod vcpu;
9pub mod vm;
10
11#[cfg(test)]
12use std::sync::atomic::{AtomicU64, Ordering};
13
14// -----------------------------------------------------------------------------------------------
15// Macros
16// -----------------------------------------------------------------------------------------------
17
18/// Macro that calls an ffi hypervisor function and wraps the resulting return value in a
19/// [`Result`].
20macro_rules! hv_unsafe_call {
21    ($x:expr) => {{
22        let ret = unsafe { $x };
23        match ret {
24            x if x == hv_error_t::HV_SUCCESS as i32 => Ok(()),
25            code => Err(HypervisorError::from(code)),
26        }
27    }};
28}
29
30pub(crate) use hv_unsafe_call;
31
32// -----------------------------------------------------------------------------------------------
33// Prelude
34// -----------------------------------------------------------------------------------------------
35
36/// The AppleVisor prelude.
37pub mod prelude {
38    pub use crate::error::*;
39    #[cfg(feature = "macos-15-0")]
40    pub use crate::gic::*;
41    pub use crate::memory::*;
42    pub use crate::vcpu::*;
43    pub use crate::vm::*;
44}
45
46// -----------------------------------------------------------------------------------------------
47// Tests
48// -----------------------------------------------------------------------------------------------
49
50#[cfg(test)]
51static ALLOC_ID: AtomicU64 = AtomicU64::new(1);
52
53#[cfg(test)]
54pub(crate) fn next_mem_addr() -> u64 {
55    ALLOC_ID.fetch_add(1, Ordering::Relaxed) * crate::memory::PAGE_SIZE as u64
56}