2 Copyright (C) 1995-2023 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 <pointer_guard.h>
21 #include <jmpbuf-offsets.h>
22 #include <jmp_buf-ssp.h>
23 #include <asm-syntax.h>
24 #include <stap-probe.h>
26 /* Don't restore shadow stack register if
27 1. Shadow stack isn't enabled. Or
28 2. __longjmp is defined for __longjmp_cancel.
30 #if !SHSTK_ENABLED || defined __longjmp
31 # undef SHADOW_STACK_POINTER_OFFSET
37 movl 4(%esp), %eax /* User's jmp_buf in %eax. */
39 # ifdef SHADOW_STACK_POINTER_OFFSET
40 # if IS_IN (libc) && defined SHARED && defined FEATURE_1_OFFSET
41 /* Check if Shadow Stack is enabled. */
42 testl $X86_FEATURE_1_SHSTK, %gs:FEATURE_1_OFFSET
47 /* Check and adjust the Shadow-Stack-Pointer. */
49 /* And compare it with the saved ssp value. */
50 subl SHADOW_STACK_POINTER_OFFSET(%eax), %edx
52 /* Count the number of frames to adjust and adjust it
53 with incssp instruction. The instruction can adjust
54 the ssp by [0..255] value only thus use a loop if
55 the number of frames is bigger than 255. */
58 /* NB: We saved Shadow-Stack-Pointer of setjmp. Since we are
59 restoring Shadow-Stack-Pointer of setjmp's caller, we
60 need to unwind shadow stack by one more frame. */
71 /* Save the return address now. */
72 movl (JB_PC*4)(%eax), %edx
73 /* Get the stack pointer. */
74 movl (JB_SP*4)(%eax), %ecx
77 LIBC_PROBE (longjmp, 3, 4@%eax, -4@8(%esp), 4@%edx)
79 cfi_register(%eip, %edx)
80 cfi_register(%esp, %ecx)
81 cfi_offset(%ebx, JB_BX*4)
82 cfi_offset(%esi, JB_SI*4)
83 cfi_offset(%edi, JB_DI*4)
84 cfi_offset(%ebp, JB_BP*4)
85 /* Restore registers. */
86 movl (JB_BX*4)(%eax), %ebx
87 movl (JB_SI*4)(%eax), %esi
88 movl (JB_DI*4)(%eax), %edi
89 movl (JB_BP*4)(%eax), %ebp
95 LIBC_PROBE (longjmp_target, 3, 4@%eax, -4@8(%esp), 4@%edx)
96 movl 8(%esp), %eax /* Second argument is return value. */
99 movl 4(%esp), %ecx /* User's jmp_buf in %ecx. */
100 movl 8(%esp), %eax /* Second argument is return value. */
101 # ifdef SHADOW_STACK_POINTER_OFFSET
102 # if IS_IN (libc) && defined SHARED
103 /* Check if Shadow Stack is enabled. */
104 testl $X86_FEATURE_1_SHSTK, %gs:FEATURE_1_OFFSET
107 /* Check and adjust the Shadow-Stack-Pointer. */
109 /* Get the current ssp. */
111 /* And compare it with the saved ssp value. */
112 subl SHADOW_STACK_POINTER_OFFSET(%ecx), %edx
114 /* Count the number of frames to adjust and adjust it
115 with incssp instruction. The instruction can adjust
116 the ssp by [0..255] value only thus use a loop if
117 the number of frames is bigger than 255. */
120 /* NB: We saved Shadow-Stack-Pointer of setjmp. Since we are
121 restoring Shadow-Stack-Pointer of setjmp's caller, we
122 need to unwind shadow stack by one more frame. */
133 /* Save the return address now. */
134 movl (JB_PC*4)(%ecx), %edx
135 LIBC_PROBE (longjmp, 3, 4@%ecx, -4@%eax, 4@%edx)
136 /* Restore registers. */
137 movl (JB_BX*4)(%ecx), %ebx
138 movl (JB_SI*4)(%ecx), %esi
139 movl (JB_DI*4)(%ecx), %edi
140 movl (JB_BP*4)(%ecx), %ebp
141 movl (JB_SP*4)(%ecx), %esp
142 LIBC_PROBE (longjmp_target, 3, 4@%ecx, -4@%ecx, 4@%edx)
144 /* Jump to saved PC. */