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/irqflags.h>
12 #include <linux/sched.h>
14 #include <linux/personality.h>
15 #include <linux/smp.h>
16 #include <linux/kernel.h>
17 #include <linux/signal.h>
18 #include <linux/errno.h>
19 #include <linux/wait.h>
20 #include <linux/ptrace.h>
21 #include <linux/unistd.h>
22 #include <linux/compiler.h>
23 #include <linux/syscalls.h>
24 #include <linux/uaccess.h>
25 #include <linux/tracehook.h>
29 #include <linux/bitops.h>
30 #include <asm/cacheflush.h>
33 #include <asm/ucontext.h>
34 #include <asm/cpu-features.h>
38 #include "signal-common.h"
40 static int (*save_fp_context
)(struct sigcontext __user
*sc
);
41 static int (*restore_fp_context
)(struct sigcontext __user
*sc
);
43 extern asmlinkage
int _save_fp_context(struct sigcontext __user
*sc
);
44 extern asmlinkage
int _restore_fp_context(struct sigcontext __user
*sc
);
46 extern asmlinkage
int fpu_emulator_save_context(struct sigcontext __user
*sc
);
47 extern asmlinkage
int fpu_emulator_restore_context(struct sigcontext __user
*sc
);
50 u32 sf_ass
[4]; /* argument save space for o32 */
51 u32 sf_pad
[2]; /* Was: signal trampoline */
52 struct sigcontext sf_sc
;
57 u32 rs_ass
[4]; /* argument save space for o32 */
58 u32 rs_pad
[2]; /* Was: signal trampoline */
59 struct siginfo rs_info
;
60 struct ucontext rs_uc
;
66 static int protected_save_fp_context(struct sigcontext __user
*sc
)
72 err
= save_fp_context(sc
); /* this might fail */
76 /* touch the sigcontext and try again */
77 err
= __put_user(0, &sc
->sc_fpregs
[0]) |
78 __put_user(0, &sc
->sc_fpregs
[31]) |
79 __put_user(0, &sc
->sc_fpc_csr
);
81 break; /* really bad sigcontext */
86 static int protected_restore_fp_context(struct sigcontext __user
*sc
)
88 int err
, tmp __maybe_unused
;
92 err
= restore_fp_context(sc
); /* this might fail */
96 /* touch the sigcontext and try again */
97 err
= __get_user(tmp
, &sc
->sc_fpregs
[0]) |
98 __get_user(tmp
, &sc
->sc_fpregs
[31]) |
99 __get_user(tmp
, &sc
->sc_fpc_csr
);
101 break; /* really bad sigcontext */
106 int setup_sigcontext(struct pt_regs
*regs
, struct sigcontext __user
*sc
)
110 unsigned int used_math
;
112 err
|= __put_user(regs
->cp0_epc
, &sc
->sc_pc
);
114 err
|= __put_user(0, &sc
->sc_regs
[0]);
115 for (i
= 1; i
< 32; i
++)
116 err
|= __put_user(regs
->regs
[i
], &sc
->sc_regs
[i
]);
118 #ifdef CONFIG_CPU_HAS_SMARTMIPS
119 err
|= __put_user(regs
->acx
, &sc
->sc_acx
);
121 err
|= __put_user(regs
->hi
, &sc
->sc_mdhi
);
122 err
|= __put_user(regs
->lo
, &sc
->sc_mdlo
);
124 err
|= __put_user(mfhi1(), &sc
->sc_hi1
);
125 err
|= __put_user(mflo1(), &sc
->sc_lo1
);
126 err
|= __put_user(mfhi2(), &sc
->sc_hi2
);
127 err
|= __put_user(mflo2(), &sc
->sc_lo2
);
128 err
|= __put_user(mfhi3(), &sc
->sc_hi3
);
129 err
|= __put_user(mflo3(), &sc
->sc_lo3
);
130 err
|= __put_user(rddsp(DSP_MASK
), &sc
->sc_dsp
);
133 used_math
= !!used_math();
134 err
|= __put_user(used_math
, &sc
->sc_used_math
);
138 * Save FPU state to signal context. Signal handler
139 * will "inherit" current FPU state.
141 err
|= protected_save_fp_context(sc
);
146 int fpcsr_pending(unsigned int __user
*fpcsr
)
149 unsigned int csr
, enabled
;
151 err
= __get_user(csr
, fpcsr
);
152 enabled
= FPU_CSR_UNI_X
| ((csr
& FPU_CSR_ALL_E
) << 5);
154 * If the signal handler set some FPU exceptions, clear it and
159 err
|= __put_user(csr
, fpcsr
);
166 check_and_restore_fp_context(struct sigcontext __user
*sc
)
170 err
= sig
= fpcsr_pending(&sc
->sc_fpc_csr
);
173 err
|= protected_restore_fp_context(sc
);
177 int restore_sigcontext(struct pt_regs
*regs
, struct sigcontext __user
*sc
)
179 unsigned int used_math
;
184 /* Always make any pending restarted system calls return -EINTR */
185 current_thread_info()->restart_block
.fn
= do_no_restart_syscall
;
187 err
|= __get_user(regs
->cp0_epc
, &sc
->sc_pc
);
189 #ifdef CONFIG_CPU_HAS_SMARTMIPS
190 err
|= __get_user(regs
->acx
, &sc
->sc_acx
);
192 err
|= __get_user(regs
->hi
, &sc
->sc_mdhi
);
193 err
|= __get_user(regs
->lo
, &sc
->sc_mdlo
);
195 err
|= __get_user(treg
, &sc
->sc_hi1
); mthi1(treg
);
196 err
|= __get_user(treg
, &sc
->sc_lo1
); mtlo1(treg
);
197 err
|= __get_user(treg
, &sc
->sc_hi2
); mthi2(treg
);
198 err
|= __get_user(treg
, &sc
->sc_lo2
); mtlo2(treg
);
199 err
|= __get_user(treg
, &sc
->sc_hi3
); mthi3(treg
);
200 err
|= __get_user(treg
, &sc
->sc_lo3
); mtlo3(treg
);
201 err
|= __get_user(treg
, &sc
->sc_dsp
); wrdsp(treg
, DSP_MASK
);
204 for (i
= 1; i
< 32; i
++)
205 err
|= __get_user(regs
->regs
[i
], &sc
->sc_regs
[i
]);
207 err
|= __get_user(used_math
, &sc
->sc_used_math
);
208 conditional_used_math(used_math
);
211 /* restore fpu context if we have used it before */
213 err
= check_and_restore_fp_context(sc
);
215 /* signal handler may have used FPU. Give it up. */
222 void __user
*get_sigframe(struct k_sigaction
*ka
, struct pt_regs
*regs
,
227 /* Default to using normal stack */
231 * FPU emulator may have it's own trampoline active just
232 * above the user stack, 16-bytes before the next lowest
233 * 16 byte boundary. Try to avoid trashing it.
237 /* This is the X/Open sanctioned signal stack switching. */
238 if ((ka
->sa
.sa_flags
& SA_ONSTACK
) && (sas_ss_flags (sp
) == 0))
239 sp
= current
->sas_ss_sp
+ current
->sas_ss_size
;
241 return (void __user
*)((sp
- frame_size
) & (ICACHE_REFILLS_WORKAROUND_WAR
? ~(cpu_icache_line_size()-1) : ALMASK
));
245 * Atomically swap in the new signal mask, and wait for a signal.
248 #ifdef CONFIG_TRAD_SIGNALS
249 asmlinkage
int sys_sigsuspend(nabi_no_regargs
struct pt_regs regs
)
252 sigset_t __user
*uset
;
254 uset
= (sigset_t __user
*) regs
.regs
[4];
255 if (copy_from_user(&newset
, uset
, sizeof(sigset_t
)))
257 sigdelsetmask(&newset
, ~_BLOCKABLE
);
259 current
->saved_sigmask
= current
->blocked
;
260 set_current_blocked(&newset
);
262 current
->state
= TASK_INTERRUPTIBLE
;
264 set_thread_flag(TIF_RESTORE_SIGMASK
);
265 return -ERESTARTNOHAND
;
269 asmlinkage
int sys_rt_sigsuspend(nabi_no_regargs
struct pt_regs regs
)
272 sigset_t __user
*unewset
;
275 /* XXX Don't preclude handling different sized sigset_t's. */
276 sigsetsize
= regs
.regs
[5];
277 if (sigsetsize
!= sizeof(sigset_t
))
280 unewset
= (sigset_t __user
*) regs
.regs
[4];
281 if (copy_from_user(&newset
, unewset
, sizeof(newset
)))
283 sigdelsetmask(&newset
, ~_BLOCKABLE
);
285 current
->saved_sigmask
= current
->blocked
;
286 set_current_blocked(&newset
);
288 current
->state
= TASK_INTERRUPTIBLE
;
290 set_thread_flag(TIF_RESTORE_SIGMASK
);
291 return -ERESTARTNOHAND
;
294 #ifdef CONFIG_TRAD_SIGNALS
295 SYSCALL_DEFINE3(sigaction
, int, sig
, const struct sigaction __user
*, act
,
296 struct sigaction __user
*, oact
)
298 struct k_sigaction new_ka
, old_ka
;
305 if (!access_ok(VERIFY_READ
, act
, sizeof(*act
)))
307 err
|= __get_user(new_ka
.sa
.sa_handler
, &act
->sa_handler
);
308 err
|= __get_user(new_ka
.sa
.sa_flags
, &act
->sa_flags
);
309 err
|= __get_user(mask
, &act
->sa_mask
.sig
[0]);
313 siginitset(&new_ka
.sa
.sa_mask
, mask
);
316 ret
= do_sigaction(sig
, act
? &new_ka
: NULL
, oact
? &old_ka
: NULL
);
319 if (!access_ok(VERIFY_WRITE
, oact
, sizeof(*oact
)))
321 err
|= __put_user(old_ka
.sa
.sa_flags
, &oact
->sa_flags
);
322 err
|= __put_user(old_ka
.sa
.sa_handler
, &oact
->sa_handler
);
323 err
|= __put_user(old_ka
.sa
.sa_mask
.sig
[0], oact
->sa_mask
.sig
);
324 err
|= __put_user(0, &oact
->sa_mask
.sig
[1]);
325 err
|= __put_user(0, &oact
->sa_mask
.sig
[2]);
326 err
|= __put_user(0, &oact
->sa_mask
.sig
[3]);
335 asmlinkage
int sys_sigaltstack(nabi_no_regargs
struct pt_regs regs
)
337 const stack_t __user
*uss
= (const stack_t __user
*) regs
.regs
[4];
338 stack_t __user
*uoss
= (stack_t __user
*) regs
.regs
[5];
339 unsigned long usp
= regs
.regs
[29];
341 return do_sigaltstack(uss
, uoss
, usp
);
344 #ifdef CONFIG_TRAD_SIGNALS
345 asmlinkage
void sys_sigreturn(nabi_no_regargs
struct pt_regs regs
)
347 struct sigframe __user
*frame
;
351 frame
= (struct sigframe __user
*) regs
.regs
[29];
352 if (!access_ok(VERIFY_READ
, frame
, sizeof(*frame
)))
354 if (__copy_from_user(&blocked
, &frame
->sf_mask
, sizeof(blocked
)))
357 sigdelsetmask(&blocked
, ~_BLOCKABLE
);
358 set_current_blocked(&blocked
);
360 sig
= restore_sigcontext(®s
, &frame
->sf_sc
);
364 force_sig(sig
, current
);
367 * Don't let your children do this ...
369 __asm__
__volatile__(
377 force_sig(SIGSEGV
, current
);
379 #endif /* CONFIG_TRAD_SIGNALS */
381 asmlinkage
void sys_rt_sigreturn(nabi_no_regargs
struct pt_regs regs
)
383 struct rt_sigframe __user
*frame
;
387 frame
= (struct rt_sigframe __user
*) regs
.regs
[29];
388 if (!access_ok(VERIFY_READ
, frame
, sizeof(*frame
)))
390 if (__copy_from_user(&set
, &frame
->rs_uc
.uc_sigmask
, sizeof(set
)))
393 sigdelsetmask(&set
, ~_BLOCKABLE
);
394 set_current_blocked(&set
);
396 sig
= restore_sigcontext(®s
, &frame
->rs_uc
.uc_mcontext
);
400 force_sig(sig
, current
);
402 /* It is more difficult to avoid calling this function than to
403 call it and ignore errors. */
404 do_sigaltstack(&frame
->rs_uc
.uc_stack
, NULL
, regs
.regs
[29]);
407 * Don't let your children do this ...
409 __asm__
__volatile__(
417 force_sig(SIGSEGV
, current
);
420 #ifdef CONFIG_TRAD_SIGNALS
421 static int setup_frame(void *sig_return
, struct k_sigaction
*ka
,
422 struct pt_regs
*regs
, int signr
, sigset_t
*set
)
424 struct sigframe __user
*frame
;
427 frame
= get_sigframe(ka
, regs
, sizeof(*frame
));
428 if (!access_ok(VERIFY_WRITE
, frame
, sizeof (*frame
)))
431 err
|= setup_sigcontext(regs
, &frame
->sf_sc
);
432 err
|= __copy_to_user(&frame
->sf_mask
, set
, sizeof(*set
));
437 * Arguments to signal handler:
440 * a1 = 0 (should be cause)
441 * a2 = pointer to struct sigcontext
443 * $25 and c0_epc point to the signal handler, $29 points to the
446 regs
->regs
[ 4] = signr
;
448 regs
->regs
[ 6] = (unsigned long) &frame
->sf_sc
;
449 regs
->regs
[29] = (unsigned long) frame
;
450 regs
->regs
[31] = (unsigned long) sig_return
;
451 regs
->cp0_epc
= regs
->regs
[25] = (unsigned long) ka
->sa
.sa_handler
;
453 DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
454 current
->comm
, current
->pid
,
455 frame
, regs
->cp0_epc
, regs
->regs
[31]);
459 force_sigsegv(signr
, current
);
464 static int setup_rt_frame(void *sig_return
, struct k_sigaction
*ka
,
465 struct pt_regs
*regs
, int signr
, sigset_t
*set
,
468 struct rt_sigframe __user
*frame
;
471 frame
= get_sigframe(ka
, regs
, sizeof(*frame
));
472 if (!access_ok(VERIFY_WRITE
, frame
, sizeof (*frame
)))
475 /* Create siginfo. */
476 err
|= copy_siginfo_to_user(&frame
->rs_info
, info
);
478 /* Create the ucontext. */
479 err
|= __put_user(0, &frame
->rs_uc
.uc_flags
);
480 err
|= __put_user(NULL
, &frame
->rs_uc
.uc_link
);
481 err
|= __put_user((void __user
*)current
->sas_ss_sp
,
482 &frame
->rs_uc
.uc_stack
.ss_sp
);
483 err
|= __put_user(sas_ss_flags(regs
->regs
[29]),
484 &frame
->rs_uc
.uc_stack
.ss_flags
);
485 err
|= __put_user(current
->sas_ss_size
,
486 &frame
->rs_uc
.uc_stack
.ss_size
);
487 err
|= setup_sigcontext(regs
, &frame
->rs_uc
.uc_mcontext
);
488 err
|= __copy_to_user(&frame
->rs_uc
.uc_sigmask
, set
, sizeof(*set
));
494 * Arguments to signal handler:
497 * a1 = 0 (should be cause)
498 * a2 = pointer to ucontext
500 * $25 and c0_epc point to the signal handler, $29 points to
501 * the struct rt_sigframe.
503 regs
->regs
[ 4] = signr
;
504 regs
->regs
[ 5] = (unsigned long) &frame
->rs_info
;
505 regs
->regs
[ 6] = (unsigned long) &frame
->rs_uc
;
506 regs
->regs
[29] = (unsigned long) frame
;
507 regs
->regs
[31] = (unsigned long) sig_return
;
508 regs
->cp0_epc
= regs
->regs
[25] = (unsigned long) ka
->sa
.sa_handler
;
510 DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
511 current
->comm
, current
->pid
,
512 frame
, regs
->cp0_epc
, regs
->regs
[31]);
517 force_sigsegv(signr
, current
);
521 struct mips_abi mips_abi
= {
522 #ifdef CONFIG_TRAD_SIGNALS
523 .setup_frame
= setup_frame
,
524 .signal_return_offset
= offsetof(struct mips_vdso
, signal_trampoline
),
526 .setup_rt_frame
= setup_rt_frame
,
527 .rt_signal_return_offset
=
528 offsetof(struct mips_vdso
, rt_signal_trampoline
),
529 .restart
= __NR_restart_syscall
532 static int handle_signal(unsigned long sig
, siginfo_t
*info
,
533 struct k_sigaction
*ka
, sigset_t
*oldset
, struct pt_regs
*regs
)
536 struct mips_abi
*abi
= current
->thread
.abi
;
537 void *vdso
= current
->mm
->context
.vdso
;
540 switch(regs
->regs
[2]) {
541 case ERESTART_RESTARTBLOCK
:
543 regs
->regs
[2] = EINTR
;
546 if (!(ka
->sa
.sa_flags
& SA_RESTART
)) {
547 regs
->regs
[2] = EINTR
;
552 regs
->regs
[7] = regs
->regs
[26];
553 regs
->regs
[2] = regs
->regs
[0];
557 regs
->regs
[0] = 0; /* Don't deal with this again. */
560 if (sig_uses_siginfo(ka
))
561 ret
= abi
->setup_rt_frame(vdso
+ abi
->rt_signal_return_offset
,
562 ka
, regs
, sig
, oldset
, info
);
564 ret
= abi
->setup_frame(vdso
+ abi
->signal_return_offset
,
565 ka
, regs
, sig
, oldset
);
570 block_sigmask(ka
, sig
);
575 static void do_signal(struct pt_regs
*regs
)
577 struct k_sigaction ka
;
583 * We want the common case to go fast, which is why we may in certain
584 * cases get here from kernel mode. Just return without doing anything
587 if (!user_mode(regs
))
590 if (test_thread_flag(TIF_RESTORE_SIGMASK
))
591 oldset
= ¤t
->saved_sigmask
;
593 oldset
= ¤t
->blocked
;
595 signr
= get_signal_to_deliver(&info
, &ka
, regs
, NULL
);
597 /* Whee! Actually deliver the signal. */
598 if (handle_signal(signr
, &info
, &ka
, oldset
, regs
) == 0) {
600 * A signal was successfully delivered; the saved
601 * sigmask will have been stored in the signal frame,
602 * and will be restored by sigreturn, so we can simply
603 * clear the TIF_RESTORE_SIGMASK flag.
605 if (test_thread_flag(TIF_RESTORE_SIGMASK
))
606 clear_thread_flag(TIF_RESTORE_SIGMASK
);
613 if (regs
->regs
[2] == ERESTARTNOHAND
||
614 regs
->regs
[2] == ERESTARTSYS
||
615 regs
->regs
[2] == ERESTARTNOINTR
) {
616 regs
->regs
[2] = regs
->regs
[0];
617 regs
->regs
[7] = regs
->regs
[26];
620 if (regs
->regs
[2] == ERESTART_RESTARTBLOCK
) {
621 regs
->regs
[2] = current
->thread
.abi
->restart
;
622 regs
->regs
[7] = regs
->regs
[26];
625 regs
->regs
[0] = 0; /* Don't deal with this again. */
629 * If there's no signal to deliver, we just put the saved sigmask
632 if (test_thread_flag(TIF_RESTORE_SIGMASK
)) {
633 clear_thread_flag(TIF_RESTORE_SIGMASK
);
634 sigprocmask(SIG_SETMASK
, ¤t
->saved_sigmask
, NULL
);
639 * notification of userspace execution resumption
640 * - triggered by the TIF_WORK_MASK flags
642 asmlinkage
void do_notify_resume(struct pt_regs
*regs
, void *unused
,
643 __u32 thread_info_flags
)
647 /* deal with pending signal delivery */
648 if (thread_info_flags
& (_TIF_SIGPENDING
| _TIF_RESTORE_SIGMASK
))
651 if (thread_info_flags
& _TIF_NOTIFY_RESUME
) {
652 clear_thread_flag(TIF_NOTIFY_RESUME
);
653 tracehook_notify_resume(regs
);
654 if (current
->replacement_session_keyring
)
655 key_replace_session_keyring();
660 static int smp_save_fp_context(struct sigcontext __user
*sc
)
662 return raw_cpu_has_fpu
663 ? _save_fp_context(sc
)
664 : fpu_emulator_save_context(sc
);
667 static int smp_restore_fp_context(struct sigcontext __user
*sc
)
669 return raw_cpu_has_fpu
670 ? _restore_fp_context(sc
)
671 : fpu_emulator_restore_context(sc
);
675 static int signal_setup(void)
678 /* For now just do the cpu_has_fpu check when the functions are invoked */
679 save_fp_context
= smp_save_fp_context
;
680 restore_fp_context
= smp_restore_fp_context
;
683 save_fp_context
= _save_fp_context
;
684 restore_fp_context
= _restore_fp_context
;
686 save_fp_context
= fpu_emulator_save_context
;
687 restore_fp_context
= fpu_emulator_restore_context
;
694 arch_initcall(signal_setup
);