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