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 */
31 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
33 extern void fpsave(unsigned long *fpregs
, unsigned long *fsr
,
34 void *fpqueue
, unsigned long *fpqdepth
);
35 extern void fpload(unsigned long *fpregs
, unsigned long *fsr
);
38 struct sparc_stackf ss
;
40 __siginfo_fpu_t __user
*fpu_save
;
41 unsigned long insns
[2] __attribute__ ((aligned (8)));
42 unsigned int extramask
[_NSIG_WORDS
- 1];
43 unsigned int extra_size
; /* Should be 0 */
44 __siginfo_rwin_t __user
*rwin_save
;
45 } __attribute__((aligned(8)));
47 struct rt_signal_frame
{
48 struct sparc_stackf ss
;
52 __siginfo_fpu_t __user
*fpu_save
;
53 unsigned int insns
[2];
55 unsigned int extra_size
; /* Should be 0 */
56 __siginfo_rwin_t __user
*rwin_save
;
57 } __attribute__((aligned(8)));
60 #define SF_ALIGNEDSZ (((sizeof(struct signal_frame) + 7) & (~7)))
61 #define RT_ALIGNEDSZ (((sizeof(struct rt_signal_frame) + 7) & (~7)))
63 static int _sigpause_common(old_sigset_t set
)
67 current
->saved_sigmask
= current
->blocked
;
70 siginitset(&blocked
, set
);
71 set_current_blocked(&blocked
);
73 current
->state
= TASK_INTERRUPTIBLE
;
75 set_thread_flag(TIF_RESTORE_SIGMASK
);
77 return -ERESTARTNOHAND
;
80 asmlinkage
int sys_sigsuspend(old_sigset_t set
)
82 return _sigpause_common(set
);
85 asmlinkage
void do_sigreturn(struct pt_regs
*regs
)
87 struct signal_frame __user
*sf
;
88 unsigned long up_psr
, pc
, npc
;
90 __siginfo_fpu_t __user
*fpu_save
;
91 __siginfo_rwin_t __user
*rwin_save
;
94 /* Always make any pending restarted system calls return -EINTR */
95 current_thread_info()->restart_block
.fn
= do_no_restart_syscall
;
97 synchronize_user_stack();
99 sf
= (struct signal_frame __user
*) regs
->u_regs
[UREG_FP
];
101 /* 1. Make sure we are not getting garbage from the user */
102 if (!access_ok(VERIFY_READ
, sf
, sizeof(*sf
)))
105 if (((unsigned long) sf
) & 3)
108 err
= __get_user(pc
, &sf
->info
.si_regs
.pc
);
109 err
|= __get_user(npc
, &sf
->info
.si_regs
.npc
);
114 /* 2. Restore the state */
116 err
|= __copy_from_user(regs
, &sf
->info
.si_regs
, sizeof(struct pt_regs
));
118 /* User can only change condition codes and FPU enabling in %psr. */
119 regs
->psr
= (up_psr
& ~(PSR_ICC
| PSR_EF
))
120 | (regs
->psr
& (PSR_ICC
| PSR_EF
));
122 /* Prevent syscall restart. */
123 pt_regs_clear_syscall(regs
);
125 err
|= __get_user(fpu_save
, &sf
->fpu_save
);
127 err
|= restore_fpu_state(regs
, fpu_save
);
128 err
|= __get_user(rwin_save
, &sf
->rwin_save
);
130 err
|= restore_rwin_state(rwin_save
);
132 /* This is pretty much atomic, no amount locking would prevent
133 * the races which exist anyways.
135 err
|= __get_user(set
.sig
[0], &sf
->info
.si_mask
);
136 err
|= __copy_from_user(&set
.sig
[1], &sf
->extramask
,
137 (_NSIG_WORDS
-1) * sizeof(unsigned int));
142 sigdelsetmask(&set
, ~_BLOCKABLE
);
143 set_current_blocked(&set
);
147 force_sig(SIGSEGV
, current
);
150 asmlinkage
void do_rt_sigreturn(struct pt_regs
*regs
)
152 struct rt_signal_frame __user
*sf
;
153 unsigned int psr
, pc
, npc
;
154 __siginfo_fpu_t __user
*fpu_save
;
155 __siginfo_rwin_t __user
*rwin_save
;
161 synchronize_user_stack();
162 sf
= (struct rt_signal_frame __user
*) regs
->u_regs
[UREG_FP
];
163 if (!access_ok(VERIFY_READ
, sf
, sizeof(*sf
)) ||
164 (((unsigned long) sf
) & 0x03))
167 err
= __get_user(pc
, &sf
->regs
.pc
);
168 err
|= __get_user(npc
, &sf
->regs
.npc
);
169 err
|= ((pc
| npc
) & 0x03);
171 err
|= __get_user(regs
->y
, &sf
->regs
.y
);
172 err
|= __get_user(psr
, &sf
->regs
.psr
);
174 err
|= __copy_from_user(®s
->u_regs
[UREG_G1
],
175 &sf
->regs
.u_regs
[UREG_G1
], 15 * sizeof(u32
));
177 regs
->psr
= (regs
->psr
& ~PSR_ICC
) | (psr
& PSR_ICC
);
179 /* Prevent syscall restart. */
180 pt_regs_clear_syscall(regs
);
182 err
|= __get_user(fpu_save
, &sf
->fpu_save
);
183 if (!err
&& fpu_save
)
184 err
|= restore_fpu_state(regs
, fpu_save
);
185 err
|= __copy_from_user(&set
, &sf
->mask
, sizeof(sigset_t
));
187 err
|= __copy_from_user(&st
, &sf
->stack
, sizeof(stack_t
));
195 /* It is more difficult to avoid calling this function than to
196 * call it and ignore errors.
200 do_sigaltstack((const stack_t __user
*) &st
, NULL
, (unsigned long)sf
);
203 err
|= __get_user(rwin_save
, &sf
->rwin_save
);
204 if (!err
&& rwin_save
) {
205 if (restore_rwin_state(rwin_save
))
209 sigdelsetmask(&set
, ~_BLOCKABLE
);
210 set_current_blocked(&set
);
213 force_sig(SIGSEGV
, current
);
216 /* Checks if the fp is valid */
217 static inline int invalid_frame_pointer(void __user
*fp
, int fplen
)
219 if ((((unsigned long) fp
) & 7) ||
220 !__access_ok((unsigned long)fp
, fplen
) ||
221 ((sparc_cpu_model
== sun4
|| sparc_cpu_model
== sun4c
) &&
222 ((unsigned long) fp
< 0xe0000000 && (unsigned long) fp
>= 0x20000000)))
228 static inline void __user
*get_sigframe(struct sigaction
*sa
, struct pt_regs
*regs
, unsigned long framesize
)
230 unsigned long sp
= regs
->u_regs
[UREG_FP
];
233 * If we are on the alternate signal stack and would overflow it, don't.
234 * Return an always-bogus address instead so we will die with SIGSEGV.
236 if (on_sig_stack(sp
) && !likely(on_sig_stack(sp
- framesize
)))
237 return (void __user
*) -1L;
239 /* This is the X/Open sanctioned signal stack switching. */
240 if (sa
->sa_flags
& SA_ONSTACK
) {
241 if (sas_ss_flags(sp
) == 0)
242 sp
= current
->sas_ss_sp
+ current
->sas_ss_size
;
247 /* Always align the stack frame. This handles two cases. First,
248 * sigaltstack need not be mindful of platform specific stack
249 * alignment. Second, if we took this signal because the stack
250 * is not aligned properly, we'd like to take the signal cleanly
255 return (void __user
*) sp
;
258 static int setup_frame(struct k_sigaction
*ka
, struct pt_regs
*regs
,
259 int signo
, sigset_t
*oldset
)
261 struct signal_frame __user
*sf
;
262 int sigframe_size
, err
, wsaved
;
265 /* 1. Make sure everything is clean */
266 synchronize_user_stack();
268 wsaved
= current_thread_info()->w_saved
;
270 sigframe_size
= sizeof(*sf
);
272 sigframe_size
+= sizeof(__siginfo_fpu_t
);
274 sigframe_size
+= sizeof(__siginfo_rwin_t
);
276 sf
= (struct signal_frame __user
*)
277 get_sigframe(&ka
->sa
, regs
, sigframe_size
);
279 if (invalid_frame_pointer(sf
, sigframe_size
))
280 goto sigill_and_return
;
284 /* 2. Save the current process state */
285 err
= __copy_to_user(&sf
->info
.si_regs
, regs
, sizeof(struct pt_regs
));
287 err
|= __put_user(0, &sf
->extra_size
);
290 __siginfo_fpu_t __user
*fp
= tail
;
292 err
|= save_fpu_state(regs
, fp
);
293 err
|= __put_user(fp
, &sf
->fpu_save
);
295 err
|= __put_user(0, &sf
->fpu_save
);
298 __siginfo_rwin_t __user
*rwp
= tail
;
299 tail
+= sizeof(*rwp
);
300 err
|= save_rwin_state(wsaved
, rwp
);
301 err
|= __put_user(rwp
, &sf
->rwin_save
);
303 err
|= __put_user(0, &sf
->rwin_save
);
306 err
|= __put_user(oldset
->sig
[0], &sf
->info
.si_mask
);
307 err
|= __copy_to_user(sf
->extramask
, &oldset
->sig
[1],
308 (_NSIG_WORDS
- 1) * sizeof(unsigned int));
310 err
|= __copy_to_user(sf
, (char *) regs
->u_regs
[UREG_FP
],
311 sizeof(struct reg_window32
));
313 struct reg_window32
*rp
;
315 rp
= ¤t_thread_info()->reg_window
[wsaved
- 1];
316 err
|= __copy_to_user(sf
, rp
, sizeof(struct reg_window32
));
321 /* 3. signal handler back-trampoline and parameters */
322 regs
->u_regs
[UREG_FP
] = (unsigned long) sf
;
323 regs
->u_regs
[UREG_I0
] = signo
;
324 regs
->u_regs
[UREG_I1
] = (unsigned long) &sf
->info
;
325 regs
->u_regs
[UREG_I2
] = (unsigned long) &sf
->info
;
327 /* 4. signal handler */
328 regs
->pc
= (unsigned long) ka
->sa
.sa_handler
;
329 regs
->npc
= (regs
->pc
+ 4);
331 /* 5. return to kernel instructions */
333 regs
->u_regs
[UREG_I7
] = (unsigned long)ka
->ka_restorer
;
335 regs
->u_regs
[UREG_I7
] = (unsigned long)(&(sf
->insns
[0]) - 2);
337 /* mov __NR_sigreturn, %g1 */
338 err
|= __put_user(0x821020d8, &sf
->insns
[0]);
341 err
|= __put_user(0x91d02010, &sf
->insns
[1]);
345 /* Flush instruction space. */
346 flush_sig_insns(current
->mm
, (unsigned long) &(sf
->insns
[0]));
355 force_sigsegv(signo
, current
);
359 static int setup_rt_frame(struct k_sigaction
*ka
, struct pt_regs
*regs
,
360 int signo
, sigset_t
*oldset
, siginfo_t
*info
)
362 struct rt_signal_frame __user
*sf
;
363 int sigframe_size
, wsaved
;
368 synchronize_user_stack();
369 wsaved
= current_thread_info()->w_saved
;
370 sigframe_size
= sizeof(*sf
);
372 sigframe_size
+= sizeof(__siginfo_fpu_t
);
374 sigframe_size
+= sizeof(__siginfo_rwin_t
);
375 sf
= (struct rt_signal_frame __user
*)
376 get_sigframe(&ka
->sa
, regs
, sigframe_size
);
377 if (invalid_frame_pointer(sf
, sigframe_size
))
381 err
= __put_user(regs
->pc
, &sf
->regs
.pc
);
382 err
|= __put_user(regs
->npc
, &sf
->regs
.npc
);
383 err
|= __put_user(regs
->y
, &sf
->regs
.y
);
387 err
|= __put_user(psr
, &sf
->regs
.psr
);
388 err
|= __copy_to_user(&sf
->regs
.u_regs
, regs
->u_regs
, sizeof(regs
->u_regs
));
389 err
|= __put_user(0, &sf
->extra_size
);
392 __siginfo_fpu_t
*fp
= tail
;
394 err
|= save_fpu_state(regs
, fp
);
395 err
|= __put_user(fp
, &sf
->fpu_save
);
397 err
|= __put_user(0, &sf
->fpu_save
);
400 __siginfo_rwin_t
*rwp
= tail
;
401 tail
+= sizeof(*rwp
);
402 err
|= save_rwin_state(wsaved
, rwp
);
403 err
|= __put_user(rwp
, &sf
->rwin_save
);
405 err
|= __put_user(0, &sf
->rwin_save
);
407 err
|= __copy_to_user(&sf
->mask
, &oldset
->sig
[0], sizeof(sigset_t
));
409 /* Setup sigaltstack */
410 err
|= __put_user(current
->sas_ss_sp
, &sf
->stack
.ss_sp
);
411 err
|= __put_user(sas_ss_flags(regs
->u_regs
[UREG_FP
]), &sf
->stack
.ss_flags
);
412 err
|= __put_user(current
->sas_ss_size
, &sf
->stack
.ss_size
);
415 err
|= __copy_to_user(sf
, (char *) regs
->u_regs
[UREG_FP
],
416 sizeof(struct reg_window32
));
418 struct reg_window32
*rp
;
420 rp
= ¤t_thread_info()->reg_window
[wsaved
- 1];
421 err
|= __copy_to_user(sf
, rp
, sizeof(struct reg_window32
));
424 err
|= copy_siginfo_to_user(&sf
->info
, info
);
429 regs
->u_regs
[UREG_FP
] = (unsigned long) sf
;
430 regs
->u_regs
[UREG_I0
] = signo
;
431 regs
->u_regs
[UREG_I1
] = (unsigned long) &sf
->info
;
432 regs
->u_regs
[UREG_I2
] = (unsigned long) &sf
->regs
;
434 regs
->pc
= (unsigned long) ka
->sa
.sa_handler
;
435 regs
->npc
= (regs
->pc
+ 4);
438 regs
->u_regs
[UREG_I7
] = (unsigned long)ka
->ka_restorer
;
440 regs
->u_regs
[UREG_I7
] = (unsigned long)(&(sf
->insns
[0]) - 2);
442 /* mov __NR_sigreturn, %g1 */
443 err
|= __put_user(0x821020d8, &sf
->insns
[0]);
446 err
|= __put_user(0x91d02010, &sf
->insns
[1]);
450 /* Flush instruction space. */
451 flush_sig_insns(current
->mm
, (unsigned long) &(sf
->insns
[0]));
460 force_sigsegv(signo
, current
);
465 handle_signal(unsigned long signr
, struct k_sigaction
*ka
,
466 siginfo_t
*info
, sigset_t
*oldset
, struct pt_regs
*regs
)
470 if (ka
->sa
.sa_flags
& SA_SIGINFO
)
471 err
= setup_rt_frame(ka
, regs
, signr
, oldset
, info
);
473 err
= setup_frame(ka
, regs
, signr
, oldset
);
478 block_sigmask(ka
, signr
);
480 tracehook_signal_handler(signr
, info
, ka
, regs
, 0);
485 static inline void syscall_restart(unsigned long orig_i0
, struct pt_regs
*regs
,
486 struct sigaction
*sa
)
488 switch(regs
->u_regs
[UREG_I0
]) {
489 case ERESTART_RESTARTBLOCK
:
491 no_system_call_restart
:
492 regs
->u_regs
[UREG_I0
] = EINTR
;
496 if (!(sa
->sa_flags
& SA_RESTART
))
497 goto no_system_call_restart
;
500 regs
->u_regs
[UREG_I0
] = orig_i0
;
506 /* Note that 'init' is a special process: it doesn't get signals it doesn't
507 * want to handle. Thus you cannot kill init even with a SIGKILL even by
510 static void do_signal(struct pt_regs
*regs
, unsigned long orig_i0
)
512 struct k_sigaction ka
;
518 if (pt_regs_is_syscall(regs
) && (regs
->psr
& PSR_C
))
523 if (test_thread_flag(TIF_RESTORE_SIGMASK
))
524 oldset
= ¤t
->saved_sigmask
;
526 oldset
= ¤t
->blocked
;
528 signr
= get_signal_to_deliver(&info
, &ka
, regs
, NULL
);
530 /* If the debugger messes with the program counter, it clears
531 * the software "in syscall" bit, directing us to not perform
534 if (restart_syscall
&& !pt_regs_is_syscall(regs
))
539 syscall_restart(orig_i0
, regs
, &ka
.sa
);
540 if (handle_signal(signr
, &ka
, &info
, oldset
, regs
) == 0) {
541 /* a signal was successfully delivered; the saved
542 * sigmask will have been stored in the signal frame,
543 * and will be restored by sigreturn, so we can simply
544 * clear the TIF_RESTORE_SIGMASK flag.
546 if (test_thread_flag(TIF_RESTORE_SIGMASK
))
547 clear_thread_flag(TIF_RESTORE_SIGMASK
);
551 if (restart_syscall
&&
552 (regs
->u_regs
[UREG_I0
] == ERESTARTNOHAND
||
553 regs
->u_regs
[UREG_I0
] == ERESTARTSYS
||
554 regs
->u_regs
[UREG_I0
] == ERESTARTNOINTR
)) {
555 /* replay the system call when we are done */
556 regs
->u_regs
[UREG_I0
] = orig_i0
;
559 pt_regs_clear_syscall(regs
);
561 if (restart_syscall
&&
562 regs
->u_regs
[UREG_I0
] == ERESTART_RESTARTBLOCK
) {
563 regs
->u_regs
[UREG_G1
] = __NR_restart_syscall
;
566 pt_regs_clear_syscall(regs
);
569 /* if there's no signal to deliver, we just put the saved sigmask
572 if (test_thread_flag(TIF_RESTORE_SIGMASK
)) {
573 clear_thread_flag(TIF_RESTORE_SIGMASK
);
574 sigprocmask(SIG_SETMASK
, ¤t
->saved_sigmask
, NULL
);
578 void do_notify_resume(struct pt_regs
*regs
, unsigned long orig_i0
,
579 unsigned long thread_info_flags
)
581 if (thread_info_flags
& (_TIF_SIGPENDING
| _TIF_RESTORE_SIGMASK
))
582 do_signal(regs
, orig_i0
);
583 if (thread_info_flags
& _TIF_NOTIFY_RESUME
) {
584 clear_thread_flag(TIF_NOTIFY_RESUME
);
585 tracehook_notify_resume(regs
);
586 if (current
->replacement_session_keyring
)
587 key_replace_session_keyring();
592 do_sys_sigstack(struct sigstack __user
*ssptr
, struct sigstack __user
*ossptr
,
597 /* First see if old state is wanted. */
599 if (put_user(current
->sas_ss_sp
+ current
->sas_ss_size
,
600 &ossptr
->the_stack
) ||
601 __put_user(on_sig_stack(sp
), &ossptr
->cur_status
))
605 /* Now see if we want to update the new state. */
609 if (get_user(ss_sp
, &ssptr
->the_stack
))
611 /* If the current stack was set with sigaltstack, don't
612 swap stacks while we are on it. */
614 if (current
->sas_ss_sp
&& on_sig_stack(sp
))
617 /* Since we don't know the extent of the stack, and we don't
618 track onstack-ness, but rather calculate it, we must
619 presume a size. Ho hum this interface is lossy. */
620 current
->sas_ss_sp
= (unsigned long)ss_sp
- SIGSTKSZ
;
621 current
->sas_ss_size
= SIGSTKSZ
;