From 88bb53a8004dfe7acb1f9edc9edf2216d60e7919 Mon Sep 17 00:00:00 2001 From: BillXiang Date: Fri, 26 Sep 2025 17:37:40 +0800 Subject: [PATCH 1/3] riscv64: Handle KVM_EXIT_RISCV_SBI exit Return riscv_sbi to VMM for further process. Signed-off-by: BillXiang --- kvm-ioctls/src/ioctls/vcpu.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/kvm-ioctls/src/ioctls/vcpu.rs b/kvm-ioctls/src/ioctls/vcpu.rs index c3043ffc..b6039201 100644 --- a/kvm-ioctls/src/ioctls/vcpu.rs +++ b/kvm-ioctls/src/ioctls/vcpu.rs @@ -184,6 +184,8 @@ pub enum VcpuExit<'a> { /// size size: u64, }, + /// Corresponds to KVM_EXIT_RISCV_SBI + RiscvSbi(&'a mut kvm_run__bindgen_ty_1__bindgen_ty_24), /// Corresponds to an exit reason that is unknown from the current version /// of the kvm-ioctls crate. Let the consumer decide about what to do with /// it. @@ -1641,6 +1643,13 @@ impl VcpuFd { Ok(VcpuExit::IoapicEoi(eoi.vector)) } KVM_EXIT_HYPERV => Ok(VcpuExit::Hyperv), + #[cfg(target_arch = "riscv64")] + KVM_EXIT_RISCV_SBI => { + // SAFETY: Safe because the exit_reason (which comes from the kernel) told us + // which union field to use. + let riscv_sbi = unsafe { &mut run.__bindgen_anon_1.riscv_sbi }; + Ok(VcpuExit::RiscvSbi(riscv_sbi)) + } r => Ok(VcpuExit::Unsupported(r)), } } else { From 71e7b5c2a3bfd75547c17c1987fd2852be62ebd5 Mon Sep 17 00:00:00 2001 From: BillXiang Date: Fri, 26 Sep 2025 17:46:12 +0800 Subject: [PATCH 2/3] 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 --- kvm-ioctls/src/ioctls/vcpu.rs | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/kvm-ioctls/src/ioctls/vcpu.rs b/kvm-ioctls/src/ioctls/vcpu.rs index b6039201..b83ca997 100644 --- a/kvm-ioctls/src/ioctls/vcpu.rs +++ b/kvm-ioctls/src/ioctls/vcpu.rs @@ -2583,6 +2583,25 @@ mod tests { 0x03, 0xa5, 0x0c, 0x00, // lw a0, 0(s9); test MMIO read 0x93, 0x05, 0x70, 0x60, // li a1, 0x0607; 0x23, 0xa0, 0xbc, 0x00, // sw a1, 0(s9); test MMIO write + //sbi_console_getchar + 0x01, 0x45, // li a0, 0 + 0x81, 0x45, // li a1, 0 + 0x01, 0x46, // li a2, 0 + 0x81, 0x46, // li a3, 0 + 0x01, 0x47, // li a4, 0 + 0x81, 0x47, // li a5, 0 + 0x01, 0x48, // li a6, 0 + 0x89, 0x48, // li a7, 2 + 0x73, 0x00, 0x00, 0x00, //ecall + //sbi_console_putchar + 0x81, 0x45, // li a1, 0 + 0x01, 0x46, // li a2, 0 + 0x81, 0x46, // li a3, 0 + 0x01, 0x47, // li a4, 0 + 0x81, 0x47, // li a5, 0 + 0x01, 0x48, // li a6, 0 + 0x85, 0x48, // li a7, 1 + 0x73, 0x00, 0x00, 0x00, //ecall 0x6f, 0x00, 0x00, 0x00, // j .; shouldn't get here, but if so loop forever ]; @@ -2654,7 +2673,20 @@ mod tests { .map(|page| page.count_ones()) .sum(); assert_eq!(dirty_pages, 1); - break; + } + VcpuExit::RiscvSbi(riscv_sbi) => { + match riscv_sbi.extension_id as usize { + 2 /* SBI_EXT_0_1_CONSOLE_GETCHAR */ => { + let ch = &mut riscv_sbi.ret[..1]; + ch[0] = 0x2a; + } + 1 /* SBI_EXT_0_1_CONSOLE_PUTCHAR */ => { + let ch = riscv_sbi.args[0]; + assert_eq!(ch, 0x2a); + break; + } + _ => panic!("unexpected extension_id: {:?}", riscv_sbi.extension_id), + } } r => panic!("unexpected exit reason: {:?}", r), } From b88d32b5776e94fbd85fefb5349c7b81c74a5a1e Mon Sep 17 00:00:00 2001 From: BillXiang Date: Fri, 26 Sep 2025 18:25:34 +0800 Subject: [PATCH 3/3] docs: update CHANGELOG.md Signed-off-by: BillXiang --- kvm-ioctls/CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/kvm-ioctls/CHANGELOG.md b/kvm-ioctls/CHANGELOG.md index fd17f5f2..f3987bce 100644 --- a/kvm-ioctls/CHANGELOG.md +++ b/kvm-ioctls/CHANGELOG.md @@ -3,6 +3,8 @@ ## Upcoming Release - Plumb through KVM_CAP_DIRTY_LOG_RING as DirtyLogRing cap. +- [[#352]](https://github.com/rust-vmm/kvm/pull/352) Return riscv_sbi + to VMM for further process. ## v0.24.0