Skip to contentSkip to conversation

KVM: x86: Update HWCR virtualization

PATCH Superseded Jim Mattson 1 participant 4 messages Sep 25, 2023

Subsystems (4)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover KVM: x86: Update HWCR virtualization

Allow HWCR.McStatusWrEn to be cleared once set, and allow HWCR.TscFreqSel to be set from userspace.

v1 -> v2: KVM no longer sets HWCR.TscFreqSel
HWCR.TscFreqSel can be cleared from userspace Selftest modified accordingly

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                            | 23 ++++++--
 tools/testing/selftests/kvm/Makefile          |  1 +
 .../selftests/kvm/x86_64/hwcr_msr_test.c      | 52 +++++++++++++++++++
 3 files changed, 71 insertions(+), 5 deletions(-)
 create mode 100644 tools/testing/selftests/kvm/x86_64/hwcr_msr_test.c
Signature · 1 line
2.42.0.515.g380fc7ccd1-goog
#1 lore raw

PATCH v2 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.

Fixes 191c8137a939 ("x86/kvm: Implement HWCR support")
---
 arch/x86/kvm/x86.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
arch/x86/kvm/x86.c +2 -3
index 9f18b06bbda6..1a323cae219c 100644
@@ -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.515.g380fc7ccd1-goog
#2 lore raw

PATCH v2 2/3 KVM: x86: Virtualize HWCR.TscFreqSel[bit 24]

In reply to Jim Mattson

On 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.

Attempts to clear this bit from within the guest are ignored, to match the behavior of modern AMD processors.

---
 arch/x86/kvm/x86.c | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)
arch/x86/kvm/x86.c +17 -3
index 1a323cae219c..202972f393b5 100644
@@ -3695,18 +3695,32 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 		break;
 	case MSR_EFER:
 		return set_efer(vcpu, msr_info);
-	case MSR_K7_HWCR:
+	case MSR_K7_HWCR: {
+		u64 allowed = BIT_ULL(18);	/* McStatusWrEn */
+
 		data &= ~(u64)0x40;	/* ignore flush filter disable */
 		data &= ~(u64)0x100;	/* ignore ignne emulation enable */
 		data &= ~(u64)0x8;	/* ignore TLB cache disable */
 
-		/* Handle McStatusWrEn */
-		if (data & ~BIT_ULL(18)) {
+		if (!msr_info->host_initiated)
+			data &= ~BIT_ULL(24);	/* ignore TscFreqSel */
+		else
+			allowed |= BIT_ULL(24);	/* TscFreqSel */
+
+		if (data & ~allowed) {
 			kvm_pr_unimpl_wrmsr(vcpu, msr, data);
 			return 1;
 		}
+
+		/*
+		 * TscFreqSel is read-only from within the
+		 * guest. Attempts to clear it are ignored.
+		 */
+		if (!msr_info->host_initiated)
+			data |= vcpu->arch.msr_hwcr & BIT_ULL(24);
 		vcpu->arch.msr_hwcr = data;
 		break;
+	}
 	case MSR_FAM10H_MMIO_CONF_BASE:
 		if (data != 0) {
 			kvm_pr_unimpl_wrmsr(vcpu, msr, data);
Signature · 1 line
2.42.0.515.g380fc7ccd1-goog
#3 lore raw

PATCH v2 3/3 KVM: selftests: Test behavior of HWCR

In reply to Jim Mattson

Verify 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
index a3bb36fb3cfc..6b0219ca65eb 100644
@@ -135,6 +135,7 @@ TEST_GEN_PROGS_x86_64 += set_memory_region_test
 TEST_GEN_PROGS_x86_64 += steal_time
 TEST_GEN_PROGS_x86_64 += kvm_binary_stats_test
 TEST_GEN_PROGS_x86_64 += system_counter_offset_test
+TEST_GEN_PROGS_x86_64 += x86_64/hwcr_msr_test
 
 # Compiled outputs used by test targets
 TEST_GEN_PROGS_EXTENDED_x86_64 += x86_64/nx_huge_pages_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.515.g380fc7ccd1-goog