xref: /linux/tools/testing/selftests/kvm/x86_64/hwcr_msr_test.c (revision 730cfa45b5f4f170095707b526dc7af99c9f0959)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2023, Google LLC.
4  */
5 #include <sys/ioctl.h>
6 
7 #include "test_util.h"
8 #include "kvm_util.h"
9 #include "vmx.h"
10 
11 void test_hwcr_bit(struct kvm_vcpu *vcpu, unsigned int bit)
12 {
13 	const uint64_t ignored = BIT_ULL(3) | BIT_ULL(6) | BIT_ULL(8);
14 	const uint64_t valid = BIT_ULL(18) | BIT_ULL(24);
15 	const uint64_t legal = ignored | valid;
16 	uint64_t val = BIT_ULL(bit);
17 	uint64_t actual;
18 	int r;
19 
20 	r = _vcpu_set_msr(vcpu, MSR_K7_HWCR, val);
21 	TEST_ASSERT(val & ~legal ? !r : r == 1,
22 		    "Expected KVM_SET_MSRS(MSR_K7_HWCR) = 0x%lx to %s",
23 		    val, val & ~legal ? "fail" : "succeed");
24 
25 	actual = vcpu_get_msr(vcpu, MSR_K7_HWCR);
26 	TEST_ASSERT(actual == (val & valid),
27 		    "Bit %u: unexpected HWCR 0x%lx; expected 0x%lx",
28 		    bit, actual, (val & valid));
29 
30 	vcpu_set_msr(vcpu, MSR_K7_HWCR, 0);
31 }
32 
33 int main(int argc, char *argv[])
34 {
35 	struct kvm_vm *vm;
36 	struct kvm_vcpu *vcpu;
37 	unsigned int bit;
38 
39 	vm = vm_create_with_one_vcpu(&vcpu, NULL);
40 
41 	for (bit = 0; bit < BITS_PER_LONG; bit++)
42 		test_hwcr_bit(vcpu, bit);
43 
44 	kvm_vm_free(vm);
45 }
46