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_trait_impl)]
7#![feature(core_intrinsics)]
8#![feature(coroutines)]
9#![feature(fn_traits)]
10#![feature(iter_advance_by)]
11#![feature(iter_from_coroutine)]
12#![feature(linkage)]
13#![feature(min_specialization)]
14//#![feature(negative_impls)]
15#![feature(ptr_metadata)]
16#![feature(sync_unsafe_cell)]
17#![expect(internal_features)]
18#![no_std]
19// Disable `missing_docs`: verification-only `inv`/`type_inv` items are
20// explained by proof context rather than API docs.
21// #![warn(missing_docs)]
22#![feature(proc_macro_hygiene)]
23#![allow(unused_parens)]
24#![allow(unused_braces)]
25#![allow(rustdoc::invalid_rust_codeblocks)]
26#![allow(rustdoc::invalid_html_tags)]
27#![allow(rustdoc::broken_intra_doc_links)]
28// Allow Verus-style specification names that mirror constants or proof-level
29// concepts rather than Rust's conventional function naming.
30#![allow(non_snake_case)]
31
32extern crate alloc;
33
34#[cfg(target_arch = "x86_64")]
35#[path = "arch/x86/mod.rs"]
36pub mod arch;
37#[cfg(target_arch = "riscv64")]
38#[path = "arch/riscv/mod.rs"]
39pub mod arch;
40#[cfg(target_arch = "loongarch64")]
41#[path = "arch/loongarch/mod.rs"]
42pub mod arch;
43pub mod boot;
44/*pub mod bus;
45pub mod console;*/
46pub mod cpu;
47pub mod error;
48/*pub mod io;
49pub mod logger;*/
50pub mod mm;
51/*pub mod panic;
52pub mod prelude;
53pub mod smp;*/
54pub mod sync;
55pub mod task;
56pub mod timer;
57pub mod trap;
58// pub mod user;
59#[path = "../specs/mod.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*/