|
1 | 1 | use crate::{call, sys, Error}; |
2 | 2 |
|
3 | | -#[cfg(target_arch = "x86_64")] |
4 | | -type Handle = sys::hv_vcpuid_t; |
5 | | -#[cfg(target_arch = "aarch64")] |
6 | | -type Handle = sys::hv_vcpu_t; |
7 | | - |
8 | 3 | /// Represents a single virtual CPU. |
9 | 4 | /// |
10 | 5 | /// [Vcpu] object is not thread safe, all calls must be performed from |
11 | 6 | /// the owning thread. |
12 | | -pub struct Vcpu(pub(crate) Handle); |
| 7 | +pub struct Vcpu { |
| 8 | + #[cfg(target_arch = "x86_64")] |
| 9 | + pub(crate) cpu: sys::hv_vcpuid_t, |
| 10 | + #[cfg(target_arch = "aarch64")] |
| 11 | + pub(crate) cpu: sys::hv_vcpu_t, |
| 12 | + #[cfg(target_arch = "aarch64")] |
| 13 | + /// The pointer to the vCPU exit information. |
| 14 | + /// The function `hv_vcpu_run` updates this structure on return. |
| 15 | + /// Apple silicon only. |
| 16 | + pub(crate) exit: *const sys::hv_vcpu_exit_t, |
| 17 | +} |
13 | 18 |
|
14 | 19 | impl Vcpu { |
15 | 20 | /// Creates a vCPU instance for the current thread. |
16 | 21 | pub(crate) fn new() -> Result<Vcpu, Error> { |
17 | | - let mut handle = 0; |
18 | 22 | #[cfg(target_arch = "x86_64")] |
19 | | - call!(sys::hv_vcpu_create(&mut handle, 0))?; |
20 | | - Ok(Vcpu(handle)) |
| 23 | + { |
| 24 | + let mut cpu = 0; |
| 25 | + call!(sys::hv_vcpu_create(&mut cpu, 0))?; |
| 26 | + Ok(Vcpu { cpu }) |
| 27 | + } |
| 28 | + |
| 29 | + #[cfg(target_arch = "aarch64")] |
| 30 | + { |
| 31 | + let mut cpu = 0; |
| 32 | + let mut exit = std::ptr::null_mut(); |
| 33 | + call!(sys::hv_vcpu_create( |
| 34 | + &mut cpu, |
| 35 | + &mut exit, |
| 36 | + std::ptr::null_mut() |
| 37 | + ))?; |
| 38 | + Ok(Vcpu { cpu, exit }) |
| 39 | + } |
21 | 40 | } |
22 | 41 |
|
23 | 42 | /// Executes a vCPU. |
| 43 | + /// |
| 44 | + /// Call blocks until the next exit of the vCPU [1]. |
| 45 | + /// The owning thread must call this function. |
| 46 | + /// |
| 47 | + /// # Intel |
| 48 | + /// On an Intel-based Mac, `hv_vcpu_run` exits from causes external to the guest. |
| 49 | + /// To avoid the overhead of spurious exits use `hv_vcpu_run_until` with the deadline `HV_DEADLINE_FOREVER`. |
| 50 | + /// |
| 51 | + /// # Apple Silicon |
| 52 | + /// If the exit is of type `HV_EXIT_REASON_VTIMER_ACTIVATED`, the VTimer is automatically masked. |
| 53 | + /// As a result, no timer fires until the timer is unmasked with `hv_vcpu_set_vtimer_mask`. |
| 54 | + /// |
| 55 | + /// [1]: https://developer.apple.com/documentation/hypervisor/1441231-hv_vcpu_run |
24 | 56 | pub fn run(&self) -> Result<(), Error> { |
25 | | - call!(sys::hv_vcpu_run(self.0)) |
26 | | - } |
27 | | - |
28 | | - /// Executes a vCPU until the given deadline. |
29 | | - #[cfg(feature = "hv_10_15")] |
30 | | - pub fn run_until(&self, deadline: u64) -> Result<(), Error> { |
31 | | - call!(sys::hv_vcpu_run_until(self.0, deadline)) |
| 57 | + call!(sys::hv_vcpu_run(self.cpu)) |
32 | 58 | } |
33 | 59 |
|
34 | 60 | /// Returns the cumulative execution time of a vCPU in nanoseconds. |
35 | 61 | pub fn exec_time(&self) -> Result<u64, Error> { |
36 | 62 | let mut out = 0_u64; |
37 | | - call!(sys::hv_vcpu_get_exec_time(self.0, &mut out))?; |
| 63 | + call!(sys::hv_vcpu_get_exec_time(self.cpu, &mut out))?; |
38 | 64 | Ok(out) |
39 | 65 | } |
40 | 66 | } |
41 | 67 |
|
42 | 68 | impl Drop for Vcpu { |
43 | 69 | /// Destroys the vCPU instance associated with the current thread. |
44 | 70 | fn drop(&mut self) { |
45 | | - let _ = call!(sys::hv_vcpu_destroy(self.0)); |
| 71 | + let _ = call!(sys::hv_vcpu_destroy(self.cpu)); |
46 | 72 | } |
47 | 73 | } |
0 commit comments