Skip to content

Commit 5914553

Browse files
jsmattsonjrsean-jc
authored andcommitted
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: Jim Mattson <[email protected]> Link: https://lore.kernel.org/r/[email protected] Co-developed-by: Sean Christopherson <[email protected]> Signed-off-by: Sean Christopherson <[email protected]>
1 parent 8b0e00f commit 5914553

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

tools/testing/selftests/kvm/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ TEST_GEN_PROGS_x86_64 += x86_64/dirty_log_page_splitting_test
6666
TEST_GEN_PROGS_x86_64 += x86_64/get_msr_index_features
6767
TEST_GEN_PROGS_x86_64 += x86_64/exit_on_emulation_failure_test
6868
TEST_GEN_PROGS_x86_64 += x86_64/fix_hypercall_test
69+
TEST_GEN_PROGS_x86_64 += x86_64/hwcr_msr_test
6970
TEST_GEN_PROGS_x86_64 += x86_64/hyperv_clock
7071
TEST_GEN_PROGS_x86_64 += x86_64/hyperv_cpuid
7172
TEST_GEN_PROGS_x86_64 += x86_64/hyperv_evmcs
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
13+
void 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+
35+
int 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+
}

0 commit comments

Comments
 (0)