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);
39 * The OSF/1 sigprocmask calling sequence is different from the
40 * C sigprocmask() sequence..
42 SYSCALL_DEFINE2(osf_sigprocmask
, int, how
, unsigned long, newmask
)
48 siginitset(&mask
, newmask
& _BLOCKABLE
);
49 res
= sigprocmask(how
, &mask
, &oldmask
);
51 force_successful_syscall_return();
57 SYSCALL_DEFINE3(osf_sigaction
, int, sig
,
58 const struct osf_sigaction __user
*, act
,
59 struct osf_sigaction __user
*, oact
)
61 struct k_sigaction new_ka
, old_ka
;
66 if (!access_ok(VERIFY_READ
, act
, sizeof(*act
)) ||
67 __get_user(new_ka
.sa
.sa_handler
, &act
->sa_handler
) ||
68 __get_user(new_ka
.sa
.sa_flags
, &act
->sa_flags
) ||
69 __get_user(mask
, &act
->sa_mask
))
71 siginitset(&new_ka
.sa
.sa_mask
, mask
);
72 new_ka
.ka_restorer
= NULL
;
75 ret
= do_sigaction(sig
, act
? &new_ka
: NULL
, oact
? &old_ka
: NULL
);
78 if (!access_ok(VERIFY_WRITE
, oact
, sizeof(*oact
)) ||
79 __put_user(old_ka
.sa
.sa_handler
, &oact
->sa_handler
) ||
80 __put_user(old_ka
.sa
.sa_flags
, &oact
->sa_flags
) ||
81 __put_user(old_ka
.sa
.sa_mask
.sig
[0], &oact
->sa_mask
))
88 SYSCALL_DEFINE5(rt_sigaction
, int, sig
, const struct sigaction __user
*, act
,
89 struct sigaction __user
*, oact
,
90 size_t, sigsetsize
, void __user
*, restorer
)
92 struct k_sigaction new_ka
, old_ka
;
95 /* XXX: Don't preclude handling different sized sigset_t's. */
96 if (sigsetsize
!= sizeof(sigset_t
))
100 new_ka
.ka_restorer
= restorer
;
101 if (copy_from_user(&new_ka
.sa
, act
, sizeof(*act
)))
105 ret
= do_sigaction(sig
, act
? &new_ka
: NULL
, oact
? &old_ka
: NULL
);
108 if (copy_to_user(oact
, &old_ka
.sa
, sizeof(*oact
)))
116 * Atomically swap in the new signal mask, and wait for a signal.
118 SYSCALL_DEFINE1(sigsuspend
, old_sigset_t
, mask
)
121 siginitset(&blocked
, mask
);
122 return sigsuspend(&blocked
);
126 sys_sigaltstack(const stack_t __user
*uss
, stack_t __user
*uoss
)
128 return do_sigaltstack(uss
, uoss
, rdusp());
132 * Do a signal return; undo the signal stack.
136 # error "Non SA_SIGINFO frame needs rearranging"
141 struct sigcontext sc
;
142 unsigned int retcode
[3];
149 unsigned int retcode
[3];
152 /* If this changes, userland unwinders that Know Things about our signal
153 frame will break. Do not undertake lightly. It also implies an ABI
154 change wrt the size of siginfo_t, which may cause some pain. */
155 extern char compile_time_assert
156 [offsetof(struct rt_sigframe
, uc
.uc_mcontext
) == 176 ? 1 : -1];
158 #define INSN_MOV_R30_R16 0x47fe0410
159 #define INSN_LDI_R0 0x201f0000
160 #define INSN_CALLSYS 0x00000083
163 restore_sigcontext(struct sigcontext __user
*sc
, struct pt_regs
*regs
,
164 struct switch_stack
*sw
)
167 long i
, err
= __get_user(regs
->pc
, &sc
->sc_pc
);
169 current_thread_info()->restart_block
.fn
= do_no_restart_syscall
;
171 sw
->r26
= (unsigned long) ret_from_sys_call
;
173 err
|= __get_user(regs
->r0
, sc
->sc_regs
+0);
174 err
|= __get_user(regs
->r1
, sc
->sc_regs
+1);
175 err
|= __get_user(regs
->r2
, sc
->sc_regs
+2);
176 err
|= __get_user(regs
->r3
, sc
->sc_regs
+3);
177 err
|= __get_user(regs
->r4
, sc
->sc_regs
+4);
178 err
|= __get_user(regs
->r5
, sc
->sc_regs
+5);
179 err
|= __get_user(regs
->r6
, sc
->sc_regs
+6);
180 err
|= __get_user(regs
->r7
, sc
->sc_regs
+7);
181 err
|= __get_user(regs
->r8
, sc
->sc_regs
+8);
182 err
|= __get_user(sw
->r9
, sc
->sc_regs
+9);
183 err
|= __get_user(sw
->r10
, sc
->sc_regs
+10);
184 err
|= __get_user(sw
->r11
, sc
->sc_regs
+11);
185 err
|= __get_user(sw
->r12
, sc
->sc_regs
+12);
186 err
|= __get_user(sw
->r13
, sc
->sc_regs
+13);
187 err
|= __get_user(sw
->r14
, sc
->sc_regs
+14);
188 err
|= __get_user(sw
->r15
, sc
->sc_regs
+15);
189 err
|= __get_user(regs
->r16
, sc
->sc_regs
+16);
190 err
|= __get_user(regs
->r17
, sc
->sc_regs
+17);
191 err
|= __get_user(regs
->r18
, sc
->sc_regs
+18);
192 err
|= __get_user(regs
->r19
, sc
->sc_regs
+19);
193 err
|= __get_user(regs
->r20
, sc
->sc_regs
+20);
194 err
|= __get_user(regs
->r21
, sc
->sc_regs
+21);
195 err
|= __get_user(regs
->r22
, sc
->sc_regs
+22);
196 err
|= __get_user(regs
->r23
, sc
->sc_regs
+23);
197 err
|= __get_user(regs
->r24
, sc
->sc_regs
+24);
198 err
|= __get_user(regs
->r25
, sc
->sc_regs
+25);
199 err
|= __get_user(regs
->r26
, sc
->sc_regs
+26);
200 err
|= __get_user(regs
->r27
, sc
->sc_regs
+27);
201 err
|= __get_user(regs
->r28
, sc
->sc_regs
+28);
202 err
|= __get_user(regs
->gp
, sc
->sc_regs
+29);
203 err
|= __get_user(usp
, sc
->sc_regs
+30);
206 for (i
= 0; i
< 31; i
++)
207 err
|= __get_user(sw
->fp
[i
], sc
->sc_fpregs
+i
);
208 err
|= __get_user(sw
->fp
[31], &sc
->sc_fpcr
);
213 /* Note that this syscall is also used by setcontext(3) to install
214 a given sigcontext. This because it's impossible to set *all*
215 registers and transfer control from userland. */
218 do_sigreturn(struct sigcontext __user
*sc
, struct pt_regs
*regs
,
219 struct switch_stack
*sw
)
223 /* Verify that it's a good sigcontext before using it */
224 if (!access_ok(VERIFY_READ
, sc
, sizeof(*sc
)))
226 if (__get_user(set
.sig
[0], &sc
->sc_mask
))
229 set_current_blocked(&set
);
231 if (restore_sigcontext(sc
, regs
, sw
))
234 /* Send SIGTRAP if we're single-stepping: */
235 if (ptrace_cancel_bpt (current
)) {
238 info
.si_signo
= SIGTRAP
;
240 info
.si_code
= TRAP_BRKPT
;
241 info
.si_addr
= (void __user
*) regs
->pc
;
243 send_sig_info(SIGTRAP
, &info
, current
);
248 force_sig(SIGSEGV
, current
);
252 do_rt_sigreturn(struct rt_sigframe __user
*frame
, struct pt_regs
*regs
,
253 struct switch_stack
*sw
)
257 /* Verify that it's a good ucontext_t before using it */
258 if (!access_ok(VERIFY_READ
, &frame
->uc
, sizeof(frame
->uc
)))
260 if (__copy_from_user(&set
, &frame
->uc
.uc_sigmask
, sizeof(set
)))
263 set_current_blocked(&set
);
265 if (restore_sigcontext(&frame
->uc
.uc_mcontext
, regs
, sw
))
268 /* Send SIGTRAP if we're single-stepping: */
269 if (ptrace_cancel_bpt (current
)) {
272 info
.si_signo
= SIGTRAP
;
274 info
.si_code
= TRAP_BRKPT
;
275 info
.si_addr
= (void __user
*) regs
->pc
;
277 send_sig_info(SIGTRAP
, &info
, current
);
282 force_sig(SIGSEGV
, current
);
287 * Set up a signal frame.
290 static inline void __user
*
291 get_sigframe(struct k_sigaction
*ka
, unsigned long sp
, size_t frame_size
)
293 if ((ka
->sa
.sa_flags
& SA_ONSTACK
) != 0 && ! sas_ss_flags(sp
))
294 sp
= current
->sas_ss_sp
+ current
->sas_ss_size
;
296 return (void __user
*)((sp
- frame_size
) & -32ul);
300 setup_sigcontext(struct sigcontext __user
*sc
, struct pt_regs
*regs
,
301 struct switch_stack
*sw
, unsigned long mask
, unsigned long sp
)
305 err
|= __put_user(on_sig_stack((unsigned long)sc
), &sc
->sc_onstack
);
306 err
|= __put_user(mask
, &sc
->sc_mask
);
307 err
|= __put_user(regs
->pc
, &sc
->sc_pc
);
308 err
|= __put_user(8, &sc
->sc_ps
);
310 err
|= __put_user(regs
->r0
, sc
->sc_regs
+0);
311 err
|= __put_user(regs
->r1
, sc
->sc_regs
+1);
312 err
|= __put_user(regs
->r2
, sc
->sc_regs
+2);
313 err
|= __put_user(regs
->r3
, sc
->sc_regs
+3);
314 err
|= __put_user(regs
->r4
, sc
->sc_regs
+4);
315 err
|= __put_user(regs
->r5
, sc
->sc_regs
+5);
316 err
|= __put_user(regs
->r6
, sc
->sc_regs
+6);
317 err
|= __put_user(regs
->r7
, sc
->sc_regs
+7);
318 err
|= __put_user(regs
->r8
, sc
->sc_regs
+8);
319 err
|= __put_user(sw
->r9
, sc
->sc_regs
+9);
320 err
|= __put_user(sw
->r10
, sc
->sc_regs
+10);
321 err
|= __put_user(sw
->r11
, sc
->sc_regs
+11);
322 err
|= __put_user(sw
->r12
, sc
->sc_regs
+12);
323 err
|= __put_user(sw
->r13
, sc
->sc_regs
+13);
324 err
|= __put_user(sw
->r14
, sc
->sc_regs
+14);
325 err
|= __put_user(sw
->r15
, sc
->sc_regs
+15);
326 err
|= __put_user(regs
->r16
, sc
->sc_regs
+16);
327 err
|= __put_user(regs
->r17
, sc
->sc_regs
+17);
328 err
|= __put_user(regs
->r18
, sc
->sc_regs
+18);
329 err
|= __put_user(regs
->r19
, sc
->sc_regs
+19);
330 err
|= __put_user(regs
->r20
, sc
->sc_regs
+20);
331 err
|= __put_user(regs
->r21
, sc
->sc_regs
+21);
332 err
|= __put_user(regs
->r22
, sc
->sc_regs
+22);
333 err
|= __put_user(regs
->r23
, sc
->sc_regs
+23);
334 err
|= __put_user(regs
->r24
, sc
->sc_regs
+24);
335 err
|= __put_user(regs
->r25
, sc
->sc_regs
+25);
336 err
|= __put_user(regs
->r26
, sc
->sc_regs
+26);
337 err
|= __put_user(regs
->r27
, sc
->sc_regs
+27);
338 err
|= __put_user(regs
->r28
, sc
->sc_regs
+28);
339 err
|= __put_user(regs
->gp
, sc
->sc_regs
+29);
340 err
|= __put_user(sp
, sc
->sc_regs
+30);
341 err
|= __put_user(0, sc
->sc_regs
+31);
343 for (i
= 0; i
< 31; i
++)
344 err
|= __put_user(sw
->fp
[i
], sc
->sc_fpregs
+i
);
345 err
|= __put_user(0, sc
->sc_fpregs
+31);
346 err
|= __put_user(sw
->fp
[31], &sc
->sc_fpcr
);
348 err
|= __put_user(regs
->trap_a0
, &sc
->sc_traparg_a0
);
349 err
|= __put_user(regs
->trap_a1
, &sc
->sc_traparg_a1
);
350 err
|= __put_user(regs
->trap_a2
, &sc
->sc_traparg_a2
);
356 setup_frame(int sig
, struct k_sigaction
*ka
, sigset_t
*set
,
357 struct pt_regs
*regs
, struct switch_stack
* sw
)
359 unsigned long oldsp
, r26
, err
= 0;
360 struct sigframe __user
*frame
;
363 frame
= get_sigframe(ka
, oldsp
, sizeof(*frame
));
364 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
)))
367 err
|= setup_sigcontext(&frame
->sc
, regs
, sw
, set
->sig
[0], oldsp
);
371 /* Set up to return from userspace. If provided, use a stub
372 already in userspace. */
373 if (ka
->ka_restorer
) {
374 r26
= (unsigned long) ka
->ka_restorer
;
376 err
|= __put_user(INSN_MOV_R30_R16
, frame
->retcode
+0);
377 err
|= __put_user(INSN_LDI_R0
+__NR_sigreturn
, frame
->retcode
+1);
378 err
|= __put_user(INSN_CALLSYS
, frame
->retcode
+2);
380 r26
= (unsigned long) frame
->retcode
;
383 /* Check that everything was written properly. */
387 /* "Return" to the handler */
389 regs
->r27
= regs
->pc
= (unsigned long) ka
->sa
.sa_handler
;
390 regs
->r16
= sig
; /* a0: signal number */
391 regs
->r17
= 0; /* a1: exception code */
392 regs
->r18
= (unsigned long) &frame
->sc
; /* a2: sigcontext pointer */
393 wrusp((unsigned long) frame
);
396 printk("SIG deliver (%s:%d): sp=%p pc=%p ra=%p\n",
397 current
->comm
, current
->pid
, frame
, regs
->pc
, regs
->r26
);
403 setup_rt_frame(int sig
, struct k_sigaction
*ka
, siginfo_t
*info
,
404 sigset_t
*set
, struct pt_regs
*regs
, struct switch_stack
* sw
)
406 unsigned long oldsp
, r26
, err
= 0;
407 struct rt_sigframe __user
*frame
;
410 frame
= get_sigframe(ka
, oldsp
, sizeof(*frame
));
411 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
)))
414 err
|= copy_siginfo_to_user(&frame
->info
, info
);
416 /* Create the ucontext. */
417 err
|= __put_user(0, &frame
->uc
.uc_flags
);
418 err
|= __put_user(0, &frame
->uc
.uc_link
);
419 err
|= __put_user(set
->sig
[0], &frame
->uc
.uc_osf_sigmask
);
420 err
|= __put_user(current
->sas_ss_sp
, &frame
->uc
.uc_stack
.ss_sp
);
421 err
|= __put_user(sas_ss_flags(oldsp
), &frame
->uc
.uc_stack
.ss_flags
);
422 err
|= __put_user(current
->sas_ss_size
, &frame
->uc
.uc_stack
.ss_size
);
423 err
|= setup_sigcontext(&frame
->uc
.uc_mcontext
, regs
, sw
,
425 err
|= __copy_to_user(&frame
->uc
.uc_sigmask
, set
, sizeof(*set
));
429 /* Set up to return from userspace. If provided, use a stub
430 already in userspace. */
431 if (ka
->ka_restorer
) {
432 r26
= (unsigned long) ka
->ka_restorer
;
434 err
|= __put_user(INSN_MOV_R30_R16
, frame
->retcode
+0);
435 err
|= __put_user(INSN_LDI_R0
+__NR_rt_sigreturn
,
437 err
|= __put_user(INSN_CALLSYS
, frame
->retcode
+2);
439 r26
= (unsigned long) frame
->retcode
;
445 /* "Return" to the handler */
447 regs
->r27
= regs
->pc
= (unsigned long) ka
->sa
.sa_handler
;
448 regs
->r16
= sig
; /* a0: signal number */
449 regs
->r17
= (unsigned long) &frame
->info
; /* a1: siginfo pointer */
450 regs
->r18
= (unsigned long) &frame
->uc
; /* a2: ucontext pointer */
451 wrusp((unsigned long) frame
);
454 printk("SIG deliver (%s:%d): sp=%p pc=%p ra=%p\n",
455 current
->comm
, current
->pid
, frame
, regs
->pc
, regs
->r26
);
463 * OK, we're invoking a handler.
466 handle_signal(int sig
, struct k_sigaction
*ka
, siginfo_t
*info
,
467 struct pt_regs
* regs
, struct switch_stack
*sw
)
469 sigset_t
*oldset
= sigmask_to_save();
472 if (ka
->sa
.sa_flags
& SA_SIGINFO
)
473 ret
= setup_rt_frame(sig
, ka
, info
, oldset
, regs
, sw
);
475 ret
= setup_frame(sig
, ka
, oldset
, regs
, sw
);
478 force_sigsegv(sig
, current
);
481 signal_delivered(sig
, info
, ka
, regs
, 0);
485 syscall_restart(unsigned long r0
, unsigned long r19
,
486 struct pt_regs
*regs
, struct k_sigaction
*ka
)
490 if (!(ka
->sa
.sa_flags
& SA_RESTART
)) {
497 regs
->r0
= r0
; /* reset v0 and a3 and replay syscall */
501 case ERESTART_RESTARTBLOCK
:
509 * Note that 'init' is a special process: it doesn't get signals it doesn't
510 * want to handle. Thus you cannot kill init even with a SIGKILL even by
513 * Note that we go through the signals twice: once to check the signals that
514 * the kernel can handle, and then we build all the user-level signal handling
515 * stack-frames in one go after that.
517 * "r0" and "r19" are the registers we need to restore for system call
518 * restart. "r0" is also used as an indicator whether we can restart at
519 * all (if we get here from anything but a syscall return, it will be 0)
522 do_signal(struct pt_regs
* regs
, struct switch_stack
* sw
,
523 unsigned long r0
, unsigned long r19
)
527 unsigned long single_stepping
= ptrace_cancel_bpt(current
);
528 struct k_sigaction ka
;
530 /* This lets the debugger run, ... */
531 signr
= get_signal_to_deliver(&info
, &ka
, regs
, NULL
);
533 /* ... so re-check the single stepping. */
534 single_stepping
|= ptrace_cancel_bpt(current
);
537 /* Whee! Actually deliver the signal. */
539 syscall_restart(r0
, r19
, regs
, &ka
);
540 handle_signal(signr
, &ka
, &info
, regs
, sw
);
542 ptrace_set_bpt(current
); /* re-set bpt */
551 /* Reset v0 and a3 and replay syscall. */
556 case ERESTART_RESTARTBLOCK
:
557 /* Force v0 to the restart syscall and reply. */
558 regs
->r0
= __NR_restart_syscall
;
564 /* If there's no signal to deliver, we just restore the saved mask. */
565 restore_saved_sigmask();
567 ptrace_set_bpt(current
); /* re-set breakpoint */
571 do_notify_resume(struct pt_regs
*regs
, struct switch_stack
*sw
,
572 unsigned long thread_info_flags
,
573 unsigned long r0
, unsigned long r19
)
575 if (thread_info_flags
& _TIF_SIGPENDING
)
576 do_signal(regs
, sw
, r0
, r19
);
578 if (thread_info_flags
& _TIF_NOTIFY_RESUME
) {
579 clear_thread_flag(TIF_NOTIFY_RESUME
);
580 tracehook_notify_resume(regs
);