Skip to contentSkip to conversation

x86: Always use eager-fpu

PATCH Superseded Ross Lagerwall 3 participants 26 messages Mar 19 – Mar 24

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover x86: Always use eager-fpu

Remove the eager-fpu option and cleanup the code accordingly.

The first patch removes the option and includes the justification. The rest of the patches are cleanup and mostly have no functional change.

I measured a 0.7% geometric mean improvement with this series across a few different benchmarks (Linux VMs on an AMD host). I'm not sure the improvement is statistically significant, but it likely indicates it is no worse than before.

A previous series to do this was submitted here:
https://lore.kernel.org/xen-devel/[email protected]/

With the exception of patch 4, I haven't kept authorship and sign-offs since this is a reimplementation, albeit ending up at a similar place.

Thanks,
Ross

Ross Lagerwall (7):
x86: Always use eager-fpu
x86/vmx: Remove lazy FPU support
x86/svm: Remove lazy FPU support
x86: Remove fully_eager_fpu
x86: Remove fpu_initialised/fpu_dirty
x86/xstate: Stop tracking nonlazy xstate use
x86: Cleanup cr0.TS flag handling

Wei Liu (1):
x86/traps: Remove lazy FPU support

 docs/misc/hypfs-paths.pandoc             |   2 -
 docs/misc/xen-command-line.pandoc        |   7 +-
 xen/arch/x86/cpu/common.c                |   3 -
 xen/arch/x86/domain.c                    |   2 +-
 xen/arch/x86/domctl.c                    |   4 +-
 xen/arch/x86/hvm/emulate.c               |  38 +-------
 xen/arch/x86/hvm/hvm.c                   |  16 ++--
 xen/arch/x86/hvm/svm/nestedsvm.c         |  67 +-------------
 xen/arch/x86/hvm/svm/svm.c               |  81 +---------------
 xen/arch/x86/hvm/svm/vmcb.c              |   4 +-
 xen/arch/x86/hvm/vlapic.c                |   3 -
 xen/arch/x86/hvm/vmx/vmcs.c              |   8 +-
 xen/arch/x86/hvm/vmx/vmx.c               |  70 +-------------
 xen/arch/x86/hvm/vmx/vvmx.c              |  15 +--
 xen/arch/x86/i387.c                      | 112 ++---------------------
 xen/arch/x86/include/asm/domain.h        |   6 --
 xen/arch/x86/include/asm/hvm/hvm.h       |   3 -
 xen/arch/x86/include/asm/hvm/svm-types.h |   6 --
 xen/arch/x86/include/asm/hvm/vmx/vmcs.h  |   2 -
 xen/arch/x86/include/asm/i387.h          |   4 +-
 xen/arch/x86/include/asm/spec_ctrl.h     |   1 -
 xen/arch/x86/include/asm/xstate.h        |  17 +---
 xen/arch/x86/pv/misc-hypercalls.c        |   3 +-
 xen/arch/x86/spec_ctrl.c                 |  88 +-----------------
 xen/arch/x86/traps.c                     |  18 ++--
 xen/arch/x86/xstate.c                    |  24 +----
 xen/common/domain.c                      |   2 -
 xen/common/efi/runtime.c                 |   4 +-
 xen/include/xen/sched.h                  |   4 -
 29 files changed, 56 insertions(+), 558 deletions(-)
Signature · 1 line
2.53.0
#1 lore raw

PATCH 1/8 x86: Always use eager-fpu

Lazy FPU avoids some work during a context switch but pushes the costs elsewhere:

* For a workload running some Windows VMs, I measured about 83% of context switches out had used the FPU so most of the time the FPU save/restore is not avoided, just delayed.
* Setting/clearing the cr0.TS bit is serializing and reportedly slower than the processor optimized xsave/restore.
* Linux uses PKRU so a partial xsave/restore is performed on each context switch anyway, followed by a second xsave/restore at some point during execution.

There is no measurable performance benefit for using lazy FPU and it adds unwanted complexity so remove the option and always use eager-fpu.

---
 docs/misc/hypfs-paths.pandoc         |  2 -
 docs/misc/xen-command-line.pandoc    |  7 +--
 xen/arch/x86/i387.c                  |  2 +-
 xen/arch/x86/include/asm/spec_ctrl.h |  1 -
 xen/arch/x86/spec_ctrl.c             | 88 ++--------------------------
 5 files changed, 6 insertions(+), 94 deletions(-)
docs/misc/hypfs-paths.pandoc +0 -2
index e86f7d0dbef9..1553cb0bcb7f 100644
@@ -108,12 +108,10 @@ A populated Xen hypervisor file system might look like the following example:
             active-hvm/      directory for mitigations active in hvm doamins
                 msr-spec-ctrl "No" or "Yes"
                 rsb          "No" or "Yes"
-                eager-fpu    "No" or "Yes"
                 md-clear     "No" or "Yes"
             active-pv/       directory for mitigations active in pv doamins
                 msr-spec-ctrl "No" or "Yes"
                 rsb          "No" or "Yes"
-                eager-fpu    "No" or "Yes"
                 md-clear     "No" or "Yes"
                 xpti         "No" or list of "dom0", "domU", "PCID-on"
                 l1tf-shadow  "No" or list of "dom0", "domU"
docs/misc/xen-command-line.pandoc +1 -6
index ebdca007d26b..6c77129732bf 100644
@@ -2470,7 +2470,7 @@ By default SSBD will be mitigated at runtime (i.e `ssbd=runtime`).
 >              {msr-sc,rsb,verw,{ibpb,bhb}-entry}=<bool>|{pv,hvm}=<bool>,
 >              bti-thunk=retpoline|lfence|jmp,bhb-seq=short|tsx|long,
 >              {ibrs,ibpb,ssbd,psfd,
->              eager-fpu,l1d-flush,branch-harden,srb-lock,
+>              l1d-flush,branch-harden,srb-lock,
 >              unpriv-mmio,gds-mit,div-scrub,lock-harden,
 >              bhi-dis-s,bp-spec-reduce,ibpb-alt}=<bool> ]`
 
@@ -2574,11 +2574,6 @@ On hardware supporting IBPB (Indirect Branch Prediction Barrier), the `ibpb=`
 option can be used to force (the default) or prevent Xen from issuing branch
 prediction barriers on vcpu context switches.
 
-On all hardware, the `eager-fpu=` option can be used to force or prevent Xen
-from using fully eager FPU context switches.  This is currently implemented as
-a global control.  By default, Xen will choose to use fully eager context
-switches on hardware believed to speculate past #NM exceptions.
-
 On hardware supporting L1D_FLUSH, the `l1d-flush=` option can be used to force
 or prevent Xen from issuing an L1 data cache flush on each VMEntry.
 Irrespective of Xen's setting, the feature is virtualised for HVM guests to
xen/arch/x86/i387.c +1 -1
index b84cd6f7a9e1..954ba3b1799b 100644
@@ -297,7 +297,7 @@ void save_fpu_enable(void)
 /* Initialize FPU's context save area */
 int vcpu_init_fpu(struct vcpu *v)
 {
-    v->arch.fully_eager_fpu = opt_eager_fpu;
+    v->arch.fully_eager_fpu = true;
 
     return xstate_alloc_save_area(v);
 }
xen/arch/x86/include/asm/spec_ctrl.h +0 -1
index 505e3ab863f0..8f82533c416a 100644
@@ -79,7 +79,6 @@ static always_inline void spec_ctrl_new_guest_context(void)
 extern int8_t opt_ibpb_ctxt_switch;
 extern bool opt_ssbd;
 extern int8_t opt_bhi_dis_s;
-extern int8_t opt_eager_fpu;
 extern int8_t opt_l1d_flush;
 
 extern bool bsp_delay_spec_ctrl;
xen/arch/x86/spec_ctrl.c +4 -84

144 lines not on this page · raw message

Signature · 1 line
2.53.0
#2 lore raw
Quoted 2 lines · Ross Lagerwall

Lazy FPU avoids some work during a context switch but pushes the costs elsewhere:

I'd phrase this as "more expensive costs elsewhere".

We're trading off a slightly-longer XRSTOR now, for

1) in PV guests, an #NM exception
2) in HVM guests, an #NM exception and VMExit

and the longer XRSTOR.

Lazy is only a win even in theory if the sum of time handing #NM is less than sum of time doing the longer XRSTOR, and with ...

Quoted 3 lines

* For a workload running some Windows VMs, I measured about 83% of context switches out had used the FPU so most of the time the FPU save/restore is not avoided, just delayed.

... this "No, 83% of the time", falls firmly into "no not a win" category.

Quoted 5 lines

* Setting/clearing the cr0.TS bit is serializing and reportedly slower than the processor optimized xsave/restore.
* Linux uses PKRU so a partial xsave/restore is performed on each context switch anyway, followed by a second xsave/restore at some point during execution.

"This interferes with the 'modified' optimisation that hardware uses to try and reduce the cost of the following XSAVE".

Quoted 5 lines

There is no measurable performance benefit for using lazy FPU and it adds unwanted complexity so remove the option and always use eager-fpu.

I think we want one other bullet point.

A key difference between 32bit and 64bit OSes is that %xmm is in the base featureset for 64bit an thus get ubiquitous use in userspace.  This is likely why we hit 83%.

Otherwise, Reviewed-by: Andrew Cooper <[email protected]>

Quoted 7 lines
docs/misc/hypfs-paths.pandoc +0 -0
index e86f7d0dbef9..1553cb0bcb7f 100644
@@ -108,12 +108,10 @@ A populated Xen hypervisor file system might look like the following example:
             active-hvm/      directory for mitigations active in hvm doamins
                 msr-spec-ctrl "No" or "Yes"

docs/misc/hypfs-paths.pandoc A populated Xen hypervisor file system might look like the following example: · line 117

Quoted 10 lines
docs/misc/hypfs-paths.pandoc +0 -2
                 rsb          "No" or "Yes"
-                eager-fpu    "No" or "Yes"
                 md-clear     "No" or "Yes"
             active-pv/       directory for mitigations active in pv doamins
                 msr-spec-ctrl "No" or "Yes"
                 rsb          "No" or "Yes"
-                eager-fpu    "No" or "Yes"
                 md-clear     "No" or "Yes"
                 xpti         "No" or list of "dom0", "domU", "PCID-on"
                 l1tf-shadow  "No" or list of "dom0", "domU"

Juergen, do we want to nuke this whole paragraph?  I recall that we never took the patch wiring up the speculation controls.

If so, it will be better to split this into a separate patch, rather than to wonder why we've got a hypfs docs change with no associated code change.

~Andrew

#3 lore raw

PATCH 2/8 x86/vmx: Remove lazy FPU support

In reply to Ross Lagerwall

Remove lazy FPU support from the VMX code since fully_eager_fpu is now always true.

No functional change intended.

---
 xen/arch/x86/hvm/vmx/vmcs.c             |  8 +--
 xen/arch/x86/hvm/vmx/vmx.c              | 70 +------------------------
 xen/arch/x86/hvm/vmx/vvmx.c             | 15 +-----
 xen/arch/x86/include/asm/hvm/vmx/vmcs.h |  2 -
 4 files changed, 5 insertions(+), 90 deletions(-)
xen/arch/x86/hvm/vmx/vmcs.c +2 -6
index c2e7f9aed39f..8e52ef4d497a 100644
@@ -1247,10 +1247,7 @@ static int construct_vmcs(struct vcpu *v)
     __vmwrite(HOST_TR_SELECTOR, TSS_SELECTOR);
 
     /* Host control registers. */
-    v->arch.hvm.vmx.host_cr0 = read_cr0() & ~X86_CR0_TS;
-    if ( !v->arch.fully_eager_fpu )
-        v->arch.hvm.vmx.host_cr0 |= X86_CR0_TS;
-    __vmwrite(HOST_CR0, v->arch.hvm.vmx.host_cr0);
+    __vmwrite(HOST_CR0, read_cr0());
     __vmwrite(HOST_CR4, mmu_cr4_features);
     if ( cpu_has_vmx_efer )
         __vmwrite(HOST_EFER, read_efer());
@@ -1330,8 +1327,7 @@ static int construct_vmcs(struct vcpu *v)
     __vmwrite(VMCS_LINK_POINTER, ~0UL);
 
     v->arch.hvm.vmx.exception_bitmap = HVM_TRAP_MASK
-              | (paging_mode_hap(d) ? 0 : (1U << X86_EXC_PF))
-              | (v->arch.fully_eager_fpu ? 0 : (1U << X86_EXC_NM));
+              | (paging_mode_hap(d) ? 0 : (1U << X86_EXC_PF));
 
     if ( cpu_has_vmx_notify_vm_exiting )
         __vmwrite(NOTIFY_WINDOW, vm_notify_window);
xen/arch/x86/hvm/vmx/vmx.c +1 -69

118 lines not on this page · raw message

xen/arch/x86/hvm/vmx/vvmx.c +2 -13
index 38952f06961e..e4cdfe55c18e 100644
@@ -1238,9 +1238,6 @@ static void virtual_vmentry(struct cpu_user_regs *regs)
     regs->rsp = get_vvmcs(v, GUEST_RSP);
     regs->rflags = get_vvmcs(v, GUEST_RFLAGS);
 
-    /* updating host cr0 to sync TS bit */
-    __vmwrite(HOST_CR0, v->arch.hvm.vmx.host_cr0);
-
     /* Setup virtual ETP for L2 guest*/
     if ( nestedhvm_paging_mode_hap(v) )
         /* This will setup the initial np2m for the nested vCPU */
@@ -1468,9 +1465,6 @@ static void virtual_vmexit(struct cpu_user_regs *regs)
     /* VM exit clears all bits except bit 1 */
     regs->rflags = X86_EFLAGS_MBS;
 
-    /* updating host cr0 to sync TS bit */
-    __vmwrite(HOST_CR0, v->arch.hvm.vmx.host_cr0);
-
     if ( cpu_has_vmx_virtual_intr_delivery )
         nvmx_update_apicv(v);
 
@@ -2458,19 +2452,14 @@ int nvmx_n2_vmexit_handler(struct cpu_user_regs *regs,
         __vmread(VM_EXIT_INTR_INFO, &intr_info);
         vector = intr_info & INTR_INFO_VECTOR_MASK;
         /*
-         * decided by L0 and L1 exception bitmap, if the vetor is set by
-         * both, L0 has priority on #PF and #NM, L1 has priority on others
+         * decided by L0 and L1 exception bitmap, if the vector is set by
+         * both, L0 has priority on #PF, L1 has priority on others
          */
         if ( vector == X86_EXC_PF )
         {
             if ( paging_mode_hap(v->domain) )
                 nvcpu->nv_vmexit_pending = 1;
         }
-        else if ( vector == X86_EXC_NM )
-        {
-            if ( v->fpu_dirtied )
-                nvcpu->nv_vmexit_pending = 1;
-        }
         else if ( (intr_info & valid_mask) == valid_mask )
         {
             exec_bitmap = get_vvmcs(v, EXCEPTION_BITMAP);
xen/arch/x86/include/asm/hvm/vmx/vmcs.h +0 -2
index 879ec10cefd0..88bded5190c9 100644
@@ -149,8 +149,6 @@ struct vmx_vcpu {
     DECLARE_BITMAP(eoi_exit_bitmap, X86_IDT_VECTORS);
     struct pi_desc       pi_desc;
 
-    unsigned long        host_cr0;
-
     /* Do we need to tolerate a spurious EPT_MISCONFIG VM exit? */
     bool                 ept_spurious_misconfig;
 
Signature · 1 line
2.53.0
Reviewed-by on v1 2/8
#4 lore raw

Re: [PATCH v1 2/8] x86/vmx: Remove lazy FPU support

Quoted 6 lines · Ross Lagerwall

Remove lazy FPU support from the VMX code since fully_eager_fpu is now always true.

No functional change intended.

Quoted 12 lines
---
 xen/arch/x86/hvm/vmx/vmcs.c             |  8 +--
 xen/arch/x86/hvm/vmx/vmx.c              | 70 +------------------------
 xen/arch/x86/hvm/vmx/vvmx.c             | 15 +-----
 xen/arch/x86/include/asm/hvm/vmx/vmcs.h |  2 -
 4 files changed, 5 insertions(+), 90 deletions(-)
xen/arch/x86/hvm/vmx/vmcs.c +0 -0
index c2e7f9aed39f..8e52ef4d497a 100644

xen/arch/x86/hvm/vmx/vmcs.c static int construct_vmcs(struct vcpu *v) · line 1250

Quoted 8 lines
xen/arch/x86/hvm/vmx/vmcs.c +1 -4
@@ -1247,10 +1247,7 @@ static int construct_vmcs(struct vcpu *v)
     __vmwrite(HOST_TR_SELECTOR, TSS_SELECTOR);
 
     /* Host control registers. */
-    v->arch.hvm.vmx.host_cr0 = read_cr0() & ~X86_CR0_TS;
-    if ( !v->arch.fully_eager_fpu )
-        v->arch.hvm.vmx.host_cr0 |= X86_CR0_TS;
-    __vmwrite(HOST_CR0, v->arch.hvm.vmx.host_cr0);
+    __vmwrite(HOST_CR0, read_cr0());

(Not for this patch) but I'm pretty sure there's room to optimise this further.

CR0 should be constant, both here and in SVM.  Reading the active cr0 is an example of the anti-pattern we need to purge to make nested-virt work better.

~Andrew

#5 lore raw

Re: [PATCH v1 2/8] x86/vmx: Remove lazy FPU support

Quoted 34 lines · Andrew Cooper
Quoted 6 lines · Ross Lagerwall

Remove lazy FPU support from the VMX code since fully_eager_fpu is now always true.

No functional change intended.

Quoted 12 lines
---
 xen/arch/x86/hvm/vmx/vmcs.c             |  8 +--
 xen/arch/x86/hvm/vmx/vmx.c              | 70 +------------------------
 xen/arch/x86/hvm/vmx/vvmx.c             | 15 +-----
 xen/arch/x86/include/asm/hvm/vmx/vmcs.h |  2 -
 4 files changed, 5 insertions(+), 90 deletions(-)
xen/arch/x86/hvm/vmx/vmcs.c +0 -0
index c2e7f9aed39f..8e52ef4d497a 100644

xen/arch/x86/hvm/vmx/vmcs.c static int construct_vmcs(struct vcpu *v) · line 1250

Quoted 8 lines
xen/arch/x86/hvm/vmx/vmcs.c +1 -4
@@ -1247,10 +1247,7 @@ static int construct_vmcs(struct vcpu *v)
     __vmwrite(HOST_TR_SELECTOR, TSS_SELECTOR);
 
     /* Host control registers. */
-    v->arch.hvm.vmx.host_cr0 = read_cr0() & ~X86_CR0_TS;
-    if ( !v->arch.fully_eager_fpu )
-        v->arch.hvm.vmx.host_cr0 |= X86_CR0_TS;
-    __vmwrite(HOST_CR0, v->arch.hvm.vmx.host_cr0);
+    __vmwrite(HOST_CR0, read_cr0());

(Not for this patch) but I'm pretty sure there's room to optimise this further.

Quoted 3 lines

CR0 should be constant, both here and in SVM.  Reading the active cr0 is an example of the anti-pattern we need to purge to make nested-virt work better.

In which case, is it a good idea to purge the host_cr0 field?

Jan

#6 lore raw

Re: [PATCH v1 2/8] x86/vmx: Remove lazy FPU support

Quoted 33 lines · Jan Beulich
Quoted 32 lines · Andrew Cooper
Quoted 6 lines · Ross Lagerwall

Remove lazy FPU support from the VMX code since fully_eager_fpu is now always true.

No functional change intended.

Quoted 12 lines
---
 xen/arch/x86/hvm/vmx/vmcs.c             |  8 +--
 xen/arch/x86/hvm/vmx/vmx.c              | 70 +------------------------
 xen/arch/x86/hvm/vmx/vvmx.c             | 15 +-----
 xen/arch/x86/include/asm/hvm/vmx/vmcs.h |  2 -
 4 files changed, 5 insertions(+), 90 deletions(-)
xen/arch/x86/hvm/vmx/vmcs.c +0 -0
index c2e7f9aed39f..8e52ef4d497a 100644

xen/arch/x86/hvm/vmx/vmcs.c static int construct_vmcs(struct vcpu *v) · line 1250

Quoted 8 lines
xen/arch/x86/hvm/vmx/vmcs.c +1 -4
@@ -1247,10 +1247,7 @@ static int construct_vmcs(struct vcpu *v)
     __vmwrite(HOST_TR_SELECTOR, TSS_SELECTOR);
 
     /* Host control registers. */
-    v->arch.hvm.vmx.host_cr0 = read_cr0() & ~X86_CR0_TS;
-    if ( !v->arch.fully_eager_fpu )
-        v->arch.hvm.vmx.host_cr0 |= X86_CR0_TS;
-    __vmwrite(HOST_CR0, v->arch.hvm.vmx.host_cr0);
+    __vmwrite(HOST_CR0, read_cr0());

(Not for this patch) but I'm pretty sure there's room to optimise this further.

Quoted 4 lines
Quoted 3 lines

CR0 should be constant, both here and in SVM.  Reading the active cr0 is an example of the anti-pattern we need to purge to make nested-virt work better.

In which case, is it a good idea to purge the host_cr0 field?

Oh hmm, I take back my R-by slightly.  We still need to initialise v->arch.hvm.vmx.host_cr0 for this patch to be no functional change. Easy enough to fix, or fix on commit.

That said, I think we probably do want to purge host_cr0 eventually.

There are a few cases where host_cr0 != guest_cr0.  CPUs prior to unrestricted_guest are the obvious case, where we can't run with CR0.PE != 1, but this should always be derivable from guest_cr0 and the hardware capabilities.

~Andrew

#7 lore raw

Re: [PATCH v1 2/8] x86/vmx: Remove lazy FPU support

Quoted 34 lines · Andrew Cooper
Quoted 33 lines · Jan Beulich
Quoted 32 lines · Andrew Cooper
Quoted 6 lines · Ross Lagerwall

Remove lazy FPU support from the VMX code since fully_eager_fpu is now always true.

No functional change intended.

Quoted 12 lines
---
  xen/arch/x86/hvm/vmx/vmcs.c             |  8 +--
  xen/arch/x86/hvm/vmx/vmx.c              | 70 +------------------------
  xen/arch/x86/hvm/vmx/vvmx.c             | 15 +-----
  xen/arch/x86/include/asm/hvm/vmx/vmcs.h |  2 -
  4 files changed, 5 insertions(+), 90 deletions(-)
xen/arch/x86/hvm/vmx/vmcs.c +0 -0
index c2e7f9aed39f..8e52ef4d497a 100644

xen/arch/x86/hvm/vmx/vmcs.c static int construct_vmcs(struct vcpu *v) · line 1250

Quoted 8 lines
xen/arch/x86/hvm/vmx/vmcs.c +1 -4
@@ -1247,10 +1247,7 @@ static int construct_vmcs(struct vcpu *v)
      __vmwrite(HOST_TR_SELECTOR, TSS_SELECTOR);
 
      /* Host control registers. */
-    v->arch.hvm.vmx.host_cr0 = read_cr0() & ~X86_CR0_TS;
-    if ( !v->arch.fully_eager_fpu )
-        v->arch.hvm.vmx.host_cr0 |= X86_CR0_TS;
-    __vmwrite(HOST_CR0, v->arch.hvm.vmx.host_cr0);
+    __vmwrite(HOST_CR0, read_cr0());

(Not for this patch) but I'm pretty sure there's room to optimise this further.

Quoted 9 lines
Quoted 4 lines
Quoted 3 lines

CR0 should be constant, both here and in SVM. Reading the active cr0 is an example of the anti-pattern we need to purge to make nested-virt work better.

In which case, is it a good idea to purge the host_cr0 field?

Oh hmm, I take back my R-by slightly. We still need to initialise v->arch.hvm.vmx.host_cr0 for this patch to be no functional change. Easy enough to fix, or fix on commit.

Not sure I follow why v->arch.hvm.vmx.host_cr0 is needed when this would be the only place it is used?

For no function change, it should be probably be...

__vmwrite(HOST_CR0, read_cr0() & ~X86_CR0_TS);

... though that might be unnecessary after patch 8.

Ross

#8 lore raw

Re: [PATCH v1 2/8] x86/vmx: Remove lazy FPU support

Quoted 44 lines · Ross Lagerwall
Quoted 43 lines · Andrew Cooper
Quoted 41 lines · Jan Beulich
Quoted 34 lines · Andrew Cooper
Quoted 7 lines · Ross Lagerwall

Remove lazy FPU support from the VMX code since fully_eager_fpu is now
always true.

No functional change intended.

Quoted 14 lines

---
  xen/arch/x86/hvm/vmx/vmcs.c             |  8 +-- xen/arch/x86/hvm/vmx/vmx.c              | 70

+------------------------
  xen/arch/x86/hvm/vmx/vvmx.c             | 15 +-----
  xen/arch/x86/include/asm/hvm/vmx/vmcs.h |  2 -
  4 files changed, 5 insertions(+), 90 deletions(-)
xen/arch/x86/hvm/vmx/vmcs.c +0 -0
index c2e7f9aed39f..8e52ef4d497a 100644

xen/arch/x86/hvm/vmx/vmcs.c static int construct_vmcs(struct vcpu *v) · line 1249

Quoted 7 lines
xen/arch/x86/hvm/vmx/vmcs.c +1 -4
@@ -1247,10 +1247,7 @@ static int construct_vmcs(struct vcpu *v)
       __vmwrite(HOST_TR_SELECTOR, TSS_SELECTOR);
         /* Host control registers. */
-    v->arch.hvm.vmx.host_cr0 = read_cr0() & ~X86_CR0_TS;
-    if ( !v->arch.fully_eager_fpu )
-        v->arch.hvm.vmx.host_cr0 |= X86_CR0_TS;
-    __vmwrite(HOST_CR0, v->arch.hvm.vmx.host_cr0);
+    __vmwrite(HOST_CR0, read_cr0());

(Not for this patch) but I'm pretty sure there's room to optimise this further.

Quoted 5 lines

CR0 should be constant, both here and in SVM.  Reading the active cr0 is
an example of the anti-pattern we need to purge to make nested-virt work
better.

In which case, is it a good idea to purge the host_cr0 field?

Quoted 7 lines
Quoted 4 lines

Oh hmm, I take back my R-by slightly.  We still need to initialise v->arch.hvm.vmx.host_cr0 for this patch to be no functional change. Easy enough to fix, or fix on commit.

Not sure I follow why v->arch.hvm.vmx.host_cr0 is needed when this would be the only place it is used?

Never mind.  I'd mixed deleting this variable.  My original R-by stands.

~Andrew

#9 lore raw

Re: [PATCH v1 2/8] x86/vmx: Remove lazy FPU support

In reply to Ross Lagerwall

xen/arch/x86/hvm/vmx/vvmx.c int nvmx_n2_vmexit_handler(struct cpu_user_regs *regs, · line 2457

Quoted 13 lines · Ross Lagerwall
xen/arch/x86/hvm/vmx/vvmx.c +2 -2
index 38952f06961e..e4cdfe55c18e 100644
@@ -2458,19 +2452,14 @@ int nvmx_n2_vmexit_handler(struct cpu_user_regs *regs,
         __vmread(VM_EXIT_INTR_INFO, &intr_info);
         vector = intr_info & INTR_INFO_VECTOR_MASK;
         /*
-         * decided by L0 and L1 exception bitmap, if the vetor is set by
-         * both, L0 has priority on #PF and #NM, L1 has priority on others
+         * decided by L0 and L1 exception bitmap, if the vector is set by
+         * both, L0 has priority on #PF, L1 has priority on others
          */

Again not for this patch, but this comment is wrong(ish).

L0 #PF only needs to take priority when L1 is a shadow mode guest.  A while back we decided to disallow nested-virt in such a configuration, so we can delete this special case.

~Andrew

#10 lore raw

PATCH 3/8 x86/svm: Remove lazy FPU support

In reply to Ross Lagerwall

Remove lazy FPU support from the SVM code since fully_eager_fpu is now always true.

No functional change intended.

---
 xen/arch/x86/hvm/svm/nestedsvm.c         | 67 +-------------------
 xen/arch/x86/hvm/svm/svm.c               | 81 +-----------------------
 xen/arch/x86/hvm/svm/vmcb.c              |  4 +-
 xen/arch/x86/include/asm/hvm/svm-types.h |  6 --
 4 files changed, 4 insertions(+), 154 deletions(-)
xen/arch/x86/hvm/svm/nestedsvm.c +1 -66
index a63ec613465f..ef6fa5d23b67 100644
@@ -165,58 +165,6 @@ int cf_check nsvm_vcpu_reset(struct vcpu *v)
     return 0;
 }
 
-static uint64_t nestedsvm_fpu_vmentry(uint64_t n1cr0,
-    struct vmcb_struct *vvmcb,
-    struct vmcb_struct *n1vmcb, struct vmcb_struct *n2vmcb)
-{
-    uint64_t vcr0;
-
-    vcr0 = vvmcb->_cr0;
-    if ( !(n1cr0 & X86_CR0_TS) && (n1vmcb->_cr0 & X86_CR0_TS) )
-    {
-        /*
-         * svm_fpu_leave() run while l1 guest was running.
-         * Sync FPU state with l2 guest.
-         */
-        vcr0 |= X86_CR0_TS;
-        n2vmcb->_exception_intercepts |= (1U << X86_EXC_NM);
-    }
-    else if ( !(vcr0 & X86_CR0_TS) && (n2vmcb->_cr0 & X86_CR0_TS) )
-    {
-        /*
-         * svm_fpu_enter() run while l1 guest was running.
-         * Sync FPU state with l2 guest.
-         */
-        vcr0 &= ~X86_CR0_TS;
-        n2vmcb->_exception_intercepts &= ~(1U << X86_EXC_NM);
-    }
-
-    return vcr0;
-}
-
-static void nestedsvm_fpu_vmexit(struct vmcb_struct *n1vmcb,
-    struct vmcb_struct *n2vmcb, uint64_t n1cr0, uint64_t guest_cr0)
-{
-    if ( !(guest_cr0 & X86_CR0_TS) && (n2vmcb->_cr0 & X86_CR0_TS) )
-    {
-        /*
-         * svm_fpu_leave() run while l2 guest was running.
-         * Sync FPU state with l1 guest.
-         */
-        n1vmcb->_cr0 |= X86_CR0_TS;
-        n1vmcb->_exception_intercepts |= (1U << X86_EXC_NM);
-    }
-    else if ( !(n1cr0 & X86_CR0_TS) && (n1vmcb->_cr0 & X86_CR0_TS) )
-    {
-        /*
-         * svm_fpu_enter() run while l2 guest was running.
-         * Sync FPU state with l1 guest.
-         */
-        n1vmcb->_cr0 &= ~X86_CR0_TS;
-        n1vmcb->_exception_intercepts &= ~(1U << X86_EXC_NM);
-    }
-}
-
 static int nsvm_vcpu_hostsave(struct vcpu *v, unsigned int inst_len)
 {
     struct nestedsvm *svm = &vcpu_nestedsvm(v);
@@ -246,7 +194,6 @@ static int nsvm_vcpu_hostsave(struct vcpu *v, unsigned int inst_len)
 static int nsvm_vcpu_hostrestore(struct vcpu *v, struct cpu_user_regs *regs)
 {
     struct nestedvcpu *nv = &vcpu_nestedhvm(v);
-    struct nestedsvm *svm = &vcpu_nestedsvm(v);
     struct vmcb_struct *n1vmcb, *n2vmcb;
     int rc;
 
@@ -281,8 +228,6 @@ static int nsvm_vcpu_hostrestore(struct vcpu *v, struct cpu_user_regs *regs)
         gdprintk(XENLOG_ERR, "hvm_set_cr4 failed, rc: %u\n", rc);
 
     /* CR0 */
-    nestedsvm_fpu_vmexit(n1vmcb, n2vmcb,
-        svm->ns_cr0, v->arch.hvm.guest_cr[0]);
     v->arch.hvm.guest_cr[0] = n1vmcb->_cr0 | X86_CR0_PE;
     n1vmcb->rflags &= ~X86_EFLAGS_VM;
     rc = hvm_set_cr0(n1vmcb->_cr0 | X86_CR0_PE, true);
@@ -290,7 +235,6 @@ static int nsvm_vcpu_hostrestore(struct vcpu *v, struct cpu_user_regs *regs)
         hvm_inject_hw_exception(X86_EXC_GP, 0);
     if ( rc != X86EMUL_OKAY )
         gdprintk(XENLOG_ERR, "hvm_set_cr0 failed, rc: %u\n", rc);
-    svm->ns_cr0 = v->arch.hvm.guest_cr[0];
 
     /* CR2 */
     v->arch.hvm.guest_cr[2] = n1vmcb->_cr2;
@@ -418,7 +362,6 @@ static int nsvm_vmcb_prepare4vmrun(struct vcpu *v, struct cpu_user_regs *regs)
     struct vmcb_struct *ns_vmcb, *n1vmcb, *n2vmcb;
     vmcbcleanbits_t clean = {};
     int rc;
-    uint64_t cr0;
 
     ns_vmcb = nv->nv_vvmcx;
     n1vmcb = nv->nv_n1vmcx;
@@ -452,7 +395,6 @@ static int nsvm_vmcb_prepare4vmrun(struct vcpu *v, struct cpu_user_regs *regs)
      *   safed here.
      * The overhead comes from (ordered from highest to lowest):
      * - svm_ctxt_switch_to (CPU context switching)
-     * - svm_fpu_enter, svm_fpu_leave (lazy FPU switching)
      * - emulated CLGI (clears VINTR intercept)
      * - host clears VINTR intercept
      * Test results show that the overhead is high enough that the
@@ -551,10 +493,8 @@ static int nsvm_vmcb_prepare4vmrun(struct vcpu *v, struct cpu_user_regs *regs)
         gdprintk(XENLOG_ERR, "hvm_set_cr4 failed, rc: %u\n", rc);
 
     /* CR0 */
-    svm->ns_cr0 = v->arch.hvm.guest_cr[0];
-    cr0 = nestedsvm_fpu_vmentry(svm->ns_cr0, ns_vmcb, n1vmcb, n2vmcb);
     v->arch.hvm.guest_cr[0] = ns_vmcb->_cr0;
-    rc = hvm_set_cr0(cr0, true);
+    rc = hvm_set_cr0(ns_vmcb->_cr0, true);
     if ( rc == X86EMUL_EXCEPTION )
         hvm_inject_hw_exception(X86_EXC_GP, 0);
     if ( rc != X86EMUL_OKAY )
@@ -1305,11 +1245,6 @@ nestedsvm_check_intercepts(struct vcpu *v, struct cpu_user_regs *regs,
     case VMEXIT_INTR:
     case VMEXIT_NMI:
         return NESTEDHVM_VMEXIT_HOST;
-    case VMEXIT_EXCEPTION_NM:
-        /* Host must handle lazy fpu context switching first.
-         * Then inject the VMEXIT if L1 guest intercepts this.
-         */
-        return NESTEDHVM_VMEXIT_HOST;
 
     case VMEXIT_NPF:
         if ( nestedhvm_paging_mode_hap(v) )
xen/arch/x86/hvm/svm/svm.c +2 -79

133 lines not on this page · raw message

xen/arch/x86/hvm/svm/vmcb.c +1 -3
index e583ef8548c7..5ed7123d9a69 100644
@@ -138,9 +138,7 @@ static int construct_vmcb(struct vcpu *v)
 
     paging_update_paging_modes(v);
 
-    vmcb->_exception_intercepts =
-        HVM_TRAP_MASK |
-        (v->arch.fully_eager_fpu ? 0 : (1U << X86_EXC_NM));
+    vmcb->_exception_intercepts = HVM_TRAP_MASK;
 
     if ( paging_mode_hap(v->domain) )
     {
xen/arch/x86/include/asm/hvm/svm-types.h +0 -6
index 051b235d8f69..3ede62cade80 100644
@@ -65,12 +65,6 @@ struct nestedsvm {
     /* Shadow io permission map */
     unsigned long *ns_iomap;
 
-     /*
-      * Cached guest_cr[0] of l1 guest while l2 guest runs.  Needed to handle
-      * FPU context switching.
-      */
-    uint64_t ns_cr0;
-
     /*
      * Cache guest cr3/host cr3 the guest sets up for the l2 guest.
      * Used by Shadow-on-Shadow and Nested-on-Nested.
Signature · 1 line
2.53.0
Reviewed-by on v1 3/8
#11 lore raw

Re: [PATCH v1 3/8] x86/svm: Remove lazy FPU support

Quoted 6 lines · Ross Lagerwall

Remove lazy FPU support from the SVM code since fully_eager_fpu is now always true.

No functional change intended.

Unagling nestedsvm_fpu_vmentry() is tricky to follow but I've managed to convince myself that it's correct.

xen/arch/x86/hvm/svm/vmcb.c static int construct_vmcb(struct vcpu *v) · line 141

Quoted 12 lines
xen/arch/x86/hvm/svm/vmcb.c +1 -3
index e583ef8548c7..5ed7123d9a69 100644
@@ -138,9 +138,7 @@ static int construct_vmcb(struct vcpu *v)
 
     paging_update_paging_modes(v);
 
-    vmcb->_exception_intercepts =
-        HVM_TRAP_MASK |
-        (v->arch.fully_eager_fpu ? 0 : (1U << X86_EXC_NM));
+    vmcb->_exception_intercepts = HVM_TRAP_MASK;

Just an observation.  This handles HAP vs Shadow differently to the Intel side.  I expect we might want to bring them back in line in due course.

~Andrew

#12 lore raw

PATCH 4/8 x86/traps: Remove lazy FPU support

In reply to Ross Lagerwall

From: Wei Liu <[email protected]>

Remove lazy FPU support from the #DNA exception handler used by PV guests since fully_eager_fpu is now always true.

---
 xen/arch/x86/i387.c               | 24 ------------------------
 xen/arch/x86/include/asm/i387.h   |  1 -
 xen/arch/x86/pv/misc-hypercalls.c |  3 +--
 xen/arch/x86/traps.c              | 18 ++++++++++--------
 4 files changed, 11 insertions(+), 35 deletions(-)
xen/arch/x86/i387.c +0 -24
index 954ba3b1799b..7da731865f73 100644
@@ -234,30 +234,6 @@ void vcpu_restore_fpu_nonlazy(struct vcpu *v, bool need_stts)
         stts();
 }
 
-/* 
- * Restore FPU state when #NM is triggered.
- */
-void vcpu_restore_fpu_lazy(struct vcpu *v)
-{
-    ASSERT(!is_idle_vcpu(v));
-
-    /* Avoid recursion. */
-    clts();
-
-    if ( v->fpu_dirtied )
-        return;
-
-    ASSERT(!v->arch.fully_eager_fpu);
-
-    if ( cpu_has_xsave )
-        fpu_xrstor(v, XSTATE_LAZY);
-    else
-        fpu_fxrstor(v);
-
-    v->fpu_initialised = 1;
-    v->fpu_dirtied = 1;
-}
-
 /* 
  * On each context switch, save the necessary FPU info of VCPU being switch 
  * out. It dispatches saving operation based on CPU's capability.
xen/arch/x86/include/asm/i387.h +0 -1
index 652d7ad2deb6..da0c7e945f95 100644
@@ -28,7 +28,6 @@ struct ix87_env {
 };
 
 void vcpu_restore_fpu_nonlazy(struct vcpu *v, bool need_stts);
-void vcpu_restore_fpu_lazy(struct vcpu *v);
 void vcpu_save_fpu(struct vcpu *v);
 void save_fpu_enable(void);
 int vcpu_init_fpu(struct vcpu *v);
xen/arch/x86/pv/misc-hypercalls.c +1 -2
index 7e915d86b724..34a0717540a9 100644
@@ -42,8 +42,7 @@ long do_fpu_taskswitch(int set)
     else
     {
         v->arch.pv.ctrlreg[0] &= ~X86_CR0_TS;
-        if ( v->fpu_dirtied )
-            clts();
+        clts();
     }
 
     return 0;
xen/arch/x86/traps.c +10 -8
index b6b119769722..fb1b94245850 100644
@@ -2045,15 +2045,17 @@ void asmlinkage do_device_not_available(struct cpu_user_regs *regs)
     }
 
 #ifdef CONFIG_PV
-    vcpu_restore_fpu_lazy(curr);
+    BUG_ON(!(curr->arch.pv.ctrlreg[0] & X86_CR0_TS));
 
-    if ( curr->arch.pv.ctrlreg[0] & X86_CR0_TS )
-    {
-        pv_inject_hw_exception(X86_EXC_NM, X86_EVENT_NO_EC);
-        curr->arch.pv.ctrlreg[0] &= ~X86_CR0_TS;
-    }
-    else
-        TRACE_TIME(TRC_PV_MATH_STATE_RESTORE);
+    /*
+     * PV ABI QUIRK: Classic Xen kernels (2.6.18 and SLES 11 SP4's
+     * 3.0) rely on Xen to clear TS. PVOPS kernels (3.0, 3.16 and 4.15
+     * are checked) always clear TS themselves.
+     */
+    clts();
+
+    pv_inject_hw_exception(X86_EXC_NM, X86_EVENT_NO_EC);
+    curr->arch.pv.ctrlreg[0] &= ~X86_CR0_TS;
 #else
     ASSERT_UNREACHABLE();
 #endif
Signature · 1 line
2.53.0
#13 lore raw

Re: [PATCH v1 4/8] x86/traps: Remove lazy FPU support

Quoted 3 lines · Ross Lagerwall

From: Wei Liu <[email protected]>

Remove lazy FPU support from the #DNA exception handler used by PV

It's the #NM exception handler.

The short name is "No Math[sic]", despite the long name being "Device Not Available".

Quoted 4 lines

guests since fully_eager_fpu is now always true.

These two tags need reversing.  Technically this says that Wei reviewed/accepted the part of the patch that was your changes.

xen/arch/x86/traps.c void asmlinkage do_device_not_available(struct cpu_user_regs *regs) · line 2048

Quoted 10 lines
xen/arch/x86/traps.c +1 -1
index b6b119769722..fb1b94245850 100644
@@ -2045,15 +2045,17 @@ void asmlinkage do_device_not_available(struct cpu_user_regs *regs)
     }
 
 #ifdef CONFIG_PV
-    vcpu_restore_fpu_lazy(curr);
+    BUG_ON(!(curr->arch.pv.ctrlreg[0] & X86_CR0_TS));

I'm not sure if this is safe.

Firstly, since Wei wrote the patch originally, a new source of #NM exceptions has come into existence.  AMX shouldn't be enabled for VMs yet, but this ought to be

    if ( !... )
    {
        ASSERT_UNREACHABLE();
        domain_crash(...);
    }

to be less fatal.

Also, at this point in the series, cpu_init() still sets TS, as does vcpu_save_fpu().  It's far from clear that TS is only set when the guest wants it, although we have at least excluded the Xen paths by this point in the handler.

xen/arch/x86/traps.c void asmlinkage do_device_not_available(struct cpu_user_regs *regs) · line 2055

Quoted 13 lines
xen/arch/x86/traps.c +6 -7
-    if ( curr->arch.pv.ctrlreg[0] & X86_CR0_TS )
-    {
-        pv_inject_hw_exception(X86_EXC_NM, X86_EVENT_NO_EC);
-        curr->arch.pv.ctrlreg[0] &= ~X86_CR0_TS;
-    }
-    else
-        TRACE_TIME(TRC_PV_MATH_STATE_RESTORE);
+    /*
+     * PV ABI QUIRK: Classic Xen kernels (2.6.18 and SLES 11 SP4's
+     * 3.0) rely on Xen to clear TS. PVOPS kernels (3.0, 3.16 and 4.15
+     * are checked) always clear TS themselves.
+     */
+    clts();

I think this wants wording differently.

"For better or worse, Xen's ABI with PV guests always clears TS on an #NM exception.  Classic-xen Linux depends on this."

The behaviour of obsolete PVOps kernels isn't relevant to the ABI, and now that Linux is strictly eager too, I doubt this statement is accurate any more.

xen/arch/x86/traps.c void asmlinkage do_device_not_available(struct cpu_user_regs *regs) · line 2058

Quoted 3 lines
xen/arch/x86/traps.c +3 -0
+
+    pv_inject_hw_exception(X86_EXC_NM, X86_EVENT_NO_EC);
+    curr->arch.pv.ctrlreg[0] &= ~X86_CR0_TS;

Swap these two operations.  The optimiser will then be able to tailcall pv_inject_hw_exception().

Clearing the guest TS ought to be tied to the clts() operation anyway.

~Andrew

#14 lore raw

PATCH 5/8 x86: Remove fully_eager_fpu

In reply to Ross Lagerwall

Since fully_eager_fpu is always true, remove it and adjust the code accordingly. At the same time, rename vcpu_restore_fpu_nonlazy since it always does the full restore.

No functional change intended.

---
 xen/arch/x86/domain.c              |  2 +-
 xen/arch/x86/hvm/emulate.c         | 18 +--------------
 xen/arch/x86/i387.c                | 35 ++++++++----------------------
 xen/arch/x86/include/asm/domain.h  |  3 ---
 xen/arch/x86/include/asm/hvm/hvm.h |  2 --
 xen/arch/x86/include/asm/i387.h    |  2 +-
 xen/common/efi/runtime.c           |  2 +-
 7 files changed, 13 insertions(+), 51 deletions(-)
xen/arch/x86/domain.c +1 -1
index 4664264b2f5d..a68b7a583294 100644
@@ -2125,7 +2125,7 @@ static void __context_switch(void)
             if ( cpu_has_xsaves && is_hvm_vcpu(n) )
                 set_msr_xss(n->arch.msrs->xss.raw);
         }
-        vcpu_restore_fpu_nonlazy(n, false);
+        vcpu_restore_fpu(n);
         nd->arch.ctxt_switch->to(n);
     }
 
xen/arch/x86/hvm/emulate.c +1 -17
index 86b1f7535668..2daea084f15c 100644
@@ -2630,23 +2630,7 @@ static void cf_check hvmemul_put_fpu(
     }
 
     if ( backout == X86EMUL_FPU_fpu )
-    {
-        /*
-         * To back out changes to the register file
-         * - in fully eager mode, restore original state immediately,
-         * - in lazy mode, simply adjust state such that upon next FPU insn
-         *   use by the guest we'll reload the state saved (or freshly loaded)
-         *   by hvmemul_get_fpu().
-         */
-        if ( curr->arch.fully_eager_fpu )
-            vcpu_restore_fpu_nonlazy(curr, false);
-        else
-        {
-            curr->fpu_dirtied = false;
-            stts();
-            alternative_vcall(hvm_funcs.fpu_leave, curr);
-        }
-    }
+        vcpu_restore_fpu(curr);
 }
 
 static int cf_check hvmemul_tlb_op(
xen/arch/x86/i387.c +9 -26
index 7da731865f73..88018397b1ad 100644
@@ -194,12 +194,8 @@ static inline void fpu_fxsave(struct vcpu *v)
 /*       VCPU FPU Functions    */
 /*******************************/
 /* Restore FPU state whenever VCPU is schduled in. */
-void vcpu_restore_fpu_nonlazy(struct vcpu *v, bool need_stts)
+void vcpu_restore_fpu(struct vcpu *v)
 {
-    /* Restore nonlazy extended state (i.e. parts not tracked by CR0.TS). */
-    if ( !v->arch.fully_eager_fpu && !v->arch.nonlazy_xstate_used )
-        goto maybe_stts;
-
     ASSERT(!is_idle_vcpu(v));
 
     /* Avoid recursion */
@@ -210,27 +206,16 @@ void vcpu_restore_fpu_nonlazy(struct vcpu *v, bool need_stts)
      * above) we also need to restore full state, to prevent subsequently
      * saving state belonging to another vCPU.
      */
-    if ( v->arch.fully_eager_fpu || xstate_all(v) )
-    {
-        if ( cpu_has_xsave )
-            fpu_xrstor(v, XSTATE_ALL);
-        else
-            fpu_fxrstor(v);
-
-        v->fpu_initialised = 1;
-        v->fpu_dirtied = 1;
-
-        /* Xen doesn't need TS set, but the guest might. */
-        need_stts = is_pv_vcpu(v) && (v->arch.pv.ctrlreg[0] & X86_CR0_TS);
-    }
+    if ( cpu_has_xsave )
+        fpu_xrstor(v, XSTATE_ALL);
     else
-    {
-        fpu_xrstor(v, XSTATE_NONLAZY);
-        need_stts = true;
-    }
+        fpu_fxrstor(v);
 
- maybe_stts:
-    if ( need_stts )
+    v->fpu_initialised = 1;
+    v->fpu_dirtied = 1;
+
+    /* Xen doesn't need TS set, but the guest might. */
+    if ( is_pv_vcpu(v) && (v->arch.pv.ctrlreg[0] & X86_CR0_TS) )
         stts();
 }
 
@@ -273,8 +258,6 @@ void save_fpu_enable(void)
 /* Initialize FPU's context save area */
 int vcpu_init_fpu(struct vcpu *v)
 {
-    v->arch.fully_eager_fpu = true;
-
     return xstate_alloc_save_area(v);
 }
 
xen/arch/x86/include/asm/domain.h +0 -3
index ad7f6adb2cb9..bd7b02085ef8 100644
@@ -671,9 +671,6 @@ struct arch_vcpu
      * and thus should be saved/restored. */
     bool nonlazy_xstate_used;
 
-    /* Restore all FPU state (lazy and non-lazy state) on context switch? */
-    bool fully_eager_fpu;
-
     struct vmce vmce;
 
     struct paging_vcpu paging;
xen/arch/x86/include/asm/hvm/hvm.h +0 -2
index 7d9774df59fe..a9425c8cffe8 100644
@@ -160,8 +160,6 @@ struct hvm_function_table {
 
     void (*cpuid_policy_changed)(struct vcpu *v);
 
-    void (*fpu_leave)(struct vcpu *v);
-
     int  (*get_guest_pat)(struct vcpu *v, uint64_t *gpat);
     int  (*set_guest_pat)(struct vcpu *v, uint64_t gpat);
 
xen/arch/x86/include/asm/i387.h +1 -1
index da0c7e945f95..fe5e4419b6f4 100644
@@ -27,7 +27,7 @@ struct ix87_env {
     uint16_t fds, _res6;
 };
 
-void vcpu_restore_fpu_nonlazy(struct vcpu *v, bool need_stts);
+void vcpu_restore_fpu(struct vcpu *v);
 void vcpu_save_fpu(struct vcpu *v);
 void save_fpu_enable(void);
 int vcpu_init_fpu(struct vcpu *v);
xen/common/efi/runtime.c +1 -1
index ba27c62132d2..982e42e8f341 100644
@@ -157,7 +157,7 @@ void efi_rs_leave(struct efi_rs_state *state)
     irq_exit();
     efi_rs_on_cpu = NR_CPUS;
     spin_unlock(&efi_rs_lock);
-    vcpu_restore_fpu_nonlazy(curr, true);
+    vcpu_restore_fpu(curr);
 }
 
 bool efi_rs_using_pgtables(void)
Signature · 1 line
2.53.0
#16 lore raw

PATCH 6/8 x86: Remove fpu_initialised/fpu_dirty

In reply to Ross Lagerwall

With lazy FPU removed, fpu_initialised and fpu_dirty are always set to true in vcpu_restore_fpu() so remove them and adjust the code accordingly.

No functional change intended.

---
 xen/arch/x86/domctl.c              |  3 +--
 xen/arch/x86/hvm/emulate.c         |  6 +-----
 xen/arch/x86/hvm/hvm.c             | 15 ++++++---------
 xen/arch/x86/hvm/vlapic.c          |  3 ---
 xen/arch/x86/i387.c                | 31 ++----------------------------
 xen/arch/x86/include/asm/hvm/hvm.h |  1 -
 xen/arch/x86/include/asm/xstate.h  | 11 -----------
 xen/arch/x86/xstate.c              | 21 +++++---------------
 xen/common/domain.c                |  2 --
 xen/include/xen/sched.h            |  4 ----
 10 files changed, 15 insertions(+), 82 deletions(-)
xen/arch/x86/domctl.c +1 -2
index 942f41c584d4..d9b08182ac1d 100644
@@ -1409,8 +1409,7 @@ void arch_get_info_guest(struct vcpu *v, vcpu_guest_context_u c)
         c(flags = v->arch.pv.vgc_flags & ~(VGCF_i387_valid|VGCF_in_kernel));
     else
         c(flags = 0);
-    if ( v->fpu_initialised )
-        c(flags |= VGCF_i387_valid);
+    c(flags |= VGCF_i387_valid);
     if ( !(v->pause_flags & VPF_down) )
         c(flags |= VGCF_online);
     if ( !compat )
xen/arch/x86/hvm/emulate.c +1 -5
index 2daea084f15c..48c7320360c7 100644
@@ -2517,9 +2517,7 @@ static int cf_check hvmemul_get_fpu(
 {
     struct vcpu *curr = current;
 
-    if ( !curr->fpu_dirtied )
-        alternative_vcall(hvm_funcs.fpu_dirty_intercept);
-    else if ( type == X86EMUL_FPU_fpu )
+    if ( type == X86EMUL_FPU_fpu )
     {
         /* Has a fastpath for `current`, so there's no actual map */
         const struct xsave_struct *xsave_area = VCPU_MAP_XSAVE_AREA(curr);
@@ -2537,8 +2535,6 @@ static int cf_check hvmemul_get_fpu(
          * masking of all exceptions by FNSTENV.)
          */
         save_fpu_enable();
-        curr->fpu_initialised = true;
-        curr->fpu_dirtied = true;
         if ( (fpu_ctxt->fcw & 0x3f) != 0x3f )
         {
             uint16_t fcw;
xen/arch/x86/hvm/hvm.c +6 -9
index 4d37a93c57a5..d4ba82845146 100644
@@ -866,6 +866,7 @@ static int cf_check hvm_save_cpu_ctxt(struct vcpu *v, hvm_domain_context_t *h)
         .dr7 = v->arch.dr7,
         .msr_efer = v->arch.hvm.guest_efer,
     };
+    const struct xsave_struct *xsave_area;
 
     /*
      * We don't need to save state for a vcpu that is down; the restore
@@ -933,15 +934,11 @@ static int cf_check hvm_save_cpu_ctxt(struct vcpu *v, hvm_domain_context_t *h)
     ctxt.ldtr_base = seg.base;
     ctxt.ldtr_arbytes = seg.attr;
 
-    if ( v->fpu_initialised )
-    {
-        const struct xsave_struct *xsave_area = VCPU_MAP_XSAVE_AREA(v);
-
-        BUILD_BUG_ON(sizeof(ctxt.fpu_regs) != sizeof(xsave_area->fpu_sse));
-        memcpy(ctxt.fpu_regs, &xsave_area->fpu_sse, sizeof(ctxt.fpu_regs));
-        VCPU_UNMAP_XSAVE_AREA(v, xsave_area);
-        ctxt.flags = XEN_X86_FPU_INITIALISED;
-    }
+    xsave_area = VCPU_MAP_XSAVE_AREA(v);
+    BUILD_BUG_ON(sizeof(ctxt.fpu_regs) != sizeof(xsave_area->fpu_sse));
+    memcpy(ctxt.fpu_regs, &xsave_area->fpu_sse, sizeof(ctxt.fpu_regs));
+    VCPU_UNMAP_XSAVE_AREA(v, xsave_area);
+    ctxt.flags = XEN_X86_FPU_INITIALISED;
 
     return hvm_save_entry(CPU, v->vcpu_id, h, &ctxt);
 }
xen/arch/x86/hvm/vlapic.c +0 -3
index 79697487ba90..885f5d304b2f 100644
@@ -276,7 +276,6 @@ static void vlapic_init_sipi_one(struct vcpu *target, uint32_t icr)
     switch ( icr & APIC_DM_MASK )
     {
     case APIC_DM_INIT: {
-        bool fpu_initialised;
         int rc;
 
         /* No work on INIT de-assert for P4-type APIC. */
@@ -289,10 +288,8 @@ static void vlapic_init_sipi_one(struct vcpu *target, uint32_t icr)
         hvm_vcpu_down(target);
         domain_lock(target->domain);
         /* Reset necessary VCPU state. This does not include FPU state. */
-        fpu_initialised = target->fpu_initialised;
         rc = vcpu_reset(target);
         ASSERT(!rc);
-        target->fpu_initialised = fpu_initialised;
         vlapic_do_init(vcpu_vlapic(target));
         domain_unlock(target->domain);
         break;
xen/arch/x86/i387.c +2 -29
index 88018397b1ad..5e893a2aab94 100644
@@ -110,22 +110,7 @@ static inline void fpu_fxrstor(struct vcpu *v)
 
 static inline uint64_t vcpu_xsave_mask(const struct vcpu *v)
 {
-    if ( v->fpu_dirtied )
-        return v->arch.nonlazy_xstate_used ? XSTATE_ALL : XSTATE_LAZY;
-
-    ASSERT(v->arch.nonlazy_xstate_used);
-
-    /*
-     * The offsets of components which live in the extended region of
-     * compact xsave area are not fixed. Xsave area may be overwritten
-     * when a xsave with v->fpu_dirtied set is followed by one with
-     * v->fpu_dirtied clear.
-     * In such case, if hypervisor uses compact xsave area and guest
-     * has ever used lazy states (checking xcr0_accum excluding
-     * XSTATE_FP_SSE), vcpu_xsave_mask will return XSTATE_ALL. Otherwise
-     * return XSTATE_NONLAZY.
-     */
-    return xstate_all(v) ? XSTATE_ALL : XSTATE_NONLAZY;
+    return v->arch.nonlazy_xstate_used ? XSTATE_ALL : XSTATE_LAZY;
 }
 
 /* Save x87 extended state */
@@ -201,19 +186,11 @@ void vcpu_restore_fpu(struct vcpu *v)
     /* Avoid recursion */
     clts();
 
-    /*
-     * When saving full state even with !v->fpu_dirtied (see vcpu_xsave_mask()
-     * above) we also need to restore full state, to prevent subsequently
-     * saving state belonging to another vCPU.
-     */
     if ( cpu_has_xsave )
         fpu_xrstor(v, XSTATE_ALL);
     else
         fpu_fxrstor(v);
 
-    v->fpu_initialised = 1;
-    v->fpu_dirtied = 1;
-
     /* Xen doesn't need TS set, but the guest might. */
     if ( is_pv_vcpu(v) && (v->arch.pv.ctrlreg[0] & X86_CR0_TS) )
         stts();
@@ -225,7 +202,7 @@ void vcpu_restore_fpu(struct vcpu *v)
  */
 static bool _vcpu_save_fpu(struct vcpu *v)
 {
-    if ( !v->fpu_dirtied && !v->arch.nonlazy_xstate_used )
+    if ( !v->arch.nonlazy_xstate_used )
         return false;
 
     ASSERT(!is_idle_vcpu(v));
@@ -238,8 +215,6 @@ static bool _vcpu_save_fpu(struct vcpu *v)
     else
         fpu_fxsave(v);
 
-    v->fpu_dirtied = 0;
-
     return true;
 }
 
@@ -265,7 +240,6 @@ void vcpu_reset_fpu(struct vcpu *v)
 {
     struct xsave_struct *xsave_area = VCPU_MAP_XSAVE_AREA(v);
 
-    v->fpu_initialised = false;
     *xsave_area = (struct xsave_struct) {
         .xsave_hdr.xstate_bv = X86_XCR0_X87,
     };
@@ -282,7 +256,6 @@ void vcpu_setup_fpu(struct vcpu *v, const void *data)
 {
     struct xsave_struct *xsave_area = VCPU_MAP_XSAVE_AREA(v);
 
-    v->fpu_initialised = true;
     *xsave_area = (struct xsave_struct) {
         .fpu_sse = *(const fpusse_t*)data,
         .xsave_hdr.xstate_bv = XSTATE_FP_SSE,
xen/arch/x86/include/asm/hvm/hvm.h +0 -1
index a9425c8cffe8..846b91ebefcc 100644
@@ -184,7 +184,6 @@ struct hvm_function_table {
 
     /* Instruction intercepts: non-void return values are X86EMUL codes. */
     void (*wbinvd_intercept)(void);
-    void (*fpu_dirty_intercept)(void);
     int (*msr_read_intercept)(unsigned int msr, uint64_t *msr_content);
     int (*msr_write_intercept)(unsigned int msr, uint64_t msr_content);
     void (*handle_cd)(struct vcpu *v, unsigned long value);
xen/arch/x86/include/asm/xstate.h +0 -11
index e3b9745543d7..ca38c43ec1c3 100644
@@ -132,17 +132,6 @@ xsave_area_compressed(const struct xsave_struct *xsave_area)
     return xsave_area->xsave_hdr.xcomp_bv & XSTATE_COMPACTION_ENABLED;
 }
 
-static inline bool xstate_all(const struct vcpu *v)
-{
-    /*
-     * XSTATE_FP_SSE may be excluded, because the offsets of XSTATE_FP_SSE
-     * (in the legacy region of xsave area) are fixed, so saving
-     * XSTATE_FP_SSE will not cause overwriting problem with XSAVES/XSAVEC.
-     */
-    return xsave_area_compressed(v->arch.xsave_area) &&
-           (v->arch.xcr0_accum & XSTATE_LAZY & ~XSTATE_FP_SSE);
-}
-
 /*
  * Fetch a pointer to a vCPU's XSAVE area
  *
xen/arch/x86/xstate.c +5 -16
index e990abc9d18c..11d390cac985 100644
@@ -994,28 +994,17 @@ int handle_xsetbv(u32 index, u64 new_bv)
     if ( new_bv & XSTATE_NONLAZY )
         curr->arch.nonlazy_xstate_used = 1;
 
-    mask &= curr->fpu_dirtied ? ~XSTATE_FP_SSE : XSTATE_NONLAZY;
+    mask &= ~XSTATE_FP_SSE;
     if ( mask )
     {
         unsigned long cr0 = read_cr0();
+        /* Has a fastpath for `current`, so there's no actual map */
+        struct xsave_struct *xsave_area = VCPU_MAP_XSAVE_AREA(curr);
 
         clts();
-        if ( curr->fpu_dirtied )
-        {
-            /* Has a fastpath for `current`, so there's no actual map */
-            struct xsave_struct *xsave_area = VCPU_MAP_XSAVE_AREA(curr);
 
-            asm ( "stmxcsr %0" : "=m" (xsave_area->fpu_sse.mxcsr) );
-            VCPU_UNMAP_XSAVE_AREA(curr, xsave_area);
-        }
-        else if ( xstate_all(curr) )
-        {
-            /* See the comment in i387.c:vcpu_restore_fpu_eager(). */
-            mask |= XSTATE_LAZY;
-            curr->fpu_initialised = 1;
-            curr->fpu_dirtied = 1;
-            cr0 &= ~X86_CR0_TS;
-        }
+        asm ( "stmxcsr %0" : "=m" (xsave_area->fpu_sse.mxcsr) );
+        VCPU_UNMAP_XSAVE_AREA(curr, xsave_area);
         xrstor(curr, mask);
         if ( cr0 & X86_CR0_TS )
             write_cr0(cr0);
xen/common/domain.c +0 -2
index ab910fcf9306..30cfea30459a 100644
@@ -1824,8 +1824,6 @@ int vcpu_reset(struct vcpu *v)
     clear_bit(v->vcpu_id, d->poll_mask);
     v->poll_evtchn = 0;
 
-    v->fpu_initialised = 0;
-    v->fpu_dirtied     = 0;
     v->is_initialised  = 0;
     if ( v->affinity_broken & VCPU_AFFINITY_OVERRIDE )
         vcpu_temporary_affinity(v, NR_CPUS, VCPU_AFFINITY_OVERRIDE);
xen/include/xen/sched.h +0 -4
index 40a35fc15c65..212c7d765c3e 100644
@@ -212,10 +212,6 @@ struct vcpu
     struct guest_area runstate_guest_area;
     unsigned int     new_state;
 
-    /* Has the FPU been initialised? */
-    bool             fpu_initialised;
-    /* Has the FPU been used since it was last saved? */
-    bool             fpu_dirtied;
     /* Initialization completed for this VCPU? */
     bool             is_initialised;
     /* Currently running on a CPU? */
Signature · 1 line
2.53.0
#17 lore raw

Re: [PATCH v1 6/8] x86: Remove fpu_initialised/fpu_dirty

Quoted 25 lines · Ross Lagerwall

With lazy FPU removed, fpu_initialised and fpu_dirty are always set to true in vcpu_restore_fpu() so remove them and adjust the code accordingly.

No functional change intended.

---
 xen/arch/x86/domctl.c              |  3 +--
 xen/arch/x86/hvm/emulate.c         |  6 +-----
 xen/arch/x86/hvm/hvm.c             | 15 ++++++---------
 xen/arch/x86/hvm/vlapic.c          |  3 ---
 xen/arch/x86/i387.c                | 31 ++----------------------------
 xen/arch/x86/include/asm/hvm/hvm.h |  1 -
 xen/arch/x86/include/asm/xstate.h  | 11 -----------
 xen/arch/x86/xstate.c              | 21 +++++---------------
 xen/common/domain.c                |  2 --
 xen/include/xen/sched.h            |  4 ----
 10 files changed, 15 insertions(+), 82 deletions(-)
xen/arch/x86/domctl.c +0 -0
index 942f41c584d4..d9b08182ac1d 100644

xen/arch/x86/domctl.c void arch_get_info_guest(struct vcpu *v, vcpu_guest_context_u c) · line 1412

Quoted 6 lines
xen/arch/x86/domctl.c +1 -2
@@ -1409,8 +1409,7 @@ void arch_get_info_guest(struct vcpu *v, vcpu_guest_context_u c)
         c(flags = v->arch.pv.vgc_flags & ~(VGCF_i387_valid|VGCF_in_kernel));
     else
         c(flags = 0);
-    if ( v->fpu_initialised )
-        c(flags |= VGCF_i387_valid);
+    c(flags |= VGCF_i387_valid);

This is an API/ABI change.  Previously, creating a vCPU and instantly getting state will hand back a record with !VGCF_i387_valid.

It's fine - I've done a bunch of API/ABI changes in the FRED work, but it at least needs calling out in the commit message.

We have had a lot of cases where calling arch_{get,set}_info_guest() without an intervening __context_switch() would lead to subtle differences.  Generally I've been moving in the direction of architectural behaviour and not worrying about API/ABI changes which would occur naturally from running the vCPU.

That said, I think d1895441b3bad (2007) was the removal of the final consumer of VGCF_i387_valid in Xen.  We don't even have a conditional reset of state based on it's absence, and of course it's documented in the usual place, so it's really unclear what the purpose of this flag ever was. [edit, see below]

xen/arch/x86/hvm/vlapic.c static void vlapic_init_sipi_one(struct vcpu *target, uint32_t icr) · line 293

Quoted 13 lines
xen/arch/x86/hvm/vlapic.c +0 -2
index 79697487ba90..885f5d304b2f 100644
@@ -289,10 +288,8 @@ static void vlapic_init_sipi_one(struct vcpu *target, uint32_t icr)
         hvm_vcpu_down(target);
         domain_lock(target->domain);
         /* Reset necessary VCPU state. This does not include FPU state. */
-        fpu_initialised = target->fpu_initialised;
         rc = vcpu_reset(target);
         ASSERT(!rc);
-        target->fpu_initialised = fpu_initialised;
         vlapic_do_init(vcpu_vlapic(target));

This whole code block irks me.  x86 has two architectural events, #RESET and #INIT which are well defined, and this is using the former to mean the latter.

We are going to need to fix this, and it's going to be some fairly invasive renaming, but the result will be better. [edit, see below]

Quoted 13 lines
xen/arch/x86/i387.c +0 -1
index 88018397b1ad..5e893a2aab94 100644
@@ -265,7 +240,6 @@ void vcpu_reset_fpu(struct vcpu *v)
 {
     struct xsave_struct *xsave_area = VCPU_MAP_XSAVE_AREA(v);
 
-    v->fpu_initialised = false;
     *xsave_area = (struct xsave_struct) {
         .xsave_hdr.xstate_bv = X86_XCR0_X87,
     };

xen/arch/x86/i387.c void vcpu_setup_fpu(struct vcpu *v, const void *data) · line 261

Quoted 7 lines
xen/arch/x86/i387.c +0 -1
@@ -282,7 +256,6 @@ void vcpu_setup_fpu(struct vcpu *v, const void *data)
 {
     struct xsave_struct *xsave_area = VCPU_MAP_XSAVE_AREA(v);
 
-    v->fpu_initialised = true;
     *xsave_area = (struct xsave_struct) {
         .fpu_sse = *(const fpusse_t*)data,
         .xsave_hdr.xstate_bv = XSTATE_FP_SSE,

Hmm, looking at the callers of these two, we find that Xen has VGCF_I387_VALID too, and does have a consumer of this flag.  (This needs deleting for sanity reasons.)

It also means that this patch does introduce a bug here.  Calling arch_get_info_guest() prior to scheduling will hand back a block of all 0's, claiming it to be valid.

We need to arrange for vcpu_reset_fpu() to be called during vCPU construction (i.e. so we've never got a bad FPU state), before this patch will be safe.

~Andrew

#18 lore raw

Re: [PATCH v1 6/8] x86: Remove fpu_initialised/fpu_dirty

Quoted 98 lines · Andrew Cooper
Quoted 25 lines · Ross Lagerwall

With lazy FPU removed, fpu_initialised and fpu_dirty are always set to true in vcpu_restore_fpu() so remove them and adjust the code accordingly.

No functional change intended.

---
  xen/arch/x86/domctl.c              |  3 +--
  xen/arch/x86/hvm/emulate.c         |  6 +-----
  xen/arch/x86/hvm/hvm.c             | 15 ++++++---------
  xen/arch/x86/hvm/vlapic.c          |  3 ---
  xen/arch/x86/i387.c                | 31 ++----------------------------
  xen/arch/x86/include/asm/hvm/hvm.h |  1 -
  xen/arch/x86/include/asm/xstate.h  | 11 -----------
  xen/arch/x86/xstate.c              | 21 +++++---------------
  xen/common/domain.c                |  2 --
  xen/include/xen/sched.h            |  4 ----
  10 files changed, 15 insertions(+), 82 deletions(-)
xen/arch/x86/domctl.c +0 -0
index 942f41c584d4..d9b08182ac1d 100644

xen/arch/x86/domctl.c void arch_get_info_guest(struct vcpu *v, vcpu_guest_context_u c) · line 1412

Quoted 6 lines
xen/arch/x86/domctl.c +1 -2
@@ -1409,8 +1409,7 @@ void arch_get_info_guest(struct vcpu *v, vcpu_guest_context_u c)
          c(flags = v->arch.pv.vgc_flags & ~(VGCF_i387_valid|VGCF_in_kernel));
      else
          c(flags = 0);
-    if ( v->fpu_initialised )
-        c(flags |= VGCF_i387_valid);
+    c(flags |= VGCF_i387_valid);

This is an API/ABI change. Previously, creating a vCPU and instantly getting state will hand back a record with !VGCF_i387_valid.

It's fine - I've done a bunch of API/ABI changes in the FRED work, but it at least needs calling out in the commit message.

We have had a lot of cases where calling arch_{get,set}_info_guest() without an intervening __context_switch() would lead to subtle differences. Generally I've been moving in the direction of architectural behaviour and not worrying about API/ABI changes which would occur naturally from running the vCPU.

That said, I think d1895441b3bad (2007) was the removal of the final consumer of VGCF_i387_valid in Xen. We don't even have a conditional reset of state based on it's absence, and of course it's documented in the usual place, so it's really unclear what the purpose of this flag ever was. [edit, see below]

xen/arch/x86/hvm/vlapic.c static void vlapic_init_sipi_one(struct vcpu *target, uint32_t icr) · line 293

Quoted 13 lines
xen/arch/x86/hvm/vlapic.c +0 -2
index 79697487ba90..885f5d304b2f 100644
@@ -289,10 +288,8 @@ static void vlapic_init_sipi_one(struct vcpu *target, uint32_t icr)
          hvm_vcpu_down(target);
          domain_lock(target->domain);
          /* Reset necessary VCPU state. This does not include FPU state. */
-        fpu_initialised = target->fpu_initialised;
          rc = vcpu_reset(target);
          ASSERT(!rc);
-        target->fpu_initialised = fpu_initialised;
          vlapic_do_init(vcpu_vlapic(target));

This whole code block irks me. x86 has two architectural events, #RESET and #INIT which are well defined, and this is using the former to mean the latter.

We are going to need to fix this, and it's going to be some fairly invasive renaming, but the result will be better. [edit, see below]

Quoted 13 lines
xen/arch/x86/i387.c +0 -1
index 88018397b1ad..5e893a2aab94 100644
@@ -265,7 +240,6 @@ void vcpu_reset_fpu(struct vcpu *v)
  {
      struct xsave_struct *xsave_area = VCPU_MAP_XSAVE_AREA(v);
 
-    v->fpu_initialised = false;
      *xsave_area = (struct xsave_struct) {
          .xsave_hdr.xstate_bv = X86_XCR0_X87,
      };

xen/arch/x86/i387.c void vcpu_setup_fpu(struct vcpu *v, const void *data) · line 261

Quoted 7 lines
xen/arch/x86/i387.c +0 -1
@@ -282,7 +256,6 @@ void vcpu_setup_fpu(struct vcpu *v, const void *data)
  {
      struct xsave_struct *xsave_area = VCPU_MAP_XSAVE_AREA(v);
 
-    v->fpu_initialised = true;
      *xsave_area = (struct xsave_struct) {
          .fpu_sse = *(const fpusse_t*)data,
          .xsave_hdr.xstate_bv = XSTATE_FP_SSE,

Hmm, looking at the callers of these two, we find that Xen has VGCF_I387_VALID too, and does have a consumer of this flag. (This needs deleting for sanity reasons.)

Quoted 8 lines

It also means that this patch does introduce a bug here. Calling arch_get_info_guest() prior to scheduling will hand back a block of all 0's, claiming it to be valid.

We need to arrange for vcpu_reset_fpu() to be called during vCPU construction (i.e. so we've never got a bad FPU state), before this patch will be safe.

I think there is a similar pre-existing bug with eager-fpu. With eager-fpu, vcpu_restore_fpu_nonlazy() will always mark the FPU as initialized so the vCPU may be created and then context switched in without having either vcpu_reset_fpu() or vcpu_setup_fpu() called on it. At that point, arch_get_info_guest() would similarly return a block of all 0's claiming it to be valid.

In any case, calling vcpu_reset_fpu() earlier would fix both issues.

Ross

#19 lore raw

PATCH 7/8 x86/xstate: Stop tracking nonlazy xstate use

In reply to Ross Lagerwall

With the removal of lazy FPU, the full state is always restored on context switch so stop tracking whether nonlazy xstate is used.

---
 xen/arch/x86/domctl.c             |  1 -
 xen/arch/x86/hvm/hvm.c            |  1 -
 xen/arch/x86/i387.c               | 12 +-----------
 xen/arch/x86/include/asm/domain.h |  3 ---
 xen/arch/x86/include/asm/xstate.h |  6 ++----
 xen/arch/x86/xstate.c             |  3 ---
 6 files changed, 3 insertions(+), 23 deletions(-)
xen/arch/x86/domctl.c +0 -1
index d9b08182ac1d..a9fbb2d405b7 100644
@@ -1013,7 +1013,6 @@ long arch_do_domctl(
 
                 v->arch.xcr0 = _xcr0;
                 v->arch.xcr0_accum = _xcr0_accum;
-                v->arch.nonlazy_xstate_used = _xcr0_accum & XSTATE_NONLAZY;
                 compress_xsave_states(v, _xsave_area,
                                       evc->size - PV_XSAVE_HDR_SIZE);
 
xen/arch/x86/hvm/hvm.c +0 -1
index d4ba82845146..0e7b66d726ae 100644
@@ -1383,7 +1383,6 @@ static int cf_check hvm_load_cpu_xsave_states(
 
     v->arch.xcr0 = ctxt->xcr0;
     v->arch.xcr0_accum = ctxt->xcr0_accum;
-    v->arch.nonlazy_xstate_used = ctxt->xcr0_accum & XSTATE_NONLAZY;
     compress_xsave_states(v, &ctxt->save_area,
                           size - offsetof(struct hvm_hw_cpu_xsave, save_area));
 
xen/arch/x86/i387.c +1 -11
index 5e893a2aab94..9acaaf4673df 100644
@@ -108,25 +108,18 @@ static inline void fpu_fxrstor(struct vcpu *v)
 /*      FPU Save Functions     */
 /*******************************/
 
-static inline uint64_t vcpu_xsave_mask(const struct vcpu *v)
-{
-    return v->arch.nonlazy_xstate_used ? XSTATE_ALL : XSTATE_LAZY;
-}
-
 /* Save x87 extended state */
 static inline void fpu_xsave(struct vcpu *v)
 {
     bool ok;
-    uint64_t mask = vcpu_xsave_mask(v);
 
-    ASSERT(mask);
     /*
      * XCR0 normally represents what guest OS set. In case of Xen itself,
      * we set the accumulated feature mask before doing save/restore.
      */
     ok = set_xcr0(v->arch.xcr0_accum | XSTATE_FP_SSE);
     ASSERT(ok);
-    xsave(v, mask);
+    xsave(v, XSTATE_ALL);
     ok = set_xcr0(v->arch.xcr0 ?: XSTATE_FP_SSE);
     ASSERT(ok);
 }
@@ -202,9 +195,6 @@ void vcpu_restore_fpu(struct vcpu *v)
  */
 static bool _vcpu_save_fpu(struct vcpu *v)
 {
-    if ( !v->arch.nonlazy_xstate_used )
-        return false;
-
     ASSERT(!is_idle_vcpu(v));
 
     /* This can happen, if a paravirtualised guest OS has set its CR0.TS. */
xen/arch/x86/include/asm/domain.h +0 -3
index bd7b02085ef8..385a6666dafa 100644
@@ -667,9 +667,6 @@ struct arch_vcpu
      * it explicitly enables it via xcr0.
      */
     uint64_t xcr0_accum;
-    /* This variable determines whether nonlazy extended state has been used,
-     * and thus should be saved/restored. */
-    bool nonlazy_xstate_used;
 
     struct vmce vmce;
 
xen/arch/x86/include/asm/xstate.h +2 -4
index ca38c43ec1c3..c96d75e38b25 100644
@@ -33,12 +33,10 @@ extern uint32_t mxcsr_mask;
 #define XSTATE_FP_SSE  (X86_XCR0_X87 | X86_XCR0_SSE)
 #define XCNTXT_MASK    (X86_XCR0_X87 | X86_XCR0_SSE | X86_XCR0_YMM | \
                         X86_XCR0_OPMASK | X86_XCR0_ZMM | X86_XCR0_HI_ZMM | \
-                        XSTATE_NONLAZY)
+                        X86_XCR0_BNDREGS | X86_XCR0_BNDCSR | X86_XCR0_PKRU | \
+                        X86_XCR0_TILE_CFG | X86_XCR0_TILE_DATA)
 
 #define XSTATE_ALL     (~(1ULL << 63))
-#define XSTATE_NONLAZY (X86_XCR0_BNDREGS | X86_XCR0_BNDCSR | X86_XCR0_PKRU | \
-                        X86_XCR0_TILE_CFG | X86_XCR0_TILE_DATA)
-#define XSTATE_LAZY    (XSTATE_ALL & ~XSTATE_NONLAZY)
 #define XSTATE_XSAVES_ONLY         0
 #define XSTATE_COMPACTION_ENABLED  (1ULL << 63)
 
xen/arch/x86/xstate.c +0 -3
index 11d390cac985..658f372b8c51 100644
@@ -991,9 +991,6 @@ int handle_xsetbv(u32 index, u64 new_bv)
     curr->arch.xcr0 = new_bv;
     curr->arch.xcr0_accum |= new_bv;
 
-    if ( new_bv & XSTATE_NONLAZY )
-        curr->arch.nonlazy_xstate_used = 1;
-
     mask &= ~XSTATE_FP_SSE;
     if ( mask )
     {
Signature · 1 line
2.53.0
#21 lore raw

PATCH 8/8 x86: Cleanup cr0.TS flag handling

In reply to Ross Lagerwall

With lazy FPU removed, Xen does not need to touch the cr0.TS flag on context switch except when saving/restoring the FPU for a PV guest.

---
 xen/arch/x86/cpu/common.c       |  3 ---
 xen/arch/x86/hvm/emulate.c      | 14 ++------------
 xen/arch/x86/i387.c             | 22 +++-------------------
 xen/arch/x86/include/asm/i387.h |  1 -
 xen/common/efi/runtime.c        |  2 +-
 5 files changed, 6 insertions(+), 36 deletions(-)
xen/arch/x86/cpu/common.c +0 -3
index 5d0523a78b52..04a049f01c07 100644
@@ -883,9 +883,6 @@ void cpu_init(void)
 	/* Install correct page table. */
 	write_ptbase(current);
 
-	/* Ensure FPU gets initialised for each domain. */
-	stts();
-
 	/* Reset debug registers: */
 	write_debugreg(0, 0);
 	write_debugreg(1, 0);
xen/arch/x86/hvm/emulate.c +2 -12
index 48c7320360c7..f3aae158e9f8 100644
@@ -2527,14 +2527,8 @@ static int cf_check hvmemul_get_fpu(
          * Latch current register state so that we can back out changes
          * if needed (namely when a memory write fails after register state
          * has already been updated).
-         * NB: We don't really need the "enable" part of the called function
-         * (->fpu_dirtied set implies CR0.TS clear), but the additional
-         * overhead should be low enough to not warrant introduction of yet
-         * another slightly different function. However, we need to undo the
-         * ->fpu_dirtied clearing the function does as well as the possible
-         * masking of all exceptions by FNSTENV.)
          */
-        save_fpu_enable();
+        vcpu_save_fpu(curr);
         if ( (fpu_ctxt->fcw & 0x3f) != 0x3f )
         {
             uint16_t fcw;
@@ -2572,12 +2566,8 @@ static void cf_check hvmemul_put_fpu(
          * Latch current register state so that we can replace FIP/FDP/FOP
          * (which have values resulting from our own invocation of the FPU
          * instruction during emulation).
-         * NB: See also the comment in hvmemul_get_fpu(); we don't need to
-         * set ->fpu_dirtied here as it is going to be cleared below, and
-         * we also don't need to reload FCW as we're forcing full state to
-         * be reloaded anyway.
          */
-        save_fpu_enable();
+        vcpu_save_fpu(curr);
 
         if ( boot_cpu_has(X86_FEATURE_FDP_EXCP_ONLY) &&
              !(fpu_ctxt->fsw & ~fpu_ctxt->fcw & 0x003f) )
xen/arch/x86/i387.c +3 -19
index 9acaaf4673df..336bc83b6e13 100644
@@ -176,9 +176,6 @@ void vcpu_restore_fpu(struct vcpu *v)
 {
     ASSERT(!is_idle_vcpu(v));
 
-    /* Avoid recursion */
-    clts();
-
     if ( cpu_has_xsave )
         fpu_xrstor(v, XSTATE_ALL);
     else
@@ -193,31 +190,18 @@ void vcpu_restore_fpu(struct vcpu *v)
  * On each context switch, save the necessary FPU info of VCPU being switch 
  * out. It dispatches saving operation based on CPU's capability.
  */
-static bool _vcpu_save_fpu(struct vcpu *v)
+void vcpu_save_fpu(struct vcpu *v)
 {
     ASSERT(!is_idle_vcpu(v));
 
     /* This can happen, if a paravirtualised guest OS has set its CR0.TS. */
-    clts();
+    if ( is_pv_vcpu(v) )
+        clts();
 
     if ( cpu_has_xsave )
         fpu_xsave(v);
     else
         fpu_fxsave(v);
-
-    return true;
-}
-
-void vcpu_save_fpu(struct vcpu *v)
-{
-    _vcpu_save_fpu(v);
-    stts();
-}
-
-void save_fpu_enable(void)
-{
-    if ( !_vcpu_save_fpu(current) )
-        clts();
 }
 
 /* Initialize FPU's context save area */
xen/arch/x86/include/asm/i387.h +0 -1
index fe5e4419b6f4..0717005d31f0 100644
@@ -29,7 +29,6 @@ struct ix87_env {
 
 void vcpu_restore_fpu(struct vcpu *v);
 void vcpu_save_fpu(struct vcpu *v);
-void save_fpu_enable(void);
 int vcpu_init_fpu(struct vcpu *v);
 void vcpu_destroy_fpu(struct vcpu *v);
 
xen/common/efi/runtime.c +1 -1
index 982e42e8f341..0f1cc765ec5e 100644
@@ -94,7 +94,7 @@ struct efi_rs_state efi_rs_enter(void)
         return state;
 
     state.cr3 = read_cr3();
-    save_fpu_enable();
+    vcpu_save_fpu(current);
     asm volatile ( "fnclex; fldcw %0" :: "m" (fcw) );
     asm volatile ( "ldmxcsr %0" :: "m" (mxcsr) );
 
Signature · 1 line
2.53.0
#22 lore raw

Re: [PATCH v1 8/8] x86: Cleanup cr0.TS flag handling

Quoted 8 lines · Ross Lagerwall
xen/arch/x86/i387.c +0 -0
index 9acaaf4673df..336bc83b6e13 100644
@@ -193,31 +190,18 @@ void vcpu_restore_fpu(struct vcpu *v)
  * On each context switch, save the necessary FPU info of VCPU being switch 
  * out. It dispatches saving operation based on CPU's capability.
  */

xen/arch/x86/i387.c void vcpu_restore_fpu(struct vcpu *v) · line 200

Quoted 10 lines
xen/arch/x86/i387.c +3 -2
-static bool _vcpu_save_fpu(struct vcpu *v)
+void vcpu_save_fpu(struct vcpu *v)
 {
     ASSERT(!is_idle_vcpu(v));
 
     /* This can happen, if a paravirtualised guest OS has set its CR0.TS. */
-    clts();
+    if ( is_pv_vcpu(v) )
+        clts();
 

It's quite likely that this would be quicker to just leave as unconditional.

is_pv_vcpu() has evaluate_nospec() in it, so forces LFENCEs, and CLTS has a fast nop path even in very early implementations.

~Andrew

#23 lore raw

Re: [PATCH v1 8/8] x86: Cleanup cr0.TS flag handling

In reply to Ross Lagerwall

xen/arch/x86/cpu/common.c void cpu_init(void) · line 887

Quoted 8 lines · Ross Lagerwall
xen/arch/x86/cpu/common.c +0 -2
@@ -883,9 +883,6 @@ void cpu_init(void)
 	/* Install correct page table. */
 	write_ptbase(current);
 
-	/* Ensure FPU gets initialised for each domain. */
-	stts();

I'm a little concerned by the removal of this and ...

Quoted 16 lines
xen/arch/x86/cpu/common.c +3 -2
@@ -193,31 +190,18 @@ void vcpu_restore_fpu(struct vcpu *v)
  * On each context switch, save the necessary FPU info of VCPU being switch 
  * out. It dispatches saving operation based on CPU's capability.
  */
-static bool _vcpu_save_fpu(struct vcpu *v)
+void vcpu_save_fpu(struct vcpu *v)
 {
     ASSERT(!is_idle_vcpu(v));
 
     /* This can happen, if a paravirtualised guest OS has set its CR0.TS. */
-    clts();
+    if ( is_pv_vcpu(v) )
+        clts();
 
     if ( cpu_has_xsave )
         fpu_xsave(v);

xen/arch/x86/cpu/common.c void vcpu_restore_fpu(struct vcpu *v) · line 214

Quoted 10 lines
xen/arch/x86/cpu/common.c +0 -8
     else
         fpu_fxsave(v);
-
-    return true;
-}
-
-void vcpu_save_fpu(struct vcpu *v)
-{
-    _vcpu_save_fpu(v);
-    stts();

... this. At present it guards us against e.g. an idle CPU or context switch code mistakenly using in particular XMM registers (but of course also other extended state).

Jan

#24 lore raw

Re: [PATCH v1 8/8] x86: Cleanup cr0.TS flag handling

Quoted 26 lines · Jan Beulich

xen/arch/x86/cpu/common.c void cpu_init(void) · line 887

Quoted 8 lines · Ross Lagerwall
xen/arch/x86/cpu/common.c +0 -2
@@ -883,9 +883,6 @@ void cpu_init(void)
  	/* Install correct page table. */
  	write_ptbase(current);
 
-	/* Ensure FPU gets initialised for each domain. */
-	stts();

I'm a little concerned by the removal of this and ...

Quoted 14 lines
xen/arch/x86/cpu/common.c +3 -2
@@ -193,31 +190,18 @@ void vcpu_restore_fpu(struct vcpu *v)
   * On each context switch, save the necessary FPU info of VCPU being switch
   * out. It dispatches saving operation based on CPU's capability.
   */
-static bool _vcpu_save_fpu(struct vcpu *v)
+void vcpu_save_fpu(struct vcpu *v)
  {
      ASSERT(!is_idle_vcpu(v));
 
      /* This can happen, if a paravirtualised guest OS has set its CR0.TS. */
-    clts();
+    if ( is_pv_vcpu(v) )
+        clts();
 
Quoted 17 lines

xen/arch/x86/cpu/common.c void vcpu_restore_fpu(struct vcpu *v) · line 214

Quoted 12 lines
xen/arch/x86/cpu/common.c +0 -8
      if ( cpu_has_xsave )
          fpu_xsave(v);
      else
          fpu_fxsave(v);
-
-    return true;
-}
-
-void vcpu_save_fpu(struct vcpu *v)
-{
-    _vcpu_save_fpu(v);
-    stts();

... this. At present it guards us against e.g. an idle CPU or context switch code mistakenly using in particular XMM registers (but of course also other extended state).

Given this concern and Andrew's comment, I could drop this patch for now. It can be revisited in future if needed.

Ross

#25 lore raw

Re: [PATCH v1 8/8] x86: Cleanup cr0.TS flag handling

Quoted 44 lines · Ross Lagerwall
Quoted 26 lines · Jan Beulich

xen/arch/x86/cpu/common.c void cpu_init(void) · line 887

Quoted 8 lines · Ross Lagerwall
xen/arch/x86/cpu/common.c +0 -2
@@ -883,9 +883,6 @@ void cpu_init(void)
  	/* Install correct page table. */
  	write_ptbase(current);
 
-	/* Ensure FPU gets initialised for each domain. */
-	stts();

I'm a little concerned by the removal of this and ...

Quoted 14 lines
xen/arch/x86/cpu/common.c +3 -2
@@ -193,31 +190,18 @@ void vcpu_restore_fpu(struct vcpu *v)
   * On each context switch, save the necessary FPU info of VCPU being switch
   * out. It dispatches saving operation based on CPU's capability.
   */
-static bool _vcpu_save_fpu(struct vcpu *v)
+void vcpu_save_fpu(struct vcpu *v)
  {
      ASSERT(!is_idle_vcpu(v));
 
      /* This can happen, if a paravirtualised guest OS has set its CR0.TS. */
-    clts();
+    if ( is_pv_vcpu(v) )
+        clts();
 
Quoted 16 lines

xen/arch/x86/cpu/common.c void vcpu_restore_fpu(struct vcpu *v) · line 214

Quoted 12 lines
xen/arch/x86/cpu/common.c +0 -8
      if ( cpu_has_xsave )
          fpu_xsave(v);
      else
          fpu_fxsave(v);
-
-    return true;
-}
-
-void vcpu_save_fpu(struct vcpu *v)
-{
-    _vcpu_save_fpu(v);
-    stts();

... this. At present it guards us against e.g. an idle CPU or context switch code mistakenly using in particular XMM registers (but of course also other extended state).

Quoted 2 lines

Given this concern and Andrew's comment, I could drop this patch for now. It can be revisited in future if needed.

We discussed this some on the x86 maintainers call earlier today. Andrew (who wants to put together a more extensive reply) indicates that getting rid of the stts() instances is a relevant goal of this work. If this can be clearly stated for this patch, and if the implications are clear, then I think this could still be okay to go in.

Jan