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//! - [`mm`]: Address map using SV39
10//! - [`sync`]: Wrap a static data structure inside it so that we are able to access it without any `unsafe`.
11//! - [`fs`]: Separate user from file system with some structures
12//!
13//! The operating system also starts in this module. Kernel code starts
14//! executing from `entry.asm`, after which [`rust_main()`] is called to
15//! initialize various pieces of functionality. (See its source code for
16//! details.)
17//!
18//! We then call [`task::run_tasks()`] and for the first time go to
19//! userspace.
20
21#![deny(missing_docs)]
22#![deny(warnings)]
23#![allow(unused_imports)]
24#![no_std]
25#![no_main]
26
27macro_rules! linker_symbol_addr {
28    ($symbol:path) => {
29        ($symbol as *const ()).addr()
30    };
31}
32
33extern crate alloc;
34
35#[macro_use]
36extern crate bitflags;
37
38use log::*;
39
40#[path = "boards/qemu.rs"]
41mod board;
42
43#[macro_use]
44mod console;
45mod config;
46mod device_tree;
47mod drivers;
48pub mod fs;
49pub mod lang_items;
50mod logging;
51pub mod mm;
52pub mod sbi;
53pub mod sync;
54pub mod syscall;
55pub mod task;
56pub mod timer;
57pub mod trap;
58
59use core::arch::global_asm;
60
61global_asm!(include_str!("entry.asm"));
62/// clear BSS segment
63fn clear_bss() {
64    unsafe extern "C" {
65        safe fn sbss();
66        safe fn ebss();
67    }
68    unsafe {
69        core::slice::from_raw_parts_mut(
70            linker_symbol_addr!(sbss) as *mut u8,
71            linker_symbol_addr!(ebss) - linker_symbol_addr!(sbss),
72        )
73        .fill(0);
74    }
75}
76
77/// the rust entry-point of os
78#[unsafe(no_mangle)]
79pub fn rust_main(_hart_id: usize, dtb_pa: usize) -> ! {
80    clear_bss();
81    logging::init();
82    device_tree::init(dtb_pa);
83    info!("[kernel] Hello, world!");
84    mm::init();
85    mm::remap_test();
86    trap::init();
87    trap::enable_timer_interrupt();
88    timer::set_next_trigger();
89    fs::list_apps();
90    task::add_initproc();
91    task::run_tasks();
92    panic!("Unreachable in rust_main!");
93}