1 /* Save current context and install the given one.
2 Copyright (C) 2002-2024 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"
25 /* int __swapcontext (ucontext_t *oucp, const ucontext_t *ucp);
27 Saves the machine context in oucp such that when it is activated,
28 it appears as if __swapcontextt() returned again, restores the
29 machine context in ucp and thereby resumes execution in that
32 This implementation is intended to be used for *synchronous* context
33 switches only. Therefore, it does not have to save anything
34 other than the PRESERVED state. */
37 /* Save the preserved registers, the registers used for passing args,
38 and the return address. */
55 leaq 8(%rsp), %rcx /* Exclude the return address. */
58 /* We have separate floating-point register content memory on the
59 stack. We use the __fpregs_mem block in the context. Set the
60 links up correctly. */
61 leaq oFPREGSMEM(%rdi), %rcx
62 movq %rcx, oFPREGS(%rdi)
63 /* Save the floating-point environment. */
68 /* The syscall destroys some registers, save them. */
72 /* Save the current signal mask and install the new one with
73 rt_sigprocmask (SIG_BLOCK, newset, oldset,_NSIG/8). */
74 leaq oSIGMASK(%rdi), %rdx
75 leaq oSIGMASK(%rsi), %rsi
76 movl $SIG_SETMASK, %edi
78 movl $__NR_rt_sigprocmask, %eax
80 cmpq $-4095, %rax /* Check %rax for error. */
81 jae SYSCALL_ERROR_LABEL /* Jump to error handler if error. */
83 /* Restore destroyed register into RDX. The choice is arbitrary,
84 but leaving RDI and RSI available for use later can avoid
88 /* Restore the floating-point context. Not the registers, only the
90 movq oFPREGS(%rdx), %rcx
94 /* Load the new stack pointer and the preserved registers. */
100 movq oR14(%rdx), %r14
101 movq oR15(%rdx), %r15
104 /* Check if shadow stack is enabled. */
105 testl $X86_FEATURE_1_SHSTK, %fs:FEATURE_1_OFFSET
109 cmpq %fs:SSP_BASE_OFFSET, %rax
110 jnz L(shadow_stack_bound_recorded)
112 /* When the shadow stack base is unset, the default shadow
113 stack is in use. Use the current shadow stack pointer
114 as the marker for the default shadow stack. */
116 movq %rax, %fs:SSP_BASE_OFFSET
118 L(shadow_stack_bound_recorded):
119 /* If we unwind the stack, we can't undo stack unwinding. Just
120 save the target shadow stack pointer as the current shadow
122 movq oSSP(%rdx), %rcx
125 /* Save the base of the current shadow stack. */
126 movq %fs:SSP_BASE_OFFSET, %rax
127 movq %rax, (oSSP + 8)(%r9)
129 /* If the base of the target shadow stack is the same as the
130 base of the current shadow stack, we unwind the shadow
131 stack. Otherwise it is a stack switch and we look for a
133 movq oSSP(%rdx), %rsi
136 /* Get the base of the target shadow stack. */
137 movq (oSSP + 8)(%rdx), %rcx
138 cmpq %fs:SSP_BASE_OFFSET, %rcx
139 je L(unwind_shadow_stack)
141 L(find_restore_token_loop):
142 /* Look for a restore token. */
146 je L(restore_shadow_stack)
148 /* Try the next slot. */
150 jmp L(find_restore_token_loop)
152 L(restore_shadow_stack):
153 /* The target shadow stack will be restored. Save the current
154 shadow stack pointer. */
158 /* Restore the target shadow stack. */
161 /* Save the restore token on the old shadow stack. NB: This
162 restore token may be checked by setcontext or swapcontext
166 /* Record the new shadow stack base that was switched to. */
167 movq (oSSP + 8)(%rdx), %rax
168 movq %rax, %fs:SSP_BASE_OFFSET
170 L(unwind_shadow_stack):
173 je L(skip_unwind_shadow_stack)
184 L(skip_unwind_shadow_stack):
185 /* Setup registers used for passing args. */
186 movq oRDI(%rdx), %rdi
187 movq oRSI(%rdx), %rsi
188 movq oRCX(%rdx), %rcx
192 /* Get the return address set with getcontext. */
193 movq oRIP(%rdx), %r10
195 /* Setup finally %rdx. */
196 movq oRDX(%rdx), %rdx
198 /* Check if return address is valid for the case when setcontext
199 is invoked from __start_context with linked context. */
202 /* Clear rax to indicate success. NB: Don't use xorl to keep
206 /* Return to the new context if return address valid. */
211 /* Jump to the new context directly. */
216 /* The following ret should return to the address set with
217 getcontext. Therefore push the address on the stack. */
218 movq oRIP(%rdx), %rcx
221 /* Setup registers used for passing args. */
222 movq oRDI(%rdx), %rdi
223 movq oRSI(%rdx), %rsi
224 movq oRCX(%rdx), %rcx
228 /* Setup finally %rdx. */
229 movq oRDX(%rdx), %rdx
231 /* Clear rax to indicate success. */
234 PSEUDO_END(__swapcontext)
236 weak_alias (__swapcontext, swapcontext)