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-05-20 08:50:52 +08:00
|
|
|
//!
|
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]
|
2020-12-02 10:32:26 +08:00
|
|
|
#![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;
|
|
|
|
|
2025-02-10 00:29:05 +08:00
|
|
|
use log::*;
|
|
|
|
|
2022-01-24 17:50:49 -08:00
|
|
|
#[path = "boards/qemu.rs"]
|
|
|
|
mod board;
|
|
|
|
|
2020-11-12 17:13:57 +08:00
|
|
|
#[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;
|
2025-02-10 00:29:05 +08:00
|
|
|
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
|
|
|
|
2022-01-01 02:24:51 -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() {
|
2025-02-19 21:27:17 +08:00
|
|
|
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
|
2025-02-19 21:27:17 +08:00
|
|
|
#[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();
|
2025-02-10 00:29:05 +08:00
|
|
|
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();
|
2020-11-20 01:18:25 +08:00
|
|
|
trap::init();
|
2020-12-07 18:57:23 +08:00
|
|
|
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();
|
2020-11-29 01:31:36 +08:00
|
|
|
panic!("Unreachable in rust_main!");
|
2022-01-01 02:24:51 -08:00
|
|
|
}
|