Skip to content
This repository was archived by the owner on Sep 1, 2024. It is now read-only.

Commit 6deea04

Browse files
committed
vm_succeed updated
1 parent cdf6bd2 commit 6deea04

File tree

3 files changed

+38
-23
lines changed

3 files changed

+38
-23
lines changed

hypervisor/src/error.rs

+6
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,10 @@ pub enum HypervisorError {
154154

155155
#[error("Failed to parse hexadecimal string")]
156156
HexParseError,
157+
158+
#[error("VM instruction failed due to carry flag being set")]
159+
VMFailToLaunch,
160+
161+
#[error("VM instruction failed due to zero flag being set")]
162+
VmInstructionError,
157163
}

hypervisor/src/intel/vm.rs

+31-20
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use crate::intel::vmerror::VmxBasicExitReason;
2-
use x86::vmx::vmcs::ro;
31
use {
42
crate::{
53
error::HypervisorError,
@@ -11,14 +9,11 @@ use {
119
shared_data::SharedData,
1210
support::{vmclear, vmptrld, vmread},
1311
vmcs::Vmcs,
12+
vmerror::{VmInstructionError, VmxBasicExitReason},
1413
vmlaunch::launch_vm,
1514
},
1615
},
17-
alloc::{
18-
boxed::Box,
19-
format,
20-
string::{String, ToString},
21-
},
16+
alloc::boxed::Box,
2217
bit_field::BitField,
2318
core::ptr::NonNull,
2419
x86::{bits64::rflags::RFlags, vmx::vmcs},
@@ -113,7 +108,7 @@ impl Vm {
113108
pub fn run(&mut self) -> Result<VmxBasicExitReason, HypervisorError> {
114109
// Run the VM until the VM-exit occurs.
115110
let flags = unsafe { launch_vm(&mut self.guest_registers, u64::from(self.has_launched)) };
116-
Self::vm_succeed(RFlags::from_raw(flags)).unwrap();
111+
Self::vm_succeed(RFlags::from_raw(flags))?;
117112
self.has_launched = true;
118113

119114
// VM-exit occurred. Copy the guest register values from VMCS so that
@@ -122,7 +117,7 @@ impl Vm {
122117
self.guest_registers.rsp = vmread(vmcs::guest::RSP);
123118
self.guest_registers.rflags = vmread(vmcs::guest::RFLAGS);
124119

125-
let exit_reason = vmread(ro::EXIT_REASON) as u32;
120+
let exit_reason = vmread(vmcs::ro::EXIT_REASON) as u32;
126121

127122
let Some(basic_exit_reason) = VmxBasicExitReason::from_u32(exit_reason) else {
128123
log::error!("Unknown exit reason: {:#x}", exit_reason);
@@ -132,20 +127,36 @@ impl Vm {
132127
return Ok(basic_exit_reason);
133128
}
134129

135-
/// Checks that the latest VMX instruction succeeded.
130+
/// Verifies that the `launch_vm` function executed successfully.
131+
///
132+
/// This method checks the RFlags for indications of failure from the `launch_vm` function.
133+
/// If a failure is detected, it will panic with a detailed error message.
134+
///
135+
/// # Arguments
136136
///
137-
/// See: 31.2 CONVENTIONS
138-
fn vm_succeed(flags: RFlags) -> Result<(), String> {
137+
/// * `flags`: The RFlags value post-execution of the `launch_vm` function.
138+
///
139+
/// Reference: Intel® 64 and IA-32 Architectures Software Developer's Manual:
140+
/// - 31.2 CONVENTIONS
141+
/// - 31.4 VM INSTRUCTION ERROR NUMBERS
142+
fn vm_succeed(flags: RFlags) -> Result<(), HypervisorError> {
139143
if flags.contains(RFlags::FLAGS_ZF) {
140-
// See: 31.4 VM INSTRUCTION ERROR NUMBERS
141-
Err(format!(
142-
"VmFailValid with {}",
143-
vmread(vmcs::ro::VM_INSTRUCTION_ERROR)
144-
))
144+
let instruction_error = vmread(vmcs::ro::VM_INSTRUCTION_ERROR) as u32;
145+
return match VmInstructionError::from_u32(instruction_error) {
146+
Some(error) => {
147+
log::error!("VM instruction error: {:?}", error);
148+
Err(HypervisorError::VmInstructionError)
149+
}
150+
None => {
151+
log::error!("Unknown VM instruction error: {:#x}", instruction_error);
152+
Err(HypervisorError::UnknownVMInstructionError)
153+
}
154+
};
145155
} else if flags.contains(RFlags::FLAGS_CF) {
146-
Err("VmFailInvalid".to_string())
147-
} else {
148-
Ok(())
156+
log::error!("VM instruction failed due to carry flag being set");
157+
return Err(HypervisorError::VMFailToLaunch);
149158
}
159+
160+
Ok(())
150161
}
151162
}

hypervisor/src/vmm.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ use {
2020
vmx::Vmx,
2121
},
2222
log::*,
23-
x86::{
24-
vmx::vmcs::{guest, ro},
25-
},
23+
x86::vmx::vmcs::{guest, ro},
2624
};
2725

2826
// pass shared data to the hypervisor soon too

0 commit comments

Comments
 (0)