Move the log lock to a better location

This commit is contained in:
Ruihan Li
2025-04-15 22:22:07 +08:00
committed by Tate, Hongliang Tian
parent 67e5e5a651
commit d6e40933b8
6 changed files with 46 additions and 73 deletions

View File

@ -2,26 +2,6 @@
//! The console I/O.
use alloc::fmt;
use core::fmt::Write;
/// Prints the formatted arguments to the standard output using the serial port.
#[inline]
pub fn print(args: fmt::Arguments) {
Stdout.write_fmt(args).unwrap();
}
struct Stdout;
impl Write for Stdout {
fn write_str(&mut self, s: &str) -> fmt::Result {
for &c in s.as_bytes() {
send(c);
}
Ok(())
}
}
/// Initializes the serial port.
pub(crate) fn init() {}

View File

@ -2,29 +2,9 @@
//! The console I/O.
use alloc::fmt;
use core::fmt::Write;
use super::device::serial::SerialPort;
use crate::io::reserve_io_port_range;
/// Prints the formatted arguments to the standard output using the serial port.
#[inline]
pub fn print(args: fmt::Arguments) {
Stdout.write_fmt(args).unwrap();
}
struct Stdout;
impl Write for Stdout {
fn write_str(&mut self, s: &str) -> fmt::Result {
for &c in s.as_bytes() {
send(c);
}
Ok(())
}
}
bitflags::bitflags! {
struct LineSts: u8 {
const INPUT_FULL = 1;

View File

@ -2,16 +2,34 @@
//! Console output.
use core::fmt::Arguments;
use core::fmt::{self, Arguments, Write};
use crate::if_tdx_enabled;
use crate::{
if_tdx_enabled,
sync::{LocalIrqDisabled, SpinLock},
};
struct Stdout;
impl Write for Stdout {
fn write_str(&mut self, s: &str) -> fmt::Result {
for &c in s.as_bytes() {
crate::arch::serial::send(c);
}
Ok(())
}
}
static STDOUT: SpinLock<Stdout, LocalIrqDisabled> = SpinLock::new(Stdout);
/// Prints formatted arguments to the console.
pub fn early_print(args: Arguments) {
if_tdx_enabled!({
// Hold the lock to prevent the logs from interleaving.
let _guard = STDOUT.lock();
tdx_guest::print(args);
} else {
crate::arch::serial::print(args);
STDOUT.lock().write_fmt(args).unwrap();
});
}

View File

@ -56,14 +56,7 @@ impl log::Log for Logger {
};
// Default implementation.
let level = record.level();
// Use a global lock to prevent interleaving of log messages.
use crate::sync::SpinLock;
static RECORD_LOCK: SpinLock<()> = SpinLock::new(());
let _lock = RECORD_LOCK.disable_irq().lock();
crate::console::early_print(format_args!("{}: {}\n", level, record.args()));
}