mshv_bindings/x86_64/unmarshal.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
// Copyright © 2020, Microsoft Corporation
//
// SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause
//
use crate::bindings::*;
use vmm_sys_util::errno;
type Result<T> = std::result::Result<T, errno::Error>;
// hv_message implementation for unmarshaling payload
impl hv_message {
#[inline]
pub fn to_cpuid_info(&self) -> Result<hv_x64_cpuid_intercept_message> {
if self.header.message_type != hv_message_type_HVMSG_X64_CPUID_INTERCEPT {
return Err(errno::Error::new(libc::EINVAL));
}
// SAFETY: We know at this point the payload is of the correct type. The payload field is
// unaligned. We use addr_of! to safely create a pointer, then call read_unaligned for
// copying its content out.
let ret =
unsafe { std::ptr::read_unaligned(std::ptr::addr_of!(self.u.payload) as *const _) };
Ok(ret)
}
#[inline]
pub fn to_memory_info(&self) -> Result<hv_x64_memory_intercept_message> {
if self.header.message_type != hv_message_type_HVMSG_GPA_INTERCEPT
&& self.header.message_type != hv_message_type_HVMSG_UNMAPPED_GPA
&& self.header.message_type != hv_message_type_HVMSG_UNACCEPTED_GPA
{
return Err(errno::Error::new(libc::EINVAL));
}
// SAFETY: We know at this point the payload is of the correct type. The payload field is
// unaligned. We use addr_of! to safely create a pointer, then call read_unaligned for
// copying its content out.
let ret =
unsafe { std::ptr::read_unaligned(std::ptr::addr_of!(self.u.payload) as *const _) };
Ok(ret)
}
#[inline]
pub fn to_gpa_attribute_info(&self) -> Result<hv_x64_gpa_attribute_intercept_message> {
if self.header.message_type != hv_message_type_HVMSG_GPA_ATTRIBUTE_INTERCEPT {
return Err(errno::Error::new(libc::EINVAL));
}
// SAFETY: We know at this point the payload is of the correct type. The payload field is
// unaligned. We use addr_of! to safely create a pointer, then call read_unaligned for
// copying its content out.
let ret =
unsafe { std::ptr::read_unaligned(std::ptr::addr_of!(self.u.payload) as *const _) };
Ok(ret)
}
#[inline]
pub fn to_ioport_info(&self) -> Result<hv_x64_io_port_intercept_message> {
if self.header.message_type != hv_message_type_HVMSG_X64_IO_PORT_INTERCEPT {
return Err(errno::Error::new(libc::EINVAL));
}
// SAFETY: We know at this point the payload is of the correct type. The payload field is
// unaligned. We use addr_of! to safely create a pointer, then call read_unaligned for
// copying its content out.
let ret =
unsafe { std::ptr::read_unaligned(std::ptr::addr_of!(self.u.payload) as *const _) };
Ok(ret)
}
#[inline]
pub fn to_msr_info(&self) -> Result<hv_x64_msr_intercept_message> {
if self.header.message_type != hv_message_type_HVMSG_X64_MSR_INTERCEPT {
return Err(errno::Error::new(libc::EINVAL));
}
// SAFETY: We know at this point the payload is of the correct type. The payload field is
// unaligned. We use addr_of! to safely create a pointer, then call read_unaligned for
// copying its content out.
let ret =
unsafe { std::ptr::read_unaligned(std::ptr::addr_of!(self.u.payload) as *const _) };
Ok(ret)
}
#[inline]
pub fn to_exception_info(&self) -> Result<hv_x64_exception_intercept_message> {
if self.header.message_type != hv_message_type_HVMSG_X64_EXCEPTION_INTERCEPT {
return Err(errno::Error::new(libc::EINVAL));
}
// SAFETY: We know at this point the payload is of the correct type. The payload field is
// unaligned. We use addr_of! to safely create a pointer, then call read_unaligned for
// copying its content out.
let ret =
unsafe { std::ptr::read_unaligned(std::ptr::addr_of!(self.u.payload) as *const _) };
Ok(ret)
}
#[inline]
pub fn to_invalid_vp_register_info(&self) -> Result<hv_x64_invalid_vp_register_message> {
if self.header.message_type != hv_message_type_HVMSG_INVALID_VP_REGISTER_VALUE {
return Err(errno::Error::new(libc::EINVAL));
}
// SAFETY: We know at this point the payload is of the correct type. The payload field is
// unaligned. We use addr_of! to safely create a pointer, then call read_unaligned for
// copying its content out.
let ret =
unsafe { std::ptr::read_unaligned(std::ptr::addr_of!(self.u.payload) as *const _) };
Ok(ret)
}
#[inline]
pub fn to_unrecoverable_exception_info(
&self,
) -> Result<hv_x64_unrecoverable_exception_message> {
if self.header.message_type != hv_message_type_HVMSG_UNRECOVERABLE_EXCEPTION {
return Err(errno::Error::new(libc::EINVAL));
}
// SAFETY: We know at this point the payload is of the correct type. The payload field is
// unaligned. We use addr_of! to safely create a pointer, then call read_unaligned for
// copying its content out.
let ret =
unsafe { std::ptr::read_unaligned(std::ptr::addr_of!(self.u.payload) as *const _) };
Ok(ret)
}
#[inline]
pub fn to_interruption_deliverable_info(
&self,
) -> Result<hv_x64_interruption_deliverable_message> {
if self.header.message_type != hv_message_type_HVMSG_X64_INTERRUPTION_DELIVERABLE {
return Err(errno::Error::new(libc::EINVAL));
}
// SAFETY: We know at this point the payload is of the correct type. The payload field is
// unaligned. We use addr_of! to safely create a pointer, then call read_unaligned for
// copying its content out.
let ret =
unsafe { std::ptr::read_unaligned(std::ptr::addr_of!(self.u.payload) as *const _) };
Ok(ret)
}
#[inline]
pub fn to_apic_eoi_info(&self) -> Result<hv_x64_apic_eoi_message> {
if self.header.message_type != hv_message_type_HVMSG_X64_APIC_EOI {
return Err(errno::Error::new(libc::EINVAL));
}
// SAFETY: We know at this point the payload is of the correct type. The payload field is
// unaligned. We use addr_of! to safely create a pointer, then call read_unaligned for
// copying its content out.
let ret =
unsafe { std::ptr::read_unaligned(std::ptr::addr_of!(self.u.payload) as *const _) };
Ok(ret)
}
#[inline]
pub fn to_hypercall_intercept_info(&self) -> Result<hv_x64_hypercall_intercept_message> {
if self.header.message_type != hv_message_type_HVMSG_HYPERCALL_INTERCEPT {
return Err(errno::Error::new(libc::EINVAL));
}
// SAFETY: We know at this point the payload is of the correct type. The payload field is
// unaligned. We use addr_of! to safely create a pointer, then call read_unaligned for
// copying its content out.
let ret =
unsafe { std::ptr::read_unaligned(std::ptr::addr_of!(self.u.payload) as *const _) };
Ok(ret)
}
#[inline]
pub fn to_sint_deliverable_info(&self) -> Result<hv_x64_sint_deliverable_message> {
if self.header.message_type != hv_message_type_HVMSG_SYNIC_SINT_DELIVERABLE {
return Err(errno::Error::new(libc::EINVAL));
}
// SAFETY: We know at this point the payload is of the correct type. The payload field is
// unaligned. We use addr_of! to safely create a pointer, then call read_unaligned for
// copying its content out.
let ret =
unsafe { std::ptr::read_unaligned(std::ptr::addr_of!(self.u.payload) as *const _) };
Ok(ret)
}
#[inline]
pub fn to_vmg_intercept_info(&self) -> Result<hv_x64_vmgexit_intercept_message> {
if self.header.message_type != hv_message_type_HVMSG_X64_SEV_VMGEXIT_INTERCEPT {
return Err(errno::Error::new(libc::EINVAL));
}
// SAFETY: We know at this point the payload is of the correct type. The payload field is
// unaligned. We use addr_of! to safely create a pointer, then call read_unaligned for
// copying its content out.
let ret =
unsafe { std::ptr::read_unaligned(std::ptr::addr_of!(self.u.payload) as *const _) };
Ok(ret)
}
}