os/
main.rs

1//! The main module and entrypoint
2//!
3//! Various facilities of the kernels are implemented as submodules. The most
4//! important ones are:
5//!
6//! - [`trap`]: Handles all cases of switching from userspace to the kernel
7//! - [`task`]: Task management
8//! - [`syscall`]: System call handling and implementation
9//!
10//! The operating system also starts in this module. Kernel code starts
11//! executing from `entry.asm`, after which [`rust_main()`] is called to
12//! initialize various pieces of functionality. (See its source code for
13//! details.)
14//!
15//! We then call [`task::run_first_task()`] and for the first time go to
16//! userspace.
17
18#![deny(missing_docs)]
19#![deny(warnings)]
20#![no_std]
21#![no_main]
22
23use core::arch::global_asm;
24use log::*;
25
26#[path = "boards/qemu.rs"]
27mod board;
28
29#[macro_use]
30mod console;
31mod config;
32mod lang_items;
33mod loader;
34mod logging;
35mod sbi;
36mod sync;
37pub mod syscall;
38pub mod task;
39mod timer;
40pub mod trap;
41
42global_asm!(include_str!("entry.asm"));
43global_asm!(include_str!("link_app.S"));
44
45/// clear BSS segment
46fn clear_bss() {
47    unsafe extern "C" {
48        safe fn sbss();
49        safe fn ebss();
50    }
51    unsafe {
52        core::slice::from_raw_parts_mut(sbss as usize as *mut u8, ebss as usize - sbss as usize)
53            .fill(0);
54    }
55}
56
57/// the rust entry-point of os
58#[unsafe(no_mangle)]
59pub fn rust_main() -> ! {
60    clear_bss();
61    logging::init();
62    info!("[kernel] Hello, world!");
63    trap::init();
64    loader::load_apps();
65    trap::enable_timer_interrupt();
66    timer::set_next_trigger();
67    task::run_first_task();
68    panic!("Unreachable in rust_main!");
69}