Extend set_sregs_test to cover CR8. Verify that the architecturally
allowed bits [3:0] can be set and read back, that every reserved bit
[63:4] is rejected by KVM_SET_SREGS, and that a successful set is
observable via KVM_GET_SREGS.

Without the accompanying fix to kvm_is_valid_sregs(), setting a reserved
CR8 bit is silently accepted and the read-back value diverges from the
value written, so this test fails on an unpatched kernel and passes once
the fix is applied.

Signed-off-by: Tharit Tangkijwanichakul <[email protected]>
---
 tools/testing/selftests/kvm/x86/set_sregs_test.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/tools/testing/selftests/kvm/x86/set_sregs_test.c 
b/tools/testing/selftests/kvm/x86/set_sregs_test.c
index 603226ffe437..4e7c35179438 100644
--- a/tools/testing/selftests/kvm/x86/set_sregs_test.c
+++ b/tools/testing/selftests/kvm/x86/set_sregs_test.c
@@ -135,6 +135,18 @@ static void test_cr_bits(struct kvm_vcpu *vcpu, u64 cr4)
        /* NW without CD is illegal, as is PG without PE. */
        TEST_INVALID_SREG_BIT(vcpu, cr0, sregs, X86_CR0_NW);
        TEST_INVALID_SREG_BIT(vcpu, cr0, sregs, X86_CR0_PG);
+
+       /* CR8 bits 3:0 are writable; bits 63:4 are reserved. */
+       vcpu_sregs_get(vcpu, &sregs);
+       sregs.cr8 = 0xf;
+       rc = _vcpu_sregs_set(vcpu, &sregs);
+       TEST_ASSERT(!rc, "Failed to set valid CR8 value 0xf");
+
+       vcpu_sregs_get(vcpu, &sregs);
+       TEST_ASSERT_EQ(sregs.cr8, 0xf);
+
+       for (i = 4; i < 64; i++)
+               TEST_INVALID_SREG_BIT(vcpu, cr8, sregs, BIT_ULL(i));
 }
 
 static void test_efer_bits(struct kvm_vcpu *vcpu, u64 efer)
-- 
2.53.0


Reply via email to