debug: Fix clang open fortify wrapper (BZ 31927)
[glibc.git] / sysdeps / unix / sysv / linux / i386 / swapcontext.S
blobbb736ae7d218ab759d33fceb3bbafd043f05bdad
1 /* Save current context and install the given one.
2    Copyright (C) 2001-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/>.  */
19 #include <sysdep.h>
21 #include "ucontext_i.h"
24 ENTRY(__swapcontext)
25         /* Load address of the context data structure we save in.  */
26         movl    4(%esp), %eax
28         /* Save the preserved register values and the return address.  */
29         movl    %edi, oEDI(%eax)
30         movl    %esi, oESI(%eax)
31         movl    %ebp, oEBP(%eax)
32         movl    (%esp), %ecx
33         movl    %ecx, oEIP(%eax)
34         leal    4(%esp), %ecx
35         movl    %ecx, oESP(%eax)
36         movl    %ebx, oEBX(%eax)
38         /* Save the FS segment register.  */
39         xorl    %edx, %edx
40         movw    %fs, %dx
41         movl    %edx, oFS(%eax)
43         /* We have separate floating-point register content memory on the
44            stack.  We use the __fpregs_mem block in the context.  Set the
45            links up correctly.  */
46         leal    oFPREGSMEM(%eax), %ecx
47         movl    %ecx, oFPREGS(%eax)
48         /* Save the floating-point context.  */
49         fnstenv (%ecx)
51         /* Load address of the context data structure we have to load.  */
52         movl    8(%esp), %ecx
54         /* Save the current signal mask and install the new one.  */
55         pushl   %ebx
56         leal    oSIGMASK(%eax), %edx
57         leal    oSIGMASK(%ecx), %ecx
58         movl    $SIG_SETMASK, %ebx
59         movl    $__NR_sigprocmask, %eax
60         ENTER_KERNEL
61         popl    %ebx
62         cmpl    $-4095, %eax            /* Check %eax for error.  */
63         jae     SYSCALL_ERROR_LABEL     /* Jump to error handler if error.  */
65         /* EAX was modified, reload it.  */
66         movl    8(%esp), %eax
68         /* Restore the floating-point context.  Not the registers, only the
69            rest.  */
70         movl    oFPREGS(%eax), %ecx
71         fldenv  (%ecx)
73         /* Restore the FS segment register.  We don't touch the GS register
74            since it is used for threads.  */
75         movl    oFS(%eax), %edx
76         movw    %dx, %fs
78         /* Fetch the address to return to.  */
79         movl    oEIP(%eax), %ecx
81         /* Load the new stack pointer.  */
82         movl    oESP(%eax), %esp
84         /* Push the return address on the new stack so we can return there.  */
85         pushl   %ecx
87         /* Load the values of all the preserved registers (except ESP).  */
88         movl    oEDI(%eax), %edi
89         movl    oESI(%eax), %esi
90         movl    oEBP(%eax), %ebp
91         movl    oEBX(%eax), %ebx
93         /* All done, return 0 for success.  */
94         xorl    %eax, %eax
96         /* The following 'ret' will pop the address of the code and jump
97            to it.  */
98         ret
99 PSEUDO_END(__swapcontext)
101 weak_alias (__swapcontext, swapcontext)