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"
39 * Horribly complicated - with the bloody RM9000 workarounds enabled
40 * the signal trampolines is moving to the end of the structure so we can
41 * increase the alignment without breaking software compatibility.
43 #if ICACHE_REFILLS_WORKAROUND_WAR == 0
46 u32 sf_ass
[4]; /* argument save space for o32 */
47 u32 sf_code
[2]; /* signal trampoline */
48 struct sigcontext sf_sc
;
53 u32 rs_ass
[4]; /* argument save space for o32 */
54 u32 rs_code
[2]; /* signal trampoline */
55 struct siginfo rs_info
;
56 struct ucontext rs_uc
;
62 u32 sf_ass
[4]; /* argument save space for o32 */
64 struct sigcontext sf_sc
; /* hw context */
66 u32 sf_code
[8] ____cacheline_aligned
; /* signal trampoline */
70 u32 rs_ass
[4]; /* argument save space for o32 */
72 struct siginfo rs_info
;
73 struct ucontext rs_uc
;
74 u32 rs_code
[8] ____cacheline_aligned
; /* signal trampoline */
82 static int protected_save_fp_context(struct sigcontext __user
*sc
)
88 err
= save_fp_context(sc
); /* this might fail */
92 /* touch the sigcontext and try again */
93 err
= __put_user(0, &sc
->sc_fpregs
[0]) |
94 __put_user(0, &sc
->sc_fpregs
[31]) |
95 __put_user(0, &sc
->sc_fpc_csr
);
97 break; /* really bad sigcontext */
102 static int protected_restore_fp_context(struct sigcontext __user
*sc
)
108 err
= restore_fp_context(sc
); /* this might fail */
112 /* touch the sigcontext and try again */
113 err
= __get_user(tmp
, &sc
->sc_fpregs
[0]) |
114 __get_user(tmp
, &sc
->sc_fpregs
[31]) |
115 __get_user(tmp
, &sc
->sc_fpc_csr
);
117 break; /* really bad sigcontext */
122 int setup_sigcontext(struct pt_regs
*regs
, struct sigcontext __user
*sc
)
126 unsigned int used_math
;
128 err
|= __put_user(regs
->cp0_epc
, &sc
->sc_pc
);
130 err
|= __put_user(0, &sc
->sc_regs
[0]);
131 for (i
= 1; i
< 32; i
++)
132 err
|= __put_user(regs
->regs
[i
], &sc
->sc_regs
[i
]);
134 #ifdef CONFIG_CPU_HAS_SMARTMIPS
135 err
|= __put_user(regs
->acx
, &sc
->sc_acx
);
137 err
|= __put_user(regs
->hi
, &sc
->sc_mdhi
);
138 err
|= __put_user(regs
->lo
, &sc
->sc_mdlo
);
140 err
|= __put_user(mfhi1(), &sc
->sc_hi1
);
141 err
|= __put_user(mflo1(), &sc
->sc_lo1
);
142 err
|= __put_user(mfhi2(), &sc
->sc_hi2
);
143 err
|= __put_user(mflo2(), &sc
->sc_lo2
);
144 err
|= __put_user(mfhi3(), &sc
->sc_hi3
);
145 err
|= __put_user(mflo3(), &sc
->sc_lo3
);
146 err
|= __put_user(rddsp(DSP_MASK
), &sc
->sc_dsp
);
149 used_math
= !!used_math();
150 err
|= __put_user(used_math
, &sc
->sc_used_math
);
154 * Save FPU state to signal context. Signal handler
155 * will "inherit" current FPU state.
157 err
|= protected_save_fp_context(sc
);
162 int fpcsr_pending(unsigned int __user
*fpcsr
)
165 unsigned int csr
, enabled
;
167 err
= __get_user(csr
, fpcsr
);
168 enabled
= FPU_CSR_UNI_X
| ((csr
& FPU_CSR_ALL_E
) << 5);
170 * If the signal handler set some FPU exceptions, clear it and
175 err
|= __put_user(csr
, fpcsr
);
182 check_and_restore_fp_context(struct sigcontext __user
*sc
)
186 err
= sig
= fpcsr_pending(&sc
->sc_fpc_csr
);
189 err
|= protected_restore_fp_context(sc
);
193 int restore_sigcontext(struct pt_regs
*regs
, struct sigcontext __user
*sc
)
195 unsigned int used_math
;
200 /* Always make any pending restarted system calls return -EINTR */
201 current_thread_info()->restart_block
.fn
= do_no_restart_syscall
;
203 err
|= __get_user(regs
->cp0_epc
, &sc
->sc_pc
);
205 #ifdef CONFIG_CPU_HAS_SMARTMIPS
206 err
|= __get_user(regs
->acx
, &sc
->sc_acx
);
208 err
|= __get_user(regs
->hi
, &sc
->sc_mdhi
);
209 err
|= __get_user(regs
->lo
, &sc
->sc_mdlo
);
211 err
|= __get_user(treg
, &sc
->sc_hi1
); mthi1(treg
);
212 err
|= __get_user(treg
, &sc
->sc_lo1
); mtlo1(treg
);
213 err
|= __get_user(treg
, &sc
->sc_hi2
); mthi2(treg
);
214 err
|= __get_user(treg
, &sc
->sc_lo2
); mtlo2(treg
);
215 err
|= __get_user(treg
, &sc
->sc_hi3
); mthi3(treg
);
216 err
|= __get_user(treg
, &sc
->sc_lo3
); mtlo3(treg
);
217 err
|= __get_user(treg
, &sc
->sc_dsp
); wrdsp(treg
, DSP_MASK
);
220 for (i
= 1; i
< 32; i
++)
221 err
|= __get_user(regs
->regs
[i
], &sc
->sc_regs
[i
]);
223 err
|= __get_user(used_math
, &sc
->sc_used_math
);
224 conditional_used_math(used_math
);
227 /* restore fpu context if we have used it before */
229 err
= check_and_restore_fp_context(sc
);
231 /* signal handler may have used FPU. Give it up. */
238 void __user
*get_sigframe(struct k_sigaction
*ka
, struct pt_regs
*regs
,
243 /* Default to using normal stack */
247 * FPU emulator may have it's own trampoline active just
248 * above the user stack, 16-bytes before the next lowest
249 * 16 byte boundary. Try to avoid trashing it.
253 /* This is the X/Open sanctioned signal stack switching. */
254 if ((ka
->sa
.sa_flags
& SA_ONSTACK
) && (sas_ss_flags (sp
) == 0))
255 sp
= current
->sas_ss_sp
+ current
->sas_ss_size
;
257 return (void __user
*)((sp
- frame_size
) & (ICACHE_REFILLS_WORKAROUND_WAR
? ~(cpu_icache_line_size()-1) : ALMASK
));
260 int install_sigtramp(unsigned int __user
*tramp
, unsigned int syscall
)
265 * Set up the return code ...
267 * li v0, __NR__foo_sigreturn
271 err
= __put_user(0x24020000 + syscall
, tramp
+ 0);
272 err
|= __put_user(0x0000000c , tramp
+ 1);
273 if (ICACHE_REFILLS_WORKAROUND_WAR
) {
274 err
|= __put_user(0, tramp
+ 2);
275 err
|= __put_user(0, tramp
+ 3);
276 err
|= __put_user(0, tramp
+ 4);
277 err
|= __put_user(0, tramp
+ 5);
278 err
|= __put_user(0, tramp
+ 6);
279 err
|= __put_user(0, tramp
+ 7);
281 flush_cache_sigtramp((unsigned long) tramp
);
287 * Atomically swap in the new signal mask, and wait for a signal.
290 #ifdef CONFIG_TRAD_SIGNALS
291 asmlinkage
int sys_sigsuspend(nabi_no_regargs
struct pt_regs regs
)
294 sigset_t __user
*uset
;
296 uset
= (sigset_t __user
*) regs
.regs
[4];
297 if (copy_from_user(&newset
, uset
, sizeof(sigset_t
)))
299 sigdelsetmask(&newset
, ~_BLOCKABLE
);
301 spin_lock_irq(¤t
->sighand
->siglock
);
302 current
->saved_sigmask
= current
->blocked
;
303 current
->blocked
= newset
;
305 spin_unlock_irq(¤t
->sighand
->siglock
);
307 current
->state
= TASK_INTERRUPTIBLE
;
309 set_thread_flag(TIF_RESTORE_SIGMASK
);
310 return -ERESTARTNOHAND
;
314 asmlinkage
int sys_rt_sigsuspend(nabi_no_regargs
struct pt_regs regs
)
317 sigset_t __user
*unewset
;
320 /* XXX Don't preclude handling different sized sigset_t's. */
321 sigsetsize
= regs
.regs
[5];
322 if (sigsetsize
!= sizeof(sigset_t
))
325 unewset
= (sigset_t __user
*) regs
.regs
[4];
326 if (copy_from_user(&newset
, unewset
, sizeof(newset
)))
328 sigdelsetmask(&newset
, ~_BLOCKABLE
);
330 spin_lock_irq(¤t
->sighand
->siglock
);
331 current
->saved_sigmask
= current
->blocked
;
332 current
->blocked
= newset
;
334 spin_unlock_irq(¤t
->sighand
->siglock
);
336 current
->state
= TASK_INTERRUPTIBLE
;
338 set_thread_flag(TIF_RESTORE_SIGMASK
);
339 return -ERESTARTNOHAND
;
342 #ifdef CONFIG_TRAD_SIGNALS
343 SYSCALL_DEFINE3(sigaction
, int, sig
, const struct sigaction __user
*, act
,
344 struct sigaction __user
*, oact
)
346 struct k_sigaction new_ka
, old_ka
;
353 if (!access_ok(VERIFY_READ
, act
, sizeof(*act
)))
355 err
|= __get_user(new_ka
.sa
.sa_handler
, &act
->sa_handler
);
356 err
|= __get_user(new_ka
.sa
.sa_flags
, &act
->sa_flags
);
357 err
|= __get_user(mask
, &act
->sa_mask
.sig
[0]);
361 siginitset(&new_ka
.sa
.sa_mask
, mask
);
364 ret
= do_sigaction(sig
, act
? &new_ka
: NULL
, oact
? &old_ka
: NULL
);
367 if (!access_ok(VERIFY_WRITE
, oact
, sizeof(*oact
)))
369 err
|= __put_user(old_ka
.sa
.sa_flags
, &oact
->sa_flags
);
370 err
|= __put_user(old_ka
.sa
.sa_handler
, &oact
->sa_handler
);
371 err
|= __put_user(old_ka
.sa
.sa_mask
.sig
[0], oact
->sa_mask
.sig
);
372 err
|= __put_user(0, &oact
->sa_mask
.sig
[1]);
373 err
|= __put_user(0, &oact
->sa_mask
.sig
[2]);
374 err
|= __put_user(0, &oact
->sa_mask
.sig
[3]);
383 asmlinkage
int sys_sigaltstack(nabi_no_regargs
struct pt_regs regs
)
385 const stack_t __user
*uss
= (const stack_t __user
*) regs
.regs
[4];
386 stack_t __user
*uoss
= (stack_t __user
*) regs
.regs
[5];
387 unsigned long usp
= regs
.regs
[29];
389 return do_sigaltstack(uss
, uoss
, usp
);
392 #ifdef CONFIG_TRAD_SIGNALS
393 asmlinkage
void sys_sigreturn(nabi_no_regargs
struct pt_regs regs
)
395 struct sigframe __user
*frame
;
399 frame
= (struct sigframe __user
*) regs
.regs
[29];
400 if (!access_ok(VERIFY_READ
, frame
, sizeof(*frame
)))
402 if (__copy_from_user(&blocked
, &frame
->sf_mask
, sizeof(blocked
)))
405 sigdelsetmask(&blocked
, ~_BLOCKABLE
);
406 spin_lock_irq(¤t
->sighand
->siglock
);
407 current
->blocked
= blocked
;
409 spin_unlock_irq(¤t
->sighand
->siglock
);
411 sig
= restore_sigcontext(®s
, &frame
->sf_sc
);
415 force_sig(sig
, current
);
418 * Don't let your children do this ...
420 __asm__
__volatile__(
428 force_sig(SIGSEGV
, current
);
430 #endif /* CONFIG_TRAD_SIGNALS */
432 asmlinkage
void sys_rt_sigreturn(nabi_no_regargs
struct pt_regs regs
)
434 struct rt_sigframe __user
*frame
;
439 frame
= (struct rt_sigframe __user
*) regs
.regs
[29];
440 if (!access_ok(VERIFY_READ
, frame
, sizeof(*frame
)))
442 if (__copy_from_user(&set
, &frame
->rs_uc
.uc_sigmask
, sizeof(set
)))
445 sigdelsetmask(&set
, ~_BLOCKABLE
);
446 spin_lock_irq(¤t
->sighand
->siglock
);
447 current
->blocked
= set
;
449 spin_unlock_irq(¤t
->sighand
->siglock
);
451 sig
= restore_sigcontext(®s
, &frame
->rs_uc
.uc_mcontext
);
455 force_sig(sig
, current
);
457 if (__copy_from_user(&st
, &frame
->rs_uc
.uc_stack
, sizeof(st
)))
459 /* It is more difficult to avoid calling this function than to
460 call it and ignore errors. */
461 do_sigaltstack((stack_t __user
*)&st
, NULL
, regs
.regs
[29]);
464 * Don't let your children do this ...
466 __asm__
__volatile__(
474 force_sig(SIGSEGV
, current
);
477 #ifdef CONFIG_TRAD_SIGNALS
478 static int setup_frame(struct k_sigaction
* ka
, struct pt_regs
*regs
,
479 int signr
, sigset_t
*set
)
481 struct sigframe __user
*frame
;
484 frame
= get_sigframe(ka
, regs
, sizeof(*frame
));
485 if (!access_ok(VERIFY_WRITE
, frame
, sizeof (*frame
)))
488 err
|= install_sigtramp(frame
->sf_code
, __NR_sigreturn
);
490 err
|= setup_sigcontext(regs
, &frame
->sf_sc
);
491 err
|= __copy_to_user(&frame
->sf_mask
, set
, sizeof(*set
));
496 * Arguments to signal handler:
499 * a1 = 0 (should be cause)
500 * a2 = pointer to struct sigcontext
502 * $25 and c0_epc point to the signal handler, $29 points to the
505 regs
->regs
[ 4] = signr
;
507 regs
->regs
[ 6] = (unsigned long) &frame
->sf_sc
;
508 regs
->regs
[29] = (unsigned long) frame
;
509 regs
->regs
[31] = (unsigned long) frame
->sf_code
;
510 regs
->cp0_epc
= regs
->regs
[25] = (unsigned long) ka
->sa
.sa_handler
;
512 DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
513 current
->comm
, current
->pid
,
514 frame
, regs
->cp0_epc
, regs
->regs
[31]);
518 force_sigsegv(signr
, current
);
523 static int setup_rt_frame(struct k_sigaction
* ka
, struct pt_regs
*regs
,
524 int signr
, sigset_t
*set
, siginfo_t
*info
)
526 struct rt_sigframe __user
*frame
;
529 frame
= get_sigframe(ka
, regs
, sizeof(*frame
));
530 if (!access_ok(VERIFY_WRITE
, frame
, sizeof (*frame
)))
533 err
|= install_sigtramp(frame
->rs_code
, __NR_rt_sigreturn
);
535 /* Create siginfo. */
536 err
|= copy_siginfo_to_user(&frame
->rs_info
, info
);
538 /* Create the ucontext. */
539 err
|= __put_user(0, &frame
->rs_uc
.uc_flags
);
540 err
|= __put_user(NULL
, &frame
->rs_uc
.uc_link
);
541 err
|= __put_user((void __user
*)current
->sas_ss_sp
,
542 &frame
->rs_uc
.uc_stack
.ss_sp
);
543 err
|= __put_user(sas_ss_flags(regs
->regs
[29]),
544 &frame
->rs_uc
.uc_stack
.ss_flags
);
545 err
|= __put_user(current
->sas_ss_size
,
546 &frame
->rs_uc
.uc_stack
.ss_size
);
547 err
|= setup_sigcontext(regs
, &frame
->rs_uc
.uc_mcontext
);
548 err
|= __copy_to_user(&frame
->rs_uc
.uc_sigmask
, set
, sizeof(*set
));
554 * Arguments to signal handler:
557 * a1 = 0 (should be cause)
558 * a2 = pointer to ucontext
560 * $25 and c0_epc point to the signal handler, $29 points to
561 * the struct rt_sigframe.
563 regs
->regs
[ 4] = signr
;
564 regs
->regs
[ 5] = (unsigned long) &frame
->rs_info
;
565 regs
->regs
[ 6] = (unsigned long) &frame
->rs_uc
;
566 regs
->regs
[29] = (unsigned long) frame
;
567 regs
->regs
[31] = (unsigned long) frame
->rs_code
;
568 regs
->cp0_epc
= regs
->regs
[25] = (unsigned long) ka
->sa
.sa_handler
;
570 DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
571 current
->comm
, current
->pid
,
572 frame
, regs
->cp0_epc
, regs
->regs
[31]);
577 force_sigsegv(signr
, current
);
581 struct mips_abi mips_abi
= {
582 #ifdef CONFIG_TRAD_SIGNALS
583 .setup_frame
= setup_frame
,
585 .setup_rt_frame
= setup_rt_frame
,
586 .restart
= __NR_restart_syscall
589 static int handle_signal(unsigned long sig
, siginfo_t
*info
,
590 struct k_sigaction
*ka
, sigset_t
*oldset
, struct pt_regs
*regs
)
594 switch(regs
->regs
[0]) {
595 case ERESTART_RESTARTBLOCK
:
597 regs
->regs
[2] = EINTR
;
600 if (!(ka
->sa
.sa_flags
& SA_RESTART
)) {
601 regs
->regs
[2] = EINTR
;
605 case ERESTARTNOINTR
: /* Userland will reload $v0. */
606 regs
->regs
[7] = regs
->regs
[26];
610 regs
->regs
[0] = 0; /* Don't deal with this again. */
612 if (sig_uses_siginfo(ka
))
613 ret
= current
->thread
.abi
->setup_rt_frame(ka
, regs
, sig
, oldset
, info
);
615 ret
= current
->thread
.abi
->setup_frame(ka
, regs
, sig
, oldset
);
617 spin_lock_irq(¤t
->sighand
->siglock
);
618 sigorsets(¤t
->blocked
, ¤t
->blocked
, &ka
->sa
.sa_mask
);
619 if (!(ka
->sa
.sa_flags
& SA_NODEFER
))
620 sigaddset(¤t
->blocked
, sig
);
622 spin_unlock_irq(¤t
->sighand
->siglock
);
627 static void do_signal(struct pt_regs
*regs
)
629 struct k_sigaction ka
;
635 * We want the common case to go fast, which is why we may in certain
636 * cases get here from kernel mode. Just return without doing anything
639 if (!user_mode(regs
))
642 if (test_thread_flag(TIF_RESTORE_SIGMASK
))
643 oldset
= ¤t
->saved_sigmask
;
645 oldset
= ¤t
->blocked
;
647 signr
= get_signal_to_deliver(&info
, &ka
, regs
, NULL
);
649 /* Whee! Actually deliver the signal. */
650 if (handle_signal(signr
, &info
, &ka
, oldset
, regs
) == 0) {
652 * A signal was successfully delivered; the saved
653 * sigmask will have been stored in the signal frame,
654 * and will be restored by sigreturn, so we can simply
655 * clear the TIF_RESTORE_SIGMASK flag.
657 if (test_thread_flag(TIF_RESTORE_SIGMASK
))
658 clear_thread_flag(TIF_RESTORE_SIGMASK
);
665 * Who's code doesn't conform to the restartable syscall convention
666 * dies here!!! The li instruction, a single machine instruction,
667 * must directly be followed by the syscall instruction.
670 if (regs
->regs
[2] == ERESTARTNOHAND
||
671 regs
->regs
[2] == ERESTARTSYS
||
672 regs
->regs
[2] == ERESTARTNOINTR
) {
673 regs
->regs
[7] = regs
->regs
[26];
676 if (regs
->regs
[2] == ERESTART_RESTARTBLOCK
) {
677 regs
->regs
[2] = current
->thread
.abi
->restart
;
678 regs
->regs
[7] = regs
->regs
[26];
681 regs
->regs
[0] = 0; /* Don't deal with this again. */
685 * If there's no signal to deliver, we just put the saved sigmask
688 if (test_thread_flag(TIF_RESTORE_SIGMASK
)) {
689 clear_thread_flag(TIF_RESTORE_SIGMASK
);
690 sigprocmask(SIG_SETMASK
, ¤t
->saved_sigmask
, NULL
);
695 * notification of userspace execution resumption
696 * - triggered by the TIF_WORK_MASK flags
698 asmlinkage
void do_notify_resume(struct pt_regs
*regs
, void *unused
,
699 __u32 thread_info_flags
)
701 /* deal with pending signal delivery */
702 if (thread_info_flags
& (_TIF_SIGPENDING
| _TIF_RESTORE_SIGMASK
))
705 if (thread_info_flags
& _TIF_NOTIFY_RESUME
) {
706 clear_thread_flag(TIF_NOTIFY_RESUME
);
707 tracehook_notify_resume(regs
);
708 if (current
->replacement_session_keyring
)
709 key_replace_session_keyring();