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