1 /* Save current context and install the given one.
2 Copyright (C) 2001-2021 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@redhat.com>, 2001.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <https://www.gnu.org/licenses/>. */
21 #include <asm/prctl.h>
23 #include "ucontext_i.h"
27 /* Load address of the context data structure we save in. */
30 /* Save the preserved register values and the return address. */
40 /* Save the FS segment register. */
45 /* We have separate floating-point register content memory on the
46 stack. We use the __fpregs_mem block in the context. Set the
47 links up correctly. */
48 leal oFPREGSMEM(%eax), %ecx
49 movl %ecx, oFPREGS(%eax)
50 /* Save the floating-point context. */
53 /* Load address of the context data structure we have to load. */
56 /* Save the current signal mask and install the new one. */
58 leal oSIGMASK(%eax), %edx
59 leal oSIGMASK(%ecx), %ecx
60 movl $SIG_SETMASK, %ebx
61 movl $__NR_sigprocmask, %eax
64 cmpl $-4095, %eax /* Check %eax for error. */
65 jae SYSCALL_ERROR_LABEL /* Jump to error handler if error. */
67 /* EAX was modified, reload it. */
70 /* Restore the floating-point context. Not the registers, only the
72 movl oFPREGS(%eax), %ecx
75 /* Restore the FS segment register. We don't touch the GS register
76 since it is used for threads. */
81 /* Check if Shadow Stack is enabled. */
82 testl $X86_FEATURE_1_SHSTK, %gs:FEATURE_1_OFFSET
86 cmpl %gs:SSP_BASE_OFFSET, %eax
87 jnz L(shadow_stack_bound_recorded)
89 /* Get the base address and size of the default shadow stack
90 which must be the current shadow stack since nothing has
94 movl $ARCH_CET_STATUS, %ebx
95 movl $__NR_arch_prctl, %eax
100 /* This should never happen. */
104 /* Record the base of the current shadow stack. */
106 movl %eax, %gs:SSP_BASE_OFFSET
109 L(shadow_stack_bound_recorded):
110 /* Load address of the context data structure we save in. */
113 /* Load address of the context data structure we swap in */
116 /* If we unwind the stack, we can't undo stack unwinding. Just
117 save the target shadow stack pointer as the current shadow
119 movl oSSP(%edx), %ecx
120 movl %ecx, oSSP(%eax)
122 /* Save the current shadow stack base in ucontext. */
123 movl %gs:SSP_BASE_OFFSET, %ecx
124 movl %ecx, (oSSP + 4)(%eax)
126 /* If the base of the target shadow stack is the same as the
127 base of the current shadow stack, we unwind the shadow
128 stack. Otherwise it is a stack switch and we look for a
130 movl oSSP(%edx), %esi
133 /* Get the base of the target shadow stack. */
134 movl (oSSP + 4)(%edx), %ecx
135 cmpl %gs:SSP_BASE_OFFSET, %ecx
136 je L(unwind_shadow_stack)
138 /* Align the saved original shadow stack pointer to the next
139 8 byte aligned boundary. */
142 L(find_restore_token_loop):
143 /* Look for a restore token. */
147 je L(restore_shadow_stack)
149 /* Try the next slot. */
151 jmp L(find_restore_token_loop)
153 L(restore_shadow_stack):
154 /* The target shadow stack will be restored. Save the current
155 shadow stack pointer. */
157 movl %ecx, oSSP(%eax)
159 /* Use the restore stoken to restore the target shadow stack. */
162 /* Save the restore token on the old shadow stack. NB: This
163 restore token may be checked by setcontext or swapcontext
167 /* Record the new shadow stack base that was switched to. */
168 movl (oSSP + 4)(%edx), %ebx
169 movl %ebx, %gs:SSP_BASE_OFFSET
171 L(unwind_shadow_stack):
174 je L(skip_unwind_shadow_stack)
185 L(skip_unwind_shadow_stack):
187 /* Load the new stack pointer. */
188 movl oESP(%edx), %esp
190 /* Load the values of all the preserved registers (except ESP). */
191 movl oEDI(%edx), %edi
192 movl oESI(%edx), %esi
193 movl oEBP(%edx), %ebp
194 movl oEBX(%edx), %ebx
196 /* Get the return address set with getcontext. */
197 movl oEIP(%edx), %ecx
199 /* Check if return address is valid for the case when setcontext
200 is invoked from L(exitcode) with linked context. */
203 /* Clear EAX to indicate success. NB: Don't use xorl to keep
207 /* Return to the new context if return address valid. */
212 /* Jump to the new context directly. */
218 /* Fetch the address to return to. */
219 movl oEIP(%eax), %ecx
221 /* Load the new stack pointer. */
222 movl oESP(%eax), %esp
224 /* Push the return address on the new stack so we can return there. */
227 /* Load the values of all the preserved registers (except ESP). */
228 movl oEDI(%eax), %edi
229 movl oESI(%eax), %esi
230 movl oEBP(%eax), %ebp
231 movl oEBX(%eax), %ebx
233 /* All done, return 0 for success. */
236 /* The following 'ret' will pop the address of the code and jump
239 PSEUDO_END(__swapcontext)
241 weak_alias (__swapcontext, swapcontext)