2 * linux/arch/ppc/kernel/signal.c
4 * $Id: signal.c,v 1.27 1999/08/03 19:16:38 cort Exp $
7 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
9 * Derived from "arch/i386/kernel/signal.c"
10 * Copyright (C) 1991, 1992 Linus Torvalds
11 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version
16 * 2 of the License, or (at your option) any later version.
19 #include <linux/sched.h>
21 #include <linux/smp.h>
22 #include <linux/smp_lock.h>
23 #include <linux/kernel.h>
24 #include <linux/signal.h>
25 #include <linux/errno.h>
26 #include <linux/wait.h>
27 #include <linux/ptrace.h>
28 #include <linux/unistd.h>
29 #include <linux/stddef.h>
30 #include <linux/elf.h>
31 #include <asm/ucontext.h>
32 #include <asm/uaccess.h>
33 #include <asm/pgtable.h>
37 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
40 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
43 #define GP_REGS_SIZE MIN(sizeof(elf_gregset_t), sizeof(struct pt_regs))
46 * These are the flags in the MSR that the user is allowed to change
47 * by modifying the saved value of the MSR on the stack. SE and BE
48 * should not be in this list since gdb may want to change these. I.e,
49 * you should be able to step out of a signal handler to see what
50 * instruction executes next after the signal handler completes.
51 * Alternately, if you stepped into a signal handler, you should be
52 * able to continue 'til the next breakpoint from within the signal
53 * handler, even if the handler returns.
55 #define MSR_USERCHANGE (MSR_FE0 | MSR_FE1)
57 int do_signal(sigset_t
*oldset
, struct pt_regs
*regs
);
58 extern int sys_wait4(pid_t pid
, unsigned long *stat_addr
,
59 int options
, unsigned long *ru
);
62 * Atomically swap in the new signal mask, and wait for a signal.
65 sys_sigsuspend(old_sigset_t mask
, int p2
, int p3
, int p4
, int p6
, int p7
,
71 spin_lock_irq(¤t
->sigmask_lock
);
72 saveset
= current
->blocked
;
73 siginitset(¤t
->blocked
, mask
);
74 recalc_sigpending(current
);
75 spin_unlock_irq(¤t
->sigmask_lock
);
77 regs
->gpr
[3] = -EINTR
;
79 current
->state
= TASK_INTERRUPTIBLE
;
81 if (do_signal(&saveset
, regs
))
83 * If a signal handler needs to be called,
84 * do_signal() has set R3 to the signal number (the
85 * first argument of the signal handler), so don't
86 * overwrite that with EINTR !
87 * In the other cases, do_signal() doesn't touch
88 * R3, so it's still set to -EINTR (see above).
95 sys_rt_sigsuspend(sigset_t
*unewset
, size_t sigsetsize
, int p3
, int p4
, int p6
,
96 int p7
, struct pt_regs
*regs
)
98 sigset_t saveset
, newset
;
100 /* XXX: Don't preclude handling different sized sigset_t's. */
101 if (sigsetsize
!= sizeof(sigset_t
))
104 if (copy_from_user(&newset
, unewset
, sizeof(newset
)))
106 sigdelsetmask(&newset
, ~_BLOCKABLE
);
108 spin_lock_irq(¤t
->sigmask_lock
);
109 saveset
= current
->blocked
;
110 current
->blocked
= newset
;
111 recalc_sigpending(current
);
112 spin_unlock_irq(¤t
->sigmask_lock
);
114 regs
->gpr
[3] = -EINTR
;
116 current
->state
= TASK_INTERRUPTIBLE
;
118 if (do_signal(&saveset
, regs
))
124 asmlinkage
int sys_rt_sigreturn(unsigned long __unused
)
126 printk("sys_rt_sigreturn(): %s/%d not yet implemented.\n",
127 current
->comm
,current
->pid
);
132 sys_sigaltstack(const stack_t
*uss
, stack_t
*uoss
)
134 struct pt_regs
*regs
= (struct pt_regs
*) &uss
;
135 return do_sigaltstack(uss
, uoss
, regs
->gpr
[1]);
139 sys_sigaction(int sig
, const struct old_sigaction
*act
,
140 struct old_sigaction
*oact
)
142 struct k_sigaction new_ka
, old_ka
;
147 if (verify_area(VERIFY_READ
, act
, sizeof(*act
)) ||
148 __get_user(new_ka
.sa
.sa_handler
, &act
->sa_handler
) ||
149 __get_user(new_ka
.sa
.sa_restorer
, &act
->sa_restorer
))
151 __get_user(new_ka
.sa
.sa_flags
, &act
->sa_flags
);
152 __get_user(mask
, &act
->sa_mask
);
153 siginitset(&new_ka
.sa
.sa_mask
, mask
);
156 ret
= do_sigaction(sig
, act
? &new_ka
: NULL
, oact
? &old_ka
: NULL
);
159 if (verify_area(VERIFY_WRITE
, oact
, sizeof(*oact
)) ||
160 __put_user(old_ka
.sa
.sa_handler
, &oact
->sa_handler
) ||
161 __put_user(old_ka
.sa
.sa_restorer
, &oact
->sa_restorer
))
163 __put_user(old_ka
.sa
.sa_flags
, &oact
->sa_flags
);
164 __put_user(old_ka
.sa
.sa_mask
.sig
[0], &oact
->sa_mask
);
171 * When we have signals to deliver, we set up on the
172 * user stack, going down from the original stack pointer:
174 * one or more sigcontext structs
175 * a gap of __SIGNAL_FRAMESIZE bytes
177 * Each of these things must be a multiple of 16 bytes in size.
179 * XXX ultimately we will have to stack up a siginfo and ucontext
180 * for each rt signal.
183 elf_gregset_t gp_regs
;
184 double fp_regs
[ELF_NFPREG
];
185 unsigned long tramp
[2];
186 /* Programs using the rs6000/xcoff abi can save up to 19 gp regs
187 and 18 fp regs below sp before decrementing it. */
192 * Do a signal return; undo the signal stack.
194 int sys_sigreturn(struct pt_regs
*regs
)
196 struct sigcontext_struct
*sc
, sigctx
;
199 elf_gregset_t saved_regs
; /* an array of ELF_NGREG unsigned longs */
201 unsigned long prevsp
;
203 sc
= (struct sigcontext_struct
*)(regs
->gpr
[1] + __SIGNAL_FRAMESIZE
);
204 if (copy_from_user(&sigctx
, sc
, sizeof(sigctx
)))
207 set
.sig
[0] = sigctx
.oldmask
;
209 set
.sig
[1] = sigctx
._unused
[3];
211 sigdelsetmask(&set
, ~_BLOCKABLE
);
212 spin_lock_irq(¤t
->sigmask_lock
);
213 current
->blocked
= set
;
214 recalc_sigpending(current
);
215 spin_unlock_irq(¤t
->sigmask_lock
);
217 sc
++; /* Look at next sigcontext */
218 if (sc
== (struct sigcontext_struct
*)(sigctx
.regs
)) {
219 /* Last stacked signal - restore registers */
220 sr
= (struct sigregs
*) sigctx
.regs
;
221 if (regs
->msr
& MSR_FP
)
223 if (copy_from_user(saved_regs
, &sr
->gp_regs
,
224 sizeof(sr
->gp_regs
)))
226 saved_regs
[PT_MSR
] = (regs
->msr
& ~MSR_USERCHANGE
)
227 | (saved_regs
[PT_MSR
] & MSR_USERCHANGE
);
228 memcpy(regs
, saved_regs
, GP_REGS_SIZE
);
230 if (copy_from_user(current
->thread
.fpr
, &sr
->fp_regs
,
231 sizeof(sr
->fp_regs
)))
237 /* More signals to go */
238 regs
->gpr
[1] = (unsigned long)sc
- __SIGNAL_FRAMESIZE
;
239 if (copy_from_user(&sigctx
, sc
, sizeof(sigctx
)))
241 sr
= (struct sigregs
*) sigctx
.regs
;
242 regs
->gpr
[3] = ret
= sigctx
.signal
;
243 regs
->gpr
[4] = (unsigned long) sc
;
244 regs
->link
= (unsigned long) &sr
->tramp
;
245 regs
->nip
= sigctx
.handler
;
247 if (get_user(prevsp
, &sr
->gp_regs
[PT_R1
])
248 || put_user(prevsp
, (unsigned long *) regs
->gpr
[1]))
259 * Set up a signal frame.
262 setup_frame(struct pt_regs
*regs
, struct sigregs
*frame
,
265 struct sigcontext_struct
*sc
= (struct sigcontext_struct
*) newsp
;
267 if (verify_area(VERIFY_WRITE
, frame
, sizeof(*frame
)))
269 if (regs
->msr
& MSR_FP
)
271 if (__copy_to_user(&frame
->gp_regs
, regs
, GP_REGS_SIZE
)
272 || __copy_to_user(&frame
->fp_regs
, current
->thread
.fpr
,
273 ELF_NFPREG
* sizeof(double))
274 || __put_user(0x38007777UL
, &frame
->tramp
[0]) /* li r0,0x7777 */
275 || __put_user(0x44000002UL
, &frame
->tramp
[1])) /* sc */
277 flush_icache_range((unsigned long) &frame
->tramp
[0],
278 (unsigned long) &frame
->tramp
[2]);
280 newsp
-= __SIGNAL_FRAMESIZE
;
281 if (put_user(regs
->gpr
[1], (unsigned long *)newsp
)
282 || get_user(regs
->nip
, &sc
->handler
)
283 || get_user(regs
->gpr
[3], &sc
->signal
))
285 regs
->gpr
[1] = newsp
;
286 regs
->gpr
[4] = (unsigned long) sc
;
287 regs
->link
= (unsigned long) frame
->tramp
;
293 printk("badframe in setup_frame, regs=%p frame=%p newsp=%lx\n",
301 * OK, we're invoking a handler
304 handle_signal(unsigned long sig
, struct k_sigaction
*ka
,
305 siginfo_t
*info
, sigset_t
*oldset
, struct pt_regs
* regs
,
306 unsigned long *newspp
, unsigned long frame
)
308 struct sigcontext_struct
*sc
;
310 if (regs
->trap
== 0x0C00 /* System Call! */
311 && ((int)regs
->result
== -ERESTARTNOHAND
||
312 ((int)regs
->result
== -ERESTARTSYS
&&
313 !(ka
->sa
.sa_flags
& SA_RESTART
))))
314 regs
->result
= -EINTR
;
316 /* Put another sigcontext on the stack */
317 *newspp
-= sizeof(*sc
);
318 sc
= (struct sigcontext_struct
*) *newspp
;
319 if (verify_area(VERIFY_WRITE
, sc
, sizeof(*sc
)))
322 if (__put_user((unsigned long) ka
->sa
.sa_handler
, &sc
->handler
)
323 || __put_user(oldset
->sig
[0], &sc
->oldmask
)
325 || __put_user(oldset
->sig
[1], &sc
->_unused
[3])
327 || __put_user((struct pt_regs
*)frame
, &sc
->regs
)
328 || __put_user(sig
, &sc
->signal
))
331 if (ka
->sa
.sa_flags
& SA_ONESHOT
)
332 ka
->sa
.sa_handler
= SIG_DFL
;
334 if (!(ka
->sa
.sa_flags
& SA_NODEFER
)) {
335 spin_lock_irq(¤t
->sigmask_lock
);
336 sigorsets(¤t
->blocked
,¤t
->blocked
,&ka
->sa
.sa_mask
);
337 sigaddset(¤t
->blocked
,sig
);
338 recalc_sigpending(current
);
339 spin_unlock_irq(¤t
->sigmask_lock
);
345 printk("badframe in handle_signal, regs=%p frame=%lx newsp=%lx\n",
346 regs
, frame
, *newspp
);
347 printk("sc=%p sig=%d ka=%p info=%p oldset=%p\n", sc
, sig
, ka
, info
, oldset
);
354 * Note that 'init' is a special process: it doesn't get signals it doesn't
355 * want to handle. Thus you cannot kill init even with a SIGKILL even by
358 int do_signal(sigset_t
*oldset
, struct pt_regs
*regs
)
361 struct k_sigaction
*ka
;
362 unsigned long frame
, newsp
;
365 oldset
= ¤t
->blocked
;
372 spin_lock_irq(¤t
->sigmask_lock
);
373 signr
= dequeue_signal(¤t
->blocked
, &info
);
374 spin_unlock_irq(¤t
->sigmask_lock
);
379 if ((current
->flags
& PF_PTRACED
) && signr
!= SIGKILL
) {
380 /* Let the debugger run. */
381 current
->exit_code
= signr
;
382 current
->state
= TASK_STOPPED
;
383 notify_parent(current
, SIGCHLD
);
386 /* We're back. Did the debugger cancel the sig? */
387 if (!(signr
= current
->exit_code
))
389 current
->exit_code
= 0;
391 /* The debugger continued. Ignore SIGSTOP. */
392 if (signr
== SIGSTOP
)
395 /* Update the siginfo structure. Is this good? */
396 if (signr
!= info
.si_signo
) {
397 info
.si_signo
= signr
;
399 info
.si_code
= SI_USER
;
400 info
.si_pid
= current
->p_pptr
->pid
;
401 info
.si_uid
= current
->p_pptr
->uid
;
404 /* If the (new) signal is now blocked, requeue it. */
405 if (sigismember(¤t
->blocked
, signr
)) {
406 send_sig_info(signr
, &info
, current
);
411 ka
= ¤t
->sig
->action
[signr
-1];
412 if (ka
->sa
.sa_handler
== SIG_IGN
) {
413 if (signr
!= SIGCHLD
)
415 /* Check for SIGCHLD: it's special. */
416 while (sys_wait4(-1, NULL
, WNOHANG
, NULL
) > 0)
421 if (ka
->sa
.sa_handler
== SIG_DFL
) {
422 int exit_code
= signr
;
424 /* Init gets no signals it doesn't want. */
425 if (current
->pid
== 1)
429 case SIGCONT
: case SIGCHLD
: case SIGWINCH
:
432 case SIGTSTP
: case SIGTTIN
: case SIGTTOU
:
433 if (is_orphaned_pgrp(current
->pgrp
))
438 current
->state
= TASK_STOPPED
;
439 current
->exit_code
= signr
;
440 if (!(current
->p_pptr
->sig
->action
[SIGCHLD
-1].sa
.sa_flags
& SA_NOCLDSTOP
))
441 notify_parent(current
, SIGCHLD
);
445 case SIGQUIT
: case SIGILL
: case SIGTRAP
:
446 case SIGABRT
: case SIGFPE
: case SIGSEGV
:
447 if (do_coredump(signr
, regs
))
453 sigaddset(¤t
->signal
, signr
);
454 recalc_sigpending(current
);
455 current
->flags
|= PF_SIGNALED
;
461 if ( (ka
->sa
.sa_flags
& SA_ONSTACK
)
462 && (! on_sig_stack(regs
->gpr
[1])))
463 newsp
= (current
->sas_ss_sp
+ current
->sas_ss_size
);
465 newsp
= regs
->gpr
[1];
466 newsp
= frame
= newsp
- sizeof(struct sigregs
);
468 /* Whee! Actually deliver the signal. */
469 handle_signal(signr
, ka
, &info
, oldset
, regs
, &newsp
, frame
);
472 if (regs
->trap
== 0x0C00 /* System Call! */ &&
473 ((int)regs
->result
== -ERESTARTNOHAND
||
474 (int)regs
->result
== -ERESTARTSYS
||
475 (int)regs
->result
== -ERESTARTNOINTR
)) {
476 regs
->gpr
[3] = regs
->orig_gpr3
;
477 regs
->nip
-= 4; /* Back up & retry system call */
482 return 0; /* no signals delivered */
484 setup_frame(regs
, (struct sigregs
*) frame
, newsp
);