Skip to contentSkip to conversation

x86: Always use eager-fpu

PATCH In review Ross Lagerwall 3 participants 15 messages Mar 24 – Mar 25

lore
Share & tools
lore
#0 lore raw

v2 cover x86: Always use eager-fpu

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

The first patch is a bugfix, new in v2.
The second 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 5, I haven't kept authorship and sign-offs since this is a reimplementation, albeit ending up at a similar place.

Thanks,
Ross

Ross Lagerwall (8):
x86/domain: Ensure a vCPU's FPU is reset early
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                    |   4 +-
 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                      | 109 ++---------------------
 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                     |  20 +++--
 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, 59 insertions(+), 556 deletions(-)
Signature · 1 line
2.53.0
#1 lore raw

PATCH v2 1/9 x86/domain: Ensure a vCPU's FPU is reset early

When using eager-fpu, a vCPU's FPU is always marked as initialized on context switch but it is possible that neither vcpu_reset_fpu() nor vcpu_setup_fpu() has been called on it. If that happens, arch_get_info_guest() would return a block of all 0's for the FPU context claiming it to be valid.

Fix this by calling vcpu_reset_fpu() during vCPU creation.

---
New in v2

 xen/arch/x86/domain.c | 2 ++
 1 file changed, 2 insertions(+)
xen/arch/x86/domain.c +2 -0
index 9ba2774762cc..82da1c5d7b38 100644
@@ -522,6 +522,8 @@ int arch_vcpu_create(struct vcpu *v)
         if ( (rc = vcpu_init_fpu(v)) != 0 )
             return rc;
 
+        vcpu_reset_fpu(v);
+
         vmce_init_vcpu(v);
 
         arch_vcpu_regs_init(v);
Signature · 1 line
2.53.0
#2 lore raw

Re: [PATCH v2 1/9] x86/domain: Ensure a vCPU's FPU is reset early

Quoted 3 lines · Ross Lagerwall

When using eager-fpu, a vCPU's FPU is always marked as initialized on context switch but it is possible that neither vcpu_reset_fpu() nor vcpu_setup_fpu() has been called on it.

How?

I don't think a PV vCPU can.  You cannot VCPUOP_up a vCPU for which v->is_initialised is false, and setting is_initialised involves either giving a good FPU, or taking the "reset" path.

An HVM use of VCPUOP_initialise only passes basic state, so can be used to set v->is_initialised without touching the FPU state.

DM_INIT preserves the v->fpu_initialised-ness while not touching the buffer.  This is correct(ish).  FPU registers are (mostly) not modified on INIT.

Quoted 7 lines

If that happens,
arch_get_info_guest() would return a block of all 0's for the FPU context claiming it to be valid.

Fix this by calling vcpu_reset_fpu() during vCPU creation.

The phrasing is a bit awkward, and the function names don't help, but it is something we're going to have to address properly when doing nested virt.

(A minor tangent which is relevant to where we want to end up)

https://sandpile.org/x86/initial.htm

#RESET and #INIT used to be a physical pins, but are just a message on the fabric. Either way they're events which alter state in well defined ways.

From Xen's point of view, vcpu_create() is the only #RESET-like thing we've got.  If we didn't model crash/reboot as constructing a new domain, that would be the other place to use #RESET.

#INIT exists explicitly for HVM guests, via the APIC interface.  Xen has no working model of this because HVM guests were built on PV which wasn't modelled on how CPUs work.

v->is_initialised is a PV-ism which has infected x86 HVM and non-x86 architectures too.  The key thing which PV vCPUs need that doesn't work like CPUs in the slightest is the chosen vCR3 (and vCR1 for PV64) need to refer to a property typed L4/L3 pagetable, and PV guests can't take a type ref on 0.

Anyway, returning from the tangent ...

Quoted 11 lines

---
New in v2

 xen/arch/x86/domain.c | 2 ++
 1 file changed, 2 insertions(+)
xen/arch/x86/domain.c +0 -0
index 9ba2774762cc..82da1c5d7b38 100644

xen/arch/x86/domain.c int arch_vcpu_create(struct vcpu *v) · line 526

Quoted 5 lines
xen/arch/x86/domain.c +2 -0
@@ -522,6 +522,8 @@ int arch_vcpu_create(struct vcpu *v)
         if ( (rc = vcpu_init_fpu(v)) != 0 )
             return rc;
 
+        vcpu_reset_fpu(v);
+

... this really should be part of allocating the memory.

First, we should never have the backing memory in the wrong state, and second, the idle vCPU doesn't take this path.  i.e. in xstate_alloc_save_area().

Looking into this asks more questions.

xstate_alloc_save_area() does set some of the backing state, but misses FXSAVE_FTW_RESET.  That's easy enough to fix, and turns out to address my original concern.

vcpu_reset_fpu() sets v->fpu_initialised = false.  Doesn't this defeat the point of this patch?

Maybe it's easiest just to fix FXSAVE_FTW_RESET and then purge the booleans in the way this series does.  I don't think trying to unpick any other bugfixes is going to be fruitful.

~Andrew

#3 lore raw

Re: [PATCH v2 1/9] x86/domain: Ensure a vCPU's FPU is reset early

Quoted 5 lines · Andrew Cooper
Quoted 3 lines · Ross Lagerwall

When using eager-fpu, a vCPU's FPU is always marked as initialized on context switch but it is possible that neither vcpu_reset_fpu() nor vcpu_setup_fpu() has been called on it.

Quoted 9 lines

How?

I don't think a PV vCPU can. You cannot VCPUOP_up a vCPU for which v->is_initialised is false, and setting is_initialised involves either giving a good FPU, or taking the "reset" path.

An HVM use of VCPUOP_initialise only passes basic state, so can be used to set v->is_initialised without touching the FPU state.

Yes, then a subsequent VCPUOP_up allows the VCPU to be scheduled. vcpu_restore_fpu_nonlazy(), called on context switch, always sets fpu_initialised to 1 in the eager-fpu case. I confirmed this happens with some debug logging during VM start:

[  106.281001] vcpu_init_fpu vCPU 1 (fpu_initialised is 0)
[  110.088015] vcpu_restore_fpu_nonlazy vCPU 1 (set fpu_initialised to 1)
[  110.088155] vcpu_restore_fpu_nonlazy vCPU 1 (set fpu_initialised to 1)
[  110.088518] vcpu_restore_fpu_nonlazy vCPU 1 (set fpu_initialised to 1)
[  110.356216] vcpu_reset_fpu vCPU 1 (set fpu_initialised to 0)
[  110.356236] vcpu_restore_fpu_nonlazy vCPU 1 (set fpu_initialised to 1)

...

Quoted 60 lines
Quoted 8 lines

If that happens,
arch_get_info_guest() would return a block of all 0's for the FPU context claiming it to be valid.

Fix this by calling vcpu_reset_fpu() during vCPU creation.

The phrasing is a bit awkward, and the function names don't help, but it is something we're going to have to address properly when doing nested virt.

(A minor tangent which is relevant to where we want to end up)

https://sandpile.org/x86/initial.htm

#RESET and #INIT used to be a physical pins, but are just a message on the fabric. Either way they're events which alter state in well defined ways.

From Xen's point of view, vcpu_create() is the only #RESET-like thing
we've got. If we didn't model crash/reboot as constructing a new domain, that would be the other place to use #RESET.

#INIT exists explicitly for HVM guests, via the APIC interface. Xen has no working model of this because HVM guests were built on PV which wasn't modelled on how CPUs work.

v->is_initialised is a PV-ism which has infected x86 HVM and non-x86 architectures too. The key thing which PV vCPUs need that doesn't work like CPUs in the slightest is the chosen vCR3 (and vCR1 for PV64) need to refer to a property typed L4/L3 pagetable, and PV guests can't take a type ref on 0.

Anyway, returning from the tangent ...

Quoted 11 lines

---
New in v2

  xen/arch/x86/domain.c | 2 ++
  1 file changed, 2 insertions(+)
xen/arch/x86/domain.c +0 -0
index 9ba2774762cc..82da1c5d7b38 100644

xen/arch/x86/domain.c int arch_vcpu_create(struct vcpu *v) · line 526

Quoted 5 lines
xen/arch/x86/domain.c +2 -0
@@ -522,6 +522,8 @@ int arch_vcpu_create(struct vcpu *v)
          if ( (rc = vcpu_init_fpu(v)) != 0 )
              return rc;
 
+        vcpu_reset_fpu(v);
+

... this really should be part of allocating the memory.

First, we should never have the backing memory in the wrong state, and second, the idle vCPU doesn't take this path. i.e. in xstate_alloc_save_area().

Quoted 8 lines

Looking into this asks more questions.

xstate_alloc_save_area() does set some of the backing state, but misses FXSAVE_FTW_RESET. That's easy enough to fix, and turns out to address my original concern.

vcpu_reset_fpu() sets v->fpu_initialised = false. Doesn't this defeat the point of this patch?

No, since it means the backing memory has been properly initialised by the time fpu_initalised gets unilaterally set in vcpu_restore_fpu_nonlazy()...

Quoted 5 lines

Maybe it's easiest just to fix FXSAVE_FTW_RESET and then purge the booleans in the way this series does. I don't think trying to unpick any other bugfixes is going to be fruitful.

... but yes, that does seem like the best way to fix this.

Ross

#4 lore raw

PATCH v2 2/9 x86: Always use eager-fpu

In reply to Ross Lagerwall

Lazy FPU avoids some work during a context switch but pushes more expensive 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.
* A key difference between 32-bit and 64-bit OSes is that %xmm is in the base featureset for 64-bit and thus gets ubiquitous use in userspace. This is likely why we hit 83%.
* 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.

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

---
In v2: Expanded the commit message

 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
#5 lore raw

PATCH v2 3/9 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.

---
Unchanged in v2

 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
#6 lore raw

PATCH v2 4/9 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.

---
Unchanged in v2

 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
#7 lore raw

PATCH v2 5/9 x86/traps: Remove lazy FPU support

In reply to Ross Lagerwall

From: Wei Liu <[email protected]>

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

---
In v2:
* Tweaked the commit message
* Crash the guest rather than host if the guest register is in an unexpected state.
* Reword a comment
* Allow tail-call optimization

 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              | 20 +++++++++++++-------
 4 files changed, 14 insertions(+), 34 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 +13 -7
index b6b119769722..0565c576ecd6 100644
@@ -2045,15 +2045,21 @@ void asmlinkage do_device_not_available(struct cpu_user_regs *regs)
     }
 
 #ifdef CONFIG_PV
-    vcpu_restore_fpu_lazy(curr);
-
-    if ( 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;
+        ASSERT_UNREACHABLE();
+        domain_crash(curr->domain);
+        return;
     }
-    else
-        TRACE_TIME(TRC_PV_MATH_STATE_RESTORE);
+
+    /*
+     * For better or worse, Xen's ABI with PV guests always clears TS on an #NM
+     * exception. Classic-xen Linux depends on this.
+     */
+    clts();
+    curr->arch.pv.ctrlreg[0] &= ~X86_CR0_TS;
+
+    pv_inject_hw_exception(X86_EXC_NM, X86_EVENT_NO_EC);
 #else
     ASSERT_UNREACHABLE();
 #endif
Signature · 1 line
2.53.0
#9 lore raw

PATCH v2 6/9 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.

---
Unchanged in v2

 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 82da1c5d7b38..a10cb87e0b58 100644
@@ -2127,7 +2127,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
#10 lore raw

PATCH v2 7/9 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.

There is a small user-visible change here. After this commit, calling XEN_DOMCTL_getvcpucontext immediately after vCPU creation would show the FPU as initialized (with a clean state) whereas before it would show as uninitialized.

---
In v2: Noted behaviour change in commit message

 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
#11 lore raw

Re: [PATCH v2 7/9] x86: Remove fpu_initialised/fpu_dirty

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

Quoted 4 lines · Ross Lagerwall
xen/arch/x86/domctl.c +0 -0
@@ -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));

It ends up a little odd to clear VGCF_i387_valid here, just to ...

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

Quoted 5 lines
xen/arch/x86/domctl.c +1 -2
     else
         c(flags = 0);
-    if ( v->fpu_initialised )
-        c(flags |= VGCF_i387_valid);
+    c(flags |= VGCF_i387_valid);

... unconditionally set it afterwards. Could I talk you into cleaning this up right away?

Jan

#12 lore raw

Re: [PATCH v2 7/9] x86: Remove fpu_initialised/fpu_dirty

Quoted 8 lines · Jan Beulich

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

Quoted 4 lines · Ross Lagerwall
xen/arch/x86/domctl.c +0 -0
@@ -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));

It ends up a little odd to clear VGCF_i387_valid here, just to ...

Quoted 9 lines

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

Quoted 5 lines
xen/arch/x86/domctl.c +1 -2
      else
          c(flags = 0);
-    if ( v->fpu_initialised )
-        c(flags |= VGCF_i387_valid);
+    c(flags |= VGCF_i387_valid);

... unconditionally set it afterwards. Could I talk you into cleaning this up right away?

Sure, I can do that in the next version.

Ross

#13 lore raw

PATCH v2 8/9 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.

---
Unchanged in v2

 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
#14 lore raw

PATCH v2 9/9 x86: Cleanup cr0.TS flag handling

In reply to Ross Lagerwall

Setting/clearing the cr0.TS bit is serializing and setting the cr0.TS bit in particular requires a write to CR0 which is implemented in microcode and is very slow. Therefore and with lazy FPU removed, avoid setting/clearing the cr0.TS flag on context switch except when saving/restoring the FPU for a PV guest.

After this, Xen's context switch code and idle CPUs run with the TS bit cleared. An unfortunate side effect of this is that any mistaken use of XMM registers or extended state will not trap and will therefore be more difficult to detect.

---
In v2:
* Add more justification to the commit message.
* Always call clts() in vcpu_save_fpu().

 xen/arch/x86/cpu/common.c       |  3 ---
 xen/arch/x86/hvm/emulate.c      | 14 ++------------
 xen/arch/x86/i387.c             | 19 +------------------
 xen/arch/x86/include/asm/i387.h |  1 -
 xen/common/efi/runtime.c        |  2 +-
 5 files changed, 4 insertions(+), 35 deletions(-)
xen/arch/x86/cpu/common.c +0 -3
index 5ac76897ae47..057592b7c870 100644
@@ -887,9 +887,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 +1 -18
index 9acaaf4673df..c6796bc97b70 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,7 +190,7 @@ 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));
 
@@ -204,20 +201,6 @@ static bool _vcpu_save_fpu(struct vcpu *v)
         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