axplat/
console.rs

1//! Console input and output.
2
3use core::fmt::{Arguments, Result, Write};
4
5/// Console input and output interface.
6#[def_plat_interface]
7pub trait ConsoleIf {
8    /// Writes given bytes to the console.
9    fn write_bytes(bytes: &[u8]);
10
11    /// Reads bytes from the console into the given mutable slice.
12    ///
13    /// Returns the number of bytes read.
14    fn read_bytes(bytes: &mut [u8]) -> usize;
15}
16
17struct EarlyConsole;
18
19impl Write for EarlyConsole {
20    fn write_str(&mut self, s: &str) -> Result {
21        write_bytes(s.as_bytes());
22        Ok(())
23    }
24}
25
26/// Lock for console operations to prevent mixed output from concurrent execution
27pub static CONSOLE_LOCK: kspin::SpinNoIrq<()> = kspin::SpinNoIrq::new(());
28
29/// Simple console print operation.
30#[macro_export]
31macro_rules! console_print {
32    ($($arg:tt)*) => {
33        $crate::console::__simple_print(format_args!($($arg)*));
34    }
35}
36
37/// Simple console print operation, with a newline.
38#[macro_export]
39macro_rules! console_println {
40    () => { $crate::ax_print!("\n") };
41    ($($arg:tt)*) => {
42        $crate::console::__simple_print(format_args!("{}\n", format_args!($($arg)*)));
43    }
44}
45
46#[doc(hidden)]
47pub fn __simple_print(fmt: Arguments) {
48    let _guard = CONSOLE_LOCK.lock();
49    EarlyConsole.write_fmt(fmt).unwrap();
50    drop(_guard);
51}