garage44.eu/jeroen/pinnacle

logclone: git clone https://garage44.eu/jeroen/pinnacle.git

.. /kernel/src/console.rs

raw

//! Text out over the board's serial port, a line at a time: the lock keeps
//! lines from different cores whole. The last [`KEPT`] bytes of it are kept
//! in memory too, for the terminal's `log` (the LOG call).

use crate::sync::SpinLock;
use core::fmt;

/// How much of what was written the kernel keeps.
pub const KEPT: usize = 64 * 1024;

pub static CONSOLE: SpinLock<Console> = SpinLock::new(Console {
    kept: [0; KEPT],
    at: 0,
    full: false,
});

pub struct Console {
    /// What was written last, round: the next byte goes at `at`.
    kept: [u8; KEPT],
    at: usize,
    /// Gone round at least once.
    full: bool,
}

impl Console {
    pub fn write_bytes(&mut self, bytes: &[u8]) {
        // And on the screen, while the boot log is drawn there.
        crate::drivers::screen_log::write(bytes);
        for &byte in bytes {
            if byte == b'\n' {
                crate::board::uart::put(b'\r');
            }
            crate::board::uart::put(byte);
            self.kept[self.at] = byte;
            self.at += 1;
            if self.at == KEPT {
                self.at = 0;
                self.full = true;
            }
        }
    }

    /// What is kept, oldest first, from the first whole line on once it
    /// has gone round, added to `out`: which has room for [`KEPT`] bytes
    /// already, as nothing may allocate with the console held (an
    /// allocator that prints would wait on itself).
    pub fn kept(&self, out: &mut alloc::vec::Vec<u8>) {
        if self.full {
            let older = &self.kept[self.at..];
            let start = older
                .iter()
                .position(|&b| b == b'\n')
                .map_or(older.len(), |i| i + 1);
            out.extend_from_slice(&older[start..]);
        }
        out.extend_from_slice(&self.kept[..self.at]);
    }
}

/// The serial port alone, without the lock or the kept copy: for a panic,
/// which may come from inside the lock.
pub struct Raw;

impl fmt::Write for Raw {
    fn write_str(&mut self, s: &str) -> fmt::Result {
        // On the screen too once `screen_log::panicking` has turned it on:
        // it only tries its lock, so it cannot wait on itself.
        crate::drivers::screen_log::write(s.as_bytes());
        for &byte in s.as_bytes() {
            if byte == b'\n' {
                crate::board::uart::put(b'\r');
            }
            crate::board::uart::put(byte);
        }
        Ok(())
    }
}

impl fmt::Write for Console {
    fn write_str(&mut self, s: &str) -> fmt::Result {
        self.write_bytes(s.as_bytes());
        Ok(())
    }
}

#[macro_export]
macro_rules! print {
    ($($arg:tt)*) => {{
        use core::fmt::Write as _;
        let _ = write!($crate::console::CONSOLE.lock(), $($arg)*);
    }};
}

#[macro_export]
macro_rules! println {
    () => { $crate::print!("\n") };
    ($($arg:tt)*) => {{
        use core::fmt::Write as _;
        let mut console = $crate::console::CONSOLE.lock();
        let _ = write!(console, $($arg)*);
        let _ = console.write_str("\n");
    }};
}