| Message ID | 20260228005355.823048-4-xujiakai2025@iscas.ac.cn |
|---|---|
| State | Superseded |
| Headers | show |
| Series | RISC-V: KVM: Validate SBI STA shmem alignment | expand |
On Sat, Feb 28, 2026 at 12:53:55AM +0000, Jiakai Xu wrote: > 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@iscas.ac.cn> > Signed-off-by: Jiakai Xu <jiakaiPeanut@gmail.com> > --- > V8 -> V9: Dropped __riscv guard around INVALID_GPA, which is common to > all architectures. > 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 | 2 ++ > tools/testing/selftests/kvm/steal_time.c | 31 +++++++++++++++++++ > 2 files changed, 33 insertions(+) > Reviewed-by: Andrew Jones <andrew.jones@oss.qualcomm.com>
diff --git a/tools/testing/selftests/kvm/include/kvm_util_types.h b/tools/testing/selftests/kvm/include/kvm_util_types.h index ec787b97cf184..0366e9bce7f93 100644 --- a/tools/testing/selftests/kvm/include/kvm_util_types.h +++ b/tools/testing/selftests/kvm/include/kvm_util_types.h @@ -17,4 +17,6 @@ typedef uint64_t vm_paddr_t; /* Virtual Machine (Guest) physical address */ typedef uint64_t vm_vaddr_t; /* Virtual Machine (Guest) virtual address */ +#define INVALID_GPA (~(uint64_t)0) + #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 75ad067f27260..0708b94ead895 100644 --- a/tools/testing/selftests/kvm/steal_time.c +++ b/tools/testing/selftests/kvm/steal_time.c @@ -332,6 +332,37 @@ static void steal_time_dump(struct kvm_vm *vm, uint32_t vcpu_idx) static void check_steal_time_uapi() { + struct kvm_vm *vm; + struct kvm_vcpu *vcpu; + struct kvm_one_reg reg; + uint64_t shmem; + int ret; + + vm = vm_create_with_one_vcpu(&vcpu, NULL); + + 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, ®); + TEST_ASSERT(ret == -1 && errno == EINVAL, + "misaligned STA shmem returns -EINVAL"); + + shmem = ST_GPA_BASE; + ret = __vcpu_ioctl(vcpu, KVM_SET_ONE_REG, ®); + TEST_ASSERT(ret == 0, + "aligned STA shmem succeeds"); + + shmem = INVALID_GPA; + ret = __vcpu_ioctl(vcpu, KVM_SET_ONE_REG, ®); + TEST_ASSERT(ret == 0, + "all-ones for STA shmem succeeds"); + + kvm_vm_free(vm); } #endif