| 1 | /* Install given context. |
| 2 | Copyright (C) 2002-2016 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 | |
| 25 | /* int __setcontext (const ucontext_t *ucp) |
| 26 | |
| 27 | Restores the machine context in UCP and thereby resumes execution |
| 28 | in that context. |
| 29 | |
| 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. */ |
| 33 | |
| 34 | ENTRY(__setcontext) |
| 35 | /* Save argument since syscall will destroy it. */ |
| 36 | pushq %rdi |
| 37 | cfi_adjust_cfa_offset(8) |
| 38 | |
| 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. */ |
| 51 | |
| 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) |
| 57 | |
| 58 | |
| 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) |
| 70 | |
| 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 |
| 78 | |
| 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 |
| 83 | |
| 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 |
| 89 | |
| 90 | /* Setup finally %rdi. */ |
| 91 | movq oRDI(%rdi), %rdi |
| 92 | |
| 93 | /* End FDE here, we fall into another context. */ |
| 94 | cfi_endproc |
| 95 | cfi_startproc |
| 96 | |
| 97 | /* Clear rax to indicate success. */ |
| 98 | xorl %eax, %eax |
| 99 | ret |
| 100 | PSEUDO_END(__setcontext) |
| 101 | |
| 102 | weak_alias (__setcontext, setcontext) |
| 103 | |