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
)
42 siginitset(&blocked
, mask
);
43 return sigsuspend(&blocked
);
47 * set signal action syscall
49 asmlinkage
long sys_sigaction(int sig
,
50 const struct old_sigaction __user
*act
,
51 struct old_sigaction __user
*oact
)
53 struct k_sigaction new_ka
, old_ka
;
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
))
64 siginitset(&new_ka
.sa
.sa_mask
, mask
);
67 ret
= do_sigaction(sig
, act
? &new_ka
: NULL
, oact
? &old_ka
: NULL
);
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
))
82 * set alternate signal stack syscall
84 asmlinkage
long sys_sigaltstack(const stack_t __user
*uss
, stack_t
*uoss
)
86 return do_sigaltstack(uss
, uoss
, current_frame()->sp
);
90 * do a signal return; undo the signal stack.
92 static int restore_sigcontext(struct pt_regs
*regs
,
93 struct sigcontext __user
*sc
, long *_d0
)
97 /* Always make any pending restarted system calls return -EINTR */
98 current_thread_info()->restart_block
.fn
= do_no_restart_syscall
;
100 if (is_using_fpu(current
))
101 fpu_kill_state(current
);
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
);
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 | \
120 #define USER_EPSW (EPSW_FLAG_Z | EPSW_FLAG_N | EPSW_FLAG_C | EPSW_FLAG_V | \
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 */
130 struct fpucontext
*buf
;
131 err
|= __get_user(buf
, &sc
->fpucontext
);
133 if (verify_area(VERIFY_READ
, buf
, sizeof(*buf
)))
135 err
|= fpu_restore_sigcontext(buf
);
139 err
|= __get_user(*_d0
, &sc
->d0
);
147 * standard signal return syscall
149 asmlinkage
long sys_sigreturn(void)
151 struct sigframe __user
*frame
;
155 frame
= (struct sigframe __user
*) current_frame()->sp
;
156 if (verify_area(VERIFY_READ
, frame
, sizeof(*frame
)))
158 if (__get_user(set
.sig
[0], &frame
->sc
.oldmask
))
161 if (_NSIG_WORDS
> 1 &&
162 __copy_from_user(&set
.sig
[1], &frame
->extramask
,
163 sizeof(frame
->extramask
)))
166 sigdelsetmask(&set
, ~_BLOCKABLE
);
167 set_current_blocked(&set
);
169 if (restore_sigcontext(current_frame(), &frame
->sc
, &d0
))
175 force_sig(SIGSEGV
, current
);
180 * realtime signal return syscall
182 asmlinkage
long sys_rt_sigreturn(void)
184 struct rt_sigframe __user
*frame
;
188 frame
= (struct rt_sigframe __user
*) current_frame()->sp
;
189 if (verify_area(VERIFY_READ
, frame
, sizeof(*frame
)))
191 if (__copy_from_user(&set
, &frame
->uc
.uc_sigmask
, sizeof(set
)))
194 sigdelsetmask(&set
, ~_BLOCKABLE
);
195 set_current_blocked(&set
);
197 if (restore_sigcontext(current_frame(), &frame
->uc
.uc_mcontext
, &d0
))
200 if (do_sigaltstack(&frame
->uc
.uc_stack
, NULL
, current_frame()->sp
) ==
207 force_sig(SIGSEGV
, current
);
212 * store the userspace context into a signal frame
214 static int setup_sigcontext(struct sigcontext __user
*sc
,
215 struct fpucontext
*fpuctx
,
216 struct pt_regs
*regs
,
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
);
232 tmp
= fpu_setup_sigcontext(fpuctx
);
236 err
|= __put_user(tmp
? fpuctx
: NULL
, &sc
->fpucontext
);
238 /* non-iBCS2 extensions.. */
239 err
|= __put_user(mask
, &sc
->oldmask
);
245 * determine which stack to use..
247 static inline void __user
*get_sigframe(struct k_sigaction
*ka
,
248 struct pt_regs
*regs
,
253 /* default to using normal stack */
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
;
262 return (void __user
*) ((sp
- frame_size
) & ~7UL);
266 * set up a normal signal frame
268 static int setup_frame(int sig
, struct k_sigaction
*ka
, sigset_t
*set
,
269 struct pt_regs
*regs
)
271 struct sigframe __user
*frame
;
274 frame
= get_sigframe(ka
, regs
, sizeof(*frame
));
276 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
)))
281 current_thread_info()->exec_domain
&&
282 current_thread_info()->exec_domain
->signal_invmap
)
283 rsig
= current_thread_info()->exec_domain
->signal_invmap
[sig
];
285 if (__put_user(rsig
, &frame
->sig
) < 0 ||
286 __put_user(&frame
->sc
, &frame
->psc
) < 0)
289 if (setup_sigcontext(&frame
->sc
, &frame
->fpuctx
, regs
, set
->sig
[0]))
292 if (_NSIG_WORDS
> 1) {
293 if (__copy_to_user(frame
->extramask
, &set
->sig
[1],
294 sizeof(frame
->extramask
)))
298 /* set up to return from userspace. If provided, use a stub already in
300 if (ka
->sa
.sa_flags
& SA_RESTORER
) {
301 if (__put_user(ka
->sa
.sa_restorer
, &frame
->pretcode
))
304 if (__put_user((void (*)(void))frame
->retcode
,
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)))
314 flush_icache_range((unsigned long) frame
->retcode
,
315 (unsigned long) frame
->retcode
+ 5);
318 /* set up registers for signal handler */
319 regs
->sp
= (unsigned long) frame
;
320 regs
->pc
= (unsigned long) ka
->sa
.sa_handler
;
322 regs
->d1
= (unsigned long) &frame
->sc
;
324 /* the tracer may want to single-step inside the handler */
325 if (test_thread_flag(TIF_SINGLESTEP
))
326 ptrace_notify(SIGTRAP
);
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
,
337 force_sigsegv(sig
, current
);
342 * set up a realtime signal frame
344 static int setup_rt_frame(int sig
, struct k_sigaction
*ka
, siginfo_t
*info
,
345 sigset_t
*set
, struct pt_regs
*regs
)
347 struct rt_sigframe __user
*frame
;
350 frame
= get_sigframe(ka
, regs
, sizeof(*frame
));
352 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
)))
357 current_thread_info()->exec_domain
&&
358 current_thread_info()->exec_domain
->signal_invmap
)
359 rsig
= current_thread_info()->exec_domain
->signal_invmap
[sig
];
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
))
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
)))
378 /* set up to return from userspace. If provided, use a stub already in
380 if (ka
->sa
.sa_flags
& SA_RESTORER
) {
381 if (__put_user(ka
->sa
.sa_restorer
, &frame
->pretcode
))
384 if (__put_user((void(*)(void))frame
->retcode
,
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)))
395 flush_icache_range((u_long
) frame
->retcode
,
396 (u_long
) frame
->retcode
+ 5);
399 /* Set up registers for signal handler */
400 regs
->sp
= (unsigned long) frame
;
401 regs
->pc
= (unsigned long) ka
->sa
.sa_handler
;
403 regs
->d1
= (long) &frame
->info
;
405 /* the tracer may want to single-step inside the handler */
406 if (test_thread_flag(TIF_SINGLESTEP
))
407 ptrace_notify(SIGTRAP
);
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
,
418 force_sigsegv(sig
, current
);
422 static inline void stepback(struct pt_regs
*regs
)
429 * handle the actual delivery of a signal to userspace
431 static int handle_signal(int sig
,
432 siginfo_t
*info
, struct k_sigaction
*ka
,
433 sigset_t
*oldset
, struct pt_regs
*regs
)
437 /* Are we from a system call? */
438 if (regs
->orig_d0
>= 0) {
439 /* If so, check system call restarting.. */
441 case -ERESTART_RESTARTBLOCK
:
442 case -ERESTARTNOHAND
:
447 if (!(ka
->sa
.sa_flags
& SA_RESTART
)) {
453 case -ERESTARTNOINTR
:
454 regs
->d0
= regs
->orig_d0
;
459 /* Set up the stack frame */
460 if (ka
->sa
.sa_flags
& SA_SIGINFO
)
461 ret
= setup_rt_frame(sig
, ka
, info
, oldset
, regs
);
463 ret
= setup_frame(sig
, ka
, oldset
, regs
);
466 block_sigmask(ka
, sig
);
472 * handle a potential signal
474 static void do_signal(struct pt_regs
*regs
)
476 struct k_sigaction ka
;
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
))
486 if (test_thread_flag(TIF_RESTORE_SIGMASK
))
487 oldset
= ¤t
->saved_sigmask
;
489 oldset
= ¤t
->blocked
;
491 signr
= get_signal_to_deliver(&info
, &ka
, regs
, NULL
);
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
);
501 tracehook_signal_handler(signr
, &info
, &ka
, regs
,
502 test_thread_flag(TIF_SINGLESTEP
));
508 /* did we come from a system call? */
509 if (regs
->orig_d0
>= 0) {
510 /* restart the system call - no handlers present */
512 case -ERESTARTNOHAND
:
514 case -ERESTARTNOINTR
:
515 regs
->d0
= regs
->orig_d0
;
519 case -ERESTART_RESTARTBLOCK
:
520 regs
->d0
= __NR_restart_syscall
;
526 /* if there's no signal to deliver, we just put the saved sigmask
528 if (test_thread_flag(TIF_RESTORE_SIGMASK
)) {
529 clear_thread_flag(TIF_RESTORE_SIGMASK
);
530 sigprocmask(SIG_SETMASK
, ¤t
->saved_sigmask
, NULL
);
535 * notification of userspace execution resumption
536 * - triggered by current->work.notify_resume
538 asmlinkage
void do_notify_resume(struct pt_regs
*regs
, u32 thread_info_flags
)
540 /* Pending single-step? */
541 if (thread_info_flags
& _TIF_SINGLESTEP
) {
542 #ifndef CONFIG_MN10300_USING_JTAG
543 regs
->epsw
|= EPSW_T
;
544 clear_thread_flag(TIF_SINGLESTEP
);
546 BUG(); /* no h/w single-step if using JTAG unit */
550 /* deal with pending signal delivery */
551 if (thread_info_flags
& (_TIF_SIGPENDING
| _TIF_RESTORE_SIGMASK
))
554 if (thread_info_flags
& _TIF_NOTIFY_RESUME
) {
555 clear_thread_flag(TIF_NOTIFY_RESUME
);
556 tracehook_notify_resume(current_frame());
557 if (current
->replacement_session_keyring
)
558 key_replace_session_keyring();