Skip to contentSkip to conversation

x86/spec-ctrl: AMD DIV fix, and VERW prerequisite bugfixes

PATCH In review Andrew Cooper 4 participants 32 messages Sep 13, 2023 – Sep 15, 2023

lore
Share & tools
lore
#0 lore raw

cover x86/spec-ctrl: AMD DIV fix, and VERW prerequisite bugfixes

Patch 8 is the XSA-439 fix for the AMD DIV issue, disclosed insufficiently ahead of August 8th for us to prepare a fix for the embargo.

Patches 1 thru 7 are prerequisites, identified while trying to write patch 8.

All 8 patches are for all security trees.

Andrew Cooper (8):
x86/spec-ctrl: Fix confusion between SPEC_CTRL_EXIT_TO_XEN{,_IST} x86/spec-ctrl: Fold DO_SPEC_CTRL_EXIT_TO_XEN into it's single user x86/spec-ctrl: Turn the remaining SPEC_CTRL_{ENTRY,EXIT}_* into asm macros x86/spec-ctrl: Extend all SPEC_CTRL_{ENTER,EXIT}_* comments x86/entry: Adjust restore_all_xen to hold stack_end in %r14 x86/entry: Track the IST-ness of an entry for the exit paths x86/spec-ctrl: Issue VERW during IST exit to Xen x86/spec-ctrl: Mitigate the Zen1 DIV leakge

 docs/misc/xen-command-line.pandoc        |   6 +-
 xen/arch/x86/hvm/svm/entry.S             |   1 +
 xen/arch/x86/include/asm/cpufeatures.h   |   2 +-
 xen/arch/x86/include/asm/spec_ctrl_asm.h | 150 ++++++++++++++++-------
 xen/arch/x86/spec_ctrl.c                 |  45 ++++++-
 xen/arch/x86/traps.c                     |  13 ++
 xen/arch/x86/x86_64/compat/entry.S       |   9 +-
 xen/arch/x86/x86_64/entry.S              |  31 +++--
 8 files changed, 198 insertions(+), 59 deletions(-)

base-commit: 6aa25c32180ab59081c73bae4c568367d9133a1f

Signature · 1 line
2.30.2
#1 lore raw

PATCH 1/8 x86/spec-ctrl: Fix confusion between SPEC_CTRL_EXIT_TO_XEN{,_IST}

c/s 3fffaf9c13e9 ("x86/entry: Avoid using alternatives in NMI/#MC paths") dropped the only user, leaving behind the (incorrect) implication that Xen had split exit paths.

Delete the unused SPEC_CTRL_EXIT_TO_XEN and rename SPEC_CTRL_EXIT_TO_XEN_IST to SPEC_CTRL_EXIT_TO_XEN for consistency.

No functional change.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>

---
 xen/arch/x86/include/asm/spec_ctrl_asm.h | 8 +-------
 xen/arch/x86/x86_64/entry.S              | 2 +-
 2 files changed, 2 insertions(+), 8 deletions(-)
xen/arch/x86/include/asm/spec_ctrl_asm.h +1 -7
index f48f9e75e8dc..14ec40e8d32f 100644
@@ -67,7 +67,6 @@
  *  - SPEC_CTRL_ENTRY_FROM_PV
  *  - SPEC_CTRL_ENTRY_FROM_INTR
  *  - SPEC_CTRL_ENTRY_FROM_INTR_IST
- *  - SPEC_CTRL_EXIT_TO_XEN_IST
  *  - SPEC_CTRL_EXIT_TO_XEN
  *  - SPEC_CTRL_EXIT_TO_PV
  *
@@ -256,11 +255,6 @@
     ALTERNATIVE "", __stringify(DO_SPEC_CTRL_ENTRY maybexen=1),         \
         X86_FEATURE_SC_MSR_PV
 
-/* Use when exiting to Xen context. */
-#define SPEC_CTRL_EXIT_TO_XEN                                           \
-    ALTERNATIVE "",                                                     \
-        DO_SPEC_CTRL_EXIT_TO_XEN, X86_FEATURE_SC_MSR_PV
-
 /* Use when exiting to PV guest context. */
 #define SPEC_CTRL_EXIT_TO_PV                                            \
     ALTERNATIVE "",                                                     \
@@ -328,7 +322,7 @@ UNLIKELY_DISPATCH_LABEL(\@_serialise):
 .endm
 
 /* Use when exiting to Xen in IST context. */
-.macro SPEC_CTRL_EXIT_TO_XEN_IST
+.macro SPEC_CTRL_EXIT_TO_XEN
 /*
  * Requires %rbx=stack_end
  * Clobbers %rax, %rcx, %rdx
xen/arch/x86/x86_64/entry.S +1 -1
index 81dd2c74b876..a1c860f56949 100644
@@ -673,7 +673,7 @@ UNLIKELY_START(ne, exit_cr3)
 UNLIKELY_END(exit_cr3)
 
         /* WARNING! `ret`, `call *`, `jmp *` not safe beyond this point. */
-        SPEC_CTRL_EXIT_TO_XEN_IST /* Req: %rbx=end, Clob: acd */
+        SPEC_CTRL_EXIT_TO_XEN     /* Req: %rbx=end, Clob: acd */
 
         RESTORE_ALL adj=8
         iretq

base-commit: 6aa25c32180ab59081c73bae4c568367d9133a1f

Signature · 1 line
2.30.2
Reviewed-by on v1 1/8
#2 lore raw

Re: [PATCH 1/8] x86/spec-ctrl: Fix confusion between SPEC_CTRL_EXIT_TO_XEN{,_IST}

Quoted 10 lines · Andrew Cooper

c/s 3fffaf9c13e9 ("x86/entry: Avoid using alternatives in NMI/#MC paths") dropped the only user, leaving behind the (incorrect) implication that Xen had split exit paths.

Delete the unused SPEC_CTRL_EXIT_TO_XEN and rename SPEC_CTRL_EXIT_TO_XEN_IST to SPEC_CTRL_EXIT_TO_XEN for consistency.

No functional change.

albeit ...

Quoted 13 lines
xen/arch/x86/include/asm/spec_ctrl_asm.h +0 -5
@@ -256,11 +255,6 @@
     ALTERNATIVE "", __stringify(DO_SPEC_CTRL_ENTRY maybexen=1),         \
         X86_FEATURE_SC_MSR_PV
 
-/* Use when exiting to Xen context. */
-#define SPEC_CTRL_EXIT_TO_XEN                                           \
-    ALTERNATIVE "",                                                     \
-        DO_SPEC_CTRL_EXIT_TO_XEN, X86_FEATURE_SC_MSR_PV
-
 /* Use when exiting to PV guest context. */
 #define SPEC_CTRL_EXIT_TO_PV                                            \
     ALTERNATIVE "",                                                     \

xen/arch/x86/include/asm/spec_ctrl_asm.h UNLIKELY_DISPATCH_LABEL(\@_serialise): · line 325

Quoted 5 lines
xen/arch/x86/include/asm/spec_ctrl_asm.h +1 -1
@@ -328,7 +322,7 @@ UNLIKELY_DISPATCH_LABEL(\@_serialise):
 .endm
 
 /* Use when exiting to Xen in IST context. */
-.macro SPEC_CTRL_EXIT_TO_XEN_IST
+.macro SPEC_CTRL_EXIT_TO_XEN

... with the comment her updated (either by dropping "in IST" or by explicitly mentioning both cases).

Jan

#3 lore raw

Re: [PATCH 1/8] x86/spec-ctrl: Fix confusion between SPEC_CTRL_EXIT_TO_XEN{,_IST}

Quoted 29 lines · Jan Beulich
Quoted 10 lines · Andrew Cooper

c/s 3fffaf9c13e9 ("x86/entry: Avoid using alternatives in NMI/#MC paths") dropped the only user, leaving behind the (incorrect) implication that Xen had split exit paths.

Delete the unused SPEC_CTRL_EXIT_TO_XEN and rename SPEC_CTRL_EXIT_TO_XEN_IST to SPEC_CTRL_EXIT_TO_XEN for consistency.

No functional change.

albeit ...

Quoted 15 lines
xen/arch/x86/include/asm/spec_ctrl_asm.h +0 -5
@@ -256,11 +255,6 @@
     ALTERNATIVE "", __stringify(DO_SPEC_CTRL_ENTRY maybexen=1),         \
         X86_FEATURE_SC_MSR_PV
 
-/* Use when exiting to Xen context. */
-#define SPEC_CTRL_EXIT_TO_XEN                                           \
-    ALTERNATIVE "",                                                     \
-        DO_SPEC_CTRL_EXIT_TO_XEN, X86_FEATURE_SC_MSR_PV
-
 /* Use when exiting to PV guest context. */
 #define SPEC_CTRL_EXIT_TO_PV                                            \
     ALTERNATIVE "",                                                     \
@@ -328,7 +322,7 @@ UNLIKELY_DISPATCH_LABEL(\@_serialise):
 .endm
 
Quoted 5 lines

xen/arch/x86/include/asm/spec_ctrl_asm.h UNLIKELY_DISPATCH_LABEL(\@_serialise): · line 325

Quoted 3 lines
xen/arch/x86/include/asm/spec_ctrl_asm.h +1 -1
 /* Use when exiting to Xen in IST context. */
-.macro SPEC_CTRL_EXIT_TO_XEN_IST
+.macro SPEC_CTRL_EXIT_TO_XEN

... with the comment her updated (either by dropping "in IST" or by explicitly mentioning both cases).

The comment is rewritten from scratch in patch 4.  I'm not moving that rewrite to here, and the comment isn't technically wrong to begin with, but I suppose I can drop the IST part.  Just means more churn.

~Andrew

#4 lore raw

PATCH 2/8 x86/spec-ctrl: Fold DO_SPEC_CTRL_EXIT_TO_XEN into it's single user

In reply to Andrew Cooper

With the SPEC_CTRL_EXIT_TO_XEN{,_IST} confusion fixed, it's now obvious that there's only a single EXIT_TO_XEN path. Fold DO_SPEC_CTRL_EXIT_TO_XEN into SPEC_CTRL_EXIT_TO_XEN to simplify further fixes.

When merging labels, switch the name to .L\@_skip_sc_msr as "skip" on its own is going to be too generic shortly.

No functional change.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>

---
 xen/arch/x86/include/asm/spec_ctrl_asm.h | 40 ++++++++++--------------
 1 file changed, 16 insertions(+), 24 deletions(-)
xen/arch/x86/include/asm/spec_ctrl_asm.h +16 -24
index 14ec40e8d32f..dd7c5e8bfc79 100644
@@ -199,27 +199,6 @@
     wrmsr
 .endm
 
-.macro DO_SPEC_CTRL_EXIT_TO_XEN
-/*
- * Requires %rbx=stack_end
- * Clobbers %rax, %rcx, %rdx
- *
- * When returning to Xen context, look to see whether SPEC_CTRL shadowing is
- * in effect, and reload the shadow value.  This covers race conditions which
- * exist with an NMI/MCE/etc hitting late in the return-to-guest path.
- */
-    xor %edx, %edx
-
-    testb $SCF_use_shadow, STACK_CPUINFO_FIELD(spec_ctrl_flags)(%rbx)
-    jz .L\@_skip
-
-    mov STACK_CPUINFO_FIELD(shadow_spec_ctrl)(%rbx), %eax
-    mov $MSR_SPEC_CTRL, %ecx
-    wrmsr
-
-.L\@_skip:
-.endm
-
 .macro DO_SPEC_CTRL_EXIT_TO_GUEST
 /*
  * Requires %eax=spec_ctrl, %rsp=regs/cpuinfo
@@ -328,11 +307,24 @@ UNLIKELY_DISPATCH_LABEL(\@_serialise):
  * Clobbers %rax, %rcx, %rdx
  */
     testb $SCF_ist_sc_msr, STACK_CPUINFO_FIELD(spec_ctrl_flags)(%rbx)
-    jz .L\@_skip
+    jz .L\@_skip_sc_msr
 
-    DO_SPEC_CTRL_EXIT_TO_XEN
+    /*
+     * When returning to Xen context, look to see whether SPEC_CTRL shadowing
+     * is in effect, and reload the shadow value.  This covers race conditions
+     * which exist with an NMI/MCE/etc hitting late in the return-to-guest
+     * path.
+     */
+    xor %edx, %edx
 
-.L\@_skip:
+    testb $SCF_use_shadow, STACK_CPUINFO_FIELD(spec_ctrl_flags)(%rbx)
+    jz .L\@_skip_sc_msr
+
+    mov STACK_CPUINFO_FIELD(shadow_spec_ctrl)(%rbx), %eax
+    mov $MSR_SPEC_CTRL, %ecx
+    wrmsr
+
+.L\@_skip_sc_msr:
 .endm
 
 #endif /* __ASSEMBLY__ */
Signature · 1 line
2.30.2
Reviewed-by on v1 2/8
#5 lore raw

Re: [PATCH 2/8] x86/spec-ctrl: Fold DO_SPEC_CTRL_EXIT_TO_XEN into it's single user

Quoted 10 lines · Andrew Cooper

With the SPEC_CTRL_EXIT_TO_XEN{,_IST} confusion fixed, it's now obvious that there's only a single EXIT_TO_XEN path. Fold DO_SPEC_CTRL_EXIT_TO_XEN into SPEC_CTRL_EXIT_TO_XEN to simplify further fixes.

When merging labels, switch the name to .L\@_skip_sc_msr as "skip" on its own is going to be too generic shortly.

No functional change.

#6 lore raw

PATCH 3/8 x86/spec-ctrl: Turn the remaining SPEC_CTRL_{ENTRY,EXIT}_* into asm macros

In reply to Andrew Cooper

These have grown more complex over time, with some already having been converted.

Provide full Requires/Clobbers comments, otherwise missing at this level of indirection.

No functional change.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>

---
 xen/arch/x86/include/asm/spec_ctrl_asm.h | 37 ++++++++++++++++++------
 1 file changed, 28 insertions(+), 9 deletions(-)
xen/arch/x86/include/asm/spec_ctrl_asm.h +28 -9
index dd7c5e8bfc79..ee75f2bced42 100644
@@ -219,26 +219,45 @@
 .endm
 
 /* Use after an entry from PV context (syscall/sysenter/int80/int82/etc). */
-#define SPEC_CTRL_ENTRY_FROM_PV                                         \
+.macro SPEC_CTRL_ENTRY_FROM_PV
+/*
+ * Requires %rsp=regs/cpuinfo, %rdx=0
+ * Clobbers %rax, %rcx, %rdx
+ */
     ALTERNATIVE "", __stringify(DO_SPEC_CTRL_COND_IBPB maybexen=0),     \
-        X86_FEATURE_IBPB_ENTRY_PV;                                      \
-    ALTERNATIVE "", DO_OVERWRITE_RSB, X86_FEATURE_SC_RSB_PV;            \
+        X86_FEATURE_IBPB_ENTRY_PV
+
+    ALTERNATIVE "", DO_OVERWRITE_RSB, X86_FEATURE_SC_RSB_PV
+
     ALTERNATIVE "", __stringify(DO_SPEC_CTRL_ENTRY maybexen=0),         \
         X86_FEATURE_SC_MSR_PV
+.endm
 
 /* Use in interrupt/exception context.  May interrupt Xen or PV context. */
-#define SPEC_CTRL_ENTRY_FROM_INTR                                       \
+.macro SPEC_CTRL_ENTRY_FROM_INTR
+/*
+ * Requires %rsp=regs, %r14=stack_end, %rdx=0
+ * Clobbers %rax, %rcx, %rdx
+ */
     ALTERNATIVE "", __stringify(DO_SPEC_CTRL_COND_IBPB maybexen=1),     \
-        X86_FEATURE_IBPB_ENTRY_PV;                                      \
-    ALTERNATIVE "", DO_OVERWRITE_RSB, X86_FEATURE_SC_RSB_PV;            \
+        X86_FEATURE_IBPB_ENTRY_PV
+
+    ALTERNATIVE "", DO_OVERWRITE_RSB, X86_FEATURE_SC_RSB_PV
+
     ALTERNATIVE "", __stringify(DO_SPEC_CTRL_ENTRY maybexen=1),         \
         X86_FEATURE_SC_MSR_PV
+.endm
 
 /* Use when exiting to PV guest context. */
-#define SPEC_CTRL_EXIT_TO_PV                                            \
-    ALTERNATIVE "",                                                     \
-        DO_SPEC_CTRL_EXIT_TO_GUEST, X86_FEATURE_SC_MSR_PV;              \
+.macro SPEC_CTRL_EXIT_TO_PV
+/*
+ * Requires %rax=spec_ctrl, %rsp=regs/info
+ * Clobbers %rcx, %rdx
+ */
+    ALTERNATIVE "", DO_SPEC_CTRL_EXIT_TO_GUEST, X86_FEATURE_SC_MSR_PV
+
     DO_SPEC_CTRL_COND_VERW
+.endm
 
 /*
  * Use in IST interrupt/exception context.  May interrupt Xen or PV context.
Signature · 1 line
2.30.2
Reviewed-by on v1 3/8
#7 lore raw

Re: [PATCH 3/8] x86/spec-ctrl: Turn the remaining SPEC_CTRL_{ENTRY,EXIT}_* into asm macros

Quoted 9 lines · Andrew Cooper

These have grown more complex over time, with some already having been converted.

Provide full Requires/Clobbers comments, otherwise missing at this level of indirection.

No functional change.

Quoted 6 lines

/* Use when exiting to PV guest context. */

xen/arch/x86/include/asm/spec_ctrl_asm.h +2 -3
-#define SPEC_CTRL_EXIT_TO_PV                                            \
-    ALTERNATIVE "",                                                     \
-        DO_SPEC_CTRL_EXIT_TO_GUEST, X86_FEATURE_SC_MSR_PV;              \
+.macro SPEC_CTRL_EXIT_TO_PV
+/*

xen/arch/x86/include/asm/spec_ctrl_asm.h line 263

Quoted 10 lines
xen/arch/x86/include/asm/spec_ctrl_asm.h +6 -0
+ * Requires %rax=spec_ctrl, %rsp=regs/info
+ * Clobbers %rcx, %rdx
+ */
+    ALTERNATIVE "", DO_SPEC_CTRL_EXIT_TO_GUEST, X86_FEATURE_SC_MSR_PV
+
     DO_SPEC_CTRL_COND_VERW
+.endm
 
 /*
  * Use in IST interrupt/exception context.  May interrupt Xen or PV context.

I'll need to carefully rebase "x86/spec-ctrl: add logic to issue IBPB on exit to guest" over this change. In particular the use of __LINE__ there (which probably you didn't really like anyway) won't work anymore. Yet with just a single use of the macro per source file we ought to be able to get away without this extra label disambiguation ...

Jan

#8 lore raw

PATCH 4/8 x86/spec-ctrl: Extend all SPEC_CTRL_{ENTER,EXIT}_* comments

In reply to Andrew Cooper

... to better explain how they're used.

Doing so highlights that SPEC_CTRL_EXIT_TO_XEN is missing a VERW flush for the corner case when e.g. an NMI hits late in an exit-to-guest path.

Leave a TODO, which will be addressed in subsequent patches which arrange for DO_COND_VERW to be safe within SPEC_CTRL_EXIT_TO_XEN.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>

This was decided not to be XSA-worthy, as guests can't usefully control when IST events occur.

---
 xen/arch/x86/include/asm/spec_ctrl_asm.h | 35 ++++++++++++++++++++----
 1 file changed, 30 insertions(+), 5 deletions(-)
xen/arch/x86/include/asm/spec_ctrl_asm.h +30 -5
index ee75f2bced42..77f6c35bb9c5 100644
@@ -218,7 +218,10 @@
     wrmsr
 .endm
 
-/* Use after an entry from PV context (syscall/sysenter/int80/int82/etc). */
+/*
+ * Used after a synchronous entry from PV context.  SYSCALL, SYSENTER, INT,
+ * etc.  Will always interrupt a guest speculation context.
+ */
 .macro SPEC_CTRL_ENTRY_FROM_PV
 /*
  * Requires %rsp=regs/cpuinfo, %rdx=0
@@ -233,7 +236,11 @@
         X86_FEATURE_SC_MSR_PV
 .endm
 
-/* Use in interrupt/exception context.  May interrupt Xen or PV context. */
+/*
+ * Used after a synchronous interrupt or exception.  May interrupt Xen or PV
+ * context, but will not interrupt Xen with a guest speculation context,
+ * outside of fatal error cases.
+ */
 .macro SPEC_CTRL_ENTRY_FROM_INTR
 /*
  * Requires %rsp=regs, %r14=stack_end, %rdx=0
@@ -248,7 +255,10 @@
         X86_FEATURE_SC_MSR_PV
 .endm
 
-/* Use when exiting to PV guest context. */
+/*
+ * Used when exiting from any entry context, back to PV context.  This
+ * includes from an IST entry which moved onto the primary stack.
+ */
 .macro SPEC_CTRL_EXIT_TO_PV
 /*
  * Requires %rax=spec_ctrl, %rsp=regs/info
@@ -260,7 +270,12 @@
 .endm
 
 /*
- * Use in IST interrupt/exception context.  May interrupt Xen or PV context.
+ * Used after an IST entry (i.e. needs special care, consider to be fully
+ * asynchronous with finding sane state).  May interrupt PV or Xen context,
+ * including other SPEC_CTRL_{ENTRY,EXIT}_* regions with unsanitised state.
+ *
+ * An IST entry which interrupts PV context moves onto the primary stack and
+ * leaves via SPEC_CTRL_EXIT_TO_PV, *not* SPEC_CTRL_EXIT_TO_XEN.
  */
 .macro SPEC_CTRL_ENTRY_FROM_INTR_IST
 /*
@@ -319,7 +334,14 @@ UNLIKELY_DISPATCH_LABEL(\@_serialise):
     UNLIKELY_END(\@_serialise)
 .endm
 
-/* Use when exiting to Xen in IST context. */
+/*
+ * Use when exiting from any entry context, back to Xen context.  This
+ * includes returning to other SPEC_CTRL_{ENTRY,EXIT}_* regions with
+ * unsanitised state.
+ *
+ * Because we might have interrupted Xen beyond SPEC_CTRL_EXIT_TO_$GUEST, we
+ * must treat this as if it were an EXIT_TO_$GUEST case too.
+ */
 .macro SPEC_CTRL_EXIT_TO_XEN
 /*
  * Requires %rbx=stack_end
@@ -344,6 +366,9 @@ UNLIKELY_DISPATCH_LABEL(\@_serialise):
     wrmsr
 
 .L\@_skip_sc_msr:
+
+    /* TODO VERW */
+
 .endm
 
 #endif /* __ASSEMBLY__ */
Signature · 1 line
2.30.2
#9 lore raw

Re: [PATCH 4/8] x86/spec-ctrl: Extend all SPEC_CTRL_{ENTER,EXIT}_* comments

Quoted 4 lines · Andrew Cooper
xen/arch/x86/include/asm/spec_ctrl_asm.h +0 -0
@@ -218,7 +218,10 @@
     wrmsr

xen/arch/x86/include/asm/spec_ctrl_asm.h line 227

Quoted 10 lines
xen/arch/x86/include/asm/spec_ctrl_asm.h +4 -1
 .endm
 
-/* Use after an entry from PV context (syscall/sysenter/int80/int82/etc). */
+/*
+ * Used after a synchronous entry from PV context.  SYSCALL, SYSENTER, INT,
+ * etc.  Will always interrupt a guest speculation context.
+ */
 .macro SPEC_CTRL_ENTRY_FROM_PV
 /*
  * Requires %rsp=regs/cpuinfo, %rdx=0

For the entry point comments - not being a native speaker -, the use of "{will,may} interrupt" reads odd. You're describing the macros here, not the the events that led to their invocation. Therefore it would seem to me that "{will,may} have interrupted" would be more appropriate.

xen/arch/x86/include/asm/spec_ctrl_asm.h line 246

Quoted 13 lines
xen/arch/x86/include/asm/spec_ctrl_asm.h +5 -1
@@ -233,7 +236,11 @@
         X86_FEATURE_SC_MSR_PV
 .endm
 
-/* Use in interrupt/exception context.  May interrupt Xen or PV context. */
+/*
+ * Used after a synchronous interrupt or exception.  May interrupt Xen or PV
+ * context, but will not interrupt Xen with a guest speculation context,
+ * outside of fatal error cases.
+ */
 .macro SPEC_CTRL_ENTRY_FROM_INTR
 /*
  * Requires %rsp=regs, %r14=stack_end, %rdx=0

I find "synchronous" here odd, when this includes interrupts. Below you at least qualify things further, by saying "fully asynchronous with finding sane state"; I think further qualification is warranted here as well, to avoid any ambiguity.

Quoted 6 lines
xen/arch/x86/include/asm/spec_ctrl_asm.h +1 -1
@@ -319,7 +334,14 @@ UNLIKELY_DISPATCH_LABEL(\@_serialise):
     UNLIKELY_END(\@_serialise)
 .endm
 
-/* Use when exiting to Xen in IST context. */
+/*

xen/arch/x86/include/asm/spec_ctrl_asm.h UNLIKELY_DISPATCH_LABEL(\@_serialise): · line 347

Quoted 10 lines
xen/arch/x86/include/asm/spec_ctrl_asm.h +7 -0
+ * Use when exiting from any entry context, back to Xen context.  This
+ * includes returning to other SPEC_CTRL_{ENTRY,EXIT}_* regions with
+ * unsanitised state.
+ *
+ * Because we might have interrupted Xen beyond SPEC_CTRL_EXIT_TO_$GUEST, we
+ * must treat this as if it were an EXIT_TO_$GUEST case too.
+ */
 .macro SPEC_CTRL_EXIT_TO_XEN
 /*
  * Requires %rbx=stack_end

Is it really "must"? At least in theory there are ways to recognize that exit is back to Xen context outside of interrupted entry/exit regions (simply by evaluating how far below stack top %rsp is).

xen/arch/x86/include/asm/spec_ctrl_asm.h UNLIKELY_DISPATCH_LABEL(\@_serialise): · line 372

Quoted 8 lines
xen/arch/x86/include/asm/spec_ctrl_asm.h +3 -0
@@ -344,6 +366,9 @@ UNLIKELY_DISPATCH_LABEL(\@_serialise):
     wrmsr
 
 .L\@_skip_sc_msr:
+
+    /* TODO VERW */
+
 .endm

I don't think this comment is strictly necessary to add here, when the omission is addressed in a later patch. But I also don't mind its addition.

Jan

#10 lore raw

Re: [PATCH 4/8] x86/spec-ctrl: Extend all SPEC_CTRL_{ENTER,EXIT}_* comments

Quoted 7 lines · Jan Beulich
Quoted 6 lines · Andrew Cooper
xen/arch/x86/include/asm/spec_ctrl_asm.h +0 -0
@@ -218,7 +218,10 @@
     wrmsr
 .endm
 
Quoted 12 lines

xen/arch/x86/include/asm/spec_ctrl_asm.h line 227

Quoted 8 lines
xen/arch/x86/include/asm/spec_ctrl_asm.h +4 -1
-/* Use after an entry from PV context (syscall/sysenter/int80/int82/etc). */
+/*
+ * Used after a synchronous entry from PV context.  SYSCALL, SYSENTER, INT,
+ * etc.  Will always interrupt a guest speculation context.
+ */
 .macro SPEC_CTRL_ENTRY_FROM_PV
 /*
  * Requires %rsp=regs/cpuinfo, %rdx=0

For the entry point comments - not being a native speaker -, the use of "{will,may} interrupt" reads odd. You're describing the macros here, not the the events that led to their invocation. Therefore it would seem to me that "{will,may} have interrupted" would be more appropriate.

The salient information is what the speculation state looks like when we're running the asm in these macros.

Sync and Async perhaps aren't the best terms.  For PV context at least, it boils down to:

1) CPL>0 => you were in fully-good guest speculation context
2) CPL=0 => you were in fully-good Xen speculation context
3) IST && CPL=0 => Here be dragons.

HVM is more of a challenge.  VT-x behaves like an IST path, while SVM does allow us to atomically switch to good Xen state.

Really, this needs to be a separate doc, with diagrams...

Quoted 4 lines
Quoted 4 lines
xen/arch/x86/include/asm/spec_ctrl_asm.h +0 -0
@@ -319,7 +334,14 @@ UNLIKELY_DISPATCH_LABEL(\@_serialise):
     UNLIKELY_END(\@_serialise)
 .endm
 
Quoted 15 lines

xen/arch/x86/include/asm/spec_ctrl_asm.h UNLIKELY_DISPATCH_LABEL(\@_serialise): · line 347

Quoted 12 lines
xen/arch/x86/include/asm/spec_ctrl_asm.h +8 -1
-/* Use when exiting to Xen in IST context. */
+/*
+ * Use when exiting from any entry context, back to Xen context.  This
+ * includes returning to other SPEC_CTRL_{ENTRY,EXIT}_* regions with
+ * unsanitised state.
+ *
+ * Because we might have interrupted Xen beyond SPEC_CTRL_EXIT_TO_$GUEST, we
+ * must treat this as if it were an EXIT_TO_$GUEST case too.
+ */
 .macro SPEC_CTRL_EXIT_TO_XEN
 /*
  * Requires %rbx=stack_end

Is it really "must"? At least in theory there are ways to recognize that exit is back to Xen context outside of interrupted entry/exit regions (simply by evaluating how far below stack top %rsp is).

Yes, it is must - it's about how Xen behaves right now, not about some theoretical future with different tracking mechanism.

Checking the stack is very fragile and we've had bugs doing this in the past.  It would break completely if we were to take things such as the recursive-NMI fix (not that we're liable to at this point with FRED on the horizon.)

A perhaps less fragile option would be to have .text.entry.spec_suspect section and check %rip being in that.

But neither of these are good options.  It's adding complexity (latency) to a fastpath to avoid a small hit in a rare case, so is a concrete anti-optimisation.

Quoted 11 lines

xen/arch/x86/include/asm/spec_ctrl_asm.h UNLIKELY_DISPATCH_LABEL(\@_serialise): · line 372

Quoted 8 lines
xen/arch/x86/include/asm/spec_ctrl_asm.h +3 -0
@@ -344,6 +366,9 @@ UNLIKELY_DISPATCH_LABEL(\@_serialise):
     wrmsr
 
 .L\@_skip_sc_msr:
+
+    /* TODO VERW */
+
 .endm

I don't think this comment is strictly necessary to add here, when the omission is addressed in a later patch. But I also don't mind its addition.

It doesn't especially matter if the series gets committed in one go, but it does matter if it ends up being split.  This is the patch which observes that VERW is missing.

~Andrew

#11 lore raw

Re: [PATCH 4/8] x86/spec-ctrl: Extend all SPEC_CTRL_{ENTER,EXIT}_* comments

Quoted 56 lines · Andrew Cooper
Quoted 7 lines · Jan Beulich
Quoted 6 lines · Andrew Cooper
xen/arch/x86/include/asm/spec_ctrl_asm.h +0 -0
@@ -218,7 +218,10 @@
     wrmsr
 .endm
 
Quoted 12 lines

xen/arch/x86/include/asm/spec_ctrl_asm.h line 227

Quoted 8 lines
xen/arch/x86/include/asm/spec_ctrl_asm.h +4 -1
-/* Use after an entry from PV context (syscall/sysenter/int80/int82/etc). */
+/*
+ * Used after a synchronous entry from PV context.  SYSCALL, SYSENTER, INT,
+ * etc.  Will always interrupt a guest speculation context.
+ */
 .macro SPEC_CTRL_ENTRY_FROM_PV
 /*
  * Requires %rsp=regs/cpuinfo, %rdx=0

For the entry point comments - not being a native speaker -, the use of "{will,may} interrupt" reads odd. You're describing the macros here, not the the events that led to their invocation. Therefore it would seem to me that "{will,may} have interrupted" would be more appropriate.

The salient information is what the speculation state looks like when we're running the asm in these macros.

Sync and Async perhaps aren't the best terms.  For PV context at least, it boils down to:

1) CPL>0 => you were in fully-good guest speculation context
2) CPL=0 => you were in fully-good Xen speculation context
3) IST && CPL=0 => Here be dragons.

HVM is more of a challenge.  VT-x behaves like an IST path, while SVM does allow us to atomically switch to good Xen state.

Really, this needs to be a separate doc, with diagrams...

Quoted 4 lines
Quoted 4 lines
xen/arch/x86/include/asm/spec_ctrl_asm.h +0 -0
@@ -319,7 +334,14 @@ UNLIKELY_DISPATCH_LABEL(\@_serialise):
     UNLIKELY_END(\@_serialise)
 .endm
 
Quoted 15 lines

xen/arch/x86/include/asm/spec_ctrl_asm.h UNLIKELY_DISPATCH_LABEL(\@_serialise): · line 347

Quoted 12 lines
xen/arch/x86/include/asm/spec_ctrl_asm.h +8 -1
-/* Use when exiting to Xen in IST context. */
+/*
+ * Use when exiting from any entry context, back to Xen context.  This
+ * includes returning to other SPEC_CTRL_{ENTRY,EXIT}_* regions with
+ * unsanitised state.
+ *
+ * Because we might have interrupted Xen beyond SPEC_CTRL_EXIT_TO_$GUEST, we
+ * must treat this as if it were an EXIT_TO_$GUEST case too.
+ */
 .macro SPEC_CTRL_EXIT_TO_XEN
 /*
  * Requires %rbx=stack_end

Is it really "must"? At least in theory there are ways to recognize that exit is back to Xen context outside of interrupted entry/exit regions (simply by evaluating how far below stack top %rsp is).

Quoted 2 lines

Yes, it is must - it's about how Xen behaves right now, not about some theoretical future with different tracking mechanism.

Well, deleting "must" does exactly that: Describe what we currently do, without imposing that it necessarily has to be that way. That's at least to me, as an - as said - non-native speaker.

Quoted 11 lines

Checking the stack is very fragile and we've had bugs doing this in the past.  It would break completely if we were to take things such as the recursive-NMI fix (not that we're liable to at this point with FRED on the horizon.)

A perhaps less fragile option would be to have .text.entry.spec_suspect section and check %rip being in that.

But neither of these are good options.  It's adding complexity (latency) to a fastpath to avoid a small hit in a rare case, so is a concrete anti-optimisation.

I fully accept all of this, and I wasn't meaning to suggest we do what might be possible. I merely dislike stronger than necessary statements, as such are liable to cause confusion down the road.

That said, I certainly won't refuse to eventually ack this patch just because of this one word.

Jan

#12 lore raw

Re: [PATCH 4/8] x86/spec-ctrl: Extend all SPEC_CTRL_{ENTER,EXIT}_* comments

Quoted 22 lines · Jan Beulich
Quoted 21 lines · Andrew Cooper
Quoted 20 lines · Jan Beulich
Quoted 6 lines · Andrew Cooper
xen/arch/x86/include/asm/spec_ctrl_asm.h +1 -1
@@ -319,7 +334,14 @@ UNLIKELY_DISPATCH_LABEL(\@_serialise):
     UNLIKELY_END(\@_serialise)
 .endm
 
-/* Use when exiting to Xen in IST context. */
+/*

xen/arch/x86/include/asm/spec_ctrl_asm.h UNLIKELY_DISPATCH_LABEL(\@_serialise): · line 347

Quoted 10 lines
xen/arch/x86/include/asm/spec_ctrl_asm.h +7 -0
+ * Use when exiting from any entry context, back to Xen context.  This
+ * includes returning to other SPEC_CTRL_{ENTRY,EXIT}_* regions with
+ * unsanitised state.
+ *
+ * Because we might have interrupted Xen beyond SPEC_CTRL_EXIT_TO_$GUEST, we
+ * must treat this as if it were an EXIT_TO_$GUEST case too.
+ */
 .macro SPEC_CTRL_EXIT_TO_XEN
 /*
  * Requires %rbx=stack_end

Is it really "must"? At least in theory there are ways to recognize that exit is back to Xen context outside of interrupted entry/exit regions (simply by evaluating how far below stack top %rsp is).

Quoted 3 lines
Quoted 2 lines

Yes, it is must - it's about how Xen behaves right now, not about some theoretical future with different tracking mechanism.

Well, deleting "must" does exactly that

Nonsense.

*When* someone changes the logic such that there's an alternative route, the comment necessarily needs updating.  And until that point, the logic *must* behave in this way to be correct.

~Andrew

#13 lore raw

PATCH 5/8 x86/entry: Adjust restore_all_xen to hold stack_end in %r14

In reply to Andrew Cooper

All other SPEC_CTRL_{ENTRY,EXIT}_* helpers hold stack_end in %r14. Adjust it for consistency, freeing up %rbx to be used differently.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>

---
 xen/arch/x86/include/asm/spec_ctrl_asm.h | 8 ++++----
 xen/arch/x86/x86_64/entry.S              | 8 ++++----
 2 files changed, 8 insertions(+), 8 deletions(-)
xen/arch/x86/include/asm/spec_ctrl_asm.h +4 -4
index 77f6c35bb9c5..acdb526d292d 100644
@@ -344,10 +344,10 @@ UNLIKELY_DISPATCH_LABEL(\@_serialise):
  */
 .macro SPEC_CTRL_EXIT_TO_XEN
 /*
- * Requires %rbx=stack_end
+ * Requires %r14=stack_end
  * Clobbers %rax, %rcx, %rdx
  */
-    testb $SCF_ist_sc_msr, STACK_CPUINFO_FIELD(spec_ctrl_flags)(%rbx)
+    testb $SCF_ist_sc_msr, STACK_CPUINFO_FIELD(spec_ctrl_flags)(%r14)
     jz .L\@_skip_sc_msr
 
     /*
@@ -358,10 +358,10 @@ UNLIKELY_DISPATCH_LABEL(\@_serialise):
      */
     xor %edx, %edx
 
-    testb $SCF_use_shadow, STACK_CPUINFO_FIELD(spec_ctrl_flags)(%rbx)
+    testb $SCF_use_shadow, STACK_CPUINFO_FIELD(spec_ctrl_flags)(%r14)
     jz .L\@_skip_sc_msr
 
-    mov STACK_CPUINFO_FIELD(shadow_spec_ctrl)(%rbx), %eax
+    mov STACK_CPUINFO_FIELD(shadow_spec_ctrl)(%r14), %eax
     mov $MSR_SPEC_CTRL, %ecx
     wrmsr
 
xen/arch/x86/x86_64/entry.S +4 -4
index a1c860f56949..525877e97330 100644
@@ -665,15 +665,15 @@ restore_all_xen:
          * Check whether we need to switch to the per-CPU page tables, in
          * case we return to late PV exit code (from an NMI or #MC).
          */
-        GET_STACK_END(bx)
-        cmpb  $0, STACK_CPUINFO_FIELD(use_pv_cr3)(%rbx)
+        GET_STACK_END(14)
+        cmpb  $0, STACK_CPUINFO_FIELD(use_pv_cr3)(%r14)
 UNLIKELY_START(ne, exit_cr3)
-        mov   STACK_CPUINFO_FIELD(pv_cr3)(%rbx), %rax
+        mov   STACK_CPUINFO_FIELD(pv_cr3)(%r14), %rax
         mov   %rax, %cr3
 UNLIKELY_END(exit_cr3)
 
         /* WARNING! `ret`, `call *`, `jmp *` not safe beyond this point. */
-        SPEC_CTRL_EXIT_TO_XEN     /* Req: %rbx=end, Clob: acd */
+        SPEC_CTRL_EXIT_TO_XEN     /* Req: %r14=end, Clob: acd */
 
         RESTORE_ALL adj=8
         iretq
Signature · 1 line
2.30.2
Reviewed-by on v1 5/8
#14 lore raw

Re: [PATCH 5/8] x86/entry: Adjust restore_all_xen to hold stack_end in %r14

Quoted 4 lines · Andrew Cooper

All other SPEC_CTRL_{ENTRY,EXIT}_* helpers hold stack_end in %r14. Adjust it for consistency, freeing up %rbx to be used differently.

The choice of %rbx was, iirc, for avoiding unnecessary code size increase (due to extra REX prefixes needed in some of the affected insns). I understand you view this as not a real concern, but I think saying so explicitly in the description would be at least desirable. After all there would also be the alternative of further parametrizing the involved macros.

Jan

#15 lore raw

Re: [PATCH 5/8] x86/entry: Adjust restore_all_xen to hold stack_end in %r14

Quoted 4 lines · Jan Beulich
Quoted 3 lines · Andrew Cooper

All other SPEC_CTRL_{ENTRY,EXIT}_* helpers hold stack_end in %r14. Adjust it for consistency, freeing up %rbx to be used differently.

Quoted 8 lines
Quoted 1 line

The choice of %rbx was, iirc, for avoiding unnecessary code size increase (due to extra REX prefixes needed in some of the affected insns). I understand you view this as not a real concern, but I think saying so explicitly in the description would be at least desirable. After all there would also be the alternative of further parametrizing the involved macros.

Yeah I'm seriously not concerned about a REX prefix or two.

This is about making all the helpers more consistent, to cut down on the number of errors involved in modifying this mess.

~Andrew

#16 lore raw

PATCH 6/8 x86/entry: Track the IST-ness of an entry for the exit paths

In reply to Andrew Cooper

Use %r12 to hold an ist_exit boolean. This register is zero elsewhere in the entry/exit asm, so it only needs setting in the IST path.

As this is subtle and fragile, add check_ist_exit() to be used in debugging builds to cross-check that the ist_exit boolean matches the entry vector.

Write check_ist_exit() it in C, because it's debug only and the logic more complicated than I care about maintaining in asm.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>

---
 xen/arch/x86/traps.c               | 13 +++++++++++++
 xen/arch/x86/x86_64/compat/entry.S |  9 ++++++++-
 xen/arch/x86/x86_64/entry.S        | 23 ++++++++++++++++++++---
 3 files changed, 41 insertions(+), 4 deletions(-)
xen/arch/x86/traps.c +13 -0
index dead728ce329..0a005f088bca 100644
@@ -2259,6 +2259,19 @@ void asm_domain_crash_synchronous(unsigned long addr)
         do_softirq();
 }
 
+#ifdef CONFIG_DEBUG
+void check_ist_exit(const struct cpu_user_regs *regs, bool ist_exit)
+{
+    const unsigned int ist_mask =
+        (1U << X86_EXC_NMI) | (1U << X86_EXC_DB) |
+        (1U << X86_EXC_DF)  | (1U << X86_EXC_MC);
+    uint8_t ev = regs->entry_vector;
+    bool is_ist = (ev < X86_EXC_NUM) && ((1U << ev) & ist_mask);
+
+    ASSERT(is_ist == ist_exit);
+}
+#endif
+
 /*
  * Local variables:
  * mode: C
xen/arch/x86/x86_64/compat/entry.S +8 -1
index bd5abd8040bd..7504bfb4f326 100644
@@ -117,8 +117,15 @@ compat_process_trap:
         call  compat_create_bounce_frame
         jmp   compat_test_all_events
 
-/* %rbx: struct vcpu, interrupts disabled */
+/* %rbx: struct vcpu, %r12: ist_exit, interrupts disabled */
 ENTRY(compat_restore_all_guest)
+
+#ifdef CONFIG_DEBUG
+        mov   %rsp, %rdi
+        mov   %r12, %rsi
+        call  check_ist_exit
+#endif
+
         ASSERT_INTERRUPTS_DISABLED
         mov   $~(X86_EFLAGS_IOPL | X86_EFLAGS_VM), %r11d
         and   UREGS_eflags(%rsp),%r11d
xen/arch/x86/x86_64/entry.S +20 -3
index 525877e97330..da084a7e8e54 100644
@@ -142,10 +142,16 @@ process_trap:
 
         .section .text.entry, "ax", @progbits
 
-/* %rbx: struct vcpu, interrupts disabled */
+/* %rbx: struct vcpu, %r12: ist_exit, interrupts disabled */
 restore_all_guest:
-        ASSERT_INTERRUPTS_DISABLED
 
+#ifdef CONFIG_DEBUG
+        mov   %rsp, %rdi
+        mov   %r12, %rsi
+        call  check_ist_exit
+#endif
+
+        ASSERT_INTERRUPTS_DISABLED
         /* Stash guest SPEC_CTRL value while we can read struct vcpu. */
         mov VCPU_arch_msrs(%rbx), %rdx
         mov VCPUMSR_spec_ctrl_raw(%rdx), %r15d
@@ -659,8 +665,15 @@ ENTRY(early_page_fault)
         .section .text.entry, "ax", @progbits
 
         ALIGN
-/* No special register assumptions. */
+/* %r12=ist_exit */
 restore_all_xen:
+
+#ifdef CONFIG_DEBUG
+        mov   %rsp, %rdi
+        mov   %r12, %rsi
+        call  check_ist_exit
+#endif
+
         /*
          * Check whether we need to switch to the per-CPU page tables, in
          * case we return to late PV exit code (from an NMI or #MC).
@@ -1087,6 +1100,10 @@ handle_ist_exception:
 .L_ist_dispatch_done:
         mov   %r15, STACK_CPUINFO_FIELD(xen_cr3)(%r14)
         mov   %bl, STACK_CPUINFO_FIELD(use_pv_cr3)(%r14)
+
+        /* This is an IST exit */
+        mov   $1, %r12
+
         cmpb  $X86_EXC_NMI, UREGS_entry_vector(%rsp)
         jne   ret_from_intr
 
Signature · 1 line
2.30.2
#17 lore raw

Re: [PATCH 6/8] x86/entry: Track the IST-ness of an entry for the exit paths

Quoted 9 lines · Andrew Cooper
xen/arch/x86/x86_64/compat/entry.S +1 -1
@@ -117,8 +117,15 @@ compat_process_trap:
         call  compat_create_bounce_frame
         jmp   compat_test_all_events
 
-/* %rbx: struct vcpu, interrupts disabled */
+/* %rbx: struct vcpu, %r12: ist_exit, interrupts disabled */
 ENTRY(compat_restore_all_guest)

xen/arch/x86/x86_64/compat/entry.S compat_process_trap: · line 131

Quoted 10 lines
xen/arch/x86/x86_64/compat/entry.S +7 -0
+
+#ifdef CONFIG_DEBUG
+        mov   %rsp, %rdi
+        mov   %r12, %rsi
+        call  check_ist_exit
+#endif
+
         ASSERT_INTERRUPTS_DISABLED
         mov   $~(X86_EFLAGS_IOPL | X86_EFLAGS_VM), %r11d
         and   UREGS_eflags(%rsp),%r11d

Without having peeked ahead, is there any use of %r12 going to appear on this path? I thought it's only going to be restore_all_xen?

Quoted 11 lines
xen/arch/x86/x86_64/entry.S +1 -2
@@ -142,10 +142,16 @@ process_trap:
 
         .section .text.entry, "ax", @progbits
 
-/* %rbx: struct vcpu, interrupts disabled */
+/* %rbx: struct vcpu, %r12: ist_exit, interrupts disabled */
 restore_all_guest:
-        ASSERT_INTERRUPTS_DISABLED
 

xen/arch/x86/x86_64/entry.S process_trap: · line 157

Quoted 10 lines
xen/arch/x86/x86_64/entry.S +7 -0
+#ifdef CONFIG_DEBUG
+        mov   %rsp, %rdi
+        mov   %r12, %rsi
+        call  check_ist_exit
+#endif
+
+        ASSERT_INTERRUPTS_DISABLED
         /* Stash guest SPEC_CTRL value while we can read struct vcpu. */
         mov VCPU_arch_msrs(%rbx), %rdx
         mov VCPUMSR_spec_ctrl_raw(%rdx), %r15d

Even here I don't think I see a need for the addition. Plus if the check is warranted here, is it really necessary for it to live ahead of the interrupts-disabled check? Further, seeing that being marco-ized, would it make sense to have a CHECK_IST_EXIT macro in case more than a single use site remains?

xen/arch/x86/x86_64/entry.S handle_ist_exception: · line 1108

Quoted 10 lines
xen/arch/x86/x86_64/entry.S +4 -0
@@ -1087,6 +1100,10 @@ handle_ist_exception:
 .L_ist_dispatch_done:
         mov   %r15, STACK_CPUINFO_FIELD(xen_cr3)(%r14)
         mov   %bl, STACK_CPUINFO_FIELD(use_pv_cr3)(%r14)
+
+        /* This is an IST exit */
+        mov   $1, %r12
+
         cmpb  $X86_EXC_NMI, UREGS_entry_vector(%rsp)
         jne   ret_from_intr

Could I talk you into using a less than 7-byte insn here? "or $1, %r12" would be 4 bytes, "mov $1, %r12b", "inc %r12", and "not %r12" would be just 3. All have certain downsides, yes, but I wonder whether switching isn't worth it. Even "mov $1, %r12d" would be at least one byte less, without any downsides. And the OR and INC variants would allow the remaining 63 bits to be used for another purpose down the road.

Jan

#18 lore raw

Re: [PATCH 6/8] x86/entry: Track the IST-ness of an entry for the exit paths

Quoted 7 lines · Jan Beulich
Quoted 6 lines · Andrew Cooper
xen/arch/x86/x86_64/compat/entry.S +0 -0
@@ -117,8 +117,15 @@ compat_process_trap:
         call  compat_create_bounce_frame
         jmp   compat_test_all_events
 
Quoted 15 lines

xen/arch/x86/x86_64/compat/entry.S compat_process_trap: · line 131

Quoted 13 lines
xen/arch/x86/x86_64/compat/entry.S +8 -1
-/* %rbx: struct vcpu, interrupts disabled */
+/* %rbx: struct vcpu, %r12: ist_exit, interrupts disabled */
 ENTRY(compat_restore_all_guest)
+
+#ifdef CONFIG_DEBUG
+        mov   %rsp, %rdi
+        mov   %r12, %rsi
+        call  check_ist_exit
+#endif
+
         ASSERT_INTERRUPTS_DISABLED
         mov   $~(X86_EFLAGS_IOPL | X86_EFLAGS_VM), %r11d
         and   UREGS_eflags(%rsp),%r11d

Without having peeked ahead, is there any use of %r12 going to appear on this path? I thought it's only going to be restore_all_xen?

For now, we only need to change behaviour based on ist_exit in restore_all_xen.

But, we do get here in IST context, and I'm not interested in having to re-do the analysis to determine if this is safe.  ist_exit is a global property of exiting Xen, so should be kept correct from the outset.

Quoted 12 lines
Quoted 12 lines
xen/arch/x86/x86_64/entry.S +1 -2
@@ -142,10 +142,16 @@ process_trap:
 
         .section .text.entry, "ax", @progbits
 
-/* %rbx: struct vcpu, interrupts disabled */
+/* %rbx: struct vcpu, %r12: ist_exit, interrupts disabled */
 restore_all_guest:
-        ASSERT_INTERRUPTS_DISABLED
 
Quoted 13 lines

xen/arch/x86/x86_64/entry.S process_trap: · line 157

Quoted 10 lines
xen/arch/x86/x86_64/entry.S +7 -0
+#ifdef CONFIG_DEBUG
+        mov   %rsp, %rdi
+        mov   %r12, %rsi
+        call  check_ist_exit
+#endif
+
+        ASSERT_INTERRUPTS_DISABLED
         /* Stash guest SPEC_CTRL value while we can read struct vcpu. */
         mov VCPU_arch_msrs(%rbx), %rdx
         mov VCPUMSR_spec_ctrl_raw(%rdx), %r15d

Even here I don't think I see a need for the addition. Plus if the check is warranted here, is it really necessary for it to live ahead of the interrupts-disabled check?

What makes you think there is a relevance to the order of two assertions in fully irqs-off code?

The checks are in the same order as the comment stating the invariants.

Quoted 4 lines

Further, seeing that being marco-ized, would
it make sense to have a CHECK_IST_EXIT macro in case more than a single use site remains?

Quoted 16 lines

xen/arch/x86/x86_64/entry.S handle_ist_exception: · line 1108

Quoted 10 lines
xen/arch/x86/x86_64/entry.S +4 -0
@@ -1087,6 +1100,10 @@ handle_ist_exception:
 .L_ist_dispatch_done:
         mov   %r15, STACK_CPUINFO_FIELD(xen_cr3)(%r14)
         mov   %bl, STACK_CPUINFO_FIELD(use_pv_cr3)(%r14)
+
+        /* This is an IST exit */
+        mov   $1, %r12
+
         cmpb  $X86_EXC_NMI, UREGS_entry_vector(%rsp)
         jne   ret_from_intr

Could I talk you into using a less than 7-byte insn here? "or $1, %r12" would be 4 bytes, "mov $1, %r12b", "inc %r12", and "not %r12" would be just 3. All have certain downsides, yes, but I wonder whether switching isn't worth it. Even "mov $1, %r12d" would be at least one byte less, without any downsides. And the OR and INC variants would allow the remaining 63 bits to be used for another purpose down the road.

This is a 2Hz-at-most path.  The size of one instruction is not something to care about.

But I did mean to use the %r12d form, so I'll go with that.  Everything else depends on the behaviour of earlier logic.

~Andrew

#19 lore raw

Re: [PATCH 6/8] x86/entry: Track the IST-ness of an entry for the exit paths

Quoted 24 lines · Andrew Cooper
Quoted 7 lines · Jan Beulich
Quoted 6 lines · Andrew Cooper
xen/arch/x86/x86_64/compat/entry.S +0 -0
@@ -117,8 +117,15 @@ compat_process_trap:
         call  compat_create_bounce_frame
         jmp   compat_test_all_events
 
Quoted 15 lines

xen/arch/x86/x86_64/compat/entry.S compat_process_trap: · line 131

Quoted 13 lines
xen/arch/x86/x86_64/compat/entry.S +8 -1
-/* %rbx: struct vcpu, interrupts disabled */
+/* %rbx: struct vcpu, %r12: ist_exit, interrupts disabled */
 ENTRY(compat_restore_all_guest)
+
+#ifdef CONFIG_DEBUG
+        mov   %rsp, %rdi
+        mov   %r12, %rsi
+        call  check_ist_exit
+#endif
+
         ASSERT_INTERRUPTS_DISABLED
         mov   $~(X86_EFLAGS_IOPL | X86_EFLAGS_VM), %r11d
         and   UREGS_eflags(%rsp),%r11d

Without having peeked ahead, is there any use of %r12 going to appear on this path? I thought it's only going to be restore_all_xen?

Quoted 6 lines

For now, we only need to change behaviour based on ist_exit in restore_all_xen.

But, we do get here in IST context, and I'm not interested in having to re-do the analysis to determine if this is safe.  ist_exit is a global property of exiting Xen, so should be kept correct from the outset.

Would be nice to mention this just-in-case aspect in the description.

Quoted 25 lines
Quoted 11 lines
Quoted 11 lines
xen/arch/x86/x86_64/entry.S +1 -2
@@ -142,10 +142,16 @@ process_trap:
 
         .section .text.entry, "ax", @progbits
 
-/* %rbx: struct vcpu, interrupts disabled */
+/* %rbx: struct vcpu, %r12: ist_exit, interrupts disabled */
 restore_all_guest:
-        ASSERT_INTERRUPTS_DISABLED
 
Quoted 13 lines

xen/arch/x86/x86_64/entry.S process_trap: · line 157

Quoted 10 lines
xen/arch/x86/x86_64/entry.S +7 -0
+#ifdef CONFIG_DEBUG
+        mov   %rsp, %rdi
+        mov   %r12, %rsi
+        call  check_ist_exit
+#endif
+
+        ASSERT_INTERRUPTS_DISABLED
         /* Stash guest SPEC_CTRL value while we can read struct vcpu. */
         mov VCPU_arch_msrs(%rbx), %rdx
         mov VCPUMSR_spec_ctrl_raw(%rdx), %r15d

Even here I don't think I see a need for the addition. Plus if the check is warranted here, is it really necessary for it to live ahead of the interrupts-disabled check?

Quoted 2 lines

What makes you think there is a relevance to the order of two assertions in fully irqs-off code?

You explicitly making it more churn than strictly needed. IOW I was simply wondering whether I was overlooking some aspect.

Quoted 1 line

The checks are in the same order as the comment stating the invariants.

If that's the only criteria, then okay (but still slightly odd to see more churn than necessary).

Jan

#20 lore raw

Re: [PATCH 6/8] x86/entry: Track the IST-ness of an entry for the exit paths

Quoted 27 lines · Jan Beulich
Quoted 26 lines · Andrew Cooper
Quoted 25 lines · Jan Beulich
Quoted 11 lines · Andrew Cooper
xen/arch/x86/x86_64/entry.S +1 -2
@@ -142,10 +142,16 @@ process_trap:
 
         .section .text.entry, "ax", @progbits
 
-/* %rbx: struct vcpu, interrupts disabled */
+/* %rbx: struct vcpu, %r12: ist_exit, interrupts disabled */
 restore_all_guest:
-        ASSERT_INTERRUPTS_DISABLED
 

xen/arch/x86/x86_64/entry.S process_trap: · line 157

Quoted 10 lines
xen/arch/x86/x86_64/entry.S +7 -0
+#ifdef CONFIG_DEBUG
+        mov   %rsp, %rdi
+        mov   %r12, %rsi
+        call  check_ist_exit
+#endif
+
+        ASSERT_INTERRUPTS_DISABLED
         /* Stash guest SPEC_CTRL value while we can read struct vcpu. */
         mov VCPU_arch_msrs(%rbx), %rdx
         mov VCPUMSR_spec_ctrl_raw(%rdx), %r15d

Even here I don't think I see a need for the addition. Plus if the check is warranted here, is it really necessary for it to live ahead of the interrupts-disabled check?

Quoted 4 lines
Quoted 2 lines

What makes you think there is a relevance to the order of two assertions in fully irqs-off code?

You explicitly making it more churn than strictly needed. IOW I was simply wondering whether I was overlooking some aspect.

That was just the diff algorithm after accidentally removing the newline after the ASSERT.  I've undone that, and the hunk is simple additions for the check, like it is in the other two hunks.

~Andrew

#21 lore raw

PATCH 7/8 x86/spec-ctrl: Issue VERW during IST exit to Xen

In reply to Andrew Cooper

There is a corner case where e.g. an NMI hitting an exit-to-guest path after SPEC_CTRL_EXIT_TO_* would have run the entire NMI handler *after* the VERW flush to scrub potentially sensitive data from uarch buffers.

In order to compensate, issue VERW when exiting to Xen from an IST entry.

SPEC_CTRL_EXIT_TO_XEN already has two reads of spec_ctrl_flags off the stack, and we're about to add a third. Load the field into %ebx, and list the register as clobbered.

%r12 has been arranged to be the ist_exit signal, so add this as an input dependency and use it to identify when to issue a VERW.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>

---
 xen/arch/x86/include/asm/spec_ctrl_asm.h | 20 +++++++++++++++-----
 xen/arch/x86/x86_64/entry.S              |  2 +-
 2 files changed, 16 insertions(+), 6 deletions(-)
xen/arch/x86/include/asm/spec_ctrl_asm.h +15 -5
index acdb526d292d..9740697114ad 100644
@@ -344,10 +344,12 @@ UNLIKELY_DISPATCH_LABEL(\@_serialise):
  */
 .macro SPEC_CTRL_EXIT_TO_XEN
 /*
- * Requires %r14=stack_end
- * Clobbers %rax, %rcx, %rdx
+ * Requires %r12=ist_exit, %r14=stack_end
+ * Clobbers %rax, %rbx, %rcx, %rdx
  */
-    testb $SCF_ist_sc_msr, STACK_CPUINFO_FIELD(spec_ctrl_flags)(%r14)
+    movzbl STACK_CPUINFO_FIELD(spec_ctrl_flags)(%r14), %ebx
+
+    testb $SCF_ist_sc_msr, %bl
     jz .L\@_skip_sc_msr
 
     /*
@@ -358,7 +360,7 @@ UNLIKELY_DISPATCH_LABEL(\@_serialise):
      */
     xor %edx, %edx
 
-    testb $SCF_use_shadow, STACK_CPUINFO_FIELD(spec_ctrl_flags)(%r14)
+    testb $SCF_use_shadow, %bl
     jz .L\@_skip_sc_msr
 
     mov STACK_CPUINFO_FIELD(shadow_spec_ctrl)(%r14), %eax
@@ -367,8 +369,16 @@ UNLIKELY_DISPATCH_LABEL(\@_serialise):
 
 .L\@_skip_sc_msr:
 
-    /* TODO VERW */
+    test %r12, %r12
+    jz .L\@_skip_ist_exit
+
+    /* Logically DO_SPEC_CTRL_COND_VERW but without the %rsp=cpuinfo dependency */
+    testb $SCF_verw, %bl
+    jz .L\@_verw_skip
+    verw STACK_CPUINFO_FIELD(verw_sel)(%r14)
+.L\@_verw_skip:
 
+.L\@_skip_ist_exit:
 .endm
 
 #endif /* __ASSEMBLY__ */
xen/arch/x86/x86_64/entry.S +1 -1
index da084a7e8e54..f70752fa36c1 100644
@@ -686,7 +686,7 @@ UNLIKELY_START(ne, exit_cr3)
 UNLIKELY_END(exit_cr3)
 
         /* WARNING! `ret`, `call *`, `jmp *` not safe beyond this point. */
-        SPEC_CTRL_EXIT_TO_XEN     /* Req: %r14=end, Clob: acd */
+        SPEC_CTRL_EXIT_TO_XEN     /* Req: %r12=ist_exit %r14=end, Clob: abcd */
 
         RESTORE_ALL adj=8
         iretq
Signature · 1 line
2.30.2
#22 lore raw

Re: [PATCH 7/8] x86/spec-ctrl: Issue VERW during IST exit to Xen

Quoted 6 lines · Andrew Cooper

There is a corner case where e.g. an NMI hitting an exit-to-guest path after SPEC_CTRL_EXIT_TO_* would have run the entire NMI handler *after* the VERW flush to scrub potentially sensitive data from uarch buffers.

In order to compensate, issue VERW when exiting to Xen from an IST entry.

Quoted 8 lines

SPEC_CTRL_EXIT_TO_XEN already has two reads of spec_ctrl_flags off the stack, and we're about to add a third. Load the field into %ebx, and list the register as clobbered.

%r12 has been arranged to be the ist_exit signal, so add this as an input dependency and use it to identify when to issue a VERW.

While looking technically okay, I still have a couple of remarks:

xen/arch/x86/include/asm/spec_ctrl_asm.h UNLIKELY_DISPATCH_LABEL(\@_serialise): · line 348

Quoted 10 lines
xen/arch/x86/include/asm/spec_ctrl_asm.h +2 -2
@@ -344,10 +344,12 @@ UNLIKELY_DISPATCH_LABEL(\@_serialise):
  */
 .macro SPEC_CTRL_EXIT_TO_XEN
 /*
- * Requires %r14=stack_end
- * Clobbers %rax, %rcx, %rdx
+ * Requires %r12=ist_exit, %r14=stack_end
+ * Clobbers %rax, %rbx, %rcx, %rdx

While I'd generally be a little concerned of the growing dependency and clobber lists, there being a single use site makes this pretty okay. The macro invocation being immediately followed by RESTORE_ALL effectively means we can clobber almost everything here.

As to register usage and my comment on patch 5: There's no real need to switch %rbx to %r14 there if I'm not mistaken; in particular you don't re-use any of the other macros which require use of %r14. You could as well use %r14b (or about any other register that isn't already used for something, yet whichever was picked apparently wouldn't make a difference) for the flags here, getting away with fewer new REX prefixes overall.

Quoted 20 lines
xen/arch/x86/include/asm/spec_ctrl_asm.h +4 -2
  */
-    testb $SCF_ist_sc_msr, STACK_CPUINFO_FIELD(spec_ctrl_flags)(%r14)
+    movzbl STACK_CPUINFO_FIELD(spec_ctrl_flags)(%r14), %ebx
+
+    testb $SCF_ist_sc_msr, %bl
     jz .L\@_skip_sc_msr
 
     /*
@@ -358,7 +360,7 @@ UNLIKELY_DISPATCH_LABEL(\@_serialise):
      */
     xor %edx, %edx
 
-    testb $SCF_use_shadow, STACK_CPUINFO_FIELD(spec_ctrl_flags)(%r14)
+    testb $SCF_use_shadow, %bl
     jz .L\@_skip_sc_msr
 
     mov STACK_CPUINFO_FIELD(shadow_spec_ctrl)(%r14), %eax
@@ -367,8 +369,16 @@ UNLIKELY_DISPATCH_LABEL(\@_serialise):
 
 .L\@_skip_sc_msr:

xen/arch/x86/include/asm/spec_ctrl_asm.h UNLIKELY_DISPATCH_LABEL(\@_serialise): · line 379

Quoted 10 lines
xen/arch/x86/include/asm/spec_ctrl_asm.h +8 -1
 
-    /* TODO VERW */
+    test %r12, %r12
+    jz .L\@_skip_ist_exit
+
+    /* Logically DO_SPEC_CTRL_COND_VERW but without the %rsp=cpuinfo dependency */
+    testb $SCF_verw, %bl
+    jz .L\@_verw_skip
+    verw STACK_CPUINFO_FIELD(verw_sel)(%r14)
+.L\@_verw_skip:

Nit: .L\@_skip_verw would be more consistent with the other label names.

xen/arch/x86/include/asm/spec_ctrl_asm.h UNLIKELY_DISPATCH_LABEL(\@_serialise): · line 381

Quoted 1 line
xen/arch/x86/include/asm/spec_ctrl_asm.h +1 -0
+.L\@_skip_ist_exit:

I was going to ask why the separate label (and whether the two JZ above couldn't sensibly be folded), but the to both answer lies in the next patch.

Jan

#23 lore raw

Re: [PATCH 7/8] x86/spec-ctrl: Issue VERW during IST exit to Xen

Quoted 32 lines · Jan Beulich
Quoted 6 lines · Andrew Cooper

There is a corner case where e.g. an NMI hitting an exit-to-guest path after SPEC_CTRL_EXIT_TO_* would have run the entire NMI handler *after* the VERW flush to scrub potentially sensitive data from uarch buffers.

In order to compensate, issue VERW when exiting to Xen from an IST entry.

Quoted 8 lines

SPEC_CTRL_EXIT_TO_XEN already has two reads of spec_ctrl_flags off the stack, and we're about to add a third. Load the field into %ebx, and list the register as clobbered.

%r12 has been arranged to be the ist_exit signal, so add this as an input dependency and use it to identify when to issue a VERW.

While looking technically okay, I still have a couple of remarks:

xen/arch/x86/include/asm/spec_ctrl_asm.h UNLIKELY_DISPATCH_LABEL(\@_serialise): · line 348

Quoted 10 lines
xen/arch/x86/include/asm/spec_ctrl_asm.h +2 -2
@@ -344,10 +344,12 @@ UNLIKELY_DISPATCH_LABEL(\@_serialise):
  */
 .macro SPEC_CTRL_EXIT_TO_XEN
 /*
- * Requires %r14=stack_end
- * Clobbers %rax, %rcx, %rdx
+ * Requires %r12=ist_exit, %r14=stack_end
+ * Clobbers %rax, %rbx, %rcx, %rdx

While I'd generally be a little concerned of the growing dependency and clobber lists, there being a single use site makes this pretty okay. The macro invocation being immediately followed by RESTORE_ALL effectively means we can clobber almost everything here.

Quoted 2 lines

As to register usage and my comment on patch 5: There's no real need to switch %rbx to %r14 there if I'm not mistaken

As said, it's about consistency of the helpers.

Quoted 21 lines
Quoted 21 lines
xen/arch/x86/include/asm/spec_ctrl_asm.h +4 -2
  */
-    testb $SCF_ist_sc_msr, STACK_CPUINFO_FIELD(spec_ctrl_flags)(%r14)
+    movzbl STACK_CPUINFO_FIELD(spec_ctrl_flags)(%r14), %ebx
+
+    testb $SCF_ist_sc_msr, %bl
     jz .L\@_skip_sc_msr
 
     /*
@@ -358,7 +360,7 @@ UNLIKELY_DISPATCH_LABEL(\@_serialise):
      */
     xor %edx, %edx
 
-    testb $SCF_use_shadow, STACK_CPUINFO_FIELD(spec_ctrl_flags)(%r14)
+    testb $SCF_use_shadow, %bl
     jz .L\@_skip_sc_msr
 
     mov STACK_CPUINFO_FIELD(shadow_spec_ctrl)(%r14), %eax
@@ -367,8 +369,16 @@ UNLIKELY_DISPATCH_LABEL(\@_serialise):
 
 .L\@_skip_sc_msr:
 
Quoted 10 lines

xen/arch/x86/include/asm/spec_ctrl_asm.h UNLIKELY_DISPATCH_LABEL(\@_serialise): · line 379

Quoted 9 lines
xen/arch/x86/include/asm/spec_ctrl_asm.h +8 -1
-    /* TODO VERW */
+    test %r12, %r12
+    jz .L\@_skip_ist_exit
+
+    /* Logically DO_SPEC_CTRL_COND_VERW but without the %rsp=cpuinfo dependency */
+    testb $SCF_verw, %bl
+    jz .L\@_verw_skip
+    verw STACK_CPUINFO_FIELD(verw_sel)(%r14)
+.L\@_verw_skip:

Nit: .L\@_skip_verw would be more consistent with the other label names.

So it would.  I'll tweak.

~Andrew

Reviewed-by
#24 lore raw

Re: [PATCH 7/8] x86/spec-ctrl: Issue VERW during IST exit to Xen

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

There is a corner case where e.g. an NMI hitting an exit-to-guest path after SPEC_CTRL_EXIT_TO_* would have run the entire NMI handler *after* the VERW flush to scrub potentially sensitive data from uarch buffers.

In order to compensate, issue VERW when exiting to Xen from an IST entry.

Quoted 8 lines

SPEC_CTRL_EXIT_TO_XEN already has two reads of spec_ctrl_flags off the stack, and we're about to add a third. Load the field into %ebx, and list the register as clobbered.

%r12 has been arranged to be the ist_exit signal, so add this as an input dependency and use it to identify when to issue a VERW.

While looking technically okay, I still have a couple of remarks:

xen/arch/x86/include/asm/spec_ctrl_asm.h UNLIKELY_DISPATCH_LABEL(\@_serialise): · line 348

Quoted 10 lines
xen/arch/x86/include/asm/spec_ctrl_asm.h +2 -2
@@ -344,10 +344,12 @@ UNLIKELY_DISPATCH_LABEL(\@_serialise):
  */
 .macro SPEC_CTRL_EXIT_TO_XEN
 /*
- * Requires %r14=stack_end
- * Clobbers %rax, %rcx, %rdx
+ * Requires %r12=ist_exit, %r14=stack_end
+ * Clobbers %rax, %rbx, %rcx, %rdx

While I'd generally be a little concerned of the growing dependency and clobber lists, there being a single use site makes this pretty okay. The macro invocation being immediately followed by RESTORE_ALL effectively means we can clobber almost everything here.

Quoted 4 lines
Quoted 2 lines

As to register usage and my comment on patch 5: There's no real need to switch %rbx to %r14 there if I'm not mistaken

As said, it's about consistency of the helpers.

While I'm not entirely happy with this, ...

Quoted 15 lines
Quoted 4 lines
Quoted 4 lines
xen/arch/x86/include/asm/spec_ctrl_asm.h +0 -0
@@ -367,8 +369,16 @@ UNLIKELY_DISPATCH_LABEL(\@_serialise):
 
 .L\@_skip_sc_msr:
 
Quoted 10 lines

xen/arch/x86/include/asm/spec_ctrl_asm.h UNLIKELY_DISPATCH_LABEL(\@_serialise): · line 379

Quoted 9 lines
xen/arch/x86/include/asm/spec_ctrl_asm.h +8 -1
-    /* TODO VERW */
+    test %r12, %r12
+    jz .L\@_skip_ist_exit
+
+    /* Logically DO_SPEC_CTRL_COND_VERW but without the %rsp=cpuinfo dependency */
+    testb $SCF_verw, %bl
+    jz .L\@_verw_skip
+    verw STACK_CPUINFO_FIELD(verw_sel)(%r14)
+.L\@_verw_skip:

Nit: .L\@_skip_verw would be more consistent with the other label names.

Quoted 1 line

So it would.  I'll tweak.

... then

Jan

#25 lore raw

PATCH 8/8 x86/spec-ctrl: Mitigate the Zen1 DIV leakge

In reply to Andrew Cooper

In the Zen1 microarchitecure, there is one divider in the pipeline which services uops from both threads. In the case of #DE, the latched result from the previous DIV to execute will be forwarded speculatively.

This is an interesting covert channel that allows two threads to communicate without any system calls. In also allows userspace to obtain the result of the most recent DIV instruction executed (even speculatively) in the core, which can be from a higher privilege context.

Scrub the buffers in the divider unit by executing a non-faulting divide. This needs performing on the exit-to-guest paths, and ist_exit-to-Xen.

This is XSA-439 / CVE-2023-20588.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>

No embargo - this is already public. XSA paperwork to follow.

---
 docs/misc/xen-command-line.pandoc        |  6 +++-
 xen/arch/x86/hvm/svm/entry.S             |  1 +
 xen/arch/x86/include/asm/cpufeatures.h   |  2 +-
 xen/arch/x86/include/asm/spec_ctrl_asm.h | 16 +++++++++
 xen/arch/x86/spec_ctrl.c                 | 45 ++++++++++++++++++++++--
 5 files changed, 66 insertions(+), 4 deletions(-)
docs/misc/xen-command-line.pandoc +5 -1
index f88e6a70aed6..7acd68885656 100644
@@ -2353,7 +2353,7 @@ By default SSBD will be mitigated at runtime (i.e `ssbd=runtime`).
 >              {msr-sc,rsb,md-clear,ibpb-entry}=<bool>|{pv,hvm}=<bool>,
 >              bti-thunk=retpoline|lfence|jmp, {ibrs,ibpb,ssbd,psfd,
 >              eager-fpu,l1d-flush,branch-harden,srb-lock,
->              unpriv-mmio,gds-mit}=<bool> ]`
+>              unpriv-mmio,gds-mit,div-scrub}=<bool> ]`
 
 Controls for speculative execution sidechannel mitigations.  By default, Xen
 will pick the most appropriate mitigations based on compiled in support,
@@ -2475,6 +2475,10 @@ has elected not to lock the configuration, Xen will use GDS_CTRL to mitigate
 GDS with.  Otherwise, Xen will mitigate by disabling AVX, which blocks the use
 of the AVX2 Gather instructions.
 
+On all hardware, the `div-scrub=` option can be used to force or prevent Xen
+from mitigating the DIV-leakage vulnerability.  By default, Xen will mitigate
+DIV-leakage on hardware believed to be vulnerable.
+
 ### sync_console
 > `= <boolean>`
 
xen/arch/x86/hvm/svm/entry.S +1 -0
index 9effd2199ba0..c52528fed4cf 100644
@@ -72,6 +72,7 @@ __UNLIKELY_END(nsvm_hap)
 1:          /* No Spectre v1 concerns.  Execution will hit VMRUN imminently. */
         .endm
         ALTERNATIVE "", svm_vmentry_spec_ctrl, X86_FEATURE_SC_MSR_HVM
+        ALTERNATIVE "", DO_SPEC_CTRL_DIV, X86_FEATURE_SC_DIV
 
         pop  %r15
         pop  %r14
xen/arch/x86/include/asm/cpufeatures.h +1 -1
index da0593de8542..724de2e11db4 100644
@@ -35,7 +35,7 @@ XEN_CPUFEATURE(SC_RSB_HVM,        X86_SYNTH(19)) /* RSB overwrite needed for HVM
 XEN_CPUFEATURE(XEN_SELFSNOOP,     X86_SYNTH(20)) /* SELFSNOOP gets used by Xen itself */
 XEN_CPUFEATURE(SC_MSR_IDLE,       X86_SYNTH(21)) /* Clear MSR_SPEC_CTRL on idle */
 XEN_CPUFEATURE(XEN_LBR,           X86_SYNTH(22)) /* Xen uses MSR_DEBUGCTL.LBR */
-/* Bits 23 unused. */
+XEN_CPUFEATURE(SC_DIV,            X86_SYNTH(25)) /* DIV scrub needed */
 XEN_CPUFEATURE(SC_RSB_IDLE,       X86_SYNTH(24)) /* RSB overwrite needed for idle. */
 XEN_CPUFEATURE(SC_VERW_IDLE,      X86_SYNTH(25)) /* VERW used by Xen for idle */
 XEN_CPUFEATURE(XEN_SHSTK,         X86_SYNTH(26)) /* Xen uses CET Shadow Stacks */
xen/arch/x86/include/asm/spec_ctrl_asm.h +16 -0
index 9740697114ad..10e57780f08b 100644
@@ -165,6 +165,18 @@
 .L\@_verw_skip:
 .endm
 
+.macro DO_SPEC_CTRL_DIV
+/*
+ * Requires nothing
+ * Clobbers %rax
+ *
+ * Issue a DIV for its flushing side effect (Zen1 uarch specific).  Any
+ * non-faulting DIV will do, and a byte DIV has least latency.
+ */
+    mov $1, %eax
+    div %al
+.endm
+
 .macro DO_SPEC_CTRL_ENTRY maybexen:req
 /*
  * Requires %rsp=regs (also cpuinfo if !maybexen)
@@ -267,6 +279,8 @@
     ALTERNATIVE "", DO_SPEC_CTRL_EXIT_TO_GUEST, X86_FEATURE_SC_MSR_PV
 
     DO_SPEC_CTRL_COND_VERW
+
+    ALTERNATIVE "", DO_SPEC_CTRL_DIV, X86_FEATURE_SC_DIV
 .endm
 
 /*
@@ -378,6 +392,8 @@ UNLIKELY_DISPATCH_LABEL(\@_serialise):
     verw STACK_CPUINFO_FIELD(verw_sel)(%r14)
 .L\@_verw_skip:
 
+    ALTERNATIVE "", DO_SPEC_CTRL_DIV, X86_FEATURE_SC_DIV
+
 .L\@_skip_ist_exit:
 .endm
 
xen/arch/x86/spec_ctrl.c +43 -2

87 lines not on this page · raw message

Signature · 1 line
2.30.2
#26 lore raw

Re: [PATCH 8/8] x86/spec-ctrl: Mitigate the Zen1 DIV leakge

Quoted 7 lines · Andrew Cooper
xen/arch/x86/spec_ctrl.c +3 -0
@@ -955,6 +960,40 @@ static void __init srso_calculations(bool hw_smt_enabled)
         setup_force_cpu_cap(X86_FEATURE_SRSO_NO);
 }
 
+/*
+ * Div leakage is specific to the AMD Zen1 microarchitecure.  Use STIBP as a
+ * heuristic to select between Zen1 and Zen2 uarches.

xen/arch/x86/spec_ctrl.c static void __init srso_calculations(bool hw_smt_enabled) · line 975

Quoted 10 lines
xen/arch/x86/spec_ctrl.c +10 -0
+ */
+static bool __init has_div_vuln(void)
+{
+    if ( !(boot_cpu_data.x86_vendor &
+           (X86_VENDOR_AMD | X86_VENDOR_HYGON)) )
+        return false;
+
+    if ( (boot_cpu_data.x86 != 0x17 && boot_cpu_data.x86 != 0x18) ||
+         !boot_cpu_has(X86_FEATURE_AMD_STIBP) )
+        return false;

Bah - this serves me right for positing before waiting for CI to check that Naples picks the right default.  The STIBP check is backwards and will mix up Zen1/2.

I'm going to create real is_zen{1,2}_uarch() helpers in amd.h to avoid opencoding this heuristic yet again.  I highly doubt this will be the final time we need it.

~Andrew

#27 lore raw

Re: [PATCH 8/8] x86/spec-ctrl: Mitigate the Zen1 DIV leakge

In reply to Andrew Cooper

xen/arch/x86/include/asm/cpufeatures.h XEN_CPUFEATURE(SC_RSB_HVM, X86_SYNTH(19)) /* RSB overwrite needed for HVM · line 38

Quoted 10 lines · Andrew Cooper
xen/arch/x86/include/asm/cpufeatures.h +1 -1
index da0593de8542..724de2e11db4 100644
@@ -35,7 +35,7 @@ XEN_CPUFEATURE(SC_RSB_HVM,        X86_SYNTH(19)) /* RSB overwrite needed for HVM
 XEN_CPUFEATURE(XEN_SELFSNOOP,     X86_SYNTH(20)) /* SELFSNOOP gets used by Xen itself */
 XEN_CPUFEATURE(SC_MSR_IDLE,       X86_SYNTH(21)) /* Clear MSR_SPEC_CTRL on idle */
 XEN_CPUFEATURE(XEN_LBR,           X86_SYNTH(22)) /* Xen uses MSR_DEBUGCTL.LBR */
-/* Bits 23 unused. */
+XEN_CPUFEATURE(SC_DIV,            X86_SYNTH(25)) /* DIV scrub needed */

s/25/23/

Quoted 3 lines
xen/arch/x86/include/asm/cpufeatures.h +0 -0
 XEN_CPUFEATURE(SC_RSB_IDLE,       X86_SYNTH(24)) /* RSB overwrite needed for idle. */
 XEN_CPUFEATURE(SC_VERW_IDLE,      X86_SYNTH(25)) /* VERW used by Xen for idle */
 XEN_CPUFEATURE(XEN_SHSTK,         X86_SYNTH(26)) /* Xen uses CET Shadow Stacks */
Signature · 3 lines
Best Regards,
Marek Marczykowski-Górecki
Invisible Things Lab
#28 lore raw

Re: [PATCH 8/8] x86/spec-ctrl: Mitigate the Zen1 DIV leakge

In reply to Andrew Cooper

xen/arch/x86/include/asm/spec_ctrl_asm.h UNLIKELY_DISPATCH_LABEL(\@_serialise): · line 398

Quoted 8 lines · Andrew Cooper
xen/arch/x86/include/asm/spec_ctrl_asm.h +2 -0
@@ -378,6 +392,8 @@ UNLIKELY_DISPATCH_LABEL(\@_serialise):
     verw STACK_CPUINFO_FIELD(verw_sel)(%r14)
 .L\@_verw_skip:
 
+    ALTERNATIVE "", DO_SPEC_CTRL_DIV, X86_FEATURE_SC_DIV
+
 .L\@_skip_ist_exit:
 .endm

While we did talk about using alternatives patching here, I'm now in doubt again, in particular because the rest of the macro uses conditionals anyway, and the code here is bypassed for non-IST exits. If you're sure you want to stick to this, then I think some justification wants adding to the description.

Quoted 20 lines
xen/arch/x86/spec_ctrl.c +16 -0
@@ -955,6 +960,40 @@ static void __init srso_calculations(bool hw_smt_enabled)
         setup_force_cpu_cap(X86_FEATURE_SRSO_NO);
 }
 
+/*
+ * Div leakage is specific to the AMD Zen1 microarchitecure.  Use STIBP as a
+ * heuristic to select between Zen1 and Zen2 uarches.
+ */
+static bool __init has_div_vuln(void)
+{
+    if ( !(boot_cpu_data.x86_vendor &
+           (X86_VENDOR_AMD | X86_VENDOR_HYGON)) )
+        return false;
+
+    if ( (boot_cpu_data.x86 != 0x17 && boot_cpu_data.x86 != 0x18) ||
+         !boot_cpu_has(X86_FEATURE_AMD_STIBP) )
+        return false;
+
+    return true;
+}

xen/arch/x86/spec_ctrl.c static void __init srso_calculations(bool hw_smt_enabled) · line 988

Quoted 10 lines
xen/arch/x86/spec_ctrl.c +10 -0
+
+static void __init div_calculations(bool hw_smt_enabled)
+{
+    bool cpu_bug_div = has_div_vuln();
+
+    if ( opt_div_scrub == -1 )
+        opt_div_scrub = cpu_bug_div;
+
+    if ( opt_div_scrub )
+        setup_force_cpu_cap(X86_FEATURE_SC_DIV);

Isn't this only lowering performance (even if just slightly) when SMT is enabled, without gaining us very much?

xen/arch/x86/spec_ctrl.c static void __init srso_calculations(bool hw_smt_enabled) · line 995

Quoted 6 lines
xen/arch/x86/spec_ctrl.c +6 -0
+    if ( opt_smt == -1 && cpu_bug_div && hw_smt_enabled )
+        warning_add(
+            "Booted on leaky-DIV hardware with SMT/Hyperthreading\n"
+            "enabled.  Please assess your configuration and choose an\n"
+            "explicit 'smt=<bool>' setting.  See XSA-439.\n");
+}

What about us running virtualized? The topology we see may not be that of the underlying hardware. Maybe check_smt_enabled() should return true when cpu_has_hypervisor is true? (In-guest decisions would similarly need to assume that they may be running on SMT-enabled hardware, despite not themselves seeing this to be the case.)

Since we can't know for sure when running virtualized, that's a case where I would consider it useful to enable the workaround nevertheless (perhaps accompanied by a warning that whether this helps depends on the next level hypervisor).

Jan

#29 lore raw

Re: [PATCH 8/8] x86/spec-ctrl: Mitigate the Zen1 DIV leakge

Quoted 5 lines · Jan Beulich
Quoted 4 lines · Andrew Cooper
xen/arch/x86/include/asm/spec_ctrl_asm.h +0 -0
@@ -378,6 +392,8 @@ UNLIKELY_DISPATCH_LABEL(\@_serialise):
     verw STACK_CPUINFO_FIELD(verw_sel)(%r14)
 .L\@_verw_skip:
 
Quoted 9 lines

xen/arch/x86/include/asm/spec_ctrl_asm.h UNLIKELY_DISPATCH_LABEL(\@_serialise): · line 398

Quoted 4 lines
xen/arch/x86/include/asm/spec_ctrl_asm.h +2 -0
+    ALTERNATIVE "", DO_SPEC_CTRL_DIV, X86_FEATURE_SC_DIV
+
 .L\@_skip_ist_exit:
 .endm

While we did talk about using alternatives patching here, I'm now in doubt again, in particular because the rest of the macro uses conditionals anyway, and the code here is bypassed for non-IST exits. If you're sure you want to stick to this, then I think some justification wants adding to the description.

Patching IST paths is safe - we drop into NMI context, and rewrite before starting APs.

VERW needs to remain a conditional because of the FB_CLEAR/MMIO paths. MSR_SPEC_CTRL is going to turn back into being an alternative when I make eIBRS work sensibly.

Quoted 24 lines
Quoted 24 lines
xen/arch/x86/spec_ctrl.c +20 -0
@@ -955,6 +960,40 @@ static void __init srso_calculations(bool hw_smt_enabled)
         setup_force_cpu_cap(X86_FEATURE_SRSO_NO);
 }
 
+/*
+ * Div leakage is specific to the AMD Zen1 microarchitecure.  Use STIBP as a
+ * heuristic to select between Zen1 and Zen2 uarches.
+ */
+static bool __init has_div_vuln(void)
+{
+    if ( !(boot_cpu_data.x86_vendor &
+           (X86_VENDOR_AMD | X86_VENDOR_HYGON)) )
+        return false;
+
+    if ( (boot_cpu_data.x86 != 0x17 && boot_cpu_data.x86 != 0x18) ||
+         !boot_cpu_has(X86_FEATURE_AMD_STIBP) )
+        return false;
+
+    return true;
+}
+
+static void __init div_calculations(bool hw_smt_enabled)
+{
+    bool cpu_bug_div = has_div_vuln();
Quoted 8 lines

xen/arch/x86/spec_ctrl.c static void __init srso_calculations(bool hw_smt_enabled) · line 988

Quoted 6 lines
xen/arch/x86/spec_ctrl.c +6 -0
+
+    if ( opt_div_scrub == -1 )
+        opt_div_scrub = cpu_bug_div;
+
+    if ( opt_div_scrub )
+        setup_force_cpu_cap(X86_FEATURE_SC_DIV);

Isn't this only lowering performance (even if just slightly) when SMT is enabled, without gaining us very much?

It is consistent with how MDS/L1TF/others work, which is important.

And it does protect against a single-threaded attacker, for what that's worth in practice.

Quoted 13 lines

xen/arch/x86/spec_ctrl.c static void __init srso_calculations(bool hw_smt_enabled) · line 995

Quoted 7 lines
xen/arch/x86/spec_ctrl.c +6 -0
+    if ( opt_smt == -1 && cpu_bug_div && hw_smt_enabled )
+        warning_add(
+            "Booted on leaky-DIV hardware with SMT/Hyperthreading\n"
+            "enabled.  Please assess your configuration and choose an\n"
+            "explicit 'smt=<bool>' setting.  See XSA-439.\n");
+}

What about us running virtualized? The topology we see may not be that of the underlying hardware. Maybe check_smt_enabled() should return true when cpu_has_hypervisor is true? (In-guest decisions would similarly need to assume that they may be running on SMT-enabled hardware, despite not themselves seeing this to be the case.)

Quoted 4 lines

Since we can't know for sure when running virtualized, that's a case where I would consider it useful to enable the workaround nevertheless (perhaps accompanied by a warning that whether this helps depends on the next level hypervisor).

Honestly, SMT's not relevant.  If you're virtualised, you've lost irrespective.

There is no FOO_NO bit, so there's no possible way a VM can figure out if it is current on, or may subsequently move to, an impacted processor.

~Andrew

#30 lore raw

Re: [PATCH 8/8] x86/spec-ctrl: Mitigate the Zen1 DIV leakge

In reply to Andrew Cooper
Quoted 15 lines · Andrew Cooper
xen/arch/x86/spec_ctrl.c +11 -0
@@ -955,6 +960,40 @@ static void __init srso_calculations(bool hw_smt_enabled)
         setup_force_cpu_cap(X86_FEATURE_SRSO_NO);
 }
 
+/*
+ * Div leakage is specific to the AMD Zen1 microarchitecure.  Use STIBP as a
+ * heuristic to select between Zen1 and Zen2 uarches.
+ */
+static bool __init has_div_vuln(void)
+{
+    if ( !(boot_cpu_data.x86_vendor &
+           (X86_VENDOR_AMD | X86_VENDOR_HYGON)) )
+        return false;
+
+    if ( (boot_cpu_data.x86 != 0x17 && boot_cpu_data.x86 != 0x18) ||

xen/arch/x86/spec_ctrl.c static void __init srso_calculations(bool hw_smt_enabled) · line 983

Quoted 10 lines
xen/arch/x86/spec_ctrl.c +10 -0
+         !boot_cpu_has(X86_FEATURE_AMD_STIBP) )
+        return false;
+
+    return true;
+}
+
+static void __init div_calculations(bool hw_smt_enabled)
+{
+    bool cpu_bug_div = has_div_vuln();
+

Would it make sense to add

    if ( !cpu_bug_div )
        return

...

xen/arch/x86/spec_ctrl.c static void __init srso_calculations(bool hw_smt_enabled) · line 988

Quoted 5 lines
xen/arch/x86/spec_ctrl.c +5 -0
+    if ( opt_div_scrub == -1 )
+        opt_div_scrub = cpu_bug_div;
+
+    if ( opt_div_scrub )
+        setup_force_cpu_cap(X86_FEATURE_SC_DIV);

...so that div-scrub=1 isn't setting X86_FEATURE_SC_DIV on un-affected hardware? Or do you want to leave command line control in place in case it might be needed as a future workaround on other hardware?

Thanks,
Jason

#31 lore raw

Re: [PATCH 8/8] x86/spec-ctrl: Mitigate the Zen1 DIV leakge

Quoted 31 lines · Jason Andryuk
Quoted 15 lines · Andrew Cooper
xen/arch/x86/spec_ctrl.c +11 -0
@@ -955,6 +960,40 @@ static void __init srso_calculations(bool hw_smt_enabled)
         setup_force_cpu_cap(X86_FEATURE_SRSO_NO);
 }
 
+/*
+ * Div leakage is specific to the AMD Zen1 microarchitecure.  Use STIBP as a
+ * heuristic to select between Zen1 and Zen2 uarches.
+ */
+static bool __init has_div_vuln(void)
+{
+    if ( !(boot_cpu_data.x86_vendor &
+           (X86_VENDOR_AMD | X86_VENDOR_HYGON)) )
+        return false;
+
+    if ( (boot_cpu_data.x86 != 0x17 && boot_cpu_data.x86 != 0x18) ||

xen/arch/x86/spec_ctrl.c static void __init srso_calculations(bool hw_smt_enabled) · line 983

Quoted 10 lines
xen/arch/x86/spec_ctrl.c +10 -0
+         !boot_cpu_has(X86_FEATURE_AMD_STIBP) )
+        return false;
+
+    return true;
+}
+
+static void __init div_calculations(bool hw_smt_enabled)
+{
+    bool cpu_bug_div = has_div_vuln();
+

Would it make sense to add

    if ( !cpu_bug_div )
        return

...

Quoted 8 lines

xen/arch/x86/spec_ctrl.c static void __init srso_calculations(bool hw_smt_enabled) · line 988

Quoted 5 lines
xen/arch/x86/spec_ctrl.c +5 -0
+    if ( opt_div_scrub == -1 )
+        opt_div_scrub = cpu_bug_div;
+
+    if ( opt_div_scrub )
+        setup_force_cpu_cap(X86_FEATURE_SC_DIV);

...so that div-scrub=1 isn't setting X86_FEATURE_SC_DIV on un-affected hardware? Or do you want to leave command line control in place in case it might be needed as a future workaround on other hardware?

All options (where possible) allow for paths to be explicitly activated on un-affected hardware so we can test this giant mess.

The only cases where we ignore a user choice is when the result will crash from e.g. #GP due to insufficient microcode.

~Andrew