Skip to content

Commit 9966a9d

Browse files
committed
refactor: move out32 into arch-specific exit module
Extract the out32 VM exit function from exit.rs into arch/amd64/exit.rs. exit.rs now delegates to the arch module via cfg_attr, keeping arch-independent code (outb, abort, debug_print) in the shared file. Signed-off-by: Ludvig Liljenberg <4257730+ludfjig@users.noreply.github.com>
1 parent c9a1a4c commit 9966a9d

2 files changed

Lines changed: 66 additions & 41 deletions

File tree

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
Copyright 2025 The Hyperlight Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
use core::arch::asm;
18+
19+
#[cfg(feature = "trace_guest")]
20+
use hyperlight_common::outb::OutBAction;
21+
22+
/// OUT function for sending a 32-bit value to the host.
23+
/// `out32` can be called from an exception context, so we must be careful
24+
/// with the tracing state that might be locked at that time.
25+
/// The tracing state calls `try_lock` internally to avoid deadlocks.
26+
/// Furthermore, the instrument macro is not used here to avoid creating spans
27+
/// in exception contexts. Because if the trace state is already locked, trying to create a span
28+
/// would cause a panic, which is undesirable in exception handling.
29+
pub(crate) unsafe fn out32(port: u16, val: u32) {
30+
#[cfg(feature = "trace_guest")]
31+
{
32+
if let Some((ptr, len)) = hyperlight_guest_tracing::serialized_data() {
33+
// If tracing is enabled and there is data to send, send it along with the OUT action
34+
unsafe {
35+
asm!("out dx, eax",
36+
in("dx") port,
37+
in("eax") val,
38+
in("r8") OutBAction::TraceBatch as u64,
39+
in("r9") ptr,
40+
in("r10") len,
41+
options(preserves_flags, nomem, nostack)
42+
)
43+
};
44+
45+
// Reset the trace state after sending the batch
46+
// This clears all existing spans/events ensuring a clean state for the next operations
47+
// The trace state is expected to be flushed before this call
48+
hyperlight_guest_tracing::reset();
49+
} else {
50+
// If tracing is not enabled, just send the value
51+
unsafe {
52+
asm!("out dx, eax", in("dx") port, in("eax") val, options(preserves_flags, nomem, nostack))
53+
};
54+
}
55+
}
56+
#[cfg(not(feature = "trace_guest"))]
57+
unsafe {
58+
asm!("out dx, eax", in("dx") port, in("eax") val, options(preserves_flags, nomem, nostack));
59+
}
60+
}

src/hyperlight_guest/src/exit.rs

Lines changed: 6 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,16 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
use core::arch::asm;
1817
use core::ffi::{CStr, c_char};
1918

2019
use hyperlight_common::outb::OutBAction;
2120

21+
#[cfg_attr(target_arch = "x86_64", path = "arch/amd64/exit.rs")]
22+
#[cfg_attr(target_arch = "x86", path = "arch/amd64/exit.rs")]
23+
#[cfg_attr(target_arch = "aarch64", path = "arch/aarch64/exit.rs")]
24+
mod arch;
25+
pub(crate) use arch::out32;
26+
2227
/// Exits the VM with an Abort OUT action and code 0.
2328
#[unsafe(no_mangle)]
2429
pub extern "C" fn abort() -> ! {
@@ -87,46 +92,6 @@ pub(crate) fn outb(port: u16, data: &[u8]) {
8792
}
8893
}
8994

90-
/// OUT function for sending a 32-bit value to the host.
91-
/// `out32` can be called from an exception context, so we must be careful
92-
/// with the tracing state that might be locked at that time.
93-
/// The tracing state calls `try_lock` internally to avoid deadlocks.
94-
/// Furthermore, the instrument macro is not used here to avoid creating spans
95-
/// in exception contexts. Because if the trace state is already locked, trying to create a span
96-
/// would cause a panic, which is undesirable in exception handling.
97-
pub(crate) unsafe fn out32(port: u16, val: u32) {
98-
#[cfg(all(feature = "trace_guest", target_arch = "x86_64"))]
99-
{
100-
if let Some((ptr, len)) = hyperlight_guest_tracing::serialized_data() {
101-
// If tracing is enabled and there is data to send, send it along with the OUT action
102-
unsafe {
103-
asm!("out dx, eax",
104-
in("dx") port,
105-
in("eax") val,
106-
in("r8") OutBAction::TraceBatch as u64,
107-
in("r9") ptr,
108-
in("r10") len,
109-
options(preserves_flags, nomem, nostack)
110-
)
111-
};
112-
113-
// Reset the trace state after sending the batch
114-
// This clears all existing spans/events ensuring a clean state for the next operations
115-
// The trace state is expected to be flushed before this call
116-
hyperlight_guest_tracing::reset();
117-
} else {
118-
// If tracing is not enabled, just send the value
119-
unsafe {
120-
asm!("out dx, eax", in("dx") port, in("eax") val, options(preserves_flags, nomem, nostack))
121-
};
122-
}
123-
}
124-
#[cfg(not(all(feature = "trace_guest", target_arch = "x86_64")))]
125-
unsafe {
126-
asm!("out dx, eax", in("dx") port, in("eax") val, options(preserves_flags, nomem, nostack));
127-
}
128-
}
129-
13095
/// Prints a message using `OutBAction::DebugPrint`. It transmits bytes of a message
13196
/// through several VMExists and, with such, it is slower than
13297
/// `print_output_with_host_print`.

0 commit comments

Comments
 (0)