Skip to contentSkip to conversation

KVM: SVM: Add support for AMD's OSVW feature in guests

PATCH In review Boris Ostrovsky 3 participants 4 messages Jan 4, 2012 – Jan 6, 2012

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 KVM: SVM: Add support for AMD's OSVW feature in guests

From: Boris Ostrovsky <[email protected]>

In some cases guests should not provide workarounds for errata even when the physical processor is affected. For example, because of erratum 400 on family 10h processors a Linux guest will read an MSR (resulting in VMEXIT) before going to idle in order to avoid getting stuck in a non-C0 state. This is not
necessary: HLT and IO instructions are intercepted and therefore there is no reason for erratum 400 workaround in the guest.

This patch allows us to present a guest with certain errata as fixed, regardless of the state of actual hardware.

---
 arch/x86/include/asm/kvm_host.h |    6 ++++
 arch/x86/kvm/svm.c              |   55 +++++++++++++++++++++++++++++++++++++++
 arch/x86/kvm/x86.c              |   30 ++++++++++++++++++++-
 3 files changed, 90 insertions(+), 1 deletions(-)

Second version of OSVW patch. svm_init_osvw() is still in svm.c but registers' values are kept in virtual MSRs and should be available on reboot after cross-vendor migration. The registers can also be set from userland so that they are consistent across a cluster.

arch/x86/include/asm/kvm_host.h +6 -0
index b4973f4..9ef9215 100644
@@ -452,6 +452,12 @@ struct kvm_vcpu_arch {
 		u32 id;
 		bool send_user_only;
 	} apf;
+
+	/* OSVW MSRs (AMD only) */
+	struct {
+		u64 length;
+		u64 status;
+	} osvw;
 };
 
 struct kvm_arch {
arch/x86/kvm/svm.c +55 -0

79 lines not on this page · raw message

arch/x86/kvm/x86.c +29 -1
index 4c938da..49a7b3c 100644
@@ -598,6 +598,14 @@ static bool guest_cpuid_has_fsgsbase(struct kvm_vcpu *vcpu)
 	return best && (best->ebx & bit(X86_FEATURE_FSGSBASE));
 }
 
+static bool guest_cpuid_has_osvw(struct kvm_vcpu *vcpu)
+{
+	struct kvm_cpuid_entry2 *best;
+
+	best = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
+	return best && (best->ecx & bit(X86_FEATURE_OSVW));
+}
+
 static void update_cpuid(struct kvm_vcpu *vcpu)
 {
 	struct kvm_cpuid_entry2 *best;
@@ -1700,6 +1708,16 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
 		 */
 		pr_unimpl(vcpu, "ignored wrmsr: 0x%x data %llx\n", msr, data);
 		break;
+	case MSR_AMD64_OSVW_ID_LENGTH:
+		if (!guest_cpuid_has_osvw(vcpu))
+			return 1;
+		vcpu->arch.osvw.length = data;
+		break;
+	case MSR_AMD64_OSVW_STATUS:
+		if (!guest_cpuid_has_osvw(vcpu))
+			return 1;
+		vcpu->arch.osvw.status = data;
+		break;
 	default:
 		if (msr && (msr == vcpu->kvm->arch.xen_hvm_config.msr))
 			return xen_hvm_config(vcpu, data);
@@ -1978,6 +1996,16 @@ int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
 		 */
 		data = 0xbe702111;
 		break;
+	case MSR_AMD64_OSVW_ID_LENGTH:
+		if (!guest_cpuid_has_osvw(vcpu))
+			return 1;
+		data = vcpu->arch.osvw.length;
+		break;
+	case MSR_AMD64_OSVW_STATUS:
+		if (!guest_cpuid_has_osvw(vcpu))
+			return 1;
+		data = vcpu->arch.osvw.status;
+		break;
 	default:
 		if (!ignore_msrs) {
 			pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
@@ -2451,7 +2479,7 @@ static void do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
 	const u32 kvm_supported_word6_x86_features =
 		F(LAHF_LM) | F(CMP_LEGACY) | 0 /*SVM*/ | 0 /* ExtApicSpace */ |
 		F(CR8_LEGACY) | F(ABM) | F(SSE4A) | F(MISALIGNSSE) |
-		F(3DNOWPREFETCH) | 0 /* OSVW */ | 0 /* IBS */ | F(XOP) |
+		F(3DNOWPREFETCH) | F(OSVW) | 0 /* IBS */ | F(XOP) |
 		0 /* SKINIT, WDT, LWP */ | F(FMA4) | F(TBM);
 
 	/* cpuid 0xC0000001.edx */
Signature · 1 line
1.7.3.4
#1 lore raw
Quoted 47 lines · Boris Ostrovsky

From: Boris Ostrovsky <[email protected]>

In some cases guests should not provide workarounds for errata even when the physical processor is affected. For example, because of erratum 400 on family 10h processors a Linux guest will read an MSR (resulting in VMEXIT) before going to idle in order to avoid getting stuck in a non-C0 state. This is not
necessary: HLT and IO instructions are intercepted and therefore there is no reason for erratum 400 workaround in the guest.

This patch allows us to present a guest with certain errata as fixed, regardless of the state of actual hardware.

---
 arch/x86/include/asm/kvm_host.h |    6 ++++
 arch/x86/kvm/svm.c              |   55 +++++++++++++++++++++++++++++++++++++++
 arch/x86/kvm/x86.c              |   30 ++++++++++++++++++++-
 3 files changed, 90 insertions(+), 1 deletions(-)

Second version of OSVW patch. svm_init_osvw() is still in svm.c but registers' values are kept in virtual MSRs and should be available on reboot after cross-vendor migration. The registers can also be set from userland so that they are consistent across a cluster.

arch/x86/include/asm/kvm_host.h +6 -0
index b4973f4..9ef9215 100644
@@ -452,6 +452,12 @@ struct kvm_vcpu_arch {
 		u32 id;
 		bool send_user_only;
 	} apf;
+
+	/* OSVW MSRs (AMD only) */
+	struct {
+		u64 length;
+		u64 status;
+	} osvw;
 };
 
 struct kvm_arch {
arch/x86/kvm/svm.c +0 -0
index e32243e..b19769d 100644

arch/x86/kvm/svm.c struct nested_state { · line 118

Quoted 9 lines
arch/x86/kvm/svm.c +6 -0
@@ -110,6 +110,13 @@ struct nested_state {
 #define MSRPM_OFFSETS	16
 static u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly;
 
+/*
+ * Set osvw_len to higher value when updated Revision Guides
+ * are published and we know what the new status bits are
+ */
+static uint64_t osvw_len = 4, osvw_status;
+static DEFINE_SPINLOCK(svm_lock);

No need for this lock, operation already serialized by kvm_lock.

Quoted 5 lines
arch/x86/kvm/svm.c +0 -0
 struct vcpu_svm {
 	struct kvm_vcpu vcpu;
 	struct vmcb *vmcb;
@@ -556,6 +563,20 @@ static void svm_init_erratum_383(void)
 	erratum_383_found = true;

arch/x86/kvm/svm.c static void svm_init_erratum_383(void) · line 573

Quoted 10 lines
arch/x86/kvm/svm.c +8 -0
 }
 
+static void svm_init_osvw(struct kvm_vcpu *vcpu)
+{
+	/*
+	 * Guests should see errata 400 and 415 as fixed (assuming that
+	 * HLT and IO instructions are intercepted).
+	 */
+	vcpu->arch.osvw.length = (osvw_len >= 3) ? (osvw_len) : 3;
+	vcpu->arch.osvw.status = osvw_status & ~(6ULL);

Do you really want to expose the hosts osvw_status and osvw_len? If only exposure of 400 and 415 as fixed is necessary, then dealing with migration is simpler (that is, you control what workarounds are applied in the guest instead of making it dependent on particular hosts).

arch/x86/kvm/svm.c static void svm_init_erratum_383(void) · line 578

Quoted 4 lines
arch/x86/kvm/svm.c +4 -0
+	/* Mark erratum 298 as present */
+	if (osvw_len == 0 && boot_cpu_data.x86 == 0x10)
+		vcpu->arch.osvw.status |= 1;
+}

Why is it necessary to explicitly do this? Please add documentation.

arch/x86/kvm/svm.c static void svm_init_erratum_383(void)

Quoted 10 lines
arch/x86/kvm/svm.c +10 -0
+	case MSR_AMD64_OSVW_ID_LENGTH:
+		if (!guest_cpuid_has_osvw(vcpu))
+			return 1;
+		vcpu->arch.osvw.length = data;
+		break;
+	case MSR_AMD64_OSVW_STATUS:
+		if (!guest_cpuid_has_osvw(vcpu))
+			return 1;
+		vcpu->arch.osvw.status = data;
+		break;

If writes are allowed, it is necessary to migrate this.

Quoted 36 lines
arch/x86/kvm/svm.c +11 -1
 	default:
 		if (msr && (msr == vcpu->kvm->arch.xen_hvm_config.msr))
 			return xen_hvm_config(vcpu, data);
@@ -1978,6 +1996,16 @@ int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
 		 */
 		data = 0xbe702111;
 		break;
+	case MSR_AMD64_OSVW_ID_LENGTH:
+		if (!guest_cpuid_has_osvw(vcpu))
+			return 1;
+		data = vcpu->arch.osvw.length;
+		break;
+	case MSR_AMD64_OSVW_STATUS:
+		if (!guest_cpuid_has_osvw(vcpu))
+			return 1;
+		data = vcpu->arch.osvw.status;
+		break;
 	default:
 		if (!ignore_msrs) {
 			pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
@@ -2451,7 +2479,7 @@ static void do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
 	const u32 kvm_supported_word6_x86_features =
 		F(LAHF_LM) | F(CMP_LEGACY) | 0 /*SVM*/ | 0 /* ExtApicSpace */ |
 		F(CR8_LEGACY) | F(ABM) | F(SSE4A) | F(MISALIGNSSE) |
-		F(3DNOWPREFETCH) | 0 /* OSVW */ | 0 /* IBS */ | F(XOP) |
+		F(3DNOWPREFETCH) | F(OSVW) | 0 /* IBS */ | F(XOP) |
 		0 /* SKINIT, WDT, LWP */ | F(FMA4) | F(TBM);
 
 	/* cpuid 0xC0000001.edx */
Signature · 6 lines
1.7.3.4

--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#2 lore raw
Quoted 10 lines · Marcelo Tosatti
Quoted 9 lines · Boris Ostrovsky
arch/x86/kvm/svm.c +0 -0
index e32243e..b19769d 100644
@@ -110,6 +110,13 @@ struct nested_state {
  #define MSRPM_OFFSETS	16
  static u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly;
 
Quoted 8 lines

arch/x86/kvm/svm.c struct nested_state { · line 118

Quoted 6 lines
arch/x86/kvm/svm.c +6 -0
+/*
+ * Set osvw_len to higher value when updated Revision Guides
+ * are published and we know what the new status bits are
+ */
+static uint64_t osvw_len = 4, osvw_status;
+static DEFINE_SPINLOCK(svm_lock);

No need for this lock, operation already serialized by kvm_lock.

Will remove the lock.

Quoted 8 lines
Quoted 8 lines
arch/x86/kvm/svm.c +0 -0
  struct vcpu_svm {
  	struct kvm_vcpu vcpu;
  	struct vmcb *vmcb;
@@ -556,6 +563,20 @@ static void svm_init_erratum_383(void)
  	erratum_383_found = true;
  }
 
Quoted 13 lines

arch/x86/kvm/svm.c static void svm_init_erratum_383(void)

Quoted 8 lines
arch/x86/kvm/svm.c +8 -0
+static void svm_init_osvw(struct kvm_vcpu *vcpu)
+{
+	/*
+	 * Guests should see errata 400 and 415 as fixed (assuming that
+	 * HLT and IO instructions are intercepted).
+	 */
+	vcpu->arch.osvw.length = (osvw_len>= 3) ? (osvw_len) : 3;
+	vcpu->arch.osvw.status = osvw_status&  ~(6ULL);

Do you really want to expose the hosts osvw_status and osvw_len? If only exposure of 400 and 415 as fixed is necessary, then dealing with migration is simpler (that is, you control what workarounds are applied in the guest instead of making it dependent on particular hosts).

I do think we should (selectively) expose osvw information to the host. As of today we have 4 errata described by these bits. Two of them (400 and 415) don't need to be seen by the guest and the patch would mask them off. As for the other two (errata 383 and 298) --- the guest should be aware of them and the patch passes them through.

Since osvw_len is initialized to 4 and cannot become larger no other bits will be passed to guests until we update the initial value (by a future patch).

If we are concerned about migration we can always ovewrite vcpu->arch.osvw registers from userspace when creating a guest.

Quoted 7 lines

arch/x86/kvm/svm.c static void svm_init_erratum_383(void) · line 578

Quoted 5 lines
arch/x86/kvm/svm.c +4 -0
+	/* Mark erratum 298 as present */
+	if (osvw_len == 0&&  boot_cpu_data.x86 == 0x10)
+		vcpu->arch.osvw.status |= 1;
+}

Why is it necessary to explicitly do this? Please add documentation.

That's because we may have bumped vcpu->arch.osvw.length to 3 in order to signal the guest that 400 and 415 are fixed. By doing this we are also telling the guest that it can rely on state of bit0.

If we leave bit0 as 0 the guest will assume that 298 is fixed. However, if host's osvw_length is 0 it means that the physical HW *may* still be affected. So we take conservative approach and tell the guest that it should work around 298.

I'll add a comment to that effect.

Quoted 13 lines

arch/x86/kvm/svm.c static void svm_init_erratum_383(void)

Quoted 11 lines
arch/x86/kvm/svm.c +10 -0
+	case MSR_AMD64_OSVW_ID_LENGTH:
+		if (!guest_cpuid_has_osvw(vcpu))
+			return 1;
+		vcpu->arch.osvw.length = data;
+		break;
+	case MSR_AMD64_OSVW_STATUS:
+		if (!guest_cpuid_has_osvw(vcpu))
+			return 1;
+		vcpu->arch.osvw.status = data;
+		break;

If writes are allowed, it is necessary to migrate this.

Not sure I understand what you mean here. Isn't vcpu->arch state migrated with the guest?

Thanks.
-boris

#3 lore raw
Quoted 50 lines · Boris Ostrovsky
Quoted 10 lines · Marcelo Tosatti
Quoted 9 lines · Boris Ostrovsky
arch/x86/kvm/svm.c +0 -0
index e32243e..b19769d 100644
@@ -110,6 +110,13 @@ struct nested_state {
 #define MSRPM_OFFSETS	16
 static u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly;
 
Quoted 8 lines

arch/x86/kvm/svm.c struct nested_state { · line 118

Quoted 6 lines
arch/x86/kvm/svm.c +6 -0
+/*
+ * Set osvw_len to higher value when updated Revision Guides
+ * are published and we know what the new status bits are
+ */
+static uint64_t osvw_len = 4, osvw_status;
+static DEFINE_SPINLOCK(svm_lock);

No need for this lock, operation already serialized by kvm_lock.

Will remove the lock.

Quoted 8 lines
Quoted 8 lines
arch/x86/kvm/svm.c +0 -0
 struct vcpu_svm {
 	struct kvm_vcpu vcpu;
 	struct vmcb *vmcb;
@@ -556,6 +563,20 @@ static void svm_init_erratum_383(void)
 	erratum_383_found = true;
 }
 
Quoted 13 lines

arch/x86/kvm/svm.c static void svm_init_erratum_383(void)

Quoted 8 lines
arch/x86/kvm/svm.c +8 -0
+static void svm_init_osvw(struct kvm_vcpu *vcpu)
+{
+	/*
+	 * Guests should see errata 400 and 415 as fixed (assuming that
+	 * HLT and IO instructions are intercepted).
+	 */
+	vcpu->arch.osvw.length = (osvw_len>= 3) ? (osvw_len) : 3;
+	vcpu->arch.osvw.status = osvw_status&  ~(6ULL);

Do you really want to expose the hosts osvw_status and osvw_len? If only exposure of 400 and 415 as fixed is necessary, then dealing with migration is simpler (that is, you control what workarounds are applied in the guest instead of making it dependent on particular hosts).

I do think we should (selectively) expose osvw information to the host. As of today we have 4 errata described by these bits. Two of them (400 and 415) don't need to be seen by the guest and the patch would mask them off. As for the other two (errata 383 and 298) --- the guest should be aware of them and the patch passes them through.

Quoted 3 lines

Since osvw_len is initialized to 4 and cannot become larger no other bits will be passed to guests until we update the initial value (by a future patch).

OK.

Quoted 15 lines

If we are concerned about migration we can always ovewrite vcpu->arch.osvw registers from userspace when creating a guest.

Quoted 7 lines

arch/x86/kvm/svm.c static void svm_init_erratum_383(void) · line 578

Quoted 5 lines
arch/x86/kvm/svm.c +4 -0
+	/* Mark erratum 298 as present */
+	if (osvw_len == 0&&  boot_cpu_data.x86 == 0x10)
+		vcpu->arch.osvw.status |= 1;
+}

Why is it necessary to explicitly do this? Please add documentation.

That's because we may have bumped vcpu->arch.osvw.length to 3 in order to signal the guest that 400 and 415 are fixed. By doing this we are also telling the guest that it can rely on state of bit0.

Quoted 6 lines

If we leave bit0 as 0 the guest will assume that 298 is fixed. However, if host's osvw_length is 0 it means that the physical HW *may* still be affected. So we take conservative approach and tell the guest that it should work around 298.

I'll add a comment to that effect.

OK thanks.

Quoted 14 lines
Quoted 13 lines

arch/x86/kvm/svm.c static void svm_init_erratum_383(void)

Quoted 11 lines
arch/x86/kvm/svm.c +10 -0
+	case MSR_AMD64_OSVW_ID_LENGTH:
+		if (!guest_cpuid_has_osvw(vcpu))
+			return 1;
+		vcpu->arch.osvw.length = data;
+		break;
+	case MSR_AMD64_OSVW_STATUS:
+		if (!guest_cpuid_has_osvw(vcpu))
+			return 1;
+		vcpu->arch.osvw.status = data;
+		break;

If writes are allowed, it is necessary to migrate this.

Quoted 2 lines

Not sure I understand what you mean here. Isn't vcpu->arch state migrated with the guest?

Since the MSR value is setup in the kernel, there is no need to migrate it, actually.