blob: 5f7d7c8a13282ffddb4be65ddfe8875b9ad482bd [file] [log] [blame]
Richard Kuocd5b61d2011-10-31 18:41:21 -05001/*
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
34#define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
35
36struct rt_sigframe {
37 unsigned long tramp[2];
38 struct siginfo info;
39 struct ucontext uc;
40};
41
42static void __user *get_sigframe(struct k_sigaction *ka, struct pt_regs *regs,
43 size_t frame_size)
44{
45 unsigned long sp = regs->r29;
46
47 /* Switch to signal stack if appropriate */
48 if ((ka->sa.sa_flags & SA_ONSTACK) && (sas_ss_flags(sp) == 0))
49 sp = current->sas_ss_sp + current->sas_ss_size;
50
51 return (void __user *)((sp - frame_size) & ~(sizeof(long long) - 1));
52}
53
54static int setup_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
55{
56 unsigned long tmp;
57 int err = 0;
58
59 err |= copy_to_user(&sc->sc_regs.r0, &regs->r00,
60 32*sizeof(unsigned long));
61
62 err |= __put_user(regs->sa0, &sc->sc_regs.sa0);
63 err |= __put_user(regs->lc0, &sc->sc_regs.lc0);
64 err |= __put_user(regs->sa1, &sc->sc_regs.sa1);
65 err |= __put_user(regs->lc1, &sc->sc_regs.lc1);
66 err |= __put_user(regs->m0, &sc->sc_regs.m0);
67 err |= __put_user(regs->m1, &sc->sc_regs.m1);
68 err |= __put_user(regs->usr, &sc->sc_regs.usr);
69 err |= __put_user(regs->preds, &sc->sc_regs.p3_0);
70 err |= __put_user(regs->gp, &sc->sc_regs.gp);
71 err |= __put_user(regs->ugp, &sc->sc_regs.ugp);
72
73 tmp = pt_elr(regs); err |= __put_user(tmp, &sc->sc_regs.pc);
74 tmp = pt_cause(regs); err |= __put_user(tmp, &sc->sc_regs.cause);
75 tmp = pt_badva(regs); err |= __put_user(tmp, &sc->sc_regs.badva);
76
77 return err;
78}
79
80static int restore_sigcontext(struct pt_regs *regs,
81 struct sigcontext __user *sc)
82{
83 unsigned long tmp;
84 int err = 0;
85
86 err |= copy_from_user(&regs->r00, &sc->sc_regs.r0,
87 32 * sizeof(unsigned long));
88
89 err |= __get_user(regs->sa0, &sc->sc_regs.sa0);
90 err |= __get_user(regs->lc0, &sc->sc_regs.lc0);
91 err |= __get_user(regs->sa1, &sc->sc_regs.sa1);
92 err |= __get_user(regs->lc1, &sc->sc_regs.lc1);
93 err |= __get_user(regs->m0, &sc->sc_regs.m0);
94 err |= __get_user(regs->m1, &sc->sc_regs.m1);
95 err |= __get_user(regs->usr, &sc->sc_regs.usr);
96 err |= __get_user(regs->preds, &sc->sc_regs.p3_0);
97 err |= __get_user(regs->gp, &sc->sc_regs.gp);
98 err |= __get_user(regs->ugp, &sc->sc_regs.ugp);
99
100 err |= __get_user(tmp, &sc->sc_regs.pc); pt_set_elr(regs, tmp);
101
102 return err;
103}
104
105/*
106 * Setup signal stack frame with siginfo structure
107 */
108static int setup_rt_frame(int signr, struct k_sigaction *ka, siginfo_t *info,
109 sigset_t *set, struct pt_regs *regs)
110{
111 int err = 0;
112 struct rt_sigframe __user *frame;
113 struct hexagon_vdso *vdso = current->mm->context.vdso;
114
115 frame = get_sigframe(ka, regs, sizeof(struct rt_sigframe));
116
117 if (!access_ok(VERIFY_WRITE, frame, sizeof(struct rt_sigframe)))
118 goto sigsegv;
119
120 if (copy_siginfo_to_user(&frame->info, info))
121 goto sigsegv;
122
123 /* The on-stack signal trampoline is no longer executed;
124 * however, the libgcc signal frame unwinding code checks for
125 * the presence of these two numeric magic values.
126 */
127 err |= __put_user(0x7800d166, &frame->tramp[0]);
128 err |= __put_user(0x5400c004, &frame->tramp[1]);
129 err |= setup_sigcontext(regs, &frame->uc.uc_mcontext);
130 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
131 if (err)
132 goto sigsegv;
133
134 /* Load r0/r1 pair with signumber/siginfo pointer... */
135 regs->r0100 = ((unsigned long long)((unsigned long)&frame->info) << 32)
136 | (unsigned long long)signr;
137 regs->r02 = (unsigned long) &frame->uc;
138 regs->r31 = (unsigned long) vdso->rt_signal_trampoline;
139 pt_psp(regs) = (unsigned long) frame;
140 pt_set_elr(regs, (unsigned long)ka->sa.sa_handler);
141
142 return 0;
143
144sigsegv:
145 force_sigsegv(signr, current);
146 return -EFAULT;
147}
148
149/*
150 * Setup invocation of signal handler
151 */
Al Viroa610d6e2012-05-21 23:42:15 -0400152static void handle_signal(int sig, siginfo_t *info, struct k_sigaction *ka,
Al Virob7f9a112012-05-02 09:59:21 -0400153 struct pt_regs *regs)
Richard Kuocd5b61d2011-10-31 18:41:21 -0500154{
Richard Kuocd5b61d2011-10-31 18:41:21 -0500155 /*
156 * If we're handling a signal that aborted a system call,
157 * set up the error return value before adding the signal
158 * frame to the stack.
159 */
160
161 if (regs->syscall_nr >= 0) {
162 switch (regs->r00) {
163 case -ERESTART_RESTARTBLOCK:
164 case -ERESTARTNOHAND:
165 regs->r00 = -EINTR;
166 break;
167 case -ERESTARTSYS:
168 if (!(ka->sa.sa_flags & SA_RESTART)) {
169 regs->r00 = -EINTR;
170 break;
171 }
172 /* Fall through */
173 case -ERESTARTNOINTR:
174 regs->r06 = regs->syscall_nr;
175 pt_set_elr(regs, pt_elr(regs) - 4);
176 regs->r00 = regs->restart_r0;
177 break;
178 default:
179 break;
180 }
181 }
182
183 /*
184 * Set up the stack frame; not doing the SA_SIGINFO thing. We
185 * only set up the rt_frame flavor.
186 */
Richard Kuocd5b61d2011-10-31 18:41:21 -0500187 /* If there was an error on setup, no signal was delivered. */
Al Viroa610d6e2012-05-21 23:42:15 -0400188 if (setup_rt_frame(sig, ka, info, sigmask_to_save(), regs) < 0)
189 return;
Richard Kuocd5b61d2011-10-31 18:41:21 -0500190
Matt Fleming43aca322012-03-23 15:02:43 -0700191 block_sigmask(ka, sig);
Al Viroa610d6e2012-05-21 23:42:15 -0400192 tracehook_signal_handler(sig, info, ka, regs,
193 test_thread_flag(TIF_SINGLESTEP));
Richard Kuocd5b61d2011-10-31 18:41:21 -0500194}
195
196/*
197 * Called from return-from-event code.
198 */
199static void do_signal(struct pt_regs *regs)
200{
201 struct k_sigaction sigact;
202 siginfo_t info;
203 int signo;
204
205 if (!user_mode(regs))
206 return;
207
208 if (try_to_freeze())
209 goto no_signal;
210
211 signo = get_signal_to_deliver(&info, &sigact, regs, NULL);
212
213 if (signo > 0) {
Al Viroa610d6e2012-05-21 23:42:15 -0400214 handle_signal(signo, &info, &sigact, regs);
Richard Kuocd5b61d2011-10-31 18:41:21 -0500215 return;
216 }
217
218no_signal:
219 /*
220 * If we came from a system call, handle the restart.
221 */
222 if (regs->syscall_nr >= 0) {
223 switch (regs->r00) {
224 case -ERESTARTNOHAND:
225 case -ERESTARTSYS:
226 case -ERESTARTNOINTR:
227 regs->r06 = regs->syscall_nr;
228 break;
229 case -ERESTART_RESTARTBLOCK:
230 regs->r06 = __NR_restart_syscall;
231 break;
232 default:
233 goto no_restart;
234 }
235 pt_set_elr(regs, pt_elr(regs) - 4);
236 regs->r00 = regs->restart_r0;
237 }
238
239no_restart:
240 /* If there's no signal to deliver, put the saved sigmask back */
Al Viro51a7b442012-05-21 23:33:55 -0400241 restore_saved_sigmask();
Richard Kuocd5b61d2011-10-31 18:41:21 -0500242}
243
244void do_notify_resume(struct pt_regs *regs, unsigned long thread_info_flags)
245{
246 if (thread_info_flags & _TIF_SIGPENDING)
247 do_signal(regs);
248
249 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
250 clear_thread_flag(TIF_NOTIFY_RESUME);
Oleg Nesterov3334bf12012-05-11 10:59:08 +1000251 tracehook_notify_resume(regs);
Richard Kuocd5b61d2011-10-31 18:41:21 -0500252 }
253}
254
255/*
256 * Architecture-specific wrappers for signal-related system calls
257 */
258asmlinkage int sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss)
259{
260 struct pt_regs *regs = current_thread_info()->regs;
261
262 return do_sigaltstack(uss, uoss, regs->r29);
263}
264
265asmlinkage int sys_rt_sigreturn(void)
266{
267 struct pt_regs *regs = current_thread_info()->regs;
268 struct rt_sigframe __user *frame;
269 sigset_t blocked;
270
Al Viroef9b1222012-04-22 03:31:24 -0400271 /* Always make any pending restarted system calls return -EINTR */
272 current_thread_info()->restart_block.fn = do_no_restart_syscall;
273
Richard Kuocd5b61d2011-10-31 18:41:21 -0500274 frame = (struct rt_sigframe __user *)pt_psp(regs);
275 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
276 goto badframe;
277 if (__copy_from_user(&blocked, &frame->uc.uc_sigmask, sizeof(blocked)))
278 goto badframe;
279
280 sigdelsetmask(&blocked, ~_BLOCKABLE);
Matt Fleming43aca322012-03-23 15:02:43 -0700281 set_current_blocked(&blocked);
Richard Kuocd5b61d2011-10-31 18:41:21 -0500282
283 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
284 goto badframe;
285
286 /* Restore the user's stack as well */
287 pt_psp(regs) = regs->r29;
288
289 /*
290 * Leave a trace in the stack frame that this was a sigreturn.
291 * If the system call is to replay, we've already restored the
292 * number in the GPR slot and it will be regenerated on the
293 * new system call trap entry. Note that if restore_sigcontext()
294 * did something other than a bulk copy of the pt_regs struct,
295 * we could avoid this assignment by simply not overwriting
296 * regs->syscall_nr.
297 */
298 regs->syscall_nr = __NR_rt_sigreturn;
299
300 /*
301 * If we were meticulous, we'd only call this if we knew that
302 * we were actually going to use an alternate stack, and we'd
303 * consider any error to be fatal. What we do here, in common
304 * with many other architectures, is call it blindly and only
305 * consider the -EFAULT return case to be proof of a problem.
306 */
307 if (do_sigaltstack(&frame->uc.uc_stack, NULL, pt_psp(regs)) == -EFAULT)
308 goto badframe;
309
310 return 0;
311
312badframe:
313 force_sig(SIGSEGV, current);
314 return 0;
315}