Chapter1: Update panic_handler.

This commit is contained in:
Yifan Wu 2020-11-13 12:06:39 +08:00
parent 49050aca3f
commit c701cd00ce
2 changed files with 25 additions and 6 deletions

View File

@ -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()
}

View File

@ -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!");
}