hyperlight_guest/exceptions/
handlers.rs

1/*
2Copyright 2024 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 alloc::format;
18use core::ffi::c_char;
19
20use hyperlight_common::flatbuffer_wrappers::guest_error::ErrorCode;
21use hyperlight_common::outb::Exception;
22
23use crate::entrypoint::abort_with_code_and_message;
24
25/// Exception handler
26#[no_mangle]
27pub extern "C" fn hl_exception_handler(
28    stack_pointer: u64,
29    exception_number: u64,
30    page_fault_address: u64,
31) {
32    let exception = Exception::try_from(exception_number as u8).expect("Invalid exception number");
33    let msg = format!(
34        "Page Fault Address: {:#x}\n\
35            Stack Pointer: {:#x}",
36        page_fault_address, stack_pointer
37    );
38
39    unsafe {
40        abort_with_code_and_message(
41            &[ErrorCode::GuestError as u8, exception as u8],
42            msg.as_ptr() as *const c_char,
43        );
44    }
45}