1 /* MN10300 Signal handling
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
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.
12 #include <linux/sched.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>
34 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
37 * atomically swap in the new signal mask, and wait for a signal.
39 asmlinkage
long sys_sigsuspend(int history0
, int history1
, old_sigset_t mask
)
43 current
->saved_sigmask
= current
->blocked
;
46 siginitset(&blocked
, mask
);
47 set_current_blocked(&blocked
);
49 current
->state
= TASK_INTERRUPTIBLE
;
51 set_thread_flag(TIF_RESTORE_SIGMASK
);
52 return -ERESTARTNOHAND
;
56 * set signal action syscall
58 asmlinkage
long sys_sigaction(int sig
,
59 const struct old_sigaction __user
*act
,
60 struct old_sigaction __user
*oact
)
62 struct k_sigaction new_ka
, old_ka
;
67 if (verify_area(VERIFY_READ
, act
, sizeof(*act
)) ||
68 __get_user(new_ka
.sa
.sa_handler
, &act
->sa_handler
) ||
69 __get_user(new_ka
.sa
.sa_restorer
, &act
->sa_restorer
) ||
70 __get_user(new_ka
.sa
.sa_flags
, &act
->sa_flags
) ||
71 __get_user(mask
, &act
->sa_mask
))
73 siginitset(&new_ka
.sa
.sa_mask
, mask
);
76 ret
= do_sigaction(sig
, act
? &new_ka
: NULL
, oact
? &old_ka
: NULL
);
79 if (verify_area(VERIFY_WRITE
, oact
, sizeof(*oact
)) ||
80 __put_user(old_ka
.sa
.sa_handler
, &oact
->sa_handler
) ||
81 __put_user(old_ka
.sa
.sa_restorer
, &oact
->sa_restorer
) ||
82 __put_user(old_ka
.sa
.sa_flags
, &oact
->sa_flags
) ||
83 __put_user(old_ka
.sa
.sa_mask
.sig
[0], &oact
->sa_mask
))
91 * set alternate signal stack syscall
93 asmlinkage
long sys_sigaltstack(const stack_t __user
*uss
, stack_t
*uoss
)
95 return do_sigaltstack(uss
, uoss
, current_frame()->sp
);
99 * do a signal return; undo the signal stack.
101 static int restore_sigcontext(struct pt_regs
*regs
,
102 struct sigcontext __user
*sc
, long *_d0
)
104 unsigned int err
= 0;
106 /* Always make any pending restarted system calls return -EINTR */
107 current_thread_info()->restart_block
.fn
= do_no_restart_syscall
;
109 if (is_using_fpu(current
))
110 fpu_kill_state(current
);
112 #define COPY(x) err |= __get_user(regs->x, &sc->x)
113 COPY(d1
); COPY(d2
); COPY(d3
);
114 COPY(a0
); COPY(a1
); COPY(a2
); COPY(a3
);
115 COPY(e0
); COPY(e1
); COPY(e2
); COPY(e3
);
116 COPY(e4
); COPY(e5
); COPY(e6
); COPY(e7
);
117 COPY(lar
); COPY(lir
);
118 COPY(mdr
); COPY(mdrq
);
119 COPY(mcvf
); COPY(mcrl
); COPY(mcrh
);
124 unsigned int tmpflags
;
125 #ifndef CONFIG_MN10300_USING_JTAG
126 #define USER_EPSW (EPSW_FLAG_Z | EPSW_FLAG_N | EPSW_FLAG_C | EPSW_FLAG_V | \
129 #define USER_EPSW (EPSW_FLAG_Z | EPSW_FLAG_N | EPSW_FLAG_C | EPSW_FLAG_V | \
132 err
|= __get_user(tmpflags
, &sc
->epsw
);
133 regs
->epsw
= (regs
->epsw
& ~USER_EPSW
) |
134 (tmpflags
& USER_EPSW
);
135 regs
->orig_d0
= -1; /* disable syscall checks */
139 struct fpucontext
*buf
;
140 err
|= __get_user(buf
, &sc
->fpucontext
);
142 if (verify_area(VERIFY_READ
, buf
, sizeof(*buf
)))
144 err
|= fpu_restore_sigcontext(buf
);
148 err
|= __get_user(*_d0
, &sc
->d0
);
156 * standard signal return syscall
158 asmlinkage
long sys_sigreturn(void)
160 struct sigframe __user
*frame
;
164 frame
= (struct sigframe __user
*) current_frame()->sp
;
165 if (verify_area(VERIFY_READ
, frame
, sizeof(*frame
)))
167 if (__get_user(set
.sig
[0], &frame
->sc
.oldmask
))
170 if (_NSIG_WORDS
> 1 &&
171 __copy_from_user(&set
.sig
[1], &frame
->extramask
,
172 sizeof(frame
->extramask
)))
175 sigdelsetmask(&set
, ~_BLOCKABLE
);
176 set_current_blocked(&set
);
178 if (restore_sigcontext(current_frame(), &frame
->sc
, &d0
))
184 force_sig(SIGSEGV
, current
);
189 * realtime signal return syscall
191 asmlinkage
long sys_rt_sigreturn(void)
193 struct rt_sigframe __user
*frame
;
197 frame
= (struct rt_sigframe __user
*) current_frame()->sp
;
198 if (verify_area(VERIFY_READ
, frame
, sizeof(*frame
)))
200 if (__copy_from_user(&set
, &frame
->uc
.uc_sigmask
, sizeof(set
)))
203 sigdelsetmask(&set
, ~_BLOCKABLE
);
204 set_current_blocked(&set
);
206 if (restore_sigcontext(current_frame(), &frame
->uc
.uc_mcontext
, &d0
))
209 if (do_sigaltstack(&frame
->uc
.uc_stack
, NULL
, current_frame()->sp
) ==
216 force_sig(SIGSEGV
, current
);
221 * store the userspace context into a signal frame
223 static int setup_sigcontext(struct sigcontext __user
*sc
,
224 struct fpucontext
*fpuctx
,
225 struct pt_regs
*regs
,
230 #define COPY(x) err |= __put_user(regs->x, &sc->x)
231 COPY(d0
); COPY(d1
); COPY(d2
); COPY(d3
);
232 COPY(a0
); COPY(a1
); COPY(a2
); COPY(a3
);
233 COPY(e0
); COPY(e1
); COPY(e2
); COPY(e3
);
234 COPY(e4
); COPY(e5
); COPY(e6
); COPY(e7
);
235 COPY(lar
); COPY(lir
);
236 COPY(mdr
); COPY(mdrq
);
237 COPY(mcvf
); COPY(mcrl
); COPY(mcrh
);
238 COPY(sp
); COPY(epsw
); COPY(pc
);
241 tmp
= fpu_setup_sigcontext(fpuctx
);
245 err
|= __put_user(tmp
? fpuctx
: NULL
, &sc
->fpucontext
);
247 /* non-iBCS2 extensions.. */
248 err
|= __put_user(mask
, &sc
->oldmask
);
254 * determine which stack to use..
256 static inline void __user
*get_sigframe(struct k_sigaction
*ka
,
257 struct pt_regs
*regs
,
262 /* default to using normal stack */
265 /* this is the X/Open sanctioned signal stack switching. */
266 if (ka
->sa
.sa_flags
& SA_ONSTACK
) {
267 if (sas_ss_flags(sp
) == 0)
268 sp
= current
->sas_ss_sp
+ current
->sas_ss_size
;
271 return (void __user
*) ((sp
- frame_size
) & ~7UL);
275 * set up a normal signal frame
277 static int setup_frame(int sig
, struct k_sigaction
*ka
, sigset_t
*set
,
278 struct pt_regs
*regs
)
280 struct sigframe __user
*frame
;
283 frame
= get_sigframe(ka
, regs
, sizeof(*frame
));
285 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
)))
290 current_thread_info()->exec_domain
&&
291 current_thread_info()->exec_domain
->signal_invmap
)
292 rsig
= current_thread_info()->exec_domain
->signal_invmap
[sig
];
294 if (__put_user(rsig
, &frame
->sig
) < 0 ||
295 __put_user(&frame
->sc
, &frame
->psc
) < 0)
298 if (setup_sigcontext(&frame
->sc
, &frame
->fpuctx
, regs
, set
->sig
[0]))
301 if (_NSIG_WORDS
> 1) {
302 if (__copy_to_user(frame
->extramask
, &set
->sig
[1],
303 sizeof(frame
->extramask
)))
307 /* set up to return from userspace. If provided, use a stub already in
309 if (ka
->sa
.sa_flags
& SA_RESTORER
) {
310 if (__put_user(ka
->sa
.sa_restorer
, &frame
->pretcode
))
313 if (__put_user((void (*)(void))frame
->retcode
,
316 /* this is mov $,d0; syscall 0 */
317 if (__put_user(0x2c, (char *)(frame
->retcode
+ 0)) ||
318 __put_user(__NR_sigreturn
, (char *)(frame
->retcode
+ 1)) ||
319 __put_user(0x00, (char *)(frame
->retcode
+ 2)) ||
320 __put_user(0xf0, (char *)(frame
->retcode
+ 3)) ||
321 __put_user(0xe0, (char *)(frame
->retcode
+ 4)))
323 flush_icache_range((unsigned long) frame
->retcode
,
324 (unsigned long) frame
->retcode
+ 5);
327 /* set up registers for signal handler */
328 regs
->sp
= (unsigned long) frame
;
329 regs
->pc
= (unsigned long) ka
->sa
.sa_handler
;
331 regs
->d1
= (unsigned long) &frame
->sc
;
333 /* the tracer may want to single-step inside the handler */
334 if (test_thread_flag(TIF_SINGLESTEP
))
335 ptrace_notify(SIGTRAP
);
338 printk(KERN_DEBUG
"SIG deliver %d (%s:%d): sp=%p pc=%lx ra=%p\n",
339 sig
, current
->comm
, current
->pid
, frame
, regs
->pc
,
346 force_sigsegv(sig
, current
);
351 * set up a realtime signal frame
353 static int setup_rt_frame(int sig
, struct k_sigaction
*ka
, siginfo_t
*info
,
354 sigset_t
*set
, struct pt_regs
*regs
)
356 struct rt_sigframe __user
*frame
;
359 frame
= get_sigframe(ka
, regs
, sizeof(*frame
));
361 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
)))
366 current_thread_info()->exec_domain
&&
367 current_thread_info()->exec_domain
->signal_invmap
)
368 rsig
= current_thread_info()->exec_domain
->signal_invmap
[sig
];
370 if (__put_user(rsig
, &frame
->sig
) ||
371 __put_user(&frame
->info
, &frame
->pinfo
) ||
372 __put_user(&frame
->uc
, &frame
->puc
) ||
373 copy_siginfo_to_user(&frame
->info
, info
))
376 /* create the ucontext. */
377 if (__put_user(0, &frame
->uc
.uc_flags
) ||
378 __put_user(0, &frame
->uc
.uc_link
) ||
379 __put_user((void *)current
->sas_ss_sp
, &frame
->uc
.uc_stack
.ss_sp
) ||
380 __put_user(sas_ss_flags(regs
->sp
), &frame
->uc
.uc_stack
.ss_flags
) ||
381 __put_user(current
->sas_ss_size
, &frame
->uc
.uc_stack
.ss_size
) ||
382 setup_sigcontext(&frame
->uc
.uc_mcontext
,
383 &frame
->fpuctx
, regs
, set
->sig
[0]) ||
384 __copy_to_user(&frame
->uc
.uc_sigmask
, set
, sizeof(*set
)))
387 /* set up to return from userspace. If provided, use a stub already in
389 if (ka
->sa
.sa_flags
& SA_RESTORER
) {
390 if (__put_user(ka
->sa
.sa_restorer
, &frame
->pretcode
))
393 if (__put_user((void(*)(void))frame
->retcode
,
395 /* This is mov $,d0; syscall 0 */
396 __put_user(0x2c, (char *)(frame
->retcode
+ 0)) ||
397 __put_user(__NR_rt_sigreturn
,
398 (char *)(frame
->retcode
+ 1)) ||
399 __put_user(0x00, (char *)(frame
->retcode
+ 2)) ||
400 __put_user(0xf0, (char *)(frame
->retcode
+ 3)) ||
401 __put_user(0xe0, (char *)(frame
->retcode
+ 4)))
404 flush_icache_range((u_long
) frame
->retcode
,
405 (u_long
) frame
->retcode
+ 5);
408 /* Set up registers for signal handler */
409 regs
->sp
= (unsigned long) frame
;
410 regs
->pc
= (unsigned long) ka
->sa
.sa_handler
;
412 regs
->d1
= (long) &frame
->info
;
414 /* the tracer may want to single-step inside the handler */
415 if (test_thread_flag(TIF_SINGLESTEP
))
416 ptrace_notify(SIGTRAP
);
419 printk(KERN_DEBUG
"SIG deliver %d (%s:%d): sp=%p pc=%lx ra=%p\n",
420 sig
, current
->comm
, current
->pid
, frame
, regs
->pc
,
427 force_sigsegv(sig
, current
);
431 static inline void stepback(struct pt_regs
*regs
)
438 * handle the actual delivery of a signal to userspace
440 static int handle_signal(int sig
,
441 siginfo_t
*info
, struct k_sigaction
*ka
,
442 sigset_t
*oldset
, struct pt_regs
*regs
)
446 /* Are we from a system call? */
447 if (regs
->orig_d0
>= 0) {
448 /* If so, check system call restarting.. */
450 case -ERESTART_RESTARTBLOCK
:
451 case -ERESTARTNOHAND
:
456 if (!(ka
->sa
.sa_flags
& SA_RESTART
)) {
462 case -ERESTARTNOINTR
:
463 regs
->d0
= regs
->orig_d0
;
468 /* Set up the stack frame */
469 if (ka
->sa
.sa_flags
& SA_SIGINFO
)
470 ret
= setup_rt_frame(sig
, ka
, info
, oldset
, regs
);
472 ret
= setup_frame(sig
, ka
, oldset
, regs
);
475 block_sigmask(ka
, sig
);
481 * handle a potential signal
483 static void do_signal(struct pt_regs
*regs
)
485 struct k_sigaction ka
;
490 /* we want the common case to go fast, which is why we may in certain
491 * cases get here from kernel mode */
492 if (!user_mode(regs
))
495 if (test_thread_flag(TIF_RESTORE_SIGMASK
))
496 oldset
= ¤t
->saved_sigmask
;
498 oldset
= ¤t
->blocked
;
500 signr
= get_signal_to_deliver(&info
, &ka
, regs
, NULL
);
502 if (handle_signal(signr
, &info
, &ka
, oldset
, regs
) == 0) {
503 /* a signal was successfully delivered; the saved
504 * sigmask will have been stored in the signal frame,
505 * and will be restored by sigreturn, so we can simply
506 * clear the TIF_RESTORE_SIGMASK flag */
507 if (test_thread_flag(TIF_RESTORE_SIGMASK
))
508 clear_thread_flag(TIF_RESTORE_SIGMASK
);
510 tracehook_signal_handler(signr
, &info
, &ka
, regs
,
511 test_thread_flag(TIF_SINGLESTEP
));
517 /* did we come from a system call? */
518 if (regs
->orig_d0
>= 0) {
519 /* restart the system call - no handlers present */
521 case -ERESTARTNOHAND
:
523 case -ERESTARTNOINTR
:
524 regs
->d0
= regs
->orig_d0
;
528 case -ERESTART_RESTARTBLOCK
:
529 regs
->d0
= __NR_restart_syscall
;
535 /* if there's no signal to deliver, we just put the saved sigmask
537 if (test_thread_flag(TIF_RESTORE_SIGMASK
)) {
538 clear_thread_flag(TIF_RESTORE_SIGMASK
);
539 sigprocmask(SIG_SETMASK
, ¤t
->saved_sigmask
, NULL
);
544 * notification of userspace execution resumption
545 * - triggered by current->work.notify_resume
547 asmlinkage
void do_notify_resume(struct pt_regs
*regs
, u32 thread_info_flags
)
549 /* Pending single-step? */
550 if (thread_info_flags
& _TIF_SINGLESTEP
) {
551 #ifndef CONFIG_MN10300_USING_JTAG
552 regs
->epsw
|= EPSW_T
;
553 clear_thread_flag(TIF_SINGLESTEP
);
555 BUG(); /* no h/w single-step if using JTAG unit */
559 /* deal with pending signal delivery */
560 if (thread_info_flags
& (_TIF_SIGPENDING
| _TIF_RESTORE_SIGMASK
))
563 if (thread_info_flags
& _TIF_NOTIFY_RESUME
) {
564 clear_thread_flag(TIF_NOTIFY_RESUME
);
565 tracehook_notify_resume(current_frame());
566 if (current
->replacement_session_keyring
)
567 key_replace_session_keyring();