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.
4
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.
9
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.
14
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/>. */
18
19#include <sysdep.h>
20#include <asm/prctl.h>
21
22#include "ucontext_i.h"
23
24
25/* int __swapcontext (ucontext_t *oucp, const ucontext_t *ucp);
26
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
30 context.
31
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. */
35
36ENTRY(__swapcontext)
37 /* Save the preserved registers, the registers used for passing args,
38 and the return address. */
39 movq %rbx, oRBX(%rdi)
40 movq %rbp, oRBP(%rdi)
41 movq %r12, oR12(%rdi)
42 movq %r13, oR13(%rdi)
43 movq %r14, oR14(%rdi)
44 movq %r15, oR15(%rdi)
45
46 movq %rdi, oRDI(%rdi)
47 movq %rsi, oRSI(%rdi)
48 movq %rdx, oRDX(%rdi)
49 movq %rcx, oRCX(%rdi)
50 movq %r8, oR8(%rdi)
51 movq %r9, oR9(%rdi)
52
53 movq (%rsp), %rcx
54 movq %rcx, oRIP(%rdi)
55 leaq 8(%rsp), %rcx /* Exclude the return address. */
56 movq %rcx, oRSP(%rdi)
57
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. */
64 fnstenv (%rcx)
65 stmxcsr oMXCSR(%rdi)
66
67
68 /* The syscall destroys some registers, save them. */
69 movq %rsi, %r12
70 movq %rdi, %r9
71
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
77 movl $_NSIG8,%r10d
78 movl $__NR_rt_sigprocmask, %eax
79 syscall
80 cmpq $-4095, %rax /* Check %rax for error. */
81 jae SYSCALL_ERROR_LABEL /* Jump to error handler if error. */
82
83 /* Restore destroyed register into RDX. The choice is arbitrary,
84 but leaving RDI and RSI available for use later can avoid
85 shuffling values. */
86 movq %r12, %rdx
87
88 /* Restore the floating-point context. Not the registers, only the
89 rest. */
90 movq oFPREGS(%rdx), %rcx
91 fldenv (%rcx)
92 ldmxcsr oMXCSR(%rdx)
93
94 /* Load the new stack pointer and the preserved registers. */
95 movq oRSP(%rdx), %rsp
96 movq oRBX(%rdx), %rbx
97 movq oRBP(%rdx), %rbp
98 movq oR12(%rdx), %r12
99 movq oR13(%rdx), %r13
100 movq oR14(%rdx), %r14
101 movq oR15(%rdx), %r15
102
103#if SHSTK_ENABLED
104 /* Check if shadow stack is enabled. */
105 testl $X86_FEATURE_1_SHSTK, %fs:FEATURE_1_OFFSET
106 jz L(no_shstk)
107
108 xorl %eax, %eax
109 cmpq %fs:SSP_BASE_OFFSET, %rax
110 jnz L(shadow_stack_bound_recorded)
111
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. */
115 rdsspq %rax
116 movq %rax, %fs:SSP_BASE_OFFSET
117
118L(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
121 stack pointer. */
122 movq oSSP(%rdx), %rcx
123 movq %rcx, oSSP(%r9)
124
125 /* Save the base of the current shadow stack. */
126 movq %fs:SSP_BASE_OFFSET, %rax
127 movq %rax, (oSSP + 8)(%r9)
128
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
132 restore token. */
133 movq oSSP(%rdx), %rsi
134 movq %rsi, %rdi
135
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)
140
141L(find_restore_token_loop):
142 /* Look for a restore token. */
143 movq -8(%rsi), %rax
144 andq $-8, %rax
145 cmpq %rsi, %rax
146 je L(restore_shadow_stack)
147
148 /* Try the next slot. */
149 subq $8, %rsi
150 jmp L(find_restore_token_loop)
151
152L(restore_shadow_stack):
153 /* The target shadow stack will be restored. Save the current
154 shadow stack pointer. */
155 rdsspq %rcx
156 movq %rcx, oSSP(%r9)
157
158 /* Restore the target shadow stack. */
159 rstorssp -8(%rsi)
160
161 /* Save the restore token on the old shadow stack. NB: This
162 restore token may be checked by setcontext or swapcontext
163 later. */
164 saveprevssp
165
166 /* Record the new shadow stack base that was switched to. */
167 movq (oSSP + 8)(%rdx), %rax
168 movq %rax, %fs:SSP_BASE_OFFSET
169
170L(unwind_shadow_stack):
171 rdsspq %rcx
172 subq %rdi, %rcx
173 je L(skip_unwind_shadow_stack)
174 negq %rcx
175 shrq $3, %rcx
176 movl $255, %esi
177L(loop):
178 cmpq %rsi, %rcx
179 cmovb %rcx, %rsi
180 incsspq %rsi
181 subq %rsi, %rcx
182 ja L(loop)
183
184L(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
189 movq oR8(%rdx), %r8
190 movq oR9(%rdx), %r9
191
192 /* Get the return address set with getcontext. */
193 movq oRIP(%rdx), %r10
194
195 /* Setup finally %rdx. */
196 movq oRDX(%rdx), %rdx
197
198 /* Check if return address is valid for the case when setcontext
199 is invoked from __start_context with linked context. */
200 rdsspq %rax
201 cmpq (%rax), %r10
202 /* Clear rax to indicate success. NB: Don't use xorl to keep
203 EFLAGS for jne. */
204 movl $0, %eax
205 jne L(jmp)
206 /* Return to the new context if return address valid. */
207 pushq %r10
208 ret
209
210L(jmp):
211 /* Jump to the new context directly. */
212 jmp *%r10
213
214L(no_shstk):
215#endif
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
219 pushq %rcx
220
221 /* Setup registers used for passing args. */
222 movq oRDI(%rdx), %rdi
223 movq oRSI(%rdx), %rsi
224 movq oRCX(%rdx), %rcx
225 movq oR8(%rdx), %r8
226 movq oR9(%rdx), %r9
227
228 /* Setup finally %rdx. */
229 movq oRDX(%rdx), %rdx
230
231 /* Clear rax to indicate success. */
232 xorl %eax, %eax
233 ret
234PSEUDO_END(__swapcontext)
235
236weak_alias (__swapcontext, swapcontext)
237

source code of glibc/sysdeps/unix/sysv/linux/x86_64/swapcontext.S