Skip to main content

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