Skip to content

Commit

Permalink
Adding more test payloads (debug, mret, mcause, mepc, mconfigpts, mts…
Browse files Browse the repository at this point in the history
…elect)

Now we got all the pieces needed to add a new bunch of tests, those are now
enabled by default (including the breakpoint test!).

Co-authored-by: Abel Vexina Wilkinson <abel.vexinawilkinson@epfl.ch>
  • Loading branch information
CharlyCst and fesqvw committed Mar 22, 2024
1 parent 3550ece commit 36dec22
Show file tree
Hide file tree
Showing 13 changed files with 369 additions and 0 deletions.
35 changes: 35 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ members = [
"payloads/perf_monitor",
"payloads/menv_msec",
"payloads/breakpoint",
"payloads/mepc",
"payloads/mcause",
"payloads/mconfigptr",
"payloads/tselect",
"payloads/mret",

# Crates
"crates/mirage_abi",
Expand Down
6 changes: 6 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ test:
cargo run --package runner -- --max-exits 200 --payload pmp
cargo run --package runner -- --max-exits 200 --payload perf_monitor
cargo run --package runner -- --max-exits 200 --payload menv_msec
cargo run --package runner -- --max-exits 200 --payload breakpoint
cargo run --package runner -- --max-exits 200 --payload mepc
cargo run --package runner -- --max-exits 200 --payload mcause
cargo run --package runner -- --max-exits 200 --payload mconfigptr
cargo run --package runner -- --max-exits 200 --payload tselect
cargo run --package runner -- --max-exits 200 --payload mret

# Checking formatting...
cargo fmt --all -- --check
Expand Down
7 changes: 7 additions & 0 deletions payloads/mcause/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "mcause"
version = "0.1.0"
edition = "2021"

[dependencies]
mirage_abi = { path = "../../crates/mirage_abi" }
72 changes: 72 additions & 0 deletions payloads/mcause/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#![no_std]
#![no_main]

use core::arch::{asm, global_asm};
use core::panic::PanicInfo;
use core::usize;

use mirage_abi::{failure, success};

global_asm!(
r#"
.text
.align 4
.global _start
_start:
j {entry}
"#,
entry = sym entry,
);

extern "C" fn entry() -> ! {
let secret1: usize = 0x42;
let mut res: usize;
unsafe {
asm!(
"li {0}, 0x42",
"csrw mcause, {0}",
"csrr {1}, mcause",
in(reg) secret1,
out(reg) res,
);
}

read_test(res, 0);

let secret2: usize = 0x9;

unsafe {
asm!(
"li {0}, 0x9",
"csrw mcause, {0}",
"csrr {1}, mcause",
in(reg) secret2,
out(reg) res,
);
}

read_test(res, secret2);

unsafe {
asm!(
"li {0}, 0x8000000000000009",
"csrw mcause, {0}",
"csrr {1}, mcause",
in(reg) secret2,
out(reg) res,
);
}

read_test(res, secret2);

success();
}

fn read_test(out_csr: usize, expected: usize) {
assert_eq!(out_csr, expected);
}

#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
failure();
}
7 changes: 7 additions & 0 deletions payloads/mconfigptr/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "mconfigptr"
version = "0.1.0"
edition = "2021"

[dependencies]
mirage_abi = { path = "../../crates/mirage_abi" }
46 changes: 46 additions & 0 deletions payloads/mconfigptr/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#![no_std]
#![no_main]

use core::arch::{asm, global_asm};
use core::panic::PanicInfo;
use core::usize;

use mirage_abi::{failure, success};

global_asm!(
r#"
.text
.align 4
.global _start
_start:
j {entry}
"#,
entry = sym entry,
);

extern "C" fn entry() -> ! {
let secret1: usize = 0x42;
let mut res: usize;
unsafe {
asm!(
"li {0}, 0x42",
"csrw mcause, {0}",
"csrr {1}, mcause",
in(reg) secret1,
out(reg) res,
);
}

read_test(res, 0);

success();
}

fn read_test(out_csr: usize, expected: usize) {
assert_eq!(out_csr, expected);
}

#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
failure();
}
7 changes: 7 additions & 0 deletions payloads/mepc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "mepc"
version = "0.1.0"
edition = "2021"

[dependencies]
mirage_abi = { path = "../../crates/mirage_abi" }
44 changes: 44 additions & 0 deletions payloads/mepc/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#![no_std]
#![no_main]

use core::arch::{asm, global_asm};
use core::panic::PanicInfo;
use core::usize;

use mirage_abi::{failure, success};

global_asm!(
r#"
.text
.align 4
.global _start
_start:
j {entry}
"#,
entry = sym entry,
);

extern "C" fn entry() -> ! {
let secret: usize = 0x42;
let res: usize;
unsafe {
asm!(
"li {0}, 0x42",
"csrw mepc, {0}",
"csrr {1}, mepc",
in(reg) secret,
out(reg) res,
);
}

if res == secret {
success();
}

panic!();
}

#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
failure();
}
7 changes: 7 additions & 0 deletions payloads/mret/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "mret"
version = "0.1.0"
edition = "2021"

[dependencies]
mirage_abi = { path = "../../crates/mirage_abi" }
80 changes: 80 additions & 0 deletions payloads/mret/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#![no_std]
#![no_main]

use core::arch::{asm, global_asm};
use core::panic::PanicInfo;

use mirage_abi::{failure, success};

global_asm!(
r#"
.text
.align 4
.global _start
_start:
j {entry}
"#,
entry = sym entry,
);

extern "C" fn entry() -> ! {
let mut mstatus: usize;
let mut t6: usize;
unsafe {
let handler = _raw_breakpoint_trap_handler as usize;
// Let's rise an exception breakpoint directly
asm!(
"csrw mtvec, {0}", // Write mtvec
"ebreak", // Cause an exception, we should return right away!
"csrr {1}, mstatus", // Read mstatus
in(reg) handler,
out(reg) mstatus,
out("t6") t6, // The handler writes a secret value in t6
);
}

// MPP = 0
read_test((mstatus >> 11) & 0b11, 0);
// MPIE = 1
read_test((mstatus >> 7) & 0b1, 1);
// MPRV = 0
read_test((mstatus >> 17) & 0b1, 0);

assert_eq!(
t6, 0x42,
"Trap handler did not properly update the value in t6"
);

success();
}

// —————————————————————————————— Trap Handler —————————————————————————————— //

global_asm!(
r#"
.text
.align 4
.global _raw_breakpoint_trap_handler
_raw_breakpoint_trap_handler:
csrr t6, mepc // Read EPC
addi t6, t6, 4 // Increment return pointer
csrw mepc, t6 // Write it back
li t6, 0x42 // And store a secret value in t6 before returning
mret
"#,
);

extern "C" {
fn _raw_breakpoint_trap_handler();
}

fn read_test(out_csr: usize, expected: usize) {
assert_eq!(out_csr, expected);
}

// ————————————————————————————— Panic Handler —————————————————————————————— //

#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
failure();
}
7 changes: 7 additions & 0 deletions payloads/tselect/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "tselect"
version = "0.1.0"
edition = "2021"

[dependencies]
mirage_abi = { path = "../../crates/mirage_abi" }
Loading

0 comments on commit 36dec22

Please sign in to comment.