1#![feature(alloc_error_handler)]
6#![feature(allocator_api)]
7#![feature(btree_cursors)]
8#![feature(core_intrinsics)]
9#![feature(iter_advance_by)]
10#![feature(linkage)]
11#![feature(macro_metavar_expr)]
12#![feature(min_specialization)]
13#![feature(negative_impls)]
14#![feature(ptr_metadata)]
15#![feature(sync_unsafe_cell)]
16#![expect(internal_features)]
17#![no_std]
18#![warn(missing_docs)]
19
20extern crate alloc;
21#[macro_use]
22extern crate ostd_pod;
23
24#[cfg_attr(target_arch = "x86_64", path = "arch/x86/mod.rs")]
25#[cfg_attr(target_arch = "riscv64", path = "arch/riscv/mod.rs")]
26#[cfg_attr(target_arch = "loongarch64", path = "arch/loongarch/mod.rs")]
27pub mod arch;
28
29pub mod boot;
30pub mod bus;
31pub mod console;
32pub mod cpu;
33mod error;
34mod ex_table;
35pub mod io;
36pub mod irq;
37pub mod logger;
38pub mod mm;
39pub mod panic;
40pub mod power;
41pub mod prelude;
42pub mod smp;
43pub mod sync;
44pub mod task;
45pub mod timer;
46pub mod user;
47pub mod util;
48
49#[cfg(feature = "coverage")]
50mod coverage;
51
52use core::sync::atomic::{AtomicBool, Ordering};
53
54pub use ostd_macros::{
55 global_frame_allocator, global_heap_allocator, global_heap_allocator_slot_map, main,
56 panic_handler,
57};
58
59pub use self::{error::Error, prelude::Result};
60
61unsafe fn init() {
74 arch::enable_cpu_features();
75
76 unsafe { mm::frame::allocator::init_early_allocator() };
79
80 #[cfg(target_arch = "x86_64")]
81 arch::if_tdx_enabled!({
82 } else {
83 arch::serial::init();
84 });
85 #[cfg(not(target_arch = "x86_64"))]
86 arch::serial::init();
87
88 logger::init();
89
90 unsafe { cpu::init_on_bsp() };
95
96 let meta_pages = unsafe { mm::frame::meta::init() };
98 unsafe { mm::frame::allocator::init() };
102
103 mm::kspace::init_kernel_page_table(meta_pages);
104
105 unsafe { mm::kspace::activate_kernel_page_table() };
107
108 sync::init();
109
110 boot::init_after_heap();
111
112 unsafe { arch::late_init_on_bsp() };
114
115 #[cfg(target_arch = "x86_64")]
116 arch::if_tdx_enabled!({
117 arch::serial::init();
118 });
119
120 smp::init();
121
122 unsafe { crate::mm::page_table::boot_pt::dismiss() };
127
128 arch::irq::enable_local();
129
130 invoke_ffi_init_funcs();
131
132 IN_BOOTSTRAP_CONTEXT.store(false, Ordering::Relaxed);
133}
134
135pub(crate) static IN_BOOTSTRAP_CONTEXT: AtomicBool = AtomicBool::new(true);
137
138fn invoke_ffi_init_funcs() {
142 unsafe extern "C" {
143 fn __sinit_array();
144 fn __einit_array();
145 }
146 let call_len = (__einit_array as *const () as usize - __sinit_array as *const () as usize) / 8;
147 for i in 0..call_len {
148 unsafe {
149 let function = (__sinit_array as *const () as usize + 8 * i) as *const fn();
150 (*function)();
151 }
152 }
153}
154
155mod feature_validation {
156 #[cfg(all(not(target_arch = "riscv64"), feature = "riscv_sv39_mode"))]
157 compile_error!(
158 "feature \"riscv_sv39_mode\" cannot be specified for architectures other than RISC-V"
159 );
160}
161
162#[cfg(ktest)]
164mod test {
165 use crate::prelude::*;
166
167 #[ktest]
168 #[expect(clippy::eq_op)]
169 fn trivial_assertion() {
170 assert_eq!(0, 0);
171 }
172
173 #[ktest]
174 #[should_panic]
175 fn failing_assertion() {
176 assert_eq!(0, 1);
177 }
178
179 #[ktest]
180 #[should_panic(expected = "expected panic message")]
181 fn expect_panic() {
182 panic!("expected panic message");
183 }
184}
185
186#[doc(hidden)]
187pub mod ktest {
188 pub use ostd_macros::{test_main as main, test_panic_handler as panic_handler};
195 pub use ostd_test::*;
196}