Blame sysdeps/i386/__longjmp.S

Packit 6c4009
/* longjmp for i386.
Packit 6c4009
   Copyright (C) 1995-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <sysdep.h>
Packit 6c4009
#include <jmpbuf-offsets.h>
Packit 6c4009
#include <jmp_buf-ssp.h>
Packit 6c4009
#include <asm-syntax.h>
Packit 6c4009
#include <stap-probe.h>
Packit 6c4009
Packit 6c4009
/* Don't restore shadow stack register if
Packit 6c4009
   1. Shadow stack isn't enabled.  Or
Packit 6c4009
   2. __longjmp is defined for __longjmp_cancel.
Packit 6c4009
 */
Packit 6c4009
#if !SHSTK_ENABLED || defined __longjmp
Packit 6c4009
# undef SHADOW_STACK_POINTER_OFFSET
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
	.text
Packit 6c4009
ENTRY (__longjmp)
Packit 6c4009
#ifdef PTR_DEMANGLE
Packit 6c4009
	movl 4(%esp), %eax	/* User's jmp_buf in %eax.  */
Packit 6c4009
Packit 6c4009
# ifdef SHADOW_STACK_POINTER_OFFSET
Packit 6c4009
#  if IS_IN (libc) && defined SHARED && defined FEATURE_1_OFFSET
Packit 6c4009
	/* Check if Shadow Stack is enabled.  */
Packit 6c4009
	testl $X86_FEATURE_1_SHSTK, %gs:FEATURE_1_OFFSET
Packit 6c4009
	jz L(skip_ssp)
Packit 6c4009
#  else
Packit 6c4009
	xorl %edx, %edx
Packit 6c4009
#  endif
Packit 6c4009
	/* Check and adjust the Shadow-Stack-Pointer.  */
Packit 6c4009
	rdsspd %edx
Packit 6c4009
	/* And compare it with the saved ssp value.  */
Packit 6c4009
	subl SHADOW_STACK_POINTER_OFFSET(%eax), %edx
Packit 6c4009
	je L(skip_ssp)
Packit 6c4009
	/* Count the number of frames to adjust and adjust it
Packit 6c4009
	   with incssp instruction.  The instruction can adjust
Packit 6c4009
	   the ssp by [0..255] value only thus use a loop if
Packit 6c4009
	   the number of frames is bigger than 255.  */
Packit 6c4009
	negl %edx
Packit 6c4009
	shrl $2, %edx
Packit 6c4009
	/* NB: We saved Shadow-Stack-Pointer of setjmp.  Since we are
Packit 6c4009
	       restoring Shadow-Stack-Pointer of setjmp's caller, we
Packit 6c4009
	       need to unwind shadow stack by one more frame.  */
Packit 6c4009
	addl $1, %edx
Packit 6c4009
	movl	$255, %ebx
Packit 6c4009
L(loop):
Packit 6c4009
	cmpl	%ebx, %edx
Packit 6c4009
	cmovb	%edx, %ebx
Packit 6c4009
	incsspd	%ebx
Packit 6c4009
	subl	%ebx, %edx
Packit 6c4009
	ja	L(loop)
Packit 6c4009
L(skip_ssp):
Packit 6c4009
# endif
Packit 6c4009
	/* Save the return address now.  */
Packit 6c4009
	movl (JB_PC*4)(%eax), %edx
Packit 6c4009
	/* Get the stack pointer.  */
Packit 6c4009
	movl (JB_SP*4)(%eax), %ecx
Packit 6c4009
	PTR_DEMANGLE (%edx)
Packit 6c4009
	PTR_DEMANGLE (%ecx)
Packit 6c4009
	LIBC_PROBE (longjmp, 3, 4@%eax, -4@8(%esp), 4@%edx)
Packit 6c4009
	cfi_def_cfa(%eax, 0)
Packit 6c4009
	cfi_register(%eip, %edx)
Packit 6c4009
	cfi_register(%esp, %ecx)
Packit 6c4009
	cfi_offset(%ebx, JB_BX*4)
Packit 6c4009
	cfi_offset(%esi, JB_SI*4)
Packit 6c4009
	cfi_offset(%edi, JB_DI*4)
Packit 6c4009
	cfi_offset(%ebp, JB_BP*4)
Packit 6c4009
	/* Restore registers.  */
Packit 6c4009
	movl (JB_BX*4)(%eax), %ebx
Packit 6c4009
	movl (JB_SI*4)(%eax), %esi
Packit 6c4009
	movl (JB_DI*4)(%eax), %edi
Packit 6c4009
	movl (JB_BP*4)(%eax), %ebp
Packit 6c4009
	cfi_restore(%ebx)
Packit 6c4009
	cfi_restore(%esi)
Packit 6c4009
	cfi_restore(%edi)
Packit 6c4009
	cfi_restore(%ebp)
Packit 6c4009
Packit 6c4009
	LIBC_PROBE (longjmp_target, 3, 4@%eax, -4@8(%esp), 4@%edx)
Packit 6c4009
	movl 8(%esp), %eax	/* Second argument is return value.  */
Packit 6c4009
	movl %ecx, %esp
Packit 6c4009
#else
Packit 6c4009
	movl 4(%esp), %ecx	/* User's jmp_buf in %ecx.  */
Packit 6c4009
	movl 8(%esp), %eax	/* Second argument is return value.  */
Packit 6c4009
# ifdef SHADOW_STACK_POINTER_OFFSET
Packit 6c4009
#  if IS_IN (libc) && defined SHARED
Packit 6c4009
	/* Check if Shadow Stack is enabled.  */
Packit 6c4009
	testl $X86_FEATURE_1_SHSTK, %gs:FEATURE_1_OFFSET
Packit 6c4009
	jz L(skip_ssp)
Packit 6c4009
#  endif
Packit 6c4009
	/* Check and adjust the Shadow-Stack-Pointer.  */
Packit 6c4009
	xorl %edx, %edx
Packit 6c4009
	/* Get the current ssp.  */
Packit 6c4009
	rdsspd	%edx
Packit 6c4009
	/* And compare it with the saved ssp value.  */
Packit 6c4009
	subl SHADOW_STACK_POINTER_OFFSET(%ecx), %edx
Packit 6c4009
	je L(skip_ssp)
Packit 6c4009
	/* Count the number of frames to adjust and adjust it
Packit 6c4009
	   with incssp instruction.  The instruction can adjust
Packit 6c4009
	   the ssp by [0..255] value only thus use a loop if
Packit 6c4009
	   the number of frames is bigger than 255.  */
Packit 6c4009
	negl %edx
Packit 6c4009
	shrl $2, %edx
Packit 6c4009
	/* NB: We saved Shadow-Stack-Pointer of setjmp.  Since we are
Packit 6c4009
	       restoring Shadow-Stack-Pointer of setjmp's caller, we
Packit 6c4009
	       need to unwind shadow stack by one more frame.  */
Packit 6c4009
	addl $1, %edx
Packit 6c4009
	movl	$255, %ebx
Packit 6c4009
L(loop):
Packit 6c4009
	cmpl	%ebx, %edx
Packit 6c4009
	cmovb	%edx, %ebx
Packit 6c4009
	incsspd	%ebx
Packit 6c4009
	subl	%ebx, %edx
Packit 6c4009
	ja	L(loop)
Packit 6c4009
L(skip_ssp):
Packit 6c4009
# endif
Packit 6c4009
	/* Save the return address now.  */
Packit 6c4009
	movl (JB_PC*4)(%ecx), %edx
Packit 6c4009
	LIBC_PROBE (longjmp, 3, 4@%ecx, -4@%eax, 4@%edx)
Packit 6c4009
	/* Restore registers.  */
Packit 6c4009
	movl (JB_BX*4)(%ecx), %ebx
Packit 6c4009
	movl (JB_SI*4)(%ecx), %esi
Packit 6c4009
	movl (JB_DI*4)(%ecx), %edi
Packit 6c4009
	movl (JB_BP*4)(%ecx), %ebp
Packit 6c4009
	movl (JB_SP*4)(%ecx), %esp
Packit 6c4009
	LIBC_PROBE (longjmp_target, 3, 4@%ecx, -4@%ecx, 4@%edx)
Packit 6c4009
#endif
Packit 6c4009
	/* Jump to saved PC.  */
Packit 6c4009
	jmp *%edx
Packit 6c4009
END (__longjmp)