ostd/
lib.rs

1// SPDX-License-Identifier: MPL-2.0
2//! The standard library for Asterinas and other Rust OSes.
3#![feature(alloc_error_handler)]
4#![feature(allocator_api)]
5#![feature(btree_cursors)]
6#![feature(const_ptr_sub_ptr)]
7#![feature(const_trait_impl)]
8#![feature(core_intrinsics)]
9#![feature(coroutines)]
10#![feature(fn_traits)]
11#![feature(iter_advance_by)]
12#![feature(iter_from_coroutine)]
13#![feature(let_chains)]
14#![feature(linkage)]
15#![feature(min_specialization)]
16//#![feature(negative_impls)]
17#![feature(ptr_metadata)]
18#![feature(ptr_sub_ptr)]
19#![feature(sync_unsafe_cell)]
20#![feature(trait_upcasting)]
21#![feature(unbounded_shifts)]
22#![expect(internal_features)]
23#![no_std]
24#![warn(missing_docs)]
25#![feature(proc_macro_hygiene)]
26#![allow(unused_parens)]
27#![allow(unused_braces)]
28#![allow(rustdoc::invalid_rust_codeblocks)]
29#![allow(rustdoc::invalid_html_tags)]
30#![allow(rustdoc::broken_intra_doc_links)]
31
32extern crate alloc;
33
34use vstd_extra::prelude;
35
36//#[cfg(target_arch = "x86_64")]
37#[path = "arch/x86/mod.rs"]
38pub mod arch;
39//#[cfg(target_arch = "riscv64")]
40//#[path = "arch/riscv/mod.rs"]
41//pub mod arch;
42pub mod boot;
43/*pub mod bus;
44pub mod console;
45pub mod cpu;*/
46pub mod error;
47/*pub mod io;
48pub mod logger;*/
49#[allow(missing_docs)]
50pub mod mm;
51/*pub mod panic;
52pub mod prelude;
53pub mod smp;*/
54pub mod sync;
55pub mod task;
56/* pub mod timer;
57pub mod trap;
58pub mod user;*/
59#[path = "../specs/lib.rs"]
60pub mod specs;
61pub mod util;
62
63/*use core::sync::atomic::{AtomicBool, Ordering};
64
65pub use ostd_macros::{
66    global_frame_allocator, global_heap_allocator, global_heap_allocator_slot_map, main,
67    panic_handler,
68};
69pub use ostd_pod::Pod;
70
71pub use self::{error::Error, prelude::Result};
72
73/// Initializes OSTD.
74///
75/// This function represents the first phase booting up the system. It makes
76/// all functionalities of OSTD available after the call.
77///
78/// # Safety
79///
80/// This function should be called only once and only on the BSP.
81//
82// TODO: We need to refactor this function to make it more modular and
83// make inter-initialization-dependencies more clear and reduce usages of
84// boot stage only global variables.
85#[doc(hidden)]
86unsafe fn init() {
87    arch::enable_cpu_features();
88
89    // SAFETY: This function is called only once, before `allocator::init`
90    // and after memory regions are initialized.
91    unsafe { mm::frame::allocator::init_early_allocator() };
92
93    #[cfg(target_arch = "x86_64")]
94    arch::if_tdx_enabled!({
95    } else {
96        arch::serial::init();
97    });
98    #[cfg(not(target_arch = "x86_64"))]
99    arch::serial::init();
100
101    logger::init();
102
103    // SAFETY:
104    // 1. They are only called once in the boot context of the BSP.
105    // 2. The number of CPUs are available because ACPI has been initialized.
106    // 3. No CPU-local objects have been accessed yet.
107    unsafe { cpu::init_on_bsp() };
108
109    // SAFETY: We are on the BSP and APs are not yet started.
110    let meta_pages = unsafe { mm::frame::meta::init() };
111    // The frame allocator should be initialized immediately after the metadata
112    // is initialized. Otherwise the boot page table can't allocate frames.
113    // SAFETY: This function is called only once.
114    unsafe { mm::frame::allocator::init() };
115
116    mm::kspace::init_kernel_page_table(meta_pages);
117
118    sync::init();
119
120    boot::init_after_heap();
121
122    mm::dma::init();
123
124    unsafe { arch::late_init_on_bsp() };
125
126    #[cfg(target_arch = "x86_64")]
127    arch::if_tdx_enabled!({
128        arch::serial::init();
129    });
130
131    smp::init();
132
133    // SAFETY: This function is called only once on the BSP.
134    unsafe {
135        mm::kspace::activate_kernel_page_table();
136    }
137
138    bus::init();
139
140    arch::irq::enable_local();
141
142    invoke_ffi_init_funcs();
143
144    IN_BOOTSTRAP_CONTEXT.store(false, Ordering::Relaxed);
145}
146
147/// Indicates whether the kernel is in bootstrap context.
148pub(crate) static IN_BOOTSTRAP_CONTEXT: AtomicBool = AtomicBool::new(true);
149
150/// Invoke the initialization functions defined in the FFI.
151/// The component system uses this function to call the initialization functions of
152/// the components.
153fn invoke_ffi_init_funcs() {
154    extern "C" {
155        fn __sinit_array();
156        fn __einit_array();
157    }
158    let call_len = (__einit_array as usize - __sinit_array as usize) / 8;
159    for i in 0..call_len {
160        unsafe {
161            let function = (__sinit_array as usize + 8 * i) as *const fn();
162            (*function)();
163        }
164    }
165}
166
167/// Simple unit tests for the ktest framework.
168#[cfg(ktest)]
169mod test {
170    use crate::prelude::*;
171
172    #[ktest]
173    #[expect(clippy::eq_op)]
174    fn trivial_assertion() {
175        assert_eq!(0, 0);
176    }
177
178    #[ktest]
179    #[should_panic]
180    fn failing_assertion() {
181        assert_eq!(0, 1);
182    }
183
184    #[ktest]
185    #[should_panic(expected = "expected panic message")]
186    fn expect_panic() {
187        panic!("expected panic message");
188    }
189}
190
191#[doc(hidden)]
192pub mod ktest {
193    //! The module re-exports everything from the [`ostd_test`] crate, as well
194    //! as the test entry point macro.
195    //!
196    //! It is rather discouraged to use the definitions here directly. The
197    //! `ktest` attribute is sufficient for all normal use cases.
198
199    pub use ostd_macros::{test_main as main, test_panic_handler as panic_handler};
200    pub use ostd_test::*;
201}
202*/