From patchwork Wed Sep 12 08:10:24 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Hao, Xudong" X-Patchwork-Id: 1441391 Return-Path: X-Original-To: patchwork-kvm@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork1.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork1.kernel.org (Postfix) with ESMTP id B589A3FE79 for ; Wed, 12 Sep 2012 08:07:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752900Ab2ILIH1 (ORCPT ); Wed, 12 Sep 2012 04:07:27 -0400 Received: from mga01.intel.com ([192.55.52.88]:32190 "EHLO mga01.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756058Ab2ILIHQ (ORCPT ); Wed, 12 Sep 2012 04:07:16 -0400 Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga101.fm.intel.com with ESMTP; 12 Sep 2012 01:07:15 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="4.80,408,1344236400"; d="scan'208";a="220937939" Received: from xhao-dev.sh.intel.com (HELO localhost.localdomain) ([10.239.48.48]) by fmsmga002.fm.intel.com with ESMTP; 12 Sep 2012 01:07:14 -0700 From: Xudong Hao To: avi@redhat.com Cc: kvm@vger.kernel.org, xiantao.zhang@intel.com, Xudong Hao Subject: [PATCH v3] kvm/fpu: Enable fully eager restore kvm FPU Date: Wed, 12 Sep 2012 16:10:24 +0800 Message-Id: <1347437424-3006-1-git-send-email-xudong.hao@intel.com> X-Mailer: git-send-email 1.5.5 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Enable KVM FPU fully eager restore, if there is other FPU state which isn't tracked by CR0.TS bit. v3 changes from v2: - Make fpu active explicitly while guest xsave is enabling and non-lazy xstate bit exist. v2 changes from v1: - Expand KVM_XSTATE_LAZY to 64 bits before negating it. Signed-off-by: Xudong Hao --- arch/x86/include/asm/kvm.h | 4 ++++ arch/x86/kvm/vmx.c | 2 ++ arch/x86/kvm/x86.c | 15 ++++++++++++++- 3 files changed, 20 insertions(+), 1 deletions(-) diff --git a/arch/x86/include/asm/kvm.h b/arch/x86/include/asm/kvm.h index 521bf25..4c27056 100644 --- a/arch/x86/include/asm/kvm.h +++ b/arch/x86/include/asm/kvm.h @@ -8,6 +8,8 @@ #include #include +#include +#include /* Select x86 specific features in */ #define __KVM_HAVE_PIT @@ -30,6 +32,8 @@ /* Architectural interrupt line count. */ #define KVM_NR_INTERRUPTS 256 +#define KVM_XSTATE_LAZY (XSTATE_FP | XSTATE_SSE | XSTATE_YMM) + struct kvm_memory_alias { __u32 slot; /* this has a different namespace than memory slots */ __u32 flags; diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c index 248c2b4..853e875 100644 --- a/arch/x86/kvm/vmx.c +++ b/arch/x86/kvm/vmx.c @@ -3028,6 +3028,8 @@ static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) if (!vcpu->fpu_active) hw_cr0 |= X86_CR0_TS | X86_CR0_MP; + else + hw_cr0 &= ~(X86_CR0_TS | X86_CR0_MP); vmcs_writel(CR0_READ_SHADOW, cr0); vmcs_writel(GUEST_CR0, hw_cr0); diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 20f2266..183cf60 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -560,6 +560,8 @@ int __kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr) return 1; if (xcr0 & ~host_xcr0) return 1; + if (xcr0 & ~((u64)KVM_XSTATE_LAZY)) + vcpu->fpu_active = 1; vcpu->arch.xcr0 = xcr0; vcpu->guest_xcr0_loaded = 0; return 0; @@ -5969,7 +5971,18 @@ void kvm_put_guest_fpu(struct kvm_vcpu *vcpu) vcpu->guest_fpu_loaded = 0; fpu_save_init(&vcpu->arch.guest_fpu); ++vcpu->stat.fpu_reload; - kvm_make_request(KVM_REQ_DEACTIVATE_FPU, vcpu); + /* + * Currently KVM trigger FPU restore by #NM (via CR0.TS), + * till now only XCR0.bit0, XCR0.bit1, XCR0.bit2 is tracked + * by TS bit, there might be other FPU state is not tracked + * by TS bit. Here it only make FPU deactivate request and do + * FPU lazy restore for these cases: 1)xsave isn't enabled + * in guest, 2)all guest FPU states can be tracked by TS bit. + * For others, doing fully FPU eager restore. + */ + if (!kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE) || + !(vcpu->arch.xcr0 & ~((u64)KVM_XSTATE_LAZY))) + kvm_make_request(KVM_REQ_DEACTIVATE_FPU, vcpu); trace_kvm_fpu(0); }