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;
20use core::hint::unreachable_unchecked;
21use core::ptr::copy_nonoverlapping;
22
23use buddy_system_allocator::LockedHeap;
24use guest_function_register::GuestFunctionRegister;
25use hyperlight_common::flatbuffer_wrappers::guest_error::ErrorCode;
26use hyperlight_common::mem::{HyperlightPEB, RunMode};
27
28use crate::host_function_call::{outb, OutBAction};
29extern crate alloc;
30
31// Modules
32pub mod entrypoint;
33pub mod shared_input_data;
34pub mod shared_output_data;
35
36pub mod guest_error;
37pub mod guest_function_call;
38pub mod guest_function_definition;
39pub mod guest_function_register;
40
41pub mod host_error;
42pub mod host_function_call;
43pub mod host_functions;
44
45pub(crate) mod guest_logger;
46pub mod memory;
47pub mod print;
48pub(crate) mod security_check;
49pub mod setjmp;
50
51pub mod chkstk;
52pub mod error;
53pub mod gdt;
54pub mod idt;
55pub mod idtr;
56pub mod interrupt_entry;
57pub mod interrupt_handlers;
58pub mod logging;
59
60// Unresolved symbols
61///cbindgen:ignore
62#[no_mangle]
63pub(crate) extern "C" fn __CxxFrameHandler3() {}
64///cbindgen:ignore
65#[no_mangle]
66pub(crate) static _fltused: i32 = 0;
67
68// It looks like rust-analyzer doesn't correctly manage no_std crates,
69// and so it displays an error about a duplicate panic_handler.
70// See more here: https://github.com/rust-lang/rust-analyzer/issues/4490
71// The cfg_attr attribute is used to avoid clippy failures as test pulls in std which pulls in a panic handler
72#[cfg_attr(not(test), panic_handler)]
73#[allow(clippy::panic)]
74// to satisfy the clippy when cfg == test
75#[allow(dead_code)]
76fn panic(info: &core::panic::PanicInfo) -> ! {
77    unsafe {
78        let peb_ptr = P_PEB.unwrap();
79        copy_nonoverlapping(
80            info.to_string().as_ptr(),
81            (*peb_ptr).guestPanicContextData.guestPanicContextDataBuffer as *mut u8,
82            (*peb_ptr).guestPanicContextData.guestPanicContextDataSize as usize,
83        );
84    }
85    outb(OutBAction::Abort as u16, ErrorCode::UnknownError as u8);
86    unsafe { unreachable_unchecked() }
87}
88
89// Globals
90#[global_allocator]
91pub(crate) static HEAP_ALLOCATOR: LockedHeap<32> = LockedHeap::<32>::empty();
92
93///cbindgen:ignore
94#[no_mangle]
95pub(crate) static mut __security_cookie: u64 = 0;
96
97pub(crate) static mut P_PEB: Option<*mut HyperlightPEB> = None;
98pub static mut MIN_STACK_ADDRESS: u64 = 0;
99
100pub static mut OS_PAGE_SIZE: u32 = 0;
101pub(crate) static mut OUTB_PTR: Option<extern "win64" fn(u16, u8)> = None;
102pub(crate) static mut OUTB_PTR_WITH_CONTEXT: Option<
103    extern "win64" fn(*mut core::ffi::c_void, u16, u8),
104> = None;
105pub static mut RUNNING_MODE: RunMode = RunMode::None;
106
107pub(crate) static mut REGISTERED_GUEST_FUNCTIONS: GuestFunctionRegister =
108    GuestFunctionRegister::new();