hyperlight_common/
outb.rs

1use core::convert::TryFrom;
2
3use anyhow::{anyhow, Error};
4
5/// Exception codes for the x86 architecture.
6/// These are helpful to identify the type of exception that occurred
7/// together with OutBAction::Abort.
8#[repr(u8)]
9#[derive(Debug, Clone, Copy)]
10pub enum Exception {
11    DivideByZero = 0,
12    Debug = 1,
13    NonMaskableInterrupt = 2,
14    Breakpoint = 3,
15    Overflow = 4,
16    BoundRangeExceeded = 5,
17    InvalidOpcode = 6,
18    DeviceNotAvailable = 7,
19    DoubleFault = 8,
20    CoprocessorSegmentOverrun = 9,
21    InvalidTSS = 10,
22    SegmentNotPresent = 11,
23    StackSegmentFault = 12,
24    GeneralProtectionFault = 13,
25    PageFault = 14,
26    Reserved = 15,
27    X87FloatingPointException = 16,
28    AlignmentCheck = 17,
29    MachineCheck = 18,
30    SIMDFloatingPointException = 19,
31    VirtualizationException = 20,
32    SecurityException = 30,
33    NoException = 0xFF,
34}
35
36impl TryFrom<u8> for Exception {
37    type Error = Error;
38
39    fn try_from(value: u8) -> Result<Self, Self::Error> {
40        use Exception::*;
41        let exception = match value {
42            0 => DivideByZero,
43            1 => Debug,
44            2 => NonMaskableInterrupt,
45            3 => Breakpoint,
46            4 => Overflow,
47            5 => BoundRangeExceeded,
48            6 => InvalidOpcode,
49            7 => DeviceNotAvailable,
50            8 => DoubleFault,
51            9 => CoprocessorSegmentOverrun,
52            10 => InvalidTSS,
53            11 => SegmentNotPresent,
54            12 => StackSegmentFault,
55            13 => GeneralProtectionFault,
56            14 => PageFault,
57            15 => Reserved,
58            16 => X87FloatingPointException,
59            17 => AlignmentCheck,
60            18 => MachineCheck,
61            19 => SIMDFloatingPointException,
62            20 => VirtualizationException,
63            30 => SecurityException,
64            0x7F => NoException,
65            _ => return Err(anyhow!("Unknown exception code: {:#x}", value)),
66        };
67
68        Ok(exception)
69    }
70}
71
72/// Supported actions when issuing an OUTB actions by Hyperlight.
73/// - Log: for logging,
74/// - CallFunction: makes a call to a host function,
75/// - Abort: aborts the execution of the guest,
76/// - DebugPrint: prints a message to the host
77pub enum OutBAction {
78    Log = 99,
79    CallFunction = 101,
80    Abort = 102,
81    DebugPrint = 103,
82}
83
84impl TryFrom<u16> for OutBAction {
85    type Error = anyhow::Error;
86    fn try_from(val: u16) -> anyhow::Result<Self> {
87        match val {
88            99 => Ok(OutBAction::Log),
89            101 => Ok(OutBAction::CallFunction),
90            102 => Ok(OutBAction::Abort),
91            103 => Ok(OutBAction::DebugPrint),
92            _ => Err(anyhow::anyhow!("Invalid OutBAction value: {}", val)),
93        }
94    }
95}