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
32#[path = "boards/qemu.rs"]
33mod board;
34
35#[macro_use]
36mod console;
37mod config;
38mod device_tree;
39mod lang_items;
40mod loader;
41mod logging;
42mod sbi;
43mod sync;
44pub mod syscall;
45pub mod task;
46mod timer;
47pub mod trap;
48
49global_asm!(include_str!("entry.asm"));
50global_asm!(include_str!("link_app.S"));
51
52/// clear BSS segment
53fn clear_bss() {
54    unsafe extern "C" {
55        safe fn sbss();
56        safe fn ebss();
57    }
58    unsafe {
59        core::slice::from_raw_parts_mut(
60            linker_symbol_addr!(sbss) as *mut u8,
61            linker_symbol_addr!(ebss) - linker_symbol_addr!(sbss),
62        )
63        .fill(0);
64    }
65}
66
67/// the rust entry-point of os
68#[unsafe(no_mangle)]
69pub fn rust_main(_hart_id: usize, dtb_pa: usize) -> ! {
70    clear_bss();
71    logging::init();
72    device_tree::init(dtb_pa);
73    info!("[kernel] Hello, world!");
74    trap::init();
75    loader::load_apps();
76    trap::enable_timer_interrupt();
77    timer::set_next_trigger();
78    task::run_first_task();
79    panic!("Unreachable in rust_main!");
80}