Skip to content

Commit b826b9a

Browse files
author
Simon Davies
committed
Adds support for Azure 3
1 parent 5de971c commit b826b9a

File tree

6 files changed

+115
-31
lines changed

6 files changed

+115
-31
lines changed

Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ repository = "https://github.com/hyperlight-dev/hyperlight"
3333
readme = "README.md"
3434

3535
[workspace.dependencies]
36-
mshv-bindings = { version = "=0.2.1" }
37-
mshv-ioctls = { version = "=0.2.1" }
3836

3937
hyperlight-common = { path = "src/hyperlight_common", version = "0.1.0", default-features = false }
4038
hyperlight-host = { path = "src/hyperlight_host", version = "0.1.0", default-features = false }

src/hyperlight_host/Cargo.toml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,12 @@ windows-result = "0.2"
7171

7272
[target.'cfg(unix)'.dependencies]
7373
seccompiler = { version = "0.4.0", optional = true }
74-
mshv-bindings = { workspace = true, optional = true }
75-
mshv-ioctls = { workspace = true, optional = true }
7674
kvm-bindings = { version = "0.10.0", features = ["fam-wrappers"], optional = true }
7775
kvm-ioctls = { version = "0.19.0", optional = true }
76+
mshv-bindings2 = { package="mshv-bindings", version = "=0.2.1", optional = true }
77+
mshv-ioctls2 = { package="mshv-ioctls", version = "=0.2.1", optional = true}
78+
mshv-bindings3 = { package="mshv-bindings", version = "0.3.1", optional = true }
79+
mshv-ioctls3 = { package="mshv-ioctls", version = "0.3.1", optional = true}
7880

7981
[dev-dependencies]
8082
signal-hook-registry = "1.4.1"
@@ -112,7 +114,7 @@ cfg_aliases = "0.2.1"
112114
built = { version = "0.7.0", features = ["chrono","git2"] }
113115

114116
[features]
115-
default = ["kvm", "mshv", "seccomp"]
117+
default = ["kvm", "mshv2", "seccomp"]
116118
seccomp = ["dep:seccompiler"]
117119
function_call_metrics = []
118120
executable_heap = []
@@ -122,7 +124,8 @@ print_debug = []
122124
# the name of the file is output to stdout and logged.
123125
dump_on_crash = ["dep:tempfile"]
124126
kvm = ["dep:kvm-bindings", "dep:kvm-ioctls"]
125-
mshv = ["dep:mshv-bindings", "dep:mshv-ioctls"]
127+
mshv2 = ["mshv-bindings2", "mshv-ioctls2"]
128+
mshv3 = ["mshv-bindings3", "mshv-ioctls3"]
126129
inprocess = []
127130

128131
[[bench]]

src/hyperlight_host/build.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ use anyhow::Result;
1818
use built::write_built_file;
1919

2020
fn main() -> Result<()> {
21+
// mshv2 and mshv3 features are mutually exclusive.
22+
#[cfg(all(feature = "mshv2", feature = "mshv3"))]
23+
panic!("mshv2 and mshv3 features are mutually exclusive");
24+
2125
// re-run the build if this script is changed (or deleted!),
2226
// even if the rust code is completely unchanged.
2327
println!("cargo:rerun-if-changed=build.rs");
@@ -85,12 +89,12 @@ fn main() -> Result<()> {
8589
}
8690

8791
// Makes #[cfg(kvm)] == #[cfg(all(feature = "kvm", target_os = "linux"))]
88-
// and #[cfg(mshv)] == #[cfg(all(feature = "mshv", target_os = "linux"))].
92+
// and #[cfg(mshv)] == #[cfg(all(any(feature = "mshv2", feature = "mshv3"), target_os = "linux"))].
8993
// Essentially the kvm and mshv features are ignored on windows as long as you use #[cfg(kvm)] and not #[cfg(feature = "kvm")].
9094
// You should never use #[cfg(feature = "kvm")] or #[cfg(feature = "mshv")] in the codebase.
9195
cfg_aliases::cfg_aliases! {
9296
kvm: { all(feature = "kvm", target_os = "linux") },
93-
mshv: { all(feature = "mshv", target_os = "linux") },
97+
mshv: { all(any(feature = "mshv2", feature = "mshv3"), target_os = "linux") },
9498
// inprocess feature is aliased with debug_assertions to make it only available in debug-builds.
9599
// You should never use #[cfg(feature = "inprocess")] in the codebase. Use #[cfg(inprocess)] instead.
96100
inprocess: { all(feature = "inprocess", debug_assertions) },

src/hyperlight_host/src/error.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17+
#[cfg(feature = "mshv2")]
18+
extern crate mshv_ioctls2 as mshv_ioctls;
19+
20+
#[cfg(feature = "mshv3")]
21+
extern crate mshv_ioctls3 as mshv_ioctls;
22+
1723
use std::array::TryFromSliceError;
1824
use std::cell::{BorrowError, BorrowMutError};
1925
use std::convert::Infallible;

src/hyperlight_host/src/hypervisor/hyperv_linux.rs

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,32 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17+
#[cfg(feature = "mshv2")]
18+
extern crate mshv_bindings2 as mshv_bindings;
19+
#[cfg(feature = "mshv2")]
20+
extern crate mshv_ioctls2 as mshv_ioctls;
21+
22+
#[cfg(feature = "mshv3")]
23+
extern crate mshv_bindings3 as mshv_bindings;
24+
#[cfg(feature = "mshv3")]
25+
extern crate mshv_ioctls3 as mshv_ioctls;
26+
1727
use std::fmt::{Debug, Formatter};
1828

1929
use log::error;
30+
#[cfg(feature = "mshv2")]
31+
use mshv_bindings::hv_message;
2032
use mshv_bindings::{
21-
hv_message, hv_message_type, hv_message_type_HVMSG_GPA_INTERCEPT,
22-
hv_message_type_HVMSG_UNMAPPED_GPA, hv_message_type_HVMSG_X64_HALT,
23-
hv_message_type_HVMSG_X64_IO_PORT_INTERCEPT, hv_register_assoc,
33+
hv_message_type, hv_message_type_HVMSG_GPA_INTERCEPT, hv_message_type_HVMSG_UNMAPPED_GPA,
34+
hv_message_type_HVMSG_X64_HALT, hv_message_type_HVMSG_X64_IO_PORT_INTERCEPT, hv_register_assoc,
2435
hv_register_name_HV_X64_REGISTER_RIP, hv_register_value, mshv_user_mem_region,
2536
FloatingPointUnit, SegmentRegister, SpecialRegisters, StandardRegisters,
2637
};
38+
#[cfg(feature = "mshv3")]
39+
use mshv_bindings::{
40+
hv_partition_property_code_HV_PARTITION_PROPERTY_SYNTHETIC_PROC_FEATURES,
41+
hv_partition_synthetic_processor_features,
42+
};
2743
use mshv_ioctls::{Mshv, VcpuFd, VmFd};
2844
use tracing::{instrument, Span};
2945

@@ -89,7 +105,19 @@ impl HypervLinuxDriver {
89105
}
90106
let mshv = Mshv::new()?;
91107
let pr = Default::default();
108+
#[cfg(feature = "mshv2")]
92109
let vm_fd = mshv.create_vm_with_config(&pr)?;
110+
#[cfg(feature = "mshv3")]
111+
let vm_fd = {
112+
let vm_fd = mshv.create_vm_with_args(&pr)?;
113+
let features: hv_partition_synthetic_processor_features = Default::default();
114+
vm_fd.hvcall_set_partition_property(
115+
hv_partition_property_code_HV_PARTITION_PROPERTY_SYNTHETIC_PROC_FEATURES,
116+
unsafe { features.as_uint64[0] },
117+
)?;
118+
vm_fd
119+
};
120+
93121
let mut vcpu_fd = vm_fd.create_vcpu(0)?;
94122

95123
mem_regions.iter().try_for_each(|region| {
@@ -283,8 +311,15 @@ impl Hypervisor for HypervLinuxDriver {
283311
const UNMAPPED_GPA_MESSAGE: hv_message_type = hv_message_type_HVMSG_UNMAPPED_GPA;
284312
const INVALID_GPA_ACCESS_MESSAGE: hv_message_type = hv_message_type_HVMSG_GPA_INTERCEPT;
285313

286-
let hv_message: hv_message = Default::default();
287-
let result = match &self.vcpu_fd.run(hv_message) {
314+
#[cfg(feature = "mshv2")]
315+
let run_result = {
316+
let hv_message: hv_message = Default::default();
317+
&self.vcpu_fd.run(hv_message)
318+
};
319+
#[cfg(feature = "mshv3")]
320+
let run_result = &self.vcpu_fd.run();
321+
322+
let result = match run_result {
288323
Ok(m) => match m.header.message_type {
289324
HALT_MESSAGE => {
290325
debug!("mshv - Halt Details : {:#?}", &self);

src/hyperlight_host/src/mem/memory_region.rs

Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,31 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17+
#[cfg(feature = "mshv2")]
18+
extern crate mshv_bindings2 as mshv_bindings;
19+
#[cfg(feature = "mshv2")]
20+
extern crate mshv_ioctls2 as mshv_ioctls;
21+
22+
#[cfg(feature = "mshv3")]
23+
extern crate mshv_bindings3 as mshv_bindings;
24+
#[cfg(feature = "mshv3")]
25+
extern crate mshv_ioctls3 as mshv_ioctls;
26+
1727
use std::ops::Range;
1828

1929
use bitflags::bitflags;
2030
#[cfg(mshv)]
2131
use hyperlight_common::mem::PAGE_SHIFT;
2232
use hyperlight_common::mem::PAGE_SIZE_USIZE;
2333
#[cfg(mshv)]
34+
use mshv_bindings::{hv_x64_memory_intercept_message, mshv_user_mem_region};
35+
#[cfg(feature = "mshv2")]
36+
use mshv_bindings::{
37+
HV_MAP_GPA_EXECUTABLE, HV_MAP_GPA_PERMISSIONS_NONE, HV_MAP_GPA_READABLE, HV_MAP_GPA_WRITABLE,
38+
};
39+
#[cfg(feature = "mshv3")]
2440
use mshv_bindings::{
25-
hv_x64_memory_intercept_message, mshv_user_mem_region, HV_MAP_GPA_EXECUTABLE,
26-
HV_MAP_GPA_PERMISSIONS_NONE, HV_MAP_GPA_READABLE, HV_MAP_GPA_WRITABLE,
41+
MSHV_SET_MEM_BIT_EXECUTABLE, MSHV_SET_MEM_BIT_UNMAP, MSHV_SET_MEM_BIT_WRITABLE,
2742
};
2843
#[cfg(target_os = "windows")]
2944
use windows::Win32::System::Hypervisor::{self, WHV_MEMORY_ACCESS_TYPE};
@@ -227,22 +242,45 @@ impl From<MemoryRegion> for mshv_user_mem_region {
227242
let guest_pfn = region.guest_region.start as u64 >> PAGE_SHIFT;
228243
let userspace_addr = region.host_region.start as u64;
229244

230-
let flags = region.flags.iter().fold(0, |acc, flag| {
231-
let flag_value = match flag {
232-
MemoryRegionFlags::NONE => HV_MAP_GPA_PERMISSIONS_NONE,
233-
MemoryRegionFlags::READ => HV_MAP_GPA_READABLE,
234-
MemoryRegionFlags::WRITE => HV_MAP_GPA_WRITABLE,
235-
MemoryRegionFlags::EXECUTE => HV_MAP_GPA_EXECUTABLE,
236-
_ => 0, // ignore any unknown flags
237-
};
238-
acc | flag_value
239-
});
240-
241-
mshv_user_mem_region {
242-
guest_pfn,
243-
size,
244-
userspace_addr,
245-
flags,
245+
#[cfg(feature = "mshv2")]
246+
{
247+
let flags = region.flags.iter().fold(0, |acc, flag| {
248+
let flag_value = match flag {
249+
MemoryRegionFlags::NONE => HV_MAP_GPA_PERMISSIONS_NONE,
250+
MemoryRegionFlags::READ => HV_MAP_GPA_READABLE,
251+
MemoryRegionFlags::WRITE => HV_MAP_GPA_WRITABLE,
252+
MemoryRegionFlags::EXECUTE => HV_MAP_GPA_EXECUTABLE,
253+
_ => 0, // ignore any unknown flags
254+
};
255+
acc | flag_value
256+
});
257+
mshv_user_mem_region {
258+
guest_pfn,
259+
size,
260+
userspace_addr,
261+
flags,
262+
}
263+
}
264+
#[cfg(feature = "mshv3")]
265+
{
266+
let flags: u8 = region.flags.iter().fold(0, |acc, flag| {
267+
let flag_value = match flag {
268+
MemoryRegionFlags::NONE => 1 << MSHV_SET_MEM_BIT_UNMAP,
269+
MemoryRegionFlags::READ => 0,
270+
MemoryRegionFlags::WRITE => 1 << MSHV_SET_MEM_BIT_WRITABLE,
271+
MemoryRegionFlags::EXECUTE => 1 << MSHV_SET_MEM_BIT_EXECUTABLE,
272+
_ => 0, // ignore any unknown flags
273+
};
274+
acc | flag_value
275+
});
276+
277+
mshv_user_mem_region {
278+
guest_pfn,
279+
size,
280+
userspace_addr,
281+
flags,
282+
..Default::default()
283+
}
246284
}
247285
}
248286
}

0 commit comments

Comments
 (0)