Skip to content

Commit 79f1c81

Browse files
committed
Move vm calls under Vm struct
Signed-off-by: Maksym Pavlenko <pavlenko.maksym@gmail.com>
1 parent 269219e commit 79f1c81

3 files changed

Lines changed: 125 additions & 112 deletions

File tree

hv/examples/caps.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
fn main() {
2-
hv::vm_create(hv::VmOptions::default()).unwrap();
1+
fn main() -> Result<(), hv::Error> {
2+
hv::Vm::create(hv::VmOptions::default())?;
33

44
println!(
55
"Max vCPUs: {}",
6-
hv::capability(hv::Capability::VCPU_MAX).unwrap()
6+
hv::Vm::capability(hv::Capability::VCPU_MAX)?
77
);
88

99
println!(
1010
"Available address spaces: {}",
11-
hv::capability(hv::Capability::ADDR_SPAC_EMAX).unwrap()
11+
hv::Vm::capability(hv::Capability::ADDR_SPAC_EMAX)?
1212
);
1313

14-
hv::vm_destroy().unwrap();
14+
hv::Vm::destroy()?;
15+
16+
Ok(())
1517
}

hv/src/lib.rs

Lines changed: 117 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -70,123 +70,134 @@ impl Default for VmOptions {
7070
}
7171
}
7272

73-
/// Creates a VM instance for the current process.
74-
pub fn vm_create(options: VmOptions) -> Result<(), Error> {
75-
call!(sys::hv_vm_create(options.bits))
76-
}
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+
}
7782

78-
/// Gets the value of capabilities of the system.
79-
pub fn capability(cap: Capability) -> Result<u64, Error> {
80-
let mut out = 0_u64;
81-
call!(sys::hv_capability(cap.bits as u64, &mut out))?;
82-
Ok(out)
83-
}
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+
}
8489

85-
/// Creates an additional guest address space for the current task.
86-
#[cfg(feature = "hv_10_15")]
87-
pub fn vm_space_create() -> Result<Space, Error> {
88-
let mut space: Space = 0;
89-
call!(sys::hv_vm_space_create(&mut space))?;
90-
Ok(space)
91-
}
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+
}
9297

93-
/// Destroys the address space instance associated with the current task.
94-
///
95-
/// # Arguments
96-
/// * `asid` - Address space ID
97-
#[cfg(feature = "hv_10_15")]
98-
pub fn vm_space_destroy(asid: Space) -> Result<(), Error> {
99-
call!(sys::hv_vm_space_destroy(asid))
100-
}
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+
}
101106

102-
/// Maps a region in the virtual address space of the current task into the guest physical
103-
/// address space of the VM.
104-
///
105-
/// # Arguments
106-
/// * `uva` - Page aligned virtual address in the current task.
107-
/// * `gpa` - Page aligned address in the guest physical address space.
108-
/// * `size` - Size in bytes of the region to be mapped.
109-
/// * `flags` - READ, WRITE and EXECUTE permissions of the region
110-
pub fn vm_map(uva: UVAddr, gpa: GPAddr, size: u64, flags: Memory) -> Result<(), Error> {
111-
call!(sys::hv_vm_map(uva, gpa, size, flags.bits.into()))
112-
}
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+
}
113118

114-
/// Unmaps a region in the guest physical address space of the VM
115-
///
116-
/// # Arguments
117-
/// * `gpa` - Page aligned address in the guest physical address space.
118-
/// * `size` - Size in bytes of the region to be unmapped.
119-
pub fn vm_unmap(gpa: GPAddr, size: u64) -> Result<(), Error> {
120-
call!(sys::hv_vm_unmap(gpa, size))
121-
}
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+
}
122127

123-
/// Modifies the permissions of a region in the guest physical address space of the VM.
124-
///
125-
/// # Arguments
126-
/// * `gpa` - Page aligned address in the guest physical address space.
127-
/// * `size` - Size in bytes of the region to be modified.
128-
/// * `flags` - New READ, WRITE and EXECUTE permissions of the region.
129-
pub fn vm_protect(gpa: GPAddr, size: u64, flags: Memory) -> Result<(), Error> {
130-
call!(sys::hv_vm_protect(gpa, size, flags.bits.into()))
131-
}
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+
}
132137

133-
/// Maps a region in the virtual address space of the current task
134-
/// into a guest physical address space of the VM.
135-
///
136-
/// # Arguments
137-
/// * `asid` - Address space ID.
138-
/// * `uva` - Page aligned virtual address in the current task.
139-
/// * `gpa` - Page aligned address in the guest physical address space.
140-
/// * `size` - Size in bytes of the region to be mapped.
141-
/// * `flags` - READ, WRITE and EXECUTE permissions of the region.
142-
#[cfg(feature = "hv_10_15")]
143-
pub fn vm_map_space(
144-
asid: Space,
145-
uva: UVAddr,
146-
gpa: GPAddr,
147-
size: u64,
148-
flags: Memory,
149-
) -> Result<(), Error> {
150-
call!(sys::hv_vm_map_space(
151-
asid,
152-
uva,
153-
gpa,
154-
size,
155-
flags.bits.into()
156-
))
157-
}
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+
}
158163

159-
/// Unmaps 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 unmapped.
165-
#[cfg(feature = "hv_10_15")]
166-
pub fn vm_unmap_space(asid: Space, gpa: GPAddr, size: u64) -> Result<(), Error> {
167-
call!(sys::hv_vm_unmap_space(asid, gpa, size))
168-
}
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+
}
169174

170-
/// Modifies the permissions of a region in a guest physical address space of the VM.
171-
///
172-
/// # Arguments
173-
/// * `asid` - Address space ID.
174-
/// * `gpa` - Page aligned address in the guest physical address space.
175-
/// * `size` - Size in bytes of the region to be modified.
176-
/// * `flags` - New READ, WRITE and EXECUTE permissions of the region.
177-
#[cfg(feature = "hv_10_15")]
178-
pub fn vm_protect_space(asid: Space, gpa: GPAddr, size: u64, flags: Memory) -> Result<(), Error> {
179-
call!(sys::hv_vm_protect_space(asid, gpa, size, flags.bits.into()))
180-
}
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+
}
181186

182-
/// Synchronizes guest TSC across all vCPUs.
183-
pub fn vm_sync_tsc(tcs: u64) -> Result<(), Error> {
184-
call!(sys::hv_vm_sync_tsc(tcs))
185-
}
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+
}
186191

187-
/// Destroys the VM instance associated with the current process.
188-
pub fn vm_destroy() -> Result<(), Error> {
189-
call!(sys::hv_vm_destroy())
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+
}
190201
}
191202

192203
/// The return type of framework functions.

hv/src/vcpu.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub struct Vcpu(sys::hv_vcpuid_t);
1111

1212
impl Vcpu {
1313
/// Creates a vCPU instance for the current thread.
14-
pub fn new() -> Result<Vcpu, Error> {
14+
pub(crate) fn new() -> Result<Vcpu, Error> {
1515
let mut handle: sys::hv_vcpuid_t = 0;
1616
call!(sys::hv_vcpu_create(&mut handle, 0))?;
1717
Ok(Vcpu(handle))

0 commit comments

Comments
 (0)