1 | /* Save current context and install the given one. |
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 __swapcontext (ucontext_t *oucp, const ucontext_t *ucp); |
26 | |
27 | Saves the machine context in oucp such that when it is activated, |
28 | it appears as if __swapcontextt() returned again, restores the |
29 | machine context in ucp and thereby resumes execution in that |
30 | context. |
31 | |
32 | This implementation is intended to be used for *synchronous* context |
33 | switches only. Therefore, it does not have to save anything |
34 | other than the PRESERVED state. */ |
35 | |
36 | ENTRY(__swapcontext) |
37 | /* Save the preserved registers, the registers used for passing args, |
38 | and the return address. */ |
39 | movq %rbx, oRBX(%rdi) |
40 | movq %rbp, oRBP(%rdi) |
41 | movq %r12, oR12(%rdi) |
42 | movq %r13, oR13(%rdi) |
43 | movq %r14, oR14(%rdi) |
44 | movq %r15, oR15(%rdi) |
45 | |
46 | movq %rdi, oRDI(%rdi) |
47 | movq %rsi, oRSI(%rdi) |
48 | movq %rdx, oRDX(%rdi) |
49 | movq %rcx, oRCX(%rdi) |
50 | movq %r8, oR8(%rdi) |
51 | movq %r9, oR9(%rdi) |
52 | |
53 | movq (%rsp), %rcx |
54 | movq %rcx, oRIP(%rdi) |
55 | leaq 8(%rsp), %rcx /* Exclude the return address. */ |
56 | movq %rcx, oRSP(%rdi) |
57 | |
58 | /* We have separate floating-point register content memory on the |
59 | stack. We use the __fpregs_mem block in the context. Set the |
60 | links up correctly. */ |
61 | leaq oFPREGSMEM(%rdi), %rcx |
62 | movq %rcx, oFPREGS(%rdi) |
63 | /* Save the floating-point environment. */ |
64 | fnstenv (%rcx) |
65 | stmxcsr oMXCSR(%rdi) |
66 | |
67 | |
68 | /* The syscall destroys some registers, save them. */ |
69 | movq %rsi, %r12 |
70 | |
71 | /* Save the current signal mask and install the new one with |
72 | rt_sigprocmask (SIG_BLOCK, newset, oldset,_NSIG/8). */ |
73 | leaq oSIGMASK(%rdi), %rdx |
74 | leaq oSIGMASK(%rsi), %rsi |
75 | movl $SIG_SETMASK, %edi |
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 | /* Restore destroyed registers. */ |
83 | movq %r12, %rsi |
84 | |
85 | /* Restore the floating-point context. Not the registers, only the |
86 | rest. */ |
87 | movq oFPREGS(%rsi), %rcx |
88 | fldenv (%rcx) |
89 | ldmxcsr oMXCSR(%rsi) |
90 | |
91 | /* Load the new stack pointer and the preserved registers. */ |
92 | movq oRSP(%rsi), %rsp |
93 | movq oRBX(%rsi), %rbx |
94 | movq oRBP(%rsi), %rbp |
95 | movq oR12(%rsi), %r12 |
96 | movq oR13(%rsi), %r13 |
97 | movq oR14(%rsi), %r14 |
98 | movq oR15(%rsi), %r15 |
99 | |
100 | /* The following ret should return to the address set with |
101 | getcontext. Therefore push the address on the stack. */ |
102 | movq oRIP(%rsi), %rcx |
103 | pushq %rcx |
104 | |
105 | /* Setup registers used for passing args. */ |
106 | movq oRDI(%rsi), %rdi |
107 | movq oRDX(%rsi), %rdx |
108 | movq oRCX(%rsi), %rcx |
109 | movq oR8(%rsi), %r8 |
110 | movq oR9(%rsi), %r9 |
111 | |
112 | /* Setup finally %rsi. */ |
113 | movq oRSI(%rsi), %rsi |
114 | |
115 | /* Clear rax to indicate success. */ |
116 | xorl %eax, %eax |
117 | ret |
118 | PSEUDO_END(__swapcontext) |
119 | |
120 | weak_alias (__swapcontext, swapcontext) |
121 | |