446cf2
commit 5d844e1b72513cf59b5e7c14295644efdcc66e44
446cf2
Author: H.J. Lu <hjl.tools@gmail.com>
446cf2
Date:   Fri Feb 14 14:45:34 2020 -0800
446cf2
446cf2
    i386: Enable CET support in ucontext functions
446cf2
    
446cf2
    1. getcontext and swapcontext are updated to save the caller's shadow
446cf2
    stack pointer and return address.
446cf2
    2. setcontext and swapcontext are updated to restore shadow stack and
446cf2
    jump to new context directly.
446cf2
    3. makecontext is updated to allocate a new shadow stack and set the
446cf2
    caller's return address to the helper code, L(exitcode).
446cf2
    4. Since we no longer save and restore EAX, ECX and EDX in getcontext,
446cf2
    setcontext and swapcontext, we can use them as scratch register slots
446cf2
    to enable CET in ucontext functions.
446cf2
    
446cf2
    Since makecontext allocates a new shadow stack when making a new
446cf2
    context and kernel allocates a new shadow stack for clone/fork/vfork
446cf2
    syscalls, we track the current shadow stack base.  In setcontext and
446cf2
    swapcontext, if the target shadow stack base is the same as the current
446cf2
    shadow stack base, we unwind the shadow stack.  Otherwise it is a stack
446cf2
    switch and we look for a restore token.
446cf2
    
446cf2
    We enable shadow stack at run-time only if program and all used shared
446cf2
    objects, including dlopened ones, are shadow stack enabled, which means
446cf2
    that they must be compiled with GCC 8 or above and glibc 2.28 or above.
446cf2
    We need to save and restore shadow stack only if shadow stack is enabled.
446cf2
    When caller of getcontext, setcontext, swapcontext and makecontext is
446cf2
    compiled with smaller ucontext_t, shadow stack won't be enabled at
446cf2
    run-time.  We check if shadow stack is enabled before accessing the
446cf2
    extended field in ucontext_t.
446cf2
    
446cf2
    Tested on i386 CET/non-CET machines.
446cf2
    
446cf2
    Reviewed-by: Carlos O'Donell <carlos@redhat.com>
446cf2
---
446cf2
446cf2
diff --git a/sysdeps/unix/sysv/linux/i386/getcontext.S b/sysdeps/unix/sysv/linux/i386/getcontext.S
446cf2
index 6637596..4ed9d03 100644
446cf2
--- a/sysdeps/unix/sysv/linux/i386/getcontext.S
446cf2
+++ b/sysdeps/unix/sysv/linux/i386/getcontext.S
446cf2
@@ -18,6 +18,7 @@
446cf2
    <http://www.gnu.org/licenses/>.  */
446cf2
 
446cf2
 #include <sysdep.h>
446cf2
+#include <asm/prctl.h>
446cf2
 
446cf2
 #include "ucontext_i.h"
446cf2
 
446cf2
@@ -42,6 +43,61 @@ ENTRY(__getcontext)
446cf2
 	movw	%fs, %dx
446cf2
 	movl	%edx, oFS(%eax)
446cf2
 
446cf2
+#if SHSTK_ENABLED
446cf2
+	/* Check if shadow stack is enabled.  */
446cf2
+	testl	$X86_FEATURE_1_SHSTK, %gs:FEATURE_1_OFFSET
446cf2
+	jz	L(no_shstk)
446cf2
+
446cf2
+	/* Save EAX in EDX.  */
446cf2
+	movl	%eax, %edx
446cf2
+
446cf2
+	xorl	%eax, %eax
446cf2
+	cmpl	%gs:SSP_BASE_OFFSET, %eax
446cf2
+	jnz	L(shadow_stack_bound_recorded)
446cf2
+
446cf2
+	/* Save EBX in the first scratch register slot.  */
446cf2
+	movl	%ebx, oSCRATCH1(%edx)
446cf2
+
446cf2
+	/* Get the base address and size of the default shadow stack
446cf2
+	   which must be the current shadow stack since nothing has
446cf2
+	   been recorded yet.  */
446cf2
+	sub	$24, %esp
446cf2
+	mov	%esp, %ecx
446cf2
+	movl	$ARCH_CET_STATUS, %ebx
446cf2
+	movl	$__NR_arch_prctl, %eax
446cf2
+	ENTER_KERNEL
446cf2
+	testl	%eax, %eax
446cf2
+	jz	L(continue_no_err)
446cf2
+
446cf2
+	/* This should never happen.  */
446cf2
+	hlt
446cf2
+
446cf2
+L(continue_no_err):
446cf2
+	/* Restore EBX from the first scratch register slot.  */
446cf2
+	movl	oSCRATCH1(%edx), %ebx
446cf2
+
446cf2
+	/* Record the base of the current shadow stack.  */
446cf2
+	movl	8(%esp), %eax
446cf2
+	movl	%eax, %gs:SSP_BASE_OFFSET
446cf2
+	add	$24, %esp
446cf2
+
446cf2
+L(shadow_stack_bound_recorded):
446cf2
+	/* Load address of the context data structure.  */
446cf2
+	movl	4(%esp), %eax
446cf2
+
446cf2
+	/* Get the current shadow stack pointer.  */
446cf2
+	rdsspd	%edx
446cf2
+	/* NB: Save the caller's shadow stack so that we can jump back
446cf2
+	   to the caller directly.  */
446cf2
+	addl	$4, %edx
446cf2
+	movl	%edx, oSSP(%eax)
446cf2
+
446cf2
+	/* Save the current shadow stack base in ucontext.  */
446cf2
+	movl	%gs:SSP_BASE_OFFSET, %edx
446cf2
+	movl	%edx, (oSSP + 4)(%eax)
446cf2
+
446cf2
+L(no_shstk):
446cf2
+#endif
446cf2
 	/* We have separate floating-point register content memory on the
446cf2
 	   stack.  We use the __fpregs_mem block in the context.  Set the
446cf2
 	   links up correctly.  */
446cf2
diff --git a/sysdeps/unix/sysv/linux/i386/makecontext.S b/sysdeps/unix/sysv/linux/i386/makecontext.S
446cf2
index e3ca3dc..2d82ddc 100644
446cf2
--- a/sysdeps/unix/sysv/linux/i386/makecontext.S
446cf2
+++ b/sysdeps/unix/sysv/linux/i386/makecontext.S
446cf2
@@ -18,6 +18,7 @@
446cf2
    <http://www.gnu.org/licenses/>.  */
446cf2
 
446cf2
 #include <sysdep.h>
446cf2
+#include <asm/prctl.h>
446cf2
 
446cf2
 #include "ucontext_i.h"
446cf2
 
446cf2
@@ -68,6 +69,127 @@ ENTRY(__makecontext)
446cf2
 	jnz	1b
446cf2
 2:
446cf2
 
446cf2
+#if SHSTK_ENABLED
446cf2
+	/* Check if Shadow Stack is enabled.  */
446cf2
+	testl	$X86_FEATURE_1_SHSTK, %gs:FEATURE_1_OFFSET
446cf2
+	jz	L(skip_ssp)
446cf2
+
446cf2
+	/* Reload the pointer to ucontext.  */
446cf2
+	movl	4(%esp), %eax
446cf2
+
446cf2
+	/* Shadow stack is enabled.  We need to allocate a new shadow
446cf2
+	   stack.  */
446cf2
+	subl	oSS_SP(%eax), %edx
446cf2
+	shrl	$STACK_SIZE_TO_SHADOW_STACK_SIZE_SHIFT, %edx
446cf2
+
446cf2
+	/* Align shadow stack size to 8 bytes.  */
446cf2
+	addl	$7, %edx
446cf2
+	andl	$-8, %edx
446cf2
+
446cf2
+	/* Store shadow stack size in __ssp[2].  */
446cf2
+	movl	%edx, (oSSP + 8)(%eax)
446cf2
+
446cf2
+	/* Save ESI in the second scratch register slot.  */
446cf2
+	movl	%esi, oSCRATCH2(%eax)
446cf2
+	/* Save EDI in the third scratch register slot.  */
446cf2
+	movl	%edi, oSCRATCH3(%eax)
446cf2
+
446cf2
+	/* Save the pointer to ucontext.  */
446cf2
+	movl	%eax, %edi
446cf2
+
446cf2
+	/* Get the original shadow stack pointer.  */
446cf2
+	rdsspd	%esi
446cf2
+
446cf2
+	/* Align the saved original shadow stack pointer to the next
446cf2
+	   8 byte aligned boundary.  */
446cf2
+	andl	$-8, %esi
446cf2
+
446cf2
+	/* Load the top of the new stack into EDX.  */
446cf2
+	movl	oESP(%eax), %edx
446cf2
+
446cf2
+	/* We need to terminate the FDE here because the unwinder looks
446cf2
+	   at ra-1 for unwind information.  */
446cf2
+	cfi_endproc
446cf2
+
446cf2
+	/* Swap the original stack pointer with the top of the new
446cf2
+	   stack.  */
446cf2
+	xchgl	%esp, %edx
446cf2
+
446cf2
+	/* Add 4 bytes since CALL will push the 4-byte return address
446cf2
+	   onto stack.  */
446cf2
+	addl	$4, %esp
446cf2
+
446cf2
+	/* Allocate the new shadow stack.  Save EBX in the first scratch
446cf2
+	   register slot.  */
446cf2
+	movl	%ebx, oSCRATCH1(%eax)
446cf2
+
446cf2
+	/* CET syscall takes 64-bit sizes.  */
446cf2
+	subl	$16, %esp
446cf2
+	movl	(oSSP + 8)(%eax), %ecx
446cf2
+	movl	%ecx, (%esp)
446cf2
+	movl	$0, 4(%esp)
446cf2
+	movl	%ecx, 8(%esp)
446cf2
+	movl	$0, 12(%esp)
446cf2
+	movl	%esp, %ecx
446cf2
+
446cf2
+	movl	$ARCH_CET_ALLOC_SHSTK, %ebx
446cf2
+	movl	$__NR_arch_prctl, %eax
446cf2
+	ENTER_KERNEL
446cf2
+	testl	%eax, %eax
446cf2
+	jne	L(hlt)		/* This should never happen.  */
446cf2
+
446cf2
+	/* Copy the base address of the new shadow stack to __ssp[1].  */
446cf2
+	movl	(%esp), %eax
446cf2
+	movl	%eax, (oSSP + 4)(%edi)
446cf2
+
446cf2
+	addl	$16, %esp
446cf2
+
446cf2
+	/* Restore EBX from the first scratch register slot.  */
446cf2
+	movl	oSCRATCH1(%edi), %ebx
446cf2
+
446cf2
+	/* Get the size of the new shadow stack.  */
446cf2
+	movl	(oSSP + 8)(%edi), %ecx
446cf2
+
446cf2
+	/* Use the restore stoken to restore the new shadow stack.  */
446cf2
+	rstorssp -8(%eax, %ecx)
446cf2
+
446cf2
+	/* Save the restore token at the next 8 byte aligned boundary
446cf2
+	   on the original shadow stack.  */
446cf2
+	saveprevssp
446cf2
+
446cf2
+	/* Push the address of "jmp exitcode" onto the new stack as
446cf2
+	   well as the new shadow stack.  */
446cf2
+	call	1f
446cf2
+	jmp	L(exitcode)
446cf2
+1:
446cf2
+
446cf2
+	/* Get the new shadow stack pointer.  */
446cf2
+	rdsspd	%eax
446cf2
+
446cf2
+	/* Use the restore stoken to restore the original shadow stack.  */
446cf2
+	rstorssp -8(%esi)
446cf2
+
446cf2
+	/* Save the restore token on the new shadow stack.  */
446cf2
+	saveprevssp
446cf2
+
446cf2
+	/* Store the new shadow stack pointer in __ssp[0].  */
446cf2
+	movl	%eax, oSSP(%edi)
446cf2
+
446cf2
+	/* Restore the original stack.  */
446cf2
+	mov	%edx, %esp
446cf2
+
446cf2
+	cfi_startproc
446cf2
+
446cf2
+	/* Restore ESI from the second scratch register slot.  */
446cf2
+	movl	oSCRATCH2(%edi), %esi
446cf2
+	/* Restore EDI from the third scratch register slot.  */
446cf2
+	movl	oSCRATCH3(%edi), %edi
446cf2
+
446cf2
+	ret
446cf2
+
446cf2
+L(skip_ssp):
446cf2
+#endif
446cf2
+
446cf2
 	/* If the function we call returns we must continue with the
446cf2
 	   context which is given in the uc_link element.  To do this
446cf2
 	   set the return address for the function the user provides
446cf2
@@ -123,6 +245,7 @@ L(call_exit):
446cf2
 	call	HIDDEN_JUMPTARGET(exit)
446cf2
 	/* The 'exit' call should never return.  In case it does cause
446cf2
 	   the process to terminate.  */
446cf2
+L(hlt):
446cf2
 	hlt
446cf2
 	cfi_startproc
446cf2
 END(__makecontext)
446cf2
diff --git a/sysdeps/unix/sysv/linux/i386/setcontext.S b/sysdeps/unix/sysv/linux/i386/setcontext.S
446cf2
index 7565d7d..7b58918 100644
446cf2
--- a/sysdeps/unix/sysv/linux/i386/setcontext.S
446cf2
+++ b/sysdeps/unix/sysv/linux/i386/setcontext.S
446cf2
@@ -18,6 +18,7 @@
446cf2
    <http://www.gnu.org/licenses/>.  */
446cf2
 
446cf2
 #include <sysdep.h>
446cf2
+#include <asm/prctl.h>
446cf2
 
446cf2
 #include "ucontext_i.h"
446cf2
 
446cf2
@@ -56,9 +57,6 @@ ENTRY(__setcontext)
446cf2
 	movl	oFS(%eax), %ecx
446cf2
 	movw	%cx, %fs
446cf2
 
446cf2
-	/* Fetch the address to return to.  */
446cf2
-	movl	oEIP(%eax), %ecx
446cf2
-
446cf2
 	/* Load the new stack pointer.  */
446cf2
 	cfi_def_cfa (eax, 0)
446cf2
 	cfi_offset (edi, oEDI)
446cf2
@@ -67,6 +65,103 @@ ENTRY(__setcontext)
446cf2
 	cfi_offset (ebx, oEBX)
446cf2
 	movl	oESP(%eax), %esp
446cf2
 
446cf2
+#if SHSTK_ENABLED
446cf2
+	/* Check if Shadow Stack is enabled.  */
446cf2
+	testl	$X86_FEATURE_1_SHSTK, %gs:FEATURE_1_OFFSET
446cf2
+	jz	L(no_shstk)
446cf2
+
446cf2
+	/* If the base of the target shadow stack is the same as the
446cf2
+	   base of the current shadow stack, we unwind the shadow
446cf2
+	   stack.  Otherwise it is a stack switch and we look for a
446cf2
+	   restore token.  */
446cf2
+	movl	oSSP(%eax), %esi
446cf2
+	movl	%esi, %edi
446cf2
+
446cf2
+	/* Get the base of the target shadow stack.  */
446cf2
+	movl	(oSSP + 4)(%eax), %ecx
446cf2
+	cmpl	%gs:SSP_BASE_OFFSET, %ecx
446cf2
+	je	L(unwind_shadow_stack)
446cf2
+
446cf2
+	/* Align the saved original shadow stack pointer to the next
446cf2
+	   8 byte aligned boundary.  */
446cf2
+	andl	$-8, %esi
446cf2
+
446cf2
+L(find_restore_token_loop):
446cf2
+	/* Look for a restore token.  */
446cf2
+	movl	-8(%esi), %ebx
446cf2
+	andl	$-8, %ebx
446cf2
+	cmpl	%esi, %ebx
446cf2
+	je	L(restore_shadow_stack)
446cf2
+
446cf2
+	/* Try the next slot.  */
446cf2
+	subl	$8, %esi
446cf2
+	jmp	L(find_restore_token_loop)
446cf2
+
446cf2
+L(restore_shadow_stack):
446cf2
+	/* Pop return address from the shadow stack since setcontext
446cf2
+	   will not return.  */
446cf2
+	movl	$1, %ebx
446cf2
+	incsspd	%ebx
446cf2
+
446cf2
+	/* Use the restore stoken to restore the target shadow stack.  */
446cf2
+	rstorssp -8(%esi)
446cf2
+
446cf2
+	/* Save the restore token on the old shadow stack.  NB: This
446cf2
+	   restore token may be checked by setcontext or swapcontext
446cf2
+	   later.  */
446cf2
+	saveprevssp
446cf2
+
446cf2
+	/* Record the new shadow stack base that was switched to.  */
446cf2
+	movl	(oSSP + 4)(%eax), %ebx
446cf2
+	movl	%ebx, %gs:SSP_BASE_OFFSET
446cf2
+
446cf2
+L(unwind_shadow_stack):
446cf2
+	rdsspd	%ebx
446cf2
+	subl	%edi, %ebx
446cf2
+	je	L(skip_unwind_shadow_stack)
446cf2
+	negl	%ebx
446cf2
+	shrl	$2, %ebx
446cf2
+	movl	$255, %esi
446cf2
+L(loop):
446cf2
+	cmpl	%esi, %ebx
446cf2
+	cmovb	%ebx, %esi
446cf2
+	incsspd	%esi
446cf2
+	subl	%esi, %ebx
446cf2
+	ja	L(loop)
446cf2
+
446cf2
+L(skip_unwind_shadow_stack):
446cf2
+
446cf2
+	/* Load the values of all the preserved registers (except ESP).  */
446cf2
+	movl	oEDI(%eax), %edi
446cf2
+	movl	oESI(%eax), %esi
446cf2
+	movl	oEBP(%eax), %ebp
446cf2
+	movl	oEBX(%eax), %ebx
446cf2
+
446cf2
+	/* Get the return address set with getcontext.  */
446cf2
+	movl	oEIP(%eax), %ecx
446cf2
+
446cf2
+	/* Check if return address is valid for the case when setcontext
446cf2
+	   is invoked from L(exitcode) with linked context.  */
446cf2
+	rdsspd	%eax
446cf2
+	cmpl	(%eax), %ecx
446cf2
+	/* Clear EAX to indicate success.  NB: Don't use xorl to keep
446cf2
+	   EFLAGS for jne.  */
446cf2
+	movl	$0, %eax
446cf2
+	jne	L(jmp)
446cf2
+	/* Return to the new context if return address valid.  */
446cf2
+	pushl	%ecx
446cf2
+	ret
446cf2
+
446cf2
+L(jmp):
446cf2
+	/* Jump to the new context directly.  */
446cf2
+	jmp	*%ecx
446cf2
+
446cf2
+L(no_shstk):
446cf2
+#endif
446cf2
+
446cf2
+	/* Fetch the address to return to.  */
446cf2
+	movl	oEIP(%eax), %ecx
446cf2
+
446cf2
 	/* Push the return address on the new stack so we can return there.  */
446cf2
 	pushl	%ecx
446cf2
 
446cf2
diff --git a/sysdeps/unix/sysv/linux/i386/swapcontext.S b/sysdeps/unix/sysv/linux/i386/swapcontext.S
446cf2
index ce27d51..d1b648c 100644
446cf2
--- a/sysdeps/unix/sysv/linux/i386/swapcontext.S
446cf2
+++ b/sysdeps/unix/sysv/linux/i386/swapcontext.S
446cf2
@@ -18,6 +18,7 @@
446cf2
    <http://www.gnu.org/licenses/>.  */
446cf2
 
446cf2
 #include <sysdep.h>
446cf2
+#include <asm/prctl.h>
446cf2
 
446cf2
 #include "ucontext_i.h"
446cf2
 
446cf2
@@ -76,6 +77,144 @@ ENTRY(__swapcontext)
446cf2
 	movl	oFS(%eax), %edx
446cf2
 	movw	%dx, %fs
446cf2
 
446cf2
+#if SHSTK_ENABLED
446cf2
+	/* Check if Shadow Stack is enabled.  */
446cf2
+	testl	$X86_FEATURE_1_SHSTK, %gs:FEATURE_1_OFFSET
446cf2
+	jz	L(no_shstk)
446cf2
+
446cf2
+	xorl	%eax, %eax
446cf2
+	cmpl	%gs:SSP_BASE_OFFSET, %eax
446cf2
+	jnz	L(shadow_stack_bound_recorded)
446cf2
+
446cf2
+	/* Get the base address and size of the default shadow stack
446cf2
+	   which must be the current shadow stack since nothing has
446cf2
+	   been recorded yet.  */
446cf2
+	sub	$24, %esp
446cf2
+	mov	%esp, %ecx
446cf2
+	movl	$ARCH_CET_STATUS, %ebx
446cf2
+	movl	$__NR_arch_prctl, %eax
446cf2
+	ENTER_KERNEL
446cf2
+	testl	%eax, %eax
446cf2
+	jz	L(continue_no_err)
446cf2
+
446cf2
+	/* This should never happen.  */
446cf2
+	hlt
446cf2
+
446cf2
+L(continue_no_err):
446cf2
+	/* Record the base of the current shadow stack.  */
446cf2
+	movl	8(%esp), %eax
446cf2
+	movl	%eax, %gs:SSP_BASE_OFFSET
446cf2
+	add	$24, %esp
446cf2
+
446cf2
+L(shadow_stack_bound_recorded):
446cf2
+	/* Load address of the context data structure we save in.  */
446cf2
+	movl	4(%esp), %eax
446cf2
+
446cf2
+	/* Load address of the context data structure we swap in  */
446cf2
+	movl	8(%esp), %edx
446cf2
+
446cf2
+       /* If we unwind the stack, we can't undo stack unwinding.  Just
446cf2
+	   save the target shadow stack pointer as the current shadow
446cf2
+	   stack pointer.   */
446cf2
+	movl	oSSP(%edx), %ecx
446cf2
+	movl	%ecx, oSSP(%eax)
446cf2
+
446cf2
+	/* Save the current shadow stack base in ucontext.  */
446cf2
+	movl	%gs:SSP_BASE_OFFSET, %ecx
446cf2
+	movl	%ecx, (oSSP + 4)(%eax)
446cf2
+
446cf2
+	/* If the base of the target shadow stack is the same as the
446cf2
+	   base of the current shadow stack, we unwind the shadow
446cf2
+	   stack.  Otherwise it is a stack switch and we look for a
446cf2
+	   restore token.  */
446cf2
+	movl	oSSP(%edx), %esi
446cf2
+	movl	%esi, %edi
446cf2
+
446cf2
+	/* Get the base of the target shadow stack.  */
446cf2
+	movl	(oSSP + 4)(%edx), %ecx
446cf2
+	cmpl	%gs:SSP_BASE_OFFSET, %ecx
446cf2
+	je	L(unwind_shadow_stack)
446cf2
+
446cf2
+	/* Align the saved original shadow stack pointer to the next
446cf2
+	   8 byte aligned boundary.  */
446cf2
+	andl	$-8, %esi
446cf2
+
446cf2
+L(find_restore_token_loop):
446cf2
+	/* Look for a restore token.  */
446cf2
+	movl	-8(%esi), %ebx
446cf2
+	andl	$-8, %ebx
446cf2
+	cmpl	%esi, %ebx
446cf2
+	je	L(restore_shadow_stack)
446cf2
+
446cf2
+	/* Try the next slot.  */
446cf2
+	subl	$8, %esi
446cf2
+	jmp	L(find_restore_token_loop)
446cf2
+
446cf2
+L(restore_shadow_stack):
446cf2
+	/* The target shadow stack will be restored.  Save the current
446cf2
+	   shadow stack pointer.  */
446cf2
+	rdsspd	%ecx
446cf2
+	movl	%ecx, oSSP(%eax)
446cf2
+
446cf2
+	/* Use the restore stoken to restore the target shadow stack.  */
446cf2
+	rstorssp -8(%esi)
446cf2
+
446cf2
+	/* Save the restore token on the old shadow stack.  NB: This
446cf2
+	   restore token may be checked by setcontext or swapcontext
446cf2
+	   later.  */
446cf2
+	saveprevssp
446cf2
+
446cf2
+	/* Record the new shadow stack base that was switched to.  */
446cf2
+	movl	(oSSP + 4)(%edx), %ebx
446cf2
+	movl	%ebx, %gs:SSP_BASE_OFFSET
446cf2
+
446cf2
+L(unwind_shadow_stack):
446cf2
+	rdsspd	%ebx
446cf2
+	subl	%edi, %ebx
446cf2
+	je	L(skip_unwind_shadow_stack)
446cf2
+	negl	%ebx
446cf2
+	shrl	$2, %ebx
446cf2
+	movl	$255, %esi
446cf2
+L(loop):
446cf2
+	cmpl	%esi, %ebx
446cf2
+	cmovb	%ebx, %esi
446cf2
+	incsspd	%esi
446cf2
+	subl	%esi, %ebx
446cf2
+	ja	L(loop)
446cf2
+
446cf2
+L(skip_unwind_shadow_stack):
446cf2
+
446cf2
+	/* Load the new stack pointer.  */
446cf2
+	movl	oESP(%edx), %esp
446cf2
+
446cf2
+	/* Load the values of all the preserved registers (except ESP).  */
446cf2
+	movl	oEDI(%edx), %edi
446cf2
+	movl	oESI(%edx), %esi
446cf2
+	movl	oEBP(%edx), %ebp
446cf2
+	movl	oEBX(%edx), %ebx
446cf2
+
446cf2
+	/* Get the return address set with getcontext.  */
446cf2
+	movl	oEIP(%edx), %ecx
446cf2
+
446cf2
+	/* Check if return address is valid for the case when setcontext
446cf2
+	   is invoked from L(exitcode) with linked context.  */
446cf2
+	rdsspd	%eax
446cf2
+	cmpl	(%eax), %ecx
446cf2
+	/* Clear EAX to indicate success.  NB: Don't use xorl to keep
446cf2
+	   EFLAGS for jne.  */
446cf2
+	movl	$0, %eax
446cf2
+	jne	L(jmp)
446cf2
+	/* Return to the new context if return address valid.  */
446cf2
+	pushl	%ecx
446cf2
+	ret
446cf2
+
446cf2
+L(jmp):
446cf2
+	/* Jump to the new context directly.  */
446cf2
+	jmp	*%ecx
446cf2
+
446cf2
+L(no_shstk):
446cf2
+#endif
446cf2
+
446cf2
 	/* Fetch the address to return to.  */
446cf2
 	movl	oEIP(%eax), %ecx
446cf2
 
446cf2
diff --git a/sysdeps/unix/sysv/linux/i386/sysdep.h b/sysdeps/unix/sysv/linux/i386/sysdep.h
446cf2
index 3255cc7..9344ac7 100644
446cf2
--- a/sysdeps/unix/sysv/linux/i386/sysdep.h
446cf2
+++ b/sysdeps/unix/sysv/linux/i386/sysdep.h
446cf2
@@ -656,4 +656,9 @@ struct libc_do_syscall_args
446cf2
 # endif
446cf2
 #endif
446cf2
 
446cf2
+/* Each shadow stack slot takes 4 bytes.  Assuming that each stack
446cf2
+   frame takes 128 bytes, this is used to compute shadow stack size
446cf2
+   from stack size.  */
446cf2
+#define STACK_SIZE_TO_SHADOW_STACK_SIZE_SHIFT 5
446cf2
+
446cf2
 #endif /* linux/i386/sysdep.h */
446cf2
diff --git a/sysdeps/unix/sysv/linux/i386/ucontext_i.sym b/sysdeps/unix/sysv/linux/i386/ucontext_i.sym
446cf2
index 1dfe03d..1d8608e 100644
446cf2
--- a/sysdeps/unix/sysv/linux/i386/ucontext_i.sym
446cf2
+++ b/sysdeps/unix/sysv/linux/i386/ucontext_i.sym
446cf2
@@ -22,6 +22,10 @@ oEBP		mreg (EBP)
446cf2
 oESP		mreg (ESP)
446cf2
 oEBX		mreg (EBX)
446cf2
 oEIP		mreg (EIP)
446cf2
+oSCRATCH1	mreg (EAX)
446cf2
+oSCRATCH2	mreg (ECX)
446cf2
+oSCRATCH3	mreg (EDX)
446cf2
 oFPREGS		mcontext (fpregs)
446cf2
 oSIGMASK	ucontext (uc_sigmask)
446cf2
 oFPREGSMEM	ucontext (__fpregs_mem)
446cf2
+oSSP		ucontext (__ssp)
446cf2