2 * arch/v850/kernel/signal.c -- Signal handling
4 * Copyright (C) 2001,02,03 NEC Electronics Corporation
5 * Copyright (C) 2001,02,03 Miles Bader <miles@gnu.org>
6 * Copyright (C) 1999,2000,2002 Niibe Yutaka & Kaz Kojima
7 * Copyright (C) 1991,1992 Linus Torvalds
9 * This file is subject to the terms and conditions of the GNU General
10 * Public License. See the file COPYING in the main directory of this
11 * archive for more details.
13 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
15 * This file was derived from the sh version, arch/sh/kernel/signal.c
19 #include <linux/smp.h>
20 #include <linux/smp_lock.h>
21 #include <linux/kernel.h>
22 #include <linux/signal.h>
23 #include <linux/errno.h>
24 #include <linux/wait.h>
25 #include <linux/ptrace.h>
26 #include <linux/unistd.h>
27 #include <linux/stddef.h>
28 #include <linux/personality.h>
29 #include <linux/tty.h>
31 #include <asm/ucontext.h>
32 #include <asm/uaccess.h>
33 #include <asm/pgtable.h>
34 #include <asm/pgalloc.h>
35 #include <asm/thread_info.h>
36 #include <asm/cacheflush.h>
40 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
42 asmlinkage
int do_signal(struct pt_regs
*regs
, sigset_t
*oldset
);
45 * Atomically swap in the new signal mask, and wait for a signal.
48 sys_sigsuspend(old_sigset_t mask
, struct pt_regs
*regs
)
53 spin_lock_irq(¤t
->sighand
->siglock
);
54 saveset
= current
->blocked
;
55 siginitset(¤t
->blocked
, mask
);
57 spin_unlock_irq(¤t
->sighand
->siglock
);
59 regs
->gpr
[GPR_RVAL
] = -EINTR
;
61 current
->state
= TASK_INTERRUPTIBLE
;
63 if (do_signal(regs
, &saveset
))
69 sys_rt_sigsuspend(sigset_t
*unewset
, size_t sigsetsize
,
72 sigset_t saveset
, newset
;
74 /* XXX: Don't preclude handling different sized sigset_t's. */
75 if (sigsetsize
!= sizeof(sigset_t
))
78 if (copy_from_user(&newset
, unewset
, sizeof(newset
)))
80 sigdelsetmask(&newset
, ~_BLOCKABLE
);
81 spin_lock_irq(¤t
->sighand
->siglock
);
82 saveset
= current
->blocked
;
83 current
->blocked
= newset
;
85 spin_unlock_irq(¤t
->sighand
->siglock
);
87 regs
->gpr
[GPR_RVAL
] = -EINTR
;
89 current
->state
= TASK_INTERRUPTIBLE
;
91 if (do_signal(regs
, &saveset
))
97 sys_sigaction(int sig
, const struct old_sigaction
*act
,
98 struct old_sigaction
*oact
)
100 struct k_sigaction new_ka
, old_ka
;
105 if (!access_ok(VERIFY_READ
, act
, sizeof(*act
)) ||
106 __get_user(new_ka
.sa
.sa_handler
, &act
->sa_handler
) ||
107 __get_user(new_ka
.sa
.sa_restorer
, &act
->sa_restorer
))
109 __get_user(new_ka
.sa
.sa_flags
, &act
->sa_flags
);
110 __get_user(mask
, &act
->sa_mask
);
111 siginitset(&new_ka
.sa
.sa_mask
, mask
);
114 ret
= do_sigaction(sig
, act
? &new_ka
: NULL
, oact
? &old_ka
: NULL
);
117 if (!access_ok(VERIFY_WRITE
, oact
, sizeof(*oact
)) ||
118 __put_user(old_ka
.sa
.sa_handler
, &oact
->sa_handler
) ||
119 __put_user(old_ka
.sa
.sa_restorer
, &oact
->sa_restorer
))
121 __put_user(old_ka
.sa
.sa_flags
, &oact
->sa_flags
);
122 __put_user(old_ka
.sa
.sa_mask
.sig
[0], &oact
->sa_mask
);
129 sys_sigaltstack(const stack_t
*uss
, stack_t
*uoss
,
130 struct pt_regs
*regs
)
132 return do_sigaltstack(uss
, uoss
, regs
->gpr
[GPR_SP
]);
137 * Do a signal return; undo the signal stack.
142 struct sigcontext sc
;
143 unsigned long extramask
[_NSIG_WORDS
-1];
144 unsigned long tramp
[2]; /* signal trampoline */
151 unsigned long tramp
[2]; /* signal trampoline */
155 restore_sigcontext(struct pt_regs
*regs
, struct sigcontext
*sc
, int *rval_p
)
157 unsigned int err
= 0;
159 #define COPY(x) err |= __get_user(regs->x, &sc->regs.x)
160 COPY(gpr
[0]); COPY(gpr
[1]); COPY(gpr
[2]); COPY(gpr
[3]);
161 COPY(gpr
[4]); COPY(gpr
[5]); COPY(gpr
[6]); COPY(gpr
[7]);
162 COPY(gpr
[8]); COPY(gpr
[9]); COPY(gpr
[10]); COPY(gpr
[11]);
163 COPY(gpr
[12]); COPY(gpr
[13]); COPY(gpr
[14]); COPY(gpr
[15]);
164 COPY(gpr
[16]); COPY(gpr
[17]); COPY(gpr
[18]); COPY(gpr
[19]);
165 COPY(gpr
[20]); COPY(gpr
[21]); COPY(gpr
[22]); COPY(gpr
[23]);
166 COPY(gpr
[24]); COPY(gpr
[25]); COPY(gpr
[26]); COPY(gpr
[27]);
167 COPY(gpr
[28]); COPY(gpr
[29]); COPY(gpr
[30]); COPY(gpr
[31]);
169 COPY(ctpc
); COPY(ctpsw
); COPY(ctbp
);
175 asmlinkage
int sys_sigreturn(struct pt_regs
*regs
)
177 struct sigframe
*frame
= (struct sigframe
*)regs
->gpr
[GPR_SP
];
181 if (!access_ok(VERIFY_READ
, frame
, sizeof(*frame
)))
184 if (__get_user(set
.sig
[0], &frame
->sc
.oldmask
)
186 && __copy_from_user(&set
.sig
[1], &frame
->extramask
,
187 sizeof(frame
->extramask
))))
190 sigdelsetmask(&set
, ~_BLOCKABLE
);
191 spin_lock_irq(¤t
->sighand
->siglock
);
192 current
->blocked
= set
;
194 spin_unlock_irq(¤t
->sighand
->siglock
);
196 if (restore_sigcontext(regs
, &frame
->sc
, &rval
))
201 force_sig(SIGSEGV
, current
);
205 asmlinkage
int sys_rt_sigreturn(struct pt_regs
*regs
)
207 struct rt_sigframe
*frame
= (struct rt_sigframe
*)regs
->gpr
[GPR_SP
];
212 if (!access_ok(VERIFY_READ
, frame
, sizeof(*frame
)))
215 if (__copy_from_user(&set
, &frame
->uc
.uc_sigmask
, sizeof(set
)))
218 sigdelsetmask(&set
, ~_BLOCKABLE
);
219 spin_lock_irq(¤t
->sighand
->siglock
);
220 current
->blocked
= set
;
222 spin_unlock_irq(¤t
->sighand
->siglock
);
224 if (restore_sigcontext(regs
, &frame
->uc
.uc_mcontext
, &rval
))
227 if (__copy_from_user(&st
, &frame
->uc
.uc_stack
, sizeof(st
)))
229 /* It is more difficult to avoid calling this function than to
230 call it and ignore errors. */
231 do_sigaltstack(&st
, NULL
, regs
->gpr
[GPR_SP
]);
236 force_sig(SIGSEGV
, current
);
241 * Set up a signal frame.
245 setup_sigcontext(struct sigcontext
*sc
, struct pt_regs
*regs
,
250 #define COPY(x) err |= __put_user(regs->x, &sc->regs.x)
251 COPY(gpr
[0]); COPY(gpr
[1]); COPY(gpr
[2]); COPY(gpr
[3]);
252 COPY(gpr
[4]); COPY(gpr
[5]); COPY(gpr
[6]); COPY(gpr
[7]);
253 COPY(gpr
[8]); COPY(gpr
[9]); COPY(gpr
[10]); COPY(gpr
[11]);
254 COPY(gpr
[12]); COPY(gpr
[13]); COPY(gpr
[14]); COPY(gpr
[15]);
255 COPY(gpr
[16]); COPY(gpr
[17]); COPY(gpr
[18]); COPY(gpr
[19]);
256 COPY(gpr
[20]); COPY(gpr
[21]); COPY(gpr
[22]); COPY(gpr
[23]);
257 COPY(gpr
[24]); COPY(gpr
[25]); COPY(gpr
[26]); COPY(gpr
[27]);
258 COPY(gpr
[28]); COPY(gpr
[29]); COPY(gpr
[30]); COPY(gpr
[31]);
260 COPY(ctpc
); COPY(ctpsw
); COPY(ctbp
);
263 err
|= __put_user(mask
, &sc
->oldmask
);
269 * Determine which stack to use..
272 get_sigframe(struct k_sigaction
*ka
, struct pt_regs
*regs
, size_t frame_size
)
274 /* Default to using normal stack */
275 unsigned long sp
= regs
->gpr
[GPR_SP
];
277 if ((ka
->sa
.sa_flags
& SA_ONSTACK
) != 0 && ! on_sig_stack(sp
))
278 sp
= current
->sas_ss_sp
+ current
->sas_ss_size
;
280 return (void *)((sp
- frame_size
) & -8UL);
283 static void setup_frame(int sig
, struct k_sigaction
*ka
,
284 sigset_t
*set
, struct pt_regs
*regs
)
286 struct sigframe
*frame
;
290 frame
= get_sigframe(ka
, regs
, sizeof(*frame
));
292 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
)))
295 signal
= current_thread_info()->exec_domain
296 && current_thread_info()->exec_domain
->signal_invmap
298 ? current_thread_info()->exec_domain
->signal_invmap
[sig
]
301 err
|= setup_sigcontext(&frame
->sc
, regs
, set
->sig
[0]);
303 if (_NSIG_WORDS
> 1) {
304 err
|= __copy_to_user(frame
->extramask
, &set
->sig
[1],
305 sizeof(frame
->extramask
));
308 /* Set up to return from userspace. If provided, use a stub
309 already in userspace. */
310 if (ka
->sa
.sa_flags
& SA_RESTORER
) {
311 regs
->gpr
[GPR_LP
] = (unsigned long) ka
->sa
.sa_restorer
;
313 /* Note, these encodings are _little endian_! */
315 /* addi __NR_sigreturn, r0, r12 */
316 err
|= __put_user(0x6600 | (__NR_sigreturn
<< 16),
319 err
|= __put_user(0x010007e0,
322 regs
->gpr
[GPR_LP
] = (unsigned long)frame
->tramp
;
324 flush_cache_sigtramp (regs
->gpr
[GPR_LP
]);
330 /* Set up registers for signal handler. */
331 regs
->pc
= (v850_reg_t
) ka
->sa
.sa_handler
;
332 regs
->gpr
[GPR_SP
] = (v850_reg_t
)frame
;
333 /* Signal handler args: */
334 regs
->gpr
[GPR_ARG0
] = signal
; /* arg 0: signum */
335 regs
->gpr
[GPR_ARG1
] = (v850_reg_t
)&frame
->sc
;/* arg 1: sigcontext */
340 printk("SIG deliver (%s:%d): sp=%p pc=%08lx ra=%08lx\n",
341 current
->comm
, current
->pid
, frame
, regs
->pc
, );
347 force_sigsegv(sig
, current
);
350 static void setup_rt_frame(int sig
, struct k_sigaction
*ka
, siginfo_t
*info
,
351 sigset_t
*set
, struct pt_regs
*regs
)
353 struct rt_sigframe
*frame
;
357 frame
= get_sigframe(ka
, regs
, sizeof(*frame
));
359 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
)))
362 signal
= current_thread_info()->exec_domain
363 && current_thread_info()->exec_domain
->signal_invmap
365 ? current_thread_info()->exec_domain
->signal_invmap
[sig
]
368 err
|= copy_siginfo_to_user(&frame
->info
, info
);
370 /* Create the ucontext. */
371 err
|= __put_user(0, &frame
->uc
.uc_flags
);
372 err
|= __put_user(0, &frame
->uc
.uc_link
);
373 err
|= __put_user((void *)current
->sas_ss_sp
,
374 &frame
->uc
.uc_stack
.ss_sp
);
375 err
|= __put_user(sas_ss_flags(regs
->gpr
[GPR_SP
]),
376 &frame
->uc
.uc_stack
.ss_flags
);
377 err
|= __put_user(current
->sas_ss_size
, &frame
->uc
.uc_stack
.ss_size
);
378 err
|= setup_sigcontext(&frame
->uc
.uc_mcontext
,
380 err
|= __copy_to_user(&frame
->uc
.uc_sigmask
, set
, sizeof(*set
));
382 /* Set up to return from userspace. If provided, use a stub
383 already in userspace. */
384 if (ka
->sa
.sa_flags
& SA_RESTORER
) {
385 regs
->gpr
[GPR_LP
] = (unsigned long) ka
->sa
.sa_restorer
;
387 /* Note, these encodings are _little endian_! */
389 /* addi __NR_sigreturn, r0, r12 */
390 err
|= __put_user(0x6600 | (__NR_sigreturn
<< 16),
393 err
|= __put_user(0x010007e0,
396 regs
->gpr
[GPR_LP
] = (unsigned long)frame
->tramp
;
398 flush_cache_sigtramp (regs
->gpr
[GPR_LP
]);
404 /* Set up registers for signal handler. */
405 regs
->pc
= (v850_reg_t
) ka
->sa
.sa_handler
;
406 regs
->gpr
[GPR_SP
] = (v850_reg_t
)frame
;
407 /* Signal handler args: */
408 regs
->gpr
[GPR_ARG0
] = signal
; /* arg 0: signum */
409 regs
->gpr
[GPR_ARG1
] = (v850_reg_t
)&frame
->info
; /* arg 1: siginfo */
410 regs
->gpr
[GPR_ARG2
] = (v850_reg_t
)&frame
->uc
; /* arg 2: ucontext */
415 printk("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
416 current
->comm
, current
->pid
, frame
, regs
->pc
, regs
->pr
);
422 force_sigsegv(sig
, current
);
426 * OK, we're invoking a handler
430 handle_signal(unsigned long sig
, siginfo_t
*info
, struct k_sigaction
*ka
,
431 sigset_t
*oldset
, struct pt_regs
* regs
)
433 /* Are we from a system call? */
434 if (PT_REGS_SYSCALL (regs
)) {
435 /* If so, check system call restarting.. */
436 switch (regs
->gpr
[GPR_RVAL
]) {
437 case -ERESTART_RESTARTBLOCK
:
438 current_thread_info()->restart_block
.fn
=
439 do_no_restart_syscall
;
441 case -ERESTARTNOHAND
:
442 regs
->gpr
[GPR_RVAL
] = -EINTR
;
446 if (!(ka
->sa
.sa_flags
& SA_RESTART
)) {
447 regs
->gpr
[GPR_RVAL
] = -EINTR
;
451 case -ERESTARTNOINTR
:
452 regs
->gpr
[12] = PT_REGS_SYSCALL (regs
);
453 regs
->pc
-= 4; /* Size of `trap 0' insn. */
456 PT_REGS_SET_SYSCALL (regs
, 0);
459 /* Set up the stack frame */
460 if (ka
->sa
.sa_flags
& SA_SIGINFO
)
461 setup_rt_frame(sig
, ka
, info
, oldset
, regs
);
463 setup_frame(sig
, ka
, oldset
, regs
);
465 spin_lock_irq(¤t
->sighand
->siglock
);
466 sigorsets(¤t
->blocked
,¤t
->blocked
,&ka
->sa
.sa_mask
);
467 if (!(ka
->sa
.sa_flags
& SA_NODEFER
))
468 sigaddset(¤t
->blocked
,sig
);
470 spin_unlock_irq(¤t
->sighand
->siglock
);
474 * Note that 'init' is a special process: it doesn't get signals it doesn't
475 * want to handle. Thus you cannot kill init even with a SIGKILL even by
478 * Note that we go through the signals twice: once to check the signals that
479 * the kernel can handle, and then we build all the user-level signal handling
480 * stack-frames in one go after that.
482 int do_signal(struct pt_regs
*regs
, sigset_t
*oldset
)
486 struct k_sigaction ka
;
489 * We want the common case to go fast, which
490 * is why we may in certain cases get here from
491 * kernel mode. Just return without doing anything
494 if (!user_mode(regs
))
498 oldset
= ¤t
->blocked
;
500 signr
= get_signal_to_deliver(&info
, &ka
, regs
, NULL
);
502 /* Whee! Actually deliver the signal. */
503 handle_signal(signr
, &info
, &ka
, oldset
, regs
);
507 /* Did we come from a system call? */
508 if (PT_REGS_SYSCALL (regs
)) {
509 int rval
= (int)regs
->gpr
[GPR_RVAL
];
510 /* Restart the system call - no handlers present */
511 if (rval
== -ERESTARTNOHAND
512 || rval
== -ERESTARTSYS
513 || rval
== -ERESTARTNOINTR
)
515 regs
->gpr
[12] = PT_REGS_SYSCALL (regs
);
516 regs
->pc
-= 4; /* Size of `trap 0' insn. */
518 else if (rval
== -ERESTART_RESTARTBLOCK
) {
519 regs
->gpr
[12] = __NR_restart_syscall
;
520 regs
->pc
-= 4; /* Size of `trap 0' insn. */