2.9
[glibc/nacl-glibc.git] / sysdeps / unix / sysv / linux / x86_64 / setcontext.S
blob61fc07f44ff614af5ed9591247353e08289d52f8
1 /* Install given context.
2    Copyright (C) 2002, 2004, 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 __setcontext (const ucontext_t *ucp)
28   Restores the machine context in UCP and thereby resumes execution
29   in that context.
31   This implementation is intended to be used for *synchronous* context
32   switches only.  Therefore, it does not have to restore anything
33   other than the PRESERVED state.  */
35 ENTRY(__setcontext)
36         /* Save argument since syscall will destroy it.  */
37         pushq   %rdi
38         cfi_adjust_cfa_offset(8)
40         /* Set the signal mask with
41            rt_sigprocmask (SIG_SETMASK, mask, NULL, _NSIG/8).  */
42         leaq    oSIGMASK(%rdi), %rsi
43         xorl    %edx, %edx
44         movl    $SIG_SETMASK, %edi
45         movl    $_NSIG8,%r10d
46         movl    $__NR_rt_sigprocmask, %eax
47         syscall
48         popq    %rdi                    /* Reload %rdi, adjust stack.  */
49         cfi_adjust_cfa_offset(-8)
50         cmpq    $-4095, %rax            /* Check %rax for error.  */
51         jae     SYSCALL_ERROR_LABEL     /* Jump to error handler if error.  */
53         /* Restore the floating-point context.  Not the registers, only the
54            rest.  */
55         movq    oFPREGS(%rdi), %rcx
56         fldenv  (%rcx)
57         ldmxcsr oMXCSR(%rdi)
60         /* Load the new stack pointer, the preserved registers and
61            registers used for passing args.  */
62         cfi_def_cfa(%rdi, 0)
63         cfi_offset(%rbx,oRBX)
64         cfi_offset(%rbp,oRBP)
65         cfi_offset(%r12,oR12)
66         cfi_offset(%r13,oR13)
67         cfi_offset(%r14,oR14)
68         cfi_offset(%r15,oR15)
69         cfi_offset(%rsp,oRSP)
70         cfi_offset(%rip,oRIP)
72         movq    oRSP(%rdi), %rsp
73         movq    oRBX(%rdi), %rbx
74         movq    oRBP(%rdi), %rbp
75         movq    oR12(%rdi), %r12
76         movq    oR13(%rdi), %r13
77         movq    oR14(%rdi), %r14
78         movq    oR15(%rdi), %r15
80         /* The following ret should return to the address set with
81         getcontext.  Therefore push the address on the stack.  */
82         movq    oRIP(%rdi), %rcx
83         pushq   %rcx
85         movq    oRSI(%rdi), %rsi
86         movq    oRDX(%rdi), %rdx
87         movq    oRCX(%rdi), %rcx
88         movq    oR8(%rdi), %r8
89         movq    oR9(%rdi), %r9
91         /* Setup finally  %rdi.  */
92         movq    oRDI(%rdi), %rdi
94         /* End FDE here, we fall into another context.  */
95         cfi_endproc
96         cfi_startproc
98         /* Clear rax to indicate success.  */
99         xorl    %eax, %eax
101 L(pseudo_end):
102         ret
103 PSEUDO_END(__setcontext)
105 weak_alias (__setcontext, setcontext)