new helper: restore_saved_sigmask()
[platform/adaptation/renesas_rcar/renesas_kernel.git] / arch / mn10300 / kernel / signal.c
1 /* MN10300 Signal handling
2  *
3  * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4  * Written by David Howells (dhowells@redhat.com)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public Licence
8  * as published by the Free Software Foundation; either version
9  * 2 of the Licence, or (at your option) any later version.
10  */
11
12 #include <linux/sched.h>
13 #include <linux/mm.h>
14 #include <linux/smp.h>
15 #include <linux/kernel.h>
16 #include <linux/signal.h>
17 #include <linux/errno.h>
18 #include <linux/wait.h>
19 #include <linux/ptrace.h>
20 #include <linux/unistd.h>
21 #include <linux/stddef.h>
22 #include <linux/tty.h>
23 #include <linux/personality.h>
24 #include <linux/suspend.h>
25 #include <linux/tracehook.h>
26 #include <asm/cacheflush.h>
27 #include <asm/ucontext.h>
28 #include <asm/uaccess.h>
29 #include <asm/fpu.h>
30 #include "sigframe.h"
31
32 #define DEBUG_SIG 0
33
34 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
35
36 /*
37  * atomically swap in the new signal mask, and wait for a signal.
38  */
39 asmlinkage long sys_sigsuspend(int history0, int history1, old_sigset_t mask)
40 {
41         sigset_t blocked;
42         siginitset(&blocked, mask);
43         return sigsuspend(&blocked);
44 }
45
46 /*
47  * set signal action syscall
48  */
49 asmlinkage long sys_sigaction(int sig,
50                               const struct old_sigaction __user *act,
51                               struct old_sigaction __user *oact)
52 {
53         struct k_sigaction new_ka, old_ka;
54         int ret;
55
56         if (act) {
57                 old_sigset_t mask;
58                 if (verify_area(VERIFY_READ, act, sizeof(*act)) ||
59                     __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
60                     __get_user(new_ka.sa.sa_restorer, &act->sa_restorer) ||
61                     __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
62                     __get_user(mask, &act->sa_mask))
63                         return -EFAULT;
64                 siginitset(&new_ka.sa.sa_mask, mask);
65         }
66
67         ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
68
69         if (!ret && oact) {
70                 if (verify_area(VERIFY_WRITE, oact, sizeof(*oact)) ||
71                     __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
72                     __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer) ||
73                     __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
74                     __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask))
75                         return -EFAULT;
76         }
77
78         return ret;
79 }
80
81 /*
82  * set alternate signal stack syscall
83  */
84 asmlinkage long sys_sigaltstack(const stack_t __user *uss, stack_t *uoss)
85 {
86         return do_sigaltstack(uss, uoss, current_frame()->sp);
87 }
88
89 /*
90  * do a signal return; undo the signal stack.
91  */
92 static int restore_sigcontext(struct pt_regs *regs,
93                               struct sigcontext __user *sc, long *_d0)
94 {
95         unsigned int err = 0;
96
97         /* Always make any pending restarted system calls return -EINTR */
98         current_thread_info()->restart_block.fn = do_no_restart_syscall;
99
100         if (is_using_fpu(current))
101                 fpu_kill_state(current);
102
103 #define COPY(x) err |= __get_user(regs->x, &sc->x)
104         COPY(d1); COPY(d2); COPY(d3);
105         COPY(a0); COPY(a1); COPY(a2); COPY(a3);
106         COPY(e0); COPY(e1); COPY(e2); COPY(e3);
107         COPY(e4); COPY(e5); COPY(e6); COPY(e7);
108         COPY(lar); COPY(lir);
109         COPY(mdr); COPY(mdrq);
110         COPY(mcvf); COPY(mcrl); COPY(mcrh);
111         COPY(sp); COPY(pc);
112 #undef COPY
113
114         {
115                 unsigned int tmpflags;
116 #ifndef CONFIG_MN10300_USING_JTAG
117 #define USER_EPSW (EPSW_FLAG_Z | EPSW_FLAG_N | EPSW_FLAG_C | EPSW_FLAG_V | \
118                    EPSW_T | EPSW_nAR)
119 #else
120 #define USER_EPSW (EPSW_FLAG_Z | EPSW_FLAG_N | EPSW_FLAG_C | EPSW_FLAG_V | \
121                    EPSW_nAR)
122 #endif
123                 err |= __get_user(tmpflags, &sc->epsw);
124                 regs->epsw = (regs->epsw & ~USER_EPSW) |
125                   (tmpflags & USER_EPSW);
126                 regs->orig_d0 = -1;             /* disable syscall checks */
127         }
128
129         {
130                 struct fpucontext *buf;
131                 err |= __get_user(buf, &sc->fpucontext);
132                 if (buf) {
133                         if (verify_area(VERIFY_READ, buf, sizeof(*buf)))
134                                 goto badframe;
135                         err |= fpu_restore_sigcontext(buf);
136                 }
137         }
138
139         err |= __get_user(*_d0, &sc->d0);
140         return err;
141
142 badframe:
143         return 1;
144 }
145
146 /*
147  * standard signal return syscall
148  */
149 asmlinkage long sys_sigreturn(void)
150 {
151         struct sigframe __user *frame;
152         sigset_t set;
153         long d0;
154
155         frame = (struct sigframe __user *) current_frame()->sp;
156         if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
157                 goto badframe;
158         if (__get_user(set.sig[0], &frame->sc.oldmask))
159                 goto badframe;
160
161         if (_NSIG_WORDS > 1 &&
162             __copy_from_user(&set.sig[1], &frame->extramask,
163                              sizeof(frame->extramask)))
164                 goto badframe;
165
166         sigdelsetmask(&set, ~_BLOCKABLE);
167         set_current_blocked(&set);
168
169         if (restore_sigcontext(current_frame(), &frame->sc, &d0))
170                 goto badframe;
171
172         return d0;
173
174 badframe:
175         force_sig(SIGSEGV, current);
176         return 0;
177 }
178
179 /*
180  * realtime signal return syscall
181  */
182 asmlinkage long sys_rt_sigreturn(void)
183 {
184         struct rt_sigframe __user *frame;
185         sigset_t set;
186         long d0;
187
188         frame = (struct rt_sigframe __user *) current_frame()->sp;
189         if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
190                 goto badframe;
191         if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
192                 goto badframe;
193
194         sigdelsetmask(&set, ~_BLOCKABLE);
195         set_current_blocked(&set);
196
197         if (restore_sigcontext(current_frame(), &frame->uc.uc_mcontext, &d0))
198                 goto badframe;
199
200         if (do_sigaltstack(&frame->uc.uc_stack, NULL, current_frame()->sp) ==
201             -EFAULT)
202                 goto badframe;
203
204         return d0;
205
206 badframe:
207         force_sig(SIGSEGV, current);
208         return 0;
209 }
210
211 /*
212  * store the userspace context into a signal frame
213  */
214 static int setup_sigcontext(struct sigcontext __user *sc,
215                             struct fpucontext *fpuctx,
216                             struct pt_regs *regs,
217                             unsigned long mask)
218 {
219         int tmp, err = 0;
220
221 #define COPY(x) err |= __put_user(regs->x, &sc->x)
222         COPY(d0); COPY(d1); COPY(d2); COPY(d3);
223         COPY(a0); COPY(a1); COPY(a2); COPY(a3);
224         COPY(e0); COPY(e1); COPY(e2); COPY(e3);
225         COPY(e4); COPY(e5); COPY(e6); COPY(e7);
226         COPY(lar); COPY(lir);
227         COPY(mdr); COPY(mdrq);
228         COPY(mcvf); COPY(mcrl); COPY(mcrh);
229         COPY(sp); COPY(epsw); COPY(pc);
230 #undef COPY
231
232         tmp = fpu_setup_sigcontext(fpuctx);
233         if (tmp < 0)
234                 err = 1;
235         else
236                 err |= __put_user(tmp ? fpuctx : NULL, &sc->fpucontext);
237
238         /* non-iBCS2 extensions.. */
239         err |= __put_user(mask, &sc->oldmask);
240
241         return err;
242 }
243
244 /*
245  * determine which stack to use..
246  */
247 static inline void __user *get_sigframe(struct k_sigaction *ka,
248                                         struct pt_regs *regs,
249                                         size_t frame_size)
250 {
251         unsigned long sp;
252
253         /* default to using normal stack */
254         sp = regs->sp;
255
256         /* this is the X/Open sanctioned signal stack switching.  */
257         if (ka->sa.sa_flags & SA_ONSTACK) {
258                 if (sas_ss_flags(sp) == 0)
259                         sp = current->sas_ss_sp + current->sas_ss_size;
260         }
261
262         return (void __user *) ((sp - frame_size) & ~7UL);
263 }
264
265 /*
266  * set up a normal signal frame
267  */
268 static int setup_frame(int sig, struct k_sigaction *ka, sigset_t *set,
269                        struct pt_regs *regs)
270 {
271         struct sigframe __user *frame;
272         int rsig;
273
274         frame = get_sigframe(ka, regs, sizeof(*frame));
275
276         if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
277                 goto give_sigsegv;
278
279         rsig = sig;
280         if (sig < 32 &&
281             current_thread_info()->exec_domain &&
282             current_thread_info()->exec_domain->signal_invmap)
283                 rsig = current_thread_info()->exec_domain->signal_invmap[sig];
284
285         if (__put_user(rsig, &frame->sig) < 0 ||
286             __put_user(&frame->sc, &frame->psc) < 0)
287                 goto give_sigsegv;
288
289         if (setup_sigcontext(&frame->sc, &frame->fpuctx, regs, set->sig[0]))
290                 goto give_sigsegv;
291
292         if (_NSIG_WORDS > 1) {
293                 if (__copy_to_user(frame->extramask, &set->sig[1],
294                                    sizeof(frame->extramask)))
295                         goto give_sigsegv;
296         }
297
298         /* set up to return from userspace.  If provided, use a stub already in
299          * userspace */
300         if (ka->sa.sa_flags & SA_RESTORER) {
301                 if (__put_user(ka->sa.sa_restorer, &frame->pretcode))
302                         goto give_sigsegv;
303         } else {
304                 if (__put_user((void (*)(void))frame->retcode,
305                                &frame->pretcode))
306                         goto give_sigsegv;
307                 /* this is mov $,d0; syscall 0 */
308                 if (__put_user(0x2c, (char *)(frame->retcode + 0)) ||
309                     __put_user(__NR_sigreturn, (char *)(frame->retcode + 1)) ||
310                     __put_user(0x00, (char *)(frame->retcode + 2)) ||
311                     __put_user(0xf0, (char *)(frame->retcode + 3)) ||
312                     __put_user(0xe0, (char *)(frame->retcode + 4)))
313                         goto give_sigsegv;
314                 flush_icache_range((unsigned long) frame->retcode,
315                                    (unsigned long) frame->retcode + 5);
316         }
317
318         /* set up registers for signal handler */
319         regs->sp = (unsigned long) frame;
320         regs->pc = (unsigned long) ka->sa.sa_handler;
321         regs->d0 = sig;
322         regs->d1 = (unsigned long) &frame->sc;
323
324         /* the tracer may want to single-step inside the handler */
325         if (test_thread_flag(TIF_SINGLESTEP))
326                 ptrace_notify(SIGTRAP);
327
328 #if DEBUG_SIG
329         printk(KERN_DEBUG "SIG deliver %d (%s:%d): sp=%p pc=%lx ra=%p\n",
330                sig, current->comm, current->pid, frame, regs->pc,
331                frame->pretcode);
332 #endif
333
334         return 0;
335
336 give_sigsegv:
337         force_sigsegv(sig, current);
338         return -EFAULT;
339 }
340
341 /*
342  * set up a realtime signal frame
343  */
344 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
345                           sigset_t *set, struct pt_regs *regs)
346 {
347         struct rt_sigframe __user *frame;
348         int rsig;
349
350         frame = get_sigframe(ka, regs, sizeof(*frame));
351
352         if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
353                 goto give_sigsegv;
354
355         rsig = sig;
356         if (sig < 32 &&
357             current_thread_info()->exec_domain &&
358             current_thread_info()->exec_domain->signal_invmap)
359                 rsig = current_thread_info()->exec_domain->signal_invmap[sig];
360
361         if (__put_user(rsig, &frame->sig) ||
362             __put_user(&frame->info, &frame->pinfo) ||
363             __put_user(&frame->uc, &frame->puc) ||
364             copy_siginfo_to_user(&frame->info, info))
365                 goto give_sigsegv;
366
367         /* create the ucontext.  */
368         if (__put_user(0, &frame->uc.uc_flags) ||
369             __put_user(0, &frame->uc.uc_link) ||
370             __put_user((void *)current->sas_ss_sp, &frame->uc.uc_stack.ss_sp) ||
371             __put_user(sas_ss_flags(regs->sp), &frame->uc.uc_stack.ss_flags) ||
372             __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size) ||
373             setup_sigcontext(&frame->uc.uc_mcontext,
374                              &frame->fpuctx, regs, set->sig[0]) ||
375             __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)))
376                 goto give_sigsegv;
377
378         /* set up to return from userspace.  If provided, use a stub already in
379          * userspace */
380         if (ka->sa.sa_flags & SA_RESTORER) {
381                 if (__put_user(ka->sa.sa_restorer, &frame->pretcode))
382                         goto give_sigsegv;
383         } else {
384                 if (__put_user((void(*)(void))frame->retcode,
385                                &frame->pretcode) ||
386                     /* This is mov $,d0; syscall 0 */
387                     __put_user(0x2c, (char *)(frame->retcode + 0)) ||
388                     __put_user(__NR_rt_sigreturn,
389                                (char *)(frame->retcode + 1)) ||
390                     __put_user(0x00, (char *)(frame->retcode + 2)) ||
391                     __put_user(0xf0, (char *)(frame->retcode + 3)) ||
392                     __put_user(0xe0, (char *)(frame->retcode + 4)))
393                         goto give_sigsegv;
394
395                 flush_icache_range((u_long) frame->retcode,
396                                    (u_long) frame->retcode + 5);
397         }
398
399         /* Set up registers for signal handler */
400         regs->sp = (unsigned long) frame;
401         regs->pc = (unsigned long) ka->sa.sa_handler;
402         regs->d0 = sig;
403         regs->d1 = (long) &frame->info;
404
405         /* the tracer may want to single-step inside the handler */
406         if (test_thread_flag(TIF_SINGLESTEP))
407                 ptrace_notify(SIGTRAP);
408
409 #if DEBUG_SIG
410         printk(KERN_DEBUG "SIG deliver %d (%s:%d): sp=%p pc=%lx ra=%p\n",
411                sig, current->comm, current->pid, frame, regs->pc,
412                frame->pretcode);
413 #endif
414
415         return 0;
416
417 give_sigsegv:
418         force_sigsegv(sig, current);
419         return -EFAULT;
420 }
421
422 static inline void stepback(struct pt_regs *regs)
423 {
424         regs->pc -= 2;
425         regs->orig_d0 = -1;
426 }
427
428 /*
429  * handle the actual delivery of a signal to userspace
430  */
431 static int handle_signal(int sig,
432                          siginfo_t *info, struct k_sigaction *ka,
433                          sigset_t *oldset, struct pt_regs *regs)
434 {
435         int ret;
436
437         /* Are we from a system call? */
438         if (regs->orig_d0 >= 0) {
439                 /* If so, check system call restarting.. */
440                 switch (regs->d0) {
441                 case -ERESTART_RESTARTBLOCK:
442                 case -ERESTARTNOHAND:
443                         regs->d0 = -EINTR;
444                         break;
445
446                 case -ERESTARTSYS:
447                         if (!(ka->sa.sa_flags & SA_RESTART)) {
448                                 regs->d0 = -EINTR;
449                                 break;
450                         }
451
452                         /* fallthrough */
453                 case -ERESTARTNOINTR:
454                         regs->d0 = regs->orig_d0;
455                         stepback(regs);
456                 }
457         }
458
459         /* Set up the stack frame */
460         if (ka->sa.sa_flags & SA_SIGINFO)
461                 ret = setup_rt_frame(sig, ka, info, oldset, regs);
462         else
463                 ret = setup_frame(sig, ka, oldset, regs);
464
465         if (ret == 0)
466                 block_sigmask(ka, sig);
467
468         return ret;
469 }
470
471 /*
472  * handle a potential signal
473  */
474 static void do_signal(struct pt_regs *regs)
475 {
476         struct k_sigaction ka;
477         siginfo_t info;
478         sigset_t *oldset;
479         int signr;
480
481         /* we want the common case to go fast, which is why we may in certain
482          * cases get here from kernel mode */
483         if (!user_mode(regs))
484                 return;
485
486         if (test_thread_flag(TIF_RESTORE_SIGMASK))
487                 oldset = &current->saved_sigmask;
488         else
489                 oldset = &current->blocked;
490
491         signr = get_signal_to_deliver(&info, &ka, regs, NULL);
492         if (signr > 0) {
493                 if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
494                         /* a signal was successfully delivered; the saved
495                          * sigmask will have been stored in the signal frame,
496                          * and will be restored by sigreturn, so we can simply
497                          * clear the TIF_RESTORE_SIGMASK flag */
498                         if (test_thread_flag(TIF_RESTORE_SIGMASK))
499                                 clear_thread_flag(TIF_RESTORE_SIGMASK);
500
501                         tracehook_signal_handler(signr, &info, &ka, regs,
502                                                  test_thread_flag(TIF_SINGLESTEP));
503                 }
504
505                 return;
506         }
507
508         /* did we come from a system call? */
509         if (regs->orig_d0 >= 0) {
510                 /* restart the system call - no handlers present */
511                 switch (regs->d0) {
512                 case -ERESTARTNOHAND:
513                 case -ERESTARTSYS:
514                 case -ERESTARTNOINTR:
515                         regs->d0 = regs->orig_d0;
516                         stepback(regs);
517                         break;
518
519                 case -ERESTART_RESTARTBLOCK:
520                         regs->d0 = __NR_restart_syscall;
521                         stepback(regs);
522                         break;
523                 }
524         }
525
526         /* if there's no signal to deliver, we just put the saved sigmask
527          * back */
528         restore_saved_sigmask();
529 }
530
531 /*
532  * notification of userspace execution resumption
533  * - triggered by current->work.notify_resume
534  */
535 asmlinkage void do_notify_resume(struct pt_regs *regs, u32 thread_info_flags)
536 {
537         /* Pending single-step? */
538         if (thread_info_flags & _TIF_SINGLESTEP) {
539 #ifndef CONFIG_MN10300_USING_JTAG
540                 regs->epsw |= EPSW_T;
541                 clear_thread_flag(TIF_SINGLESTEP);
542 #else
543                 BUG(); /* no h/w single-step if using JTAG unit */
544 #endif
545         }
546
547         /* deal with pending signal delivery */
548         if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
549                 do_signal(regs);
550
551         if (thread_info_flags & _TIF_NOTIFY_RESUME) {
552                 clear_thread_flag(TIF_NOTIFY_RESUME);
553                 tracehook_notify_resume(current_frame());
554         }
555 }