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 <[email protected]>
Signed-off-by: Jiakai Xu <[email protected]>
---
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)
---
 tools/testing/selftests/kvm/steal_time.c | 26 +++++++++++++++++++++++-
 1 file changed, 25 insertions(+), 1 deletion(-)

diff --git a/tools/testing/selftests/kvm/steal_time.c 
b/tools/testing/selftests/kvm/steal_time.c
index a814f6f3f8b41..2af7fd7513d55 100644
--- a/tools/testing/selftests/kvm/steal_time.c
+++ b/tools/testing/selftests/kvm/steal_time.c
@@ -233,6 +233,7 @@ static void check_steal_time_uapi(struct kvm_vcpu *vcpu)
 
 /* SBI STA shmem must have 64-byte alignment */
 #define STEAL_TIME_SIZE                ((sizeof(struct sta_struct) + 63) & ~63)
+#define INVALID_GPA (~(u64)0)
 
 static vm_paddr_t st_gpa[NR_VCPUS];
 
@@ -327,7 +328,30 @@ static void steal_time_dump(struct kvm_vm *vm, uint32_t 
vcpu_idx)
 
 static void check_steal_time_uapi(struct kvm_vcpu *vcpu)
 {
-       /* RISC-V UAPI tests will be added in a subsequent patch */
+       struct kvm_one_reg reg;
+       uint64_t shmem;
+       int ret;
+
+       reg.id = KVM_REG_RISCV_SBI_STA_REG(shmem_lo);
+       reg.addr = (uint64_t)&shmem;
+
+       /* Case 1: misaligned GPA */
+       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");
+
+       /* Case 2: 64-byte aligned GPA */
+       shmem = ST_GPA_BASE;
+       ret = __vcpu_ioctl(vcpu, KVM_SET_ONE_REG, &reg);
+       TEST_ASSERT(ret == 0,
+                   "aligned STA shmem succeeds");
+
+       /* Case 3: INVALID_GPA disables STA */
+       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


Reply via email to