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