Skip to main content

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
23macro_rules! linker_symbol_addr {
24    ($symbol:path) => {
25        ($symbol as *const ()).addr()
26    };
27}
28
29use core::arch::global_asm;
30use log::*;
31#[macro_use]
32mod console;
33mod config;
34mod lang_items;
35mod loader;
36mod logging;
37mod sbi;
38mod sync;
39pub mod syscall;
40pub mod task;
41pub mod trap;
42
43global_asm!(include_str!("entry.asm"));
44global_asm!(include_str!("link_app.S"));
45
46/// clear BSS segment
47fn clear_bss() {
48    unsafe extern "C" {
49        safe fn sbss();
50        safe fn ebss();
51    }
52    unsafe {
53        core::slice::from_raw_parts_mut(
54            linker_symbol_addr!(sbss) as *mut u8,
55            linker_symbol_addr!(ebss) - linker_symbol_addr!(sbss),
56        )
57        .fill(0);
58    }
59}
60
61/// the rust entry-point of os
62#[unsafe(no_mangle)]
63pub fn rust_main() -> ! {
64    clear_bss();
65    logging::init();
66    info!("[kernel] Hello, world!");
67    trap::init();
68    loader::load_apps();
69    task::run_first_task();
70    panic!("Unreachable in rust_main!");
71}