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