08c3a6
commit e805606193e1a39956ca5ef73cb44a8796730686
08c3a6
Author: Noah Goldstein <goldstein.w.n@gmail.com>
08c3a6
Date:   Mon Jun 6 21:11:28 2022 -0700
08c3a6
08c3a6
    x86: Add COND_VZEROUPPER that can replace vzeroupper if no `ret`
08c3a6
    
08c3a6
    The RTM vzeroupper mitigation has no way of replacing inline
08c3a6
    vzeroupper not before a return.
08c3a6
    
08c3a6
    This can be useful when hoisting a vzeroupper to save code size
08c3a6
    for example:
08c3a6
    
08c3a6
    ```
08c3a6
    L(foo):
08c3a6
            cmpl    %eax, %edx
08c3a6
            jz      L(bar)
08c3a6
            tzcntl  %eax, %eax
08c3a6
            addq    %rdi, %rax
08c3a6
            VZEROUPPER_RETURN
08c3a6
    
08c3a6
    L(bar):
08c3a6
            xorl    %eax, %eax
08c3a6
            VZEROUPPER_RETURN
08c3a6
    ```
08c3a6
    
08c3a6
    Can become:
08c3a6
    
08c3a6
    ```
08c3a6
    L(foo):
08c3a6
            COND_VZEROUPPER
08c3a6
            cmpl    %eax, %edx
08c3a6
            jz      L(bar)
08c3a6
            tzcntl  %eax, %eax
08c3a6
            addq    %rdi, %rax
08c3a6
            ret
08c3a6
    
08c3a6
    L(bar):
08c3a6
            xorl    %eax, %eax
08c3a6
            ret
08c3a6
    ```
08c3a6
    
08c3a6
    This code does not change any existing functionality.
08c3a6
    
08c3a6
    There is no difference in the objdump of libc.so before and after this
08c3a6
    patch.
08c3a6
    Reviewed-by: H.J. Lu <hjl.tools@gmail.com>
08c3a6
    
08c3a6
    (cherry picked from commit dd5c483b2598f411428df4d8864c15c4b8a3cd68)
08c3a6
08c3a6
diff --git a/sysdeps/x86_64/multiarch/avx-rtm-vecs.h b/sysdeps/x86_64/multiarch/avx-rtm-vecs.h
08c3a6
index 3f531dd47fceefe9..6ca9f5e6bae7ba72 100644
08c3a6
--- a/sysdeps/x86_64/multiarch/avx-rtm-vecs.h
08c3a6
+++ b/sysdeps/x86_64/multiarch/avx-rtm-vecs.h
08c3a6
@@ -20,6 +20,7 @@
08c3a6
 #ifndef _AVX_RTM_VECS_H
08c3a6
 #define _AVX_RTM_VECS_H			1
08c3a6
 
08c3a6
+#define COND_VZEROUPPER			COND_VZEROUPPER_XTEST
08c3a6
 #define ZERO_UPPER_VEC_REGISTERS_RETURN	\
08c3a6
 	ZERO_UPPER_VEC_REGISTERS_RETURN_XTEST
08c3a6
 
08c3a6
diff --git a/sysdeps/x86_64/sysdep.h b/sysdeps/x86_64/sysdep.h
08c3a6
index 7bebdeb21095eda0..93e44be22e2275f1 100644
08c3a6
--- a/sysdeps/x86_64/sysdep.h
08c3a6
+++ b/sysdeps/x86_64/sysdep.h
08c3a6
@@ -106,6 +106,24 @@ lose:									      \
08c3a6
 	vzeroupper;						\
08c3a6
 	ret
08c3a6
 
08c3a6
+/* Can be used to replace vzeroupper that is not directly before a
08c3a6
+   return.  This is useful when hoisting a vzeroupper from multiple
08c3a6
+   return paths to decrease the total number of vzerouppers and code
08c3a6
+   size.  */
08c3a6
+#define COND_VZEROUPPER_XTEST							\
08c3a6
+    xtest;							\
08c3a6
+    jz 1f;							\
08c3a6
+    vzeroall;							\
08c3a6
+    jmp 2f;							\
08c3a6
+1:							\
08c3a6
+    vzeroupper;							\
08c3a6
+2:
08c3a6
+
08c3a6
+/* In RTM define this as COND_VZEROUPPER_XTEST.  */
08c3a6
+#ifndef COND_VZEROUPPER
08c3a6
+# define COND_VZEROUPPER vzeroupper
08c3a6
+#endif
08c3a6
+
08c3a6
 /* Zero upper vector registers and return.  */
08c3a6
 #ifndef ZERO_UPPER_VEC_REGISTERS_RETURN
08c3a6
 # define ZERO_UPPER_VEC_REGISTERS_RETURN \