Commit 59145532 authored by Jim Mattson's avatar Jim Mattson Committed by Sean Christopherson
Browse files

KVM: selftests: Test behavior of HWCR, a.k.a. MSR_K7_HWCR



Verify the following behavior holds true for writes and reads of HWCR from
host userspace:

* Attempts to set bits 3, 6, or 8 are ignored
* Bits 18 and 24 are the only bits that can be set
* Any bit that can be set can also be cleared

Signed-off-by: default avatarJim Mattson <jmattson@google.com>
Link: https://lore.kernel.org/r/20230929230246.1954854-4-jmattson@google.com


Co-developed-by: default avatarSean Christopherson <seanjc@google.com>
Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
parent 8b0e00fb
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -66,6 +66,7 @@ TEST_GEN_PROGS_x86_64 += x86_64/dirty_log_page_splitting_test
TEST_GEN_PROGS_x86_64 += x86_64/get_msr_index_features
TEST_GEN_PROGS_x86_64 += x86_64/exit_on_emulation_failure_test
TEST_GEN_PROGS_x86_64 += x86_64/fix_hypercall_test
TEST_GEN_PROGS_x86_64 += x86_64/hwcr_msr_test
TEST_GEN_PROGS_x86_64 += x86_64/hyperv_clock
TEST_GEN_PROGS_x86_64 += x86_64/hyperv_cpuid
TEST_GEN_PROGS_x86_64 += x86_64/hyperv_evmcs
+47 −0
Original line number Diff line number Diff line
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (C) 2023, Google LLC.
 */

#define _GNU_SOURCE /* for program_invocation_short_name */
#include <sys/ioctl.h>

#include "test_util.h"
#include "kvm_util.h"
#include "vmx.h"

void test_hwcr_bit(struct kvm_vcpu *vcpu, unsigned int bit)
{
	const uint64_t ignored = BIT_ULL(3) | BIT_ULL(6) | BIT_ULL(8);
	const uint64_t valid = BIT_ULL(18) | BIT_ULL(24);
	const uint64_t legal = ignored | valid;
	uint64_t val = BIT_ULL(bit);
	uint64_t actual;
	int r;

	r = _vcpu_set_msr(vcpu, MSR_K7_HWCR, val);
	TEST_ASSERT(val & ~legal ? !r : r == 1,
		    "Expected KVM_SET_MSRS(MSR_K7_HWCR) = 0x%lx to %s",
		    val, val & ~legal ? "fail" : "succeed");

	actual = vcpu_get_msr(vcpu, MSR_K7_HWCR);
	TEST_ASSERT(actual == (val & valid),
		    "Bit %u: unexpected HWCR 0x%lx; expected 0x%lx",
		    bit, actual, (val & valid));

	vcpu_set_msr(vcpu, MSR_K7_HWCR, 0);
}

int main(int argc, char *argv[])
{
	struct kvm_vm *vm;
	struct kvm_vcpu *vcpu;
	unsigned int bit;

	vm = vm_create_with_one_vcpu(&vcpu, NULL);

	for (bit = 0; bit < BITS_PER_LONG; bit++)
		test_hwcr_bit(vcpu, bit);

	kvm_vm_free(vm);
}