2 * linux/arch/alpha/kernel/signal.c
4 * Copyright (C) 1995 Linus Torvalds
6 * 1997-11-02 Modified for POSIX.1b signals by Richard Henderson
9 #include <linux/sched.h>
10 #include <linux/kernel.h>
11 #include <linux/signal.h>
12 #include <linux/errno.h>
13 #include <linux/wait.h>
14 #include <linux/ptrace.h>
15 #include <linux/unistd.h>
17 #include <linux/smp.h>
18 #include <linux/stddef.h>
19 #include <linux/tty.h>
20 #include <linux/binfmts.h>
21 #include <linux/bitops.h>
22 #include <linux/syscalls.h>
23 #include <linux/tracehook.h>
25 #include <asm/uaccess.h>
26 #include <asm/sigcontext.h>
27 #include <asm/ucontext.h>
34 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
36 asmlinkage
void ret_from_sys_call(void);
37 static void do_signal(struct pt_regs
*, struct switch_stack
*,
38 unsigned long, unsigned long);
42 * The OSF/1 sigprocmask calling sequence is different from the
43 * C sigprocmask() sequence..
45 SYSCALL_DEFINE2(osf_sigprocmask
, int, how
, unsigned long, newmask
)
51 siginitset(&mask
, newmask
& ~_BLOCKABLE
);
52 res
= siprocmask(how
, &mask
, &oldmask
);
54 force_successful_syscall_return();
55 res
= oldmask
->sig
[0];
60 SYSCALL_DEFINE3(osf_sigaction
, int, sig
,
61 const struct osf_sigaction __user
*, act
,
62 struct osf_sigaction __user
*, oact
)
64 struct k_sigaction new_ka
, old_ka
;
69 if (!access_ok(VERIFY_READ
, act
, sizeof(*act
)) ||
70 __get_user(new_ka
.sa
.sa_handler
, &act
->sa_handler
) ||
71 __get_user(new_ka
.sa
.sa_flags
, &act
->sa_flags
))
73 __get_user(mask
, &act
->sa_mask
);
74 siginitset(&new_ka
.sa
.sa_mask
, mask
);
75 new_ka
.ka_restorer
= NULL
;
78 ret
= do_sigaction(sig
, act
? &new_ka
: NULL
, oact
? &old_ka
: NULL
);
81 if (!access_ok(VERIFY_WRITE
, oact
, sizeof(*oact
)) ||
82 __put_user(old_ka
.sa
.sa_handler
, &oact
->sa_handler
) ||
83 __put_user(old_ka
.sa
.sa_flags
, &oact
->sa_flags
))
85 __put_user(old_ka
.sa
.sa_mask
.sig
[0], &oact
->sa_mask
);
91 SYSCALL_DEFINE5(rt_sigaction
, int, sig
, const struct sigaction __user
*, act
,
92 struct sigaction __user
*, oact
,
93 size_t, sigsetsize
, void __user
*, restorer
)
95 struct k_sigaction new_ka
, old_ka
;
98 /* XXX: Don't preclude handling different sized sigset_t's. */
99 if (sigsetsize
!= sizeof(sigset_t
))
103 new_ka
.ka_restorer
= restorer
;
104 if (copy_from_user(&new_ka
.sa
, act
, sizeof(*act
)))
108 ret
= do_sigaction(sig
, act
? &new_ka
: NULL
, oact
? &old_ka
: NULL
);
111 if (copy_to_user(oact
, &old_ka
.sa
, sizeof(*oact
)))
119 * Atomically swap in the new signal mask, and wait for a signal.
121 SYSCALL_DEFINE1(sigsuspend
, old_sigset_t
, mask
)
124 spin_lock_irq(¤t
->sighand
->siglock
);
125 current
->saved_sigmask
= current
->blocked
;
126 siginitset(¤t
->blocked
, mask
);
128 spin_unlock_irq(¤t
->sighand
->siglock
);
130 current
->state
= TASK_INTERRUPTIBLE
;
132 set_thread_flag(TIF_RESTORE_SIGMASK
);
133 return -ERESTARTNOHAND
;
137 sys_sigaltstack(const stack_t __user
*uss
, stack_t __user
*uoss
)
139 return do_sigaltstack(uss
, uoss
, rdusp());
143 * Do a signal return; undo the signal stack.
147 # error "Non SA_SIGINFO frame needs rearranging"
152 struct sigcontext sc
;
153 unsigned int retcode
[3];
160 unsigned int retcode
[3];
163 /* If this changes, userland unwinders that Know Things about our signal
164 frame will break. Do not undertake lightly. It also implies an ABI
165 change wrt the size of siginfo_t, which may cause some pain. */
166 extern char compile_time_assert
167 [offsetof(struct rt_sigframe
, uc
.uc_mcontext
) == 176 ? 1 : -1];
169 #define INSN_MOV_R30_R16 0x47fe0410
170 #define INSN_LDI_R0 0x201f0000
171 #define INSN_CALLSYS 0x00000083
174 restore_sigcontext(struct sigcontext __user
*sc
, struct pt_regs
*regs
,
175 struct switch_stack
*sw
)
178 long i
, err
= __get_user(regs
->pc
, &sc
->sc_pc
);
180 current_thread_info()->restart_block
.fn
= do_no_restart_syscall
;
182 sw
->r26
= (unsigned long) ret_from_sys_call
;
184 err
|= __get_user(regs
->r0
, sc
->sc_regs
+0);
185 err
|= __get_user(regs
->r1
, sc
->sc_regs
+1);
186 err
|= __get_user(regs
->r2
, sc
->sc_regs
+2);
187 err
|= __get_user(regs
->r3
, sc
->sc_regs
+3);
188 err
|= __get_user(regs
->r4
, sc
->sc_regs
+4);
189 err
|= __get_user(regs
->r5
, sc
->sc_regs
+5);
190 err
|= __get_user(regs
->r6
, sc
->sc_regs
+6);
191 err
|= __get_user(regs
->r7
, sc
->sc_regs
+7);
192 err
|= __get_user(regs
->r8
, sc
->sc_regs
+8);
193 err
|= __get_user(sw
->r9
, sc
->sc_regs
+9);
194 err
|= __get_user(sw
->r10
, sc
->sc_regs
+10);
195 err
|= __get_user(sw
->r11
, sc
->sc_regs
+11);
196 err
|= __get_user(sw
->r12
, sc
->sc_regs
+12);
197 err
|= __get_user(sw
->r13
, sc
->sc_regs
+13);
198 err
|= __get_user(sw
->r14
, sc
->sc_regs
+14);
199 err
|= __get_user(sw
->r15
, sc
->sc_regs
+15);
200 err
|= __get_user(regs
->r16
, sc
->sc_regs
+16);
201 err
|= __get_user(regs
->r17
, sc
->sc_regs
+17);
202 err
|= __get_user(regs
->r18
, sc
->sc_regs
+18);
203 err
|= __get_user(regs
->r19
, sc
->sc_regs
+19);
204 err
|= __get_user(regs
->r20
, sc
->sc_regs
+20);
205 err
|= __get_user(regs
->r21
, sc
->sc_regs
+21);
206 err
|= __get_user(regs
->r22
, sc
->sc_regs
+22);
207 err
|= __get_user(regs
->r23
, sc
->sc_regs
+23);
208 err
|= __get_user(regs
->r24
, sc
->sc_regs
+24);
209 err
|= __get_user(regs
->r25
, sc
->sc_regs
+25);
210 err
|= __get_user(regs
->r26
, sc
->sc_regs
+26);
211 err
|= __get_user(regs
->r27
, sc
->sc_regs
+27);
212 err
|= __get_user(regs
->r28
, sc
->sc_regs
+28);
213 err
|= __get_user(regs
->gp
, sc
->sc_regs
+29);
214 err
|= __get_user(usp
, sc
->sc_regs
+30);
217 for (i
= 0; i
< 31; i
++)
218 err
|= __get_user(sw
->fp
[i
], sc
->sc_fpregs
+i
);
219 err
|= __get_user(sw
->fp
[31], &sc
->sc_fpcr
);
224 /* Note that this syscall is also used by setcontext(3) to install
225 a given sigcontext. This because it's impossible to set *all*
226 registers and transfer control from userland. */
229 do_sigreturn(struct sigcontext __user
*sc
, struct pt_regs
*regs
,
230 struct switch_stack
*sw
)
234 /* Verify that it's a good sigcontext before using it */
235 if (!access_ok(VERIFY_READ
, sc
, sizeof(*sc
)))
237 if (__get_user(set
.sig
[0], &sc
->sc_mask
))
240 sigdelsetmask(&set
, ~_BLOCKABLE
);
241 spin_lock_irq(¤t
->sighand
->siglock
);
242 current
->blocked
= set
;
244 spin_unlock_irq(¤t
->sighand
->siglock
);
246 if (restore_sigcontext(sc
, regs
, sw
))
249 /* Send SIGTRAP if we're single-stepping: */
250 if (ptrace_cancel_bpt (current
)) {
253 info
.si_signo
= SIGTRAP
;
255 info
.si_code
= TRAP_BRKPT
;
256 info
.si_addr
= (void __user
*) regs
->pc
;
258 send_sig_info(SIGTRAP
, &info
, current
);
263 force_sig(SIGSEGV
, current
);
267 do_rt_sigreturn(struct rt_sigframe __user
*frame
, struct pt_regs
*regs
,
268 struct switch_stack
*sw
)
272 /* Verify that it's a good ucontext_t before using it */
273 if (!access_ok(VERIFY_READ
, &frame
->uc
, sizeof(frame
->uc
)))
275 if (__copy_from_user(&set
, &frame
->uc
.uc_sigmask
, sizeof(set
)))
278 sigdelsetmask(&set
, ~_BLOCKABLE
);
279 spin_lock_irq(¤t
->sighand
->siglock
);
280 current
->blocked
= set
;
282 spin_unlock_irq(¤t
->sighand
->siglock
);
284 if (restore_sigcontext(&frame
->uc
.uc_mcontext
, regs
, sw
))
287 /* Send SIGTRAP if we're single-stepping: */
288 if (ptrace_cancel_bpt (current
)) {
291 info
.si_signo
= SIGTRAP
;
293 info
.si_code
= TRAP_BRKPT
;
294 info
.si_addr
= (void __user
*) regs
->pc
;
296 send_sig_info(SIGTRAP
, &info
, current
);
301 force_sig(SIGSEGV
, current
);
306 * Set up a signal frame.
309 static inline void __user
*
310 get_sigframe(struct k_sigaction
*ka
, unsigned long sp
, size_t frame_size
)
312 if ((ka
->sa
.sa_flags
& SA_ONSTACK
) != 0 && ! sas_ss_flags(sp
))
313 sp
= current
->sas_ss_sp
+ current
->sas_ss_size
;
315 return (void __user
*)((sp
- frame_size
) & -32ul);
319 setup_sigcontext(struct sigcontext __user
*sc
, struct pt_regs
*regs
,
320 struct switch_stack
*sw
, unsigned long mask
, unsigned long sp
)
324 err
|= __put_user(on_sig_stack((unsigned long)sc
), &sc
->sc_onstack
);
325 err
|= __put_user(mask
, &sc
->sc_mask
);
326 err
|= __put_user(regs
->pc
, &sc
->sc_pc
);
327 err
|= __put_user(8, &sc
->sc_ps
);
329 err
|= __put_user(regs
->r0
, sc
->sc_regs
+0);
330 err
|= __put_user(regs
->r1
, sc
->sc_regs
+1);
331 err
|= __put_user(regs
->r2
, sc
->sc_regs
+2);
332 err
|= __put_user(regs
->r3
, sc
->sc_regs
+3);
333 err
|= __put_user(regs
->r4
, sc
->sc_regs
+4);
334 err
|= __put_user(regs
->r5
, sc
->sc_regs
+5);
335 err
|= __put_user(regs
->r6
, sc
->sc_regs
+6);
336 err
|= __put_user(regs
->r7
, sc
->sc_regs
+7);
337 err
|= __put_user(regs
->r8
, sc
->sc_regs
+8);
338 err
|= __put_user(sw
->r9
, sc
->sc_regs
+9);
339 err
|= __put_user(sw
->r10
, sc
->sc_regs
+10);
340 err
|= __put_user(sw
->r11
, sc
->sc_regs
+11);
341 err
|= __put_user(sw
->r12
, sc
->sc_regs
+12);
342 err
|= __put_user(sw
->r13
, sc
->sc_regs
+13);
343 err
|= __put_user(sw
->r14
, sc
->sc_regs
+14);
344 err
|= __put_user(sw
->r15
, sc
->sc_regs
+15);
345 err
|= __put_user(regs
->r16
, sc
->sc_regs
+16);
346 err
|= __put_user(regs
->r17
, sc
->sc_regs
+17);
347 err
|= __put_user(regs
->r18
, sc
->sc_regs
+18);
348 err
|= __put_user(regs
->r19
, sc
->sc_regs
+19);
349 err
|= __put_user(regs
->r20
, sc
->sc_regs
+20);
350 err
|= __put_user(regs
->r21
, sc
->sc_regs
+21);
351 err
|= __put_user(regs
->r22
, sc
->sc_regs
+22);
352 err
|= __put_user(regs
->r23
, sc
->sc_regs
+23);
353 err
|= __put_user(regs
->r24
, sc
->sc_regs
+24);
354 err
|= __put_user(regs
->r25
, sc
->sc_regs
+25);
355 err
|= __put_user(regs
->r26
, sc
->sc_regs
+26);
356 err
|= __put_user(regs
->r27
, sc
->sc_regs
+27);
357 err
|= __put_user(regs
->r28
, sc
->sc_regs
+28);
358 err
|= __put_user(regs
->gp
, sc
->sc_regs
+29);
359 err
|= __put_user(sp
, sc
->sc_regs
+30);
360 err
|= __put_user(0, sc
->sc_regs
+31);
362 for (i
= 0; i
< 31; i
++)
363 err
|= __put_user(sw
->fp
[i
], sc
->sc_fpregs
+i
);
364 err
|= __put_user(0, sc
->sc_fpregs
+31);
365 err
|= __put_user(sw
->fp
[31], &sc
->sc_fpcr
);
367 err
|= __put_user(regs
->trap_a0
, &sc
->sc_traparg_a0
);
368 err
|= __put_user(regs
->trap_a1
, &sc
->sc_traparg_a1
);
369 err
|= __put_user(regs
->trap_a2
, &sc
->sc_traparg_a2
);
375 setup_frame(int sig
, struct k_sigaction
*ka
, sigset_t
*set
,
376 struct pt_regs
*regs
, struct switch_stack
* sw
)
378 unsigned long oldsp
, r26
, err
= 0;
379 struct sigframe __user
*frame
;
382 frame
= get_sigframe(ka
, oldsp
, sizeof(*frame
));
383 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
)))
386 err
|= setup_sigcontext(&frame
->sc
, regs
, sw
, set
->sig
[0], oldsp
);
390 /* Set up to return from userspace. If provided, use a stub
391 already in userspace. */
392 if (ka
->ka_restorer
) {
393 r26
= (unsigned long) ka
->ka_restorer
;
395 err
|= __put_user(INSN_MOV_R30_R16
, frame
->retcode
+0);
396 err
|= __put_user(INSN_LDI_R0
+__NR_sigreturn
, frame
->retcode
+1);
397 err
|= __put_user(INSN_CALLSYS
, frame
->retcode
+2);
399 r26
= (unsigned long) frame
->retcode
;
402 /* Check that everything was written properly. */
406 /* "Return" to the handler */
408 regs
->r27
= regs
->pc
= (unsigned long) ka
->sa
.sa_handler
;
409 regs
->r16
= sig
; /* a0: signal number */
410 regs
->r17
= 0; /* a1: exception code */
411 regs
->r18
= (unsigned long) &frame
->sc
; /* a2: sigcontext pointer */
412 wrusp((unsigned long) frame
);
415 printk("SIG deliver (%s:%d): sp=%p pc=%p ra=%p\n",
416 current
->comm
, current
->pid
, frame
, regs
->pc
, regs
->r26
);
422 force_sigsegv(sig
, current
);
427 setup_rt_frame(int sig
, struct k_sigaction
*ka
, siginfo_t
*info
,
428 sigset_t
*set
, struct pt_regs
*regs
, struct switch_stack
* sw
)
430 unsigned long oldsp
, r26
, err
= 0;
431 struct rt_sigframe __user
*frame
;
434 frame
= get_sigframe(ka
, oldsp
, sizeof(*frame
));
435 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
)))
438 err
|= copy_siginfo_to_user(&frame
->info
, info
);
440 /* Create the ucontext. */
441 err
|= __put_user(0, &frame
->uc
.uc_flags
);
442 err
|= __put_user(0, &frame
->uc
.uc_link
);
443 err
|= __put_user(set
->sig
[0], &frame
->uc
.uc_osf_sigmask
);
444 err
|= __put_user(current
->sas_ss_sp
, &frame
->uc
.uc_stack
.ss_sp
);
445 err
|= __put_user(sas_ss_flags(oldsp
), &frame
->uc
.uc_stack
.ss_flags
);
446 err
|= __put_user(current
->sas_ss_size
, &frame
->uc
.uc_stack
.ss_size
);
447 err
|= setup_sigcontext(&frame
->uc
.uc_mcontext
, regs
, sw
,
449 err
|= __copy_to_user(&frame
->uc
.uc_sigmask
, set
, sizeof(*set
));
453 /* Set up to return from userspace. If provided, use a stub
454 already in userspace. */
455 if (ka
->ka_restorer
) {
456 r26
= (unsigned long) ka
->ka_restorer
;
458 err
|= __put_user(INSN_MOV_R30_R16
, frame
->retcode
+0);
459 err
|= __put_user(INSN_LDI_R0
+__NR_rt_sigreturn
,
461 err
|= __put_user(INSN_CALLSYS
, frame
->retcode
+2);
463 r26
= (unsigned long) frame
->retcode
;
469 /* "Return" to the handler */
471 regs
->r27
= regs
->pc
= (unsigned long) ka
->sa
.sa_handler
;
472 regs
->r16
= sig
; /* a0: signal number */
473 regs
->r17
= (unsigned long) &frame
->info
; /* a1: siginfo pointer */
474 regs
->r18
= (unsigned long) &frame
->uc
; /* a2: ucontext pointer */
475 wrusp((unsigned long) frame
);
478 printk("SIG deliver (%s:%d): sp=%p pc=%p ra=%p\n",
479 current
->comm
, current
->pid
, frame
, regs
->pc
, regs
->r26
);
485 force_sigsegv(sig
, current
);
491 * OK, we're invoking a handler.
494 handle_signal(int sig
, struct k_sigaction
*ka
, siginfo_t
*info
,
495 sigset_t
*oldset
, struct pt_regs
* regs
, struct switch_stack
*sw
)
499 if (ka
->sa
.sa_flags
& SA_SIGINFO
)
500 ret
= setup_rt_frame(sig
, ka
, info
, oldset
, regs
, sw
);
502 ret
= setup_frame(sig
, ka
, oldset
, regs
, sw
);
505 spin_lock_irq(¤t
->sighand
->siglock
);
506 sigorsets(¤t
->blocked
,¤t
->blocked
,&ka
->sa
.sa_mask
);
507 if (!(ka
->sa
.sa_flags
& SA_NODEFER
))
508 sigaddset(¤t
->blocked
,sig
);
510 spin_unlock_irq(¤t
->sighand
->siglock
);
517 syscall_restart(unsigned long r0
, unsigned long r19
,
518 struct pt_regs
*regs
, struct k_sigaction
*ka
)
522 if (!(ka
->sa
.sa_flags
& SA_RESTART
)) {
529 regs
->r0
= r0
; /* reset v0 and a3 and replay syscall */
533 case ERESTART_RESTARTBLOCK
:
541 * Note that 'init' is a special process: it doesn't get signals it doesn't
542 * want to handle. Thus you cannot kill init even with a SIGKILL even by
545 * Note that we go through the signals twice: once to check the signals that
546 * the kernel can handle, and then we build all the user-level signal handling
547 * stack-frames in one go after that.
549 * "r0" and "r19" are the registers we need to restore for system call
550 * restart. "r0" is also used as an indicator whether we can restart at
551 * all (if we get here from anything but a syscall return, it will be 0)
554 do_signal(struct pt_regs
* regs
, struct switch_stack
* sw
,
555 unsigned long r0
, unsigned long r19
)
559 unsigned long single_stepping
= ptrace_cancel_bpt(current
);
560 struct k_sigaction ka
;
563 if (test_thread_flag(TIF_RESTORE_SIGMASK
))
564 oldset
= ¤t
->saved_sigmask
;
566 oldset
= ¤t
->blocked
;
568 /* This lets the debugger run, ... */
569 signr
= get_signal_to_deliver(&info
, &ka
, regs
, NULL
);
571 /* ... so re-check the single stepping. */
572 single_stepping
|= ptrace_cancel_bpt(current
);
575 /* Whee! Actually deliver the signal. */
577 syscall_restart(r0
, r19
, regs
, &ka
);
578 if (handle_signal(signr
, &ka
, &info
, oldset
, regs
, sw
) == 0) {
579 /* A signal was successfully delivered, and the
580 saved sigmask was stored on the signal frame,
581 and will be restored by sigreturn. So we can
582 simply clear the restore sigmask flag. */
583 if (test_thread_flag(TIF_RESTORE_SIGMASK
))
584 clear_thread_flag(TIF_RESTORE_SIGMASK
);
587 ptrace_set_bpt(current
); /* re-set bpt */
596 /* Reset v0 and a3 and replay syscall. */
601 case ERESTART_RESTARTBLOCK
:
602 /* Force v0 to the restart syscall and reply. */
603 regs
->r0
= __NR_restart_syscall
;
609 /* If there's no signal to deliver, we just restore the saved mask. */
610 if (test_thread_flag(TIF_RESTORE_SIGMASK
)) {
611 clear_thread_flag(TIF_RESTORE_SIGMASK
);
612 sigprocmask(SIG_SETMASK
, ¤t
->saved_sigmask
, NULL
);
616 ptrace_set_bpt(current
); /* re-set breakpoint */
620 do_notify_resume(struct pt_regs
*regs
, struct switch_stack
*sw
,
621 unsigned long thread_info_flags
,
622 unsigned long r0
, unsigned long r19
)
624 if (thread_info_flags
& (_TIF_SIGPENDING
| _TIF_RESTORE_SIGMASK
))
625 do_signal(regs
, sw
, r0
, r19
);
627 if (thread_info_flags
& _TIF_NOTIFY_RESUME
) {
628 clear_thread_flag(TIF_NOTIFY_RESUME
);
629 tracehook_notify_resume(regs
);
630 if (current
->replacement_session_keyring
)
631 key_replace_session_keyring();