aloe/
lib.rs

1/*
2  ____                 __               __  __
3 / __ \__ _____ ____  / /___ ____ _    / / / /__ ___ ____
4/ /_/ / // / _ `/ _ \/ __/ // /  ' \  / /_/ (_-</ -_) __/
5\___\_\_,_/\_,_/_//_/\__/\_,_/_/_/_/  \____/___/\__/_/
6  Part of the Quantum OS Kernel
7
8Copyright 2025 Gavin Kellam
9
10Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
11associated documentation files (the "Software"), to deal in the Software without restriction,
12including without limitation the rights to use, copy, modify, merge, publish, distribute,
13sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
14furnished to do so, subject to the following conditions:
15
16The above copyright notice and this permission notice shall be included in all copies or substantial
17portions of the Software.
18
19THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
20NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
22DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
23OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24*/
25
26#![no_std]
27
28pub mod alloc;
29pub mod debug;
30pub mod ipc;
31pub mod sync;
32pub mod uio;
33
34// Import syscall interface
35pub use vera_portal::sys_client::*;
36pub use vera_portal::*;
37
38/// Termination trait for `main` to convert `()` or `Result<O, E>` to an exit status.
39pub trait QuantumTermination {
40    fn exit_status(self) -> ExitReason;
41}
42
43impl QuantumTermination for () {
44    fn exit_status(self) -> ExitReason {
45        ExitReason::Success
46    }
47}
48
49impl<E: core::error::Error> QuantumTermination for Result<(), E> {
50    fn exit_status(self) -> ExitReason {
51        match self {
52            Ok(_) => ExitReason::Success,
53            Err(err) => {
54                dbugln!("Failure {err}");
55                ExitReason::Failure
56            }
57        }
58    }
59}
60
61/// A micro version of Rust's standard library's prelude.
62#[macro_export]
63macro_rules! tiny_std {
64    () => {
65        extern crate alloc;
66
67        #[global_allocator]
68        static ALLOC: $crate::alloc::QuantumHeap = $crate::alloc::QuantumHeap::new();
69
70        #[cfg(not(test))]
71        #[panic_handler]
72        fn panic(info: &core::panic::PanicInfo) -> ! {
73            $crate::dbugln!("{}", info);
74            $crate::exit($crate::ExitReason::Failure);
75        }
76
77        #[doc(hidden)]
78        mod hidden_debug {
79            pub(super) fn debug_output(args: ::core::fmt::Arguments) {
80                use core::fmt::Write;
81                _ = (::aloe::debug::DebugOut {}).write_fmt(args);
82            }
83        }
84
85        #[unsafe(link_section = ".start")]
86        #[unsafe(no_mangle)]
87        extern "C" fn _start() {
88            ::aloe::debug::set_global_debug_fn(hidden_debug::debug_output);
89
90            let main_result = main();
91            let exit_status = $crate::QuantumTermination::exit_status(main_result);
92
93            $crate::exit(exit_status);
94        }
95    };
96}