2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
6 * Copyright (C) 1991, 1992 Linus Torvalds
7 * Copyright (C) 1994 - 2000 Ralf Baechle
8 * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
10 #include <linux/cache.h>
11 #include <linux/sched.h>
13 #include <linux/personality.h>
14 #include <linux/smp.h>
15 #include <linux/kernel.h>
16 #include <linux/signal.h>
17 #include <linux/errno.h>
18 #include <linux/wait.h>
19 #include <linux/ptrace.h>
20 #include <linux/unistd.h>
21 #include <linux/compiler.h>
22 #include <linux/syscalls.h>
23 #include <linux/uaccess.h>
24 #include <linux/tracehook.h>
28 #include <linux/bitops.h>
29 #include <asm/cacheflush.h>
32 #include <asm/ucontext.h>
33 #include <asm/cpu-features.h>
36 #include "signal-common.h"
38 static int (*save_fp_context
)(struct sigcontext __user
*sc
);
39 static int (*restore_fp_context
)(struct sigcontext __user
*sc
);
41 extern asmlinkage
int _save_fp_context(struct sigcontext __user
*sc
);
42 extern asmlinkage
int _restore_fp_context(struct sigcontext __user
*sc
);
44 extern asmlinkage
int fpu_emulator_save_context(struct sigcontext __user
*sc
);
45 extern asmlinkage
int fpu_emulator_restore_context(struct sigcontext __user
*sc
);
48 * Horribly complicated - with the bloody RM9000 workarounds enabled
49 * the signal trampolines is moving to the end of the structure so we can
50 * increase the alignment without breaking software compatibility.
52 #if ICACHE_REFILLS_WORKAROUND_WAR == 0
55 u32 sf_ass
[4]; /* argument save space for o32 */
56 u32 sf_code
[2]; /* signal trampoline */
57 struct sigcontext sf_sc
;
62 u32 rs_ass
[4]; /* argument save space for o32 */
63 u32 rs_code
[2]; /* signal trampoline */
64 struct siginfo rs_info
;
65 struct ucontext rs_uc
;
71 u32 sf_ass
[4]; /* argument save space for o32 */
73 struct sigcontext sf_sc
; /* hw context */
75 u32 sf_code
[8] ____cacheline_aligned
; /* signal trampoline */
79 u32 rs_ass
[4]; /* argument save space for o32 */
81 struct siginfo rs_info
;
82 struct ucontext rs_uc
;
83 u32 rs_code
[8] ____cacheline_aligned
; /* signal trampoline */
91 static int protected_save_fp_context(struct sigcontext __user
*sc
)
97 err
= save_fp_context(sc
); /* this might fail */
101 /* touch the sigcontext and try again */
102 err
= __put_user(0, &sc
->sc_fpregs
[0]) |
103 __put_user(0, &sc
->sc_fpregs
[31]) |
104 __put_user(0, &sc
->sc_fpc_csr
);
106 break; /* really bad sigcontext */
111 static int protected_restore_fp_context(struct sigcontext __user
*sc
)
117 err
= restore_fp_context(sc
); /* this might fail */
121 /* touch the sigcontext and try again */
122 err
= __get_user(tmp
, &sc
->sc_fpregs
[0]) |
123 __get_user(tmp
, &sc
->sc_fpregs
[31]) |
124 __get_user(tmp
, &sc
->sc_fpc_csr
);
126 break; /* really bad sigcontext */
131 int setup_sigcontext(struct pt_regs
*regs
, struct sigcontext __user
*sc
)
135 unsigned int used_math
;
137 err
|= __put_user(regs
->cp0_epc
, &sc
->sc_pc
);
139 err
|= __put_user(0, &sc
->sc_regs
[0]);
140 for (i
= 1; i
< 32; i
++)
141 err
|= __put_user(regs
->regs
[i
], &sc
->sc_regs
[i
]);
143 #ifdef CONFIG_CPU_HAS_SMARTMIPS
144 err
|= __put_user(regs
->acx
, &sc
->sc_acx
);
146 err
|= __put_user(regs
->hi
, &sc
->sc_mdhi
);
147 err
|= __put_user(regs
->lo
, &sc
->sc_mdlo
);
149 err
|= __put_user(mfhi1(), &sc
->sc_hi1
);
150 err
|= __put_user(mflo1(), &sc
->sc_lo1
);
151 err
|= __put_user(mfhi2(), &sc
->sc_hi2
);
152 err
|= __put_user(mflo2(), &sc
->sc_lo2
);
153 err
|= __put_user(mfhi3(), &sc
->sc_hi3
);
154 err
|= __put_user(mflo3(), &sc
->sc_lo3
);
155 err
|= __put_user(rddsp(DSP_MASK
), &sc
->sc_dsp
);
158 used_math
= !!used_math();
159 err
|= __put_user(used_math
, &sc
->sc_used_math
);
163 * Save FPU state to signal context. Signal handler
164 * will "inherit" current FPU state.
166 err
|= protected_save_fp_context(sc
);
171 int fpcsr_pending(unsigned int __user
*fpcsr
)
174 unsigned int csr
, enabled
;
176 err
= __get_user(csr
, fpcsr
);
177 enabled
= FPU_CSR_UNI_X
| ((csr
& FPU_CSR_ALL_E
) << 5);
179 * If the signal handler set some FPU exceptions, clear it and
184 err
|= __put_user(csr
, fpcsr
);
191 check_and_restore_fp_context(struct sigcontext __user
*sc
)
195 err
= sig
= fpcsr_pending(&sc
->sc_fpc_csr
);
198 err
|= protected_restore_fp_context(sc
);
202 int restore_sigcontext(struct pt_regs
*regs
, struct sigcontext __user
*sc
)
204 unsigned int used_math
;
209 /* Always make any pending restarted system calls return -EINTR */
210 current_thread_info()->restart_block
.fn
= do_no_restart_syscall
;
212 err
|= __get_user(regs
->cp0_epc
, &sc
->sc_pc
);
214 #ifdef CONFIG_CPU_HAS_SMARTMIPS
215 err
|= __get_user(regs
->acx
, &sc
->sc_acx
);
217 err
|= __get_user(regs
->hi
, &sc
->sc_mdhi
);
218 err
|= __get_user(regs
->lo
, &sc
->sc_mdlo
);
220 err
|= __get_user(treg
, &sc
->sc_hi1
); mthi1(treg
);
221 err
|= __get_user(treg
, &sc
->sc_lo1
); mtlo1(treg
);
222 err
|= __get_user(treg
, &sc
->sc_hi2
); mthi2(treg
);
223 err
|= __get_user(treg
, &sc
->sc_lo2
); mtlo2(treg
);
224 err
|= __get_user(treg
, &sc
->sc_hi3
); mthi3(treg
);
225 err
|= __get_user(treg
, &sc
->sc_lo3
); mtlo3(treg
);
226 err
|= __get_user(treg
, &sc
->sc_dsp
); wrdsp(treg
, DSP_MASK
);
229 for (i
= 1; i
< 32; i
++)
230 err
|= __get_user(regs
->regs
[i
], &sc
->sc_regs
[i
]);
232 err
|= __get_user(used_math
, &sc
->sc_used_math
);
233 conditional_used_math(used_math
);
236 /* restore fpu context if we have used it before */
238 err
= check_and_restore_fp_context(sc
);
240 /* signal handler may have used FPU. Give it up. */
247 void __user
*get_sigframe(struct k_sigaction
*ka
, struct pt_regs
*regs
,
252 /* Default to using normal stack */
256 * FPU emulator may have it's own trampoline active just
257 * above the user stack, 16-bytes before the next lowest
258 * 16 byte boundary. Try to avoid trashing it.
262 /* This is the X/Open sanctioned signal stack switching. */
263 if ((ka
->sa
.sa_flags
& SA_ONSTACK
) && (sas_ss_flags (sp
) == 0))
264 sp
= current
->sas_ss_sp
+ current
->sas_ss_size
;
266 return (void __user
*)((sp
- frame_size
) & (ICACHE_REFILLS_WORKAROUND_WAR
? ~(cpu_icache_line_size()-1) : ALMASK
));
269 int install_sigtramp(unsigned int __user
*tramp
, unsigned int syscall
)
274 * Set up the return code ...
276 * li v0, __NR__foo_sigreturn
280 err
= __put_user(0x24020000 + syscall
, tramp
+ 0);
281 err
|= __put_user(0x0000000c , tramp
+ 1);
282 if (ICACHE_REFILLS_WORKAROUND_WAR
) {
283 err
|= __put_user(0, tramp
+ 2);
284 err
|= __put_user(0, tramp
+ 3);
285 err
|= __put_user(0, tramp
+ 4);
286 err
|= __put_user(0, tramp
+ 5);
287 err
|= __put_user(0, tramp
+ 6);
288 err
|= __put_user(0, tramp
+ 7);
290 flush_cache_sigtramp((unsigned long) tramp
);
296 * Atomically swap in the new signal mask, and wait for a signal.
299 #ifdef CONFIG_TRAD_SIGNALS
300 asmlinkage
int sys_sigsuspend(nabi_no_regargs
struct pt_regs regs
)
303 sigset_t __user
*uset
;
305 uset
= (sigset_t __user
*) regs
.regs
[4];
306 if (copy_from_user(&newset
, uset
, sizeof(sigset_t
)))
308 sigdelsetmask(&newset
, ~_BLOCKABLE
);
310 spin_lock_irq(¤t
->sighand
->siglock
);
311 current
->saved_sigmask
= current
->blocked
;
312 current
->blocked
= newset
;
314 spin_unlock_irq(¤t
->sighand
->siglock
);
316 current
->state
= TASK_INTERRUPTIBLE
;
318 set_thread_flag(TIF_RESTORE_SIGMASK
);
319 return -ERESTARTNOHAND
;
323 asmlinkage
int sys_rt_sigsuspend(nabi_no_regargs
struct pt_regs regs
)
326 sigset_t __user
*unewset
;
329 /* XXX Don't preclude handling different sized sigset_t's. */
330 sigsetsize
= regs
.regs
[5];
331 if (sigsetsize
!= sizeof(sigset_t
))
334 unewset
= (sigset_t __user
*) regs
.regs
[4];
335 if (copy_from_user(&newset
, unewset
, sizeof(newset
)))
337 sigdelsetmask(&newset
, ~_BLOCKABLE
);
339 spin_lock_irq(¤t
->sighand
->siglock
);
340 current
->saved_sigmask
= current
->blocked
;
341 current
->blocked
= newset
;
343 spin_unlock_irq(¤t
->sighand
->siglock
);
345 current
->state
= TASK_INTERRUPTIBLE
;
347 set_thread_flag(TIF_RESTORE_SIGMASK
);
348 return -ERESTARTNOHAND
;
351 #ifdef CONFIG_TRAD_SIGNALS
352 SYSCALL_DEFINE3(sigaction
, int, sig
, const struct sigaction __user
*, act
,
353 struct sigaction __user
*, oact
)
355 struct k_sigaction new_ka
, old_ka
;
362 if (!access_ok(VERIFY_READ
, act
, sizeof(*act
)))
364 err
|= __get_user(new_ka
.sa
.sa_handler
, &act
->sa_handler
);
365 err
|= __get_user(new_ka
.sa
.sa_flags
, &act
->sa_flags
);
366 err
|= __get_user(mask
, &act
->sa_mask
.sig
[0]);
370 siginitset(&new_ka
.sa
.sa_mask
, mask
);
373 ret
= do_sigaction(sig
, act
? &new_ka
: NULL
, oact
? &old_ka
: NULL
);
376 if (!access_ok(VERIFY_WRITE
, oact
, sizeof(*oact
)))
378 err
|= __put_user(old_ka
.sa
.sa_flags
, &oact
->sa_flags
);
379 err
|= __put_user(old_ka
.sa
.sa_handler
, &oact
->sa_handler
);
380 err
|= __put_user(old_ka
.sa
.sa_mask
.sig
[0], oact
->sa_mask
.sig
);
381 err
|= __put_user(0, &oact
->sa_mask
.sig
[1]);
382 err
|= __put_user(0, &oact
->sa_mask
.sig
[2]);
383 err
|= __put_user(0, &oact
->sa_mask
.sig
[3]);
392 asmlinkage
int sys_sigaltstack(nabi_no_regargs
struct pt_regs regs
)
394 const stack_t __user
*uss
= (const stack_t __user
*) regs
.regs
[4];
395 stack_t __user
*uoss
= (stack_t __user
*) regs
.regs
[5];
396 unsigned long usp
= regs
.regs
[29];
398 return do_sigaltstack(uss
, uoss
, usp
);
401 #ifdef CONFIG_TRAD_SIGNALS
402 asmlinkage
void sys_sigreturn(nabi_no_regargs
struct pt_regs regs
)
404 struct sigframe __user
*frame
;
408 frame
= (struct sigframe __user
*) regs
.regs
[29];
409 if (!access_ok(VERIFY_READ
, frame
, sizeof(*frame
)))
411 if (__copy_from_user(&blocked
, &frame
->sf_mask
, sizeof(blocked
)))
414 sigdelsetmask(&blocked
, ~_BLOCKABLE
);
415 spin_lock_irq(¤t
->sighand
->siglock
);
416 current
->blocked
= blocked
;
418 spin_unlock_irq(¤t
->sighand
->siglock
);
420 sig
= restore_sigcontext(®s
, &frame
->sf_sc
);
424 force_sig(sig
, current
);
427 * Don't let your children do this ...
429 __asm__
__volatile__(
437 force_sig(SIGSEGV
, current
);
439 #endif /* CONFIG_TRAD_SIGNALS */
441 asmlinkage
void sys_rt_sigreturn(nabi_no_regargs
struct pt_regs regs
)
443 struct rt_sigframe __user
*frame
;
448 frame
= (struct rt_sigframe __user
*) regs
.regs
[29];
449 if (!access_ok(VERIFY_READ
, frame
, sizeof(*frame
)))
451 if (__copy_from_user(&set
, &frame
->rs_uc
.uc_sigmask
, sizeof(set
)))
454 sigdelsetmask(&set
, ~_BLOCKABLE
);
455 spin_lock_irq(¤t
->sighand
->siglock
);
456 current
->blocked
= set
;
458 spin_unlock_irq(¤t
->sighand
->siglock
);
460 sig
= restore_sigcontext(®s
, &frame
->rs_uc
.uc_mcontext
);
464 force_sig(sig
, current
);
466 if (__copy_from_user(&st
, &frame
->rs_uc
.uc_stack
, sizeof(st
)))
468 /* It is more difficult to avoid calling this function than to
469 call it and ignore errors. */
470 do_sigaltstack((stack_t __user
*)&st
, NULL
, regs
.regs
[29]);
473 * Don't let your children do this ...
475 __asm__
__volatile__(
483 force_sig(SIGSEGV
, current
);
486 #ifdef CONFIG_TRAD_SIGNALS
487 static int setup_frame(struct k_sigaction
* ka
, struct pt_regs
*regs
,
488 int signr
, sigset_t
*set
)
490 struct sigframe __user
*frame
;
493 frame
= get_sigframe(ka
, regs
, sizeof(*frame
));
494 if (!access_ok(VERIFY_WRITE
, frame
, sizeof (*frame
)))
497 err
|= install_sigtramp(frame
->sf_code
, __NR_sigreturn
);
499 err
|= setup_sigcontext(regs
, &frame
->sf_sc
);
500 err
|= __copy_to_user(&frame
->sf_mask
, set
, sizeof(*set
));
505 * Arguments to signal handler:
508 * a1 = 0 (should be cause)
509 * a2 = pointer to struct sigcontext
511 * $25 and c0_epc point to the signal handler, $29 points to the
514 regs
->regs
[ 4] = signr
;
516 regs
->regs
[ 6] = (unsigned long) &frame
->sf_sc
;
517 regs
->regs
[29] = (unsigned long) frame
;
518 regs
->regs
[31] = (unsigned long) frame
->sf_code
;
519 regs
->cp0_epc
= regs
->regs
[25] = (unsigned long) ka
->sa
.sa_handler
;
521 DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
522 current
->comm
, current
->pid
,
523 frame
, regs
->cp0_epc
, regs
->regs
[31]);
527 force_sigsegv(signr
, current
);
532 static int setup_rt_frame(struct k_sigaction
* ka
, struct pt_regs
*regs
,
533 int signr
, sigset_t
*set
, siginfo_t
*info
)
535 struct rt_sigframe __user
*frame
;
538 frame
= get_sigframe(ka
, regs
, sizeof(*frame
));
539 if (!access_ok(VERIFY_WRITE
, frame
, sizeof (*frame
)))
542 err
|= install_sigtramp(frame
->rs_code
, __NR_rt_sigreturn
);
544 /* Create siginfo. */
545 err
|= copy_siginfo_to_user(&frame
->rs_info
, info
);
547 /* Create the ucontext. */
548 err
|= __put_user(0, &frame
->rs_uc
.uc_flags
);
549 err
|= __put_user(NULL
, &frame
->rs_uc
.uc_link
);
550 err
|= __put_user((void __user
*)current
->sas_ss_sp
,
551 &frame
->rs_uc
.uc_stack
.ss_sp
);
552 err
|= __put_user(sas_ss_flags(regs
->regs
[29]),
553 &frame
->rs_uc
.uc_stack
.ss_flags
);
554 err
|= __put_user(current
->sas_ss_size
,
555 &frame
->rs_uc
.uc_stack
.ss_size
);
556 err
|= setup_sigcontext(regs
, &frame
->rs_uc
.uc_mcontext
);
557 err
|= __copy_to_user(&frame
->rs_uc
.uc_sigmask
, set
, sizeof(*set
));
563 * Arguments to signal handler:
566 * a1 = 0 (should be cause)
567 * a2 = pointer to ucontext
569 * $25 and c0_epc point to the signal handler, $29 points to
570 * the struct rt_sigframe.
572 regs
->regs
[ 4] = signr
;
573 regs
->regs
[ 5] = (unsigned long) &frame
->rs_info
;
574 regs
->regs
[ 6] = (unsigned long) &frame
->rs_uc
;
575 regs
->regs
[29] = (unsigned long) frame
;
576 regs
->regs
[31] = (unsigned long) frame
->rs_code
;
577 regs
->cp0_epc
= regs
->regs
[25] = (unsigned long) ka
->sa
.sa_handler
;
579 DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
580 current
->comm
, current
->pid
,
581 frame
, regs
->cp0_epc
, regs
->regs
[31]);
586 force_sigsegv(signr
, current
);
590 struct mips_abi mips_abi
= {
591 #ifdef CONFIG_TRAD_SIGNALS
592 .setup_frame
= setup_frame
,
594 .setup_rt_frame
= setup_rt_frame
,
595 .restart
= __NR_restart_syscall
598 static int handle_signal(unsigned long sig
, siginfo_t
*info
,
599 struct k_sigaction
*ka
, sigset_t
*oldset
, struct pt_regs
*regs
)
603 switch(regs
->regs
[0]) {
604 case ERESTART_RESTARTBLOCK
:
606 regs
->regs
[2] = EINTR
;
609 if (!(ka
->sa
.sa_flags
& SA_RESTART
)) {
610 regs
->regs
[2] = EINTR
;
614 case ERESTARTNOINTR
: /* Userland will reload $v0. */
615 regs
->regs
[7] = regs
->regs
[26];
619 regs
->regs
[0] = 0; /* Don't deal with this again. */
621 if (sig_uses_siginfo(ka
))
622 ret
= current
->thread
.abi
->setup_rt_frame(ka
, regs
, sig
, oldset
, info
);
624 ret
= current
->thread
.abi
->setup_frame(ka
, regs
, sig
, oldset
);
626 spin_lock_irq(¤t
->sighand
->siglock
);
627 sigorsets(¤t
->blocked
, ¤t
->blocked
, &ka
->sa
.sa_mask
);
628 if (!(ka
->sa
.sa_flags
& SA_NODEFER
))
629 sigaddset(¤t
->blocked
, sig
);
631 spin_unlock_irq(¤t
->sighand
->siglock
);
636 static void do_signal(struct pt_regs
*regs
)
638 struct k_sigaction ka
;
644 * We want the common case to go fast, which is why we may in certain
645 * cases get here from kernel mode. Just return without doing anything
648 if (!user_mode(regs
))
651 if (test_thread_flag(TIF_RESTORE_SIGMASK
))
652 oldset
= ¤t
->saved_sigmask
;
654 oldset
= ¤t
->blocked
;
656 signr
= get_signal_to_deliver(&info
, &ka
, regs
, NULL
);
658 /* Whee! Actually deliver the signal. */
659 if (handle_signal(signr
, &info
, &ka
, oldset
, regs
) == 0) {
661 * A signal was successfully delivered; the saved
662 * sigmask will have been stored in the signal frame,
663 * and will be restored by sigreturn, so we can simply
664 * clear the TIF_RESTORE_SIGMASK flag.
666 if (test_thread_flag(TIF_RESTORE_SIGMASK
))
667 clear_thread_flag(TIF_RESTORE_SIGMASK
);
674 * Who's code doesn't conform to the restartable syscall convention
675 * dies here!!! The li instruction, a single machine instruction,
676 * must directly be followed by the syscall instruction.
679 if (regs
->regs
[2] == ERESTARTNOHAND
||
680 regs
->regs
[2] == ERESTARTSYS
||
681 regs
->regs
[2] == ERESTARTNOINTR
) {
682 regs
->regs
[7] = regs
->regs
[26];
685 if (regs
->regs
[2] == ERESTART_RESTARTBLOCK
) {
686 regs
->regs
[2] = current
->thread
.abi
->restart
;
687 regs
->regs
[7] = regs
->regs
[26];
690 regs
->regs
[0] = 0; /* Don't deal with this again. */
694 * If there's no signal to deliver, we just put the saved sigmask
697 if (test_thread_flag(TIF_RESTORE_SIGMASK
)) {
698 clear_thread_flag(TIF_RESTORE_SIGMASK
);
699 sigprocmask(SIG_SETMASK
, ¤t
->saved_sigmask
, NULL
);
704 * notification of userspace execution resumption
705 * - triggered by the TIF_WORK_MASK flags
707 asmlinkage
void do_notify_resume(struct pt_regs
*regs
, void *unused
,
708 __u32 thread_info_flags
)
710 /* deal with pending signal delivery */
711 if (thread_info_flags
& (_TIF_SIGPENDING
| _TIF_RESTORE_SIGMASK
))
714 if (thread_info_flags
& _TIF_NOTIFY_RESUME
) {
715 clear_thread_flag(TIF_NOTIFY_RESUME
);
716 tracehook_notify_resume(regs
);
717 if (current
->replacement_session_keyring
)
718 key_replace_session_keyring();
723 static int smp_save_fp_context(struct sigcontext __user
*sc
)
725 return raw_cpu_has_fpu
726 ? _save_fp_context(sc
)
727 : fpu_emulator_save_context(sc
);
730 static int smp_restore_fp_context(struct sigcontext __user
*sc
)
732 return raw_cpu_has_fpu
733 ? _restore_fp_context(sc
)
734 : fpu_emulator_restore_context(sc
);
738 static int signal_setup(void)
741 /* For now just do the cpu_has_fpu check when the functions are invoked */
742 save_fp_context
= smp_save_fp_context
;
743 restore_fp_context
= smp_restore_fp_context
;
746 save_fp_context
= _save_fp_context
;
747 restore_fp_context
= _restore_fp_context
;
749 save_fp_context
= fpu_emulator_save_context
;
750 restore_fp_context
= fpu_emulator_restore_context
;
757 arch_initcall(signal_setup
);