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>
22 #include <asm/uaccess.h>
23 #include <asm/ptrace.h>
24 #include <asm/pgalloc.h>
25 #include <asm/pgtable.h>
26 #include <asm/cacheflush.h> /* flush_sig_insns */
28 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
30 extern void fpsave(unsigned long *fpregs
, unsigned long *fsr
,
31 void *fpqueue
, unsigned long *fpqdepth
);
32 extern void fpload(unsigned long *fpregs
, unsigned long *fsr
);
35 struct sparc_stackf ss
;
37 __siginfo_fpu_t __user
*fpu_save
;
38 unsigned long insns
[2] __attribute__ ((aligned (8)));
39 unsigned int extramask
[_NSIG_WORDS
- 1];
40 unsigned int extra_size
; /* Should be 0 */
41 __siginfo_fpu_t fpu_state
;
44 struct rt_signal_frame
{
45 struct sparc_stackf ss
;
49 __siginfo_fpu_t __user
*fpu_save
;
50 unsigned int insns
[2];
52 unsigned int extra_size
; /* Should be 0 */
53 __siginfo_fpu_t fpu_state
;
57 #define SF_ALIGNEDSZ (((sizeof(struct signal_frame) + 7) & (~7)))
58 #define RT_ALIGNEDSZ (((sizeof(struct rt_signal_frame) + 7) & (~7)))
60 static int _sigpause_common(old_sigset_t set
)
63 spin_lock_irq(¤t
->sighand
->siglock
);
64 current
->saved_sigmask
= current
->blocked
;
65 siginitset(¤t
->blocked
, set
);
67 spin_unlock_irq(¤t
->sighand
->siglock
);
69 current
->state
= TASK_INTERRUPTIBLE
;
71 set_thread_flag(TIF_RESTORE_SIGMASK
);
73 return -ERESTARTNOHAND
;
76 asmlinkage
int sys_sigsuspend(old_sigset_t set
)
78 return _sigpause_common(set
);
82 restore_fpu_state(struct pt_regs
*regs
, __siginfo_fpu_t __user
*fpu
)
86 if (test_tsk_thread_flag(current
, TIF_USEDFPU
))
89 if (current
== last_task_used_math
) {
90 last_task_used_math
= NULL
;
95 clear_tsk_thread_flag(current
, TIF_USEDFPU
);
97 if (!access_ok(VERIFY_READ
, fpu
, sizeof(*fpu
)))
100 err
= __copy_from_user(¤t
->thread
.float_regs
[0], &fpu
->si_float_regs
[0],
101 (sizeof(unsigned long) * 32));
102 err
|= __get_user(current
->thread
.fsr
, &fpu
->si_fsr
);
103 err
|= __get_user(current
->thread
.fpqdepth
, &fpu
->si_fpqdepth
);
104 if (current
->thread
.fpqdepth
!= 0)
105 err
|= __copy_from_user(¤t
->thread
.fpqueue
[0],
107 ((sizeof(unsigned long) +
108 (sizeof(unsigned long *)))*16));
112 asmlinkage
void do_sigreturn(struct pt_regs
*regs
)
114 struct signal_frame __user
*sf
;
115 unsigned long up_psr
, pc
, npc
;
117 __siginfo_fpu_t __user
*fpu_save
;
120 /* Always make any pending restarted system calls return -EINTR */
121 current_thread_info()->restart_block
.fn
= do_no_restart_syscall
;
123 synchronize_user_stack();
125 sf
= (struct signal_frame __user
*) regs
->u_regs
[UREG_FP
];
127 /* 1. Make sure we are not getting garbage from the user */
128 if (!access_ok(VERIFY_READ
, sf
, sizeof(*sf
)))
131 if (((unsigned long) sf
) & 3)
134 err
= __get_user(pc
, &sf
->info
.si_regs
.pc
);
135 err
|= __get_user(npc
, &sf
->info
.si_regs
.npc
);
140 /* 2. Restore the state */
142 err
|= __copy_from_user(regs
, &sf
->info
.si_regs
, sizeof(struct pt_regs
));
144 /* User can only change condition codes and FPU enabling in %psr. */
145 regs
->psr
= (up_psr
& ~(PSR_ICC
| PSR_EF
))
146 | (regs
->psr
& (PSR_ICC
| PSR_EF
));
148 /* Prevent syscall restart. */
149 pt_regs_clear_syscall(regs
);
151 err
|= __get_user(fpu_save
, &sf
->fpu_save
);
154 err
|= restore_fpu_state(regs
, fpu_save
);
156 /* This is pretty much atomic, no amount locking would prevent
157 * the races which exist anyways.
159 err
|= __get_user(set
.sig
[0], &sf
->info
.si_mask
);
160 err
|= __copy_from_user(&set
.sig
[1], &sf
->extramask
,
161 (_NSIG_WORDS
-1) * sizeof(unsigned int));
166 sigdelsetmask(&set
, ~_BLOCKABLE
);
167 spin_lock_irq(¤t
->sighand
->siglock
);
168 current
->blocked
= set
;
170 spin_unlock_irq(¤t
->sighand
->siglock
);
174 force_sig(SIGSEGV
, current
);
177 asmlinkage
void do_rt_sigreturn(struct pt_regs
*regs
)
179 struct rt_signal_frame __user
*sf
;
180 unsigned int psr
, pc
, npc
;
181 __siginfo_fpu_t __user
*fpu_save
;
187 synchronize_user_stack();
188 sf
= (struct rt_signal_frame __user
*) regs
->u_regs
[UREG_FP
];
189 if (!access_ok(VERIFY_READ
, sf
, sizeof(*sf
)) ||
190 (((unsigned long) sf
) & 0x03))
193 err
= __get_user(pc
, &sf
->regs
.pc
);
194 err
|= __get_user(npc
, &sf
->regs
.npc
);
195 err
|= ((pc
| npc
) & 0x03);
197 err
|= __get_user(regs
->y
, &sf
->regs
.y
);
198 err
|= __get_user(psr
, &sf
->regs
.psr
);
200 err
|= __copy_from_user(®s
->u_regs
[UREG_G1
],
201 &sf
->regs
.u_regs
[UREG_G1
], 15 * sizeof(u32
));
203 regs
->psr
= (regs
->psr
& ~PSR_ICC
) | (psr
& PSR_ICC
);
205 /* Prevent syscall restart. */
206 pt_regs_clear_syscall(regs
);
208 err
|= __get_user(fpu_save
, &sf
->fpu_save
);
211 err
|= restore_fpu_state(regs
, fpu_save
);
212 err
|= __copy_from_user(&set
, &sf
->mask
, sizeof(sigset_t
));
214 err
|= __copy_from_user(&st
, &sf
->stack
, sizeof(stack_t
));
222 /* It is more difficult to avoid calling this function than to
223 * call it and ignore errors.
227 do_sigaltstack((const stack_t __user
*) &st
, NULL
, (unsigned long)sf
);
230 sigdelsetmask(&set
, ~_BLOCKABLE
);
231 spin_lock_irq(¤t
->sighand
->siglock
);
232 current
->blocked
= set
;
234 spin_unlock_irq(¤t
->sighand
->siglock
);
237 force_sig(SIGSEGV
, current
);
240 /* Checks if the fp is valid */
241 static inline int invalid_frame_pointer(void __user
*fp
, int fplen
)
243 if ((((unsigned long) fp
) & 7) ||
244 !__access_ok((unsigned long)fp
, fplen
) ||
245 ((sparc_cpu_model
== sun4
|| sparc_cpu_model
== sun4c
) &&
246 ((unsigned long) fp
< 0xe0000000 && (unsigned long) fp
>= 0x20000000)))
252 static inline void __user
*get_sigframe(struct sigaction
*sa
, struct pt_regs
*regs
, unsigned long framesize
)
254 unsigned long sp
= regs
->u_regs
[UREG_FP
];
257 * If we are on the alternate signal stack and would overflow it, don't.
258 * Return an always-bogus address instead so we will die with SIGSEGV.
260 if (on_sig_stack(sp
) && !likely(on_sig_stack(sp
- framesize
)))
261 return (void __user
*) -1L;
263 /* This is the X/Open sanctioned signal stack switching. */
264 if (sa
->sa_flags
& SA_ONSTACK
) {
265 if (sas_ss_flags(sp
) == 0)
266 sp
= current
->sas_ss_sp
+ current
->sas_ss_size
;
269 /* Always align the stack frame. This handles two cases. First,
270 * sigaltstack need not be mindful of platform specific stack
271 * alignment. Second, if we took this signal because the stack
272 * is not aligned properly, we'd like to take the signal cleanly
277 return (void __user
*)(sp
- framesize
);
281 save_fpu_state(struct pt_regs
*regs
, __siginfo_fpu_t __user
*fpu
)
285 if (test_tsk_thread_flag(current
, TIF_USEDFPU
)) {
286 put_psr(get_psr() | PSR_EF
);
287 fpsave(¤t
->thread
.float_regs
[0], ¤t
->thread
.fsr
,
288 ¤t
->thread
.fpqueue
[0], ¤t
->thread
.fpqdepth
);
289 regs
->psr
&= ~(PSR_EF
);
290 clear_tsk_thread_flag(current
, TIF_USEDFPU
);
293 if (current
== last_task_used_math
) {
294 put_psr(get_psr() | PSR_EF
);
295 fpsave(¤t
->thread
.float_regs
[0], ¤t
->thread
.fsr
,
296 ¤t
->thread
.fpqueue
[0], ¤t
->thread
.fpqdepth
);
297 last_task_used_math
= NULL
;
298 regs
->psr
&= ~(PSR_EF
);
301 err
|= __copy_to_user(&fpu
->si_float_regs
[0],
302 ¤t
->thread
.float_regs
[0],
303 (sizeof(unsigned long) * 32));
304 err
|= __put_user(current
->thread
.fsr
, &fpu
->si_fsr
);
305 err
|= __put_user(current
->thread
.fpqdepth
, &fpu
->si_fpqdepth
);
306 if (current
->thread
.fpqdepth
!= 0)
307 err
|= __copy_to_user(&fpu
->si_fpqueue
[0],
308 ¤t
->thread
.fpqueue
[0],
309 ((sizeof(unsigned long) +
310 (sizeof(unsigned long *)))*16));
315 static void setup_frame(struct k_sigaction
*ka
, struct pt_regs
*regs
,
316 int signo
, sigset_t
*oldset
)
318 struct signal_frame __user
*sf
;
319 int sigframe_size
, err
;
321 /* 1. Make sure everything is clean */
322 synchronize_user_stack();
324 sigframe_size
= SF_ALIGNEDSZ
;
326 sigframe_size
-= sizeof(__siginfo_fpu_t
);
328 sf
= (struct signal_frame __user
*)
329 get_sigframe(&ka
->sa
, regs
, sigframe_size
);
331 if (invalid_frame_pointer(sf
, sigframe_size
))
332 goto sigill_and_return
;
334 if (current_thread_info()->w_saved
!= 0)
335 goto sigill_and_return
;
337 /* 2. Save the current process state */
338 err
= __copy_to_user(&sf
->info
.si_regs
, regs
, sizeof(struct pt_regs
));
340 err
|= __put_user(0, &sf
->extra_size
);
343 err
|= save_fpu_state(regs
, &sf
->fpu_state
);
344 err
|= __put_user(&sf
->fpu_state
, &sf
->fpu_save
);
346 err
|= __put_user(0, &sf
->fpu_save
);
349 err
|= __put_user(oldset
->sig
[0], &sf
->info
.si_mask
);
350 err
|= __copy_to_user(sf
->extramask
, &oldset
->sig
[1],
351 (_NSIG_WORDS
- 1) * sizeof(unsigned int));
352 err
|= __copy_to_user(sf
, (char *) regs
->u_regs
[UREG_FP
],
353 sizeof(struct reg_window
));
357 /* 3. signal handler back-trampoline and parameters */
358 regs
->u_regs
[UREG_FP
] = (unsigned long) sf
;
359 regs
->u_regs
[UREG_I0
] = signo
;
360 regs
->u_regs
[UREG_I1
] = (unsigned long) &sf
->info
;
361 regs
->u_regs
[UREG_I2
] = (unsigned long) &sf
->info
;
363 /* 4. signal handler */
364 regs
->pc
= (unsigned long) ka
->sa
.sa_handler
;
365 regs
->npc
= (regs
->pc
+ 4);
367 /* 5. return to kernel instructions */
369 regs
->u_regs
[UREG_I7
] = (unsigned long)ka
->ka_restorer
;
371 regs
->u_regs
[UREG_I7
] = (unsigned long)(&(sf
->insns
[0]) - 2);
373 /* mov __NR_sigreturn, %g1 */
374 err
|= __put_user(0x821020d8, &sf
->insns
[0]);
377 err
|= __put_user(0x91d02010, &sf
->insns
[1]);
381 /* Flush instruction space. */
382 flush_sig_insns(current
->mm
, (unsigned long) &(sf
->insns
[0]));
389 force_sigsegv(signo
, current
);
392 static void setup_rt_frame(struct k_sigaction
*ka
, struct pt_regs
*regs
,
393 int signo
, sigset_t
*oldset
, siginfo_t
*info
)
395 struct rt_signal_frame __user
*sf
;
400 synchronize_user_stack();
401 sigframe_size
= RT_ALIGNEDSZ
;
403 sigframe_size
-= sizeof(__siginfo_fpu_t
);
404 sf
= (struct rt_signal_frame __user
*)
405 get_sigframe(&ka
->sa
, regs
, sigframe_size
);
406 if (invalid_frame_pointer(sf
, sigframe_size
))
408 if (current_thread_info()->w_saved
!= 0)
411 err
= __put_user(regs
->pc
, &sf
->regs
.pc
);
412 err
|= __put_user(regs
->npc
, &sf
->regs
.npc
);
413 err
|= __put_user(regs
->y
, &sf
->regs
.y
);
417 err
|= __put_user(psr
, &sf
->regs
.psr
);
418 err
|= __copy_to_user(&sf
->regs
.u_regs
, regs
->u_regs
, sizeof(regs
->u_regs
));
419 err
|= __put_user(0, &sf
->extra_size
);
422 err
|= save_fpu_state(regs
, &sf
->fpu_state
);
423 err
|= __put_user(&sf
->fpu_state
, &sf
->fpu_save
);
425 err
|= __put_user(0, &sf
->fpu_save
);
427 err
|= __copy_to_user(&sf
->mask
, &oldset
->sig
[0], sizeof(sigset_t
));
429 /* Setup sigaltstack */
430 err
|= __put_user(current
->sas_ss_sp
, &sf
->stack
.ss_sp
);
431 err
|= __put_user(sas_ss_flags(regs
->u_regs
[UREG_FP
]), &sf
->stack
.ss_flags
);
432 err
|= __put_user(current
->sas_ss_size
, &sf
->stack
.ss_size
);
434 err
|= __copy_to_user(sf
, (char *) regs
->u_regs
[UREG_FP
],
435 sizeof(struct reg_window
));
437 err
|= copy_siginfo_to_user(&sf
->info
, info
);
442 regs
->u_regs
[UREG_FP
] = (unsigned long) sf
;
443 regs
->u_regs
[UREG_I0
] = signo
;
444 regs
->u_regs
[UREG_I1
] = (unsigned long) &sf
->info
;
445 regs
->u_regs
[UREG_I2
] = (unsigned long) &sf
->regs
;
447 regs
->pc
= (unsigned long) ka
->sa
.sa_handler
;
448 regs
->npc
= (regs
->pc
+ 4);
451 regs
->u_regs
[UREG_I7
] = (unsigned long)ka
->ka_restorer
;
453 regs
->u_regs
[UREG_I7
] = (unsigned long)(&(sf
->insns
[0]) - 2);
455 /* mov __NR_sigreturn, %g1 */
456 err
|= __put_user(0x821020d8, &sf
->insns
[0]);
459 err
|= __put_user(0x91d02010, &sf
->insns
[1]);
463 /* Flush instruction space. */
464 flush_sig_insns(current
->mm
, (unsigned long) &(sf
->insns
[0]));
471 force_sigsegv(signo
, current
);
475 handle_signal(unsigned long signr
, struct k_sigaction
*ka
,
476 siginfo_t
*info
, sigset_t
*oldset
, struct pt_regs
*regs
)
478 if (ka
->sa
.sa_flags
& SA_SIGINFO
)
479 setup_rt_frame(ka
, regs
, signr
, oldset
, info
);
481 setup_frame(ka
, regs
, signr
, oldset
);
483 spin_lock_irq(¤t
->sighand
->siglock
);
484 sigorsets(¤t
->blocked
,¤t
->blocked
,&ka
->sa
.sa_mask
);
485 if (!(ka
->sa
.sa_flags
& SA_NOMASK
))
486 sigaddset(¤t
->blocked
, signr
);
488 spin_unlock_irq(¤t
->sighand
->siglock
);
491 static inline void syscall_restart(unsigned long orig_i0
, struct pt_regs
*regs
,
492 struct sigaction
*sa
)
494 switch(regs
->u_regs
[UREG_I0
]) {
495 case ERESTART_RESTARTBLOCK
:
497 no_system_call_restart
:
498 regs
->u_regs
[UREG_I0
] = EINTR
;
502 if (!(sa
->sa_flags
& SA_RESTART
))
503 goto no_system_call_restart
;
506 regs
->u_regs
[UREG_I0
] = orig_i0
;
512 /* Note that 'init' is a special process: it doesn't get signals it doesn't
513 * want to handle. Thus you cannot kill init even with a SIGKILL even by
516 asmlinkage
void do_signal(struct pt_regs
* regs
, unsigned long orig_i0
)
518 struct k_sigaction ka
;
524 if (pt_regs_is_syscall(regs
) && (regs
->psr
& PSR_C
))
529 if (test_thread_flag(TIF_RESTORE_SIGMASK
))
530 oldset
= ¤t
->saved_sigmask
;
532 oldset
= ¤t
->blocked
;
534 signr
= get_signal_to_deliver(&info
, &ka
, regs
, NULL
);
536 /* If the debugger messes with the program counter, it clears
537 * the software "in syscall" bit, directing us to not perform
540 if (restart_syscall
&& !pt_regs_is_syscall(regs
))
545 syscall_restart(orig_i0
, regs
, &ka
.sa
);
546 handle_signal(signr
, &ka
, &info
, oldset
, regs
);
548 /* a signal was successfully delivered; the saved
549 * sigmask will have been stored in the signal frame,
550 * and will be restored by sigreturn, so we can simply
551 * clear the TIF_RESTORE_SIGMASK flag.
553 if (test_thread_flag(TIF_RESTORE_SIGMASK
))
554 clear_thread_flag(TIF_RESTORE_SIGMASK
);
557 if (restart_syscall
&&
558 (regs
->u_regs
[UREG_I0
] == ERESTARTNOHAND
||
559 regs
->u_regs
[UREG_I0
] == ERESTARTSYS
||
560 regs
->u_regs
[UREG_I0
] == ERESTARTNOINTR
)) {
561 /* replay the system call when we are done */
562 regs
->u_regs
[UREG_I0
] = orig_i0
;
566 if (restart_syscall
&&
567 regs
->u_regs
[UREG_I0
] == ERESTART_RESTARTBLOCK
) {
568 regs
->u_regs
[UREG_G1
] = __NR_restart_syscall
;
573 /* if there's no signal to deliver, we just put the saved sigmask
576 if (test_thread_flag(TIF_RESTORE_SIGMASK
)) {
577 clear_thread_flag(TIF_RESTORE_SIGMASK
);
578 sigprocmask(SIG_SETMASK
, ¤t
->saved_sigmask
, NULL
);
583 do_sys_sigstack(struct sigstack __user
*ssptr
, struct sigstack __user
*ossptr
,
588 /* First see if old state is wanted. */
590 if (put_user(current
->sas_ss_sp
+ current
->sas_ss_size
,
591 &ossptr
->the_stack
) ||
592 __put_user(on_sig_stack(sp
), &ossptr
->cur_status
))
596 /* Now see if we want to update the new state. */
600 if (get_user(ss_sp
, &ssptr
->the_stack
))
602 /* If the current stack was set with sigaltstack, don't
603 swap stacks while we are on it. */
605 if (current
->sas_ss_sp
&& on_sig_stack(sp
))
608 /* Since we don't know the extent of the stack, and we don't
609 track onstack-ness, but rather calculate it, we must
610 presume a size. Ho hum this interface is lossy. */
611 current
->sas_ss_sp
= (unsigned long)ss_sp
- SIGSTKSZ
;
612 current
->sas_ss_size
= SIGSTKSZ
;