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