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/smp_lock.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/uaccess.h>
27 #include <linux/bitops.h>
28 #include <asm/cacheflush.h>
31 #include <asm/ucontext.h>
32 #include <asm/cpu-features.h>
35 #include "signal-common.h"
38 * Horribly complicated - with the bloody RM9000 workarounds enabled
39 * the signal trampolines is moving to the end of the structure so we can
40 * increase the alignment without breaking software compatibility.
42 #if ICACHE_REFILLS_WORKAROUND_WAR == 0
45 u32 sf_ass
[4]; /* argument save space for o32 */
46 u32 sf_code
[2]; /* signal trampoline */
47 struct sigcontext sf_sc
;
52 u32 rs_ass
[4]; /* argument save space for o32 */
53 u32 rs_code
[2]; /* signal trampoline */
54 struct siginfo rs_info
;
55 struct ucontext rs_uc
;
61 u32 sf_ass
[4]; /* argument save space for o32 */
63 struct sigcontext sf_sc
; /* hw context */
65 u32 sf_code
[8] ____cacheline_aligned
; /* signal trampoline */
69 u32 rs_ass
[4]; /* argument save space for o32 */
71 struct siginfo rs_info
;
72 struct ucontext rs_uc
;
73 u32 rs_code
[8] ____cacheline_aligned
; /* signal trampoline */
81 static int protected_save_fp_context(struct sigcontext __user
*sc
)
87 err
= save_fp_context(sc
); /* this might fail */
91 /* touch the sigcontext and try again */
92 err
= __put_user(0, &sc
->sc_fpregs
[0]) |
93 __put_user(0, &sc
->sc_fpregs
[31]) |
94 __put_user(0, &sc
->sc_fpc_csr
);
96 break; /* really bad sigcontext */
101 static int protected_restore_fp_context(struct sigcontext __user
*sc
)
107 err
= restore_fp_context(sc
); /* this might fail */
111 /* touch the sigcontext and try again */
112 err
= __get_user(tmp
, &sc
->sc_fpregs
[0]) |
113 __get_user(tmp
, &sc
->sc_fpregs
[31]) |
114 __get_user(tmp
, &sc
->sc_fpc_csr
);
116 break; /* really bad sigcontext */
121 int setup_sigcontext(struct pt_regs
*regs
, struct sigcontext __user
*sc
)
125 unsigned int used_math
;
127 err
|= __put_user(regs
->cp0_epc
, &sc
->sc_pc
);
129 err
|= __put_user(0, &sc
->sc_regs
[0]);
130 for (i
= 1; i
< 32; i
++)
131 err
|= __put_user(regs
->regs
[i
], &sc
->sc_regs
[i
]);
133 #ifdef CONFIG_CPU_HAS_SMARTMIPS
134 err
|= __put_user(regs
->acx
, &sc
->sc_acx
);
136 err
|= __put_user(regs
->hi
, &sc
->sc_mdhi
);
137 err
|= __put_user(regs
->lo
, &sc
->sc_mdlo
);
139 err
|= __put_user(mfhi1(), &sc
->sc_hi1
);
140 err
|= __put_user(mflo1(), &sc
->sc_lo1
);
141 err
|= __put_user(mfhi2(), &sc
->sc_hi2
);
142 err
|= __put_user(mflo2(), &sc
->sc_lo2
);
143 err
|= __put_user(mfhi3(), &sc
->sc_hi3
);
144 err
|= __put_user(mflo3(), &sc
->sc_lo3
);
145 err
|= __put_user(rddsp(DSP_MASK
), &sc
->sc_dsp
);
148 used_math
= !!used_math();
149 err
|= __put_user(used_math
, &sc
->sc_used_math
);
153 * Save FPU state to signal context. Signal handler
154 * will "inherit" current FPU state.
156 err
|= protected_save_fp_context(sc
);
161 int fpcsr_pending(unsigned int __user
*fpcsr
)
164 unsigned int csr
, enabled
;
166 err
= __get_user(csr
, fpcsr
);
167 enabled
= FPU_CSR_UNI_X
| ((csr
& FPU_CSR_ALL_E
) << 5);
169 * If the signal handler set some FPU exceptions, clear it and
174 err
|= __put_user(csr
, fpcsr
);
181 check_and_restore_fp_context(struct sigcontext __user
*sc
)
185 err
= sig
= fpcsr_pending(&sc
->sc_fpc_csr
);
188 err
|= protected_restore_fp_context(sc
);
192 int restore_sigcontext(struct pt_regs
*regs
, struct sigcontext __user
*sc
)
194 unsigned int used_math
;
199 /* Always make any pending restarted system calls return -EINTR */
200 current_thread_info()->restart_block
.fn
= do_no_restart_syscall
;
202 err
|= __get_user(regs
->cp0_epc
, &sc
->sc_pc
);
204 #ifdef CONFIG_CPU_HAS_SMARTMIPS
205 err
|= __get_user(regs
->acx
, &sc
->sc_acx
);
207 err
|= __get_user(regs
->hi
, &sc
->sc_mdhi
);
208 err
|= __get_user(regs
->lo
, &sc
->sc_mdlo
);
210 err
|= __get_user(treg
, &sc
->sc_hi1
); mthi1(treg
);
211 err
|= __get_user(treg
, &sc
->sc_lo1
); mtlo1(treg
);
212 err
|= __get_user(treg
, &sc
->sc_hi2
); mthi2(treg
);
213 err
|= __get_user(treg
, &sc
->sc_lo2
); mtlo2(treg
);
214 err
|= __get_user(treg
, &sc
->sc_hi3
); mthi3(treg
);
215 err
|= __get_user(treg
, &sc
->sc_lo3
); mtlo3(treg
);
216 err
|= __get_user(treg
, &sc
->sc_dsp
); wrdsp(treg
, DSP_MASK
);
219 for (i
= 1; i
< 32; i
++)
220 err
|= __get_user(regs
->regs
[i
], &sc
->sc_regs
[i
]);
222 err
|= __get_user(used_math
, &sc
->sc_used_math
);
223 conditional_used_math(used_math
);
226 /* restore fpu context if we have used it before */
228 err
= check_and_restore_fp_context(sc
);
230 /* signal handler may have used FPU. Give it up. */
237 void __user
*get_sigframe(struct k_sigaction
*ka
, struct pt_regs
*regs
,
242 /* Default to using normal stack */
246 * FPU emulator may have it's own trampoline active just
247 * above the user stack, 16-bytes before the next lowest
248 * 16 byte boundary. Try to avoid trashing it.
252 /* This is the X/Open sanctioned signal stack switching. */
253 if ((ka
->sa
.sa_flags
& SA_ONSTACK
) && (sas_ss_flags (sp
) == 0))
254 sp
= current
->sas_ss_sp
+ current
->sas_ss_size
;
256 return (void __user
*)((sp
- frame_size
) & (ICACHE_REFILLS_WORKAROUND_WAR
? ~(cpu_icache_line_size()-1) : ALMASK
));
259 int install_sigtramp(unsigned int __user
*tramp
, unsigned int syscall
)
264 * Set up the return code ...
266 * li v0, __NR__foo_sigreturn
270 err
= __put_user(0x24020000 + syscall
, tramp
+ 0);
271 err
|= __put_user(0x0000000c , tramp
+ 1);
272 if (ICACHE_REFILLS_WORKAROUND_WAR
) {
273 err
|= __put_user(0, tramp
+ 2);
274 err
|= __put_user(0, tramp
+ 3);
275 err
|= __put_user(0, tramp
+ 4);
276 err
|= __put_user(0, tramp
+ 5);
277 err
|= __put_user(0, tramp
+ 6);
278 err
|= __put_user(0, tramp
+ 7);
280 flush_cache_sigtramp((unsigned long) tramp
);
286 * Atomically swap in the new signal mask, and wait for a signal.
289 #ifdef CONFIG_TRAD_SIGNALS
290 asmlinkage
int sys_sigsuspend(nabi_no_regargs
struct pt_regs regs
)
293 sigset_t __user
*uset
;
295 uset
= (sigset_t __user
*) regs
.regs
[4];
296 if (copy_from_user(&newset
, uset
, sizeof(sigset_t
)))
298 sigdelsetmask(&newset
, ~_BLOCKABLE
);
300 spin_lock_irq(¤t
->sighand
->siglock
);
301 current
->saved_sigmask
= current
->blocked
;
302 current
->blocked
= newset
;
304 spin_unlock_irq(¤t
->sighand
->siglock
);
306 current
->state
= TASK_INTERRUPTIBLE
;
308 set_thread_flag(TIF_RESTORE_SIGMASK
);
309 return -ERESTARTNOHAND
;
313 asmlinkage
int sys_rt_sigsuspend(nabi_no_regargs
struct pt_regs regs
)
316 sigset_t __user
*unewset
;
319 /* XXX Don't preclude handling different sized sigset_t's. */
320 sigsetsize
= regs
.regs
[5];
321 if (sigsetsize
!= sizeof(sigset_t
))
324 unewset
= (sigset_t __user
*) regs
.regs
[4];
325 if (copy_from_user(&newset
, unewset
, sizeof(newset
)))
327 sigdelsetmask(&newset
, ~_BLOCKABLE
);
329 spin_lock_irq(¤t
->sighand
->siglock
);
330 current
->saved_sigmask
= current
->blocked
;
331 current
->blocked
= newset
;
333 spin_unlock_irq(¤t
->sighand
->siglock
);
335 current
->state
= TASK_INTERRUPTIBLE
;
337 set_thread_flag(TIF_RESTORE_SIGMASK
);
338 return -ERESTARTNOHAND
;
341 #ifdef CONFIG_TRAD_SIGNALS
342 asmlinkage
int sys_sigaction(int sig
, const struct sigaction __user
*act
,
343 struct sigaction __user
*oact
)
345 struct k_sigaction new_ka
, old_ka
;
352 if (!access_ok(VERIFY_READ
, act
, sizeof(*act
)))
354 err
|= __get_user(new_ka
.sa
.sa_handler
, &act
->sa_handler
);
355 err
|= __get_user(new_ka
.sa
.sa_flags
, &act
->sa_flags
);
356 err
|= __get_user(mask
, &act
->sa_mask
.sig
[0]);
360 siginitset(&new_ka
.sa
.sa_mask
, mask
);
363 ret
= do_sigaction(sig
, act
? &new_ka
: NULL
, oact
? &old_ka
: NULL
);
366 if (!access_ok(VERIFY_WRITE
, oact
, sizeof(*oact
)))
368 err
|= __put_user(old_ka
.sa
.sa_flags
, &oact
->sa_flags
);
369 err
|= __put_user(old_ka
.sa
.sa_handler
, &oact
->sa_handler
);
370 err
|= __put_user(old_ka
.sa
.sa_mask
.sig
[0], oact
->sa_mask
.sig
);
371 err
|= __put_user(0, &oact
->sa_mask
.sig
[1]);
372 err
|= __put_user(0, &oact
->sa_mask
.sig
[2]);
373 err
|= __put_user(0, &oact
->sa_mask
.sig
[3]);
382 asmlinkage
int sys_sigaltstack(nabi_no_regargs
struct pt_regs regs
)
384 const stack_t __user
*uss
= (const stack_t __user
*) regs
.regs
[4];
385 stack_t __user
*uoss
= (stack_t __user
*) regs
.regs
[5];
386 unsigned long usp
= regs
.regs
[29];
388 return do_sigaltstack(uss
, uoss
, usp
);
391 #ifdef CONFIG_TRAD_SIGNALS
392 asmlinkage
void sys_sigreturn(nabi_no_regargs
struct pt_regs regs
)
394 struct sigframe __user
*frame
;
398 frame
= (struct sigframe __user
*) regs
.regs
[29];
399 if (!access_ok(VERIFY_READ
, frame
, sizeof(*frame
)))
401 if (__copy_from_user(&blocked
, &frame
->sf_mask
, sizeof(blocked
)))
404 sigdelsetmask(&blocked
, ~_BLOCKABLE
);
405 spin_lock_irq(¤t
->sighand
->siglock
);
406 current
->blocked
= blocked
;
408 spin_unlock_irq(¤t
->sighand
->siglock
);
410 sig
= restore_sigcontext(®s
, &frame
->sf_sc
);
414 force_sig(sig
, current
);
417 * Don't let your children do this ...
419 __asm__
__volatile__(
427 force_sig(SIGSEGV
, current
);
429 #endif /* CONFIG_TRAD_SIGNALS */
431 asmlinkage
void sys_rt_sigreturn(nabi_no_regargs
struct pt_regs regs
)
433 struct rt_sigframe __user
*frame
;
438 frame
= (struct rt_sigframe __user
*) regs
.regs
[29];
439 if (!access_ok(VERIFY_READ
, frame
, sizeof(*frame
)))
441 if (__copy_from_user(&set
, &frame
->rs_uc
.uc_sigmask
, sizeof(set
)))
444 sigdelsetmask(&set
, ~_BLOCKABLE
);
445 spin_lock_irq(¤t
->sighand
->siglock
);
446 current
->blocked
= set
;
448 spin_unlock_irq(¤t
->sighand
->siglock
);
450 sig
= restore_sigcontext(®s
, &frame
->rs_uc
.uc_mcontext
);
454 force_sig(sig
, current
);
456 if (__copy_from_user(&st
, &frame
->rs_uc
.uc_stack
, sizeof(st
)))
458 /* It is more difficult to avoid calling this function than to
459 call it and ignore errors. */
460 do_sigaltstack((stack_t __user
*)&st
, NULL
, regs
.regs
[29]);
463 * Don't let your children do this ...
465 __asm__
__volatile__(
473 force_sig(SIGSEGV
, current
);
476 #ifdef CONFIG_TRAD_SIGNALS
477 static int setup_frame(struct k_sigaction
* ka
, struct pt_regs
*regs
,
478 int signr
, sigset_t
*set
)
480 struct sigframe __user
*frame
;
483 frame
= get_sigframe(ka
, regs
, sizeof(*frame
));
484 if (!access_ok(VERIFY_WRITE
, frame
, sizeof (*frame
)))
487 err
|= install_sigtramp(frame
->sf_code
, __NR_sigreturn
);
489 err
|= setup_sigcontext(regs
, &frame
->sf_sc
);
490 err
|= __copy_to_user(&frame
->sf_mask
, set
, sizeof(*set
));
495 * Arguments to signal handler:
498 * a1 = 0 (should be cause)
499 * a2 = pointer to struct sigcontext
501 * $25 and c0_epc point to the signal handler, $29 points to the
504 regs
->regs
[ 4] = signr
;
506 regs
->regs
[ 6] = (unsigned long) &frame
->sf_sc
;
507 regs
->regs
[29] = (unsigned long) frame
;
508 regs
->regs
[31] = (unsigned long) frame
->sf_code
;
509 regs
->cp0_epc
= regs
->regs
[25] = (unsigned long) ka
->sa
.sa_handler
;
511 DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
512 current
->comm
, current
->pid
,
513 frame
, regs
->cp0_epc
, regs
->regs
[31]);
517 force_sigsegv(signr
, current
);
522 static int setup_rt_frame(struct k_sigaction
* ka
, struct pt_regs
*regs
,
523 int signr
, sigset_t
*set
, siginfo_t
*info
)
525 struct rt_sigframe __user
*frame
;
528 frame
= get_sigframe(ka
, regs
, sizeof(*frame
));
529 if (!access_ok(VERIFY_WRITE
, frame
, sizeof (*frame
)))
532 err
|= install_sigtramp(frame
->rs_code
, __NR_rt_sigreturn
);
534 /* Create siginfo. */
535 err
|= copy_siginfo_to_user(&frame
->rs_info
, info
);
537 /* Create the ucontext. */
538 err
|= __put_user(0, &frame
->rs_uc
.uc_flags
);
539 err
|= __put_user(NULL
, &frame
->rs_uc
.uc_link
);
540 err
|= __put_user((void __user
*)current
->sas_ss_sp
,
541 &frame
->rs_uc
.uc_stack
.ss_sp
);
542 err
|= __put_user(sas_ss_flags(regs
->regs
[29]),
543 &frame
->rs_uc
.uc_stack
.ss_flags
);
544 err
|= __put_user(current
->sas_ss_size
,
545 &frame
->rs_uc
.uc_stack
.ss_size
);
546 err
|= setup_sigcontext(regs
, &frame
->rs_uc
.uc_mcontext
);
547 err
|= __copy_to_user(&frame
->rs_uc
.uc_sigmask
, set
, sizeof(*set
));
553 * Arguments to signal handler:
556 * a1 = 0 (should be cause)
557 * a2 = pointer to ucontext
559 * $25 and c0_epc point to the signal handler, $29 points to
560 * the struct rt_sigframe.
562 regs
->regs
[ 4] = signr
;
563 regs
->regs
[ 5] = (unsigned long) &frame
->rs_info
;
564 regs
->regs
[ 6] = (unsigned long) &frame
->rs_uc
;
565 regs
->regs
[29] = (unsigned long) frame
;
566 regs
->regs
[31] = (unsigned long) frame
->rs_code
;
567 regs
->cp0_epc
= regs
->regs
[25] = (unsigned long) ka
->sa
.sa_handler
;
569 DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
570 current
->comm
, current
->pid
,
571 frame
, regs
->cp0_epc
, regs
->regs
[31]);
576 force_sigsegv(signr
, current
);
580 struct mips_abi mips_abi
= {
581 #ifdef CONFIG_TRAD_SIGNALS
582 .setup_frame
= setup_frame
,
584 .setup_rt_frame
= setup_rt_frame
,
585 .restart
= __NR_restart_syscall
588 static int handle_signal(unsigned long sig
, siginfo_t
*info
,
589 struct k_sigaction
*ka
, sigset_t
*oldset
, struct pt_regs
*regs
)
593 switch(regs
->regs
[0]) {
594 case ERESTART_RESTARTBLOCK
:
596 regs
->regs
[2] = EINTR
;
599 if (!(ka
->sa
.sa_flags
& SA_RESTART
)) {
600 regs
->regs
[2] = EINTR
;
604 case ERESTARTNOINTR
: /* Userland will reload $v0. */
605 regs
->regs
[7] = regs
->regs
[26];
609 regs
->regs
[0] = 0; /* Don't deal with this again. */
611 if (sig_uses_siginfo(ka
))
612 ret
= current
->thread
.abi
->setup_rt_frame(ka
, regs
, sig
, oldset
, info
);
614 ret
= current
->thread
.abi
->setup_frame(ka
, regs
, sig
, oldset
);
616 spin_lock_irq(¤t
->sighand
->siglock
);
617 sigorsets(¤t
->blocked
,¤t
->blocked
,&ka
->sa
.sa_mask
);
618 if (!(ka
->sa
.sa_flags
& SA_NODEFER
))
619 sigaddset(¤t
->blocked
,sig
);
621 spin_unlock_irq(¤t
->sighand
->siglock
);
626 static void do_signal(struct pt_regs
*regs
)
628 struct k_sigaction ka
;
634 * We want the common case to go fast, which is why we may in certain
635 * cases get here from kernel mode. Just return without doing anything
638 if (!user_mode(regs
))
641 if (test_thread_flag(TIF_RESTORE_SIGMASK
))
642 oldset
= ¤t
->saved_sigmask
;
644 oldset
= ¤t
->blocked
;
646 signr
= get_signal_to_deliver(&info
, &ka
, regs
, NULL
);
648 /* Whee! Actually deliver the signal. */
649 if (handle_signal(signr
, &info
, &ka
, oldset
, regs
) == 0) {
651 * A signal was successfully delivered; the saved
652 * sigmask will have been stored in the signal frame,
653 * and will be restored by sigreturn, so we can simply
654 * clear the TIF_RESTORE_SIGMASK flag.
656 if (test_thread_flag(TIF_RESTORE_SIGMASK
))
657 clear_thread_flag(TIF_RESTORE_SIGMASK
);
664 * Who's code doesn't conform to the restartable syscall convention
665 * dies here!!! The li instruction, a single machine instruction,
666 * must directly be followed by the syscall instruction.
669 if (regs
->regs
[2] == ERESTARTNOHAND
||
670 regs
->regs
[2] == ERESTARTSYS
||
671 regs
->regs
[2] == ERESTARTNOINTR
) {
672 regs
->regs
[7] = regs
->regs
[26];
675 if (regs
->regs
[2] == ERESTART_RESTARTBLOCK
) {
676 regs
->regs
[2] = current
->thread
.abi
->restart
;
677 regs
->regs
[7] = regs
->regs
[26];
680 regs
->regs
[0] = 0; /* Don't deal with this again. */
684 * If there's no signal to deliver, we just put the saved sigmask
687 if (test_thread_flag(TIF_RESTORE_SIGMASK
)) {
688 clear_thread_flag(TIF_RESTORE_SIGMASK
);
689 sigprocmask(SIG_SETMASK
, ¤t
->saved_sigmask
, NULL
);
694 * notification of userspace execution resumption
695 * - triggered by the TIF_WORK_MASK flags
697 asmlinkage
void do_notify_resume(struct pt_regs
*regs
, void *unused
,
698 __u32 thread_info_flags
)
700 /* deal with pending signal delivery */
701 if (thread_info_flags
& (_TIF_SIGPENDING
| _TIF_RESTORE_SIGMASK
))