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