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