public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
From: Avi Kivity <avi@qumranet.com>
To: kvm-devel@lists.sourceforge.net
Cc: linux-kernel@vger.kernel.org,
	Anthony Liguori <aliguori@us.ibm.com>,
	Avi Kivity <avi@qumranet.com>
Subject: [PATCH 17/18] KVM: Lazy FPU support for SVM
Date: Thu, 26 Apr 2007 12:22:17 +0300	[thread overview]
Message-ID: <11775793381543-git-send-email-avi@qumranet.com> (raw)
In-Reply-To: <11775793382353-git-send-email-avi@qumranet.com>

From: Anthony Liguori <aliguori@us.ibm.com>

Avoid saving and restoring the guest fpu state on every exit.  This
shaves ~100 cycles off the guest/host switch.

Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
Signed-off-by: Avi Kivity <avi@qumranet.com>
---
 drivers/kvm/kvm.h |    2 ++
 drivers/kvm/svm.c |   35 +++++++++++++++++++++++++++++++----
 2 files changed, 33 insertions(+), 4 deletions(-)

diff --git a/drivers/kvm/kvm.h b/drivers/kvm/kvm.h
index d1a90c5..61ff085 100644
--- a/drivers/kvm/kvm.h
+++ b/drivers/kvm/kvm.h
@@ -63,6 +63,7 @@
 #define FX_BUF_SIZE (2 * FX_IMAGE_SIZE + FX_IMAGE_ALIGN)
 
 #define DE_VECTOR 0
+#define NM_VECTOR 7
 #define DF_VECTOR 8
 #define TS_VECTOR 10
 #define NP_VECTOR 11
@@ -301,6 +302,7 @@ struct kvm_vcpu {
 	char fx_buf[FX_BUF_SIZE];
 	char *host_fx_image;
 	char *guest_fx_image;
+	int fpu_active;
 
 	int mmio_needed;
 	int mmio_read_completed;
diff --git a/drivers/kvm/svm.c b/drivers/kvm/svm.c
index 644efc5..9438931 100644
--- a/drivers/kvm/svm.c
+++ b/drivers/kvm/svm.c
@@ -587,6 +587,7 @@ static int svm_create_vcpu(struct kvm_vcpu *vcpu)
 	init_vmcb(vcpu->svm->vmcb);
 
 	fx_init(vcpu);
+	vcpu->fpu_active = 1;
 	vcpu->apic_base = 0xfee00000 |
 			/*for vcpu 0*/ MSR_IA32_APICBASE_BSP |
 			MSR_IA32_APICBASE_ENABLE;
@@ -756,6 +757,11 @@ static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
 		}
 	}
 #endif
+	if ((vcpu->cr0 & CR0_TS_MASK) && !(cr0 & CR0_TS_MASK)) {
+		vcpu->svm->vmcb->control.intercept_exceptions &= ~(1 << NM_VECTOR);
+		vcpu->fpu_active = 1;
+	}
+ 
 	vcpu->cr0 = cr0;
 	cr0 |= CR0_PG_MASK | CR0_WP_MASK;
 	cr0 &= ~(CR0_CD_MASK | CR0_NW_MASK);
@@ -928,6 +934,16 @@ static int pf_interception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
 	return 0;
 }
 
+static int nm_interception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
+{
+       vcpu->svm->vmcb->control.intercept_exceptions &= ~(1 << NM_VECTOR);
+       if (!(vcpu->cr0 & CR0_TS_MASK))
+               vcpu->svm->vmcb->save.cr0 &= ~CR0_TS_MASK;
+       vcpu->fpu_active = 1;
+
+       return 1;
+}
+
 static int shutdown_interception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
 {
 	/*
@@ -1292,6 +1308,7 @@ static int (*svm_exit_handlers[])(struct kvm_vcpu *vcpu,
 	[SVM_EXIT_WRITE_DR5]			= emulate_on_interception,
 	[SVM_EXIT_WRITE_DR7]			= emulate_on_interception,
 	[SVM_EXIT_EXCP_BASE + PF_VECTOR] 	= pf_interception,
+	[SVM_EXIT_EXCP_BASE + NM_VECTOR] 	= nm_interception,
 	[SVM_EXIT_INTR] 			= nop_on_interception,
 	[SVM_EXIT_NMI]				= nop_on_interception,
 	[SVM_EXIT_SMI]				= nop_on_interception,
@@ -1481,8 +1498,10 @@ again:
 		load_db_regs(vcpu->svm->db_regs);
 	}
 
-	fx_save(vcpu->host_fx_image);
-	fx_restore(vcpu->guest_fx_image);
+	if (vcpu->fpu_active) {
+		fx_save(vcpu->host_fx_image);
+		fx_restore(vcpu->guest_fx_image);
+	}
 
 	asm volatile (
 #ifdef CONFIG_X86_64
@@ -1593,8 +1612,10 @@ again:
 #endif
 		: "cc", "memory" );
 
-	fx_save(vcpu->guest_fx_image);
-	fx_restore(vcpu->host_fx_image);
+	if (vcpu->fpu_active) {
+		fx_save(vcpu->guest_fx_image);
+		fx_restore(vcpu->host_fx_image);
+	}
 
 	if ((vcpu->svm->vmcb->save.dr7 & 0xff))
 		load_db_regs(vcpu->svm->host_db_regs);
@@ -1664,6 +1685,12 @@ static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
 {
 	vcpu->svm->vmcb->save.cr3 = root;
 	force_new_asid(vcpu);
+ 
+	if (vcpu->fpu_active) {
+		vcpu->svm->vmcb->control.intercept_exceptions |= (1 << NM_VECTOR);
+		vcpu->svm->vmcb->save.cr0 |= CR0_TS_MASK;
+		vcpu->fpu_active = 0;
+	}
 }
 
 static void svm_inject_page_fault(struct kvm_vcpu *vcpu,
-- 
1.5.0.6

  parent reply	other threads:[~2007-04-26  9:22 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-04-26  9:22 [PATCH 00/18] KVM updates for 2.6.22 Avi Kivity
2007-04-26  9:22 ` [PATCH 02/18] KVM: Fix overflow bug in overflow detection code Avi Kivity
2007-04-26  9:22 ` [PATCH 03/18] KVM: Initialize cr0 to indicate an fpu is present Avi Kivity
2007-04-26  9:22 ` [PATCH 04/18] KVM: Handle partial pae pdptr Avi Kivity
     [not found] ` <11775793382353-git-send-email-avi-atKUWr5tajBWk0Htik3J/w@public.gmane.org>
2007-04-26  9:22   ` [PATCH 01/18] KVM: Use kernel-standard types Avi Kivity
2007-04-26  9:22   ` [PATCH 05/18] KVM: Use slab caches to allocate mmu data structures Avi Kivity
2007-04-26  9:22   ` [PATCH 06/18] KVM: Retry sleeping allocation if atomic allocation fails Avi Kivity
2007-04-26  9:22   ` [PATCH 07/18] KVM: SVM: Report hardware exit reason to userspace instead of dmesg Avi Kivity
2007-04-26  9:22   ` [PATCH 08/18] KVM: Handle guest page faults when emulating mmio Avi Kivity
2007-04-26  9:22   ` [PATCH 09/18] KVM: VMX: Reduce unnecessary saving of host msrs Avi Kivity
2007-04-26  9:22   ` [PATCH 10/18] KVM: VMX: Don't switch 64-bit msrs for 32-bit guests Avi Kivity
2007-04-26  9:22   ` [PATCH 11/18] KVM: Fold drivers/kvm/kvm_vmx.h into drivers/kvm/vmx.c Avi Kivity
2007-04-26  9:22   ` [PATCH 12/18] KVM: VMX: Only save/restore MSR_K6_STAR if necessary Avi Kivity
2007-04-26  9:22   ` [PATCH 13/18] KVM: MMU: Avoid heavy ASSERT at non debug mode Avi Kivity
2007-04-26  9:22   ` [PATCH 14/18] KVM: VMX: Avoid unnecessary vcpu_load()/vcpu_put() cycles Avi Kivity
2007-04-26  9:22   ` [PATCH 15/18] KVM: Per-vcpu statistics Avi Kivity
2007-04-26  9:22 ` [PATCH 16/18] KVM: Allow passing 64-bit values to the emulated read/write API Avi Kivity
2007-04-26  9:22 ` Avi Kivity [this message]
2007-04-26  9:22 ` [PATCH 18/18] KVM: Don't complain about cpu erratum AA15 Avi Kivity

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=11775793381543-git-send-email-avi@qumranet.com \
    --to=avi@qumranet.com \
    --cc=aliguori@us.ibm.com \
    --cc=kvm-devel@lists.sourceforge.net \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox