Skip to main content

hermit/
rt.rs

1use crate::arch::kernel;
2use crate::arch::kernel::core_local::{core_id, core_scheduler};
3use crate::arch::kernel::interrupts;
4use crate::env::{self, StartInfo};
5use crate::scheduler::{PerCoreScheduler, PerCoreSchedulerExt};
6use crate::{built_info, console, drivers, executor, fs, logging, mm, scheduler, syscalls};
7
8#[cfg(feature = "hermit-entry")]
9hermit_entry::define_abi_tag!();
10
11#[cfg(feature = "hermit-entry")]
12hermit_entry::define_entry_version!();
13
14#[cfg(test)]
15#[unsafe(no_mangle)]
16extern "C" fn runtime_entry(_argc: i32, _argv: *const *const u8, _env: *const *const u8) -> ! {
17	println!("Executing hermit unittests. Any arguments are dropped");
18	crate::test_main();
19	core_scheduler().exit(0)
20}
21
22//https://github.com/rust-lang/rust/issues/50297#issuecomment-524180479
23#[cfg(test)]
24pub fn test_runner(tests: &[&dyn Fn()]) {
25	println!("Running {} tests", tests.len());
26	for test in tests {
27		test();
28	}
29	core_scheduler().exit(0)
30}
31
32#[test_case]
33fn trivial_test() {
34	println!("Test test test");
35	panic!("Test called");
36}
37
38/// Entry point of a kernel thread, which initialize the libos
39extern "C" fn initd(_arg: usize) {
40	unsafe extern "C" {
41		#[cfg(all(not(test), not(any(feature = "nostd", feature = "common-os"))))]
42		fn runtime_entry(argc: i32, argv: *const *const u8, env: *const *const u8) -> !;
43		#[cfg(all(not(test), any(feature = "nostd", feature = "common-os")))]
44		fn main(argc: i32, argv: *const *const u8, env: *const *const u8);
45	}
46
47	// Initialize Drivers
48	drivers::init();
49	// The filesystem needs to be initialized before network to allow writing packet captures to a file.
50	fs::init();
51	executor::init();
52
53	syscalls::init();
54	#[cfg(feature = "shell")]
55	crate::shell::init();
56
57	// Get the application arguments and environment variables.
58	#[cfg(not(test))]
59	let (argc, argv, environ) = syscalls::get_application_parameters();
60
61	// give the IP thread time to initialize the network interface
62	core_scheduler().reschedule();
63
64	if cfg!(feature = "warn-prebuilt") {
65		warn!("This is a prebuilt Hermit kernel.");
66		warn!("For non-default device drivers and features, consider building a custom kernel.");
67	}
68
69	info!("Jumping into application");
70
71	#[cfg(not(test))]
72	unsafe {
73		// And finally start the application.
74		#[cfg(all(not(test), not(any(feature = "nostd", feature = "common-os"))))]
75		runtime_entry(argc, argv, environ);
76		#[cfg(all(not(test), any(feature = "nostd", feature = "common-os")))]
77		main(argc, argv, environ);
78	}
79	#[cfg(test)]
80	crate::test_main();
81}
82
83#[cfg(feature = "smp")]
84fn synch_all_cores() {
85	use core::hint;
86	use core::sync::atomic::{AtomicU32, Ordering};
87
88	static CORE_COUNTER: AtomicU32 = AtomicU32::new(0);
89
90	CORE_COUNTER.fetch_add(1, Ordering::SeqCst);
91
92	let possible_cpus = kernel::get_possible_cpus();
93	while CORE_COUNTER.load(Ordering::SeqCst) != possible_cpus {
94		hint::spin_loop();
95	}
96}
97
98/// Entry Point of Hermit for the Boot Processor
99pub fn boot_processor_main() -> ! {
100	use crate::config::USER_STACK_SIZE;
101
102	// Initialize the kernel and hardware.
103	mm::claim_initial_heap();
104	hermit_sync::Lazy::force(&console::CONSOLE);
105	unsafe {
106		logging::init();
107	}
108
109	info!("Welcome to Hermit {}", env!("CARGO_PKG_VERSION"));
110	if let Some(git_version) = built_info::GIT_VERSION {
111		let dirty = if built_info::GIT_DIRTY == Some(true) {
112			" (dirty)"
113		} else {
114			""
115		};
116
117		let opt_level = if built_info::OPT_LEVEL == "3" {
118			format_args!("")
119		} else {
120			format_args!(" (opt-level={})", built_info::OPT_LEVEL)
121		};
122
123		info!("Git version: {git_version}{dirty}{opt_level}");
124	}
125	let arch = built_info::TARGET.split_once('-').unwrap().0;
126	info!("Architecture: {arch}");
127	info!("Enabled features: {}", built_info::FEATURES_LOWERCASE_STR);
128	info!("Built on {}", built_info::BUILT_TIME_UTC);
129
130	debug!("Executable start: {:p}", elf_symbols::executable_start());
131	debug!("ELF header:       {:p}", elf_symbols::elf_header());
132	debug!("Text segment end: {:p}", elf_symbols::text_end());
133	debug!("Data segment end: {:p}", elf_symbols::data_end());
134	debug!("Executable end:   {:p}", elf_symbols::executable_end());
135
136	debug!("{}", env::start_info().display());
137
138	kernel::boot_processor_init();
139
140	#[cfg(not(target_arch = "riscv64"))]
141	scheduler::add_current_core();
142	interrupts::enable();
143
144	kernel::boot_next_processor();
145
146	#[cfg(feature = "smp")]
147	synch_all_cores();
148
149	#[cfg(feature = "pci")]
150	drivers::pci::print_information();
151
152	// Start the initd task.
153	unsafe { PerCoreScheduler::spawn(initd, 0, scheduler::task::NORMAL_PRIO, 0, USER_STACK_SIZE) };
154
155	// Run the scheduler loop.
156	PerCoreScheduler::run();
157}
158
159/// Entry Point of Hermit for an Application Processor
160#[cfg(feature = "smp")]
161pub fn application_processor_main() -> ! {
162	kernel::application_processor_init();
163	#[cfg(not(target_arch = "riscv64"))]
164	scheduler::add_current_core();
165	interrupts::enable();
166	kernel::boot_next_processor();
167
168	debug!("Entering idle loop for application processor");
169
170	synch_all_cores();
171	executor::init();
172
173	// Run the scheduler loop.
174	PerCoreScheduler::run();
175}
176
177#[panic_handler]
178fn panic(info: &core::panic::PanicInfo<'_>) -> ! {
179	let core_id = core_id();
180	panic_println!("[{core_id}][PANIC] {info}\n");
181
182	scheduler::shutdown(1);
183}