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>
40 #include "signal-common.h"
42 static int (*save_fp_context
)(struct sigcontext __user
*sc
);
43 static int (*restore_fp_context
)(struct sigcontext __user
*sc
);
45 extern asmlinkage
int _save_fp_context(struct sigcontext __user
*sc
);
46 extern asmlinkage
int _restore_fp_context(struct sigcontext __user
*sc
);
48 extern asmlinkage
int fpu_emulator_save_context(struct sigcontext __user
*sc
);
49 extern asmlinkage
int fpu_emulator_restore_context(struct sigcontext __user
*sc
);
52 u32 sf_ass
[4]; /* argument save space for o32 */
53 u32 sf_pad
[2]; /* Was: signal trampoline */
54 struct sigcontext sf_sc
;
59 u32 rs_ass
[4]; /* argument save space for o32 */
60 u32 rs_pad
[2]; /* Was: signal trampoline */
61 struct siginfo rs_info
;
62 struct ucontext rs_uc
;
68 static int protected_save_fp_context(struct sigcontext __user
*sc
)
74 err
= save_fp_context(sc
); /* this might fail */
78 /* touch the sigcontext and try again */
79 err
= __put_user(0, &sc
->sc_fpregs
[0]) |
80 __put_user(0, &sc
->sc_fpregs
[31]) |
81 __put_user(0, &sc
->sc_fpc_csr
);
83 break; /* really bad sigcontext */
88 static int protected_restore_fp_context(struct sigcontext __user
*sc
)
90 int err
, tmp __maybe_unused
;
94 err
= restore_fp_context(sc
); /* this might fail */
98 /* touch the sigcontext and try again */
99 err
= __get_user(tmp
, &sc
->sc_fpregs
[0]) |
100 __get_user(tmp
, &sc
->sc_fpregs
[31]) |
101 __get_user(tmp
, &sc
->sc_fpc_csr
);
103 break; /* really bad sigcontext */
108 int setup_sigcontext(struct pt_regs
*regs
, struct sigcontext __user
*sc
)
112 unsigned int used_math
;
114 err
|= __put_user(regs
->cp0_epc
, &sc
->sc_pc
);
116 err
|= __put_user(0, &sc
->sc_regs
[0]);
117 for (i
= 1; i
< 32; i
++)
118 err
|= __put_user(regs
->regs
[i
], &sc
->sc_regs
[i
]);
120 #ifdef CONFIG_CPU_HAS_SMARTMIPS
121 err
|= __put_user(regs
->acx
, &sc
->sc_acx
);
123 err
|= __put_user(regs
->hi
, &sc
->sc_mdhi
);
124 err
|= __put_user(regs
->lo
, &sc
->sc_mdlo
);
126 err
|= __put_user(mfhi1(), &sc
->sc_hi1
);
127 err
|= __put_user(mflo1(), &sc
->sc_lo1
);
128 err
|= __put_user(mfhi2(), &sc
->sc_hi2
);
129 err
|= __put_user(mflo2(), &sc
->sc_lo2
);
130 err
|= __put_user(mfhi3(), &sc
->sc_hi3
);
131 err
|= __put_user(mflo3(), &sc
->sc_lo3
);
132 err
|= __put_user(rddsp(DSP_MASK
), &sc
->sc_dsp
);
135 used_math
= !!used_math();
136 err
|= __put_user(used_math
, &sc
->sc_used_math
);
140 * Save FPU state to signal context. Signal handler
141 * will "inherit" current FPU state.
143 err
|= protected_save_fp_context(sc
);
148 int fpcsr_pending(unsigned int __user
*fpcsr
)
151 unsigned int csr
, enabled
;
153 err
= __get_user(csr
, fpcsr
);
154 enabled
= FPU_CSR_UNI_X
| ((csr
& FPU_CSR_ALL_E
) << 5);
156 * If the signal handler set some FPU exceptions, clear it and
161 err
|= __put_user(csr
, fpcsr
);
168 check_and_restore_fp_context(struct sigcontext __user
*sc
)
172 err
= sig
= fpcsr_pending(&sc
->sc_fpc_csr
);
175 err
|= protected_restore_fp_context(sc
);
179 int restore_sigcontext(struct pt_regs
*regs
, struct sigcontext __user
*sc
)
181 unsigned int used_math
;
186 /* Always make any pending restarted system calls return -EINTR */
187 current_thread_info()->restart_block
.fn
= do_no_restart_syscall
;
189 err
|= __get_user(regs
->cp0_epc
, &sc
->sc_pc
);
191 #ifdef CONFIG_CPU_HAS_SMARTMIPS
192 err
|= __get_user(regs
->acx
, &sc
->sc_acx
);
194 err
|= __get_user(regs
->hi
, &sc
->sc_mdhi
);
195 err
|= __get_user(regs
->lo
, &sc
->sc_mdlo
);
197 err
|= __get_user(treg
, &sc
->sc_hi1
); mthi1(treg
);
198 err
|= __get_user(treg
, &sc
->sc_lo1
); mtlo1(treg
);
199 err
|= __get_user(treg
, &sc
->sc_hi2
); mthi2(treg
);
200 err
|= __get_user(treg
, &sc
->sc_lo2
); mtlo2(treg
);
201 err
|= __get_user(treg
, &sc
->sc_hi3
); mthi3(treg
);
202 err
|= __get_user(treg
, &sc
->sc_lo3
); mtlo3(treg
);
203 err
|= __get_user(treg
, &sc
->sc_dsp
); wrdsp(treg
, DSP_MASK
);
206 for (i
= 1; i
< 32; i
++)
207 err
|= __get_user(regs
->regs
[i
], &sc
->sc_regs
[i
]);
209 err
|= __get_user(used_math
, &sc
->sc_used_math
);
210 conditional_used_math(used_math
);
213 /* restore fpu context if we have used it before */
215 err
= check_and_restore_fp_context(sc
);
217 /* signal handler may have used FPU. Give it up. */
224 void __user
*get_sigframe(struct k_sigaction
*ka
, struct pt_regs
*regs
,
229 /* Default to using normal stack */
233 * FPU emulator may have it's own trampoline active just
234 * above the user stack, 16-bytes before the next lowest
235 * 16 byte boundary. Try to avoid trashing it.
239 /* This is the X/Open sanctioned signal stack switching. */
240 if ((ka
->sa
.sa_flags
& SA_ONSTACK
) && (sas_ss_flags (sp
) == 0))
241 sp
= current
->sas_ss_sp
+ current
->sas_ss_size
;
243 return (void __user
*)((sp
- frame_size
) & (ICACHE_REFILLS_WORKAROUND_WAR
? ~(cpu_icache_line_size()-1) : ALMASK
));
247 * Atomically swap in the new signal mask, and wait for a signal.
250 #ifdef CONFIG_TRAD_SIGNALS
251 SYSCALL_DEFINE1(sigsuspend
, sigset_t __user
*, uset
)
253 return sys_rt_sigsuspend(uset
, sizeof(sigset_t
));
257 #ifdef CONFIG_TRAD_SIGNALS
258 SYSCALL_DEFINE3(sigaction
, int, sig
, const struct sigaction __user
*, act
,
259 struct sigaction __user
*, oact
)
261 struct k_sigaction new_ka
, old_ka
;
268 if (!access_ok(VERIFY_READ
, act
, sizeof(*act
)))
270 err
|= __get_user(new_ka
.sa
.sa_handler
, &act
->sa_handler
);
271 err
|= __get_user(new_ka
.sa
.sa_flags
, &act
->sa_flags
);
272 err
|= __get_user(mask
, &act
->sa_mask
.sig
[0]);
276 siginitset(&new_ka
.sa
.sa_mask
, mask
);
279 ret
= do_sigaction(sig
, act
? &new_ka
: NULL
, oact
? &old_ka
: NULL
);
282 if (!access_ok(VERIFY_WRITE
, oact
, sizeof(*oact
)))
284 err
|= __put_user(old_ka
.sa
.sa_flags
, &oact
->sa_flags
);
285 err
|= __put_user(old_ka
.sa
.sa_handler
, &oact
->sa_handler
);
286 err
|= __put_user(old_ka
.sa
.sa_mask
.sig
[0], oact
->sa_mask
.sig
);
287 err
|= __put_user(0, &oact
->sa_mask
.sig
[1]);
288 err
|= __put_user(0, &oact
->sa_mask
.sig
[2]);
289 err
|= __put_user(0, &oact
->sa_mask
.sig
[3]);
298 #ifdef CONFIG_TRAD_SIGNALS
299 asmlinkage
void sys_sigreturn(nabi_no_regargs
struct pt_regs regs
)
301 struct sigframe __user
*frame
;
305 frame
= (struct sigframe __user
*) regs
.regs
[29];
306 if (!access_ok(VERIFY_READ
, frame
, sizeof(*frame
)))
308 if (__copy_from_user(&blocked
, &frame
->sf_mask
, sizeof(blocked
)))
311 set_current_blocked(&blocked
);
313 sig
= restore_sigcontext(®s
, &frame
->sf_sc
);
317 force_sig(sig
, current
);
320 * Don't let your children do this ...
322 __asm__
__volatile__(
330 force_sig(SIGSEGV
, current
);
332 #endif /* CONFIG_TRAD_SIGNALS */
334 asmlinkage
void sys_rt_sigreturn(nabi_no_regargs
struct pt_regs regs
)
336 struct rt_sigframe __user
*frame
;
340 frame
= (struct rt_sigframe __user
*) regs
.regs
[29];
341 if (!access_ok(VERIFY_READ
, frame
, sizeof(*frame
)))
343 if (__copy_from_user(&set
, &frame
->rs_uc
.uc_sigmask
, sizeof(set
)))
346 set_current_blocked(&set
);
348 sig
= restore_sigcontext(®s
, &frame
->rs_uc
.uc_mcontext
);
352 force_sig(sig
, current
);
354 if (restore_altstack(&frame
->rs_uc
.uc_stack
))
358 * Don't let your children do this ...
360 __asm__
__volatile__(
368 force_sig(SIGSEGV
, current
);
371 #ifdef CONFIG_TRAD_SIGNALS
372 static int setup_frame(void *sig_return
, struct k_sigaction
*ka
,
373 struct pt_regs
*regs
, int signr
, sigset_t
*set
)
375 struct sigframe __user
*frame
;
378 frame
= get_sigframe(ka
, regs
, sizeof(*frame
));
379 if (!access_ok(VERIFY_WRITE
, frame
, sizeof (*frame
)))
382 err
|= setup_sigcontext(regs
, &frame
->sf_sc
);
383 err
|= __copy_to_user(&frame
->sf_mask
, set
, sizeof(*set
));
388 * Arguments to signal handler:
391 * a1 = 0 (should be cause)
392 * a2 = pointer to struct sigcontext
394 * $25 and c0_epc point to the signal handler, $29 points to the
397 regs
->regs
[ 4] = signr
;
399 regs
->regs
[ 6] = (unsigned long) &frame
->sf_sc
;
400 regs
->regs
[29] = (unsigned long) frame
;
401 regs
->regs
[31] = (unsigned long) sig_return
;
402 regs
->cp0_epc
= regs
->regs
[25] = (unsigned long) ka
->sa
.sa_handler
;
404 DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
405 current
->comm
, current
->pid
,
406 frame
, regs
->cp0_epc
, regs
->regs
[31]);
410 force_sigsegv(signr
, current
);
415 static int setup_rt_frame(void *sig_return
, struct k_sigaction
*ka
,
416 struct pt_regs
*regs
, int signr
, sigset_t
*set
,
419 struct rt_sigframe __user
*frame
;
422 frame
= get_sigframe(ka
, regs
, sizeof(*frame
));
423 if (!access_ok(VERIFY_WRITE
, frame
, sizeof (*frame
)))
426 /* Create siginfo. */
427 err
|= copy_siginfo_to_user(&frame
->rs_info
, info
);
429 /* Create the ucontext. */
430 err
|= __put_user(0, &frame
->rs_uc
.uc_flags
);
431 err
|= __put_user(NULL
, &frame
->rs_uc
.uc_link
);
432 err
|= __save_altstack(&frame
->rs_uc
.uc_stack
, regs
->regs
[29]);
433 err
|= setup_sigcontext(regs
, &frame
->rs_uc
.uc_mcontext
);
434 err
|= __copy_to_user(&frame
->rs_uc
.uc_sigmask
, set
, sizeof(*set
));
440 * Arguments to signal handler:
443 * a1 = 0 (should be cause)
444 * a2 = pointer to ucontext
446 * $25 and c0_epc point to the signal handler, $29 points to
447 * the struct rt_sigframe.
449 regs
->regs
[ 4] = signr
;
450 regs
->regs
[ 5] = (unsigned long) &frame
->rs_info
;
451 regs
->regs
[ 6] = (unsigned long) &frame
->rs_uc
;
452 regs
->regs
[29] = (unsigned long) frame
;
453 regs
->regs
[31] = (unsigned long) sig_return
;
454 regs
->cp0_epc
= regs
->regs
[25] = (unsigned long) ka
->sa
.sa_handler
;
456 DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
457 current
->comm
, current
->pid
,
458 frame
, regs
->cp0_epc
, regs
->regs
[31]);
463 force_sigsegv(signr
, current
);
467 struct mips_abi mips_abi
= {
468 #ifdef CONFIG_TRAD_SIGNALS
469 .setup_frame
= setup_frame
,
470 .signal_return_offset
= offsetof(struct mips_vdso
, signal_trampoline
),
472 .setup_rt_frame
= setup_rt_frame
,
473 .rt_signal_return_offset
=
474 offsetof(struct mips_vdso
, rt_signal_trampoline
),
475 .restart
= __NR_restart_syscall
478 static void handle_signal(unsigned long sig
, siginfo_t
*info
,
479 struct k_sigaction
*ka
, struct pt_regs
*regs
)
481 sigset_t
*oldset
= sigmask_to_save();
483 struct mips_abi
*abi
= current
->thread
.abi
;
484 #ifdef CONFIG_CPU_MICROMIPS
486 unsigned int tmp
= (unsigned int)current
->mm
->context
.vdso
;
491 void *vdso
= current
->mm
->context
.vdso
;
495 switch(regs
->regs
[2]) {
496 case ERESTART_RESTARTBLOCK
:
498 regs
->regs
[2] = EINTR
;
501 if (!(ka
->sa
.sa_flags
& SA_RESTART
)) {
502 regs
->regs
[2] = EINTR
;
507 regs
->regs
[7] = regs
->regs
[26];
508 regs
->regs
[2] = regs
->regs
[0];
512 regs
->regs
[0] = 0; /* Don't deal with this again. */
515 if (sig_uses_siginfo(ka
))
516 ret
= abi
->setup_rt_frame(vdso
+ abi
->rt_signal_return_offset
,
517 ka
, regs
, sig
, oldset
, info
);
519 ret
= abi
->setup_frame(vdso
+ abi
->signal_return_offset
,
520 ka
, regs
, sig
, oldset
);
525 signal_delivered(sig
, info
, ka
, regs
, 0);
528 static void do_signal(struct pt_regs
*regs
)
530 struct k_sigaction ka
;
534 signr
= get_signal_to_deliver(&info
, &ka
, regs
, NULL
);
536 /* Whee! Actually deliver the signal. */
537 handle_signal(signr
, &info
, &ka
, regs
);
542 switch (regs
->regs
[2]) {
546 regs
->regs
[2] = regs
->regs
[0];
547 regs
->regs
[7] = regs
->regs
[26];
551 case ERESTART_RESTARTBLOCK
:
552 regs
->regs
[2] = current
->thread
.abi
->restart
;
553 regs
->regs
[7] = regs
->regs
[26];
557 regs
->regs
[0] = 0; /* Don't deal with this again. */
561 * If there's no signal to deliver, we just put the saved sigmask
564 restore_saved_sigmask();
568 * notification of userspace execution resumption
569 * - triggered by the TIF_WORK_MASK flags
571 asmlinkage
void do_notify_resume(struct pt_regs
*regs
, void *unused
,
572 __u32 thread_info_flags
)
576 /* deal with pending signal delivery */
577 if (thread_info_flags
& _TIF_SIGPENDING
)
580 if (thread_info_flags
& _TIF_NOTIFY_RESUME
) {
581 clear_thread_flag(TIF_NOTIFY_RESUME
);
582 tracehook_notify_resume(regs
);
587 static int smp_save_fp_context(struct sigcontext __user
*sc
)
589 return raw_cpu_has_fpu
590 ? _save_fp_context(sc
)
591 : fpu_emulator_save_context(sc
);
594 static int smp_restore_fp_context(struct sigcontext __user
*sc
)
596 return raw_cpu_has_fpu
597 ? _restore_fp_context(sc
)
598 : fpu_emulator_restore_context(sc
);
602 static int signal_setup(void)
605 /* For now just do the cpu_has_fpu check when the functions are invoked */
606 save_fp_context
= smp_save_fp_context
;
607 restore_fp_context
= smp_restore_fp_context
;
610 save_fp_context
= _save_fp_context
;
611 restore_fp_context
= _restore_fp_context
;
613 save_fp_context
= fpu_emulator_save_context
;
614 restore_fp_context
= fpu_emulator_restore_context
;
621 arch_initcall(signal_setup
);