diff --git a/docs/src/integrate/dxe_core.md b/docs/src/integrate/dxe_core.md index 49e6be5fa8..2355175e13 100644 --- a/docs/src/integrate/dxe_core.md +++ b/docs/src/integrate/dxe_core.md @@ -367,7 +367,7 @@ In this example, we will talk about how to configure the loggers provided by Pat ```rust # extern crate patina; # extern crate log; -use patina::peripheral::serial::uart::UartNull; // UartPl011 (AARCH64) and Uart16550 (X64) exist +use patina::peripheral::serial::uart::UartNull; // UartPl011 (AARCH64), Uart16550 (X64) and UartQemuDebug (X64) exist use patina::debug::log::{Format, SerialLogger}; use log::LevelFilter; diff --git a/sdk/patina/src/debug/log/serial_logger.rs b/sdk/patina/src/debug/log/serial_logger.rs index 77c57d07dc..93952283d9 100644 --- a/sdk/patina/src/debug/log/serial_logger.rs +++ b/sdk/patina/src/debug/log/serial_logger.rs @@ -6,7 +6,7 @@ //! //! SPDX-License-Identifier: Apache-2.0 //! -use crate::peripheral::serial::{SerialIO, shared::SharedSerial}; +use crate::peripheral::serial::{shared::SharedSerial, SerialIO}; use core::marker::Send; use spin::Mutex; @@ -57,6 +57,11 @@ where } } + /// Initialize serial port. + pub fn init(&self) { + let _ = self.serial_port.init(); + } + /// Switches the logger to blocking (spinning) serial acquisition, consuming and returning `self`. /// /// In blocking mode, each record is written under a logger-level lock so concurrent cores @@ -150,6 +155,7 @@ mod tests { ] { let (mock, buffer) = recording_serial(); let logger = Logger::new(format, &[], LevelFilter::Trace, mock); + logger.init(); let args = format_args!("hello"); logger.log(&log::Record::builder().args(args).level(level).target("test").build()); assert_eq!(String::from_utf8(buffer.lock().clone()).expect("valid utf8"), expected); @@ -159,12 +165,14 @@ mod tests { #[test] fn test_serial_logger_filters() { let logger = Logger::new(Format::Standard, &[], LevelFilter::Warn, MockSerialIO::new()); + logger.init(); assert!(logger.enabled(&metadata(Level::Error, "any"))); assert!(logger.enabled(&metadata(Level::Warn, "any"))); assert!(!logger.enabled(&metadata(Level::Info, "any"))); let filters = &[("noisy::module", LevelFilter::Off), ("app", LevelFilter::Debug)]; let logger = Logger::new(Format::Standard, filters, LevelFilter::Info, MockSerialIO::new()); + logger.init(); assert!(!logger.enabled(&metadata(Level::Error, "noisy::module::inner"))); assert!(logger.enabled(&metadata(Level::Debug, "app::sub"))); assert!(!logger.enabled(&metadata(Level::Trace, "app::sub"))); @@ -177,6 +185,7 @@ mod tests { let mut mock = MockSerialIO::new(); mock.expect_write().never(); let logger = Logger::new(Format::Standard, &[], LevelFilter::Warn, mock); + logger.init(); let args = format_args!("should be filtered out"); logger.log(&log::Record::builder().args(args).level(Level::Info).target("test").build()); @@ -187,6 +196,7 @@ mod tests { fn test_serial_logger_blocking_writes_intact_record() { let (mock, buffer) = recording_serial(); let logger = Logger::new(Format::Standard, &[], LevelFilter::Trace, mock).with_blocking(); + logger.init(); assert!(logger.blocking); let args = format_args!("hello"); @@ -206,6 +216,7 @@ mod tests { let (mock, buffer) = recording_serial(); let logger = Logger::new(Format::Standard, &[], LevelFilter::Trace, mock).with_blocking(); + logger.init(); thread::scope(|scope| { for message in ["aaaa", "bbbb"] { diff --git a/sdk/patina/src/peripheral/serial/uart.rs b/sdk/patina/src/peripheral/serial/uart.rs index 65d887c0cd..6975bc529b 100644 --- a/sdk/patina/src/peripheral/serial/uart.rs +++ b/sdk/patina/src/peripheral/serial/uart.rs @@ -15,5 +15,10 @@ mod uart_16550; #[cfg(target_arch = "x86_64")] pub use uart_16550::Uart16550; +#[cfg(target_arch = "x86_64")] +mod qemudebug; +#[cfg(target_arch = "x86_64")] +pub use qemudebug::UartQemuDebug; + mod pl011; pub use pl011::UartPl011; diff --git a/sdk/patina/src/peripheral/serial/uart/qemudebug.rs b/sdk/patina/src/peripheral/serial/uart/qemudebug.rs new file mode 100644 index 0000000000..f79c74801b --- /dev/null +++ b/sdk/patina/src/peripheral/serial/uart/qemudebug.rs @@ -0,0 +1,86 @@ +//! [`UartQemuDebug`] - [`SerialIO`](crate::peripheral::serial::SerialIO) implementation. +//! +//! ## License +//! +//! SPDX-License-Identifier: Apache-2.0 +//! + +use core::arch::asm; + +/// interface for the qemu debug console +/// +/// The `isa-debugcon` device logs any data written to the port +/// specified via `iobase` property. On reads it returns 0xe9. The +/// latter allows presense detection, it is not possible to actually +/// read input data, this is a output-only device. +/// +/// By convention `iobase=0x402` is used for firmware logging. +/// +/// typical usage: +/// ``` +/// qemu-system-x86_64 \ +/// -chardev file,path=/path/to/firmware.log,id=fw \ +/// -device isa-debugcon,iobase=0x402,chardev=fw \ +/// $moreargs +/// ``` +#[derive(Debug)] +pub struct UartQemuDebug { + io_base: u16, + present: bool, +} + +impl UartQemuDebug { + // read value indicating the device is present + const PROBE: u8 = 0xe9; + + /// create `UartQemuDebug` with io base specified + /// + /// # Safety + /// + /// The caller must ensure `io_base` points to a valid isa-debugcon io port. + pub const unsafe fn new(io_base: u16) -> Self { + Self { io_base, present: false } + } + + fn outb(&self, value: u8) { + // SAFETY: write output byte to isa-debugcon device + unsafe { + asm!("outb dx, al", in("al") value, in("dx") self.io_base, + options(nomem,nostack,preserves_flags) + ) + } + } + + fn inb(&self) -> u8 { + // SAFETY: read device detection byte from isa-debugcon device + unsafe { + let ret: u8; + asm!("inb al, dx", in("dx") self.io_base, out("al") ret); + ret + } + } +} + +impl crate::peripheral::serial::SerialIO for UartQemuDebug { + fn init(&mut self) { + let value = self.inb(); + self.present = value == Self::PROBE; + } + + fn write(&mut self, buffer: &[u8]) { + if self.present { + for c in buffer { + self.outb(*c); + } + } + } + + fn read(&mut self) -> u8 { + // PANIC: Would loop forever, better to panic. + panic!(); + } + + fn try_read(&mut self) -> Option { + None + } +}