diff --git a/os/src/lang_items.rs b/os/src/lang_items.rs index c03b9538..655f6c4f 100644 --- a/os/src/lang_items.rs +++ b/os/src/lang_items.rs @@ -1,6 +1,12 @@ use core::panic::PanicInfo; +use crate::sbi::shutdown; #[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().unwrap()); + } else { + println!("Panicked: {}", info.message().unwrap()); + } + shutdown() } diff --git a/os/src/main.rs b/os/src/main.rs index 02c67547..ebdea882 100644 --- a/os/src/main.rs +++ b/os/src/main.rs @@ -2,17 +2,28 @@ #![no_main] #![feature(global_asm)] #![feature(llvm_asm)] +#![feature(panic_info_message)] -mod lang_items; -mod sbi; #[macro_use] mod console; +mod lang_items; +mod sbi; + global_asm!(include_str!("entry.asm")); +fn clear_bss() { + extern "C" { + fn sbss(); + fn ebss(); + } + (sbss as usize..ebss as usize).for_each(|a| { + unsafe { (a as *mut u8).write_volatile(0) } + }); +} + #[no_mangle] pub fn rust_main() -> ! { - println!("Hello, world!"); extern "C" { fn stext(); fn etext(); @@ -25,10 +36,12 @@ pub fn rust_main() -> ! { fn boot_stack(); fn boot_stack_top(); }; + clear_bss(); + println!("Hello, world!"); println!(".text [{:#x}, {:#x})", stext as usize, etext as usize); println!(".rodata [{:#x}, {:#x})", srodata as usize, erodata as usize); println!(".data [{:#x}, {:#x})", sdata as usize, edata as usize); println!("boot_stack [{:#x}, {:#x})", boot_stack as usize, boot_stack_top as usize); println!(".bss [{:#x}, {:#x})", sbss as usize, ebss as usize); - loop {} + panic!("Shutdown machine!"); } \ No newline at end of file