|
| 1 | +//! `hv` is a high level safe Rust crate to access Hypervisor Framework. |
| 2 | +
|
| 3 | +use std::error; |
| 4 | +use std::fmt; |
| 5 | + |
| 6 | +/// Low level access to generated bindings. |
1 | 7 | pub use hv_sys as sys; |
| 8 | + |
| 9 | +mod vcpu; |
| 10 | +pub mod vmx; |
| 11 | +pub mod x86; |
| 12 | + |
| 13 | +pub use vcpu::Vcpu; |
| 14 | + |
| 15 | +/// Helper macro to call unsafe Hypervisor functions and map returned error codes to [Error] type. |
| 16 | +#[macro_export] |
| 17 | +macro_rules! call { |
| 18 | + ($f:expr) => {{ |
| 19 | + let code = unsafe { $f }; |
| 20 | + match code { |
| 21 | + ::hv_sys::hv_return_t_HV_SUCCESS => Ok(()), |
| 22 | + _ => Err(Error::from(code)), |
| 23 | + } |
| 24 | + }}; |
| 25 | +} |
| 26 | + |
| 27 | +/// The type of system capabilities. |
| 28 | +#[repr(u64)] |
| 29 | +#[non_exhaustive] |
| 30 | +#[derive(Debug, Copy, Clone, Eq, PartialEq)] |
| 31 | +pub enum Capability { |
| 32 | + VCpuMax = sys::hv_capability_t_HV_CAP_VCPUMAX, |
| 33 | + AddrSpaceMax = sys::hv_capability_t_HV_CAP_ADDRSPACEMAX, |
| 34 | +} |
| 35 | + |
| 36 | +/// Type of a user virtual address. |
| 37 | +pub type UVAddr = sys::hv_uvaddr_t; |
| 38 | + |
| 39 | +/// Type of a guest physical address. |
| 40 | +pub type GPAddr = sys::hv_gpaddr_t; |
| 41 | + |
| 42 | +/// Type of a guest address space. |
| 43 | +pub type Space = sys::hv_vm_space_t; |
| 44 | + |
| 45 | +pub const VM_SPACE_DEFAULT: Space = sys::HV_VM_SPACE_DEFAULT; |
| 46 | + |
| 47 | +/// Guest physical memory region permissions. |
| 48 | +#[repr(u32)] |
| 49 | +#[non_exhaustive] |
| 50 | +#[derive(Debug, Copy, Clone, Eq, PartialEq)] |
| 51 | +pub enum Memory { |
| 52 | + Read = sys::HV_MEMORY_READ, |
| 53 | + Write = sys::HV_MEMORY_WRITE, |
| 54 | + Exec = sys::HV_MEMORY_EXEC, |
| 55 | +} |
| 56 | + |
| 57 | +/// Creates a VM instance for the current process. |
| 58 | +pub fn vm_create() -> Result<(), Error> { |
| 59 | + call!(sys::hv_vm_create(0)) |
| 60 | +} |
| 61 | + |
| 62 | +/// Gets the value of capabilities of the system. |
| 63 | +pub fn capability(cap: Capability) -> Result<u64, Error> { |
| 64 | + let mut out = 0_u64; |
| 65 | + call!(sys::hv_capability(cap as sys::hv_capability_t, &mut out))?; |
| 66 | + Ok(out) |
| 67 | +} |
| 68 | + |
| 69 | +/// Creates an additional guest address space for the current task. |
| 70 | +pub fn vm_space_create() -> Result<Space, Error> { |
| 71 | + let mut space: Space = 0; |
| 72 | + call!(sys::hv_vm_space_create(&mut space))?; |
| 73 | + Ok(space) |
| 74 | +} |
| 75 | + |
| 76 | +/// Destroys the address space instance associated with the current task. |
| 77 | +/// |
| 78 | +/// # Arguments |
| 79 | +/// * `asid` - Address space ID |
| 80 | +pub fn vm_space_destroy(asid: Space) -> Result<(), Error> { |
| 81 | + call!(sys::hv_vm_space_destroy(asid)) |
| 82 | +} |
| 83 | + |
| 84 | +/// Maps a region in the virtual address space of the current task into the guest physical |
| 85 | +/// address space of the VM. |
| 86 | +/// |
| 87 | +/// # Arguments |
| 88 | +/// * `uva` - Page aligned virtual address in the current task. |
| 89 | +/// * `gpa` - Page aligned address in the guest physical address space. |
| 90 | +/// * `size` - Size in bytes of the region to be mapped. |
| 91 | +/// * `flags` - READ, WRITE and EXECUTE permissions of the region |
| 92 | +pub fn vm_map(uva: UVAddr, gpa: GPAddr, size: u64, flags: Memory) -> Result<(), Error> { |
| 93 | + call!(sys::hv_vm_map( |
| 94 | + uva, |
| 95 | + gpa, |
| 96 | + size, |
| 97 | + flags as sys::hv_memory_flags_t |
| 98 | + )) |
| 99 | +} |
| 100 | + |
| 101 | +/// Unmaps a region in the guest physical address space of the VM |
| 102 | +/// |
| 103 | +/// # Arguments |
| 104 | +/// * `gpa` - Page aligned address in the guest physical address space. |
| 105 | +/// * `size` - Size in bytes of the region to be unmapped. |
| 106 | +pub fn vm_unmap(gpa: GPAddr, size: u64) -> Result<(), Error> { |
| 107 | + call!(sys::hv_vm_unmap(gpa, size)) |
| 108 | +} |
| 109 | + |
| 110 | +/// Modifies the permissions of a region in the guest physical address space of the VM. |
| 111 | +/// |
| 112 | +/// # Arguments |
| 113 | +/// * `gpa` - Page aligned address in the guest physical address space. |
| 114 | +/// * `size` - Size in bytes of the region to be modified. |
| 115 | +/// * `flags` - New READ, WRITE and EXECUTE permissions of the region. |
| 116 | +pub fn vm_protect(gpa: GPAddr, size: u64, flags: Memory) -> Result<(), Error> { |
| 117 | + call!(sys::hv_vm_protect( |
| 118 | + gpa, |
| 119 | + size, |
| 120 | + flags as sys::hv_memory_flags_t |
| 121 | + )) |
| 122 | +} |
| 123 | + |
| 124 | +/// Maps a region in the virtual address space of the current task |
| 125 | +/// into a guest physical address space of the VM. |
| 126 | +/// |
| 127 | +/// # Arguments |
| 128 | +/// * `asid` - Address space ID. |
| 129 | +/// * `uva` - Page aligned virtual address in the current task. |
| 130 | +/// * `gpa` - Page aligned address in the guest physical address space. |
| 131 | +/// * `size` - Size in bytes of the region to be mapped. |
| 132 | +/// * `flags` - READ, WRITE and EXECUTE permissions of the region. |
| 133 | +pub fn vm_map_space( |
| 134 | + asid: Space, |
| 135 | + uva: UVAddr, |
| 136 | + gpa: GPAddr, |
| 137 | + size: u64, |
| 138 | + flags: Memory, |
| 139 | +) -> Result<(), Error> { |
| 140 | + call!(sys::hv_vm_map_space( |
| 141 | + asid, |
| 142 | + uva, |
| 143 | + gpa, |
| 144 | + size, |
| 145 | + flags as sys::hv_memory_flags_t |
| 146 | + )) |
| 147 | +} |
| 148 | + |
| 149 | +/// Unmaps a region in a guest physical address space of the VM. |
| 150 | +/// |
| 151 | +/// # Arguments |
| 152 | +/// * `asid` - Address space ID. |
| 153 | +/// * `gpa` - Page aligned address in the guest physical address space. |
| 154 | +/// * `size` - Size in bytes of the region to be unmapped. |
| 155 | +pub fn vm_unmap_space(asid: Space, gpa: GPAddr, size: u64) -> Result<(), Error> { |
| 156 | + call!(sys::hv_vm_unmap_space(asid, gpa, size)) |
| 157 | +} |
| 158 | + |
| 159 | +/// Modifies the permissions of a region in a guest physical address space of the VM. |
| 160 | +/// |
| 161 | +/// # Arguments |
| 162 | +/// * `asid` - Address space ID. |
| 163 | +/// * `gpa` - Page aligned address in the guest physical address space. |
| 164 | +/// * `size` - Size in bytes of the region to be modified. |
| 165 | +/// * `flags` - New READ, WRITE and EXECUTE permissions of the region. |
| 166 | +pub fn vm_protect_space(asid: Space, gpa: GPAddr, size: u64, flags: Memory) -> Result<(), Error> { |
| 167 | + call!(sys::hv_vm_protect_space( |
| 168 | + asid, |
| 169 | + gpa, |
| 170 | + size, |
| 171 | + flags as sys::hv_memory_flags_t |
| 172 | + )) |
| 173 | +} |
| 174 | + |
| 175 | +/// Synchronizes guest TSC across all vCPUs. |
| 176 | +pub fn vm_sync_tsc(tcs: u64) -> Result<(), Error> { |
| 177 | + call!(sys::hv_vm_sync_tsc(tcs)) |
| 178 | +} |
| 179 | + |
| 180 | +/// Destroys the VM instance associated with the current process. |
| 181 | +pub fn vm_destroy() -> Result<(), Error> { |
| 182 | + call!(sys::hv_vm_destroy()) |
| 183 | +} |
| 184 | + |
| 185 | +/// The return type of framework functions. |
| 186 | +/// Wraps the underlying `hv_return_t` type. |
| 187 | +#[derive(Debug, Copy, Clone, Eq, PartialEq)] |
| 188 | +pub enum Error { |
| 189 | + Unsuccessful, |
| 190 | + Busy, |
| 191 | + BadArgument, |
| 192 | + NoResources, |
| 193 | + NoDevice, |
| 194 | + Unsupported, |
| 195 | + /// Not mapped error code. |
| 196 | + Unknown(sys::hv_return_t), |
| 197 | +} |
| 198 | + |
| 199 | +impl error::Error for Error {} |
| 200 | + |
| 201 | +impl fmt::Display for Error { |
| 202 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 203 | + match self { |
| 204 | + Error::Unsuccessful => write!(f, "The operation was unsuccessful"), |
| 205 | + Error::Busy => write!(f, "The operation was unsuccessful because the owning resource was busy"), |
| 206 | + Error::BadArgument => write!(f, "The operation was unsuccessful because the function call had an invalid argument"), |
| 207 | + Error::NoResources => write!(f, "The operation was unsuccessful because the host had no resources available to complete the request"), |
| 208 | + Error::NoDevice => write!(f, "The operation was unsuccessful because no VM or vCPU was available"), |
| 209 | + Error::Unsupported => write!(f, "The operation requested isn’t supported by the hypervisor"), |
| 210 | + Error::Unknown(code) => write!(f, "Error code: {}", *code as i32), |
| 211 | + } |
| 212 | + } |
| 213 | +} |
| 214 | + |
| 215 | +impl From<sys::hv_return_t> for Error { |
| 216 | + fn from(value: sys::hv_return_t) -> Self { |
| 217 | + match value { |
| 218 | + sys::hv_return_t_HV_ERROR => Error::Unsuccessful, |
| 219 | + sys::hv_return_t_HV_BUSY => Error::Busy, |
| 220 | + sys::hv_return_t_HV_BAD_ARGUMENT => Error::BadArgument, |
| 221 | + sys::hv_return_t_HV_NO_RESOURCES => Error::NoResources, |
| 222 | + sys::hv_return_t_HV_NO_DEVICE => Error::NoDevice, |
| 223 | + sys::hv_return_t_HV_UNSUPPORTED => Error::Unsupported, |
| 224 | + _ => Error::Unknown(value), |
| 225 | + } |
| 226 | + } |
| 227 | +} |
0 commit comments