From: "Jan Beulich" Subject: [PATCH 3/4] x86/xsave: adjust state management Date: Tue, 04 Jun 2013 09:09:14 +0100 Message-ID: <51ADBCCA02000078000DAF0B@nat28.tlf.novell.com> References: <51ADBA6102000078000DAEAA@nat28.tlf.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__Part8EBE7FBA.1__=" Return-path: In-Reply-To: <51ADBA6102000078000DAEAA@nat28.tlf.novell.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: xen-devel Cc: George Dunlap , Keir Fraser List-Id: xen-devel@lists.xenproject.org This is a MIME message. If you are reading this text, you may want to consider changing to a mail reader or gateway that understands how to properly handle MIME multipart messages. --=__Part8EBE7FBA.1__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline The initial state for a vCPU is using default values, so there's no need to force the XRSTOR to read the state from memory. This saves a couple of thousand restores from memory just during boot of Linux on my Sandy Bridge system (I didn't try to make further measurements). The above requires that arch_set_info_guest() updates the state flags in the save area when valid floating point state got passed in, but that would really have been needed even before in case XSAVE{,OPT} decided to clear one or both of the FP and SSE bits. Furthermore, hvm_vcpu_reset_state() shouldn't just clear out the FPU/ SSE area, but needs to re-initialized MXCSR and FCW. Signed-off-by: Jan Beulich --- a/xen/arch/x86/domain.c +++ b/xen/arch/x86/domain.c @@ -712,7 +712,11 @@ int arch_set_info_guest( v->arch.vgc_flags =3D flags; =20 if ( flags & VGCF_I387_VALID ) + { memcpy(v->arch.fpu_ctxt, &c.nat->fpu_ctxt, sizeof(c.nat->fpu_ctxt)= ); + if ( v->arch.xsave_area ) + v->arch.xsave_area->xsave_hdr.xstate_bv =3D XSTATE_FP_SSE; + } =20 if ( !compat ) { --- a/xen/arch/x86/hvm/hvm.c +++ b/xen/arch/x86/hvm/hvm.c @@ -3454,6 +3454,7 @@ void hvm_vcpu_reset_state(struct vcpu *v { struct domain *d =3D v->domain; struct segment_register reg; + typeof(v->arch.xsave_area->fpu_sse) *fpu_ctxt =3D v->arch.fpu_ctxt; =20 domain_lock(d); =20 @@ -3467,7 +3468,12 @@ void hvm_vcpu_reset_state(struct vcpu *v v->arch.guest_table =3D pagetable_null(); } =20 - memset(v->arch.fpu_ctxt, 0, sizeof(v->arch.xsave_area->fpu_sse)); + memset(fpu_ctxt, 0, sizeof(*fpu_ctxt)); + fpu_ctxt->fcw =3D FCW_RESET; + fpu_ctxt->mxcsr =3D MXCSR_DEFAULT; + if ( v->arch.xsave_area ) + v->arch.xsave_area->xsave_hdr.xstate_bv =3D XSTATE_FP; + v->arch.vgc_flags =3D VGCF_online; memset(&v->arch.user_regs, 0, sizeof(v->arch.user_regs)); v->arch.user_regs.eflags =3D 2; --- a/xen/arch/x86/xstate.c +++ b/xen/arch/x86/xstate.c @@ -204,9 +204,13 @@ int xstate_alloc_save_area(struct vcpu * if ( save_area =3D=3D NULL ) return -ENOMEM; =20 + /* + * Set the memory image to default values, but don't force the = context + * to be loaded from memory (i.e. keep save_area->xsave_hdr.xstate_bv + * clear). + */ save_area->fpu_sse.fcw =3D FCW_DEFAULT; save_area->fpu_sse.mxcsr =3D MXCSR_DEFAULT; - save_area->xsave_hdr.xstate_bv =3D XSTATE_FP_SSE; =20 v->arch.xsave_area =3D save_area; v->arch.xcr0 =3D XSTATE_FP_SSE; --- a/xen/include/asm-x86/xstate.h +++ b/xen/include/asm-x86/xstate.h @@ -11,6 +11,7 @@ #include =20 #define FCW_DEFAULT 0x037f +#define FCW_RESET 0x0040 #define MXCSR_DEFAULT 0x1f80 =20 #define XSTATE_CPUID 0x0000000d --=__Part8EBE7FBA.1__= Content-Type: text/plain; name="x86-xstate-default.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="x86-xstate-default.patch" x86/xsave: adjust state management=0A=0AThe initial state for a vCPU is = using default values, so there's no=0Aneed to force the XRSTOR to read the = state from memory. This saves a=0Acouple of thousand restores from memory = just during boot of Linux on=0Amy Sandy Bridge system (I didn't try to = make further measurements).=0A=0AThe above requires that arch_set_info_gues= t() updates the state flags=0Ain the save area when valid floating point = state got passed in, but=0Athat would really have been needed even before = in case XSAVE{,OPT}=0Adecided to clear one or both of the FP and SSE = bits.=0A=0AFurthermore, hvm_vcpu_reset_state() shouldn't just clear out = the FPU/=0ASSE area, but needs to re-initialized MXCSR and FCW.=0A=0ASigned= -off-by: Jan Beulich =0A=0A--- a/xen/arch/x86/domain.c= =0A+++ b/xen/arch/x86/domain.c=0A@@ -712,7 +712,11 @@ int arch_set_info_gue= st(=0A v->arch.vgc_flags =3D flags;=0A =0A if ( flags & VGCF_I387_V= ALID )=0A+ {=0A memcpy(v->arch.fpu_ctxt, &c.nat->fpu_ctxt, = sizeof(c.nat->fpu_ctxt));=0A+ if ( v->arch.xsave_area )=0A+ = v->arch.xsave_area->xsave_hdr.xstate_bv =3D XSTATE_FP_SSE;=0A+ }=0A = =0A if ( !compat )=0A {=0A--- a/xen/arch/x86/hvm/hvm.c=0A+++ = b/xen/arch/x86/hvm/hvm.c=0A@@ -3454,6 +3454,7 @@ void hvm_vcpu_reset_state(= struct vcpu *v=0A {=0A struct domain *d =3D v->domain;=0A struct = segment_register reg;=0A+ typeof(v->arch.xsave_area->fpu_sse) *fpu_ctxt = =3D v->arch.fpu_ctxt;=0A =0A domain_lock(d);=0A =0A@@ -3467,7 +3468,12 = @@ void hvm_vcpu_reset_state(struct vcpu *v=0A v->arch.guest_table = =3D pagetable_null();=0A }=0A =0A- memset(v->arch.fpu_ctxt, 0, = sizeof(v->arch.xsave_area->fpu_sse));=0A+ memset(fpu_ctxt, 0, sizeof(*fp= u_ctxt));=0A+ fpu_ctxt->fcw =3D FCW_RESET;=0A+ fpu_ctxt->mxcsr =3D = MXCSR_DEFAULT;=0A+ if ( v->arch.xsave_area )=0A+ v->arch.xsave_ar= ea->xsave_hdr.xstate_bv =3D XSTATE_FP;=0A+=0A v->arch.vgc_flags =3D = VGCF_online;=0A memset(&v->arch.user_regs, 0, sizeof(v->arch.user_regs)= );=0A v->arch.user_regs.eflags =3D 2;=0A--- a/xen/arch/x86/xstate.c=0A+= ++ b/xen/arch/x86/xstate.c=0A@@ -204,9 +204,13 @@ int xstate_alloc_save_are= a(struct vcpu *=0A if ( save_area =3D=3D NULL )=0A return = -ENOMEM;=0A =0A+ /*=0A+ * Set the memory image to default values, = but don't force the context=0A+ * to be loaded from memory (i.e. keep = save_area->xsave_hdr.xstate_bv=0A+ * clear).=0A+ */=0A = save_area->fpu_sse.fcw =3D FCW_DEFAULT;=0A save_area->fpu_sse.mxcsr = =3D MXCSR_DEFAULT;=0A- save_area->xsave_hdr.xstate_bv =3D XSTATE_FP_SSE;= =0A =0A v->arch.xsave_area =3D save_area;=0A v->arch.xcr0 =3D = XSTATE_FP_SSE;=0A--- a/xen/include/asm-x86/xstate.h=0A+++ b/xen/include/asm= -x86/xstate.h=0A@@ -11,6 +11,7 @@=0A #include =0A =0A #define = FCW_DEFAULT 0x037f=0A+#define FCW_RESET = 0x0040=0A #define MXCSR_DEFAULT 0x1f80=0A =0A #define = XSTATE_CPUID 0x0000000d=0A --=__Part8EBE7FBA.1__= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xen.org http://lists.xen.org/xen-devel --=__Part8EBE7FBA.1__=--