Skip to main content

airbender_rt/
boot.rs

1//! Boot helpers for Airbender guest programs.
2
3/// Initialize the Airbender runtime and then execute the entrypoint.
4///
5/// This helper is available only for built-in allocator backends. When
6/// `allocator-custom` is enabled, use `start_with_allocator_init` so the
7/// runtime receives an explicit allocator init hook.
8#[cfg(not(feature = "allocator-custom"))]
9pub fn start<F>(entry: F) -> !
10where
11    F: FnOnce() -> core::convert::Infallible,
12{
13    start_with_allocator_init(crate::allocator::init, entry)
14}
15
16/// Initialize the Airbender runtime with a custom allocator init hook.
17pub fn start_with_allocator_init<F>(
18    allocator_init: unsafe fn(*mut usize, *mut usize),
19    entry: F,
20) -> !
21where
22    F: FnOnce() -> core::convert::Infallible,
23{
24    #[cfg(not(target_arch = "riscv32"))]
25    let _ = allocator_init;
26
27    #[cfg(target_arch = "riscv32")]
28    {
29        riscv_common::boot_sequence::init();
30        // SAFETY: The boot sequence guarantees that heap_start and heap_end
31        // point to a valid, exclusively-owned memory region.
32        unsafe {
33            allocator_init(
34                riscv_common::boot_sequence::heap_start(),
35                riscv_common::boot_sequence::heap_end(),
36            );
37        }
38    }
39
40    match entry() {}
41}