hyperlight_guest/
lib.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
17#![no_std]
18// Deps
19use alloc::string::ToString;
20
21use buddy_system_allocator::LockedHeap;
22use guest_function_register::GuestFunctionRegister;
23use hyperlight_common::flatbuffer_wrappers::guest_error::ErrorCode;
24use hyperlight_common::mem::HyperlightPEB;
25
26use crate::entrypoint::abort_with_code_and_message;
27extern crate alloc;
28
29// Modules
30pub mod entrypoint;
31pub mod shared_input_data;
32pub mod shared_output_data;
33
34pub mod guest_error;
35pub mod guest_function_call;
36pub mod guest_function_definition;
37pub mod guest_function_register;
38
39pub mod host_function_call;
40
41pub(crate) mod guest_logger;
42pub mod memory;
43pub mod print;
44
45pub mod error;
46#[cfg(target_arch = "x86_64")]
47pub mod exceptions {
48    pub mod gdt;
49    pub mod handlers;
50    pub mod idt;
51    pub mod idtr;
52    pub mod interrupt_entry;
53}
54pub mod logging;
55
56// It looks like rust-analyzer doesn't correctly manage no_std crates,
57// and so it displays an error about a duplicate panic_handler.
58// See more here: https://github.com/rust-lang/rust-analyzer/issues/4490
59// The cfg_attr attribute is used to avoid clippy failures as test pulls in std which pulls in a panic handler
60#[cfg_attr(not(test), panic_handler)]
61#[allow(clippy::panic)]
62// to satisfy the clippy when cfg == test
63#[allow(dead_code)]
64fn panic(info: &core::panic::PanicInfo) -> ! {
65    let msg = info.to_string();
66    let c_string = alloc::ffi::CString::new(msg)
67        .unwrap_or_else(|_| alloc::ffi::CString::new("panic (invalid utf8)").unwrap());
68
69    unsafe { abort_with_code_and_message(&[ErrorCode::UnknownError as u8], c_string.as_ptr()) }
70}
71
72// Globals
73#[global_allocator]
74pub(crate) static HEAP_ALLOCATOR: LockedHeap<32> = LockedHeap::<32>::empty();
75
76pub(crate) static mut P_PEB: Option<*mut HyperlightPEB> = None;
77pub static mut MIN_STACK_ADDRESS: u64 = 0;
78
79pub static mut OS_PAGE_SIZE: u32 = 0;
80
81pub(crate) static mut REGISTERED_GUEST_FUNCTIONS: GuestFunctionRegister =
82    GuestFunctionRegister::new();