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