Skip to main content

hyperlight_common/
outb.rs

1/*
2Copyright 2025 The Hyperlight Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17use core::convert::TryFrom;
18
19use anyhow::{Error, anyhow};
20
21/// Exception codes for the x86 architecture.
22/// These are helpful to identify the type of exception that occurred
23/// together with OutBAction::Abort.
24#[repr(u8)]
25#[derive(Debug, Clone, Copy)]
26pub enum Exception {
27    DivideByZero = 0,
28    Debug = 1,
29    NonMaskableInterrupt = 2,
30    Breakpoint = 3,
31    Overflow = 4,
32    BoundRangeExceeded = 5,
33    InvalidOpcode = 6,
34    DeviceNotAvailable = 7,
35    DoubleFault = 8,
36    CoprocessorSegmentOverrun = 9,
37    InvalidTSS = 10,
38    SegmentNotPresent = 11,
39    StackSegmentFault = 12,
40    GeneralProtectionFault = 13,
41    PageFault = 14,
42    Reserved = 15,
43    X87FloatingPointException = 16,
44    AlignmentCheck = 17,
45    MachineCheck = 18,
46    SIMDFloatingPointException = 19,
47    VirtualizationException = 20,
48    SecurityException = 30,
49    NoException = 0xFF,
50}
51
52impl TryFrom<u8> for Exception {
53    type Error = Error;
54
55    fn try_from(value: u8) -> Result<Self, Self::Error> {
56        use Exception::*;
57        let exception = match value {
58            0 => DivideByZero,
59            1 => Debug,
60            2 => NonMaskableInterrupt,
61            3 => Breakpoint,
62            4 => Overflow,
63            5 => BoundRangeExceeded,
64            6 => InvalidOpcode,
65            7 => DeviceNotAvailable,
66            8 => DoubleFault,
67            9 => CoprocessorSegmentOverrun,
68            10 => InvalidTSS,
69            11 => SegmentNotPresent,
70            12 => StackSegmentFault,
71            13 => GeneralProtectionFault,
72            14 => PageFault,
73            15 => Reserved,
74            16 => X87FloatingPointException,
75            17 => AlignmentCheck,
76            18 => MachineCheck,
77            19 => SIMDFloatingPointException,
78            20 => VirtualizationException,
79            30 => SecurityException,
80            0xFF => NoException,
81            _ => return Err(anyhow!("Unknown exception code: {:#x}", value)),
82        };
83
84        Ok(exception)
85    }
86}
87
88/// Supported actions when issuing an OUTB actions by Hyperlight.
89/// These are handled by the sandbox-level outb dispatcher.
90/// - Log: for logging,
91/// - CallFunction: makes a call to a host function,
92/// - Abort: aborts the execution of the guest,
93/// - DebugPrint: prints a message to the host
94/// - TraceBatch: reports a batch of spans and events from the guest
95/// - TraceMemoryAlloc: records memory allocation events
96/// - TraceMemoryFree: records memory deallocation events
97pub enum OutBAction {
98    Log = 99,
99    CallFunction = 101,
100    Abort = 102,
101    DebugPrint = 103,
102    #[cfg(feature = "trace_guest")]
103    TraceBatch = 104,
104    #[cfg(feature = "mem_profile")]
105    TraceMemoryAlloc = 105,
106    #[cfg(feature = "mem_profile")]
107    TraceMemoryFree = 106,
108}
109
110/// IO-port actions intercepted at the hypervisor level (in `run_vcpu`)
111/// before they ever reach the sandbox outb handler.  These are split
112/// from [`OutBAction`] so the outb handler does not need unreachable
113/// match arms for ports it can never see.
114pub enum VmAction {
115    /// IO port for PV timer configuration. The guest writes a 32-bit
116    /// LE value representing the desired timer period in microseconds.
117    /// A value of 0 disables the timer.
118    PvTimerConfig = 107,
119    /// IO port the guest writes to signal "I'm done" to the host.
120    /// This replaces the `hlt` instruction for halt signaling so that
121    /// KVM's in-kernel LAPIC (which absorbs HLT exits) does not interfere
122    /// with hyperlight's halt-based guest-host protocol.
123    Halt = 108,
124}
125
126impl TryFrom<u16> for OutBAction {
127    type Error = anyhow::Error;
128    fn try_from(val: u16) -> anyhow::Result<Self> {
129        match val {
130            99 => Ok(OutBAction::Log),
131            101 => Ok(OutBAction::CallFunction),
132            102 => Ok(OutBAction::Abort),
133            103 => Ok(OutBAction::DebugPrint),
134            #[cfg(feature = "trace_guest")]
135            104 => Ok(OutBAction::TraceBatch),
136            #[cfg(feature = "mem_profile")]
137            105 => Ok(OutBAction::TraceMemoryAlloc),
138            #[cfg(feature = "mem_profile")]
139            106 => Ok(OutBAction::TraceMemoryFree),
140            _ => Err(anyhow::anyhow!("Invalid OutBAction value: {}", val)),
141        }
142    }
143}
144
145impl TryFrom<u16> for VmAction {
146    type Error = anyhow::Error;
147    fn try_from(val: u16) -> anyhow::Result<Self> {
148        match val {
149            107 => Ok(VmAction::PvTimerConfig),
150            108 => Ok(VmAction::Halt),
151            _ => Err(anyhow::anyhow!("Invalid VmAction value: {}", val)),
152        }
153    }
154}