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
= sigprocmask(how
, &mask
, &oldmask
);
54 force_successful_syscall_return();
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
) ||
72 __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
) ||
84 __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
)
125 current
->saved_sigmask
= current
->blocked
;
128 siginitset(&blocked
, mask
);
129 set_current_blocked(&blocked
);
131 current
->state
= TASK_INTERRUPTIBLE
;
133 set_thread_flag(TIF_RESTORE_SIGMASK
);
134 return -ERESTARTNOHAND
;
138 sys_sigaltstack(const stack_t __user
*uss
, stack_t __user
*uoss
)
140 return do_sigaltstack(uss
, uoss
, rdusp());
144 * Do a signal return; undo the signal stack.
148 # error "Non SA_SIGINFO frame needs rearranging"
153 struct sigcontext sc
;
154 unsigned int retcode
[3];
161 unsigned int retcode
[3];
164 /* If this changes, userland unwinders that Know Things about our signal
165 frame will break. Do not undertake lightly. It also implies an ABI
166 change wrt the size of siginfo_t, which may cause some pain. */
167 extern char compile_time_assert
168 [offsetof(struct rt_sigframe
, uc
.uc_mcontext
) == 176 ? 1 : -1];
170 #define INSN_MOV_R30_R16 0x47fe0410
171 #define INSN_LDI_R0 0x201f0000
172 #define INSN_CALLSYS 0x00000083
175 restore_sigcontext(struct sigcontext __user
*sc
, struct pt_regs
*regs
,
176 struct switch_stack
*sw
)
179 long i
, err
= __get_user(regs
->pc
, &sc
->sc_pc
);
181 current_thread_info()->restart_block
.fn
= do_no_restart_syscall
;
183 sw
->r26
= (unsigned long) ret_from_sys_call
;
185 err
|= __get_user(regs
->r0
, sc
->sc_regs
+0);
186 err
|= __get_user(regs
->r1
, sc
->sc_regs
+1);
187 err
|= __get_user(regs
->r2
, sc
->sc_regs
+2);
188 err
|= __get_user(regs
->r3
, sc
->sc_regs
+3);
189 err
|= __get_user(regs
->r4
, sc
->sc_regs
+4);
190 err
|= __get_user(regs
->r5
, sc
->sc_regs
+5);
191 err
|= __get_user(regs
->r6
, sc
->sc_regs
+6);
192 err
|= __get_user(regs
->r7
, sc
->sc_regs
+7);
193 err
|= __get_user(regs
->r8
, sc
->sc_regs
+8);
194 err
|= __get_user(sw
->r9
, sc
->sc_regs
+9);
195 err
|= __get_user(sw
->r10
, sc
->sc_regs
+10);
196 err
|= __get_user(sw
->r11
, sc
->sc_regs
+11);
197 err
|= __get_user(sw
->r12
, sc
->sc_regs
+12);
198 err
|= __get_user(sw
->r13
, sc
->sc_regs
+13);
199 err
|= __get_user(sw
->r14
, sc
->sc_regs
+14);
200 err
|= __get_user(sw
->r15
, sc
->sc_regs
+15);
201 err
|= __get_user(regs
->r16
, sc
->sc_regs
+16);
202 err
|= __get_user(regs
->r17
, sc
->sc_regs
+17);
203 err
|= __get_user(regs
->r18
, sc
->sc_regs
+18);
204 err
|= __get_user(regs
->r19
, sc
->sc_regs
+19);
205 err
|= __get_user(regs
->r20
, sc
->sc_regs
+20);
206 err
|= __get_user(regs
->r21
, sc
->sc_regs
+21);
207 err
|= __get_user(regs
->r22
, sc
->sc_regs
+22);
208 err
|= __get_user(regs
->r23
, sc
->sc_regs
+23);
209 err
|= __get_user(regs
->r24
, sc
->sc_regs
+24);
210 err
|= __get_user(regs
->r25
, sc
->sc_regs
+25);
211 err
|= __get_user(regs
->r26
, sc
->sc_regs
+26);
212 err
|= __get_user(regs
->r27
, sc
->sc_regs
+27);
213 err
|= __get_user(regs
->r28
, sc
->sc_regs
+28);
214 err
|= __get_user(regs
->gp
, sc
->sc_regs
+29);
215 err
|= __get_user(usp
, sc
->sc_regs
+30);
218 for (i
= 0; i
< 31; i
++)
219 err
|= __get_user(sw
->fp
[i
], sc
->sc_fpregs
+i
);
220 err
|= __get_user(sw
->fp
[31], &sc
->sc_fpcr
);
225 /* Note that this syscall is also used by setcontext(3) to install
226 a given sigcontext. This because it's impossible to set *all*
227 registers and transfer control from userland. */
230 do_sigreturn(struct sigcontext __user
*sc
, struct pt_regs
*regs
,
231 struct switch_stack
*sw
)
235 /* Verify that it's a good sigcontext before using it */
236 if (!access_ok(VERIFY_READ
, sc
, sizeof(*sc
)))
238 if (__get_user(set
.sig
[0], &sc
->sc_mask
))
241 sigdelsetmask(&set
, ~_BLOCKABLE
);
242 set_current_blocked(&set
);
244 if (restore_sigcontext(sc
, regs
, sw
))
247 /* Send SIGTRAP if we're single-stepping: */
248 if (ptrace_cancel_bpt (current
)) {
251 info
.si_signo
= SIGTRAP
;
253 info
.si_code
= TRAP_BRKPT
;
254 info
.si_addr
= (void __user
*) regs
->pc
;
256 send_sig_info(SIGTRAP
, &info
, current
);
261 force_sig(SIGSEGV
, current
);
265 do_rt_sigreturn(struct rt_sigframe __user
*frame
, struct pt_regs
*regs
,
266 struct switch_stack
*sw
)
270 /* Verify that it's a good ucontext_t before using it */
271 if (!access_ok(VERIFY_READ
, &frame
->uc
, sizeof(frame
->uc
)))
273 if (__copy_from_user(&set
, &frame
->uc
.uc_sigmask
, sizeof(set
)))
276 sigdelsetmask(&set
, ~_BLOCKABLE
);
277 set_current_blocked(&set
);
279 if (restore_sigcontext(&frame
->uc
.uc_mcontext
, regs
, sw
))
282 /* Send SIGTRAP if we're single-stepping: */
283 if (ptrace_cancel_bpt (current
)) {
286 info
.si_signo
= SIGTRAP
;
288 info
.si_code
= TRAP_BRKPT
;
289 info
.si_addr
= (void __user
*) regs
->pc
;
291 send_sig_info(SIGTRAP
, &info
, current
);
296 force_sig(SIGSEGV
, current
);
301 * Set up a signal frame.
304 static inline void __user
*
305 get_sigframe(struct k_sigaction
*ka
, unsigned long sp
, size_t frame_size
)
307 if ((ka
->sa
.sa_flags
& SA_ONSTACK
) != 0 && ! sas_ss_flags(sp
))
308 sp
= current
->sas_ss_sp
+ current
->sas_ss_size
;
310 return (void __user
*)((sp
- frame_size
) & -32ul);
314 setup_sigcontext(struct sigcontext __user
*sc
, struct pt_regs
*regs
,
315 struct switch_stack
*sw
, unsigned long mask
, unsigned long sp
)
319 err
|= __put_user(on_sig_stack((unsigned long)sc
), &sc
->sc_onstack
);
320 err
|= __put_user(mask
, &sc
->sc_mask
);
321 err
|= __put_user(regs
->pc
, &sc
->sc_pc
);
322 err
|= __put_user(8, &sc
->sc_ps
);
324 err
|= __put_user(regs
->r0
, sc
->sc_regs
+0);
325 err
|= __put_user(regs
->r1
, sc
->sc_regs
+1);
326 err
|= __put_user(regs
->r2
, sc
->sc_regs
+2);
327 err
|= __put_user(regs
->r3
, sc
->sc_regs
+3);
328 err
|= __put_user(regs
->r4
, sc
->sc_regs
+4);
329 err
|= __put_user(regs
->r5
, sc
->sc_regs
+5);
330 err
|= __put_user(regs
->r6
, sc
->sc_regs
+6);
331 err
|= __put_user(regs
->r7
, sc
->sc_regs
+7);
332 err
|= __put_user(regs
->r8
, sc
->sc_regs
+8);
333 err
|= __put_user(sw
->r9
, sc
->sc_regs
+9);
334 err
|= __put_user(sw
->r10
, sc
->sc_regs
+10);
335 err
|= __put_user(sw
->r11
, sc
->sc_regs
+11);
336 err
|= __put_user(sw
->r12
, sc
->sc_regs
+12);
337 err
|= __put_user(sw
->r13
, sc
->sc_regs
+13);
338 err
|= __put_user(sw
->r14
, sc
->sc_regs
+14);
339 err
|= __put_user(sw
->r15
, sc
->sc_regs
+15);
340 err
|= __put_user(regs
->r16
, sc
->sc_regs
+16);
341 err
|= __put_user(regs
->r17
, sc
->sc_regs
+17);
342 err
|= __put_user(regs
->r18
, sc
->sc_regs
+18);
343 err
|= __put_user(regs
->r19
, sc
->sc_regs
+19);
344 err
|= __put_user(regs
->r20
, sc
->sc_regs
+20);
345 err
|= __put_user(regs
->r21
, sc
->sc_regs
+21);
346 err
|= __put_user(regs
->r22
, sc
->sc_regs
+22);
347 err
|= __put_user(regs
->r23
, sc
->sc_regs
+23);
348 err
|= __put_user(regs
->r24
, sc
->sc_regs
+24);
349 err
|= __put_user(regs
->r25
, sc
->sc_regs
+25);
350 err
|= __put_user(regs
->r26
, sc
->sc_regs
+26);
351 err
|= __put_user(regs
->r27
, sc
->sc_regs
+27);
352 err
|= __put_user(regs
->r28
, sc
->sc_regs
+28);
353 err
|= __put_user(regs
->gp
, sc
->sc_regs
+29);
354 err
|= __put_user(sp
, sc
->sc_regs
+30);
355 err
|= __put_user(0, sc
->sc_regs
+31);
357 for (i
= 0; i
< 31; i
++)
358 err
|= __put_user(sw
->fp
[i
], sc
->sc_fpregs
+i
);
359 err
|= __put_user(0, sc
->sc_fpregs
+31);
360 err
|= __put_user(sw
->fp
[31], &sc
->sc_fpcr
);
362 err
|= __put_user(regs
->trap_a0
, &sc
->sc_traparg_a0
);
363 err
|= __put_user(regs
->trap_a1
, &sc
->sc_traparg_a1
);
364 err
|= __put_user(regs
->trap_a2
, &sc
->sc_traparg_a2
);
370 setup_frame(int sig
, struct k_sigaction
*ka
, sigset_t
*set
,
371 struct pt_regs
*regs
, struct switch_stack
* sw
)
373 unsigned long oldsp
, r26
, err
= 0;
374 struct sigframe __user
*frame
;
377 frame
= get_sigframe(ka
, oldsp
, sizeof(*frame
));
378 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
)))
381 err
|= setup_sigcontext(&frame
->sc
, regs
, sw
, set
->sig
[0], oldsp
);
385 /* Set up to return from userspace. If provided, use a stub
386 already in userspace. */
387 if (ka
->ka_restorer
) {
388 r26
= (unsigned long) ka
->ka_restorer
;
390 err
|= __put_user(INSN_MOV_R30_R16
, frame
->retcode
+0);
391 err
|= __put_user(INSN_LDI_R0
+__NR_sigreturn
, frame
->retcode
+1);
392 err
|= __put_user(INSN_CALLSYS
, frame
->retcode
+2);
394 r26
= (unsigned long) frame
->retcode
;
397 /* Check that everything was written properly. */
401 /* "Return" to the handler */
403 regs
->r27
= regs
->pc
= (unsigned long) ka
->sa
.sa_handler
;
404 regs
->r16
= sig
; /* a0: signal number */
405 regs
->r17
= 0; /* a1: exception code */
406 regs
->r18
= (unsigned long) &frame
->sc
; /* a2: sigcontext pointer */
407 wrusp((unsigned long) frame
);
410 printk("SIG deliver (%s:%d): sp=%p pc=%p ra=%p\n",
411 current
->comm
, current
->pid
, frame
, regs
->pc
, regs
->r26
);
417 force_sigsegv(sig
, current
);
422 setup_rt_frame(int sig
, struct k_sigaction
*ka
, siginfo_t
*info
,
423 sigset_t
*set
, struct pt_regs
*regs
, struct switch_stack
* sw
)
425 unsigned long oldsp
, r26
, err
= 0;
426 struct rt_sigframe __user
*frame
;
429 frame
= get_sigframe(ka
, oldsp
, sizeof(*frame
));
430 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
)))
433 err
|= copy_siginfo_to_user(&frame
->info
, info
);
435 /* Create the ucontext. */
436 err
|= __put_user(0, &frame
->uc
.uc_flags
);
437 err
|= __put_user(0, &frame
->uc
.uc_link
);
438 err
|= __put_user(set
->sig
[0], &frame
->uc
.uc_osf_sigmask
);
439 err
|= __put_user(current
->sas_ss_sp
, &frame
->uc
.uc_stack
.ss_sp
);
440 err
|= __put_user(sas_ss_flags(oldsp
), &frame
->uc
.uc_stack
.ss_flags
);
441 err
|= __put_user(current
->sas_ss_size
, &frame
->uc
.uc_stack
.ss_size
);
442 err
|= setup_sigcontext(&frame
->uc
.uc_mcontext
, regs
, sw
,
444 err
|= __copy_to_user(&frame
->uc
.uc_sigmask
, set
, sizeof(*set
));
448 /* Set up to return from userspace. If provided, use a stub
449 already in userspace. */
450 if (ka
->ka_restorer
) {
451 r26
= (unsigned long) ka
->ka_restorer
;
453 err
|= __put_user(INSN_MOV_R30_R16
, frame
->retcode
+0);
454 err
|= __put_user(INSN_LDI_R0
+__NR_rt_sigreturn
,
456 err
|= __put_user(INSN_CALLSYS
, frame
->retcode
+2);
458 r26
= (unsigned long) frame
->retcode
;
464 /* "Return" to the handler */
466 regs
->r27
= regs
->pc
= (unsigned long) ka
->sa
.sa_handler
;
467 regs
->r16
= sig
; /* a0: signal number */
468 regs
->r17
= (unsigned long) &frame
->info
; /* a1: siginfo pointer */
469 regs
->r18
= (unsigned long) &frame
->uc
; /* a2: ucontext pointer */
470 wrusp((unsigned long) frame
);
473 printk("SIG deliver (%s:%d): sp=%p pc=%p ra=%p\n",
474 current
->comm
, current
->pid
, frame
, regs
->pc
, regs
->r26
);
480 force_sigsegv(sig
, current
);
486 * OK, we're invoking a handler.
489 handle_signal(int sig
, struct k_sigaction
*ka
, siginfo_t
*info
,
490 sigset_t
*oldset
, struct pt_regs
* regs
, struct switch_stack
*sw
)
494 if (ka
->sa
.sa_flags
& SA_SIGINFO
)
495 ret
= setup_rt_frame(sig
, ka
, info
, oldset
, regs
, sw
);
497 ret
= setup_frame(sig
, ka
, oldset
, regs
, sw
);
500 block_sigmask(ka
, sig
);
506 syscall_restart(unsigned long r0
, unsigned long r19
,
507 struct pt_regs
*regs
, struct k_sigaction
*ka
)
511 if (!(ka
->sa
.sa_flags
& SA_RESTART
)) {
518 regs
->r0
= r0
; /* reset v0 and a3 and replay syscall */
522 case ERESTART_RESTARTBLOCK
:
530 * Note that 'init' is a special process: it doesn't get signals it doesn't
531 * want to handle. Thus you cannot kill init even with a SIGKILL even by
534 * Note that we go through the signals twice: once to check the signals that
535 * the kernel can handle, and then we build all the user-level signal handling
536 * stack-frames in one go after that.
538 * "r0" and "r19" are the registers we need to restore for system call
539 * restart. "r0" is also used as an indicator whether we can restart at
540 * all (if we get here from anything but a syscall return, it will be 0)
543 do_signal(struct pt_regs
* regs
, struct switch_stack
* sw
,
544 unsigned long r0
, unsigned long r19
)
548 unsigned long single_stepping
= ptrace_cancel_bpt(current
);
549 struct k_sigaction ka
;
552 if (test_thread_flag(TIF_RESTORE_SIGMASK
))
553 oldset
= ¤t
->saved_sigmask
;
555 oldset
= ¤t
->blocked
;
557 /* This lets the debugger run, ... */
558 signr
= get_signal_to_deliver(&info
, &ka
, regs
, NULL
);
560 /* ... so re-check the single stepping. */
561 single_stepping
|= ptrace_cancel_bpt(current
);
564 /* Whee! Actually deliver the signal. */
566 syscall_restart(r0
, r19
, regs
, &ka
);
567 if (handle_signal(signr
, &ka
, &info
, oldset
, regs
, sw
) == 0) {
568 /* A signal was successfully delivered, and the
569 saved sigmask was stored on the signal frame,
570 and will be restored by sigreturn. So we can
571 simply clear the restore sigmask flag. */
572 if (test_thread_flag(TIF_RESTORE_SIGMASK
))
573 clear_thread_flag(TIF_RESTORE_SIGMASK
);
576 ptrace_set_bpt(current
); /* re-set bpt */
585 /* Reset v0 and a3 and replay syscall. */
590 case ERESTART_RESTARTBLOCK
:
591 /* Force v0 to the restart syscall and reply. */
592 regs
->r0
= __NR_restart_syscall
;
598 /* If there's no signal to deliver, we just restore the saved mask. */
599 if (test_thread_flag(TIF_RESTORE_SIGMASK
)) {
600 clear_thread_flag(TIF_RESTORE_SIGMASK
);
601 sigprocmask(SIG_SETMASK
, ¤t
->saved_sigmask
, NULL
);
605 ptrace_set_bpt(current
); /* re-set breakpoint */
609 do_notify_resume(struct pt_regs
*regs
, struct switch_stack
*sw
,
610 unsigned long thread_info_flags
,
611 unsigned long r0
, unsigned long r19
)
613 if (thread_info_flags
& (_TIF_SIGPENDING
| _TIF_RESTORE_SIGMASK
))
614 do_signal(regs
, sw
, r0
, r19
);
616 if (thread_info_flags
& _TIF_NOTIFY_RESUME
) {
617 clear_thread_flag(TIF_NOTIFY_RESUME
);
618 tracehook_notify_resume(regs
);
619 if (current
->replacement_session_keyring
)
620 key_replace_session_keyring();