1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#![warn(clippy::all)]
#![allow(clippy::redundant_field_names)]
#![allow(clippy::identity_op)]
#![allow(clippy::needless_range_loop)]
#![allow(clippy::tabs_in_doc_comments)]
#![allow(clippy::toplevel_ref_arg)]
#![allow(clippy::not_unsafe_ptr_arg_deref)]
#![allow(clippy::println_empty_string)]
#![allow(clippy::single_match)]
#![allow(clippy::cognitive_complexity)]
#![allow(clippy::forget_copy)]
#![allow(incomplete_features)]
#![feature(abi_x86_interrupt)]
#![feature(allocator_api)]
#![feature(const_btree_new)]
#![feature(const_fn)]
#![feature(const_mut_refs)]
#![feature(global_asm)]
#![feature(lang_items)]
#![feature(linkage)]
#![feature(linked_list_cursors)]
#![feature(llvm_asm)]
#![feature(panic_info_message)]
#![feature(specialization)]
#![feature(nonnull_slice_from_raw_parts)]
#![feature(core_intrinsics)]
#![feature(alloc_error_handler)]
#![feature(vec_into_raw_parts)]
#![feature(drain_filter)]
#![allow(unused_macros)]
#![no_std]
#![cfg_attr(target_os = "hermit", feature(custom_test_frameworks))]
#![cfg_attr(target_os = "hermit", cfg_attr(test, test_runner(crate::test_runner)))]
#![cfg_attr(
target_os = "hermit",
cfg_attr(test, reexport_test_harness_main = "test_main")
)]
#![cfg_attr(target_os = "hermit", cfg_attr(test, no_main))]
#[macro_use]
extern crate alloc;
#[macro_use]
extern crate bitflags;
extern crate crossbeam_utils;
#[macro_use]
extern crate log;
#[cfg(target_arch = "x86_64")]
extern crate multiboot;
extern crate num;
#[macro_use]
extern crate num_derive;
extern crate num_traits;
extern crate scopeguard;
#[cfg(not(target_os = "hermit"))]
#[macro_use]
extern crate std;
#[cfg(target_arch = "x86_64")]
extern crate x86;
use alloc::alloc::Layout;
use core::alloc::GlobalAlloc;
#[cfg(feature = "smp")]
use core::hint::spin_loop;
#[cfg(feature = "smp")]
use core::sync::atomic::{AtomicU32, Ordering};
use arch::percore::*;
use mm::allocator::LockedHeap;
pub use crate::arch::*;
pub use crate::config::*;
pub use crate::syscalls::*;
#[macro_use]
mod macros;
#[macro_use]
mod logging;
mod arch;
mod collections;
mod config;
mod console;
mod drivers;
pub mod environment;
mod errno;
mod kernel_message_buffer;
mod mm;
#[cfg(target_os = "hermit")]
mod runtime_glue;
mod scheduler;
mod synch;
mod syscalls;
mod util;
#[doc(hidden)]
pub fn _print(args: ::core::fmt::Arguments) {
use core::fmt::Write;
crate::console::CONSOLE.lock().write_fmt(args).unwrap();
}
#[cfg(test)]
#[cfg(target_os = "hermit")]
#[no_mangle]
extern "C" fn runtime_entry(_argc: i32, _argv: *const *const u8, _env: *const *const u8) -> ! {
println!("Executing hermit unittests. Any arguments are dropped");
test_main();
sys_exit(0);
}
#[cfg(test)]
pub fn test_runner(tests: &[&dyn Fn()]) {
println!("Running {} tests", tests.len());
for test in tests {
test();
}
sys_exit(0);
}
#[cfg(target_os = "hermit")]
#[test_case]
fn trivial_test() {
println!("Test test test");
panic!("Test called");
}
#[cfg(target_os = "hermit")]
#[global_allocator]
static ALLOCATOR: LockedHeap = LockedHeap::empty();
#[cfg(target_os = "hermit")]
pub fn __sys_malloc(size: usize, align: usize) -> *mut u8 {
let layout_res = Layout::from_size_align(size, align);
if layout_res.is_err() || size == 0 {
warn!(
"__sys_malloc called with size 0x{:x}, align 0x{:x} is an invalid layout!",
size, align
);
return core::ptr::null::<*mut u8>() as *mut u8;
}
let layout = layout_res.unwrap();
let ptr = unsafe { ALLOCATOR.alloc(layout) };
trace!(
"__sys_malloc: allocate memory at 0x{:x} (size 0x{:x}, align 0x{:x})",
ptr as usize,
size,
align
);
ptr
}
#[cfg(target_os = "hermit")]
pub unsafe fn __sys_realloc(ptr: *mut u8, size: usize, align: usize, new_size: usize) -> *mut u8 {
let layout_res = Layout::from_size_align(size, align);
if layout_res.is_err() || size == 0 || new_size == 0 {
warn!(
"__sys_realloc called with ptr 0x{:x}, size 0x{:x}, align 0x{:x}, new_size 0x{:x} is an invalid layout!",
ptr as usize, size, align, new_size
);
return core::ptr::null::<*mut u8>() as *mut u8;
}
let layout = layout_res.unwrap();
let new_ptr = ALLOCATOR.realloc(ptr, layout, new_size);
if new_ptr.is_null() {
debug!(
"__sys_realloc failed to resize ptr 0x{:x} with size 0x{:x}, align 0x{:x}, new_size 0x{:x} !",
ptr as usize, size, align, new_size
);
} else {
trace!(
"__sys_realloc: resized memory at 0x{:x}, new address 0x{:x}",
ptr as usize,
new_ptr as usize
);
}
new_ptr
}
#[cfg(target_os = "hermit")]
pub unsafe fn __sys_free(ptr: *mut u8, size: usize, align: usize) {
let layout_res = Layout::from_size_align(size, align);
if layout_res.is_err() || size == 0 {
warn!(
"__sys_free called with size 0x{:x}, align 0x{:x} is an invalid layout!",
size, align
);
debug_assert!(layout_res.is_err(), "__sys_free error: Invalid layout");
debug_assert_ne!(size, 0, "__sys_free error: size cannot be 0");
} else {
trace!(
"sys_free: deallocate memory at 0x{:x} (size 0x{:x})",
ptr as usize,
size
);
}
let layout = layout_res.unwrap();
ALLOCATOR.dealloc(ptr, layout);
}
#[cfg(target_os = "hermit")]
extern "C" {
static mut __bss_start: usize;
}
#[cfg(feature = "newlib")]
fn has_ipdevice() -> bool {
arch::x86_64::kernel::has_ipdevice()
}
#[cfg(target_os = "hermit")]
extern "C" fn initd(_arg: usize) {
extern "C" {
#[cfg(not(test))]
fn runtime_entry(argc: i32, argv: *const *const u8, env: *const *const u8) -> !;
#[cfg(feature = "newlib")]
fn init_lwip();
}
#[cfg(feature = "newlib")]
unsafe {
if has_ipdevice() {
init_lwip();
}
}
if environment::is_uhyve() {
info!("HermitCore is running on uhyve!");
} else if !environment::is_single_kernel() {
info!("HermitCore is running side-by-side to Linux!");
} else {
info!("HermitCore is running on common system!");
}
#[cfg(all(target_arch = "x86_64", feature = "pci"))]
x86_64::kernel::pci::init_drivers();
syscalls::init();
#[cfg(not(test))]
let (argc, argv, environ) = syscalls::get_application_parameters();
config::sanity_check();
core_scheduler().reschedule();
#[cfg(not(test))]
unsafe {
runtime_entry(argc, argv, environ)
}
#[cfg(test)]
test_main();
}
#[cfg(feature = "smp")]
fn synch_all_cores() {
static CORE_COUNTER: AtomicU32 = AtomicU32::new(0);
CORE_COUNTER.fetch_add(1, Ordering::SeqCst);
while CORE_COUNTER.load(Ordering::SeqCst) != get_processor_count() {
spin_loop();
}
}
#[cfg(target_os = "hermit")]
fn boot_processor_main() -> ! {
arch::message_output_init();
logging::init();
info!("Welcome to HermitCore-rs {}", env!("CARGO_PKG_VERSION"));
info!("Kernel starts at 0x{:x}", environment::get_base_address());
info!("BSS starts at 0x{:x}", unsafe {
&__bss_start as *const usize as usize
});
info!(
"TLS starts at 0x{:x} (size {} Bytes)",
environment::get_tls_start(),
environment::get_tls_memsz()
);
arch::boot_processor_init();
scheduler::add_current_core();
if environment::is_single_kernel() && !environment::is_uhyve() {
arch::boot_application_processors();
}
#[cfg(feature = "smp")]
synch_all_cores();
#[cfg(feature = "pci")]
info!("Compiled with PCI support");
#[cfg(feature = "acpi")]
info!("Compiled with ACPI support");
#[cfg(feature = "fsgsbase")]
info!("Compiled with FSGSBASE support");
#[cfg(feature = "smp")]
info!("Compiled with SMP support");
scheduler::PerCoreScheduler::spawn(initd, 0, scheduler::task::NORMAL_PRIO, 0, USER_STACK_SIZE);
let core_scheduler = core_scheduler();
core_scheduler.run();
}
#[cfg(all(target_os = "hermit", feature = "smp"))]
fn application_processor_main() -> ! {
arch::application_processor_init();
scheduler::add_current_core();
info!("Entering idle loop for application processor");
synch_all_cores();
let core_scheduler = core_scheduler();
core_scheduler.run();
}