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>
35 * atomically swap in the new signal mask, and wait for a signal.
37 asmlinkage
long sys_sigsuspend(int history0
, int history1
, old_sigset_t mask
)
40 siginitset(&blocked
, mask
);
41 return sigsuspend(&blocked
);
45 * set signal action syscall
47 asmlinkage
long sys_sigaction(int sig
,
48 const struct old_sigaction __user
*act
,
49 struct old_sigaction __user
*oact
)
51 struct k_sigaction new_ka
, old_ka
;
56 if (verify_area(VERIFY_READ
, act
, sizeof(*act
)) ||
57 __get_user(new_ka
.sa
.sa_handler
, &act
->sa_handler
) ||
58 __get_user(new_ka
.sa
.sa_restorer
, &act
->sa_restorer
) ||
59 __get_user(new_ka
.sa
.sa_flags
, &act
->sa_flags
) ||
60 __get_user(mask
, &act
->sa_mask
))
62 siginitset(&new_ka
.sa
.sa_mask
, mask
);
65 ret
= do_sigaction(sig
, act
? &new_ka
: NULL
, oact
? &old_ka
: NULL
);
68 if (verify_area(VERIFY_WRITE
, oact
, sizeof(*oact
)) ||
69 __put_user(old_ka
.sa
.sa_handler
, &oact
->sa_handler
) ||
70 __put_user(old_ka
.sa
.sa_restorer
, &oact
->sa_restorer
) ||
71 __put_user(old_ka
.sa
.sa_flags
, &oact
->sa_flags
) ||
72 __put_user(old_ka
.sa
.sa_mask
.sig
[0], &oact
->sa_mask
))
80 * set alternate signal stack syscall
82 asmlinkage
long sys_sigaltstack(const stack_t __user
*uss
, stack_t
*uoss
)
84 return do_sigaltstack(uss
, uoss
, current_frame()->sp
);
88 * do a signal return; undo the signal stack.
90 static int restore_sigcontext(struct pt_regs
*regs
,
91 struct sigcontext __user
*sc
, long *_d0
)
95 /* Always make any pending restarted system calls return -EINTR */
96 current_thread_info()->restart_block
.fn
= do_no_restart_syscall
;
98 if (is_using_fpu(current
))
99 fpu_kill_state(current
);
101 #define COPY(x) err |= __get_user(regs->x, &sc->x)
102 COPY(d1
); COPY(d2
); COPY(d3
);
103 COPY(a0
); COPY(a1
); COPY(a2
); COPY(a3
);
104 COPY(e0
); COPY(e1
); COPY(e2
); COPY(e3
);
105 COPY(e4
); COPY(e5
); COPY(e6
); COPY(e7
);
106 COPY(lar
); COPY(lir
);
107 COPY(mdr
); COPY(mdrq
);
108 COPY(mcvf
); COPY(mcrl
); COPY(mcrh
);
113 unsigned int tmpflags
;
114 #ifndef CONFIG_MN10300_USING_JTAG
115 #define USER_EPSW (EPSW_FLAG_Z | EPSW_FLAG_N | EPSW_FLAG_C | EPSW_FLAG_V | \
118 #define USER_EPSW (EPSW_FLAG_Z | EPSW_FLAG_N | EPSW_FLAG_C | EPSW_FLAG_V | \
121 err
|= __get_user(tmpflags
, &sc
->epsw
);
122 regs
->epsw
= (regs
->epsw
& ~USER_EPSW
) |
123 (tmpflags
& USER_EPSW
);
124 regs
->orig_d0
= -1; /* disable syscall checks */
128 struct fpucontext
*buf
;
129 err
|= __get_user(buf
, &sc
->fpucontext
);
131 if (verify_area(VERIFY_READ
, buf
, sizeof(*buf
)))
133 err
|= fpu_restore_sigcontext(buf
);
137 err
|= __get_user(*_d0
, &sc
->d0
);
145 * standard signal return syscall
147 asmlinkage
long sys_sigreturn(void)
149 struct sigframe __user
*frame
;
153 frame
= (struct sigframe __user
*) current_frame()->sp
;
154 if (verify_area(VERIFY_READ
, frame
, sizeof(*frame
)))
156 if (__get_user(set
.sig
[0], &frame
->sc
.oldmask
))
159 if (_NSIG_WORDS
> 1 &&
160 __copy_from_user(&set
.sig
[1], &frame
->extramask
,
161 sizeof(frame
->extramask
)))
164 set_current_blocked(&set
);
166 if (restore_sigcontext(current_frame(), &frame
->sc
, &d0
))
172 force_sig(SIGSEGV
, current
);
177 * realtime signal return syscall
179 asmlinkage
long sys_rt_sigreturn(void)
181 struct rt_sigframe __user
*frame
;
185 frame
= (struct rt_sigframe __user
*) current_frame()->sp
;
186 if (verify_area(VERIFY_READ
, frame
, sizeof(*frame
)))
188 if (__copy_from_user(&set
, &frame
->uc
.uc_sigmask
, sizeof(set
)))
191 set_current_blocked(&set
);
193 if (restore_sigcontext(current_frame(), &frame
->uc
.uc_mcontext
, &d0
))
196 if (do_sigaltstack(&frame
->uc
.uc_stack
, NULL
, current_frame()->sp
) ==
203 force_sig(SIGSEGV
, current
);
208 * store the userspace context into a signal frame
210 static int setup_sigcontext(struct sigcontext __user
*sc
,
211 struct fpucontext
*fpuctx
,
212 struct pt_regs
*regs
,
217 #define COPY(x) err |= __put_user(regs->x, &sc->x)
218 COPY(d0
); COPY(d1
); COPY(d2
); COPY(d3
);
219 COPY(a0
); COPY(a1
); COPY(a2
); COPY(a3
);
220 COPY(e0
); COPY(e1
); COPY(e2
); COPY(e3
);
221 COPY(e4
); COPY(e5
); COPY(e6
); COPY(e7
);
222 COPY(lar
); COPY(lir
);
223 COPY(mdr
); COPY(mdrq
);
224 COPY(mcvf
); COPY(mcrl
); COPY(mcrh
);
225 COPY(sp
); COPY(epsw
); COPY(pc
);
228 tmp
= fpu_setup_sigcontext(fpuctx
);
232 err
|= __put_user(tmp
? fpuctx
: NULL
, &sc
->fpucontext
);
234 /* non-iBCS2 extensions.. */
235 err
|= __put_user(mask
, &sc
->oldmask
);
241 * determine which stack to use..
243 static inline void __user
*get_sigframe(struct k_sigaction
*ka
,
244 struct pt_regs
*regs
,
249 /* default to using normal stack */
252 /* this is the X/Open sanctioned signal stack switching. */
253 if (ka
->sa
.sa_flags
& SA_ONSTACK
) {
254 if (sas_ss_flags(sp
) == 0)
255 sp
= current
->sas_ss_sp
+ current
->sas_ss_size
;
258 return (void __user
*) ((sp
- frame_size
) & ~7UL);
262 * set up a normal signal frame
264 static int setup_frame(int sig
, struct k_sigaction
*ka
, sigset_t
*set
,
265 struct pt_regs
*regs
)
267 struct sigframe __user
*frame
;
270 frame
= get_sigframe(ka
, regs
, sizeof(*frame
));
272 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
)))
277 current_thread_info()->exec_domain
&&
278 current_thread_info()->exec_domain
->signal_invmap
)
279 rsig
= current_thread_info()->exec_domain
->signal_invmap
[sig
];
281 if (__put_user(rsig
, &frame
->sig
) < 0 ||
282 __put_user(&frame
->sc
, &frame
->psc
) < 0)
285 if (setup_sigcontext(&frame
->sc
, &frame
->fpuctx
, regs
, set
->sig
[0]))
288 if (_NSIG_WORDS
> 1) {
289 if (__copy_to_user(frame
->extramask
, &set
->sig
[1],
290 sizeof(frame
->extramask
)))
294 /* set up to return from userspace. If provided, use a stub already in
296 if (ka
->sa
.sa_flags
& SA_RESTORER
) {
297 if (__put_user(ka
->sa
.sa_restorer
, &frame
->pretcode
))
300 if (__put_user((void (*)(void))frame
->retcode
,
303 /* this is mov $,d0; syscall 0 */
304 if (__put_user(0x2c, (char *)(frame
->retcode
+ 0)) ||
305 __put_user(__NR_sigreturn
, (char *)(frame
->retcode
+ 1)) ||
306 __put_user(0x00, (char *)(frame
->retcode
+ 2)) ||
307 __put_user(0xf0, (char *)(frame
->retcode
+ 3)) ||
308 __put_user(0xe0, (char *)(frame
->retcode
+ 4)))
310 flush_icache_range((unsigned long) frame
->retcode
,
311 (unsigned long) frame
->retcode
+ 5);
314 /* set up registers for signal handler */
315 regs
->sp
= (unsigned long) frame
;
316 regs
->pc
= (unsigned long) ka
->sa
.sa_handler
;
318 regs
->d1
= (unsigned long) &frame
->sc
;
321 printk(KERN_DEBUG
"SIG deliver %d (%s:%d): sp=%p pc=%lx ra=%p\n",
322 sig
, current
->comm
, current
->pid
, frame
, regs
->pc
,
329 force_sigsegv(sig
, current
);
334 * set up a realtime signal frame
336 static int setup_rt_frame(int sig
, struct k_sigaction
*ka
, siginfo_t
*info
,
337 sigset_t
*set
, struct pt_regs
*regs
)
339 struct rt_sigframe __user
*frame
;
342 frame
= get_sigframe(ka
, regs
, sizeof(*frame
));
344 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
)))
349 current_thread_info()->exec_domain
&&
350 current_thread_info()->exec_domain
->signal_invmap
)
351 rsig
= current_thread_info()->exec_domain
->signal_invmap
[sig
];
353 if (__put_user(rsig
, &frame
->sig
) ||
354 __put_user(&frame
->info
, &frame
->pinfo
) ||
355 __put_user(&frame
->uc
, &frame
->puc
) ||
356 copy_siginfo_to_user(&frame
->info
, info
))
359 /* create the ucontext. */
360 if (__put_user(0, &frame
->uc
.uc_flags
) ||
361 __put_user(0, &frame
->uc
.uc_link
) ||
362 __put_user((void *)current
->sas_ss_sp
, &frame
->uc
.uc_stack
.ss_sp
) ||
363 __put_user(sas_ss_flags(regs
->sp
), &frame
->uc
.uc_stack
.ss_flags
) ||
364 __put_user(current
->sas_ss_size
, &frame
->uc
.uc_stack
.ss_size
) ||
365 setup_sigcontext(&frame
->uc
.uc_mcontext
,
366 &frame
->fpuctx
, regs
, set
->sig
[0]) ||
367 __copy_to_user(&frame
->uc
.uc_sigmask
, set
, sizeof(*set
)))
370 /* set up to return from userspace. If provided, use a stub already in
372 if (ka
->sa
.sa_flags
& SA_RESTORER
) {
373 if (__put_user(ka
->sa
.sa_restorer
, &frame
->pretcode
))
376 if (__put_user((void(*)(void))frame
->retcode
,
378 /* This is mov $,d0; syscall 0 */
379 __put_user(0x2c, (char *)(frame
->retcode
+ 0)) ||
380 __put_user(__NR_rt_sigreturn
,
381 (char *)(frame
->retcode
+ 1)) ||
382 __put_user(0x00, (char *)(frame
->retcode
+ 2)) ||
383 __put_user(0xf0, (char *)(frame
->retcode
+ 3)) ||
384 __put_user(0xe0, (char *)(frame
->retcode
+ 4)))
387 flush_icache_range((u_long
) frame
->retcode
,
388 (u_long
) frame
->retcode
+ 5);
391 /* Set up registers for signal handler */
392 regs
->sp
= (unsigned long) frame
;
393 regs
->pc
= (unsigned long) ka
->sa
.sa_handler
;
395 regs
->d1
= (long) &frame
->info
;
398 printk(KERN_DEBUG
"SIG deliver %d (%s:%d): sp=%p pc=%lx ra=%p\n",
399 sig
, current
->comm
, current
->pid
, frame
, regs
->pc
,
406 force_sigsegv(sig
, current
);
410 static inline void stepback(struct pt_regs
*regs
)
417 * handle the actual delivery of a signal to userspace
419 static int handle_signal(int sig
,
420 siginfo_t
*info
, struct k_sigaction
*ka
,
421 struct pt_regs
*regs
)
423 sigset_t
*oldset
= sigmask_to_save();
426 /* Are we from a system call? */
427 if (regs
->orig_d0
>= 0) {
428 /* If so, check system call restarting.. */
430 case -ERESTART_RESTARTBLOCK
:
431 case -ERESTARTNOHAND
:
436 if (!(ka
->sa
.sa_flags
& SA_RESTART
)) {
442 case -ERESTARTNOINTR
:
443 regs
->d0
= regs
->orig_d0
;
448 /* Set up the stack frame */
449 if (ka
->sa
.sa_flags
& SA_SIGINFO
)
450 ret
= setup_rt_frame(sig
, ka
, info
, oldset
, regs
);
452 ret
= setup_frame(sig
, ka
, oldset
, regs
);
456 signal_delivered(sig
, info
, ka
, regs
,
457 test_thread_flag(TIF_SINGLESTEP
));
462 * handle a potential signal
464 static void do_signal(struct pt_regs
*regs
)
466 struct k_sigaction ka
;
470 signr
= get_signal_to_deliver(&info
, &ka
, regs
, NULL
);
472 if (handle_signal(signr
, &info
, &ka
, regs
) == 0) {
478 /* did we come from a system call? */
479 if (regs
->orig_d0
>= 0) {
480 /* restart the system call - no handlers present */
482 case -ERESTARTNOHAND
:
484 case -ERESTARTNOINTR
:
485 regs
->d0
= regs
->orig_d0
;
489 case -ERESTART_RESTARTBLOCK
:
490 regs
->d0
= __NR_restart_syscall
;
496 /* if there's no signal to deliver, we just put the saved sigmask
498 restore_saved_sigmask();
502 * notification of userspace execution resumption
503 * - triggered by current->work.notify_resume
505 asmlinkage
void do_notify_resume(struct pt_regs
*regs
, u32 thread_info_flags
)
507 /* Pending single-step? */
508 if (thread_info_flags
& _TIF_SINGLESTEP
) {
509 #ifndef CONFIG_MN10300_USING_JTAG
510 regs
->epsw
|= EPSW_T
;
511 clear_thread_flag(TIF_SINGLESTEP
);
513 BUG(); /* no h/w single-step if using JTAG unit */
517 /* deal with pending signal delivery */
518 if (thread_info_flags
& _TIF_SIGPENDING
)
521 if (thread_info_flags
& _TIF_NOTIFY_RESUME
) {
522 clear_thread_flag(TIF_NOTIFY_RESUME
);
523 tracehook_notify_resume(current_frame());