1 /* Save current context and install the given one.
2 Copyright (C) 2001-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 <asm/prctl.h>
22 #include "ucontext_i.h"
26 /* Load address of the context data structure we save in. */
29 /* Return value of swapcontext. EAX is the only register whose
30 value is not preserved. */
33 /* Save the 32-bit register values and the return address. */
45 /* Save the FS segment register. */
50 /* We have separate floating-point register content memory on the
51 stack. We use the __fpregs_mem block in the context. Set the
52 links up correctly. */
53 leal oFPREGSMEM(%eax), %ecx
54 movl %ecx, oFPREGS(%eax)
55 /* Save the floating-point context. */
58 /* Load address of the context data structure we have to load. */
61 /* Save the current signal mask and install the new one. */
63 leal oSIGMASK(%eax), %edx
64 leal oSIGMASK(%ecx), %ecx
65 movl $SIG_SETMASK, %ebx
66 movl $__NR_sigprocmask, %eax
69 cmpl $-4095, %eax /* Check %eax for error. */
70 jae SYSCALL_ERROR_LABEL /* Jump to error handler if error. */
72 /* EAX was modified, reload it. */
75 /* Restore the floating-point context. Not the registers, only the
77 movl oFPREGS(%eax), %ecx
80 /* Restore the FS segment register. We don't touch the GS register
81 since it is used for threads. */
86 /* Check if Shadow Stack is enabled. */
87 testl $X86_FEATURE_1_SHSTK, %gs:FEATURE_1_OFFSET
91 cmpl %gs:SSP_BASE_OFFSET, %eax
92 jnz L(shadow_stack_bound_recorded)
94 /* Get the base address and size of the default shadow stack
95 which must be the current shadow stack since nothing has
99 movl $ARCH_CET_STATUS, %ebx
100 movl $__NR_arch_prctl, %eax
103 jz L(continue_no_err)
105 /* This should never happen. */
109 /* Record the base of the current shadow stack. */
111 movl %eax, %gs:SSP_BASE_OFFSET
114 L(shadow_stack_bound_recorded):
115 /* Load address of the context data structure we save in. */
118 /* Load address of the context data structure we swap in */
121 /* If we unwind the stack, we can't undo stack unwinding. Just
122 save the target shadow stack pointer as the current shadow
124 movl oSSP(%edx), %ecx
125 movl %ecx, oSSP(%eax)
127 /* Save the current shadow stack base in ucontext. */
128 movl %gs:SSP_BASE_OFFSET, %ecx
129 movl %ecx, (oSSP + 4)(%eax)
131 /* If the base of the target shadow stack is the same as the
132 base of the current shadow stack, we unwind the shadow
133 stack. Otherwise it is a stack switch and we look for a
135 movl oSSP(%edx), %esi
138 /* Get the base of the target shadow stack. */
139 movl (oSSP + 4)(%edx), %ecx
140 cmpl %gs:SSP_BASE_OFFSET, %ecx
141 je L(unwind_shadow_stack)
143 /* Align the saved original shadow stack pointer to the next
144 8 byte aligned boundary. */
147 L(find_restore_token_loop):
148 /* Look for a restore token. */
152 je L(restore_shadow_stack)
154 /* Try the next slot. */
156 jmp L(find_restore_token_loop)
158 L(restore_shadow_stack):
159 /* The target shadow stack will be restored. Save the current
160 shadow stack pointer. */
162 movl %ecx, oSSP(%eax)
164 /* Use the restore stoken to restore the target shadow stack. */
167 /* Save the restore token on the old shadow stack. NB: This
168 restore token may be checked by setcontext or swapcontext
172 /* Record the new shadow stack base that was switched to. */
173 movl (oSSP + 4)(%edx), %ebx
174 movl %ebx, %gs:SSP_BASE_OFFSET
176 L(unwind_shadow_stack):
179 je L(skip_unwind_shadow_stack)
190 L(skip_unwind_shadow_stack):
192 /* Load the new stack pointer. */
193 movl oESP(%edx), %esp
195 /* Load the values of all the preserved registers (except ESP). */
196 movl oEDI(%edx), %edi
197 movl oESI(%edx), %esi
198 movl oEBP(%edx), %ebp
199 movl oEBX(%edx), %ebx
201 /* Get the return address set with getcontext. */
202 movl oEIP(%edx), %ecx
204 /* Check if return address is valid for the case when setcontext
205 is invoked from L(exitcode) with linked context. */
208 /* Clear EAX to indicate success. NB: Don't use xorl to keep
212 /* Return to the new context if return address valid. */
217 /* Jump to the new context directly. */
223 /* Fetch the address to return to. */
224 movl oEIP(%eax), %ecx
226 /* Load the new stack pointer. */
227 movl oESP(%eax), %esp
229 /* Push the return address on the new stack so we can return there. */
232 /* Load the values of all the 32-bit registers (except ESP).
233 Since we are loading from EAX, it must be last. */
234 movl oEDI(%eax), %edi
235 movl oESI(%eax), %esi
236 movl oEBP(%eax), %ebp
237 movl oEBX(%eax), %ebx
238 movl oEDX(%eax), %edx
239 movl oECX(%eax), %ecx
240 movl oEAX(%eax), %eax
242 /* The following 'ret' will pop the address of the code and jump
245 PSEUDO_END(__swapcontext)
247 weak_alias (__swapcontext, swapcontext)