asterinas/ostd/src/lib.rs

156 lines
3.9 KiB
Rust
Raw Normal View History

2024-01-03 03:22:36 +00:00
// SPDX-License-Identifier: MPL-2.0
2024-06-19 08:18:39 +00:00
//! The standard library for Asterinas and other Rust OSes.
2022-08-23 09:50:07 +00:00
#![feature(alloc_error_handler)]
#![feature(const_mut_refs)]
#![feature(const_ptr_sub_ptr)]
#![feature(const_trait_impl)]
2024-06-28 08:39:25 +00:00
#![feature(core_intrinsics)]
2023-12-02 09:23:08 +00:00
#![feature(coroutines)]
#![feature(fn_traits)]
#![feature(generic_const_exprs)]
2023-12-02 09:23:08 +00:00
#![feature(iter_from_coroutine)]
2023-09-21 02:16:33 +00:00
#![feature(let_chains)]
#![feature(min_specialization)]
#![feature(negative_impls)]
#![feature(new_uninit)]
#![feature(panic_info_message)]
2023-10-30 05:29:01 +00:00
#![feature(ptr_sub_ptr)]
#![feature(strict_provenance)]
// The `generic_const_exprs` feature is incomplete however required for the page table
// const generic implementation. We are using this feature in a conservative manner.
#![allow(incomplete_features)]
2024-06-28 08:39:25 +00:00
#![allow(internal_features)]
#![no_std]
2024-05-23 08:55:39 +00:00
#![warn(missing_docs)]
2022-08-26 15:59:20 +00:00
2022-08-08 01:01:42 +00:00
extern crate alloc;
2023-10-08 09:40:58 +00:00
extern crate static_assertions;
2022-08-08 01:01:42 +00:00
pub mod arch;
pub mod boot;
2023-07-05 13:35:07 +00:00
pub mod bus;
pub mod collections;
2023-09-08 03:54:01 +00:00
pub mod console;
2022-08-08 01:01:42 +00:00
pub mod cpu;
mod error;
2023-07-23 10:40:41 +00:00
pub mod io_mem;
pub mod logger;
pub mod mm;
pub mod panicking;
2022-08-08 01:01:42 +00:00
pub mod prelude;
pub mod sync;
2022-08-08 01:01:42 +00:00
pub mod task;
2022-11-02 11:35:39 +00:00
pub mod trap;
2022-08-08 01:01:42 +00:00
pub mod user;
2024-08-22 13:17:38 +00:00
use core::sync::atomic::AtomicBool;
2024-06-20 06:16:04 +00:00
pub use ostd_macros::main;
pub use ostd_pod::Pod;
2023-02-22 14:57:19 +00:00
pub use self::{error::Error, prelude::Result};
// [`CpuLocalCell`] is easy to be misused, so we don't expose it to the users.
pub(crate) use crate::cpu::local::cpu_local_cell;
2024-06-20 06:16:04 +00:00
/// Initializes OSTD.
///
/// This function represents the first phase booting up the system. It makes
2024-06-19 08:18:39 +00:00
/// all functionalities of OSTD available after the call.
///
/// # Safety
///
/// This function should be called only once and only on the BSP.
//
// TODO: We need to refactor this function to make it more modular and
// make inter-initialization-dependencies more clear and reduce usages of
// boot stage only global variables.
#[doc(hidden)]
pub unsafe fn init() {
arch::enable_cpu_features();
arch::serial::init();
#[cfg(feature = "cvm_guest")]
arch::check_tdx_init();
// SAFETY: This function is called only once and only on the BSP.
unsafe { cpu::local::early_init_bsp_local_base() };
mm::heap_allocator::init();
boot::init();
logger::init();
mm::page::allocator::init();
2024-06-12 08:55:59 +00:00
mm::kspace::init_boot_page_table();
mm::kspace::init_kernel_page_table(mm::init_page_meta());
mm::misc_init();
2022-11-09 12:33:41 +00:00
trap::init();
arch::init_on_bsp();
2023-07-23 10:31:43 +00:00
bus::init();
2024-06-12 08:55:59 +00:00
mm::kspace::activate_kernel_page_table();
arch::irq::enable_local();
invoke_ffi_init_funcs();
2022-09-05 06:41:15 +00:00
}
2024-08-22 13:17:38 +00:00
/// Indicates whether the kernel is in bootstrap context.
pub static IN_BOOTSTRAP_CONTEXT: AtomicBool = AtomicBool::new(true);
/// Invoke the initialization functions defined in the FFI.
/// The component system uses this function to call the initialization functions of
/// the components.
fn invoke_ffi_init_funcs() {
extern "C" {
fn __sinit_array();
fn __einit_array();
}
2023-09-04 03:04:42 +00:00
let call_len = (__einit_array as usize - __sinit_array as usize) / 8;
for i in 0..call_len {
unsafe {
2023-09-04 03:04:42 +00:00
let function = (__sinit_array as usize + 8 * i) as *const fn();
(*function)();
}
}
}
/// Simple unit tests for the ktest framework.
#[cfg(ktest)]
2023-11-04 08:41:30 +00:00
mod test {
2024-06-21 05:51:13 +00:00
use crate::prelude::*;
2023-11-04 08:41:30 +00:00
#[ktest]
#[allow(clippy::eq_op)]
2023-11-04 08:41:30 +00:00
fn trivial_assertion() {
assert_eq!(0, 0);
}
#[ktest]
#[should_panic]
fn failing_assertion() {
assert_eq!(0, 1);
}
#[ktest]
#[should_panic(expected = "expected panic message")]
fn expect_panic() {
panic!("expected panic message");
}
2023-11-04 08:41:30 +00:00
}
2024-06-21 05:51:13 +00:00
#[doc(hidden)]
2024-06-21 05:51:13 +00:00
pub mod ktest {
//! The module re-exports everything from the [`ostd_test`] crate, as well
//! as the test entry point macro.
//!
//! It is rather discouraged to use the definitions here directly. The
//! `ktest` attribute is sufficient for all normal use cases.
pub use ostd_macros::test_main as main;
pub use ostd_test::*;
2024-06-21 05:51:13 +00:00
}