blob: d822700d4f15dacf49d96c612bc1ea6ac65c3c4b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* signal.c: FRV specific bits of signal handling
2 *
3 * Copyright (C) 2003-5 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 * - Derived from arch/m68k/kernel/signal.c
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 */
12
13#include <linux/sched.h>
14#include <linux/mm.h>
15#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/kernel.h>
17#include <linux/signal.h>
18#include <linux/errno.h>
19#include <linux/wait.h>
20#include <linux/ptrace.h>
21#include <linux/unistd.h>
22#include <linux/personality.h>
David Howells4a3b9892009-06-11 13:05:24 +010023#include <linux/tracehook.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <asm/ucontext.h>
25#include <asm/uaccess.h>
26#include <asm/cacheflush.h>
27
28#define DEBUG_SIG 0
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030struct fdpic_func_descriptor {
31 unsigned long text;
32 unsigned long GOT;
33};
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035/*
36 * Do a signal return; undo the signal stack.
37 */
38
39struct sigframe
40{
Al Viro9e4d11f2006-06-23 02:04:04 -070041 __sigrestore_t pretcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 int sig;
43 struct sigcontext sc;
44 unsigned long extramask[_NSIG_WORDS-1];
45 uint32_t retcode[2];
46};
47
48struct rt_sigframe
49{
Al Viro9e4d11f2006-06-23 02:04:04 -070050 __sigrestore_t pretcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 int sig;
Al Viro9e4d11f2006-06-23 02:04:04 -070052 struct siginfo __user *pinfo;
53 void __user *puc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 struct siginfo info;
55 struct ucontext uc;
56 uint32_t retcode[2];
57};
58
59static int restore_sigcontext(struct sigcontext __user *sc, int *_gr8)
60{
61 struct user_context *user = current->thread.user;
62 unsigned long tbr, psr;
63
Al Viro20cd5142010-09-20 15:13:04 +010064 /* Always make any pending restarted system calls return -EINTR */
65 current_thread_info()->restart_block.fn = do_no_restart_syscall;
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 tbr = user->i.tbr;
68 psr = user->i.psr;
69 if (copy_from_user(user, &sc->sc_context, sizeof(sc->sc_context)))
70 goto badframe;
71 user->i.tbr = tbr;
72 user->i.psr = psr;
73
74 restore_user_regs(user);
75
76 user->i.syscallno = -1; /* disable syscall checks */
77
78 *_gr8 = user->i.gr[8];
79 return 0;
80
81 badframe:
82 return 1;
83}
84
85asmlinkage int sys_sigreturn(void)
86{
87 struct sigframe __user *frame = (struct sigframe __user *) __frame->sp;
88 sigset_t set;
89 int gr8;
90
91 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
92 goto badframe;
93 if (__get_user(set.sig[0], &frame->sc.sc_oldmask))
94 goto badframe;
95
96 if (_NSIG_WORDS > 1 &&
97 __copy_from_user(&set.sig[1], &frame->extramask, sizeof(frame->extramask)))
98 goto badframe;
99
Matt Fleming7ebe0c52012-05-11 10:58:52 +1000100 set_current_blocked(&set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102 if (restore_sigcontext(&frame->sc, &gr8))
103 goto badframe;
104 return gr8;
105
106 badframe:
107 force_sig(SIGSEGV, current);
108 return 0;
109}
110
111asmlinkage int sys_rt_sigreturn(void)
112{
113 struct rt_sigframe __user *frame = (struct rt_sigframe __user *) __frame->sp;
114 sigset_t set;
115 int gr8;
116
117 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
118 goto badframe;
119 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
120 goto badframe;
121
Matt Fleming7ebe0c52012-05-11 10:58:52 +1000122 set_current_blocked(&set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124 if (restore_sigcontext(&frame->uc.uc_mcontext, &gr8))
125 goto badframe;
126
Al Virofe761412012-12-23 02:13:57 -0500127 if (restore_altstack(&frame->uc.uc_stack))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 goto badframe;
129
130 return gr8;
131
132badframe:
133 force_sig(SIGSEGV, current);
134 return 0;
135}
136
137/*
138 * Set up a signal frame
139 */
140static int setup_sigcontext(struct sigcontext __user *sc, unsigned long mask)
141{
142 save_user_regs(current->thread.user);
143
144 if (copy_to_user(&sc->sc_context, current->thread.user, sizeof(sc->sc_context)) != 0)
145 goto badframe;
146
147 /* non-iBCS2 extensions.. */
148 if (__put_user(mask, &sc->sc_oldmask) < 0)
149 goto badframe;
150
151 return 0;
152
153 badframe:
154 return 1;
155}
156
157/*****************************************************************************/
158/*
159 * Determine which stack to use..
160 */
161static inline void __user *get_sigframe(struct k_sigaction *ka,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 size_t frame_size)
163{
164 unsigned long sp;
165
166 /* Default to using normal stack */
David Howellsfef2b582006-01-06 00:11:45 -0800167 sp = __frame->sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169 /* This is the X/Open sanctioned signal stack switching. */
170 if (ka->sa.sa_flags & SA_ONSTACK) {
Laurent MEYERd09042d2006-06-23 02:05:36 -0700171 if (! sas_ss_flags(sp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 sp = current->sas_ss_sp + current->sas_ss_size;
173 }
174
175 return (void __user *) ((sp - frame_size) & ~7UL);
David Howellsfef2b582006-01-06 00:11:45 -0800176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177} /* end get_sigframe() */
178
179/*****************************************************************************/
180/*
181 *
182 */
David Howellsfef2b582006-01-06 00:11:45 -0800183static int setup_frame(int sig, struct k_sigaction *ka, sigset_t *set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
185 struct sigframe __user *frame;
186 int rsig;
187
Al Viro5f4ad042010-09-20 15:13:09 +0100188 set_fs(USER_DS);
189
David Howellsfef2b582006-01-06 00:11:45 -0800190 frame = get_sigframe(ka, sizeof(*frame));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
193 goto give_sigsegv;
194
195 rsig = sig;
196 if (sig < 32 &&
197 __current_thread_info->exec_domain &&
198 __current_thread_info->exec_domain->signal_invmap)
199 rsig = __current_thread_info->exec_domain->signal_invmap[sig];
200
201 if (__put_user(rsig, &frame->sig) < 0)
202 goto give_sigsegv;
203
204 if (setup_sigcontext(&frame->sc, set->sig[0]))
205 goto give_sigsegv;
206
207 if (_NSIG_WORDS > 1) {
208 if (__copy_to_user(frame->extramask, &set->sig[1],
209 sizeof(frame->extramask)))
210 goto give_sigsegv;
211 }
212
213 /* Set up to return from userspace. If provided, use a stub
214 * already in userspace. */
215 if (ka->sa.sa_flags & SA_RESTORER) {
216 if (__put_user(ka->sa.sa_restorer, &frame->pretcode) < 0)
217 goto give_sigsegv;
218 }
219 else {
220 /* Set up the following code on the stack:
221 * setlos #__NR_sigreturn,gr7
222 * tira gr0,0
223 */
Al Viro9e4d11f2006-06-23 02:04:04 -0700224 if (__put_user((__sigrestore_t)frame->retcode, &frame->pretcode) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 __put_user(0x8efc0000|__NR_sigreturn, &frame->retcode[0]) ||
226 __put_user(0xc0700000, &frame->retcode[1]))
227 goto give_sigsegv;
228
229 flush_icache_range((unsigned long) frame->retcode,
230 (unsigned long) (frame->retcode + 2));
231 }
232
Al Viro5f4ad042010-09-20 15:13:09 +0100233 /* Set up registers for the signal handler */
WANG Congecd0fa92008-04-29 00:59:15 -0700234 if (current->personality & FDPIC_FUNCPTRS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 struct fdpic_func_descriptor __user *funcptr =
Al Viro9e4d11f2006-06-23 02:04:04 -0700236 (struct fdpic_func_descriptor __user *) ka->sa.sa_handler;
Al Viro5f4ad042010-09-20 15:13:09 +0100237 struct fdpic_func_descriptor desc;
238 if (copy_from_user(&desc, funcptr, sizeof(desc)))
239 goto give_sigsegv;
240 __frame->pc = desc.text;
241 __frame->gr15 = desc.GOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 } else {
David Howellsfef2b582006-01-06 00:11:45 -0800243 __frame->pc = (unsigned long) ka->sa.sa_handler;
244 __frame->gr15 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 }
246
Al Viro5f4ad042010-09-20 15:13:09 +0100247 __frame->sp = (unsigned long) frame;
248 __frame->lr = (unsigned long) &frame->retcode;
249 __frame->gr8 = sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251#if DEBUG_SIG
252 printk("SIG deliver %d (%s:%d): sp=%p pc=%lx ra=%p\n",
David Howellsfef2b582006-01-06 00:11:45 -0800253 sig, current->comm, current->pid, frame, __frame->pc,
David Howells8efc0ab2006-01-06 00:11:44 -0800254 frame->pretcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255#endif
256
David Howellsa411aee2006-01-18 17:43:59 -0800257 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259give_sigsegv:
Al Viroad0acab2010-09-20 15:13:14 +0100260 force_sigsegv(sig, current);
David Howellsa411aee2006-01-18 17:43:59 -0800261 return -EFAULT;
David Howells8efc0ab2006-01-06 00:11:44 -0800262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263} /* end setup_frame() */
264
265/*****************************************************************************/
266/*
267 *
268 */
David Howells8efc0ab2006-01-06 00:11:44 -0800269static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
David Howellsfef2b582006-01-06 00:11:45 -0800270 sigset_t *set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
272 struct rt_sigframe __user *frame;
273 int rsig;
274
Al Viro5f4ad042010-09-20 15:13:09 +0100275 set_fs(USER_DS);
276
David Howellsfef2b582006-01-06 00:11:45 -0800277 frame = get_sigframe(ka, sizeof(*frame));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
280 goto give_sigsegv;
281
282 rsig = sig;
283 if (sig < 32 &&
284 __current_thread_info->exec_domain &&
285 __current_thread_info->exec_domain->signal_invmap)
286 rsig = __current_thread_info->exec_domain->signal_invmap[sig];
287
288 if (__put_user(rsig, &frame->sig) ||
289 __put_user(&frame->info, &frame->pinfo) ||
290 __put_user(&frame->uc, &frame->puc))
291 goto give_sigsegv;
292
293 if (copy_siginfo_to_user(&frame->info, info))
294 goto give_sigsegv;
295
296 /* Create the ucontext. */
297 if (__put_user(0, &frame->uc.uc_flags) ||
Al Viro9e4d11f2006-06-23 02:04:04 -0700298 __put_user(NULL, &frame->uc.uc_link) ||
Al Virofe761412012-12-23 02:13:57 -0500299 __save_altstack(&frame->uc.uc_stack, __frame->sp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 goto give_sigsegv;
301
302 if (setup_sigcontext(&frame->uc.uc_mcontext, set->sig[0]))
303 goto give_sigsegv;
304
305 if (__copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)))
306 goto give_sigsegv;
307
308 /* Set up to return from userspace. If provided, use a stub
309 * already in userspace. */
310 if (ka->sa.sa_flags & SA_RESTORER) {
311 if (__put_user(ka->sa.sa_restorer, &frame->pretcode))
312 goto give_sigsegv;
313 }
314 else {
315 /* Set up the following code on the stack:
316 * setlos #__NR_sigreturn,gr7
317 * tira gr0,0
318 */
Al Viro9e4d11f2006-06-23 02:04:04 -0700319 if (__put_user((__sigrestore_t)frame->retcode, &frame->pretcode) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 __put_user(0x8efc0000|__NR_rt_sigreturn, &frame->retcode[0]) ||
321 __put_user(0xc0700000, &frame->retcode[1]))
322 goto give_sigsegv;
323
324 flush_icache_range((unsigned long) frame->retcode,
325 (unsigned long) (frame->retcode + 2));
326 }
327
328 /* Set up registers for signal handler */
WANG Congecd0fa92008-04-29 00:59:15 -0700329 if (current->personality & FDPIC_FUNCPTRS) {
Al Viro9e4d11f2006-06-23 02:04:04 -0700330 struct fdpic_func_descriptor __user *funcptr =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 (struct fdpic_func_descriptor __user *) ka->sa.sa_handler;
Al Viro5f4ad042010-09-20 15:13:09 +0100332 struct fdpic_func_descriptor desc;
333 if (copy_from_user(&desc, funcptr, sizeof(desc)))
334 goto give_sigsegv;
335 __frame->pc = desc.text;
336 __frame->gr15 = desc.GOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 } else {
David Howellsfef2b582006-01-06 00:11:45 -0800338 __frame->pc = (unsigned long) ka->sa.sa_handler;
339 __frame->gr15 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 }
341
Al Viro5f4ad042010-09-20 15:13:09 +0100342 __frame->sp = (unsigned long) frame;
343 __frame->lr = (unsigned long) &frame->retcode;
344 __frame->gr8 = sig;
345 __frame->gr9 = (unsigned long) &frame->info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347#if DEBUG_SIG
348 printk("SIG deliver %d (%s:%d): sp=%p pc=%lx ra=%p\n",
David Howellsfef2b582006-01-06 00:11:45 -0800349 sig, current->comm, current->pid, frame, __frame->pc,
David Howells8efc0ab2006-01-06 00:11:44 -0800350 frame->pretcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351#endif
352
David Howellsa411aee2006-01-18 17:43:59 -0800353 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355give_sigsegv:
Al Viroad0acab2010-09-20 15:13:14 +0100356 force_sigsegv(sig, current);
David Howellsa411aee2006-01-18 17:43:59 -0800357 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359} /* end setup_rt_frame() */
360
361/*****************************************************************************/
362/*
363 * OK, we're invoking a handler
364 */
Al Viroa610d6e2012-05-21 23:42:15 -0400365static void handle_signal(unsigned long sig, siginfo_t *info,
Al Virob7f9a112012-05-02 09:59:21 -0400366 struct k_sigaction *ka)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Al Virob7f9a112012-05-02 09:59:21 -0400368 sigset_t *oldset = sigmask_to_save();
David Howells8efc0ab2006-01-06 00:11:44 -0800369 int ret;
370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 /* Are we from a system call? */
Al Viroed1cde62010-09-20 15:13:25 +0100372 if (__frame->syscallno != -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 /* If so, check system call restarting.. */
David Howellsfef2b582006-01-06 00:11:45 -0800374 switch (__frame->gr8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 case -ERESTART_RESTARTBLOCK:
376 case -ERESTARTNOHAND:
David Howellsfef2b582006-01-06 00:11:45 -0800377 __frame->gr8 = -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 break;
379
380 case -ERESTARTSYS:
381 if (!(ka->sa.sa_flags & SA_RESTART)) {
David Howellsfef2b582006-01-06 00:11:45 -0800382 __frame->gr8 = -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 break;
384 }
David Howells8efc0ab2006-01-06 00:11:44 -0800385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 /* fallthrough */
387 case -ERESTARTNOINTR:
David Howellsfef2b582006-01-06 00:11:45 -0800388 __frame->gr8 = __frame->orig_gr8;
389 __frame->pc -= 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 }
Al Viroed1cde62010-09-20 15:13:25 +0100391 __frame->syscallno = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 }
393
394 /* Set up the stack frame */
395 if (ka->sa.sa_flags & SA_SIGINFO)
David Howellsfef2b582006-01-06 00:11:45 -0800396 ret = setup_rt_frame(sig, ka, info, oldset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 else
David Howellsfef2b582006-01-06 00:11:45 -0800398 ret = setup_frame(sig, ka, oldset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Al Viroa610d6e2012-05-21 23:42:15 -0400400 if (ret)
401 return;
David Howells8efc0ab2006-01-06 00:11:44 -0800402
Al Viroefee9842012-04-28 02:04:15 -0400403 signal_delivered(sig, info, ka, __frame,
Al Viroa610d6e2012-05-21 23:42:15 -0400404 test_thread_flag(TIF_SINGLESTEP));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405} /* end handle_signal() */
406
407/*****************************************************************************/
408/*
409 * Note that 'init' is a special process: it doesn't get signals it doesn't
410 * want to handle. Thus you cannot kill init even with a SIGKILL even by
411 * mistake.
412 */
David Howellsa411aee2006-01-18 17:43:59 -0800413static void do_signal(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
415 struct k_sigaction ka;
416 siginfo_t info;
417 int signr;
418
David Howellsfef2b582006-01-06 00:11:45 -0800419 signr = get_signal_to_deliver(&info, &ka, __frame, NULL);
David Howellsa411aee2006-01-18 17:43:59 -0800420 if (signr > 0) {
Al Viroa610d6e2012-05-21 23:42:15 -0400421 handle_signal(signr, &info, &ka);
David Howellsa411aee2006-01-18 17:43:59 -0800422 return;
423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 /* Did we come from a system call? */
Roel Kluinc896a2e2009-10-26 16:50:23 -0700426 if (__frame->syscallno != -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 /* Restart the system call - no handlers present */
David Howellsa411aee2006-01-18 17:43:59 -0800428 switch (__frame->gr8) {
429 case -ERESTARTNOHAND:
430 case -ERESTARTSYS:
431 case -ERESTARTNOINTR:
David Howellsfef2b582006-01-06 00:11:45 -0800432 __frame->gr8 = __frame->orig_gr8;
433 __frame->pc -= 4;
David Howellsa411aee2006-01-18 17:43:59 -0800434 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
David Howellsa411aee2006-01-18 17:43:59 -0800436 case -ERESTART_RESTARTBLOCK:
Al Viro44c7aff2010-09-20 15:13:19 +0100437 __frame->gr7 = __NR_restart_syscall;
David Howellsfef2b582006-01-06 00:11:45 -0800438 __frame->pc -= 4;
David Howellsa411aee2006-01-18 17:43:59 -0800439 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 }
Al Viroed1cde62010-09-20 15:13:25 +0100441 __frame->syscallno = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 }
443
David Howellsa411aee2006-01-18 17:43:59 -0800444 /* if there's no signal to deliver, we just put the saved sigmask
445 * back */
Al Viro51a7b442012-05-21 23:33:55 -0400446 restore_saved_sigmask();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447} /* end do_signal() */
448
449/*****************************************************************************/
450/*
451 * notification of userspace execution resumption
David Howellsa411aee2006-01-18 17:43:59 -0800452 * - triggered by the TIF_WORK_MASK flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 */
454asmlinkage void do_notify_resume(__u32 thread_info_flags)
455{
456 /* pending single-step? */
457 if (thread_info_flags & _TIF_SINGLESTEP)
458 clear_thread_flag(TIF_SINGLESTEP);
459
460 /* deal with pending signal delivery */
Geert Uytterhoevena3936242012-06-02 13:08:39 +0200461 if (thread_info_flags & _TIF_SIGPENDING)
David Howellsa411aee2006-01-18 17:43:59 -0800462 do_signal();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
David Howellsb7bab882009-06-11 13:05:14 +0100464 /* deal with notification on about to resume userspace execution */
465 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
466 clear_thread_flag(TIF_NOTIFY_RESUME);
David Howells4a3b9892009-06-11 13:05:24 +0100467 tracehook_notify_resume(__frame);
David Howellsb7bab882009-06-11 13:05:14 +0100468 }
469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470} /* end do_notify_resume() */