openhcl_boot/rt.rs
1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! Architecture-independent runtime support.
5
6// This must match the hardcoded value set at the entry point in the asm.
7pub(crate) const STACK_SIZE: usize = 32768;
8pub(crate) const STACK_COOKIE: u32 = 0x30405060;
9
10#[repr(C, align(16))]
11pub struct Stack([u8; STACK_SIZE]);
12
13pub static mut STACK: Stack = Stack([0; STACK_SIZE]);
14
15/// Validate the stack cookie is still present. Panics if overwritten.
16pub fn verify_stack_cookie() {
17 // SAFETY: It's possible we've overrun the stack at this point if any
18 // previous stack frame was too large. But, we know the pointer is valid and
19 // never came from a rust reference, and we're about to crash if the value
20 // is bogus.
21 unsafe {
22 let stack_ptr = core::ptr::addr_of!(STACK).cast::<u32>();
23 if core::ptr::read(stack_ptr) != STACK_COOKIE {
24 panic!("Stack was overrun - check for large variables");
25 }
26 }
27}
28
29/// The entry point.
30///
31/// X64: The relative offset for shim parameters are passed in the rsi register.
32/// rax contains the base address of where the shim was loaded at.
33///
34/// ARM64: The relative offset for shim parameters are passed in the x1 register.
35/// x2 contains the base address of where the shim was loaded at.
36///
37/// # Safety
38///
39/// The caller must ensure that the passed shim_params_offset is the correct offset
40/// from the shim base to the shim parameters.
41#[cfg_attr(not(minimal_rt), expect(dead_code))]
42pub unsafe extern "C" fn start(_: usize, shim_params_offset: isize) -> ! {
43 crate::shim_main(shim_params_offset)
44}
45
46#[cfg(minimal_rt)]
47mod instead_of_builtins {
48 #[panic_handler]
49 fn panic(panic: &core::panic::PanicInfo<'_>) -> ! {
50 crate::boot_logger::log!("{panic}");
51 // The stack is identity mapped.
52 minimal_rt::enlightened_panic::report(*b"OHCLBOOT", panic, |va| Some(va as usize));
53 minimal_rt::arch::fault();
54 }
55}