1 | /* Install given context. |
2 | Copyright (C) 2002-2021 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 | <https://www.gnu.org/licenses/>. */ |
19 | |
20 | #include <sysdep.h> |
21 | #include <asm/prctl.h> |
22 | |
23 | #include "ucontext_i.h" |
24 | |
25 | |
26 | /* int __setcontext (const ucontext_t *ucp) |
27 | |
28 | Restores the machine context in UCP and thereby resumes execution |
29 | in that context. |
30 | |
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. */ |
34 | |
35 | ENTRY(__setcontext) |
36 | /* Save argument since syscall will destroy it. */ |
37 | pushq %rdi |
38 | cfi_adjust_cfa_offset(8) |
39 | |
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 | /* Pop the pointer into RDX. The choice is arbitrary, but |
49 | leaving RDI and RSI available for use later can avoid |
50 | shuffling values. */ |
51 | popq %rdx |
52 | cfi_adjust_cfa_offset(-8) |
53 | cmpq $-4095, %rax /* Check %rax for error. */ |
54 | jae SYSCALL_ERROR_LABEL /* Jump to error handler if error. */ |
55 | |
56 | /* Restore the floating-point context. Not the registers, only the |
57 | rest. */ |
58 | movq oFPREGS(%rdx), %rcx |
59 | fldenv (%rcx) |
60 | ldmxcsr oMXCSR(%rdx) |
61 | |
62 | |
63 | /* Load the new stack pointer, the preserved registers and |
64 | registers used for passing args. */ |
65 | cfi_def_cfa(%rdx, 0) |
66 | cfi_offset(%rbx,oRBX) |
67 | cfi_offset(%rbp,oRBP) |
68 | cfi_offset(%r12,oR12) |
69 | cfi_offset(%r13,oR13) |
70 | cfi_offset(%r14,oR14) |
71 | cfi_offset(%r15,oR15) |
72 | cfi_offset(%rsp,oRSP) |
73 | cfi_offset(%rip,oRIP) |
74 | |
75 | movq oRSP(%rdx), %rsp |
76 | movq oRBX(%rdx), %rbx |
77 | movq oRBP(%rdx), %rbp |
78 | movq oR12(%rdx), %r12 |
79 | movq oR13(%rdx), %r13 |
80 | movq oR14(%rdx), %r14 |
81 | movq oR15(%rdx), %r15 |
82 | |
83 | #if SHSTK_ENABLED |
84 | /* Check if shadow stack is enabled. */ |
85 | testl $X86_FEATURE_1_SHSTK, %fs:FEATURE_1_OFFSET |
86 | jz L(no_shstk) |
87 | |
88 | /* If the base of the target shadow stack is the same as the |
89 | base of the current shadow stack, we unwind the shadow |
90 | stack. Otherwise it is a stack switch and we look for a |
91 | restore token. */ |
92 | movq oSSP(%rdx), %rsi |
93 | movq %rsi, %rdi |
94 | |
95 | /* Get the base of the target shadow stack. */ |
96 | movq (oSSP + 8)(%rdx), %rcx |
97 | cmpq %fs:SSP_BASE_OFFSET, %rcx |
98 | je L(unwind_shadow_stack) |
99 | |
100 | L(find_restore_token_loop): |
101 | /* Look for a restore token. */ |
102 | movq -8(%rsi), %rax |
103 | andq $-8, %rax |
104 | cmpq %rsi, %rax |
105 | je L(restore_shadow_stack) |
106 | |
107 | /* Try the next slot. */ |
108 | subq $8, %rsi |
109 | jmp L(find_restore_token_loop) |
110 | |
111 | L(restore_shadow_stack): |
112 | /* Pop return address from the shadow stack since setcontext |
113 | will not return. */ |
114 | movq $1, %rax |
115 | incsspq %rax |
116 | |
117 | /* Use the restore stoken to restore the target shadow stack. */ |
118 | rstorssp -8(%rsi) |
119 | |
120 | /* Save the restore token on the old shadow stack. NB: This |
121 | restore token may be checked by setcontext or swapcontext |
122 | later. */ |
123 | saveprevssp |
124 | |
125 | /* Record the new shadow stack base that was switched to. */ |
126 | movq (oSSP + 8)(%rdx), %rax |
127 | movq %rax, %fs:SSP_BASE_OFFSET |
128 | |
129 | L(unwind_shadow_stack): |
130 | rdsspq %rcx |
131 | subq %rdi, %rcx |
132 | je L(skip_unwind_shadow_stack) |
133 | negq %rcx |
134 | shrq $3, %rcx |
135 | movl $255, %esi |
136 | L(loop): |
137 | cmpq %rsi, %rcx |
138 | cmovb %rcx, %rsi |
139 | incsspq %rsi |
140 | subq %rsi, %rcx |
141 | ja L(loop) |
142 | |
143 | L(skip_unwind_shadow_stack): |
144 | movq oRSI(%rdx), %rsi |
145 | movq oRDI(%rdx), %rdi |
146 | movq oRCX(%rdx), %rcx |
147 | movq oR8(%rdx), %r8 |
148 | movq oR9(%rdx), %r9 |
149 | |
150 | /* Get the return address set with getcontext. */ |
151 | movq oRIP(%rdx), %r10 |
152 | |
153 | /* Setup finally %rdx. */ |
154 | movq oRDX(%rdx), %rdx |
155 | |
156 | /* Check if return address is valid for the case when setcontext |
157 | is invoked from __start_context with linked context. */ |
158 | rdsspq %rax |
159 | cmpq (%rax), %r10 |
160 | /* Clear RAX to indicate success. NB: Don't use xorl to keep |
161 | EFLAGS for jne. */ |
162 | movl $0, %eax |
163 | jne L(jmp) |
164 | /* Return to the new context if return address valid. */ |
165 | pushq %r10 |
166 | ret |
167 | |
168 | L(jmp): |
169 | /* Jump to the new context directly. */ |
170 | jmp *%r10 |
171 | |
172 | L(no_shstk): |
173 | #endif |
174 | /* The following ret should return to the address set with |
175 | getcontext. Therefore push the address on the stack. */ |
176 | movq oRIP(%rdx), %rcx |
177 | pushq %rcx |
178 | |
179 | movq oRSI(%rdx), %rsi |
180 | movq oRDI(%rdx), %rdi |
181 | movq oRCX(%rdx), %rcx |
182 | movq oR8(%rdx), %r8 |
183 | movq oR9(%rdx), %r9 |
184 | |
185 | /* Setup finally %rdx. */ |
186 | movq oRDX(%rdx), %rdx |
187 | |
188 | /* End FDE here, we fall into another context. */ |
189 | cfi_endproc |
190 | cfi_startproc |
191 | |
192 | /* Clear rax to indicate success. */ |
193 | xorl %eax, %eax |
194 | ret |
195 | PSEUDO_END(__setcontext) |
196 | |
197 | weak_alias (__setcontext, setcontext) |
198 | |