From ef7139d3645ca47fbbc36b26d9b9ae9ff2a8698a Mon Sep 17 00:00:00 2001 From: Yifan Wu Date: Mon, 3 Jan 2022 19:34:51 -0800 Subject: [PATCH] Update applications --- os/src/trap/mod.rs | 4 ++-- user/Cargo.toml | 1 + user/src/bin/00hello_world.rs | 5 ----- user/src/bin/03priv_inst.rs | 17 +++++++++++++++++ user/src/bin/04priv_csr.rs | 17 +++++++++++++++++ 5 files changed, 37 insertions(+), 7 deletions(-) create mode 100644 user/src/bin/03priv_inst.rs create mode 100644 user/src/bin/04priv_csr.rs diff --git a/os/src/trap/mod.rs b/os/src/trap/mod.rs index 98ae35cf..5c2ea91e 100644 --- a/os/src/trap/mod.rs +++ b/os/src/trap/mod.rs @@ -34,11 +34,11 @@ pub fn trap_handler(cx: &mut TrapContext) -> &mut TrapContext { } Trap::Exception(Exception::StoreFault) | Trap::Exception(Exception::StorePageFault) => { - println!("[kernel] PageFault in application, core dumped."); + println!("[kernel] PageFault in application, killed by kernel."); run_next_app(); } Trap::Exception(Exception::IllegalInstruction) => { - println!("[kernel] IllegalInstruction in application, core dumped."); + println!("[kernel] IllegalInstruction in application, killed by kernel."); run_next_app(); } _ => { diff --git a/user/Cargo.toml b/user/Cargo.toml index 817ca525..b7518dde 100644 --- a/user/Cargo.toml +++ b/user/Cargo.toml @@ -7,3 +7,4 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +riscv = { git = "https://github.com/rcore-os/riscv", features = ["inline-asm"] } diff --git a/user/src/bin/00hello_world.rs b/user/src/bin/00hello_world.rs index f4f0332e..cefa39ba 100644 --- a/user/src/bin/00hello_world.rs +++ b/user/src/bin/00hello_world.rs @@ -4,13 +4,8 @@ #[macro_use] extern crate user_lib; -use core::arch::asm; - #[no_mangle] fn main() -> i32 { println!("Hello, world!"); - unsafe { - asm!("sret"); - } 0 } diff --git a/user/src/bin/03priv_inst.rs b/user/src/bin/03priv_inst.rs new file mode 100644 index 00000000..04dac37e --- /dev/null +++ b/user/src/bin/03priv_inst.rs @@ -0,0 +1,17 @@ +#![no_std] +#![no_main] + +#[macro_use] +extern crate user_lib; + +use core::arch::asm; + +#[no_mangle] +fn main() -> i32 { + println!("Try to execute privileged instruction in U Mode"); + println!("Kernel should kill this application!"); + unsafe { + asm!("sret"); + } + 0 +} diff --git a/user/src/bin/04priv_csr.rs b/user/src/bin/04priv_csr.rs new file mode 100644 index 00000000..fbd678f7 --- /dev/null +++ b/user/src/bin/04priv_csr.rs @@ -0,0 +1,17 @@ +#![no_std] +#![no_main] + +#[macro_use] +extern crate user_lib; + +use riscv::register::sstatus::{self, SPP}; + +#[no_mangle] +fn main() -> i32 { + println!("Try to access privileged CSR in U Mode"); + println!("Kernel should kill this application!"); + unsafe { + sstatus::set_spp(SPP::User); + } + 0 +}