cris: use set_current_blocked() and block_sigmask()
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / arch / mn10300 / kernel / signal.c
CommitLineData
b920de1b
DH
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>
b920de1b
DH
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>
5d289964 25#include <linux/tracehook.h>
b920de1b
DH
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 */
39asmlinkage long sys_sigsuspend(int history0, int history1, old_sigset_t mask)
40{
68f3f16d
AV
41 sigset_t blocked;
42 siginitset(&blocked, mask);
43 return sigsuspend(&blocked);
b920de1b
DH
44}
45
46/*
47 * set signal action syscall
48 */
49asmlinkage 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) ||
60bdb72e
AV
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))
b920de1b 63 return -EFAULT;
b920de1b
DH
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) ||
60bdb72e
AV
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))
b920de1b 75 return -EFAULT;
b920de1b
DH
76 }
77
78 return ret;
79}
80
81/*
82 * set alternate signal stack syscall
83 */
84asmlinkage long sys_sigaltstack(const stack_t __user *uss, stack_t *uoss)
85{
7c7fcf76 86 return do_sigaltstack(uss, uoss, current_frame()->sp);
b920de1b
DH
87}
88
89/*
90 * do a signal return; undo the signal stack.
91 */
92static int restore_sigcontext(struct pt_regs *regs,
93 struct sigcontext __user *sc, long *_d0)
94{
95 unsigned int err = 0;
96
c05628b4
AV
97 /* Always make any pending restarted system calls return -EINTR */
98 current_thread_info()->restart_block.fn = do_no_restart_syscall;
99
b920de1b
DH
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
142badframe:
143 return 1;
144}
145
146/*
147 * standard signal return syscall
148 */
149asmlinkage long sys_sigreturn(void)
150{
7c7fcf76 151 struct sigframe __user *frame;
b920de1b
DH
152 sigset_t set;
153 long d0;
154
7c7fcf76 155 frame = (struct sigframe __user *) current_frame()->sp;
b920de1b
DH
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 spin_lock_irq(&current->sighand->siglock);
168 current->blocked = set;
169 recalc_sigpending();
170 spin_unlock_irq(&current->sighand->siglock);
171
7c7fcf76 172 if (restore_sigcontext(current_frame(), &frame->sc, &d0))
b920de1b
DH
173 goto badframe;
174
175 return d0;
176
177badframe:
178 force_sig(SIGSEGV, current);
179 return 0;
180}
181
182/*
183 * realtime signal return syscall
184 */
185asmlinkage long sys_rt_sigreturn(void)
186{
7c7fcf76 187 struct rt_sigframe __user *frame;
b920de1b 188 sigset_t set;
7c7fcf76 189 long d0;
b920de1b 190
7c7fcf76 191 frame = (struct rt_sigframe __user *) current_frame()->sp;
b920de1b
DH
192 if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
193 goto badframe;
194 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
195 goto badframe;
196
197 sigdelsetmask(&set, ~_BLOCKABLE);
198 spin_lock_irq(&current->sighand->siglock);
199 current->blocked = set;
200 recalc_sigpending();
201 spin_unlock_irq(&current->sighand->siglock);
202
7c7fcf76 203 if (restore_sigcontext(current_frame(), &frame->uc.uc_mcontext, &d0))
b920de1b
DH
204 goto badframe;
205
7c7fcf76
DH
206 if (do_sigaltstack(&frame->uc.uc_stack, NULL, current_frame()->sp) ==
207 -EFAULT)
b920de1b
DH
208 goto badframe;
209
210 return d0;
211
212badframe:
213 force_sig(SIGSEGV, current);
214 return 0;
215}
216
217/*
218 * store the userspace context into a signal frame
219 */
220static int setup_sigcontext(struct sigcontext __user *sc,
221 struct fpucontext *fpuctx,
222 struct pt_regs *regs,
223 unsigned long mask)
224{
225 int tmp, err = 0;
226
227#define COPY(x) err |= __put_user(regs->x, &sc->x)
228 COPY(d0); COPY(d1); COPY(d2); COPY(d3);
229 COPY(a0); COPY(a1); COPY(a2); COPY(a3);
230 COPY(e0); COPY(e1); COPY(e2); COPY(e3);
231 COPY(e4); COPY(e5); COPY(e6); COPY(e7);
232 COPY(lar); COPY(lir);
233 COPY(mdr); COPY(mdrq);
234 COPY(mcvf); COPY(mcrl); COPY(mcrh);
235 COPY(sp); COPY(epsw); COPY(pc);
236#undef COPY
237
238 tmp = fpu_setup_sigcontext(fpuctx);
239 if (tmp < 0)
240 err = 1;
241 else
242 err |= __put_user(tmp ? fpuctx : NULL, &sc->fpucontext);
243
244 /* non-iBCS2 extensions.. */
245 err |= __put_user(mask, &sc->oldmask);
246
247 return err;
248}
249
250/*
251 * determine which stack to use..
252 */
253static inline void __user *get_sigframe(struct k_sigaction *ka,
254 struct pt_regs *regs,
255 size_t frame_size)
256{
257 unsigned long sp;
258
259 /* default to using normal stack */
260 sp = regs->sp;
261
262 /* this is the X/Open sanctioned signal stack switching. */
263 if (ka->sa.sa_flags & SA_ONSTACK) {
4c03ee73 264 if (sas_ss_flags(sp) == 0)
b920de1b
DH
265 sp = current->sas_ss_sp + current->sas_ss_size;
266 }
267
268 return (void __user *) ((sp - frame_size) & ~7UL);
269}
270
271/*
272 * set up a normal signal frame
273 */
274static int setup_frame(int sig, struct k_sigaction *ka, sigset_t *set,
275 struct pt_regs *regs)
276{
277 struct sigframe __user *frame;
278 int rsig;
279
280 frame = get_sigframe(ka, regs, sizeof(*frame));
281
282 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
283 goto give_sigsegv;
284
285 rsig = sig;
286 if (sig < 32 &&
287 current_thread_info()->exec_domain &&
288 current_thread_info()->exec_domain->signal_invmap)
289 rsig = current_thread_info()->exec_domain->signal_invmap[sig];
290
291 if (__put_user(rsig, &frame->sig) < 0 ||
292 __put_user(&frame->sc, &frame->psc) < 0)
293 goto give_sigsegv;
294
295 if (setup_sigcontext(&frame->sc, &frame->fpuctx, regs, set->sig[0]))
296 goto give_sigsegv;
297
298 if (_NSIG_WORDS > 1) {
299 if (__copy_to_user(frame->extramask, &set->sig[1],
300 sizeof(frame->extramask)))
301 goto give_sigsegv;
302 }
303
304 /* set up to return from userspace. If provided, use a stub already in
305 * userspace */
306 if (ka->sa.sa_flags & SA_RESTORER) {
307 if (__put_user(ka->sa.sa_restorer, &frame->pretcode))
308 goto give_sigsegv;
309 } else {
310 if (__put_user((void (*)(void))frame->retcode,
311 &frame->pretcode))
312 goto give_sigsegv;
313 /* this is mov $,d0; syscall 0 */
314 if (__put_user(0x2c, (char *)(frame->retcode + 0)) ||
315 __put_user(__NR_sigreturn, (char *)(frame->retcode + 1)) ||
316 __put_user(0x00, (char *)(frame->retcode + 2)) ||
317 __put_user(0xf0, (char *)(frame->retcode + 3)) ||
318 __put_user(0xe0, (char *)(frame->retcode + 4)))
319 goto give_sigsegv;
320 flush_icache_range((unsigned long) frame->retcode,
321 (unsigned long) frame->retcode + 5);
322 }
323
324 /* set up registers for signal handler */
325 regs->sp = (unsigned long) frame;
326 regs->pc = (unsigned long) ka->sa.sa_handler;
327 regs->d0 = sig;
328 regs->d1 = (unsigned long) &frame->sc;
329
b920de1b
DH
330 /* the tracer may want to single-step inside the handler */
331 if (test_thread_flag(TIF_SINGLESTEP))
332 ptrace_notify(SIGTRAP);
333
334#if DEBUG_SIG
335 printk(KERN_DEBUG "SIG deliver %d (%s:%d): sp=%p pc=%lx ra=%p\n",
336 sig, current->comm, current->pid, frame, regs->pc,
337 frame->pretcode);
338#endif
339
340 return 0;
341
342give_sigsegv:
e46924d2 343 force_sigsegv(sig, current);
b920de1b
DH
344 return -EFAULT;
345}
346
347/*
348 * set up a realtime signal frame
349 */
350static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
351 sigset_t *set, struct pt_regs *regs)
352{
353 struct rt_sigframe __user *frame;
354 int rsig;
355
356 frame = get_sigframe(ka, regs, sizeof(*frame));
357
358 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
359 goto give_sigsegv;
360
361 rsig = sig;
362 if (sig < 32 &&
363 current_thread_info()->exec_domain &&
364 current_thread_info()->exec_domain->signal_invmap)
365 rsig = current_thread_info()->exec_domain->signal_invmap[sig];
366
367 if (__put_user(rsig, &frame->sig) ||
368 __put_user(&frame->info, &frame->pinfo) ||
369 __put_user(&frame->uc, &frame->puc) ||
370 copy_siginfo_to_user(&frame->info, info))
371 goto give_sigsegv;
372
373 /* create the ucontext. */
374 if (__put_user(0, &frame->uc.uc_flags) ||
375 __put_user(0, &frame->uc.uc_link) ||
376 __put_user((void *)current->sas_ss_sp, &frame->uc.uc_stack.ss_sp) ||
377 __put_user(sas_ss_flags(regs->sp), &frame->uc.uc_stack.ss_flags) ||
378 __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size) ||
379 setup_sigcontext(&frame->uc.uc_mcontext,
380 &frame->fpuctx, regs, set->sig[0]) ||
381 __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)))
382 goto give_sigsegv;
383
384 /* set up to return from userspace. If provided, use a stub already in
385 * userspace */
386 if (ka->sa.sa_flags & SA_RESTORER) {
387 if (__put_user(ka->sa.sa_restorer, &frame->pretcode))
388 goto give_sigsegv;
389 } else {
390 if (__put_user((void(*)(void))frame->retcode,
391 &frame->pretcode) ||
392 /* This is mov $,d0; syscall 0 */
393 __put_user(0x2c, (char *)(frame->retcode + 0)) ||
394 __put_user(__NR_rt_sigreturn,
395 (char *)(frame->retcode + 1)) ||
396 __put_user(0x00, (char *)(frame->retcode + 2)) ||
397 __put_user(0xf0, (char *)(frame->retcode + 3)) ||
398 __put_user(0xe0, (char *)(frame->retcode + 4)))
399 goto give_sigsegv;
400
401 flush_icache_range((u_long) frame->retcode,
402 (u_long) frame->retcode + 5);
403 }
404
405 /* Set up registers for signal handler */
406 regs->sp = (unsigned long) frame;
407 regs->pc = (unsigned long) ka->sa.sa_handler;
408 regs->d0 = sig;
409 regs->d1 = (long) &frame->info;
410
b920de1b
DH
411 /* the tracer may want to single-step inside the handler */
412 if (test_thread_flag(TIF_SINGLESTEP))
413 ptrace_notify(SIGTRAP);
414
415#if DEBUG_SIG
416 printk(KERN_DEBUG "SIG deliver %d (%s:%d): sp=%p pc=%lx ra=%p\n",
417 sig, current->comm, current->pid, frame, regs->pc,
418 frame->pretcode);
419#endif
420
421 return 0;
422
423give_sigsegv:
e46924d2 424 force_sigsegv(sig, current);
b920de1b
DH
425 return -EFAULT;
426}
427
00cbf608
AV
428static inline void stepback(struct pt_regs *regs)
429{
430 regs->pc -= 2;
431 regs->orig_d0 = -1;
432}
433
b920de1b
DH
434/*
435 * handle the actual delivery of a signal to userspace
436 */
437static int handle_signal(int sig,
438 siginfo_t *info, struct k_sigaction *ka,
439 sigset_t *oldset, struct pt_regs *regs)
440{
441 int ret;
442
443 /* Are we from a system call? */
444 if (regs->orig_d0 >= 0) {
445 /* If so, check system call restarting.. */
446 switch (regs->d0) {
447 case -ERESTART_RESTARTBLOCK:
448 case -ERESTARTNOHAND:
449 regs->d0 = -EINTR;
450 break;
451
452 case -ERESTARTSYS:
453 if (!(ka->sa.sa_flags & SA_RESTART)) {
454 regs->d0 = -EINTR;
455 break;
456 }
457
458 /* fallthrough */
459 case -ERESTARTNOINTR:
460 regs->d0 = regs->orig_d0;
00cbf608 461 stepback(regs);
b920de1b
DH
462 }
463 }
464
465 /* Set up the stack frame */
466 if (ka->sa.sa_flags & SA_SIGINFO)
467 ret = setup_rt_frame(sig, ka, info, oldset, regs);
468 else
469 ret = setup_frame(sig, ka, oldset, regs);
470
471 if (ret == 0) {
472 spin_lock_irq(&current->sighand->siglock);
473 sigorsets(&current->blocked, &current->blocked,
474 &ka->sa.sa_mask);
475 if (!(ka->sa.sa_flags & SA_NODEFER))
476 sigaddset(&current->blocked, sig);
477 recalc_sigpending();
478 spin_unlock_irq(&current->sighand->siglock);
479 }
480
481 return ret;
482}
483
484/*
485 * handle a potential signal
486 */
487static void do_signal(struct pt_regs *regs)
488{
489 struct k_sigaction ka;
490 siginfo_t info;
491 sigset_t *oldset;
492 int signr;
493
494 /* we want the common case to go fast, which is why we may in certain
495 * cases get here from kernel mode */
496 if (!user_mode(regs))
497 return;
498
499 if (test_thread_flag(TIF_RESTORE_SIGMASK))
500 oldset = &current->saved_sigmask;
501 else
502 oldset = &current->blocked;
503
504 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
505 if (signr > 0) {
506 if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
507 /* a signal was successfully delivered; the saved
508 * sigmask will have been stored in the signal frame,
509 * and will be restored by sigreturn, so we can simply
510 * clear the TIF_RESTORE_SIGMASK flag */
511 if (test_thread_flag(TIF_RESTORE_SIGMASK))
512 clear_thread_flag(TIF_RESTORE_SIGMASK);
5d289964
DH
513
514 tracehook_signal_handler(signr, &info, &ka, regs,
515 test_thread_flag(TIF_SINGLESTEP));
b920de1b
DH
516 }
517
518 return;
519 }
520
521 /* did we come from a system call? */
522 if (regs->orig_d0 >= 0) {
523 /* restart the system call - no handlers present */
524 switch (regs->d0) {
525 case -ERESTARTNOHAND:
526 case -ERESTARTSYS:
527 case -ERESTARTNOINTR:
528 regs->d0 = regs->orig_d0;
00cbf608 529 stepback(regs);
b920de1b
DH
530 break;
531
532 case -ERESTART_RESTARTBLOCK:
533 regs->d0 = __NR_restart_syscall;
00cbf608 534 stepback(regs);
b920de1b
DH
535 break;
536 }
537 }
538
539 /* if there's no signal to deliver, we just put the saved sigmask
540 * back */
541 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
542 clear_thread_flag(TIF_RESTORE_SIGMASK);
543 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
544 }
545}
546
547/*
548 * notification of userspace execution resumption
549 * - triggered by current->work.notify_resume
550 */
551asmlinkage void do_notify_resume(struct pt_regs *regs, u32 thread_info_flags)
552{
553 /* Pending single-step? */
554 if (thread_info_flags & _TIF_SINGLESTEP) {
555#ifndef CONFIG_MN10300_USING_JTAG
556 regs->epsw |= EPSW_T;
557 clear_thread_flag(TIF_SINGLESTEP);
558#else
559 BUG(); /* no h/w single-step if using JTAG unit */
560#endif
561 }
562
563 /* deal with pending signal delivery */
564 if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
565 do_signal(regs);
5d289964
DH
566
567 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
568 clear_thread_flag(TIF_NOTIFY_RESUME);
7c7fcf76 569 tracehook_notify_resume(current_frame());
ee18d64c
DH
570 if (current->replacement_session_keyring)
571 key_replace_session_keyring();
5d289964 572 }
b920de1b 573}