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 thiserror::Error;
58
59#[cfg(target_arch = "x86_64")]
60use self::x86_64::ffi::*;
61#[cfg(target_arch = "aarch64")]
62use aarch64::ffi::*;
63#[cfg(target_arch = "aarch64")]
64pub use aarch64::*;
65#[cfg(target_arch = "x86_64")]
66pub use x86_64::*;
67
68/// Error returned after every call
69#[derive(Error, Debug)]
70pub enum Error {
71	#[error("success")]
72	Success,
73	#[error("error")]
74	Error,
75	#[error("busy")]
76	Busy,
77	#[error("bad argument")]
78	BadArg,
79	#[error("no resource")]
80	NoRes,
81	#[error("no device")]
82	NoDev,
83	#[error("unsupported")]
84	Unsupp,
85}
86
87// Returns an Error for a hv_return_t
88fn match_error_code(code: hv_return_t) -> Result<(), Error> {
89	match code {
90		HV_SUCCESS => Ok(()),
91		HV_BUSY => Err(Error::Busy),
92		HV_BAD_ARGUMENT => Err(Error::BadArg),
93		HV_NO_RESOURCES => Err(Error::NoRes),
94		HV_NO_DEVICE => Err(Error::NoDev),
95		HV_UNSUPPORTED => Err(Error::Unsupp),
96		_ => Err(Error::Error),
97	}
98}
99
100/// Destroys the VM instance associated with the current Mach task
101pub fn destroy_vm() -> Result<(), Error> {
102	match_error_code(unsafe { hv_vm_destroy() })
103}
104
105/// Guest physical memory region permissions
106#[derive(Debug)]
107pub enum MemPerm {
108	/// No access
109	None,
110	/// Read
111	Read,
112	/// Write
113	Write,
114	/// Read and write access
115	ReadWrite,
116	/// Execute
117	Exec,
118	/// Execute and write
119	ExecWrite,
120	/// Execute and read
121	ExecRead,
122	/// Execute, read and write
123	ExecReadWrite,
124}
125
126#[allow(non_snake_case)]
127#[inline(always)]
128fn match_MemPerm(mem_perm: MemPerm) -> u64 {
129	match mem_perm {
130		MemPerm::None => 0,
131		MemPerm::Read => HV_MEMORY_READ,
132		MemPerm::Write => HV_MEMORY_WRITE,
133		MemPerm::ReadWrite => HV_MEMORY_WRITE | HV_MEMORY_READ,
134		MemPerm::Exec => HV_MEMORY_EXEC,
135		MemPerm::ExecWrite => HV_MEMORY_EXEC | HV_MEMORY_WRITE,
136		MemPerm::ExecRead => HV_MEMORY_EXEC | HV_MEMORY_READ,
137		MemPerm::ExecReadWrite => HV_MEMORY_EXEC | HV_MEMORY_READ | HV_MEMORY_WRITE,
138	}
139}
140
141impl VirtualCpu {
142	/// Destroys the VirtualCpu instance associated with the current thread
143	pub fn destroy(&self) -> Result<(), Error> {
144		match_error_code(unsafe { hv_vcpu_destroy(self.get_handle()) })
145	}
146
147	/// Executes the VirtualCpu
148	pub fn run(&self) -> Result<(), Error> {
149		match_error_code(unsafe { hv_vcpu_run(self.get_handle()) })
150	}
151}