Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add various helper function required by SEV-SNP guest #107

Merged
merged 3 commits into from
Jul 7, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions mshv-bindings/src/snp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
non_upper_case_globals
)]
use crate::bindings::*;
use vmm_sys_util::errno;

type Result<T> = std::result::Result<T, errno::Error>;

pub const GHCB_PROTOCOL_VERSION_MIN: u32 = 1;
pub const GHCB_PROTOCOL_VERSION_MAX: u32 = 2;
Expand Down Expand Up @@ -658,3 +661,59 @@ pub fn get_default_vmgexit_offload_features() -> hv_sev_vmgexit_offload {

offload_feature
}

///
/// Helper function to parse the GPA range
///
pub fn parse_gpa_range(range: hv_gpa_page_range) -> Result<(u64, u64)> {
let gpa_page_start;
let gpa_page_count;

// SAFETY: access union field
unsafe {
if range.page.largepage() > 0 {
return Err(errno::Error::new(libc::EINVAL));
} else {
gpa_page_start = range.page.basepfn();
gpa_page_count = 1 + range.page.additional_pages();
}
}
Ok((gpa_page_start, gpa_page_count))
}

///
/// Get default SEV-SNP guest policy supported by
/// Microsoft Hypervisor.
///
pub fn get_default_snp_guest_policy() -> hv_snp_guest_policy {
let mut snp_policy = hv_snp_guest_policy { as_uint64: 0_u64 };

// SAFETY: access union field
unsafe {
snp_policy.__bindgen_anon_1.set_minor_version(0x1f);
snp_policy.__bindgen_anon_1.set_major_version(0x00);
snp_policy.__bindgen_anon_1.set_smt_allowed(1);
snp_policy.__bindgen_anon_1.set_vmpls_required(1);
snp_policy.__bindgen_anon_1.set_migration_agent_allowed(0);
snp_policy.__bindgen_anon_1.set_debug_allowed(0);
}

snp_policy
}

///
/// Helper function to get sev control register
/// for a given VMSA PFN.
///
pub fn get_sev_control_register(vmsa_pfn: u64) -> u64 {
let mut sev_control = hv_x64_register_sev_control { as_uint64: 0_u64 };

// SAFETY: access union field
unsafe {
sev_control.__bindgen_anon_1.set_enable_encrypted_state(1);
sev_control
.__bindgen_anon_1
.set_vmsa_gpa_page_number(vmsa_pfn);
sev_control.as_uint64
}
}
Loading