ostd/console/
mod.rs

1// SPDX-License-Identifier: MPL-2.0
2
3//! Console output.
4
5use core::fmt::{Arguments, Write};
6
7use crate::arch::serial::SERIAL_PORT;
8
9pub mod uart_ns16650a;
10
11/// Prints formatted arguments to the console.
12pub fn early_print(args: Arguments) {
13    let Some(serial) = SERIAL_PORT.get() else {
14        return;
15    };
16
17    #[cfg(target_arch = "x86_64")]
18    crate::arch::if_tdx_enabled!({
19        // Hold the lock to prevent the logs from interleaving.
20        let _guard = serial.lock();
21        tdx_guest::print(args);
22    } else {
23        serial.lock().write_fmt(args).unwrap();
24    });
25    #[cfg(not(target_arch = "x86_64"))]
26    serial.lock().write_fmt(args).unwrap();
27}
28
29/// Prints to the console.
30#[macro_export]
31macro_rules! early_print {
32    ($fmt: literal $(, $($arg: tt)+)?) => {
33        $crate::console::early_print(format_args!($fmt $(, $($arg)+)?))
34    }
35}
36
37/// Prints to the console with a newline.
38#[macro_export]
39macro_rules! early_println {
40    () => { $crate::early_print!("\n") };
41    ($fmt: literal $(, $($arg: tt)+)?) => {
42        $crate::console::early_print(format_args!(concat!($fmt, "\n") $(, $($arg)+)?))
43    }
44}