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)]
|
2023-11-04 16:14:28 +00:00
|
|
|
#![feature(const_mut_refs)]
|
|
|
|
|
#![feature(const_ptr_sub_ptr)]
|
2023-03-09 05:26:54 +00:00
|
|
|
#![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)]
|
2023-11-04 16:14:28 +00:00
|
|
|
#![feature(fn_traits)]
|
2024-03-25 05:25:32 +00:00
|
|
|
#![feature(generic_const_exprs)]
|
2024-08-24 02:36:38 +00:00
|
|
|
#![feature(is_none_or)]
|
2023-12-02 09:23:08 +00:00
|
|
|
#![feature(iter_from_coroutine)]
|
2023-09-21 02:16:33 +00:00
|
|
|
#![feature(let_chains)]
|
2024-08-02 12:28:03 +00:00
|
|
|
#![feature(min_specialization)]
|
2023-11-04 16:14:28 +00:00
|
|
|
#![feature(negative_impls)]
|
|
|
|
|
#![feature(new_uninit)]
|
|
|
|
|
#![feature(panic_info_message)]
|
2023-10-30 05:29:01 +00:00
|
|
|
#![feature(ptr_sub_ptr)]
|
2023-11-04 16:14:28 +00:00
|
|
|
#![feature(strict_provenance)]
|
2024-09-19 02:11:32 +00:00
|
|
|
#![feature(sync_unsafe_cell)]
|
2024-03-25 05:25:32 +00:00
|
|
|
// 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)]
|
2023-11-04 16:14:28 +00:00
|
|
|
#![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
|
|
|
|
2023-03-25 09:27:09 +00:00
|
|
|
pub mod arch;
|
2023-07-25 04:22:03 +00:00
|
|
|
pub mod boot;
|
2023-07-05 13:35:07 +00:00
|
|
|
pub mod bus;
|
2024-04-08 06:26:36 +00:00
|
|
|
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;
|
2023-07-25 02:18:20 +00:00
|
|
|
pub mod logger;
|
2024-05-26 17:53:44 +00:00
|
|
|
pub mod mm;
|
2023-11-04 16:14:28 +00:00
|
|
|
pub mod panicking;
|
2022-08-08 01:01:42 +00:00
|
|
|
pub mod prelude;
|
2024-08-15 11:43:37 +00:00
|
|
|
pub mod smp;
|
2022-08-17 08:40:55 +00:00
|
|
|
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;
|
2024-07-01 08:00:09 +00:00
|
|
|
pub use ostd_pod::Pod;
|
2023-02-22 14:57:19 +00:00
|
|
|
|
2024-08-02 12:28:03 +00:00
|
|
|
pub use self::{error::Error, prelude::Result};
|
2024-08-26 07:31:58 +00:00
|
|
|
// [`CpuLocalCell`] is easy to be misused, so we don't expose it to the users.
|
2024-08-02 12:28:03 +00:00
|
|
|
pub(crate) use crate::cpu::local::cpu_local_cell;
|
2024-02-25 14:09:24 +00:00
|
|
|
|
2024-06-20 06:16:04 +00:00
|
|
|
/// Initializes OSTD.
|
2024-06-02 11:00:34 +00:00
|
|
|
///
|
|
|
|
|
/// 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.
|
2024-06-02 11:00:34 +00:00
|
|
|
///
|
2024-08-13 14:51:27 +00:00
|
|
|
/// # 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() {
|
2024-07-06 04:00:40 +00:00
|
|
|
arch::enable_cpu_features();
|
|
|
|
|
arch::serial::init();
|
|
|
|
|
|
2024-08-09 05:21:52 +00:00
|
|
|
#[cfg(feature = "cvm_guest")]
|
2024-07-06 04:00:40 +00:00
|
|
|
arch::check_tdx_init();
|
2024-05-26 16:25:59 +00:00
|
|
|
|
2024-07-19 13:23:28 +00:00
|
|
|
// SAFETY: This function is called only once and only on the BSP.
|
2024-08-02 12:28:03 +00:00
|
|
|
unsafe { cpu::local::early_init_bsp_local_base() };
|
2024-07-19 13:23:28 +00:00
|
|
|
|
2024-05-26 17:53:44 +00:00
|
|
|
mm::heap_allocator::init();
|
2024-05-26 16:25:59 +00:00
|
|
|
|
2023-07-25 04:22:03 +00:00
|
|
|
boot::init();
|
2024-06-28 05:29:28 +00:00
|
|
|
logger::init();
|
2024-05-26 16:25:59 +00:00
|
|
|
|
2024-05-26 17:53:44 +00:00
|
|
|
mm::page::allocator::init();
|
2024-06-12 08:55:59 +00:00
|
|
|
mm::kspace::init_kernel_page_table(mm::init_page_meta());
|
2024-05-26 17:53:44 +00:00
|
|
|
mm::misc_init();
|
2024-05-26 16:25:59 +00:00
|
|
|
|
2024-08-26 07:49:06 +00:00
|
|
|
// SAFETY: This function is called only once in the entire system.
|
|
|
|
|
unsafe { trap::softirq::init() };
|
2024-07-06 04:43:33 +00:00
|
|
|
arch::init_on_bsp();
|
2024-07-06 04:00:40 +00:00
|
|
|
|
2024-08-15 11:43:37 +00:00
|
|
|
smp::init();
|
|
|
|
|
|
2023-07-23 10:31:43 +00:00
|
|
|
bus::init();
|
2024-05-26 16:25:59 +00:00
|
|
|
|
2024-08-22 10:48:33 +00:00
|
|
|
// SAFETY: This function is called only once on the BSP.
|
|
|
|
|
unsafe {
|
|
|
|
|
mm::kspace::activate_kernel_page_table();
|
|
|
|
|
}
|
2024-05-26 16:25:59 +00:00
|
|
|
|
2024-08-12 13:01:01 +00:00
|
|
|
arch::irq::enable_local();
|
|
|
|
|
|
2023-11-04 16:14:28 +00:00
|
|
|
invoke_ffi_init_funcs();
|
2022-09-05 06:41:15 +00:00
|
|
|
}
|
2023-02-27 08:18:04 +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);
|
|
|
|
|
|
2024-03-25 05:25:32 +00:00
|
|
|
/// Invoke the initialization functions defined in the FFI.
|
|
|
|
|
/// The component system uses this function to call the initialization functions of
|
|
|
|
|
/// the components.
|
2023-11-04 16:14:28 +00:00
|
|
|
fn invoke_ffi_init_funcs() {
|
2023-02-27 08:18:04 +00:00
|
|
|
extern "C" {
|
2023-07-19 02:08:59 +00:00
|
|
|
fn __sinit_array();
|
|
|
|
|
fn __einit_array();
|
2023-02-27 08:18:04 +00:00
|
|
|
}
|
2023-09-04 03:04:42 +00:00
|
|
|
let call_len = (__einit_array as usize - __sinit_array as usize) / 8;
|
2023-02-27 08:18:04 +00:00
|
|
|
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();
|
2023-02-27 08:18:04 +00:00
|
|
|
(*function)();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-06 06:44:26 +00:00
|
|
|
/// Simple unit tests for the ktest framework.
|
2024-02-21 08:58:40 +00:00
|
|
|
#[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]
|
2024-08-15 07:17:59 +00:00
|
|
|
#[allow(clippy::eq_op)]
|
2023-11-04 08:41:30 +00:00
|
|
|
fn trivial_assertion() {
|
|
|
|
|
assert_eq!(0, 0);
|
|
|
|
|
}
|
2023-11-04 16:14:28 +00:00
|
|
|
|
|
|
|
|
#[ktest]
|
|
|
|
|
#[should_panic]
|
|
|
|
|
fn failing_assertion() {
|
|
|
|
|
assert_eq!(0, 1);
|
|
|
|
|
}
|
2023-11-06 06:44:26 +00:00
|
|
|
|
|
|
|
|
#[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
|
|
|
|
2024-08-13 14:51:27 +00:00
|
|
|
#[doc(hidden)]
|
2024-06-21 05:51:13 +00:00
|
|
|
pub mod ktest {
|
2024-08-13 14:51:27 +00:00
|
|
|
//! 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;
|
2024-07-01 08:00:09 +00:00
|
|
|
pub use ostd_test::*;
|
2024-06-21 05:51:13 +00:00
|
|
|
}
|