1/* longjmp for i386. 2 Copyright (C) 1995-2022 Free Software Foundation, Inc. 3 This file is part of the GNU C Library. 4 5 The GNU C Library is free software; you can redistribute it and/or 6 modify it under the terms of the GNU Lesser General Public 7 License as published by the Free Software Foundation; either 8 version 2.1 of the License, or (at your option) any later version. 9 10 The GNU C Library is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 Lesser General Public License for more details. 14 15 You should have received a copy of the GNU Lesser General Public 16 License along with the GNU C Library; if not, see 17 <https://www.gnu.org/licenses/>. */ 18 19#include <sysdep.h> 20#include <jmpbuf-offsets.h> 21#include <jmp_buf-ssp.h> 22#include <asm-syntax.h> 23#include <stap-probe.h> 24 25/* Don't restore shadow stack register if 26 1. Shadow stack isn't enabled. Or 27 2. __longjmp is defined for __longjmp_cancel. 28 */ 29#if !SHSTK_ENABLED || defined __longjmp 30# undef SHADOW_STACK_POINTER_OFFSET 31#endif 32 33 .text 34ENTRY (__longjmp) 35#ifdef PTR_DEMANGLE 36 movl 4(%esp), %eax /* User's jmp_buf in %eax. */ 37 38# ifdef SHADOW_STACK_POINTER_OFFSET 39# if IS_IN (libc) && defined SHARED && defined FEATURE_1_OFFSET 40 /* Check if Shadow Stack is enabled. */ 41 testl $X86_FEATURE_1_SHSTK, %gs:FEATURE_1_OFFSET 42 jz L(skip_ssp) 43# else 44 xorl %edx, %edx 45# endif 46 /* Check and adjust the Shadow-Stack-Pointer. */ 47 rdsspd %edx 48 /* And compare it with the saved ssp value. */ 49 subl SHADOW_STACK_POINTER_OFFSET(%eax), %edx 50 je L(skip_ssp) 51 /* Count the number of frames to adjust and adjust it 52 with incssp instruction. The instruction can adjust 53 the ssp by [0..255] value only thus use a loop if 54 the number of frames is bigger than 255. */ 55 negl %edx 56 shrl $2, %edx 57 /* NB: We saved Shadow-Stack-Pointer of setjmp. Since we are 58 restoring Shadow-Stack-Pointer of setjmp's caller, we 59 need to unwind shadow stack by one more frame. */ 60 addl $1, %edx 61 movl $255, %ebx 62L(loop): 63 cmpl %ebx, %edx 64 cmovb %edx, %ebx 65 incsspd %ebx 66 subl %ebx, %edx 67 ja L(loop) 68L(skip_ssp): 69# endif 70 /* Save the return address now. */ 71 movl (JB_PC*4)(%eax), %edx 72 /* Get the stack pointer. */ 73 movl (JB_SP*4)(%eax), %ecx 74 PTR_DEMANGLE (%edx) 75 PTR_DEMANGLE (%ecx) 76 LIBC_PROBE (longjmp, 3, 4@%eax, -4@8(%esp), 4@%edx) 77 cfi_def_cfa(%eax, 0) 78 cfi_register(%eip, %edx) 79 cfi_register(%esp, %ecx) 80 cfi_offset(%ebx, JB_BX*4) 81 cfi_offset(%esi, JB_SI*4) 82 cfi_offset(%edi, JB_DI*4) 83 cfi_offset(%ebp, JB_BP*4) 84 /* Restore registers. */ 85 movl (JB_BX*4)(%eax), %ebx 86 movl (JB_SI*4)(%eax), %esi 87 movl (JB_DI*4)(%eax), %edi 88 movl (JB_BP*4)(%eax), %ebp 89 cfi_restore(%ebx) 90 cfi_restore(%esi) 91 cfi_restore(%edi) 92 cfi_restore(%ebp) 93 94 LIBC_PROBE (longjmp_target, 3, 4@%eax, -4@8(%esp), 4@%edx) 95 movl 8(%esp), %eax /* Second argument is return value. */ 96 movl %ecx, %esp 97#else 98 movl 4(%esp), %ecx /* User's jmp_buf in %ecx. */ 99 movl 8(%esp), %eax /* Second argument is return value. */ 100# ifdef SHADOW_STACK_POINTER_OFFSET 101# if IS_IN (libc) && defined SHARED 102 /* Check if Shadow Stack is enabled. */ 103 testl $X86_FEATURE_1_SHSTK, %gs:FEATURE_1_OFFSET 104 jz L(skip_ssp) 105# endif 106 /* Check and adjust the Shadow-Stack-Pointer. */ 107 xorl %edx, %edx 108 /* Get the current ssp. */ 109 rdsspd %edx 110 /* And compare it with the saved ssp value. */ 111 subl SHADOW_STACK_POINTER_OFFSET(%ecx), %edx 112 je L(skip_ssp) 113 /* Count the number of frames to adjust and adjust it 114 with incssp instruction. The instruction can adjust 115 the ssp by [0..255] value only thus use a loop if 116 the number of frames is bigger than 255. */ 117 negl %edx 118 shrl $2, %edx 119 /* NB: We saved Shadow-Stack-Pointer of setjmp. Since we are 120 restoring Shadow-Stack-Pointer of setjmp's caller, we 121 need to unwind shadow stack by one more frame. */ 122 addl $1, %edx 123 movl $255, %ebx 124L(loop): 125 cmpl %ebx, %edx 126 cmovb %edx, %ebx 127 incsspd %ebx 128 subl %ebx, %edx 129 ja L(loop) 130L(skip_ssp): 131# endif 132 /* Save the return address now. */ 133 movl (JB_PC*4)(%ecx), %edx 134 LIBC_PROBE (longjmp, 3, 4@%ecx, -4@%eax, 4@%edx) 135 /* Restore registers. */ 136 movl (JB_BX*4)(%ecx), %ebx 137 movl (JB_SI*4)(%ecx), %esi 138 movl (JB_DI*4)(%ecx), %edi 139 movl (JB_BP*4)(%ecx), %ebp 140 movl (JB_SP*4)(%ecx), %esp 141 LIBC_PROBE (longjmp_target, 3, 4@%ecx, -4@%ecx, 4@%edx) 142#endif 143 /* Jump to saved PC. */ 144 jmp *%edx 145END (__longjmp) 146