1 /* linux/arch/sparc/kernel/signal.c
3 * Copyright (C) 1991, 1992 Linus Torvalds
4 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
5 * Copyright (C) 1996 Miguel de Icaza (miguel@nuclecu.unam.mx)
6 * Copyright (C) 1997 Eddie C. Dost (ecd@skynet.be)
9 #include <linux/sched.h>
10 #include <linux/kernel.h>
11 #include <linux/signal.h>
12 #include <linux/errno.h>
13 #include <linux/wait.h>
14 #include <linux/ptrace.h>
15 #include <linux/unistd.h>
17 #include <linux/tty.h>
18 #include <linux/smp.h>
19 #include <linux/binfmts.h> /* do_coredum */
20 #include <linux/bitops.h>
21 #include <linux/tracehook.h>
23 #include <asm/uaccess.h>
24 #include <asm/ptrace.h>
25 #include <asm/pgalloc.h>
26 #include <asm/pgtable.h>
27 #include <asm/cacheflush.h> /* flush_sig_insns */
29 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
31 extern void fpsave(unsigned long *fpregs
, unsigned long *fsr
,
32 void *fpqueue
, unsigned long *fpqdepth
);
33 extern void fpload(unsigned long *fpregs
, unsigned long *fsr
);
36 struct sparc_stackf ss
;
38 __siginfo_fpu_t __user
*fpu_save
;
39 unsigned long insns
[2] __attribute__ ((aligned (8)));
40 unsigned int extramask
[_NSIG_WORDS
- 1];
41 unsigned int extra_size
; /* Should be 0 */
42 __siginfo_fpu_t fpu_state
;
45 struct rt_signal_frame
{
46 struct sparc_stackf ss
;
50 __siginfo_fpu_t __user
*fpu_save
;
51 unsigned int insns
[2];
53 unsigned int extra_size
; /* Should be 0 */
54 __siginfo_fpu_t fpu_state
;
58 #define SF_ALIGNEDSZ (((sizeof(struct signal_frame) + 7) & (~7)))
59 #define RT_ALIGNEDSZ (((sizeof(struct rt_signal_frame) + 7) & (~7)))
61 static int _sigpause_common(old_sigset_t set
)
64 spin_lock_irq(¤t
->sighand
->siglock
);
65 current
->saved_sigmask
= current
->blocked
;
66 siginitset(¤t
->blocked
, set
);
68 spin_unlock_irq(¤t
->sighand
->siglock
);
70 current
->state
= TASK_INTERRUPTIBLE
;
72 set_thread_flag(TIF_RESTORE_SIGMASK
);
74 return -ERESTARTNOHAND
;
77 asmlinkage
int sys_sigsuspend(old_sigset_t set
)
79 return _sigpause_common(set
);
83 restore_fpu_state(struct pt_regs
*regs
, __siginfo_fpu_t __user
*fpu
)
87 if (test_tsk_thread_flag(current
, TIF_USEDFPU
))
90 if (current
== last_task_used_math
) {
91 last_task_used_math
= NULL
;
96 clear_tsk_thread_flag(current
, TIF_USEDFPU
);
98 if (!access_ok(VERIFY_READ
, fpu
, sizeof(*fpu
)))
101 err
= __copy_from_user(¤t
->thread
.float_regs
[0], &fpu
->si_float_regs
[0],
102 (sizeof(unsigned long) * 32));
103 err
|= __get_user(current
->thread
.fsr
, &fpu
->si_fsr
);
104 err
|= __get_user(current
->thread
.fpqdepth
, &fpu
->si_fpqdepth
);
105 if (current
->thread
.fpqdepth
!= 0)
106 err
|= __copy_from_user(¤t
->thread
.fpqueue
[0],
108 ((sizeof(unsigned long) +
109 (sizeof(unsigned long *)))*16));
113 asmlinkage
void do_sigreturn(struct pt_regs
*regs
)
115 struct signal_frame __user
*sf
;
116 unsigned long up_psr
, pc
, npc
;
118 __siginfo_fpu_t __user
*fpu_save
;
121 /* Always make any pending restarted system calls return -EINTR */
122 current_thread_info()->restart_block
.fn
= do_no_restart_syscall
;
124 synchronize_user_stack();
126 sf
= (struct signal_frame __user
*) regs
->u_regs
[UREG_FP
];
128 /* 1. Make sure we are not getting garbage from the user */
129 if (!access_ok(VERIFY_READ
, sf
, sizeof(*sf
)))
132 if (((unsigned long) sf
) & 3)
135 err
= __get_user(pc
, &sf
->info
.si_regs
.pc
);
136 err
|= __get_user(npc
, &sf
->info
.si_regs
.npc
);
141 /* 2. Restore the state */
143 err
|= __copy_from_user(regs
, &sf
->info
.si_regs
, sizeof(struct pt_regs
));
145 /* User can only change condition codes and FPU enabling in %psr. */
146 regs
->psr
= (up_psr
& ~(PSR_ICC
| PSR_EF
))
147 | (regs
->psr
& (PSR_ICC
| PSR_EF
));
149 /* Prevent syscall restart. */
150 pt_regs_clear_syscall(regs
);
152 err
|= __get_user(fpu_save
, &sf
->fpu_save
);
155 err
|= restore_fpu_state(regs
, fpu_save
);
157 /* This is pretty much atomic, no amount locking would prevent
158 * the races which exist anyways.
160 err
|= __get_user(set
.sig
[0], &sf
->info
.si_mask
);
161 err
|= __copy_from_user(&set
.sig
[1], &sf
->extramask
,
162 (_NSIG_WORDS
-1) * sizeof(unsigned int));
167 sigdelsetmask(&set
, ~_BLOCKABLE
);
168 spin_lock_irq(¤t
->sighand
->siglock
);
169 current
->blocked
= set
;
171 spin_unlock_irq(¤t
->sighand
->siglock
);
175 force_sig(SIGSEGV
, current
);
178 asmlinkage
void do_rt_sigreturn(struct pt_regs
*regs
)
180 struct rt_signal_frame __user
*sf
;
181 unsigned int psr
, pc
, npc
;
182 __siginfo_fpu_t __user
*fpu_save
;
188 synchronize_user_stack();
189 sf
= (struct rt_signal_frame __user
*) regs
->u_regs
[UREG_FP
];
190 if (!access_ok(VERIFY_READ
, sf
, sizeof(*sf
)) ||
191 (((unsigned long) sf
) & 0x03))
194 err
= __get_user(pc
, &sf
->regs
.pc
);
195 err
|= __get_user(npc
, &sf
->regs
.npc
);
196 err
|= ((pc
| npc
) & 0x03);
198 err
|= __get_user(regs
->y
, &sf
->regs
.y
);
199 err
|= __get_user(psr
, &sf
->regs
.psr
);
201 err
|= __copy_from_user(®s
->u_regs
[UREG_G1
],
202 &sf
->regs
.u_regs
[UREG_G1
], 15 * sizeof(u32
));
204 regs
->psr
= (regs
->psr
& ~PSR_ICC
) | (psr
& PSR_ICC
);
206 /* Prevent syscall restart. */
207 pt_regs_clear_syscall(regs
);
209 err
|= __get_user(fpu_save
, &sf
->fpu_save
);
212 err
|= restore_fpu_state(regs
, fpu_save
);
213 err
|= __copy_from_user(&set
, &sf
->mask
, sizeof(sigset_t
));
215 err
|= __copy_from_user(&st
, &sf
->stack
, sizeof(stack_t
));
223 /* It is more difficult to avoid calling this function than to
224 * call it and ignore errors.
228 do_sigaltstack((const stack_t __user
*) &st
, NULL
, (unsigned long)sf
);
231 sigdelsetmask(&set
, ~_BLOCKABLE
);
232 spin_lock_irq(¤t
->sighand
->siglock
);
233 current
->blocked
= set
;
235 spin_unlock_irq(¤t
->sighand
->siglock
);
238 force_sig(SIGSEGV
, current
);
241 /* Checks if the fp is valid */
242 static inline int invalid_frame_pointer(void __user
*fp
, int fplen
)
244 if ((((unsigned long) fp
) & 7) ||
245 !__access_ok((unsigned long)fp
, fplen
) ||
246 ((sparc_cpu_model
== sun4
|| sparc_cpu_model
== sun4c
) &&
247 ((unsigned long) fp
< 0xe0000000 && (unsigned long) fp
>= 0x20000000)))
253 static inline void __user
*get_sigframe(struct sigaction
*sa
, struct pt_regs
*regs
, unsigned long framesize
)
255 unsigned long sp
= regs
->u_regs
[UREG_FP
];
258 * If we are on the alternate signal stack and would overflow it, don't.
259 * Return an always-bogus address instead so we will die with SIGSEGV.
261 if (on_sig_stack(sp
) && !likely(on_sig_stack(sp
- framesize
)))
262 return (void __user
*) -1L;
264 /* This is the X/Open sanctioned signal stack switching. */
265 if (sa
->sa_flags
& SA_ONSTACK
) {
266 if (sas_ss_flags(sp
) == 0)
267 sp
= current
->sas_ss_sp
+ current
->sas_ss_size
;
272 /* Always align the stack frame. This handles two cases. First,
273 * sigaltstack need not be mindful of platform specific stack
274 * alignment. Second, if we took this signal because the stack
275 * is not aligned properly, we'd like to take the signal cleanly
280 return (void __user
*) sp
;
284 save_fpu_state(struct pt_regs
*regs
, __siginfo_fpu_t __user
*fpu
)
288 if (test_tsk_thread_flag(current
, TIF_USEDFPU
)) {
289 put_psr(get_psr() | PSR_EF
);
290 fpsave(¤t
->thread
.float_regs
[0], ¤t
->thread
.fsr
,
291 ¤t
->thread
.fpqueue
[0], ¤t
->thread
.fpqdepth
);
292 regs
->psr
&= ~(PSR_EF
);
293 clear_tsk_thread_flag(current
, TIF_USEDFPU
);
296 if (current
== last_task_used_math
) {
297 put_psr(get_psr() | PSR_EF
);
298 fpsave(¤t
->thread
.float_regs
[0], ¤t
->thread
.fsr
,
299 ¤t
->thread
.fpqueue
[0], ¤t
->thread
.fpqdepth
);
300 last_task_used_math
= NULL
;
301 regs
->psr
&= ~(PSR_EF
);
304 err
|= __copy_to_user(&fpu
->si_float_regs
[0],
305 ¤t
->thread
.float_regs
[0],
306 (sizeof(unsigned long) * 32));
307 err
|= __put_user(current
->thread
.fsr
, &fpu
->si_fsr
);
308 err
|= __put_user(current
->thread
.fpqdepth
, &fpu
->si_fpqdepth
);
309 if (current
->thread
.fpqdepth
!= 0)
310 err
|= __copy_to_user(&fpu
->si_fpqueue
[0],
311 ¤t
->thread
.fpqueue
[0],
312 ((sizeof(unsigned long) +
313 (sizeof(unsigned long *)))*16));
318 static int setup_frame(struct k_sigaction
*ka
, struct pt_regs
*regs
,
319 int signo
, sigset_t
*oldset
)
321 struct signal_frame __user
*sf
;
322 int sigframe_size
, err
;
324 /* 1. Make sure everything is clean */
325 synchronize_user_stack();
327 sigframe_size
= SF_ALIGNEDSZ
;
329 sigframe_size
-= sizeof(__siginfo_fpu_t
);
331 sf
= (struct signal_frame __user
*)
332 get_sigframe(&ka
->sa
, regs
, sigframe_size
);
334 if (invalid_frame_pointer(sf
, sigframe_size
))
335 goto sigill_and_return
;
337 if (current_thread_info()->w_saved
!= 0)
338 goto sigill_and_return
;
340 /* 2. Save the current process state */
341 err
= __copy_to_user(&sf
->info
.si_regs
, regs
, sizeof(struct pt_regs
));
343 err
|= __put_user(0, &sf
->extra_size
);
346 err
|= save_fpu_state(regs
, &sf
->fpu_state
);
347 err
|= __put_user(&sf
->fpu_state
, &sf
->fpu_save
);
349 err
|= __put_user(0, &sf
->fpu_save
);
352 err
|= __put_user(oldset
->sig
[0], &sf
->info
.si_mask
);
353 err
|= __copy_to_user(sf
->extramask
, &oldset
->sig
[1],
354 (_NSIG_WORDS
- 1) * sizeof(unsigned int));
355 err
|= __copy_to_user(sf
, (char *) regs
->u_regs
[UREG_FP
],
356 sizeof(struct reg_window32
));
360 /* 3. signal handler back-trampoline and parameters */
361 regs
->u_regs
[UREG_FP
] = (unsigned long) sf
;
362 regs
->u_regs
[UREG_I0
] = signo
;
363 regs
->u_regs
[UREG_I1
] = (unsigned long) &sf
->info
;
364 regs
->u_regs
[UREG_I2
] = (unsigned long) &sf
->info
;
366 /* 4. signal handler */
367 regs
->pc
= (unsigned long) ka
->sa
.sa_handler
;
368 regs
->npc
= (regs
->pc
+ 4);
370 /* 5. return to kernel instructions */
372 regs
->u_regs
[UREG_I7
] = (unsigned long)ka
->ka_restorer
;
374 regs
->u_regs
[UREG_I7
] = (unsigned long)(&(sf
->insns
[0]) - 2);
376 /* mov __NR_sigreturn, %g1 */
377 err
|= __put_user(0x821020d8, &sf
->insns
[0]);
380 err
|= __put_user(0x91d02010, &sf
->insns
[1]);
384 /* Flush instruction space. */
385 flush_sig_insns(current
->mm
, (unsigned long) &(sf
->insns
[0]));
394 force_sigsegv(signo
, current
);
398 static int setup_rt_frame(struct k_sigaction
*ka
, struct pt_regs
*regs
,
399 int signo
, sigset_t
*oldset
, siginfo_t
*info
)
401 struct rt_signal_frame __user
*sf
;
406 synchronize_user_stack();
407 sigframe_size
= RT_ALIGNEDSZ
;
409 sigframe_size
-= sizeof(__siginfo_fpu_t
);
410 sf
= (struct rt_signal_frame __user
*)
411 get_sigframe(&ka
->sa
, regs
, sigframe_size
);
412 if (invalid_frame_pointer(sf
, sigframe_size
))
414 if (current_thread_info()->w_saved
!= 0)
417 err
= __put_user(regs
->pc
, &sf
->regs
.pc
);
418 err
|= __put_user(regs
->npc
, &sf
->regs
.npc
);
419 err
|= __put_user(regs
->y
, &sf
->regs
.y
);
423 err
|= __put_user(psr
, &sf
->regs
.psr
);
424 err
|= __copy_to_user(&sf
->regs
.u_regs
, regs
->u_regs
, sizeof(regs
->u_regs
));
425 err
|= __put_user(0, &sf
->extra_size
);
428 err
|= save_fpu_state(regs
, &sf
->fpu_state
);
429 err
|= __put_user(&sf
->fpu_state
, &sf
->fpu_save
);
431 err
|= __put_user(0, &sf
->fpu_save
);
433 err
|= __copy_to_user(&sf
->mask
, &oldset
->sig
[0], sizeof(sigset_t
));
435 /* Setup sigaltstack */
436 err
|= __put_user(current
->sas_ss_sp
, &sf
->stack
.ss_sp
);
437 err
|= __put_user(sas_ss_flags(regs
->u_regs
[UREG_FP
]), &sf
->stack
.ss_flags
);
438 err
|= __put_user(current
->sas_ss_size
, &sf
->stack
.ss_size
);
440 err
|= __copy_to_user(sf
, (char *) regs
->u_regs
[UREG_FP
],
441 sizeof(struct reg_window32
));
443 err
|= copy_siginfo_to_user(&sf
->info
, info
);
448 regs
->u_regs
[UREG_FP
] = (unsigned long) sf
;
449 regs
->u_regs
[UREG_I0
] = signo
;
450 regs
->u_regs
[UREG_I1
] = (unsigned long) &sf
->info
;
451 regs
->u_regs
[UREG_I2
] = (unsigned long) &sf
->regs
;
453 regs
->pc
= (unsigned long) ka
->sa
.sa_handler
;
454 regs
->npc
= (regs
->pc
+ 4);
457 regs
->u_regs
[UREG_I7
] = (unsigned long)ka
->ka_restorer
;
459 regs
->u_regs
[UREG_I7
] = (unsigned long)(&(sf
->insns
[0]) - 2);
461 /* mov __NR_sigreturn, %g1 */
462 err
|= __put_user(0x821020d8, &sf
->insns
[0]);
465 err
|= __put_user(0x91d02010, &sf
->insns
[1]);
469 /* Flush instruction space. */
470 flush_sig_insns(current
->mm
, (unsigned long) &(sf
->insns
[0]));
479 force_sigsegv(signo
, current
);
484 handle_signal(unsigned long signr
, struct k_sigaction
*ka
,
485 siginfo_t
*info
, sigset_t
*oldset
, struct pt_regs
*regs
)
489 if (ka
->sa
.sa_flags
& SA_SIGINFO
)
490 err
= setup_rt_frame(ka
, regs
, signr
, oldset
, info
);
492 err
= setup_frame(ka
, regs
, signr
, oldset
);
497 spin_lock_irq(¤t
->sighand
->siglock
);
498 sigorsets(¤t
->blocked
,¤t
->blocked
,&ka
->sa
.sa_mask
);
499 if (!(ka
->sa
.sa_flags
& SA_NOMASK
))
500 sigaddset(¤t
->blocked
, signr
);
502 spin_unlock_irq(¤t
->sighand
->siglock
);
504 tracehook_signal_handler(signr
, info
, ka
, regs
, 0);
509 static inline void syscall_restart(unsigned long orig_i0
, struct pt_regs
*regs
,
510 struct sigaction
*sa
)
512 switch(regs
->u_regs
[UREG_I0
]) {
513 case ERESTART_RESTARTBLOCK
:
515 no_system_call_restart
:
516 regs
->u_regs
[UREG_I0
] = EINTR
;
520 if (!(sa
->sa_flags
& SA_RESTART
))
521 goto no_system_call_restart
;
524 regs
->u_regs
[UREG_I0
] = orig_i0
;
530 /* Note that 'init' is a special process: it doesn't get signals it doesn't
531 * want to handle. Thus you cannot kill init even with a SIGKILL even by
534 static void do_signal(struct pt_regs
*regs
, unsigned long orig_i0
)
536 struct k_sigaction ka
;
542 if (pt_regs_is_syscall(regs
) && (regs
->psr
& PSR_C
))
547 if (test_thread_flag(TIF_RESTORE_SIGMASK
))
548 oldset
= ¤t
->saved_sigmask
;
550 oldset
= ¤t
->blocked
;
552 signr
= get_signal_to_deliver(&info
, &ka
, regs
, NULL
);
554 /* If the debugger messes with the program counter, it clears
555 * the software "in syscall" bit, directing us to not perform
558 if (restart_syscall
&& !pt_regs_is_syscall(regs
))
563 syscall_restart(orig_i0
, regs
, &ka
.sa
);
564 if (handle_signal(signr
, &ka
, &info
, oldset
, regs
) == 0) {
565 /* a signal was successfully delivered; the saved
566 * sigmask will have been stored in the signal frame,
567 * and will be restored by sigreturn, so we can simply
568 * clear the TIF_RESTORE_SIGMASK flag.
570 if (test_thread_flag(TIF_RESTORE_SIGMASK
))
571 clear_thread_flag(TIF_RESTORE_SIGMASK
);
575 if (restart_syscall
&&
576 (regs
->u_regs
[UREG_I0
] == ERESTARTNOHAND
||
577 regs
->u_regs
[UREG_I0
] == ERESTARTSYS
||
578 regs
->u_regs
[UREG_I0
] == ERESTARTNOINTR
)) {
579 /* replay the system call when we are done */
580 regs
->u_regs
[UREG_I0
] = orig_i0
;
583 pt_regs_clear_syscall(regs
);
585 if (restart_syscall
&&
586 regs
->u_regs
[UREG_I0
] == ERESTART_RESTARTBLOCK
) {
587 regs
->u_regs
[UREG_G1
] = __NR_restart_syscall
;
590 pt_regs_clear_syscall(regs
);
593 /* if there's no signal to deliver, we just put the saved sigmask
596 if (test_thread_flag(TIF_RESTORE_SIGMASK
)) {
597 clear_thread_flag(TIF_RESTORE_SIGMASK
);
598 sigprocmask(SIG_SETMASK
, ¤t
->saved_sigmask
, NULL
);
602 void do_notify_resume(struct pt_regs
*regs
, unsigned long orig_i0
,
603 unsigned long thread_info_flags
)
605 if (thread_info_flags
& (_TIF_SIGPENDING
| _TIF_RESTORE_SIGMASK
))
606 do_signal(regs
, orig_i0
);
607 if (thread_info_flags
& _TIF_NOTIFY_RESUME
) {
608 clear_thread_flag(TIF_NOTIFY_RESUME
);
609 tracehook_notify_resume(regs
);
610 if (current
->replacement_session_keyring
)
611 key_replace_session_keyring();
616 do_sys_sigstack(struct sigstack __user
*ssptr
, struct sigstack __user
*ossptr
,
621 /* First see if old state is wanted. */
623 if (put_user(current
->sas_ss_sp
+ current
->sas_ss_size
,
624 &ossptr
->the_stack
) ||
625 __put_user(on_sig_stack(sp
), &ossptr
->cur_status
))
629 /* Now see if we want to update the new state. */
633 if (get_user(ss_sp
, &ssptr
->the_stack
))
635 /* If the current stack was set with sigaltstack, don't
636 swap stacks while we are on it. */
638 if (current
->sas_ss_sp
&& on_sig_stack(sp
))
641 /* Since we don't know the extent of the stack, and we don't
642 track onstack-ness, but rather calculate it, we must
643 presume a size. Ho hum this interface is lossy. */
644 current
->sas_ss_sp
= (unsigned long)ss_sp
- SIGSTKSZ
;
645 current
->sas_ss_size
= SIGSTKSZ
;