openhcl_boot/arch/x86_64/
hypercall.rs1use core::ptr::addr_of;
5use hvdef::HV_PAGE_SIZE;
6use hvdef::hypercall::HvGuestOsMicrosoft;
7use minimal_rt::arch::hypercall::HYPERCALL_PAGE;
8use minimal_rt::arch::msr::read_msr;
9use minimal_rt::arch::msr::write_msr;
10
11fn report_os_id(guest_os_id: HvGuestOsMicrosoft) {
12 unsafe {
14 write_msr(hvdef::HV_X64_MSR_GUEST_OS_ID, guest_os_id.into());
15 };
16}
17
18fn write_hypercall_msr(enable: bool) {
20 let hypercall_contents = hvdef::hypercall::MsrHypercallContents::from(unsafe {
22 read_msr(hvdef::HV_X64_MSR_HYPERCALL)
23 });
24
25 let hypercall_page_num = addr_of!(HYPERCALL_PAGE) as u64 / HV_PAGE_SIZE;
26
27 assert!(
28 !enable || !hypercall_contents.enable(),
29 "{:?}",
30 hypercall_contents
31 );
32 let new_hv_contents = hypercall_contents.with_enable(enable).with_gpn(if enable {
33 hypercall_page_num
34 } else {
35 0
36 });
37
38 unsafe { write_msr(hvdef::HV_X64_MSR_HYPERCALL, new_hv_contents.into()) };
40}
41
42pub(crate) fn initialize(guest_os_id: HvGuestOsMicrosoft) {
44 report_os_id(guest_os_id);
47 write_hypercall_msr(true);
48}
49
50pub(crate) fn uninitialize() {
52 write_hypercall_msr(false);
53 report_os_id(0.into());
54}