]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/unix/sysv/linux/i386/swapcontext.S
Update copyright dates with scripts/update-copyrights
[thirdparty/glibc.git] / sysdeps / unix / sysv / linux / i386 / swapcontext.S
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.
5
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.
10
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.
15
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/>. */
19
20 #include <sysdep.h>
21 #include <asm/prctl.h>
22
23 #include "ucontext_i.h"
24
25
26 ENTRY(__swapcontext)
27 /* Load address of the context data structure we save in. */
28 movl 4(%esp), %eax
29
30 /* Save the preserved register values and the return address. */
31 movl %edi, oEDI(%eax)
32 movl %esi, oESI(%eax)
33 movl %ebp, oEBP(%eax)
34 movl (%esp), %ecx
35 movl %ecx, oEIP(%eax)
36 leal 4(%esp), %ecx
37 movl %ecx, oESP(%eax)
38 movl %ebx, oEBX(%eax)
39
40 /* Save the FS segment register. */
41 xorl %edx, %edx
42 movw %fs, %dx
43 movl %edx, oFS(%eax)
44
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. */
51 fnstenv (%ecx)
52
53 /* Load address of the context data structure we have to load. */
54 movl 8(%esp), %ecx
55
56 /* Save the current signal mask and install the new one. */
57 pushl %ebx
58 leal oSIGMASK(%eax), %edx
59 leal oSIGMASK(%ecx), %ecx
60 movl $SIG_SETMASK, %ebx
61 movl $__NR_sigprocmask, %eax
62 ENTER_KERNEL
63 popl %ebx
64 cmpl $-4095, %eax /* Check %eax for error. */
65 jae SYSCALL_ERROR_LABEL /* Jump to error handler if error. */
66
67 /* EAX was modified, reload it. */
68 movl 8(%esp), %eax
69
70 /* Restore the floating-point context. Not the registers, only the
71 rest. */
72 movl oFPREGS(%eax), %ecx
73 fldenv (%ecx)
74
75 /* Restore the FS segment register. We don't touch the GS register
76 since it is used for threads. */
77 movl oFS(%eax), %edx
78 movw %dx, %fs
79
80 #if SHSTK_ENABLED
81 /* Check if Shadow Stack is enabled. */
82 testl $X86_FEATURE_1_SHSTK, %gs:FEATURE_1_OFFSET
83 jz L(no_shstk)
84
85 xorl %eax, %eax
86 cmpl %gs:SSP_BASE_OFFSET, %eax
87 jnz L(shadow_stack_bound_recorded)
88
89 /* Get the base address and size of the default shadow stack
90 which must be the current shadow stack since nothing has
91 been recorded yet. */
92 sub $24, %esp
93 mov %esp, %ecx
94 movl $ARCH_CET_STATUS, %ebx
95 movl $__NR_arch_prctl, %eax
96 ENTER_KERNEL
97 testl %eax, %eax
98 jz L(continue_no_err)
99
100 /* This should never happen. */
101 hlt
102
103 L(continue_no_err):
104 /* Record the base of the current shadow stack. */
105 movl 8(%esp), %eax
106 movl %eax, %gs:SSP_BASE_OFFSET
107 add $24, %esp
108
109 L(shadow_stack_bound_recorded):
110 /* Load address of the context data structure we save in. */
111 movl 4(%esp), %eax
112
113 /* Load address of the context data structure we swap in */
114 movl 8(%esp), %edx
115
116 /* If we unwind the stack, we can't undo stack unwinding. Just
117 save the target shadow stack pointer as the current shadow
118 stack pointer. */
119 movl oSSP(%edx), %ecx
120 movl %ecx, oSSP(%eax)
121
122 /* Save the current shadow stack base in ucontext. */
123 movl %gs:SSP_BASE_OFFSET, %ecx
124 movl %ecx, (oSSP + 4)(%eax)
125
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
129 restore token. */
130 movl oSSP(%edx), %esi
131 movl %esi, %edi
132
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)
137
138 /* Align the saved original shadow stack pointer to the next
139 8 byte aligned boundary. */
140 andl $-8, %esi
141
142 L(find_restore_token_loop):
143 /* Look for a restore token. */
144 movl -8(%esi), %ebx
145 andl $-8, %ebx
146 cmpl %esi, %ebx
147 je L(restore_shadow_stack)
148
149 /* Try the next slot. */
150 subl $8, %esi
151 jmp L(find_restore_token_loop)
152
153 L(restore_shadow_stack):
154 /* The target shadow stack will be restored. Save the current
155 shadow stack pointer. */
156 rdsspd %ecx
157 movl %ecx, oSSP(%eax)
158
159 /* Use the restore stoken to restore the target shadow stack. */
160 rstorssp -8(%esi)
161
162 /* Save the restore token on the old shadow stack. NB: This
163 restore token may be checked by setcontext or swapcontext
164 later. */
165 saveprevssp
166
167 /* Record the new shadow stack base that was switched to. */
168 movl (oSSP + 4)(%edx), %ebx
169 movl %ebx, %gs:SSP_BASE_OFFSET
170
171 L(unwind_shadow_stack):
172 rdsspd %ebx
173 subl %edi, %ebx
174 je L(skip_unwind_shadow_stack)
175 negl %ebx
176 shrl $2, %ebx
177 movl $255, %esi
178 L(loop):
179 cmpl %esi, %ebx
180 cmovb %ebx, %esi
181 incsspd %esi
182 subl %esi, %ebx
183 ja L(loop)
184
185 L(skip_unwind_shadow_stack):
186
187 /* Load the new stack pointer. */
188 movl oESP(%edx), %esp
189
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
195
196 /* Get the return address set with getcontext. */
197 movl oEIP(%edx), %ecx
198
199 /* Check if return address is valid for the case when setcontext
200 is invoked from L(exitcode) with linked context. */
201 rdsspd %eax
202 cmpl (%eax), %ecx
203 /* Clear EAX to indicate success. NB: Don't use xorl to keep
204 EFLAGS for jne. */
205 movl $0, %eax
206 jne L(jmp)
207 /* Return to the new context if return address valid. */
208 pushl %ecx
209 ret
210
211 L(jmp):
212 /* Jump to the new context directly. */
213 jmp *%ecx
214
215 L(no_shstk):
216 #endif
217
218 /* Fetch the address to return to. */
219 movl oEIP(%eax), %ecx
220
221 /* Load the new stack pointer. */
222 movl oESP(%eax), %esp
223
224 /* Push the return address on the new stack so we can return there. */
225 pushl %ecx
226
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
232
233 /* All done, return 0 for success. */
234 xorl %eax, %eax
235
236 /* The following 'ret' will pop the address of the code and jump
237 to it. */
238 ret
239 PSEUDO_END(__swapcontext)
240
241 weak_alias (__swapcontext, swapcontext)