From 693f27ea2ec14ea9102b7d257b2b25c9fe73a733 Mon Sep 17 00:00:00 2001 From: Gerd Hoffmann Date: Tue, 17 Nov 2026 03:07:20 -0500 Subject: [PATCH 1/8] SerialLogger: add init function Call the init function of the serial port used for logging. Signed-off-by: Gerd Hoffmann --- sdk/patina/src/debug/log/serial_logger.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/sdk/patina/src/debug/log/serial_logger.rs b/sdk/patina/src/debug/log/serial_logger.rs index 77c57d07dc..1db301e7e6 100644 --- a/sdk/patina/src/debug/log/serial_logger.rs +++ b/sdk/patina/src/debug/log/serial_logger.rs @@ -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 From b64bf72869ed887d9c83ebf60ed36894878e0588 Mon Sep 17 00:00:00 2001 From: Gerd Hoffmann Date: Sat, 14 Nov 2026 13:33:02 -0500 Subject: [PATCH 2/8] patina/serial: add qemu debug console driver Add driver for the qemu debug console (-device isa-debugcon). The iobase passed to UartQemuDebug::new() must match the iobase set for the isa-debugcon device. By convention 0x402 is used for firmware logging. Calling init() is mandatory, this will detect whenever the device is present or not. Logging happens only in case the device has been detected. Signed-off-by: Gerd Hoffmann --- sdk/patina/src/peripheral/serial/uart.rs | 5 ++ .../src/peripheral/serial/uart/qemudebug.rs | 62 +++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 sdk/patina/src/peripheral/serial/uart/qemudebug.rs 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..7d032c3dbc --- /dev/null +++ b/sdk/patina/src/peripheral/serial/uart/qemudebug.rs @@ -0,0 +1,62 @@ +//! QemuDebug [`SerialIO`](crate::peripheral::serial::SerialIO) implementation. +//! +//! ## License +//! +//! SPDX-License-Identifier: Apache-2.0 +//! + +use core::arch::asm; + +/// qemu debug console +#[derive(Debug)] +pub struct UartQemuDebug { + iobase: u16, + present: bool, +} + +impl UartQemuDebug { + // read value indicating the device is present + const PROBE: u8 = 0xe9; + + pub const fn new(iobase: u16) -> Self { + Self { iobase, present: false } + } + + fn outb(&self, value: u8) { + // SAFETY: write output byte to isa-debugcon device + unsafe { asm!("outb %al, %dx", in("al") value, in("dx") self.iobase, options(att_syntax)) } + } + + fn inb(&self) -> u8 { + // SAFETY: read device detection byte from isa-debugcon device + unsafe { + let ret: u8; + asm!("inb %dx, %al", in("dx") self.iobase, out("al") ret, options(att_syntax)); + 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 + } +} From 377e4cf4598d5f8892beff1fb00f458af9f75f58 Mon Sep 17 00:00:00 2001 From: Gerd Hoffmann Date: Fri, 20 Nov 2026 15:46:49 +0100 Subject: [PATCH 3/8] fixup! patina/serial: add qemu debug console driver --- sdk/patina/src/peripheral/serial/uart/qemudebug.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/sdk/patina/src/peripheral/serial/uart/qemudebug.rs b/sdk/patina/src/peripheral/serial/uart/qemudebug.rs index 7d032c3dbc..f4180476b7 100644 --- a/sdk/patina/src/peripheral/serial/uart/qemudebug.rs +++ b/sdk/patina/src/peripheral/serial/uart/qemudebug.rs @@ -1,4 +1,4 @@ -//! QemuDebug [`SerialIO`](crate::peripheral::serial::SerialIO) implementation. +//! [`UartQemuDebug`] - [`SerialIO`](crate::peripheral::serial::SerialIO) implementation. //! //! ## License //! @@ -10,7 +10,7 @@ use core::arch::asm; /// qemu debug console #[derive(Debug)] pub struct UartQemuDebug { - iobase: u16, + io_base: u16, present: bool, } @@ -18,20 +18,20 @@ impl UartQemuDebug { // read value indicating the device is present const PROBE: u8 = 0xe9; - pub const fn new(iobase: u16) -> Self { - Self { iobase, present: false } + pub const 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 %al, %dx", in("al") value, in("dx") self.iobase, options(att_syntax)) } + unsafe { asm!("outb %al, %dx", in("al") value, in("dx") self.io_base, options(att_syntax)) } } fn inb(&self) -> u8 { // SAFETY: read device detection byte from isa-debugcon device unsafe { let ret: u8; - asm!("inb %dx, %al", in("dx") self.iobase, out("al") ret, options(att_syntax)); + asm!("inb %dx, %al", in("dx") self.io_base, out("al") ret, options(att_syntax)); ret } } From 4dd2507f7756ca748f7929865b28beb9ecaff8bb Mon Sep 17 00:00:00 2001 From: Gerd Hoffmann Date: Fri, 20 Nov 2026 16:31:57 +0100 Subject: [PATCH 4/8] fixup! patina/serial: add qemu debug console driver --- sdk/patina/src/peripheral/serial/uart/qemudebug.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/sdk/patina/src/peripheral/serial/uart/qemudebug.rs b/sdk/patina/src/peripheral/serial/uart/qemudebug.rs index f4180476b7..c3f50b37bc 100644 --- a/sdk/patina/src/peripheral/serial/uart/qemudebug.rs +++ b/sdk/patina/src/peripheral/serial/uart/qemudebug.rs @@ -18,6 +18,7 @@ impl UartQemuDebug { // read value indicating the device is present const PROBE: u8 = 0xe9; + // create `UartQemuDebug` with io base specified pub const fn new(io_base: u16) -> Self { Self { io_base, present: false } } From 2420378445f432b80aa7aca6c5ee8832fc27c968 Mon Sep 17 00:00:00 2001 From: Gerd Hoffmann Date: Fri, 20 Nov 2026 16:34:59 +0100 Subject: [PATCH 5/8] fixup! SerialLogger: add init function --- sdk/patina/src/debug/log/serial_logger.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/sdk/patina/src/debug/log/serial_logger.rs b/sdk/patina/src/debug/log/serial_logger.rs index 1db301e7e6..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; @@ -155,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); @@ -164,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"))); @@ -182,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()); @@ -192,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"); @@ -211,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"] { From f4976be60f074f23044695b03fc23525499f1f64 Mon Sep 17 00:00:00 2001 From: Gerd Hoffmann Date: Fri, 20 Nov 2026 16:52:46 +0100 Subject: [PATCH 6/8] fixup! patina/serial: add qemu debug console driver --- .../src/peripheral/serial/uart/qemudebug.rs | 25 ++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/sdk/patina/src/peripheral/serial/uart/qemudebug.rs b/sdk/patina/src/peripheral/serial/uart/qemudebug.rs index c3f50b37bc..04e4cfd6d7 100644 --- a/sdk/patina/src/peripheral/serial/uart/qemudebug.rs +++ b/sdk/patina/src/peripheral/serial/uart/qemudebug.rs @@ -7,7 +7,22 @@ use core::arch::asm; -/// qemu debug console +/// 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, @@ -18,8 +33,12 @@ impl UartQemuDebug { // read value indicating the device is present const PROBE: u8 = 0xe9; - // create `UartQemuDebug` with io base specified - pub const fn new(io_base: u16) -> Self { + /// 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 } } From cf8d9a91dbf39d82d35e13ee844ab2ef3b1bd727 Mon Sep 17 00:00:00 2001 From: Gerd Hoffmann Date: Fri, 20 Nov 2026 17:01:23 +0100 Subject: [PATCH 7/8] fixup! patina/serial: add qemu debug console driver --- sdk/patina/src/peripheral/serial/uart/qemudebug.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/sdk/patina/src/peripheral/serial/uart/qemudebug.rs b/sdk/patina/src/peripheral/serial/uart/qemudebug.rs index 04e4cfd6d7..f79c74801b 100644 --- a/sdk/patina/src/peripheral/serial/uart/qemudebug.rs +++ b/sdk/patina/src/peripheral/serial/uart/qemudebug.rs @@ -44,14 +44,18 @@ impl UartQemuDebug { fn outb(&self, value: u8) { // SAFETY: write output byte to isa-debugcon device - unsafe { asm!("outb %al, %dx", in("al") value, in("dx") self.io_base, options(att_syntax)) } + 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 %dx, %al", in("dx") self.io_base, out("al") ret, options(att_syntax)); + asm!("inb al, dx", in("dx") self.io_base, out("al") ret); ret } } From 43eba1d248354c131534cefc9aa34ccec935d85f Mon Sep 17 00:00:00 2001 From: Gerd Hoffmann Date: Fri, 20 Nov 2026 17:04:10 +0100 Subject: [PATCH 8/8] add UartQemuDebug to integration docs Signed-off-by: Gerd Hoffmann --- docs/src/integrate/dxe_core.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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;