1#![allow(clippy::missing_safety_doc)]
6#![cfg_attr(
7 any(target_arch = "aarch64", target_arch = "riscv64"),
8 allow(incomplete_features)
9)]
10#![cfg_attr(target_arch = "x86_64", feature(abi_x86_interrupt))]
11#![feature(allocator_api)]
12#![feature(linkage)]
13#![feature(linked_list_cursors)]
14#![feature(map_try_insert)]
15#![feature(maybe_uninit_as_bytes)]
16#![feature(maybe_uninit_slice)]
17#![feature(maybe_uninit_write_slice)]
18#![feature(never_type)]
19#![feature(slice_from_ptr_range)]
20#![feature(slice_ptr_get)]
21#![cfg_attr(
22 any(target_arch = "aarch64", target_arch = "riscv64"),
23 feature(specialization)
24)]
25#![feature(thread_local)]
26#![cfg_attr(target_os = "none", no_std)]
27#![cfg_attr(target_os = "none", feature(custom_test_frameworks))]
28#![cfg_attr(all(target_os = "none", test), test_runner(crate::test_runner))]
29#![cfg_attr(
30 all(target_os = "none", test),
31 reexport_test_harness_main = "test_main"
32)]
33#![cfg_attr(all(target_os = "none", test), no_main)]
34
35#[macro_use]
37extern crate alloc;
38#[macro_use]
39extern crate bitflags;
40#[macro_use]
41extern crate log;
42#[cfg(not(target_os = "none"))]
43#[macro_use]
44extern crate std;
45
46#[cfg(feature = "smp")]
47use core::hint::spin_loop;
48#[cfg(feature = "smp")]
49use core::sync::atomic::{AtomicU32, Ordering};
50
51use arch::core_local::*;
52
53pub(crate) use crate::arch::*;
54pub use crate::config::DEFAULT_STACK_SIZE;
55pub(crate) use crate::config::*;
56pub use crate::fs::create_file;
57use crate::kernel::is_uhyve_with_pci;
58use crate::scheduler::{PerCoreScheduler, PerCoreSchedulerExt};
59
60#[macro_use]
61mod macros;
62
63#[macro_use]
64mod logging;
65
66pub mod arch;
67mod config;
68pub mod console;
69mod drivers;
70mod entropy;
71mod env;
72pub mod errno;
73mod executor;
74pub mod fd;
75pub mod fs;
76mod init_cell;
77pub mod io;
78pub mod mm;
79pub mod scheduler;
80#[cfg(feature = "shell")]
81mod shell;
82mod synch;
83pub mod syscalls;
84pub mod time;
85
86mod built_info {
87 include!(concat!(env!("OUT_DIR"), "/built.rs"));
88}
89
90hermit_entry::define_abi_tag!();
91
92#[cfg(target_os = "none")]
93hermit_entry::define_entry_version!();
94
95#[cfg(test)]
96#[cfg(target_os = "none")]
97#[unsafe(no_mangle)]
98extern "C" fn runtime_entry(_argc: i32, _argv: *const *const u8, _env: *const *const u8) -> ! {
99 println!("Executing hermit unittests. Any arguments are dropped");
100 test_main();
101 core_scheduler().exit(0)
102}
103
104#[cfg(test)]
106pub fn test_runner(tests: &[&dyn Fn()]) {
107 println!("Running {} tests", tests.len());
108 for test in tests {
109 test();
110 }
111 core_scheduler().exit(0)
112}
113
114#[cfg(target_os = "none")]
115#[test_case]
116fn trivial_test() {
117 println!("Test test test");
118 panic!("Test called");
119}
120
121#[cfg(target_os = "none")]
123extern "C" fn initd(_arg: usize) {
124 unsafe extern "C" {
125 #[cfg(all(not(test), not(any(feature = "nostd", feature = "common-os"))))]
126 fn runtime_entry(argc: i32, argv: *const *const u8, env: *const *const u8) -> !;
127 #[cfg(all(not(test), any(feature = "nostd", feature = "common-os")))]
128 fn main(argc: i32, argv: *const *const u8, env: *const *const u8);
129 }
130
131 if env::is_uhyve() {
132 info!("Hermit is running on uhyve!");
133 } else {
134 info!("Hermit is running on common system!");
135 }
136
137 drivers::init();
139 crate::executor::init();
140
141 syscalls::init();
142 fs::init();
143 #[cfg(feature = "shell")]
144 shell::init();
145
146 #[cfg(not(test))]
148 let (argc, argv, environ) = syscalls::get_application_parameters();
149
150 core_scheduler().reschedule();
152
153 info!("Jumping into application");
154
155 #[cfg(not(test))]
156 unsafe {
157 #[cfg(all(not(test), not(any(feature = "nostd", feature = "common-os"))))]
159 runtime_entry(argc, argv, environ);
160 #[cfg(all(not(test), any(feature = "nostd", feature = "common-os")))]
161 main(argc, argv, environ);
162 }
163 #[cfg(test)]
164 test_main();
165}
166
167#[cfg(feature = "smp")]
168fn synch_all_cores() {
169 static CORE_COUNTER: AtomicU32 = AtomicU32::new(0);
170
171 CORE_COUNTER.fetch_add(1, Ordering::SeqCst);
172
173 let possible_cpus = kernel::get_possible_cpus();
174 while CORE_COUNTER.load(Ordering::SeqCst) != possible_cpus {
175 spin_loop();
176 }
177}
178
179#[cfg(target_os = "none")]
181fn boot_processor_main() -> ! {
182 hermit_sync::Lazy::force(&console::CONSOLE);
184 unsafe {
185 logging::init();
186 }
187
188 info!("Welcome to Hermit {}", env!("CARGO_PKG_VERSION"));
189 if let Some(git_version) = built_info::GIT_VERSION {
190 let dirty = if built_info::GIT_DIRTY == Some(true) {
191 " (dirty)"
192 } else {
193 ""
194 };
195
196 let opt_level = if built_info::OPT_LEVEL == "3" {
197 format_args!("")
198 } else {
199 format_args!(" (opt-level={})", built_info::OPT_LEVEL)
200 };
201
202 info!("Git version: {git_version}{dirty}{opt_level}");
203 }
204 info!("Enabled features: {}", built_info::FEATURES_LOWERCASE_STR);
205 info!("Built on {}", built_info::BUILT_TIME_UTC);
206
207 info!("Kernel starts at {:p}", env::get_base_address());
208
209 if let Some(fdt) = env::fdt() {
210 info!("FDT:\n{fdt:#?}");
211 }
212
213 unsafe extern "C" {
214 static mut __bss_start: u8;
215 }
216 let bss_ptr = core::ptr::addr_of_mut!(__bss_start);
217 info!("BSS starts at {bss_ptr:p}");
218 info!("tls_info = {:#x?}", env::boot_info().load_info.tls_info);
219 arch::boot_processor_init();
220
221 #[cfg(not(target_arch = "riscv64"))]
222 scheduler::add_current_core();
223 interrupts::enable();
224
225 arch::kernel::boot_next_processor();
226
227 #[cfg(feature = "smp")]
228 synch_all_cores();
229
230 #[cfg(feature = "pci")]
231 info!("Compiled with PCI support");
232 #[cfg(all(feature = "acpi", target_arch = "x86_64"))]
233 info!("Compiled with ACPI support");
234 #[cfg(all(feature = "fsgsbase", target_arch = "x86_64"))]
235 info!("Compiled with FSGSBASE support");
236 #[cfg(feature = "smp")]
237 info!("Compiled with SMP support");
238
239 if is_uhyve_with_pci() || !env::is_uhyve() {
240 #[cfg(feature = "pci")]
241 crate::drivers::pci::print_information();
242 }
243
244 unsafe {
246 scheduler::PerCoreScheduler::spawn(
247 initd,
248 0,
249 scheduler::task::NORMAL_PRIO,
250 0,
251 USER_STACK_SIZE,
252 )
253 };
254
255 PerCoreScheduler::run();
257}
258
259#[cfg(all(target_os = "none", feature = "smp"))]
261fn application_processor_main() -> ! {
262 arch::application_processor_init();
263 #[cfg(not(target_arch = "riscv64"))]
264 scheduler::add_current_core();
265 interrupts::enable();
266 arch::kernel::boot_next_processor();
267
268 debug!("Entering idle loop for application processor");
269
270 synch_all_cores();
271 crate::executor::init();
272
273 PerCoreScheduler::run();
275}
276
277#[cfg(target_os = "none")]
278#[panic_handler]
279fn panic(info: &core::panic::PanicInfo<'_>) -> ! {
280 let core_id = crate::arch::core_local::core_id();
281 panic_println!("[{core_id}][PANIC] {info}\n");
282
283 crate::scheduler::shutdown(1);
284}