From: "Jan Beulich" Subject: [PATCH RFC] x86/EFI: allow FPU/XMM use in runtime service functions Date: Wed, 11 Jun 2014 09:53:20 +0100 Message-ID: <539835200200007800019CBE@mail.emea.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__PartAA986C10.0__=" Return-path: Received: from mail6.bemta5.messagelabs.com ([195.245.231.135]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1WueHe-0005Ee-QT for xen-devel@lists.xenproject.org; Wed, 11 Jun 2014 08:53:27 +0000 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: 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. --=__PartAA986C10.0__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline UEFI spec update 2.4B developed a requirement to enter runtime service functions with CR0.TS (and CR0.EM) clear, thus making feasible the already previously stated permission for these functions to use some of the XMM registers. Enforce this requirement (along with the connected ones on FPU control word and MXCSR) by going through a full FPU save cycle (if the FPU was dirty) in efi_rs_enter() (along with loading the specified values into the other two registers). Note that the UEFI spec mandates that extension registers other than XMM ones (for our purposes all that get restored eagerly) are preserved across runtime function calls, hence there's nothing we need to restore in efi_rs_leave() (they do get saved, but just for simplicity's sake). Signed-off-by: Jan Beulich --- RFC because the patch is yet to be tested on a system where XMM registers are actually being used by runtime service calls. --- a/xen/arch/x86/efi/runtime.c +++ b/xen/arch/x86/efi/runtime.c @@ -10,6 +10,8 @@ DEFINE_XEN_GUEST_HANDLE(CHAR16); =20 #ifndef COMPAT =20 +# include +# include # include =20 const bool_t efi_enabled =3D 1; @@ -45,8 +47,14 @@ const struct efi_pci_rom *__read_mostly=20 =20 unsigned long efi_rs_enter(void) { + static const u16 fcw =3D FCW_DEFAULT; + static const u32 mxcsr =3D MXCSR_DEFAULT; unsigned long cr3 =3D read_cr3(); =20 + save_fpu_enable(); + asm volatile ( "fldcw %0" :: "m" (fcw) ); + asm volatile ( "ldmxcsr %0" :: "m" (mxcsr) ); + spin_lock(&efi_rs_lock); =20 /* prevent fixup_page_fault() from doing anything */ @@ -82,6 +90,7 @@ void efi_rs_leave(unsigned long cr3) } irq_exit(); spin_unlock(&efi_rs_lock); + stts(); } =20 unsigned long efi_get_time(void) --- a/xen/arch/x86/i387.c +++ b/xen/arch/x86/i387.c @@ -266,10 +266,10 @@ void vcpu_restore_fpu_lazy(struct vcpu * * On each context switch, save the necessary FPU info of VCPU being = switch=20 * out. It dispatches saving operation based on CPU's capability. */ -void vcpu_save_fpu(struct vcpu *v) +static bool_t _vcpu_save_fpu(struct vcpu *v) { if ( !v->fpu_dirtied && !v->arch.nonlazy_xstate_used ) - return; + return 0; =20 ASSERT(!is_idle_vcpu(v)); =20 @@ -284,9 +284,22 @@ void vcpu_save_fpu(struct vcpu *v) fpu_fsave(v); =20 v->fpu_dirtied =3D 0; + + return 1; +} + +void vcpu_save_fpu(struct vcpu *v) +{ + _vcpu_save_fpu(v); stts(); } =20 +void save_fpu_enable(void) +{ + if ( !_vcpu_save_fpu(current) ) + clts(); +} + /* Initialize FPU's context save area */ int vcpu_init_fpu(struct vcpu *v) { --- a/xen/include/asm-x86/i387.h +++ b/xen/include/asm-x86/i387.h @@ -38,6 +38,7 @@ struct ix87_state { void vcpu_restore_fpu_eager(struct vcpu *v); void vcpu_restore_fpu_lazy(struct vcpu *v); void vcpu_save_fpu(struct vcpu *v); +void save_fpu_enable(void); =20 int vcpu_init_fpu(struct vcpu *v); void vcpu_destroy_fpu(struct vcpu *v); --=__PartAA986C10.0__= Content-Type: text/plain; name="x86-EFI-runtime-permit-XMM.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="x86-EFI-runtime-permit-XMM.patch" x86/EFI: allow FPU/XMM use in runtime service functions=0A=0AUEFI spec = update 2.4B developed a requirement to enter runtime service=0Afunctions = with CR0.TS (and CR0.EM) clear, thus making feasible the=0Aalready = previously stated permission for these functions to use some of=0Athe XMM = registers. Enforce this requirement (along with the connected=0Aones on = FPU control word and MXCSR) by going through a full FPU save=0Acycle (if = the FPU was dirty) in efi_rs_enter() (along with loading the=0Aspecified = values into the other two registers).=0A=0ANote that the UEFI spec = mandates that extension registers other than=0AXMM ones (for our purposes = all that get restored eagerly) are preserved=0Aacross runtime function = calls, hence there's nothing we need to restore=0Ain efi_rs_leave() (they = do get saved, but just for simplicity's sake).=0A=0ASigned-off-by: Jan = Beulich =0A---=0ARFC because the patch is yet to be = tested on a system where XMM=0Aregisters are actually being used by = runtime service calls.=0A=0A--- a/xen/arch/x86/efi/runtime.c=0A+++ = b/xen/arch/x86/efi/runtime.c=0A@@ -10,6 +10,8 @@ DEFINE_XEN_GUEST_HANDLE(CH= AR16);=0A =0A #ifndef COMPAT=0A =0A+# include =0A+# include = =0A # include =0A =0A const bool_t = efi_enabled =3D 1;=0A@@ -45,8 +47,14 @@ const struct efi_pci_rom *__read_mo= stly =0A =0A unsigned long efi_rs_enter(void)=0A {=0A+ static const u16 = fcw =3D FCW_DEFAULT;=0A+ static const u32 mxcsr =3D MXCSR_DEFAULT;=0A = unsigned long cr3 =3D read_cr3();=0A =0A+ save_fpu_enable();=0A+ = asm volatile ( "fldcw %0" :: "m" (fcw) );=0A+ asm volatile ( "ldmxcsr = %0" :: "m" (mxcsr) );=0A+=0A spin_lock(&efi_rs_lock);=0A =0A /* = prevent fixup_page_fault() from doing anything */=0A@@ -82,6 +90,7 @@ void = efi_rs_leave(unsigned long cr3)=0A }=0A irq_exit();=0A = spin_unlock(&efi_rs_lock);=0A+ stts();=0A }=0A =0A unsigned long = efi_get_time(void)=0A--- a/xen/arch/x86/i387.c=0A+++ b/xen/arch/x86/i387.c= =0A@@ -266,10 +266,10 @@ void vcpu_restore_fpu_lazy(struct vcpu *=0A * On = each context switch, save the necessary FPU info of VCPU being switch =0A = * out. It dispatches saving operation based on CPU's capability.=0A = */=0A-void vcpu_save_fpu(struct vcpu *v)=0A+static bool_t _vcpu_save_fpu(st= ruct vcpu *v)=0A {=0A if ( !v->fpu_dirtied && !v->arch.nonlazy_xstate_u= sed )=0A- return;=0A+ return 0;=0A =0A ASSERT(!is_idle_vc= pu(v));=0A =0A@@ -284,9 +284,22 @@ void vcpu_save_fpu(struct vcpu *v)=0A = fpu_fsave(v);=0A =0A v->fpu_dirtied =3D 0;=0A+=0A+ return = 1;=0A+}=0A+=0A+void vcpu_save_fpu(struct vcpu *v)=0A+{=0A+ _vcpu_save_fp= u(v);=0A stts();=0A }=0A =0A+void save_fpu_enable(void)=0A+{=0A+ if = ( !_vcpu_save_fpu(current) )=0A+ clts();=0A+}=0A+=0A /* Initialize = FPU's context save area */=0A int vcpu_init_fpu(struct vcpu *v)=0A {=0A--- = a/xen/include/asm-x86/i387.h=0A+++ b/xen/include/asm-x86/i387.h=0A@@ -38,6 = +38,7 @@ struct ix87_state {=0A void vcpu_restore_fpu_eager(struct vcpu = *v);=0A void vcpu_restore_fpu_lazy(struct vcpu *v);=0A void vcpu_save_fpu(s= truct vcpu *v);=0A+void save_fpu_enable(void);=0A =0A int vcpu_init_fpu(str= uct vcpu *v);=0A void vcpu_destroy_fpu(struct vcpu *v);=0A --=__PartAA986C10.0__= 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 --=__PartAA986C10.0__=--