diff --git a/Cargo.lock b/Cargo.lock index 1ce498e5..b093b1c8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -295,6 +295,7 @@ dependencies = [ "log", "multiboot", "naked-function", + "one-shot-mutex", "qemu-exit", "riscv", "sbi-rt", @@ -386,6 +387,15 @@ version = "1.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" +[[package]] +name = "one-shot-mutex" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ab252a69210b0deb4dc8d8695c257d718b058c69a7c3ac6d83cf78310eb6ece" +dependencies = [ + "lock_api", +] + [[package]] name = "paste" version = "1.0.14" diff --git a/Cargo.toml b/Cargo.toml index 243bfcc3..9248ab38 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,7 @@ edition = "2021" align-address = "0.1" hermit-entry = { version = "0.9", features = ["loader"] } log = "0.4" +one-shot-mutex = "0.1" sptr = "0.3" vm-fdt = { version = "0.3", default-features = false, features = ["alloc"] } diff --git a/src/console.rs b/src/console.rs index 8e66e7c6..61dd3e32 100644 --- a/src/console.rs +++ b/src/console.rs @@ -1,5 +1,7 @@ use core::fmt; +use one_shot_mutex::OneShotMutex; + pub struct Console(()); impl fmt::Write for Console { @@ -11,4 +13,4 @@ impl fmt::Write for Console { } } -pub static mut CONSOLE: Console = Console(()); +pub static CONSOLE: OneShotMutex = OneShotMutex::new(Console(())); diff --git a/src/main.rs b/src/main.rs index 7cdec0ae..e464b188 100644 --- a/src/main.rs +++ b/src/main.rs @@ -31,7 +31,5 @@ extern crate alloc; fn _print(args: core::fmt::Arguments<'_>) { use core::fmt::Write; - unsafe { - console::CONSOLE.write_fmt(args).unwrap(); - } + console::CONSOLE.lock().write_fmt(args).unwrap(); } diff --git a/src/none.rs b/src/none.rs index f7051cd4..405e45ac 100644 --- a/src/none.rs +++ b/src/none.rs @@ -44,7 +44,7 @@ pub(crate) unsafe extern "C" fn loader_main() -> ! { #[panic_handler] fn panic(info: &core::panic::PanicInfo<'_>) -> ! { // We can't use `println!` or related macros, because `_print` unwraps a result and might panic again - writeln!(unsafe { &mut console::CONSOLE }, "[LOADER] {info}").ok(); + writeln!(console::CONSOLE.lock(), "[LOADER] {info}").ok(); loop {} }