Skip to contentSkip to conversation

x86/alternatives: Support for automatic padding calculations

PATCH In review Andrew Cooper 2 participants 15 messages Mar 7, 2018 – Mar 8, 2018

lore
Share & tools
lore
#0 lore raw

v3 cover x86/alternatives: Support for automatic padding calculations

This is the end result of a lot of work I started during the Spectre/Meltdown embargo window, and deferred because it was taking too long. It finally resolves the explict padding calculations for the SPEC_CTRL alternatives.

The only changes from v3 are in patches 5 and 7.

http://xenbits.xen.org/gitweb/?p=people/andrewcoop/xen.git;a=shortlog;h=refs/heads/alternatives-v3

Andrew Cooper (7):
x86/alt: Drop unused alternative infrastructure x86/alt: Clean up struct alt_instr and its users x86/alt: Clean up the assembly used to generate alternatives x86/asm: Remove opencoded uses of altinstruction_entry x86/alt: Support for automatic padding calculations x86/alt: Drop explicit padding of origin sites x86/build: Use new .nops directive when available

 xen/arch/x86/Rules.mk                 |   8 ++
 xen/arch/x86/alternative.c            |  62 ++++++++++----
 xen/arch/x86/x86_64/compat/entry.S    |  26 ++----
 xen/arch/x86/x86_64/entry.S           |  20 +----
 xen/include/asm-x86/alternative-asm.h | 106 ++++++++++++++++-------
 xen/include/asm-x86/alternative.h     | 154 +++++++++++++++++++---------------
 xen/include/asm-x86/asm_defns.h       |  34 +++-----
 xen/include/asm-x86/nops.h            |   7 --
 xen/include/asm-x86/spec_ctrl_asm.h   |  19 ++---
 9 files changed, 250 insertions(+), 186 deletions(-)
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#1 lore raw

PATCH v3 1/7 x86/alt: Drop unused alternative infrastructure

ALTERNATIVE_3 is more complicated than ALTERNATIVE_2 when it comes to calculating extra padding length, and we have no need for the complexity.

---
v2:
* Retain ASM_OUTPUT2()

---
 xen/include/asm-x86/alternative.h | 22 ----------------------
 1 file changed, 22 deletions(-)
xen/include/asm-x86/alternative.h +0 -22
index ba537d6..325a29f 100644
@@ -65,11 +65,6 @@ extern void alternative_instructions(void);
 	ALTERNATIVE(oldinstr, newinstr1, feature1)			  \
 	ALTERNATIVE_N(newinstr2, feature2, 2)
 
-#define ALTERNATIVE_3(oldinstr, newinstr1, feature1, newinstr2, feature2, \
-		      newinstr3, feature3)				  \
-	ALTERNATIVE_2(oldinstr, newinstr1, feature1, newinstr2, feature2) \
-	ALTERNATIVE_N(newinstr3, feature3, 3)
-
 /*
  * Alternative instructions for different CPU types or capabilities.
  *
@@ -118,23 +113,6 @@ extern void alternative_instructions(void);
 				   newinstr2, feature2)			\
 		     : output : input)
 
-/*
- * This is similar to alternative_io. But it has three features and
- * respective instructions.
- *
- * If CPU has feature3, newinstr3 is used.
- * Otherwise, if CPU has feature2, newinstr2 is used.
- * Otherwise, if CPU has feature1, newinstr1 is used.
- * Otherwise, oldinstr is used.
- */
-#define alternative_io_3(oldinstr, newinstr1, feature1, newinstr2,	\
-			 feature2, newinstr3, feature3, output,		\
-			 input...)					\
-	asm volatile(ALTERNATIVE_3(oldinstr, newinstr1, feature1,	\
-				   newinstr2, feature2, newinstr3,	\
-				   feature3)				\
-		     : output : input)
-
 /* Use this macro(s) if you need more than one output parameter. */
 #define ASM_OUTPUT2(a...) a
 
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#2 lore raw

PATCH v3 2/7 x86/alt: Clean up struct alt_instr and its users

In reply to Andrew Cooper

* Rename some fields for consistency and clarity, and use standard types.
* Don't opencode the use of ALT_{ORIG,REPL}_PTR().

No functional change.

---
v2:
* Change more types to standard ones

---
 xen/arch/x86/alternative.c        | 24 ++++++++++++------------
 xen/include/asm-x86/alternative.h | 14 +++++++-------
 2 files changed, 19 insertions(+), 19 deletions(-)
xen/arch/x86/alternative.c +12 -12
index 5c8b6f6..51ca53e 100644
@@ -163,8 +163,6 @@ void init_or_livepatch apply_alternatives(const struct alt_instr *start,
                                           const struct alt_instr *end)
 {
     const struct alt_instr *a;
-    u8 *instr, *replacement;
-    u8 insnbuf[MAX_PATCH_LEN];
 
     printk(KERN_INFO "alt table %p -> %p\n", start, end);
 
@@ -179,23 +177,25 @@ void init_or_livepatch apply_alternatives(const struct alt_instr *start,
      */
     for ( a = start; a < end; a++ )
     {
-        instr = (u8 *)&a->instr_offset + a->instr_offset;
-        replacement = (u8 *)&a->repl_offset + a->repl_offset;
-        BUG_ON(a->replacementlen > a->instrlen);
-        BUG_ON(a->instrlen > sizeof(insnbuf));
+        uint8_t *orig = ALT_ORIG_PTR(a);
+        uint8_t *repl = ALT_REPL_PTR(a);
+        uint8_t buf[MAX_PATCH_LEN];
+
+        BUG_ON(a->repl_len > a->orig_len);
+        BUG_ON(a->orig_len > sizeof(buf));
         BUG_ON(a->cpuid >= NCAPINTS * 32);
+
         if ( !boot_cpu_has(a->cpuid) )
             continue;
 
-        memcpy(insnbuf, replacement, a->replacementlen);
+        memcpy(buf, repl, a->repl_len);
 
         /* 0xe8/0xe9 are relative branches; fix the offset. */
-        if ( a->replacementlen >= 5 && (*insnbuf & 0xfe) == 0xe8 )
-            *(s32 *)(insnbuf + 1) += replacement - instr;
+        if ( a->repl_len >= 5 && (*buf & 0xfe) == 0xe8 )
+            *(int32_t *)(buf + 1) += repl - orig;
 
-        add_nops(insnbuf + a->replacementlen,
-                 a->instrlen - a->replacementlen);
-        text_poke(instr, insnbuf, a->instrlen);
+        add_nops(buf + a->repl_len, a->orig_len - a->repl_len);
+        text_poke(orig, buf, a->orig_len);
     }
 }
 
xen/include/asm-x86/alternative.h +7 -7
index 325a29f..d9706fd 100644
@@ -9,15 +9,15 @@
 #include <xen/types.h>
 
 struct alt_instr {
-    s32 instr_offset;       /* original instruction */
-    s32 repl_offset;        /* offset to replacement instruction */
-    u16 cpuid;              /* cpuid bit set for replacement */
-    u8  instrlen;           /* length of original instruction */
-    u8  replacementlen;     /* length of new instruction, <= instrlen */
+    int32_t  orig_offset;   /* original instruction */
+    int32_t  repl_offset;   /* offset to replacement instruction */
+    uint16_t cpuid;         /* cpuid bit set for replacement */
+    uint8_t  orig_len;      /* length of original instruction */
+    uint8_t  repl_len;      /* length of new instruction, <= instrlen */
 };
 
-#define __ALT_PTR(a,f)      ((u8 *)((void *)&(a)->f + (a)->f))
-#define ALT_ORIG_PTR(a)     __ALT_PTR(a, instr_offset)
+#define __ALT_PTR(a,f)      ((uint8_t *)((void *)&(a)->f + (a)->f))
+#define ALT_ORIG_PTR(a)     __ALT_PTR(a, orig_offset)
 #define ALT_REPL_PTR(a)     __ALT_PTR(a, repl_offset)
 
 extern void add_nops(void *insns, unsigned int len);
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#3 lore raw

PATCH v3 3/7 x86/alt: Clean up the assembly used to generate alternatives

In reply to Andrew Cooper

* On the C side, switch to using local lables rather than hardcoded numbers.
* Rename parameters and lables to be consistent with alt_instr names, and consistent between the the C and asm versions.
* On the asm side, factor some expressions out into macros to aid clarity.
* Consistently declare section attributes.

No functional change.

---
v2:
* repl_{s,e} => alt_repl_{s,e}
* Use .LXEN%= prefix for the C lables
v3:
* Factor our decl_orig() on the ASM side

---
 xen/include/asm-x86/alternative-asm.h | 60 +++++++++++++++++---------------
 xen/include/asm-x86/alternative.h     | 65 +++++++++++++++++++----------------
 2 files changed, 68 insertions(+), 57 deletions(-)
xen/include/asm-x86/alternative-asm.h +33 -27
index 6640e85..2af4f6b 100644
@@ -9,60 +9,66 @@
  * enough information for the alternatives patching code to patch an
  * instruction. See apply_alternatives().
  */
-.macro altinstruction_entry orig alt feature orig_len alt_len
+.macro altinstruction_entry orig repl feature orig_len repl_len
     .long \orig - .
-    .long \alt - .
+    .long \repl - .
     .word \feature
     .byte \orig_len
-    .byte \alt_len
+    .byte \repl_len
 .endm
 
+#define decl_orig(insn)         .L\@_orig_s:      insn; .L\@_orig_e:
+#define orig_len               (.L\@_orig_e       -     .L\@_orig_s)
+
+#define decl_repl(insn, nr)     .L\@_repl_s\()nr: insn; .L\@_repl_e\()nr:
+#define repl_len(nr)           (.L\@_repl_e\()nr  -     .L\@_repl_s\()nr)
+
 .macro ALTERNATIVE oldinstr, newinstr, feature
-.Lold_start_\@:
-    \oldinstr
-.Lold_end_\@:
+    decl_orig(\oldinstr)
 
     .pushsection .altinstructions, "a", @progbits
-    altinstruction_entry .Lold_start_\@, .Lnew_start_\@, \feature, \
-        (.Lold_end_\@ - .Lold_start_\@), (.Lnew_end_\@ - .Lnew_start_\@)
+    altinstruction_entry .L\@_orig_s, .L\@_repl_s1, \feature, \
+        orig_len, repl_len(1)
 
     .section .discard, "a", @progbits
     /* Assembler-time check that \newinstr isn't longer than \oldinstr. */
-    .byte 0xff + (.Lnew_end_\@ - .Lnew_start_\@) - (.Lold_end_\@ - .Lold_start_\@)
+    .byte 0xff + repl_len(1) - orig_len
 
     .section .altinstr_replacement, "ax", @progbits
-.Lnew_start_\@:
-    \newinstr
-.Lnew_end_\@:
+
+    decl_repl(\newinstr, 1)
+
     .popsection
 .endm
 
 .macro ALTERNATIVE_2 oldinstr, newinstr1, feature1, newinstr2, feature2
-.Lold_start_\@:
-    \oldinstr
-.Lold_end_\@:
+    decl_orig(\oldinstr)
 
     .pushsection .altinstructions, "a", @progbits
-    altinstruction_entry .Lold_start_\@, .Lnew1_start_\@, \feature1, \
-        (.Lold_end_\@ - .Lold_start_\@), (.Lnew1_end_\@ - .Lnew1_start_\@)
-    altinstruction_entry .Lold_start_\@, .Lnew2_start_\@, \feature2, \
-        (.Lold_end_\@ - .Lold_start_\@), (.Lnew2_end_\@ - .Lnew2_start_\@)
+
+    altinstruction_entry .L\@_orig_s, .L\@_repl_s1, \feature1, \
+        orig_len, repl_len(1)
+    altinstruction_entry .L\@_orig_s, .L\@_repl_s2, \feature2, \
+        orig_len, repl_len(2)
 
     .section .discard, "a", @progbits
     /* Assembler-time check that \newinstr{1,2} aren't longer than \oldinstr. */
-    .byte 0xff + (.Lnew1_end_\@ - .Lnew1_start_\@) - (.Lold_end_\@ - .Lold_start_\@)
-    .byte 0xff + (.Lnew2_end_\@ - .Lnew2_start_\@) - (.Lold_end_\@ - .Lold_start_\@)
+    .byte 0xff + repl_len(1) - orig_len
+    .byte 0xff + repl_len(2) - orig_len
 
     .section .altinstr_replacement, "ax", @progbits
-.Lnew1_start_\@:
-    \newinstr1
-.Lnew1_end_\@:
-.Lnew2_start_\@:
-    \newinstr2
-.Lnew2_end_\@:
+
+    decl_repl(\newinstr1, 1)
+    decl_repl(\newinstr2, 2)
+
     .popsection
 .endm
 
+#undef repl_len
+#undef decl_repl
+#undef orig_len
+#undef decl_orig
+
 #endif /* __ASSEMBLY__ */
 #endif /* _ASM_X86_ALTERNATIVE_ASM_H_ */
 
xen/include/asm-x86/alternative.h +35 -30

79 lines not on this page · raw message

Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#4 lore raw

PATCH v3 4/7 x86/asm: Remove opencoded uses of altinstruction_entry

In reply to Andrew Cooper

With future changes, altinstruction_entry is going to become more complicated to use. Furthermore, there are already ALTERNATIVE* macros which can be used to avoid opencoding the creation of replacement information.

For ASM_STAC, ASM_CLAC and CR4_PV32_RESTORE, this means the removal of all hardocded label numbers. For the cr4_pv32 alternatives, this means hardcoding the extra space required in the original patch site, but the hardcoding will be removed by a later patch.

No change to any functionality, but the handling of nops inside the original patch sites are a bit different.

---
 xen/arch/x86/x86_64/compat/entry.S | 26 +++++++++-----------------
 xen/arch/x86/x86_64/entry.S        | 20 +++-----------------
 xen/include/asm-x86/asm_defns.h    | 34 ++++++++++++----------------------
 3 files changed, 24 insertions(+), 56 deletions(-)
xen/arch/x86/x86_64/compat/entry.S +9 -17
index bf3a7ac..84f5eb1 100644
@@ -111,13 +111,10 @@ ENTRY(compat_restore_all_guest)
         ASSERT_INTERRUPTS_DISABLED
         mov   $~(X86_EFLAGS_IOPL|X86_EFLAGS_NT|X86_EFLAGS_VM),%r11d
         and   UREGS_eflags(%rsp),%r11d
-.Lcr4_orig:
-        .skip .Lcr4_alt_end - .Lcr4_alt, 0x90
-.Lcr4_orig_end:
-        .pushsection .altinstr_replacement, "ax"
-.Lcr4_alt:
+
+.macro alt_cr4_pv32
         testb $3,UREGS_cs(%rsp)
-        jpe   .Lcr4_alt_end
+        jpe   2f
         mov   CPUINFO_cr4-CPUINFO_guest_cpu_user_regs(%rsp), %rax
         and   $~XEN_CR4_PV32_BITS, %rax
 1:
@@ -135,17 +132,12 @@ ENTRY(compat_restore_all_guest)
          */
         cmp   %rax, CPUINFO_cr4-CPUINFO_guest_cpu_user_regs(%rsp)
         jne   1b
-.Lcr4_alt_end:
-        .section .altinstructions, "a"
-        altinstruction_entry .Lcr4_orig, .Lcr4_orig, X86_FEATURE_ALWAYS, \
-                             (.Lcr4_orig_end - .Lcr4_orig), 0
-        altinstruction_entry .Lcr4_orig, .Lcr4_alt, X86_FEATURE_XEN_SMEP, \
-                             (.Lcr4_orig_end - .Lcr4_orig), \
-                             (.Lcr4_alt_end - .Lcr4_alt)
-        altinstruction_entry .Lcr4_orig, .Lcr4_alt, X86_FEATURE_XEN_SMAP, \
-                             (.Lcr4_orig_end - .Lcr4_orig), \
-                             (.Lcr4_alt_end - .Lcr4_alt)
-        .popsection
+2:
+.endm
+	ALTERNATIVE_2 ".skip 45, 0x90", \
+            alt_cr4_pv32, X86_FEATURE_XEN_SMEP, \
+            alt_cr4_pv32, X86_FEATURE_XEN_SMAP
+
         or    $X86_EFLAGS_IF,%r11
         mov   %r11d,UREGS_eflags(%rsp)
 
xen/arch/x86/x86_64/entry.S +3 -17
index 6493796..207c1e0 100644
@@ -601,23 +601,9 @@ handle_exception_saved:
         testb $X86_EFLAGS_IF>>8,UREGS_eflags+1(%rsp)
         jz    exception_with_ints_disabled
 
-.Lcr4_pv32_orig:
-        jmp   .Lcr4_pv32_done
-        .skip (.Lcr4_pv32_alt_end - .Lcr4_pv32_alt) - (. - .Lcr4_pv32_orig), 0xcc
-        .pushsection .altinstr_replacement, "ax"
-.Lcr4_pv32_alt:
-        mov   VCPU_domain(%rbx),%rax
-.Lcr4_pv32_alt_end:
-        .section .altinstructions, "a"
-        altinstruction_entry .Lcr4_pv32_orig, .Lcr4_pv32_alt, \
-                             X86_FEATURE_XEN_SMEP, \
-                             (.Lcr4_pv32_alt_end - .Lcr4_pv32_alt), \
-                             (.Lcr4_pv32_alt_end - .Lcr4_pv32_alt)
-        altinstruction_entry .Lcr4_pv32_orig, .Lcr4_pv32_alt, \
-                             X86_FEATURE_XEN_SMAP, \
-                             (.Lcr4_pv32_alt_end - .Lcr4_pv32_alt), \
-                             (.Lcr4_pv32_alt_end - .Lcr4_pv32_alt)
-        .popsection
+        ALTERNATIVE_2 "jmp .Lcr4_pv32_done; .skip 2, 0x90", \
+            __stringify(mov VCPU_domain(%rbx), %rax), X86_FEATURE_XEN_SMEP, \
+            __stringify(mov VCPU_domain(%rbx), %rax), X86_FEATURE_XEN_SMAP
 
         testb $3,UREGS_cs(%rsp)
         jz    .Lcr4_pv32_done
xen/include/asm-x86/asm_defns.h +12 -22
index cc5ec65..f8128c0 100644
@@ -195,28 +195,18 @@ void ret_from_intr(void);
 #define __ASM_STAC      .byte 0x0f,0x01,0xcb
 
 #ifdef __ASSEMBLY__
-#define ASM_AC(op)                                                     \
-        661: ASM_NOP3;                                                 \
-        .pushsection .altinstr_replacement, "ax";                      \
-        662: __ASM_##op;                                               \
-        .popsection;                                                   \
-        .pushsection .altinstructions, "a";                            \
-        altinstruction_entry 661b, 661b, X86_FEATURE_ALWAYS, 3, 0;     \
-        altinstruction_entry 661b, 662b, X86_FEATURE_XEN_SMAP, 3, 3;       \
-        .popsection
-
-#define ASM_STAC ASM_AC(STAC)
-#define ASM_CLAC ASM_AC(CLAC)
-
-#define CR4_PV32_RESTORE                                           \
-        667: ASM_NOP5;                                             \
-        .pushsection .altinstr_replacement, "ax";                  \
-        668: call cr4_pv32_restore;                                \
-        .section .altinstructions, "a";                            \
-        altinstruction_entry 667b, 667b, X86_FEATURE_ALWAYS, 5, 0; \
-        altinstruction_entry 667b, 668b, X86_FEATURE_XEN_SMEP, 5, 5;   \
-        altinstruction_entry 667b, 668b, X86_FEATURE_XEN_SMAP, 5, 5;   \
-        .popsection
+#define ASM_STAC                                        \
+    ALTERNATIVE __stringify(ASM_NOP3),                  \
+        __stringify(__ASM_STAC), X86_FEATURE_XEN_SMAP
+
+#define ASM_CLAC                                        \
+    ALTERNATIVE __stringify(ASM_NOP3),                  \
+        __stringify(__ASM_CLAC), X86_FEATURE_XEN_SMAP
+
+#define CR4_PV32_RESTORE                                \
+    ALTERNATIVE_2 __stringify(ASM_NOP5),                \
+        "call cr4_pv32_restore", X86_FEATURE_XEN_SMEP,  \
+        "call cr4_pv32_restore", X86_FEATURE_XEN_SMAP
 
 #else
 static always_inline void clac(void)
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#5 lore raw

PATCH v3 5/7 x86/alt: Support for automatic padding calculations

In reply to Andrew Cooper

The correct amount of padding in an origin patch site can be calculated automatically, based on the relative lengths of the replacements.

This requires a bit of trickery to calculate correctly, especially in the ALTENRATIVE_2 case where a branchless max() calculation in needed. The calculation is further complicated because GAS's idea of true is -1 rather than 1, which is why the extra negations are required.

Additionally, have apply_alternatives() attempt to optimise the padding nops. This is complicated by the fact that we must not attempt to optimise nops over an origin site which has already been modified.

To keep track of this, add a priv field to struct alt_instr, which gets modified by apply_alternatives(). One extra requirement is that alt_instr's referring to the same origin site must now be consecutive, but we already have this property.

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

v2:
* Fix build with Clang
v3:
* s/GCC/GAS/ for the NEGATIVE_TRUE comments
* Factor out OLDINSTR() generation on the C side
* Introduce alt_instr->priv and use it to avoid repeatedly optimising the alternatives.

---
 xen/arch/x86/Rules.mk                 |  4 +++
 xen/arch/x86/alternative.c            | 45 +++++++++++++++++++++++-----
 xen/include/asm-x86/alternative-asm.h | 56 +++++++++++++++++++++++++++--------
 xen/include/asm-x86/alternative.h     | 50 ++++++++++++++++++++++++-------
 4 files changed, 124 insertions(+), 31 deletions(-)
xen/arch/x86/Rules.mk +4 -0
index a29ab22..ac585a3 100644
@@ -25,6 +25,10 @@ $(call as-option-add,CFLAGS,CC,".equ \"x\"$$(comma)1", \
                      '-D__OBJECT_LABEL__=$(subst $(BASEDIR)/,,$(CURDIR))/$$@')
 $(call as-option-add,CFLAGS,CC,"invpcid (%rax)$$(comma)%rax",-DHAVE_AS_INVPCID)
 
+# GAS's idea of true is -1.  Clang's idea is 1
+$(call as-option-add,CFLAGS,CC,\
+    ".if ((1 > 0) < 0); .error \"\";.endif",,-DHAVE_AS_NEGATIVE_TRUE)
+
 CFLAGS += -mno-red-zone -fpic -fno-asynchronous-unwind-tables
 
 # Xen doesn't use SSE interally.  If the compiler supports it, also skip the
xen/arch/x86/alternative.c +37 -8

73 lines not on this page · raw message

xen/include/asm-x86/alternative-asm.h +44 -12

103 lines not on this page · raw message

xen/include/asm-x86/alternative.h +39 -11

98 lines not on this page · raw message

Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#10 lore raw

PATCH v3 6/7 x86/alt: Drop explicit padding of origin sites

In reply to Andrew Cooper

Now that the alternatives infrastructure can calculate the required padding automatically, there is no need to hard code it.

---
 xen/arch/x86/x86_64/compat/entry.S  |  2 +-
 xen/arch/x86/x86_64/entry.S         |  2 +-
 xen/include/asm-x86/nops.h          |  7 -------
 xen/include/asm-x86/spec_ctrl_asm.h | 19 ++++++++-----------
 4 files changed, 10 insertions(+), 20 deletions(-)
xen/arch/x86/x86_64/compat/entry.S +1 -1
index 84f5eb1..f52bffc 100644
@@ -134,7 +134,7 @@ ENTRY(compat_restore_all_guest)
         jne   1b
 2:
 .endm
-	ALTERNATIVE_2 ".skip 45, 0x90", \
+	ALTERNATIVE_2 "", \
             alt_cr4_pv32, X86_FEATURE_XEN_SMEP, \
             alt_cr4_pv32, X86_FEATURE_XEN_SMAP
 
xen/arch/x86/x86_64/entry.S +1 -1
index 207c1e0..3824867 100644
@@ -601,7 +601,7 @@ handle_exception_saved:
         testb $X86_EFLAGS_IF>>8,UREGS_eflags+1(%rsp)
         jz    exception_with_ints_disabled
 
-        ALTERNATIVE_2 "jmp .Lcr4_pv32_done; .skip 2, 0x90", \
+        ALTERNATIVE_2 "jmp .Lcr4_pv32_done", \
             __stringify(mov VCPU_domain(%rbx), %rax), X86_FEATURE_XEN_SMEP, \
             __stringify(mov VCPU_domain(%rbx), %rax), X86_FEATURE_XEN_SMAP
 
xen/include/asm-x86/nops.h +0 -7
index 61319cc..1a46b97 100644
@@ -65,13 +65,6 @@
 #define ASM_NOP8 _ASM_MK_NOP(P6_NOP8)
 #define ASM_NOP9 _ASM_MK_NOP(P6_NOP9)
 
-#define ASM_NOP17 ASM_NOP8; ASM_NOP7; ASM_NOP2
-#define ASM_NOP21 ASM_NOP8; ASM_NOP8; ASM_NOP5
-#define ASM_NOP24 ASM_NOP8; ASM_NOP8; ASM_NOP8
-#define ASM_NOP29 ASM_NOP8; ASM_NOP8; ASM_NOP8; ASM_NOP5
-#define ASM_NOP32 ASM_NOP8; ASM_NOP8; ASM_NOP8; ASM_NOP8
-#define ASM_NOP40 ASM_NOP8; ASM_NOP8; ASM_NOP8; ASM_NOP8; ASM_NOP8
-
 #define ASM_NOP_MAX 9
 
 #endif /* __X86_ASM_NOPS_H__ */
xen/include/asm-x86/spec_ctrl_asm.h +8 -11
index 1f2b6f3..1623fc0 100644
@@ -216,9 +216,8 @@
 
 /* Use after a VMEXIT from an HVM guest. */
 #define SPEC_CTRL_ENTRY_FROM_VMEXIT                                     \
-    ALTERNATIVE __stringify(ASM_NOP40),                                 \
-        DO_OVERWRITE_RSB, X86_FEATURE_RSB_VMEXIT;                       \
-    ALTERNATIVE_2 __stringify(ASM_NOP32),                               \
+    ALTERNATIVE "", DO_OVERWRITE_RSB, X86_FEATURE_RSB_VMEXIT;           \
+    ALTERNATIVE_2 "",                                                   \
         __stringify(DO_SPEC_CTRL_ENTRY_FROM_VMEXIT                      \
                     ibrs_val=SPEC_CTRL_IBRS),                           \
         X86_FEATURE_XEN_IBRS_SET,                                       \
@@ -228,9 +227,8 @@
 
 /* Use after an entry from PV context (syscall/sysenter/int80/int82/etc). */
 #define SPEC_CTRL_ENTRY_FROM_PV                                         \
-    ALTERNATIVE __stringify(ASM_NOP40),                                 \
-        DO_OVERWRITE_RSB, X86_FEATURE_RSB_NATIVE;                       \
-    ALTERNATIVE_2 __stringify(ASM_NOP21),                               \
+    ALTERNATIVE "", DO_OVERWRITE_RSB, X86_FEATURE_RSB_NATIVE;           \
+    ALTERNATIVE_2 "",                                                   \
         __stringify(DO_SPEC_CTRL_ENTRY maybexen=0                       \
                     ibrs_val=SPEC_CTRL_IBRS),                           \
         X86_FEATURE_XEN_IBRS_SET,                                       \
@@ -239,9 +237,8 @@
 
 /* Use in interrupt/exception context.  May interrupt Xen or PV context. */
 #define SPEC_CTRL_ENTRY_FROM_INTR                                       \
-    ALTERNATIVE __stringify(ASM_NOP40),                                 \
-        DO_OVERWRITE_RSB, X86_FEATURE_RSB_NATIVE;                       \
-    ALTERNATIVE_2 __stringify(ASM_NOP29),                               \
+    ALTERNATIVE "", DO_OVERWRITE_RSB, X86_FEATURE_RSB_NATIVE;           \
+    ALTERNATIVE_2 "",                                                   \
         __stringify(DO_SPEC_CTRL_ENTRY maybexen=1                       \
                     ibrs_val=SPEC_CTRL_IBRS),                           \
         X86_FEATURE_XEN_IBRS_SET,                                       \
@@ -250,13 +247,13 @@
 
 /* Use when exiting to Xen context. */
 #define SPEC_CTRL_EXIT_TO_XEN                                           \
-    ALTERNATIVE_2 __stringify(ASM_NOP17),                               \
+    ALTERNATIVE_2 "",                                                   \
         DO_SPEC_CTRL_EXIT_TO_XEN, X86_FEATURE_XEN_IBRS_SET,             \
         DO_SPEC_CTRL_EXIT_TO_XEN, X86_FEATURE_XEN_IBRS_CLEAR
 
 /* Use when exiting to guest context. */
 #define SPEC_CTRL_EXIT_TO_GUEST                                         \
-    ALTERNATIVE_2 __stringify(ASM_NOP24),                               \
+    ALTERNATIVE_2 "",                                                   \
         DO_SPEC_CTRL_EXIT_TO_GUEST, X86_FEATURE_XEN_IBRS_SET,           \
         DO_SPEC_CTRL_EXIT_TO_GUEST, X86_FEATURE_XEN_IBRS_CLEAR
 
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#11 lore raw

PATCH v3 7/7 x86/build: Use new .nops directive when available

In reply to Andrew Cooper

Newer versions of binutils are capable of emitting an exact number bytes worth of optimised nops, which are P6 nops. Use this in preference to .skip when available, and skip optimising nops entirely when they correct for the running hardware.

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

v3:
* Rewrite toolstack P6 nops when K8 nops are the correct ones for the system.

---
 xen/arch/x86/Rules.mk                 |  4 ++++
 xen/arch/x86/alternative.c            |  3 ++-
 xen/include/asm-x86/alternative-asm.h | 12 +++++++++++-
 xen/include/asm-x86/alternative.h     | 13 +++++++++++--
 4 files changed, 28 insertions(+), 4 deletions(-)
xen/arch/x86/Rules.mk +4 -0
index ac585a3..c84ed20 100644
@@ -29,6 +29,10 @@ $(call as-option-add,CFLAGS,CC,"invpcid (%rax)$$(comma)%rax",-DHAVE_AS_INVPCID)
 $(call as-option-add,CFLAGS,CC,\
     ".if ((1 > 0) < 0); .error \"\";.endif",,-DHAVE_AS_NEGATIVE_TRUE)
 
+# Check to see whether the assmbler supports the .nop directive.
+$(call as-option-add,CFLAGS,CC,\
+    ".L1: .L2: .nops (.L2 - .L1)$$(comma)9",-DHAVE_AS_NOP_DIRECTIVE)
+
 CFLAGS += -mno-red-zone -fpic -fno-asynchronous-unwind-tables
 
 # Xen doesn't use SSE interally.  If the compiler supports it, also skip the
xen/arch/x86/alternative.c +2 -1
index 93adf56..dc3ef24 100644
@@ -207,7 +207,8 @@ void init_or_livepatch apply_alternatives(struct alt_instr *start,
             base->priv = 1;
 
             /* Nothing useful to do? */
-            if ( a->pad_len <= 1 )
+            if ( (TOOLCHAIN_P6_NOPS && ideal_nops == p6_nops) ||
+                 a->pad_len <= 1 )
                 continue;
 
             add_nops(buf, a->pad_len);
xen/include/asm-x86/alternative-asm.h +11 -1
index 0b61516..0d6fb4b 100644
@@ -1,6 +1,8 @@
 #ifndef _ASM_X86_ALTERNATIVE_ASM_H_
 #define _ASM_X86_ALTERNATIVE_ASM_H_
 
+#include <asm/nops.h>
+
 #ifdef __ASSEMBLY__
 
 /*
@@ -19,6 +21,14 @@
     .byte 0 /* priv */
 .endm
 
+.macro mknops nr_bytes
+#ifdef HAVE_AS_NOP_DIRECTIVE
+    .nops \nr_bytes, ASM_NOP_MAX
+#else
+    .skip \nr_bytes, 0x90
+#endif
+.endm
+
 /* GAS's idea of true is -1, while Clang's idea is 1. */
 #ifdef HAVE_AS_NEGATIVE_TRUE
 # define as_true(x) (-(x))
@@ -29,7 +39,7 @@
 #define decl_orig(insn, padding)                  \
  .L\@_orig_s: insn; .L\@_orig_e:                  \
  .L\@_diff = padding;                             \
- .skip as_true(.L\@_diff > 0) * .L\@_diff, 0x90;  \
+ mknops (as_true(.L\@_diff > 0) * .L\@_diff);     \
  .L\@_orig_p:
 
 #define orig_len               (.L\@_orig_e       -     .L\@_orig_s)
xen/include/asm-x86/alternative.h +11 -2
index 4803368..7a88c43 100644
@@ -2,7 +2,6 @@
 #define __X86_ALTERNATIVE_H__
 
 #include <asm/alternative-asm.h>
-#include <asm/nops.h>
 
 #ifndef __ASSEMBLY__
 #include <xen/stringify.h>
@@ -27,6 +26,16 @@ extern void add_nops(void *insns, unsigned int len);
 extern void apply_alternatives(struct alt_instr *start, struct alt_instr *end);
 extern void alternative_instructions(void);
 
+asm ( ".macro mknops nr_bytes\n\t"
+#ifdef HAVE_AS_NOP_DIRECTIVE
+# define TOOLCHAIN_P6_NOPS 1
+      ".nops \\nr_bytes, " __stringify(ASM_NOP_MAX) "\n\t"
+#else
+# define TOOLCHAIN_P6_NOPS 0
+      ".skip \\nr_bytes, 0x90\n\t"
+#endif
+      ".endm\n\t" );
+
 #define alt_orig_len       "(.LXEN%=_orig_e - .LXEN%=_orig_s)"
 #define alt_pad_len        "(.LXEN%=_orig_p - .LXEN%=_orig_e)"
 #define alt_total_len      "(.LXEN%=_orig_p - .LXEN%=_orig_s)"
@@ -46,7 +55,7 @@ extern void alternative_instructions(void);
 #define OLDINSTR(oldinstr, padding)                              \
     ".LXEN%=_orig_s:\n\t" oldinstr "\n .LXEN%=_orig_e:\n\t"      \
     ".LXEN%=_diff = " padding "\n\t"                             \
-    ".skip "AS_TRUE"(.LXEN%=_diff > 0) * .LXEN%=_diff, 0x90\n\t" \
+    "mknops ("AS_TRUE"(.LXEN%=_diff > 0) * .LXEN%=_diff)\n\t"    \
     ".LXEN%=_orig_p:\n\t"
 
 #define OLDINSTR_1(oldinstr, n1)                                 \
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#6 lore raw

Re: [PATCH v3 5/7] x86/alt: Support for automatic padding calculations

In reply to Andrew Cooper
Quoted 19 lines
Quoted 1 line
Quoted 1 line · On 07.03.18 at 16:51, <[email protected]> wrote:
xen/arch/x86/alternative.c +4 -3
@@ -175,18 +175,47 @@ void init_or_livepatch apply_alternatives(const struct alt_instr *start,
      * So be careful if you want to change the scan order to any other
      * order.
      */
-    for ( a = start; a < end; a++ )
+    for ( a = base = start; a < end; a++ )
     {
         uint8_t *orig = ALT_ORIG_PTR(a);
         uint8_t *repl = ALT_REPL_PTR(a);
         uint8_t buf[MAX_PATCH_LEN];
+        unsigned int total_len = a->orig_len + a->pad_len;
 
-        BUG_ON(a->repl_len > a->orig_len);
-        BUG_ON(a->orig_len > sizeof(buf));
+        BUG_ON(a->repl_len > total_len);
+        BUG_ON(total_len > sizeof(buf));
         BUG_ON(a->cpuid >= NCAPINTS * 32);
 

xen/arch/x86/alternative.c void init_or_livepatch apply_alternatives(const struct alt_instr *start, · line 198

Quoted 10 lines
xen/arch/x86/alternative.c +10 -0
+        /*
+         * Detect sequences of alt_instr's patching the same origin site, and
+         * keep base pointing at the first alt_instr entry.  This is so we can
+         * refer to a single ->priv field for patching decisions.
+         *
+         * ->priv being nonzero means that the origin site has already been
+         * modified, and we shouldn't try to optimise the nops again.
+         */
+        if ( ALT_ORIG_PTR(base) != orig )
+            base = a;

I don't understand why you need the new "priv" field - have a boolean local variable which you reset instead of base here, and which you check/set instead of base->priv below.

Everything else looks fine to me.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#12 lore raw

Re: [PATCH v3 7/7] x86/build: Use new .nops directive when available

In reply to Andrew Cooper

xen/arch/x86/Rules.mk $(call as-option-add,CFLAGS,CC,"invpcid · line 35

Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 07.03.18 at 16:51, <[email protected]> wrote:
xen/arch/x86/Rules.mk +3 -0
@@ -29,6 +29,10 @@ $(call as-option-add,CFLAGS,CC,"invpcid 
 (%rax)$$(comma)%rax",-DHAVE_AS_INVPCID)
 $(call as-option-add,CFLAGS,CC,\
     ".if ((1 > 0) < 0); .error \"\";.endif",,-DHAVE_AS_NEGATIVE_TRUE)
 
+# Check to see whether the assmbler supports the .nop directive.
+$(call as-option-add,CFLAGS,CC,\
+    ".L1: .L2: .nops (.L2 - .L1)$$(comma)9",-DHAVE_AS_NOP_DIRECTIVE)

The construct is right, but the comment still has the old directive name.

xen/arch/x86/alternative.c void init_or_livepatch apply_alternatives(struct alt_instr *start, · line 212

Quoted 10 lines
xen/arch/x86/alternative.c +2 -1
@@ -207,7 +207,8 @@ void init_or_livepatch apply_alternatives(struct alt_instr *start,
             base->priv = 1;
 
             /* Nothing useful to do? */
-            if ( a->pad_len <= 1 )
+            if ( (TOOLCHAIN_P6_NOPS && ideal_nops == p6_nops) ||
+                 a->pad_len <= 1 )
                 continue;

I'm sorry, but no - we can't assume all gas versions going forward will continue to produce the NOPs we want. They may change at any time, and we may change our mind at any time. If anything you'd need to actively check that what their .nops produces matches what our table has.

Additionally such skipping on the vast majority of hardware is prone to leave bugs undiscovered for quite some time.

Anyway - I continue to fail to see the value of this patch with the immediately preceding one already doing all we need.

An alternative might be to have a Kconfig option to suppress the NOP optimization altogether, and rely on what the tool chain produces.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#7 lore raw

Re: [PATCH v3 5/7] x86/alt: Support for automatic padding calculations

In reply to Jan Beulich
Quoted 19 lines · Jan Beulich
Quoted 19 lines
Quoted 1 line
Quoted 1 line · On 07.03.18 at 16:51, <[email protected]> wrote:
xen/arch/x86/alternative.c +4 -3
@@ -175,18 +175,47 @@ void init_or_livepatch apply_alternatives(const struct alt_instr *start,
      * So be careful if you want to change the scan order to any other
      * order.
      */
-    for ( a = start; a < end; a++ )
+    for ( a = base = start; a < end; a++ )
     {
         uint8_t *orig = ALT_ORIG_PTR(a);
         uint8_t *repl = ALT_REPL_PTR(a);
         uint8_t buf[MAX_PATCH_LEN];
+        unsigned int total_len = a->orig_len + a->pad_len;
 
-        BUG_ON(a->repl_len > a->orig_len);
-        BUG_ON(a->orig_len > sizeof(buf));
+        BUG_ON(a->repl_len > total_len);
+        BUG_ON(total_len > sizeof(buf));
         BUG_ON(a->cpuid >= NCAPINTS * 32);
 
Quoted 13 lines

xen/arch/x86/alternative.c void init_or_livepatch apply_alternatives(const struct alt_instr *start, · line 198

Quoted 10 lines
xen/arch/x86/alternative.c +10 -0
+        /*
+         * Detect sequences of alt_instr's patching the same origin site, and
+         * keep base pointing at the first alt_instr entry.  This is so we can
+         * refer to a single ->priv field for patching decisions.
+         *
+         * ->priv being nonzero means that the origin site has already been
+         * modified, and we shouldn't try to optimise the nops again.
+         */
+        if ( ALT_ORIG_PTR(base) != orig )
+            base = a;

I don't understand why you need the new "priv" field - have a boolean local variable which you reset instead of base here, and which you check/set instead of base->priv below.

That can break in a "corrupted instruction stream" kind of way if we perform two passes over the same set of alt_instr's, e.g. after loading microcode, finding some new features, and rerunning alternatives.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#13 lore raw

Re: [PATCH v3 7/7] x86/build: Use new .nops directive when available

In reply to Jan Beulich
Quoted 18 lines · Jan Beulich

xen/arch/x86/Rules.mk $(call as-option-add,CFLAGS,CC,"invpcid · line 35

Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 07.03.18 at 16:51, <[email protected]> wrote:
xen/arch/x86/Rules.mk +3 -0
@@ -29,6 +29,10 @@ $(call as-option-add,CFLAGS,CC,"invpcid 
 (%rax)$$(comma)%rax",-DHAVE_AS_INVPCID)
 $(call as-option-add,CFLAGS,CC,\
     ".if ((1 > 0) < 0); .error \"\";.endif",,-DHAVE_AS_NEGATIVE_TRUE)
 
+# Check to see whether the assmbler supports the .nop directive.
+$(call as-option-add,CFLAGS,CC,\
+    ".L1: .L2: .nops (.L2 - .L1)$$(comma)9",-DHAVE_AS_NOP_DIRECTIVE)

The construct is right, but the comment still has the old directive name.

Quoted 5 lines
xen/arch/x86/alternative.c +0 -0
@@ -207,7 +207,8 @@ void init_or_livepatch apply_alternatives(struct alt_instr *start,
             base->priv = 1;
 
Quoted 10 lines

xen/arch/x86/alternative.c void init_or_livepatch apply_alternatives(struct alt_instr *start, · line 212

Quoted 5 lines
xen/arch/x86/alternative.c +2 -1
             /* Nothing useful to do? */
-            if ( a->pad_len <= 1 )
+            if ( (TOOLCHAIN_P6_NOPS && ideal_nops == p6_nops) ||
+                 a->pad_len <= 1 )
                 continue;

I'm sorry, but no - we can't assume all gas versions going forward will continue to produce the NOPs we want. They may change at any time, and we may change our mind at any time. If anything you'd need to actively check that what their .nops produces matches what our table has.

Hmm perhaps, but the chances of either of these actually happening are extremely low.

Quoted 2 lines

Additionally such skipping on the vast majority of hardware is prone to leave bugs undiscovered for quite some time.

Such as?  This statement isn't exactly helpful, and

Quoted 2 lines

Anyway - I continue to fail to see the value of this patch with the immediately preceding one already doing all we need.

The purpose, as explained before, is to avoid patching whenever possible.

"Whenever possible" is every time we fail a feature check, and the toolchain puts out the correct nops (which, for a capable toolchain, is overwhelmingly likely given our 64bit-ness), and has a direct effect on our boot time safety.

Quoted 3 lines

An alternative might be to have a Kconfig option to suppress the NOP optimization altogether, and rely on what the tool chain produces.

How and why would a user wish to change this option?  I don't think anyone would find it useful.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

Reviewed-by
#8 lore raw

Re: [PATCH v3 5/7] x86/alt: Support for automatic padding calculations

In reply to Andrew Cooper
Quoted 35 lines
Quoted 1 line
Quoted 1 line · On 08.03.18 at 17:23, <[email protected]> wrote:
Quoted 19 lines · Jan Beulich
Quoted 19 lines
Quoted 1 line
Quoted 1 line · On 07.03.18 at 16:51, <[email protected]> wrote:
xen/arch/x86/alternative.c +4 -3
@@ -175,18 +175,47 @@ void init_or_livepatch apply_alternatives(const struct alt_instr *start,
      * So be careful if you want to change the scan order to any other
      * order.
      */
-    for ( a = start; a < end; a++ )
+    for ( a = base = start; a < end; a++ )
     {
         uint8_t *orig = ALT_ORIG_PTR(a);
         uint8_t *repl = ALT_REPL_PTR(a);
         uint8_t buf[MAX_PATCH_LEN];
+        unsigned int total_len = a->orig_len + a->pad_len;
 
-        BUG_ON(a->repl_len > a->orig_len);
-        BUG_ON(a->orig_len > sizeof(buf));
+        BUG_ON(a->repl_len > total_len);
+        BUG_ON(total_len > sizeof(buf));
         BUG_ON(a->cpuid >= NCAPINTS * 32);
 
Quoted 13 lines

xen/arch/x86/alternative.c void init_or_livepatch apply_alternatives(const struct alt_instr *start, · line 198

Quoted 10 lines
xen/arch/x86/alternative.c +10 -0
+        /*
+         * Detect sequences of alt_instr's patching the same origin site, and
+         * keep base pointing at the first alt_instr entry.  This is so we can
+         * refer to a single ->priv field for patching decisions.
+         *
+         * ->priv being nonzero means that the origin site has already been
+         * modified, and we shouldn't try to optimise the nops again.
+         */
+        if ( ALT_ORIG_PTR(base) != orig )
+            base = a;

I don't understand why you need the new "priv" field - have a boolean local variable which you reset instead of base here, and which you check/set instead of base->priv below.

Quoted 3 lines

That can break in a "corrupted instruction stream" kind of way if we perform two passes over the same set of alt_instr's, e.g. after loading microcode, finding some new features, and rerunning alternatives.

Well, we don't do anything like that today (which first of all would require all that stuff to come out of .init.*). But yes, if you want the code be prepared for that, fine with me:

But it would be nice if you called out that extra aspect in the description.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#9 lore raw

Re: [PATCH v3 5/7] x86/alt: Support for automatic padding calculations

Quoted 37 lines · Jan Beulich
Quoted 34 lines
Quoted 1 line
Quoted 1 line · On 08.03.18 at 17:23, <[email protected]> wrote:
Quoted 19 lines · Jan Beulich
Quoted 19 lines
Quoted 1 line
Quoted 1 line · On 07.03.18 at 16:51, <[email protected]> wrote:
xen/arch/x86/alternative.c +4 -3
@@ -175,18 +175,47 @@ void init_or_livepatch apply_alternatives(const struct alt_instr *start,
      * So be careful if you want to change the scan order to any other
      * order.
      */
-    for ( a = start; a < end; a++ )
+    for ( a = base = start; a < end; a++ )
     {
         uint8_t *orig = ALT_ORIG_PTR(a);
         uint8_t *repl = ALT_REPL_PTR(a);
         uint8_t buf[MAX_PATCH_LEN];
+        unsigned int total_len = a->orig_len + a->pad_len;
 
-        BUG_ON(a->repl_len > a->orig_len);
-        BUG_ON(a->orig_len > sizeof(buf));
+        BUG_ON(a->repl_len > total_len);
+        BUG_ON(total_len > sizeof(buf));
         BUG_ON(a->cpuid >= NCAPINTS * 32);
 
Quoted 13 lines

xen/arch/x86/alternative.c void init_or_livepatch apply_alternatives(const struct alt_instr *start, · line 198

Quoted 10 lines
xen/arch/x86/alternative.c +10 -0
+        /*
+         * Detect sequences of alt_instr's patching the same origin site, and
+         * keep base pointing at the first alt_instr entry.  This is so we can
+         * refer to a single ->priv field for patching decisions.
+         *
+         * ->priv being nonzero means that the origin site has already been
+         * modified, and we shouldn't try to optimise the nops again.
+         */
+        if ( ALT_ORIG_PTR(base) != orig )
+            base = a;

I don't understand why you need the new "priv" field - have a boolean local variable which you reset instead of base here, and which you check/set instead of base->priv below.

Quoted 3 lines

That can break in a "corrupted instruction stream" kind of way if we perform two passes over the same set of alt_instr's, e.g. after loading microcode, finding some new features, and rerunning alternatives.

Quoted 8 lines

Well, we don't do anything like that today (which first of all would require all that stuff to come out of .init.*). But yes, if you want the code be prepared for that, fine with me:

But it would be nice if you called out that extra aspect in the description.

Sure - I'll update this comment in the code.

FWIW, I chose this specific example because alternatives handling for livepatching Spectre mitigations was a particularly sore area for some people.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#14 lore raw

Re: [PATCH v3 7/7] x86/build: Use new .nops directive when available

In reply to Andrew Cooper
Quoted 31 lines
Quoted 1 line
Quoted 1 line · On 08.03.18 at 17:48, <[email protected]> wrote:
Quoted 18 lines · Jan Beulich

xen/arch/x86/Rules.mk $(call as-option-add,CFLAGS,CC,"invpcid · line 35

Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 07.03.18 at 16:51, <[email protected]> wrote:
xen/arch/x86/Rules.mk +3 -0
@@ -29,6 +29,10 @@ $(call as-option-add,CFLAGS,CC,"invpcid 
 (%rax)$$(comma)%rax",-DHAVE_AS_INVPCID)
 $(call as-option-add,CFLAGS,CC,\
     ".if ((1 > 0) < 0); .error \"\";.endif",,-DHAVE_AS_NEGATIVE_TRUE)
 
+# Check to see whether the assmbler supports the .nop directive.
+$(call as-option-add,CFLAGS,CC,\
+    ".L1: .L2: .nops (.L2 - .L1)$$(comma)9",-DHAVE_AS_NOP_DIRECTIVE)

The construct is right, but the comment still has the old directive name.

Quoted 5 lines
xen/arch/x86/alternative.c +0 -0
@@ -207,7 +207,8 @@ void init_or_livepatch apply_alternatives(struct alt_instr *start,
             base->priv = 1;
 
Quoted 10 lines

xen/arch/x86/alternative.c void init_or_livepatch apply_alternatives(struct alt_instr *start, · line 212

Quoted 5 lines
xen/arch/x86/alternative.c +2 -1
             /* Nothing useful to do? */
-            if ( a->pad_len <= 1 )
+            if ( (TOOLCHAIN_P6_NOPS && ideal_nops == p6_nops) ||
+                 a->pad_len <= 1 )
                 continue;

I'm sorry, but no - we can't assume all gas versions going forward will continue to produce the NOPs we want. They may change at any time, and we may change our mind at any time. If anything you'd need to actively check that what their .nops produces matches what our table has.

Quoted 7 lines

Hmm perhaps, but the chances of either of these actually happening are extremely low.

Quoted 2 lines

Additionally such skipping on the vast majority of hardware is prone to leave bugs undiscovered for quite some time.

Such as? This statement isn't exactly helpful, and

Well, the code past the "continue" will effectively only get compile tested for extended periods of time. Whatever bug it is that might creep in there. (But then again your sentence looks unfinished.)

Quoted 9 lines
Quoted 2 lines

Anyway - I continue to fail to see the value of this patch with the immediately preceding one already doing all we need.

The purpose, as explained before, is to avoid patching whenever possible.

"Whenever possible" is every time we fail a feature check, and the toolchain puts out the correct nops (which, for a capable toolchain, is overwhelmingly likely given our 64bit-ness), and has a direct effect on our boot time safety.

If our patching has a bug, we'll have to fix it. Whereas (as said before) the view on what is "correct" may vary over time.

Quoted 6 lines
Quoted 3 lines

An alternative might be to have a Kconfig option to suppress the NOP optimization altogether, and rely on what the tool chain produces.

How and why would a user wish to change this option? I don't think anyone would find it useful.

If you find such an option useless, then why is this patch useful? I, for one, would leave the option off, making sure runtime NOP replacement happens at all times.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel