2.9
[glibc/nacl-glibc.git] / sysdeps / unix / sysv / linux / x86_64 / swapcontext.S
blobfc7996ccdc6f7fea28aad9b24ee3e429e0e12c25
1 /* Save current context and install the given one.
2    Copyright (C) 2002, 2005 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, write to the Free
18    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19    02111-1307 USA.  */
21 #include <sysdep.h>
23 #include "ucontext_i.h"
26 /* int __swapcontext (ucontext_t *oucp, const ucontext_t *ucp);
28   Saves the machine context in oucp such that when it is activated,
29   it appears as if __swapcontextt() returned again, restores the
30   machine context in ucp and thereby resumes execution in that
31   context.
33   This implementation is intended to be used for *synchronous* context
34   switches only.  Therefore, it does not have to save anything
35   other than the PRESERVED state.  */
37 ENTRY(__swapcontext)
38         /* Save the preserved registers, the registers used for passing args,
39            and the return address.  */
40         movq    %rbx, oRBX(%rdi)
41         movq    %rbp, oRBP(%rdi)
42         movq    %r12, oR12(%rdi)
43         movq    %r13, oR13(%rdi)
44         movq    %r14, oR14(%rdi)
45         movq    %r15, oR15(%rdi)
47         movq    %rdi, oRDI(%rdi)
48         movq    %rsi, oRSI(%rdi)
49         movq    %rdx, oRDX(%rdi)
50         movq    %rcx, oRCX(%rdi)
51         movq    %r8, oR8(%rdi)
52         movq    %r9, oR9(%rdi)
54         movq    (%rsp), %rcx
55         movq    %rcx, oRIP(%rdi)
56         leaq    8(%rsp), %rcx           /* Exclude the return address.  */
57         movq    %rcx, oRSP(%rdi)
59         /* We have separate floating-point register content memory on the
60            stack.  We use the __fpregs_mem block in the context.  Set the
61            links up correctly.  */
62         leaq    oFPREGSMEM(%rdi), %rcx
63         movq    %rcx, oFPREGS(%rdi)
64         /* Save the floating-point environment.  */
65         fnstenv (%rcx)
66         stmxcsr oMXCSR(%rdi)
69         /* The syscall destroys some registers, save them.  */
70         movq    %rsi, %r12
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.  */
83         /* Restore destroyed registers.  */
84         movq    %r12, %rsi
86         /* Restore the floating-point context.  Not the registers, only the
87            rest.  */
88         movq    oFPREGS(%rsi), %rcx
89         fldenv  (%rcx)
90         ldmxcsr oMXCSR(%rsi)
92         /* Load the new stack pointer and the preserved registers.  */
93         movq    oRSP(%rsi), %rsp
94         movq    oRBX(%rsi), %rbx
95         movq    oRBP(%rsi), %rbp
96         movq    oR12(%rsi), %r12
97         movq    oR13(%rsi), %r13
98         movq    oR14(%rsi), %r14
99         movq    oR15(%rsi), %r15
101         /* The following ret should return to the address set with
102         getcontext.  Therefore push the address on the stack.  */
103         movq    oRIP(%rsi), %rcx
104         pushq   %rcx
106         /* Setup registers used for passing args.  */
107         movq    oRDI(%rsi), %rdi
108         movq    oRDX(%rsi), %rdx
109         movq    oRCX(%rsi), %rcx
110         movq    oR8(%rsi), %r8
111         movq    oR9(%rsi), %r9
113         /* Setup finally  %rsi.  */
114         movq    oRSI(%rsi), %rsi
116         /* Clear rax to indicate success.  */
117         xorl    %eax, %eax
119 L(pseudo_end):
120         ret
121 PSEUDO_END(__swapcontext)
123 weak_alias (__swapcontext, swapcontext)