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#[macro_use]
46extern crate num_derive;
47
48#[cfg(feature = "smp")]
49use core::hint::spin_loop;
50#[cfg(feature = "smp")]
51use core::sync::atomic::{AtomicU32, Ordering};
52
53use arch::core_local::*;
54
55pub(crate) use crate::arch::*;
56pub use crate::config::DEFAULT_STACK_SIZE;
57pub(crate) use crate::config::*;
58pub use crate::fs::create_file;
59use crate::kernel::is_uhyve_with_pci;
60use crate::scheduler::{PerCoreScheduler, PerCoreSchedulerExt};
61
62#[macro_use]
63mod macros;
64
65#[macro_use]
66mod logging;
67
68pub mod arch;
69mod config;
70pub mod console;
71mod drivers;
72mod entropy;
73mod env;
74pub mod errno;
75mod executor;
76pub mod fd;
77pub mod fs;
78mod init_cell;
79pub mod io;
80pub mod mm;
81pub mod scheduler;
82#[cfg(all(feature = "shell", target_arch = "x86_64"))]
83mod shell;
84mod synch;
85pub mod syscalls;
86pub mod time;
87
88hermit_entry::define_abi_tag!();
89
90#[cfg(target_os = "none")]
91hermit_entry::define_entry_version!();
92
93#[cfg(test)]
94#[cfg(target_os = "none")]
95#[unsafe(no_mangle)]
96extern "C" fn runtime_entry(_argc: i32, _argv: *const *const u8, _env: *const *const u8) -> ! {
97 println!("Executing hermit unittests. Any arguments are dropped");
98 test_main();
99 core_scheduler().exit(0)
100}
101
102#[cfg(test)]
104pub fn test_runner(tests: &[&dyn Fn()]) {
105 println!("Running {} tests", tests.len());
106 for test in tests {
107 test();
108 }
109 core_scheduler().exit(0)
110}
111
112#[cfg(target_os = "none")]
113#[test_case]
114fn trivial_test() {
115 println!("Test test test");
116 panic!("Test called");
117}
118
119#[cfg(target_os = "none")]
121extern "C" fn initd(_arg: usize) {
122 unsafe extern "C" {
123 #[cfg(all(not(test), not(any(feature = "nostd", feature = "common-os"))))]
124 fn runtime_entry(argc: i32, argv: *const *const u8, env: *const *const u8) -> !;
125 #[cfg(all(not(test), any(feature = "nostd", feature = "common-os")))]
126 fn main(argc: i32, argv: *const *const u8, env: *const *const u8);
127 }
128
129 if env::is_uhyve() {
130 info!("Hermit is running on uhyve!");
131 } else {
132 info!("Hermit is running on common system!");
133 }
134
135 drivers::init();
137 crate::executor::init();
138
139 syscalls::init();
140 fs::init();
141 #[cfg(all(feature = "shell", target_arch = "x86_64"))]
142 shell::init();
143
144 #[cfg(not(test))]
146 let (argc, argv, environ) = syscalls::get_application_parameters();
147
148 core_scheduler().reschedule();
150
151 info!("Jumping into application");
152
153 #[cfg(not(test))]
154 unsafe {
155 #[cfg(all(not(test), not(any(feature = "nostd", feature = "common-os"))))]
157 runtime_entry(argc, argv, environ);
158 #[cfg(all(not(test), any(feature = "nostd", feature = "common-os")))]
159 main(argc, argv, environ);
160 }
161 #[cfg(test)]
162 test_main();
163}
164
165#[cfg(feature = "smp")]
166fn synch_all_cores() {
167 static CORE_COUNTER: AtomicU32 = AtomicU32::new(0);
168
169 CORE_COUNTER.fetch_add(1, Ordering::SeqCst);
170
171 let possible_cpus = kernel::get_possible_cpus();
172 while CORE_COUNTER.load(Ordering::SeqCst) != possible_cpus {
173 spin_loop();
174 }
175}
176
177#[cfg(target_os = "none")]
179fn boot_processor_main() -> ! {
180 hermit_sync::Lazy::force(&console::CONSOLE);
182 unsafe {
183 logging::init();
184 }
185
186 info!("Welcome to Hermit {}", env!("CARGO_PKG_VERSION"));
187 info!("Kernel starts at {:p}", env::get_base_address());
188
189 if let Some(fdt) = env::fdt() {
190 info!("FDT:\n{fdt:#?}");
191 }
192
193 unsafe extern "C" {
194 static mut __bss_start: u8;
195 }
196 let bss_ptr = core::ptr::addr_of_mut!(__bss_start);
197 info!("BSS starts at {bss_ptr:p}");
198 info!("tls_info = {:#x?}", env::boot_info().load_info.tls_info);
199 arch::boot_processor_init();
200
201 #[cfg(not(target_arch = "riscv64"))]
202 scheduler::add_current_core();
203 interrupts::enable();
204
205 arch::kernel::boot_next_processor();
206
207 #[cfg(feature = "smp")]
208 synch_all_cores();
209
210 #[cfg(feature = "pci")]
211 info!("Compiled with PCI support");
212 #[cfg(all(feature = "acpi", target_arch = "x86_64"))]
213 info!("Compiled with ACPI support");
214 #[cfg(all(feature = "fsgsbase", target_arch = "x86_64"))]
215 info!("Compiled with FSGSBASE support");
216 #[cfg(feature = "smp")]
217 info!("Compiled with SMP support");
218
219 if is_uhyve_with_pci() || !env::is_uhyve() {
220 #[cfg(feature = "pci")]
221 crate::drivers::pci::print_information();
222 }
223
224 unsafe {
226 scheduler::PerCoreScheduler::spawn(
227 initd,
228 0,
229 scheduler::task::NORMAL_PRIO,
230 0,
231 USER_STACK_SIZE,
232 )
233 };
234
235 PerCoreScheduler::run();
237}
238
239#[cfg(all(target_os = "none", feature = "smp"))]
241fn application_processor_main() -> ! {
242 arch::application_processor_init();
243 #[cfg(not(target_arch = "riscv64"))]
244 scheduler::add_current_core();
245 interrupts::enable();
246 arch::kernel::boot_next_processor();
247
248 debug!("Entering idle loop for application processor");
249
250 synch_all_cores();
251 crate::executor::init();
252
253 PerCoreScheduler::run();
255}
256
257#[cfg(target_os = "none")]
258#[panic_handler]
259fn panic(info: &core::panic::PanicInfo<'_>) -> ! {
260 let core_id = crate::arch::core_local::core_id();
261 panic_println!("[{core_id}][PANIC] {info}\n");
262
263 crate::scheduler::shutdown(1);
264}