2 * Architecture-specific signal handling support.
4 * Copyright (C) 1999-2000 Hewlett-Packard Co
5 * Copyright (C) 1999-2000 David Mosberger-Tang <davidm@hpl.hp.com>
7 * Derived from i386 and Alpha versions.
10 #include <linux/config.h>
11 #include <linux/errno.h>
12 #include <linux/kernel.h>
14 #include <linux/ptrace.h>
15 #include <linux/sched.h>
16 #include <linux/signal.h>
17 #include <linux/smp.h>
18 #include <linux/smp_lock.h>
19 #include <linux/stddef.h>
20 #include <linux/unistd.h>
21 #include <linux/wait.h>
24 #include <asm/uaccess.h>
26 #include <asm/sigcontext.h>
29 #define STACK_ALIGN 16 /* minimal alignment for stack pointer */
30 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
33 # define PUT_SIGSET(k,u) __copy_to_user((u)->sig, (k)->sig, sizeof(sigset_t))
34 # define GET_SIGSET(k,u) __copy_from_user((k)->sig, (u)->sig, sizeof(sigset_t))
36 # define PUT_SIGSET(k,u) __put_user((k)->sig[0], &(u)->sig[0])
37 # define GET_SIGSET(k,u) __get_user((k)->sig[0], &(u)->sig[0])
41 #ifdef CONFIG_IA64_NEW_UNWIND
42 unsigned long scratch_unat
; /* ar.unat for the general registers saved in pt */
45 struct switch_stack sw
;
55 extern long sys_wait4 (int, int *, int, struct rusage
*);
56 extern long ia64_do_signal (sigset_t
*, struct sigscratch
*, long); /* forward decl */
59 ia64_rt_sigsuspend (sigset_t
*uset
, size_t sigsetsize
, struct sigscratch
*scr
)
63 /* XXX: Don't preclude handling different sized sigset_t's. */
64 if (sigsetsize
!= sizeof(sigset_t
))
66 if (GET_SIGSET(&set
, uset
))
69 sigdelsetmask(&set
, ~_BLOCKABLE
);
71 spin_lock_irq(¤t
->sigmask_lock
);
73 oldset
= current
->blocked
;
74 current
->blocked
= set
;
75 recalc_sigpending(current
);
77 spin_unlock_irq(¤t
->sigmask_lock
);
80 * The return below usually returns to the signal handler. We need to
81 * pre-set the correct error code here to ensure that the right values
82 * get saved in sigcontext by ia64_do_signal.
84 #ifdef CONFIG_IA32_SUPPORT
85 if (IS_IA32_PROCESS(&scr
->pt
)) {
94 set_current_state(TASK_INTERRUPTIBLE
);
96 if (ia64_do_signal(&oldset
, scr
, 1))
102 sys_sigaltstack (const stack_t
*uss
, stack_t
*uoss
, long arg2
, long arg3
, long arg4
,
103 long arg5
, long arg6
, long arg7
, long stack
)
105 struct pt_regs
*pt
= (struct pt_regs
*) &stack
;
107 return do_sigaltstack(uss
, uoss
, pt
->r12
);
111 restore_sigcontext (struct sigcontext
*sc
, struct sigscratch
*scr
)
113 unsigned long ip
, flags
, nat
, um
, cfm
;
116 /* restore scratch that always needs gets updated during signal delivery: */
117 err
= __get_user(flags
, &sc
->sc_flags
);
119 err
|= __get_user(nat
, &sc
->sc_nat
);
120 err
|= __get_user(ip
, &sc
->sc_ip
); /* instruction pointer */
121 err
|= __get_user(cfm
, &sc
->sc_cfm
);
122 err
|= __get_user(um
, &sc
->sc_um
); /* user mask */
123 err
|= __get_user(scr
->pt
.ar_rsc
, &sc
->sc_ar_rsc
);
124 err
|= __get_user(scr
->pt
.ar_ccv
, &sc
->sc_ar_ccv
);
125 err
|= __get_user(scr
->pt
.ar_unat
, &sc
->sc_ar_unat
);
126 err
|= __get_user(scr
->pt
.ar_fpsr
, &sc
->sc_ar_fpsr
);
127 err
|= __get_user(scr
->pt
.ar_pfs
, &sc
->sc_ar_pfs
);
128 err
|= __get_user(scr
->pt
.pr
, &sc
->sc_pr
); /* predicates */
129 err
|= __get_user(scr
->pt
.b0
, &sc
->sc_br
[0]); /* b0 (rp) */
130 err
|= __get_user(scr
->pt
.b6
, &sc
->sc_br
[6]); /* b6 */
131 err
|= __get_user(scr
->pt
.b7
, &sc
->sc_br
[7]); /* b7 */
132 err
|= __copy_from_user(&scr
->pt
.r1
, &sc
->sc_gr
[1], 3*8); /* r1-r3 */
133 err
|= __copy_from_user(&scr
->pt
.r8
, &sc
->sc_gr
[8], 4*8); /* r8-r11 */
134 err
|= __copy_from_user(&scr
->pt
.r12
, &sc
->sc_gr
[12], 4*8); /* r12-r15 */
135 err
|= __copy_from_user(&scr
->pt
.r16
, &sc
->sc_gr
[16], 16*8); /* r16-r31 */
137 scr
->pt
.cr_ifs
= cfm
| (1UL << 63);
139 /* establish new instruction pointer: */
140 scr
->pt
.cr_iip
= ip
& ~0x3UL
;
141 ia64_psr(&scr
->pt
)->ri
= ip
& 0x3;
142 scr
->pt
.cr_ipsr
= (scr
->pt
.cr_ipsr
& ~IA64_PSR_UM
) | (um
& IA64_PSR_UM
);
144 #ifdef CONFIG_IA64_NEW_UNWIND
145 scr
->scratch_unat
= ia64_put_scratch_nat_bits(&scr
->pt
, nat
);
147 ia64_put_nat_bits(&scr
->pt
, &scr
->sw
, nat
); /* restore the original scratch NaT bits */
150 if (flags
& IA64_SC_FLAG_FPH_VALID
) {
151 struct task_struct
*fpu_owner
= ia64_get_fpu_owner();
153 __copy_from_user(current
->thread
.fph
, &sc
->sc_fr
[32], 96*16);
154 if (fpu_owner
== current
) {
155 __ia64_load_fpu(current
->thread
.fph
);
162 copy_siginfo_to_user (siginfo_t
*to
, siginfo_t
*from
)
164 if (!access_ok (VERIFY_WRITE
, to
, sizeof(siginfo_t
)))
166 if (from
->si_code
< 0)
167 return __copy_to_user(to
, from
, sizeof(siginfo_t
));
172 * If you change siginfo_t structure, please be sure
173 * this code is fixed accordingly. It should never
174 * copy any pad contained in the structure to avoid
175 * security leaks, but must copy the generic 3 ints
176 * plus the relevant union member.
178 err
= __put_user(from
->si_signo
, &to
->si_signo
);
179 err
|= __put_user(from
->si_errno
, &to
->si_errno
);
180 err
|= __put_user((short)from
->si_code
, &to
->si_code
);
181 switch (from
->si_code
>> 16) {
182 case __SI_FAULT
>> 16:
183 err
|= __put_user(from
->si_isr
, &to
->si_isr
);
184 case __SI_POLL
>> 16:
185 err
|= __put_user(from
->si_addr
, &to
->si_addr
);
186 err
|= __put_user(from
->si_imm
, &to
->si_imm
);
188 case __SI_CHLD
>> 16:
189 err
|= __put_user(from
->si_utime
, &to
->si_utime
);
190 err
|= __put_user(from
->si_stime
, &to
->si_stime
);
191 err
|= __put_user(from
->si_status
, &to
->si_status
);
193 err
|= __put_user(from
->si_uid
, &to
->si_uid
);
194 err
|= __put_user(from
->si_pid
, &to
->si_pid
);
196 /* case __SI_RT: This is not generated by the kernel as of now. */
203 ia64_rt_sigreturn (struct sigscratch
*scr
)
205 extern char ia64_strace_leave_kernel
, ia64_leave_kernel
;
206 struct sigcontext
*sc
;
211 sc
= &((struct sigframe
*) (scr
->pt
.r12
+ 16))->sc
;
214 * When we return to the previously executing context, r8 and
215 * r10 have already been setup the way we want them. Indeed,
216 * if the signal wasn't delivered while in a system call, we
217 * must not touch r8 or r10 as otherwise user-level stat could
220 retval
= (long) &ia64_leave_kernel
;
221 if (current
->ptrace
& PT_TRACESYS
)
223 * strace expects to be notified after sigreturn
224 * returns even though the context to which we return
225 * may not be in the middle of a syscall. Thus, the
226 * return-value that strace displays for sigreturn is
229 retval
= (long) &ia64_strace_leave_kernel
;
231 if (!access_ok(VERIFY_READ
, sc
, sizeof(*sc
)))
234 if (GET_SIGSET(&set
, &sc
->sc_mask
))
237 sigdelsetmask(&set
, ~_BLOCKABLE
);
238 spin_lock_irq(¤t
->sigmask_lock
);
239 current
->blocked
= set
;
240 recalc_sigpending(current
);
241 spin_unlock_irq(¤t
->sigmask_lock
);
243 if (restore_sigcontext(sc
, scr
))
247 printk("SIG return (%s:%d): sp=%lx ip=%lx\n",
248 current
->comm
, current
->pid
, scr
->pt
.r12
, scr
->pt
.cr_iip
);
251 * It is more difficult to avoid calling this function than to
252 * call it and ignore errors.
254 do_sigaltstack(&sc
->sc_stack
, 0, scr
->pt
.r12
);
258 si
.si_signo
= SIGSEGV
;
260 si
.si_code
= SI_KERNEL
;
261 si
.si_pid
= current
->pid
;
262 si
.si_uid
= current
->uid
;
264 force_sig_info(SIGSEGV
, &si
, current
);
269 * This does just the minimum required setup of sigcontext.
270 * Specifically, it only installs data that is either not knowable at
271 * the user-level or that gets modified before execution in the
272 * trampoline starts. Everything else is done at the user-level.
275 setup_sigcontext (struct sigcontext
*sc
, sigset_t
*mask
, struct sigscratch
*scr
)
277 struct task_struct
*fpu_owner
= ia64_get_fpu_owner();
278 unsigned long flags
= 0, ifs
, nat
;
281 ifs
= scr
->pt
.cr_ifs
;
283 if (on_sig_stack((unsigned long) sc
))
284 flags
|= IA64_SC_FLAG_ONSTACK
;
285 if ((ifs
& (1UL << 63)) == 0) {
286 /* if cr_ifs isn't valid, we got here through a syscall */
287 flags
|= IA64_SC_FLAG_IN_SYSCALL
;
289 if ((fpu_owner
== current
) || (current
->thread
.flags
& IA64_THREAD_FPH_VALID
)) {
290 flags
|= IA64_SC_FLAG_FPH_VALID
;
291 if (fpu_owner
== current
) {
292 __ia64_save_fpu(current
->thread
.fph
);
294 __copy_to_user(&sc
->sc_fr
[32], current
->thread
.fph
, 96*16);
298 * Note: sw->ar_unat is UNDEFINED unless the process is being
299 * PTRACED. However, this is OK because the NaT bits of the
300 * preserved registers (r4-r7) are never being looked at by
301 * the signal handler (registers r4-r7 are used instead).
303 #ifdef CONFIG_IA64_NEW_UNWIND
304 nat
= ia64_get_scratch_nat_bits(&scr
->pt
, scr
->scratch_unat
);
306 nat
= ia64_get_nat_bits(&scr
->pt
, &scr
->sw
);
309 err
= __put_user(flags
, &sc
->sc_flags
);
311 err
|= __put_user(nat
, &sc
->sc_nat
);
312 err
|= PUT_SIGSET(mask
, &sc
->sc_mask
);
313 err
|= __put_user(scr
->pt
.cr_ipsr
& IA64_PSR_UM
, &sc
->sc_um
);
314 err
|= __put_user(scr
->pt
.ar_rsc
, &sc
->sc_ar_rsc
);
315 err
|= __put_user(scr
->pt
.ar_ccv
, &sc
->sc_ar_ccv
);
316 err
|= __put_user(scr
->pt
.ar_unat
, &sc
->sc_ar_unat
); /* ar.unat */
317 err
|= __put_user(scr
->pt
.ar_fpsr
, &sc
->sc_ar_fpsr
); /* ar.fpsr */
318 err
|= __put_user(scr
->pt
.ar_pfs
, &sc
->sc_ar_pfs
);
319 err
|= __put_user(scr
->pt
.pr
, &sc
->sc_pr
); /* predicates */
320 err
|= __put_user(scr
->pt
.b0
, &sc
->sc_br
[0]); /* b0 (rp) */
321 err
|= __put_user(scr
->pt
.b6
, &sc
->sc_br
[6]); /* b6 */
322 err
|= __put_user(scr
->pt
.b7
, &sc
->sc_br
[7]); /* b7 */
324 err
|= __copy_to_user(&sc
->sc_gr
[1], &scr
->pt
.r1
, 3*8); /* r1-r3 */
325 err
|= __copy_to_user(&sc
->sc_gr
[8], &scr
->pt
.r8
, 4*8); /* r8-r11 */
326 err
|= __copy_to_user(&sc
->sc_gr
[12], &scr
->pt
.r12
, 4*8); /* r12-r15 */
327 err
|= __copy_to_user(&sc
->sc_gr
[16], &scr
->pt
.r16
, 16*8); /* r16-r31 */
329 err
|= __put_user(scr
->pt
.cr_iip
+ ia64_psr(&scr
->pt
)->ri
, &sc
->sc_ip
);
334 setup_frame (int sig
, struct k_sigaction
*ka
, siginfo_t
*info
, sigset_t
*set
,
335 struct sigscratch
*scr
)
337 extern char ia64_sigtramp
[], __start_gate_section
[];
338 unsigned long tramp_addr
, new_rbs
= 0;
339 struct sigframe
*frame
;
343 frame
= (void *) scr
->pt
.r12
;
344 tramp_addr
= GATE_ADDR
+ (ia64_sigtramp
- __start_gate_section
);
345 if ((ka
->sa
.sa_flags
& SA_ONSTACK
) != 0 && !on_sig_stack((unsigned long) frame
)) {
346 new_rbs
= (current
->sas_ss_sp
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
347 frame
= (void *) ((current
->sas_ss_sp
+ current
->sas_ss_size
)
348 & ~(STACK_ALIGN
- 1));
350 frame
= (void *) frame
- ((sizeof(*frame
) + STACK_ALIGN
- 1) & ~(STACK_ALIGN
- 1));
352 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
)))
355 err
= copy_siginfo_to_user(&frame
->info
, info
);
357 err
|= __put_user(current
->sas_ss_sp
, &frame
->sc
.sc_stack
.ss_sp
);
358 err
|= __put_user(current
->sas_ss_size
, &frame
->sc
.sc_stack
.ss_size
);
359 err
|= __put_user(sas_ss_flags(scr
->pt
.r12
), &frame
->sc
.sc_stack
.ss_flags
);
360 err
|= setup_sigcontext(&frame
->sc
, set
, scr
);
365 scr
->pt
.r12
= (unsigned long) frame
- 16; /* new stack pointer */
366 scr
->pt
.r2
= sig
; /* signal number */
367 scr
->pt
.r3
= (unsigned long) ka
->sa
.sa_handler
; /* addr. of handler's proc desc */
368 scr
->pt
.r15
= new_rbs
;
369 scr
->pt
.ar_fpsr
= FPSR_DEFAULT
; /* reset fpsr for signal handler */
370 scr
->pt
.cr_iip
= tramp_addr
;
371 ia64_psr(&scr
->pt
)->ri
= 0; /* start executing in first slot */
373 #ifdef CONFIG_IA64_NEW_UNWIND
375 * Note: this affects only the NaT bits of the scratch regs
376 * (the ones saved in pt_regs), which is exactly what we want.
378 scr
->scratch_unat
= 0; /* ensure NaT bits of at least r2, r3, r12, and r15 are clear */
381 * Note: this affects only the NaT bits of the scratch regs
382 * (the ones saved in pt_regs), which is exactly what we want.
383 * The NaT bits for the preserved regs (r4-r7) are in
384 * sw->ar_unat iff this process is being PTRACED.
386 scr
->sw
.caller_unat
= 0; /* ensure NaT bits of at least r2, r3, r12, and r15 are clear */
390 printk("SIG deliver (%s:%d): sig=%d sp=%lx ip=%lx handler=%lx\n",
391 current
->comm
, current
->pid
, sig
, scr
->pt
.r12
, scr
->pt
.cr_iip
, scr
->pt
.r3
);
397 ka
->sa
.sa_handler
= SIG_DFL
;
398 si
.si_signo
= SIGSEGV
;
400 si
.si_code
= SI_KERNEL
;
401 si
.si_pid
= current
->pid
;
402 si
.si_uid
= current
->uid
;
404 force_sig_info(SIGSEGV
, &si
, current
);
409 handle_signal (unsigned long sig
, struct k_sigaction
*ka
, siginfo_t
*info
, sigset_t
*oldset
,
410 struct sigscratch
*scr
)
412 #ifdef CONFIG_IA32_SUPPORT
413 if (IS_IA32_PROCESS(&scr
->pt
)) {
414 /* send signal to IA-32 process */
415 if (!ia32_setup_frame1(sig
, ka
, info
, oldset
, &scr
->pt
))
419 /* send signal to IA-64 process */
420 if (!setup_frame(sig
, ka
, info
, oldset
, scr
))
423 if (ka
->sa
.sa_flags
& SA_ONESHOT
)
424 ka
->sa
.sa_handler
= SIG_DFL
;
426 if (!(ka
->sa
.sa_flags
& SA_NODEFER
)) {
427 spin_lock_irq(¤t
->sigmask_lock
);
428 sigorsets(¤t
->blocked
, ¤t
->blocked
, &ka
->sa
.sa_mask
);
429 sigaddset(¤t
->blocked
, sig
);
430 recalc_sigpending(current
);
431 spin_unlock_irq(¤t
->sigmask_lock
);
437 * Note that `init' is a special process: it doesn't get signals it
438 * doesn't want to handle. Thus you cannot kill init even with a
439 * SIGKILL even by mistake.
441 * Note that we go through the signals twice: once to check the
442 * signals that the kernel can handle, and then we build all the
443 * user-level signal handling stack-frames in one go after that.
446 ia64_do_signal (sigset_t
*oldset
, struct sigscratch
*scr
, long in_syscall
)
448 struct k_sigaction
*ka
;
450 long restart
= in_syscall
;
451 long errno
= scr
->pt
.r8
;
454 * In the ia64_leave_kernel code path, we want the common case
455 * to go fast, which is why we may in certain cases get here
456 * from kernel mode. Just return without doing anything if so.
458 if (!user_mode(&scr
->pt
))
462 oldset
= ¤t
->blocked
;
464 #ifdef CONFIG_IA32_SUPPORT
465 if (IS_IA32_PROCESS(&scr
->pt
)) {
474 if (scr
->pt
.r10
!= -1) {
476 * A system calls has to be restarted only if one of
477 * the error codes ERESTARTNOHAND, ERESTARTSYS, or
478 * ERESTARTNOINTR is returned. If r10 isn't -1 then
479 * r8 doesn't hold an error code and we don't need to
480 * restart the syscall, so we set in_syscall to zero.
488 spin_lock_irq(¤t
->sigmask_lock
);
489 signr
= dequeue_signal(¤t
->blocked
, &info
);
490 spin_unlock_irq(¤t
->sigmask_lock
);
495 if ((current
->ptrace
& PT_PTRACED
) && signr
!= SIGKILL
) {
496 /* Let the debugger run. */
497 current
->exit_code
= signr
;
498 current
->thread
.siginfo
= &info
;
499 set_current_state(TASK_STOPPED
);
500 notify_parent(current
, SIGCHLD
);
502 signr
= current
->exit_code
;
503 current
->thread
.siginfo
= 0;
505 /* We're back. Did the debugger cancel the sig? */
508 current
->exit_code
= 0;
510 /* The debugger continued. Ignore SIGSTOP. */
511 if (signr
== SIGSTOP
)
514 /* Update the siginfo structure. Is this good? */
515 if (signr
!= info
.si_signo
) {
516 info
.si_signo
= signr
;
518 info
.si_code
= SI_USER
;
519 info
.si_pid
= current
->p_pptr
->pid
;
520 info
.si_uid
= current
->p_pptr
->uid
;
523 /* If the (new) signal is now blocked, requeue it. */
524 if (sigismember(¤t
->blocked
, signr
)) {
525 send_sig_info(signr
, &info
, current
);
530 ka
= ¤t
->sig
->action
[signr
- 1];
531 if (ka
->sa
.sa_handler
== SIG_IGN
) {
532 if (signr
!= SIGCHLD
)
534 /* Check for SIGCHLD: it's special. */
535 while (sys_wait4(-1, NULL
, WNOHANG
, NULL
) > 0)
540 if (ka
->sa
.sa_handler
== SIG_DFL
) {
541 int exit_code
= signr
;
543 /* Init gets no signals it doesn't want. */
544 if (current
->pid
== 1)
548 case SIGCONT
: case SIGCHLD
: case SIGWINCH
:
551 case SIGTSTP
: case SIGTTIN
: case SIGTTOU
:
552 if (is_orphaned_pgrp(current
->pgrp
))
557 set_current_state(TASK_STOPPED
);
558 current
->exit_code
= signr
;
559 if (!(current
->p_pptr
->sig
->action
[SIGCHLD
-1].sa
.sa_flags
561 notify_parent(current
, SIGCHLD
);
565 case SIGQUIT
: case SIGILL
: case SIGTRAP
:
566 case SIGABRT
: case SIGFPE
: case SIGSEGV
:
567 case SIGBUS
: case SIGSYS
: case SIGXCPU
: case SIGXFSZ
:
568 if (do_coredump(signr
, &scr
->pt
))
573 sigaddset(¤t
->pending
.signal
, signr
);
574 recalc_sigpending(current
);
575 current
->flags
|= PF_SIGNALED
;
584 if ((ka
->sa
.sa_flags
& SA_RESTART
) == 0) {
586 #ifdef CONFIG_IA32_SUPPORT
587 if (IS_IA32_PROCESS(&scr
->pt
))
592 /* note: scr->pt.r10 is already -1 */
596 #ifdef CONFIG_IA32_SUPPORT
597 if (IS_IA32_PROCESS(&scr
->pt
)) {
598 scr
->pt
.r8
= scr
->pt
.r1
;
602 ia64_decrement_ip(&scr
->pt
);
606 /* Whee! Actually deliver the signal. If the
607 delivery failed, we need to continue to iterate in
608 this loop so we can deliver the SIGSEGV... */
609 if (handle_signal(signr
, ka
, &info
, oldset
, scr
))
613 /* Did we come from a system call? */
615 /* Restart the system call - no handlers present */
616 if (errno
== ERESTARTNOHAND
|| errno
== ERESTARTSYS
|| errno
== ERESTARTNOINTR
) {
617 #ifdef CONFIG_IA32_SUPPORT
618 if (IS_IA32_PROCESS(&scr
->pt
)) {
619 scr
->pt
.r8
= scr
->pt
.r1
;
624 * Note: the syscall number is in r15 which is
625 * saved in pt_regs so all we need to do here
626 * is adjust ip so that the "break"
627 * instruction gets re-executed.
629 ia64_decrement_ip(&scr
->pt
);