Skip to content

Commit

Permalink
serial: Allow for nested logging
Browse files Browse the repository at this point in the history
The current serial implementation will panic if you log! within a log!
statment. For example, the following code panics:

fn foo() {
	log!("foo: {}, bar()");
}

fn bar() -> u64 {
	log!("bar");
	42
}

We change the implementation to have the PORT be static rather than
the entire Serial structure.

Signed-off-by: Joe Richey <[email protected]>
  • Loading branch information
josephlr authored and rbradford committed Mar 3, 2020
1 parent efd9748 commit 86660a7
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/efi/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub extern "win64" fn stdout_output_string(
message: *mut Char16,
) -> Status {
use core::fmt::Write;
let mut serial = crate::serial::SERIAL.borrow_mut();
let mut serial = crate::serial::Serial;
let mut string_end = false;

loop {
Expand Down
21 changes: 6 additions & 15 deletions src/serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,16 @@ use core::fmt;
use atomic_refcell::AtomicRefCell;
use x86_64::instructions::port::PortWriteOnly;

pub static SERIAL: AtomicRefCell<Serial> = AtomicRefCell::new(Serial::new());
// We use COM1 as it is the standard first serial port.
static PORT: AtomicRefCell<PortWriteOnly<u8>> = AtomicRefCell::new(PortWriteOnly::new(0x3f8));

pub struct Serial {
port: PortWriteOnly<u8>,
}

impl Serial {
pub const fn new() -> Self {
// We use COM1 as it is the standard first serial port.
Self {
port: PortWriteOnly::new(0x3f8),
}
}
}
pub struct Serial;

impl fmt::Write for Serial {
fn write_str(&mut self, s: &str) -> fmt::Result {
let mut port = PORT.borrow_mut();
for b in s.bytes() {
unsafe { self.port.write(b) }
unsafe { port.write(b) }
}
Ok(())
}
Expand All @@ -49,7 +40,7 @@ macro_rules! log {
($($arg:tt)*) => {{
use core::fmt::Write;
#[cfg(all(feature = "log-serial", not(test)))]
writeln!(crate::serial::SERIAL.borrow_mut(), $($arg)*).unwrap();
writeln!(crate::serial::Serial, $($arg)*).unwrap();
#[cfg(all(feature = "log-serial", test))]
println!($($arg)*);
}};
Expand Down

0 comments on commit 86660a7

Please sign in to comment.