Skip to content

Commit 3b0660e

Browse files
committed
Make room for arch64 target
Signed-off-by: Maksym Pavlenko <pavlenko.maksym@gmail.com>
1 parent 79f1c81 commit 3b0660e

8 files changed

Lines changed: 527 additions & 399 deletions

File tree

hv-sys/wrapper.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,11 @@
1+
#if defined(__arm64__)
2+
3+
// Newer version of Hypervisor.Framework (the ones with ARM support) includes more convenient `Hypervisor.h`
4+
#include "Hypervisor/Hypervisor.h"
5+
6+
#elif defined(__x86_64__)
7+
18
#include "Hypervisor/hv.h"
29
#include "Hypervisor/hv_vmx.h"
10+
11+
#endif

hv/examples/caps.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1+
use hv::x86::{Capability, VmExt, VmOptions};
2+
13
fn main() -> Result<(), hv::Error> {
2-
hv::Vm::create(hv::VmOptions::default())?;
4+
hv::Vm::create(VmOptions::default())?;
35

4-
println!(
5-
"Max vCPUs: {}",
6-
hv::Vm::capability(hv::Capability::VCPU_MAX)?
7-
);
6+
println!("Max vCPUs: {}", hv::Vm::capability(Capability::VcpuMax)?);
87

98
println!(
109
"Available address spaces: {}",
11-
hv::Vm::capability(hv::Capability::ADDR_SPAC_EMAX)?
10+
hv::Vm::capability(Capability::AddrSpaceMax)?
1211
);
1312

1413
hv::Vm::destroy()?;

hv/src/lib.rs

Lines changed: 5 additions & 179 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ use std::fmt;
55

66
/// Low level access to generated bindings.
77
pub use hv_sys as sys;
8+
pub use vcpu::Vcpu;
9+
pub use vm::Vm;
810

911
mod vcpu;
10-
pub mod vmx;
11-
pub mod x86;
12+
pub mod vm;
1213

13-
pub use vcpu::Vcpu;
14+
#[cfg(target_arch = "x86_64")]
15+
pub mod x86;
1416

1517
/// Helper macro to call unsafe Hypervisor functions and map returned error codes to [Error] type.
1618
#[macro_export]
@@ -24,182 +26,6 @@ macro_rules! call {
2426
}};
2527
}
2628

27-
bitflags::bitflags! {
28-
/// The type of system capabilities.
29-
pub struct Capability: u32 {
30-
const VCPU_MAX = sys::HV_CAP_VCPUMAX;
31-
const ADDR_SPAC_EMAX = sys::HV_CAP_ADDRSPACEMAX;
32-
}
33-
}
34-
35-
/// Type of a user virtual address.
36-
pub type UVAddr = sys::hv_uvaddr_t;
37-
38-
/// Type of a guest physical address.
39-
pub type GPAddr = sys::hv_gpaddr_t;
40-
41-
/// Type of a guest address space.
42-
pub type Space = sys::hv_vm_space_t;
43-
44-
pub const VM_SPACE_DEFAULT: Space = sys::HV_VM_SPACE_DEFAULT;
45-
46-
bitflags::bitflags! {
47-
/// Guest physical memory region permissions.
48-
pub struct Memory: u32 {
49-
const READ = sys::HV_MEMORY_READ;
50-
const WRITE = sys::HV_MEMORY_WRITE;
51-
const EXEC = sys::HV_MEMORY_EXEC;
52-
}
53-
}
54-
55-
bitflags::bitflags! {
56-
pub struct VmOptions: u64 {
57-
const DEFAULT = sys::HV_VM_DEFAULT as _;
58-
const SPECIFY_MITIGATIONS = sys::HV_VM_SPECIFY_MITIGATIONS as _;
59-
const MITIGATION_A_ENABLE = sys::HV_VM_MITIGATION_A_ENABLE as _;
60-
const MITIGATION_B_ENABLE = sys::HV_VM_MITIGATION_B_ENABLE as _;
61-
const MITIGATION_C_ENABLE = sys::HV_VM_MITIGATION_C_ENABLE as _;
62-
const MITIGATION_D_ENABLE = sys::HV_VM_MITIGATION_D_ENABLE as _;
63-
const MITIGATION_E_ENABLE = sys::HV_VM_MITIGATION_E_ENABLE as _;
64-
}
65-
}
66-
67-
impl Default for VmOptions {
68-
fn default() -> Self {
69-
VmOptions::DEFAULT
70-
}
71-
}
72-
73-
/// Vm is an entry point to Hypervisor Framework.
74-
#[derive(Debug)]
75-
pub struct Vm;
76-
77-
impl Vm {
78-
/// Creates a VM instance for the current process.
79-
pub fn create(options: VmOptions) -> Result<(), Error> {
80-
call!(sys::hv_vm_create(options.bits))
81-
}
82-
83-
/// Gets the value of capabilities of the system.
84-
pub fn capability(cap: Capability) -> Result<u64, Error> {
85-
let mut out = 0_u64;
86-
call!(sys::hv_capability(cap.bits as u64, &mut out))?;
87-
Ok(out)
88-
}
89-
90-
/// Creates an additional guest address space for the current task.
91-
#[cfg(feature = "hv_10_15")]
92-
pub fn space_create() -> Result<Space, Error> {
93-
let mut space: Space = 0;
94-
call!(sys::hv_vm_space_create(&mut space))?;
95-
Ok(space)
96-
}
97-
98-
/// Destroys the address space instance associated with the current task.
99-
///
100-
/// # Arguments
101-
/// * `asid` - Address space ID
102-
#[cfg(feature = "hv_10_15")]
103-
pub fn space_destroy(asid: Space) -> Result<(), Error> {
104-
call!(sys::hv_vm_space_destroy(asid))
105-
}
106-
107-
/// Maps a region in the virtual address space of the current task into the guest physical
108-
/// address space of the VM.
109-
///
110-
/// # Arguments
111-
/// * `uva` - Page aligned virtual address in the current task.
112-
/// * `gpa` - Page aligned address in the guest physical address space.
113-
/// * `size` - Size in bytes of the region to be mapped.
114-
/// * `flags` - READ, WRITE and EXECUTE permissions of the region
115-
pub fn map(uva: UVAddr, gpa: GPAddr, size: u64, flags: Memory) -> Result<(), Error> {
116-
call!(sys::hv_vm_map(uva, gpa, size, flags.bits.into()))
117-
}
118-
119-
/// Unmaps a region in the guest physical address space of the VM
120-
///
121-
/// # Arguments
122-
/// * `gpa` - Page aligned address in the guest physical address space.
123-
/// * `size` - Size in bytes of the region to be unmapped.
124-
pub fn unmap(gpa: GPAddr, size: u64) -> Result<(), Error> {
125-
call!(sys::hv_vm_unmap(gpa, size))
126-
}
127-
128-
/// Modifies the permissions of a region in the guest physical address space of the VM.
129-
///
130-
/// # Arguments
131-
/// * `gpa` - Page aligned address in the guest physical address space.
132-
/// * `size` - Size in bytes of the region to be modified.
133-
/// * `flags` - New READ, WRITE and EXECUTE permissions of the region.
134-
pub fn protect(gpa: GPAddr, size: u64, flags: Memory) -> Result<(), Error> {
135-
call!(sys::hv_vm_protect(gpa, size, flags.bits.into()))
136-
}
137-
138-
/// Maps a region in the virtual address space of the current task
139-
/// into a guest physical address space of the VM.
140-
///
141-
/// # Arguments
142-
/// * `asid` - Address space ID.
143-
/// * `uva` - Page aligned virtual address in the current task.
144-
/// * `gpa` - Page aligned address in the guest physical address space.
145-
/// * `size` - Size in bytes of the region to be mapped.
146-
/// * `flags` - READ, WRITE and EXECUTE permissions of the region.
147-
#[cfg(feature = "hv_10_15")]
148-
pub fn map_space(
149-
asid: Space,
150-
uva: UVAddr,
151-
gpa: GPAddr,
152-
size: u64,
153-
flags: Memory,
154-
) -> Result<(), Error> {
155-
call!(sys::hv_vm_map_space(
156-
asid,
157-
uva,
158-
gpa,
159-
size,
160-
flags.bits.into()
161-
))
162-
}
163-
164-
/// Unmaps a region in a guest physical address space of the VM.
165-
///
166-
/// # Arguments
167-
/// * `asid` - Address space ID.
168-
/// * `gpa` - Page aligned address in the guest physical address space.
169-
/// * `size` - Size in bytes of the region to be unmapped.
170-
#[cfg(feature = "hv_10_15")]
171-
pub fn unmap_space(asid: Space, gpa: GPAddr, size: u64) -> Result<(), Error> {
172-
call!(sys::hv_vm_unmap_space(asid, gpa, size))
173-
}
174-
175-
/// Modifies the permissions of a region in a guest physical address space of the VM.
176-
///
177-
/// # Arguments
178-
/// * `asid` - Address space ID.
179-
/// * `gpa` - Page aligned address in the guest physical address space.
180-
/// * `size` - Size in bytes of the region to be modified.
181-
/// * `flags` - New READ, WRITE and EXECUTE permissions of the region.
182-
#[cfg(feature = "hv_10_15")]
183-
pub fn protect_space(asid: Space, gpa: GPAddr, size: u64, flags: Memory) -> Result<(), Error> {
184-
call!(sys::hv_vm_protect_space(asid, gpa, size, flags.bits.into()))
185-
}
186-
187-
/// Synchronizes guest TSC across all vCPUs.
188-
pub fn sync_tsc(tcs: u64) -> Result<(), Error> {
189-
call!(sys::hv_vm_sync_tsc(tcs))
190-
}
191-
192-
/// Creates a vCPU instance for the current thread.
193-
pub fn create_cpu() -> Result<Vcpu, Error> {
194-
Vcpu::new()
195-
}
196-
197-
/// Destroys the VM instance associated with the current process.
198-
pub fn destroy() -> Result<(), Error> {
199-
call!(sys::hv_vm_destroy())
200-
}
201-
}
202-
20329
/// The return type of framework functions.
20430
/// Wraps the underlying `hv_return_t` type.
20531
#[derive(Debug, Copy, Clone, Eq, PartialEq)]

0 commit comments

Comments
 (0)