Log messages to the serial atomically and rename arch::serial

Co-authored-by: Chuandong Li <lichuand@pku.edu.cn>
This commit is contained in:
Zhang Junyang
2024-06-29 11:54:01 +00:00
committed by Tate, Hongliang Tian
parent 6ff6db2167
commit 9e5f3123e1
7 changed files with 29 additions and 16 deletions

View File

@ -18,6 +18,7 @@ impl Write for VirtioConsolesPrinter {
} }
} }
/// Prints the formatted arguments to the standard output.
pub fn _print(args: Arguments) { pub fn _print(args: Arguments) {
VirtioConsolesPrinter.write_fmt(args).unwrap(); VirtioConsolesPrinter.write_fmt(args).unwrap();
} }

View File

@ -2,7 +2,6 @@
#![allow(dead_code)] #![allow(dead_code)]
pub use ostd::arch::console;
use ostd::mm::VmReader; use ostd::mm::VmReader;
use spin::Once; use spin::Once;

View File

@ -3,7 +3,6 @@
//! Platform-specific code for the x86 platform. //! Platform-specific code for the x86 platform.
pub mod boot; pub mod boot;
pub mod console;
pub(crate) mod cpu; pub(crate) mod cpu;
pub mod device; pub mod device;
pub(crate) mod ex_table; pub(crate) mod ex_table;
@ -13,6 +12,7 @@ pub(crate) mod kernel;
pub(crate) mod mm; pub(crate) mod mm;
pub(crate) mod pci; pub(crate) mod pci;
pub mod qemu; pub mod qemu;
pub mod serial;
pub mod task; pub mod task;
#[cfg(feature = "intel_tdx")] #[cfg(feature = "intel_tdx")]
pub(crate) mod tdx_guest; pub(crate) mod tdx_guest;
@ -31,7 +31,7 @@ use log::{info, warn};
pub(crate) fn before_all_init() { pub(crate) fn before_all_init() {
enable_common_cpu_features(); enable_common_cpu_features();
console::init(); serial::init();
} }
pub(crate) fn after_all_init() { pub(crate) fn after_all_init() {
@ -46,7 +46,7 @@ pub(crate) fn after_all_init() {
kernel::pic::enable(); kernel::pic::enable();
} }
} }
console::callback_init(); serial::callback_init();
timer::init(); timer::init();
#[cfg(feature = "intel_tdx")] #[cfg(feature = "intel_tdx")]
if !tdx_is_enabled() { if !tdx_is_enabled() {

View File

@ -15,7 +15,7 @@ use trapframe::TrapFrame;
use super::{device::serial::SerialPort, kernel::IO_APIC}; use super::{device::serial::SerialPort, kernel::IO_APIC};
use crate::{sync::SpinLock, trap::IrqLine}; use crate::{sync::SpinLock, trap::IrqLine};
/// Prints the formatted arguments to the standard output. /// Prints the formatted arguments to the standard output using the serial port.
#[inline] #[inline]
pub fn print(args: fmt::Arguments) { pub fn print(args: fmt::Arguments) {
Stdout.write_fmt(args).unwrap(); Stdout.write_fmt(args).unwrap();

View File

@ -158,7 +158,7 @@ fn run_ktests(test_whitelist: Option<&[&str]>, crate_whitelist: Option<&[&str]>)
use ktest::runner::{run_ktests, KtestResult}; use ktest::runner::{run_ktests, KtestResult};
match run_ktests( match run_ktests(
&crate::console::print, &crate::console::early_print,
fn_catch_unwind, fn_catch_unwind,
test_whitelist.map(|s| s.iter().map(|s| s.to_string())), test_whitelist.map(|s| s.iter().map(|s| s.to_string())),
crate_whitelist, crate_whitelist,

View File

@ -5,23 +5,23 @@
use core::fmt::Arguments; use core::fmt::Arguments;
/// Prints formatted arguments to the console. /// Prints formatted arguments to the console.
pub fn print(args: Arguments) { pub fn early_print(args: Arguments) {
crate::arch::console::print(args); crate::arch::serial::print(args);
} }
/// Prints to the console. /// Prints to the console.
#[macro_export] #[macro_export]
macro_rules! early_print { macro_rules! early_print {
($fmt: literal $(, $($arg: tt)+)?) => { ($fmt: literal $(, $($arg: tt)+)?) => {
$crate::console::print(format_args!($fmt $(, $($arg)+)?)) $crate::console::early_print(format_args!($fmt $(, $($arg)+)?))
} }
} }
/// Prints to the console, with a newline. /// Prints to the console with a newline.
#[macro_export] #[macro_export]
macro_rules! early_println { macro_rules! early_println {
() => { $crate::early_print!("\n") }; () => { $crate::early_print!("\n") };
($fmt: literal $(, $($arg: tt)+)?) => { ($fmt: literal $(, $($arg: tt)+)?) => {
$crate::console::print(format_args!(concat!($fmt, "\n") $(, $($arg)+)?)) $crate::console::early_print(format_args!(concat!($fmt, "\n") $(, $($arg)+)?))
} }
} }

View File

@ -1,6 +1,14 @@
// SPDX-License-Identifier: MPL-2.0 // SPDX-License-Identifier: MPL-2.0
//! Logging support. //! Logging support.
//!
//! Currently the logger prints the logs to the console.
//!
//! This module guarantees _atomicity_ under concurrency: messages are always
//! printed in their entirety without being mixed with messages generated
//! concurrently on other cores.
//!
//! IRQs are disabled while printing. So do not print long log messages.
use alloc::format; use alloc::format;
@ -48,6 +56,11 @@ impl log::Log for Logger {
(timestamp, level, record_str) (timestamp, level, record_str)
}; };
// Use a global lock to prevent interleaving of log messages.
use crate::sync::SpinLock;
static RECORD_LOCK: SpinLock<()> = SpinLock::new(());
let _lock = RECORD_LOCK.lock_irq_disabled();
early_println!("{} {}: {}", timestamp, level, record_str); early_println!("{} {}: {}", timestamp, level, record_str);
} }