KVM: x86: Update HWCR virtualization
v4 cover KVM: x86: Update HWCR virtualization
Allow HWCR.McStatusWrEn to be cleared once set, and allow HWCR.TscFreqSel to be set as well.
v1 -> v2: KVM no longer sets HWCR.TscFreqSel
HWCR.TscFreqSel can be cleared from userspace Selftest modified accordingly
v2 -> v3: kvm_set_msr_common() changes simplified v3 -> v4: kvm_set_msr_common() changes further simplified
HWCR.TscFreqSel can be modified from the guest Targets reordered in selftest Makefile
Jim Mattson (3):
KVM: x86: Allow HWCR.McStatusWrEn to be cleared once set
KVM: x86: Virtualize HWCR.TscFreqSel[bit 24]
KVM: selftests: Test behavior of HWCR
arch/x86/kvm/x86.c | 11 ++-- tools/testing/selftests/kvm/Makefile | 1 + .../selftests/kvm/x86_64/hwcr_msr_test.c | 52 +++++++++++++++++++ 3 files changed, 60 insertions(+), 4 deletions(-) create mode 100644 tools/testing/selftests/kvm/x86_64/hwcr_msr_test.c
Signature · 1 line
2.42.0.582.g8ccd20d70d-goog
PATCH v4 1/3 KVM: x86: Allow HWCR.McStatusWrEn to be cleared once set
When HWCR is set to 0, store 0 in vcpu->arch.msr_hwcr.
--- arch/x86/kvm/x86.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
arch/x86/kvm/x86.c +2 -3
@@ -3701,12 +3701,11 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) data &= ~(u64 )0x8 ;/* ignore TLB cache disable */ /* Handle McStatusWrEn */ -if (data ==BIT_ULL (18 )) { - vcpu->arch.msr_hwcr = data; - }else if (data !=0 ) { +if (data & ~BIT_ULL (18 )) {kvm_pr_unimpl_wrmsr (vcpu, msr, data);return 1 ; } + vcpu->arch.msr_hwcr = data;break ;case MSR_FAM10H_MMIO_CONF_BASE :if (data !=0 ) {
Signature · 1 line
2.42.0.582.g8ccd20d70d-goog
PATCH v4 2/3 KVM: x86: Virtualize HWCR.TscFreqSel[bit 24]
In reply to Jim MattsonOn certain CPUs, Linux guests expect HWCR.TscFreqSel[bit 24] to be set. If it isn't set, they complain:
[Firmware Bug]: TSC doesn't count with P0 frequency!
Allow userspace to set this bit in the virtual HWCR to eliminate the above complaint.
--- arch/x86/kvm/x86.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
arch/x86/kvm/x86.c +6 -2
@@ -3700,8 +3700,12 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) data &= ~(u64 )0x100 ;/* ignore ignne emulation enable */ data &= ~(u64 )0x8 ;/* ignore TLB cache disable */ -/* Handle McStatusWrEn */ -if (data & ~BIT_ULL (18 )) { +/* +* Allow McStatusWrEn and TscFreqSel. (Linux guests from v3.2 +* through at least v6.6 whine if TscFreqSel is clear, +* depending on F/M/S. +*/ +if (data & ~(BIT_ULL (18 ) |BIT_ULL (24 ))) {kvm_pr_unimpl_wrmsr (vcpu, msr, data);return 1 ; }
Signature · 1 line
2.42.0.582.g8ccd20d70d-goog
PATCH v4 3/3 KVM: selftests: Test behavior of HWCR
In reply to Jim MattsonVerify the following:
* 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
--- tools/testing/selftests/kvm/Makefile | 1 + .../selftests/kvm/x86_64/hwcr_msr_test.c | 52 +++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86_64/hwcr_msr_test.c
tools/testing/selftests/kvm/Makefile +1 -0
@@ -119,6 +119,7 @@ TEST_GEN_PROGS_x86_64 += x86_64/amx_test TEST_GEN_PROGS_x86_64 += x86_64/max_vcpuid_cap_test TEST_GEN_PROGS_x86_64 += x86_64/triple_fault_event_test TEST_GEN_PROGS_x86_64 += x86_64/recalc_apic_map_test +TEST_GEN_PROGS_x86_64 += x86_64/hwcr_msr_test TEST_GEN_PROGS_x86_64 += access_tracking_perf_test TEST_GEN_PROGS_x86_64 += demand_paging_test TEST_GEN_PROGS_x86_64 += dirty_log_test
tools/testing/selftests/kvm/x86_64/hwcr_msr_test.c new file +52 -0
52 lines not on this page · raw message
Signature · 1 line
2.42.0.582.g8ccd20d70d-goog
Sean Christopherson
maintainerRe: [PATCH v4 3/3] KVM: selftests: Test behavior of HWCR
tools/testing/selftests/kvm/x86_64/hwcr_msr_test.c line 5
Quoted 11 lines · Jim Mattson
tools/testing/selftests/kvm/x86_64/hwcr_msr_test.c new file +5
-0@@ -0,0 +1,52 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright (C) 2023, Google LLC. +* +* Tests for the K7_HWCR MSR.
My vote is to drop this. I have a hard time believing anyone ever gets value out of these, and they have a bad habit of becoming stale.
tools/testing/selftests/kvm/x86_64/hwcr_msr_test.c line 17
Quoted 12 lines
tools/testing/selftests/kvm/x86_64/hwcr_msr_test.c new file +12
-0+*/ + +#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 unsigned long long ignored =BIT_ULL (3 ) |BIT_ULL (6 ) |BIT_ULL (8 );
uint64_t instead of "unsigned long long"
tools/testing/selftests/kvm/x86_64/hwcr_msr_test.c line 25
Quoted 8 lines
tools/testing/selftests/kvm/x86_64/hwcr_msr_test.c new file +8
-0+const unsigned long long valid =BIT_ULL (18 ) |BIT_ULL (24 ); +const unsigned long long legal = ignored | valid; +uint64_t val =BIT_ULL (bit); +uint64_t check; +int r; + + r =_vcpu_set_msr (vcpu,MSR_K7_HWCR , val); + TEST_ASSERT((r == 1 && (val & legal)) || (r == 0 && !(val & legal)),
Hrm. The "val & legal" is weird. It works, but only because "val" is guaranteed to be a single bit. I much prefer to check for illegal bits, not if there is at least one legal bit. It's kinda ugly, but IMO using a ternary operator is more intuitive than checking the same thing twice.
tools/testing/selftests/kvm/x86_64/hwcr_msr_test.c line 26
Quoted 1 line
tools/testing/selftests/kvm/x86_64/hwcr_msr_test.c new file +1
-0+ "Unexpected result (%d) when setting HWCR[bit %u]", r, bit);
And rather than just say "Unexpected result", explicitly say "fail" or "succeed", I always forget that KVM_SET_MSRS has odd return codes.
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 ? "succeed" : "fail");
tools/testing/selftests/kvm/x86_64/hwcr_msr_test.c line 27
Quoted 1 line
tools/testing/selftests/kvm/x86_64/hwcr_msr_test.c new file +1
-0+ check =vcpu_get_msr (vcpu,MSR_K7_HWCR );
s/check/actual?
tools/testing/selftests/kvm/x86_64/hwcr_msr_test.c line 29
Quoted 2 lines
tools/testing/selftests/kvm/x86_64/hwcr_msr_test.c new file +2
-0+if (val & valid) { + TEST_ASSERT(check == val,
There's no reason to force this code to work with single bits. I don't mind applying the test with single bit testing, but making it play nice with more complex values is trivial, and IMO much easier on the eyes.
TEST_ASSERT(actual == (val & valid), "Bit %u: unexpected HWCR 0x%lx; expected 0x%lx", bit, actual, (val & valid));
tools/testing/selftests/kvm/x86_64/hwcr_msr_test.c line 30
Quoted 1 line
tools/testing/selftests/kvm/x86_64/hwcr_msr_test.c new file +1
-0+ "Bit %u: unexpected HWCR %lx; expected %lx", bit,
0x for the value to avoid decimal vs. hex confusion.
tools/testing/selftests/kvm/x86_64/hwcr_msr_test.c line 38
Quoted 8 lines
tools/testing/selftests/kvm/x86_64/hwcr_msr_test.c new file +8
-0+ check, val); +vcpu_set_msr (vcpu,MSR_K7_HWCR ,0 ); + }else { +TEST_ASSERT (!check, +"Bit %u: unexpected HWCR %lx; expected 0" , bit, + check); + } +}
If you've no objections, I'll apply the changes as below:
--- tools/testing/selftests/kvm/Makefile | 1 + .../selftests/kvm/x86_64/hwcr_msr_test.c | 47 +++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86_64/hwcr_msr_test.c
tools/testing/selftests/kvm/Makefile +1 -0
@@ -119,6 +119,7 @@ TEST_GEN_PROGS_x86_64 += x86_64/amx_test TEST_GEN_PROGS_x86_64 += x86_64/max_vcpuid_cap_test TEST_GEN_PROGS_x86_64 += x86_64/triple_fault_event_test TEST_GEN_PROGS_x86_64 += x86_64/recalc_apic_map_test +TEST_GEN_PROGS_x86_64 += x86_64/hwcr_msr_test TEST_GEN_PROGS_x86_64 += access_tracking_perf_test TEST_GEN_PROGS_x86_64 += demand_paging_test TEST_GEN_PROGS_x86_64 += dirty_log_test
tools/testing/selftests/kvm/x86_64/hwcr_msr_test.c new file +47 -0
47 lines not on this page · raw message
base-commit: 9a90e7aad79e519f7e4afd9d98778192c3b44b6e
Re: [PATCH v4 3/3] KVM: selftests: Test behavior of HWCR
Quoted 1 line · Sean Christopherson
If you've no objections, I'll apply the changes as below:
Go for it.
Quoted 11 lines · Jim Mattson
Allow HWCR.McStatusWrEn to be cleared once set, and allow HWCR.TscFreqSel to be set as well.
v1 -> v2: KVM no longer sets HWCR.TscFreqSel
HWCR.TscFreqSel can be cleared from userspace Selftest modified accordingly
v2 -> v3:kvm_set_msr_common()changes simplified v3 -> v4:kvm_set_msr_common()changes further simplified
HWCR.TscFreqSel can be modified from the guest Targets reordered in selftestMakefile
Quoted 1 line
[...]
Applied to kvm-x86 misc, thanks!
[1/3] KVM: x86: Allow HWCR.McStatusWrEn to be cleared once set
https://github.com/kvm-x86/linux/commit/598a790fc20f [2/3] KVM: x86: Virtualize HWCR.TscFreqSel[bit 24] https://github.com/kvm-x86/linux/commit/8b0e00fba934 [3/3] KVM: selftests: Test behavior of HWCR https://github.com/kvm-x86/linux/commit/591455325a79