Skip to main content

strat9_kernel/ostd/
mod.rs

1//! OSTD-like (OS Trusted Domain) abstraction layer for Strat9-OS
2//!
3//! This module provides a minimal, auditable unsafe code base that abstracts
4//! hardware operations behind safe interfaces. Inspired by Asterinas OSTD.
5//!
6//! # Design concept
7//!
8//! - **Minimal TCB**: only essential unsafe code lives here
9//! - **Safe Abstractions**: all public APIs are safe wrappers
10//! - **Hardware Traits**: platform-independent interfaces
11//! - **Confined Unsafe**: all `unsafe` blocks are justified with SAFETY comments
12
13#![allow(unsafe_code)]
14#![allow(unsafe_op_in_unsafe_fn)]
15
16extern crate alloc;
17
18pub mod boot;
19pub mod cpu;
20pub mod mm;
21pub mod task;
22pub mod util;
23
24pub use cpu::CpuId;
25pub use mm::{PhysAddr, VirtAddr};
26
27/// Early print macro for bootstrap debugging (before full logger is ready)
28#[macro_export]
29macro_rules! early_println {
30    ($($arg:tt)*) => {{
31        use core::fmt::Write;
32        let _ = write!($crate::ostd::early_print::EarlyWriter, $($arg)*);
33        let _ = writeln!($crate::ostd::early_print::EarlyWriter);
34    }};
35}
36
37/// Early print macro without newline
38#[macro_export]
39macro_rules! early_print {
40    ($($arg:tt)*) => {{
41        use core::fmt::Write;
42        let _ = write!($crate::ostd::early_print::EarlyWriter, $($arg)*);
43    }};
44}
45
46pub mod early_print {
47    //! Early debugging output via serial port
48    //!
49    //! Available before the full logger is initialized.
50
51    use core::fmt::{Result, Write};
52
53    pub struct EarlyWriter;
54
55    impl Write for EarlyWriter {
56        /// Writes str.
57        fn write_str(&mut self, s: &str) -> Result {
58            // SAFETY: serial port is initialized early in boot and is a shared resource.
59            // We accept potential race conditions during early boot for debug output.
60            crate::arch::x86_64::serial::_print(format_args!("{}", s));
61            Ok(())
62        }
63    }
64}