sel4_root_task/
entry.rs

1//
2// Copyright 2024, Colias Group, LLC
3//
4// SPDX-License-Identifier: BSD-2-Clause
5//
6
7use core::panic::UnwindSafe;
8
9use crate::{abort, panicking::catch_unwind, Termination};
10
11sel4_runtime_common::declare_entrypoint! {
12    (bootinfo: *const sel4::BootInfo) -> ! {
13        let bootinfo = unsafe { sel4::BootInfoPtr::new(bootinfo) };
14
15        let ipc_buffer = unsafe { bootinfo.ipc_buffer().as_mut().unwrap() };
16        sel4::set_ipc_buffer(ipc_buffer);
17
18        unsafe {
19            __sel4_root_task__main(&bootinfo);
20        }
21    }
22}
23
24extern "Rust" {
25    fn __sel4_root_task__main(bootinfo: &sel4::BootInfoPtr) -> !;
26}
27
28#[doc(hidden)]
29#[macro_export]
30macro_rules! declare_main {
31    ($main:expr) => {
32        #[allow(non_snake_case)]
33        #[no_mangle]
34        fn __sel4_root_task__main(bootinfo: &$crate::_private::BootInfoPtr) -> ! {
35            $crate::_private::run_main($main, bootinfo);
36        }
37    };
38}
39
40#[doc(hidden)]
41#[allow(clippy::missing_safety_doc)]
42pub fn run_main<F, T>(f: F, bootinfo: &sel4::BootInfoPtr) -> !
43where
44    F: FnOnce(&sel4::BootInfoPtr) -> T + UnwindSafe,
45    T: Termination,
46{
47    let result = catch_unwind(move || f(bootinfo).report());
48    match result {
49        Ok(err) => abort!("main thread terminated with error: {err:?}"),
50        Err(_) => abort!("uncaught panic in main thread"),
51    }
52}