]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
RISC-V: KVM: selftests: Add RISC-V SBI STA shmem alignment tests
authorJiakai Xu <xujiakai2025@iscas.ac.cn>
Tue, 3 Mar 2026 01:08:59 +0000 (01:08 +0000)
committerAnup Patel <anup@brainfault.org>
Thu, 26 Mar 2026 15:51:03 +0000 (21:21 +0530)
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>
Reviewed-by: Andrew Jones <andrew.jones@oss.qualcomm.com>
Link: https://lore.kernel.org/r/20260303010859.1763177-4-xujiakai2025@iscas.ac.cn
Signed-off-by: Anup Patel <anup@brainfault.org>
tools/testing/selftests/kvm/include/kvm_util_types.h
tools/testing/selftests/kvm/steal_time.c

index ec787b97cf184e10ca81664ab7fdfe68731ea513..0366e9bce7f93664b8709836b5d2055b8c07f7bb 100644 (file)
@@ -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 */
index 8e4d7c13b5986b4e15cf96a9b5b275e09dc33625..efe56a10d13e83e74118cca2558dc33556653161 100644 (file)
@@ -332,6 +332,37 @@ static void steal_time_dump(struct kvm_vm *vm, uint32_t vcpu_idx)
 
 static void check_steal_time_uapi(void)
 {
+       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, &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");
+
+       kvm_vm_free(vm);
 }
 
 #elif defined(__loongarch__)