Skip to content

Commit 0adf872

Browse files
author
BillXiang
committed
riscv64: Add test for KVM_EXIT_RISCV_SBI exit
Introduce sbi-spec and add tests for LEGACY_CONSOLE_GETCHAR and LEGACY_CONSOLE_PUTCHAR. Signed-off-by: BillXiang <[email protected]>
1 parent 676ad9c commit 0adf872

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

kvm-ioctls/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ libc = "0.2.39"
1515
kvm-bindings = { path = "../kvm-bindings", version = "0.14.0", features = ["fam-wrappers"] }
1616
vmm-sys-util = { workspace = true }
1717
bitflags = "2.4.1"
18+
sbi-spec = { version = "0.0.8", features = ["legacy",]}
1819

1920
[dev-dependencies]
2021
byteorder = "1.2.1"

kvm-ioctls/src/ioctls/vcpu.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2572,6 +2572,7 @@ mod tests {
25722572
#[cfg(target_arch = "riscv64")]
25732573
#[test]
25742574
fn test_run_code() {
2575+
use sbi_spec::legacy::*;
25752576
use std::io::Write;
25762577

25772578
let kvm = Kvm::new().unwrap();
@@ -2583,6 +2584,25 @@ mod tests {
25832584
0x03, 0xa5, 0x0c, 0x00, // lw a0, 0(s9); test MMIO read
25842585
0x93, 0x05, 0x70, 0x60, // li a1, 0x0607;
25852586
0x23, 0xa0, 0xbc, 0x00, // sw a1, 0(s9); test MMIO write
2587+
//sbi_console_getchar
2588+
0x01, 0x45, // li a0, 0
2589+
0x81, 0x45, // li a1, 0
2590+
0x01, 0x46, // li a2, 0
2591+
0x81, 0x46, // li a3, 0
2592+
0x01, 0x47, // li a4, 0
2593+
0x81, 0x47, // li a5, 0
2594+
0x01, 0x48, // li a6, 0
2595+
0x89, 0x48, // li a7, 2
2596+
0x73, 0x00, 0x00, 0x00, //ecall
2597+
//sbi_console_putchar
2598+
0x81, 0x45, // li a1, 0
2599+
0x01, 0x46, // li a2, 0
2600+
0x81, 0x46, // li a3, 0
2601+
0x01, 0x47, // li a4, 0
2602+
0x81, 0x47, // li a5, 0
2603+
0x01, 0x48, // li a6, 0
2604+
0x85, 0x48, // li a7, 1
2605+
0x73, 0x00, 0x00, 0x00, //ecall
25862606
0x6f, 0x00, 0x00, 0x00, // j .; shouldn't get here, but if so loop forever
25872607
];
25882608

@@ -2654,7 +2674,24 @@ mod tests {
26542674
.map(|page| page.count_ones())
26552675
.sum();
26562676
assert_eq!(dirty_pages, 1);
2677+
}
2678+
VcpuExit::RiscvSbi(riscv_sbi) => {
2679+
match riscv_sbi.extension_id as usize {
2680+
LEGACY_CONSOLE_GETCHAR => {
2681+
// SAFETY: Safe because the extension_id (which comes from the kernel) told us
2682+
// ow to use riscv_sbi
2683+
let ch = &mut riscv_sbi.ret[..1];
2684+
ch[0] = 0x2a;
2685+
}
2686+
LEGACY_CONSOLE_PUTCHAR => {
2687+
// SAFETY: Safe because the extension_id (which comes from the kernel) told us
2688+
// how to use riscv_sbi
2689+
let ch = riscv_sbi.args[0];
2690+
assert_eq!(ch, 0x2a);
26572691
break;
2692+
}
2693+
_ => panic!("unexpected extension_id: {:?}", riscv_sbi.extension_id),
2694+
}
26582695
}
26592696
r => panic!("unexpected exit reason: {:?}", r),
26602697
}

0 commit comments

Comments
 (0)