From d99d4f754f10ab7f3053515a68ea6ce0390b61d2 Mon Sep 17 00:00:00 2001 From: Yu Chen Date: Fri, 11 Mar 2022 19:11:40 +0800 Subject: [PATCH] update ch4:add some exception support in trap_handler function, add two *_fault.rs apps --- os/src/trap/mod.rs | 3 ++- user/src/bin/04load_fault.rs | 18 ++++++++++++++++++ user/src/bin/05store_fault.rs | 18 ++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 user/src/bin/04load_fault.rs create mode 100644 user/src/bin/05store_fault.rs diff --git a/os/src/trap/mod.rs b/os/src/trap/mod.rs index 67577210..022f3adb 100644 --- a/os/src/trap/mod.rs +++ b/os/src/trap/mod.rs @@ -48,7 +48,8 @@ pub fn trap_handler() -> ! { cx.sepc += 4; cx.x[10] = syscall(cx.x[17], [cx.x[10], cx.x[11], cx.x[12]]) as usize; } - Trap::Exception(Exception::StoreFault) | Trap::Exception(Exception::StorePageFault) => { + Trap::Exception(Exception::StoreFault) | Trap::Exception(Exception::StorePageFault) | + Trap::Exception(Exception::LoadFault) | Trap::Exception(Exception::LoadPageFault) => { println!("[kernel] PageFault in application, bad addr = {:#x}, bad instruction = {:#x}, kernel killed it.", stval, cx.sepc); exit_current_and_run_next(); } diff --git a/user/src/bin/04load_fault.rs b/user/src/bin/04load_fault.rs new file mode 100644 index 00000000..87b2fad2 --- /dev/null +++ b/user/src/bin/04load_fault.rs @@ -0,0 +1,18 @@ +#![no_std] +#![no_main] + +#[macro_use] +extern crate user_lib; + +use core::ptr::{read_volatile,null_mut}; + +#[no_mangle] +fn main() -> i32 { + println!("\nload_fault APP running...\n"); + println!("Into Test load_fault, we will insert an invalid load operation..."); + println!("Kernel should kill this application!"); + unsafe { + let _i=read_volatile(null_mut::()); + } + 0 +} \ No newline at end of file diff --git a/user/src/bin/05store_fault.rs b/user/src/bin/05store_fault.rs new file mode 100644 index 00000000..d94f337c --- /dev/null +++ b/user/src/bin/05store_fault.rs @@ -0,0 +1,18 @@ +#![no_std] +#![no_main] + +#[macro_use] +extern crate user_lib; + +use core::ptr::null_mut; + +#[no_mangle] +fn main() -> i32 { + println!("\nstore_fault APP running...\n"); + println!("Into Test store_fault, we will insert an invalid store operation..."); + println!("Kernel should kill this application!"); + unsafe { + null_mut::().write_volatile(1); + } + 0 +} \ No newline at end of file