Commit | Line | Data |
---|---|---|
cd5b61d6 RK |
1 | /* |
2 | * Signal support for Hexagon processor | |
3 | * | |
4 | * Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved. | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License version 2 and | |
8 | * only version 2 as published by the Free Software Foundation. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program; if not, write to the Free Software | |
17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | |
18 | * 02110-1301, USA. | |
19 | */ | |
20 | ||
21 | #include <linux/linkage.h> | |
22 | #include <linux/syscalls.h> | |
23 | #include <linux/freezer.h> | |
24 | #include <linux/tracehook.h> | |
25 | #include <asm/registers.h> | |
26 | #include <asm/thread_info.h> | |
27 | #include <asm/unistd.h> | |
28 | #include <asm/uaccess.h> | |
29 | #include <asm/ucontext.h> | |
30 | #include <asm/cacheflush.h> | |
31 | #include <asm/signal.h> | |
32 | #include <asm/vdso.h> | |
33 | ||
cd5b61d6 RK |
34 | struct rt_sigframe { |
35 | unsigned long tramp[2]; | |
36 | struct siginfo info; | |
37 | struct ucontext uc; | |
38 | }; | |
39 | ||
40 | static void __user *get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, | |
41 | size_t frame_size) | |
42 | { | |
43 | unsigned long sp = regs->r29; | |
44 | ||
45 | /* Switch to signal stack if appropriate */ | |
46 | if ((ka->sa.sa_flags & SA_ONSTACK) && (sas_ss_flags(sp) == 0)) | |
47 | sp = current->sas_ss_sp + current->sas_ss_size; | |
48 | ||
49 | return (void __user *)((sp - frame_size) & ~(sizeof(long long) - 1)); | |
50 | } | |
51 | ||
52 | static int setup_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc) | |
53 | { | |
54 | unsigned long tmp; | |
55 | int err = 0; | |
56 | ||
57 | err |= copy_to_user(&sc->sc_regs.r0, ®s->r00, | |
58 | 32*sizeof(unsigned long)); | |
59 | ||
60 | err |= __put_user(regs->sa0, &sc->sc_regs.sa0); | |
61 | err |= __put_user(regs->lc0, &sc->sc_regs.lc0); | |
62 | err |= __put_user(regs->sa1, &sc->sc_regs.sa1); | |
63 | err |= __put_user(regs->lc1, &sc->sc_regs.lc1); | |
64 | err |= __put_user(regs->m0, &sc->sc_regs.m0); | |
65 | err |= __put_user(regs->m1, &sc->sc_regs.m1); | |
66 | err |= __put_user(regs->usr, &sc->sc_regs.usr); | |
67 | err |= __put_user(regs->preds, &sc->sc_regs.p3_0); | |
68 | err |= __put_user(regs->gp, &sc->sc_regs.gp); | |
69 | err |= __put_user(regs->ugp, &sc->sc_regs.ugp); | |
70 | ||
71 | tmp = pt_elr(regs); err |= __put_user(tmp, &sc->sc_regs.pc); | |
72 | tmp = pt_cause(regs); err |= __put_user(tmp, &sc->sc_regs.cause); | |
73 | tmp = pt_badva(regs); err |= __put_user(tmp, &sc->sc_regs.badva); | |
74 | ||
75 | return err; | |
76 | } | |
77 | ||
78 | static int restore_sigcontext(struct pt_regs *regs, | |
79 | struct sigcontext __user *sc) | |
80 | { | |
81 | unsigned long tmp; | |
82 | int err = 0; | |
83 | ||
84 | err |= copy_from_user(®s->r00, &sc->sc_regs.r0, | |
85 | 32 * sizeof(unsigned long)); | |
86 | ||
87 | err |= __get_user(regs->sa0, &sc->sc_regs.sa0); | |
88 | err |= __get_user(regs->lc0, &sc->sc_regs.lc0); | |
89 | err |= __get_user(regs->sa1, &sc->sc_regs.sa1); | |
90 | err |= __get_user(regs->lc1, &sc->sc_regs.lc1); | |
91 | err |= __get_user(regs->m0, &sc->sc_regs.m0); | |
92 | err |= __get_user(regs->m1, &sc->sc_regs.m1); | |
93 | err |= __get_user(regs->usr, &sc->sc_regs.usr); | |
94 | err |= __get_user(regs->preds, &sc->sc_regs.p3_0); | |
95 | err |= __get_user(regs->gp, &sc->sc_regs.gp); | |
96 | err |= __get_user(regs->ugp, &sc->sc_regs.ugp); | |
97 | ||
98 | err |= __get_user(tmp, &sc->sc_regs.pc); pt_set_elr(regs, tmp); | |
99 | ||
100 | return err; | |
101 | } | |
102 | ||
103 | /* | |
104 | * Setup signal stack frame with siginfo structure | |
105 | */ | |
106 | static int setup_rt_frame(int signr, struct k_sigaction *ka, siginfo_t *info, | |
107 | sigset_t *set, struct pt_regs *regs) | |
108 | { | |
109 | int err = 0; | |
110 | struct rt_sigframe __user *frame; | |
111 | struct hexagon_vdso *vdso = current->mm->context.vdso; | |
112 | ||
113 | frame = get_sigframe(ka, regs, sizeof(struct rt_sigframe)); | |
114 | ||
115 | if (!access_ok(VERIFY_WRITE, frame, sizeof(struct rt_sigframe))) | |
116 | goto sigsegv; | |
117 | ||
118 | if (copy_siginfo_to_user(&frame->info, info)) | |
119 | goto sigsegv; | |
120 | ||
121 | /* The on-stack signal trampoline is no longer executed; | |
122 | * however, the libgcc signal frame unwinding code checks for | |
123 | * the presence of these two numeric magic values. | |
124 | */ | |
125 | err |= __put_user(0x7800d166, &frame->tramp[0]); | |
126 | err |= __put_user(0x5400c004, &frame->tramp[1]); | |
127 | err |= setup_sigcontext(regs, &frame->uc.uc_mcontext); | |
128 | err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)); | |
129 | if (err) | |
130 | goto sigsegv; | |
131 | ||
132 | /* Load r0/r1 pair with signumber/siginfo pointer... */ | |
133 | regs->r0100 = ((unsigned long long)((unsigned long)&frame->info) << 32) | |
134 | | (unsigned long long)signr; | |
135 | regs->r02 = (unsigned long) &frame->uc; | |
136 | regs->r31 = (unsigned long) vdso->rt_signal_trampoline; | |
137 | pt_psp(regs) = (unsigned long) frame; | |
138 | pt_set_elr(regs, (unsigned long)ka->sa.sa_handler); | |
139 | ||
140 | return 0; | |
141 | ||
142 | sigsegv: | |
143 | force_sigsegv(signr, current); | |
144 | return -EFAULT; | |
145 | } | |
146 | ||
147 | /* | |
148 | * Setup invocation of signal handler | |
149 | */ | |
a610d6e6 | 150 | static void handle_signal(int sig, siginfo_t *info, struct k_sigaction *ka, |
b7f9a11a | 151 | struct pt_regs *regs) |
cd5b61d6 | 152 | { |
cd5b61d6 RK |
153 | /* |
154 | * If we're handling a signal that aborted a system call, | |
155 | * set up the error return value before adding the signal | |
156 | * frame to the stack. | |
157 | */ | |
158 | ||
159 | if (regs->syscall_nr >= 0) { | |
160 | switch (regs->r00) { | |
161 | case -ERESTART_RESTARTBLOCK: | |
162 | case -ERESTARTNOHAND: | |
163 | regs->r00 = -EINTR; | |
164 | break; | |
165 | case -ERESTARTSYS: | |
166 | if (!(ka->sa.sa_flags & SA_RESTART)) { | |
167 | regs->r00 = -EINTR; | |
168 | break; | |
169 | } | |
170 | /* Fall through */ | |
171 | case -ERESTARTNOINTR: | |
172 | regs->r06 = regs->syscall_nr; | |
173 | pt_set_elr(regs, pt_elr(regs) - 4); | |
174 | regs->r00 = regs->restart_r0; | |
175 | break; | |
176 | default: | |
177 | break; | |
178 | } | |
179 | } | |
180 | ||
181 | /* | |
182 | * Set up the stack frame; not doing the SA_SIGINFO thing. We | |
183 | * only set up the rt_frame flavor. | |
184 | */ | |
cd5b61d6 | 185 | /* If there was an error on setup, no signal was delivered. */ |
a610d6e6 AV |
186 | if (setup_rt_frame(sig, ka, info, sigmask_to_save(), regs) < 0) |
187 | return; | |
cd5b61d6 | 188 | |
43aca324 | 189 | block_sigmask(ka, sig); |
a610d6e6 AV |
190 | tracehook_signal_handler(sig, info, ka, regs, |
191 | test_thread_flag(TIF_SINGLESTEP)); | |
cd5b61d6 RK |
192 | } |
193 | ||
194 | /* | |
195 | * Called from return-from-event code. | |
196 | */ | |
197 | static void do_signal(struct pt_regs *regs) | |
198 | { | |
199 | struct k_sigaction sigact; | |
200 | siginfo_t info; | |
201 | int signo; | |
202 | ||
203 | if (!user_mode(regs)) | |
204 | return; | |
205 | ||
cd5b61d6 RK |
206 | signo = get_signal_to_deliver(&info, &sigact, regs, NULL); |
207 | ||
208 | if (signo > 0) { | |
a610d6e6 | 209 | handle_signal(signo, &info, &sigact, regs); |
cd5b61d6 RK |
210 | return; |
211 | } | |
212 | ||
cd5b61d6 RK |
213 | /* |
214 | * If we came from a system call, handle the restart. | |
215 | */ | |
216 | if (regs->syscall_nr >= 0) { | |
217 | switch (regs->r00) { | |
218 | case -ERESTARTNOHAND: | |
219 | case -ERESTARTSYS: | |
220 | case -ERESTARTNOINTR: | |
221 | regs->r06 = regs->syscall_nr; | |
222 | break; | |
223 | case -ERESTART_RESTARTBLOCK: | |
224 | regs->r06 = __NR_restart_syscall; | |
225 | break; | |
226 | default: | |
227 | goto no_restart; | |
228 | } | |
229 | pt_set_elr(regs, pt_elr(regs) - 4); | |
230 | regs->r00 = regs->restart_r0; | |
231 | } | |
232 | ||
233 | no_restart: | |
234 | /* If there's no signal to deliver, put the saved sigmask back */ | |
51a7b448 | 235 | restore_saved_sigmask(); |
cd5b61d6 RK |
236 | } |
237 | ||
238 | void do_notify_resume(struct pt_regs *regs, unsigned long thread_info_flags) | |
239 | { | |
240 | if (thread_info_flags & _TIF_SIGPENDING) | |
241 | do_signal(regs); | |
242 | ||
243 | if (thread_info_flags & _TIF_NOTIFY_RESUME) { | |
244 | clear_thread_flag(TIF_NOTIFY_RESUME); | |
3334bf12 | 245 | tracehook_notify_resume(regs); |
cd5b61d6 RK |
246 | } |
247 | } | |
248 | ||
249 | /* | |
250 | * Architecture-specific wrappers for signal-related system calls | |
251 | */ | |
252 | asmlinkage int sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss) | |
253 | { | |
254 | struct pt_regs *regs = current_thread_info()->regs; | |
255 | ||
256 | return do_sigaltstack(uss, uoss, regs->r29); | |
257 | } | |
258 | ||
259 | asmlinkage int sys_rt_sigreturn(void) | |
260 | { | |
261 | struct pt_regs *regs = current_thread_info()->regs; | |
262 | struct rt_sigframe __user *frame; | |
263 | sigset_t blocked; | |
264 | ||
ef9b122f AV |
265 | /* Always make any pending restarted system calls return -EINTR */ |
266 | current_thread_info()->restart_block.fn = do_no_restart_syscall; | |
267 | ||
cd5b61d6 RK |
268 | frame = (struct rt_sigframe __user *)pt_psp(regs); |
269 | if (!access_ok(VERIFY_READ, frame, sizeof(*frame))) | |
270 | goto badframe; | |
271 | if (__copy_from_user(&blocked, &frame->uc.uc_sigmask, sizeof(blocked))) | |
272 | goto badframe; | |
273 | ||
43aca324 | 274 | set_current_blocked(&blocked); |
cd5b61d6 RK |
275 | |
276 | if (restore_sigcontext(regs, &frame->uc.uc_mcontext)) | |
277 | goto badframe; | |
278 | ||
279 | /* Restore the user's stack as well */ | |
280 | pt_psp(regs) = regs->r29; | |
281 | ||
282 | /* | |
283 | * Leave a trace in the stack frame that this was a sigreturn. | |
284 | * If the system call is to replay, we've already restored the | |
285 | * number in the GPR slot and it will be regenerated on the | |
286 | * new system call trap entry. Note that if restore_sigcontext() | |
287 | * did something other than a bulk copy of the pt_regs struct, | |
288 | * we could avoid this assignment by simply not overwriting | |
289 | * regs->syscall_nr. | |
290 | */ | |
291 | regs->syscall_nr = __NR_rt_sigreturn; | |
292 | ||
293 | /* | |
294 | * If we were meticulous, we'd only call this if we knew that | |
295 | * we were actually going to use an alternate stack, and we'd | |
296 | * consider any error to be fatal. What we do here, in common | |
297 | * with many other architectures, is call it blindly and only | |
298 | * consider the -EFAULT return case to be proof of a problem. | |
299 | */ | |
300 | if (do_sigaltstack(&frame->uc.uc_stack, NULL, pt_psp(regs)) == -EFAULT) | |
301 | goto badframe; | |
302 | ||
303 | return 0; | |
304 | ||
305 | badframe: | |
306 | force_sig(SIGSEGV, current); | |
307 | return 0; | |
308 | } |