[PATCH v8 3/3] RISC-V: KVM: selftests: Add RISC-V SBI STA shmem alignment tests

From: Jiakai Xu

Date: Thu Feb 26 2026 - 03:34:38 EST


Add RISC-V KVM selftests to verify the SBI Steal-Time Accounting (STA)
shared memory alignment requirements.

The SBI specification requires the STA shared memory GPA to be 64-byte
aligned, or set to all-ones to explicitly disable steal-time accounting.
This test verifies that KVM enforces the expected behavior when
configuring the SBI STA shared memory via KVM_SET_ONE_REG.

Specifically, the test checks that:
- misaligned GPAs are rejected with -EINVAL
- 64-byte aligned GPAs are accepted
- all-ones GPA is accepted

Signed-off-by: Jiakai Xu <xujiakai2025@xxxxxxxxxxx>
Signed-off-by: Jiakai Xu <jiakaiPeanut@xxxxxxxxx>
---
V7 -> V8: Moved INVALID_GPA definition to kvm_util_types.h.
Removed comments in RISC-V check_steal_time_uapi().
Corrected reg.id assignment for SBI STA.
V6 -> V7: Removed RISCV_SBI_STA_REG() macro addition and used existing
KVM_REG_RISCV_SBI_STA_REG(shmem_lo) instead.
Refined assertion messages per review feedback.
Split into two patches per Andrew Jones' suggestion:
Refactored UAPI tests from steal_time_init() into dedicated
check_steal_time_uapi() function and added empty stub for
RISC-V.
Filled in RISC-V stub with STA alignment tests. (this patch)
---
.../selftests/kvm/include/kvm_util_types.h | 4 +++
tools/testing/selftests/kvm/steal_time.c | 25 +++++++++++++++++++
2 files changed, 29 insertions(+)

diff --git a/tools/testing/selftests/kvm/include/kvm_util_types.h b/tools/testing/selftests/kvm/include/kvm_util_types.h
index ec787b97cf184..90567f8243fe9 100644
--- a/tools/testing/selftests/kvm/include/kvm_util_types.h
+++ b/tools/testing/selftests/kvm/include/kvm_util_types.h
@@ -17,4 +17,8 @@
typedef uint64_t vm_paddr_t; /* Virtual Machine (Guest) physical address */
typedef uint64_t vm_vaddr_t; /* Virtual Machine (Guest) virtual address */

+#if defined(__riscv)
+#define INVALID_GPA (~(uint64_t)0)
+#endif
+
#endif /* SELFTEST_KVM_UTIL_TYPES_H */
diff --git a/tools/testing/selftests/kvm/steal_time.c b/tools/testing/selftests/kvm/steal_time.c
index 6f77df4deaad3..e90aad9561ff7 100644
--- a/tools/testing/selftests/kvm/steal_time.c
+++ b/tools/testing/selftests/kvm/steal_time.c
@@ -324,6 +324,31 @@ static void steal_time_dump(struct kvm_vm *vm, uint32_t vcpu_idx)

static void check_steal_time_uapi(struct kvm_vcpu *vcpu)
{
+ struct kvm_one_reg reg;
+ uint64_t shmem;
+ int ret;
+
+ reg.id = KVM_REG_RISCV |
+ KVM_REG_SIZE_ULONG |
+ KVM_REG_RISCV_SBI_STATE |
+ KVM_REG_RISCV_SBI_STA |
+ KVM_REG_RISCV_SBI_STA_REG(shmem_lo);
+ reg.addr = (uint64_t)&shmem;
+
+ shmem = ST_GPA_BASE + 1;
+ ret = __vcpu_ioctl(vcpu, KVM_SET_ONE_REG, &reg);
+ TEST_ASSERT(ret == -1 && errno == EINVAL,
+ "misaligned STA shmem returns -EINVAL");
+
+ shmem = ST_GPA_BASE;
+ ret = __vcpu_ioctl(vcpu, KVM_SET_ONE_REG, &reg);
+ TEST_ASSERT(ret == 0,
+ "aligned STA shmem succeeds");
+
+ shmem = INVALID_GPA;
+ ret = __vcpu_ioctl(vcpu, KVM_SET_ONE_REG, &reg);
+ TEST_ASSERT(ret == 0,
+ "all-ones for STA shmem succeeds");
}

#endif
--
2.34.1