From 3681c27870cd5d1ab9c0a06a3ed4f4358670741d Mon Sep 17 00:00:00 2001 From: Tateisi Date: Sat, 2 Aug 2025 19:30:14 +0800 Subject: [PATCH] ch1: Print and shutdown based on sbi --- Cargo.lock | 18 ++++++++++++++++++ Cargo.toml | 1 + src/console.rs | 29 +++++++++++++++++++++++++++++ src/lang_items.rs | 15 +++++++++++++-- src/main.rs | 15 ++++++++++++++- src/sbi.rs | 19 +++++++++++++++++++ 6 files changed, 94 insertions(+), 3 deletions(-) create mode 100644 src/console.rs create mode 100644 src/sbi.rs diff --git a/Cargo.lock b/Cargo.lock index 5159e80..f721321 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5,3 +5,21 @@ version = 3 [[package]] name = "os" version = "0.1.0" +dependencies = [ + "sbi-rt", +] + +[[package]] +name = "sbi-rt" +version = "0.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7fbaa69be1eedc61c426e6d489b2260482e928b465360576900d52d496a58bd0" +dependencies = [ + "sbi-spec", +] + +[[package]] +name = "sbi-spec" +version = "0.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6e36312fb5ddc10d08ecdc65187402baba4ac34585cb9d1b78522ae2358d890" diff --git a/Cargo.toml b/Cargo.toml index aad338f..61fc07a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,3 +4,4 @@ version = "0.1.0" edition = "2021" [dependencies] +sbi-rt = "0.0.3" diff --git a/src/console.rs b/src/console.rs new file mode 100644 index 0000000..00aa650 --- /dev/null +++ b/src/console.rs @@ -0,0 +1,29 @@ +use core::fmt::{self, Write}; +use crate::sbi; + +struct Stdout; + +impl Write for Stdout { + fn write_str(&mut self, s: &str) -> core::fmt::Result { + sbi::console_write(s.as_bytes()); + Ok(()) + } +} + +pub fn print(args: fmt::Arguments) { + Stdout.write_fmt(args).unwrap(); +} + +#[macro_export] +macro_rules! print { + ($fmt: literal $(, $($arg: tt)+)?) => { + $crate::console::print(format_args!($fmt $(, $($arg)+)?)); + }; +} + +#[macro_export] +macro_rules! println { + ($fmt: literal $(, $($arg: tt)+)?) => { + $crate::console::print(format_args!(concat!($fmt, "\n") $(, $($arg)+)?)); + }; +} \ No newline at end of file diff --git a/src/lang_items.rs b/src/lang_items.rs index a5f300c..749fc53 100644 --- a/src/lang_items.rs +++ b/src/lang_items.rs @@ -1,6 +1,17 @@ use core::panic::PanicInfo; +use crate::{sbi, println}; #[panic_handler] -fn panic(_info: &PanicInfo) -> ! { - loop {} +fn panic(info: &PanicInfo) -> ! { + if let Some(location) = info.location() { + println!( + "Panicked at {}:{} {}", + location.file(), + location.line(), + info.message().as_str().unwrap() + ); + } else { + println!("Panicked: {}", info.message().as_str().unwrap()); + } + sbi::shutdown(true) } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index dee9f08..d4dcc6e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,10 @@ #![no_main] mod lang_items; +mod sbi; + +#[macro_use] +mod console; use core::arch::global_asm; global_asm!(include_str!("entry.asm")); @@ -9,7 +13,16 @@ global_asm!(include_str!("entry.asm")); #[no_mangle] pub fn rust_main() -> ! { clear_bss(); - loop {} + + sbi::console_write_byte('O' as u8); + sbi::console_write_byte('K' as u8); + sbi::console_write_byte('\n' as u8); + + sbi::console_write("Hello World.\n".as_bytes()); + + println!("hello world from macro"); + + panic!("Shutdown machine!"); } fn clear_bss() { diff --git a/src/sbi.rs b/src/sbi.rs new file mode 100644 index 0000000..ad5f72f --- /dev/null +++ b/src/sbi.rs @@ -0,0 +1,19 @@ +pub fn console_write_byte(c: u8) { + sbi_rt::console_write_byte(c); +} + +pub fn console_write(bytes: &[u8]) { + let range = bytes.as_ptr_range(); + let bytes = sbi_rt::Physical::new(bytes.len(), range.start as usize, range.end as usize); + sbi_rt::console_write(bytes); +} + +pub fn shutdown(failure: bool) -> ! { + use sbi_rt::{system_reset, NoReason, Shutdown, SystemFailure}; + if !failure { + system_reset(Shutdown, NoReason); + } else { + system_reset(Shutdown, SystemFailure); + } + unreachable!() +} \ No newline at end of file