hyperlight_guest/exit.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::ffi::{CStr, c_char};
18
19use hyperlight_common::outb::OutBAction;
20
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")]
24mod arch;
25pub(crate) use arch::out32;
26
27/// Exits the VM with an Abort OUT action and code 0.
28#[unsafe(no_mangle)]
29pub extern "C" fn abort() -> ! {
30 abort_with_code(&[0, 0xFF])
31}
32
33/// Exits the VM with an Abort OUT action and a specific code.
34pub fn abort_with_code(code: &[u8]) -> ! {
35 // End any ongoing trace before aborting
36 #[cfg(all(feature = "trace_guest", target_arch = "x86_64"))]
37 hyperlight_guest_tracing::end_trace();
38 outb(OutBAction::Abort as u16, code);
39 outb(OutBAction::Abort as u16, &[0xFF]); // send abort terminator (if not included in code)
40 unreachable!()
41}
42
43/// Aborts the program with a code and a message.
44///
45/// # Safety
46/// This function is unsafe because it dereferences a raw pointer.
47pub unsafe fn abort_with_code_and_message(code: &[u8], message_ptr: *const c_char) -> ! {
48 // End any ongoing trace before aborting
49 #[cfg(all(feature = "trace_guest", target_arch = "x86_64"))]
50 hyperlight_guest_tracing::end_trace();
51 unsafe {
52 // Step 1: Send abort code (typically 1 byte, but `code` allows flexibility)
53 outb(OutBAction::Abort as u16, code);
54
55 // Step 2: Convert the C string to bytes
56 let message_bytes = CStr::from_ptr(message_ptr).to_bytes(); // excludes null terminator
57
58 // Step 3: Send the message itself in chunks
59 outb(OutBAction::Abort as u16, message_bytes);
60
61 // Step 4: Send abort terminator to signal completion (e.g., 0xFF)
62 outb(OutBAction::Abort as u16, &[0xFF]);
63
64 // This function never returns
65 unreachable!()
66 }
67}
68
69/// This function exists to give the guest more manual control
70/// over the abort sequence. For example, in `hyperlight_guest_bin`'s panic handler,
71/// we have a message of unknown length that we want to stream
72/// to the host, which requires sending the message in chunks
73pub fn write_abort(code: &[u8]) {
74 outb(OutBAction::Abort as u16, code);
75}
76
77/// OUT bytes to the host through multiple exits.
78pub(crate) fn outb(port: u16, data: &[u8]) {
79 // Ensure all tracing data is flushed before sending OUT bytes
80 unsafe {
81 let mut i = 0;
82 while i < data.len() {
83 let remaining = data.len() - i;
84 let chunk_len = remaining.min(3);
85 let mut chunk = [0u8; 4];
86 chunk[0] = chunk_len as u8;
87 chunk[1..1 + chunk_len].copy_from_slice(&data[i..i + chunk_len]);
88 let val = u32::from_le_bytes(chunk);
89 out32(port, val);
90 i += chunk_len;
91 }
92 }
93}
94
95/// Prints a message using `OutBAction::DebugPrint`. It transmits bytes of a message
96/// through several VMExists and, with such, it is slower than
97/// `print_output_with_host_print`.
98///
99/// This function should be used in debug mode only. This function does not
100/// require memory to be setup to be used.
101pub fn debug_print(msg: &str) {
102 for byte in msg.bytes() {
103 unsafe {
104 out32(OutBAction::DebugPrint as u16, byte as u32);
105 }
106 }
107}