| 1 | /* Save current context. |
| 2 | Copyright (C) 2002-2017 Free Software Foundation, Inc. |
| 3 | This file is part of the GNU C Library. |
| 4 | Contributed by Andreas Jaeger <aj@suse.de>, 2002. |
| 5 | |
| 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. |
| 10 | |
| 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. |
| 15 | |
| 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/>. */ |
| 19 | |
| 20 | #include <sysdep.h> |
| 21 | |
| 22 | #include "ucontext_i.h" |
| 23 | |
| 24 | /* int __getcontext (ucontext_t *ucp) |
| 25 | |
| 26 | Saves the machine context in UCP such that when it is activated, |
| 27 | it appears as if __getcontext() returned again. |
| 28 | |
| 29 | This implementation is intended to be used for *synchronous* context |
| 30 | switches only. Therefore, it does not have to save anything |
| 31 | other than the PRESERVED state. */ |
| 32 | |
| 33 | |
| 34 | ENTRY(__getcontext) |
| 35 | /* Save the preserved registers, the registers used for passing |
| 36 | args, and the return address. */ |
| 37 | movq %rbx, oRBX(%rdi) |
| 38 | movq %rbp, oRBP(%rdi) |
| 39 | movq %r12, oR12(%rdi) |
| 40 | movq %r13, oR13(%rdi) |
| 41 | movq %r14, oR14(%rdi) |
| 42 | movq %r15, oR15(%rdi) |
| 43 | |
| 44 | movq %rdi, oRDI(%rdi) |
| 45 | movq %rsi, oRSI(%rdi) |
| 46 | movq %rdx, oRDX(%rdi) |
| 47 | movq %rcx, oRCX(%rdi) |
| 48 | movq %r8, oR8(%rdi) |
| 49 | movq %r9, oR9(%rdi) |
| 50 | |
| 51 | movq (%rsp), %rcx |
| 52 | movq %rcx, oRIP(%rdi) |
| 53 | leaq 8(%rsp), %rcx /* Exclude the return address. */ |
| 54 | movq %rcx, oRSP(%rdi) |
| 55 | |
| 56 | /* We have separate floating-point register content memory on the |
| 57 | stack. We use the __fpregs_mem block in the context. Set the |
| 58 | links up correctly. */ |
| 59 | |
| 60 | leaq oFPREGSMEM(%rdi), %rcx |
| 61 | movq %rcx, oFPREGS(%rdi) |
| 62 | /* Save the floating-point environment. */ |
| 63 | fnstenv (%rcx) |
| 64 | fldenv (%rcx) |
| 65 | stmxcsr oMXCSR(%rdi) |
| 66 | |
| 67 | /* Save the current signal mask with |
| 68 | rt_sigprocmask (SIG_BLOCK, NULL, set,_NSIG/8). */ |
| 69 | leaq oSIGMASK(%rdi), %rdx |
| 70 | xorl %esi,%esi |
| 71 | #if SIG_BLOCK == 0 |
| 72 | xorl %edi, %edi |
| 73 | #else |
| 74 | movl $SIG_BLOCK, %edi |
| 75 | #endif |
| 76 | movl $_NSIG8,%r10d |
| 77 | movl $__NR_rt_sigprocmask, %eax |
| 78 | syscall |
| 79 | cmpq $-4095, %rax /* Check %rax for error. */ |
| 80 | jae SYSCALL_ERROR_LABEL /* Jump to error handler if error. */ |
| 81 | |
| 82 | /* All done, return 0 for success. */ |
| 83 | xorl %eax, %eax |
| 84 | ret |
| 85 | PSEUDO_END(__getcontext) |
| 86 | |
| 87 | weak_alias (__getcontext, getcontext) |
| 88 | |