]>
Commit | Line | Data |
---|---|---|
3cf01adb | 1 | /* Install given context. |
04277e02 | 2 | Copyright (C) 2002-2019 Free Software Foundation, Inc. |
3cf01adb AJ |
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 | |
59ba27a6 PE |
17 | License along with the GNU C Library; if not, see |
18 | <http://www.gnu.org/licenses/>. */ | |
3cf01adb AJ |
19 | |
20 | #include <sysdep.h> | |
25123a1c | 21 | #include <asm/prctl.h> |
3cf01adb AJ |
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 | |
4ef09295 | 38 | cfi_adjust_cfa_offset(8) |
3cf01adb AJ |
39 | |
40 | /* Set the signal mask with | |
41 | rt_sigprocmask (SIG_SETMASK, mask, NULL, _NSIG/8). */ | |
42 | leaq oSIGMASK(%rdi), %rsi | |
ee618985 UD |
43 | xorl %edx, %edx |
44 | movl $SIG_SETMASK, %edi | |
45 | movl $_NSIG8,%r10d | |
46 | movl $__NR_rt_sigprocmask, %eax | |
3cf01adb | 47 | syscall |
8b8f32b2 L |
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 | |
4ef09295 | 52 | cfi_adjust_cfa_offset(-8) |
3cf01adb AJ |
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. */ | |
8b8f32b2 | 58 | movq oFPREGS(%rdx), %rcx |
3cf01adb | 59 | fldenv (%rcx) |
8b8f32b2 | 60 | ldmxcsr oMXCSR(%rdx) |
3cf01adb AJ |
61 | |
62 | ||
c816e074 AJ |
63 | /* Load the new stack pointer, the preserved registers and |
64 | registers used for passing args. */ | |
8b8f32b2 | 65 | cfi_def_cfa(%rdx, 0) |
4ef09295 AJ |
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 | ||
8b8f32b2 L |
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 | |
3cf01adb | 82 | |
25123a1c L |
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 | |
3cf01adb AJ |
174 | /* The following ret should return to the address set with |
175 | getcontext. Therefore push the address on the stack. */ | |
8b8f32b2 | 176 | movq oRIP(%rdx), %rcx |
3cf01adb AJ |
177 | pushq %rcx |
178 | ||
8b8f32b2 L |
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 | |
c816e074 | 184 | |
8b8f32b2 L |
185 | /* Setup finally %rdx. */ |
186 | movq oRDX(%rdx), %rdx | |
c816e074 | 187 | |
4ef09295 AJ |
188 | /* End FDE here, we fall into another context. */ |
189 | cfi_endproc | |
190 | cfi_startproc | |
191 | ||
3cf01adb | 192 | /* Clear rax to indicate success. */ |
ee618985 | 193 | xorl %eax, %eax |
3cf01adb AJ |
194 | ret |
195 | PSEUDO_END(__setcontext) | |
196 | ||
1ab18a5b | 197 | weak_alias (__setcontext, setcontext) |