From 771e7b12ff140edf235f23ad28304a67a54dd66a Mon Sep 17 00:00:00 2001 From: zhangxinyu <840317537@qq.com> Date: Fri, 19 May 2023 14:07:26 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=94=A8=E6=88=B7=E5=BA=94?= =?UTF-8?q?=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ch2/user/src/bin/01hello_world.rs | 10 ---------- ch2/user/src/bin/01store_fault.rs | 15 +++++++++++++++ ch2/user/src/bin/02power.rs | 27 +++++++++++++++++++++++++++ ch2/user/src/bin/03priv_inst.rs | 17 +++++++++++++++++ ch2/user/src/bin/04priv_csr.rs | 17 +++++++++++++++++ 5 files changed, 76 insertions(+), 10 deletions(-) delete mode 100644 ch2/user/src/bin/01hello_world.rs create mode 100644 ch2/user/src/bin/01store_fault.rs create mode 100644 ch2/user/src/bin/02power.rs create mode 100644 ch2/user/src/bin/03priv_inst.rs create mode 100644 ch2/user/src/bin/04priv_csr.rs diff --git a/ch2/user/src/bin/01hello_world.rs b/ch2/user/src/bin/01hello_world.rs deleted file mode 100644 index 1e0f9bc..0000000 --- a/ch2/user/src/bin/01hello_world.rs +++ /dev/null @@ -1,10 +0,0 @@ -#![no_std] -#![no_main] - -use user_lib::*; - -#[no_mangle] -fn main() -> i32 { - println!("hello 2"); - 0 -} \ No newline at end of file diff --git a/ch2/user/src/bin/01store_fault.rs b/ch2/user/src/bin/01store_fault.rs new file mode 100644 index 0000000..f8023eb --- /dev/null +++ b/ch2/user/src/bin/01store_fault.rs @@ -0,0 +1,15 @@ +#![no_std] +#![no_main] + +#[macro_use] +extern crate user_lib; + +#[no_mangle] +fn main() -> i32 { + println!("Into Test store_fault, we will insert an invalid store operation..."); + println!("Kernel should kill this application!"); + unsafe { + core::ptr::null_mut::().write_volatile(0); + } + 0 +} diff --git a/ch2/user/src/bin/02power.rs b/ch2/user/src/bin/02power.rs new file mode 100644 index 0000000..f628f34 --- /dev/null +++ b/ch2/user/src/bin/02power.rs @@ -0,0 +1,27 @@ +#![no_std] +#![no_main] + +#[macro_use] +extern crate user_lib; + +const SIZE: usize = 10; +const P: u32 = 3; +const STEP: usize = 100000; +const MOD: u32 = 10007; + +#[no_mangle] +fn main() -> i32 { + let mut pow = [0u32; SIZE]; + let mut index: usize = 0; + pow[index] = 1; + for i in 1..=STEP { + let last = pow[index]; + index = (index + 1) % SIZE; + pow[index] = last * P % MOD; + if i % 10000 == 0 { + println!("{}^{}={}(MOD {})", P, i, pow[index], MOD); + } + } + println!("Test power OK!"); + 0 +} diff --git a/ch2/user/src/bin/03priv_inst.rs b/ch2/user/src/bin/03priv_inst.rs new file mode 100644 index 0000000..04dac37 --- /dev/null +++ b/ch2/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/ch2/user/src/bin/04priv_csr.rs b/ch2/user/src/bin/04priv_csr.rs new file mode 100644 index 0000000..fbd678f --- /dev/null +++ b/ch2/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 +}