diff --git a/framework/jinux-frame/src/lib.rs b/framework/jinux-frame/src/lib.rs index 7174b2a07..6b0e60868 100644 --- a/framework/jinux-frame/src/lib.rs +++ b/framework/jinux-frame/src/lib.rs @@ -57,7 +57,8 @@ pub fn init() { #[cfg(feature = "intel_tdx")] println!( "td gpaw: {}, td attributes: {:?}\nTDX guest is initialized", - td_info.gpaw, td_info.attributes + td_info.gpaw, + td_info.attributes ); vm::heap_allocator::init(); boot::init(); diff --git a/services/libs/jinux-std/src/console.rs b/services/libs/jinux-std/src/console.rs new file mode 100644 index 000000000..b64572d16 --- /dev/null +++ b/services/libs/jinux-std/src/console.rs @@ -0,0 +1,40 @@ +//! `print` and `println` macros +//! +//! FIXME: It will print to all `virtio-console` devices, which is not a good choice. +//! + +use core::fmt::{Arguments, Write}; + +struct VirtioConsolesPrinter; + +impl Write for VirtioConsolesPrinter { + fn write_str(&mut self, s: &str) -> core::fmt::Result { + for (_, device) in jinux_console::all_devices() { + device.send(s.as_bytes()); + } + Ok(()) + } +} + +pub fn _print(args: Arguments) { + VirtioConsolesPrinter.write_fmt(args).unwrap(); +} + +/// Copy from Rust std: https://github.com/rust-lang/rust/blob/master/library/std/src/macros.rs +#[macro_export] +macro_rules! print { + ($($arg:tt)*) => {{ + $crate::console::_print(format_args!($($arg)*)); + }}; +} + +/// Copy from Rust std: https://github.com/rust-lang/rust/blob/master/library/std/src/macros.rs +#[macro_export] +macro_rules! println { + () => { + $crate::print!("\n") + }; + ($($arg:tt)*) => {{ + $crate::console::_print(format_args_nl!($($arg)*)); + }}; +} diff --git a/services/libs/jinux-std/src/lib.rs b/services/libs/jinux-std/src/lib.rs index ee4246b0f..44e64e36d 100644 --- a/services/libs/jinux-std/src/lib.rs +++ b/services/libs/jinux-std/src/lib.rs @@ -18,6 +18,7 @@ #![feature(trait_alias)] #![feature(register_tool)] #![feature(trait_upcasting)] +#![feature(format_args_nl)] #![register_tool(component_access_control)] use crate::{ @@ -40,6 +41,7 @@ extern crate controlled; #[macro_use] extern crate ktest; +pub mod console; pub mod device; pub mod driver; pub mod error; diff --git a/services/libs/jinux-std/src/prelude.rs b/services/libs/jinux-std/src/prelude.rs index 8ef99bba1..d14de0724 100644 --- a/services/libs/jinux-std/src/prelude.rs +++ b/services/libs/jinux-std/src/prelude.rs @@ -20,7 +20,6 @@ pub(crate) use int_to_c_enum::TryFromInt; pub(crate) use jinux_frame::config::PAGE_SIZE; pub(crate) use jinux_frame::sync::{Mutex, MutexGuard, RwLock, SpinLock, SpinLockGuard}; pub(crate) use jinux_frame::vm::Vaddr; -pub(crate) use jinux_frame::{print, println}; pub(crate) use log::{debug, error, info, trace, warn}; pub(crate) use pod::Pod; @@ -43,6 +42,7 @@ macro_rules! current_thread { pub(crate) use crate::current; pub(crate) use crate::current_thread; pub(crate) use crate::error::{Errno, Error}; +pub(crate) use crate::{print, println}; pub(crate) use lazy_static::lazy_static; pub(crate) type Result = core::result::Result; pub(crate) use crate::{return_errno, return_errno_with_message};