246e17
commit 15eab1e3e89129ab3ed03f5bdc3415b26e9caeb9
246e17
Author: H.J. Lu <hjl.tools@gmail.com>
246e17
Date:   Sat Feb 1 05:44:55 2020 -0800
246e17
246e17
    i386: Don't unnecessarily save and restore EAX, ECX and EDX [BZ# 25262]
246e17
    
246e17
    On i386, since EAX, ECX and EDX are caller-saved, there are no need
246e17
    to save and restore EAX, ECX and EDX in getcontext, setcontext and
246e17
    swapcontext.  They just need to clear EAX on success.  The extra
246e17
    scratch registers are needed to enable CET.
246e17
    
246e17
    Tested on i386.
246e17
    
246e17
    Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
246e17
---
246e17
246e17
diff --git a/sysdeps/unix/sysv/linux/i386/getcontext.S b/sysdeps/unix/sysv/linux/i386/getcontext.S
246e17
index 26ca08a..6637596 100644
246e17
--- a/sysdeps/unix/sysv/linux/i386/getcontext.S
246e17
+++ b/sysdeps/unix/sysv/linux/i386/getcontext.S
246e17
@@ -26,13 +26,7 @@ ENTRY(__getcontext)
246e17
 	/* Load address of the context data structure.  */
246e17
 	movl	4(%esp), %eax
246e17
 
246e17
-	/* Return value of getcontext.  EAX is the only register whose
246e17
-	   value is not preserved.  */
246e17
-	movl	$0, oEAX(%eax)
246e17
-
246e17
-	/* Save the 32-bit register values and the return address.  */
246e17
-	movl	%ecx, oECX(%eax)
246e17
-	movl	%edx, oEDX(%eax)
246e17
+	/* Save the preserved register values and the return address.  */
246e17
 	movl	%edi, oEDI(%eax)
246e17
 	movl	%esi, oESI(%eax)
246e17
 	movl	%ebp, oEBP(%eax)
246e17
diff --git a/sysdeps/unix/sysv/linux/i386/setcontext.S b/sysdeps/unix/sysv/linux/i386/setcontext.S
246e17
index a604fca..7565d7d 100644
246e17
--- a/sysdeps/unix/sysv/linux/i386/setcontext.S
246e17
+++ b/sysdeps/unix/sysv/linux/i386/setcontext.S
246e17
@@ -65,22 +65,19 @@ ENTRY(__setcontext)
246e17
 	cfi_offset (esi, oESI)
246e17
 	cfi_offset (ebp, oEBP)
246e17
 	cfi_offset (ebx, oEBX)
246e17
-	cfi_offset (edx, oEDX)
246e17
-	cfi_offset (ecx, oECX)
246e17
 	movl	oESP(%eax), %esp
246e17
 
246e17
 	/* Push the return address on the new stack so we can return there.  */
246e17
 	pushl	%ecx
246e17
 
246e17
-	/* Load the values of all the 32-bit registers (except ESP).
246e17
-	   Since we are loading from EAX, it must be last.  */
246e17
+	/* Load the values of all the preserved registers (except ESP).  */
246e17
 	movl	oEDI(%eax), %edi
246e17
 	movl	oESI(%eax), %esi
246e17
 	movl	oEBP(%eax), %ebp
246e17
 	movl	oEBX(%eax), %ebx
246e17
-	movl	oEDX(%eax), %edx
246e17
-	movl	oECX(%eax), %ecx
246e17
-	movl	oEAX(%eax), %eax
246e17
+
246e17
+	/* All done, return 0 for success.  */
246e17
+	xorl	%eax, %eax
246e17
 
246e17
 	/* End FDE here, we fall into another context.  */
246e17
 	cfi_endproc
246e17
diff --git a/sysdeps/unix/sysv/linux/i386/swapcontext.S b/sysdeps/unix/sysv/linux/i386/swapcontext.S
246e17
index 431f22c..ce27d51 100644
246e17
--- a/sysdeps/unix/sysv/linux/i386/swapcontext.S
246e17
+++ b/sysdeps/unix/sysv/linux/i386/swapcontext.S
246e17
@@ -26,13 +26,7 @@ ENTRY(__swapcontext)
246e17
 	/* Load address of the context data structure we save in.  */
246e17
 	movl	4(%esp), %eax
246e17
 
246e17
-	/* Return value of swapcontext.  EAX is the only register whose
246e17
-	   value is not preserved.  */
246e17
-	movl	$0, oEAX(%eax)
246e17
-
246e17
-	/* Save the 32-bit register values and the return address.  */
246e17
-	movl	%ecx, oECX(%eax)
246e17
-	movl	%edx, oEDX(%eax)
246e17
+	/* Save the preserved register values and the return address.  */
246e17
 	movl	%edi, oEDI(%eax)
246e17
 	movl	%esi, oESI(%eax)
246e17
 	movl	%ebp, oEBP(%eax)
246e17
@@ -91,15 +85,14 @@ ENTRY(__swapcontext)
246e17
 	/* Push the return address on the new stack so we can return there.  */
246e17
 	pushl	%ecx
246e17
 
246e17
-	/* Load the values of all the 32-bit registers (except ESP).
246e17
-	   Since we are loading from EAX, it must be last.  */
246e17
+	/* Load the values of all the preserved registers (except ESP).  */
246e17
 	movl	oEDI(%eax), %edi
246e17
 	movl	oESI(%eax), %esi
246e17
 	movl	oEBP(%eax), %ebp
246e17
 	movl	oEBX(%eax), %ebx
246e17
-	movl	oEDX(%eax), %edx
246e17
-	movl	oECX(%eax), %ecx
246e17
-	movl	oEAX(%eax), %eax
246e17
+
246e17
+	/* All done, return 0 for success.  */
246e17
+	xorl	%eax, %eax
246e17
 
246e17
 	/* The following 'ret' will pop the address of the code and jump
246e17
 	   to it.  */
246e17
diff --git a/sysdeps/unix/sysv/linux/i386/ucontext_i.sym b/sysdeps/unix/sysv/linux/i386/ucontext_i.sym
246e17
index b11a550..1dfe03d 100644
246e17
--- a/sysdeps/unix/sysv/linux/i386/ucontext_i.sym
246e17
+++ b/sysdeps/unix/sysv/linux/i386/ucontext_i.sym
246e17
@@ -21,9 +21,6 @@ oESI		mreg (ESI)
246e17
 oEBP		mreg (EBP)
246e17
 oESP		mreg (ESP)
246e17
 oEBX		mreg (EBX)
246e17
-oEDX		mreg (EDX)
246e17
-oECX		mreg (ECX)
246e17
-oEAX		mreg (EAX)
246e17
 oEIP		mreg (EIP)
246e17
 oFPREGS		mcontext (fpregs)
246e17
 oSIGMASK	ucontext (uc_sigmask)
246e17