4 * Copyright (C) 2008-2009 Michal Simek <monstr@monstr.eu>
5 * Copyright (C) 2008-2009 PetaLogix
6 * Copyright (C) 2003,2004 John Williams <jwilliams@itee.uq.edu.au>
7 * Copyright (C) 2001 NEC Corporation
8 * Copyright (C) 2001 Miles Bader <miles@gnu.org>
9 * Copyright (C) 1999,2000 Niibe Yutaka & Kaz Kojima
10 * Copyright (C) 1991,1992 Linus Torvalds
12 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
14 * This file was was derived from the sh version, arch/sh/kernel/signal.c
16 * This file is subject to the terms and conditions of the GNU General
17 * Public License. See the file COPYING in the main directory of this
18 * archive for more details.
21 #include <linux/sched.h>
23 #include <linux/smp.h>
24 #include <linux/smp_lock.h>
25 #include <linux/kernel.h>
26 #include <linux/signal.h>
27 #include <linux/errno.h>
28 #include <linux/wait.h>
29 #include <linux/ptrace.h>
30 #include <linux/unistd.h>
31 #include <linux/stddef.h>
32 #include <linux/personality.h>
33 #include <linux/percpu.h>
34 #include <linux/linkage.h>
35 #include <asm/entry.h>
36 #include <asm/ucontext.h>
37 #include <linux/uaccess.h>
38 #include <asm/pgtable.h>
39 #include <asm/pgalloc.h>
40 #include <linux/syscalls.h>
41 #include <asm/cacheflush.h>
42 #include <asm/syscalls.h>
44 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
46 asmlinkage
int do_signal(struct pt_regs
*regs
, sigset_t
*oldset
, int in_sycall
);
49 * Atomically swap in the new signal mask, and wait for a signal.
52 sys_sigsuspend(old_sigset_t mask
, struct pt_regs
*regs
)
57 spin_lock_irq(¤t
->sighand
->siglock
);
58 saveset
= current
->blocked
;
59 siginitset(¤t
->blocked
, mask
);
61 spin_unlock_irq(¤t
->sighand
->siglock
);
65 current
->state
= TASK_INTERRUPTIBLE
;
67 if (do_signal(regs
, &saveset
, 1))
73 sys_rt_sigsuspend(sigset_t __user
*unewset
, size_t sigsetsize
,
76 sigset_t saveset
, newset
;
78 /* XXX: Don't preclude handling different sized sigset_t's. */
79 if (sigsetsize
!= sizeof(sigset_t
))
82 if (copy_from_user(&newset
, unewset
, sizeof(newset
)))
84 sigdelsetmask(&newset
, ~_BLOCKABLE
);
85 spin_lock_irq(¤t
->sighand
->siglock
);
86 saveset
= current
->blocked
;
87 current
->blocked
= newset
;
89 spin_unlock_irq(¤t
->sighand
->siglock
);
93 current
->state
= TASK_INTERRUPTIBLE
;
95 if (do_signal(regs
, &saveset
, 1))
101 sys_sigaction(int sig
, const struct old_sigaction
*act
,
102 struct old_sigaction
*oact
)
104 struct k_sigaction new_ka
, old_ka
;
109 if (!access_ok(VERIFY_READ
, act
, sizeof(*act
)) ||
110 __get_user(new_ka
.sa
.sa_handler
, &act
->sa_handler
) ||
111 __get_user(new_ka
.sa
.sa_restorer
, &act
->sa_restorer
))
113 __get_user(new_ka
.sa
.sa_flags
, &act
->sa_flags
);
114 __get_user(mask
, &act
->sa_mask
);
115 siginitset(&new_ka
.sa
.sa_mask
, mask
);
118 ret
= do_sigaction(sig
, act
? &new_ka
: NULL
, oact
? &old_ka
: NULL
);
121 if (!access_ok(VERIFY_WRITE
, oact
, sizeof(*oact
)) ||
122 __put_user(old_ka
.sa
.sa_handler
, &oact
->sa_handler
) ||
123 __put_user(old_ka
.sa
.sa_restorer
, &oact
->sa_restorer
))
125 __put_user(old_ka
.sa
.sa_flags
, &oact
->sa_flags
);
126 __put_user(old_ka
.sa
.sa_mask
.sig
[0], &oact
->sa_mask
);
133 sys_sigaltstack(const stack_t __user
*uss
, stack_t __user
*uoss
,
134 struct pt_regs
*regs
)
136 return do_sigaltstack(uss
, uoss
, regs
->r1
);
140 * Do a signal return; undo the signal stack.
144 struct sigcontext sc
;
145 unsigned long extramask
[_NSIG_WORDS
-1];
146 unsigned long tramp
[2]; /* signal trampoline */
152 unsigned long tramp
[2]; /* signal trampoline */
156 restore_sigcontext(struct pt_regs
*regs
, struct sigcontext
*sc
, int *rval_p
)
158 unsigned int err
= 0;
160 #define COPY(x) {err |= __get_user(regs->x, &sc->regs.x); }
163 COPY(r2
); COPY(r3
); COPY(r4
); COPY(r5
);
164 COPY(r6
); COPY(r7
); COPY(r8
); COPY(r9
);
165 COPY(r10
); COPY(r11
); COPY(r12
); COPY(r13
);
166 COPY(r14
); COPY(r15
); COPY(r16
); COPY(r17
);
167 COPY(r18
); COPY(r19
); COPY(r20
); COPY(r21
);
168 COPY(r22
); COPY(r23
); COPY(r24
); COPY(r25
);
169 COPY(r26
); COPY(r27
); COPY(r28
); COPY(r29
);
170 COPY(r30
); COPY(r31
);
171 COPY(pc
); COPY(ear
); COPY(esr
); COPY(fsr
);
179 asmlinkage
int sys_sigreturn(struct pt_regs
*regs
)
181 struct sigframe
*frame
= (struct sigframe
*)regs
->r1
;
185 if (!access_ok(VERIFY_READ
, frame
, sizeof(*frame
)))
188 if (__get_user(set
.sig
[0], &frame
->sc
.oldmask
)
190 && __copy_from_user(&set
.sig
[1], &frame
->extramask
,
191 sizeof(frame
->extramask
))))
194 sigdelsetmask(&set
, ~_BLOCKABLE
);
196 spin_lock_irq(¤t
->sighand
->siglock
);
197 current
->blocked
= set
;
199 spin_unlock_irq(¤t
->sighand
->siglock
);
201 if (restore_sigcontext(regs
, &frame
->sc
, &rval
))
206 force_sig(SIGSEGV
, current
);
210 asmlinkage
int sys_rt_sigreturn(struct pt_regs
*regs
)
212 struct rt_sigframe
*frame
= (struct rt_sigframe
*)regs
->r1
;
217 if (!access_ok(VERIFY_READ
, frame
, sizeof(*frame
)))
220 if (__copy_from_user(&set
, &frame
->uc
.uc_sigmask
, sizeof(set
)))
223 sigdelsetmask(&set
, ~_BLOCKABLE
);
224 spin_lock_irq(¤t
->sighand
->siglock
);
225 current
->blocked
= set
;
227 spin_unlock_irq(¤t
->sighand
->siglock
);
229 if (restore_sigcontext(regs
, &frame
->uc
.uc_mcontext
, &rval
))
232 if (__copy_from_user((void *)&st
, &frame
->uc
.uc_stack
, sizeof(st
)))
234 /* It is more difficult to avoid calling this function than to
235 call it and ignore errors. */
236 do_sigaltstack(&st
, NULL
, regs
->r1
);
241 force_sig(SIGSEGV
, current
);
246 * Set up a signal frame.
250 setup_sigcontext(struct sigcontext
*sc
, struct pt_regs
*regs
,
255 #define COPY(x) {err |= __put_user(regs->x, &sc->regs.x); }
258 COPY(r2
); COPY(r3
); COPY(r4
); COPY(r5
);
259 COPY(r6
); COPY(r7
); COPY(r8
); COPY(r9
);
260 COPY(r10
); COPY(r11
); COPY(r12
); COPY(r13
);
261 COPY(r14
); COPY(r15
); COPY(r16
); COPY(r17
);
262 COPY(r18
); COPY(r19
); COPY(r20
); COPY(r21
);
263 COPY(r22
); COPY(r23
); COPY(r24
); COPY(r25
);
264 COPY(r26
); COPY(r27
); COPY(r28
); COPY(r29
);
265 COPY(r30
); COPY(r31
);
266 COPY(pc
); COPY(ear
); COPY(esr
); COPY(fsr
);
269 err
|= __put_user(mask
, &sc
->oldmask
);
275 * Determine which stack to use..
278 get_sigframe(struct k_sigaction
*ka
, struct pt_regs
*regs
, size_t frame_size
)
280 /* Default to using normal stack */
281 unsigned long sp
= regs
->r1
;
283 if ((ka
->sa
.sa_flags
& SA_ONSTACK
) != 0 && !on_sig_stack(sp
))
284 sp
= current
->sas_ss_sp
+ current
->sas_ss_size
;
286 return (void *)((sp
- frame_size
) & -8UL);
289 static void setup_frame(int sig
, struct k_sigaction
*ka
,
290 sigset_t
*set
, struct pt_regs
*regs
)
292 struct sigframe
*frame
;
296 frame
= get_sigframe(ka
, regs
, sizeof(*frame
));
298 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
)))
301 signal
= current_thread_info()->exec_domain
302 && current_thread_info()->exec_domain
->signal_invmap
304 ? current_thread_info()->exec_domain
->signal_invmap
[sig
]
307 err
|= setup_sigcontext(&frame
->sc
, regs
, set
->sig
[0]);
309 if (_NSIG_WORDS
> 1) {
310 err
|= __copy_to_user(frame
->extramask
, &set
->sig
[1],
311 sizeof(frame
->extramask
));
314 /* Set up to return from userspace. If provided, use a stub
315 already in userspace. */
316 /* minus 8 is offset to cater for "rtsd r15,8" offset */
317 if (ka
->sa
.sa_flags
& SA_RESTORER
) {
318 regs
->r15
= ((unsigned long)ka
->sa
.sa_restorer
)-8;
320 /* Note, these encodings are _big endian_! */
322 /* addi r12, r0, __NR_sigreturn */
323 err
|= __put_user(0x31800000 | __NR_sigreturn
,
326 err
|= __put_user(0xb9cc0008, frame
->tramp
+ 1);
328 /* Return from sighandler will jump to the tramp.
329 Negative 8 offset because return is rtsd r15, 8 */
330 regs
->r15
= ((unsigned long)frame
->tramp
)-8;
332 __invalidate_cache_sigtramp((unsigned long)frame
->tramp
);
338 /* Set up registers for signal handler */
339 regs
->r1
= (unsigned long) frame
;
340 /* Signal handler args: */
341 regs
->r5
= signal
; /* Arg 0: signum */
342 regs
->r6
= (unsigned long) &frame
->sc
; /* arg 1: sigcontext */
344 /* Offset of 4 to handle microblaze rtid r14, 0 */
345 regs
->pc
= (unsigned long)ka
->sa
.sa_handler
;
350 printk(KERN_INFO
"SIG deliver (%s:%d): sp=%p pc=%08lx\n",
351 current
->comm
, current
->pid
, frame
, regs
->pc
);
358 ka
->sa
.sa_handler
= SIG_DFL
;
359 force_sig(SIGSEGV
, current
);
362 static void setup_rt_frame(int sig
, struct k_sigaction
*ka
, siginfo_t
*info
,
363 sigset_t
*set
, struct pt_regs
*regs
)
365 struct rt_sigframe
*frame
;
369 frame
= get_sigframe(ka
, regs
, sizeof(*frame
));
371 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
)))
374 signal
= current_thread_info()->exec_domain
375 && current_thread_info()->exec_domain
->signal_invmap
377 ? current_thread_info()->exec_domain
->signal_invmap
[sig
]
380 err
|= copy_siginfo_to_user(&frame
->info
, info
);
382 /* Create the ucontext. */
383 err
|= __put_user(0, &frame
->uc
.uc_flags
);
384 err
|= __put_user(0, &frame
->uc
.uc_link
);
385 err
|= __put_user((void *)current
->sas_ss_sp
,
386 &frame
->uc
.uc_stack
.ss_sp
);
387 err
|= __put_user(sas_ss_flags(regs
->r1
),
388 &frame
->uc
.uc_stack
.ss_flags
);
389 err
|= __put_user(current
->sas_ss_size
, &frame
->uc
.uc_stack
.ss_size
);
390 err
|= setup_sigcontext(&frame
->uc
.uc_mcontext
,
392 err
|= __copy_to_user(&frame
->uc
.uc_sigmask
, set
, sizeof(*set
));
394 /* Set up to return from userspace. If provided, use a stub
395 already in userspace. */
396 /* minus 8 is offset to cater for "rtsd r15,8" */
397 if (ka
->sa
.sa_flags
& SA_RESTORER
) {
398 regs
->r15
= ((unsigned long)ka
->sa
.sa_restorer
)-8;
400 /* addi r12, r0, __NR_sigreturn */
401 err
|= __put_user(0x31800000 | __NR_rt_sigreturn
,
404 err
|= __put_user(0xb9cc0008, frame
->tramp
+ 1);
406 /* Return from sighandler will jump to the tramp.
407 Negative 8 offset because return is rtsd r15, 8 */
408 regs
->r15
= ((unsigned long)frame
->tramp
)-8;
410 __invalidate_cache_sigtramp((unsigned long)frame
->tramp
);
416 /* Set up registers for signal handler */
417 regs
->r1
= (unsigned long) frame
;
418 /* Signal handler args: */
419 regs
->r5
= signal
; /* arg 0: signum */
420 regs
->r6
= (unsigned long) &frame
->info
; /* arg 1: siginfo */
421 regs
->r7
= (unsigned long) &frame
->uc
; /* arg2: ucontext */
422 /* Offset to handle microblaze rtid r14, 0 */
423 regs
->pc
= (unsigned long)ka
->sa
.sa_handler
;
428 printk(KERN_INFO
"SIG deliver (%s:%d): sp=%p pc=%08lx\n",
429 current
->comm
, current
->pid
, frame
, regs
->pc
);
436 ka
->sa
.sa_handler
= SIG_DFL
;
437 force_sig(SIGSEGV
, current
);
440 /* Handle restarting system calls */
442 handle_restart(struct pt_regs
*regs
, struct k_sigaction
*ka
, int has_handler
)
445 case -ERESTART_RESTARTBLOCK
:
446 case -ERESTARTNOHAND
:
452 if (has_handler
&& !(ka
->sa
.sa_flags
& SA_RESTART
)) {
457 case -ERESTARTNOINTR
:
459 /* offset of 4 bytes to re-execute trap (brki) instruction */
466 * OK, we're invoking a handler
470 handle_signal(unsigned long sig
, struct k_sigaction
*ka
,
471 siginfo_t
*info
, sigset_t
*oldset
, struct pt_regs
*regs
)
473 /* Set up the stack frame */
474 if (ka
->sa
.sa_flags
& SA_SIGINFO
)
475 setup_rt_frame(sig
, ka
, info
, oldset
, regs
);
477 setup_frame(sig
, ka
, oldset
, regs
);
479 if (ka
->sa
.sa_flags
& SA_ONESHOT
)
480 ka
->sa
.sa_handler
= SIG_DFL
;
482 if (!(ka
->sa
.sa_flags
& SA_NODEFER
)) {
483 spin_lock_irq(¤t
->sighand
->siglock
);
484 sigorsets(¤t
->blocked
,
485 ¤t
->blocked
, &ka
->sa
.sa_mask
);
486 sigaddset(¤t
->blocked
, sig
);
488 spin_unlock_irq(¤t
->sighand
->siglock
);
493 * Note that 'init' is a special process: it doesn't get signals it doesn't
494 * want to handle. Thus you cannot kill init even with a SIGKILL even by
497 * Note that we go through the signals twice: once to check the signals that
498 * the kernel can handle, and then we build all the user-level signal handling
499 * stack-frames in one go after that.
501 int do_signal(struct pt_regs
*regs
, sigset_t
*oldset
, int in_syscall
)
505 struct k_sigaction ka
;
507 printk(KERN_INFO
"do signal: %p %p %d\n", regs
, oldset
, in_syscall
);
508 printk(KERN_INFO
"do signal2: %lx %lx %ld [%lx]\n", regs
->pc
, regs
->r1
,
509 regs
->r12
, current_thread_info()->flags
);
512 * We want the common case to go fast, which
513 * is why we may in certain cases get here from
514 * kernel mode. Just return without doing anything
517 if (kernel_mode(regs
))
521 oldset
= ¤t
->blocked
;
523 signr
= get_signal_to_deliver(&info
, &ka
, regs
, NULL
);
525 /* Whee! Actually deliver the signal. */
527 handle_restart(regs
, &ka
, 1);
528 handle_signal(signr
, &ka
, &info
, oldset
, regs
);
533 handle_restart(regs
, NULL
, 0);
535 /* Did we come from a system call? */