xhypervisor/
lib.rs

1/*
2Copyright (c) 2016 Saurav Sachidanand
3			  2021 Stefan Lankes
4
5Permission is hereby granted, free of charge, to any person obtaining a copy
6of this software and associated documentation files (the "Software"), to deal
7in the Software without restriction, including without limitation the rights
8to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9copies of the Software, and to permit persons to whom the Software is
10furnished to do so, subject to the following conditions:
11
12The above copyright notice and this permission notice shall be included in
13all copies or substantial portions of the Software.
14
15THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21THE SOFTWARE.
22*/
23
24/*!
25This is a Rust library that taps into functionality that enables
26hardware-accelerated execution of virtual machines on OS X.
27
28It binds to the `Hypervisor` framework on OS X, and exposes a safe Rust
29interface through the `xhypervisor` module, and an unsafe foreign function
30interface through the `xhypervisor::ffi` module.
31
32To use this library, you need
33
34* OS X Yosemite (10.10), or newer
35
36* an Intel processor with the VT-x feature set that includes Extended Page
37Tables (EPT) and Unrestricted Mode. To verify this, run and expect the following
38in your Terminal:
39
40  ```shell
41  $ sysctl kern.hv_support
42  kern.hv_support: 1
43  ```
44!*/
45
46extern crate core;
47extern crate libc;
48extern crate thiserror;
49
50#[cfg(target_arch = "aarch64")]
51#[allow(non_camel_case_types)]
52pub mod aarch64;
53#[cfg(target_arch = "x86_64")]
54#[allow(non_camel_case_types)]
55pub mod x86_64;
56
57use core::fmt;
58use thiserror::Error;
59
60#[cfg(target_arch = "x86_64")]
61use self::x86_64::ffi::*;
62#[cfg(target_arch = "aarch64")]
63use aarch64::ffi::*;
64#[cfg(target_arch = "aarch64")]
65pub use aarch64::*;
66#[cfg(target_arch = "x86_64")]
67pub use x86_64::*;
68
69/// Error returned after every call
70#[derive(Error, Debug)]
71pub enum Error {
72	#[error("success")]
73	Success,
74	#[error("error")]
75	Error,
76	#[error("busy")]
77	Busy,
78	#[error("bad argument")]
79	BadArg,
80	#[error("no resource")]
81	NoRes,
82	#[error("no device")]
83	NoDev,
84	#[error("unsupported")]
85	Unsupp,
86}
87
88// Returns an Error for a hv_return_t
89fn match_error_code(code: hv_return_t) -> Result<(), Error> {
90	match code {
91		HV_SUCCESS => Ok(()),
92		HV_BUSY => Err(Error::Busy),
93		HV_BAD_ARGUMENT => Err(Error::BadArg),
94		HV_NO_RESOURCES => Err(Error::NoRes),
95		HV_NO_DEVICE => Err(Error::NoDev),
96		HV_UNSUPPORTED => Err(Error::Unsupp),
97		_ => Err(Error::Error),
98	}
99}
100
101/// Destroys the VM instance associated with the current Mach task
102pub fn destroy_vm() -> Result<(), Error> {
103	match_error_code(unsafe { hv_vm_destroy() })
104}
105
106/// Guest physical memory region permissions
107#[derive(Debug)]
108pub enum MemPerm {
109	/// Read
110	Read,
111	/// Write (implies read)
112	Write,
113	/// Execute
114	Exec,
115	/// Execute and write (implies read)
116	ExecAndWrite,
117	/// Execute and read
118	ExecAndRead,
119}
120
121#[allow(non_snake_case)]
122#[inline(always)]
123fn match_MemPerm(mem_perm: MemPerm) -> u64 {
124	match mem_perm {
125		MemPerm::Read => HV_MEMORY_READ,
126		MemPerm::Write => HV_MEMORY_WRITE | HV_MEMORY_READ,
127		MemPerm::Exec => HV_MEMORY_EXEC,
128		MemPerm::ExecAndWrite => HV_MEMORY_EXEC | HV_MEMORY_WRITE | HV_MEMORY_READ,
129		MemPerm::ExecAndRead => HV_MEMORY_EXEC | HV_MEMORY_READ,
130	}
131}
132
133impl VirtualCpu {
134	/// Destroys the VirtualCpu instance associated with the current thread
135	pub fn destroy(&self) -> Result<(), Error> {
136		match_error_code(unsafe { hv_vcpu_destroy(self.get_id()) })
137	}
138
139	/// Executes the VirtualCpu
140	pub fn run(&self) -> Result<(), Error> {
141		match_error_code(unsafe { hv_vcpu_run(self.get_id()) })
142	}
143}
144
145impl fmt::Debug for VirtualCpu {
146	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
147		write!(f, "VirtualCpu ID: {}", (*self).get_id())
148	}
149}