rcore-tutorial/os/src/main.rs

84 lines
1.9 KiB
Rust
Raw Normal View History

2022-04-30 16:04:58 +08:00
//! The main module and entrypoint
//!
//! Various facilities of the kernels are implemented as submodules. The most
//! important ones are:
//!
//! - [`trap`]: Handles all cases of switching from userspace to the kernel
//! - [`task`]: Task management
//! - [`syscall`]: System call handling and implementation
//! - [`mm`]: Address map using SV39
//! - [`sync`]: Wrap a static data structure inside it so that we are able to access it without any `unsafe`.
//! - [`fs`]: Separate user from file system with some structures
//!
2022-04-30 16:04:58 +08:00
//! The operating system also starts in this module. Kernel code starts
//! executing from `entry.asm`, after which [`rust_main()`] is called to
//! initialize various pieces of functionality. (See its source code for
//! details.)
//!
//! We then call [`task::run_tasks()`] and for the first time go to
//! userspace.
#![deny(missing_docs)]
#![deny(warnings)]
#![allow(unused_imports)]
2020-11-10 23:02:38 +08:00
#![no_std]
#![no_main]
#![feature(alloc_error_handler)]
extern crate alloc;
2020-11-10 23:02:38 +08:00
2020-12-04 17:23:35 +08:00
#[macro_use]
extern crate bitflags;
use log::*;
2022-01-24 17:50:49 -08:00
#[path = "boards/qemu.rs"]
mod board;
#[macro_use]
mod console;
2022-01-21 14:56:19 -08:00
mod config;
mod drivers;
2022-04-30 16:04:58 +08:00
pub mod fs;
pub mod lang_items;
mod logging;
2022-04-30 16:04:58 +08:00
pub mod mm;
pub mod sbi;
pub mod sync;
pub mod syscall;
pub mod task;
pub mod timer;
pub mod trap;
2020-11-10 23:02:38 +08:00
use core::arch::global_asm;
2020-11-11 16:50:00 +08:00
global_asm!(include_str!("entry.asm"));
2022-04-30 16:04:58 +08:00
/// clear BSS segment
2020-11-13 12:06:39 +08:00
fn clear_bss() {
unsafe extern "C" {
safe fn sbss();
safe fn ebss();
2020-11-13 12:06:39 +08:00
}
2021-07-20 22:10:22 +08:00
unsafe {
2022-01-21 14:56:19 -08:00
core::slice::from_raw_parts_mut(sbss as usize as *mut u8, ebss as usize - sbss as usize)
.fill(0);
2021-07-20 22:10:22 +08:00
}
2020-11-13 12:06:39 +08:00
}
2022-04-30 16:04:58 +08:00
/// the rust entry-point of os
#[unsafe(no_mangle)]
2020-11-11 23:40:00 +08:00
pub fn rust_main() -> ! {
2020-11-13 12:06:39 +08:00
clear_bss();
logging::init();
info!("[kernel] Hello, world!");
2020-12-03 10:40:30 +08:00
mm::init();
2020-12-04 17:23:35 +08:00
mm::remap_test();
trap::init();
trap::enable_timer_interrupt();
timer::set_next_trigger();
2022-01-18 02:48:50 -08:00
fs::list_apps();
task::add_initproc();
2020-12-08 15:37:10 +08:00
task::run_tasks();
panic!("Unreachable in rust_main!");
}