2 Copyright (C) 1995-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
20 #include <jmpbuf-offsets.h>
21 #include <jmp_buf-ssp.h>
22 #include <asm-syntax.h>
23 #include <stap-probe.h>
25 /* Don't restore shadow stack register if
26 1. Shadow stack isn't enabled. Or
27 2. __longjmp is defined for __longjmp_cancel.
29 #if !SHSTK_ENABLED || defined __longjmp
30 # undef SHADOW_STACK_POINTER_OFFSET
36 movl 4(%esp), %eax /* User's jmp_buf in %eax. */
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
46 /* Check and adjust the Shadow-Stack-Pointer. */
48 /* And compare it with the saved ssp value. */
49 subl SHADOW_STACK_POINTER_OFFSET(%eax), %edx
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. */
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. */
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
76 LIBC_PROBE (longjmp, 3, 4@%eax, -4@8(%esp), 4@%edx)
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
94 LIBC_PROBE (longjmp_target, 3, 4@%eax, -4@8(%esp), 4@%edx)
95 movl 8(%esp), %eax /* Second argument is return value. */
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
106 /* Check and adjust the Shadow-Stack-Pointer. */
108 /* Get the current ssp. */
110 /* And compare it with the saved ssp value. */
111 subl SHADOW_STACK_POINTER_OFFSET(%ecx), %edx
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. */
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. */
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)
143 /* Jump to saved PC. */