1 /* Save current context and install the given one.
2 Copyright (C) 2002-2018 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Andreas Jaeger <aj@suse.de>, 2002.
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.
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.
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 <http://www.gnu.org/licenses/>. */
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. */
71 /* Save the current signal mask and install the new one with
72 rt_sigprocmask (SIG_BLOCK, newset, oldset,_NSIG/8). */
73 leaq oSIGMASK(%rdi), %rdx
74 leaq oSIGMASK(%rsi), %rsi
75 movl $SIG_SETMASK, %edi
77 movl $__NR_rt_sigprocmask, %eax
79 cmpq $-4095, %rax /* Check %rax for error. */
80 jae SYSCALL_ERROR_LABEL /* Jump to error handler if error. */
82 /* Restore destroyed register into RDX. The choice is arbitrary,
83 but leaving RDI and RSI available for use later can avoid
87 /* Restore the floating-point context. Not the registers, only the
89 movq oFPREGS(%rdx), %rcx
93 /* Load the new stack pointer and the preserved registers. */
100 movq oR15(%rdx), %r15
102 /* The following ret should return to the address set with
103 getcontext. Therefore push the address on the stack. */
104 movq oRIP(%rdx), %rcx
107 /* Setup registers used for passing args. */
108 movq oRDI(%rdx), %rdi
109 movq oRSI(%rdx), %rsi
110 movq oRCX(%rdx), %rcx
114 /* Setup finally %rdx. */
115 movq oRDX(%rdx), %rdx
117 /* Clear rax to indicate success. */
120 PSEUDO_END(__swapcontext)
122 weak_alias (__swapcontext, swapcontext)